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