Skip to content

Commit 0a1d7b5

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 0a1d7b5

File tree

6 files changed

+368
-0
lines changed

6 files changed

+368
-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: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
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, off) ((uint32_t)PIN_NUM(n, idx) << (2 + (idx + off + 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, off) ((uint64_t)PIN_NUM(n, idx) << ((idx + off + 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 offs 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 FOREACH_PINCTRL_SUBGROUP(n, fn, sep, ...) \
50+
DT_FOREACH_PROP_ELEM_SEP_VARGS(n, pinmux, fn, sep, __VA_ARGS__)
51+
#define FOREACH_PINCTRL_GROUP(n, sep, fn, ...) \
52+
DT_FOREACH_CHILD_SEP_VARGS(n, FOREACH_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, off, end) \
58+
(IS_LAST_PIN(end, i, off) ? ENCODE_PIN(n, i, (off) + 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) (FOREACH_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) (FOREACH_PINCTRL_GROUP(n, (|), EACH_PIN_FUNC))
70+
#define GROUP_PIN_HEADER(n) (BI_PINS_ENCODING_MULTI | (GROUP_PIN_FUNC(n) << 3))
71+
72+
/* Check if pin functions are all equal within a group */
73+
74+
#define EACH_PIN_FUNC_IS(n, p, i, func) (PIN_FUNC(n, i) == func)
75+
#define ALL_PINS_FUNC_IS(n, pinfunc) (FOREACH_PINCTRL_GROUP(n, (&&), EACH_PIN_FUNC_IS, pinfunc))
76+
77+
#define PININFO_PINCTRL_GROUP_(n) \
78+
BUILD_ASSERT(PINCTRL_TOTAL_PINS(n) > 0, "Group must contain at least one pin"); \
79+
BUILD_ASSERT(PINCTRL_TOTAL_PINS(n) <= MAX_PIN_ENTRY, "Too many pin in group"); \
80+
BUILD_ASSERT(ALL_PINS_FUNC_IS(n, GROUP_PIN_FUNC(n)), "All pins func must same in group"); \
81+
bi_decl(BI_ENCODE_PINS_WITH_FUNC(GROUP_PIN_HEADER(n) | ENCODE_GROUP_PINS(n)))
82+
83+
#define PININFO_PINCTRL_GROUP_SELECT(child, idx) \
84+
COND_CODE_1(IS_EQ(DT_NODE_CHILD_IDX(child), idx), (PININFO_PINCTRL_GROUP_(child)), ())
85+
86+
#define PININFO_PINCTRL_GROUP(n, idx) DT_FOREACH_CHILD_VARGS(n, PININFO_PINCTRL_GROUP_SELECT, idx)
87+
88+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_NAME
89+
#define BI_PROGRAM_NAME CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_NAME
90+
#endif
91+
92+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_DESCRIPTION
93+
#define BI_PROGRAM_DESCRIPTION CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_DESCRIPTION
94+
#endif
95+
96+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_URL
97+
#define BI_PROGRAM_URL CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_URL
98+
#endif
99+
100+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_BUILD_DATE
101+
#define BI_PROGRAM_BUILD_DATE CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_BUILD_DATE
102+
#else
103+
#define BI_PROGRAM_BUILD_DATE __DATE__
104+
#endif
105+
106+
extern uint32_t __rom_region_end;
107+
bi_decl(bi_binary_end((intptr_t)&__rom_region_end));
108+
109+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_NAME_ENABLE
110+
bi_decl(bi_program_name((uint32_t)BI_PROGRAM_NAME));
111+
#endif
112+
113+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PICO_BOARD_ENABLE
114+
bi_decl(bi_string(BINARY_INFO_TAG_RASPBERRY_PI, BINARY_INFO_ID_RP_PICO_BOARD,
115+
(uint32_t)CONFIG_BOARD));
116+
#endif
117+
118+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_SDK_VERSION_STRING_ENABLE
119+
bi_decl(bi_string(BINARY_INFO_TAG_RASPBERRY_PI, BINARY_INFO_ID_RP_SDK_VERSION,
120+
(uint32_t)"zephyr-" STRINGIFY(BUILD_VERSION)));
121+
#endif
122+
123+
#if defined(CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_VERSION_STRING_ENABLE) && defined(HAS_APP_VERSION)
124+
bi_decl(bi_program_version_string((uint32_t)APP_VERSION_EXTENDED_STRING));
125+
#endif
126+
127+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_DESCRIPTION_ENABLE
128+
bi_decl(bi_program_description((uint32_t)BI_PROGRAM_DESCRIPTION));
129+
#endif
130+
131+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_URL_ENABLE
132+
bi_decl(bi_program_url((uint32_t)BI_PROGRAM_URL));
133+
#endif
134+
135+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_BUILD_DATE_ENABLE
136+
bi_decl(bi_program_build_date_string((uint32_t)BI_PROGRAM_BUILD_DATE));
137+
#endif
138+
139+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_BOOT_STAGE2_NAME_ENABLE
140+
bi_decl(bi_string(BINARY_INFO_TAG_RASPBERRY_PI, BINARY_INFO_ID_RP_BOOT2_NAME,
141+
(uint32_t)PICO_BOOT_STAGE2_NAME));
142+
#endif
143+
144+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_ATTRIBUTE_BUILD_TYPE_ENABLE
145+
#ifdef CONFIG_DEBUG
146+
bi_decl(bi_program_build_attribute((uint32_t)"Debug"));
147+
#else
148+
bi_decl(bi_program_build_attribute((uint32_t)"Release"));
149+
#endif
150+
#endif
151+
152+
#ifdef CONFIG_RPI_PICO_BINARY_INFO_PINS_WITH_FUNC_ENABLE
153+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 0
154+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 0);
155+
#endif
156+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 1
157+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 1);
158+
#endif
159+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 2
160+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 2);
161+
#endif
162+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 3
163+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 3);
164+
#endif
165+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 4
166+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 4);
167+
#endif
168+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 5
169+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 5);
170+
#endif
171+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 6
172+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 6);
173+
#endif
174+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 7
175+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 7);
176+
#endif
177+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 8
178+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 8);
179+
#endif
180+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 9
181+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 9);
182+
#endif
183+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 10
184+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 10);
185+
#endif
186+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 11
187+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 11);
188+
#endif
189+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 12
190+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 12);
191+
#endif
192+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 13
193+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 13);
194+
#endif
195+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 14
196+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 14);
197+
#endif
198+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 15
199+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 15);
200+
#endif
201+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 16
202+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 16);
203+
#endif
204+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 17
205+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 17);
206+
#endif
207+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 18
208+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 18);
209+
#endif
210+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 19
211+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 19);
212+
#endif
213+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 20
214+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 20);
215+
#endif
216+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 21
217+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 21);
218+
#endif
219+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 22
220+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 22);
221+
#endif
222+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 23
223+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 23);
224+
#endif
225+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 24
226+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 24);
227+
#endif
228+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 25
229+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 25);
230+
#endif
231+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 26
232+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 26);
233+
#endif
234+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 27
235+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 27);
236+
#endif
237+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 28
238+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 28);
239+
#endif
240+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 29
241+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 29);
242+
#endif
243+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 30
244+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 30);
245+
#endif
246+
#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 31
247+
PININFO_PINCTRL_GROUP(DT_NODELABEL(pinctrl), 31);
248+
#endif
249+
#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)