Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 17 | #include "hf/fdt.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 18 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 19 | #include <libfdt.h> |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 20 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 21 | #include "hf/static_assert.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 22 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 23 | /** Returns pointer to the FDT buffer. */ |
| 24 | const void *fdt_base(const struct fdt *fdt) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 25 | { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 26 | return memiter_base(&fdt->buf); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 27 | } |
| 28 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 29 | /** Returns size of the FDT buffer. */ |
| 30 | size_t fdt_size(const struct fdt *fdt) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 31 | { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 32 | return memiter_size(&fdt->buf); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 33 | } |
| 34 | |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 35 | /** |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 36 | * Extracts total size of the FDT structure from its FDT header. |
| 37 | * Returns true on success, false if header validation failed. |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 38 | */ |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 39 | bool fdt_size_from_header(const void *ptr, size_t *val) |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 40 | { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 41 | if (fdt_check_header(ptr) != 0) { |
| 42 | return false; |
| 43 | } |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 44 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 45 | *val = fdt_totalsize(ptr); |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Initializes `struct fdt` to point to a given buffer. |
| 51 | * Returns true on success, false if FDT validation failed. |
| 52 | */ |
| 53 | bool fdt_init_from_ptr(struct fdt *fdt, const void *ptr, size_t len) |
| 54 | { |
| 55 | if (fdt_check_full(ptr, len) != 0) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | memiter_init(&fdt->buf, ptr, len); |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Initializes `struct fdt` to point to a given buffer. |
| 65 | * Returns true on success, false if FDT validation failed. |
| 66 | */ |
| 67 | bool fdt_init_from_memiter(struct fdt *fdt, const struct memiter *it) |
| 68 | { |
| 69 | return fdt_init_from_ptr(fdt, memiter_base(it), memiter_size(it)); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Invalidates the internal pointer to FDT buffer. |
| 74 | * This is meant to prevent use-after-free bugs. |
| 75 | */ |
| 76 | void fdt_fini(struct fdt *fdt) |
| 77 | { |
| 78 | memiter_init(&fdt->buf, NULL, 0); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Finds a node of a given path in the device tree. |
| 83 | * Unit addresses of components may be omitted but result is undefined if |
| 84 | * the path is not unique. |
| 85 | * Returns true on success, false if not found or an error occurred. |
| 86 | */ |
| 87 | bool fdt_find_node(const struct fdt *fdt, const char *path, |
| 88 | struct fdt_node *node) |
| 89 | { |
| 90 | int offset = fdt_path_offset(fdt_base(fdt), path); |
| 91 | |
| 92 | if (offset < 0) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | *node = (struct fdt_node){.fdt = *fdt, .offset = offset}; |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Retrieves address size for a bus represented in the device tree. |
| 102 | * Result is value of '#address-cells' at `node` multiplied by cell size. |
| 103 | * If '#address-cells' is not found, the default value is 2 cells. |
| 104 | * Returns true on success, false if an error occurred. |
| 105 | */ |
| 106 | bool fdt_address_size(const struct fdt_node *node, size_t *size) |
| 107 | { |
| 108 | int s = fdt_address_cells(fdt_base(&node->fdt), node->offset); |
| 109 | |
| 110 | if (s < 0) { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | *size = (size_t)s * sizeof(uint32_t); |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Retrieves address range size for a bus represented in the device tree. |
| 120 | * Result is value of '#size-cells' at `node` multiplied by cell size. |
| 121 | * If '#size-cells' is not found, the default value is 1 cell. |
| 122 | * Returns true on success, false if an error occurred. |
| 123 | */ |
| 124 | bool fdt_size_size(const struct fdt_node *node, size_t *size) |
| 125 | { |
| 126 | int s = fdt_size_cells(fdt_base(&node->fdt), node->offset); |
| 127 | |
| 128 | if (s < 0) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | *size = (size_t)s * sizeof(uint32_t); |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Retrieves the buffer with value of property `name` at `node`. |
| 138 | * Returns true on success, false if not found or an error occurred. |
| 139 | */ |
| 140 | bool fdt_read_property(const struct fdt_node *node, const char *name, |
| 141 | struct memiter *data) |
| 142 | { |
| 143 | const void *ptr; |
| 144 | int lenp; |
| 145 | |
| 146 | ptr = fdt_getprop(fdt_base(&node->fdt), node->offset, name, &lenp); |
| 147 | if (ptr == NULL) { |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | CHECK(lenp >= 0); |
| 152 | memiter_init(data, ptr, (size_t)lenp); |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Reads the value of property `name` at `node` as a uint. |
| 158 | * The size of the uint is inferred from the size of the property's value. |
| 159 | * Returns true on success, false if property not found or an error occurred. |
| 160 | */ |
| 161 | bool fdt_read_number(const struct fdt_node *node, const char *name, |
| 162 | uint64_t *val) |
| 163 | { |
| 164 | struct memiter data; |
| 165 | |
| 166 | return fdt_read_property(node, name, &data) && |
| 167 | fdt_parse_number(&data, memiter_size(&data), val) && |
| 168 | (memiter_size(&data) == 0); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Parses a uint of given `size` from the beginning of `data`. |
| 173 | * On success returns true and advances `data` by `size` bytes. |
| 174 | * Returns false if `data` is too short or uints of `size` are not supported. |
| 175 | */ |
| 176 | bool fdt_parse_number(struct memiter *data, size_t size, uint64_t *val) |
| 177 | { |
| 178 | struct memiter data_int; |
| 179 | struct memiter data_rem; |
| 180 | |
| 181 | data_rem = *data; |
| 182 | if (!memiter_consume(&data_rem, size, &data_int)) { |
| 183 | return false; |
| 184 | } |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 185 | |
| 186 | switch (size) { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 187 | case sizeof(uint32_t): { |
| 188 | static_assert(sizeof(uint32_t) == sizeof(fdt32_t), |
| 189 | "Size mismatch"); |
| 190 | *val = fdt32_ld((const fdt32_t *)memiter_base(&data_int)); |
| 191 | break; |
| 192 | } |
| 193 | case sizeof(uint64_t): { |
| 194 | static_assert(sizeof(uint64_t) == sizeof(fdt64_t), |
| 195 | "Size mismatch"); |
| 196 | *val = fdt64_ld((const fdt64_t *)memiter_base(&data_int)); |
| 197 | break; |
| 198 | } |
| 199 | default: { |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 200 | return false; |
| 201 | } |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 202 | } |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 203 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 204 | *data = data_rem; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 205 | return true; |
| 206 | } |
| 207 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 208 | /** |
| 209 | * Finds first direct subnode of `node`. |
| 210 | * If found, makes `node` point to the subnode and returns true. |
| 211 | * Returns false if no subnode is found. |
| 212 | */ |
| 213 | bool fdt_first_child(struct fdt_node *node) |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 214 | { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 215 | int child_off = fdt_first_subnode(fdt_base(&node->fdt), node->offset); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 216 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 217 | if (child_off < 0) { |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 218 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 219 | } |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 220 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 221 | node->offset = child_off; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 222 | return true; |
| 223 | } |
| 224 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 225 | /** |
| 226 | * Finds next sibling node of `node`. Call repeatedly to discover all siblings. |
| 227 | * If found, makes `node` point to the next sibling node and returns true. |
| 228 | * Returns false if no next sibling node is found. |
| 229 | */ |
| 230 | bool fdt_next_sibling(struct fdt_node *node) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 231 | { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 232 | int sib_off = fdt_next_subnode(fdt_base(&node->fdt), node->offset); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 233 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 234 | if (sib_off < 0) { |
| 235 | return false; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 236 | } |
| 237 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 238 | node->offset = sib_off; |
| 239 | return true; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 240 | } |
| 241 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 242 | /** |
| 243 | * Finds a node named `name` among subnodes of `node`. |
| 244 | * Returns true if found, false if not found or an error occurred. |
| 245 | */ |
| 246 | bool fdt_find_child(struct fdt_node *node, const struct string *name) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 247 | { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 248 | struct fdt_node child = *node; |
| 249 | const void *base = fdt_base(&node->fdt); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 250 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 251 | if (!fdt_first_child(&child)) { |
| 252 | return false; |
Andrew Scull | 8364429 | 2018-10-05 22:38:46 +0100 | [diff] [blame] | 253 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 254 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 255 | do { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 256 | const char *child_name; |
| 257 | int lenp; |
| 258 | struct memiter it; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 259 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 260 | child_name = fdt_get_name(base, child.offset, &lenp); |
| 261 | if (child_name == NULL) { |
| 262 | /* Error */ |
| 263 | return false; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 264 | } |
| 265 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 266 | CHECK(lenp >= 0); |
| 267 | memiter_init(&it, child_name, (size_t)lenp); |
| 268 | if (string_eq(name, &it)) { |
| 269 | node->offset = child.offset; |
| 270 | return true; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 271 | } |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 272 | } while (fdt_next_sibling(&child)); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 273 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 274 | /* Not found */ |
| 275 | return false; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 276 | } |