wolfBoot/test-app/gen_hdr_c.py

37 lines
1.5 KiB
Python

#!/usr/bin/env python3
# gen_hdr_c.py - emit a C source placing the wolfBoot signed header in flash.
#
# Input: a header cell blob (256 little-endian 16-bit words, from
# c2000_flashimg.py hdr2cells) OR --placeholder for an all-0xFFFF stub.
# Output: wolfboot_hdr.c with `const unsigned int wolfboot_header[256]` in the
# .wolfboot_hdr section (placed at the BOOT partition base 0xA0000 by
# the app linker cmd), one octet per 16-bit cell.
import struct
import sys
N = 256
def main():
if "--placeholder" in sys.argv:
words = [0xFFFF] * N
else:
data = open(sys.argv[1], "rb").read()
if len(data) < 2 * N:
sys.stderr.write(
"gen_hdr_c: %s is %d bytes; need at least %d (%d 16-bit words)\n"
% (sys.argv[1], len(data), 2 * N, N))
sys.exit(1)
words = [struct.unpack_from("<H", data, i)[0] for i in range(0, 2 * N, 2)]
out = sys.argv[sys.argv.index("-o") + 1] if "-o" in sys.argv else "wolfboot_hdr.c"
with open(out, "w") as f:
f.write("/* Generated by gen_hdr_c.py - wolfBoot signed header (octet/cell). */\n")
f.write('#pragma DATA_SECTION(wolfboot_header, ".wolfboot_hdr")\n')
f.write("const unsigned int wolfboot_header[%d] = {\n" % N)
for i in range(0, N, 8):
f.write(" " + ", ".join("0x%04X" % w for w in words[i:i + 8]) + ",\n")
f.write("};\n")
sys.stderr.write("gen_hdr_c: wrote %s (%d words)\n" % (out, N))
if __name__ == "__main__":
main()