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