bootconfig.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_XBC_H
  3. #define _LINUX_XBC_H
  4. /*
  5. * Extra Boot Config
  6. * Copyright (C) 2019 Linaro Ltd.
  7. * Author: Masami Hiramatsu <[email protected]>
  8. */
  9. #ifdef __KERNEL__
  10. #include <linux/kernel.h>
  11. #include <linux/types.h>
  12. #else /* !__KERNEL__ */
  13. /*
  14. * NOTE: This is only for tools/bootconfig, because tools/bootconfig will
  15. * run the parser sanity test.
  16. * This does NOT mean linux/bootconfig.h is available in the user space.
  17. * However, if you change this file, please make sure the tools/bootconfig
  18. * has no issue on building and running.
  19. */
  20. #endif
  21. #define BOOTCONFIG_MAGIC "#BOOTCONFIG\n"
  22. #define BOOTCONFIG_MAGIC_LEN 12
  23. #define BOOTCONFIG_ALIGN_SHIFT 2
  24. #define BOOTCONFIG_ALIGN (1 << BOOTCONFIG_ALIGN_SHIFT)
  25. #define BOOTCONFIG_ALIGN_MASK (BOOTCONFIG_ALIGN - 1)
  26. /**
  27. * xbc_calc_checksum() - Calculate checksum of bootconfig
  28. * @data: Bootconfig data.
  29. * @size: The size of the bootconfig data.
  30. *
  31. * Calculate the checksum value of the bootconfig data.
  32. * The checksum will be used with the BOOTCONFIG_MAGIC and the size for
  33. * embedding the bootconfig in the initrd image.
  34. */
  35. static inline __init uint32_t xbc_calc_checksum(void *data, uint32_t size)
  36. {
  37. unsigned char *p = data;
  38. uint32_t ret = 0;
  39. while (size--)
  40. ret += *p++;
  41. return ret;
  42. }
  43. /* XBC tree node */
  44. struct xbc_node {
  45. uint16_t next;
  46. uint16_t child;
  47. uint16_t parent;
  48. uint16_t data;
  49. } __attribute__ ((__packed__));
  50. #define XBC_KEY 0
  51. #define XBC_VALUE (1 << 15)
  52. /* Maximum size of boot config is 32KB - 1 */
  53. #define XBC_DATA_MAX (XBC_VALUE - 1)
  54. #define XBC_NODE_MAX 8192
  55. #define XBC_KEYLEN_MAX 256
  56. #define XBC_DEPTH_MAX 16
  57. /* Node tree access raw APIs */
  58. struct xbc_node * __init xbc_root_node(void);
  59. int __init xbc_node_index(struct xbc_node *node);
  60. struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node);
  61. struct xbc_node * __init xbc_node_get_child(struct xbc_node *node);
  62. struct xbc_node * __init xbc_node_get_next(struct xbc_node *node);
  63. const char * __init xbc_node_get_data(struct xbc_node *node);
  64. /**
  65. * xbc_node_is_value() - Test the node is a value node
  66. * @node: An XBC node.
  67. *
  68. * Test the @node is a value node and return true if a value node, false if not.
  69. */
  70. static inline __init bool xbc_node_is_value(struct xbc_node *node)
  71. {
  72. return node->data & XBC_VALUE;
  73. }
  74. /**
  75. * xbc_node_is_key() - Test the node is a key node
  76. * @node: An XBC node.
  77. *
  78. * Test the @node is a key node and return true if a key node, false if not.
  79. */
  80. static inline __init bool xbc_node_is_key(struct xbc_node *node)
  81. {
  82. return !xbc_node_is_value(node);
  83. }
  84. /**
  85. * xbc_node_is_array() - Test the node is an arraied value node
  86. * @node: An XBC node.
  87. *
  88. * Test the @node is an arraied value node.
  89. */
  90. static inline __init bool xbc_node_is_array(struct xbc_node *node)
  91. {
  92. return xbc_node_is_value(node) && node->child != 0;
  93. }
  94. /**
  95. * xbc_node_is_leaf() - Test the node is a leaf key node
  96. * @node: An XBC node.
  97. *
  98. * Test the @node is a leaf key node which is a key node and has a value node
  99. * or no child. Returns true if it is a leaf node, or false if not.
  100. * Note that the leaf node can have subkey nodes in addition to the
  101. * value node.
  102. */
  103. static inline __init bool xbc_node_is_leaf(struct xbc_node *node)
  104. {
  105. return xbc_node_is_key(node) &&
  106. (!node->child || xbc_node_is_value(xbc_node_get_child(node)));
  107. }
  108. /* Tree-based key-value access APIs */
  109. struct xbc_node * __init xbc_node_find_subkey(struct xbc_node *parent,
  110. const char *key);
  111. const char * __init xbc_node_find_value(struct xbc_node *parent,
  112. const char *key,
  113. struct xbc_node **vnode);
  114. struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root,
  115. struct xbc_node *leaf);
  116. const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
  117. struct xbc_node **leaf);
  118. /**
  119. * xbc_find_value() - Find a value which matches the key
  120. * @key: Search key
  121. * @vnode: A container pointer of XBC value node.
  122. *
  123. * Search a value whose key matches @key from whole of XBC tree and return
  124. * the value if found. Found value node is stored in *@vnode.
  125. * Note that this can return 0-length string and store NULL in *@vnode for
  126. * key-only (non-value) entry.
  127. */
  128. static inline const char * __init
  129. xbc_find_value(const char *key, struct xbc_node **vnode)
  130. {
  131. return xbc_node_find_value(NULL, key, vnode);
  132. }
  133. /**
  134. * xbc_find_node() - Find a node which matches the key
  135. * @key: Search key
  136. *
  137. * Search a (key) node whose key matches @key from whole of XBC tree and
  138. * return the node if found. If not found, returns NULL.
  139. */
  140. static inline struct xbc_node * __init xbc_find_node(const char *key)
  141. {
  142. return xbc_node_find_subkey(NULL, key);
  143. }
  144. /**
  145. * xbc_node_get_subkey() - Return the first subkey node if exists
  146. * @node: Parent node
  147. *
  148. * Return the first subkey node of the @node. If the @node has no child
  149. * or only value node, this will return NULL.
  150. */
  151. static inline struct xbc_node * __init xbc_node_get_subkey(struct xbc_node *node)
  152. {
  153. struct xbc_node *child = xbc_node_get_child(node);
  154. if (child && xbc_node_is_value(child))
  155. return xbc_node_get_next(child);
  156. else
  157. return child;
  158. }
  159. /**
  160. * xbc_array_for_each_value() - Iterate value nodes on an array
  161. * @anode: An XBC arraied value node
  162. * @value: A value
  163. *
  164. * Iterate array value nodes and values starts from @anode. This is expected to
  165. * be used with xbc_find_value() and xbc_node_find_value(), so that user can
  166. * process each array entry node.
  167. */
  168. #define xbc_array_for_each_value(anode, value) \
  169. for (value = xbc_node_get_data(anode); anode != NULL ; \
  170. anode = xbc_node_get_child(anode), \
  171. value = anode ? xbc_node_get_data(anode) : NULL)
  172. /**
  173. * xbc_node_for_each_child() - Iterate child nodes
  174. * @parent: An XBC node.
  175. * @child: Iterated XBC node.
  176. *
  177. * Iterate child nodes of @parent. Each child nodes are stored to @child.
  178. * The @child can be mixture of a value node and subkey nodes.
  179. */
  180. #define xbc_node_for_each_child(parent, child) \
  181. for (child = xbc_node_get_child(parent); child != NULL ; \
  182. child = xbc_node_get_next(child))
  183. /**
  184. * xbc_node_for_each_subkey() - Iterate child subkey nodes
  185. * @parent: An XBC node.
  186. * @child: Iterated XBC node.
  187. *
  188. * Iterate subkey nodes of @parent. Each child nodes are stored to @child.
  189. * The @child is only the subkey node.
  190. */
  191. #define xbc_node_for_each_subkey(parent, child) \
  192. for (child = xbc_node_get_subkey(parent); child != NULL ; \
  193. child = xbc_node_get_next(child))
  194. /**
  195. * xbc_node_for_each_array_value() - Iterate array entries of geven key
  196. * @node: An XBC node.
  197. * @key: A key string searched under @node
  198. * @anode: Iterated XBC node of array entry.
  199. * @value: Iterated value of array entry.
  200. *
  201. * Iterate array entries of given @key under @node. Each array entry node
  202. * is stored to @anode and @value. If the @node doesn't have @key node,
  203. * it does nothing.
  204. * Note that even if the found key node has only one value (not array)
  205. * this executes block once. However, if the found key node has no value
  206. * (key-only node), this does nothing. So don't use this for testing the
  207. * key-value pair existence.
  208. */
  209. #define xbc_node_for_each_array_value(node, key, anode, value) \
  210. for (value = xbc_node_find_value(node, key, &anode); value != NULL; \
  211. anode = xbc_node_get_child(anode), \
  212. value = anode ? xbc_node_get_data(anode) : NULL)
  213. /**
  214. * xbc_node_for_each_key_value() - Iterate key-value pairs under a node
  215. * @node: An XBC node.
  216. * @knode: Iterated key node
  217. * @value: Iterated value string
  218. *
  219. * Iterate key-value pairs under @node. Each key node and value string are
  220. * stored in @knode and @value respectively.
  221. */
  222. #define xbc_node_for_each_key_value(node, knode, value) \
  223. for (knode = NULL, value = xbc_node_find_next_key_value(node, &knode);\
  224. knode != NULL; value = xbc_node_find_next_key_value(node, &knode))
  225. /**
  226. * xbc_for_each_key_value() - Iterate key-value pairs
  227. * @knode: Iterated key node
  228. * @value: Iterated value string
  229. *
  230. * Iterate key-value pairs in whole XBC tree. Each key node and value string
  231. * are stored in @knode and @value respectively.
  232. */
  233. #define xbc_for_each_key_value(knode, value) \
  234. xbc_node_for_each_key_value(NULL, knode, value)
  235. /* Compose partial key */
  236. int __init xbc_node_compose_key_after(struct xbc_node *root,
  237. struct xbc_node *node, char *buf, size_t size);
  238. /**
  239. * xbc_node_compose_key() - Compose full key string of the XBC node
  240. * @node: An XBC node.
  241. * @buf: A buffer to store the key.
  242. * @size: The size of the @buf.
  243. *
  244. * Compose the full-length key of the @node into @buf. Returns the total
  245. * length of the key stored in @buf. Or returns -EINVAL if @node is NULL,
  246. * and -ERANGE if the key depth is deeper than max depth.
  247. */
  248. static inline int __init xbc_node_compose_key(struct xbc_node *node,
  249. char *buf, size_t size)
  250. {
  251. return xbc_node_compose_key_after(NULL, node, buf, size);
  252. }
  253. /* XBC node initializer */
  254. int __init xbc_init(const char *buf, size_t size, const char **emsg, int *epos);
  255. /* XBC node and size information */
  256. int __init xbc_get_info(int *node_size, size_t *data_size);
  257. /* XBC cleanup data structures */
  258. void __init xbc_exit(void);
  259. /* XBC embedded bootconfig data in kernel */
  260. #ifdef CONFIG_BOOT_CONFIG_EMBED
  261. const char * __init xbc_get_embedded_bootconfig(size_t *size);
  262. #else
  263. static inline const char *xbc_get_embedded_bootconfig(size_t *size)
  264. {
  265. return NULL;
  266. }
  267. #endif
  268. #endif