katapult/scripts/buildbinary.py
Kevin O'Connor fb50570994 buildbinary: Add support for building lpc176x checksum
The lpc176x internal rom checks that the first 8 words of the flash
sum to zero.  Many flash writing tools for the lpc176x will
automatically add the appropriate checksum.  However, it can be useful
to build the checksum locally so that the image supports direct flash
writing.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2022-07-29 12:20:00 -04:00

54 lines
1.6 KiB
Python

#!/usr/bin/env python3
# Tool to check final CanBoot 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 CanBoot binary is too large for the configured APPLICATION_START.
Rerun "make menuconfig" and either increase the APPLICATION_START or
disable features to reduce the final binary size.
"""
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 CanBoot 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("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)
f = open(args.output_file, 'wb')
f.write(data)
f.close()
if __name__ == '__main__':
main()