Skip to content

Commit ebca6cd

Browse files
Refactor mcuboot build fragments for reuse
Extract reusable portions of boot/zephyr for use in other mcuboot builds: - mcuboot signature key file generation build logic - mcuboot zephyr runner mass erase hooks Verification: 1. (Pass) west build -p always -b nrf52840dk_nrf52840 mcuboot/boot/zephyr/ 2. (Pass) ./zephyr/scripts/twister --testsuite-root mcuboot/boot/zephyr/ 3. (Pass) ./zephyr/scripts/twister --testsuite-root zephyr/samples/tfm_integration/psa_crypto/ 4. (Pass) ./zephyr/scripts/twister --testsuite-root zephyr/tests/subsys/dfu/ 5. (Pass) ./zephyr/scripts/twister --testsuite-root zephyr/samples/subsys/mgmt/mcumgr/smp_svr/ Signed-off-by: Gregory Shue <[email protected]>
1 parent 81be8fd commit ebca6cd

File tree

3 files changed

+76
-57
lines changed

3 files changed

+76
-57
lines changed

boot/zephyr/CMakeLists.txt

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,9 @@
77

88
cmake_minimum_required(VERSION 3.13.1)
99

10-
# Enable Zephyr runner options which request mass erase if so
11-
# configured.
12-
#
13-
# Note that this also disables the default "leave" option when
14-
# targeting STM32 DfuSe devices with dfu-util, making the chip stay in
15-
# the bootloader after flashing.
16-
#
17-
# That's the right thing, because mcuboot has nothing to do since the
18-
# chip was just erased. The next thing the user is going to want to do
19-
# is flash the application. (Developers can reset DfuSE devices
20-
# manually to test mcuboot behavior on an otherwise erased flash
21-
# device.)
22-
macro(app_set_runner_args)
23-
if(CONFIG_ZEPHYR_TRY_MASS_ERASE)
24-
board_runner_args(dfu-util "--dfuse-modifiers=force:mass-erase")
25-
board_runner_args(pyocd "--flash-opt=-e=chip")
26-
board_runner_args(nrfjprog "--erase")
27-
endif()
28-
endmacro()
10+
# NOTE: ${ZEPHYR_MCUBOOT_MODULE_DIR} requires delayed evaluation,
11+
# as it has not yet been defined. Relative paths must be used at this point.
12+
include(${CMAKE_CURRENT_LIST_DIR}/../../zephyr/cmake/mcuboot_zephyr_runner_mass_erase.cmake)
2913

3014
# find_package(Zephyr) in order to load application boilerplate:
3115
# http://docs.zephyrproject.org/application/application.html
@@ -36,41 +20,4 @@ target_sources(app PRIVATE
3620
keys.c
3721
)
3822

