xref: /linux/lib/bootconfig.c (revision 5a643e4623238e14b03d75ca0d4eda0645720cee)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Extra Boot Config
4  * Masami Hiramatsu <mhiramat@kernel.org>
5  */
6 
7 /*
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.
13  */
14 #include <linux/bootconfig.h>
15 
16 #ifdef __KERNEL__
17 #include <linux/bug.h>
18 #include <linux/ctype.h>
19 #include <linux/errno.h>
20 #include <linux/cache.h>
21 #include <linux/compiler.h>
22 #include <linux/sprintf.h>
23 #include <linux/memblock.h>
24 #include <linux/string.h>
25 
26 #ifdef CONFIG_BOOT_CONFIG_EMBED
27 /* embedded_bootconfig_data is defined in bootconfig-data.S */
28 extern __visible const char embedded_bootconfig_data[];
29 extern __visible const char embedded_bootconfig_data_end[];
30 
31 const char * __init xbc_get_embedded_bootconfig(size_t *size)
32 {
33 	*size = embedded_bootconfig_data_end - embedded_bootconfig_data;
34 	return (*size) ? embedded_bootconfig_data : NULL;
35 }
36 #endif
37 #endif
38 
39 /*
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
45  * node (for array).
46  */
47 
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;
57 
58 #ifdef __KERNEL__
59 static inline void * __init xbc_alloc_mem(size_t size)
60 {
61 	return memblock_alloc(size, SMP_CACHE_BYTES);
62 }
63 
64 static inline void __init xbc_free_mem(void *addr, size_t size, bool early)
65 {
66 	if (early)
67 		memblock_free(addr, size);
68 	else if (addr)
69 		memblock_free(addr, size);
70 }
71 
72 #else /* !__KERNEL__ */
73 
74 static inline void *xbc_alloc_mem(size_t size)
75 {
76 	return calloc(1, size);
77 }
78 
79 static inline void xbc_free_mem(void *addr, size_t size, bool early)
80 {
81 	free(addr);
82 }
83 #endif
84 
85 /**
86  * xbc_get_info() - Get the information of loaded boot config
87  * @node_size: A pointer to store the number of nodes.
88  * @data_size: A pointer to store the size of bootconfig data.
89  *
90  * Get the number of used nodes in @node_size if it is not NULL,
91  * and the size of bootconfig data in @data_size if it is not NULL.
92  * Return 0 if the boot config is initialized, or return -ENODEV.
93  */
94 int __init xbc_get_info(int *node_size, size_t *data_size)
95 {
96 	if (!xbc_data)
97 		return -ENODEV;
98 
99 	if (node_size)
100 		*node_size = xbc_node_num;
101 	if (data_size)
102 		*data_size = xbc_data_size;
103 	return 0;
104 }
105 
106 static int __init xbc_parse_error(const char *msg, const char *p)
107 {
108 	xbc_err_msg = msg;
109 	xbc_err_pos = (int)(p - xbc_data);
110 
111 	return -EINVAL;
112 }
113 
114 /**
115  * xbc_root_node() - Get the root node of extended boot config
116  *
117  * Return the address of root node of extended boot config. If the
118  * extended boot config is not initialized, return NULL.
119  */
120 struct xbc_node * __init xbc_root_node(void)
121 {
122 	if (unlikely(!xbc_data))
123 		return NULL;
124 
125 	return xbc_nodes;
126 }
127 
128 /**
129  * xbc_node_index() - Get the index of XBC node
130  * @node: A target node of getting index.
131  *
132  * Return the index number of @node in XBC node list.
133  */
134 uint16_t __init xbc_node_index(struct xbc_node *node)
135 {
136 	return (uint16_t)(node - &xbc_nodes[0]);
137 }
138 
139 /**
140  * xbc_node_get_parent() - Get the parent XBC node
141  * @node: An XBC node.
142  *
143  * Return the parent node of @node. If the node is top node of the tree,
144  * return NULL.
145  */
146 struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node)
147 {
148 	return node->parent == XBC_NODE_MAX ? NULL : &xbc_nodes[node->parent];
149 }
150 
151 /**
152  * xbc_node_get_child() - Get the child XBC node
153  * @node: An XBC node.
154  *
155  * Return the first child node of @node. If the node has no child, return
156  * NULL.
157  */
158 struct xbc_node * __init xbc_node_get_child(struct xbc_node *node)
159 {
160 	return node->child ? &xbc_nodes[node->child] : NULL;
161 }
162 
163 /**
164  * xbc_node_get_next() - Get the next sibling XBC node
165  * @node: An XBC node.
166  *
167  * Return the NEXT sibling node of @node. If the node has no next sibling,
168  * return NULL. Note that even if this returns NULL, it doesn't mean @node
169  * has no siblings. (You also has to check whether the parent's child node
170  * is @node or not.)
171  */
172 struct xbc_node * __init xbc_node_get_next(struct xbc_node *node)
173 {
174 	return node->next ? &xbc_nodes[node->next] : NULL;
175 }
176 
177 /**
178  * xbc_node_get_data() - Get the data of XBC node
179  * @node: An XBC node.
180  *
181  * Return the data (which is always a null terminated string) of @node.
182  * If the node has invalid data, warn and return NULL.
183  */
184 const char * __init xbc_node_get_data(struct xbc_node *node)
185 {
186 	size_t offset = node->data & ~XBC_VALUE;
187 
188 	if (WARN_ON(offset >= xbc_data_size))
189 		return NULL;
190 
191 	return xbc_data + offset;
192 }
193 
194 static bool __init
195 xbc_node_match_prefix(struct xbc_node *node, const char **prefix)
196 {
197 	const char *p = xbc_node_get_data(node);
198 	size_t len = strlen(p);
199 
200 	if (strncmp(*prefix, p, len))
201 		return false;
202 
203 	p = *prefix + len;
204 	if (*p == '.')
205 		p++;
206 	else if (*p != '\0')
207 		return false;
208 	*prefix = p;
209 
210 	return true;
211 }
212 
213 /**
214  * xbc_node_find_subkey() - Find a subkey node which matches given key
215  * @parent: An XBC node.
216  * @key: A key string.
217  *
218  * Search a key node under @parent which matches @key. The @key can contain
219  * several words jointed with '.'. If @parent is NULL, this searches the
220  * node from whole tree. Return NULL if no node is matched.
221  */
222 struct xbc_node * __init
223 xbc_node_find_subkey(struct xbc_node *parent, const char *key)
224 {
225 	struct xbc_node *node;
226 
227 	if (parent)
228 		node = xbc_node_get_subkey(parent);
229 	else
230 		node = xbc_root_node();
231 
232 	while (node && xbc_node_is_key(node)) {
233 		if (!xbc_node_match_prefix(node, &key))
234 			node = xbc_node_get_next(node);
235 		else if (*key != '\0')
236 			node = xbc_node_get_subkey(node);
237 		else
238 			break;
239 	}
240 
241 	return node;
242 }
243 
244 /**
245  * xbc_node_find_value() - Find a value node which matches given key
246  * @parent: An XBC node.
247  * @key: A key string.
248  * @vnode: A container pointer of found XBC node.
249  *
250  * Search a value node under @parent whose (parent) key node matches @key,
251  * store it in *@vnode, and returns the value string.
252  * The @key can contain several words jointed with '.'. If @parent is NULL,
253  * this searches the node from whole tree. Return the value string if a
254  * matched key found, return NULL if no node is matched.
255  * Note that this returns 0-length string and stores NULL in *@vnode if the
256  * key has no value. And also it will return the value of the first entry if
257  * the value is an array.
258  */
259 const char * __init
260 xbc_node_find_value(struct xbc_node *parent, const char *key,
261 		    struct xbc_node **vnode)
262 {
263 	struct xbc_node *node = xbc_node_find_subkey(parent, key);
264 
265 	if (!node || !xbc_node_is_key(node))
266 		return NULL;
267 
268 	node = xbc_node_get_child(node);
269 	if (node && !xbc_node_is_value(node))
270 		return NULL;
271 
272 	if (vnode)
273 		*vnode = node;
274 
275 	return node ? xbc_node_get_data(node) : "";
276 }
277 
278 /**
279  * xbc_node_compose_key_after() - Compose partial key string of the XBC node
280  * @root: Root XBC node
281  * @node: Target XBC node.
282  * @buf: A buffer to store the key.
283  * @size: The size of the @buf.
284  *
285  * Compose the partial key of the @node into @buf, which is starting right
286  * after @root (@root is not included.) If @root is NULL, this returns full
287  * key words of @node.
288  * Returns the total length of the key stored in @buf. Returns -EINVAL
289  * if @node is NULL or @root is not the ancestor of @node or @root is @node,
290  * or returns -ERANGE if the key depth is deeper than max depth.
291  * This is expected to be used with xbc_find_node() to list up all (child)
292  * keys under given key.
293  */
294 int __init xbc_node_compose_key_after(struct xbc_node *root,
295 				      struct xbc_node *node,
296 				      char *buf, size_t size)
297 {
298 	uint16_t keys[XBC_DEPTH_MAX];
299 	int depth = 0, ret = 0, total = 0;
300 
301 	if (!node || node == root)
302 		return -EINVAL;
303 
304 	if (xbc_node_is_value(node))
305 		node = xbc_node_get_parent(node);
306 
307 	while (node && node != root) {
308 		keys[depth++] = xbc_node_index(node);
309 		if (depth == XBC_DEPTH_MAX)
310 			return -ERANGE;
311 		node = xbc_node_get_parent(node);
312 	}
313 	if (!node && root)
314 		return -EINVAL;
315 
316 	while (--depth >= 0) {
317 		node = xbc_nodes + keys[depth];
318 		ret = snprintf(buf, size, "%s%s", xbc_node_get_data(node),
319 			       depth ? "." : "");
320 		if (ret < 0)
321 			return ret;
322 		if (ret >= size) {
323 			size = 0;
324 		} else {
325 			size -= ret;
326 			buf += ret;
327 		}
328 		total += ret;
329 	}
330 
331 	return total;
332 }
333 
334 /**
335  * xbc_node_find_next_leaf() - Find the next leaf node under given node
336  * @root: An XBC root node
337  * @node: An XBC node which starts from.
338  *
339  * Search the next leaf node (which means the terminal key node) of @node
340  * under @root node (including @root node itself).
341  * Return the next node or NULL if next leaf node is not found.
342  */
343 struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root,
344 						 struct xbc_node *node)
345 {
346 	struct xbc_node *next;
347 
348 	if (unlikely(!xbc_data))
349 		return NULL;
350 
351 	if (!node) {	/* First try */
352 		node = root;
353 		if (!node)
354 			node = xbc_nodes;
355 	} else {
356 		/* Leaf node may have a subkey */
357 		next = xbc_node_get_subkey(node);
358 		if (next) {
359 			node = next;
360 			goto found;
361 		}
362 
363 		if (node == root)	/* @root was a leaf, no child node. */
364 			return NULL;
365 
366 		while (!node->next) {
367 			node = xbc_node_get_parent(node);
368 			if (node == root)
369 				return NULL;
370 			/* User passed a node which is not under parent */
371 			if (WARN_ON(!node))
372 				return NULL;
373 		}
374 		node = xbc_node_get_next(node);
375 	}
376 
377 found:
378 	while (node && !xbc_node_is_leaf(node))
379 		node = xbc_node_get_child(node);
380 
381 	return node;
382 }
383 
384 /**
385  * xbc_node_find_next_key_value() - Find the next key-value pair nodes
386  * @root: An XBC root node
387  * @leaf: A container pointer of XBC node which starts from.
388  *
389  * Search the next leaf node (which means the terminal key node) of *@leaf
390  * under @root node. Returns the value and update *@leaf if next leaf node
391  * is found, or NULL if no next leaf node is found.
392  * Note that this returns 0-length string if the key has no value, or
393  * the value of the first entry if the value is an array.
394  */
395 const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
396 						 struct xbc_node **leaf)
397 {
398 	/* tip must be passed */
399 	if (WARN_ON(!leaf))
400 		return NULL;
401 
402 	*leaf = xbc_node_find_next_leaf(root, *leaf);
403 	if (!*leaf)
404 		return NULL;
405 	if ((*leaf)->child)
406 		return xbc_node_get_data(xbc_node_get_child(*leaf));
407 	else
408 		return "";	/* No value key */
409 }
410 
411 static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata;
412 
413 #define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0)
414 
415 /**
416  * xbc_snprint_cmdline() - Render bootconfig keys under @root as a cmdline string
417  * @buf: Destination buffer (may be NULL when @size is 0 to query the length)
418  * @size: Size of @buf in bytes
419  * @root: Subtree root whose key=value pairs should be rendered
420  *
421  * Walk all key/value pairs under @root and emit them as a space-separated
422  * cmdline string into @buf. Values containing whitespace are quoted with
423  * double quotes. Returns the number of bytes that would be written if @buf
424  * were large enough (matching snprintf semantics), or a negative errno on
425  * failure.
426  */
427 int __init xbc_snprint_cmdline(char *buf, size_t size, struct xbc_node *root)
428 {
429 	struct xbc_node *knode, *vnode;
430 	char *end = buf + size;
431 	const char *val, *q;
432 	int ret;
433 
434 	xbc_node_for_each_key_value(root, knode, val) {
435 		ret = xbc_node_compose_key_after(root, knode,
436 					xbc_namebuf, XBC_KEYLEN_MAX);
437 		if (ret < 0)
438 			return ret;
439 
440 		vnode = xbc_node_get_child(knode);
441 		if (!vnode) {
442 			ret = snprintf(buf, rest(buf, end), "%s ", xbc_namebuf);
443 			if (ret < 0)
444 				return ret;
445 			buf += ret;
446 			continue;
447 		}
448 		xbc_array_for_each_value(vnode, val) {
449 			/*
450 			 * For prettier and more readable /proc/cmdline, only
451 			 * quote the value when necessary, i.e. when it contains
452 			 * whitespace.
453 			 */
454 			q = strpbrk(val, " \t\r\n") ? "\"" : "";
455 			ret = snprintf(buf, rest(buf, end), "%s=%s%s%s ",
456 				       xbc_namebuf, q, val, q);
457 			if (ret < 0)
458 				return ret;
459 			buf += ret;
460 		}
461 	}
462 
463 	return buf - (end - size);
464 }
465 #undef rest
466 
467 /* XBC parse and tree build */
468 
469 static int __init xbc_init_node(struct xbc_node *node, char *data, uint16_t flag)
470 {
471 	long offset = data - xbc_data;
472 
473 	if (WARN_ON(offset < 0 || offset >= XBC_DATA_MAX))
474 		return -EINVAL;
475 
476 	node->data = (uint16_t)offset | flag;
477 	node->child = 0;
478 	node->next = 0;
479 
480 	return 0;
481 }
482 
483 static struct xbc_node * __init xbc_add_node(char *data, uint16_t flag)
484 {
485 	struct xbc_node *node;
486 
487 	if (xbc_node_num == XBC_NODE_MAX)
488 		return NULL;
489 
490 	node = &xbc_nodes[xbc_node_num];
491 	if (xbc_init_node(node, data, flag) < 0)
492 		return NULL;
493 	xbc_node_num++;
494 
495 	return node;
496 }
497 
498 static inline __init struct xbc_node *xbc_last_sibling(struct xbc_node *node)
499 {
500 	while (node->next)
501 		node = xbc_node_get_next(node);
502 
503 	return node;
504 }
505 
506 static inline __init struct xbc_node *xbc_last_child(struct xbc_node *node)
507 {
508 	while (node->child)
509 		node = xbc_node_get_child(node);
510 
511 	return node;
512 }
513 
514 static struct xbc_node * __init __xbc_add_sibling(char *data, uint16_t flag, bool head)
515 {
516 	struct xbc_node *sib, *node = xbc_add_node(data, flag);
517 
518 	if (node) {
519 		if (!last_parent) {
520 			/* Ignore @head in this case */
521 			node->parent = XBC_NODE_MAX;
522 			sib = xbc_last_sibling(xbc_nodes);
523 			sib->next = xbc_node_index(node);
524 		} else {
525 			node->parent = xbc_node_index(last_parent);
526 			if (!last_parent->child || head) {
527 				node->next = last_parent->child;
528 				last_parent->child = xbc_node_index(node);
529 			} else {
530 				sib = xbc_node_get_child(last_parent);
531 				sib = xbc_last_sibling(sib);
532 				sib->next = xbc_node_index(node);
533 			}
534 		}
535 	} else {
536 		xbc_parse_error("Too many nodes", data);
537 	}
538 
539 	return node;
540 }
541 
542 static inline struct xbc_node * __init xbc_add_sibling(char *data, uint16_t flag)
543 {
544 	return __xbc_add_sibling(data, flag, false);
545 }
546 
547 static inline struct xbc_node * __init xbc_add_head_sibling(char *data, uint16_t flag)
548 {
549 	return __xbc_add_sibling(data, flag, true);
550 }
551 
552 static inline __init struct xbc_node *xbc_add_child(char *data, uint16_t flag)
553 {
554 	struct xbc_node *node = xbc_add_sibling(data, flag);
555 
556 	if (node)
557 		last_parent = node;
558 
559 	return node;
560 }
561 
562 static inline __init bool xbc_valid_keyword(char *key)
563 {
564 	if (key[0] == '\0')
565 		return false;
566 
567 	while (isalnum(*key) || *key == '-' || *key == '_')
568 		key++;
569 
570 	return *key == '\0';
571 }
572 
573 static char *skip_comment(char *p)
574 {
575 	char *ret;
576 
577 	ret = strchr(p, '\n');
578 	if (!ret)
579 		ret = p + strlen(p);
580 	else
581 		ret++;
582 
583 	return ret;
584 }
585 
586 static char *skip_spaces_until_newline(char *p)
587 {
588 	while (isspace(*p) && *p != '\n')
589 		p++;
590 	return p;
591 }
592 
593 static int __init __xbc_open_brace(char *p)
594 {
595 	/* Push the last key as open brace */
596 	if (brace_index >= XBC_DEPTH_MAX)
597 		return xbc_parse_error("Exceed max depth of braces", p);
598 	open_brace[brace_index++] = xbc_node_index(last_parent);
599 
600 	return 0;
601 }
602 
603 static int __init __xbc_close_brace(char *p)
604 {
605 	brace_index--;
606 	if (!last_parent || brace_index < 0 ||
607 	    (open_brace[brace_index] != xbc_node_index(last_parent)))
608 		return xbc_parse_error("Unexpected closing brace", p);
609 
610 	if (brace_index == 0)
611 		last_parent = NULL;
612 	else
613 		last_parent = &xbc_nodes[open_brace[brace_index - 1]];
614 
615 	return 0;
616 }
617 
618 /*
619  * Return delimiter or error, no node added. As same as lib/cmdline.c,
620  * you can use " around spaces, but can't escape " for value.
621  * *@__v must point real value string. (not including spaces before value.)
622  */
623 static int __init __xbc_parse_value(char **__v, char **__n)
624 {
625 	char *p, *v = *__v;
626 	int c, quotes = 0;
627 
628 	if (*v == '"' || *v == '\'') {
629 		quotes = *v;
630 		v++;
631 	}
632 	p = v - 1;
633 	while ((c = *++p)) {
634 		if (!isprint(c) && !isspace(c))
635 			return xbc_parse_error("Non printable value", p);
636 		if (quotes) {
637 			if (c != quotes)
638 				continue;
639 			quotes = 0;
640 			*p++ = '\0';
641 			p = skip_spaces_until_newline(p);
642 			c = *p;
643 			if (c && !strchr(",;\n#}", c))
644 				return xbc_parse_error("No value delimiter", p);
645 			if (*p)
646 				p++;
647 			break;
648 		}
649 		if (strchr(",;\n#}", c)) {
650 			*p++ = '\0';
651 			v = strim(v);
652 			break;
653 		}
654 	}
655 	if (quotes)
656 		return xbc_parse_error("No closing quotes", p);
657 	if (c == '#') {
658 		p = skip_comment(p);
659 		c = '\n';	/* A comment must be treated as a newline */
660 	}
661 	*__n = p;
662 	*__v = v;
663 
664 	return c;
665 }
666 
667 static int __init xbc_parse_array(char **__v)
668 {
669 	struct xbc_node *node;
670 	char *next;
671 	int c = 0;
672 
673 	if (last_parent->child)
674 		last_parent = xbc_node_get_child(last_parent);
675 
676 	do {
677 		/* Search the next array value beyond comments and empty lines */
678 		next = skip_spaces(*__v);
679 		while (*next == '#') {
680 			next = skip_comment(next);
681 			next = skip_spaces(next);
682 		}
683 		*__v = next;
684 		c = __xbc_parse_value(__v, &next);
685 		if (c < 0)
686 			return c;
687 
688 		node = xbc_add_child(*__v, XBC_VALUE);
689 		if (!node)
690 			return -ENOMEM;
691 		*__v = next;
692 	} while (c == ',');
693 	node->child = 0;
694 
695 	return c;
696 }
697 
698 static inline __init
699 struct xbc_node *find_match_node(struct xbc_node *node, char *k)
700 {
701 	while (node) {
702 		if (!strcmp(xbc_node_get_data(node), k))
703 			break;
704 		node = xbc_node_get_next(node);
705 	}
706 	return node;
707 }
708 
709 static int __init __xbc_add_key(char *k)
710 {
711 	struct xbc_node *node, *child;
712 
713 	if (!xbc_valid_keyword(k))
714 		return xbc_parse_error("Invalid keyword", k);
715 
716 	if (unlikely(xbc_node_num == 0))
717 		goto add_node;
718 
719 	if (!last_parent) {	/* the first level */
720 		node = find_match_node(xbc_nodes, k);
721 	} else {
722 		child = xbc_node_get_child(last_parent);
723 		/* Since the value node is the first child, skip it. */
724 		if (child && xbc_node_is_value(child))
725 			child = xbc_node_get_next(child);
726 		node = find_match_node(child, k);
727 	}
728 
729 	if (node) {
730 		last_parent = node;
731 	} else {
732 add_node:
733 		node = xbc_add_child(k, XBC_KEY);
734 		if (!node)
735 			return -ENOMEM;
736 	}
737 	return 0;
738 }
739 
740 static int __init __xbc_parse_keys(char *k)
741 {
742 	char *p;
743 	int ret;
744 
745 	k = strim(k);
746 	while ((p = strchr(k, '.'))) {
747 		*p++ = '\0';
748 		ret = __xbc_add_key(k);
749 		if (ret)
750 			return ret;
751 		k = p;
752 	}
753 
754 	return __xbc_add_key(k);
755 }
756 
757 static int __init xbc_parse_kv(char **k, char *v, int op)
758 {
759 	struct xbc_node *prev_parent = last_parent;
760 	struct xbc_node *child;
761 	char *next;
762 	int c, ret;
763 
764 	ret = __xbc_parse_keys(*k);
765 	if (ret)
766 		return ret;
767 
768 	v = skip_spaces_until_newline(v);
769 	/* If there is a comment, this has an empty value. */
770 	if (*v == '#') {
771 		next = skip_comment(v);
772 		*v = '\0';
773 		c = '\n';
774 	} else {
775 		c = __xbc_parse_value(&v, &next);
776 		if (c < 0)
777 			return c;
778 	}
779 
780 	child = xbc_node_get_child(last_parent);
781 	if (child && xbc_node_is_value(child)) {
782 		if (op == '=')
783 			return xbc_parse_error("Value is redefined", v);
784 		if (op == ':') {
785 			unsigned short nidx = child->next;
786 
787 			if (xbc_init_node(child, v, XBC_VALUE) < 0)
788 				return xbc_parse_error("Failed to override value", v);
789 			child->next = nidx;	/* keep subkeys */
790 			goto array;
791 		}
792 		/* op must be '+' */
793 		last_parent = xbc_last_child(child);
794 	}
795 	/* The value node should always be the first child */
796 	if (!xbc_add_head_sibling(v, XBC_VALUE))
797 		return -ENOMEM;
798 
799 array:
800 	if (c == ',') {	/* Array */
801 		c = xbc_parse_array(&next);
802 		if (c < 0)
803 			return c;
804 	}
805 
806 	last_parent = prev_parent;
807 
808 	if (c == '}') {
809 		ret = __xbc_close_brace(next - 1);
810 		if (ret < 0)
811 			return ret;
812 	}
813 
814 	*k = next;
815 
816 	return 0;
817 }
818 
819 static int __init xbc_parse_key(char **k, char *n)
820 {
821 	struct xbc_node *prev_parent = last_parent;
822 	int ret;
823 
824 	*k = strim(*k);
825 	if (**k != '\0') {
826 		ret = __xbc_parse_keys(*k);
827 		if (ret)
828 			return ret;
829 		last_parent = prev_parent;
830 	}
831 	*k = n;
832 
833 	return 0;
834 }
835 
836 static int __init xbc_open_brace(char **k, char *n)
837 {
838 	int ret;
839 
840 	ret = __xbc_parse_keys(*k);
841 	if (ret)
842 		return ret;
843 	*k = n;
844 
845 	return __xbc_open_brace(n - 1);
846 }
847 
848 static int __init xbc_close_brace(char **k, char *n)
849 {
850 	int ret;
851 
852 	ret = xbc_parse_key(k, n);
853 	if (ret)
854 		return ret;
855 	/* k is updated in xbc_parse_key() */
856 
857 	return __xbc_close_brace(n - 1);
858 }
859 
860 static int __init xbc_verify_tree(void)
861 {
862 	int i, depth;
863 	size_t len, wlen;
864 	struct xbc_node *n, *m;
865 
866 	/* Brace closing */
867 	if (brace_index) {
868 		n = &xbc_nodes[open_brace[brace_index - 1]];
869 		return xbc_parse_error("Brace is not closed",
870 					xbc_node_get_data(n));
871 	}
872 
873 	/* Empty tree */
874 	if (xbc_node_num == 0) {
875 		xbc_parse_error("Empty config", xbc_data);
876 		return -ENOENT;
877 	}
878 
879 	for (i = 0; i < xbc_node_num; i++) {
880 		if (xbc_nodes[i].next >= xbc_node_num) {
881 			return xbc_parse_error("No closing brace",
882 				xbc_node_get_data(xbc_nodes + i));
883 		}
884 		if (xbc_nodes[i].child >= xbc_node_num) {
885 			return xbc_parse_error("Broken child node",
886 				xbc_node_get_data(xbc_nodes + i));
887 		}
888 	}
889 
890 	/* Key tree limitation check */
891 	n = &xbc_nodes[0];
892 	depth = 1;
893 	len = 0;
894 
895 	while (n) {
896 		wlen = strlen(xbc_node_get_data(n)) + 1;
897 		len += wlen;
898 		if (len > XBC_KEYLEN_MAX)
899 			return xbc_parse_error("Too long key length",
900 				xbc_node_get_data(n));
901 
902 		m = xbc_node_get_child(n);
903 		if (m && xbc_node_is_key(m)) {
904 			n = m;
905 			depth++;
906 			if (depth > XBC_DEPTH_MAX)
907 				return xbc_parse_error("Too many key words",
908 						xbc_node_get_data(n));
909 			continue;
910 		}
911 		len -= wlen;
912 		m = xbc_node_get_next(n);
913 		while (!m) {
914 			n = xbc_node_get_parent(n);
915 			if (!n)
916 				break;
917 			len -= strlen(xbc_node_get_data(n)) + 1;
918 			depth--;
919 			m = xbc_node_get_next(n);
920 		}
921 		n = m;
922 	}
923 
924 	return 0;
925 }
926 
927 /* Need to setup xbc_data and xbc_nodes before call this. */
928 static int __init xbc_parse_tree(void)
929 {
930 	char *p, *q;
931 	int ret = 0, c;
932 
933 	last_parent = NULL;
934 	p = xbc_data;
935 	do {
936 		q = strpbrk(p, "{}=+;:\n#");
937 		if (!q) {
938 			p = skip_spaces(p);
939 			if (*p != '\0')
940 				ret = xbc_parse_error("No delimiter", p);
941 			break;
942 		}
943 
944 		c = *q;
945 		*q++ = '\0';
946 		switch (c) {
947 		case ':':
948 		case '+':
949 			if (*q++ != '=') {
950 				ret = xbc_parse_error(c == '+' ?
951 						"Wrong '+' operator" :
952 						"Wrong ':' operator",
953 							q - 2);
954 				break;
955 			}
956 			fallthrough;
957 		case '=':
958 			ret = xbc_parse_kv(&p, q, c);
959 			break;
960 		case '{':
961 			ret = xbc_open_brace(&p, q);
962 			break;
963 		case '#':
964 			q = skip_comment(q);
965 			fallthrough;
966 		case ';':
967 		case '\n':
968 			ret = xbc_parse_key(&p, q);
969 			break;
970 		case '}':
971 			ret = xbc_close_brace(&p, q);
972 			break;
973 		}
974 	} while (!ret);
975 
976 	return ret;
977 }
978 
979 /**
980  * _xbc_exit() - Clean up all parsed bootconfig
981  * @early: Set true if this is called before budy system is initialized.
982  *
983  * This clears all data structures of parsed bootconfig on memory.
984  * If you need to reuse xbc_init() with new boot config, you can
985  * use this.
986  */
987 void __init _xbc_exit(bool early)
988 {
989 	xbc_free_mem(xbc_data, xbc_data_size, early);
990 	xbc_data = NULL;
991 	xbc_data_size = 0;
992 	xbc_node_num = 0;
993 	xbc_free_mem(xbc_nodes, sizeof(struct xbc_node) * XBC_NODE_MAX, early);
994 	xbc_nodes = NULL;
995 	brace_index = 0;
996 }
997 
998 /**
999  * xbc_init() - Parse given XBC file and build XBC internal tree
1000  * @data: The boot config text original data
1001  * @size: The size of @data
1002  * @emsg: A pointer of const char * to store the error message
1003  * @epos: A pointer of int to store the error position
1004  *
1005  * This parses the boot config text in @data. @size must be smaller
1006  * than XBC_DATA_MAX.
1007  * Return the number of stored nodes (>0) if succeeded, or -errno
1008  * if there is any error.
1009  * In error cases, @emsg will be updated with an error message and
1010  * @epos will be updated with the error position which is the byte offset
1011  * of @buf. If the error is not a parser error, @epos will be -1.
1012  */
1013 int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos)
1014 {
1015 	int ret;
1016 
1017 	if (epos)
1018 		*epos = -1;
1019 
1020 	if (xbc_data) {
1021 		if (emsg)
1022 			*emsg = "Bootconfig is already initialized";
1023 		return -EBUSY;
1024 	}
1025 	if (size > XBC_DATA_MAX || size == 0) {
1026 		if (emsg)
1027 			*emsg = size ? "Config data is too big" :
1028 				"Config data is empty";
1029 		return -ERANGE;
1030 	}
1031 
1032 	xbc_data = xbc_alloc_mem(size + 1);
1033 	if (!xbc_data) {
1034 		if (emsg)
1035 			*emsg = "Failed to allocate bootconfig data";
1036 		return -ENOMEM;
1037 	}
1038 	memcpy(xbc_data, data, size);
1039 	xbc_data[size] = '\0';
1040 	xbc_data_size = size + 1;
1041 
1042 	xbc_nodes = xbc_alloc_mem(sizeof(struct xbc_node) * XBC_NODE_MAX);
1043 	if (!xbc_nodes) {
1044 		if (emsg)
1045 			*emsg = "Failed to allocate bootconfig nodes";
1046 		_xbc_exit(true);
1047 		return -ENOMEM;
1048 	}
1049 
1050 	ret = xbc_parse_tree();
1051 	if (!ret)
1052 		ret = xbc_verify_tree();
1053 
1054 	if (ret < 0) {
1055 		if (epos)
1056 			*epos = xbc_err_pos;
1057 		if (emsg)
1058 			*emsg = xbc_err_msg;
1059 		_xbc_exit(true);
1060 	} else {
1061 		ret = xbc_node_num;
1062 	}
1063 
1064 	return ret;
1065 }
1066