| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <assert.h> |
| 5 |
#include <commonlib/device_tree.h> |
| 6 |
#include <ctype.h> |
| 7 |
#include <endian.h> |
| 8 |
#include <stdbool.h> |
| 9 |
#include <stdint.h> |
| 10 |
#ifdef __COREBOOT__ |
| 11 |
#include <console/console.h> |
| 12 |
#else |
| 13 |
#define printk(level, ...) printf(__VA_ARGS__) |
| 14 |
#endif |
| 15 |
#include <stdio.h> |
| 16 |
#include <string.h> |
| 17 |
#include <stddef.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <limits.h> |
| 20 |
|
| 21 |
#define FDT_PATH_MAX_DEPTH 10 // should be a good enough upper bound |
| 22 |
#define FDT_PATH_MAX_LEN 128 // should be a good enough upper bound |
| 23 |
#define FDT_MAX_MEMORY_NODES 4 // should be a good enough upper bound |
| 24 |
#define FDT_MAX_MEMORY_REGIONS 16 // should be a good enough upper bound |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#define LP_ALLOC_NODE_SCRATCH_COUNT 2000 |
| 35 |
#define LP_ALLOC_PROP_SCRATCH_COUNT 10000 |
| 36 |
|
| 37 |
static struct device_tree_node *alloc_node(void) |
| 38 |
{ |
| 39 |
#ifndef __COREBOOT__ |
| 40 |
static struct device_tree_node scratch[LP_ALLOC_NODE_SCRATCH_COUNT]; |
| 41 |
static int counter = 0; |
| 42 |
|
| 43 |
if (counter < ARRAY_SIZE(scratch)) |
| 44 |
return &scratch[counter++]; |
| 45 |
#endif |
| 46 |
return xzalloc(sizeof(struct device_tree_node)); |
| 47 |
} |
| 48 |
|
| 49 |
static struct device_tree_property *alloc_prop(void) |
| 50 |
{ |
| 51 |
#ifndef __COREBOOT__ |
| 52 |
static struct device_tree_property scratch[LP_ALLOC_PROP_SCRATCH_COUNT]; |
| 53 |
static int counter = 0; |
| 54 |
|
| 55 |
if (counter < ARRAY_SIZE(scratch)) |
| 56 |
return &scratch[counter++]; |
| 57 |
#endif |
| 58 |
return xzalloc(sizeof(struct device_tree_property)); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
static size_t read_reg_prop(struct fdt_property *prop, u32 addr_cells, u32 size_cells, |
| 68 |
struct device_tree_region regions[], size_t regions_count) |
| 69 |
{ |
| 70 |
|
| 71 |
size_t count = prop->size / (4 * addr_cells + 4 * size_cells); |
| 72 |
if (count > regions_count) { |
| 73 |
printk(BIOS_ERR, "reg property has more entries (%zd) than regions array can hold (%zd)\n", count, regions_count); |
| 74 |
count = regions_count; |
| 75 |
} |
| 76 |
if (addr_cells > 2 || size_cells > 2) { |
| 77 |
printk(BIOS_ERR, "addr_cells (%d) or size_cells (%d) bigger than 2\n", |
| 78 |
addr_cells, size_cells); |
| 79 |
return 0; |
| 80 |
} |
| 81 |
uint32_t *ptr = prop->data; |
| 82 |
for (int i = 0; i < count; i++) { |
| 83 |
if (addr_cells == 1) |
| 84 |
regions[i].addr = be32dec(ptr); |
| 85 |
else if (addr_cells == 2) |
| 86 |
regions[i].addr = be64dec(ptr); |
| 87 |
ptr += addr_cells; |
| 88 |
if (size_cells == 1) |
| 89 |
regions[i].size = be32dec(ptr); |
| 90 |
else if (size_cells == 2) |
| 91 |
regions[i].size = be64dec(ptr); |
| 92 |
ptr += size_cells; |
| 93 |
} |
| 94 |
|
| 95 |
return count; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
static int fdt_skip_nops(const void *blob, uint32_t offset) |
| 105 |
{ |
| 106 |
uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset); |
| 107 |
|
| 108 |
int index = 0; |
| 109 |
while (be32toh(ptr[index]) == FDT_TOKEN_NOP) |
| 110 |
index++; |
| 111 |
|
| 112 |
return index * sizeof(uint32_t); |
| 113 |
} |
| 114 |
|
| 115 |
int fdt_next_property(const void *blob, uint32_t offset, |
| 116 |
struct fdt_property *prop) |
| 117 |
{ |
| 118 |
struct fdt_header *header = (struct fdt_header *)blob; |
| 119 |
uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset); |
| 120 |
|
| 121 |
|
| 122 |
offset += fdt_skip_nops(blob, offset); |
| 123 |
|
| 124 |
int index = 0; |
| 125 |
if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY) |
| 126 |
return 0; |
| 127 |
|
| 128 |
uint32_t size = be32toh(ptr[index++]); |
| 129 |
uint32_t name_offset = be32toh(ptr[index++]); |
| 130 |
name_offset += be32toh(header->strings_offset); |
| 131 |
|
| 132 |
if (prop) { |
| 133 |
prop->name = (char *)((uint8_t *)blob + name_offset); |
| 134 |
prop->data = &ptr[index]; |
| 135 |
prop->size = size; |
| 136 |
} |
| 137 |
|
| 138 |
index += DIV_ROUND_UP(size, sizeof(uint32_t)); |
| 139 |
|
| 140 |
return index * sizeof(uint32_t); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
int fdt_next_node_name(const void *blob, uint32_t offset, const char **name) |
| 153 |
{ |
| 154 |
|
| 155 |
offset += fdt_skip_nops(blob, offset); |
| 156 |
|
| 157 |
char *ptr = ((char *)blob) + offset; |
| 158 |
if (be32dec(ptr) != FDT_TOKEN_BEGIN_NODE) |
| 159 |
return 0; |
| 160 |
|
| 161 |
ptr += 4; |
| 162 |
if (name) |
| 163 |
*name = ptr; |
| 164 |
|
| 165 |
return ALIGN_UP(strlen(ptr) + 1, 4) + 4; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
int fdt_skip_node(const void *blob, uint32_t start_offset) |
| 172 |
{ |
| 173 |
uint32_t offset = start_offset; |
| 174 |
|
| 175 |
const char *name; |
| 176 |
int size = fdt_next_node_name(blob, offset, &name); |
| 177 |
if (!size) |
| 178 |
return 0; |
| 179 |
offset += size; |
| 180 |
|
| 181 |
while ((size = fdt_next_property(blob, offset, NULL))) |
| 182 |
offset += size; |
| 183 |
|
| 184 |
while ((size = fdt_skip_node(blob, offset))) |
| 185 |
offset += size; |
| 186 |
|
| 187 |
|
| 188 |
offset += fdt_skip_nops(blob, offset); |
| 189 |
|
| 190 |
return offset - start_offset + sizeof(uint32_t); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
u32 fdt_read_prop(const void *blob, u32 node_offset, const char *prop_name, |
| 205 |
struct fdt_property *fdt_prop) |
| 206 |
{ |
| 207 |
u32 offset = node_offset; |
| 208 |
|
| 209 |
offset += fdt_next_node_name(blob, offset, NULL); |
| 210 |
|
| 211 |
size_t size; |
| 212 |
while ((size = fdt_next_property(blob, offset, fdt_prop))) { |
| 213 |
if (strcmp(fdt_prop->name, prop_name) == 0) |
| 214 |
return offset; |
| 215 |
offset += size; |
| 216 |
} |
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
u32 fdt_read_reg_prop(const void *blob, u32 node_offset, u32 addr_cells, u32 size_cells, |
| 234 |
struct device_tree_region regions[], size_t regions_count) |
| 235 |
{ |
| 236 |
struct fdt_property prop; |
| 237 |
u32 offset = fdt_read_prop(blob, node_offset, "reg", &prop); |
| 238 |
|
| 239 |
if (!offset) { |
| 240 |
printk(BIOS_DEBUG, "no reg property found in node_offset: %x\n", node_offset); |
| 241 |
return 0; |
| 242 |
} |
| 243 |
|
| 244 |
return read_reg_prop(&prop, addr_cells, size_cells, regions, regions_count); |
| 245 |
} |
| 246 |
|
| 247 |
static u32 fdt_read_cell_props(const void *blob, u32 node_offset, u32 *addrcp, u32 *sizecp) |
| 248 |
{ |
| 249 |
struct fdt_property prop; |
| 250 |
u32 offset = node_offset; |
| 251 |
size_t size; |
| 252 |
while ((size = fdt_next_property(blob, offset, &prop))) { |
| 253 |
if (addrcp && !strcmp(prop.name, "#address-cells")) |
| 254 |
*addrcp = be32dec(prop.data); |
| 255 |
if (sizecp && !strcmp(prop.name, "#size-cells")) |
| 256 |
*sizecp = be32dec(prop.data); |
| 257 |
offset += size; |
| 258 |
} |
| 259 |
return offset; |
| 260 |
} |
| 261 |
|
| 262 |
uint64_t fdt_read_int_prop(struct fdt_property *prop, u32 cells) |
| 263 |
{ |
| 264 |
if (cells == 0) |
| 265 |
cells = prop->size / 4; |
| 266 |
|
| 267 |
if (cells * 4 != prop->size) { |
| 268 |
printk(BIOS_ERR, "FDT integer property of size %u @%p doesn't match expected cell count %u\n", |
| 269 |
prop->size, prop->data, cells); |
| 270 |
return 0; |
| 271 |
} |
| 272 |
|
| 273 |
if (cells == 2) |
| 274 |
return be64dec(prop->data); |
| 275 |
else if (cells == 1) |
| 276 |
return be32dec(prop->data); |
| 277 |
|
| 278 |
printk(BIOS_ERR, "Illegal FDT integer property size %u @%p\n", prop->size, prop); |
| 279 |
return 0; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
static u32 fdt_find_node(const void *blob, u32 parent_node_offset, char **path, |
| 299 |
u32 *addrcp, u32 *sizecp) |
| 300 |
{ |
| 301 |
if (*path == NULL) |
| 302 |
return parent_node_offset; |
| 303 |
|
| 304 |
size_t size = fdt_next_node_name(blob, parent_node_offset, NULL); |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
u32 node_offset = fdt_read_cell_props(blob, parent_node_offset + size, addrcp, sizecp); |
| 312 |
|
| 313 |
const char *node_name; |
| 314 |
|
| 315 |
while ((size = fdt_next_node_name(blob, node_offset, &node_name))) { |
| 316 |
if (!strcmp(*path, node_name)) { |
| 317 |
|
| 318 |
return fdt_find_node(blob, node_offset, path + 1, addrcp, sizecp); |
| 319 |
} |
| 320 |
|
| 321 |
node_offset += fdt_skip_node(blob, node_offset); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
return 0; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
u32 fdt_find_node_by_path(const void *blob, const char *path, u32 *addrcp, u32 *sizecp) |
| 342 |
{ |
| 343 |
|
| 344 |
if (path[0] != '/') { |
| 345 |
printk(BIOS_ERR, "devicetree path must start with a /\n"); |
| 346 |
return 0; |
| 347 |
} |
| 348 |
if (!blob) { |
| 349 |
printk(BIOS_ERR, "devicetree blob is NULL\n"); |
| 350 |
return 0; |
| 351 |
} |
| 352 |
|
| 353 |
if (addrcp) |
| 354 |
*addrcp = 2; |
| 355 |
if (sizecp) |
| 356 |
*sizecp = 1; |
| 357 |
|
| 358 |
struct fdt_header *fdt_hdr = (struct fdt_header *)blob; |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
char *path_array[FDT_PATH_MAX_DEPTH]; |
| 365 |
size_t path_size = strlen(path); |
| 366 |
assert(path_size < FDT_PATH_MAX_LEN); |
| 367 |
char path_copy[FDT_PATH_MAX_LEN]; |
| 368 |
memcpy(path_copy, path, path_size + 1); |
| 369 |
char *cur = path_copy; |
| 370 |
int i; |
| 371 |
for (i = 0; i < FDT_PATH_MAX_DEPTH; i++) { |
| 372 |
path_array[i] = strtok_r(NULL, "/", &cur); |
| 373 |
if (!path_array[i]) |
| 374 |
break; |
| 375 |
} |
| 376 |
assert(i < FDT_PATH_MAX_DEPTH); |
| 377 |
|
| 378 |
return fdt_find_node(blob, be32toh(fdt_hdr->structure_offset), path_array, addrcp, sizecp); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
size_t fdt_find_subnodes_by_prefix(const void *blob, u32 node_offset, const char *prefix, |
| 405 |
u32 *addrcp, u32 *sizecp, u32 *results, size_t results_len) |
| 406 |
{ |
| 407 |
|
| 408 |
if (!blob || !results || !prefix) { |
| 409 |
printk(BIOS_ERR, "%s: input parameter cannot be null/\n", __func__); |
| 410 |
return 0; |
| 411 |
} |
| 412 |
|
| 413 |
u32 offset = node_offset; |
| 414 |
|
| 415 |
|
| 416 |
u32 size = fdt_next_node_name(blob, offset, NULL); |
| 417 |
if (!size) { |
| 418 |
printk(BIOS_ERR, "%s: node_offset: %x does not point to a node\n", |
| 419 |
__func__, node_offset); |
| 420 |
return 0; |
| 421 |
} |
| 422 |
offset += size; |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
offset = fdt_read_cell_props(blob, offset, addrcp, sizecp); |
| 429 |
|
| 430 |
size_t count_results = 0; |
| 431 |
int prefix_len = strlen(prefix); |
| 432 |
const char *node_name; |
| 433 |
|
| 434 |
while ((size = fdt_next_node_name(blob, offset, &node_name))) { |
| 435 |
|
| 436 |
|
| 437 |
if (count_results >= results_len) |
| 438 |
break; |
| 439 |
|
| 440 |
if (!strncmp(prefix, node_name, prefix_len)) { |
| 441 |
|
| 442 |
results[count_results++] = offset; |
| 443 |
} |
| 444 |
|
| 445 |
offset += fdt_skip_node(blob, offset); |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
return count_results; |
| 450 |
} |
| 451 |
|
| 452 |
static const char *fdt_read_alias_prop(const void *blob, const char *alias_name) |
| 453 |
{ |
| 454 |
u32 node_offset = fdt_find_node_by_path(blob, "/aliases", NULL, NULL); |
| 455 |
if (!node_offset) { |
| 456 |
printk(BIOS_DEBUG, "no /aliases node found\n"); |
| 457 |
return NULL; |
| 458 |
} |
| 459 |
struct fdt_property alias_prop; |
| 460 |
if (!fdt_read_prop(blob, node_offset, alias_name, &alias_prop)) { |
| 461 |
printk(BIOS_DEBUG, "property %s in /aliases node not found\n", alias_name); |
| 462 |
return NULL; |
| 463 |
} |
| 464 |
return (const char *)alias_prop.data; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
u32 fdt_find_node_by_alias(const void *blob, const char *alias_name, u32 *addrcp, u32 *sizecp) |
| 480 |
{ |
| 481 |
const char *node_name = fdt_read_alias_prop(blob, alias_name); |
| 482 |
if (!node_name) { |
| 483 |
printk(BIOS_DEBUG, "alias %s not found\n", alias_name); |
| 484 |
return 0; |
| 485 |
} |
| 486 |
|
| 487 |
u32 node_offset = fdt_find_node_by_path(blob, node_name, addrcp, sizecp); |
| 488 |
if (!node_offset) { |
| 489 |
|
| 490 |
printk(BIOS_WARNING, |
| 491 |
"Could not find node '%s', which alias was referring to '%s'\n", |
| 492 |
node_name, alias_name); |
| 493 |
return 0; |
| 494 |
} |
| 495 |
return node_offset; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
static void print_indent(int depth) |
| 504 |
{ |
| 505 |
printk(BIOS_DEBUG, "%*s", depth * 8, ""); |
| 506 |
} |
| 507 |
|
| 508 |
static void print_property(const struct fdt_property *prop, int depth) |
| 509 |
{ |
| 510 |
int is_string = prop->size > 0 && |
| 511 |
((char *)prop->data)[prop->size - 1] == '\0'; |
| 512 |
|
| 513 |
if (is_string) { |
| 514 |
for (int i = 0; i < prop->size - 1; i++) { |
| 515 |
if (!isprint(((char *)prop->data)[i])) { |
| 516 |
is_string = 0; |
| 517 |
break; |
| 518 |
} |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
print_indent(depth); |
| 523 |
if (is_string) { |
| 524 |
printk(BIOS_DEBUG, "%s = \"%s\";\n", |
| 525 |
prop->name, (const char *)prop->data); |
| 526 |
} else { |
| 527 |
printk(BIOS_DEBUG, "%s = < ", prop->name); |
| 528 |
for (int i = 0; i < MIN(128, prop->size); i += 4) { |
| 529 |
uint32_t val = 0; |
| 530 |
for (int j = 0; j < MIN(4, prop->size - i); j++) |
| 531 |
val |= ((uint8_t *)prop->data)[i + j] << |
| 532 |
(24 - j * 8); |
| 533 |
printk(BIOS_DEBUG, "%#.2x ", val); |
| 534 |
} |
| 535 |
if (prop->size > 128) |
| 536 |
printk(BIOS_DEBUG, "..."); |
| 537 |
printk(BIOS_DEBUG, ">;\n"); |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
static int print_flat_node(const void *blob, uint32_t start_offset, int depth) |
| 542 |
{ |
| 543 |
int offset = start_offset; |
| 544 |
const char *name; |
| 545 |
int size; |
| 546 |
|
| 547 |
size = fdt_next_node_name(blob, offset, &name); |
| 548 |
if (!size) |
| 549 |
return 0; |
| 550 |
offset += size; |
| 551 |
|
| 552 |
print_indent(depth); |
| 553 |
printk(BIOS_DEBUG, "%s {\n", name); |
| 554 |
|
| 555 |
struct fdt_property prop; |
| 556 |
while ((size = fdt_next_property(blob, offset, &prop))) { |
| 557 |
print_property(&prop, depth + 1); |
| 558 |
|
| 559 |
offset += size; |
| 560 |
} |
| 561 |
|
| 562 |
printk(BIOS_DEBUG, "\n"); |
| 563 |
|
| 564 |
while ((size = print_flat_node(blob, offset, depth + 1))) |
| 565 |
offset += size; |
| 566 |
|
| 567 |
print_indent(depth); |
| 568 |
printk(BIOS_DEBUG, "}\n"); |
| 569 |
|
| 570 |
return offset - start_offset + sizeof(uint32_t); |
| 571 |
} |
| 572 |
|
| 573 |
void fdt_print_node(const void *blob, uint32_t offset) |
| 574 |
{ |
| 575 |
print_flat_node(blob, offset, 0); |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
size_t fdt_read_memory_regions(const void *blob, |
| 590 |
struct device_tree_region regions[], |
| 591 |
size_t regions_count) |
| 592 |
{ |
| 593 |
u32 node, root, addrcp, sizecp; |
| 594 |
u32 nodes[FDT_MAX_MEMORY_NODES] = {0}; |
| 595 |
size_t region_idx = 0; |
| 596 |
size_t node_count = 0; |
| 597 |
|
| 598 |
if (!fdt_is_valid(blob)) |
| 599 |
return 0; |
| 600 |
|
| 601 |
node = fdt_find_node_by_path(blob, "/memory", &addrcp, &sizecp); |
| 602 |
if (node) { |
| 603 |
region_idx += fdt_read_reg_prop(blob, node, addrcp, sizecp, |
| 604 |
regions, regions_count); |
| 605 |
if (region_idx >= regions_count) { |
| 606 |
printk(BIOS_WARNING, "FDT: Too many memory regions\n"); |
| 607 |
goto out; |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
root = fdt_find_node_by_path(blob, "/", &addrcp, &sizecp); |
| 612 |
node_count = fdt_find_subnodes_by_prefix(blob, root, "memory@", |
| 613 |
&addrcp, &sizecp, nodes, |
| 614 |
FDT_MAX_MEMORY_NODES); |
| 615 |
if (node_count >= FDT_MAX_MEMORY_NODES) { |
| 616 |
printk(BIOS_WARNING, "FDT: Too many memory nodes\n"); |
| 617 |
|
| 618 |
} |
| 619 |
|
| 620 |
for (size_t i = 0; i < MIN(node_count, FDT_MAX_MEMORY_NODES); i++) { |
| 621 |
region_idx += fdt_read_reg_prop(blob, nodes[i], addrcp, sizecp, |
| 622 |
®ions[region_idx], |
| 623 |
regions_count - region_idx); |
| 624 |
if (region_idx >= regions_count) { |
| 625 |
printk(BIOS_WARNING, "FDT: Too many memory regions\n"); |
| 626 |
goto out; |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
out: |
| 631 |
for (size_t i = 0; i < MIN(region_idx, regions_count); i++) { |
| 632 |
printk(BIOS_DEBUG, "FDT: Memory region [%#llx - %#llx]\n", |
| 633 |
regions[i].addr, regions[i].addr + regions[i].size); |
| 634 |
} |
| 635 |
|
| 636 |
return region_idx; |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
uint64_t fdt_get_memory_top(const void *blob) |
| 647 |
{ |
| 648 |
struct device_tree_region regions[FDT_MAX_MEMORY_REGIONS] = {0}; |
| 649 |
uint64_t top = 0; |
| 650 |
uint64_t total = 0; |
| 651 |
size_t count; |
| 652 |
|
| 653 |
if (!fdt_is_valid(blob)) |
| 654 |
return 0; |
| 655 |
|
| 656 |
count = fdt_read_memory_regions(blob, regions, FDT_MAX_MEMORY_REGIONS); |
| 657 |
for (size_t i = 0; i < MIN(count, FDT_MAX_MEMORY_REGIONS); i++) { |
| 658 |
top = MAX(top, regions[i].addr + regions[i].size); |
| 659 |
total += regions[i].size; |
| 660 |
} |
| 661 |
|
| 662 |
printk(BIOS_DEBUG, "FDT: Found %u MiB of RAM\n", |
| 663 |
(uint32_t)(total / MiB)); |
| 664 |
|
| 665 |
return top; |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
static int dt_prop_is_phandle(struct device_tree_property *prop) |
| 673 |
{ |
| 674 |
return !(strcmp("phandle", prop->prop.name) && |
| 675 |
strcmp("linux,phandle", prop->prop.name)); |
| 676 |
} |
| 677 |
|
| 678 |
static int fdt_unflatten_node(const void *blob, uint32_t start_offset, |
| 679 |
struct device_tree *tree, |
| 680 |
struct device_tree_node **new_node) |
| 681 |
{ |
| 682 |
struct list_node *last; |
| 683 |
int offset = start_offset; |
| 684 |
const char *name; |
| 685 |
int size; |
| 686 |
|
| 687 |
size = fdt_next_node_name(blob, offset, &name); |
| 688 |
if (!size) |
| 689 |
return 0; |
| 690 |
offset += size; |
| 691 |
|
| 692 |
struct device_tree_node *node = alloc_node(); |
| 693 |
*new_node = node; |
| 694 |
node->name = name; |
| 695 |
|
| 696 |
struct fdt_property fprop; |
| 697 |
last = &node->properties; |
| 698 |
while ((size = fdt_next_property(blob, offset, &fprop))) { |
| 699 |
struct device_tree_property *prop = alloc_prop(); |
| 700 |
prop->prop = fprop; |
| 701 |
|
| 702 |
if (dt_prop_is_phandle(prop)) { |
| 703 |
node->phandle = be32dec(prop->prop.data); |
| 704 |
if (node->phandle > tree->max_phandle) |
| 705 |
tree->max_phandle = node->phandle; |
| 706 |
} |
| 707 |
|
| 708 |
list_insert_after(&prop->list_node, last); |
| 709 |
last = &prop->list_node; |
| 710 |
|
| 711 |
offset += size; |
| 712 |
} |
| 713 |
|
| 714 |
struct device_tree_node *child; |
| 715 |
last = &node->children; |
| 716 |
while ((size = fdt_unflatten_node(blob, offset, tree, &child))) { |
| 717 |
list_insert_after(&child->list_node, last); |
| 718 |
last = &child->list_node; |
| 719 |
|
| 720 |
offset += size; |
| 721 |
} |
| 722 |
|
| 723 |
return offset - start_offset + sizeof(uint32_t); |
| 724 |
} |
| 725 |
|
| 726 |
static int fdt_unflatten_map_entry(const void *blob, uint32_t offset, |
| 727 |
struct device_tree_reserve_map_entry **new) |
| 728 |
{ |
| 729 |
const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset); |
| 730 |
const uint64_t start = be64toh(ptr[0]); |
| 731 |
const uint64_t size = be64toh(ptr[1]); |
| 732 |
|
| 733 |
if (!size) |
| 734 |
return 0; |
| 735 |
|
| 736 |
struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry)); |
| 737 |
*new = entry; |
| 738 |
entry->start = start; |
| 739 |
entry->size = size; |
| 740 |
|
| 741 |
return sizeof(uint64_t) * 2; |
| 742 |
} |
| 743 |
|
| 744 |
bool fdt_is_valid(const void *blob) |
| 745 |
{ |
| 746 |
const struct fdt_header *header = (const struct fdt_header *)blob; |
| 747 |
|
| 748 |
uint32_t magic = be32toh(header->magic); |
| 749 |
uint32_t version = be32toh(header->version); |
| 750 |
uint32_t last_comp_version = be32toh(header->last_comp_version); |
| 751 |
|
| 752 |
if (magic != FDT_HEADER_MAGIC) { |
| 753 |
printk(BIOS_ERR, "Invalid device tree magic %#.8x!\n", magic); |
| 754 |
return false; |
| 755 |
} |
| 756 |
if (last_comp_version > FDT_SUPPORTED_VERSION) { |
| 757 |
printk(BIOS_ERR, "Unsupported device tree version %u(>=%u)\n", |
| 758 |
version, last_comp_version); |
| 759 |
return false; |
| 760 |
} |
| 761 |
if (version > FDT_SUPPORTED_VERSION) |
| 762 |
printk(BIOS_NOTICE, "FDT version %u too new, should add support!\n", |
| 763 |
version); |
| 764 |
return true; |
| 765 |
} |
| 766 |
|
| 767 |
struct device_tree *fdt_unflatten(const void *blob) |
| 768 |
{ |
| 769 |
if (!fdt_is_valid(blob)) |
| 770 |
return NULL; |
| 771 |
|
| 772 |
const struct fdt_header *header = blob; |
| 773 |
struct device_tree *tree = xzalloc(sizeof(*tree)); |
| 774 |
tree->header = header; |
| 775 |
|
| 776 |
uint32_t struct_offset = be32toh(header->structure_offset); |
| 777 |
uint32_t strings_offset = be32toh(header->strings_offset); |
| 778 |
uint32_t reserve_offset = be32toh(header->reserve_map_offset); |
| 779 |
uint32_t min_offset = 0; |
| 780 |
min_offset = MIN(struct_offset, strings_offset); |
| 781 |
min_offset = MIN(min_offset, reserve_offset); |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
tree->header_size = min_offset; |
| 786 |
|
| 787 |
struct device_tree_reserve_map_entry *entry; |
| 788 |
uint32_t offset = reserve_offset; |
| 789 |
int size; |
| 790 |
struct list_node *last = &tree->reserve_map; |
| 791 |
while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) { |
| 792 |
list_insert_after(&entry->list_node, last); |
| 793 |
last = &entry->list_node; |
| 794 |
|
| 795 |
offset += size; |
| 796 |
} |
| 797 |
|
| 798 |
fdt_unflatten_node(blob, struct_offset, tree, &tree->root); |
| 799 |
|
| 800 |
return tree; |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
static void dt_flat_prop_size(struct device_tree_property *prop, |
| 810 |
uint32_t *struct_size, uint32_t *strings_size) |
| 811 |
{ |
| 812 |
|
| 813 |
*struct_size += sizeof(uint32_t); |
| 814 |
|
| 815 |
*struct_size += sizeof(uint32_t); |
| 816 |
|
| 817 |
*struct_size += sizeof(uint32_t); |
| 818 |
|
| 819 |
*struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t)); |
| 820 |
|
| 821 |
|
| 822 |
*strings_size += strlen(prop->prop.name) + 1; |
| 823 |
} |
| 824 |
|
| 825 |
static void dt_flat_node_size(struct device_tree_node *node, |
| 826 |
uint32_t *struct_size, uint32_t *strings_size) |
| 827 |
{ |
| 828 |
|
| 829 |
*struct_size += sizeof(uint32_t); |
| 830 |
|
| 831 |
*struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t)); |
| 832 |
|
| 833 |
struct device_tree_property *prop; |
| 834 |
list_for_each(prop, node->properties, list_node) |
| 835 |
dt_flat_prop_size(prop, struct_size, strings_size); |
| 836 |
|
| 837 |
struct device_tree_node *child; |
| 838 |
list_for_each(child, node->children, list_node) |
| 839 |
dt_flat_node_size(child, struct_size, strings_size); |
| 840 |
|
| 841 |
|
| 842 |
*struct_size += sizeof(uint32_t); |
| 843 |
} |
| 844 |
|
| 845 |
uint32_t dt_flat_size(const struct device_tree *tree) |
| 846 |
{ |
| 847 |
uint32_t size = tree->header_size; |
| 848 |
struct device_tree_reserve_map_entry *entry; |
| 849 |
list_for_each(entry, tree->reserve_map, list_node) |
| 850 |
size += sizeof(uint64_t) * 2; |
| 851 |
size += sizeof(uint64_t) * 2; |
| 852 |
|
| 853 |
uint32_t struct_size = 0; |
| 854 |
uint32_t strings_size = 0; |
| 855 |
dt_flat_node_size(tree->root, &struct_size, &strings_size); |
| 856 |
|
| 857 |
size += struct_size; |
| 858 |
|
| 859 |
size += sizeof(uint32_t); |
| 860 |
|
| 861 |
size += strings_size; |
| 862 |
|
| 863 |
return size; |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry, |
| 873 |
void **map_start) |
| 874 |
{ |
| 875 |
((uint64_t *)*map_start)[0] = htobe64(entry->start); |
| 876 |
((uint64_t *)*map_start)[1] = htobe64(entry->size); |
| 877 |
*map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2; |
| 878 |
} |
| 879 |
|
| 880 |
static void dt_flatten_prop(struct device_tree_property *prop, |
| 881 |
void **struct_start, void *strings_base, |
| 882 |
void **strings_start) |
| 883 |
{ |
| 884 |
uint8_t *dstruct = (uint8_t *)*struct_start; |
| 885 |
uint8_t *dstrings = (uint8_t *)*strings_start; |
| 886 |
|
| 887 |
be32enc(dstruct, FDT_TOKEN_PROPERTY); |
| 888 |
dstruct += sizeof(uint32_t); |
| 889 |
|
| 890 |
be32enc(dstruct, prop->prop.size); |
| 891 |
dstruct += sizeof(uint32_t); |
| 892 |
|
| 893 |
uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base; |
| 894 |
be32enc(dstruct, name_offset); |
| 895 |
dstruct += sizeof(uint32_t); |
| 896 |
|
| 897 |
strcpy((char *)dstrings, prop->prop.name); |
| 898 |
dstrings += strlen(prop->prop.name) + 1; |
| 899 |
|
| 900 |
memcpy(dstruct, prop->prop.data, prop->prop.size); |
| 901 |
dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t)); |
| 902 |
|
| 903 |
*struct_start = dstruct; |
| 904 |
*strings_start = dstrings; |
| 905 |
} |
| 906 |
|
| 907 |
static void dt_flatten_node(const struct device_tree_node *node, |
| 908 |
void **struct_start, void *strings_base, |
| 909 |
void **strings_start) |
| 910 |
{ |
| 911 |
uint8_t *dstruct = (uint8_t *)*struct_start; |
| 912 |
uint8_t *dstrings = (uint8_t *)*strings_start; |
| 913 |
|
| 914 |
be32enc(dstruct, FDT_TOKEN_BEGIN_NODE); |
| 915 |
dstruct += sizeof(uint32_t); |
| 916 |
|
| 917 |
strcpy((char *)dstruct, node->name); |
| 918 |
dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t)); |
| 919 |
|
| 920 |
struct device_tree_property *prop; |
| 921 |
list_for_each(prop, node->properties, list_node) |
| 922 |
dt_flatten_prop(prop, (void **)&dstruct, strings_base, |
| 923 |
(void **)&dstrings); |
| 924 |
|
| 925 |
struct device_tree_node *child; |
| 926 |
list_for_each(child, node->children, list_node) |
| 927 |
dt_flatten_node(child, (void **)&dstruct, strings_base, |
| 928 |
(void **)&dstrings); |
| 929 |
|
| 930 |
be32enc(dstruct, FDT_TOKEN_END_NODE); |
| 931 |
dstruct += sizeof(uint32_t); |
| 932 |
|
| 933 |
*struct_start = dstruct; |
| 934 |
*strings_start = dstrings; |
| 935 |
} |
| 936 |
|
| 937 |
void dt_flatten(const struct device_tree *tree, void *start_dest) |
| 938 |
{ |
| 939 |
uint8_t *dest = (uint8_t *)start_dest; |
| 940 |
|
| 941 |
memcpy(dest, tree->header, tree->header_size); |
| 942 |
struct fdt_header *header = (struct fdt_header *)dest; |
| 943 |
dest += tree->header_size; |
| 944 |
|
| 945 |
struct device_tree_reserve_map_entry *entry; |
| 946 |
list_for_each(entry, tree->reserve_map, list_node) |
| 947 |
dt_flatten_map_entry(entry, (void **)&dest); |
| 948 |
((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0; |
| 949 |
dest += sizeof(uint64_t) * 2; |
| 950 |
|
| 951 |
uint32_t struct_size = 0; |
| 952 |
uint32_t strings_size = 0; |
| 953 |
dt_flat_node_size(tree->root, &struct_size, &strings_size); |
| 954 |
|
| 955 |
uint8_t *struct_start = dest; |
| 956 |
header->structure_offset = htobe32(dest - (uint8_t *)start_dest); |
| 957 |
be32enc(&dest[struct_size], FDT_TOKEN_END); |
| 958 |
struct_size += sizeof(uint32_t); |
| 959 |
header->structure_size = htobe32(struct_size); |
| 960 |
dest += struct_size; |
| 961 |
|
| 962 |
uint8_t *strings_start = dest; |
| 963 |
header->strings_offset = htobe32(dest - (uint8_t *)start_dest); |
| 964 |
header->strings_size = htobe32(strings_size); |
| 965 |
dest += strings_size; |
| 966 |
|
| 967 |
dt_flatten_node(tree->root, (void **)&struct_start, strings_start, |
| 968 |
(void **)&strings_start); |
| 969 |
|
| 970 |
header->totalsize = htobe32(dest - (uint8_t *)start_dest); |
| 971 |
} |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
static void print_node(const struct device_tree_node *node, int depth) |
| 980 |
{ |
| 981 |
print_indent(depth); |
| 982 |
if (depth == 0) |
| 983 |
printk(BIOS_DEBUG, "/"); |
| 984 |
printk(BIOS_DEBUG, "%s {\n", node->name); |
| 985 |
|
| 986 |
struct device_tree_property *prop; |
| 987 |
list_for_each(prop, node->properties, list_node) |
| 988 |
print_property(&prop->prop, depth + 1); |
| 989 |
|
| 990 |
printk(BIOS_DEBUG, "\n"); |
| 991 |
|
| 992 |
struct device_tree_node *child; |
| 993 |
list_for_each(child, node->children, list_node) |
| 994 |
print_node(child, depth + 1); |
| 995 |
|
| 996 |
print_indent(depth); |
| 997 |
printk(BIOS_DEBUG, "};\n"); |
| 998 |
} |
| 999 |
|
| 1000 |
void dt_print_node(const struct device_tree_node *node) |
| 1001 |
{ |
| 1002 |
print_node(node, 0); |
| 1003 |
} |
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
size_t dt_read_reg_prop(struct device_tree_node *node, u32 addr_cells, u32 size_cells, |
| 1024 |
struct device_tree_region regions[], size_t regions_count) |
| 1025 |
{ |
| 1026 |
struct device_tree_property *prop; |
| 1027 |
bool found = false; |
| 1028 |
list_for_each(prop, node->properties, list_node) { |
| 1029 |
if (!strcmp("reg", prop->prop.name)) { |
| 1030 |
found = true; |
| 1031 |
break; |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
if (!found) { |
| 1036 |
printk(BIOS_DEBUG, "no reg property found\n"); |
| 1037 |
return 0; |
| 1038 |
} |
| 1039 |
return read_reg_prop(&prop->prop, addr_cells, size_cells, regions, regions_count); |
| 1040 |
} |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp, |
| 1050 |
u32 *sizecp) |
| 1051 |
{ |
| 1052 |
struct device_tree_property *prop; |
| 1053 |
list_for_each(prop, node->properties, list_node) { |
| 1054 |
if (addrcp && !strcmp("#address-cells", prop->prop.name)) |
| 1055 |
*addrcp = be32dec(prop->prop.data); |
| 1056 |
if (sizecp && !strcmp("#size-cells", prop->prop.name)) |
| 1057 |
*sizecp = be32dec(prop->prop.data); |
| 1058 |
} |
| 1059 |
} |
| 1060 |
|
| 1061 |
static struct device_tree_node *_dt_find_node(struct device_tree_node *parent, |
| 1062 |
const char *const *path, u32 *addrcp, |
| 1063 |
u32 *sizecp, int create) |
| 1064 |
{ |
| 1065 |
struct device_tree_node *node, *found = NULL; |
| 1066 |
|
| 1067 |
if (!*path) |
| 1068 |
return parent; |
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
if (addrcp || sizecp) |
| 1073 |
dt_read_cell_props(parent, addrcp, sizecp); |
| 1074 |
|
| 1075 |
|
| 1076 |
list_for_each(node, parent->children, list_node) { |
| 1077 |
if (!strcmp(node->name, *path)) { |
| 1078 |
found = node; |
| 1079 |
break; |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
|
| 1084 |
if (!found) { |
| 1085 |
if (!create) |
| 1086 |
return NULL; |
| 1087 |
|
| 1088 |
found = alloc_node(); |
| 1089 |
found->name = strdup(*path); |
| 1090 |
if (!found->name) |
| 1091 |
return NULL; |
| 1092 |
|
| 1093 |
list_insert_after(&found->list_node, &parent->children); |
| 1094 |
} |
| 1095 |
|
| 1096 |
return _dt_find_node(found, path + 1, addrcp, sizecp, create); |
| 1097 |
} |
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
struct device_tree_node *dt_find_node(struct device_tree_node *parent, |
| 1114 |
const char *const *path, u32 *addrcp, |
| 1115 |
u32 *sizecp, int create) |
| 1116 |
{ |
| 1117 |
|
| 1118 |
if (addrcp) |
| 1119 |
*addrcp = 2; |
| 1120 |
|
| 1121 |
if (sizecp) |
| 1122 |
*sizecp = 1; |
| 1123 |
|
| 1124 |
return _dt_find_node(parent, path, addrcp, sizecp, create); |
| 1125 |
} |
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
struct device_tree_node *dt_find_node_by_path(struct device_tree *tree, |
| 1144 |
const char *path, u32 *addrcp, |
| 1145 |
u32 *sizecp, int create) |
| 1146 |
{ |
| 1147 |
char *sub_path; |
| 1148 |
char *duped_str; |
| 1149 |
struct device_tree_node *parent; |
| 1150 |
char *next_slash; |
| 1151 |
|
| 1152 |
const char *path_array[15]; |
| 1153 |
int i; |
| 1154 |
struct device_tree_node *node = NULL; |
| 1155 |
|
| 1156 |
if (path[0] == '/') { |
| 1157 |
if (path[1] == '\0') { |
| 1158 |
if (addrcp || sizecp) |
| 1159 |
dt_read_cell_props(tree->root, addrcp, sizecp); |
| 1160 |
return tree->root; |
| 1161 |
} |
| 1162 |
|
| 1163 |
sub_path = duped_str = strdup(&path[1]); |
| 1164 |
if (!sub_path) |
| 1165 |
return NULL; |
| 1166 |
|
| 1167 |
parent = tree->root; |
| 1168 |
} else { |
| 1169 |
char *alias; |
| 1170 |
|
| 1171 |
alias = duped_str = strdup(path); |
| 1172 |
if (!alias) |
| 1173 |
return NULL; |
| 1174 |
|
| 1175 |
sub_path = strchr(alias, '/'); |
| 1176 |
if (sub_path) |
| 1177 |
*sub_path = '\0'; |
| 1178 |
|
| 1179 |
parent = dt_find_node_by_alias(tree, alias); |
| 1180 |
if (!parent) { |
| 1181 |
printk(BIOS_DEBUG, |
| 1182 |
"Could not find node '%s', alias '%s' does not exist\n", |
| 1183 |
path, alias); |
| 1184 |
free(duped_str); |
| 1185 |
return NULL; |
| 1186 |
} |
| 1187 |
|
| 1188 |
if (!sub_path) { |
| 1189 |
|
| 1190 |
free(duped_str); |
| 1191 |
return parent; |
| 1192 |
} |
| 1193 |
|
| 1194 |
sub_path++; |
| 1195 |
} |
| 1196 |
|
| 1197 |
next_slash = sub_path; |
| 1198 |
path_array[0] = sub_path; |
| 1199 |
for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) { |
| 1200 |
next_slash = strchr(next_slash, '/'); |
| 1201 |
if (!next_slash) |
| 1202 |
break; |
| 1203 |
|
| 1204 |
*next_slash++ = '\0'; |
| 1205 |
path_array[i] = next_slash; |
| 1206 |
} |
| 1207 |
|
| 1208 |
if (!next_slash) { |
| 1209 |
path_array[i] = NULL; |
| 1210 |
node = dt_find_node(parent, path_array, |
| 1211 |
addrcp, sizecp, create); |
| 1212 |
} |
| 1213 |
|
| 1214 |
free(duped_str); |
| 1215 |
return node; |
| 1216 |
} |
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
struct device_tree_node *dt_find_node_by_alias(struct device_tree *tree, |
| 1226 |
const char *alias) |
| 1227 |
{ |
| 1228 |
struct device_tree_node *node; |
| 1229 |
const char *alias_path; |
| 1230 |
|
| 1231 |
node = dt_find_node_by_path(tree, "/aliases", NULL, NULL, 0); |
| 1232 |
if (!node) |
| 1233 |
return NULL; |
| 1234 |
|
| 1235 |
alias_path = dt_find_string_prop(node, alias); |
| 1236 |
if (!alias_path) |
| 1237 |
return NULL; |
| 1238 |
|
| 1239 |
return dt_find_node_by_path(tree, alias_path, NULL, NULL, 0); |
| 1240 |
} |
| 1241 |
|
| 1242 |
struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root, |
| 1243 |
uint32_t phandle) |
| 1244 |
{ |
| 1245 |
if (!root) |
| 1246 |
return NULL; |
| 1247 |
|
| 1248 |
if (root->phandle == phandle) |
| 1249 |
return root; |
| 1250 |
|
| 1251 |
struct device_tree_node *node; |
| 1252 |
struct device_tree_node *result; |
| 1253 |
list_for_each(node, root->children, list_node) { |
| 1254 |
result = dt_find_node_by_phandle(node, phandle); |
| 1255 |
if (result) |
| 1256 |
return result; |
| 1257 |
} |
| 1258 |
|
| 1259 |
return NULL; |
| 1260 |
} |
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
static int dt_check_compat_match(struct device_tree_node *node, |
| 1270 |
const char *compat) |
| 1271 |
{ |
| 1272 |
struct device_tree_property *prop; |
| 1273 |
|
| 1274 |
list_for_each(prop, node->properties, list_node) { |
| 1275 |
if (!strcmp("compatible", prop->prop.name)) { |
| 1276 |
size_t bytes = prop->prop.size; |
| 1277 |
const char *str = prop->prop.data; |
| 1278 |
while (bytes > 0) { |
| 1279 |
if (!strncmp(compat, str, bytes)) |
| 1280 |
return 1; |
| 1281 |
size_t len = strnlen(str, bytes) + 1; |
| 1282 |
if (bytes <= len) |
| 1283 |
break; |
| 1284 |
str += len; |
| 1285 |
bytes -= len; |
| 1286 |
} |
| 1287 |
break; |
| 1288 |
} |
| 1289 |
} |
| 1290 |
|
| 1291 |
return 0; |
| 1292 |
} |
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
struct device_tree_node *dt_find_compat(struct device_tree_node *parent, |
| 1302 |
const char *compat) |
| 1303 |
{ |
| 1304 |
|
| 1305 |
if (dt_check_compat_match(parent, compat)) |
| 1306 |
return parent; |
| 1307 |
|
| 1308 |
struct device_tree_node *child; |
| 1309 |
list_for_each(child, parent->children, list_node) { |
| 1310 |
struct device_tree_node *found = dt_find_compat(child, compat); |
| 1311 |
if (found) |
| 1312 |
return found; |
| 1313 |
} |
| 1314 |
|
| 1315 |
return NULL; |
| 1316 |
} |
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
struct device_tree_node * |
| 1330 |
dt_find_next_compat_child(struct device_tree_node *parent, |
| 1331 |
struct device_tree_node *child, |
| 1332 |
const char *compat) |
| 1333 |
{ |
| 1334 |
struct device_tree_node *next; |
| 1335 |
int ignore = 0; |
| 1336 |
|
| 1337 |
if (child) |
| 1338 |
ignore = 1; |
| 1339 |
|
| 1340 |
list_for_each(next, parent->children, list_node) { |
| 1341 |
if (ignore) { |
| 1342 |
if (child == next) |
| 1343 |
ignore = 0; |
| 1344 |
continue; |
| 1345 |
} |
| 1346 |
|
| 1347 |
if (dt_check_compat_match(next, compat)) |
| 1348 |
return next; |
| 1349 |
} |
| 1350 |
|
| 1351 |
return NULL; |
| 1352 |
} |
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent, |
| 1363 |
const char *name, void *data, |
| 1364 |
size_t size) |
| 1365 |
{ |
| 1366 |
struct device_tree_property *prop; |
| 1367 |
|
| 1368 |
|
| 1369 |
list_for_each(prop, parent->properties, list_node) { |
| 1370 |
if (!strcmp(name, prop->prop.name)) { |
| 1371 |
size_t bytes = prop->prop.size; |
| 1372 |
const void *prop_data = prop->prop.data; |
| 1373 |
if (size != bytes) |
| 1374 |
break; |
| 1375 |
if (!memcmp(data, prop_data, size)) |
| 1376 |
return parent; |
| 1377 |
break; |
| 1378 |
} |
| 1379 |
} |
| 1380 |
|
| 1381 |
struct device_tree_node *child; |
| 1382 |
list_for_each(child, parent->children, list_node) { |
| 1383 |
struct device_tree_node *found = dt_find_prop_value(child, name, |
| 1384 |
data, size); |
| 1385 |
if (found) |
| 1386 |
return found; |
| 1387 |
} |
| 1388 |
return NULL; |
| 1389 |
} |
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
void dt_write_int(u8 *dest, u64 src, size_t length) |
| 1399 |
{ |
| 1400 |
while (length--) { |
| 1401 |
dest[length] = (u8)src; |
| 1402 |
src >>= 8; |
| 1403 |
} |
| 1404 |
} |
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
void dt_delete_prop(struct device_tree_node *node, const char *name) |
| 1413 |
{ |
| 1414 |
struct device_tree_property *prop; |
| 1415 |
|
| 1416 |
list_for_each(prop, node->properties, list_node) { |
| 1417 |
if (!strcmp(prop->prop.name, name)) { |
| 1418 |
list_remove(&prop->list_node); |
| 1419 |
return; |
| 1420 |
} |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
void dt_add_bin_prop(struct device_tree_node *node, const char *name, |
| 1433 |
void *data, size_t size) |
| 1434 |
{ |
| 1435 |
struct device_tree_property *prop; |
| 1436 |
|
| 1437 |
list_for_each(prop, node->properties, list_node) { |
| 1438 |
if (!strcmp(prop->prop.name, name)) { |
| 1439 |
prop->prop.data = data; |
| 1440 |
prop->prop.size = size; |
| 1441 |
return; |
| 1442 |
} |
| 1443 |
} |
| 1444 |
|
| 1445 |
prop = alloc_prop(); |
| 1446 |
list_insert_after(&prop->list_node, &node->properties); |
| 1447 |
prop->prop.name = name; |
| 1448 |
prop->prop.data = data; |
| 1449 |
prop->prop.size = size; |
| 1450 |
} |
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
const char *dt_find_string_prop(const struct device_tree_node *node, |
| 1460 |
const char *name) |
| 1461 |
{ |
| 1462 |
const void *content; |
| 1463 |
size_t size; |
| 1464 |
|
| 1465 |
dt_find_bin_prop(node, name, &content, &size); |
| 1466 |
|
| 1467 |
return content; |
| 1468 |
} |
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
void dt_find_bin_prop(const struct device_tree_node *node, const char *name, |
| 1479 |
const void **data, size_t *size) |
| 1480 |
{ |
| 1481 |
struct device_tree_property *prop; |
| 1482 |
|
| 1483 |
*data = NULL; |
| 1484 |
*size = 0; |
| 1485 |
|
| 1486 |
list_for_each(prop, node->properties, list_node) { |
| 1487 |
if (!strcmp(prop->prop.name, name)) { |
| 1488 |
*data = prop->prop.data; |
| 1489 |
*size = prop->prop.size; |
| 1490 |
return; |
| 1491 |
} |
| 1492 |
} |
| 1493 |
} |
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
void dt_add_string_prop(struct device_tree_node *node, const char *name, |
| 1503 |
const char *str) |
| 1504 |
{ |
| 1505 |
dt_add_bin_prop(node, name, (char *)str, strlen(str) + 1); |
| 1506 |
} |
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val) |
| 1516 |
{ |
| 1517 |
u32 *val_ptr = xmalloc(sizeof(val)); |
| 1518 |
*val_ptr = htobe32(val); |
| 1519 |
dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr)); |
| 1520 |
} |
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val) |
| 1530 |
{ |
| 1531 |
u64 *val_ptr = xmalloc(sizeof(val)); |
| 1532 |
*val_ptr = htobe64(val); |
| 1533 |
dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr)); |
| 1534 |
} |
| 1535 |
|
| 1536 |
static void dt_add_addr_and_size_prop(struct device_tree_node *node, const char *prop_name, |
| 1537 |
const u64 *addrs, const u64 *sizes, int count, |
| 1538 |
u32 addr_cells, u32 size_cells) |
| 1539 |
{ |
| 1540 |
int i; |
| 1541 |
size_t length = (size_t)(addr_cells + size_cells) * sizeof(u32) * count; |
| 1542 |
u8 *data = xmalloc(length); |
| 1543 |
u8 *cur = data; |
| 1544 |
|
| 1545 |
for (i = 0; i < count; i++) { |
| 1546 |
dt_write_int(cur, addrs[i], addr_cells * sizeof(u32)); |
| 1547 |
cur += addr_cells * sizeof(u32); |
| 1548 |
dt_write_int(cur, sizes[i], size_cells * sizeof(u32)); |
| 1549 |
cur += size_cells * sizeof(u32); |
| 1550 |
} |
| 1551 |
|
| 1552 |
dt_add_bin_prop(node, prop_name, data, length); |
| 1553 |
} |
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
void dt_add_reg_prop(struct device_tree_node *node, const u64 *addrs, const u64 *sizes, |
| 1566 |
int count, u32 addr_cells, u32 size_cells) |
| 1567 |
{ |
| 1568 |
dt_add_addr_and_size_prop(node, "reg", addrs, sizes, count, addr_cells, size_cells); |
| 1569 |
} |
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
void dt_add_iommu_addr_prop(struct device_tree_node *node, const u64 *addrs, const u64 *sizes, |
| 1582 |
int count, u32 addr_cells, u32 size_cells) |
| 1583 |
{ |
| 1584 |
dt_add_addr_and_size_prop(node, "iommu-addresses", addrs, sizes, count, addr_cells, |
| 1585 |
size_cells); |
| 1586 |
} |
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
struct list_node device_tree_fixups; |
| 1593 |
|
| 1594 |
int dt_apply_fixups(struct device_tree *tree) |
| 1595 |
{ |
| 1596 |
struct device_tree_fixup *fixup; |
| 1597 |
list_for_each(fixup, device_tree_fixups, list_node) { |
| 1598 |
assert(fixup->fixup); |
| 1599 |
if (fixup->fixup(fixup, tree)) |
| 1600 |
return 1; |
| 1601 |
} |
| 1602 |
return 0; |
| 1603 |
} |
| 1604 |
|
| 1605 |
int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path, |
| 1606 |
void *data, size_t data_size, int create) |
| 1607 |
{ |
| 1608 |
char *path_copy, *prop_name; |
| 1609 |
struct device_tree_node *dt_node; |
| 1610 |
|
| 1611 |
path_copy = strdup(path); |
| 1612 |
|
| 1613 |
if (!path_copy) { |
| 1614 |
printk(BIOS_ERR, "Failed to allocate a copy of path %s\n", |
| 1615 |
path); |
| 1616 |
return 1; |
| 1617 |
} |
| 1618 |
|
| 1619 |
prop_name = strrchr(path_copy, '/'); |
| 1620 |
if (!prop_name) { |
| 1621 |
free(path_copy); |
| 1622 |
printk(BIOS_ERR, "Path %s does not include '/'\n", path); |
| 1623 |
return 1; |
| 1624 |
} |
| 1625 |
|
| 1626 |
*prop_name++ = '\0'; |
| 1627 |
|
| 1628 |
dt_node = dt_find_node_by_path(tree, path_copy, NULL, |
| 1629 |
NULL, create); |
| 1630 |
|
| 1631 |
if (!dt_node) { |
| 1632 |
printk(BIOS_ERR, "Failed to %s %s in the device tree\n", |
| 1633 |
create ? "create" : "find", path_copy); |
| 1634 |
free(path_copy); |
| 1635 |
return 1; |
| 1636 |
} |
| 1637 |
|
| 1638 |
dt_add_bin_prop(dt_node, prop_name, data, data_size); |
| 1639 |
free(path_copy); |
| 1640 |
|
| 1641 |
return 0; |
| 1642 |
} |
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree) |
| 1654 |
{ |
| 1655 |
struct device_tree_node *reserved; |
| 1656 |
u32 addr = 0, size = 0; |
| 1657 |
|
| 1658 |
reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr, |
| 1659 |
&size, 1); |
| 1660 |
if (!reserved) |
| 1661 |
return NULL; |
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
dt_add_u32_prop(reserved, "#address-cells", addr); |
| 1666 |
dt_add_u32_prop(reserved, "#size-cells", size); |
| 1667 |
|
| 1668 |
dt_add_bin_prop(reserved, "ranges", NULL, 0); |
| 1669 |
|
| 1670 |
return reserved; |
| 1671 |
} |
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
static uint32_t dt_adjust_phandle(struct device_tree_property *prop, |
| 1683 |
uint32_t adjustment, uint32_t offset) |
| 1684 |
{ |
| 1685 |
if (offset + 4 > prop->prop.size) |
| 1686 |
return 0; |
| 1687 |
|
| 1688 |
uint32_t phandle = be32dec(prop->prop.data + offset); |
| 1689 |
if (phandle == 0 || |
| 1690 |
phandle == FDT_PHANDLE_ILLEGAL || |
| 1691 |
phandle == 0xffffffff) |
| 1692 |
return 0; |
| 1693 |
|
| 1694 |
phandle += adjustment; |
| 1695 |
if (phandle >= FDT_PHANDLE_ILLEGAL) |
| 1696 |
return 0; |
| 1697 |
|
| 1698 |
be32enc(prop->prop.data + offset, phandle); |
| 1699 |
return phandle; |
| 1700 |
} |
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
static uint32_t dt_adjust_all_phandles(struct device_tree_node *node, |
| 1711 |
uint32_t base) |
| 1712 |
{ |
| 1713 |
uint32_t new_max = MAX(base, 1); |
| 1714 |
struct device_tree_property *prop; |
| 1715 |
struct device_tree_node *child; |
| 1716 |
|
| 1717 |
if (!node) |
| 1718 |
return new_max; |
| 1719 |
|
| 1720 |
list_for_each(prop, node->properties, list_node) |
| 1721 |
if (dt_prop_is_phandle(prop)) { |
| 1722 |
node->phandle = dt_adjust_phandle(prop, base, 0); |
| 1723 |
if (!node->phandle) |
| 1724 |
return 0; |
| 1725 |
new_max = MAX(new_max, node->phandle); |
| 1726 |
} |
| 1727 |
|
| 1728 |
list_for_each(child, node->children, list_node) |
| 1729 |
new_max = MAX(new_max, dt_adjust_all_phandles(child, base)); |
| 1730 |
|
| 1731 |
return new_max; |
| 1732 |
} |
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
static int dt_fixup_locals(struct device_tree_node *node, |
| 1744 |
struct device_tree_node *fixup, uint32_t base) |
| 1745 |
{ |
| 1746 |
struct device_tree_property *prop; |
| 1747 |
struct device_tree_property *fixup_prop; |
| 1748 |
struct device_tree_node *child; |
| 1749 |
struct device_tree_node *fixup_child; |
| 1750 |
int i; |
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
list_for_each(fixup_prop, fixup->properties, list_node) { |
| 1760 |
struct device_tree_property *base_prop = NULL; |
| 1761 |
list_for_each(prop, node->properties, list_node) |
| 1762 |
if (!strcmp(prop->prop.name, fixup_prop->prop.name)) { |
| 1763 |
base_prop = prop; |
| 1764 |
break; |
| 1765 |
} |
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
if (!base_prop || fixup_prop->prop.size % sizeof(uint32_t)) |
| 1770 |
return -1; |
| 1771 |
|
| 1772 |
for (i = 0; i < fixup_prop->prop.size; i += sizeof(uint32_t)) |
| 1773 |
if (!dt_adjust_phandle(base_prop, base, be32dec( |
| 1774 |
fixup_prop->prop.data + i))) |
| 1775 |
return -1; |
| 1776 |
} |
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
list_for_each(fixup_child, fixup->children, list_node) { |
| 1781 |
struct device_tree_node *base_child = NULL; |
| 1782 |
list_for_each(child, node->children, list_node) |
| 1783 |
if (!strcmp(child->name, fixup_child->name)) { |
| 1784 |
base_child = child; |
| 1785 |
break; |
| 1786 |
} |
| 1787 |
|
| 1788 |
|
| 1789 |
if (!base_child) |
| 1790 |
return -1; |
| 1791 |
|
| 1792 |
if (dt_fixup_locals(base_child, fixup_child, base) < 0) |
| 1793 |
return -1; |
| 1794 |
} |
| 1795 |
|
| 1796 |
return 0; |
| 1797 |
} |
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
static void dt_fix_symbols(struct device_tree_node *symbols, |
| 1808 |
struct device_tree_node *fragment, |
| 1809 |
const char *base_path) |
| 1810 |
{ |
| 1811 |
struct device_tree_property *prop; |
| 1812 |
char buf[512]; |
| 1813 |
char node_path[64]; |
| 1814 |
|
| 1815 |
if (!symbols) |
| 1816 |
return; |
| 1817 |
|
| 1818 |
int len = snprintf(node_path, sizeof(node_path), "/%s/__overlay__", |
| 1819 |
fragment->name); |
| 1820 |
|
| 1821 |
list_for_each(prop, symbols->properties, list_node) |
| 1822 |
if (!strncmp(prop->prop.data, node_path, len)) { |
| 1823 |
prop->prop.size = snprintf(buf, sizeof(buf), "%s%s", |
| 1824 |
base_path, (char *)prop->prop.data + len) + 1; |
| 1825 |
free(prop->prop.data); |
| 1826 |
prop->prop.data = strdup(buf); |
| 1827 |
} |
| 1828 |
} |
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
|
| 1841 |
|
| 1842 |
static int dt_fixup_external(struct device_tree *overlay, |
| 1843 |
struct device_tree_property *fixup, |
| 1844 |
uint32_t phandle, const char *base_path, |
| 1845 |
struct device_tree_node *overlay_symbols) |
| 1846 |
{ |
| 1847 |
struct device_tree_property *prop; |
| 1848 |
|
| 1849 |
|
| 1850 |
char *entry = fixup->prop.data; |
| 1851 |
while ((void *)entry < fixup->prop.data + fixup->prop.size) { |
| 1852 |
|
| 1853 |
char *node_path = entry; |
| 1854 |
entry = strchr(node_path, ':'); |
| 1855 |
if (!entry) |
| 1856 |
return -1; |
| 1857 |
*entry++ = '\0'; |
| 1858 |
|
| 1859 |
char *prop_name = entry; |
| 1860 |
entry = strchr(prop_name, ':'); |
| 1861 |
if (!entry) |
| 1862 |
return -1; |
| 1863 |
*entry++ = '\0'; |
| 1864 |
|
| 1865 |
struct device_tree_node *ovl_node = dt_find_node_by_path( |
| 1866 |
overlay, node_path, NULL, NULL, 0); |
| 1867 |
if (!ovl_node || !isdigit(*entry)) |
| 1868 |
return -1; |
| 1869 |
|
| 1870 |
struct device_tree_property *ovl_prop = NULL; |
| 1871 |
list_for_each(prop, ovl_node->properties, list_node) |
| 1872 |
if (!strcmp(prop->prop.name, prop_name)) { |
| 1873 |
ovl_prop = prop; |
| 1874 |
break; |
| 1875 |
} |
| 1876 |
|
| 1877 |
|
| 1878 |
uint32_t offset = skip_atoi(&entry); |
| 1879 |
if (!ovl_prop || offset + 4 > ovl_prop->prop.size || entry[0]) |
| 1880 |
return -1; |
| 1881 |
entry++; |
| 1882 |
|
| 1883 |
be32enc(ovl_prop->prop.data + offset, phandle); |
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
if (offset == 0 && !strcmp(prop_name, "target") && |
| 1888 |
!strchr(node_path + 1, '/')) |
| 1889 |
dt_fix_symbols(overlay_symbols, ovl_node, base_path); |
| 1890 |
} |
| 1891 |
|
| 1892 |
return 0; |
| 1893 |
} |
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
static int dt_fixup_all_externals(struct device_tree *tree, |
| 1908 |
struct device_tree_node *symbols, |
| 1909 |
struct device_tree *overlay, |
| 1910 |
struct device_tree_node *fixups, |
| 1911 |
struct device_tree_node *overlay_symbols) |
| 1912 |
{ |
| 1913 |
struct device_tree_property *fix; |
| 1914 |
|
| 1915 |
|
| 1916 |
if (!symbols) |
| 1917 |
return -1; |
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
list_for_each(fix, fixups->properties, list_node) { |
| 1925 |
|
| 1926 |
|
| 1927 |
const char *path = dt_find_string_prop(symbols, fix->prop.name); |
| 1928 |
if (!path) |
| 1929 |
return -1; |
| 1930 |
|
| 1931 |
|
| 1932 |
struct device_tree_node *node = dt_find_node_by_path(tree, path, |
| 1933 |
NULL, NULL, 0); |
| 1934 |
if (!node) |
| 1935 |
return -1; |
| 1936 |
|
| 1937 |
|
| 1938 |
if (dt_fixup_external(overlay, fix, node->phandle, |
| 1939 |
path, overlay_symbols) < 0) |
| 1940 |
return -1; |
| 1941 |
} |
| 1942 |
|
| 1943 |
return 0; |
| 1944 |
} |
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
static void dt_copy_subtree(struct device_tree_node *dst, |
| 1955 |
struct device_tree_node *src, int upd) |
| 1956 |
{ |
| 1957 |
struct device_tree_property *prop; |
| 1958 |
struct device_tree_property *src_prop; |
| 1959 |
list_for_each(src_prop, src->properties, list_node) { |
| 1960 |
if (dt_prop_is_phandle(src_prop) || |
| 1961 |
!strcmp(src_prop->prop.name, "name")) { |
| 1962 |
printk(BIOS_DEBUG, |
| 1963 |
"WARNING: ignoring illegal overlay prop '%s'\n", |
| 1964 |
src_prop->prop.name); |
| 1965 |
continue; |
| 1966 |
} |
| 1967 |
|
| 1968 |
struct device_tree_property *dst_prop = NULL; |
| 1969 |
list_for_each(prop, dst->properties, list_node) |
| 1970 |
if (!strcmp(prop->prop.name, src_prop->prop.name)) { |
| 1971 |
dst_prop = prop; |
| 1972 |
break; |
| 1973 |
} |
| 1974 |
|
| 1975 |
if (dst_prop) { |
| 1976 |
if (!upd) { |
| 1977 |
printk(BIOS_DEBUG, |
| 1978 |
"WARNING: ignoring prop update '%s'\n", |
| 1979 |
src_prop->prop.name); |
| 1980 |
continue; |
| 1981 |
} |
| 1982 |
} else { |
| 1983 |
dst_prop = alloc_prop(); |
| 1984 |
list_insert_after(&dst_prop->list_node, |
| 1985 |
&dst->properties); |
| 1986 |
} |
| 1987 |
|
| 1988 |
dst_prop->prop = src_prop->prop; |
| 1989 |
} |
| 1990 |
|
| 1991 |
struct device_tree_node *node; |
| 1992 |
struct device_tree_node *src_node; |
| 1993 |
list_for_each(src_node, src->children, list_node) { |
| 1994 |
struct device_tree_node *dst_node = NULL; |
| 1995 |
list_for_each(node, dst->children, list_node) |
| 1996 |
if (!strcmp(node->name, src_node->name)) { |
| 1997 |
dst_node = node; |
| 1998 |
break; |
| 1999 |
} |
| 2000 |
|
| 2001 |
if (!dst_node) { |
| 2002 |
dst_node = alloc_node(); |
| 2003 |
*dst_node = *src_node; |
| 2004 |
list_insert_after(&dst_node->list_node, &dst->children); |
| 2005 |
} else { |
| 2006 |
dt_copy_subtree(dst_node, src_node, upd); |
| 2007 |
} |
| 2008 |
} |
| 2009 |
} |
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
static int dt_import_fragment(struct device_tree *tree, |
| 2021 |
struct device_tree_node *fragment, |
| 2022 |
struct device_tree_node *overlay_symbols) |
| 2023 |
{ |
| 2024 |
|
| 2025 |
static const char *overlay_path[] = { "__overlay__", NULL }; |
| 2026 |
struct device_tree_node *overlay = dt_find_node(fragment, overlay_path, |
| 2027 |
NULL, NULL, 0); |
| 2028 |
|
| 2029 |
|
| 2030 |
if (!overlay) |
| 2031 |
return 0; |
| 2032 |
|
| 2033 |
|
| 2034 |
struct device_tree_property *prop; |
| 2035 |
struct device_tree_property *phandle = NULL; |
| 2036 |
struct device_tree_property *path = NULL; |
| 2037 |
list_for_each(prop, fragment->properties, list_node) { |
| 2038 |
if (!strcmp(prop->prop.name, "target")) { |
| 2039 |
phandle = prop; |
| 2040 |
break; |
| 2041 |
} |
| 2042 |
if (!strcmp(prop->prop.name, "target-path")) |
| 2043 |
path = prop; |
| 2044 |
} |
| 2045 |
|
| 2046 |
struct device_tree_node *target = NULL; |
| 2047 |
if (phandle) { |
| 2048 |
if (phandle->prop.size != sizeof(uint32_t)) |
| 2049 |
return -1; |
| 2050 |
target = dt_find_node_by_phandle(tree->root, |
| 2051 |
be32dec(phandle->prop.data)); |
| 2052 |
|
| 2053 |
} else if (path) { |
| 2054 |
target = dt_find_node_by_path(tree, path->prop.data, |
| 2055 |
NULL, NULL, 0); |
| 2056 |
dt_fix_symbols(overlay_symbols, fragment, path->prop.data); |
| 2057 |
} |
| 2058 |
if (!target) |
| 2059 |
return -1; |
| 2060 |
|
| 2061 |
dt_copy_subtree(target, overlay, 1); |
| 2062 |
return 0; |
| 2063 |
} |
| 2064 |
|
| 2065 |
|
| 2066 |
|
| 2067 |
|
| 2068 |
|
| 2069 |
|
| 2070 |
|
| 2071 |
|
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
int dt_apply_overlay(struct device_tree *tree, struct device_tree *overlay) |
| 2076 |
{ |
| 2077 |
|
| 2078 |
|
| 2079 |
|
| 2080 |
|
| 2081 |
|
| 2082 |
|
| 2083 |
uint32_t phandle_base = tree->max_phandle; |
| 2084 |
uint32_t new_max = dt_adjust_all_phandles(overlay->root, phandle_base); |
| 2085 |
if (!new_max) { |
| 2086 |
printk(BIOS_ERR, "invalid phandles in overlay\n"); |
| 2087 |
return -1; |
| 2088 |
} |
| 2089 |
tree->max_phandle = new_max; |
| 2090 |
|
| 2091 |
|
| 2092 |
|
| 2093 |
struct device_tree_node *local_fixups = dt_find_node_by_path(overlay, |
| 2094 |
"/__local_fixups__", NULL, NULL, 0); |
| 2095 |
if (local_fixups && dt_fixup_locals(overlay->root, local_fixups, |
| 2096 |
phandle_base) < 0) { |
| 2097 |
printk(BIOS_ERR, "invalid local fixups in overlay\n"); |
| 2098 |
return -1; |
| 2099 |
} |
| 2100 |
|
| 2101 |
|
| 2102 |
|
| 2103 |
|
| 2104 |
|
| 2105 |
|
| 2106 |
|
| 2107 |
|
| 2108 |
|
| 2109 |
|
| 2110 |
|
| 2111 |
|
| 2112 |
|
| 2113 |
struct device_tree_node *symbols = dt_find_node_by_path(tree, |
| 2114 |
"/__symbols__", NULL, NULL, 0); |
| 2115 |
struct device_tree_node *fixups = dt_find_node_by_path(overlay, |
| 2116 |
"/__fixups__", NULL, NULL, 0); |
| 2117 |
struct device_tree_node *overlay_symbols = dt_find_node_by_path(overlay, |
| 2118 |
"/__symbols__", NULL, NULL, 0); |
| 2119 |
if (fixups && dt_fixup_all_externals(tree, symbols, overlay, |
| 2120 |
fixups, overlay_symbols) < 0) { |
| 2121 |
printk(BIOS_ERR, "cannot match external fixups from overlay\n"); |
| 2122 |
return -1; |
| 2123 |
} |
| 2124 |
|
| 2125 |
|
| 2126 |
|
| 2127 |
struct device_tree_node *fragment; |
| 2128 |
list_for_each(fragment, overlay->root->children, list_node) |
| 2129 |
if (dt_import_fragment(tree, fragment, overlay_symbols) < 0) { |
| 2130 |
printk(BIOS_ERR, "bad DT fragment '%s'\n", |
| 2131 |
fragment->name); |
| 2132 |
return -1; |
| 2133 |
} |
| 2134 |
|
| 2135 |
|
| 2136 |
|
| 2137 |
|
| 2138 |
|
| 2139 |
|
| 2140 |
|
| 2141 |
|
| 2142 |
if (overlay_symbols) { |
| 2143 |
if (symbols) |
| 2144 |
dt_copy_subtree(symbols, overlay_symbols, 0); |
| 2145 |
else |
| 2146 |
list_insert_after(&overlay_symbols->list_node, |
| 2147 |
&tree->root->children); |
| 2148 |
} |
| 2149 |
|
| 2150 |
return 0; |
| 2151 |
} |