Skip to content

Commit 07dc5a3

Browse files
committed
cmake: Add function to read partition address
Add a common bootloader_dts_util to read both the relative and absolute address of a fixed sub/partition from DTS. Signed-off-by: Tomasz Chyrowicz <[email protected]>
1 parent 90a5e51 commit 07dc5a3

File tree

5 files changed

+120
-19
lines changed

5 files changed

+120
-19
lines changed

cmake/modules/kconfig.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ if(CONFIG_NCS_IS_VARIANT_IMAGE)
2727

2828
import_kconfig("CONFIG" ${DOTCONFIG})
2929
else()
30+
include(${ZEPHYR_NRF_MODULE_DIR}/cmake/sysbuild/bootloader_dts_utils.cmake)
31+
3032
dt_chosen(code_partition PROPERTY "zephyr,code-partition")
31-
dt_reg_addr(code_partition_offset PATH "${code_partition}" REQUIRED)
33+
dt_partition_addr(code_partition_offset PATH "${code_partition}" REQUIRED)
3234
dt_reg_size(code_partition_size PATH "${code_partition}" REQUIRED)
3335

3436
set(preload_autoconf_h ${PRELOAD_BINARY_DIR}/zephyr/include/generated/zephyr/autoconf.h)

cmake/sysbuild/bootloader_dts_utils.cmake

Lines changed: 108 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,115 @@
55
# data coming from the devicetree for configurations using
66
# a bootloader.
77

8-
function(get_address_from_dt_partition_nodelabel label address)
9-
dt_nodelabel(partition_node TARGET ${DEFAULT_IMAGE} NODELABEL ${label} REQUIRED)
10-
dt_reg_addr(partition_offset TARGET ${DEFAULT_IMAGE} PATH ${partition_node})
8+
function(dt_get_parent node)
9+
string(FIND "${${node}}" "/" pos REVERSE)
10+
11+
if(pos EQUAL -1)
12+
message(FATAL_ERROR "Unable to get parent of node: ${${node}}")
13+
endif()
14+
15+
string(SUBSTRING "${${node}}" 0 ${pos} ${node})
16+
set(${node} "${${node}}" PARENT_SCOPE)
17+
endfunction()
18+
19+
# Usage:
20+
# dt_partition_addr(<var> [LABEL <label>|PATH <path>] [TARGET <target>] [ABSOLUTE])
21+
#
22+
# Get the partition address, based on the path or label.
23+
# The value will be returned in the <var> parameter.
24+
# The returned address is either relative to the memory controller, or absolute if ABSOLUTE is
25+
# specified.
26+
#
27+
# The <path> value may be any of these:
28+
#
29+
# - absolute path to a node, like '/foo/bar'
30+
# - a node alias, like 'my-alias'
31+
# - a node alias followed by a path to a child node, like 'my-alias/child-node'
32+
#
33+
# Results can be:
34+
# - The base address of the register block
35+
# - <var> will be undefined if node does not exists or does not have a register block at the
36+
# requested index or with the requested name
37+
#
38+
# <var> : Return variable where the address value will be stored
39+
# LABEL <label> : Node label
40+
# PATH <path> : Node path
41+
# TARGET <target>: Optional target to retrieve devicetree information from
42+
# ABSOLUTE : Return absolute address rather than address relative to the memory controller
43+
# REQUIRED : Generate a fatal error if the node is not found
44+
function(dt_partition_addr var)
45+
cmake_parse_arguments(arg_DT_PARTITION "ABSOLUTE;REQUIRED" "LABEL;PATH;TARGET" "" ${ARGN})
46+
47+
zephyr_check_arguments_required(${CMAKE_CURRENT_FUNCTION} arg_DT_PARTITION PATH LABEL)
48+
zephyr_check_arguments_exclusive(${CMAKE_CURRENT_FUNCTION} arg_DT_PARTITION PATH LABEL)
49+
50+
if(NOT arg_DT_PARTITION_PATH)
51+
# Calculate the partition path, based on the label.
52+
dt_nodelabel(arg_DT_PARTITION_PATH NODELABEL "${arg_DT_PARTITION_LABEL}" TARGET
53+
"${arg_DT_PARTITION_TARGET}")
54+
if(NOT DEFINED arg_DT_PARTITION_PATH)
55+
if(arg_DT_PARTITION_REQUIRED)
56+
message(FATAL_ERROR "dt_partition_addr: Unable to find a node with label: "
57+
"${arg_DT_PARTITION_LABEL} for target: ${arg_DT_PARTITION_TARGET}")
58+
else()
59+
set(${var} "${var}-NOTFOUND" PARENT_SCOPE)
60+
return()
61+
endif()
62+
endif()
63+
else()
64+
# Check if a partition with the given path exists.
65+
dt_node_exists(arg_DT_PARTITION_EXISTS PATH "${arg_DT_PARTITION_PATH}" TARGET
66+
"${arg_DT_PARTITION_TARGET}")
67+
if(NOT arg_DT_PARTITION_EXISTS)
68+
if(arg_DT_PARTITION_REQUIRED)
69+
message(FATAL_ERROR "dt_partition_addr: Unable to find a node with path: "
70+
"${arg_DT_PARTITION_PATH} for target: ${arg_DT_PARTITION_TARGET}")
71+
else()
72+
set(${var} "${var}-NOTFOUND" PARENT_SCOPE)
73+
return()
74+
endif()
75+
endif()
76+
endif()
1177

