Skip to content

Commit 0dacc25

Browse files
danieldegrassenashif
authored andcommitted
tests: boot: add mcuboot test
Add test to verify mcuboot support. This test is only enabled for specific platforms, since it it not possible to filter for mcuboot support. Sysbuild support is required to flash multiple samples using twister. Signed-off-by: Daniel DeGrasse <[email protected]>
1 parent 50c7341 commit 0dacc25

File tree

11 files changed

+123
-0
lines changed

11 files changed

+123
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(test_mcuboot)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})

tests/boot/test_mcuboot/README.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
MCUBoot Swap Testing
2+
#####################
3+
4+
Tests MCUBoot's image swap support. This application is built in three parts
5+
using sysbuild. The first application is the MCUBoot bootloader. The second
6+
application is the main sysbuild target, and will request an image swap
7+
from MCUBoot when booted. The third application is build with a load address
8+
adjustment using CONFIG_BUILD_OUTPUT_ADJUST_LMA, and will be the application
9+
that MCUBoot swaps to when the image swap is requested.
10+
11+
This sequence of applications allows the test to verify support for the MCUBoot
12+
upgrade process on any platform supporting it.

tests/boot/test_mcuboot/prj.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Enable zephyr dfu boot utility library
2+
CONFIG_IMG_MANAGER=y
3+
# Enable flash drivers for MCUBoot
4+
CONFIG_FLASH=y
5+
# Enable reboot API to reset board after selecting new image
6+
CONFIG_REBOOT=y

tests/boot/test_mcuboot/src/main.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2022 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/sys/reboot.h>
9+
#include <zephyr/dfu/mcuboot.h>
10+
11+
/* Main entry point */
12+
void main(void)
13+
{
14+
printk("Launching primary slot application on %s\n", CONFIG_BOARD);
15+
/* Perform a permanent swap of MCUBoot application */
16+
boot_request_upgrade(1);
17+
printk("Secondary application ready for swap, rebooting\n");
18+
sys_reboot(SYS_REBOOT_COLD);
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(test_mcuboot)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright 2022 NXP
2+
# SPDX-License-Identifier: Apache-2.0
3+
source "Kconfig.zephyr"
4+
# Workaround for not being able to have commas in macro arguments
5+
DT_CHOSEN_Z_CODE_PARTITION := zephyr,code-partition
6+
7+
config BUILD_OUTPUT_ADJUST_LMA
8+
default "$(dt_node_reg_addr_hex,$(dt_nodelabel_path,slot1_partition))-\
9+
$(dt_chosen_reg_addr_hex,$(DT_CHOSEN_Z_CODE_PARTITION))"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Since LMA adjustment is used to move the image location to the secondary
2+
# slot, we need to enable hex build output. Otherwise, some debuggers will not
3+
# flash the binary to the second image slot.
4+
CONFIG_BUILD_OUTPUT_HEX=y
5+
CONFIG_BOOTLOADER_MCUBOOT=y
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright 2022 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
9+
/* Main entry point */
10+
void main(void)
11+
{
12+
printk("Swapped application booted on %s\n", CONFIG_BOARD);
13+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2022 NXP
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Add the mcuboot key file to the secondary swapped app
5+
# This must be done here to ensure that the same key file is used for signing
6+
# both the primary and secondary apps
7+
set(swapped_app_CONFIG_MCUBOOT_SIGNATURE_KEY_FILE
8+
\"${SB_CONFIG_BOOT_SIGNATURE_KEY_FILE}\" CACHE STRING
9+
"Signature key file for signing" FORCE)
10+
11+
# Add the swapped app to the build
12+
ExternalZephyrProject_Add(
13+
APPLICATION swapped_app
14+
SOURCE_DIR ${APP_DIR}/swapped_app
15+
)
16+
17+
# Add the swapped app to the list of images to flash
18+
# Ensure the order of images is as follows:
19+
# - mcuboot
20+
# - swapped app
21+
# - primary app (test_mcuboot)
22+
# This order means that if the debugger resets the MCU in between flash
23+
# iterations, the MCUBoot swap won't be triggered until the secondary app
24+
# is actually present in flash.
25+
set(IMAGES "mcuboot" "swapped_app" "test_mcuboot")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SB_CONFIG_BOOTLOADER_MCUBOOT=y

0 commit comments

Comments
 (0)