blob: bb40eee2316b04d5b9dc20c1f1e98b048833de01 [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
Patrick Rudolph666c1722018-04-03 09:57:33 +0200223 uint32_t struct_offset = be32toh(header->structure_offset);
224 uint32_t strings_offset = be32toh(header->strings_offset);
225 uint32_t reserve_offset = be32toh(header->reserve_map_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200226 uint32_t min_offset = 0;
227 min_offset = MIN(struct_offset, strings_offset);
228 min_offset = MIN(min_offset, reserve_offset);
229 // Assume everything up to the first non-header component is part of
230 // the header and needs to be preserved. This will protect us against
231 // new elements being added in the future.
232 tree->header_size = min_offset;
233
Patrick Rudolph666c1722018-04-03 09:57:33 +0200234 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200235 uint32_t offset = reserve_offset;
236 int size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200237 struct list_node *last = &tree->reserve_map;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200238 while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) {
239 list_insert_after(&entry->list_node, last);
240 last = &entry->list_node;
241
242 offset += size;
243 }
244
245 fdt_unflatten_node(blob, struct_offset, &tree->root);
246
247 return tree;
248}
249
250
251
252/*
Patrick Rudolph666c1722018-04-03 09:57:33 +0200253 * Functions to find the size of the device tree if it was flattened.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200254 */
255
Patrick Rudolph666c1722018-04-03 09:57:33 +0200256static void dt_flat_prop_size(struct device_tree_property *prop,
257 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200258{
259 // Starting token.
260 *struct_size += sizeof(uint32_t);
261 // Size.
262 *struct_size += sizeof(uint32_t);
263 // Name offset.
264 *struct_size += sizeof(uint32_t);
265 // Property value.
Patrick Rudolph666c1722018-04-03 09:57:33 +0200266 *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200267
268 // Property name.
269 *strings_size += strlen(prop->prop.name) + 1;
270}
271
Patrick Rudolph666c1722018-04-03 09:57:33 +0200272static void dt_flat_node_size(struct device_tree_node *node,
273 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200274{
275 // Starting token.
276 *struct_size += sizeof(uint32_t);
277 // Node name.
Patrick Rudolph666c1722018-04-03 09:57:33 +0200278 *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200279
Patrick Rudolph666c1722018-04-03 09:57:33 +0200280 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200281 list_for_each(prop, node->properties, list_node)
282 dt_flat_prop_size(prop, struct_size, strings_size);
283
Patrick Rudolph666c1722018-04-03 09:57:33 +0200284 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200285 list_for_each(child, node->children, list_node)
286 dt_flat_node_size(child, struct_size, strings_size);
287
288 // End token.
289 *struct_size += sizeof(uint32_t);
290}
291
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200292uint32_t dt_flat_size(const struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200293{
294 uint32_t size = tree->header_size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200295 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200296 list_for_each(entry, tree->reserve_map, list_node)
297 size += sizeof(uint64_t) * 2;
298 size += sizeof(uint64_t) * 2;
299
300 uint32_t struct_size = 0;
301 uint32_t strings_size = 0;
302 dt_flat_node_size(tree->root, &struct_size, &strings_size);
303
304 size += struct_size;
305 // End token.
306 size += sizeof(uint32_t);
307
308 size += strings_size;
309
310 return size;
311}
312
313
314
315/*
316 * Functions to flatten a device tree.
317 */
318
Patrick Rudolph666c1722018-04-03 09:57:33 +0200319static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200320 void **map_start)
321{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200322 ((uint64_t *)*map_start)[0] = htobe64(entry->start);
323 ((uint64_t *)*map_start)[1] = htobe64(entry->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200324 *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2;
325}
326
Patrick Rudolph666c1722018-04-03 09:57:33 +0200327static void dt_flatten_prop(struct device_tree_property *prop,
328 void **struct_start, void *strings_base,
329 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200330{
331 uint8_t *dstruct = (uint8_t *)*struct_start;
332 uint8_t *dstrings = (uint8_t *)*strings_start;
333
Julius Wernera5ea3a22019-05-07 17:38:12 -0700334 be32enc(dstruct, FDT_TOKEN_PROPERTY);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200335 dstruct += sizeof(uint32_t);
336
Julius Wernera5ea3a22019-05-07 17:38:12 -0700337 be32enc(dstruct, prop->prop.size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200338 dstruct += sizeof(uint32_t);
339
340 uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base;
Julius Wernera5ea3a22019-05-07 17:38:12 -0700341 be32enc(dstruct, name_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200342 dstruct += sizeof(uint32_t);
343
344 strcpy((char *)dstrings, prop->prop.name);
345 dstrings += strlen(prop->prop.name) + 1;
346
347 memcpy(dstruct, prop->prop.data, prop->prop.size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200348 dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200349
350 *struct_start = dstruct;
351 *strings_start = dstrings;
352}
353
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200354static void dt_flatten_node(const struct device_tree_node *node,
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_BEGIN_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200362 dstruct += sizeof(uint32_t);
363
364 strcpy((char *)dstruct, node->name);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200365 dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200366
Patrick Rudolph666c1722018-04-03 09:57:33 +0200367 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200368 list_for_each(prop, node->properties, list_node)
369 dt_flatten_prop(prop, (void **)&dstruct, strings_base,
370 (void **)&dstrings);
371
Patrick Rudolph666c1722018-04-03 09:57:33 +0200372 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200373 list_for_each(child, node->children, list_node)
374 dt_flatten_node(child, (void **)&dstruct, strings_base,
375 (void **)&dstrings);
376
Julius Wernera5ea3a22019-05-07 17:38:12 -0700377 be32enc(dstruct, FDT_TOKEN_END_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200378 dstruct += sizeof(uint32_t);
379
380 *struct_start = dstruct;
381 *strings_start = dstrings;
382}
383
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200384void dt_flatten(const struct device_tree *tree, void *start_dest)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200385{
386 uint8_t *dest = (uint8_t *)start_dest;
387
388 memcpy(dest, tree->header, tree->header_size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200389 struct fdt_header *header = (struct fdt_header *)dest;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200390 dest += tree->header_size;
391
Patrick Rudolph666c1722018-04-03 09:57:33 +0200392 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200393 list_for_each(entry, tree->reserve_map, list_node)
394 dt_flatten_map_entry(entry, (void **)&dest);
395 ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0;
396 dest += sizeof(uint64_t) * 2;
397
398 uint32_t struct_size = 0;
399 uint32_t strings_size = 0;
400 dt_flat_node_size(tree->root, &struct_size, &strings_size);
401
402 uint8_t *struct_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200403 header->structure_offset = htobe32(dest - (uint8_t *)start_dest);
404 header->structure_size = htobe32(struct_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200405 dest += struct_size;
406
Patrick Rudolph666c1722018-04-03 09:57:33 +0200407 *((uint32_t *)dest) = htobe32(FDT_TOKEN_END);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200408 dest += sizeof(uint32_t);
409
410 uint8_t *strings_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200411 header->strings_offset = htobe32(dest - (uint8_t *)start_dest);
412 header->strings_size = htobe32(strings_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200413 dest += strings_size;
414
415 dt_flatten_node(tree->root, (void **)&struct_start, strings_start,
416 (void **)&strings_start);
417
Patrick Rudolph666c1722018-04-03 09:57:33 +0200418 header->totalsize = htobe32(dest - (uint8_t *)start_dest);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200419}
420
421
422
423/*
424 * Functions for printing a non-flattened device tree.
425 */
426
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200427static void print_node(const struct device_tree_node *node, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200428{
429 print_indent(depth);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200430 printk(BIOS_DEBUG, "name = %s\n", node->name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200431
Patrick Rudolph666c1722018-04-03 09:57:33 +0200432 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200433 list_for_each(prop, node->properties, list_node)
434 print_property(&prop->prop, depth + 1);
435
Patrick Rudolph666c1722018-04-03 09:57:33 +0200436 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200437 list_for_each(child, node->children, list_node)
438 print_node(child, depth + 1);
439}
440
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200441void dt_print_node(const struct device_tree_node *node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200442{
443 print_node(node, 0);
444}
445
446
447
448/*
449 * Functions for reading and manipulating an unflattened device tree.
450 */
451
452/*
453 * Read #address-cells and #size-cells properties from a node.
454 *
455 * @param node The device tree node to read from.
456 * @param addrcp Pointer to store #address-cells in, skipped if NULL.
457 * @param sizecp Pointer to store #size-cells in, skipped if NULL.
458 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200459void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
460 u32 *sizecp)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200461{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200462 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200463 list_for_each(prop, node->properties, list_node) {
464 if (addrcp && !strcmp("#address-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700465 *addrcp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200466 if (sizecp && !strcmp("#size-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700467 *sizecp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200468 }
469}
470
471/*
472 * Find a node from a device tree path, relative to a parent node.
473 *
474 * @param parent The node from which to start the relative path lookup.
475 * @param path An array of path component strings that will be looked
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200476 * up in order to find the node. Must be terminated with
477 * a NULL pointer. Example: {'firmware', 'coreboot', NULL}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200478 * @param addrcp Pointer that will be updated with any #address-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200479 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200480 * @param sizecp Pointer that will be updated with any #size-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200481 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200482 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
483 * @return The found/created node, or NULL.
484 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200485struct device_tree_node *dt_find_node(struct device_tree_node *parent,
486 const char **path, u32 *addrcp,
487 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200488{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200489 struct device_tree_node *node, *found = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200490
491 // Update #address-cells and #size-cells for this level.
492 dt_read_cell_props(parent, addrcp, sizecp);
493
494 if (!*path)
495 return parent;
496
497 // Find the next node in the path, if it exists.
498 list_for_each(node, parent->children, list_node) {
499 if (!strcmp(node->name, *path)) {
500 found = node;
501 break;
502 }
503 }
504
505 // Otherwise create it or return NULL.
506 if (!found) {
507 if (!create)
508 return NULL;
509
Julius Werner9636a102019-05-03 17:36:43 -0700510 found = malloc(sizeof(*found));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200511 if (!found)
512 return NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200513 found->name = strdup(*path);
514 if (!found->name)
515 return NULL;
516
517 list_insert_after(&found->list_node, &parent->children);
518 }
519
520 return dt_find_node(found, path + 1, addrcp, sizecp, create);
521}
522
523/*
524 * Find a node from a string device tree path, relative to a parent node.
525 *
526 * @param parent The node from which to start the relative path lookup.
527 * @param path A string representing a path in the device tree, with
528 * nodes separated by '/'. Example: "soc/firmware/coreboot"
529 * @param addrcp Pointer that will be updated with any #address-cells
530 * value found in the path. May be NULL to ignore.
531 * @param sizecp Pointer that will be updated with any #size-cells
532 * value found in the path. May be NULL to ignore.
533 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
534 * @return The found/created node, or NULL.
535 *
536 * It is the caller responsibility to provide the correct path string, namely
537 * not starting or ending with a '/', and not having "//" anywhere in it.
538 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200539struct device_tree_node *dt_find_node_by_path(struct device_tree_node *parent,
540 const char *path, u32 *addrcp,
541 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200542{
543 char *dup_path = strdup(path);
544 /* Hopefully enough depth for any node. */
545 const char *path_array[15];
546 int i;
547 char *next_slash;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200548 struct device_tree_node *node = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200549
550 if (!dup_path)
551 return NULL;
552
553 next_slash = dup_path;
554 path_array[0] = dup_path;
555 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
556
557 next_slash = strchr(next_slash, '/');
558 if (!next_slash)
559 break;
560
561 *next_slash++ = '\0';
562 path_array[i] = next_slash;
563 }
564
565 if (!next_slash) {
566 path_array[i] = NULL;
567 node = dt_find_node(parent, path_array,
568 addrcp, sizecp, create);
569 }
570
571 free(dup_path);
572 return node;
573}
574
575/*
576 * Check if given node is compatible.
577 *
578 * @param node The node which is to be checked for compatible property.
579 * @param compat The compatible string to match.
580 * @return 1 = compatible, 0 = not compatible.
581 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200582static int dt_check_compat_match(struct device_tree_node *node,
583 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200584{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200585 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200586
587 list_for_each(prop, node->properties, list_node) {
588 if (!strcmp("compatible", prop->prop.name)) {
589 size_t bytes = prop->prop.size;
590 const char *str = prop->prop.data;
591 while (bytes > 0) {
592 if (!strncmp(compat, str, bytes))
593 return 1;
594 size_t len = strnlen(str, bytes) + 1;
595 if (bytes <= len)
596 break;
597 str += len;
598 bytes -= len;
599 }
600 break;
601 }
602 }
603
604 return 0;
605}
606
607/*
608 * Find a node from a compatible string, in the subtree of a parent node.
609 *
610 * @param parent The parent node under which to look.
611 * @param compat The compatible string to find.
612 * @return The found node, or NULL.
613 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200614struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
615 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200616{
617 // Check if the parent node itself is compatible.
618 if (dt_check_compat_match(parent, compat))
619 return parent;
620
Patrick Rudolph666c1722018-04-03 09:57:33 +0200621 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200622 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200623 struct device_tree_node *found = dt_find_compat(child, compat);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200624 if (found)
625 return found;
626 }
627
628 return NULL;
629}
630
631/*
632 * Find the next compatible child of a given parent. All children upto the
633 * child passed in by caller are ignored. If child is NULL, it considers all the
634 * children to find the first child which is compatible.
635 *
636 * @param parent The parent node under which to look.
637 * @param child The child node to start search from (exclusive). If NULL
638 * consider all children.
639 * @param compat The compatible string to find.
640 * @return The found node, or NULL.
641 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200642struct device_tree_node *
643dt_find_next_compat_child(struct device_tree_node *parent,
644 struct device_tree_node *child,
645 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200646{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200647 struct device_tree_node *next;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200648 int ignore = 0;
649
650 if (child)
651 ignore = 1;
652
653 list_for_each(next, parent->children, list_node) {
654 if (ignore) {
655 if (child == next)
656 ignore = 0;
657 continue;
658 }
659
660 if (dt_check_compat_match(next, compat))
661 return next;
662 }
663
664 return NULL;
665}
666
667/*
668 * Find a node with matching property value, in the subtree of a parent node.
669 *
670 * @param parent The parent node under which to look.
671 * @param name The property name to look for.
672 * @param data The property value to look for.
673 * @param size The property size.
674 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200675struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
676 const char *name, void *data,
677 size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200678{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200679 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200680
681 /* Check if parent itself has the required property value. */
682 list_for_each(prop, parent->properties, list_node) {
683 if (!strcmp(name, prop->prop.name)) {
684 size_t bytes = prop->prop.size;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200685 const void *prop_data = prop->prop.data;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200686 if (size != bytes)
687 break;
688 if (!memcmp(data, prop_data, size))
689 return parent;
690 break;
691 }
692 }
693
Patrick Rudolph666c1722018-04-03 09:57:33 +0200694 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200695 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200696 struct device_tree_node *found = dt_find_prop_value(child, name,
697 data, size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200698 if (found)
699 return found;
700 }
701 return NULL;
702}
703
Patrick Rudolphc38960d2018-05-08 11:18:54 +0200704/**
705 * Find the phandle of a node.
706 *
707 * @param node Pointer to node containing the phandle
708 * @return Zero on error, the phandle on success
709 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200710uint32_t dt_get_phandle(const struct device_tree_node *node)
Patrick Rudolphc38960d2018-05-08 11:18:54 +0200711{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200712 const uint32_t *phandle;
Patrick Rudolphc38960d2018-05-08 11:18:54 +0200713 size_t len;
714
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200715 dt_find_bin_prop(node, "phandle", (const void **)&phandle, &len);
Patrick Rudolphc38960d2018-05-08 11:18:54 +0200716 if (phandle != NULL && len == sizeof(*phandle))
717 return be32_to_cpu(*phandle);
718
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200719 dt_find_bin_prop(node, "linux,phandle", (const void **)&phandle, &len);
Patrick Rudolphc38960d2018-05-08 11:18:54 +0200720 if (phandle != NULL && len == sizeof(*phandle))
721 return be32_to_cpu(*phandle);
722
723 return 0;
724}
725
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200726/*
727 * Write an arbitrary sized big-endian integer into a pointer.
728 *
729 * @param dest Pointer to the DT property data buffer to write.
Elyes HAOUAS1ec76442018-08-07 12:20:04 +0200730 * @param src The integer to write (in CPU endianness).
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200731 * @param length the length of the destination integer in bytes.
732 */
733void dt_write_int(u8 *dest, u64 src, size_t length)
734{
735 while (length--) {
736 dest[length] = (u8)src;
737 src >>= 8;
738 }
739}
740
741/*
Patrick Rudolph5ccc7312018-05-30 15:05:28 +0200742 * Delete a property by name in a given node if it exists.
743 *
744 * @param node The device tree node to operate on.
745 * @param name The name of the property to delete.
746 */
747void dt_delete_prop(struct device_tree_node *node, const char *name)
748{
749 struct device_tree_property *prop;
750
751 list_for_each(prop, node->properties, list_node) {
752 if (!strcmp(prop->prop.name, name)) {
753 list_remove(&prop->list_node);
754 return;
755 }
756 }
757}
758
759/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200760 * Add an arbitrary property to a node, or update it if it already exists.
761 *
762 * @param node The device tree node to add to.
763 * @param name The name of the new property.
764 * @param data The raw data blob to be stored in the property.
765 * @param size The size of data in bytes.
766 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200767void dt_add_bin_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200768 const void *data, size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200769{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200770 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200771
772 list_for_each(prop, node->properties, list_node) {
773 if (!strcmp(prop->prop.name, name)) {
774 prop->prop.data = data;
775 prop->prop.size = size;
776 return;
777 }
778 }
779
Julius Werner9636a102019-05-03 17:36:43 -0700780 prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200781 list_insert_after(&prop->list_node, &node->properties);
782 prop->prop.name = name;
783 prop->prop.data = data;
784 prop->prop.size = size;
785}
786
787/*
788 * Find given string property in a node and return its content.
789 *
790 * @param node The device tree node to search.
791 * @param name The name of the property.
792 * @return The found string, or NULL.
793 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200794const char *dt_find_string_prop(const struct device_tree_node *node,
795 const char *name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200796{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200797 const void *content;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200798 size_t size;
799
800 dt_find_bin_prop(node, name, &content, &size);
801
802 return content;
803}
804
805/*
806 * Find given property in a node.
807 *
808 * @param node The device tree node to search.
809 * @param name The name of the property.
810 * @param data Pointer to return raw data blob in the property.
811 * @param size Pointer to return the size of data in bytes.
812 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200813void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
814 const void **data, size_t *size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200815{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200816 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200817
818 *data = NULL;
819 *size = 0;
820
821 list_for_each(prop, node->properties, list_node) {
822 if (!strcmp(prop->prop.name, name)) {
823 *data = prop->prop.data;
824 *size = prop->prop.size;
825 return;
826 }
827 }
828}
829
830/*
831 * Add a string property to a node, or update it if it already exists.
832 *
833 * @param node The device tree node to add to.
834 * @param name The name of the new property.
835 * @param str The zero-terminated string to be stored in the property.
836 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200837void dt_add_string_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200838 const char *str)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200839{
840 dt_add_bin_prop(node, name, str, strlen(str) + 1);
841}
842
843/*
844 * Add a 32-bit integer property to a node, or update it if it already exists.
845 *
846 * @param node The device tree node to add to.
847 * @param name The name of the new property.
848 * @param val The integer to be stored in the property.
849 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200850void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200851{
Julius Werner9636a102019-05-03 17:36:43 -0700852 u32 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200853 *val_ptr = htobe32(val);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200854 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
855}
856
857/*
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200858 * Add a 64-bit integer property to a node, or update it if it already exists.
859 *
860 * @param node The device tree node to add to.
861 * @param name The name of the new property.
862 * @param val The integer to be stored in the property.
863 */
864void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
865{
Julius Werner9636a102019-05-03 17:36:43 -0700866 u64 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200867 *val_ptr = htobe64(val);
868 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
869}
870
871/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200872 * Add a 'reg' address list property to a node, or update it if it exists.
873 *
874 * @param node The device tree node to add to.
875 * @param addrs Array of address values to be stored in the property.
876 * @param sizes Array of corresponding size values to 'addrs'.
877 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
878 * @param addr_cells Value of #address-cells property valid for this node.
879 * @param size_cells Value of #size-cells property valid for this node.
880 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200881void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200882 int count, u32 addr_cells, u32 size_cells)
883{
884 int i;
885 size_t length = (addr_cells + size_cells) * sizeof(u32) * count;
Julius Werner9636a102019-05-03 17:36:43 -0700886 u8 *data = xmalloc(length);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200887 u8 *cur = data;
888
889 for (i = 0; i < count; i++) {
890 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
891 cur += addr_cells * sizeof(u32);
892 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
893 cur += size_cells * sizeof(u32);
894 }
895
896 dt_add_bin_prop(node, "reg", data, length);
897}
898
899/*
900 * Fixups to apply to a kernel's device tree before booting it.
901 */
902
Patrick Rudolph666c1722018-04-03 09:57:33 +0200903struct list_node device_tree_fixups;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200904
Patrick Rudolph666c1722018-04-03 09:57:33 +0200905int dt_apply_fixups(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200906{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200907 struct device_tree_fixup *fixup;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200908 list_for_each(fixup, device_tree_fixups, list_node) {
909 assert(fixup->fixup);
910 if (fixup->fixup(fixup, tree))
911 return 1;
912 }
913 return 0;
914}
915
Patrick Rudolph666c1722018-04-03 09:57:33 +0200916int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200917 void *data, size_t data_size, int create)
918{
919 char *path_copy, *prop_name;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200920 struct device_tree_node *dt_node;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200921
922 path_copy = strdup(path);
923
924 if (!path_copy) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200925 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
926 path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200927 return 1;
928 }
929
930 prop_name = strrchr(path_copy, '/');
931 if (!prop_name) {
Patrick Rudolph679d6242018-07-11 13:53:04 +0200932 free(path_copy);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200933 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200934 return 1;
935 }
936
937 *prop_name++ = '\0'; /* Separate path from the property name. */
938
939 dt_node = dt_find_node_by_path(tree->root, path_copy, NULL,
940 NULL, create);
941
942 if (!dt_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200943 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200944 create ? "create" : "find", path_copy);
Patrick Rudolph679d6242018-07-11 13:53:04 +0200945 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200946 return 1;
947 }
948
949 dt_add_bin_prop(dt_node, prop_name, data, data_size);
Patrick Rudolph679d6242018-07-11 13:53:04 +0200950 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200951
952 return 0;
953}
954
955/*
956 * Prepare the /reserved-memory/ node.
957 *
958 * Technically, this can be called more than one time, to init and/or retrieve
959 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
960 *
961 * @tree: Device tree to add/retrieve from.
962 * @return: The /reserved-memory/ node (or NULL, if error).
963 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200964struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200965{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200966 struct device_tree_node *reserved;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200967 u32 addr = 0, size = 0;
968
969 reserved = dt_find_node_by_path(tree->root, "reserved-memory", &addr,
970 &size, 1);
971 if (!reserved)
972 return NULL;
973
974 // Binding doc says this should have the same #{address,size}-cells as
975 // the root.
976 dt_add_u32_prop(reserved, "#address-cells", addr);
977 dt_add_u32_prop(reserved, "#size-cells", size);
978 // Binding doc says this should be empty (i.e., 1:1 mapping from root).
979 dt_add_bin_prop(reserved, "ranges", NULL, 0);
980
981 return reserved;
982}