2017-11-05 22:41:03 +01:00
|
|
|
/* magiskinit.c - Pre-init Magisk support
|
|
|
|
*
|
|
|
|
* This code has to be compiled statically to work properly.
|
|
|
|
*
|
2017-11-09 17:54:54 +01:00
|
|
|
* To unify Magisk support for both legacy "normal" devices and new skip_initramfs devices,
|
|
|
|
* magisk binary compilation is split into two parts - first part only compiles "magisk".
|
|
|
|
* The python build script will load the magisk main binary and compress with lzma2, dumping
|
|
|
|
* the results into "dump.h". The "magisk" binary is embedded into this binary, and will
|
|
|
|
* get extracted to the overlay folder along with init.magisk.rc.
|
|
|
|
*
|
|
|
|
* This tool does all pre-init operations to setup a Magisk environment, which pathces rootfs
|
|
|
|
* on the fly, providing fundamental support such as init, init.rc, and sepolicy patching.
|
|
|
|
*
|
|
|
|
* Magiskinit is also responsible for constructing a proper rootfs on skip_initramfs devices.
|
2017-11-05 22:41:03 +01:00
|
|
|
* On skip_initramfs devices, it will parse kernel cmdline, mount sysfs, parse through
|
|
|
|
* uevent files to make the system (or vendor if available) block device node, then copy
|
2017-11-09 17:54:54 +01:00
|
|
|
* rootfs files from system.
|
|
|
|
*
|
2017-11-05 22:41:03 +01:00
|
|
|
* This tool will be replaced with the real init to continue the boot process, but hardlinks are
|
|
|
|
* preserved as it also provides CLI for sepolicy patching (magiskpolicy)
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2017-12-05 14:05:20 +01:00
|
|
|
#define _GNU_SOURCE
|
2017-11-05 22:41:03 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/sendfile.h>
|
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
|
2018-08-09 23:03:54 +02:00
|
|
|
#include <xz.h>
|
|
|
|
|
2018-08-09 12:13:07 +02:00
|
|
|
#include "binaries.h"
|
|
|
|
#include "binaries_arch.h"
|
2018-06-10 10:55:00 +02:00
|
|
|
|
2017-11-27 20:42:48 +01:00
|
|
|
#include "magiskrc.h"
|
2017-11-05 22:41:03 +01:00
|
|
|
#include "utils.h"
|
|
|
|
#include "magiskpolicy.h"
|
2017-11-27 20:42:48 +01:00
|
|
|
#include "daemon.h"
|
|
|
|
#include "magisk.h"
|
2017-11-05 22:41:03 +01:00
|
|
|
|
2017-12-06 20:21:13 +01:00
|
|
|
#ifdef MAGISK_DEBUG
|
2018-07-20 23:12:22 +02:00
|
|
|
#define VLOG(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
|
2017-12-06 20:21:13 +01:00
|
|
|
#else
|
2017-11-06 15:33:41 +01:00
|
|
|
#define VLOG(fmt, ...)
|
2017-12-06 20:21:13 +01:00
|
|
|
#endif
|
2017-11-06 15:33:41 +01:00
|
|
|
|
2018-07-17 18:45:10 +02:00
|
|
|
#define DEFAULT_DT_DIR "/proc/device-tree/firmware/android"
|
|
|
|
|
2017-11-27 21:43:46 +01:00
|
|
|
int (*init_applet_main[]) (int, char *[]) = { magiskpolicy_main, magiskpolicy_main, NULL };
|
|
|
|
|
2017-11-05 22:41:03 +01:00
|
|
|
struct cmdline {
|
2018-02-13 17:57:52 +01:00
|
|
|
char skip_initramfs;
|
2017-11-05 22:41:03 +01:00
|
|
|
char slot[3];
|
2018-07-17 18:45:10 +02:00
|
|
|
char dt_dir[128];
|
|
|
|
};
|
|
|
|
|
2017-11-05 22:41:03 +01:00
|
|
|
struct device {
|
|
|
|
dev_t major;
|
|
|
|
dev_t minor;
|
|
|
|
char devname[32];
|
|
|
|
char partname[32];
|
|
|
|
char path[64];
|
|
|
|
};
|
|
|
|
|
|
|
|
static void parse_cmdline(struct cmdline *cmd) {
|
2017-11-23 11:38:12 +01:00
|
|
|
// cleanup
|
2018-06-16 04:42:36 +02:00
|
|
|
memset(cmd, 0, sizeof(*cmd));
|
2017-11-23 11:38:12 +01:00
|
|
|
|
|
|
|
char cmdline[4096];
|
2017-11-05 22:41:03 +01:00
|
|
|
int fd = open("/proc/cmdline", O_RDONLY | O_CLOEXEC);
|
2017-11-23 11:38:12 +01:00
|
|
|
cmdline[read(fd, cmdline, sizeof(cmdline))] = '\0';
|
2017-11-05 22:41:03 +01:00
|
|
|
close(fd);
|
2017-12-06 18:30:48 +01:00
|
|
|
for (char *tok = strtok(cmdline, " "); tok; tok = strtok(NULL, " ")) {
|
2017-11-05 22:41:03 +01:00
|
|
|
if (strncmp(tok, "androidboot.slot_suffix", 23) == 0) {
|
|
|
|
sscanf(tok, "androidboot.slot_suffix=%s", cmd->slot);
|
2017-11-06 15:33:41 +01:00
|
|
|
} else if (strncmp(tok, "androidboot.slot", 16) == 0) {
|
|
|
|
cmd->slot[0] = '_';
|
2018-02-13 17:57:52 +01:00
|
|
|
sscanf(tok, "androidboot.slot=%c", cmd->slot + 1);
|
2017-11-05 22:41:03 +01:00
|
|
|
} else if (strcmp(tok, "skip_initramfs") == 0) {
|
|
|
|
cmd->skip_initramfs = 1;
|
2018-07-17 18:45:10 +02:00
|
|
|
} else if (strncmp(tok, "androidboot.android_dt_dir", 26) == 0) {
|
|
|
|
sscanf(tok, "androidboot.android_dt_dir=%s", cmd->dt_dir);
|
2017-11-05 22:41:03 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-13 17:57:52 +01:00
|
|
|
|
2018-07-17 18:45:10 +02:00
|
|
|
if (cmd->dt_dir[0] == '\0')
|
|
|
|
strcpy(cmd->dt_dir, DEFAULT_DT_DIR);
|
|
|
|
|
|
|
|
VLOG("cmdline: skip_initramfs[%d] slot[%s] dt_dir[%s]\n", cmd->skip_initramfs, cmd->slot, cmd->dt_dir);
|
2017-11-05 22:41:03 +01:00
|
|
|
}
|
|
|
|
|
2018-07-17 18:45:10 +02:00
|
|
|
static void parse_device(struct device *dev, const char *uevent) {
|
2017-11-06 15:33:41 +01:00
|
|
|
dev->partname[0] = '\0';
|
2018-07-17 18:45:10 +02:00
|
|
|
FILE *fp = xfopen(uevent, "r");
|
|
|
|
char buf[64];
|
|
|
|
while (fgets(buf, sizeof(buf), fp)) {
|
|
|
|
if (strncmp(buf, "MAJOR", 5) == 0) {
|
|
|
|
sscanf(buf, "MAJOR=%ld", (long*) &dev->major);
|
|
|
|
} else if (strncmp(buf, "MINOR", 5) == 0) {
|
|
|
|
sscanf(buf, "MINOR=%ld", (long*) &dev->minor);
|
|
|
|
} else if (strncmp(buf, "DEVNAME", 7) == 0) {
|
|
|
|
sscanf(buf, "DEVNAME=%s", dev->devname);
|
|
|
|
} else if (strncmp(buf, "PARTNAME", 8) == 0) {
|
|
|
|
sscanf(buf, "PARTNAME=%s", dev->partname);
|
2017-11-05 22:41:03 +01:00
|
|
|
}
|
|
|
|
}
|
2018-07-17 18:45:10 +02:00
|
|
|
fclose(fp);
|
2017-11-06 15:33:41 +01:00
|
|
|
VLOG("%s [%s] (%u, %u)\n", dev->devname, dev->partname, (unsigned) dev->major, (unsigned) dev->minor);
|
2017-11-05 22:41:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int setup_block(struct device *dev, const char *partname) {
|
2018-07-17 18:45:10 +02:00
|
|
|
char path[128];
|
2017-11-05 22:41:03 +01:00
|
|
|
struct dirent *entry;
|
|
|
|
DIR *dir = opendir("/sys/dev/block");
|
|
|
|
if (dir == NULL)
|
|
|
|
return 1;
|
|
|
|
int found = 0;
|
|
|
|
while ((entry = readdir(dir))) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
2018-07-17 18:45:10 +02:00
|
|
|
sprintf(path, "/sys/dev/block/%s/uevent", entry->d_name);
|
|
|
|
parse_device(dev, path);
|
2018-05-05 09:50:06 +02:00
|
|
|
if (strcasecmp(dev->partname, partname) == 0) {
|
2018-07-17 18:45:10 +02:00
|
|
|
sprintf(dev->path, "/dev/block/%s", dev->devname);
|
2017-11-05 22:41:03 +01:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
mkdir("/dev", 0755);
|
|
|
|
mkdir("/dev/block", 0755);
|
|
|
|
mknod(dev->path, S_IFBLK | 0600, makedev(dev->major, dev->minor));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-18 21:12:28 +02:00
|
|
|
static int read_fstab_dt(const struct cmdline *cmd, const char *mnt_point, char *partname) {
|
|
|
|
char buf[128];
|
2018-07-19 11:43:37 +02:00
|
|
|
struct stat st;
|
|
|
|
sprintf(buf, "/%s", mnt_point);
|
|
|
|
lstat(buf, &st);
|
|
|
|
// Don't early mount if the mount point is symlink
|
|
|
|
if (S_ISLNK(st.st_mode))
|
|
|
|
return 1;
|
2018-07-18 21:12:28 +02:00
|
|
|
sprintf(buf, "%s/fstab/%s/dev", cmd->dt_dir, mnt_point);
|
2018-07-17 18:45:10 +02:00
|
|
|
if (access(buf, F_OK) == 0) {
|
|
|
|
int fd = open(buf, O_RDONLY | O_CLOEXEC);
|
|
|
|
read(fd, buf, sizeof(buf));
|
|
|
|
close(fd);
|
2018-07-18 21:12:28 +02:00
|
|
|
char *name = strrchr(buf, '/') + 1;
|
|
|
|
sprintf(partname, "%s%s", name, strend(name, cmd->slot) ? cmd->slot : "");
|
|
|
|
return 0;
|
2018-07-17 18:45:10 +02:00
|
|
|
}
|
2018-07-18 21:12:28 +02:00
|
|
|
return 1;
|
2017-11-05 22:41:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int verify_precompiled() {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
int fd;
|
2018-07-17 18:45:10 +02:00
|
|
|
char sys_sha[64], ven_sha[64];
|
2017-11-05 22:41:03 +01:00
|
|
|
|
2017-11-30 20:33:25 +01:00
|
|
|
// init the strings with different value
|
|
|
|
sys_sha[0] = 0;
|
|
|
|
ven_sha[0] = 1;
|
|
|
|
|
|
|
|
dir = opendir(NONPLAT_POLICY_DIR);
|
2017-11-05 22:41:03 +01:00
|
|
|
while ((entry = readdir(dir))) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
if (strend(entry->d_name, ".sha256") == 0) {
|
|
|
|
fd = openat(dirfd(dir), entry->d_name, O_RDONLY | O_CLOEXEC);
|
2018-07-17 18:45:10 +02:00
|
|
|
read(fd, ven_sha, sizeof(ven_sha));
|
2017-11-05 22:41:03 +01:00
|
|
|
close(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
2017-11-30 20:33:25 +01:00
|
|
|
dir = opendir(PLAT_POLICY_DIR);
|
2017-11-05 22:41:03 +01:00
|
|
|
while ((entry = readdir(dir))) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
if (strend(entry->d_name, ".sha256") == 0) {
|
|
|
|
fd = openat(dirfd(dir), entry->d_name, O_RDONLY | O_CLOEXEC);
|
2018-07-17 18:45:10 +02:00
|
|
|
read(fd, sys_sha, sizeof(sys_sha));
|
2017-11-05 22:41:03 +01:00
|
|
|
close(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
2018-07-17 18:45:10 +02:00
|
|
|
VLOG("sys_sha[%.*s]\nven_sha[%.*s]\n", sizeof(sys_sha), sys_sha, sizeof(ven_sha), ven_sha);
|
|
|
|
return memcmp(sys_sha, ven_sha, sizeof(sys_sha)) == 0;
|
2017-11-05 22:41:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-31 14:30:56 +01:00
|
|
|
static int patch_sepolicy() {
|
2018-07-17 18:45:10 +02:00
|
|
|
int init_patch = 0;
|
|
|
|
if (access(SPLIT_PRECOMPILE, R_OK) == 0 && verify_precompiled()) {
|
|
|
|
init_patch = 1;
|
2017-11-30 20:33:25 +01:00
|
|
|
load_policydb(SPLIT_PRECOMPILE);
|
2018-07-17 18:45:10 +02:00
|
|
|
} else if (access(SPLIT_PLAT_CIL, R_OK) == 0) {
|
|
|
|
init_patch = 1;
|
2018-07-20 23:12:22 +02:00
|
|
|
compile_split_cil();
|
2018-07-17 18:45:10 +02:00
|
|
|
} else if (access("/sepolicy", R_OK) == 0) {
|
|
|
|
load_policydb("/sepolicy");
|
|
|
|
} else {
|
2017-12-31 14:30:56 +01:00
|
|
|
return 1;
|
2018-07-17 18:45:10 +02:00
|
|
|
}
|
2017-11-05 22:41:03 +01:00
|
|
|
|
2017-11-30 13:57:40 +01:00
|
|
|
sepol_magisk_rules();
|
2018-08-08 21:20:28 +02:00
|
|
|
sepol_allow(SEPOL_PROC_DOMAIN, ALL, ALL, ALL);
|
2017-11-05 22:41:03 +01:00
|
|
|
dump_policydb("/sepolicy");
|
2017-12-31 14:30:56 +01:00
|
|
|
|
2018-02-01 21:17:13 +01:00
|
|
|
// Remove the stupid debug sepolicy and use our own
|
|
|
|
if (access("/sepolicy_debug", F_OK) == 0) {
|
|
|
|
unlink("/sepolicy_debug");
|
|
|
|
link("/sepolicy", "/sepolicy_debug");
|
|
|
|
}
|
|
|
|
|
2018-07-17 18:45:10 +02:00
|
|
|
if (init_patch) {
|
|
|
|
// Force init to load /sepolicy
|
|
|
|
void *addr;
|
|
|
|
size_t size;
|
|
|
|
mmap_rw("/init", &addr, &size);
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
if (memcmp(addr + i, SPLIT_PLAT_CIL, sizeof(SPLIT_PLAT_CIL) - 1) == 0) {
|
|
|
|
memcpy(addr + i + sizeof(SPLIT_PLAT_CIL) - 4, "xxx", 3);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
munmap(addr, size);
|
|
|
|
}
|
|
|
|
|
2017-12-31 14:30:56 +01:00
|
|
|
return 0;
|
2017-11-05 22:41:03 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 23:03:54 +02:00
|
|
|
static int unxz(int fd, const void *buf, size_t size) {
|
|
|
|
uint8_t out[8192];
|
|
|
|
struct xz_dec *dec = xz_dec_init(XZ_DYNALLOC, 1 << 26);
|
|
|
|
struct xz_buf b = {
|
|
|
|
.in = buf,
|
|
|
|
.in_pos = 0,
|
|
|
|
.in_size = size,
|
|
|
|
.out = out,
|
|
|
|
.out_pos = 0,
|
|
|
|
.out_size = sizeof(out)
|
|
|
|
};
|
|
|
|
enum xz_ret ret;
|
|
|
|
do {
|
|
|
|
ret = xz_dec_run(dec, &b);
|
|
|
|
if (ret != XZ_OK && ret != XZ_STREAM_END)
|
|
|
|
return 1;
|
|
|
|
write(fd, out, b.out_pos);
|
|
|
|
b.out_pos = 0;
|
|
|
|
} while (b.in_pos != size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-09 17:54:54 +01:00
|
|
|
static int dump_magisk(const char *path, mode_t mode) {
|
|
|
|
int fd = creat(path, mode);
|
2018-08-09 23:03:54 +02:00
|
|
|
unxz(fd, magisk_xz, sizeof(magisk_xz));
|
2017-11-09 17:54:54 +01:00
|
|
|
close(fd);
|
2018-08-09 12:13:07 +02:00
|
|
|
return 0;
|
2017-11-09 17:54:54 +01:00
|
|
|
}
|
|
|
|
|
2018-06-13 18:59:08 +02:00
|
|
|
static int dump_manager(const char *path, mode_t mode) {
|
|
|
|
int fd = creat(path, mode);
|
2018-08-09 23:03:54 +02:00
|
|
|
unxz(fd, manager_xz, sizeof(manager_xz));
|
2018-06-13 18:59:08 +02:00
|
|
|
close(fd);
|
2018-08-09 12:13:07 +02:00
|
|
|
return 0;
|
2018-06-13 18:59:08 +02:00
|
|
|
}
|
|
|
|
|
2017-11-09 17:54:54 +01:00
|
|
|
static int dump_magiskrc(const char *path, mode_t mode) {
|
|
|
|
int fd = creat(path, mode);
|
2018-07-17 00:18:36 +02:00
|
|
|
xwrite(fd, magiskrc, sizeof(magiskrc));
|
2017-11-09 17:54:54 +01:00
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-10 20:40:09 +01:00
|
|
|
static void patch_socket_name(const char *path) {
|
|
|
|
void *buf;
|
2018-07-02 16:11:28 +02:00
|
|
|
char name[sizeof(MAIN_SOCKET)];
|
2018-02-10 20:40:09 +01:00
|
|
|
size_t size;
|
|
|
|
mmap_rw(path, &buf, &size);
|
2018-07-02 16:11:28 +02:00
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
if (memcmp(buf + i, MAIN_SOCKET, sizeof(MAIN_SOCKET)) == 0) {
|
|
|
|
gen_rand_str(name, sizeof(name));
|
|
|
|
memcpy(buf + i, name, sizeof(name));
|
|
|
|
i += sizeof(name);
|
|
|
|
}
|
|
|
|
if (memcmp(buf + i, LOG_SOCKET, sizeof(LOG_SOCKET)) == 0) {
|
|
|
|
gen_rand_str(name, sizeof(name));
|
|
|
|
memcpy(buf + i, name, sizeof(name));
|
|
|
|
i += sizeof(name);
|
2018-02-10 20:40:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
munmap(buf, size);
|
|
|
|
}
|
|
|
|
|
2017-11-05 22:41:03 +01:00
|
|
|
int main(int argc, char *argv[]) {
|
2017-11-23 11:38:12 +01:00
|
|
|
umask(0);
|
|
|
|
|
2017-11-27 21:43:46 +01:00
|
|
|
for (int i = 0; init_applet[i]; ++i) {
|
|
|
|
if (strcmp(basename(argv[0]), init_applet[i]) == 0)
|
|
|
|
return (*init_applet_main[i])(argc, argv);
|
|
|
|
}
|
2017-11-05 22:41:03 +01:00
|
|
|
|
2017-11-23 11:38:12 +01:00
|
|
|
if (argc > 1 && strcmp(argv[1], "-x") == 0) {
|
2017-11-09 17:54:54 +01:00
|
|
|
if (strcmp(argv[2], "magisk") == 0)
|
|
|
|
return dump_magisk(argv[3], 0755);
|
2018-06-13 18:59:08 +02:00
|
|
|
else if (strcmp(argv[2], "manager") == 0)
|
|
|
|
return dump_manager(argv[3], 0644);
|
2017-11-09 17:54:54 +01:00
|
|
|
else if (strcmp(argv[2], "magiskrc") == 0)
|
|
|
|
return dump_magiskrc(argv[3], 0755);
|
|
|
|
}
|
|
|
|
|
2017-12-05 14:05:20 +01:00
|
|
|
// Prevent file descriptor confusion
|
|
|
|
mknod("/null", S_IFCHR | 0666, makedev(1, 3));
|
|
|
|
int null = open("/null", O_RDWR | O_CLOEXEC);
|
|
|
|
unlink("/null");
|
2018-07-12 23:41:29 +02:00
|
|
|
xdup3(null, STDIN_FILENO, O_CLOEXEC);
|
|
|
|
xdup3(null, STDOUT_FILENO, O_CLOEXEC);
|
|
|
|
xdup3(null, STDERR_FILENO, O_CLOEXEC);
|
2017-12-15 21:40:54 +01:00
|
|
|
if (null > STDERR_FILENO)
|
|
|
|
close(null);
|
2017-12-05 14:05:20 +01:00
|
|
|
|
2018-04-21 14:16:59 +02:00
|
|
|
// Backup self
|
2018-07-17 00:18:36 +02:00
|
|
|
rename("/init", "/init.bak");
|
2017-11-09 17:54:54 +01:00
|
|
|
|
2018-07-17 18:45:10 +02:00
|
|
|
// Communicate with kernel using procfs and sysfs
|
|
|
|
mkdir("/proc", 0755);
|
|
|
|
xmount("proc", "/proc", "proc", 0, NULL);
|
|
|
|
mkdir("/sys", 0755);
|
|
|
|
xmount("sysfs", "/sys", "sysfs", 0, NULL);
|
|
|
|
|
2017-11-05 22:41:03 +01:00
|
|
|
struct cmdline cmd;
|
|
|
|
parse_cmdline(&cmd);
|
|
|
|
|
2018-07-17 18:45:10 +02:00
|
|
|
/* ***********
|
|
|
|
* Initialize
|
|
|
|
* ***********/
|
|
|
|
|
2017-11-05 22:41:03 +01:00
|
|
|
int root = open("/", O_RDONLY | O_CLOEXEC);
|
2018-08-06 20:20:40 +02:00
|
|
|
int mnt_system = 0;
|
|
|
|
int mnt_vendor = 0;
|
2017-11-05 22:41:03 +01:00
|
|
|
|
|
|
|
if (cmd.skip_initramfs) {
|
|
|
|
// Clear rootfs
|
2018-07-17 18:45:10 +02:00
|
|
|
excl_list = (char *[]) { "overlay", ".backup", "proc", "sys", "init.bak", NULL };
|
2018-02-13 17:57:52 +01:00
|
|
|
frm_rf(root);
|
|
|
|
} else {
|
|
|
|
// Revert original init binary
|
|
|
|
link("/.backup/init", "/init");
|
|
|
|
}
|
2017-11-05 22:41:03 +01:00
|
|
|
|
2018-08-06 20:20:40 +02:00
|
|
|
// Do not go further if system_root device is booting as recovery
|
|
|
|
if (!cmd.skip_initramfs && access("/sbin/recovery", F_OK) == 0)
|
|
|
|
goto exec_init;
|
|
|
|
|
2018-02-13 17:57:52 +01:00
|
|
|
/* ************
|
|
|
|
* Early Mount
|
|
|
|
* ************/
|
2017-11-05 22:41:03 +01:00
|
|
|
|
2018-07-17 18:45:10 +02:00
|
|
|
struct device dev;
|
2018-07-18 21:12:28 +02:00
|
|
|
char partname[32];
|
2017-11-05 22:41:03 +01:00
|
|
|
|
2018-07-17 18:45:10 +02:00
|
|
|
if (cmd.skip_initramfs) {
|
2018-07-18 21:12:28 +02:00
|
|
|
sprintf(partname, "system%s", cmd.slot);
|
|
|
|
setup_block(&dev, partname);
|
2018-07-17 18:45:10 +02:00
|
|
|
xmkdir("/system_root", 0755);
|
|
|
|
xmount(dev.path, "/system_root", "ext4", MS_RDONLY, NULL);
|
|
|
|
int system_root = open("/system_root", O_RDONLY | O_CLOEXEC);
|
|
|
|
|
|
|
|
// Clone rootfs except /system
|
|
|
|
excl_list = (char *[]) { "system", NULL };
|
|
|
|
clone_dir(system_root, root);
|
|
|
|
close(system_root);
|
|
|
|
|
|
|
|
xmkdir("/system", 0755);
|
|
|
|
xmount("/system_root/system", "/system", NULL, MS_BIND, NULL);
|
2018-07-18 21:12:28 +02:00
|
|
|
} else if (read_fstab_dt(&cmd, "system", partname) == 0) {
|
|
|
|
setup_block(&dev, partname);
|
2018-07-17 18:45:10 +02:00
|
|
|
xmount(dev.path, "/system", "ext4", MS_RDONLY, NULL);
|
|
|
|
mnt_system = 1;
|
|
|
|
}
|
2017-11-05 22:41:03 +01:00
|
|
|
|
2018-07-18 21:12:28 +02:00
|
|
|
if (read_fstab_dt(&cmd, "vendor", partname) == 0) {
|
|
|
|
setup_block(&dev, partname);
|
2018-07-17 18:45:10 +02:00
|
|
|
xmount(dev.path, "/vendor", "ext4", MS_RDONLY, NULL);
|
|
|
|
mnt_vendor = 1;
|
2018-07-17 00:18:36 +02:00
|
|
|
}
|
2017-12-06 20:21:13 +01:00
|
|
|
|
2018-07-17 18:45:10 +02:00
|
|
|
/* ****************
|
|
|
|
* Ramdisk Patches
|
|
|
|
* ****************/
|
|
|
|
|
2018-08-06 20:20:40 +02:00
|
|
|
// Handle ramdisk overlays
|
|
|
|
int fd = open("/overlay", O_RDONLY | O_CLOEXEC);
|
|
|
|
if (fd >= 0) {
|
|
|
|
mv_dir(fd, root);
|
|
|
|
close(fd);
|
|
|
|
rmdir("/overlay");
|
|
|
|
}
|
2017-11-05 22:41:03 +01:00
|
|
|
|
2018-08-06 20:20:40 +02:00
|
|
|
// Patch init.rc to load magisk scripts
|
|
|
|
int injected = 0;
|
|
|
|
char tok[4096];
|
|
|
|
FILE *fp = xfopen("/init.rc", "r");
|
|
|
|
fd = creat("/init.rc.new", 0750);
|
|
|
|
while(fgets(tok, sizeof(tok), fp)) {
|
|
|
|
if (!injected && strncmp(tok, "import", 6) == 0) {
|
|
|
|
if (strstr(tok, "init.magisk.rc")) {
|
|
|
|
injected = 1;
|
|
|
|
} else {
|
|
|
|
xwrite(fd, "import /init.magisk.rc\n", 23);
|
|
|
|
injected = 1;
|
2018-07-17 00:18:36 +02:00
|
|
|
}
|
2018-08-06 20:20:40 +02:00
|
|
|
} else if (strstr(tok, "selinux.reload_policy")) {
|
|
|
|
// Do not allow sepolicy patch
|
|
|
|
continue;
|
2018-07-17 00:18:36 +02:00
|
|
|
}
|
2018-08-06 20:20:40 +02:00
|
|
|
xwrite(fd, tok, strlen(tok));
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
close(fd);
|
|
|
|
rename("/init.rc.new", "/init.rc");
|
2018-07-17 00:18:36 +02:00
|
|
|
|
2018-08-06 20:20:40 +02:00
|
|
|
// Patch sepolicy
|
|
|
|
patch_sepolicy();
|
2018-04-21 14:16:59 +02:00
|
|
|
|
2018-08-06 20:20:40 +02:00
|
|
|
// Dump binaries
|
|
|
|
dump_magiskrc("/init.magisk.rc", 0750);
|
|
|
|
dump_magisk("/sbin/magisk", 0755);
|
|
|
|
patch_socket_name("/sbin/magisk");
|
|
|
|
rename("/init.bak", "/sbin/magiskinit");
|
2017-11-05 22:41:03 +01:00
|
|
|
|
2018-08-06 20:20:40 +02:00
|
|
|
exec_init:
|
2017-12-06 20:21:13 +01:00
|
|
|
// Clean up
|
|
|
|
close(root);
|
2018-07-13 16:14:32 +02:00
|
|
|
umount("/proc");
|
|
|
|
umount("/sys");
|
2018-07-17 00:18:36 +02:00
|
|
|
if (mnt_system)
|
2018-02-13 17:57:52 +01:00
|
|
|
umount("/system");
|
2018-07-17 00:18:36 +02:00
|
|
|
if (mnt_vendor)
|
2018-05-20 01:57:50 +02:00
|
|
|
umount("/vendor");
|
2017-12-06 20:21:13 +01:00
|
|
|
|
2017-11-23 11:38:12 +01:00
|
|
|
execv("/init", argv);
|
2017-11-05 22:41:03 +01:00
|
|
|
}
|