12-
# Get the parent "two levels up" (../../) of the partition node
13-
# This is the partition flash area node
14-
string(REPLACE "/" ";" partition_node_split ${partition_node})
15-
list(LENGTH partition_node_split child_path_length)
16-
math(EXPR parent_path_length "${child_path_length} - 2")
17-
list(SUBLIST partition_node_split 0 ${parent_path_length} parent_path_split)
18-
string(REPLACE ";" "/" flash_area_node "${parent_path_split}")
78+
# Get the list of partitions and subpartitions.
79+
dt_comp_path(fixed_partitions COMPATIBLE "fixed-partitions" TARGET "${arg_DT_PARTITION_TARGET}")
80+
dt_comp_path(fixed_subpartitions COMPATIBLE "fixed-subpartitions" TARGET
81+
"${arg_DT_PARTITION_TARGET}")
1982

20-
dt_reg_addr(flash_area_addr TARGET ${DEFAULT_IMAGE} PATH ${flash_area_node})
83+
# Read the partition offset.
84+
dt_reg_addr(dt_partition_offset PATH "${arg_DT_PARTITION_PATH}" TARGET
85+
"${arg_DT_PARTITION_TARGET}")
2186

22-
math(EXPR ${address} "${flash_area_addr} + ${partition_offset}")
23-
set(${address} ${${address}} PARENT_SCOPE)
87+
# The partition parent should be either a fixed-partitions or a fixed-subpartitions node.
88+
set(dt_partition_parent "${arg_DT_PARTITION_PATH}")
89+
dt_get_parent(dt_partition_parent)
90+
91+
if("${dt_partition_parent}" IN_LIST fixed_subpartitions)
92+
# If the parent is a subpartition, add the parent partition address.
93+
dt_reg_addr(parent_addr PATH "${dt_partition_parent}" TARGET "${arg_DT_PARTITION_TARGET}")
94+
math(EXPR dt_partition_offset "${dt_partition_offset} + ${parent_addr}" OUTPUT_FORMAT
95+
HEXADECIMAL)
96+
97+
# Get the parent of the subpartition node, which should be a fixed-partitions node.
98+
dt_get_parent(dt_partition_parent)
99+
elseif(NOT "${dt_partition_parent}" IN_LIST fixed_partitions)
100+
message(FATAL_ERROR "dt_partition_addr: Node is not a partition or subpartition: "
101+
"${arg_DT_PARTITION_PATH} in target: ${arg_DT_PARTITION_TARGET}")
102+
endif()
103+
104+
if(NOT arg_DT_PARTITION_ABSOLUTE)
105+
# A parent of the "fixed-partitions" node should be the memory controller.
106+
dt_get_parent(dt_partition_parent)
107+
# Add the memory controller base address to get an absolute address.
108+
dt_reg_addr(parent_addr PATH "${dt_partition_parent}" TARGET "${arg_DT_PARTITION_TARGET}")
109+
math(EXPR dt_partition_offset "${dt_partition_offset} + ${parent_addr}" OUTPUT_FORMAT
110+
HEXADECIMAL)
111+
endif()
112+
113+
set(${var} "${dt_partition_offset}" PARENT_SCOPE)
114+
endfunction()
115+
116+
function(get_address_from_dt_partition_nodelabel label address)
117+
dt_partition_addr(${address} LABEL "${label}" TARGET "${DEFAULT_IMAGE}" ABSOLUTE REQUIRED)
118+
set(${address} "${${address}}" PARENT_SCOPE)
24119
endfunction()

