Skip to content

Commit 8d8d523

Browse files
gchwiernordicjm
authored andcommitted
cmake: Added default key file generation for KMU provisioning
This commit introduces the capability to automatically generate the keyfile.json during the build process for nRF54L series devices. Added new Kconfigs in Kconfig.mcuboot and Kconfig.secureboot to control creating keyfile.json during the build process. Creating keyfile.json is implemented in generate_default_keyfile.cmake Signed-off-by: Grzegorz Chwierut <[email protected]>
1 parent d340a9a commit 8d8d523

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright (c) 2025 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
3+
4+
# This script defines a CMake target 'generate_kmu_keyfile_json' to create keyfile.json
5+
# using 'west ncs-provision upload --dry-run'.
6+
7+
# --- Construct the list of commands and dependencies ---
8+
set(kmu_json_commands "")
9+
set(kmu_json_dependencies "")
10+
11+
# First command: Generate keyfile for BL_PUBKEY
12+
if(SB_CONFIG_SECURE_BOOT_GENERATE_DEFAULT_KMU_KEYFILE)
13+
# --- Determine the signing key file to use ---
14+
set(signature_private_key_file "") # Initialize
15+
16+
if(SB_CONFIG_SECURE_BOOT_SIGNING_KEY_FILE)
17+
string(CONFIGURE "${SB_CONFIG_SECURE_BOOT_SIGNING_KEY_FILE}" keyfile)
18+
if(IS_ABSOLUTE ${keyfile})
19+
set(signature_private_key_file ${keyfile})
20+
else()
21+
set(signature_private_key_file ${APPLICATION_CONFIG_DIR}/${keyfile})
22+
endif()
23+
set(keyfile)
24+
25+
if(NOT EXISTS ${signature_private_key_file})
26+
message(FATAL_ERROR "Config points to non-existing PEM file '${signature_private_key_file}'")
27+
endif()
28+
else()
29+
set(signature_private_key_file "${CMAKE_BINARY_DIR}/GENERATED_NON_SECURE_SIGN_KEY_PRIVATE.pem")
30+
endif()
31+
32+
list(APPEND kmu_json_commands
33+
COMMAND ${Python3_EXECUTABLE} -m west ncs-provision upload
34+
--keyname BL_PUBKEY
35+
--key ${signature_private_key_file}
36+
--build-dir ${CMAKE_BINARY_DIR}
37+
--dry-run
38+
)
39+
list(APPEND kmu_json_dependencies ${signature_private_key_file})
40+
endif()
41+
42+
# Second command (conditional): Update keyfile for UROT_PUBKEY
43+
if(SB_CONFIG_MCUBOOT_GENERATE_DEFAULT_KMU_KEYFILE)
44+
list(APPEND kmu_json_commands
45+
COMMAND ${Python3_EXECUTABLE} -m west ncs-provision upload
46+
--keyname UROT_PUBKEY
47+
--key ${SB_CONFIG_BOOT_SIGNATURE_KEY_FILE}
48+
--build-dir ${CMAKE_BINARY_DIR}
49+
--dry-run
50+
)
51+
list(APPEND kmu_json_dependencies ${SB_CONFIG_BOOT_SIGNATURE_KEY_FILE})
52+
endif()
53+
54+
# --- Add custom command to generate/update keyfile.json ---
55+
if(NOT kmu_json_commands STREQUAL "")
56+
add_custom_command(
57+
OUTPUT ${CMAKE_BINARY_DIR}/keyfile.json
58+
${kmu_json_commands} # Expands to one or more COMMAND clauses
59+
DEPENDS ${kmu_json_dependencies}
60+
COMMENT "Generating/Updating KMU keyfile JSON (${CMAKE_BINARY_DIR}/keyfile.json)"
61+
VERBATIM
62+
)
63+
64+
# --- Add custom target to trigger the generation ---
65+
add_custom_target(
66+
generate_kmu_keyfile_json ALL
67+
DEPENDS ${CMAKE_BINARY_DIR}/keyfile.json
68+
)
69+
endif()

sysbuild/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,10 @@ function(${SYSBUILD_CURRENT_MODULE_NAME}_post_cmake)
737737
include_provision_hex()
738738
endif()
739739

740+
if(SB_CONFIG_SECURE_BOOT_GENERATE_DEFAULT_KMU_KEYFILE OR SB_CONFIG_MCUBOOT_GENERATE_DEFAULT_KMU_KEYFILE)
741+
include(${ZEPHYR_NRF_MODULE_DIR}/cmake/sysbuild/generate_default_keyfile.cmake)
742+
endif()
743+
740744
if(SB_CONFIG_MATTER_OTA)
741745
include(${ZEPHYR_CONNECTEDHOMEIP_MODULE_DIR}/config/zephyr/ota-image_sysbuild.cmake)
742746
if(SB_CONFIG_DFU_MULTI_IMAGE_PACKAGE_BUILD OR SB_CONFIG_SUIT_MULTI_IMAGE_PACKAGE_BUILD)

sysbuild/Kconfig.mcuboot

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ config BOOT_SHARED_CRYPTO_ECDSA_P256
176176
depends on BOOT_SIGNATURE_TYPE_ECDSA_P256 && !SOC_SERIES_NRF54LX
177177
default y
178178

179+
config MCUBOOT_GENERATE_DEFAULT_KMU_KEYFILE
180+
bool "Generate default keyfile for provisioning during build"
181+
depends on SOC_SERIES_NRF54LX
182+
depends on MCUBOOT_SIGNATURE_USING_KMU
183+
help
184+
If enabled, the build system will generate keyfile.json file in the build directory.
185+
179186
endif
180187

181188
config MCUBOOT_USE_ALL_AVAILABLE_RAM

sysbuild/Kconfig.secureboot

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,13 @@ config SECURE_BOOT_DEBUG_NO_VERIFY_HASHES
361361
help
362362
[DEBUG] Don't check public key hashes for applicability. Only Use this in (negative) tests!
363363

364+
config SECURE_BOOT_GENERATE_DEFAULT_KMU_KEYFILE
365+
bool "Generate default keyfile for provisioning during build"
366+
depends on SOC_SERIES_NRF54LX
367+
depends on SECURE_BOOT_APPCORE
368+
help
369+
If enabled, the build system will generate keyfile.json file in the build directory.
370+
364371
endif
365372

366373
endmenu

0 commit comments

Comments
 (0)