blob: bbcd7c06f6c5c5acb4f862d3a99974ab3cdbddb2 [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{
83 while (depth--)
Patrick Rudolph666c1722018-04-03 09:57:33 +020084 printk(BIOS_DEBUG, " ");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020085}
86
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020087static void print_property(const struct fdt_property *prop, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020088{
89 print_indent(depth);
Patrick Rudolph666c1722018-04-03 09:57:33 +020090 printk(BIOS_DEBUG, "prop \"%s\" (%d bytes).\n", prop->name, prop->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020091 print_indent(depth + 1);
92 for (int i = 0; i < MIN(25, prop->size); i++) {
Patrick Rudolph666c1722018-04-03 09:57:33 +020093 printk(BIOS_DEBUG, "%02x ", ((uint8_t *)prop->data)[i]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020094 }
95 if (prop->size > 25)
Patrick Rudolph666c1722018-04-03 09:57:33 +020096 printk(BIOS_DEBUG, "...");
97 printk(BIOS_DEBUG, "\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020098}
99
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200100static int print_flat_node(const void *blob, uint32_t start_offset, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200101{
102 int offset = start_offset;
103 const char *name;
104 int size;
105
106 size = fdt_node_name(blob, offset, &name);
107 if (!size)
108 return 0;
109 offset += size;
110
111 print_indent(depth);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200112 printk(BIOS_DEBUG, "name = %s\n", name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200113
Patrick Rudolph666c1722018-04-03 09:57:33 +0200114 struct fdt_property prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200115 while ((size = fdt_next_property(blob, offset, &prop))) {
116 print_property(&prop, depth + 1);
117
118 offset += size;
119 }
120
121 while ((size = print_flat_node(blob, offset, depth + 1)))
122 offset += size;
123
124 return offset - start_offset + sizeof(uint32_t);
125}
126
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200127void fdt_print_node(const void *blob, uint32_t offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200128{
129 print_flat_node(blob, offset, 0);
130}
131
132
133
134/*
135 * A utility function to skip past nodes in flattened trees.
136 */
137
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200138int fdt_skip_node(const void *blob, uint32_t start_offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200139{
140 int offset = start_offset;
141 int size;
142
143 const char *name;
144 size = fdt_node_name(blob, offset, &name);
145 if (!size)
146 return 0;
147 offset += size;
148
149 while ((size = fdt_next_property(blob, offset, NULL)))
150 offset += size;
151
152 while ((size = fdt_skip_node(blob, offset)))
153 offset += size;
154
155 return offset - start_offset + sizeof(uint32_t);
156}
157
158
159
160/*
161 * Functions to turn a flattened tree into an unflattened one.
162 */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200163
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200164static int fdt_unflatten_node(const void *blob, uint32_t start_offset,
Julius Werner6702b682019-05-03 18:13:53 -0700165 struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200166 struct device_tree_node **new_node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200167{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200168 struct list_node *last;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200169 int offset = start_offset;
170 const char *name;
171 int size;
172
173 size = fdt_node_name(blob, offset, &name);
174 if (!size)
175 return 0;
176 offset += size;
177
Julius Werner9636a102019-05-03 17:36:43 -0700178 struct device_tree_node *node = xzalloc(sizeof(*node));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200179 *new_node = node;
180 node->name = name;
181
Patrick Rudolph666c1722018-04-03 09:57:33 +0200182 struct fdt_property fprop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200183 last = &node->properties;
184 while ((size = fdt_next_property(blob, offset, &fprop))) {
Julius Werner9636a102019-05-03 17:36:43 -0700185 struct device_tree_property *prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200186 prop->prop = fprop;
187
Julius Werner6702b682019-05-03 18:13:53 -0700188 if (dt_prop_is_phandle(prop)) {
189 node->phandle = be32dec(prop->prop.data);
190 if (node->phandle > tree->max_phandle)
191 tree->max_phandle = node->phandle;
192 }
193
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200194 list_insert_after(&prop->list_node, last);
195 last = &prop->list_node;
196
197 offset += size;
198 }
199
Patrick Rudolph666c1722018-04-03 09:57:33 +0200200 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200201 last = &node->children;
Julius Werner6702b682019-05-03 18:13:53 -0700202 while ((size = fdt_unflatten_node(blob, offset, tree, &child))) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200203 list_insert_after(&child->list_node, last);
204 last = &child->list_node;
205
206 offset += size;
207 }
208
209 return offset - start_offset + sizeof(uint32_t);
210}
211
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200212static int fdt_unflatten_map_entry(const void *blob, uint32_t offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200213 struct device_tree_reserve_map_entry **new)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200214{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200215 const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset);
216 const uint64_t start = be64toh(ptr[0]);
217 const uint64_t size = be64toh(ptr[1]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200218
219 if (!size)
220 return 0;
221
Julius Werner9636a102019-05-03 17:36:43 -0700222 struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200223 *new = entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200224 entry->start = start;
225 entry->size = size;
226
227 return sizeof(uint64_t) * 2;
228}
229
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200230struct device_tree *fdt_unflatten(const void *blob)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200231{
Julius Werner9636a102019-05-03 17:36:43 -0700232 struct device_tree *tree = xzalloc(sizeof(*tree));
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200233 const struct fdt_header *header = (const struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200234 tree->header = header;
235
Julius Werner73eaec82019-05-03 17:58:07 -0700236 uint32_t magic = be32toh(header->magic);
237 uint32_t version = be32toh(header->version);
238 uint32_t last_comp_version = be32toh(header->last_comp_version);
239
240 if (magic != FDT_HEADER_MAGIC) {
241 printk(BIOS_DEBUG, "Invalid device tree magic %#.8x!\n", magic);
242 return NULL;
243 }
244 if (last_comp_version > FDT_SUPPORTED_VERSION) {
245 printk(BIOS_DEBUG, "Unsupported device tree version %u(>=%u)\n",
246 version, last_comp_version);
247 return NULL;
248 }
249 if (version > FDT_SUPPORTED_VERSION)
250 printk(BIOS_DEBUG,
251 "NOTE: FDT version %u too new, should add support!\n",
252 version);
253
Patrick Rudolph666c1722018-04-03 09:57:33 +0200254 uint32_t struct_offset = be32toh(header->structure_offset);
255 uint32_t strings_offset = be32toh(header->strings_offset);
256 uint32_t reserve_offset = be32toh(header->reserve_map_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200257 uint32_t min_offset = 0;
258 min_offset = MIN(struct_offset, strings_offset);
259 min_offset = MIN(min_offset, reserve_offset);
260 // Assume everything up to the first non-header component is part of
261 // the header and needs to be preserved. This will protect us against
262 // new elements being added in the future.
263 tree->header_size = min_offset;
264
Patrick Rudolph666c1722018-04-03 09:57:33 +0200265 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200266 uint32_t offset = reserve_offset;
267 int size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200268 struct list_node *last = &tree->reserve_map;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200269 while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) {
270 list_insert_after(&entry->list_node, last);
271 last = &entry->list_node;
272
273 offset += size;
274 }
275
Julius Werner6702b682019-05-03 18:13:53 -0700276 fdt_unflatten_node(blob, struct_offset, tree, &tree->root);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200277
278 return tree;
279}
280
281
282
283/*
Patrick Rudolph666c1722018-04-03 09:57:33 +0200284 * Functions to find the size of the device tree if it was flattened.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200285 */
286
Patrick Rudolph666c1722018-04-03 09:57:33 +0200287static void dt_flat_prop_size(struct device_tree_property *prop,
288 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200289{
290 // Starting token.
291 *struct_size += sizeof(uint32_t);
292 // Size.
293 *struct_size += sizeof(uint32_t);
294 // Name offset.
295 *struct_size += sizeof(uint32_t);
296 // Property value.
Patrick Rudolph666c1722018-04-03 09:57:33 +0200297 *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200298
299 // Property name.
300 *strings_size += strlen(prop->prop.name) + 1;
301}
302
Patrick Rudolph666c1722018-04-03 09:57:33 +0200303static void dt_flat_node_size(struct device_tree_node *node,
304 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200305{
306 // Starting token.
307 *struct_size += sizeof(uint32_t);
308 // Node name.
Patrick Rudolph666c1722018-04-03 09:57:33 +0200309 *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200310
Patrick Rudolph666c1722018-04-03 09:57:33 +0200311 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200312 list_for_each(prop, node->properties, list_node)
313 dt_flat_prop_size(prop, struct_size, strings_size);
314
Patrick Rudolph666c1722018-04-03 09:57:33 +0200315 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200316 list_for_each(child, node->children, list_node)
317 dt_flat_node_size(child, struct_size, strings_size);
318
319 // End token.
320 *struct_size += sizeof(uint32_t);
321}
322
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200323uint32_t dt_flat_size(const struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200324{
325 uint32_t size = tree->header_size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200326 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200327 list_for_each(entry, tree->reserve_map, list_node)
328 size += sizeof(uint64_t) * 2;
329 size += sizeof(uint64_t) * 2;
330
331 uint32_t struct_size = 0;
332 uint32_t strings_size = 0;
333 dt_flat_node_size(tree->root, &struct_size, &strings_size);
334
335 size += struct_size;
336 // End token.
337 size += sizeof(uint32_t);
338
339 size += strings_size;
340
341 return size;
342}
343
344
345
346/*
347 * Functions to flatten a device tree.
348 */
349
Patrick Rudolph666c1722018-04-03 09:57:33 +0200350static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200351 void **map_start)
352{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200353 ((uint64_t *)*map_start)[0] = htobe64(entry->start);
354 ((uint64_t *)*map_start)[1] = htobe64(entry->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200355 *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2;
356}
357
Patrick Rudolph666c1722018-04-03 09:57:33 +0200358static void dt_flatten_prop(struct device_tree_property *prop,
359 void **struct_start, void *strings_base,
360 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200361{
362 uint8_t *dstruct = (uint8_t *)*struct_start;
363 uint8_t *dstrings = (uint8_t *)*strings_start;
364
Julius Wernera5ea3a22019-05-07 17:38:12 -0700365 be32enc(dstruct, FDT_TOKEN_PROPERTY);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200366 dstruct += sizeof(uint32_t);
367
Julius Wernera5ea3a22019-05-07 17:38:12 -0700368 be32enc(dstruct, prop->prop.size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200369 dstruct += sizeof(uint32_t);
370
371 uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base;
Julius Wernera5ea3a22019-05-07 17:38:12 -0700372 be32enc(dstruct, name_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200373 dstruct += sizeof(uint32_t);
374
375 strcpy((char *)dstrings, prop->prop.name);
376 dstrings += strlen(prop->prop.name) + 1;
377
378 memcpy(dstruct, prop->prop.data, prop->prop.size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200379 dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200380
381 *struct_start = dstruct;
382 *strings_start = dstrings;
383}
384
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200385static void dt_flatten_node(const struct device_tree_node *node,
386 void **struct_start, void *strings_base,
387 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200388{
389 uint8_t *dstruct = (uint8_t *)*struct_start;
390 uint8_t *dstrings = (uint8_t *)*strings_start;
391
Julius Wernera5ea3a22019-05-07 17:38:12 -0700392 be32enc(dstruct, FDT_TOKEN_BEGIN_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200393 dstruct += sizeof(uint32_t);
394
395 strcpy((char *)dstruct, node->name);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200396 dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200397
Patrick Rudolph666c1722018-04-03 09:57:33 +0200398 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200399 list_for_each(prop, node->properties, list_node)
400 dt_flatten_prop(prop, (void **)&dstruct, strings_base,
401 (void **)&dstrings);
402
Patrick Rudolph666c1722018-04-03 09:57:33 +0200403 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200404 list_for_each(child, node->children, list_node)
405 dt_flatten_node(child, (void **)&dstruct, strings_base,
406 (void **)&dstrings);
407
Julius Wernera5ea3a22019-05-07 17:38:12 -0700408 be32enc(dstruct, FDT_TOKEN_END_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200409 dstruct += sizeof(uint32_t);
410
411 *struct_start = dstruct;
412 *strings_start = dstrings;
413}
414
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200415void dt_flatten(const struct device_tree *tree, void *start_dest)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200416{
417 uint8_t *dest = (uint8_t *)start_dest;
418
419 memcpy(dest, tree->header, tree->header_size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200420 struct fdt_header *header = (struct fdt_header *)dest;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200421 dest += tree->header_size;
422
Patrick Rudolph666c1722018-04-03 09:57:33 +0200423 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200424 list_for_each(entry, tree->reserve_map, list_node)
425 dt_flatten_map_entry(entry, (void **)&dest);
426 ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0;
427 dest += sizeof(uint64_t) * 2;
428
429 uint32_t struct_size = 0;
430 uint32_t strings_size = 0;
431 dt_flat_node_size(tree->root, &struct_size, &strings_size);
432
433 uint8_t *struct_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200434 header->structure_offset = htobe32(dest - (uint8_t *)start_dest);
435 header->structure_size = htobe32(struct_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200436 dest += struct_size;
437
Patrick Rudolph666c1722018-04-03 09:57:33 +0200438 *((uint32_t *)dest) = htobe32(FDT_TOKEN_END);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200439 dest += sizeof(uint32_t);
440
441 uint8_t *strings_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200442 header->strings_offset = htobe32(dest - (uint8_t *)start_dest);
443 header->strings_size = htobe32(strings_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200444 dest += strings_size;
445
446 dt_flatten_node(tree->root, (void **)&struct_start, strings_start,
447 (void **)&strings_start);
448
Patrick Rudolph666c1722018-04-03 09:57:33 +0200449 header->totalsize = htobe32(dest - (uint8_t *)start_dest);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200450}
451
452
453
454/*
455 * Functions for printing a non-flattened device tree.
456 */
457
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200458static void print_node(const struct device_tree_node *node, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200459{
460 print_indent(depth);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200461 printk(BIOS_DEBUG, "name = %s\n", node->name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200462
Patrick Rudolph666c1722018-04-03 09:57:33 +0200463 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200464 list_for_each(prop, node->properties, list_node)
465 print_property(&prop->prop, depth + 1);
466
Patrick Rudolph666c1722018-04-03 09:57:33 +0200467 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200468 list_for_each(child, node->children, list_node)
469 print_node(child, depth + 1);
470}
471
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200472void dt_print_node(const struct device_tree_node *node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200473{
474 print_node(node, 0);
475}
476
477
478
479/*
480 * Functions for reading and manipulating an unflattened device tree.
481 */
482
483/*
484 * Read #address-cells and #size-cells properties from a node.
485 *
486 * @param node The device tree node to read from.
487 * @param addrcp Pointer to store #address-cells in, skipped if NULL.
488 * @param sizecp Pointer to store #size-cells in, skipped if NULL.
489 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200490void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
491 u32 *sizecp)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200492{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200493 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200494 list_for_each(prop, node->properties, list_node) {
495 if (addrcp && !strcmp("#address-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700496 *addrcp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200497 if (sizecp && !strcmp("#size-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700498 *sizecp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200499 }
500}
501
502/*
503 * Find a node from a device tree path, relative to a parent node.
504 *
505 * @param parent The node from which to start the relative path lookup.
506 * @param path An array of path component strings that will be looked
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200507 * up in order to find the node. Must be terminated with
508 * a NULL pointer. Example: {'firmware', 'coreboot', NULL}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200509 * @param addrcp Pointer that will be updated with any #address-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200510 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200511 * @param sizecp Pointer that will be updated with any #size-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200512 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200513 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
514 * @return The found/created node, or NULL.
515 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200516struct device_tree_node *dt_find_node(struct device_tree_node *parent,
517 const char **path, u32 *addrcp,
518 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200519{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200520 struct device_tree_node *node, *found = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200521
522 // Update #address-cells and #size-cells for this level.
523 dt_read_cell_props(parent, addrcp, sizecp);
524
525 if (!*path)
526 return parent;
527
528 // Find the next node in the path, if it exists.
529 list_for_each(node, parent->children, list_node) {
530 if (!strcmp(node->name, *path)) {
531 found = node;
532 break;
533 }
534 }
535
536 // Otherwise create it or return NULL.
537 if (!found) {
538 if (!create)
539 return NULL;
540
Julius Werner9636a102019-05-03 17:36:43 -0700541 found = malloc(sizeof(*found));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200542 if (!found)
543 return NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200544 found->name = strdup(*path);
545 if (!found->name)
546 return NULL;
547
548 list_insert_after(&found->list_node, &parent->children);
549 }
550
551 return dt_find_node(found, path + 1, addrcp, sizecp, create);
552}
553
554/*
Julius Wernerf36d53c2019-05-03 18:23:34 -0700555 * Find a node in the tree from a string device tree path.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200556 *
Julius Wernerf36d53c2019-05-03 18:23:34 -0700557 * @param tree The device tree to search.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200558 * @param path A string representing a path in the device tree, with
Julius Wernerfbec63d2019-05-03 18:29:28 -0700559 * nodes separated by '/'. Example: "/firmware/coreboot"
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200560 * @param addrcp Pointer that will be updated with any #address-cells
561 * value found in the path. May be NULL to ignore.
562 * @param sizecp Pointer that will be updated with any #size-cells
563 * value found in the path. May be NULL to ignore.
564 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
565 * @return The found/created node, or NULL.
566 *
Julius Werner6d5695f2019-05-06 19:23:28 -0700567 * It is the caller responsibility to provide a path string that doesn't end
568 * with a '/' and doesn't contain any "//". If the path does not start with a
569 * '/', the first segment is interpreted as an alias. */
Julius Wernerf36d53c2019-05-03 18:23:34 -0700570struct device_tree_node *dt_find_node_by_path(struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200571 const char *path, u32 *addrcp,
572 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200573{
Julius Werner6d5695f2019-05-06 19:23:28 -0700574 char *sub_path;
575 char *duped_str;
576 struct device_tree_node *parent;
577 char *next_slash;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200578 /* Hopefully enough depth for any node. */
579 const char *path_array[15];
580 int i;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200581 struct device_tree_node *node = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200582
Julius Werner6d5695f2019-05-06 19:23:28 -0700583 if (path[0] == '/') { // regular path
584 if (path[1] == '\0') { // special case: "/" is root node
585 dt_read_cell_props(tree->root, addrcp, sizecp);
586 return tree->root;
587 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200588
Julius Werner6d5695f2019-05-06 19:23:28 -0700589 sub_path = duped_str = strdup(&path[1]);
590 if (!sub_path)
591 return NULL;
592
593 parent = tree->root;
594 } else { // alias
595 char *alias;
596
597 alias = duped_str = strdup(path);
598 if (!alias)
599 return NULL;
600
601 sub_path = strchr(alias, '/');
602 if (sub_path)
603 *sub_path = '\0';
604
605 parent = dt_find_node_by_alias(tree, alias);
606 if (!parent) {
607 printk(BIOS_DEBUG,
608 "Could not find node '%s', alias '%s' does not exist\n",
609 path, alias);
610 free(duped_str);
611 return NULL;
612 }
613
614 if (!sub_path) {
615 // it's just the alias, no sub-path
616 free(duped_str);
617 return parent;
618 }
619
620 sub_path++;
621 }
622
623 next_slash = sub_path;
624 path_array[0] = sub_path;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200625 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200626 next_slash = strchr(next_slash, '/');
627 if (!next_slash)
628 break;
629
630 *next_slash++ = '\0';
631 path_array[i] = next_slash;
632 }
633
634 if (!next_slash) {
635 path_array[i] = NULL;
Julius Werner6d5695f2019-05-06 19:23:28 -0700636 node = dt_find_node(parent, path_array,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200637 addrcp, sizecp, create);
638 }
639
Julius Werner6d5695f2019-05-06 19:23:28 -0700640 free(duped_str);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200641 return node;
642}
643
Julius Werner6d5695f2019-05-06 19:23:28 -0700644/*
645 * Find a node from an alias
646 *
647 * @param tree The device tree.
648 * @param alias The alias name.
649 * @return The found node, or NULL.
650 */
651struct device_tree_node *dt_find_node_by_alias(struct device_tree *tree,
652 const char *alias)
653{
654 struct device_tree_node *node;
655 const char *alias_path;
656
657 node = dt_find_node_by_path(tree, "/aliases", NULL, NULL, 0);
658 if (!node)
659 return NULL;
660
661 alias_path = dt_find_string_prop(node, alias);
662 if (!alias_path)
663 return NULL;
664
665 return dt_find_node_by_path(tree, alias_path, NULL, NULL, 0);
666}
667
Julius Werner6702b682019-05-03 18:13:53 -0700668struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root,
669 uint32_t phandle)
670{
671 if (!root)
672 return NULL;
673
674 if (root->phandle == phandle)
675 return root;
676
677 struct device_tree_node *node;
678 struct device_tree_node *result;
679 list_for_each(node, root->children, list_node) {
680 result = dt_find_node_by_phandle(node, phandle);
681 if (result)
682 return result;
683 }
684
685 return NULL;
686}
687
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200688/*
689 * Check if given node is compatible.
690 *
691 * @param node The node which is to be checked for compatible property.
692 * @param compat The compatible string to match.
693 * @return 1 = compatible, 0 = not compatible.
694 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200695static int dt_check_compat_match(struct device_tree_node *node,
696 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200697{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200698 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200699
700 list_for_each(prop, node->properties, list_node) {
701 if (!strcmp("compatible", prop->prop.name)) {
702 size_t bytes = prop->prop.size;
703 const char *str = prop->prop.data;
704 while (bytes > 0) {
705 if (!strncmp(compat, str, bytes))
706 return 1;
707 size_t len = strnlen(str, bytes) + 1;
708 if (bytes <= len)
709 break;
710 str += len;
711 bytes -= len;
712 }
713 break;
714 }
715 }
716
717 return 0;
718}
719
720/*
721 * Find a node from a compatible string, in the subtree of a parent node.
722 *
723 * @param parent The parent node under which to look.
724 * @param compat The compatible string to find.
725 * @return The found node, or NULL.
726 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200727struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
728 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200729{
730 // Check if the parent node itself is compatible.
731 if (dt_check_compat_match(parent, compat))
732 return parent;
733
Patrick Rudolph666c1722018-04-03 09:57:33 +0200734 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200735 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200736 struct device_tree_node *found = dt_find_compat(child, compat);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200737 if (found)
738 return found;
739 }
740
741 return NULL;
742}
743
744/*
745 * Find the next compatible child of a given parent. All children upto the
746 * child passed in by caller are ignored. If child is NULL, it considers all the
747 * children to find the first child which is compatible.
748 *
749 * @param parent The parent node under which to look.
750 * @param child The child node to start search from (exclusive). If NULL
751 * consider all children.
752 * @param compat The compatible string to find.
753 * @return The found node, or NULL.
754 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200755struct device_tree_node *
756dt_find_next_compat_child(struct device_tree_node *parent,
757 struct device_tree_node *child,
758 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200759{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200760 struct device_tree_node *next;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200761 int ignore = 0;
762
763 if (child)
764 ignore = 1;
765
766 list_for_each(next, parent->children, list_node) {
767 if (ignore) {
768 if (child == next)
769 ignore = 0;
770 continue;
771 }
772
773 if (dt_check_compat_match(next, compat))
774 return next;
775 }
776
777 return NULL;
778}
779
780/*
781 * Find a node with matching property value, in the subtree of a parent node.
782 *
783 * @param parent The parent node under which to look.
784 * @param name The property name to look for.
785 * @param data The property value to look for.
786 * @param size The property size.
787 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200788struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
789 const char *name, void *data,
790 size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200791{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200792 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200793
794 /* Check if parent itself has the required property value. */
795 list_for_each(prop, parent->properties, list_node) {
796 if (!strcmp(name, prop->prop.name)) {
797 size_t bytes = prop->prop.size;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200798 const void *prop_data = prop->prop.data;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200799 if (size != bytes)
800 break;
801 if (!memcmp(data, prop_data, size))
802 return parent;
803 break;
804 }
805 }
806
Patrick Rudolph666c1722018-04-03 09:57:33 +0200807 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200808 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200809 struct device_tree_node *found = dt_find_prop_value(child, name,
810 data, size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200811 if (found)
812 return found;
813 }
814 return NULL;
815}
816
817/*
818 * Write an arbitrary sized big-endian integer into a pointer.
819 *
820 * @param dest Pointer to the DT property data buffer to write.
Elyes HAOUAS1ec76442018-08-07 12:20:04 +0200821 * @param src The integer to write (in CPU endianness).
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200822 * @param length the length of the destination integer in bytes.
823 */
824void dt_write_int(u8 *dest, u64 src, size_t length)
825{
826 while (length--) {
827 dest[length] = (u8)src;
828 src >>= 8;
829 }
830}
831
832/*
Patrick Rudolph5ccc7312018-05-30 15:05:28 +0200833 * Delete a property by name in a given node if it exists.
834 *
835 * @param node The device tree node to operate on.
836 * @param name The name of the property to delete.
837 */
838void dt_delete_prop(struct device_tree_node *node, const char *name)
839{
840 struct device_tree_property *prop;
841
842 list_for_each(prop, node->properties, list_node) {
843 if (!strcmp(prop->prop.name, name)) {
844 list_remove(&prop->list_node);
845 return;
846 }
847 }
848}
849
850/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200851 * Add an arbitrary property to a node, or update it if it already exists.
852 *
853 * @param node The device tree node to add to.
854 * @param name The name of the new property.
855 * @param data The raw data blob to be stored in the property.
856 * @param size The size of data in bytes.
857 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200858void dt_add_bin_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200859 const void *data, size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200860{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200861 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200862
863 list_for_each(prop, node->properties, list_node) {
864 if (!strcmp(prop->prop.name, name)) {
865 prop->prop.data = data;
866 prop->prop.size = size;
867 return;
868 }
869 }
870
Julius Werner9636a102019-05-03 17:36:43 -0700871 prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200872 list_insert_after(&prop->list_node, &node->properties);
873 prop->prop.name = name;
874 prop->prop.data = data;
875 prop->prop.size = size;
876}
877
878/*
879 * Find given string property in a node and return its content.
880 *
881 * @param node The device tree node to search.
882 * @param name The name of the property.
883 * @return The found string, or NULL.
884 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200885const char *dt_find_string_prop(const struct device_tree_node *node,
886 const char *name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200887{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200888 const void *content;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200889 size_t size;
890
891 dt_find_bin_prop(node, name, &content, &size);
892
893 return content;
894}
895
896/*
897 * Find given property in a node.
898 *
899 * @param node The device tree node to search.
900 * @param name The name of the property.
901 * @param data Pointer to return raw data blob in the property.
902 * @param size Pointer to return the size of data in bytes.
903 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200904void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
905 const void **data, size_t *size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200906{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200907 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200908
909 *data = NULL;
910 *size = 0;
911
912 list_for_each(prop, node->properties, list_node) {
913 if (!strcmp(prop->prop.name, name)) {
914 *data = prop->prop.data;
915 *size = prop->prop.size;
916 return;
917 }
918 }
919}
920
921/*
922 * Add a string property to a node, or update it if it already exists.
923 *
924 * @param node The device tree node to add to.
925 * @param name The name of the new property.
926 * @param str The zero-terminated string to be stored in the property.
927 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200928void dt_add_string_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200929 const char *str)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200930{
931 dt_add_bin_prop(node, name, str, strlen(str) + 1);
932}
933
934/*
935 * Add a 32-bit integer property to a node, or update it if it already exists.
936 *
937 * @param node The device tree node to add to.
938 * @param name The name of the new property.
939 * @param val The integer to be stored in the property.
940 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200941void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200942{
Julius Werner9636a102019-05-03 17:36:43 -0700943 u32 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200944 *val_ptr = htobe32(val);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200945 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
946}
947
948/*
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200949 * Add a 64-bit integer property to a node, or update it if it already exists.
950 *
951 * @param node The device tree node to add to.
952 * @param name The name of the new property.
953 * @param val The integer to be stored in the property.
954 */
955void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
956{
Julius Werner9636a102019-05-03 17:36:43 -0700957 u64 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200958 *val_ptr = htobe64(val);
959 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
960}
961
962/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200963 * Add a 'reg' address list property to a node, or update it if it exists.
964 *
965 * @param node The device tree node to add to.
966 * @param addrs Array of address values to be stored in the property.
967 * @param sizes Array of corresponding size values to 'addrs'.
968 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
969 * @param addr_cells Value of #address-cells property valid for this node.
970 * @param size_cells Value of #size-cells property valid for this node.
971 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200972void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200973 int count, u32 addr_cells, u32 size_cells)
974{
975 int i;
976 size_t length = (addr_cells + size_cells) * sizeof(u32) * count;
Julius Werner9636a102019-05-03 17:36:43 -0700977 u8 *data = xmalloc(length);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200978 u8 *cur = data;
979
980 for (i = 0; i < count; i++) {
981 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
982 cur += addr_cells * sizeof(u32);
983 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
984 cur += size_cells * sizeof(u32);
985 }
986
987 dt_add_bin_prop(node, "reg", data, length);
988}
989
990/*
991 * Fixups to apply to a kernel's device tree before booting it.
992 */
993
Patrick Rudolph666c1722018-04-03 09:57:33 +0200994struct list_node device_tree_fixups;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200995
Patrick Rudolph666c1722018-04-03 09:57:33 +0200996int dt_apply_fixups(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200997{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200998 struct device_tree_fixup *fixup;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200999 list_for_each(fixup, device_tree_fixups, list_node) {
1000 assert(fixup->fixup);
1001 if (fixup->fixup(fixup, tree))
1002 return 1;
1003 }
1004 return 0;
1005}
1006
Patrick Rudolph666c1722018-04-03 09:57:33 +02001007int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001008 void *data, size_t data_size, int create)
1009{
1010 char *path_copy, *prop_name;
Patrick Rudolph666c1722018-04-03 09:57:33 +02001011 struct device_tree_node *dt_node;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001012
1013 path_copy = strdup(path);
1014
1015 if (!path_copy) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001016 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
1017 path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001018 return 1;
1019 }
1020
1021 prop_name = strrchr(path_copy, '/');
1022 if (!prop_name) {
Patrick Rudolph679d6242018-07-11 13:53:04 +02001023 free(path_copy);
Patrick Rudolph666c1722018-04-03 09:57:33 +02001024 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001025 return 1;
1026 }
1027
1028 *prop_name++ = '\0'; /* Separate path from the property name. */
1029
Julius Wernerf36d53c2019-05-03 18:23:34 -07001030 dt_node = dt_find_node_by_path(tree, path_copy, NULL,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001031 NULL, create);
1032
1033 if (!dt_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001034 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001035 create ? "create" : "find", path_copy);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001036 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001037 return 1;
1038 }
1039
1040 dt_add_bin_prop(dt_node, prop_name, data, data_size);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001041 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001042
1043 return 0;
1044}
1045
1046/*
1047 * Prepare the /reserved-memory/ node.
1048 *
1049 * Technically, this can be called more than one time, to init and/or retrieve
1050 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
1051 *
1052 * @tree: Device tree to add/retrieve from.
1053 * @return: The /reserved-memory/ node (or NULL, if error).
1054 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001055struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001056{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001057 struct device_tree_node *reserved;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001058 u32 addr = 0, size = 0;
1059
Julius Wernerfbec63d2019-05-03 18:29:28 -07001060 reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001061 &size, 1);
1062 if (!reserved)
1063 return NULL;
1064
1065 // Binding doc says this should have the same #{address,size}-cells as
1066 // the root.
1067 dt_add_u32_prop(reserved, "#address-cells", addr);
1068 dt_add_u32_prop(reserved, "#size-cells", size);
1069 // Binding doc says this should be empty (i.e., 1:1 mapping from root).
1070 dt_add_bin_prop(reserved, "ranges", NULL, 0);
1071
1072 return reserved;
1073}