# -*- coding: utf-8 -*- """Pack the built bootloader into project/src/AT32F403ARCT7_BOOT.c.""" import os ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BIN = os.path.join( ROOT, "AT32F403ARCT7_BOOT", "project", "IAR_V7.4", "AT32F403ARCT7_BOOT", "Exe", "AT32F403ARCT7_BOOT.bin", ) OUT = os.path.join(ROOT, "project", "src", "AT32F403ARCT7_BOOT.c") data = open(BIN, "rb").read() if len(data) > 0x8000: raise SystemExit("boot image %d exceeds 32KB" % len(data)) print("binsize", len(data), data[:16].hex()) print("nvm addr hits", data.find(bytes([0x00, 0xFC, 0x03, 0x08]))) pad = data + bytes([0xFF]) * (0x8000 - len(data)) lines = [ "#ifdef BOOT", '#pragma location=".boot"', "__root const unsigned char FW_BOOT[0x8000] = {", ] for i in range(0, 0x8000, 16): chunk = ", ".join("0x%02X" % b for b in pad[i : i + 16]) lines.append("\t\t" + chunk + ",") text = "\n".join(lines) text = text[:-1] + "\n};\n\n\n#endif\n" open(OUT, "w", encoding="latin1", newline="\n").write(text) print("wrote", OUT, "chars", len(text))