Compress binaries and use xz-embedded in magiskinit

This commit is contained in:
topjohnwu 2018-08-10 05:03:54 +08:00
parent 97588408a2
commit d4568aa0a7
4 changed files with 37 additions and 9 deletions

View File

@ -106,6 +106,9 @@ def execv(cmd, redirect=None):
def system(cmd, redirect=None): def system(cmd, redirect=None):
return subprocess.run(cmd, shell=True, stdout=redirect if redirect != None else STDOUT) return subprocess.run(cmd, shell=True, stdout=redirect if redirect != None else STDOUT)
def xz(data):
return lzma.compress(data, preset=9, check=lzma.CHECK_NONE)
def sign_zip(unsigned, output, release): def sign_zip(unsigned, output, release):
signer_name = 'zipsigner-3.0.jar' signer_name = 'zipsigner-3.0.jar'
zipsigner = os.path.join('utils', 'build', 'libs', signer_name) zipsigner = os.path.join('utils', 'build', 'libs', signer_name)
@ -151,7 +154,7 @@ def sign_apk(source, target):
def binary_dump(src, out, var_name): def binary_dump(src, out, var_name):
out.write('const static unsigned char {}[] = {{'.format(var_name)) out.write('const static unsigned char {}[] = {{'.format(var_name))
for i, c in enumerate(src.read()): for i, c in enumerate(xz(src.read())):
if i % 16 == 0: if i % 16 == 0:
out.write('\n') out.write('\n')
out.write('0x{:02X},'.format(c)) out.write('0x{:02X},'.format(c))
@ -173,11 +176,11 @@ def gen_update_binary():
binary = os.path.join('native', 'out', 'armeabi-v7a', 'busybox') binary = os.path.join('native', 'out', 'armeabi-v7a', 'busybox')
with open(binary, 'rb') as busybox: with open(binary, 'rb') as busybox:
update_bin.append('\'\nBB_ARM=') update_bin.append('\'\nBB_ARM=')
update_bin.append(base64.b64encode(lzma.compress(busybox.read(), preset=9, check=lzma.CHECK_NONE)).decode('ascii')) update_bin.append(base64.b64encode(xz(busybox.read())).decode('ascii'))
binary = os.path.join('native', 'out', 'x86', 'busybox') binary = os.path.join('native', 'out', 'x86', 'busybox')
with open(binary, 'rb') as busybox: with open(binary, 'rb') as busybox:
update_bin.append('\nBB_X86=') update_bin.append('\nBB_X86=')
update_bin.append(base64.b64encode(lzma.compress(busybox.read(), preset=9, check=lzma.CHECK_NONE)).decode('ascii')) update_bin.append(base64.b64encode(xz(busybox.read())).decode('ascii'))
update_bin.append('\n') update_bin.append('\n')
with open(os.path.join('scripts', 'update_binary.sh'), 'r') as script: with open(os.path.join('scripts', 'update_binary.sh'), 'r') as script:
update_bin.append(script.read()) update_bin.append(script.read())
@ -213,7 +216,7 @@ def build_binary(args):
bin_file = os.path.join('native', 'out', arch, 'magisk') bin_file = os.path.join('native', 'out', arch, 'magisk')
with open(os.path.join('native', 'out', arch, 'binaries_arch.h'), 'w') as out: with open(os.path.join('native', 'out', arch, 'binaries_arch.h'), 'w') as out:
with open(bin_file, 'rb') as src: with open(bin_file, 'rb') as src:
binary_dump(src, out, 'magisk_bin') binary_dump(src, out, 'magisk_xz')
old_plat = False old_plat = False
flags = base_flags flags = base_flags
@ -280,7 +283,7 @@ def build_apk(args):
mkdir(os.path.join('native', 'out')) mkdir(os.path.join('native', 'out'))
with open(os.path.join('native', 'out', 'binaries.h'), 'w') as out: with open(os.path.join('native', 'out', 'binaries.h'), 'w') as out:
with open(release, 'rb') as src: with open(release, 'rb') as src:
binary_dump(src, out, 'manager_bin'); binary_dump(src, out, 'manager_xz');
else: else:
proc = execv([gradlew, 'app:assembleDebug']) proc = execv([gradlew, 'app:assembleDebug'])
if proc.returncode != 0: if proc.returncode != 0:

View File

@ -67,10 +67,11 @@ ifdef B_INIT
# magiskinit # magiskinit
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_MODULE := magiskinit LOCAL_MODULE := magiskinit
LOCAL_STATIC_LIBRARIES := libsepol LOCAL_STATIC_LIBRARIES := libsepol libxz
LOCAL_C_INCLUDES := \ LOCAL_C_INCLUDES := \
jni/include \ jni/include \
jni/magiskpolicy \ jni/magiskpolicy \
$(EXT_PATH)/include \
out \ out \
out/$(TARGET_ARCH_ABI) \ out/$(TARGET_ARCH_ABI) \
$(LIBSEPOL) $(LIBSEPOL)

View File

@ -22,7 +22,7 @@ static void decodeblock(uint8_t* in, uint8_t* out) {
out[2] = (uint8_t)(((in[2] << 6) & 0xc0) | in[3]); out[2] = (uint8_t)(((in[2] << 6) & 0xc0) | in[3]);
} }
static int unxz(struct xz_dec *dec, void *buf, unsigned size) { static int unxz(struct xz_dec *dec, const void *buf, unsigned size) {
uint8_t out[8192]; uint8_t out[8192];
struct xz_buf b = { struct xz_buf b = {
.in = buf, .in = buf,

View File

@ -36,6 +36,8 @@
#include <sys/sendfile.h> #include <sys/sendfile.h>
#include <sys/sysmacros.h> #include <sys/sysmacros.h>
#include <xz.h>
#include "binaries.h" #include "binaries.h"
#include "binaries_arch.h" #include "binaries_arch.h"
@ -243,16 +245,38 @@ static int patch_sepolicy() {
return 0; return 0;
} }
static int unxz(int fd, const void *buf, size_t size) {
uint8_t out[8192];
struct xz_dec *dec = xz_dec_init(XZ_DYNALLOC, 1 << 26);
struct xz_buf b = {
.in = buf,
.in_pos = 0,
.in_size = size,
.out = out,
.out_pos = 0,
.out_size = sizeof(out)
};
enum xz_ret ret;
do {
ret = xz_dec_run(dec, &b);
if (ret != XZ_OK && ret != XZ_STREAM_END)
return 1;
write(fd, out, b.out_pos);
b.out_pos = 0;
} while (b.in_pos != size);
return 0;
}
static int dump_magisk(const char *path, mode_t mode) { static int dump_magisk(const char *path, mode_t mode) {
int fd = creat(path, mode); int fd = creat(path, mode);
xwrite(fd, magisk_bin, sizeof(magisk_bin)); unxz(fd, magisk_xz, sizeof(magisk_xz));
close(fd); close(fd);
return 0; return 0;
} }
static int dump_manager(const char *path, mode_t mode) { static int dump_manager(const char *path, mode_t mode) {
int fd = creat(path, mode); int fd = creat(path, mode);
xwrite(fd, manager_bin, sizeof(manager_bin)); unxz(fd, manager_xz, sizeof(manager_xz));
close(fd); close(fd);
return 0; return 0;
} }