blob: 3c4bd244628c827c3cf19b2f051b652bc52f877e [file] [log] [blame]
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001/*
2 * Copyright 2013 Google Inc.
Patrick Rudolph666c1722018-04-03 09:57:33 +02003 * Copyright 2018-present Facebook, Inc.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02004 *
Patrick Rudolph666c1722018-04-03 09:57:33 +02005 * Taken from depthcharge: src/base/device_tree.c
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but without any warranty; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <assert.h>
Julius Werner9636a102019-05-03 17:36:43 -070019#include <commonlib/stdlib.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +020020#include <console/console.h>
21#include <device_tree.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020022#include <endian.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020023#include <stdint.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +020024#include <string.h>
25#include <stddef.h>
26#include <stdlib.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020027
28/*
29 * Functions for picking apart flattened trees.
30 */
31
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020032int fdt_next_property(const void *blob, uint32_t offset,
33 struct fdt_property *prop)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020034{
Patrick Rudolph666c1722018-04-03 09:57:33 +020035 struct fdt_header *header = (struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020036 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
37
38 int index = 0;
Patrick Rudolph666c1722018-04-03 09:57:33 +020039 if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020040 return 0;
41
Patrick Rudolph666c1722018-04-03 09:57:33 +020042 uint32_t size = be32toh(ptr[index++]);
43 uint32_t name_offset = be32toh(ptr[index++]);
44 name_offset += be32toh(header->strings_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020045
46 if (prop) {
47 prop->name = (char *)((uint8_t *)blob + name_offset);
48 prop->data = &ptr[index];
49 prop->size = size;
50 }
51
Patrick Rudolph666c1722018-04-03 09:57:33 +020052 index += DIV_ROUND_UP(size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020053
Patrick Rudolph666c1722018-04-03 09:57:33 +020054 return index * sizeof(uint32_t);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020055}
56
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020057int fdt_node_name(const void *blob, uint32_t offset, const char **name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020058{
59 uint8_t *ptr = ((uint8_t *)blob) + offset;
Julius Wernera5ea3a22019-05-07 17:38:12 -070060 if (be32dec(ptr) != FDT_TOKEN_BEGIN_NODE)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020061 return 0;
62
63 ptr += 4;
64 if (name)
65 *name = (char *)ptr;
Patrick Rudolph666c1722018-04-03 09:57:33 +020066 return ALIGN_UP(strlen((char *)ptr) + 1, sizeof(uint32_t)) + 4;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020067}
68
Julius Werner6702b682019-05-03 18:13:53 -070069static int dt_prop_is_phandle(struct device_tree_property *prop)
70{
71 return !(strcmp("phandle", prop->prop.name) &&
72 strcmp("linux,phandle", prop->prop.name));
73}
74
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020075
76
77/*
78 * Functions for printing flattened trees.
79 */
80
81static void print_indent(int depth)
82{
Julius Werner0d746532019-05-06 19:35:56 -070083 printk(BIOS_DEBUG, "%*s", depth * 8, "");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020084}
85
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020086static void print_property(const struct fdt_property *prop, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020087{
Julius Werner0d746532019-05-06 19:35:56 -070088 int is_string = prop->size > 0 &&
89 ((char *)prop->data)[prop->size - 1] == '\0';
90
91 if (is_string)
92 for (const char *c = prop->data; *c != '\0'; c++)
93 if (!isprint(*c))
94 is_string = 0;
95
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020096 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -070097 if (is_string) {
98 printk(BIOS_DEBUG, "%s = \"%s\";\n",
99 prop->name, (const char *)prop->data);
100 } else {
101 printk(BIOS_DEBUG, "%s = < ", prop->name);
102 for (int i = 0; i < MIN(128, prop->size); i += 4) {
103 uint32_t val = 0;
104 for (int j = 0; j < MIN(4, prop->size - i); j++)
105 val |= ((uint8_t *)prop->data)[i + j] <<
106 (24 - j * 8);
107 printk(BIOS_DEBUG, "%#.2x ", val);
108 }
109 if (prop->size > 128)
110 printk(BIOS_DEBUG, "...");
111 printk(BIOS_DEBUG, ">;\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200112 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200113}
114
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200115static int print_flat_node(const void *blob, uint32_t start_offset, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200116{
117 int offset = start_offset;
118 const char *name;
119 int size;
120
121 size = fdt_node_name(blob, offset, &name);
122 if (!size)
123 return 0;
124 offset += size;
125
126 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -0700127 printk(BIOS_DEBUG, "%s {\n", name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200128
Patrick Rudolph666c1722018-04-03 09:57:33 +0200129 struct fdt_property prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200130 while ((size = fdt_next_property(blob, offset, &prop))) {
131 print_property(&prop, depth + 1);
132
133 offset += size;
134 }
135
Julius Werner0d746532019-05-06 19:35:56 -0700136 printk(BIOS_DEBUG, "\n"); // empty line between props and nodes
137
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200138 while ((size = print_flat_node(blob, offset, depth + 1)))
139 offset += size;
140
Julius Werner0d746532019-05-06 19:35:56 -0700141 print_indent(depth);
142 printk(BIOS_DEBUG, "}\n");
143
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200144 return offset - start_offset + sizeof(uint32_t);
145}
146
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200147void fdt_print_node(const void *blob, uint32_t offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200148{
149 print_flat_node(blob, offset, 0);
150}
151
152
153
154/*
155 * A utility function to skip past nodes in flattened trees.
156 */
157
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200158int fdt_skip_node(const void *blob, uint32_t start_offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200159{
160 int offset = start_offset;
161 int size;
162
163 const char *name;
164 size = fdt_node_name(blob, offset, &name);
165 if (!size)
166 return 0;
167 offset += size;
168
169 while ((size = fdt_next_property(blob, offset, NULL)))
170 offset += size;
171
172 while ((size = fdt_skip_node(blob, offset)))
173 offset += size;
174
175 return offset - start_offset + sizeof(uint32_t);
176}
177
178
179
180/*
181 * Functions to turn a flattened tree into an unflattened one.
182 */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200183
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200184static int fdt_unflatten_node(const void *blob, uint32_t start_offset,
Julius Werner6702b682019-05-03 18:13:53 -0700185 struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200186 struct device_tree_node **new_node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200187{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200188 struct list_node *last;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200189 int offset = start_offset;
190 const char *name;
191 int size;
192
193 size = fdt_node_name(blob, offset, &name);
194 if (!size)
195 return 0;
196 offset += size;
197
Julius Werner9636a102019-05-03 17:36:43 -0700198 struct device_tree_node *node = xzalloc(sizeof(*node));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200199 *new_node = node;
200 node->name = name;
201
Patrick Rudolph666c1722018-04-03 09:57:33 +0200202 struct fdt_property fprop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200203 last = &node->properties;
204 while ((size = fdt_next_property(blob, offset, &fprop))) {
Julius Werner9636a102019-05-03 17:36:43 -0700205 struct device_tree_property *prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200206 prop->prop = fprop;
207
Julius Werner6702b682019-05-03 18:13:53 -0700208 if (dt_prop_is_phandle(prop)) {
209 node->phandle = be32dec(prop->prop.data);
210 if (node->phandle > tree->max_phandle)
211 tree->max_phandle = node->phandle;
212 }
213
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200214 list_insert_after(&prop->list_node, last);
215 last = &prop->list_node;
216
217 offset += size;
218 }
219
Patrick Rudolph666c1722018-04-03 09:57:33 +0200220 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200221 last = &node->children;
Julius Werner6702b682019-05-03 18:13:53 -0700222 while ((size = fdt_unflatten_node(blob, offset, tree, &child))) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200223 list_insert_after(&child->list_node, last);
224 last = &child->list_node;
225
226 offset += size;
227 }
228
229 return offset - start_offset + sizeof(uint32_t);
230}
231
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200232static int fdt_unflatten_map_entry(const void *blob, uint32_t offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200233 struct device_tree_reserve_map_entry **new)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200234{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200235 const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset);
236 const uint64_t start = be64toh(ptr[0]);
237 const uint64_t size = be64toh(ptr[1]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200238
239 if (!size)
240 return 0;
241
Julius Werner9636a102019-05-03 17:36:43 -0700242 struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200243 *new = entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200244 entry->start = start;
245 entry->size = size;
246
247 return sizeof(uint64_t) * 2;
248}
249
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200250struct device_tree *fdt_unflatten(const void *blob)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200251{
Julius Werner9636a102019-05-03 17:36:43 -0700252 struct device_tree *tree = xzalloc(sizeof(*tree));
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200253 const struct fdt_header *header = (const struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200254 tree->header = header;
255
Julius Werner73eaec82019-05-03 17:58:07 -0700256 uint32_t magic = be32toh(header->magic);
257 uint32_t version = be32toh(header->version);
258 uint32_t last_comp_version = be32toh(header->last_comp_version);
259
260 if (magic != FDT_HEADER_MAGIC) {
261 printk(BIOS_DEBUG, "Invalid device tree magic %#.8x!\n", magic);
262 return NULL;
263 }
264 if (last_comp_version > FDT_SUPPORTED_VERSION) {
265 printk(BIOS_DEBUG, "Unsupported device tree version %u(>=%u)\n",
266 version, last_comp_version);
267 return NULL;
268 }
269 if (version > FDT_SUPPORTED_VERSION)
270 printk(BIOS_DEBUG,
271 "NOTE: FDT version %u too new, should add support!\n",
272 version);
273
Patrick Rudolph666c1722018-04-03 09:57:33 +0200274 uint32_t struct_offset = be32toh(header->structure_offset);
275 uint32_t strings_offset = be32toh(header->strings_offset);
276 uint32_t reserve_offset = be32toh(header->reserve_map_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200277 uint32_t min_offset = 0;
278 min_offset = MIN(struct_offset, strings_offset);
279 min_offset = MIN(min_offset, reserve_offset);
280 // Assume everything up to the first non-header component is part of
281 // the header and needs to be preserved. This will protect us against
282 // new elements being added in the future.
283 tree->header_size = min_offset;
284
Patrick Rudolph666c1722018-04-03 09:57:33 +0200285 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200286 uint32_t offset = reserve_offset;
287 int size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200288 struct list_node *last = &tree->reserve_map;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200289 while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) {
290 list_insert_after(&entry->list_node, last);
291 last = &entry->list_node;
292
293 offset += size;
294 }
295
Julius Werner6702b682019-05-03 18:13:53 -0700296 fdt_unflatten_node(blob, struct_offset, tree, &tree->root);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200297
298 return tree;
299}
300
301
302
303/*
Patrick Rudolph666c1722018-04-03 09:57:33 +0200304 * Functions to find the size of the device tree if it was flattened.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200305 */
306
Patrick Rudolph666c1722018-04-03 09:57:33 +0200307static void dt_flat_prop_size(struct device_tree_property *prop,
308 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200309{
310 // Starting token.
311 *struct_size += sizeof(uint32_t);
312 // Size.
313 *struct_size += sizeof(uint32_t);
314 // Name offset.
315 *struct_size += sizeof(uint32_t);
316 // Property value.
Patrick Rudolph666c1722018-04-03 09:57:33 +0200317 *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200318
319 // Property name.
320 *strings_size += strlen(prop->prop.name) + 1;
321}
322
Patrick Rudolph666c1722018-04-03 09:57:33 +0200323static void dt_flat_node_size(struct device_tree_node *node,
324 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200325{
326 // Starting token.
327 *struct_size += sizeof(uint32_t);
328 // Node name.
Patrick Rudolph666c1722018-04-03 09:57:33 +0200329 *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200330
Patrick Rudolph666c1722018-04-03 09:57:33 +0200331 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200332 list_for_each(prop, node->properties, list_node)
333 dt_flat_prop_size(prop, struct_size, strings_size);
334
Patrick Rudolph666c1722018-04-03 09:57:33 +0200335 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200336 list_for_each(child, node->children, list_node)
337 dt_flat_node_size(child, struct_size, strings_size);
338
339 // End token.
340 *struct_size += sizeof(uint32_t);
341}
342
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200343uint32_t dt_flat_size(const struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200344{
345 uint32_t size = tree->header_size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200346 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200347 list_for_each(entry, tree->reserve_map, list_node)
348 size += sizeof(uint64_t) * 2;
349 size += sizeof(uint64_t) * 2;
350
351 uint32_t struct_size = 0;
352 uint32_t strings_size = 0;
353 dt_flat_node_size(tree->root, &struct_size, &strings_size);
354
355 size += struct_size;
356 // End token.
357 size += sizeof(uint32_t);
358
359 size += strings_size;
360
361 return size;
362}
363
364
365
366/*
367 * Functions to flatten a device tree.
368 */
369
Patrick Rudolph666c1722018-04-03 09:57:33 +0200370static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200371 void **map_start)
372{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200373 ((uint64_t *)*map_start)[0] = htobe64(entry->start);
374 ((uint64_t *)*map_start)[1] = htobe64(entry->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200375 *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2;
376}
377
Patrick Rudolph666c1722018-04-03 09:57:33 +0200378static void dt_flatten_prop(struct device_tree_property *prop,
379 void **struct_start, void *strings_base,
380 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200381{
382 uint8_t *dstruct = (uint8_t *)*struct_start;
383 uint8_t *dstrings = (uint8_t *)*strings_start;
384
Julius Wernera5ea3a22019-05-07 17:38:12 -0700385 be32enc(dstruct, FDT_TOKEN_PROPERTY);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200386 dstruct += sizeof(uint32_t);
387
Julius Wernera5ea3a22019-05-07 17:38:12 -0700388 be32enc(dstruct, prop->prop.size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200389 dstruct += sizeof(uint32_t);
390
391 uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base;
Julius Wernera5ea3a22019-05-07 17:38:12 -0700392 be32enc(dstruct, name_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200393 dstruct += sizeof(uint32_t);
394
395 strcpy((char *)dstrings, prop->prop.name);
396 dstrings += strlen(prop->prop.name) + 1;
397
398 memcpy(dstruct, prop->prop.data, prop->prop.size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200399 dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200400
401 *struct_start = dstruct;
402 *strings_start = dstrings;
403}
404
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200405static void dt_flatten_node(const struct device_tree_node *node,
406 void **struct_start, void *strings_base,
407 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200408{
409 uint8_t *dstruct = (uint8_t *)*struct_start;
410 uint8_t *dstrings = (uint8_t *)*strings_start;
411
Julius Wernera5ea3a22019-05-07 17:38:12 -0700412 be32enc(dstruct, FDT_TOKEN_BEGIN_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200413 dstruct += sizeof(uint32_t);
414
415 strcpy((char *)dstruct, node->name);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200416 dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200417
Patrick Rudolph666c1722018-04-03 09:57:33 +0200418 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200419 list_for_each(prop, node->properties, list_node)
420 dt_flatten_prop(prop, (void **)&dstruct, strings_base,
421 (void **)&dstrings);
422
Patrick Rudolph666c1722018-04-03 09:57:33 +0200423 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200424 list_for_each(child, node->children, list_node)
425 dt_flatten_node(child, (void **)&dstruct, strings_base,
426 (void **)&dstrings);
427
Julius Wernera5ea3a22019-05-07 17:38:12 -0700428 be32enc(dstruct, FDT_TOKEN_END_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200429 dstruct += sizeof(uint32_t);
430
431 *struct_start = dstruct;
432 *strings_start = dstrings;
433}
434
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200435void dt_flatten(const struct device_tree *tree, void *start_dest)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200436{
437 uint8_t *dest = (uint8_t *)start_dest;
438
439 memcpy(dest, tree->header, tree->header_size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200440 struct fdt_header *header = (struct fdt_header *)dest;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200441 dest += tree->header_size;
442
Patrick Rudolph666c1722018-04-03 09:57:33 +0200443 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200444 list_for_each(entry, tree->reserve_map, list_node)
445 dt_flatten_map_entry(entry, (void **)&dest);
446 ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0;
447 dest += sizeof(uint64_t) * 2;
448
449 uint32_t struct_size = 0;
450 uint32_t strings_size = 0;
451 dt_flat_node_size(tree->root, &struct_size, &strings_size);
452
453 uint8_t *struct_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200454 header->structure_offset = htobe32(dest - (uint8_t *)start_dest);
455 header->structure_size = htobe32(struct_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200456 dest += struct_size;
457
Patrick Rudolph666c1722018-04-03 09:57:33 +0200458 *((uint32_t *)dest) = htobe32(FDT_TOKEN_END);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200459 dest += sizeof(uint32_t);
460
461 uint8_t *strings_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200462 header->strings_offset = htobe32(dest - (uint8_t *)start_dest);
463 header->strings_size = htobe32(strings_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200464 dest += strings_size;
465
466 dt_flatten_node(tree->root, (void **)&struct_start, strings_start,
467 (void **)&strings_start);
468
Patrick Rudolph666c1722018-04-03 09:57:33 +0200469 header->totalsize = htobe32(dest - (uint8_t *)start_dest);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200470}
471
472
473
474/*
475 * Functions for printing a non-flattened device tree.
476 */
477
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200478static void print_node(const struct device_tree_node *node, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200479{
480 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -0700481 if (depth == 0) // root node has no name, print a starting slash
482 printk(BIOS_DEBUG, "/");
483 printk(BIOS_DEBUG, "%s {\n", node->name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200484
Patrick Rudolph666c1722018-04-03 09:57:33 +0200485 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200486 list_for_each(prop, node->properties, list_node)
487 print_property(&prop->prop, depth + 1);
488
Julius Werner0d746532019-05-06 19:35:56 -0700489 printk(BIOS_DEBUG, "\n"); // empty line between props and nodes
490
Patrick Rudolph666c1722018-04-03 09:57:33 +0200491 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200492 list_for_each(child, node->children, list_node)
493 print_node(child, depth + 1);
Julius Werner0d746532019-05-06 19:35:56 -0700494
495 print_indent(depth);
496 printk(BIOS_DEBUG, "};\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200497}
498
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200499void dt_print_node(const struct device_tree_node *node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200500{
501 print_node(node, 0);
502}
503
504
505
506/*
507 * Functions for reading and manipulating an unflattened device tree.
508 */
509
510/*
511 * Read #address-cells and #size-cells properties from a node.
512 *
513 * @param node The device tree node to read from.
514 * @param addrcp Pointer to store #address-cells in, skipped if NULL.
515 * @param sizecp Pointer to store #size-cells in, skipped if NULL.
516 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200517void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
518 u32 *sizecp)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200519{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200520 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200521 list_for_each(prop, node->properties, list_node) {
522 if (addrcp && !strcmp("#address-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700523 *addrcp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200524 if (sizecp && !strcmp("#size-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700525 *sizecp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200526 }
527}
528
529/*
530 * Find a node from a device tree path, relative to a parent node.
531 *
532 * @param parent The node from which to start the relative path lookup.
533 * @param path An array of path component strings that will be looked
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200534 * up in order to find the node. Must be terminated with
535 * a NULL pointer. Example: {'firmware', 'coreboot', NULL}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200536 * @param addrcp Pointer that will be updated with any #address-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200537 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200538 * @param sizecp Pointer that will be updated with any #size-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200539 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200540 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
541 * @return The found/created node, or NULL.
542 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200543struct device_tree_node *dt_find_node(struct device_tree_node *parent,
544 const char **path, u32 *addrcp,
545 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200546{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200547 struct device_tree_node *node, *found = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200548
549 // Update #address-cells and #size-cells for this level.
550 dt_read_cell_props(parent, addrcp, sizecp);
551
552 if (!*path)
553 return parent;
554
555 // Find the next node in the path, if it exists.
556 list_for_each(node, parent->children, list_node) {
557 if (!strcmp(node->name, *path)) {
558 found = node;
559 break;
560 }
561 }
562
563 // Otherwise create it or return NULL.
564 if (!found) {
565 if (!create)
566 return NULL;
567
Julius Werner9636a102019-05-03 17:36:43 -0700568 found = malloc(sizeof(*found));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200569 if (!found)
570 return NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200571 found->name = strdup(*path);
572 if (!found->name)
573 return NULL;
574
575 list_insert_after(&found->list_node, &parent->children);
576 }
577
578 return dt_find_node(found, path + 1, addrcp, sizecp, create);
579}
580
581/*
Julius Wernerf36d53c2019-05-03 18:23:34 -0700582 * Find a node in the tree from a string device tree path.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200583 *
Julius Wernerf36d53c2019-05-03 18:23:34 -0700584 * @param tree The device tree to search.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200585 * @param path A string representing a path in the device tree, with
Julius Wernerfbec63d2019-05-03 18:29:28 -0700586 * nodes separated by '/'. Example: "/firmware/coreboot"
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200587 * @param addrcp Pointer that will be updated with any #address-cells
588 * value found in the path. May be NULL to ignore.
589 * @param sizecp Pointer that will be updated with any #size-cells
590 * value found in the path. May be NULL to ignore.
591 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
592 * @return The found/created node, or NULL.
593 *
Julius Werner6d5695f2019-05-06 19:23:28 -0700594 * It is the caller responsibility to provide a path string that doesn't end
595 * with a '/' and doesn't contain any "//". If the path does not start with a
596 * '/', the first segment is interpreted as an alias. */
Julius Wernerf36d53c2019-05-03 18:23:34 -0700597struct device_tree_node *dt_find_node_by_path(struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200598 const char *path, u32 *addrcp,
599 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200600{
Julius Werner6d5695f2019-05-06 19:23:28 -0700601 char *sub_path;
602 char *duped_str;
603 struct device_tree_node *parent;
604 char *next_slash;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200605 /* Hopefully enough depth for any node. */
606 const char *path_array[15];
607 int i;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200608 struct device_tree_node *node = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200609
Julius Werner6d5695f2019-05-06 19:23:28 -0700610 if (path[0] == '/') { // regular path
611 if (path[1] == '\0') { // special case: "/" is root node
612 dt_read_cell_props(tree->root, addrcp, sizecp);
613 return tree->root;
614 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200615
Julius Werner6d5695f2019-05-06 19:23:28 -0700616 sub_path = duped_str = strdup(&path[1]);
617 if (!sub_path)
618 return NULL;
619
620 parent = tree->root;
621 } else { // alias
622 char *alias;
623
624 alias = duped_str = strdup(path);
625 if (!alias)
626 return NULL;
627
628 sub_path = strchr(alias, '/');
629 if (sub_path)
630 *sub_path = '\0';
631
632 parent = dt_find_node_by_alias(tree, alias);
633 if (!parent) {
634 printk(BIOS_DEBUG,
635 "Could not find node '%s', alias '%s' does not exist\n",
636 path, alias);
637 free(duped_str);
638 return NULL;
639 }
640
641 if (!sub_path) {
642 // it's just the alias, no sub-path
643 free(duped_str);
644 return parent;
645 }
646
647 sub_path++;
648 }
649
650 next_slash = sub_path;
651 path_array[0] = sub_path;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200652 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200653 next_slash = strchr(next_slash, '/');
654 if (!next_slash)
655 break;
656
657 *next_slash++ = '\0';
658 path_array[i] = next_slash;
659 }
660
661 if (!next_slash) {
662 path_array[i] = NULL;
Julius Werner6d5695f2019-05-06 19:23:28 -0700663 node = dt_find_node(parent, path_array,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200664 addrcp, sizecp, create);
665 }
666
Julius Werner6d5695f2019-05-06 19:23:28 -0700667 free(duped_str);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200668 return node;
669}
670
Julius Werner6d5695f2019-05-06 19:23:28 -0700671/*
672 * Find a node from an alias
673 *
674 * @param tree The device tree.
675 * @param alias The alias name.
676 * @return The found node, or NULL.
677 */
678struct device_tree_node *dt_find_node_by_alias(struct device_tree *tree,
679 const char *alias)
680{
681 struct device_tree_node *node;
682 const char *alias_path;
683
684 node = dt_find_node_by_path(tree, "/aliases", NULL, NULL, 0);
685 if (!node)
686 return NULL;
687
688 alias_path = dt_find_string_prop(node, alias);
689 if (!alias_path)
690 return NULL;
691
692 return dt_find_node_by_path(tree, alias_path, NULL, NULL, 0);
693}
694
Julius Werner6702b682019-05-03 18:13:53 -0700695struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root,
696 uint32_t phandle)
697{
698 if (!root)
699 return NULL;
700
701 if (root->phandle == phandle)
702 return root;
703
704 struct device_tree_node *node;
705 struct device_tree_node *result;
706 list_for_each(node, root->children, list_node) {
707 result = dt_find_node_by_phandle(node, phandle);
708 if (result)
709 return result;
710 }
711
712 return NULL;
713}
714
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200715/*
716 * Check if given node is compatible.
717 *
718 * @param node The node which is to be checked for compatible property.
719 * @param compat The compatible string to match.
720 * @return 1 = compatible, 0 = not compatible.
721 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200722static int dt_check_compat_match(struct device_tree_node *node,
723 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200724{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200725 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200726
727 list_for_each(prop, node->properties, list_node) {
728 if (!strcmp("compatible", prop->prop.name)) {
729 size_t bytes = prop->prop.size;
730 const char *str = prop->prop.data;
731 while (bytes > 0) {
732 if (!strncmp(compat, str, bytes))
733 return 1;
734 size_t len = strnlen(str, bytes) + 1;
735 if (bytes <= len)
736 break;
737 str += len;
738 bytes -= len;
739 }
740 break;
741 }
742 }
743
744 return 0;
745}
746
747/*
748 * Find a node from a compatible string, in the subtree of a parent node.
749 *
750 * @param parent The parent node under which to look.
751 * @param compat The compatible string to find.
752 * @return The found node, or NULL.
753 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200754struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
755 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200756{
757 // Check if the parent node itself is compatible.
758 if (dt_check_compat_match(parent, compat))
759 return parent;
760
Patrick Rudolph666c1722018-04-03 09:57:33 +0200761 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200762 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200763 struct device_tree_node *found = dt_find_compat(child, compat);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200764 if (found)
765 return found;
766 }
767
768 return NULL;
769}
770
771/*
772 * Find the next compatible child of a given parent. All children upto the
773 * child passed in by caller are ignored. If child is NULL, it considers all the
774 * children to find the first child which is compatible.
775 *
776 * @param parent The parent node under which to look.
777 * @param child The child node to start search from (exclusive). If NULL
778 * consider all children.
779 * @param compat The compatible string to find.
780 * @return The found node, or NULL.
781 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200782struct device_tree_node *
783dt_find_next_compat_child(struct device_tree_node *parent,
784 struct device_tree_node *child,
785 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200786{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200787 struct device_tree_node *next;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200788 int ignore = 0;
789
790 if (child)
791 ignore = 1;
792
793 list_for_each(next, parent->children, list_node) {
794 if (ignore) {
795 if (child == next)
796 ignore = 0;
797 continue;
798 }
799
800 if (dt_check_compat_match(next, compat))
801 return next;
802 }
803
804 return NULL;
805}
806
807/*
808 * Find a node with matching property value, in the subtree of a parent node.
809 *
810 * @param parent The parent node under which to look.
811 * @param name The property name to look for.
812 * @param data The property value to look for.
813 * @param size The property size.
814 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200815struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
816 const char *name, void *data,
817 size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200818{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200819 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200820
821 /* Check if parent itself has the required property value. */
822 list_for_each(prop, parent->properties, list_node) {
823 if (!strcmp(name, prop->prop.name)) {
824 size_t bytes = prop->prop.size;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200825 const void *prop_data = prop->prop.data;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200826 if (size != bytes)
827 break;
828 if (!memcmp(data, prop_data, size))
829 return parent;
830 break;
831 }
832 }
833
Patrick Rudolph666c1722018-04-03 09:57:33 +0200834 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200835 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200836 struct device_tree_node *found = dt_find_prop_value(child, name,
837 data, size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200838 if (found)
839 return found;
840 }
841 return NULL;
842}
843
844/*
845 * Write an arbitrary sized big-endian integer into a pointer.
846 *
847 * @param dest Pointer to the DT property data buffer to write.
Elyes HAOUAS1ec76442018-08-07 12:20:04 +0200848 * @param src The integer to write (in CPU endianness).
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200849 * @param length the length of the destination integer in bytes.
850 */
851void dt_write_int(u8 *dest, u64 src, size_t length)
852{
853 while (length--) {
854 dest[length] = (u8)src;
855 src >>= 8;
856 }
857}
858
859/*
Patrick Rudolph5ccc7312018-05-30 15:05:28 +0200860 * Delete a property by name in a given node if it exists.
861 *
862 * @param node The device tree node to operate on.
863 * @param name The name of the property to delete.
864 */
865void dt_delete_prop(struct device_tree_node *node, const char *name)
866{
867 struct device_tree_property *prop;
868
869 list_for_each(prop, node->properties, list_node) {
870 if (!strcmp(prop->prop.name, name)) {
871 list_remove(&prop->list_node);
872 return;
873 }
874 }
875}
876
877/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200878 * Add an arbitrary property to a node, or update it if it already exists.
879 *
880 * @param node The device tree node to add to.
881 * @param name The name of the new property.
882 * @param data The raw data blob to be stored in the property.
883 * @param size The size of data in bytes.
884 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200885void dt_add_bin_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200886 const void *data, size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200887{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200888 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200889
890 list_for_each(prop, node->properties, list_node) {
891 if (!strcmp(prop->prop.name, name)) {
892 prop->prop.data = data;
893 prop->prop.size = size;
894 return;
895 }
896 }
897
Julius Werner9636a102019-05-03 17:36:43 -0700898 prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200899 list_insert_after(&prop->list_node, &node->properties);
900 prop->prop.name = name;
901 prop->prop.data = data;
902 prop->prop.size = size;
903}
904
905/*
906 * Find given string property in a node and return its content.
907 *
908 * @param node The device tree node to search.
909 * @param name The name of the property.
910 * @return The found string, or NULL.
911 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200912const char *dt_find_string_prop(const struct device_tree_node *node,
913 const char *name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200914{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200915 const void *content;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200916 size_t size;
917
918 dt_find_bin_prop(node, name, &content, &size);
919
920 return content;
921}
922
923/*
924 * Find given property in a node.
925 *
926 * @param node The device tree node to search.
927 * @param name The name of the property.
928 * @param data Pointer to return raw data blob in the property.
929 * @param size Pointer to return the size of data in bytes.
930 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200931void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
932 const void **data, size_t *size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200933{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200934 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200935
936 *data = NULL;
937 *size = 0;
938
939 list_for_each(prop, node->properties, list_node) {
940 if (!strcmp(prop->prop.name, name)) {
941 *data = prop->prop.data;
942 *size = prop->prop.size;
943 return;
944 }
945 }
946}
947
948/*
949 * Add a string property to a node, or update it if it already exists.
950 *
951 * @param node The device tree node to add to.
952 * @param name The name of the new property.
953 * @param str The zero-terminated string to be stored in the property.
954 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200955void dt_add_string_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200956 const char *str)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200957{
958 dt_add_bin_prop(node, name, str, strlen(str) + 1);
959}
960
961/*
962 * Add a 32-bit integer property to a node, or update it if it already exists.
963 *
964 * @param node The device tree node to add to.
965 * @param name The name of the new property.
966 * @param val The integer to be stored in the property.
967 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200968void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200969{
Julius Werner9636a102019-05-03 17:36:43 -0700970 u32 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200971 *val_ptr = htobe32(val);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200972 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
973}
974
975/*
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200976 * Add a 64-bit integer property to a node, or update it if it already exists.
977 *
978 * @param node The device tree node to add to.
979 * @param name The name of the new property.
980 * @param val The integer to be stored in the property.
981 */
982void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
983{
Julius Werner9636a102019-05-03 17:36:43 -0700984 u64 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200985 *val_ptr = htobe64(val);
986 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
987}
988
989/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200990 * Add a 'reg' address list property to a node, or update it if it exists.
991 *
992 * @param node The device tree node to add to.
993 * @param addrs Array of address values to be stored in the property.
994 * @param sizes Array of corresponding size values to 'addrs'.
995 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
996 * @param addr_cells Value of #address-cells property valid for this node.
997 * @param size_cells Value of #size-cells property valid for this node.
998 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200999void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001000 int count, u32 addr_cells, u32 size_cells)
1001{
1002 int i;
1003 size_t length = (addr_cells + size_cells) * sizeof(u32) * count;
Julius Werner9636a102019-05-03 17:36:43 -07001004 u8 *data = xmalloc(length);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001005 u8 *cur = data;
1006
1007 for (i = 0; i < count; i++) {
1008 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
1009 cur += addr_cells * sizeof(u32);
1010 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
1011 cur += size_cells * sizeof(u32);
1012 }
1013
1014 dt_add_bin_prop(node, "reg", data, length);
1015}
1016
1017/*
1018 * Fixups to apply to a kernel's device tree before booting it.
1019 */
1020
Patrick Rudolph666c1722018-04-03 09:57:33 +02001021struct list_node device_tree_fixups;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001022
Patrick Rudolph666c1722018-04-03 09:57:33 +02001023int dt_apply_fixups(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001024{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001025 struct device_tree_fixup *fixup;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001026 list_for_each(fixup, device_tree_fixups, list_node) {
1027 assert(fixup->fixup);
1028 if (fixup->fixup(fixup, tree))
1029 return 1;
1030 }
1031 return 0;
1032}
1033
Patrick Rudolph666c1722018-04-03 09:57:33 +02001034int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001035 void *data, size_t data_size, int create)
1036{
1037 char *path_copy, *prop_name;
Patrick Rudolph666c1722018-04-03 09:57:33 +02001038 struct device_tree_node *dt_node;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001039
1040 path_copy = strdup(path);
1041
1042 if (!path_copy) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001043 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
1044 path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001045 return 1;
1046 }
1047
1048 prop_name = strrchr(path_copy, '/');
1049 if (!prop_name) {
Patrick Rudolph679d6242018-07-11 13:53:04 +02001050 free(path_copy);
Patrick Rudolph666c1722018-04-03 09:57:33 +02001051 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001052 return 1;
1053 }
1054
1055 *prop_name++ = '\0'; /* Separate path from the property name. */
1056
Julius Wernerf36d53c2019-05-03 18:23:34 -07001057 dt_node = dt_find_node_by_path(tree, path_copy, NULL,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001058 NULL, create);
1059
1060 if (!dt_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001061 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001062 create ? "create" : "find", path_copy);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001063 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001064 return 1;
1065 }
1066
1067 dt_add_bin_prop(dt_node, prop_name, data, data_size);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001068 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001069
1070 return 0;
1071}
1072
1073/*
1074 * Prepare the /reserved-memory/ node.
1075 *
1076 * Technically, this can be called more than one time, to init and/or retrieve
1077 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
1078 *
1079 * @tree: Device tree to add/retrieve from.
1080 * @return: The /reserved-memory/ node (or NULL, if error).
1081 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001082struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001083{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001084 struct device_tree_node *reserved;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001085 u32 addr = 0, size = 0;
1086
Julius Wernerfbec63d2019-05-03 18:29:28 -07001087 reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001088 &size, 1);
1089 if (!reserved)
1090 return NULL;
1091
1092 // Binding doc says this should have the same #{address,size}-cells as
1093 // the root.
1094 dt_add_u32_prop(reserved, "#address-cells", addr);
1095 dt_add_u32_prop(reserved, "#size-cells", size);
1096 // Binding doc says this should be empty (i.e., 1:1 mapping from root).
1097 dt_add_bin_prop(reserved, "ranges", NULL, 0);
1098
1099 return reserved;
1100}