Skip to content

Commit

Permalink
run update-binary in a tmpfs chroot
Browse files Browse the repository at this point in the history
  • Loading branch information
capntrips committed Mar 21, 2023
1 parent 762583c commit c834f5f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
32 changes: 32 additions & 0 deletions 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 <RECOVERY_API_VERSION> <OUTFD> <ZIPFILE>
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;
Expand Up @@ -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 {
Expand Down
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit c834f5f

Please sign in to comment.