Skip to content

Commit 545e2c0

Browse files
SebastianBoembolivar-nordic
authored andcommitted
[nrf noup] cmake: multi-image + partition manager
- Upstream PR#430, plus various fixes from the v1.0.0 development cycle. This added support for being built as a sub image from the downstream Nordic patch set for a zephyr multi image build system. - Downstream partition manager support, plus bug fixes from the v1.0.0 dev cycle. Partition Manager is a component which uses yaml files to resolve flash placement with a wholistic view of the device. Over time, these formerly separate downstream patch series grew increasingly intertwined after the initial rejection of the upstream multi-image patch set. This represents the results which roughly appeared in NCS v1.0. Signed-off-by: Håkon Øye Amundsen <[email protected]> Signed-off-by: Øyvind Rønningstad <[email protected]> Signed-off-by: Sebastian Bøe <[email protected]> Signed-off-by: Sigvart Hovland <[email protected]> Signed-off-by: Marti Bolivar <[email protected]> (cherry picked from commit 0e68ab4) (cherry picked from commit 6168414) (cherry picked from commit d62e189) (cherry picked from commit a4db98d) (cherry picked from commit b6d3687) (cherry picked from commit 4b91989) (cherry picked from commit edc8dc6) (cherry picked from commit 289f108) (cherry picked from commit 447495f) (cherry picked from commit 8cb0e49) (cherry picked from commit 4a61a17) (cherry picked from commit 8b0e50c)
1 parent cd41748 commit 545e2c0

File tree

6 files changed

+239
-11
lines changed

6 files changed

+239
-11
lines changed

boot/zephyr/CMakeLists.txt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ cmake_minimum_required(VERSION 3.8.2)
88

99
# Board-specific CONF_FILES should get merged into the build as well.
1010
# Default to qemu_x86 if no board has been specified.
11-
set(BOARD qemu_x86)
11+
# If BOARD is already defined, mcuboot is being built in a multi image
12+
# context, and should default to its parent image BOARD.
13+
if (NOT BOARD)
14+
set(BOARD qemu_x86)
15+
endif()
1216

1317
# Add a common dts overlay necessary to ensure mcuboot is linked into,
1418
# and fits inside, the boot partition. (If the user specified a
@@ -176,7 +180,7 @@ if(CONFIG_MCUBOOT_SERIAL)
176180

177181
zephyr_link_libraries_ifdef(
178182
CONFIG_TINYCBOR
179-
TINYCBOR
183+
${IMAGE}TINYCBOR
180184
)
181185

