Skip to content

Commit 16856ae

Browse files
committed
[nrf fromlist] linker: Use zephyr,code-partition value
Enforce usage of values from devicetree if USE_DT_CODE_PARTITION Kconfig symbol is enabled. Currently even if this symbol is enabled, the FLASH_LOAD_OFFSET value is used in the linker script. The FLASH_LOAD_OFFSET symbol, even being promptless is such case, may be changed, leading to a very confusing situation, where devicetree values have no effect on the build. A good example of the build that wants to use exactly the same set of configs, but a different link address is the A/B update scheme (known ad Direct XIP in MCUboot), where the same application is linked to two different addresses. If the source of the address remains in the Kconifg, the current approach forces a usage of two different configurations. Upstream PR #: 91591 Signed-off-by: Tomasz Chyrowicz <[email protected]>
1 parent 68a48ec commit 16856ae

File tree

8 files changed

+122
-29
lines changed

8 files changed

+122
-29
lines changed

CMakeLists.txt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,16 +1874,30 @@ if(CONFIG_BUILD_OUTPUT_BIN AND CONFIG_BUILD_OUTPUT_UF2)
18741874
if(CONFIG_BUILD_OUTPUT_UF2_USE_FLASH_BASE)
18751875
set(code_address "${CONFIG_FLASH_BASE_ADDRESS}")
18761876
else()
1877-
set(code_address "${CONFIG_FLASH_LOAD_OFFSET}")
1877+
if(CONFIG_USE_DT_CODE_PARTITION)
1878+
dt_chosen(chosen_code_partition PROPERTY "zephyr,code-partition")
1879+
dt_reg_addr(code_address PATH ${chosen_code_partition})
1880+
else()
1881+
set(code_address "${CONFIG_FLASH_LOAD_OFFSET}")
1882+
endif()
18781883
endif()
18791884

18801885
if(CONFIG_BUILD_OUTPUT_UF2_USE_FLASH_OFFSET)
18811886
# Note, the `+ 0` in formula below avoids errors in cases where a Kconfig
18821887
# variable is undefined and thus expands to nothing.
1883-
math(EXPR code_address
1884-
"${code_address} + ${CONFIG_FLASH_LOAD_OFFSET} + 0"
1885-
OUTPUT_FORMAT HEXADECIMAL
1886-
)
1888+
if(CONFIG_USE_DT_CODE_PARTITION)
1889+
dt_chosen(chosen_code_partition PROPERTY "zephyr,code-partition")
1890+
dt_reg_addr(flash_code_partition_addr PATH ${chosen_code_partition})
1891+
math(EXPR code_address
1892+
"${code_address} + ${flash_code_partition_addr} + 0"
1893+
OUTPUT_FORMAT HEXADECIMAL
1894+
)
1895+
else()
1896+
math(EXPR code_address
1897+
"${code_address} + ${CONFIG_FLASH_LOAD_OFFSET} + 0"
1898+
OUTPUT_FORMAT HEXADECIMAL
1899+
)
1900+
endif()
18871901
endif()
18881902

18891903
# No-XIP images (such as ones for RP2350 with CONFIG_XIP=n)

cmake/linker_script/arm/linker.cmake

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,30 @@ endif()
3333
zephyr_linker_include_var(VAR APP_SHARED_ALIGN_BYTES VALUE ${region_min_align})
3434
zephyr_linker_include_var(VAR SMEM_PARTITION_ALIGN_BYTES VALUE ${MPU_ALIGN_BYTES})
3535

36+
if(CONFIG_USE_DT_CODE_PARTITION)
37+
dt_chosen(chosen_code_partition PROPERTY "zephyr,code-partition")
38+
dt_reg_addr(flash_code_partition_addr PATH ${chosen_code_partition})
39+
dt_reg_size(flash_code_partition_size PATH ${chosen_code_partition})
40+
else()
41+
set(flash_code_partition_addr "${CONFIG_FLASH_LOAD_OFFSET}")
42+
set(flash_code_partition_size "${CONFIG_FLASH_LOAD_SIZE}")
43+
endif()
44+
3645
# Note, the `+ 0` in formulas below avoids errors in cases where a Kconfig
3746
# variable is undefined and thus expands to nothing.
3847
math(EXPR FLASH_ADDR
39-
"${CONFIG_FLASH_BASE_ADDRESS} + ${CONFIG_FLASH_LOAD_OFFSET} + 0"
48+
"${CONFIG_FLASH_BASE_ADDRESS} + ${flash_code_partition_addr} + 0"
4049
OUTPUT_FORMAT HEXADECIMAL
4150
)
4251

