From ea73a704b3c2d56cc2df2a67e566f5dd093d9589 Mon Sep 17 00:00:00 2001 From: cfig Date: Wed, 4 Nov 2020 18:46:59 +0800 Subject: [PATCH] be tolerant in checking osVersion --- bbootimg/src/main/kotlin/bootimg/Common.kt | 29 +++++++++++++++-- bbootimg/src/main/kotlin/bootimg/v2/BootV2.kt | 32 +++++++------------ bbootimg/src/main/kotlin/bootimg/v3/BootV3.kt | 11 +------ .../src/main/kotlin/bootimg/v3/VendorBoot.kt | 4 +-- 4 files changed, 41 insertions(+), 35 deletions(-) diff --git a/bbootimg/src/main/kotlin/bootimg/Common.kt b/bbootimg/src/main/kotlin/bootimg/Common.kt index a2232b6..693addf 100644 --- a/bbootimg/src/main/kotlin/bootimg/Common.kt +++ b/bbootimg/src/main/kotlin/bootimg/Common.kt @@ -3,7 +3,6 @@ package cfig.bootimg import cfig.EnvironmentVerifier import cfig.dtb_util.DTC import cfig.helper.Helper -import cfig.helper.Helper.Companion.check_call import cfig.helper.ZipHelper import cfig.io.Struct3.InputStreamExt.Companion.getInt import cfig.kernel_util.KernelExtractor @@ -11,7 +10,10 @@ import org.apache.commons.exec.CommandLine import org.apache.commons.exec.DefaultExecutor import org.apache.commons.exec.PumpStreamHandler import org.slf4j.LoggerFactory -import java.io.* +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.io.File +import java.io.FileInputStream import java.nio.ByteBuffer import java.nio.ByteOrder import java.security.MessageDigest @@ -36,6 +38,7 @@ class Common { companion object { private val log = LoggerFactory.getLogger(Common::class.java) + private const val MAX_ANDROID_VER = 11 @Throws(IllegalArgumentException::class) fun packOsVersion(x: String?): Int { @@ -271,5 +274,27 @@ class Common { fis.getInt(ByteOrder.LITTLE_ENDIAN) } } + + fun parseOsMajor(osVersion: String): Int { + return try { + log.info("OS Major: " + osVersion.split(".")[0]) + val ret = Integer.parseInt(osVersion.split(".")[0]) + when { + ret > MAX_ANDROID_VER -> { + log.warn("Os Major exceeds current max $MAX_ANDROID_VER") + MAX_ANDROID_VER + } + ret < 10 -> { + 10 + } + else -> { + ret + } + } + } catch (e: Exception) { + log.warn("can not parse osVersion from $osVersion") + 10 + } + } } } diff --git a/bbootimg/src/main/kotlin/bootimg/v2/BootV2.kt b/bbootimg/src/main/kotlin/bootimg/v2/BootV2.kt index 0b2e684..6221637 100644 --- a/bbootimg/src/main/kotlin/bootimg/v2/BootV2.kt +++ b/bbootimg/src/main/kotlin/bootimg/v2/BootV2.kt @@ -85,12 +85,12 @@ data class BootV2( } ret.kernel.let { theKernel -> theKernel.file = "${workDir}kernel" - theKernel.size = bh2.kernelLength.toInt() + theKernel.size = bh2.kernelLength theKernel.loadOffset = bh2.kernelOffset theKernel.position = ret.getKernelPosition() } ret.ramdisk.let { theRamdisk -> - theRamdisk.size = bh2.ramdiskLength.toInt() + theRamdisk.size = bh2.ramdiskLength theRamdisk.loadOffset = bh2.ramdiskOffset theRamdisk.position = ret.getRamdiskPosition() if (bh2.ramdiskLength > 0) { @@ -99,7 +99,7 @@ data class BootV2( } if (bh2.secondBootloaderLength > 0) { ret.secondBootloader = CommArgs() - ret.secondBootloader!!.size = bh2.secondBootloaderLength.toInt() + ret.secondBootloader!!.size = bh2.secondBootloaderLength ret.secondBootloader!!.loadOffset = bh2.secondBootloaderOffset ret.secondBootloader!!.file = "${workDir}second" ret.secondBootloader!!.position = ret.getSecondBootloaderPosition() @@ -178,19 +178,19 @@ data class BootV2( secondBootloader?.let { Helper.extractFile(info.output, secondBootloader!!.file!!, - secondBootloader!!.position.toLong(), + secondBootloader!!.position, secondBootloader!!.size) } //recovery dtbo recoveryDtbo?.let { Helper.extractFile(info.output, recoveryDtbo!!.file!!, - recoveryDtbo!!.position.toLong(), - recoveryDtbo!!.size.toInt()) + recoveryDtbo!!.position, + recoveryDtbo!!.size) } //dtb this.dtb?.let { _ -> - Common.dumpDtb(Slice(info.output, dtb!!.position.toInt(), dtb!!.size.toInt(), dtb!!.file!!)) + Common.dumpDtb(Slice(info.output, dtb!!.position.toInt(), dtb!!.size, dtb!!.file!!)) } return this @@ -309,16 +309,6 @@ data class BootV2( ) } - fun parseOsMajor(): Int { - return try { - log.info("OS Major: " + (info.osVersion?.split(".")?.get(0) ?: "null")) - val ret = Integer.parseInt(info.osVersion!!.split(".")[0]) - if (ret <= 10) 10 else ret - } catch (e: Exception) { - 10 - } - } - fun pack(): BootV2 { //refresh kernel size this.kernel.size = File(this.kernel.file!!).length().toInt() @@ -333,7 +323,7 @@ data class BootV2( } else { File(this.ramdisk.file!!).deleleIfExists() File(this.ramdisk.file!!.removeSuffix(".gz")).deleleIfExists() - Common.packRootfs("${workDir}/root", this.ramdisk.file!!, parseOsMajor()) + Common.packRootfs("${workDir}/root", this.ramdisk.file!!, Common.parseOsMajor(info.osVersion.toString())) } this.ramdisk.size = File(this.ramdisk.file!!).length().toInt() } @@ -449,11 +439,11 @@ data class BootV2( ret.addArgument("--dtb ") ret.addArgument(dtb!!.file!!) ret.addArgument("--dtb_offset ") - ret.addArgument("0x" + java.lang.Long.toHexString(dtb!!.loadOffset.toLong())) + ret.addArgument("0x" + java.lang.Long.toHexString(dtb!!.loadOffset)) } } ret.addArgument(" --pagesize ") - ret.addArgument(Integer.toString(info.pageSize.toInt())) + ret.addArgument(info.pageSize.toString()) ret.addArgument(" --cmdline ") ret.addArgument(info.cmdline, false) if (!info.osVersion.isNullOrBlank()) { @@ -476,7 +466,7 @@ data class BootV2( } fun sign(): BootV2 { - val avbtool = String.format(Helper.prop("avbtool"), if (parseOsMajor() > 10) "v1.2" else "v1.1") + val avbtool = String.format(Helper.prop("avbtool"), if (Common.parseOsMajor(info.osVersion.toString()) > 10) "v1.2" else "v1.1") if (info.verify == "VB2.0") { Signer.signAVB(info.output, this.info.imageSize, avbtool) log.info("Adding hash_footer with verified-boot 2.0 style") diff --git a/bbootimg/src/main/kotlin/bootimg/v3/BootV3.kt b/bbootimg/src/main/kotlin/bootimg/v3/BootV3.kt index 4de7093..81cf2ff 100644 --- a/bbootimg/src/main/kotlin/bootimg/v3/BootV3.kt +++ b/bbootimg/src/main/kotlin/bootimg/v3/BootV3.kt @@ -72,15 +72,6 @@ data class BootV3(var info: MiscInfo = MiscInfo(), var position: Int = 0, var size: Int = 0) - fun parseOsMajor(): Int { - return try { - log.info("OS Major: " + info.osVersion.split(".")[0]) - Integer.parseInt(info.osVersion.split(".")[0]) - } catch (e: Exception) { - 11 - } - } - fun pack(): BootV3 { if (File(this.ramdisk.file).exists() && !File(workDir + "root").exists()) { //do nothing if we have ramdisk.img.gz but no /root @@ -88,7 +79,7 @@ data class BootV3(var info: MiscInfo = MiscInfo(), } else { File(this.ramdisk.file).deleleIfExists() File(this.ramdisk.file.replaceFirst("[.][^.]+$", "")).deleleIfExists() - C.packRootfs("$workDir/root", this.ramdisk.file, parseOsMajor()) + C.packRootfs("$workDir/root", this.ramdisk.file, C.parseOsMajor(info.osVersion)) } this.kernel.size = File(this.kernel.file).length().toInt() this.ramdisk.size = File(this.ramdisk.file).length().toInt() diff --git a/bbootimg/src/main/kotlin/bootimg/v3/VendorBoot.kt b/bbootimg/src/main/kotlin/bootimg/v3/VendorBoot.kt index 1a80704..bc60d67 100644 --- a/bbootimg/src/main/kotlin/bootimg/v3/VendorBoot.kt +++ b/bbootimg/src/main/kotlin/bootimg/v3/VendorBoot.kt @@ -150,13 +150,13 @@ data class VendorBoot(var info: MiscInfo = MiscInfo(), //header ObjectMapper().writerWithDefaultPrettyPrinter().writeValue(File(workDir + this.info.json), this) //ramdisk - val fmt = 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, 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)) + C.dumpDtb(C.Slice(info.output, dtb.position.toInt(), dtb.size, dtb.file)) return this }