blob: 2bfd5dd3760e16f030351f96bc6d2f30e623f0f0 [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>
Patrick Rudolph666c1722018-04-03 09:57:33 +020011#include <string.h>
12#include <stddef.h>
13#include <stdlib.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020014
Maximilian Brune33079b82024-03-04 15:34:41 +010015#define FDT_PATH_MAX_DEPTH 10 // should be a good enough upper bound
16#define FDT_PATH_MAX_LEN 128 // should be a good enough upper bound
17
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020018/*
19 * Functions for picking apart flattened trees.
20 */
21
Maximilian Brune33079b82024-03-04 15:34:41 +010022static int fdt_skip_nops(const void *blob, uint32_t offset)
23{
24 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
25
26 int index = 0;
27 while (be32toh(ptr[index]) == FDT_TOKEN_NOP)
28 index++;
29
30 return index * sizeof(uint32_t);
31}
32
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020033int fdt_next_property(const void *blob, uint32_t offset,
34 struct fdt_property *prop)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020035{
Patrick Rudolph666c1722018-04-03 09:57:33 +020036 struct fdt_header *header = (struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020037 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
38
Maximilian Brune33079b82024-03-04 15:34:41 +010039 // skip NOP tokens
40 offset += fdt_skip_nops(blob, offset);
41
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020042 int index = 0;
Patrick Rudolph666c1722018-04-03 09:57:33 +020043 if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020044 return 0;
45
Patrick Rudolph666c1722018-04-03 09:57:33 +020046 uint32_t size = be32toh(ptr[index++]);
47 uint32_t name_offset = be32toh(ptr[index++]);
48 name_offset += be32toh(header->strings_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020049
50 if (prop) {
51 prop->name = (char *)((uint8_t *)blob + name_offset);
52 prop->data = &ptr[index];
53 prop->size = size;
54 }
55
Patrick Rudolph666c1722018-04-03 09:57:33 +020056 index += DIV_ROUND_UP(size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020057
Patrick Rudolph666c1722018-04-03 09:57:33 +020058 return index * sizeof(uint32_t);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020059}
60
Maximilian Brune33079b82024-03-04 15:34:41 +010061/*
62 * fdt_next_node_name reads a node name
63 *
64 * @params blob address of FDT
65 * @params offset offset to the node to read the name from
66 * @params name parameter to hold the name that has been read or NULL
67 *
68 * @returns Either 0 on error or offset to the properties that come after the node name
69 */
70int fdt_next_node_name(const void *blob, uint32_t offset, const char **name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020071{
Maximilian Brune33079b82024-03-04 15:34:41 +010072 // skip NOP tokens
73 offset += fdt_skip_nops(blob, offset);
74
75 char *ptr = ((char *)blob) + offset;
Julius Wernera5ea3a22019-05-07 17:38:12 -070076 if (be32dec(ptr) != FDT_TOKEN_BEGIN_NODE)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020077 return 0;
78
79 ptr += 4;
80 if (name)
Maximilian Brune33079b82024-03-04 15:34:41 +010081 *name = ptr;
82
83 return ALIGN_UP(strlen(ptr) + 1, 4) + 4;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020084}
85
Maximilian Brune33079b82024-03-04 15:34:41 +010086/*
87 * A utility function to skip past nodes in flattened trees.
88 */
89int fdt_skip_node(const void *blob, uint32_t start_offset)
Julius Werner6702b682019-05-03 18:13:53 -070090{
Maximilian Brune33079b82024-03-04 15:34:41 +010091 uint32_t offset = start_offset;
92
93 const char *name;
94 int size = fdt_next_node_name(blob, offset, &name);
95 if (!size)
96 return 0;
97 offset += size;
98
99 while ((size = fdt_next_property(blob, offset, NULL)))
100 offset += size;
101
102 while ((size = fdt_skip_node(blob, offset)))
103 offset += size;
104
105 // skip NOP tokens
106 offset += fdt_skip_nops(blob, offset);
107
108 return offset - start_offset + sizeof(uint32_t);
Julius Werner6702b682019-05-03 18:13:53 -0700109}
110
Maximilian Brune33079b82024-03-04 15:34:41 +0100111/*
112 * fdt_read_prop reads a property inside a node
113 *
114 * @params blob address of FDT
115 * @params node_offset offset to the node to read the property from
116 * @params prop_name name of the property to read
117 * @params fdt_prop property is saved inside this parameter
118 *
119 * @returns Either 0 if no property has been found or an offset that points to the location
120 * of the property
121 */
122u32 fdt_read_prop(const void *blob, u32 node_offset, const char *prop_name,
123 struct fdt_property *fdt_prop)
124{
125 u32 offset = node_offset;
126
127 offset += fdt_next_node_name(blob, offset, NULL); // skip node name
128
129 size_t size;
130 while ((size = fdt_next_property(blob, offset, fdt_prop))) {
131 if (strcmp(fdt_prop->name, prop_name) == 0)
132 return offset;
133 offset += size;
134 }
135 return 0; // property not found
136}
137
138/*
139 * fdt_read_reg_prop reads the reg property inside a node
140 *
141 * @params blob address of FDT
142 * @params node_offset offset to the node to read the reg property from
143 * @params addr_cells number of cells used for one address
144 * @params size_cells number of cells used for one size
145 * @params regions all regions that are read inside the reg property are saved inside
146 * this array
147 * @params regions_count maximum number of entries that can be saved inside the regions array.
148 *
149 * Returns: Either 0 on error or returns the number of regions put into the regions array.
150 */
151u32 fdt_read_reg_prop(const void *blob, u32 node_offset, u32 addr_cells, u32 size_cells,
152 struct device_tree_region regions[], size_t regions_count)
153{
154 struct fdt_property prop;
155 u32 offset = fdt_read_prop(blob, node_offset, "reg", &prop);
156
157 if (!offset) {
158 printk(BIOS_DEBUG, "no reg property found in node_offset: %x\n", node_offset);
159 return 0;
160 }
161
162 // we found the reg property, now need to parse all regions in 'reg'
163 size_t count = prop.size / (4 * addr_cells + 4 * size_cells);
164 if (count > regions_count) {
165 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);
166 count = regions_count;
167 }
168 if (addr_cells > 2 || size_cells > 2) {
169 printk(BIOS_ERR, "addr_cells (%d) or size_cells (%d) bigger than 2\n",
170 addr_cells, size_cells);
171 return 0;
172 }
173 uint32_t *ptr = prop.data;
174 for (int i = 0; i < count; i++) {
175 if (addr_cells == 1)
176 regions[i].addr = be32dec(ptr);
177 else if (addr_cells == 2)
178 regions[i].addr = be64dec(ptr);
179 ptr += addr_cells;
180 if (size_cells == 1)
181 regions[i].size = be32dec(ptr);
182 else if (size_cells == 2)
183 regions[i].size = be64dec(ptr);
184 ptr += size_cells;
185 }
186
187 return count; // return the number of regions found in the reg property
188}
189
190static u32 fdt_read_cell_props(const void *blob, u32 node_offset, u32 *addrcp, u32 *sizecp)
191{
192 struct fdt_property prop;
193 u32 offset = node_offset;
194 size_t size;
195 while ((size = fdt_next_property(blob, offset, &prop))) {
196 if (addrcp && !strcmp(prop.name, "#address-cells"))
197 *addrcp = be32dec(prop.data);
198 if (sizecp && !strcmp(prop.name, "#size-cells"))
199 *sizecp = be32dec(prop.data);
200 offset += size;
201 }
202 return offset;
203}
204
205/*
206 * fdt_find_node searches for a node relative to another node
207 *
208 * @params blob address of FDT
209 *
210 * @params parent_node_offset offset to node from which to traverse the tree
211 *
212 * @params path null terminated array of node names specifying a
213 * relative path (e.g: { "cpus", "cpu0", NULL })
214 *
215 * @params addrcp/sizecp If any address-cells and size-cells properties are found that are
216 * part of the parent node of the node we are looking, addrcp and sizecp
217 * are set to these respectively.
218 *
219 * @returns: Either 0 if no node has been found or the offset to the node found
220 */
221static u32 fdt_find_node(const void *blob, u32 parent_node_offset, char **path,
222 u32 *addrcp, u32 *sizecp)
223{
224 if (*path == NULL)
225 return parent_node_offset; // node found
226
227 size_t size = fdt_next_node_name(blob, parent_node_offset, NULL); // skip node name
228
229 /*
230 * get address-cells and size-cells properties while skipping the others.
231 * According to spec address-cells and size-cells are not inherited, but we
232 * intentionally follow the Linux implementation here and treat them as inheritable.
233 */
234 u32 node_offset = fdt_read_cell_props(blob, parent_node_offset + size, addrcp, sizecp);
235
236 const char *node_name;
237 // walk all children nodes
238 while ((size = fdt_next_node_name(blob, node_offset, &node_name))) {
239 if (!strcmp(*path, node_name)) {
240 // traverse one level deeper into the path
241 return fdt_find_node(blob, node_offset, path + 1, addrcp, sizecp);
242 }
243 // node is not the correct one. skip current node
244 node_offset += fdt_skip_node(blob, node_offset);
245 }
246
247 // we have searched everything and could not find a fitting node
248 return 0;
249}
250
251/*
252 * fdt_find_node_by_path finds a node behind a given node path
253 *
254 * @params blob address of FDT
255 * @params path absolute path to the node that should be searched for
256 *
257 * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells
258 * value found in the node of the node specified by node_offset. Either
259 * may be NULL to ignore. If no #address-cells and #size-cells is found
260 * default values of #address-cells=2 and #size-cells=1 are returned.
261 *
262 * @returns Either 0 on error or the offset to the node found behind the path
263 */
264u32 fdt_find_node_by_path(const void *blob, const char *path, u32 *addrcp, u32 *sizecp)
265{
266 // sanity check
267 if (path[0] != '/') {
268 printk(BIOS_ERR, "devicetree path must start with a /\n");
269 return 0;
270 }
271 if (!blob) {
272 printk(BIOS_ERR, "devicetree blob is NULL\n");
273 return 0;
274 }
275
276 if (addrcp)
277 *addrcp = 2;
278 if (sizecp)
279 *sizecp = 1;
280
281 struct fdt_header *fdt_hdr = (struct fdt_header *)blob;
282
283 /*
284 * split path into separate nodes
285 * e.g: "/cpus/cpu0" -> { "cpus", "cpu0" }
286 */
287 char *path_array[FDT_PATH_MAX_DEPTH];
288 size_t path_size = strlen(path);
289 assert(path_size < FDT_PATH_MAX_LEN);
290 char path_copy[FDT_PATH_MAX_LEN];
291 memcpy(path_copy, path, path_size + 1);
292 char *cur = path_copy;
293 int i;
294 for (i = 0; i < FDT_PATH_MAX_DEPTH; i++) {
295 path_array[i] = strtok_r(NULL, "/", &cur);
296 if (!path_array[i])
297 break;
298 }
299 assert(i < FDT_PATH_MAX_DEPTH);
300
301 return fdt_find_node(blob, be32toh(fdt_hdr->structure_offset), path_array, addrcp, sizecp);
302}
303
304/*
305 * fdt_find_subnodes_by_prefix finds a node with a given prefix relative to a parent node
306 *
307 * @params blob The FDT to search.
308 *
309 * @params node_offset offset to the node of which the children should be searched
310 *
311 * @params prefix A string to search for a node with a given prefix. This can for example
312 * be 'cpu' to look for all nodes matching this prefix. Only children of
313 * node_offset are searched. Therefore in order to search all nodes matching
314 * the 'cpu' prefix, node_offset should probably point to the 'cpus' node.
315 * An empty prefix ("") searches for all children nodes of node_offset.
316 *
317 * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells
318 * value found in the node of the node specified by node_offset. Either
319 * may be NULL to ignore. If no #address-cells and #size-cells is found
320 * addrcp and sizecp are left untouched.
321 *
322 * @params results Array of offsets pointing to each node matching the given prefix.
323 * @params results_len Number of entries allocated for the 'results' array
324 *
325 * @returns offset to last node found behind path or 0 if no node has been found
326 */
327size_t fdt_find_subnodes_by_prefix(const void *blob, u32 node_offset, const char *prefix,
328 u32 *addrcp, u32 *sizecp, u32 *results, size_t results_len)
329{
330 // sanity checks
331 if (!blob || !results || !prefix) {
332 printk(BIOS_ERR, "%s: input parameter cannot be null/\n", __func__);
333 return 0;
334 }
335
336 u32 offset = node_offset;
337
338 // we don't care about the name of the current node
339 u32 size = fdt_next_node_name(blob, offset, NULL);
340 if (!size) {
341 printk(BIOS_ERR, "%s: node_offset: %x does not point to a node\n",
342 __func__, node_offset);
343 return 0;
344 }
345 offset += size;
346
347 /*
348 * update addrcp and sizecp if the node contains an address-cells and size-cells
349 * property. Otherwise use addrcp and sizecp provided by caller.
350 */
351 offset = fdt_read_cell_props(blob, offset, addrcp, sizecp);
352
353 size_t count_results = 0;
354 int prefix_len = strlen(prefix);
355 const char *node_name;
356 // walk all children nodes of offset
357 while ((size = fdt_next_node_name(blob, offset, &node_name))) {
358
359 if (count_results >= results_len) {
360 printk(BIOS_WARNING,
361 "%s: results_len (%zd) smaller than count_results (%zd)\n",
362 __func__, results_len, count_results);
363 break;
364 }
365
366 if (!strncmp(prefix, node_name, prefix_len)) {
367 // we found a node that matches the prefix
368 results[count_results++] = offset;
369 }
370
371 // node does not match the prefix. skip current node
372 offset += fdt_skip_node(blob, offset);
373 }
374
375 // return last occurrence
376 return count_results;
377}
378
379static const char *fdt_read_alias_prop(const void *blob, const char *alias_name)
380{
381 u32 node_offset = fdt_find_node_by_path(blob, "/aliases", NULL, NULL);
382 if (!node_offset) {
383 printk(BIOS_DEBUG, "no /aliases node found\n");
384 return NULL;
385 }
386 struct fdt_property alias_prop;
387 if (!fdt_read_prop(blob, node_offset, alias_name, &alias_prop)) {
388 printk(BIOS_DEBUG, "property %s in /aliases node not found\n", alias_name);
389 return NULL;
390 }
391 return (const char *)alias_prop.data;
392}
393
394/*
395 * Find a node in the tree from a string device tree path.
396 *
397 * @params blob Address to the FDT
398 * @params alias_name node name alias that should be searched for.
399 * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells
400 * value found in the node of the node specified by node_offset. Either
401 * may be NULL to ignore. If no #address-cells and #size-cells is found
402 * default values of #address-cells=2 and #size-cells=1 are returned.
403 *
404 * @returns offset to last node found behind path or 0 if no node has been found
405 */
406u32 fdt_find_node_by_alias(const void *blob, const char *alias_name, u32 *addrcp, u32 *sizecp)
407{
408 const char *node_name = fdt_read_alias_prop(blob, alias_name);
409 if (!node_name) {
410 printk(BIOS_DEBUG, "alias %s not found\n", alias_name);
411 return 0;
412 }
413
414 u32 node_offset = fdt_find_node_by_path(blob, node_name, addrcp, sizecp);
415 if (!node_offset) {
416 // This should not happen (invalid devicetree)
417 printk(BIOS_WARNING,
418 "Could not find node '%s', which alias was referring to '%s'\n",
419 node_name, alias_name);
420 return 0;
421 }
422 return node_offset;
423}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200424
425
426/*
427 * Functions for printing flattened trees.
428 */
429
430static void print_indent(int depth)
431{
Julius Werner0d746532019-05-06 19:35:56 -0700432 printk(BIOS_DEBUG, "%*s", depth * 8, "");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200433}
434
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200435static void print_property(const struct fdt_property *prop, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200436{
Julius Werner0d746532019-05-06 19:35:56 -0700437 int is_string = prop->size > 0 &&
438 ((char *)prop->data)[prop->size - 1] == '\0';
439
Maximilian Brune77eaec62023-09-20 05:12:04 +0200440 if (is_string) {
441 for (int i = 0; i < prop->size - 1; i++) {
442 if (!isprint(((char *)prop->data)[i])) {
Julius Werner0d746532019-05-06 19:35:56 -0700443 is_string = 0;
Maximilian Brune77eaec62023-09-20 05:12:04 +0200444 break;
445 }
446 }
447 }
Julius Werner0d746532019-05-06 19:35:56 -0700448
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200449 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -0700450 if (is_string) {
451 printk(BIOS_DEBUG, "%s = \"%s\";\n",
452 prop->name, (const char *)prop->data);
453 } else {
454 printk(BIOS_DEBUG, "%s = < ", prop->name);
455 for (int i = 0; i < MIN(128, prop->size); i += 4) {
456 uint32_t val = 0;
457 for (int j = 0; j < MIN(4, prop->size - i); j++)
458 val |= ((uint8_t *)prop->data)[i + j] <<
459 (24 - j * 8);
460 printk(BIOS_DEBUG, "%#.2x ", val);
461 }
462 if (prop->size > 128)
463 printk(BIOS_DEBUG, "...");
464 printk(BIOS_DEBUG, ">;\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200465 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200466}
467
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200468static int print_flat_node(const void *blob, uint32_t start_offset, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200469{
470 int offset = start_offset;
471 const char *name;
472 int size;
473
Maximilian Brune33079b82024-03-04 15:34:41 +0100474 size = fdt_next_node_name(blob, offset, &name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200475 if (!size)
476 return 0;
477 offset += size;
478
479 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -0700480 printk(BIOS_DEBUG, "%s {\n", name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200481
Patrick Rudolph666c1722018-04-03 09:57:33 +0200482 struct fdt_property prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200483 while ((size = fdt_next_property(blob, offset, &prop))) {
484 print_property(&prop, depth + 1);
485
486 offset += size;
487 }
488
Julius Werner23df4772019-05-17 22:50:18 -0700489 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700490
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200491 while ((size = print_flat_node(blob, offset, depth + 1)))
492 offset += size;
493
Julius Werner0d746532019-05-06 19:35:56 -0700494 print_indent(depth);
495 printk(BIOS_DEBUG, "}\n");
496
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200497 return offset - start_offset + sizeof(uint32_t);
498}
499
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200500void fdt_print_node(const void *blob, uint32_t offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200501{
502 print_flat_node(blob, offset, 0);
503}
504
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200505/*
506 * Functions to turn a flattened tree into an unflattened one.
507 */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200508
Maximilian Brune33079b82024-03-04 15:34:41 +0100509static int dt_prop_is_phandle(struct device_tree_property *prop)
510{
511 return !(strcmp("phandle", prop->prop.name) &&
512 strcmp("linux,phandle", prop->prop.name));
513}
514
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200515static int fdt_unflatten_node(const void *blob, uint32_t start_offset,
Julius Werner6702b682019-05-03 18:13:53 -0700516 struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200517 struct device_tree_node **new_node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200518{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200519 struct list_node *last;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200520 int offset = start_offset;
521 const char *name;
522 int size;
523
Maximilian Brune33079b82024-03-04 15:34:41 +0100524 size = fdt_next_node_name(blob, offset, &name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200525 if (!size)
526 return 0;
527 offset += size;
528
Julius Werner9636a102019-05-03 17:36:43 -0700529 struct device_tree_node *node = xzalloc(sizeof(*node));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200530 *new_node = node;
531 node->name = name;
532
Patrick Rudolph666c1722018-04-03 09:57:33 +0200533 struct fdt_property fprop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200534 last = &node->properties;
535 while ((size = fdt_next_property(blob, offset, &fprop))) {
Julius Werner9636a102019-05-03 17:36:43 -0700536 struct device_tree_property *prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200537 prop->prop = fprop;
538
Julius Werner6702b682019-05-03 18:13:53 -0700539 if (dt_prop_is_phandle(prop)) {
540 node->phandle = be32dec(prop->prop.data);
541 if (node->phandle > tree->max_phandle)
542 tree->max_phandle = node->phandle;
543 }
544
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200545 list_insert_after(&prop->list_node, last);
546 last = &prop->list_node;
547
548 offset += size;
549 }
550
Patrick Rudolph666c1722018-04-03 09:57:33 +0200551 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200552 last = &node->children;
Julius Werner6702b682019-05-03 18:13:53 -0700553 while ((size = fdt_unflatten_node(blob, offset, tree, &child))) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200554 list_insert_after(&child->list_node, last);
555 last = &child->list_node;
556
557 offset += size;
558 }
559
560 return offset - start_offset + sizeof(uint32_t);
561}
562
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200563static int fdt_unflatten_map_entry(const void *blob, uint32_t offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200564 struct device_tree_reserve_map_entry **new)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200565{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200566 const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset);
567 const uint64_t start = be64toh(ptr[0]);
568 const uint64_t size = be64toh(ptr[1]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200569
570 if (!size)
571 return 0;
572
Julius Werner9636a102019-05-03 17:36:43 -0700573 struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200574 *new = entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200575 entry->start = start;
576 entry->size = size;
577
578 return sizeof(uint64_t) * 2;
579}
580
Maximilian Brune33079b82024-03-04 15:34:41 +0100581bool fdt_is_valid(const void *blob)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200582{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200583 const struct fdt_header *header = (const struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200584
Julius Werner73eaec82019-05-03 17:58:07 -0700585 uint32_t magic = be32toh(header->magic);
586 uint32_t version = be32toh(header->version);
587 uint32_t last_comp_version = be32toh(header->last_comp_version);
588
589 if (magic != FDT_HEADER_MAGIC) {
Maximilian Brune33079b82024-03-04 15:34:41 +0100590 printk(BIOS_ERR, "Invalid device tree magic %#.8x!\n", magic);
591 return false;
Julius Werner73eaec82019-05-03 17:58:07 -0700592 }
593 if (last_comp_version > FDT_SUPPORTED_VERSION) {
Maximilian Brune33079b82024-03-04 15:34:41 +0100594 printk(BIOS_ERR, "Unsupported device tree version %u(>=%u)\n",
Julius Werner73eaec82019-05-03 17:58:07 -0700595 version, last_comp_version);
Maximilian Brune33079b82024-03-04 15:34:41 +0100596 return false;
Julius Werner73eaec82019-05-03 17:58:07 -0700597 }
598 if (version > FDT_SUPPORTED_VERSION)
Elyes Haouasd7326282022-12-21 08:16:03 +0100599 printk(BIOS_NOTICE, "FDT version %u too new, should add support!\n",
Julius Werner73eaec82019-05-03 17:58:07 -0700600 version);
Maximilian Brune33079b82024-03-04 15:34:41 +0100601 return true;
602}
603
604struct device_tree *fdt_unflatten(const void *blob)
605{
606 struct device_tree *tree = xzalloc(sizeof(*tree));
607 const struct fdt_header *header = (const struct fdt_header *)blob;
608 tree->header = header;
609
610 if (fdt_is_valid(blob))
611 return NULL;
Julius Werner73eaec82019-05-03 17:58:07 -0700612
Patrick Rudolph666c1722018-04-03 09:57:33 +0200613 uint32_t struct_offset = be32toh(header->structure_offset);
614 uint32_t strings_offset = be32toh(header->strings_offset);
615 uint32_t reserve_offset = be32toh(header->reserve_map_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200616 uint32_t min_offset = 0;
617 min_offset = MIN(struct_offset, strings_offset);
618 min_offset = MIN(min_offset, reserve_offset);
Julius Werner23df4772019-05-17 22:50:18 -0700619 /* Assume everything up to the first non-header component is part of
620 the header and needs to be preserved. This will protect us against
621 new elements being added in the future. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200622 tree->header_size = min_offset;
623
Patrick Rudolph666c1722018-04-03 09:57:33 +0200624 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200625 uint32_t offset = reserve_offset;
626 int size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200627 struct list_node *last = &tree->reserve_map;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200628 while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) {
629 list_insert_after(&entry->list_node, last);
630 last = &entry->list_node;
631
632 offset += size;
633 }
634
Julius Werner6702b682019-05-03 18:13:53 -0700635 fdt_unflatten_node(blob, struct_offset, tree, &tree->root);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200636
637 return tree;
638}
639
640
641
642/*
Patrick Rudolph666c1722018-04-03 09:57:33 +0200643 * Functions to find the size of the device tree if it was flattened.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200644 */
645
Patrick Rudolph666c1722018-04-03 09:57:33 +0200646static void dt_flat_prop_size(struct device_tree_property *prop,
647 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200648{
Julius Werner23df4772019-05-17 22:50:18 -0700649 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200650 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700651 /* Size. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200652 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700653 /* Name offset. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200654 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700655 /* Property value. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200656 *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200657
Julius Werner23df4772019-05-17 22:50:18 -0700658 /* Property name. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200659 *strings_size += strlen(prop->prop.name) + 1;
660}
661
Patrick Rudolph666c1722018-04-03 09:57:33 +0200662static void dt_flat_node_size(struct device_tree_node *node,
663 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200664{
Julius Werner23df4772019-05-17 22:50:18 -0700665 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200666 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700667 /* Node name. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200668 *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200669
Patrick Rudolph666c1722018-04-03 09:57:33 +0200670 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200671 list_for_each(prop, node->properties, list_node)
672 dt_flat_prop_size(prop, struct_size, strings_size);
673
Patrick Rudolph666c1722018-04-03 09:57:33 +0200674 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200675 list_for_each(child, node->children, list_node)
676 dt_flat_node_size(child, struct_size, strings_size);
677
Julius Werner23df4772019-05-17 22:50:18 -0700678 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200679 *struct_size += sizeof(uint32_t);
680}
681
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200682uint32_t dt_flat_size(const struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200683{
684 uint32_t size = tree->header_size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200685 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200686 list_for_each(entry, tree->reserve_map, list_node)
687 size += sizeof(uint64_t) * 2;
688 size += sizeof(uint64_t) * 2;
689
690 uint32_t struct_size = 0;
691 uint32_t strings_size = 0;
692 dt_flat_node_size(tree->root, &struct_size, &strings_size);
693
694 size += struct_size;
Julius Werner23df4772019-05-17 22:50:18 -0700695 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200696 size += sizeof(uint32_t);
697
698 size += strings_size;
699
700 return size;
701}
702
703
704
705/*
706 * Functions to flatten a device tree.
707 */
708
Patrick Rudolph666c1722018-04-03 09:57:33 +0200709static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200710 void **map_start)
711{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200712 ((uint64_t *)*map_start)[0] = htobe64(entry->start);
713 ((uint64_t *)*map_start)[1] = htobe64(entry->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200714 *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2;
715}
716
Patrick Rudolph666c1722018-04-03 09:57:33 +0200717static void dt_flatten_prop(struct device_tree_property *prop,
718 void **struct_start, void *strings_base,
719 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200720{
721 uint8_t *dstruct = (uint8_t *)*struct_start;
722 uint8_t *dstrings = (uint8_t *)*strings_start;
723
Julius Wernera5ea3a22019-05-07 17:38:12 -0700724 be32enc(dstruct, FDT_TOKEN_PROPERTY);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200725 dstruct += sizeof(uint32_t);
726
Julius Wernera5ea3a22019-05-07 17:38:12 -0700727 be32enc(dstruct, prop->prop.size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200728 dstruct += sizeof(uint32_t);
729
730 uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base;
Julius Wernera5ea3a22019-05-07 17:38:12 -0700731 be32enc(dstruct, name_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200732 dstruct += sizeof(uint32_t);
733
734 strcpy((char *)dstrings, prop->prop.name);
735 dstrings += strlen(prop->prop.name) + 1;
736
737 memcpy(dstruct, prop->prop.data, prop->prop.size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200738 dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200739
740 *struct_start = dstruct;
741 *strings_start = dstrings;
742}
743
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200744static void dt_flatten_node(const struct device_tree_node *node,
745 void **struct_start, void *strings_base,
746 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200747{
748 uint8_t *dstruct = (uint8_t *)*struct_start;
749 uint8_t *dstrings = (uint8_t *)*strings_start;
750
Julius Wernera5ea3a22019-05-07 17:38:12 -0700751 be32enc(dstruct, FDT_TOKEN_BEGIN_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200752 dstruct += sizeof(uint32_t);
753
754 strcpy((char *)dstruct, node->name);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200755 dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200756
Patrick Rudolph666c1722018-04-03 09:57:33 +0200757 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200758 list_for_each(prop, node->properties, list_node)
759 dt_flatten_prop(prop, (void **)&dstruct, strings_base,
760 (void **)&dstrings);
761
Patrick Rudolph666c1722018-04-03 09:57:33 +0200762 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200763 list_for_each(child, node->children, list_node)
764 dt_flatten_node(child, (void **)&dstruct, strings_base,
765 (void **)&dstrings);
766
Julius Wernera5ea3a22019-05-07 17:38:12 -0700767 be32enc(dstruct, FDT_TOKEN_END_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200768 dstruct += sizeof(uint32_t);
769
770 *struct_start = dstruct;
771 *strings_start = dstrings;
772}
773
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200774void dt_flatten(const struct device_tree *tree, void *start_dest)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200775{
776 uint8_t *dest = (uint8_t *)start_dest;
777
778 memcpy(dest, tree->header, tree->header_size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200779 struct fdt_header *header = (struct fdt_header *)dest;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200780 dest += tree->header_size;
781
Patrick Rudolph666c1722018-04-03 09:57:33 +0200782 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200783 list_for_each(entry, tree->reserve_map, list_node)
784 dt_flatten_map_entry(entry, (void **)&dest);
785 ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0;
786 dest += sizeof(uint64_t) * 2;
787
788 uint32_t struct_size = 0;
789 uint32_t strings_size = 0;
790 dt_flat_node_size(tree->root, &struct_size, &strings_size);
791
792 uint8_t *struct_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200793 header->structure_offset = htobe32(dest - (uint8_t *)start_dest);
794 header->structure_size = htobe32(struct_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200795 dest += struct_size;
796
Patrick Rudolph666c1722018-04-03 09:57:33 +0200797 *((uint32_t *)dest) = htobe32(FDT_TOKEN_END);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200798 dest += sizeof(uint32_t);
799
800 uint8_t *strings_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200801 header->strings_offset = htobe32(dest - (uint8_t *)start_dest);
802 header->strings_size = htobe32(strings_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200803 dest += strings_size;
804
805 dt_flatten_node(tree->root, (void **)&struct_start, strings_start,
806 (void **)&strings_start);
807
Patrick Rudolph666c1722018-04-03 09:57:33 +0200808 header->totalsize = htobe32(dest - (uint8_t *)start_dest);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200809}
810
811
812
813/*
814 * Functions for printing a non-flattened device tree.
815 */
816
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200817static void print_node(const struct device_tree_node *node, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200818{
819 print_indent(depth);
Julius Werner23df4772019-05-17 22:50:18 -0700820 if (depth == 0) /* root node has no name, print a starting slash */
Julius Werner0d746532019-05-06 19:35:56 -0700821 printk(BIOS_DEBUG, "/");
822 printk(BIOS_DEBUG, "%s {\n", node->name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200823
Patrick Rudolph666c1722018-04-03 09:57:33 +0200824 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200825 list_for_each(prop, node->properties, list_node)
826 print_property(&prop->prop, depth + 1);
827
Julius Werner23df4772019-05-17 22:50:18 -0700828 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700829
Patrick Rudolph666c1722018-04-03 09:57:33 +0200830 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200831 list_for_each(child, node->children, list_node)
832 print_node(child, depth + 1);
Julius Werner0d746532019-05-06 19:35:56 -0700833
834 print_indent(depth);
835 printk(BIOS_DEBUG, "};\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200836}
837
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200838void dt_print_node(const struct device_tree_node *node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200839{
840 print_node(node, 0);
841}
842
843
844
845/*
846 * Functions for reading and manipulating an unflattened device tree.
847 */
848
849/*
850 * Read #address-cells and #size-cells properties from a node.
851 *
852 * @param node The device tree node to read from.
853 * @param addrcp Pointer to store #address-cells in, skipped if NULL.
854 * @param sizecp Pointer to store #size-cells in, skipped if NULL.
855 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200856void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
857 u32 *sizecp)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200858{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200859 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200860 list_for_each(prop, node->properties, list_node) {
861 if (addrcp && !strcmp("#address-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700862 *addrcp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200863 if (sizecp && !strcmp("#size-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700864 *sizecp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200865 }
866}
867
868/*
869 * Find a node from a device tree path, relative to a parent node.
870 *
871 * @param parent The node from which to start the relative path lookup.
872 * @param path An array of path component strings that will be looked
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200873 * up in order to find the node. Must be terminated with
874 * a NULL pointer. Example: {'firmware', 'coreboot', NULL}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200875 * @param addrcp Pointer that will be updated with any #address-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200876 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200877 * @param sizecp Pointer that will be updated with any #size-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200878 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200879 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
880 * @return The found/created node, or NULL.
881 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200882struct device_tree_node *dt_find_node(struct device_tree_node *parent,
883 const char **path, u32 *addrcp,
884 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200885{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200886 struct device_tree_node *node, *found = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200887
Julius Werner23df4772019-05-17 22:50:18 -0700888 /* Update #address-cells and #size-cells for this level. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200889 dt_read_cell_props(parent, addrcp, sizecp);
890
891 if (!*path)
892 return parent;
893
Julius Werner23df4772019-05-17 22:50:18 -0700894 /* Find the next node in the path, if it exists. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200895 list_for_each(node, parent->children, list_node) {
896 if (!strcmp(node->name, *path)) {
897 found = node;
898 break;
899 }
900 }
901
Julius Werner23df4772019-05-17 22:50:18 -0700902 /* Otherwise create it or return NULL. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200903 if (!found) {
904 if (!create)
905 return NULL;
906
Sergii Dmytruk206328d2022-03-13 18:23:17 +0200907 found = calloc(1, sizeof(*found));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200908 if (!found)
909 return NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200910 found->name = strdup(*path);
911 if (!found->name)
912 return NULL;
913
914 list_insert_after(&found->list_node, &parent->children);
915 }
916
917 return dt_find_node(found, path + 1, addrcp, sizecp, create);
918}
919
920/*
Julius Wernerf36d53c2019-05-03 18:23:34 -0700921 * Find a node in the tree from a string device tree path.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200922 *
Julius Wernerf36d53c2019-05-03 18:23:34 -0700923 * @param tree The device tree to search.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200924 * @param path A string representing a path in the device tree, with
Julius Wernerfbec63d2019-05-03 18:29:28 -0700925 * nodes separated by '/'. Example: "/firmware/coreboot"
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200926 * @param addrcp Pointer that will be updated with any #address-cells
927 * value found in the path. May be NULL to ignore.
928 * @param sizecp Pointer that will be updated with any #size-cells
929 * value found in the path. May be NULL to ignore.
930 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
931 * @return The found/created node, or NULL.
932 *
Julius Werner6d5695f2019-05-06 19:23:28 -0700933 * It is the caller responsibility to provide a path string that doesn't end
934 * with a '/' and doesn't contain any "//". If the path does not start with a
935 * '/', the first segment is interpreted as an alias. */
Julius Wernerf36d53c2019-05-03 18:23:34 -0700936struct device_tree_node *dt_find_node_by_path(struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200937 const char *path, u32 *addrcp,
938 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200939{
Julius Werner6d5695f2019-05-06 19:23:28 -0700940 char *sub_path;
941 char *duped_str;
942 struct device_tree_node *parent;
943 char *next_slash;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200944 /* Hopefully enough depth for any node. */
945 const char *path_array[15];
946 int i;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200947 struct device_tree_node *node = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200948
Julius Werner23df4772019-05-17 22:50:18 -0700949 if (path[0] == '/') { /* regular path */
950 if (path[1] == '\0') { /* special case: "/" is root node */
Julius Werner6d5695f2019-05-06 19:23:28 -0700951 dt_read_cell_props(tree->root, addrcp, sizecp);
952 return tree->root;
953 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200954
Julius Werner6d5695f2019-05-06 19:23:28 -0700955 sub_path = duped_str = strdup(&path[1]);
956 if (!sub_path)
957 return NULL;
958
959 parent = tree->root;
Julius Werner23df4772019-05-17 22:50:18 -0700960 } else { /* alias */
Julius Werner6d5695f2019-05-06 19:23:28 -0700961 char *alias;
962
963 alias = duped_str = strdup(path);
964 if (!alias)
965 return NULL;
966
967 sub_path = strchr(alias, '/');
968 if (sub_path)
969 *sub_path = '\0';
970
971 parent = dt_find_node_by_alias(tree, alias);
972 if (!parent) {
973 printk(BIOS_DEBUG,
974 "Could not find node '%s', alias '%s' does not exist\n",
975 path, alias);
976 free(duped_str);
977 return NULL;
978 }
979
980 if (!sub_path) {
Julius Werner23df4772019-05-17 22:50:18 -0700981 /* it's just the alias, no sub-path */
Julius Werner6d5695f2019-05-06 19:23:28 -0700982 free(duped_str);
983 return parent;
984 }
985
986 sub_path++;
987 }
988
989 next_slash = sub_path;
990 path_array[0] = sub_path;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200991 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200992 next_slash = strchr(next_slash, '/');
993 if (!next_slash)
994 break;
995
996 *next_slash++ = '\0';
997 path_array[i] = next_slash;
998 }
999
1000 if (!next_slash) {
1001 path_array[i] = NULL;
Julius Werner6d5695f2019-05-06 19:23:28 -07001002 node = dt_find_node(parent, path_array,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001003 addrcp, sizecp, create);
1004 }
1005
Julius Werner6d5695f2019-05-06 19:23:28 -07001006 free(duped_str);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001007 return node;
1008}
1009
Julius Werner6d5695f2019-05-06 19:23:28 -07001010/*
1011 * Find a node from an alias
1012 *
1013 * @param tree The device tree.
1014 * @param alias The alias name.
1015 * @return The found node, or NULL.
1016 */
1017struct device_tree_node *dt_find_node_by_alias(struct device_tree *tree,
1018 const char *alias)
1019{
1020 struct device_tree_node *node;
1021 const char *alias_path;
1022
1023 node = dt_find_node_by_path(tree, "/aliases", NULL, NULL, 0);
1024 if (!node)
1025 return NULL;
1026
1027 alias_path = dt_find_string_prop(node, alias);
1028 if (!alias_path)
1029 return NULL;
1030
1031 return dt_find_node_by_path(tree, alias_path, NULL, NULL, 0);
1032}
1033
Julius Werner6702b682019-05-03 18:13:53 -07001034struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root,
1035 uint32_t phandle)
1036{
1037 if (!root)
1038 return NULL;
1039
1040 if (root->phandle == phandle)
1041 return root;
1042
1043 struct device_tree_node *node;
1044 struct device_tree_node *result;
1045 list_for_each(node, root->children, list_node) {
1046 result = dt_find_node_by_phandle(node, phandle);
1047 if (result)
1048 return result;
1049 }
1050
1051 return NULL;
1052}
1053
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001054/*
1055 * Check if given node is compatible.
1056 *
1057 * @param node The node which is to be checked for compatible property.
1058 * @param compat The compatible string to match.
1059 * @return 1 = compatible, 0 = not compatible.
1060 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001061static int dt_check_compat_match(struct device_tree_node *node,
1062 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001063{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001064 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001065
1066 list_for_each(prop, node->properties, list_node) {
1067 if (!strcmp("compatible", prop->prop.name)) {
1068 size_t bytes = prop->prop.size;
1069 const char *str = prop->prop.data;
1070 while (bytes > 0) {
1071 if (!strncmp(compat, str, bytes))
1072 return 1;
1073 size_t len = strnlen(str, bytes) + 1;
1074 if (bytes <= len)
1075 break;
1076 str += len;
1077 bytes -= len;
1078 }
1079 break;
1080 }
1081 }
1082
1083 return 0;
1084}
1085
1086/*
1087 * Find a node from a compatible string, in the subtree of a parent node.
1088 *
1089 * @param parent The parent node under which to look.
1090 * @param compat The compatible string to find.
1091 * @return The found node, or NULL.
1092 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001093struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
1094 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001095{
Julius Werner23df4772019-05-17 22:50:18 -07001096 /* Check if the parent node itself is compatible. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001097 if (dt_check_compat_match(parent, compat))
1098 return parent;
1099
Patrick Rudolph666c1722018-04-03 09:57:33 +02001100 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001101 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001102 struct device_tree_node *found = dt_find_compat(child, compat);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001103 if (found)
1104 return found;
1105 }
1106
1107 return NULL;
1108}
1109
1110/*
Martin Roth0949e732021-10-01 14:28:22 -06001111 * Find the next compatible child of a given parent. All children up to the
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001112 * child passed in by caller are ignored. If child is NULL, it considers all the
1113 * children to find the first child which is compatible.
1114 *
1115 * @param parent The parent node under which to look.
1116 * @param child The child node to start search from (exclusive). If NULL
1117 * consider all children.
1118 * @param compat The compatible string to find.
1119 * @return The found node, or NULL.
1120 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001121struct device_tree_node *
1122dt_find_next_compat_child(struct device_tree_node *parent,
1123 struct device_tree_node *child,
1124 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001125{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001126 struct device_tree_node *next;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001127 int ignore = 0;
1128
1129 if (child)
1130 ignore = 1;
1131
1132 list_for_each(next, parent->children, list_node) {
1133 if (ignore) {
1134 if (child == next)
1135 ignore = 0;
1136 continue;
1137 }
1138
1139 if (dt_check_compat_match(next, compat))
1140 return next;
1141 }
1142
1143 return NULL;
1144}
1145
1146/*
1147 * Find a node with matching property value, in the subtree of a parent node.
1148 *
1149 * @param parent The parent node under which to look.
1150 * @param name The property name to look for.
1151 * @param data The property value to look for.
1152 * @param size The property size.
1153 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001154struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
1155 const char *name, void *data,
1156 size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001157{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001158 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001159
1160 /* Check if parent itself has the required property value. */
1161 list_for_each(prop, parent->properties, list_node) {
1162 if (!strcmp(name, prop->prop.name)) {
1163 size_t bytes = prop->prop.size;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001164 const void *prop_data = prop->prop.data;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001165 if (size != bytes)
1166 break;
1167 if (!memcmp(data, prop_data, size))
1168 return parent;
1169 break;
1170 }
1171 }
1172
Patrick Rudolph666c1722018-04-03 09:57:33 +02001173 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001174 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001175 struct device_tree_node *found = dt_find_prop_value(child, name,
1176 data, size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001177 if (found)
1178 return found;
1179 }
1180 return NULL;
1181}
1182
1183/*
1184 * Write an arbitrary sized big-endian integer into a pointer.
1185 *
1186 * @param dest Pointer to the DT property data buffer to write.
Elyes HAOUAS1ec76442018-08-07 12:20:04 +02001187 * @param src The integer to write (in CPU endianness).
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001188 * @param length the length of the destination integer in bytes.
1189 */
1190void dt_write_int(u8 *dest, u64 src, size_t length)
1191{
1192 while (length--) {
1193 dest[length] = (u8)src;
1194 src >>= 8;
1195 }
1196}
1197
1198/*
Patrick Rudolph5ccc7312018-05-30 15:05:28 +02001199 * Delete a property by name in a given node if it exists.
1200 *
1201 * @param node The device tree node to operate on.
1202 * @param name The name of the property to delete.
1203 */
1204void dt_delete_prop(struct device_tree_node *node, const char *name)
1205{
1206 struct device_tree_property *prop;
1207
1208 list_for_each(prop, node->properties, list_node) {
1209 if (!strcmp(prop->prop.name, name)) {
1210 list_remove(&prop->list_node);
1211 return;
1212 }
1213 }
1214}
1215
1216/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001217 * Add an arbitrary property to a node, or update it if it already exists.
1218 *
1219 * @param node The device tree node to add to.
1220 * @param name The name of the new property.
1221 * @param data The raw data blob to be stored in the property.
1222 * @param size The size of data in bytes.
1223 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001224void dt_add_bin_prop(struct device_tree_node *node, const char *name,
Julius Werner0e9116f2019-05-13 17:30:31 -07001225 void *data, size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001226{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001227 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001228
1229 list_for_each(prop, node->properties, list_node) {
1230 if (!strcmp(prop->prop.name, name)) {
1231 prop->prop.data = data;
1232 prop->prop.size = size;
1233 return;
1234 }
1235 }
1236
Julius Werner9636a102019-05-03 17:36:43 -07001237 prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001238 list_insert_after(&prop->list_node, &node->properties);
1239 prop->prop.name = name;
1240 prop->prop.data = data;
1241 prop->prop.size = size;
1242}
1243
1244/*
1245 * Find given string property in a node and return its content.
1246 *
1247 * @param node The device tree node to search.
1248 * @param name The name of the property.
1249 * @return The found string, or NULL.
1250 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001251const char *dt_find_string_prop(const struct device_tree_node *node,
1252 const char *name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001253{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001254 const void *content;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001255 size_t size;
1256
1257 dt_find_bin_prop(node, name, &content, &size);
1258
1259 return content;
1260}
1261
1262/*
1263 * Find given property in a node.
1264 *
1265 * @param node The device tree node to search.
1266 * @param name The name of the property.
1267 * @param data Pointer to return raw data blob in the property.
1268 * @param size Pointer to return the size of data in bytes.
1269 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001270void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
1271 const void **data, size_t *size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001272{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001273 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001274
1275 *data = NULL;
1276 *size = 0;
1277
1278 list_for_each(prop, node->properties, list_node) {
1279 if (!strcmp(prop->prop.name, name)) {
1280 *data = prop->prop.data;
1281 *size = prop->prop.size;
1282 return;
1283 }
1284 }
1285}
1286
1287/*
1288 * Add a string property to a node, or update it if it already exists.
1289 *
1290 * @param node The device tree node to add to.
1291 * @param name The name of the new property.
1292 * @param str The zero-terminated string to be stored in the property.
1293 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001294void dt_add_string_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001295 const char *str)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001296{
Julius Werner0e9116f2019-05-13 17:30:31 -07001297 dt_add_bin_prop(node, name, (char *)str, strlen(str) + 1);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001298}
1299
1300/*
1301 * Add a 32-bit integer property to a node, or update it if it already exists.
1302 *
1303 * @param node The device tree node to add to.
1304 * @param name The name of the new property.
1305 * @param val The integer to be stored in the property.
1306 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001307void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001308{
Julius Werner9636a102019-05-03 17:36:43 -07001309 u32 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph666c1722018-04-03 09:57:33 +02001310 *val_ptr = htobe32(val);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001311 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
1312}
1313
1314/*
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +02001315 * Add a 64-bit integer property to a node, or update it if it already exists.
1316 *
1317 * @param node The device tree node to add to.
1318 * @param name The name of the new property.
1319 * @param val The integer to be stored in the property.
1320 */
1321void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
1322{
Julius Werner9636a102019-05-03 17:36:43 -07001323 u64 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +02001324 *val_ptr = htobe64(val);
1325 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
1326}
1327
1328/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001329 * Add a 'reg' address list property to a node, or update it if it exists.
1330 *
1331 * @param node The device tree node to add to.
Maximilian Brune33079b82024-03-04 15:34:41 +01001332 * @param regions Array of address values to be stored in the property.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001333 * @param sizes Array of corresponding size values to 'addrs'.
1334 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
1335 * @param addr_cells Value of #address-cells property valid for this node.
1336 * @param size_cells Value of #size-cells property valid for this node.
1337 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001338void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001339 int count, u32 addr_cells, u32 size_cells)
1340{
1341 int i;
1342 size_t length = (addr_cells + size_cells) * sizeof(u32) * count;
Julius Werner9636a102019-05-03 17:36:43 -07001343 u8 *data = xmalloc(length);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001344 u8 *cur = data;
1345
1346 for (i = 0; i < count; i++) {
1347 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
1348 cur += addr_cells * sizeof(u32);
1349 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
1350 cur += size_cells * sizeof(u32);
1351 }
1352
1353 dt_add_bin_prop(node, "reg", data, length);
1354}
1355
1356/*
1357 * Fixups to apply to a kernel's device tree before booting it.
1358 */
1359
Patrick Rudolph666c1722018-04-03 09:57:33 +02001360struct list_node device_tree_fixups;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001361
Patrick Rudolph666c1722018-04-03 09:57:33 +02001362int dt_apply_fixups(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001363{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001364 struct device_tree_fixup *fixup;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001365 list_for_each(fixup, device_tree_fixups, list_node) {
1366 assert(fixup->fixup);
1367 if (fixup->fixup(fixup, tree))
1368 return 1;
1369 }
1370 return 0;
1371}
1372
Patrick Rudolph666c1722018-04-03 09:57:33 +02001373int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001374 void *data, size_t data_size, int create)
1375{
1376 char *path_copy, *prop_name;
Patrick Rudolph666c1722018-04-03 09:57:33 +02001377 struct device_tree_node *dt_node;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001378
1379 path_copy = strdup(path);
1380
1381 if (!path_copy) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001382 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
1383 path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001384 return 1;
1385 }
1386
1387 prop_name = strrchr(path_copy, '/');
1388 if (!prop_name) {
Patrick Rudolph679d6242018-07-11 13:53:04 +02001389 free(path_copy);
Patrick Rudolph666c1722018-04-03 09:57:33 +02001390 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001391 return 1;
1392 }
1393
1394 *prop_name++ = '\0'; /* Separate path from the property name. */
1395
Julius Wernerf36d53c2019-05-03 18:23:34 -07001396 dt_node = dt_find_node_by_path(tree, path_copy, NULL,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001397 NULL, create);
1398
1399 if (!dt_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001400 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001401 create ? "create" : "find", path_copy);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001402 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001403 return 1;
1404 }
1405
1406 dt_add_bin_prop(dt_node, prop_name, data, data_size);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001407 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001408
1409 return 0;
1410}
1411
1412/*
1413 * Prepare the /reserved-memory/ node.
1414 *
1415 * Technically, this can be called more than one time, to init and/or retrieve
1416 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
1417 *
1418 * @tree: Device tree to add/retrieve from.
1419 * @return: The /reserved-memory/ node (or NULL, if error).
1420 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001421struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001422{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001423 struct device_tree_node *reserved;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001424 u32 addr = 0, size = 0;
1425
Julius Wernerfbec63d2019-05-03 18:29:28 -07001426 reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001427 &size, 1);
1428 if (!reserved)
1429 return NULL;
1430
Julius Werner23df4772019-05-17 22:50:18 -07001431 /* Binding doc says this should have the same #{address,size}-cells as
1432 the root. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001433 dt_add_u32_prop(reserved, "#address-cells", addr);
1434 dt_add_u32_prop(reserved, "#size-cells", size);
Julius Werner23df4772019-05-17 22:50:18 -07001435 /* Binding doc says this should be empty (1:1 mapping from root). */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001436 dt_add_bin_prop(reserved, "ranges", NULL, 0);
1437
1438 return reserved;
1439}
Julius Werner735ddc92019-05-07 17:05:28 -07001440
1441/*
1442 * Increment a single phandle in prop at a given offset by a given adjustment.
1443 *
1444 * @param prop Property whose phandle should be adjusted.
1445 * @param adjustment Value that should be added to the existing phandle.
1446 * @param offset Byte offset of the phandle in the property data.
1447 *
1448 * @return New phandle value, or 0 on error.
1449 */
1450static uint32_t dt_adjust_phandle(struct device_tree_property *prop,
1451 uint32_t adjustment, uint32_t offset)
1452{
1453 if (offset + 4 > prop->prop.size)
1454 return 0;
1455
1456 uint32_t phandle = be32dec(prop->prop.data + offset);
1457 if (phandle == 0 ||
1458 phandle == FDT_PHANDLE_ILLEGAL ||
1459 phandle == 0xffffffff)
1460 return 0;
1461
1462 phandle += adjustment;
1463 if (phandle >= FDT_PHANDLE_ILLEGAL)
1464 return 0;
1465
1466 be32enc(prop->prop.data + offset, phandle);
1467 return phandle;
1468}
1469
1470/*
1471 * Adjust all phandles in subtree by adding a new base offset.
1472 *
1473 * @param node Root node of the subtree to work on.
1474 * @param base New phandle base to be added to all phandles.
1475 *
1476 * @return New highest phandle in the subtree, or 0 on error.
1477 */
1478static uint32_t dt_adjust_all_phandles(struct device_tree_node *node,
1479 uint32_t base)
1480{
Julius Werner23df4772019-05-17 22:50:18 -07001481 uint32_t new_max = MAX(base, 1); /* make sure we don't return 0 */
Julius Werner735ddc92019-05-07 17:05:28 -07001482 struct device_tree_property *prop;
1483 struct device_tree_node *child;
1484
1485 if (!node)
1486 return new_max;
1487
1488 list_for_each(prop, node->properties, list_node)
1489 if (dt_prop_is_phandle(prop)) {
1490 node->phandle = dt_adjust_phandle(prop, base, 0);
1491 if (!node->phandle)
1492 return 0;
1493 new_max = MAX(new_max, node->phandle);
Julius Werner23df4772019-05-17 22:50:18 -07001494 } /* no break -- can have more than one phandle prop */
Julius Werner735ddc92019-05-07 17:05:28 -07001495
1496 list_for_each(child, node->children, list_node)
1497 new_max = MAX(new_max, dt_adjust_all_phandles(child, base));
1498
1499 return new_max;
1500}
1501
1502/*
1503 * Apply a /__local_fixup__ subtree to the corresponding overlay subtree.
1504 *
1505 * @param node Root node of the overlay subtree to fix up.
1506 * @param node Root node of the /__local_fixup__ subtree.
1507 * @param base Adjustment that was added to phandles in the overlay.
1508 *
1509 * @return 0 on success, -1 on error.
1510 */
1511static int dt_fixup_locals(struct device_tree_node *node,
1512 struct device_tree_node *fixup, uint32_t base)
1513{
1514 struct device_tree_property *prop;
1515 struct device_tree_property *fixup_prop;
1516 struct device_tree_node *child;
1517 struct device_tree_node *fixup_child;
1518 int i;
1519
Julius Werner23df4772019-05-17 22:50:18 -07001520 /*
1521 * For local fixups the /__local_fixup__ subtree contains the same node
1522 * hierarchy as the main tree we're fixing up. Each property contains
1523 * the fixup offsets for the respective property in the main tree. For
1524 * each property in the fixup node, find the corresponding property in
1525 * the base node and apply fixups to all offsets it specifies.
1526 */
Julius Werner735ddc92019-05-07 17:05:28 -07001527 list_for_each(fixup_prop, fixup->properties, list_node) {
1528 struct device_tree_property *base_prop = NULL;
1529 list_for_each(prop, node->properties, list_node)
1530 if (!strcmp(prop->prop.name, fixup_prop->prop.name)) {
1531 base_prop = prop;
1532 break;
1533 }
1534
Julius Werner23df4772019-05-17 22:50:18 -07001535 /* We should always find a corresponding base prop for a fixup,
1536 and fixup props contain a list of 32-bit fixup offsets. */
Julius Werner735ddc92019-05-07 17:05:28 -07001537 if (!base_prop || fixup_prop->prop.size % sizeof(uint32_t))
1538 return -1;
1539
1540 for (i = 0; i < fixup_prop->prop.size; i += sizeof(uint32_t))
1541 if (!dt_adjust_phandle(base_prop, base, be32dec(
1542 fixup_prop->prop.data + i)))
1543 return -1;
1544 }
1545
Julius Werner23df4772019-05-17 22:50:18 -07001546 /* Now recursively descend both the base tree and the /__local_fixups__
1547 subtree in sync to apply all fixups. */
Julius Werner735ddc92019-05-07 17:05:28 -07001548 list_for_each(fixup_child, fixup->children, list_node) {
1549 struct device_tree_node *base_child = NULL;
1550 list_for_each(child, node->children, list_node)
1551 if (!strcmp(child->name, fixup_child->name)) {
1552 base_child = child;
1553 break;
1554 }
1555
Julius Werner23df4772019-05-17 22:50:18 -07001556 /* All fixup nodes should have a corresponding base node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001557 if (!base_child)
1558 return -1;
1559
1560 if (dt_fixup_locals(base_child, fixup_child, base) < 0)
1561 return -1;
1562 }
1563
1564 return 0;
1565}
1566
1567/*
1568 * Update all /__symbols__ properties in an overlay that start with
1569 * "/fragment@X/__overlay__" with corresponding path prefix in the base tree.
1570 *
1571 * @param symbols /__symbols__ done to update.
1572 * @param fragment /fragment@X node that references to should be updated.
1573 * @param base_path Path of base tree node that the fragment overlaid.
1574 */
1575static void dt_fix_symbols(struct device_tree_node *symbols,
1576 struct device_tree_node *fragment,
1577 const char *base_path)
1578{
1579 struct device_tree_property *prop;
Julius Werner23df4772019-05-17 22:50:18 -07001580 char buf[512]; /* Should be enough for maximum DT path length? */
1581 char node_path[64]; /* easily enough for /fragment@XXXX/__overlay__ */
Julius Werner735ddc92019-05-07 17:05:28 -07001582
Julius Werner23df4772019-05-17 22:50:18 -07001583 if (!symbols) /* If the overlay has no /__symbols__ node, we're done! */
Julius Werner735ddc92019-05-07 17:05:28 -07001584 return;
1585
1586 int len = snprintf(node_path, sizeof(node_path), "/%s/__overlay__",
1587 fragment->name);
1588
1589 list_for_each(prop, symbols->properties, list_node)
1590 if (!strncmp(prop->prop.data, node_path, len)) {
1591 prop->prop.size = snprintf(buf, sizeof(buf), "%s%s",
1592 base_path, (char *)prop->prop.data + len) + 1;
1593 free(prop->prop.data);
1594 prop->prop.data = strdup(buf);
1595 }
1596}
1597
1598/*
1599 * Fix up overlay according to a property in /__fixup__. If the fixed property
1600 * is a /fragment@X:target, also update /__symbols__ references to fragment.
1601 *
1602 * @params overlay Overlay to fix up.
1603 * @params fixup /__fixup__ property.
1604 * @params phandle phandle value to insert where the fixup points to.
1605 * @params base_path Path to the base DT node that the fixup points to.
1606 * @params overlay_symbols /__symbols__ node of the overlay.
1607 *
1608 * @return 0 on success, -1 on error.
1609 */
1610static int dt_fixup_external(struct device_tree *overlay,
1611 struct device_tree_property *fixup,
1612 uint32_t phandle, const char *base_path,
1613 struct device_tree_node *overlay_symbols)
1614{
1615 struct device_tree_property *prop;
1616
Julius Werner23df4772019-05-17 22:50:18 -07001617 /* External fixup properties are encoded as "<path>:<prop>:<offset>". */
Julius Werner735ddc92019-05-07 17:05:28 -07001618 char *entry = fixup->prop.data;
1619 while ((void *)entry < fixup->prop.data + fixup->prop.size) {
Julius Werner23df4772019-05-17 22:50:18 -07001620 /* okay to destroy fixup property value, won't need it again */
Julius Werner735ddc92019-05-07 17:05:28 -07001621 char *node_path = entry;
1622 entry = strchr(node_path, ':');
1623 if (!entry)
1624 return -1;
1625 *entry++ = '\0';
1626
1627 char *prop_name = entry;
1628 entry = strchr(prop_name, ':');
1629 if (!entry)
1630 return -1;
1631 *entry++ = '\0';
1632
1633 struct device_tree_node *ovl_node = dt_find_node_by_path(
1634 overlay, node_path, NULL, NULL, 0);
1635 if (!ovl_node || !isdigit(*entry))
1636 return -1;
1637
1638 struct device_tree_property *ovl_prop = NULL;
1639 list_for_each(prop, ovl_node->properties, list_node)
1640 if (!strcmp(prop->prop.name, prop_name)) {
1641 ovl_prop = prop;
1642 break;
1643 }
1644
Julius Werner23df4772019-05-17 22:50:18 -07001645 /* Move entry to first char after number, must be a '\0'. */
Julius Werner735ddc92019-05-07 17:05:28 -07001646 uint32_t offset = skip_atoi(&entry);
1647 if (!ovl_prop || offset + 4 > ovl_prop->prop.size || entry[0])
1648 return -1;
Julius Werner23df4772019-05-17 22:50:18 -07001649 entry++; /* jump over '\0' to potential next fixup */
Julius Werner735ddc92019-05-07 17:05:28 -07001650
1651 be32enc(ovl_prop->prop.data + offset, phandle);
1652
Julius Werner23df4772019-05-17 22:50:18 -07001653 /* If this is a /fragment@X:target property, update references
1654 to this fragment in the overlay __symbols__ now. */
Julius Werner735ddc92019-05-07 17:05:28 -07001655 if (offset == 0 && !strcmp(prop_name, "target") &&
Julius Werner23df4772019-05-17 22:50:18 -07001656 !strchr(node_path + 1, '/')) /* only toplevel nodes */
Julius Werner735ddc92019-05-07 17:05:28 -07001657 dt_fix_symbols(overlay_symbols, ovl_node, base_path);
1658 }
1659
1660 return 0;
1661}
1662
1663/*
1664 * Apply all /__fixup__ properties in the overlay. This will destroy the
1665 * property data in /__fixup__ and it should not be accessed again.
1666 *
1667 * @params tree Base device tree that the overlay updates.
1668 * @params symbols /__symbols__ node of the base device tree.
1669 * @params overlay Overlay to fix up.
1670 * @params fixups /__fixup__ node in the overlay.
1671 * @params overlay_symbols /__symbols__ node of the overlay.
1672 *
1673 * @return 0 on success, -1 on error.
1674 */
1675static int dt_fixup_all_externals(struct device_tree *tree,
1676 struct device_tree_node *symbols,
1677 struct device_tree *overlay,
1678 struct device_tree_node *fixups,
1679 struct device_tree_node *overlay_symbols)
1680{
1681 struct device_tree_property *fix;
1682
Julius Werner23df4772019-05-17 22:50:18 -07001683 /* If we have any external fixups, base tree must have /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001684 if (!symbols)
1685 return -1;
1686
Julius Werner23df4772019-05-17 22:50:18 -07001687 /*
1688 * Unlike /__local_fixups__, /__fixups__ is not a whole subtree that
1689 * mirrors the node hierarchy. It's just a directory of fixup properties
1690 * that each directly contain all information necessary to apply them.
1691 */
Julius Werner735ddc92019-05-07 17:05:28 -07001692 list_for_each(fix, fixups->properties, list_node) {
Julius Werner23df4772019-05-17 22:50:18 -07001693 /* The name of a fixup property is the label of the node we want
1694 a property to phandle-reference. Look up in /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001695 const char *path = dt_find_string_prop(symbols, fix->prop.name);
1696 if (!path)
1697 return -1;
1698
Elyes HAOUAS0afaff22021-01-16 15:02:31 +01001699 /* Find node the label pointed to figure out its phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001700 struct device_tree_node *node = dt_find_node_by_path(tree, path,
1701 NULL, NULL, 0);
1702 if (!node)
1703 return -1;
1704
Julius Werner23df4772019-05-17 22:50:18 -07001705 /* Write into the overlay property(s) pointing to that node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001706 if (dt_fixup_external(overlay, fix, node->phandle,
1707 path, overlay_symbols) < 0)
1708 return -1;
1709 }
1710
1711 return 0;
1712}
1713
1714/*
1715 * Copy all nodes and properties from one DT subtree into another. This is a
1716 * shallow copy so both trees will point to the same property data afterwards.
1717 *
1718 * @params dst Destination subtree to copy into.
1719 * @params src Source subtree to copy from.
1720 * @params upd 1 to overwrite same-name properties, 0 to discard them.
1721 */
1722static void dt_copy_subtree(struct device_tree_node *dst,
1723 struct device_tree_node *src, int upd)
1724{
1725 struct device_tree_property *prop;
1726 struct device_tree_property *src_prop;
1727 list_for_each(src_prop, src->properties, list_node) {
1728 if (dt_prop_is_phandle(src_prop) ||
1729 !strcmp(src_prop->prop.name, "name")) {
1730 printk(BIOS_DEBUG,
1731 "WARNING: ignoring illegal overlay prop '%s'\n",
1732 src_prop->prop.name);
1733 continue;
1734 }
1735
1736 struct device_tree_property *dst_prop = NULL;
1737 list_for_each(prop, dst->properties, list_node)
1738 if (!strcmp(prop->prop.name, src_prop->prop.name)) {
1739 dst_prop = prop;
1740 break;
1741 }
1742
1743 if (dst_prop) {
1744 if (!upd) {
1745 printk(BIOS_DEBUG,
1746 "WARNING: ignoring prop update '%s'\n",
1747 src_prop->prop.name);
1748 continue;
1749 }
1750 } else {
1751 dst_prop = xzalloc(sizeof(*dst_prop));
1752 list_insert_after(&dst_prop->list_node,
1753 &dst->properties);
1754 }
1755
1756 dst_prop->prop = src_prop->prop;
1757 }
1758
1759 struct device_tree_node *node;
1760 struct device_tree_node *src_node;
1761 list_for_each(src_node, src->children, list_node) {
1762 struct device_tree_node *dst_node = NULL;
1763 list_for_each(node, dst->children, list_node)
1764 if (!strcmp(node->name, src_node->name)) {
1765 dst_node = node;
1766 break;
1767 }
1768
1769 if (!dst_node) {
1770 dst_node = xzalloc(sizeof(*dst_node));
1771 *dst_node = *src_node;
1772 list_insert_after(&dst_node->list_node, &dst->children);
1773 } else {
1774 dt_copy_subtree(dst_node, src_node, upd);
1775 }
1776 }
1777}
1778
1779/*
1780 * Apply an overlay /fragment@X node to a base device tree.
1781 *
1782 * @param tree Base device tree.
1783 * @param fragment /fragment@X node.
1784 * @params overlay_symbols /__symbols__ node of the overlay.
1785 *
1786 * @return 0 on success, -1 on error.
1787 */
1788static int dt_import_fragment(struct device_tree *tree,
1789 struct device_tree_node *fragment,
1790 struct device_tree_node *overlay_symbols)
1791{
Julius Werner23df4772019-05-17 22:50:18 -07001792 /* The actual overlaid nodes/props are in an __overlay__ child node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001793 static const char *overlay_path[] = { "__overlay__", NULL };
1794 struct device_tree_node *overlay = dt_find_node(fragment, overlay_path,
1795 NULL, NULL, 0);
1796
Julius Werner23df4772019-05-17 22:50:18 -07001797 /* If it doesn't have an __overlay__ child, it's not a fragment. */
Julius Werner735ddc92019-05-07 17:05:28 -07001798 if (!overlay)
1799 return 0;
1800
Julius Werner23df4772019-05-17 22:50:18 -07001801 /* Target node of the fragment can be given by path or by phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001802 struct device_tree_property *prop;
1803 struct device_tree_property *phandle = NULL;
1804 struct device_tree_property *path = NULL;
1805 list_for_each(prop, fragment->properties, list_node) {
1806 if (!strcmp(prop->prop.name, "target")) {
1807 phandle = prop;
Julius Werner23df4772019-05-17 22:50:18 -07001808 break; /* phandle target has priority, stop looking */
Julius Werner735ddc92019-05-07 17:05:28 -07001809 }
1810 if (!strcmp(prop->prop.name, "target-path"))
1811 path = prop;
1812 }
1813
1814 struct device_tree_node *target = NULL;
1815 if (phandle) {
1816 if (phandle->prop.size != sizeof(uint32_t))
1817 return -1;
1818 target = dt_find_node_by_phandle(tree->root,
1819 be32dec(phandle->prop.data));
Julius Werner23df4772019-05-17 22:50:18 -07001820 /* Symbols already updated as part of dt_fixup_external(). */
Julius Werner735ddc92019-05-07 17:05:28 -07001821 } else if (path) {
1822 target = dt_find_node_by_path(tree, path->prop.data,
1823 NULL, NULL, 0);
1824 dt_fix_symbols(overlay_symbols, fragment, path->prop.data);
1825 }
1826 if (!target)
1827 return -1;
1828
1829 dt_copy_subtree(target, overlay, 1);
1830 return 0;
1831}
1832
1833/*
1834 * Apply a device tree overlay to a base device tree. This will
1835 * destroy/incorporate the overlay data, so it should not be freed or reused.
1836 * See dtc.git/Documentation/dt-object-internal.txt for overlay format details.
1837 *
1838 * @param tree Unflattened base device tree to add the overlay into.
1839 * @param overlay Unflattened overlay device tree to apply to the base.
1840 *
1841 * @return 0 on success, -1 on error.
1842 */
1843int dt_apply_overlay(struct device_tree *tree, struct device_tree *overlay)
1844{
Julius Werner23df4772019-05-17 22:50:18 -07001845 /*
1846 * First, we need to make sure phandles inside the overlay don't clash
1847 * with those in the base tree. We just define the highest phandle value
1848 * in the base tree as the "phandle offset" for this overlay and
1849 * increment all phandles in it by that value.
1850 */
Julius Werner735ddc92019-05-07 17:05:28 -07001851 uint32_t phandle_base = tree->max_phandle;
1852 uint32_t new_max = dt_adjust_all_phandles(overlay->root, phandle_base);
1853 if (!new_max) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001854 printk(BIOS_ERR, "invalid phandles in overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001855 return -1;
1856 }
1857 tree->max_phandle = new_max;
1858
Julius Werner23df4772019-05-17 22:50:18 -07001859 /* Now that we changed phandles in the overlay, we need to update any
1860 nodes referring to them. Those are listed in /__local_fixups__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001861 struct device_tree_node *local_fixups = dt_find_node_by_path(overlay,
1862 "/__local_fixups__", NULL, NULL, 0);
1863 if (local_fixups && dt_fixup_locals(overlay->root, local_fixups,
1864 phandle_base) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001865 printk(BIOS_ERR, "invalid local fixups in overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001866 return -1;
1867 }
1868
Julius Werner23df4772019-05-17 22:50:18 -07001869 /*
1870 * Besides local phandle references (from nodes within the overlay to
1871 * other nodes within the overlay), the overlay may also contain phandle
1872 * references to the base tree. These are stored with invalid values and
1873 * must be updated now. /__symbols__ contains a list of all labels in
1874 * the base tree, and /__fixups__ describes all nodes in the overlay
1875 * that contain external phandle references.
1876 * We also take this opportunity to update all /fragment@X/__overlay__/
1877 * prefixes in the overlay's /__symbols__ node to the correct path that
1878 * the fragment will be placed in later, since this is the only step
1879 * where we have all necessary information for that easily available.
1880 */
Julius Werner735ddc92019-05-07 17:05:28 -07001881 struct device_tree_node *symbols = dt_find_node_by_path(tree,
1882 "/__symbols__", NULL, NULL, 0);
1883 struct device_tree_node *fixups = dt_find_node_by_path(overlay,
1884 "/__fixups__", NULL, NULL, 0);
1885 struct device_tree_node *overlay_symbols = dt_find_node_by_path(overlay,
1886 "/__symbols__", NULL, NULL, 0);
1887 if (fixups && dt_fixup_all_externals(tree, symbols, overlay,
1888 fixups, overlay_symbols) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001889 printk(BIOS_ERR, "cannot match external fixups from overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001890 return -1;
1891 }
1892
Julius Werner23df4772019-05-17 22:50:18 -07001893 /* After all this fixing up, we can finally merge overlay into the tree
1894 (one fragment at a time, because for some reason it's split up). */
Julius Werner735ddc92019-05-07 17:05:28 -07001895 struct device_tree_node *fragment;
1896 list_for_each(fragment, overlay->root->children, list_node)
1897 if (dt_import_fragment(tree, fragment, overlay_symbols) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001898 printk(BIOS_ERR, "bad DT fragment '%s'\n",
Julius Werner735ddc92019-05-07 17:05:28 -07001899 fragment->name);
1900 return -1;
1901 }
1902
Julius Werner23df4772019-05-17 22:50:18 -07001903 /*
1904 * We need to also update /__symbols__ to include labels from this
1905 * overlay, in case we want to load further overlays with external
1906 * phandle references to it. If the base tree already has a /__symbols__
1907 * we merge them together, otherwise we just insert the overlay's
1908 * /__symbols__ node into the base tree root.
1909 */
Julius Werner735ddc92019-05-07 17:05:28 -07001910 if (overlay_symbols) {
1911 if (symbols)
1912 dt_copy_subtree(symbols, overlay_symbols, 0);
1913 else
1914 list_insert_after(&overlay_symbols->list_node,
1915 &tree->root->children);
1916 }
1917
1918 return 0;
1919}