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