Remove unused code

This commit is contained in:
topjohnwu 2022-03-16 21:31:22 -07:00
parent e841aab9e7
commit e266a81167
3 changed files with 25 additions and 120 deletions

View File

@ -5,7 +5,7 @@ using kv_pairs = std::vector<std::pair<std::string, std::string>>;
// For API 28 AVD, it uses legacy SAR setup that requires // For API 28 AVD, it uses legacy SAR setup that requires
// special hacks in magiskinit to work properly. We do not // special hacks in magiskinit to work properly. We do not
// necessarily want this enabled in production builds. // necessarily want this enabled in production builds.
#define ENABLE_AVD_HACK 1 #define ENABLE_AVD_HACK 0
struct BootConfig { struct BootConfig {
bool skip_initramfs; bool skip_initramfs;
@ -22,20 +22,6 @@ struct BootConfig {
void print(); void print();
}; };
struct fstab_entry {
std::string dev;
std::string mnt_point;
std::string type;
std::string mnt_flags;
std::string fsmgr_flags;
fstab_entry() = default;
fstab_entry(const fstab_entry &) = delete;
fstab_entry(fstab_entry &&) = default;
fstab_entry &operator=(const fstab_entry&) = delete;
fstab_entry &operator=(fstab_entry&&) = default;
};
#define DEFAULT_DT_DIR "/proc/device-tree/firmware/android" #define DEFAULT_DT_DIR "/proc/device-tree/firmware/android"
extern std::vector<std::string> mount_list; extern std::vector<std::string> mount_list;
@ -64,6 +50,8 @@ public:
}; };
class MagiskInit : public BaseInit { class MagiskInit : public BaseInit {
private:
void mount_rules_dir();
protected: protected:
mmap_data self; mmap_data self;
mmap_data magisk_cfg; mmap_data magisk_cfg;
@ -81,7 +69,6 @@ protected:
void patch_sepolicy(const char *file); void patch_sepolicy(const char *file);
void hijack_sepolicy(); void hijack_sepolicy();
void setup_tmp(const char *path); void setup_tmp(const char *path);
void mount_rules_dir(const char *dev_base, const char *mnt_base);
void patch_rw_root(); void patch_rw_root();
public: public:
using BaseInit::BaseInit; using BaseInit::BaseInit;

View File

@ -60,11 +60,9 @@ static struct {
char block_dev[64]; char block_dev[64];
} blk_info; } blk_info;
static int64_t setup_block(bool write_block) { static int64_t setup_block() {
if (dev_list.empty()) if (dev_list.empty())
collect_devices(); collect_devices();
xmkdir("/dev", 0755);
xmkdir("/dev/block", 0755);
for (int tries = 0; tries < 3; ++tries) { for (int tries = 0; tries < 3; ++tries) {
for (auto &dev : dev_list) { for (auto &dev : dev_list) {
@ -75,9 +73,6 @@ static int64_t setup_block(bool write_block) {
else else
continue; continue;
if (write_block) {
sprintf(blk_info.block_dev, "/dev/block/%s", dev.devname);
}
dev_t rdev = makedev(dev.major, dev.minor); dev_t rdev = makedev(dev.major, dev.minor);
xmknod(blk_info.block_dev, S_IFBLK | 0600, rdev); xmknod(blk_info.block_dev, S_IFBLK | 0600, rdev);
return rdev; return rdev;
@ -92,87 +87,6 @@ static int64_t setup_block(bool write_block) {
return -1; return -1;
} }
static bool is_lnk(const char *name) {
struct stat st{};
if (lstat(name, &st))
return false;
return S_ISLNK(st.st_mode);
}
#define read_info(val) \
if (access(#val, F_OK) == 0) {\
entry.val = rtrim(full_read(#val)); \
}
static void read_dt_fstab(BootConfig *config, vector<fstab_entry> &fstab) {
if (access(config->dt_dir, F_OK) != 0)
return;
char cwd[128];
getcwd(cwd, sizeof(cwd));
chdir(config->dt_dir);
run_finally cd([&]{ chdir(cwd); });
if (access("fstab", F_OK) != 0)
return;
chdir("fstab");
// Make sure dt fstab is enabled
if (access("status", F_OK) == 0) {
auto status = rtrim(full_read("status"));
if (status != "okay" && status != "ok")
return;
}
auto dir = xopen_dir(".");
for (dirent *dp; (dp = xreaddir(dir.get()));) {
if (dp->d_type != DT_DIR)
continue;
chdir(dp->d_name);
run_finally f([]{ chdir(".."); });
if (access("status", F_OK) == 0) {
auto status = rtrim(full_read("status"));
if (status != "okay" && status != "ok")
continue;
}
fstab_entry entry;
read_info(dev);
read_info(mnt_point) else {
entry.mnt_point = "/";
entry.mnt_point += dp->d_name;
}
read_info(type);
read_info(mnt_flags);
read_info(fsmgr_flags);
fstab.emplace_back(std::move(entry));
}
}
static void avd_hack_mount(BootConfig *config) {
vector<fstab_entry> fstab;
read_dt_fstab(config, fstab);
for (const auto &entry : fstab) {
if (is_lnk(entry.mnt_point.data()))
continue;
if (entry.mnt_point == "/system") {
// When we force AVD to disable SystemAsRoot, it will always add system
// to dt fstab. We actually already mounted it as root, so skip this one.
continue;
}
// Derive partname from dev
sprintf(blk_info.partname, "%s%s", basename(entry.dev.data()), config->slot);
setup_block(true);
xmkdir(entry.mnt_point.data(), 0755);
xmount(blk_info.block_dev, entry.mnt_point.data(), entry.type.data(), MS_RDONLY, nullptr);
// Do not add any early mount partitions to mount_list as we will
// actually forcefully disable original init's early mount.
}
}
static void switch_root(const string &path) { static void switch_root(const string &path) {
LOGD("Switch root to %s\n", path.data()); LOGD("Switch root to %s\n", path.data());
int root = xopen("/", O_RDONLY); int root = xopen("/", O_RDONLY);
@ -202,10 +116,10 @@ static void switch_root(const string &path) {
frm_rf(root); frm_rf(root);
} }
void MagiskInit::mount_rules_dir(const char *dev_base, const char *mnt_base) { void MagiskInit::mount_rules_dir() {
char path[128]; char path[128];
xrealpath(dev_base, blk_info.block_dev); xrealpath(BLOCKDIR, blk_info.block_dev);
xrealpath(mnt_base, path); xrealpath(MIRRDIR, path);
char *b = blk_info.block_dev + strlen(blk_info.block_dev); char *b = blk_info.block_dev + strlen(blk_info.block_dev);
char *p = path + strlen(path); char *p = path + strlen(path);
@ -221,10 +135,10 @@ void MagiskInit::mount_rules_dir(const char *dev_base, const char *mnt_base) {
strcpy(blk_info.partname, "userdata"); strcpy(blk_info.partname, "userdata");
strcpy(b, "/data"); strcpy(b, "/data");
strcpy(p, "/data"); strcpy(p, "/data");
if (setup_block(false) < 0) { if (setup_block() < 0) {
// Try NVIDIA naming scheme // Try NVIDIA naming scheme
strcpy(blk_info.partname, "UDA"); strcpy(blk_info.partname, "UDA");
if (setup_block(false) < 0) if (setup_block() < 0)
goto cache; goto cache;
} }
// WARNING: DO NOT ATTEMPT TO MOUNT F2FS AS IT MAY CRASH THE KERNEL // WARNING: DO NOT ATTEMPT TO MOUNT F2FS AS IT MAY CRASH THE KERNEL
@ -255,10 +169,10 @@ cache:
strcpy(blk_info.partname, "cache"); strcpy(blk_info.partname, "cache");
strcpy(b, "/cache"); strcpy(b, "/cache");
strcpy(p, "/cache"); strcpy(p, "/cache");
if (setup_block(false) < 0) { if (setup_block() < 0) {
// Try NVIDIA naming scheme // Try NVIDIA naming scheme
strcpy(blk_info.partname, "CAC"); strcpy(blk_info.partname, "CAC");
if (setup_block(false) < 0) if (setup_block() < 0)
goto metadata; goto metadata;
} }
if (!do_mount("ext4")) if (!do_mount("ext4"))
@ -271,7 +185,7 @@ metadata:
strcpy(blk_info.partname, "metadata"); strcpy(blk_info.partname, "metadata");
strcpy(b, "/metadata"); strcpy(b, "/metadata");
strcpy(p, "/metadata"); strcpy(p, "/metadata");
if (setup_block(false) < 0 || !do_mount("ext4")) if (setup_block() < 0 || !do_mount("ext4"))
goto persist; goto persist;
custom_rules_dir = path + "/magisk"s; custom_rules_dir = path + "/magisk"s;
goto success; goto success;
@ -281,7 +195,7 @@ persist:
strcpy(blk_info.partname, "persist"); strcpy(blk_info.partname, "persist");
strcpy(b, "/persist"); strcpy(b, "/persist");
strcpy(p, "/persist"); strcpy(p, "/persist");
if (setup_block(false) < 0 || !do_mount("ext4")) if (setup_block() < 0 || !do_mount("ext4"))
return; return;
custom_rules_dir = path + "/magisk"s; custom_rules_dir = path + "/magisk"s;
@ -321,18 +235,18 @@ bool LegacySARInit::mount_system_root() {
do { do {
// Try legacy SAR dm-verity // Try legacy SAR dm-verity
strcpy(blk_info.partname, "vroot"); strcpy(blk_info.partname, "vroot");
auto dev = setup_block(false); auto dev = setup_block();
if (dev >= 0) if (dev >= 0)
goto mount_root; goto mount_root;
// Try NVIDIA naming scheme // Try NVIDIA naming scheme
strcpy(blk_info.partname, "APP"); strcpy(blk_info.partname, "APP");
dev = setup_block(false); dev = setup_block();
if (dev >= 0) if (dev >= 0)
goto mount_root; goto mount_root;
sprintf(blk_info.partname, "system%s", config->slot); sprintf(blk_info.partname, "system%s", config->slot);
dev = setup_block(false); dev = setup_block();
if (dev >= 0) if (dev >= 0)
goto mount_root; goto mount_root;
@ -368,7 +282,13 @@ mount_root:
xmkdir("/dev", 0755); xmkdir("/dev", 0755);
xmount("tmpfs", "/dev", "tmpfs", 0, "mode=755"); xmount("tmpfs", "/dev", "tmpfs", 0, "mode=755");
mount_list.emplace_back("/dev"); mount_list.emplace_back("/dev");
avd_hack_mount(config);
// These values are hardcoded for API 28 AVD
xmkdir("/dev/block", 0755);
strcpy(blk_info.block_dev, "/dev/block/vde1");
strcpy(blk_info.partname, "vendor");
setup_block();
xmount(blk_info.block_dev, "/vendor", "ext4", MS_RDONLY, nullptr);
} }
} }
#endif #endif
@ -396,6 +316,8 @@ void MagiskInit::setup_tmp(const char *path) {
xmkdir(MIRRDIR, 0); xmkdir(MIRRDIR, 0);
xmkdir(BLOCKDIR, 0); xmkdir(BLOCKDIR, 0);
mount_rules_dir();
int fd = xopen(INTLROOT "/config", O_WRONLY | O_CREAT, 0); int fd = xopen(INTLROOT "/config", O_WRONLY | O_CREAT, 0);
xwrite(fd, magisk_cfg.buf, magisk_cfg.sz); xwrite(fd, magisk_cfg.buf, magisk_cfg.sz);
close(fd); close(fd);

View File

@ -306,8 +306,6 @@ void SARBase::patch_ro_root() {
setup_tmp(tmp_dir.data()); setup_tmp(tmp_dir.data());
chdir(tmp_dir.data()); chdir(tmp_dir.data());
mount_rules_dir(BLOCKDIR, MIRRDIR);
// Mount system_root mirror // Mount system_root mirror
xmkdir(ROOTMIR, 0755); xmkdir(ROOTMIR, 0755);
xmount("/", ROOTMIR, nullptr, MS_BIND, nullptr); xmount("/", ROOTMIR, nullptr, MS_BIND, nullptr);
@ -406,8 +404,6 @@ void MagiskInit::patch_rw_root() {
setup_tmp(PRE_TMPDIR); setup_tmp(PRE_TMPDIR);
chdir(PRE_TMPDIR); chdir(PRE_TMPDIR);
mount_rules_dir(BLOCKDIR, MIRRDIR);
{ {
// Extract magisk // Extract magisk
auto magisk = mmap_data("/sbin/magisk32.xz"); auto magisk = mmap_data("/sbin/magisk32.xz");