2018-03-11 14:58:11 +00:00
|
|
|
#!/usr/bin/env python
|
2022-11-23 15:02:05 +00:00
|
|
|
import sys
|
2018-03-11 14:58:11 +00:00
|
|
|
from hashlib import sha256
|
|
|
|
|
2022-11-23 15:02:05 +00:00
|
|
|
fn = sys.argv[1]
|
|
|
|
|
|
|
|
print("Embedding bootloader", fn)
|
2018-03-11 14:58:11 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
data = open(fn, "rb").read()
|
2018-03-11 14:58:11 +00:00
|
|
|
if len(data) > 32768:
|
2019-04-18 14:27:27 +00:00
|
|
|
raise Exception("bootloader has to be smaller than 32768 bytes")
|
2018-03-11 14:58:11 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
data += b"\x00" * (32768 - len(data))
|
2018-03-11 14:58:11 +00:00
|
|
|
|
2021-04-22 18:19:23 +00:00
|
|
|
bh = sha256(sha256(data).digest()).digest()
|
2018-03-11 14:58:11 +00:00
|
|
|
|
2021-04-22 18:19:23 +00:00
|
|
|
bl_hash = ", ".join("0x%02x" % x for x in bytearray(bh))
|
2019-04-18 14:27:27 +00:00
|
|
|
bl_data = ", ".join("0x%02x" % x for x in bytearray(data))
|
2018-03-11 14:58:11 +00:00
|
|
|
|
2019-04-18 14:27:27 +00:00
|
|
|
with open("bl_data.h", "wt") as f:
|
2021-09-27 10:13:51 +00:00
|
|
|
f.write(f"static const uint8_t bl_hash[32] = {{{bl_hash}}};\n")
|
|
|
|
f.write(f"static const uint8_t bl_data[32768] = {{{bl_data}}};\n")
|
2021-04-22 18:19:23 +00:00
|
|
|
|
2022-11-23 15:02:05 +00:00
|
|
|
if fn != "bootloader_qa.dat":
|
|
|
|
# make sure the last item listed in known_bootloader function
|
|
|
|
# is our bootloader
|
|
|
|
with open("bl_check.c", "rt") as f:
|
|
|
|
hashes = []
|
|
|
|
for l in f.readlines():
|
|
|
|
if not len(l) >= 78 or not l.startswith(' "\\x'):
|
|
|
|
continue
|
|
|
|
l = l[14:78]
|
|
|
|
h = ""
|
|
|
|
for i in range(0, len(l), 4):
|
|
|
|
h += l[i + 2 : i + 4]
|
|
|
|
hashes.append(h)
|
|
|
|
check = hashes[-2] + hashes[-1]
|
|
|
|
if check != bh.hex():
|
|
|
|
raise Exception("bootloader hash not listed in bl_check.c")
|