From ad0e6511e11ebec65aa9b5b916e1397342850319 Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Mon, 31 Oct 2022 22:31:15 +0800 Subject: [PATCH] Stop embedding stub.apk in magiskinit --- .../magisk/core/tasks/MagiskInstaller.kt | 2 +- build.py | 15 ---- buildSrc/src/main/java/Setup.kt | 82 +++++++++---------- native/src/base/include/embed.hpp | 2 - native/src/init/init.cpp | 10 --- native/src/init/init.hpp | 1 - native/src/init/rootdir.cpp | 9 +- scripts/avd_magisk.sh | 3 +- scripts/avd_patch.sh | 6 +- scripts/boot_patch.sh | 4 +- 10 files changed, 55 insertions(+), 79 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 7a1c4d151..60a6b5123 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 @@ -131,7 +131,7 @@ abstract class MagiskInstallImpl protected constructor( } // Extract scripts - for (script in listOf("util_functions.sh", "boot_patch.sh", "addon.d.sh")) { + for (script in listOf("util_functions.sh", "boot_patch.sh", "addon.d.sh", "stub.apk")) { val dest = File(installDir, script) context.assets.open(script).writeTo(dest) } diff --git a/build.py b/build.py index 7c28053b6..5c3fed2b8 100755 --- a/build.py +++ b/build.py @@ -309,13 +309,7 @@ def binary_dump(src, var_name, compressor=xz): def dump_bin_header(args): - stub = op.join(config['outdir'], f'stub-{"release" if args.release else "debug"}.apk') - if not op.exists(stub): - error('Build stub APK before building "magiskinit"') mkdir_p(native_gen_path) - with open(stub, 'rb') as src: - text = binary_dump(src, 'manager_xz') - write_if_diff(op.join(native_gen_path, 'binaries.h'), text) for arch in archs: preload = op.join('native', 'out', arch, 'libinit-ld.so') with open(preload, 'rb') as src: @@ -432,11 +426,6 @@ def build_app(args): build_apk(args, 'app') -def build_stub(args): - header('* Building the stub app') - build_apk(args, 'stub') - - def cleanup(args): support_targets = {'native', 'java'} if args.target: @@ -554,7 +543,6 @@ def patch_avd_ramdisk(args): def build_all(args): - build_stub(args) build_binary(args) build_app(args) @@ -582,9 +570,6 @@ binary_parser.set_defaults(func=build_binary) app_parser = subparsers.add_parser('app', help='build the Magisk app') app_parser.set_defaults(func=build_app) -stub_parser = subparsers.add_parser('stub', help='build the stub app') -stub_parser.set_defaults(func=build_stub) - avd_parser = subparsers.add_parser( 'emulator', help='setup AVD for development') avd_parser.add_argument('-s', '--skip', action='store_true', diff --git a/buildSrc/src/main/java/Setup.kt b/buildSrc/src/main/java/Setup.kt index 788344329..596a80741 100644 --- a/buildSrc/src/main/java/Setup.kt +++ b/buildSrc/src/main/java/Setup.kt @@ -15,8 +15,7 @@ import org.gradle.api.tasks.Copy import org.gradle.api.tasks.Delete import org.gradle.api.tasks.StopExecutionException import org.gradle.api.tasks.Sync -import org.gradle.kotlin.dsl.filter -import org.gradle.kotlin.dsl.named +import org.gradle.kotlin.dsl.* import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream @@ -40,7 +39,7 @@ private fun BaseExtension.kotlinOptions(configure: Action) = } private val Project.android: BaseAppModuleExtension - get() = extensions.getByName("android") as BaseAppModuleExtension + get() = extensions["android"] as BaseAppModuleExtension fun Project.setupCommon() { androidBase { @@ -112,14 +111,14 @@ private fun Project.setupAppCommon() { } buildTypes { - signingConfigs.getByName("config").also { - getByName("debug") { + signingConfigs["config"].also { + debug { signingConfig = if (it.storeFile?.exists() == true) it - else signingConfigs.getByName("debug") + else signingConfigs["debug"] } - getByName("release") { + release { signingConfig = if (it.storeFile?.exists() == true) it - else signingConfigs.getByName("debug") + else signingConfigs["debug"] } } } @@ -147,7 +146,7 @@ private fun Project.setupAppCommon() { fun Project.setupApp() { setupAppCommon() - val syncLibs = tasks.register("syncLibs", Sync::class.java) { + val syncLibs by tasks.registering(Sync::class) { into("src/main/jniLibs") into("armeabi-v7a") { from(rootProject.file("native/out/armeabi-v7a")) { @@ -180,35 +179,8 @@ fun Project.setupApp() { } } - val syncAssets = tasks.register("syncAssets", Sync::class.java) { + val syncResources by tasks.registering(Sync::class) { dependsOn(syncLibs) - inputs.property("version", Config.version) - inputs.property("versionCode", Config.versionCode) - into("src/main/assets") - from(rootProject.file("scripts")) { - include("util_functions.sh", "boot_patch.sh", "addon.d.sh") - include("uninstaller.sh", "module_installer.sh") - } - from(rootProject.file("tools/bootctl")) - into("chromeos") { - from(rootProject.file("tools/futility")) - from(rootProject.file("tools/keys")) { - include("kernel_data_key.vbprivk", "kernel.keyblock") - } - } - filesMatching("**/util_functions.sh") { - filter { - it.replace( - "#MAGISK_VERSION_STUB", - "MAGISK_VER='${Config.version}'\nMAGISK_VER_CODE=${Config.versionCode}" - ) - } - filter("eol" to FixCrLfFilter.CrLf.newInstance("lf")) - } - } - - val syncResources = tasks.register("syncResources", Sync::class.java) { - dependsOn(syncAssets) into("src/main/resources/META-INF/com/google/android") from(rootProject.file("scripts/update_binary.sh")) { rename { "update-binary" } @@ -222,20 +194,42 @@ fun Project.setupApp() { val variantCapped = name.capitalize(Locale.ROOT) val variantLowered = name.toLowerCase(Locale.ROOT) - val copyStub = tasks.register("copy${variantCapped}StubApk", Copy::class.java) { + val syncAssets = tasks.register("sync${variantCapped}Assets", Sync::class) { dependsOn(syncResources) + inputs.property("version", Config.version) + inputs.property("versionCode", Config.versionCode) into("src/main/assets") - from(rootProject.file("out/stub-${variantLowered}.apk")) { + from(rootProject.file("scripts")) { + include("util_functions.sh", "boot_patch.sh", "addon.d.sh") + include("uninstaller.sh", "module_installer.sh") + } + from(rootProject.file("tools/bootctl")) + into("chromeos") { + from(rootProject.file("tools/futility")) + from(rootProject.file("tools/keys")) { + include("kernel_data_key.vbprivk", "kernel.keyblock") + } + } + val task = tasks.getByPath(":stub:package$variantCapped") + dependsOn(task) + val apk = task.outputs.files.asFileTree.filter { + it.name.endsWith(".apk") + } + from(apk) { rename { "stub.apk" } } - onlyIf { - if (inputs.sourceFiles.files.size != 1) - throw StopExecutionException("Please build stub first! (./build.py stub)") - true + filesMatching("**/util_functions.sh") { + filter { + it.replace( + "#MAGISK_VERSION_STUB", + "MAGISK_VER='${Config.version}'\nMAGISK_VER_CODE=${Config.versionCode}" + ) + } + filter("eol" to FixCrLfFilter.CrLf.newInstance("lf")) } } - preBuildProvider.get().dependsOn(copyStub) + preBuildProvider.get().dependsOn(syncAssets) val keysDir = rootProject.file("tools/keys") val outSrcDir = File(buildDir, "generated/source/keydata/$name") diff --git a/native/src/base/include/embed.hpp b/native/src/base/include/embed.hpp index 41250240e..c34394a55 100644 --- a/native/src/base/include/embed.hpp +++ b/native/src/base/include/embed.hpp @@ -1,5 +1,3 @@ -#include - #if defined(__arm__) #include #elif defined(__aarch64__) diff --git a/native/src/init/init.cpp b/native/src/init/init.cpp index 33f59bcec..8417bd67e 100644 --- a/native/src/init/init.cpp +++ b/native/src/init/init.cpp @@ -61,10 +61,6 @@ void restore_ramdisk_init() { } } -int dump_manager(const char *path, mode_t mode) { - return dump_bin(manager_xz, sizeof(manager_xz), path, mode); -} - int dump_preload(const char *path, mode_t mode) { return dump_bin(init_ld_xz, sizeof(init_ld_xz), path, mode); } @@ -87,12 +83,6 @@ int main(int argc, char *argv[]) { if (name == "magisk"sv) return magisk_proxy_main(argc, argv); - if (argc > 1 && argv[1] == "-x"sv) { - if (argc > 2 && argv[2] == "manager"sv) - return dump_manager(argv[3], 0644); - return 1; - } - if (getpid() != 1) return 1; diff --git a/native/src/init/init.hpp b/native/src/init/init.hpp index 1e1125488..3d9c95bc0 100644 --- a/native/src/init/init.hpp +++ b/native/src/init/init.hpp @@ -36,7 +36,6 @@ bool check_two_stage(); void setup_klog(); const char *backup_init(); void restore_ramdisk_init(); -int dump_manager(const char *path, mode_t mode); int dump_preload(const char *path, mode_t mode); /*************** diff --git a/native/src/init/rootdir.cpp b/native/src/init/rootdir.cpp index b7c5fa42d..c30308ac0 100644 --- a/native/src/init/rootdir.cpp +++ b/native/src/init/rootdir.cpp @@ -151,6 +151,7 @@ static void patch_socket_name(const char *path) { 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 *stub_xz = sbin ? "/sbin/stub.xz" : "stub.xz"; if (access(m32, F_OK) == 0) { auto magisk = mmap_data(m32); @@ -172,7 +173,13 @@ static void extract_files(bool sbin) { xsymlink("./magisk32", "magisk"); } - dump_manager("stub.apk", 0); + { + auto stub = mmap_data(stub_xz); + unlink(stub_xz); + int fd = xopen("stub.apk", O_WRONLY | O_CREAT, 0); + unxz(fd, stub.buf, stub.sz); + close(fd); + } } #define ROOTMIR MIRRDIR "/system_root" diff --git a/scripts/avd_magisk.sh b/scripts/avd_magisk.sh index d8b65471a..664d6eb03 100755 --- a/scripts/avd_magisk.sh +++ b/scripts/avd_magisk.sh @@ -120,6 +120,7 @@ fi # Magisk stuff mkdir -p $MAGISKBIN 2>/dev/null unzip -oj magisk.apk 'assets/*.sh' -d $MAGISKBIN +unzip -oj magisk.apk 'assets/stub.apk' -d $MAGISKTMP mkdir $NVBASE/modules 2>/dev/null mkdir $POSTFSDATAD 2>/dev/null mkdir $SERVICED 2>/dev/null @@ -143,8 +144,6 @@ ln -s ./magisk $MAGISKTMP/resetprop ln -s ./magisk $MAGISKTMP/magiskhide ln -s ./magiskpolicy $MAGISKTMP/supolicy -./magiskinit -x manager $MAGISKTMP/stub.apk - mkdir -p $MAGISKTMP/.magisk/mirror mkdir $MAGISKTMP/.magisk/block touch $MAGISKTMP/.magisk/config diff --git a/scripts/avd_patch.sh b/scripts/avd_patch.sh index e787953ca..268b4decb 100644 --- a/scripts/avd_patch.sh +++ b/scripts/avd_patch.sh @@ -43,7 +43,7 @@ if [ -z "$FIRST_STAGE" ]; then fi # Extract files from APK -unzip -oj magisk.apk 'assets/util_functions.sh' +unzip -oj magisk.apk 'assets/util_functions.sh' 'assets/stub.apk' . ./util_functions.sh api_level_arch_detect @@ -65,6 +65,7 @@ touch config ./magiskboot compress=xz magisk32 magisk32.xz ./magiskboot compress=xz magisk64 magisk64.xz +./magiskboot compress=xz stub.apk stub.xz export KEEPVERITY=false export KEEPFORCEENCRYPT=true @@ -75,10 +76,11 @@ export KEEPFORCEENCRYPT=true "mkdir 0750 overlay.d/sbin" \ "add 0644 overlay.d/sbin/magisk32.xz magisk32.xz" \ "add 0644 overlay.d/sbin/magisk64.xz magisk64.xz" \ +"add 0644 overlay.d/sbin/stub.xz stub.xz" \ "patch" \ "backup ramdisk.cpio.orig" \ "mkdir 000 .backup" \ "add 000 .backup/.magisk config" -rm -f ramdisk.cpio.orig config magisk*.xz +rm -f ramdisk.cpio.orig config magisk*.xz stub.xz ./magiskboot compress=gzip ramdisk.cpio ramdisk.cpio.gz diff --git a/scripts/boot_patch.sh b/scripts/boot_patch.sh index c334e8d84..e3519ba6e 100644 --- a/scripts/boot_patch.sh +++ b/scripts/boot_patch.sh @@ -164,6 +164,7 @@ if [ -f magisk64 ]; then ./magiskboot compress=xz magisk64 magisk64.xz unset SKIP64 fi +./magiskboot compress=xz stub.apk stub.xz ./magiskboot cpio ramdisk.cpio \ "add 0750 $INIT magiskinit" \ @@ -171,12 +172,13 @@ fi "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/stub.xz stub.xz" \ "patch" \ "backup ramdisk.cpio.orig" \ "mkdir 000 .backup" \ "add 000 .backup/.magisk config" -rm -f ramdisk.cpio.orig config magisk*.xz +rm -f ramdisk.cpio.orig config magisk*.xz stub.xz ################# # Binary Patches