diff --git a/config/mbed/CMakeLists.txt b/config/mbed/CMakeLists.txt index d467d6fee56672..fd5f4d37a7f40f 100644 --- a/config/mbed/CMakeLists.txt +++ b/config/mbed/CMakeLists.txt @@ -469,6 +469,33 @@ if (CONFIG_CHIP_OTA_REQUESTOR) ) endif(CONFIG_CHIP_OTA_REQUESTOR) +if(BOOT_ENABLED) + add_subdirectory(${MCUBOOT_PATH}/boot/bootutil/ ./mbed_mcu_boot_util_build) + add_subdirectory(${MCUBOOT_PATH}/boot/mbed/ ./mbed_mcu_boot_build) + + target_include_directories(bootutil PUBLIC + ${CHIP_ROOT}/config/mbed/mbedtls + ) + + target_link_libraries(${APP_TARGET} mbed-mcuboot bootutil) + + file(READ ${APP_PATH}/mbed_app.json mbedAppJson) + string(JSON PRIMARY_SLOT_ADDRESS GET "${mbedAppJson}" target_overrides ${MBED_TARGET} mcuboot.primary-slot-address) + string(JSON HEADER_SIZE GET "${mbedAppJson}" target_overrides ${MBED_TARGET} mcuboot.header-size) + string(JSON SLOT_SIZE GET "${mbedAppJson}" target_overrides ${MBED_TARGET} mcuboot.slot-size) + math(EXPR APP_START "${PRIMARY_SLOT_ADDRESS} + ${HEADER_SIZE}" OUTPUT_FORMAT HEXADECIMAL) + math(EXPR APP_SIZE "${SLOT_SIZE} - 2 * ${HEADER_SIZE}" OUTPUT_FORMAT HEXADECIMAL) + target_compile_definitions(mbed-core + INTERFACE + "-DMBED_APP_START=${APP_START}" + "-DMBED_APP_SIZE=${APP_SIZE}" + ) + + list(APPEND CHIP_DEFINES + BOOT_ENABLED=1 + ) +endif() + target_include_directories(${APP_TARGET} PRIVATE ${CHIP_INCLUDES} diff --git a/examples/ota-requestor-app/mbed/.gitignore b/examples/ota-requestor-app/mbed/.gitignore index 414487d53eb835..ef564644de9016 100644 --- a/examples/ota-requestor-app/mbed/.gitignore +++ b/examples/ota-requestor-app/mbed/.gitignore @@ -1 +1,2 @@ build-*/ +mcuboot diff --git a/examples/ota-requestor-app/mbed/CMakeLists.txt b/examples/ota-requestor-app/mbed/CMakeLists.txt index 50988c999f8b09..34854c6c3fa5d0 100644 --- a/examples/ota-requestor-app/mbed/CMakeLists.txt +++ b/examples/ota-requestor-app/mbed/CMakeLists.txt @@ -16,9 +16,21 @@ configure_file( set(MBED_PATH ${MBED_OS_PATH} CACHE INTERNAL "") set(MBED_CONFIG_PATH ${CMAKE_CURRENT_BINARY_DIR} CACHE INTERNAL "") +set(MCUBOOT_PATH ${MBED_MCU_BOOT_PATH} CACHE INTERNAL "") +set(APP_PATH ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "") +set(APP_TYPE ${MBED_APP_TYPE} CACHE INTERNAL "") +set(BOOT_ENABLED FALSE) set(APP_TARGET chip-mbed-ota-requestor-app-example) +if(APP_TYPE STREQUAL "boot" OR APP_TYPE STREQUAL "upgrade") + set(BOOT_ENABLED TRUE) +endif() + include(${MBED_PATH}/tools/cmake/app.cmake) +if(MBED_TARGET STREQUAL "CY8CPROTO_062_4343W" AND BOOT_ENABLED) + list(REMOVE_ITEM MBED_TARGET_LABELS CM0P_SLEEP) + list(REMOVE_ITEM MBED_TARGET_DEFINITIONS COMPONENT_CM0P_SLEEP=1) +endif() include(${CHIP_ROOT}/src/app/chip_data_model.cmake) project(${APP_TARGET}) diff --git a/examples/ota-requestor-app/mbed/main/AppTask.cpp b/examples/ota-requestor-app/mbed/main/AppTask.cpp index 21edab8816d58d..af99d2f42992b0 100644 --- a/examples/ota-requestor-app/mbed/main/AppTask.cpp +++ b/examples/ota-requestor-app/mbed/main/AppTask.cpp @@ -37,6 +37,20 @@ #include #endif // CHIP_OTA_REQUESTOR +#ifdef BOOT_ENABLED +#include "blockdevice/SlicingBlockDevice.h" +#include +#endif + +#ifdef BOOT_ENABLED +mbed::BlockDevice * get_secondary_bd() +{ + mbed::BlockDevice * default_bd = mbed::BlockDevice::get_default_instance(); + static mbed::SlicingBlockDevice sliced_bd(default_bd, 0x0, MCUBOOT_SLOT_SIZE); + return &sliced_bd; +} +#endif + static bool sIsWiFiStationProvisioned = false; static bool sIsWiFiStationEnabled = false; static bool sIsWiFiStationConnected = false; diff --git a/examples/ota-requestor-app/mbed/main/include/CHIPProjectConfig.h b/examples/ota-requestor-app/mbed/main/include/CHIPProjectConfig.h index 222b6ed33c0b37..c2f465ba178172 100644 --- a/examples/ota-requestor-app/mbed/main/include/CHIPProjectConfig.h +++ b/examples/ota-requestor-app/mbed/main/include/CHIPProjectConfig.h @@ -31,3 +31,11 @@ // Use a default pairing code if one hasn't been provisioned in flash. #define CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE 20202021 #define CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR 0xF00 + +#ifndef CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION +#define CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING MBED_CONF_APP_VERSION_NUMBER_STR +#endif + +#ifndef CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION +#define CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION MBED_CONF_APP_VERSION_NUMBER +#endif \ No newline at end of file diff --git a/examples/ota-requestor-app/mbed/main/main.cpp b/examples/ota-requestor-app/mbed/main/main.cpp index 52499c0a813ebd..2e8ba26a9eb826 100644 --- a/examples/ota-requestor-app/mbed/main/main.cpp +++ b/examples/ota-requestor-app/mbed/main/main.cpp @@ -23,6 +23,10 @@ #include #include +#ifdef BOOT_ENABLED +#include +#endif + using namespace ::chip; using namespace ::chip::DeviceLayer; using namespace ::chip::Platform; @@ -37,6 +41,20 @@ int main() ChipLogProgress(SoftwareUpdate, "Mbed OTA Requestor example application start"); +#ifdef BOOT_ENABLED + ret = boot_set_confirmed(); + if (ret == 0) + { + ChipLogProgress(NotSpecified, "Boot confirmed"); + } + else + { + ChipLogError(NotSpecified, "Failed to confirm boot: %d", ret); + } + ChipLogProgress(NotSpecified, "Current software version: [%ld] %s", uint32_t(CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION), + CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING); +#endif + ret = mbedtls_platform_setup(NULL); if (ret) { diff --git a/examples/ota-requestor-app/mbed/mbed_app.json b/examples/ota-requestor-app/mbed/mbed_app.json index 48d1146f5785ec..482463cff188f5 100644 --- a/examples/ota-requestor-app/mbed/mbed_app.json +++ b/examples/ota-requestor-app/mbed/mbed_app.json @@ -10,7 +10,10 @@ "nsapi.default-wifi-password": "\"YOUR_PASSWORD\"", "mbed-trace.max-level": "TRACE_LEVEL_DEBUG", "mbed-trace.enable": true, - "target.printf_lib": "std" + "target.printf_lib": "std", + "mcuboot.bootloader-build": false, + "mcuboot.log-level": "MCUBOOT_LOG_LEVEL_DEBUG", + "target.c_lib": "small" }, "CY8CPROTO_062_4343W": { "target.network-default-interface-type": "WIFI", @@ -19,7 +22,13 @@ "NL_ASSERT_LOG=NL_ASSERT_LOG_DEFAULT", "NL_ASSERT_EXPECT_FLAGS=NL_ASSERT_FLAG_LOG", "WHD_PRINT_DISABLE" - ] + ], + "mcuboot.primary-slot-address": "0x10022000", + "mcuboot.slot-size": "0x140000", + "mcuboot.scratch-address": "0x101AF000", + "mcuboot.scratch-size": "0x1000", + "mcuboot.max-img-sectors": "0xA00", + "mcuboot.header-size": "0x400" } }, "config": { @@ -38,6 +47,12 @@ "use-gatt-indication-ack-hack": { "help": "Fake a TX transfer confirmation. Send a 'kCHIPoBLEIndicateConfirm' event as soon as data is sent, without waiting for the actual ACK from the GATT client. This hack has to stay until we provide a fix in the Mbed OS repo.", "value": 1 + }, + "version-number": { + "value": "0" + }, + "version-number-str": { + "value": "\"0.1.0\"" } } } diff --git a/examples/platform/mbed/bootloader/mbed_app.json b/examples/platform/mbed/bootloader/mbed_app.json index b54291398399b0..b7e94160056cf6 100644 --- a/examples/platform/mbed/bootloader/mbed_app.json +++ b/examples/platform/mbed/bootloader/mbed_app.json @@ -19,10 +19,10 @@ "CY8CPROTO_062_4343W": { "target.components_remove": [ "WHD" ], "mcuboot.primary-slot-address": "0x10022000", - "mcuboot.slot-size": "0xC0000", + "mcuboot.slot-size": "0x140000", "mcuboot.scratch-address": "0x101A2000", "mcuboot.scratch-size": "0x1000", - "mcuboot.max-img-sectors": "0x600" + "mcuboot.max-img-sectors": "0xA00" } } } diff --git a/scripts/examples/mbed_example.sh b/scripts/examples/mbed_example.sh index bcf4159b7d9a06..a3dbf1e9abb064 100755 --- a/scripts/examples/mbed_example.sh +++ b/scripts/examples/mbed_example.sh @@ -22,46 +22,49 @@ 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 bootloader) +SUPPORTED_APP=(lock-app lighting-app pigweed-app all-clusters-app shell ota-requestor-app) SUPPORTED_PROFILES=(release develop debug) SUPPORTED_COMMAND=(build flash build-flash) +SUPPORTED_TYPE=(simple boot upgrade) COMMAND=build APP=lock-app TARGET_BOARD=CY8CPROTO_062_4343W TOOLCHAIN=GCC_ARM PROFILE=release -BOOTLOADER=false +TYPE=simple + +TARGET_MEMORY_ALIGN[CY8CPROTO_062_4343W]=8 for i in "$@"; do case $i in - -a=* | --app=*) - APP="${i#*=}" - shift - ;; - -b=* | --board=*) - TARGET_BOARD="${i#*=}" - shift - ;; - -t=* | --toolchain=*) - TOOLCHAIN="${i#*=}" - shift - ;; - -p=* | --profile=*) - PROFILE="${i#*=}" - shift - ;; - -c=* | --command=*) - COMMAND="${i#*=}" - shift - ;; - -B=* | --bootloader=*) - BOOTLOADER="${i#*=}" - shift - ;; - *) - # unknown option - ;; + -a=* | --app=*) + APP="${i#*=}" + shift + ;; + -b=* | --board=*) + TARGET_BOARD="${i#*=}" + shift + ;; + -t=* | --toolchain=*) + TOOLCHAIN="${i#*=}" + shift + ;; + -p=* | --profile=*) + PROFILE="${i#*=}" + shift + ;; + -c=* | --command=*) + COMMAND="${i#*=}" + shift + ;; + -T=* | --type=*) + TYPE="${i#*=}" + shift + ;; + *) + # unknown option + ;; esac done @@ -90,6 +93,11 @@ if [[ ! " ${SUPPORTED_PROFILES[@]} " =~ " ${PROFILE} " ]]; then exit 1 fi +if [[ ! " ${SUPPORTED_TYPE[@]} " =~ " ${TYPE} " ]]; then + echo "ERROR: Type $TYPE not supported" + exit 1 +fi + set -e # Exit immediately if a command exits with a non-zero status. # Activate Matter environment @@ -105,7 +113,7 @@ BOOTLOADER_ROOT_DIRECTORY="$CHIP_ROOT"/examples/platform/mbed/bootloader BOOTLOADER_BUILD_DIRECTORY="$BOOTLOADER_ROOT_DIRECTORY"/build-"$TARGET_BOARD"/"$PROFILE"/ if [[ "$COMMAND" == *"build"* ]]; then - echo "Build $APP app for $TARGET_BOARD target with $TOOLCHAIN toolchain and $PROFILE profile" + echo "Build $TYPE $APP app for $TARGET_BOARD target with $TOOLCHAIN toolchain and $PROFILE profile" # Set Mbed OS path MBED_OS_PATH="$CHIP_ROOT"/third_party/mbed-os/repo @@ -113,9 +121,10 @@ if [[ "$COMMAND" == *"build"* ]]; then # Set Mbed OS posix socket submodule path MBED_OS_POSIX_SOCKET_PATH="$CHIP_ROOT"/third_party/mbed-os-posix-socket/repo - if [[ "$APP" == "bootloader" || $BOOTLOADER == true ]]; then - # Set Mbed MCU boot path - MBED_MCU_BOOT_PATH="$CHIP_ROOT"/third_party/mbed-mcu-boot/repo + # Set Mbed MCU boot path + MBED_MCU_BOOT_PATH="$CHIP_ROOT"/third_party/mbed-mcu-boot/repo + + if [[ "$TYPE" == "boot" || "$TYPE" == "upgrade" ]]; then cd "$BOOTLOADER_ROOT_DIRECTORY" @@ -143,17 +152,13 @@ if [[ "$COMMAND" == *"build"* ]]; then cmake -S . -B "$BOOTLOADER_BUILD_DIRECTORY" -GNinja -DCMAKE_BUILD_TYPE="$PROFILE" -DMBED_OS_PATH="$MBED_OS_PATH" -DMBED_MCU_BOOT_PATH="$MBED_MCU_BOOT_PATH" cmake --build "$BOOTLOADER_BUILD_DIRECTORY" - if [[ "$APP" == "bootloader" ]]; then - exit - fi - cd "$CHIP_ROOT"/examples fi # Set Mbed OS posix socket submodule path MBED_OS_POSIX_SOCKET_PATH="$CHIP_ROOT"/third_party/mbed-os-posix-socket/repo - if [[ $BOOTLOADER == true ]]; then + if [[ "$TYPE" == "boot" || "$TYPE" == "upgrade" ]]; then ln -sfTr "$MBED_MCU_BOOT_PATH"/boot/mbed "$APP"/mbed/mcuboot fi @@ -164,13 +169,16 @@ if [[ "$COMMAND" == *"build"* ]]; then rm -rf "$BUILD_DIRECTORY/chip-"* # Build application - cmake -S "$APP/mbed" -B "$BUILD_DIRECTORY" -GNinja -DCMAKE_BUILD_TYPE="$PROFILE" -DMBED_OS_PATH="$MBED_OS_PATH" -DMBED_OS_POSIX_SOCKET_PATH="$MBED_OS_POSIX_SOCKET_PATH" -DMBED_MCU_BOOT_PATH="$MBED_MCU_BOOT_PATH" + cmake -S "$APP/mbed" -B "$BUILD_DIRECTORY" -GNinja -DCMAKE_BUILD_TYPE="$PROFILE" -DMBED_OS_PATH="$MBED_OS_PATH" -DMBED_OS_POSIX_SOCKET_PATH="$MBED_OS_POSIX_SOCKET_PATH" -DMBED_MCU_BOOT_PATH="$MBED_MCU_BOOT_PATH" -DMBED_APP_TYPE="$TYPE" cmake --build "$BUILD_DIRECTORY" - if [[ $BOOTLOADER == true ]]; then + if [[ "$TYPE" == "boot" || "$TYPE" == "upgrade" ]]; then + APP_VERSION=$(jq '.config."version-number-str".value' $APP/mbed/mbed_app.json | tr -d '\\"') + HEADER_SIZE=$(jq '.target_overrides.'\"${TARGET_BOARD}\"'."mcuboot.header-size"' $APP/mbed/mbed_app.json | tr -d \") + SLOT_SIZE=$(jq '.target_overrides.'\"${TARGET_BOARD}\"'."mcuboot.slot-size"' $APP/mbed/mbed_app.json | tr -d \") # Signed the primary application "$MBED_MCU_BOOT_PATH"/scripts/imgtool.py sign -k "$BOOTLOADER_ROOT_DIRECTORY"/signing-keys.pem \ - --align 8 -v "0.1.0" --header-size 4096 --pad-header -S 0xC0000 \ + --align ${TARGET_MEMORY_ALIGN[$TARGET_BOARD]} -v $APP_VERSION --header-size $(($HEADER_SIZE)) --pad-header -S $SLOT_SIZE \ "$BUILD_DIRECTORY"/chip-mbed-"$APP"-example.hex "$BUILD_DIRECTORY"/chip-mbed-"$APP"-example-signed.hex # Create the factory firmware (bootlaoder + signed primary application)