katapult/scripts/buildbinary.py
Eric Callahan 5e794c05d9
build: rename references from CanBoot to Katapult
Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
2023-07-30 10:35:54 -04:00

83 lines
2.3 KiB
Python

#!/usr/bin/env python3
# Tool to check final Katapult binary size
#
# Copyright (C) 2022 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import sys, argparse, struct
ERR_MSG = """
The Katapult binary is too large for the configured LAUNCH_APP_ADDRESS.
Rerun "make menuconfig" and either increase the LAUNCH_APP_ADDRESS or
disable features to reduce the final binary size.
"""
C_TEMPLATE = """
/* DO NOT EDIT! This is an autogenerated file. See scripts/buildbinary.py. */
#include <stdint.h>
#include "deployer.h"
const uint8_t deployer_canboot_binary[] = {
%s
};
const uint32_t deployer_canboot_binary_size = %d;
"""
def format_c_code(data):
ldata = len(data)
data = data + (b"\xff" * (8 - (ldata % 8)))
vals = [" "]
for i, d in enumerate(data):
vals.append("0x%02x," % (d,))
if i % 14 == 13:
vals.append("\n ")
return C_TEMPLATE % ("".join(vals), ldata)
def update_lpc176x_checksum(data):
data28 = data[:28]
words = struct.unpack('<IIIIIII', data28)
csum = (-sum(words)) & 0xffffffff
return data28 + struct.pack('<I', csum) + data[32:]
def main():
parser = argparse.ArgumentParser(description="Build Katapult binary")
parser.add_argument("-b", "--base", help="Address of flash start")
parser.add_argument("-s", "--start", help="Address of application start")
parser.add_argument("-l", "--lpc176x", action='store_true',
help="Perform lpc176x checksum")
parser.add_argument("-c", "--code", help="C code file containing binary")
parser.add_argument("input_file", help="Raw binary filename")
parser.add_argument("output_file", help="Final binary filename")
args = parser.parse_args()
start = int(args.start, 0)
base = int(args.base, 0)
max_size = start - base
f = open(args.input_file, 'rb')
data = f.read()
f.close()
if len(data) > max_size:
msg = "\nMaximum size %d. Current size %d.\n\n" % (max_size, len(data))
sys.stderr.write(ERR_MSG + msg)
sys.exit(-1)
if args.lpc176x:
data = update_lpc176x_checksum(data)
if args.code:
f = open(args.code, 'w')
f.write(format_c_code(data))
f.close()
f = open(args.output_file, 'wb')
f.write(data)
f.close()
if __name__ == '__main__':
main()