newt: Prepare support for header skipping

In preparation for a tool to sign Zephyr images, add support for
Zephyr-style headers.

When Zephyr is configured to allow space for the image header
(CONFIG_TEXT_OFFSET), it's build system emits a binary with zeros padded
out for this header space.  To support this, add a `SrcSkip` option to
the `Image` struct to indicate a number of bytes of zeros to skip at the
beginning of a binary image.  Print a warning if this header doesn't
actually contain zeros, since this usually indicates that the parameter
is incorrect.

This patch adds the field to the struct, but since no code yet sets this
field, there should be no change in functionality.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/8564eb31
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/8564eb31
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/8564eb31

Branch: refs/heads/develop
Commit: 8564eb314919e4eeacd00e2bec3053d0e3d0d002
Parents: 1855170
Author: David Brown <[email protected]>
Authored: Fri Jan 27 17:59:09 2017 -0700
Committer: David Brown <[email protected]>
Committed: Fri Jan 27 18:07:57 2017 -0700

----------------------------------------------------------------------
 newt/image/image.go | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/8564eb31/newt/image/image.go
----------------------------------------------------------------------
diff --git a/newt/image/image.go b/newt/image/image.go
index 87d01de..916d386 100644
--- a/newt/image/image.go
+++ b/newt/image/image.go
@@ -61,6 +61,7 @@ type Image struct {
        SigningEC  *ecdsa.PrivateKey
        KeyId      uint8
        Hash       []byte
+       SrcSkip    uint // Number of bytes to skip from the source image.
 }
 
 type ImageHdr struct {
@@ -367,7 +368,7 @@ func (image *Image) Generate(loader *Image) error {
                Pad1:  0,
                HdrSz: IMAGE_HEADER_SIZE,
                Pad2:  0,
-               ImgSz: uint32(binInfo.Size()),
+               ImgSz: uint32(binInfo.Size()) - uint32(image.SrcSkip),
                Flags: 0,
                Vers:  image.Version,
                Pad3:  0,
@@ -404,6 +405,30 @@ func (image *Image) Generate(loader *Image) error {
        }
 
        /*
+        * Skip requested initial part of image.
+        */
+       if image.SrcSkip > 0 {
+               buf := make([]byte, image.SrcSkip)
+               _, err = binFile.Read(buf)
+               if err != nil {
+                       return util.NewNewtError(fmt.Sprintf("Failed to read 
from %s: %s",
+                               image.SourceBin, err.Error()))
+               }
+
+               nonZero := false
+               for _, b := range buf {
+                       if b != 0 {
+                               nonZero = true
+                               break
+                       }
+               }
+               if nonZero {
+                       log.Warnf("Skip requested of iamge %s, but image not 
preceeded by %d bytes of all zeros",
+                               image.SourceBin, image.SrcSkip)
+               }
+       }
+
+       /*
         * Followed by data.
         */
        dataBuf := make([]byte, 1024)

Reply via email to