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