You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.1 KiB
Go
87 lines
2.1 KiB
Go
package commands
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"git.cheetah.cat/cheetah/moto-flash-data/flashpart"
|
|
"git.cheetah.cat/cheetah/moto-flash-data/s19"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
type S19IsolatePartitionCommand struct {
|
|
FileName string `long:"file" short:"f" required:"true" description:"s19 filename"`
|
|
PartitionIndex *int `long:"part-index" short:"i" description:"partition-number"`
|
|
|
|
OutputFileName string `long:"out" short:"o" required:"true" description:"output-filename, format:mpar"`
|
|
}
|
|
|
|
func (command *S19IsolatePartitionCommand) 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)
|
|
//
|
|
//
|
|
if command.PartitionIndex != nil {
|
|
partIndex := *command.PartitionIndex
|
|
for index, segment := range ranges {
|
|
if index != partIndex {
|
|
continue
|
|
}
|
|
//
|
|
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.Error().Msg("cannot extract a partition thats empty")
|
|
// return nil
|
|
// }
|
|
//
|
|
{
|
|
outMBarFile, err := os.Create(command.OutputFileName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer outMBarFile.Close()
|
|
for _, record := range records[segment.SliceStart : segment.SliceEnd+1] {
|
|
outMBarFile.Write(record.Data)
|
|
}
|
|
}
|
|
break
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
}
|