Welcome to the 64 bit world!

Close #854
This commit is contained in:
topjohnwu 2019-02-24 08:13:27 -05:00
parent ea129fb206
commit 6ca8db2f0c
8 changed files with 61 additions and 58 deletions

View File

@ -108,7 +108,7 @@ public abstract class MagiskInstaller {
protected boolean extractZip() { protected boolean extractZip() {
String arch; String arch;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= 21) {
List<String> abis = Arrays.asList(Build.SUPPORTED_ABIS); List<String> abis = Arrays.asList(Build.SUPPORTED_ABIS);
arch = abis.contains("x86") ? "x86" : "arm"; arch = abis.contains("x86") ? "x86" : "arm";
} else { } else {
@ -162,8 +162,6 @@ public abstract class MagiskInstaller {
console.add("! Cannot unzip zip"); console.add("! Cannot unzip zip");
return false; return false;
} }
Shell.sh(Utils.fmt("chmod -R 755 %s/*; %s/magiskinit -x magisk %s/magisk",
installDir, installDir, installDir)).exec();
return true; return true;
} }
@ -213,14 +211,18 @@ public abstract class MagiskInstaller {
return false; return false;
} }
// Patch boot image Shell.Job job = Shell.sh("cd " + installDir);
if (!Shell.sh(Utils.fmt("cd %s; KEEPFORCEENCRYPT=%b KEEPVERITY=%b " + if (Build.VERSION.SDK_INT >= 21 && Build.SUPPORTED_64_BIT_ABIS.length != 0) {
job.add("mv -f magiskinit64 magiskinit 2>/dev/null");
} else {
job.add("rm -f magiskinit64 2>/dev/null");
}
if (!job.add(Utils.fmt("KEEPFORCEENCRYPT=%b KEEPVERITY=%b " +
"sh update-binary sh boot_patch.sh %s", "sh update-binary sh boot_patch.sh %s",
installDir, Config.keepEnc, Config.keepVerity, srcBoot)) Config.keepEnc, Config.keepVerity, srcBoot)).to(console, logs).exec().isSuccess())
.to(console, logs).exec().isSuccess())
return false; return false;
Shell.Job job = Shell.sh("mv bin/busybox busybox", job = Shell.sh("mv bin/busybox busybox",
"rm -rf magisk.apk bin boot.img update-binary", "rm -rf magisk.apk bin boot.img update-binary",
"cd /"); "cd /");

View File

