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;
34 static int open_brace[XBC_DEPTH_MAX] __initdata;
35 static int brace_index __initdata;
37 static int __init xbc_parse_error(const char *msg, const char *p)
40 xbc_err_pos = (int)(p - xbc_data);
46 * xbc_root_node() - Get the root node of extended boot config
48 * Return the address of root node of extended boot config. If the
49 * extended boot config is not initiized, return NULL.
51 struct xbc_node * __init xbc_root_node(void)
53 if (unlikely(!xbc_data))
60 * xbc_node_index() - Get the index of XBC node
61 * @node: A target node of getting index.
63 * Return the index number of @node in XBC node list.
65 int __init xbc_node_index(struct xbc_node *node)
67 return node - &xbc_nodes[0];
71 * xbc_node_get_parent() - Get the parent XBC node
74 * Return the parent node of @node. If the node is top node of the tree,
77 struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node)
79 return node->parent == XBC_NODE_MAX ? NULL : &xbc_nodes[node->parent];
83 * xbc_node_get_child() - Get the child XBC node
86 * Return the first child node of @node. If the node has no child, return
89 struct xbc_node * __init xbc_node_get_child(struct xbc_node *node)
91 return node->child ? &xbc_nodes[node->child] : NULL;
95 * xbc_node_get_next() - Get the next sibling XBC node
98 * Return the NEXT sibling node of @node. If the node has no next sibling,
99 * return NULL. Note that even if this returns NULL, it doesn't mean @node
100 * has no siblings. (You also has to check whether the parent's child node
103 struct xbc_node * __init xbc_node_get_next(struct xbc_node *node)
105 return node->next ? &xbc_nodes[node->next] : NULL;
109 * xbc_node_get_data() - Get the data of XBC node
110 * @node: An XBC node.
112 * Return the data (which is always a null terminated string) of @node.
113 * If the node has invalid data, warn and return NULL.
115 const char * __init xbc_node_get_data(struct xbc_node *node)
117 int offset = node->data & ~XBC_VALUE;
119 if (WARN_ON(offset >= xbc_data_size))
122 return xbc_data + offset;
126 xbc_node_match_prefix(struct xbc_node *node, const char **prefix)
128 const char *p = xbc_node_get_data(node);
131 if (strncmp(*prefix, p, len))
145 * xbc_node_find_child() - Find a child node which matches given key
146 * @parent: An XBC node.
147 * @key: A key string.
149 * Search a node under @parent which matches @key. The @key can contain
150 * several words jointed with '.'. If @parent is NULL, this searches the
151 * node from whole tree. Return NULL if no node is matched.
153 struct xbc_node * __init
154 xbc_node_find_child(struct xbc_node *parent, const char *key)
156 struct xbc_node *node;
159 node = xbc_node_get_subkey(parent);
161 node = xbc_root_node();
163 while (node && xbc_node_is_key(node)) {
164 if (!xbc_node_match_prefix(node, &key))
165 node = xbc_node_get_next(node);
166 else if (*key != '\0')
167 node = xbc_node_get_subkey(node);
176 * xbc_node_find_value() - Find a value node which matches given key
177 * @parent: An XBC node.
178 * @key: A key string.
179 * @vnode: A container pointer of found XBC node.
181 * Search a value node under @parent whose (parent) key node matches @key,
182 * store it in *@vnode, and returns the value string.
183 * The @key can contain several words jointed with '.'. If @parent is NULL,
184 * this searches the node from whole tree. Return the value string if a
185 * matched key found, return NULL if no node is matched.
186 * Note that this returns 0-length string and stores NULL in *@vnode if the
187 * key has no value. And also it will return the value of the first entry if
188 * the value is an array.
191 xbc_node_find_value(struct xbc_node *parent, const char *key,
192 struct xbc_node **vnode)
194 struct xbc_node *node = xbc_node_find_child(parent, key);
196 if (!node || !xbc_node_is_key(node))
199 node = xbc_node_get_child(node);
200 if (node && !xbc_node_is_value(node))
206 return node ? xbc_node_get_data(node) : "";
210 * xbc_node_compose_key_after() - Compose partial key string of the XBC node
211 * @root: Root XBC node
212 * @node: Target XBC node.
213 * @buf: A buffer to store the key.
214 * @size: The size of the @buf.
216 * Compose the partial key of the @node into @buf, which is starting right
217 * after @root (@root is not included.) If @root is NULL, this returns full
218 * key words of @node.
219 * Returns the total length of the key stored in @buf. Returns -EINVAL
220 * if @node is NULL or @root is not the ancestor of @node or @root is @node,
221 * or returns -ERANGE if the key depth is deeper than max depth.
222 * This is expected to be used with xbc_find_node() to list up all (child)
223 * keys under given key.
225 int __init xbc_node_compose_key_after(struct xbc_node *root,
226 struct xbc_node *node,
227 char *buf, size_t size)
229 u16 keys[XBC_DEPTH_MAX];
230 int depth = 0, ret = 0, total = 0;
232 if (!node || node == root)
235 if (xbc_node_is_value(node))
236 node = xbc_node_get_parent(node);
238 while (node && node != root) {
239 keys[depth++] = xbc_node_index(node);
240 if (depth == XBC_DEPTH_MAX)
242 node = xbc_node_get_parent(node);
247 while (--depth >= 0) {
248 node = xbc_nodes + keys[depth];
249 ret = snprintf(buf, size, "%s%s", xbc_node_get_data(node),
266 * xbc_node_find_next_leaf() - Find the next leaf node under given node
267 * @root: An XBC root node
268 * @node: An XBC node which starts from.
270 * Search the next leaf node (which means the terminal key node) of @node
271 * under @root node (including @root node itself).
272 * Return the next node or NULL if next leaf node is not found.
274 struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root,
275 struct xbc_node *node)
277 struct xbc_node *next;
279 if (unlikely(!xbc_data))
282 if (!node) { /* First try */
287 /* Leaf node may have a subkey */
288 next = xbc_node_get_subkey(node);
294 if (node == root) /* @root was a leaf, no child node. */
297 while (!node->next) {
298 node = xbc_node_get_parent(node);
301 /* User passed a node which is not uder parent */
305 node = xbc_node_get_next(node);
309 while (node && !xbc_node_is_leaf(node))
310 node = xbc_node_get_child(node);
316 * xbc_node_find_next_key_value() - Find the next key-value pair nodes
317 * @root: An XBC root node
318 * @leaf: A container pointer of XBC node which starts from.
320 * Search the next leaf node (which means the terminal key node) of *@leaf
321 * under @root node. Returns the value and update *@leaf if next leaf node
322 * is found, or NULL if no next leaf node is found.
323 * Note that this returns 0-length string if the key has no value, or
324 * the value of the first entry if the value is an array.
326 const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
327 struct xbc_node **leaf)
329 /* tip must be passed */
333 *leaf = xbc_node_find_next_leaf(root, *leaf);
337 return xbc_node_get_data(xbc_node_get_child(*leaf));
339 return ""; /* No value key */
342 /* XBC parse and tree build */
344 static int __init xbc_init_node(struct xbc_node *node, char *data, u32 flag)
346 unsigned long offset = data - xbc_data;
348 if (WARN_ON(offset >= XBC_DATA_MAX))
351 node->data = (u16)offset | flag;
358 static struct xbc_node * __init xbc_add_node(char *data, u32 flag)
360 struct xbc_node *node;
362 if (xbc_node_num == XBC_NODE_MAX)
365 node = &xbc_nodes[xbc_node_num++];
366 if (xbc_init_node(node, data, flag) < 0)
372 static inline __init struct xbc_node *xbc_last_sibling(struct xbc_node *node)
375 node = xbc_node_get_next(node);
380 static inline __init struct xbc_node *xbc_last_child(struct xbc_node *node)
383 node = xbc_node_get_child(node);
388 static struct xbc_node * __init __xbc_add_sibling(char *data, u32 flag, bool head)
390 struct xbc_node *sib, *node = xbc_add_node(data, flag);
394 /* Ignore @head in this case */
395 node->parent = XBC_NODE_MAX;
396 sib = xbc_last_sibling(xbc_nodes);
397 sib->next = xbc_node_index(node);
399 node->parent = xbc_node_index(last_parent);
400 if (!last_parent->child || head) {
401 node->next = last_parent->child;
402 last_parent->child = xbc_node_index(node);
404 sib = xbc_node_get_child(last_parent);
405 sib = xbc_last_sibling(sib);
406 sib->next = xbc_node_index(node);
410 xbc_parse_error("Too many nodes", data);
415 static inline struct xbc_node * __init xbc_add_sibling(char *data, u32 flag)
417 return __xbc_add_sibling(data, flag, false);
420 static inline struct xbc_node * __init xbc_add_head_sibling(char *data, u32 flag)
422 return __xbc_add_sibling(data, flag, true);
425 static inline __init struct xbc_node *xbc_add_child(char *data, u32 flag)
427 struct xbc_node *node = xbc_add_sibling(data, flag);
435 static inline __init bool xbc_valid_keyword(char *key)
440 while (isalnum(*key) || *key == '-' || *key == '_')
446 static char *skip_comment(char *p)
450 ret = strchr(p, '\n');
459 static char *skip_spaces_until_newline(char *p)
461 while (isspace(*p) && *p != '\n')
466 static int __init __xbc_open_brace(char *p)
468 /* Push the last key as open brace */
469 open_brace[brace_index++] = xbc_node_index(last_parent);
470 if (brace_index >= XBC_DEPTH_MAX)
471 return xbc_parse_error("Exceed max depth of braces", p);
476 static int __init __xbc_close_brace(char *p)
479 if (!last_parent || brace_index < 0 ||
480 (open_brace[brace_index] != xbc_node_index(last_parent)))
481 return xbc_parse_error("Unexpected closing brace", p);
483 if (brace_index == 0)
486 last_parent = &xbc_nodes[open_brace[brace_index - 1]];
492 * Return delimiter or error, no node added. As same as lib/cmdline.c,
493 * you can use " around spaces, but can't escape " for value.
495 static int __init __xbc_parse_value(char **__v, char **__n)
505 if (*v == '"' || *v == '\'') {
511 if (!isprint(c) && !isspace(c))
512 return xbc_parse_error("Non printable value", p);
518 p = skip_spaces_until_newline(p);
520 if (c && !strchr(",;\n#}", c))
521 return xbc_parse_error("No value delimiter", p);
526 if (strchr(",;\n#}", c)) {
533 return xbc_parse_error("No closing quotes", p);
536 c = '\n'; /* A comment must be treated as a newline */
544 static int __init xbc_parse_array(char **__v)
546 struct xbc_node *node;
550 if (last_parent->child)
551 last_parent = xbc_node_get_child(last_parent);
554 c = __xbc_parse_value(__v, &next);
558 node = xbc_add_child(*__v, XBC_VALUE);
569 struct xbc_node *find_match_node(struct xbc_node *node, char *k)
572 if (!strcmp(xbc_node_get_data(node), k))
574 node = xbc_node_get_next(node);
579 static int __init __xbc_add_key(char *k)
581 struct xbc_node *node, *child;
583 if (!xbc_valid_keyword(k))
584 return xbc_parse_error("Invalid keyword", k);
586 if (unlikely(xbc_node_num == 0))
589 if (!last_parent) /* the first level */
590 node = find_match_node(xbc_nodes, k);
592 child = xbc_node_get_child(last_parent);
593 /* Since the value node is the first child, skip it. */
594 if (child && xbc_node_is_value(child))
595 child = xbc_node_get_next(child);
596 node = find_match_node(child, k);
603 node = xbc_add_child(k, XBC_KEY);
610 static int __init __xbc_parse_keys(char *k)
616 while ((p = strchr(k, '.'))) {
618 ret = __xbc_add_key(k);
624 return __xbc_add_key(k);
627 static int __init xbc_parse_kv(char **k, char *v, int op)
629 struct xbc_node *prev_parent = last_parent;
630 struct xbc_node *child;
634 ret = __xbc_parse_keys(*k);
638 c = __xbc_parse_value(&v, &next);
642 child = xbc_node_get_child(last_parent);
643 if (child && xbc_node_is_value(child)) {
645 return xbc_parse_error("Value is redefined", v);
647 unsigned short nidx = child->next;
649 xbc_init_node(child, v, XBC_VALUE);
650 child->next = nidx; /* keep subkeys */
654 last_parent = xbc_last_child(child);
656 /* The value node should always be the first child */
657 if (!xbc_add_head_sibling(v, XBC_VALUE))
661 if (c == ',') { /* Array */
662 c = xbc_parse_array(&next);
667 last_parent = prev_parent;
670 ret = __xbc_close_brace(next - 1);
680 static int __init xbc_parse_key(char **k, char *n)
682 struct xbc_node *prev_parent = last_parent;
687 ret = __xbc_parse_keys(*k);
690 last_parent = prev_parent;
697 static int __init xbc_open_brace(char **k, char *n)
701 ret = __xbc_parse_keys(*k);
706 return __xbc_open_brace(n - 1);
709 static int __init xbc_close_brace(char **k, char *n)
713 ret = xbc_parse_key(k, n);
716 /* k is updated in xbc_parse_key() */
718 return __xbc_close_brace(n - 1);
721 static int __init xbc_verify_tree(void)
723 int i, depth, len, wlen;
724 struct xbc_node *n, *m;
728 n = &xbc_nodes[open_brace[brace_index]];
729 return xbc_parse_error("Brace is not closed",
730 xbc_node_get_data(n));
734 if (xbc_node_num == 0) {
735 xbc_parse_error("Empty config", xbc_data);
739 for (i = 0; i < xbc_node_num; i++) {
740 if (xbc_nodes[i].next > xbc_node_num) {
741 return xbc_parse_error("No closing brace",
742 xbc_node_get_data(xbc_nodes + i));
746 /* Key tree limitation check */
752 wlen = strlen(xbc_node_get_data(n)) + 1;
754 if (len > XBC_KEYLEN_MAX)
755 return xbc_parse_error("Too long key length",
756 xbc_node_get_data(n));
758 m = xbc_node_get_child(n);
759 if (m && xbc_node_is_key(m)) {
762 if (depth > XBC_DEPTH_MAX)
763 return xbc_parse_error("Too many key words",
764 xbc_node_get_data(n));
768 m = xbc_node_get_next(n);
770 n = xbc_node_get_parent(n);
773 len -= strlen(xbc_node_get_data(n)) + 1;
775 m = xbc_node_get_next(n);
784 * xbc_destroy_all() - Clean up all parsed bootconfig
786 * This clears all data structures of parsed bootconfig on memory.
787 * If you need to reuse xbc_init() with new boot config, you can
790 void __init xbc_destroy_all(void)
795 memblock_free(__pa(xbc_nodes), sizeof(struct xbc_node) * XBC_NODE_MAX);
801 * xbc_init() - Parse given XBC file and build XBC internal tree
802 * @buf: boot config text
803 * @emsg: A pointer of const char * to store the error message
804 * @epos: A pointer of int to store the error position
806 * This parses the boot config text in @buf. @buf must be a
807 * null terminated string and smaller than XBC_DATA_MAX.
808 * Return the number of stored nodes (>0) if succeeded, or -errno
809 * if there is any error.
810 * In error cases, @emsg will be updated with an error message and
811 * @epos will be updated with the error position which is the byte offset
812 * of @buf. If the error is not a parser error, @epos will be -1.
814 int __init xbc_init(char *buf, const char **emsg, int *epos)
824 *emsg = "Bootconfig is already initialized";
829 if (ret > XBC_DATA_MAX - 1 || ret == 0) {
831 *emsg = ret ? "Config data is too big" :
832 "Config data is empty";
836 xbc_nodes = memblock_alloc(sizeof(struct xbc_node) * XBC_NODE_MAX,
840 *emsg = "Failed to allocate bootconfig nodes";
843 memset(xbc_nodes, 0, sizeof(struct xbc_node) * XBC_NODE_MAX);
845 xbc_data_size = ret + 1;
850 q = strpbrk(p, "{}=+;:\n#");
854 ret = xbc_parse_error("No delimiter", p);
864 ret = xbc_parse_error(c == '+' ?
865 "Wrong '+' operator" :
866 "Wrong ':' operator",
872 ret = xbc_parse_kv(&p, q, c);
875 ret = xbc_open_brace(&p, q);
882 ret = xbc_parse_key(&p, q);
885 ret = xbc_close_brace(&p, q);
891 ret = xbc_verify_tree();
906 * xbc_debug_dump() - Dump current XBC node list
908 * Dump the current XBC node list on printk buffer for debug.
910 void __init xbc_debug_dump(void)
914 for (i = 0; i < xbc_node_num; i++) {
915 pr_debug("[%d] %s (%s) .next=%d, .child=%d .parent=%d\n", i,
916 xbc_node_get_data(xbc_nodes + i),
917 xbc_node_is_value(xbc_nodes + i) ? "value" : "key",
918 xbc_nodes[i].next, xbc_nodes[i].child,
919 xbc_nodes[i].parent);