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