From fb5ee86615ed3df830e8538f8b39b1b133caea34 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 31 Mar 2024 22:01:22 -0700 Subject: [PATCH] Install one single ABI in ramdisk --- .../magisk/core/tasks/MagiskInstaller.kt | 46 ++++++++++--------- buildSrc/src/main/java/Setup.kt | 8 ++-- native/src/core/bootstages.cpp | 6 +++ native/src/core/module.cpp | 11 +++-- native/src/core/zygisk/entry.cpp | 6 ++- native/src/init/rootdir.cpp | 22 ++------- scripts/avd_magisk.sh | 18 ++++---- scripts/avd_patch.sh | 22 ++------- scripts/boot_patch.sh | 20 ++------ scripts/flash_script.sh | 2 +- 10 files changed, 72 insertions(+), 89 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt b/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt index 1669a4b19..a13763301 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt @@ -1,6 +1,7 @@ package com.topjohnwu.magisk.core.tasks import android.net.Uri +import android.os.Process import android.system.ErrnoException import android.system.Os import android.system.OsConstants @@ -105,40 +106,41 @@ abstract class MagiskInstallImpl protected constructor( try { // Extract binaries if (isRunningAsStub) { - val zf = ZipFile(StubApk.current(context)) + ZipFile(StubApk.current(context)).use { zf -> + zf.entries().asSequence().filter { + !it.isDirectory && it.name.startsWith("/lib/${Const.CPU_ABI}/") + }.forEach { + val n = it.name.substring(it.name.lastIndexOf('/') + 1) + val name = n.substring(3, n.length - 3) + val dest = File(installDir, name) + zf.getInputStream(it).writeTo(dest) + dest.setExecutable(true) + } - // Also extract magisk32 on non 64-bit only 64-bit devices - val is32lib = Const.CPU_ABI_32?.let { - { entry: ZipEntry -> entry.name == "lib/$it/libmagisk32.so" } - } ?: { false } - - zf.entries().asSequence().filter { - !it.isDirectory && (it.name.startsWith("lib/${Const.CPU_ABI}/") || is32lib(it)) - }.forEach { - val n = it.name.substring(it.name.lastIndexOf('/') + 1) - val name = n.substring(3, n.length - 3) - val dest = File(installDir, name) - zf.getInputStream(it).writeTo(dest) - dest.setExecutable(true) + val abi32 = Const.CPU_ABI_32 + if (Process.is64Bit() && abi32 != null) { + val magisk32 = File(installDir, "magisk32") + zf.getInputStream(ZipEntry("lib/$abi32/libmagisk.so")).writeTo(magisk32) + magisk32.setExecutable(true) + } } - zf.close() } else { val info = context.applicationInfo var libs = File(info.nativeLibraryDir).listFiles { _, name -> name.startsWith("lib") && name.endsWith(".so") } ?: emptyArray() - // Also symlink magisk32 on non 64-bit only 64-bit devices - val lib32 = info.javaClass.getDeclaredField("secondaryNativeLibraryDir") - .get(info) as String? - if (lib32 != null) { - libs += File(lib32, "libmagisk32.so") - } - for (lib in libs) { val name = lib.name.substring(3, lib.name.length - 3) Os.symlink(lib.path, "$installDir/$name") } + + // Also symlink magisk32 on 64-bit devices that supports 32-bit + val lib32 = info.javaClass.getDeclaredField("secondaryNativeLibraryDir") + .get(info) as String? + if (lib32 != null) { + Os.symlink("$lib32/libmagisk.so", "$installDir/magisk32"); + } } // Extract scripts diff --git a/buildSrc/src/main/java/Setup.kt b/buildSrc/src/main/java/Setup.kt index bd5f133b8..81f4e02ef 100644 --- a/buildSrc/src/main/java/Setup.kt +++ b/buildSrc/src/main/java/Setup.kt @@ -231,25 +231,25 @@ fun Project.setupApp() { into("armeabi-v7a") { from(rootProject.file("native/out/armeabi-v7a")) { include("busybox", "magiskboot", "magiskinit", "magiskpolicy", "magisk") - rename { if (it == "magisk") "libmagisk32.so" else "lib$it.so" } + rename { "lib$it.so" } } } into("x86") { from(rootProject.file("native/out/x86")) { include("busybox", "magiskboot", "magiskinit", "magiskpolicy", "magisk") - rename { if (it == "magisk") "libmagisk32.so" else "lib$it.so" } + rename { "lib$it.so" } } } into("arm64-v8a") { from(rootProject.file("native/out/arm64-v8a")) { include("busybox", "magiskboot", "magiskinit", "magiskpolicy", "magisk") - rename { if (it == "magisk") "libmagisk64.so" else "lib$it.so" } + rename { "lib$it.so" } } } into("x86_64") { from(rootProject.file("native/out/x86_64")) { include("busybox", "magiskboot", "magiskinit", "magiskpolicy", "magisk") - rename { if (it == "magisk") "libmagisk64.so" else "lib$it.so" } + rename { "lib$it.so" } } } onlyIf { diff --git a/native/src/core/bootstages.cpp b/native/src/core/bootstages.cpp index 84023f7d9..ba77f912e 100644 --- a/native/src/core/bootstages.cpp +++ b/native/src/core/bootstages.cpp @@ -41,6 +41,12 @@ static bool magisk_env() { cp_afc(DATABIN "/busybox", buf); exec_command_async(buf, "--install", "-s", dirname(buf)); + // magisk32 and magiskpolicy are not installed into ramdisk and has to be copied + // from data to magisk tmp + if (access(DATABIN "/magisk32", X_OK) == 0) { + ssprintf(buf, sizeof(buf), "%s/magisk32", get_magisk_tmp()); + cp_afc(DATABIN "/magisk32", buf); + } if (access(DATABIN "/magiskpolicy", X_OK) == 0) { ssprintf(buf, sizeof(buf), "%s/magiskpolicy", get_magisk_tmp()); cp_afc(DATABIN "/magiskpolicy", buf); diff --git a/native/src/core/module.cpp b/native/src/core/module.cpp index 6b3b750dc..2f5113866 100644 --- a/native/src/core/module.cpp +++ b/native/src/core/module.cpp @@ -203,11 +203,16 @@ public: class zygisk_node : public node_entry { public: - explicit zygisk_node(const char *name, bool is64bit) : node_entry(name, DT_REG, this), - is64bit(is64bit) {} + explicit zygisk_node(const char *name, bool is64bit) + : node_entry(name, DT_REG, this), is64bit(is64bit) {} void mount() override { - const string src = get_magisk_tmp() + "/magisk"s + (is64bit ? "64" : "32"); +#if defined(__LP64__) + const string src = get_magisk_tmp() + "/magisk"s + (is64bit ? "" : "32"); +#else + const string src = get_magisk_tmp() + "/magisk"s; + (void) is64bit; +#endif create_and_mount("zygisk", src, true); } diff --git a/native/src/core/zygisk/entry.cpp b/native/src/core/zygisk/entry.cpp index 838213540..24ec964eb 100644 --- a/native/src/core/zygisk/entry.cpp +++ b/native/src/core/zygisk/entry.cpp @@ -97,7 +97,11 @@ static void connect_companion(int client, bool is_64_bit) { zygiskd_socket = fds[0]; if (fork_dont_care() == 0) { char exe[64]; - ssprintf(exe, sizeof(exe), "%s/magisk%s", get_magisk_tmp(), (is_64_bit ? "64" : "32")); +#if defined(__LP64__) + ssprintf(exe, sizeof(exe), "%s/magisk%s", get_magisk_tmp(), (is_64_bit ? "" : "32")); +#else + ssprintf(exe, sizeof(exe), "%s/magisk", get_magisk_tmp()); +#endif // This fd has to survive exec fcntl(fds[1], F_SETFD, 0); char buf[16]; diff --git a/native/src/init/rootdir.cpp b/native/src/init/rootdir.cpp index 58f3f8f25..522222e68 100644 --- a/native/src/init/rootdir.cpp +++ b/native/src/init/rootdir.cpp @@ -187,29 +187,17 @@ static void magic_mount(const string &sdir, const string &ddir = "") { } static void extract_files(bool sbin) { - const char *m32 = sbin ? "/sbin/magisk32.xz" : "magisk32.xz"; - const char *m64 = sbin ? "/sbin/magisk64.xz" : "magisk64.xz"; + const char *magisk_xz = sbin ? "/sbin/magisk.xz" : "magisk.xz"; const char *stub_xz = sbin ? "/sbin/stub.xz" : "stub.xz"; - if (access(m32, F_OK) == 0) { - mmap_data magisk(m32); - unlink(m32); - int fd = xopen("magisk32", O_WRONLY | O_CREAT, 0755); + if (access(magisk_xz, F_OK) == 0) { + mmap_data magisk(magisk_xz); + unlink(magisk_xz); + int fd = xopen("magisk", O_WRONLY | O_CREAT, 0755); fd_stream ch(fd); unxz(ch, magisk); close(fd); } - if (access(m64, F_OK) == 0) { - mmap_data magisk(m64); - unlink(m64); - int fd = xopen("magisk64", O_WRONLY | O_CREAT, 0755); - fd_stream ch(fd); - unxz(ch, magisk); - close(fd); - xsymlink("./magisk64", "magisk"); - } else { - xsymlink("./magisk32", "magisk"); - } if (access(stub_xz, F_OK) == 0) { mmap_data stub(stub_xz); unlink(stub_xz); diff --git a/scripts/avd_magisk.sh b/scripts/avd_magisk.sh index 1746a3493..422a03eb7 100755 --- a/scripts/avd_magisk.sh +++ b/scripts/avd_magisk.sh @@ -52,12 +52,18 @@ unzip -oj magisk.apk 'assets/util_functions.sh' 'assets/stub.apk' api_level_arch_detect -unzip -oj magisk.apk "lib/$ABI/*" "lib/$ABI32/libmagisk32.so" -x "lib/$ABI/libbusybox.so" +unzip -oj magisk.apk "lib/$ABI/*" -x "lib/$ABI/libbusybox.so" for file in lib*.so; do chmod 755 $file mv "$file" "${file:3:${#file}-6}" done +if $IS64BIT; then + unzip -oj magisk.apk "lib/$ABI32/libmagisk.so" + mv libmagisk.so magisk32 + chmod 755 magisk32 +fi + # Stop zygote (and previous setup if exists) magisk --stop 2>/dev/null stop @@ -109,8 +115,9 @@ else # Android Q+ without sbin MAGISKTMP=/debug_ramdisk # If a file name 'magisk' is in current directory, mount will fail - rm -f magisk + mv magisk magisk.tmp mount -t tmpfs -o 'mode=0755' magisk /debug_ramdisk + mv magisk.tmp magisk fi # Magisk stuff @@ -120,7 +127,7 @@ mkdir $NVBASE/modules 2>/dev/null mkdir $NVBASE/post-fs-data.d 2>/dev/null mkdir $NVBASE/service.d 2>/dev/null -for file in magisk32 magisk64 magiskpolicy stub.apk; do +for file in magisk magisk32 magiskpolicy stub.apk; do chmod 755 ./$file cp -af ./$file $MAGISKTMP/$file cp -af ./$file $MAGISKBIN/$file @@ -129,11 +136,6 @@ cp -af ./magiskboot $MAGISKBIN/magiskboot cp -af ./magiskinit $MAGISKBIN/magiskinit cp -af ./busybox $MAGISKBIN/busybox -if $IS64BIT; then - ln -s ./magisk64 $MAGISKTMP/magisk -else - ln -s ./magisk32 $MAGISKTMP/magisk -fi ln -s ./magisk $MAGISKTMP/su ln -s ./magisk $MAGISKTMP/resetprop ln -s ./magiskpolicy $MAGISKTMP/supolicy diff --git a/scripts/avd_patch.sh b/scripts/avd_patch.sh index 887f5fc9c..de8bfc653 100644 --- a/scripts/avd_patch.sh +++ b/scripts/avd_patch.sh @@ -43,7 +43,7 @@ unzip -oj magisk.apk 'assets/util_functions.sh' 'assets/stub.apk' api_level_arch_detect -unzip -oj magisk.apk "lib/$ABI/*" "lib/$ABI32/libmagisk32.so" -x "lib/$ABI/libbusybox.so" +unzip -oj magisk.apk "lib/$ABI/*" -x "lib/$ABI/libbusybox.so" for file in lib*.so; do chmod 755 $file mv "$file" "${file:3:${#file}-6}" @@ -57,34 +57,20 @@ export KEEPFORCEENCRYPT=true echo "KEEPVERITY=$KEEPVERITY" > config echo "KEEPFORCEENCRYPT=$KEEPFORCEENCRYPT" >> config -if [ -f magisk64 ]; then - echo "PREINITDEVICE=$(./magisk64 --preinit-device)" >> config -else - echo "PREINITDEVICE=$(./magisk32 --preinit-device)" >> config -fi +echo "PREINITDEVICE=$(./magisk --preinit-device)" >> config # For API 28, we also patch advancedFeatures.ini to disable SAR # Manually override skip_initramfs by setting RECOVERYMODE=true [ $API = "28" ] && echo 'RECOVERYMODE=true' >> config cat config -SKIP32="#" -SKIP64="#" -if [ -f magisk64 ]; then - ./magiskboot compress=xz magisk64 magisk64.xz - unset SKIP64 -fi -if [ -e "/system/bin/linker" ]; then - ./magiskboot compress=xz magisk32 magisk32.xz - unset SKIP32 -fi +./magiskboot compress=xz magisk magisk.xz ./magiskboot compress=xz stub.apk stub.xz ./magiskboot cpio ramdisk.cpio \ "add 0750 init magiskinit" \ "mkdir 0750 overlay.d" \ "mkdir 0750 overlay.d/sbin" \ -"$SKIP32 add 0644 overlay.d/sbin/magisk32.xz magisk32.xz" \ -"$SKIP64 add 0644 overlay.d/sbin/magisk64.xz magisk64.xz" \ +"add 0644 overlay.d/sbin/magisk.xz magisk.xz" \ "add 0644 overlay.d/sbin/stub.xz stub.xz" \ "patch" \ "backup ramdisk.cpio.orig" \ diff --git a/scripts/boot_patch.sh b/scripts/boot_patch.sh index b7a081aa7..e7c2c2822 100644 --- a/scripts/boot_patch.sh +++ b/scripts/boot_patch.sh @@ -18,7 +18,7 @@ # util_functions.sh script A script which hosts all functions required # for this script to work properly. # magiskinit binary The binary to replace /init. -# magisk(32/64) binary The magisk binaries. +# magisk binary The magisk binary. # magiskboot binary A tool to manipulate boot images. # stub.apk binary The stub Magisk app to embed into ramdisk. # chromeos folder This folder includes the utility and keys to sign @@ -156,19 +156,10 @@ fi ui_print "- Patching ramdisk" +$BOOTMODE && [ -z "$PREINITDEVICE" ] && PREINITDEVICE=$(./magisk --preinit-device) + # Compress to save precious ramdisk space -SKIP32="#" -SKIP64="#" -if [ -f magisk64 ]; then - $BOOTMODE && [ -z "$PREINITDEVICE" ] && PREINITDEVICE=$(./magisk64 --preinit-device) - ./magiskboot compress=xz magisk64 magisk64.xz - unset SKIP64 -fi -if [ -f magisk32 ]; then - $BOOTMODE && [ -z "$PREINITDEVICE" ] && PREINITDEVICE=$(./magisk32 --preinit-device) - ./magiskboot compress=xz magisk32 magisk32.xz - unset SKIP32 -fi +./magiskboot compress=xz magisk magisk.xz ./magiskboot compress=xz stub.apk stub.xz echo "KEEPVERITY=$KEEPVERITY" > config @@ -184,8 +175,7 @@ fi "add 0750 init magiskinit" \ "mkdir 0750 overlay.d" \ "mkdir 0750 overlay.d/sbin" \ -"$SKIP32 add 0644 overlay.d/sbin/magisk32.xz magisk32.xz" \ -"$SKIP64 add 0644 overlay.d/sbin/magisk64.xz magisk64.xz" \ +"add 0644 overlay.d/sbin/magisk.xz magisk.xz" \ "add 0644 overlay.d/sbin/stub.xz stub.xz" \ "patch" \ "$SKIP_BACKUP backup ramdisk.cpio.orig" \ diff --git a/scripts/flash_script.sh b/scripts/flash_script.sh index 77a074c5f..149bc784a 100644 --- a/scripts/flash_script.sh +++ b/scripts/flash_script.sh @@ -55,7 +55,7 @@ BINDIR=$INSTALLER/lib/$ABI cd $BINDIR for file in lib*.so; do mv "$file" "${file:3:${#file}-6}"; done cd / -cp -af $INSTALLER/lib/$ABI32/libmagisk32.so $BINDIR/magisk32 2>/dev/null +cp -af $INSTALLER/lib/$ABI32/libmagisk.so $BINDIR/magisk32 2>/dev/null # Check if system root is installed and remove $BOOTMODE || remove_system_su