Let MagiskBoot handle dtb fstab patching

This commit is contained in:
topjohnwu 2021-01-14 06:20:12 -08:00
parent bb303d2da1
commit 76061296c9
2 changed files with 38 additions and 68 deletions

View File

@ -38,85 +38,55 @@ void FirstStageInit::prepare() {
rename("/.backup/init", "/init"); rename("/.backup/init", "/init");
} }
// Try to load fstab from dt
vector<fstab_entry> fstab;
read_dt_fstab(fstab);
char fstab_file[128]; char fstab_file[128];
fstab_file[0] = '\0'; fstab_file[0] = '\0';
// Find existing fstab file // Find existing fstab file
for (const char *hw : { cmd->fstab_suffix, cmd->hardware, cmd->hardware_plat }) { for (const char *suffix : { cmd->fstab_suffix, cmd->hardware, cmd->hardware_plat }) {
if (hw[0] == '\0') if (suffix[0] == '\0')
continue; continue;
sprintf(fstab_file, "fstab.%s", hw); for (const char *prefix: { "odm/etc/fstab", "vendor/etc/fstab", "fstab" }) {
if (access(fstab_file, F_OK) != 0) { sprintf(fstab_file, "%s.%s", prefix, suffix);
fstab_file[0] = '\0'; if (access(fstab_file, F_OK) != 0) {
continue; fstab_file[0] = '\0';
} else { } else {
LOGD("Found fstab file: %s\n", fstab_file); LOGD("Found fstab file: %s\n", fstab_file);
break; goto exit_loop;
}
} }
} }
exit_loop:
if (fstab.empty()) { if (fstab_file[0] == '\0') {
// fstab has to be somewhere in ramdisk LOGI("Cannot find fstab file in ramdisk!\n");
if (fstab_file[0] == '\0') { return;
LOGE("Cannot find fstab file in ramdisk!\n"); }
return;
}
// Parse and load fstab file // Parse and load fstab file
file_readline(fstab_file, [&](string_view l) -> bool { vector<fstab_entry> fstab;
if (l[0] == '#' || l.length() == 1) file_readline(fstab_file, [&](string_view l) -> bool {
return true; if (l[0] == '#' || l.length() == 1)
char *line = (char *) l.data();
int dev0, dev1, mnt_point0, mnt_point1, type0, type1,
mnt_flags0, mnt_flags1, fsmgr_flags0, fsmgr_flags1;
sscanf(line, "%n%*s%n %n%*s%n %n%*s%n %n%*s%n %n%*s%n",
&dev0, &dev1, &mnt_point0, &mnt_point1, &type0, &type1,
&mnt_flags0, &mnt_flags1, &fsmgr_flags0, &fsmgr_flags1);
fstab_entry entry;
set_info(dev);
set_info(mnt_point);
set_info(type);
set_info(mnt_flags);
set_info(fsmgr_flags);
fstab.emplace_back(std::move(entry));
return true; return true;
}); char *line = (char *) l.data();
} else {
// All dt fstab entries should be first_stage_mount
for (auto &entry : fstab) {
if (!str_contains(entry.fsmgr_flags, "first_stage_mount")) {
if (!entry.fsmgr_flags.empty())
entry.fsmgr_flags += ',';
entry.fsmgr_flags += "first_stage_mount";
}
}
// Dump dt fstab to fstab file in rootfs int dev0, dev1, mnt_point0, mnt_point1, type0, type1,
if (fstab_file[0] == '\0') { mnt_flags0, mnt_flags1, fsmgr_flags0, fsmgr_flags1;
const char *suffix =
cmd->fstab_suffix[0] ? cmd->fstab_suffix :
(cmd->hardware[0] ? cmd->hardware :
(cmd->hardware_plat[0] ? cmd->hardware_plat : nullptr));
if (suffix == nullptr) {
LOGE("Cannot determine fstab suffix!\n");
return;
}
sprintf(fstab_file, "fstab.%s", suffix);
}
// Patch init to force IsDtFstabCompatible() return false sscanf(line, "%n%*s%n %n%*s%n %n%*s%n %n%*s%n %n%*s%n",
auto init = mmap_data::rw("/init"); &dev0, &dev1, &mnt_point0, &mnt_point1, &type0, &type1,
init.patch({ make_pair("android,fstab", "xxx") }); &mnt_flags0, &mnt_flags1, &fsmgr_flags0, &fsmgr_flags1);
}
fstab_entry entry;
set_info(dev);
set_info(mnt_point);
set_info(type);
set_info(mnt_flags);
set_info(fsmgr_flags);
fstab.emplace_back(std::move(entry));
return true;
});
{ {
LOGD("Write fstab file: %s\n", fstab_file); LOGD("Write fstab file: %s\n", fstab_file);

View File

@ -207,7 +207,7 @@ static bool fdt_patch(void *fdt) {
int node; int node;
fdt_for_each_subnode(node, fdt, fstab) { fdt_for_each_subnode(node, fdt, fstab) {
const char *name = fdt_get_name(fdt, node, nullptr); const char *name = fdt_get_name(fdt, node, nullptr);
// Always patch verity if 2SI // Force remove AVB for 2SI since it may bootloop some devices
int len; int len;
auto value = (const char *) fdt_getprop(fdt, node, "fsmgr_flags", &len); auto value = (const char *) fdt_getprop(fdt, node, "fsmgr_flags", &len);
string copy(value, len); string copy(value, len);