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