diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index da8a7e2..0000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,18 +0,0 @@ -# Python CircleCI 2.0 configuration file -# -# Check https://circleci.com/docs/2.0/language-python/ for more details -# -version: 2 -jobs: - build: - docker: - - image: mbedos/mbed-os-env:stable - working_directory: ~ - steps: - - checkout: - path: mbed-os-example-blinky - - run: | - cd mbed-os-example-blinky - mbed deploy - mbed compile -t GCC_ARM -m K64F - diff --git a/.github/workflows/compile.yml b/.github/workflows/compile.yml new file mode 100644 index 0000000..5187ef1 --- /dev/null +++ b/.github/workflows/compile.yml @@ -0,0 +1,33 @@ +# Example GitHub Actions workflow which provides a CI build for your Mbed CE project. + +name: Test that this Mbed project compiles + +on: push + +jobs: + compile: + runs-on: ubuntu-latest + container: ghcr.io/armmbed/mbed-os-env:master-latest + + strategy: + matrix: + mbed_target: + # Change the below to match the target(s) you want to compile the project for. + - K64F + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Install python3-venv + run: | + apt-get update + apt-get install -y python3-venv + + - name: Build project for ${{ matrix.mbed_target }} + run: | + mkdir build && cd build + cmake .. -GNinja -DMBED_TARGET=${{ matrix.mbed_target }} + ninja diff --git a/.gitignore b/.gitignore index 57974bd..501193d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,41 @@ -.build -.mbed -projectfiles -*.py* +# Private key file +signing-keys.pem + +# CLion build dirs +cmake-build-*/ + +# User-specific CLion stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf +.idea/runConfigurations + +# Generated CLion files +.idea/**/contentModel.xml + +# Sensitive or high-churn CLion files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# VS Code build dir +build/ + +# VS Code config files +.vscode/ + +# Python stuff +*.pyc + +# Mac stuff +.DS_Store \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..288ea54 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "mbed-os"] + path = mbed-os + url = https://github.com/mbed-ce/mbed-os.git +[submodule "mcuboot"] + path = mcuboot + url = https://github.com/multiplemonomials/mcuboot.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 5998ea4..ff2c181 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,41 +1,41 @@ -# Copyright (c) 2021 ARM Limited. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 +cmake_minimum_required(VERSION 3.19) +cmake_policy(VERSION 3.19) -# Mbed-MCUboot Demo Application +set(MBED_APP_JSON_PATH mbed_app.json5) -cmake_minimum_required(VERSION 3.19.0 FATAL_ERROR) +include(mbed-os/tools/cmake/app.cmake) -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 application) +# Make sure that the build system generates both bin and hex files, regardless of +# target settings. +set(MBED_OUTPUT_EXT "" CACHE STRING "" FORCE) -include(${MBED_PATH}/tools/cmake/app.cmake) +add_subdirectory(mbed-os) -project(${APP_TARGET}) +project(mbed-mcuboot-demo-app) -add_subdirectory(${MBED_PATH}) -add_subdirectory(${MCUBOOT_PATH}/boot/bootutil/) -add_subdirectory(${MCUBOOT_PATH}/boot/mbed/) # Mbed-MCUboot Port +# Compile mcuboot sources +add_subdirectory(mcuboot/boot/bootutil) +add_subdirectory(mcuboot/boot/mbed) -add_executable(${APP_TARGET}) +add_executable(SimpleApp SimpleApp.cpp secondary_bd.cpp) +target_link_libraries(SimpleApp + mbed-baremetal + mbed-storage + mbed-mcuboot) +mbed_set_post_build(SimpleApp) -target_sources(${APP_TARGET} - PUBLIC - main.cpp -) +add_executable(UpdaterApp UpdaterApp.cpp secondary_bd.cpp) +target_link_libraries(UpdaterApp + mbed-os + mbed-storage + mbed-mcuboot) +mbed_set_post_build(UpdaterApp) -target_link_libraries(${APP_TARGET} - PUBLIC - bootutil - mbed-mcuboot - mbed-storage - mbed-os -) +# Time for a bit of CMake magic: we take the bin file for SimpleApp, then link it into +# UpdaterApp as block of constant data. The name in code is based on the filename passed +# here to the linker. +# See here for more details on this: https://gareus.org/wiki/embedding_resources_in_executables +target_link_options(UpdaterApp PRIVATE -Wl,-b,binary,$.bin -Wl,-b,elf32-littlearm) +add_dependencies(UpdaterApp SimpleApp) # Ensure SimpleApp gets built before UpdaterApp -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 +mbed_finalize_build() \ No newline at end of file diff --git a/README.md b/README.md index 33682a4..b381d0d 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ This example project shows how to configure an Mbed-OS application to be booted by an mcuboot bootloader. -All information for this example has been moved to the combined README in the bootloader repository, [`mbed-mcuboot-demo`](https://github.com/AGlass0fMilk/mbed-mcuboot-demo). +All information for this example has been moved to the combined README in the bootloader repository, [`mbed-mcuboot-bootloader`](https://github.com/mbed-ce/mbed-mcuboot-bootloader). ### License and contributions diff --git a/SimpleApp.cpp b/SimpleApp.cpp new file mode 100644 index 0000000..cf5d7c8 --- /dev/null +++ b/SimpleApp.cpp @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2024 Jamie Smith + * SPDX-License-Identifier: Apache-2.0 + * + * The Simple App is an application which simply has the ability to set the + * update complete flag, and otherwise just blinks the LED and prints messages. + */ + +#include "mbed.h" +#include "mbed-trace/mbed_trace.h" + +#include "bootutil/bootutil.h" + +#define TRACE_GROUP "SimpleApp" + +#if DEMO_BUTTON_ACTIVE_LOW +#define DEMO_BUTTON_IS_PRESSED !btn +#else +#define DEMO_BUTTON_IS_PRESSED btn +#endif + +int main() +{ + // Enable traces from relevant trace groups + mbed_trace_init(); + mbed_trace_include_filters_set("SimpleApp,MCUb,BL"); + + DigitalIn btn(DEMO_BUTTON); + DigitalOut led(LED1); + + // Check if an update has been performed + int swap_type = boot_swap_type(); + int ret; + switch (swap_type) + { + case BOOT_SWAP_TYPE_NONE: + tr_info("Regular boot"); + break; + + case BOOT_SWAP_TYPE_REVERT: + // After MCUboot has swapped a (non-permanent) update image + // into the primary slot, it defaults to reverting the image on the NEXT boot. + // This is why we see "[INFO][MCUb]: Swap type: revert" which can be misleading. + // Confirming the CURRENT boot dismisses the reverting. + tr_info("Firmware update applied successfully"); + + // Do whatever is needed to verify the firmware is okay (eg: self test) + // then mark the update as successful. Here we let the user press a button. + tr_info("Press the button to confirm, or reboot to revert the update"); + + while (DEMO_BUTTON_IS_PRESSED) + { + ThisThread::sleep_for(10ms); + } + + ret = boot_set_confirmed(); + if (ret == 0) + { + tr_info("Current firmware set as confirmed"); + return 0; + } + else + { + tr_error("Failed to confirm the firmware: %d", ret); + } + break; + + // Note: Below are intermediate states of MCUboot and + // should never reach the application... + case BOOT_SWAP_TYPE_FAIL: // Unable to boot due to invalid image + case BOOT_SWAP_TYPE_PERM: // Permanent update requested (when signing the image) and to be performed + case BOOT_SWAP_TYPE_TEST: // Revertable update requested and to be performed + case BOOT_SWAP_TYPE_PANIC: // Unrecoverable error + default: + tr_error("Unexpected swap type: %d", swap_type); + } + + while(true) + { + ThisThread::sleep_for(1s); + printf("Simple app is running...\n"); + led = 1; + ThisThread::sleep_for(250ms); + led = 0; + ThisThread::sleep_for(250ms); + led = 1; + ThisThread::sleep_for(250ms); + led = 0; + } +} \ No newline at end of file diff --git a/UpdaterApp.cpp b/UpdaterApp.cpp new file mode 100644 index 0000000..968c52d --- /dev/null +++ b/UpdaterApp.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2020 Embedded Planet + * Copyright (c) 2020 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "mbed.h" + +#include "bootutil/bootutil.h" +#include "bootutil/image.h" +#include "flash_map_backend/secondary_bd.h" + +#include "FlashIAP/FlashIAPBlockDevice.h" +#include "blockdevice/BufferedBlockDevice.h" +#include "drivers/InterruptIn.h" + +#define TRACE_GROUP "UpdaterApp" +#include "mbed-trace/mbed_trace.h" + +#if DEMO_BUTTON_ACTIVE_LOW +#define DEMO_BUTTON_IS_PRESSED !btn +#else +#define DEMO_BUTTON_IS_PRESSED btn +#endif + +// SimpleApp.bin gets loaded into ram between these addresses. +// See CMakeLists.txt for details on how this is done. +extern "C" uint8_t _binary_SimpleApp_bin_start; +extern "C" uint8_t _binary_SimpleApp_bin_end; +const size_t SimpleApp_bin_length = &_binary_SimpleApp_bin_end - &_binary_SimpleApp_bin_start; + +int main() +{ + // Enable traces from relevant trace groups + mbed_trace_init(); + mbed_trace_include_filters_set("UpdaterApp,MCUb,BL"); + + DigitalIn btn(DEMO_BUTTON); + + // 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 + + tr_info("> Press button to erase secondary slot"); + + while(!DEMO_BUTTON_IS_PRESSED) { + ThisThread::sleep_for(10ms); + } + + BlockDevice *secondary_bd = get_secondary_bd(); + int ret = secondary_bd->init(); + if (ret == 0) { + tr_info("Secondary BlockDevice inited"); + } else { + tr_error("Cannot init secondary BlockDevice: %d", ret); + } + + // Use a buffered block device to allow arbitrary length writes to the underlying BD + BufferedBlockDevice bufferedSecBD(secondary_bd); + + tr_info("Erasing secondary BlockDevice..."); + ret = bufferedSecBD.erase(0, bufferedSecBD.size()); + if (ret == 0) { + tr_info("Secondary BlockDevice erased"); + } else { + tr_error("Cannot erase secondary BlockDevice: %d", ret); + } + + tr_info("> Press button to copy update image to secondary BlockDevice"); + + while(!DEMO_BUTTON_IS_PRESSED) { + ThisThread::sleep_for(10ms); + } + + // Copy the update image from internal flash to secondary BlockDevice + bufferedSecBD.program(&_binary_SimpleApp_bin_start, 0, SimpleApp_bin_length); + + // Activate the image in the secondary BlockDevice + tr_info("> Image copied to secondary BlockDevice, press button to activate"); + + while(!DEMO_BUTTON_IS_PRESSED) { + ThisThread::sleep_for(10ms); + } + + ret = boot_set_pending(false); + if (ret == 0) { + tr_info("> Secondary image pending, reboot to update"); + } else { + tr_error("Failed to set secondary image pending: %d", ret); + } +} diff --git a/main.cpp b/main.cpp deleted file mode 100644 index 9f4a558..0000000 --- a/main.cpp +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright (c) 2020 Embedded Planet - * Copyright (c) 2020 ARM Limited - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "mbed.h" - -#include "bootutil/bootutil.h" -#include "bootutil/image.h" -#include "FlashIAP/FlashIAPBlockDevice.h" -#include "blockdevice/SlicingBlockDevice.h" -#include "drivers/InterruptIn.h" - -#define TRACE_GROUP "main" -#include "mbed-trace/mbed_trace.h" - -mbed::BlockDevice* get_secondary_bd(void) { - mbed::BlockDevice* default_bd = mbed::BlockDevice::get_default_instance(); - static mbed::SlicingBlockDevice sliced_bd(default_bd, 0x0, MCUBOOT_SLOT_SIZE); - return &sliced_bd; -} - -int main() -{ - // Enable traces from relevant trace groups - mbed_trace_init(); - mbed_trace_include_filters_set("main,MCUb,BL"); - - InterruptIn btn(DEMO_BUTTON); - - // Check if an update has been performed - int swap_type = boot_swap_type(); - int ret; - switch(swap_type) { - case BOOT_SWAP_TYPE_NONE: - tr_info("Regular boot"); - break; - - case BOOT_SWAP_TYPE_REVERT: - // After MCUboot has swapped a (non-permanent) update image - // into the primary slot, it defaults to reverting the image on the NEXT boot. - // This is why we see "[INFO][MCUb]: Swap type: revert" which can be misleading. - // Confirming the CURRENT boot dismisses the reverting. - tr_info("Firmware update applied successfully"); - - // Do whatever is needed to verify the firmware is okay (eg: self test) - // then mark the update as successful. Here we let the user press a button. - tr_info("Press the button to confirm, or reboot to revert the update"); -#if DEMO_BUTTON_ACTIVE_LOW - while (btn) { -#else - while (!btn) { -#endif - sleep(); - } - - ret = boot_set_confirmed(); - if (ret == 0) { - tr_info("Current firmware set as confirmed"); - return 0; - } else { - tr_error("Failed to confirm the firmware: %d", ret); - } - break; - - // Note: Below are intermediate states of MCUboot and - // should never reach the application... - case BOOT_SWAP_TYPE_FAIL: // Unable to boot due to invalid image - case BOOT_SWAP_TYPE_PERM: // Permanent update requested (when signing the image) and to be performed - case BOOT_SWAP_TYPE_TEST: // Revertable update requested and to be performed - case BOOT_SWAP_TYPE_PANIC: // Unrecoverable error - default: - tr_error("Unexpected swap type: %d", swap_type); - } - - // 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 - - tr_info("> Press button to erase secondary slot"); - -#if DEMO_BUTTON_ACTIVE_LOW - while (btn) { -#else - while (!btn) { -#endif - sleep(); - } - - BlockDevice *secondary_bd = get_secondary_bd(); - ret = secondary_bd->init(); - if (ret == 0) { - tr_info("Secondary BlockDevice inited"); - } else { - tr_error("Cannot init secondary BlockDevice: %d", ret); - } - - tr_info("Erasing secondary BlockDevice..."); - ret = secondary_bd->erase(0, secondary_bd->size()); - if (ret == 0) { - tr_info("Secondary BlockDevice erased"); - } else { - tr_error("Cannot erase secondary BlockDevice: %d", ret); - } - - tr_info("> Press button to copy update image to secondary BlockDevice"); - -#if DEMO_BUTTON_ACTIVE_LOW - while (btn) { -#else - while (!btn) { -#endif - sleep(); - } - - // Copy the update image from internal flash to secondary BlockDevice - // This is a "hack" that requires you to preload the update image into `mcuboot.primary-slot-address` + 0x40000 - - FlashIAPBlockDevice fbd(MCUBOOT_PRIMARY_SLOT_START_ADDR + 0x40000, 0x20000); - ret = fbd.init(); - if (ret == 0) { - tr_info("FlashIAPBlockDevice inited"); - } else { - tr_error("Cannot init FlashIAPBlockDevice: %d", ret); - } - - static uint8_t buffer[0x1000]; - for (size_t offset = 0; offset < 0x20000; offset+= sizeof(buffer)) { - ret = fbd.read(buffer, offset, sizeof(buffer)); - if (ret != 0) { - tr_error("Failed to read FlashIAPBlockDevice at offset %u", offset); - } - ret = secondary_bd->program(buffer, offset, sizeof(buffer)); - if (ret != 0) { - tr_error("Failed to program secondary BlockDevice at offset %u", offset); - } - } - - // Activate the image in the secondary BlockDevice - - tr_info("> Image copied to secondary BlockDevice, press button to activate"); - -#if DEMO_BUTTON_ACTIVE_LOW - while (btn) { -#else - while (!btn) { -#endif - sleep(); - } - - ret = boot_set_pending(false); - if (ret == 0) { - tr_info("> Secondary image pending, reboot to update"); - } else { - tr_error("Failed to set secondary image pending: %d", ret); - } -} diff --git a/mbed-os b/mbed-os new file mode 160000 index 0000000..615d0dd --- /dev/null +++ b/mbed-os @@ -0,0 +1 @@ +Subproject commit 615d0ddd8cb02bb8035743b16a74c6db701b06a5 diff --git a/mbed-os.lib b/mbed-os.lib deleted file mode 100644 index 68e51ac..0000000 --- a/mbed-os.lib +++ /dev/null @@ -1 +0,0 @@ -https://github.com/ARMmbed/mbed-os/#3377f083b3a6bd7a1b45ed2cea5cf083b9007527 diff --git a/mbed_app.json b/mbed_app.json deleted file mode 100644 index ae75e93..0000000 --- a/mbed_app.json +++ /dev/null @@ -1,62 +0,0 @@ -{ - "config": { - "demo-button": { - "macro_name": "DEMO_BUTTON", - "required": true, - "value": "BUTTON1" - }, - "demo-button-active-low": { - "help": "true if the button state is low when pressed, high when released", - "macro_name": "DEMO_BUTTON_ACTIVE_LOW", - "required": false - }, - "mbed_app_start": { - "help": "Use a custom application start address", - "macro_name": "MBED_APP_START", - "required": true - } - }, - "target_overrides": { - "*": { - "mcuboot.bootloader-build": 0, - "target.c_lib": "small", - "target.OUTPUT_EXT": "hex", - "mcuboot.log-level": "MCUBOOT_LOG_LEVEL_DEBUG", - "mbed-trace.enable": true - }, - "NRF52840_DK": { - "demo-button-active-low": true, - "mbed_app_start": "0x21000", - "target.mbed_app_size": "0xBE000", - "mcuboot.primary-slot-address": "0x20000", - "mcuboot.slot-size": "0xC0000", - "mcuboot.scratch-address": "0xE0000", - "mcuboot.scratch-size": "0x20000", - "mcuboot.max-img-sectors": "0x180", - "mcuboot.read-granularity": 4, - "qspif.QSPI_MIN_PROG_SIZE": 4 - }, - "EP_AGORA": { - "demo-button-active-low": true, - "mbed_app_start": "0x21000", - "target.mbed_app_size": "0xBE000", - "mcuboot.primary-slot-address": "0x20000", - "mcuboot.slot-size": "0xC0000", - "mcuboot.scratch-address": "0xE0000", - "mcuboot.scratch-size": "0x20000", - "mcuboot.max-img-sectors": "0x180", - "mcuboot.read-granularity": 4, - "qspif.QSPI_MIN_PROG_SIZE": 4 - }, - "DISCO_L475VG_IOT01A": { - "demo-button-active-low": true, - "mbed_app_start": "0x8021000", - "target.mbed_app_size": "0xBE000", - "mcuboot.primary-slot-address": "0x8020000", - "mcuboot.slot-size": "0xC0000", - "mcuboot.scratch-address": "0x80E0000", - "mcuboot.scratch-size": "0x20000", - "mcuboot.max-img-sectors": "0x180" - } - } -} diff --git a/mbed_app.json5 b/mbed_app.json5 new file mode 100644 index 0000000..a65ab69 --- /dev/null +++ b/mbed_app.json5 @@ -0,0 +1,78 @@ +{ + "config": { + "demo-button": { + "macro_name": "DEMO_BUTTON", + "required": true, + "value": "BUTTON1" + }, + "demo-button-active-low": { + "help": "true if the button state is low when pressed, high when released", + "macro_name": "DEMO_BUTTON_ACTIVE_LOW", + "required": false + }, + }, + "target_overrides": { + "*": { + "mcuboot.bootloader-build": 0, + "target.c_lib": "small", + "mcuboot.log-level": "MCUBOOT_LOG_LEVEL_DEBUG", + "mbed-trace.enable": true + }, +// "NRF52840_DK": { +// "demo-button-active-low": true, +// "mbed_app_start": "0x21000", +// "target.mbed_app_size": "0xBE000", +// "mcuboot.primary-slot-address": "0x20000", +// "mcuboot.slot-size": "0xC0000", +// "mcuboot.scratch-address": "0xE0000", +// "mcuboot.scratch-size": "0x20000", +// "mcuboot.max-img-sectors": "0x180", +// "mcuboot.read-granularity": 4, +// "qspif.QSPI_MIN_PROG_SIZE": 4 +// }, +// "EP_AGORA": { +// "demo-button-active-low": true, +// "mbed_app_start": "0x21000", +// "target.mbed_app_size": "0xBE000", +// "mcuboot.primary-slot-address": "0x20000", +// "mcuboot.slot-size": "0xC0000", +// "mcuboot.scratch-address": "0xE0000", +// "mcuboot.scratch-size": "0x20000", +// "mcuboot.max-img-sectors": "0x180", +// "mcuboot.read-granularity": 4, +// "qspif.QSPI_MIN_PROG_SIZE": 4 +// }, +// "DISCO_L475VG_IOT01A": { +// "demo-button-active-low": true, +// "mbed_app_start": "0x8021000", +// "target.mbed_app_size": "0xBE000", +// "mcuboot.primary-slot-address": "0x8020000", +// "mcuboot.slot-size": "0xC0000", +// "mcuboot.scratch-address": "0x80E0000", +// "mcuboot.scratch-size": "0x20000", +// "mcuboot.max-img-sectors": "0x180" +// } + + "K64F": { + // Configure application to start after the end of the application header + "target.memory_bank_config": { + "IROM1": { + "start": 0x21000, // bootloader size + application header size + "size": 0xDE000 // flash size - bootloader size - application header size - application trailer size + } + }, + + // On K64F we store the secondary slot in external memory, not internal. + // So, the primary slot can take up most of flash. + "mcuboot.primary-slot-address": "0x20000", + "mcuboot.slot-size": "0xC0000", + + // Store the scratch space at the end of flash + "mcuboot.scratch-address": "0xE0000", + "mcuboot.scratch-size": "0x20000", + + "mcuboot.max-img-sectors": "0x180", + "mcuboot.read-granularity": 512 // External SD card used as block device, this is its read size. + } + } +} diff --git a/mcuboot b/mcuboot new file mode 160000 index 0000000..464c796 --- /dev/null +++ b/mcuboot @@ -0,0 +1 @@ +Subproject commit 464c79661da3409ebe4abb8c4bb09f28d5ec5907 diff --git a/mcuboot.lib b/mcuboot.lib deleted file mode 100644 index 120b416..0000000 --- a/mcuboot.lib +++ /dev/null @@ -1 +0,0 @@ -https://github.com/lambda-shuttle/mcuboot.git diff --git a/secondary_bd.cpp b/secondary_bd.cpp new file mode 100644 index 0000000..52c1ba6 --- /dev/null +++ b/secondary_bd.cpp @@ -0,0 +1,43 @@ +/* + * default_bd.cpp + * + * Created on: Jul 30, 2020 + * Author: gdbeckstein + */ + +#include "BlockDevice.h" + +#include "SlicingBlockDevice.h" +#include "FlashIAPBlockDevice.h" + +#if MBED_CONF_APP_SECONDARY_SLOT_IN_FLASH + +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; +} + +#else + +/** + * You can override this function to suit your hardware/memory configuration + * By default it simply returns what is returned by BlockDevice::get_default_instance(); + */ +mbed::BlockDevice* get_secondary_bd(void) { + + // Use the PlatformStorage API to get the "default" block device for this project. + // This will return the (O/Q)SPI flash or SD card instance if those components are available. + // 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 + MBED_ASSERT(default_bd != nullptr); + + // In this case, our flash is much larger than a single image so + // slice it into the size of an image slot + static mbed::SlicingBlockDevice sliced_bd(default_bd, 0x0, MCUBOOT_SLOT_SIZE); + return &sliced_bd; +} +#endif