182186
zephyr_include_directories_ifdef(
@@ -191,6 +195,14 @@ if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
191195
else()
192196
set(KEY_FILE ${MCUBOOT_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE})
193197
endif()
198+
199+
set_property(
200+
GLOBAL
201+
PROPERTY
202+
KEY_FILE
203+
${KEY_FILE}
204+
)
205+
194206
set(GENERATED_PUBKEY ${ZEPHYR_BINARY_DIR}/autogen-pubkey.c)
195207
add_custom_command(
196208
OUTPUT ${GENERATED_PUBKEY}
@@ -205,4 +217,3 @@ if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
205217
)
206218
zephyr_library_sources(${GENERATED_PUBKEY})
207219
endif()
208-

boot/zephyr/Kconfig

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,37 @@ config MCUBOOT
1414
select MPU_ALLOW_FLASH_WRITE if ARM_MPU
1515
select USE_CODE_PARTITION if HAS_FLASH_LOAD_OFFSET
1616

17+
# Define used by partition_manager.py to deduce size of partition
18+
config PM_PARTITION_SIZE_MCUBOOT
19+
hex "Flash space reserved for bootloader."
20+
default 0xc000
21+
help
22+
Flash space set aside for the MCUBoot. Note, the name
23+
of this configuration needs to match the requirements set by the
24+
script 'partition_manager.py'. See pm.yaml.
25+
26+
27+
# Define used by partition_manager.py to deduce size of partition
28+
config PM_PARTITION_SIZE_MCUBOOT_SCRATCH
29+
hex "Flash space reserved for scratch."
30+
default 0x1e000
31+
help
32+
Flash space set aside for the scratch area.
33+
34+
# Define used by partition_manager.py to deduce size of partition
35+
config PM_PARTITION_SIZE_MCUBOOT_STORAGE
36+
hex "Flash space reserved for storage."
37+
default 0x4000
38+
help
39+
Flash space set aside for the storage area.
40+
41+
# Define used by partition_manager.py to deduce size of partition
42+
config PM_PARTITION_SIZE_MCUBOOT_PAD
43+
hex "Flash space reserved for padding area."
44+
default 0x200
45+
help
46+
Flash space set aside for the padding area.
47+
1748
config BOOT_USE_MBEDTLS
1849
bool
1950
# Hidden option
@@ -111,14 +142,6 @@ endif #BOOT_SIGNATURE_TYPE_ECDSA_P256
111142

112143
endchoice
113144

114-
config BOOT_SIGNATURE_KEY_FILE
115-
string "PEM key file"
116-
default ""
117-
help
118-
The key file will be parsed by imgtool's getpub command and a .c source
119-
with the public key information will be written in a format expected by
120-
MCUboot.
121-
122145
config MBEDTLS_CFG_FILE
123146
default "mcuboot-mbedtls-cfg.h"
124147

boot/zephyr/include/sysflash/sysflash.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@
33
#ifndef __SYSFLASH_H__
44
#define __SYSFLASH_H__
55

6+
#if USE_PARTITION_MANAGER
7+
#include <pm_config.h>
8+
9+
#define FLASH_AREA_IMAGE_PRIMARY PM_MCUBOOT_PRIMARY_ID
10+
#define FLASH_AREA_IMAGE_SECONDARY PM_MCUBOOT_SECONDARY_ID
11+
#define FLASH_AREA_IMAGE_SCRATCH PM_MCUBOOT_SCRATCH_ID
12+
13+
#else
14+
615
#include <generated_dts_board.h>
716

817
#define FLASH_AREA_IMAGE_PRIMARY DT_FLASH_AREA_IMAGE_0_ID
918
#define FLASH_AREA_IMAGE_SECONDARY DT_FLASH_AREA_IMAGE_1_ID
1019
#define FLASH_AREA_IMAGE_SCRATCH DT_FLASH_AREA_IMAGE_SCRATCH_ID
20+
#endif /* USE_PARTITION_MANAGER */
21+
1122

1223
#endif /* __SYSFLASH_H__ */

boot/zephyr/pm.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <autoconf.h>
2+
3+
mcuboot:
4+
size: CONFIG_PM_PARTITION_SIZE_MCUBOOT
5+
placement:
6+
before: [mcuboot_primary]
7+
8+
mcuboot_primary_app:
9+
# All images to be placed in MCUboot's slot 0 should be placed in this
10+
# partition
11+
span: [app]
12+
13+
mcuboot_primary:
14+
span: [mcuboot_pad, mcuboot_primary_app]
15+
16+
mcuboot_secondary:
17+
share_size: [mcuboot_primary]
18+
placement:
19+
after:
20+
[mcuboot_primary]
21+
22+
mcuboot_scratch:
23+
size: CONFIG_PM_PARTITION_SIZE_MCUBOOT_SCRATCH
24+
placement:
25+
after: [app]
26+
27+
mcuboot_storage:
28+
size: CONFIG_PM_PARTITION_SIZE_MCUBOOT_STORAGE
29+
placement:
30+
after: [mcuboot_scratch]
31+
32+
# Padding placed before image to boot
33+
mcuboot_pad:
34+
# MCUboot pad must be placed before the 'spm' partition if that is present.
35+
# If 'spm' partition is not present, it must be placed before the 'app'.
36+
size: CONFIG_PM_PARTITION_SIZE_MCUBOOT_PAD
37+
placement:
38+
before: [mcuboot_primary_app]

zephyr/CMakeLists.txt

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
if(CONFIG_BOOTLOADER_MCUBOOT)
2+
# Build a second bootloader image
3+
4+
set(MCUBOOT_BASE ${CMAKE_CURRENT_LIST_DIR}/..)
5+
6+
zephyr_add_executable(mcuboot require_build)
7+
8+
if (${require_build})
9+
add_subdirectory(${MCUBOOT_BASE}/boot/zephyr ${CMAKE_BINARY_DIR}/mcuboot)
10+
endif() # ${require_build}
11+
12+
set(to_sign_hex ${KERNEL_HEX_NAME})
13+
14+
# TODO: Assert that the bootloader and image use the same key.
15+
16+
set(signed_image_hex ${PROJECT_BINARY_DIR}/signed.hex)
17+
set(signed_image_bin ${PROJECT_BINARY_DIR}/signed.bin)
18+
set(to_sign_bin ${PROJECT_BINARY_DIR}/to_sign.bin)
19+
set(update_hex ${PROJECT_BINARY_DIR}/update.hex)
20+
set(update_bin ${PROJECT_BINARY_DIR}/update.bin)
21+
22+
get_property(app_binary_dir GLOBAL PROPERTY PROJECT_BINARY_DIR)
23+
set(merged_hex_file
24+
${app_binary_dir}/mcuboot_primary_app.hex)
25+
set(merged_hex_file_depends
26+
mcuboot_primary_app_hex$<SEMICOLON>${PROJECT_BINARY_DIR}/mcuboot_primary_app.hex)
27+
set(sign_merged
28+
$<TARGET_EXISTS:partition_manager>)
29+
set(to_sign_hex
30+
$<IF:${sign_merged},${merged_hex_file},${PROJECT_BINARY_DIR}/${KERNEL_HEX_NAME}>)
31+
set(sign_depends
32+
$<IF:${sign_merged},${merged_hex_file_depends},zephyr_final>)
33+
set(sign_cmd
34+
${PYTHON_EXECUTABLE}
35+
${MCUBOOT_BASE}/scripts/imgtool.py
36+
sign
37+
--key ${MCUBOOT_BASE}/${CONFIG_BOOT_SIGNATURE_KEY_FILE}
38+
--header-size $<TARGET_PROPERTY:partition_manager,PM_MCUBOOT_PAD_SIZE>
39+
--align ${DT_FLASH_WRITE_BLOCK_SIZE}
40+
--version ${CONFIG_MCUBOOT_IMAGE_VERSION}
41+
--slot-size $<TARGET_PROPERTY:partition_manager,PM_MCUBOOT_PRIMARY_SIZE>
42+
--pad-header
43+
)
44+
45+
add_custom_command(
46+
OUTPUT
47+
${signed_image_hex}
48+
${update_hex}
49+
${update_bin}
50+
COMMAND
51+
${sign_cmd}
52+
${to_sign_hex}
53+
${signed_image_hex}
54+
COMMAND
55+
${CMAKE_OBJCOPY}
56+
--input-target=ihex
57+
--output-target=binary
58+
${to_sign_hex}
59+
${to_sign_bin}
60+
COMMAND
61+
${sign_cmd}
62+
${to_sign_bin}
63+
${update_bin}
64+
COMMAND
65+
${sign_cmd}
66+
--pad # This argument is needed for MCUboot to apply the test swap.
67+
${to_sign_hex}
68+
${update_hex}
69+
COMMAND
70+
${CMAKE_OBJCOPY}
71+
--input-target=ihex
72+
--output-target=ihex
73+
--change-address $<TARGET_PROPERTY:partition_manager,PM_MCUBOOT_PRIMARY_SIZE>
74+
${update_hex}
75+
${PROJECT_BINARY_DIR}/moved_update.hex
76+
DEPENDS
77+
${sign_depends}
78+
)
79+
add_custom_target(mcuboot_sign_target DEPENDS ${signed_image_hex})
80+
81+
set_property(GLOBAL PROPERTY
82+
mcuboot_primary_app_PM_HEX_FILE
83+
${signed_image_hex}
84+
)
85+
set_property(GLOBAL PROPERTY
86+
mcuboot_primary_app_PM_TARGET
87+
mcuboot_sign_target
88+
)
89+
endif()

zephyr/Kconfig

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
menu "MCUboot"
2+
3+
if BOOTLOADER_MCUBOOT
4+
5+
config MCUBOOT_CMAKELISTS_DIR
6+
string "Path to the directory of the MCUBoot CMakeLists.txt file"
7+
default "$MCUBOOT_BASE/boot/zephyr/"
8+
9+
10+
choice
11+
prompt "MCUBoot build strategy"
12+
default MCUBOOT_BUILD_STRATEGY_FROM_SOURCE
13+
14+
config MCUBOOT_BUILD_STRATEGY_USE_HEX_FILE
15+
# Mandatory option when being built through 'zephyr_add_executable'
16+
bool "Use hex file instead of building MCUBoot"
17+
18+
if MCUBOOT_BUILD_STRATEGY_USE_HEX_FILE
19+
20+
config MCUBOOT_HEX_FILE
21+
# Mandatory option when being built through 'zephyr_add_executable'
22+
string "MCUBoot hex file"
23+
24+
endif # MCUBOOT_USE_HEX_FILE
25+
26+
config MCUBOOT_BUILD_STRATEGY_SKIP_BUILD
27+
# Mandatory option when being built through 'zephyr_add_executable'
28+
bool "Skip building MCUBoot"
29+
30+
config MCUBOOT_BUILD_STRATEGY_FROM_SOURCE
31+
# Mandatory option when being built through 'zephyr_add_executable'
32+
bool "Build from source"
33+
34+
endchoice
35+
36+
config MCUBOOT_IMAGE_VERSION
37+
string "Image version"
38+
default "0.0.0+0"
39+
help
40+
Value to be passed as 'version' argument to 'imgtool.py' when
41+
creating signed image. Note that no semantics are connected to
42+
this variable. It does not provide downgrade prevention, and is only
43+
valuable for debugging purposes. Format: maj.min.rev+build with
44+
latter parts optional.
45+
46+
endif # BOOTLOADER_MCUBOOT
47+
48+
config BOOT_SIGNATURE_KEY_FILE
49+
string "PEM key file"
50+
default "root-rsa-2048.pem"
51+
help
52+
The key file will be parsed by imgtool's getpub command and a .c source
53+
with the public key information will be written in a format expected by
54+
MCUboot.
55+
56+
endmenu

0 commit comments

Comments
 (0)