43-
if(CONFIG_FLASH_LOAD_SIZE GREATER 0)
52+
if(flash_code_partition_size GREATER 0)
4453
math(EXPR FLASH_SIZE
45-
"(${CONFIG_FLASH_LOAD_SIZE} + 0) - (${CONFIG_ROM_END_OFFSET} + 0)"
54+
"(${flash_code_partition_size} + 0) - (${CONFIG_ROM_END_OFFSET} + 0)"
4655
OUTPUT_FORMAT HEXADECIMAL
4756
)
4857
else()
4958
math(EXPR FLASH_SIZE
50-
"(${CONFIG_FLASH_SIZE} + 0) * 1024 - (${CONFIG_FLASH_LOAD_OFFSET} + 0) - (${CONFIG_ROM_END_OFFSET} + 0)"
59+
"(${CONFIG_FLASH_SIZE} + 0) * 1024 - (${flash_code_partition_addr} + 0) - (${CONFIG_ROM_END_OFFSET} + 0)"
5160
OUTPUT_FORMAT HEXADECIMAL
5261
)
5362
endif()

include/zephyr/arch/arm/cortex_a_r/scripts/linker.ld

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@
3434
#define ROMSTART_REGION ROMABLE_REGION
3535
#endif
3636

37+
#if CONFIG_USE_DT_CODE_PARTITION
38+
#define FLASH_LOAD_OFFSET DT_REG_ADDR(DT_CHOSEN(zephyr_code_partition))
39+
#define FLASH_LOAD_SIZE DT_REG_SIZE(DT_CHOSEN(zephyr_code_partition))
40+
#else
41+
#define FLASH_LOAD_OFFSET CONFIG_FLASH_LOAD_OFFSET
42+
#define FLASH_LOAD_SIZE CONFIG_FLASH_LOAD_SIZE
43+
#endif
44+
3745
#if !defined(CONFIG_XIP) && (CONFIG_FLASH_SIZE == 0)
3846
#define ROM_ADDR RAM_ADDR
3947
#else
40-
#define ROM_ADDR (CONFIG_FLASH_BASE_ADDRESS + CONFIG_FLASH_LOAD_OFFSET)
48+
#define ROM_ADDR (CONFIG_FLASH_BASE_ADDRESS + FLASH_LOAD_OFFSET)
4149
#endif
4250

4351
#if defined(CONFIG_ROM_END_OFFSET)
@@ -46,10 +54,10 @@
4654
#define ROM_END_OFFSET 0
4755
#endif
4856

49-
#if CONFIG_FLASH_LOAD_SIZE > 0
50-
#define ROM_SIZE (CONFIG_FLASH_LOAD_SIZE - ROM_END_OFFSET)
57+
#if FLASH_LOAD_SIZE > 0
58+
#define ROM_SIZE (FLASH_LOAD_SIZE - ROM_END_OFFSET)
5159
#else
52-
#define ROM_SIZE (CONFIG_FLASH_SIZE*1K - CONFIG_FLASH_LOAD_OFFSET - ROM_END_OFFSET)
60+
#define ROM_SIZE (CONFIG_FLASH_SIZE*1K - FLASH_LOAD_OFFSET - ROM_END_OFFSET)
5361
#endif
5462

5563
#define RAM_SIZE (CONFIG_SRAM_SIZE * 1K)

include/zephyr/arch/arm/cortex_m/scripts/linker.ld

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,18 @@ _image_1_primary_slot_id = PM_S1_ID;
6767

6868
#else /* ! USE_PARTITION_MANAGER */
6969

70+
#if CONFIG_USE_DT_CODE_PARTITION
71+
#define FLASH_LOAD_OFFSET DT_REG_ADDR(DT_CHOSEN(zephyr_code_partition))
72+
#define FLASH_LOAD_SIZE DT_REG_SIZE(DT_CHOSEN(zephyr_code_partition))
73+
#else
74+
#define FLASH_LOAD_OFFSET CONFIG_FLASH_LOAD_OFFSET
75+
#define FLASH_LOAD_SIZE CONFIG_FLASH_LOAD_SIZE
76+
#endif
77+
7078
#if !defined(CONFIG_XIP) && (CONFIG_FLASH_SIZE == 0)
7179
#define ROM_ADDR RAM_ADDR
7280
#else
73-
#define ROM_ADDR (CONFIG_FLASH_BASE_ADDRESS + CONFIG_FLASH_LOAD_OFFSET)
81+
#define ROM_ADDR (CONFIG_FLASH_BASE_ADDRESS + FLASH_LOAD_OFFSET)
7482
#endif
7583

