From 30eb4074cb91de1427077763313256b295b94241 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 10 Jun 2018 16:55:00 +0800 Subject: [PATCH] Update binary dump method --- build.py | 32 ++++++++++++++++++++------------ native/jni/core/magiskinit.c | 5 +++-- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/build.py b/build.py index d8bdf5453..7e952f4a7 100755 --- a/build.py +++ b/build.py @@ -42,6 +42,7 @@ else: ndk_build = os.path.join(os.environ['ANDROID_HOME'], 'ndk-bundle', 'ndk-build') cpu_count = multiprocessing.cpu_count() +archs = ['armeabi-v7a', 'x86'] def mv(source, target): try: @@ -79,13 +80,13 @@ def zip_with_msg(zipfile, source, target): zipfile.write(source, target) def build_all(args): - build_binary(args) build_apk(args) + build_binary(args) zip_main(args) zip_uninstaller(args) def collect_binary(): - for arch in ['armeabi-v7a', 'x86']: + for arch in archs: mkdir_p(os.path.join('native', 'out', arch)) for bin in ['magisk', 'magiskinit', 'magiskboot', 'busybox', 'b64xz']: source = os.path.join('native', 'libs', arch, bin) @@ -139,16 +140,13 @@ def build_binary(args): flags = base_flags if 'magiskinit' in targets: - # We need to create dump.h beforehand - if not os.path.exists(os.path.join('native', 'out', 'armeabi-v7a', 'magisk')): - error('Build "magisk" before building "magiskinit"') - for arch in ['armeabi-v7a', 'x86']: - with open(os.path.join('native', 'out', arch, 'dump.h'), 'w') as dump: - dump.write('#include "stdlib.h"\n') - with open(os.path.join('native', 'out', arch, 'magisk'), 'rb') as bin: - dump.write('const uint8_t magisk_dump[] = "') - dump.write(''.join("\\x{:02X}".format(c) for c in lzma.compress(bin.read(), preset=9))) - dump.write('";\n') + for arch in archs: + bin_file = os.path.join('native', 'out', arch, 'magisk') + if not os.path.exists(bin_file): + error('Build "magisk" before building "magiskinit"') + 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') flags += ' B_INIT=1' other = True @@ -239,8 +237,18 @@ def build_snet(args): with zipfile.ZipFile(source) as zin: for item in zin.infolist(): zout.writestr(item.filename, zin.read(item)) + rm(source) header('Output: ' + target) +def xz_dump(src, out, var_name): + out.write('const static unsigned char {}[] = {{'.format(var_name)) + for i, c in enumerate(lzma.compress(src.read(), preset=9)): + if i % 16 == 0: + out.write('\n') + out.write('0x{:02X},'.format(c)) + out.write('\n};\n') + out.flush() + def gen_update_binary(): update_bin = [] binary = os.path.join('native', 'out', 'armeabi-v7a', 'b64xz') diff --git a/native/jni/core/magiskinit.c b/native/jni/core/magiskinit.c index 10535caf8..c2a339d2d 100644 --- a/native/jni/core/magiskinit.c +++ b/native/jni/core/magiskinit.c @@ -39,7 +39,8 @@ #include #include -#include "dump.h" +#include "binaries_arch_xz.h" + #include "magiskrc.h" #include "utils.h" #include "magiskpolicy.h" @@ -321,7 +322,7 @@ static int unxz(const void *buf, size_t size, int fd) { static int dump_magisk(const char *path, mode_t mode) { unlink(path); int fd = creat(path, mode); - int ret = unxz(magisk_dump, sizeof(magisk_dump), fd); + int ret = unxz(magisk_xz, sizeof(magisk_xz), fd); close(fd); return ret; }