Optionally add size of pre loaded app 0 when generating partition table

This commit is contained in:
Mikael Ågren 2025-04-04 19:11:08 +02:00
parent 8347ae7c8e
commit da275acebc
No known key found for this signature in database
GPG key ID: E02DA3D397792C46

View file

@ -2,7 +2,6 @@ package main
import ( import (
"encoding/binary" "encoding/binary"
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"io" "io"
@ -41,84 +40,38 @@ type PartTableStorage struct {
type Flash struct { type Flash struct {
Bitstream [0x20000]uint8 Bitstream [0x20000]uint8
PartitionTableStorage PartTableStorage PartitionTableStorage PartTableStorage
PartitionTablePadding [64*1024 - 365]uint8 PartitionTablePadding [64*1024 - 349]uint8
PreLoadedApp0 [0x20000]uint8 PreLoadedApp0 [0x20000]uint8
PreLoadedApp1 [0x20000]uint8 PreLoadedApp1 [0x20000]uint8
AppStorage [4][0x20000]uint8 AppStorage [4][0x20000]uint8
EndPadding [0x10000]uint8 EndPadding [0x10000]uint8
} }
func readPartTableStorage(filename string) PartTableStorage { func readStruct[T PartTableStorage | Flash](filename string) T {
var storage PartTableStorage var s T
tblFile, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {
panic(err) panic(err)
} }
if err := binary.Read(tblFile, binary.LittleEndian, &storage); err != nil { if err := binary.Read(file, binary.LittleEndian, &s); err != nil {
fmt.Println("binary.Read failed:", err) panic(err)
} }
tblStructLen, _ := tblFile.Seek(0, io.SeekCurrent) sLen, err := file.Seek(0, io.SeekCurrent)
fmt.Fprintf(os.Stderr, "INFO: Go partition table struct is %d byte long\n", tblStructLen)
return storage
}
func readPartTable(filename string) PartTable {
var tbl PartTable
tblFile, err := os.Open(filename)
if err != nil { if err != nil {
panic(err) panic(err)
} }
if err := binary.Read(tblFile, binary.LittleEndian, &tbl); err != nil { fmt.Fprintf(os.Stderr, "INFO: %T struct is %d byte long\n", *new(T), sLen)
fmt.Println("binary.Read failed:", err)
return s
} }
tblStructLen, _ := tblFile.Seek(0, io.SeekCurrent) func printPartTableStorageCondensed(storage PartTableStorage) {
fmt.Fprintf(os.Stderr, "INFO: Go partition table struct is %d byte long\n", tblStructLen) fmt.Printf("Partition Table Storage\n")
fmt.Printf(" Partition Table\n")
return tbl
}
func readFlashDump(filename string) Flash {
var flash Flash
flashFile, err := os.Open(filename)
if err != nil {
panic(err)
}
if err := binary.Read(flashFile, binary.LittleEndian, &flash); err != nil {
fmt.Println("binary.Read failed:", err)
}
flashStructLen, _ := flashFile.Seek(0, io.SeekCurrent)
fmt.Fprintf(os.Stderr, "INFO: Go flash table struct is %d Mbyte long\n", flashStructLen/1024/1024)
return flash
}
func printPartTableStorageJson(storage PartTableStorage) {
json, err := json.MarshalIndent(storage, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("%s\n", string(json))
}
func printPartTableJson(tbl PartTable) {
partTableJSON, err := json.MarshalIndent(tbl, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("%s\n", string(partTableJSON))
}
func printPartTableCondensed(storage PartTableStorage) {
fmt.Printf(" Header\n") fmt.Printf(" Header\n")
fmt.Printf(" Version : %d\n", storage.PartTable.Version) fmt.Printf(" Version : %d\n", storage.PartTable.Version)
@ -132,24 +85,24 @@ func printPartTableCondensed(storage PartTableStorage) {
fmt.Printf(" %x\n", appData.Signature[32:48]) fmt.Printf(" %x\n", appData.Signature[32:48])
fmt.Printf(" %x\n", appData.Signature[48:]) fmt.Printf(" %x\n", appData.Signature[48:])
} }
} fmt.Printf(" Digest : %x\n", storage.Digest)
func printPartTableStorageCondensed(storage PartTableStorage) {
} }
func calculateStorageDigest(data []byte) []byte { func calculateStorageDigest(data []byte) []byte {
key := [16]byte{} key := [16]byte{}
hash, err := blake2s.New128(key[:]) hash, err := blake2s.New128(key[:])
if err != nil { if err != nil {
panic(err) panic(err)
} }
hash.Write(data) hash.Write(data)
digest := hash.Sum([]byte{}) digest := hash.Sum([]byte{})
return digest return digest
} }
func generatePartTableStorage(filename string) { func generatePartTableStorage(outputFilename string, app0Filename string) {
storage := PartTableStorage{ storage := PartTableStorage{
PartTable: PartTable{ PartTable: PartTable{
Version: 1, Version: 1,
@ -158,54 +111,62 @@ func generatePartTableStorage(filename string) {
}, },
} }
if app0Filename != "" {
stat, err := os.Stat(app0Filename)
if err != nil {
panic(err)
}
storage.PartTable.PreLoadedAppData[0].Size = uint32(stat.Size())
}
buf := make([]byte, 4096, 4096) buf := make([]byte, 4096, 4096)
len, err := binary.Encode(buf, binary.LittleEndian, storage.PartTable) len, err := binary.Encode(buf, binary.LittleEndian, storage.PartTable)
if err != nil { if err != nil {
panic(err) panic(err)
} }
fmt.Printf("buf - len: %d, data: %x\n", len, buf[:len])
digest := calculateStorageDigest(buf[:len]) digest := calculateStorageDigest(buf[:len])
copy(storage.Digest[:], digest) copy(storage.Digest[:], digest)
fmt.Printf("digest: %x\n", digest)
storageFile, err := os.Create(filename) storageFile, err := os.Create(outputFilename)
if err != nil { if err != nil {
panic(err) panic(err)
} }
if err := binary.Write(storageFile, binary.LittleEndian, storage); err != nil { if err := binary.Write(storageFile, binary.LittleEndian, storage); err != nil {
fmt.Println("binary.Write failed:", err) panic(err)
} }
} }
func main() { func main() {
filename := "" input := ""
json := false output := ""
app0 := ""
flash := false flash := false
flag.StringVar(&filename, "i", "", "Partition table binary dump file.") flag.StringVar(&input, "i", "", "Input binary dump file. Cannot be used with -o.")
flag.BoolVar(&json, "j", false, "Print partition table in JSON format.") flag.StringVar(&output, "o", "", "Output binary dump file. Cannot be used with -i.")
flag.StringVar(&app0, "app0", "", "Binary in pre loaded app slot 0. Can be used with -o.")
flag.BoolVar(&flash, "f", false, "Treat input file as a dump of the entire flash memory.") flag.BoolVar(&flash, "f", false, "Treat input file as a dump of the entire flash memory.")
flag.Parse() flag.Parse()
if filename == "" { if (input == "" && output == "") || (input != "" && output != "") {
flag.Usage() flag.Usage()
os.Exit(1) os.Exit(1)
} }
generatePartTableStorage(filename) if input != "" {
return var storage PartTableStorage
// var tbl PartTable if flash {
storage = readStruct[Flash](input).PartitionTableStorage
// if flash { } else {
// tbl = readFlashDump(filename).PartitionTable storage = readStruct[PartTableStorage](input)
// } else { }
// tbl = readPartTable(filename) printPartTableStorageCondensed(storage)
// } } else if output != "" {
generatePartTableStorage(output, app0)
// if json { }
// printPartTableJson(tbl)
// } else {
// printPartTableCondensed(tbl)
// }
} }