63 lines
2.1 KiB
Python
Executable File
63 lines
2.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Build a DFU zip signed for the signed (SIGNED_FW) Adafruit bootloader.
|
|
|
|
usage: bl_sign_pkg.py <firmware.hex> <signing-key.pem> <out.zip>
|
|
|
|
adafruit-nrfutil's own `dfu genpkg --key-file` builds the package; this only
|
|
runs it with two fixes. Its signed path (0.5.3.post16, the latest on PyPI and
|
|
the one PlatformIO ships) never worked on Python 3: Signing.load_key() ends
|
|
with a dead Python 2 line, `c.encode('hex') for c in sk.to_string()`, and
|
|
manifest.json cannot serialize the image hash (bytes). The init packet layout stays
|
|
nrfutil's; tools/bl_verify_pkg.py checks the result independently.
|
|
|
|
Device type 0x0052 (Adafruit), any SoftDevice (0xFFFE), as the bootloader
|
|
expects for an application (src/dfu_init.c).
|
|
"""
|
|
import sys
|
|
|
|
if len(sys.argv) != 4 or sys.argv[1] in ("-h", "--help"):
|
|
print(__doc__.strip())
|
|
sys.exit(0 if len(sys.argv) > 1 and sys.argv[1] in ("-h", "--help") else 2)
|
|
|
|
import json
|
|
|
|
from ecdsa import SigningKey
|
|
from nordicsemi import __main__ as nrfutil
|
|
from nordicsemi.dfu import manifest, signing
|
|
|
|
|
|
def _load_key(self, filename):
|
|
with open(filename) as f:
|
|
self.sk = SigningKey.from_pem(f.read())
|
|
|
|
|
|
signing.Signing.load_key = _load_key
|
|
|
|
|
|
# Second Python 3 break on the signed path: manifest.json carries the image
|
|
# hash as bytes, which the json default hook does not know. Informational only
|
|
# (the bootloader reads the .dat init packet), written as hex like nrfutil 0.5.x.
|
|
def _to_json(self):
|
|
def remove_none_entries(d):
|
|
if not isinstance(d, dict):
|
|
return d
|
|
return {k: remove_none_entries(v) for k, v in d.items() if v is not None}
|
|
|
|
def default(o):
|
|
if isinstance(o, bytes):
|
|
return o.hex()
|
|
return remove_none_entries(o.__dict__)
|
|
|
|
return json.dumps({"manifest": self.manifest}, default=default,
|
|
sort_keys=True, indent=4, separators=(",", ": "))
|
|
|
|
|
|
manifest.ManifestGenerator.to_json = _to_json
|
|
|
|
hexfile, keyfile, out = sys.argv[1:]
|
|
sys.exit(nrfutil.cli.main(
|
|
["dfu", "genpkg", "--dev-type", "0x0052", "--sd-req", "0xFFFE",
|
|
"--application", hexfile, "--key-file", keyfile, out],
|
|
standalone_mode=True,
|
|
))
|