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