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
;
33 static int __init
xbc_parse_error(const char *msg
, const char *p
)
35 int pos
= p
- xbc_data
;
37 pr_err("Parse error at pos %d: %s\n", pos
, msg
);
42 * xbc_root_node() - Get the root node of extended boot config
44 * Return the address of root node of extended boot config. If the
45 * extended boot config is not initiized, return NULL.
47 struct xbc_node
* __init
xbc_root_node(void)
49 if (unlikely(!xbc_data
))
56 * xbc_node_index() - Get the index of XBC node
57 * @node: A target node of getting index.
59 * Return the index number of @node in XBC node list.
61 int __init
xbc_node_index(struct xbc_node
*node
)
63 return node
- &xbc_nodes
[0];
67 * xbc_node_get_parent() - Get the parent XBC node
70 * Return the parent node of @node. If the node is top node of the tree,
73 struct xbc_node
* __init
xbc_node_get_parent(struct xbc_node
*node
)
75 return node
->parent
== XBC_NODE_MAX
? NULL
: &xbc_nodes
[node
->parent
];
79 * xbc_node_get_child() - Get the child XBC node
82 * Return the first child node of @node. If the node has no child, return
85 struct xbc_node
* __init
xbc_node_get_child(struct xbc_node
*node
)
87 return node
->child
? &xbc_nodes
[node
->child
] : NULL
;
91 * xbc_node_get_next() - Get the next sibling XBC node
94 * Return the NEXT sibling node of @node. If the node has no next sibling,
95 * return NULL. Note that even if this returns NULL, it doesn't mean @node
96 * has no siblings. (You also has to check whether the parent's child node
99 struct xbc_node
* __init
xbc_node_get_next(struct xbc_node
*node
)
101 return node
->next
? &xbc_nodes
[node
->next
] : NULL
;
105 * xbc_node_get_data() - Get the data of XBC node
106 * @node: An XBC node.
108 * Return the data (which is always a null terminated string) of @node.
109 * If the node has invalid data, warn and return NULL.
111 const char * __init
xbc_node_get_data(struct xbc_node
*node
)
113 int offset
= node
->data
& ~XBC_VALUE
;
115 if (WARN_ON(offset
>= xbc_data_size
))
118 return xbc_data
+ offset
;
122 xbc_node_match_prefix(struct xbc_node
*node
, const char **prefix
)
124 const char *p
= xbc_node_get_data(node
);
127 if (strncmp(*prefix
, p
, len
))
141 * xbc_node_find_child() - Find a child node which matches given key
142 * @parent: An XBC node.
143 * @key: A key string.
145 * Search a node under @parent which matches @key. The @key can contain
146 * several words jointed with '.'. If @parent is NULL, this searches the
147 * node from whole tree. Return NULL if no node is matched.
149 struct xbc_node
* __init
150 xbc_node_find_child(struct xbc_node
*parent
, const char *key
)
152 struct xbc_node
*node
;
155 node
= xbc_node_get_child(parent
);
157 node
= xbc_root_node();
159 while (node
&& xbc_node_is_key(node
)) {
160 if (!xbc_node_match_prefix(node
, &key
))
161 node
= xbc_node_get_next(node
);
162 else if (*key
!= '\0')
163 node
= xbc_node_get_child(node
);
172 * xbc_node_find_value() - Find a value node which matches given key
173 * @parent: An XBC node.
174 * @key: A key string.
175 * @vnode: A container pointer of found XBC node.
177 * Search a value node under @parent whose (parent) key node matches @key,
178 * store it in *@vnode, and returns the value string.
179 * The @key can contain several words jointed with '.'. If @parent is NULL,
180 * this searches the node from whole tree. Return the value string if a
181 * matched key found, return NULL if no node is matched.
182 * Note that this returns 0-length string and stores NULL in *@vnode if the
183 * key has no value. And also it will return the value of the first entry if
184 * the value is an array.
187 xbc_node_find_value(struct xbc_node
*parent
, const char *key
,
188 struct xbc_node
**vnode
)
190 struct xbc_node
*node
= xbc_node_find_child(parent
, key
);
192 if (!node
|| !xbc_node_is_key(node
))
195 node
= xbc_node_get_child(node
);
196 if (node
&& !xbc_node_is_value(node
))
202 return node
? xbc_node_get_data(node
) : "";
206 * xbc_node_compose_key_after() - Compose partial key string of the XBC node
207 * @root: Root XBC node
208 * @node: Target XBC node.
209 * @buf: A buffer to store the key.
210 * @size: The size of the @buf.
212 * Compose the partial key of the @node into @buf, which is starting right
213 * after @root (@root is not included.) If @root is NULL, this returns full
214 * key words of @node.
215 * Returns the total length of the key stored in @buf. Returns -EINVAL
216 * if @node is NULL or @root is not the ancestor of @node or @root is @node,
217 * or returns -ERANGE if the key depth is deeper than max depth.
218 * This is expected to be used with xbc_find_node() to list up all (child)
219 * keys under given key.
221 int __init
xbc_node_compose_key_after(struct xbc_node
*root
,
222 struct xbc_node
*node
,
223 char *buf
, size_t size
)
225 u16 keys
[XBC_DEPTH_MAX
];
226 int depth
= 0, ret
= 0, total
= 0;
228 if (!node
|| node
== root
)
231 if (xbc_node_is_value(node
))
232 node
= xbc_node_get_parent(node
);
234 while (node
&& node
!= root
) {
235 keys
[depth
++] = xbc_node_index(node
);
236 if (depth
== XBC_DEPTH_MAX
)
238 node
= xbc_node_get_parent(node
);
243 while (--depth
>= 0) {
244 node
= xbc_nodes
+ keys
[depth
];
245 ret
= snprintf(buf
, size
, "%s%s", xbc_node_get_data(node
),
262 * xbc_node_find_next_leaf() - Find the next leaf node under given node
263 * @root: An XBC root node
264 * @node: An XBC node which starts from.
266 * Search the next leaf node (which means the terminal key node) of @node
267 * under @root node (including @root node itself).
268 * Return the next node or NULL if next leaf node is not found.
270 struct xbc_node
* __init
xbc_node_find_next_leaf(struct xbc_node
*root
,
271 struct xbc_node
*node
)
273 if (unlikely(!xbc_data
))
276 if (!node
) { /* First try */
281 if (node
== root
) /* @root was a leaf, no child node. */
284 while (!node
->next
) {
285 node
= xbc_node_get_parent(node
);
288 /* User passed a node which is not uder parent */
292 node
= xbc_node_get_next(node
);
295 while (node
&& !xbc_node_is_leaf(node
))
296 node
= xbc_node_get_child(node
);
302 * xbc_node_find_next_key_value() - Find the next key-value pair nodes
303 * @root: An XBC root node
304 * @leaf: A container pointer of XBC node which starts from.
306 * Search the next leaf node (which means the terminal key node) of *@leaf
307 * under @root node. Returns the value and update *@leaf if next leaf node
308 * is found, or NULL if no next leaf node is found.
309 * Note that this returns 0-length string if the key has no value, or
310 * the value of the first entry if the value is an array.
312 const char * __init
xbc_node_find_next_key_value(struct xbc_node
*root
,
313 struct xbc_node
**leaf
)
315 /* tip must be passed */
319 *leaf
= xbc_node_find_next_leaf(root
, *leaf
);
323 return xbc_node_get_data(xbc_node_get_child(*leaf
));
325 return ""; /* No value key */
328 /* XBC parse and tree build */
330 static struct xbc_node
* __init
xbc_add_node(char *data
, u32 flag
)
332 struct xbc_node
*node
;
333 unsigned long offset
;
335 if (xbc_node_num
== XBC_NODE_MAX
)
338 node
= &xbc_nodes
[xbc_node_num
++];
339 offset
= data
- xbc_data
;
340 node
->data
= (u16
)offset
;
341 if (WARN_ON(offset
>= XBC_DATA_MAX
))
350 static inline __init
struct xbc_node
*xbc_last_sibling(struct xbc_node
*node
)
353 node
= xbc_node_get_next(node
);
358 static struct xbc_node
* __init
xbc_add_sibling(char *data
, u32 flag
)
360 struct xbc_node
*sib
, *node
= xbc_add_node(data
, flag
);
364 node
->parent
= XBC_NODE_MAX
;
365 sib
= xbc_last_sibling(xbc_nodes
);
366 sib
->next
= xbc_node_index(node
);
368 node
->parent
= xbc_node_index(last_parent
);
369 if (!last_parent
->child
) {
370 last_parent
->child
= xbc_node_index(node
);
372 sib
= xbc_node_get_child(last_parent
);
373 sib
= xbc_last_sibling(sib
);
374 sib
->next
= xbc_node_index(node
);
378 xbc_parse_error("Too many nodes", data
);
383 static inline __init
struct xbc_node
*xbc_add_child(char *data
, u32 flag
)
385 struct xbc_node
*node
= xbc_add_sibling(data
, flag
);
393 static inline __init
bool xbc_valid_keyword(char *key
)
398 while (isalnum(*key
) || *key
== '-' || *key
== '_')
404 static char *skip_comment(char *p
)
408 ret
= strchr(p
, '\n');
417 static char *skip_spaces_until_newline(char *p
)
419 while (isspace(*p
) && *p
!= '\n')
424 static int __init
__xbc_open_brace(void)
426 /* Mark the last key as open brace */
427 last_parent
->next
= XBC_NODE_MAX
;
432 static int __init
__xbc_close_brace(char *p
)
434 struct xbc_node
*node
;
436 if (!last_parent
|| last_parent
->next
!= XBC_NODE_MAX
)
437 return xbc_parse_error("Unexpected closing brace", p
);
442 node
= xbc_node_get_parent(node
);
443 } while (node
&& node
->next
!= XBC_NODE_MAX
);
450 * Return delimiter or error, no node added. As same as lib/cmdline.c,
451 * you can use " around spaces, but can't escape " for value.
453 static int __init
__xbc_parse_value(char **__v
, char **__n
)
463 if (*v
== '"' || *v
== '\'') {
469 if (!isprint(c
) && !isspace(c
))
470 return xbc_parse_error("Non printable value", p
);
476 p
= skip_spaces_until_newline(p
);
478 if (c
&& !strchr(",;\n#}", c
))
479 return xbc_parse_error("No value delimiter", p
);
484 if (strchr(",;\n#}", c
)) {
491 return xbc_parse_error("No closing quotes", p
);
494 c
= '\n'; /* A comment must be treated as a newline */
502 static int __init
xbc_parse_array(char **__v
)
504 struct xbc_node
*node
;
509 c
= __xbc_parse_value(__v
, &next
);
513 node
= xbc_add_sibling(*__v
, XBC_VALUE
);
524 struct xbc_node
*find_match_node(struct xbc_node
*node
, char *k
)
527 if (!strcmp(xbc_node_get_data(node
), k
))
529 node
= xbc_node_get_next(node
);
534 static int __init
__xbc_add_key(char *k
)
536 struct xbc_node
*node
, *child
;
538 if (!xbc_valid_keyword(k
))
539 return xbc_parse_error("Invalid keyword", k
);
541 if (unlikely(xbc_node_num
== 0))
544 if (!last_parent
) /* the first level */
545 node
= find_match_node(xbc_nodes
, k
);
547 child
= xbc_node_get_child(last_parent
);
548 if (child
&& xbc_node_is_value(child
))
549 return xbc_parse_error("Subkey is mixed with value", k
);
550 node
= find_match_node(child
, k
);
557 node
= xbc_add_child(k
, XBC_KEY
);
564 static int __init
__xbc_parse_keys(char *k
)
570 while ((p
= strchr(k
, '.'))) {
572 ret
= __xbc_add_key(k
);
578 return __xbc_add_key(k
);
581 static int __init
xbc_parse_kv(char **k
, char *v
, int op
)
583 struct xbc_node
*prev_parent
= last_parent
;
584 struct xbc_node
*child
;
588 ret
= __xbc_parse_keys(*k
);
592 child
= xbc_node_get_child(last_parent
);
594 if (xbc_node_is_key(child
))
595 return xbc_parse_error("Value is mixed with subkey", v
);
597 return xbc_parse_error("Value is redefined", v
);
600 c
= __xbc_parse_value(&v
, &next
);
604 if (!xbc_add_sibling(v
, XBC_VALUE
))
607 if (c
== ',') { /* Array */
608 c
= xbc_parse_array(&next
);
613 last_parent
= prev_parent
;
616 ret
= __xbc_close_brace(next
- 1);
626 static int __init
xbc_parse_key(char **k
, char *n
)
628 struct xbc_node
*prev_parent
= last_parent
;
633 ret
= __xbc_parse_keys(*k
);
636 last_parent
= prev_parent
;
643 static int __init
xbc_open_brace(char **k
, char *n
)
647 ret
= __xbc_parse_keys(*k
);
652 return __xbc_open_brace();
655 static int __init
xbc_close_brace(char **k
, char *n
)
659 ret
= xbc_parse_key(k
, n
);
662 /* k is updated in xbc_parse_key() */
664 return __xbc_close_brace(n
- 1);
667 static int __init
xbc_verify_tree(void)
669 int i
, depth
, len
, wlen
;
670 struct xbc_node
*n
, *m
;
673 if (xbc_node_num
== 0) {
674 xbc_parse_error("Empty config", xbc_data
);
678 for (i
= 0; i
< xbc_node_num
; i
++) {
679 if (xbc_nodes
[i
].next
> xbc_node_num
) {
680 return xbc_parse_error("No closing brace",
681 xbc_node_get_data(xbc_nodes
+ i
));
685 /* Key tree limitation check */
691 wlen
= strlen(xbc_node_get_data(n
)) + 1;
693 if (len
> XBC_KEYLEN_MAX
)
694 return xbc_parse_error("Too long key length",
695 xbc_node_get_data(n
));
697 m
= xbc_node_get_child(n
);
698 if (m
&& xbc_node_is_key(m
)) {
701 if (depth
> XBC_DEPTH_MAX
)
702 return xbc_parse_error("Too many key words",
703 xbc_node_get_data(n
));
707 m
= xbc_node_get_next(n
);
709 n
= xbc_node_get_parent(n
);
712 len
-= strlen(xbc_node_get_data(n
)) + 1;
714 m
= xbc_node_get_next(n
);
723 * xbc_destroy_all() - Clean up all parsed bootconfig
725 * This clears all data structures of parsed bootconfig on memory.
726 * If you need to reuse xbc_init() with new boot config, you can
729 void __init
xbc_destroy_all(void)
734 memblock_free(__pa(xbc_nodes
), sizeof(struct xbc_node
) * XBC_NODE_MAX
);
739 * xbc_init() - Parse given XBC file and build XBC internal tree
740 * @buf: boot config text
742 * This parses the boot config text in @buf. @buf must be a
743 * null terminated string and smaller than XBC_DATA_MAX.
744 * Return the number of stored nodes (>0) if succeeded, or -errno
745 * if there is any error.
747 int __init
xbc_init(char *buf
)
753 pr_err("Error: bootconfig is already initialized.\n");
758 if (ret
> XBC_DATA_MAX
- 1 || ret
== 0) {
759 pr_err("Error: Config data is %s.\n",
760 ret
? "too big" : "empty");
764 xbc_nodes
= memblock_alloc(sizeof(struct xbc_node
) * XBC_NODE_MAX
,
767 pr_err("Failed to allocate memory for bootconfig nodes.\n");
770 memset(xbc_nodes
, 0, sizeof(struct xbc_node
) * XBC_NODE_MAX
);
772 xbc_data_size
= ret
+ 1;
777 q
= strpbrk(p
, "{}=+;\n#");
781 ret
= xbc_parse_error("No delimiter", p
);
790 ret
= xbc_parse_error("Wrong '+' operator",
796 ret
= xbc_parse_kv(&p
, q
, c
);
799 ret
= xbc_open_brace(&p
, q
);
806 ret
= xbc_parse_key(&p
, q
);
809 ret
= xbc_close_brace(&p
, q
);
815 ret
= xbc_verify_tree();
826 * xbc_debug_dump() - Dump current XBC node list
828 * Dump the current XBC node list on printk buffer for debug.
830 void __init
xbc_debug_dump(void)
834 for (i
= 0; i
< xbc_node_num
; i
++) {
835 pr_debug("[%d] %s (%s) .next=%d, .child=%d .parent=%d\n", i
,
836 xbc_node_get_data(xbc_nodes
+ i
),
837 xbc_node_is_value(xbc_nodes
+ i
) ? "value" : "key",
838 xbc_nodes
[i
].next
, xbc_nodes
[i
].child
,
839 xbc_nodes
[i
].parent
);