Skip to content

Commit

Permalink
Implement Mbed bootloader in ota-requestor app
Browse files Browse the repository at this point in the history
  • Loading branch information
ATmobica committed Dec 17, 2021
1 parent e62cbc8 commit 21a6695
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 45 deletions.
27 changes: 27 additions & 0 deletions config/mbed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions examples/ota-requestor-app/mbed/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build-*/
mcuboot
12 changes: 12 additions & 0 deletions examples/ota-requestor-app/mbed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
14 changes: 14 additions & 0 deletions examples/ota-requestor-app/mbed/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@
#include <OTARequestor.h>
#endif // CHIP_OTA_REQUESTOR

#ifdef BOOT_ENABLED
#include "blockdevice/SlicingBlockDevice.h"
#include <bootutil/bootutil.h>
#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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions examples/ota-requestor-app/mbed/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#include <platform/CHIPDeviceLayer.h>
#include <platform/mbed/Logging.h>

#ifdef BOOT_ENABLED
#include <bootutil/bootutil.h>
#endif

using namespace ::chip;
using namespace ::chip::DeviceLayer;
using namespace ::chip::Platform;
Expand All @@ -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)
{
Expand Down
19 changes: 17 additions & 2 deletions examples/ota-requestor-app/mbed/mbed_app.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand All @@ -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\""
}
}
}
4 changes: 2 additions & 2 deletions examples/platform/mbed/bootloader/mbed_app.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
90 changes: 49 additions & 41 deletions scripts/examples/mbed_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -105,17 +113,18 @@ 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

# 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"

Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down

0 comments on commit 21a6695

Please sign in to comment.