From a8c3166994455c51330db8e8a7333fc92613d804 Mon Sep 17 00:00:00 2001 From: cfig Date: Mon, 16 Oct 2023 17:05:22 +0800 Subject: [PATCH] Mass update for ramdisk and lazybox lazybox: support bootchart ramdisk: support cpio entry of type CHAR_DEV --- .../main/kotlin/bootimg/cpio/AndroidCpio.kt | 21 ++++++++++ lazybox/src/main/kotlin/cfig/lazybox/App.kt | 9 +++- .../kotlin/cfig/lazybox/sysinfo/BootChart.kt | 41 +++++++++++++++++++ .../kotlin/cfig/lazybox/sysinfo/SysInfo.kt | 15 +++++++ tools/factory_image_parser.py | 2 + 5 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 lazybox/src/main/kotlin/cfig/lazybox/sysinfo/BootChart.kt diff --git a/bbootimg/src/main/kotlin/bootimg/cpio/AndroidCpio.kt b/bbootimg/src/main/kotlin/bootimg/cpio/AndroidCpio.kt index 6f657ae..dd40d46 100644 --- a/bbootimg/src/main/kotlin/bootimg/cpio/AndroidCpio.kt +++ b/bbootimg/src/main/kotlin/bootimg/cpio/AndroidCpio.kt @@ -70,6 +70,7 @@ class AndroidCpio { ino = inodeNumber++ ) } + Files.isDirectory(item.toPath()) -> { log.debug("DIR: " + item.path + ", " + item.toPath()) AndroidCpioEntry( @@ -79,6 +80,7 @@ class AndroidCpio { ino = inodeNumber++ ) } + Files.isRegularFile(item.toPath()) -> { log.debug("REG: " + item.path) AndroidCpioEntry( @@ -88,6 +90,7 @@ class AndroidCpio { ino = inodeNumber++ ) } + else -> { throw IllegalArgumentException("do not support file " + item.name) } @@ -142,10 +145,12 @@ class AndroidCpio { log.warn("${entry.name} has NO fsconfig/prefix match") } } + 1 -> { log.debug("${entry.name} == preset fsconfig") entry.statMode = itemConfig[0].statMode } + else -> { //Issue #75: https://github.com/cfig/Android_boot_image_editor/issues/75 //Reason: cpio may have multiple entries with the same name, that's caused by man-made errors @@ -248,6 +253,7 @@ class AndroidCpio { Files.createSymbolicLink(Paths.get(outEntryName), Paths.get(String(buffer))) } } + entry.isRegularFile -> { entryInfo.note = ("REG " + entryInfo.note) File(outEntryName).also { it.parentFile.mkdirs() }.writeBytes(buffer) @@ -260,6 +266,7 @@ class AndroidCpio { ) } } + entry.isDirectory -> { entryInfo.note = ("DIR " + entryInfo.note) File(outEntryName).mkdir() @@ -272,6 +279,20 @@ class AndroidCpio { //Windows } } + + entry.isCharacterDevice -> { + entryInfo.note = ("CHAR " + entryInfo.note) + File(outEntryName).also { it.parentFile.mkdirs() }.writeBytes(buffer) + if (EnvironmentVerifier().isWindows) { + //Windows: Posix not supported + } else { + Files.setPosixFilePermissions( + Paths.get(outEntryName), + Helper.modeToPermissions(((entry.mode and PERM_MASK) or 0b111_000_000).toInt()) + ) + } + } + else -> throw IllegalArgumentException("??? type unknown") } File(outEntryName).setLastModified(entry.time) diff --git a/lazybox/src/main/kotlin/cfig/lazybox/App.kt b/lazybox/src/main/kotlin/cfig/lazybox/App.kt index 01c6a36..28aa4b6 100644 --- a/lazybox/src/main/kotlin/cfig/lazybox/App.kt +++ b/lazybox/src/main/kotlin/cfig/lazybox/App.kt @@ -1,5 +1,6 @@ package cfig.lazybox +import cfig.lazybox.sysinfo.BootChart import cfig.lazybox.sysinfo.CpuInfo import cfig.lazybox.sysinfo.Pidstat import cfig.lazybox.sysinfo.SysInfo @@ -16,7 +17,10 @@ fun main(args: Array) { println("Usage: args: (Array) ...") println(" or: function [arguments]...") println("\nCurrently defined functions:") - println("\tcpuinfo sysinfo sysstat pidstat") + println("\tcpuinfo sysinfo sysstat pidstat bootchart") + println("\nCommand Usage:") + println("bootchart: generate Android bootchart") + println("pidstat : given a pid, profile its CPU usage") exitProcess(0) } if (args.get(0) == "cpuinfo") { @@ -37,4 +41,7 @@ fun main(args: Array) { if (args.get(0) == "pidstat") { Pidstat.run() } + if (args.get(0) == "bootchart") { + BootChart.run() + } } diff --git a/lazybox/src/main/kotlin/cfig/lazybox/sysinfo/BootChart.kt b/lazybox/src/main/kotlin/cfig/lazybox/sysinfo/BootChart.kt new file mode 100644 index 0000000..281229d --- /dev/null +++ b/lazybox/src/main/kotlin/cfig/lazybox/sysinfo/BootChart.kt @@ -0,0 +1,41 @@ +package cfig.lazybox.sysinfo + +import cfig.helper.Helper +import cfig.helper.Helper.Companion.check_call +import org.slf4j.LoggerFactory +import java.util.concurrent.TimeUnit + +class BootChart { + companion object { + private val log = LoggerFactory.getLogger(BootChart::class.java) + fun run() { + "adb wait-for-device".check_call() + "adb root".check_call() + "adb wait-for-device".check_call() + "adb shell touch /data/bootchart/enabled".check_call() + Helper.adbCmd("reboot") + "adb wait-for-device".check_call() + "adb root".check_call() + "adb wait-for-device".check_call() + Helper.adbCmd("rm -fv /data/bootchart/enabled") + while (true) { + val comp = Helper.adbCmd("getprop sys.boot_completed") + if (comp == "1") { + log.info("boot completed") + TimeUnit.SECONDS.sleep(3) + break + } else { + log.info("still booting ...") + TimeUnit.SECONDS.sleep(1) + } + } + "header proc_stat.log proc_ps.log proc_diskstats.log".split("\\s".toRegex()).forEach { + val LOGROOT = "/data/bootchart/" + "adb pull ${LOGROOT}$it".check_call() + } + "tar -czf bootchart.tgz header proc_stat.log proc_ps.log proc_diskstats.log".check_call() + "pybootchartgui bootchart.tgz".check_call() + "xdg-open bootchart.png".check_call() + } + } +} \ No newline at end of file diff --git a/lazybox/src/main/kotlin/cfig/lazybox/sysinfo/SysInfo.kt b/lazybox/src/main/kotlin/cfig/lazybox/sysinfo/SysInfo.kt index 877afe2..67bf431 100644 --- a/lazybox/src/main/kotlin/cfig/lazybox/sysinfo/SysInfo.kt +++ b/lazybox/src/main/kotlin/cfig/lazybox/sysinfo/SysInfo.kt @@ -30,6 +30,21 @@ class SysInfo { } } + fun makeTar(tarFile: String, srcDir: String, fmt: String) { + val pyScript = + """ +import os, sys, subprocess, gzip, logging, shutil, tarfile, os.path +def makeTar(output_filename, source_dir): + with tarfile.open(output_filename, "w:%s") as tar: + tar.add(source_dir, arcname=os.path.basename(source_dir)) +makeTar("%s", "%s") +""".trim() + val tmp = Files.createTempFile(Paths.get("."), "xx.", ".yy") + tmp.writeText(String.format(fmt, pyScript, tarFile, srcDir)) + ("python " + tmp.fileName).check_call() + tmp.deleteIfExists() + } + private fun makeTar(tarFile: String, srcDir: String) { val pyScript = """ diff --git a/tools/factory_image_parser.py b/tools/factory_image_parser.py index 38f32cb..62879ff 100755 --- a/tools/factory_image_parser.py +++ b/tools/factory_image_parser.py @@ -74,6 +74,8 @@ def prepare(zipFile): list2 = [] purgeFolder(tmp1) purgeFolder(tmp2) + os.mkdir(tmp1) + os.mkdir(tmp2) with ZipFile(zipFile, 'r') as zf: zf.extractall(path=tmp1)