Skip to content

Commit 716f338

Browse files
thedjnKde-nordic
authored andcommitted
boot: zephyr: Remove weird custom key directory handling
This code seems to have been introduced by someone without knowledge of the zephyr build system, specifying a Kconfig value in multiple files is a completely legal operation, the one that was applied last is the one that is used, and the default directory for keys should be the application configuration directory, not assuming where a .conf file is specifies the same folder as a key file (which is completely at odds with how Zephyr's file finding CMake code works). Signed-off-by: Jamie McCrae <[email protected]>
1 parent c52f8af commit 716f338

File tree

2 files changed

+34
-51
lines changed

2 files changed

+34
-51
lines changed

boot/zephyr/CMakeLists.txt

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -339,28 +339,17 @@ if(CONFIG_MCUBOOT_SERIAL)
339339
endif()
340340

341341
if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
342-
# CONF_FILE points to the KConfig configuration files of the bootloader.
343-
foreach (filepath ${CONF_FILE})
344-
file(READ ${filepath} temp_text)
345-
string(FIND "${temp_text}" ${CONFIG_BOOT_SIGNATURE_KEY_FILE} match)
346-
if (${match} GREATER_EQUAL 0)
347-
if (NOT DEFINED CONF_DIR)
348-
get_filename_component(CONF_DIR ${filepath} DIRECTORY)
349-
else()
350-
message(FATAL_ERROR "Signature key file defined in multiple conf files")
351-
endif()
352-
endif()
353-
endforeach()
342+
set(key_file "${CONFIG_BOOT_SIGNATURE_KEY_FILE}")
343+
string(CONFIGURE "${key_file}" key_file)
354344

355-
if(IS_ABSOLUTE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
356-
set(KEY_FILE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
357-
elseif((DEFINED CONF_DIR) AND
358-
(EXISTS ${CONF_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE}))
359-
set(KEY_FILE ${CONF_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE})
345+
if(IS_ABSOLUTE ${key_file})
346+
set(signing_key_file ${key_file})
347+
elseif(EXISTS ${APPLICATION_CONFIG_DIR}/${key_file})
348+
set(signing_key_file ${APPLICATION_CONFIG_DIR}/${key_file})
360349
else()
361-
set(KEY_FILE ${MCUBOOT_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE})
350+
set(signing_key_file ${MCUBOOT_DIR}/${key_file})
362351
endif()
363-
message("MCUBoot bootloader key file: ${KEY_FILE}")
352+
message("MCUBoot bootloader key file: ${signing_key_file}")
364353

365354
set(mcuboot_default_signature_files
366355
${MCUBOOT_DIR}/root-ec-p256-pkcs8.pem
@@ -373,7 +362,7 @@ if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
373362
)
374363

375364
# Emit a warning if using one of the default MCUboot key files
376-
if(${KEY_FILE} IN_LIST mcuboot_default_signature_files)
365+
if(${signing_key_file} IN_LIST mcuboot_default_signature_files)
377366
message(WARNING "WARNING: Using default MCUboot signing key file, this file is for debug use only and is not secure!")
378367
endif()
379368

@@ -385,37 +374,25 @@ if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
385374
${MCUBOOT_DIR}/scripts/imgtool.py
386375
getpub
387376
-k
388-
${KEY_FILE}
377+
${signing_key_file}
389378
> ${GENERATED_PUBKEY}
390-
DEPENDS ${KEY_FILE}
379+
DEPENDS ${signing_key_file}
391380
)
392381
zephyr_library_sources(${GENERATED_PUBKEY})
393382
endif()
394383

395384
if(CONFIG_BOOT_ENCRYPTION_KEY_FILE AND NOT CONFIG_BOOT_ENCRYPTION_KEY_FILE STREQUAL "")
396-
# CONF_FILE points to the KConfig configuration files of the bootloader.
397-
unset(CONF_DIR)
398-
foreach(filepath ${CONF_FILE})
399-
file(READ ${filepath} temp_text)
400-
string(FIND "${temp_text}" ${CONFIG_BOOT_ENCRYPTION_KEY_FILE} match)
401-
if(${match} GREATER_EQUAL 0)
402-
if(NOT DEFINED CONF_DIR)
403-
get_filename_component(CONF_DIR ${filepath} DIRECTORY)
404-
else()
405-
message(FATAL_ERROR "Encryption key file defined in multiple conf files")
406-
endif()
407-
endif()
408-
endforeach()
385+
set(key_file "${CONFIG_BOOT_ENCRYPTION_KEY_FILE}")
386+
string(CONFIGURE "${key_file}" key_file)
409387

410-
if(IS_ABSOLUTE ${CONFIG_BOOT_ENCRYPTION_KEY_FILE})
411-
set(KEY_FILE ${CONFIG_BOOT_ENCRYPTION_KEY_FILE})
412-
elseif((DEFINED CONF_DIR) AND
413-
(EXISTS ${CONF_DIR}/${CONFIG_BOOT_ENCRYPTION_KEY_FILE}))
414-
set(KEY_FILE ${CONF_DIR}/${CONFIG_BOOT_ENCRYPTION_KEY_FILE})
388+
if(IS_ABSOLUTE ${key_file})
389+
set(encryption_key_file ${key_file})
390+
elseif(EXISTS ${APPLICATION_CONFIG_DIR}/${key_file})
391+
set(encryption_key_file ${APPLICATION_CONFIG_DIR}/${key_file})
415392
else()
416-
set(KEY_FILE ${MCUBOOT_DIR}/${CONFIG_BOOT_ENCRYPTION_KEY_FILE})
393+
set(encryption_key_file ${MCUBOOT_DIR}/${key_file})
417394
endif()
418-
message("MCUBoot bootloader encryption key file: ${KEY_FILE}")
395+
message("MCUBoot bootloader encryption key file: ${encryption_key_file}")
419396

420397
# Emit a warning if using one of the default MCUboot key files
421398
set(mcuboot_default_encryption_files
@@ -427,7 +404,7 @@ if(CONFIG_BOOT_ENCRYPTION_KEY_FILE AND NOT CONFIG_BOOT_ENCRYPTION_KEY_FILE STREQ
427404
${MCUBOOT_DIR}/enc-x25519-pub.pem
428405
)
429406

430-
if(${KEY_FILE} IN_LIST mcuboot_default_encryption_files)
407+
if(${encryption_key_file} IN_LIST mcuboot_default_encryption_files)
431408
message(WARNING "WARNING: Using default MCUboot encryption key file, this file is for debug use only and is not secure!")
432409
endif()
433410

@@ -439,9 +416,9 @@ if(CONFIG_BOOT_ENCRYPTION_KEY_FILE AND NOT CONFIG_BOOT_ENCRYPTION_KEY_FILE STREQ
439416
${MCUBOOT_DIR}/scripts/imgtool.py
440417
getpriv
441418
-k
442-
${KEY_FILE}
419+
${encryption_key_file}
443420
> ${GENERATED_ENCKEY}
444-
DEPENDS ${KEY_FILE}
421+
DEPENDS ${encryption_key_file}
445422
)
446423
zephyr_library_sources(${GENERATED_ENCKEY})
447424
endif()

boot/zephyr/Kconfig

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,16 @@ config BOOT_SIGNATURE_KEY_FILE
399399
help
400400
You can use either absolute or relative path.
401401
In case relative path is used, the build system assumes that it starts
402-
from the directory where the MCUBoot KConfig configuration file is
403-
located. If the key file is not there, the build system uses relative
404-
path that starts from the MCUBoot repository root directory.
402+
from the APPLICATION_CONFIG_DIR directory. If the key file is not there, the build
403+
system uses relative path that starts from the MCUBoot repository root directory.
405404
The key file will be parsed by imgtool's getpub command and a .c source
406405
with the public key information will be written in a format expected by
407406
MCUboot.
408407

408+
Note: In configuration files, escaped CMake variables can be used to refer to paths
409+
e.g. \${CMAKE_CURRENT_LIST_DIR} will allow referencing a file in that directory, these
410+
will be automatically configured by the build system.
411+
409412
config MCUBOOT_CLEANUP_ARM_CORE
410413
bool "Perform core cleanup before chain-load the application"
411414
depends on CPU_CORTEX_M || ARMV7_R
@@ -757,13 +760,16 @@ config BOOT_ENCRYPTION_KEY_FILE
757760
help
758761
You can use either absolute or relative path.
759762
In case relative path is used, the build system assumes that it starts
760-
from the directory where the MCUBoot KConfig configuration file is
761-
located. If the key file is not there, the build system uses relative
762-
path that starts from the MCUBoot repository root directory.
763+
from the APPLICATION_CONFIG_DIR directory. If the key file is not there, the build
764+
system uses relative path that starts from the MCUBoot repository root directory.
763765
The key file will be parsed by imgtool's getpriv command and a .c source
764766
with the public key information will be written in a format expected by
765767
MCUboot.
766768

769+
Note: In configuration files, escaped CMake variables can be used to refer to paths
770+
e.g. \${CMAKE_CURRENT_LIST_DIR} will allow referencing a file in that directory, these
771+
will be automatically configured by the build system.
772+
767773
config BOOT_MAX_IMG_SECTORS_AUTO
768774
bool "Calculate maximum sectors automatically"
769775
default y

0 commit comments

Comments
 (0)