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