From 970a2e87b37a08e5876a933cd9637b78dc36cc9e Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 14 Jun 2018 00:59:08 +0800 Subject: [PATCH] Bundle in stub APK into magiskinit --- build.py | 9 +++++++++ native/jni/Android.mk | 1 + native/jni/core/magiskinit.c | 11 +++++++++++ 3 files changed, 21 insertions(+) diff --git a/build.py b/build.py index 7e952f4a7..f41d2f5fd 100755 --- a/build.py +++ b/build.py @@ -147,6 +147,15 @@ def build_binary(args): with open(os.path.join('native', 'out', arch, 'binaries_arch_xz.h'), 'w') as out: with open(bin_file, 'rb') as src: xz_dump(src, out, 'magisk_xz') + + stub_apk = os.path.join(config['outdir'], 'stub-release.apk') + if not os.path.exists(stub_apk): + error('Build release stub APK before building "magiskinit"') + + with open(os.path.join('native', 'out', 'binaries_xz.h'), 'w') as out: + with open(stub_apk, 'rb') as src: + xz_dump(src, out, 'manager_xz') + flags += ' B_INIT=1' other = True diff --git a/native/jni/Android.mk b/native/jni/Android.mk index 133e1747d..8f42138c9 100644 --- a/native/jni/Android.mk +++ b/native/jni/Android.mk @@ -72,6 +72,7 @@ LOCAL_STATIC_LIBRARIES := libsepol liblzma LOCAL_C_INCLUDES := \ jni/include \ jni/magiskpolicy \ + out \ out/$(TARGET_ARCH_ABI) \ $(LIBSEPOL) \ $(LIBLZMA) diff --git a/native/jni/core/magiskinit.c b/native/jni/core/magiskinit.c index c2a339d2d..342754e7e 100644 --- a/native/jni/core/magiskinit.c +++ b/native/jni/core/magiskinit.c @@ -39,6 +39,7 @@ #include #include +#include "binaries_xz.h" #include "binaries_arch_xz.h" #include "magiskrc.h" @@ -327,6 +328,14 @@ static int dump_magisk(const char *path, mode_t mode) { return ret; } +static int dump_manager(const char *path, mode_t mode) { + unlink(path); + int fd = creat(path, mode); + int ret = unxz(manager_xz, sizeof(manager_xz), fd); + close(fd); + return ret; +} + static int dump_magiskrc(const char *path, mode_t mode) { int fd = creat(path, mode); write(fd, magiskrc, sizeof(magiskrc)); @@ -362,6 +371,8 @@ int main(int argc, char *argv[]) { if (argc > 1 && strcmp(argv[1], "-x") == 0) { if (strcmp(argv[2], "magisk") == 0) return dump_magisk(argv[3], 0755); + else if (strcmp(argv[2], "manager") == 0) + return dump_manager(argv[3], 0644); else if (strcmp(argv[2], "magiskrc") == 0) return dump_magiskrc(argv[3], 0755); }