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

View File

@ -92,7 +92,7 @@ static bool check_key_combo() {
return false;
}
void MagiskInit::load_kernel_info() {
void load_kernel_info(cmdline *cmd) {
// Communicate with kernel using procfs and sysfs
xmkdir("/proc", 0755);
xmount("proc", "/proc", "proc", 0, nullptr);
@ -106,14 +106,14 @@ void MagiskInit::load_kernel_info() {
parse_cmdline([&](auto key, auto value) -> void {
LOGD("cmdline: [%s]=[%s]\n", key.data(), value);
if (key == "androidboot.slot_suffix") {
strcpy(cmd.slot, value);
strcpy(cmd->slot, value);
} else if (key == "androidboot.slot") {
cmd.slot[0] = '_';
strcpy(cmd.slot + 1, value);
cmd->slot[0] = '_';
strcpy(cmd->slot + 1, value);
} else if (key == "skip_initramfs") {
cmd.system_as_root = true;
cmd->system_as_root = true;
} else if (key == "androidboot.android_dt_dir") {
strcpy(cmd.dt_dir, value);
strcpy(cmd->dt_dir, value);
} else if (key == "enter_recovery") {
enter_recovery = value[0] == '1';
} else if (key == "androidboot.hardware") {
@ -140,13 +140,13 @@ void MagiskInit::load_kernel_info() {
if (recovery_mode) {
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')
strcpy(cmd.dt_dir, DEFAULT_DT_DIR);
if (cmd->dt_dir[0] == '\0')
strcpy(cmd->dt_dir, DEFAULT_DT_DIR);
LOGD("system_as_root=[%d]\n", cmd.system_as_root);
LOGD("slot=[%s]\n", cmd.slot);
LOGD("dt_dir=[%s]\n", cmd.dt_dir);
LOGD("system_as_root=[%d]\n", cmd->system_as_root);
LOGD("slot=[%s]\n", cmd->slot);
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() {
root = open("/", O_RDONLY | O_CLOEXEC);
if (cmd.system_as_root) {
if (cmd->system_as_root) {
// Clear rootfs
LOGD("Cleaning rootfs\n");
frm_rf(root, { "overlay", "proc", "sys" });
@ -171,10 +171,6 @@ void MagiskInit::start() {
if (null > STDERR_FILENO)
close(null);
setup_klog();
load_kernel_info();
full_read("/init", &self.buf, &self.sz);
full_read("/.backup/.magisk", &config.buf, &config.sz);
@ -192,7 +188,6 @@ void MagiskInit::test() {
chroot(".");
chdir("/");
load_kernel_info();
preset();
early_mount();
setup_rootfs();
@ -200,7 +195,9 @@ void MagiskInit::test() {
}
static int test_main(int, char *argv[]) {
MagiskInit init(argv);
cmdline cmd{};
load_kernel_info(&cmd);
MagiskInit init(argv, &cmd);
init.test();
return 0;
}
@ -223,7 +220,12 @@ int main(int argc, char *argv[]) {
if (getpid() != 1)
return 1;
MagiskInit init(argv);
setup_klog();
cmdline cmd{};
load_kernel_info(&cmd);
MagiskInit init(argv, &cmd);
// Run the main routine
init.start();

View File

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

View File

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