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 */
26 extern __visible const char embedded_bootconfig_data[];
27 extern __visible const char embedded_bootconfig_data_end[];
28
xbc_get_embedded_bootconfig(size_t * size)29 const 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
46 static struct xbc_node *xbc_nodes __initdata;
47 static int xbc_node_num __initdata;
48 static char *xbc_data __initdata;
49 static size_t xbc_data_size __initdata;
50 static struct xbc_node *last_parent __initdata;
51 static const char *xbc_err_msg __initdata;
52 static int xbc_err_pos __initdata;
53 static int open_brace[XBC_DEPTH_MAX] __initdata;
54 static int brace_index __initdata;
55
56 #ifdef __KERNEL__
xbc_alloc_mem(size_t size)57 static inline void * __init xbc_alloc_mem(size_t size)
58 {
59 return memblock_alloc(size, SMP_CACHE_BYTES);
60 }
61
xbc_free_mem(void * addr,size_t size,bool early)62 static 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
xbc_alloc_mem(size_t size)72 static inline void *xbc_alloc_mem(size_t size)
73 {
74 return malloc(size);
75 }
76
xbc_free_mem(void * addr,size_t size,bool early)77 static 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 */
xbc_get_info(int * node_size,size_t * data_size)91 int __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
xbc_parse_error(const char * msg,const char * p)103 static 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 */
xbc_root_node(void)117 struct 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 */
xbc_node_index(struct xbc_node * node)131 int __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 */
xbc_node_get_parent(struct xbc_node * node)143 struct 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 */
xbc_node_get_child(struct xbc_node * node)155 struct 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 */
xbc_node_get_next(struct xbc_node * node)169 struct 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 */
xbc_node_get_data(struct xbc_node * node)181 const 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
191 static bool __init
xbc_node_match_prefix(struct xbc_node * node,const char ** prefix)192 xbc_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 */
219 struct xbc_node * __init
xbc_node_find_subkey(struct xbc_node * parent,const char * key)220 xbc_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 */
256 const char * __init
xbc_node_find_value(struct xbc_node * parent,const char * key,struct xbc_node ** vnode)257 xbc_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 */
xbc_node_compose_key_after(struct xbc_node * root,struct xbc_node * node,char * buf,size_t size)291 int __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 */
xbc_node_find_next_leaf(struct xbc_node * root,struct xbc_node * node)340 struct 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
374 found:
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 */
xbc_node_find_next_key_value(struct xbc_node * root,struct xbc_node ** leaf)392 const 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
xbc_init_node(struct xbc_node * node,char * data,uint32_t flag)410 static 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
xbc_add_node(char * data,uint32_t flag)424 static 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
xbc_last_sibling(struct xbc_node * node)438 static 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
xbc_last_child(struct xbc_node * node)446 static 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
__xbc_add_sibling(char * data,uint32_t flag,bool head)454 static 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
xbc_add_sibling(char * data,uint32_t flag)481 static inline struct xbc_node * __init xbc_add_sibling(char *data, uint32_t flag)
482 {
483 return __xbc_add_sibling(data, flag, false);
484 }
485
xbc_add_head_sibling(char * data,uint32_t flag)486 static 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
xbc_add_child(char * data,uint32_t flag)491 static 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
xbc_valid_keyword(char * key)501 static 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
skip_comment(char * p)512 static 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
skip_spaces_until_newline(char * p)525 static char *skip_spaces_until_newline(char *p)
526 {
527 while (isspace(*p) && *p != '\n')
528 p++;
529 return p;
530 }
531
__xbc_open_brace(char * p)532 static int __init __xbc_open_brace(char *p)
533 {
534 /* Push the last key as open brace */
535 if (brace_index >= XBC_DEPTH_MAX)
536 return xbc_parse_error("Exceed max depth of braces", p);
537 open_brace[brace_index++] = xbc_node_index(last_parent);
538
539 return 0;
540 }
541
__xbc_close_brace(char * p)542 static 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 * *@__v must point real value string. (not including spaces before value.)
561 */
__xbc_parse_value(char ** __v,char ** __n)562 static int __init __xbc_parse_value(char **__v, char **__n)
563 {
564 char *p, *v = *__v;
565 int c, quotes = 0;
566
567 if (*v == '"' || *v == '\'') {
568 quotes = *v;
569 v++;
570 }
571 p = v - 1;
572 while ((c = *++p)) {
573 if (!isprint(c) && !isspace(c))
574 return xbc_parse_error("Non printable value", p);
575 if (quotes) {
576 if (c != quotes)
577 continue;
578 quotes = 0;
579 *p++ = '\0';
580 p = skip_spaces_until_newline(p);
581 c = *p;
582 if (c && !strchr(",;\n#}", c))
583 return xbc_parse_error("No value delimiter", p);
584 if (*p)
585 p++;
586 break;
587 }
588 if (strchr(",;\n#}", c)) {
589 *p++ = '\0';
590 v = strim(v);
591 break;
592 }
593 }
594 if (quotes)
595 return xbc_parse_error("No closing quotes", p);
596 if (c == '#') {
597 p = skip_comment(p);
598 c = '\n'; /* A comment must be treated as a newline */
599 }
600 *__n = p;
601 *__v = v;
602
603 return c;
604 }
605
xbc_parse_array(char ** __v)606 static int __init xbc_parse_array(char **__v)
607 {
608 struct xbc_node *node;
609 char *next;
610 int c = 0;
611
612 if (last_parent->child)
613 last_parent = xbc_node_get_child(last_parent);
614
615 do {
616 /* Search the next array value beyond comments and empty lines */
617 next = skip_spaces(*__v);
618 while (*next == '#') {
619 next = skip_comment(next);
620 next = skip_spaces(next);
621 }
622 *__v = next;
623 c = __xbc_parse_value(__v, &next);
624 if (c < 0)
625 return c;
626
627 node = xbc_add_child(*__v, XBC_VALUE);
628 if (!node)
629 return -ENOMEM;
630 *__v = next;
631 } while (c == ',');
632 node->child = 0;
633
634 return c;
635 }
636
637 static inline __init
find_match_node(struct xbc_node * node,char * k)638 struct xbc_node *find_match_node(struct xbc_node *node, char *k)
639 {
640 while (node) {
641 if (!strcmp(xbc_node_get_data(node), k))
642 break;
643 node = xbc_node_get_next(node);
644 }
645 return node;
646 }
647
__xbc_add_key(char * k)648 static int __init __xbc_add_key(char *k)
649 {
650 struct xbc_node *node, *child;
651
652 if (!xbc_valid_keyword(k))
653 return xbc_parse_error("Invalid keyword", k);
654
655 if (unlikely(xbc_node_num == 0))
656 goto add_node;
657
658 if (!last_parent) /* the first level */
659 node = find_match_node(xbc_nodes, k);
660 else {
661 child = xbc_node_get_child(last_parent);
662 /* Since the value node is the first child, skip it. */
663 if (child && xbc_node_is_value(child))
664 child = xbc_node_get_next(child);
665 node = find_match_node(child, k);
666 }
667
668 if (node)
669 last_parent = node;
670 else {
671 add_node:
672 node = xbc_add_child(k, XBC_KEY);
673 if (!node)
674 return -ENOMEM;
675 }
676 return 0;
677 }
678
__xbc_parse_keys(char * k)679 static int __init __xbc_parse_keys(char *k)
680 {
681 char *p;
682 int ret;
683
684 k = strim(k);
685 while ((p = strchr(k, '.'))) {
686 *p++ = '\0';
687 ret = __xbc_add_key(k);
688 if (ret)
689 return ret;
690 k = p;
691 }
692
693 return __xbc_add_key(k);
694 }
695
xbc_parse_kv(char ** k,char * v,int op)696 static int __init xbc_parse_kv(char **k, char *v, int op)
697 {
698 struct xbc_node *prev_parent = last_parent;
699 struct xbc_node *child;
700 char *next;
701 int c, ret;
702
703 ret = __xbc_parse_keys(*k);
704 if (ret)
705 return ret;
706
707 v = skip_spaces_until_newline(v);
708 /* If there is a comment, this has an empty value. */
709 if (*v == '#') {
710 next = skip_comment(v);
711 *v = '\0';
712 c = '\n';
713 } else {
714 c = __xbc_parse_value(&v, &next);
715 if (c < 0)
716 return c;
717 }
718
719 child = xbc_node_get_child(last_parent);
720 if (child && xbc_node_is_value(child)) {
721 if (op == '=')
722 return xbc_parse_error("Value is redefined", v);
723 if (op == ':') {
724 unsigned short nidx = child->next;
725
726 if (xbc_init_node(child, v, XBC_VALUE) < 0)
727 return xbc_parse_error("Failed to override value", v);
728 child->next = nidx; /* keep subkeys */
729 goto array;
730 }
731 /* op must be '+' */
732 last_parent = xbc_last_child(child);
733 }
734 /* The value node should always be the first child */
735 if (!xbc_add_head_sibling(v, XBC_VALUE))
736 return -ENOMEM;
737
738 array:
739 if (c == ',') { /* Array */
740 c = xbc_parse_array(&next);
741 if (c < 0)
742 return c;
743 }
744
745 last_parent = prev_parent;
746
747 if (c == '}') {
748 ret = __xbc_close_brace(next - 1);
749 if (ret < 0)
750 return ret;
751 }
752
753 *k = next;
754
755 return 0;
756 }
757
xbc_parse_key(char ** k,char * n)758 static int __init xbc_parse_key(char **k, char *n)
759 {
760 struct xbc_node *prev_parent = last_parent;
761 int ret;
762
763 *k = strim(*k);
764 if (**k != '\0') {
765 ret = __xbc_parse_keys(*k);
766 if (ret)
767 return ret;
768 last_parent = prev_parent;
769 }
770 *k = n;
771
772 return 0;
773 }
774
xbc_open_brace(char ** k,char * n)775 static int __init xbc_open_brace(char **k, char *n)
776 {
777 int ret;
778
779 ret = __xbc_parse_keys(*k);
780 if (ret)
781 return ret;
782 *k = n;
783
784 return __xbc_open_brace(n - 1);
785 }
786
xbc_close_brace(char ** k,char * n)787 static int __init xbc_close_brace(char **k, char *n)
788 {
789 int ret;
790
791 ret = xbc_parse_key(k, n);
792 if (ret)
793 return ret;
794 /* k is updated in xbc_parse_key() */
795
796 return __xbc_close_brace(n - 1);
797 }
798
xbc_verify_tree(void)799 static int __init xbc_verify_tree(void)
800 {
801 int i, depth, len, wlen;
802 struct xbc_node *n, *m;
803
804 /* Brace closing */
805 if (brace_index) {
806 n = &xbc_nodes[open_brace[brace_index - 1]];
807 return xbc_parse_error("Brace is not closed",
808 xbc_node_get_data(n));
809 }
810
811 /* Empty tree */
812 if (xbc_node_num == 0) {
813 xbc_parse_error("Empty config", xbc_data);
814 return -ENOENT;
815 }
816
817 for (i = 0; i < xbc_node_num; i++) {
818 if (xbc_nodes[i].next > xbc_node_num) {
819 return xbc_parse_error("No closing brace",
820 xbc_node_get_data(xbc_nodes + i));
821 }
822 }
823
824 /* Key tree limitation check */
825 n = &xbc_nodes[0];
826 depth = 1;
827 len = 0;
828
829 while (n) {
830 wlen = strlen(xbc_node_get_data(n)) + 1;
831 len += wlen;
832 if (len > XBC_KEYLEN_MAX)
833 return xbc_parse_error("Too long key length",
834 xbc_node_get_data(n));
835
836 m = xbc_node_get_child(n);
837 if (m && xbc_node_is_key(m)) {
838 n = m;
839 depth++;
840 if (depth > XBC_DEPTH_MAX)
841 return xbc_parse_error("Too many key words",
842 xbc_node_get_data(n));
843 continue;
844 }
845 len -= wlen;
846 m = xbc_node_get_next(n);
847 while (!m) {
848 n = xbc_node_get_parent(n);
849 if (!n)
850 break;
851 len -= strlen(xbc_node_get_data(n)) + 1;
852 depth--;
853 m = xbc_node_get_next(n);
854 }
855 n = m;
856 }
857
858 return 0;
859 }
860
861 /* Need to setup xbc_data and xbc_nodes before call this. */
xbc_parse_tree(void)862 static int __init xbc_parse_tree(void)
863 {
864 char *p, *q;
865 int ret = 0, c;
866
867 last_parent = NULL;
868 p = xbc_data;
869 do {
870 q = strpbrk(p, "{}=+;:\n#");
871 if (!q) {
872 p = skip_spaces(p);
873 if (*p != '\0')
874 ret = xbc_parse_error("No delimiter", p);
875 break;
876 }
877
878 c = *q;
879 *q++ = '\0';
880 switch (c) {
881 case ':':
882 case '+':
883 if (*q++ != '=') {
884 ret = xbc_parse_error(c == '+' ?
885 "Wrong '+' operator" :
886 "Wrong ':' operator",
887 q - 2);
888 break;
889 }
890 fallthrough;
891 case '=':
892 ret = xbc_parse_kv(&p, q, c);
893 break;
894 case '{':
895 ret = xbc_open_brace(&p, q);
896 break;
897 case '#':
898 q = skip_comment(q);
899 fallthrough;
900 case ';':
901 case '\n':
902 ret = xbc_parse_key(&p, q);
903 break;
904 case '}':
905 ret = xbc_close_brace(&p, q);
906 break;
907 }
908 } while (!ret);
909
910 return ret;
911 }
912
913 /**
914 * _xbc_exit() - Clean up all parsed bootconfig
915 * @early: Set true if this is called before budy system is initialized.
916 *
917 * This clears all data structures of parsed bootconfig on memory.
918 * If you need to reuse xbc_init() with new boot config, you can
919 * use this.
920 */
_xbc_exit(bool early)921 void __init _xbc_exit(bool early)
922 {
923 xbc_free_mem(xbc_data, xbc_data_size, early);
924 xbc_data = NULL;
925 xbc_data_size = 0;
926 xbc_node_num = 0;
927 xbc_free_mem(xbc_nodes, sizeof(struct xbc_node) * XBC_NODE_MAX, early);
928 xbc_nodes = NULL;
929 brace_index = 0;
930 }
931
932 /**
933 * xbc_init() - Parse given XBC file and build XBC internal tree
934 * @data: The boot config text original data
935 * @size: The size of @data
936 * @emsg: A pointer of const char * to store the error message
937 * @epos: A pointer of int to store the error position
938 *
939 * This parses the boot config text in @data. @size must be smaller
940 * than XBC_DATA_MAX.
941 * Return the number of stored nodes (>0) if succeeded, or -errno
942 * if there is any error.
943 * In error cases, @emsg will be updated with an error message and
944 * @epos will be updated with the error position which is the byte offset
945 * of @buf. If the error is not a parser error, @epos will be -1.
946 */
xbc_init(const char * data,size_t size,const char ** emsg,int * epos)947 int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos)
948 {
949 int ret;
950
951 if (epos)
952 *epos = -1;
953
954 if (xbc_data) {
955 if (emsg)
956 *emsg = "Bootconfig is already initialized";
957 return -EBUSY;
958 }
959 if (size > XBC_DATA_MAX || size == 0) {
960 if (emsg)
961 *emsg = size ? "Config data is too big" :
962 "Config data is empty";
963 return -ERANGE;
964 }
965
966 xbc_data = xbc_alloc_mem(size + 1);
967 if (!xbc_data) {
968 if (emsg)
969 *emsg = "Failed to allocate bootconfig data";
970 return -ENOMEM;
971 }
972 memcpy(xbc_data, data, size);
973 xbc_data[size] = '\0';
974 xbc_data_size = size + 1;
975
976 xbc_nodes = xbc_alloc_mem(sizeof(struct xbc_node) * XBC_NODE_MAX);
977 if (!xbc_nodes) {
978 if (emsg)
979 *emsg = "Failed to allocate bootconfig nodes";
980 _xbc_exit(true);
981 return -ENOMEM;
982 }
983 memset(xbc_nodes, 0, sizeof(struct xbc_node) * XBC_NODE_MAX);
984
985 ret = xbc_parse_tree();
986 if (!ret)
987 ret = xbc_verify_tree();
988
989 if (ret < 0) {
990 if (epos)
991 *epos = xbc_err_pos;
992 if (emsg)
993 *emsg = xbc_err_msg;
994 _xbc_exit(true);
995 } else
996 ret = xbc_node_num;
997
998 return ret;
999 }
1000