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