Skip to content

Commit

Permalink
Issue #123: better support dtb/dts
Browse files Browse the repository at this point in the history
● support dumping /proc/device-tree from a running rooted Android
● support editing standalone dtb file
  • Loading branch information
cfig committed Jun 5, 2023
1 parent ec6f478 commit 7c6c911
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 18 deletions.
75 changes: 62 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@ Well done you did it! The last step is to star this repo :smile

## Supported ROM image types

| Image Type | file names | platforms | note |
| --------------- | ----------------------------------- |-------------|-------------------------|
| boot | boot.img, init_boot.img, boot(-debug|-test-harness).img | all | |
|vendor boot | vendor_boot.img, vendor_boot-debug.img, vendor_kernel_boot.img | all | |
| recovery | recovery.img, recovery-two-step.img | all | |
| vbmeta | vbmeta.img, vbmeta_system.img etc. | all | |
| dtbo | dtbo.img | linux & mac | |
| sparse images | system.img, vendor.img, product.img etc.| linux | |
| OTA payload | payload.bin | all | Windows git-bash |
| Image Type | file names | platforms | note |
| --------------- |----------------------------------------------------------------|-------------|-------------------------|
| boot | boot.img, init_boot.img, boot-debug.img, boot-test-harness.img | all | |
|vendor boot | vendor_boot.img, vendor_boot-debug.img, vendor_kernel_boot.img | all | |
| recovery | recovery.img, recovery-two-step.img | all | |
| vbmeta | vbmeta.img, vbmeta_system.img etc. | all | |
| dtbo | dtbo.img | linux & mac | |
| dtb | *.dtb | linux & mac | |
| sparse images | system.img, vendor.img, product.img etc. | linux | |
| OTA payload | payload.bin | all | Windows git-bash |

