blob: a36798619b78cf6dc104425c7a198df3f68c3443 [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>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02009#include <stdint.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +020010#include <string.h>
11#include <stddef.h>
12#include <stdlib.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020013
14/*
15 * Functions for picking apart flattened trees.
16 */
17
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020018int fdt_next_property(const void *blob, uint32_t offset,
19 struct fdt_property *prop)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020020{
Patrick Rudolph666c1722018-04-03 09:57:33 +020021 struct fdt_header *header = (struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020022 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
23
24 int index = 0;
Patrick Rudolph666c1722018-04-03 09:57:33 +020025 if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020026 return 0;
27
Patrick Rudolph666c1722018-04-03 09:57:33 +020028 uint32_t size = be32toh(ptr[index++]);
29 uint32_t name_offset = be32toh(ptr[index++]);
30 name_offset += be32toh(header->strings_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020031
32 if (prop) {
33 prop->name = (char *)((uint8_t *)blob + name_offset);
34 prop->data = &ptr[index];
35 prop->size = size;
36 }
37
Patrick Rudolph666c1722018-04-03 09:57:33 +020038 index += DIV_ROUND_UP(size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020039
Patrick Rudolph666c1722018-04-03 09:57:33 +020040 return index * sizeof(uint32_t);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020041}
42
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020043int fdt_node_name(const void *blob, uint32_t offset, const char **name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020044{
45 uint8_t *ptr = ((uint8_t *)blob) + offset;
Julius Wernera5ea3a22019-05-07 17:38:12 -070046 if (be32dec(ptr) != FDT_TOKEN_BEGIN_NODE)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020047 return 0;
48
49 ptr += 4;
50 if (name)
51 *name = (char *)ptr;
Patrick Rudolph666c1722018-04-03 09:57:33 +020052 return ALIGN_UP(strlen((char *)ptr) + 1, sizeof(uint32_t)) + 4;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020053}
54
Julius Werner6702b682019-05-03 18:13:53 -070055static int dt_prop_is_phandle(struct device_tree_property *prop)
56{
57 return !(strcmp("phandle", prop->prop.name) &&
58 strcmp("linux,phandle", prop->prop.name));
59}
60
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020061
62
63/*
64 * Functions for printing flattened trees.
65 */
66
67static void print_indent(int depth)
68{
Julius Werner0d746532019-05-06 19:35:56 -070069 printk(BIOS_DEBUG, "%*s", depth * 8, "");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020070}
71
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020072static void print_property(const struct fdt_property *prop, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020073{
Julius Werner0d746532019-05-06 19:35:56 -070074 int is_string = prop->size > 0 &&
75 ((char *)prop->data)[prop->size - 1] == '\0';
76
Maximilian Brune77eaec62023-09-20 05:12:04 +020077 if (is_string) {
78 for (int i = 0; i < prop->size - 1; i++) {
79 if (!isprint(((char *)prop->data)[i])) {
Julius Werner0d746532019-05-06 19:35:56 -070080 is_string = 0;
Maximilian Brune77eaec62023-09-20 05:12:04 +020081 break;
82 }
83 }
84 }
Julius Werner0d746532019-05-06 19:35:56 -070085
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020086 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -070087 if (is_string) {
88 printk(BIOS_DEBUG, "%s = \"%s\";\n",
89 prop->name, (const char *)prop->data);
90 } else {
91 printk(BIOS_DEBUG, "%s = < ", prop->name);
92 for (int i = 0; i < MIN(128, prop->size); i += 4) {
93 uint32_t val = 0;
94 for (int j = 0; j < MIN(4, prop->size - i); j++)
95 val |= ((uint8_t *)prop->data)[i + j] <<
96 (24 - j * 8);
97 printk(BIOS_DEBUG, "%#.2x ", val);
98 }
99 if (prop->size > 128)
100 printk(BIOS_DEBUG, "...");
101 printk(BIOS_DEBUG, ">;\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200102 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200103}
104
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200105static int print_flat_node(const void *blob, uint32_t start_offset, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200106{
107 int offset = start_offset;
108 const char *name;
109 int size;
110
111 size = fdt_node_name(blob, offset, &name);
112 if (!size)
113 return 0;
114 offset += size;
115
116 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -0700117 printk(BIOS_DEBUG, "%s {\n", name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200118
Patrick Rudolph666c1722018-04-03 09:57:33 +0200119 struct fdt_property prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200120 while ((size = fdt_next_property(blob, offset, &prop))) {
121 print_property(&prop, depth + 1);
122
123 offset += size;
124 }
125
Julius Werner23df4772019-05-17 22:50:18 -0700126 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700127
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200128 while ((size = print_flat_node(blob, offset, depth + 1)))
129 offset += size;
130
Julius Werner0d746532019-05-06 19:35:56 -0700131 print_indent(depth);
132 printk(BIOS_DEBUG, "}\n");
133
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200134 return offset - start_offset + sizeof(uint32_t);
135}
136
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200137void fdt_print_node(const void *blob, uint32_t offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200138{
139 print_flat_node(blob, offset, 0);
140}
141
142
143
144/*
145 * A utility function to skip past nodes in flattened trees.
146 */
147
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200148int fdt_skip_node(const void *blob, uint32_t start_offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200149{
150 int offset = start_offset;
151 int size;
152
153 const char *name;
154 size = fdt_node_name(blob, offset, &name);
155 if (!size)
156 return 0;
157 offset += size;
158
159 while ((size = fdt_next_property(blob, offset, NULL)))
160 offset += size;
161
162 while ((size = fdt_skip_node(blob, offset)))
163 offset += size;
164
165 return offset - start_offset + sizeof(uint32_t);
166}
167
168
169
170/*
171 * Functions to turn a flattened tree into an unflattened one.
172 */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200173
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200174static int fdt_unflatten_node(const void *blob, uint32_t start_offset,
Julius Werner6702b682019-05-03 18:13:53 -0700175 struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200176 struct device_tree_node **new_node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200177{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200178 struct list_node *last;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200179 int offset = start_offset;
180 const char *name;
181 int size;
182
183 size = fdt_node_name(blob, offset, &name);
184 if (!size)
185 return 0;
186 offset += size;
187
Julius Werner9636a102019-05-03 17:36:43 -0700188 struct device_tree_node *node = xzalloc(sizeof(*node));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200189 *new_node = node;
190 node->name = name;
191
Patrick Rudolph666c1722018-04-03 09:57:33 +0200192 struct fdt_property fprop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200193 last = &node->properties;
194 while ((size = fdt_next_property(blob, offset, &fprop))) {
Julius Werner9636a102019-05-03 17:36:43 -0700195 struct device_tree_property *prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200196 prop->prop = fprop;
197
Julius Werner6702b682019-05-03 18:13:53 -0700198 if (dt_prop_is_phandle(prop)) {
199 node->phandle = be32dec(prop->prop.data);
200 if (node->phandle > tree->max_phandle)
201 tree->max_phandle = node->phandle;
202 }
203
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200204 list_insert_after(&prop->list_node, last);
205 last = &prop->list_node;
206
207 offset += size;
208 }
209
Patrick Rudolph666c1722018-04-03 09:57:33 +0200210 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200211 last = &node->children;
Julius Werner6702b682019-05-03 18:13:53 -0700212 while ((size = fdt_unflatten_node(blob, offset, tree, &child))) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200213 list_insert_after(&child->list_node, last);
214 last = &child->list_node;
215
216 offset += size;
217 }
218
219 return offset - start_offset + sizeof(uint32_t);
220}
221
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200222static int fdt_unflatten_map_entry(const void *blob, uint32_t offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200223 struct device_tree_reserve_map_entry **new)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200224{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200225 const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset);
226 const uint64_t start = be64toh(ptr[0]);
227 const uint64_t size = be64toh(ptr[1]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200228
229 if (!size)
230 return 0;
231
Julius Werner9636a102019-05-03 17:36:43 -0700232 struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200233 *new = entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200234 entry->start = start;
235 entry->size = size;
236
237 return sizeof(uint64_t) * 2;
238}
239
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200240struct device_tree *fdt_unflatten(const void *blob)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200241{
Julius Werner9636a102019-05-03 17:36:43 -0700242 struct device_tree *tree = xzalloc(sizeof(*tree));
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200243 const struct fdt_header *header = (const struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200244 tree->header = header;
245
Julius Werner73eaec82019-05-03 17:58:07 -0700246 uint32_t magic = be32toh(header->magic);
247 uint32_t version = be32toh(header->version);
248 uint32_t last_comp_version = be32toh(header->last_comp_version);
249
250 if (magic != FDT_HEADER_MAGIC) {
251 printk(BIOS_DEBUG, "Invalid device tree magic %#.8x!\n", magic);
Jacob Garber698d83a2019-06-07 10:28:54 -0600252 free(tree);
Julius Werner73eaec82019-05-03 17:58:07 -0700253 return NULL;
254 }
255 if (last_comp_version > FDT_SUPPORTED_VERSION) {
256 printk(BIOS_DEBUG, "Unsupported device tree version %u(>=%u)\n",
257 version, last_comp_version);
Jacob Garber698d83a2019-06-07 10:28:54 -0600258 free(tree);
Julius Werner73eaec82019-05-03 17:58:07 -0700259 return NULL;
260 }
261 if (version > FDT_SUPPORTED_VERSION)
Elyes Haouasd7326282022-12-21 08:16:03 +0100262 printk(BIOS_NOTICE, "FDT version %u too new, should add support!\n",
Julius Werner73eaec82019-05-03 17:58:07 -0700263 version);
264
Patrick Rudolph666c1722018-04-03 09:57:33 +0200265 uint32_t struct_offset = be32toh(header->structure_offset);
266 uint32_t strings_offset = be32toh(header->strings_offset);
267 uint32_t reserve_offset = be32toh(header->reserve_map_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200268 uint32_t min_offset = 0;
269 min_offset = MIN(struct_offset, strings_offset);
270 min_offset = MIN(min_offset, reserve_offset);
Julius Werner23df4772019-05-17 22:50:18 -0700271 /* Assume everything up to the first non-header component is part of
272 the header and needs to be preserved. This will protect us against
273 new elements being added in the future. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200274 tree->header_size = min_offset;
275
Patrick Rudolph666c1722018-04-03 09:57:33 +0200276 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200277 uint32_t offset = reserve_offset;
278 int size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200279 struct list_node *last = &tree->reserve_map;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200280 while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) {
281 list_insert_after(&entry->list_node, last);
282 last = &entry->list_node;
283
284 offset += size;
285 }
286
Julius Werner6702b682019-05-03 18:13:53 -0700287 fdt_unflatten_node(blob, struct_offset, tree, &tree->root);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200288
289 return tree;
290}
291
292
293
294/*
Patrick Rudolph666c1722018-04-03 09:57:33 +0200295 * Functions to find the size of the device tree if it was flattened.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200296 */
297
Patrick Rudolph666c1722018-04-03 09:57:33 +0200298static void dt_flat_prop_size(struct device_tree_property *prop,
299 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200300{
Julius Werner23df4772019-05-17 22:50:18 -0700301 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200302 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700303 /* Size. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200304 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700305 /* Name offset. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200306 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700307 /* Property value. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200308 *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200309
Julius Werner23df4772019-05-17 22:50:18 -0700310 /* Property name. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200311 *strings_size += strlen(prop->prop.name) + 1;
312}
313
Patrick Rudolph666c1722018-04-03 09:57:33 +0200314static void dt_flat_node_size(struct device_tree_node *node,
315 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200316{
Julius Werner23df4772019-05-17 22:50:18 -0700317 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200318 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700319 /* Node name. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200320 *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200321
Patrick Rudolph666c1722018-04-03 09:57:33 +0200322 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200323 list_for_each(prop, node->properties, list_node)
324 dt_flat_prop_size(prop, struct_size, strings_size);
325
Patrick Rudolph666c1722018-04-03 09:57:33 +0200326 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200327 list_for_each(child, node->children, list_node)
328 dt_flat_node_size(child, struct_size, strings_size);
329
Julius Werner23df4772019-05-17 22:50:18 -0700330 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200331 *struct_size += sizeof(uint32_t);
332}
333
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200334uint32_t dt_flat_size(const struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200335{
336 uint32_t size = tree->header_size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200337 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200338 list_for_each(entry, tree->reserve_map, list_node)
339 size += sizeof(uint64_t) * 2;
340 size += sizeof(uint64_t) * 2;
341
342 uint32_t struct_size = 0;
343 uint32_t strings_size = 0;
344 dt_flat_node_size(tree->root, &struct_size, &strings_size);
345
346 size += struct_size;
Julius Werner23df4772019-05-17 22:50:18 -0700347 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200348 size += sizeof(uint32_t);
349
350 size += strings_size;
351
352 return size;
353}
354
355
356
357/*
358 * Functions to flatten a device tree.
359 */
360
Patrick Rudolph666c1722018-04-03 09:57:33 +0200361static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200362 void **map_start)
363{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200364 ((uint64_t *)*map_start)[0] = htobe64(entry->start);
365 ((uint64_t *)*map_start)[1] = htobe64(entry->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200366 *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2;
367}
368
Patrick Rudolph666c1722018-04-03 09:57:33 +0200369static void dt_flatten_prop(struct device_tree_property *prop,
370 void **struct_start, void *strings_base,
371 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200372{
373 uint8_t *dstruct = (uint8_t *)*struct_start;
374 uint8_t *dstrings = (uint8_t *)*strings_start;
375
Julius Wernera5ea3a22019-05-07 17:38:12 -0700376 be32enc(dstruct, FDT_TOKEN_PROPERTY);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200377 dstruct += sizeof(uint32_t);
378
Julius Wernera5ea3a22019-05-07 17:38:12 -0700379 be32enc(dstruct, prop->prop.size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200380 dstruct += sizeof(uint32_t);
381
382 uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base;
Julius Wernera5ea3a22019-05-07 17:38:12 -0700383 be32enc(dstruct, name_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200384 dstruct += sizeof(uint32_t);
385
386 strcpy((char *)dstrings, prop->prop.name);
387 dstrings += strlen(prop->prop.name) + 1;
388
389 memcpy(dstruct, prop->prop.data, prop->prop.size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200390 dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200391
392 *struct_start = dstruct;
393 *strings_start = dstrings;
394}
395
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200396static void dt_flatten_node(const struct device_tree_node *node,
397 void **struct_start, void *strings_base,
398 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200399{
400 uint8_t *dstruct = (uint8_t *)*struct_start;
401 uint8_t *dstrings = (uint8_t *)*strings_start;
402
Julius Wernera5ea3a22019-05-07 17:38:12 -0700403 be32enc(dstruct, FDT_TOKEN_BEGIN_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200404 dstruct += sizeof(uint32_t);
405
406 strcpy((char *)dstruct, node->name);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200407 dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200408
Patrick Rudolph666c1722018-04-03 09:57:33 +0200409 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200410 list_for_each(prop, node->properties, list_node)
411 dt_flatten_prop(prop, (void **)&dstruct, strings_base,
412 (void **)&dstrings);
413
Patrick Rudolph666c1722018-04-03 09:57:33 +0200414 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200415 list_for_each(child, node->children, list_node)
416 dt_flatten_node(child, (void **)&dstruct, strings_base,
417 (void **)&dstrings);
418
Julius Wernera5ea3a22019-05-07 17:38:12 -0700419 be32enc(dstruct, FDT_TOKEN_END_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200420 dstruct += sizeof(uint32_t);
421
422 *struct_start = dstruct;
423 *strings_start = dstrings;
424}
425
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200426void dt_flatten(const struct device_tree *tree, void *start_dest)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200427{
428 uint8_t *dest = (uint8_t *)start_dest;
429
430 memcpy(dest, tree->header, tree->header_size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200431 struct fdt_header *header = (struct fdt_header *)dest;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200432 dest += tree->header_size;
433
Patrick Rudolph666c1722018-04-03 09:57:33 +0200434 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200435 list_for_each(entry, tree->reserve_map, list_node)
436 dt_flatten_map_entry(entry, (void **)&dest);
437 ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0;
438 dest += sizeof(uint64_t) * 2;
439
440 uint32_t struct_size = 0;
441 uint32_t strings_size = 0;
442 dt_flat_node_size(tree->root, &struct_size, &strings_size);
443
444 uint8_t *struct_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200445 header->structure_offset = htobe32(dest - (uint8_t *)start_dest);
446 header->structure_size = htobe32(struct_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200447 dest += struct_size;
448
Patrick Rudolph666c1722018-04-03 09:57:33 +0200449 *((uint32_t *)dest) = htobe32(FDT_TOKEN_END);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200450 dest += sizeof(uint32_t);
451
452 uint8_t *strings_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200453 header->strings_offset = htobe32(dest - (uint8_t *)start_dest);
454 header->strings_size = htobe32(strings_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200455 dest += strings_size;
456
457 dt_flatten_node(tree->root, (void **)&struct_start, strings_start,
458 (void **)&strings_start);
459
Patrick Rudolph666c1722018-04-03 09:57:33 +0200460 header->totalsize = htobe32(dest - (uint8_t *)start_dest);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200461}
462
463
464
465/*
466 * Functions for printing a non-flattened device tree.
467 */
468
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200469static void print_node(const struct device_tree_node *node, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200470{
471 print_indent(depth);
Julius Werner23df4772019-05-17 22:50:18 -0700472 if (depth == 0) /* root node has no name, print a starting slash */
Julius Werner0d746532019-05-06 19:35:56 -0700473 printk(BIOS_DEBUG, "/");
474 printk(BIOS_DEBUG, "%s {\n", node->name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200475
Patrick Rudolph666c1722018-04-03 09:57:33 +0200476 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200477 list_for_each(prop, node->properties, list_node)
478 print_property(&prop->prop, depth + 1);
479
Julius Werner23df4772019-05-17 22:50:18 -0700480 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700481
Patrick Rudolph666c1722018-04-03 09:57:33 +0200482 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200483 list_for_each(child, node->children, list_node)
484 print_node(child, depth + 1);
Julius Werner0d746532019-05-06 19:35:56 -0700485
486 print_indent(depth);
487 printk(BIOS_DEBUG, "};\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200488}
489
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200490void dt_print_node(const struct device_tree_node *node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200491{
492 print_node(node, 0);
493}
494
495
496
497/*
498 * Functions for reading and manipulating an unflattened device tree.
499 */
500
501/*
502 * Read #address-cells and #size-cells properties from a node.
503 *
504 * @param node The device tree node to read from.
505 * @param addrcp Pointer to store #address-cells in, skipped if NULL.
506 * @param sizecp Pointer to store #size-cells in, skipped if NULL.
507 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200508void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
509 u32 *sizecp)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200510{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200511 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200512 list_for_each(prop, node->properties, list_node) {
513 if (addrcp && !strcmp("#address-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700514 *addrcp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200515 if (sizecp && !strcmp("#size-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700516 *sizecp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200517 }
518}
519
520/*
521 * Find a node from a device tree path, relative to a parent node.
522 *
523 * @param parent The node from which to start the relative path lookup.
524 * @param path An array of path component strings that will be looked
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200525 * up in order to find the node. Must be terminated with
526 * a NULL pointer. Example: {'firmware', 'coreboot', NULL}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200527 * @param addrcp Pointer that will be updated with any #address-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200528 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200529 * @param sizecp Pointer that will be updated with any #size-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200530 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200531 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
532 * @return The found/created node, or NULL.
533 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200534struct device_tree_node *dt_find_node(struct device_tree_node *parent,
535 const char **path, u32 *addrcp,
536 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200537{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200538 struct device_tree_node *node, *found = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200539
Julius Werner23df4772019-05-17 22:50:18 -0700540 /* Update #address-cells and #size-cells for this level. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200541 dt_read_cell_props(parent, addrcp, sizecp);
542
543 if (!*path)
544 return parent;
545
Julius Werner23df4772019-05-17 22:50:18 -0700546 /* Find the next node in the path, if it exists. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200547 list_for_each(node, parent->children, list_node) {
548 if (!strcmp(node->name, *path)) {
549 found = node;
550 break;
551 }
552 }
553
Julius Werner23df4772019-05-17 22:50:18 -0700554 /* Otherwise create it or return NULL. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200555 if (!found) {
556 if (!create)
557 return NULL;
558
Sergii Dmytruk206328d2022-03-13 18:23:17 +0200559 found = calloc(1, sizeof(*found));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200560 if (!found)
561 return NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200562 found->name = strdup(*path);
563 if (!found->name)
564 return NULL;
565
566 list_insert_after(&found->list_node, &parent->children);
567 }
568
569 return dt_find_node(found, path + 1, addrcp, sizecp, create);
570}
571
572/*
Julius Wernerf36d53c2019-05-03 18:23:34 -0700573 * Find a node in the tree from a string device tree path.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200574 *
Julius Wernerf36d53c2019-05-03 18:23:34 -0700575 * @param tree The device tree to search.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200576 * @param path A string representing a path in the device tree, with
Julius Wernerfbec63d2019-05-03 18:29:28 -0700577 * nodes separated by '/'. Example: "/firmware/coreboot"
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200578 * @param addrcp Pointer that will be updated with any #address-cells
579 * value found in the path. May be NULL to ignore.
580 * @param sizecp Pointer that will be updated with any #size-cells
581 * value found in the path. May be NULL to ignore.
582 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
583 * @return The found/created node, or NULL.
584 *
Julius Werner6d5695f2019-05-06 19:23:28 -0700585 * It is the caller responsibility to provide a path string that doesn't end
586 * with a '/' and doesn't contain any "//". If the path does not start with a
587 * '/', the first segment is interpreted as an alias. */
Julius Wernerf36d53c2019-05-03 18:23:34 -0700588struct device_tree_node *dt_find_node_by_path(struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200589 const char *path, u32 *addrcp,
590 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200591{
Julius Werner6d5695f2019-05-06 19:23:28 -0700592 char *sub_path;
593 char *duped_str;
594 struct device_tree_node *parent;
595 char *next_slash;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200596 /* Hopefully enough depth for any node. */
597 const char *path_array[15];
598 int i;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200599 struct device_tree_node *node = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200600
Julius Werner23df4772019-05-17 22:50:18 -0700601 if (path[0] == '/') { /* regular path */
602 if (path[1] == '\0') { /* special case: "/" is root node */
Julius Werner6d5695f2019-05-06 19:23:28 -0700603 dt_read_cell_props(tree->root, addrcp, sizecp);
604 return tree->root;
605 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200606
Julius Werner6d5695f2019-05-06 19:23:28 -0700607 sub_path = duped_str = strdup(&path[1]);
608 if (!sub_path)
609 return NULL;
610
611 parent = tree->root;
Julius Werner23df4772019-05-17 22:50:18 -0700612 } else { /* alias */
Julius Werner6d5695f2019-05-06 19:23:28 -0700613 char *alias;
614
615 alias = duped_str = strdup(path);
616 if (!alias)
617 return NULL;
618
619 sub_path = strchr(alias, '/');
620 if (sub_path)
621 *sub_path = '\0';
622
623 parent = dt_find_node_by_alias(tree, alias);
624 if (!parent) {
625 printk(BIOS_DEBUG,
626 "Could not find node '%s', alias '%s' does not exist\n",
627 path, alias);
628 free(duped_str);
629 return NULL;
630 }
631
632 if (!sub_path) {
Julius Werner23df4772019-05-17 22:50:18 -0700633 /* it's just the alias, no sub-path */
Julius Werner6d5695f2019-05-06 19:23:28 -0700634 free(duped_str);
635 return parent;
636 }
637
638 sub_path++;
639 }
640
641 next_slash = sub_path;
642 path_array[0] = sub_path;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200643 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200644 next_slash = strchr(next_slash, '/');
645 if (!next_slash)
646 break;
647
648 *next_slash++ = '\0';
649 path_array[i] = next_slash;
650 }
651
652 if (!next_slash) {
653 path_array[i] = NULL;
Julius Werner6d5695f2019-05-06 19:23:28 -0700654 node = dt_find_node(parent, path_array,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200655 addrcp, sizecp, create);
656 }
657
Julius Werner6d5695f2019-05-06 19:23:28 -0700658 free(duped_str);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200659 return node;
660}
661
Julius Werner6d5695f2019-05-06 19:23:28 -0700662/*
663 * Find a node from an alias
664 *
665 * @param tree The device tree.
666 * @param alias The alias name.
667 * @return The found node, or NULL.
668 */
669struct device_tree_node *dt_find_node_by_alias(struct device_tree *tree,
670 const char *alias)
671{
672 struct device_tree_node *node;
673 const char *alias_path;
674
675 node = dt_find_node_by_path(tree, "/aliases", NULL, NULL, 0);
676 if (!node)
677 return NULL;
678
679 alias_path = dt_find_string_prop(node, alias);
680 if (!alias_path)
681 return NULL;
682
683 return dt_find_node_by_path(tree, alias_path, NULL, NULL, 0);
684}
685
Julius Werner6702b682019-05-03 18:13:53 -0700686struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root,
687 uint32_t phandle)
688{
689 if (!root)
690 return NULL;
691
692 if (root->phandle == phandle)
693 return root;
694
695 struct device_tree_node *node;
696 struct device_tree_node *result;
697 list_for_each(node, root->children, list_node) {
698 result = dt_find_node_by_phandle(node, phandle);
699 if (result)
700 return result;
701 }
702
703 return NULL;
704}
705
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200706/*
707 * Check if given node is compatible.
708 *
709 * @param node The node which is to be checked for compatible property.
710 * @param compat The compatible string to match.
711 * @return 1 = compatible, 0 = not compatible.
712 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200713static int dt_check_compat_match(struct device_tree_node *node,
714 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200715{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200716 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200717
718 list_for_each(prop, node->properties, list_node) {
719 if (!strcmp("compatible", prop->prop.name)) {
720 size_t bytes = prop->prop.size;
721 const char *str = prop->prop.data;
722 while (bytes > 0) {
723 if (!strncmp(compat, str, bytes))
724 return 1;
725 size_t len = strnlen(str, bytes) + 1;
726 if (bytes <= len)
727 break;
728 str += len;
729 bytes -= len;
730 }
731 break;
732 }
733 }
734
735 return 0;
736}
737
738/*
739 * Find a node from a compatible string, in the subtree of a parent node.
740 *
741 * @param parent The parent node under which to look.
742 * @param compat The compatible string to find.
743 * @return The found node, or NULL.
744 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200745struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
746 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200747{
Julius Werner23df4772019-05-17 22:50:18 -0700748 /* Check if the parent node itself is compatible. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200749 if (dt_check_compat_match(parent, compat))
750 return parent;
751
Patrick Rudolph666c1722018-04-03 09:57:33 +0200752 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200753 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200754 struct device_tree_node *found = dt_find_compat(child, compat);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200755 if (found)
756 return found;
757 }
758
759 return NULL;
760}
761
762/*
Martin Roth0949e732021-10-01 14:28:22 -0600763 * Find the next compatible child of a given parent. All children up to the
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200764 * child passed in by caller are ignored. If child is NULL, it considers all the
765 * children to find the first child which is compatible.
766 *
767 * @param parent The parent node under which to look.
768 * @param child The child node to start search from (exclusive). If NULL
769 * consider all children.
770 * @param compat The compatible string to find.
771 * @return The found node, or NULL.
772 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200773struct device_tree_node *
774dt_find_next_compat_child(struct device_tree_node *parent,
775 struct device_tree_node *child,
776 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200777{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200778 struct device_tree_node *next;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200779 int ignore = 0;
780
781 if (child)
782 ignore = 1;
783
784 list_for_each(next, parent->children, list_node) {
785 if (ignore) {
786 if (child == next)
787 ignore = 0;
788 continue;
789 }
790
791 if (dt_check_compat_match(next, compat))
792 return next;
793 }
794
795 return NULL;
796}
797
798/*
799 * Find a node with matching property value, in the subtree of a parent node.
800 *
801 * @param parent The parent node under which to look.
802 * @param name The property name to look for.
803 * @param data The property value to look for.
804 * @param size The property size.
805 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200806struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
807 const char *name, void *data,
808 size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200809{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200810 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200811
812 /* Check if parent itself has the required property value. */
813 list_for_each(prop, parent->properties, list_node) {
814 if (!strcmp(name, prop->prop.name)) {
815 size_t bytes = prop->prop.size;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200816 const void *prop_data = prop->prop.data;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200817 if (size != bytes)
818 break;
819 if (!memcmp(data, prop_data, size))
820 return parent;
821 break;
822 }
823 }
824
Patrick Rudolph666c1722018-04-03 09:57:33 +0200825 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200826 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200827 struct device_tree_node *found = dt_find_prop_value(child, name,
828 data, size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200829 if (found)
830 return found;
831 }
832 return NULL;
833}
834
835/*
836 * Write an arbitrary sized big-endian integer into a pointer.
837 *
838 * @param dest Pointer to the DT property data buffer to write.
Elyes HAOUAS1ec76442018-08-07 12:20:04 +0200839 * @param src The integer to write (in CPU endianness).
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200840 * @param length the length of the destination integer in bytes.
841 */
842void dt_write_int(u8 *dest, u64 src, size_t length)
843{
844 while (length--) {
845 dest[length] = (u8)src;
846 src >>= 8;
847 }
848}
849
850/*
Patrick Rudolph5ccc7312018-05-30 15:05:28 +0200851 * Delete a property by name in a given node if it exists.
852 *
853 * @param node The device tree node to operate on.
854 * @param name The name of the property to delete.
855 */
856void dt_delete_prop(struct device_tree_node *node, const char *name)
857{
858 struct device_tree_property *prop;
859
860 list_for_each(prop, node->properties, list_node) {
861 if (!strcmp(prop->prop.name, name)) {
862 list_remove(&prop->list_node);
863 return;
864 }
865 }
866}
867
868/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200869 * Add an arbitrary property to a node, or update it if it already exists.
870 *
871 * @param node The device tree node to add to.
872 * @param name The name of the new property.
873 * @param data The raw data blob to be stored in the property.
874 * @param size The size of data in bytes.
875 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200876void dt_add_bin_prop(struct device_tree_node *node, const char *name,
Julius Werner0e9116f2019-05-13 17:30:31 -0700877 void *data, size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200878{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200879 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200880
881 list_for_each(prop, node->properties, list_node) {
882 if (!strcmp(prop->prop.name, name)) {
883 prop->prop.data = data;
884 prop->prop.size = size;
885 return;
886 }
887 }
888
Julius Werner9636a102019-05-03 17:36:43 -0700889 prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200890 list_insert_after(&prop->list_node, &node->properties);
891 prop->prop.name = name;
892 prop->prop.data = data;
893 prop->prop.size = size;
894}
895
896/*
897 * Find given string property in a node and return its content.
898 *
899 * @param node The device tree node to search.
900 * @param name The name of the property.
901 * @return The found string, or NULL.
902 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200903const char *dt_find_string_prop(const struct device_tree_node *node,
904 const char *name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200905{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200906 const void *content;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200907 size_t size;
908
909 dt_find_bin_prop(node, name, &content, &size);
910
911 return content;
912}
913
914/*
915 * Find given property in a node.
916 *
917 * @param node The device tree node to search.
918 * @param name The name of the property.
919 * @param data Pointer to return raw data blob in the property.
920 * @param size Pointer to return the size of data in bytes.
921 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200922void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
923 const void **data, size_t *size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200924{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200925 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200926
927 *data = NULL;
928 *size = 0;
929
930 list_for_each(prop, node->properties, list_node) {
931 if (!strcmp(prop->prop.name, name)) {
932 *data = prop->prop.data;
933 *size = prop->prop.size;
934 return;
935 }
936 }
937}
938
939/*
940 * Add a string property to a node, or update it if it already exists.
941 *
942 * @param node The device tree node to add to.
943 * @param name The name of the new property.
944 * @param str The zero-terminated string to be stored in the property.
945 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200946void dt_add_string_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200947 const char *str)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200948{
Julius Werner0e9116f2019-05-13 17:30:31 -0700949 dt_add_bin_prop(node, name, (char *)str, strlen(str) + 1);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200950}
951
952/*
953 * Add a 32-bit integer property to a node, or update it if it already exists.
954 *
955 * @param node The device tree node to add to.
956 * @param name The name of the new property.
957 * @param val The integer to be stored in the property.
958 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200959void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200960{
Julius Werner9636a102019-05-03 17:36:43 -0700961 u32 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200962 *val_ptr = htobe32(val);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200963 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
964}
965
966/*
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200967 * Add a 64-bit integer property to a node, or update it if it already exists.
968 *
969 * @param node The device tree node to add to.
970 * @param name The name of the new property.
971 * @param val The integer to be stored in the property.
972 */
973void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
974{
Julius Werner9636a102019-05-03 17:36:43 -0700975 u64 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200976 *val_ptr = htobe64(val);
977 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
978}
979
980/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200981 * Add a 'reg' address list property to a node, or update it if it exists.
982 *
983 * @param node The device tree node to add to.
984 * @param addrs Array of address values to be stored in the property.
985 * @param sizes Array of corresponding size values to 'addrs'.
986 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
987 * @param addr_cells Value of #address-cells property valid for this node.
988 * @param size_cells Value of #size-cells property valid for this node.
989 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200990void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200991 int count, u32 addr_cells, u32 size_cells)
992{
993 int i;
994 size_t length = (addr_cells + size_cells) * sizeof(u32) * count;
Julius Werner9636a102019-05-03 17:36:43 -0700995 u8 *data = xmalloc(length);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200996 u8 *cur = data;
997
998 for (i = 0; i < count; i++) {
999 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
1000 cur += addr_cells * sizeof(u32);
1001 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
1002 cur += size_cells * sizeof(u32);
1003 }
1004
1005 dt_add_bin_prop(node, "reg", data, length);
1006}
1007
1008/*
1009 * Fixups to apply to a kernel's device tree before booting it.
1010 */
1011
Patrick Rudolph666c1722018-04-03 09:57:33 +02001012struct list_node device_tree_fixups;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001013
Patrick Rudolph666c1722018-04-03 09:57:33 +02001014int dt_apply_fixups(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001015{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001016 struct device_tree_fixup *fixup;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001017 list_for_each(fixup, device_tree_fixups, list_node) {
1018 assert(fixup->fixup);
1019 if (fixup->fixup(fixup, tree))
1020 return 1;
1021 }
1022 return 0;
1023}
1024
Patrick Rudolph666c1722018-04-03 09:57:33 +02001025int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001026 void *data, size_t data_size, int create)
1027{
1028 char *path_copy, *prop_name;
Patrick Rudolph666c1722018-04-03 09:57:33 +02001029 struct device_tree_node *dt_node;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001030
1031 path_copy = strdup(path);
1032
1033 if (!path_copy) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001034 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
1035 path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001036 return 1;
1037 }
1038
1039 prop_name = strrchr(path_copy, '/');
1040 if (!prop_name) {
Patrick Rudolph679d6242018-07-11 13:53:04 +02001041 free(path_copy);
Patrick Rudolph666c1722018-04-03 09:57:33 +02001042 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001043 return 1;
1044 }
1045
1046 *prop_name++ = '\0'; /* Separate path from the property name. */
1047
Julius Wernerf36d53c2019-05-03 18:23:34 -07001048 dt_node = dt_find_node_by_path(tree, path_copy, NULL,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001049 NULL, create);
1050
1051 if (!dt_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001052 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001053 create ? "create" : "find", path_copy);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001054 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001055 return 1;
1056 }
1057
1058 dt_add_bin_prop(dt_node, prop_name, data, data_size);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001059 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001060
1061 return 0;
1062}
1063
1064/*
1065 * Prepare the /reserved-memory/ node.
1066 *
1067 * Technically, this can be called more than one time, to init and/or retrieve
1068 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
1069 *
1070 * @tree: Device tree to add/retrieve from.
1071 * @return: The /reserved-memory/ node (or NULL, if error).
1072 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001073struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001074{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001075 struct device_tree_node *reserved;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001076 u32 addr = 0, size = 0;
1077
Julius Wernerfbec63d2019-05-03 18:29:28 -07001078 reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001079 &size, 1);
1080 if (!reserved)
1081 return NULL;
1082
Julius Werner23df4772019-05-17 22:50:18 -07001083 /* Binding doc says this should have the same #{address,size}-cells as
1084 the root. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001085 dt_add_u32_prop(reserved, "#address-cells", addr);
1086 dt_add_u32_prop(reserved, "#size-cells", size);
Julius Werner23df4772019-05-17 22:50:18 -07001087 /* Binding doc says this should be empty (1:1 mapping from root). */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001088 dt_add_bin_prop(reserved, "ranges", NULL, 0);
1089
1090 return reserved;
1091}
Julius Werner735ddc92019-05-07 17:05:28 -07001092
1093/*
1094 * Increment a single phandle in prop at a given offset by a given adjustment.
1095 *
1096 * @param prop Property whose phandle should be adjusted.
1097 * @param adjustment Value that should be added to the existing phandle.
1098 * @param offset Byte offset of the phandle in the property data.
1099 *
1100 * @return New phandle value, or 0 on error.
1101 */
1102static uint32_t dt_adjust_phandle(struct device_tree_property *prop,
1103 uint32_t adjustment, uint32_t offset)
1104{
1105 if (offset + 4 > prop->prop.size)
1106 return 0;
1107
1108 uint32_t phandle = be32dec(prop->prop.data + offset);
1109 if (phandle == 0 ||
1110 phandle == FDT_PHANDLE_ILLEGAL ||
1111 phandle == 0xffffffff)
1112 return 0;
1113
1114 phandle += adjustment;
1115 if (phandle >= FDT_PHANDLE_ILLEGAL)
1116 return 0;
1117
1118 be32enc(prop->prop.data + offset, phandle);
1119 return phandle;
1120}
1121
1122/*
1123 * Adjust all phandles in subtree by adding a new base offset.
1124 *
1125 * @param node Root node of the subtree to work on.
1126 * @param base New phandle base to be added to all phandles.
1127 *
1128 * @return New highest phandle in the subtree, or 0 on error.
1129 */
1130static uint32_t dt_adjust_all_phandles(struct device_tree_node *node,
1131 uint32_t base)
1132{
Julius Werner23df4772019-05-17 22:50:18 -07001133 uint32_t new_max = MAX(base, 1); /* make sure we don't return 0 */
Julius Werner735ddc92019-05-07 17:05:28 -07001134 struct device_tree_property *prop;
1135 struct device_tree_node *child;
1136
1137 if (!node)
1138 return new_max;
1139
1140 list_for_each(prop, node->properties, list_node)
1141 if (dt_prop_is_phandle(prop)) {
1142 node->phandle = dt_adjust_phandle(prop, base, 0);
1143 if (!node->phandle)
1144 return 0;
1145 new_max = MAX(new_max, node->phandle);
Julius Werner23df4772019-05-17 22:50:18 -07001146 } /* no break -- can have more than one phandle prop */
Julius Werner735ddc92019-05-07 17:05:28 -07001147
1148 list_for_each(child, node->children, list_node)
1149 new_max = MAX(new_max, dt_adjust_all_phandles(child, base));
1150
1151 return new_max;
1152}
1153
1154/*
1155 * Apply a /__local_fixup__ subtree to the corresponding overlay subtree.
1156 *
1157 * @param node Root node of the overlay subtree to fix up.
1158 * @param node Root node of the /__local_fixup__ subtree.
1159 * @param base Adjustment that was added to phandles in the overlay.
1160 *
1161 * @return 0 on success, -1 on error.
1162 */
1163static int dt_fixup_locals(struct device_tree_node *node,
1164 struct device_tree_node *fixup, uint32_t base)
1165{
1166 struct device_tree_property *prop;
1167 struct device_tree_property *fixup_prop;
1168 struct device_tree_node *child;
1169 struct device_tree_node *fixup_child;
1170 int i;
1171
Julius Werner23df4772019-05-17 22:50:18 -07001172 /*
1173 * For local fixups the /__local_fixup__ subtree contains the same node
1174 * hierarchy as the main tree we're fixing up. Each property contains
1175 * the fixup offsets for the respective property in the main tree. For
1176 * each property in the fixup node, find the corresponding property in
1177 * the base node and apply fixups to all offsets it specifies.
1178 */
Julius Werner735ddc92019-05-07 17:05:28 -07001179 list_for_each(fixup_prop, fixup->properties, list_node) {
1180 struct device_tree_property *base_prop = NULL;
1181 list_for_each(prop, node->properties, list_node)
1182 if (!strcmp(prop->prop.name, fixup_prop->prop.name)) {
1183 base_prop = prop;
1184 break;
1185 }
1186
Julius Werner23df4772019-05-17 22:50:18 -07001187 /* We should always find a corresponding base prop for a fixup,
1188 and fixup props contain a list of 32-bit fixup offsets. */
Julius Werner735ddc92019-05-07 17:05:28 -07001189 if (!base_prop || fixup_prop->prop.size % sizeof(uint32_t))
1190 return -1;
1191
1192 for (i = 0; i < fixup_prop->prop.size; i += sizeof(uint32_t))
1193 if (!dt_adjust_phandle(base_prop, base, be32dec(
1194 fixup_prop->prop.data + i)))
1195 return -1;
1196 }
1197
Julius Werner23df4772019-05-17 22:50:18 -07001198 /* Now recursively descend both the base tree and the /__local_fixups__
1199 subtree in sync to apply all fixups. */
Julius Werner735ddc92019-05-07 17:05:28 -07001200 list_for_each(fixup_child, fixup->children, list_node) {
1201 struct device_tree_node *base_child = NULL;
1202 list_for_each(child, node->children, list_node)
1203 if (!strcmp(child->name, fixup_child->name)) {
1204 base_child = child;
1205 break;
1206 }
1207
Julius Werner23df4772019-05-17 22:50:18 -07001208 /* All fixup nodes should have a corresponding base node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001209 if (!base_child)
1210 return -1;
1211
1212 if (dt_fixup_locals(base_child, fixup_child, base) < 0)
1213 return -1;
1214 }
1215
1216 return 0;
1217}
1218
1219/*
1220 * Update all /__symbols__ properties in an overlay that start with
1221 * "/fragment@X/__overlay__" with corresponding path prefix in the base tree.
1222 *
1223 * @param symbols /__symbols__ done to update.
1224 * @param fragment /fragment@X node that references to should be updated.
1225 * @param base_path Path of base tree node that the fragment overlaid.
1226 */
1227static void dt_fix_symbols(struct device_tree_node *symbols,
1228 struct device_tree_node *fragment,
1229 const char *base_path)
1230{
1231 struct device_tree_property *prop;
Julius Werner23df4772019-05-17 22:50:18 -07001232 char buf[512]; /* Should be enough for maximum DT path length? */
1233 char node_path[64]; /* easily enough for /fragment@XXXX/__overlay__ */
Julius Werner735ddc92019-05-07 17:05:28 -07001234
Julius Werner23df4772019-05-17 22:50:18 -07001235 if (!symbols) /* If the overlay has no /__symbols__ node, we're done! */
Julius Werner735ddc92019-05-07 17:05:28 -07001236 return;
1237
1238 int len = snprintf(node_path, sizeof(node_path), "/%s/__overlay__",
1239 fragment->name);
1240
1241 list_for_each(prop, symbols->properties, list_node)
1242 if (!strncmp(prop->prop.data, node_path, len)) {
1243 prop->prop.size = snprintf(buf, sizeof(buf), "%s%s",
1244 base_path, (char *)prop->prop.data + len) + 1;
1245 free(prop->prop.data);
1246 prop->prop.data = strdup(buf);
1247 }
1248}
1249
1250/*
1251 * Fix up overlay according to a property in /__fixup__. If the fixed property
1252 * is a /fragment@X:target, also update /__symbols__ references to fragment.
1253 *
1254 * @params overlay Overlay to fix up.
1255 * @params fixup /__fixup__ property.
1256 * @params phandle phandle value to insert where the fixup points to.
1257 * @params base_path Path to the base DT node that the fixup points to.
1258 * @params overlay_symbols /__symbols__ node of the overlay.
1259 *
1260 * @return 0 on success, -1 on error.
1261 */
1262static int dt_fixup_external(struct device_tree *overlay,
1263 struct device_tree_property *fixup,
1264 uint32_t phandle, const char *base_path,
1265 struct device_tree_node *overlay_symbols)
1266{
1267 struct device_tree_property *prop;
1268
Julius Werner23df4772019-05-17 22:50:18 -07001269 /* External fixup properties are encoded as "<path>:<prop>:<offset>". */
Julius Werner735ddc92019-05-07 17:05:28 -07001270 char *entry = fixup->prop.data;
1271 while ((void *)entry < fixup->prop.data + fixup->prop.size) {
Julius Werner23df4772019-05-17 22:50:18 -07001272 /* okay to destroy fixup property value, won't need it again */
Julius Werner735ddc92019-05-07 17:05:28 -07001273 char *node_path = entry;
1274 entry = strchr(node_path, ':');
1275 if (!entry)
1276 return -1;
1277 *entry++ = '\0';
1278
1279 char *prop_name = entry;
1280 entry = strchr(prop_name, ':');
1281 if (!entry)
1282 return -1;
1283 *entry++ = '\0';
1284
1285 struct device_tree_node *ovl_node = dt_find_node_by_path(
1286 overlay, node_path, NULL, NULL, 0);
1287 if (!ovl_node || !isdigit(*entry))
1288 return -1;
1289
1290 struct device_tree_property *ovl_prop = NULL;
1291 list_for_each(prop, ovl_node->properties, list_node)
1292 if (!strcmp(prop->prop.name, prop_name)) {
1293 ovl_prop = prop;
1294 break;
1295 }
1296
Julius Werner23df4772019-05-17 22:50:18 -07001297 /* Move entry to first char after number, must be a '\0'. */
Julius Werner735ddc92019-05-07 17:05:28 -07001298 uint32_t offset = skip_atoi(&entry);
1299 if (!ovl_prop || offset + 4 > ovl_prop->prop.size || entry[0])
1300 return -1;
Julius Werner23df4772019-05-17 22:50:18 -07001301 entry++; /* jump over '\0' to potential next fixup */
Julius Werner735ddc92019-05-07 17:05:28 -07001302
1303 be32enc(ovl_prop->prop.data + offset, phandle);
1304
Julius Werner23df4772019-05-17 22:50:18 -07001305 /* If this is a /fragment@X:target property, update references
1306 to this fragment in the overlay __symbols__ now. */
Julius Werner735ddc92019-05-07 17:05:28 -07001307 if (offset == 0 && !strcmp(prop_name, "target") &&
Julius Werner23df4772019-05-17 22:50:18 -07001308 !strchr(node_path + 1, '/')) /* only toplevel nodes */
Julius Werner735ddc92019-05-07 17:05:28 -07001309 dt_fix_symbols(overlay_symbols, ovl_node, base_path);
1310 }
1311
1312 return 0;
1313}
1314
1315/*
1316 * Apply all /__fixup__ properties in the overlay. This will destroy the
1317 * property data in /__fixup__ and it should not be accessed again.
1318 *
1319 * @params tree Base device tree that the overlay updates.
1320 * @params symbols /__symbols__ node of the base device tree.
1321 * @params overlay Overlay to fix up.
1322 * @params fixups /__fixup__ node in the overlay.
1323 * @params overlay_symbols /__symbols__ node of the overlay.
1324 *
1325 * @return 0 on success, -1 on error.
1326 */
1327static int dt_fixup_all_externals(struct device_tree *tree,
1328 struct device_tree_node *symbols,
1329 struct device_tree *overlay,
1330 struct device_tree_node *fixups,
1331 struct device_tree_node *overlay_symbols)
1332{
1333 struct device_tree_property *fix;
1334
Julius Werner23df4772019-05-17 22:50:18 -07001335 /* If we have any external fixups, base tree must have /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001336 if (!symbols)
1337 return -1;
1338
Julius Werner23df4772019-05-17 22:50:18 -07001339 /*
1340 * Unlike /__local_fixups__, /__fixups__ is not a whole subtree that
1341 * mirrors the node hierarchy. It's just a directory of fixup properties
1342 * that each directly contain all information necessary to apply them.
1343 */
Julius Werner735ddc92019-05-07 17:05:28 -07001344 list_for_each(fix, fixups->properties, list_node) {
Julius Werner23df4772019-05-17 22:50:18 -07001345 /* The name of a fixup property is the label of the node we want
1346 a property to phandle-reference. Look up in /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001347 const char *path = dt_find_string_prop(symbols, fix->prop.name);
1348 if (!path)
1349 return -1;
1350
Elyes HAOUAS0afaff22021-01-16 15:02:31 +01001351 /* Find node the label pointed to figure out its phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001352 struct device_tree_node *node = dt_find_node_by_path(tree, path,
1353 NULL, NULL, 0);
1354 if (!node)
1355 return -1;
1356
Julius Werner23df4772019-05-17 22:50:18 -07001357 /* Write into the overlay property(s) pointing to that node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001358 if (dt_fixup_external(overlay, fix, node->phandle,
1359 path, overlay_symbols) < 0)
1360 return -1;
1361 }
1362
1363 return 0;
1364}
1365
1366/*
1367 * Copy all nodes and properties from one DT subtree into another. This is a
1368 * shallow copy so both trees will point to the same property data afterwards.
1369 *
1370 * @params dst Destination subtree to copy into.
1371 * @params src Source subtree to copy from.
1372 * @params upd 1 to overwrite same-name properties, 0 to discard them.
1373 */
1374static void dt_copy_subtree(struct device_tree_node *dst,
1375 struct device_tree_node *src, int upd)
1376{
1377 struct device_tree_property *prop;
1378 struct device_tree_property *src_prop;
1379 list_for_each(src_prop, src->properties, list_node) {
1380 if (dt_prop_is_phandle(src_prop) ||
1381 !strcmp(src_prop->prop.name, "name")) {
1382 printk(BIOS_DEBUG,
1383 "WARNING: ignoring illegal overlay prop '%s'\n",
1384 src_prop->prop.name);
1385 continue;
1386 }
1387
1388 struct device_tree_property *dst_prop = NULL;
1389 list_for_each(prop, dst->properties, list_node)
1390 if (!strcmp(prop->prop.name, src_prop->prop.name)) {
1391 dst_prop = prop;
1392 break;
1393 }
1394
1395 if (dst_prop) {
1396 if (!upd) {
1397 printk(BIOS_DEBUG,
1398 "WARNING: ignoring prop update '%s'\n",
1399 src_prop->prop.name);
1400 continue;
1401 }
1402 } else {
1403 dst_prop = xzalloc(sizeof(*dst_prop));
1404 list_insert_after(&dst_prop->list_node,
1405 &dst->properties);
1406 }
1407
1408 dst_prop->prop = src_prop->prop;
1409 }
1410
1411 struct device_tree_node *node;
1412 struct device_tree_node *src_node;
1413 list_for_each(src_node, src->children, list_node) {
1414 struct device_tree_node *dst_node = NULL;
1415 list_for_each(node, dst->children, list_node)
1416 if (!strcmp(node->name, src_node->name)) {
1417 dst_node = node;
1418 break;
1419 }
1420
1421 if (!dst_node) {
1422 dst_node = xzalloc(sizeof(*dst_node));
1423 *dst_node = *src_node;
1424 list_insert_after(&dst_node->list_node, &dst->children);
1425 } else {
1426 dt_copy_subtree(dst_node, src_node, upd);
1427 }
1428 }
1429}
1430
1431/*
1432 * Apply an overlay /fragment@X node to a base device tree.
1433 *
1434 * @param tree Base device tree.
1435 * @param fragment /fragment@X node.
1436 * @params overlay_symbols /__symbols__ node of the overlay.
1437 *
1438 * @return 0 on success, -1 on error.
1439 */
1440static int dt_import_fragment(struct device_tree *tree,
1441 struct device_tree_node *fragment,
1442 struct device_tree_node *overlay_symbols)
1443{
Julius Werner23df4772019-05-17 22:50:18 -07001444 /* The actual overlaid nodes/props are in an __overlay__ child node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001445 static const char *overlay_path[] = { "__overlay__", NULL };
1446 struct device_tree_node *overlay = dt_find_node(fragment, overlay_path,
1447 NULL, NULL, 0);
1448
Julius Werner23df4772019-05-17 22:50:18 -07001449 /* If it doesn't have an __overlay__ child, it's not a fragment. */
Julius Werner735ddc92019-05-07 17:05:28 -07001450 if (!overlay)
1451 return 0;
1452
Julius Werner23df4772019-05-17 22:50:18 -07001453 /* Target node of the fragment can be given by path or by phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001454 struct device_tree_property *prop;
1455 struct device_tree_property *phandle = NULL;
1456 struct device_tree_property *path = NULL;
1457 list_for_each(prop, fragment->properties, list_node) {
1458 if (!strcmp(prop->prop.name, "target")) {
1459 phandle = prop;
Julius Werner23df4772019-05-17 22:50:18 -07001460 break; /* phandle target has priority, stop looking */
Julius Werner735ddc92019-05-07 17:05:28 -07001461 }
1462 if (!strcmp(prop->prop.name, "target-path"))
1463 path = prop;
1464 }
1465
1466 struct device_tree_node *target = NULL;
1467 if (phandle) {
1468 if (phandle->prop.size != sizeof(uint32_t))
1469 return -1;
1470 target = dt_find_node_by_phandle(tree->root,
1471 be32dec(phandle->prop.data));
Julius Werner23df4772019-05-17 22:50:18 -07001472 /* Symbols already updated as part of dt_fixup_external(). */
Julius Werner735ddc92019-05-07 17:05:28 -07001473 } else if (path) {
1474 target = dt_find_node_by_path(tree, path->prop.data,
1475 NULL, NULL, 0);
1476 dt_fix_symbols(overlay_symbols, fragment, path->prop.data);
1477 }
1478 if (!target)
1479 return -1;
1480
1481 dt_copy_subtree(target, overlay, 1);
1482 return 0;
1483}
1484
1485/*
1486 * Apply a device tree overlay to a base device tree. This will
1487 * destroy/incorporate the overlay data, so it should not be freed or reused.
1488 * See dtc.git/Documentation/dt-object-internal.txt for overlay format details.
1489 *
1490 * @param tree Unflattened base device tree to add the overlay into.
1491 * @param overlay Unflattened overlay device tree to apply to the base.
1492 *
1493 * @return 0 on success, -1 on error.
1494 */
1495int dt_apply_overlay(struct device_tree *tree, struct device_tree *overlay)
1496{
Julius Werner23df4772019-05-17 22:50:18 -07001497 /*
1498 * First, we need to make sure phandles inside the overlay don't clash
1499 * with those in the base tree. We just define the highest phandle value
1500 * in the base tree as the "phandle offset" for this overlay and
1501 * increment all phandles in it by that value.
1502 */
Julius Werner735ddc92019-05-07 17:05:28 -07001503 uint32_t phandle_base = tree->max_phandle;
1504 uint32_t new_max = dt_adjust_all_phandles(overlay->root, phandle_base);
1505 if (!new_max) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001506 printk(BIOS_ERR, "invalid phandles in overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001507 return -1;
1508 }
1509 tree->max_phandle = new_max;
1510
Julius Werner23df4772019-05-17 22:50:18 -07001511 /* Now that we changed phandles in the overlay, we need to update any
1512 nodes referring to them. Those are listed in /__local_fixups__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001513 struct device_tree_node *local_fixups = dt_find_node_by_path(overlay,
1514 "/__local_fixups__", NULL, NULL, 0);
1515 if (local_fixups && dt_fixup_locals(overlay->root, local_fixups,
1516 phandle_base) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001517 printk(BIOS_ERR, "invalid local fixups in overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001518 return -1;
1519 }
1520
Julius Werner23df4772019-05-17 22:50:18 -07001521 /*
1522 * Besides local phandle references (from nodes within the overlay to
1523 * other nodes within the overlay), the overlay may also contain phandle
1524 * references to the base tree. These are stored with invalid values and
1525 * must be updated now. /__symbols__ contains a list of all labels in
1526 * the base tree, and /__fixups__ describes all nodes in the overlay
1527 * that contain external phandle references.
1528 * We also take this opportunity to update all /fragment@X/__overlay__/
1529 * prefixes in the overlay's /__symbols__ node to the correct path that
1530 * the fragment will be placed in later, since this is the only step
1531 * where we have all necessary information for that easily available.
1532 */
Julius Werner735ddc92019-05-07 17:05:28 -07001533 struct device_tree_node *symbols = dt_find_node_by_path(tree,
1534 "/__symbols__", NULL, NULL, 0);
1535 struct device_tree_node *fixups = dt_find_node_by_path(overlay,
1536 "/__fixups__", NULL, NULL, 0);
1537 struct device_tree_node *overlay_symbols = dt_find_node_by_path(overlay,
1538 "/__symbols__", NULL, NULL, 0);
1539 if (fixups && dt_fixup_all_externals(tree, symbols, overlay,
1540 fixups, overlay_symbols) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001541 printk(BIOS_ERR, "cannot match external fixups from overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001542 return -1;
1543 }
1544
Julius Werner23df4772019-05-17 22:50:18 -07001545 /* After all this fixing up, we can finally merge overlay into the tree
1546 (one fragment at a time, because for some reason it's split up). */
Julius Werner735ddc92019-05-07 17:05:28 -07001547 struct device_tree_node *fragment;
1548 list_for_each(fragment, overlay->root->children, list_node)
1549 if (dt_import_fragment(tree, fragment, overlay_symbols) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001550 printk(BIOS_ERR, "bad DT fragment '%s'\n",
Julius Werner735ddc92019-05-07 17:05:28 -07001551 fragment->name);
1552 return -1;
1553 }
1554
Julius Werner23df4772019-05-17 22:50:18 -07001555 /*
1556 * We need to also update /__symbols__ to include labels from this
1557 * overlay, in case we want to load further overlays with external
1558 * phandle references to it. If the base tree already has a /__symbols__
1559 * we merge them together, otherwise we just insert the overlay's
1560 * /__symbols__ node into the base tree root.
1561 */
Julius Werner735ddc92019-05-07 17:05:28 -07001562 if (overlay_symbols) {
1563 if (symbols)
1564 dt_copy_subtree(symbols, overlay_symbols, 0);
1565 else
1566 list_insert_after(&overlay_symbols->list_node,
1567 &tree->root->children);
1568 }
1569
1570 return 0;
1571}