Skip to content

Commit ed2079d

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 ecd0955 commit ed2079d

File tree

3 files changed

+77
-65
lines changed

3 files changed

+77
-65
lines changed

boot/zephyr/CMakeLists.txt

Lines changed: 8 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,14 @@ cmake_minimum_required(VERSION 3.13.1)
1111
# and fits inside, the boot partition. (If the user specified a
1212
# DTC_OVERLAY_FILE on the CMake command line, we need to append onto
1313
# the list).
14-
if(DTC_OVERLAY_FILE)
15-
set(DTC_OVERLAY_FILE
16-
"${DTC_OVERLAY_FILE} ${CMAKE_CURRENT_LIST_DIR}/dts.overlay"
17-
CACHE STRING "" FORCE
18-
)
19-
else()
20-
set(DTC_OVERLAY_FILE ${CMAKE_CURRENT_LIST_DIR}/dts.overlay)
21-
endif()
14+
set(DTC_OVERLAY_FILE
15+
"${DTC_OVERLAY_FILE};${CMAKE_CURRENT_LIST_DIR}/dts.overlay"
16+
CACHE STRING "" FORCE
17+
)
2218

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

4323
# find_package(Zephyr) in order to load application boilerplate:
4424
# http://docs.zephyrproject.org/application/application.html
@@ -49,41 +29,4 @@ target_sources(app PRIVATE
4929
keys.c
5030
)
5131

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

0 commit comments

Comments
 (0)