7684
#if defined(CONFIG_ROM_END_OFFSET)
@@ -79,10 +87,10 @@ _image_1_primary_slot_id = PM_S1_ID;
7987
#define ROM_END_OFFSET 0
8088
#endif
8189

82-
#if CONFIG_FLASH_LOAD_SIZE > 0
83-
#define ROM_SIZE (CONFIG_FLASH_LOAD_SIZE - ROM_END_OFFSET)
90+
#if FLASH_LOAD_SIZE > 0
91+
#define ROM_SIZE (FLASH_LOAD_SIZE - ROM_END_OFFSET)
8492
#else
85-
#define ROM_SIZE (CONFIG_FLASH_SIZE * 1024 - CONFIG_FLASH_LOAD_OFFSET - ROM_END_OFFSET)
93+
#define ROM_SIZE (CONFIG_FLASH_SIZE * 1024 - FLASH_LOAD_OFFSET - ROM_END_OFFSET)
8694
#endif
8795

8896
#define RAM_SIZE (CONFIG_SRAM_SIZE * 1K)

include/zephyr/arch/arm64/scripts/linker.ld

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,18 @@
2626
#endif
2727
#define RAMABLE_REGION RAM
2828

29+
#if CONFIG_USE_DT_CODE_PARTITION
30+
#define FLASH_LOAD_OFFSET DT_REG_ADDR(DT_CHOSEN(zephyr_code_partition))
31+
#define FLASH_LOAD_SIZE DT_REG_SIZE(DT_CHOSEN(zephyr_code_partition))
32+
#else
33+
#define FLASH_LOAD_OFFSET CONFIG_FLASH_LOAD_OFFSET
34+
#define FLASH_LOAD_SIZE CONFIG_FLASH_LOAD_SIZE
35+
#endif
36+
2937
#if !defined(CONFIG_XIP) && (CONFIG_FLASH_SIZE == 0)
3038
#define ROM_ADDR RAM_ADDR
3139
#else
32-
#define ROM_ADDR (CONFIG_FLASH_BASE_ADDRESS + CONFIG_FLASH_LOAD_OFFSET)
40+
#define ROM_ADDR (CONFIG_FLASH_BASE_ADDRESS + FLASH_LOAD_OFFSET)
3341
#endif
3442

3543
#if defined(CONFIG_ROM_END_OFFSET)
@@ -38,10 +46,10 @@
3846
#define ROM_END_OFFSET 0
3947
#endif
4048

41-
#if CONFIG_FLASH_LOAD_SIZE > 0
42-
#define ROM_SIZE (CONFIG_FLASH_LOAD_SIZE - ROM_END_OFFSET)
49+
#if FLASH_LOAD_SIZE > 0
50+
#define ROM_SIZE (FLASH_LOAD_SIZE - ROM_END_OFFSET)
4351
#else
44-
#define ROM_SIZE (CONFIG_FLASH_SIZE * 1K - CONFIG_FLASH_LOAD_OFFSET - ROM_END_OFFSET)
52+
#define ROM_SIZE (CONFIG_FLASH_SIZE * 1K - FLASH_LOAD_OFFSET - ROM_END_OFFSET)
4553
#endif
4654

4755
#define RAM_SIZE (CONFIG_SRAM_SIZE * 1K)

include/zephyr/arch/riscv/common/linker.ld

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,22 @@
3535
#define ROM_END_OFFSET 0
3636
#endif
3737

38+
#if CONFIG_USE_DT_CODE_PARTITION
39+
#define FLASH_LOAD_OFFSET DT_REG_ADDR(DT_CHOSEN(zephyr_code_partition))
40+
#define FLASH_LOAD_SIZE DT_REG_SIZE(DT_CHOSEN(zephyr_code_partition))
41+
#else
3842
#if defined(CONFIG_FLASH_LOAD_OFFSET)
3943
#define FLASH_LOAD_OFFSET CONFIG_FLASH_LOAD_OFFSET
4044
#else
4145
#define FLASH_LOAD_OFFSET 0
4246
#endif
47+
#define FLASH_LOAD_SIZE CONFIG_FLASH_LOAD_SIZE
48+
#endif
4349

4450
#ifdef CONFIG_XIP
4551

46-
#if CONFIG_FLASH_LOAD_SIZE > 0
47-
#define ROM_SIZE (CONFIG_FLASH_LOAD_SIZE - ROM_END_OFFSET)
52+
#if FLASH_LOAD_SIZE > 0
53+
#define ROM_SIZE (FLASH_LOAD_SIZE - ROM_END_OFFSET)
4854
#endif
4955

