1 // SPDX-License-Identifier: GPL-2.0
4 * Masami Hiramatsu <mhiramat@kernel.org>
7 #define pr_fmt(fmt) "bootconfig: " fmt
10 #include <linux/ctype.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/printk.h>
14 #include <linux/bootconfig.h>
15 #include <linux/string.h>
18 * Extra Boot Config (XBC) is given as tree-structured ascii text of
19 * key-value pairs on memory.
20 * xbc_parse() parses the text to build a simple tree. Each tree node is
21 * simply a key word or a value. A key node may have a next key node or/and
22 * a child node (both key and value). A value node may have a next value
26 static struct xbc_node xbc_nodes[XBC_NODE_MAX] __initdata;
27 static int xbc_node_num __initdata;
28 static char *xbc_data __initdata;
29 static size_t xbc_data_size __initdata;
30 static struct xbc_node *last_parent __initdata;
32 static int __init xbc_parse_error(const char *msg, const char *p)
34 int pos = p - xbc_data;
36 pr_err("Parse error at pos %d: %s\n", pos, msg);
41 * xbc_root_node() - Get the root node of extended boot config
43 * Return the address of root node of extended boot config. If the
44 * extended boot config is not initiized, return NULL.
46 struct xbc_node * __init xbc_root_node(void)
48 if (unlikely(!xbc_data))
55 * xbc_node_index() - Get the index of XBC node
56 * @node: A target node of getting index.
58 * Return the index number of @node in XBC node list.
60 int __init xbc_node_index(struct xbc_node *node)
62 return node - &xbc_nodes[0];
66 * xbc_node_get_parent() - Get the parent XBC node
69 * Return the parent node of @node. If the node is top node of the tree,
72 struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node)
74 return node->parent == XBC_NODE_MAX ? NULL : &xbc_nodes[node->parent];
78 * xbc_node_get_child() - Get the child XBC node
81 * Return the first child node of @node. If the node has no child, return
84 struct xbc_node * __init xbc_node_get_child(struct xbc_node *node)
86 return node->child ? &xbc_nodes[node->child] : NULL;
90 * xbc_node_get_next() - Get the next sibling XBC node
93 * Return the NEXT sibling node of @node. If the node has no next sibling,
94 * return NULL. Note that even if this returns NULL, it doesn't mean @node
95 * has no siblings. (You also has to check whether the parent's child node
98 struct xbc_node * __init xbc_node_get_next(struct xbc_node *node)
100 return node->next ? &xbc_nodes[node->next] : NULL;
104 * xbc_node_get_data() - Get the data of XBC node
105 * @node: An XBC node.
107 * Return the data (which is always a null terminated string) of @node.
108 * If the node has invalid data, warn and return NULL.
110 const char * __init xbc_node_get_data(struct xbc_node *node)
112 int offset = node->data & ~XBC_VALUE;
114 if (WARN_ON(offset >= xbc_data_size))
117 return xbc_data + offset;
121 xbc_node_match_prefix(struct xbc_node *node, const char **prefix)
123 const char *p = xbc_node_get_data(node);
126 if (strncmp(*prefix, p, len))
140 * xbc_node_find_child() - Find a child node which matches given key
141 * @parent: An XBC node.
142 * @key: A key string.
144 * Search a node under @parent which matches @key. The @key can contain
145 * several words jointed with '.'. If @parent is NULL, this searches the
146 * node from whole tree. Return NULL if no node is matched.
148 struct xbc_node * __init
149 xbc_node_find_child(struct xbc_node *parent, const char *key)
151 struct xbc_node *node;
154 node = xbc_node_get_child(parent);
156 node = xbc_root_node();
158 while (node && xbc_node_is_key(node)) {
159 if (!xbc_node_match_prefix(node, &key))
160 node = xbc_node_get_next(node);
161 else if (*key != '\0')
162 node = xbc_node_get_child(node);
171 * xbc_node_find_value() - Find a value node which matches given key
172 * @parent: An XBC node.
173 * @key: A key string.
174 * @vnode: A container pointer of found XBC node.
176 * Search a value node under @parent whose (parent) key node matches @key,
177 * store it in *@vnode, and returns the value string.
178 * The @key can contain several words jointed with '.'. If @parent is NULL,
179 * this searches the node from whole tree. Return the value string if a
180 * matched key found, return NULL if no node is matched.
181 * Note that this returns 0-length string and stores NULL in *@vnode if the
182 * key has no value. And also it will return the value of the first entry if
183 * the value is an array.
186 xbc_node_find_value(struct xbc_node *parent, const char *key,
187 struct xbc_node **vnode)
189 struct xbc_node *node = xbc_node_find_child(parent, key);
191 if (!node || !xbc_node_is_key(node))
194 node = xbc_node_get_child(node);
195 if (node && !xbc_node_is_value(node))
201 return node ? xbc_node_get_data(node) : "";
205 * xbc_node_compose_key_after() - Compose partial key string of the XBC node
206 * @root: Root XBC node
207 * @node: Target XBC node.
208 * @buf: A buffer to store the key.
209 * @size: The size of the @buf.
211 * Compose the partial key of the @node into @buf, which is starting right
212 * after @root (@root is not included.) If @root is NULL, this returns full
213 * key words of @node.
214 * Returns the total length of the key stored in @buf. Returns -EINVAL
215 * if @node is NULL or @root is not the ancestor of @node or @root is @node,
216 * or returns -ERANGE if the key depth is deeper than max depth.
217 * This is expected to be used with xbc_find_node() to list up all (child)
218 * keys under given key.
220 int __init xbc_node_compose_key_after(struct xbc_node *root,
221 struct xbc_node *node,
222 char *buf, size_t size)
224 u16 keys[XBC_DEPTH_MAX];
225 int depth = 0, ret = 0, total = 0;
227 if (!node || node == root)
230 if (xbc_node_is_value(node))
231 node = xbc_node_get_parent(node);
233 while (node && node != root) {
234 keys[depth++] = xbc_node_index(node);
235 if (depth == XBC_DEPTH_MAX)
237 node = xbc_node_get_parent(node);
242 while (--depth >= 0) {
243 node = xbc_nodes + keys[depth];
244 ret = snprintf(buf, size, "%s%s", xbc_node_get_data(node),
261 * xbc_node_find_next_leaf() - Find the next leaf node under given node
262 * @root: An XBC root node
263 * @node: An XBC node which starts from.
265 * Search the next leaf node (which means the terminal key node) of @node
266 * under @root node (including @root node itself).
267 * Return the next node or NULL if next leaf node is not found.
269 struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root,
270 struct xbc_node *node)
272 if (unlikely(!xbc_data))
275 if (!node) { /* First try */
280 if (node == root) /* @root was a leaf, no child node. */
283 while (!node->next) {
284 node = xbc_node_get_parent(node);
287 /* User passed a node which is not uder parent */
291 node = xbc_node_get_next(node);
294 while (node && !xbc_node_is_leaf(node))
295 node = xbc_node_get_child(node);
301 * xbc_node_find_next_key_value() - Find the next key-value pair nodes
302 * @root: An XBC root node
303 * @leaf: A container pointer of XBC node which starts from.
305 * Search the next leaf node (which means the terminal key node) of *@leaf
306 * under @root node. Returns the value and update *@leaf if next leaf node
307 * is found, or NULL if no next leaf node is found.
308 * Note that this returns 0-length string if the key has no value, or
309 * the value of the first entry if the value is an array.
311 const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
312 struct xbc_node **leaf)
314 /* tip must be passed */
318 *leaf = xbc_node_find_next_leaf(root, *leaf);
322 return xbc_node_get_data(xbc_node_get_child(*leaf));
324 return ""; /* No value key */
327 /* XBC parse and tree build */
329 static struct xbc_node * __init xbc_add_node(char *data, u32 flag)
331 struct xbc_node *node;
332 unsigned long offset;
334 if (xbc_node_num == XBC_NODE_MAX)
337 node = &xbc_nodes[xbc_node_num++];
338 offset = data - xbc_data;
339 node->data = (u16)offset;
340 if (WARN_ON(offset >= XBC_DATA_MAX))
349 static inline __init struct xbc_node *xbc_last_sibling(struct xbc_node *node)
352 node = xbc_node_get_next(node);
357 static struct xbc_node * __init xbc_add_sibling(char *data, u32 flag)
359 struct xbc_node *sib, *node = xbc_add_node(data, flag);
363 node->parent = XBC_NODE_MAX;
364 sib = xbc_last_sibling(xbc_nodes);
365 sib->next = xbc_node_index(node);
367 node->parent = xbc_node_index(last_parent);
368 if (!last_parent->child) {
369 last_parent->child = xbc_node_index(node);
371 sib = xbc_node_get_child(last_parent);
372 sib = xbc_last_sibling(sib);
373 sib->next = xbc_node_index(node);
381 static inline __init struct xbc_node *xbc_add_child(char *data, u32 flag)
383 struct xbc_node *node = xbc_add_sibling(data, flag);
391 static inline __init bool xbc_valid_keyword(char *key)
396 while (isalnum(*key) || *key == '-' || *key == '_')
402 static char *skip_comment(char *p)
406 ret = strchr(p, '\n');
415 static char *skip_spaces_until_newline(char *p)
417 while (isspace(*p) && *p != '\n')
422 static int __init __xbc_open_brace(void)
424 /* Mark the last key as open brace */
425 last_parent->next = XBC_NODE_MAX;
430 static int __init __xbc_close_brace(char *p)
432 struct xbc_node *node;
434 if (!last_parent || last_parent->next != XBC_NODE_MAX)
435 return xbc_parse_error("Unexpected closing brace", p);
440 node = xbc_node_get_parent(node);
441 } while (node && node->next != XBC_NODE_MAX);
448 * Return delimiter or error, no node added. As same as lib/cmdline.c,
449 * you can use " around spaces, but can't escape " for value.
451 static int __init __xbc_parse_value(char **__v, char **__n)
461 if (*v == '"' || *v == '\'') {
467 if (!isprint(c) && !isspace(c))
468 return xbc_parse_error("Non printable value", p);
474 p = skip_spaces_until_newline(p);
476 if (c && !strchr(",;\n#}", c))
477 return xbc_parse_error("No value delimiter", p);
482 if (strchr(",;\n#}", c)) {
489 return xbc_parse_error("No closing quotes", p);
492 c = '\n'; /* A comment must be treated as a newline */
500 static int __init xbc_parse_array(char **__v)
502 struct xbc_node *node;
507 c = __xbc_parse_value(__v, &next);
511 node = xbc_add_sibling(*__v, XBC_VALUE);
522 struct xbc_node *find_match_node(struct xbc_node *node, char *k)
525 if (!strcmp(xbc_node_get_data(node), k))
527 node = xbc_node_get_next(node);
532 static int __init __xbc_add_key(char *k)
534 struct xbc_node *node;
536 if (!xbc_valid_keyword(k))
537 return xbc_parse_error("Invalid keyword", k);
539 if (unlikely(xbc_node_num == 0))
542 if (!last_parent) /* the first level */
543 node = find_match_node(xbc_nodes, k);
545 node = find_match_node(xbc_node_get_child(last_parent), k);
551 node = xbc_add_child(k, XBC_KEY);
558 static int __init __xbc_parse_keys(char *k)
564 while ((p = strchr(k, '.'))) {
566 ret = __xbc_add_key(k);
572 return __xbc_add_key(k);
575 static int __init xbc_parse_kv(char **k, char *v)
577 struct xbc_node *prev_parent = last_parent;
578 struct xbc_node *node;
582 ret = __xbc_parse_keys(*k);
586 c = __xbc_parse_value(&v, &next);
590 node = xbc_add_sibling(v, XBC_VALUE);
594 if (c == ',') { /* Array */
595 c = xbc_parse_array(&next);
600 last_parent = prev_parent;
603 ret = __xbc_close_brace(next - 1);
613 static int __init xbc_parse_key(char **k, char *n)
615 struct xbc_node *prev_parent = last_parent;
620 ret = __xbc_parse_keys(*k);
623 last_parent = prev_parent;
630 static int __init xbc_open_brace(char **k, char *n)
634 ret = __xbc_parse_keys(*k);
639 return __xbc_open_brace();
642 static int __init xbc_close_brace(char **k, char *n)
646 ret = xbc_parse_key(k, n);
649 /* k is updated in xbc_parse_key() */
651 return __xbc_close_brace(n - 1);
654 static int __init xbc_verify_tree(void)
656 int i, depth, len, wlen;
657 struct xbc_node *n, *m;
660 if (xbc_node_num == 0)
663 for (i = 0; i < xbc_node_num; i++) {
664 if (xbc_nodes[i].next > xbc_node_num) {
665 return xbc_parse_error("No closing brace",
666 xbc_node_get_data(xbc_nodes + i));
670 /* Key tree limitation check */
676 wlen = strlen(xbc_node_get_data(n)) + 1;
678 if (len > XBC_KEYLEN_MAX)
679 return xbc_parse_error("Too long key length",
680 xbc_node_get_data(n));
682 m = xbc_node_get_child(n);
683 if (m && xbc_node_is_key(m)) {
686 if (depth > XBC_DEPTH_MAX)
687 return xbc_parse_error("Too many key words",
688 xbc_node_get_data(n));
692 m = xbc_node_get_next(n);
694 n = xbc_node_get_parent(n);
697 len -= strlen(xbc_node_get_data(n)) + 1;
699 m = xbc_node_get_next(n);
708 * xbc_destroy_all() - Clean up all parsed bootconfig
710 * This clears all data structures of parsed bootconfig on memory.
711 * If you need to reuse xbc_init() with new boot config, you can
714 void __init xbc_destroy_all(void)
719 memset(xbc_nodes, 0, sizeof(xbc_nodes));
723 * xbc_init() - Parse given XBC file and build XBC internal tree
724 * @buf: boot config text
726 * This parses the boot config text in @buf. @buf must be a
727 * null terminated string and smaller than XBC_DATA_MAX.
728 * Return 0 if succeeded, or -errno if there is any error.
730 int __init xbc_init(char *buf)
739 if (ret > XBC_DATA_MAX - 1 || ret == 0)
743 xbc_data_size = ret + 1;
748 q = strpbrk(p, "{}=;\n#");
752 ret = xbc_parse_error("No delimiter", p);
760 ret = xbc_parse_kv(&p, q);
763 ret = xbc_open_brace(&p, q);
770 ret = xbc_parse_key(&p, q);
773 ret = xbc_close_brace(&p, q);
779 ret = xbc_verify_tree();
788 * xbc_debug_dump() - Dump current XBC node list
790 * Dump the current XBC node list on printk buffer for debug.
792 void __init xbc_debug_dump(void)
796 for (i = 0; i < xbc_node_num; i++) {
797 pr_debug("[%d] %s (%s) .next=%d, .child=%d .parent=%d\n", i,
798 xbc_node_get_data(xbc_nodes + i),
799 xbc_node_is_value(xbc_nodes + i) ? "value" : "key",
800 xbc_nodes[i].next, xbc_nodes[i].child,
801 xbc_nodes[i].parent);