From b569909ea57957d60df5417c8c2bb271a6ae71aa Mon Sep 17 00:00:00 2001 From: Jamie Smith Date: Thu, 2 Jan 2025 23:45:34 -0800 Subject: [PATCH 1/6] Start on MIMXRT1060_EVK support. App boots but won't update yet... --- SimpleApp.cpp | 6 ++++++ UpdaterApp.cpp | 14 ++++++++++++++ mbed-os | 2 +- mbed_app.json5 | 44 ++++++++++++++++++++++++++++++++++++++------ mcuboot | 2 +- secondary_bd.cpp | 8 ++++---- 6 files changed, 64 insertions(+), 12 deletions(-) diff --git a/SimpleApp.cpp b/SimpleApp.cpp index 5a1f321..2bc5898 100644 --- a/SimpleApp.cpp +++ b/SimpleApp.cpp @@ -26,7 +26,10 @@ int main() mbed_trace_include_filters_set("SimpleApp,MCUb,BL"); DigitalIn btn(DEMO_BUTTON); + +#ifdef LED1 DigitalOut led(LED1); +#endif // Check if an update has been performed int swap_type = boot_swap_type(); @@ -81,6 +84,8 @@ int main() { ThisThread::sleep_for(1s); printf("Simple app is running...\n"); + +#ifdef LED1 led = 1; ThisThread::sleep_for(250ms); led = 0; @@ -88,5 +93,6 @@ int main() led = 1; ThisThread::sleep_for(250ms); led = 0; +#endif } } \ No newline at end of file diff --git a/UpdaterApp.cpp b/UpdaterApp.cpp index 6ede64b..d1e8243 100644 --- a/UpdaterApp.cpp +++ b/UpdaterApp.cpp @@ -45,6 +45,17 @@ int main() tr_error("Cannot init secondary BlockDevice: %d", ret); } + tr_info(" "); + tr_info("mcuboot configuration: -----------------------------------"); + tr_info("- Primary slot: start address 0x%x, size %ukiB", MCUBOOT_PRIMARY_SLOT_START_ADDR, MCUBOOT_SLOT_SIZE/1024); + tr_info(" - Header size: 0x%x", MCUBOOT_HEADER_SIZE); + tr_info(" - Max flash sectors per slot: %u", MCUBOOT_MAX_IMG_SECTORS); + tr_info("- Scratch area: start address 0x%x, size %ukiB", MCUBOOT_SCRATCH_START_ADDR, MCUBOOT_SCRATCH_SIZE/1024); + tr_info("- Secondary BD: size %" PRIu64 "kiB", secondary_bd->size()/1024); + tr_info(" - Program (block) size %" PRIu64 " bytes", secondary_bd->get_program_size()); + tr_info(" - Erase (sector) size %" PRIu64 " bytes", secondary_bd->get_erase_size()); + tr_info(" "); + // Erase secondary slot // On the first boot, the secondary BlockDevice needs to be clean // If the first boot is not normal, please run the erase step, then reboot @@ -89,6 +100,9 @@ int main() tr_error("Cannot deinit secondary BlockDevice: %d", ret); } + // Give user time to react, as programming can be pretty fast + ThisThread::sleep_for(250ms); + // Activate the image in the secondary BlockDevice tr_info("> Image copied to secondary BlockDevice, press button to activate"); diff --git a/mbed-os b/mbed-os index b49e4f3..a9fa279 160000 --- a/mbed-os +++ b/mbed-os @@ -1 +1 @@ -Subproject commit b49e4f3d14f7ba344ef5afe4523495f95e90d746 +Subproject commit a9fa279a25847134d48fdaad4ddccc4353a2739d diff --git a/mbed_app.json5 b/mbed_app.json5 index 7fc15db..cb09a09 100644 --- a/mbed_app.json5 +++ b/mbed_app.json5 @@ -10,12 +10,16 @@ "macro_name": "DEMO_BUTTON_ACTIVE_LOW", "required": false }, + "secondary-slot-in-flash": { + "help": "If enabled, store the secondary slot in the application flash immediately after the primary slot.", + "value": false + }, + "secondary-slot-flash-start-addr": { + "help": "If secondary-slot-in-flash is enabled, this sets the start address of the secondary slot.", + "value": null + } }, - "macros": [ - "MBEDTLS_CIPHER_MODE_CTR" - ], - "target_overrides": { "*": { "mcuboot.bootloader-build": 0, @@ -81,8 +85,36 @@ "mcuboot.scratch-address": "0xE0000", "mcuboot.scratch-size": "0x20000", - "mcuboot.max-img-sectors": "0x180", + "mcuboot.max-img-sectors": "1536", // External SD card has smaller sector size, so divide slot size by read granularity "mcuboot.read-granularity": 512 // External SD card used as block device, this is its read size. + }, + + "MIMXRT1060_EVK": { + "target.memory_bank_config": { + "EXT_FLASH": { + "start": 0x60021000, // mcuboot.primary-slot-address + mcuboot.header-size + "size": 0x2FF000 // mcuboot.slot-size - mcuboot.header-size + } + }, + + // Primary slot is 3MiB and begins right after the bootloader + "mcuboot.primary-slot-address": "0x60020000", + "mcuboot.slot-size": "0x300000", + + // Use flash for secondary slot as well + "secondary-slot-in-flash": true, + "secondary-slot-flash-start-addr": "0x60320000", + + // Store the scratch space at the end of flash + "mcuboot.scratch-address": "0x60620000", + "mcuboot.scratch-size": "0x20000", + + "mcuboot.read-granularity": 1, // Flash is byte addressable + + "mcuboot.max-img-sectors": 768, // Maximum flash sectors per slot. 3MiB/4kiB = 768. + "mcuboot.flash-block-size": 256, + + "demo-button-active-low": true } - } + }, } diff --git a/mcuboot b/mcuboot index ce340d5..49ea56a 160000 --- a/mcuboot +++ b/mcuboot @@ -1 +1 @@ -Subproject commit ce340d5722ee4898137a1e8106988399db20bc53 +Subproject commit 49ea56a0009aa611bf2becceed85d59bbef05564 diff --git a/secondary_bd.cpp b/secondary_bd.cpp index 38e78be..b55326b 100644 --- a/secondary_bd.cpp +++ b/secondary_bd.cpp @@ -1,5 +1,5 @@ /* - * default_bd.cpp +* default_bd.cpp * * Created on: Jul 30, 2020 * Author: gdbeckstein @@ -15,8 +15,8 @@ mbed::BlockDevice* get_secondary_bd(void) { - // Use a section of FlashIAP immediately after the secondary slot - static FlashIAPBlockDevice flashBD(MCUBOOT_PRIMARY_SLOT_START_ADDR + MCUBOOT_SLOT_SIZE, MCUBOOT_SLOT_SIZE); + // Use FlashIAP for the secondary BD. + static FlashIAPBlockDevice flashBD(MBED_CONF_APP_SECONDARY_SLOT_FLASH_START_ADDR, MCUBOOT_SLOT_SIZE); return &flashBD; } @@ -33,7 +33,7 @@ mbed::BlockDevice* get_secondary_bd(void) { // Otherwise it will return the flash IAP block device. mbed::BlockDevice* default_bd = mbed::BlockDevice::get_default_instance(); - // If this assert fails, there is no block def + // If this assert fails, there is no default block device defined for your board. MBED_ASSERT(default_bd != nullptr); // mcuboot assumes that the read size of the secondary block device is the same as the read size From 915aa8fa1e015b9115ecc6153990ef4b809abe4b Mon Sep 17 00:00:00 2001 From: Jamie Smith Date: Thu, 2 Jan 2025 23:50:45 -0800 Subject: [PATCH 2/6] Update workflow --- .github/workflows/compile.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/compile.yml b/.github/workflows/compile.yml index 2d8d87e..68b06e1 100644 --- a/.github/workflows/compile.yml +++ b/.github/workflows/compile.yml @@ -14,6 +14,7 @@ jobs: mbed_target: # Change the below to match the target(s) you want to compile the project for. - K64F + - MIMXRT1060_EVK steps: - name: Checkout @@ -36,5 +37,5 @@ jobs: - name: Build project for ${{ matrix.mbed_target }} run: | mkdir build && cd build - cmake .. -GNinja -DMBED_TARGET=${{ matrix.mbed_target }} -DMCUBOOT_SIGNING_KEY=signing-keys.pem + cmake .. -GNinja -DMBED_TARGET=${{ matrix.mbed_target }} -DUPLOAD_METHOD=NONE -DMCUBOOT_SIGNING_KEY=signing-keys.pem ninja From e6e5a861b153509f6a1249a40ea8d81a1aa96118 Mon Sep 17 00:00:00 2001 From: Jamie Smith Date: Thu, 2 Jan 2025 23:55:37 -0800 Subject: [PATCH 3/6] Fix build with uploading disabled --- mcuboot | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mcuboot b/mcuboot index 49ea56a..2894851 160000 --- a/mcuboot +++ b/mcuboot @@ -1 +1 @@ -Subproject commit 49ea56a0009aa611bf2becceed85d59bbef05564 +Subproject commit 289485156355d4373735534b11baa7ab50a9d6c6 From 1283bf3c3283de74f00f06d87ec0127b3255c9f9 Mon Sep 17 00:00:00 2001 From: Jamie Smith Date: Sat, 4 Jan 2025 15:56:59 -0800 Subject: [PATCH 4/6] Fix silent failure of program when not aligned to flash blocks --- UpdaterApp.cpp | 7 ++++++- secondary_bd.cpp | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/UpdaterApp.cpp b/UpdaterApp.cpp index d1e8243..7194be3 100644 --- a/UpdaterApp.cpp +++ b/UpdaterApp.cpp @@ -91,7 +91,12 @@ int main() } // Copy the update image from internal flash to secondary BlockDevice - secondary_bd->program(&_binary_SimpleApp_update_image_bin_start, 0, SimpleApp_update_image_bin_length); + ret = secondary_bd->program(&_binary_SimpleApp_update_image_bin_start, 0, SimpleApp_update_image_bin_length); + if (ret == 0) { + tr_info("Image copied."); + } else { + tr_error("Cannot copy image: %d", ret); + } ret = secondary_bd->deinit(); if (ret == 0) { diff --git a/secondary_bd.cpp b/secondary_bd.cpp index b55326b..81d4298 100644 --- a/secondary_bd.cpp +++ b/secondary_bd.cpp @@ -17,7 +17,11 @@ mbed::BlockDevice* get_secondary_bd(void) { // Use FlashIAP for the secondary BD. static FlashIAPBlockDevice flashBD(MBED_CONF_APP_SECONDARY_SLOT_FLASH_START_ADDR, MCUBOOT_SLOT_SIZE); - return &flashBD; + + // Our UpdaterApp needs to be able to program the flash with single-byte granularity + static mbed::BufferedBlockDevice bufferedBD(&flashBD); + + return &bufferedBD; } #else From 8685bf871804d751338053460aee5eed4aa96100 Mon Sep 17 00:00:00 2001 From: Jamie Smith Date: Sat, 4 Jan 2025 16:04:29 -0800 Subject: [PATCH 5/6] Final Mbed OS hash --- mbed-os | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mbed-os b/mbed-os index a9fa279..25b05a1 160000 --- a/mbed-os +++ b/mbed-os @@ -1 +1 @@ -Subproject commit a9fa279a25847134d48fdaad4ddccc4353a2739d +Subproject commit 25b05a10ec8e1ce25318817587bc77a9cca61418 From abcebdd5c3c2b018926f722a47bd800ca1af491a Mon Sep 17 00:00:00 2001 From: Jamie Smith Date: Sun, 5 Jan 2025 09:26:55 -0800 Subject: [PATCH 6/6] NUCLEO_H743ZI2 support --- .github/workflows/compile.yml | 1 + SimpleApp.cpp | 1 - mbed_app.json5 | 25 +++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/.github/workflows/compile.yml b/.github/workflows/compile.yml index 68b06e1..8a6099d 100644 --- a/.github/workflows/compile.yml +++ b/.github/workflows/compile.yml @@ -15,6 +15,7 @@ jobs: # Change the below to match the target(s) you want to compile the project for. - K64F - MIMXRT1060_EVK + - NUCLEO_H743ZI2 steps: - name: Checkout diff --git a/SimpleApp.cpp b/SimpleApp.cpp index 2bc5898..382e1e4 100644 --- a/SimpleApp.cpp +++ b/SimpleApp.cpp @@ -63,7 +63,6 @@ int main() if (ret == 0) { tr_info("Current firmware set as confirmed"); - return 0; } else { diff --git a/mbed_app.json5 b/mbed_app.json5 index cb09a09..d570019 100644 --- a/mbed_app.json5 +++ b/mbed_app.json5 @@ -115,6 +115,31 @@ "mcuboot.flash-block-size": 256, "demo-button-active-low": true + }, + + "MCU_STM32H743xI": { + "target.memory_bank_config": { + "IROM1": { + "start": 0x08021000, // mcuboot.primary-slot-address + mcuboot.header-size + "size": 0xDF000 // mcuboot.slot-size - mcuboot.header-size + } + }, + + // Since STM32H743 boards have no external block device, keep everything in the MCU flash. + "app.secondary-slot-in-flash": true, + "app.secondary-slot-flash-start-addr": "0x08100000", + + // Slot size can be as big as 896k, since we need to reserve the first flash sector for the bootloader + // and the last flash sector for scratch space + "mcuboot.primary-slot-address": "0x08020000", + "mcuboot.max-img-sectors": "7", // 7 flash sectors per slot + "mcuboot.slot-size": "0xE0000", + "mcuboot.flash-block-size": 32, // Flash program size is 32 + + // STM32H7 flash sector size is 128k, so we need to make the scratch region at least that big + "mcuboot.scratch-address": "0x081E0000", + "mcuboot.scratch-size": "0x20000" + } }, }