From 519bd2f30f5d74fe08d655cab0b5b9b5db3a42ab Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 19 Jan 2022 20:28:01 -0800 Subject: [PATCH] Disable AVD hacks by default --- native/jni/init/init.hpp | 10 ++++++++++ native/jni/init/mount.cpp | 16 ++++++++++------ scripts/avd_magisk.sh | 2 +- scripts/avd_patch.sh | 6 ++++++ 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/native/jni/init/init.hpp b/native/jni/init/init.hpp index f3217890b..46927fee3 100644 --- a/native/jni/init/init.hpp +++ b/native/jni/init/init.hpp @@ -2,6 +2,11 @@ using kv_pairs = std::vector>; +// For API 28 AVD, it uses legacy SAR setup that requires +// special hacks in magiskinit to work properly. We do not +// necessarily want this enabled in production builds. +#define ENABLE_AVD_HACK 0 + struct BootConfig { bool skip_initramfs; bool force_normal_boot; @@ -67,9 +72,14 @@ protected: mmap_data magisk_config; std::string custom_rules_dir; +#if ENABLE_AVD_HACK // When this boolean is set, this means we are currently // running magiskinit on legacy SAR AVD emulator bool avd_hack = false; +#else + // Make it const so compiler can optimize hacks out of the code + static const bool avd_hack = false; +#endif void mount_with_dt(); bool patch_sepolicy(const char *file); diff --git a/native/jni/init/mount.cpp b/native/jni/init/mount.cpp index 06410a318..779221d35 100644 --- a/native/jni/init/mount.cpp +++ b/native/jni/init/mount.cpp @@ -158,19 +158,21 @@ void MagiskInit::mount_with_dt() { for (const auto &entry : fstab) { if (is_lnk(entry.mnt_point.data())) continue; - // When we force AVD to disable SystemAsRoot, it will always add system - // to dt fstab, which we actually have already mounted as root - if (avd_hack && entry.mnt_point == "/system") + if (avd_hack && 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); - // When avd_hack is true, do not add any early mount partitions to mount_list - // as we will actually forcefully disable original init's early mount - if (!avd_hack) + if (!avd_hack) { + // When avd_hack is true, do not add any early mount partitions to mount_list + // as we will actually forcefully disable original init's early mount mount_list.push_back(entry.mnt_point); + } } } @@ -379,7 +381,9 @@ void SARInit::early_mount() { xmkdir("/dev", 0755); xmount("tmpfs", "/dev", "tmpfs", 0, "mode=755"); mount_list.emplace_back("/dev"); +#if ENABLE_AVD_HACK avd_hack = config->emulator; +#endif mount_with_dt(); } } diff --git a/scripts/avd_magisk.sh b/scripts/avd_magisk.sh index 2c5baaa31..a8b388ea7 100755 --- a/scripts/avd_magisk.sh +++ b/scripts/avd_magisk.sh @@ -16,7 +16,7 @@ # the official Android emulator (AVD) instead of a real device. # # This only covers the "core" features of Magisk. For testing -# magiskinit, please checkout avd_init.sh. +# magiskinit, please checkout avd_patch.sh. # ##################################################################### diff --git a/scripts/avd_patch.sh b/scripts/avd_patch.sh index f73bc1855..dcb00a973 100644 --- a/scripts/avd_patch.sh +++ b/scripts/avd_patch.sh @@ -14,6 +14,10 @@ # After patching ramdisk.img, close the emulator, then select # "Cold Boot Now" in AVD Manager to force a full reboot. # +# P.S. If running against the API 28 image, modify init.hpp and set +# ENABLE_AVD_HACK to 1 to enable special hacks designed specifically +# for this use case. +# ##################################################################### # AVD Init Configurations: # @@ -39,6 +43,8 @@ if [ -z "$FIRST_STAGE" ]; then exec ./busybox sh $0 fi +pm install -r $(pwd)/app-debug.apk + # Extract files from APK unzip -oj app-debug.apk 'assets/util_functions.sh' . ./util_functions.sh