Move load kernel info out of class

This commit is contained in:
topjohnwu 2019-06-15 22:25:09 -07:00
parent e8d900c58e
commit f1d9015e5f
5 changed files with 31 additions and 29 deletions

View File

@ -76,14 +76,14 @@ static void setup_block(const char *partname, char *block_dev) {
bool MagiskInit::read_dt_fstab(const char *name, char *partname, char *fstype) { bool MagiskInit::read_dt_fstab(const char *name, char *partname, char *fstype) {
char path[128]; char path[128];
int fd; int fd;
sprintf(path, "%s/fstab/%s/dev", cmd.dt_dir, name); sprintf(path, "%s/fstab/%s/dev", cmd->dt_dir, name);
if ((fd = xopen(path, O_RDONLY | O_CLOEXEC)) >= 0) { if ((fd = xopen(path, O_RDONLY | O_CLOEXEC)) >= 0) {
read(fd, path, sizeof(path)); read(fd, path, sizeof(path));
close(fd); close(fd);
// Some custom treble use different names, so use what we read // Some custom treble use different names, so use what we read
char *part = rtrim(strrchr(path, '/') + 1); char *part = rtrim(strrchr(path, '/') + 1);
sprintf(partname, "%s%s", part, strend(part, cmd.slot) ? cmd.slot : ""); sprintf(partname, "%s%s", part, strend(part, cmd->slot) ? cmd->slot : "");
sprintf(path, "%s/fstab/%s/type", cmd.dt_dir, name); sprintf(path, "%s/fstab/%s/type", cmd->dt_dir, name);
if ((fd = xopen(path, O_RDONLY | O_CLOEXEC)) >= 0) { if ((fd = xopen(path, O_RDONLY | O_CLOEXEC)) >= 0) {
read(fd, fstype, 32); read(fd, fstype, 32);
close(fd); close(fd);
@ -111,9 +111,9 @@ void MagiskInit::early_mount() {
char fstype[32]; char fstype[32];
char block_dev[64]; char block_dev[64];
if (cmd.system_as_root) { if (cmd->system_as_root) {
LOGD("Early mount system_root\n"); LOGD("Early mount system_root\n");
sprintf(partname, "system%s", cmd.slot); sprintf(partname, "system%s", cmd->slot);
setup_block(partname, block_dev); setup_block(partname, block_dev);
xmkdir("/system_root", 0755); xmkdir("/system_root", 0755);
if (xmount(block_dev, "/system_root", "ext4", MS_RDONLY, nullptr)) if (xmount(block_dev, "/system_root", "ext4", MS_RDONLY, nullptr))

View File

@ -92,7 +92,7 @@ static bool check_key_combo() {
return false; return false;
} }
void MagiskInit::load_kernel_info() { void load_kernel_info(cmdline *cmd) {
// Communicate with kernel using procfs and sysfs // Communicate with kernel using procfs and sysfs
xmkdir("/proc", 0755); xmkdir("/proc", 0755);
xmount("proc", "/proc", "proc", 0, nullptr); xmount("proc", "/proc", "proc", 0, nullptr);
@ -106,14 +106,14 @@ void MagiskInit::load_kernel_info() {
parse_cmdline([&](auto key, auto value) -> void { parse_cmdline([&](auto key, auto value) -> void {
LOGD("cmdline: [%s]=[%s]\n", key.data(), value); LOGD("cmdline: [%s]=[%s]\n", key.data(), value);
if (key == "androidboot.slot_suffix") { if (key == "androidboot.slot_suffix") {
strcpy(cmd.slot, value); strcpy(cmd->slot, value);
} else if (key == "androidboot.slot") { } else if (key == "androidboot.slot") {
cmd.slot[0] = '_'; cmd->slot[0] = '_';
strcpy(cmd.slot + 1, value); strcpy(cmd->slot + 1, value);
} else if (key == "skip_initramfs") { } else if (key == "skip_initramfs") {
cmd.system_as_root = true; cmd->system_as_root = true;
} else if (key == "androidboot.android_dt_dir") { } else if (key == "androidboot.android_dt_dir") {
strcpy(cmd.dt_dir, value); strcpy(cmd->dt_dir, value);
} else if (key == "enter_recovery") { } else if (key == "enter_recovery") {
enter_recovery = value[0] == '1'; enter_recovery = value[0] == '1';
} else if (key == "androidboot.hardware") { } else if (key == "androidboot.hardware") {
@ -140,13 +140,13 @@ void MagiskInit::load_kernel_info() {
if (recovery_mode) { if (recovery_mode) {
LOGD("Running in recovery mode, waiting for key...\n"); LOGD("Running in recovery mode, waiting for key...\n");
cmd.system_as_root = !check_key_combo(); cmd->system_as_root = !check_key_combo();
} }
if (cmd.dt_dir[0] == '\0') if (cmd->dt_dir[0] == '\0')
strcpy(cmd.dt_dir, DEFAULT_DT_DIR); strcpy(cmd->dt_dir, DEFAULT_DT_DIR);
LOGD("system_as_root=[%d]\n", cmd.system_as_root); LOGD("system_as_root=[%d]\n", cmd->system_as_root);
LOGD("slot=[%s]\n", cmd.slot); LOGD("slot=[%s]\n", cmd->slot);
LOGD("dt_dir=[%s]\n", cmd.dt_dir); LOGD("dt_dir=[%s]\n", cmd->dt_dir);
} }

View File

@ -120,7 +120,7 @@ static int dump_manager(const char *path, mode_t mode) {
void MagiskInit::preset() { void MagiskInit::preset() {
root = open("/", O_RDONLY | O_CLOEXEC); root = open("/", O_RDONLY | O_CLOEXEC);
if (cmd.system_as_root) { if (cmd->system_as_root) {
// Clear rootfs // Clear rootfs
LOGD("Cleaning rootfs\n"); LOGD("Cleaning rootfs\n");
frm_rf(root, { "overlay", "proc", "sys" }); frm_rf(root, { "overlay", "proc", "sys" });
@ -171,10 +171,6 @@ void MagiskInit::start() {
if (null > STDERR_FILENO) if (null > STDERR_FILENO)
close(null); close(null);
setup_klog();
load_kernel_info();
full_read("/init", &self.buf, &self.sz); full_read("/init", &self.buf, &self.sz);
full_read("/.backup/.magisk", &config.buf, &config.sz); full_read("/.backup/.magisk", &config.buf, &config.sz);
@ -192,7 +188,6 @@ void MagiskInit::test() {
chroot("."); chroot(".");
chdir("/"); chdir("/");
load_kernel_info();
preset(); preset();
early_mount(); early_mount();
setup_rootfs(); setup_rootfs();
@ -200,7 +195,9 @@ void MagiskInit::test() {
} }
static int test_main(int, char *argv[]) { static int test_main(int, char *argv[]) {
MagiskInit init(argv); cmdline cmd{};
load_kernel_info(&cmd);
MagiskInit init(argv, &cmd);
init.test(); init.test();
return 0; return 0;
} }
@ -223,7 +220,12 @@ int main(int argc, char *argv[]) {
if (getpid() != 1) if (getpid() != 1)
return 1; return 1;
MagiskInit init(argv); setup_klog();
cmdline cmd{};
load_kernel_info(&cmd);
MagiskInit init(argv, &cmd);
// Run the main routine // Run the main routine
init.start(); init.start();

View File

@ -13,7 +13,7 @@ struct raw_data {
class MagiskInit { class MagiskInit {
private: private:
cmdline cmd{}; cmdline *cmd;
raw_data self{}; raw_data self{};
raw_data config{}; raw_data config{};
int root = -1; int root = -1;
@ -24,7 +24,6 @@ private:
bool mnt_product = false; bool mnt_product = false;
bool mnt_odm = false; bool mnt_odm = false;
void load_kernel_info();
void preset(); void preset();
void early_mount(); void early_mount();
void setup_rootfs(); void setup_rootfs();
@ -34,7 +33,7 @@ private:
void re_exec_init(); void re_exec_init();
public: public:
explicit MagiskInit(char *argv[]) : argv(argv) {} explicit MagiskInit(char *argv[], cmdline *cmd) : cmd(cmd), argv(argv) {}
void start(); void start();
void test(); void test();
}; };
@ -46,4 +45,5 @@ static inline bool is_lnk(const char *name) {
return S_ISLNK(st.st_mode); return S_ISLNK(st.st_mode);
} }
void load_kernel_info(cmdline *cmd);
int dump_magisk(const char *path, mode_t mode); int dump_magisk(const char *path, mode_t mode);

View File

@ -40,7 +40,7 @@ constexpr const char wrapper[] =
void MagiskInit::setup_rootfs() { void MagiskInit::setup_rootfs() {
bool patch_init = patch_sepolicy(); bool patch_init = patch_sepolicy();
if (cmd.system_as_root) { if (cmd->system_as_root) {
// Clone rootfs // Clone rootfs
LOGD("Clone root dir from system to rootfs\n"); LOGD("Clone root dir from system to rootfs\n");
int system_root = xopen("/system_root", O_RDONLY | O_CLOEXEC); int system_root = xopen("/system_root", O_RDONLY | O_CLOEXEC);