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