Skip to content

Commit

Permalink
Add ability to embed extra data in FW bundles using --extra-attr
Browse files Browse the repository at this point in the history
Data is embedded as a JSON string with attibute ID 0x293a

Example:

  $ mos create-fw-bundle -o test.zip --extra-attr foo=bar --extra-attr bar=123 ...

Will embed `{"bar":123,"foo":"bar"}` in the manifest's extra field.

This mechanism will later be used for manifest signatures.

CL: mos: Add ability to embed extra data in FW bundles using --extra-attr
  • Loading branch information
rojer committed Jun 27, 2019
1 parent 934af90 commit 1495404
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
27 changes: 23 additions & 4 deletions common/fwbundle/fw_bundle_zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package fwbundle
import (
"bytes"
"compress/flate"
"encoding/binary"
"encoding/json"
"io"
"io/ioutil"
Expand All @@ -33,6 +34,13 @@ import (

const (
ManifestFileName = "manifest.json"

// The ZIP AppNOte 6.3.6 says:
// Header IDs of 0 thru 31 are reserved for use by PKWARE.
// The remaining IDs can be used by third party vendors for
// proprietary usage.
// 0x293a looks like a smiley face when written in LE (0x3a 0x29), so... hey, why not? :)
zipExtraDataID = uint16(0x293a)
)

func ReadZipFirmwareBundle(fname string) (*FirmwareBundle, error) {
Expand Down Expand Up @@ -85,7 +93,7 @@ func ReadZipFirmwareBundle(fname string) (*FirmwareBundle, error) {
return fwb, nil
}

func WriteZipFirmwareBytes(fwb *FirmwareBundle, buf *bytes.Buffer, compress bool) error {
func WriteZipFirmwareBytes(fwb *FirmwareBundle, buf *bytes.Buffer, compress bool, extraAttrs map[string]interface{}) error {
zw := zip.NewWriter(buf)
// When compressing, use best compression.
zw.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
Expand All @@ -110,9 +118,20 @@ func WriteZipFirmwareBytes(fwb *FirmwareBundle, buf *bytes.Buffer, compress bool
if err != nil {
return errors.Annotatef(err, "error marshaling manifest")
}
extraData := bytes.NewBuffer(nil)
if len(extraAttrs) > 0 {
extraAttrData, err := json.Marshal(extraAttrs)
if err != nil {
return errors.Annotatef(err, "error marshaling extra attrs")
}
binary.Write(extraData, binary.LittleEndian, zipExtraDataID)
binary.Write(extraData, binary.LittleEndian, uint16(len(extraAttrData)))
extraData.Write(extraAttrData)
}
glog.V(1).Infof("Manifest:\n%s", string(manifestData))
zfh := &zip.FileHeader{
Name: ManifestFileName,
Name: ManifestFileName,
Extra: extraData.Bytes(),
}
if compress {
zfh.Method = zip.Deflate
Expand Down Expand Up @@ -146,9 +165,9 @@ func WriteZipFirmwareBytes(fwb *FirmwareBundle, buf *bytes.Buffer, compress bool
return nil
}

func WriteZipFirmwareBundle(fwb *FirmwareBundle, fname string, compress bool) error {
func WriteZipFirmwareBundle(fwb *FirmwareBundle, fname string, compress bool, extraAttrs map[string]interface{}) error {
buf := new(bytes.Buffer)
if err := WriteZipFirmwareBytes(fwb, buf, compress); err != nil {
if err := WriteZipFirmwareBytes(fwb, buf, compress, extraAttrs); err != nil {
return err
}
return ioutil.WriteFile(fname, buf.Bytes(), 0644)
Expand Down
29 changes: 29 additions & 0 deletions mos/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package moscommon

import (
"strconv"
"strings"

"github.com/cesanta/errors"
Expand Down Expand Up @@ -45,3 +46,31 @@ func ParseParamValues(args []string) (map[string]string, error) {
}
return ret, nil
}

func ParseParamValuesTyped(args []string) (map[string]interface{}, error) {
var res map[string]interface{}
params, err := ParseParamValues(args)
if err != nil {
return nil, errors.Trace(err)
}
for p, valueStr := range params {
var value interface{}
switch valueStr {
case "true":
value = true
case "false":
value = false
default:
if i, err := strconv.ParseInt(valueStr, 0, 64); err == nil {
value = i
} else {
value = valueStr
}
}
if res == nil {
res = make(map[string]interface{})
}
res[p] = value
}
return res, nil
}
24 changes: 7 additions & 17 deletions mos/create_fw_bundle/create_fw_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"strings"

moscommon "github.com/mongoose-os/mos/mos/common"
Expand Down Expand Up @@ -114,26 +113,17 @@ func CreateFWBundle(ctx context.Context, devConn dev.DevConn) error {
}
}
}
attrs, err := moscommon.ParseParamValues(*flags.Attr)
attrs, err := moscommon.ParseParamValuesTyped(*flags.Attr)
if err != nil {
return errors.Annotatef(err, "failed to parse --attr")
}
for attr, valueStr := range attrs {
var value interface{}
switch valueStr {
case "true":
value = true
case "false":
value = false
default:
if i, err := strconv.ParseInt(valueStr, 0, 64); err == nil {
value = i
} else {
value = valueStr
}
}
for attr, value := range attrs {
fwb.SetAttr(attr, value)
}
extraAttrs, err := moscommon.ParseParamValuesTyped(*flags.ExtraAttr)
if err != nil {
return errors.Annotatef(err, "failed to parse --extra-attr")
}
ourutil.Reportf("Writing %s", *flags.Output)
return fwbundle.WriteZipFirmwareBundle(fwb, *flags.Output, *flags.Compress)
return fwbundle.WriteZipFirmwareBundle(fwb, *flags.Output, *flags.Compress, extraAttrs)
}
3 changes: 2 additions & 1 deletion mos/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ var (
KeepTempFiles = flag.Bool("keep-temp-files", false, "keep temp files after the build is done (by default they are in ~/.mos/tmp)")
KeepFS = flag.Bool("keep-fs", false, "When flashing, skip the filesystem parts")

Attr = flag.StringArray("attr", []string{}, "manifest attribute, can be used multiple times")
Attr = flag.StringArray("attr", nil, "manifest attribute, can be used multiple times")
ExtraAttr = flag.StringArray("extra-attr", nil, "manifest extra attribute info to be added to ZIP")
)

func Platform() string {
Expand Down

0 comments on commit 1495404

Please sign in to comment.