diff --git a/native/jni/init/init.h b/native/jni/init/init.h index eb9b1b810..f9ebf5a56 100644 --- a/native/jni/init/init.h +++ b/native/jni/init/init.h @@ -68,7 +68,7 @@ protected: raw_data config; dev_t system_dev; - void backup_files(); + void backup_files(const char *self_path); void patch_rootdir(); public: SARBase(char *argv[], cmdline *cmd) : MagiskInit(argv, cmd) { diff --git a/native/jni/init/mount.cpp b/native/jni/init/mount.cpp index 72c66268f..f4a770f5d 100644 --- a/native/jni/init/mount.cpp +++ b/native/jni/init/mount.cpp @@ -183,11 +183,11 @@ void RootFSInit::early_mount() { mount_list.emplace_back("/dev/mnt/cache"); } -void SARBase::backup_files() { +void SARBase::backup_files(const char *self_path) { if (access("/overlay.d", F_OK) == 0) cp_afc("/overlay.d", "/dev/overlay.d"); - full_read("/init", self.buf, self.sz); + full_read(self_path, self.buf, self.sz); full_read("/.backup/.magisk", config.buf, config.sz); } @@ -197,7 +197,7 @@ void SARInit::early_mount() { xmount("tmpfs", "/dev", "tmpfs", 0, "mode=755"); mount_list.emplace_back("/dev"); - backup_files(); + backup_files("/init"); LOGD("Cleaning rootfs\n"); int root = xopen("/", O_RDONLY | O_CLOEXEC); @@ -232,7 +232,7 @@ void SARInit::early_mount() { void SecondStageInit::early_mount() { // Early mounts should already be done by first stage init - backup_files(); + backup_files("/system/bin/init"); rm_rf("/system"); rm_rf("/.backup"); rm_rf("/overlay.d"); diff --git a/native/jni/init/rootdir.cpp b/native/jni/init/rootdir.cpp index e7f1655ce..addd637a0 100644 --- a/native/jni/init/rootdir.cpp +++ b/native/jni/init/rootdir.cpp @@ -407,20 +407,24 @@ static void patch_fstab(const string &fstab) { #define FSR "/first_stage_ramdisk" void ABFirstStageInit::prepare() { - auto dir = xopen_dir(FSR); - if (!dir) - return; - string fstab(FSR "/"); - for (dirent *de; (de = xreaddir(dir.get()));) { - if (strstr(de->d_name, "fstab")) { - fstab += de->d_name; - break; - } - } - if (fstab.length() == sizeof(FSR)) - return; + // It is actually possible to NOT have FSR, create it just in case + xmkdir(FSR, 0755); - patch_fstab(fstab); + if (auto dir = xopen_dir(FSR); dir) { + string fstab(FSR "/"); + for (dirent *de; (de = xreaddir(dir.get()));) { + if (strstr(de->d_name, "fstab")) { + fstab += de->d_name; + break; + } + } + if (fstab.length() == sizeof(FSR)) + return; + + patch_fstab(fstab); + } else { + return; + } // Move stuffs for next stage xmkdir(FSR "/system", 0755);