blob: e26021e9ee2605341260f16ea6bad67f5fc1c670 [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 *
567 * It is the caller responsibility to provide the correct path string, namely
Julius Wernerfbec63d2019-05-03 18:29:28 -0700568 * starting with a '/', not ending in a '/' and not having "//" anywhere in it.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200569 */
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 Wernerfbec63d2019-05-03 18:29:28 -0700574 char *dup_path = strdup(&path[1]); /* remove leading '/' */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200575 /* Hopefully enough depth for any node. */
576 const char *path_array[15];
577 int i;
578 char *next_slash;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200579 struct device_tree_node *node = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200580
581 if (!dup_path)
582 return NULL;
583
584 next_slash = dup_path;
585 path_array[0] = dup_path;
586 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
587
588 next_slash = strchr(next_slash, '/');
589 if (!next_slash)
590 break;
591
592 *next_slash++ = '\0';
593 path_array[i] = next_slash;
594 }
595
596 if (!next_slash) {
597 path_array[i] = NULL;
Julius Wernerf36d53c2019-05-03 18:23:34 -0700598 node = dt_find_node(tree->root, path_array,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200599 addrcp, sizecp, create);
600 }
601
602 free(dup_path);
603 return node;
604}
605
Julius Werner6702b682019-05-03 18:13:53 -0700606struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root,
607 uint32_t phandle)
608{
609 if (!root)
610 return NULL;
611
612 if (root->phandle == phandle)
613 return root;
614
615 struct device_tree_node *node;
616 struct device_tree_node *result;
617 list_for_each(node, root->children, list_node) {
618 result = dt_find_node_by_phandle(node, phandle);
619 if (result)
620 return result;
621 }
622
623 return NULL;
624}
625
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200626/*
627 * Check if given node is compatible.
628 *
629 * @param node The node which is to be checked for compatible property.
630 * @param compat The compatible string to match.
631 * @return 1 = compatible, 0 = not compatible.
632 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200633static int dt_check_compat_match(struct device_tree_node *node,
634 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200635{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200636 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200637
638 list_for_each(prop, node->properties, list_node) {
639 if (!strcmp("compatible", prop->prop.name)) {
640 size_t bytes = prop->prop.size;
641 const char *str = prop->prop.data;
642 while (bytes > 0) {
643 if (!strncmp(compat, str, bytes))
644 return 1;
645 size_t len = strnlen(str, bytes) + 1;
646 if (bytes <= len)
647 break;
648 str += len;
649 bytes -= len;
650 }
651 break;
652 }
653 }
654
655 return 0;
656}
657
658/*
659 * Find a node from a compatible string, in the subtree of a parent node.
660 *
661 * @param parent The parent node under which to look.
662 * @param compat The compatible string to find.
663 * @return The found node, or NULL.
664 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200665struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
666 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200667{
668 // Check if the parent node itself is compatible.
669 if (dt_check_compat_match(parent, compat))
670 return parent;
671
Patrick Rudolph666c1722018-04-03 09:57:33 +0200672 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200673 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200674 struct device_tree_node *found = dt_find_compat(child, compat);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200675 if (found)
676 return found;
677 }
678
679 return NULL;
680}
681
682/*
683 * Find the next compatible child of a given parent. All children upto the
684 * child passed in by caller are ignored. If child is NULL, it considers all the
685 * children to find the first child which is compatible.
686 *
687 * @param parent The parent node under which to look.
688 * @param child The child node to start search from (exclusive). If NULL
689 * consider all children.
690 * @param compat The compatible string to find.
691 * @return The found node, or NULL.
692 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200693struct device_tree_node *
694dt_find_next_compat_child(struct device_tree_node *parent,
695 struct device_tree_node *child,
696 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200697{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200698 struct device_tree_node *next;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200699 int ignore = 0;
700
701 if (child)
702 ignore = 1;
703
704 list_for_each(next, parent->children, list_node) {
705 if (ignore) {
706 if (child == next)
707 ignore = 0;
708 continue;
709 }
710
711 if (dt_check_compat_match(next, compat))
712 return next;
713 }
714
715 return NULL;
716}
717
718/*
719 * Find a node with matching property value, in the subtree of a parent node.
720 *
721 * @param parent The parent node under which to look.
722 * @param name The property name to look for.
723 * @param data The property value to look for.
724 * @param size The property size.
725 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200726struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
727 const char *name, void *data,
728 size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200729{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200730 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200731
732 /* Check if parent itself has the required property value. */
733 list_for_each(prop, parent->properties, list_node) {
734 if (!strcmp(name, prop->prop.name)) {
735 size_t bytes = prop->prop.size;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200736 const void *prop_data = prop->prop.data;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200737 if (size != bytes)
738 break;
739 if (!memcmp(data, prop_data, size))
740 return parent;
741 break;
742 }
743 }
744
Patrick Rudolph666c1722018-04-03 09:57:33 +0200745 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200746 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200747 struct device_tree_node *found = dt_find_prop_value(child, name,
748 data, size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200749 if (found)
750 return found;
751 }
752 return NULL;
753}
754
755/*
756 * Write an arbitrary sized big-endian integer into a pointer.
757 *
758 * @param dest Pointer to the DT property data buffer to write.
Elyes HAOUAS1ec76442018-08-07 12:20:04 +0200759 * @param src The integer to write (in CPU endianness).
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200760 * @param length the length of the destination integer in bytes.
761 */
762void dt_write_int(u8 *dest, u64 src, size_t length)
763{
764 while (length--) {
765 dest[length] = (u8)src;
766 src >>= 8;
767 }
768}
769
770/*
Patrick Rudolph5ccc7312018-05-30 15:05:28 +0200771 * Delete a property by name in a given node if it exists.
772 *
773 * @param node The device tree node to operate on.
774 * @param name The name of the property to delete.
775 */
776void dt_delete_prop(struct device_tree_node *node, const char *name)
777{
778 struct device_tree_property *prop;
779
780 list_for_each(prop, node->properties, list_node) {
781 if (!strcmp(prop->prop.name, name)) {
782 list_remove(&prop->list_node);
783 return;
784 }
785 }
786}
787
788/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200789 * Add an arbitrary property to a node, or update it if it already exists.
790 *
791 * @param node The device tree node to add to.
792 * @param name The name of the new property.
793 * @param data The raw data blob to be stored in the property.
794 * @param size The size of data in bytes.
795 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200796void dt_add_bin_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200797 const void *data, size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200798{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200799 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200800
801 list_for_each(prop, node->properties, list_node) {
802 if (!strcmp(prop->prop.name, name)) {
803 prop->prop.data = data;
804 prop->prop.size = size;
805 return;
806 }
807 }
808
Julius Werner9636a102019-05-03 17:36:43 -0700809 prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200810 list_insert_after(&prop->list_node, &node->properties);
811 prop->prop.name = name;
812 prop->prop.data = data;
813 prop->prop.size = size;
814}
815
816/*
817 * Find given string property in a node and return its content.
818 *
819 * @param node The device tree node to search.
820 * @param name The name of the property.
821 * @return The found string, or NULL.
822 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200823const char *dt_find_string_prop(const struct device_tree_node *node,
824 const char *name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200825{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200826 const void *content;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200827 size_t size;
828
829 dt_find_bin_prop(node, name, &content, &size);
830
831 return content;
832}
833
834/*
835 * Find given property in a node.
836 *
837 * @param node The device tree node to search.
838 * @param name The name of the property.
839 * @param data Pointer to return raw data blob in the property.
840 * @param size Pointer to return the size of data in bytes.
841 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200842void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
843 const void **data, size_t *size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200844{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200845 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200846
847 *data = NULL;
848 *size = 0;
849
850 list_for_each(prop, node->properties, list_node) {
851 if (!strcmp(prop->prop.name, name)) {
852 *data = prop->prop.data;
853 *size = prop->prop.size;
854 return;
855 }
856 }
857}
858
859/*
860 * Add a string property to a node, or update it if it already exists.
861 *
862 * @param node The device tree node to add to.
863 * @param name The name of the new property.
864 * @param str The zero-terminated string to be stored in the property.
865 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200866void dt_add_string_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200867 const char *str)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200868{
869 dt_add_bin_prop(node, name, str, strlen(str) + 1);
870}
871
872/*
873 * Add a 32-bit integer property to a node, or update it if it already exists.
874 *
875 * @param node The device tree node to add to.
876 * @param name The name of the new property.
877 * @param val The integer to be stored in the property.
878 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200879void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200880{
Julius Werner9636a102019-05-03 17:36:43 -0700881 u32 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200882 *val_ptr = htobe32(val);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200883 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
884}
885
886/*
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200887 * Add a 64-bit integer property to a node, or update it if it already exists.
888 *
889 * @param node The device tree node to add to.
890 * @param name The name of the new property.
891 * @param val The integer to be stored in the property.
892 */
893void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
894{
Julius Werner9636a102019-05-03 17:36:43 -0700895 u64 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200896 *val_ptr = htobe64(val);
897 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
898}
899
900/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200901 * Add a 'reg' address list property to a node, or update it if it exists.
902 *
903 * @param node The device tree node to add to.
904 * @param addrs Array of address values to be stored in the property.
905 * @param sizes Array of corresponding size values to 'addrs'.
906 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
907 * @param addr_cells Value of #address-cells property valid for this node.
908 * @param size_cells Value of #size-cells property valid for this node.
909 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200910void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200911 int count, u32 addr_cells, u32 size_cells)
912{
913 int i;
914 size_t length = (addr_cells + size_cells) * sizeof(u32) * count;
Julius Werner9636a102019-05-03 17:36:43 -0700915 u8 *data = xmalloc(length);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200916 u8 *cur = data;
917
918 for (i = 0; i < count; i++) {
919 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
920 cur += addr_cells * sizeof(u32);
921 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
922 cur += size_cells * sizeof(u32);
923 }
924
925 dt_add_bin_prop(node, "reg", data, length);
926}
927
928/*
929 * Fixups to apply to a kernel's device tree before booting it.
930 */
931
Patrick Rudolph666c1722018-04-03 09:57:33 +0200932struct list_node device_tree_fixups;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200933
Patrick Rudolph666c1722018-04-03 09:57:33 +0200934int dt_apply_fixups(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200935{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200936 struct device_tree_fixup *fixup;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200937 list_for_each(fixup, device_tree_fixups, list_node) {
938 assert(fixup->fixup);
939 if (fixup->fixup(fixup, tree))
940 return 1;
941 }
942 return 0;
943}
944
Patrick Rudolph666c1722018-04-03 09:57:33 +0200945int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200946 void *data, size_t data_size, int create)
947{
948 char *path_copy, *prop_name;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200949 struct device_tree_node *dt_node;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200950
951 path_copy = strdup(path);
952
953 if (!path_copy) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200954 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
955 path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200956 return 1;
957 }
958
959 prop_name = strrchr(path_copy, '/');
960 if (!prop_name) {
Patrick Rudolph679d6242018-07-11 13:53:04 +0200961 free(path_copy);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200962 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200963 return 1;
964 }
965
966 *prop_name++ = '\0'; /* Separate path from the property name. */
967
Julius Wernerf36d53c2019-05-03 18:23:34 -0700968 dt_node = dt_find_node_by_path(tree, path_copy, NULL,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200969 NULL, create);
970
971 if (!dt_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200972 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200973 create ? "create" : "find", path_copy);
Patrick Rudolph679d6242018-07-11 13:53:04 +0200974 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200975 return 1;
976 }
977
978 dt_add_bin_prop(dt_node, prop_name, data, data_size);
Patrick Rudolph679d6242018-07-11 13:53:04 +0200979 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200980
981 return 0;
982}
983
984/*
985 * Prepare the /reserved-memory/ node.
986 *
987 * Technically, this can be called more than one time, to init and/or retrieve
988 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
989 *
990 * @tree: Device tree to add/retrieve from.
991 * @return: The /reserved-memory/ node (or NULL, if error).
992 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200993struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200994{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200995 struct device_tree_node *reserved;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200996 u32 addr = 0, size = 0;
997
Julius Wernerfbec63d2019-05-03 18:29:28 -0700998 reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200999 &size, 1);
1000 if (!reserved)
1001 return NULL;
1002
1003 // Binding doc says this should have the same #{address,size}-cells as
1004 // the root.
1005 dt_add_u32_prop(reserved, "#address-cells", addr);
1006 dt_add_u32_prop(reserved, "#size-cells", size);
1007 // Binding doc says this should be empty (i.e., 1:1 mapping from root).
1008 dt_add_bin_prop(reserved, "ranges", NULL, 0);
1009
1010 return reserved;
1011}