fdt_region.h 13 KB

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