blob: 2e81a087792a3455fcda7e1dc7dd52a94c1def67 [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>
21#include <device_tree.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020022#include <endian.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020023#include <stdint.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +020024#include <string.h>
25#include <stddef.h>
26#include <stdlib.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020027
28/*
29 * Functions for picking apart flattened trees.
30 */
31
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020032int fdt_next_property(const void *blob, uint32_t offset,
33 struct fdt_property *prop)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020034{
Patrick Rudolph666c1722018-04-03 09:57:33 +020035 struct fdt_header *header = (struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020036 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
37
38 int index = 0;
Patrick Rudolph666c1722018-04-03 09:57:33 +020039 if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020040 return 0;
41
Patrick Rudolph666c1722018-04-03 09:57:33 +020042 uint32_t size = be32toh(ptr[index++]);
43 uint32_t name_offset = be32toh(ptr[index++]);
44 name_offset += be32toh(header->strings_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020045
46 if (prop) {
47 prop->name = (char *)((uint8_t *)blob + name_offset);
48 prop->data = &ptr[index];
49 prop->size = size;
50 }
51
Patrick Rudolph666c1722018-04-03 09:57:33 +020052 index += DIV_ROUND_UP(size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020053
Patrick Rudolph666c1722018-04-03 09:57:33 +020054 return index * sizeof(uint32_t);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020055}
56
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020057int fdt_node_name(const void *blob, uint32_t offset, const char **name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020058{
59 uint8_t *ptr = ((uint8_t *)blob) + offset;
Julius Wernera5ea3a22019-05-07 17:38:12 -070060 if (be32dec(ptr) != FDT_TOKEN_BEGIN_NODE)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020061 return 0;
62
63 ptr += 4;
64 if (name)
65 *name = (char *)ptr;
Patrick Rudolph666c1722018-04-03 09:57:33 +020066 return ALIGN_UP(strlen((char *)ptr) + 1, sizeof(uint32_t)) + 4;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020067}
68
Julius Werner6702b682019-05-03 18:13:53 -070069static int dt_prop_is_phandle(struct device_tree_property *prop)
70{
71 return !(strcmp("phandle", prop->prop.name) &&
72 strcmp("linux,phandle", prop->prop.name));
73}
74
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020075
76
77/*
78 * Functions for printing flattened trees.
79 */
80
81static void print_indent(int depth)
82{
Julius Werner0d746532019-05-06 19:35:56 -070083 printk(BIOS_DEBUG, "%*s", depth * 8, "");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020084}
85
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020086static void print_property(const struct fdt_property *prop, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020087{
Julius Werner0d746532019-05-06 19:35:56 -070088 int is_string = prop->size > 0 &&
89 ((char *)prop->data)[prop->size - 1] == '\0';
90
91 if (is_string)
92 for (const char *c = prop->data; *c != '\0'; c++)
93 if (!isprint(*c))
94 is_string = 0;
95
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020096 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -070097 if (is_string) {
98 printk(BIOS_DEBUG, "%s = \"%s\";\n",
99 prop->name, (const char *)prop->data);
100 } else {
101 printk(BIOS_DEBUG, "%s = < ", prop->name);
102 for (int i = 0; i < MIN(128, prop->size); i += 4) {
103 uint32_t val = 0;
104 for (int j = 0; j < MIN(4, prop->size - i); j++)
105 val |= ((uint8_t *)prop->data)[i + j] <<
106 (24 - j * 8);
107 printk(BIOS_DEBUG, "%#.2x ", val);
108 }
109 if (prop->size > 128)
110 printk(BIOS_DEBUG, "...");
111 printk(BIOS_DEBUG, ">;\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200112 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200113}
114
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200115static int print_flat_node(const void *blob, uint32_t start_offset, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200116{
117 int offset = start_offset;
118 const char *name;
119 int size;
120
121 size = fdt_node_name(blob, offset, &name);
122 if (!size)
123 return 0;
124 offset += size;
125
126 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -0700127 printk(BIOS_DEBUG, "%s {\n", name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200128
Patrick Rudolph666c1722018-04-03 09:57:33 +0200129 struct fdt_property prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200130 while ((size = fdt_next_property(blob, offset, &prop))) {
131 print_property(&prop, depth + 1);
132
133 offset += size;
134 }
135
Julius Werner23df4772019-05-17 22:50:18 -0700136 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700137
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200138 while ((size = print_flat_node(blob, offset, depth + 1)))
139 offset += size;
140
Julius Werner0d746532019-05-06 19:35:56 -0700141 print_indent(depth);
142 printk(BIOS_DEBUG, "}\n");
143
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200144 return offset - start_offset + sizeof(uint32_t);
145}
146
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200147void fdt_print_node(const void *blob, uint32_t offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200148{
149 print_flat_node(blob, offset, 0);
150}
151
152
153
154/*
155 * A utility function to skip past nodes in flattened trees.
156 */
157
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200158int fdt_skip_node(const void *blob, uint32_t start_offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200159{
160 int offset = start_offset;
161 int size;
162
163 const char *name;
164 size = fdt_node_name(blob, offset, &name);
165 if (!size)
166 return 0;
167 offset += size;
168
169 while ((size = fdt_next_property(blob, offset, NULL)))
170 offset += size;
171
172 while ((size = fdt_skip_node(blob, offset)))
173 offset += size;
174
175 return offset - start_offset + sizeof(uint32_t);
176}
177
178
179
180/*
181 * Functions to turn a flattened tree into an unflattened one.
182 */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200183
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200184static int fdt_unflatten_node(const void *blob, uint32_t start_offset,
Julius Werner6702b682019-05-03 18:13:53 -0700185 struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200186 struct device_tree_node **new_node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200187{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200188 struct list_node *last;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200189 int offset = start_offset;
190 const char *name;
191 int size;
192
193 size = fdt_node_name(blob, offset, &name);
194 if (!size)
195 return 0;
196 offset += size;
197
Julius Werner9636a102019-05-03 17:36:43 -0700198 struct device_tree_node *node = xzalloc(sizeof(*node));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200199 *new_node = node;
200 node->name = name;
201
Patrick Rudolph666c1722018-04-03 09:57:33 +0200202 struct fdt_property fprop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200203 last = &node->properties;
204 while ((size = fdt_next_property(blob, offset, &fprop))) {
Julius Werner9636a102019-05-03 17:36:43 -0700205 struct device_tree_property *prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200206 prop->prop = fprop;
207
Julius Werner6702b682019-05-03 18:13:53 -0700208 if (dt_prop_is_phandle(prop)) {
209 node->phandle = be32dec(prop->prop.data);
210 if (node->phandle > tree->max_phandle)
211 tree->max_phandle = node->phandle;
212 }
213
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200214 list_insert_after(&prop->list_node, last);
215 last = &prop->list_node;
216
217 offset += size;
218 }
219
Patrick Rudolph666c1722018-04-03 09:57:33 +0200220 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200221 last = &node->children;
Julius Werner6702b682019-05-03 18:13:53 -0700222 while ((size = fdt_unflatten_node(blob, offset, tree, &child))) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200223 list_insert_after(&child->list_node, last);
224 last = &child->list_node;
225
226 offset += size;
227 }
228
229 return offset - start_offset + sizeof(uint32_t);
230}
231
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200232static int fdt_unflatten_map_entry(const void *blob, uint32_t offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200233 struct device_tree_reserve_map_entry **new)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200234{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200235 const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset);
236 const uint64_t start = be64toh(ptr[0]);
237 const uint64_t size = be64toh(ptr[1]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200238
239 if (!size)
240 return 0;
241
Julius Werner9636a102019-05-03 17:36:43 -0700242 struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200243 *new = entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200244 entry->start = start;
245 entry->size = size;
246
247 return sizeof(uint64_t) * 2;
248}
249
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200250struct device_tree *fdt_unflatten(const void *blob)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200251{
Julius Werner9636a102019-05-03 17:36:43 -0700252 struct device_tree *tree = xzalloc(sizeof(*tree));
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200253 const struct fdt_header *header = (const struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200254 tree->header = header;
255
Julius Werner73eaec82019-05-03 17:58:07 -0700256 uint32_t magic = be32toh(header->magic);
257 uint32_t version = be32toh(header->version);
258 uint32_t last_comp_version = be32toh(header->last_comp_version);
259
260 if (magic != FDT_HEADER_MAGIC) {
261 printk(BIOS_DEBUG, "Invalid device tree magic %#.8x!\n", magic);
Jacob Garber698d83a2019-06-07 10:28:54 -0600262 free(tree);
Julius Werner73eaec82019-05-03 17:58:07 -0700263 return NULL;
264 }
265 if (last_comp_version > FDT_SUPPORTED_VERSION) {
266 printk(BIOS_DEBUG, "Unsupported device tree version %u(>=%u)\n",
267 version, last_comp_version);
Jacob Garber698d83a2019-06-07 10:28:54 -0600268 free(tree);
Julius Werner73eaec82019-05-03 17:58:07 -0700269 return NULL;
270 }
271 if (version > FDT_SUPPORTED_VERSION)
272 printk(BIOS_DEBUG,
273 "NOTE: FDT version %u too new, should add support!\n",
274 version);
275
Patrick Rudolph666c1722018-04-03 09:57:33 +0200276 uint32_t struct_offset = be32toh(header->structure_offset);
277 uint32_t strings_offset = be32toh(header->strings_offset);
278 uint32_t reserve_offset = be32toh(header->reserve_map_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200279 uint32_t min_offset = 0;
280 min_offset = MIN(struct_offset, strings_offset);
281 min_offset = MIN(min_offset, reserve_offset);
Julius Werner23df4772019-05-17 22:50:18 -0700282 /* Assume everything up to the first non-header component is part of
283 the header and needs to be preserved. This will protect us against
284 new elements being added in the future. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200285 tree->header_size = min_offset;
286
Patrick Rudolph666c1722018-04-03 09:57:33 +0200287 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200288 uint32_t offset = reserve_offset;
289 int size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200290 struct list_node *last = &tree->reserve_map;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200291 while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) {
292 list_insert_after(&entry->list_node, last);
293 last = &entry->list_node;
294
295 offset += size;
296 }
297
Julius Werner6702b682019-05-03 18:13:53 -0700298 fdt_unflatten_node(blob, struct_offset, tree, &tree->root);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200299
300 return tree;
301}
302
303
304
305/*
Patrick Rudolph666c1722018-04-03 09:57:33 +0200306 * Functions to find the size of the device tree if it was flattened.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200307 */
308
Patrick Rudolph666c1722018-04-03 09:57:33 +0200309static void dt_flat_prop_size(struct device_tree_property *prop,
310 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200311{
Julius Werner23df4772019-05-17 22:50:18 -0700312 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200313 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700314 /* Size. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200315 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700316 /* Name offset. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200317 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700318 /* Property value. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200319 *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200320
Julius Werner23df4772019-05-17 22:50:18 -0700321 /* Property name. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200322 *strings_size += strlen(prop->prop.name) + 1;
323}
324
Patrick Rudolph666c1722018-04-03 09:57:33 +0200325static void dt_flat_node_size(struct device_tree_node *node,
326 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200327{
Julius Werner23df4772019-05-17 22:50:18 -0700328 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200329 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700330 /* Node name. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200331 *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200332
Patrick Rudolph666c1722018-04-03 09:57:33 +0200333 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200334 list_for_each(prop, node->properties, list_node)
335 dt_flat_prop_size(prop, struct_size, strings_size);
336
Patrick Rudolph666c1722018-04-03 09:57:33 +0200337 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200338 list_for_each(child, node->children, list_node)
339 dt_flat_node_size(child, struct_size, strings_size);
340
Julius Werner23df4772019-05-17 22:50:18 -0700341 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200342 *struct_size += sizeof(uint32_t);
343}
344
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200345uint32_t dt_flat_size(const struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200346{
347 uint32_t size = tree->header_size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200348 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200349 list_for_each(entry, tree->reserve_map, list_node)
350 size += sizeof(uint64_t) * 2;
351 size += sizeof(uint64_t) * 2;
352
353 uint32_t struct_size = 0;
354 uint32_t strings_size = 0;
355 dt_flat_node_size(tree->root, &struct_size, &strings_size);
356
357 size += struct_size;
Julius Werner23df4772019-05-17 22:50:18 -0700358 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200359 size += sizeof(uint32_t);
360
361 size += strings_size;
362
363 return size;
364}
365
366
367
368/*
369 * Functions to flatten a device tree.
370 */
371
Patrick Rudolph666c1722018-04-03 09:57:33 +0200372static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200373 void **map_start)
374{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200375 ((uint64_t *)*map_start)[0] = htobe64(entry->start);
376 ((uint64_t *)*map_start)[1] = htobe64(entry->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200377 *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2;
378}
379
Patrick Rudolph666c1722018-04-03 09:57:33 +0200380static void dt_flatten_prop(struct device_tree_property *prop,
381 void **struct_start, void *strings_base,
382 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200383{
384 uint8_t *dstruct = (uint8_t *)*struct_start;
385 uint8_t *dstrings = (uint8_t *)*strings_start;
386
Julius Wernera5ea3a22019-05-07 17:38:12 -0700387 be32enc(dstruct, FDT_TOKEN_PROPERTY);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200388 dstruct += sizeof(uint32_t);
389
Julius Wernera5ea3a22019-05-07 17:38:12 -0700390 be32enc(dstruct, prop->prop.size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200391 dstruct += sizeof(uint32_t);
392
393 uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base;
Julius Wernera5ea3a22019-05-07 17:38:12 -0700394 be32enc(dstruct, name_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200395 dstruct += sizeof(uint32_t);
396
397 strcpy((char *)dstrings, prop->prop.name);
398 dstrings += strlen(prop->prop.name) + 1;
399
400 memcpy(dstruct, prop->prop.data, prop->prop.size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200401 dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200402
403 *struct_start = dstruct;
404 *strings_start = dstrings;
405}
406
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200407static void dt_flatten_node(const struct device_tree_node *node,
408 void **struct_start, void *strings_base,
409 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200410{
411 uint8_t *dstruct = (uint8_t *)*struct_start;
412 uint8_t *dstrings = (uint8_t *)*strings_start;
413
Julius Wernera5ea3a22019-05-07 17:38:12 -0700414 be32enc(dstruct, FDT_TOKEN_BEGIN_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200415 dstruct += sizeof(uint32_t);
416
417 strcpy((char *)dstruct, node->name);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200418 dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200419
Patrick Rudolph666c1722018-04-03 09:57:33 +0200420 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200421 list_for_each(prop, node->properties, list_node)
422 dt_flatten_prop(prop, (void **)&dstruct, strings_base,
423 (void **)&dstrings);
424
Patrick Rudolph666c1722018-04-03 09:57:33 +0200425 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200426 list_for_each(child, node->children, list_node)
427 dt_flatten_node(child, (void **)&dstruct, strings_base,
428 (void **)&dstrings);
429
Julius Wernera5ea3a22019-05-07 17:38:12 -0700430 be32enc(dstruct, FDT_TOKEN_END_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200431 dstruct += sizeof(uint32_t);
432
433 *struct_start = dstruct;
434 *strings_start = dstrings;
435}
436
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200437void dt_flatten(const struct device_tree *tree, void *start_dest)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200438{
439 uint8_t *dest = (uint8_t *)start_dest;
440
441 memcpy(dest, tree->header, tree->header_size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200442 struct fdt_header *header = (struct fdt_header *)dest;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200443 dest += tree->header_size;
444
Patrick Rudolph666c1722018-04-03 09:57:33 +0200445 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200446 list_for_each(entry, tree->reserve_map, list_node)
447 dt_flatten_map_entry(entry, (void **)&dest);
448 ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0;
449 dest += sizeof(uint64_t) * 2;
450
451 uint32_t struct_size = 0;
452 uint32_t strings_size = 0;
453 dt_flat_node_size(tree->root, &struct_size, &strings_size);
454
455 uint8_t *struct_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200456 header->structure_offset = htobe32(dest - (uint8_t *)start_dest);
457 header->structure_size = htobe32(struct_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200458 dest += struct_size;
459
Patrick Rudolph666c1722018-04-03 09:57:33 +0200460 *((uint32_t *)dest) = htobe32(FDT_TOKEN_END);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200461 dest += sizeof(uint32_t);
462
463 uint8_t *strings_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200464 header->strings_offset = htobe32(dest - (uint8_t *)start_dest);
465 header->strings_size = htobe32(strings_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200466 dest += strings_size;
467
468 dt_flatten_node(tree->root, (void **)&struct_start, strings_start,
469 (void **)&strings_start);
470
Patrick Rudolph666c1722018-04-03 09:57:33 +0200471 header->totalsize = htobe32(dest - (uint8_t *)start_dest);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200472}
473
474
475
476/*
477 * Functions for printing a non-flattened device tree.
478 */
479
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200480static void print_node(const struct device_tree_node *node, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200481{
482 print_indent(depth);
Julius Werner23df4772019-05-17 22:50:18 -0700483 if (depth == 0) /* root node has no name, print a starting slash */
Julius Werner0d746532019-05-06 19:35:56 -0700484 printk(BIOS_DEBUG, "/");
485 printk(BIOS_DEBUG, "%s {\n", node->name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200486
Patrick Rudolph666c1722018-04-03 09:57:33 +0200487 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200488 list_for_each(prop, node->properties, list_node)
489 print_property(&prop->prop, depth + 1);
490
Julius Werner23df4772019-05-17 22:50:18 -0700491 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700492
Patrick Rudolph666c1722018-04-03 09:57:33 +0200493 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200494 list_for_each(child, node->children, list_node)
495 print_node(child, depth + 1);
Julius Werner0d746532019-05-06 19:35:56 -0700496
497 print_indent(depth);
498 printk(BIOS_DEBUG, "};\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200499}
500
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200501void dt_print_node(const struct device_tree_node *node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200502{
503 print_node(node, 0);
504}
505
506
507
508/*
509 * Functions for reading and manipulating an unflattened device tree.
510 */
511
512/*
513 * Read #address-cells and #size-cells properties from a node.
514 *
515 * @param node The device tree node to read from.
516 * @param addrcp Pointer to store #address-cells in, skipped if NULL.
517 * @param sizecp Pointer to store #size-cells in, skipped if NULL.
518 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200519void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
520 u32 *sizecp)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200521{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200522 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200523 list_for_each(prop, node->properties, list_node) {
524 if (addrcp && !strcmp("#address-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700525 *addrcp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200526 if (sizecp && !strcmp("#size-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700527 *sizecp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200528 }
529}
530
531/*
532 * Find a node from a device tree path, relative to a parent node.
533 *
534 * @param parent The node from which to start the relative path lookup.
535 * @param path An array of path component strings that will be looked
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200536 * up in order to find the node. Must be terminated with
537 * a NULL pointer. Example: {'firmware', 'coreboot', NULL}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200538 * @param addrcp Pointer that will be updated with any #address-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200539 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200540 * @param sizecp Pointer that will be updated with any #size-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200541 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200542 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
543 * @return The found/created node, or NULL.
544 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200545struct device_tree_node *dt_find_node(struct device_tree_node *parent,
546 const char **path, u32 *addrcp,
547 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200548{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200549 struct device_tree_node *node, *found = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200550
Julius Werner23df4772019-05-17 22:50:18 -0700551 /* Update #address-cells and #size-cells for this level. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200552 dt_read_cell_props(parent, addrcp, sizecp);
553
554 if (!*path)
555 return parent;
556
Julius Werner23df4772019-05-17 22:50:18 -0700557 /* Find the next node in the path, if it exists. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200558 list_for_each(node, parent->children, list_node) {
559 if (!strcmp(node->name, *path)) {
560 found = node;
561 break;
562 }
563 }
564
Julius Werner23df4772019-05-17 22:50:18 -0700565 /* Otherwise create it or return NULL. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200566 if (!found) {
567 if (!create)
568 return NULL;
569
Julius Werner9636a102019-05-03 17:36:43 -0700570 found = malloc(sizeof(*found));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200571 if (!found)
572 return NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200573 found->name = strdup(*path);
574 if (!found->name)
575 return NULL;
576
577 list_insert_after(&found->list_node, &parent->children);
578 }
579
580 return dt_find_node(found, path + 1, addrcp, sizecp, create);
581}
582
583/*
Julius Wernerf36d53c2019-05-03 18:23:34 -0700584 * Find a node in the tree from a string device tree path.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200585 *
Julius Wernerf36d53c2019-05-03 18:23:34 -0700586 * @param tree The device tree to search.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200587 * @param path A string representing a path in the device tree, with
Julius Wernerfbec63d2019-05-03 18:29:28 -0700588 * nodes separated by '/'. Example: "/firmware/coreboot"
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200589 * @param addrcp Pointer that will be updated with any #address-cells
590 * value found in the path. May be NULL to ignore.
591 * @param sizecp Pointer that will be updated with any #size-cells
592 * value found in the path. May be NULL to ignore.
593 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
594 * @return The found/created node, or NULL.
595 *
Julius Werner6d5695f2019-05-06 19:23:28 -0700596 * It is the caller responsibility to provide a path string that doesn't end
597 * with a '/' and doesn't contain any "//". If the path does not start with a
598 * '/', the first segment is interpreted as an alias. */
Julius Wernerf36d53c2019-05-03 18:23:34 -0700599struct device_tree_node *dt_find_node_by_path(struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200600 const char *path, u32 *addrcp,
601 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200602{
Julius Werner6d5695f2019-05-06 19:23:28 -0700603 char *sub_path;
604 char *duped_str;
605 struct device_tree_node *parent;
606 char *next_slash;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200607 /* Hopefully enough depth for any node. */
608 const char *path_array[15];
609 int i;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200610 struct device_tree_node *node = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200611
Julius Werner23df4772019-05-17 22:50:18 -0700612 if (path[0] == '/') { /* regular path */
613 if (path[1] == '\0') { /* special case: "/" is root node */
Julius Werner6d5695f2019-05-06 19:23:28 -0700614 dt_read_cell_props(tree->root, addrcp, sizecp);
615 return tree->root;
616 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200617
Julius Werner6d5695f2019-05-06 19:23:28 -0700618 sub_path = duped_str = strdup(&path[1]);
619 if (!sub_path)
620 return NULL;
621
622 parent = tree->root;
Julius Werner23df4772019-05-17 22:50:18 -0700623 } else { /* alias */
Julius Werner6d5695f2019-05-06 19:23:28 -0700624 char *alias;
625
626 alias = duped_str = strdup(path);
627 if (!alias)
628 return NULL;
629
630 sub_path = strchr(alias, '/');
631 if (sub_path)
632 *sub_path = '\0';
633
634 parent = dt_find_node_by_alias(tree, alias);
635 if (!parent) {
636 printk(BIOS_DEBUG,
637 "Could not find node '%s', alias '%s' does not exist\n",
638 path, alias);
639 free(duped_str);
640 return NULL;
641 }
642
643 if (!sub_path) {
Julius Werner23df4772019-05-17 22:50:18 -0700644 /* it's just the alias, no sub-path */
Julius Werner6d5695f2019-05-06 19:23:28 -0700645 free(duped_str);
646 return parent;
647 }
648
649 sub_path++;
650 }
651
652 next_slash = sub_path;
653 path_array[0] = sub_path;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200654 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200655 next_slash = strchr(next_slash, '/');
656 if (!next_slash)
657 break;
658
659 *next_slash++ = '\0';
660 path_array[i] = next_slash;
661 }
662
663 if (!next_slash) {
664 path_array[i] = NULL;
Julius Werner6d5695f2019-05-06 19:23:28 -0700665 node = dt_find_node(parent, path_array,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200666 addrcp, sizecp, create);
667 }
668
Julius Werner6d5695f2019-05-06 19:23:28 -0700669 free(duped_str);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200670 return node;
671}
672
Julius Werner6d5695f2019-05-06 19:23:28 -0700673/*
674 * Find a node from an alias
675 *
676 * @param tree The device tree.
677 * @param alias The alias name.
678 * @return The found node, or NULL.
679 */
680struct device_tree_node *dt_find_node_by_alias(struct device_tree *tree,
681 const char *alias)
682{
683 struct device_tree_node *node;
684 const char *alias_path;
685
686 node = dt_find_node_by_path(tree, "/aliases", NULL, NULL, 0);
687 if (!node)
688 return NULL;
689
690 alias_path = dt_find_string_prop(node, alias);
691 if (!alias_path)
692 return NULL;
693
694 return dt_find_node_by_path(tree, alias_path, NULL, NULL, 0);
695}
696
Julius Werner6702b682019-05-03 18:13:53 -0700697struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root,
698 uint32_t phandle)
699{
700 if (!root)
701 return NULL;
702
703 if (root->phandle == phandle)
704 return root;
705
706 struct device_tree_node *node;
707 struct device_tree_node *result;
708 list_for_each(node, root->children, list_node) {
709 result = dt_find_node_by_phandle(node, phandle);
710 if (result)
711 return result;
712 }
713
714 return NULL;
715}
716
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200717/*
718 * Check if given node is compatible.
719 *
720 * @param node The node which is to be checked for compatible property.
721 * @param compat The compatible string to match.
722 * @return 1 = compatible, 0 = not compatible.
723 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200724static int dt_check_compat_match(struct device_tree_node *node,
725 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200726{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200727 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200728
729 list_for_each(prop, node->properties, list_node) {
730 if (!strcmp("compatible", prop->prop.name)) {
731 size_t bytes = prop->prop.size;
732 const char *str = prop->prop.data;
733 while (bytes > 0) {
734 if (!strncmp(compat, str, bytes))
735 return 1;
736 size_t len = strnlen(str, bytes) + 1;
737 if (bytes <= len)
738 break;
739 str += len;
740 bytes -= len;
741 }
742 break;
743 }
744 }
745
746 return 0;
747}
748
749/*
750 * Find a node from a compatible string, in the subtree of a parent node.
751 *
752 * @param parent The parent node under which to look.
753 * @param compat The compatible string to find.
754 * @return The found node, or NULL.
755 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200756struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
757 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200758{
Julius Werner23df4772019-05-17 22:50:18 -0700759 /* Check if the parent node itself is compatible. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200760 if (dt_check_compat_match(parent, compat))
761 return parent;
762
Patrick Rudolph666c1722018-04-03 09:57:33 +0200763 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200764 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200765 struct device_tree_node *found = dt_find_compat(child, compat);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200766 if (found)
767 return found;
768 }
769
770 return NULL;
771}
772
773/*
774 * Find the next compatible child of a given parent. All children upto the
775 * child passed in by caller are ignored. If child is NULL, it considers all the
776 * children to find the first child which is compatible.
777 *
778 * @param parent The parent node under which to look.
779 * @param child The child node to start search from (exclusive). If NULL
780 * consider all children.
781 * @param compat The compatible string to find.
782 * @return The found node, or NULL.
783 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200784struct device_tree_node *
785dt_find_next_compat_child(struct device_tree_node *parent,
786 struct device_tree_node *child,
787 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200788{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200789 struct device_tree_node *next;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200790 int ignore = 0;
791
792 if (child)
793 ignore = 1;
794
795 list_for_each(next, parent->children, list_node) {
796 if (ignore) {
797 if (child == next)
798 ignore = 0;
799 continue;
800 }
801
802 if (dt_check_compat_match(next, compat))
803 return next;
804 }
805
806 return NULL;
807}
808
809/*
810 * Find a node with matching property value, in the subtree of a parent node.
811 *
812 * @param parent The parent node under which to look.
813 * @param name The property name to look for.
814 * @param data The property value to look for.
815 * @param size The property size.
816 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200817struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
818 const char *name, void *data,
819 size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200820{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200821 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200822
823 /* Check if parent itself has the required property value. */
824 list_for_each(prop, parent->properties, list_node) {
825 if (!strcmp(name, prop->prop.name)) {
826 size_t bytes = prop->prop.size;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200827 const void *prop_data = prop->prop.data;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200828 if (size != bytes)
829 break;
830 if (!memcmp(data, prop_data, size))
831 return parent;
832 break;
833 }
834 }
835
Patrick Rudolph666c1722018-04-03 09:57:33 +0200836 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200837 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200838 struct device_tree_node *found = dt_find_prop_value(child, name,
839 data, size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200840 if (found)
841 return found;
842 }
843 return NULL;
844}
845
846/*
847 * Write an arbitrary sized big-endian integer into a pointer.
848 *
849 * @param dest Pointer to the DT property data buffer to write.
Elyes HAOUAS1ec76442018-08-07 12:20:04 +0200850 * @param src The integer to write (in CPU endianness).
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200851 * @param length the length of the destination integer in bytes.
852 */
853void dt_write_int(u8 *dest, u64 src, size_t length)
854{
855 while (length--) {
856 dest[length] = (u8)src;
857 src >>= 8;
858 }
859}
860
861/*
Patrick Rudolph5ccc7312018-05-30 15:05:28 +0200862 * Delete a property by name in a given node if it exists.
863 *
864 * @param node The device tree node to operate on.
865 * @param name The name of the property to delete.
866 */
867void dt_delete_prop(struct device_tree_node *node, const char *name)
868{
869 struct device_tree_property *prop;
870
871 list_for_each(prop, node->properties, list_node) {
872 if (!strcmp(prop->prop.name, name)) {
873 list_remove(&prop->list_node);
874 return;
875 }
876 }
877}
878
879/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200880 * Add an arbitrary property to a node, or update it if it already exists.
881 *
882 * @param node The device tree node to add to.
883 * @param name The name of the new property.
884 * @param data The raw data blob to be stored in the property.
885 * @param size The size of data in bytes.
886 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200887void dt_add_bin_prop(struct device_tree_node *node, const char *name,
Julius Werner0e9116f2019-05-13 17:30:31 -0700888 void *data, size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200889{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200890 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200891
892 list_for_each(prop, node->properties, list_node) {
893 if (!strcmp(prop->prop.name, name)) {
894 prop->prop.data = data;
895 prop->prop.size = size;
896 return;
897 }
898 }
899
Julius Werner9636a102019-05-03 17:36:43 -0700900 prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200901 list_insert_after(&prop->list_node, &node->properties);
902 prop->prop.name = name;
903 prop->prop.data = data;
904 prop->prop.size = size;
905}
906
907/*
908 * Find given string property in a node and return its content.
909 *
910 * @param node The device tree node to search.
911 * @param name The name of the property.
912 * @return The found string, or NULL.
913 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200914const char *dt_find_string_prop(const struct device_tree_node *node,
915 const char *name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200916{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200917 const void *content;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200918 size_t size;
919
920 dt_find_bin_prop(node, name, &content, &size);
921
922 return content;
923}
924
925/*
926 * Find given property in a node.
927 *
928 * @param node The device tree node to search.
929 * @param name The name of the property.
930 * @param data Pointer to return raw data blob in the property.
931 * @param size Pointer to return the size of data in bytes.
932 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200933void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
934 const void **data, size_t *size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200935{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200936 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200937
938 *data = NULL;
939 *size = 0;
940
941 list_for_each(prop, node->properties, list_node) {
942 if (!strcmp(prop->prop.name, name)) {
943 *data = prop->prop.data;
944 *size = prop->prop.size;
945 return;
946 }
947 }
948}
949
950/*
951 * Add a string property to a node, or update it if it already exists.
952 *
953 * @param node The device tree node to add to.
954 * @param name The name of the new property.
955 * @param str The zero-terminated string to be stored in the property.
956 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200957void dt_add_string_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200958 const char *str)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200959{
Julius Werner0e9116f2019-05-13 17:30:31 -0700960 dt_add_bin_prop(node, name, (char *)str, strlen(str) + 1);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200961}
962
963/*
964 * Add a 32-bit integer property to a node, or update it if it already exists.
965 *
966 * @param node The device tree node to add to.
967 * @param name The name of the new property.
968 * @param val The integer to be stored in the property.
969 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200970void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200971{
Julius Werner9636a102019-05-03 17:36:43 -0700972 u32 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200973 *val_ptr = htobe32(val);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200974 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
975}
976
977/*
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200978 * Add a 64-bit integer property to a node, or update it if it already exists.
979 *
980 * @param node The device tree node to add to.
981 * @param name The name of the new property.
982 * @param val The integer to be stored in the property.
983 */
984void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
985{
Julius Werner9636a102019-05-03 17:36:43 -0700986 u64 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200987 *val_ptr = htobe64(val);
988 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
989}
990
991/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200992 * Add a 'reg' address list property to a node, or update it if it exists.
993 *
994 * @param node The device tree node to add to.
995 * @param addrs Array of address values to be stored in the property.
996 * @param sizes Array of corresponding size values to 'addrs'.
997 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
998 * @param addr_cells Value of #address-cells property valid for this node.
999 * @param size_cells Value of #size-cells property valid for this node.
1000 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001001void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001002 int count, u32 addr_cells, u32 size_cells)
1003{
1004 int i;
1005 size_t length = (addr_cells + size_cells) * sizeof(u32) * count;
Julius Werner9636a102019-05-03 17:36:43 -07001006 u8 *data = xmalloc(length);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001007 u8 *cur = data;
1008
1009 for (i = 0; i < count; i++) {
1010 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
1011 cur += addr_cells * sizeof(u32);
1012 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
1013 cur += size_cells * sizeof(u32);
1014 }
1015
1016 dt_add_bin_prop(node, "reg", data, length);
1017}
1018
1019/*
1020 * Fixups to apply to a kernel's device tree before booting it.
1021 */
1022
Patrick Rudolph666c1722018-04-03 09:57:33 +02001023struct list_node device_tree_fixups;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001024
Patrick Rudolph666c1722018-04-03 09:57:33 +02001025int dt_apply_fixups(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001026{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001027 struct device_tree_fixup *fixup;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001028 list_for_each(fixup, device_tree_fixups, list_node) {
1029 assert(fixup->fixup);
1030 if (fixup->fixup(fixup, tree))
1031 return 1;
1032 }
1033 return 0;
1034}
1035
Patrick Rudolph666c1722018-04-03 09:57:33 +02001036int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001037 void *data, size_t data_size, int create)
1038{
1039 char *path_copy, *prop_name;
Patrick Rudolph666c1722018-04-03 09:57:33 +02001040 struct device_tree_node *dt_node;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001041
1042 path_copy = strdup(path);
1043
1044 if (!path_copy) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001045 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
1046 path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001047 return 1;
1048 }
1049
1050 prop_name = strrchr(path_copy, '/');
1051 if (!prop_name) {
Patrick Rudolph679d6242018-07-11 13:53:04 +02001052 free(path_copy);
Patrick Rudolph666c1722018-04-03 09:57:33 +02001053 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001054 return 1;
1055 }
1056
1057 *prop_name++ = '\0'; /* Separate path from the property name. */
1058
Julius Wernerf36d53c2019-05-03 18:23:34 -07001059 dt_node = dt_find_node_by_path(tree, path_copy, NULL,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001060 NULL, create);
1061
1062 if (!dt_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001063 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001064 create ? "create" : "find", path_copy);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001065 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001066 return 1;
1067 }
1068
1069 dt_add_bin_prop(dt_node, prop_name, data, data_size);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001070 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001071
1072 return 0;
1073}
1074
1075/*
1076 * Prepare the /reserved-memory/ node.
1077 *
1078 * Technically, this can be called more than one time, to init and/or retrieve
1079 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
1080 *
1081 * @tree: Device tree to add/retrieve from.
1082 * @return: The /reserved-memory/ node (or NULL, if error).
1083 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001084struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001085{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001086 struct device_tree_node *reserved;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001087 u32 addr = 0, size = 0;
1088
Julius Wernerfbec63d2019-05-03 18:29:28 -07001089 reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001090 &size, 1);
1091 if (!reserved)
1092 return NULL;
1093
Julius Werner23df4772019-05-17 22:50:18 -07001094 /* Binding doc says this should have the same #{address,size}-cells as
1095 the root. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001096 dt_add_u32_prop(reserved, "#address-cells", addr);
1097 dt_add_u32_prop(reserved, "#size-cells", size);
Julius Werner23df4772019-05-17 22:50:18 -07001098 /* Binding doc says this should be empty (1:1 mapping from root). */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001099 dt_add_bin_prop(reserved, "ranges", NULL, 0);
1100
1101 return reserved;
1102}
Julius Werner735ddc92019-05-07 17:05:28 -07001103
1104/*
1105 * Increment a single phandle in prop at a given offset by a given adjustment.
1106 *
1107 * @param prop Property whose phandle should be adjusted.
1108 * @param adjustment Value that should be added to the existing phandle.
1109 * @param offset Byte offset of the phandle in the property data.
1110 *
1111 * @return New phandle value, or 0 on error.
1112 */
1113static uint32_t dt_adjust_phandle(struct device_tree_property *prop,
1114 uint32_t adjustment, uint32_t offset)
1115{
1116 if (offset + 4 > prop->prop.size)
1117 return 0;
1118
1119 uint32_t phandle = be32dec(prop->prop.data + offset);
1120 if (phandle == 0 ||
1121 phandle == FDT_PHANDLE_ILLEGAL ||
1122 phandle == 0xffffffff)
1123 return 0;
1124
1125 phandle += adjustment;
1126 if (phandle >= FDT_PHANDLE_ILLEGAL)
1127 return 0;
1128
1129 be32enc(prop->prop.data + offset, phandle);
1130 return phandle;
1131}
1132
1133/*
1134 * Adjust all phandles in subtree by adding a new base offset.
1135 *
1136 * @param node Root node of the subtree to work on.
1137 * @param base New phandle base to be added to all phandles.
1138 *
1139 * @return New highest phandle in the subtree, or 0 on error.
1140 */
1141static uint32_t dt_adjust_all_phandles(struct device_tree_node *node,
1142 uint32_t base)
1143{
Julius Werner23df4772019-05-17 22:50:18 -07001144 uint32_t new_max = MAX(base, 1); /* make sure we don't return 0 */
Julius Werner735ddc92019-05-07 17:05:28 -07001145 struct device_tree_property *prop;
1146 struct device_tree_node *child;
1147
1148 if (!node)
1149 return new_max;
1150
1151 list_for_each(prop, node->properties, list_node)
1152 if (dt_prop_is_phandle(prop)) {
1153 node->phandle = dt_adjust_phandle(prop, base, 0);
1154 if (!node->phandle)
1155 return 0;
1156 new_max = MAX(new_max, node->phandle);
Julius Werner23df4772019-05-17 22:50:18 -07001157 } /* no break -- can have more than one phandle prop */
Julius Werner735ddc92019-05-07 17:05:28 -07001158
1159 list_for_each(child, node->children, list_node)
1160 new_max = MAX(new_max, dt_adjust_all_phandles(child, base));
1161
1162 return new_max;
1163}
1164
1165/*
1166 * Apply a /__local_fixup__ subtree to the corresponding overlay subtree.
1167 *
1168 * @param node Root node of the overlay subtree to fix up.
1169 * @param node Root node of the /__local_fixup__ subtree.
1170 * @param base Adjustment that was added to phandles in the overlay.
1171 *
1172 * @return 0 on success, -1 on error.
1173 */
1174static int dt_fixup_locals(struct device_tree_node *node,
1175 struct device_tree_node *fixup, uint32_t base)
1176{
1177 struct device_tree_property *prop;
1178 struct device_tree_property *fixup_prop;
1179 struct device_tree_node *child;
1180 struct device_tree_node *fixup_child;
1181 int i;
1182
Julius Werner23df4772019-05-17 22:50:18 -07001183 /*
1184 * For local fixups the /__local_fixup__ subtree contains the same node
1185 * hierarchy as the main tree we're fixing up. Each property contains
1186 * the fixup offsets for the respective property in the main tree. For
1187 * each property in the fixup node, find the corresponding property in
1188 * the base node and apply fixups to all offsets it specifies.
1189 */
Julius Werner735ddc92019-05-07 17:05:28 -07001190 list_for_each(fixup_prop, fixup->properties, list_node) {
1191 struct device_tree_property *base_prop = NULL;
1192 list_for_each(prop, node->properties, list_node)
1193 if (!strcmp(prop->prop.name, fixup_prop->prop.name)) {
1194 base_prop = prop;
1195 break;
1196 }
1197
Julius Werner23df4772019-05-17 22:50:18 -07001198 /* We should always find a corresponding base prop for a fixup,
1199 and fixup props contain a list of 32-bit fixup offsets. */
Julius Werner735ddc92019-05-07 17:05:28 -07001200 if (!base_prop || fixup_prop->prop.size % sizeof(uint32_t))
1201 return -1;
1202
1203 for (i = 0; i < fixup_prop->prop.size; i += sizeof(uint32_t))
1204 if (!dt_adjust_phandle(base_prop, base, be32dec(
1205 fixup_prop->prop.data + i)))
1206 return -1;
1207 }
1208
Julius Werner23df4772019-05-17 22:50:18 -07001209 /* Now recursively descend both the base tree and the /__local_fixups__
1210 subtree in sync to apply all fixups. */
Julius Werner735ddc92019-05-07 17:05:28 -07001211 list_for_each(fixup_child, fixup->children, list_node) {
1212 struct device_tree_node *base_child = NULL;
1213 list_for_each(child, node->children, list_node)
1214 if (!strcmp(child->name, fixup_child->name)) {
1215 base_child = child;
1216 break;
1217 }
1218
Julius Werner23df4772019-05-17 22:50:18 -07001219 /* All fixup nodes should have a corresponding base node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001220 if (!base_child)
1221 return -1;
1222
1223 if (dt_fixup_locals(base_child, fixup_child, base) < 0)
1224 return -1;
1225 }
1226
1227 return 0;
1228}
1229
1230/*
1231 * Update all /__symbols__ properties in an overlay that start with
1232 * "/fragment@X/__overlay__" with corresponding path prefix in the base tree.
1233 *
1234 * @param symbols /__symbols__ done to update.
1235 * @param fragment /fragment@X node that references to should be updated.
1236 * @param base_path Path of base tree node that the fragment overlaid.
1237 */
1238static void dt_fix_symbols(struct device_tree_node *symbols,
1239 struct device_tree_node *fragment,
1240 const char *base_path)
1241{
1242 struct device_tree_property *prop;
Julius Werner23df4772019-05-17 22:50:18 -07001243 char buf[512]; /* Should be enough for maximum DT path length? */
1244 char node_path[64]; /* easily enough for /fragment@XXXX/__overlay__ */
Julius Werner735ddc92019-05-07 17:05:28 -07001245
Julius Werner23df4772019-05-17 22:50:18 -07001246 if (!symbols) /* If the overlay has no /__symbols__ node, we're done! */
Julius Werner735ddc92019-05-07 17:05:28 -07001247 return;
1248
1249 int len = snprintf(node_path, sizeof(node_path), "/%s/__overlay__",
1250 fragment->name);
1251
1252 list_for_each(prop, symbols->properties, list_node)
1253 if (!strncmp(prop->prop.data, node_path, len)) {
1254 prop->prop.size = snprintf(buf, sizeof(buf), "%s%s",
1255 base_path, (char *)prop->prop.data + len) + 1;
1256 free(prop->prop.data);
1257 prop->prop.data = strdup(buf);
1258 }
1259}
1260
1261/*
1262 * Fix up overlay according to a property in /__fixup__. If the fixed property
1263 * is a /fragment@X:target, also update /__symbols__ references to fragment.
1264 *
1265 * @params overlay Overlay to fix up.
1266 * @params fixup /__fixup__ property.
1267 * @params phandle phandle value to insert where the fixup points to.
1268 * @params base_path Path to the base DT node that the fixup points to.
1269 * @params overlay_symbols /__symbols__ node of the overlay.
1270 *
1271 * @return 0 on success, -1 on error.
1272 */
1273static int dt_fixup_external(struct device_tree *overlay,
1274 struct device_tree_property *fixup,
1275 uint32_t phandle, const char *base_path,
1276 struct device_tree_node *overlay_symbols)
1277{
1278 struct device_tree_property *prop;
1279
Julius Werner23df4772019-05-17 22:50:18 -07001280 /* External fixup properties are encoded as "<path>:<prop>:<offset>". */
Julius Werner735ddc92019-05-07 17:05:28 -07001281 char *entry = fixup->prop.data;
1282 while ((void *)entry < fixup->prop.data + fixup->prop.size) {
Julius Werner23df4772019-05-17 22:50:18 -07001283 /* okay to destroy fixup property value, won't need it again */
Julius Werner735ddc92019-05-07 17:05:28 -07001284 char *node_path = entry;
1285 entry = strchr(node_path, ':');
1286 if (!entry)
1287 return -1;
1288 *entry++ = '\0';
1289
1290 char *prop_name = entry;
1291 entry = strchr(prop_name, ':');
1292 if (!entry)
1293 return -1;
1294 *entry++ = '\0';
1295
1296 struct device_tree_node *ovl_node = dt_find_node_by_path(
1297 overlay, node_path, NULL, NULL, 0);
1298 if (!ovl_node || !isdigit(*entry))
1299 return -1;
1300
1301 struct device_tree_property *ovl_prop = NULL;
1302 list_for_each(prop, ovl_node->properties, list_node)
1303 if (!strcmp(prop->prop.name, prop_name)) {
1304 ovl_prop = prop;
1305 break;
1306 }
1307
Julius Werner23df4772019-05-17 22:50:18 -07001308 /* Move entry to first char after number, must be a '\0'. */
Julius Werner735ddc92019-05-07 17:05:28 -07001309 uint32_t offset = skip_atoi(&entry);
1310 if (!ovl_prop || offset + 4 > ovl_prop->prop.size || entry[0])
1311 return -1;
Julius Werner23df4772019-05-17 22:50:18 -07001312 entry++; /* jump over '\0' to potential next fixup */
Julius Werner735ddc92019-05-07 17:05:28 -07001313
1314 be32enc(ovl_prop->prop.data + offset, phandle);
1315
Julius Werner23df4772019-05-17 22:50:18 -07001316 /* If this is a /fragment@X:target property, update references
1317 to this fragment in the overlay __symbols__ now. */
Julius Werner735ddc92019-05-07 17:05:28 -07001318 if (offset == 0 && !strcmp(prop_name, "target") &&
Julius Werner23df4772019-05-17 22:50:18 -07001319 !strchr(node_path + 1, '/')) /* only toplevel nodes */
Julius Werner735ddc92019-05-07 17:05:28 -07001320 dt_fix_symbols(overlay_symbols, ovl_node, base_path);
1321 }
1322
1323 return 0;
1324}
1325
1326/*
1327 * Apply all /__fixup__ properties in the overlay. This will destroy the
1328 * property data in /__fixup__ and it should not be accessed again.
1329 *
1330 * @params tree Base device tree that the overlay updates.
1331 * @params symbols /__symbols__ node of the base device tree.
1332 * @params overlay Overlay to fix up.
1333 * @params fixups /__fixup__ node in the overlay.
1334 * @params overlay_symbols /__symbols__ node of the overlay.
1335 *
1336 * @return 0 on success, -1 on error.
1337 */
1338static int dt_fixup_all_externals(struct device_tree *tree,
1339 struct device_tree_node *symbols,
1340 struct device_tree *overlay,
1341 struct device_tree_node *fixups,
1342 struct device_tree_node *overlay_symbols)
1343{
1344 struct device_tree_property *fix;
1345
Julius Werner23df4772019-05-17 22:50:18 -07001346 /* If we have any external fixups, base tree must have /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001347 if (!symbols)
1348 return -1;
1349
Julius Werner23df4772019-05-17 22:50:18 -07001350 /*
1351 * Unlike /__local_fixups__, /__fixups__ is not a whole subtree that
1352 * mirrors the node hierarchy. It's just a directory of fixup properties
1353 * that each directly contain all information necessary to apply them.
1354 */
Julius Werner735ddc92019-05-07 17:05:28 -07001355 list_for_each(fix, fixups->properties, list_node) {
Julius Werner23df4772019-05-17 22:50:18 -07001356 /* The name of a fixup property is the label of the node we want
1357 a property to phandle-reference. Look up in /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001358 const char *path = dt_find_string_prop(symbols, fix->prop.name);
1359 if (!path)
1360 return -1;
1361
Julius Werner23df4772019-05-17 22:50:18 -07001362 /* Find node the label pointed to to figure out its phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001363 struct device_tree_node *node = dt_find_node_by_path(tree, path,
1364 NULL, NULL, 0);
1365 if (!node)
1366 return -1;
1367
Julius Werner23df4772019-05-17 22:50:18 -07001368 /* Write into the overlay property(s) pointing to that node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001369 if (dt_fixup_external(overlay, fix, node->phandle,
1370 path, overlay_symbols) < 0)
1371 return -1;
1372 }
1373
1374 return 0;
1375}
1376
1377/*
1378 * Copy all nodes and properties from one DT subtree into another. This is a
1379 * shallow copy so both trees will point to the same property data afterwards.
1380 *
1381 * @params dst Destination subtree to copy into.
1382 * @params src Source subtree to copy from.
1383 * @params upd 1 to overwrite same-name properties, 0 to discard them.
1384 */
1385static void dt_copy_subtree(struct device_tree_node *dst,
1386 struct device_tree_node *src, int upd)
1387{
1388 struct device_tree_property *prop;
1389 struct device_tree_property *src_prop;
1390 list_for_each(src_prop, src->properties, list_node) {
1391 if (dt_prop_is_phandle(src_prop) ||
1392 !strcmp(src_prop->prop.name, "name")) {
1393 printk(BIOS_DEBUG,
1394 "WARNING: ignoring illegal overlay prop '%s'\n",
1395 src_prop->prop.name);
1396 continue;
1397 }
1398
1399 struct device_tree_property *dst_prop = NULL;
1400 list_for_each(prop, dst->properties, list_node)
1401 if (!strcmp(prop->prop.name, src_prop->prop.name)) {
1402 dst_prop = prop;
1403 break;
1404 }
1405
1406 if (dst_prop) {
1407 if (!upd) {
1408 printk(BIOS_DEBUG,
1409 "WARNING: ignoring prop update '%s'\n",
1410 src_prop->prop.name);
1411 continue;
1412 }
1413 } else {
1414 dst_prop = xzalloc(sizeof(*dst_prop));
1415 list_insert_after(&dst_prop->list_node,
1416 &dst->properties);
1417 }
1418
1419 dst_prop->prop = src_prop->prop;
1420 }
1421
1422 struct device_tree_node *node;
1423 struct device_tree_node *src_node;
1424 list_for_each(src_node, src->children, list_node) {
1425 struct device_tree_node *dst_node = NULL;
1426 list_for_each(node, dst->children, list_node)
1427 if (!strcmp(node->name, src_node->name)) {
1428 dst_node = node;
1429 break;
1430 }
1431
1432 if (!dst_node) {
1433 dst_node = xzalloc(sizeof(*dst_node));
1434 *dst_node = *src_node;
1435 list_insert_after(&dst_node->list_node, &dst->children);
1436 } else {
1437 dt_copy_subtree(dst_node, src_node, upd);
1438 }
1439 }
1440}
1441
1442/*
1443 * Apply an overlay /fragment@X node to a base device tree.
1444 *
1445 * @param tree Base device tree.
1446 * @param fragment /fragment@X node.
1447 * @params overlay_symbols /__symbols__ node of the overlay.
1448 *
1449 * @return 0 on success, -1 on error.
1450 */
1451static int dt_import_fragment(struct device_tree *tree,
1452 struct device_tree_node *fragment,
1453 struct device_tree_node *overlay_symbols)
1454{
Julius Werner23df4772019-05-17 22:50:18 -07001455 /* The actual overlaid nodes/props are in an __overlay__ child node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001456 static const char *overlay_path[] = { "__overlay__", NULL };
1457 struct device_tree_node *overlay = dt_find_node(fragment, overlay_path,
1458 NULL, NULL, 0);
1459
Julius Werner23df4772019-05-17 22:50:18 -07001460 /* If it doesn't have an __overlay__ child, it's not a fragment. */
Julius Werner735ddc92019-05-07 17:05:28 -07001461 if (!overlay)
1462 return 0;
1463
Julius Werner23df4772019-05-17 22:50:18 -07001464 /* Target node of the fragment can be given by path or by phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001465 struct device_tree_property *prop;
1466 struct device_tree_property *phandle = NULL;
1467 struct device_tree_property *path = NULL;
1468 list_for_each(prop, fragment->properties, list_node) {
1469 if (!strcmp(prop->prop.name, "target")) {
1470 phandle = prop;
Julius Werner23df4772019-05-17 22:50:18 -07001471 break; /* phandle target has priority, stop looking */
Julius Werner735ddc92019-05-07 17:05:28 -07001472 }
1473 if (!strcmp(prop->prop.name, "target-path"))
1474 path = prop;
1475 }
1476
1477 struct device_tree_node *target = NULL;
1478 if (phandle) {
1479 if (phandle->prop.size != sizeof(uint32_t))
1480 return -1;
1481 target = dt_find_node_by_phandle(tree->root,
1482 be32dec(phandle->prop.data));
Julius Werner23df4772019-05-17 22:50:18 -07001483 /* Symbols already updated as part of dt_fixup_external(). */
Julius Werner735ddc92019-05-07 17:05:28 -07001484 } else if (path) {
1485 target = dt_find_node_by_path(tree, path->prop.data,
1486 NULL, NULL, 0);
1487 dt_fix_symbols(overlay_symbols, fragment, path->prop.data);
1488 }
1489 if (!target)
1490 return -1;
1491
1492 dt_copy_subtree(target, overlay, 1);
1493 return 0;
1494}
1495
1496/*
1497 * Apply a device tree overlay to a base device tree. This will
1498 * destroy/incorporate the overlay data, so it should not be freed or reused.
1499 * See dtc.git/Documentation/dt-object-internal.txt for overlay format details.
1500 *
1501 * @param tree Unflattened base device tree to add the overlay into.
1502 * @param overlay Unflattened overlay device tree to apply to the base.
1503 *
1504 * @return 0 on success, -1 on error.
1505 */
1506int dt_apply_overlay(struct device_tree *tree, struct device_tree *overlay)
1507{
Julius Werner23df4772019-05-17 22:50:18 -07001508 /*
1509 * First, we need to make sure phandles inside the overlay don't clash
1510 * with those in the base tree. We just define the highest phandle value
1511 * in the base tree as the "phandle offset" for this overlay and
1512 * increment all phandles in it by that value.
1513 */
Julius Werner735ddc92019-05-07 17:05:28 -07001514 uint32_t phandle_base = tree->max_phandle;
1515 uint32_t new_max = dt_adjust_all_phandles(overlay->root, phandle_base);
1516 if (!new_max) {
1517 printk(BIOS_DEBUG, "ERROR: invalid phandles in overlay\n");
1518 return -1;
1519 }
1520 tree->max_phandle = new_max;
1521
Julius Werner23df4772019-05-17 22:50:18 -07001522 /* Now that we changed phandles in the overlay, we need to update any
1523 nodes referring to them. Those are listed in /__local_fixups__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001524 struct device_tree_node *local_fixups = dt_find_node_by_path(overlay,
1525 "/__local_fixups__", NULL, NULL, 0);
1526 if (local_fixups && dt_fixup_locals(overlay->root, local_fixups,
1527 phandle_base) < 0) {
1528 printk(BIOS_DEBUG, "ERROR: invalid local fixups in overlay\n");
1529 return -1;
1530 }
1531
Julius Werner23df4772019-05-17 22:50:18 -07001532 /*
1533 * Besides local phandle references (from nodes within the overlay to
1534 * other nodes within the overlay), the overlay may also contain phandle
1535 * references to the base tree. These are stored with invalid values and
1536 * must be updated now. /__symbols__ contains a list of all labels in
1537 * the base tree, and /__fixups__ describes all nodes in the overlay
1538 * that contain external phandle references.
1539 * We also take this opportunity to update all /fragment@X/__overlay__/
1540 * prefixes in the overlay's /__symbols__ node to the correct path that
1541 * the fragment will be placed in later, since this is the only step
1542 * where we have all necessary information for that easily available.
1543 */
Julius Werner735ddc92019-05-07 17:05:28 -07001544 struct device_tree_node *symbols = dt_find_node_by_path(tree,
1545 "/__symbols__", NULL, NULL, 0);
1546 struct device_tree_node *fixups = dt_find_node_by_path(overlay,
1547 "/__fixups__", NULL, NULL, 0);
1548 struct device_tree_node *overlay_symbols = dt_find_node_by_path(overlay,
1549 "/__symbols__", NULL, NULL, 0);
1550 if (fixups && dt_fixup_all_externals(tree, symbols, overlay,
1551 fixups, overlay_symbols) < 0) {
1552 printk(BIOS_DEBUG,
1553 "ERROR: cannot match external fixups from overlay\n");
1554 return -1;
1555 }
1556
Julius Werner23df4772019-05-17 22:50:18 -07001557 /* After all this fixing up, we can finally merge overlay into the tree
1558 (one fragment at a time, because for some reason it's split up). */
Julius Werner735ddc92019-05-07 17:05:28 -07001559 struct device_tree_node *fragment;
1560 list_for_each(fragment, overlay->root->children, list_node)
1561 if (dt_import_fragment(tree, fragment, overlay_symbols) < 0) {
1562 printk(BIOS_DEBUG, "ERROR: bad DT fragment '%s'\n",
1563 fragment->name);
1564 return -1;
1565 }
1566
Julius Werner23df4772019-05-17 22:50:18 -07001567 /*
1568 * We need to also update /__symbols__ to include labels from this
1569 * overlay, in case we want to load further overlays with external
1570 * phandle references to it. If the base tree already has a /__symbols__
1571 * we merge them together, otherwise we just insert the overlay's
1572 * /__symbols__ node into the base tree root.
1573 */
Julius Werner735ddc92019-05-07 17:05:28 -07001574 if (overlay_symbols) {
1575 if (symbols)
1576 dt_copy_subtree(symbols, overlay_symbols, 0);
1577 else
1578 list_insert_after(&overlay_symbols->list_node,
1579 &tree->root->children);
1580 }
1581
1582 return 0;
1583}