This is an automated email from the ASF dual-hosted git repository. ccollins pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mynewt-imgmod.git
commit 14683ca04c0778be383806534ef46d6f8f65e977 Author: Christopher Collins <ccoll...@apache.org> AuthorDate: Fri Feb 28 16:26:41 2020 -0800 New command: image setbody --- cli/image_cmds.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ iimg/iimg.go | 7 +++++++ 2 files changed, 56 insertions(+) diff --git a/cli/image_cmds.go b/cli/image_cmds.go index 882277b..5aa1315 100644 --- a/cli/image_cmds.go +++ b/cli/image_cmds.go @@ -591,6 +591,42 @@ func runEncryptFullCmd(cmd *cobra.Command, args []string) { } } +func runSetBodyCmd(cmd *cobra.Command, args []string) { + if len(args) < 2 { + ImgmodUsage(cmd, nil) + } + + imgFilename := args[0] + bodyFilename := args[1] + + if bodyFilename == "" { + ImgmodUsage(cmd, errors.Errorf("image body required")) + } + bodyBytes, err := ioutil.ReadFile(bodyFilename) + if err != nil { + ImgmodUsage(cmd, errors.Wrapf(err, "error reading image body file")) + } + + outFilename, err := CalcOutFilename(imgFilename) + if err != nil { + ImgmodUsage(cmd, err) + } + + img, err := readImage(imgFilename) + if err != nil { + ImgmodUsage(cmd, err) + } + + img, err = iimg.ReplaceBody(img, bodyBytes) + if err != nil { + ImgmodUsage(nil, err) + } + + if err := writeImage(img, outFilename); err != nil { + ImgmodUsage(nil, err) + } +} + func runVerifyCmd(cmd *cobra.Command, args []string) { if len(args) < 1 { ImgmodUsage(cmd, nil) @@ -820,6 +856,19 @@ func AddImageCommands(cmd *cobra.Command) { imageCmd.AddCommand(encryptFullCmd) + setBodyCmd := &cobra.Command{ + Use: "setbody <image> <body-filename>", + Short: "Replaces an image's body with a file's contents", + Run: runSetBodyCmd, + } + + setBodyCmd.PersistentFlags().StringVarP(&OptOutFilename, "outfile", "o", + "", "File to write to") + setBodyCmd.PersistentFlags().BoolVarP(&OptInPlace, "inplace", "i", false, + "Replace input file") + + imageCmd.AddCommand(setBodyCmd) + verifyCmd := &cobra.Command{ Use: "verify <image>", Short: "Verifies an Mynewt image's integrity", diff --git a/iimg/iimg.go b/iimg/iimg.go index 9fc53fd..72e86b3 100644 --- a/iimg/iimg.go +++ b/iimg/iimg.go @@ -220,3 +220,10 @@ func EncryptImageFull(img image.Image, return img, nil } + +func ReplaceBody(img image.Image, body []byte) (image.Image, error) { + delta := len(body) - len(img.Body) + img.Body = body + img.Header.ImgSz += uint32(delta) + return img, nil +}