Skip to content

Commit

Permalink
trivial improvements
Browse files Browse the repository at this point in the history
- replace String.deleteIfExists with kotlin.io.Path.deleteIfExists
- remove unused "dtb.src", use "dtb.*.src" instead
  • Loading branch information
cfig committed Jan 9, 2024
1 parent 2076fad commit 94570b1
Show file tree
Hide file tree
Showing 25 changed files with 97 additions and 80 deletions.
6 changes: 3 additions & 3 deletions bbootimg/src/main/kotlin/bootimg/Common.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package cfig.bootimg
import cc.cfig.io.Struct
import cfig.utils.EnvironmentVerifier
import cfig.bootimg.cpio.AndroidCpio
import cfig.utils.DTC
import rom.fdt.DTC
import cfig.helper.Helper
import cfig.helper.ZipHelper
import cfig.utils.KernelExtractor
Expand Down Expand Up @@ -179,10 +179,10 @@ class Common {
return ret
}

fun dumpDtb(s: Helper.Slice) {
fun dumpDtb(s: Helper.Slice, decompile: Boolean = true) {
Helper.extractFile(s.srcFile, s.dumpFile, s.offset.toLong(), s.length)
//extract DTB
if (EnvironmentVerifier().hasDtc) {
if (EnvironmentVerifier().hasDtc && decompile) {
DTC().decompile(s.dumpFile, s.dumpFile + "." + Helper.prop("config.dts_suffix"))
}
}
Expand Down
8 changes: 4 additions & 4 deletions bbootimg/src/main/kotlin/bootimg/v2/BootV2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import cfig.helper.Dumpling
import cfig.helper.Helper
import cfig.helper.ZipHelper
import cfig.packable.VBMetaParser
import cfig.utils.DTC
import rom.fdt.DTC
import cfig.utils.EnvironmentVerifier
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.freva.asciitable.HorizontalAlign
Expand Down Expand Up @@ -250,7 +250,7 @@ data class BootV2(
}
//dtb
this.dtb?.let { _ ->
Common.dumpDtb(Helper.Slice(info.output, dtb!!.position.toInt(), dtb!!.size, dtb!!.file!!))
Common.dumpDtb(Helper.Slice(info.output, dtb!!.position.toInt(), dtb!!.size, dtb!!.file!!), false)
this.dtb!!.dtbList = DTC.parseMultiple(dtb!!.file!!)
log.info("dtb sz = " + this.dtb!!.dtbList.size)
//dump info again
Expand Down Expand Up @@ -359,7 +359,7 @@ data class BootV2(
it.addRule()
it.addRow("dtb", theDtb.file)
prints.add(Pair("dtb", theDtb.file.toString()))
if (File(theDtb.file + ".${dtsSuffix}").exists()) {
if (File(theDtb.file + ".0.${dtsSuffix}").exists()) {
it.addRow("\\-- decompiled dts [$dtbCount]", theDtb.file + ".*.${dtsSuffix}")
prints.add(Pair("\\-- decompiled dts [$dtbCount]", theDtb.file + ".*.${dtsSuffix}"))
}
Expand Down Expand Up @@ -456,7 +456,7 @@ data class BootV2(
}
//refresh dtb size
dtb?.let { theDtb ->
if (File(theDtb.file!! + ".${dtsSuffix}").exists()) {
if (File(theDtb.file!! + ".0.${dtsSuffix}").exists()) {
DTC.packMultiple(theDtb.file!!, theDtb.dtbList)
}
theDtb.size = File(theDtb.file!!).length().toInt()
Expand Down
8 changes: 4 additions & 4 deletions bbootimg/src/main/kotlin/bootimg/v3/VendorBoot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import cfig.helper.Dumpling
import cfig.helper.Helper
import cfig.helper.ZipHelper
import cfig.packable.VBMetaParser
import cfig.utils.DTC
import rom.fdt.DTC
import cfig.utils.EnvironmentVerifier
import com.fasterxml.jackson.databind.ObjectMapper
import de.vandermeer.asciitable.AsciiTable
Expand Down Expand Up @@ -258,7 +258,7 @@ data class VendorBoot(
}
}
//update dtb
if (File(this.dtb.file + ".${dtsSuffix}").exists()) {
if (File(this.dtb.file + ".0.${dtsSuffix}").exists()) {
DTC.packMultiple(this.dtb.file, this.dtb.dtbList)
}
this.dtb.size = File(this.dtb.file).length().toInt()
Expand Down Expand Up @@ -368,7 +368,7 @@ data class VendorBoot(
}
//dtb
run {
C.dumpDtb(Helper.Slice(info.output, dtb.position.toInt(), dtb.size, dtb.file))
C.dumpDtb(Helper.Slice(info.output, dtb.position.toInt(), dtb.size, dtb.file), false)
if (dtb.size > 0) {
dtb.dtbList = DTC.parseMultiple(dtb.file)
DTC.extractMultiple(dtb.file, dtb.dtbList)
Expand Down Expand Up @@ -438,7 +438,7 @@ data class VendorBoot(
if (this.dtb.size > 0) {
it.addRow("dtb", this.dtb.file)
prints.add(Pair("dtb", this.dtb.file))
if (File(this.dtb.file + ".${dtsSuffix}").exists()) {
if (File(this.dtb.file + ".0.${dtsSuffix}").exists()) {
it.addRow("\\-- decompiled dts [${dtb.dtbList.size}]", dtb.file + "*.${dtsSuffix}")
prints.add(Pair("\\-- decompiled dts [${dtb.dtbList.size}]", dtb.file + "*.${dtsSuffix}"))
}
Expand Down
5 changes: 3 additions & 2 deletions bbootimg/src/main/kotlin/packable/BootImgParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import cfig.bootimg.v2.BootV2
import cfig.bootimg.v2.BootV2Dialects
import cfig.bootimg.v3.BootV3
import cfig.helper.Helper
import cfig.helper.Helper.Companion.deleteIfExists
import com.fasterxml.jackson.databind.ObjectMapper
import de.vandermeer.asciitable.AsciiTable
import org.slf4j.LoggerFactory
import java.io.File
import java.io.FileInputStream
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists

class BootImgParser : IPackable {
override val loopNo: Int
Expand Down Expand Up @@ -142,7 +143,7 @@ class BootImgParser : IPackable {
fun clear(fileName: String) {
super.clear()
listOf("", ".clear", ".google", ".clear", ".signed", ".signed2").forEach {
"$fileName$it".deleteIfExists()
Path("$fileName$it").deleteIfExists()
}
VBMetaParser().clear("vbmeta.img")
}
Expand Down
7 changes: 4 additions & 3 deletions bbootimg/src/main/kotlin/packable/DeviceTreeParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ 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 rom.fdt.DTC
import java.io.File
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists

class DeviceTreeParser : IPackable {
override fun capabilities(): List<String> {
Expand Down Expand Up @@ -74,7 +75,7 @@ class DeviceTreeParser : IPackable {
fun clear(fileName: String) {
super.clear()
listOf("", ".new").forEach {
"$fileName$it".deleteIfExists()
Path("$fileName$it").deleteIfExists()
}
}

Expand Down
9 changes: 5 additions & 4 deletions bbootimg/src/main/kotlin/packable/DtboParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
package cfig.packable

import cfig.helper.Helper
import cfig.helper.Helper.Companion.deleteIfExists
import cfig.utils.DTC
import cfig.utils.EnvironmentVerifier
import com.fasterxml.jackson.databind.ObjectMapper
import org.apache.commons.exec.CommandLine
import org.apache.commons.exec.DefaultExecutor
import org.slf4j.LoggerFactory
import utils.Dtbo
import rom.fdt.DTC
import rom.fdt.Dtbo
import java.io.File
import java.io.FileInputStream
import java.util.*
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists

class DtboParser(val workDir: File) : IPackable {
override val loopNo: Int
Expand Down Expand Up @@ -78,7 +79,7 @@ class DtboParser(val workDir: File) : IPackable {
fun clear(fileName: String) {
super.clear()
listOf("", ".clear", ".google", ".clear", ".signed", ".signed2").forEach {
"$fileName$it".deleteIfExists()
Path("$fileName$it").deleteIfExists()
}
VBMetaParser().clear("vbmeta.img")
}
Expand Down
9 changes: 6 additions & 3 deletions bbootimg/src/main/kotlin/packable/IPackable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ package cfig.packable

import avb.AVBInfo
import cfig.Avb
import cfig.helper.Helper
import cfig.helper.Dumpling
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 org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists

interface IPackable {
val loopNo: Int
Expand Down Expand Up @@ -79,7 +80,9 @@ interface IPackable {
File(workDir).deleteRecursively()
}
File(workDir).mkdirs()
"uiderrors".deleteIfExists()
// java.nio.file.Files.deleteIfExists() will throw exception on Windows platform, so use java.io.File
// Caused by: java.nio.file.FileSystemException: uiderrors: The process cannot access the file because it is being used by another process
File("uiderrors").deleteOnExit()
}

companion object {
Expand Down
7 changes: 4 additions & 3 deletions bbootimg/src/main/kotlin/packable/MiscImgParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
package cfig.packable

import cfig.helper.Helper
import rom.misc.MiscImage
import cfig.helper.Helper.Companion.deleteIfExists
import com.fasterxml.jackson.databind.ObjectMapper
import org.slf4j.LoggerFactory
import rom.misc.MiscImage
import java.io.File
import java.io.RandomAccessFile
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists

class MiscImgParser : IPackable {
override val loopNo: Int
Expand Down Expand Up @@ -78,7 +79,7 @@ class MiscImgParser : IPackable {
fun clear(fileName: String) {
super.clear()
listOf("", ".new").forEach {
"$fileName$it".deleteIfExists()
Path("$fileName$it").deleteIfExists()
}
}

Expand Down
5 changes: 3 additions & 2 deletions bbootimg/src/main/kotlin/packable/PayloadBinParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
package cfig.packable

import cc.cfig.droid.ota.Payload
import cfig.helper.Helper.Companion.deleteIfExists
import org.slf4j.LoggerFactory
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists

class PayloadBinParser : IPackable {
override val loopNo: Int = 0
Expand Down Expand Up @@ -47,7 +48,7 @@ class PayloadBinParser : IPackable {

fun clear(fileName: String) {
super.clear()
fileName.deleteIfExists()
Path(fileName).deleteIfExists()
}

override fun flash(fileName: String, deviceName: String) {
Expand Down
5 changes: 3 additions & 2 deletions bbootimg/src/main/kotlin/packable/VBMetaParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import avb.AVBInfo
import cfig.Avb
import cfig.helper.Dumpling
import cfig.helper.Helper
import cfig.helper.Helper.Companion.deleteIfExists
import com.fasterxml.jackson.databind.ObjectMapper
import org.slf4j.LoggerFactory
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardOpenOption
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists

class VBMetaParser : IPackable {
override val loopNo: Int
Expand Down Expand Up @@ -66,7 +67,7 @@ class VBMetaParser : IPackable {
fun clear(fileName: String) {
super.clear()
listOf("", ".signed").forEach {
"$fileName$it".deleteIfExists()
Path("$fileName$it").deleteIfExists()
}
}

Expand Down
5 changes: 3 additions & 2 deletions bbootimg/src/main/kotlin/packable/VendorBootParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
package cfig.packable

import cfig.bootimg.v3.VendorBoot
import cfig.helper.Helper.Companion.deleteIfExists
import com.fasterxml.jackson.databind.ObjectMapper
import org.slf4j.LoggerFactory
import java.io.File
import kotlin.io.path.Path
import kotlin.io.path.deleteIfExists

class VendorBootParser : IPackable {
override val loopNo: Int = 0
Expand Down Expand Up @@ -59,7 +60,7 @@ class VendorBootParser : IPackable {
fun clear(fileName: String) {
super.clear()
listOf("", ".clear", ".google", ".clear", ".signed", ".signed2").forEach {
"$fileName$it".deleteIfExists()
Path("$fileName$it").deleteIfExists()
}
VBMetaParser().clear("vbmeta.img")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cfig.utils
package rom.fdt

import cc.cfig.io.Struct
import cfig.helper.Dumpling
import cfig.helper.Helper
import cfig.utils.EnvironmentVerifier
import org.apache.commons.exec.CommandLine
import org.apache.commons.exec.DefaultExecutor
import org.slf4j.LoggerFactory
Expand All @@ -34,7 +35,7 @@ class DTC {
)

data class FdtHeader(
var magic: Int = 0,
var magic: Long = 0L,
val totalsize: Int = 0,
val offDtStruct: Int = 0,
val offDtStrings: Int = 0,
Expand All @@ -47,7 +48,7 @@ class DTC {
) {
companion object {
private const val MAGIC = 0xd00dfeedu
const val FORMAT_STRING = ">10i"
const val FORMAT_STRING = ">I9i"
const val SIZE = 40

init {
Expand All @@ -58,7 +59,7 @@ class DTC {
fun parse(iS: InputStream): FdtHeader {
val info = Struct(FORMAT_STRING).unpack(iS)
val ret = FdtHeader(
info[0] as Int,
(info[0] as UInt).toLong(),
info[1] as Int,
info[2] as Int,
info[3] as Int,
Expand Down Expand Up @@ -150,9 +151,9 @@ class DTC {
}
val remainder = File(fileName).length() - ret.sumOf { it.header.totalsize }.toLong()
if (remainder == 0L) {
log.info("Successfully parsed ${ret.size} FDT headers")
log.info("Successfully parsed ${ret.size} FDT headers from $fileName")
} else {
log.warn("Successfully parsed ${ret.size} FDT headers, remainder: $remainder bytes")
log.warn("Successfully parsed ${ret.size} FDT headers from $fileName, remainder: $remainder bytes")
}
return ret
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package rom.fdt

import avb.AVBInfo
import cc.cfig.io.Struct
Expand All @@ -8,7 +8,6 @@ import cfig.bootimg.Signer
import cfig.helper.Dumpling
import cfig.helper.Helper
import cfig.packable.VBMetaParser
import cfig.utils.DTC
import com.fasterxml.jackson.databind.ObjectMapper
import de.vandermeer.asciitable.AsciiTable
import org.slf4j.LoggerFactory
Expand Down Expand Up @@ -59,8 +58,8 @@ class Dtbo(
}
totalSize = info[1] as Int
headerSize = info[2] as Int
if (headerSize != DtboHeader.SIZE) {
log.warn("headerSize $headerSize != ${DtboHeader.SIZE}")
if (headerSize != SIZE) {
log.warn("headerSize $headerSize != $SIZE")
}
entrySize = info[3] as Int
if (entrySize != DeviceTreeTableEntry.SIZE) {
Expand Down Expand Up @@ -186,7 +185,7 @@ class Dtbo(
.toInt()
// Part II - a
for (index in 0 until dtEntries.size) {
DTC().compile("${outDir}dt/dt.${index}.${dtsSuffix}", "${outDir}dt/dt.${index}")
DTC().compile("${outDir}dt/dt.${index}.$dtsSuffix", "${outDir}dt/dt.${index}")
}
// Part II - b
var offset = DtboHeader.SIZE + (header.entryCount * DeviceTreeTableEntry.SIZE)
Expand Down Expand Up @@ -221,7 +220,7 @@ class Dtbo(
it.addRow("image info", outDir + info.output.removeSuffix(".img") + ".json")
it.addRule()
it.addRow("device-tree blob (${this.header.entryCount} blobs)", "${outDir}dt/dt.*")
it.addRow("\\-- device-tree source ", "${outDir}dt/dt.*.${dtsSuffix}")
it.addRow("\\-- device-tree source ", "${outDir}dt/dt.*.$dtsSuffix")
it.addRule()
it.addRow("AVB info", Avb.getJsonFileName(info.output))
it.addRule()
Expand Down
Loading

0 comments on commit 94570b1

Please sign in to comment.