5056
#if DT_NODE_HAS_COMPAT_STATUS(DT_CHOSEN(zephyr_flash), soc_nv_flash, okay)

scripts/west_commands/runners/core.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import errno
1717
import logging
1818
import os
19+
import pathlib
20+
import pickle
1921
import platform
2022
import re
2123
import selectors
@@ -715,13 +717,45 @@ def get_flash_address(args: argparse.Namespace,
715717
else:
716718
return default
717719

720+
@staticmethod
721+
def get_chosen_code_partition_offset(build_dir: str):
722+
'''Get the offset corresponding to the zephyr,code-partition.'''
723+
b = pathlib.Path(build_dir)
724+
edt_pickle = b / 'zephyr' / 'edt.pickle'
725+
if not edt_pickle.is_file():
726+
raise RuntimeError('cannot load devicetree; expected to find:' +
727+
str(edt_pickle))
728+
729+
# Load the devicetree.
730+
try:
731+
with open(edt_pickle, 'rb') as f:
732+
edt = pickle.load(f)
733+
except ModuleNotFoundError as err:
734+
raise RuntimeError('could not load devicetree, something may be'
735+
'wrong with the python environment') from err
736+
737+
# Find the zephyr,code-partition node.
738+
node = edt.chosen_node('zephyr,code-partition')
739+
if node is not None:
740+
return node.regs[0].addr
741+
742+
return None
743+
718744
@staticmethod
719745
def flash_address_from_build_conf(build_conf: BuildConfiguration):
720-
'''If CONFIG_HAS_FLASH_LOAD_OFFSET is n in build_conf,
746+
'''If CONFIG_USE_DT_CODE_PARTITION return zephyr,code-partition
747+
offset + CONFIG_FLASH_BASE_ADDRESS.
748+
If CONFIG_HAS_FLASH_LOAD_OFFSET is n in build_conf,
721749
return the CONFIG_FLASH_BASE_ADDRESS value. Otherwise, return
722750
CONFIG_FLASH_BASE_ADDRESS + CONFIG_FLASH_LOAD_OFFSET.
723751
'''
724-
if build_conf.getboolean('CONFIG_HAS_FLASH_LOAD_OFFSET'):
752+
if build_conf.getboolean('CONFIG_USE_DT_CODE_PARTITION'):
753+
offset = ZephyrBinaryRunner.get_chosen_code_partition_offset(build_conf.build_dir)
754+
if offset is None:
755+
raise RuntimeError('The device tree zephyr,code-partition chosen'
756+
' node must be defined.')
757+
return build_conf['CONFIG_FLASH_BASE_ADDRESS'] + offset
758+
elif build_conf.getboolean('CONFIG_HAS_FLASH_LOAD_OFFSET'):
725759
return (build_conf['CONFIG_FLASH_BASE_ADDRESS'] +
726760
build_conf['CONFIG_FLASH_LOAD_OFFSET'])
727761
else:

subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,20 @@ BUILD_ASSERT(PM_MCUBOOT_PAD_SIZE == PM_MCUBOOT_SECONDARY_PAD_SIZE);
6060
(FIXED_PARTITION_OFFSET(label) == (PM_ADDRESS - PM_ADDRESS_OFFSET))
6161

6262
#else /* ! USE_PARTITION_MANAGER */
63-
#ifndef CONFIG_FLASH_LOAD_OFFSET
63+
64+
#ifdef CONFIG_USE_DT_CODE_PARTITION
65+
#define FLASH_LOAD_OFFSET DT_REG_ADDR(DT_CHOSEN(zephyr_code_partition))
66+
#elif defined(CONFIG_FLASH_LOAD_OFFSET)
67+
#define FLASH_LOAD_OFFSET CONFIG_FLASH_LOAD_OFFSET
68+
#endif
69+
70+
#ifndef FLASH_LOAD_OFFSET
6471
#error MCUmgr requires application to be built with CONFIG_FLASH_LOAD_OFFSET set \
6572
to be able to figure out application running slot.
6673
#endif
6774

68-
#define FIXED_PARTITION_IS_RUNNING_APP_PARTITION(label) \
69-
(FIXED_PARTITION_OFFSET(label) == CONFIG_FLASH_LOAD_OFFSET)
70-
#endif /* USE_PARTITION_MANAGER */
75+
#define FIXED_PARTITION_IS_RUNNING_APP_PARTITION(label) \
76+
(FIXED_PARTITION_OFFSET(label) == FLASH_LOAD_OFFSET)
7177

7278
BUILD_ASSERT(sizeof(struct image_header) == IMAGE_HEADER_SIZE,
7379
"struct image_header not required size");

0 commit comments

Comments
 (0)