2019-05-27 09:29:43 +02:00
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2019-07-02 07:58:19 +02:00
|
|
|
#include <vector>
|
2019-05-27 09:29:43 +02:00
|
|
|
|
2020-03-09 09:50:30 +01:00
|
|
|
#include <utils.hpp>
|
|
|
|
#include <logging.hpp>
|
|
|
|
#include <selinux.hpp>
|
|
|
|
#include <magisk.hpp>
|
2019-05-27 09:29:43 +02:00
|
|
|
|
2020-03-09 09:50:30 +01:00
|
|
|
#include "init.hpp"
|
2019-05-27 09:29:43 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2019-05-27 11:55:46 +02:00
|
|
|
struct devinfo {
|
|
|
|
int major;
|
|
|
|
int minor;
|
|
|
|
char devname[32];
|
|
|
|
char partname[32];
|
|
|
|
};
|
|
|
|
|
|
|
|
static vector<devinfo> dev_list;
|
|
|
|
|
2019-12-05 22:29:45 +01:00
|
|
|
static char partname[32];
|
|
|
|
static char fstype[32];
|
|
|
|
static char block_dev[64];
|
|
|
|
|
2019-05-27 11:55:46 +02:00
|
|
|
static void parse_device(devinfo *dev, const char *uevent) {
|
2019-05-27 09:29:43 +02:00
|
|
|
dev->partname[0] = '\0';
|
2019-05-27 11:55:46 +02:00
|
|
|
parse_prop_file(uevent, [=](string_view key, string_view value) -> bool {
|
|
|
|
if (key == "MAJOR")
|
2019-12-05 22:29:45 +01:00
|
|
|
dev->major = parse_int(value.data());
|
2019-05-27 11:55:46 +02:00
|
|
|
else if (key == "MINOR")
|
2019-12-05 22:29:45 +01:00
|
|
|
dev->minor = parse_int(value.data());
|
2019-05-27 11:55:46 +02:00
|
|
|
else if (key == "DEVNAME")
|
|
|
|
strcpy(dev->devname, value.data());
|
|
|
|
else if (key == "PARTNAME")
|
|
|
|
strcpy(dev->partname, value.data());
|
2019-05-27 09:29:43 +02:00
|
|
|
|
2019-05-27 11:55:46 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
2019-05-27 09:29:43 +02:00
|
|
|
|
|
|
|
static void collect_devices() {
|
|
|
|
char path[128];
|
2019-12-05 23:18:23 +01:00
|
|
|
devinfo dev{};
|
2019-12-13 06:37:06 +01:00
|
|
|
if (auto dir = xopen_dir("/sys/dev/block"); dir) {
|
|
|
|
for (dirent *entry; (entry = readdir(dir.get()));) {
|
|
|
|
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);
|
|
|
|
}
|
2019-05-27 09:29:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 08:44:49 +01:00
|
|
|
static int64_t setup_block(bool write_block = true) {
|
2019-05-27 09:29:43 +02:00
|
|
|
if (dev_list.empty())
|
|
|
|
collect_devices();
|
2019-12-05 23:18:23 +01:00
|
|
|
xmkdir("/dev", 0755);
|
2019-12-09 08:44:49 +01:00
|
|
|
xmkdir("/dev/block", 0755);
|
2019-12-05 23:18:23 +01:00
|
|
|
|
|
|
|
for (int tries = 0; tries < 3; ++tries) {
|
2019-05-28 00:01:49 +02:00
|
|
|
for (auto &dev : dev_list) {
|
|
|
|
if (strcasecmp(dev.partname, partname) == 0) {
|
2019-12-09 08:44:49 +01:00
|
|
|
if (write_block) {
|
2019-06-24 00:14:47 +02:00
|
|
|
sprintf(block_dev, "/dev/block/%s", dev.devname);
|
|
|
|
}
|
|
|
|
LOGD("Found %s: [%s] (%d, %d)\n", dev.partname, dev.devname, dev.major, dev.minor);
|
2019-06-24 10:21:33 +02:00
|
|
|
dev_t rdev = makedev(dev.major, dev.minor);
|
2019-12-09 08:44:49 +01:00
|
|
|
mknod(block_dev, S_IFBLK | 0600, rdev);
|
2019-06-24 10:21:33 +02:00
|
|
|
return rdev;
|
2019-05-28 00:01:49 +02:00
|
|
|
}
|
2019-05-27 09:29:43 +02:00
|
|
|
}
|
2019-05-28 00:01:49 +02:00
|
|
|
// Wait 10ms and try again
|
|
|
|
usleep(10000);
|
|
|
|
dev_list.clear();
|
|
|
|
collect_devices();
|
2019-05-27 09:29:43 +02:00
|
|
|
}
|
2019-12-05 23:18:23 +01:00
|
|
|
|
|
|
|
// The requested partname does not exist
|
|
|
|
return -1;
|
2019-05-27 09:29:43 +02:00
|
|
|
}
|
|
|
|
|
2019-06-30 20:39:13 +02:00
|
|
|
static bool is_lnk(const char *name) {
|
|
|
|
struct stat st;
|
|
|
|
if (lstat(name, &st))
|
|
|
|
return false;
|
|
|
|
return S_ISLNK(st.st_mode);
|
|
|
|
}
|
|
|
|
|
2019-12-05 22:29:45 +01:00
|
|
|
static bool read_dt_fstab(cmdline *cmd, const char *name) {
|
2019-05-27 09:29:43 +02:00
|
|
|
char path[128];
|
|
|
|
int fd;
|
2019-06-16 07:25:09 +02:00
|
|
|
sprintf(path, "%s/fstab/%s/dev", cmd->dt_dir, name);
|
2020-01-08 15:42:54 +01:00
|
|
|
if ((fd = open(path, O_RDONLY | O_CLOEXEC)) >= 0) {
|
2019-05-27 09:29:43 +02:00
|
|
|
read(fd, path, sizeof(path));
|
|
|
|
close(fd);
|
2020-03-27 07:56:49 +01:00
|
|
|
path[strcspn(path, "\r\n")] = '\0';
|
2019-05-27 09:29:43 +02:00
|
|
|
// Some custom treble use different names, so use what we read
|
|
|
|
char *part = rtrim(strrchr(path, '/') + 1);
|
2019-06-16 07:25:09 +02: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 09:29:43 +02:00
|
|
|
if ((fd = xopen(path, O_RDONLY | O_CLOEXEC)) >= 0) {
|
2019-05-27 11:55:46 +02:00
|
|
|
read(fd, fstype, 32);
|
2019-05-27 09:29:43 +02:00
|
|
|
close(fd);
|
2020-03-27 07:56:49 +01:00
|
|
|
fstype[strcspn(fstype, "\r\n")] = '\0';
|
2019-05-27 09:29:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define mount_root(name) \
|
2019-12-05 22:29:45 +01:00
|
|
|
if (!is_lnk("/" #name) && read_dt_fstab(cmd, #name)) { \
|
2019-05-27 09:29:43 +02:00
|
|
|
LOGD("Early mount " #name "\n"); \
|
2019-12-09 08:44:49 +01:00
|
|
|
setup_block(); \
|
2019-05-27 09:29:43 +02:00
|
|
|
xmkdir("/" #name, 0755); \
|
|
|
|
xmount(block_dev, "/" #name, fstype, MS_RDONLY, nullptr); \
|
2019-11-19 06:16:20 +01:00
|
|
|
mount_list.emplace_back("/" #name); \
|
2019-05-27 09:29:43 +02:00
|
|
|
}
|
|
|
|
|
2019-06-24 00:14:47 +02:00
|
|
|
static void switch_root(const string &path) {
|
|
|
|
LOGD("Switch root to %s\n", path.data());
|
2020-04-02 08:37:11 +02:00
|
|
|
int root = xopen("/", O_RDONLY);
|
2019-06-24 00:14:47 +02:00
|
|
|
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 09:47:29 +02: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) {
|
2020-02-21 06:08:44 +01:00
|
|
|
if (strncmp(me->mnt_dir, m.data(), m.length()) == 0 && me->mnt_dir[m.length()] == '/')
|
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
|
|
|
return true;
|
|
|
|
}
|
|
|
|
mounts.emplace_back(me->mnt_dir);
|
2019-06-24 00:14:47 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
for (auto &dir : mounts) {
|
|
|
|
auto new_path = path + dir;
|
|
|
|
mkdir(new_path.data(), 0755);
|
2020-02-21 06:08:44 +01:00
|
|
|
xmount(dir.data(), new_path.data(), nullptr, MS_MOVE, nullptr);
|
2019-06-24 00:14:47 +02:00
|
|
|
}
|
|
|
|
chdir(path.data());
|
|
|
|
xmount(path.data(), "/", nullptr, MS_MOVE, nullptr);
|
|
|
|
chroot(".");
|
2020-04-02 08:37:11 +02:00
|
|
|
|
|
|
|
LOGD("Cleaning rootfs\n");
|
|
|
|
frm_rf(root);
|
2019-06-24 00:14:47 +02:00
|
|
|
}
|
|
|
|
|
2020-01-08 15:42:54 +01:00
|
|
|
static void mount_persist(const char *dev_base, const char *mnt_base) {
|
|
|
|
string mnt_point = mnt_base + "/persist"s;
|
|
|
|
strcpy(partname, "persist");
|
|
|
|
sprintf(block_dev, "%s/persist", dev_base);
|
|
|
|
if (setup_block(false) < 0) {
|
|
|
|
// Fallback to cache
|
|
|
|
strcpy(partname, "cache");
|
|
|
|
sprintf(block_dev, "%s/cache", dev_base);
|
|
|
|
if (setup_block(false) < 0) {
|
|
|
|
// Try NVIDIA's BS
|
|
|
|
strcpy(partname, "CAC");
|
|
|
|
if (setup_block(false) < 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
xsymlink("./cache", mnt_point.data());
|
|
|
|
mnt_point = mnt_base + "/cache"s;
|
|
|
|
}
|
|
|
|
xmkdir(mnt_point.data(), 0755);
|
|
|
|
xmount(block_dev, mnt_point.data(), "ext4", 0, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RootFSInit::early_mount() {
|
|
|
|
full_read("/init", self.buf, self.sz);
|
|
|
|
|
|
|
|
LOGD("Reverting /init\n");
|
|
|
|
rename("/.backup/init", "/init");
|
|
|
|
|
|
|
|
mount_root(system);
|
|
|
|
mount_root(vendor);
|
|
|
|
mount_root(product);
|
|
|
|
mount_root(odm);
|
|
|
|
|
|
|
|
xmkdir("/dev/mnt", 0755);
|
|
|
|
mount_persist("/dev/block", "/dev/mnt");
|
|
|
|
mount_list.emplace_back("/dev/mnt/persist");
|
2020-04-12 14:34:56 +02:00
|
|
|
persist_dir = "/dev/mnt/persist/magisk";
|
2020-01-08 15:42:54 +01:00
|
|
|
}
|
|
|
|
|
2020-04-01 13:39:28 +02:00
|
|
|
void SARBase::backup_files() {
|
2019-07-16 10:08:28 +02:00
|
|
|
if (access("/overlay.d", F_OK) == 0)
|
2020-04-01 13:39:28 +02:00
|
|
|
backup_folder("/overlay.d", overlays);
|
2019-07-16 10:08:28 +02:00
|
|
|
|
2020-04-01 13:39:28 +02:00
|
|
|
full_read("/proc/self/exe", self.buf, self.sz);
|
2019-07-16 10:08:28 +02:00
|
|
|
full_read("/.backup/.magisk", config.buf, config.sz);
|
|
|
|
}
|
|
|
|
|
2020-04-01 13:39:28 +02:00
|
|
|
void SARBase::mount_system_root() {
|
2019-06-24 00:14:47 +02:00
|
|
|
LOGD("Early mount system_root\n");
|
|
|
|
sprintf(partname, "system%s", cmd->slot);
|
2019-12-09 08:44:49 +01:00
|
|
|
strcpy(block_dev, "/dev/root");
|
|
|
|
auto dev = setup_block(false);
|
2019-12-05 23:18:23 +01:00
|
|
|
if (dev < 0) {
|
|
|
|
// Try NVIDIA naming scheme
|
|
|
|
strcpy(partname, "APP");
|
2020-01-12 00:52:17 +01:00
|
|
|
dev = setup_block(false);
|
2019-12-05 23:18:23 +01:00
|
|
|
if (dev < 0) {
|
|
|
|
// We don't really know what to do at this point...
|
|
|
|
LOGE("Cannot find root partition, abort\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2019-06-24 00:14:47 +02:00
|
|
|
xmkdir("/system_root", 0755);
|
|
|
|
if (xmount("/dev/root", "/system_root", "ext4", MS_RDONLY, nullptr))
|
|
|
|
xmount("/dev/root", "/system_root", "erofs", MS_RDONLY, nullptr);
|
2020-04-01 13:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SARInit::early_mount() {
|
|
|
|
// Make dev writable
|
|
|
|
xmkdir("/dev", 0755);
|
|
|
|
xmount("tmpfs", "/dev", "tmpfs", 0, "mode=755");
|
|
|
|
mount_list.emplace_back("/dev");
|
|
|
|
|
|
|
|
backup_files();
|
|
|
|
|
|
|
|
mount_system_root();
|
2019-06-24 00:14:47 +02:00
|
|
|
switch_root("/system_root");
|
2019-06-25 11:47:16 +02:00
|
|
|
|
|
|
|
mount_root(vendor);
|
|
|
|
mount_root(product);
|
|
|
|
mount_root(odm);
|
2019-06-24 00:14:47 +02:00
|
|
|
}
|
|
|
|
|
2020-04-01 13:39:28 +02:00
|
|
|
void SARFirstStageInit::early_mount() {
|
|
|
|
backup_files();
|
|
|
|
mount_system_root();
|
|
|
|
switch_root("/system_root");
|
|
|
|
}
|
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-01 13:39:28 +02:00
|
|
|
void SecondStageInit::early_mount() {
|
|
|
|
backup_files();
|
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
|
|
|
rm_rf("/system");
|
|
|
|
rm_rf("/.backup");
|
2019-07-16 10:08:28 +02:00
|
|
|
rm_rf("/overlay.d");
|
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-01 13:39:28 +02:00
|
|
|
umount2("/system/bin/init", MNT_DETACH);
|
|
|
|
|
|
|
|
if (access("/system_root", F_OK) == 0)
|
|
|
|
switch_root("/system_root");
|
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-05 22:29:45 +01:00
|
|
|
|
|
|
|
void BaseInit::cleanup() {
|
|
|
|
// Unmount in reverse order
|
|
|
|
for (auto &p : reversed(mount_list)) {
|
2019-12-13 12:05:12 +01:00
|
|
|
if (xumount(p.data()) == 0)
|
|
|
|
LOGD("Unmount [%s]\n", p.data());
|
2019-12-05 22:29:45 +01:00
|
|
|
}
|
2019-12-13 12:05:12 +01:00
|
|
|
mount_list.clear();
|
|
|
|
mount_list.shrink_to_fit();
|
2019-12-05 22:29:45 +01:00
|
|
|
}
|
2019-12-09 08:44:49 +01:00
|
|
|
|
2020-04-12 14:34:56 +02:00
|
|
|
static void patch_socket_name(const char *path) {
|
|
|
|
char *buf;
|
|
|
|
size_t size;
|
|
|
|
mmap_rw(path, buf, size);
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
if (memcmp(buf + i, MAIN_SOCKET, sizeof(MAIN_SOCKET)) == 0) {
|
|
|
|
gen_rand_str(buf + i, 16);
|
|
|
|
i += sizeof(MAIN_SOCKET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
munmap(buf, size);
|
|
|
|
}
|
2019-12-09 08:44:49 +01:00
|
|
|
|
2020-04-12 14:34:56 +02:00
|
|
|
void setup_tmp(const char *path, const raw_data &self, const raw_data &config) {
|
|
|
|
LOGD("Setup Magisk tmp at %s\n", path);
|
|
|
|
xmount("tmpfs", path, "tmpfs", 0, "mode=755");
|
|
|
|
|
|
|
|
chdir(path);
|
|
|
|
|
|
|
|
xmkdir(INTLROOT, 0755);
|
2019-12-09 08:44:49 +01:00
|
|
|
xmkdir(MIRRDIR, 0);
|
|
|
|
xmkdir(BLOCKDIR, 0);
|
|
|
|
|
2020-01-08 15:42:54 +01:00
|
|
|
mount_persist(BLOCKDIR, MIRRDIR);
|
2020-04-12 14:34:56 +02:00
|
|
|
|
|
|
|
int fd = xopen(INTLROOT "/config", O_WRONLY | O_CREAT, 0);
|
|
|
|
xwrite(fd, config.buf, config.sz);
|
|
|
|
close(fd);
|
|
|
|
fd = xopen("magiskinit", O_WRONLY | O_CREAT, 0755);
|
|
|
|
xwrite(fd, self.buf, self.sz);
|
|
|
|
close(fd);
|
|
|
|
dump_magisk("magisk", 0755);
|
|
|
|
patch_socket_name("magisk");
|
|
|
|
|
|
|
|
// Create applet symlinks
|
|
|
|
for (int i = 0; applet_names[i]; ++i)
|
|
|
|
xsymlink("./magisk", applet_names[i]);
|
|
|
|
xsymlink("./magiskinit", "magiskpolicy");
|
|
|
|
xsymlink("./magiskinit", "supolicy");
|
|
|
|
|
|
|
|
chdir("/");
|
2019-12-09 08:44:49 +01:00
|
|
|
}
|