cmake/sysbuild/image_signing.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
# Since this file is brought in via include(), we do the work in a
1111
# function to avoid polluting the top-level scope.
1212

13+
include(${ZEPHYR_NRF_MODULE_DIR}/cmake/sysbuild/bootloader_dts_utils.cmake)
14+
1315
function(zephyr_runner_file type path)
1416
# Property magic which makes west flash choose the signed build
1517
# output of a given type.
@@ -87,7 +89,7 @@ function(zephyr_mcuboot_tasks)
8789
set(imgtool_rom_command)
8890
if(CONFIG_MCUBOOT_BOOTLOADER_MODE_DIRECT_XIP_WITH_REVERT OR CONFIG_MCUBOOT_BOOTLOADER_MODE_DIRECT_XIP)
8991
dt_chosen(code_partition PROPERTY "zephyr,code-partition")
90-
dt_reg_addr(code_partition_offset PATH "${code_partition}" REQUIRED)
92+
dt_partition_addr(code_partition_offset PATH "${code_partition}" REQUIRED)
9193
set(imgtool_rom_command --rom-fixed ${code_partition_offset})
9294
endif()
9395
set(imgtool_sign ${PYTHON_EXECUTABLE} ${IMGTOOL} sign --version ${CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION} --align ${write_block_size} --slot-size ${slot_size} --header-size ${CONFIG_ROM_START_OFFSET} ${imgtool_rom_command})

cmake/sysbuild/sign_nrf54h20.cmake

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#
33
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
44

5+
include(${ZEPHYR_NRF_MODULE_DIR}/cmake/sysbuild/bootloader_dts_utils.cmake)
6+
57
function(merge_images_nrf54h20 output_artifact images)
68
find_program(MERGEHEX mergehex.py HINTS ${ZEPHYR_BASE}/scripts/build/ NAMES
79
mergehex NAMES_PER_DIR)
@@ -177,10 +179,10 @@ function(mcuboot_sign_merged_nrf54h20 merged_hex main_image)
177179
dt_prop(write_block_size TARGET mcuboot PATH "${flash_node}" PROPERTY
178180
"write-block-size")
179181
dt_nodelabel(slot0_path TARGET mcuboot NODELABEL "slot0_partition" REQUIRED)
180-
dt_reg_addr(slot0_addr TARGET mcuboot PATH ${slot0_path})
182+
dt_partition_addr(slot0_addr PATH "${slot0_path}" TARGET mcuboot REQUIRED)
181183
dt_reg_size(slot0_size TARGET mcuboot PATH ${slot0_path})
182184
dt_nodelabel(slot1_path TARGET mcuboot NODELABEL "slot1_partition" REQUIRED)
183-
dt_reg_addr(slot1_addr TARGET mcuboot PATH ${slot1_path})
185+
dt_partition_addr(slot1_addr PATH "${slot1_path}" TARGET mcuboot REQUIRED)
184186
dt_reg_size(slot1_size TARGET mcuboot PATH ${slot1_path})
185187
if(NOT write_block_size)
186188
set(write_block_size 4)
@@ -190,7 +192,7 @@ function(mcuboot_sign_merged_nrf54h20 merged_hex main_image)
190192

191193
# Fetch devicetree details for the active code partition.
192194
dt_chosen(code_flash TARGET ${main_image} PROPERTY "zephyr,code-partition")
193-
dt_reg_addr(code_addr TARGET ${main_image} PATH ${code_flash})
195+
dt_partition_addr(code_addr PATH "${code_flash}" TARGET ${main_image} REQUIRED)
194196
set(start_offset)
195197
sysbuild_get(start_offset IMAGE ${main_image} VAR CONFIG_ROM_START_OFFSET
196198
KCONFIG)

west.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ manifest:
6565
# https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/guides/modules.html
6666
- name: zephyr
6767
repo-path: sdk-zephyr
68-
revision: e2ea7ff53ff6701c74be3da713c33d473c311aad
68+
revision: d47f109210e74e2e9df3ab5485a32b8a34254caf
6969
import:
7070
# In addition to the zephyr repository itself, NCS also
7171
# imports the contents of zephyr/west.yml at the above

0 commit comments

Comments
 (0)