2019-05-27 07:29:43 +00:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <utils.h>
|
|
|
|
#include <logging.h>
|
2019-06-16 19:45:32 +00:00
|
|
|
#include <selinux.h>
|
2019-05-27 07:29:43 +00:00
|
|
|
|
|
|
|
#include "init.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2019-05-27 09:55:46 +00:00
|
|
|
struct devinfo {
|
|
|
|
int major;
|
|
|
|
int minor;
|
|
|
|
char devname[32];
|
|
|
|
char partname[32];
|
|
|
|
};
|
|
|
|
|
|
|
|
static vector<devinfo> dev_list;
|
|
|
|
|
|
|
|
static void parse_device(devinfo *dev, const char *uevent) {
|
2019-05-27 07:29:43 +00:00
|
|
|
dev->partname[0] = '\0';
|
2019-05-27 09:55:46 +00:00
|
|
|
parse_prop_file(uevent, [=](string_view key, string_view value) -> bool {
|
|
|
|
if (key == "MAJOR")
|
|
|
|
dev->major = atoi(value.data());
|
|
|
|
else if (key == "MINOR")
|
|
|
|
dev->minor = atoi(value.data());
|
|
|
|
else if (key == "DEVNAME")
|
|
|
|
strcpy(dev->devname, value.data());
|
|
|
|
else if (key == "PARTNAME")
|
|
|
|
strcpy(dev->partname, value.data());
|
2019-05-27 07:29:43 +00:00
|
|
|
|
2019-05-27 09:55:46 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
2019-05-27 07:29:43 +00:00
|
|
|
|
|
|
|
static void collect_devices() {
|
|
|
|
char path[128];
|
|
|
|
struct dirent *entry;
|
2019-05-27 09:55:46 +00:00
|
|
|
devinfo dev;
|
2019-05-27 07:29:43 +00:00
|
|
|
DIR *dir = xopendir("/sys/dev/block");
|
|
|
|
if (dir == nullptr)
|
|
|
|
return;
|
|
|
|
while ((entry = readdir(dir))) {
|
|
|
|
if (entry->d_name == "."sv || entry->d_name == ".."sv)
|
|
|
|
continue;
|
|
|
|
sprintf(path, "/sys/dev/block/%s/uevent", entry->d_name);
|
|
|
|
parse_device(&dev, path);
|
|
|
|
dev_list.push_back(dev);
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
2019-06-24 08:21:33 +00:00
|
|
|
static dev_t setup_block(const char *partname, char *block_dev = nullptr) {
|
2019-05-27 07:29:43 +00:00
|
|
|
if (dev_list.empty())
|
|
|
|
collect_devices();
|
2019-05-27 22:01:49 +00:00
|
|
|
for (;;) {
|
|
|
|
for (auto &dev : dev_list) {
|
|
|
|
if (strcasecmp(dev.partname, partname) == 0) {
|
|
|
|
xmkdir("/dev", 0755);
|
2019-06-23 22:14:47 +00:00
|
|
|
if (block_dev) {
|
|
|
|
sprintf(block_dev, "/dev/block/%s", dev.devname);
|
|
|
|
xmkdir("/dev/block", 0755);
|
|
|
|
}
|
|
|
|
LOGD("Found %s: [%s] (%d, %d)\n", dev.partname, dev.devname, dev.major, dev.minor);
|
2019-06-24 08:21:33 +00:00
|
|
|
dev_t rdev = makedev(dev.major, dev.minor);
|
|
|
|
mknod(block_dev ? block_dev : "/dev/root", S_IFBLK | 0600, rdev);
|
|
|
|
return rdev;
|
2019-05-27 22:01:49 +00:00
|
|
|
}
|
2019-05-27 07:29:43 +00:00
|
|
|
}
|
2019-05-27 22:01:49 +00:00
|
|
|
// Wait 10ms and try again
|
|
|
|
usleep(10000);
|
|
|
|
dev_list.clear();
|
|
|
|
collect_devices();
|
2019-05-27 07:29:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 10:14:33 +00:00
|
|
|
bool MagiskInit::read_dt_fstab(const char *name, char *partname, char *fstype) {
|
2019-05-27 07:29:43 +00:00
|
|
|
char path[128];
|
|
|
|
int fd;
|
2019-06-16 05:25:09 +00:00
|
|
|
sprintf(path, "%s/fstab/%s/dev", cmd->dt_dir, name);
|
2019-05-27 07:29:43 +00:00
|
|
|
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);
|
2019-06-16 05:25:09 +00:00
|
|
|
sprintf(partname, "%s%s", part, strend(part, cmd->slot) ? cmd->slot : "");
|
|
|
|
sprintf(path, "%s/fstab/%s/type", cmd->dt_dir, name);
|
2019-05-27 07:29:43 +00:00
|
|
|
if ((fd = xopen(path, O_RDONLY | O_CLOEXEC)) >= 0) {
|
2019-05-27 09:55:46 +00:00
|
|
|
read(fd, fstype, 32);
|
2019-05-27 07:29:43 +00:00
|
|
|
close(fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define link_root(name) \
|
|
|
|
if (is_lnk("/system_root" name)) \
|
|
|
|
cp_afc("/system_root" name, name)
|
|
|
|
|
|
|
|
#define mount_root(name) \
|
|
|
|
if (!is_lnk("/" #name) && read_dt_fstab(#name, partname, fstype)) { \
|
|
|
|
LOGD("Early mount " #name "\n"); \
|
|
|
|
setup_block(partname, block_dev); \
|
|
|
|
xmkdir("/" #name, 0755); \
|
|
|
|
xmount(block_dev, "/" #name, fstype, MS_RDONLY, nullptr); \
|
|
|
|
mnt_##name = true; \
|
|
|
|
}
|
|
|
|
|
2019-06-16 19:45:32 +00:00
|
|
|
void LegacyInit::early_mount() {
|
2019-05-27 07:29:43 +00:00
|
|
|
char partname[32];
|
|
|
|
char fstype[32];
|
|
|
|
char block_dev[64];
|
|
|
|
|
2019-06-24 08:50:47 +00:00
|
|
|
full_read("/init", &self.buf, &self.sz);
|
|
|
|
|
|
|
|
LOGD("Reverting /init\n");
|
|
|
|
root = xopen("/", O_RDONLY | O_CLOEXEC);
|
|
|
|
rename("/.backup/init", "/init");
|
|
|
|
|
2019-06-16 19:45:32 +00:00
|
|
|
mount_root(system);
|
|
|
|
mount_root(vendor);
|
|
|
|
mount_root(product);
|
|
|
|
mount_root(odm);
|
|
|
|
}
|
2019-05-27 07:29:43 +00:00
|
|
|
|
2019-06-22 10:18:45 +00:00
|
|
|
void SARCompatInit::early_mount() {
|
2019-06-16 19:45:32 +00:00
|
|
|
char partname[32];
|
|
|
|
char fstype[32];
|
|
|
|
char block_dev[64];
|
|
|
|
|
2019-06-24 08:50:47 +00:00
|
|
|
full_read("/init", &self.buf, &self.sz);
|
|
|
|
|
|
|
|
LOGD("Cleaning rootfs\n");
|
|
|
|
root = xopen("/", O_RDONLY | O_CLOEXEC);
|
|
|
|
frm_rf(root, { ".backup", "overlay", "proc", "sys" });
|
|
|
|
|
2019-06-16 19:45:32 +00:00
|
|
|
LOGD("Early mount system_root\n");
|
|
|
|
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))
|
|
|
|
xmount(block_dev, "/system_root", "erofs", MS_RDONLY, nullptr);
|
|
|
|
xmkdir("/system", 0755);
|
|
|
|
xmount("/system_root/system", "/system", nullptr, MS_BIND, nullptr);
|
|
|
|
|
|
|
|
link_root("/vendor");
|
|
|
|
link_root("/product");
|
|
|
|
link_root("/odm");
|
2019-05-27 07:29:43 +00:00
|
|
|
mount_root(vendor);
|
|
|
|
mount_root(product);
|
|
|
|
mount_root(odm);
|
|
|
|
}
|
2019-06-16 19:45:32 +00:00
|
|
|
|
2019-06-23 22:14:47 +00:00
|
|
|
static void switch_root(const string &path) {
|
|
|
|
LOGD("Switch root to %s\n", path.data());
|
|
|
|
vector<string> mounts;
|
|
|
|
parse_mnt("/proc/mounts", [&](mntent *me) {
|
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
|
|
|
// Skip root and self
|
|
|
|
if (me->mnt_dir == "/"sv || me->mnt_dir == path)
|
|
|
|
return true;
|
|
|
|
// Do not include subtrees
|
|
|
|
for (const auto &m : mounts) {
|
|
|
|
if (strncmp(me->mnt_dir, m.data(), m.length()) == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
mounts.emplace_back(me->mnt_dir);
|
2019-06-23 22:14:47 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
for (auto &dir : mounts) {
|
|
|
|
auto new_path = path + dir;
|
|
|
|
mkdir(new_path.data(), 0755);
|
|
|
|
xmount(dir.data(), new_path.c_str(), nullptr, MS_MOVE, nullptr);
|
|
|
|
}
|
|
|
|
chdir(path.data());
|
|
|
|
xmount(path.data(), "/", nullptr, MS_MOVE, nullptr);
|
|
|
|
chroot(".");
|
|
|
|
}
|
|
|
|
|
|
|
|
void SARInit::early_mount() {
|
|
|
|
char partname[32];
|
2019-06-25 09:47:16 +00:00
|
|
|
char fstype[32];
|
|
|
|
char block_dev[64];
|
2019-06-23 22:14:47 +00:00
|
|
|
|
2019-06-24 08:50:47 +00:00
|
|
|
full_read("/init", &self.buf, &self.sz);
|
|
|
|
full_read("/.backup/.magisk", &config.buf, &config.sz);
|
|
|
|
|
|
|
|
LOGD("Cleaning rootfs\n");
|
|
|
|
int root = xopen("/", O_RDONLY | O_CLOEXEC);
|
|
|
|
frm_rf(root, { "proc", "sys" });
|
|
|
|
close(root);
|
|
|
|
|
2019-06-23 22:14:47 +00:00
|
|
|
LOGD("Early mount system_root\n");
|
|
|
|
sprintf(partname, "system%s", cmd->slot);
|
2019-06-24 08:21:33 +00:00
|
|
|
system_dev = setup_block(partname);
|
2019-06-23 22:14:47 +00:00
|
|
|
xmkdir("/system_root", 0755);
|
|
|
|
if (xmount("/dev/root", "/system_root", "ext4", MS_RDONLY, nullptr))
|
|
|
|
xmount("/dev/root", "/system_root", "erofs", MS_RDONLY, nullptr);
|
|
|
|
switch_root("/system_root");
|
2019-06-25 09:47:16 +00:00
|
|
|
|
|
|
|
// Make dev writable
|
|
|
|
xmount("tmpfs", "/dev", "tmpfs", 0, "mode=755");
|
|
|
|
|
|
|
|
mount_root(vendor);
|
|
|
|
mount_root(product);
|
|
|
|
mount_root(odm);
|
2019-06-23 22:14:47 +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
|
|
|
void SecondStageInit::early_mount() {
|
|
|
|
// Early mounts should already be done by first stage init
|
|
|
|
|
|
|
|
full_read("/system/bin/init", &self.buf, &self.sz);
|
|
|
|
full_read("/.backup/.magisk", &config.buf, &config.sz);
|
|
|
|
rm_rf("/system");
|
|
|
|
rm_rf("/.backup");
|
|
|
|
|
|
|
|
// Find system_dev
|
|
|
|
parse_mnt("/proc/mounts", [&](mntent *me) -> bool {
|
|
|
|
if (me->mnt_dir == "/system_root"sv) {
|
|
|
|
struct stat st;
|
|
|
|
stat(me->mnt_fsname, &st);
|
|
|
|
system_dev = st.st_rdev;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
switch_root("/system_root");
|
|
|
|
}
|
|
|
|
|
2019-06-16 19:45:32 +00:00
|
|
|
#define umount_root(name) \
|
|
|
|
if (mnt_##name) \
|
|
|
|
umount("/" #name);
|
|
|
|
|
2019-06-22 10:14:33 +00:00
|
|
|
void MagiskInit::cleanup() {
|
2019-06-16 19:45:32 +00:00
|
|
|
umount(SELINUX_MNT);
|
2019-06-25 09:47:16 +00:00
|
|
|
BaseInit::cleanup();
|
2019-06-16 19:45:32 +00:00
|
|
|
umount_root(system);
|
|
|
|
umount_root(vendor);
|
|
|
|
umount_root(product);
|
|
|
|
umount_root(odm);
|
|
|
|
}
|