blob: 912ef4921398687d8c4483004a38b48319d08758 [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;
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +090032static const char *xbc_err_msg __initdata;
33static int xbc_err_pos __initdata;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +090034
35static int __init xbc_parse_error(const char *msg, const char *p)
36{
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +090037 xbc_err_msg = msg;
38 xbc_err_pos = (int)(p - xbc_data);
Masami Hiramatsu76db5a22020-01-11 01:03:32 +090039
Masami Hiramatsu76db5a22020-01-11 01:03:32 +090040 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 */
49struct 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 */
63int __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 */
75struct 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 */
87struct 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 */
101struct 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 */
113const 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
123static bool __init
124xbc_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 */
151struct xbc_node * __init
152xbc_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 */
188const char * __init
189xbc_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 */
223int __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 */
272struct 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 */
314const 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
332static struct xbc_node * __init xbc_add_node(char *data, u32 flag)
333{
334 struct xbc_node *node;
335 unsigned long offset;
336
337 if (xbc_node_num == XBC_NODE_MAX)
338 return NULL;
339
340 node = &xbc_nodes[xbc_node_num++];
341 offset = data - xbc_data;
342 node->data = (u16)offset;
343 if (WARN_ON(offset >= XBC_DATA_MAX))
344 return NULL;
345 node->data |= flag;
346 node->child = 0;
347 node->next = 0;
348
349 return node;
350}
351
352static inline __init struct xbc_node *xbc_last_sibling(struct xbc_node *node)
353{
354 while (node->next)
355 node = xbc_node_get_next(node);
356
357 return node;
358}
359
360static struct xbc_node * __init xbc_add_sibling(char *data, u32 flag)
361{
362 struct xbc_node *sib, *node = xbc_add_node(data, flag);
363
364 if (node) {
365 if (!last_parent) {
366 node->parent = XBC_NODE_MAX;
367 sib = xbc_last_sibling(xbc_nodes);
368 sib->next = xbc_node_index(node);
369 } else {
370 node->parent = xbc_node_index(last_parent);
371 if (!last_parent->child) {
372 last_parent->child = xbc_node_index(node);
373 } else {
374 sib = xbc_node_get_child(last_parent);
375 sib = xbc_last_sibling(sib);
376 sib->next = xbc_node_index(node);
377 }
378 }
Masami Hiramatsu597c0e32020-02-05 22:50:04 +0900379 } else
380 xbc_parse_error("Too many nodes", data);
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900381
382 return node;
383}
384
385static inline __init struct xbc_node *xbc_add_child(char *data, u32 flag)
386{
387 struct xbc_node *node = xbc_add_sibling(data, flag);
388
389 if (node)
390 last_parent = node;
391
392 return node;
393}
394
395static inline __init bool xbc_valid_keyword(char *key)
396{
397 if (key[0] == '\0')
398 return false;
399
400 while (isalnum(*key) || *key == '-' || *key == '_')
401 key++;
402
403 return *key == '\0';
404}
405
406static char *skip_comment(char *p)
407{
408 char *ret;
409
410 ret = strchr(p, '\n');
411 if (!ret)
412 ret = p + strlen(p);
413 else
414 ret++;
415
416 return ret;
417}
418
419static char *skip_spaces_until_newline(char *p)
420{
421 while (isspace(*p) && *p != '\n')
422 p++;
423 return p;
424}
425
426static int __init __xbc_open_brace(void)
427{
428 /* Mark the last key as open brace */
429 last_parent->next = XBC_NODE_MAX;
430
431 return 0;
432}
433
434static int __init __xbc_close_brace(char *p)
435{
436 struct xbc_node *node;
437
438 if (!last_parent || last_parent->next != XBC_NODE_MAX)
439 return xbc_parse_error("Unexpected closing brace", p);
440
441 node = last_parent;
442 node->next = 0;
443 do {
444 node = xbc_node_get_parent(node);
445 } while (node && node->next != XBC_NODE_MAX);
446 last_parent = node;
447
448 return 0;
449}
450
451/*
452 * Return delimiter or error, no node added. As same as lib/cmdline.c,
453 * you can use " around spaces, but can't escape " for value.
454 */
455static int __init __xbc_parse_value(char **__v, char **__n)
456{
457 char *p, *v = *__v;
458 int c, quotes = 0;
459
460 v = skip_spaces(v);
461 while (*v == '#') {
462 v = skip_comment(v);
463 v = skip_spaces(v);
464 }
465 if (*v == '"' || *v == '\'') {
466 quotes = *v;
467 v++;
468 }
469 p = v - 1;
470 while ((c = *++p)) {
471 if (!isprint(c) && !isspace(c))
472 return xbc_parse_error("Non printable value", p);
473 if (quotes) {
474 if (c != quotes)
475 continue;
476 quotes = 0;
477 *p++ = '\0';
478 p = skip_spaces_until_newline(p);
479 c = *p;
480 if (c && !strchr(",;\n#}", c))
481 return xbc_parse_error("No value delimiter", p);
482 if (*p)
483 p++;
484 break;
485 }
486 if (strchr(",;\n#}", c)) {
487 v = strim(v);
488 *p++ = '\0';
489 break;
490 }
491 }
492 if (quotes)
493 return xbc_parse_error("No closing quotes", p);
494 if (c == '#') {
495 p = skip_comment(p);
496 c = '\n'; /* A comment must be treated as a newline */
497 }
498 *__n = p;
499 *__v = v;
500
501 return c;
502}
503
504static int __init xbc_parse_array(char **__v)
505{
506 struct xbc_node *node;
507 char *next;
508 int c = 0;
509
510 do {
511 c = __xbc_parse_value(__v, &next);
512 if (c < 0)
513 return c;
514
515 node = xbc_add_sibling(*__v, XBC_VALUE);
516 if (!node)
517 return -ENOMEM;
518 *__v = next;
519 } while (c == ',');
520 node->next = 0;
521
522 return c;
523}
524
525static inline __init
526struct xbc_node *find_match_node(struct xbc_node *node, char *k)
527{
528 while (node) {
529 if (!strcmp(xbc_node_get_data(node), k))
530 break;
531 node = xbc_node_get_next(node);
532 }
533 return node;
534}
535
536static int __init __xbc_add_key(char *k)
537{
Masami Hiramatsua24d2862020-02-20 21:19:12 +0900538 struct xbc_node *node, *child;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900539
540 if (!xbc_valid_keyword(k))
541 return xbc_parse_error("Invalid keyword", k);
542
543 if (unlikely(xbc_node_num == 0))
544 goto add_node;
545
546 if (!last_parent) /* the first level */
547 node = find_match_node(xbc_nodes, k);
Masami Hiramatsua24d2862020-02-20 21:19:12 +0900548 else {
549 child = xbc_node_get_child(last_parent);
550 if (child && xbc_node_is_value(child))
551 return xbc_parse_error("Subkey is mixed with value", k);
552 node = find_match_node(child, k);
553 }
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900554
555 if (node)
556 last_parent = node;
557 else {
558add_node:
559 node = xbc_add_child(k, XBC_KEY);
560 if (!node)
561 return -ENOMEM;
562 }
563 return 0;
564}
565
566static int __init __xbc_parse_keys(char *k)
567{
568 char *p;
569 int ret;
570
571 k = strim(k);
572 while ((p = strchr(k, '.'))) {
573 *p++ = '\0';
574 ret = __xbc_add_key(k);
575 if (ret)
576 return ret;
577 k = p;
578 }
579
580 return __xbc_add_key(k);
581}
582
Masami Hiramatsu5f811c52020-02-21 17:13:52 +0900583static int __init xbc_parse_kv(char **k, char *v, int op)
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900584{
585 struct xbc_node *prev_parent = last_parent;
Masami Hiramatsu4e4694d2020-02-21 17:13:42 +0900586 struct xbc_node *child;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900587 char *next;
588 int c, ret;
589
590 ret = __xbc_parse_keys(*k);
591 if (ret)
592 return ret;
593
Masami Hiramatsua24d2862020-02-20 21:19:12 +0900594 child = xbc_node_get_child(last_parent);
Masami Hiramatsu4e4694d2020-02-21 17:13:42 +0900595 if (child) {
596 if (xbc_node_is_key(child))
597 return xbc_parse_error("Value is mixed with subkey", v);
Masami Hiramatsu5f811c52020-02-21 17:13:52 +0900598 else if (op == '=')
Masami Hiramatsu4e4694d2020-02-21 17:13:42 +0900599 return xbc_parse_error("Value is redefined", v);
600 }
Masami Hiramatsua24d2862020-02-20 21:19:12 +0900601
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900602 c = __xbc_parse_value(&v, &next);
603 if (c < 0)
604 return c;
605
Masami Hiramatsu4e4694d2020-02-21 17:13:42 +0900606 if (!xbc_add_sibling(v, XBC_VALUE))
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900607 return -ENOMEM;
608
609 if (c == ',') { /* Array */
610 c = xbc_parse_array(&next);
611 if (c < 0)
612 return c;
613 }
614
615 last_parent = prev_parent;
616
617 if (c == '}') {
618 ret = __xbc_close_brace(next - 1);
619 if (ret < 0)
620 return ret;
621 }
622
623 *k = next;
624
625 return 0;
626}
627
628static int __init xbc_parse_key(char **k, char *n)
629{
630 struct xbc_node *prev_parent = last_parent;
631 int ret;
632
633 *k = strim(*k);
634 if (**k != '\0') {
635 ret = __xbc_parse_keys(*k);
636 if (ret)
637 return ret;
638 last_parent = prev_parent;
639 }
640 *k = n;
641
642 return 0;
643}
644
645static int __init xbc_open_brace(char **k, char *n)
646{
647 int ret;
648
649 ret = __xbc_parse_keys(*k);
650 if (ret)
651 return ret;
652 *k = n;
653
654 return __xbc_open_brace();
655}
656
657static int __init xbc_close_brace(char **k, char *n)
658{
659 int ret;
660
661 ret = xbc_parse_key(k, n);
662 if (ret)
663 return ret;
664 /* k is updated in xbc_parse_key() */
665
666 return __xbc_close_brace(n - 1);
667}
668
669static int __init xbc_verify_tree(void)
670{
671 int i, depth, len, wlen;
672 struct xbc_node *n, *m;
673
674 /* Empty tree */
Masami Hiramatsu597c0e32020-02-05 22:50:04 +0900675 if (xbc_node_num == 0) {
676 xbc_parse_error("Empty config", xbc_data);
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900677 return -ENOENT;
Masami Hiramatsu597c0e32020-02-05 22:50:04 +0900678 }
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900679
680 for (i = 0; i < xbc_node_num; i++) {
681 if (xbc_nodes[i].next > xbc_node_num) {
682 return xbc_parse_error("No closing brace",
683 xbc_node_get_data(xbc_nodes + i));
684 }
685 }
686
687 /* Key tree limitation check */
688 n = &xbc_nodes[0];
689 depth = 1;
690 len = 0;
691
692 while (n) {
693 wlen = strlen(xbc_node_get_data(n)) + 1;
694 len += wlen;
695 if (len > XBC_KEYLEN_MAX)
696 return xbc_parse_error("Too long key length",
697 xbc_node_get_data(n));
698
699 m = xbc_node_get_child(n);
700 if (m && xbc_node_is_key(m)) {
701 n = m;
702 depth++;
703 if (depth > XBC_DEPTH_MAX)
704 return xbc_parse_error("Too many key words",
705 xbc_node_get_data(n));
706 continue;
707 }
708 len -= wlen;
709 m = xbc_node_get_next(n);
710 while (!m) {
711 n = xbc_node_get_parent(n);
712 if (!n)
713 break;
714 len -= strlen(xbc_node_get_data(n)) + 1;
715 depth--;
716 m = xbc_node_get_next(n);
717 }
718 n = m;
719 }
720
721 return 0;
722}
723
724/**
725 * xbc_destroy_all() - Clean up all parsed bootconfig
726 *
727 * This clears all data structures of parsed bootconfig on memory.
728 * If you need to reuse xbc_init() with new boot config, you can
729 * use this.
730 */
731void __init xbc_destroy_all(void)
732{
733 xbc_data = NULL;
734 xbc_data_size = 0;
735 xbc_node_num = 0;
Masami Hiramatsua91e4f12020-02-07 23:28:17 +0900736 memblock_free(__pa(xbc_nodes), sizeof(struct xbc_node) * XBC_NODE_MAX);
737 xbc_nodes = NULL;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900738}
739
740/**
741 * xbc_init() - Parse given XBC file and build XBC internal tree
742 * @buf: boot config text
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900743 * @emsg: A pointer of const char * to store the error message
744 * @epos: A pointer of int to store the error position
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900745 *
746 * This parses the boot config text in @buf. @buf must be a
747 * null terminated string and smaller than XBC_DATA_MAX.
Masami Hiramatsu0f0d0a72020-02-05 22:50:13 +0900748 * Return the number of stored nodes (>0) if succeeded, or -errno
749 * if there is any error.
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900750 * In error cases, @emsg will be updated with an error message and
751 * @epos will be updated with the error position which is the byte offset
752 * of @buf. If the error is not a parser error, @epos will be -1.
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900753 */
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900754int __init xbc_init(char *buf, const char **emsg, int *epos)
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900755{
756 char *p, *q;
757 int ret, c;
758
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900759 if (epos)
760 *epos = -1;
761
Masami Hiramatsu597c0e32020-02-05 22:50:04 +0900762 if (xbc_data) {
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900763 if (emsg)
764 *emsg = "Bootconfig is already initialized";
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900765 return -EBUSY;
Masami Hiramatsu597c0e32020-02-05 22:50:04 +0900766 }
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900767
768 ret = strlen(buf);
Masami Hiramatsu597c0e32020-02-05 22:50:04 +0900769 if (ret > XBC_DATA_MAX - 1 || ret == 0) {
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900770 if (emsg)
771 *emsg = ret ? "Config data is too big" :
772 "Config data is empty";
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900773 return -ERANGE;
Masami Hiramatsu597c0e32020-02-05 22:50:04 +0900774 }
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900775
Masami Hiramatsua91e4f12020-02-07 23:28:17 +0900776 xbc_nodes = memblock_alloc(sizeof(struct xbc_node) * XBC_NODE_MAX,
777 SMP_CACHE_BYTES);
778 if (!xbc_nodes) {
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900779 if (emsg)
780 *emsg = "Failed to allocate bootconfig nodes";
Masami Hiramatsua91e4f12020-02-07 23:28:17 +0900781 return -ENOMEM;
782 }
783 memset(xbc_nodes, 0, sizeof(struct xbc_node) * XBC_NODE_MAX);
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900784 xbc_data = buf;
785 xbc_data_size = ret + 1;
786 last_parent = NULL;
787
788 p = buf;
789 do {
Masami Hiramatsu5f811c52020-02-21 17:13:52 +0900790 q = strpbrk(p, "{}=+;\n#");
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900791 if (!q) {
792 p = skip_spaces(p);
793 if (*p != '\0')
794 ret = xbc_parse_error("No delimiter", p);
795 break;
796 }
797
798 c = *q;
799 *q++ = '\0';
800 switch (c) {
Masami Hiramatsu5f811c52020-02-21 17:13:52 +0900801 case '+':
802 if (*q++ != '=') {
803 ret = xbc_parse_error("Wrong '+' operator",
804 q - 2);
805 break;
806 }
807 /* Fall through */
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900808 case '=':
Masami Hiramatsu5f811c52020-02-21 17:13:52 +0900809 ret = xbc_parse_kv(&p, q, c);
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900810 break;
811 case '{':
812 ret = xbc_open_brace(&p, q);
813 break;
814 case '#':
815 q = skip_comment(q);
816 /* fall through */
817 case ';':
818 case '\n':
819 ret = xbc_parse_key(&p, q);
820 break;
821 case '}':
822 ret = xbc_close_brace(&p, q);
823 break;
824 }
825 } while (!ret);
826
827 if (!ret)
828 ret = xbc_verify_tree();
829
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900830 if (ret < 0) {
831 if (epos)
832 *epos = xbc_err_pos;
833 if (emsg)
834 *emsg = xbc_err_msg;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900835 xbc_destroy_all();
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900836 } else
Masami Hiramatsu0f0d0a72020-02-05 22:50:13 +0900837 ret = xbc_node_num;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900838
839 return ret;
840}
841
842/**
843 * xbc_debug_dump() - Dump current XBC node list
844 *
845 * Dump the current XBC node list on printk buffer for debug.
846 */
847void __init xbc_debug_dump(void)
848{
849 int i;
850
851 for (i = 0; i < xbc_node_num; i++) {
852 pr_debug("[%d] %s (%s) .next=%d, .child=%d .parent=%d\n", i,
853 xbc_node_get_data(xbc_nodes + i),
854 xbc_node_is_value(xbc_nodes + i) ? "value" : "key",
855 xbc_nodes[i].next, xbc_nodes[i].child,
856 xbc_nodes[i].parent);
857 }
858}