1 // SPDX-License-Identifier: GPL-2.0
8 #include <linux/bootconfig.h>
10 #include <linux/ctype.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/memblock.h>
14 #include <linux/string.h>
16 #ifdef CONFIG_BOOT_CONFIG_EMBED
17 /* embedded_bootconfig_data is defined in bootconfig-data.S */
18 extern __visible const char embedded_bootconfig_data[];
19 extern __visible const char embedded_bootconfig_data_end[];
21 const char * __init xbc_get_embedded_bootconfig(size_t *size)
23 *size = embedded_bootconfig_data_end - embedded_bootconfig_data;
24 return (*size) ? embedded_bootconfig_data : NULL;
28 #else /* !__KERNEL__ */
30 * NOTE: This is only for tools/bootconfig, because tools/bootconfig will
31 * run the parser sanity test.
32 * This does NOT mean lib/bootconfig.c is available in the user space.
33 * However, if you change this file, please make sure the tools/bootconfig
34 * has no issue on building and running.
36 #include <linux/bootconfig.h>
40 * Extra Boot Config (XBC) is given as tree-structured ascii text of
41 * key-value pairs on memory.
42 * xbc_parse() parses the text to build a simple tree. Each tree node is
43 * simply a key word or a value. A key node may have a next key node or/and
44 * a child node (both key and value). A value node may have a next value
48 static struct xbc_node *xbc_nodes __initdata;
49 static int xbc_node_num __initdata;
50 static char *xbc_data __initdata;
51 static size_t xbc_data_size __initdata;
52 static struct xbc_node *last_parent __initdata;
53 static const char *xbc_err_msg __initdata;
54 static int xbc_err_pos __initdata;
55 static int open_brace[XBC_DEPTH_MAX] __initdata;
56 static int brace_index __initdata;
59 static inline void * __init xbc_alloc_mem(size_t size)
61 return memblock_alloc(size, SMP_CACHE_BYTES);
64 static inline void __init xbc_free_mem(void *addr, size_t size, bool early)
67 memblock_free(addr, size);
69 memblock_free_late(__pa(addr), size);
72 #else /* !__KERNEL__ */
74 static inline void *xbc_alloc_mem(size_t size)
79 static inline void xbc_free_mem(void *addr, size_t size, bool early)
85 * xbc_get_info() - Get the information of loaded boot config
86 * @node_size: A pointer to store the number of nodes.
87 * @data_size: A pointer to store the size of bootconfig data.
89 * Get the number of used nodes in @node_size if it is not NULL,
90 * and the size of bootconfig data in @data_size if it is not NULL.
91 * Return 0 if the boot config is initialized, or return -ENODEV.
93 int __init xbc_get_info(int *node_size, size_t *data_size)
99 *node_size = xbc_node_num;
101 *data_size = xbc_data_size;
105 static int __init xbc_parse_error(const char *msg, const char *p)
108 xbc_err_pos = (int)(p - xbc_data);
114 * xbc_root_node() - Get the root node of extended boot config
116 * Return the address of root node of extended boot config. If the
117 * extended boot config is not initiized, return NULL.
119 struct xbc_node * __init xbc_root_node(void)
121 if (unlikely(!xbc_data))
128 * xbc_node_index() - Get the index of XBC node
129 * @node: A target node of getting index.
131 * Return the index number of @node in XBC node list.
133 int __init xbc_node_index(struct xbc_node *node)
135 return node - &xbc_nodes[0];
139 * xbc_node_get_parent() - Get the parent XBC node
140 * @node: An XBC node.
142 * Return the parent node of @node. If the node is top node of the tree,
145 struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node)
147 return node->parent == XBC_NODE_MAX ? NULL : &xbc_nodes[node->parent];
151 * xbc_node_get_child() - Get the child XBC node
152 * @node: An XBC node.
154 * Return the first child node of @node. If the node has no child, return
157 struct xbc_node * __init xbc_node_get_child(struct xbc_node *node)
159 return node->child ? &xbc_nodes[node->child] : NULL;
163 * xbc_node_get_next() - Get the next sibling XBC node
164 * @node: An XBC node.
166 * Return the NEXT sibling node of @node. If the node has no next sibling,
167 * return NULL. Note that even if this returns NULL, it doesn't mean @node
168 * has no siblings. (You also has to check whether the parent's child node
171 struct xbc_node * __init xbc_node_get_next(struct xbc_node *node)
173 return node->next ? &xbc_nodes[node->next] : NULL;
177 * xbc_node_get_data() - Get the data of XBC node
178 * @node: An XBC node.
180 * Return the data (which is always a null terminated string) of @node.
181 * If the node has invalid data, warn and return NULL.
183 const char * __init xbc_node_get_data(struct xbc_node *node)
185 int offset = node->data & ~XBC_VALUE;
187 if (WARN_ON(offset >= xbc_data_size))
190 return xbc_data + offset;
194 xbc_node_match_prefix(struct xbc_node *node, const char **prefix)
196 const char *p = xbc_node_get_data(node);
199 if (strncmp(*prefix, p, len))
213 * xbc_node_find_subkey() - Find a subkey node which matches given key
214 * @parent: An XBC node.
215 * @key: A key string.
217 * Search a key node under @parent which matches @key. The @key can contain
218 * several words jointed with '.'. If @parent is NULL, this searches the
219 * node from whole tree. Return NULL if no node is matched.
221 struct xbc_node * __init
222 xbc_node_find_subkey(struct xbc_node *parent, const char *key)
224 struct xbc_node *node;
227 node = xbc_node_get_subkey(parent);
229 node = xbc_root_node();
231 while (node && xbc_node_is_key(node)) {
232 if (!xbc_node_match_prefix(node, &key))
233 node = xbc_node_get_next(node);
234 else if (*key != '\0')
235 node = xbc_node_get_subkey(node);
244 * xbc_node_find_value() - Find a value node which matches given key
245 * @parent: An XBC node.
246 * @key: A key string.
247 * @vnode: A container pointer of found XBC node.
249 * Search a value node under @parent whose (parent) key node matches @key,
250 * store it in *@vnode, and returns the value string.
251 * The @key can contain several words jointed with '.'. If @parent is NULL,
252 * this searches the node from whole tree. Return the value string if a
253 * matched key found, return NULL if no node is matched.
254 * Note that this returns 0-length string and stores NULL in *@vnode if the
255 * key has no value. And also it will return the value of the first entry if
256 * the value is an array.
259 xbc_node_find_value(struct xbc_node *parent, const char *key,
260 struct xbc_node **vnode)
262 struct xbc_node *node = xbc_node_find_subkey(parent, key);
264 if (!node || !xbc_node_is_key(node))
267 node = xbc_node_get_child(node);
268 if (node && !xbc_node_is_value(node))
274 return node ? xbc_node_get_data(node) : "";
278 * xbc_node_compose_key_after() - Compose partial key string of the XBC node
279 * @root: Root XBC node
280 * @node: Target XBC node.
281 * @buf: A buffer to store the key.
282 * @size: The size of the @buf.
284 * Compose the partial key of the @node into @buf, which is starting right
285 * after @root (@root is not included.) If @root is NULL, this returns full
286 * key words of @node.
287 * Returns the total length of the key stored in @buf. Returns -EINVAL
288 * if @node is NULL or @root is not the ancestor of @node or @root is @node,
289 * or returns -ERANGE if the key depth is deeper than max depth.
290 * This is expected to be used with xbc_find_node() to list up all (child)
291 * keys under given key.
293 int __init xbc_node_compose_key_after(struct xbc_node *root,
294 struct xbc_node *node,
295 char *buf, size_t size)
297 uint16_t keys[XBC_DEPTH_MAX];
298 int depth = 0, ret = 0, total = 0;
300 if (!node || node == root)
303 if (xbc_node_is_value(node))
304 node = xbc_node_get_parent(node);
306 while (node && node != root) {
307 keys[depth++] = xbc_node_index(node);
308 if (depth == XBC_DEPTH_MAX)
310 node = xbc_node_get_parent(node);
315 while (--depth >= 0) {
316 node = xbc_nodes + keys[depth];
317 ret = snprintf(buf, size, "%s%s", xbc_node_get_data(node),
334 * xbc_node_find_next_leaf() - Find the next leaf node under given node
335 * @root: An XBC root node
336 * @node: An XBC node which starts from.
338 * Search the next leaf node (which means the terminal key node) of @node
339 * under @root node (including @root node itself).
340 * Return the next node or NULL if next leaf node is not found.
342 struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root,
343 struct xbc_node *node)
345 struct xbc_node *next;
347 if (unlikely(!xbc_data))
350 if (!node) { /* First try */
355 /* Leaf node may have a subkey */
356 next = xbc_node_get_subkey(node);
362 if (node == root) /* @root was a leaf, no child node. */
365 while (!node->next) {
366 node = xbc_node_get_parent(node);
369 /* User passed a node which is not uder parent */
373 node = xbc_node_get_next(node);
377 while (node && !xbc_node_is_leaf(node))
378 node = xbc_node_get_child(node);
384 * xbc_node_find_next_key_value() - Find the next key-value pair nodes
385 * @root: An XBC root node
386 * @leaf: A container pointer of XBC node which starts from.
388 * Search the next leaf node (which means the terminal key node) of *@leaf
389 * under @root node. Returns the value and update *@leaf if next leaf node
390 * is found, or NULL if no next leaf node is found.
391 * Note that this returns 0-length string if the key has no value, or
392 * the value of the first entry if the value is an array.
394 const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
395 struct xbc_node **leaf)
397 /* tip must be passed */
401 *leaf = xbc_node_find_next_leaf(root, *leaf);
405 return xbc_node_get_data(xbc_node_get_child(*leaf));
407 return ""; /* No value key */
410 /* XBC parse and tree build */
412 static int __init xbc_init_node(struct xbc_node *node, char *data, uint32_t flag)
414 unsigned long offset = data - xbc_data;
416 if (WARN_ON(offset >= XBC_DATA_MAX))
419 node->data = (uint16_t)offset | flag;
426 static struct xbc_node * __init xbc_add_node(char *data, uint32_t flag)
428 struct xbc_node *node;
430 if (xbc_node_num == XBC_NODE_MAX)
433 node = &xbc_nodes[xbc_node_num++];
434 if (xbc_init_node(node, data, flag) < 0)
440 static inline __init struct xbc_node *xbc_last_sibling(struct xbc_node *node)
443 node = xbc_node_get_next(node);
448 static inline __init struct xbc_node *xbc_last_child(struct xbc_node *node)
451 node = xbc_node_get_child(node);
456 static struct xbc_node * __init __xbc_add_sibling(char *data, uint32_t flag, bool head)
458 struct xbc_node *sib, *node = xbc_add_node(data, flag);
462 /* Ignore @head in this case */
463 node->parent = XBC_NODE_MAX;
464 sib = xbc_last_sibling(xbc_nodes);
465 sib->next = xbc_node_index(node);
467 node->parent = xbc_node_index(last_parent);
468 if (!last_parent->child || head) {
469 node->next = last_parent->child;
470 last_parent->child = xbc_node_index(node);
472 sib = xbc_node_get_child(last_parent);
473 sib = xbc_last_sibling(sib);
474 sib->next = xbc_node_index(node);
478 xbc_parse_error("Too many nodes", data);
483 static inline struct xbc_node * __init xbc_add_sibling(char *data, uint32_t flag)
485 return __xbc_add_sibling(data, flag, false);
488 static inline struct xbc_node * __init xbc_add_head_sibling(char *data, uint32_t flag)
490 return __xbc_add_sibling(data, flag, true);
493 static inline __init struct xbc_node *xbc_add_child(char *data, uint32_t flag)
495 struct xbc_node *node = xbc_add_sibling(data, flag);
503 static inline __init bool xbc_valid_keyword(char *key)
508 while (isalnum(*key) || *key == '-' || *key == '_')
514 static char *skip_comment(char *p)
518 ret = strchr(p, '\n');
527 static char *skip_spaces_until_newline(char *p)
529 while (isspace(*p) && *p != '\n')
534 static int __init __xbc_open_brace(char *p)
536 /* Push the last key as open brace */
537 open_brace[brace_index++] = xbc_node_index(last_parent);
538 if (brace_index >= XBC_DEPTH_MAX)
539 return xbc_parse_error("Exceed max depth of braces", p);
544 static int __init __xbc_close_brace(char *p)
547 if (!last_parent || brace_index < 0 ||
548 (open_brace[brace_index] != xbc_node_index(last_parent)))
549 return xbc_parse_error("Unexpected closing brace", p);
551 if (brace_index == 0)
554 last_parent = &xbc_nodes[open_brace[brace_index - 1]];
560 * Return delimiter or error, no node added. As same as lib/cmdline.c,
561 * you can use " around spaces, but can't escape " for value.
563 static int __init __xbc_parse_value(char **__v, char **__n)
573 if (*v == '"' || *v == '\'') {
579 if (!isprint(c) && !isspace(c))
580 return xbc_parse_error("Non printable value", p);
586 p = skip_spaces_until_newline(p);
588 if (c && !strchr(",;\n#}", c))
589 return xbc_parse_error("No value delimiter", p);
594 if (strchr(",;\n#}", c)) {
601 return xbc_parse_error("No closing quotes", p);
604 c = '\n'; /* A comment must be treated as a newline */
612 static int __init xbc_parse_array(char **__v)
614 struct xbc_node *node;
618 if (last_parent->child)
619 last_parent = xbc_node_get_child(last_parent);
622 c = __xbc_parse_value(__v, &next);
626 node = xbc_add_child(*__v, XBC_VALUE);
637 struct xbc_node *find_match_node(struct xbc_node *node, char *k)
640 if (!strcmp(xbc_node_get_data(node), k))
642 node = xbc_node_get_next(node);
647 static int __init __xbc_add_key(char *k)
649 struct xbc_node *node, *child;
651 if (!xbc_valid_keyword(k))
652 return xbc_parse_error("Invalid keyword", k);
654 if (unlikely(xbc_node_num == 0))
657 if (!last_parent) /* the first level */
658 node = find_match_node(xbc_nodes, k);
660 child = xbc_node_get_child(last_parent);
661 /* Since the value node is the first child, skip it. */
662 if (child && xbc_node_is_value(child))
663 child = xbc_node_get_next(child);
664 node = find_match_node(child, k);
671 node = xbc_add_child(k, XBC_KEY);
678 static int __init __xbc_parse_keys(char *k)
684 while ((p = strchr(k, '.'))) {
686 ret = __xbc_add_key(k);
692 return __xbc_add_key(k);
695 static int __init xbc_parse_kv(char **k, char *v, int op)
697 struct xbc_node *prev_parent = last_parent;
698 struct xbc_node *child;
702 ret = __xbc_parse_keys(*k);
706 c = __xbc_parse_value(&v, &next);
710 child = xbc_node_get_child(last_parent);
711 if (child && xbc_node_is_value(child)) {
713 return xbc_parse_error("Value is redefined", v);
715 unsigned short nidx = child->next;
717 xbc_init_node(child, v, XBC_VALUE);
718 child->next = nidx; /* keep subkeys */
722 last_parent = xbc_last_child(child);
724 /* The value node should always be the first child */
725 if (!xbc_add_head_sibling(v, XBC_VALUE))
729 if (c == ',') { /* Array */
730 c = xbc_parse_array(&next);
735 last_parent = prev_parent;
738 ret = __xbc_close_brace(next - 1);
748 static int __init xbc_parse_key(char **k, char *n)
750 struct xbc_node *prev_parent = last_parent;
755 ret = __xbc_parse_keys(*k);
758 last_parent = prev_parent;
765 static int __init xbc_open_brace(char **k, char *n)
769 ret = __xbc_parse_keys(*k);
774 return __xbc_open_brace(n - 1);
777 static int __init xbc_close_brace(char **k, char *n)
781 ret = xbc_parse_key(k, n);
784 /* k is updated in xbc_parse_key() */
786 return __xbc_close_brace(n - 1);
789 static int __init xbc_verify_tree(void)
791 int i, depth, len, wlen;
792 struct xbc_node *n, *m;
796 n = &xbc_nodes[open_brace[brace_index]];
797 return xbc_parse_error("Brace is not closed",
798 xbc_node_get_data(n));
802 if (xbc_node_num == 0) {
803 xbc_parse_error("Empty config", xbc_data);
807 for (i = 0; i < xbc_node_num; i++) {
808 if (xbc_nodes[i].next > xbc_node_num) {
809 return xbc_parse_error("No closing brace",
810 xbc_node_get_data(xbc_nodes + i));
814 /* Key tree limitation check */
820 wlen = strlen(xbc_node_get_data(n)) + 1;
822 if (len > XBC_KEYLEN_MAX)
823 return xbc_parse_error("Too long key length",
824 xbc_node_get_data(n));
826 m = xbc_node_get_child(n);
827 if (m && xbc_node_is_key(m)) {
830 if (depth > XBC_DEPTH_MAX)
831 return xbc_parse_error("Too many key words",
832 xbc_node_get_data(n));
836 m = xbc_node_get_next(n);
838 n = xbc_node_get_parent(n);
841 len -= strlen(xbc_node_get_data(n)) + 1;
843 m = xbc_node_get_next(n);
851 /* Need to setup xbc_data and xbc_nodes before call this. */
852 static int __init xbc_parse_tree(void)
860 q = strpbrk(p, "{}=+;:\n#");
864 ret = xbc_parse_error("No delimiter", p);
874 ret = xbc_parse_error(c == '+' ?
875 "Wrong '+' operator" :
876 "Wrong ':' operator",
882 ret = xbc_parse_kv(&p, q, c);
885 ret = xbc_open_brace(&p, q);
892 ret = xbc_parse_key(&p, q);
895 ret = xbc_close_brace(&p, q);
904 * _xbc_exit() - Clean up all parsed bootconfig
905 * @early: Set true if this is called before budy system is initialized.
907 * This clears all data structures of parsed bootconfig on memory.
908 * If you need to reuse xbc_init() with new boot config, you can
911 void __init _xbc_exit(bool early)
913 xbc_free_mem(xbc_data, xbc_data_size, early);
917 xbc_free_mem(xbc_nodes, sizeof(struct xbc_node) * XBC_NODE_MAX, early);
923 * xbc_init() - Parse given XBC file and build XBC internal tree
924 * @data: The boot config text original data
925 * @size: The size of @data
926 * @emsg: A pointer of const char * to store the error message
927 * @epos: A pointer of int to store the error position
929 * This parses the boot config text in @data. @size must be smaller
931 * Return the number of stored nodes (>0) if succeeded, or -errno
932 * if there is any error.
933 * In error cases, @emsg will be updated with an error message and
934 * @epos will be updated with the error position which is the byte offset
935 * of @buf. If the error is not a parser error, @epos will be -1.
937 int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos)
946 *emsg = "Bootconfig is already initialized";
949 if (size > XBC_DATA_MAX || size == 0) {
951 *emsg = size ? "Config data is too big" :
952 "Config data is empty";
956 xbc_data = xbc_alloc_mem(size + 1);
959 *emsg = "Failed to allocate bootconfig data";
962 memcpy(xbc_data, data, size);
963 xbc_data[size] = '\0';
964 xbc_data_size = size + 1;
966 xbc_nodes = xbc_alloc_mem(sizeof(struct xbc_node) * XBC_NODE_MAX);
969 *emsg = "Failed to allocate bootconfig nodes";
973 memset(xbc_nodes, 0, sizeof(struct xbc_node) * XBC_NODE_MAX);
975 ret = xbc_parse_tree();
977 ret = xbc_verify_tree();