added s19-isolate-all-part

master
cheetah 1 month ago
parent f2b0019eeb
commit c3f58da995

@ -20,9 +20,10 @@ type BaseCommand struct {
MparUnpack MparUnpackCommand `command:"mpar-unpack" description:"Extracts the Raw Binary Data from an Partition-File"`
MparPack MparPackCommand `command:"mpar-pack" description:"Creates a new Partition-File from Raw Binary-Data"`
S19Tree S19TreeCommand `command:"s19-tree" description:"Dump the S19 Record Tree and seperate Memory Regions"`
S19IsolatePart S19IsolatePartitionCommand `command:"s19-isolate-part" description:"Isolate a Partition from an S19-File"`
S19FromMpar S19FromMparCommand `command:"s19-from-mpar" description:"Creates a new S19-File from a Partition-File"`
S19Tree S19TreeCommand `command:"s19-tree" description:"Dump the S19 Record Tree and seperate Memory Regions"`
S19IsolatePart S19IsolatePartitionCommand `command:"s19-isolate-part" description:"Isolate a Partition from an S19-File"`
S19IsolateAllPartitions S19IsolateAllPartitionsCommand `command:"s19-isolate-all-part" description:"Isolate all available Partitions from an S19-File"`
S19FromMpar S19FromMparCommand `command:"s19-from-mpar" description:"Creates a new S19-File from a Partition-File"`
MparPaxList MparPaxListCommand `command:"mpar-pax-list" description:"prints the contents of an pax archive within an mpar"`
MparPaxExtract MparPaxExtractCommand `command:"mpar-pax-extract" description:"extracts a single file from an pax'd mpar file"`

@ -0,0 +1,91 @@
package commands
import (
"fmt"
"os"
"path"
"git.cheetah.cat/cheetah/moto-flash-data/flashpart"
"git.cheetah.cat/cheetah/moto-flash-data/s19"
"github.com/rs/zerolog/log"
)
type S19IsolateAllPartitionsCommand struct {
FileName string `long:"file" short:"f" required:"true" description:"s19 filename"`
OutputDir string `long:"out" short:"o" required:"true" description:"output dir, format:dir+mpar/bin"`
}
func (command *S19IsolateAllPartitionsCommand) Execute(args []string) error {
log.Info().Str("fileName", command.FileName).Msg("S19-Isolate")
s19File, err := s19.NewS19Reader(command.FileName)
if err != nil {
return err
}
defer s19File.Close()
log.Info().Int64("fileSize", s19File.OsSize).Msg("Reading file...")
//
records, err := s19File.ReadAllRecords()
if err != nil {
return err
}
records = records[1:] // skip first ??
ranges := s19File.DetectAddressRanges(records)
//
err = os.MkdirAll(path.Join("s19-dump", path.Base(command.FileName)), 0700)
if err != nil {
return err
}
for index, segment := range ranges {
log.Info().Msg(
fmt.Sprintf("Index: %d, Start: 0x%08X, End: 0x%08X, Size: %d bytes, Slice: %d-%d",
index,
segment.StartAddress, segment.EndAddress,
segment.Size,
segment.SliceStart, segment.SliceEnd,
),
)
//
fpHeader := flashpart.ParseFlashPartHeader(records[segment.SliceStart].Data)
log.Info().Msg(
fmt.Sprintf("Index: %d, Media-ID: 0x%02X, Partition Size: 0x%08X / %d bytes, Partition Tag: %s",
index,
fpHeader.MediaID,
fpHeader.TotalSize,
fpHeader.TotalSize,
fpHeader.VersionText,
),
)
if fpHeader.TotalSize == 0 {
log.Warn().Msg("cannot extract a partition thats empty")
continue
}
//
{
outMBarFile, err := os.Create(path.Join("s19-dump", path.Base(command.FileName), fmt.Sprintf("part%d.mpar", index)))
if err != nil {
return err
}
defer outMBarFile.Close()
for _, record := range records[segment.SliceStart : segment.SliceEnd+1] {
outMBarFile.Write(record.Data)
}
// if we have more than just the partition header, write a convenient bin file
if segment.SliceStart+1 < segment.SliceEnd {
outBinFile, err := os.Create(path.Join("s19-dump", path.Base(command.FileName), fmt.Sprintf("part%d.bin", index)))
if err != nil {
return err
}
defer outBinFile.Close()
for _, record := range records[segment.SliceStart+1 : segment.SliceEnd+1] {
outBinFile.Write(record.Data)
}
}
}
}
return nil
}
Loading…
Cancel
Save