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