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