Magisk/native/jni/init/getinfo.cpp

146 lines
3.6 KiB
C++
Raw Normal View History

2019-05-27 00:29:43 -07:00
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/input.h>
#include <fcntl.h>
2019-07-01 22:58:19 -07:00
#include <vector>
2019-05-27 00:29:43 -07:00
2020-03-09 01:50:30 -07:00
#include <utils.hpp>
#include <logging.hpp>
2019-05-27 00:29:43 -07:00
2020-03-09 01:50:30 -07:00
#include "init.hpp"
2019-05-27 00:29:43 -07:00
using namespace std;
static void parse_cmdline(const std::function<void (std::string_view, const char *)> &fn) {
char cmdline[4096];
2020-05-03 22:49:54 -07:00
int fd = xopen("/proc/cmdline", O_RDONLY | O_CLOEXEC);
2019-05-27 00:29:43 -07:00
cmdline[read(fd, cmdline, sizeof(cmdline))] = '\0';
close(fd);
char *tok, *eql, *tmp, *saveptr;
saveptr = cmdline;
while ((tok = strtok_r(nullptr, " \n", &saveptr)) != nullptr) {
eql = strchr(tok, '=');
if (eql) {
*eql = '\0';
if (eql[1] == '"') {
tmp = strchr(saveptr, '"');
if (tmp != nullptr) {
*tmp = '\0';
saveptr[-1] = ' ';
saveptr = tmp + 1;
eql++;
}
}
fn(tok, eql + 1);
} else {
fn(tok, "");
}
}
}
#define test_bit(bit, array) (array[bit / 8] & (1 << (bit % 8)))
static bool check_key_combo() {
uint8_t bitmask[(KEY_MAX + 1) / 8];
vector<int> events;
constexpr const char *name = "/event";
for (int minor = 64; minor < 96; ++minor) {
if (mknod(name, S_IFCHR | 0444, makedev(13, minor))) {
PLOGE("mknod");
continue;
}
int fd = open(name, O_RDONLY | O_CLOEXEC);
unlink(name);
if (fd < 0)
continue;
memset(bitmask, 0, sizeof(bitmask));
ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(bitmask)), bitmask);
if (test_bit(KEY_VOLUMEUP, bitmask))
events.push_back(fd);
}
if (events.empty())
return false;
2019-09-25 23:55:39 -04:00
run_finally fin([&]() -> void {
2019-05-27 00:29:43 -07:00
for (const int &fd : events)
close(fd);
});
// Return true if volume key up is hold for more than 3 seconds
int count = 0;
for (int i = 0; i < 500; ++i) {
for (const int &fd : events) {
memset(bitmask, 0, sizeof(bitmask));
ioctl(fd, EVIOCGKEY(sizeof(bitmask)), bitmask);
if (test_bit(KEY_VOLUMEUP, bitmask)) {
count++;
break;
}
}
if (count >= 300) {
LOGD("KEY_VOLUMEUP detected: disable system-as-root\n");
return true;
}
// Check every 10ms
usleep(10000);
}
return false;
}
2019-06-15 22:25:09 -07:00
void load_kernel_info(cmdline *cmd) {
2019-05-27 00:29:43 -07:00
// Communicate with kernel using procfs and sysfs
xmkdir("/proc", 0755);
xmount("proc", "/proc", "proc", 0, nullptr);
xmkdir("/sys", 0755);
xmount("sysfs", "/sys", "sysfs", 0, nullptr);
2020-04-21 20:04:29 -07:00
// Disable kmsg rate limiting
if (FILE *rate = fopen("/proc/sys/kernel/printk_devkmsg", "w")) {
2020-04-21 20:04:29 -07:00
fprintf(rate, "on\n");
fclose(rate);
}
2019-05-27 00:29:43 -07:00
parse_cmdline([&](auto key, auto value) -> void {
if (key == "androidboot.slot_suffix") {
2019-06-15 22:25:09 -07:00
strcpy(cmd->slot, value);
2019-05-27 00:29:43 -07:00
} else if (key == "androidboot.slot") {
2019-06-15 22:25:09 -07:00
cmd->slot[0] = '_';
strcpy(cmd->slot + 1, value);
2019-05-27 00:29:43 -07:00
} else if (key == "skip_initramfs") {
2019-12-06 12:02:34 -05:00
cmd->skip_initramfs = true;
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 00:47:29 -07:00
} else if (key == "androidboot.force_normal_boot") {
cmd->force_normal_boot = value[0] == '1';
2019-05-27 00:29:43 -07:00
} else if (key == "androidboot.android_dt_dir") {
2019-06-15 22:25:09 -07:00
strcpy(cmd->dt_dir, value);
2019-05-27 00:29:43 -07:00
} else if (key == "androidboot.hardware") {
2020-05-03 22:49:54 -07:00
strcpy(cmd->hardware, value);
} else if (key == "androidboot.hardware.platform") {
strcpy(cmd->hardware_plat, value);
2019-05-27 00:29:43 -07:00
}
});
parse_prop_file("/.backup/.magisk", [=](auto key, auto value) -> bool {
if (key == "RECOVERYMODE" && value == "true") {
LOGD("Running in recovery mode, waiting for key...\n");
cmd->skip_initramfs = !check_key_combo();
return false;
}
2019-05-27 00:29:43 -07:00
return true;
});
2019-06-15 22:25:09 -07:00
if (cmd->dt_dir[0] == '\0')
strcpy(cmd->dt_dir, DEFAULT_DT_DIR);
2019-05-27 00:29:43 -07:00
2020-05-03 22:49:54 -07:00
LOGD("Device info:\n");
2019-12-06 12:02:34 -05:00
LOGD("skip_initramfs=[%d]\n", cmd->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 00:47:29 -07:00
LOGD("force_normal_boot=[%d]\n", cmd->force_normal_boot);
2019-06-15 22:25:09 -07:00
LOGD("slot=[%s]\n", cmd->slot);
LOGD("dt_dir=[%s]\n", cmd->dt_dir);
2020-05-03 22:49:54 -07:00
LOGD("hardware=[%s]\n", cmd->hardware);
LOGD("hardware.platform=[%s]\n", cmd->hardware_plat);
2019-05-27 00:29:43 -07:00
}