Skip to content

Commit c20f3c8

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 47d4811 commit c20f3c8

File tree

6 files changed

+369
-0
lines changed

6 files changed

+369
-0
lines changed

soc/raspberrypi/rpi_pico/common/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,16 @@ zephyr_include_directories(.)
66
zephyr_sources(
77
soc.c
88
)
9+
10+
zephyr_library_sources_ifdef(CONFIG_RPI_PICO_BINARY_INFO
11+
binary_info.c
12+
binary_info_header.S
13+
)
14+
15+
zephyr_linker_sources_ifdef(CONFIG_RPI_PICO_BINARY_INFO
16+
ROM_START SORT_KEY 0x0binary_info_header binary_info_header.ld
17+
)
18+
19+
zephyr_linker_sources_ifdef(CONFIG_RPI_PICO_BINARY_INFO
20+
RODATA binary_info.ld
21+
)
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: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
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 PIN_ENCODE(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 PIN_ENCODE(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 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 GROUP_OFFSET_STEP(i, node_id) + GROUP_PIN_COUNT(DT_CHILD_BY_IDX(node_id, i))
44+
45+
#define GROUP_OFFSET(node_id, idx) (0 LISTIFY(idx, GROUP_OFFSET_STEP, (), node_id))
46+
47+
#define GROUP_PIN_COUNT_PLUS(node_id) + GROUP_PIN_COUNT(node_id)
48+
49+
#define COUNT_ALL_PINS(node_id) (0 DT_FOREACH_CHILD(node_id, GROUP_PIN_COUNT_PLUS))
50+
51+
/* Iterate groups and subgroups */
52+
53+
#define EACH_PINCTRL_SUBGROUP(n, fn, sep, ...) \
54+
DT_FOREACH_PROP_ELEM_SEP_VARGS(n, pinmux, fn, sep, __VA_ARGS__)
55+
#define EACH_PINCTRL_GROUP(n, sep, fn, ...) \
56+
DT_FOREACH_CHILD_SEP_VARGS(n, EACH_PINCTRL_SUBGROUP, sep, fn, sep, __VA_ARGS__)
57+
58+
/* Encode the pins in a group */
59+
60+
#define IS_LAST_PIN(end, idx, off) (((idx) + (off) + 1) == (end))
61+
#define PIN_TERMINATE(n, p, i, offset, end) \
62+
(IS_LAST_PIN(end, i, offset) ? PIN_ENCODE(n, i, (offset) + 1) : 0)
63+
#define PIN_ENTRY(n, p, i, off) (DT_PROP_HAS_IDX(n, p, i) ? PIN_ENCODE(n, i, off) : 0)
64+
#define ENCODE_EACH_PIN(n, p, i, end) \
65+
PIN_ENTRY(n, p, i, GROUP_OFFSET(DT_PARENT(n), DT_NODE_CHILD_IDX(n))) | \
66+
PIN_TERMINATE(n, p, i, GROUP_OFFSET(DT_PARENT(n), DT_NODE_CHILD_IDX(n)), end)
67+
#define ENCODE_GROUP_PINS(n) (EACH_PINCTRL_GROUP(n, (|), ENCODE_EACH_PIN, COUNT_ALL_PINS(n)))
68+
69+
/* Get group-wide pin functions */
70+
71+
#define EACH_PIN_FUNC(n, p, i, _) PIN_FUNC(n, i)
72+
#define GROUP_PIN_FUNC(n) (EACH_PINCTRL_GROUP(n, (|), EACH_PIN_FUNC))
73+
74+
/* Check if pin functions are all equal within a group */
75+
76+
#define EACH_PIN_FUNC_IS(n, p, i, func) (PIN_FUNC(n, i) == func)
77+
#define ALL_PIN_FUNC_IS(n, pinfunc) (EACH_PINCTRL_GROUP(n, (&&), EACH_PIN_FUNC_IS, pinfunc))
78+
79+
#define BI_PINS_FROM_PINCTRL_GROUP_(n) \
80+
BUILD_ASSERT(COUNT_ALL_PINS(n) > 0, "Group must contain at least one pin"); \
81+
BUILD_ASSERT(COUNT_ALL_PINS(n) <= MAX_PIN_ENTRY, "Too many pin in group"); \
82+
BUILD_ASSERT(ALL_PIN_FUNC_IS(n, GROUP_PIN_FUNC(n)), \
83+
"Group must contain only single function type"); \
84+
bi_decl(BI_ENCODE_PINS_WITH_FUNC(BI_PINS_ENCODING_MULTI | \
85+
(GROUP_PIN_FUNC(n) << 3) | ENCODE_GROUP_PINS(n)))
86+
87+
#define BI_PINS_FROM_PINCTRL_GROUP(n, idx) BI_PINS_FROM_PINCTRL_GROUP_(DT_CHILD_BY_IDX(n, idx))
88+
89+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_NAME
90+
#define BI_PROGRAM_NAME CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_NAME
91+
#endif
92+
93+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_DESCRIPTION
94+
#define BI_PROGRAM_DESCRIPTION CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_DESCRIPTION
95+
#endif
96+
97+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_URL
98+
#define BI_PROGRAM_URL CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_URL
99+
#endif
100+
101+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_BUILD_DATE
102+
#define BI_PROGRAM_BUILD_DATE CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_BUILD_DATE
103+
#else
104+
#define BI_PROGRAM_BUILD_DATE __DATE__
105+
#endif
106+
107+
extern uint32_t __rom_region_end;
108+
bi_decl(bi_binary_end((intptr_t)&__rom_region_end));
109+
110+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_NAME_ENABLE
111+
bi_decl(bi_program_name((uint32_t)BI_PROGRAM_NAME));
112+
#endif
113+
114+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PICO_BOARD_ENABLE
115+
bi_decl(bi_string(BINARY_INFO_TAG_RASPBERRY_PI, BINARY_INFO_ID_RP_PICO_BOARD,
116+
(uint32_t)CONFIG_BOARD));
117+
#endif
118+
119+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_SDK_VERSION_STRING_ENABLE
120+
bi_decl(bi_string(BINARY_INFO_TAG_RASPBERRY_PI, BINARY_INFO_ID_RP_SDK_VERSION,
121+
(uint32_t)"zephyr-" STRINGIFY(BUILD_VERSION)));
122+
#endif
123+
124+
#if defined(CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_VERSION_STRING_ENABLE) && defined(HAS_APP_VERSION)
125+
bi_decl(bi_program_version_string((uint32_t)APP_VERSION_EXTENDED_STRING));
126+
#endif
127+
128+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_DESCRIPTION_ENABLE
129+
bi_decl(bi_program_description((uint32_t)BI_PROGRAM_DESCRIPTION));
130+
#endif
131+
132+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_URL_ENABLE
133+
bi_decl(bi_program_url((uint32_t)BI_PROGRAM_URL));
134+
#endif
135+
136+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_BUILD_DATE_ENABLE
137+
bi_decl(bi_program_build_date_string((uint32_t)BI_PROGRAM_BUILD_DATE));
138+
#endif
139+
140+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_BOOT_STAGE2_NAME_ENABLE
141+
bi_decl(bi_string(BINARY_INFO_TAG_RASPBERRY_PI, BINARY_INFO_ID_RP_BOOT2_NAME,
142+
(uint32_t)PICO_BOOT_STAGE2_NAME));
143+
#endif
144+
145+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_ATTRIBUTE_BUILD_TYPE_ENABLE
146+
#ifdef CONFIG_DEBUG
147+
bi_decl(bi_program_build_attribute((uint32_t)"Debug"));
148+
#else
149+
bi_decl(bi_program_build_attribute((uint32_t)"Release"));
150+
#endif
151+
#endif
152+
153+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PINS_WITH_FUNC_ENABLE
154+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 0
155+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 0);
156+
#endif
157+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 1
158+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 1);
159+
#endif
160+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 2
161+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 2);
162+
#endif
163+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 3
164+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 3);
165+
#endif
166+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 4
167+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 4);
168+
#endif
169+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 5
170+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 5);
171+
#endif
172+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 6
173+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 6);
174+
#endif
175+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 7
176+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 7);
177+
#endif
178+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 8
179+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 8);
180+
#endif
181+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 9
182+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 9);
183+
#endif
184+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 10
185+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 10);
186+
#endif
187+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 11
188+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 11);
189+
#endif
190+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 12
191+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 12);
192+
#endif
193+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 13
194+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 13);
195+
#endif
196+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 14
197+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 14);
198+
#endif
199+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 15
200+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 15);
201+
#endif
202+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 16
203+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 16);
204+
#endif
205+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 17
206+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 17);
207+
#endif
208+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 18
209+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 18);
210+
#endif
211+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 19
212+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 19);
213+
#endif
214+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 20
215+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 20);
216+
#endif
217+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 21
218+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 21);
219+
#endif
220+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 22
221+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 22);
222+
#endif
223+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 23
224+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 23);
225+
#endif
226+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 24
227+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 24);
228+
#endif
229+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 25
230+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 25);
231+
#endif
232+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 26
233+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 26);
234+
#endif
235+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 27
236+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 27);
237+
#endif
238+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 28
239+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 28);
240+
#endif
241+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 29
242+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 29);
243+
#endif
244+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 30
245+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 30);
246+
#endif
247+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 31
248+
BI_PINS_FROM_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 31);
249+
#endif
250+
#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)