2020-06-17 10:17:28 +02:00
|
|
|
#include <utils.hpp>
|
2019-12-13 12:05:12 +01:00
|
|
|
|
2020-10-27 04:46:15 +01:00
|
|
|
#include "raw_data.hpp"
|
|
|
|
|
2019-05-27 09:29:43 +02:00
|
|
|
struct cmdline {
|
2020-12-31 07:11:24 +01:00
|
|
|
bool skip_initramfs;
|
|
|
|
bool force_normal_boot;
|
|
|
|
bool rootwait;
|
|
|
|
char slot[3];
|
|
|
|
char dt_dir[64];
|
|
|
|
char fstab_suffix[32];
|
|
|
|
char hardware[32];
|
|
|
|
char hardware_plat[32];
|
2019-05-27 09:29:43 +02:00
|
|
|
};
|
|
|
|
|
2020-05-04 07:49:54 +02:00
|
|
|
struct fstab_entry {
|
2020-12-31 07:11:24 +01:00
|
|
|
std::string dev;
|
|
|
|
std::string mnt_point;
|
|
|
|
std::string type;
|
|
|
|
std::string mnt_flags;
|
|
|
|
std::string fsmgr_flags;
|
|
|
|
|
|
|
|
fstab_entry() = default;
|
|
|
|
fstab_entry(const fstab_entry &) = delete;
|
|
|
|
fstab_entry(fstab_entry &&) = default;
|
|
|
|
void to_file(FILE *fp);
|
2020-05-04 07:49:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#define INIT_SOCKET "MAGISKINIT"
|
|
|
|
#define DEFAULT_DT_DIR "/proc/device-tree/firmware/android"
|
|
|
|
|
2021-01-15 06:14:54 +01:00
|
|
|
extern std::vector<std::string> mount_list;
|
|
|
|
|
2021-01-18 13:25:26 +01:00
|
|
|
bool unxz(int fd, const uint8_t *buf, size_t size);
|
2020-05-04 07:49:54 +02:00
|
|
|
void load_kernel_info(cmdline *cmd);
|
2020-09-03 06:20:00 +02:00
|
|
|
bool check_two_stage();
|
2020-05-04 07:49:54 +02:00
|
|
|
void setup_klog();
|
|
|
|
|
2020-04-19 13:56:56 +02:00
|
|
|
/***************
|
Logical Resizable Android Partitions support
The way how logical partition, or "Logical Resizable Android Partitions"
as they say in AOSP source code, is setup makes it impossible to early
mount the partitions from the shared super partition with just
a few lines of code; in fact, AOSP has a whole "fs_mgr" folder which
consist of multiple complex libraries, with 15K lines of code just
to deal with the device mapper shenanigans.
In order to keep the already overly complicated MagiskInit more
managable, I chose NOT to go the route of including fs_mgr directly
into MagiskInit. Luckily, starting from Android Q, Google decided to
split init startup into 3 stages, with the first stage doing _only_
early mount. This is great news, because we can simply let the stock
init do its own thing for us, and we intercept the bootup sequence.
So the workflow can be visualized roughly below:
Magisk First Stage --> First Stage Mount --> Magisk Second Stage --+
(MagiskInit) (Original Init) (MagiskInit) +
+
+
...Rest of the boot... <-- Second Stage <-- Selinux Setup <--+
(__________________ Original Init ____________________)
The catch here is that after doing all the first stage mounting, /init
will pivot /system as root directory (/), leaving us impossible to
regain control after we hand it over. So the solution here is to patch
fstab in /first_stage_ramdisk on-the-fly to redirect /system to
/system_root, making the original init do all the hard work for
us and mount required early mount partitions, but skips the step of
switching root directory. It will also conveniently hand over execution
back to MagiskInit, which we will reuse the routine for patching
root directory in normal system-as-root situations.
2019-06-29 09:47:29 +02:00
|
|
|
* Base classes
|
2020-04-19 13:56:56 +02:00
|
|
|
***************/
|
Logical Resizable Android Partitions support
The way how logical partition, or "Logical Resizable Android Partitions"
as they say in AOSP source code, is setup makes it impossible to early
mount the partitions from the shared super partition with just
a few lines of code; in fact, AOSP has a whole "fs_mgr" folder which
consist of multiple complex libraries, with 15K lines of code just
to deal with the device mapper shenanigans.
In order to keep the already overly complicated MagiskInit more
managable, I chose NOT to go the route of including fs_mgr directly
into MagiskInit. Luckily, starting from Android Q, Google decided to
split init startup into 3 stages, with the first stage doing _only_
early mount. This is great news, because we can simply let the stock
init do its own thing for us, and we intercept the bootup sequence.
So the workflow can be visualized roughly below:
Magisk First Stage --> First Stage Mount --> Magisk Second Stage --+
(MagiskInit) (Original Init) (MagiskInit) +
+
+
...Rest of the boot... <-- Second Stage <-- Selinux Setup <--+
(__________________ Original Init ____________________)
The catch here is that after doing all the first stage mounting, /init
will pivot /system as root directory (/), leaving us impossible to
regain control after we hand it over. So the solution here is to patch
fstab in /first_stage_ramdisk on-the-fly to redirect /system to
/system_root, making the original init do all the hard work for
us and mount required early mount partitions, but skips the step of
switching root directory. It will also conveniently hand over execution
back to MagiskInit, which we will reuse the routine for patching
root directory in normal system-as-root situations.
2019-06-29 09:47:29 +02:00
|
|
|
|
2019-06-16 21:45:32 +02:00
|
|
|
class BaseInit {
|
|
|
|
protected:
|
2020-12-31 07:11:24 +01:00
|
|
|
cmdline *cmd;
|
|
|
|
char **argv;
|
Logical Resizable Android Partitions support
The way how logical partition, or "Logical Resizable Android Partitions"
as they say in AOSP source code, is setup makes it impossible to early
mount the partitions from the shared super partition with just
a few lines of code; in fact, AOSP has a whole "fs_mgr" folder which
consist of multiple complex libraries, with 15K lines of code just
to deal with the device mapper shenanigans.
In order to keep the already overly complicated MagiskInit more
managable, I chose NOT to go the route of including fs_mgr directly
into MagiskInit. Luckily, starting from Android Q, Google decided to
split init startup into 3 stages, with the first stage doing _only_
early mount. This is great news, because we can simply let the stock
init do its own thing for us, and we intercept the bootup sequence.
So the workflow can be visualized roughly below:
Magisk First Stage --> First Stage Mount --> Magisk Second Stage --+
(MagiskInit) (Original Init) (MagiskInit) +
+
+
...Rest of the boot... <-- Second Stage <-- Selinux Setup <--+
(__________________ Original Init ____________________)
The catch here is that after doing all the first stage mounting, /init
will pivot /system as root directory (/), leaving us impossible to
regain control after we hand it over. So the solution here is to patch
fstab in /first_stage_ramdisk on-the-fly to redirect /system to
/system_root, making the original init do all the hard work for
us and mount required early mount partitions, but skips the step of
switching root directory. It will also conveniently hand over execution
back to MagiskInit, which we will reuse the routine for patching
root directory in normal system-as-root situations.
2019-06-29 09:47:29 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
[[noreturn]] void exec_init();
|
|
|
|
void read_dt_fstab(std::vector<fstab_entry> &fstab);
|
2019-06-22 12:14:33 +02:00
|
|
|
public:
|
2021-01-15 06:14:54 +01:00
|
|
|
BaseInit(char *argv[], cmdline *cmd) : cmd(cmd), argv(argv) {}
|
2020-12-31 07:11:24 +01:00
|
|
|
virtual ~BaseInit() = default;
|
|
|
|
virtual void start() = 0;
|
2019-06-22 12:14:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class MagiskInit : public BaseInit {
|
|
|
|
protected:
|
2021-01-13 07:50:55 +01:00
|
|
|
mmap_data self;
|
|
|
|
mmap_data config;
|
2020-12-31 07:11:24 +01:00
|
|
|
std::string custom_rules_dir;
|
|
|
|
|
|
|
|
void mount_with_dt();
|
|
|
|
bool patch_sepolicy(const char *file);
|
|
|
|
void setup_tmp(const char *path);
|
|
|
|
void mount_rules_dir(const char *dev_base, const char *mnt_base);
|
2019-05-27 09:29:43 +02:00
|
|
|
public:
|
2020-12-31 07:11:24 +01:00
|
|
|
MagiskInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {}
|
2019-06-24 00:14:47 +02:00
|
|
|
};
|
|
|
|
|
2019-09-22 11:20:51 +02:00
|
|
|
class SARBase : public MagiskInit {
|
2019-06-24 00:14:47 +02:00
|
|
|
protected:
|
2020-12-31 07:11:24 +01:00
|
|
|
std::vector<raw_file> overlays;
|
2019-06-24 10:21:33 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
void backup_files();
|
|
|
|
void patch_rootdir();
|
|
|
|
void mount_system_root();
|
2019-06-24 00:14:47 +02:00
|
|
|
public:
|
2020-12-31 07:11:24 +01:00
|
|
|
SARBase(char *argv[], cmdline *cmd) : MagiskInit(argv, cmd) {}
|
2019-06-24 00:14:47 +02:00
|
|
|
};
|
|
|
|
|
2020-04-19 13:56:56 +02:00
|
|
|
/***************
|
2019-09-22 11:15:31 +02:00
|
|
|
* 2 Stage Init
|
2020-04-19 13:56:56 +02:00
|
|
|
***************/
|
2019-06-16 21:45:32 +02:00
|
|
|
|
2020-04-01 13:39:28 +02:00
|
|
|
class FirstStageInit : public BaseInit {
|
2019-12-12 09:25:48 +01:00
|
|
|
private:
|
2020-12-31 07:11:24 +01:00
|
|
|
void prepare();
|
2019-09-22 11:15:31 +02:00
|
|
|
public:
|
2020-12-31 07:11:24 +01:00
|
|
|
FirstStageInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {
|
|
|
|
LOGD("%s\n", __FUNCTION__);
|
|
|
|
};
|
|
|
|
void start() override {
|
|
|
|
prepare();
|
|
|
|
exec_init();
|
|
|
|
}
|
2019-09-22 11:15:31 +02:00
|
|
|
};
|
|
|
|
|
2019-09-22 11:20:51 +02:00
|
|
|
class SecondStageInit : public SARBase {
|
2020-12-06 12:07:47 +01:00
|
|
|
private:
|
2020-12-31 07:11:24 +01:00
|
|
|
void prepare();
|
Logical Resizable Android Partitions support
The way how logical partition, or "Logical Resizable Android Partitions"
as they say in AOSP source code, is setup makes it impossible to early
mount the partitions from the shared super partition with just
a few lines of code; in fact, AOSP has a whole "fs_mgr" folder which
consist of multiple complex libraries, with 15K lines of code just
to deal with the device mapper shenanigans.
In order to keep the already overly complicated MagiskInit more
managable, I chose NOT to go the route of including fs_mgr directly
into MagiskInit. Luckily, starting from Android Q, Google decided to
split init startup into 3 stages, with the first stage doing _only_
early mount. This is great news, because we can simply let the stock
init do its own thing for us, and we intercept the bootup sequence.
So the workflow can be visualized roughly below:
Magisk First Stage --> First Stage Mount --> Magisk Second Stage --+
(MagiskInit) (Original Init) (MagiskInit) +
+
+
...Rest of the boot... <-- Second Stage <-- Selinux Setup <--+
(__________________ Original Init ____________________)
The catch here is that after doing all the first stage mounting, /init
will pivot /system as root directory (/), leaving us impossible to
regain control after we hand it over. So the solution here is to patch
fstab in /first_stage_ramdisk on-the-fly to redirect /system to
/system_root, making the original init do all the hard work for
us and mount required early mount partitions, but skips the step of
switching root directory. It will also conveniently hand over execution
back to MagiskInit, which we will reuse the routine for patching
root directory in normal system-as-root situations.
2019-06-29 09:47:29 +02:00
|
|
|
public:
|
2020-12-31 07:11:24 +01:00
|
|
|
SecondStageInit(char *argv[]) : SARBase(argv, nullptr) {
|
|
|
|
LOGD("%s\n", __FUNCTION__);
|
|
|
|
};
|
|
|
|
void start() override {
|
|
|
|
prepare();
|
|
|
|
patch_rootdir();
|
|
|
|
exec_init();
|
|
|
|
}
|
Logical Resizable Android Partitions support
The way how logical partition, or "Logical Resizable Android Partitions"
as they say in AOSP source code, is setup makes it impossible to early
mount the partitions from the shared super partition with just
a few lines of code; in fact, AOSP has a whole "fs_mgr" folder which
consist of multiple complex libraries, with 15K lines of code just
to deal with the device mapper shenanigans.
In order to keep the already overly complicated MagiskInit more
managable, I chose NOT to go the route of including fs_mgr directly
into MagiskInit. Luckily, starting from Android Q, Google decided to
split init startup into 3 stages, with the first stage doing _only_
early mount. This is great news, because we can simply let the stock
init do its own thing for us, and we intercept the bootup sequence.
So the workflow can be visualized roughly below:
Magisk First Stage --> First Stage Mount --> Magisk Second Stage --+
(MagiskInit) (Original Init) (MagiskInit) +
+
+
...Rest of the boot... <-- Second Stage <-- Selinux Setup <--+
(__________________ Original Init ____________________)
The catch here is that after doing all the first stage mounting, /init
will pivot /system as root directory (/), leaving us impossible to
regain control after we hand it over. So the solution here is to patch
fstab in /first_stage_ramdisk on-the-fly to redirect /system to
/system_root, making the original init do all the hard work for
us and mount required early mount partitions, but skips the step of
switching root directory. It will also conveniently hand over execution
back to MagiskInit, which we will reuse the routine for patching
root directory in normal system-as-root situations.
2019-06-29 09:47:29 +02:00
|
|
|
};
|
|
|
|
|
2020-04-19 13:56:56 +02:00
|
|
|
/*************
|
2019-09-22 11:15:31 +02:00
|
|
|
* Legacy SAR
|
2020-04-19 13:56:56 +02:00
|
|
|
*************/
|
Logical Resizable Android Partitions support
The way how logical partition, or "Logical Resizable Android Partitions"
as they say in AOSP source code, is setup makes it impossible to early
mount the partitions from the shared super partition with just
a few lines of code; in fact, AOSP has a whole "fs_mgr" folder which
consist of multiple complex libraries, with 15K lines of code just
to deal with the device mapper shenanigans.
In order to keep the already overly complicated MagiskInit more
managable, I chose NOT to go the route of including fs_mgr directly
into MagiskInit. Luckily, starting from Android Q, Google decided to
split init startup into 3 stages, with the first stage doing _only_
early mount. This is great news, because we can simply let the stock
init do its own thing for us, and we intercept the bootup sequence.
So the workflow can be visualized roughly below:
Magisk First Stage --> First Stage Mount --> Magisk Second Stage --+
(MagiskInit) (Original Init) (MagiskInit) +
+
+
...Rest of the boot... <-- Second Stage <-- Selinux Setup <--+
(__________________ Original Init ____________________)
The catch here is that after doing all the first stage mounting, /init
will pivot /system as root directory (/), leaving us impossible to
regain control after we hand it over. So the solution here is to patch
fstab in /first_stage_ramdisk on-the-fly to redirect /system to
/system_root, making the original init do all the hard work for
us and mount required early mount partitions, but skips the step of
switching root directory. It will also conveniently hand over execution
back to MagiskInit, which we will reuse the routine for patching
root directory in normal system-as-root situations.
2019-06-29 09:47:29 +02:00
|
|
|
|
2019-09-22 11:20:51 +02:00
|
|
|
class SARInit : public SARBase {
|
2020-10-27 04:46:15 +01:00
|
|
|
private:
|
2020-12-31 07:11:24 +01:00
|
|
|
bool is_two_stage;
|
2020-12-06 12:07:47 +01:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
void early_mount();
|
|
|
|
void first_stage_prep();
|
2020-10-27 04:46:15 +01:00
|
|
|
public:
|
2020-12-31 07:11:24 +01:00
|
|
|
SARInit(char *argv[], cmdline *cmd) : SARBase(argv, cmd), is_two_stage(false) {
|
|
|
|
LOGD("%s\n", __FUNCTION__);
|
|
|
|
};
|
|
|
|
void start() override {
|
|
|
|
early_mount();
|
|
|
|
if (is_two_stage)
|
|
|
|
first_stage_prep();
|
|
|
|
else
|
|
|
|
patch_rootdir();
|
|
|
|
exec_init();
|
|
|
|
}
|
2020-10-27 04:46:15 +01:00
|
|
|
};
|
|
|
|
|
2020-04-19 13:56:56 +02:00
|
|
|
/************
|
Logical Resizable Android Partitions support
The way how logical partition, or "Logical Resizable Android Partitions"
as they say in AOSP source code, is setup makes it impossible to early
mount the partitions from the shared super partition with just
a few lines of code; in fact, AOSP has a whole "fs_mgr" folder which
consist of multiple complex libraries, with 15K lines of code just
to deal with the device mapper shenanigans.
In order to keep the already overly complicated MagiskInit more
managable, I chose NOT to go the route of including fs_mgr directly
into MagiskInit. Luckily, starting from Android Q, Google decided to
split init startup into 3 stages, with the first stage doing _only_
early mount. This is great news, because we can simply let the stock
init do its own thing for us, and we intercept the bootup sequence.
So the workflow can be visualized roughly below:
Magisk First Stage --> First Stage Mount --> Magisk Second Stage --+
(MagiskInit) (Original Init) (MagiskInit) +
+
+
...Rest of the boot... <-- Second Stage <-- Selinux Setup <--+
(__________________ Original Init ____________________)
The catch here is that after doing all the first stage mounting, /init
will pivot /system as root directory (/), leaving us impossible to
regain control after we hand it over. So the solution here is to patch
fstab in /first_stage_ramdisk on-the-fly to redirect /system to
/system_root, making the original init do all the hard work for
us and mount required early mount partitions, but skips the step of
switching root directory. It will also conveniently hand over execution
back to MagiskInit, which we will reuse the routine for patching
root directory in normal system-as-root situations.
2019-06-29 09:47:29 +02:00
|
|
|
* Initramfs
|
2020-04-19 13:56:56 +02:00
|
|
|
************/
|
Logical Resizable Android Partitions support
The way how logical partition, or "Logical Resizable Android Partitions"
as they say in AOSP source code, is setup makes it impossible to early
mount the partitions from the shared super partition with just
a few lines of code; in fact, AOSP has a whole "fs_mgr" folder which
consist of multiple complex libraries, with 15K lines of code just
to deal with the device mapper shenanigans.
In order to keep the already overly complicated MagiskInit more
managable, I chose NOT to go the route of including fs_mgr directly
into MagiskInit. Luckily, starting from Android Q, Google decided to
split init startup into 3 stages, with the first stage doing _only_
early mount. This is great news, because we can simply let the stock
init do its own thing for us, and we intercept the bootup sequence.
So the workflow can be visualized roughly below:
Magisk First Stage --> First Stage Mount --> Magisk Second Stage --+
(MagiskInit) (Original Init) (MagiskInit) +
+
+
...Rest of the boot... <-- Second Stage <-- Selinux Setup <--+
(__________________ Original Init ____________________)
The catch here is that after doing all the first stage mounting, /init
will pivot /system as root directory (/), leaving us impossible to
regain control after we hand it over. So the solution here is to patch
fstab in /first_stage_ramdisk on-the-fly to redirect /system to
/system_root, making the original init do all the hard work for
us and mount required early mount partitions, but skips the step of
switching root directory. It will also conveniently hand over execution
back to MagiskInit, which we will reuse the routine for patching
root directory in normal system-as-root situations.
2019-06-29 09:47:29 +02:00
|
|
|
|
2019-12-12 09:25:48 +01:00
|
|
|
class RootFSInit : public MagiskInit {
|
|
|
|
private:
|
2020-12-31 07:11:24 +01:00
|
|
|
void early_mount();
|
|
|
|
void patch_rootfs();
|
2019-06-16 21:45:32 +02:00
|
|
|
public:
|
2020-12-31 07:11:24 +01:00
|
|
|
RootFSInit(char *argv[], cmdline *cmd) : MagiskInit(argv, cmd) {
|
|
|
|
LOGD("%s\n", __FUNCTION__);
|
|
|
|
}
|
|
|
|
void start() override {
|
|
|
|
early_mount();
|
|
|
|
patch_rootfs();
|
|
|
|
exec_init();
|
|
|
|
}
|
2019-05-27 09:29:43 +02:00
|
|
|
};
|
2020-10-27 04:46:15 +01:00
|
|
|
|
|
|
|
class MagiskProxy : public MagiskInit {
|
|
|
|
public:
|
2020-12-31 07:11:24 +01:00
|
|
|
explicit MagiskProxy(char *argv[]) : MagiskInit(argv, nullptr) {
|
|
|
|
LOGD("%s\n", __FUNCTION__);
|
|
|
|
}
|
|
|
|
void start() override;
|
2020-10-27 04:46:15 +01:00
|
|
|
};
|