blob: 7a3128efcf7e4202ecb9e68752694e558579bfe5 [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
69
70
71/*
72 * Functions for printing flattened trees.
73 */
74
75static void print_indent(int depth)
76{
77 while (depth--)
Patrick Rudolph666c1722018-04-03 09:57:33 +020078 printk(BIOS_DEBUG, " ");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020079}
80
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020081static void print_property(const struct fdt_property *prop, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020082{
83 print_indent(depth);
Patrick Rudolph666c1722018-04-03 09:57:33 +020084 printk(BIOS_DEBUG, "prop \"%s\" (%d bytes).\n", prop->name, prop->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020085 print_indent(depth + 1);
86 for (int i = 0; i < MIN(25, prop->size); i++) {
Patrick Rudolph666c1722018-04-03 09:57:33 +020087 printk(BIOS_DEBUG, "%02x ", ((uint8_t *)prop->data)[i]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020088 }
89 if (prop->size > 25)
Patrick Rudolph666c1722018-04-03 09:57:33 +020090 printk(BIOS_DEBUG, "...");
91 printk(BIOS_DEBUG, "\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020092}
93
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020094static int print_flat_node(const void *blob, uint32_t start_offset, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020095{
96 int offset = start_offset;
97 const char *name;
98 int size;
99
100 size = fdt_node_name(blob, offset, &name);
101 if (!size)
102 return 0;
103 offset += size;
104
105 print_indent(depth);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200106 printk(BIOS_DEBUG, "name = %s\n", name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200107
Patrick Rudolph666c1722018-04-03 09:57:33 +0200108 struct fdt_property prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200109 while ((size = fdt_next_property(blob, offset, &prop))) {
110 print_property(&prop, depth + 1);
111
112 offset += size;
113 }
114
115 while ((size = print_flat_node(blob, offset, depth + 1)))
116 offset += size;
117
118 return offset - start_offset + sizeof(uint32_t);
119}
120
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200121void fdt_print_node(const void *blob, uint32_t offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200122{
123 print_flat_node(blob, offset, 0);
124}
125
126
127
128/*
129 * A utility function to skip past nodes in flattened trees.
130 */
131
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200132int fdt_skip_node(const void *blob, uint32_t start_offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200133{
134 int offset = start_offset;
135 int size;
136
137 const char *name;
138 size = fdt_node_name(blob, offset, &name);
139 if (!size)
140 return 0;
141 offset += size;
142
143 while ((size = fdt_next_property(blob, offset, NULL)))
144 offset += size;
145
146 while ((size = fdt_skip_node(blob, offset)))
147 offset += size;
148
149 return offset - start_offset + sizeof(uint32_t);
150}
151
152
153
154/*
155 * Functions to turn a flattened tree into an unflattened one.
156 */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200157
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200158static int fdt_unflatten_node(const void *blob, uint32_t start_offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200159 struct device_tree_node **new_node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200160{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200161 struct list_node *last;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200162 int offset = start_offset;
163 const char *name;
164 int size;
165
166 size = fdt_node_name(blob, offset, &name);
167 if (!size)
168 return 0;
169 offset += size;
170
Julius Werner9636a102019-05-03 17:36:43 -0700171 struct device_tree_node *node = xzalloc(sizeof(*node));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200172 *new_node = node;
173 node->name = name;
174
Patrick Rudolph666c1722018-04-03 09:57:33 +0200175 struct fdt_property fprop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200176 last = &node->properties;
177 while ((size = fdt_next_property(blob, offset, &fprop))) {
Julius Werner9636a102019-05-03 17:36:43 -0700178 struct device_tree_property *prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200179 prop->prop = fprop;
180
181 list_insert_after(&prop->list_node, last);
182 last = &prop->list_node;
183
184 offset += size;
185 }
186
Patrick Rudolph666c1722018-04-03 09:57:33 +0200187 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200188 last = &node->children;
189 while ((size = fdt_unflatten_node(blob, offset, &child))) {
190 list_insert_after(&child->list_node, last);
191 last = &child->list_node;
192
193 offset += size;
194 }
195
196 return offset - start_offset + sizeof(uint32_t);
197}
198
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200199static int fdt_unflatten_map_entry(const void *blob, uint32_t offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200200 struct device_tree_reserve_map_entry **new)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200201{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200202 const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset);
203 const uint64_t start = be64toh(ptr[0]);
204 const uint64_t size = be64toh(ptr[1]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200205
206 if (!size)
207 return 0;
208
Julius Werner9636a102019-05-03 17:36:43 -0700209 struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200210 *new = entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200211 entry->start = start;
212 entry->size = size;
213
214 return sizeof(uint64_t) * 2;
215}
216
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200217struct device_tree *fdt_unflatten(const void *blob)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200218{
Julius Werner9636a102019-05-03 17:36:43 -0700219 struct device_tree *tree = xzalloc(sizeof(*tree));
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200220 const struct fdt_header *header = (const struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200221 tree->header = header;
222
Julius Werner73eaec82019-05-03 17:58:07 -0700223 uint32_t magic = be32toh(header->magic);
224 uint32_t version = be32toh(header->version);
225 uint32_t last_comp_version = be32toh(header->last_comp_version);
226
227 if (magic != FDT_HEADER_MAGIC) {
228 printk(BIOS_DEBUG, "Invalid device tree magic %#.8x!\n", magic);
229 return NULL;
230 }
231 if (last_comp_version > FDT_SUPPORTED_VERSION) {
232 printk(BIOS_DEBUG, "Unsupported device tree version %u(>=%u)\n",
233 version, last_comp_version);
234 return NULL;
235 }
236 if (version > FDT_SUPPORTED_VERSION)
237 printk(BIOS_DEBUG,
238 "NOTE: FDT version %u too new, should add support!\n",
239 version);
240
Patrick Rudolph666c1722018-04-03 09:57:33 +0200241 uint32_t struct_offset = be32toh(header->structure_offset);
242 uint32_t strings_offset = be32toh(header->strings_offset);
243 uint32_t reserve_offset = be32toh(header->reserve_map_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200244 uint32_t min_offset = 0;
245 min_offset = MIN(struct_offset, strings_offset);
246 min_offset = MIN(min_offset, reserve_offset);
247 // Assume everything up to the first non-header component is part of
248 // the header and needs to be preserved. This will protect us against
249 // new elements being added in the future.
250 tree->header_size = min_offset;
251
Patrick Rudolph666c1722018-04-03 09:57:33 +0200252 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200253 uint32_t offset = reserve_offset;
254 int size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200255 struct list_node *last = &tree->reserve_map;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200256 while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) {
257 list_insert_after(&entry->list_node, last);
258 last = &entry->list_node;
259
260 offset += size;
261 }
262
263 fdt_unflatten_node(blob, struct_offset, &tree->root);
264
265 return tree;
266}
267
268
269
270/*
Patrick Rudolph666c1722018-04-03 09:57:33 +0200271 * Functions to find the size of the device tree if it was flattened.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200272 */
273
Patrick Rudolph666c1722018-04-03 09:57:33 +0200274static void dt_flat_prop_size(struct device_tree_property *prop,
275 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200276{
277 // Starting token.
278 *struct_size += sizeof(uint32_t);
279 // Size.
280 *struct_size += sizeof(uint32_t);
281 // Name offset.
282 *struct_size += sizeof(uint32_t);
283 // Property value.
Patrick Rudolph666c1722018-04-03 09:57:33 +0200284 *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200285
286 // Property name.
287 *strings_size += strlen(prop->prop.name) + 1;
288}
289
Patrick Rudolph666c1722018-04-03 09:57:33 +0200290static void dt_flat_node_size(struct device_tree_node *node,
291 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200292{
293 // Starting token.
294 *struct_size += sizeof(uint32_t);
295 // Node name.
Patrick Rudolph666c1722018-04-03 09:57:33 +0200296 *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200297
Patrick Rudolph666c1722018-04-03 09:57:33 +0200298 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200299 list_for_each(prop, node->properties, list_node)
300 dt_flat_prop_size(prop, struct_size, strings_size);
301
Patrick Rudolph666c1722018-04-03 09:57:33 +0200302 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200303 list_for_each(child, node->children, list_node)
304 dt_flat_node_size(child, struct_size, strings_size);
305
306 // End token.
307 *struct_size += sizeof(uint32_t);
308}
309
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200310uint32_t dt_flat_size(const struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200311{
312 uint32_t size = tree->header_size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200313 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200314 list_for_each(entry, tree->reserve_map, list_node)
315 size += sizeof(uint64_t) * 2;
316 size += sizeof(uint64_t) * 2;
317
318 uint32_t struct_size = 0;
319 uint32_t strings_size = 0;
320 dt_flat_node_size(tree->root, &struct_size, &strings_size);
321
322 size += struct_size;
323 // End token.
324 size += sizeof(uint32_t);
325
326 size += strings_size;
327
328 return size;
329}
330
331
332
333/*
334 * Functions to flatten a device tree.
335 */
336
Patrick Rudolph666c1722018-04-03 09:57:33 +0200337static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200338 void **map_start)
339{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200340 ((uint64_t *)*map_start)[0] = htobe64(entry->start);
341 ((uint64_t *)*map_start)[1] = htobe64(entry->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200342 *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2;
343}
344
Patrick Rudolph666c1722018-04-03 09:57:33 +0200345static void dt_flatten_prop(struct device_tree_property *prop,
346 void **struct_start, void *strings_base,
347 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200348{
349 uint8_t *dstruct = (uint8_t *)*struct_start;
350 uint8_t *dstrings = (uint8_t *)*strings_start;
351
Julius Wernera5ea3a22019-05-07 17:38:12 -0700352 be32enc(dstruct, FDT_TOKEN_PROPERTY);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200353 dstruct += sizeof(uint32_t);
354
Julius Wernera5ea3a22019-05-07 17:38:12 -0700355 be32enc(dstruct, prop->prop.size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200356 dstruct += sizeof(uint32_t);
357
358 uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base;
Julius Wernera5ea3a22019-05-07 17:38:12 -0700359 be32enc(dstruct, name_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200360 dstruct += sizeof(uint32_t);
361
362 strcpy((char *)dstrings, prop->prop.name);
363 dstrings += strlen(prop->prop.name) + 1;
364
365 memcpy(dstruct, prop->prop.data, prop->prop.size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200366 dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200367
368 *struct_start = dstruct;
369 *strings_start = dstrings;
370}
371
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200372static void dt_flatten_node(const struct device_tree_node *node,
373 void **struct_start, void *strings_base,
374 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200375{
376 uint8_t *dstruct = (uint8_t *)*struct_start;
377 uint8_t *dstrings = (uint8_t *)*strings_start;
378
Julius Wernera5ea3a22019-05-07 17:38:12 -0700379 be32enc(dstruct, FDT_TOKEN_BEGIN_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200380 dstruct += sizeof(uint32_t);
381
382 strcpy((char *)dstruct, node->name);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200383 dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200384
Patrick Rudolph666c1722018-04-03 09:57:33 +0200385 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200386 list_for_each(prop, node->properties, list_node)
387 dt_flatten_prop(prop, (void **)&dstruct, strings_base,
388 (void **)&dstrings);
389
Patrick Rudolph666c1722018-04-03 09:57:33 +0200390 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200391 list_for_each(child, node->children, list_node)
392 dt_flatten_node(child, (void **)&dstruct, strings_base,
393 (void **)&dstrings);
394
Julius Wernera5ea3a22019-05-07 17:38:12 -0700395 be32enc(dstruct, FDT_TOKEN_END_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200396 dstruct += sizeof(uint32_t);
397
398 *struct_start = dstruct;
399 *strings_start = dstrings;
400}
401
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200402void dt_flatten(const struct device_tree *tree, void *start_dest)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200403{
404 uint8_t *dest = (uint8_t *)start_dest;
405
406 memcpy(dest, tree->header, tree->header_size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200407 struct fdt_header *header = (struct fdt_header *)dest;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200408 dest += tree->header_size;
409
Patrick Rudolph666c1722018-04-03 09:57:33 +0200410 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200411 list_for_each(entry, tree->reserve_map, list_node)
412 dt_flatten_map_entry(entry, (void **)&dest);
413 ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0;
414 dest += sizeof(uint64_t) * 2;
415
416 uint32_t struct_size = 0;
417 uint32_t strings_size = 0;
418 dt_flat_node_size(tree->root, &struct_size, &strings_size);
419
420 uint8_t *struct_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200421 header->structure_offset = htobe32(dest - (uint8_t *)start_dest);
422 header->structure_size = htobe32(struct_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200423 dest += struct_size;
424
Patrick Rudolph666c1722018-04-03 09:57:33 +0200425 *((uint32_t *)dest) = htobe32(FDT_TOKEN_END);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200426 dest += sizeof(uint32_t);
427
428 uint8_t *strings_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200429 header->strings_offset = htobe32(dest - (uint8_t *)start_dest);
430 header->strings_size = htobe32(strings_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200431 dest += strings_size;
432
433 dt_flatten_node(tree->root, (void **)&struct_start, strings_start,
434 (void **)&strings_start);
435
Patrick Rudolph666c1722018-04-03 09:57:33 +0200436 header->totalsize = htobe32(dest - (uint8_t *)start_dest);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200437}
438
439
440
441/*
442 * Functions for printing a non-flattened device tree.
443 */
444
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200445static void print_node(const struct device_tree_node *node, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200446{
447 print_indent(depth);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200448 printk(BIOS_DEBUG, "name = %s\n", node->name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200449
Patrick Rudolph666c1722018-04-03 09:57:33 +0200450 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200451 list_for_each(prop, node->properties, list_node)
452 print_property(&prop->prop, depth + 1);
453
Patrick Rudolph666c1722018-04-03 09:57:33 +0200454 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200455 list_for_each(child, node->children, list_node)
456 print_node(child, depth + 1);
457}
458
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200459void dt_print_node(const struct device_tree_node *node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200460{
461 print_node(node, 0);
462}
463
464
465
466/*
467 * Functions for reading and manipulating an unflattened device tree.
468 */
469
470/*
471 * Read #address-cells and #size-cells properties from a node.
472 *
473 * @param node The device tree node to read from.
474 * @param addrcp Pointer to store #address-cells in, skipped if NULL.
475 * @param sizecp Pointer to store #size-cells in, skipped if NULL.
476 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200477void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
478 u32 *sizecp)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200479{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200480 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200481 list_for_each(prop, node->properties, list_node) {
482 if (addrcp && !strcmp("#address-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700483 *addrcp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200484 if (sizecp && !strcmp("#size-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700485 *sizecp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200486 }
487}
488
489/*
490 * Find a node from a device tree path, relative to a parent node.
491 *
492 * @param parent The node from which to start the relative path lookup.
493 * @param path An array of path component strings that will be looked
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200494 * up in order to find the node. Must be terminated with
495 * a NULL pointer. Example: {'firmware', 'coreboot', NULL}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200496 * @param addrcp Pointer that will be updated with any #address-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200497 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200498 * @param sizecp Pointer that will be updated with any #size-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200499 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200500 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
501 * @return The found/created node, or NULL.
502 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200503struct device_tree_node *dt_find_node(struct device_tree_node *parent,
504 const char **path, u32 *addrcp,
505 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200506{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200507 struct device_tree_node *node, *found = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200508
509 // Update #address-cells and #size-cells for this level.
510 dt_read_cell_props(parent, addrcp, sizecp);
511
512 if (!*path)
513 return parent;
514
515 // Find the next node in the path, if it exists.
516 list_for_each(node, parent->children, list_node) {
517 if (!strcmp(node->name, *path)) {
518 found = node;
519 break;
520 }
521 }
522
523 // Otherwise create it or return NULL.
524 if (!found) {
525 if (!create)
526 return NULL;
527
Julius Werner9636a102019-05-03 17:36:43 -0700528 found = malloc(sizeof(*found));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200529 if (!found)
530 return NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200531 found->name = strdup(*path);
532 if (!found->name)
533 return NULL;
534
535 list_insert_after(&found->list_node, &parent->children);
536 }
537
538 return dt_find_node(found, path + 1, addrcp, sizecp, create);
539}
540
541/*
542 * Find a node from a string device tree path, relative to a parent node.
543 *
544 * @param parent The node from which to start the relative path lookup.
545 * @param path A string representing a path in the device tree, with
546 * nodes separated by '/'. Example: "soc/firmware/coreboot"
547 * @param addrcp Pointer that will be updated with any #address-cells
548 * value found in the path. May be NULL to ignore.
549 * @param sizecp Pointer that will be updated with any #size-cells
550 * value found in the path. May be NULL to ignore.
551 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
552 * @return The found/created node, or NULL.
553 *
554 * It is the caller responsibility to provide the correct path string, namely
555 * not starting or ending with a '/', and not having "//" anywhere in it.
556 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200557struct device_tree_node *dt_find_node_by_path(struct device_tree_node *parent,
558 const char *path, u32 *addrcp,
559 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200560{
561 char *dup_path = strdup(path);
562 /* Hopefully enough depth for any node. */
563 const char *path_array[15];
564 int i;
565 char *next_slash;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200566 struct device_tree_node *node = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200567
568 if (!dup_path)
569 return NULL;
570
571 next_slash = dup_path;
572 path_array[0] = dup_path;
573 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
574
575 next_slash = strchr(next_slash, '/');
576 if (!next_slash)
577 break;
578
579 *next_slash++ = '\0';
580 path_array[i] = next_slash;
581 }
582
583 if (!next_slash) {
584 path_array[i] = NULL;
585 node = dt_find_node(parent, path_array,
586 addrcp, sizecp, create);
587 }
588
589 free(dup_path);
590 return node;
591}
592
593/*
594 * Check if given node is compatible.
595 *
596 * @param node The node which is to be checked for compatible property.
597 * @param compat The compatible string to match.
598 * @return 1 = compatible, 0 = not compatible.
599 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200600static int dt_check_compat_match(struct device_tree_node *node,
601 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200602{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200603 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200604
605 list_for_each(prop, node->properties, list_node) {
606 if (!strcmp("compatible", prop->prop.name)) {
607 size_t bytes = prop->prop.size;
608 const char *str = prop->prop.data;
609 while (bytes > 0) {
610 if (!strncmp(compat, str, bytes))
611 return 1;
612 size_t len = strnlen(str, bytes) + 1;
613 if (bytes <= len)
614 break;
615 str += len;
616 bytes -= len;
617 }
618 break;
619 }
620 }
621
622 return 0;
623}
624
625/*
626 * Find a node from a compatible string, in the subtree of a parent node.
627 *
628 * @param parent The parent node under which to look.
629 * @param compat The compatible string to find.
630 * @return The found node, or NULL.
631 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200632struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
633 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200634{
635 // Check if the parent node itself is compatible.
636 if (dt_check_compat_match(parent, compat))
637 return parent;
638
Patrick Rudolph666c1722018-04-03 09:57:33 +0200639 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200640 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200641 struct device_tree_node *found = dt_find_compat(child, compat);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200642 if (found)
643 return found;
644 }
645
646 return NULL;
647}
648
649/*
650 * Find the next compatible child of a given parent. All children upto the
651 * child passed in by caller are ignored. If child is NULL, it considers all the
652 * children to find the first child which is compatible.
653 *
654 * @param parent The parent node under which to look.
655 * @param child The child node to start search from (exclusive). If NULL
656 * consider all children.
657 * @param compat The compatible string to find.
658 * @return The found node, or NULL.
659 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200660struct device_tree_node *
661dt_find_next_compat_child(struct device_tree_node *parent,
662 struct device_tree_node *child,
663 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200664{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200665 struct device_tree_node *next;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200666 int ignore = 0;
667
668 if (child)
669 ignore = 1;
670
671 list_for_each(next, parent->children, list_node) {
672 if (ignore) {
673 if (child == next)
674 ignore = 0;
675 continue;
676 }
677
678 if (dt_check_compat_match(next, compat))
679 return next;
680 }
681
682 return NULL;
683}
684
685/*
686 * Find a node with matching property value, in the subtree of a parent node.
687 *
688 * @param parent The parent node under which to look.
689 * @param name The property name to look for.
690 * @param data The property value to look for.
691 * @param size The property size.
692 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200693struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
694 const char *name, void *data,
695 size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200696{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200697 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200698
699 /* Check if parent itself has the required property value. */
700 list_for_each(prop, parent->properties, list_node) {
701 if (!strcmp(name, prop->prop.name)) {
702 size_t bytes = prop->prop.size;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200703 const void *prop_data = prop->prop.data;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200704 if (size != bytes)
705 break;
706 if (!memcmp(data, prop_data, size))
707 return parent;
708 break;
709 }
710 }
711
Patrick Rudolph666c1722018-04-03 09:57:33 +0200712 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200713 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200714 struct device_tree_node *found = dt_find_prop_value(child, name,
715 data, size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200716 if (found)
717 return found;
718 }
719 return NULL;
720}
721
Patrick Rudolphc38960d2018-05-08 11:18:54 +0200722/**
723 * Find the phandle of a node.
724 *
725 * @param node Pointer to node containing the phandle
726 * @return Zero on error, the phandle on success
727 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200728uint32_t dt_get_phandle(const struct device_tree_node *node)
Patrick Rudolphc38960d2018-05-08 11:18:54 +0200729{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200730 const uint32_t *phandle;
Patrick Rudolphc38960d2018-05-08 11:18:54 +0200731 size_t len;
732
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200733 dt_find_bin_prop(node, "phandle", (const void **)&phandle, &len);
Patrick Rudolphc38960d2018-05-08 11:18:54 +0200734 if (phandle != NULL && len == sizeof(*phandle))
735 return be32_to_cpu(*phandle);
736
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200737 dt_find_bin_prop(node, "linux,phandle", (const void **)&phandle, &len);
Patrick Rudolphc38960d2018-05-08 11:18:54 +0200738 if (phandle != NULL && len == sizeof(*phandle))
739 return be32_to_cpu(*phandle);
740
741 return 0;
742}
743
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200744/*
745 * Write an arbitrary sized big-endian integer into a pointer.
746 *
747 * @param dest Pointer to the DT property data buffer to write.
Elyes HAOUAS1ec76442018-08-07 12:20:04 +0200748 * @param src The integer to write (in CPU endianness).
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200749 * @param length the length of the destination integer in bytes.
750 */
751void dt_write_int(u8 *dest, u64 src, size_t length)
752{
753 while (length--) {
754 dest[length] = (u8)src;
755 src >>= 8;
756 }
757}
758
759/*
Patrick Rudolph5ccc7312018-05-30 15:05:28 +0200760 * Delete a property by name in a given node if it exists.
761 *
762 * @param node The device tree node to operate on.
763 * @param name The name of the property to delete.
764 */
765void dt_delete_prop(struct device_tree_node *node, const char *name)
766{
767 struct device_tree_property *prop;
768
769 list_for_each(prop, node->properties, list_node) {
770 if (!strcmp(prop->prop.name, name)) {
771 list_remove(&prop->list_node);
772 return;
773 }
774 }
775}
776
777/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200778 * Add an arbitrary property to a node, or update it if it already exists.
779 *
780 * @param node The device tree node to add to.
781 * @param name The name of the new property.
782 * @param data The raw data blob to be stored in the property.
783 * @param size The size of data in bytes.
784 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200785void dt_add_bin_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200786 const void *data, size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200787{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200788 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200789
790 list_for_each(prop, node->properties, list_node) {
791 if (!strcmp(prop->prop.name, name)) {
792 prop->prop.data = data;
793 prop->prop.size = size;
794 return;
795 }
796 }
797
Julius Werner9636a102019-05-03 17:36:43 -0700798 prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200799 list_insert_after(&prop->list_node, &node->properties);
800 prop->prop.name = name;
801 prop->prop.data = data;
802 prop->prop.size = size;
803}
804
805/*
806 * Find given string property in a node and return its content.
807 *
808 * @param node The device tree node to search.
809 * @param name The name of the property.
810 * @return The found string, or NULL.
811 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200812const char *dt_find_string_prop(const struct device_tree_node *node,
813 const char *name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200814{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200815 const void *content;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200816 size_t size;
817
818 dt_find_bin_prop(node, name, &content, &size);
819
820 return content;
821}
822
823/*
824 * Find given property in a node.
825 *
826 * @param node The device tree node to search.
827 * @param name The name of the property.
828 * @param data Pointer to return raw data blob in the property.
829 * @param size Pointer to return the size of data in bytes.
830 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200831void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
832 const void **data, size_t *size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200833{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200834 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200835
836 *data = NULL;
837 *size = 0;
838
839 list_for_each(prop, node->properties, list_node) {
840 if (!strcmp(prop->prop.name, name)) {
841 *data = prop->prop.data;
842 *size = prop->prop.size;
843 return;
844 }
845 }
846}
847
848/*
849 * Add a string property to a node, or update it if it already exists.
850 *
851 * @param node The device tree node to add to.
852 * @param name The name of the new property.
853 * @param str The zero-terminated string to be stored in the property.
854 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200855void dt_add_string_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200856 const char *str)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200857{
858 dt_add_bin_prop(node, name, str, strlen(str) + 1);
859}
860
861/*
862 * Add a 32-bit integer property to a node, or update it if it already exists.
863 *
864 * @param node The device tree node to add to.
865 * @param name The name of the new property.
866 * @param val The integer to be stored in the property.
867 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200868void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200869{
Julius Werner9636a102019-05-03 17:36:43 -0700870 u32 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200871 *val_ptr = htobe32(val);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200872 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
873}
874
875/*
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200876 * Add a 64-bit integer property to a node, or update it if it already exists.
877 *
878 * @param node The device tree node to add to.
879 * @param name The name of the new property.
880 * @param val The integer to be stored in the property.
881 */
882void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
883{
Julius Werner9636a102019-05-03 17:36:43 -0700884 u64 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200885 *val_ptr = htobe64(val);
886 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
887}
888
889/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200890 * Add a 'reg' address list property to a node, or update it if it exists.
891 *
892 * @param node The device tree node to add to.
893 * @param addrs Array of address values to be stored in the property.
894 * @param sizes Array of corresponding size values to 'addrs'.
895 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
896 * @param addr_cells Value of #address-cells property valid for this node.
897 * @param size_cells Value of #size-cells property valid for this node.
898 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200899void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200900 int count, u32 addr_cells, u32 size_cells)
901{
902 int i;
903 size_t length = (addr_cells + size_cells) * sizeof(u32) * count;
Julius Werner9636a102019-05-03 17:36:43 -0700904 u8 *data = xmalloc(length);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200905 u8 *cur = data;
906
907 for (i = 0; i < count; i++) {
908 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
909 cur += addr_cells * sizeof(u32);
910 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
911 cur += size_cells * sizeof(u32);
912 }
913
914 dt_add_bin_prop(node, "reg", data, length);
915}
916
917/*
918 * Fixups to apply to a kernel's device tree before booting it.
919 */
920
Patrick Rudolph666c1722018-04-03 09:57:33 +0200921struct list_node device_tree_fixups;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200922
Patrick Rudolph666c1722018-04-03 09:57:33 +0200923int dt_apply_fixups(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200924{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200925 struct device_tree_fixup *fixup;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200926 list_for_each(fixup, device_tree_fixups, list_node) {
927 assert(fixup->fixup);
928 if (fixup->fixup(fixup, tree))
929 return 1;
930 }
931 return 0;
932}
933
Patrick Rudolph666c1722018-04-03 09:57:33 +0200934int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200935 void *data, size_t data_size, int create)
936{
937 char *path_copy, *prop_name;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200938 struct device_tree_node *dt_node;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200939
940 path_copy = strdup(path);
941
942 if (!path_copy) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200943 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
944 path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200945 return 1;
946 }
947
948 prop_name = strrchr(path_copy, '/');
949 if (!prop_name) {
Patrick Rudolph679d6242018-07-11 13:53:04 +0200950 free(path_copy);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200951 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200952 return 1;
953 }
954
955 *prop_name++ = '\0'; /* Separate path from the property name. */
956
957 dt_node = dt_find_node_by_path(tree->root, path_copy, NULL,
958 NULL, create);
959
960 if (!dt_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200961 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200962 create ? "create" : "find", path_copy);
Patrick Rudolph679d6242018-07-11 13:53:04 +0200963 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200964 return 1;
965 }
966
967 dt_add_bin_prop(dt_node, prop_name, data, data_size);
Patrick Rudolph679d6242018-07-11 13:53:04 +0200968 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200969
970 return 0;
971}
972
973/*
974 * Prepare the /reserved-memory/ node.
975 *
976 * Technically, this can be called more than one time, to init and/or retrieve
977 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
978 *
979 * @tree: Device tree to add/retrieve from.
980 * @return: The /reserved-memory/ node (or NULL, if error).
981 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200982struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200983{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200984 struct device_tree_node *reserved;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200985 u32 addr = 0, size = 0;
986
987 reserved = dt_find_node_by_path(tree->root, "reserved-memory", &addr,
988 &size, 1);
989 if (!reserved)
990 return NULL;
991
992 // Binding doc says this should have the same #{address,size}-cells as
993 // the root.
994 dt_add_u32_prop(reserved, "#address-cells", addr);
995 dt_add_u32_prop(reserved, "#size-cells", size);
996 // Binding doc says this should be empty (i.e., 1:1 mapping from root).
997 dt_add_bin_prop(reserved, "ranges", NULL, 0);
998
999 return reserved;
1000}