Skip to content

Commit 7451a29

Browse files
committed
squashed commit
gradle 8.14 kotlin 2.1.21 updated support of be Signed-off-by: cfig <yuyezhong@gmail.com>
1 parent 763427a commit 7451a29

29 files changed

+917
-210
lines changed

bbootimg/build.gradle.kts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
1616

1717
plugins {
18-
kotlin("jvm") version "2.0.20"
18+
kotlin("jvm") version "2.1.21"
1919
application
2020
}
2121

@@ -62,11 +62,13 @@ java {
6262
targetCompatibility = JavaVersion.VERSION_11
6363
}
6464

65-
tasks.withType<KotlinCompile>().all {
66-
kotlinOptions {
67-
freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
68-
freeCompilerArgs += "-opt-in=kotlin.ExperimentalUnsignedTypes"
69-
jvmTarget = "11"
65+
tasks.withType<KotlinCompile>().configureEach {
66+
compilerOptions {
67+
freeCompilerArgs.addAll(
68+
"-opt-in=kotlin.RequiresOptIn",
69+
"-opt-in=kotlin.ExperimentalUnsignedTypes"
70+
)
71+
jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_11)
7072
}
7173
}
7274

bbootimg/src/main/kotlin/avb/Avb.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class Avb {
206206
val readBackInfo = ObjectMapper().readValue(File(getJsonFileName(fileName)), AVBInfo::class.java)
207207
val intermediateDir = Helper.joinPath(Helper.prop("workDir")!!, "intermediate")
208208
val newHashDesc = if (File(intermediateDir).exists()) {
209-
AVBInfo.parseFrom(Dumpling(Helper.joinPath(intermediateDir, "$fileName.signed")))
209+
AVBInfo.parseFrom(Dumpling(Helper.joinPath(intermediateDir, File("$fileName.signed").name)))
210210
} else {
211211
//FIXME: before BootV2 supports abe mode
212212
AVBInfo.parseFrom(Dumpling("$fileName.signed"))

bbootimg/src/main/kotlin/bootimg/Common.kt

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import cfig.bootimg.cpio.AndroidCpio
2020
import rom.fdt.DTC
2121
import cfig.helper.Helper
2222
import cfig.helper.ZipHelper
23+
import cfig.packable.BootImgParser
2324
import cfig.utils.KernelExtractor
2425
import com.github.freva.asciitable.HorizontalAlign
2526
import org.apache.commons.exec.CommandLine
@@ -32,6 +33,8 @@ import java.io.File
3233
import java.nio.file.Files
3334
import java.nio.file.Paths
3435
import java.io.FileInputStream
36+
import java.io.FileOutputStream
37+
import java.io.IOException
3538
import java.lang.NumberFormatException
3639
import java.nio.ByteBuffer
3740
import java.nio.ByteOrder
@@ -52,6 +55,12 @@ class Common {
5255
private val log = LoggerFactory.getLogger(Common::class.java)
5356
private const val MAX_ANDROID_VER = 11
5457

58+
val loadProperties: (String) -> Properties = { fileName ->
59+
Properties().apply {
60+
File(fileName).inputStream().use { load(it) }
61+
}
62+
}
63+
5564
@Throws(IllegalArgumentException::class)
5665
fun packOsVersion(x: String?): Int {
5766
if (x.isNullOrBlank()) return 0
@@ -423,19 +432,24 @@ class Common {
423432
)
424433
}
425434

426-
fun printPackSummary(imageName: String) {
435+
fun printPackSummary(imageName: String, outFile: String? = null) {
427436
val prints: MutableList<Pair<String, String>> = mutableListOf()
428437
val tableHeader = de.vandermeer.asciitable.AsciiTable().apply {
429438
addRule(); addRow("What", "Where"); addRule()
430439
}
431440
val tab = de.vandermeer.asciitable.AsciiTable().let {
432441
it.addRule()
433-
if (File("$imageName.signed").exists()) {
434-
it.addRow("re-packed $imageName", "$imageName.signed")
435-
prints.add(Pair("re-packed $imageName", "$imageName.signed"))
442+
if (outFile != null) {
443+
it.addRow("re-packed $imageName", outFile)
444+
prints.add(Pair("re-packed $imageName", outFile))
436445
} else {
437-
it.addRow("re-packed $imageName", "$imageName.clear")
438-
prints.add(Pair("re-packed $imageName", "$imageName.clear"))
446+
if (File("$imageName.signed").exists()) {
447+
it.addRow("re-packed $imageName", "$imageName.signed")
448+
prints.add(Pair("re-packed $imageName", "$imageName.signed"))
449+
} else {
450+
it.addRow("re-packed $imageName", "$imageName.clear")
451+
prints.add(Pair("re-packed $imageName", "$imageName.clear"))
452+
}
439453
}
440454
it.addRule()
441455
it
@@ -457,6 +471,31 @@ class Common {
457471
}
458472
}
459473

474+
/*
475+
be_caller_dir: set in "be" script, to support out of tree invocation
476+
*/
477+
fun shortenPath(fullPath: String, inCurrentPath: String = System.getProperty("user.dir")): String {
478+
val currentPath = System.getenv("be_caller_dir") ?: inCurrentPath
479+
val full = Paths.get(fullPath).normalize().toAbsolutePath()
480+
val base = Paths.get(currentPath).normalize().toAbsolutePath()
481+
return try {
482+
base.relativize(full).toString()
483+
} catch (e: IllegalArgumentException) {
484+
full.toString()
485+
}
486+
}
487+
488+
fun String.toShortenPath(inCurrentPath: String = System.getProperty("user.dir")): String {
489+
val currentPath = System.getenv("be_caller_dir") ?: inCurrentPath
490+
val full = Paths.get(this).normalize().toAbsolutePath()
491+
val base = Paths.get(currentPath).normalize().toAbsolutePath()
492+
return try {
493+
base.relativize(full).toString()
494+
} catch (e: IllegalArgumentException) {
495+
full.toString()
496+
}
497+
}
498+
460499
fun printPackSummaryInternal(imageName: String) {
461500
val prints: MutableList<Pair<String, String>> = mutableListOf()
462501
val tableHeader = de.vandermeer.asciitable.AsciiTable().apply {
@@ -485,5 +524,47 @@ class Common {
485524
log.info("\n\t\t\tPack Summary of ${imageName}\n{}\n{}", tableHeader.render(), tab.render())
486525
}
487526
}
527+
528+
fun createWorkspaceIni(fileName: String, iniFileName: String = "workspace.ini", prefix: String? = null) {
529+
log.trace("create workspace file")
530+
val workDir = Helper.prop("workDir")
531+
val workspaceFile = File(workDir, iniFileName)
532+
533+
try {
534+
if (prefix.isNullOrBlank()) {
535+
// override existing file entirely when prefix is null or empty
536+
val props = Properties().apply {
537+
setProperty("file", fileName)
538+
setProperty("workDir", workDir)
539+
setProperty("role", File(fileName).name)
540+
}
541+
FileOutputStream(workspaceFile).use { out ->
542+
props.store(out, "unpackInternal configuration (overridden)")
543+
}
544+
log.info("workspace file overridden: ${workspaceFile.canonicalPath}")
545+
} else {
546+
// merge into existing (or create new) with prefixed keys
547+
val props = Properties().apply {
548+
if (workspaceFile.exists()) {
549+
FileInputStream(workspaceFile).use { load(it) }
550+
}
551+
}
552+
553+
fun key(name: String) = "${prefix.trim()}.$name"
554+
props.setProperty(key("file"), fileName)
555+
props.setProperty(key("workDir"), workDir)
556+
props.setProperty(key("role"), File(fileName).name)
557+
558+
FileOutputStream(workspaceFile).use { out ->
559+
props.store(out, "unpackInternal configuration (with prefix='$prefix')")
560+
}
561+
log.info("workspace file created/updated with prefix '$prefix': ${workspaceFile.canonicalPath}")
562+
}
563+
} catch (e: IOException) {
564+
log.error("error writing workspace file: ${e.message}")
565+
}
566+
567+
log.trace("create workspace file done")
568+
}
488569
}
489570
}

bbootimg/src/main/kotlin/bootimg/Signer.kt

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,58 @@ class Signer {
3030
companion object {
3131
private val log = LoggerFactory.getLogger(Signer::class.java)
3232

33+
fun signAVB2(inFile: String, //"$output.clear"
34+
outFile: String, //"$output.signed"
35+
aiFile: String, //AVBInfo
36+
imageSize: Long,
37+
avbtool: String) {
38+
log.info("Adding hash_footer with verified-boot 2.0 style: $inFile -> $outFile")
39+
val ai = ObjectMapper().readValue(File(aiFile), AVBInfo::class.java)
40+
val alg = Algorithms.get(ai.header!!.algorithm_type)
41+
val bootDesc = ai.auxBlob!!.hashDescriptors[0]
42+
val newAvbInfo = ObjectMapper().readValue(File(aiFile), AVBInfo::class.java)
43+
44+
//our signer
45+
File(inFile).copyTo(File(outFile), overwrite = true)
46+
Avb().addHashFooter(outFile,
47+
imageSize,
48+
partition_name = bootDesc.partition_name,
49+
newAvbInfo = newAvbInfo)
50+
//original signer
51+
val cmdPrefix = if (EnvironmentVerifier().isWindows) "python " else ""
52+
CommandLine.parse("$cmdPrefix$avbtool add_hash_footer").apply {
53+
addArguments("--image ${outFile}2") //boot.img.signed2
54+
addArguments("--flags ${ai.header!!.flags}")
55+
addArguments("--partition_size ${imageSize}")
56+
addArguments("--salt ${Helper.toHexString(bootDesc.salt)}")
57+
addArguments("--partition_name ${bootDesc.partition_name}")
58+
addArguments("--hash_algorithm ${bootDesc.hash_algorithm}")
59+
addArguments("--algorithm ${alg!!.name}")
60+
addArguments("--rollback_index ${ai.header!!.rollback_index}")
61+
if (alg.defaultKey.isNotBlank()) {
62+
addArguments("--key ${alg.defaultKey}")
63+
}
64+
newAvbInfo.auxBlob?.let { newAuxblob ->
65+
newAuxblob.propertyDescriptors.forEach { newProp ->
66+
addArguments(arrayOf("--prop", "${newProp.key}:${newProp.value}"))
67+
}
68+
}
69+
addArgument("--internal_release_string")
70+
addArgument(ai.header!!.release_string, false)
71+
log.info(this.toString())
72+
73+
File(inFile).copyTo(File("${outFile}2"), overwrite = true)
74+
DefaultExecutor().execute(this)
75+
}
76+
Helper.assertFileEquals(outFile, "${outFile}2")
77+
File("${outFile}2").delete()
78+
//TODO: decide what to verify
79+
//Parser.verifyAVBIntegrity(cfg.info.output + ".signed", avbtool)
80+
//Parser.verifyAVBIntegrity(cfg.info.output + ".signed2", avbtool)
81+
}
82+
3383
fun signAVB(output: String, imageSize: Long, avbtool: String) {
34-
log.info("Adding hash_footer with verified-boot 2.0 style")
84+
log.info("Adding hash_footer with verified-boot 2.0 style: $output")
3585
val ai = ObjectMapper().readValue(File(getJsonFileName(output)), AVBInfo::class.java)
3686
val alg = Algorithms.get(ai.header!!.algorithm_type)
3787
val bootDesc = ai.auxBlob!!.hashDescriptors[0]

bbootimg/src/main/kotlin/bootimg/cpio/AndroidCpio.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ class AndroidCpio {
194194
val rounded = Helper.round_to_multiple(len, 256) //file in page 256
195195
if (len != rounded) {
196196
FileOutputStream(outFile, true).use { fos ->
197+
log.info("cpio padding size: " + (rounded - len) + " bytes")
197198
fos.write(ByteArray((rounded - len).toInt()))
198199
}
199200
}

0 commit comments

Comments
 (0)