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 int __init xbc_init_node(struct xbc_node *node, char *data, u32 flag)
334 unsigned long offset = data - xbc_data;
336 if (WARN_ON(offset >= XBC_DATA_MAX))
339 node->data = (u16)offset | flag;
346 static struct xbc_node * __init xbc_add_node(char *data, u32 flag)
348 struct xbc_node *node;
350 if (xbc_node_num == XBC_NODE_MAX)
353 node = &xbc_nodes[xbc_node_num++];
354 if (xbc_init_node(node, data, flag) < 0)
360 static inline __init struct xbc_node *xbc_last_sibling(struct xbc_node *node)
363 node = xbc_node_get_next(node);
368 static struct xbc_node * __init xbc_add_sibling(char *data, u32 flag)
370 struct xbc_node *sib, *node = xbc_add_node(data, flag);
374 node->parent = XBC_NODE_MAX;
375 sib = xbc_last_sibling(xbc_nodes);
376 sib->next = xbc_node_index(node);
378 node->parent = xbc_node_index(last_parent);
379 if (!last_parent->child) {
380 last_parent->child = xbc_node_index(node);
382 sib = xbc_node_get_child(last_parent);
383 sib = xbc_last_sibling(sib);
384 sib->next = xbc_node_index(node);
388 xbc_parse_error("Too many nodes", data);
393 static inline __init struct xbc_node *xbc_add_child(char *data, u32 flag)
395 struct xbc_node *node = xbc_add_sibling(data, flag);
403 static inline __init bool xbc_valid_keyword(char *key)
408 while (isalnum(*key) || *key == '-' || *key == '_')
414 static char *skip_comment(char *p)
418 ret = strchr(p, '\n');
427 static char *skip_spaces_until_newline(char *p)
429 while (isspace(*p) && *p != '\n')
434 static int __init __xbc_open_brace(void)
436 /* Mark the last key as open brace */
437 last_parent->next = XBC_NODE_MAX;
442 static int __init __xbc_close_brace(char *p)
444 struct xbc_node *node;
446 if (!last_parent || last_parent->next != XBC_NODE_MAX)
447 return xbc_parse_error("Unexpected closing brace", p);
452 node = xbc_node_get_parent(node);
453 } while (node && node->next != XBC_NODE_MAX);
460 * Return delimiter or error, no node added. As same as lib/cmdline.c,
461 * you can use " around spaces, but can't escape " for value.
463 static int __init __xbc_parse_value(char **__v, char **__n)
473 if (*v == '"' || *v == '\'') {
479 if (!isprint(c) && !isspace(c))
480 return xbc_parse_error("Non printable value", p);
486 p = skip_spaces_until_newline(p);
488 if (c && !strchr(",;\n#}", c))
489 return xbc_parse_error("No value delimiter", p);
494 if (strchr(",;\n#}", c)) {
501 return xbc_parse_error("No closing quotes", p);
504 c = '\n'; /* A comment must be treated as a newline */
512 static int __init xbc_parse_array(char **__v)
514 struct xbc_node *node;
519 c = __xbc_parse_value(__v, &next);
523 node = xbc_add_sibling(*__v, XBC_VALUE);
534 struct xbc_node *find_match_node(struct xbc_node *node, char *k)
537 if (!strcmp(xbc_node_get_data(node), k))
539 node = xbc_node_get_next(node);
544 static int __init __xbc_add_key(char *k)
546 struct xbc_node *node, *child;
548 if (!xbc_valid_keyword(k))
549 return xbc_parse_error("Invalid keyword", k);
551 if (unlikely(xbc_node_num == 0))
554 if (!last_parent) /* the first level */
555 node = find_match_node(xbc_nodes, k);
557 child = xbc_node_get_child(last_parent);
558 if (child && xbc_node_is_value(child))
559 return xbc_parse_error("Subkey is mixed with value", k);
560 node = find_match_node(child, k);
567 node = xbc_add_child(k, XBC_KEY);
574 static int __init __xbc_parse_keys(char *k)
580 while ((p = strchr(k, '.'))) {
582 ret = __xbc_add_key(k);
588 return __xbc_add_key(k);
591 static int __init xbc_parse_kv(char **k, char *v, int op)
593 struct xbc_node *prev_parent = last_parent;
594 struct xbc_node *child;
598 ret = __xbc_parse_keys(*k);
602 child = xbc_node_get_child(last_parent);
604 if (xbc_node_is_key(child))
605 return xbc_parse_error("Value is mixed with subkey", v);
607 return xbc_parse_error("Value is redefined", v);
610 c = __xbc_parse_value(&v, &next);
614 if (op == ':' && child) {
615 xbc_init_node(child, v, XBC_VALUE);
616 } else if (!xbc_add_sibling(v, XBC_VALUE))
619 if (c == ',') { /* Array */
620 c = xbc_parse_array(&next);
625 last_parent = prev_parent;
628 ret = __xbc_close_brace(next - 1);
638 static int __init xbc_parse_key(char **k, char *n)
640 struct xbc_node *prev_parent = last_parent;
645 ret = __xbc_parse_keys(*k);
648 last_parent = prev_parent;
655 static int __init xbc_open_brace(char **k, char *n)
659 ret = __xbc_parse_keys(*k);
664 return __xbc_open_brace();
667 static int __init xbc_close_brace(char **k, char *n)
671 ret = xbc_parse_key(k, n);
674 /* k is updated in xbc_parse_key() */
676 return __xbc_close_brace(n - 1);
679 static int __init xbc_verify_tree(void)
681 int i, depth, len, wlen;
682 struct xbc_node *n, *m;
685 if (xbc_node_num == 0) {
686 xbc_parse_error("Empty config", xbc_data);
690 for (i = 0; i < xbc_node_num; i++) {
691 if (xbc_nodes[i].next > xbc_node_num) {
692 return xbc_parse_error("No closing brace",
693 xbc_node_get_data(xbc_nodes + i));
697 /* Key tree limitation check */
703 wlen = strlen(xbc_node_get_data(n)) + 1;
705 if (len > XBC_KEYLEN_MAX)
706 return xbc_parse_error("Too long key length",
707 xbc_node_get_data(n));
709 m = xbc_node_get_child(n);
710 if (m && xbc_node_is_key(m)) {
713 if (depth > XBC_DEPTH_MAX)
714 return xbc_parse_error("Too many key words",
715 xbc_node_get_data(n));
719 m = xbc_node_get_next(n);
721 n = xbc_node_get_parent(n);
724 len -= strlen(xbc_node_get_data(n)) + 1;
726 m = xbc_node_get_next(n);
735 * xbc_destroy_all() - Clean up all parsed bootconfig
737 * This clears all data structures of parsed bootconfig on memory.
738 * If you need to reuse xbc_init() with new boot config, you can
741 void __init xbc_destroy_all(void)
746 memblock_free(__pa(xbc_nodes), sizeof(struct xbc_node) * XBC_NODE_MAX);
751 * xbc_init() - Parse given XBC file and build XBC internal tree
752 * @buf: boot config text
753 * @emsg: A pointer of const char * to store the error message
754 * @epos: A pointer of int to store the error position
756 * This parses the boot config text in @buf. @buf must be a
757 * null terminated string and smaller than XBC_DATA_MAX.
758 * Return the number of stored nodes (>0) if succeeded, or -errno
759 * if there is any error.
760 * In error cases, @emsg will be updated with an error message and
761 * @epos will be updated with the error position which is the byte offset
762 * of @buf. If the error is not a parser error, @epos will be -1.
764 int __init xbc_init(char *buf, const char **emsg, int *epos)
774 *emsg = "Bootconfig is already initialized";
779 if (ret > XBC_DATA_MAX - 1 || ret == 0) {
781 *emsg = ret ? "Config data is too big" :
782 "Config data is empty";
786 xbc_nodes = memblock_alloc(sizeof(struct xbc_node) * XBC_NODE_MAX,
790 *emsg = "Failed to allocate bootconfig nodes";
793 memset(xbc_nodes, 0, sizeof(struct xbc_node) * XBC_NODE_MAX);
795 xbc_data_size = ret + 1;
800 q = strpbrk(p, "{}=+;:\n#");
804 ret = xbc_parse_error("No delimiter", p);
814 ret = xbc_parse_error(c == '+' ?
815 "Wrong '+' operator" :
816 "Wrong ':' operator",
822 ret = xbc_parse_kv(&p, q, c);
825 ret = xbc_open_brace(&p, q);
832 ret = xbc_parse_key(&p, q);
835 ret = xbc_close_brace(&p, q);
841 ret = xbc_verify_tree();
856 * xbc_debug_dump() - Dump current XBC node list
858 * Dump the current XBC node list on printk buffer for debug.
860 void __init xbc_debug_dump(void)
864 for (i = 0; i < xbc_node_num; i++) {
865 pr_debug("[%d] %s (%s) .next=%d, .child=%d .parent=%d\n", i,
866 xbc_node_get_data(xbc_nodes + i),
867 xbc_node_is_value(xbc_nodes + i) ? "value" : "key",
868 xbc_nodes[i].next, xbc_nodes[i].child,
869 xbc_nodes[i].parent);