mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-04 19:10:47 -05:00
app: add WholeFilePatcher and ImageExtractor
Define WholeFilePatcher and ImageExtractor interfaces for whole-file patching and single-image extraction pipelines. Have TarProcessor implement WholeFilePatcher, and OtaPayloadExtractor and ZipExtractor implement ImageExtractor. In MagiskInstallImpl, streamline terminology by renaming srcBoot to targetImage, useRootDir to useRootFs, and processArchive to processWholeFile. Assisted-by: Gemini 3.8 Flash
This commit is contained in:
@@ -40,16 +40,29 @@ import java.security.SecureRandom
|
|||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
import java.util.concurrent.atomic.AtomicBoolean
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
|
|
||||||
|
interface WholeFilePatcher {
|
||||||
|
@Throws(IOException::class)
|
||||||
|
suspend fun start(channel: DataChannel): ExtendedFile
|
||||||
|
|
||||||
|
@Throws(IOException::class)
|
||||||
|
suspend fun finish(patched: ExtendedFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ImageExtractor {
|
||||||
|
@Throws(IOException::class)
|
||||||
|
suspend fun extract(channel: DataChannel)
|
||||||
|
}
|
||||||
|
|
||||||
abstract class MagiskInstallImpl protected constructor(
|
abstract class MagiskInstallImpl protected constructor(
|
||||||
protected val console: MutableList<String>,
|
protected val console: MutableList<String>,
|
||||||
private val logs: MutableList<String>
|
private val logs: MutableList<String>
|
||||||
) {
|
) {
|
||||||
|
|
||||||
private lateinit var installDir: ExtendedFile
|
private lateinit var installDir: ExtendedFile
|
||||||
private lateinit var srcBoot: ExtendedFile
|
private lateinit var targetImage: ExtendedFile
|
||||||
|
|
||||||
private val shell = Shell.getShell()
|
private val shell = Shell.getShell()
|
||||||
private val useRootDir = shell.isRoot && Info.noDataExec
|
private val useRootFs = shell.isRoot && Info.noDataExec
|
||||||
protected val context get() = ServiceLocator.deContext
|
protected val context get() = ServiceLocator.deContext
|
||||||
|
|
||||||
private val rootFS get() = RootUtils.fs
|
private val rootFS get() = RootUtils.fs
|
||||||
@@ -82,7 +95,7 @@ abstract class MagiskInstallImpl protected constructor(
|
|||||||
console.add("! Unable to detect target image")
|
console.add("! Unable to detect target image")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
srcBoot = rootFS.getFile(bootPath)
|
targetImage = rootFS.getFile(bootPath)
|
||||||
console.add("- Target image: $bootPath")
|
console.add("- Target image: $bootPath")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -169,7 +182,7 @@ abstract class MagiskInstallImpl protected constructor(
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if (useRootDir) {
|
if (useRootFs) {
|
||||||
// Move everything to tmpfs to workaround Samsung bullshit
|
// Move everything to tmpfs to workaround Samsung bullshit
|
||||||
rootFS.getFile(Const.TMPDIR).also {
|
rootFS.getFile(Const.TMPDIR).also {
|
||||||
arrayOf(
|
arrayOf(
|
||||||
@@ -223,7 +236,7 @@ abstract class MagiskInstallImpl protected constructor(
|
|||||||
val tarMagic = head.copyOfRange(257, 262)
|
val tarMagic = head.copyOfRange(257, 262)
|
||||||
|
|
||||||
return if (tarMagic.contentEquals("ustar".toByteArray())) {
|
return if (tarMagic.contentEquals("ustar".toByteArray())) {
|
||||||
processArchive(input)
|
processWholeFile(input)
|
||||||
} else {
|
} else {
|
||||||
extractAndProcessImage(input, magic)
|
extractAndProcessImage(input, magic)
|
||||||
}
|
}
|
||||||
@@ -237,14 +250,14 @@ abstract class MagiskInstallImpl protected constructor(
|
|||||||
|
|
||||||
// Patch the boot image in a tar archive, and output a new tar archive
|
// Patch the boot image in a tar archive, and output a new tar archive
|
||||||
// with all the original entries and the patched boot image
|
// with all the original entries and the patched boot image
|
||||||
private suspend fun processArchive(input: DataChannel): Boolean {
|
private suspend fun processWholeFile(input: DataChannel): Boolean {
|
||||||
val outFile = MediaStoreUtils.getFile("$destName.tar")
|
val outFile = MediaStoreUtils.getFile("$destName.tar")
|
||||||
val outStream = outFile.uri.outputStream().buffered(1024 * 1024)
|
val outStream = outFile.uri.outputStream().buffered(1024 * 1024)
|
||||||
val tar = TarProcessor(installDir, outStream, console, logs)
|
val tar = TarProcessor(installDir, outStream, console, logs)
|
||||||
|
|
||||||
// Process input file
|
// Process input file
|
||||||
try {
|
try {
|
||||||
srcBoot = tar.consume(input)
|
targetImage = tar.start(input)
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
runCatching { outStream.close() }
|
runCatching { outStream.close() }
|
||||||
outFile.delete()
|
outFile.delete()
|
||||||
@@ -282,15 +295,15 @@ abstract class MagiskInstallImpl protected constructor(
|
|||||||
// Extract the boot image from the input, and output the patched boot image
|
// Extract the boot image from the input, and output the patched boot image
|
||||||
private suspend fun extractAndProcessImage(input: DataChannel, magic: ByteArray): Boolean {
|
private suspend fun extractAndProcessImage(input: DataChannel, magic: ByteArray): Boolean {
|
||||||
// Process input file
|
// Process input file
|
||||||
srcBoot = installDir.getChildFile("boot.img")
|
targetImage = installDir.getChildFile("boot.img")
|
||||||
try {
|
try {
|
||||||
if (magic.contentEquals("CrAU".toByteArray())) {
|
if (magic.contentEquals("CrAU".toByteArray())) {
|
||||||
OtaPayloadExtractor(srcBoot, console, logs).consume(input)
|
OtaPayloadExtractor(targetImage, console, logs).extract(input)
|
||||||
} else if (magic.contentEquals("PK\u0003\u0004".toByteArray())) {
|
} else if (magic.contentEquals("PK\u0003\u0004".toByteArray())) {
|
||||||
ZipExtractor(srcBoot, console, logs).consume(input)
|
ZipExtractor(targetImage, console, logs).extract(input)
|
||||||
} else {
|
} else {
|
||||||
console.add("- Copying image to cache")
|
console.add("- Copying image to cache")
|
||||||
input.stream().use { it.copyAndCloseOut(srcBoot.newOutputStream()) }
|
input.stream().use { it.copyAndCloseOut(targetImage.newOutputStream()) }
|
||||||
}
|
}
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
console.add("! Process error")
|
console.add("! Process error")
|
||||||
@@ -328,7 +341,7 @@ abstract class MagiskInstallImpl protected constructor(
|
|||||||
console.add("****************************")
|
console.add("****************************")
|
||||||
|
|
||||||
// Fix up binaries
|
// Fix up binaries
|
||||||
srcBoot.delete()
|
targetImage.delete()
|
||||||
"cp_readlink $installDir".sh()
|
"cp_readlink $installDir".sh()
|
||||||
|
|
||||||
return true
|
return true
|
||||||
@@ -336,7 +349,7 @@ abstract class MagiskInstallImpl protected constructor(
|
|||||||
|
|
||||||
private fun patchBoot(): Boolean {
|
private fun patchBoot(): Boolean {
|
||||||
val newBoot = installDir.getChildFile("new-boot.img")
|
val newBoot = installDir.getChildFile("new-boot.img")
|
||||||
if (!useRootDir) {
|
if (!useRootFs) {
|
||||||
// Create output files before hand
|
// Create output files before hand
|
||||||
newBoot.createNewFile()
|
newBoot.createNewFile()
|
||||||
File(installDir, "stock_boot.img").createNewFile()
|
File(installDir, "stock_boot.img").createNewFile()
|
||||||
@@ -349,7 +362,7 @@ abstract class MagiskInstallImpl protected constructor(
|
|||||||
"PATCHVBMETAFLAG=${Info.patchBootVbmeta} " +
|
"PATCHVBMETAFLAG=${Info.patchBootVbmeta} " +
|
||||||
"RECOVERYMODE=${Config.recovery} " +
|
"RECOVERYMODE=${Config.recovery} " +
|
||||||
"LEGACYSAR=${Info.legacySAR} " +
|
"LEGACYSAR=${Info.legacySAR} " +
|
||||||
"sh boot_patch.sh $srcBoot")
|
"sh boot_patch.sh $targetImage")
|
||||||
val isSuccess = cmds.sh().isSuccess
|
val isSuccess = cmds.sh().isSuccess
|
||||||
|
|
||||||
shell.newJob().add("./magiskboot cleanup", "cd /").exec()
|
shell.newJob().add("./magiskboot cleanup", "cd /").exec()
|
||||||
@@ -357,7 +370,7 @@ abstract class MagiskInstallImpl protected constructor(
|
|||||||
return isSuccess
|
return isSuccess
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun flashBoot() = "direct_install $installDir $srcBoot".sh().isSuccess
|
private fun flashBoot() = "direct_install $installDir $targetImage".sh().isSuccess
|
||||||
|
|
||||||
private fun postOTA(): Boolean {
|
private fun postOTA(): Boolean {
|
||||||
"post_ota".sh()
|
"post_ota".sh()
|
||||||
@@ -369,11 +382,9 @@ abstract class MagiskInstallImpl protected constructor(
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Array<String>.eq() = shell.newJob().add(*this).to(console, logs).enqueue()
|
|
||||||
private fun String.sh() = shell.newJob().add(this).to(console, logs).exec()
|
private fun String.sh() = shell.newJob().add(this).to(console, logs).exec()
|
||||||
private fun Array<String>.sh() = shell.newJob().add(*this).to(console, logs).exec()
|
private fun Array<String>.sh() = shell.newJob().add(*this).to(console, logs).exec()
|
||||||
private fun String.fsh() = ShellUtils.fastCmd(shell, this)
|
private fun String.fsh() = ShellUtils.fastCmd(shell, this)
|
||||||
private fun Array<String>.fsh() = ShellUtils.fastCmd(shell, *this)
|
|
||||||
|
|
||||||
protected suspend fun patchFile(file: Uri) = extractFiles() && processFile(file)
|
protected suspend fun patchFile(file: Uri) = extractFiles() && processFile(file)
|
||||||
|
|
||||||
@@ -386,7 +397,7 @@ abstract class MagiskInstallImpl protected constructor(
|
|||||||
|
|
||||||
protected suspend fun fixEnv() = extractFiles() && "fix_env $installDir".sh().isSuccess
|
protected suspend fun fixEnv() = extractFiles() && "fix_env $installDir".sh().isSuccess
|
||||||
|
|
||||||
protected fun restore() = findImage() && "restore_imgs $srcBoot".sh().isSuccess
|
protected fun restore() = findImage() && "restore_imgs $targetImage".sh().isSuccess
|
||||||
|
|
||||||
protected fun uninstall() = "run_uninstaller $AppApkPath".sh().isSuccess
|
protected fun uninstall() = "run_uninstaller $AppApkPath".sh().isSuccess
|
||||||
|
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ class OtaPayloadExtractor(
|
|||||||
private val outFile: File,
|
private val outFile: File,
|
||||||
private val console: MutableList<String>,
|
private val console: MutableList<String>,
|
||||||
private val logs: MutableList<String>,
|
private val logs: MutableList<String>,
|
||||||
) {
|
) : ImageExtractor {
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
fun consume(channel: DataChannel) {
|
override suspend fun extract(channel: DataChannel) {
|
||||||
channel.use {
|
channel.use {
|
||||||
val manifest = readPayloadHeader(it)
|
val manifest = readPayloadHeader(it)
|
||||||
val dataBase = channel.position()
|
val dataBase = channel.position()
|
||||||
|
|||||||
@@ -20,19 +20,19 @@ import java.nio.ByteBuffer
|
|||||||
* Patch boot images in tar firmware packages (e.g. Samsung AP tar files).
|
* Patch boot images in tar firmware packages (e.g. Samsung AP tar files).
|
||||||
*
|
*
|
||||||
* A single-use, two-phase processor; the caller patches the boot image in between:
|
* A single-use, two-phase processor; the caller patches the boot image in between:
|
||||||
* 1. [consume]: extract boot images from the input tar into [installDir], and copy all
|
* 1. [start]: extract boot images from the input tar into [workingDir], and copy all
|
||||||
* other entries to the output tar. Returns the image to be patched.
|
* other entries to the output tar. Returns the image to be patched.
|
||||||
* 2. [finish]: write the patched image to the output tar and finalize the archive.
|
* 2. [finish]: write the patched image to the output tar and finalize the archive.
|
||||||
*
|
*
|
||||||
* [consume] closes the input channel. The caller owns the output stream and is
|
* [start] closes the input channel. The caller owns the output stream and is
|
||||||
* responsible for closing it.
|
* responsible for closing it.
|
||||||
*/
|
*/
|
||||||
class TarProcessor(
|
class TarProcessor(
|
||||||
private val installDir: ExtendedFile,
|
private val workingDir: ExtendedFile,
|
||||||
output: OutputStream,
|
output: OutputStream,
|
||||||
private val console: MutableList<String>,
|
private val console: MutableList<String>,
|
||||||
private val logs: MutableList<String>,
|
private val logs: MutableList<String>,
|
||||||
) {
|
) : WholeFilePatcher {
|
||||||
class NoBootException : IOException()
|
class NoBootException : IOException()
|
||||||
|
|
||||||
private val tarOut = TarArchiveOutputStream(output).apply {
|
private val tarOut = TarArchiveOutputStream(output).apply {
|
||||||
@@ -44,7 +44,7 @@ class TarProcessor(
|
|||||||
private lateinit var target: BootItem
|
private lateinit var target: BootItem
|
||||||
|
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
suspend fun consume(channel: DataChannel): ExtendedFile = channel.use {
|
override suspend fun start(channel: DataChannel): ExtendedFile = channel.use {
|
||||||
val tarIn = TarArchiveInputStream(it.stream().buffered(1024 * 1024))
|
val tarIn = TarArchiveInputStream(it.stream().buffered(1024 * 1024))
|
||||||
tarIn.use { processEntries(tarIn) }
|
tarIn.use { processEntries(tarIn) }
|
||||||
}
|
}
|
||||||
@@ -131,7 +131,7 @@ class TarProcessor(
|
|||||||
if (boot != null) {
|
if (boot != null) {
|
||||||
// Repack boot image to prevent auto restore
|
// Repack boot image to prevent auto restore
|
||||||
Shell.cmd(
|
Shell.cmd(
|
||||||
"cd $installDir",
|
"cd $workingDir",
|
||||||
"chmod -R 755 .",
|
"chmod -R 755 .",
|
||||||
"./magiskboot unpack boot.img",
|
"./magiskboot unpack boot.img",
|
||||||
"./magiskboot repack boot.img",
|
"./magiskboot repack boot.img",
|
||||||
@@ -155,7 +155,7 @@ class TarProcessor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
suspend fun finish(patched: ExtendedFile) {
|
override suspend fun finish(patched: ExtendedFile) {
|
||||||
target.write(patched)
|
target.write(patched)
|
||||||
tarOut.finish()
|
tarOut.finish()
|
||||||
}
|
}
|
||||||
@@ -168,7 +168,7 @@ class TarProcessor(
|
|||||||
|
|
||||||
private inner class BootItem(private val entry: TarArchiveEntry) {
|
private inner class BootItem(private val entry: TarArchiveEntry) {
|
||||||
val name = entry.name.replace(".lz4", "")
|
val name = entry.name.replace(".lz4", "")
|
||||||
val file: ExtendedFile = installDir.getChildFile(name)
|
val file: ExtendedFile = workingDir.getChildFile(name)
|
||||||
|
|
||||||
// Write the image to the output tar, preserving the original entry's metadata
|
// Write the image to the output tar, preserving the original entry's metadata
|
||||||
suspend fun write(image: ExtendedFile = file) {
|
suspend fun write(image: ExtendedFile = file) {
|
||||||
|
|||||||
@@ -16,9 +16,9 @@ class ZipExtractor(
|
|||||||
private val outFile: File,
|
private val outFile: File,
|
||||||
private val console: MutableList<String>,
|
private val console: MutableList<String>,
|
||||||
private val logs: MutableList<String>,
|
private val logs: MutableList<String>,
|
||||||
) {
|
) : ImageExtractor {
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
fun consume(channel: DataChannel) {
|
override suspend fun extract(channel: DataChannel) {
|
||||||
ZipFile.builder()
|
ZipFile.builder()
|
||||||
.setSeekableByteChannel(channel)
|
.setSeekableByteChannel(channel)
|
||||||
.setIgnoreLocalFileHeader(true)
|
.setIgnoreLocalFileHeader(true)
|
||||||
@@ -49,7 +49,7 @@ class ZipExtractor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
private fun extractFromOTAPackage(
|
private suspend fun extractFromOTAPackage(
|
||||||
payload: ZipArchiveEntry,
|
payload: ZipArchiveEntry,
|
||||||
channel: DataChannel,
|
channel: DataChannel,
|
||||||
) {
|
) {
|
||||||
@@ -58,7 +58,7 @@ class ZipExtractor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
OtaPayloadExtractor(outFile, console, logs)
|
OtaPayloadExtractor(outFile, console, logs)
|
||||||
.consume(channel.slice(payload.dataOffset, payload.size))
|
.extract(channel.slice(payload.dataOffset, payload.size))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
|
|||||||
Reference in New Issue
Block a user