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