blob: aff9e157cd8f8b845ee47e82e897b35be1a832ec [file] [log] [blame]
Patrick Georgiafd4c872020-05-05 23:43:18 +02001/* Taken from depthcharge: src/base/device_tree.c */
Patrick Georgiac959032020-05-05 22:49:26 +02002/* SPDX-License-Identifier: GPL-2.0-or-later */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02003
4#include <assert.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +02005#include <console/console.h>
Joel Kitching393c71c2019-06-16 16:09:42 +08006#include <ctype.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +02007#include <device_tree.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02008#include <endian.h>
Maximilian Brune33079b82024-03-04 15:34:41 +01009#include <stdbool.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020010#include <stdint.h>
Elyes Haouasbdd03c22024-05-27 11:20:07 +020011#include <stdio.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +020012#include <string.h>
13#include <stddef.h>
14#include <stdlib.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020015
Maximilian Brune33079b82024-03-04 15:34:41 +010016#define FDT_PATH_MAX_DEPTH 10 // should be a good enough upper bound
17#define FDT_PATH_MAX_LEN 128 // should be a good enough upper bound
18
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020019/*
20 * Functions for picking apart flattened trees.
21 */
22
Maximilian Brune33079b82024-03-04 15:34:41 +010023static int fdt_skip_nops(const void *blob, uint32_t offset)
24{
25 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
26
27 int index = 0;
28 while (be32toh(ptr[index]) == FDT_TOKEN_NOP)
29 index++;
30
31 return index * sizeof(uint32_t);
32}
33
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020034int fdt_next_property(const void *blob, uint32_t offset,
35 struct fdt_property *prop)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020036{
Patrick Rudolph666c1722018-04-03 09:57:33 +020037 struct fdt_header *header = (struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020038 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
39
Maximilian Brune33079b82024-03-04 15:34:41 +010040 // skip NOP tokens
41 offset += fdt_skip_nops(blob, offset);
42
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020043 int index = 0;
Patrick Rudolph666c1722018-04-03 09:57:33 +020044 if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020045 return 0;
46
Patrick Rudolph666c1722018-04-03 09:57:33 +020047 uint32_t size = be32toh(ptr[index++]);
48 uint32_t name_offset = be32toh(ptr[index++]);
49 name_offset += be32toh(header->strings_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020050
51 if (prop) {
52 prop->name = (char *)((uint8_t *)blob + name_offset);
53 prop->data = &ptr[index];
54 prop->size = size;
55 }
56
Patrick Rudolph666c1722018-04-03 09:57:33 +020057 index += DIV_ROUND_UP(size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020058
Patrick Rudolph666c1722018-04-03 09:57:33 +020059 return index * sizeof(uint32_t);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020060}
61
Maximilian Brune33079b82024-03-04 15:34:41 +010062/*
63 * fdt_next_node_name reads a node name
64 *
65 * @params blob address of FDT
66 * @params offset offset to the node to read the name from
67 * @params name parameter to hold the name that has been read or NULL
68 *
69 * @returns Either 0 on error or offset to the properties that come after the node name
70 */
71int fdt_next_node_name(const void *blob, uint32_t offset, const char **name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020072{
Maximilian Brune33079b82024-03-04 15:34:41 +010073 // skip NOP tokens
74 offset += fdt_skip_nops(blob, offset);
75
76 char *ptr = ((char *)blob) + offset;
Julius Wernera5ea3a22019-05-07 17:38:12 -070077 if (be32dec(ptr) != FDT_TOKEN_BEGIN_NODE)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020078 return 0;
79
80 ptr += 4;
81 if (name)
Maximilian Brune33079b82024-03-04 15:34:41 +010082 *name = ptr;
83
84 return ALIGN_UP(strlen(ptr) + 1, 4) + 4;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020085}
86
Maximilian Brune33079b82024-03-04 15:34:41 +010087/*
88 * A utility function to skip past nodes in flattened trees.
89 */
90int fdt_skip_node(const void *blob, uint32_t start_offset)
Julius Werner6702b682019-05-03 18:13:53 -070091{
Maximilian Brune33079b82024-03-04 15:34:41 +010092 uint32_t offset = start_offset;
93
94 const char *name;
95 int size = fdt_next_node_name(blob, offset, &name);
96 if (!size)
97 return 0;
98 offset += size;
99
100 while ((size = fdt_next_property(blob, offset, NULL)))
101 offset += size;
102
103 while ((size = fdt_skip_node(blob, offset)))
104 offset += size;
105
106 // skip NOP tokens
107 offset += fdt_skip_nops(blob, offset);
108
109 return offset - start_offset + sizeof(uint32_t);
Julius Werner6702b682019-05-03 18:13:53 -0700110}
111
Maximilian Brune33079b82024-03-04 15:34:41 +0100112/*
113 * fdt_read_prop reads a property inside a node
114 *
115 * @params blob address of FDT
116 * @params node_offset offset to the node to read the property from
117 * @params prop_name name of the property to read
118 * @params fdt_prop property is saved inside this parameter
119 *
120 * @returns Either 0 if no property has been found or an offset that points to the location
121 * of the property
122 */
123u32 fdt_read_prop(const void *blob, u32 node_offset, const char *prop_name,
124 struct fdt_property *fdt_prop)
125{
126 u32 offset = node_offset;
127
128 offset += fdt_next_node_name(blob, offset, NULL); // skip node name
129
130 size_t size;
131 while ((size = fdt_next_property(blob, offset, fdt_prop))) {
132 if (strcmp(fdt_prop->name, prop_name) == 0)
133 return offset;
134 offset += size;
135 }
136 return 0; // property not found
137}
138
139/*
140 * fdt_read_reg_prop reads the reg property inside a node
141 *
142 * @params blob address of FDT
143 * @params node_offset offset to the node to read the reg property from
144 * @params addr_cells number of cells used for one address
145 * @params size_cells number of cells used for one size
146 * @params regions all regions that are read inside the reg property are saved inside
147 * this array
148 * @params regions_count maximum number of entries that can be saved inside the regions array.
149 *
150 * Returns: Either 0 on error or returns the number of regions put into the regions array.
151 */
152u32 fdt_read_reg_prop(const void *blob, u32 node_offset, u32 addr_cells, u32 size_cells,
153 struct device_tree_region regions[], size_t regions_count)
154{
155 struct fdt_property prop;
156 u32 offset = fdt_read_prop(blob, node_offset, "reg", &prop);
157
158 if (!offset) {
159 printk(BIOS_DEBUG, "no reg property found in node_offset: %x\n", node_offset);
160 return 0;
161 }
162
163 // we found the reg property, now need to parse all regions in 'reg'
164 size_t count = prop.size / (4 * addr_cells + 4 * size_cells);
165 if (count > regions_count) {
166 printk(BIOS_ERR, "reg property at node_offset: %x has more entries (%zd) than regions array can hold (%zd)\n", node_offset, count, regions_count);
167 count = regions_count;
168 }
169 if (addr_cells > 2 || size_cells > 2) {
170 printk(BIOS_ERR, "addr_cells (%d) or size_cells (%d) bigger than 2\n",
171 addr_cells, size_cells);
172 return 0;
173 }
174 uint32_t *ptr = prop.data;
175 for (int i = 0; i < count; i++) {
176 if (addr_cells == 1)
177 regions[i].addr = be32dec(ptr);
178 else if (addr_cells == 2)
179 regions[i].addr = be64dec(ptr);
180 ptr += addr_cells;
181 if (size_cells == 1)
182 regions[i].size = be32dec(ptr);
183 else if (size_cells == 2)
184 regions[i].size = be64dec(ptr);
185 ptr += size_cells;
186 }
187
188 return count; // return the number of regions found in the reg property
189}
190
191static u32 fdt_read_cell_props(const void *blob, u32 node_offset, u32 *addrcp, u32 *sizecp)
192{
193 struct fdt_property prop;
194 u32 offset = node_offset;
195 size_t size;
196 while ((size = fdt_next_property(blob, offset, &prop))) {
197 if (addrcp && !strcmp(prop.name, "#address-cells"))
198 *addrcp = be32dec(prop.data);
199 if (sizecp && !strcmp(prop.name, "#size-cells"))
200 *sizecp = be32dec(prop.data);
201 offset += size;
202 }
203 return offset;
204}
205
206/*
207 * fdt_find_node searches for a node relative to another node
208 *
209 * @params blob address of FDT
210 *
211 * @params parent_node_offset offset to node from which to traverse the tree
212 *
213 * @params path null terminated array of node names specifying a
214 * relative path (e.g: { "cpus", "cpu0", NULL })
215 *
216 * @params addrcp/sizecp If any address-cells and size-cells properties are found that are
217 * part of the parent node of the node we are looking, addrcp and sizecp
218 * are set to these respectively.
219 *
220 * @returns: Either 0 if no node has been found or the offset to the node found
221 */
222static u32 fdt_find_node(const void *blob, u32 parent_node_offset, char **path,
223 u32 *addrcp, u32 *sizecp)
224{
225 if (*path == NULL)
226 return parent_node_offset; // node found
227
228 size_t size = fdt_next_node_name(blob, parent_node_offset, NULL); // skip node name
229
230 /*
231 * get address-cells and size-cells properties while skipping the others.
232 * According to spec address-cells and size-cells are not inherited, but we
233 * intentionally follow the Linux implementation here and treat them as inheritable.
234 */
235 u32 node_offset = fdt_read_cell_props(blob, parent_node_offset + size, addrcp, sizecp);
236
237 const char *node_name;
238 // walk all children nodes
239 while ((size = fdt_next_node_name(blob, node_offset, &node_name))) {
240 if (!strcmp(*path, node_name)) {
241 // traverse one level deeper into the path
242 return fdt_find_node(blob, node_offset, path + 1, addrcp, sizecp);
243 }
244 // node is not the correct one. skip current node
245 node_offset += fdt_skip_node(blob, node_offset);
246 }
247
248 // we have searched everything and could not find a fitting node
249 return 0;
250}
251
252/*
253 * fdt_find_node_by_path finds a node behind a given node path
254 *
255 * @params blob address of FDT
256 * @params path absolute path to the node that should be searched for
257 *
258 * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells
259 * value found in the node of the node specified by node_offset. Either
260 * may be NULL to ignore. If no #address-cells and #size-cells is found
261 * default values of #address-cells=2 and #size-cells=1 are returned.
262 *
263 * @returns Either 0 on error or the offset to the node found behind the path
264 */
265u32 fdt_find_node_by_path(const void *blob, const char *path, u32 *addrcp, u32 *sizecp)
266{
267 // sanity check
268 if (path[0] != '/') {
269 printk(BIOS_ERR, "devicetree path must start with a /\n");
270 return 0;
271 }
272 if (!blob) {
273 printk(BIOS_ERR, "devicetree blob is NULL\n");
274 return 0;
275 }
276
277 if (addrcp)
278 *addrcp = 2;
279 if (sizecp)
280 *sizecp = 1;
281
282 struct fdt_header *fdt_hdr = (struct fdt_header *)blob;
283
284 /*
285 * split path into separate nodes
286 * e.g: "/cpus/cpu0" -> { "cpus", "cpu0" }
287 */
288 char *path_array[FDT_PATH_MAX_DEPTH];
289 size_t path_size = strlen(path);
290 assert(path_size < FDT_PATH_MAX_LEN);
291 char path_copy[FDT_PATH_MAX_LEN];
292 memcpy(path_copy, path, path_size + 1);
293 char *cur = path_copy;
294 int i;
295 for (i = 0; i < FDT_PATH_MAX_DEPTH; i++) {
296 path_array[i] = strtok_r(NULL, "/", &cur);
297 if (!path_array[i])
298 break;
299 }
300 assert(i < FDT_PATH_MAX_DEPTH);
301
302 return fdt_find_node(blob, be32toh(fdt_hdr->structure_offset), path_array, addrcp, sizecp);
303}
304
305/*
306 * fdt_find_subnodes_by_prefix finds a node with a given prefix relative to a parent node
307 *
308 * @params blob The FDT to search.
309 *
310 * @params node_offset offset to the node of which the children should be searched
311 *
312 * @params prefix A string to search for a node with a given prefix. This can for example
313 * be 'cpu' to look for all nodes matching this prefix. Only children of
314 * node_offset are searched. Therefore in order to search all nodes matching
315 * the 'cpu' prefix, node_offset should probably point to the 'cpus' node.
316 * An empty prefix ("") searches for all children nodes of node_offset.
317 *
318 * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells
319 * value found in the node of the node specified by node_offset. Either
320 * may be NULL to ignore. If no #address-cells and #size-cells is found
321 * addrcp and sizecp are left untouched.
322 *
323 * @params results Array of offsets pointing to each node matching the given prefix.
324 * @params results_len Number of entries allocated for the 'results' array
325 *
326 * @returns offset to last node found behind path or 0 if no node has been found
327 */
328size_t fdt_find_subnodes_by_prefix(const void *blob, u32 node_offset, const char *prefix,
329 u32 *addrcp, u32 *sizecp, u32 *results, size_t results_len)
330{
331 // sanity checks
332 if (!blob || !results || !prefix) {
333 printk(BIOS_ERR, "%s: input parameter cannot be null/\n", __func__);
334 return 0;
335 }
336
337 u32 offset = node_offset;
338
339 // we don't care about the name of the current node
340 u32 size = fdt_next_node_name(blob, offset, NULL);
341 if (!size) {
342 printk(BIOS_ERR, "%s: node_offset: %x does not point to a node\n",
343 __func__, node_offset);
344 return 0;
345 }
346 offset += size;
347
348 /*
349 * update addrcp and sizecp if the node contains an address-cells and size-cells
350 * property. Otherwise use addrcp and sizecp provided by caller.
351 */
352 offset = fdt_read_cell_props(blob, offset, addrcp, sizecp);
353
354 size_t count_results = 0;
355 int prefix_len = strlen(prefix);
356 const char *node_name;
357 // walk all children nodes of offset
358 while ((size = fdt_next_node_name(blob, offset, &node_name))) {
359
360 if (count_results >= results_len) {
361 printk(BIOS_WARNING,
362 "%s: results_len (%zd) smaller than count_results (%zd)\n",
363 __func__, results_len, count_results);
364 break;
365 }
366
367 if (!strncmp(prefix, node_name, prefix_len)) {
368 // we found a node that matches the prefix
369 results[count_results++] = offset;
370 }
371
372 // node does not match the prefix. skip current node
373 offset += fdt_skip_node(blob, offset);
374 }
375
376 // return last occurrence
377 return count_results;
378}
379
380static const char *fdt_read_alias_prop(const void *blob, const char *alias_name)
381{
382 u32 node_offset = fdt_find_node_by_path(blob, "/aliases", NULL, NULL);
383 if (!node_offset) {
384 printk(BIOS_DEBUG, "no /aliases node found\n");
385 return NULL;
386 }
387 struct fdt_property alias_prop;
388 if (!fdt_read_prop(blob, node_offset, alias_name, &alias_prop)) {
389 printk(BIOS_DEBUG, "property %s in /aliases node not found\n", alias_name);
390 return NULL;
391 }
392 return (const char *)alias_prop.data;
393}
394
395/*
396 * Find a node in the tree from a string device tree path.
397 *
398 * @params blob Address to the FDT
399 * @params alias_name node name alias that should be searched for.
400 * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells
401 * value found in the node of the node specified by node_offset. Either
402 * may be NULL to ignore. If no #address-cells and #size-cells is found
403 * default values of #address-cells=2 and #size-cells=1 are returned.
404 *
405 * @returns offset to last node found behind path or 0 if no node has been found
406 */
407u32 fdt_find_node_by_alias(const void *blob, const char *alias_name, u32 *addrcp, u32 *sizecp)
408{
409 const char *node_name = fdt_read_alias_prop(blob, alias_name);
410 if (!node_name) {
411 printk(BIOS_DEBUG, "alias %s not found\n", alias_name);
412 return 0;
413 }
414
415 u32 node_offset = fdt_find_node_by_path(blob, node_name, addrcp, sizecp);
416 if (!node_offset) {
417 // This should not happen (invalid devicetree)
418 printk(BIOS_WARNING,
419 "Could not find node '%s', which alias was referring to '%s'\n",
420 node_name, alias_name);
421 return 0;
422 }
423 return node_offset;
424}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200425
426
427/*
428 * Functions for printing flattened trees.
429 */
430
431static void print_indent(int depth)
432{
Julius Werner0d746532019-05-06 19:35:56 -0700433 printk(BIOS_DEBUG, "%*s", depth * 8, "");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200434}
435
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200436static void print_property(const struct fdt_property *prop, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200437{
Julius Werner0d746532019-05-06 19:35:56 -0700438 int is_string = prop->size > 0 &&
439 ((char *)prop->data)[prop->size - 1] == '\0';
440
Maximilian Brune77eaec62023-09-20 05:12:04 +0200441 if (is_string) {
442 for (int i = 0; i < prop->size - 1; i++) {
443 if (!isprint(((char *)prop->data)[i])) {
Julius Werner0d746532019-05-06 19:35:56 -0700444 is_string = 0;
Maximilian Brune77eaec62023-09-20 05:12:04 +0200445 break;
446 }
447 }
448 }
Julius Werner0d746532019-05-06 19:35:56 -0700449
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200450 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -0700451 if (is_string) {
452 printk(BIOS_DEBUG, "%s = \"%s\";\n",
453 prop->name, (const char *)prop->data);
454 } else {
455 printk(BIOS_DEBUG, "%s = < ", prop->name);
456 for (int i = 0; i < MIN(128, prop->size); i += 4) {
457 uint32_t val = 0;
458 for (int j = 0; j < MIN(4, prop->size - i); j++)
459 val |= ((uint8_t *)prop->data)[i + j] <<
460 (24 - j * 8);
461 printk(BIOS_DEBUG, "%#.2x ", val);
462 }
463 if (prop->size > 128)
464 printk(BIOS_DEBUG, "...");
465 printk(BIOS_DEBUG, ">;\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200466 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200467}
468
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200469static int print_flat_node(const void *blob, uint32_t start_offset, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200470{
471 int offset = start_offset;
472 const char *name;
473 int size;
474
Maximilian Brune33079b82024-03-04 15:34:41 +0100475 size = fdt_next_node_name(blob, offset, &name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200476 if (!size)
477 return 0;
478 offset += size;
479
480 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -0700481 printk(BIOS_DEBUG, "%s {\n", name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200482
Patrick Rudolph666c1722018-04-03 09:57:33 +0200483 struct fdt_property prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200484 while ((size = fdt_next_property(blob, offset, &prop))) {
485 print_property(&prop, depth + 1);
486
487 offset += size;
488 }
489
Julius Werner23df4772019-05-17 22:50:18 -0700490 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700491
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200492 while ((size = print_flat_node(blob, offset, depth + 1)))
493 offset += size;
494
Julius Werner0d746532019-05-06 19:35:56 -0700495 print_indent(depth);
496 printk(BIOS_DEBUG, "}\n");
497
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200498 return offset - start_offset + sizeof(uint32_t);
499}
500
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200501void fdt_print_node(const void *blob, uint32_t offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200502{
503 print_flat_node(blob, offset, 0);
504}
505
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200506/*
507 * Functions to turn a flattened tree into an unflattened one.
508 */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200509
Maximilian Brune33079b82024-03-04 15:34:41 +0100510static int dt_prop_is_phandle(struct device_tree_property *prop)
511{
512 return !(strcmp("phandle", prop->prop.name) &&
513 strcmp("linux,phandle", prop->prop.name));
514}
515
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200516static int fdt_unflatten_node(const void *blob, uint32_t start_offset,
Julius Werner6702b682019-05-03 18:13:53 -0700517 struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200518 struct device_tree_node **new_node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200519{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200520 struct list_node *last;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200521 int offset = start_offset;
522 const char *name;
523 int size;
524
Maximilian Brune33079b82024-03-04 15:34:41 +0100525 size = fdt_next_node_name(blob, offset, &name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200526 if (!size)
527 return 0;
528 offset += size;
529
Julius Werner9636a102019-05-03 17:36:43 -0700530 struct device_tree_node *node = xzalloc(sizeof(*node));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200531 *new_node = node;
532 node->name = name;
533
Patrick Rudolph666c1722018-04-03 09:57:33 +0200534 struct fdt_property fprop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200535 last = &node->properties;
536 while ((size = fdt_next_property(blob, offset, &fprop))) {
Julius Werner9636a102019-05-03 17:36:43 -0700537 struct device_tree_property *prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200538 prop->prop = fprop;
539
Julius Werner6702b682019-05-03 18:13:53 -0700540 if (dt_prop_is_phandle(prop)) {
541 node->phandle = be32dec(prop->prop.data);
542 if (node->phandle > tree->max_phandle)
543 tree->max_phandle = node->phandle;
544 }
545
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200546 list_insert_after(&prop->list_node, last);
547 last = &prop->list_node;
548
549 offset += size;
550 }
551
Patrick Rudolph666c1722018-04-03 09:57:33 +0200552 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200553 last = &node->children;
Julius Werner6702b682019-05-03 18:13:53 -0700554 while ((size = fdt_unflatten_node(blob, offset, tree, &child))) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200555 list_insert_after(&child->list_node, last);
556 last = &child->list_node;
557
558 offset += size;
559 }
560
561 return offset - start_offset + sizeof(uint32_t);
562}
563
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200564static int fdt_unflatten_map_entry(const void *blob, uint32_t offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200565 struct device_tree_reserve_map_entry **new)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200566{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200567 const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset);
568 const uint64_t start = be64toh(ptr[0]);
569 const uint64_t size = be64toh(ptr[1]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200570
571 if (!size)
572 return 0;
573
Julius Werner9636a102019-05-03 17:36:43 -0700574 struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200575 *new = entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200576 entry->start = start;
577 entry->size = size;
578
579 return sizeof(uint64_t) * 2;
580}
581
Maximilian Brune33079b82024-03-04 15:34:41 +0100582bool fdt_is_valid(const void *blob)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200583{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200584 const struct fdt_header *header = (const struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200585
Julius Werner73eaec82019-05-03 17:58:07 -0700586 uint32_t magic = be32toh(header->magic);
587 uint32_t version = be32toh(header->version);
588 uint32_t last_comp_version = be32toh(header->last_comp_version);
589
590 if (magic != FDT_HEADER_MAGIC) {
Maximilian Brune33079b82024-03-04 15:34:41 +0100591 printk(BIOS_ERR, "Invalid device tree magic %#.8x!\n", magic);
592 return false;
Julius Werner73eaec82019-05-03 17:58:07 -0700593 }
594 if (last_comp_version > FDT_SUPPORTED_VERSION) {
Maximilian Brune33079b82024-03-04 15:34:41 +0100595 printk(BIOS_ERR, "Unsupported device tree version %u(>=%u)\n",
Julius Werner73eaec82019-05-03 17:58:07 -0700596 version, last_comp_version);
Maximilian Brune33079b82024-03-04 15:34:41 +0100597 return false;
Julius Werner73eaec82019-05-03 17:58:07 -0700598 }
599 if (version > FDT_SUPPORTED_VERSION)
Elyes Haouasd7326282022-12-21 08:16:03 +0100600 printk(BIOS_NOTICE, "FDT version %u too new, should add support!\n",
Julius Werner73eaec82019-05-03 17:58:07 -0700601 version);
Maximilian Brune33079b82024-03-04 15:34:41 +0100602 return true;
603}
604
605struct device_tree *fdt_unflatten(const void *blob)
606{
607 struct device_tree *tree = xzalloc(sizeof(*tree));
608 const struct fdt_header *header = (const struct fdt_header *)blob;
609 tree->header = header;
610
611 if (fdt_is_valid(blob))
612 return NULL;
Julius Werner73eaec82019-05-03 17:58:07 -0700613
Patrick Rudolph666c1722018-04-03 09:57:33 +0200614 uint32_t struct_offset = be32toh(header->structure_offset);
615 uint32_t strings_offset = be32toh(header->strings_offset);
616 uint32_t reserve_offset = be32toh(header->reserve_map_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200617 uint32_t min_offset = 0;
618 min_offset = MIN(struct_offset, strings_offset);
619 min_offset = MIN(min_offset, reserve_offset);
Julius Werner23df4772019-05-17 22:50:18 -0700620 /* Assume everything up to the first non-header component is part of
621 the header and needs to be preserved. This will protect us against
622 new elements being added in the future. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200623 tree->header_size = min_offset;
624
Patrick Rudolph666c1722018-04-03 09:57:33 +0200625 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200626 uint32_t offset = reserve_offset;
627 int size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200628 struct list_node *last = &tree->reserve_map;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200629 while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) {
630 list_insert_after(&entry->list_node, last);
631 last = &entry->list_node;
632
633 offset += size;
634 }
635
Julius Werner6702b682019-05-03 18:13:53 -0700636 fdt_unflatten_node(blob, struct_offset, tree, &tree->root);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200637
638 return tree;
639}
640
641
642
643/*
Patrick Rudolph666c1722018-04-03 09:57:33 +0200644 * Functions to find the size of the device tree if it was flattened.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200645 */
646
Patrick Rudolph666c1722018-04-03 09:57:33 +0200647static void dt_flat_prop_size(struct device_tree_property *prop,
648 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200649{
Julius Werner23df4772019-05-17 22:50:18 -0700650 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200651 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700652 /* Size. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200653 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700654 /* Name offset. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200655 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700656 /* Property value. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200657 *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200658
Julius Werner23df4772019-05-17 22:50:18 -0700659 /* Property name. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200660 *strings_size += strlen(prop->prop.name) + 1;
661}
662
Patrick Rudolph666c1722018-04-03 09:57:33 +0200663static void dt_flat_node_size(struct device_tree_node *node,
664 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200665{
Julius Werner23df4772019-05-17 22:50:18 -0700666 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200667 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700668 /* Node name. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200669 *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200670
Patrick Rudolph666c1722018-04-03 09:57:33 +0200671 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200672 list_for_each(prop, node->properties, list_node)
673 dt_flat_prop_size(prop, struct_size, strings_size);
674
Patrick Rudolph666c1722018-04-03 09:57:33 +0200675 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200676 list_for_each(child, node->children, list_node)
677 dt_flat_node_size(child, struct_size, strings_size);
678
Julius Werner23df4772019-05-17 22:50:18 -0700679 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200680 *struct_size += sizeof(uint32_t);
681}
682
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200683uint32_t dt_flat_size(const struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200684{
685 uint32_t size = tree->header_size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200686 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200687 list_for_each(entry, tree->reserve_map, list_node)
688 size += sizeof(uint64_t) * 2;
689 size += sizeof(uint64_t) * 2;
690
691 uint32_t struct_size = 0;
692 uint32_t strings_size = 0;
693 dt_flat_node_size(tree->root, &struct_size, &strings_size);
694
695 size += struct_size;
Julius Werner23df4772019-05-17 22:50:18 -0700696 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200697 size += sizeof(uint32_t);
698
699 size += strings_size;
700
701 return size;
702}
703
704
705
706/*
707 * Functions to flatten a device tree.
708 */
709
Patrick Rudolph666c1722018-04-03 09:57:33 +0200710static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200711 void **map_start)
712{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200713 ((uint64_t *)*map_start)[0] = htobe64(entry->start);
714 ((uint64_t *)*map_start)[1] = htobe64(entry->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200715 *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2;
716}
717
Patrick Rudolph666c1722018-04-03 09:57:33 +0200718static void dt_flatten_prop(struct device_tree_property *prop,
719 void **struct_start, void *strings_base,
720 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200721{
722 uint8_t *dstruct = (uint8_t *)*struct_start;
723 uint8_t *dstrings = (uint8_t *)*strings_start;
724
Julius Wernera5ea3a22019-05-07 17:38:12 -0700725 be32enc(dstruct, FDT_TOKEN_PROPERTY);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200726 dstruct += sizeof(uint32_t);
727
Julius Wernera5ea3a22019-05-07 17:38:12 -0700728 be32enc(dstruct, prop->prop.size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200729 dstruct += sizeof(uint32_t);
730
731 uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base;
Julius Wernera5ea3a22019-05-07 17:38:12 -0700732 be32enc(dstruct, name_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200733 dstruct += sizeof(uint32_t);
734
735 strcpy((char *)dstrings, prop->prop.name);
736 dstrings += strlen(prop->prop.name) + 1;
737
738 memcpy(dstruct, prop->prop.data, prop->prop.size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200739 dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200740
741 *struct_start = dstruct;
742 *strings_start = dstrings;
743}
744
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200745static void dt_flatten_node(const struct device_tree_node *node,
746 void **struct_start, void *strings_base,
747 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200748{
749 uint8_t *dstruct = (uint8_t *)*struct_start;
750 uint8_t *dstrings = (uint8_t *)*strings_start;
751
Julius Wernera5ea3a22019-05-07 17:38:12 -0700752 be32enc(dstruct, FDT_TOKEN_BEGIN_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200753 dstruct += sizeof(uint32_t);
754
755 strcpy((char *)dstruct, node->name);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200756 dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200757
Patrick Rudolph666c1722018-04-03 09:57:33 +0200758 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200759 list_for_each(prop, node->properties, list_node)
760 dt_flatten_prop(prop, (void **)&dstruct, strings_base,
761 (void **)&dstrings);
762
Patrick Rudolph666c1722018-04-03 09:57:33 +0200763 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200764 list_for_each(child, node->children, list_node)
765 dt_flatten_node(child, (void **)&dstruct, strings_base,
766 (void **)&dstrings);
767
Julius Wernera5ea3a22019-05-07 17:38:12 -0700768 be32enc(dstruct, FDT_TOKEN_END_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200769 dstruct += sizeof(uint32_t);
770
771 *struct_start = dstruct;
772 *strings_start = dstrings;
773}
774
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200775void dt_flatten(const struct device_tree *tree, void *start_dest)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200776{
777 uint8_t *dest = (uint8_t *)start_dest;
778
779 memcpy(dest, tree->header, tree->header_size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200780 struct fdt_header *header = (struct fdt_header *)dest;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200781 dest += tree->header_size;
782
Patrick Rudolph666c1722018-04-03 09:57:33 +0200783 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200784 list_for_each(entry, tree->reserve_map, list_node)
785 dt_flatten_map_entry(entry, (void **)&dest);
786 ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0;
787 dest += sizeof(uint64_t) * 2;
788
789 uint32_t struct_size = 0;
790 uint32_t strings_size = 0;
791 dt_flat_node_size(tree->root, &struct_size, &strings_size);
792
793 uint8_t *struct_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200794 header->structure_offset = htobe32(dest - (uint8_t *)start_dest);
795 header->structure_size = htobe32(struct_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200796 dest += struct_size;
797
Patrick Rudolph666c1722018-04-03 09:57:33 +0200798 *((uint32_t *)dest) = htobe32(FDT_TOKEN_END);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200799 dest += sizeof(uint32_t);
800
801 uint8_t *strings_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200802 header->strings_offset = htobe32(dest - (uint8_t *)start_dest);
803 header->strings_size = htobe32(strings_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200804 dest += strings_size;
805
806 dt_flatten_node(tree->root, (void **)&struct_start, strings_start,
807 (void **)&strings_start);
808
Patrick Rudolph666c1722018-04-03 09:57:33 +0200809 header->totalsize = htobe32(dest - (uint8_t *)start_dest);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200810}
811
812
813
814/*
815 * Functions for printing a non-flattened device tree.
816 */
817
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200818static void print_node(const struct device_tree_node *node, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200819{
820 print_indent(depth);
Julius Werner23df4772019-05-17 22:50:18 -0700821 if (depth == 0) /* root node has no name, print a starting slash */
Julius Werner0d746532019-05-06 19:35:56 -0700822 printk(BIOS_DEBUG, "/");
823 printk(BIOS_DEBUG, "%s {\n", node->name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200824
Patrick Rudolph666c1722018-04-03 09:57:33 +0200825 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200826 list_for_each(prop, node->properties, list_node)
827 print_property(&prop->prop, depth + 1);
828
Julius Werner23df4772019-05-17 22:50:18 -0700829 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700830
Patrick Rudolph666c1722018-04-03 09:57:33 +0200831 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200832 list_for_each(child, node->children, list_node)
833 print_node(child, depth + 1);
Julius Werner0d746532019-05-06 19:35:56 -0700834
835 print_indent(depth);
836 printk(BIOS_DEBUG, "};\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200837}
838
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200839void dt_print_node(const struct device_tree_node *node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200840{
841 print_node(node, 0);
842}
843
844
845
846/*
847 * Functions for reading and manipulating an unflattened device tree.
848 */
849
850/*
851 * Read #address-cells and #size-cells properties from a node.
852 *
853 * @param node The device tree node to read from.
854 * @param addrcp Pointer to store #address-cells in, skipped if NULL.
855 * @param sizecp Pointer to store #size-cells in, skipped if NULL.
856 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200857void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
858 u32 *sizecp)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200859{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200860 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200861 list_for_each(prop, node->properties, list_node) {
862 if (addrcp && !strcmp("#address-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700863 *addrcp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200864 if (sizecp && !strcmp("#size-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700865 *sizecp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200866 }
867}
868
869/*
870 * Find a node from a device tree path, relative to a parent node.
871 *
872 * @param parent The node from which to start the relative path lookup.
873 * @param path An array of path component strings that will be looked
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200874 * up in order to find the node. Must be terminated with
875 * a NULL pointer. Example: {'firmware', 'coreboot', NULL}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200876 * @param addrcp Pointer that will be updated with any #address-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200877 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200878 * @param sizecp Pointer that will be updated with any #size-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200879 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200880 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
881 * @return The found/created node, or NULL.
882 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200883struct device_tree_node *dt_find_node(struct device_tree_node *parent,
884 const char **path, u32 *addrcp,
885 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200886{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200887 struct device_tree_node *node, *found = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200888
Julius Werner23df4772019-05-17 22:50:18 -0700889 /* Update #address-cells and #size-cells for this level. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200890 dt_read_cell_props(parent, addrcp, sizecp);
891
892 if (!*path)
893 return parent;
894
Julius Werner23df4772019-05-17 22:50:18 -0700895 /* Find the next node in the path, if it exists. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200896 list_for_each(node, parent->children, list_node) {
897 if (!strcmp(node->name, *path)) {
898 found = node;
899 break;
900 }
901 }
902
Julius Werner23df4772019-05-17 22:50:18 -0700903 /* Otherwise create it or return NULL. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200904 if (!found) {
905 if (!create)
906 return NULL;
907
Sergii Dmytruk206328d2022-03-13 18:23:17 +0200908 found = calloc(1, sizeof(*found));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200909 if (!found)
910 return NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200911 found->name = strdup(*path);
912 if (!found->name)
913 return NULL;
914
915 list_insert_after(&found->list_node, &parent->children);
916 }
917
918 return dt_find_node(found, path + 1, addrcp, sizecp, create);
919}
920
921/*
Julius Wernerf36d53c2019-05-03 18:23:34 -0700922 * Find a node in the tree from a string device tree path.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200923 *
Julius Wernerf36d53c2019-05-03 18:23:34 -0700924 * @param tree The device tree to search.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200925 * @param path A string representing a path in the device tree, with
Julius Wernerfbec63d2019-05-03 18:29:28 -0700926 * nodes separated by '/'. Example: "/firmware/coreboot"
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200927 * @param addrcp Pointer that will be updated with any #address-cells
928 * value found in the path. May be NULL to ignore.
929 * @param sizecp Pointer that will be updated with any #size-cells
930 * value found in the path. May be NULL to ignore.
931 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
932 * @return The found/created node, or NULL.
933 *
Julius Werner6d5695f2019-05-06 19:23:28 -0700934 * It is the caller responsibility to provide a path string that doesn't end
935 * with a '/' and doesn't contain any "//". If the path does not start with a
936 * '/', the first segment is interpreted as an alias. */
Julius Wernerf36d53c2019-05-03 18:23:34 -0700937struct device_tree_node *dt_find_node_by_path(struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200938 const char *path, u32 *addrcp,
939 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200940{
Julius Werner6d5695f2019-05-06 19:23:28 -0700941 char *sub_path;
942 char *duped_str;
943 struct device_tree_node *parent;
944 char *next_slash;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200945 /* Hopefully enough depth for any node. */
946 const char *path_array[15];
947 int i;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200948 struct device_tree_node *node = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200949
Julius Werner23df4772019-05-17 22:50:18 -0700950 if (path[0] == '/') { /* regular path */
951 if (path[1] == '\0') { /* special case: "/" is root node */
Julius Werner6d5695f2019-05-06 19:23:28 -0700952 dt_read_cell_props(tree->root, addrcp, sizecp);
953 return tree->root;
954 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200955
Julius Werner6d5695f2019-05-06 19:23:28 -0700956 sub_path = duped_str = strdup(&path[1]);
957 if (!sub_path)
958 return NULL;
959
960 parent = tree->root;
Julius Werner23df4772019-05-17 22:50:18 -0700961 } else { /* alias */
Julius Werner6d5695f2019-05-06 19:23:28 -0700962 char *alias;
963
964 alias = duped_str = strdup(path);
965 if (!alias)
966 return NULL;
967
968 sub_path = strchr(alias, '/');
969 if (sub_path)
970 *sub_path = '\0';
971
972 parent = dt_find_node_by_alias(tree, alias);
973 if (!parent) {
974 printk(BIOS_DEBUG,
975 "Could not find node '%s', alias '%s' does not exist\n",
976 path, alias);
977 free(duped_str);
978 return NULL;
979 }
980
981 if (!sub_path) {
Julius Werner23df4772019-05-17 22:50:18 -0700982 /* it's just the alias, no sub-path */
Julius Werner6d5695f2019-05-06 19:23:28 -0700983 free(duped_str);
984 return parent;
985 }
986
987 sub_path++;
988 }
989
990 next_slash = sub_path;
991 path_array[0] = sub_path;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200992 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200993 next_slash = strchr(next_slash, '/');
994 if (!next_slash)
995 break;
996
997 *next_slash++ = '\0';
998 path_array[i] = next_slash;
999 }
1000
1001 if (!next_slash) {
1002 path_array[i] = NULL;
Julius Werner6d5695f2019-05-06 19:23:28 -07001003 node = dt_find_node(parent, path_array,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001004 addrcp, sizecp, create);
1005 }
1006
Julius Werner6d5695f2019-05-06 19:23:28 -07001007 free(duped_str);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001008 return node;
1009}
1010
Julius Werner6d5695f2019-05-06 19:23:28 -07001011/*
1012 * Find a node from an alias
1013 *
1014 * @param tree The device tree.
1015 * @param alias The alias name.
1016 * @return The found node, or NULL.
1017 */
1018struct device_tree_node *dt_find_node_by_alias(struct device_tree *tree,
1019 const char *alias)
1020{
1021 struct device_tree_node *node;
1022 const char *alias_path;
1023
1024 node = dt_find_node_by_path(tree, "/aliases", NULL, NULL, 0);
1025 if (!node)
1026 return NULL;
1027
1028 alias_path = dt_find_string_prop(node, alias);
1029 if (!alias_path)
1030 return NULL;
1031
1032 return dt_find_node_by_path(tree, alias_path, NULL, NULL, 0);
1033}
1034
Julius Werner6702b682019-05-03 18:13:53 -07001035struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root,
1036 uint32_t phandle)
1037{
1038 if (!root)
1039 return NULL;
1040
1041 if (root->phandle == phandle)
1042 return root;
1043
1044 struct device_tree_node *node;
1045 struct device_tree_node *result;
1046 list_for_each(node, root->children, list_node) {
1047 result = dt_find_node_by_phandle(node, phandle);
1048 if (result)
1049 return result;
1050 }
1051
1052 return NULL;
1053}
1054
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001055/*
1056 * Check if given node is compatible.
1057 *
1058 * @param node The node which is to be checked for compatible property.
1059 * @param compat The compatible string to match.
1060 * @return 1 = compatible, 0 = not compatible.
1061 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001062static int dt_check_compat_match(struct device_tree_node *node,
1063 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001064{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001065 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001066
1067 list_for_each(prop, node->properties, list_node) {
1068 if (!strcmp("compatible", prop->prop.name)) {
1069 size_t bytes = prop->prop.size;
1070 const char *str = prop->prop.data;
1071 while (bytes > 0) {
1072 if (!strncmp(compat, str, bytes))
1073 return 1;
1074 size_t len = strnlen(str, bytes) + 1;
1075 if (bytes <= len)
1076 break;
1077 str += len;
1078 bytes -= len;
1079 }
1080 break;
1081 }
1082 }
1083
1084 return 0;
1085}
1086
1087/*
1088 * Find a node from a compatible string, in the subtree of a parent node.
1089 *
1090 * @param parent The parent node under which to look.
1091 * @param compat The compatible string to find.
1092 * @return The found node, or NULL.
1093 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001094struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
1095 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001096{
Julius Werner23df4772019-05-17 22:50:18 -07001097 /* Check if the parent node itself is compatible. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001098 if (dt_check_compat_match(parent, compat))
1099 return parent;
1100
Patrick Rudolph666c1722018-04-03 09:57:33 +02001101 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001102 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001103 struct device_tree_node *found = dt_find_compat(child, compat);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001104 if (found)
1105 return found;
1106 }
1107
1108 return NULL;
1109}
1110
1111/*
Martin Roth0949e732021-10-01 14:28:22 -06001112 * Find the next compatible child of a given parent. All children up to the
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001113 * child passed in by caller are ignored. If child is NULL, it considers all the
1114 * children to find the first child which is compatible.
1115 *
1116 * @param parent The parent node under which to look.
1117 * @param child The child node to start search from (exclusive). If NULL
1118 * consider all children.
1119 * @param compat The compatible string to find.
1120 * @return The found node, or NULL.
1121 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001122struct device_tree_node *
1123dt_find_next_compat_child(struct device_tree_node *parent,
1124 struct device_tree_node *child,
1125 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001126{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001127 struct device_tree_node *next;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001128 int ignore = 0;
1129
1130 if (child)
1131 ignore = 1;
1132
1133 list_for_each(next, parent->children, list_node) {
1134 if (ignore) {
1135 if (child == next)
1136 ignore = 0;
1137 continue;
1138 }
1139
1140 if (dt_check_compat_match(next, compat))
1141 return next;
1142 }
1143
1144 return NULL;
1145}
1146
1147/*
1148 * Find a node with matching property value, in the subtree of a parent node.
1149 *
1150 * @param parent The parent node under which to look.
1151 * @param name The property name to look for.
1152 * @param data The property value to look for.
1153 * @param size The property size.
1154 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001155struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
1156 const char *name, void *data,
1157 size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001158{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001159 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001160
1161 /* Check if parent itself has the required property value. */
1162 list_for_each(prop, parent->properties, list_node) {
1163 if (!strcmp(name, prop->prop.name)) {
1164 size_t bytes = prop->prop.size;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001165 const void *prop_data = prop->prop.data;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001166 if (size != bytes)
1167 break;
1168 if (!memcmp(data, prop_data, size))
1169 return parent;
1170 break;
1171 }
1172 }
1173
Patrick Rudolph666c1722018-04-03 09:57:33 +02001174 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001175 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001176 struct device_tree_node *found = dt_find_prop_value(child, name,
1177 data, size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001178 if (found)
1179 return found;
1180 }
1181 return NULL;
1182}
1183
1184/*
1185 * Write an arbitrary sized big-endian integer into a pointer.
1186 *
1187 * @param dest Pointer to the DT property data buffer to write.
Elyes HAOUAS1ec76442018-08-07 12:20:04 +02001188 * @param src The integer to write (in CPU endianness).
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001189 * @param length the length of the destination integer in bytes.
1190 */
1191void dt_write_int(u8 *dest, u64 src, size_t length)
1192{
1193 while (length--) {
1194 dest[length] = (u8)src;
1195 src >>= 8;
1196 }
1197}
1198
1199/*
Patrick Rudolph5ccc7312018-05-30 15:05:28 +02001200 * Delete a property by name in a given node if it exists.
1201 *
1202 * @param node The device tree node to operate on.
1203 * @param name The name of the property to delete.
1204 */
1205void dt_delete_prop(struct device_tree_node *node, const char *name)
1206{
1207 struct device_tree_property *prop;
1208
1209 list_for_each(prop, node->properties, list_node) {
1210 if (!strcmp(prop->prop.name, name)) {
1211 list_remove(&prop->list_node);
1212 return;
1213 }
1214 }
1215}
1216
1217/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001218 * Add an arbitrary property to a node, or update it if it already exists.
1219 *
1220 * @param node The device tree node to add to.
1221 * @param name The name of the new property.
1222 * @param data The raw data blob to be stored in the property.
1223 * @param size The size of data in bytes.
1224 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001225void dt_add_bin_prop(struct device_tree_node *node, const char *name,
Julius Werner0e9116f2019-05-13 17:30:31 -07001226 void *data, size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001227{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001228 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001229
1230 list_for_each(prop, node->properties, list_node) {
1231 if (!strcmp(prop->prop.name, name)) {
1232 prop->prop.data = data;
1233 prop->prop.size = size;
1234 return;
1235 }
1236 }
1237
Julius Werner9636a102019-05-03 17:36:43 -07001238 prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001239 list_insert_after(&prop->list_node, &node->properties);
1240 prop->prop.name = name;
1241 prop->prop.data = data;
1242 prop->prop.size = size;
1243}
1244
1245/*
1246 * Find given string property in a node and return its content.
1247 *
1248 * @param node The device tree node to search.
1249 * @param name The name of the property.
1250 * @return The found string, or NULL.
1251 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001252const char *dt_find_string_prop(const struct device_tree_node *node,
1253 const char *name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001254{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001255 const void *content;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001256 size_t size;
1257
1258 dt_find_bin_prop(node, name, &content, &size);
1259
1260 return content;
1261}
1262
1263/*
1264 * Find given property in a node.
1265 *
1266 * @param node The device tree node to search.
1267 * @param name The name of the property.
1268 * @param data Pointer to return raw data blob in the property.
1269 * @param size Pointer to return the size of data in bytes.
1270 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001271void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
1272 const void **data, size_t *size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001273{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001274 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001275
1276 *data = NULL;
1277 *size = 0;
1278
1279 list_for_each(prop, node->properties, list_node) {
1280 if (!strcmp(prop->prop.name, name)) {
1281 *data = prop->prop.data;
1282 *size = prop->prop.size;
1283 return;
1284 }
1285 }
1286}
1287
1288/*
1289 * Add a string property to a node, or update it if it already exists.
1290 *
1291 * @param node The device tree node to add to.
1292 * @param name The name of the new property.
1293 * @param str The zero-terminated string to be stored in the property.
1294 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001295void dt_add_string_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001296 const char *str)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001297{
Julius Werner0e9116f2019-05-13 17:30:31 -07001298 dt_add_bin_prop(node, name, (char *)str, strlen(str) + 1);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001299}
1300
1301/*
1302 * Add a 32-bit integer property to a node, or update it if it already exists.
1303 *
1304 * @param node The device tree node to add to.
1305 * @param name The name of the new property.
1306 * @param val The integer to be stored in the property.
1307 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001308void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001309{
Julius Werner9636a102019-05-03 17:36:43 -07001310 u32 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph666c1722018-04-03 09:57:33 +02001311 *val_ptr = htobe32(val);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001312 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
1313}
1314
1315/*
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +02001316 * Add a 64-bit integer property to a node, or update it if it already exists.
1317 *
1318 * @param node The device tree node to add to.
1319 * @param name The name of the new property.
1320 * @param val The integer to be stored in the property.
1321 */
1322void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
1323{
Julius Werner9636a102019-05-03 17:36:43 -07001324 u64 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +02001325 *val_ptr = htobe64(val);
1326 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
1327}
1328
1329/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001330 * Add a 'reg' address list property to a node, or update it if it exists.
1331 *
1332 * @param node The device tree node to add to.
Maximilian Brune33079b82024-03-04 15:34:41 +01001333 * @param regions Array of address values to be stored in the property.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001334 * @param sizes Array of corresponding size values to 'addrs'.
1335 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
1336 * @param addr_cells Value of #address-cells property valid for this node.
1337 * @param size_cells Value of #size-cells property valid for this node.
1338 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001339void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001340 int count, u32 addr_cells, u32 size_cells)
1341{
1342 int i;
1343 size_t length = (addr_cells + size_cells) * sizeof(u32) * count;
Julius Werner9636a102019-05-03 17:36:43 -07001344 u8 *data = xmalloc(length);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001345 u8 *cur = data;
1346
1347 for (i = 0; i < count; i++) {
1348 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
1349 cur += addr_cells * sizeof(u32);
1350 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
1351 cur += size_cells * sizeof(u32);
1352 }
1353
1354 dt_add_bin_prop(node, "reg", data, length);
1355}
1356
1357/*
1358 * Fixups to apply to a kernel's device tree before booting it.
1359 */
1360
Patrick Rudolph666c1722018-04-03 09:57:33 +02001361struct list_node device_tree_fixups;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001362
Patrick Rudolph666c1722018-04-03 09:57:33 +02001363int dt_apply_fixups(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001364{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001365 struct device_tree_fixup *fixup;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001366 list_for_each(fixup, device_tree_fixups, list_node) {
1367 assert(fixup->fixup);
1368 if (fixup->fixup(fixup, tree))
1369 return 1;
1370 }
1371 return 0;
1372}
1373
Patrick Rudolph666c1722018-04-03 09:57:33 +02001374int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001375 void *data, size_t data_size, int create)
1376{
1377 char *path_copy, *prop_name;
Patrick Rudolph666c1722018-04-03 09:57:33 +02001378 struct device_tree_node *dt_node;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001379
1380 path_copy = strdup(path);
1381
1382 if (!path_copy) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001383 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
1384 path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001385 return 1;
1386 }
1387
1388 prop_name = strrchr(path_copy, '/');
1389 if (!prop_name) {
Patrick Rudolph679d6242018-07-11 13:53:04 +02001390 free(path_copy);
Patrick Rudolph666c1722018-04-03 09:57:33 +02001391 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001392 return 1;
1393 }
1394
1395 *prop_name++ = '\0'; /* Separate path from the property name. */
1396
Julius Wernerf36d53c2019-05-03 18:23:34 -07001397 dt_node = dt_find_node_by_path(tree, path_copy, NULL,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001398 NULL, create);
1399
1400 if (!dt_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001401 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001402 create ? "create" : "find", path_copy);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001403 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001404 return 1;
1405 }
1406
1407 dt_add_bin_prop(dt_node, prop_name, data, data_size);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001408 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001409
1410 return 0;
1411}
1412
1413/*
1414 * Prepare the /reserved-memory/ node.
1415 *
1416 * Technically, this can be called more than one time, to init and/or retrieve
1417 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
1418 *
1419 * @tree: Device tree to add/retrieve from.
1420 * @return: The /reserved-memory/ node (or NULL, if error).
1421 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001422struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001423{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001424 struct device_tree_node *reserved;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001425 u32 addr = 0, size = 0;
1426
Julius Wernerfbec63d2019-05-03 18:29:28 -07001427 reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001428 &size, 1);
1429 if (!reserved)
1430 return NULL;
1431
Julius Werner23df4772019-05-17 22:50:18 -07001432 /* Binding doc says this should have the same #{address,size}-cells as
1433 the root. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001434 dt_add_u32_prop(reserved, "#address-cells", addr);
1435 dt_add_u32_prop(reserved, "#size-cells", size);
Julius Werner23df4772019-05-17 22:50:18 -07001436 /* Binding doc says this should be empty (1:1 mapping from root). */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001437 dt_add_bin_prop(reserved, "ranges", NULL, 0);
1438
1439 return reserved;
1440}
Julius Werner735ddc92019-05-07 17:05:28 -07001441
1442/*
1443 * Increment a single phandle in prop at a given offset by a given adjustment.
1444 *
1445 * @param prop Property whose phandle should be adjusted.
1446 * @param adjustment Value that should be added to the existing phandle.
1447 * @param offset Byte offset of the phandle in the property data.
1448 *
1449 * @return New phandle value, or 0 on error.
1450 */
1451static uint32_t dt_adjust_phandle(struct device_tree_property *prop,
1452 uint32_t adjustment, uint32_t offset)
1453{
1454 if (offset + 4 > prop->prop.size)
1455 return 0;
1456
1457 uint32_t phandle = be32dec(prop->prop.data + offset);
1458 if (phandle == 0 ||
1459 phandle == FDT_PHANDLE_ILLEGAL ||
1460 phandle == 0xffffffff)
1461 return 0;
1462
1463 phandle += adjustment;
1464 if (phandle >= FDT_PHANDLE_ILLEGAL)
1465 return 0;
1466
1467 be32enc(prop->prop.data + offset, phandle);
1468 return phandle;
1469}
1470
1471/*
1472 * Adjust all phandles in subtree by adding a new base offset.
1473 *
1474 * @param node Root node of the subtree to work on.
1475 * @param base New phandle base to be added to all phandles.
1476 *
1477 * @return New highest phandle in the subtree, or 0 on error.
1478 */
1479static uint32_t dt_adjust_all_phandles(struct device_tree_node *node,
1480 uint32_t base)
1481{
Julius Werner23df4772019-05-17 22:50:18 -07001482 uint32_t new_max = MAX(base, 1); /* make sure we don't return 0 */
Julius Werner735ddc92019-05-07 17:05:28 -07001483 struct device_tree_property *prop;
1484 struct device_tree_node *child;
1485
1486 if (!node)
1487 return new_max;
1488
1489 list_for_each(prop, node->properties, list_node)
1490 if (dt_prop_is_phandle(prop)) {
1491 node->phandle = dt_adjust_phandle(prop, base, 0);
1492 if (!node->phandle)
1493 return 0;
1494 new_max = MAX(new_max, node->phandle);
Julius Werner23df4772019-05-17 22:50:18 -07001495 } /* no break -- can have more than one phandle prop */
Julius Werner735ddc92019-05-07 17:05:28 -07001496
1497 list_for_each(child, node->children, list_node)
1498 new_max = MAX(new_max, dt_adjust_all_phandles(child, base));
1499
1500 return new_max;
1501}
1502
1503/*
1504 * Apply a /__local_fixup__ subtree to the corresponding overlay subtree.
1505 *
1506 * @param node Root node of the overlay subtree to fix up.
1507 * @param node Root node of the /__local_fixup__ subtree.
1508 * @param base Adjustment that was added to phandles in the overlay.
1509 *
1510 * @return 0 on success, -1 on error.
1511 */
1512static int dt_fixup_locals(struct device_tree_node *node,
1513 struct device_tree_node *fixup, uint32_t base)
1514{
1515 struct device_tree_property *prop;
1516 struct device_tree_property *fixup_prop;
1517 struct device_tree_node *child;
1518 struct device_tree_node *fixup_child;
1519 int i;
1520
Julius Werner23df4772019-05-17 22:50:18 -07001521 /*
1522 * For local fixups the /__local_fixup__ subtree contains the same node
1523 * hierarchy as the main tree we're fixing up. Each property contains
1524 * the fixup offsets for the respective property in the main tree. For
1525 * each property in the fixup node, find the corresponding property in
1526 * the base node and apply fixups to all offsets it specifies.
1527 */
Julius Werner735ddc92019-05-07 17:05:28 -07001528 list_for_each(fixup_prop, fixup->properties, list_node) {
1529 struct device_tree_property *base_prop = NULL;
1530 list_for_each(prop, node->properties, list_node)
1531 if (!strcmp(prop->prop.name, fixup_prop->prop.name)) {
1532 base_prop = prop;
1533 break;
1534 }
1535
Julius Werner23df4772019-05-17 22:50:18 -07001536 /* We should always find a corresponding base prop for a fixup,
1537 and fixup props contain a list of 32-bit fixup offsets. */
Julius Werner735ddc92019-05-07 17:05:28 -07001538 if (!base_prop || fixup_prop->prop.size % sizeof(uint32_t))
1539 return -1;
1540
1541 for (i = 0; i < fixup_prop->prop.size; i += sizeof(uint32_t))
1542 if (!dt_adjust_phandle(base_prop, base, be32dec(
1543 fixup_prop->prop.data + i)))
1544 return -1;
1545 }
1546
Julius Werner23df4772019-05-17 22:50:18 -07001547 /* Now recursively descend both the base tree and the /__local_fixups__
1548 subtree in sync to apply all fixups. */
Julius Werner735ddc92019-05-07 17:05:28 -07001549 list_for_each(fixup_child, fixup->children, list_node) {
1550 struct device_tree_node *base_child = NULL;
1551 list_for_each(child, node->children, list_node)
1552 if (!strcmp(child->name, fixup_child->name)) {
1553 base_child = child;
1554 break;
1555 }
1556
Julius Werner23df4772019-05-17 22:50:18 -07001557 /* All fixup nodes should have a corresponding base node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001558 if (!base_child)
1559 return -1;
1560
1561 if (dt_fixup_locals(base_child, fixup_child, base) < 0)
1562 return -1;
1563 }
1564
1565 return 0;
1566}
1567
1568/*
1569 * Update all /__symbols__ properties in an overlay that start with
1570 * "/fragment@X/__overlay__" with corresponding path prefix in the base tree.
1571 *
1572 * @param symbols /__symbols__ done to update.
1573 * @param fragment /fragment@X node that references to should be updated.
1574 * @param base_path Path of base tree node that the fragment overlaid.
1575 */
1576static void dt_fix_symbols(struct device_tree_node *symbols,
1577 struct device_tree_node *fragment,
1578 const char *base_path)
1579{
1580 struct device_tree_property *prop;
Julius Werner23df4772019-05-17 22:50:18 -07001581 char buf[512]; /* Should be enough for maximum DT path length? */
1582 char node_path[64]; /* easily enough for /fragment@XXXX/__overlay__ */
Julius Werner735ddc92019-05-07 17:05:28 -07001583
Julius Werner23df4772019-05-17 22:50:18 -07001584 if (!symbols) /* If the overlay has no /__symbols__ node, we're done! */
Julius Werner735ddc92019-05-07 17:05:28 -07001585 return;
1586
1587 int len = snprintf(node_path, sizeof(node_path), "/%s/__overlay__",
1588 fragment->name);
1589
1590 list_for_each(prop, symbols->properties, list_node)
1591 if (!strncmp(prop->prop.data, node_path, len)) {
1592 prop->prop.size = snprintf(buf, sizeof(buf), "%s%s",
1593 base_path, (char *)prop->prop.data + len) + 1;
1594 free(prop->prop.data);
1595 prop->prop.data = strdup(buf);
1596 }
1597}
1598
1599/*
1600 * Fix up overlay according to a property in /__fixup__. If the fixed property
1601 * is a /fragment@X:target, also update /__symbols__ references to fragment.
1602 *
1603 * @params overlay Overlay to fix up.
1604 * @params fixup /__fixup__ property.
1605 * @params phandle phandle value to insert where the fixup points to.
1606 * @params base_path Path to the base DT node that the fixup points to.
1607 * @params overlay_symbols /__symbols__ node of the overlay.
1608 *
1609 * @return 0 on success, -1 on error.
1610 */
1611static int dt_fixup_external(struct device_tree *overlay,
1612 struct device_tree_property *fixup,
1613 uint32_t phandle, const char *base_path,
1614 struct device_tree_node *overlay_symbols)
1615{
1616 struct device_tree_property *prop;
1617
Julius Werner23df4772019-05-17 22:50:18 -07001618 /* External fixup properties are encoded as "<path>:<prop>:<offset>". */
Julius Werner735ddc92019-05-07 17:05:28 -07001619 char *entry = fixup->prop.data;
1620 while ((void *)entry < fixup->prop.data + fixup->prop.size) {
Julius Werner23df4772019-05-17 22:50:18 -07001621 /* okay to destroy fixup property value, won't need it again */
Julius Werner735ddc92019-05-07 17:05:28 -07001622 char *node_path = entry;
1623 entry = strchr(node_path, ':');
1624 if (!entry)
1625 return -1;
1626 *entry++ = '\0';
1627
1628 char *prop_name = entry;
1629 entry = strchr(prop_name, ':');
1630 if (!entry)
1631 return -1;
1632 *entry++ = '\0';
1633
1634 struct device_tree_node *ovl_node = dt_find_node_by_path(
1635 overlay, node_path, NULL, NULL, 0);
1636 if (!ovl_node || !isdigit(*entry))
1637 return -1;
1638
1639 struct device_tree_property *ovl_prop = NULL;
1640 list_for_each(prop, ovl_node->properties, list_node)
1641 if (!strcmp(prop->prop.name, prop_name)) {
1642 ovl_prop = prop;
1643 break;
1644 }
1645
Julius Werner23df4772019-05-17 22:50:18 -07001646 /* Move entry to first char after number, must be a '\0'. */
Julius Werner735ddc92019-05-07 17:05:28 -07001647 uint32_t offset = skip_atoi(&entry);
1648 if (!ovl_prop || offset + 4 > ovl_prop->prop.size || entry[0])
1649 return -1;
Julius Werner23df4772019-05-17 22:50:18 -07001650 entry++; /* jump over '\0' to potential next fixup */
Julius Werner735ddc92019-05-07 17:05:28 -07001651
1652 be32enc(ovl_prop->prop.data + offset, phandle);
1653
Julius Werner23df4772019-05-17 22:50:18 -07001654 /* If this is a /fragment@X:target property, update references
1655 to this fragment in the overlay __symbols__ now. */
Julius Werner735ddc92019-05-07 17:05:28 -07001656 if (offset == 0 && !strcmp(prop_name, "target") &&
Julius Werner23df4772019-05-17 22:50:18 -07001657 !strchr(node_path + 1, '/')) /* only toplevel nodes */
Julius Werner735ddc92019-05-07 17:05:28 -07001658 dt_fix_symbols(overlay_symbols, ovl_node, base_path);
1659 }
1660
1661 return 0;
1662}
1663
1664/*
1665 * Apply all /__fixup__ properties in the overlay. This will destroy the
1666 * property data in /__fixup__ and it should not be accessed again.
1667 *
1668 * @params tree Base device tree that the overlay updates.
1669 * @params symbols /__symbols__ node of the base device tree.
1670 * @params overlay Overlay to fix up.
1671 * @params fixups /__fixup__ node in the overlay.
1672 * @params overlay_symbols /__symbols__ node of the overlay.
1673 *
1674 * @return 0 on success, -1 on error.
1675 */
1676static int dt_fixup_all_externals(struct device_tree *tree,
1677 struct device_tree_node *symbols,
1678 struct device_tree *overlay,
1679 struct device_tree_node *fixups,
1680 struct device_tree_node *overlay_symbols)
1681{
1682 struct device_tree_property *fix;
1683
Julius Werner23df4772019-05-17 22:50:18 -07001684 /* If we have any external fixups, base tree must have /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001685 if (!symbols)
1686 return -1;
1687
Julius Werner23df4772019-05-17 22:50:18 -07001688 /*
1689 * Unlike /__local_fixups__, /__fixups__ is not a whole subtree that
1690 * mirrors the node hierarchy. It's just a directory of fixup properties
1691 * that each directly contain all information necessary to apply them.
1692 */
Julius Werner735ddc92019-05-07 17:05:28 -07001693 list_for_each(fix, fixups->properties, list_node) {
Julius Werner23df4772019-05-17 22:50:18 -07001694 /* The name of a fixup property is the label of the node we want
1695 a property to phandle-reference. Look up in /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001696 const char *path = dt_find_string_prop(symbols, fix->prop.name);
1697 if (!path)
1698 return -1;
1699
Elyes HAOUAS0afaff22021-01-16 15:02:31 +01001700 /* Find node the label pointed to figure out its phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001701 struct device_tree_node *node = dt_find_node_by_path(tree, path,
1702 NULL, NULL, 0);
1703 if (!node)
1704 return -1;
1705
Julius Werner23df4772019-05-17 22:50:18 -07001706 /* Write into the overlay property(s) pointing to that node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001707 if (dt_fixup_external(overlay, fix, node->phandle,
1708 path, overlay_symbols) < 0)
1709 return -1;
1710 }
1711
1712 return 0;
1713}
1714
1715/*
1716 * Copy all nodes and properties from one DT subtree into another. This is a
1717 * shallow copy so both trees will point to the same property data afterwards.
1718 *
1719 * @params dst Destination subtree to copy into.
1720 * @params src Source subtree to copy from.
1721 * @params upd 1 to overwrite same-name properties, 0 to discard them.
1722 */
1723static void dt_copy_subtree(struct device_tree_node *dst,
1724 struct device_tree_node *src, int upd)
1725{
1726 struct device_tree_property *prop;
1727 struct device_tree_property *src_prop;
1728 list_for_each(src_prop, src->properties, list_node) {
1729 if (dt_prop_is_phandle(src_prop) ||
1730 !strcmp(src_prop->prop.name, "name")) {
1731 printk(BIOS_DEBUG,
1732 "WARNING: ignoring illegal overlay prop '%s'\n",
1733 src_prop->prop.name);
1734 continue;
1735 }
1736
1737 struct device_tree_property *dst_prop = NULL;
1738 list_for_each(prop, dst->properties, list_node)
1739 if (!strcmp(prop->prop.name, src_prop->prop.name)) {
1740 dst_prop = prop;
1741 break;
1742 }
1743
1744 if (dst_prop) {
1745 if (!upd) {
1746 printk(BIOS_DEBUG,
1747 "WARNING: ignoring prop update '%s'\n",
1748 src_prop->prop.name);
1749 continue;
1750 }
1751 } else {
1752 dst_prop = xzalloc(sizeof(*dst_prop));
1753 list_insert_after(&dst_prop->list_node,
1754 &dst->properties);
1755 }
1756
1757 dst_prop->prop = src_prop->prop;
1758 }
1759
1760 struct device_tree_node *node;
1761 struct device_tree_node *src_node;
1762 list_for_each(src_node, src->children, list_node) {
1763 struct device_tree_node *dst_node = NULL;
1764 list_for_each(node, dst->children, list_node)
1765 if (!strcmp(node->name, src_node->name)) {
1766 dst_node = node;
1767 break;
1768 }
1769
1770 if (!dst_node) {
1771 dst_node = xzalloc(sizeof(*dst_node));
1772 *dst_node = *src_node;
1773 list_insert_after(&dst_node->list_node, &dst->children);
1774 } else {
1775 dt_copy_subtree(dst_node, src_node, upd);
1776 }
1777 }
1778}
1779
1780/*
1781 * Apply an overlay /fragment@X node to a base device tree.
1782 *
1783 * @param tree Base device tree.
1784 * @param fragment /fragment@X node.
1785 * @params overlay_symbols /__symbols__ node of the overlay.
1786 *
1787 * @return 0 on success, -1 on error.
1788 */
1789static int dt_import_fragment(struct device_tree *tree,
1790 struct device_tree_node *fragment,
1791 struct device_tree_node *overlay_symbols)
1792{
Julius Werner23df4772019-05-17 22:50:18 -07001793 /* The actual overlaid nodes/props are in an __overlay__ child node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001794 static const char *overlay_path[] = { "__overlay__", NULL };
1795 struct device_tree_node *overlay = dt_find_node(fragment, overlay_path,
1796 NULL, NULL, 0);
1797
Julius Werner23df4772019-05-17 22:50:18 -07001798 /* If it doesn't have an __overlay__ child, it's not a fragment. */
Julius Werner735ddc92019-05-07 17:05:28 -07001799 if (!overlay)
1800 return 0;
1801
Julius Werner23df4772019-05-17 22:50:18 -07001802 /* Target node of the fragment can be given by path or by phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001803 struct device_tree_property *prop;
1804 struct device_tree_property *phandle = NULL;
1805 struct device_tree_property *path = NULL;
1806 list_for_each(prop, fragment->properties, list_node) {
1807 if (!strcmp(prop->prop.name, "target")) {
1808 phandle = prop;
Julius Werner23df4772019-05-17 22:50:18 -07001809 break; /* phandle target has priority, stop looking */
Julius Werner735ddc92019-05-07 17:05:28 -07001810 }
1811 if (!strcmp(prop->prop.name, "target-path"))
1812 path = prop;
1813 }
1814
1815 struct device_tree_node *target = NULL;
1816 if (phandle) {
1817 if (phandle->prop.size != sizeof(uint32_t))
1818 return -1;
1819 target = dt_find_node_by_phandle(tree->root,
1820 be32dec(phandle->prop.data));
Julius Werner23df4772019-05-17 22:50:18 -07001821 /* Symbols already updated as part of dt_fixup_external(). */
Julius Werner735ddc92019-05-07 17:05:28 -07001822 } else if (path) {
1823 target = dt_find_node_by_path(tree, path->prop.data,
1824 NULL, NULL, 0);
1825 dt_fix_symbols(overlay_symbols, fragment, path->prop.data);
1826 }
1827 if (!target)
1828 return -1;
1829
1830 dt_copy_subtree(target, overlay, 1);
1831 return 0;
1832}
1833
1834/*
1835 * Apply a device tree overlay to a base device tree. This will
1836 * destroy/incorporate the overlay data, so it should not be freed or reused.
1837 * See dtc.git/Documentation/dt-object-internal.txt for overlay format details.
1838 *
1839 * @param tree Unflattened base device tree to add the overlay into.
1840 * @param overlay Unflattened overlay device tree to apply to the base.
1841 *
1842 * @return 0 on success, -1 on error.
1843 */
1844int dt_apply_overlay(struct device_tree *tree, struct device_tree *overlay)
1845{
Julius Werner23df4772019-05-17 22:50:18 -07001846 /*
1847 * First, we need to make sure phandles inside the overlay don't clash
1848 * with those in the base tree. We just define the highest phandle value
1849 * in the base tree as the "phandle offset" for this overlay and
1850 * increment all phandles in it by that value.
1851 */
Julius Werner735ddc92019-05-07 17:05:28 -07001852 uint32_t phandle_base = tree->max_phandle;
1853 uint32_t new_max = dt_adjust_all_phandles(overlay->root, phandle_base);
1854 if (!new_max) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001855 printk(BIOS_ERR, "invalid phandles in overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001856 return -1;
1857 }
1858 tree->max_phandle = new_max;
1859
Julius Werner23df4772019-05-17 22:50:18 -07001860 /* Now that we changed phandles in the overlay, we need to update any
1861 nodes referring to them. Those are listed in /__local_fixups__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001862 struct device_tree_node *local_fixups = dt_find_node_by_path(overlay,
1863 "/__local_fixups__", NULL, NULL, 0);
1864 if (local_fixups && dt_fixup_locals(overlay->root, local_fixups,
1865 phandle_base) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001866 printk(BIOS_ERR, "invalid local fixups in overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001867 return -1;
1868 }
1869
Julius Werner23df4772019-05-17 22:50:18 -07001870 /*
1871 * Besides local phandle references (from nodes within the overlay to
1872 * other nodes within the overlay), the overlay may also contain phandle
1873 * references to the base tree. These are stored with invalid values and
1874 * must be updated now. /__symbols__ contains a list of all labels in
1875 * the base tree, and /__fixups__ describes all nodes in the overlay
1876 * that contain external phandle references.
1877 * We also take this opportunity to update all /fragment@X/__overlay__/
1878 * prefixes in the overlay's /__symbols__ node to the correct path that
1879 * the fragment will be placed in later, since this is the only step
1880 * where we have all necessary information for that easily available.
1881 */
Julius Werner735ddc92019-05-07 17:05:28 -07001882 struct device_tree_node *symbols = dt_find_node_by_path(tree,
1883 "/__symbols__", NULL, NULL, 0);
1884 struct device_tree_node *fixups = dt_find_node_by_path(overlay,
1885 "/__fixups__", NULL, NULL, 0);
1886 struct device_tree_node *overlay_symbols = dt_find_node_by_path(overlay,
1887 "/__symbols__", NULL, NULL, 0);
1888 if (fixups && dt_fixup_all_externals(tree, symbols, overlay,
1889 fixups, overlay_symbols) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001890 printk(BIOS_ERR, "cannot match external fixups from overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001891 return -1;
1892 }
1893
Julius Werner23df4772019-05-17 22:50:18 -07001894 /* After all this fixing up, we can finally merge overlay into the tree
1895 (one fragment at a time, because for some reason it's split up). */
Julius Werner735ddc92019-05-07 17:05:28 -07001896 struct device_tree_node *fragment;
1897 list_for_each(fragment, overlay->root->children, list_node)
1898 if (dt_import_fragment(tree, fragment, overlay_symbols) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001899 printk(BIOS_ERR, "bad DT fragment '%s'\n",
Julius Werner735ddc92019-05-07 17:05:28 -07001900 fragment->name);
1901 return -1;
1902 }
1903
Julius Werner23df4772019-05-17 22:50:18 -07001904 /*
1905 * We need to also update /__symbols__ to include labels from this
1906 * overlay, in case we want to load further overlays with external
1907 * phandle references to it. If the base tree already has a /__symbols__
1908 * we merge them together, otherwise we just insert the overlay's
1909 * /__symbols__ node into the base tree root.
1910 */
Julius Werner735ddc92019-05-07 17:05:28 -07001911 if (overlay_symbols) {
1912 if (symbols)
1913 dt_copy_subtree(symbols, overlay_symbols, 0);
1914 else
1915 list_insert_after(&overlay_symbols->list_node,
1916 &tree->root->children);
1917 }
1918
1919 return 0;
1920}