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