blob: ab9c937b38b676c335a505fd56a8a4a4da51ec13 [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>
Julius Werner9636a102019-05-03 17:36:43 -07005#include <commonlib/stdlib.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +02006#include <console/console.h>
Joel Kitching393c71c2019-06-16 16:09:42 +08007#include <ctype.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +02008#include <device_tree.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02009#include <endian.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
15/*
16 * Functions for picking apart flattened trees.
17 */
18
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020019int fdt_next_property(const void *blob, uint32_t offset,
20 struct fdt_property *prop)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020021{
Patrick Rudolph666c1722018-04-03 09:57:33 +020022 struct fdt_header *header = (struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020023 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
24
25 int index = 0;
Patrick Rudolph666c1722018-04-03 09:57:33 +020026 if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020027 return 0;
28
Patrick Rudolph666c1722018-04-03 09:57:33 +020029 uint32_t size = be32toh(ptr[index++]);
30 uint32_t name_offset = be32toh(ptr[index++]);
31 name_offset += be32toh(header->strings_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020032
33 if (prop) {
34 prop->name = (char *)((uint8_t *)blob + name_offset);
35 prop->data = &ptr[index];
36 prop->size = size;
37 }
38
Patrick Rudolph666c1722018-04-03 09:57:33 +020039 index += DIV_ROUND_UP(size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020040
Patrick Rudolph666c1722018-04-03 09:57:33 +020041 return index * sizeof(uint32_t);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020042}
43
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020044int fdt_node_name(const void *blob, uint32_t offset, const char **name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020045{
46 uint8_t *ptr = ((uint8_t *)blob) + offset;
Julius Wernera5ea3a22019-05-07 17:38:12 -070047 if (be32dec(ptr) != FDT_TOKEN_BEGIN_NODE)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020048 return 0;
49
50 ptr += 4;
51 if (name)
52 *name = (char *)ptr;
Patrick Rudolph666c1722018-04-03 09:57:33 +020053 return ALIGN_UP(strlen((char *)ptr) + 1, sizeof(uint32_t)) + 4;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020054}
55
Julius Werner6702b682019-05-03 18:13:53 -070056static int dt_prop_is_phandle(struct device_tree_property *prop)
57{
58 return !(strcmp("phandle", prop->prop.name) &&
59 strcmp("linux,phandle", prop->prop.name));
60}
61
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020062
63
64/*
65 * Functions for printing flattened trees.
66 */
67
68static void print_indent(int depth)
69{
Julius Werner0d746532019-05-06 19:35:56 -070070 printk(BIOS_DEBUG, "%*s", depth * 8, "");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020071}
72
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020073static void print_property(const struct fdt_property *prop, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020074{
Julius Werner0d746532019-05-06 19:35:56 -070075 int is_string = prop->size > 0 &&
76 ((char *)prop->data)[prop->size - 1] == '\0';
77
Maximilian Brune77eaec62023-09-20 05:12:04 +020078 if (is_string) {
79 for (int i = 0; i < prop->size - 1; i++) {
80 if (!isprint(((char *)prop->data)[i])) {
Julius Werner0d746532019-05-06 19:35:56 -070081 is_string = 0;
Maximilian Brune77eaec62023-09-20 05:12:04 +020082 break;
83 }
84 }
85 }
Julius Werner0d746532019-05-06 19:35:56 -070086
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020087 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -070088 if (is_string) {
89 printk(BIOS_DEBUG, "%s = \"%s\";\n",
90 prop->name, (const char *)prop->data);
91 } else {
92 printk(BIOS_DEBUG, "%s = < ", prop->name);
93 for (int i = 0; i < MIN(128, prop->size); i += 4) {
94 uint32_t val = 0;
95 for (int j = 0; j < MIN(4, prop->size - i); j++)
96 val |= ((uint8_t *)prop->data)[i + j] <<
97 (24 - j * 8);
98 printk(BIOS_DEBUG, "%#.2x ", val);
99 }
100 if (prop->size > 128)
101 printk(BIOS_DEBUG, "...");
102 printk(BIOS_DEBUG, ">;\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200103 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200104}
105
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200106static int print_flat_node(const void *blob, uint32_t start_offset, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200107{
108 int offset = start_offset;
109 const char *name;
110 int size;
111
112 size = fdt_node_name(blob, offset, &name);
113 if (!size)
114 return 0;
115 offset += size;
116
117 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -0700118 printk(BIOS_DEBUG, "%s {\n", name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200119
Patrick Rudolph666c1722018-04-03 09:57:33 +0200120 struct fdt_property prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200121 while ((size = fdt_next_property(blob, offset, &prop))) {
122 print_property(&prop, depth + 1);
123
124 offset += size;
125 }
126
Julius Werner23df4772019-05-17 22:50:18 -0700127 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700128
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200129 while ((size = print_flat_node(blob, offset, depth + 1)))
130 offset += size;
131
Julius Werner0d746532019-05-06 19:35:56 -0700132 print_indent(depth);
133 printk(BIOS_DEBUG, "}\n");
134
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200135 return offset - start_offset + sizeof(uint32_t);
136}
137
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200138void fdt_print_node(const void *blob, uint32_t offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200139{
140 print_flat_node(blob, offset, 0);
141}
142
143
144
145/*
146 * A utility function to skip past nodes in flattened trees.
147 */
148
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200149int fdt_skip_node(const void *blob, uint32_t start_offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200150{
151 int offset = start_offset;
152 int size;
153
154 const char *name;
155 size = fdt_node_name(blob, offset, &name);
156 if (!size)
157 return 0;
158 offset += size;
159
160 while ((size = fdt_next_property(blob, offset, NULL)))
161 offset += size;
162
163 while ((size = fdt_skip_node(blob, offset)))
164 offset += size;
165
166 return offset - start_offset + sizeof(uint32_t);
167}
168
169
170
171/*
172 * Functions to turn a flattened tree into an unflattened one.
173 */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200174
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200175static int fdt_unflatten_node(const void *blob, uint32_t start_offset,
Julius Werner6702b682019-05-03 18:13:53 -0700176 struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200177 struct device_tree_node **new_node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200178{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200179 struct list_node *last;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200180 int offset = start_offset;
181 const char *name;
182 int size;
183
184 size = fdt_node_name(blob, offset, &name);
185 if (!size)
186 return 0;
187 offset += size;
188
Julius Werner9636a102019-05-03 17:36:43 -0700189 struct device_tree_node *node = xzalloc(sizeof(*node));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200190 *new_node = node;
191 node->name = name;
192
Patrick Rudolph666c1722018-04-03 09:57:33 +0200193 struct fdt_property fprop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200194 last = &node->properties;
195 while ((size = fdt_next_property(blob, offset, &fprop))) {
Julius Werner9636a102019-05-03 17:36:43 -0700196 struct device_tree_property *prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200197 prop->prop = fprop;
198
Julius Werner6702b682019-05-03 18:13:53 -0700199 if (dt_prop_is_phandle(prop)) {
200 node->phandle = be32dec(prop->prop.data);
201 if (node->phandle > tree->max_phandle)
202 tree->max_phandle = node->phandle;
203 }
204
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200205 list_insert_after(&prop->list_node, last);
206 last = &prop->list_node;
207
208 offset += size;
209 }
210
Patrick Rudolph666c1722018-04-03 09:57:33 +0200211 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200212 last = &node->children;
Julius Werner6702b682019-05-03 18:13:53 -0700213 while ((size = fdt_unflatten_node(blob, offset, tree, &child))) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200214 list_insert_after(&child->list_node, last);
215 last = &child->list_node;
216
217 offset += size;
218 }
219
220 return offset - start_offset + sizeof(uint32_t);
221}
222
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200223static int fdt_unflatten_map_entry(const void *blob, uint32_t offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200224 struct device_tree_reserve_map_entry **new)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200225{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200226 const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset);
227 const uint64_t start = be64toh(ptr[0]);
228 const uint64_t size = be64toh(ptr[1]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200229
230 if (!size)
231 return 0;
232
Julius Werner9636a102019-05-03 17:36:43 -0700233 struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200234 *new = entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200235 entry->start = start;
236 entry->size = size;
237
238 return sizeof(uint64_t) * 2;
239}
240
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200241struct device_tree *fdt_unflatten(const void *blob)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200242{
Julius Werner9636a102019-05-03 17:36:43 -0700243 struct device_tree *tree = xzalloc(sizeof(*tree));
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200244 const struct fdt_header *header = (const struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200245 tree->header = header;
246
Julius Werner73eaec82019-05-03 17:58:07 -0700247 uint32_t magic = be32toh(header->magic);
248 uint32_t version = be32toh(header->version);
249 uint32_t last_comp_version = be32toh(header->last_comp_version);
250
251 if (magic != FDT_HEADER_MAGIC) {
252 printk(BIOS_DEBUG, "Invalid device tree magic %#.8x!\n", magic);
Jacob Garber698d83a2019-06-07 10:28:54 -0600253 free(tree);
Julius Werner73eaec82019-05-03 17:58:07 -0700254 return NULL;
255 }
256 if (last_comp_version > FDT_SUPPORTED_VERSION) {
257 printk(BIOS_DEBUG, "Unsupported device tree version %u(>=%u)\n",
258 version, last_comp_version);
Jacob Garber698d83a2019-06-07 10:28:54 -0600259 free(tree);
Julius Werner73eaec82019-05-03 17:58:07 -0700260 return NULL;
261 }
262 if (version > FDT_SUPPORTED_VERSION)
Elyes Haouasd7326282022-12-21 08:16:03 +0100263 printk(BIOS_NOTICE, "FDT version %u too new, should add support!\n",
Julius Werner73eaec82019-05-03 17:58:07 -0700264 version);
265
Patrick Rudolph666c1722018-04-03 09:57:33 +0200266 uint32_t struct_offset = be32toh(header->structure_offset);
267 uint32_t strings_offset = be32toh(header->strings_offset);
268 uint32_t reserve_offset = be32toh(header->reserve_map_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200269 uint32_t min_offset = 0;
270 min_offset = MIN(struct_offset, strings_offset);
271 min_offset = MIN(min_offset, reserve_offset);
Julius Werner23df4772019-05-17 22:50:18 -0700272 /* Assume everything up to the first non-header component is part of
273 the header and needs to be preserved. This will protect us against
274 new elements being added in the future. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200275 tree->header_size = min_offset;
276
Patrick Rudolph666c1722018-04-03 09:57:33 +0200277 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200278 uint32_t offset = reserve_offset;
279 int size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200280 struct list_node *last = &tree->reserve_map;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200281 while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) {
282 list_insert_after(&entry->list_node, last);
283 last = &entry->list_node;
284
285 offset += size;
286 }
287
Julius Werner6702b682019-05-03 18:13:53 -0700288 fdt_unflatten_node(blob, struct_offset, tree, &tree->root);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200289
290 return tree;
291}
292
293
294
295/*
Patrick Rudolph666c1722018-04-03 09:57:33 +0200296 * Functions to find the size of the device tree if it was flattened.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200297 */
298
Patrick Rudolph666c1722018-04-03 09:57:33 +0200299static void dt_flat_prop_size(struct device_tree_property *prop,
300 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200301{
Julius Werner23df4772019-05-17 22:50:18 -0700302 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200303 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700304 /* Size. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200305 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700306 /* Name offset. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200307 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700308 /* Property value. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200309 *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200310
Julius Werner23df4772019-05-17 22:50:18 -0700311 /* Property name. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200312 *strings_size += strlen(prop->prop.name) + 1;
313}
314
Patrick Rudolph666c1722018-04-03 09:57:33 +0200315static void dt_flat_node_size(struct device_tree_node *node,
316 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200317{
Julius Werner23df4772019-05-17 22:50:18 -0700318 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200319 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700320 /* Node name. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200321 *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200322
Patrick Rudolph666c1722018-04-03 09:57:33 +0200323 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200324 list_for_each(prop, node->properties, list_node)
325 dt_flat_prop_size(prop, struct_size, strings_size);
326
Patrick Rudolph666c1722018-04-03 09:57:33 +0200327 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200328 list_for_each(child, node->children, list_node)
329 dt_flat_node_size(child, struct_size, strings_size);
330
Julius Werner23df4772019-05-17 22:50:18 -0700331 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200332 *struct_size += sizeof(uint32_t);
333}
334
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200335uint32_t dt_flat_size(const struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200336{
337 uint32_t size = tree->header_size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200338 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200339 list_for_each(entry, tree->reserve_map, list_node)
340 size += sizeof(uint64_t) * 2;
341 size += sizeof(uint64_t) * 2;
342
343 uint32_t struct_size = 0;
344 uint32_t strings_size = 0;
345 dt_flat_node_size(tree->root, &struct_size, &strings_size);
346
347 size += struct_size;
Julius Werner23df4772019-05-17 22:50:18 -0700348 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200349 size += sizeof(uint32_t);
350
351 size += strings_size;
352
353 return size;
354}
355
356
357
358/*
359 * Functions to flatten a device tree.
360 */
361
Patrick Rudolph666c1722018-04-03 09:57:33 +0200362static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200363 void **map_start)
364{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200365 ((uint64_t *)*map_start)[0] = htobe64(entry->start);
366 ((uint64_t *)*map_start)[1] = htobe64(entry->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200367 *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2;
368}
369
Patrick Rudolph666c1722018-04-03 09:57:33 +0200370static void dt_flatten_prop(struct device_tree_property *prop,
371 void **struct_start, void *strings_base,
372 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200373{
374 uint8_t *dstruct = (uint8_t *)*struct_start;
375 uint8_t *dstrings = (uint8_t *)*strings_start;
376
Julius Wernera5ea3a22019-05-07 17:38:12 -0700377 be32enc(dstruct, FDT_TOKEN_PROPERTY);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200378 dstruct += sizeof(uint32_t);
379
Julius Wernera5ea3a22019-05-07 17:38:12 -0700380 be32enc(dstruct, prop->prop.size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200381 dstruct += sizeof(uint32_t);
382
383 uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base;
Julius Wernera5ea3a22019-05-07 17:38:12 -0700384 be32enc(dstruct, name_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200385 dstruct += sizeof(uint32_t);
386
387 strcpy((char *)dstrings, prop->prop.name);
388 dstrings += strlen(prop->prop.name) + 1;
389
390 memcpy(dstruct, prop->prop.data, prop->prop.size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200391 dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200392
393 *struct_start = dstruct;
394 *strings_start = dstrings;
395}
396
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200397static void dt_flatten_node(const struct device_tree_node *node,
398 void **struct_start, void *strings_base,
399 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200400{
401 uint8_t *dstruct = (uint8_t *)*struct_start;
402 uint8_t *dstrings = (uint8_t *)*strings_start;
403
Julius Wernera5ea3a22019-05-07 17:38:12 -0700404 be32enc(dstruct, FDT_TOKEN_BEGIN_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200405 dstruct += sizeof(uint32_t);
406
407 strcpy((char *)dstruct, node->name);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200408 dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200409
Patrick Rudolph666c1722018-04-03 09:57:33 +0200410 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200411 list_for_each(prop, node->properties, list_node)
412 dt_flatten_prop(prop, (void **)&dstruct, strings_base,
413 (void **)&dstrings);
414
Patrick Rudolph666c1722018-04-03 09:57:33 +0200415 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200416 list_for_each(child, node->children, list_node)
417 dt_flatten_node(child, (void **)&dstruct, strings_base,
418 (void **)&dstrings);
419
Julius Wernera5ea3a22019-05-07 17:38:12 -0700420 be32enc(dstruct, FDT_TOKEN_END_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200421 dstruct += sizeof(uint32_t);
422
423 *struct_start = dstruct;
424 *strings_start = dstrings;
425}
426
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200427void dt_flatten(const struct device_tree *tree, void *start_dest)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200428{
429 uint8_t *dest = (uint8_t *)start_dest;
430
431 memcpy(dest, tree->header, tree->header_size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200432 struct fdt_header *header = (struct fdt_header *)dest;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200433 dest += tree->header_size;
434
Patrick Rudolph666c1722018-04-03 09:57:33 +0200435 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200436 list_for_each(entry, tree->reserve_map, list_node)
437 dt_flatten_map_entry(entry, (void **)&dest);
438 ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0;
439 dest += sizeof(uint64_t) * 2;
440
441 uint32_t struct_size = 0;
442 uint32_t strings_size = 0;
443 dt_flat_node_size(tree->root, &struct_size, &strings_size);
444
445 uint8_t *struct_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200446 header->structure_offset = htobe32(dest - (uint8_t *)start_dest);
447 header->structure_size = htobe32(struct_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200448 dest += struct_size;
449
Patrick Rudolph666c1722018-04-03 09:57:33 +0200450 *((uint32_t *)dest) = htobe32(FDT_TOKEN_END);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200451 dest += sizeof(uint32_t);
452
453 uint8_t *strings_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200454 header->strings_offset = htobe32(dest - (uint8_t *)start_dest);
455 header->strings_size = htobe32(strings_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200456 dest += strings_size;
457
458 dt_flatten_node(tree->root, (void **)&struct_start, strings_start,
459 (void **)&strings_start);
460
Patrick Rudolph666c1722018-04-03 09:57:33 +0200461 header->totalsize = htobe32(dest - (uint8_t *)start_dest);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200462}
463
464
465
466/*
467 * Functions for printing a non-flattened device tree.
468 */
469
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200470static void print_node(const struct device_tree_node *node, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200471{
472 print_indent(depth);
Julius Werner23df4772019-05-17 22:50:18 -0700473 if (depth == 0) /* root node has no name, print a starting slash */
Julius Werner0d746532019-05-06 19:35:56 -0700474 printk(BIOS_DEBUG, "/");
475 printk(BIOS_DEBUG, "%s {\n", node->name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200476
Patrick Rudolph666c1722018-04-03 09:57:33 +0200477 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200478 list_for_each(prop, node->properties, list_node)
479 print_property(&prop->prop, depth + 1);
480
Julius Werner23df4772019-05-17 22:50:18 -0700481 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700482
Patrick Rudolph666c1722018-04-03 09:57:33 +0200483 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200484 list_for_each(child, node->children, list_node)
485 print_node(child, depth + 1);
Julius Werner0d746532019-05-06 19:35:56 -0700486
487 print_indent(depth);
488 printk(BIOS_DEBUG, "};\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200489}
490
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200491void dt_print_node(const struct device_tree_node *node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200492{
493 print_node(node, 0);
494}
495
496
497
498/*
499 * Functions for reading and manipulating an unflattened device tree.
500 */
501
502/*
503 * Read #address-cells and #size-cells properties from a node.
504 *
505 * @param node The device tree node to read from.
506 * @param addrcp Pointer to store #address-cells in, skipped if NULL.
507 * @param sizecp Pointer to store #size-cells in, skipped if NULL.
508 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200509void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
510 u32 *sizecp)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200511{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200512 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200513 list_for_each(prop, node->properties, list_node) {
514 if (addrcp && !strcmp("#address-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700515 *addrcp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200516 if (sizecp && !strcmp("#size-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700517 *sizecp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200518 }
519}
520
521/*
522 * Find a node from a device tree path, relative to a parent node.
523 *
524 * @param parent The node from which to start the relative path lookup.
525 * @param path An array of path component strings that will be looked
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200526 * up in order to find the node. Must be terminated with
527 * a NULL pointer. Example: {'firmware', 'coreboot', NULL}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200528 * @param addrcp Pointer that will be updated with any #address-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200529 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200530 * @param sizecp Pointer that will be updated with any #size-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200531 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200532 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
533 * @return The found/created node, or NULL.
534 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200535struct device_tree_node *dt_find_node(struct device_tree_node *parent,
536 const char **path, u32 *addrcp,
537 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200538{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200539 struct device_tree_node *node, *found = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200540
Julius Werner23df4772019-05-17 22:50:18 -0700541 /* Update #address-cells and #size-cells for this level. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200542 dt_read_cell_props(parent, addrcp, sizecp);
543
544 if (!*path)
545 return parent;
546
Julius Werner23df4772019-05-17 22:50:18 -0700547 /* Find the next node in the path, if it exists. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200548 list_for_each(node, parent->children, list_node) {
549 if (!strcmp(node->name, *path)) {
550 found = node;
551 break;
552 }
553 }
554
Julius Werner23df4772019-05-17 22:50:18 -0700555 /* Otherwise create it or return NULL. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200556 if (!found) {
557 if (!create)
558 return NULL;
559
Sergii Dmytruk206328d2022-03-13 18:23:17 +0200560 found = calloc(1, sizeof(*found));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200561 if (!found)
562 return NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200563 found->name = strdup(*path);
564 if (!found->name)
565 return NULL;
566
567 list_insert_after(&found->list_node, &parent->children);
568 }
569
570 return dt_find_node(found, path + 1, addrcp, sizecp, create);
571}
572
573/*
Julius Wernerf36d53c2019-05-03 18:23:34 -0700574 * Find a node in the tree from a string device tree path.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200575 *
Julius Wernerf36d53c2019-05-03 18:23:34 -0700576 * @param tree The device tree to search.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200577 * @param path A string representing a path in the device tree, with
Julius Wernerfbec63d2019-05-03 18:29:28 -0700578 * nodes separated by '/'. Example: "/firmware/coreboot"
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200579 * @param addrcp Pointer that will be updated with any #address-cells
580 * value found in the path. May be NULL to ignore.
581 * @param sizecp Pointer that will be updated with any #size-cells
582 * value found in the path. May be NULL to ignore.
583 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
584 * @return The found/created node, or NULL.
585 *
Julius Werner6d5695f2019-05-06 19:23:28 -0700586 * It is the caller responsibility to provide a path string that doesn't end
587 * with a '/' and doesn't contain any "//". If the path does not start with a
588 * '/', the first segment is interpreted as an alias. */
Julius Wernerf36d53c2019-05-03 18:23:34 -0700589struct device_tree_node *dt_find_node_by_path(struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200590 const char *path, u32 *addrcp,
591 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200592{
Julius Werner6d5695f2019-05-06 19:23:28 -0700593 char *sub_path;
594 char *duped_str;
595 struct device_tree_node *parent;
596 char *next_slash;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200597 /* Hopefully enough depth for any node. */
598 const char *path_array[15];
599 int i;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200600 struct device_tree_node *node = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200601
Julius Werner23df4772019-05-17 22:50:18 -0700602 if (path[0] == '/') { /* regular path */
603 if (path[1] == '\0') { /* special case: "/" is root node */
Julius Werner6d5695f2019-05-06 19:23:28 -0700604 dt_read_cell_props(tree->root, addrcp, sizecp);
605 return tree->root;
606 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200607
Julius Werner6d5695f2019-05-06 19:23:28 -0700608 sub_path = duped_str = strdup(&path[1]);
609 if (!sub_path)
610 return NULL;
611
612 parent = tree->root;
Julius Werner23df4772019-05-17 22:50:18 -0700613 } else { /* alias */
Julius Werner6d5695f2019-05-06 19:23:28 -0700614 char *alias;
615
616 alias = duped_str = strdup(path);
617 if (!alias)
618 return NULL;
619
620 sub_path = strchr(alias, '/');
621 if (sub_path)
622 *sub_path = '\0';
623
624 parent = dt_find_node_by_alias(tree, alias);
625 if (!parent) {
626 printk(BIOS_DEBUG,
627 "Could not find node '%s', alias '%s' does not exist\n",
628 path, alias);
629 free(duped_str);
630 return NULL;
631 }
632
633 if (!sub_path) {
Julius Werner23df4772019-05-17 22:50:18 -0700634 /* it's just the alias, no sub-path */
Julius Werner6d5695f2019-05-06 19:23:28 -0700635 free(duped_str);
636 return parent;
637 }
638
639 sub_path++;
640 }
641
642 next_slash = sub_path;
643 path_array[0] = sub_path;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200644 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200645 next_slash = strchr(next_slash, '/');
646 if (!next_slash)
647 break;
648
649 *next_slash++ = '\0';
650 path_array[i] = next_slash;
651 }
652
653 if (!next_slash) {
654 path_array[i] = NULL;
Julius Werner6d5695f2019-05-06 19:23:28 -0700655 node = dt_find_node(parent, path_array,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200656 addrcp, sizecp, create);
657 }
658
Julius Werner6d5695f2019-05-06 19:23:28 -0700659 free(duped_str);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200660 return node;
661}
662
Julius Werner6d5695f2019-05-06 19:23:28 -0700663/*
664 * Find a node from an alias
665 *
666 * @param tree The device tree.
667 * @param alias The alias name.
668 * @return The found node, or NULL.
669 */
670struct device_tree_node *dt_find_node_by_alias(struct device_tree *tree,
671 const char *alias)
672{
673 struct device_tree_node *node;
674 const char *alias_path;
675
676 node = dt_find_node_by_path(tree, "/aliases", NULL, NULL, 0);
677 if (!node)
678 return NULL;
679
680 alias_path = dt_find_string_prop(node, alias);
681 if (!alias_path)
682 return NULL;
683
684 return dt_find_node_by_path(tree, alias_path, NULL, NULL, 0);
685}
686
Julius Werner6702b682019-05-03 18:13:53 -0700687struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root,
688 uint32_t phandle)
689{
690 if (!root)
691 return NULL;
692
693 if (root->phandle == phandle)
694 return root;
695
696 struct device_tree_node *node;
697 struct device_tree_node *result;
698 list_for_each(node, root->children, list_node) {
699 result = dt_find_node_by_phandle(node, phandle);
700 if (result)
701 return result;
702 }
703
704 return NULL;
705}
706
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200707/*
708 * Check if given node is compatible.
709 *
710 * @param node The node which is to be checked for compatible property.
711 * @param compat The compatible string to match.
712 * @return 1 = compatible, 0 = not compatible.
713 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200714static int dt_check_compat_match(struct device_tree_node *node,
715 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200716{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200717 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200718
719 list_for_each(prop, node->properties, list_node) {
720 if (!strcmp("compatible", prop->prop.name)) {
721 size_t bytes = prop->prop.size;
722 const char *str = prop->prop.data;
723 while (bytes > 0) {
724 if (!strncmp(compat, str, bytes))
725 return 1;
726 size_t len = strnlen(str, bytes) + 1;
727 if (bytes <= len)
728 break;
729 str += len;
730 bytes -= len;
731 }
732 break;
733 }
734 }
735
736 return 0;
737}
738
739/*
740 * Find a node from a compatible string, in the subtree of a parent node.
741 *
742 * @param parent The parent node under which to look.
743 * @param compat The compatible string to find.
744 * @return The found node, or NULL.
745 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200746struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
747 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200748{
Julius Werner23df4772019-05-17 22:50:18 -0700749 /* Check if the parent node itself is compatible. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200750 if (dt_check_compat_match(parent, compat))
751 return parent;
752
Patrick Rudolph666c1722018-04-03 09:57:33 +0200753 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200754 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200755 struct device_tree_node *found = dt_find_compat(child, compat);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200756 if (found)
757 return found;
758 }
759
760 return NULL;
761}
762
763/*
Martin Roth0949e732021-10-01 14:28:22 -0600764 * Find the next compatible child of a given parent. All children up to the
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200765 * child passed in by caller are ignored. If child is NULL, it considers all the
766 * children to find the first child which is compatible.
767 *
768 * @param parent The parent node under which to look.
769 * @param child The child node to start search from (exclusive). If NULL
770 * consider all children.
771 * @param compat The compatible string to find.
772 * @return The found node, or NULL.
773 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200774struct device_tree_node *
775dt_find_next_compat_child(struct device_tree_node *parent,
776 struct device_tree_node *child,
777 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200778{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200779 struct device_tree_node *next;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200780 int ignore = 0;
781
782 if (child)
783 ignore = 1;
784
785 list_for_each(next, parent->children, list_node) {
786 if (ignore) {
787 if (child == next)
788 ignore = 0;
789 continue;
790 }
791
792 if (dt_check_compat_match(next, compat))
793 return next;
794 }
795
796 return NULL;
797}
798
799/*
800 * Find a node with matching property value, in the subtree of a parent node.
801 *
802 * @param parent The parent node under which to look.
803 * @param name The property name to look for.
804 * @param data The property value to look for.
805 * @param size The property size.
806 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200807struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
808 const char *name, void *data,
809 size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200810{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200811 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200812
813 /* Check if parent itself has the required property value. */
814 list_for_each(prop, parent->properties, list_node) {
815 if (!strcmp(name, prop->prop.name)) {
816 size_t bytes = prop->prop.size;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200817 const void *prop_data = prop->prop.data;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200818 if (size != bytes)
819 break;
820 if (!memcmp(data, prop_data, size))
821 return parent;
822 break;
823 }
824 }
825
Patrick Rudolph666c1722018-04-03 09:57:33 +0200826 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200827 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200828 struct device_tree_node *found = dt_find_prop_value(child, name,
829 data, size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200830 if (found)
831 return found;
832 }
833 return NULL;
834}
835
836/*
837 * Write an arbitrary sized big-endian integer into a pointer.
838 *
839 * @param dest Pointer to the DT property data buffer to write.
Elyes HAOUAS1ec76442018-08-07 12:20:04 +0200840 * @param src The integer to write (in CPU endianness).
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200841 * @param length the length of the destination integer in bytes.
842 */
843void dt_write_int(u8 *dest, u64 src, size_t length)
844{
845 while (length--) {
846 dest[length] = (u8)src;
847 src >>= 8;
848 }
849}
850
851/*
Patrick Rudolph5ccc7312018-05-30 15:05:28 +0200852 * Delete a property by name in a given node if it exists.
853 *
854 * @param node The device tree node to operate on.
855 * @param name The name of the property to delete.
856 */
857void dt_delete_prop(struct device_tree_node *node, const char *name)
858{
859 struct device_tree_property *prop;
860
861 list_for_each(prop, node->properties, list_node) {
862 if (!strcmp(prop->prop.name, name)) {
863 list_remove(&prop->list_node);
864 return;
865 }
866 }
867}
868
869/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200870 * Add an arbitrary property to a node, or update it if it already exists.
871 *
872 * @param node The device tree node to add to.
873 * @param name The name of the new property.
874 * @param data The raw data blob to be stored in the property.
875 * @param size The size of data in bytes.
876 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200877void dt_add_bin_prop(struct device_tree_node *node, const char *name,
Julius Werner0e9116f2019-05-13 17:30:31 -0700878 void *data, size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200879{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200880 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200881
882 list_for_each(prop, node->properties, list_node) {
883 if (!strcmp(prop->prop.name, name)) {
884 prop->prop.data = data;
885 prop->prop.size = size;
886 return;
887 }
888 }
889
Julius Werner9636a102019-05-03 17:36:43 -0700890 prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200891 list_insert_after(&prop->list_node, &node->properties);
892 prop->prop.name = name;
893 prop->prop.data = data;
894 prop->prop.size = size;
895}
896
897/*
898 * Find given string property in a node and return its content.
899 *
900 * @param node The device tree node to search.
901 * @param name The name of the property.
902 * @return The found string, or NULL.
903 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200904const char *dt_find_string_prop(const struct device_tree_node *node,
905 const char *name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200906{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200907 const void *content;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200908 size_t size;
909
910 dt_find_bin_prop(node, name, &content, &size);
911
912 return content;
913}
914
915/*
916 * Find given property in a node.
917 *
918 * @param node The device tree node to search.
919 * @param name The name of the property.
920 * @param data Pointer to return raw data blob in the property.
921 * @param size Pointer to return the size of data in bytes.
922 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200923void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
924 const void **data, size_t *size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200925{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200926 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200927
928 *data = NULL;
929 *size = 0;
930
931 list_for_each(prop, node->properties, list_node) {
932 if (!strcmp(prop->prop.name, name)) {
933 *data = prop->prop.data;
934 *size = prop->prop.size;
935 return;
936 }
937 }
938}
939
940/*
941 * Add a string property to a node, or update it if it already exists.
942 *
943 * @param node The device tree node to add to.
944 * @param name The name of the new property.
945 * @param str The zero-terminated string to be stored in the property.
946 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200947void dt_add_string_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200948 const char *str)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200949{
Julius Werner0e9116f2019-05-13 17:30:31 -0700950 dt_add_bin_prop(node, name, (char *)str, strlen(str) + 1);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200951}
952
953/*
954 * Add a 32-bit integer property to a node, or update it if it already exists.
955 *
956 * @param node The device tree node to add to.
957 * @param name The name of the new property.
958 * @param val The integer to be stored in the property.
959 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200960void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200961{
Julius Werner9636a102019-05-03 17:36:43 -0700962 u32 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200963 *val_ptr = htobe32(val);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200964 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
965}
966
967/*
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200968 * Add a 64-bit integer property to a node, or update it if it already exists.
969 *
970 * @param node The device tree node to add to.
971 * @param name The name of the new property.
972 * @param val The integer to be stored in the property.
973 */
974void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
975{
Julius Werner9636a102019-05-03 17:36:43 -0700976 u64 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200977 *val_ptr = htobe64(val);
978 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
979}
980
981/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200982 * Add a 'reg' address list property to a node, or update it if it exists.
983 *
984 * @param node The device tree node to add to.
985 * @param addrs Array of address values to be stored in the property.
986 * @param sizes Array of corresponding size values to 'addrs'.
987 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
988 * @param addr_cells Value of #address-cells property valid for this node.
989 * @param size_cells Value of #size-cells property valid for this node.
990 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200991void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200992 int count, u32 addr_cells, u32 size_cells)
993{
994 int i;
995 size_t length = (addr_cells + size_cells) * sizeof(u32) * count;
Julius Werner9636a102019-05-03 17:36:43 -0700996 u8 *data = xmalloc(length);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200997 u8 *cur = data;
998
999 for (i = 0; i < count; i++) {
1000 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
1001 cur += addr_cells * sizeof(u32);
1002 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
1003 cur += size_cells * sizeof(u32);
1004 }
1005
1006 dt_add_bin_prop(node, "reg", data, length);
1007}
1008
1009/*
1010 * Fixups to apply to a kernel's device tree before booting it.
1011 */
1012
Patrick Rudolph666c1722018-04-03 09:57:33 +02001013struct list_node device_tree_fixups;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001014
Patrick Rudolph666c1722018-04-03 09:57:33 +02001015int dt_apply_fixups(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001016{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001017 struct device_tree_fixup *fixup;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001018 list_for_each(fixup, device_tree_fixups, list_node) {
1019 assert(fixup->fixup);
1020 if (fixup->fixup(fixup, tree))
1021 return 1;
1022 }
1023 return 0;
1024}
1025
Patrick Rudolph666c1722018-04-03 09:57:33 +02001026int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001027 void *data, size_t data_size, int create)
1028{
1029 char *path_copy, *prop_name;
Patrick Rudolph666c1722018-04-03 09:57:33 +02001030 struct device_tree_node *dt_node;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001031
1032 path_copy = strdup(path);
1033
1034 if (!path_copy) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001035 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
1036 path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001037 return 1;
1038 }
1039
1040 prop_name = strrchr(path_copy, '/');
1041 if (!prop_name) {
Patrick Rudolph679d6242018-07-11 13:53:04 +02001042 free(path_copy);
Patrick Rudolph666c1722018-04-03 09:57:33 +02001043 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001044 return 1;
1045 }
1046
1047 *prop_name++ = '\0'; /* Separate path from the property name. */
1048
Julius Wernerf36d53c2019-05-03 18:23:34 -07001049 dt_node = dt_find_node_by_path(tree, path_copy, NULL,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001050 NULL, create);
1051
1052 if (!dt_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001053 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001054 create ? "create" : "find", path_copy);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001055 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001056 return 1;
1057 }
1058
1059 dt_add_bin_prop(dt_node, prop_name, data, data_size);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001060 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001061
1062 return 0;
1063}
1064
1065/*
1066 * Prepare the /reserved-memory/ node.
1067 *
1068 * Technically, this can be called more than one time, to init and/or retrieve
1069 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
1070 *
1071 * @tree: Device tree to add/retrieve from.
1072 * @return: The /reserved-memory/ node (or NULL, if error).
1073 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001074struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001075{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001076 struct device_tree_node *reserved;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001077 u32 addr = 0, size = 0;
1078
Julius Wernerfbec63d2019-05-03 18:29:28 -07001079 reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001080 &size, 1);
1081 if (!reserved)
1082 return NULL;
1083
Julius Werner23df4772019-05-17 22:50:18 -07001084 /* Binding doc says this should have the same #{address,size}-cells as
1085 the root. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001086 dt_add_u32_prop(reserved, "#address-cells", addr);
1087 dt_add_u32_prop(reserved, "#size-cells", size);
Julius Werner23df4772019-05-17 22:50:18 -07001088 /* Binding doc says this should be empty (1:1 mapping from root). */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001089 dt_add_bin_prop(reserved, "ranges", NULL, 0);
1090
1091 return reserved;
1092}
Julius Werner735ddc92019-05-07 17:05:28 -07001093
1094/*
1095 * Increment a single phandle in prop at a given offset by a given adjustment.
1096 *
1097 * @param prop Property whose phandle should be adjusted.
1098 * @param adjustment Value that should be added to the existing phandle.
1099 * @param offset Byte offset of the phandle in the property data.
1100 *
1101 * @return New phandle value, or 0 on error.
1102 */
1103static uint32_t dt_adjust_phandle(struct device_tree_property *prop,
1104 uint32_t adjustment, uint32_t offset)
1105{
1106 if (offset + 4 > prop->prop.size)
1107 return 0;
1108
1109 uint32_t phandle = be32dec(prop->prop.data + offset);
1110 if (phandle == 0 ||
1111 phandle == FDT_PHANDLE_ILLEGAL ||
1112 phandle == 0xffffffff)
1113 return 0;
1114
1115 phandle += adjustment;
1116 if (phandle >= FDT_PHANDLE_ILLEGAL)
1117 return 0;
1118
1119 be32enc(prop->prop.data + offset, phandle);
1120 return phandle;
1121}
1122
1123/*
1124 * Adjust all phandles in subtree by adding a new base offset.
1125 *
1126 * @param node Root node of the subtree to work on.
1127 * @param base New phandle base to be added to all phandles.
1128 *
1129 * @return New highest phandle in the subtree, or 0 on error.
1130 */
1131static uint32_t dt_adjust_all_phandles(struct device_tree_node *node,
1132 uint32_t base)
1133{
Julius Werner23df4772019-05-17 22:50:18 -07001134 uint32_t new_max = MAX(base, 1); /* make sure we don't return 0 */
Julius Werner735ddc92019-05-07 17:05:28 -07001135 struct device_tree_property *prop;
1136 struct device_tree_node *child;
1137
1138 if (!node)
1139 return new_max;
1140
1141 list_for_each(prop, node->properties, list_node)
1142 if (dt_prop_is_phandle(prop)) {
1143 node->phandle = dt_adjust_phandle(prop, base, 0);
1144 if (!node->phandle)
1145 return 0;
1146 new_max = MAX(new_max, node->phandle);
Julius Werner23df4772019-05-17 22:50:18 -07001147 } /* no break -- can have more than one phandle prop */
Julius Werner735ddc92019-05-07 17:05:28 -07001148
1149 list_for_each(child, node->children, list_node)
1150 new_max = MAX(new_max, dt_adjust_all_phandles(child, base));
1151
1152 return new_max;
1153}
1154
1155/*
1156 * Apply a /__local_fixup__ subtree to the corresponding overlay subtree.
1157 *
1158 * @param node Root node of the overlay subtree to fix up.
1159 * @param node Root node of the /__local_fixup__ subtree.
1160 * @param base Adjustment that was added to phandles in the overlay.
1161 *
1162 * @return 0 on success, -1 on error.
1163 */
1164static int dt_fixup_locals(struct device_tree_node *node,
1165 struct device_tree_node *fixup, uint32_t base)
1166{
1167 struct device_tree_property *prop;
1168 struct device_tree_property *fixup_prop;
1169 struct device_tree_node *child;
1170 struct device_tree_node *fixup_child;
1171 int i;
1172
Julius Werner23df4772019-05-17 22:50:18 -07001173 /*
1174 * For local fixups the /__local_fixup__ subtree contains the same node
1175 * hierarchy as the main tree we're fixing up. Each property contains
1176 * the fixup offsets for the respective property in the main tree. For
1177 * each property in the fixup node, find the corresponding property in
1178 * the base node and apply fixups to all offsets it specifies.
1179 */
Julius Werner735ddc92019-05-07 17:05:28 -07001180 list_for_each(fixup_prop, fixup->properties, list_node) {
1181 struct device_tree_property *base_prop = NULL;
1182 list_for_each(prop, node->properties, list_node)
1183 if (!strcmp(prop->prop.name, fixup_prop->prop.name)) {
1184 base_prop = prop;
1185 break;
1186 }
1187
Julius Werner23df4772019-05-17 22:50:18 -07001188 /* We should always find a corresponding base prop for a fixup,
1189 and fixup props contain a list of 32-bit fixup offsets. */
Julius Werner735ddc92019-05-07 17:05:28 -07001190 if (!base_prop || fixup_prop->prop.size % sizeof(uint32_t))
1191 return -1;
1192
1193 for (i = 0; i < fixup_prop->prop.size; i += sizeof(uint32_t))
1194 if (!dt_adjust_phandle(base_prop, base, be32dec(
1195 fixup_prop->prop.data + i)))
1196 return -1;
1197 }
1198
Julius Werner23df4772019-05-17 22:50:18 -07001199 /* Now recursively descend both the base tree and the /__local_fixups__
1200 subtree in sync to apply all fixups. */
Julius Werner735ddc92019-05-07 17:05:28 -07001201 list_for_each(fixup_child, fixup->children, list_node) {
1202 struct device_tree_node *base_child = NULL;
1203 list_for_each(child, node->children, list_node)
1204 if (!strcmp(child->name, fixup_child->name)) {
1205 base_child = child;
1206 break;
1207 }
1208
Julius Werner23df4772019-05-17 22:50:18 -07001209 /* All fixup nodes should have a corresponding base node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001210 if (!base_child)
1211 return -1;
1212
1213 if (dt_fixup_locals(base_child, fixup_child, base) < 0)
1214 return -1;
1215 }
1216
1217 return 0;
1218}
1219
1220/*
1221 * Update all /__symbols__ properties in an overlay that start with
1222 * "/fragment@X/__overlay__" with corresponding path prefix in the base tree.
1223 *
1224 * @param symbols /__symbols__ done to update.
1225 * @param fragment /fragment@X node that references to should be updated.
1226 * @param base_path Path of base tree node that the fragment overlaid.
1227 */
1228static void dt_fix_symbols(struct device_tree_node *symbols,
1229 struct device_tree_node *fragment,
1230 const char *base_path)
1231{
1232 struct device_tree_property *prop;
Julius Werner23df4772019-05-17 22:50:18 -07001233 char buf[512]; /* Should be enough for maximum DT path length? */
1234 char node_path[64]; /* easily enough for /fragment@XXXX/__overlay__ */
Julius Werner735ddc92019-05-07 17:05:28 -07001235
Julius Werner23df4772019-05-17 22:50:18 -07001236 if (!symbols) /* If the overlay has no /__symbols__ node, we're done! */
Julius Werner735ddc92019-05-07 17:05:28 -07001237 return;
1238
1239 int len = snprintf(node_path, sizeof(node_path), "/%s/__overlay__",
1240 fragment->name);
1241
1242 list_for_each(prop, symbols->properties, list_node)
1243 if (!strncmp(prop->prop.data, node_path, len)) {
1244 prop->prop.size = snprintf(buf, sizeof(buf), "%s%s",
1245 base_path, (char *)prop->prop.data + len) + 1;
1246 free(prop->prop.data);
1247 prop->prop.data = strdup(buf);
1248 }
1249}
1250
1251/*
1252 * Fix up overlay according to a property in /__fixup__. If the fixed property
1253 * is a /fragment@X:target, also update /__symbols__ references to fragment.
1254 *
1255 * @params overlay Overlay to fix up.
1256 * @params fixup /__fixup__ property.
1257 * @params phandle phandle value to insert where the fixup points to.
1258 * @params base_path Path to the base DT node that the fixup points to.
1259 * @params overlay_symbols /__symbols__ node of the overlay.
1260 *
1261 * @return 0 on success, -1 on error.
1262 */
1263static int dt_fixup_external(struct device_tree *overlay,
1264 struct device_tree_property *fixup,
1265 uint32_t phandle, const char *base_path,
1266 struct device_tree_node *overlay_symbols)
1267{
1268 struct device_tree_property *prop;
1269
Julius Werner23df4772019-05-17 22:50:18 -07001270 /* External fixup properties are encoded as "<path>:<prop>:<offset>". */
Julius Werner735ddc92019-05-07 17:05:28 -07001271 char *entry = fixup->prop.data;
1272 while ((void *)entry < fixup->prop.data + fixup->prop.size) {
Julius Werner23df4772019-05-17 22:50:18 -07001273 /* okay to destroy fixup property value, won't need it again */
Julius Werner735ddc92019-05-07 17:05:28 -07001274 char *node_path = entry;
1275 entry = strchr(node_path, ':');
1276 if (!entry)
1277 return -1;
1278 *entry++ = '\0';
1279
1280 char *prop_name = entry;
1281 entry = strchr(prop_name, ':');
1282 if (!entry)
1283 return -1;
1284 *entry++ = '\0';
1285
1286 struct device_tree_node *ovl_node = dt_find_node_by_path(
1287 overlay, node_path, NULL, NULL, 0);
1288 if (!ovl_node || !isdigit(*entry))
1289 return -1;
1290
1291 struct device_tree_property *ovl_prop = NULL;
1292 list_for_each(prop, ovl_node->properties, list_node)
1293 if (!strcmp(prop->prop.name, prop_name)) {
1294 ovl_prop = prop;
1295 break;
1296 }
1297
Julius Werner23df4772019-05-17 22:50:18 -07001298 /* Move entry to first char after number, must be a '\0'. */
Julius Werner735ddc92019-05-07 17:05:28 -07001299 uint32_t offset = skip_atoi(&entry);
1300 if (!ovl_prop || offset + 4 > ovl_prop->prop.size || entry[0])
1301 return -1;
Julius Werner23df4772019-05-17 22:50:18 -07001302 entry++; /* jump over '\0' to potential next fixup */
Julius Werner735ddc92019-05-07 17:05:28 -07001303
1304 be32enc(ovl_prop->prop.data + offset, phandle);
1305
Julius Werner23df4772019-05-17 22:50:18 -07001306 /* If this is a /fragment@X:target property, update references
1307 to this fragment in the overlay __symbols__ now. */
Julius Werner735ddc92019-05-07 17:05:28 -07001308 if (offset == 0 && !strcmp(prop_name, "target") &&
Julius Werner23df4772019-05-17 22:50:18 -07001309 !strchr(node_path + 1, '/')) /* only toplevel nodes */
Julius Werner735ddc92019-05-07 17:05:28 -07001310 dt_fix_symbols(overlay_symbols, ovl_node, base_path);
1311 }
1312
1313 return 0;
1314}
1315
1316/*
1317 * Apply all /__fixup__ properties in the overlay. This will destroy the
1318 * property data in /__fixup__ and it should not be accessed again.
1319 *
1320 * @params tree Base device tree that the overlay updates.
1321 * @params symbols /__symbols__ node of the base device tree.
1322 * @params overlay Overlay to fix up.
1323 * @params fixups /__fixup__ node in the overlay.
1324 * @params overlay_symbols /__symbols__ node of the overlay.
1325 *
1326 * @return 0 on success, -1 on error.
1327 */
1328static int dt_fixup_all_externals(struct device_tree *tree,
1329 struct device_tree_node *symbols,
1330 struct device_tree *overlay,
1331 struct device_tree_node *fixups,
1332 struct device_tree_node *overlay_symbols)
1333{
1334 struct device_tree_property *fix;
1335
Julius Werner23df4772019-05-17 22:50:18 -07001336 /* If we have any external fixups, base tree must have /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001337 if (!symbols)
1338 return -1;
1339
Julius Werner23df4772019-05-17 22:50:18 -07001340 /*
1341 * Unlike /__local_fixups__, /__fixups__ is not a whole subtree that
1342 * mirrors the node hierarchy. It's just a directory of fixup properties
1343 * that each directly contain all information necessary to apply them.
1344 */
Julius Werner735ddc92019-05-07 17:05:28 -07001345 list_for_each(fix, fixups->properties, list_node) {
Julius Werner23df4772019-05-17 22:50:18 -07001346 /* The name of a fixup property is the label of the node we want
1347 a property to phandle-reference. Look up in /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001348 const char *path = dt_find_string_prop(symbols, fix->prop.name);
1349 if (!path)
1350 return -1;
1351
Elyes HAOUAS0afaff22021-01-16 15:02:31 +01001352 /* Find node the label pointed to figure out its phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001353 struct device_tree_node *node = dt_find_node_by_path(tree, path,
1354 NULL, NULL, 0);
1355 if (!node)
1356 return -1;
1357
Julius Werner23df4772019-05-17 22:50:18 -07001358 /* Write into the overlay property(s) pointing to that node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001359 if (dt_fixup_external(overlay, fix, node->phandle,
1360 path, overlay_symbols) < 0)
1361 return -1;
1362 }
1363
1364 return 0;
1365}
1366
1367/*
1368 * Copy all nodes and properties from one DT subtree into another. This is a
1369 * shallow copy so both trees will point to the same property data afterwards.
1370 *
1371 * @params dst Destination subtree to copy into.
1372 * @params src Source subtree to copy from.
1373 * @params upd 1 to overwrite same-name properties, 0 to discard them.
1374 */
1375static void dt_copy_subtree(struct device_tree_node *dst,
1376 struct device_tree_node *src, int upd)
1377{
1378 struct device_tree_property *prop;
1379 struct device_tree_property *src_prop;
1380 list_for_each(src_prop, src->properties, list_node) {
1381 if (dt_prop_is_phandle(src_prop) ||
1382 !strcmp(src_prop->prop.name, "name")) {
1383 printk(BIOS_DEBUG,
1384 "WARNING: ignoring illegal overlay prop '%s'\n",
1385 src_prop->prop.name);
1386 continue;
1387 }
1388
1389 struct device_tree_property *dst_prop = NULL;
1390 list_for_each(prop, dst->properties, list_node)
1391 if (!strcmp(prop->prop.name, src_prop->prop.name)) {
1392 dst_prop = prop;
1393 break;
1394 }
1395
1396 if (dst_prop) {
1397 if (!upd) {
1398 printk(BIOS_DEBUG,
1399 "WARNING: ignoring prop update '%s'\n",
1400 src_prop->prop.name);
1401 continue;
1402 }
1403 } else {
1404 dst_prop = xzalloc(sizeof(*dst_prop));
1405 list_insert_after(&dst_prop->list_node,
1406 &dst->properties);
1407 }
1408
1409 dst_prop->prop = src_prop->prop;
1410 }
1411
1412 struct device_tree_node *node;
1413 struct device_tree_node *src_node;
1414 list_for_each(src_node, src->children, list_node) {
1415 struct device_tree_node *dst_node = NULL;
1416 list_for_each(node, dst->children, list_node)
1417 if (!strcmp(node->name, src_node->name)) {
1418 dst_node = node;
1419 break;
1420 }
1421
1422 if (!dst_node) {
1423 dst_node = xzalloc(sizeof(*dst_node));
1424 *dst_node = *src_node;
1425 list_insert_after(&dst_node->list_node, &dst->children);
1426 } else {
1427 dt_copy_subtree(dst_node, src_node, upd);
1428 }
1429 }
1430}
1431
1432/*
1433 * Apply an overlay /fragment@X node to a base device tree.
1434 *
1435 * @param tree Base device tree.
1436 * @param fragment /fragment@X node.
1437 * @params overlay_symbols /__symbols__ node of the overlay.
1438 *
1439 * @return 0 on success, -1 on error.
1440 */
1441static int dt_import_fragment(struct device_tree *tree,
1442 struct device_tree_node *fragment,
1443 struct device_tree_node *overlay_symbols)
1444{
Julius Werner23df4772019-05-17 22:50:18 -07001445 /* The actual overlaid nodes/props are in an __overlay__ child node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001446 static const char *overlay_path[] = { "__overlay__", NULL };
1447 struct device_tree_node *overlay = dt_find_node(fragment, overlay_path,
1448 NULL, NULL, 0);
1449
Julius Werner23df4772019-05-17 22:50:18 -07001450 /* If it doesn't have an __overlay__ child, it's not a fragment. */
Julius Werner735ddc92019-05-07 17:05:28 -07001451 if (!overlay)
1452 return 0;
1453
Julius Werner23df4772019-05-17 22:50:18 -07001454 /* Target node of the fragment can be given by path or by phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001455 struct device_tree_property *prop;
1456 struct device_tree_property *phandle = NULL;
1457 struct device_tree_property *path = NULL;
1458 list_for_each(prop, fragment->properties, list_node) {
1459 if (!strcmp(prop->prop.name, "target")) {
1460 phandle = prop;
Julius Werner23df4772019-05-17 22:50:18 -07001461 break; /* phandle target has priority, stop looking */
Julius Werner735ddc92019-05-07 17:05:28 -07001462 }
1463 if (!strcmp(prop->prop.name, "target-path"))
1464 path = prop;
1465 }
1466
1467 struct device_tree_node *target = NULL;
1468 if (phandle) {
1469 if (phandle->prop.size != sizeof(uint32_t))
1470 return -1;
1471 target = dt_find_node_by_phandle(tree->root,
1472 be32dec(phandle->prop.data));
Julius Werner23df4772019-05-17 22:50:18 -07001473 /* Symbols already updated as part of dt_fixup_external(). */
Julius Werner735ddc92019-05-07 17:05:28 -07001474 } else if (path) {
1475 target = dt_find_node_by_path(tree, path->prop.data,
1476 NULL, NULL, 0);
1477 dt_fix_symbols(overlay_symbols, fragment, path->prop.data);
1478 }
1479 if (!target)
1480 return -1;
1481
1482 dt_copy_subtree(target, overlay, 1);
1483 return 0;
1484}
1485
1486/*
1487 * Apply a device tree overlay to a base device tree. This will
1488 * destroy/incorporate the overlay data, so it should not be freed or reused.
1489 * See dtc.git/Documentation/dt-object-internal.txt for overlay format details.
1490 *
1491 * @param tree Unflattened base device tree to add the overlay into.
1492 * @param overlay Unflattened overlay device tree to apply to the base.
1493 *
1494 * @return 0 on success, -1 on error.
1495 */
1496int dt_apply_overlay(struct device_tree *tree, struct device_tree *overlay)
1497{
Julius Werner23df4772019-05-17 22:50:18 -07001498 /*
1499 * First, we need to make sure phandles inside the overlay don't clash
1500 * with those in the base tree. We just define the highest phandle value
1501 * in the base tree as the "phandle offset" for this overlay and
1502 * increment all phandles in it by that value.
1503 */
Julius Werner735ddc92019-05-07 17:05:28 -07001504 uint32_t phandle_base = tree->max_phandle;
1505 uint32_t new_max = dt_adjust_all_phandles(overlay->root, phandle_base);
1506 if (!new_max) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001507 printk(BIOS_ERR, "invalid phandles in overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001508 return -1;
1509 }
1510 tree->max_phandle = new_max;
1511
Julius Werner23df4772019-05-17 22:50:18 -07001512 /* Now that we changed phandles in the overlay, we need to update any
1513 nodes referring to them. Those are listed in /__local_fixups__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001514 struct device_tree_node *local_fixups = dt_find_node_by_path(overlay,
1515 "/__local_fixups__", NULL, NULL, 0);
1516 if (local_fixups && dt_fixup_locals(overlay->root, local_fixups,
1517 phandle_base) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001518 printk(BIOS_ERR, "invalid local fixups in overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001519 return -1;
1520 }
1521
Julius Werner23df4772019-05-17 22:50:18 -07001522 /*
1523 * Besides local phandle references (from nodes within the overlay to
1524 * other nodes within the overlay), the overlay may also contain phandle
1525 * references to the base tree. These are stored with invalid values and
1526 * must be updated now. /__symbols__ contains a list of all labels in
1527 * the base tree, and /__fixups__ describes all nodes in the overlay
1528 * that contain external phandle references.
1529 * We also take this opportunity to update all /fragment@X/__overlay__/
1530 * prefixes in the overlay's /__symbols__ node to the correct path that
1531 * the fragment will be placed in later, since this is the only step
1532 * where we have all necessary information for that easily available.
1533 */
Julius Werner735ddc92019-05-07 17:05:28 -07001534 struct device_tree_node *symbols = dt_find_node_by_path(tree,
1535 "/__symbols__", NULL, NULL, 0);
1536 struct device_tree_node *fixups = dt_find_node_by_path(overlay,
1537 "/__fixups__", NULL, NULL, 0);
1538 struct device_tree_node *overlay_symbols = dt_find_node_by_path(overlay,
1539 "/__symbols__", NULL, NULL, 0);
1540 if (fixups && dt_fixup_all_externals(tree, symbols, overlay,
1541 fixups, overlay_symbols) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001542 printk(BIOS_ERR, "cannot match external fixups from overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001543 return -1;
1544 }
1545
Julius Werner23df4772019-05-17 22:50:18 -07001546 /* After all this fixing up, we can finally merge overlay into the tree
1547 (one fragment at a time, because for some reason it's split up). */
Julius Werner735ddc92019-05-07 17:05:28 -07001548 struct device_tree_node *fragment;
1549 list_for_each(fragment, overlay->root->children, list_node)
1550 if (dt_import_fragment(tree, fragment, overlay_symbols) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001551 printk(BIOS_ERR, "bad DT fragment '%s'\n",
Julius Werner735ddc92019-05-07 17:05:28 -07001552 fragment->name);
1553 return -1;
1554 }
1555
Julius Werner23df4772019-05-17 22:50:18 -07001556 /*
1557 * We need to also update /__symbols__ to include labels from this
1558 * overlay, in case we want to load further overlays with external
1559 * phandle references to it. If the base tree already has a /__symbols__
1560 * we merge them together, otherwise we just insert the overlay's
1561 * /__symbols__ node into the base tree root.
1562 */
Julius Werner735ddc92019-05-07 17:05:28 -07001563 if (overlay_symbols) {
1564 if (symbols)
1565 dt_copy_subtree(symbols, overlay_symbols, 0);
1566 else
1567 list_insert_after(&overlay_symbols->list_node,
1568 &tree->root->children);
1569 }
1570
1571 return 0;
1572}