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