golioth / golioth-zephyr-sdk

Golioth SDK For Zephyr
https://www.golioth.io
Apache License 2.0
65 stars 20 forks source link

samples: dfu: support 'esp32' platform with sysbuild #376

Closed mniestroj closed 1 year ago

mniestroj commented 1 year ago

Add support for sysbuild for esp32 board. That way all vanilla Zephyr boards will be supported using single, much simplified build/flash flow.

The old flow

Update the submodules needed by the Espressif port:

$ git submodule update --init --recursive --checkout boot/espressif/hal/esp-idf

Install IDF dependencies and set environment variables:

$ cd boot/espressif/hal/esp-idf
$ ./install.sh
$ . ./export.sh
$ cd ../..

Compile and generate the ELF:

$ cmake -DCMAKE_TOOLCHAIN_FILE=tools/toolchain-<target>.cmake -DMCUBOOT_TARGET=<target> -B build -GNinja
$ cmake --build build/

Convert the ELF to the final bootloader image, ready to be flashed:

$ esptool.py --chip <target> elf2image --flash_mode dio --flash_freq 40m -o build/mcuboot_<target>.bin build/mcuboot_<target>.elf

Flash MCUboot in your board:

$ esptool.py -p <PORT> -b <BAUD> --before default_reset --after hard_reset --chip <target> write_flash --flash_mode dio --flash_size detect --flash_freq 40m 0x1000 build/mcuboot_<target>.bin

Flash Golioth DFU sample:

$ west build -b esp32 samples/dfu

Sign sample image:

$ west sign -t imgtool -- --key $(west topdir)/bootloader/mcuboot/root-rsa-2048.pem

Flash sample image:

$ west flash --bin-file build/zephyr/zephyr.signed.bin --hex-file build/zephyr/zephyr.signed.hex

Prepare application image with new version (1.2.3):

$ west sign -t imgtool --no-hex -B new.bin -- --key $(west topdir)/bootloader/mcuboot/root-rsa-2048.pem --version 1.2.3

The new flow

Build bootloader and (signed) Golioth DFU sample:

$ west build -b esp32 --sysbuild samples/dfu

Flash bootloader and sample:

$ west flash

Prepare application image with new version (1.2.3):

$ west build -b esp32 --sysbuild samples/dfu -- -DCONFIG_MCUBOOT_EXTRA_IMGTOOL_ARGS='"--version 1.2.3"'
github-actions[bot] commented 1 year ago

Visit the preview URL for this PR (updated for commit acb8b90):

https://golioth-zephyr-sdk-doxygen-dev--pr376-esp32-boot-o0r0haeq.web.app

(expires Thu, 04 May 2023 20:01:26 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: a389eefadf4b4b68a539327b3459dd66c142cf49

mniestroj commented 1 year ago

@ChrisGammell This is where sysbuild is used. Based on the brief documentation above, you can see what are the advantages of using it in case of DFU capable firmware.

szczys commented 1 year ago

I'm having trouble getting the second binary build to make it past the signing process. It looks like the version number is in there, but it's not used as the argument for the --version flag.

west build -b esp32 --sysbuild samples/dfu -- -DCONFIG_MCUBOOT_EXTRA_IMGTOOL_ARGS=\"1.2.3\"

...snip...

Error: Got unexpected extra argument (/home/mike/golioth-zephyr-workspace/modules/lib/golioth/build/dfu/zephyr/zephyr.signed.bin)
FATAL ERROR: command exited with status 2: /home/mike/golioth-zephyr-workspace/.venv/bin/imgtool sign --version 0.0.0+0 --align 4 --header-size 32 --slot-size 1048576 --key /home/mike/golioth-zephyr-workspace/bootloader/mcuboot/root-rsa-2048.pem 1.2.3 /home/mike/golioth-zephyr-workspace/modules/lib/golioth/build/dfu/zephyr/zephyr.bin /home/mike/golioth-zephyr-workspace/modules/lib/golioth/build/dfu/zephyr/zephyr.signed.bin
ninja: build stopped: subcommand failed.
[10/16] No configure step for 'mcuboot_espressif'
FAILED: dfu-prefix/src/dfu-stamp/dfu-build /home/mike/golioth-zephyr-workspace/modules/lib/golioth/build/dfu-prefix/src/dfu-stamp/dfu-build 
cd /home/mike/golioth-zephyr-workspace/modules/lib/golioth/build/dfu && /usr/local/bin/cmake --build .
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1: /usr/local/bin/cmake --build /home/mike/golioth-zephyr-workspace/modules/lib/golioth/build
mniestroj commented 1 year ago

I'm having trouble getting the second binary build to make it past the signing process. It looks like the version number is in there, but it's not used as the argument for the --version flag.

west build -b esp32 --sysbuild samples/dfu -- -DCONFIG_MCUBOOT_EXTRA_IMGTOOL_ARGS=\"1.2.3\"

...snip...

Error: Got unexpected extra argument (/home/mike/golioth-zephyr-workspace/modules/lib/golioth/build/dfu/zephyr/zephyr.signed.bin)
FATAL ERROR: command exited with status 2: /home/mike/golioth-zephyr-workspace/.venv/bin/imgtool sign --version 0.0.0+0 --align 4 --header-size 32 --slot-size 1048576 --key /home/mike/golioth-zephyr-workspace/bootloader/mcuboot/root-rsa-2048.pem 1.2.3 /home/mike/golioth-zephyr-workspace/modules/lib/golioth/build/dfu/zephyr/zephyr.bin /home/mike/golioth-zephyr-workspace/modules/lib/golioth/build/dfu/zephyr/zephyr.signed.bin
ninja: build stopped: subcommand failed.
[10/16] No configure step for 'mcuboot_espressif'
FAILED: dfu-prefix/src/dfu-stamp/dfu-build /home/mike/golioth-zephyr-workspace/modules/lib/golioth/build/dfu-prefix/src/dfu-stamp/dfu-build 
cd /home/mike/golioth-zephyr-workspace/modules/lib/golioth/build/dfu && /usr/local/bin/cmake --build .
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1: /usr/local/bin/cmake --build /home/mike/golioth-zephyr-workspace/modules/lib/golioth/build

Thanks for catching this! I've updated -DCONFIG_MCUBOOT_EXTRA_IMGTOOL_ARGS=... argument in docs and in this PR description. It should work now.

szczys commented 1 year ago

Yes that got it building for me, thanks!

Edit: I had previously uploaded the wrong .bin file as the artifact and reported a problem here. All working now!