vvb2060 2021-05-26 01:21:54 +08:00 committed by John Wu
parent 34e5a7cd24
commit 79e8962854
4 changed files with 97 additions and 39 deletions

View File

@ -13,33 +13,48 @@ using namespace std;
vector<string> mount_list; vector<string> mount_list;
template<typename Func> static vector<pair<string, string>> parse_cmdline(const string &cmdline) {
static void parse_cmdline(const Func &fn) { vector<pair<string, string>> result;
char cmdline[4096]; size_t base = 0;
int fd = xopen("/proc/cmdline", O_RDONLY | O_CLOEXEC); while (true) {
cmdline[read(fd, cmdline, sizeof(cmdline))] = '\0'; auto found = base;
close(fd); while (((found = cmdline.find_first_of(" \"", found)) != string::npos) &&
(cmdline[found] == '"')) {
char *tok, *eql, *tmp, *saveptr; if ((found = cmdline.find('"', found + 1)) == string::npos)
saveptr = cmdline; break;
while ((tok = strtok_r(nullptr, " \n", &saveptr)) != nullptr) { found++;
eql = strchr(tok, '='); }
if (eql) { string piece;
*eql = '\0'; auto source = cmdline.substr(base, found - base);
if (eql[1] == '"') { remove_copy(source.begin(), source.end(), back_insert_iterator<string>(piece), '"');
tmp = strchr(saveptr, '"'); auto equal_sign = piece.find('=');
if (tmp != nullptr) { if (equal_sign == string::npos) {
*tmp = '\0'; if (!piece.empty())
saveptr[-1] = ' '; result.emplace_back(std::move(piece), "");
saveptr = tmp + 1;
eql++;
}
}
fn(tok, eql + 1);
} else { } else {
fn(tok, ""); result.emplace_back(piece.substr(0, equal_sign), piece.substr(equal_sign + 1));
}
if (found == string::npos)
break;
base = found + 1;
}
return result;
}
static vector<pair<string, string>> parse_bootconfig(const string &bootconfig) {
vector<pair<string, string>> result;
for (auto &line : split(bootconfig, "\n")) {
line.erase(remove(line.begin(), line.end(), '"'), line.end());
auto equal_sign = line.find('=');
if (equal_sign == string::npos) {
if (!line.empty())
result.emplace_back(move(line), "");
} else {
result.emplace_back(rtrim(line.substr(0, equal_sign)),
ltrim(line.substr(equal_sign + 1)));
} }
} }
return result;
} }
#define test_bit(bit, array) (array[bit / 8] & (1 << (bit % 8))) #define test_bit(bit, array) (array[bit / 8] & (1 << (bit % 8)))
@ -155,12 +170,12 @@ void load_kernel_info(cmdline *cmd) {
// Log to kernel // Log to kernel
setup_klog(); setup_klog();
parse_cmdline([=](string_view key, const char *value) { for (const auto&[key, value] : parse_cmdline(full_read("/proc/cmdline"))) {
if (key == "androidboot.slot_suffix") { if (key == "androidboot.slot_suffix") {
strcpy(cmd->slot, value); strcpy(cmd->slot, value.data());
} else if (key == "androidboot.slot") { } else if (key == "androidboot.slot") {
cmd->slot[0] = '_'; cmd->slot[0] = '_';
strcpy(cmd->slot + 1, value); strcpy(cmd->slot + 1, value.data());
} else if (key == "skip_initramfs") { } else if (key == "skip_initramfs") {
cmd->skip_initramfs = true; cmd->skip_initramfs = true;
} else if (key == "androidboot.force_normal_boot") { } else if (key == "androidboot.force_normal_boot") {
@ -168,15 +183,15 @@ void load_kernel_info(cmdline *cmd) {
} else if (key == "rootwait") { } else if (key == "rootwait") {
cmd->rootwait = true; cmd->rootwait = true;
} else if (key == "androidboot.android_dt_dir") { } else if (key == "androidboot.android_dt_dir") {
strcpy(cmd->dt_dir, value); strcpy(cmd->dt_dir, value.data());
} else if (key == "androidboot.hardware") { } else if (key == "androidboot.hardware") {
strcpy(cmd->hardware, value); strcpy(cmd->hardware, value.data());
} else if (key == "androidboot.hardware.platform") { } else if (key == "androidboot.hardware.platform") {
strcpy(cmd->hardware_plat, value); strcpy(cmd->hardware_plat, value.data());
} else if (key == "androidboot.fstab_suffix") { } else if (key == "androidboot.fstab_suffix") {
strcpy(cmd->fstab_suffix, value); strcpy(cmd->fstab_suffix, value.data());
} }
}); }
LOGD("Kernel cmdline info:\n"); LOGD("Kernel cmdline info:\n");
LOGD("skip_initramfs=[%d]\n", cmd->skip_initramfs); LOGD("skip_initramfs=[%d]\n", cmd->skip_initramfs);
@ -188,6 +203,30 @@ void load_kernel_info(cmdline *cmd) {
LOGD("hardware=[%s]\n", cmd->hardware); LOGD("hardware=[%s]\n", cmd->hardware);
LOGD("hardware.platform=[%s]\n", cmd->hardware_plat); LOGD("hardware.platform=[%s]\n", cmd->hardware_plat);
for (const auto&[key, value] : parse_bootconfig(full_read("/proc/bootconfig"))) {
if (key == "androidboot.slot_suffix") {
strcpy(cmd->slot, value.data());
} else if (key == "androidboot.force_normal_boot") {
cmd->force_normal_boot = value[0] == '1';
} else if (key == "androidboot.android_dt_dir") {
strcpy(cmd->dt_dir, value.data());
} else if (key == "androidboot.hardware") {
strcpy(cmd->hardware, value.data());
} else if (key == "androidboot.hardware.platform") {
strcpy(cmd->hardware_plat, value.data());
} else if (key == "androidboot.fstab_suffix") {
strcpy(cmd->fstab_suffix, value.data());
}
}
LOGD("Boot config info:\n");
LOGD("force_normal_boot=[%d]\n", cmd->force_normal_boot);
LOGD("slot=[%s]\n", cmd->slot);
LOGD("dt_dir=[%s]\n", cmd->dt_dir);
LOGD("fstab_suffix=[%s]\n", cmd->fstab_suffix);
LOGD("hardware=[%s]\n", cmd->hardware);
LOGD("hardware.platform=[%s]\n", cmd->hardware_plat);
parse_prop_file("/.backup/.magisk", [=](auto key, auto value) -> bool { parse_prop_file("/.backup/.magisk", [=](auto key, auto value) -> bool {
if (key == "RECOVERYMODE" && value == "true") { if (key == "RECOVERYMODE" && value == "true") {
LOGD("Running in recovery mode, waiting for key...\n"); LOGD("Running in recovery mode, waiting for key...\n");

View File

@ -10,13 +10,6 @@
using namespace std; using namespace std;
static string rtrim(string &&str) {
// Trim space, newline, and null byte from end of string
while (memchr(" \n\r", str[str.length() - 1], 4))
str.pop_back();
return std::move(str);
}
struct devinfo { struct devinfo {
int major; int major;
int minor; int minor;

View File

@ -188,3 +188,16 @@ string &replace_all(string &str, string_view from, string_view to) {
} }
return str; return str;
} }
vector<string> split(const string& s, const string& delimiters) {
vector<string> result;
size_t base = 0;
size_t found;
while (true) {
found = s.find_first_of(delimiters, base);
result.push_back(s.substr(base, found - base));
if (found == string::npos) break;
base = found + 1;
}
return result;
}

View File

@ -92,6 +92,18 @@ static inline bool str_starts(std::string_view s, std::string_view ss) {
static inline bool str_ends(std::string_view s, std::string_view ss) { static inline bool str_ends(std::string_view s, std::string_view ss) {
return s.size() >= ss.size() && s.compare(s.size() - ss.size(), std::string::npos, ss) == 0; return s.size() >= ss.size() && s.compare(s.size() - ss.size(), std::string::npos, ss) == 0;
} }
static inline std::string ltrim(std::string &&s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
return std::move(s);
}
static inline std::string rtrim(std::string &&s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(ch) && ch != '\0';
}).base(), s.end());
return std::move(s);
}
int fork_dont_care(); int fork_dont_care();
int fork_no_orphan(); int fork_no_orphan();
@ -101,6 +113,7 @@ uint32_t binary_gcd(uint32_t u, uint32_t v);
int switch_mnt_ns(int pid); int switch_mnt_ns(int pid);
int gen_rand_str(char *buf, int len, bool varlen = true); int gen_rand_str(char *buf, int len, bool varlen = true);
std::string &replace_all(std::string &str, std::string_view from, std::string_view to); std::string &replace_all(std::string &str, std::string_view from, std::string_view to);
std::vector<std::string> split(const std::string& s, const std::string& delimiters);
struct exec_t { struct exec_t {
bool err = false; bool err = false;