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.

71 lines
1.6 KiB
Go

package commands
import (
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"git.cheetah.cat/cheetah/moto-flash-data/flashpart"
"github.com/rs/zerolog/log"
)
type MparUnpackCommand struct {
SourceFileName string `long:"src" short:"i" required:"true" description:"input-filename"`
DestFileName string `long:"dest" short:"o" required:"true" description:"output-filename"`
}
func (command *MparUnpackCommand) Execute(args []string) error {
inputFile, err := os.Open(command.SourceFileName)
if err != nil {
return err
}
defer inputFile.Close()
outputFile, err := os.Create(command.DestFileName)
if err != nil {
return err
}
defer outputFile.Close()
inputFileStat, err := inputFile.Stat()
if err != nil {
return err
}
log.Info().Int64("fileSize", inputFileStat.Size()).Msg("Reading raw file...")
pdata := make([]byte, inputFileStat.Size())
_, err = io.ReadFull(inputFile, pdata)
if err != nil {
return err
}
fpHeader := flashpart.ParseFlashPartHeader(pdata[:32])
log.Info().Msg(
fmt.Sprintf("Media-ID: 0x%02X, Partition Size: 0x%08X / %d bytes, Partition Tag: %s",
fpHeader.MediaID,
fpHeader.TotalSize,
fpHeader.TotalSize,
fpHeader.VersionText,
),
)
log.Info().Msg(
fmt.Sprintf("Original-Full-Header-Hex: %s", hex.EncodeToString(pdata[:32])),
)
if len(pdata[32:]) != len(pdata)-32 {
return errors.New("something doesnt add up")
}
if len(pdata[32:]) != int(fpHeader.TotalSize)-32 {
log.Error().Uint32("header ts", fpHeader.TotalSize).Uint32("len pdata", uint32(len(pdata))).Msg("size mismatch")
return nil
}
_, err = outputFile.Write(pdata[32:])
if err != nil {
return err
}
return nil
}