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>
This commit is contained in:
Kevin O'Connor 2022-07-28 10:59:55 -04:00 committed by Eric Callahan
parent 28628012bf
commit fb50570994
3 changed files with 15 additions and 4 deletions

View File

@ -4,7 +4,7 @@
# 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
import sys, argparse, struct
ERR_MSG = """
The CanBoot binary is too large for the configured APPLICATION_START.
@ -13,10 +13,18 @@ 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()
@ -34,6 +42,9 @@ def main():
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()

View File

@ -25,7 +25,7 @@ src-$(CONFIG_SERIAL) += lpc176x/serial.c generic/serial_irq.c
# Build the additional bin output file
target-y += $(OUT)canboot.bin
$(OUT)canboot.bin: $(OUT)canboot.elf
$(OUT)canboot.bin: $(OUT)canboot.elf ./scripts/buildbinary.py
@echo " Creating hex file $@"
$(Q)$(OBJCOPY) -O binary $< $(OUT)canboot.work
$(Q)$(PYTHON) ./scripts/buildbinary.py -b $(CONFIG_FLASH_START) -s $(CONFIG_APPLICATION_START) $(OUT)canboot.work $@
$(Q)$(PYTHON) ./scripts/buildbinary.py -b $(CONFIG_FLASH_START) -s $(CONFIG_APPLICATION_START) -l $(OUT)canboot.work $@

View File

@ -61,7 +61,7 @@ src-$(CONFIG_CANSERIAL) += $(canbus-src-y) generic/canbus.c stm32/chipid.c
# Binary output file rules
target-y += $(OUT)canboot.bin
$(OUT)canboot.bin: $(OUT)canboot.elf
$(OUT)canboot.bin: $(OUT)canboot.elf ./scripts/buildbinary.py
@echo " Creating hex file $@"
$(Q)$(OBJCOPY) -O binary $< $(OUT)canboot.work
$(Q)$(PYTHON) ./scripts/buildbinary.py -b $(CONFIG_FLASH_START) -s $(CONFIG_APPLICATION_START) $(OUT)canboot.work $@