2019-06-30 18:39:13 +00:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <unistd.h>
|
2019-05-27 07:29:43 +00:00
|
|
|
#include <stdlib.h>
|
2019-11-19 05:16:20 +00:00
|
|
|
#include <vector>
|
2019-05-27 07:29:43 +00:00
|
|
|
|
2020-03-09 08:50:30 +00:00
|
|
|
#include <magisk.hpp>
|
2019-12-13 11:05:12 +00:00
|
|
|
|
2019-05-27 07:29:43 +00:00
|
|
|
struct cmdline {
|
2019-12-06 17:02:34 +00:00
|
|
|
bool skip_initramfs;
|
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 07:47:29 +00:00
|
|
|
bool force_normal_boot;
|
2019-05-27 07:29:43 +00:00
|
|
|
char slot[3];
|
|
|
|
char dt_dir[128];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct raw_data {
|
2019-07-16 08:08:28 +00:00
|
|
|
uint8_t *buf = nullptr;
|
|
|
|
size_t sz = 0;
|
|
|
|
|
|
|
|
raw_data() = default;
|
|
|
|
raw_data(const raw_data&) = delete;
|
|
|
|
raw_data(raw_data &&d) {
|
2019-07-17 06:30:54 +00:00
|
|
|
buf = d.buf;
|
|
|
|
sz = d.sz;
|
|
|
|
d.buf = nullptr;
|
|
|
|
d.sz = 0;
|
2019-07-16 08:08:28 +00:00
|
|
|
}
|
|
|
|
~raw_data() {
|
|
|
|
free(buf);
|
|
|
|
}
|
2019-05-27 07:29:43 +00: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 07:47:29 +00:00
|
|
|
/* *************
|
|
|
|
* Base classes
|
|
|
|
* *************/
|
|
|
|
|
2019-06-16 19:45:32 +00:00
|
|
|
class BaseInit {
|
|
|
|
protected:
|
2019-06-16 05:25:09 +00:00
|
|
|
cmdline *cmd;
|
2019-05-27 07:29:43 +00:00
|
|
|
char **argv;
|
2019-11-19 05:16:20 +00:00
|
|
|
std::vector<std::string> mount_list;
|
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 07:47:29 +00:00
|
|
|
|
2019-06-30 18:39:13 +00:00
|
|
|
void exec_init(const char *init = "/init") {
|
|
|
|
cleanup();
|
|
|
|
execv(init, argv);
|
|
|
|
exit(1);
|
|
|
|
}
|
2019-11-19 05:16:20 +00:00
|
|
|
virtual void cleanup();
|
2019-06-22 10:14:33 +00:00
|
|
|
public:
|
2019-11-19 05:16:20 +00:00
|
|
|
BaseInit(char *argv[], cmdline *cmd) :
|
2019-12-13 11:05:12 +00:00
|
|
|
cmd(cmd), argv(argv), mount_list{"/sys", "/proc"} {}
|
2019-06-22 10:14:33 +00:00
|
|
|
virtual ~BaseInit() = default;
|
|
|
|
virtual void start() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MagiskInit : public BaseInit {
|
|
|
|
protected:
|
2019-07-16 08:08:28 +00:00
|
|
|
raw_data self;
|
2019-12-13 11:05:12 +00:00
|
|
|
const char *persist_dir;
|
2019-05-27 07:29:43 +00:00
|
|
|
|
2019-06-22 10:14:33 +00:00
|
|
|
virtual void early_mount() = 0;
|
2019-06-26 04:34:02 +00:00
|
|
|
bool patch_sepolicy(const char *file = "/sepolicy");
|
2019-05-27 07:29:43 +00:00
|
|
|
public:
|
2019-06-22 10:14:33 +00:00
|
|
|
MagiskInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {};
|
2019-06-23 22:14:47 +00:00
|
|
|
};
|
|
|
|
|
2019-09-22 09:20:51 +00:00
|
|
|
class SARBase : public MagiskInit {
|
2019-06-23 22:14:47 +00:00
|
|
|
protected:
|
2019-07-16 08:08:28 +00:00
|
|
|
raw_data config;
|
2019-06-24 08:21:33 +00:00
|
|
|
dev_t system_dev;
|
|
|
|
|
2020-01-21 21:12:04 +00:00
|
|
|
void backup_files(const char *self_path);
|
2019-06-24 08:21:33 +00:00
|
|
|
void patch_rootdir();
|
2019-06-23 22:14:47 +00:00
|
|
|
public:
|
2019-12-13 11:05:12 +00:00
|
|
|
SARBase(char *argv[], cmdline *cmd) : MagiskInit(argv, cmd) {
|
|
|
|
persist_dir = MIRRDIR "/persist/magisk";
|
|
|
|
}
|
2019-06-30 18:39:13 +00:00
|
|
|
void start() override {
|
|
|
|
early_mount();
|
|
|
|
patch_rootdir();
|
|
|
|
exec_init();
|
|
|
|
}
|
2019-06-23 22:14:47 +00:00
|
|
|
};
|
|
|
|
|
2019-09-22 09:15:31 +00:00
|
|
|
/* *************
|
|
|
|
* 2 Stage Init
|
|
|
|
* *************/
|
2019-06-24 08:50:47 +00:00
|
|
|
|
2019-09-22 09:15:31 +00:00
|
|
|
class ABFirstStageInit : public BaseInit {
|
2019-12-12 08:25:48 +00:00
|
|
|
private:
|
2019-07-16 08:08:28 +00:00
|
|
|
void prepare();
|
2019-06-23 22:14:47 +00:00
|
|
|
public:
|
2019-09-22 09:15:31 +00:00
|
|
|
ABFirstStageInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {};
|
2019-06-30 18:39:13 +00:00
|
|
|
void start() override {
|
2019-07-16 08:08:28 +00:00
|
|
|
prepare();
|
2019-06-30 18:39:13 +00:00
|
|
|
exec_init("/system/bin/init");
|
|
|
|
}
|
2019-06-16 19:45:32 +00:00
|
|
|
};
|
|
|
|
|
2019-09-22 09:15:31 +00:00
|
|
|
class AFirstStageInit : public BaseInit {
|
2019-12-12 08:25:48 +00:00
|
|
|
private:
|
2019-09-22 09:15:31 +00:00
|
|
|
void prepare();
|
|
|
|
public:
|
|
|
|
AFirstStageInit(char *argv[], cmdline *cmd) : BaseInit(argv, cmd) {};
|
|
|
|
void start() override {
|
|
|
|
prepare();
|
|
|
|
exec_init();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-22 09:20:51 +00:00
|
|
|
class SecondStageInit : public SARBase {
|
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 07:47:29 +00:00
|
|
|
protected:
|
|
|
|
void early_mount() override;
|
|
|
|
void cleanup() override { /* Do not do any cleanup */ }
|
|
|
|
public:
|
2019-09-22 09:20:51 +00:00
|
|
|
SecondStageInit(char *argv[]) : SARBase(argv, nullptr) {};
|
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 07:47:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* ***********
|
2019-09-22 09:15:31 +00:00
|
|
|
* Legacy SAR
|
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 07:47:29 +00:00
|
|
|
* ***********/
|
|
|
|
|
2019-09-22 09:20:51 +00:00
|
|
|
class SARInit : public SARBase {
|
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 07:47:29 +00:00
|
|
|
protected:
|
|
|
|
void early_mount() override;
|
|
|
|
public:
|
2019-09-22 09:20:51 +00:00
|
|
|
SARInit(char *argv[], cmdline *cmd) : SARBase(argv, cmd) {};
|
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 07:47:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* **********
|
|
|
|
* Initramfs
|
|
|
|
* **********/
|
|
|
|
|
2019-12-12 08:25:48 +00:00
|
|
|
class RootFSInit : public MagiskInit {
|
|
|
|
private:
|
|
|
|
int root = -1;
|
|
|
|
void setup_rootfs();
|
2019-06-16 19:45:32 +00:00
|
|
|
protected:
|
|
|
|
void early_mount() override;
|
|
|
|
public:
|
2019-12-13 11:05:12 +00:00
|
|
|
RootFSInit(char *argv[], cmdline *cmd) : MagiskInit(argv, cmd) {
|
2020-01-08 14:42:54 +00:00
|
|
|
persist_dir = "/dev/mnt/persist/magisk";
|
2019-12-13 11:05:12 +00:00
|
|
|
}
|
2019-06-16 19:45:32 +00:00
|
|
|
|
2019-12-12 08:25:48 +00:00
|
|
|
void start() override {
|
|
|
|
early_mount();
|
|
|
|
setup_rootfs();
|
|
|
|
exec_init();
|
|
|
|
}
|
2019-05-27 07:29:43 +00:00
|
|
|
};
|
|
|
|
|
2019-06-16 05:25:09 +00:00
|
|
|
void load_kernel_info(cmdline *cmd);
|
2019-05-27 07:29:43 +00:00
|
|
|
int dump_magisk(const char *path, mode_t mode);
|
2019-06-22 10:14:33 +00:00
|
|
|
int magisk_proxy_main(int argc, char *argv[]);
|
2019-11-18 22:18:56 +00:00
|
|
|
void setup_klog();
|
2019-12-09 07:44:49 +00:00
|
|
|
void mount_sbin();
|