Patrick Georgi | afd4c87 | 2020-05-05 23:43:18 +0200 | [diff] [blame] | 1 | /* Taken from depthcharge: src/base/device_tree.c */ |
Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 2 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 3 | |
| 4 | #include <assert.h> |
Maximilian Brune | da336cd | 2023-09-16 20:08:41 +0200 | [diff] [blame] | 5 | #include <commonlib/device_tree.h> |
Joel Kitching | 393c71c | 2019-06-16 16:09:42 +0800 | [diff] [blame] | 6 | #include <ctype.h> |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 7 | #include <endian.h> |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 8 | #include <stdbool.h> |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 9 | #include <stdint.h> |
Maximilian Brune | da336cd | 2023-09-16 20:08:41 +0200 | [diff] [blame] | 10 | #ifdef __COREBOOT__ |
| 11 | #include <console/console.h> |
| 12 | #else |
| 13 | #include <stdio.h> |
| 14 | #define printk(level, ...) printf(__VA_ARGS__) |
| 15 | #endif |
Elyes Haouas | bdd03c2 | 2024-05-27 11:20:07 +0200 | [diff] [blame] | 16 | #include <stdio.h> |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 17 | #include <string.h> |
| 18 | #include <stddef.h> |
| 19 | #include <stdlib.h> |
Alper Nebi Yasak | 377157c | 2024-02-05 17:31:20 +0300 | [diff] [blame] | 20 | #include <limits.h> |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 21 | |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 22 | #define FDT_PATH_MAX_DEPTH 10 // should be a good enough upper bound |
| 23 | #define FDT_PATH_MAX_LEN 128 // should be a good enough upper bound |
Alper Nebi Yasak | 377157c | 2024-02-05 17:31:20 +0300 | [diff] [blame] | 24 | #define FDT_MAX_MEMORY_NODES 4 // should be a good enough upper bound |
| 25 | #define FDT_MAX_MEMORY_REGIONS 16 // should be a good enough upper bound |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 26 | |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 27 | /* |
| 28 | * Functions for picking apart flattened trees. |
| 29 | */ |
| 30 | |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 31 | static int fdt_skip_nops(const void *blob, uint32_t offset) |
| 32 | { |
| 33 | uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset); |
| 34 | |
| 35 | int index = 0; |
| 36 | while (be32toh(ptr[index]) == FDT_TOKEN_NOP) |
| 37 | index++; |
| 38 | |
| 39 | return index * sizeof(uint32_t); |
| 40 | } |
| 41 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 42 | int fdt_next_property(const void *blob, uint32_t offset, |
| 43 | struct fdt_property *prop) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 44 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 45 | struct fdt_header *header = (struct fdt_header *)blob; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 46 | uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset); |
| 47 | |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 48 | // skip NOP tokens |
| 49 | offset += fdt_skip_nops(blob, offset); |
| 50 | |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 51 | int index = 0; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 52 | if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 53 | return 0; |
| 54 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 55 | uint32_t size = be32toh(ptr[index++]); |
| 56 | uint32_t name_offset = be32toh(ptr[index++]); |
| 57 | name_offset += be32toh(header->strings_offset); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 58 | |
| 59 | if (prop) { |
| 60 | prop->name = (char *)((uint8_t *)blob + name_offset); |
| 61 | prop->data = &ptr[index]; |
| 62 | prop->size = size; |
| 63 | } |
| 64 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 65 | index += DIV_ROUND_UP(size, sizeof(uint32_t)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 66 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 67 | return index * sizeof(uint32_t); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 70 | /* |
| 71 | * fdt_next_node_name reads a node name |
| 72 | * |
| 73 | * @params blob address of FDT |
| 74 | * @params offset offset to the node to read the name from |
| 75 | * @params name parameter to hold the name that has been read or NULL |
| 76 | * |
| 77 | * @returns Either 0 on error or offset to the properties that come after the node name |
| 78 | */ |
| 79 | int fdt_next_node_name(const void *blob, uint32_t offset, const char **name) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 80 | { |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 81 | // skip NOP tokens |
| 82 | offset += fdt_skip_nops(blob, offset); |
| 83 | |
| 84 | char *ptr = ((char *)blob) + offset; |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 85 | if (be32dec(ptr) != FDT_TOKEN_BEGIN_NODE) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 86 | return 0; |
| 87 | |
| 88 | ptr += 4; |
| 89 | if (name) |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 90 | *name = ptr; |
| 91 | |
| 92 | return ALIGN_UP(strlen(ptr) + 1, 4) + 4; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 95 | /* |
| 96 | * A utility function to skip past nodes in flattened trees. |
| 97 | */ |
| 98 | int fdt_skip_node(const void *blob, uint32_t start_offset) |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 99 | { |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 100 | uint32_t offset = start_offset; |
| 101 | |
| 102 | const char *name; |
| 103 | int size = fdt_next_node_name(blob, offset, &name); |
| 104 | if (!size) |
| 105 | return 0; |
| 106 | offset += size; |
| 107 | |
| 108 | while ((size = fdt_next_property(blob, offset, NULL))) |
| 109 | offset += size; |
| 110 | |
| 111 | while ((size = fdt_skip_node(blob, offset))) |
| 112 | offset += size; |
| 113 | |
| 114 | // skip NOP tokens |
| 115 | offset += fdt_skip_nops(blob, offset); |
| 116 | |
| 117 | return offset - start_offset + sizeof(uint32_t); |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 120 | /* |
| 121 | * fdt_read_prop reads a property inside a node |
| 122 | * |
| 123 | * @params blob address of FDT |
| 124 | * @params node_offset offset to the node to read the property from |
| 125 | * @params prop_name name of the property to read |
| 126 | * @params fdt_prop property is saved inside this parameter |
| 127 | * |
| 128 | * @returns Either 0 if no property has been found or an offset that points to the location |
| 129 | * of the property |
| 130 | */ |
| 131 | u32 fdt_read_prop(const void *blob, u32 node_offset, const char *prop_name, |
| 132 | struct fdt_property *fdt_prop) |
| 133 | { |
| 134 | u32 offset = node_offset; |
| 135 | |
| 136 | offset += fdt_next_node_name(blob, offset, NULL); // skip node name |
| 137 | |
| 138 | size_t size; |
| 139 | while ((size = fdt_next_property(blob, offset, fdt_prop))) { |
| 140 | if (strcmp(fdt_prop->name, prop_name) == 0) |
| 141 | return offset; |
| 142 | offset += size; |
| 143 | } |
| 144 | return 0; // property not found |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * fdt_read_reg_prop reads the reg property inside a node |
| 149 | * |
| 150 | * @params blob address of FDT |
| 151 | * @params node_offset offset to the node to read the reg property from |
| 152 | * @params addr_cells number of cells used for one address |
| 153 | * @params size_cells number of cells used for one size |
| 154 | * @params regions all regions that are read inside the reg property are saved inside |
| 155 | * this array |
| 156 | * @params regions_count maximum number of entries that can be saved inside the regions array. |
| 157 | * |
| 158 | * Returns: Either 0 on error or returns the number of regions put into the regions array. |
| 159 | */ |
| 160 | u32 fdt_read_reg_prop(const void *blob, u32 node_offset, u32 addr_cells, u32 size_cells, |
| 161 | struct device_tree_region regions[], size_t regions_count) |
| 162 | { |
| 163 | struct fdt_property prop; |
| 164 | u32 offset = fdt_read_prop(blob, node_offset, "reg", &prop); |
| 165 | |
| 166 | if (!offset) { |
| 167 | printk(BIOS_DEBUG, "no reg property found in node_offset: %x\n", node_offset); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | // we found the reg property, now need to parse all regions in 'reg' |
| 172 | size_t count = prop.size / (4 * addr_cells + 4 * size_cells); |
| 173 | if (count > regions_count) { |
| 174 | printk(BIOS_ERR, "reg property at node_offset: %x has more entries (%zd) than regions array can hold (%zd)\n", node_offset, count, regions_count); |
| 175 | count = regions_count; |
| 176 | } |
| 177 | if (addr_cells > 2 || size_cells > 2) { |
| 178 | printk(BIOS_ERR, "addr_cells (%d) or size_cells (%d) bigger than 2\n", |
| 179 | addr_cells, size_cells); |
| 180 | return 0; |
| 181 | } |
| 182 | uint32_t *ptr = prop.data; |
| 183 | for (int i = 0; i < count; i++) { |
| 184 | if (addr_cells == 1) |
| 185 | regions[i].addr = be32dec(ptr); |
| 186 | else if (addr_cells == 2) |
| 187 | regions[i].addr = be64dec(ptr); |
| 188 | ptr += addr_cells; |
| 189 | if (size_cells == 1) |
| 190 | regions[i].size = be32dec(ptr); |
| 191 | else if (size_cells == 2) |
| 192 | regions[i].size = be64dec(ptr); |
| 193 | ptr += size_cells; |
| 194 | } |
| 195 | |
| 196 | return count; // return the number of regions found in the reg property |
| 197 | } |
| 198 | |
| 199 | static u32 fdt_read_cell_props(const void *blob, u32 node_offset, u32 *addrcp, u32 *sizecp) |
| 200 | { |
| 201 | struct fdt_property prop; |
| 202 | u32 offset = node_offset; |
| 203 | size_t size; |
| 204 | while ((size = fdt_next_property(blob, offset, &prop))) { |
| 205 | if (addrcp && !strcmp(prop.name, "#address-cells")) |
| 206 | *addrcp = be32dec(prop.data); |
| 207 | if (sizecp && !strcmp(prop.name, "#size-cells")) |
| 208 | *sizecp = be32dec(prop.data); |
| 209 | offset += size; |
| 210 | } |
| 211 | return offset; |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * fdt_find_node searches for a node relative to another node |
| 216 | * |
| 217 | * @params blob address of FDT |
| 218 | * |
| 219 | * @params parent_node_offset offset to node from which to traverse the tree |
| 220 | * |
| 221 | * @params path null terminated array of node names specifying a |
| 222 | * relative path (e.g: { "cpus", "cpu0", NULL }) |
| 223 | * |
| 224 | * @params addrcp/sizecp If any address-cells and size-cells properties are found that are |
| 225 | * part of the parent node of the node we are looking, addrcp and sizecp |
| 226 | * are set to these respectively. |
| 227 | * |
| 228 | * @returns: Either 0 if no node has been found or the offset to the node found |
| 229 | */ |
| 230 | static u32 fdt_find_node(const void *blob, u32 parent_node_offset, char **path, |
| 231 | u32 *addrcp, u32 *sizecp) |
| 232 | { |
| 233 | if (*path == NULL) |
| 234 | return parent_node_offset; // node found |
| 235 | |
| 236 | size_t size = fdt_next_node_name(blob, parent_node_offset, NULL); // skip node name |
| 237 | |
| 238 | /* |
| 239 | * get address-cells and size-cells properties while skipping the others. |
| 240 | * According to spec address-cells and size-cells are not inherited, but we |
| 241 | * intentionally follow the Linux implementation here and treat them as inheritable. |
| 242 | */ |
| 243 | u32 node_offset = fdt_read_cell_props(blob, parent_node_offset + size, addrcp, sizecp); |
| 244 | |
| 245 | const char *node_name; |
| 246 | // walk all children nodes |
| 247 | while ((size = fdt_next_node_name(blob, node_offset, &node_name))) { |
| 248 | if (!strcmp(*path, node_name)) { |
| 249 | // traverse one level deeper into the path |
| 250 | return fdt_find_node(blob, node_offset, path + 1, addrcp, sizecp); |
| 251 | } |
| 252 | // node is not the correct one. skip current node |
| 253 | node_offset += fdt_skip_node(blob, node_offset); |
| 254 | } |
| 255 | |
| 256 | // we have searched everything and could not find a fitting node |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * fdt_find_node_by_path finds a node behind a given node path |
| 262 | * |
| 263 | * @params blob address of FDT |
| 264 | * @params path absolute path to the node that should be searched for |
| 265 | * |
| 266 | * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells |
| 267 | * value found in the node of the node specified by node_offset. Either |
| 268 | * may be NULL to ignore. If no #address-cells and #size-cells is found |
| 269 | * default values of #address-cells=2 and #size-cells=1 are returned. |
| 270 | * |
| 271 | * @returns Either 0 on error or the offset to the node found behind the path |
| 272 | */ |
| 273 | u32 fdt_find_node_by_path(const void *blob, const char *path, u32 *addrcp, u32 *sizecp) |
| 274 | { |
| 275 | // sanity check |
| 276 | if (path[0] != '/') { |
| 277 | printk(BIOS_ERR, "devicetree path must start with a /\n"); |
| 278 | return 0; |
| 279 | } |
| 280 | if (!blob) { |
| 281 | printk(BIOS_ERR, "devicetree blob is NULL\n"); |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | if (addrcp) |
| 286 | *addrcp = 2; |
| 287 | if (sizecp) |
| 288 | *sizecp = 1; |
| 289 | |
| 290 | struct fdt_header *fdt_hdr = (struct fdt_header *)blob; |
| 291 | |
| 292 | /* |
| 293 | * split path into separate nodes |
| 294 | * e.g: "/cpus/cpu0" -> { "cpus", "cpu0" } |
| 295 | */ |
| 296 | char *path_array[FDT_PATH_MAX_DEPTH]; |
| 297 | size_t path_size = strlen(path); |
| 298 | assert(path_size < FDT_PATH_MAX_LEN); |
| 299 | char path_copy[FDT_PATH_MAX_LEN]; |
| 300 | memcpy(path_copy, path, path_size + 1); |
| 301 | char *cur = path_copy; |
| 302 | int i; |
| 303 | for (i = 0; i < FDT_PATH_MAX_DEPTH; i++) { |
| 304 | path_array[i] = strtok_r(NULL, "/", &cur); |
| 305 | if (!path_array[i]) |
| 306 | break; |
| 307 | } |
| 308 | assert(i < FDT_PATH_MAX_DEPTH); |
| 309 | |
| 310 | return fdt_find_node(blob, be32toh(fdt_hdr->structure_offset), path_array, addrcp, sizecp); |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * fdt_find_subnodes_by_prefix finds a node with a given prefix relative to a parent node |
| 315 | * |
| 316 | * @params blob The FDT to search. |
| 317 | * |
| 318 | * @params node_offset offset to the node of which the children should be searched |
| 319 | * |
| 320 | * @params prefix A string to search for a node with a given prefix. This can for example |
| 321 | * be 'cpu' to look for all nodes matching this prefix. Only children of |
| 322 | * node_offset are searched. Therefore in order to search all nodes matching |
| 323 | * the 'cpu' prefix, node_offset should probably point to the 'cpus' node. |
| 324 | * An empty prefix ("") searches for all children nodes of node_offset. |
| 325 | * |
| 326 | * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells |
| 327 | * value found in the node of the node specified by node_offset. Either |
| 328 | * may be NULL to ignore. If no #address-cells and #size-cells is found |
| 329 | * addrcp and sizecp are left untouched. |
| 330 | * |
| 331 | * @params results Array of offsets pointing to each node matching the given prefix. |
| 332 | * @params results_len Number of entries allocated for the 'results' array |
| 333 | * |
| 334 | * @returns offset to last node found behind path or 0 if no node has been found |
| 335 | */ |
| 336 | size_t fdt_find_subnodes_by_prefix(const void *blob, u32 node_offset, const char *prefix, |
| 337 | u32 *addrcp, u32 *sizecp, u32 *results, size_t results_len) |
| 338 | { |
| 339 | // sanity checks |
| 340 | if (!blob || !results || !prefix) { |
| 341 | printk(BIOS_ERR, "%s: input parameter cannot be null/\n", __func__); |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | u32 offset = node_offset; |
| 346 | |
| 347 | // we don't care about the name of the current node |
| 348 | u32 size = fdt_next_node_name(blob, offset, NULL); |
| 349 | if (!size) { |
| 350 | printk(BIOS_ERR, "%s: node_offset: %x does not point to a node\n", |
| 351 | __func__, node_offset); |
| 352 | return 0; |
| 353 | } |
| 354 | offset += size; |
| 355 | |
| 356 | /* |
| 357 | * update addrcp and sizecp if the node contains an address-cells and size-cells |
| 358 | * property. Otherwise use addrcp and sizecp provided by caller. |
| 359 | */ |
| 360 | offset = fdt_read_cell_props(blob, offset, addrcp, sizecp); |
| 361 | |
| 362 | size_t count_results = 0; |
| 363 | int prefix_len = strlen(prefix); |
| 364 | const char *node_name; |
| 365 | // walk all children nodes of offset |
| 366 | while ((size = fdt_next_node_name(blob, offset, &node_name))) { |
| 367 | |
| 368 | if (count_results >= results_len) { |
| 369 | printk(BIOS_WARNING, |
| 370 | "%s: results_len (%zd) smaller than count_results (%zd)\n", |
| 371 | __func__, results_len, count_results); |
| 372 | break; |
| 373 | } |
| 374 | |
| 375 | if (!strncmp(prefix, node_name, prefix_len)) { |
| 376 | // we found a node that matches the prefix |
| 377 | results[count_results++] = offset; |
| 378 | } |
| 379 | |
| 380 | // node does not match the prefix. skip current node |
| 381 | offset += fdt_skip_node(blob, offset); |
| 382 | } |
| 383 | |
| 384 | // return last occurrence |
| 385 | return count_results; |
| 386 | } |
| 387 | |
| 388 | static const char *fdt_read_alias_prop(const void *blob, const char *alias_name) |
| 389 | { |
| 390 | u32 node_offset = fdt_find_node_by_path(blob, "/aliases", NULL, NULL); |
| 391 | if (!node_offset) { |
| 392 | printk(BIOS_DEBUG, "no /aliases node found\n"); |
| 393 | return NULL; |
| 394 | } |
| 395 | struct fdt_property alias_prop; |
| 396 | if (!fdt_read_prop(blob, node_offset, alias_name, &alias_prop)) { |
| 397 | printk(BIOS_DEBUG, "property %s in /aliases node not found\n", alias_name); |
| 398 | return NULL; |
| 399 | } |
| 400 | return (const char *)alias_prop.data; |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Find a node in the tree from a string device tree path. |
| 405 | * |
| 406 | * @params blob Address to the FDT |
| 407 | * @params alias_name node name alias that should be searched for. |
| 408 | * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells |
| 409 | * value found in the node of the node specified by node_offset. Either |
| 410 | * may be NULL to ignore. If no #address-cells and #size-cells is found |
| 411 | * default values of #address-cells=2 and #size-cells=1 are returned. |
| 412 | * |
| 413 | * @returns offset to last node found behind path or 0 if no node has been found |
| 414 | */ |
| 415 | u32 fdt_find_node_by_alias(const void *blob, const char *alias_name, u32 *addrcp, u32 *sizecp) |
| 416 | { |
| 417 | const char *node_name = fdt_read_alias_prop(blob, alias_name); |
| 418 | if (!node_name) { |
| 419 | printk(BIOS_DEBUG, "alias %s not found\n", alias_name); |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | u32 node_offset = fdt_find_node_by_path(blob, node_name, addrcp, sizecp); |
| 424 | if (!node_offset) { |
| 425 | // This should not happen (invalid devicetree) |
| 426 | printk(BIOS_WARNING, |
| 427 | "Could not find node '%s', which alias was referring to '%s'\n", |
| 428 | node_name, alias_name); |
| 429 | return 0; |
| 430 | } |
| 431 | return node_offset; |
| 432 | } |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 433 | |
| 434 | |
| 435 | /* |
| 436 | * Functions for printing flattened trees. |
| 437 | */ |
| 438 | |
| 439 | static void print_indent(int depth) |
| 440 | { |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 441 | printk(BIOS_DEBUG, "%*s", depth * 8, ""); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 442 | } |
| 443 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 444 | static void print_property(const struct fdt_property *prop, int depth) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 445 | { |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 446 | int is_string = prop->size > 0 && |
| 447 | ((char *)prop->data)[prop->size - 1] == '\0'; |
| 448 | |
Maximilian Brune | 77eaec6 | 2023-09-20 05:12:04 +0200 | [diff] [blame] | 449 | if (is_string) { |
| 450 | for (int i = 0; i < prop->size - 1; i++) { |
| 451 | if (!isprint(((char *)prop->data)[i])) { |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 452 | is_string = 0; |
Maximilian Brune | 77eaec6 | 2023-09-20 05:12:04 +0200 | [diff] [blame] | 453 | break; |
| 454 | } |
| 455 | } |
| 456 | } |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 457 | |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 458 | print_indent(depth); |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 459 | if (is_string) { |
| 460 | printk(BIOS_DEBUG, "%s = \"%s\";\n", |
| 461 | prop->name, (const char *)prop->data); |
| 462 | } else { |
| 463 | printk(BIOS_DEBUG, "%s = < ", prop->name); |
| 464 | for (int i = 0; i < MIN(128, prop->size); i += 4) { |
| 465 | uint32_t val = 0; |
| 466 | for (int j = 0; j < MIN(4, prop->size - i); j++) |
| 467 | val |= ((uint8_t *)prop->data)[i + j] << |
| 468 | (24 - j * 8); |
| 469 | printk(BIOS_DEBUG, "%#.2x ", val); |
| 470 | } |
| 471 | if (prop->size > 128) |
| 472 | printk(BIOS_DEBUG, "..."); |
| 473 | printk(BIOS_DEBUG, ">;\n"); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 474 | } |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 475 | } |
| 476 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 477 | 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] | 478 | { |
| 479 | int offset = start_offset; |
| 480 | const char *name; |
| 481 | int size; |
| 482 | |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 483 | size = fdt_next_node_name(blob, offset, &name); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 484 | if (!size) |
| 485 | return 0; |
| 486 | offset += size; |
| 487 | |
| 488 | print_indent(depth); |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 489 | printk(BIOS_DEBUG, "%s {\n", name); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 490 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 491 | struct fdt_property prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 492 | while ((size = fdt_next_property(blob, offset, &prop))) { |
| 493 | print_property(&prop, depth + 1); |
| 494 | |
| 495 | offset += size; |
| 496 | } |
| 497 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 498 | printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */ |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 499 | |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 500 | while ((size = print_flat_node(blob, offset, depth + 1))) |
| 501 | offset += size; |
| 502 | |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 503 | print_indent(depth); |
| 504 | printk(BIOS_DEBUG, "}\n"); |
| 505 | |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 506 | return offset - start_offset + sizeof(uint32_t); |
| 507 | } |
| 508 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 509 | void fdt_print_node(const void *blob, uint32_t offset) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 510 | { |
| 511 | print_flat_node(blob, offset, 0); |
| 512 | } |
| 513 | |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 514 | /* |
Alper Nebi Yasak | 377157c | 2024-02-05 17:31:20 +0300 | [diff] [blame] | 515 | * fdt_read_memory_regions finds memory ranges from a flat device-tree |
| 516 | * |
| 517 | * @params blob address of FDT |
| 518 | * @params regions all regions that are read inside the reg property of |
| 519 | * memory nodes are saved inside this array |
| 520 | * @params regions_count maximum number of entries that can be saved inside |
| 521 | * the regions array. |
| 522 | * |
| 523 | * Returns: Either 0 on error or returns the number of regions put into the regions array. |
| 524 | */ |
| 525 | size_t fdt_read_memory_regions(const void *blob, |
| 526 | struct device_tree_region regions[], |
| 527 | size_t regions_count) |
| 528 | { |
| 529 | u32 node, root, addrcp, sizecp; |
| 530 | u32 nodes[FDT_MAX_MEMORY_NODES] = {0}; |
| 531 | size_t region_idx = 0; |
| 532 | size_t node_count = 0; |
| 533 | |
| 534 | if (!fdt_is_valid(blob)) |
| 535 | return 0; |
| 536 | |
| 537 | node = fdt_find_node_by_path(blob, "/memory", &addrcp, &sizecp); |
| 538 | if (node) { |
| 539 | region_idx += fdt_read_reg_prop(blob, node, addrcp, sizecp, |
| 540 | regions, regions_count); |
| 541 | if (region_idx >= regions_count) { |
| 542 | printk(BIOS_WARNING, "FDT: Too many memory regions\n"); |
| 543 | goto out; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | root = fdt_find_node_by_path(blob, "/", &addrcp, &sizecp); |
| 548 | node_count = fdt_find_subnodes_by_prefix(blob, root, "memory@", |
| 549 | &addrcp, &sizecp, nodes, |
| 550 | FDT_MAX_MEMORY_NODES); |
| 551 | if (node_count >= FDT_MAX_MEMORY_NODES) { |
| 552 | printk(BIOS_WARNING, "FDT: Too many memory nodes\n"); |
| 553 | /* Can still reading the regions for those we got */ |
| 554 | } |
| 555 | |
| 556 | for (size_t i = 0; i < MIN(node_count, FDT_MAX_MEMORY_NODES); i++) { |
| 557 | region_idx += fdt_read_reg_prop(blob, nodes[i], addrcp, sizecp, |
| 558 | ®ions[region_idx], |
| 559 | regions_count - region_idx); |
| 560 | if (region_idx >= regions_count) { |
| 561 | printk(BIOS_WARNING, "FDT: Too many memory regions\n"); |
| 562 | goto out; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | out: |
| 567 | for (size_t i = 0; i < MIN(region_idx, regions_count); i++) { |
| 568 | printk(BIOS_DEBUG, "FDT: Memory region [%#llx - %#llx]\n", |
| 569 | regions[i].addr, regions[i].addr + regions[i].size); |
| 570 | } |
| 571 | |
| 572 | return region_idx; |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | * fdt_get_memory_top finds top of memory from a flat device-tree |
| 577 | * |
| 578 | * @params blob address of FDT |
| 579 | * |
| 580 | * Returns: Either 0 on error or returns the maximum memory address |
| 581 | */ |
| 582 | uint64_t fdt_get_memory_top(const void *blob) |
| 583 | { |
| 584 | struct device_tree_region regions[FDT_MAX_MEMORY_REGIONS] = {0}; |
| 585 | uint64_t top = 0; |
| 586 | uint64_t total = 0; |
| 587 | size_t count; |
| 588 | |
| 589 | if (!fdt_is_valid(blob)) |
| 590 | return 0; |
| 591 | |
| 592 | count = fdt_read_memory_regions(blob, regions, FDT_MAX_MEMORY_REGIONS); |
| 593 | for (size_t i = 0; i < MIN(count, FDT_MAX_MEMORY_REGIONS); i++) { |
| 594 | top = MAX(top, regions[i].addr + regions[i].size); |
| 595 | total += regions[i].size; |
| 596 | } |
| 597 | |
| 598 | printk(BIOS_DEBUG, "FDT: Found %u MiB of RAM\n", |
| 599 | (uint32_t)(total / MiB)); |
| 600 | |
| 601 | return top; |
| 602 | } |
| 603 | |
| 604 | /* |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 605 | * Functions to turn a flattened tree into an unflattened one. |
| 606 | */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 607 | |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 608 | static int dt_prop_is_phandle(struct device_tree_property *prop) |
| 609 | { |
| 610 | return !(strcmp("phandle", prop->prop.name) && |
| 611 | strcmp("linux,phandle", prop->prop.name)); |
| 612 | } |
| 613 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 614 | static int fdt_unflatten_node(const void *blob, uint32_t start_offset, |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 615 | struct device_tree *tree, |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 616 | struct device_tree_node **new_node) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 617 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 618 | struct list_node *last; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 619 | int offset = start_offset; |
| 620 | const char *name; |
| 621 | int size; |
| 622 | |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 623 | size = fdt_next_node_name(blob, offset, &name); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 624 | if (!size) |
| 625 | return 0; |
| 626 | offset += size; |
| 627 | |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 628 | struct device_tree_node *node = xzalloc(sizeof(*node)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 629 | *new_node = node; |
| 630 | node->name = name; |
| 631 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 632 | struct fdt_property fprop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 633 | last = &node->properties; |
| 634 | while ((size = fdt_next_property(blob, offset, &fprop))) { |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 635 | struct device_tree_property *prop = xzalloc(sizeof(*prop)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 636 | prop->prop = fprop; |
| 637 | |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 638 | if (dt_prop_is_phandle(prop)) { |
| 639 | node->phandle = be32dec(prop->prop.data); |
| 640 | if (node->phandle > tree->max_phandle) |
| 641 | tree->max_phandle = node->phandle; |
| 642 | } |
| 643 | |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 644 | list_insert_after(&prop->list_node, last); |
| 645 | last = &prop->list_node; |
| 646 | |
| 647 | offset += size; |
| 648 | } |
| 649 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 650 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 651 | last = &node->children; |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 652 | while ((size = fdt_unflatten_node(blob, offset, tree, &child))) { |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 653 | list_insert_after(&child->list_node, last); |
| 654 | last = &child->list_node; |
| 655 | |
| 656 | offset += size; |
| 657 | } |
| 658 | |
| 659 | return offset - start_offset + sizeof(uint32_t); |
| 660 | } |
| 661 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 662 | static int fdt_unflatten_map_entry(const void *blob, uint32_t offset, |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 663 | struct device_tree_reserve_map_entry **new) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 664 | { |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 665 | const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset); |
| 666 | const uint64_t start = be64toh(ptr[0]); |
| 667 | const uint64_t size = be64toh(ptr[1]); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 668 | |
| 669 | if (!size) |
| 670 | return 0; |
| 671 | |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 672 | struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry)); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 673 | *new = entry; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 674 | entry->start = start; |
| 675 | entry->size = size; |
| 676 | |
| 677 | return sizeof(uint64_t) * 2; |
| 678 | } |
| 679 | |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 680 | bool fdt_is_valid(const void *blob) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 681 | { |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 682 | const struct fdt_header *header = (const struct fdt_header *)blob; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 683 | |
Julius Werner | 73eaec8 | 2019-05-03 17:58:07 -0700 | [diff] [blame] | 684 | uint32_t magic = be32toh(header->magic); |
| 685 | uint32_t version = be32toh(header->version); |
| 686 | uint32_t last_comp_version = be32toh(header->last_comp_version); |
| 687 | |
| 688 | if (magic != FDT_HEADER_MAGIC) { |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 689 | printk(BIOS_ERR, "Invalid device tree magic %#.8x!\n", magic); |
| 690 | return false; |
Julius Werner | 73eaec8 | 2019-05-03 17:58:07 -0700 | [diff] [blame] | 691 | } |
| 692 | if (last_comp_version > FDT_SUPPORTED_VERSION) { |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 693 | printk(BIOS_ERR, "Unsupported device tree version %u(>=%u)\n", |
Julius Werner | 73eaec8 | 2019-05-03 17:58:07 -0700 | [diff] [blame] | 694 | version, last_comp_version); |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 695 | return false; |
Julius Werner | 73eaec8 | 2019-05-03 17:58:07 -0700 | [diff] [blame] | 696 | } |
| 697 | if (version > FDT_SUPPORTED_VERSION) |
Elyes Haouas | d732628 | 2022-12-21 08:16:03 +0100 | [diff] [blame] | 698 | printk(BIOS_NOTICE, "FDT version %u too new, should add support!\n", |
Julius Werner | 73eaec8 | 2019-05-03 17:58:07 -0700 | [diff] [blame] | 699 | version); |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 700 | return true; |
| 701 | } |
| 702 | |
| 703 | struct device_tree *fdt_unflatten(const void *blob) |
| 704 | { |
| 705 | struct device_tree *tree = xzalloc(sizeof(*tree)); |
| 706 | const struct fdt_header *header = (const struct fdt_header *)blob; |
| 707 | tree->header = header; |
| 708 | |
Maximilian Brune | 6466354 | 2024-06-03 05:24:32 +0200 | [diff] [blame] | 709 | if (!fdt_is_valid(blob)) |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 710 | return NULL; |
Julius Werner | 73eaec8 | 2019-05-03 17:58:07 -0700 | [diff] [blame] | 711 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 712 | uint32_t struct_offset = be32toh(header->structure_offset); |
| 713 | uint32_t strings_offset = be32toh(header->strings_offset); |
| 714 | uint32_t reserve_offset = be32toh(header->reserve_map_offset); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 715 | uint32_t min_offset = 0; |
| 716 | min_offset = MIN(struct_offset, strings_offset); |
| 717 | min_offset = MIN(min_offset, reserve_offset); |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 718 | /* Assume everything up to the first non-header component is part of |
| 719 | the header and needs to be preserved. This will protect us against |
| 720 | new elements being added in the future. */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 721 | tree->header_size = min_offset; |
| 722 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 723 | struct device_tree_reserve_map_entry *entry; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 724 | uint32_t offset = reserve_offset; |
| 725 | int size; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 726 | struct list_node *last = &tree->reserve_map; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 727 | while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) { |
| 728 | list_insert_after(&entry->list_node, last); |
| 729 | last = &entry->list_node; |
| 730 | |
| 731 | offset += size; |
| 732 | } |
| 733 | |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 734 | fdt_unflatten_node(blob, struct_offset, tree, &tree->root); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 735 | |
| 736 | return tree; |
| 737 | } |
| 738 | |
| 739 | |
| 740 | |
| 741 | /* |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 742 | * 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] | 743 | */ |
| 744 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 745 | static void dt_flat_prop_size(struct device_tree_property *prop, |
| 746 | uint32_t *struct_size, uint32_t *strings_size) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 747 | { |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 748 | /* Starting token. */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 749 | *struct_size += sizeof(uint32_t); |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 750 | /* Size. */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 751 | *struct_size += sizeof(uint32_t); |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 752 | /* Name offset. */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 753 | *struct_size += sizeof(uint32_t); |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 754 | /* Property value. */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 755 | *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 756 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 757 | /* Property name. */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 758 | *strings_size += strlen(prop->prop.name) + 1; |
| 759 | } |
| 760 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 761 | static void dt_flat_node_size(struct device_tree_node *node, |
| 762 | uint32_t *struct_size, uint32_t *strings_size) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 763 | { |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 764 | /* Starting token. */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 765 | *struct_size += sizeof(uint32_t); |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 766 | /* Node name. */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 767 | *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 768 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 769 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 770 | list_for_each(prop, node->properties, list_node) |
| 771 | dt_flat_prop_size(prop, struct_size, strings_size); |
| 772 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 773 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 774 | list_for_each(child, node->children, list_node) |
| 775 | dt_flat_node_size(child, struct_size, strings_size); |
| 776 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 777 | /* End token. */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 778 | *struct_size += sizeof(uint32_t); |
| 779 | } |
| 780 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 781 | uint32_t dt_flat_size(const struct device_tree *tree) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 782 | { |
| 783 | uint32_t size = tree->header_size; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 784 | struct device_tree_reserve_map_entry *entry; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 785 | list_for_each(entry, tree->reserve_map, list_node) |
| 786 | size += sizeof(uint64_t) * 2; |
| 787 | size += sizeof(uint64_t) * 2; |
| 788 | |
| 789 | uint32_t struct_size = 0; |
| 790 | uint32_t strings_size = 0; |
| 791 | dt_flat_node_size(tree->root, &struct_size, &strings_size); |
| 792 | |
| 793 | size += struct_size; |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 794 | /* End token. */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 795 | size += sizeof(uint32_t); |
| 796 | |
| 797 | size += strings_size; |
| 798 | |
| 799 | return size; |
| 800 | } |
| 801 | |
| 802 | |
| 803 | |
| 804 | /* |
| 805 | * Functions to flatten a device tree. |
| 806 | */ |
| 807 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 808 | 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] | 809 | void **map_start) |
| 810 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 811 | ((uint64_t *)*map_start)[0] = htobe64(entry->start); |
| 812 | ((uint64_t *)*map_start)[1] = htobe64(entry->size); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 813 | *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2; |
| 814 | } |
| 815 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 816 | static void dt_flatten_prop(struct device_tree_property *prop, |
| 817 | void **struct_start, void *strings_base, |
| 818 | void **strings_start) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 819 | { |
| 820 | uint8_t *dstruct = (uint8_t *)*struct_start; |
| 821 | uint8_t *dstrings = (uint8_t *)*strings_start; |
| 822 | |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 823 | be32enc(dstruct, FDT_TOKEN_PROPERTY); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 824 | dstruct += sizeof(uint32_t); |
| 825 | |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 826 | be32enc(dstruct, prop->prop.size); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 827 | dstruct += sizeof(uint32_t); |
| 828 | |
| 829 | uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base; |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 830 | be32enc(dstruct, name_offset); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 831 | dstruct += sizeof(uint32_t); |
| 832 | |
| 833 | strcpy((char *)dstrings, prop->prop.name); |
| 834 | dstrings += strlen(prop->prop.name) + 1; |
| 835 | |
| 836 | memcpy(dstruct, prop->prop.data, prop->prop.size); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 837 | dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 838 | |
| 839 | *struct_start = dstruct; |
| 840 | *strings_start = dstrings; |
| 841 | } |
| 842 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 843 | static void dt_flatten_node(const struct device_tree_node *node, |
| 844 | void **struct_start, void *strings_base, |
| 845 | void **strings_start) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 846 | { |
| 847 | uint8_t *dstruct = (uint8_t *)*struct_start; |
| 848 | uint8_t *dstrings = (uint8_t *)*strings_start; |
| 849 | |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 850 | be32enc(dstruct, FDT_TOKEN_BEGIN_NODE); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 851 | dstruct += sizeof(uint32_t); |
| 852 | |
| 853 | strcpy((char *)dstruct, node->name); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 854 | dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 855 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 856 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 857 | list_for_each(prop, node->properties, list_node) |
| 858 | dt_flatten_prop(prop, (void **)&dstruct, strings_base, |
| 859 | (void **)&dstrings); |
| 860 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 861 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 862 | list_for_each(child, node->children, list_node) |
| 863 | dt_flatten_node(child, (void **)&dstruct, strings_base, |
| 864 | (void **)&dstrings); |
| 865 | |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 866 | be32enc(dstruct, FDT_TOKEN_END_NODE); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 867 | dstruct += sizeof(uint32_t); |
| 868 | |
| 869 | *struct_start = dstruct; |
| 870 | *strings_start = dstrings; |
| 871 | } |
| 872 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 873 | void dt_flatten(const struct device_tree *tree, void *start_dest) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 874 | { |
| 875 | uint8_t *dest = (uint8_t *)start_dest; |
| 876 | |
| 877 | memcpy(dest, tree->header, tree->header_size); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 878 | struct fdt_header *header = (struct fdt_header *)dest; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 879 | dest += tree->header_size; |
| 880 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 881 | struct device_tree_reserve_map_entry *entry; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 882 | list_for_each(entry, tree->reserve_map, list_node) |
| 883 | dt_flatten_map_entry(entry, (void **)&dest); |
| 884 | ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0; |
| 885 | dest += sizeof(uint64_t) * 2; |
| 886 | |
| 887 | uint32_t struct_size = 0; |
| 888 | uint32_t strings_size = 0; |
| 889 | dt_flat_node_size(tree->root, &struct_size, &strings_size); |
| 890 | |
| 891 | uint8_t *struct_start = dest; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 892 | header->structure_offset = htobe32(dest - (uint8_t *)start_dest); |
| 893 | header->structure_size = htobe32(struct_size); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 894 | dest += struct_size; |
| 895 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 896 | *((uint32_t *)dest) = htobe32(FDT_TOKEN_END); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 897 | dest += sizeof(uint32_t); |
| 898 | |
| 899 | uint8_t *strings_start = dest; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 900 | header->strings_offset = htobe32(dest - (uint8_t *)start_dest); |
| 901 | header->strings_size = htobe32(strings_size); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 902 | dest += strings_size; |
| 903 | |
| 904 | dt_flatten_node(tree->root, (void **)&struct_start, strings_start, |
| 905 | (void **)&strings_start); |
| 906 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 907 | header->totalsize = htobe32(dest - (uint8_t *)start_dest); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | |
| 911 | |
| 912 | /* |
| 913 | * Functions for printing a non-flattened device tree. |
| 914 | */ |
| 915 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 916 | static void print_node(const struct device_tree_node *node, int depth) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 917 | { |
| 918 | print_indent(depth); |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 919 | if (depth == 0) /* root node has no name, print a starting slash */ |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 920 | printk(BIOS_DEBUG, "/"); |
| 921 | printk(BIOS_DEBUG, "%s {\n", node->name); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 922 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 923 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 924 | list_for_each(prop, node->properties, list_node) |
| 925 | print_property(&prop->prop, depth + 1); |
| 926 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 927 | printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */ |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 928 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 929 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 930 | list_for_each(child, node->children, list_node) |
| 931 | print_node(child, depth + 1); |
Julius Werner | 0d74653 | 2019-05-06 19:35:56 -0700 | [diff] [blame] | 932 | |
| 933 | print_indent(depth); |
| 934 | printk(BIOS_DEBUG, "};\n"); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 935 | } |
| 936 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 937 | void dt_print_node(const struct device_tree_node *node) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 938 | { |
| 939 | print_node(node, 0); |
| 940 | } |
| 941 | |
| 942 | |
| 943 | |
| 944 | /* |
| 945 | * Functions for reading and manipulating an unflattened device tree. |
| 946 | */ |
| 947 | |
| 948 | /* |
| 949 | * Read #address-cells and #size-cells properties from a node. |
| 950 | * |
| 951 | * @param node The device tree node to read from. |
| 952 | * @param addrcp Pointer to store #address-cells in, skipped if NULL. |
| 953 | * @param sizecp Pointer to store #size-cells in, skipped if NULL. |
| 954 | */ |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 955 | void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp, |
| 956 | u32 *sizecp) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 957 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 958 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 959 | list_for_each(prop, node->properties, list_node) { |
| 960 | if (addrcp && !strcmp("#address-cells", prop->prop.name)) |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 961 | *addrcp = be32dec(prop->prop.data); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 962 | if (sizecp && !strcmp("#size-cells", prop->prop.name)) |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 963 | *sizecp = be32dec(prop->prop.data); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * Find a node from a device tree path, relative to a parent node. |
| 969 | * |
| 970 | * @param parent The node from which to start the relative path lookup. |
| 971 | * @param path An array of path component strings that will be looked |
Elyes HAOUAS | e3e3f4f | 2018-06-29 21:41:41 +0200 | [diff] [blame] | 972 | * up in order to find the node. Must be terminated with |
| 973 | * a NULL pointer. Example: {'firmware', 'coreboot', NULL} |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 974 | * @param addrcp Pointer that will be updated with any #address-cells |
Elyes HAOUAS | e3e3f4f | 2018-06-29 21:41:41 +0200 | [diff] [blame] | 975 | * value found in the path. May be NULL to ignore. |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 976 | * @param sizecp Pointer that will be updated with any #size-cells |
Elyes HAOUAS | e3e3f4f | 2018-06-29 21:41:41 +0200 | [diff] [blame] | 977 | * value found in the path. May be NULL to ignore. |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 978 | * @param create 1: Create node(s) if not found. 0: Return NULL instead. |
| 979 | * @return The found/created node, or NULL. |
| 980 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 981 | struct device_tree_node *dt_find_node(struct device_tree_node *parent, |
| 982 | const char **path, u32 *addrcp, |
| 983 | u32 *sizecp, int create) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 984 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 985 | struct device_tree_node *node, *found = NULL; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 986 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 987 | /* Update #address-cells and #size-cells for this level. */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 988 | dt_read_cell_props(parent, addrcp, sizecp); |
| 989 | |
| 990 | if (!*path) |
| 991 | return parent; |
| 992 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 993 | /* Find the next node in the path, if it exists. */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 994 | list_for_each(node, parent->children, list_node) { |
| 995 | if (!strcmp(node->name, *path)) { |
| 996 | found = node; |
| 997 | break; |
| 998 | } |
| 999 | } |
| 1000 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1001 | /* Otherwise create it or return NULL. */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1002 | if (!found) { |
| 1003 | if (!create) |
| 1004 | return NULL; |
| 1005 | |
Sergii Dmytruk | 206328d | 2022-03-13 18:23:17 +0200 | [diff] [blame] | 1006 | found = calloc(1, sizeof(*found)); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1007 | if (!found) |
| 1008 | return NULL; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1009 | found->name = strdup(*path); |
| 1010 | if (!found->name) |
| 1011 | return NULL; |
| 1012 | |
| 1013 | list_insert_after(&found->list_node, &parent->children); |
| 1014 | } |
| 1015 | |
| 1016 | return dt_find_node(found, path + 1, addrcp, sizecp, create); |
| 1017 | } |
| 1018 | |
| 1019 | /* |
Julius Werner | f36d53c | 2019-05-03 18:23:34 -0700 | [diff] [blame] | 1020 | * Find a node in the tree from a string device tree path. |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1021 | * |
Julius Werner | f36d53c | 2019-05-03 18:23:34 -0700 | [diff] [blame] | 1022 | * @param tree The device tree to search. |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1023 | * @param path A string representing a path in the device tree, with |
Julius Werner | fbec63d | 2019-05-03 18:29:28 -0700 | [diff] [blame] | 1024 | * nodes separated by '/'. Example: "/firmware/coreboot" |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1025 | * @param addrcp Pointer that will be updated with any #address-cells |
| 1026 | * value found in the path. May be NULL to ignore. |
| 1027 | * @param sizecp Pointer that will be updated with any #size-cells |
| 1028 | * value found in the path. May be NULL to ignore. |
| 1029 | * @param create 1: Create node(s) if not found. 0: Return NULL instead. |
| 1030 | * @return The found/created node, or NULL. |
| 1031 | * |
Julius Werner | 6d5695f | 2019-05-06 19:23:28 -0700 | [diff] [blame] | 1032 | * It is the caller responsibility to provide a path string that doesn't end |
| 1033 | * with a '/' and doesn't contain any "//". If the path does not start with a |
| 1034 | * '/', the first segment is interpreted as an alias. */ |
Julius Werner | f36d53c | 2019-05-03 18:23:34 -0700 | [diff] [blame] | 1035 | 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] | 1036 | const char *path, u32 *addrcp, |
| 1037 | u32 *sizecp, int create) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1038 | { |
Julius Werner | 6d5695f | 2019-05-06 19:23:28 -0700 | [diff] [blame] | 1039 | char *sub_path; |
| 1040 | char *duped_str; |
| 1041 | struct device_tree_node *parent; |
| 1042 | char *next_slash; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1043 | /* Hopefully enough depth for any node. */ |
| 1044 | const char *path_array[15]; |
| 1045 | int i; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1046 | struct device_tree_node *node = NULL; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1047 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1048 | if (path[0] == '/') { /* regular path */ |
| 1049 | if (path[1] == '\0') { /* special case: "/" is root node */ |
Julius Werner | 6d5695f | 2019-05-06 19:23:28 -0700 | [diff] [blame] | 1050 | dt_read_cell_props(tree->root, addrcp, sizecp); |
| 1051 | return tree->root; |
| 1052 | } |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1053 | |
Julius Werner | 6d5695f | 2019-05-06 19:23:28 -0700 | [diff] [blame] | 1054 | sub_path = duped_str = strdup(&path[1]); |
| 1055 | if (!sub_path) |
| 1056 | return NULL; |
| 1057 | |
| 1058 | parent = tree->root; |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1059 | } else { /* alias */ |
Julius Werner | 6d5695f | 2019-05-06 19:23:28 -0700 | [diff] [blame] | 1060 | char *alias; |
| 1061 | |
| 1062 | alias = duped_str = strdup(path); |
| 1063 | if (!alias) |
| 1064 | return NULL; |
| 1065 | |
| 1066 | sub_path = strchr(alias, '/'); |
| 1067 | if (sub_path) |
| 1068 | *sub_path = '\0'; |
| 1069 | |
| 1070 | parent = dt_find_node_by_alias(tree, alias); |
| 1071 | if (!parent) { |
| 1072 | printk(BIOS_DEBUG, |
| 1073 | "Could not find node '%s', alias '%s' does not exist\n", |
| 1074 | path, alias); |
| 1075 | free(duped_str); |
| 1076 | return NULL; |
| 1077 | } |
| 1078 | |
| 1079 | if (!sub_path) { |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1080 | /* it's just the alias, no sub-path */ |
Julius Werner | 6d5695f | 2019-05-06 19:23:28 -0700 | [diff] [blame] | 1081 | free(duped_str); |
| 1082 | return parent; |
| 1083 | } |
| 1084 | |
| 1085 | sub_path++; |
| 1086 | } |
| 1087 | |
| 1088 | next_slash = sub_path; |
| 1089 | path_array[0] = sub_path; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1090 | for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) { |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1091 | next_slash = strchr(next_slash, '/'); |
| 1092 | if (!next_slash) |
| 1093 | break; |
| 1094 | |
| 1095 | *next_slash++ = '\0'; |
| 1096 | path_array[i] = next_slash; |
| 1097 | } |
| 1098 | |
| 1099 | if (!next_slash) { |
| 1100 | path_array[i] = NULL; |
Julius Werner | 6d5695f | 2019-05-06 19:23:28 -0700 | [diff] [blame] | 1101 | node = dt_find_node(parent, path_array, |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1102 | addrcp, sizecp, create); |
| 1103 | } |
| 1104 | |
Julius Werner | 6d5695f | 2019-05-06 19:23:28 -0700 | [diff] [blame] | 1105 | free(duped_str); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1106 | return node; |
| 1107 | } |
| 1108 | |
Julius Werner | 6d5695f | 2019-05-06 19:23:28 -0700 | [diff] [blame] | 1109 | /* |
| 1110 | * Find a node from an alias |
| 1111 | * |
| 1112 | * @param tree The device tree. |
| 1113 | * @param alias The alias name. |
| 1114 | * @return The found node, or NULL. |
| 1115 | */ |
| 1116 | struct device_tree_node *dt_find_node_by_alias(struct device_tree *tree, |
| 1117 | const char *alias) |
| 1118 | { |
| 1119 | struct device_tree_node *node; |
| 1120 | const char *alias_path; |
| 1121 | |
| 1122 | node = dt_find_node_by_path(tree, "/aliases", NULL, NULL, 0); |
| 1123 | if (!node) |
| 1124 | return NULL; |
| 1125 | |
| 1126 | alias_path = dt_find_string_prop(node, alias); |
| 1127 | if (!alias_path) |
| 1128 | return NULL; |
| 1129 | |
| 1130 | return dt_find_node_by_path(tree, alias_path, NULL, NULL, 0); |
| 1131 | } |
| 1132 | |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 1133 | struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root, |
| 1134 | uint32_t phandle) |
| 1135 | { |
| 1136 | if (!root) |
| 1137 | return NULL; |
| 1138 | |
| 1139 | if (root->phandle == phandle) |
| 1140 | return root; |
| 1141 | |
| 1142 | struct device_tree_node *node; |
| 1143 | struct device_tree_node *result; |
| 1144 | list_for_each(node, root->children, list_node) { |
| 1145 | result = dt_find_node_by_phandle(node, phandle); |
| 1146 | if (result) |
| 1147 | return result; |
| 1148 | } |
| 1149 | |
| 1150 | return NULL; |
| 1151 | } |
| 1152 | |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1153 | /* |
| 1154 | * Check if given node is compatible. |
| 1155 | * |
| 1156 | * @param node The node which is to be checked for compatible property. |
| 1157 | * @param compat The compatible string to match. |
| 1158 | * @return 1 = compatible, 0 = not compatible. |
| 1159 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1160 | static int dt_check_compat_match(struct device_tree_node *node, |
| 1161 | const char *compat) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1162 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1163 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1164 | |
| 1165 | list_for_each(prop, node->properties, list_node) { |
| 1166 | if (!strcmp("compatible", prop->prop.name)) { |
| 1167 | size_t bytes = prop->prop.size; |
| 1168 | const char *str = prop->prop.data; |
| 1169 | while (bytes > 0) { |
| 1170 | if (!strncmp(compat, str, bytes)) |
| 1171 | return 1; |
| 1172 | size_t len = strnlen(str, bytes) + 1; |
| 1173 | if (bytes <= len) |
| 1174 | break; |
| 1175 | str += len; |
| 1176 | bytes -= len; |
| 1177 | } |
| 1178 | break; |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | /* |
| 1186 | * Find a node from a compatible string, in the subtree of a parent node. |
| 1187 | * |
| 1188 | * @param parent The parent node under which to look. |
| 1189 | * @param compat The compatible string to find. |
| 1190 | * @return The found node, or NULL. |
| 1191 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1192 | struct device_tree_node *dt_find_compat(struct device_tree_node *parent, |
| 1193 | const char *compat) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1194 | { |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1195 | /* Check if the parent node itself is compatible. */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1196 | if (dt_check_compat_match(parent, compat)) |
| 1197 | return parent; |
| 1198 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1199 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1200 | list_for_each(child, parent->children, list_node) { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1201 | struct device_tree_node *found = dt_find_compat(child, compat); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1202 | if (found) |
| 1203 | return found; |
| 1204 | } |
| 1205 | |
| 1206 | return NULL; |
| 1207 | } |
| 1208 | |
| 1209 | /* |
Martin Roth | 0949e73 | 2021-10-01 14:28:22 -0600 | [diff] [blame] | 1210 | * Find the next compatible child of a given parent. All children up to the |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1211 | * child passed in by caller are ignored. If child is NULL, it considers all the |
| 1212 | * children to find the first child which is compatible. |
| 1213 | * |
| 1214 | * @param parent The parent node under which to look. |
| 1215 | * @param child The child node to start search from (exclusive). If NULL |
| 1216 | * consider all children. |
| 1217 | * @param compat The compatible string to find. |
| 1218 | * @return The found node, or NULL. |
| 1219 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1220 | struct device_tree_node * |
| 1221 | dt_find_next_compat_child(struct device_tree_node *parent, |
| 1222 | struct device_tree_node *child, |
| 1223 | const char *compat) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1224 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1225 | struct device_tree_node *next; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1226 | int ignore = 0; |
| 1227 | |
| 1228 | if (child) |
| 1229 | ignore = 1; |
| 1230 | |
| 1231 | list_for_each(next, parent->children, list_node) { |
| 1232 | if (ignore) { |
| 1233 | if (child == next) |
| 1234 | ignore = 0; |
| 1235 | continue; |
| 1236 | } |
| 1237 | |
| 1238 | if (dt_check_compat_match(next, compat)) |
| 1239 | return next; |
| 1240 | } |
| 1241 | |
| 1242 | return NULL; |
| 1243 | } |
| 1244 | |
| 1245 | /* |
| 1246 | * Find a node with matching property value, in the subtree of a parent node. |
| 1247 | * |
| 1248 | * @param parent The parent node under which to look. |
| 1249 | * @param name The property name to look for. |
| 1250 | * @param data The property value to look for. |
| 1251 | * @param size The property size. |
| 1252 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1253 | struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent, |
| 1254 | const char *name, void *data, |
| 1255 | size_t size) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1256 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1257 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1258 | |
| 1259 | /* Check if parent itself has the required property value. */ |
| 1260 | list_for_each(prop, parent->properties, list_node) { |
| 1261 | if (!strcmp(name, prop->prop.name)) { |
| 1262 | size_t bytes = prop->prop.size; |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 1263 | const void *prop_data = prop->prop.data; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1264 | if (size != bytes) |
| 1265 | break; |
| 1266 | if (!memcmp(data, prop_data, size)) |
| 1267 | return parent; |
| 1268 | break; |
| 1269 | } |
| 1270 | } |
| 1271 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1272 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1273 | list_for_each(child, parent->children, list_node) { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1274 | struct device_tree_node *found = dt_find_prop_value(child, name, |
| 1275 | data, size); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1276 | if (found) |
| 1277 | return found; |
| 1278 | } |
| 1279 | return NULL; |
| 1280 | } |
| 1281 | |
| 1282 | /* |
| 1283 | * Write an arbitrary sized big-endian integer into a pointer. |
| 1284 | * |
| 1285 | * @param dest Pointer to the DT property data buffer to write. |
Elyes HAOUAS | 1ec7644 | 2018-08-07 12:20:04 +0200 | [diff] [blame] | 1286 | * @param src The integer to write (in CPU endianness). |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1287 | * @param length the length of the destination integer in bytes. |
| 1288 | */ |
| 1289 | void dt_write_int(u8 *dest, u64 src, size_t length) |
| 1290 | { |
| 1291 | while (length--) { |
| 1292 | dest[length] = (u8)src; |
| 1293 | src >>= 8; |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | /* |
Patrick Rudolph | 5ccc731 | 2018-05-30 15:05:28 +0200 | [diff] [blame] | 1298 | * Delete a property by name in a given node if it exists. |
| 1299 | * |
| 1300 | * @param node The device tree node to operate on. |
| 1301 | * @param name The name of the property to delete. |
| 1302 | */ |
| 1303 | void dt_delete_prop(struct device_tree_node *node, const char *name) |
| 1304 | { |
| 1305 | struct device_tree_property *prop; |
| 1306 | |
| 1307 | list_for_each(prop, node->properties, list_node) { |
| 1308 | if (!strcmp(prop->prop.name, name)) { |
| 1309 | list_remove(&prop->list_node); |
| 1310 | return; |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | /* |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1316 | * Add an arbitrary property to a node, or update it if it already exists. |
| 1317 | * |
| 1318 | * @param node The device tree node to add to. |
| 1319 | * @param name The name of the new property. |
| 1320 | * @param data The raw data blob to be stored in the property. |
| 1321 | * @param size The size of data in bytes. |
| 1322 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1323 | 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] | 1324 | void *data, size_t size) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1325 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1326 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1327 | |
| 1328 | list_for_each(prop, node->properties, list_node) { |
| 1329 | if (!strcmp(prop->prop.name, name)) { |
| 1330 | prop->prop.data = data; |
| 1331 | prop->prop.size = size; |
| 1332 | return; |
| 1333 | } |
| 1334 | } |
| 1335 | |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 1336 | prop = xzalloc(sizeof(*prop)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1337 | list_insert_after(&prop->list_node, &node->properties); |
| 1338 | prop->prop.name = name; |
| 1339 | prop->prop.data = data; |
| 1340 | prop->prop.size = size; |
| 1341 | } |
| 1342 | |
| 1343 | /* |
| 1344 | * Find given string property in a node and return its content. |
| 1345 | * |
| 1346 | * @param node The device tree node to search. |
| 1347 | * @param name The name of the property. |
| 1348 | * @return The found string, or NULL. |
| 1349 | */ |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 1350 | const char *dt_find_string_prop(const struct device_tree_node *node, |
| 1351 | const char *name) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1352 | { |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 1353 | const void *content; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1354 | size_t size; |
| 1355 | |
| 1356 | dt_find_bin_prop(node, name, &content, &size); |
| 1357 | |
| 1358 | return content; |
| 1359 | } |
| 1360 | |
| 1361 | /* |
| 1362 | * Find given property in a node. |
| 1363 | * |
| 1364 | * @param node The device tree node to search. |
| 1365 | * @param name The name of the property. |
| 1366 | * @param data Pointer to return raw data blob in the property. |
| 1367 | * @param size Pointer to return the size of data in bytes. |
| 1368 | */ |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 1369 | void dt_find_bin_prop(const struct device_tree_node *node, const char *name, |
| 1370 | const void **data, size_t *size) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1371 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1372 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1373 | |
| 1374 | *data = NULL; |
| 1375 | *size = 0; |
| 1376 | |
| 1377 | list_for_each(prop, node->properties, list_node) { |
| 1378 | if (!strcmp(prop->prop.name, name)) { |
| 1379 | *data = prop->prop.data; |
| 1380 | *size = prop->prop.size; |
| 1381 | return; |
| 1382 | } |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | /* |
| 1387 | * Add a string property to a node, or update it if it already exists. |
| 1388 | * |
| 1389 | * @param node The device tree node to add to. |
| 1390 | * @param name The name of the new property. |
| 1391 | * @param str The zero-terminated string to be stored in the property. |
| 1392 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1393 | 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] | 1394 | const char *str) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1395 | { |
Julius Werner | 0e9116f | 2019-05-13 17:30:31 -0700 | [diff] [blame] | 1396 | dt_add_bin_prop(node, name, (char *)str, strlen(str) + 1); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | /* |
| 1400 | * Add a 32-bit integer property to a node, or update it if it already exists. |
| 1401 | * |
| 1402 | * @param node The device tree node to add to. |
| 1403 | * @param name The name of the new property. |
| 1404 | * @param val The integer to be stored in the property. |
| 1405 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1406 | 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] | 1407 | { |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 1408 | u32 *val_ptr = xmalloc(sizeof(val)); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1409 | *val_ptr = htobe32(val); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1410 | dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr)); |
| 1411 | } |
| 1412 | |
| 1413 | /* |
Patrick Rudolph | 3fca4ed | 2018-08-10 10:12:35 +0200 | [diff] [blame] | 1414 | * Add a 64-bit integer property to a node, or update it if it already exists. |
| 1415 | * |
| 1416 | * @param node The device tree node to add to. |
| 1417 | * @param name The name of the new property. |
| 1418 | * @param val The integer to be stored in the property. |
| 1419 | */ |
| 1420 | void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val) |
| 1421 | { |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 1422 | u64 *val_ptr = xmalloc(sizeof(val)); |
Patrick Rudolph | 3fca4ed | 2018-08-10 10:12:35 +0200 | [diff] [blame] | 1423 | *val_ptr = htobe64(val); |
| 1424 | dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr)); |
| 1425 | } |
| 1426 | |
| 1427 | /* |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1428 | * Add a 'reg' address list property to a node, or update it if it exists. |
| 1429 | * |
| 1430 | * @param node The device tree node to add to. |
Maximilian Brune | 33079b8 | 2024-03-04 15:34:41 +0100 | [diff] [blame] | 1431 | * @param regions Array of address values to be stored in the property. |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1432 | * @param sizes Array of corresponding size values to 'addrs'. |
| 1433 | * @param count Number of values in 'addrs' and 'sizes' (must be equal). |
| 1434 | * @param addr_cells Value of #address-cells property valid for this node. |
| 1435 | * @param size_cells Value of #size-cells property valid for this node. |
| 1436 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1437 | 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] | 1438 | int count, u32 addr_cells, u32 size_cells) |
| 1439 | { |
| 1440 | int i; |
| 1441 | size_t length = (addr_cells + size_cells) * sizeof(u32) * count; |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 1442 | u8 *data = xmalloc(length); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1443 | u8 *cur = data; |
| 1444 | |
| 1445 | for (i = 0; i < count; i++) { |
| 1446 | dt_write_int(cur, addrs[i], addr_cells * sizeof(u32)); |
| 1447 | cur += addr_cells * sizeof(u32); |
| 1448 | dt_write_int(cur, sizes[i], size_cells * sizeof(u32)); |
| 1449 | cur += size_cells * sizeof(u32); |
| 1450 | } |
| 1451 | |
| 1452 | dt_add_bin_prop(node, "reg", data, length); |
| 1453 | } |
| 1454 | |
| 1455 | /* |
| 1456 | * Fixups to apply to a kernel's device tree before booting it. |
| 1457 | */ |
| 1458 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1459 | struct list_node device_tree_fixups; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1460 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1461 | int dt_apply_fixups(struct device_tree *tree) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1462 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1463 | struct device_tree_fixup *fixup; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1464 | list_for_each(fixup, device_tree_fixups, list_node) { |
| 1465 | assert(fixup->fixup); |
| 1466 | if (fixup->fixup(fixup, tree)) |
| 1467 | return 1; |
| 1468 | } |
| 1469 | return 0; |
| 1470 | } |
| 1471 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1472 | 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] | 1473 | void *data, size_t data_size, int create) |
| 1474 | { |
| 1475 | char *path_copy, *prop_name; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1476 | struct device_tree_node *dt_node; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1477 | |
| 1478 | path_copy = strdup(path); |
| 1479 | |
| 1480 | if (!path_copy) { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1481 | printk(BIOS_ERR, "Failed to allocate a copy of path %s\n", |
| 1482 | path); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1483 | return 1; |
| 1484 | } |
| 1485 | |
| 1486 | prop_name = strrchr(path_copy, '/'); |
| 1487 | if (!prop_name) { |
Patrick Rudolph | 679d624 | 2018-07-11 13:53:04 +0200 | [diff] [blame] | 1488 | free(path_copy); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1489 | printk(BIOS_ERR, "Path %s does not include '/'\n", path); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1490 | return 1; |
| 1491 | } |
| 1492 | |
| 1493 | *prop_name++ = '\0'; /* Separate path from the property name. */ |
| 1494 | |
Julius Werner | f36d53c | 2019-05-03 18:23:34 -0700 | [diff] [blame] | 1495 | dt_node = dt_find_node_by_path(tree, path_copy, NULL, |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1496 | NULL, create); |
| 1497 | |
| 1498 | if (!dt_node) { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1499 | printk(BIOS_ERR, "Failed to %s %s in the device tree\n", |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1500 | create ? "create" : "find", path_copy); |
Patrick Rudolph | 679d624 | 2018-07-11 13:53:04 +0200 | [diff] [blame] | 1501 | free(path_copy); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1502 | return 1; |
| 1503 | } |
| 1504 | |
| 1505 | dt_add_bin_prop(dt_node, prop_name, data, data_size); |
Patrick Rudolph | 679d624 | 2018-07-11 13:53:04 +0200 | [diff] [blame] | 1506 | free(path_copy); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1507 | |
| 1508 | return 0; |
| 1509 | } |
| 1510 | |
| 1511 | /* |
| 1512 | * Prepare the /reserved-memory/ node. |
| 1513 | * |
| 1514 | * Technically, this can be called more than one time, to init and/or retrieve |
| 1515 | * the node. But dt_add_u32_prop() may leak a bit of memory if you do. |
| 1516 | * |
| 1517 | * @tree: Device tree to add/retrieve from. |
| 1518 | * @return: The /reserved-memory/ node (or NULL, if error). |
| 1519 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1520 | 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] | 1521 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 1522 | struct device_tree_node *reserved; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1523 | u32 addr = 0, size = 0; |
| 1524 | |
Julius Werner | fbec63d | 2019-05-03 18:29:28 -0700 | [diff] [blame] | 1525 | reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr, |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1526 | &size, 1); |
| 1527 | if (!reserved) |
| 1528 | return NULL; |
| 1529 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1530 | /* Binding doc says this should have the same #{address,size}-cells as |
| 1531 | the root. */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1532 | dt_add_u32_prop(reserved, "#address-cells", addr); |
| 1533 | dt_add_u32_prop(reserved, "#size-cells", size); |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1534 | /* Binding doc says this should be empty (1:1 mapping from root). */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 1535 | dt_add_bin_prop(reserved, "ranges", NULL, 0); |
| 1536 | |
| 1537 | return reserved; |
| 1538 | } |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1539 | |
| 1540 | /* |
| 1541 | * Increment a single phandle in prop at a given offset by a given adjustment. |
| 1542 | * |
| 1543 | * @param prop Property whose phandle should be adjusted. |
| 1544 | * @param adjustment Value that should be added to the existing phandle. |
| 1545 | * @param offset Byte offset of the phandle in the property data. |
| 1546 | * |
| 1547 | * @return New phandle value, or 0 on error. |
| 1548 | */ |
| 1549 | static uint32_t dt_adjust_phandle(struct device_tree_property *prop, |
| 1550 | uint32_t adjustment, uint32_t offset) |
| 1551 | { |
| 1552 | if (offset + 4 > prop->prop.size) |
| 1553 | return 0; |
| 1554 | |
| 1555 | uint32_t phandle = be32dec(prop->prop.data + offset); |
| 1556 | if (phandle == 0 || |
| 1557 | phandle == FDT_PHANDLE_ILLEGAL || |
| 1558 | phandle == 0xffffffff) |
| 1559 | return 0; |
| 1560 | |
| 1561 | phandle += adjustment; |
| 1562 | if (phandle >= FDT_PHANDLE_ILLEGAL) |
| 1563 | return 0; |
| 1564 | |
| 1565 | be32enc(prop->prop.data + offset, phandle); |
| 1566 | return phandle; |
| 1567 | } |
| 1568 | |
| 1569 | /* |
| 1570 | * Adjust all phandles in subtree by adding a new base offset. |
| 1571 | * |
| 1572 | * @param node Root node of the subtree to work on. |
| 1573 | * @param base New phandle base to be added to all phandles. |
| 1574 | * |
| 1575 | * @return New highest phandle in the subtree, or 0 on error. |
| 1576 | */ |
| 1577 | static uint32_t dt_adjust_all_phandles(struct device_tree_node *node, |
| 1578 | uint32_t base) |
| 1579 | { |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1580 | uint32_t new_max = MAX(base, 1); /* make sure we don't return 0 */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1581 | struct device_tree_property *prop; |
| 1582 | struct device_tree_node *child; |
| 1583 | |
| 1584 | if (!node) |
| 1585 | return new_max; |
| 1586 | |
| 1587 | list_for_each(prop, node->properties, list_node) |
| 1588 | if (dt_prop_is_phandle(prop)) { |
| 1589 | node->phandle = dt_adjust_phandle(prop, base, 0); |
| 1590 | if (!node->phandle) |
| 1591 | return 0; |
| 1592 | new_max = MAX(new_max, node->phandle); |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1593 | } /* no break -- can have more than one phandle prop */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1594 | |
| 1595 | list_for_each(child, node->children, list_node) |
| 1596 | new_max = MAX(new_max, dt_adjust_all_phandles(child, base)); |
| 1597 | |
| 1598 | return new_max; |
| 1599 | } |
| 1600 | |
| 1601 | /* |
| 1602 | * Apply a /__local_fixup__ subtree to the corresponding overlay subtree. |
| 1603 | * |
| 1604 | * @param node Root node of the overlay subtree to fix up. |
| 1605 | * @param node Root node of the /__local_fixup__ subtree. |
| 1606 | * @param base Adjustment that was added to phandles in the overlay. |
| 1607 | * |
| 1608 | * @return 0 on success, -1 on error. |
| 1609 | */ |
| 1610 | static int dt_fixup_locals(struct device_tree_node *node, |
| 1611 | struct device_tree_node *fixup, uint32_t base) |
| 1612 | { |
| 1613 | struct device_tree_property *prop; |
| 1614 | struct device_tree_property *fixup_prop; |
| 1615 | struct device_tree_node *child; |
| 1616 | struct device_tree_node *fixup_child; |
| 1617 | int i; |
| 1618 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1619 | /* |
| 1620 | * For local fixups the /__local_fixup__ subtree contains the same node |
| 1621 | * hierarchy as the main tree we're fixing up. Each property contains |
| 1622 | * the fixup offsets for the respective property in the main tree. For |
| 1623 | * each property in the fixup node, find the corresponding property in |
| 1624 | * the base node and apply fixups to all offsets it specifies. |
| 1625 | */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1626 | list_for_each(fixup_prop, fixup->properties, list_node) { |
| 1627 | struct device_tree_property *base_prop = NULL; |
| 1628 | list_for_each(prop, node->properties, list_node) |
| 1629 | if (!strcmp(prop->prop.name, fixup_prop->prop.name)) { |
| 1630 | base_prop = prop; |
| 1631 | break; |
| 1632 | } |
| 1633 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1634 | /* We should always find a corresponding base prop for a fixup, |
| 1635 | and fixup props contain a list of 32-bit fixup offsets. */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1636 | if (!base_prop || fixup_prop->prop.size % sizeof(uint32_t)) |
| 1637 | return -1; |
| 1638 | |
| 1639 | for (i = 0; i < fixup_prop->prop.size; i += sizeof(uint32_t)) |
| 1640 | if (!dt_adjust_phandle(base_prop, base, be32dec( |
| 1641 | fixup_prop->prop.data + i))) |
| 1642 | return -1; |
| 1643 | } |
| 1644 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1645 | /* Now recursively descend both the base tree and the /__local_fixups__ |
| 1646 | subtree in sync to apply all fixups. */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1647 | list_for_each(fixup_child, fixup->children, list_node) { |
| 1648 | struct device_tree_node *base_child = NULL; |
| 1649 | list_for_each(child, node->children, list_node) |
| 1650 | if (!strcmp(child->name, fixup_child->name)) { |
| 1651 | base_child = child; |
| 1652 | break; |
| 1653 | } |
| 1654 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1655 | /* All fixup nodes should have a corresponding base node. */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1656 | if (!base_child) |
| 1657 | return -1; |
| 1658 | |
| 1659 | if (dt_fixup_locals(base_child, fixup_child, base) < 0) |
| 1660 | return -1; |
| 1661 | } |
| 1662 | |
| 1663 | return 0; |
| 1664 | } |
| 1665 | |
| 1666 | /* |
| 1667 | * Update all /__symbols__ properties in an overlay that start with |
| 1668 | * "/fragment@X/__overlay__" with corresponding path prefix in the base tree. |
| 1669 | * |
| 1670 | * @param symbols /__symbols__ done to update. |
| 1671 | * @param fragment /fragment@X node that references to should be updated. |
| 1672 | * @param base_path Path of base tree node that the fragment overlaid. |
| 1673 | */ |
| 1674 | static void dt_fix_symbols(struct device_tree_node *symbols, |
| 1675 | struct device_tree_node *fragment, |
| 1676 | const char *base_path) |
| 1677 | { |
| 1678 | struct device_tree_property *prop; |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1679 | char buf[512]; /* Should be enough for maximum DT path length? */ |
| 1680 | char node_path[64]; /* easily enough for /fragment@XXXX/__overlay__ */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1681 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1682 | if (!symbols) /* If the overlay has no /__symbols__ node, we're done! */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1683 | return; |
| 1684 | |
| 1685 | int len = snprintf(node_path, sizeof(node_path), "/%s/__overlay__", |
| 1686 | fragment->name); |
| 1687 | |
| 1688 | list_for_each(prop, symbols->properties, list_node) |
| 1689 | if (!strncmp(prop->prop.data, node_path, len)) { |
| 1690 | prop->prop.size = snprintf(buf, sizeof(buf), "%s%s", |
| 1691 | base_path, (char *)prop->prop.data + len) + 1; |
| 1692 | free(prop->prop.data); |
| 1693 | prop->prop.data = strdup(buf); |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | /* |
| 1698 | * Fix up overlay according to a property in /__fixup__. If the fixed property |
| 1699 | * is a /fragment@X:target, also update /__symbols__ references to fragment. |
| 1700 | * |
| 1701 | * @params overlay Overlay to fix up. |
| 1702 | * @params fixup /__fixup__ property. |
| 1703 | * @params phandle phandle value to insert where the fixup points to. |
| 1704 | * @params base_path Path to the base DT node that the fixup points to. |
| 1705 | * @params overlay_symbols /__symbols__ node of the overlay. |
| 1706 | * |
| 1707 | * @return 0 on success, -1 on error. |
| 1708 | */ |
| 1709 | static int dt_fixup_external(struct device_tree *overlay, |
| 1710 | struct device_tree_property *fixup, |
| 1711 | uint32_t phandle, const char *base_path, |
| 1712 | struct device_tree_node *overlay_symbols) |
| 1713 | { |
| 1714 | struct device_tree_property *prop; |
| 1715 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1716 | /* External fixup properties are encoded as "<path>:<prop>:<offset>". */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1717 | char *entry = fixup->prop.data; |
| 1718 | while ((void *)entry < fixup->prop.data + fixup->prop.size) { |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1719 | /* okay to destroy fixup property value, won't need it again */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1720 | char *node_path = entry; |
| 1721 | entry = strchr(node_path, ':'); |
| 1722 | if (!entry) |
| 1723 | return -1; |
| 1724 | *entry++ = '\0'; |
| 1725 | |
| 1726 | char *prop_name = entry; |
| 1727 | entry = strchr(prop_name, ':'); |
| 1728 | if (!entry) |
| 1729 | return -1; |
| 1730 | *entry++ = '\0'; |
| 1731 | |
| 1732 | struct device_tree_node *ovl_node = dt_find_node_by_path( |
| 1733 | overlay, node_path, NULL, NULL, 0); |
| 1734 | if (!ovl_node || !isdigit(*entry)) |
| 1735 | return -1; |
| 1736 | |
| 1737 | struct device_tree_property *ovl_prop = NULL; |
| 1738 | list_for_each(prop, ovl_node->properties, list_node) |
| 1739 | if (!strcmp(prop->prop.name, prop_name)) { |
| 1740 | ovl_prop = prop; |
| 1741 | break; |
| 1742 | } |
| 1743 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1744 | /* Move entry to first char after number, must be a '\0'. */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1745 | uint32_t offset = skip_atoi(&entry); |
| 1746 | if (!ovl_prop || offset + 4 > ovl_prop->prop.size || entry[0]) |
| 1747 | return -1; |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1748 | entry++; /* jump over '\0' to potential next fixup */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1749 | |
| 1750 | be32enc(ovl_prop->prop.data + offset, phandle); |
| 1751 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1752 | /* If this is a /fragment@X:target property, update references |
| 1753 | to this fragment in the overlay __symbols__ now. */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1754 | if (offset == 0 && !strcmp(prop_name, "target") && |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1755 | !strchr(node_path + 1, '/')) /* only toplevel nodes */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1756 | dt_fix_symbols(overlay_symbols, ovl_node, base_path); |
| 1757 | } |
| 1758 | |
| 1759 | return 0; |
| 1760 | } |
| 1761 | |
| 1762 | /* |
| 1763 | * Apply all /__fixup__ properties in the overlay. This will destroy the |
| 1764 | * property data in /__fixup__ and it should not be accessed again. |
| 1765 | * |
| 1766 | * @params tree Base device tree that the overlay updates. |
| 1767 | * @params symbols /__symbols__ node of the base device tree. |
| 1768 | * @params overlay Overlay to fix up. |
| 1769 | * @params fixups /__fixup__ node in the overlay. |
| 1770 | * @params overlay_symbols /__symbols__ node of the overlay. |
| 1771 | * |
| 1772 | * @return 0 on success, -1 on error. |
| 1773 | */ |
| 1774 | static int dt_fixup_all_externals(struct device_tree *tree, |
| 1775 | struct device_tree_node *symbols, |
| 1776 | struct device_tree *overlay, |
| 1777 | struct device_tree_node *fixups, |
| 1778 | struct device_tree_node *overlay_symbols) |
| 1779 | { |
| 1780 | struct device_tree_property *fix; |
| 1781 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1782 | /* If we have any external fixups, base tree must have /__symbols__. */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1783 | if (!symbols) |
| 1784 | return -1; |
| 1785 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1786 | /* |
| 1787 | * Unlike /__local_fixups__, /__fixups__ is not a whole subtree that |
| 1788 | * mirrors the node hierarchy. It's just a directory of fixup properties |
| 1789 | * that each directly contain all information necessary to apply them. |
| 1790 | */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1791 | list_for_each(fix, fixups->properties, list_node) { |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1792 | /* The name of a fixup property is the label of the node we want |
| 1793 | a property to phandle-reference. Look up in /__symbols__. */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1794 | const char *path = dt_find_string_prop(symbols, fix->prop.name); |
| 1795 | if (!path) |
| 1796 | return -1; |
| 1797 | |
Elyes HAOUAS | 0afaff2 | 2021-01-16 15:02:31 +0100 | [diff] [blame] | 1798 | /* Find node the label pointed to figure out its phandle. */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1799 | struct device_tree_node *node = dt_find_node_by_path(tree, path, |
| 1800 | NULL, NULL, 0); |
| 1801 | if (!node) |
| 1802 | return -1; |
| 1803 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1804 | /* Write into the overlay property(s) pointing to that node. */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1805 | if (dt_fixup_external(overlay, fix, node->phandle, |
| 1806 | path, overlay_symbols) < 0) |
| 1807 | return -1; |
| 1808 | } |
| 1809 | |
| 1810 | return 0; |
| 1811 | } |
| 1812 | |
| 1813 | /* |
| 1814 | * Copy all nodes and properties from one DT subtree into another. This is a |
| 1815 | * shallow copy so both trees will point to the same property data afterwards. |
| 1816 | * |
| 1817 | * @params dst Destination subtree to copy into. |
| 1818 | * @params src Source subtree to copy from. |
| 1819 | * @params upd 1 to overwrite same-name properties, 0 to discard them. |
| 1820 | */ |
| 1821 | static void dt_copy_subtree(struct device_tree_node *dst, |
| 1822 | struct device_tree_node *src, int upd) |
| 1823 | { |
| 1824 | struct device_tree_property *prop; |
| 1825 | struct device_tree_property *src_prop; |
| 1826 | list_for_each(src_prop, src->properties, list_node) { |
| 1827 | if (dt_prop_is_phandle(src_prop) || |
| 1828 | !strcmp(src_prop->prop.name, "name")) { |
| 1829 | printk(BIOS_DEBUG, |
| 1830 | "WARNING: ignoring illegal overlay prop '%s'\n", |
| 1831 | src_prop->prop.name); |
| 1832 | continue; |
| 1833 | } |
| 1834 | |
| 1835 | struct device_tree_property *dst_prop = NULL; |
| 1836 | list_for_each(prop, dst->properties, list_node) |
| 1837 | if (!strcmp(prop->prop.name, src_prop->prop.name)) { |
| 1838 | dst_prop = prop; |
| 1839 | break; |
| 1840 | } |
| 1841 | |
| 1842 | if (dst_prop) { |
| 1843 | if (!upd) { |
| 1844 | printk(BIOS_DEBUG, |
| 1845 | "WARNING: ignoring prop update '%s'\n", |
| 1846 | src_prop->prop.name); |
| 1847 | continue; |
| 1848 | } |
| 1849 | } else { |
| 1850 | dst_prop = xzalloc(sizeof(*dst_prop)); |
| 1851 | list_insert_after(&dst_prop->list_node, |
| 1852 | &dst->properties); |
| 1853 | } |
| 1854 | |
| 1855 | dst_prop->prop = src_prop->prop; |
| 1856 | } |
| 1857 | |
| 1858 | struct device_tree_node *node; |
| 1859 | struct device_tree_node *src_node; |
| 1860 | list_for_each(src_node, src->children, list_node) { |
| 1861 | struct device_tree_node *dst_node = NULL; |
| 1862 | list_for_each(node, dst->children, list_node) |
| 1863 | if (!strcmp(node->name, src_node->name)) { |
| 1864 | dst_node = node; |
| 1865 | break; |
| 1866 | } |
| 1867 | |
| 1868 | if (!dst_node) { |
| 1869 | dst_node = xzalloc(sizeof(*dst_node)); |
| 1870 | *dst_node = *src_node; |
| 1871 | list_insert_after(&dst_node->list_node, &dst->children); |
| 1872 | } else { |
| 1873 | dt_copy_subtree(dst_node, src_node, upd); |
| 1874 | } |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | /* |
| 1879 | * Apply an overlay /fragment@X node to a base device tree. |
| 1880 | * |
| 1881 | * @param tree Base device tree. |
| 1882 | * @param fragment /fragment@X node. |
| 1883 | * @params overlay_symbols /__symbols__ node of the overlay. |
| 1884 | * |
| 1885 | * @return 0 on success, -1 on error. |
| 1886 | */ |
| 1887 | static int dt_import_fragment(struct device_tree *tree, |
| 1888 | struct device_tree_node *fragment, |
| 1889 | struct device_tree_node *overlay_symbols) |
| 1890 | { |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1891 | /* The actual overlaid nodes/props are in an __overlay__ child node. */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1892 | static const char *overlay_path[] = { "__overlay__", NULL }; |
| 1893 | struct device_tree_node *overlay = dt_find_node(fragment, overlay_path, |
| 1894 | NULL, NULL, 0); |
| 1895 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1896 | /* If it doesn't have an __overlay__ child, it's not a fragment. */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1897 | if (!overlay) |
| 1898 | return 0; |
| 1899 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1900 | /* Target node of the fragment can be given by path or by phandle. */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1901 | struct device_tree_property *prop; |
| 1902 | struct device_tree_property *phandle = NULL; |
| 1903 | struct device_tree_property *path = NULL; |
| 1904 | list_for_each(prop, fragment->properties, list_node) { |
| 1905 | if (!strcmp(prop->prop.name, "target")) { |
| 1906 | phandle = prop; |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1907 | break; /* phandle target has priority, stop looking */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1908 | } |
| 1909 | if (!strcmp(prop->prop.name, "target-path")) |
| 1910 | path = prop; |
| 1911 | } |
| 1912 | |
| 1913 | struct device_tree_node *target = NULL; |
| 1914 | if (phandle) { |
| 1915 | if (phandle->prop.size != sizeof(uint32_t)) |
| 1916 | return -1; |
| 1917 | target = dt_find_node_by_phandle(tree->root, |
| 1918 | be32dec(phandle->prop.data)); |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1919 | /* Symbols already updated as part of dt_fixup_external(). */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1920 | } else if (path) { |
| 1921 | target = dt_find_node_by_path(tree, path->prop.data, |
| 1922 | NULL, NULL, 0); |
| 1923 | dt_fix_symbols(overlay_symbols, fragment, path->prop.data); |
| 1924 | } |
| 1925 | if (!target) |
| 1926 | return -1; |
| 1927 | |
| 1928 | dt_copy_subtree(target, overlay, 1); |
| 1929 | return 0; |
| 1930 | } |
| 1931 | |
| 1932 | /* |
| 1933 | * Apply a device tree overlay to a base device tree. This will |
| 1934 | * destroy/incorporate the overlay data, so it should not be freed or reused. |
| 1935 | * See dtc.git/Documentation/dt-object-internal.txt for overlay format details. |
| 1936 | * |
| 1937 | * @param tree Unflattened base device tree to add the overlay into. |
| 1938 | * @param overlay Unflattened overlay device tree to apply to the base. |
| 1939 | * |
| 1940 | * @return 0 on success, -1 on error. |
| 1941 | */ |
| 1942 | int dt_apply_overlay(struct device_tree *tree, struct device_tree *overlay) |
| 1943 | { |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1944 | /* |
| 1945 | * First, we need to make sure phandles inside the overlay don't clash |
| 1946 | * with those in the base tree. We just define the highest phandle value |
| 1947 | * in the base tree as the "phandle offset" for this overlay and |
| 1948 | * increment all phandles in it by that value. |
| 1949 | */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1950 | uint32_t phandle_base = tree->max_phandle; |
| 1951 | uint32_t new_max = dt_adjust_all_phandles(overlay->root, phandle_base); |
| 1952 | if (!new_max) { |
Elyes HAOUAS | 8b5841e | 2022-02-08 21:23:12 +0100 | [diff] [blame] | 1953 | printk(BIOS_ERR, "invalid phandles in overlay\n"); |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1954 | return -1; |
| 1955 | } |
| 1956 | tree->max_phandle = new_max; |
| 1957 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1958 | /* Now that we changed phandles in the overlay, we need to update any |
| 1959 | nodes referring to them. Those are listed in /__local_fixups__. */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1960 | struct device_tree_node *local_fixups = dt_find_node_by_path(overlay, |
| 1961 | "/__local_fixups__", NULL, NULL, 0); |
| 1962 | if (local_fixups && dt_fixup_locals(overlay->root, local_fixups, |
| 1963 | phandle_base) < 0) { |
Elyes HAOUAS | 8b5841e | 2022-02-08 21:23:12 +0100 | [diff] [blame] | 1964 | printk(BIOS_ERR, "invalid local fixups in overlay\n"); |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1965 | return -1; |
| 1966 | } |
| 1967 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1968 | /* |
| 1969 | * Besides local phandle references (from nodes within the overlay to |
| 1970 | * other nodes within the overlay), the overlay may also contain phandle |
| 1971 | * references to the base tree. These are stored with invalid values and |
| 1972 | * must be updated now. /__symbols__ contains a list of all labels in |
| 1973 | * the base tree, and /__fixups__ describes all nodes in the overlay |
| 1974 | * that contain external phandle references. |
| 1975 | * We also take this opportunity to update all /fragment@X/__overlay__/ |
| 1976 | * prefixes in the overlay's /__symbols__ node to the correct path that |
| 1977 | * the fragment will be placed in later, since this is the only step |
| 1978 | * where we have all necessary information for that easily available. |
| 1979 | */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1980 | struct device_tree_node *symbols = dt_find_node_by_path(tree, |
| 1981 | "/__symbols__", NULL, NULL, 0); |
| 1982 | struct device_tree_node *fixups = dt_find_node_by_path(overlay, |
| 1983 | "/__fixups__", NULL, NULL, 0); |
| 1984 | struct device_tree_node *overlay_symbols = dt_find_node_by_path(overlay, |
| 1985 | "/__symbols__", NULL, NULL, 0); |
| 1986 | if (fixups && dt_fixup_all_externals(tree, symbols, overlay, |
| 1987 | fixups, overlay_symbols) < 0) { |
Elyes HAOUAS | 8b5841e | 2022-02-08 21:23:12 +0100 | [diff] [blame] | 1988 | printk(BIOS_ERR, "cannot match external fixups from overlay\n"); |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1989 | return -1; |
| 1990 | } |
| 1991 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 1992 | /* After all this fixing up, we can finally merge overlay into the tree |
| 1993 | (one fragment at a time, because for some reason it's split up). */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1994 | struct device_tree_node *fragment; |
| 1995 | list_for_each(fragment, overlay->root->children, list_node) |
| 1996 | if (dt_import_fragment(tree, fragment, overlay_symbols) < 0) { |
Elyes HAOUAS | 8b5841e | 2022-02-08 21:23:12 +0100 | [diff] [blame] | 1997 | printk(BIOS_ERR, "bad DT fragment '%s'\n", |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 1998 | fragment->name); |
| 1999 | return -1; |
| 2000 | } |
| 2001 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 2002 | /* |
| 2003 | * We need to also update /__symbols__ to include labels from this |
| 2004 | * overlay, in case we want to load further overlays with external |
| 2005 | * phandle references to it. If the base tree already has a /__symbols__ |
| 2006 | * we merge them together, otherwise we just insert the overlay's |
| 2007 | * /__symbols__ node into the base tree root. |
| 2008 | */ |
Julius Werner | 735ddc9 | 2019-05-07 17:05:28 -0700 | [diff] [blame] | 2009 | if (overlay_symbols) { |
| 2010 | if (symbols) |
| 2011 | dt_copy_subtree(symbols, overlay_symbols, 0); |
| 2012 | else |
| 2013 | list_insert_after(&overlay_symbols->list_node, |
| 2014 | &tree->root->children); |
| 2015 | } |
| 2016 | |
| 2017 | return 0; |
| 2018 | } |