Please note that the boot.img MUST follows AOSP verified boot flow, either [Boot image signature](https://source.android.com/security/verifiedboot/verified-boot#signature_format) in VBoot 1.0 or [AVB HASH footer](https://android.googlesource.com/platform/external/avb/+/master/README.md#The-VBMeta-struct) (a.k.a. AVB) in VBoot 2.0.

Expand Down Expand Up @@ -156,7 +157,7 @@ Please note that to use 'gradle flash', your host machine must be connectted to
</details>

<details>
<summary>edit device-tree blob(dtb) inside vendor_boot.img</summary>
<summary>How to edit device tree blob(dtb) inside vendor_boot.img</summary>

If you want to edit the device-tree blob in place:

Expand Down Expand Up @@ -184,6 +185,55 @@ cp <your_dtb> build/unzip_boot/dtb

</details>

<details>

<summary>How to pull device tree blob(dtb) from a rooted device</summary>

If you have a rooted device and want to pull /proc/device-tree
```bash
touch fake.dtb
./gradlew pull
```
This tool will copy `dtc` to the target device via `adb`, and dump the dtb and dts file. Eventually you should get something like this
```
+--------+------------------------------+
| What | Where |
+--------+------------------------------+
| source | /proc/device-tree |
+--------+------------------------------+
| DTB | panther.dtb |
+--------+------------------------------+
| DTS | build/unzip_boot/panther.dts |
+--------+------------------------------+
```

</details>

<details>

<summary>How to work edit device tree blob(dtb) file</summary>

If you have a dtb file and want to edit its content
```bash
cp <your_dtb_file> .
./gradlew unpack
```
This tool will decompile it and put the decompiled source to build/unzip_boot.

```
Unpack Summary of panther.dtb
+------+------------------------------+
| What | Where |
+------+------------------------------+
| DTB | panther.dtb |
+------+------------------------------+
| DTS | build/unzip_boot/panther.dts |
+------+------------------------------+
```

</details>

<details>
<summary>working with system.img</summary>

Expand Down Expand Up @@ -255,11 +305,10 @@ https://android.googlesource.com/platform/system/core/+/refs/heads/master/libspa
Android Nexus/Pixle factory images<br/>
https://developers.google.cn/android/images<br/>

This project is developed with products by Jetbrains.
</details>

This project is developed with products by Jetbrains.

<a href="https://jb.gg/OpenSource">
<img src="https://user-images.githubusercontent.com/1133314/116802621-c076be80-ab46-11eb-8a14-9454a933de7d.png" alt="drawing" width="80">
</a>

</details>
83 changes: 83 additions & 0 deletions bbootimg/src/main/kotlin/packable/DeviceTreeParser.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package packable

import cfig.bootimg.Common
import cfig.helper.Helper
import cfig.helper.Helper.Companion.check_call
import cfig.helper.Helper.Companion.check_output
import cfig.helper.Helper.Companion.deleteIfExists
import cfig.packable.IPackable
import cfig.utils.DTC
import org.slf4j.LoggerFactory
import java.io.File

class DeviceTreeParser : IPackable {
override fun capabilities(): List<String> {
return listOf("^.*\\.dtb$")
}
override val loopNo: Int
get() = 1

override fun unpack(fileName: String) {
super.clear()
log.info("unpacking $fileName")
val outFile = workDir + fileName.removeSuffix(".dtb") + "." + Helper.prop("config.dts_suffix")
DTC().decompile(fileName, outFile)

//print summary
val prints: MutableList<Pair<String, String>> = mutableListOf()
prints.add(Pair("DTB", fileName))
prints.add(Pair("DTS", outFile))
log.info("\n\t\t\tUnpack Summary of {}\n{}\n", fileName, Common.table2String(prints))
}

override fun pack(fileName: String) {
log.info("packing $fileName")
val outFile = workDir + fileName.removeSuffix(".dtb") + "." + Helper.prop("config.dts_suffix")
check(DTC().compile(outFile, "$fileName.new")) { "fail to compile dts" }

//print summary
val prints: MutableList<Pair<String, String>> = mutableListOf()
prints.add(Pair("DTS", outFile))
prints.add(Pair("updated DTB", "$fileName.new"))
log.info("\n\t\t\tPack Summary of {}\n{}\n", fileName, Common.table2String(prints))
}

override fun pull(fileName: String, deviceName: String) {
//prepare
super.clear()
File(workDir).mkdir()

//pull
"adb root".check_call()
"adb push tools/bin/dtc-android /data/vendor/dtc-android".check_call()
val hw = "adb shell getprop ro.hardware".check_output()
log.info("ro.hardware=$hw")
"adb shell /data/vendor/dtc-android -I fs /proc/device-tree -o /data/vendor/file.to.pull".check_call()
"adb pull /data/vendor/file.to.pull $workDir$hw.dts".check_call()
"adb shell /data/vendor/dtc-android -I fs -O dtb /proc/device-tree -o /data/vendor/file.to.pull".check_call()
"adb pull /data/vendor/file.to.pull $hw.dtb".check_call()
"adb shell rm /data/vendor/file.to.pull".check_call()
"adb shell rm /data/vendor/dtc-android".check_call()
if (fileName != "$hw.dtb") {
File(fileName).delete()
log.warn("deleting intermediate dtb file: $fileName")
}

//print summary
val prints: MutableList<Pair<String, String>> = mutableListOf()
prints.add(Pair("source", "/proc/device-tree"))
prints.add(Pair("DTB", "$hw.dtb"))
prints.add(Pair("DTS", "$workDir$hw.dts"))
log.info("\n\t\t\tPull Summary of {}\n{}\n", "$hw.dtb", Common.table2String(prints))
}

fun clear(fileName: String) {
super.clear()
listOf(".new").forEach {
"$fileName$it".deleteIfExists()
}
}

private val log = LoggerFactory.getLogger(DeviceTreeParser::class.java)
private val workDir = Helper.prop("workDir")
}
4 changes: 3 additions & 1 deletion bbootimg/src/main/kotlin/packable/PackableLauncher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cfig.packable

import cfig.utils.SparseImgParser
import org.slf4j.LoggerFactory
import packable.DeviceTreeParser
import java.io.File
import java.util.regex.Pattern
import kotlin.reflect.KClass
Expand All @@ -30,7 +31,8 @@ fun main(args: Array<String>) {
val packablePool = mutableMapOf<List<String>, KClass<IPackable>>()
listOf(
DtboParser(), VBMetaParser(), BootImgParser(), SparseImgParser(), VendorBootParser(), PayloadBinParser(),
MiscImgParser()
MiscImgParser(),
DeviceTreeParser()
).forEach {
@Suppress("UNCHECKED_CAST")
packablePool.put(it.capabilities(), it::class as KClass<IPackable>)
Expand Down
3 changes: 0 additions & 3 deletions bbootimg/src/main/kotlin/packable/VBMetaParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@
package cfig.packable

import avb.AVBInfo
import avb.alg.Algorithms
import cfig.Avb
import cfig.helper.CryptoHelper
import cfig.helper.Dumpling
import cfig.helper.Helper
import cfig.helper.Helper.Companion.deleteIfExists
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import org.slf4j.LoggerFactory
import java.io.File
Expand Down
2 changes: 2 additions & 0 deletions integrationTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ def main():
verifySingleDir(resDir2, "issue_117_xz_crc")
# Issue 122: ramdisk.img, boot image v0
verifySingleDir(resDir2, "issue_122_ramdisk_img")
# Issue 123: dtb
verifySingleDir(resDir2, "issue_123_dtb")

log.info(successLogo)

Expand Down
2 changes: 1 addition & 1 deletion src/integrationTest/resources_2
Submodule resources_2 updated from 5fdce9 to 517ca7
Binary file added tools/bin/dtc-android
Binary file not shown.

0 comments on commit 7c6c911

Please sign in to comment.