blob: b8faab53b8d3fde3e8c1228ac68d00080a1d7801 [file] [log] [blame]
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001/*
2 * Copyright 2013 Google Inc.
Patrick Rudolph666c1722018-04-03 09:57:33 +02003 * Copyright 2018-present Facebook, Inc.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02004 *
Patrick Rudolph666c1722018-04-03 09:57:33 +02005 * Taken from depthcharge: src/base/device_tree.c
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but without any warranty; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <assert.h>
Julius Werner9636a102019-05-03 17:36:43 -070019#include <commonlib/stdlib.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +020020#include <console/console.h>
Joel Kitching393c71c2019-06-16 16:09:42 +080021#include <ctype.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +020022#include <device_tree.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020023#include <endian.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020024#include <stdint.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +020025#include <string.h>
26#include <stddef.h>
27#include <stdlib.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020028
29/*
30 * Functions for picking apart flattened trees.
31 */
32
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020033int fdt_next_property(const void *blob, uint32_t offset,
34 struct fdt_property *prop)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020035{
Patrick Rudolph666c1722018-04-03 09:57:33 +020036 struct fdt_header *header = (struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020037 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
38
39 int index = 0;
Patrick Rudolph666c1722018-04-03 09:57:33 +020040 if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020041 return 0;
42
Patrick Rudolph666c1722018-04-03 09:57:33 +020043 uint32_t size = be32toh(ptr[index++]);
44 uint32_t name_offset = be32toh(ptr[index++]);
45 name_offset += be32toh(header->strings_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020046
47 if (prop) {
48 prop->name = (char *)((uint8_t *)blob + name_offset);
49 prop->data = &ptr[index];
50 prop->size = size;
51 }
52
Patrick Rudolph666c1722018-04-03 09:57:33 +020053 index += DIV_ROUND_UP(size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020054
Patrick Rudolph666c1722018-04-03 09:57:33 +020055 return index * sizeof(uint32_t);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020056}
57
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020058int fdt_node_name(const void *blob, uint32_t offset, const char **name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020059{
60 uint8_t *ptr = ((uint8_t *)blob) + offset;
Julius Wernera5ea3a22019-05-07 17:38:12 -070061 if (be32dec(ptr) != FDT_TOKEN_BEGIN_NODE)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020062 return 0;
63
64 ptr += 4;
65 if (name)
66 *name = (char *)ptr;
Patrick Rudolph666c1722018-04-03 09:57:33 +020067 return ALIGN_UP(strlen((char *)ptr) + 1, sizeof(uint32_t)) + 4;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020068}
69
Julius Werner6702b682019-05-03 18:13:53 -070070static int dt_prop_is_phandle(struct device_tree_property *prop)
71{
72 return !(strcmp("phandle", prop->prop.name) &&
73 strcmp("linux,phandle", prop->prop.name));
74}
75
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020076
77
78/*
79 * Functions for printing flattened trees.
80 */
81
82static void print_indent(int depth)
83{
Julius Werner0d746532019-05-06 19:35:56 -070084 printk(BIOS_DEBUG, "%*s", depth * 8, "");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020085}
86
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020087static void print_property(const struct fdt_property *prop, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020088{
Julius Werner0d746532019-05-06 19:35:56 -070089 int is_string = prop->size > 0 &&
90 ((char *)prop->data)[prop->size - 1] == '\0';
91
92 if (is_string)
93 for (const char *c = prop->data; *c != '\0'; c++)
94 if (!isprint(*c))
95 is_string = 0;
96
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020097 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -070098 if (is_string) {
99 printk(BIOS_DEBUG, "%s = \"%s\";\n",
100 prop->name, (const char *)prop->data);
101 } else {
102 printk(BIOS_DEBUG, "%s = < ", prop->name);
103 for (int i = 0; i < MIN(128, prop->size); i += 4) {
104 uint32_t val = 0;
105 for (int j = 0; j < MIN(4, prop->size - i); j++)
106 val |= ((uint8_t *)prop->data)[i + j] <<
107 (24 - j * 8);
108 printk(BIOS_DEBUG, "%#.2x ", val);
109 }
110 if (prop->size > 128)
111 printk(BIOS_DEBUG, "...");
112 printk(BIOS_DEBUG, ">;\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200113 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200114}
115
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200116static int print_flat_node(const void *blob, uint32_t start_offset, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200117{
118 int offset = start_offset;
119 const char *name;
120 int size;
121
122 size = fdt_node_name(blob, offset, &name);
123 if (!size)
124 return 0;
125 offset += size;
126
127 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -0700128 printk(BIOS_DEBUG, "%s {\n", name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200129
Patrick Rudolph666c1722018-04-03 09:57:33 +0200130 struct fdt_property prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200131 while ((size = fdt_next_property(blob, offset, &prop))) {
132 print_property(&prop, depth + 1);
133
134 offset += size;
135 }
136
Julius Werner23df4772019-05-17 22:50:18 -0700137 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700138
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200139 while ((size = print_flat_node(blob, offset, depth + 1)))
140 offset += size;
141
Julius Werner0d746532019-05-06 19:35:56 -0700142 print_indent(depth);
143 printk(BIOS_DEBUG, "}\n");
144
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200145 return offset - start_offset + sizeof(uint32_t);
146}
147
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200148void fdt_print_node(const void *blob, uint32_t offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200149{
150 print_flat_node(blob, offset, 0);
151}
152
153
154
155/*
156 * A utility function to skip past nodes in flattened trees.
157 */
158
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200159int fdt_skip_node(const void *blob, uint32_t start_offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200160{
161 int offset = start_offset;
162 int size;
163
164 const char *name;
165 size = fdt_node_name(blob, offset, &name);
166 if (!size)
167 return 0;
168 offset += size;
169
170 while ((size = fdt_next_property(blob, offset, NULL)))
171 offset += size;
172
173 while ((size = fdt_skip_node(blob, offset)))
174 offset += size;
175
176 return offset - start_offset + sizeof(uint32_t);
177}
178
179
180
181/*
182 * Functions to turn a flattened tree into an unflattened one.
183 */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200184
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200185static int fdt_unflatten_node(const void *blob, uint32_t start_offset,
Julius Werner6702b682019-05-03 18:13:53 -0700186 struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200187 struct device_tree_node **new_node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200188{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200189 struct list_node *last;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200190 int offset = start_offset;
191 const char *name;
192 int size;
193
194 size = fdt_node_name(blob, offset, &name);
195 if (!size)
196 return 0;
197 offset += size;
198
Julius Werner9636a102019-05-03 17:36:43 -0700199 struct device_tree_node *node = xzalloc(sizeof(*node));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200200 *new_node = node;
201 node->name = name;
202
Patrick Rudolph666c1722018-04-03 09:57:33 +0200203 struct fdt_property fprop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200204 last = &node->properties;
205 while ((size = fdt_next_property(blob, offset, &fprop))) {
Julius Werner9636a102019-05-03 17:36:43 -0700206 struct device_tree_property *prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200207 prop->prop = fprop;
208
Julius Werner6702b682019-05-03 18:13:53 -0700209 if (dt_prop_is_phandle(prop)) {
210 node->phandle = be32dec(prop->prop.data);
211 if (node->phandle > tree->max_phandle)
212 tree->max_phandle = node->phandle;
213 }
214
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200215 list_insert_after(&prop->list_node, last);
216 last = &prop->list_node;
217
218 offset += size;
219 }
220
Patrick Rudolph666c1722018-04-03 09:57:33 +0200221 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200222 last = &node->children;
Julius Werner6702b682019-05-03 18:13:53 -0700223 while ((size = fdt_unflatten_node(blob, offset, tree, &child))) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200224 list_insert_after(&child->list_node, last);
225 last = &child->list_node;
226
227 offset += size;
228 }
229
230 return offset - start_offset + sizeof(uint32_t);
231}
232
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200233static int fdt_unflatten_map_entry(const void *blob, uint32_t offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200234 struct device_tree_reserve_map_entry **new)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200235{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200236 const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset);
237 const uint64_t start = be64toh(ptr[0]);
238 const uint64_t size = be64toh(ptr[1]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200239
240 if (!size)
241 return 0;
242
Julius Werner9636a102019-05-03 17:36:43 -0700243 struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200244 *new = entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200245 entry->start = start;
246 entry->size = size;
247
248 return sizeof(uint64_t) * 2;
249}
250
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200251struct device_tree *fdt_unflatten(const void *blob)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200252{
Julius Werner9636a102019-05-03 17:36:43 -0700253 struct device_tree *tree = xzalloc(sizeof(*tree));
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200254 const struct fdt_header *header = (const struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200255 tree->header = header;
256
Julius Werner73eaec82019-05-03 17:58:07 -0700257 uint32_t magic = be32toh(header->magic);
258 uint32_t version = be32toh(header->version);
259 uint32_t last_comp_version = be32toh(header->last_comp_version);
260
261 if (magic != FDT_HEADER_MAGIC) {
262 printk(BIOS_DEBUG, "Invalid device tree magic %#.8x!\n", magic);
Jacob Garber698d83a2019-06-07 10:28:54 -0600263 free(tree);
Julius Werner73eaec82019-05-03 17:58:07 -0700264 return NULL;
265 }
266 if (last_comp_version > FDT_SUPPORTED_VERSION) {
267 printk(BIOS_DEBUG, "Unsupported device tree version %u(>=%u)\n",
268 version, last_comp_version);
Jacob Garber698d83a2019-06-07 10:28:54 -0600269 free(tree);
Julius Werner73eaec82019-05-03 17:58:07 -0700270 return NULL;
271 }
272 if (version > FDT_SUPPORTED_VERSION)
273 printk(BIOS_DEBUG,
274 "NOTE: FDT version %u too new, should add support!\n",
275 version);
276
Patrick Rudolph666c1722018-04-03 09:57:33 +0200277 uint32_t struct_offset = be32toh(header->structure_offset);
278 uint32_t strings_offset = be32toh(header->strings_offset);
279 uint32_t reserve_offset = be32toh(header->reserve_map_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200280 uint32_t min_offset = 0;
281 min_offset = MIN(struct_offset, strings_offset);
282 min_offset = MIN(min_offset, reserve_offset);
Julius Werner23df4772019-05-17 22:50:18 -0700283 /* Assume everything up to the first non-header component is part of
284 the header and needs to be preserved. This will protect us against
285 new elements being added in the future. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200286 tree->header_size = min_offset;
287
Patrick Rudolph666c1722018-04-03 09:57:33 +0200288 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200289 uint32_t offset = reserve_offset;
290 int size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200291 struct list_node *last = &tree->reserve_map;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200292 while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) {
293 list_insert_after(&entry->list_node, last);
294 last = &entry->list_node;
295
296 offset += size;
297 }
298
Julius Werner6702b682019-05-03 18:13:53 -0700299 fdt_unflatten_node(blob, struct_offset, tree, &tree->root);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200300
301 return tree;
302}
303
304
305
306/*
Patrick Rudolph666c1722018-04-03 09:57:33 +0200307 * Functions to find the size of the device tree if it was flattened.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200308 */
309
Patrick Rudolph666c1722018-04-03 09:57:33 +0200310static void dt_flat_prop_size(struct device_tree_property *prop,
311 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200312{
Julius Werner23df4772019-05-17 22:50:18 -0700313 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200314 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700315 /* Size. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200316 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700317 /* Name offset. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200318 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700319 /* Property value. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200320 *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200321
Julius Werner23df4772019-05-17 22:50:18 -0700322 /* Property name. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200323 *strings_size += strlen(prop->prop.name) + 1;
324}
325
Patrick Rudolph666c1722018-04-03 09:57:33 +0200326static void dt_flat_node_size(struct device_tree_node *node,
327 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200328{
Julius Werner23df4772019-05-17 22:50:18 -0700329 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200330 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700331 /* Node name. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200332 *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200333
Patrick Rudolph666c1722018-04-03 09:57:33 +0200334 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200335 list_for_each(prop, node->properties, list_node)
336 dt_flat_prop_size(prop, struct_size, strings_size);
337
Patrick Rudolph666c1722018-04-03 09:57:33 +0200338 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200339 list_for_each(child, node->children, list_node)
340 dt_flat_node_size(child, struct_size, strings_size);
341
Julius Werner23df4772019-05-17 22:50:18 -0700342 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200343 *struct_size += sizeof(uint32_t);
344}
345
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200346uint32_t dt_flat_size(const struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200347{
348 uint32_t size = tree->header_size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200349 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200350 list_for_each(entry, tree->reserve_map, list_node)
351 size += sizeof(uint64_t) * 2;
352 size += sizeof(uint64_t) * 2;
353
354 uint32_t struct_size = 0;
355 uint32_t strings_size = 0;
356 dt_flat_node_size(tree->root, &struct_size, &strings_size);
357
358 size += struct_size;
Julius Werner23df4772019-05-17 22:50:18 -0700359 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200360 size += sizeof(uint32_t);
361
362 size += strings_size;
363
364 return size;
365}
366
367
368
369/*
370 * Functions to flatten a device tree.
371 */
372
Patrick Rudolph666c1722018-04-03 09:57:33 +0200373static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200374 void **map_start)
375{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200376 ((uint64_t *)*map_start)[0] = htobe64(entry->start);
377 ((uint64_t *)*map_start)[1] = htobe64(entry->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200378 *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2;
379}
380
Patrick Rudolph666c1722018-04-03 09:57:33 +0200381static void dt_flatten_prop(struct device_tree_property *prop,
382 void **struct_start, void *strings_base,
383 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200384{
385 uint8_t *dstruct = (uint8_t *)*struct_start;
386 uint8_t *dstrings = (uint8_t *)*strings_start;
387
Julius Wernera5ea3a22019-05-07 17:38:12 -0700388 be32enc(dstruct, FDT_TOKEN_PROPERTY);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200389 dstruct += sizeof(uint32_t);
390
Julius Wernera5ea3a22019-05-07 17:38:12 -0700391 be32enc(dstruct, prop->prop.size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200392 dstruct += sizeof(uint32_t);
393
394 uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base;
Julius Wernera5ea3a22019-05-07 17:38:12 -0700395 be32enc(dstruct, name_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200396 dstruct += sizeof(uint32_t);
397
398 strcpy((char *)dstrings, prop->prop.name);
399 dstrings += strlen(prop->prop.name) + 1;
400
401 memcpy(dstruct, prop->prop.data, prop->prop.size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200402 dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200403
404 *struct_start = dstruct;
405 *strings_start = dstrings;
406}
407
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200408static void dt_flatten_node(const struct device_tree_node *node,
409 void **struct_start, void *strings_base,
410 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200411{
412 uint8_t *dstruct = (uint8_t *)*struct_start;
413 uint8_t *dstrings = (uint8_t *)*strings_start;
414
Julius Wernera5ea3a22019-05-07 17:38:12 -0700415 be32enc(dstruct, FDT_TOKEN_BEGIN_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200416 dstruct += sizeof(uint32_t);
417
418 strcpy((char *)dstruct, node->name);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200419 dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200420
Patrick Rudolph666c1722018-04-03 09:57:33 +0200421 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200422 list_for_each(prop, node->properties, list_node)
423 dt_flatten_prop(prop, (void **)&dstruct, strings_base,
424 (void **)&dstrings);
425
Patrick Rudolph666c1722018-04-03 09:57:33 +0200426 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200427 list_for_each(child, node->children, list_node)
428 dt_flatten_node(child, (void **)&dstruct, strings_base,
429 (void **)&dstrings);
430
Julius Wernera5ea3a22019-05-07 17:38:12 -0700431 be32enc(dstruct, FDT_TOKEN_END_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200432 dstruct += sizeof(uint32_t);
433
434 *struct_start = dstruct;
435 *strings_start = dstrings;
436}
437
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200438void dt_flatten(const struct device_tree *tree, void *start_dest)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200439{
440 uint8_t *dest = (uint8_t *)start_dest;
441
442 memcpy(dest, tree->header, tree->header_size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200443 struct fdt_header *header = (struct fdt_header *)dest;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200444 dest += tree->header_size;
445
Patrick Rudolph666c1722018-04-03 09:57:33 +0200446 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200447 list_for_each(entry, tree->reserve_map, list_node)
448 dt_flatten_map_entry(entry, (void **)&dest);
449 ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0;
450 dest += sizeof(uint64_t) * 2;
451
452 uint32_t struct_size = 0;
453 uint32_t strings_size = 0;
454 dt_flat_node_size(tree->root, &struct_size, &strings_size);
455
456 uint8_t *struct_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200457 header->structure_offset = htobe32(dest - (uint8_t *)start_dest);
458 header->structure_size = htobe32(struct_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200459 dest += struct_size;
460
Patrick Rudolph666c1722018-04-03 09:57:33 +0200461 *((uint32_t *)dest) = htobe32(FDT_TOKEN_END);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200462 dest += sizeof(uint32_t);
463
464 uint8_t *strings_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200465 header->strings_offset = htobe32(dest - (uint8_t *)start_dest);
466 header->strings_size = htobe32(strings_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200467 dest += strings_size;
468
469 dt_flatten_node(tree->root, (void **)&struct_start, strings_start,
470 (void **)&strings_start);
471
Patrick Rudolph666c1722018-04-03 09:57:33 +0200472 header->totalsize = htobe32(dest - (uint8_t *)start_dest);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200473}
474
475
476
477/*
478 * Functions for printing a non-flattened device tree.
479 */
480
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200481static void print_node(const struct device_tree_node *node, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200482{
483 print_indent(depth);
Julius Werner23df4772019-05-17 22:50:18 -0700484 if (depth == 0) /* root node has no name, print a starting slash */
Julius Werner0d746532019-05-06 19:35:56 -0700485 printk(BIOS_DEBUG, "/");
486 printk(BIOS_DEBUG, "%s {\n", node->name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200487
Patrick Rudolph666c1722018-04-03 09:57:33 +0200488 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200489 list_for_each(prop, node->properties, list_node)
490 print_property(&prop->prop, depth + 1);
491
Julius Werner23df4772019-05-17 22:50:18 -0700492 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700493
Patrick Rudolph666c1722018-04-03 09:57:33 +0200494 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200495 list_for_each(child, node->children, list_node)
496 print_node(child, depth + 1);
Julius Werner0d746532019-05-06 19:35:56 -0700497
498 print_indent(depth);
499 printk(BIOS_DEBUG, "};\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200500}
501
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200502void dt_print_node(const struct device_tree_node *node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200503{
504 print_node(node, 0);
505}
506
507
508
509/*
510 * Functions for reading and manipulating an unflattened device tree.
511 */
512
513/*
514 * Read #address-cells and #size-cells properties from a node.
515 *
516 * @param node The device tree node to read from.
517 * @param addrcp Pointer to store #address-cells in, skipped if NULL.
518 * @param sizecp Pointer to store #size-cells in, skipped if NULL.
519 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200520void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
521 u32 *sizecp)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200522{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200523 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200524 list_for_each(prop, node->properties, list_node) {
525 if (addrcp && !strcmp("#address-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700526 *addrcp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200527 if (sizecp && !strcmp("#size-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700528 *sizecp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200529 }
530}
531
532/*
533 * Find a node from a device tree path, relative to a parent node.
534 *
535 * @param parent The node from which to start the relative path lookup.
536 * @param path An array of path component strings that will be looked
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200537 * up in order to find the node. Must be terminated with
538 * a NULL pointer. Example: {'firmware', 'coreboot', NULL}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200539 * @param addrcp Pointer that will be updated with any #address-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200540 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200541 * @param sizecp Pointer that will be updated with any #size-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200542 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200543 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
544 * @return The found/created node, or NULL.
545 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200546struct device_tree_node *dt_find_node(struct device_tree_node *parent,
547 const char **path, u32 *addrcp,
548 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200549{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200550 struct device_tree_node *node, *found = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200551
Julius Werner23df4772019-05-17 22:50:18 -0700552 /* Update #address-cells and #size-cells for this level. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200553 dt_read_cell_props(parent, addrcp, sizecp);
554
555 if (!*path)
556 return parent;
557
Julius Werner23df4772019-05-17 22:50:18 -0700558 /* Find the next node in the path, if it exists. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200559 list_for_each(node, parent->children, list_node) {
560 if (!strcmp(node->name, *path)) {
561 found = node;
562 break;
563 }
564 }
565
Julius Werner23df4772019-05-17 22:50:18 -0700566 /* Otherwise create it or return NULL. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200567 if (!found) {
568 if (!create)
569 return NULL;
570
Julius Werner9636a102019-05-03 17:36:43 -0700571 found = malloc(sizeof(*found));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200572 if (!found)
573 return NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200574 found->name = strdup(*path);
575 if (!found->name)
576 return NULL;
577
578 list_insert_after(&found->list_node, &parent->children);
579 }
580
581 return dt_find_node(found, path + 1, addrcp, sizecp, create);
582}
583
584/*
Julius Wernerf36d53c2019-05-03 18:23:34 -0700585 * Find a node in the tree from a string device tree path.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200586 *
Julius Wernerf36d53c2019-05-03 18:23:34 -0700587 * @param tree The device tree to search.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200588 * @param path A string representing a path in the device tree, with
Julius Wernerfbec63d2019-05-03 18:29:28 -0700589 * nodes separated by '/'. Example: "/firmware/coreboot"
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200590 * @param addrcp Pointer that will be updated with any #address-cells
591 * value found in the path. May be NULL to ignore.
592 * @param sizecp Pointer that will be updated with any #size-cells
593 * value found in the path. May be NULL to ignore.
594 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
595 * @return The found/created node, or NULL.
596 *
Julius Werner6d5695f2019-05-06 19:23:28 -0700597 * It is the caller responsibility to provide a path string that doesn't end
598 * with a '/' and doesn't contain any "//". If the path does not start with a
599 * '/', the first segment is interpreted as an alias. */
Julius Wernerf36d53c2019-05-03 18:23:34 -0700600struct device_tree_node *dt_find_node_by_path(struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200601 const char *path, u32 *addrcp,
602 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200603{
Julius Werner6d5695f2019-05-06 19:23:28 -0700604 char *sub_path;
605 char *duped_str;
606 struct device_tree_node *parent;
607 char *next_slash;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200608 /* Hopefully enough depth for any node. */
609 const char *path_array[15];
610 int i;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200611 struct device_tree_node *node = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200612
Julius Werner23df4772019-05-17 22:50:18 -0700613 if (path[0] == '/') { /* regular path */
614 if (path[1] == '\0') { /* special case: "/" is root node */
Julius Werner6d5695f2019-05-06 19:23:28 -0700615 dt_read_cell_props(tree->root, addrcp, sizecp);
616 return tree->root;
617 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200618
Julius Werner6d5695f2019-05-06 19:23:28 -0700619 sub_path = duped_str = strdup(&path[1]);
620 if (!sub_path)
621 return NULL;
622
623 parent = tree->root;
Julius Werner23df4772019-05-17 22:50:18 -0700624 } else { /* alias */
Julius Werner6d5695f2019-05-06 19:23:28 -0700625 char *alias;
626
627 alias = duped_str = strdup(path);
628 if (!alias)
629 return NULL;
630
631 sub_path = strchr(alias, '/');
632 if (sub_path)
633 *sub_path = '\0';
634
635 parent = dt_find_node_by_alias(tree, alias);
636 if (!parent) {
637 printk(BIOS_DEBUG,
638 "Could not find node '%s', alias '%s' does not exist\n",
639 path, alias);
640 free(duped_str);
641 return NULL;
642 }
643
644 if (!sub_path) {
Julius Werner23df4772019-05-17 22:50:18 -0700645 /* it's just the alias, no sub-path */
Julius Werner6d5695f2019-05-06 19:23:28 -0700646 free(duped_str);
647 return parent;
648 }
649
650 sub_path++;
651 }
652
653 next_slash = sub_path;
654 path_array[0] = sub_path;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200655 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200656 next_slash = strchr(next_slash, '/');
657 if (!next_slash)
658 break;
659
660 *next_slash++ = '\0';
661 path_array[i] = next_slash;
662 }
663
664 if (!next_slash) {
665 path_array[i] = NULL;
Julius Werner6d5695f2019-05-06 19:23:28 -0700666 node = dt_find_node(parent, path_array,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200667 addrcp, sizecp, create);
668 }
669
Julius Werner6d5695f2019-05-06 19:23:28 -0700670 free(duped_str);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200671 return node;
672}
673
Julius Werner6d5695f2019-05-06 19:23:28 -0700674/*
675 * Find a node from an alias
676 *
677 * @param tree The device tree.
678 * @param alias The alias name.
679 * @return The found node, or NULL.
680 */
681struct device_tree_node *dt_find_node_by_alias(struct device_tree *tree,
682 const char *alias)
683{
684 struct device_tree_node *node;
685 const char *alias_path;
686
687 node = dt_find_node_by_path(tree, "/aliases", NULL, NULL, 0);
688 if (!node)
689 return NULL;
690
691 alias_path = dt_find_string_prop(node, alias);
692 if (!alias_path)
693 return NULL;
694
695 return dt_find_node_by_path(tree, alias_path, NULL, NULL, 0);
696}
697
Julius Werner6702b682019-05-03 18:13:53 -0700698struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root,
699 uint32_t phandle)
700{
701 if (!root)
702 return NULL;
703
704 if (root->phandle == phandle)
705 return root;
706
707 struct device_tree_node *node;
708 struct device_tree_node *result;
709 list_for_each(node, root->children, list_node) {
710 result = dt_find_node_by_phandle(node, phandle);
711 if (result)
712 return result;
713 }
714
715 return NULL;
716}
717
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200718/*
719 * Check if given node is compatible.
720 *
721 * @param node The node which is to be checked for compatible property.
722 * @param compat The compatible string to match.
723 * @return 1 = compatible, 0 = not compatible.
724 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200725static int dt_check_compat_match(struct device_tree_node *node,
726 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200727{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200728 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200729
730 list_for_each(prop, node->properties, list_node) {
731 if (!strcmp("compatible", prop->prop.name)) {
732 size_t bytes = prop->prop.size;
733 const char *str = prop->prop.data;
734 while (bytes > 0) {
735 if (!strncmp(compat, str, bytes))
736 return 1;
737 size_t len = strnlen(str, bytes) + 1;
738 if (bytes <= len)
739 break;
740 str += len;
741 bytes -= len;
742 }
743 break;
744 }
745 }
746
747 return 0;
748}
749
750/*
751 * Find a node from a compatible string, in the subtree of a parent node.
752 *
753 * @param parent The parent node under which to look.
754 * @param compat The compatible string to find.
755 * @return The found node, or NULL.
756 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200757struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
758 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200759{
Julius Werner23df4772019-05-17 22:50:18 -0700760 /* Check if the parent node itself is compatible. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200761 if (dt_check_compat_match(parent, compat))
762 return parent;
763
Patrick Rudolph666c1722018-04-03 09:57:33 +0200764 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200765 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200766 struct device_tree_node *found = dt_find_compat(child, compat);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200767 if (found)
768 return found;
769 }
770
771 return NULL;
772}
773
774/*
775 * Find the next compatible child of a given parent. All children upto the
776 * child passed in by caller are ignored. If child is NULL, it considers all the
777 * children to find the first child which is compatible.
778 *
779 * @param parent The parent node under which to look.
780 * @param child The child node to start search from (exclusive). If NULL
781 * consider all children.
782 * @param compat The compatible string to find.
783 * @return The found node, or NULL.
784 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200785struct device_tree_node *
786dt_find_next_compat_child(struct device_tree_node *parent,
787 struct device_tree_node *child,
788 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200789{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200790 struct device_tree_node *next;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200791 int ignore = 0;
792
793 if (child)
794 ignore = 1;
795
796 list_for_each(next, parent->children, list_node) {
797 if (ignore) {
798 if (child == next)
799 ignore = 0;
800 continue;
801 }
802
803 if (dt_check_compat_match(next, compat))
804 return next;
805 }
806
807 return NULL;
808}
809
810/*
811 * Find a node with matching property value, in the subtree of a parent node.
812 *
813 * @param parent The parent node under which to look.
814 * @param name The property name to look for.
815 * @param data The property value to look for.
816 * @param size The property size.
817 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200818struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
819 const char *name, void *data,
820 size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200821{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200822 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200823
824 /* Check if parent itself has the required property value. */
825 list_for_each(prop, parent->properties, list_node) {
826 if (!strcmp(name, prop->prop.name)) {
827 size_t bytes = prop->prop.size;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200828 const void *prop_data = prop->prop.data;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200829 if (size != bytes)
830 break;
831 if (!memcmp(data, prop_data, size))
832 return parent;
833 break;
834 }
835 }
836
Patrick Rudolph666c1722018-04-03 09:57:33 +0200837 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200838 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200839 struct device_tree_node *found = dt_find_prop_value(child, name,
840 data, size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200841 if (found)
842 return found;
843 }
844 return NULL;
845}
846
847/*
848 * Write an arbitrary sized big-endian integer into a pointer.
849 *
850 * @param dest Pointer to the DT property data buffer to write.
Elyes HAOUAS1ec76442018-08-07 12:20:04 +0200851 * @param src The integer to write (in CPU endianness).
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200852 * @param length the length of the destination integer in bytes.
853 */
854void dt_write_int(u8 *dest, u64 src, size_t length)
855{
856 while (length--) {
857 dest[length] = (u8)src;
858 src >>= 8;
859 }
860}
861
862/*
Patrick Rudolph5ccc7312018-05-30 15:05:28 +0200863 * Delete a property by name in a given node if it exists.
864 *
865 * @param node The device tree node to operate on.
866 * @param name The name of the property to delete.
867 */
868void dt_delete_prop(struct device_tree_node *node, const char *name)
869{
870 struct device_tree_property *prop;
871
872 list_for_each(prop, node->properties, list_node) {
873 if (!strcmp(prop->prop.name, name)) {
874 list_remove(&prop->list_node);
875 return;
876 }
877 }
878}
879
880/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200881 * Add an arbitrary property to a node, or update it if it already exists.
882 *
883 * @param node The device tree node to add to.
884 * @param name The name of the new property.
885 * @param data The raw data blob to be stored in the property.
886 * @param size The size of data in bytes.
887 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200888void dt_add_bin_prop(struct device_tree_node *node, const char *name,
Julius Werner0e9116f2019-05-13 17:30:31 -0700889 void *data, size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200890{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200891 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200892
893 list_for_each(prop, node->properties, list_node) {
894 if (!strcmp(prop->prop.name, name)) {
895 prop->prop.data = data;
896 prop->prop.size = size;
897 return;
898 }
899 }
900
Julius Werner9636a102019-05-03 17:36:43 -0700901 prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200902 list_insert_after(&prop->list_node, &node->properties);
903 prop->prop.name = name;
904 prop->prop.data = data;
905 prop->prop.size = size;
906}
907
908/*
909 * Find given string property in a node and return its content.
910 *
911 * @param node The device tree node to search.
912 * @param name The name of the property.
913 * @return The found string, or NULL.
914 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200915const char *dt_find_string_prop(const struct device_tree_node *node,
916 const char *name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200917{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200918 const void *content;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200919 size_t size;
920
921 dt_find_bin_prop(node, name, &content, &size);
922
923 return content;
924}
925
926/*
927 * Find given property in a node.
928 *
929 * @param node The device tree node to search.
930 * @param name The name of the property.
931 * @param data Pointer to return raw data blob in the property.
932 * @param size Pointer to return the size of data in bytes.
933 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200934void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
935 const void **data, size_t *size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200936{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200937 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200938
939 *data = NULL;
940 *size = 0;
941
942 list_for_each(prop, node->properties, list_node) {
943 if (!strcmp(prop->prop.name, name)) {
944 *data = prop->prop.data;
945 *size = prop->prop.size;
946 return;
947 }
948 }
949}
950
951/*
952 * Add a string property to a node, or update it if it already exists.
953 *
954 * @param node The device tree node to add to.
955 * @param name The name of the new property.
956 * @param str The zero-terminated string to be stored in the property.
957 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200958void dt_add_string_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200959 const char *str)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200960{
Julius Werner0e9116f2019-05-13 17:30:31 -0700961 dt_add_bin_prop(node, name, (char *)str, strlen(str) + 1);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200962}
963
964/*
965 * Add a 32-bit integer property to a node, or update it if it already exists.
966 *
967 * @param node The device tree node to add to.
968 * @param name The name of the new property.
969 * @param val The integer to be stored in the property.
970 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200971void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200972{
Julius Werner9636a102019-05-03 17:36:43 -0700973 u32 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200974 *val_ptr = htobe32(val);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200975 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
976}
977
978/*
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200979 * Add a 64-bit integer property to a node, or update it if it already exists.
980 *
981 * @param node The device tree node to add to.
982 * @param name The name of the new property.
983 * @param val The integer to be stored in the property.
984 */
985void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
986{
Julius Werner9636a102019-05-03 17:36:43 -0700987 u64 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200988 *val_ptr = htobe64(val);
989 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
990}
991
992/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200993 * Add a 'reg' address list property to a node, or update it if it exists.
994 *
995 * @param node The device tree node to add to.
996 * @param addrs Array of address values to be stored in the property.
997 * @param sizes Array of corresponding size values to 'addrs'.
998 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
999 * @param addr_cells Value of #address-cells property valid for this node.
1000 * @param size_cells Value of #size-cells property valid for this node.
1001 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001002void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001003 int count, u32 addr_cells, u32 size_cells)
1004{
1005 int i;
1006 size_t length = (addr_cells + size_cells) * sizeof(u32) * count;
Julius Werner9636a102019-05-03 17:36:43 -07001007 u8 *data = xmalloc(length);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001008 u8 *cur = data;
1009
1010 for (i = 0; i < count; i++) {
1011 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
1012 cur += addr_cells * sizeof(u32);
1013 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
1014 cur += size_cells * sizeof(u32);
1015 }
1016
1017 dt_add_bin_prop(node, "reg", data, length);
1018}
1019
1020/*
1021 * Fixups to apply to a kernel's device tree before booting it.
1022 */
1023
Patrick Rudolph666c1722018-04-03 09:57:33 +02001024struct list_node device_tree_fixups;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001025
Patrick Rudolph666c1722018-04-03 09:57:33 +02001026int dt_apply_fixups(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001027{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001028 struct device_tree_fixup *fixup;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001029 list_for_each(fixup, device_tree_fixups, list_node) {
1030 assert(fixup->fixup);
1031 if (fixup->fixup(fixup, tree))
1032 return 1;
1033 }
1034 return 0;
1035}
1036
Patrick Rudolph666c1722018-04-03 09:57:33 +02001037int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001038 void *data, size_t data_size, int create)
1039{
1040 char *path_copy, *prop_name;
Patrick Rudolph666c1722018-04-03 09:57:33 +02001041 struct device_tree_node *dt_node;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001042
1043 path_copy = strdup(path);
1044
1045 if (!path_copy) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001046 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
1047 path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001048 return 1;
1049 }
1050
1051 prop_name = strrchr(path_copy, '/');
1052 if (!prop_name) {
Patrick Rudolph679d6242018-07-11 13:53:04 +02001053 free(path_copy);
Patrick Rudolph666c1722018-04-03 09:57:33 +02001054 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001055 return 1;
1056 }
1057
1058 *prop_name++ = '\0'; /* Separate path from the property name. */
1059
Julius Wernerf36d53c2019-05-03 18:23:34 -07001060 dt_node = dt_find_node_by_path(tree, path_copy, NULL,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001061 NULL, create);
1062
1063 if (!dt_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001064 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001065 create ? "create" : "find", path_copy);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001066 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001067 return 1;
1068 }
1069
1070 dt_add_bin_prop(dt_node, prop_name, data, data_size);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001071 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001072
1073 return 0;
1074}
1075
1076/*
1077 * Prepare the /reserved-memory/ node.
1078 *
1079 * Technically, this can be called more than one time, to init and/or retrieve
1080 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
1081 *
1082 * @tree: Device tree to add/retrieve from.
1083 * @return: The /reserved-memory/ node (or NULL, if error).
1084 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001085struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001086{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001087 struct device_tree_node *reserved;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001088 u32 addr = 0, size = 0;
1089
Julius Wernerfbec63d2019-05-03 18:29:28 -07001090 reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001091 &size, 1);
1092 if (!reserved)
1093 return NULL;
1094
Julius Werner23df4772019-05-17 22:50:18 -07001095 /* Binding doc says this should have the same #{address,size}-cells as
1096 the root. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001097 dt_add_u32_prop(reserved, "#address-cells", addr);
1098 dt_add_u32_prop(reserved, "#size-cells", size);
Julius Werner23df4772019-05-17 22:50:18 -07001099 /* Binding doc says this should be empty (1:1 mapping from root). */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001100 dt_add_bin_prop(reserved, "ranges", NULL, 0);
1101
1102 return reserved;
1103}
Julius Werner735ddc92019-05-07 17:05:28 -07001104
1105/*
1106 * Increment a single phandle in prop at a given offset by a given adjustment.
1107 *
1108 * @param prop Property whose phandle should be adjusted.
1109 * @param adjustment Value that should be added to the existing phandle.
1110 * @param offset Byte offset of the phandle in the property data.
1111 *
1112 * @return New phandle value, or 0 on error.
1113 */
1114static uint32_t dt_adjust_phandle(struct device_tree_property *prop,
1115 uint32_t adjustment, uint32_t offset)
1116{
1117 if (offset + 4 > prop->prop.size)
1118 return 0;
1119
1120 uint32_t phandle = be32dec(prop->prop.data + offset);
1121 if (phandle == 0 ||
1122 phandle == FDT_PHANDLE_ILLEGAL ||
1123 phandle == 0xffffffff)
1124 return 0;
1125
1126 phandle += adjustment;
1127 if (phandle >= FDT_PHANDLE_ILLEGAL)
1128 return 0;
1129
1130 be32enc(prop->prop.data + offset, phandle);
1131 return phandle;
1132}
1133
1134/*
1135 * Adjust all phandles in subtree by adding a new base offset.
1136 *
1137 * @param node Root node of the subtree to work on.
1138 * @param base New phandle base to be added to all phandles.
1139 *
1140 * @return New highest phandle in the subtree, or 0 on error.
1141 */
1142static uint32_t dt_adjust_all_phandles(struct device_tree_node *node,
1143 uint32_t base)
1144{
Julius Werner23df4772019-05-17 22:50:18 -07001145 uint32_t new_max = MAX(base, 1); /* make sure we don't return 0 */
Julius Werner735ddc92019-05-07 17:05:28 -07001146 struct device_tree_property *prop;
1147 struct device_tree_node *child;
1148
1149 if (!node)
1150 return new_max;
1151
1152 list_for_each(prop, node->properties, list_node)
1153 if (dt_prop_is_phandle(prop)) {
1154 node->phandle = dt_adjust_phandle(prop, base, 0);
1155 if (!node->phandle)
1156 return 0;
1157 new_max = MAX(new_max, node->phandle);
Julius Werner23df4772019-05-17 22:50:18 -07001158 } /* no break -- can have more than one phandle prop */
Julius Werner735ddc92019-05-07 17:05:28 -07001159
1160 list_for_each(child, node->children, list_node)
1161 new_max = MAX(new_max, dt_adjust_all_phandles(child, base));
1162
1163 return new_max;
1164}
1165
1166/*
1167 * Apply a /__local_fixup__ subtree to the corresponding overlay subtree.
1168 *
1169 * @param node Root node of the overlay subtree to fix up.
1170 * @param node Root node of the /__local_fixup__ subtree.
1171 * @param base Adjustment that was added to phandles in the overlay.
1172 *
1173 * @return 0 on success, -1 on error.
1174 */
1175static int dt_fixup_locals(struct device_tree_node *node,
1176 struct device_tree_node *fixup, uint32_t base)
1177{
1178 struct device_tree_property *prop;
1179 struct device_tree_property *fixup_prop;
1180 struct device_tree_node *child;
1181 struct device_tree_node *fixup_child;
1182 int i;
1183
Julius Werner23df4772019-05-17 22:50:18 -07001184 /*
1185 * For local fixups the /__local_fixup__ subtree contains the same node
1186 * hierarchy as the main tree we're fixing up. Each property contains
1187 * the fixup offsets for the respective property in the main tree. For
1188 * each property in the fixup node, find the corresponding property in
1189 * the base node and apply fixups to all offsets it specifies.
1190 */
Julius Werner735ddc92019-05-07 17:05:28 -07001191 list_for_each(fixup_prop, fixup->properties, list_node) {
1192 struct device_tree_property *base_prop = NULL;
1193 list_for_each(prop, node->properties, list_node)
1194 if (!strcmp(prop->prop.name, fixup_prop->prop.name)) {
1195 base_prop = prop;
1196 break;
1197 }
1198
Julius Werner23df4772019-05-17 22:50:18 -07001199 /* We should always find a corresponding base prop for a fixup,
1200 and fixup props contain a list of 32-bit fixup offsets. */
Julius Werner735ddc92019-05-07 17:05:28 -07001201 if (!base_prop || fixup_prop->prop.size % sizeof(uint32_t))
1202 return -1;
1203
1204 for (i = 0; i < fixup_prop->prop.size; i += sizeof(uint32_t))
1205 if (!dt_adjust_phandle(base_prop, base, be32dec(
1206 fixup_prop->prop.data + i)))
1207 return -1;
1208 }
1209
Julius Werner23df4772019-05-17 22:50:18 -07001210 /* Now recursively descend both the base tree and the /__local_fixups__
1211 subtree in sync to apply all fixups. */
Julius Werner735ddc92019-05-07 17:05:28 -07001212 list_for_each(fixup_child, fixup->children, list_node) {
1213 struct device_tree_node *base_child = NULL;
1214 list_for_each(child, node->children, list_node)
1215 if (!strcmp(child->name, fixup_child->name)) {
1216 base_child = child;
1217 break;
1218 }
1219
Julius Werner23df4772019-05-17 22:50:18 -07001220 /* All fixup nodes should have a corresponding base node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001221 if (!base_child)
1222 return -1;
1223
1224 if (dt_fixup_locals(base_child, fixup_child, base) < 0)
1225 return -1;
1226 }
1227
1228 return 0;
1229}
1230
1231/*
1232 * Update all /__symbols__ properties in an overlay that start with
1233 * "/fragment@X/__overlay__" with corresponding path prefix in the base tree.
1234 *
1235 * @param symbols /__symbols__ done to update.
1236 * @param fragment /fragment@X node that references to should be updated.
1237 * @param base_path Path of base tree node that the fragment overlaid.
1238 */
1239static void dt_fix_symbols(struct device_tree_node *symbols,
1240 struct device_tree_node *fragment,
1241 const char *base_path)
1242{
1243 struct device_tree_property *prop;
Julius Werner23df4772019-05-17 22:50:18 -07001244 char buf[512]; /* Should be enough for maximum DT path length? */
1245 char node_path[64]; /* easily enough for /fragment@XXXX/__overlay__ */
Julius Werner735ddc92019-05-07 17:05:28 -07001246
Julius Werner23df4772019-05-17 22:50:18 -07001247 if (!symbols) /* If the overlay has no /__symbols__ node, we're done! */
Julius Werner735ddc92019-05-07 17:05:28 -07001248 return;
1249
1250 int len = snprintf(node_path, sizeof(node_path), "/%s/__overlay__",
1251 fragment->name);
1252
1253 list_for_each(prop, symbols->properties, list_node)
1254 if (!strncmp(prop->prop.data, node_path, len)) {
1255 prop->prop.size = snprintf(buf, sizeof(buf), "%s%s",
1256 base_path, (char *)prop->prop.data + len) + 1;
1257 free(prop->prop.data);
1258 prop->prop.data = strdup(buf);
1259 }
1260}
1261
1262/*
1263 * Fix up overlay according to a property in /__fixup__. If the fixed property
1264 * is a /fragment@X:target, also update /__symbols__ references to fragment.
1265 *
1266 * @params overlay Overlay to fix up.
1267 * @params fixup /__fixup__ property.
1268 * @params phandle phandle value to insert where the fixup points to.
1269 * @params base_path Path to the base DT node that the fixup points to.
1270 * @params overlay_symbols /__symbols__ node of the overlay.
1271 *
1272 * @return 0 on success, -1 on error.
1273 */
1274static int dt_fixup_external(struct device_tree *overlay,
1275 struct device_tree_property *fixup,
1276 uint32_t phandle, const char *base_path,
1277 struct device_tree_node *overlay_symbols)
1278{
1279 struct device_tree_property *prop;
1280
Julius Werner23df4772019-05-17 22:50:18 -07001281 /* External fixup properties are encoded as "<path>:<prop>:<offset>". */
Julius Werner735ddc92019-05-07 17:05:28 -07001282 char *entry = fixup->prop.data;
1283 while ((void *)entry < fixup->prop.data + fixup->prop.size) {
Julius Werner23df4772019-05-17 22:50:18 -07001284 /* okay to destroy fixup property value, won't need it again */
Julius Werner735ddc92019-05-07 17:05:28 -07001285 char *node_path = entry;
1286 entry = strchr(node_path, ':');
1287 if (!entry)
1288 return -1;
1289 *entry++ = '\0';
1290
1291 char *prop_name = entry;
1292 entry = strchr(prop_name, ':');
1293 if (!entry)
1294 return -1;
1295 *entry++ = '\0';
1296
1297 struct device_tree_node *ovl_node = dt_find_node_by_path(
1298 overlay, node_path, NULL, NULL, 0);
1299 if (!ovl_node || !isdigit(*entry))
1300 return -1;
1301
1302 struct device_tree_property *ovl_prop = NULL;
1303 list_for_each(prop, ovl_node->properties, list_node)
1304 if (!strcmp(prop->prop.name, prop_name)) {
1305 ovl_prop = prop;
1306 break;
1307 }
1308
Julius Werner23df4772019-05-17 22:50:18 -07001309 /* Move entry to first char after number, must be a '\0'. */
Julius Werner735ddc92019-05-07 17:05:28 -07001310 uint32_t offset = skip_atoi(&entry);
1311 if (!ovl_prop || offset + 4 > ovl_prop->prop.size || entry[0])
1312 return -1;
Julius Werner23df4772019-05-17 22:50:18 -07001313 entry++; /* jump over '\0' to potential next fixup */
Julius Werner735ddc92019-05-07 17:05:28 -07001314
1315 be32enc(ovl_prop->prop.data + offset, phandle);
1316
Julius Werner23df4772019-05-17 22:50:18 -07001317 /* If this is a /fragment@X:target property, update references
1318 to this fragment in the overlay __symbols__ now. */
Julius Werner735ddc92019-05-07 17:05:28 -07001319 if (offset == 0 && !strcmp(prop_name, "target") &&
Julius Werner23df4772019-05-17 22:50:18 -07001320 !strchr(node_path + 1, '/')) /* only toplevel nodes */
Julius Werner735ddc92019-05-07 17:05:28 -07001321 dt_fix_symbols(overlay_symbols, ovl_node, base_path);
1322 }
1323
1324 return 0;
1325}
1326
1327/*
1328 * Apply all /__fixup__ properties in the overlay. This will destroy the
1329 * property data in /__fixup__ and it should not be accessed again.
1330 *
1331 * @params tree Base device tree that the overlay updates.
1332 * @params symbols /__symbols__ node of the base device tree.
1333 * @params overlay Overlay to fix up.
1334 * @params fixups /__fixup__ node in the overlay.
1335 * @params overlay_symbols /__symbols__ node of the overlay.
1336 *
1337 * @return 0 on success, -1 on error.
1338 */
1339static int dt_fixup_all_externals(struct device_tree *tree,
1340 struct device_tree_node *symbols,
1341 struct device_tree *overlay,
1342 struct device_tree_node *fixups,
1343 struct device_tree_node *overlay_symbols)
1344{
1345 struct device_tree_property *fix;
1346
Julius Werner23df4772019-05-17 22:50:18 -07001347 /* If we have any external fixups, base tree must have /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001348 if (!symbols)
1349 return -1;
1350
Julius Werner23df4772019-05-17 22:50:18 -07001351 /*
1352 * Unlike /__local_fixups__, /__fixups__ is not a whole subtree that
1353 * mirrors the node hierarchy. It's just a directory of fixup properties
1354 * that each directly contain all information necessary to apply them.
1355 */
Julius Werner735ddc92019-05-07 17:05:28 -07001356 list_for_each(fix, fixups->properties, list_node) {
Julius Werner23df4772019-05-17 22:50:18 -07001357 /* The name of a fixup property is the label of the node we want
1358 a property to phandle-reference. Look up in /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001359 const char *path = dt_find_string_prop(symbols, fix->prop.name);
1360 if (!path)
1361 return -1;
1362
Julius Werner23df4772019-05-17 22:50:18 -07001363 /* Find node the label pointed to to figure out its phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001364 struct device_tree_node *node = dt_find_node_by_path(tree, path,
1365 NULL, NULL, 0);
1366 if (!node)
1367 return -1;
1368
Julius Werner23df4772019-05-17 22:50:18 -07001369 /* Write into the overlay property(s) pointing to that node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001370 if (dt_fixup_external(overlay, fix, node->phandle,
1371 path, overlay_symbols) < 0)
1372 return -1;
1373 }
1374
1375 return 0;
1376}
1377
1378/*
1379 * Copy all nodes and properties from one DT subtree into another. This is a
1380 * shallow copy so both trees will point to the same property data afterwards.
1381 *
1382 * @params dst Destination subtree to copy into.
1383 * @params src Source subtree to copy from.
1384 * @params upd 1 to overwrite same-name properties, 0 to discard them.
1385 */
1386static void dt_copy_subtree(struct device_tree_node *dst,
1387 struct device_tree_node *src, int upd)
1388{
1389 struct device_tree_property *prop;
1390 struct device_tree_property *src_prop;
1391 list_for_each(src_prop, src->properties, list_node) {
1392 if (dt_prop_is_phandle(src_prop) ||
1393 !strcmp(src_prop->prop.name, "name")) {
1394 printk(BIOS_DEBUG,
1395 "WARNING: ignoring illegal overlay prop '%s'\n",
1396 src_prop->prop.name);
1397 continue;
1398 }
1399
1400 struct device_tree_property *dst_prop = NULL;
1401 list_for_each(prop, dst->properties, list_node)
1402 if (!strcmp(prop->prop.name, src_prop->prop.name)) {
1403 dst_prop = prop;
1404 break;
1405 }
1406
1407 if (dst_prop) {
1408 if (!upd) {
1409 printk(BIOS_DEBUG,
1410 "WARNING: ignoring prop update '%s'\n",
1411 src_prop->prop.name);
1412 continue;
1413 }
1414 } else {
1415 dst_prop = xzalloc(sizeof(*dst_prop));
1416 list_insert_after(&dst_prop->list_node,
1417 &dst->properties);
1418 }
1419
1420 dst_prop->prop = src_prop->prop;
1421 }
1422
1423 struct device_tree_node *node;
1424 struct device_tree_node *src_node;
1425 list_for_each(src_node, src->children, list_node) {
1426 struct device_tree_node *dst_node = NULL;
1427 list_for_each(node, dst->children, list_node)
1428 if (!strcmp(node->name, src_node->name)) {
1429 dst_node = node;
1430 break;
1431 }
1432
1433 if (!dst_node) {
1434 dst_node = xzalloc(sizeof(*dst_node));
1435 *dst_node = *src_node;
1436 list_insert_after(&dst_node->list_node, &dst->children);
1437 } else {
1438 dt_copy_subtree(dst_node, src_node, upd);
1439 }
1440 }
1441}
1442
1443/*
1444 * Apply an overlay /fragment@X node to a base device tree.
1445 *
1446 * @param tree Base device tree.
1447 * @param fragment /fragment@X node.
1448 * @params overlay_symbols /__symbols__ node of the overlay.
1449 *
1450 * @return 0 on success, -1 on error.
1451 */
1452static int dt_import_fragment(struct device_tree *tree,
1453 struct device_tree_node *fragment,
1454 struct device_tree_node *overlay_symbols)
1455{
Julius Werner23df4772019-05-17 22:50:18 -07001456 /* The actual overlaid nodes/props are in an __overlay__ child node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001457 static const char *overlay_path[] = { "__overlay__", NULL };
1458 struct device_tree_node *overlay = dt_find_node(fragment, overlay_path,
1459 NULL, NULL, 0);
1460
Julius Werner23df4772019-05-17 22:50:18 -07001461 /* If it doesn't have an __overlay__ child, it's not a fragment. */
Julius Werner735ddc92019-05-07 17:05:28 -07001462 if (!overlay)
1463 return 0;
1464
Julius Werner23df4772019-05-17 22:50:18 -07001465 /* Target node of the fragment can be given by path or by phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001466 struct device_tree_property *prop;
1467 struct device_tree_property *phandle = NULL;
1468 struct device_tree_property *path = NULL;
1469 list_for_each(prop, fragment->properties, list_node) {
1470 if (!strcmp(prop->prop.name, "target")) {
1471 phandle = prop;
Julius Werner23df4772019-05-17 22:50:18 -07001472 break; /* phandle target has priority, stop looking */
Julius Werner735ddc92019-05-07 17:05:28 -07001473 }
1474 if (!strcmp(prop->prop.name, "target-path"))
1475 path = prop;
1476 }
1477
1478 struct device_tree_node *target = NULL;
1479 if (phandle) {
1480 if (phandle->prop.size != sizeof(uint32_t))
1481 return -1;
1482 target = dt_find_node_by_phandle(tree->root,
1483 be32dec(phandle->prop.data));
Julius Werner23df4772019-05-17 22:50:18 -07001484 /* Symbols already updated as part of dt_fixup_external(). */
Julius Werner735ddc92019-05-07 17:05:28 -07001485 } else if (path) {
1486 target = dt_find_node_by_path(tree, path->prop.data,
1487 NULL, NULL, 0);
1488 dt_fix_symbols(overlay_symbols, fragment, path->prop.data);
1489 }
1490 if (!target)
1491 return -1;
1492
1493 dt_copy_subtree(target, overlay, 1);
1494 return 0;
1495}
1496
1497/*
1498 * Apply a device tree overlay to a base device tree. This will
1499 * destroy/incorporate the overlay data, so it should not be freed or reused.
1500 * See dtc.git/Documentation/dt-object-internal.txt for overlay format details.
1501 *
1502 * @param tree Unflattened base device tree to add the overlay into.
1503 * @param overlay Unflattened overlay device tree to apply to the base.
1504 *
1505 * @return 0 on success, -1 on error.
1506 */
1507int dt_apply_overlay(struct device_tree *tree, struct device_tree *overlay)
1508{
Julius Werner23df4772019-05-17 22:50:18 -07001509 /*
1510 * First, we need to make sure phandles inside the overlay don't clash
1511 * with those in the base tree. We just define the highest phandle value
1512 * in the base tree as the "phandle offset" for this overlay and
1513 * increment all phandles in it by that value.
1514 */
Julius Werner735ddc92019-05-07 17:05:28 -07001515 uint32_t phandle_base = tree->max_phandle;
1516 uint32_t new_max = dt_adjust_all_phandles(overlay->root, phandle_base);
1517 if (!new_max) {
1518 printk(BIOS_DEBUG, "ERROR: invalid phandles in overlay\n");
1519 return -1;
1520 }
1521 tree->max_phandle = new_max;
1522
Julius Werner23df4772019-05-17 22:50:18 -07001523 /* Now that we changed phandles in the overlay, we need to update any
1524 nodes referring to them. Those are listed in /__local_fixups__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001525 struct device_tree_node *local_fixups = dt_find_node_by_path(overlay,
1526 "/__local_fixups__", NULL, NULL, 0);
1527 if (local_fixups && dt_fixup_locals(overlay->root, local_fixups,
1528 phandle_base) < 0) {
1529 printk(BIOS_DEBUG, "ERROR: invalid local fixups in overlay\n");
1530 return -1;
1531 }
1532
Julius Werner23df4772019-05-17 22:50:18 -07001533 /*
1534 * Besides local phandle references (from nodes within the overlay to
1535 * other nodes within the overlay), the overlay may also contain phandle
1536 * references to the base tree. These are stored with invalid values and
1537 * must be updated now. /__symbols__ contains a list of all labels in
1538 * the base tree, and /__fixups__ describes all nodes in the overlay
1539 * that contain external phandle references.
1540 * We also take this opportunity to update all /fragment@X/__overlay__/
1541 * prefixes in the overlay's /__symbols__ node to the correct path that
1542 * the fragment will be placed in later, since this is the only step
1543 * where we have all necessary information for that easily available.
1544 */
Julius Werner735ddc92019-05-07 17:05:28 -07001545 struct device_tree_node *symbols = dt_find_node_by_path(tree,
1546 "/__symbols__", NULL, NULL, 0);
1547 struct device_tree_node *fixups = dt_find_node_by_path(overlay,
1548 "/__fixups__", NULL, NULL, 0);
1549 struct device_tree_node *overlay_symbols = dt_find_node_by_path(overlay,
1550 "/__symbols__", NULL, NULL, 0);
1551 if (fixups && dt_fixup_all_externals(tree, symbols, overlay,
1552 fixups, overlay_symbols) < 0) {
1553 printk(BIOS_DEBUG,
1554 "ERROR: cannot match external fixups from overlay\n");
1555 return -1;
1556 }
1557
Julius Werner23df4772019-05-17 22:50:18 -07001558 /* After all this fixing up, we can finally merge overlay into the tree
1559 (one fragment at a time, because for some reason it's split up). */
Julius Werner735ddc92019-05-07 17:05:28 -07001560 struct device_tree_node *fragment;
1561 list_for_each(fragment, overlay->root->children, list_node)
1562 if (dt_import_fragment(tree, fragment, overlay_symbols) < 0) {
1563 printk(BIOS_DEBUG, "ERROR: bad DT fragment '%s'\n",
1564 fragment->name);
1565 return -1;
1566 }
1567
Julius Werner23df4772019-05-17 22:50:18 -07001568 /*
1569 * We need to also update /__symbols__ to include labels from this
1570 * overlay, in case we want to load further overlays with external
1571 * phandle references to it. If the base tree already has a /__symbols__
1572 * we merge them together, otherwise we just insert the overlay's
1573 * /__symbols__ node into the base tree root.
1574 */
Julius Werner735ddc92019-05-07 17:05:28 -07001575 if (overlay_symbols) {
1576 if (symbols)
1577 dt_copy_subtree(symbols, overlay_symbols, 0);
1578 else
1579 list_insert_after(&overlay_symbols->list_node,
1580 &tree->root->children);
1581 }
1582
1583 return 0;
1584}