1 // SPDX-License-Identifier: GPL-2.0
4 * Masami Hiramatsu <mhiramat@kernel.org>
7 #define pr_fmt(fmt) "bootconfig: " fmt
10 #include <linux/ctype.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/printk.h>
14 #include <linux/bootconfig.h>
15 #include <linux/string.h>
18 * Extra Boot Config (XBC) is given as tree-structured ascii text of
19 * key-value pairs on memory.
20 * xbc_parse() parses the text to build a simple tree. Each tree node is
21 * simply a key word or a value. A key node may have a next key node or/and
22 * a child node (both key and value). A value node may have a next value
26 static struct xbc_node xbc_nodes
[XBC_NODE_MAX
] __initdata
;
27 static int xbc_node_num __initdata
;
28 static char *xbc_data __initdata
;
29 static size_t xbc_data_size __initdata
;
30 static struct xbc_node
*last_parent __initdata
;
32 static int __init
xbc_parse_error(const char *msg
, const char *p
)
34 int pos
= p
- xbc_data
;
36 pr_err("Parse error at pos %d: %s\n", pos
, msg
);
41 * xbc_root_node() - Get the root node of extended boot config
43 * Return the address of root node of extended boot config. If the
44 * extended boot config is not initiized, return NULL.
46 struct xbc_node
* __init
xbc_root_node(void)
48 if (unlikely(!xbc_data
))
55 * xbc_node_index() - Get the index of XBC node
56 * @node: A target node of getting index.
58 * Return the index number of @node in XBC node list.
60 int __init
xbc_node_index(struct xbc_node
*node
)
62 return node
- &xbc_nodes
[0];
66 * xbc_node_get_parent() - Get the parent XBC node
69 * Return the parent node of @node. If the node is top node of the tree,
72 struct xbc_node
* __init
xbc_node_get_parent(struct xbc_node
*node
)
74 return node
->parent
== XBC_NODE_MAX
? NULL
: &xbc_nodes
[node
->parent
];
78 * xbc_node_get_child() - Get the child XBC node
81 * Return the first child node of @node. If the node has no child, return
84 struct xbc_node
* __init
xbc_node_get_child(struct xbc_node
*node
)
86 return node
->child
? &xbc_nodes
[node
->child
] : NULL
;
90 * xbc_node_get_next() - Get the next sibling XBC node
93 * Return the NEXT sibling node of @node. If the node has no next sibling,
94 * return NULL. Note that even if this returns NULL, it doesn't mean @node
95 * has no siblings. (You also has to check whether the parent's child node
98 struct xbc_node
* __init
xbc_node_get_next(struct xbc_node
*node
)
100 return node
->next
? &xbc_nodes
[node
->next
] : NULL
;
104 * xbc_node_get_data() - Get the data of XBC node
105 * @node: An XBC node.
107 * Return the data (which is always a null terminated string) of @node.
108 * If the node has invalid data, warn and return NULL.
110 const char * __init
xbc_node_get_data(struct xbc_node
*node
)
112 int offset
= node
->data
& ~XBC_VALUE
;
114 if (WARN_ON(offset
>= xbc_data_size
))
117 return xbc_data
+ offset
;
121 xbc_node_match_prefix(struct xbc_node
*node
, const char **prefix
)
123 const char *p
= xbc_node_get_data(node
);
126 if (strncmp(*prefix
, p
, len
))
140 * xbc_node_find_child() - Find a child node which matches given key
141 * @parent: An XBC node.
142 * @key: A key string.
144 * Search a node under @parent which matches @key. The @key can contain
145 * several words jointed with '.'. If @parent is NULL, this searches the
146 * node from whole tree. Return NULL if no node is matched.
148 struct xbc_node
* __init
149 xbc_node_find_child(struct xbc_node
*parent
, const char *key
)
151 struct xbc_node
*node
;
154 node
= xbc_node_get_child(parent
);
156 node
= xbc_root_node();
158 while (node
&& xbc_node_is_key(node
)) {
159 if (!xbc_node_match_prefix(node
, &key
))
160 node
= xbc_node_get_next(node
);
161 else if (*key
!= '\0')
162 node
= xbc_node_get_child(node
);
171 * xbc_node_find_value() - Find a value node which matches given key
172 * @parent: An XBC node.
173 * @key: A key string.
174 * @vnode: A container pointer of found XBC node.
176 * Search a value node under @parent whose (parent) key node matches @key,
177 * store it in *@vnode, and returns the value string.
178 * The @key can contain several words jointed with '.'. If @parent is NULL,
179 * this searches the node from whole tree. Return the value string if a
180 * matched key found, return NULL if no node is matched.
181 * Note that this returns 0-length string and stores NULL in *@vnode if the
182 * key has no value. And also it will return the value of the first entry if
183 * the value is an array.
186 xbc_node_find_value(struct xbc_node
*parent
, const char *key
,
187 struct xbc_node
**vnode
)
189 struct xbc_node
*node
= xbc_node_find_child(parent
, key
);
191 if (!node
|| !xbc_node_is_key(node
))
194 node
= xbc_node_get_child(node
);
195 if (node
&& !xbc_node_is_value(node
))
201 return node
? xbc_node_get_data(node
) : "";
205 * xbc_node_compose_key_after() - Compose partial key string of the XBC node
206 * @root: Root XBC node
207 * @node: Target XBC node.
208 * @buf: A buffer to store the key.
209 * @size: The size of the @buf.
211 * Compose the partial key of the @node into @buf, which is starting right
212 * after @root (@root is not included.) If @root is NULL, this returns full
213 * key words of @node.
214 * Returns the total length of the key stored in @buf. Returns -EINVAL
215 * if @node is NULL or @root is not the ancestor of @node or @root is @node,
216 * or returns -ERANGE if the key depth is deeper than max depth.
217 * This is expected to be used with xbc_find_node() to list up all (child)
218 * keys under given key.
220 int __init
xbc_node_compose_key_after(struct xbc_node
*root
,
221 struct xbc_node
*node
,
222 char *buf
, size_t size
)
224 u16 keys
[XBC_DEPTH_MAX
];
225 int depth
= 0, ret
= 0, total
= 0;
227 if (!node
|| node
== root
)
230 if (xbc_node_is_value(node
))
231 node
= xbc_node_get_parent(node
);
233 while (node
&& node
!= root
) {
234 keys
[depth
++] = xbc_node_index(node
);
235 if (depth
== XBC_DEPTH_MAX
)
237 node
= xbc_node_get_parent(node
);
242 while (--depth
>= 0) {
243 node
= xbc_nodes
+ keys
[depth
];
244 ret
= snprintf(buf
, size
, "%s%s", xbc_node_get_data(node
),
261 * xbc_node_find_next_leaf() - Find the next leaf node under given node
262 * @root: An XBC root node
263 * @node: An XBC node which starts from.
265 * Search the next leaf node (which means the terminal key node) of @node
266 * under @root node (including @root node itself).
267 * Return the next node or NULL if next leaf node is not found.
269 struct xbc_node
* __init
xbc_node_find_next_leaf(struct xbc_node
*root
,
270 struct xbc_node
*node
)
272 if (unlikely(!xbc_data
))
275 if (!node
) { /* First try */
280 if (node
== root
) /* @root was a leaf, no child node. */
283 while (!node
->next
) {
284 node
= xbc_node_get_parent(node
);
287 /* User passed a node which is not uder parent */
291 node
= xbc_node_get_next(node
);
294 while (node
&& !xbc_node_is_leaf(node
))
295 node
= xbc_node_get_child(node
);
301 * xbc_node_find_next_key_value() - Find the next key-value pair nodes
302 * @root: An XBC root node
303 * @leaf: A container pointer of XBC node which starts from.
305 * Search the next leaf node (which means the terminal key node) of *@leaf
306 * under @root node. Returns the value and update *@leaf if next leaf node
307 * is found, or NULL if no next leaf node is found.
308 * Note that this returns 0-length string if the key has no value, or
309 * the value of the first entry if the value is an array.
311 const char * __init
xbc_node_find_next_key_value(struct xbc_node
*root
,
312 struct xbc_node
**leaf
)
314 /* tip must be passed */
318 *leaf
= xbc_node_find_next_leaf(root
, *leaf
);
322 return xbc_node_get_data(xbc_node_get_child(*leaf
));
324 return ""; /* No value key */
327 /* XBC parse and tree build */
329 static struct xbc_node
* __init
xbc_add_node(char *data
, u32 flag
)
331 struct xbc_node
*node
;
332 unsigned long offset
;
334 if (xbc_node_num
== XBC_NODE_MAX
)
337 node
= &xbc_nodes
[xbc_node_num
++];
338 offset
= data
- xbc_data
;
339 node
->data
= (u16
)offset
;
340 if (WARN_ON(offset
>= XBC_DATA_MAX
))
349 static inline __init
struct xbc_node
*xbc_last_sibling(struct xbc_node
*node
)
352 node
= xbc_node_get_next(node
);
357 static struct xbc_node
* __init
xbc_add_sibling(char *data
, u32 flag
)
359 struct xbc_node
*sib
, *node
= xbc_add_node(data
, flag
);
363 node
->parent
= XBC_NODE_MAX
;
364 sib
= xbc_last_sibling(xbc_nodes
);
365 sib
->next
= xbc_node_index(node
);
367 node
->parent
= xbc_node_index(last_parent
);
368 if (!last_parent
->child
) {
369 last_parent
->child
= xbc_node_index(node
);
371 sib
= xbc_node_get_child(last_parent
);
372 sib
= xbc_last_sibling(sib
);
373 sib
->next
= xbc_node_index(node
);
377 xbc_parse_error("Too many nodes", data
);
382 static inline __init
struct xbc_node
*xbc_add_child(char *data
, u32 flag
)
384 struct xbc_node
*node
= xbc_add_sibling(data
, flag
);
392 static inline __init
bool xbc_valid_keyword(char *key
)
397 while (isalnum(*key
) || *key
== '-' || *key
== '_')
403 static char *skip_comment(char *p
)
407 ret
= strchr(p
, '\n');
416 static char *skip_spaces_until_newline(char *p
)
418 while (isspace(*p
) && *p
!= '\n')
423 static int __init
__xbc_open_brace(void)
425 /* Mark the last key as open brace */
426 last_parent
->next
= XBC_NODE_MAX
;
431 static int __init
__xbc_close_brace(char *p
)
433 struct xbc_node
*node
;
435 if (!last_parent
|| last_parent
->next
!= XBC_NODE_MAX
)
436 return xbc_parse_error("Unexpected closing brace", p
);
441 node
= xbc_node_get_parent(node
);
442 } while (node
&& node
->next
!= XBC_NODE_MAX
);
449 * Return delimiter or error, no node added. As same as lib/cmdline.c,
450 * you can use " around spaces, but can't escape " for value.
452 static int __init
__xbc_parse_value(char **__v
, char **__n
)
462 if (*v
== '"' || *v
== '\'') {
468 if (!isprint(c
) && !isspace(c
))
469 return xbc_parse_error("Non printable value", p
);
475 p
= skip_spaces_until_newline(p
);
477 if (c
&& !strchr(",;\n#}", c
))
478 return xbc_parse_error("No value delimiter", p
);
483 if (strchr(",;\n#}", c
)) {
490 return xbc_parse_error("No closing quotes", p
);
493 c
= '\n'; /* A comment must be treated as a newline */
501 static int __init
xbc_parse_array(char **__v
)
503 struct xbc_node
*node
;
508 c
= __xbc_parse_value(__v
, &next
);
512 node
= xbc_add_sibling(*__v
, XBC_VALUE
);
523 struct xbc_node
*find_match_node(struct xbc_node
*node
, char *k
)
526 if (!strcmp(xbc_node_get_data(node
), k
))
528 node
= xbc_node_get_next(node
);
533 static int __init
__xbc_add_key(char *k
)
535 struct xbc_node
*node
;
537 if (!xbc_valid_keyword(k
))
538 return xbc_parse_error("Invalid keyword", k
);
540 if (unlikely(xbc_node_num
== 0))
543 if (!last_parent
) /* the first level */
544 node
= find_match_node(xbc_nodes
, k
);
546 node
= find_match_node(xbc_node_get_child(last_parent
), k
);
552 node
= xbc_add_child(k
, XBC_KEY
);
559 static int __init
__xbc_parse_keys(char *k
)
565 while ((p
= strchr(k
, '.'))) {
567 ret
= __xbc_add_key(k
);
573 return __xbc_add_key(k
);
576 static int __init
xbc_parse_kv(char **k
, char *v
)
578 struct xbc_node
*prev_parent
= last_parent
;
579 struct xbc_node
*node
;
583 ret
= __xbc_parse_keys(*k
);
587 c
= __xbc_parse_value(&v
, &next
);
591 node
= xbc_add_sibling(v
, XBC_VALUE
);
595 if (c
== ',') { /* Array */
596 c
= xbc_parse_array(&next
);
601 last_parent
= prev_parent
;
604 ret
= __xbc_close_brace(next
- 1);
614 static int __init
xbc_parse_key(char **k
, char *n
)
616 struct xbc_node
*prev_parent
= last_parent
;
621 ret
= __xbc_parse_keys(*k
);
624 last_parent
= prev_parent
;
631 static int __init
xbc_open_brace(char **k
, char *n
)
635 ret
= __xbc_parse_keys(*k
);
640 return __xbc_open_brace();
643 static int __init
xbc_close_brace(char **k
, char *n
)
647 ret
= xbc_parse_key(k
, n
);
650 /* k is updated in xbc_parse_key() */
652 return __xbc_close_brace(n
- 1);
655 static int __init
xbc_verify_tree(void)
657 int i
, depth
, len
, wlen
;
658 struct xbc_node
*n
, *m
;
661 if (xbc_node_num
== 0) {
662 xbc_parse_error("Empty config", xbc_data
);
666 for (i
= 0; i
< xbc_node_num
; i
++) {
667 if (xbc_nodes
[i
].next
> xbc_node_num
) {
668 return xbc_parse_error("No closing brace",
669 xbc_node_get_data(xbc_nodes
+ i
));
673 /* Key tree limitation check */
679 wlen
= strlen(xbc_node_get_data(n
)) + 1;
681 if (len
> XBC_KEYLEN_MAX
)
682 return xbc_parse_error("Too long key length",
683 xbc_node_get_data(n
));
685 m
= xbc_node_get_child(n
);
686 if (m
&& xbc_node_is_key(m
)) {
689 if (depth
> XBC_DEPTH_MAX
)
690 return xbc_parse_error("Too many key words",
691 xbc_node_get_data(n
));
695 m
= xbc_node_get_next(n
);
697 n
= xbc_node_get_parent(n
);
700 len
-= strlen(xbc_node_get_data(n
)) + 1;
702 m
= xbc_node_get_next(n
);
711 * xbc_destroy_all() - Clean up all parsed bootconfig
713 * This clears all data structures of parsed bootconfig on memory.
714 * If you need to reuse xbc_init() with new boot config, you can
717 void __init
xbc_destroy_all(void)
722 memset(xbc_nodes
, 0, sizeof(xbc_nodes
));
726 * xbc_init() - Parse given XBC file and build XBC internal tree
727 * @buf: boot config text
729 * This parses the boot config text in @buf. @buf must be a
730 * null terminated string and smaller than XBC_DATA_MAX.
731 * Return the number of stored nodes (>0) if succeeded, or -errno
732 * if there is any error.
734 int __init
xbc_init(char *buf
)
740 pr_err("Error: bootconfig is already initialized.\n");
745 if (ret
> XBC_DATA_MAX
- 1 || ret
== 0) {
746 pr_err("Error: Config data is %s.\n",
747 ret
? "too big" : "empty");
752 xbc_data_size
= ret
+ 1;
757 q
= strpbrk(p
, "{}=;\n#");
761 ret
= xbc_parse_error("No delimiter", p
);
769 ret
= xbc_parse_kv(&p
, q
);
772 ret
= xbc_open_brace(&p
, q
);
779 ret
= xbc_parse_key(&p
, q
);
782 ret
= xbc_close_brace(&p
, q
);
788 ret
= xbc_verify_tree();
799 * xbc_debug_dump() - Dump current XBC node list
801 * Dump the current XBC node list on printk buffer for debug.
803 void __init
xbc_debug_dump(void)
807 for (i
= 0; i
< xbc_node_num
; i
++) {
808 pr_debug("[%d] %s (%s) .next=%d, .child=%d .parent=%d\n", i
,
809 xbc_node_get_data(xbc_nodes
+ i
),
810 xbc_node_is_value(xbc_nodes
+ i
) ? "value" : "key",
811 xbc_nodes
[i
].next
, xbc_nodes
[i
].child
,
812 xbc_nodes
[i
].parent
);