Detect 2SI after system_root mount on legacy SAR

This commit is contained in:
topjohnwu 2020-12-04 03:06:21 -08:00
parent dead74801d
commit ff0a76606e
4 changed files with 23 additions and 35 deletions

View File

@ -166,18 +166,14 @@ int main(int argc, char *argv[]) {
// This will also mount /sys and /proc // This will also mount /sys and /proc
load_kernel_info(&cmd); load_kernel_info(&cmd);
bool two_stage = check_two_stage();
if (cmd.skip_initramfs) { if (cmd.skip_initramfs) {
if (two_stage) init = new SARInit(argv, &cmd);
init = new SARFirstStageInit(argv, &cmd);
else
init = new SARInit(argv, &cmd);
} else { } else {
if (cmd.force_normal_boot) if (cmd.force_normal_boot)
init = new FirstStageInit(argv, &cmd); init = new FirstStageInit(argv, &cmd);
else if (access("/sbin/recovery", F_OK) == 0 || access("/system/bin/recovery", F_OK) == 0) else if (access("/sbin/recovery", F_OK) == 0 || access("/system/bin/recovery", F_OK) == 0)
init = new RecoveryInit(argv, &cmd); init = new RecoveryInit(argv, &cmd);
else if (two_stage) else if (check_two_stage())
init = new FirstStageInit(argv, &cmd); init = new FirstStageInit(argv, &cmd);
else else
init = new RootFSInit(argv, &cmd); init = new RootFSInit(argv, &cmd);

View File

@ -128,28 +128,21 @@ public:
*************/ *************/
class SARInit : public SARBase { 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: private:
void prepare(); bool is_two_stage;
void first_stage_prep();
protected: protected:
void early_mount() override; void early_mount() override;
public: 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__); LOGD("%s\n", __FUNCTION__);
}; };
void start() override { void start() override {
early_mount(); early_mount();
prepare(); if (is_two_stage)
first_stage_prep();
else
patch_rootdir();
exec_init(); exec_init();
} }
}; };

View File

@ -338,23 +338,22 @@ mount_root:
} }
void SARInit::early_mount() { 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(); backup_files();
mount_system_root(); mount_system_root();
switch_root("/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() { void SecondStageInit::early_mount() {

View File

@ -139,7 +139,7 @@ void FirstStageInit::prepare() {
#define INIT_PATH "/system/bin/init" #define INIT_PATH "/system/bin/init"
#define REDIR_PATH "/system/bin/am" #define REDIR_PATH "/system/bin/am"
void SARFirstStageInit::prepare() { void SARInit::first_stage_prep() {
int pid = getpid(); int pid = getpid();
xmount("tmpfs", "/dev", "tmpfs", 0, "mode=755"); xmount("tmpfs", "/dev", "tmpfs", 0, "mode=755");