39-
if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
40-
# CONF_FILE points to the KConfig configuration files of the bootloader.
41-
foreach (filepath ${CONF_FILE})
42-
file(READ ${filepath} temp_text)
43-
string(FIND "${temp_text}" ${CONFIG_BOOT_SIGNATURE_KEY_FILE} match)
44-
if (${match} GREATER_EQUAL 0)
45-
if (NOT DEFINED CONF_DIR)
46-
get_filename_component(CONF_DIR ${filepath} DIRECTORY)
47-
else()
48-
message(FATAL_ERROR "Signature key file defined in multiple conf files")
49-
endif()
50-
endif()
51-
endforeach()
52-
53-
if(IS_ABSOLUTE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
54-
set(KEY_FILE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
55-
elseif((DEFINED CONF_DIR) AND
56-
(EXISTS ${CONF_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE}))
57-
set(KEY_FILE ${CONF_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE})
58-
else()
59-
set(KEY_FILE ${ZEPHYR_MCUBOOT_MODULE_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE})
60-
endif()
61-
message("MCUBoot bootloader key file: ${KEY_FILE}")
62-
63-
set(GENERATED_PUBKEY ${ZEPHYR_BINARY_DIR}/autogen-pubkey.c)
64-
add_custom_command(
65-
OUTPUT ${GENERATED_PUBKEY}
66-
COMMAND
67-
${PYTHON_EXECUTABLE}
68-
${ZEPHYR_MCUBOOT_MODULE_DIR}/scripts/imgtool.py
69-
getpub
70-
-k
71-
${KEY_FILE}
72-
> ${GENERATED_PUBKEY}
73-
DEPENDS ${KEY_FILE}
74-
)
75-
zephyr_library_sources(${GENERATED_PUBKEY})
76-
endif()
23+
include(${ZEPHYR_MCUBOOT_MODULE_DIR}/zephyr/cmake/mcuboot_signature_key_file_generation.cmake)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright (c) 2017 Open Source Foundaries Limited
2+
# Copyright (c) 2022 Legrand North America, LLC.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
#cmake_minimum_required(VERSION 3.20.0)
7+
8+
# This is a MCUBoot-specific fragment for top-level CMakeLists.txt files.
9+
# Sourcing this file brings in reusable logic to generate the public key file.
10+
11+
if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
12+
# CONF_FILE points to the KConfig configuration files of the bootloader.
13+
foreach (mcuboot_filepath ${CONF_FILE})
14+
string(CONFIGURE "${mcuboot_filepath}" mcuboot_filepath_expanded)
15+
file(READ ${mcuboot_filepath_expanded} temp_text)
16+
string(FIND "${temp_text}" ${CONFIG_BOOT_SIGNATURE_KEY_FILE} match)
17+
if (${match} GREATER_EQUAL 0)
18+
if (NOT DEFINED mcuboot_signature_file_dir)
19+
get_filename_component(mcuboot_signature_file_dir ${mcuboot_filepath_expanded} DIRECTORY)
20+
else()
21+
message(FATAL_ERROR "Signature key file defined in multiple conf files")
22+
endif()
23+
endif()
24+
endforeach()
25+
26+
if(IS_ABSOLUTE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
27+
set(KEY_FILE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
28+
elseif((DEFINED mcuboot_signature_file_dir) AND
29+
(EXISTS ${mcuboot_signature_file_dir}/${CONFIG_BOOT_SIGNATURE_KEY_FILE}))
30+
set(KEY_FILE ${mcuboot_signature_file_dir}/${CONFIG_BOOT_SIGNATURE_KEY_FILE})
31+
else()
32+
set(KEY_FILE ${ZEPHYR_MCUBOOT_MODULE_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE})
33+
endif()
34+
message("MCUBoot bootloader key file: ${KEY_FILE}")
35+
36+
set(GENERATED_PUBKEY ${ZEPHYR_BINARY_DIR}/autogen-pubkey.c)
37+
add_custom_command(
38+
OUTPUT ${GENERATED_PUBKEY}
39+
COMMAND
40+
${PYTHON_EXECUTABLE}
41+
${ZEPHYR_MCUBOOT_MODULE_DIR}/scripts/imgtool.py
42+
getpub
43+
-k
44+
${KEY_FILE}
45+
> ${GENERATED_PUBKEY}
46+
DEPENDS ${KEY_FILE}
47+
)
48+
zephyr_library_sources(${GENERATED_PUBKEY})
49+
endif()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright (c) 2017 Open Source Foundaries Limited
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
# Enable Zephyr runner options which request mass erase if so
6+
# configured.
7+
#
8+
# Note that this also disables the default "leave" option when
9+
# targeting STM32 DfuSe devices with dfu-util, making the chip stay in
10+
# the bootloader after flashing.
11+
#
12+
# That's the right thing, because mcuboot has nothing to do since the
13+
# chip was just erased. The next thing the user is going to want to do
14+
# is flash the application. (Developers can reset DfuSE devices
15+
# manually to test mcuboot behavior on an otherwise erased flash
16+
# device.)
17+
macro(app_set_runner_args)
18+
if(CONFIG_ZEPHYR_TRY_MASS_ERASE)
19+
board_runner_args(dfu-util "--dfuse-modifiers=force:mass-erase")
20+
board_runner_args(pyocd "--flash-opt=-e=chip")
21+
board_runner_args(nrfjprog "--erase")
22+
endif()
23+
endmacro()

0 commit comments

Comments
 (0)