Remove high compression mode

This commit is contained in:
topjohnwu 2018-08-09 18:13:07 +08:00
parent 8b50d84a05
commit 6add682705
10 changed files with 74 additions and 209 deletions

View File

@ -134,9 +134,9 @@ def build_binary(args):
# Dump the binary to header
for arch in archs:
bin_file = os.path.join('native', 'out', arch, 'magisk')
with open(os.path.join('native', 'out', arch, 'binaries_arch_xz.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:
xz_dump(src, out, 'magisk_xz')
binary_dump(src, out, 'magisk_bin')
old_plat = False
flags = base_flags
@ -146,9 +146,9 @@ def build_binary(args):
old_plat = True
if 'magiskinit' in targets:
if not os.path.exists(os.path.join('native', 'out', 'x86', 'binaries_arch_xz.h')):
if not os.path.exists(os.path.join('native', 'out', 'x86', 'binaries_arch.h')):
error('Build "magisk" before building "magiskinit"')
if not os.path.exists(os.path.join('native', 'out', 'binaries_xz.h')):
if not os.path.exists(os.path.join('native', 'out', 'binaries.h')):
error('Build release stub APK before building "magiskinit"')
flags += ' B_INIT=1'
old_plat = True
@ -219,6 +219,15 @@ def sign_apk(source, target):
if proc.returncode != 0:
error('Release sign Magisk Manager failed!')
def binary_dump(src, out, var_name):
out.write('const static unsigned char {}[] = {{'.format(var_name))
for i, c in enumerate(src.read()):
if i % 16 == 0:
out.write('\n')
out.write('0x{:02X},'.format(c))
out.write('\n};\n')
out.flush()
def build_apk(args):
header('* Building Magisk Manager')
@ -244,9 +253,9 @@ def build_apk(args):
rm(unsigned)
# Dump the stub APK to header
mkdir(os.path.join('native', 'out'))
with open(os.path.join('native', 'out', 'binaries_xz.h'), 'w') as out:
with open(os.path.join('native', 'out', 'binaries.h'), 'w') as out:
with open(release, 'rb') as src:
xz_dump(src, out, 'manager_xz')
binary_dump(src, out, 'manager_bin');
else:
proc = execv([gradlew, 'app:assembleDebug'])
if proc.returncode != 0:
@ -276,15 +285,6 @@ def build_snet(args):
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')

View File

@ -68,7 +68,7 @@ ifdef B_INIT
# magiskinit
include $(CLEAR_VARS)
LOCAL_MODULE := magiskinit
LOCAL_STATIC_LIBRARIES := libsepol liblzma
LOCAL_STATIC_LIBRARIES := libsepol
LOCAL_C_INCLUDES := \
jni/include \
jni/magiskpolicy \

View File

@ -36,16 +36,13 @@
#include <sys/sendfile.h>
#include <sys/sysmacros.h>
#include <lzma.h>
#include "binaries_xz.h"
#include "binaries_arch_xz.h"
#include "binaries.h"
#include "binaries_arch.h"
#include "magiskrc.h"
#include "utils.h"
#include "magiskpolicy.h"
#include "daemon.h"
#include "cpio.h"
#include "magisk.h"
#ifdef MAGISK_DEBUG
@ -246,45 +243,18 @@ static int patch_sepolicy() {
return 0;
}
#define BUFSIZE (1 << 20)
static int unxz(const void *buf, size_t size, int fd) {
lzma_stream strm = LZMA_STREAM_INIT;
if (lzma_auto_decoder(&strm, UINT64_MAX, 0) != LZMA_OK)
return 1;
lzma_ret ret;
void *out = malloc(BUFSIZE);
strm.next_in = buf;
strm.avail_in = size;
do {
strm.next_out = out;
strm.avail_out = BUFSIZE;
ret = lzma_code(&strm, LZMA_RUN);
xwrite(fd, out, BUFSIZE - strm.avail_out);
} while (strm.avail_out == 0 && ret == LZMA_OK);
free(out);
lzma_end(&strm);
if (ret != LZMA_OK && ret != LZMA_STREAM_END)
return 1;
static int dump_magisk(const char *path, mode_t mode) {
int fd = creat(path, mode);
xwrite(fd, magisk_bin, sizeof(magisk_bin));
close(fd);
return 0;
}
static int dump_magisk(const char *path, mode_t mode) {
unlink(path);
int fd = creat(path, mode);
int ret = unxz(magisk_xz, sizeof(magisk_xz), fd);
close(fd);
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);
xwrite(fd, manager_bin, sizeof(manager_bin));
close(fd);
return ret;
return 0;
}
static int dump_magiskrc(const char *path, mode_t mode) {
@ -365,23 +335,6 @@ int main(int argc, char *argv[]) {
// Clear rootfs
excl_list = (char *[]) { "overlay", ".backup", "proc", "sys", "init.bak", NULL };
frm_rf(root);
} else if (access("/ramdisk.cpio.xz", R_OK) == 0) {
// High compression mode
void *addr;
size_t size;
mmap_ro("/ramdisk.cpio.xz", &addr, &size);
int fd = creat("/ramdisk.cpio", 0);
unxz(addr, size, fd);
munmap(addr, size);
close(fd);
struct vector v;
vec_init(&v);
parse_cpio(&v, "/ramdisk.cpio");
excl_list = (char *[]) { "overlay", ".backup", "proc", "sys", "init.bak", NULL };
frm_rf(root);
chdir("/");
cpio_extract_all(&v);
cpio_vec_destroy(&v);
} else {
// Revert original init binary
link("/.backup/init", "/init");

View File

@ -139,8 +139,8 @@ int fsetattr(int fd, struct file_attr *a);
void fclone_attr(const int sourcefd, const int targetfd);
void clone_attr(const char *source, const char *target);
void restorecon();
int mmap_ro(const char *filename, void **buf, size_t *size);
int mmap_rw(const char *filename, void **buf, size_t *size);
void mmap_ro(const char *filename, void **buf, size_t *size);
void mmap_rw(const char *filename, void **buf, size_t *size);
void fd_full_read(int fd, void **buf, size_t *size);
void full_read(const char *filename, void **buf, size_t *size);
void full_read_at(int dirfd, const char *filename, void **buf, size_t *size);

View File

@ -11,11 +11,6 @@
#include "mincrypt/sha.h"
#include "mincrypt/sha256.h"
#define INSUF_BLOCK_RET 2
#define CHROMEOS_RET 3
#define ELF32_RET 4
#define ELF64_RET 5
// Macros to determine header on-the-go
#define lheader(b, e, o) \
((b)->flags & PXA_FLAG) ? \
@ -88,10 +83,13 @@ static void clean_boot(boot_img *boot) {
memset(boot, 0, sizeof(*boot));
}
#define CHROMEOS_RET 2
#define ELF32_RET 3
#define ELF64_RET 4
#define pos_align() pos = align(pos, header(boot, page_size))
int parse_img(const char *image, boot_img *boot) {
memset(boot, 0, sizeof(*boot));
int is_blk = mmap_ro(image, &boot->map_addr, &boot->map_size);
mmap_ro(image, &boot->map_addr, &boot->map_size);
// Parse image
fprintf(stderr, "Parsing boot image: [%s]\n", image);
@ -246,8 +244,7 @@ int parse_img(const char *image, boot_img *boot) {
get_fmt_name(boot->r_fmt, fmt);
fprintf(stderr, "RAMDISK_FMT [%s]\n", fmt);
return boot->flags & CHROMEOS_FLAG ? CHROMEOS_RET :
((is_blk && boot->tail_size < 500 * 1024) ? INSUF_BLOCK_RET : 0);
return boot->flags & CHROMEOS_FLAG ? CHROMEOS_RET : 0;
default:
continue;
}

View File

@ -17,14 +17,10 @@ static void usage(char *arg0) {
"Usage: %s <action> [args...]\n"
"\n"
"Supported actions:\n"
" --parse <bootimg>\n"
" Parse <bootimg> only, do not unpack. Return values: \n"
" 0:OK 1:error 2:insufficient boot partition size\n"
" 3:chromeos 4:ELF32 5:ELF64\n"
"\n"
" --unpack <bootimg>\n"
" Unpack <bootimg> to kernel, ramdisk.cpio, (second), (dtb), (extra) into\n"
" the current directory. Return value is the same as --parse\n"
" Unpack <bootimg> to kernel, ramdisk.cpio, and if available, second, dtb,\n"
" and extra into the current directory. Return values:\n"
" 0:valid 1:error 2:chromeos 3:ELF32 4:ELF64\n"
"\n"
" --repack <origbootimg> [outbootimg]\n"
" Repack kernel, ramdisk.cpio[.ext], second, dtb... from current directory\n"
@ -54,7 +50,8 @@ static void usage(char *arg0) {
" Extract ENTRY to OUT, or extract all entries to current directory\n"
" test\n"
" Test the current cpio's patch status\n"
" Return value: 0/stock 1/Magisk 2/other (phh, SuperSU, Xposed)\n"
" Return values:\n"
" 0:stock 1:Magisk 2:unsupported (phh, SuperSU, Xposed)\n"
" patch KEEPVERITY KEEPFORCEENCRYPT\n"
" Ramdisk patches. KEEP**** are boolean values\n"
" backup ORIG [SHA1]\n"
@ -62,10 +59,10 @@ static void usage(char *arg0) {
" SHA1 of stock boot image is optional\n"
" restore\n"
" Restore ramdisk from ramdisk backup stored within incpio\n"
" magisk ORIG HIGHCOMP KEEPVERITY KEEPFORCEENCRYPT [SHA1]\n"
" magisk ORIG KEEPVERITY KEEPFORCEENCRYPT [SHA1]\n"
" Do Magisk patches and backups all in one step\n"
" Create ramdisk backups from ORIG\n"
" HIGHCOMP, KEEP**** are boolean values\n"
" KEEP**** are boolean values\n"
" SHA1 of stock boot image is optional\n"
" sha1\n"
" Print stock boot SHA1 if previously stored\n"
@ -77,7 +74,8 @@ static void usage(char *arg0) {
" Dump all contents from dtb for debugging\n"
" test\n"
" Check if fstab has verity/avb flags\n"
" Return value: 0/no flags 1/flag exists\n"
" Return values:\n"
" 0:no flags 1:flag exists\n"
" patch\n"
" Search for fstab and remove verity/avb\n"
"\n"
@ -135,9 +133,6 @@ int main(int argc, char *argv[]) {
printf("%02x", sha1[i]);
printf("\n");
munmap(buf, size);
} else if (argc > 2 && strcmp(argv[1], "--parse") == 0) {
boot_img boot;
return parse_img(argv[2], &boot);
} else if (argc > 2 && strcmp(argv[1], "--unpack") == 0) {
return unpack(argv[2]);
} else if (argc > 2 && strcmp(argv[1], "--repack") == 0) {

View File

@ -32,21 +32,17 @@ static void cpio_patch(struct vector *v, int keepverity, int keepforceencrypt) {
}
}
#define STOCK_BOOT 0x0
#define MAGISK_PATCH 0x1
#define HIGH_COMPRESS 0x2
#define OTHER_PATCH 0x3
#define STOCK_BOOT 0x0
#define MAGISK_PATCH 0x1
#define UNSUPPORT_PATCH 0x2
static int cpio_test(struct vector *v) {
const char *OTHER_LIST[] = { "sbin/launch_daemonsu.sh", "sbin/su", "init.xposed.rc", "boot/sbin/launch_daemonsu.sh", NULL };
const char *UNSUPPORT_LIST[] = { "sbin/launch_daemonsu.sh", "sbin/su", "init.xposed.rc", "boot/sbin/launch_daemonsu.sh", NULL };
const char *MAGISK_LIST[] = { ".backup/.magisk", "init.magisk.rc", "overlay/init.magisk.rc", NULL };
for (int i = 0; OTHER_LIST[i]; ++i)
if (cpio_find(v, OTHER_LIST[i]) >= 0)
return OTHER_PATCH;
if (cpio_find(v, "ramdisk.cpio.xz") >= 0)
return HIGH_COMPRESS;
for (int i = 0; UNSUPPORT_LIST[i]; ++i)
if (cpio_find(v, UNSUPPORT_LIST[i]) >= 0)
return UNSUPPORT_PATCH;
for (int i = 0; MAGISK_LIST[i]; ++i)
if (cpio_find(v, MAGISK_LIST[i]) >= 0)
@ -196,55 +192,6 @@ static void cpio_restore(struct vector *v) {
cpio_rm(v, 0, "sbin/magic_mask.sh");
cpio_rm(v, 0, "init.magisk.rc");
cpio_rm(v, 0, "magisk");
cpio_rm(v, 0, "ramdisk-recovery.xz");
}
static void restore_high_compress(struct vector *v, const char *incpio) {
// Check if the ramdisk is in high compression mode
if (cpio_extract(v, "ramdisk.cpio.xz", incpio) == 0) {
void *xz;
size_t size;
full_read(incpio, &xz, &size);
int fd = creat(incpio, 0644);
lzma(0, fd, xz, size);
close(fd);
free(xz);
cpio_rm(v, 0, "ramdisk.cpio.xz");
cpio_rm(v, 0, "init");
struct vector vv;
vec_init(&vv);
parse_cpio(&vv, incpio);
cpio_entry *e;
vec_for_each(&vv, e)
vec_push_back(v, e);
vec_destroy(&vv);
}
}
static void enable_high_compress(struct vector *v, struct vector *b, const char *incpio) {
cpio_entry *init, *magiskinit;
// Swap magiskinit with original init
int i = cpio_find(b, ".backup/init"), j = cpio_find(v, "init");
init = vec_entry(b)[i];
magiskinit = vec_entry(v)[j];
free(init->filename);
init->filename = strdup("init");
vec_entry(v)[j] = init;
vec_entry(b)[i] = NULL;
dump_cpio(v, incpio);
cpio_vec_destroy(v);
void *cpio;
size_t size;
full_read(incpio, &cpio, &size);
int fd = creat(incpio, 0644);
lzma(1, fd, cpio, size);
close(fd);
free(cpio);
vec_init(v);
vec_push_back(v, magiskinit);
cpio_add(v, 0, "ramdisk.cpio.xz", incpio);
}
int cpio_commands(int argc, char *argv[]) {
@ -267,7 +214,6 @@ int cpio_commands(int argc, char *argv[]) {
if (strcmp(cmdv[0], "test") == 0) {
exit(cpio_test(&v));
} else if (strcmp(cmdv[0], "restore") == 0) {
restore_high_compress(&v, incpio);
cpio_restore(&v);
} else if (strcmp(cmdv[0], "sha1") == 0) {
char *sha1 = cpio_sha1(&v);
@ -282,12 +228,12 @@ int cpio_commands(int argc, char *argv[]) {
vec_for_each(&back, e)
if (e) vec_push_back(&v, e);
vec_destroy(&back);
} else if (cmdc >= 5 && strcmp(cmdv[0], "magisk") == 0) {
cpio_patch(&v, strcmp(cmdv[3], "true") == 0, strcmp(cmdv[4], "true") == 0);
} else if (cmdc >= 4 && strcmp(cmdv[0], "magisk") == 0) {
cpio_patch(&v, strcmp(cmdv[2], "true") == 0, strcmp(cmdv[3], "true") == 0);
struct vector back;
vec_init(&back);
cpio_backup(&v, &back, cmdv[1], cmdc > 5 ? cmdv[5] : NULL);
cpio_backup(&v, &back, cmdv[1], cmdc > 4 ? cmdv[4] : NULL);
cpio_entry *e;
e = xcalloc(sizeof(*e), 1);
@ -298,10 +244,6 @@ int cpio_commands(int argc, char *argv[]) {
e->filesize = strlen(e->data) + 1;
vec_push_back(&back, e);
// Enable high compression mode
if (strcmp(cmdv[2], "true") == 0)
enable_high_compress(&v, &back, incpio);
vec_for_each(&back, e)
if (e) vec_push_back(&v, e);
vec_destroy(&back);

View File

@ -425,7 +425,7 @@ void restorecon() {
#endif // SELINUX
static int _mmap(int rw, const char *filename, void **buf, size_t *size) {
static void _mmap(int rw, const char *filename, void **buf, size_t *size) {
struct stat st;
int fd = xopen(filename, (rw ? O_RDWR : O_RDONLY) | O_CLOEXEC);
fstat(fd, &st);
@ -435,15 +435,14 @@ static int _mmap(int rw, const char *filename, void **buf, size_t *size) {
*size = st.st_size;
*buf = *size > 0 ? xmmap(NULL, *size, PROT_READ | (rw ? PROT_WRITE : 0), MAP_SHARED, fd, 0) : NULL;
close(fd);
return S_ISBLK(st.st_mode);
}
int mmap_ro(const char *filename, void **buf, size_t *size) {
return _mmap(0, filename, buf, size);
void mmap_ro(const char *filename, void **buf, size_t *size) {
_mmap(0, filename, buf, size);
}
int mmap_rw(const char *filename, void **buf, size_t *size) {
return _mmap(1, filename, buf, size);
void mmap_rw(const char *filename, void **buf, size_t *size) {
_mmap(1, filename, buf, size);
}
void fd_full_read(int fd, void **buf, size_t *size) {

View File

@ -4,10 +4,10 @@
# Magisk Boot Image Patcher
# by topjohnwu
#
# Usage: sh boot_patch.sh <bootimage>
# Usage: boot_patch.sh <bootimage>
#
# The following additional flags can be set in environment variables:
# KEEPVERITY, KEEPFORCEENCRYPT, HIGHCOMP
# The following flags can be set in environment variables:
# KEEPVERITY, KEEPFORCEENCRYPT
#
# This script should be placed in a directory with the following files:
#
@ -58,7 +58,6 @@ BOOTIMAGE="$1"
# Flags
[ -z $KEEPVERITY ] && KEEPVERITY=false
[ -z $KEEPFORCEENCRYPT ] && KEEPFORCEENCRYPT=false
[ -z $HIGHCOMP ] && HIGHCOMP=false
chmod -R 755 .
@ -79,17 +78,14 @@ case $? in
abort "! Unable to unpack boot image"
;;
2 )
HIGHCOMP=true
;;
3 )
ui_print "- ChromeOS boot image detected"
CHROMEOS=true
;;
4 )
3 )
ui_print "! Sony ELF32 format detected"
abort "! Please use BootBridge from @AdrianDC to flash Magisk"
;;
5 )
4 )
ui_print "! Sony ELF64 format detected"
abort "! Stock kernel cannot be patched, please use a custom kernel"
esac
@ -100,7 +96,6 @@ esac
# Test patch status and do restore, after this section, ramdisk.cpio.orig is guaranteed to exist
ui_print "- Checking ramdisk status"
MAGISK_PATCHED=false
./magiskboot --cpio ramdisk.cpio test
case $? in
0 ) # Stock boot
@ -112,32 +107,18 @@ case $? in
cp -af ramdisk.cpio ramdisk.cpio.orig
;;
1 ) # Magisk patched
MAGISK_PATCHED=true
HIGHCOMP=false
ui_print "- Magisk patched boot image detected"
# Find SHA1 of stock boot image
[ -z $SHA1 ] && SHA1=`./magiskboot --cpio ramdisk.cpio sha1 2>/dev/null`
./magiskboot --cpio ramdisk.cpio restore
cp -af ramdisk.cpio ramdisk.cpio.orig
;;
2 ) # High compression mode
MAGISK_PATCHED=true
HIGHCOMP=true
;;
3 ) # Other patched
ui_print "! Boot image patched by other programs"
2 ) # Other patched
ui_print "! Boot image patched by unsupported programs"
abort "! Please restore stock boot image"
;;
esac
if $MAGISK_PATCHED; then
ui_print "- Magisk patched image detected"
# Find SHA1 of stock boot image
[ -z $SHA1 ] && SHA1=`./magiskboot --cpio ramdisk.cpio sha1 2>/dev/null`
./magiskboot --cpio ramdisk.cpio restore
cp -af ramdisk.cpio ramdisk.cpio.orig
fi
if $HIGHCOMP; then
ui_print "! Insufficient boot partition size detected"
ui_print "- Enable high compression mode"
fi
##########################################################################################
# Ramdisk patches
##########################################################################################
@ -146,7 +127,7 @@ ui_print "- Patching ramdisk"
./magiskboot --cpio ramdisk.cpio \
"add 750 init magiskinit" \
"magisk ramdisk.cpio.orig $HIGHCOMP $KEEPVERITY $KEEPFORCEENCRYPT $SHA1"
"magisk ramdisk.cpio.orig $KEEPVERITY $KEEPFORCEENCRYPT $SHA1"
rm -f ramdisk.cpio.orig

View File

@ -1,11 +1,9 @@
#MAGISK
##########################################################################################
#
# Magisk Uninstaller (used in recovery)
# Magisk Uninstaller
# by topjohnwu
#
# This script will load the real uninstaller in a flashable zip
#
##########################################################################################
##########################################################################################
@ -75,15 +73,15 @@ case $? in
1 )
abort "! Unable to unpack boot image"
;;
3 )
2 )
ui_print "- ChromeOS boot image detected"
CHROMEOS=true
;;
4 )
3 )
ui_print "! Sony ELF32 format detected"
abort "! Please use BootBridge from @AdrianDC"
;;
5 )
4 )
ui_print "! Sony ELF64 format detected"
abort "! Stock kernel cannot be patched, please use a custom kernel"
esac
@ -95,7 +93,7 @@ case $? in
0 ) # Stock boot
ui_print "- Stock boot image detected"
;;
1|2 ) # Magisk patched
1 ) # Magisk patched
ui_print "- Magisk patched image detected"
./magisk --unlock-blocks 2>/dev/null
# Find SHA1 of stock boot image
@ -119,7 +117,7 @@ case $? in
flash_boot_image new-boot.img $BOOTIMAGE
fi
;;
3 ) # Other patched
2 ) # Other patched
ui_print "! Boot image patched by other programs"
abort "! Cannot uninstall"
;;