blob: 89387ffdbf3db8907711993ce26f50ad9348dbf0 [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 Rudolph666c1722018-04-03 09:57:33 +020031int fdt_next_property(void *blob, uint32_t offset, struct fdt_property *prop)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020032{
Patrick Rudolph666c1722018-04-03 09:57:33 +020033 struct fdt_header *header = (struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020034 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
35
36 int index = 0;
Patrick Rudolph666c1722018-04-03 09:57:33 +020037 if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020038 return 0;
39
Patrick Rudolph666c1722018-04-03 09:57:33 +020040 uint32_t size = be32toh(ptr[index++]);
41 uint32_t name_offset = be32toh(ptr[index++]);
42 name_offset += be32toh(header->strings_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020043
44 if (prop) {
45 prop->name = (char *)((uint8_t *)blob + name_offset);
46 prop->data = &ptr[index];
47 prop->size = size;
48 }
49
Patrick Rudolph666c1722018-04-03 09:57:33 +020050 index += DIV_ROUND_UP(size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020051
Patrick Rudolph666c1722018-04-03 09:57:33 +020052 return index * sizeof(uint32_t);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020053}
54
55int fdt_node_name(void *blob, uint32_t offset, const char **name)
56{
57 uint8_t *ptr = ((uint8_t *)blob) + offset;
Patrick Rudolph666c1722018-04-03 09:57:33 +020058 if (be32toh(*(uint32_t *)ptr) != FDT_TOKEN_BEGIN_NODE)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020059 return 0;
60
61 ptr += 4;
62 if (name)
63 *name = (char *)ptr;
Patrick Rudolph666c1722018-04-03 09:57:33 +020064 return ALIGN_UP(strlen((char *)ptr) + 1, sizeof(uint32_t)) + 4;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020065}
66
67
68
69/*
70 * Functions for printing flattened trees.
71 */
72
73static void print_indent(int depth)
74{
75 while (depth--)
Patrick Rudolph666c1722018-04-03 09:57:33 +020076 printk(BIOS_DEBUG, " ");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020077}
78
Patrick Rudolph666c1722018-04-03 09:57:33 +020079static void print_property(struct fdt_property *prop, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020080{
81 print_indent(depth);
Patrick Rudolph666c1722018-04-03 09:57:33 +020082 printk(BIOS_DEBUG, "prop \"%s\" (%d bytes).\n", prop->name, prop->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020083 print_indent(depth + 1);
84 for (int i = 0; i < MIN(25, prop->size); i++) {
Patrick Rudolph666c1722018-04-03 09:57:33 +020085 printk(BIOS_DEBUG, "%02x ", ((uint8_t *)prop->data)[i]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020086 }
87 if (prop->size > 25)
Patrick Rudolph666c1722018-04-03 09:57:33 +020088 printk(BIOS_DEBUG, "...");
89 printk(BIOS_DEBUG, "\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020090}
91
92static int print_flat_node(void *blob, uint32_t start_offset, int depth)
93{
94 int offset = start_offset;
95 const char *name;
96 int size;
97
98 size = fdt_node_name(blob, offset, &name);
99 if (!size)
100 return 0;
101 offset += size;
102
103 print_indent(depth);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200104 printk(BIOS_DEBUG, "name = %s\n", name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200105
Patrick Rudolph666c1722018-04-03 09:57:33 +0200106 struct fdt_property prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200107 while ((size = fdt_next_property(blob, offset, &prop))) {
108 print_property(&prop, depth + 1);
109
110 offset += size;
111 }
112
113 while ((size = print_flat_node(blob, offset, depth + 1)))
114 offset += size;
115
116 return offset - start_offset + sizeof(uint32_t);
117}
118
119void fdt_print_node(void *blob, uint32_t offset)
120{
121 print_flat_node(blob, offset, 0);
122}
123
124
125
126/*
127 * A utility function to skip past nodes in flattened trees.
128 */
129
130int fdt_skip_node(void *blob, uint32_t start_offset)
131{
132 int offset = start_offset;
133 int size;
134
135 const char *name;
136 size = fdt_node_name(blob, offset, &name);
137 if (!size)
138 return 0;
139 offset += size;
140
141 while ((size = fdt_next_property(blob, offset, NULL)))
142 offset += size;
143
144 while ((size = fdt_skip_node(blob, offset)))
145 offset += size;
146
147 return offset - start_offset + sizeof(uint32_t);
148}
149
150
151
152/*
153 * Functions to turn a flattened tree into an unflattened one.
154 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200155static struct device_tree_node *alloc_node(void)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200156{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200157 struct device_tree_node *buf = malloc(sizeof(struct device_tree_node));
158 if (!buf)
159 return NULL;
160 memset(buf, 0, sizeof(*buf));
161 return buf;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200162}
Patrick Rudolph666c1722018-04-03 09:57:33 +0200163
164static struct device_tree_property *alloc_prop(void)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200165{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200166 struct device_tree_property *buf =
167 malloc(sizeof(struct device_tree_property));
168 if (!buf)
169 return NULL;
170 memset(buf, 0, sizeof(*buf));
171 return buf;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200172}
173
174static int fdt_unflatten_node(void *blob, uint32_t start_offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200175 struct device_tree_node **new_node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200176{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200177 struct list_node *last;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200178 int offset = start_offset;
179 const char *name;
180 int size;
181
182 size = fdt_node_name(blob, offset, &name);
183 if (!size)
184 return 0;
185 offset += size;
186
Patrick Rudolph666c1722018-04-03 09:57:33 +0200187 struct device_tree_node *node = alloc_node();
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200188 *new_node = node;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200189 if (!node)
190 return 0;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200191 node->name = name;
192
Patrick Rudolph666c1722018-04-03 09:57:33 +0200193 struct fdt_property fprop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200194 last = &node->properties;
195 while ((size = fdt_next_property(blob, offset, &fprop))) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200196 struct device_tree_property *prop = alloc_prop();
197 if (!prop)
198 return 0;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200199 prop->prop = fprop;
200
201 list_insert_after(&prop->list_node, last);
202 last = &prop->list_node;
203
204 offset += size;
205 }
206
Patrick Rudolph666c1722018-04-03 09:57:33 +0200207 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200208 last = &node->children;
209 while ((size = fdt_unflatten_node(blob, offset, &child))) {
210 list_insert_after(&child->list_node, last);
211 last = &child->list_node;
212
213 offset += size;
214 }
215
216 return offset - start_offset + sizeof(uint32_t);
217}
218
219static int fdt_unflatten_map_entry(void *blob, uint32_t offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200220 struct device_tree_reserve_map_entry **new)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200221{
222 uint64_t *ptr = (uint64_t *)(((uint8_t *)blob) + offset);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200223 uint64_t start = be64toh(ptr[0]);
224 uint64_t size = be64toh(ptr[1]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200225
226 if (!size)
227 return 0;
228
Patrick Rudolph666c1722018-04-03 09:57:33 +0200229 struct device_tree_reserve_map_entry *entry = malloc(sizeof(*entry));
230 if (!entry)
231 return 0;
232 memset(entry, 0, sizeof(*entry));
233 *new = entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200234 entry->start = start;
235 entry->size = size;
236
237 return sizeof(uint64_t) * 2;
238}
239
Patrick Rudolph666c1722018-04-03 09:57:33 +0200240struct device_tree *fdt_unflatten(void *blob)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200241{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200242 struct device_tree *tree = malloc(sizeof(*tree));
243 struct fdt_header *header = (struct fdt_header *)blob;
244 if (!tree)
245 return NULL;
246 memset(tree, 0, sizeof(*tree));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200247 tree->header = header;
248
Patrick Rudolph666c1722018-04-03 09:57:33 +0200249 uint32_t struct_offset = be32toh(header->structure_offset);
250 uint32_t strings_offset = be32toh(header->strings_offset);
251 uint32_t reserve_offset = be32toh(header->reserve_map_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200252 uint32_t min_offset = 0;
253 min_offset = MIN(struct_offset, strings_offset);
254 min_offset = MIN(min_offset, reserve_offset);
255 // Assume everything up to the first non-header component is part of
256 // the header and needs to be preserved. This will protect us against
257 // new elements being added in the future.
258 tree->header_size = min_offset;
259
Patrick Rudolph666c1722018-04-03 09:57:33 +0200260 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200261 uint32_t offset = reserve_offset;
262 int size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200263 struct list_node *last = &tree->reserve_map;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200264 while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) {
265 list_insert_after(&entry->list_node, last);
266 last = &entry->list_node;
267
268 offset += size;
269 }
270
271 fdt_unflatten_node(blob, struct_offset, &tree->root);
272
273 return tree;
274}
275
276
277
278/*
Patrick Rudolph666c1722018-04-03 09:57:33 +0200279 * Functions to find the size of the device tree if it was flattened.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200280 */
281
Patrick Rudolph666c1722018-04-03 09:57:33 +0200282static void dt_flat_prop_size(struct device_tree_property *prop,
283 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200284{
285 // Starting token.
286 *struct_size += sizeof(uint32_t);
287 // Size.
288 *struct_size += sizeof(uint32_t);
289 // Name offset.
290 *struct_size += sizeof(uint32_t);
291 // Property value.
Patrick Rudolph666c1722018-04-03 09:57:33 +0200292 *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200293
294 // Property name.
295 *strings_size += strlen(prop->prop.name) + 1;
296}
297
Patrick Rudolph666c1722018-04-03 09:57:33 +0200298static void dt_flat_node_size(struct device_tree_node *node,
299 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200300{
301 // Starting token.
302 *struct_size += sizeof(uint32_t);
303 // Node name.
Patrick Rudolph666c1722018-04-03 09:57:33 +0200304 *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200305
Patrick Rudolph666c1722018-04-03 09:57:33 +0200306 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200307 list_for_each(prop, node->properties, list_node)
308 dt_flat_prop_size(prop, struct_size, strings_size);
309
Patrick Rudolph666c1722018-04-03 09:57:33 +0200310 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200311 list_for_each(child, node->children, list_node)
312 dt_flat_node_size(child, struct_size, strings_size);
313
314 // End token.
315 *struct_size += sizeof(uint32_t);
316}
317
Patrick Rudolph666c1722018-04-03 09:57:33 +0200318uint32_t dt_flat_size(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200319{
320 uint32_t size = tree->header_size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200321 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200322 list_for_each(entry, tree->reserve_map, list_node)
323 size += sizeof(uint64_t) * 2;
324 size += sizeof(uint64_t) * 2;
325
326 uint32_t struct_size = 0;
327 uint32_t strings_size = 0;
328 dt_flat_node_size(tree->root, &struct_size, &strings_size);
329
330 size += struct_size;
331 // End token.
332 size += sizeof(uint32_t);
333
334 size += strings_size;
335
336 return size;
337}
338
339
340
341/*
342 * Functions to flatten a device tree.
343 */
344
Patrick Rudolph666c1722018-04-03 09:57:33 +0200345static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200346 void **map_start)
347{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200348 ((uint64_t *)*map_start)[0] = htobe64(entry->start);
349 ((uint64_t *)*map_start)[1] = htobe64(entry->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200350 *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2;
351}
352
Patrick Rudolph666c1722018-04-03 09:57:33 +0200353static void dt_flatten_prop(struct device_tree_property *prop,
354 void **struct_start, void *strings_base,
355 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200356{
357 uint8_t *dstruct = (uint8_t *)*struct_start;
358 uint8_t *dstrings = (uint8_t *)*strings_start;
359
Patrick Rudolph666c1722018-04-03 09:57:33 +0200360 *((uint32_t *)dstruct) = htobe32(FDT_TOKEN_PROPERTY);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200361 dstruct += sizeof(uint32_t);
362
Patrick Rudolph666c1722018-04-03 09:57:33 +0200363 *((uint32_t *)dstruct) = htobe32(prop->prop.size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200364 dstruct += sizeof(uint32_t);
365
366 uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200367 *((uint32_t *)dstruct) = htobe32(name_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200368 dstruct += sizeof(uint32_t);
369
370 strcpy((char *)dstrings, prop->prop.name);
371 dstrings += strlen(prop->prop.name) + 1;
372
373 memcpy(dstruct, prop->prop.data, prop->prop.size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200374 dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200375
376 *struct_start = dstruct;
377 *strings_start = dstrings;
378}
379
Patrick Rudolph666c1722018-04-03 09:57:33 +0200380static void dt_flatten_node(struct device_tree_node *node, void **struct_start,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200381 void *strings_base, void **strings_start)
382{
383 uint8_t *dstruct = (uint8_t *)*struct_start;
384 uint8_t *dstrings = (uint8_t *)*strings_start;
385
Patrick Rudolph666c1722018-04-03 09:57:33 +0200386 *((uint32_t *)dstruct) = htobe32(FDT_TOKEN_BEGIN_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200387 dstruct += sizeof(uint32_t);
388
389 strcpy((char *)dstruct, node->name);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200390 dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200391
Patrick Rudolph666c1722018-04-03 09:57:33 +0200392 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200393 list_for_each(prop, node->properties, list_node)
394 dt_flatten_prop(prop, (void **)&dstruct, strings_base,
395 (void **)&dstrings);
396
Patrick Rudolph666c1722018-04-03 09:57:33 +0200397 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200398 list_for_each(child, node->children, list_node)
399 dt_flatten_node(child, (void **)&dstruct, strings_base,
400 (void **)&dstrings);
401
Patrick Rudolph666c1722018-04-03 09:57:33 +0200402 *((uint32_t *)dstruct) = htobe32(FDT_TOKEN_END_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200403 dstruct += sizeof(uint32_t);
404
405 *struct_start = dstruct;
406 *strings_start = dstrings;
407}
408
Patrick Rudolph666c1722018-04-03 09:57:33 +0200409void dt_flatten(struct device_tree *tree, void *start_dest)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200410{
411 uint8_t *dest = (uint8_t *)start_dest;
412
413 memcpy(dest, tree->header, tree->header_size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200414 struct fdt_header *header = (struct fdt_header *)dest;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200415 dest += tree->header_size;
416
Patrick Rudolph666c1722018-04-03 09:57:33 +0200417 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200418 list_for_each(entry, tree->reserve_map, list_node)
419 dt_flatten_map_entry(entry, (void **)&dest);
420 ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0;
421 dest += sizeof(uint64_t) * 2;
422
423 uint32_t struct_size = 0;
424 uint32_t strings_size = 0;
425 dt_flat_node_size(tree->root, &struct_size, &strings_size);
426
427 uint8_t *struct_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200428 header->structure_offset = htobe32(dest - (uint8_t *)start_dest);
429 header->structure_size = htobe32(struct_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200430 dest += struct_size;
431
Patrick Rudolph666c1722018-04-03 09:57:33 +0200432 *((uint32_t *)dest) = htobe32(FDT_TOKEN_END);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200433 dest += sizeof(uint32_t);
434
435 uint8_t *strings_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200436 header->strings_offset = htobe32(dest - (uint8_t *)start_dest);
437 header->strings_size = htobe32(strings_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200438 dest += strings_size;
439
440 dt_flatten_node(tree->root, (void **)&struct_start, strings_start,
441 (void **)&strings_start);
442
Patrick Rudolph666c1722018-04-03 09:57:33 +0200443 header->totalsize = htobe32(dest - (uint8_t *)start_dest);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200444}
445
446
447
448/*
449 * Functions for printing a non-flattened device tree.
450 */
451
Patrick Rudolph666c1722018-04-03 09:57:33 +0200452static void print_node(struct device_tree_node *node, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200453{
454 print_indent(depth);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200455 printk(BIOS_DEBUG, "name = %s\n", node->name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200456
Patrick Rudolph666c1722018-04-03 09:57:33 +0200457 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200458 list_for_each(prop, node->properties, list_node)
459 print_property(&prop->prop, depth + 1);
460
Patrick Rudolph666c1722018-04-03 09:57:33 +0200461 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200462 list_for_each(child, node->children, list_node)
463 print_node(child, depth + 1);
464}
465
Patrick Rudolph666c1722018-04-03 09:57:33 +0200466void dt_print_node(struct device_tree_node *node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200467{
468 print_node(node, 0);
469}
470
471
472
473/*
474 * Functions for reading and manipulating an unflattened device tree.
475 */
476
477/*
478 * Read #address-cells and #size-cells properties from a node.
479 *
480 * @param node The device tree node to read from.
481 * @param addrcp Pointer to store #address-cells in, skipped if NULL.
482 * @param sizecp Pointer to store #size-cells in, skipped if NULL.
483 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200484void dt_read_cell_props(struct device_tree_node *node, u32 *addrcp, u32 *sizecp)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200485{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200486 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200487 list_for_each(prop, node->properties, list_node) {
488 if (addrcp && !strcmp("#address-cells", prop->prop.name))
Patrick Rudolph666c1722018-04-03 09:57:33 +0200489 *addrcp = be32toh(*(u32 *)prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200490 if (sizecp && !strcmp("#size-cells", prop->prop.name))
Patrick Rudolph666c1722018-04-03 09:57:33 +0200491 *sizecp = be32toh(*(u32 *)prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200492 }
493}
494
495/*
496 * Find a node from a device tree path, relative to a parent node.
497 *
498 * @param parent The node from which to start the relative path lookup.
499 * @param path An array of path component strings that will be looked
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200500 * up in order to find the node. Must be terminated with
501 * a NULL pointer. Example: {'firmware', 'coreboot', NULL}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200502 * @param addrcp Pointer that will be updated with any #address-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200503 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200504 * @param sizecp Pointer that will be updated with any #size-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200505 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200506 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
507 * @return The found/created node, or NULL.
508 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200509struct device_tree_node *dt_find_node(struct device_tree_node *parent,
510 const char **path, u32 *addrcp,
511 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200512{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200513 struct device_tree_node *node, *found = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200514
515 // Update #address-cells and #size-cells for this level.
516 dt_read_cell_props(parent, addrcp, sizecp);
517
518 if (!*path)
519 return parent;
520
521 // Find the next node in the path, if it exists.
522 list_for_each(node, parent->children, list_node) {
523 if (!strcmp(node->name, *path)) {
524 found = node;
525 break;
526 }
527 }
528
529 // Otherwise create it or return NULL.
530 if (!found) {
531 if (!create)
532 return NULL;
533
534 found = alloc_node();
Patrick Rudolph666c1722018-04-03 09:57:33 +0200535 if (!found)
536 return NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200537 found->name = strdup(*path);
538 if (!found->name)
539 return NULL;
540
541 list_insert_after(&found->list_node, &parent->children);
542 }
543
544 return dt_find_node(found, path + 1, addrcp, sizecp, create);
545}
546
547/*
548 * Find a node from a string device tree path, relative to a parent node.
549 *
550 * @param parent The node from which to start the relative path lookup.
551 * @param path A string representing a path in the device tree, with
552 * nodes separated by '/'. Example: "soc/firmware/coreboot"
553 * @param addrcp Pointer that will be updated with any #address-cells
554 * value found in the path. May be NULL to ignore.
555 * @param sizecp Pointer that will be updated with any #size-cells
556 * value found in the path. May be NULL to ignore.
557 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
558 * @return The found/created node, or NULL.
559 *
560 * It is the caller responsibility to provide the correct path string, namely
561 * not starting or ending with a '/', and not having "//" anywhere in it.
562 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200563struct device_tree_node *dt_find_node_by_path(struct device_tree_node *parent,
564 const char *path, u32 *addrcp,
565 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200566{
567 char *dup_path = strdup(path);
568 /* Hopefully enough depth for any node. */
569 const char *path_array[15];
570 int i;
571 char *next_slash;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200572 struct device_tree_node *node = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200573
574 if (!dup_path)
575 return NULL;
576
577 next_slash = dup_path;
578 path_array[0] = dup_path;
579 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
580
581 next_slash = strchr(next_slash, '/');
582 if (!next_slash)
583 break;
584
585 *next_slash++ = '\0';
586 path_array[i] = next_slash;
587 }
588
589 if (!next_slash) {
590 path_array[i] = NULL;
591 node = dt_find_node(parent, path_array,
592 addrcp, sizecp, create);
593 }
594
595 free(dup_path);
596 return node;
597}
598
599/*
600 * Check if given node is compatible.
601 *
602 * @param node The node which is to be checked for compatible property.
603 * @param compat The compatible string to match.
604 * @return 1 = compatible, 0 = not compatible.
605 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200606static int dt_check_compat_match(struct device_tree_node *node,
607 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200608{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200609 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200610
611 list_for_each(prop, node->properties, list_node) {
612 if (!strcmp("compatible", prop->prop.name)) {
613 size_t bytes = prop->prop.size;
614 const char *str = prop->prop.data;
615 while (bytes > 0) {
616 if (!strncmp(compat, str, bytes))
617 return 1;
618 size_t len = strnlen(str, bytes) + 1;
619 if (bytes <= len)
620 break;
621 str += len;
622 bytes -= len;
623 }
624 break;
625 }
626 }
627
628 return 0;
629}
630
631/*
632 * Find a node from a compatible string, in the subtree of a parent node.
633 *
634 * @param parent The parent node under which to look.
635 * @param compat The compatible string to find.
636 * @return The found node, or NULL.
637 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200638struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
639 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200640{
641 // Check if the parent node itself is compatible.
642 if (dt_check_compat_match(parent, compat))
643 return parent;
644
Patrick Rudolph666c1722018-04-03 09:57:33 +0200645 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200646 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200647 struct device_tree_node *found = dt_find_compat(child, compat);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200648 if (found)
649 return found;
650 }
651
652 return NULL;
653}
654
655/*
656 * Find the next compatible child of a given parent. All children upto the
657 * child passed in by caller are ignored. If child is NULL, it considers all the
658 * children to find the first child which is compatible.
659 *
660 * @param parent The parent node under which to look.
661 * @param child The child node to start search from (exclusive). If NULL
662 * consider all children.
663 * @param compat The compatible string to find.
664 * @return The found node, or NULL.
665 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200666struct device_tree_node *
667dt_find_next_compat_child(struct device_tree_node *parent,
668 struct device_tree_node *child,
669 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200670{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200671 struct device_tree_node *next;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200672 int ignore = 0;
673
674 if (child)
675 ignore = 1;
676
677 list_for_each(next, parent->children, list_node) {
678 if (ignore) {
679 if (child == next)
680 ignore = 0;
681 continue;
682 }
683
684 if (dt_check_compat_match(next, compat))
685 return next;
686 }
687
688 return NULL;
689}
690
691/*
692 * Find a node with matching property value, in the subtree of a parent node.
693 *
694 * @param parent The parent node under which to look.
695 * @param name The property name to look for.
696 * @param data The property value to look for.
697 * @param size The property size.
698 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200699struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
700 const char *name, void *data,
701 size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200702{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200703 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200704
705 /* Check if parent itself has the required property value. */
706 list_for_each(prop, parent->properties, list_node) {
707 if (!strcmp(name, prop->prop.name)) {
708 size_t bytes = prop->prop.size;
709 void *prop_data = prop->prop.data;
710 if (size != bytes)
711 break;
712 if (!memcmp(data, prop_data, size))
713 return parent;
714 break;
715 }
716 }
717
Patrick Rudolph666c1722018-04-03 09:57:33 +0200718 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200719 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200720 struct device_tree_node *found = dt_find_prop_value(child, name,
721 data, size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200722 if (found)
723 return found;
724 }
725 return NULL;
726}
727
Patrick Rudolphc38960d2018-05-08 11:18:54 +0200728/**
729 * Find the phandle of a node.
730 *
731 * @param node Pointer to node containing the phandle
732 * @return Zero on error, the phandle on success
733 */
734uint32_t dt_get_phandle(struct device_tree_node *node)
735{
736 uint32_t *phandle;
737 size_t len;
738
739 dt_find_bin_prop(node, "phandle", (void **)&phandle, &len);
740 if (phandle != NULL && len == sizeof(*phandle))
741 return be32_to_cpu(*phandle);
742
743 dt_find_bin_prop(node, "linux,phandle", (void **)&phandle, &len);
744 if (phandle != NULL && len == sizeof(*phandle))
745 return be32_to_cpu(*phandle);
746
747 return 0;
748}
749
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200750/*
751 * Write an arbitrary sized big-endian integer into a pointer.
752 *
753 * @param dest Pointer to the DT property data buffer to write.
Elyes HAOUAS1ec76442018-08-07 12:20:04 +0200754 * @param src The integer to write (in CPU endianness).
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200755 * @param length the length of the destination integer in bytes.
756 */
757void dt_write_int(u8 *dest, u64 src, size_t length)
758{
759 while (length--) {
760 dest[length] = (u8)src;
761 src >>= 8;
762 }
763}
764
765/*
Patrick Rudolph5ccc7312018-05-30 15:05:28 +0200766 * Delete a property by name in a given node if it exists.
767 *
768 * @param node The device tree node to operate on.
769 * @param name The name of the property to delete.
770 */
771void dt_delete_prop(struct device_tree_node *node, const char *name)
772{
773 struct device_tree_property *prop;
774
775 list_for_each(prop, node->properties, list_node) {
776 if (!strcmp(prop->prop.name, name)) {
777 list_remove(&prop->list_node);
778 return;
779 }
780 }
781}
782
783/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200784 * Add an arbitrary property to a node, or update it if it already exists.
785 *
786 * @param node The device tree node to add to.
787 * @param name The name of the new property.
788 * @param data The raw data blob to be stored in the property.
789 * @param size The size of data in bytes.
790 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200791void dt_add_bin_prop(struct device_tree_node *node, const char *name,
792 void *data, size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200793{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200794 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200795
796 list_for_each(prop, node->properties, list_node) {
797 if (!strcmp(prop->prop.name, name)) {
798 prop->prop.data = data;
799 prop->prop.size = size;
800 return;
801 }
802 }
803
804 prop = alloc_prop();
Patrick Rudolph666c1722018-04-03 09:57:33 +0200805 if (!prop)
806 return;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200807 list_insert_after(&prop->list_node, &node->properties);
808 prop->prop.name = name;
809 prop->prop.data = data;
810 prop->prop.size = size;
811}
812
813/*
814 * Find given string property in a node and return its content.
815 *
816 * @param node The device tree node to search.
817 * @param name The name of the property.
818 * @return The found string, or NULL.
819 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200820const char *dt_find_string_prop(struct device_tree_node *node, const char *name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200821{
822 void *content;
823 size_t size;
824
825 dt_find_bin_prop(node, name, &content, &size);
826
827 return content;
828}
829
830/*
831 * Find given property in a node.
832 *
833 * @param node The device tree node to search.
834 * @param name The name of the property.
835 * @param data Pointer to return raw data blob in the property.
836 * @param size Pointer to return the size of data in bytes.
837 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200838void dt_find_bin_prop(struct device_tree_node *node, const char *name,
839 void **data, size_t *size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200840{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200841 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200842
843 *data = NULL;
844 *size = 0;
845
846 list_for_each(prop, node->properties, list_node) {
847 if (!strcmp(prop->prop.name, name)) {
848 *data = prop->prop.data;
849 *size = prop->prop.size;
850 return;
851 }
852 }
853}
854
855/*
856 * Add a string property to a node, or update it if it already exists.
857 *
858 * @param node The device tree node to add to.
859 * @param name The name of the new property.
860 * @param str The zero-terminated string to be stored in the property.
861 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200862void dt_add_string_prop(struct device_tree_node *node, const char *name,
863 char *str)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200864{
865 dt_add_bin_prop(node, name, str, strlen(str) + 1);
866}
867
868/*
869 * Add a 32-bit integer property to a node, or update it if it already exists.
870 *
871 * @param node The device tree node to add to.
872 * @param name The name of the new property.
873 * @param val The integer to be stored in the property.
874 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200875void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200876{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200877 u32 *val_ptr = malloc(sizeof(val));
878 if (!val_ptr)
879 return;
880 *val_ptr = htobe32(val);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200881 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
882}
883
884/*
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200885 * Add a 64-bit integer property to a node, or update it if it already exists.
886 *
887 * @param node The device tree node to add to.
888 * @param name The name of the new property.
889 * @param val The integer to be stored in the property.
890 */
891void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
892{
893 u64 *val_ptr = malloc(sizeof(val));
894 if (!val_ptr)
895 return;
896 *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;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200915 u8 *data = malloc(length);
916 if (!data)
917 return;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200918 u8 *cur = data;
919
920 for (i = 0; i < count; i++) {
921 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
922 cur += addr_cells * sizeof(u32);
923 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
924 cur += size_cells * sizeof(u32);
925 }
926
927 dt_add_bin_prop(node, "reg", data, length);
928}
929
930/*
931 * Fixups to apply to a kernel's device tree before booting it.
932 */
933
Patrick Rudolph666c1722018-04-03 09:57:33 +0200934struct list_node device_tree_fixups;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200935
Patrick Rudolph666c1722018-04-03 09:57:33 +0200936int dt_apply_fixups(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200937{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200938 struct device_tree_fixup *fixup;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200939 list_for_each(fixup, device_tree_fixups, list_node) {
940 assert(fixup->fixup);
941 if (fixup->fixup(fixup, tree))
942 return 1;
943 }
944 return 0;
945}
946
Patrick Rudolph666c1722018-04-03 09:57:33 +0200947int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200948 void *data, size_t data_size, int create)
949{
950 char *path_copy, *prop_name;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200951 struct device_tree_node *dt_node;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200952
953 path_copy = strdup(path);
954
955 if (!path_copy) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200956 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
957 path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200958 return 1;
959 }
960
961 prop_name = strrchr(path_copy, '/');
962 if (!prop_name) {
Patrick Rudolph679d6242018-07-11 13:53:04 +0200963 free(path_copy);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200964 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200965 return 1;
966 }
967
968 *prop_name++ = '\0'; /* Separate path from the property name. */
969
970 dt_node = dt_find_node_by_path(tree->root, path_copy, NULL,
971 NULL, create);
972
973 if (!dt_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200974 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200975 create ? "create" : "find", path_copy);
Patrick Rudolph679d6242018-07-11 13:53:04 +0200976 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200977 return 1;
978 }
979
980 dt_add_bin_prop(dt_node, prop_name, data, data_size);
Patrick Rudolph679d6242018-07-11 13:53:04 +0200981 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200982
983 return 0;
984}
985
986/*
987 * Prepare the /reserved-memory/ node.
988 *
989 * Technically, this can be called more than one time, to init and/or retrieve
990 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
991 *
992 * @tree: Device tree to add/retrieve from.
993 * @return: The /reserved-memory/ node (or NULL, if error).
994 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200995struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200996{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200997 struct device_tree_node *reserved;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200998 u32 addr = 0, size = 0;
999
1000 reserved = dt_find_node_by_path(tree->root, "reserved-memory", &addr,
1001 &size, 1);
1002 if (!reserved)
1003 return NULL;
1004
1005 // Binding doc says this should have the same #{address,size}-cells as
1006 // the root.
1007 dt_add_u32_prop(reserved, "#address-cells", addr);
1008 dt_add_u32_prop(reserved, "#size-cells", size);
1009 // Binding doc says this should be empty (i.e., 1:1 mapping from root).
1010 dt_add_bin_prop(reserved, "ranges", NULL, 0);
1011
1012 return reserved;
1013}