Stop embedding stub.apk in magiskinit

This commit is contained in:
vvb2060 2022-10-31 22:31:15 +08:00 committed by John Wu
parent a4a734458b
commit ad0e6511e1
10 changed files with 55 additions and 79 deletions

View File

@ -131,7 +131,7 @@ abstract class MagiskInstallImpl protected constructor(
} }
// Extract scripts // Extract scripts
for (script in listOf("util_functions.sh", "boot_patch.sh", "addon.d.sh")) { for (script in listOf("util_functions.sh", "boot_patch.sh", "addon.d.sh", "stub.apk")) {
val dest = File(installDir, script) val dest = File(installDir, script)
context.assets.open(script).writeTo(dest) context.assets.open(script).writeTo(dest)
} }

View File

@ -309,13 +309,7 @@ def binary_dump(src, var_name, compressor=xz):
def dump_bin_header(args): def dump_bin_header(args):
stub = op.join(config['outdir'], f'stub-{"release" if args.release else "debug"}.apk')
if not op.exists(stub):
error('Build stub APK before building "magiskinit"')
mkdir_p(native_gen_path) mkdir_p(native_gen_path)
with open(stub, 'rb') as src:
text = binary_dump(src, 'manager_xz')
write_if_diff(op.join(native_gen_path, 'binaries.h'), text)
for arch in archs: for arch in archs:
preload = op.join('native', 'out', arch, 'libinit-ld.so') preload = op.join('native', 'out', arch, 'libinit-ld.so')
with open(preload, 'rb') as src: with open(preload, 'rb') as src:
@ -432,11 +426,6 @@ def build_app(args):
build_apk(args, 'app') build_apk(args, 'app')
def build_stub(args):
header('* Building the stub app')
build_apk(args, 'stub')
def cleanup(args): def cleanup(args):
support_targets = {'native', 'java'} support_targets = {'native', 'java'}
if args.target: if args.target:
@ -554,7 +543,6 @@ def patch_avd_ramdisk(args):
def build_all(args): def build_all(args):
build_stub(args)
build_binary(args) build_binary(args)
build_app(args) build_app(args)
@ -582,9 +570,6 @@ binary_parser.set_defaults(func=build_binary)
app_parser = subparsers.add_parser('app', help='build the Magisk app') app_parser = subparsers.add_parser('app', help='build the Magisk app')
app_parser.set_defaults(func=build_app) app_parser.set_defaults(func=build_app)
stub_parser = subparsers.add_parser('stub', help='build the stub app')
stub_parser.set_defaults(func=build_stub)
avd_parser = subparsers.add_parser( avd_parser = subparsers.add_parser(
'emulator', help='setup AVD for development') 'emulator', help='setup AVD for development')
avd_parser.add_argument('-s', '--skip', action='store_true', avd_parser.add_argument('-s', '--skip', action='store_true',

View File

@ -15,8 +15,7 @@ import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.Delete import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.StopExecutionException import org.gradle.api.tasks.StopExecutionException
import org.gradle.api.tasks.Sync import org.gradle.api.tasks.Sync
import org.gradle.kotlin.dsl.filter import org.gradle.kotlin.dsl.*
import org.gradle.kotlin.dsl.named
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
@ -40,7 +39,7 @@ private fun BaseExtension.kotlinOptions(configure: Action<KotlinJvmOptions>) =
} }
private val Project.android: BaseAppModuleExtension private val Project.android: BaseAppModuleExtension
get() = extensions.getByName("android") as BaseAppModuleExtension get() = extensions["android"] as BaseAppModuleExtension
fun Project.setupCommon() { fun Project.setupCommon() {
androidBase { androidBase {
@ -112,14 +111,14 @@ private fun Project.setupAppCommon() {
} }
buildTypes { buildTypes {
signingConfigs.getByName("config").also { signingConfigs["config"].also {
getByName("debug") { debug {
signingConfig = if (it.storeFile?.exists() == true) it signingConfig = if (it.storeFile?.exists() == true) it
else signingConfigs.getByName("debug") else signingConfigs["debug"]
} }
getByName("release") { release {
signingConfig = if (it.storeFile?.exists() == true) it signingConfig = if (it.storeFile?.exists() == true) it
else signingConfigs.getByName("debug") else signingConfigs["debug"]
} }
} }
} }
@ -147,7 +146,7 @@ private fun Project.setupAppCommon() {
fun Project.setupApp() { fun Project.setupApp() {
setupAppCommon() setupAppCommon()
val syncLibs = tasks.register("syncLibs", Sync::class.java) { val syncLibs by tasks.registering(Sync::class) {
into("src/main/jniLibs") into("src/main/jniLibs")
into("armeabi-v7a") { into("armeabi-v7a") {
from(rootProject.file("native/out/armeabi-v7a")) { from(rootProject.file("native/out/armeabi-v7a")) {
@ -180,35 +179,8 @@ fun Project.setupApp() {
} }
} }
val syncAssets = tasks.register("syncAssets", Sync::class.java) { val syncResources by tasks.registering(Sync::class) {
dependsOn(syncLibs) dependsOn(syncLibs)
inputs.property("version", Config.version)
inputs.property("versionCode", Config.versionCode)
into("src/main/assets")
from(rootProject.file("scripts")) {
include("util_functions.sh", "boot_patch.sh", "addon.d.sh")
include("uninstaller.sh", "module_installer.sh")
}
from(rootProject.file("tools/bootctl"))
into("chromeos") {
from(rootProject.file("tools/futility"))
from(rootProject.file("tools/keys")) {
include("kernel_data_key.vbprivk", "kernel.keyblock")
}
}
filesMatching("**/util_functions.sh") {
filter {
it.replace(
"#MAGISK_VERSION_STUB",
"MAGISK_VER='${Config.version}'\nMAGISK_VER_CODE=${Config.versionCode}"
)
}
filter<FixCrLfFilter>("eol" to FixCrLfFilter.CrLf.newInstance("lf"))
}
}
val syncResources = tasks.register("syncResources", Sync::class.java) {
dependsOn(syncAssets)
into("src/main/resources/META-INF/com/google/android") into("src/main/resources/META-INF/com/google/android")
from(rootProject.file("scripts/update_binary.sh")) { from(rootProject.file("scripts/update_binary.sh")) {
rename { "update-binary" } rename { "update-binary" }
@ -222,20 +194,42 @@ fun Project.setupApp() {
val variantCapped = name.capitalize(Locale.ROOT) val variantCapped = name.capitalize(Locale.ROOT)
val variantLowered = name.toLowerCase(Locale.ROOT) val variantLowered = name.toLowerCase(Locale.ROOT)
val copyStub = tasks.register("copy${variantCapped}StubApk", Copy::class.java) { val syncAssets = tasks.register("sync${variantCapped}Assets", Sync::class) {
dependsOn(syncResources) dependsOn(syncResources)
inputs.property("version", Config.version)
inputs.property("versionCode", Config.versionCode)
into("src/main/assets") into("src/main/assets")
from(rootProject.file("out/stub-${variantLowered}.apk")) { from(rootProject.file("scripts")) {
include("util_functions.sh", "boot_patch.sh", "addon.d.sh")
include("uninstaller.sh", "module_installer.sh")
}
from(rootProject.file("tools/bootctl"))
into("chromeos") {
from(rootProject.file("tools/futility"))
from(rootProject.file("tools/keys")) {
include("kernel_data_key.vbprivk", "kernel.keyblock")
}
}
val task = tasks.getByPath(":stub:package$variantCapped")
dependsOn(task)
val apk = task.outputs.files.asFileTree.filter {
it.name.endsWith(".apk")
}
from(apk) {
rename { "stub.apk" } rename { "stub.apk" }
} }
onlyIf { filesMatching("**/util_functions.sh") {
if (inputs.sourceFiles.files.size != 1) filter {
throw StopExecutionException("Please build stub first! (./build.py stub)") it.replace(
true "#MAGISK_VERSION_STUB",
"MAGISK_VER='${Config.version}'\nMAGISK_VER_CODE=${Config.versionCode}"
)
}
filter<FixCrLfFilter>("eol" to FixCrLfFilter.CrLf.newInstance("lf"))
} }
} }
preBuildProvider.get().dependsOn(copyStub) preBuildProvider.get().dependsOn(syncAssets)
val keysDir = rootProject.file("tools/keys") val keysDir = rootProject.file("tools/keys")
val outSrcDir = File(buildDir, "generated/source/keydata/$name") val outSrcDir = File(buildDir, "generated/source/keydata/$name")

View File

@ -1,5 +1,3 @@
#include <binaries.h>
#if defined(__arm__) #if defined(__arm__)
#include <armeabi-v7a_binaries.h> #include <armeabi-v7a_binaries.h>
#elif defined(__aarch64__) #elif defined(__aarch64__)

View File

@ -61,10 +61,6 @@ void restore_ramdisk_init() {
} }
} }
int dump_manager(const char *path, mode_t mode) {
return dump_bin(manager_xz, sizeof(manager_xz), path, mode);
}
int dump_preload(const char *path, mode_t mode) { int dump_preload(const char *path, mode_t mode) {
return dump_bin(init_ld_xz, sizeof(init_ld_xz), path, mode); return dump_bin(init_ld_xz, sizeof(init_ld_xz), path, mode);
} }
@ -87,12 +83,6 @@ int main(int argc, char *argv[]) {
if (name == "magisk"sv) if (name == "magisk"sv)
return magisk_proxy_main(argc, argv); return magisk_proxy_main(argc, argv);
if (argc > 1 && argv[1] == "-x"sv) {
if (argc > 2 && argv[2] == "manager"sv)
return dump_manager(argv[3], 0644);
return 1;
}
if (getpid() != 1) if (getpid() != 1)
return 1; return 1;

View File

@ -36,7 +36,6 @@ bool check_two_stage();
void setup_klog(); void setup_klog();
const char *backup_init(); const char *backup_init();
void restore_ramdisk_init(); void restore_ramdisk_init();
int dump_manager(const char *path, mode_t mode);
int dump_preload(const char *path, mode_t mode); int dump_preload(const char *path, mode_t mode);
/*************** /***************

View File

@ -151,6 +151,7 @@ static void patch_socket_name(const char *path) {
static void extract_files(bool sbin) { static void extract_files(bool sbin) {
const char *m32 = sbin ? "/sbin/magisk32.xz" : "magisk32.xz"; const char *m32 = sbin ? "/sbin/magisk32.xz" : "magisk32.xz";
const char *m64 = sbin ? "/sbin/magisk64.xz" : "magisk64.xz"; const char *m64 = sbin ? "/sbin/magisk64.xz" : "magisk64.xz";
const char *stub_xz = sbin ? "/sbin/stub.xz" : "stub.xz";
if (access(m32, F_OK) == 0) { if (access(m32, F_OK) == 0) {
auto magisk = mmap_data(m32); auto magisk = mmap_data(m32);
@ -172,7 +173,13 @@ static void extract_files(bool sbin) {
xsymlink("./magisk32", "magisk"); xsymlink("./magisk32", "magisk");
} }
dump_manager("stub.apk", 0); {
auto stub = mmap_data(stub_xz);
unlink(stub_xz);
int fd = xopen("stub.apk", O_WRONLY | O_CREAT, 0);
unxz(fd, stub.buf, stub.sz);
close(fd);
}
} }
#define ROOTMIR MIRRDIR "/system_root" #define ROOTMIR MIRRDIR "/system_root"

View File

@ -120,6 +120,7 @@ fi
# Magisk stuff # Magisk stuff
mkdir -p $MAGISKBIN 2>/dev/null mkdir -p $MAGISKBIN 2>/dev/null
unzip -oj magisk.apk 'assets/*.sh' -d $MAGISKBIN unzip -oj magisk.apk 'assets/*.sh' -d $MAGISKBIN
unzip -oj magisk.apk 'assets/stub.apk' -d $MAGISKTMP
mkdir $NVBASE/modules 2>/dev/null mkdir $NVBASE/modules 2>/dev/null
mkdir $POSTFSDATAD 2>/dev/null mkdir $POSTFSDATAD 2>/dev/null
mkdir $SERVICED 2>/dev/null mkdir $SERVICED 2>/dev/null
@ -143,8 +144,6 @@ ln -s ./magisk $MAGISKTMP/resetprop
ln -s ./magisk $MAGISKTMP/magiskhide ln -s ./magisk $MAGISKTMP/magiskhide
ln -s ./magiskpolicy $MAGISKTMP/supolicy ln -s ./magiskpolicy $MAGISKTMP/supolicy
./magiskinit -x manager $MAGISKTMP/stub.apk
mkdir -p $MAGISKTMP/.magisk/mirror mkdir -p $MAGISKTMP/.magisk/mirror
mkdir $MAGISKTMP/.magisk/block mkdir $MAGISKTMP/.magisk/block
touch $MAGISKTMP/.magisk/config touch $MAGISKTMP/.magisk/config

View File

@ -43,7 +43,7 @@ if [ -z "$FIRST_STAGE" ]; then
fi fi
# Extract files from APK # Extract files from APK
unzip -oj magisk.apk 'assets/util_functions.sh' unzip -oj magisk.apk 'assets/util_functions.sh' 'assets/stub.apk'
. ./util_functions.sh . ./util_functions.sh
api_level_arch_detect api_level_arch_detect
@ -65,6 +65,7 @@ touch config
./magiskboot compress=xz magisk32 magisk32.xz ./magiskboot compress=xz magisk32 magisk32.xz
./magiskboot compress=xz magisk64 magisk64.xz ./magiskboot compress=xz magisk64 magisk64.xz
./magiskboot compress=xz stub.apk stub.xz
export KEEPVERITY=false export KEEPVERITY=false
export KEEPFORCEENCRYPT=true export KEEPFORCEENCRYPT=true
@ -75,10 +76,11 @@ export KEEPFORCEENCRYPT=true
"mkdir 0750 overlay.d/sbin" \ "mkdir 0750 overlay.d/sbin" \
"add 0644 overlay.d/sbin/magisk32.xz magisk32.xz" \ "add 0644 overlay.d/sbin/magisk32.xz magisk32.xz" \
"add 0644 overlay.d/sbin/magisk64.xz magisk64.xz" \ "add 0644 overlay.d/sbin/magisk64.xz magisk64.xz" \
"add 0644 overlay.d/sbin/stub.xz stub.xz" \
"patch" \ "patch" \
"backup ramdisk.cpio.orig" \ "backup ramdisk.cpio.orig" \
"mkdir 000 .backup" \ "mkdir 000 .backup" \
"add 000 .backup/.magisk config" "add 000 .backup/.magisk config"
rm -f ramdisk.cpio.orig config magisk*.xz rm -f ramdisk.cpio.orig config magisk*.xz stub.xz
./magiskboot compress=gzip ramdisk.cpio ramdisk.cpio.gz ./magiskboot compress=gzip ramdisk.cpio ramdisk.cpio.gz

View File

@ -164,6 +164,7 @@ if [ -f magisk64 ]; then
./magiskboot compress=xz magisk64 magisk64.xz ./magiskboot compress=xz magisk64 magisk64.xz
unset SKIP64 unset SKIP64
fi fi
./magiskboot compress=xz stub.apk stub.xz
./magiskboot cpio ramdisk.cpio \ ./magiskboot cpio ramdisk.cpio \
"add 0750 $INIT magiskinit" \ "add 0750 $INIT magiskinit" \
@ -171,12 +172,13 @@ fi
"mkdir 0750 overlay.d/sbin" \ "mkdir 0750 overlay.d/sbin" \
"$SKIP32 add 0644 overlay.d/sbin/magisk32.xz magisk32.xz" \ "$SKIP32 add 0644 overlay.d/sbin/magisk32.xz magisk32.xz" \
"$SKIP64 add 0644 overlay.d/sbin/magisk64.xz magisk64.xz" \ "$SKIP64 add 0644 overlay.d/sbin/magisk64.xz magisk64.xz" \
"add 0644 overlay.d/sbin/stub.xz stub.xz" \
"patch" \ "patch" \
"backup ramdisk.cpio.orig" \ "backup ramdisk.cpio.orig" \
"mkdir 000 .backup" \ "mkdir 000 .backup" \
"add 000 .backup/.magisk config" "add 000 .backup/.magisk config"
rm -f ramdisk.cpio.orig config magisk*.xz rm -f ramdisk.cpio.orig config magisk*.xz stub.xz
################# #################
# Binary Patches # Binary Patches