diff --git a/.gitignore b/.gitignore index 8635655..16c2958 100644 --- a/.gitignore +++ b/.gitignore @@ -47,3 +47,5 @@ libftdi1*.bz2 libusb*/ hidapi*/ .idea/* + +/reloader/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..25600a7 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "reloader"] + path = reloader + url = git@github.com:daniel-thompson/wasp-reloader.git diff --git a/README.md b/README.md index c36d988..b82b0c0 100644 --- a/README.md +++ b/README.md @@ -68,8 +68,9 @@ The goal of this firmware is to provide a mean for the user to OTA a new firmwar - Install `newt` tool - Clone the project and `cd` into it +- Init and update submodules : `git submodule update --init --recursive` - Configure mynewt : `newt upgrade` -- Build : `scripts/nrf52/build-boot.sh`. The firmware is generated in `bin/targets/nrf52_boot/app/@mcuboot/boot/mynewt/mynewt.elf` +- Build : `scripts/nrf52/build-boot.sh`. The firmware is generated in `bin/targets/nrf52_boot/app/@mcuboot/boot/mynewt/mynewt.elf` and the DFU file for the reloader : `reloader/build-pinetime/reloader-mcuboot.zip` ## About the code diff --git a/scripts/hex2c.py b/scripts/hex2c.py new file mode 100755 index 0000000..74f8431 --- /dev/null +++ b/scripts/hex2c.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +# SPDX-License-Identifier: LGPL-3.0-or-later +# Copyright (C) 2020 Daniel Thompson + +import binascii +import intelhex +import sys + +def generate_c(ihex): + print('/* this file is auto-generated - DO NOT EDIT */') + print() + print('#include ') + print() + print('struct segment {') + print(' uint32_t start;'); + print(' uint32_t end;'); + print(' uint32_t crc32;') + print(' const uint8_t *data;') + print('};') + print() + + segments = [] + chunk = 32 * 1024 + for (start, end) in ihex.segments(): + while start + chunk < end: + segments.append((start, start + chunk)) + start += chunk + if start < end: + segments.append((start, end)) + + for i, segment in enumerate(segments): + print(f'static const uint8_t segment{i}[] = {{', end='') + + for j in range(segment[0], segment[1]): + if 0 == j % 12: + print('\n ', end='') + print(f' 0x{ihex[j]:02x},', end='') + + print('\n};\n') + print(f'const struct segment segments[] = {{') + for i, segment in enumerate(segments): + sg = ihex.tobinarray(start=segment[0], end=segment[1]-1) + crc = binascii.crc32(sg) + print(f' 0x{segment[0]:08x}, 0x{segment[1]:08x}, 0x{crc:08x}, segment{i},') + print('};') + +ihex = intelhex.IntelHex() +ihex.loadhex(sys.argv[1]) +generate_c(ihex) diff --git a/scripts/nrf52/build-boot.sh b/scripts/nrf52/build-boot.sh index 5922623..fef67e0 100755 --- a/scripts/nrf52/build-boot.sh +++ b/scripts/nrf52/build-boot.sh @@ -13,6 +13,12 @@ newt build nrf52_boot # Show the size. newt size -v nrf52_boot -# Copy the disassembler and linker map to the logs folder. For Stub Bootloader, select "bin/targets/nrf52_boot/app/apps/boot_stub/boot_stub.elf.*" -cp bin/targets/nrf52_boot/app/boot/mynewt/mynewt.elf.lst logs -cp bin/targets/nrf52_boot/app/boot/mynewt/mynewt.elf.map logs +arm-none-eabi-objcopy -I binary -O ihex bin/targets/nrf52_boot/app/@mcuboot/boot/mynewt/mynewt.elf.bin bin/targets/nrf52_boot/app/@mcuboot/boot/mynewt/mynewt.elf.hex +scripts/hex2c.py bin/targets/nrf52_boot/app/@mcuboot/boot/mynewt/mynewt.elf.hex > reloader/src/boards/pinetime/bootloader.h +make -C reloader BOARD=pinetime + +set +x +echo "Bootloader firmware (elf) : bin/targets/nrf52_boot/app/@mcuboot/boot/mynewt/mynewt.elf" +echo "Bootloader firmware (bin) : bin/targets/nrf52_boot/app/@mcuboot/boot/mynewt/mynewt.bin" +echo "Bootloader firmware (hex) : bin/targets/nrf52_boot/app/@mcuboot/boot/mynewt/mynewt.hex" +echo "Reloader (DFU) : reloader/build-pinetime/reloader-mcuboot.zip"