From a445ee538cf8c3832c280f5d4a98a61eb81d9a72 Mon Sep 17 00:00:00 2001 From: Jamie McCrae Date: Tue, 7 Mar 2023 14:19:46 +0000 Subject: [PATCH] tests: Add mcuboot_recovery_retention test Adds a test that exercises the boot mode (retention subsystem) feature to check that an application can successfully boot into mcuboot's serial recovery mode. Signed-off-by: Jamie McCrae --- .../mcuboot_recovery_retention/CMakeLists.txt | 19 ++++++ .../boards/nrf52840dk_nrf52840.overlay | 42 +++++++++++++ .../boards/nrf52840dk_nrf52840_mem.conf | 7 +++ .../boards/nrf52840dk_nrf52840_mem.overlay | 61 +++++++++++++++++++ .../boot/mcuboot_recovery_retention/prj.conf | 10 +++ .../mcuboot_recovery_retention/src/main.c | 25 ++++++++ .../mcuboot_recovery_retention/sysbuild.cmake | 1 + .../mcuboot_recovery_retention/sysbuild.conf | 1 + .../mcuboot/boards/nrf52840dk_nrf52840.conf | 5 ++ .../boards/nrf52840dk_nrf52840.overlay | 14 +++++ .../sysbuild/mcuboot/prj.conf | 11 ++++ .../test_module/CMakeLists.txt | 4 ++ .../test_module/src/hook.c | 14 +++++ .../test_module/zephyr/module.yml | 2 + .../mcuboot_recovery_retention/testcase.yaml | 32 ++++++++++ 15 files changed, 248 insertions(+) create mode 100644 tests/boot/mcuboot_recovery_retention/CMakeLists.txt create mode 100644 tests/boot/mcuboot_recovery_retention/boards/nrf52840dk_nrf52840.overlay create mode 100644 tests/boot/mcuboot_recovery_retention/boards/nrf52840dk_nrf52840_mem.conf create mode 100644 tests/boot/mcuboot_recovery_retention/boards/nrf52840dk_nrf52840_mem.overlay create mode 100644 tests/boot/mcuboot_recovery_retention/prj.conf create mode 100644 tests/boot/mcuboot_recovery_retention/src/main.c create mode 100644 tests/boot/mcuboot_recovery_retention/sysbuild.cmake create mode 100644 tests/boot/mcuboot_recovery_retention/sysbuild.conf create mode 100644 tests/boot/mcuboot_recovery_retention/sysbuild/mcuboot/boards/nrf52840dk_nrf52840.conf create mode 100644 tests/boot/mcuboot_recovery_retention/sysbuild/mcuboot/boards/nrf52840dk_nrf52840.overlay create mode 100644 tests/boot/mcuboot_recovery_retention/sysbuild/mcuboot/prj.conf create mode 100644 tests/boot/mcuboot_recovery_retention/test_module/CMakeLists.txt create mode 100644 tests/boot/mcuboot_recovery_retention/test_module/src/hook.c create mode 100644 tests/boot/mcuboot_recovery_retention/test_module/zephyr/module.yml create mode 100644 tests/boot/mcuboot_recovery_retention/testcase.yaml diff --git a/tests/boot/mcuboot_recovery_retention/CMakeLists.txt b/tests/boot/mcuboot_recovery_retention/CMakeLists.txt new file mode 100644 index 00000000000..df207f0da12 --- /dev/null +++ b/tests/boot/mcuboot_recovery_retention/CMakeLists.txt @@ -0,0 +1,19 @@ +# +# Copyright (c) 2023 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: Apache-2.0 +# + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(mcuboot_recovery_retention) + +if(NOT (DEFINED SYSBUILD)) + message(FATAL_ERROR "This test must be built with sysbuild.") +endif() + +FILE(GLOB app_sources + src/*.c +) + +target_sources(app PRIVATE ${app_sources}) diff --git a/tests/boot/mcuboot_recovery_retention/boards/nrf52840dk_nrf52840.overlay b/tests/boot/mcuboot_recovery_retention/boards/nrf52840dk_nrf52840.overlay new file mode 100644 index 00000000000..2257c1a8888 --- /dev/null +++ b/tests/boot/mcuboot_recovery_retention/boards/nrf52840dk_nrf52840.overlay @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: Apache-2.0 */ + +/ { + chosen { + zephyr,boot-mode = &boot_mode0; + }; +}; + +&gpregret1 { + status = "okay"; + + boot_mode0: boot_mode@0 { + compatible = "zephyr,retention"; + status = "okay"; + reg = <0x0 0x1>; + }; +}; + +/delete-node/ &boot_partition; +/delete-node/ &slot0_partition; +/delete-node/ &slot1_partition; + +&flash0 { + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + boot_partition: partition@0 { + label = "mcuboot"; + reg = <0x00000000 0x00020000>; + }; + slot0_partition: partition@20000 { + label = "image-0"; + reg = <0x00020000 0x00020000>; + }; + slot1_partition: partition@40000 { + label = "image-1"; + reg = <0x00040000 0x00020000>; + }; + }; +}; diff --git a/tests/boot/mcuboot_recovery_retention/boards/nrf52840dk_nrf52840_mem.conf b/tests/boot/mcuboot_recovery_retention/boards/nrf52840dk_nrf52840_mem.conf new file mode 100644 index 00000000000..8f39b13f7ec --- /dev/null +++ b/tests/boot/mcuboot_recovery_retention/boards/nrf52840dk_nrf52840_mem.conf @@ -0,0 +1,7 @@ +# +# Copyright (c) 2023 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: Apache-2.0 +# +CONFIG_RETAINED_MEM_NRF_GPREGRET=n +CONFIG_RETAINED_MEM_ZEPHYR_RAM=y diff --git a/tests/boot/mcuboot_recovery_retention/boards/nrf52840dk_nrf52840_mem.overlay b/tests/boot/mcuboot_recovery_retention/boards/nrf52840dk_nrf52840_mem.overlay new file mode 100644 index 00000000000..907b1c0cb50 --- /dev/null +++ b/tests/boot/mcuboot_recovery_retention/boards/nrf52840dk_nrf52840_mem.overlay @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: Apache-2.0 */ + +/ { + sram@2003FC00 { + compatible = "zephyr,memory-region", "mmio-sram"; + reg = <0x2003FC00 DT_SIZE_K(1)>; + zephyr,memory-region = "RetainedMem"; + status = "okay"; + + retainedmem { + compatible = "zephyr,retained-ram"; + status = "okay"; + #address-cells = <1>; + #size-cells = <1>; + + boot_mode0: boot_mode@0 { + compatible = "zephyr,retention"; + status = "okay"; + reg = <0x0 0x100>; + prefix = [08 04]; + checksum = <1>; + }; + }; + }; + + + chosen { + zephyr,boot-mode = &boot_mode0; + }; +}; + +/delete-node/ &gpregret1; +/delete-node/ &gpregret2; +/delete-node/ &boot_partition; +/delete-node/ &slot0_partition; +/delete-node/ &slot1_partition; + +&sram0 { + reg = <0x20000000 DT_SIZE_K(255)>; +}; + +&flash0 { + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + boot_partition: partition@0 { + label = "mcuboot"; + reg = <0x00000000 0x00020000>; + }; + slot0_partition: partition@20000 { + label = "image-0"; + reg = <0x00020000 0x00020000>; + }; + slot1_partition: partition@40000 { + label = "image-1"; + reg = <0x00040000 0x00020000>; + }; + }; +}; diff --git a/tests/boot/mcuboot_recovery_retention/prj.conf b/tests/boot/mcuboot_recovery_retention/prj.conf new file mode 100644 index 00000000000..c5c2cbc676c --- /dev/null +++ b/tests/boot/mcuboot_recovery_retention/prj.conf @@ -0,0 +1,10 @@ +# +# Copyright (c) 2023 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: Apache-2.0 +# +CONFIG_RETAINED_MEM=y +CONFIG_RETENTION=y +CONFIG_RETENTION_BOOT_MODE=y +CONFIG_REBOOT=y +CONFIG_LOG=y diff --git a/tests/boot/mcuboot_recovery_retention/src/main.c b/tests/boot/mcuboot_recovery_retention/src/main.c new file mode 100644 index 00000000000..fd1d785b72f --- /dev/null +++ b/tests/boot/mcuboot_recovery_retention/src/main.c @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include + +void main(void) +{ + printf("Waiting...\n"); + k_sleep(K_SECONDS(1)); + + int rc = bootmode_set(BOOT_MODE_TYPE_BOOTLOADER); + + if (rc == 0) { + sys_reboot(SYS_REBOOT_WARM); + } else { + printf("Error, failed to set boot mode: %d\n", rc); + } +} diff --git a/tests/boot/mcuboot_recovery_retention/sysbuild.cmake b/tests/boot/mcuboot_recovery_retention/sysbuild.cmake new file mode 100644 index 00000000000..2825102e11e --- /dev/null +++ b/tests/boot/mcuboot_recovery_retention/sysbuild.cmake @@ -0,0 +1 @@ +set(mcuboot_ZEPHYR_EXTRA_MODULES "${CMAKE_CURRENT_LIST_DIR}/test_module" CACHE INTERNAL "test_module directory") diff --git a/tests/boot/mcuboot_recovery_retention/sysbuild.conf b/tests/boot/mcuboot_recovery_retention/sysbuild.conf new file mode 100644 index 00000000000..47f00ff3cff --- /dev/null +++ b/tests/boot/mcuboot_recovery_retention/sysbuild.conf @@ -0,0 +1 @@ +SB_CONFIG_BOOTLOADER_MCUBOOT=y diff --git a/tests/boot/mcuboot_recovery_retention/sysbuild/mcuboot/boards/nrf52840dk_nrf52840.conf b/tests/boot/mcuboot_recovery_retention/sysbuild/mcuboot/boards/nrf52840dk_nrf52840.conf new file mode 100644 index 00000000000..0553e1bd746 --- /dev/null +++ b/tests/boot/mcuboot_recovery_retention/sysbuild/mcuboot/boards/nrf52840dk_nrf52840.conf @@ -0,0 +1,5 @@ +CONFIG_USE_SEGGER_RTT=n +CONFIG_NORDIC_QSPI_NOR=n +CONFIG_BOOT_VALIDATE_SLOT0=n +CONFIG_MCUBOOT_CLEANUP_ARM_CORE=n +CONFIG_BOOT_WATCHDOG_FEED=n diff --git a/tests/boot/mcuboot_recovery_retention/sysbuild/mcuboot/boards/nrf52840dk_nrf52840.overlay b/tests/boot/mcuboot_recovery_retention/sysbuild/mcuboot/boards/nrf52840dk_nrf52840.overlay new file mode 100644 index 00000000000..186bd511340 --- /dev/null +++ b/tests/boot/mcuboot_recovery_retention/sysbuild/mcuboot/boards/nrf52840dk_nrf52840.overlay @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#include "../../../boards/nrf52840dk_nrf52840.overlay" + +/ { + chosen { + zephyr,code-partition = &boot_partition; + }; +}; + +&zephyr_udc0 { + cdc_acm_uart0 { + compatible = "zephyr,cdc-acm-uart"; + }; +}; diff --git a/tests/boot/mcuboot_recovery_retention/sysbuild/mcuboot/prj.conf b/tests/boot/mcuboot_recovery_retention/sysbuild/mcuboot/prj.conf new file mode 100644 index 00000000000..98f4a2a30cd --- /dev/null +++ b/tests/boot/mcuboot_recovery_retention/sysbuild/mcuboot/prj.conf @@ -0,0 +1,11 @@ +CONFIG_MCUBOOT_SERIAL=y +CONFIG_BOOT_SERIAL_CDC_ACM=y +CONFIG_BOOT_SERIAL_ENTRANCE_GPIO=n +CONFIG_BOOT_SERIAL_BOOT_MODE=y +CONFIG_RETAINED_MEM=y +CONFIG_RETENTION=y +CONFIG_RETENTION_BOOT_MODE=y +CONFIG_FLASH=y +CONFIG_FLASH_MAP=y +CONFIG_LOG=y +CONFIG_MCUBOOT_ACTION_HOOKS=y diff --git a/tests/boot/mcuboot_recovery_retention/test_module/CMakeLists.txt b/tests/boot/mcuboot_recovery_retention/test_module/CMakeLists.txt new file mode 100644 index 00000000000..06daf24155b --- /dev/null +++ b/tests/boot/mcuboot_recovery_retention/test_module/CMakeLists.txt @@ -0,0 +1,4 @@ +if(CONFIG_MCUBOOT) + # Append source file which outputs the current mode of mcuboot + target_sources(app PRIVATE src/hook.c) +endif() diff --git a/tests/boot/mcuboot_recovery_retention/test_module/src/hook.c b/tests/boot/mcuboot_recovery_retention/test_module/src/hook.c new file mode 100644 index 00000000000..549346ea7d2 --- /dev/null +++ b/tests/boot/mcuboot_recovery_retention/test_module/src/hook.c @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2023 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +void mcuboot_status_change(mcuboot_status_type_t status) +{ + printf("mcuboot_status: %d\n", status); +} diff --git a/tests/boot/mcuboot_recovery_retention/test_module/zephyr/module.yml b/tests/boot/mcuboot_recovery_retention/test_module/zephyr/module.yml new file mode 100644 index 00000000000..eb317c3ce8a --- /dev/null +++ b/tests/boot/mcuboot_recovery_retention/test_module/zephyr/module.yml @@ -0,0 +1,2 @@ +build: + cmake: . diff --git a/tests/boot/mcuboot_recovery_retention/testcase.yaml b/tests/boot/mcuboot_recovery_retention/testcase.yaml new file mode 100644 index 00000000000..69f571fdf71 --- /dev/null +++ b/tests/boot/mcuboot_recovery_retention/testcase.yaml @@ -0,0 +1,32 @@ +# +# Copyright (c) 2023 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: Apache-2.0 +# +common: + sysbuild: true + timeout: 10 + harness: console + harness_config: + type: multi_line + regex: + - "mcuboot_status: 0" + - "mcuboot_status: 1" + - "mcuboot_status: 2" + - "Waiting..." + - "mcuboot_status: 0" + - "mcuboot_status: 1" + - "mcuboot_status: 8" + - "Starting bootloader" + - "Secondary image: magic" + - "Boot source: none" +tests: + mcuboot.recovery.retention: + platform_allow: nrf52840dk_nrf52840 + tags: mcuboot sysbuild recovery + mcuboot.recovery.retention.mem: + platform_allow: nrf52840dk_nrf52840 + extra_args: | + 'OVERLAY_CONFIG="nrf52840dk_nrf52840_mem.conf"' + 'DTC_OVERLAY_FILE="boards/nrf52840dk_nrf52840_mem.overlay"' + tags: mcuboot sysbuild recovery