blob: 2124edd582a643ddb83d4a3ee9201f72d1e63c17 [file] [log] [blame]
Patrick Georgiafd4c872020-05-05 23:43:18 +02001/* Taken from depthcharge: src/base/device_tree.c */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02002/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02003 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but without any warranty; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <assert.h>
Julius Werner9636a102019-05-03 17:36:43 -070015#include <commonlib/stdlib.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +020016#include <console/console.h>
Joel Kitching393c71c2019-06-16 16:09:42 +080017#include <ctype.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +020018#include <device_tree.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020019#include <endian.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020020#include <stdint.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +020021#include <string.h>
22#include <stddef.h>
23#include <stdlib.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020024
25/*
26 * Functions for picking apart flattened trees.
27 */
28
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020029int fdt_next_property(const void *blob, uint32_t offset,
30 struct fdt_property *prop)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020031{
Patrick Rudolph666c1722018-04-03 09:57:33 +020032 struct fdt_header *header = (struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020033 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
34
35 int index = 0;
Patrick Rudolph666c1722018-04-03 09:57:33 +020036 if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020037 return 0;
38
Patrick Rudolph666c1722018-04-03 09:57:33 +020039 uint32_t size = be32toh(ptr[index++]);
40 uint32_t name_offset = be32toh(ptr[index++]);
41 name_offset += be32toh(header->strings_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020042
43 if (prop) {
44 prop->name = (char *)((uint8_t *)blob + name_offset);
45 prop->data = &ptr[index];
46 prop->size = size;
47 }
48
Patrick Rudolph666c1722018-04-03 09:57:33 +020049 index += DIV_ROUND_UP(size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020050
Patrick Rudolph666c1722018-04-03 09:57:33 +020051 return index * sizeof(uint32_t);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020052}
53
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020054int fdt_node_name(const void *blob, uint32_t offset, const char **name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020055{
56 uint8_t *ptr = ((uint8_t *)blob) + offset;
Julius Wernera5ea3a22019-05-07 17:38:12 -070057 if (be32dec(ptr) != FDT_TOKEN_BEGIN_NODE)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020058 return 0;
59
60 ptr += 4;
61 if (name)
62 *name = (char *)ptr;
Patrick Rudolph666c1722018-04-03 09:57:33 +020063 return ALIGN_UP(strlen((char *)ptr) + 1, sizeof(uint32_t)) + 4;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020064}
65
Julius Werner6702b682019-05-03 18:13:53 -070066static int dt_prop_is_phandle(struct device_tree_property *prop)
67{
68 return !(strcmp("phandle", prop->prop.name) &&
69 strcmp("linux,phandle", prop->prop.name));
70}
71
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020072
73
74/*
75 * Functions for printing flattened trees.
76 */
77
78static void print_indent(int depth)
79{
Julius Werner0d746532019-05-06 19:35:56 -070080 printk(BIOS_DEBUG, "%*s", depth * 8, "");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020081}
82
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020083static void print_property(const struct fdt_property *prop, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020084{
Julius Werner0d746532019-05-06 19:35:56 -070085 int is_string = prop->size > 0 &&
86 ((char *)prop->data)[prop->size - 1] == '\0';
87
88 if (is_string)
89 for (const char *c = prop->data; *c != '\0'; c++)
90 if (!isprint(*c))
91 is_string = 0;
92
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020093 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -070094 if (is_string) {
95 printk(BIOS_DEBUG, "%s = \"%s\";\n",
96 prop->name, (const char *)prop->data);
97 } else {
98 printk(BIOS_DEBUG, "%s = < ", prop->name);
99 for (int i = 0; i < MIN(128, prop->size); i += 4) {
100 uint32_t val = 0;
101 for (int j = 0; j < MIN(4, prop->size - i); j++)
102 val |= ((uint8_t *)prop->data)[i + j] <<
103 (24 - j * 8);
104 printk(BIOS_DEBUG, "%#.2x ", val);
105 }
106 if (prop->size > 128)
107 printk(BIOS_DEBUG, "...");
108 printk(BIOS_DEBUG, ">;\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200109 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200110}
111
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200112static int print_flat_node(const void *blob, uint32_t start_offset, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200113{
114 int offset = start_offset;
115 const char *name;
116 int size;
117
118 size = fdt_node_name(blob, offset, &name);
119 if (!size)
120 return 0;
121 offset += size;
122
123 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -0700124 printk(BIOS_DEBUG, "%s {\n", name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200125
Patrick Rudolph666c1722018-04-03 09:57:33 +0200126 struct fdt_property prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200127 while ((size = fdt_next_property(blob, offset, &prop))) {
128 print_property(&prop, depth + 1);
129
130 offset += size;
131 }
132
Julius Werner23df4772019-05-17 22:50:18 -0700133 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700134
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200135 while ((size = print_flat_node(blob, offset, depth + 1)))
136 offset += size;
137
Julius Werner0d746532019-05-06 19:35:56 -0700138 print_indent(depth);
139 printk(BIOS_DEBUG, "}\n");
140
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200141 return offset - start_offset + sizeof(uint32_t);
142}
143
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200144void fdt_print_node(const void *blob, uint32_t offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200145{
146 print_flat_node(blob, offset, 0);
147}
148
149
150
151/*
152 * A utility function to skip past nodes in flattened trees.
153 */
154
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200155int fdt_skip_node(const void *blob, uint32_t start_offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200156{
157 int offset = start_offset;
158 int size;
159
160 const char *name;
161 size = fdt_node_name(blob, offset, &name);
162 if (!size)
163 return 0;
164 offset += size;
165
166 while ((size = fdt_next_property(blob, offset, NULL)))
167 offset += size;
168
169 while ((size = fdt_skip_node(blob, offset)))
170 offset += size;
171
172 return offset - start_offset + sizeof(uint32_t);
173}
174
175
176
177/*
178 * Functions to turn a flattened tree into an unflattened one.
179 */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200180
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200181static int fdt_unflatten_node(const void *blob, uint32_t start_offset,
Julius Werner6702b682019-05-03 18:13:53 -0700182 struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200183 struct device_tree_node **new_node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200184{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200185 struct list_node *last;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200186 int offset = start_offset;
187 const char *name;
188 int size;
189
190 size = fdt_node_name(blob, offset, &name);
191 if (!size)
192 return 0;
193 offset += size;
194
Julius Werner9636a102019-05-03 17:36:43 -0700195 struct device_tree_node *node = xzalloc(sizeof(*node));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200196 *new_node = node;
197 node->name = name;
198
Patrick Rudolph666c1722018-04-03 09:57:33 +0200199 struct fdt_property fprop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200200 last = &node->properties;
201 while ((size = fdt_next_property(blob, offset, &fprop))) {
Julius Werner9636a102019-05-03 17:36:43 -0700202 struct device_tree_property *prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200203 prop->prop = fprop;
204
Julius Werner6702b682019-05-03 18:13:53 -0700205 if (dt_prop_is_phandle(prop)) {
206 node->phandle = be32dec(prop->prop.data);
207 if (node->phandle > tree->max_phandle)
208 tree->max_phandle = node->phandle;
209 }
210
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200211 list_insert_after(&prop->list_node, last);
212 last = &prop->list_node;
213
214 offset += size;
215 }
216
Patrick Rudolph666c1722018-04-03 09:57:33 +0200217 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200218 last = &node->children;
Julius Werner6702b682019-05-03 18:13:53 -0700219 while ((size = fdt_unflatten_node(blob, offset, tree, &child))) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200220 list_insert_after(&child->list_node, last);
221 last = &child->list_node;
222
223 offset += size;
224 }
225
226 return offset - start_offset + sizeof(uint32_t);
227}
228
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200229static int fdt_unflatten_map_entry(const void *blob, uint32_t offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200230 struct device_tree_reserve_map_entry **new)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200231{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200232 const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset);
233 const uint64_t start = be64toh(ptr[0]);
234 const uint64_t size = be64toh(ptr[1]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200235
236 if (!size)
237 return 0;
238
Julius Werner9636a102019-05-03 17:36:43 -0700239 struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200240 *new = entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200241 entry->start = start;
242 entry->size = size;
243
244 return sizeof(uint64_t) * 2;
245}
246
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200247struct device_tree *fdt_unflatten(const void *blob)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200248{
Julius Werner9636a102019-05-03 17:36:43 -0700249 struct device_tree *tree = xzalloc(sizeof(*tree));
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200250 const struct fdt_header *header = (const struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200251 tree->header = header;
252
Julius Werner73eaec82019-05-03 17:58:07 -0700253 uint32_t magic = be32toh(header->magic);
254 uint32_t version = be32toh(header->version);
255 uint32_t last_comp_version = be32toh(header->last_comp_version);
256
257 if (magic != FDT_HEADER_MAGIC) {
258 printk(BIOS_DEBUG, "Invalid device tree magic %#.8x!\n", magic);
Jacob Garber698d83a2019-06-07 10:28:54 -0600259 free(tree);
Julius Werner73eaec82019-05-03 17:58:07 -0700260 return NULL;
261 }
262 if (last_comp_version > FDT_SUPPORTED_VERSION) {
263 printk(BIOS_DEBUG, "Unsupported device tree version %u(>=%u)\n",
264 version, last_comp_version);
Jacob Garber698d83a2019-06-07 10:28:54 -0600265 free(tree);
Julius Werner73eaec82019-05-03 17:58:07 -0700266 return NULL;
267 }
268 if (version > FDT_SUPPORTED_VERSION)
269 printk(BIOS_DEBUG,
270 "NOTE: FDT version %u too new, should add support!\n",
271 version);
272
Patrick Rudolph666c1722018-04-03 09:57:33 +0200273 uint32_t struct_offset = be32toh(header->structure_offset);
274 uint32_t strings_offset = be32toh(header->strings_offset);
275 uint32_t reserve_offset = be32toh(header->reserve_map_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200276 uint32_t min_offset = 0;
277 min_offset = MIN(struct_offset, strings_offset);
278 min_offset = MIN(min_offset, reserve_offset);
Julius Werner23df4772019-05-17 22:50:18 -0700279 /* Assume everything up to the first non-header component is part of
280 the header and needs to be preserved. This will protect us against
281 new elements being added in the future. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200282 tree->header_size = min_offset;
283
Patrick Rudolph666c1722018-04-03 09:57:33 +0200284 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200285 uint32_t offset = reserve_offset;
286 int size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200287 struct list_node *last = &tree->reserve_map;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200288 while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) {
289 list_insert_after(&entry->list_node, last);
290 last = &entry->list_node;
291
292 offset += size;
293 }
294
Julius Werner6702b682019-05-03 18:13:53 -0700295 fdt_unflatten_node(blob, struct_offset, tree, &tree->root);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200296
297 return tree;
298}
299
300
301
302/*
Patrick Rudolph666c1722018-04-03 09:57:33 +0200303 * Functions to find the size of the device tree if it was flattened.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200304 */
305
Patrick Rudolph666c1722018-04-03 09:57:33 +0200306static void dt_flat_prop_size(struct device_tree_property *prop,
307 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200308{
Julius Werner23df4772019-05-17 22:50:18 -0700309 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200310 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700311 /* Size. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200312 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700313 /* Name offset. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200314 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700315 /* Property value. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200316 *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200317
Julius Werner23df4772019-05-17 22:50:18 -0700318 /* Property name. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200319 *strings_size += strlen(prop->prop.name) + 1;
320}
321
Patrick Rudolph666c1722018-04-03 09:57:33 +0200322static void dt_flat_node_size(struct device_tree_node *node,
323 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200324{
Julius Werner23df4772019-05-17 22:50:18 -0700325 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200326 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700327 /* Node name. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200328 *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200329
Patrick Rudolph666c1722018-04-03 09:57:33 +0200330 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200331 list_for_each(prop, node->properties, list_node)
332 dt_flat_prop_size(prop, struct_size, strings_size);
333
Patrick Rudolph666c1722018-04-03 09:57:33 +0200334 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200335 list_for_each(child, node->children, list_node)
336 dt_flat_node_size(child, struct_size, strings_size);
337
Julius Werner23df4772019-05-17 22:50:18 -0700338 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200339 *struct_size += sizeof(uint32_t);
340}
341
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200342uint32_t dt_flat_size(const struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200343{
344 uint32_t size = tree->header_size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200345 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200346 list_for_each(entry, tree->reserve_map, list_node)
347 size += sizeof(uint64_t) * 2;
348 size += sizeof(uint64_t) * 2;
349
350 uint32_t struct_size = 0;
351 uint32_t strings_size = 0;
352 dt_flat_node_size(tree->root, &struct_size, &strings_size);
353
354 size += struct_size;
Julius Werner23df4772019-05-17 22:50:18 -0700355 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200356 size += sizeof(uint32_t);
357
358 size += strings_size;
359
360 return size;
361}
362
363
364
365/*
366 * Functions to flatten a device tree.
367 */
368
Patrick Rudolph666c1722018-04-03 09:57:33 +0200369static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200370 void **map_start)
371{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200372 ((uint64_t *)*map_start)[0] = htobe64(entry->start);
373 ((uint64_t *)*map_start)[1] = htobe64(entry->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200374 *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2;
375}
376
Patrick Rudolph666c1722018-04-03 09:57:33 +0200377static void dt_flatten_prop(struct device_tree_property *prop,
378 void **struct_start, void *strings_base,
379 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200380{
381 uint8_t *dstruct = (uint8_t *)*struct_start;
382 uint8_t *dstrings = (uint8_t *)*strings_start;
383
Julius Wernera5ea3a22019-05-07 17:38:12 -0700384 be32enc(dstruct, FDT_TOKEN_PROPERTY);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200385 dstruct += sizeof(uint32_t);
386
Julius Wernera5ea3a22019-05-07 17:38:12 -0700387 be32enc(dstruct, prop->prop.size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200388 dstruct += sizeof(uint32_t);
389
390 uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base;
Julius Wernera5ea3a22019-05-07 17:38:12 -0700391 be32enc(dstruct, name_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200392 dstruct += sizeof(uint32_t);
393
394 strcpy((char *)dstrings, prop->prop.name);
395 dstrings += strlen(prop->prop.name) + 1;
396
397 memcpy(dstruct, prop->prop.data, prop->prop.size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200398 dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200399
400 *struct_start = dstruct;
401 *strings_start = dstrings;
402}
403
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200404static void dt_flatten_node(const struct device_tree_node *node,
405 void **struct_start, void *strings_base,
406 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200407{
408 uint8_t *dstruct = (uint8_t *)*struct_start;
409 uint8_t *dstrings = (uint8_t *)*strings_start;
410
Julius Wernera5ea3a22019-05-07 17:38:12 -0700411 be32enc(dstruct, FDT_TOKEN_BEGIN_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200412 dstruct += sizeof(uint32_t);
413
414 strcpy((char *)dstruct, node->name);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200415 dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200416
Patrick Rudolph666c1722018-04-03 09:57:33 +0200417 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200418 list_for_each(prop, node->properties, list_node)
419 dt_flatten_prop(prop, (void **)&dstruct, strings_base,
420 (void **)&dstrings);
421
Patrick Rudolph666c1722018-04-03 09:57:33 +0200422 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200423 list_for_each(child, node->children, list_node)
424 dt_flatten_node(child, (void **)&dstruct, strings_base,
425 (void **)&dstrings);
426
Julius Wernera5ea3a22019-05-07 17:38:12 -0700427 be32enc(dstruct, FDT_TOKEN_END_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200428 dstruct += sizeof(uint32_t);
429
430 *struct_start = dstruct;
431 *strings_start = dstrings;
432}
433
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200434void dt_flatten(const struct device_tree *tree, void *start_dest)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200435{
436 uint8_t *dest = (uint8_t *)start_dest;
437
438 memcpy(dest, tree->header, tree->header_size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200439 struct fdt_header *header = (struct fdt_header *)dest;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200440 dest += tree->header_size;
441
Patrick Rudolph666c1722018-04-03 09:57:33 +0200442 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200443 list_for_each(entry, tree->reserve_map, list_node)
444 dt_flatten_map_entry(entry, (void **)&dest);
445 ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0;
446 dest += sizeof(uint64_t) * 2;
447
448 uint32_t struct_size = 0;
449 uint32_t strings_size = 0;
450 dt_flat_node_size(tree->root, &struct_size, &strings_size);
451
452 uint8_t *struct_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200453 header->structure_offset = htobe32(dest - (uint8_t *)start_dest);
454 header->structure_size = htobe32(struct_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200455 dest += struct_size;
456
Patrick Rudolph666c1722018-04-03 09:57:33 +0200457 *((uint32_t *)dest) = htobe32(FDT_TOKEN_END);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200458 dest += sizeof(uint32_t);
459
460 uint8_t *strings_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200461 header->strings_offset = htobe32(dest - (uint8_t *)start_dest);
462 header->strings_size = htobe32(strings_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200463 dest += strings_size;
464
465 dt_flatten_node(tree->root, (void **)&struct_start, strings_start,
466 (void **)&strings_start);
467
Patrick Rudolph666c1722018-04-03 09:57:33 +0200468 header->totalsize = htobe32(dest - (uint8_t *)start_dest);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200469}
470
471
472
473/*
474 * Functions for printing a non-flattened device tree.
475 */
476
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200477static void print_node(const struct device_tree_node *node, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200478{
479 print_indent(depth);
Julius Werner23df4772019-05-17 22:50:18 -0700480 if (depth == 0) /* root node has no name, print a starting slash */
Julius Werner0d746532019-05-06 19:35:56 -0700481 printk(BIOS_DEBUG, "/");
482 printk(BIOS_DEBUG, "%s {\n", node->name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200483
Patrick Rudolph666c1722018-04-03 09:57:33 +0200484 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200485 list_for_each(prop, node->properties, list_node)
486 print_property(&prop->prop, depth + 1);
487
Julius Werner23df4772019-05-17 22:50:18 -0700488 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700489
Patrick Rudolph666c1722018-04-03 09:57:33 +0200490 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200491 list_for_each(child, node->children, list_node)
492 print_node(child, depth + 1);
Julius Werner0d746532019-05-06 19:35:56 -0700493
494 print_indent(depth);
495 printk(BIOS_DEBUG, "};\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200496}
497
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200498void dt_print_node(const struct device_tree_node *node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200499{
500 print_node(node, 0);
501}
502
503
504
505/*
506 * Functions for reading and manipulating an unflattened device tree.
507 */
508
509/*
510 * Read #address-cells and #size-cells properties from a node.
511 *
512 * @param node The device tree node to read from.
513 * @param addrcp Pointer to store #address-cells in, skipped if NULL.
514 * @param sizecp Pointer to store #size-cells in, skipped if NULL.
515 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200516void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
517 u32 *sizecp)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200518{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200519 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200520 list_for_each(prop, node->properties, list_node) {
521 if (addrcp && !strcmp("#address-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700522 *addrcp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200523 if (sizecp && !strcmp("#size-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700524 *sizecp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200525 }
526}
527
528/*
529 * Find a node from a device tree path, relative to a parent node.
530 *
531 * @param parent The node from which to start the relative path lookup.
532 * @param path An array of path component strings that will be looked
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200533 * up in order to find the node. Must be terminated with
534 * a NULL pointer. Example: {'firmware', 'coreboot', NULL}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200535 * @param addrcp Pointer that will be updated with any #address-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200536 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200537 * @param sizecp Pointer that will be updated with any #size-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200538 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200539 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
540 * @return The found/created node, or NULL.
541 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200542struct device_tree_node *dt_find_node(struct device_tree_node *parent,
543 const char **path, u32 *addrcp,
544 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200545{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200546 struct device_tree_node *node, *found = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200547
Julius Werner23df4772019-05-17 22:50:18 -0700548 /* Update #address-cells and #size-cells for this level. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200549 dt_read_cell_props(parent, addrcp, sizecp);
550
551 if (!*path)
552 return parent;
553
Julius Werner23df4772019-05-17 22:50:18 -0700554 /* Find the next node in the path, if it exists. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200555 list_for_each(node, parent->children, list_node) {
556 if (!strcmp(node->name, *path)) {
557 found = node;
558 break;
559 }
560 }
561
Julius Werner23df4772019-05-17 22:50:18 -0700562 /* Otherwise create it or return NULL. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200563 if (!found) {
564 if (!create)
565 return NULL;
566
Julius Werner9636a102019-05-03 17:36:43 -0700567 found = malloc(sizeof(*found));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200568 if (!found)
569 return NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200570 found->name = strdup(*path);
571 if (!found->name)
572 return NULL;
573
574 list_insert_after(&found->list_node, &parent->children);
575 }
576
577 return dt_find_node(found, path + 1, addrcp, sizecp, create);
578}
579
580/*
Julius Wernerf36d53c2019-05-03 18:23:34 -0700581 * Find a node in the tree from a string device tree path.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200582 *
Julius Wernerf36d53c2019-05-03 18:23:34 -0700583 * @param tree The device tree to search.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200584 * @param path A string representing a path in the device tree, with
Julius Wernerfbec63d2019-05-03 18:29:28 -0700585 * nodes separated by '/'. Example: "/firmware/coreboot"
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200586 * @param addrcp Pointer that will be updated with any #address-cells
587 * value found in the path. May be NULL to ignore.
588 * @param sizecp Pointer that will be updated with any #size-cells
589 * value found in the path. May be NULL to ignore.
590 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
591 * @return The found/created node, or NULL.
592 *
Julius Werner6d5695f2019-05-06 19:23:28 -0700593 * It is the caller responsibility to provide a path string that doesn't end
594 * with a '/' and doesn't contain any "//". If the path does not start with a
595 * '/', the first segment is interpreted as an alias. */
Julius Wernerf36d53c2019-05-03 18:23:34 -0700596struct device_tree_node *dt_find_node_by_path(struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200597 const char *path, u32 *addrcp,
598 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200599{
Julius Werner6d5695f2019-05-06 19:23:28 -0700600 char *sub_path;
601 char *duped_str;
602 struct device_tree_node *parent;
603 char *next_slash;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200604 /* Hopefully enough depth for any node. */
605 const char *path_array[15];
606 int i;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200607 struct device_tree_node *node = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200608
Julius Werner23df4772019-05-17 22:50:18 -0700609 if (path[0] == '/') { /* regular path */
610 if (path[1] == '\0') { /* special case: "/" is root node */
Julius Werner6d5695f2019-05-06 19:23:28 -0700611 dt_read_cell_props(tree->root, addrcp, sizecp);
612 return tree->root;
613 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200614
Julius Werner6d5695f2019-05-06 19:23:28 -0700615 sub_path = duped_str = strdup(&path[1]);
616 if (!sub_path)
617 return NULL;
618
619 parent = tree->root;
Julius Werner23df4772019-05-17 22:50:18 -0700620 } else { /* alias */
Julius Werner6d5695f2019-05-06 19:23:28 -0700621 char *alias;
622
623 alias = duped_str = strdup(path);
624 if (!alias)
625 return NULL;
626
627 sub_path = strchr(alias, '/');
628 if (sub_path)
629 *sub_path = '\0';
630
631 parent = dt_find_node_by_alias(tree, alias);
632 if (!parent) {
633 printk(BIOS_DEBUG,
634 "Could not find node '%s', alias '%s' does not exist\n",
635 path, alias);
636 free(duped_str);
637 return NULL;
638 }
639
640 if (!sub_path) {
Julius Werner23df4772019-05-17 22:50:18 -0700641 /* it's just the alias, no sub-path */
Julius Werner6d5695f2019-05-06 19:23:28 -0700642 free(duped_str);
643 return parent;
644 }
645
646 sub_path++;
647 }
648
649 next_slash = sub_path;
650 path_array[0] = sub_path;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200651 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200652 next_slash = strchr(next_slash, '/');
653 if (!next_slash)
654 break;
655
656 *next_slash++ = '\0';
657 path_array[i] = next_slash;
658 }
659
660 if (!next_slash) {
661 path_array[i] = NULL;
Julius Werner6d5695f2019-05-06 19:23:28 -0700662 node = dt_find_node(parent, path_array,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200663 addrcp, sizecp, create);
664 }
665
Julius Werner6d5695f2019-05-06 19:23:28 -0700666 free(duped_str);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200667 return node;
668}
669
Julius Werner6d5695f2019-05-06 19:23:28 -0700670/*
671 * Find a node from an alias
672 *
673 * @param tree The device tree.
674 * @param alias The alias name.
675 * @return The found node, or NULL.
676 */
677struct device_tree_node *dt_find_node_by_alias(struct device_tree *tree,
678 const char *alias)
679{
680 struct device_tree_node *node;
681 const char *alias_path;
682
683 node = dt_find_node_by_path(tree, "/aliases", NULL, NULL, 0);
684 if (!node)
685 return NULL;
686
687 alias_path = dt_find_string_prop(node, alias);
688 if (!alias_path)
689 return NULL;
690
691 return dt_find_node_by_path(tree, alias_path, NULL, NULL, 0);
692}
693
Julius Werner6702b682019-05-03 18:13:53 -0700694struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root,
695 uint32_t phandle)
696{
697 if (!root)
698 return NULL;
699
700 if (root->phandle == phandle)
701 return root;
702
703 struct device_tree_node *node;
704 struct device_tree_node *result;
705 list_for_each(node, root->children, list_node) {
706 result = dt_find_node_by_phandle(node, phandle);
707 if (result)
708 return result;
709 }
710
711 return NULL;
712}
713
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200714/*
715 * Check if given node is compatible.
716 *
717 * @param node The node which is to be checked for compatible property.
718 * @param compat The compatible string to match.
719 * @return 1 = compatible, 0 = not compatible.
720 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200721static int dt_check_compat_match(struct device_tree_node *node,
722 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200723{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200724 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200725
726 list_for_each(prop, node->properties, list_node) {
727 if (!strcmp("compatible", prop->prop.name)) {
728 size_t bytes = prop->prop.size;
729 const char *str = prop->prop.data;
730 while (bytes > 0) {
731 if (!strncmp(compat, str, bytes))
732 return 1;
733 size_t len = strnlen(str, bytes) + 1;
734 if (bytes <= len)
735 break;
736 str += len;
737 bytes -= len;
738 }
739 break;
740 }
741 }
742
743 return 0;
744}
745
746/*
747 * Find a node from a compatible string, in the subtree of a parent node.
748 *
749 * @param parent The parent node under which to look.
750 * @param compat The compatible string to find.
751 * @return The found node, or NULL.
752 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200753struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
754 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200755{
Julius Werner23df4772019-05-17 22:50:18 -0700756 /* Check if the parent node itself is compatible. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200757 if (dt_check_compat_match(parent, compat))
758 return parent;
759
Patrick Rudolph666c1722018-04-03 09:57:33 +0200760 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200761 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200762 struct device_tree_node *found = dt_find_compat(child, compat);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200763 if (found)
764 return found;
765 }
766
767 return NULL;
768}
769
770/*
771 * Find the next compatible child of a given parent. All children upto the
772 * child passed in by caller are ignored. If child is NULL, it considers all the
773 * children to find the first child which is compatible.
774 *
775 * @param parent The parent node under which to look.
776 * @param child The child node to start search from (exclusive). If NULL
777 * consider all children.
778 * @param compat The compatible string to find.
779 * @return The found node, or NULL.
780 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200781struct device_tree_node *
782dt_find_next_compat_child(struct device_tree_node *parent,
783 struct device_tree_node *child,
784 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200785{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200786 struct device_tree_node *next;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200787 int ignore = 0;
788
789 if (child)
790 ignore = 1;
791
792 list_for_each(next, parent->children, list_node) {
793 if (ignore) {
794 if (child == next)
795 ignore = 0;
796 continue;
797 }
798
799 if (dt_check_compat_match(next, compat))
800 return next;
801 }
802
803 return NULL;
804}
805
806/*
807 * Find a node with matching property value, in the subtree of a parent node.
808 *
809 * @param parent The parent node under which to look.
810 * @param name The property name to look for.
811 * @param data The property value to look for.
812 * @param size The property size.
813 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200814struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
815 const char *name, void *data,
816 size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200817{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200818 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200819
820 /* Check if parent itself has the required property value. */
821 list_for_each(prop, parent->properties, list_node) {
822 if (!strcmp(name, prop->prop.name)) {
823 size_t bytes = prop->prop.size;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200824 const void *prop_data = prop->prop.data;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200825 if (size != bytes)
826 break;
827 if (!memcmp(data, prop_data, size))
828 return parent;
829 break;
830 }
831 }
832
Patrick Rudolph666c1722018-04-03 09:57:33 +0200833 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200834 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200835 struct device_tree_node *found = dt_find_prop_value(child, name,
836 data, size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200837 if (found)
838 return found;
839 }
840 return NULL;
841}
842
843/*
844 * Write an arbitrary sized big-endian integer into a pointer.
845 *
846 * @param dest Pointer to the DT property data buffer to write.
Elyes HAOUAS1ec76442018-08-07 12:20:04 +0200847 * @param src The integer to write (in CPU endianness).
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200848 * @param length the length of the destination integer in bytes.
849 */
850void dt_write_int(u8 *dest, u64 src, size_t length)
851{
852 while (length--) {
853 dest[length] = (u8)src;
854 src >>= 8;
855 }
856}
857
858/*
Patrick Rudolph5ccc7312018-05-30 15:05:28 +0200859 * Delete a property by name in a given node if it exists.
860 *
861 * @param node The device tree node to operate on.
862 * @param name The name of the property to delete.
863 */
864void dt_delete_prop(struct device_tree_node *node, const char *name)
865{
866 struct device_tree_property *prop;
867
868 list_for_each(prop, node->properties, list_node) {
869 if (!strcmp(prop->prop.name, name)) {
870 list_remove(&prop->list_node);
871 return;
872 }
873 }
874}
875
876/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200877 * Add an arbitrary property to a node, or update it if it already exists.
878 *
879 * @param node The device tree node to add to.
880 * @param name The name of the new property.
881 * @param data The raw data blob to be stored in the property.
882 * @param size The size of data in bytes.
883 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200884void dt_add_bin_prop(struct device_tree_node *node, const char *name,
Julius Werner0e9116f2019-05-13 17:30:31 -0700885 void *data, size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200886{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200887 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200888
889 list_for_each(prop, node->properties, list_node) {
890 if (!strcmp(prop->prop.name, name)) {
891 prop->prop.data = data;
892 prop->prop.size = size;
893 return;
894 }
895 }
896
Julius Werner9636a102019-05-03 17:36:43 -0700897 prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200898 list_insert_after(&prop->list_node, &node->properties);
899 prop->prop.name = name;
900 prop->prop.data = data;
901 prop->prop.size = size;
902}
903
904/*
905 * Find given string property in a node and return its content.
906 *
907 * @param node The device tree node to search.
908 * @param name The name of the property.
909 * @return The found string, or NULL.
910 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200911const char *dt_find_string_prop(const struct device_tree_node *node,
912 const char *name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200913{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200914 const void *content;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200915 size_t size;
916
917 dt_find_bin_prop(node, name, &content, &size);
918
919 return content;
920}
921
922/*
923 * Find given property in a node.
924 *
925 * @param node The device tree node to search.
926 * @param name The name of the property.
927 * @param data Pointer to return raw data blob in the property.
928 * @param size Pointer to return the size of data in bytes.
929 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200930void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
931 const void **data, size_t *size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200932{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200933 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200934
935 *data = NULL;
936 *size = 0;
937
938 list_for_each(prop, node->properties, list_node) {
939 if (!strcmp(prop->prop.name, name)) {
940 *data = prop->prop.data;
941 *size = prop->prop.size;
942 return;
943 }
944 }
945}
946
947/*
948 * Add a string property to a node, or update it if it already exists.
949 *
950 * @param node The device tree node to add to.
951 * @param name The name of the new property.
952 * @param str The zero-terminated string to be stored in the property.
953 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200954void dt_add_string_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200955 const char *str)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200956{
Julius Werner0e9116f2019-05-13 17:30:31 -0700957 dt_add_bin_prop(node, name, (char *)str, strlen(str) + 1);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200958}
959
960/*
961 * Add a 32-bit integer property to a node, or update it if it already exists.
962 *
963 * @param node The device tree node to add to.
964 * @param name The name of the new property.
965 * @param val The integer to be stored in the property.
966 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200967void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200968{
Julius Werner9636a102019-05-03 17:36:43 -0700969 u32 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200970 *val_ptr = htobe32(val);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200971 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
972}
973
974/*
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200975 * Add a 64-bit integer property to a node, or update it if it already exists.
976 *
977 * @param node The device tree node to add to.
978 * @param name The name of the new property.
979 * @param val The integer to be stored in the property.
980 */
981void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
982{
Julius Werner9636a102019-05-03 17:36:43 -0700983 u64 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200984 *val_ptr = htobe64(val);
985 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
986}
987
988/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200989 * Add a 'reg' address list property to a node, or update it if it exists.
990 *
991 * @param node The device tree node to add to.
992 * @param addrs Array of address values to be stored in the property.
993 * @param sizes Array of corresponding size values to 'addrs'.
994 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
995 * @param addr_cells Value of #address-cells property valid for this node.
996 * @param size_cells Value of #size-cells property valid for this node.
997 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200998void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200999 int count, u32 addr_cells, u32 size_cells)
1000{
1001 int i;
1002 size_t length = (addr_cells + size_cells) * sizeof(u32) * count;
Julius Werner9636a102019-05-03 17:36:43 -07001003 u8 *data = xmalloc(length);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001004 u8 *cur = data;
1005
1006 for (i = 0; i < count; i++) {
1007 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
1008 cur += addr_cells * sizeof(u32);
1009 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
1010 cur += size_cells * sizeof(u32);
1011 }
1012
1013 dt_add_bin_prop(node, "reg", data, length);
1014}
1015
1016/*
1017 * Fixups to apply to a kernel's device tree before booting it.
1018 */
1019
Patrick Rudolph666c1722018-04-03 09:57:33 +02001020struct list_node device_tree_fixups;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001021
Patrick Rudolph666c1722018-04-03 09:57:33 +02001022int dt_apply_fixups(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001023{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001024 struct device_tree_fixup *fixup;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001025 list_for_each(fixup, device_tree_fixups, list_node) {
1026 assert(fixup->fixup);
1027 if (fixup->fixup(fixup, tree))
1028 return 1;
1029 }
1030 return 0;
1031}
1032
Patrick Rudolph666c1722018-04-03 09:57:33 +02001033int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001034 void *data, size_t data_size, int create)
1035{
1036 char *path_copy, *prop_name;
Patrick Rudolph666c1722018-04-03 09:57:33 +02001037 struct device_tree_node *dt_node;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001038
1039 path_copy = strdup(path);
1040
1041 if (!path_copy) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001042 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
1043 path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001044 return 1;
1045 }
1046
1047 prop_name = strrchr(path_copy, '/');
1048 if (!prop_name) {
Patrick Rudolph679d6242018-07-11 13:53:04 +02001049 free(path_copy);
Patrick Rudolph666c1722018-04-03 09:57:33 +02001050 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001051 return 1;
1052 }
1053
1054 *prop_name++ = '\0'; /* Separate path from the property name. */
1055
Julius Wernerf36d53c2019-05-03 18:23:34 -07001056 dt_node = dt_find_node_by_path(tree, path_copy, NULL,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001057 NULL, create);
1058
1059 if (!dt_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001060 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001061 create ? "create" : "find", path_copy);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001062 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001063 return 1;
1064 }
1065
1066 dt_add_bin_prop(dt_node, prop_name, data, data_size);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001067 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001068
1069 return 0;
1070}
1071
1072/*
1073 * Prepare the /reserved-memory/ node.
1074 *
1075 * Technically, this can be called more than one time, to init and/or retrieve
1076 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
1077 *
1078 * @tree: Device tree to add/retrieve from.
1079 * @return: The /reserved-memory/ node (or NULL, if error).
1080 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001081struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001082{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001083 struct device_tree_node *reserved;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001084 u32 addr = 0, size = 0;
1085
Julius Wernerfbec63d2019-05-03 18:29:28 -07001086 reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001087 &size, 1);
1088 if (!reserved)
1089 return NULL;
1090
Julius Werner23df4772019-05-17 22:50:18 -07001091 /* Binding doc says this should have the same #{address,size}-cells as
1092 the root. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001093 dt_add_u32_prop(reserved, "#address-cells", addr);
1094 dt_add_u32_prop(reserved, "#size-cells", size);
Julius Werner23df4772019-05-17 22:50:18 -07001095 /* Binding doc says this should be empty (1:1 mapping from root). */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001096 dt_add_bin_prop(reserved, "ranges", NULL, 0);
1097
1098 return reserved;
1099}
Julius Werner735ddc92019-05-07 17:05:28 -07001100
1101/*
1102 * Increment a single phandle in prop at a given offset by a given adjustment.
1103 *
1104 * @param prop Property whose phandle should be adjusted.
1105 * @param adjustment Value that should be added to the existing phandle.
1106 * @param offset Byte offset of the phandle in the property data.
1107 *
1108 * @return New phandle value, or 0 on error.
1109 */
1110static uint32_t dt_adjust_phandle(struct device_tree_property *prop,
1111 uint32_t adjustment, uint32_t offset)
1112{
1113 if (offset + 4 > prop->prop.size)
1114 return 0;
1115
1116 uint32_t phandle = be32dec(prop->prop.data + offset);
1117 if (phandle == 0 ||
1118 phandle == FDT_PHANDLE_ILLEGAL ||
1119 phandle == 0xffffffff)
1120 return 0;
1121
1122 phandle += adjustment;
1123 if (phandle >= FDT_PHANDLE_ILLEGAL)
1124 return 0;
1125
1126 be32enc(prop->prop.data + offset, phandle);
1127 return phandle;
1128}
1129
1130/*
1131 * Adjust all phandles in subtree by adding a new base offset.
1132 *
1133 * @param node Root node of the subtree to work on.
1134 * @param base New phandle base to be added to all phandles.
1135 *
1136 * @return New highest phandle in the subtree, or 0 on error.
1137 */
1138static uint32_t dt_adjust_all_phandles(struct device_tree_node *node,
1139 uint32_t base)
1140{
Julius Werner23df4772019-05-17 22:50:18 -07001141 uint32_t new_max = MAX(base, 1); /* make sure we don't return 0 */
Julius Werner735ddc92019-05-07 17:05:28 -07001142 struct device_tree_property *prop;
1143 struct device_tree_node *child;
1144
1145 if (!node)
1146 return new_max;
1147
1148 list_for_each(prop, node->properties, list_node)
1149 if (dt_prop_is_phandle(prop)) {
1150 node->phandle = dt_adjust_phandle(prop, base, 0);
1151 if (!node->phandle)
1152 return 0;
1153 new_max = MAX(new_max, node->phandle);
Julius Werner23df4772019-05-17 22:50:18 -07001154 } /* no break -- can have more than one phandle prop */
Julius Werner735ddc92019-05-07 17:05:28 -07001155
1156 list_for_each(child, node->children, list_node)
1157 new_max = MAX(new_max, dt_adjust_all_phandles(child, base));
1158
1159 return new_max;
1160}
1161
1162/*
1163 * Apply a /__local_fixup__ subtree to the corresponding overlay subtree.
1164 *
1165 * @param node Root node of the overlay subtree to fix up.
1166 * @param node Root node of the /__local_fixup__ subtree.
1167 * @param base Adjustment that was added to phandles in the overlay.
1168 *
1169 * @return 0 on success, -1 on error.
1170 */
1171static int dt_fixup_locals(struct device_tree_node *node,
1172 struct device_tree_node *fixup, uint32_t base)
1173{
1174 struct device_tree_property *prop;
1175 struct device_tree_property *fixup_prop;
1176 struct device_tree_node *child;
1177 struct device_tree_node *fixup_child;
1178 int i;
1179
Julius Werner23df4772019-05-17 22:50:18 -07001180 /*
1181 * For local fixups the /__local_fixup__ subtree contains the same node
1182 * hierarchy as the main tree we're fixing up. Each property contains
1183 * the fixup offsets for the respective property in the main tree. For
1184 * each property in the fixup node, find the corresponding property in
1185 * the base node and apply fixups to all offsets it specifies.
1186 */
Julius Werner735ddc92019-05-07 17:05:28 -07001187 list_for_each(fixup_prop, fixup->properties, list_node) {
1188 struct device_tree_property *base_prop = NULL;
1189 list_for_each(prop, node->properties, list_node)
1190 if (!strcmp(prop->prop.name, fixup_prop->prop.name)) {
1191 base_prop = prop;
1192 break;
1193 }
1194
Julius Werner23df4772019-05-17 22:50:18 -07001195 /* We should always find a corresponding base prop for a fixup,
1196 and fixup props contain a list of 32-bit fixup offsets. */
Julius Werner735ddc92019-05-07 17:05:28 -07001197 if (!base_prop || fixup_prop->prop.size % sizeof(uint32_t))
1198 return -1;
1199
1200 for (i = 0; i < fixup_prop->prop.size; i += sizeof(uint32_t))
1201 if (!dt_adjust_phandle(base_prop, base, be32dec(
1202 fixup_prop->prop.data + i)))
1203 return -1;
1204 }
1205
Julius Werner23df4772019-05-17 22:50:18 -07001206 /* Now recursively descend both the base tree and the /__local_fixups__
1207 subtree in sync to apply all fixups. */
Julius Werner735ddc92019-05-07 17:05:28 -07001208 list_for_each(fixup_child, fixup->children, list_node) {
1209 struct device_tree_node *base_child = NULL;
1210 list_for_each(child, node->children, list_node)
1211 if (!strcmp(child->name, fixup_child->name)) {
1212 base_child = child;
1213 break;
1214 }
1215
Julius Werner23df4772019-05-17 22:50:18 -07001216 /* All fixup nodes should have a corresponding base node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001217 if (!base_child)
1218 return -1;
1219
1220 if (dt_fixup_locals(base_child, fixup_child, base) < 0)
1221 return -1;
1222 }
1223
1224 return 0;
1225}
1226
1227/*
1228 * Update all /__symbols__ properties in an overlay that start with
1229 * "/fragment@X/__overlay__" with corresponding path prefix in the base tree.
1230 *
1231 * @param symbols /__symbols__ done to update.
1232 * @param fragment /fragment@X node that references to should be updated.
1233 * @param base_path Path of base tree node that the fragment overlaid.
1234 */
1235static void dt_fix_symbols(struct device_tree_node *symbols,
1236 struct device_tree_node *fragment,
1237 const char *base_path)
1238{
1239 struct device_tree_property *prop;
Julius Werner23df4772019-05-17 22:50:18 -07001240 char buf[512]; /* Should be enough for maximum DT path length? */
1241 char node_path[64]; /* easily enough for /fragment@XXXX/__overlay__ */
Julius Werner735ddc92019-05-07 17:05:28 -07001242
Julius Werner23df4772019-05-17 22:50:18 -07001243 if (!symbols) /* If the overlay has no /__symbols__ node, we're done! */
Julius Werner735ddc92019-05-07 17:05:28 -07001244 return;
1245
1246 int len = snprintf(node_path, sizeof(node_path), "/%s/__overlay__",
1247 fragment->name);
1248
1249 list_for_each(prop, symbols->properties, list_node)
1250 if (!strncmp(prop->prop.data, node_path, len)) {
1251 prop->prop.size = snprintf(buf, sizeof(buf), "%s%s",
1252 base_path, (char *)prop->prop.data + len) + 1;
1253 free(prop->prop.data);
1254 prop->prop.data = strdup(buf);
1255 }
1256}
1257
1258/*
1259 * Fix up overlay according to a property in /__fixup__. If the fixed property
1260 * is a /fragment@X:target, also update /__symbols__ references to fragment.
1261 *
1262 * @params overlay Overlay to fix up.
1263 * @params fixup /__fixup__ property.
1264 * @params phandle phandle value to insert where the fixup points to.
1265 * @params base_path Path to the base DT node that the fixup points to.
1266 * @params overlay_symbols /__symbols__ node of the overlay.
1267 *
1268 * @return 0 on success, -1 on error.
1269 */
1270static int dt_fixup_external(struct device_tree *overlay,
1271 struct device_tree_property *fixup,
1272 uint32_t phandle, const char *base_path,
1273 struct device_tree_node *overlay_symbols)
1274{
1275 struct device_tree_property *prop;
1276
Julius Werner23df4772019-05-17 22:50:18 -07001277 /* External fixup properties are encoded as "<path>:<prop>:<offset>". */
Julius Werner735ddc92019-05-07 17:05:28 -07001278 char *entry = fixup->prop.data;
1279 while ((void *)entry < fixup->prop.data + fixup->prop.size) {
Julius Werner23df4772019-05-17 22:50:18 -07001280 /* okay to destroy fixup property value, won't need it again */
Julius Werner735ddc92019-05-07 17:05:28 -07001281 char *node_path = entry;
1282 entry = strchr(node_path, ':');
1283 if (!entry)
1284 return -1;
1285 *entry++ = '\0';
1286
1287 char *prop_name = entry;
1288 entry = strchr(prop_name, ':');
1289 if (!entry)
1290 return -1;
1291 *entry++ = '\0';
1292
1293 struct device_tree_node *ovl_node = dt_find_node_by_path(
1294 overlay, node_path, NULL, NULL, 0);
1295 if (!ovl_node || !isdigit(*entry))
1296 return -1;
1297
1298 struct device_tree_property *ovl_prop = NULL;
1299 list_for_each(prop, ovl_node->properties, list_node)
1300 if (!strcmp(prop->prop.name, prop_name)) {
1301 ovl_prop = prop;
1302 break;
1303 }
1304
Julius Werner23df4772019-05-17 22:50:18 -07001305 /* Move entry to first char after number, must be a '\0'. */
Julius Werner735ddc92019-05-07 17:05:28 -07001306 uint32_t offset = skip_atoi(&entry);
1307 if (!ovl_prop || offset + 4 > ovl_prop->prop.size || entry[0])
1308 return -1;
Julius Werner23df4772019-05-17 22:50:18 -07001309 entry++; /* jump over '\0' to potential next fixup */
Julius Werner735ddc92019-05-07 17:05:28 -07001310
1311 be32enc(ovl_prop->prop.data + offset, phandle);
1312
Julius Werner23df4772019-05-17 22:50:18 -07001313 /* If this is a /fragment@X:target property, update references
1314 to this fragment in the overlay __symbols__ now. */
Julius Werner735ddc92019-05-07 17:05:28 -07001315 if (offset == 0 && !strcmp(prop_name, "target") &&
Julius Werner23df4772019-05-17 22:50:18 -07001316 !strchr(node_path + 1, '/')) /* only toplevel nodes */
Julius Werner735ddc92019-05-07 17:05:28 -07001317 dt_fix_symbols(overlay_symbols, ovl_node, base_path);
1318 }
1319
1320 return 0;
1321}
1322
1323/*
1324 * Apply all /__fixup__ properties in the overlay. This will destroy the
1325 * property data in /__fixup__ and it should not be accessed again.
1326 *
1327 * @params tree Base device tree that the overlay updates.
1328 * @params symbols /__symbols__ node of the base device tree.
1329 * @params overlay Overlay to fix up.
1330 * @params fixups /__fixup__ node in the overlay.
1331 * @params overlay_symbols /__symbols__ node of the overlay.
1332 *
1333 * @return 0 on success, -1 on error.
1334 */
1335static int dt_fixup_all_externals(struct device_tree *tree,
1336 struct device_tree_node *symbols,
1337 struct device_tree *overlay,
1338 struct device_tree_node *fixups,
1339 struct device_tree_node *overlay_symbols)
1340{
1341 struct device_tree_property *fix;
1342
Julius Werner23df4772019-05-17 22:50:18 -07001343 /* If we have any external fixups, base tree must have /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001344 if (!symbols)
1345 return -1;
1346
Julius Werner23df4772019-05-17 22:50:18 -07001347 /*
1348 * Unlike /__local_fixups__, /__fixups__ is not a whole subtree that
1349 * mirrors the node hierarchy. It's just a directory of fixup properties
1350 * that each directly contain all information necessary to apply them.
1351 */
Julius Werner735ddc92019-05-07 17:05:28 -07001352 list_for_each(fix, fixups->properties, list_node) {
Julius Werner23df4772019-05-17 22:50:18 -07001353 /* The name of a fixup property is the label of the node we want
1354 a property to phandle-reference. Look up in /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001355 const char *path = dt_find_string_prop(symbols, fix->prop.name);
1356 if (!path)
1357 return -1;
1358
Julius Werner23df4772019-05-17 22:50:18 -07001359 /* Find node the label pointed to to figure out its phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001360 struct device_tree_node *node = dt_find_node_by_path(tree, path,
1361 NULL, NULL, 0);
1362 if (!node)
1363 return -1;
1364
Julius Werner23df4772019-05-17 22:50:18 -07001365 /* Write into the overlay property(s) pointing to that node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001366 if (dt_fixup_external(overlay, fix, node->phandle,
1367 path, overlay_symbols) < 0)
1368 return -1;
1369 }
1370
1371 return 0;
1372}
1373
1374/*
1375 * Copy all nodes and properties from one DT subtree into another. This is a
1376 * shallow copy so both trees will point to the same property data afterwards.
1377 *
1378 * @params dst Destination subtree to copy into.
1379 * @params src Source subtree to copy from.
1380 * @params upd 1 to overwrite same-name properties, 0 to discard them.
1381 */
1382static void dt_copy_subtree(struct device_tree_node *dst,
1383 struct device_tree_node *src, int upd)
1384{
1385 struct device_tree_property *prop;
1386 struct device_tree_property *src_prop;
1387 list_for_each(src_prop, src->properties, list_node) {
1388 if (dt_prop_is_phandle(src_prop) ||
1389 !strcmp(src_prop->prop.name, "name")) {
1390 printk(BIOS_DEBUG,
1391 "WARNING: ignoring illegal overlay prop '%s'\n",
1392 src_prop->prop.name);
1393 continue;
1394 }
1395
1396 struct device_tree_property *dst_prop = NULL;
1397 list_for_each(prop, dst->properties, list_node)
1398 if (!strcmp(prop->prop.name, src_prop->prop.name)) {
1399 dst_prop = prop;
1400 break;
1401 }
1402
1403 if (dst_prop) {
1404 if (!upd) {
1405 printk(BIOS_DEBUG,
1406 "WARNING: ignoring prop update '%s'\n",
1407 src_prop->prop.name);
1408 continue;
1409 }
1410 } else {
1411 dst_prop = xzalloc(sizeof(*dst_prop));
1412 list_insert_after(&dst_prop->list_node,
1413 &dst->properties);
1414 }
1415
1416 dst_prop->prop = src_prop->prop;
1417 }
1418
1419 struct device_tree_node *node;
1420 struct device_tree_node *src_node;
1421 list_for_each(src_node, src->children, list_node) {
1422 struct device_tree_node *dst_node = NULL;
1423 list_for_each(node, dst->children, list_node)
1424 if (!strcmp(node->name, src_node->name)) {
1425 dst_node = node;
1426 break;
1427 }
1428
1429 if (!dst_node) {
1430 dst_node = xzalloc(sizeof(*dst_node));
1431 *dst_node = *src_node;
1432 list_insert_after(&dst_node->list_node, &dst->children);
1433 } else {
1434 dt_copy_subtree(dst_node, src_node, upd);
1435 }
1436 }
1437}
1438
1439/*
1440 * Apply an overlay /fragment@X node to a base device tree.
1441 *
1442 * @param tree Base device tree.
1443 * @param fragment /fragment@X node.
1444 * @params overlay_symbols /__symbols__ node of the overlay.
1445 *
1446 * @return 0 on success, -1 on error.
1447 */
1448static int dt_import_fragment(struct device_tree *tree,
1449 struct device_tree_node *fragment,
1450 struct device_tree_node *overlay_symbols)
1451{
Julius Werner23df4772019-05-17 22:50:18 -07001452 /* The actual overlaid nodes/props are in an __overlay__ child node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001453 static const char *overlay_path[] = { "__overlay__", NULL };
1454 struct device_tree_node *overlay = dt_find_node(fragment, overlay_path,
1455 NULL, NULL, 0);
1456
Julius Werner23df4772019-05-17 22:50:18 -07001457 /* If it doesn't have an __overlay__ child, it's not a fragment. */
Julius Werner735ddc92019-05-07 17:05:28 -07001458 if (!overlay)
1459 return 0;
1460
Julius Werner23df4772019-05-17 22:50:18 -07001461 /* Target node of the fragment can be given by path or by phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001462 struct device_tree_property *prop;
1463 struct device_tree_property *phandle = NULL;
1464 struct device_tree_property *path = NULL;
1465 list_for_each(prop, fragment->properties, list_node) {
1466 if (!strcmp(prop->prop.name, "target")) {
1467 phandle = prop;
Julius Werner23df4772019-05-17 22:50:18 -07001468 break; /* phandle target has priority, stop looking */
Julius Werner735ddc92019-05-07 17:05:28 -07001469 }
1470 if (!strcmp(prop->prop.name, "target-path"))
1471 path = prop;
1472 }
1473
1474 struct device_tree_node *target = NULL;
1475 if (phandle) {
1476 if (phandle->prop.size != sizeof(uint32_t))
1477 return -1;
1478 target = dt_find_node_by_phandle(tree->root,
1479 be32dec(phandle->prop.data));
Julius Werner23df4772019-05-17 22:50:18 -07001480 /* Symbols already updated as part of dt_fixup_external(). */
Julius Werner735ddc92019-05-07 17:05:28 -07001481 } else if (path) {
1482 target = dt_find_node_by_path(tree, path->prop.data,
1483 NULL, NULL, 0);
1484 dt_fix_symbols(overlay_symbols, fragment, path->prop.data);
1485 }
1486 if (!target)
1487 return -1;
1488
1489 dt_copy_subtree(target, overlay, 1);
1490 return 0;
1491}
1492
1493/*
1494 * Apply a device tree overlay to a base device tree. This will
1495 * destroy/incorporate the overlay data, so it should not be freed or reused.
1496 * See dtc.git/Documentation/dt-object-internal.txt for overlay format details.
1497 *
1498 * @param tree Unflattened base device tree to add the overlay into.
1499 * @param overlay Unflattened overlay device tree to apply to the base.
1500 *
1501 * @return 0 on success, -1 on error.
1502 */
1503int dt_apply_overlay(struct device_tree *tree, struct device_tree *overlay)
1504{
Julius Werner23df4772019-05-17 22:50:18 -07001505 /*
1506 * First, we need to make sure phandles inside the overlay don't clash
1507 * with those in the base tree. We just define the highest phandle value
1508 * in the base tree as the "phandle offset" for this overlay and
1509 * increment all phandles in it by that value.
1510 */
Julius Werner735ddc92019-05-07 17:05:28 -07001511 uint32_t phandle_base = tree->max_phandle;
1512 uint32_t new_max = dt_adjust_all_phandles(overlay->root, phandle_base);
1513 if (!new_max) {
1514 printk(BIOS_DEBUG, "ERROR: invalid phandles in overlay\n");
1515 return -1;
1516 }
1517 tree->max_phandle = new_max;
1518
Julius Werner23df4772019-05-17 22:50:18 -07001519 /* Now that we changed phandles in the overlay, we need to update any
1520 nodes referring to them. Those are listed in /__local_fixups__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001521 struct device_tree_node *local_fixups = dt_find_node_by_path(overlay,
1522 "/__local_fixups__", NULL, NULL, 0);
1523 if (local_fixups && dt_fixup_locals(overlay->root, local_fixups,
1524 phandle_base) < 0) {
1525 printk(BIOS_DEBUG, "ERROR: invalid local fixups in overlay\n");
1526 return -1;
1527 }
1528
Julius Werner23df4772019-05-17 22:50:18 -07001529 /*
1530 * Besides local phandle references (from nodes within the overlay to
1531 * other nodes within the overlay), the overlay may also contain phandle
1532 * references to the base tree. These are stored with invalid values and
1533 * must be updated now. /__symbols__ contains a list of all labels in
1534 * the base tree, and /__fixups__ describes all nodes in the overlay
1535 * that contain external phandle references.
1536 * We also take this opportunity to update all /fragment@X/__overlay__/
1537 * prefixes in the overlay's /__symbols__ node to the correct path that
1538 * the fragment will be placed in later, since this is the only step
1539 * where we have all necessary information for that easily available.
1540 */
Julius Werner735ddc92019-05-07 17:05:28 -07001541 struct device_tree_node *symbols = dt_find_node_by_path(tree,
1542 "/__symbols__", NULL, NULL, 0);
1543 struct device_tree_node *fixups = dt_find_node_by_path(overlay,
1544 "/__fixups__", NULL, NULL, 0);
1545 struct device_tree_node *overlay_symbols = dt_find_node_by_path(overlay,
1546 "/__symbols__", NULL, NULL, 0);
1547 if (fixups && dt_fixup_all_externals(tree, symbols, overlay,
1548 fixups, overlay_symbols) < 0) {
1549 printk(BIOS_DEBUG,
1550 "ERROR: cannot match external fixups from overlay\n");
1551 return -1;
1552 }
1553
Julius Werner23df4772019-05-17 22:50:18 -07001554 /* After all this fixing up, we can finally merge overlay into the tree
1555 (one fragment at a time, because for some reason it's split up). */
Julius Werner735ddc92019-05-07 17:05:28 -07001556 struct device_tree_node *fragment;
1557 list_for_each(fragment, overlay->root->children, list_node)
1558 if (dt_import_fragment(tree, fragment, overlay_symbols) < 0) {
1559 printk(BIOS_DEBUG, "ERROR: bad DT fragment '%s'\n",
1560 fragment->name);
1561 return -1;
1562 }
1563
Julius Werner23df4772019-05-17 22:50:18 -07001564 /*
1565 * We need to also update /__symbols__ to include labels from this
1566 * overlay, in case we want to load further overlays with external
1567 * phandle references to it. If the base tree already has a /__symbols__
1568 * we merge them together, otherwise we just insert the overlay's
1569 * /__symbols__ node into the base tree root.
1570 */
Julius Werner735ddc92019-05-07 17:05:28 -07001571 if (overlay_symbols) {
1572 if (symbols)
1573 dt_copy_subtree(symbols, overlay_symbols, 0);
1574 else
1575 list_insert_after(&overlay_symbols->list_node,
1576 &tree->root->children);
1577 }
1578
1579 return 0;
1580}