You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by vi...@apache.org on 2019/04/04 01:49:06 UTC

[mynewt-newtmgr] branch master updated: Allow upgrade-only requests

This is an automated email from the ASF dual-hosted git repository.

vipulrahane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newtmgr.git


The following commit(s) were added to refs/heads/master by this push:
     new 28df3b0  Allow upgrade-only requests
     new 61ff2b0  Merge pull request #120 from ccollins476ad/image-upgrade
28df3b0 is described below

commit 28df3b03854ae4c4f0cafa3528370867298ff75d
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Tue Apr 2 14:24:46 2019 -0700

    Allow upgrade-only requests
    
    This commit adds an additional boolean field to the "image upload"
    request: `upgrade`.  If this field is true, the firmware will only
    accept an upload attempt if the new image's version number is greater
    than that of the currently running image (using a semver-compatible
    comparision).
    
    If the `upgrade` field is false or missing, no version check is
    performed.
    
    The firmware only processes the `upgrade` field on the first image
    chunk.  It is harmless, but pointless, for a client to specify `upgrade`
    in any subsequent chunks.
---
 newtmgr/cli/image.go |  6 ++++++
 nmxact/nmp/image.go  | 19 +++++++++----------
 nmxact/xact/image.go | 30 +++++++++++++++++++-----------
 3 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/newtmgr/cli/image.go b/newtmgr/cli/image.go
index 605ec8f..d432f7f 100644
--- a/newtmgr/cli/image.go
+++ b/newtmgr/cli/image.go
@@ -43,6 +43,7 @@ var (
 )
 
 var noerase bool
