1 // SPDX-License-Identifier: GPL-2.0
4 * Masami Hiramatsu <mhiramat@kernel.org>
7 #define pr_fmt(fmt) "bootconfig: " fmt
9 #include <linux/bootconfig.h>
10 #include <linux/bug.h>
11 #include <linux/ctype.h>
12 #include <linux/errno.h>
13 #include <linux/kernel.h>
14 #include <linux/memblock.h>
15 #include <linux/printk.h>
16 #include <linux/string.h>
19 * Extra Boot Config (XBC) is given as tree-structured ascii text of
20 * key-value pairs on memory.
21 * xbc_parse() parses the text to build a simple tree. Each tree node is
22 * simply a key word or a value. A key node may have a next key node or/and
23 * a child node (both key and value). A value node may have a next value
27 static struct xbc_node *xbc_nodes __initdata;
28 static int xbc_node_num __initdata;
29 static char *xbc_data __initdata;
30 static size_t xbc_data_size __initdata;
31 static struct xbc_node *last_parent __initdata;
32 static const char *xbc_err_msg __initdata;
33 static int xbc_err_pos __initdata;
35 static int __init xbc_parse_error(const char *msg, const char *p)
38 xbc_err_pos = (int)(p - xbc_data);
44 * xbc_root_node() - Get the root node of extended boot config
46 * Return the address of root node of extended boot config. If the
47 * extended boot config is not initiized, return NULL.
49 struct xbc_node * __init xbc_root_node(void)
51 if (unlikely(!xbc_data))
58 * xbc_node_index() - Get the index of XBC node
59 * @node: A target node of getting index.
61 * Return the index number of @node in XBC node list.
63 int __init xbc_node_index(struct xbc_node *node)
65 return node - &xbc_nodes[0];
69 * xbc_node_get_parent() - Get the parent XBC node
72 * Return the parent node of @node. If the node is top node of the tree,
75 struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node)
77 return node->parent == XBC_NODE_MAX ? NULL : &xbc_nodes[node->parent];
81 * xbc_node_get_child() - Get the child XBC node
84 * Return the first child node of @node. If the node has no child, return
87 struct xbc_node * __init xbc_node_get_child(struct xbc_node *node)
89 return node->child ? &xbc_nodes[node->child] : NULL;
93 * xbc_node_get_next() - Get the next sibling XBC node
96 * Return the NEXT sibling node of @node. If the node has no next sibling,
97 * return NULL. Note that even if this returns NULL, it doesn't mean @node
98 * has no siblings. (You also has to check whether the parent's child node
101 struct xbc_node * __init xbc_node_get_next(struct xbc_node *node)
103 return node->next ? &xbc_nodes[node->next] : NULL;
107 * xbc_node_get_data() - Get the data of XBC node
108 * @node: An XBC node.
110 * Return the data (which is always a null terminated string) of @node.
111 * If the node has invalid data, warn and return NULL.
113 const char * __init xbc_node_get_data(struct xbc_node *node)
115 int offset = node->data & ~XBC_VALUE;
117 if (WARN_ON(offset >= xbc_data_size))
120 return xbc_data + offset;
124 xbc_node_match_prefix(struct xbc_node *node, const char **prefix)
126 const char *p = xbc_node_get_data(node);
129 if (strncmp(*prefix, p, len))
143 * xbc_node_find_child() - Find a child node which matches given key
144 * @parent: An XBC node.
145 * @key: A key string.
147 * Search a node under @parent which matches @key. The @key can contain
148 * several words jointed with '.'. If @parent is NULL, this searches the
149 * node from whole tree. Return NULL if no node is matched.
151 struct xbc_node * __init
152 xbc_node_find_child(struct xbc_node *parent, const char *key)
154 struct xbc_node *node;
157 node = xbc_node_get_child(parent);
159 node = xbc_root_node();
161 while (node && xbc_node_is_key(node)) {
162 if (!xbc_node_match_prefix(node, &key))
163 node = xbc_node_get_next(node);
164 else if (*key != '\0')
165 node = xbc_node_get_child(node);
174 * xbc_node_find_value() - Find a value node which matches given key
175 * @parent: An XBC node.
176 * @key: A key string.
177 * @vnode: A container pointer of found XBC node.
179 * Search a value node under @parent whose (parent) key node matches @key,
180 * store it in *@vnode, and returns the value string.
181 * The @key can contain several words jointed with '.'. If @parent is NULL,
182 * this searches the node from whole tree. Return the value string if a
183 * matched key found, return NULL if no node is matched.
184 * Note that this returns 0-length string and stores NULL in *@vnode if the
185 * key has no value. And also it will return the value of the first entry if
186 * the value is an array.
189 xbc_node_find_value(struct xbc_node *parent, const char *key,
190 struct xbc_node **vnode)
192 struct xbc_node *node = xbc_node_find_child(parent, key);
194 if (!node || !xbc_node_is_key(node))
197 node = xbc_node_get_child(node);
198 if (node && !xbc_node_is_value(node))
204 return node ? xbc_node_get_data(node) : "";
208 * xbc_node_compose_key_after() - Compose partial key string of the XBC node
209 * @root: Root XBC node
210 * @node: Target XBC node.
211 * @buf: A buffer to store the key.
212 * @size: The size of the @buf.
214 * Compose the partial key of the @node into @buf, which is starting right
215 * after @root (@root is not included.) If @root is NULL, this returns full
216 * key words of @node.
217 * Returns the total length of the key stored in @buf. Returns -EINVAL
218 * if @node is NULL or @root is not the ancestor of @node or @root is @node,
219 * or returns -ERANGE if the key depth is deeper than max depth.
220 * This is expected to be used with xbc_find_node() to list up all (child)
221 * keys under given key.
223 int __init xbc_node_compose_key_after(struct xbc_node *root,
224 struct xbc_node *node,
225 char *buf, size_t size)
227 u16 keys[XBC_DEPTH_MAX];
228 int depth = 0, ret = 0, total = 0;
230 if (!node || node == root)
233 if (xbc_node_is_value(node))
234 node = xbc_node_get_parent(node);
236 while (node && node != root) {
237 keys[depth++] = xbc_node_index(node);
238 if (depth == XBC_DEPTH_MAX)
240 node = xbc_node_get_parent(node);
245 while (--depth >= 0) {
246 node = xbc_nodes + keys[depth];
247 ret = snprintf(buf, size, "%s%s", xbc_node_get_data(node),
264 * xbc_node_find_next_leaf() - Find the next leaf node under given node
265 * @root: An XBC root node
266 * @node: An XBC node which starts from.
268 * Search the next leaf node (which means the terminal key node) of @node
269 * under @root node (including @root node itself).
270 * Return the next node or NULL if next leaf node is not found.
272 struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root,
273 struct xbc_node *node)
275 if (unlikely(!xbc_data))
278 if (!node) { /* First try */
283 if (node == root) /* @root was a leaf, no child node. */
286 while (!node->next) {
287 node = xbc_node_get_parent(node);
290 /* User passed a node which is not uder parent */
294 node = xbc_node_get_next(node);
297 while (node && !xbc_node_is_leaf(node))
298 node = xbc_node_get_child(node);
304 * xbc_node_find_next_key_value() - Find the next key-value pair nodes
305 * @root: An XBC root node
306 * @leaf: A container pointer of XBC node which starts from.
308 * Search the next leaf node (which means the terminal key node) of *@leaf
309 * under @root node. Returns the value and update *@leaf if next leaf node
310 * is found, or NULL if no next leaf node is found.
311 * Note that this returns 0-length string if the key has no value, or
312 * the value of the first entry if the value is an array.
314 const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
315 struct xbc_node **leaf)
317 /* tip must be passed */
321 *leaf = xbc_node_find_next_leaf(root, *leaf);
325 return xbc_node_get_data(xbc_node_get_child(*leaf));
327 return ""; /* No value key */
330 /* XBC parse and tree build */
332 static struct xbc_node * __init xbc_add_node(char *data, u32 flag)
334 struct xbc_node *node;
335 unsigned long offset;
337 if (xbc_node_num == XBC_NODE_MAX)
340 node = &xbc_nodes[xbc_node_num++];
341 offset = data - xbc_data;
342 node->data = (u16)offset;
343 if (WARN_ON(offset >= XBC_DATA_MAX))
352 static inline __init struct xbc_node *xbc_last_sibling(struct xbc_node *node)
355 node = xbc_node_get_next(node);
360 static struct xbc_node * __init xbc_add_sibling(char *data, u32 flag)
362 struct xbc_node *sib, *node = xbc_add_node(data, flag);
366 node->parent = XBC_NODE_MAX;
367 sib = xbc_last_sibling(xbc_nodes);
368 sib->next = xbc_node_index(node);
370 node->parent = xbc_node_index(last_parent);
371 if (!last_parent->child) {
372 last_parent->child = xbc_node_index(node);
374 sib = xbc_node_get_child(last_parent);
375 sib = xbc_last_sibling(sib);
376 sib->next = xbc_node_index(node);
380 xbc_parse_error("Too many nodes", data);
385 static inline __init struct xbc_node *xbc_add_child(char *data, u32 flag)
387 struct xbc_node *node = xbc_add_sibling(data, flag);
395 static inline __init bool xbc_valid_keyword(char *key)
400 while (isalnum(*key) || *key == '-' || *key == '_')
406 static char *skip_comment(char *p)
410 ret = strchr(p, '\n');
419 static char *skip_spaces_until_newline(char *p)
421 while (isspace(*p) && *p != '\n')
426 static int __init __xbc_open_brace(void)
428 /* Mark the last key as open brace */
429 last_parent->next = XBC_NODE_MAX;
434 static int __init __xbc_close_brace(char *p)
436 struct xbc_node *node;
438 if (!last_parent || last_parent->next != XBC_NODE_MAX)
439 return xbc_parse_error("Unexpected closing brace", p);
444 node = xbc_node_get_parent(node);
445 } while (node && node->next != XBC_NODE_MAX);
452 * Return delimiter or error, no node added. As same as lib/cmdline.c,
453 * you can use " around spaces, but can't escape " for value.
455 static int __init __xbc_parse_value(char **__v, char **__n)
465 if (*v == '"' || *v == '\'') {
471 if (!isprint(c) && !isspace(c))
472 return xbc_parse_error("Non printable value", p);
478 p = skip_spaces_until_newline(p);
480 if (c && !strchr(",;\n#}", c))
481 return xbc_parse_error("No value delimiter", p);
486 if (strchr(",;\n#}", c)) {
493 return xbc_parse_error("No closing quotes", p);
496 c = '\n'; /* A comment must be treated as a newline */
504 static int __init xbc_parse_array(char **__v)
506 struct xbc_node *node;
511 c = __xbc_parse_value(__v, &next);
515 node = xbc_add_sibling(*__v, XBC_VALUE);
526 struct xbc_node *find_match_node(struct xbc_node *node, char *k)
529 if (!strcmp(xbc_node_get_data(node), k))
531 node = xbc_node_get_next(node);
536 static int __init __xbc_add_key(char *k)
538 struct xbc_node *node, *child;
540 if (!xbc_valid_keyword(k))
541 return xbc_parse_error("Invalid keyword", k);
543 if (unlikely(xbc_node_num == 0))
546 if (!last_parent) /* the first level */
547 node = find_match_node(xbc_nodes, k);
549 child = xbc_node_get_child(last_parent);
550 if (child && xbc_node_is_value(child))
551 return xbc_parse_error("Subkey is mixed with value", k);
552 node = find_match_node(child, k);
559 node = xbc_add_child(k, XBC_KEY);
566 static int __init __xbc_parse_keys(char *k)
572 while ((p = strchr(k, '.'))) {
574 ret = __xbc_add_key(k);
580 return __xbc_add_key(k);
583 static int __init xbc_parse_kv(char **k, char *v, int op)
585 struct xbc_node *prev_parent = last_parent;
586 struct xbc_node *child;
590 ret = __xbc_parse_keys(*k);
594 child = xbc_node_get_child(last_parent);
596 if (xbc_node_is_key(child))
597 return xbc_parse_error("Value is mixed with subkey", v);
599 return xbc_parse_error("Value is redefined", v);
602 c = __xbc_parse_value(&v, &next);
606 if (!xbc_add_sibling(v, XBC_VALUE))
609 if (c == ',') { /* Array */
610 c = xbc_parse_array(&next);
615 last_parent = prev_parent;
618 ret = __xbc_close_brace(next - 1);
628 static int __init xbc_parse_key(char **k, char *n)
630 struct xbc_node *prev_parent = last_parent;
635 ret = __xbc_parse_keys(*k);
638 last_parent = prev_parent;
645 static int __init xbc_open_brace(char **k, char *n)
649 ret = __xbc_parse_keys(*k);
654 return __xbc_open_brace();
657 static int __init xbc_close_brace(char **k, char *n)
661 ret = xbc_parse_key(k, n);
664 /* k is updated in xbc_parse_key() */
666 return __xbc_close_brace(n - 1);
669 static int __init xbc_verify_tree(void)
671 int i, depth, len, wlen;
672 struct xbc_node *n, *m;
675 if (xbc_node_num == 0) {
676 xbc_parse_error("Empty config", xbc_data);
680 for (i = 0; i < xbc_node_num; i++) {
681 if (xbc_nodes[i].next > xbc_node_num) {
682 return xbc_parse_error("No closing brace",
683 xbc_node_get_data(xbc_nodes + i));
687 /* Key tree limitation check */
693 wlen = strlen(xbc_node_get_data(n)) + 1;
695 if (len > XBC_KEYLEN_MAX)
696 return xbc_parse_error("Too long key length",
697 xbc_node_get_data(n));
699 m = xbc_node_get_child(n);
700 if (m && xbc_node_is_key(m)) {
703 if (depth > XBC_DEPTH_MAX)
704 return xbc_parse_error("Too many key words",
705 xbc_node_get_data(n));
709 m = xbc_node_get_next(n);
711 n = xbc_node_get_parent(n);
714 len -= strlen(xbc_node_get_data(n)) + 1;
716 m = xbc_node_get_next(n);
725 * xbc_destroy_all() - Clean up all parsed bootconfig
727 * This clears all data structures of parsed bootconfig on memory.
728 * If you need to reuse xbc_init() with new boot config, you can
731 void __init xbc_destroy_all(void)
736 memblock_free(__pa(xbc_nodes), sizeof(struct xbc_node) * XBC_NODE_MAX);
741 * xbc_init() - Parse given XBC file and build XBC internal tree
742 * @buf: boot config text
743 * @emsg: A pointer of const char * to store the error message
744 * @epos: A pointer of int to store the error position
746 * This parses the boot config text in @buf. @buf must be a
747 * null terminated string and smaller than XBC_DATA_MAX.
748 * Return the number of stored nodes (>0) if succeeded, or -errno
749 * if there is any error.
750 * In error cases, @emsg will be updated with an error message and
751 * @epos will be updated with the error position which is the byte offset
752 * of @buf. If the error is not a parser error, @epos will be -1.
754 int __init xbc_init(char *buf, const char **emsg, int *epos)
764 *emsg = "Bootconfig is already initialized";
769 if (ret > XBC_DATA_MAX - 1 || ret == 0) {
771 *emsg = ret ? "Config data is too big" :
772 "Config data is empty";
776 xbc_nodes = memblock_alloc(sizeof(struct xbc_node) * XBC_NODE_MAX,
780 *emsg = "Failed to allocate bootconfig nodes";
783 memset(xbc_nodes, 0, sizeof(struct xbc_node) * XBC_NODE_MAX);
785 xbc_data_size = ret + 1;
790 q = strpbrk(p, "{}=+;\n#");
794 ret = xbc_parse_error("No delimiter", p);
803 ret = xbc_parse_error("Wrong '+' operator",
809 ret = xbc_parse_kv(&p, q, c);
812 ret = xbc_open_brace(&p, q);
819 ret = xbc_parse_key(&p, q);
822 ret = xbc_close_brace(&p, q);
828 ret = xbc_verify_tree();
843 * xbc_debug_dump() - Dump current XBC node list
845 * Dump the current XBC node list on printk buffer for debug.
847 void __init xbc_debug_dump(void)
851 for (i = 0; i < xbc_node_num; i++) {
852 pr_debug("[%d] %s (%s) .next=%d, .child=%d .parent=%d\n", i,
853 xbc_node_get_data(xbc_nodes + i),
854 xbc_node_is_value(xbc_nodes + i) ? "value" : "key",
855 xbc_nodes[i].next, xbc_nodes[i].child,
856 xbc_nodes[i].parent);