@ -14,6 +14,7 @@ fix_env() {
./busybox rm -f $MAGISKTMP/busybox/* ./busybox rm -f $MAGISKTMP/busybox/*
$MAGISKTMP/mirror/bin/busybox --install -s $MAGISKTMP/busybox $MAGISKTMP/mirror/bin/busybox --install -s $MAGISKTMP/busybox
rm -f update-binary magisk.apk rm -f update-binary magisk.apk
chmod -R 755 .
cd / cd /
} }

View File

@ -53,6 +53,7 @@ else:
cpu_count = multiprocessing.cpu_count() cpu_count = multiprocessing.cpu_count()
gradlew = os.path.join('.', 'gradlew.bat' if os.name == 'nt' else 'gradlew') gradlew = os.path.join('.', 'gradlew.bat' if os.name == 'nt' else 'gradlew')
archs = ['armeabi-v7a', 'x86'] archs = ['armeabi-v7a', 'x86']
arch64 = ['arm64-v8a', 'x86_64']
keystore = 'release-key.jks' keystore = 'release-key.jks'
config = {} config = {}
@ -99,9 +100,9 @@ def zip_with_msg(zip_file, source, target):
def collect_binary(): def collect_binary():
for arch in archs: for arch in archs + arch64:
mkdir_p(os.path.join('native', 'out', arch)) mkdir_p(os.path.join('native', 'out', arch))
for bin in ['magisk', 'magiskinit', 'magiskboot', 'busybox']: for bin in ['magisk', 'magiskinit', 'magiskinit64', 'magiskboot', 'busybox']:
source = os.path.join('native', 'libs', arch, bin) source = os.path.join('native', 'libs', arch, bin)
target = os.path.join('native', 'out', arch, bin) target = os.path.join('native', 'out', arch, bin)
mv(source, target) mv(source, target)
@ -174,67 +175,60 @@ def gen_update_binary():
return update_bin return update_bin
def run_ndk_build(flags):
proc = system(f'{ndk_build} -C native {base_flags} {flags} -j{cpu_count}')
if proc.returncode != 0:
error('Build binary failed!')
collect_binary()
def build_binary(args): def build_binary(args):
support_targets = {'magisk', 'magiskinit', 'magiskboot', 'busybox'} support_targets = {'magisk', 'magiskinit', 'magiskboot', 'busybox'}
if args.target: if args.target:
args.target = set(args.target) & support_targets args.target = set(args.target) & support_targets
if not args.target:
return
else: else:
# If nothing specified, build everything # If nothing specified, build everything
args.target = support_targets args.target = support_targets
# Unsure why this is separate
if not args.target:
return
header('* Building binaries: ' + ' '.join(args.target)) header('* Building binaries: ' + ' '.join(args.target))
os.utime(os.path.join('native', 'jni', 'include', 'flags.h')) os.utime(os.path.join('native', 'jni', 'include', 'flags.h'))
# Basic flags # Basic flags
global base_flags
base_flags = f'MAGISK_VERSION={config["version"]} MAGISK_VER_CODE={config["versionCode"]}' base_flags = f'MAGISK_VERSION={config["version"]} MAGISK_VER_CODE={config["versionCode"]}'
if not args.release: if not args.release:
base_flags += ' MAGISK_DEBUG=1' base_flags += ' MAGISK_DEBUG=1'
# Magisk is special case as it is a dependency of magiskinit
if 'magisk' in args.target: if 'magisk' in args.target:
proc = system(f'{ndk_build} -C native {base_flags} B_MAGISK=1 -j{cpu_count}') run_ndk_build('B_MAGISK=1')
if proc.returncode != 0:
error('Build Magisk binary failed!')
collect_binary()
# Dump the binary to header # Dump the binary to header
for arch in archs: for arch in archs:
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_xz') binary_dump(src, out, 'magisk_xz')
for arch, arch32 in list(zip(arch64, archs)):
bin_file = os.path.join('native', 'out', arch, 'magisk')
with open(os.path.join('native', 'out', arch32, 'binaries_arch64.h'), 'w') as out:
with open(bin_file, 'rb') as src:
binary_dump(src, out, 'magisk_xz')
# BusyBox is special case as it needs special flags to build
if 'busybox' in args.target: if 'busybox' in args.target:
proc = system(f'{ndk_build} -C native {base_flags} B_BB=1 -j{cpu_count}') run_ndk_build('B_BB=1')
if proc.returncode != 0:
error('Build binaries failed!')
collect_binary()
build = False
flags = base_flags
if 'magiskinit' in args.target: if 'magiskinit' in args.target:
if not os.path.exists(os.path.join('native', 'out', 'x86', 'binaries_arch.h')): if not os.path.exists(os.path.join('native', 'out', 'x86', 'binaries_arch.h')):
error('Build "magisk" before building "magiskinit"') error('Build "magisk" before building "magiskinit"')
if not os.path.exists(os.path.join('native', 'out', 'binaries.h')): if not os.path.exists(os.path.join('native', 'out', 'binaries.h')):
error('Build stub APK before building "magiskinit"') error('Build stub APK before building "magiskinit"')
flags += ' B_INIT=1' run_ndk_build('B_INIT=1')
build = True run_ndk_build('B_INIT64=1')
if 'magiskboot' in args.target: if 'magiskboot' in args.target:
flags += ' B_BOOT=1' run_ndk_build('B_BOOT=1')
build = True
if build:
proc = system(f'{ndk_build} -C native {flags} -j{cpu_count}')
if proc.returncode != 0:
error('Build binaries failed!')
collect_binary()
def build_apk(args, flavor): def build_apk(args, flavor):
@ -311,7 +305,7 @@ def zip_main(args):
# Binaries # Binaries
for lib_dir, zip_dir in [('armeabi-v7a', 'arm'), ('x86', 'x86')]: for lib_dir, zip_dir in [('armeabi-v7a', 'arm'), ('x86', 'x86')]:
for binary in ['magiskinit', 'magiskboot']: for binary in ['magiskinit', 'magiskinit64', 'magiskboot']:
source = os.path.join('native', 'out', lib_dir, binary) source = os.path.join('native', 'out', lib_dir, binary)
target = os.path.join(zip_dir, binary) target = os.path.join(zip_dir, binary)
zip_with_msg(zipf, source, target) zip_with_msg(zipf, source, target)

View File

@ -12,7 +12,7 @@ android {
externalNativeBuild { externalNativeBuild {
ndkBuild { ndkBuild {
// Pass arguments to ndk-build. // Pass arguments to ndk-build.
arguments('B_MAGISK=1', 'B_INIT=1', 'B_BOOT=1', arguments('B_MAGISK=1', 'B_INIT64=1', 'B_BOOT=1',
'MAGISK_DEBUG=1', 'MAGISK_VERSION=debug', 'MAGISK_VER_CODE=INT_MAX') 'MAGISK_DEBUG=1', 'MAGISK_VERSION=debug', 'MAGISK_VER_CODE=INT_MAX')
} }
} }

View File

@ -19,7 +19,6 @@ LIBUTILS := jni/utils/include
ifdef B_MAGISK ifdef B_MAGISK
# magisk main binary
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_MODULE := magisk LOCAL_MODULE := magisk
LOCAL_SHARED_LIBRARIES := libsqlite LOCAL_SHARED_LIBRARIES := libsqlite
@ -56,11 +55,19 @@ include $(BUILD_EXECUTABLE)
endif endif
ifdef B_INIT
# magiskinit
include $(CLEAR_VARS) include $(CLEAR_VARS)
ifdef B_INIT
LOCAL_MODULE := magiskinit LOCAL_MODULE := magiskinit
BB_INIT := 1
else ifdef B_INIT64
LOCAL_MODULE := magiskinit64
LOCAL_CPPFLAGS += -DUSE_64BIT
BB_INIT := 1
endif
ifdef BB_INIT
LOCAL_STATIC_LIBRARIES := libsepol libxz libutils LOCAL_STATIC_LIBRARIES := libsepol libxz libutils
LOCAL_C_INCLUDES := \ LOCAL_C_INCLUDES := \
jni/include \ jni/include \
@ -85,7 +92,6 @@ endif
ifdef B_BOOT ifdef B_BOOT
# magiskboot
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_MODULE := magiskboot LOCAL_MODULE := magiskboot
LOCAL_STATIC_LIBRARIES := libmincrypt liblzma liblz4 libbz2 libfdt libutils LOCAL_STATIC_LIBRARIES := libmincrypt liblzma liblz4 libbz2 libfdt libutils
@ -116,7 +122,6 @@ endif
ifdef B_BB ifdef B_BB
# Busybox
include jni/external/busybox/Android.mk include jni/external/busybox/Android.mk
endif endif

View File

@ -9,10 +9,15 @@ ifdef MAGISK_DEBUG
APP_CFLAGS += -D__MDBG APP_CFLAGS += -D__MDBG
endif endif
# Build 64 bit binaries for magisk
ifdef B_MAGISK
APP_ABI += arm64-v8a x86_64
endif
# Busybox require some additional settings # Busybox require some additional settings
ifdef B_BB ifdef B_BB
APP_CFLAGS := -Os APP_CFLAGS := -Os
APP_SHORT_COMMANDS := true APP_SHORT_COMMANDS := true
NDK_TOOLCHAIN_VERSION := 4.9 NDK_TOOLCHAIN_VERSION := 4.9
APP_PLATFORM := android-22 APP_PLATFORM := android-21
endif endif

View File

@ -44,7 +44,11 @@
#include <flags.h> #include <flags.h>
#include "binaries.h" #include "binaries.h"
#ifdef USE_64BIT
#include "binaries_arch64.h"
#else
#include "binaries_arch.h" #include "binaries_arch.h"
#endif
#include "magiskrc.h" #include "magiskrc.h"
#define DEFAULT_DT_DIR "/proc/device-tree/firmware/android" #define DEFAULT_DT_DIR "/proc/device-tree/firmware/android"
@ -367,12 +371,6 @@ static void setup_init_rc() {
fclose(rc); fclose(rc);
} }
static const char wrapper[] =
"#!/system/bin/sh\n"
"unset LD_LIBRARY_PATH\n"
"unset LD_PRELOAD\n"
"exec /sbin/magisk.bin \"${0##*/}\" \"$@\"\n";
static void setup_overlay() { static void setup_overlay() {
char buf[128]; char buf[128];
int fd; int fd;
@ -402,13 +400,9 @@ static void setup_overlay() {
fd = open("/sbin/magiskinit", O_WRONLY | O_CREAT, 0755); fd = open("/sbin/magiskinit", O_WRONLY | O_CREAT, 0755);
write(fd, self, self_sz); write(fd, self, self_sz);
close(fd); close(fd);
fd = open("/sbin/magisk", O_WRONLY | O_CREAT, 0755); dump_magisk("/sbin/magisk", 0755);
write(fd, wrapper, sizeof(wrapper)); patch_socket_name("/sbin/magisk");
close(fd);
dump_magisk("/sbin/magisk.bin", 0755);
patch_socket_name("/sbin/magisk.bin");
setfilecon("/sbin/magisk", "u:object_r:" SEPOL_FILE_DOMAIN ":s0"); setfilecon("/sbin/magisk", "u:object_r:" SEPOL_FILE_DOMAIN ":s0");
setfilecon("/sbin/magisk.bin", "u:object_r:" SEPOL_FILE_DOMAIN ":s0");
setfilecon("/sbin/magiskinit", "u:object_r:" SEPOL_FILE_DOMAIN ":s0"); setfilecon("/sbin/magiskinit", "u:object_r:" SEPOL_FILE_DOMAIN ":s0");
// Create applet symlinks // Create applet symlinks

View File

@ -100,6 +100,8 @@ $BOOTSIGNED && ui_print "- Boot image is signed with AVB 1.0"
SOURCEDMODE=true SOURCEDMODE=true
cd $MAGISKBIN cd $MAGISKBIN
$IS64BIT && mv -f magiskinit64 magiskinit || rm -f magiskinit64
# Source the boot patcher # Source the boot patcher
. ./boot_patch.sh "$BOOTIMAGE" . ./boot_patch.sh "$BOOTIMAGE"