Fix vbmeta.img tar patching

This commit is contained in:
topjohnwu 2024-07-01 03:20:09 -07:00
parent c194168d9b
commit 47e918bc92

View File

@ -226,10 +226,10 @@ abstract class MagiskInstallImpl protected constructor(
tarOut: TarArchiveOutputStream
): BootItem {
console.add("- Processing tar file")
lateinit var entry: TarArchiveEntry
var entry: TarArchiveEntry? = tarIn.nextEntry
fun decompressedStream(): InputStream {
val stream = if (entry.name.endsWith(".lz4"))
fun TarArchiveEntry.decompressedStream(): InputStream {
val stream = if (name.endsWith(".lz4"))
FramedLZ4CompressorInputStream(tarIn, true) else tarIn
return NoAvailableStream(stream)
}
@ -238,9 +238,7 @@ abstract class MagiskInstallImpl protected constructor(
var initBoot: BootItem? = null
var recovery: BootItem? = null
while (true) {
entry = tarIn.nextEntry ?: break
while (entry != null) {
val bootItem: BootItem?
if (entry.name.startsWith("boot.img")) {
bootItem = BootItem(entry)
@ -257,9 +255,9 @@ abstract class MagiskInstallImpl protected constructor(
if (bootItem != null) {
console.add("-- Extracting: ${bootItem.name}")
decompressedStream().copyAndCloseOut(bootItem.file.newOutputStream())
entry.decompressedStream().copyAndCloseOut(bootItem.file.newOutputStream())
} else if (entry.name.contains("vbmeta.img")) {
val rawData = decompressedStream().readBytes()
val rawData = entry.decompressedStream().readBytes()
// Valid vbmeta.img should be at least 256 bytes
if (rawData.size < 256)
continue
@ -274,23 +272,28 @@ abstract class MagiskInstallImpl protected constructor(
// AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED
ByteBuffer.wrap(rawData).putInt(120, 3)
// Fetch the next entry first before modifying current entry
val vbmeta = entry
entry = tarIn.nextEntry
// Update entry with new information
entry.name = name
entry.size = rawData.size.toLong()
vbmeta.name = name
vbmeta.size = rawData.size.toLong()
// Write output
tarOut.putArchiveEntry(entry)
tarOut.putArchiveEntry(vbmeta)
tarOut.write(rawData)
tarOut.closeArchiveEntry()
continue
} else if (entry.name.contains("userdata.img")) {
console.add("-- Skipping : ${entry.name}")
continue
} else {
console.add("-- Copying : ${entry.name}")
tarOut.putArchiveEntry(entry)
tarIn.copyAll(tarOut, bufferSize = 1024 * 1024)
tarOut.closeArchiveEntry()
}
entry = tarIn.nextEntry ?: break
}
// Patch priority: recovery > init_boot > boot