From c834f5ff27fda6f4b30706c6d63f54af20ba94d6 Mon Sep 17 00:00:00 2001 From: capntrips <31455058+capntrips@users.noreply.github.com> Date: Thu, 23 Feb 2023 12:51:31 -0600 Subject: [PATCH] run update-binary in a tmpfs chroot --- app/src/main/assets/flash_ak3.sh | 32 +++++++++++++++++++ .../capntrips/kernelflasher/MainActivity.kt | 1 + .../ui/screens/slot/SlotViewModel.kt | 30 +++++------------ 3 files changed, 41 insertions(+), 22 deletions(-) create mode 100644 app/src/main/assets/flash_ak3.sh diff --git a/app/src/main/assets/flash_ak3.sh b/app/src/main/assets/flash_ak3.sh new file mode 100644 index 0000000..2bae07a --- /dev/null +++ b/app/src/main/assets/flash_ak3.sh @@ -0,0 +1,32 @@ +#!/system/bin/sh + +## setup for testing: +unzip -p $Z tools*/busybox > $F/busybox; +unzip -p $Z META-INF/com/google/android/update-binary > $F/update-binary; +## + +chmod 755 $F/busybox; +$F/busybox chmod 755 $F/update-binary; +$F/busybox chown root:root $F/busybox $F/update-binary; + +TMP=$F/tmp; + +$F/busybox umount $TMP 2>/dev/null; +$F/busybox rm -rf $TMP 2>/dev/null; +$F/busybox mkdir -p $TMP; + +$F/busybox mount -t tmpfs -o noatime tmpfs $TMP; +$F/busybox mount | $F/busybox grep -q " $TMP " || exit 1; + +# update-binary +AKHOME=$TMP/anykernel $F/busybox ash $F/update-binary 3 1 "$Z"; +RC=$?; + +$F/busybox umount $TMP; +$F/busybox rm -rf $TMP; +$F/busybox mount -o ro,remount -t auto /; +$F/busybox rm -f $F/update-binary $F/busybox; + +# work around libsu not cleanly accepting return or exit as last line +safereturn() { return $RC; } +safereturn; diff --git a/app/src/main/java/com/github/capntrips/kernelflasher/MainActivity.kt b/app/src/main/java/com/github/capntrips/kernelflasher/MainActivity.kt index 6dab1b6..92c14b3 100644 --- a/app/src/main/java/com/github/capntrips/kernelflasher/MainActivity.kt +++ b/app/src/main/java/com/github/capntrips/kernelflasher/MainActivity.kt @@ -154,6 +154,7 @@ class MainActivity : ComponentActivity() { copyAsset("lptools_static") copyAsset("httools_static") copyAsset("magiskboot") // version: Magisk 25.2 stable release + copyAsset("flash_ak3.sh") } catch (e: Exception) { Log.e(TAG, e.message, e) setContent { diff --git a/app/src/main/java/com/github/capntrips/kernelflasher/ui/screens/slot/SlotViewModel.kt b/app/src/main/java/com/github/capntrips/kernelflasher/ui/screens/slot/SlotViewModel.kt index 90d154d..4b4ff18 100644 --- a/app/src/main/java/com/github/capntrips/kernelflasher/ui/screens/slot/SlotViewModel.kt +++ b/app/src/main/java/com/github/capntrips/kernelflasher/ui/screens/slot/SlotViewModel.kt @@ -196,10 +196,6 @@ class SlotViewModel( zip.delete() } } - val akHome = File(context.filesDir, "akhome") - if (akHome.exists()) { - Shell.cmd("rm -r $akHome").exec() - } } @Suppress("FunctionName") @@ -509,29 +505,19 @@ class SlotViewModel( if (!isActive) { resetSlot() } - val zip = File(context.filesDir, flashFilename!!) + val zip = File(context.filesDir.canonicalPath, flashFilename!!) _checkZip(context, zip) - val akHome = File(context.filesDir, "akhome") try { if (zip.exists()) { - akHome.mkdir() _wasFlashSuccess.value = false - if (akHome.exists()) { - val updateBinary = File(akHome, "update-binary") - Shell.cmd("unzip -p \"$zip\" META-INF/com/google/android/update-binary > $akHome/update-binary").exec() - if (updateBinary.exists()) { - val result = Shell.Builder.create().setFlags(Shell.FLAG_MOUNT_MASTER or Shell.FLAG_REDIRECT_STDERR).build().newJob().add("AKHOME=$akHome /system/bin/sh $akHome/update-binary 3 1 \"$zip\"").to(flashOutput).exec() - if (result.isSuccess) { - log(context, "Kernel flashed successfully") - _wasFlashSuccess.value = true - } else { - log(context, "Failed to flash zip", shouldThrow = false) - } - } else { - log(context, "Failed to extract update-binary", shouldThrow = true) - } + val files = File(context.filesDir.canonicalPath) + val flashScript = File(files, "flash_ak3.sh") + val result = Shell.Builder.create().setFlags(Shell.FLAG_MOUNT_MASTER or Shell.FLAG_REDIRECT_STDERR).build().newJob().add("F=$files Z=\"$zip\" /system/bin/sh $flashScript").to(flashOutput).exec() + if (result.isSuccess) { + log(context, "Kernel flashed successfully") + _wasFlashSuccess.value = true } else { - log(context, "Failed to create temporary folder", shouldThrow = true) + log(context, "Failed to flash zip", shouldThrow = false) } clearTmp(context) } else {