Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 3 | * Copyright 2018-present Facebook, Inc. |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 4 | * |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 5 | * Taken from depthcharge: src/base/device_tree.c |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 6 | * |
| 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 Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 19 | #include <commonlib/stdlib.h> |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 20 | #include <console/console.h> |
| 21 | #include <device_tree.h> |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 22 | #include <endian.h> |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 23 | #include <stdint.h> |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 24 | #include <string.h> |
| 25 | #include <stddef.h> |
| 26 | #include <stdlib.h> |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * Functions for picking apart flattened trees. |
| 30 | */ |
| 31 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 32 | int fdt_next_property(const void *blob, uint32_t offset, |
| 33 | struct fdt_property *prop) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 34 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 35 | struct fdt_header *header = (struct fdt_header *)blob; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 36 | uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset); |
| 37 | |
| 38 | int index = 0; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 39 | if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 40 | return 0; |
| 41 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 42 | uint32_t size = be32toh(ptr[index++]); |
| 43 | uint32_t name_offset = be32toh(ptr[index++]); |
| 44 | name_offset += be32toh(header->strings_offset); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 45 | |
| 46 | if (prop) { |
| 47 | prop->name = (char *)((uint8_t *)blob + name_offset); |
| 48 | prop->data = &ptr[index]; |
| 49 | prop->size = size; |
| 50 | } |
| 51 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 52 | index += DIV_ROUND_UP(size, sizeof(uint32_t)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 53 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 54 | return index * sizeof(uint32_t); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 57 | int fdt_node_name(const void *blob, uint32_t offset, const char **name) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 58 | { |
| 59 | uint8_t *ptr = ((uint8_t *)blob) + offset; |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 60 | if (be32dec(ptr) != FDT_TOKEN_BEGIN_NODE) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 61 | return 0; |
| 62 | |
| 63 | ptr += 4; |
| 64 | if (name) |
| 65 | *name = (char *)ptr; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 66 | return ALIGN_UP(strlen((char *)ptr) + 1, sizeof(uint32_t)) + 4; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 69 | static 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 Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 75 | |
| 76 | |
| 77 | /* |
| 78 | * Functions for printing flattened trees. |
| 79 | */ |
| 80 | |
| 81 | static void print_indent(int depth) |
| 82 | { |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 83 | printk(BIOS_DEBUG, "%*s", depth * 8, ""); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 86 | static void print_property(const struct fdt_property *prop, int depth) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 87 | { |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 88 | 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 Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 96 | print_indent(depth); |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 97 | 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 Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 112 | } |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 115 | static int print_flat_node(const void *blob, uint32_t start_offset, int depth) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 116 | { |
| 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 Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 127 | printk(BIOS_DEBUG, "%s {\n", name); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 128 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 129 | struct fdt_property prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 130 | while ((size = fdt_next_property(blob, offset, &prop))) { |
| 131 | print_property(&prop, depth + 1); |
| 132 | |
| 133 | offset += size; |
| 134 | } |
| 135 | |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 136 | printk(BIOS_DEBUG, "\n"); // empty line between props and nodes |
| 137 | |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 138 | while ((size = print_flat_node(blob, offset, depth + 1))) |
| 139 | offset += size; |
| 140 | |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 141 | print_indent(depth); |
| 142 | printk(BIOS_DEBUG, "}\n"); |
| 143 | |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 144 | return offset - start_offset + sizeof(uint32_t); |
| 145 | } |
| 146 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 147 | void fdt_print_node(const void *blob, uint32_t offset) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 148 | { |
| 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 Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 158 | int fdt_skip_node(const void *blob, uint32_t start_offset) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 159 | { |
| 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 Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 183 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 184 | static int fdt_unflatten_node(const void *blob, uint32_t start_offset, |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 185 | struct device_tree *tree, |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 186 | struct device_tree_node **new_node) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 187 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 188 | struct list_node *last; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 189 | 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 Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 198 | struct device_tree_node *node = xzalloc(sizeof(*node)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 199 | *new_node = node; |
| 200 | node->name = name; |
| 201 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 202 | struct fdt_property fprop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 203 | last = &node->properties; |
| 204 | while ((size = fdt_next_property(blob, offset, &fprop))) { |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 205 | struct device_tree_property *prop = xzalloc(sizeof(*prop)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 206 | prop->prop = fprop; |
| 207 | |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 208 | 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 Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 214 | list_insert_after(&prop->list_node, last); |
| 215 | last = &prop->list_node; |
| 216 | |
| 217 | offset += size; |
| 218 | } |
| 219 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 220 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 221 | last = &node->children; |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 222 | while ((size = fdt_unflatten_node(blob, offset, tree, &child))) { |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 223 | 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 Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 232 | static int fdt_unflatten_map_entry(const void *blob, uint32_t offset, |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 233 | struct device_tree_reserve_map_entry **new) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 234 | { |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 235 | 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 Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 238 | |
| 239 | if (!size) |
| 240 | return 0; |
| 241 | |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 242 | struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry)); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 243 | *new = entry; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 244 | entry->start = start; |
| 245 | entry->size = size; |
| 246 | |
| 247 | return sizeof(uint64_t) * 2; |
| 248 | } |
| 249 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 250 | struct device_tree *fdt_unflatten(const void *blob) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 251 | { |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 252 | struct device_tree *tree = xzalloc(sizeof(*tree)); |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 253 | const struct fdt_header *header = (const struct fdt_header *)blob; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 254 | tree->header = header; |
| 255 | |
Julius Werner | 73eaec8 | 2019-05-03 17:58:07 -0700 | [diff] [blame] | 256 | 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); |
Jacob Garber | 698d83a | 2019-06-07 10:28:54 -0600 | [diff] [blame] | 262 | free(tree); |
Julius Werner | 73eaec8 | 2019-05-03 17:58:07 -0700 | [diff] [blame] | 263 | return NULL; |
| 264 | } |
| 265 | if (last_comp_version > FDT_SUPPORTED_VERSION) { |
| 266 | printk(BIOS_DEBUG, "Unsupported device tree version %u(>=%u)\n", |
| 267 | version, last_comp_version); |
Jacob Garber | 698d83a | 2019-06-07 10:28:54 -0600 | [diff] [blame] | 268 | free(tree); |
Julius Werner | 73eaec8 | 2019-05-03 17:58:07 -0700 | [diff] [blame] | 269 | return NULL; |
| 270 | } |
| 271 | if (version > FDT_SUPPORTED_VERSION) |
| 272 | printk(BIOS_DEBUG, |
| 273 | "NOTE: FDT version %u too new, should add support!\n", |
| 274 | version); |
| 275 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 276 | uint32_t struct_offset = be32toh(header->structure_offset); |
| 277 | uint32_t strings_offset = be32toh(header->strings_offset); |
| 278 | uint32_t reserve_offset = be32toh(header->reserve_map_offset); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 279 | uint32_t min_offset = 0; |
| 280 | min_offset = MIN(struct_offset, strings_offset); |
| 281 | min_offset = MIN(min_offset, reserve_offset); |
| 282 | // Assume everything up to the first non-header component is part of |
| 283 | // the header and needs to be preserved. This will protect us against |
| 284 | // new elements being added in the future. |
| 285 | tree->header_size = min_offset; |
| 286 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 287 | struct device_tree_reserve_map_entry *entry; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 288 | uint32_t offset = reserve_offset; |
| 289 | int size; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 290 | struct list_node *last = &tree->reserve_map; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 291 | while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) { |
| 292 | list_insert_after(&entry->list_node, last); |
| 293 | last = &entry->list_node; |
| 294 | |
| 295 | offset += size; |
| 296 | } |
| 297 | |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 298 | fdt_unflatten_node(blob, struct_offset, tree, &tree->root); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 299 | |
| 300 | return tree; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | |
| 305 | /* |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 306 | * Functions to find the size of the device tree if it was flattened. |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 307 | */ |
| 308 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 309 | static void dt_flat_prop_size(struct device_tree_property *prop, |
| 310 | uint32_t *struct_size, uint32_t *strings_size) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 311 | { |
| 312 | // Starting token. |
| 313 | *struct_size += sizeof(uint32_t); |
| 314 | // Size. |
| 315 | *struct_size += sizeof(uint32_t); |
| 316 | // Name offset. |
| 317 | *struct_size += sizeof(uint32_t); |
| 318 | // Property value. |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 319 | *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 320 | |
| 321 | // Property name. |
| 322 | *strings_size += strlen(prop->prop.name) + 1; |
| 323 | } |
| 324 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 325 | static void dt_flat_node_size(struct device_tree_node *node, |
| 326 | uint32_t *struct_size, uint32_t *strings_size) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 327 | { |
| 328 | // Starting token. |
| 329 | *struct_size += sizeof(uint32_t); |
| 330 | // Node name. |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 331 | *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 332 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 333 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 334 | list_for_each(prop, node->properties, list_node) |
| 335 | dt_flat_prop_size(prop, struct_size, strings_size); |
| 336 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 337 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 338 | list_for_each(child, node->children, list_node) |
| 339 | dt_flat_node_size(child, struct_size, strings_size); |
| 340 | |
| 341 | // End token. |
| 342 | *struct_size += sizeof(uint32_t); |
| 343 | } |
| 344 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 345 | uint32_t dt_flat_size(const struct device_tree *tree) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 346 | { |
| 347 | uint32_t size = tree->header_size; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 348 | struct device_tree_reserve_map_entry *entry; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 349 | list_for_each(entry, tree->reserve_map, list_node) |
| 350 | size += sizeof(uint64_t) * 2; |
| 351 | size += sizeof(uint64_t) * 2; |
| 352 | |
| 353 | uint32_t struct_size = 0; |
| 354 | uint32_t strings_size = 0; |
| 355 | dt_flat_node_size(tree->root, &struct_size, &strings_size); |
| 356 | |
| 357 | size += struct_size; |
| 358 | // End token. |
| 359 | size += sizeof(uint32_t); |
| 360 | |
| 361 | size += strings_size; |
| 362 | |
| 363 | return size; |
| 364 | } |
| 365 | |
| 366 | |
| 367 | |
| 368 | /* |
| 369 | * Functions to flatten a device tree. |
| 370 | */ |
| 371 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 372 | static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry, |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 373 | void **map_start) |
| 374 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 375 | ((uint64_t *)*map_start)[0] = htobe64(entry->start); |
| 376 | ((uint64_t *)*map_start)[1] = htobe64(entry->size); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 377 | *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2; |
| 378 | } |
| 379 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 380 | static void dt_flatten_prop(struct device_tree_property *prop, |
| 381 | void **struct_start, void *strings_base, |
| 382 | void **strings_start) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 383 | { |
| 384 | uint8_t *dstruct = (uint8_t *)*struct_start; |
| 385 | uint8_t *dstrings = (uint8_t *)*strings_start; |
| 386 | |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 387 | be32enc(dstruct, FDT_TOKEN_PROPERTY); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 388 | dstruct += sizeof(uint32_t); |
| 389 | |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 390 | be32enc(dstruct, prop->prop.size); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 391 | dstruct += sizeof(uint32_t); |
| 392 | |
| 393 | uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base; |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 394 | be32enc(dstruct, name_offset); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 395 | dstruct += sizeof(uint32_t); |
| 396 | |
| 397 | strcpy((char *)dstrings, prop->prop.name); |
| 398 | dstrings += strlen(prop->prop.name) + 1; |
| 399 | |
| 400 | memcpy(dstruct, prop->prop.data, prop->prop.size); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 401 | dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 402 | |
| 403 | *struct_start = dstruct; |
| 404 | *strings_start = dstrings; |
| 405 | } |
| 406 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 407 | static void dt_flatten_node(const struct device_tree_node *node, |
| 408 | void **struct_start, void *strings_base, |
| 409 | void **strings_start) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 410 | { |
| 411 | uint8_t *dstruct = (uint8_t *)*struct_start; |
| 412 | uint8_t *dstrings = (uint8_t *)*strings_start; |
| 413 | |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 414 | be32enc(dstruct, FDT_TOKEN_BEGIN_NODE); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 415 | dstruct += sizeof(uint32_t); |
| 416 | |
| 417 | strcpy((char *)dstruct, node->name); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 418 | dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 419 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 420 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 421 | list_for_each(prop, node->properties, list_node) |
| 422 | dt_flatten_prop(prop, (void **)&dstruct, strings_base, |
| 423 | (void **)&dstrings); |
| 424 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 425 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 426 | list_for_each(child, node->children, list_node) |
| 427 | dt_flatten_node(child, (void **)&dstruct, strings_base, |
| 428 | (void **)&dstrings); |
| 429 | |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 430 | be32enc(dstruct, FDT_TOKEN_END_NODE); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 431 | dstruct += sizeof(uint32_t); |
| 432 | |
| 433 | *struct_start = dstruct; |
| 434 | *strings_start = dstrings; |
| 435 | } |
| 436 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 437 | void dt_flatten(const struct device_tree *tree, void *start_dest) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 438 | { |
| 439 | uint8_t *dest = (uint8_t *)start_dest; |
| 440 | |
| 441 | memcpy(dest, tree->header, tree->header_size); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 442 | struct fdt_header *header = (struct fdt_header *)dest; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 443 | dest += tree->header_size; |
| 444 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 445 | struct device_tree_reserve_map_entry *entry; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 446 | list_for_each(entry, tree->reserve_map, list_node) |
| 447 | dt_flatten_map_entry(entry, (void **)&dest); |
| 448 | ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0; |
| 449 | dest += sizeof(uint64_t) * 2; |
| 450 | |
| 451 | uint32_t struct_size = 0; |
| 452 | uint32_t strings_size = 0; |
| 453 | dt_flat_node_size(tree->root, &struct_size, &strings_size); |
| 454 | |
| 455 | uint8_t *struct_start = dest; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 456 | header->structure_offset = htobe32(dest - (uint8_t *)start_dest); |
| 457 | header->structure_size = htobe32(struct_size); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 458 | dest += struct_size; |
| 459 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 460 | *((uint32_t *)dest) = htobe32(FDT_TOKEN_END); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 461 | dest += sizeof(uint32_t); |
| 462 | |
| 463 | uint8_t *strings_start = dest; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 464 | header->strings_offset = htobe32(dest - (uint8_t *)start_dest); |
| 465 | header->strings_size = htobe32(strings_size); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 466 | dest += strings_size; |
| 467 | |
| 468 | dt_flatten_node(tree->root, (void **)&struct_start, strings_start, |
| 469 | (void **)&strings_start); |
| 470 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 471 | header->totalsize = htobe32(dest - (uint8_t *)start_dest); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | |
| 475 | |
| 476 | /* |
| 477 | * Functions for printing a non-flattened device tree. |
| 478 | */ |
| 479 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 480 | static void print_node(const struct device_tree_node *node, int depth) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 481 | { |
| 482 | print_indent(depth); |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 483 | if (depth == 0) // root node has no name, print a starting slash |
| 484 | printk(BIOS_DEBUG, "/"); |
| 485 | printk(BIOS_DEBUG, "%s {\n", node->name); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 486 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 487 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 488 | list_for_each(prop, node->properties, list_node) |
| 489 | print_property(&prop->prop, depth + 1); |
| 490 | |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 491 | printk(BIOS_DEBUG, "\n"); // empty line between props and nodes |
| 492 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 493 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 494 | list_for_each(child, node->children, list_node) |
| 495 | print_node(child, depth + 1); |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 496 | |
| 497 | print_indent(depth); |
| 498 | printk(BIOS_DEBUG, "};\n"); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 499 | } |
| 500 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 501 | void dt_print_node(const struct device_tree_node *node) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 502 | { |
| 503 | print_node(node, 0); |
| 504 | } |
| 505 | |
| 506 | |
| 507 | |
| 508 | /* |
| 509 | * Functions for reading and manipulating an unflattened device tree. |
| 510 | */ |
| 511 | |
| 512 | /* |
| 513 | * Read #address-cells and #size-cells properties from a node. |
| 514 | * |
| 515 | * @param node The device tree node to read from. |
| 516 | * @param addrcp Pointer to store #address-cells in, skipped if NULL. |
| 517 | * @param sizecp Pointer to store #size-cells in, skipped if NULL. |
| 518 | */ |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 519 | void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp, |
| 520 | u32 *sizecp) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 521 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 522 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 523 | list_for_each(prop, node->properties, list_node) { |
| 524 | if (addrcp && !strcmp("#address-cells", prop->prop.name)) |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 525 | *addrcp = be32dec(prop->prop.data); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 526 | if (sizecp && !strcmp("#size-cells", prop->prop.name)) |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 527 | *sizecp = be32dec(prop->prop.data); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Find a node from a device tree path, relative to a parent node. |
| 533 | * |
| 534 | * @param parent The node from which to start the relative path lookup. |
| 535 | * @param path An array of path component strings that will be looked |
Elyes HAOUAS | e3e3f4f | 2018-06-29 21:41:41 +0200 | [diff] [blame] | 536 | * up in order to find the node. Must be terminated with |
| 537 | * a NULL pointer. Example: {'firmware', 'coreboot', NULL} |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 538 | * @param addrcp Pointer that will be updated with any #address-cells |
Elyes HAOUAS | e3e3f4f | 2018-06-29 21:41:41 +0200 | [diff] [blame] | 539 | * value found in the path. May be NULL to ignore. |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 540 | * @param sizecp Pointer that will be updated with any #size-cells |
Elyes HAOUAS | e3e3f4f | 2018-06-29 21:41:41 +0200 | [diff] [blame] | 541 | * value found in the path. May be NULL to ignore. |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 542 | * @param create 1: Create node(s) if not found. 0: Return NULL instead. |
| 543 | * @return The found/created node, or NULL. |
| 544 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 545 | struct device_tree_node *dt_find_node(struct device_tree_node *parent, |
| 546 | const char **path, u32 *addrcp, |
| 547 | u32 *sizecp, int create) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 548 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 549 | struct device_tree_node *node, *found = NULL; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 550 | |
| 551 | // Update #address-cells and #size-cells for this level. |
| 552 | dt_read_cell_props(parent, addrcp, sizecp); |
| 553 | |
| 554 | if (!*path) |
| 555 | return parent; |
| 556 | |
| 557 | // Find the next node in the path, if it exists. |
| 558 | list_for_each(node, parent->children, list_node) { |
| 559 | if (!strcmp(node->name, *path)) { |
| 560 | found = node; |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | // Otherwise create it or return NULL. |
| 566 | if (!found) { |
| 567 | if (!create) |
| 568 | return NULL; |
| 569 | |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 570 | found = malloc(sizeof(*found)); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 571 | if (!found) |
| 572 | return NULL; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 573 | found->name = strdup(*path); |
| 574 | if (!found->name) |
| 575 | return NULL; |
| 576 | |
| 577 | list_insert_after(&found->list_node, &parent->children); |
| 578 | } |
| 579 | |
| 580 | return dt_find_node(found, path + 1, addrcp, sizecp, create); |
| 581 | } |
| 582 | |
| 583 | /* |
Julius Werner | f36d53c | 2019-05-03 18:23:34 -0700 | [diff] [blame] | 584 | * Find a node in the tree from a string device tree path. |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 585 | * |
Julius Werner | f36d53c | 2019-05-03 18:23:34 -0700 | [diff] [blame] | 586 | * @param tree The device tree to search. |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 587 | * @param path A string representing a path in the device tree, with |
Julius Werner | fbec63d | 2019-05-03 18:29:28 -0700 | [diff] [blame] | 588 | * nodes separated by '/'. Example: "/firmware/coreboot" |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 589 | * @param addrcp Pointer that will be updated with any #address-cells |
| 590 | * value found in the path. May be NULL to ignore. |
| 591 | * @param sizecp Pointer that will be updated with any #size-cells |
| 592 | * value found in the path. May be NULL to ignore. |
| 593 | * @param create 1: Create node(s) if not found. 0: Return NULL instead. |
| 594 | * @return The found/created node, or NULL. |
| 595 | * |
Julius Werner | 6d5695f | 2019-05-06 19:23:28 -0700 | [diff] [blame] | 596 | * It is the caller responsibility to provide a path string that doesn't end |
| 597 | * with a '/' and doesn't contain any "//". If the path does not start with a |
| 598 | * '/', the first segment is interpreted as an alias. */ |
Julius Werner | f36d53c | 2019-05-03 18:23:34 -0700 | [diff] [blame] | 599 | struct device_tree_node *dt_find_node_by_path(struct device_tree *tree, |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 600 | const char *path, u32 *addrcp, |
| 601 | u32 *sizecp, int create) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 602 | { |
Julius Werner | 6d5695f | 2019-05-06 19:23:28 -0700 | [diff] [blame] | 603 | char *sub_path; |
| 604 | char *duped_str; |
| 605 | struct device_tree_node *parent; |
| 606 | char *next_slash; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 607 | /* Hopefully enough depth for any node. */ |
| 608 | const char *path_array[15]; |
| 609 | int i; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 610 | struct device_tree_node *node = NULL; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 611 | |
Julius Werner | 6d5695f | 2019-05-06 19:23:28 -0700 | [diff] [blame] | 612 | if (path[0] == '/') { // regular path |
| 613 | if (path[1] == '\0') { // special case: "/" is root node |
| 614 | dt_read_cell_props(tree->root, addrcp, sizecp); |
| 615 | return tree->root; |
| 616 | } |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 617 | |
Julius Werner | 6d5695f | 2019-05-06 19:23:28 -0700 | [diff] [blame] | 618 | sub_path = duped_str = strdup(&path[1]); |
| 619 | if (!sub_path) |
| 620 | return NULL; |
| 621 | |
| 622 | parent = tree->root; |
| 623 | } else { // alias |
| 624 | char *alias; |
| 625 | |
| 626 | alias = duped_str = strdup(path); |
| 627 | if (!alias) |
| 628 | return NULL; |
| 629 | |
| 630 | sub_path = strchr(alias, '/'); |
| 631 | if (sub_path) |
| 632 | *sub_path = '\0'; |
| 633 | |
| 634 | parent = dt_find_node_by_alias(tree, alias); |
| 635 | if (!parent) { |
| 636 | printk(BIOS_DEBUG, |
| 637 | "Could not find node '%s', alias '%s' does not exist\n", |
| 638 | path, alias); |
| 639 | free(duped_str); |
| 640 | return NULL; |
| 641 | } |
| 642 | |
| 643 | if (!sub_path) { |
| 644 | // it's just the alias, no sub-path |
| 645 | free(duped_str); |
| 646 | return parent; |
| 647 | } |
| 648 | |
| 649 | sub_path++; |
| 650 | } |
| 651 | |
| 652 | next_slash = sub_path; |
| 653 | path_array[0] = sub_path; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 654 | for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) { |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 655 | next_slash = strchr(next_slash, '/'); |
| 656 | if (!next_slash) |
| 657 | break; |
| 658 | |
| 659 | *next_slash++ = '\0'; |
| 660 | path_array[i] = next_slash; |
| 661 | } |
| 662 | |
| 663 | if (!next_slash) { |
| 664 | path_array[i] = NULL; |
Julius Werner | 6d5695f | 2019-05-06 19:23:28 -0700 | [diff] [blame] | 665 | node = dt_find_node(parent, path_array, |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 666 | addrcp, sizecp, create); |
| 667 | } |
| 668 | |
Julius Werner | 6d5695f | 2019-05-06 19:23:28 -0700 | [diff] [blame] | 669 | free(duped_str); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 670 | return node; |
| 671 | } |
| 672 | |
Julius Werner | 6d5695f | 2019-05-06 19:23:28 -0700 | [diff] [blame] | 673 | /* |
| 674 | * Find a node from an alias |
| 675 | * |
| 676 | * @param tree The device tree. |
| 677 | * @param alias The alias name. |
| 678 | * @return The found node, or NULL. |
| 679 | */ |
| 680 | struct device_tree_node *dt_find_node_by_alias(struct device_tree *tree, |
| 681 | const char *alias) |
| 682 | { |
| 683 | struct device_tree_node *node; |
| 684 | const char *alias_path; |
| 685 | |
| 686 | node = dt_find_node_by_path(tree, "/aliases", NULL, NULL, 0); |
| 687 | if (!node) |
| 688 | return NULL; |
| 689 | |
| 690 | alias_path = dt_find_string_prop(node, alias); |
| 691 | if (!alias_path) |
| 692 | return NULL; |
| 693 | |
| 694 | return dt_find_node_by_path(tree, alias_path, NULL, NULL, 0); |
| 695 | } |
| 696 | |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 697 | struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root, |
| 698 | uint32_t phandle) |
| 699 | { |
| 700 | if (!root) |
| 701 | return NULL; |
| 702 | |
| 703 | if (root->phandle == phandle) |
| 704 | return root; |
| 705 | |
| 706 | struct device_tree_node *node; |
| 707 | struct device_tree_node *result; |
| 708 | list_for_each(node, root->children, list_node) { |
| 709 | result = dt_find_node_by_phandle(node, phandle); |
| 710 | if (result) |
| 711 | return result; |
| 712 | } |
| 713 | |
| 714 | return NULL; |
| 715 | } |
| 716 | |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 717 | /* |
| 718 | * Check if given node is compatible. |
| 719 | * |
| 720 | * @param node The node which is to be checked for compatible property. |
| 721 | * @param compat The compatible string to match. |
| 722 | * @return 1 = compatible, 0 = not compatible. |
| 723 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 724 | static int dt_check_compat_match(struct device_tree_node *node, |
| 725 | const char *compat) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 726 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 727 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 728 | |
| 729 | list_for_each(prop, node->properties, list_node) { |
| 730 | if (!strcmp("compatible", prop->prop.name)) { |
| 731 | size_t bytes = prop->prop.size; |
| 732 | const char *str = prop->prop.data; |
| 733 | while (bytes > 0) { |
| 734 | if (!strncmp(compat, str, bytes)) |
| 735 | return 1; |
| 736 | size_t len = strnlen(str, bytes) + 1; |
| 737 | if (bytes <= len) |
| 738 | break; |
| 739 | str += len; |
| 740 | bytes -= len; |
| 741 | } |
| 742 | break; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | * Find a node from a compatible string, in the subtree of a parent node. |
| 751 | * |
| 752 | * @param parent The parent node under which to look. |
| 753 | * @param compat The compatible string to find. |
| 754 | * @return The found node, or NULL. |
| 755 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 756 | struct device_tree_node *dt_find_compat(struct device_tree_node *parent, |
| 757 | const char *compat) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 758 | { |
| 759 | // Check if the parent node itself is compatible. |
| 760 | if (dt_check_compat_match(parent, compat)) |
| 761 | return parent; |
| 762 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 763 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 764 | list_for_each(child, parent->children, list_node) { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 765 | struct device_tree_node *found = dt_find_compat(child, compat); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 766 | if (found) |
| 767 | return found; |
| 768 | } |
| 769 | |
| 770 | return NULL; |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * Find the next compatible child of a given parent. All children upto the |
| 775 | * child passed in by caller are ignored. If child is NULL, it considers all the |
| 776 | * children to find the first child which is compatible. |
| 777 | * |
| 778 | * @param parent The parent node under which to look. |
| 779 | * @param child The child node to start search from (exclusive). If NULL |
| 780 | * consider all children. |
| 781 | * @param compat The compatible string to find. |
| 782 | * @return The found node, or NULL. |
| 783 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 784 | struct device_tree_node * |
| 785 | dt_find_next_compat_child(struct device_tree_node *parent, |
| 786 | struct device_tree_node *child, |
| 787 | const char *compat) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 788 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 789 | struct device_tree_node *next; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 790 | int ignore = 0; |
| 791 | |
| 792 | if (child) |
| 793 | ignore = 1; |
| 794 | |
| 795 | list_for_each(next, parent->children, list_node) { |
| 796 | if (ignore) { |
| 797 | if (child == next) |
| 798 | ignore = 0; |
| 799 | continue; |
| 800 | } |
| 801 | |
| 802 | if (dt_check_compat_match(next, compat)) |
| 803 | return next; |
| 804 | } |
| 805 | |
| 806 | return NULL; |
| 807 | } |
| 808 | |
| 809 | /* |
| 810 | * Find a node with matching property value, in the subtree of a parent node. |
| 811 | * |
| 812 | * @param parent The parent node under which to look. |
| 813 | * @param name The property name to look for. |
| 814 | * @param data The property value to look for. |
| 815 | * @param size The property size. |
| 816 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 817 | struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent, |
| 818 | const char *name, void *data, |
| 819 | size_t size) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 820 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 821 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 822 | |
| 823 | /* Check if parent itself has the required property value. */ |
| 824 | list_for_each(prop, parent->properties, list_node) { |
| 825 | if (!strcmp(name, prop->prop.name)) { |
| 826 | size_t bytes = prop->prop.size; |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 827 | const void *prop_data = prop->prop.data; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 828 | if (size != bytes) |
| 829 | break; |
| 830 | if (!memcmp(data, prop_data, size)) |
| 831 | return parent; |
| 832 | break; |
| 833 | } |
| 834 | } |
| 835 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 836 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 837 | list_for_each(child, parent->children, list_node) { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 838 | struct device_tree_node *found = dt_find_prop_value(child, name, |
| 839 | data, size); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 840 | if (found) |
| 841 | return found; |
| 842 | } |
| 843 | return NULL; |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | * Write an arbitrary sized big-endian integer into a pointer. |
| 848 | * |
| 849 | * @param dest Pointer to the DT property data buffer to write. |
Elyes HAOUAS | 1ec7644 | 2018-08-07 12:20:04 +0200 | [diff] [blame] | 850 | * @param src The integer to write (in CPU endianness). |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 851 | * @param length the length of the destination integer in bytes. |
| 852 | */ |
| 853 | void dt_write_int(u8 *dest, u64 src, size_t length) |
| 854 | { |
| 855 | while (length--) { |
| 856 | dest[length] = (u8)src; |
| 857 | src >>= 8; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | /* |
Patrick Rudolph | 5ccc731 | 2018-05-30 15:05:28 +0200 | [diff] [blame] | 862 | * Delete a property by name in a given node if it exists. |
| 863 | * |
| 864 | * @param node The device tree node to operate on. |
| 865 | * @param name The name of the property to delete. |
| 866 | */ |
| 867 | void dt_delete_prop(struct device_tree_node *node, const char *name) |
| 868 | { |
| 869 | struct device_tree_property *prop; |
| 870 | |
| 871 | list_for_each(prop, node->properties, list_node) { |
| 872 | if (!strcmp(prop->prop.name, name)) { |
| 873 | list_remove(&prop->list_node); |
| 874 | return; |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | /* |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 880 | * Add an arbitrary property to a node, or update it if it already exists. |
| 881 | * |
| 882 | * @param node The device tree node to add to. |
| 883 | * @param name The name of the new property. |
| 884 | * @param data The raw data blob to be stored in the property. |
| 885 | * @param size The size of data in bytes. |
| 886 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 887 | void dt_add_bin_prop(struct device_tree_node *node, const char *name, |
Julius Werner | 0e9116f | 2019-05-13 17:30:31 -0700 | [diff] [blame] | 888 | void *data, size_t size) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 889 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 890 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 891 | |
| 892 | list_for_each(prop, node->properties, list_node) { |
| 893 | if (!strcmp(prop->prop.name, name)) { |
| 894 | prop->prop.data = data; |
| 895 | prop->prop.size = size; |
| 896 | return; |
| 897 | } |
| 898 | } |
| 899 | |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 900 | prop = xzalloc(sizeof(*prop)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 901 | list_insert_after(&prop->list_node, &node->properties); |
| 902 | prop->prop.name = name; |
| 903 | prop->prop.data = data; |
| 904 | prop->prop.size = size; |
| 905 | } |
| 906 | |
| 907 | /* |
| 908 | * Find given string property in a node and return its content. |
| 909 | * |
| 910 | * @param node The device tree node to search. |
| 911 | * @param name The name of the property. |
| 912 | * @return The found string, or NULL. |
| 913 | */ |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 914 | const char *dt_find_string_prop(const struct device_tree_node *node, |
| 915 | const char *name) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 916 | { |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 917 | const void *content; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 918 | size_t size; |
| 919 | |
| 920 | dt_find_bin_prop(node, name, &content, &size); |
| 921 | |
| 922 | return content; |
| 923 | } |
| 924 | |
| 925 | /* |
| 926 | * Find given property in a node. |
| 927 | * |
| 928 | * @param node The device tree node to search. |
| 929 | * @param name The name of the property. |
| 930 | * @param data Pointer to return raw data blob in the property. |
| 931 | * @param size Pointer to return the size of data in bytes. |
| 932 | */ |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 933 | void dt_find_bin_prop(const struct device_tree_node *node, const char *name, |
| 934 | const void **data, size_t *size) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 935 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 936 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 937 | |
| 938 | *data = NULL; |
| 939 | *size = 0; |
| 940 | |
| 941 | list_for_each(prop, node->properties, list_node) { |
| 942 | if (!strcmp(prop->prop.name, name)) { |
| 943 | *data = prop->prop.data; |
| 944 | *size = prop->prop.size; |
| 945 | return; |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | /* |
| 951 | * Add a string property to a node, or update it if it already exists. |
| 952 | * |
| 953 | * @param node The device tree node to add to. |
| 954 | * @param name The name of the new property. |
| 955 | * @param str The zero-terminated string to be stored in the property. |
| 956 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 957 | void dt_add_string_prop(struct device_tree_node *node, const char *name, |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 958 | const char *str) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 959 | { |
Julius Werner | 0e9116f | 2019-05-13 17:30:31 -0700 | [diff] [blame] | 960 | dt_add_bin_prop(node, name, (char *)str, strlen(str) + 1); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | /* |
| 964 | * Add a 32-bit integer property to a node, or update it if it already exists. |
| 965 | * |
| 966 | * @param node The device tree node to add to. |
| 967 | * @param name The name of the new property. |
| 968 | * @param val The integer to be stored in the property. |
| 969 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 970 | void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 971 | { |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 972 | u32 *val_ptr = xmalloc(sizeof(val)); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 973 | *val_ptr = htobe32(val); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 974 | dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr)); |
| 975 | } |
| 976 | |
| 977 | /* |
Patrick Rudolph | 3fca4ed | 2018-08-10 10:12:35 +0200 | [diff] [blame] | 978 | * Add a 64-bit integer property to a node, or update it if it already exists. |
| 979 | * |
| 980 | * @param node The device tree node to add to. |
| 981 | * @param name The name of the new property. |
| 982 | * @param val The integer to be stored in the property. |
| 983 | */ |
| 984 | void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val) |
| 985 | { |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 986 | u64 *val_ptr = xmalloc(sizeof(val)); |
Patrick Rudolph | 3fca4ed | 2018-08-10 10:12:35 +0200 | [diff] [blame] | 987 | *val_ptr = htobe64(val); |
| 988 | dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr)); |
| 989 | } |
| 990 | |
| 991 | /* |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 992 | * Add a 'reg' address list property to a node, or update it if it exists. |
| 993 | * |
| 994 | * @param node The device tree node to add to. |
| 995 | * @param addrs Array of address values to be stored in the property. |
| 996 | * @param sizes Array of corresponding size values to 'addrs'. |
| 997 | * @param count Number of values in 'addrs' and 'sizes' (must be equal). |
| 998 | * @param addr_cells Value of #address-cells property valid for this node. |
| 999 | * @param size_cells Value of #size-cells property valid for this node. |
| 1000 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1001 | void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes, |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1002 | int count, u32 addr_cells, u32 size_cells) |
| 1003 | { |
| 1004 | int i; |
| 1005 | size_t length = (addr_cells + size_cells) * sizeof(u32) * count; |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 1006 | u8 *data = xmalloc(length); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1007 | u8 *cur = data; |
| 1008 | |
| 1009 | for (i = 0; i < count; i++) { |
| 1010 | dt_write_int(cur, addrs[i], addr_cells * sizeof(u32)); |
| 1011 | cur += addr_cells * sizeof(u32); |
| 1012 | dt_write_int(cur, sizes[i], size_cells * sizeof(u32)); |
| 1013 | cur += size_cells * sizeof(u32); |
| 1014 | } |
| 1015 | |
| 1016 | dt_add_bin_prop(node, "reg", data, length); |
| 1017 | } |
| 1018 | |
| 1019 | /* |
| 1020 | * Fixups to apply to a kernel's device tree before booting it. |
| 1021 | */ |
| 1022 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1023 | struct list_node device_tree_fixups; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1024 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1025 | int dt_apply_fixups(struct device_tree *tree) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1026 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1027 | struct device_tree_fixup *fixup; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1028 | list_for_each(fixup, device_tree_fixups, list_node) { |
| 1029 | assert(fixup->fixup); |
| 1030 | if (fixup->fixup(fixup, tree)) |
| 1031 | return 1; |
| 1032 | } |
| 1033 | return 0; |
| 1034 | } |
| 1035 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1036 | int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path, |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1037 | void *data, size_t data_size, int create) |
| 1038 | { |
| 1039 | char *path_copy, *prop_name; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1040 | struct device_tree_node *dt_node; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1041 | |
| 1042 | path_copy = strdup(path); |
| 1043 | |
| 1044 | if (!path_copy) { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1045 | printk(BIOS_ERR, "Failed to allocate a copy of path %s\n", |
| 1046 | path); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1047 | return 1; |
| 1048 | } |
| 1049 | |
| 1050 | prop_name = strrchr(path_copy, '/'); |
| 1051 | if (!prop_name) { |
Patrick Rudolph | 679d624 | 2018-07-11 13:53:04 +0200 | [diff] [blame] | 1052 | free(path_copy); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1053 | printk(BIOS_ERR, "Path %s does not include '/'\n", path); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1054 | return 1; |
| 1055 | } |
| 1056 | |
| 1057 | *prop_name++ = '\0'; /* Separate path from the property name. */ |
| 1058 | |
Julius Werner | f36d53c | 2019-05-03 18:23:34 -0700 | [diff] [blame] | 1059 | dt_node = dt_find_node_by_path(tree, path_copy, NULL, |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1060 | NULL, create); |
| 1061 | |
| 1062 | if (!dt_node) { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1063 | printk(BIOS_ERR, "Failed to %s %s in the device tree\n", |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1064 | create ? "create" : "find", path_copy); |
Patrick Rudolph | 679d624 | 2018-07-11 13:53:04 +0200 | [diff] [blame] | 1065 | free(path_copy); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1066 | return 1; |
| 1067 | } |
| 1068 | |
| 1069 | dt_add_bin_prop(dt_node, prop_name, data, data_size); |
Patrick Rudolph | 679d624 | 2018-07-11 13:53:04 +0200 | [diff] [blame] | 1070 | free(path_copy); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1071 | |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Prepare the /reserved-memory/ node. |
| 1077 | * |
| 1078 | * Technically, this can be called more than one time, to init and/or retrieve |
| 1079 | * the node. But dt_add_u32_prop() may leak a bit of memory if you do. |
| 1080 | * |
| 1081 | * @tree: Device tree to add/retrieve from. |
| 1082 | * @return: The /reserved-memory/ node (or NULL, if error). |
| 1083 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1084 | struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1085 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1086 | struct device_tree_node *reserved; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1087 | u32 addr = 0, size = 0; |
| 1088 | |
Julius Werner | fbec63d | 2019-05-03 18:29:28 -0700 | [diff] [blame] | 1089 | reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr, |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1090 | &size, 1); |
| 1091 | if (!reserved) |
| 1092 | return NULL; |
| 1093 | |
| 1094 | // Binding doc says this should have the same #{address,size}-cells as |
| 1095 | // the root. |
| 1096 | dt_add_u32_prop(reserved, "#address-cells", addr); |
| 1097 | dt_add_u32_prop(reserved, "#size-cells", size); |
| 1098 | // Binding doc says this should be empty (i.e., 1:1 mapping from root). |
| 1099 | dt_add_bin_prop(reserved, "ranges", NULL, 0); |
| 1100 | |
| 1101 | return reserved; |
| 1102 | } |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame^] | 1103 | |
| 1104 | /* |
| 1105 | * Increment a single phandle in prop at a given offset by a given adjustment. |
| 1106 | * |
| 1107 | * @param prop Property whose phandle should be adjusted. |
| 1108 | * @param adjustment Value that should be added to the existing phandle. |
| 1109 | * @param offset Byte offset of the phandle in the property data. |
| 1110 | * |
| 1111 | * @return New phandle value, or 0 on error. |
| 1112 | */ |
| 1113 | static uint32_t dt_adjust_phandle(struct device_tree_property *prop, |
| 1114 | uint32_t adjustment, uint32_t offset) |
| 1115 | { |
| 1116 | if (offset + 4 > prop->prop.size) |
| 1117 | return 0; |
| 1118 | |
| 1119 | uint32_t phandle = be32dec(prop->prop.data + offset); |
| 1120 | if (phandle == 0 || |
| 1121 | phandle == FDT_PHANDLE_ILLEGAL || |
| 1122 | phandle == 0xffffffff) |
| 1123 | return 0; |
| 1124 | |
| 1125 | phandle += adjustment; |
| 1126 | if (phandle >= FDT_PHANDLE_ILLEGAL) |
| 1127 | return 0; |
| 1128 | |
| 1129 | be32enc(prop->prop.data + offset, phandle); |
| 1130 | return phandle; |
| 1131 | } |
| 1132 | |
| 1133 | /* |
| 1134 | * Adjust all phandles in subtree by adding a new base offset. |
| 1135 | * |
| 1136 | * @param node Root node of the subtree to work on. |
| 1137 | * @param base New phandle base to be added to all phandles. |
| 1138 | * |
| 1139 | * @return New highest phandle in the subtree, or 0 on error. |
| 1140 | */ |
| 1141 | static uint32_t dt_adjust_all_phandles(struct device_tree_node *node, |
| 1142 | uint32_t base) |
| 1143 | { |
| 1144 | uint32_t new_max = MAX(base, 1); // make sure we don't return 0 |
| 1145 | struct device_tree_property *prop; |
| 1146 | struct device_tree_node *child; |
| 1147 | |
| 1148 | if (!node) |
| 1149 | return new_max; |
| 1150 | |
| 1151 | list_for_each(prop, node->properties, list_node) |
| 1152 | if (dt_prop_is_phandle(prop)) { |
| 1153 | node->phandle = dt_adjust_phandle(prop, base, 0); |
| 1154 | if (!node->phandle) |
| 1155 | return 0; |
| 1156 | new_max = MAX(new_max, node->phandle); |
| 1157 | } // no break -- can have more than one phandle prop |
| 1158 | |
| 1159 | list_for_each(child, node->children, list_node) |
| 1160 | new_max = MAX(new_max, dt_adjust_all_phandles(child, base)); |
| 1161 | |
| 1162 | return new_max; |
| 1163 | } |
| 1164 | |
| 1165 | /* |
| 1166 | * Apply a /__local_fixup__ subtree to the corresponding overlay subtree. |
| 1167 | * |
| 1168 | * @param node Root node of the overlay subtree to fix up. |
| 1169 | * @param node Root node of the /__local_fixup__ subtree. |
| 1170 | * @param base Adjustment that was added to phandles in the overlay. |
| 1171 | * |
| 1172 | * @return 0 on success, -1 on error. |
| 1173 | */ |
| 1174 | static int dt_fixup_locals(struct device_tree_node *node, |
| 1175 | struct device_tree_node *fixup, uint32_t base) |
| 1176 | { |
| 1177 | struct device_tree_property *prop; |
| 1178 | struct device_tree_property *fixup_prop; |
| 1179 | struct device_tree_node *child; |
| 1180 | struct device_tree_node *fixup_child; |
| 1181 | int i; |
| 1182 | |
| 1183 | // For local fixups the /__local_fixup__ subtree contains the same node |
| 1184 | // hierarchy as the main tree we're fixing up. Each property contains |
| 1185 | // the fixup offsets for the respective property in the main tree. For |
| 1186 | // each property in the fixup node, find the corresponding property in |
| 1187 | // the base node and apply fixups to all offsets it specifies. |
| 1188 | list_for_each(fixup_prop, fixup->properties, list_node) { |
| 1189 | struct device_tree_property *base_prop = NULL; |
| 1190 | list_for_each(prop, node->properties, list_node) |
| 1191 | if (!strcmp(prop->prop.name, fixup_prop->prop.name)) { |
| 1192 | base_prop = prop; |
| 1193 | break; |
| 1194 | } |
| 1195 | |
| 1196 | // We should always find a corresponding base prop for a fixup, |
| 1197 | // and fixup props contain a list of 32-bit fixup offsets. |
| 1198 | if (!base_prop || fixup_prop->prop.size % sizeof(uint32_t)) |
| 1199 | return -1; |
| 1200 | |
| 1201 | for (i = 0; i < fixup_prop->prop.size; i += sizeof(uint32_t)) |
| 1202 | if (!dt_adjust_phandle(base_prop, base, be32dec( |
| 1203 | fixup_prop->prop.data + i))) |
| 1204 | return -1; |
| 1205 | } |
| 1206 | |
| 1207 | // Now recursively descend both the base tree and the /__local_fixups__ |
| 1208 | // subtree in sync to apply all fixups. |
| 1209 | list_for_each(fixup_child, fixup->children, list_node) { |
| 1210 | struct device_tree_node *base_child = NULL; |
| 1211 | list_for_each(child, node->children, list_node) |
| 1212 | if (!strcmp(child->name, fixup_child->name)) { |
| 1213 | base_child = child; |
| 1214 | break; |
| 1215 | } |
| 1216 | |
| 1217 | // All fixup nodes should have a corresponding base node. |
| 1218 | if (!base_child) |
| 1219 | return -1; |
| 1220 | |
| 1221 | if (dt_fixup_locals(base_child, fixup_child, base) < 0) |
| 1222 | return -1; |
| 1223 | } |
| 1224 | |
| 1225 | return 0; |
| 1226 | } |
| 1227 | |
| 1228 | /* |
| 1229 | * Update all /__symbols__ properties in an overlay that start with |
| 1230 | * "/fragment@X/__overlay__" with corresponding path prefix in the base tree. |
| 1231 | * |
| 1232 | * @param symbols /__symbols__ done to update. |
| 1233 | * @param fragment /fragment@X node that references to should be updated. |
| 1234 | * @param base_path Path of base tree node that the fragment overlaid. |
| 1235 | */ |
| 1236 | static void dt_fix_symbols(struct device_tree_node *symbols, |
| 1237 | struct device_tree_node *fragment, |
| 1238 | const char *base_path) |
| 1239 | { |
| 1240 | struct device_tree_property *prop; |
| 1241 | char buf[512]; // Should be enough for maximum DT path length? |
| 1242 | char node_path[64]; // easily enough for /fragment@XXXX/__overlay__ |
| 1243 | |
| 1244 | if (!symbols) // If the overlay has no /__symbols__ node, we're done! |
| 1245 | return; |
| 1246 | |
| 1247 | int len = snprintf(node_path, sizeof(node_path), "/%s/__overlay__", |
| 1248 | fragment->name); |
| 1249 | |
| 1250 | list_for_each(prop, symbols->properties, list_node) |
| 1251 | if (!strncmp(prop->prop.data, node_path, len)) { |
| 1252 | prop->prop.size = snprintf(buf, sizeof(buf), "%s%s", |
| 1253 | base_path, (char *)prop->prop.data + len) + 1; |
| 1254 | free(prop->prop.data); |
| 1255 | prop->prop.data = strdup(buf); |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | /* |
| 1260 | * Fix up overlay according to a property in /__fixup__. If the fixed property |
| 1261 | * is a /fragment@X:target, also update /__symbols__ references to fragment. |
| 1262 | * |
| 1263 | * @params overlay Overlay to fix up. |
| 1264 | * @params fixup /__fixup__ property. |
| 1265 | * @params phandle phandle value to insert where the fixup points to. |
| 1266 | * @params base_path Path to the base DT node that the fixup points to. |
| 1267 | * @params overlay_symbols /__symbols__ node of the overlay. |
| 1268 | * |
| 1269 | * @return 0 on success, -1 on error. |
| 1270 | */ |
| 1271 | static int dt_fixup_external(struct device_tree *overlay, |
| 1272 | struct device_tree_property *fixup, |
| 1273 | uint32_t phandle, const char *base_path, |
| 1274 | struct device_tree_node *overlay_symbols) |
| 1275 | { |
| 1276 | struct device_tree_property *prop; |
| 1277 | |
| 1278 | // External fixup properties are encoded as "<path>:<prop>:<offset>". |
| 1279 | char *entry = fixup->prop.data; |
| 1280 | while ((void *)entry < fixup->prop.data + fixup->prop.size) { |
| 1281 | // okay to destroy fixup property value, won't be needed again |
| 1282 | char *node_path = entry; |
| 1283 | entry = strchr(node_path, ':'); |
| 1284 | if (!entry) |
| 1285 | return -1; |
| 1286 | *entry++ = '\0'; |
| 1287 | |
| 1288 | char *prop_name = entry; |
| 1289 | entry = strchr(prop_name, ':'); |
| 1290 | if (!entry) |
| 1291 | return -1; |
| 1292 | *entry++ = '\0'; |
| 1293 | |
| 1294 | struct device_tree_node *ovl_node = dt_find_node_by_path( |
| 1295 | overlay, node_path, NULL, NULL, 0); |
| 1296 | if (!ovl_node || !isdigit(*entry)) |
| 1297 | return -1; |
| 1298 | |
| 1299 | struct device_tree_property *ovl_prop = NULL; |
| 1300 | list_for_each(prop, ovl_node->properties, list_node) |
| 1301 | if (!strcmp(prop->prop.name, prop_name)) { |
| 1302 | ovl_prop = prop; |
| 1303 | break; |
| 1304 | } |
| 1305 | |
| 1306 | // Move entry to first char after number, must be a '\0'. |
| 1307 | uint32_t offset = skip_atoi(&entry); |
| 1308 | if (!ovl_prop || offset + 4 > ovl_prop->prop.size || entry[0]) |
| 1309 | return -1; |
| 1310 | entry++; // jump over '\0' to potential next fixup |
| 1311 | |
| 1312 | be32enc(ovl_prop->prop.data + offset, phandle); |
| 1313 | |
| 1314 | // If this is a /fragment@X:target property, update |
| 1315 | // references to this fragment in the overlay __symbols__ now. |
| 1316 | if (offset == 0 && !strcmp(prop_name, "target") && |
| 1317 | !strchr(node_path + 1, '/')) // only toplevel nodes |
| 1318 | dt_fix_symbols(overlay_symbols, ovl_node, base_path); |
| 1319 | } |
| 1320 | |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
| 1324 | /* |
| 1325 | * Apply all /__fixup__ properties in the overlay. This will destroy the |
| 1326 | * property data in /__fixup__ and it should not be accessed again. |
| 1327 | * |
| 1328 | * @params tree Base device tree that the overlay updates. |
| 1329 | * @params symbols /__symbols__ node of the base device tree. |
| 1330 | * @params overlay Overlay to fix up. |
| 1331 | * @params fixups /__fixup__ node in the overlay. |
| 1332 | * @params overlay_symbols /__symbols__ node of the overlay. |
| 1333 | * |
| 1334 | * @return 0 on success, -1 on error. |
| 1335 | */ |
| 1336 | static int dt_fixup_all_externals(struct device_tree *tree, |
| 1337 | struct device_tree_node *symbols, |
| 1338 | struct device_tree *overlay, |
| 1339 | struct device_tree_node *fixups, |
| 1340 | struct device_tree_node *overlay_symbols) |
| 1341 | { |
| 1342 | struct device_tree_property *fix; |
| 1343 | |
| 1344 | // If we have any external fixups, the base tree must have /__symbols__. |
| 1345 | if (!symbols) |
| 1346 | return -1; |
| 1347 | |
| 1348 | // Unlike /__local_fixups__, /__fixups__ is not a whole subtree that |
| 1349 | // mirrors the node hierarchy. It's just a directory of fixup properties |
| 1350 | // that each directly contain all information necessary to apply them. |
| 1351 | list_for_each(fix, fixups->properties, list_node) { |
| 1352 | // The name of a fixup property is the label of the node we want |
| 1353 | // a property to phandle-reference. Look it up in /__symbols__. |
| 1354 | const char *path = dt_find_string_prop(symbols, fix->prop.name); |
| 1355 | if (!path) |
| 1356 | return -1; |
| 1357 | |
| 1358 | // Find the node the label pointed to to figure out its phandle. |
| 1359 | struct device_tree_node *node = dt_find_node_by_path(tree, path, |
| 1360 | NULL, NULL, 0); |
| 1361 | if (!node) |
| 1362 | return -1; |
| 1363 | |
| 1364 | // Write it into the overlay property(s) pointing to that node. |
| 1365 | if (dt_fixup_external(overlay, fix, node->phandle, |
| 1366 | path, overlay_symbols) < 0) |
| 1367 | return -1; |
| 1368 | } |
| 1369 | |
| 1370 | return 0; |
| 1371 | } |
| 1372 | |
| 1373 | /* |
| 1374 | * Copy all nodes and properties from one DT subtree into another. This is a |
| 1375 | * shallow copy so both trees will point to the same property data afterwards. |
| 1376 | * |
| 1377 | * @params dst Destination subtree to copy into. |
| 1378 | * @params src Source subtree to copy from. |
| 1379 | * @params upd 1 to overwrite same-name properties, 0 to discard them. |
| 1380 | */ |
| 1381 | static void dt_copy_subtree(struct device_tree_node *dst, |
| 1382 | struct device_tree_node *src, int upd) |
| 1383 | { |
| 1384 | struct device_tree_property *prop; |
| 1385 | struct device_tree_property *src_prop; |
| 1386 | list_for_each(src_prop, src->properties, list_node) { |
| 1387 | if (dt_prop_is_phandle(src_prop) || |
| 1388 | !strcmp(src_prop->prop.name, "name")) { |
| 1389 | printk(BIOS_DEBUG, |
| 1390 | "WARNING: ignoring illegal overlay prop '%s'\n", |
| 1391 | src_prop->prop.name); |
| 1392 | continue; |
| 1393 | } |
| 1394 | |
| 1395 | struct device_tree_property *dst_prop = NULL; |
| 1396 | list_for_each(prop, dst->properties, list_node) |
| 1397 | if (!strcmp(prop->prop.name, src_prop->prop.name)) { |
| 1398 | dst_prop = prop; |
| 1399 | break; |
| 1400 | } |
| 1401 | |
| 1402 | if (dst_prop) { |
| 1403 | if (!upd) { |
| 1404 | printk(BIOS_DEBUG, |
| 1405 | "WARNING: ignoring prop update '%s'\n", |
| 1406 | src_prop->prop.name); |
| 1407 | continue; |
| 1408 | } |
| 1409 | } else { |
| 1410 | dst_prop = xzalloc(sizeof(*dst_prop)); |
| 1411 | list_insert_after(&dst_prop->list_node, |
| 1412 | &dst->properties); |
| 1413 | } |
| 1414 | |
| 1415 | dst_prop->prop = src_prop->prop; |
| 1416 | } |
| 1417 | |
| 1418 | struct device_tree_node *node; |
| 1419 | struct device_tree_node *src_node; |
| 1420 | list_for_each(src_node, src->children, list_node) { |
| 1421 | struct device_tree_node *dst_node = NULL; |
| 1422 | list_for_each(node, dst->children, list_node) |
| 1423 | if (!strcmp(node->name, src_node->name)) { |
| 1424 | dst_node = node; |
| 1425 | break; |
| 1426 | } |
| 1427 | |
| 1428 | if (!dst_node) { |
| 1429 | dst_node = xzalloc(sizeof(*dst_node)); |
| 1430 | *dst_node = *src_node; |
| 1431 | list_insert_after(&dst_node->list_node, &dst->children); |
| 1432 | } else { |
| 1433 | dt_copy_subtree(dst_node, src_node, upd); |
| 1434 | } |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | /* |
| 1439 | * Apply an overlay /fragment@X node to a base device tree. |
| 1440 | * |
| 1441 | * @param tree Base device tree. |
| 1442 | * @param fragment /fragment@X node. |
| 1443 | * @params overlay_symbols /__symbols__ node of the overlay. |
| 1444 | * |
| 1445 | * @return 0 on success, -1 on error. |
| 1446 | */ |
| 1447 | static int dt_import_fragment(struct device_tree *tree, |
| 1448 | struct device_tree_node *fragment, |
| 1449 | struct device_tree_node *overlay_symbols) |
| 1450 | { |
| 1451 | // The actually overlaid nodes/props are in an __overlay__ child node. |
| 1452 | static const char *overlay_path[] = { "__overlay__", NULL }; |
| 1453 | struct device_tree_node *overlay = dt_find_node(fragment, overlay_path, |
| 1454 | NULL, NULL, 0); |
| 1455 | |
| 1456 | // If it doesn't have an __overlay__ child, it's not a fragment. |
| 1457 | if (!overlay) |
| 1458 | return 0; |
| 1459 | |
| 1460 | // The target node of the fragment can be given by path or by phandle. |
| 1461 | struct device_tree_property *prop; |
| 1462 | struct device_tree_property *phandle = NULL; |
| 1463 | struct device_tree_property *path = NULL; |
| 1464 | list_for_each(prop, fragment->properties, list_node) { |
| 1465 | if (!strcmp(prop->prop.name, "target")) { |
| 1466 | phandle = prop; |
| 1467 | break; // phandle target has priority, stop looking here |
| 1468 | } |
| 1469 | if (!strcmp(prop->prop.name, "target-path")) |
| 1470 | path = prop; |
| 1471 | } |
| 1472 | |
| 1473 | struct device_tree_node *target = NULL; |
| 1474 | if (phandle) { |
| 1475 | if (phandle->prop.size != sizeof(uint32_t)) |
| 1476 | return -1; |
| 1477 | target = dt_find_node_by_phandle(tree->root, |
| 1478 | be32dec(phandle->prop.data)); |
| 1479 | // Symbols already updated as part of dt_fixup_external(target). |
| 1480 | } else if (path) { |
| 1481 | target = dt_find_node_by_path(tree, path->prop.data, |
| 1482 | NULL, NULL, 0); |
| 1483 | dt_fix_symbols(overlay_symbols, fragment, path->prop.data); |
| 1484 | } |
| 1485 | if (!target) |
| 1486 | return -1; |
| 1487 | |
| 1488 | dt_copy_subtree(target, overlay, 1); |
| 1489 | return 0; |
| 1490 | } |
| 1491 | |
| 1492 | /* |
| 1493 | * Apply a device tree overlay to a base device tree. This will |
| 1494 | * destroy/incorporate the overlay data, so it should not be freed or reused. |
| 1495 | * See dtc.git/Documentation/dt-object-internal.txt for overlay format details. |
| 1496 | * |
| 1497 | * @param tree Unflattened base device tree to add the overlay into. |
| 1498 | * @param overlay Unflattened overlay device tree to apply to the base. |
| 1499 | * |
| 1500 | * @return 0 on success, -1 on error. |
| 1501 | */ |
| 1502 | int dt_apply_overlay(struct device_tree *tree, struct device_tree *overlay) |
| 1503 | { |
| 1504 | // First, we need to make sure phandles inside the overlay don't clash |
| 1505 | // with those in the base tree. We just define the highest phandle value |
| 1506 | // in the base tree as the "phandle offset" for this overlay and |
| 1507 | // increment all phandles in it by that value. |
| 1508 | uint32_t phandle_base = tree->max_phandle; |
| 1509 | uint32_t new_max = dt_adjust_all_phandles(overlay->root, phandle_base); |
| 1510 | if (!new_max) { |
| 1511 | printk(BIOS_DEBUG, "ERROR: invalid phandles in overlay\n"); |
| 1512 | return -1; |
| 1513 | } |
| 1514 | tree->max_phandle = new_max; |
| 1515 | |
| 1516 | // Now that we changed phandles in the overlay, we need to update any |
| 1517 | // nodes referring to them. Those are listed in /__local_fixups__. |
| 1518 | struct device_tree_node *local_fixups = dt_find_node_by_path(overlay, |
| 1519 | "/__local_fixups__", NULL, NULL, 0); |
| 1520 | if (local_fixups && dt_fixup_locals(overlay->root, local_fixups, |
| 1521 | phandle_base) < 0) { |
| 1522 | printk(BIOS_DEBUG, "ERROR: invalid local fixups in overlay\n"); |
| 1523 | return -1; |
| 1524 | } |
| 1525 | |
| 1526 | // Besides local phandle references (from nodes within the overlay to |
| 1527 | // other nodes within the overlay), the overlay may also contain phandle |
| 1528 | // references to the base tree. These are stored with invalid values and |
| 1529 | // must be updated now. /__symbols__ contains a list of all labels in |
| 1530 | // the base tree, and /__fixups__ describes all nodes in the overlay |
| 1531 | // that contain external phandle references. |
| 1532 | // We also take this opportunity to update all /fragment@X/__overlay__/ |
| 1533 | // prefixes in the overlay's /__symbols__ node to the correct path that |
| 1534 | // the fragment will be placed in later, since this is the only step |
| 1535 | // where we have all necessary information for that easily available. |
| 1536 | struct device_tree_node *symbols = dt_find_node_by_path(tree, |
| 1537 | "/__symbols__", NULL, NULL, 0); |
| 1538 | struct device_tree_node *fixups = dt_find_node_by_path(overlay, |
| 1539 | "/__fixups__", NULL, NULL, 0); |
| 1540 | struct device_tree_node *overlay_symbols = dt_find_node_by_path(overlay, |
| 1541 | "/__symbols__", NULL, NULL, 0); |
| 1542 | if (fixups && dt_fixup_all_externals(tree, symbols, overlay, |
| 1543 | fixups, overlay_symbols) < 0) { |
| 1544 | printk(BIOS_DEBUG, |
| 1545 | "ERROR: cannot match external fixups from overlay\n"); |
| 1546 | return -1; |
| 1547 | } |
| 1548 | |
| 1549 | // After all this fixing up, we can finally merge the overlay into the |
| 1550 | // tree (one fragment at a time, because for some reason it's split up). |
| 1551 | struct device_tree_node *fragment; |
| 1552 | list_for_each(fragment, overlay->root->children, list_node) |
| 1553 | if (dt_import_fragment(tree, fragment, overlay_symbols) < 0) { |
| 1554 | printk(BIOS_DEBUG, "ERROR: bad DT fragment '%s'\n", |
| 1555 | fragment->name); |
| 1556 | return -1; |
| 1557 | } |
| 1558 | |
| 1559 | // We need to also update /__symbols__ to include labels from this |
| 1560 | // overlay, in case we want to load further overlays with external |
| 1561 | // phandle references to it. If the base tree already has a /__symbols__ |
| 1562 | // we merge them together, otherwise we just insert the overlay's |
| 1563 | // /__symbols__ node into the base tree root. |
| 1564 | if (overlay_symbols) { |
| 1565 | if (symbols) |
| 1566 | dt_copy_subtree(symbols, overlay_symbols, 0); |
| 1567 | else |
| 1568 | list_insert_after(&overlay_symbols->list_node, |
| 1569 | &tree->root->children); |
| 1570 | } |
| 1571 | |
| 1572 | return 0; |
| 1573 | } |