Skip to content

Commit b6d3687

Browse files
hakonfamSebastianBoe
authored andcommitted
[nrf noup] zephyr: introduce partition manager
Partition Manager is a component which uses yaml files to resolve flash placement with a wholistic view of the device Signed-off-by: Håkon Øye Amundsen <[email protected]>
1 parent d62e189 commit b6d3687

File tree

4 files changed

+144
-8
lines changed

4 files changed

+144
-8
lines changed

boot/zephyr/Kconfig

Lines changed: 31 additions & 0 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

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_PARTITIONS_PRIMARY_ID
10+
#define FLASH_AREA_IMAGE_SECONDARY PM_MCUBOOT_PARTITIONS_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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# The size of this partition is defined by the kconfig symbol
2+
# CONFIG_PM_PARTITION_SIZE_MCUBOOT
3+
mcuboot:
4+
placement:
5+
# MCUboot must be placed in front of 'mcuboot_pad'.
6+
before: [mcuboot_pad]
7+
8+
mcuboot_partitions:
9+
# Define a set of sub-partitions which spans over 'mcuboot_pad', 'spm' and
10+
# 'app' if both are present. If only 'app' is present, then these partitions
11+
# will only span across that partition.
12+
# The sub partitions share the size of the parent partition(s) equally.
13+
sub_partitions: [primary, secondary]
14+
span: [mcuboot_pad, spm, app]
15+
16+
# The size of this partition is defined by the kconfig symbol
17+
# CONFIG_PM_PARTITION_SIZE_MCUBOOT_SCRATCH
18+
mcuboot_scratch:
19+
placement:
20+
after: [app]
21+
22+
# The size of this partition is defined by the kconfig symbol
23+
# CONFIG_PM_PARTITION_SIZE_MCUBOOT_STORAGE
24+
mcuboot_storage:
25+
placement:
26+
after: [mcuboot_scratch]
27+
28+
# Padding placed before image to boot
29+
mcuboot_pad:
30+
# MCUboot pad must be placed before the 'spm' partition if that is present.
31+
# If 'spm' partition is not present, it must be placed before the 'app'.
32+
placement:
33+
before: [spm, app]

zephyr/CMakeLists.txt

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,93 @@ if(CONFIG_BOOTLOADER_MCUBOOT)
88
if (${require_build})
99
add_subdirectory(${MCUBOOT_BASE}/boot/zephyr ${CMAKE_BINARY_DIR}/mcuboot)
1010

11+
set(to_sign ${KERNEL_HEX_NAME})
12+
1113
# TODO: Assert that the bootloader and image use the same key.
1214

13-
set(SIGNED_IMAGE signed.hex)
15+
set(signed_image_hex ${PROJECT_BINARY_DIR}/signed.hex)
16+
set(signed_image_bin ${PROJECT_BINARY_DIR}/signed.bin)
17+
set(to_sign_bin ${PROJECT_BINARY_DIR}/to_sign.bin)
18+
set(update_hex ${PROJECT_BINARY_DIR}/update.hex)
19+
set(update_bin ${PROJECT_BINARY_DIR}/update.bin)
1420

15-
set_property(GLOBAL APPEND PROPERTY
16-
extra_post_build_commands
21+
set(merged_hex_file
22+
$<TARGET_PROPERTY:partition_manager,MCUBOOT_TO_SIGN>)
23+
set(merged_hex_file_depends
24+
$<TARGET_PROPERTY:partition_manager,MCUBOOT_TO_SIGN_DEPENDS>)
25+
set(sign_merged
26+
$<BOOL:$<TARGET_PROPERTY:partition_manager,MCUBOOT_TO_SIGN>>)
27+
set(to_sign
28+
$<IF:${sign_merged},${merged_hex_file},${PROJECT_BINARY_DIR}/${KERNEL_HEX_NAME}>)
29+
set(sign_depends
30+
$<IF:${sign_merged},${merged_hex_file_depends},kernel_elf>)
31+
add_custom_command(
32+
OUTPUT
33+
${signed_image_hex}
34+
${update_hex}
35+
${update_bin}
1736
COMMAND
1837
${PYTHON_EXECUTABLE}
1938
${MCUBOOT_BASE}/scripts/imgtool.py
2039
sign
2140
--key ${MCUBOOT_BASE}/${CONFIG_BOOT_SIGNATURE_KEY_FILE}
22-
--header-size ${CONFIG_TEXT_SECTION_OFFSET}
41+
--header-size $<TARGET_PROPERTY:partition_manager,MCUBOOT_HEADER_SIZE>
2342
--align ${DT_FLASH_WRITE_BLOCK_SIZE}
2443
--version 0.1 # TODO: Configurable?
25-
--slot-size 0x32000 # TODO: Configurable?
26-
${KERNEL_HEX_NAME} # TODO: Enforce that this will be present through Kconfig
27-
${SIGNED_IMAGE}
44+
--slot-size $<TARGET_PROPERTY:partition_manager,MCUBOOT_SLOT_SIZE>
45+
--pad-header
46+
${to_sign}
47+
${signed_image_hex}
48+
COMMAND
49+
${CMAKE_OBJCOPY}
50+
--input-target=ihex
51+
--output-target=binary
52+
${to_sign}
53+
${to_sign_bin}
54+
COMMAND
55+
${PYTHON_EXECUTABLE}
56+
${MCUBOOT_BASE}/scripts/imgtool.py
57+
sign
58+
--key ${MCUBOOT_BASE}/${CONFIG_BOOT_SIGNATURE_KEY_FILE}
59+
--header-size $<TARGET_PROPERTY:partition_manager,MCUBOOT_HEADER_SIZE>
60+
--align ${DT_FLASH_WRITE_BLOCK_SIZE}
61+
--version 0.1 # TODO: Configurable?
62+
--slot-size $<TARGET_PROPERTY:partition_manager,MCUBOOT_SLOT_SIZE>
63+
--pad-header
64+
${to_sign_bin}
65+
${update_bin}
66+
COMMAND
67+
${CMAKE_OBJCOPY}
68+
--input-target=binary
69+
--output-target=ihex
70+
${update_bin}
71+
${update_hex}
72+
COMMAND
73+
${CMAKE_OBJCOPY}
74+
--input-target=binary
75+
--output-target=ihex
76+
${update_bin}
77+
${update_hex}
78+
COMMAND
79+
${CMAKE_OBJCOPY}
80+
--input-target=ihex
81+
--output-target=ihex
82+
--change-address $<TARGET_PROPERTY:partition_manager,MCUBOOT_SECONDARY_ADDRESS>
83+
${update_hex}
84+
${PROJECT_BINARY_DIR}/moved_update.hex
85+
DEPENDS
86+
${sign_depends}
2887
)
88+
add_custom_target(mcuboot_sign_target DEPENDS ${signed_image_hex})
2989

3090
set_property(GLOBAL APPEND PROPERTY
3191
HEX_FILES_TO_MERGE
32-
${SIGNED_IMAGE}
92+
${signed_image_hex}
3393
)
3494
set_property(GLOBAL APPEND PROPERTY
3595
HEX_FILES_TO_MERGE_TARGET
3696
${logical_target_for_zephyr_elf}
97+
mcuboot_sign_target
3798
)
3899
endif() # ${require_build}
39100
endif()

0 commit comments

Comments
 (0)