diff --git a/.github/workflows/compile.yml b/.github/workflows/compile.yml index 2d8d87e..8a6099d 100644 --- a/.github/workflows/compile.yml +++ b/.github/workflows/compile.yml @@ -14,6 +14,8 @@ jobs: mbed_target: # Change the below to match the target(s) you want to compile the project for. - K64F + - MIMXRT1060_EVK + - NUCLEO_H743ZI2 steps: - name: Checkout @@ -36,5 +38,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 diff --git a/SimpleApp.cpp b/SimpleApp.cpp index 5a1f321..382e1e4 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(); @@ -60,7 +63,6 @@ int main() if (ret == 0) { tr_info("Current firmware set as confirmed"); - return 0; } else { @@ -81,6 +83,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 +92,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..7194be3 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 @@ -80,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) { @@ -89,6 +105,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..25b05a1 160000 --- a/mbed-os +++ b/mbed-os @@ -1 +1 @@ -Subproject commit b49e4f3d14f7ba344ef5afe4523495f95e90d746 +Subproject commit 25b05a10ec8e1ce25318817587bc77a9cca61418 diff --git a/mbed_app.json5 b/mbed_app.json5 index 7fc15db..d570019 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,61 @@ "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 + }, + + "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" + } - } + }, } diff --git a/mcuboot b/mcuboot index ce340d5..2894851 160000 --- a/mcuboot +++ b/mcuboot @@ -1 +1 @@ -Subproject commit ce340d5722ee4898137a1e8106988399db20bc53 +Subproject commit 289485156355d4373735534b11baa7ab50a9d6c6 diff --git a/secondary_bd.cpp b/secondary_bd.cpp index 38e78be..81d4298 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,9 +15,13 @@ 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); - return &flashBD; + // Use FlashIAP for the secondary BD. + static FlashIAPBlockDevice flashBD(MBED_CONF_APP_SECONDARY_SLOT_FLASH_START_ADDR, MCUBOOT_SLOT_SIZE); + + // Our UpdaterApp needs to be able to program the flash with single-byte granularity + static mbed::BufferedBlockDevice bufferedBD(&flashBD); + + return &bufferedBD; } #else @@ -33,7 +37,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