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