From ff0a76606e350931b6523aa894428011a4beb3ea Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 4 Dec 2020 03:06:21 -0800 Subject: [PATCH] Detect 2SI after system_root mount on legacy SAR --- native/jni/init/init.cpp | 8 ++------ native/jni/init/init.hpp | 21 +++++++-------------- native/jni/init/mount.cpp | 27 +++++++++++++-------------- native/jni/init/twostage.cpp | 2 +- 4 files changed, 23 insertions(+), 35 deletions(-) diff --git a/native/jni/init/init.cpp b/native/jni/init/init.cpp index b5cdf6bea..9a69ef724 100644 --- a/native/jni/init/init.cpp +++ b/native/jni/init/init.cpp @@ -166,18 +166,14 @@ int main(int argc, char *argv[]) { // This will also mount /sys and /proc load_kernel_info(&cmd); - bool two_stage = check_two_stage(); if (cmd.skip_initramfs) { - if (two_stage) - init = new SARFirstStageInit(argv, &cmd); - else - init = new SARInit(argv, &cmd); + init = new SARInit(argv, &cmd); } else { if (cmd.force_normal_boot) init = new FirstStageInit(argv, &cmd); else if (access("/sbin/recovery", F_OK) == 0 || access("/system/bin/recovery", F_OK) == 0) init = new RecoveryInit(argv, &cmd); - else if (two_stage) + else if (check_two_stage()) init = new FirstStageInit(argv, &cmd); else init = new RootFSInit(argv, &cmd); diff --git a/native/jni/init/init.hpp b/native/jni/init/init.hpp index 81fe1bc73..6bcead98e 100644 --- a/native/jni/init/init.hpp +++ b/native/jni/init/init.hpp @@ -128,28 +128,21 @@ public: *************/ class SARInit : public SARBase { -protected: - void early_mount() override; -public: - SARInit(char *argv[], cmdline *cmd) : SARBase(argv, cmd) { - LOGD("%s\n", __FUNCTION__); - }; -}; - -// Special case for legacy SAR on Android 10+ -// Should be followed by normal 2SI SecondStageInit -class SARFirstStageInit : public SARBase { private: - void prepare(); + bool is_two_stage; + void first_stage_prep(); protected: void early_mount() override; public: - SARFirstStageInit(char *argv[], cmdline *cmd) : SARBase(argv, cmd) { + SARInit(char *argv[], cmdline *cmd) : SARBase(argv, cmd), is_two_stage(false) { LOGD("%s\n", __FUNCTION__); }; void start() override { early_mount(); - prepare(); + if (is_two_stage) + first_stage_prep(); + else + patch_rootdir(); exec_init(); } }; diff --git a/native/jni/init/mount.cpp b/native/jni/init/mount.cpp index dd665102e..9671f2263 100644 --- a/native/jni/init/mount.cpp +++ b/native/jni/init/mount.cpp @@ -338,23 +338,22 @@ mount_root: } void SARInit::early_mount() { - // Make dev writable - xmkdir("/dev", 0755); - xmount("tmpfs", "/dev", "tmpfs", 0, "mode=755"); - mount_list.emplace_back("/dev"); - - backup_files(); - - mount_system_root(); - switch_root("/system_root"); - - mount_with_dt(); -} - -void SARFirstStageInit::early_mount() { backup_files(); mount_system_root(); switch_root("/system_root"); + + { + auto init = raw_data::mmap_ro("/init"); + is_two_stage = init.contains("selinux_setup"); + } + + if (!is_two_stage) { + // Make dev writable + xmkdir("/dev", 0755); + xmount("tmpfs", "/dev", "tmpfs", 0, "mode=755"); + mount_list.emplace_back("/dev"); + mount_with_dt(); + } } void SecondStageInit::early_mount() { diff --git a/native/jni/init/twostage.cpp b/native/jni/init/twostage.cpp index e802bdbd5..edc583855 100644 --- a/native/jni/init/twostage.cpp +++ b/native/jni/init/twostage.cpp @@ -139,7 +139,7 @@ void FirstStageInit::prepare() { #define INIT_PATH "/system/bin/init" #define REDIR_PATH "/system/bin/am" -void SARFirstStageInit::prepare() { +void SARInit::first_stage_prep() { int pid = getpid(); xmount("tmpfs", "/dev", "tmpfs", 0, "mode=755");