1 // SPDX-License-Identifier: GPL-2.0
8 * NOTE: This is only for tools/bootconfig, because tools/bootconfig will
9 * run the parser sanity test.
10 * This does NOT mean lib/bootconfig.c is available in the user space.
11 * However, if you change this file, please make sure the tools/bootconfig
12 * has no issue on building and running.
14 #include <linux/bootconfig.h>
17 #include <linux/bug.h>
18 #include <linux/ctype.h>
19 #include <linux/errno.h>
20 #include <linux/kernel.h>
21 #include <linux/memblock.h>
22 #include <linux/string.h>
24 #ifdef CONFIG_BOOT_CONFIG_EMBED
25 /* embedded_bootconfig_data is defined in bootconfig-data.S */
26 extern __visible const char embedded_bootconfig_data[];
27 extern __visible const char embedded_bootconfig_data_end[];
29 const char * __init xbc_get_embedded_bootconfig(size_t *size)
31 *size = embedded_bootconfig_data_end - embedded_bootconfig_data;
32 return (*size) ? embedded_bootconfig_data : NULL;
38 * Extra Boot Config (XBC) is given as tree-structured ascii text of
39 * key-value pairs on memory.
40 * xbc_parse() parses the text to build a simple tree. Each tree node is
41 * simply a key word or a value. A key node may have a next key node or/and
42 * a child node (both key and value). A value node may have a next value
46 static struct xbc_node *xbc_nodes __initdata;
47 static int xbc_node_num __initdata;
48 static char *xbc_data __initdata;
49 static size_t xbc_data_size __initdata;
50 static struct xbc_node *last_parent __initdata;
51 static const char *xbc_err_msg __initdata;
52 static int xbc_err_pos __initdata;
53 static int open_brace[XBC_DEPTH_MAX] __initdata;
54 static int brace_index __initdata;
57 static inline void * __init xbc_alloc_mem(size_t size)
59 return memblock_alloc(size, SMP_CACHE_BYTES);
62 static inline void __init xbc_free_mem(void *addr, size_t size, bool early)
65 memblock_free(addr, size);
67 memblock_free_late(__pa(addr), size);
70 #else /* !__KERNEL__ */
72 static inline void *xbc_alloc_mem(size_t size)
77 static inline void xbc_free_mem(void *addr, size_t size, bool early)
83 * xbc_get_info() - Get the information of loaded boot config
84 * @node_size: A pointer to store the number of nodes.
85 * @data_size: A pointer to store the size of bootconfig data.
87 * Get the number of used nodes in @node_size if it is not NULL,
88 * and the size of bootconfig data in @data_size if it is not NULL.
89 * Return 0 if the boot config is initialized, or return -ENODEV.
91 int __init xbc_get_info(int *node_size, size_t *data_size)
97 *node_size = xbc_node_num;
99 *data_size = xbc_data_size;
103 static int __init xbc_parse_error(const char *msg, const char *p)
106 xbc_err_pos = (int)(p - xbc_data);
112 * xbc_root_node() - Get the root node of extended boot config
114 * Return the address of root node of extended boot config. If the
115 * extended boot config is not initiized, return NULL.
117 struct xbc_node * __init xbc_root_node(void)
119 if (unlikely(!xbc_data))
126 * xbc_node_index() - Get the index of XBC node
127 * @node: A target node of getting index.
129 * Return the index number of @node in XBC node list.
131 int __init xbc_node_index(struct xbc_node *node)
133 return node - &xbc_nodes[0];
137 * xbc_node_get_parent() - Get the parent XBC node
138 * @node: An XBC node.
140 * Return the parent node of @node. If the node is top node of the tree,
143 struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node)
145 return node->parent == XBC_NODE_MAX ? NULL : &xbc_nodes[node->parent];
149 * xbc_node_get_child() - Get the child XBC node
150 * @node: An XBC node.
152 * Return the first child node of @node. If the node has no child, return
155 struct xbc_node * __init xbc_node_get_child(struct xbc_node *node)
157 return node->child ? &xbc_nodes[node->child] : NULL;
161 * xbc_node_get_next() - Get the next sibling XBC node
162 * @node: An XBC node.
164 * Return the NEXT sibling node of @node. If the node has no next sibling,
165 * return NULL. Note that even if this returns NULL, it doesn't mean @node
166 * has no siblings. (You also has to check whether the parent's child node
169 struct xbc_node * __init xbc_node_get_next(struct xbc_node *node)
171 return node->next ? &xbc_nodes[node->next] : NULL;
175 * xbc_node_get_data() - Get the data of XBC node
176 * @node: An XBC node.
178 * Return the data (which is always a null terminated string) of @node.
179 * If the node has invalid data, warn and return NULL.
181 const char * __init xbc_node_get_data(struct xbc_node *node)
183 int offset = node->data & ~XBC_VALUE;
185 if (WARN_ON(offset >= xbc_data_size))
188 return xbc_data + offset;
192 xbc_node_match_prefix(struct xbc_node *node, const char **prefix)
194 const char *p = xbc_node_get_data(node);
197 if (strncmp(*prefix, p, len))
211 * xbc_node_find_subkey() - Find a subkey node which matches given key
212 * @parent: An XBC node.
213 * @key: A key string.
215 * Search a key node under @parent which matches @key. The @key can contain
216 * several words jointed with '.'. If @parent is NULL, this searches the
217 * node from whole tree. Return NULL if no node is matched.
219 struct xbc_node * __init
220 xbc_node_find_subkey(struct xbc_node *parent, const char *key)
222 struct xbc_node *node;
225 node = xbc_node_get_subkey(parent);
227 node = xbc_root_node();
229 while (node && xbc_node_is_key(node)) {
230 if (!xbc_node_match_prefix(node, &key))
231 node = xbc_node_get_next(node);
232 else if (*key != '\0')
233 node = xbc_node_get_subkey(node);
242 * xbc_node_find_value() - Find a value node which matches given key
243 * @parent: An XBC node.
244 * @key: A key string.
245 * @vnode: A container pointer of found XBC node.
247 * Search a value node under @parent whose (parent) key node matches @key,
248 * store it in *@vnode, and returns the value string.
249 * The @key can contain several words jointed with '.'. If @parent is NULL,
250 * this searches the node from whole tree. Return the value string if a
251 * matched key found, return NULL if no node is matched.
252 * Note that this returns 0-length string and stores NULL in *@vnode if the
253 * key has no value. And also it will return the value of the first entry if
254 * the value is an array.
257 xbc_node_find_value(struct xbc_node *parent, const char *key,
258 struct xbc_node **vnode)
260 struct xbc_node *node = xbc_node_find_subkey(parent, key);
262 if (!node || !xbc_node_is_key(node))
265 node = xbc_node_get_child(node);
266 if (node && !xbc_node_is_value(node))
272 return node ? xbc_node_get_data(node) : "";
276 * xbc_node_compose_key_after() - Compose partial key string of the XBC node
277 * @root: Root XBC node
278 * @node: Target XBC node.
279 * @buf: A buffer to store the key.
280 * @size: The size of the @buf.
282 * Compose the partial key of the @node into @buf, which is starting right
283 * after @root (@root is not included.) If @root is NULL, this returns full
284 * key words of @node.
285 * Returns the total length of the key stored in @buf. Returns -EINVAL
286 * if @node is NULL or @root is not the ancestor of @node or @root is @node,
287 * or returns -ERANGE if the key depth is deeper than max depth.
288 * This is expected to be used with xbc_find_node() to list up all (child)
289 * keys under given key.
291 int __init xbc_node_compose_key_after(struct xbc_node *root,
292 struct xbc_node *node,
293 char *buf, size_t size)
295 uint16_t keys[XBC_DEPTH_MAX];
296 int depth = 0, ret = 0, total = 0;
298 if (!node || node == root)
301 if (xbc_node_is_value(node))
302 node = xbc_node_get_parent(node);
304 while (node && node != root) {
305 keys[depth++] = xbc_node_index(node);
306 if (depth == XBC_DEPTH_MAX)
308 node = xbc_node_get_parent(node);
313 while (--depth >= 0) {
314 node = xbc_nodes + keys[depth];
315 ret = snprintf(buf, size, "%s%s", xbc_node_get_data(node),
332 * xbc_node_find_next_leaf() - Find the next leaf node under given node
333 * @root: An XBC root node
334 * @node: An XBC node which starts from.
336 * Search the next leaf node (which means the terminal key node) of @node
337 * under @root node (including @root node itself).
338 * Return the next node or NULL if next leaf node is not found.
340 struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root,
341 struct xbc_node *node)
343 struct xbc_node *next;
345 if (unlikely(!xbc_data))
348 if (!node) { /* First try */
353 /* Leaf node may have a subkey */
354 next = xbc_node_get_subkey(node);
360 if (node == root) /* @root was a leaf, no child node. */
363 while (!node->next) {
364 node = xbc_node_get_parent(node);
367 /* User passed a node which is not uder parent */
371 node = xbc_node_get_next(node);
375 while (node && !xbc_node_is_leaf(node))
376 node = xbc_node_get_child(node);
382 * xbc_node_find_next_key_value() - Find the next key-value pair nodes
383 * @root: An XBC root node
384 * @leaf: A container pointer of XBC node which starts from.
386 * Search the next leaf node (which means the terminal key node) of *@leaf
387 * under @root node. Returns the value and update *@leaf if next leaf node
388 * is found, or NULL if no next leaf node is found.
389 * Note that this returns 0-length string if the key has no value, or
390 * the value of the first entry if the value is an array.
392 const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
393 struct xbc_node **leaf)
395 /* tip must be passed */
399 *leaf = xbc_node_find_next_leaf(root, *leaf);
403 return xbc_node_get_data(xbc_node_get_child(*leaf));
405 return ""; /* No value key */
408 /* XBC parse and tree build */
410 static int __init xbc_init_node(struct xbc_node *node, char *data, uint32_t flag)
412 unsigned long offset = data - xbc_data;
414 if (WARN_ON(offset >= XBC_DATA_MAX))
417 node->data = (uint16_t)offset | flag;
424 static struct xbc_node * __init xbc_add_node(char *data, uint32_t flag)
426 struct xbc_node *node;
428 if (xbc_node_num == XBC_NODE_MAX)
431 node = &xbc_nodes[xbc_node_num++];
432 if (xbc_init_node(node, data, flag) < 0)
438 static inline __init struct xbc_node *xbc_last_sibling(struct xbc_node *node)
441 node = xbc_node_get_next(node);
446 static inline __init struct xbc_node *xbc_last_child(struct xbc_node *node)
449 node = xbc_node_get_child(node);
454 static struct xbc_node * __init __xbc_add_sibling(char *data, uint32_t flag, bool head)
456 struct xbc_node *sib, *node = xbc_add_node(data, flag);
460 /* Ignore @head in this case */
461 node->parent = XBC_NODE_MAX;
462 sib = xbc_last_sibling(xbc_nodes);
463 sib->next = xbc_node_index(node);
465 node->parent = xbc_node_index(last_parent);
466 if (!last_parent->child || head) {
467 node->next = last_parent->child;
468 last_parent->child = xbc_node_index(node);
470 sib = xbc_node_get_child(last_parent);
471 sib = xbc_last_sibling(sib);
472 sib->next = xbc_node_index(node);
476 xbc_parse_error("Too many nodes", data);
481 static inline struct xbc_node * __init xbc_add_sibling(char *data, uint32_t flag)
483 return __xbc_add_sibling(data, flag, false);
486 static inline struct xbc_node * __init xbc_add_head_sibling(char *data, uint32_t flag)
488 return __xbc_add_sibling(data, flag, true);
491 static inline __init struct xbc_node *xbc_add_child(char *data, uint32_t flag)
493 struct xbc_node *node = xbc_add_sibling(data, flag);
501 static inline __init bool xbc_valid_keyword(char *key)
506 while (isalnum(*key) || *key == '-' || *key == '_')
512 static char *skip_comment(char *p)
516 ret = strchr(p, '\n');
525 static char *skip_spaces_until_newline(char *p)
527 while (isspace(*p) && *p != '\n')
532 static int __init __xbc_open_brace(char *p)
534 /* Push the last key as open brace */
535 open_brace[brace_index++] = xbc_node_index(last_parent);
536 if (brace_index >= XBC_DEPTH_MAX)
537 return xbc_parse_error("Exceed max depth of braces", p);
542 static int __init __xbc_close_brace(char *p)
545 if (!last_parent || brace_index < 0 ||
546 (open_brace[brace_index] != xbc_node_index(last_parent)))
547 return xbc_parse_error("Unexpected closing brace", p);
549 if (brace_index == 0)
552 last_parent = &xbc_nodes[open_brace[brace_index - 1]];
558 * Return delimiter or error, no node added. As same as lib/cmdline.c,
559 * you can use " around spaces, but can't escape " for value.
561 static int __init __xbc_parse_value(char **__v, char **__n)
571 if (*v == '"' || *v == '\'') {
577 if (!isprint(c) && !isspace(c))
578 return xbc_parse_error("Non printable value", p);
584 p = skip_spaces_until_newline(p);
586 if (c && !strchr(",;\n#}", c))
587 return xbc_parse_error("No value delimiter", p);
592 if (strchr(",;\n#}", c)) {
599 return xbc_parse_error("No closing quotes", p);
602 c = '\n'; /* A comment must be treated as a newline */
610 static int __init xbc_parse_array(char **__v)
612 struct xbc_node *node;
616 if (last_parent->child)
617 last_parent = xbc_node_get_child(last_parent);
620 c = __xbc_parse_value(__v, &next);
624 node = xbc_add_child(*__v, XBC_VALUE);
635 struct xbc_node *find_match_node(struct xbc_node *node, char *k)
638 if (!strcmp(xbc_node_get_data(node), k))
640 node = xbc_node_get_next(node);
645 static int __init __xbc_add_key(char *k)
647 struct xbc_node *node, *child;
649 if (!xbc_valid_keyword(k))
650 return xbc_parse_error("Invalid keyword", k);
652 if (unlikely(xbc_node_num == 0))
655 if (!last_parent) /* the first level */
656 node = find_match_node(xbc_nodes, k);
658 child = xbc_node_get_child(last_parent);
659 /* Since the value node is the first child, skip it. */
660 if (child && xbc_node_is_value(child))
661 child = xbc_node_get_next(child);
662 node = find_match_node(child, k);
669 node = xbc_add_child(k, XBC_KEY);
676 static int __init __xbc_parse_keys(char *k)
682 while ((p = strchr(k, '.'))) {
684 ret = __xbc_add_key(k);
690 return __xbc_add_key(k);
693 static int __init xbc_parse_kv(char **k, char *v, int op)
695 struct xbc_node *prev_parent = last_parent;
696 struct xbc_node *child;
700 ret = __xbc_parse_keys(*k);
704 c = __xbc_parse_value(&v, &next);
708 child = xbc_node_get_child(last_parent);
709 if (child && xbc_node_is_value(child)) {
711 return xbc_parse_error("Value is redefined", v);
713 unsigned short nidx = child->next;
715 xbc_init_node(child, v, XBC_VALUE);
716 child->next = nidx; /* keep subkeys */
720 last_parent = xbc_last_child(child);
722 /* The value node should always be the first child */
723 if (!xbc_add_head_sibling(v, XBC_VALUE))
727 if (c == ',') { /* Array */
728 c = xbc_parse_array(&next);
733 last_parent = prev_parent;
736 ret = __xbc_close_brace(next - 1);
746 static int __init xbc_parse_key(char **k, char *n)
748 struct xbc_node *prev_parent = last_parent;
753 ret = __xbc_parse_keys(*k);
756 last_parent = prev_parent;
763 static int __init xbc_open_brace(char **k, char *n)
767 ret = __xbc_parse_keys(*k);
772 return __xbc_open_brace(n - 1);
775 static int __init xbc_close_brace(char **k, char *n)
779 ret = xbc_parse_key(k, n);
782 /* k is updated in xbc_parse_key() */
784 return __xbc_close_brace(n - 1);
787 static int __init xbc_verify_tree(void)
789 int i, depth, len, wlen;
790 struct xbc_node *n, *m;
794 n = &xbc_nodes[open_brace[brace_index]];
795 return xbc_parse_error("Brace is not closed",
796 xbc_node_get_data(n));
800 if (xbc_node_num == 0) {
801 xbc_parse_error("Empty config", xbc_data);
805 for (i = 0; i < xbc_node_num; i++) {
806 if (xbc_nodes[i].next > xbc_node_num) {
807 return xbc_parse_error("No closing brace",
808 xbc_node_get_data(xbc_nodes + i));
812 /* Key tree limitation check */
818 wlen = strlen(xbc_node_get_data(n)) + 1;
820 if (len > XBC_KEYLEN_MAX)
821 return xbc_parse_error("Too long key length",
822 xbc_node_get_data(n));
824 m = xbc_node_get_child(n);
825 if (m && xbc_node_is_key(m)) {
828 if (depth > XBC_DEPTH_MAX)
829 return xbc_parse_error("Too many key words",
830 xbc_node_get_data(n));
834 m = xbc_node_get_next(n);
836 n = xbc_node_get_parent(n);
839 len -= strlen(xbc_node_get_data(n)) + 1;
841 m = xbc_node_get_next(n);
849 /* Need to setup xbc_data and xbc_nodes before call this. */
850 static int __init xbc_parse_tree(void)
858 q = strpbrk(p, "{}=+;:\n#");
862 ret = xbc_parse_error("No delimiter", p);
872 ret = xbc_parse_error(c == '+' ?
873 "Wrong '+' operator" :
874 "Wrong ':' operator",
880 ret = xbc_parse_kv(&p, q, c);
883 ret = xbc_open_brace(&p, q);
890 ret = xbc_parse_key(&p, q);
893 ret = xbc_close_brace(&p, q);
902 * _xbc_exit() - Clean up all parsed bootconfig
903 * @early: Set true if this is called before budy system is initialized.
905 * This clears all data structures of parsed bootconfig on memory.
906 * If you need to reuse xbc_init() with new boot config, you can
909 void __init _xbc_exit(bool early)
911 xbc_free_mem(xbc_data, xbc_data_size, early);
915 xbc_free_mem(xbc_nodes, sizeof(struct xbc_node) * XBC_NODE_MAX, early);
921 * xbc_init() - Parse given XBC file and build XBC internal tree
922 * @data: The boot config text original data
923 * @size: The size of @data
924 * @emsg: A pointer of const char * to store the error message
925 * @epos: A pointer of int to store the error position
927 * This parses the boot config text in @data. @size must be smaller
929 * Return the number of stored nodes (>0) if succeeded, or -errno
930 * if there is any error.
931 * In error cases, @emsg will be updated with an error message and
932 * @epos will be updated with the error position which is the byte offset
933 * of @buf. If the error is not a parser error, @epos will be -1.
935 int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos)
944 *emsg = "Bootconfig is already initialized";
947 if (size > XBC_DATA_MAX || size == 0) {
949 *emsg = size ? "Config data is too big" :
950 "Config data is empty";
954 xbc_data = xbc_alloc_mem(size + 1);
957 *emsg = "Failed to allocate bootconfig data";
960 memcpy(xbc_data, data, size);
961 xbc_data[size] = '\0';
962 xbc_data_size = size + 1;
964 xbc_nodes = xbc_alloc_mem(sizeof(struct xbc_node) * XBC_NODE_MAX);
967 *emsg = "Failed to allocate bootconfig nodes";
971 memset(xbc_nodes, 0, sizeof(struct xbc_node) * XBC_NODE_MAX);
973 ret = xbc_parse_tree();
975 ret = xbc_verify_tree();