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> |
| 21 | #include <device_tree.h> |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 22 | #include <endian.h> |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 23 | #include <stdint.h> |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 24 | #include <string.h> |
| 25 | #include <stddef.h> |
| 26 | #include <stdlib.h> |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * Functions for picking apart flattened trees. |
| 30 | */ |
| 31 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 32 | int fdt_next_property(const void *blob, uint32_t offset, |
| 33 | struct fdt_property *prop) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 34 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 35 | struct fdt_header *header = (struct fdt_header *)blob; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 36 | uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset); |
| 37 | |
| 38 | int index = 0; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 39 | if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 40 | return 0; |
| 41 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 42 | uint32_t size = be32toh(ptr[index++]); |
| 43 | uint32_t name_offset = be32toh(ptr[index++]); |
| 44 | name_offset += be32toh(header->strings_offset); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 45 | |
| 46 | if (prop) { |
| 47 | prop->name = (char *)((uint8_t *)blob + name_offset); |
| 48 | prop->data = &ptr[index]; |
| 49 | prop->size = size; |
| 50 | } |
| 51 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 52 | index += DIV_ROUND_UP(size, sizeof(uint32_t)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 53 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 54 | return index * sizeof(uint32_t); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 57 | 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] | 58 | { |
| 59 | uint8_t *ptr = ((uint8_t *)blob) + offset; |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 60 | if (be32dec(ptr) != FDT_TOKEN_BEGIN_NODE) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 61 | return 0; |
| 62 | |
| 63 | ptr += 4; |
| 64 | if (name) |
| 65 | *name = (char *)ptr; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 66 | return ALIGN_UP(strlen((char *)ptr) + 1, sizeof(uint32_t)) + 4; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 69 | static int dt_prop_is_phandle(struct device_tree_property *prop) |
| 70 | { |
| 71 | return !(strcmp("phandle", prop->prop.name) && |
| 72 | strcmp("linux,phandle", prop->prop.name)); |
| 73 | } |
| 74 | |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 75 | |
| 76 | |
| 77 | /* |
| 78 | * Functions for printing flattened trees. |
| 79 | */ |
| 80 | |
| 81 | static void print_indent(int depth) |
| 82 | { |
| 83 | while (depth--) |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 84 | printk(BIOS_DEBUG, " "); |
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 | { |
| 89 | print_indent(depth); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 90 | printk(BIOS_DEBUG, "prop \"%s\" (%d bytes).\n", prop->name, prop->size); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 91 | print_indent(depth + 1); |
| 92 | for (int i = 0; i < MIN(25, prop->size); i++) { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 93 | printk(BIOS_DEBUG, "%02x ", ((uint8_t *)prop->data)[i]); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 94 | } |
| 95 | if (prop->size > 25) |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 96 | printk(BIOS_DEBUG, "..."); |
| 97 | printk(BIOS_DEBUG, "\n"); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 100 | 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] | 101 | { |
| 102 | int offset = start_offset; |
| 103 | const char *name; |
| 104 | int size; |
| 105 | |
| 106 | size = fdt_node_name(blob, offset, &name); |
| 107 | if (!size) |
| 108 | return 0; |
| 109 | offset += size; |
| 110 | |
| 111 | print_indent(depth); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 112 | printk(BIOS_DEBUG, "name = %s\n", name); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 113 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 114 | struct fdt_property prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 115 | while ((size = fdt_next_property(blob, offset, &prop))) { |
| 116 | print_property(&prop, depth + 1); |
| 117 | |
| 118 | offset += size; |
| 119 | } |
| 120 | |
| 121 | while ((size = print_flat_node(blob, offset, depth + 1))) |
| 122 | offset += size; |
| 123 | |
| 124 | return offset - start_offset + sizeof(uint32_t); |
| 125 | } |
| 126 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 127 | void fdt_print_node(const void *blob, uint32_t offset) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 128 | { |
| 129 | print_flat_node(blob, offset, 0); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | |
| 134 | /* |
| 135 | * A utility function to skip past nodes in flattened trees. |
| 136 | */ |
| 137 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 138 | int fdt_skip_node(const void *blob, uint32_t start_offset) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 139 | { |
| 140 | int offset = start_offset; |
| 141 | int size; |
| 142 | |
| 143 | const char *name; |
| 144 | size = fdt_node_name(blob, offset, &name); |
| 145 | if (!size) |
| 146 | return 0; |
| 147 | offset += size; |
| 148 | |
| 149 | while ((size = fdt_next_property(blob, offset, NULL))) |
| 150 | offset += size; |
| 151 | |
| 152 | while ((size = fdt_skip_node(blob, offset))) |
| 153 | offset += size; |
| 154 | |
| 155 | return offset - start_offset + sizeof(uint32_t); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | /* |
| 161 | * Functions to turn a flattened tree into an unflattened one. |
| 162 | */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 163 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 164 | static int fdt_unflatten_node(const void *blob, uint32_t start_offset, |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 165 | struct device_tree *tree, |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 166 | struct device_tree_node **new_node) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 167 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 168 | struct list_node *last; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 169 | int offset = start_offset; |
| 170 | const char *name; |
| 171 | int size; |
| 172 | |
| 173 | size = fdt_node_name(blob, offset, &name); |
| 174 | if (!size) |
| 175 | return 0; |
| 176 | offset += size; |
| 177 | |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 178 | struct device_tree_node *node = xzalloc(sizeof(*node)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 179 | *new_node = node; |
| 180 | node->name = name; |
| 181 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 182 | struct fdt_property fprop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 183 | last = &node->properties; |
| 184 | while ((size = fdt_next_property(blob, offset, &fprop))) { |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 185 | struct device_tree_property *prop = xzalloc(sizeof(*prop)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 186 | prop->prop = fprop; |
| 187 | |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 188 | if (dt_prop_is_phandle(prop)) { |
| 189 | node->phandle = be32dec(prop->prop.data); |
| 190 | if (node->phandle > tree->max_phandle) |
| 191 | tree->max_phandle = node->phandle; |
| 192 | } |
| 193 | |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 194 | list_insert_after(&prop->list_node, last); |
| 195 | last = &prop->list_node; |
| 196 | |
| 197 | offset += size; |
| 198 | } |
| 199 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 200 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 201 | last = &node->children; |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 202 | while ((size = fdt_unflatten_node(blob, offset, tree, &child))) { |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 203 | list_insert_after(&child->list_node, last); |
| 204 | last = &child->list_node; |
| 205 | |
| 206 | offset += size; |
| 207 | } |
| 208 | |
| 209 | return offset - start_offset + sizeof(uint32_t); |
| 210 | } |
| 211 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 212 | static int fdt_unflatten_map_entry(const void *blob, uint32_t offset, |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 213 | struct device_tree_reserve_map_entry **new) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 214 | { |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 215 | const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset); |
| 216 | const uint64_t start = be64toh(ptr[0]); |
| 217 | const uint64_t size = be64toh(ptr[1]); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 218 | |
| 219 | if (!size) |
| 220 | return 0; |
| 221 | |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 222 | struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry)); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 223 | *new = entry; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 224 | entry->start = start; |
| 225 | entry->size = size; |
| 226 | |
| 227 | return sizeof(uint64_t) * 2; |
| 228 | } |
| 229 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 230 | struct device_tree *fdt_unflatten(const void *blob) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 231 | { |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 232 | struct device_tree *tree = xzalloc(sizeof(*tree)); |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 233 | const struct fdt_header *header = (const struct fdt_header *)blob; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 234 | tree->header = header; |
| 235 | |
Julius Werner | 73eaec8 | 2019-05-03 17:58:07 -0700 | [diff] [blame] | 236 | uint32_t magic = be32toh(header->magic); |
| 237 | uint32_t version = be32toh(header->version); |
| 238 | uint32_t last_comp_version = be32toh(header->last_comp_version); |
| 239 | |
| 240 | if (magic != FDT_HEADER_MAGIC) { |
| 241 | printk(BIOS_DEBUG, "Invalid device tree magic %#.8x!\n", magic); |
| 242 | return NULL; |
| 243 | } |
| 244 | if (last_comp_version > FDT_SUPPORTED_VERSION) { |
| 245 | printk(BIOS_DEBUG, "Unsupported device tree version %u(>=%u)\n", |
| 246 | version, last_comp_version); |
| 247 | return NULL; |
| 248 | } |
| 249 | if (version > FDT_SUPPORTED_VERSION) |
| 250 | printk(BIOS_DEBUG, |
| 251 | "NOTE: FDT version %u too new, should add support!\n", |
| 252 | version); |
| 253 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 254 | uint32_t struct_offset = be32toh(header->structure_offset); |
| 255 | uint32_t strings_offset = be32toh(header->strings_offset); |
| 256 | uint32_t reserve_offset = be32toh(header->reserve_map_offset); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 257 | uint32_t min_offset = 0; |
| 258 | min_offset = MIN(struct_offset, strings_offset); |
| 259 | min_offset = MIN(min_offset, reserve_offset); |
| 260 | // Assume everything up to the first non-header component is part of |
| 261 | // the header and needs to be preserved. This will protect us against |
| 262 | // new elements being added in the future. |
| 263 | tree->header_size = min_offset; |
| 264 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 265 | struct device_tree_reserve_map_entry *entry; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 266 | uint32_t offset = reserve_offset; |
| 267 | int size; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 268 | struct list_node *last = &tree->reserve_map; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 269 | while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) { |
| 270 | list_insert_after(&entry->list_node, last); |
| 271 | last = &entry->list_node; |
| 272 | |
| 273 | offset += size; |
| 274 | } |
| 275 | |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 276 | fdt_unflatten_node(blob, struct_offset, tree, &tree->root); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 277 | |
| 278 | return tree; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | |
| 283 | /* |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 284 | * 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] | 285 | */ |
| 286 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 287 | static void dt_flat_prop_size(struct device_tree_property *prop, |
| 288 | uint32_t *struct_size, uint32_t *strings_size) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 289 | { |
| 290 | // Starting token. |
| 291 | *struct_size += sizeof(uint32_t); |
| 292 | // Size. |
| 293 | *struct_size += sizeof(uint32_t); |
| 294 | // Name offset. |
| 295 | *struct_size += sizeof(uint32_t); |
| 296 | // Property value. |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 297 | *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 298 | |
| 299 | // Property name. |
| 300 | *strings_size += strlen(prop->prop.name) + 1; |
| 301 | } |
| 302 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 303 | static void dt_flat_node_size(struct device_tree_node *node, |
| 304 | uint32_t *struct_size, uint32_t *strings_size) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 305 | { |
| 306 | // Starting token. |
| 307 | *struct_size += sizeof(uint32_t); |
| 308 | // Node name. |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 309 | *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 310 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 311 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 312 | list_for_each(prop, node->properties, list_node) |
| 313 | dt_flat_prop_size(prop, struct_size, strings_size); |
| 314 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 315 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 316 | list_for_each(child, node->children, list_node) |
| 317 | dt_flat_node_size(child, struct_size, strings_size); |
| 318 | |
| 319 | // End token. |
| 320 | *struct_size += sizeof(uint32_t); |
| 321 | } |
| 322 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 323 | uint32_t dt_flat_size(const struct device_tree *tree) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 324 | { |
| 325 | uint32_t size = tree->header_size; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 326 | struct device_tree_reserve_map_entry *entry; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 327 | list_for_each(entry, tree->reserve_map, list_node) |
| 328 | size += sizeof(uint64_t) * 2; |
| 329 | size += sizeof(uint64_t) * 2; |
| 330 | |
| 331 | uint32_t struct_size = 0; |
| 332 | uint32_t strings_size = 0; |
| 333 | dt_flat_node_size(tree->root, &struct_size, &strings_size); |
| 334 | |
| 335 | size += struct_size; |
| 336 | // End token. |
| 337 | size += sizeof(uint32_t); |
| 338 | |
| 339 | size += strings_size; |
| 340 | |
| 341 | return size; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | |
| 346 | /* |
| 347 | * Functions to flatten a device tree. |
| 348 | */ |
| 349 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 350 | 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] | 351 | void **map_start) |
| 352 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 353 | ((uint64_t *)*map_start)[0] = htobe64(entry->start); |
| 354 | ((uint64_t *)*map_start)[1] = htobe64(entry->size); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 355 | *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2; |
| 356 | } |
| 357 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 358 | static void dt_flatten_prop(struct device_tree_property *prop, |
| 359 | void **struct_start, void *strings_base, |
| 360 | void **strings_start) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 361 | { |
| 362 | uint8_t *dstruct = (uint8_t *)*struct_start; |
| 363 | uint8_t *dstrings = (uint8_t *)*strings_start; |
| 364 | |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 365 | be32enc(dstruct, FDT_TOKEN_PROPERTY); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 366 | dstruct += sizeof(uint32_t); |
| 367 | |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 368 | be32enc(dstruct, prop->prop.size); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 369 | dstruct += sizeof(uint32_t); |
| 370 | |
| 371 | uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base; |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 372 | be32enc(dstruct, name_offset); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 373 | dstruct += sizeof(uint32_t); |
| 374 | |
| 375 | strcpy((char *)dstrings, prop->prop.name); |
| 376 | dstrings += strlen(prop->prop.name) + 1; |
| 377 | |
| 378 | memcpy(dstruct, prop->prop.data, prop->prop.size); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 379 | dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 380 | |
| 381 | *struct_start = dstruct; |
| 382 | *strings_start = dstrings; |
| 383 | } |
| 384 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 385 | static void dt_flatten_node(const struct device_tree_node *node, |
| 386 | void **struct_start, void *strings_base, |
| 387 | void **strings_start) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 388 | { |
| 389 | uint8_t *dstruct = (uint8_t *)*struct_start; |
| 390 | uint8_t *dstrings = (uint8_t *)*strings_start; |
| 391 | |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 392 | be32enc(dstruct, FDT_TOKEN_BEGIN_NODE); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 393 | dstruct += sizeof(uint32_t); |
| 394 | |
| 395 | strcpy((char *)dstruct, node->name); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 396 | dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 397 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 398 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 399 | list_for_each(prop, node->properties, list_node) |
| 400 | dt_flatten_prop(prop, (void **)&dstruct, strings_base, |
| 401 | (void **)&dstrings); |
| 402 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 403 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 404 | list_for_each(child, node->children, list_node) |
| 405 | dt_flatten_node(child, (void **)&dstruct, strings_base, |
| 406 | (void **)&dstrings); |
| 407 | |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 408 | be32enc(dstruct, FDT_TOKEN_END_NODE); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 409 | dstruct += sizeof(uint32_t); |
| 410 | |
| 411 | *struct_start = dstruct; |
| 412 | *strings_start = dstrings; |
| 413 | } |
| 414 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 415 | void dt_flatten(const struct device_tree *tree, void *start_dest) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 416 | { |
| 417 | uint8_t *dest = (uint8_t *)start_dest; |
| 418 | |
| 419 | memcpy(dest, tree->header, tree->header_size); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 420 | struct fdt_header *header = (struct fdt_header *)dest; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 421 | dest += tree->header_size; |
| 422 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 423 | struct device_tree_reserve_map_entry *entry; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 424 | list_for_each(entry, tree->reserve_map, list_node) |
| 425 | dt_flatten_map_entry(entry, (void **)&dest); |
| 426 | ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0; |
| 427 | dest += sizeof(uint64_t) * 2; |
| 428 | |
| 429 | uint32_t struct_size = 0; |
| 430 | uint32_t strings_size = 0; |
| 431 | dt_flat_node_size(tree->root, &struct_size, &strings_size); |
| 432 | |
| 433 | uint8_t *struct_start = dest; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 434 | header->structure_offset = htobe32(dest - (uint8_t *)start_dest); |
| 435 | header->structure_size = htobe32(struct_size); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 436 | dest += struct_size; |
| 437 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 438 | *((uint32_t *)dest) = htobe32(FDT_TOKEN_END); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 439 | dest += sizeof(uint32_t); |
| 440 | |
| 441 | uint8_t *strings_start = dest; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 442 | header->strings_offset = htobe32(dest - (uint8_t *)start_dest); |
| 443 | header->strings_size = htobe32(strings_size); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 444 | dest += strings_size; |
| 445 | |
| 446 | dt_flatten_node(tree->root, (void **)&struct_start, strings_start, |
| 447 | (void **)&strings_start); |
| 448 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 449 | header->totalsize = htobe32(dest - (uint8_t *)start_dest); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | |
| 453 | |
| 454 | /* |
| 455 | * Functions for printing a non-flattened device tree. |
| 456 | */ |
| 457 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 458 | static void print_node(const struct device_tree_node *node, int depth) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 459 | { |
| 460 | print_indent(depth); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 461 | printk(BIOS_DEBUG, "name = %s\n", node->name); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 462 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 463 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 464 | list_for_each(prop, node->properties, list_node) |
| 465 | print_property(&prop->prop, depth + 1); |
| 466 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 467 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 468 | list_for_each(child, node->children, list_node) |
| 469 | print_node(child, depth + 1); |
| 470 | } |
| 471 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 472 | void dt_print_node(const struct device_tree_node *node) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 473 | { |
| 474 | print_node(node, 0); |
| 475 | } |
| 476 | |
| 477 | |
| 478 | |
| 479 | /* |
| 480 | * Functions for reading and manipulating an unflattened device tree. |
| 481 | */ |
| 482 | |
| 483 | /* |
| 484 | * Read #address-cells and #size-cells properties from a node. |
| 485 | * |
| 486 | * @param node The device tree node to read from. |
| 487 | * @param addrcp Pointer to store #address-cells in, skipped if NULL. |
| 488 | * @param sizecp Pointer to store #size-cells in, skipped if NULL. |
| 489 | */ |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 490 | void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp, |
| 491 | u32 *sizecp) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 492 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 493 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 494 | list_for_each(prop, node->properties, list_node) { |
| 495 | if (addrcp && !strcmp("#address-cells", prop->prop.name)) |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 496 | *addrcp = be32dec(prop->prop.data); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 497 | if (sizecp && !strcmp("#size-cells", prop->prop.name)) |
Julius Werner | a5ea3a2 | 2019-05-07 17:38:12 -0700 | [diff] [blame] | 498 | *sizecp = be32dec(prop->prop.data); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * Find a node from a device tree path, relative to a parent node. |
| 504 | * |
| 505 | * @param parent The node from which to start the relative path lookup. |
| 506 | * @param path An array of path component strings that will be looked |
Elyes HAOUAS | e3e3f4f | 2018-06-29 21:41:41 +0200 | [diff] [blame] | 507 | * up in order to find the node. Must be terminated with |
| 508 | * a NULL pointer. Example: {'firmware', 'coreboot', NULL} |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 509 | * @param addrcp Pointer that will be updated with any #address-cells |
Elyes HAOUAS | e3e3f4f | 2018-06-29 21:41:41 +0200 | [diff] [blame] | 510 | * value found in the path. May be NULL to ignore. |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 511 | * @param sizecp Pointer that will be updated with any #size-cells |
Elyes HAOUAS | e3e3f4f | 2018-06-29 21:41:41 +0200 | [diff] [blame] | 512 | * value found in the path. May be NULL to ignore. |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 513 | * @param create 1: Create node(s) if not found. 0: Return NULL instead. |
| 514 | * @return The found/created node, or NULL. |
| 515 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 516 | struct device_tree_node *dt_find_node(struct device_tree_node *parent, |
| 517 | const char **path, u32 *addrcp, |
| 518 | u32 *sizecp, int create) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 519 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 520 | struct device_tree_node *node, *found = NULL; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 521 | |
| 522 | // Update #address-cells and #size-cells for this level. |
| 523 | dt_read_cell_props(parent, addrcp, sizecp); |
| 524 | |
| 525 | if (!*path) |
| 526 | return parent; |
| 527 | |
| 528 | // Find the next node in the path, if it exists. |
| 529 | list_for_each(node, parent->children, list_node) { |
| 530 | if (!strcmp(node->name, *path)) { |
| 531 | found = node; |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | // Otherwise create it or return NULL. |
| 537 | if (!found) { |
| 538 | if (!create) |
| 539 | return NULL; |
| 540 | |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 541 | found = malloc(sizeof(*found)); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 542 | if (!found) |
| 543 | return NULL; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 544 | found->name = strdup(*path); |
| 545 | if (!found->name) |
| 546 | return NULL; |
| 547 | |
| 548 | list_insert_after(&found->list_node, &parent->children); |
| 549 | } |
| 550 | |
| 551 | return dt_find_node(found, path + 1, addrcp, sizecp, create); |
| 552 | } |
| 553 | |
| 554 | /* |
Julius Werner | f36d53c | 2019-05-03 18:23:34 -0700 | [diff] [blame] | 555 | * Find a node in the tree from a string device tree path. |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 556 | * |
Julius Werner | f36d53c | 2019-05-03 18:23:34 -0700 | [diff] [blame] | 557 | * @param tree The device tree to search. |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 558 | * @param path A string representing a path in the device tree, with |
Julius Werner | fbec63d | 2019-05-03 18:29:28 -0700 | [diff] [blame^] | 559 | * nodes separated by '/'. Example: "/firmware/coreboot" |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 560 | * @param addrcp Pointer that will be updated with any #address-cells |
| 561 | * value found in the path. May be NULL to ignore. |
| 562 | * @param sizecp Pointer that will be updated with any #size-cells |
| 563 | * value found in the path. May be NULL to ignore. |
| 564 | * @param create 1: Create node(s) if not found. 0: Return NULL instead. |
| 565 | * @return The found/created node, or NULL. |
| 566 | * |
| 567 | * It is the caller responsibility to provide the correct path string, namely |
Julius Werner | fbec63d | 2019-05-03 18:29:28 -0700 | [diff] [blame^] | 568 | * starting with a '/', not ending in a '/' and not having "//" anywhere in it. |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 569 | */ |
Julius Werner | f36d53c | 2019-05-03 18:23:34 -0700 | [diff] [blame] | 570 | 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] | 571 | const char *path, u32 *addrcp, |
| 572 | u32 *sizecp, int create) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 573 | { |
Julius Werner | fbec63d | 2019-05-03 18:29:28 -0700 | [diff] [blame^] | 574 | char *dup_path = strdup(&path[1]); /* remove leading '/' */ |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 575 | /* Hopefully enough depth for any node. */ |
| 576 | const char *path_array[15]; |
| 577 | int i; |
| 578 | char *next_slash; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 579 | struct device_tree_node *node = NULL; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 580 | |
| 581 | if (!dup_path) |
| 582 | return NULL; |
| 583 | |
| 584 | next_slash = dup_path; |
| 585 | path_array[0] = dup_path; |
| 586 | for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) { |
| 587 | |
| 588 | next_slash = strchr(next_slash, '/'); |
| 589 | if (!next_slash) |
| 590 | break; |
| 591 | |
| 592 | *next_slash++ = '\0'; |
| 593 | path_array[i] = next_slash; |
| 594 | } |
| 595 | |
| 596 | if (!next_slash) { |
| 597 | path_array[i] = NULL; |
Julius Werner | f36d53c | 2019-05-03 18:23:34 -0700 | [diff] [blame] | 598 | node = dt_find_node(tree->root, path_array, |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 599 | addrcp, sizecp, create); |
| 600 | } |
| 601 | |
| 602 | free(dup_path); |
| 603 | return node; |
| 604 | } |
| 605 | |
Julius Werner | 6702b68 | 2019-05-03 18:13:53 -0700 | [diff] [blame] | 606 | struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root, |
| 607 | uint32_t phandle) |
| 608 | { |
| 609 | if (!root) |
| 610 | return NULL; |
| 611 | |
| 612 | if (root->phandle == phandle) |
| 613 | return root; |
| 614 | |
| 615 | struct device_tree_node *node; |
| 616 | struct device_tree_node *result; |
| 617 | list_for_each(node, root->children, list_node) { |
| 618 | result = dt_find_node_by_phandle(node, phandle); |
| 619 | if (result) |
| 620 | return result; |
| 621 | } |
| 622 | |
| 623 | return NULL; |
| 624 | } |
| 625 | |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 626 | /* |
| 627 | * Check if given node is compatible. |
| 628 | * |
| 629 | * @param node The node which is to be checked for compatible property. |
| 630 | * @param compat The compatible string to match. |
| 631 | * @return 1 = compatible, 0 = not compatible. |
| 632 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 633 | static int dt_check_compat_match(struct device_tree_node *node, |
| 634 | const char *compat) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 635 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 636 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 637 | |
| 638 | list_for_each(prop, node->properties, list_node) { |
| 639 | if (!strcmp("compatible", prop->prop.name)) { |
| 640 | size_t bytes = prop->prop.size; |
| 641 | const char *str = prop->prop.data; |
| 642 | while (bytes > 0) { |
| 643 | if (!strncmp(compat, str, bytes)) |
| 644 | return 1; |
| 645 | size_t len = strnlen(str, bytes) + 1; |
| 646 | if (bytes <= len) |
| 647 | break; |
| 648 | str += len; |
| 649 | bytes -= len; |
| 650 | } |
| 651 | break; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * Find a node from a compatible string, in the subtree of a parent node. |
| 660 | * |
| 661 | * @param parent The parent node under which to look. |
| 662 | * @param compat The compatible string to find. |
| 663 | * @return The found node, or NULL. |
| 664 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 665 | struct device_tree_node *dt_find_compat(struct device_tree_node *parent, |
| 666 | const char *compat) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 667 | { |
| 668 | // Check if the parent node itself is compatible. |
| 669 | if (dt_check_compat_match(parent, compat)) |
| 670 | return parent; |
| 671 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 672 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 673 | list_for_each(child, parent->children, list_node) { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 674 | struct device_tree_node *found = dt_find_compat(child, compat); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 675 | if (found) |
| 676 | return found; |
| 677 | } |
| 678 | |
| 679 | return NULL; |
| 680 | } |
| 681 | |
| 682 | /* |
| 683 | * Find the next compatible child of a given parent. All children upto the |
| 684 | * child passed in by caller are ignored. If child is NULL, it considers all the |
| 685 | * children to find the first child which is compatible. |
| 686 | * |
| 687 | * @param parent The parent node under which to look. |
| 688 | * @param child The child node to start search from (exclusive). If NULL |
| 689 | * consider all children. |
| 690 | * @param compat The compatible string to find. |
| 691 | * @return The found node, or NULL. |
| 692 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 693 | struct device_tree_node * |
| 694 | dt_find_next_compat_child(struct device_tree_node *parent, |
| 695 | struct device_tree_node *child, |
| 696 | const char *compat) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 697 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 698 | struct device_tree_node *next; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 699 | int ignore = 0; |
| 700 | |
| 701 | if (child) |
| 702 | ignore = 1; |
| 703 | |
| 704 | list_for_each(next, parent->children, list_node) { |
| 705 | if (ignore) { |
| 706 | if (child == next) |
| 707 | ignore = 0; |
| 708 | continue; |
| 709 | } |
| 710 | |
| 711 | if (dt_check_compat_match(next, compat)) |
| 712 | return next; |
| 713 | } |
| 714 | |
| 715 | return NULL; |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | * Find a node with matching property value, in the subtree of a parent node. |
| 720 | * |
| 721 | * @param parent The parent node under which to look. |
| 722 | * @param name The property name to look for. |
| 723 | * @param data The property value to look for. |
| 724 | * @param size The property size. |
| 725 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 726 | struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent, |
| 727 | const char *name, void *data, |
| 728 | size_t size) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 729 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 730 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 731 | |
| 732 | /* Check if parent itself has the required property value. */ |
| 733 | list_for_each(prop, parent->properties, list_node) { |
| 734 | if (!strcmp(name, prop->prop.name)) { |
| 735 | size_t bytes = prop->prop.size; |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 736 | const void *prop_data = prop->prop.data; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 737 | if (size != bytes) |
| 738 | break; |
| 739 | if (!memcmp(data, prop_data, size)) |
| 740 | return parent; |
| 741 | break; |
| 742 | } |
| 743 | } |
| 744 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 745 | struct device_tree_node *child; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 746 | list_for_each(child, parent->children, list_node) { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 747 | struct device_tree_node *found = dt_find_prop_value(child, name, |
| 748 | data, size); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 749 | if (found) |
| 750 | return found; |
| 751 | } |
| 752 | return NULL; |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | * Write an arbitrary sized big-endian integer into a pointer. |
| 757 | * |
| 758 | * @param dest Pointer to the DT property data buffer to write. |
Elyes HAOUAS | 1ec7644 | 2018-08-07 12:20:04 +0200 | [diff] [blame] | 759 | * @param src The integer to write (in CPU endianness). |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 760 | * @param length the length of the destination integer in bytes. |
| 761 | */ |
| 762 | void dt_write_int(u8 *dest, u64 src, size_t length) |
| 763 | { |
| 764 | while (length--) { |
| 765 | dest[length] = (u8)src; |
| 766 | src >>= 8; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | /* |
Patrick Rudolph | 5ccc731 | 2018-05-30 15:05:28 +0200 | [diff] [blame] | 771 | * Delete a property by name in a given node if it exists. |
| 772 | * |
| 773 | * @param node The device tree node to operate on. |
| 774 | * @param name The name of the property to delete. |
| 775 | */ |
| 776 | void dt_delete_prop(struct device_tree_node *node, const char *name) |
| 777 | { |
| 778 | struct device_tree_property *prop; |
| 779 | |
| 780 | list_for_each(prop, node->properties, list_node) { |
| 781 | if (!strcmp(prop->prop.name, name)) { |
| 782 | list_remove(&prop->list_node); |
| 783 | return; |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | /* |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 789 | * Add an arbitrary property to a node, or update it if it already exists. |
| 790 | * |
| 791 | * @param node The device tree node to add to. |
| 792 | * @param name The name of the new property. |
| 793 | * @param data The raw data blob to be stored in the property. |
| 794 | * @param size The size of data in bytes. |
| 795 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 796 | void dt_add_bin_prop(struct device_tree_node *node, const char *name, |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 797 | const void *data, size_t size) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 798 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 799 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 800 | |
| 801 | list_for_each(prop, node->properties, list_node) { |
| 802 | if (!strcmp(prop->prop.name, name)) { |
| 803 | prop->prop.data = data; |
| 804 | prop->prop.size = size; |
| 805 | return; |
| 806 | } |
| 807 | } |
| 808 | |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 809 | prop = xzalloc(sizeof(*prop)); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 810 | list_insert_after(&prop->list_node, &node->properties); |
| 811 | prop->prop.name = name; |
| 812 | prop->prop.data = data; |
| 813 | prop->prop.size = size; |
| 814 | } |
| 815 | |
| 816 | /* |
| 817 | * Find given string property in a node and return its content. |
| 818 | * |
| 819 | * @param node The device tree node to search. |
| 820 | * @param name The name of the property. |
| 821 | * @return The found string, or NULL. |
| 822 | */ |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 823 | const char *dt_find_string_prop(const struct device_tree_node *node, |
| 824 | const char *name) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 825 | { |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 826 | const void *content; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 827 | size_t size; |
| 828 | |
| 829 | dt_find_bin_prop(node, name, &content, &size); |
| 830 | |
| 831 | return content; |
| 832 | } |
| 833 | |
| 834 | /* |
| 835 | * Find given property in a node. |
| 836 | * |
| 837 | * @param node The device tree node to search. |
| 838 | * @param name The name of the property. |
| 839 | * @param data Pointer to return raw data blob in the property. |
| 840 | * @param size Pointer to return the size of data in bytes. |
| 841 | */ |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 842 | void dt_find_bin_prop(const struct device_tree_node *node, const char *name, |
| 843 | const void **data, size_t *size) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 844 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 845 | struct device_tree_property *prop; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 846 | |
| 847 | *data = NULL; |
| 848 | *size = 0; |
| 849 | |
| 850 | list_for_each(prop, node->properties, list_node) { |
| 851 | if (!strcmp(prop->prop.name, name)) { |
| 852 | *data = prop->prop.data; |
| 853 | *size = prop->prop.size; |
| 854 | return; |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | /* |
| 860 | * Add a string property to a node, or update it if it already exists. |
| 861 | * |
| 862 | * @param node The device tree node to add to. |
| 863 | * @param name The name of the new property. |
| 864 | * @param str The zero-terminated string to be stored in the property. |
| 865 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 866 | 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] | 867 | const char *str) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 868 | { |
| 869 | dt_add_bin_prop(node, name, str, strlen(str) + 1); |
| 870 | } |
| 871 | |
| 872 | /* |
| 873 | * Add a 32-bit integer property to a node, or update it if it already exists. |
| 874 | * |
| 875 | * @param node The device tree node to add to. |
| 876 | * @param name The name of the new property. |
| 877 | * @param val The integer to be stored in the property. |
| 878 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 879 | 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] | 880 | { |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 881 | u32 *val_ptr = xmalloc(sizeof(val)); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 882 | *val_ptr = htobe32(val); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 883 | dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr)); |
| 884 | } |
| 885 | |
| 886 | /* |
Patrick Rudolph | 3fca4ed | 2018-08-10 10:12:35 +0200 | [diff] [blame] | 887 | * Add a 64-bit integer property to a node, or update it if it already exists. |
| 888 | * |
| 889 | * @param node The device tree node to add to. |
| 890 | * @param name The name of the new property. |
| 891 | * @param val The integer to be stored in the property. |
| 892 | */ |
| 893 | void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val) |
| 894 | { |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 895 | u64 *val_ptr = xmalloc(sizeof(val)); |
Patrick Rudolph | 3fca4ed | 2018-08-10 10:12:35 +0200 | [diff] [blame] | 896 | *val_ptr = htobe64(val); |
| 897 | dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr)); |
| 898 | } |
| 899 | |
| 900 | /* |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 901 | * Add a 'reg' address list property to a node, or update it if it exists. |
| 902 | * |
| 903 | * @param node The device tree node to add to. |
| 904 | * @param addrs Array of address values to be stored in the property. |
| 905 | * @param sizes Array of corresponding size values to 'addrs'. |
| 906 | * @param count Number of values in 'addrs' and 'sizes' (must be equal). |
| 907 | * @param addr_cells Value of #address-cells property valid for this node. |
| 908 | * @param size_cells Value of #size-cells property valid for this node. |
| 909 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 910 | 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] | 911 | int count, u32 addr_cells, u32 size_cells) |
| 912 | { |
| 913 | int i; |
| 914 | size_t length = (addr_cells + size_cells) * sizeof(u32) * count; |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 915 | u8 *data = xmalloc(length); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 916 | u8 *cur = data; |
| 917 | |
| 918 | for (i = 0; i < count; i++) { |
| 919 | dt_write_int(cur, addrs[i], addr_cells * sizeof(u32)); |
| 920 | cur += addr_cells * sizeof(u32); |
| 921 | dt_write_int(cur, sizes[i], size_cells * sizeof(u32)); |
| 922 | cur += size_cells * sizeof(u32); |
| 923 | } |
| 924 | |
| 925 | dt_add_bin_prop(node, "reg", data, length); |
| 926 | } |
| 927 | |
| 928 | /* |
| 929 | * Fixups to apply to a kernel's device tree before booting it. |
| 930 | */ |
| 931 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 932 | struct list_node device_tree_fixups; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 933 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 934 | int dt_apply_fixups(struct device_tree *tree) |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 935 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 936 | struct device_tree_fixup *fixup; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 937 | list_for_each(fixup, device_tree_fixups, list_node) { |
| 938 | assert(fixup->fixup); |
| 939 | if (fixup->fixup(fixup, tree)) |
| 940 | return 1; |
| 941 | } |
| 942 | return 0; |
| 943 | } |
| 944 | |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 945 | 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] | 946 | void *data, size_t data_size, int create) |
| 947 | { |
| 948 | char *path_copy, *prop_name; |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 949 | struct device_tree_node *dt_node; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 950 | |
| 951 | path_copy = strdup(path); |
| 952 | |
| 953 | if (!path_copy) { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 954 | printk(BIOS_ERR, "Failed to allocate a copy of path %s\n", |
| 955 | path); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 956 | return 1; |
| 957 | } |
| 958 | |
| 959 | prop_name = strrchr(path_copy, '/'); |
| 960 | if (!prop_name) { |
Patrick Rudolph | 679d624 | 2018-07-11 13:53:04 +0200 | [diff] [blame] | 961 | free(path_copy); |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 962 | printk(BIOS_ERR, "Path %s does not include '/'\n", path); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 963 | return 1; |
| 964 | } |
| 965 | |
| 966 | *prop_name++ = '\0'; /* Separate path from the property name. */ |
| 967 | |
Julius Werner | f36d53c | 2019-05-03 18:23:34 -0700 | [diff] [blame] | 968 | dt_node = dt_find_node_by_path(tree, path_copy, NULL, |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 969 | NULL, create); |
| 970 | |
| 971 | if (!dt_node) { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 972 | printk(BIOS_ERR, "Failed to %s %s in the device tree\n", |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 973 | create ? "create" : "find", path_copy); |
Patrick Rudolph | 679d624 | 2018-07-11 13:53:04 +0200 | [diff] [blame] | 974 | free(path_copy); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 975 | return 1; |
| 976 | } |
| 977 | |
| 978 | dt_add_bin_prop(dt_node, prop_name, data, data_size); |
Patrick Rudolph | 679d624 | 2018-07-11 13:53:04 +0200 | [diff] [blame] | 979 | free(path_copy); |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 980 | |
| 981 | return 0; |
| 982 | } |
| 983 | |
| 984 | /* |
| 985 | * Prepare the /reserved-memory/ node. |
| 986 | * |
| 987 | * Technically, this can be called more than one time, to init and/or retrieve |
| 988 | * the node. But dt_add_u32_prop() may leak a bit of memory if you do. |
| 989 | * |
| 990 | * @tree: Device tree to add/retrieve from. |
| 991 | * @return: The /reserved-memory/ node (or NULL, if error). |
| 992 | */ |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 993 | 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] | 994 | { |
Patrick Rudolph | 666c172 | 2018-04-03 09:57:33 +0200 | [diff] [blame] | 995 | struct device_tree_node *reserved; |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 996 | u32 addr = 0, size = 0; |
| 997 | |
Julius Werner | fbec63d | 2019-05-03 18:29:28 -0700 | [diff] [blame^] | 998 | reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr, |
Patrick Rudolph | 67aca3e | 2018-04-12 11:44:43 +0200 | [diff] [blame] | 999 | &size, 1); |
| 1000 | if (!reserved) |
| 1001 | return NULL; |
| 1002 | |
| 1003 | // Binding doc says this should have the same #{address,size}-cells as |
| 1004 | // the root. |
| 1005 | dt_add_u32_prop(reserved, "#address-cells", addr); |
| 1006 | dt_add_u32_prop(reserved, "#size-cells", size); |
| 1007 | // Binding doc says this should be empty (i.e., 1:1 mapping from root). |
| 1008 | dt_add_bin_prop(reserved, "ranges", NULL, 0); |
| 1009 | |
| 1010 | return reserved; |
| 1011 | } |