1 // SPDX-License-Identifier: GPL-2.0
4 * Masami Hiramatsu <mhiramat@kernel.org>
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>
15 #else /* !__KERNEL__ */
17 * NOTE: This is only for tools/bootconfig, because tools/bootconfig will
18 * run the parser sanity test.
19 * This does NOT mean lib/bootconfig.c is available in the user space.
20 * However, if you change this file, please make sure the tools/bootconfig
21 * has no issue on building and running.
23 #include <linux/bootconfig.h>
27 * Extra Boot Config (XBC) is given as tree-structured ascii text of
28 * key-value pairs on memory.
29 * xbc_parse() parses the text to build a simple tree. Each tree node is
30 * simply a key word or a value. A key node may have a next key node or/and
31 * a child node (both key and value). A value node may have a next value
35 static struct xbc_node *xbc_nodes __initdata;
36 static int xbc_node_num __initdata;
37 static char *xbc_data __initdata;
38 static size_t xbc_data_size __initdata;
39 static struct xbc_node *last_parent __initdata;
40 static const char *xbc_err_msg __initdata;
41 static int xbc_err_pos __initdata;
42 static int open_brace[XBC_DEPTH_MAX] __initdata;
43 static int brace_index __initdata;
46 static inline void * __init xbc_alloc_mem(size_t size)
48 return memblock_alloc(size, SMP_CACHE_BYTES);
51 static inline void __init xbc_free_mem(void *addr, size_t size)
53 memblock_free(addr, size);
56 #else /* !__KERNEL__ */
58 static inline void *xbc_alloc_mem(size_t size)
63 static inline void xbc_free_mem(void *addr, size_t size)
69 * xbc_get_info() - Get the information of loaded boot config
70 * @node_size: A pointer to store the number of nodes.
71 * @data_size: A pointer to store the size of bootconfig data.
73 * Get the number of used nodes in @node_size if it is not NULL,
74 * and the size of bootconfig data in @data_size if it is not NULL.
75 * Return 0 if the boot config is initialized, or return -ENODEV.
77 int __init xbc_get_info(int *node_size, size_t *data_size)
83 *node_size = xbc_node_num;
85 *data_size = xbc_data_size;
89 static int __init xbc_parse_error(const char *msg, const char *p)
92 xbc_err_pos = (int)(p - xbc_data);
98 * xbc_root_node() - Get the root node of extended boot config
100 * Return the address of root node of extended boot config. If the
101 * extended boot config is not initiized, return NULL.
103 struct xbc_node * __init xbc_root_node(void)
105 if (unlikely(!xbc_data))
112 * xbc_node_index() - Get the index of XBC node
113 * @node: A target node of getting index.
115 * Return the index number of @node in XBC node list.
117 int __init xbc_node_index(struct xbc_node *node)
119 return node - &xbc_nodes[0];
123 * xbc_node_get_parent() - Get the parent XBC node
124 * @node: An XBC node.
126 * Return the parent node of @node. If the node is top node of the tree,
129 struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node)
131 return node->parent == XBC_NODE_MAX ? NULL : &xbc_nodes[node->parent];
135 * xbc_node_get_child() - Get the child XBC node
136 * @node: An XBC node.
138 * Return the first child node of @node. If the node has no child, return
141 struct xbc_node * __init xbc_node_get_child(struct xbc_node *node)
143 return node->child ? &xbc_nodes[node->child] : NULL;
147 * xbc_node_get_next() - Get the next sibling XBC node
148 * @node: An XBC node.
150 * Return the NEXT sibling node of @node. If the node has no next sibling,
151 * return NULL. Note that even if this returns NULL, it doesn't mean @node
152 * has no siblings. (You also has to check whether the parent's child node
155 struct xbc_node * __init xbc_node_get_next(struct xbc_node *node)
157 return node->next ? &xbc_nodes[node->next] : NULL;
161 * xbc_node_get_data() - Get the data of XBC node
162 * @node: An XBC node.
164 * Return the data (which is always a null terminated string) of @node.
165 * If the node has invalid data, warn and return NULL.
167 const char * __init xbc_node_get_data(struct xbc_node *node)
169 int offset = node->data & ~XBC_VALUE;
171 if (WARN_ON(offset >= xbc_data_size))
174 return xbc_data + offset;
178 xbc_node_match_prefix(struct xbc_node *node, const char **prefix)
180 const char *p = xbc_node_get_data(node);
183 if (strncmp(*prefix, p, len))
197 * xbc_node_find_subkey() - Find a subkey node which matches given key
198 * @parent: An XBC node.
199 * @key: A key string.
201 * Search a key node under @parent which matches @key. The @key can contain
202 * several words jointed with '.'. If @parent is NULL, this searches the
203 * node from whole tree. Return NULL if no node is matched.
205 struct xbc_node * __init
206 xbc_node_find_subkey(struct xbc_node *parent, const char *key)
208 struct xbc_node *node;
211 node = xbc_node_get_subkey(parent);
213 node = xbc_root_node();
215 while (node && xbc_node_is_key(node)) {
216 if (!xbc_node_match_prefix(node, &key))
217 node = xbc_node_get_next(node);
218 else if (*key != '\0')
219 node = xbc_node_get_subkey(node);
228 * xbc_node_find_value() - Find a value node which matches given key
229 * @parent: An XBC node.
230 * @key: A key string.
231 * @vnode: A container pointer of found XBC node.
233 * Search a value node under @parent whose (parent) key node matches @key,
234 * store it in *@vnode, and returns the value string.
235 * The @key can contain several words jointed with '.'. If @parent is NULL,
236 * this searches the node from whole tree. Return the value string if a
237 * matched key found, return NULL if no node is matched.
238 * Note that this returns 0-length string and stores NULL in *@vnode if the
239 * key has no value. And also it will return the value of the first entry if
240 * the value is an array.
243 xbc_node_find_value(struct xbc_node *parent, const char *key,
244 struct xbc_node **vnode)
246 struct xbc_node *node = xbc_node_find_subkey(parent, key);
248 if (!node || !xbc_node_is_key(node))
251 node = xbc_node_get_child(node);
252 if (node && !xbc_node_is_value(node))
258 return node ? xbc_node_get_data(node) : "";
262 * xbc_node_compose_key_after() - Compose partial key string of the XBC node
263 * @root: Root XBC node
264 * @node: Target XBC node.
265 * @buf: A buffer to store the key.
266 * @size: The size of the @buf.
268 * Compose the partial key of the @node into @buf, which is starting right
269 * after @root (@root is not included.) If @root is NULL, this returns full
270 * key words of @node.
271 * Returns the total length of the key stored in @buf. Returns -EINVAL
272 * if @node is NULL or @root is not the ancestor of @node or @root is @node,
273 * or returns -ERANGE if the key depth is deeper than max depth.
274 * This is expected to be used with xbc_find_node() to list up all (child)
275 * keys under given key.
277 int __init xbc_node_compose_key_after(struct xbc_node *root,
278 struct xbc_node *node,
279 char *buf, size_t size)
281 uint16_t keys[XBC_DEPTH_MAX];
282 int depth = 0, ret = 0, total = 0;
284 if (!node || node == root)
287 if (xbc_node_is_value(node))
288 node = xbc_node_get_parent(node);
290 while (node && node != root) {
291 keys[depth++] = xbc_node_index(node);
292 if (depth == XBC_DEPTH_MAX)
294 node = xbc_node_get_parent(node);
299 while (--depth >= 0) {
300 node = xbc_nodes + keys[depth];
301 ret = snprintf(buf, size, "%s%s", xbc_node_get_data(node),
318 * xbc_node_find_next_leaf() - Find the next leaf node under given node
319 * @root: An XBC root node
320 * @node: An XBC node which starts from.
322 * Search the next leaf node (which means the terminal key node) of @node
323 * under @root node (including @root node itself).
324 * Return the next node or NULL if next leaf node is not found.
326 struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root,
327 struct xbc_node *node)
329 struct xbc_node *next;
331 if (unlikely(!xbc_data))
334 if (!node) { /* First try */
339 /* Leaf node may have a subkey */
340 next = xbc_node_get_subkey(node);
346 if (node == root) /* @root was a leaf, no child node. */
349 while (!node->next) {
350 node = xbc_node_get_parent(node);
353 /* User passed a node which is not uder parent */
357 node = xbc_node_get_next(node);
361 while (node && !xbc_node_is_leaf(node))
362 node = xbc_node_get_child(node);
368 * xbc_node_find_next_key_value() - Find the next key-value pair nodes
369 * @root: An XBC root node
370 * @leaf: A container pointer of XBC node which starts from.
372 * Search the next leaf node (which means the terminal key node) of *@leaf
373 * under @root node. Returns the value and update *@leaf if next leaf node
374 * is found, or NULL if no next leaf node is found.
375 * Note that this returns 0-length string if the key has no value, or
376 * the value of the first entry if the value is an array.
378 const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
379 struct xbc_node **leaf)
381 /* tip must be passed */
385 *leaf = xbc_node_find_next_leaf(root, *leaf);
389 return xbc_node_get_data(xbc_node_get_child(*leaf));
391 return ""; /* No value key */
394 /* XBC parse and tree build */
396 static int __init xbc_init_node(struct xbc_node *node, char *data, uint32_t flag)
398 unsigned long offset = data - xbc_data;
400 if (WARN_ON(offset >= XBC_DATA_MAX))
403 node->data = (uint16_t)offset | flag;
410 static struct xbc_node * __init xbc_add_node(char *data, uint32_t flag)
412 struct xbc_node *node;
414 if (xbc_node_num == XBC_NODE_MAX)
417 node = &xbc_nodes[xbc_node_num++];
418 if (xbc_init_node(node, data, flag) < 0)
424 static inline __init struct xbc_node *xbc_last_sibling(struct xbc_node *node)
427 node = xbc_node_get_next(node);
432 static inline __init struct xbc_node *xbc_last_child(struct xbc_node *node)
435 node = xbc_node_get_child(node);
440 static struct xbc_node * __init __xbc_add_sibling(char *data, uint32_t flag, bool head)
442 struct xbc_node *sib, *node = xbc_add_node(data, flag);
446 /* Ignore @head in this case */
447 node->parent = XBC_NODE_MAX;
448 sib = xbc_last_sibling(xbc_nodes);
449 sib->next = xbc_node_index(node);
451 node->parent = xbc_node_index(last_parent);
452 if (!last_parent->child || head) {
453 node->next = last_parent->child;
454 last_parent->child = xbc_node_index(node);
456 sib = xbc_node_get_child(last_parent);
457 sib = xbc_last_sibling(sib);
458 sib->next = xbc_node_index(node);
462 xbc_parse_error("Too many nodes", data);
467 static inline struct xbc_node * __init xbc_add_sibling(char *data, uint32_t flag)
469 return __xbc_add_sibling(data, flag, false);
472 static inline struct xbc_node * __init xbc_add_head_sibling(char *data, uint32_t flag)
474 return __xbc_add_sibling(data, flag, true);
477 static inline __init struct xbc_node *xbc_add_child(char *data, uint32_t flag)
479 struct xbc_node *node = xbc_add_sibling(data, flag);
487 static inline __init bool xbc_valid_keyword(char *key)
492 while (isalnum(*key) || *key == '-' || *key == '_')
498 static char *skip_comment(char *p)
502 ret = strchr(p, '\n');
511 static char *skip_spaces_until_newline(char *p)
513 while (isspace(*p) && *p != '\n')
518 static int __init __xbc_open_brace(char *p)
520 /* Push the last key as open brace */
521 open_brace[brace_index++] = xbc_node_index(last_parent);
522 if (brace_index >= XBC_DEPTH_MAX)
523 return xbc_parse_error("Exceed max depth of braces", p);
528 static int __init __xbc_close_brace(char *p)
531 if (!last_parent || brace_index < 0 ||
532 (open_brace[brace_index] != xbc_node_index(last_parent)))
533 return xbc_parse_error("Unexpected closing brace", p);
535 if (brace_index == 0)
538 last_parent = &xbc_nodes[open_brace[brace_index - 1]];
544 * Return delimiter or error, no node added. As same as lib/cmdline.c,
545 * you can use " around spaces, but can't escape " for value.
547 static int __init __xbc_parse_value(char **__v, char **__n)
557 if (*v == '"' || *v == '\'') {
563 if (!isprint(c) && !isspace(c))
564 return xbc_parse_error("Non printable value", p);
570 p = skip_spaces_until_newline(p);
572 if (c && !strchr(",;\n#}", c))
573 return xbc_parse_error("No value delimiter", p);
578 if (strchr(",;\n#}", c)) {
585 return xbc_parse_error("No closing quotes", p);
588 c = '\n'; /* A comment must be treated as a newline */
596 static int __init xbc_parse_array(char **__v)
598 struct xbc_node *node;
602 if (last_parent->child)
603 last_parent = xbc_node_get_child(last_parent);
606 c = __xbc_parse_value(__v, &next);
610 node = xbc_add_child(*__v, XBC_VALUE);
621 struct xbc_node *find_match_node(struct xbc_node *node, char *k)
624 if (!strcmp(xbc_node_get_data(node), k))
626 node = xbc_node_get_next(node);
631 static int __init __xbc_add_key(char *k)
633 struct xbc_node *node, *child;
635 if (!xbc_valid_keyword(k))
636 return xbc_parse_error("Invalid keyword", k);
638 if (unlikely(xbc_node_num == 0))
641 if (!last_parent) /* the first level */
642 node = find_match_node(xbc_nodes, k);
644 child = xbc_node_get_child(last_parent);
645 /* Since the value node is the first child, skip it. */
646 if (child && xbc_node_is_value(child))
647 child = xbc_node_get_next(child);
648 node = find_match_node(child, k);
655 node = xbc_add_child(k, XBC_KEY);
662 static int __init __xbc_parse_keys(char *k)
668 while ((p = strchr(k, '.'))) {
670 ret = __xbc_add_key(k);
676 return __xbc_add_key(k);
679 static int __init xbc_parse_kv(char **k, char *v, int op)
681 struct xbc_node *prev_parent = last_parent;
682 struct xbc_node *child;
686 ret = __xbc_parse_keys(*k);
690 c = __xbc_parse_value(&v, &next);
694 child = xbc_node_get_child(last_parent);
695 if (child && xbc_node_is_value(child)) {
697 return xbc_parse_error("Value is redefined", v);
699 unsigned short nidx = child->next;
701 xbc_init_node(child, v, XBC_VALUE);
702 child->next = nidx; /* keep subkeys */
706 last_parent = xbc_last_child(child);
708 /* The value node should always be the first child */
709 if (!xbc_add_head_sibling(v, XBC_VALUE))
713 if (c == ',') { /* Array */
714 c = xbc_parse_array(&next);
719 last_parent = prev_parent;
722 ret = __xbc_close_brace(next - 1);
732 static int __init xbc_parse_key(char **k, char *n)
734 struct xbc_node *prev_parent = last_parent;
739 ret = __xbc_parse_keys(*k);
742 last_parent = prev_parent;
749 static int __init xbc_open_brace(char **k, char *n)
753 ret = __xbc_parse_keys(*k);
758 return __xbc_open_brace(n - 1);
761 static int __init xbc_close_brace(char **k, char *n)
765 ret = xbc_parse_key(k, n);
768 /* k is updated in xbc_parse_key() */
770 return __xbc_close_brace(n - 1);
773 static int __init xbc_verify_tree(void)
775 int i, depth, len, wlen;
776 struct xbc_node *n, *m;
780 n = &xbc_nodes[open_brace[brace_index]];
781 return xbc_parse_error("Brace is not closed",
782 xbc_node_get_data(n));
786 if (xbc_node_num == 0) {
787 xbc_parse_error("Empty config", xbc_data);
791 for (i = 0; i < xbc_node_num; i++) {
792 if (xbc_nodes[i].next > xbc_node_num) {
793 return xbc_parse_error("No closing brace",
794 xbc_node_get_data(xbc_nodes + i));
798 /* Key tree limitation check */
804 wlen = strlen(xbc_node_get_data(n)) + 1;
806 if (len > XBC_KEYLEN_MAX)
807 return xbc_parse_error("Too long key length",
808 xbc_node_get_data(n));
810 m = xbc_node_get_child(n);
811 if (m && xbc_node_is_key(m)) {
814 if (depth > XBC_DEPTH_MAX)
815 return xbc_parse_error("Too many key words",
816 xbc_node_get_data(n));
820 m = xbc_node_get_next(n);
822 n = xbc_node_get_parent(n);
825 len -= strlen(xbc_node_get_data(n)) + 1;
827 m = xbc_node_get_next(n);
835 /* Need to setup xbc_data and xbc_nodes before call this. */
836 static int __init xbc_parse_tree(void)
844 q = strpbrk(p, "{}=+;:\n#");
848 ret = xbc_parse_error("No delimiter", p);
858 ret = xbc_parse_error(c == '+' ?
859 "Wrong '+' operator" :
860 "Wrong ':' operator",
866 ret = xbc_parse_kv(&p, q, c);
869 ret = xbc_open_brace(&p, q);
876 ret = xbc_parse_key(&p, q);
879 ret = xbc_close_brace(&p, q);
888 * xbc_exit() - Clean up all parsed bootconfig
890 * This clears all data structures of parsed bootconfig on memory.
891 * If you need to reuse xbc_init() with new boot config, you can
894 void __init xbc_exit(void)
896 xbc_free_mem(xbc_data, xbc_data_size);
900 xbc_free_mem(xbc_nodes, sizeof(struct xbc_node) * XBC_NODE_MAX);
906 * xbc_init() - Parse given XBC file and build XBC internal tree
907 * @data: The boot config text original data
908 * @size: The size of @data
909 * @emsg: A pointer of const char * to store the error message
910 * @epos: A pointer of int to store the error position
912 * This parses the boot config text in @data. @size must be smaller
914 * Return the number of stored nodes (>0) if succeeded, or -errno
915 * if there is any error.
916 * In error cases, @emsg will be updated with an error message and
917 * @epos will be updated with the error position which is the byte offset
918 * of @buf. If the error is not a parser error, @epos will be -1.
920 int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos)
929 *emsg = "Bootconfig is already initialized";
932 if (size > XBC_DATA_MAX || size == 0) {
934 *emsg = size ? "Config data is too big" :
935 "Config data is empty";
939 xbc_data = xbc_alloc_mem(size + 1);
942 *emsg = "Failed to allocate bootconfig data";
945 memcpy(xbc_data, data, size);
946 xbc_data[size] = '\0';
947 xbc_data_size = size + 1;
949 xbc_nodes = xbc_alloc_mem(sizeof(struct xbc_node) * XBC_NODE_MAX);
952 *emsg = "Failed to allocate bootconfig nodes";
956 memset(xbc_nodes, 0, sizeof(struct xbc_node) * XBC_NODE_MAX);
958 ret = xbc_parse_tree();
960 ret = xbc_verify_tree();