1 /*
2  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _PICO_BINARY_INFO_STRUCTURE_H
8 #define _PICO_BINARY_INFO_STRUCTURE_H
9 
10 // NOTE: This file may be included by non SDK code, so does not use SDK includes
11 
12 // NOTE: ALL CHANGES MUST BE BACKWARDS COMPATIBLE
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 #include <stdint.h>
19 
20 #ifndef __packed
21 #define __packed __attribute__((packed))
22 #endif
23 
24 typedef struct _binary_info_core binary_info_t;
25 
26 #define BINARY_INFO_TYPE_RAW_DATA 1
27 #define BINARY_INFO_TYPE_SIZED_DATA 2
28 #define BINARY_INFO_TYPE_BINARY_INFO_LIST_ZERO_TERMINATED 3
29 #define BINARY_INFO_TYPE_BSON 4
30 #define BINARY_INFO_TYPE_ID_AND_INT 5
31 #define BINARY_INFO_TYPE_ID_AND_STRING 6
32 // traditional block device
33 #define BINARY_INFO_TYPE_BLOCK_DEVICE 7
34 #define BINARY_INFO_TYPE_PINS_WITH_FUNC 8
35 #define BINARY_INFO_TYPE_PINS_WITH_NAME 9
36 #define BINARY_INFO_TYPE_NAMED_GROUP 10
37 #define BINARY_INFO_TYPE_PTR_INT32_WITH_NAME 11
38 #define BINARY_INFO_TYPE_PTR_STRING_WITH_NAME 12
39 #define BINARY_INFO_TYPE_PINS64_WITH_FUNC 13
40 #define BINARY_INFO_TYPE_PINS64_WITH_NAME 14
41 
42 // note plan is to reserve c1 = 0->31 for "collision tags"; i.e.
43 // for which you should always use random IDs with the binary_info,
44 // giving you 4 + 8 + 32 = 44 bits to avoid collisions
45 #define BINARY_INFO_MAKE_TAG(c1, c2) ((((uint)c2&0xffu)<<8u)|((uint)c1&0xffu))
46 
47 // Raspberry Pi defined. do not use
48 #define BINARY_INFO_TAG_RASPBERRY_PI BINARY_INFO_MAKE_TAG('R','P')
49 
50 #define BINARY_INFO_ID_RP_PROGRAM_NAME 0x02031c86
51 #define BINARY_INFO_ID_RP_PROGRAM_VERSION_STRING 0x11a9bc3a
52 #define BINARY_INFO_ID_RP_PROGRAM_BUILD_DATE_STRING 0x9da22254
53 #define BINARY_INFO_ID_RP_BINARY_END 0x68f465de
54 #define BINARY_INFO_ID_RP_PROGRAM_URL 0x1856239a
55 #define BINARY_INFO_ID_RP_PROGRAM_DESCRIPTION 0xb6a07c19
56 #define BINARY_INFO_ID_RP_PROGRAM_FEATURE 0xa1f4b453
57 #define BINARY_INFO_ID_RP_PROGRAM_BUILD_ATTRIBUTE 0x4275f0d3
58 #define BINARY_INFO_ID_RP_SDK_VERSION 0x5360b3ab
59 #define BINARY_INFO_ID_RP_PICO_BOARD 0xb63cffbb
60 #define BINARY_INFO_ID_RP_BOOT2_NAME 0x7f8882e1
61 
62 #if PICO_ON_DEVICE
63 #define bi_ptr_of(x) x *
64 #else
65 #define bi_ptr_of(x) uint32_t
66 #endif
67 typedef struct __packed _binary_info_core {
68         uint16_t type;
69         uint16_t tag;
70 } binary_info_core_t;
71 
72 typedef struct __packed _binary_info_raw_data {
73         struct _binary_info_core core;
74         uint8_t bytes[1];
75 } binary_info_raw_data_t;
76 
77 typedef struct __packed _binary_info_sized_data {
78         struct _binary_info_core core;
79         uint32_t length;
80         uint8_t bytes[1];
81 } binary_info_sized_data_t;
82 
83 typedef struct __packed _binary_info_list_zero_terminated {
84         struct _binary_info_core core;
85         bi_ptr_of(binary_info_t) list;
86 } binary_info_list_zero_terminated_t;
87 
88 typedef struct __packed _binary_info_id_and_int {
89         struct _binary_info_core core;
90         uint32_t id;
91         int32_t value;
92 } binary_info_id_and_int_t;
93 
94 typedef struct __packed _binary_info_id_and_string {
95         struct _binary_info_core core;
96         uint32_t id;
97         bi_ptr_of(const char) value;
98 } binary_info_id_and_string_t;
99 
100 typedef struct __packed _binary_info_ptr_int32_with_name {
101         struct _binary_info_core core;
102         int32_t id;
103         bi_ptr_of(const int) value;
104         bi_ptr_of(const char) label;
105 } binary_info_ptr_int32_with_name_t;
106 
107 typedef struct __packed _binary_info_ptr_string_with_name {
108         struct _binary_info_core core;
109         int32_t id;
110         bi_ptr_of(const char) value;
111         bi_ptr_of(const char) label;
112         uint32_t len;
113 } binary_info_ptr_string_with_name_t;
114 
115 typedef struct __packed _binary_info_block_device {
116         struct _binary_info_core core;
117         bi_ptr_of(const char) name; // optional static name (independent of what is formatted)
118         uint32_t address;
119         uint32_t size;
120         bi_ptr_of(binary_info_t) extra; // additional info
121         uint16_t flags;
122 } binary_info_block_device_t;
123 
124 #define BI_PINS_ENCODING_RANGE 1
125 #define BI_PINS_ENCODING_MULTI 2
126 
127 typedef struct __packed _binary_info_pins_with_func {
128     struct _binary_info_core core;
129     // p4_5 : p3_5 : p2_5 : p1_5 : p0_5 : func_4 : 010_3 //individual pins p0,p1,p2,p3,p4 ... if fewer than 5 then duplicate p
130     //                    phi_5 : plo_5 : func_4 : 001_3 // pin range plo-phi inclusive
131     uint32_t pin_encoding;
132 } binary_info_pins_with_func_t;
133 
134 typedef struct __packed _binary_info_pins64_with_func {
135     struct _binary_info_core core;
136     // p6_8 : p5_8 : p4_8 : p3_8 : p2_8 : p1_8 : p0_8 : func_5 : 010_3 //individual pins p0,p1,p2 ... if fewer than 7 then duplicate p
137     //                    phi_8 : plo_8 : func_5 : 001_3 // pin range plo-phi inclusive
138     uint64_t pin_encoding;
139 } binary_info_pins64_with_func_t;
140 
141 typedef struct __packed _binary_info_pins_with_name {
142     struct _binary_info_core core;
143     uint32_t pin_mask;
144     bi_ptr_of(const char) label;
145 } binary_info_pins_with_name_t;
146 
147 typedef struct __packed _binary_info_pins64_with_name {
148     struct _binary_info_core core;
149     uint64_t pin_mask;
150     bi_ptr_of(const char) label;
151 } binary_info_pins64_with_name_t;
152 
153 #define BI_NAMED_GROUP_SHOW_IF_EMPTY   0x0001  // default is to hide
154 #define BI_NAMED_GROUP_SEPARATE_COMMAS 0x0002  // default is newlines
155 #define BI_NAMED_GROUP_SORT_ALPHA      0x0004  // default is no sort
156 #define BI_NAMED_GROUP_ADVANCED        0x0008  // if set, then only shown in say info -a
157 
158 typedef struct __packed _binary_info_named_group {
159     struct _binary_info_core core;
160     uint32_t parent_id;
161     uint16_t flags;
162     uint16_t group_tag;
163     uint32_t group_id;
164     bi_ptr_of(const char) label;
165 } binary_info_named_group_t;
166 
167 enum {
168     BINARY_INFO_BLOCK_DEV_FLAG_READ = 1 << 0, // if not readable, then it is basically hidden, but tools may choose to avoid overwriting it
169     BINARY_INFO_BLOCK_DEV_FLAG_WRITE = 1 << 1,
170     BINARY_INFO_BLOCK_DEV_FLAG_REFORMAT = 1 << 2, // may be reformatted..
171 
172     BINARY_INFO_BLOCK_DEV_FLAG_PT_UNKNOWN = 0 << 4, // unknown free to look
173     BINARY_INFO_BLOCK_DEV_FLAG_PT_MBR = 1 << 4, // expect MBR
174     BINARY_INFO_BLOCK_DEV_FLAG_PT_GPT = 2 << 4, // expect GPT
175     BINARY_INFO_BLOCK_DEV_FLAG_PT_NONE = 3 << 4, // no partition table
176 };
177 
178 #ifdef __cplusplus
179 }
180 #endif
181 #endif
182