Skip to content

Commit d13ceb9

Browse files
committed
soc: arm: rpi_pico: Add basic support for binary info feature
Enable embedding binary info to flash. As a default, information collects from the build configuration. It can override by the Kconfig configurations, such as ``` CONFIG_RP2_BINARY_INFO_PROGRAM_NAME_OVERRIDE=y CONFIG_RP2_BINARY_INFO_PROGRAM_NAME="my program name" ``` Signed-off-by: TOKITA Hiroshi <[email protected]>
1 parent 13d0f40 commit d13ceb9

File tree

6 files changed

+366
-0
lines changed

6 files changed

+366
-0
lines changed

soc/raspberrypi/rpi_pico/common/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,16 @@ zephyr_sources(
88
)
99

1010
zephyr_sources_ifdef(CONFIG_RPI_PICO_ROM_BOOTLOADER rom_bootloader.c)
11+
12+
zephyr_library_sources_ifdef(CONFIG_RPI_PICO_BINARY_INFO
13+
binary_info.c
14+
binary_info_header.S
15+
)
16+
17+
zephyr_linker_sources_ifdef(CONFIG_RPI_PICO_BINARY_INFO
18+
ROM_START SORT_KEY 0x0binary_info_header binary_info_header.ld
19+
)
20+
21+
zephyr_linker_sources_ifdef(CONFIG_RPI_PICO_BINARY_INFO
22+
RODATA binary_info.ld
23+
)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright (c) 2024 TOKITA Hiroshi
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config RPI_PICO_BINARY_INFO
5+
bool "Generate RaspberryPi Pico binary info"
6+
default y
7+
help
8+
Binary info is able to embed machine readable information with the binary in FLASH.
9+
It can read with picotool(https://github.com/raspberrypi/picotool).
10+
11+
if RPI_PICO_BINARY_INFO
12+
13+
config RPI_PICO_BINARY_INFO_PROGRAM_NAME_ENABLE
14+
bool "Override program name in binary info"
15+
16+
config RPI_PICO_BINARY_INFO_PROGRAM_NAME
17+
string "Override string for program name in binary info"
18+
depends on RPI_PICO_BINARY_INFO_PROGRAM_NAME_ENABLE
19+
20+
config RPI_PICO_BINARY_INFO_PROGRAM_URL_ENABLE
21+
bool "Override program url in binary info"
22+
23+
config RPI_PICO_BINARY_INFO_PROGRAM_URL
24+
string "String for program description in binary info"
25+
depends on RPI_PICO_BINARY_INFO_PROGRAM_URL_ENABLE
26+
27+
config RPI_PICO_BINARY_INFO_PROGRAM_DESCRIPTION_ENABLE
28+
bool "Override program descrpition in binary info"
29+
30+
config RPI_PICO_BINARY_INFO_PROGRAM_DESCRIPTION
31+
string "String for program description in binary info"
32+
depends on RPI_PICO_BINARY_INFO_PROGRAM_DESCRIPTION_ENABLE
33+
34+
config RPI_PICO_BINARY_INFO_PROGRAM_BUILD_DATE_ENABLE
35+
bool "Override build date in binary info"
36+
37+
config RPI_PICO_BINARY_INFO_PROGRAM_BUILD_DATE
38+
string "Override string for build date in binary info"
39+
depends on RPI_PICO_BINARY_INFO_PROGRAM_BUILD_DATE_ENABLE
40+
41+
config RPI_PICO_BINARY_INFO_PROGRAM_VERSION_STRING_ENABLE
42+
bool "Override program version in binary info"
43+
default y
44+
45+
config RPI_PICO_BINARY_INFO_SDK_VERSION_STRING_ENABLE
46+
bool "Override sdk version in binary info"
47+
default y
48+
49+
config RPI_PICO_BINARY_INFO_PICO_BOARD_ENABLE
50+
bool "Override board in binary info"
51+
default y
52+
53+
config RPI_PICO_BINARY_INFO_ATTRIBUTE_BUILD_TYPE_ENABLE
54+
bool "Override board in binary info"
55+
default y
56+
57+
config RPI_PICO_BINARY_INFO_BOOT_STAGE2_NAME_ENABLE
58+
bool "Override board in binary info"
59+
default y
60+
61+
config RPI_PICO_BINARY_INFO_PINS_WITH_FUNC_ENABLE
62+
bool "Override board in binary info"
63+
default y
64+
65+
endif
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
/*
2+
* Copyright (c) 2023 TOKITA Hiroshi <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/devicetree.h>
9+
#include <zephyr/sys/util_macro.h>
10+
11+
#include <zephyr/dt-bindings/pinctrl/rpi-pico-pinctrl-common.h>
12+
13+
#include <soc.h>
14+
15+
#include <boot_stage2/config.h>
16+
#include <pico/binary_info.h>
17+
18+
#include <version.h>
19+
#ifdef HAS_APP_VERSION
20+
#include <app_version.h>
21+
#endif
22+
23+
/* utils for pin encoding calcluation */
24+
25+
#ifdef CONFIG_SOC_RP2040
26+
#define ENCODE_PIN(n, idx, offset) ((uint32_t)PIN_NUM(n, idx) << (2 + (idx + offset + 1) * 5))
27+
#define MAX_PIN_ENTRY 4
28+
#define BI_ENCODE_PINS_WITH_FUNC __bi_encoded_pins_with_func
29+
#else
30+
#define ENCODE_PIN(n, idx, offset) ((uint64_t)PIN_NUM(n, idx) << ((idx + offset + 1) * 8))
31+
#define MAX_PIN_ENTRY 6
32+
#define BI_ENCODE_PINS_WITH_FUNC __bi_encoded_pins_64_with_func
33+
#endif
34+
35+
#define PIN_NUM(n, idx) ((DT_PROP_BY_IDX(n, pinmux, idx) >> RP2_PIN_NUM_POS) & RP2_PIN_NUM_MASK)
36+
#define PIN_FUNC(n, idx) (DT_PROP_BY_IDX(n, pinmux, idx) & RP2_ALT_FUNC_MASK)
37+
38+
/* Pin counts and offsets for groups */
39+
40+
#define PINCTRL_GROUP_PIN_COUNT(node_id) \
41+
COND_CODE_1(DT_NODE_HAS_PROP(node_id, pinmux), (DT_PROP_LEN(node_id, pinmux)), (0))
42+
43+
#define PINCTRL_OFFSET_TERM(i, node_id) +PINCTRL_GROUP_PIN_COUNT(DT_CHILD_BY_IDX(node_id, i))
44+
#define PINCTRL_GROUP_OFFSET(node_id, idx) (0 LISTIFY(idx, PINCTRL_OFFSET_TERM, (), node_id))
45+
#define PINCTRL_TOTAL_PINS(node_id) PINCTRL_GROUP_OFFSET(node_id, DT_CHILD_NUM(node_id))
46+
47+
/* Iterate groups and subgroups */
48+
49+
#define EACH_PINCTRL_SUBGROUP(n, fn, sep, ...) \
50+
DT_FOREACH_PROP_ELEM_SEP_VARGS(n, pinmux, fn, sep, __VA_ARGS__)
51+
#define EACH_PINCTRL_GROUP(n, sep, fn, ...) \
52+
DT_FOREACH_CHILD_SEP_VARGS(n, EACH_PINCTRL_SUBGROUP, sep, fn, sep, __VA_ARGS__)
53+
54+
/* Encode the pins in a group */
55+
56+
#define IS_LAST_PIN(end, idx, off) (((idx) + (off) + 1) == (end))
57+
#define PIN_TERMINATE(n, p, i, offset, end) \
58+
(IS_LAST_PIN(end, i, offset) ? ENCODE_PIN(n, i, (offset) + 1) : 0)
59+
#define PIN_ENTRY(n, p, i, off) (DT_PROP_HAS_IDX(n, p, i) ? ENCODE_PIN(n, i, off) : 0)
60+
#define ENCODE_EACH_PIN(n, p, i, end) \
61+
PIN_ENTRY(n, p, i, PINCTRL_GROUP_OFFSET(DT_PARENT(n), DT_NODE_CHILD_IDX(n))) | \
62+
PIN_TERMINATE(n, p, i, PINCTRL_GROUP_OFFSET(DT_PARENT(n), DT_NODE_CHILD_IDX(n)), \
63+
end)
64+
#define ENCODE_GROUP_PINS(n) (EACH_PINCTRL_GROUP(n, (|), ENCODE_EACH_PIN, PINCTRL_TOTAL_PINS(n)))
65+
66+
/* Get group-wide pin functions */
67+
68+
#define EACH_PIN_FUNC(n, p, i, _) PIN_FUNC(n, i)
69+
#define GROUP_PIN_FUNC(n) (EACH_PINCTRL_GROUP(n, (|), EACH_PIN_FUNC))
70+
71+
/* Check if pin functions are all equal within a group */
72+
73+
#define EACH_PIN_FUNC_IS(n, p, i, func) (PIN_FUNC(n, i) == func)
74+
#define ALL_PIN_FUNC_IS(n, pinfunc) (EACH_PINCTRL_GROUP(n, (&&), EACH_PIN_FUNC_IS, pinfunc))
75+
76+
#define BI_PINS_FROM_PINCTRL_GROUP_(n) \
77+
BUILD_ASSERT(PINCTRL_TOTAL_PINS(n) > 0, "Group must contain at least one pin"); \
78+
BUILD_ASSERT(PINCTRL_TOTAL_PINS(n) <= MAX_PIN_ENTRY, "Too many pin in group"); \
79+
BUILD_ASSERT(ALL_PIN_FUNC_IS(n, GROUP_PIN_FUNC(n)), \
80+
"Group must contain only single function type"); \
81+
bi_decl(BI_ENCODE_PINS_WITH_FUNC(BI_PINS_ENCODING_MULTI | (GROUP_PIN_FUNC(n) << 3) | \
82+
ENCODE_GROUP_PINS(n)))
83+
84+
#define BI_PINS_FROM_PINCTRL_GROUP(n, idx) BI_PINS_FROM_PINCTRL_GROUP_(DT_CHILD_BY_IDX(n, idx))
85+
86+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_NAME
87+
#define BI_PROGRAM_NAME CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_NAME
88+
#endif
89+
90+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_DESCRIPTION
91+
#define BI_PROGRAM_DESCRIPTION CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_DESCRIPTION
92+
#endif
93+
94+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_URL
95+
#define BI_PROGRAM_URL CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_URL
96+
#endif
97+
98+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_BUILD_DATE
99+
#define BI_PROGRAM_BUILD_DATE CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_BUILD_DATE
100+
#else
101+
#define BI_PROGRAM_BUILD_DATE __DATE__
102+
#endif
103+
104+
extern uint32_t __rom_region_end;
105+
bi_decl(bi_binary_end((intptr_t)&__rom_region_end));
106+
107+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_NAME_ENABLE
108+
bi_decl(bi_program_name((uint32_t)BI_PROGRAM_NAME));
109+
#endif
110+
111+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PICO_BOARD_ENABLE
112+
bi_decl(bi_string(BINARY_INFO_TAG_RASPBERRY_PI, BINARY_INFO_ID_RP_PICO_BOARD,
113+
(uint32_t)CONFIG_BOARD));
114+
#endif
115+
116+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_SDK_VERSION_STRING_ENABLE
117+
bi_decl(bi_string(BINARY_INFO_TAG_RASPBERRY_PI, BINARY_INFO_ID_RP_SDK_VERSION,
118+
(uint32_t)"zephyr-" STRINGIFY(BUILD_VERSION)));
119+
#endif
120+
121+
#if defined(CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_VERSION_STRING_ENABLE) && defined(HAS_APP_VERSION)
122+
bi_decl(bi_program_version_string((uint32_t)APP_VERSION_EXTENDED_STRING));
123+
#endif
124+
125+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_DESCRIPTION_ENABLE
126+
bi_decl(bi_program_description((uint32_t)BI_PROGRAM_DESCRIPTION));
127+
#endif
128+
129+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_URL_ENABLE
130+
bi_decl(bi_program_url((uint32_t)BI_PROGRAM_URL));
131+
#endif
132+
133+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_BUILD_DATE_ENABLE
134+
bi_decl(bi_program_build_date_string((uint32_t)BI_PROGRAM_BUILD_DATE));
135+
#endif
136+
137+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_BOOT_STAGE2_NAME_ENABLE
138+
bi_decl(bi_string(BINARY_INFO_TAG_RASPBERRY_PI, BINARY_INFO_ID_RP_BOOT2_NAME,
139+
(uint32_t)PICO_BOOT_STAGE2_NAME));
140+
#endif
141+
142+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_ATTRIBUTE_BUILD_TYPE_ENABLE
143+
#ifdef CONFIG_DEBUG
144+
bi_decl(bi_program_build_attribute((uint32_t)"Debug"));
145+
#else
146+
bi_decl(bi_program_build_attribute((uint32_t)"Release"));
147+
#endif
148+
#endif
149+
150+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PINS_WITH_FUNC_ENABLE
151+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 0
152+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 0);
153+
#endif
154+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 1
155+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 1);
156+
#endif
157+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 2
158+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 2);
159+
#endif
160+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 3
161+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 3);
162+
#endif
163+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 4
164+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 4);
165+
#endif
166+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 5
167+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 5);
168+
#endif
169+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 6
170+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 6);
171+
#endif
172+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 7
173+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 7);
174+
#endif
175+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 8
176+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 8);
177+
#endif
178+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 9
179+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 9);
180+
#endif
181+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 10
182+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 10);
183+
#endif
184+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 11
185+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 11);
186+
#endif
187+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 12
188+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 12);
189+
#endif
190+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 13
191+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 13);
192+
#endif
193+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 14
194+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 14);
195+
#endif
196+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 15
197+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 15);
198+
#endif
199+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 16
200+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 16);
201+
#endif
202+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 17
203+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 17);
204+
#endif
205+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 18
206+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 18);
207+
#endif
208+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 19
209+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 19);
210+
#endif
211+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 20
212+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 20);
213+
#endif
214+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 21
215+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 21);
216+
#endif
217+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 22
218+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 22);
219+
#endif
220+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 23
221+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 23);
222+
#endif
223+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 24
224+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 24);
225+
#endif
226+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 25
227+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 25);
228+
#endif
229+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 26
230+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 26);
231+
#endif
232+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 27
233+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 27);
234+
#endif
235+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 28
236+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 28);
237+
#endif
238+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 29
239+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 29);
240+
#endif
241+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 30
242+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 30);
243+
#endif
244+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 31
245+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 31);
246+
#endif
247+
#endif
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright (c) 2023 TOKITA Hiroshi <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
. = ALIGN(4);
8+
__binary_info_start = .;
9+
KEEP(*(.binary_info.keep.*))
10+
*(.binary_info.*)
11+
__binary_info_end = .;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2023 TOKITA Hiroshi <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "pico/binary_info/defs.h"
8+
9+
.section .binary_info_header
10+
11+
/* binary_info_header */
12+
binary_info_header:
13+
.word BINARY_INFO_MARKER_START
14+
.word __binary_info_start
15+
.word __binary_info_end
16+
.word data_cpy_table /* we may need to decode pointers that are in RAM at runtime.*/
17+
.word BINARY_INFO_MARKER_END
18+
19+
.align 2
20+
21+
/* data_cpy_table */
22+
data_cpy_table:
23+
.word 0 /* centinel */
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Copyright (c) 2023 TOKITA Hiroshi <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
KEEP (*(.binary_info_header))

0 commit comments

Comments
 (0)