tty: serial: ifx6x60: Convert to GPIO descriptors
[linux/fpc-iii.git] / lib / bootconfig.c
blobec3ce7fd299f6f36ca3a07897b45bf876e0c2db2
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Extra Boot Config
4 * Masami Hiramatsu <mhiramat@kernel.org>
5 */
7 #define pr_fmt(fmt) "bootconfig: " fmt
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>
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).
27 static struct xbc_node *xbc_nodes __initdata;
28 static int xbc_node_num __initdata;
29 static char *xbc_data __initdata;
30 static size_t xbc_data_size __initdata;
31 static struct xbc_node *last_parent __initdata;
33 static int __init xbc_parse_error(const char *msg, const char *p)
35 int pos = p - xbc_data;
37 pr_err("Parse error at pos %d: %s\n", pos, msg);
38 return -EINVAL;
41 /**
42 * xbc_root_node() - Get the root node of extended boot config
44 * Return the address of root node of extended boot config. If the
45 * extended boot config is not initiized, return NULL.
47 struct xbc_node * __init xbc_root_node(void)
49 if (unlikely(!xbc_data))
50 return NULL;
52 return xbc_nodes;
55 /**
56 * xbc_node_index() - Get the index of XBC node
57 * @node: A target node of getting index.
59 * Return the index number of @node in XBC node list.
61 int __init xbc_node_index(struct xbc_node *node)
63 return node - &xbc_nodes[0];
66 /**
67 * xbc_node_get_parent() - Get the parent XBC node
68 * @node: An XBC node.
70 * Return the parent node of @node. If the node is top node of the tree,
71 * return NULL.
73 struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node)
75 return node->parent == XBC_NODE_MAX ? NULL : &xbc_nodes[node->parent];
78 /**
79 * xbc_node_get_child() - Get the child XBC node
80 * @node: An XBC node.
82 * Return the first child node of @node. If the node has no child, return
83 * NULL.
85 struct xbc_node * __init xbc_node_get_child(struct xbc_node *node)
87 return node->child ? &xbc_nodes[node->child] : NULL;
90 /**
91 * xbc_node_get_next() - Get the next sibling XBC node
92 * @node: An XBC node.
94 * Return the NEXT sibling node of @node. If the node has no next sibling,
95 * return NULL. Note that even if this returns NULL, it doesn't mean @node
96 * has no siblings. (You also has to check whether the parent's child node
97 * is @node or not.)
99 struct xbc_node * __init xbc_node_get_next(struct xbc_node *node)
101 return node->next ? &xbc_nodes[node->next] : NULL;
105 * xbc_node_get_data() - Get the data of XBC node
106 * @node: An XBC node.
108 * Return the data (which is always a null terminated string) of @node.
109 * If the node has invalid data, warn and return NULL.
111 const char * __init xbc_node_get_data(struct xbc_node *node)
113 int offset = node->data & ~XBC_VALUE;
115 if (WARN_ON(offset >= xbc_data_size))
116 return NULL;
118 return xbc_data + offset;
121 static bool __init
122 xbc_node_match_prefix(struct xbc_node *node, const char **prefix)
124 const char *p = xbc_node_get_data(node);
125 int len = strlen(p);
127 if (strncmp(*prefix, p, len))
128 return false;
130 p = *prefix + len;
131 if (*p == '.')
132 p++;
133 else if (*p != '\0')
134 return false;
135 *prefix = p;
137 return true;
141 * xbc_node_find_child() - Find a child node which matches given key
142 * @parent: An XBC node.
143 * @key: A key string.
145 * Search a node under @parent which matches @key. The @key can contain
146 * several words jointed with '.'. If @parent is NULL, this searches the
147 * node from whole tree. Return NULL if no node is matched.
149 struct xbc_node * __init
150 xbc_node_find_child(struct xbc_node *parent, const char *key)
152 struct xbc_node *node;
154 if (parent)
155 node = xbc_node_get_child(parent);
156 else
157 node = xbc_root_node();
159 while (node && xbc_node_is_key(node)) {
160 if (!xbc_node_match_prefix(node, &key))
161 node = xbc_node_get_next(node);
162 else if (*key != '\0')
163 node = xbc_node_get_child(node);
164 else
165 break;
168 return node;
172 * xbc_node_find_value() - Find a value node which matches given key
173 * @parent: An XBC node.
174 * @key: A key string.
175 * @vnode: A container pointer of found XBC node.
177 * Search a value node under @parent whose (parent) key node matches @key,
178 * store it in *@vnode, and returns the value string.
179 * The @key can contain several words jointed with '.'. If @parent is NULL,
180 * this searches the node from whole tree. Return the value string if a
181 * matched key found, return NULL if no node is matched.
182 * Note that this returns 0-length string and stores NULL in *@vnode if the
183 * key has no value. And also it will return the value of the first entry if
184 * the value is an array.
186 const char * __init
187 xbc_node_find_value(struct xbc_node *parent, const char *key,
188 struct xbc_node **vnode)
190 struct xbc_node *node = xbc_node_find_child(parent, key);
192 if (!node || !xbc_node_is_key(node))
193 return NULL;
195 node = xbc_node_get_child(node);
196 if (node && !xbc_node_is_value(node))
197 return NULL;
199 if (vnode)
200 *vnode = node;
202 return node ? xbc_node_get_data(node) : "";
206 * xbc_node_compose_key_after() - Compose partial key string of the XBC node
207 * @root: Root XBC node
208 * @node: Target XBC node.
209 * @buf: A buffer to store the key.
210 * @size: The size of the @buf.
212 * Compose the partial key of the @node into @buf, which is starting right
213 * after @root (@root is not included.) If @root is NULL, this returns full
214 * key words of @node.
215 * Returns the total length of the key stored in @buf. Returns -EINVAL
216 * if @node is NULL or @root is not the ancestor of @node or @root is @node,
217 * or returns -ERANGE if the key depth is deeper than max depth.
218 * This is expected to be used with xbc_find_node() to list up all (child)
219 * keys under given key.
221 int __init xbc_node_compose_key_after(struct xbc_node *root,
222 struct xbc_node *node,
223 char *buf, size_t size)
225 u16 keys[XBC_DEPTH_MAX];
226 int depth = 0, ret = 0, total = 0;
228 if (!node || node == root)
229 return -EINVAL;
231 if (xbc_node_is_value(node))
232 node = xbc_node_get_parent(node);
234 while (node && node != root) {
235 keys[depth++] = xbc_node_index(node);
236 if (depth == XBC_DEPTH_MAX)
237 return -ERANGE;
238 node = xbc_node_get_parent(node);
240 if (!node && root)
241 return -EINVAL;
243 while (--depth >= 0) {
244 node = xbc_nodes + keys[depth];
245 ret = snprintf(buf, size, "%s%s", xbc_node_get_data(node),
246 depth ? "." : "");
247 if (ret < 0)
248 return ret;
249 if (ret > size) {
250 size = 0;
251 } else {
252 size -= ret;
253 buf += ret;
255 total += ret;
258 return total;
262 * xbc_node_find_next_leaf() - Find the next leaf node under given node
263 * @root: An XBC root node
264 * @node: An XBC node which starts from.
266 * Search the next leaf node (which means the terminal key node) of @node
267 * under @root node (including @root node itself).
268 * Return the next node or NULL if next leaf node is not found.
270 struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root,
271 struct xbc_node *node)
273 if (unlikely(!xbc_data))
274 return NULL;
276 if (!node) { /* First try */
277 node = root;
278 if (!node)
279 node = xbc_nodes;
280 } else {
281 if (node == root) /* @root was a leaf, no child node. */
282 return NULL;
284 while (!node->next) {
285 node = xbc_node_get_parent(node);
286 if (node == root)
287 return NULL;
288 /* User passed a node which is not uder parent */
289 if (WARN_ON(!node))
290 return NULL;
292 node = xbc_node_get_next(node);
295 while (node && !xbc_node_is_leaf(node))
296 node = xbc_node_get_child(node);
298 return node;
302 * xbc_node_find_next_key_value() - Find the next key-value pair nodes
303 * @root: An XBC root node
304 * @leaf: A container pointer of XBC node which starts from.
306 * Search the next leaf node (which means the terminal key node) of *@leaf
307 * under @root node. Returns the value and update *@leaf if next leaf node
308 * is found, or NULL if no next leaf node is found.
309 * Note that this returns 0-length string if the key has no value, or
310 * the value of the first entry if the value is an array.
312 const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
313 struct xbc_node **leaf)
315 /* tip must be passed */
316 if (WARN_ON(!leaf))
317 return NULL;
319 *leaf = xbc_node_find_next_leaf(root, *leaf);
320 if (!*leaf)
321 return NULL;
322 if ((*leaf)->child)
323 return xbc_node_get_data(xbc_node_get_child(*leaf));
324 else
325 return ""; /* No value key */
328 /* XBC parse and tree build */
330 static struct xbc_node * __init xbc_add_node(char *data, u32 flag)
332 struct xbc_node *node;
333 unsigned long offset;
335 if (xbc_node_num == XBC_NODE_MAX)
336 return NULL;
338 node = &xbc_nodes[xbc_node_num++];
339 offset = data - xbc_data;
340 node->data = (u16)offset;
341 if (WARN_ON(offset >= XBC_DATA_MAX))
342 return NULL;
343 node->data |= flag;
344 node->child = 0;
345 node->next = 0;
347 return node;
350 static inline __init struct xbc_node *xbc_last_sibling(struct xbc_node *node)
352 while (node->next)
353 node = xbc_node_get_next(node);
355 return node;
358 static struct xbc_node * __init xbc_add_sibling(char *data, u32 flag)
360 struct xbc_node *sib, *node = xbc_add_node(data, flag);
362 if (node) {
363 if (!last_parent) {
364 node->parent = XBC_NODE_MAX;
365 sib = xbc_last_sibling(xbc_nodes);
366 sib->next = xbc_node_index(node);
367 } else {
368 node->parent = xbc_node_index(last_parent);
369 if (!last_parent->child) {
370 last_parent->child = xbc_node_index(node);
371 } else {
372 sib = xbc_node_get_child(last_parent);
373 sib = xbc_last_sibling(sib);
374 sib->next = xbc_node_index(node);
377 } else
378 xbc_parse_error("Too many nodes", data);
380 return node;
383 static inline __init struct xbc_node *xbc_add_child(char *data, u32 flag)
385 struct xbc_node *node = xbc_add_sibling(data, flag);
387 if (node)
388 last_parent = node;
390 return node;
393 static inline __init bool xbc_valid_keyword(char *key)
395 if (key[0] == '\0')
396 return false;
398 while (isalnum(*key) || *key == '-' || *key == '_')
399 key++;
401 return *key == '\0';
404 static char *skip_comment(char *p)
406 char *ret;
408 ret = strchr(p, '\n');
409 if (!ret)
410 ret = p + strlen(p);
411 else
412 ret++;
414 return ret;
417 static char *skip_spaces_until_newline(char *p)
419 while (isspace(*p) && *p != '\n')
420 p++;
421 return p;
424 static int __init __xbc_open_brace(void)
426 /* Mark the last key as open brace */
427 last_parent->next = XBC_NODE_MAX;
429 return 0;
432 static int __init __xbc_close_brace(char *p)
434 struct xbc_node *node;
436 if (!last_parent || last_parent->next != XBC_NODE_MAX)
437 return xbc_parse_error("Unexpected closing brace", p);
439 node = last_parent;
440 node->next = 0;
441 do {
442 node = xbc_node_get_parent(node);
443 } while (node && node->next != XBC_NODE_MAX);
444 last_parent = node;
446 return 0;
450 * Return delimiter or error, no node added. As same as lib/cmdline.c,
451 * you can use " around spaces, but can't escape " for value.
453 static int __init __xbc_parse_value(char **__v, char **__n)
455 char *p, *v = *__v;
456 int c, quotes = 0;
458 v = skip_spaces(v);
459 while (*v == '#') {
460 v = skip_comment(v);
461 v = skip_spaces(v);
463 if (*v == '"' || *v == '\'') {
464 quotes = *v;
465 v++;
467 p = v - 1;
468 while ((c = *++p)) {
469 if (!isprint(c) && !isspace(c))
470 return xbc_parse_error("Non printable value", p);
471 if (quotes) {
472 if (c != quotes)
473 continue;
474 quotes = 0;
475 *p++ = '\0';
476 p = skip_spaces_until_newline(p);
477 c = *p;
478 if (c && !strchr(",;\n#}", c))
479 return xbc_parse_error("No value delimiter", p);
480 if (*p)
481 p++;
482 break;
484 if (strchr(",;\n#}", c)) {
485 v = strim(v);
486 *p++ = '\0';
487 break;
490 if (quotes)
491 return xbc_parse_error("No closing quotes", p);
492 if (c == '#') {
493 p = skip_comment(p);
494 c = '\n'; /* A comment must be treated as a newline */
496 *__n = p;
497 *__v = v;
499 return c;
502 static int __init xbc_parse_array(char **__v)
504 struct xbc_node *node;
505 char *next;
506 int c = 0;
508 do {
509 c = __xbc_parse_value(__v, &next);
510 if (c < 0)
511 return c;
513 node = xbc_add_sibling(*__v, XBC_VALUE);
514 if (!node)
515 return -ENOMEM;
516 *__v = next;
517 } while (c == ',');
518 node->next = 0;
520 return c;
523 static inline __init
524 struct xbc_node *find_match_node(struct xbc_node *node, char *k)
526 while (node) {
527 if (!strcmp(xbc_node_get_data(node), k))
528 break;
529 node = xbc_node_get_next(node);
531 return node;
534 static int __init __xbc_add_key(char *k)
536 struct xbc_node *node, *child;
538 if (!xbc_valid_keyword(k))
539 return xbc_parse_error("Invalid keyword", k);
541 if (unlikely(xbc_node_num == 0))
542 goto add_node;
544 if (!last_parent) /* the first level */
545 node = find_match_node(xbc_nodes, k);
546 else {
547 child = xbc_node_get_child(last_parent);
548 if (child && xbc_node_is_value(child))
549 return xbc_parse_error("Subkey is mixed with value", k);
550 node = find_match_node(child, k);
553 if (node)
554 last_parent = node;
555 else {
556 add_node:
557 node = xbc_add_child(k, XBC_KEY);
558 if (!node)
559 return -ENOMEM;
561 return 0;
564 static int __init __xbc_parse_keys(char *k)
566 char *p;
567 int ret;
569 k = strim(k);
570 while ((p = strchr(k, '.'))) {
571 *p++ = '\0';
572 ret = __xbc_add_key(k);
573 if (ret)
574 return ret;
575 k = p;
578 return __xbc_add_key(k);
581 static int __init xbc_parse_kv(char **k, char *v, int op)
583 struct xbc_node *prev_parent = last_parent;
584 struct xbc_node *child;
585 char *next;
586 int c, ret;
588 ret = __xbc_parse_keys(*k);
589 if (ret)
590 return ret;
592 child = xbc_node_get_child(last_parent);
593 if (child) {
594 if (xbc_node_is_key(child))
595 return xbc_parse_error("Value is mixed with subkey", v);
596 else if (op == '=')
597 return xbc_parse_error("Value is redefined", v);
600 c = __xbc_parse_value(&v, &next);
601 if (c < 0)
602 return c;
604 if (!xbc_add_sibling(v, XBC_VALUE))
605 return -ENOMEM;
607 if (c == ',') { /* Array */
608 c = xbc_parse_array(&next);
609 if (c < 0)
610 return c;
613 last_parent = prev_parent;
615 if (c == '}') {
616 ret = __xbc_close_brace(next - 1);
617 if (ret < 0)
618 return ret;
621 *k = next;
623 return 0;
626 static int __init xbc_parse_key(char **k, char *n)
628 struct xbc_node *prev_parent = last_parent;
629 int ret;
631 *k = strim(*k);
632 if (**k != '\0') {
633 ret = __xbc_parse_keys(*k);
634 if (ret)
635 return ret;
636 last_parent = prev_parent;
638 *k = n;
640 return 0;
643 static int __init xbc_open_brace(char **k, char *n)
645 int ret;
647 ret = __xbc_parse_keys(*k);
648 if (ret)
649 return ret;
650 *k = n;
652 return __xbc_open_brace();
655 static int __init xbc_close_brace(char **k, char *n)
657 int ret;
659 ret = xbc_parse_key(k, n);
660 if (ret)
661 return ret;
662 /* k is updated in xbc_parse_key() */
664 return __xbc_close_brace(n - 1);
667 static int __init xbc_verify_tree(void)
669 int i, depth, len, wlen;
670 struct xbc_node *n, *m;
672 /* Empty tree */
673 if (xbc_node_num == 0) {
674 xbc_parse_error("Empty config", xbc_data);
675 return -ENOENT;
678 for (i = 0; i < xbc_node_num; i++) {
679 if (xbc_nodes[i].next > xbc_node_num) {
680 return xbc_parse_error("No closing brace",
681 xbc_node_get_data(xbc_nodes + i));
685 /* Key tree limitation check */
686 n = &xbc_nodes[0];
687 depth = 1;
688 len = 0;
690 while (n) {
691 wlen = strlen(xbc_node_get_data(n)) + 1;
692 len += wlen;
693 if (len > XBC_KEYLEN_MAX)
694 return xbc_parse_error("Too long key length",
695 xbc_node_get_data(n));
697 m = xbc_node_get_child(n);
698 if (m && xbc_node_is_key(m)) {
699 n = m;
700 depth++;
701 if (depth > XBC_DEPTH_MAX)
702 return xbc_parse_error("Too many key words",
703 xbc_node_get_data(n));
704 continue;
706 len -= wlen;
707 m = xbc_node_get_next(n);
708 while (!m) {
709 n = xbc_node_get_parent(n);
710 if (!n)
711 break;
712 len -= strlen(xbc_node_get_data(n)) + 1;
713 depth--;
714 m = xbc_node_get_next(n);
716 n = m;
719 return 0;
723 * xbc_destroy_all() - Clean up all parsed bootconfig
725 * This clears all data structures of parsed bootconfig on memory.
726 * If you need to reuse xbc_init() with new boot config, you can
727 * use this.
729 void __init xbc_destroy_all(void)
731 xbc_data = NULL;
732 xbc_data_size = 0;
733 xbc_node_num = 0;
734 memblock_free(__pa(xbc_nodes), sizeof(struct xbc_node) * XBC_NODE_MAX);
735 xbc_nodes = NULL;
739 * xbc_init() - Parse given XBC file and build XBC internal tree
740 * @buf: boot config text
742 * This parses the boot config text in @buf. @buf must be a
743 * null terminated string and smaller than XBC_DATA_MAX.
744 * Return the number of stored nodes (>0) if succeeded, or -errno
745 * if there is any error.
747 int __init xbc_init(char *buf)
749 char *p, *q;
750 int ret, c;
752 if (xbc_data) {
753 pr_err("Error: bootconfig is already initialized.\n");
754 return -EBUSY;
757 ret = strlen(buf);
758 if (ret > XBC_DATA_MAX - 1 || ret == 0) {
759 pr_err("Error: Config data is %s.\n",
760 ret ? "too big" : "empty");
761 return -ERANGE;
764 xbc_nodes = memblock_alloc(sizeof(struct xbc_node) * XBC_NODE_MAX,
765 SMP_CACHE_BYTES);
766 if (!xbc_nodes) {
767 pr_err("Failed to allocate memory for bootconfig nodes.\n");
768 return -ENOMEM;
770 memset(xbc_nodes, 0, sizeof(struct xbc_node) * XBC_NODE_MAX);
771 xbc_data = buf;
772 xbc_data_size = ret + 1;
773 last_parent = NULL;
775 p = buf;
776 do {
777 q = strpbrk(p, "{}=+;\n#");
778 if (!q) {
779 p = skip_spaces(p);
780 if (*p != '\0')
781 ret = xbc_parse_error("No delimiter", p);
782 break;
785 c = *q;
786 *q++ = '\0';
787 switch (c) {
788 case '+':
789 if (*q++ != '=') {
790 ret = xbc_parse_error("Wrong '+' operator",
791 q - 2);
792 break;
794 /* Fall through */
795 case '=':
796 ret = xbc_parse_kv(&p, q, c);
797 break;
798 case '{':
799 ret = xbc_open_brace(&p, q);
800 break;
801 case '#':
802 q = skip_comment(q);
803 /* fall through */
804 case ';':
805 case '\n':
806 ret = xbc_parse_key(&p, q);
807 break;
808 case '}':
809 ret = xbc_close_brace(&p, q);
810 break;
812 } while (!ret);
814 if (!ret)
815 ret = xbc_verify_tree();
817 if (ret < 0)
818 xbc_destroy_all();
819 else
820 ret = xbc_node_num;
822 return ret;
826 * xbc_debug_dump() - Dump current XBC node list
828 * Dump the current XBC node list on printk buffer for debug.
830 void __init xbc_debug_dump(void)
832 int i;
834 for (i = 0; i < xbc_node_num; i++) {
835 pr_debug("[%d] %s (%s) .next=%d, .child=%d .parent=%d\n", i,
836 xbc_node_get_data(xbc_nodes + i),
837 xbc_node_is_value(xbc_nodes + i) ? "value" : "key",
838 xbc_nodes[i].next, xbc_nodes[i].child,
839 xbc_nodes[i].parent);