Skip to content

Commit 1ba9793

Browse files
committed
staging
1 parent 4307801 commit 1ba9793

File tree

8 files changed

+121
-37
lines changed

8 files changed

+121
-37
lines changed

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

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -525,23 +525,46 @@ class Common {
525525
}
526526
}
527527

528-
fun createWorkspaceIni(fileName: String) {
529-
//create workspace file
530-
val workspaceFile = File(Helper.prop("workDir"), "workspace.ini").canonicalPath
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+
531533
try {
532-
FileOutputStream(workspaceFile).use { output ->
533-
Properties().also {
534-
it.setProperty("file", fileName)
535-
it.setProperty("workDir", Helper.prop("workDir"))
536-
it.setProperty("role", File(fileName).name)
537-
it.store(output, "unpackInternal configuration")
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+
}
538551
}
539-
log.info("workspace file created: $workspaceFile")
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}")
540562
}
541563
} catch (e: IOException) {
542564
log.error("error writing workspace file: ${e.message}")
543565
}
544-
//create workspace file done
566+
567+
log.trace("create workspace file done")
545568
}
546569
}
547570
}

bbootimg/src/main/kotlin/bootimg/v2/BootV2.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,8 @@ data class BootV2(
300300
"verify fail"
301301
}
302302
Avb.getJsonFileName(info.output).let { jsonFile ->
303-
it.addRow("AVB info [$verifyStatus]", jsonFile)
304-
prints.add(Pair("AVB info [$verifyStatus]", jsonFile))
303+
it.addRow("AVB info [$verifyStatus]", shortenPath(jsonFile))
304+
prints.add(Pair("AVB info [$verifyStatus]", shortenPath(jsonFile)))
305305
if (File(jsonFile).exists()) {
306306
mapper.readValue(File(jsonFile), AVBInfo::class.java).let { ai ->
307307
val inspectRet = Avb.inspectKey(ai)
@@ -362,11 +362,11 @@ data class BootV2(
362362
if (theDtb.size > 0) {
363363
val dtbCount = this.dtb!!.dtbList.size
364364
it.addRule()
365-
it.addRow("dtb", theDtb.file)
366-
prints.add(Pair("dtb", theDtb.file.toString()))
365+
it.addRow("dtb", theDtb.file?.let { fullPath -> shortenPath(fullPath) })
366+
prints.add(Pair("dtb", shortenPath(theDtb.file.toString())))
367367
if (File(theDtb.file + ".0.${dtsSuffix}").exists()) {
368-
it.addRow("\\-- decompiled dts [$dtbCount]", theDtb.file + ".*.${dtsSuffix}")
369-
prints.add(Pair("\\-- decompiled dts [$dtbCount]", theDtb.file + ".*.${dtsSuffix}"))
368+
it.addRow("\\-- decompiled dts [$dtbCount]", shortenPath(theDtb.file + ".*.${dtsSuffix}"))
369+
prints.add(Pair("\\-- decompiled dts [$dtbCount]", shortenPath(theDtb.file + ".*.${dtsSuffix}")))
370370
}
371371
}
372372
}
@@ -377,8 +377,8 @@ data class BootV2(
377377
val tabVBMeta = AsciiTable().let {
378378
if (File("vbmeta.img").exists()) {
379379
it.addRule()
380-
it.addRow("vbmeta.img", Avb.getJsonFileName("vbmeta.img"))
381-
prints.add(Pair("vbmeta.img", Avb.getJsonFileName("vbmeta.img")))
380+
it.addRow("vbmeta.img", shortenPath(Avb.getJsonFileName("vbmeta.img")))
381+
prints.add(Pair("vbmeta.img", shortenPath(Avb.getJsonFileName("vbmeta.img"))))
382382
it.addRule()
383383
"\n" + it.render()
384384
} else {

bbootimg/src/main/kotlin/bootimg/v3/VendorBoot.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ data class VendorBoot(
341341
}
342342

343343
fun postCopy(outFile: String): VendorBoot {
344-
val signedFile = Helper.joinPath(Helper.prop("intermediateDir")!!, this.info.role + ".signed")
344+
val dir = Helper.prop("intermediateDir")!!
345+
val signedFile = Helper.joinPath(dir, "${info.role}.signed").takeIf { File(it).exists() }
346+
?: Helper.joinPath(dir, "${info.role}.clear")
345347
log.info("COPY $signedFile -> $outFile")
346348
File(signedFile).copyTo(File(outFile), overwrite = true)
347349
return this

bbootimg/src/main/kotlin/packable/DeviceTreeParser.kt

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package packable
22

33
import cfig.bootimg.Common
4+
import cfig.bootimg.v3.VendorBoot
45
import cfig.helper.Helper
56
import cfig.helper.Helper.Companion.check_call
67
import cfig.helper.Helper.Companion.check_output
@@ -15,13 +16,24 @@ class DeviceTreeParser : IPackable {
1516
override fun capabilities(): List<String> {
1617
return listOf("^.*\\.dtb$")
1718
}
19+
1820
override val loopNo: Int
1921
get() = 1
2022

2123
override fun unpack(fileName: String) {
22-
super.clear()
23-
log.info("unpacking $fileName")
24-
val outFile = workDir + fileName.removeSuffix(".dtb") + "." + Helper.prop("config.dts_suffix")
24+
unpackInternal(fileName, Helper.prop("workDir")!!)
25+
}
26+
27+
fun unpackInternal(fileName: String, unpackDir: String) {
28+
//set workdir
29+
log.info("unpackInternal(fileName: $fileName, unpackDir: $unpackDir)")
30+
Helper.setProp("workDir", unpackDir)
31+
clear()
32+
//create workspace file
33+
Common.createWorkspaceIni(fileName)
34+
//create workspace file done
35+
36+
val outFile = File(workDir, File(fileName).nameWithoutExtension + "." + Helper.prop("config.dts_suffix")).path
2537
DTC().decompile(fileName, outFile)
2638

2739
//print summary
@@ -32,15 +44,26 @@ class DeviceTreeParser : IPackable {
3244
}
3345

3446
override fun pack(fileName: String) {
35-
log.info("packing $fileName")
36-
val outFile = workDir + fileName.removeSuffix(".dtb") + "." + Helper.prop("config.dts_suffix")
37-
check(DTC().compile(outFile, "$fileName.new")) { "fail to compile dts" }
47+
packInternal(Helper.prop("workDir")!!, fileName)
48+
}
49+
50+
fun packInternal(workspace: String, outFileName: String) {
51+
log.info("packInternal($workspace, $outFileName)")
52+
Helper.setProp("workDir", workspace)
53+
//workspace+cfg
54+
val iniRole = Common.loadProperties(File(workspace, "workspace.ini").canonicalPath).getProperty("role")
55+
val dtsSrc = File(workDir, File(iniRole).nameWithoutExtension + "." + Helper.prop("config.dts_suffix")).path
56+
57+
val origFile = File(workDir, File(outFileName).name + ".orig").path
58+
log.info("COPY $outFileName -> $origFile")
59+
File(outFileName).copyTo(File(origFile), overwrite = true)
60+
check(DTC().compile(dtsSrc, outFileName)) { "fail to compile dts" }
3861

3962
//print summary
4063
val prints: MutableList<Pair<String, String>> = mutableListOf()
41-
prints.add(Pair("DTS", outFile))
42-
prints.add(Pair("updated DTB", "$fileName.new"))
43-
log.info("\n\t\t\tPack Summary of {}\n{}\n", fileName, Common.table2String(prints))
64+
prints.add(Pair("DTS", dtsSrc))
65+
prints.add(Pair("updated DTB", outFileName))
66+
log.info("\n\t\t\tPack Summary of {}\n{}\n", outFileName, Common.table2String(prints))
4467
}
4568

4669
fun pull(fileName: String) {

bbootimg/src/main/kotlin/packable/VBMetaParser.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ class VBMetaParser : IPackable {
5353
it.mkdirs()
5454
}
5555
}
56+
//workspace.ini
5657
log.info("workspace set to $unpackDir")
57-
Common.createWorkspaceIni(fileName)
58+
Common.createWorkspaceIni(fileName, prefix = "vbmeta")
5859

5960
val ai = AVBInfo.parseFrom(Dumpling(fileName)).dumpDefault(fileName)
6061
log.info("Signing Key: " + Avb.inspectKey(ai))
@@ -66,9 +67,9 @@ class VBMetaParser : IPackable {
6667

6768
// called via reflection
6869
fun packInternal(workspace: String, outFileName: String) {
69-
log.info("packInternal(workspace: $workspace)")
70+
log.info("packInternal(workspace: $workspace, $outFileName)")
7071
Helper.setProp("workDir", workspace)
71-
val iniRole = Common.loadProperties(File(workspace, "workspace.ini").canonicalPath).getProperty("role")
72+
val iniRole = Common.loadProperties(File(workspace, "workspace.ini").canonicalPath).getProperty("vbmeta.role")
7273
val blob = ObjectMapper().readValue(File(Avb.getJsonFileName(iniRole)), AVBInfo::class.java).encodePadded()
7374
log.info("Writing padded vbmeta to file: $outFileName.signed")
7475
Files.write(Paths.get("$outFileName.signed"), blob, StandardOpenOption.CREATE)

bbootimg/src/main/kotlin/packable/VendorBootParser.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class VendorBootParser : IPackable {
3636
}
3737

3838
fun unpackInternal(fileName: String, unpackDir: String) {
39+
//set workdir
3940
log.info("unpackInternal(fileName: $fileName, unpackDir: $unpackDir)")
4041
Helper.setProp("workDir", unpackDir)
4142
clear()

bbootimg/src/main/kotlin/rom/sparse/SparseImage.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,29 +147,32 @@ data class SparseImage(var info: SparseInfo = SparseInfo()) {
147147
val ret = SparseImage()
148148
ret.info.json = File(fileName).name.removeSuffix(".img") + ".json"
149149
ret.info.output = fileName
150-
ret.info.pulp = workDir + fileName
150+
ret.info.pulp = File(Helper.prop("workDir")!!, File(fileName).nameWithoutExtension).path
151+
log.info(ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(ret.info))
151152
if (isSparse(fileName)) {
152153
val tempFile = UUID.randomUUID().toString()
153154
ret.info.outerFsType = "sparse"
154-
val rawFile = "${workDir}${File(fileName).nameWithoutExtension}"
155155
simg2img(fileName, tempFile)
156156
ret.info.pulp = if (isExt4(tempFile)) {
157157
ret.info.innerFsType = "ext4"
158-
"$rawFile.ext4"
158+
ret.info.pulp + ".ext4"
159159
} else if (isErofs(tempFile)) {
160160
ret.info.innerFsType = "erofs"
161-
"$rawFile.erofs"
161+
ret.info.pulp + ".erofs"
162162
} else {
163-
"$rawFile.raw"
163+
ret.info.pulp + ".raw"
164164
}
165165
Files.move(Path(tempFile), Path(ret.info.pulp))
166166
} else if (isExt4(fileName)) {
167167
ret.info.outerFsType = "ext4"
168168
ret.info.innerFsType = "ext4"
169+
ret.info.pulp = ret.info.pulp + ".ext4"
170+
log.info("COPY $fileName -> ${ret.info.pulp}")
169171
File(fileName).copyTo(File(ret.info.pulp))
170172
} else if (isErofs(fileName)) {
171173
ret.info.outerFsType = "erofs"
172174
ret.info.innerFsType = "erofs"
175+
ret.info.pulp = ret.info.pulp + ".erofs"
173176
File(fileName).copyTo(File(ret.info.pulp))
174177
}
175178
when (ret.info.innerFsType) {

bbootimg/src/main/kotlin/rom/sparse/SparseImgParser.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package rom.sparse
1616

1717
import avb.blob.Footer
18+
import cfig.bootimg.Common
1819
import cfig.helper.Helper
1920
import cfig.packable.IPackable
2021
import cfig.packable.VBMetaParser
@@ -38,12 +39,24 @@ class SparseImgParser : IPackable {
3839
}
3940

4041
override fun unpack(fileName: String) {
42+
log.info("unpack(fileName: $fileName)")
43+
unpackInternal(fileName, Helper.prop("workDir")!!)
44+
}
45+
46+
fun unpackInternal(fileName: String, unpackDir: String) {
47+
//set workdir
48+
log.info("unpackInternal(fileName: $fileName, unpackDir: $unpackDir)")
49+
Helper.setProp("workDir", unpackDir)
4150
clear()
51+
//create workspace file
52+
Common.createWorkspaceIni(fileName)
53+
//create workspace file done
4254
SparseImage
4355
.parse(fileName)
4456
.printSummary(fileName)
4557
}
4658

59+
4760
override fun pack(fileName: String) {
4861
//TODO("not implemented: refer to https://github.com/cfig/Android_boot_image_editor/issues/133")
4962
val cfgFile = outDir + fileName.removeSuffix(".img") + ".json"
@@ -54,6 +67,24 @@ class SparseImgParser : IPackable {
5467
.unwrap()
5568
}
5669

70+
fun packInternal(workspace: String, outFileName: String) {
71+
log.info("packInternal($workspace, $outFileName)")
72+
Helper.setProp("workDir", workspace)
73+
//intermediate
74+
Helper.joinPath(workspace, "intermediate").also { intermediateDir ->
75+
File(intermediateDir).let {
76+
if (!it.exists()) {
77+
it.mkdir()
78+
}
79+
}
80+
Helper.setProp("intermediateDir", intermediateDir)
81+
}
82+
//workspace+cfg
83+
val iniRole = Common.loadProperties(File(workspace, "workspace.ini").canonicalPath).getProperty("role")
84+
val cfgFile = File(workspace, iniRole.removeSuffix(".img") + ".json").canonicalPath
85+
log.info("Loading config from $cfgFile")
86+
}
87+
5788
// invoked solely by reflection
5889
fun `@footer`(fileName: String) {
5990
FileInputStream(fileName).use { fis ->

0 commit comments

Comments
 (0)