From fed5daeebadf0db3398985b9f92c58d348cf8e3c Mon Sep 17 00:00:00 2001 From: cfig Date: Thu, 15 Oct 2020 00:15:11 +0800 Subject: [PATCH] extensive lz4 ramdisk support --- bbootimg/src/main/kotlin/bootimg/Common.kt | 9 +++++---- bbootimg/src/main/kotlin/bootimg/v2/BootV2.kt | 8 ++++++-- bbootimg/src/main/kotlin/bootimg/v3/BootV3.kt | 19 ++++++++----------- .../src/main/kotlin/bootimg/v3/VendorBoot.kt | 10 ++++++---- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/bbootimg/src/main/kotlin/bootimg/Common.kt b/bbootimg/src/main/kotlin/bootimg/Common.kt index 44d60e6..1cfd101 100644 --- a/bbootimg/src/main/kotlin/bootimg/Common.kt +++ b/bbootimg/src/main/kotlin/bootimg/Common.kt @@ -111,19 +111,20 @@ class Common { Helper.extractFile(s.srcFile, s.dumpFile, s.offset.toLong(), s.length) when { Helper.isGZ(s.dumpFile) -> { - Helper.unGnuzipFile(s.dumpFile, s.dumpFile.removeSuffix(".gz")) + File(s.dumpFile).renameTo(File(s.dumpFile + ".gz")) + Helper.unGnuzipFile(s.dumpFile + ".gz", s.dumpFile) } Helper.isLZ4(s.dumpFile) -> { log.info("ramdisk is compressed lz4") - Helper.decompressLZ4(s.dumpFile, s.dumpFile.removeSuffix(".gz")) - File(s.dumpFile).renameTo(File(s.dumpFile.replace(".gz", ".lz4"))) + File(s.dumpFile).renameTo(File(s.dumpFile + ".lz4")) + Helper.decompressLZ4(s.dumpFile + ".lz4", s.dumpFile) ret = "lz4" } else -> { throw IllegalArgumentException("ramdisk is in unknown format") } } - unpackRamdisk(s.dumpFile.removeSuffix(".gz"), root) + unpackRamdisk(s.dumpFile, root) return ret } diff --git a/bbootimg/src/main/kotlin/bootimg/v2/BootV2.kt b/bbootimg/src/main/kotlin/bootimg/v2/BootV2.kt index e6c6b39..8ba8814 100644 --- a/bbootimg/src/main/kotlin/bootimg/v2/BootV2.kt +++ b/bbootimg/src/main/kotlin/bootimg/v2/BootV2.kt @@ -6,6 +6,7 @@ import cfig.bootimg.Common import cfig.bootimg.Common.Companion.deleleIfExists import cfig.bootimg.Common.Slice import cfig.bootimg.Signer +import cfig.bootimg.v3.BootV3 import cfig.packable.VBMetaParser import com.fasterxml.jackson.databind.ObjectMapper import de.vandermeer.asciitable.AsciiTable @@ -94,7 +95,7 @@ data class BootV2( theRamdisk.loadOffset = bh2.ramdiskOffset theRamdisk.position = ret.getRamdiskPosition() if (bh2.ramdiskLength > 0) { - theRamdisk.file = "${workDir}ramdisk.img.gz" + theRamdisk.file = "${workDir}ramdisk.img" } } if (bh2.secondBootloaderLength > 0) { @@ -168,8 +169,11 @@ data class BootV2( Common.dumpKernel(Slice(info.output, kernel.position.toInt(), kernel.size, kernel.file!!)) //ramdisk if (this.ramdisk.size > 0) { - Common.dumpRamdisk(Slice(info.output, ramdisk.position.toInt(), ramdisk.size, ramdisk.file!!), + val fmt = Common.dumpRamdisk(Slice(info.output, ramdisk.position.toInt(), ramdisk.size, ramdisk.file!!), "${workDir}root") + this.ramdisk.file = this.ramdisk.file!! + ".$fmt" + //dump info again + ObjectMapper().writerWithDefaultPrettyPrinter().writeValue(File(workDir + this.info.json), this) } //second bootloader secondBootloader?.let { diff --git a/bbootimg/src/main/kotlin/bootimg/v3/BootV3.kt b/bbootimg/src/main/kotlin/bootimg/v3/BootV3.kt index 31b71fb..cfda15d 100644 --- a/bbootimg/src/main/kotlin/bootimg/v3/BootV3.kt +++ b/bbootimg/src/main/kotlin/bootimg/v3/BootV3.kt @@ -45,7 +45,7 @@ data class BootV3(var info: MiscInfo = MiscInfo(), ret.kernel.size = header.kernelSize ret.kernel.position = BootHeaderV3.pageSize //ramdisk - ret.ramdisk.file = workDir + "ramdisk.img.gz" + ret.ramdisk.file = workDir + "ramdisk.img" ret.ramdisk.size = header.ramdiskSize ret.ramdisk.position = ret.kernel.position + header.kernelSize + getPaddingSize(header.kernelSize, BootHeaderV3.pageSize) @@ -82,7 +82,7 @@ data class BootV3(var info: MiscInfo = MiscInfo(), } fun pack(): BootV3 { - if (File( this.ramdisk.file).exists() && !File(workDir + "root").exists()) { + if (File(this.ramdisk.file).exists() && !File(workDir + "root").exists()) { //do nothing if we have ramdisk.img.gz but no /root log.warn("Use prebuilt ramdisk file: ${this.ramdisk.file}") } else { @@ -91,7 +91,7 @@ data class BootV3(var info: MiscInfo = MiscInfo(), C.packRootfs("$workDir/root", this.ramdisk.file, parseOsMajor()) } this.kernel.size = File(this.kernel.file).length().toInt() - this.ramdisk.size = File( this.ramdisk.file).length().toInt() + this.ramdisk.size = File(this.ramdisk.file).length().toInt() //header FileOutputStream(this.info.output + ".clear", false).use { fos -> @@ -147,14 +147,11 @@ data class BootV3(var info: MiscInfo = MiscInfo(), //kernel C.dumpKernel(C.Slice(info.output, kernel.position, kernel.size, kernel.file)) //ramdisk - val fmt = C.dumpRamdisk(C.Slice(info.output, ramdisk.position, ramdisk.size, ramdisk.file), "${workDir}root") - if (fmt in listOf("xz", "lzma", "bz2", "lz4")) { - this.ramdisk.file = this.ramdisk.file.replace(".gz", ".$fmt") - //dump info again - ObjectMapper().writerWithDefaultPrettyPrinter().writeValue(File(workDir + this.info.json), this) - } else { - throw IllegalArgumentException("unknown format $fmt") - } + val fmt = C.dumpRamdisk( + C.Slice(info.output, ramdisk.position, ramdisk.size, ramdisk.file), "${workDir}root") + this.ramdisk.file = this.ramdisk.file + ".$fmt" + //dump info again + ObjectMapper().writerWithDefaultPrettyPrinter().writeValue(File(workDir + this.info.json), this) return this } diff --git a/bbootimg/src/main/kotlin/bootimg/v3/VendorBoot.kt b/bbootimg/src/main/kotlin/bootimg/v3/VendorBoot.kt index aff7afd..bf2a12e 100644 --- a/bbootimg/src/main/kotlin/bootimg/v3/VendorBoot.kt +++ b/bbootimg/src/main/kotlin/bootimg/v3/VendorBoot.kt @@ -57,7 +57,7 @@ data class VendorBoot(var info: MiscInfo = MiscInfo(), ret.info.pageSize = header.pageSize ret.info.headerVersion = header.headerVersion //ramdisk - ret.ramdisk.file = workDir + "ramdisk.img.gz" + ret.ramdisk.file = workDir + "ramdisk.img" ret.ramdisk.size = header.vndRamdiskSize ret.ramdisk.loadAddr = header.ramdiskLoadAddr ret.ramdisk.position = Helper.round_to_multiple( @@ -145,11 +145,13 @@ data class VendorBoot(var info: MiscInfo = MiscInfo(), fun extractImages(): VendorBoot { val workDir = Helper.prop("workDir") //header - ObjectMapper().writerWithDefaultPrettyPrinter().writeValue( - File(workDir + this.info.json), this) + ObjectMapper().writerWithDefaultPrettyPrinter().writeValue(File(workDir + this.info.json), this) //ramdisk - C.dumpRamdisk(C.Slice(info.output, ramdisk.position.toInt(), ramdisk.size.toInt(), ramdisk.file), + val fmt = C.dumpRamdisk(C.Slice(info.output, ramdisk.position.toInt(), ramdisk.size.toInt(), ramdisk.file), "${workDir}root") + this.ramdisk.file = this.ramdisk.file + ".$fmt" + //dump info again + ObjectMapper().writerWithDefaultPrettyPrinter().writeValue(File(workDir + this.info.json), this) //dtb C.dumpDtb(C.Slice(info.output, dtb.position.toInt(), dtb.size.toInt(), dtb.file)) return this