blob: 8dbc510c10b6648df5d6978d005a0ac20ca20a2c [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>
Patrick Rudolph666c1722018-04-03 09:57:33 +020019#include <console/console.h>
20#include <device_tree.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020021#include <endian.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020022#include <stdint.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +020023#include <string.h>
24#include <stddef.h>
25#include <stdlib.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020026
27/*
28 * Functions for picking apart flattened trees.
29 */
30
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020031int fdt_next_property(const void *blob, uint32_t offset,
32 struct fdt_property *prop)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020033{
Patrick Rudolph666c1722018-04-03 09:57:33 +020034 struct fdt_header *header = (struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020035 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
36
37 int index = 0;
Patrick Rudolph666c1722018-04-03 09:57:33 +020038 if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020039 return 0;
40
Patrick Rudolph666c1722018-04-03 09:57:33 +020041 uint32_t size = be32toh(ptr[index++]);
42 uint32_t name_offset = be32toh(ptr[index++]);
43 name_offset += be32toh(header->strings_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020044
45 if (prop) {
46 prop->name = (char *)((uint8_t *)blob + name_offset);
47 prop->data = &ptr[index];
48 prop->size = size;
49 }
50
Patrick Rudolph666c1722018-04-03 09:57:33 +020051 index += DIV_ROUND_UP(size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020052
Patrick Rudolph666c1722018-04-03 09:57:33 +020053 return index * sizeof(uint32_t);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020054}
55
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020056int fdt_node_name(const void *blob, uint32_t offset, const char **name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020057{
58 uint8_t *ptr = ((uint8_t *)blob) + offset;
Julius Wernera5ea3a22019-05-07 17:38:12 -070059 if (be32dec(ptr) != FDT_TOKEN_BEGIN_NODE)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020060 return 0;
61
62 ptr += 4;
63 if (name)
64 *name = (char *)ptr;
Patrick Rudolph666c1722018-04-03 09:57:33 +020065 return ALIGN_UP(strlen((char *)ptr) + 1, sizeof(uint32_t)) + 4;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020066}
67
68
69
70/*
71 * Functions for printing flattened trees.
72 */
73
74static void print_indent(int depth)
75{
76 while (depth--)
Patrick Rudolph666c1722018-04-03 09:57:33 +020077 printk(BIOS_DEBUG, " ");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020078}
79
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020080static void print_property(const struct fdt_property *prop, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020081{
82 print_indent(depth);
Patrick Rudolph666c1722018-04-03 09:57:33 +020083 printk(BIOS_DEBUG, "prop \"%s\" (%d bytes).\n", prop->name, prop->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020084 print_indent(depth + 1);
85 for (int i = 0; i < MIN(25, prop->size); i++) {
Patrick Rudolph666c1722018-04-03 09:57:33 +020086 printk(BIOS_DEBUG, "%02x ", ((uint8_t *)prop->data)[i]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020087 }
88 if (prop->size > 25)
Patrick Rudolph666c1722018-04-03 09:57:33 +020089 printk(BIOS_DEBUG, "...");
90 printk(BIOS_DEBUG, "\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020091}
92
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020093static int print_flat_node(const void *blob, uint32_t start_offset, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020094{
95 int offset = start_offset;
96 const char *name;
97 int size;
98
99 size = fdt_node_name(blob, offset, &name);
100 if (!size)
101 return 0;
102 offset += size;
103
104 print_indent(depth);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200105 printk(BIOS_DEBUG, "name = %s\n", name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200106
Patrick Rudolph666c1722018-04-03 09:57:33 +0200107 struct fdt_property prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200108 while ((size = fdt_next_property(blob, offset, &prop))) {
109 print_property(&prop, depth + 1);
110
111 offset += size;
112 }
113
114 while ((size = print_flat_node(blob, offset, depth + 1)))
115 offset += size;
116
117 return offset - start_offset + sizeof(uint32_t);
118}
119
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200120void fdt_print_node(const void *blob, uint32_t offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200121{
122 print_flat_node(blob, offset, 0);
123}
124
125
126
127/*
128 * A utility function to skip past nodes in flattened trees.
129 */
130
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200131int fdt_skip_node(const void *blob, uint32_t start_offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200132{
133 int offset = start_offset;
134 int size;
135
136 const char *name;
137 size = fdt_node_name(blob, offset, &name);
138 if (!size)
139 return 0;
140 offset += size;
141
142 while ((size = fdt_next_property(blob, offset, NULL)))
143 offset += size;
144
145 while ((size = fdt_skip_node(blob, offset)))
146 offset += size;
147
148 return offset - start_offset + sizeof(uint32_t);
149}
150
151
152
153/*
154 * Functions to turn a flattened tree into an unflattened one.
155 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200156static struct device_tree_node *alloc_node(void)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200157{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200158 struct device_tree_node *buf = malloc(sizeof(struct device_tree_node));
159 if (!buf)
160 return NULL;
161 memset(buf, 0, sizeof(*buf));
162 return buf;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200163}
Patrick Rudolph666c1722018-04-03 09:57:33 +0200164
165static struct device_tree_property *alloc_prop(void)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200166{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200167 struct device_tree_property *buf =
168 malloc(sizeof(struct device_tree_property));
169 if (!buf)
170 return NULL;
171 memset(buf, 0, sizeof(*buf));
172 return buf;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200173}
174
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200175static int fdt_unflatten_node(const void *blob, uint32_t start_offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200176 struct device_tree_node **new_node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200177{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200178 struct list_node *last;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200179 int offset = start_offset;
180 const char *name;
181 int size;
182
183 size = fdt_node_name(blob, offset, &name);
184 if (!size)
185 return 0;
186 offset += size;
187
Patrick Rudolph666c1722018-04-03 09:57:33 +0200188 struct device_tree_node *node = alloc_node();
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200189 *new_node = node;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200190 if (!node)
191 return 0;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200192 node->name = name;
193
Patrick Rudolph666c1722018-04-03 09:57:33 +0200194 struct fdt_property fprop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200195 last = &node->properties;
196 while ((size = fdt_next_property(blob, offset, &fprop))) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200197 struct device_tree_property *prop = alloc_prop();
198 if (!prop)
199 return 0;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200200 prop->prop = fprop;
201
202 list_insert_after(&prop->list_node, last);
203 last = &prop->list_node;
204
205 offset += size;
206 }
207
Patrick Rudolph666c1722018-04-03 09:57:33 +0200208 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200209 last = &node->children;
210 while ((size = fdt_unflatten_node(blob, offset, &child))) {
211 list_insert_after(&child->list_node, last);
212 last = &child->list_node;
213
214 offset += size;
215 }
216
217 return offset - start_offset + sizeof(uint32_t);
218}
219
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200220static int fdt_unflatten_map_entry(const void *blob, uint32_t offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200221 struct device_tree_reserve_map_entry **new)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200222{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200223 const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset);
224 const uint64_t start = be64toh(ptr[0]);
225 const uint64_t size = be64toh(ptr[1]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200226
227 if (!size)
228 return 0;
229
Patrick Rudolph666c1722018-04-03 09:57:33 +0200230 struct device_tree_reserve_map_entry *entry = malloc(sizeof(*entry));
231 if (!entry)
232 return 0;
233 memset(entry, 0, sizeof(*entry));
234 *new = entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200235 entry->start = start;
236 entry->size = size;
237
238 return sizeof(uint64_t) * 2;
239}
240
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200241struct device_tree *fdt_unflatten(const void *blob)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200242{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200243 struct device_tree *tree = malloc(sizeof(*tree));
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200244 const struct fdt_header *header = (const struct fdt_header *)blob;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200245 if (!tree)
246 return NULL;
247 memset(tree, 0, sizeof(*tree));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200248 tree->header = header;
249
Patrick Rudolph666c1722018-04-03 09:57:33 +0200250 uint32_t struct_offset = be32toh(header->structure_offset);
251 uint32_t strings_offset = be32toh(header->strings_offset);
252 uint32_t reserve_offset = be32toh(header->reserve_map_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200253 uint32_t min_offset = 0;
254 min_offset = MIN(struct_offset, strings_offset);
255 min_offset = MIN(min_offset, reserve_offset);
256 // Assume everything up to the first non-header component is part of
257 // the header and needs to be preserved. This will protect us against
258 // new elements being added in the future.
259 tree->header_size = min_offset;
260
Patrick Rudolph666c1722018-04-03 09:57:33 +0200261 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200262 uint32_t offset = reserve_offset;
263 int size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200264 struct list_node *last = &tree->reserve_map;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200265 while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) {
266 list_insert_after(&entry->list_node, last);
267 last = &entry->list_node;
268
269 offset += size;
270 }
271
272 fdt_unflatten_node(blob, struct_offset, &tree->root);
273
274 return tree;
275}
276
277
278
279/*
Patrick Rudolph666c1722018-04-03 09:57:33 +0200280 * Functions to find the size of the device tree if it was flattened.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200281 */
282
Patrick Rudolph666c1722018-04-03 09:57:33 +0200283static void dt_flat_prop_size(struct device_tree_property *prop,
284 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200285{
286 // Starting token.
287 *struct_size += sizeof(uint32_t);
288 // Size.
289 *struct_size += sizeof(uint32_t);
290 // Name offset.
291 *struct_size += sizeof(uint32_t);
292 // Property value.
Patrick Rudolph666c1722018-04-03 09:57:33 +0200293 *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200294
295 // Property name.
296 *strings_size += strlen(prop->prop.name) + 1;
297}
298
Patrick Rudolph666c1722018-04-03 09:57:33 +0200299static void dt_flat_node_size(struct device_tree_node *node,
300 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200301{
302 // Starting token.
303 *struct_size += sizeof(uint32_t);
304 // Node name.
Patrick Rudolph666c1722018-04-03 09:57:33 +0200305 *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200306
Patrick Rudolph666c1722018-04-03 09:57:33 +0200307 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200308 list_for_each(prop, node->properties, list_node)
309 dt_flat_prop_size(prop, struct_size, strings_size);
310
Patrick Rudolph666c1722018-04-03 09:57:33 +0200311 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200312 list_for_each(child, node->children, list_node)
313 dt_flat_node_size(child, struct_size, strings_size);
314
315 // End token.
316 *struct_size += sizeof(uint32_t);
317}
318
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200319uint32_t dt_flat_size(const struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200320{
321 uint32_t size = tree->header_size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200322 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200323 list_for_each(entry, tree->reserve_map, list_node)
324 size += sizeof(uint64_t) * 2;
325 size += sizeof(uint64_t) * 2;
326
327 uint32_t struct_size = 0;
328 uint32_t strings_size = 0;
329 dt_flat_node_size(tree->root, &struct_size, &strings_size);
330
331 size += struct_size;
332 // End token.
333 size += sizeof(uint32_t);
334
335 size += strings_size;
336
337 return size;
338}
339
340
341
342/*
343 * Functions to flatten a device tree.
344 */
345
Patrick Rudolph666c1722018-04-03 09:57:33 +0200346static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200347 void **map_start)
348{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200349 ((uint64_t *)*map_start)[0] = htobe64(entry->start);
350 ((uint64_t *)*map_start)[1] = htobe64(entry->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200351 *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2;
352}
353
Patrick Rudolph666c1722018-04-03 09:57:33 +0200354static void dt_flatten_prop(struct device_tree_property *prop,
355 void **struct_start, void *strings_base,
356 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200357{
358 uint8_t *dstruct = (uint8_t *)*struct_start;
359 uint8_t *dstrings = (uint8_t *)*strings_start;
360
Julius Wernera5ea3a22019-05-07 17:38:12 -0700361 be32enc(dstruct, FDT_TOKEN_PROPERTY);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200362 dstruct += sizeof(uint32_t);
363
Julius Wernera5ea3a22019-05-07 17:38:12 -0700364 be32enc(dstruct, prop->prop.size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200365 dstruct += sizeof(uint32_t);
366
367 uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base;
Julius Wernera5ea3a22019-05-07 17:38:12 -0700368 be32enc(dstruct, name_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200369 dstruct += sizeof(uint32_t);
370
371 strcpy((char *)dstrings, prop->prop.name);
372 dstrings += strlen(prop->prop.name) + 1;
373
374 memcpy(dstruct, prop->prop.data, prop->prop.size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200375 dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200376
377 *struct_start = dstruct;
378 *strings_start = dstrings;
379}
380
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200381static void dt_flatten_node(const struct device_tree_node *node,
382 void **struct_start, void *strings_base,
383 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200384{
385 uint8_t *dstruct = (uint8_t *)*struct_start;
386 uint8_t *dstrings = (uint8_t *)*strings_start;
387
Julius Wernera5ea3a22019-05-07 17:38:12 -0700388 be32enc(dstruct, FDT_TOKEN_BEGIN_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200389 dstruct += sizeof(uint32_t);
390
391 strcpy((char *)dstruct, node->name);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200392 dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200393
Patrick Rudolph666c1722018-04-03 09:57:33 +0200394 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200395 list_for_each(prop, node->properties, list_node)
396 dt_flatten_prop(prop, (void **)&dstruct, strings_base,
397 (void **)&dstrings);
398
Patrick Rudolph666c1722018-04-03 09:57:33 +0200399 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200400 list_for_each(child, node->children, list_node)
401 dt_flatten_node(child, (void **)&dstruct, strings_base,
402 (void **)&dstrings);
403
Julius Wernera5ea3a22019-05-07 17:38:12 -0700404 be32enc(dstruct, FDT_TOKEN_END_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200405 dstruct += sizeof(uint32_t);
406
407 *struct_start = dstruct;
408 *strings_start = dstrings;
409}
410
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200411void dt_flatten(const struct device_tree *tree, void *start_dest)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200412{
413 uint8_t *dest = (uint8_t *)start_dest;
414
415 memcpy(dest, tree->header, tree->header_size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200416 struct fdt_header *header = (struct fdt_header *)dest;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200417 dest += tree->header_size;
418
Patrick Rudolph666c1722018-04-03 09:57:33 +0200419 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200420 list_for_each(entry, tree->reserve_map, list_node)
421 dt_flatten_map_entry(entry, (void **)&dest);
422 ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0;
423 dest += sizeof(uint64_t) * 2;
424
425 uint32_t struct_size = 0;
426 uint32_t strings_size = 0;
427 dt_flat_node_size(tree->root, &struct_size, &strings_size);
428
429 uint8_t *struct_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200430 header->structure_offset = htobe32(dest - (uint8_t *)start_dest);
431 header->structure_size = htobe32(struct_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200432 dest += struct_size;
433
Patrick Rudolph666c1722018-04-03 09:57:33 +0200434 *((uint32_t *)dest) = htobe32(FDT_TOKEN_END);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200435 dest += sizeof(uint32_t);
436
437 uint8_t *strings_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200438 header->strings_offset = htobe32(dest - (uint8_t *)start_dest);
439 header->strings_size = htobe32(strings_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200440 dest += strings_size;
441
442 dt_flatten_node(tree->root, (void **)&struct_start, strings_start,
443 (void **)&strings_start);
444
Patrick Rudolph666c1722018-04-03 09:57:33 +0200445 header->totalsize = htobe32(dest - (uint8_t *)start_dest);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200446}
447
448
449
450/*
451 * Functions for printing a non-flattened device tree.
452 */
453
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200454static void print_node(const struct device_tree_node *node, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200455{
456 print_indent(depth);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200457 printk(BIOS_DEBUG, "name = %s\n", node->name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200458
Patrick Rudolph666c1722018-04-03 09:57:33 +0200459 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200460 list_for_each(prop, node->properties, list_node)
461 print_property(&prop->prop, depth + 1);
462
Patrick Rudolph666c1722018-04-03 09:57:33 +0200463 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200464 list_for_each(child, node->children, list_node)
465 print_node(child, depth + 1);
466}
467
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200468void dt_print_node(const struct device_tree_node *node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200469{
470 print_node(node, 0);
471}
472
473
474
475/*
476 * Functions for reading and manipulating an unflattened device tree.
477 */
478
479/*
480 * Read #address-cells and #size-cells properties from a node.
481 *
482 * @param node The device tree node to read from.
483 * @param addrcp Pointer to store #address-cells in, skipped if NULL.
484 * @param sizecp Pointer to store #size-cells in, skipped if NULL.
485 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200486void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
487 u32 *sizecp)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200488{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200489 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200490 list_for_each(prop, node->properties, list_node) {
491 if (addrcp && !strcmp("#address-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700492 *addrcp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200493 if (sizecp && !strcmp("#size-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700494 *sizecp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200495 }
496}
497
498/*
499 * Find a node from a device tree path, relative to a parent node.
500 *
501 * @param parent The node from which to start the relative path lookup.
502 * @param path An array of path component strings that will be looked
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200503 * up in order to find the node. Must be terminated with
504 * a NULL pointer. Example: {'firmware', 'coreboot', NULL}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200505 * @param addrcp Pointer that will be updated with any #address-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200506 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200507 * @param sizecp Pointer that will be updated with any #size-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200508 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200509 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
510 * @return The found/created node, or NULL.
511 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200512struct device_tree_node *dt_find_node(struct device_tree_node *parent,
513 const char **path, u32 *addrcp,
514 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200515{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200516 struct device_tree_node *node, *found = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200517
518 // Update #address-cells and #size-cells for this level.
519 dt_read_cell_props(parent, addrcp, sizecp);
520
521 if (!*path)
522 return parent;
523
524 // Find the next node in the path, if it exists.
525 list_for_each(node, parent->children, list_node) {
526 if (!strcmp(node->name, *path)) {
527 found = node;
528 break;
529 }
530 }
531
532 // Otherwise create it or return NULL.
533 if (!found) {
534 if (!create)
535 return NULL;
536
537 found = alloc_node();
Patrick Rudolph666c1722018-04-03 09:57:33 +0200538 if (!found)
539 return NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200540 found->name = strdup(*path);
541 if (!found->name)
542 return NULL;
543
544 list_insert_after(&found->list_node, &parent->children);
545 }
546
547 return dt_find_node(found, path + 1, addrcp, sizecp, create);
548}
549
550/*
551 * Find a node from a string device tree path, relative to a parent node.
552 *
553 * @param parent The node from which to start the relative path lookup.
554 * @param path A string representing a path in the device tree, with
555 * nodes separated by '/'. Example: "soc/firmware/coreboot"
556 * @param addrcp Pointer that will be updated with any #address-cells
557 * value found in the path. May be NULL to ignore.
558 * @param sizecp Pointer that will be updated with any #size-cells
559 * value found in the path. May be NULL to ignore.
560 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
561 * @return The found/created node, or NULL.
562 *
563 * It is the caller responsibility to provide the correct path string, namely
564 * not starting or ending with a '/', and not having "//" anywhere in it.
565 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200566struct device_tree_node *dt_find_node_by_path(struct device_tree_node *parent,
567 const char *path, u32 *addrcp,
568 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200569{
570 char *dup_path = strdup(path);
571 /* Hopefully enough depth for any node. */
572 const char *path_array[15];
573 int i;
574 char *next_slash;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200575 struct device_tree_node *node = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200576
577 if (!dup_path)
578 return NULL;
579
580 next_slash = dup_path;
581 path_array[0] = dup_path;
582 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
583
584 next_slash = strchr(next_slash, '/');
585 if (!next_slash)
586 break;
587
588 *next_slash++ = '\0';
589 path_array[i] = next_slash;
590 }
591
592 if (!next_slash) {
593 path_array[i] = NULL;
594 node = dt_find_node(parent, path_array,
595 addrcp, sizecp, create);
596 }
597
598 free(dup_path);
599 return node;
600}
601
602/*
603 * Check if given node is compatible.
604 *
605 * @param node The node which is to be checked for compatible property.
606 * @param compat The compatible string to match.
607 * @return 1 = compatible, 0 = not compatible.
608 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200609static int dt_check_compat_match(struct device_tree_node *node,
610 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200611{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200612 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200613
614 list_for_each(prop, node->properties, list_node) {
615 if (!strcmp("compatible", prop->prop.name)) {
616 size_t bytes = prop->prop.size;
617 const char *str = prop->prop.data;
618 while (bytes > 0) {
619 if (!strncmp(compat, str, bytes))
620 return 1;
621 size_t len = strnlen(str, bytes) + 1;
622 if (bytes <= len)
623 break;
624 str += len;
625 bytes -= len;
626 }
627 break;
628 }
629 }
630
631 return 0;
632}
633
634/*
635 * Find a node from a compatible string, in the subtree of a parent node.
636 *
637 * @param parent The parent node under which to look.
638 * @param compat The compatible string to find.
639 * @return The found node, or NULL.
640 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200641struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
642 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200643{
644 // Check if the parent node itself is compatible.
645 if (dt_check_compat_match(parent, compat))
646 return parent;
647
Patrick Rudolph666c1722018-04-03 09:57:33 +0200648 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200649 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200650 struct device_tree_node *found = dt_find_compat(child, compat);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200651 if (found)
652 return found;
653 }
654
655 return NULL;
656}
657
658/*
659 * Find the next compatible child of a given parent. All children upto the
660 * child passed in by caller are ignored. If child is NULL, it considers all the
661 * children to find the first child which is compatible.
662 *
663 * @param parent The parent node under which to look.
664 * @param child The child node to start search from (exclusive). If NULL
665 * consider all children.
666 * @param compat The compatible string to find.
667 * @return The found node, or NULL.
668 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200669struct device_tree_node *
670dt_find_next_compat_child(struct device_tree_node *parent,
671 struct device_tree_node *child,
672 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200673{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200674 struct device_tree_node *next;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200675 int ignore = 0;
676
677 if (child)
678 ignore = 1;
679
680 list_for_each(next, parent->children, list_node) {
681 if (ignore) {
682 if (child == next)
683 ignore = 0;
684 continue;
685 }
686
687 if (dt_check_compat_match(next, compat))
688 return next;
689 }
690
691 return NULL;
692}
693
694/*
695 * Find a node with matching property value, in the subtree of a parent node.
696 *
697 * @param parent The parent node under which to look.
698 * @param name The property name to look for.
699 * @param data The property value to look for.
700 * @param size The property size.
701 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200702struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
703 const char *name, void *data,
704 size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200705{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200706 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200707
708 /* Check if parent itself has the required property value. */
709 list_for_each(prop, parent->properties, list_node) {
710 if (!strcmp(name, prop->prop.name)) {
711 size_t bytes = prop->prop.size;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200712 const void *prop_data = prop->prop.data;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200713 if (size != bytes)
714 break;
715 if (!memcmp(data, prop_data, size))
716 return parent;
717 break;
718 }
719 }
720
Patrick Rudolph666c1722018-04-03 09:57:33 +0200721 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200722 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200723 struct device_tree_node *found = dt_find_prop_value(child, name,
724 data, size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200725 if (found)
726 return found;
727 }
728 return NULL;
729}
730
Patrick Rudolphc38960d2018-05-08 11:18:54 +0200731/**
732 * Find the phandle of a node.
733 *
734 * @param node Pointer to node containing the phandle
735 * @return Zero on error, the phandle on success
736 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200737uint32_t dt_get_phandle(const struct device_tree_node *node)
Patrick Rudolphc38960d2018-05-08 11:18:54 +0200738{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200739 const uint32_t *phandle;
Patrick Rudolphc38960d2018-05-08 11:18:54 +0200740 size_t len;
741
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200742 dt_find_bin_prop(node, "phandle", (const void **)&phandle, &len);
Patrick Rudolphc38960d2018-05-08 11:18:54 +0200743 if (phandle != NULL && len == sizeof(*phandle))
744 return be32_to_cpu(*phandle);
745
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200746 dt_find_bin_prop(node, "linux,phandle", (const void **)&phandle, &len);
Patrick Rudolphc38960d2018-05-08 11:18:54 +0200747 if (phandle != NULL && len == sizeof(*phandle))
748 return be32_to_cpu(*phandle);
749
750 return 0;
751}
752
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200753/*
754 * Write an arbitrary sized big-endian integer into a pointer.
755 *
756 * @param dest Pointer to the DT property data buffer to write.
Elyes HAOUAS1ec76442018-08-07 12:20:04 +0200757 * @param src The integer to write (in CPU endianness).
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200758 * @param length the length of the destination integer in bytes.
759 */
760void dt_write_int(u8 *dest, u64 src, size_t length)
761{
762 while (length--) {
763 dest[length] = (u8)src;
764 src >>= 8;
765 }
766}
767
768/*
Patrick Rudolph5ccc7312018-05-30 15:05:28 +0200769 * Delete a property by name in a given node if it exists.
770 *
771 * @param node The device tree node to operate on.
772 * @param name The name of the property to delete.
773 */
774void dt_delete_prop(struct device_tree_node *node, const char *name)
775{
776 struct device_tree_property *prop;
777
778 list_for_each(prop, node->properties, list_node) {
779 if (!strcmp(prop->prop.name, name)) {
780 list_remove(&prop->list_node);
781 return;
782 }
783 }
784}
785
786/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200787 * Add an arbitrary property to a node, or update it if it already exists.
788 *
789 * @param node The device tree node to add to.
790 * @param name The name of the new property.
791 * @param data The raw data blob to be stored in the property.
792 * @param size The size of data in bytes.
793 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200794void dt_add_bin_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200795 const void *data, size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200796{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200797 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200798
799 list_for_each(prop, node->properties, list_node) {
800 if (!strcmp(prop->prop.name, name)) {
801 prop->prop.data = data;
802 prop->prop.size = size;
803 return;
804 }
805 }
806
807 prop = alloc_prop();
Patrick Rudolph666c1722018-04-03 09:57:33 +0200808 if (!prop)
809 return;
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{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200881 u32 *val_ptr = malloc(sizeof(val));
882 if (!val_ptr)
883 return;
884 *val_ptr = htobe32(val);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200885 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
886}
887
888/*
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200889 * Add a 64-bit integer property to a node, or update it if it already exists.
890 *
891 * @param node The device tree node to add to.
892 * @param name The name of the new property.
893 * @param val The integer to be stored in the property.
894 */
895void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
896{
897 u64 *val_ptr = malloc(sizeof(val));
898 if (!val_ptr)
899 return;
900 *val_ptr = htobe64(val);
901 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
902}
903
904/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200905 * Add a 'reg' address list property to a node, or update it if it exists.
906 *
907 * @param node The device tree node to add to.
908 * @param addrs Array of address values to be stored in the property.
909 * @param sizes Array of corresponding size values to 'addrs'.
910 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
911 * @param addr_cells Value of #address-cells property valid for this node.
912 * @param size_cells Value of #size-cells property valid for this node.
913 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200914void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200915 int count, u32 addr_cells, u32 size_cells)
916{
917 int i;
918 size_t length = (addr_cells + size_cells) * sizeof(u32) * count;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200919 u8 *data = malloc(length);
920 if (!data)
921 return;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200922 u8 *cur = data;
923
924 for (i = 0; i < count; i++) {
925 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
926 cur += addr_cells * sizeof(u32);
927 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
928 cur += size_cells * sizeof(u32);
929 }
930
931 dt_add_bin_prop(node, "reg", data, length);
932}
933
934/*
935 * Fixups to apply to a kernel's device tree before booting it.
936 */
937
Patrick Rudolph666c1722018-04-03 09:57:33 +0200938struct list_node device_tree_fixups;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200939
Patrick Rudolph666c1722018-04-03 09:57:33 +0200940int dt_apply_fixups(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200941{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200942 struct device_tree_fixup *fixup;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200943 list_for_each(fixup, device_tree_fixups, list_node) {
944 assert(fixup->fixup);
945 if (fixup->fixup(fixup, tree))
946 return 1;
947 }
948 return 0;
949}
950
Patrick Rudolph666c1722018-04-03 09:57:33 +0200951int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200952 void *data, size_t data_size, int create)
953{
954 char *path_copy, *prop_name;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200955 struct device_tree_node *dt_node;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200956
957 path_copy = strdup(path);
958
959 if (!path_copy) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200960 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
961 path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200962 return 1;
963 }
964
965 prop_name = strrchr(path_copy, '/');
966 if (!prop_name) {
Patrick Rudolph679d6242018-07-11 13:53:04 +0200967 free(path_copy);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200968 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200969 return 1;
970 }
971
972 *prop_name++ = '\0'; /* Separate path from the property name. */
973
974 dt_node = dt_find_node_by_path(tree->root, path_copy, NULL,
975 NULL, create);
976
977 if (!dt_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200978 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200979 create ? "create" : "find", path_copy);
Patrick Rudolph679d6242018-07-11 13:53:04 +0200980 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200981 return 1;
982 }
983
984 dt_add_bin_prop(dt_node, prop_name, data, data_size);
Patrick Rudolph679d6242018-07-11 13:53:04 +0200985 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200986
987 return 0;
988}
989
990/*
991 * Prepare the /reserved-memory/ node.
992 *
993 * Technically, this can be called more than one time, to init and/or retrieve
994 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
995 *
996 * @tree: Device tree to add/retrieve from.
997 * @return: The /reserved-memory/ node (or NULL, if error).
998 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200999struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001000{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001001 struct device_tree_node *reserved;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001002 u32 addr = 0, size = 0;
1003
1004 reserved = dt_find_node_by_path(tree->root, "reserved-memory", &addr,
1005 &size, 1);
1006 if (!reserved)
1007 return NULL;
1008
1009 // Binding doc says this should have the same #{address,size}-cells as
1010 // the root.
1011 dt_add_u32_prop(reserved, "#address-cells", addr);
1012 dt_add_u32_prop(reserved, "#size-cells", size);
1013 // Binding doc says this should be empty (i.e., 1:1 mapping from root).
1014 dt_add_bin_prop(reserved, "ranges", NULL, 0);
1015
1016 return reserved;
1017}