1 /* SPDX-License-Identifier: GPL-2.0 */
6 * Copyright (C) 2019 Linaro Ltd.
7 * Author: Masami Hiramatsu <mhiramat@kernel.org>
10 #include <linux/kernel.h>
11 #include <linux/types.h>
13 #define BOOTCONFIG_MAGIC "#BOOTCONFIG\n"
14 #define BOOTCONFIG_MAGIC_LEN 12
15 #define BOOTCONFIG_ALIGN_SHIFT 2
16 #define BOOTCONFIG_ALIGN (1 << BOOTCONFIG_ALIGN_SHIFT)
17 #define BOOTCONFIG_ALIGN_MASK (BOOTCONFIG_ALIGN - 1)
20 * xbc_calc_checksum() - Calculate checksum of bootconfig
21 * @data: Bootconfig data.
22 * @size: The size of the bootconfig data.
24 * Calculate the checksum value of the bootconfig data.
25 * The checksum will be used with the BOOTCONFIG_MAGIC and the size for
26 * embedding the bootconfig in the initrd image.
28 static inline __init u32 xbc_calc_checksum(void *data, u32 size)
30 unsigned char *p = data;
45 } __attribute__ ((__packed__));
48 #define XBC_VALUE (1 << 15)
49 /* Maximum size of boot config is 32KB - 1 */
50 #define XBC_DATA_MAX (XBC_VALUE - 1)
52 #define XBC_NODE_MAX 1024
53 #define XBC_KEYLEN_MAX 256
54 #define XBC_DEPTH_MAX 16
56 /* Node tree access raw APIs */
57 struct xbc_node * __init xbc_root_node(void);
58 int __init xbc_node_index(struct xbc_node *node);
59 struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node);
60 struct xbc_node * __init xbc_node_get_child(struct xbc_node *node);
61 struct xbc_node * __init xbc_node_get_next(struct xbc_node *node);
62 const char * __init xbc_node_get_data(struct xbc_node *node);
65 * xbc_node_is_value() - Test the node is a value node
68 * Test the @node is a value node and return true if a value node, false if not.
70 static inline __init bool xbc_node_is_value(struct xbc_node *node)
72 return node->data & XBC_VALUE;
76 * xbc_node_is_key() - Test the node is a key node
79 * Test the @node is a key node and return true if a key node, false if not.
81 static inline __init bool xbc_node_is_key(struct xbc_node *node)
83 return !xbc_node_is_value(node);
87 * xbc_node_is_array() - Test the node is an arraied value node
90 * Test the @node is an arraied value node.
92 static inline __init bool xbc_node_is_array(struct xbc_node *node)
94 return xbc_node_is_value(node) && node->child != 0;
98 * xbc_node_is_leaf() - Test the node is a leaf key node
101 * Test the @node is a leaf key node which is a key node and has a value node
102 * or no child. Returns true if it is a leaf node, or false if not.
103 * Note that the leaf node can have subkey nodes in addition to the
106 static inline __init bool xbc_node_is_leaf(struct xbc_node *node)
108 return xbc_node_is_key(node) &&
109 (!node->child || xbc_node_is_value(xbc_node_get_child(node)));
112 /* Tree-based key-value access APIs */
113 struct xbc_node * __init xbc_node_find_subkey(struct xbc_node *parent,
116 const char * __init xbc_node_find_value(struct xbc_node *parent,
118 struct xbc_node **vnode);
120 struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root,
121 struct xbc_node *leaf);
123 const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
124 struct xbc_node **leaf);
127 * xbc_find_value() - Find a value which matches the key
129 * @vnode: A container pointer of XBC value node.
131 * Search a value whose key matches @key from whole of XBC tree and return
132 * the value if found. Found value node is stored in *@vnode.
133 * Note that this can return 0-length string and store NULL in *@vnode for
134 * key-only (non-value) entry.
136 static inline const char * __init
137 xbc_find_value(const char *key, struct xbc_node **vnode)
139 return xbc_node_find_value(NULL, key, vnode);
143 * xbc_find_node() - Find a node which matches the key
146 * Search a (key) node whose key matches @key from whole of XBC tree and
147 * return the node if found. If not found, returns NULL.
149 static inline struct xbc_node * __init xbc_find_node(const char *key)
151 return xbc_node_find_subkey(NULL, key);
155 * xbc_node_get_subkey() - Return the first subkey node if exists
158 * Return the first subkey node of the @node. If the @node has no child
159 * or only value node, this will return NULL.
161 static inline struct xbc_node * __init xbc_node_get_subkey(struct xbc_node *node)
163 struct xbc_node *child = xbc_node_get_child(node);
165 if (child && xbc_node_is_value(child))
166 return xbc_node_get_next(child);
172 * xbc_array_for_each_value() - Iterate value nodes on an array
173 * @anode: An XBC arraied value node
176 * Iterate array value nodes and values starts from @anode. This is expected to
177 * be used with xbc_find_value() and xbc_node_find_value(), so that user can
178 * process each array entry node.
180 #define xbc_array_for_each_value(anode, value) \
181 for (value = xbc_node_get_data(anode); anode != NULL ; \
182 anode = xbc_node_get_child(anode), \
183 value = anode ? xbc_node_get_data(anode) : NULL)
186 * xbc_node_for_each_child() - Iterate child nodes
187 * @parent: An XBC node.
188 * @child: Iterated XBC node.
190 * Iterate child nodes of @parent. Each child nodes are stored to @child.
191 * The @child can be mixture of a value node and subkey nodes.
193 #define xbc_node_for_each_child(parent, child) \
194 for (child = xbc_node_get_child(parent); child != NULL ; \
195 child = xbc_node_get_next(child))
198 * xbc_node_for_each_subkey() - Iterate child subkey nodes
199 * @parent: An XBC node.
200 * @child: Iterated XBC node.
202 * Iterate subkey nodes of @parent. Each child nodes are stored to @child.
203 * The @child is only the subkey node.
205 #define xbc_node_for_each_subkey(parent, child) \
206 for (child = xbc_node_get_subkey(parent); child != NULL ; \
207 child = xbc_node_get_next(child))
210 * xbc_node_for_each_array_value() - Iterate array entries of geven key
211 * @node: An XBC node.
212 * @key: A key string searched under @node
213 * @anode: Iterated XBC node of array entry.
214 * @value: Iterated value of array entry.
216 * Iterate array entries of given @key under @node. Each array entry node
217 * is stored to @anode and @value. If the @node doesn't have @key node,
219 * Note that even if the found key node has only one value (not array)
220 * this executes block once. However, if the found key node has no value
221 * (key-only node), this does nothing. So don't use this for testing the
222 * key-value pair existence.
224 #define xbc_node_for_each_array_value(node, key, anode, value) \
225 for (value = xbc_node_find_value(node, key, &anode); value != NULL; \
226 anode = xbc_node_get_child(anode), \
227 value = anode ? xbc_node_get_data(anode) : NULL)
230 * xbc_node_for_each_key_value() - Iterate key-value pairs under a node
231 * @node: An XBC node.
232 * @knode: Iterated key node
233 * @value: Iterated value string
235 * Iterate key-value pairs under @node. Each key node and value string are
236 * stored in @knode and @value respectively.
238 #define xbc_node_for_each_key_value(node, knode, value) \
239 for (knode = NULL, value = xbc_node_find_next_key_value(node, &knode);\
240 knode != NULL; value = xbc_node_find_next_key_value(node, &knode))
243 * xbc_for_each_key_value() - Iterate key-value pairs
244 * @knode: Iterated key node
245 * @value: Iterated value string
247 * Iterate key-value pairs in whole XBC tree. Each key node and value string
248 * are stored in @knode and @value respectively.
250 #define xbc_for_each_key_value(knode, value) \
251 xbc_node_for_each_key_value(NULL, knode, value)
253 /* Compose partial key */
254 int __init xbc_node_compose_key_after(struct xbc_node *root,
255 struct xbc_node *node, char *buf, size_t size);
258 * xbc_node_compose_key() - Compose full key string of the XBC node
259 * @node: An XBC node.
260 * @buf: A buffer to store the key.
261 * @size: The size of the @buf.
263 * Compose the full-length key of the @node into @buf. Returns the total
264 * length of the key stored in @buf. Or returns -EINVAL if @node is NULL,
265 * and -ERANGE if the key depth is deeper than max depth.
267 static inline int __init xbc_node_compose_key(struct xbc_node *node,
268 char *buf, size_t size)
270 return xbc_node_compose_key_after(NULL, node, buf, size);
273 /* XBC node initializer */
274 int __init xbc_init(char *buf, const char **emsg, int *epos);
277 /* XBC cleanup data structures */
278 void __init xbc_destroy_all(void);
280 /* Debug dump functions */
281 void __init xbc_debug_dump(void);