2020-12-06 12:07:47 +01:00
|
|
|
#include <sys/mount.h>
|
|
|
|
|
2020-04-12 14:34:56 +02:00
|
|
|
#include <magisk.hpp>
|
2020-04-01 13:39:28 +02:00
|
|
|
#include <utils.hpp>
|
2020-04-12 14:34:56 +02:00
|
|
|
#include <socket.hpp>
|
2020-04-01 13:39:28 +02:00
|
|
|
|
|
|
|
#include "init.hpp"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
Force init to load fstab from file in 2SI
Patching DTBs is proven to be difficult and problematic as there are
tons of different formats out there. Adding support for all the formats
in magiskboot has been quite an headache in the past year, and it still
definitely does not cover all possible cases of them out there.
There is another issue: fake dt fstabs. Some super old devices do not
have device trees in their boot images, so some custom ROM developers
had came up with a "genius" solution: hardcode fstab entries directly
in the kernel source code and create fake device tree nodes even if
Android 10+ init can graciously take fstab files instead (-_-) 。。。
And there is YET another issue: DTBs are not always in boot images!
Google is crazy enough to litter DTBs all over the place, it is like
they cannot make up their minds (duh). This means the dt fstabs can be
either concatnated after the kernel (1), in the DTB partition (2), in
the DTBO partition (3), in the recovery_dtbo section in boot images (4),
or in the dtb section in boot images (5). FIVE f**king places, how can
anyone keep up with that!
With Android 10+ that uses 2 stage inits, it is crutual for Magisk to
be able to modify fstab mount points in order to let the original init
mount partitions for us, but NOT switch root and continue booting. For
devices using dt for early mount fstab, we used to patch the DTB at
install time with magiskboot. However these changes are permanent and
cannot be restored back at reinstallation.
With this commit, Magisk will read dt fstabs and write them to ramdisk
at boot time. And in that case, the init binary will also be patched
to force it to NEVER use fstabs in device-tree. By doing so, we can
unify ramdisk based 2SI fstab patching as basically we are just patching
fstab files. This also means we can manipulate fstab whatever Magisk
needs in the future without the need to going through the headache that
is patching DTBs at installation.
2020-05-04 11:21:51 +02:00
|
|
|
void fstab_entry::to_file(FILE *fp) {
|
2020-12-31 07:11:24 +01:00
|
|
|
fprintf(fp, "%s %s %s %s %s\n", dev.data(), mnt_point.data(),
|
|
|
|
type.data(), mnt_flags.data(), fsmgr_flags.data());
|
2020-04-01 13:39:28 +02:00
|
|
|
}
|
|
|
|
|
Force init to load fstab from file in 2SI
Patching DTBs is proven to be difficult and problematic as there are
tons of different formats out there. Adding support for all the formats
in magiskboot has been quite an headache in the past year, and it still
definitely does not cover all possible cases of them out there.
There is another issue: fake dt fstabs. Some super old devices do not
have device trees in their boot images, so some custom ROM developers
had came up with a "genius" solution: hardcode fstab entries directly
in the kernel source code and create fake device tree nodes even if
Android 10+ init can graciously take fstab files instead (-_-) 。。。
And there is YET another issue: DTBs are not always in boot images!
Google is crazy enough to litter DTBs all over the place, it is like
they cannot make up their minds (duh). This means the dt fstabs can be
either concatnated after the kernel (1), in the DTB partition (2), in
the DTBO partition (3), in the recovery_dtbo section in boot images (4),
or in the dtb section in boot images (5). FIVE f**king places, how can
anyone keep up with that!
With Android 10+ that uses 2 stage inits, it is crutual for Magisk to
be able to modify fstab mount points in order to let the original init
mount partitions for us, but NOT switch root and continue booting. For
devices using dt for early mount fstab, we used to patch the DTB at
install time with magiskboot. However these changes are permanent and
cannot be restored back at reinstallation.
With this commit, Magisk will read dt fstabs and write them to ramdisk
at boot time. And in that case, the init binary will also be patched
to force it to NEVER use fstabs in device-tree. By doing so, we can
unify ramdisk based 2SI fstab patching as basically we are just patching
fstab files. This also means we can manipulate fstab whatever Magisk
needs in the future without the need to going through the headache that
is patching DTBs at installation.
2020-05-04 11:21:51 +02:00
|
|
|
#define set_info(val) \
|
|
|
|
line[val##1] = '\0'; \
|
|
|
|
entry.val = &line[val##0];
|
|
|
|
|
2020-04-01 13:39:28 +02:00
|
|
|
#define FSR "/first_stage_ramdisk"
|
|
|
|
|
2020-05-05 10:39:19 +02:00
|
|
|
extern uint32_t patch_verity(void *buf, uint32_t size);
|
|
|
|
|
2020-04-19 13:56:56 +02:00
|
|
|
void FirstStageInit::prepare() {
|
2020-12-31 07:11:24 +01:00
|
|
|
if (cmd->force_normal_boot) {
|
|
|
|
xmkdirs(FSR "/system/bin", 0755);
|
|
|
|
rename("/init" /* magiskinit */, FSR "/system/bin/init");
|
|
|
|
symlink("/system/bin/init", FSR "/init");
|
|
|
|
rename("/.backup", FSR "/.backup");
|
|
|
|
rename("/overlay.d", FSR "/overlay.d");
|
|
|
|
xsymlink("/system/bin/init", "/init");
|
|
|
|
|
|
|
|
chdir(FSR);
|
|
|
|
} else {
|
|
|
|
xmkdir("/system", 0755);
|
|
|
|
xmkdir("/system/bin", 0755);
|
|
|
|
rename("/init" /* magiskinit */ , "/system/bin/init");
|
|
|
|
rename("/.backup/init", "/init");
|
|
|
|
}
|
|
|
|
|
|
|
|
char fstab_file[128];
|
|
|
|
fstab_file[0] = '\0';
|
|
|
|
|
|
|
|
// Find existing fstab file
|
2021-01-14 15:20:12 +01:00
|
|
|
for (const char *suffix : { cmd->fstab_suffix, cmd->hardware, cmd->hardware_plat }) {
|
|
|
|
if (suffix[0] == '\0')
|
2020-12-31 07:11:24 +01:00
|
|
|
continue;
|
2021-01-14 15:20:12 +01:00
|
|
|
for (const char *prefix: { "odm/etc/fstab", "vendor/etc/fstab", "fstab" }) {
|
|
|
|
sprintf(fstab_file, "%s.%s", prefix, suffix);
|
|
|
|
if (access(fstab_file, F_OK) != 0) {
|
|
|
|
fstab_file[0] = '\0';
|
|
|
|
} else {
|
|
|
|
LOGD("Found fstab file: %s\n", fstab_file);
|
|
|
|
goto exit_loop;
|
|
|
|
}
|
2020-12-31 07:11:24 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-14 15:20:12 +01:00
|
|
|
exit_loop:
|
2020-12-31 07:11:24 +01:00
|
|
|
|
2021-01-15 04:48:00 +01:00
|
|
|
// Try to load dt fstab
|
2021-01-14 15:20:12 +01:00
|
|
|
vector<fstab_entry> fstab;
|
2021-01-15 04:48:00 +01:00
|
|
|
read_dt_fstab(fstab);
|
2020-12-31 07:11:24 +01:00
|
|
|
|
2021-01-15 04:48:00 +01:00
|
|
|
if (!fstab.empty()) {
|
|
|
|
// Dump dt fstab to fstab file in rootfs and force init to use it instead
|
2020-12-31 07:11:24 +01:00
|
|
|
|
2021-01-15 04:48:00 +01:00
|
|
|
// 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";
|
|
|
|
}
|
|
|
|
}
|
2020-12-31 07:11:24 +01:00
|
|
|
|
2021-01-15 04:48:00 +01:00
|
|
|
if (fstab_file[0] == '\0') {
|
|
|
|
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);
|
|
|
|
}
|
2020-12-31 07:11:24 +01:00
|
|
|
|
2021-01-15 04:48:00 +01:00
|
|
|
// Patch init to force IsDtFstabCompatible() return false
|
|
|
|
auto init = mmap_data::rw("/init");
|
|
|
|
init.patch({ make_pair("android,fstab", "xxx") });
|
|
|
|
} else {
|
|
|
|
// Parse and load the fstab file
|
|
|
|
file_readline(fstab_file, [&](string_view l) -> bool {
|
|
|
|
if (l[0] == '#' || l.length() == 1)
|
|
|
|
return true;
|
|
|
|
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;
|
2020-12-31 07:11:24 +01:00
|
|
|
|
2021-01-15 04:48:00 +01:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
2020-12-31 07:11:24 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
LOGD("Write fstab file: %s\n", fstab_file);
|
|
|
|
auto fp = xopen_file(fstab_file, "we");
|
|
|
|
for (auto &entry : fstab) {
|
|
|
|
// Redirect system mnt_point so init won't switch root in first stage init
|
|
|
|
if (entry.mnt_point == "/system")
|
|
|
|
entry.mnt_point = "/system_root";
|
|
|
|
|
|
|
|
// Force remove AVB for 2SI since it may bootloop some devices
|
|
|
|
auto len = patch_verity(entry.fsmgr_flags.data(), entry.fsmgr_flags.length());
|
|
|
|
entry.fsmgr_flags.resize(len);
|
|
|
|
|
|
|
|
entry.to_file(fp.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chmod(fstab_file, 0644);
|
|
|
|
|
|
|
|
chdir("/");
|
2020-04-01 13:39:28 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 07:15:12 +02:00
|
|
|
#define INIT_PATH "/system/bin/init"
|
2020-04-20 13:27:25 +02:00
|
|
|
#define REDIR_PATH "/system/bin/am"
|
2020-04-01 13:39:28 +02:00
|
|
|
|
2020-12-04 12:06:21 +01:00
|
|
|
void SARInit::first_stage_prep() {
|
2020-12-31 07:11:24 +01:00
|
|
|
xmount("tmpfs", "/dev", "tmpfs", 0, "mode=755");
|
|
|
|
|
|
|
|
// Patch init binary
|
|
|
|
int src = xopen("/init", O_RDONLY);
|
|
|
|
int dest = xopen("/dev/init", O_CREAT | O_WRONLY, 0);
|
|
|
|
{
|
2021-01-13 07:50:55 +01:00
|
|
|
auto init = mmap_data::ro("/init");
|
2020-12-31 07:11:24 +01:00
|
|
|
init.patch({ make_pair(INIT_PATH, REDIR_PATH) });
|
|
|
|
write(dest, init.buf, init.sz);
|
|
|
|
fclone_attr(src, dest);
|
|
|
|
close(dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace redirect init with magiskinit
|
|
|
|
dest = xopen("/dev/magiskinit", O_CREAT | O_WRONLY, 0);
|
|
|
|
write(dest, self.buf, self.sz);
|
|
|
|
fclone_attr(src, dest);
|
|
|
|
close(src);
|
|
|
|
close(dest);
|
|
|
|
|
|
|
|
xmount("/dev/init", "/init", nullptr, MS_BIND, nullptr);
|
|
|
|
xmount("/dev/magiskinit", REDIR_PATH, nullptr, MS_BIND, nullptr);
|
|
|
|
xumount2("/dev", MNT_DETACH);
|
|
|
|
|
|
|
|
// Block SIGUSR1
|
|
|
|
sigset_t block, old;
|
|
|
|
sigemptyset(&block);
|
|
|
|
sigaddset(&block, SIGUSR1);
|
|
|
|
sigprocmask(SIG_BLOCK, &block, &old);
|
|
|
|
|
2021-01-14 04:41:57 +01:00
|
|
|
if (int child = xfork()) {
|
2020-12-31 07:11:24 +01:00
|
|
|
LOGD("init daemon [%d]\n", child);
|
|
|
|
// Wait for children signal
|
|
|
|
int sig;
|
|
|
|
sigwait(&block, &sig);
|
|
|
|
|
|
|
|
// Restore sigmask
|
|
|
|
sigprocmask(SIG_SETMASK, &old, nullptr);
|
|
|
|
} else {
|
|
|
|
// Establish socket for 2nd stage ack
|
|
|
|
struct sockaddr_un sun;
|
|
|
|
int sockfd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
|
|
|
xbind(sockfd, (struct sockaddr*) &sun, setup_sockaddr(&sun, INIT_SOCKET));
|
|
|
|
xlisten(sockfd, 1);
|
|
|
|
|
|
|
|
// Resume parent
|
2021-01-14 04:41:57 +01:00
|
|
|
kill(getppid(), SIGUSR1);
|
2020-12-31 07:11:24 +01:00
|
|
|
|
|
|
|
// Wait for second stage ack
|
|
|
|
int client = xaccept4(sockfd, nullptr, nullptr, SOCK_CLOEXEC);
|
|
|
|
|
|
|
|
// Write backup files
|
2021-01-12 09:07:48 +01:00
|
|
|
string tmp_dir = read_string(client);
|
|
|
|
chdir(tmp_dir.data());
|
2020-12-31 07:11:24 +01:00
|
|
|
int cfg = xopen(INTLROOT "/config", O_WRONLY | O_CREAT, 0);
|
|
|
|
xwrite(cfg, config.buf, config.sz);
|
|
|
|
close(cfg);
|
|
|
|
restore_folder(ROOTOVL, overlays);
|
|
|
|
|
|
|
|
// Ack and bail out!
|
2021-01-18 13:13:54 +01:00
|
|
|
write_int(client, 0);
|
2020-12-31 07:11:24 +01:00
|
|
|
close(client);
|
|
|
|
close(sockfd);
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
2020-04-01 13:39:28 +02:00
|
|
|
}
|