/src/barebox/common/tlv/barebox.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | |
3 | | #include "barebox-info.h" |
4 | | #include <common.h> |
5 | | #include <net.h> |
6 | | #include <tlv/tlv.h> |
7 | | #include <param.h> |
8 | | #include <string.h> |
9 | | |
10 | | |
11 | | int tlv_handle_serial(struct tlv_device *dev, struct tlv_mapping *map, u16 len, const u8 *val) |
12 | 57 | { |
13 | 57 | const char *str; |
14 | | |
15 | 57 | str = __tlv_format_str(dev, map, len, val); |
16 | 57 | if (!str) |
17 | 1 | return -ENOMEM; |
18 | | |
19 | 56 | barebox_set_serial_number(str); |
20 | 56 | return 0; |
21 | 57 | } |
22 | | |
23 | | int tlv_handle_eth_address(struct tlv_device *dev, struct tlv_mapping *map, u16 len, const u8 *val) |
24 | 517 | { |
25 | 517 | int ret; |
26 | 517 | int i; |
27 | | |
28 | 517 | if (len % ETH_ALEN != 0) |
29 | 4 | return -EINVAL; |
30 | | |
31 | 1.34k | for (i = 0; i < len / ETH_ALEN; i++) { |
32 | 832 | eth_register_ethaddr(i, val + i * ETH_ALEN); |
33 | 832 | ret = tlv_format_mac(dev, map, ETH_ALEN, val + i * ETH_ALEN); |
34 | 832 | if (ret < 0) |
35 | 2 | return ret; |
36 | 832 | } |
37 | 511 | return 0; |
38 | 513 | } |
39 | | |
40 | | int tlv_handle_eth_address_seq(struct tlv_device *dev, struct tlv_mapping *map, u16 len, const u8 *val) |
41 | 873 | { |
42 | 873 | u8 eth_addr[ETH_ALEN]; |
43 | 873 | int eth_count; |
44 | 873 | int ret; |
45 | | |
46 | 873 | eth_count = *val; |
47 | | |
48 | 873 | if (len != 1 + ETH_ALEN) |
49 | 23 | return -EINVAL; |
50 | | |
51 | 850 | memcpy(eth_addr, val + 1, ETH_ALEN); |
52 | | |
53 | 14.3k | for (int i = 0; i < eth_count; i++, eth_addr_inc(eth_addr)) { |
54 | 13.5k | eth_register_ethaddr(i, eth_addr); |
55 | 13.5k | ret = tlv_format_mac(dev, map, ETH_ALEN, eth_addr); |
56 | 13.5k | if (ret < 0) |
57 | 2 | return ret; |
58 | 13.5k | } |
59 | | |
60 | 848 | return 0; |
61 | 850 | } |
62 | | |
63 | | static const char *__tlv_format(struct tlv_device *dev, struct tlv_mapping *map, char *buf) |
64 | 483 | { |
65 | 483 | struct param_d *param; |
66 | 483 | int ret; |
67 | | |
68 | 483 | if (!buf) |
69 | 0 | return NULL; |
70 | | |
71 | 483 | param = dev_add_param_fixed(&dev->dev, map->prop, NULL); |
72 | 483 | if (IS_ERR(param)) { |
73 | 18 | free(buf); |
74 | 18 | return NULL; |
75 | 18 | } |
76 | | |
77 | 465 | param->value = buf; /* pass ownership */ |
78 | | |
79 | 465 | ret = of_append_property(tlv_of_node(dev), map->prop, buf, strlen(buf) + 1); |
80 | 465 | if (ret) |
81 | 0 | return NULL; |
82 | | |
83 | 465 | return buf; |
84 | 465 | } |
85 | | |
86 | 256 | #define tlv_format(tlvdev, map, ...) ({ __tlv_format(tlvdev, map, basprintf(__VA_ARGS__)) ? 0 : -ENOMEM; }) |
87 | | |
88 | | const char * __tlv_format_str(struct tlv_device *dev, struct tlv_mapping *map, u16 len, const u8 *val) |
89 | 227 | { |
90 | 227 | return __tlv_format(dev, map, basprintf("%.*s", len, val)); |
91 | 227 | } |
92 | | |
93 | | int tlv_format_str(struct tlv_device *dev, struct tlv_mapping *map, u16 len, const u8 *val) |
94 | 170 | { |
95 | 170 | return __tlv_format_str(dev, map, len, val) ? 0 : -ENOMEM; |
96 | 170 | } |
97 | | |
98 | | int tlv_format_hex(struct tlv_device *dev, struct tlv_mapping *map, u16 len, const u8 *val) |
99 | 140 | { |
100 | 140 | return tlv_format(dev, map, "%*ph", len, val); |
101 | 140 | } |
102 | | |
103 | | int tlv_format_blob(struct tlv_device *dev, struct tlv_mapping *map, u16 len, const u8 *val) |
104 | 0 | { |
105 | 0 | struct param_d *param; |
106 | |
|
107 | 0 | param = dev_add_param_fixed(&dev->dev, map->prop, NULL); |
108 | 0 | if (!IS_ERR(param)) |
109 | 0 | param->value = basprintf("%*phN", len, val); |
110 | |
|
111 | 0 | return of_append_property(tlv_of_node(dev), map->prop, val, len); |
112 | 0 | } |
113 | | |
114 | | static struct device_node *of_append_node(struct device_node *root, const char *name) |
115 | 14.3k | { |
116 | 14.3k | struct device_node *np; |
117 | | |
118 | 14.3k | np = of_get_child_by_name(root, name); |
119 | 14.3k | if (np) |
120 | 14.2k | return np; |
121 | | |
122 | 129 | return of_new_node(root, name); |
123 | 14.3k | } |
124 | | |
125 | | /* |
126 | | * Each address is looked up among all previous ones when added, so limit |
127 | | * their number to keep a crafted TLV from taking ages to parse |
128 | | */ |
129 | 14.3k | #define TLV_MAX_ETH_ADDRESSES 256 |
130 | | |
131 | | /* add a single address-<num> entry to the property */ |
132 | | int tlv_format_mac(struct tlv_device *dev, struct tlv_mapping *map, u16 len, const u8 *val) |
133 | 14.3k | { |
134 | 14.3k | struct device_node *np = tlv_of_node(dev); |
135 | 14.3k | struct property *pp; |
136 | 14.3k | char propname[sizeof("address-4294967295")]; |
137 | 14.3k | int base = 0, ret; |
138 | | |
139 | 14.3k | if (len != ETH_ALEN) |
140 | 0 | return -EINVAL; |
141 | | |
142 | 14.3k | np = of_append_node(np, map->prop); |
143 | 14.3k | if (!np) |
144 | 0 | return -ENOMEM; |
145 | | |
146 | 14.3k | for_each_property_of_node(np, pp) |
147 | 1.37M | base++; |
148 | | |
149 | 14.3k | if (base >= TLV_MAX_ETH_ADDRESSES) |
150 | 4 | return -E2BIG; |
151 | | |
152 | 14.3k | snprintf(propname, sizeof(propname), "address-%u", base); |
153 | 14.3k | ret = of_property_sprintf(np, propname, "%*phC", ETH_ALEN, val); |
154 | 14.3k | if (ret < 0) |
155 | 0 | return ret; |
156 | | |
157 | 14.3k | return 0; |
158 | 14.3k | } |
159 | | |
160 | | int tlv_format_dec(struct tlv_device *dev, struct tlv_mapping *map, u16 len, const u8 *val) |
161 | 256 | { |
162 | 256 | switch (len) { |
163 | 9 | case 1: |
164 | 9 | return tlv_format(dev, map, "%u", *(u8 *)val); |
165 | 8 | case 2: |
166 | 8 | return tlv_format(dev, map, "%u", get_unaligned_be16(val)); |
167 | 10 | case 4: |
168 | 10 | return tlv_format(dev, map, "%u", get_unaligned_be32(val)); |
169 | 89 | case 8: |
170 | 89 | return tlv_format(dev, map, "%llu", get_unaligned_be64(val)); |
171 | 140 | default: |
172 | 140 | return tlv_format_hex(dev, map, len, val); |
173 | 256 | } |
174 | 256 | } |
175 | | |
176 | | int tlv_bind_soc_uid(struct tlv_device *dev, struct tlv_mapping *map, u16 len, const u8 *val) |
177 | 1 | { |
178 | 1 | const void *soc_uid = 0; |
179 | 1 | size_t soc_uid_len = 0; |
180 | | |
181 | 1 | if (barebox_get_soc_uid_bin(&soc_uid, &soc_uid_len)) |
182 | 1 | return -EACCES; |
183 | | |
184 | 0 | if (soc_uid && (size_t)len == soc_uid_len && !memcmp(val, soc_uid, len)) |
185 | 0 | return tlv_format(dev, map, "%*phN", len, val); |
186 | | |
187 | 0 | dev_err(&dev->dev, "%s: tlv bound to SoC UID %*phN, got %*phN\n", __func__, |
188 | 0 | len, val, (int)soc_uid_len, soc_uid); |
189 | |
|
190 | 0 | return -EACCES; |
191 | 0 | } |
192 | | |
193 | | struct tlv_mapping barebox_tlv_v1_mappings[] = { |
194 | | /* Detailed release information string for the device */ |
195 | | { 0x0002, tlv_format_str, "device-hardware-release" }, |
196 | | /* UNIX timestamp of fabrication */ |
197 | | { 0x0003, tlv_format_timestamp, "factory-timestamp" }, |
198 | | /* Device serial number string */ |
199 | | { 0x0004, tlv_handle_serial, "device-serial-number"}, |
200 | | /* Modification: 0: Device unmodified; 1: undocumented modifications */ |
201 | | { 0x0005, tlv_format_dec, "modification" }, |
202 | | /* a comma separated list of features */ |
203 | | { 0x0006, tlv_format_str, "featureset" }, |
204 | | /* Printed Circuit Board Assembly serial number string */ |
205 | | { 0x0007, tlv_format_str, "pcba-serial-number"}, |
206 | | /* Printed Circuit Board Assembly hardware release */ |
207 | | { 0x0008, tlv_format_str, "pcba-hardware-release"}, |
208 | | /* A list of Ethernet addresses or a single Ethernet address */ |
209 | | { 0x0011, tlv_handle_eth_address, "ethernet-address" }, |
210 | | /* A sequence of subsequent Ethernet addresses, by number and starting address */ |
211 | | { 0x0012, tlv_handle_eth_address_seq, "ethernet-address" }, |
212 | | /* Reject TLV if supplied binary data does not match UID SoC register */ |
213 | | { 0x0024, tlv_bind_soc_uid, "bound-soc-uid"}, |
214 | | { /* sentintel */ }, |
215 | | }; |
216 | | |
217 | | static struct tlv_mapping *mappings[] = { barebox_tlv_v1_mappings, NULL }; |
218 | | static struct of_device_id of_matches[] = { |
219 | | { .compatible = "barebox,tlv-v1" }, |
220 | | { /* sentinel */} |
221 | | }; |
222 | | static struct of_device_id of_matches_signed[] = { |
223 | | { .compatible = "barebox,tlv-v1-signed" }, |
224 | | { /* sentinel */ } |
225 | | }; |
226 | | |
227 | | static struct tlv_decoder barebox_tlv_v1 = { |
228 | | .magic = TLV_MAGIC_BAREBOX_V1, |
229 | | .driver.name = "barebox-tlv-v1", |
230 | | .driver.of_compatible = of_matches, |
231 | | .mappings = mappings, |
232 | | .signature_keyring = NULL, |
233 | | }; |
234 | | |
235 | | static struct tlv_decoder barebox_tlv_v1_signed = { |
236 | | .magic = TLV_MAGIC_BAREBOX_V1_SIGNED, |
237 | | .driver.name = "barebox-tlv-v1-signed", |
238 | | .driver.of_compatible = of_matches_signed, |
239 | | .mappings = mappings, |
240 | | .signature_keyring = "tlv-generic", |
241 | | }; |
242 | | |
243 | | static int tlv_register_default(void) |
244 | 32 | { |
245 | 32 | int err; |
246 | | |
247 | 32 | err = tlv_register_decoder(&barebox_tlv_v1); |
248 | 32 | if (err) |
249 | 0 | return err; |
250 | 32 | if (IS_ENABLED(CONFIG_TLV_SIGNATURE)) { |
251 | 0 | err = tlv_register_decoder(&barebox_tlv_v1_signed); |
252 | 0 | if (err) |
253 | 0 | return err; |
254 | 0 | } |
255 | 32 | return 0; |
256 | 32 | } |
257 | | late_initcall(tlv_register_default); |