diff --git a/.gitmodules b/.gitmodules index 85e0f84d9a05ce..a25da78977b130 100644 --- a/.gitmodules +++ b/.gitmodules @@ -162,3 +162,6 @@ [submodule "p6/lwip"] path = third_party/p6/p6_sdk/libs/lwip url = https://git.savannah.nongnu.org/git/lwip +[submodule "third_party/mbed-mcu-boot/repo"] + path = third_party/mbed-mcu-boot/repo + url = https://github.com/lambda-shuttle/mcuboot.git diff --git a/.vscode/launch.json b/.vscode/launch.json index aa8c9cd8d31dc0..6dcc5bad353f69 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -264,7 +264,7 @@ "configFiles": ["${input:mbedTarget}.tcl"], "overrideLaunchCommands": [ "monitor reset halt", - "monitor program {./build-${input:mbedTarget}/${input:mbedFlashProfile}/chip-mbed-unit-tests}", + "monitor program {./build-${input:mbedTarget}/${input:mbedFlashProfile}/chip-mbed-unit-tests.hex}", "monitor reset run", "quit" ], @@ -289,6 +289,49 @@ "quit" ], "showDevDebugOutput": false // When set to true, displays output of GDB. + }, + + { + "name": "Flash Mbed bootlaoder", + "type": "cortex-debug", + "request": "launch", + "cwd": "${workspaceRoot}/examples/platform/mbed/bootloader", + "executable": "./build-${input:mbedTarget}/${input:mbedFlashProfile}/chip-mbed-bootloader.elf", + "armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/pigweed/bin/", // Pigweed environment bootstraping required + "servertype": "openocd", + "openocdPath": "${env:OPENOCD_PATH/bin}", + "searchDir": [ + "${workspaceRoot}/config/mbed/scripts", + "${env:OPENOCD_PATH/scripts}" + ], + "configFiles": ["${input:mbedTarget}.tcl"], + "overrideLaunchCommands": [ + "monitor reset halt", + "monitor program {./build-${input:mbedTarget}/${input:mbedFlashProfile}/chip-mbed-bootloader.hex}", + "monitor reset run", + "quit" + ], + "numberOfProcessors": 2, + "targetProcessor": 1, // Set to 0 for the CM0+, set to 1 for the CM4 + "showDevDebugOutput": false // When set to true, displays output of GDB. + }, + + { + "name": "Flash Mbed bootloader [remote]", + "type": "cortex-debug", + "request": "launch", + "cwd": "${workspaceRoot}/examples/platform/mbed/bootloader", + "executable": "./build-${input:mbedTarget}/${input:mbedFlashProfile}/chip-mbed-bootloader.elf", + "armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/pigweed/bin/", // Pigweed environment bootstraping required + "servertype": "external", + "gdbTarget": "host.docker.internal:3334", //port 3333 for the CM0+, 3334 for the CM4 + "overrideLaunchCommands": [ + "monitor reset halt", + "load ./build-${input:mbedTarget}/${input:mbedFlashProfile}/chip-mbed-bootloader.hex", + "monitor reset run", + "quit" + ], + "showDevDebugOutput": false // When set to true, displays output of GDB. } ], "inputs": [ diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 60eaca349383cc..c070c8c3247860 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -201,7 +201,8 @@ "pigweed-app", "all-clusters-app", "shell", - "ota-requestor-app" + "ota-requestor-app", + "bootloader" ], "default": "lock-app" }, diff --git a/examples/platform/mbed/bootloader/.gitignore b/examples/platform/mbed/bootloader/.gitignore new file mode 100644 index 00000000000000..721b8439436940 --- /dev/null +++ b/examples/platform/mbed/bootloader/.gitignore @@ -0,0 +1,5 @@ +build-*/ +dist/ +imgtool* +mcuboot +signing* diff --git a/examples/platform/mbed/bootloader/CMakeLists.txt b/examples/platform/mbed/bootloader/CMakeLists.txt new file mode 100644 index 00000000000000..eecdaf64fa2e33 --- /dev/null +++ b/examples/platform/mbed/bootloader/CMakeLists.txt @@ -0,0 +1,41 @@ +# Copyright (c) 2021 ARM Limited. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.19.0 FATAL_ERROR) + +set(MBED_PATH ${MBED_OS_PATH} CACHE INTERNAL "") +set(MCUBOOT_PATH ${MBED_MCU_BOOT_PATH} CACHE INTERNAL "") +set(MBED_CONFIG_PATH ${CMAKE_CURRENT_BINARY_DIR} CACHE INTERNAL "") +set(APP_TARGET chip-mbed-bootloader) + +include(${MBED_PATH}/tools/cmake/app.cmake) + +project(${APP_TARGET}) + +add_subdirectory(${MBED_PATH} ./mbed_build) +add_subdirectory(${MCUBOOT_PATH}/boot/bootutil/ ./mbed_mcu_boot_util) +add_subdirectory(${MCUBOOT_PATH}/boot/mbed/ ./mbed_mcu_boot) # 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() diff --git a/examples/platform/mbed/bootloader/default_bd.cpp b/examples/platform/mbed/bootloader/default_bd.cpp new file mode 100644 index 00000000000000..a2b1c744179373 --- /dev/null +++ b/examples/platform/mbed/bootloader/default_bd.cpp @@ -0,0 +1,78 @@ +/* + * default_bd.cpp + * + * Created on: Jul 30, 2020 + * Author: gdbeckstein + */ + +#include "BlockDevice.h" + +#include "SlicingBlockDevice.h" + +#if COMPONENT_SPIF +#include "SPIFBlockDevice.h" +#endif + +#if COMPONENT_QSPIF +#include "QSPIFBlockDevice.h" +#endif + +#if COMPONENT_DATAFLASH +#include "DataFlashBlockDevice.h" +#endif + +#if COMPONENT_SD +#include "SDBlockDevice.h" + +#if (STATIC_PINMAP_READY) +const spi_pinmap_t static_spi_pinmap = get_spi_pinmap(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, NC); +#endif +#endif + +BlockDevice * BlockDevice::get_default_instance() +{ +#if COMPONENT_SPIF + + static SPIFBlockDevice default_bd; + + return &default_bd; + +#elif COMPONENT_QSPIF + + static QSPIFBlockDevice default_bd; + + return &default_bd; + +#elif COMPONENT_DATAFLASH + + static DataFlashBlockDevice default_bd; + + return &default_bd; + +#elif COMPONENT_SD + +#if (STATIC_PINMAP_READY) + static SDBlockDevice default_bd(static_spi_pinmap, MBED_CONF_SD_SPI_CS); +#else + static SDBlockDevice default_bd; +#endif + + return &default_bd; + +#else + + return NULL; + +#endif +} + +/** + * 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) +{ + mbed::BlockDevice * default_bd = mbed::BlockDevice::get_default_instance(); + static mbed::SlicingBlockDevice sliced_bd(default_bd, 0x0, MCUBOOT_SLOT_SIZE); + return &sliced_bd; +} \ No newline at end of file diff --git a/examples/platform/mbed/bootloader/mbed_app.json b/examples/platform/mbed/bootloader/mbed_app.json new file mode 100644 index 00000000000000..a21ba94ccd1960 --- /dev/null +++ b/examples/platform/mbed/bootloader/mbed_app.json @@ -0,0 +1,35 @@ +{ + "requires": ["bare-metal", "mbedtls", "mcuboot", "flashiap-block-device", "spif-driver", "qspif", "mbed-trace"], + "config": { + "serial-bootloader-enable": { + "help": "Build bootloader with serial update support", + "value": 0 + } + }, + "target_overrides": { + "*": { + "platform.stdio-baud-rate": 115200, + "target.restrict_size": "0x20000", + "target.c_lib": "small", + "mcuboot.log-level": "MCUBOOT_LOG_LEVEL_DEBUG", + "mbed-trace.enable": true, + "mbed-trace.max-level": "TRACE_LEVEL_DEBUG", + "mbed-trace.fea-ipv6": false + }, + "CY8CPROTO_062_4343W": { + "target.network-default-interface-type": "WIFI", + "target.macros_add": [ + "MXCRYPTO_DISABLED", + "NL_ASSERT_LOG=NL_ASSERT_LOG_DEFAULT", + "NL_ASSERT_EXPECT_FLAGS=NL_ASSERT_FLAG_LOG", + "WHD_PRINT_DISABLE" + ], + "mcuboot.primary-slot-address": "0x10020000", + "mcuboot.slot-size": "0xC0000", + "mcuboot.scratch-address": "0x100E0000", + "mcuboot.scratch-size": "0x20000", + "mcuboot.max-img-sectors": "0x180", + "mcuboot.read-granularity": 512 + } + } +} diff --git a/scripts/examples/mbed_example.sh b/scripts/examples/mbed_example.sh index d042a8206e820e..8e1b2d6571bf85 100755 --- a/scripts/examples/mbed_example.sh +++ b/scripts/examples/mbed_example.sh @@ -22,7 +22,7 @@ cd "$CHIP_ROOT"/examples SUPPORTED_TOOLCHAIN=(GCC_ARM ARM) SUPPORTED_TARGET_BOARD=(CY8CPROTO_062_4343W) -SUPPORTED_APP=(lock-app lighting-app pigweed-app all-clusters-app shell ota-requestor-app) +SUPPORTED_APP=(lock-app lighting-app pigweed-app all-clusters-app shell ota-requestor-app bootloader) SUPPORTED_PROFILES=(release develop debug) SUPPORTED_COMMAND=(build flash build-flash) @@ -93,6 +93,47 @@ source "$CHIP_ROOT"/scripts/activate.sh # Build directory setup BUILD_DIRECTORY="$APP"/mbed/build-"$TARGET_BOARD"/"$PROFILE"/ +if [[ "$APP" == "bootloader" ]]; then + echo "Build $APP app for $TARGET_BOARD target with $TOOLCHAIN toolchain and $PROFILE profile" + + cd platform/mbed/bootloader + BUILD_DIRECTORY=build-"$TARGET_BOARD"/"$PROFILE"/ + + # Set Mbed OS path + MBED_OS_PATH="$CHIP_ROOT"/third_party/mbed-os/repo + + # Set Mbed MCU boot path + MBED_MCU_BOOT_PATH="$CHIP_ROOT"/third_party/mbed-mcu-boot/repo + + # Install mcuboot requirements (silently) + pip install -q -r "$MBED_MCU_BOOT_PATH"/scripts/requirements.txt || + fail "Unable to install mcuboot requirements" "Please take a look at "$MBED_MCU_BOOT_PATH"/scripts/requirements.txt" + + # Run mcuboot setup script + python "$MBED_MCU_BOOT_PATH"/scripts/setup.py install || + fail "MCUboot setup script failed" + + # Create the signing keys + # shellcheck disable=SC2015 + "$MBED_MCU_BOOT_PATH"/scripts/imgtool.py keygen -k signing-keys.pem -t rsa-2048 && + "$MBED_MCU_BOOT_PATH"/scripts/imgtool.py getpub -k signing-keys.pem >signing_keys.c || + fail "Unable to create the signing keys" + + ln -sfTr "$MBED_MCU_BOOT_PATH"/boot/mbed mcuboot + + # Generate config file for selected target, toolchain and hardware + mbed-tools configure -t "$TOOLCHAIN" -m "$TARGET_BOARD" -o "$BUILD_DIRECTORY" --mbed-os-path "$MBED_OS_PATH" + + # Remove old artifacts to force linking + rm -rf "$BUILD_DIRECTORY/chip-"* + + # Build application + cmake -S . -B "$BUILD_DIRECTORY" -GNinja -DCMAKE_BUILD_TYPE="$PROFILE" -DMBED_OS_PATH="$MBED_OS_PATH" -DMBED_MCU_BOOT_PATH="$MBED_MCU_BOOT_PATH" + cmake --build "$BUILD_DIRECTORY" + + exit +fi + if [[ "$COMMAND" == *"build"* ]]; then echo "Build $APP app for $TARGET_BOARD target with $TOOLCHAIN toolchain and $PROFILE profile" diff --git a/third_party/mbed-mcu-boot/repo b/third_party/mbed-mcu-boot/repo new file mode 160000 index 00000000000000..c1233dbea300ff --- /dev/null +++ b/third_party/mbed-mcu-boot/repo @@ -0,0 +1 @@ +Subproject commit c1233dbea300ff54a80bb882f3a4bc57b02541c1