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_PINS_WITH_NAMES 9
37 #define BINARY_INFO_TYPE_NAMED_GROUP 10
38 
39 // note plan is to reserve c1 = 0->31 for "collision tags"; i.e.
40 // for which you should always use random IDs with the binary_info,
41 // giving you 4 + 8 + 32 = 44 bits to avoid collisions
42 #define BINARY_INFO_MAKE_TAG(c1, c2) ((((uint)c2&0xffu)<<8u)|((uint)c1&0xffu))
43 
44 // Raspberry Pi defined. do not use
45 #define BINARY_INFO_TAG_RASPBERRY_PI BINARY_INFO_MAKE_TAG('R','P')
46 
47 #define BINARY_INFO_ID_RP_PROGRAM_NAME 0x02031c86
48 #define BINARY_INFO_ID_RP_PROGRAM_VERSION_STRING 0x11a9bc3a
49 #define BINARY_INFO_ID_RP_PROGRAM_BUILD_DATE_STRING 0x9da22254
50 #define BINARY_INFO_ID_RP_BINARY_END 0x68f465de
51 #define BINARY_INFO_ID_RP_PROGRAM_URL 0x1856239a
52 #define BINARY_INFO_ID_RP_PROGRAM_DESCRIPTION 0xb6a07c19
53 #define BINARY_INFO_ID_RP_PROGRAM_FEATURE 0xa1f4b453
54 #define BINARY_INFO_ID_RP_PROGRAM_BUILD_ATTRIBUTE 0x4275f0d3
55 #define BINARY_INFO_ID_RP_SDK_VERSION 0x5360b3ab
56 #define BINARY_INFO_ID_RP_PICO_BOARD 0xb63cffbb
57 
58 #if PICO_ON_DEVICE
59 #define bi_ptr_of(x) x *
60 #else
61 #define bi_ptr_of(x) uint32_t
62 #endif
63 typedef struct __packed _binary_info_core {
64         uint16_t type;
65         uint16_t tag;
66 } binary_info_core_t;
67 
68 typedef struct __packed _binary_info_raw_data {
69         struct _binary_info_core core;
70         uint8_t bytes[1];
71 } binary_info_raw_data_t;
72 
73 typedef struct __packed _binary_info_sized_data {
74         struct _binary_info_core core;
75         uint32_t length;
76         uint8_t bytes[1];
77 } binary_info_sized_data_t;
78 
79 typedef struct __packed _binary_info_list_zero_terminated {
80         struct _binary_info_core core;
81         bi_ptr_of(binary_info_t) list;
82 } binary_info_list_zero_terminated_t;
83 
84 typedef struct __packed _binary_info_id_and_int {
85         struct _binary_info_core core;
86         uint32_t id;
87         int32_t value;
88 } binary_info_id_and_int_t;
89 
90 typedef struct __packed _binary_info_id_and_string {
91         struct _binary_info_core core;
92         uint32_t id;
93         bi_ptr_of(const char) value;
94 } binary_info_id_and_string_t;
95 
96 typedef struct __packed _binary_info_block_device {
97         struct _binary_info_core core;
98         bi_ptr_of(const char) name; // optional static name (independent of what is formatted)
99         uint32_t address;
100         uint32_t size;
101         bi_ptr_of(binary_info_t) extra; // additional info
102         uint16_t flags;
103 } binary_info_block_device_t;
104 
105 #define BI_PINS_ENCODING_RANGE 1
106 #define BI_PINS_ENCODING_MULTI 2
107 
108 typedef struct __packed _binary_info_pins_with_func {
109     struct _binary_info_core core;
110     // p4_5 : p3_5 : p2_5 : p1_5 : p0_5 : func_4 : 001_3 //individual pins p0,p1,p2,p3,p4 ... if fewer than 5 then duplicate p
111     //                    phi_5 : plo_5 : func_4 : 010_3 // pin range plo-phi inclusive
112     uint32_t pin_encoding;
113 } binary_info_pins_with_func_t;
114 
115 typedef struct __packed _binary_info_pins_with_name {
116     struct _binary_info_core core;
117     uint32_t pin_mask;
118     bi_ptr_of(const char) label;
119 } binary_info_pins_with_name_t;
120 
121 #define BI_NAMED_GROUP_SHOW_IF_EMPTY   0x0001  // default is to hide
122 #define BI_NAMED_GROUP_SEPARATE_COMMAS 0x0002  // default is newlines
123 #define BI_NAMED_GROUP_SORT_ALPHA      0x0004  // default is no sort
124 #define BI_NAMED_GROUP_ADVANCED        0x0008  // if set, then only shown in say info -a
125 
126 typedef struct __packed _binary_info_named_group {
127     struct _binary_info_core core;
128     uint32_t parent_id;
129     uint16_t flags;
130     uint16_t group_tag;
131     uint32_t group_id;
132     bi_ptr_of(const char) label;
133 } binary_info_named_group_t;
134 
135 enum {
136     BINARY_INFO_BLOCK_DEV_FLAG_READ =
137     1 << 0, // if not readable, then it is basically hidden, but tools may choose to avoid overwriting it
138     BINARY_INFO_BLOCK_DEV_FLAG_WRITE = 1 << 1,
139     BINARY_INFO_BLOCK_DEV_FLAG_REFORMAT = 1 << 2, // may be reformatted..
140 
141     BINARY_INFO_BLOCK_DEV_FLAG_PT_UNKNOWN = 0 << 4, // unknown free to look
142     BINARY_INFO_BLOCK_DEV_FLAG_PT_MBR = 1 << 4, // expect MBR
143     BINARY_INFO_BLOCK_DEV_FLAG_PT_GPT = 2 << 4, // expect GPT
144     BINARY_INFO_BLOCK_DEV_FLAG_PT_NONE = 3 << 4, // no partition table
145 };
146 
147 #ifdef __cplusplus
148 }
149 #endif
150 #endif