+var upgrade bool
 
 func imageFlagsStr(image nmp.ImageStateEntry) string {
 	strs := []string{}
@@ -189,6 +190,7 @@ func imageUploadCmd(cmd *cobra.Command, args []string) {
 	if noerase == true {
 		c.NoErase = true
 	}
+	c.Upgrade = upgrade
 	c.ProgressBar = pb.StartNew(len(imageFile))
 	c.ProgressBar.SetUnits(pb.U_BYTES)
 	c.ProgressBar.ShowSpeed = true
@@ -397,6 +399,10 @@ func imageCmd() *cobra.Command {
 	uploadCmd.PersistentFlags().BoolVarP(&noerase,
 		"noerase", "e", false,
 		"Don't send specific image erase command to start with")
+	uploadCmd.PersistentFlags().BoolVarP(&upgrade,
+		"upgrade", "u", false,
+		"Only allow the upload if the new image's version is greater than "+
+			"that of the currently running image")
 	imageCmd.AddCommand(uploadCmd)
 
 	coreListCmd := &cobra.Command{
diff --git a/nmxact/nmp/image.go b/nmxact/nmp/image.go
index 4bc9436..a9e1461 100644
--- a/nmxact/nmp/image.go
+++ b/nmxact/nmp/image.go
@@ -19,18 +19,17 @@
 
 package nmp
 
-import ()
-
 //////////////////////////////////////////////////////////////////////////////
 // $upload                                                                  //
 //////////////////////////////////////////////////////////////////////////////
 
 type ImageUploadReq struct {
-	NmpBase         `codec:"-"`
-	Off      uint32 `codec:"off"`
-	Len      uint32 `codec:"len,omitempty"`
-	DataSha  []byte `codec:"sha,omitempty"`
-	Data     []byte `codec:"data"`
+	NmpBase `codec:"-"`
+	Off     uint32 `codec:"off"`
+	Len     uint32 `codec:"len,omitempty"`
+	DataSha []byte `codec:"sha,omitempty"`
+	Upgrade bool   `codec:"upgrade,omitempty"`
+	Data    []byte `codec:"data"`
 }
 
 type ImageUploadRsp struct {
@@ -97,7 +96,7 @@ type ImageStateReadReq struct {
 }
 
 type ImageStateWriteReq struct {
-	NmpBase        `codec:"-"`
+	NmpBase `codec:"-"`
 	Hash    []byte `codec:"hash"`
 	Confirm bool   `codec:"confirm"`
 }
@@ -163,8 +162,8 @@ func (r *CoreListRsp) Msg() *NmpMsg { return MsgFromReq(r) }
 //////////////////////////////////////////////////////////////////////////////
 
 type CoreLoadReq struct {
-	NmpBase    `codec:"-"`
-	Off uint32 `codec:"off"`
+	NmpBase `codec:"-"`
+	Off     uint32 `codec:"off"`
 }
 
 type CoreLoadRsp struct {
diff --git a/nmxact/xact/image.go b/nmxact/xact/image.go
index f8791d8..534856e 100644
--- a/nmxact/xact/image.go
+++ b/nmxact/xact/image.go
@@ -41,6 +41,7 @@ type ImageUploadCmd struct {
 	CmdBase
 	Data       []byte
 	StartOff   int
+	Upgrade    bool
 	ProgressCb ImageUploadProgressFn
 }
 
@@ -66,7 +67,7 @@ func (r *ImageUploadResult) Status() int {
 	}
 }
 
-func buildImageUploadReq(imageSz int, hash []byte, chunk []byte,
+func buildImageUploadReq(imageSz int, hash []byte, upgrade bool, chunk []byte,
 	off int) *nmp.ImageUploadReq {
 
 	r := nmp.NewImageUploadReq()
@@ -74,6 +75,7 @@ func buildImageUploadReq(imageSz int, hash []byte, chunk []byte,
 	if off == 0 {
 		r.Len = uint32(imageSz)
 		r.DataSha = hash
+		r.Upgrade = upgrade
 	}
 	r.Off = uint32(off)
 	r.Data = chunk
@@ -88,11 +90,14 @@ func min(a, b int) int {
 	return b
 }
 
-func findChunkLen(s sesn.Sesn, hash []byte, data []byte, off int) (int, error) {
+func findChunkLen(s sesn.Sesn, hash []byte, upgrade bool, data []byte,
+	off int) (int, error) {
+
 	// Let's start by encoding max allowed chunk len and we will see how many
 	// bytes we need to cut
-	chunklen := min(len(data) - off, IMAGE_UPLOAD_MAX_CHUNK)
-	r := buildImageUploadReq(len(data), hash, data[off:off+chunklen], off)
+	chunklen := min(len(data)-off, IMAGE_UPLOAD_MAX_CHUNK)
+	r := buildImageUploadReq(len(data), hash, upgrade, data[off:off+chunklen],
+		off)
 	enc, err := mgmt.EncodeMgmt(s, r.Msg())
 	if err != nil {
 		return 0, err
@@ -107,7 +112,7 @@ func findChunkLen(s sesn.Sesn, hash []byte, data []byte, off int) (int, error) {
 	return chunklen, nil
 }
 
-func nextImageUploadReq(s sesn.Sesn, data []byte, off int) (
+func nextImageUploadReq(s sesn.Sesn, upgrade bool, data []byte, off int) (
 	*nmp.ImageUploadReq, error) {
 	var hash []byte = nil
 
@@ -118,7 +123,7 @@ func nextImageUploadReq(s sesn.Sesn, data []byte, off int) (
 	}
 
 	// Find chunk length
-	chunklen, err := findChunkLen(s, hash, data, off)
+	chunklen, err := findChunkLen(s, hash, upgrade, data, off)
 	if err != nil {
 		return nil, err
 	}
@@ -127,7 +132,7 @@ func nextImageUploadReq(s sesn.Sesn, data []byte, off int) (
 	// fit we'll recalculate without hash
 	if off == 0 && chunklen < IMAGE_UPLOAD_MIN_1ST_CHUNK {
 		hash = nil
-		chunklen, err = findChunkLen(s, hash, data, off)
+		chunklen, err = findChunkLen(s, hash, upgrade, data, off)
 		if err != nil {
 			return nil, err
 		}
@@ -141,10 +146,11 @@ func nextImageUploadReq(s sesn.Sesn, data []byte, off int) (
 			s.MtuOut())
 	}
 
-	r := buildImageUploadReq(len(data), hash, data[off:off+chunklen], off)
+	r := buildImageUploadReq(len(data), hash, upgrade,
+		data[off:off+chunklen], off)
 
-	// Request above should encode just fine since we calculate proper chunk length
-	// but (at least for now) let's double check it
+	// Request above should encode just fine since we calculate proper chunk
+	// length but (at least for now) let's double check it
 	enc, err := mgmt.EncodeMgmt(s, r.Msg())
 	if err != nil {
 		return nil, err
@@ -161,7 +167,7 @@ func (c *ImageUploadCmd) Run(s sesn.Sesn) (Result, error) {
 	res := newImageUploadResult()
 
 	for off := c.StartOff; off < len(c.Data); {
-		r, err := nextImageUploadReq(s, c.Data, off)
+		r, err := nextImageUploadReq(s, c.Upgrade, c.Data, off)
 		if err != nil {
 			return nil, err
 		}
@@ -211,6 +217,7 @@ type ImageUpgradeCmd struct {
 	NoErase     bool
 	ProgressCb  ImageUploadProgressFn
 	LastOff     uint32
+	Upgrade     bool
 	ProgressBar *pb.ProgressBar
 }
 
@@ -284,6 +291,7 @@ func (c *ImageUpgradeCmd) runUpload(s sesn.Sesn) (*ImageUploadResult, error) {
 		cmd := NewImageUploadCmd()
 		cmd.Data = c.Data
 		cmd.StartOff = startOff
+		cmd.Upgrade = c.Upgrade
 		cmd.ProgressCb = progressCb
 		cmd.SetTxOptions(c.TxOptions())