newt: Prepare support for header padding

Since Zephyr is able to use the vector table directly out of flash, and
some MCUs have requirements on the alignment of this header, add a
`HeaderSize` value to the `Image` struct.

If this is set to a value larger than the `IMAGE_HEADER_SIZE`, padding
will be inserted between the header and the image.  The `HdrSz` field in
the header will be set accordingly, so that the image can still be
found.

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/2f83e67f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/2f83e67f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/2f83e67f

Branch: refs/heads/develop
Commit: 2f83e67f0ce8f1ce124c3c92d6ba2254ae4bfadc
Parents: 8564eb3
Author: David Brown <[email protected]>
Authored: Fri Jan 27 18:04:09 2017 -0700
Committer: David Brown <[email protected]>
Committed: Fri Jan 27 18:08:02 2017 -0700

----------------------------------------------------------------------
 newt/image/image.go | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/2f83e67f/newt/image/image.go
----------------------------------------------------------------------
diff --git a/newt/image/image.go b/newt/image/image.go
index 916d386..c63eb2e 100644
--- a/newt/image/image.go
+++ b/newt/image/image.go
@@ -62,6 +62,7 @@ type Image struct {
        KeyId      uint8
        Hash       []byte
        SrcSkip    uint // Number of bytes to skip from the source image.
+       HeaderSize uint // If non-zero pad out the header to this size.
 }
 
 type ImageHdr struct {
@@ -393,6 +394,19 @@ func (image *Image) Generate(loader *Image) error {
                hdr.Flags |= IMAGE_F_NON_BOOTABLE
        }
 
+       if image.HeaderSize != 0 {
+               /*
+                * Pad the header out to the given size.  There will
+                * just be zeros between the header and the start of
+                * the image when it is padded.
+                */
+               if image.HeaderSize < IMAGE_HEADER_SIZE {
+                       return util.NewNewtError(fmt.Sprintf("Image header must 
be at least %d bytes", IMAGE_HEADER_SIZE))
+               }
+
+               hdr.HdrSz = uint16(image.HeaderSize)
+       }
+
        err = binary.Write(imgFile, binary.LittleEndian, hdr)
        if err != nil {
                return util.NewNewtError(fmt.Sprintf("Failed to serialize image 
hdr: %s",
@@ -404,6 +418,26 @@ func (image *Image) Generate(loader *Image) error {
                        err.Error()))
        }
 
+       if image.HeaderSize > IMAGE_HEADER_SIZE {
+               /*
+                * Pad the image (and hash) with zero bytes to fill
+                * out the buffer.
+                */
+               buf := make([]byte, image.HeaderSize-IMAGE_HEADER_SIZE)
+
+               _, err = imgFile.Write(buf)
+               if err != nil {
+                       return util.NewNewtError(fmt.Sprintf("Failed to write 
padding: %s",
+                               err.Error()))
+               }
+
+               _, err = hash.Write(buf)
+               if err != nil {
+                       return util.NewNewtError(fmt.Sprintf("Failed to hash 
padding: %s",
+                               err.Error()))
+               }
+       }
+
        /*
         * Skip requested initial part of image.
         */

Reply via email to