Masahiro Yamada | 25ad202 | 2018-01-21 19:19:16 +0900 | [diff] [blame] | 1 | #ifndef UBOOT_LIBFDT_H |
| 2 | #define UBOOT_LIBFDT_H |
| 3 | /* |
| 4 | * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause |
| 5 | */ |
| 6 | |
Masahiro Yamada | 144fbea | 2018-01-21 19:19:17 +0900 | [diff] [blame] | 7 | #ifdef USE_HOSTCC |
| 8 | #include "../scripts/dtc/libfdt/libfdt.h" |
| 9 | #else |
| 10 | #include <linux/libfdt.h> |
| 11 | #endif |
Masahiro Yamada | 25ad202 | 2018-01-21 19:19:16 +0900 | [diff] [blame] | 12 | |
Masahiro Yamada | 144fbea | 2018-01-21 19:19:17 +0900 | [diff] [blame] | 13 | /* U-Boot local hacks */ |
| 14 | |
| 15 | #ifndef SWIG /* Not available in Python */ |
| 16 | struct fdt_region { |
| 17 | int offset; |
| 18 | int size; |
| 19 | }; |
| 20 | |
| 21 | /* |
| 22 | * Flags for fdt_find_regions() |
| 23 | * |
| 24 | * Add a region for the string table (always the last region) |
| 25 | */ |
| 26 | #define FDT_REG_ADD_STRING_TAB (1 << 0) |
| 27 | |
| 28 | /* |
| 29 | * Add all supernodes of a matching node/property, useful for creating a |
| 30 | * valid subset tree |
| 31 | */ |
| 32 | #define FDT_REG_SUPERNODES (1 << 1) |
| 33 | |
| 34 | /* Add the FDT_BEGIN_NODE tags of subnodes, including their names */ |
| 35 | #define FDT_REG_DIRECT_SUBNODES (1 << 2) |
| 36 | |
| 37 | /* Add all subnodes of a matching node */ |
| 38 | #define FDT_REG_ALL_SUBNODES (1 << 3) |
| 39 | |
| 40 | /* Add a region for the mem_rsvmap table (always the first region) */ |
| 41 | #define FDT_REG_ADD_MEM_RSVMAP (1 << 4) |
| 42 | |
| 43 | /* Indicates what an fdt part is (node, property, value) */ |
| 44 | #define FDT_IS_NODE (1 << 0) |
| 45 | #define FDT_IS_PROP (1 << 1) |
| 46 | #define FDT_IS_VALUE (1 << 2) /* not supported */ |
| 47 | #define FDT_IS_COMPAT (1 << 3) /* used internally */ |
| 48 | #define FDT_NODE_HAS_PROP (1 << 4) /* node contains prop */ |
| 49 | |
| 50 | #define FDT_ANY_GLOBAL (FDT_IS_NODE | FDT_IS_PROP | FDT_IS_VALUE | \ |
| 51 | FDT_IS_COMPAT) |
| 52 | #define FDT_IS_ANY 0x1f /* all the above */ |
| 53 | |
| 54 | /* We set a reasonable limit on the number of nested nodes */ |
| 55 | #define FDT_MAX_DEPTH 32 |
| 56 | |
| 57 | /* Decribes what we want to include from the current tag */ |
| 58 | enum want_t { |
| 59 | WANT_NOTHING, |
| 60 | WANT_NODES_ONLY, /* No properties */ |
| 61 | WANT_NODES_AND_PROPS, /* Everything for one level */ |
| 62 | WANT_ALL_NODES_AND_PROPS /* Everything for all levels */ |
| 63 | }; |
| 64 | |
| 65 | /* Keeps track of the state at parent nodes */ |
| 66 | struct fdt_subnode_stack { |
| 67 | int offset; /* Offset of node */ |
| 68 | enum want_t want; /* The 'want' value here */ |
| 69 | int included; /* 1 if we included this node, 0 if not */ |
| 70 | }; |
| 71 | |
| 72 | struct fdt_region_ptrs { |
| 73 | int depth; /* Current tree depth */ |
| 74 | int done; /* What we have completed scanning */ |
| 75 | enum want_t want; /* What we are currently including */ |
| 76 | char *end; /* Pointer to end of full node path */ |
| 77 | int nextoffset; /* Next node offset to check */ |
| 78 | }; |
| 79 | |
| 80 | /* The state of our finding algortihm */ |
| 81 | struct fdt_region_state { |
| 82 | struct fdt_subnode_stack stack[FDT_MAX_DEPTH]; /* node stack */ |
| 83 | struct fdt_region *region; /* Contains list of regions found */ |
| 84 | int count; /* Numnber of regions found */ |
| 85 | const void *fdt; /* FDT blob */ |
| 86 | int max_regions; /* Maximum regions to find */ |
| 87 | int can_merge; /* 1 if we can merge with previous region */ |
| 88 | int start; /* Start position of current region */ |
| 89 | struct fdt_region_ptrs ptrs; /* Pointers for what we are up to */ |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * fdt_find_regions() - find regions in device tree |
| 94 | * |
| 95 | * Given a list of nodes to include and properties to exclude, find |
| 96 | * the regions of the device tree which describe those included parts. |
| 97 | * |
| 98 | * The intent is to get a list of regions which will be invariant provided |
| 99 | * those parts are invariant. For example, if you request a list of regions |
| 100 | * for all nodes but exclude the property "data", then you will get the |
| 101 | * same region contents regardless of any change to "data" properties. |
| 102 | * |
| 103 | * This function can be used to produce a byte-stream to send to a hashing |
| 104 | * function to verify that critical parts of the FDT have not changed. |
| 105 | * |
| 106 | * Nodes which are given in 'inc' are included in the region list, as |
| 107 | * are the names of the immediate subnodes nodes (but not the properties |
| 108 | * or subnodes of those subnodes). |
| 109 | * |
| 110 | * For eaxample "/" means to include the root node, all root properties |
| 111 | * and the FDT_BEGIN_NODE and FDT_END_NODE of all subnodes of /. The latter |
| 112 | * ensures that we capture the names of the subnodes. In a hashing situation |
| 113 | * it prevents the root node from changing at all Any change to non-excluded |
| 114 | * properties, names of subnodes or number of subnodes would be detected. |
| 115 | * |
| 116 | * When used with FITs this provides the ability to hash and sign parts of |
| 117 | * the FIT based on different configurations in the FIT. Then it is |
| 118 | * impossible to change anything about that configuration (include images |
| 119 | * attached to the configuration), but it may be possible to add new |
| 120 | * configurations, new images or new signatures within the existing |
| 121 | * framework. |
| 122 | * |
| 123 | * Adding new properties to a device tree may result in the string table |
| 124 | * being extended (if the new property names are different from those |
| 125 | * already added). This function can optionally include a region for |
| 126 | * the string table so that this can be part of the hash too. |
| 127 | * |
| 128 | * The device tree header is not included in the list. |
| 129 | * |
| 130 | * @fdt: Device tree to check |
| 131 | * @inc: List of node paths to included |
| 132 | * @inc_count: Number of node paths in list |
| 133 | * @exc_prop: List of properties names to exclude |
| 134 | * @exc_prop_count: Number of properties in exclude list |
| 135 | * @region: Returns list of regions |
| 136 | * @max_region: Maximum length of region list |
| 137 | * @path: Pointer to a temporary string for the function to use for |
| 138 | * building path names |
| 139 | * @path_len: Length of path, must be large enough to hold the longest |
| 140 | * path in the tree |
| 141 | * @add_string_tab: 1 to add a region for the string table |
| 142 | * @return number of regions in list. If this is >max_regions then the |
| 143 | * region array was exhausted. You should increase max_regions and try |
| 144 | * the call again. |
| 145 | */ |
| 146 | int fdt_find_regions(const void *fdt, char * const inc[], int inc_count, |
| 147 | char * const exc_prop[], int exc_prop_count, |
| 148 | struct fdt_region region[], int max_regions, |
| 149 | char *path, int path_len, int add_string_tab); |
| 150 | |
| 151 | /** |
| 152 | * fdt_first_region() - find regions in device tree |
| 153 | * |
| 154 | * Given a nodes and properties to include and properties to exclude, find |
| 155 | * the regions of the device tree which describe those included parts. |
| 156 | * |
| 157 | * The use for this function is twofold. Firstly it provides a convenient |
| 158 | * way of performing a structure-aware grep of the tree. For example it is |
| 159 | * possible to grep for a node and get all the properties associated with |
| 160 | * that node. Trees can be subsetted easily, by specifying the nodes that |
| 161 | * are required, and then writing out the regions returned by this function. |
| 162 | * This is useful for small resource-constrained systems, such as boot |
| 163 | * loaders, which want to use an FDT but do not need to know about all of |
| 164 | * it. |
| 165 | * |
| 166 | * Secondly it makes it easy to hash parts of the tree and detect changes. |
| 167 | * The intent is to get a list of regions which will be invariant provided |
| 168 | * those parts are invariant. For example, if you request a list of regions |
| 169 | * for all nodes but exclude the property "data", then you will get the |
| 170 | * same region contents regardless of any change to "data" properties. |
| 171 | * |
| 172 | * This function can be used to produce a byte-stream to send to a hashing |
| 173 | * function to verify that critical parts of the FDT have not changed. |
| 174 | * Note that semantically null changes in order could still cause false |
| 175 | * hash misses. Such reordering might happen if the tree is regenerated |
| 176 | * from source, and nodes are reordered (the bytes-stream will be emitted |
| 177 | * in a different order and many hash functions will detect this). However |
| 178 | * if an existing tree is modified using libfdt functions, such as |
| 179 | * fdt_add_subnode() and fdt_setprop(), then this problem is avoided. |
| 180 | * |
| 181 | * The nodes/properties to include/exclude are defined by a function |
| 182 | * provided by the caller. This function is called for each node and |
| 183 | * property, and must return: |
| 184 | * |
| 185 | * 0 - to exclude this part |
| 186 | * 1 - to include this part |
| 187 | * -1 - for FDT_IS_PROP only: no information is available, so include |
| 188 | * if its containing node is included |
| 189 | * |
| 190 | * The last case is only used to deal with properties. Often a property is |
| 191 | * included if its containing node is included - this is the case where |
| 192 | * -1 is returned.. However if the property is specifically required to be |
| 193 | * included/excluded, then 0 or 1 can be returned. Note that including a |
| 194 | * property when the FDT_REG_SUPERNODES flag is given will force its |
| 195 | * containing node to be included since it is not valid to have a property |
| 196 | * that is not in a node. |
| 197 | * |
| 198 | * Using the information provided, the inclusion of a node can be controlled |
| 199 | * either by a node name or its compatible string, or any other property |
| 200 | * that the function can determine. |
| 201 | * |
| 202 | * As an example, including node "/" means to include the root node and all |
| 203 | * root properties. A flag provides a way of also including supernodes (of |
| 204 | * which there is none for the root node), and another flag includes |
| 205 | * immediate subnodes, so in this case we would get the FDT_BEGIN_NODE and |
| 206 | * FDT_END_NODE of all subnodes of /. |
| 207 | * |
| 208 | * The subnode feature helps in a hashing situation since it prevents the |
| 209 | * root node from changing at all. Any change to non-excluded properties, |
| 210 | * names of subnodes or number of subnodes would be detected. |
| 211 | * |
| 212 | * When used with FITs this provides the ability to hash and sign parts of |
| 213 | * the FIT based on different configurations in the FIT. Then it is |
| 214 | * impossible to change anything about that configuration (include images |
| 215 | * attached to the configuration), but it may be possible to add new |
| 216 | * configurations, new images or new signatures within the existing |
| 217 | * framework. |
| 218 | * |
| 219 | * Adding new properties to a device tree may result in the string table |
| 220 | * being extended (if the new property names are different from those |
| 221 | * already added). This function can optionally include a region for |
| 222 | * the string table so that this can be part of the hash too. This is always |
| 223 | * the last region. |
| 224 | * |
| 225 | * The FDT also has a mem_rsvmap table which can also be included, and is |
| 226 | * always the first region if so. |
| 227 | * |
| 228 | * The device tree header is not included in the region list. Since the |
| 229 | * contents of the FDT are changing (shrinking, often), the caller will need |
| 230 | * to regenerate the header anyway. |
| 231 | * |
| 232 | * @fdt: Device tree to check |
| 233 | * @h_include: Function to call to determine whether to include a part or |
| 234 | * not: |
| 235 | * |
| 236 | * @priv: Private pointer as passed to fdt_find_regions() |
| 237 | * @fdt: Pointer to FDT blob |
| 238 | * @offset: Offset of this node / property |
| 239 | * @type: Type of this part, FDT_IS_... |
| 240 | * @data: Pointer to data (node name, property name, compatible |
| 241 | * string, value (not yet supported) |
| 242 | * @size: Size of data, or 0 if none |
| 243 | * @return 0 to exclude, 1 to include, -1 if no information is |
| 244 | * available |
| 245 | * @priv: Private pointer passed to h_include |
| 246 | * @region: Returns list of regions, sorted by offset |
| 247 | * @max_regions: Maximum length of region list |
| 248 | * @path: Pointer to a temporary string for the function to use for |
| 249 | * building path names |
| 250 | * @path_len: Length of path, must be large enough to hold the longest |
| 251 | * path in the tree |
| 252 | * @flags: Various flags that control the region algortihm, see |
| 253 | * FDT_REG_... |
| 254 | * @return number of regions in list. If this is >max_regions then the |
| 255 | * region array was exhausted. You should increase max_regions and try |
| 256 | * the call again. Only the first max_regions elements are available in the |
| 257 | * array. |
| 258 | * |
| 259 | * On error a -ve value is return, which can be: |
| 260 | * |
| 261 | * -FDT_ERR_BADSTRUCTURE (too deep or more END tags than BEGIN tags |
| 262 | * -FDT_ERR_BADLAYOUT |
| 263 | * -FDT_ERR_NOSPACE (path area is too small) |
| 264 | */ |
| 265 | int fdt_first_region(const void *fdt, |
| 266 | int (*h_include)(void *priv, const void *fdt, int offset, |
| 267 | int type, const char *data, int size), |
| 268 | void *priv, struct fdt_region *region, |
| 269 | char *path, int path_len, int flags, |
| 270 | struct fdt_region_state *info); |
| 271 | |
| 272 | /** fdt_next_region() - find next region |
| 273 | * |
| 274 | * See fdt_first_region() for full description. This function finds the |
| 275 | * next region according to the provided parameters, which must be the same |
| 276 | * as passed to fdt_first_region(). |
| 277 | * |
| 278 | * This function can additionally return -FDT_ERR_NOTFOUND when there are no |
| 279 | * more regions |
| 280 | */ |
| 281 | int fdt_next_region(const void *fdt, |
| 282 | int (*h_include)(void *priv, const void *fdt, int offset, |
| 283 | int type, const char *data, int size), |
| 284 | void *priv, struct fdt_region *region, |
| 285 | char *path, int path_len, int flags, |
| 286 | struct fdt_region_state *info); |
| 287 | |
| 288 | /** |
| 289 | * fdt_add_alias_regions() - find aliases that point to existing regions |
| 290 | * |
| 291 | * Once a device tree grep is complete some of the nodes will be present |
| 292 | * and some will have been dropped. This function checks all the alias nodes |
| 293 | * to figure out which points point to nodes which are still present. These |
| 294 | * aliases need to be kept, along with the nodes they reference. |
| 295 | * |
| 296 | * Given a list of regions function finds the aliases that still apply and |
| 297 | * adds more regions to the list for these. This function is called after |
| 298 | * fdt_next_region() has finished returning regions and requires the same |
| 299 | * state. |
| 300 | * |
| 301 | * @fdt: Device tree file to reference |
| 302 | * @region: List of regions that will be kept |
| 303 | * @count: Number of regions |
| 304 | * @max_regions: Number of entries that can fit in @region |
| 305 | * @info: Region state as returned from fdt_next_region() |
| 306 | * @return new number of regions in @region (i.e. count + the number added) |
| 307 | * or -FDT_ERR_NOSPACE if there was not enough space. |
| 308 | */ |
| 309 | int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count, |
| 310 | int max_regions, struct fdt_region_state *info); |
| 311 | #endif /* SWIG */ |
| 312 | |
| 313 | extern struct fdt_header *working_fdt; /* Pointer to the working fdt */ |
Masahiro Yamada | 25ad202 | 2018-01-21 19:19:16 +0900 | [diff] [blame] | 314 | |
| 315 | /* adding a ramdisk needs 0x44 bytes in version 2008.10 */ |
| 316 | #define FDT_RAMDISK_OVERHEAD 0x80 |
| 317 | |
| 318 | #endif /* UBOOT_LIBFDT_H */ |