moto-flash-data/flashpart/flashpart.go
2025-01-20 13:10:35 +01:00

58 lines
1.2 KiB
Go

package flashpart
import "encoding/binary"
type FlashPartHeader struct {
MediaID byte
Byte1 byte
Byte2 byte
Byte3 byte
TotalSize uint32
RawSize uint32
VersionText string // 16byte
raw []byte
}
func ParseFlashPartHeader(data []byte) (fph *FlashPartHeader) {
if len(data) != 32 {
panic("flash part header len != 32")
}
fph = &FlashPartHeader{
MediaID: data[0],
Byte1: data[1],
Byte2: data[2],
Byte3: data[3],
TotalSize: binary.LittleEndian.Uint32(data[4:8]),
VersionText: string(data[15:]),
raw: data,
}
fph.RawSize = fph.TotalSize - 32
// copyright string in header space
if fph.MediaID == 67 && fph.Byte1 == 111 && fph.Byte2 == 112 && fph.Byte3 == 121 {
fph.VersionText = string(data[:])
fph.TotalSize = 0
fph.RawSize = 0
}
return fph
}
func (fph *FlashPartHeader) AdjustRawSize(size uint32) {
fph.RawSize = size
fph.AdjustTotalSize(size + 32)
}
func (fph *FlashPartHeader) AdjustTotalSize(size uint32) {
fph.TotalSize = size
totalSizeBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(totalSizeBytes, fph.TotalSize)
// TODO: shitty copy
for i, b := range totalSizeBytes {
fph.raw[4+i] = b
}
}
func (fph *FlashPartHeader) GetBytes() []byte {
return fph.raw
}