From 62ef8ade8f4109d99b256e251d8123c7a110af7b Mon Sep 17 00:00:00 2001 From: LoveSy Date: Wed, 29 Dec 2021 13:04:09 +0800 Subject: [PATCH] Skip loading Magisk when detecting DSU Fix #4402 Co-authored-by: topjohnwu --- native/jni/init/init.hpp | 1 + native/jni/init/mount.cpp | 23 ++++++++++++++++++++++- native/jni/init/twostage.cpp | 6 ++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/native/jni/init/init.hpp b/native/jni/init/init.hpp index 75bc4b474..901b19a10 100644 --- a/native/jni/init/init.hpp +++ b/native/jni/init/init.hpp @@ -38,6 +38,7 @@ extern std::vector mount_list; bool unxz(int fd, const uint8_t *buf, size_t size); void load_kernel_info(BootConfig *config); +bool is_dsu(); bool check_two_stage(); void setup_klog(); const char *backup_init(); diff --git a/native/jni/init/mount.cpp b/native/jni/init/mount.cpp index a355f86ca..58ec5ca59 100644 --- a/native/jni/init/mount.cpp +++ b/native/jni/init/mount.cpp @@ -93,7 +93,7 @@ static int64_t setup_block(bool write_block) { } static bool is_lnk(const char *name) { - struct stat st; + struct stat st{}; if (lstat(name, &st)) return false; return S_ISLNK(st.st_mode); @@ -196,6 +196,27 @@ static void switch_root(const string &path) { frm_rf(root); } +bool is_dsu() { + strcpy(blk_info.partname, "metadata"); + xmkdir("/metadata", 0755); + if (setup_block(true) < 0 || + xmount(blk_info.block_dev, "/metadata", "ext4", MS_RDONLY, nullptr)) { + PLOGE("Failed to mount /metadata"); + return false; + } else { + run_finally f([]{ xumount2("/metadata", MNT_DETACH); }); + constexpr auto dsu_status = "/metadata/gsi/dsu/install_status"; + if (xaccess(dsu_status, F_OK) == 0) { + char status[PATH_MAX] = {0}; + auto fp = xopen_file(dsu_status, "r"); + fgets(status, sizeof(status), fp.get()); + if (status == "ok"sv || status == "0"sv) + return true; + } + } + return false; +} + void MagiskInit::mount_rules_dir(const char *dev_base, const char *mnt_base) { char path[128]; xrealpath(dev_base, blk_info.block_dev); diff --git a/native/jni/init/twostage.cpp b/native/jni/init/twostage.cpp index 132250672..98c276b51 100644 --- a/native/jni/init/twostage.cpp +++ b/native/jni/init/twostage.cpp @@ -63,6 +63,12 @@ static bool read_fstab_file(const char *fstab_file, vector &fstab) extern uint32_t patch_verity(void *buf, uint32_t size); void FirstStageInit::prepare() { + if (is_dsu()) { + rename(backup_init(), "/init"); + LOGI("Skip loading Magisk because of DSU\n"); + return; + } + run_finally finally([]{ chdir("/"); }); if (config->force_normal_boot) { xmkdirs(FSR "/system/bin", 0755);