From b302a87f3c914d94466dc229cc43ffab6d16c76c Mon Sep 17 00:00:00 2001 From: Lingkai Dong Date: Tue, 24 Aug 2021 15:18:37 +0100 Subject: [PATCH 1/7] Generate hex image for all targets by default Mbed OS builds .bin images by default for most targets, but many targets (e.g. ST targets running the latest ST-LINK firmware) also support .hex files, making this demo much easier to prepare. In case a target doesn't support .hex files, README.md already contains steps for merging .hex files into a single one and converting it to a .bin file. --- README.md | 6 +++++- mbed_app.json | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0d77dfa..2ae018d 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,8 @@ You should also install the python package `intelhex` if it is not already insta This python package includes several utility scripts for working with binaries and .hex files. +By default, the demo application and bootloader are built as hex files to support flashing multiple images to different addresses. Some targets need to have the latest debug interface firmwares to support this, for example ST-LINK's firmware upgrader can be found [here](https://www.st.com/en/development-tools/stsw-link007.html). If your target does not support hex images, a single bin image with everything merged together can be used instead as will be described in [Merging it all together](#Merging-it-all-together) below. + ### Creating the signing keys and building the bootloader This section will only cover steps specific to setting up this project with a signing key pair and signing a main application binary. For more advanced use cases and information, such as using alternative signing algorithms to rsa-2048, see the [mcuboot documentation on Image Signing](https://github.com/mcu-tools/mcuboot/blob/master/docs/signed_images.md#image-signing). @@ -236,7 +238,9 @@ Other toolchains will have a similar utility to perform this step. ### Merging it all together -To be able to drag-and-drop program the whole demo together, it is necessary to merge all the hex files we created in the last steps together. +Usually, if your target supports hex files, you can drag-and-drop one image at a time (waiting for each to finish) to program them into their respective locations. + +If your target doesn't support hex files or performs a whole chip erase upon every drag-and-drop action, it is necessary to merge all the hex files we created in the last steps together. At this step, you should have created three hex files: - mbed-mcuboot-demo.hex (the bootloader) diff --git a/mbed_app.json b/mbed_app.json index fc30592..cef9af4 100644 --- a/mbed_app.json +++ b/mbed_app.json @@ -10,6 +10,7 @@ "*": { "target.restrict_size": "0x20000", "target.c_lib": "small", + "target.OUTPUT_EXT": "hex", "mcuboot.log-level": "MCUBOOT_LOG_LEVEL_DEBUG", "mbed-trace.enable": true, "mbed-trace.fea-ipv6": false From 7f6980a52691f8ece89294c25369ff6538572ad4 Mon Sep 17 00:00:00 2001 From: Lingkai Dong Date: Tue, 24 Aug 2021 15:57:23 +0100 Subject: [PATCH 2/7] Do not enable mcuboot.read-granularity on DISCO_L475VG_IOT01A The majority of flash devices support reading single bytes without alignment concerns. The config `mcuboot.read-granularity` enables additional code to handle read alignment, and it comes with a slight code size and performance overhead. --- mbed_app.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mbed_app.json b/mbed_app.json index cef9af4..79b2ada 100644 --- a/mbed_app.json +++ b/mbed_app.json @@ -42,9 +42,7 @@ "mcuboot.slot-size": "0xC0000", "mcuboot.scratch-address": "0x80E0000", "mcuboot.scratch-size": "0x20000", - "mcuboot.max-img-sectors": "0x180", - "mcuboot.read-granularity": 1, - "qspif.QSPI_MIN_PROG_SIZE": 1 + "mcuboot.max-img-sectors": "0x180" } } } From f9b42e4e39c805a6db4342ed7b8f0710e73f4825 Mon Sep 17 00:00:00 2001 From: Lingkai Dong Date: Tue, 24 Aug 2021 16:24:17 +0100 Subject: [PATCH 3/7] Update Mbed OS to 6.14.0 --- mbed-os.lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mbed-os.lib b/mbed-os.lib index f6a52ca..68e51ac 100644 --- a/mbed-os.lib +++ b/mbed-os.lib @@ -1 +1 @@ -https://github.com/ARMmbed/mbed-os/#8ef0a435b2356f8159dea8e427b2935d177309f8 +https://github.com/ARMmbed/mbed-os/#3377f083b3a6bd7a1b45ed2cea5cf083b9007527 From 41572533d2863b3ffed20fe1855724a8e3461bab Mon Sep 17 00:00:00 2001 From: Lingkai Dong Date: Tue, 24 Aug 2021 17:07:00 +0100 Subject: [PATCH 4/7] Update logs in README to support update confirmation/rejection This matches the responding change in mbed-mcuboot-blinky. --- README.md | 48 +++++++++++++++++------------------------------- 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 2ae018d..bc4df7e 100644 --- a/README.md +++ b/README.md @@ -280,15 +280,16 @@ Once your target has been programmed, open a serial terminal to view the debug o ``` -- Terminal on /dev/ttyACM1 - 9600,8,N,1 --- [INFO][BL]: Starting MCUboot -[INFO][MCUb]: Primary image: magic=good, swap_type=0x2, copy_done=0x1, image_ok=0x1 -[INFO][MCUb]: Scratch: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3 -[INFO][MCUb]: Boot source: none -[INFO][MCUb]: Swap type: none + + + [INFO][BL]: Booting firmware image at 0x21000 -[INFO][main]: Hello version 1.2.3+4 + + +[INFO][main]: Regular boot [INFO][main]: > Press button to erase secondary slot ``` @@ -323,8 +324,6 @@ This causes the bootloader to check the secondary slot for a valid update binary You should see output similar to the following: ``` -[DBG ][MCUb]: writing magic; fa_id=1 off=0xbfff0 (0xbfff0) -[DBG ][MCUb]: writing swap_info; fa_id=1 off=0xbffd8 (0xbffd8), swap_type=0x2 image_num=0x0 [INFO][main]: > Secondary image pending, reboot to update ``` @@ -334,39 +333,26 @@ Now all you need to do to perform the update is reset your Mbed board! The mcubo ``` [INFO][BL]: Starting MCUboot -[INFO][MCUb]: Primary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3 -[INFO][MCUb]: Scratch: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3 -[INFO][MCUb]: Boot source: primary slot -[INFO][MCUb]: Swap type: test -[DBG ][MCUb]: erasing scratch area -[DBG ][MCUb]: initializing status; fa_id=2 -[DBG ][MCUb]: writing swap_info; fa_id=2 off=0x1ffd8 (0xfffd8), swap_type=0x2 image_num=0x0 -[DBG ][MCUb]: writing swap_size; fa_id=2 off=0x1ffd0 (0xfffd0) -[DBG ][MCUb]: writing magic; fa_id=2 off=0x1fff0 (0xffff0) -[DBG ][MCUb]: erasing trailer; fa_id=0 -[DBG ][MCUb]: initializing status; fa_id=0 -[DBG ][MCUb]: writing swap_info; fa_id=0 off=0xbffd8 (0xdffd8), swap_type=0x2 image_num=0x0 -[DBG ][MCUb]: writing swap_size; fa_id=0 off=0xbffd0 (0xdffd0) -[DBG ][MCUb]: writing magic; fa_id=0 off=0xbfff0 (0xdfff0) -[DBG ][MCUb]: erasing trailer; fa_id=1 -[DBG ][MCUb]: writing copy_done; fa_id=0 off=0xbffe0 (0xdffe0) + + + [INFO][BL]: Booting firmware image at 0x21000 -[INFO][main]: Boot confirmed -[INFO][main]: Hello version 1.2.3+5 -[INFO][main]: > Press button to erase secondary slot +[INFO][main]: Firmware update applied successfully +[INFO][main]: Press the button to confirm, or reboot to revert the update ``` -Notice that the reported version number has changed! The update was performed successfully. - **Note:** After an update is performed, the new application must mark itself as "okay" to the mcuboot bootloader. If this does not occur, mcuboot will revert the update upon the next reboot (if configured to do so). -The blinky application in this demo always does this at startup: +To make the update as "okay", press the button as instructed. This causes the application to call +`boot_set_confirmed()`. + +Now you should see: ``` -int ret = boot_set_confirmed(); +[INFO][main]: Current firmware set as confirmed ``` -In real world situations, your application should run a self test routine to ensure it can receive updates in the future (eg: the UART software works as expected, the BLE stack initializes successfully, etc). +**Note:** In real world situations, your application should run a self test routine to ensure it can receive updates in the future (eg: the UART software works as expected, the BLE stack initializes successfully, etc). From d1709f2af7253a3292f8e6829a4efc44698d9cb8 Mon Sep 17 00:00:00 2001 From: Lingkai Dong Date: Wed, 25 Aug 2021 10:47:18 +0100 Subject: [PATCH 5/7] Remove redundant overrides of qspif.QSPI_MIN_PROG_SIZE The latest Mbed OS already has `qspif.QSPI_MIN_PROG_SIZE` set to 4 for MCU_NRF52840 which is the base target of NRF52840_DK and EP_AGORA. --- mbed_app.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mbed_app.json b/mbed_app.json index 79b2ada..f4930bd 100644 --- a/mbed_app.json +++ b/mbed_app.json @@ -23,8 +23,7 @@ "mcuboot.scratch-address": "0xE0000", "mcuboot.scratch-size": "0x20000", "mcuboot.max-img-sectors": "0x180", - "mcuboot.read-granularity": 4, - "qspif.QSPI_MIN_PROG_SIZE": 4 + "mcuboot.read-granularity": 4 }, "EP_AGORA": { "target.features_remove": ["CRYPTOCELL310"], @@ -34,8 +33,7 @@ "mcuboot.scratch-address": "0xE0000", "mcuboot.scratch-size": "0x20000", "mcuboot.max-img-sectors": "0x180", - "mcuboot.read-granularity": 4, - "qspif.QSPI_MIN_PROG_SIZE": 4 + "mcuboot.read-granularity": 4 }, "DISCO_L475VG_IOT01A": { "mcuboot.primary-slot-address": "0x8020000", From a0ec1f3038938a862a0ec04aeaf0070f0fcb083c Mon Sep 17 00:00:00 2001 From: Sudarshan Sreeram Date: Fri, 10 Sep 2021 18:13:24 +0100 Subject: [PATCH 6/7] Update mcuboot.lib to point to cmake support fork --- mcuboot.lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mcuboot.lib b/mcuboot.lib index 2c3e13b..120b416 100644 --- a/mcuboot.lib +++ b/mcuboot.lib @@ -1 +1 @@ -https://github.com/mcu-tools/mcuboot.git \ No newline at end of file +https://github.com/lambda-shuttle/mcuboot.git From 69fd468fe1f27c909a6593a7a58709216638bb7d Mon Sep 17 00:00:00 2001 From: Sudarshan Sreeram Date: Fri, 10 Sep 2021 18:14:05 +0100 Subject: [PATCH 7/7] Add support for cmake build workflow Co-authored-by: Lingkai Dong --- CMakeLists.txt | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..33abbec --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,43 @@ +# Copyright (c) 2021 ARM Limited. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +# Mbed-MCUboot Demo Bootloader + +cmake_minimum_required(VERSION 3.19.0 FATAL_ERROR) + +set(MBED_PATH ${CMAKE_CURRENT_SOURCE_DIR}/mbed-os CACHE INTERNAL "") +set(MCUBOOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/mcuboot CACHE INTERNAL "") +set(MBED_CONFIG_PATH ${CMAKE_CURRENT_BINARY_DIR} CACHE INTERNAL "") +set(APP_TARGET bootloader) + +include(${MBED_PATH}/tools/cmake/app.cmake) + +project(${APP_TARGET}) + +add_subdirectory(${MBED_PATH}) +add_subdirectory(${MCUBOOT_PATH}/boot/bootutil/) +add_subdirectory(${MCUBOOT_PATH}/boot/mbed/) # Mbed-MCUboot Port + +add_executable(${APP_TARGET}) + +target_sources(${APP_TARGET} + PUBLIC + default_bd.cpp + signing_keys.c +) + +target_link_libraries(${APP_TARGET} + PUBLIC + bootutil + mbed-mcuboot + mbed-storage-spif + mbed-storage-qspif + mbed-baremetal +) + +mbed_set_post_build(${APP_TARGET}) + +option(VERBOSE_BUILD "Have a verbose build process") +if(VERBOSE_BUILD) + set(CMAKE_VERBOSE_MAKEFILE ON) +endif() \ No newline at end of file