fdt_region.h 13 KB

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