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
|
|
|
|
2022-05-12 02:03:42 -07:00
|
|
|
#include <base.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;
|
|
|
|
|
2021-05-31 20:51:25 +08:00
|
|
|
template<char... cs> using chars = integer_sequence<char, cs...>;
|
|
|
|
|
2021-10-26 00:35:55 -07:00
|
|
|
// If quoted, parsing ends when we find char in [breaks]
|
|
|
|
// If not quoted, parsing ends when we find char in [breaks] + [escapes]
|
2021-05-31 20:51:25 +08:00
|
|
|
template<char... escapes, char... breaks>
|
2021-10-26 00:35:55 -07:00
|
|
|
static string extract_quoted_str_until(chars<escapes...>, chars<breaks...>,
|
|
|
|
string_view str, size_t &pos, bool "ed) {
|
2021-05-31 20:51:25 +08:00
|
|
|
string result;
|
|
|
|
char match_array[] = {escapes..., breaks..., '"'};
|
2021-10-26 00:35:55 -07:00
|
|
|
string_view match(match_array, std::size(match_array));
|
|
|
|
for (size_t cur = pos;; ++cur) {
|
|
|
|
cur = str.find_first_of(match, cur);
|
|
|
|
if (cur == string_view::npos ||
|
|
|
|
((str[cur] == breaks) || ...) ||
|
|
|
|
(!quoted && ((str[cur] == escapes) || ...))) {
|
|
|
|
result.append(str.substr(pos, cur - pos));
|
|
|
|
pos = cur;
|
2021-05-31 20:51:25 +08:00
|
|
|
return result;
|
2021-05-26 01:21:54 +08:00
|
|
|
}
|
2021-10-26 00:35:55 -07:00
|
|
|
if (str[cur] == '"') {
|
2021-05-31 20:51:25 +08:00
|
|
|
quoted = !quoted;
|
2021-10-26 00:35:55 -07:00
|
|
|
result.append(str.substr(pos, cur - pos));
|
|
|
|
pos = cur + 1;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
}
|
2021-05-26 01:21:54 +08:00
|
|
|
}
|
|
|
|
|
2021-10-26 00:35:55 -07:00
|
|
|
// Parse string into key value pairs.
|
2024-08-08 18:11:40 +08:00
|
|
|
// The string format: [delim][key][padding][eq][padding][value][delim]
|
|
|
|
template<char delim, char eq, char... padding>
|
2021-10-26 00:35:55 -07:00
|
|
|
static kv_pairs parse_impl(chars<padding...>, string_view str) {
|
|
|
|
kv_pairs kv;
|
2024-08-08 18:11:40 +08:00
|
|
|
char skip_array[] = {eq, padding...};
|
2021-10-26 00:35:55 -07:00
|
|
|
string_view skip(skip_array, std::size(skip_array));
|
2021-05-31 20:51:25 +08:00
|
|
|
bool quoted = false;
|
2021-10-26 00:35:55 -07:00
|
|
|
for (size_t pos = 0u; pos < str.size(); pos = str.find_first_not_of(delim, pos)) {
|
|
|
|
auto key = extract_quoted_str_until(
|
2024-08-08 18:11:40 +08:00
|
|
|
chars<padding..., delim>{}, chars<eq>{}, str, pos, quoted);
|
2021-10-26 00:35:55 -07:00
|
|
|
pos = str.find_first_not_of(skip, pos);
|
|
|
|
if (pos == string_view::npos || str[pos] == delim) {
|
2021-05-31 20:51:25 +08:00
|
|
|
kv.emplace_back(key, "");
|
|
|
|
continue;
|
2021-05-26 01:21:54 +08:00
|
|
|
}
|
2021-10-26 00:35:55 -07:00
|
|
|
auto value = extract_quoted_str_until(chars<delim>{}, chars<>{}, str, pos, quoted);
|
2021-05-31 20:51:25 +08:00
|
|
|
kv.emplace_back(key, value);
|
2021-05-26 01:21:54 +08:00
|
|
|
}
|
2021-05-31 20:51:25 +08:00
|
|
|
return kv;
|
|
|
|
}
|
|
|
|
|
2021-10-26 00:35:55 -07:00
|
|
|
static kv_pairs parse_cmdline(string_view str) {
|
2024-08-08 18:11:40 +08:00
|
|
|
return parse_impl<' ', '='>(chars<>{}, str);
|
2021-05-31 20:51:25 +08:00
|
|
|
}
|
2021-10-26 00:35:55 -07:00
|
|
|
static kv_pairs parse_bootconfig(string_view str) {
|
2024-08-08 18:11:40 +08:00
|
|
|
return parse_impl<'\n', '='>(chars<' '>{}, str);
|
|
|
|
}
|
|
|
|
static kv_pairs parse_partition_map(std::string_view str) {
|
|
|
|
return parse_impl<';', ','>(chars<>{}, str);
|
2019-05-27 00:29:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#define test_bit(bit, array) (array[bit / 8] & (1 << (bit % 8)))
|
|
|
|
|
|
|
|
static bool check_key_combo() {
|
Introduce new sepolicy injection mechanism
In the current implementation, Magisk will either have to recreate
all early mount implementation (for legacy SAR and rootfs devices) or
delegate early mount to first stage init (for 2SI devices) to access
required partitions for loading sepolicy. It then has to recreate the
split sepolicy loading implementation in-house, apply patches, then
dump the compiled + patched policies into monolithic format somewhere.
Finally, it patches the original init to force it to load the sepolicy
file we just created.
With the increasing complexity involved in early mount and split
sepolicy (there is even APEX module involved in the future!),
it is about time to rethink Magisk's sepolicy strategy as rebuilding
init's functionality is not scalable and easy to maintain.
In this commit, instead of building sepolicy ourselves, we mock
selinuxfs with FIFO files connected to a pre-init daemon, waiting
for the actual init process to directly write the sepolicy file into
MagiskInit. We then patch the file and load it into the kernel. Some
FIFO tricks has to be used to hijack the original init process's
control flow and prevent race conditions, details are directly in the
comments in code.
At the moment, only system-as-root (read-only root) support is added.
Support for legacy rootfs devices will come with a follow up commit.
2022-03-16 00:31:53 -07:00
|
|
|
LOGD("Running in recovery mode, waiting for key...\n");
|
2020-12-30 22:11:24 -08:00
|
|
|
uint8_t bitmask[(KEY_MAX + 1) / 8];
|
|
|
|
vector<int> events;
|
|
|
|
constexpr const char *name = "/event";
|
|
|
|
|
|
|
|
for (int minor = 64; minor < 96; ++minor) {
|
|
|
|
if (xmknod(name, S_IFCHR | 0444, makedev(13, minor)))
|
|
|
|
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);
|
|
|
|
else
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
if (events.empty())
|
|
|
|
return false;
|
|
|
|
|
2021-05-31 20:51:25 +08:00
|
|
|
run_finally fin([&] { for_each(events.begin(), events.end(), close); });
|
2020-12-30 22:11:24 -08:00
|
|
|
|
|
|
|
// Return true if volume up key is held 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-05-27 00:29:43 -07:00
|
|
|
}
|
|
|
|
|
2025-01-30 18:39:34 +08:00
|
|
|
void BootConfig::set(const kv_pairs &kv) noexcept {
|
2021-10-26 00:35:55 -07:00
|
|
|
for (const auto &[key, value] : kv) {
|
|
|
|
if (key == "androidboot.slot_suffix") {
|
2022-05-10 11:57:14 +08:00
|
|
|
// Many Amlogic devices are A-only but have slot_suffix...
|
|
|
|
if (value == "normal") {
|
|
|
|
LOGW("Skip invalid androidboot.slot_suffix=[normal]\n");
|
|
|
|
continue;
|
|
|
|
}
|
2025-01-30 18:39:34 +08:00
|
|
|
strscpy(slot.data(), value.data(), slot.size());
|
2021-10-26 00:35:55 -07:00
|
|
|
} else if (key == "androidboot.slot") {
|
|
|
|
slot[0] = '_';
|
2025-01-30 18:39:34 +08:00
|
|
|
strscpy(slot.data() + 1, value.data(), slot.size() - 1);
|
2021-10-26 00:35:55 -07:00
|
|
|
} else if (key == "skip_initramfs") {
|
|
|
|
skip_initramfs = true;
|
|
|
|
} else if (key == "androidboot.force_normal_boot") {
|
|
|
|
force_normal_boot = !value.empty() && value[0] == '1';
|
|
|
|
} else if (key == "rootwait") {
|
|
|
|
rootwait = true;
|
|
|
|
} else if (key == "androidboot.android_dt_dir") {
|
2025-01-30 18:39:34 +08:00
|
|
|
strscpy(dt_dir.data(), value.data(), dt_dir.size());
|
2021-10-26 00:35:55 -07:00
|
|
|
} else if (key == "androidboot.hardware") {
|
2025-01-30 18:39:34 +08:00
|
|
|
strscpy(hardware.data(), value.data(), hardware.size());
|
2021-10-26 00:35:55 -07:00
|
|
|
} else if (key == "androidboot.hardware.platform") {
|
2025-01-30 18:39:34 +08:00
|
|
|
strscpy(hardware_plat.data(), value.data(), hardware_plat.size());
|
2021-10-26 00:35:55 -07:00
|
|
|
} else if (key == "androidboot.fstab_suffix") {
|
2025-01-30 18:39:34 +08:00
|
|
|
strscpy(fstab_suffix.data(), value.data(), fstab_suffix.size());
|
2022-01-19 05:12:11 -08:00
|
|
|
} else if (key == "qemu") {
|
|
|
|
emulator = true;
|
2024-12-02 17:14:38 -08:00
|
|
|
} else if (key == "androidboot.partition_map") {
|
|
|
|
// androidboot.partition_map allows mapping a partition name to a raw block device.
|
|
|
|
// For example, "androidboot.partition_map=vdb,metadata;vdc,userdata" maps
|
|
|
|
// "vdb" to "metadata", and "vdc" to "userdata".
|
|
|
|
// https://android.googlesource.com/platform/system/core/+/refs/heads/android13-release/init/devices.cpp#191
|
2025-01-30 18:39:34 +08:00
|
|
|
for (const auto &[k, v]: parse_partition_map(value)) {
|
|
|
|
partition_map.emplace_back(k, v);
|
|
|
|
}
|
2021-10-26 00:35:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define read_dt(name, key) \
|
2025-01-30 18:39:34 +08:00
|
|
|
ssprintf(file_name, sizeof(file_name), "%s/" name, dt_dir.data()); \
|
2021-10-26 00:35:55 -07:00
|
|
|
if (access(file_name, R_OK) == 0) { \
|
|
|
|
string data = full_read(file_name); \
|
|
|
|
if (!data.empty()) { \
|
|
|
|
data.pop_back(); \
|
2025-01-30 18:39:34 +08:00
|
|
|
strscpy(key.data(), data.data(), key.size()); \
|
2021-10-26 00:35:55 -07:00
|
|
|
} \
|
2020-12-09 20:22:17 +08:00
|
|
|
}
|
|
|
|
|
2025-01-30 18:39:34 +08:00
|
|
|
void BootConfig::init() noexcept {
|
2024-12-02 17:14:38 -08:00
|
|
|
set(parse_cmdline(full_read("/proc/cmdline")));
|
|
|
|
set(parse_bootconfig(full_read("/proc/bootconfig")));
|
2021-05-26 01:21:54 +08:00
|
|
|
|
2024-12-02 17:14:38 -08:00
|
|
|
parse_prop_file("/.backup/.magisk", [&](auto key, auto value) -> bool {
|
2020-12-30 22:11:24 -08:00
|
|
|
if (key == "RECOVERYMODE" && value == "true") {
|
2024-12-02 17:14:38 -08:00
|
|
|
skip_initramfs = emulator || !check_key_combo();
|
2020-12-30 22:11:24 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2024-12-02 17:14:38 -08:00
|
|
|
if (dt_dir[0] == '\0')
|
2025-01-30 18:39:34 +08:00
|
|
|
strscpy(dt_dir.data(), DEFAULT_DT_DIR, dt_dir.size());
|
2020-12-30 22:11:24 -08:00
|
|
|
|
|
|
|
char file_name[128];
|
|
|
|
read_dt("fstab_suffix", fstab_suffix)
|
|
|
|
read_dt("hardware", hardware)
|
|
|
|
read_dt("hardware.platform", hardware_plat)
|
2022-01-20 00:18:46 -08:00
|
|
|
|
|
|
|
LOGD("Device config:\n");
|
2024-12-02 17:14:38 -08:00
|
|
|
print();
|
2024-08-07 12:13:12 +08:00
|
|
|
}
|