blob: 74f3201ab8e59e6201c02b5e97839cb148c9c880 [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
Masami Hiramatsu4ee1b4c2021-09-17 19:03:16 +09007#ifdef __KERNEL__
Masami Hiramatsua91e4f12020-02-07 23:28:17 +09008#include <linux/bootconfig.h>
Masami Hiramatsu76db5a22020-01-11 01:03:32 +09009#include <linux/bug.h>
10#include <linux/ctype.h>
11#include <linux/errno.h>
12#include <linux/kernel.h>
Masami Hiramatsua91e4f12020-02-07 23:28:17 +090013#include <linux/memblock.h>
Masami Hiramatsu76db5a22020-01-11 01:03:32 +090014#include <linux/string.h>
Masami Hiramatsu4ee1b4c2021-09-17 19:03:16 +090015#else /* !__KERNEL__ */
16/*
17 * NOTE: This is only for tools/bootconfig, because tools/bootconfig will
18 * run the parser sanity test.
19 * This does NOT mean lib/bootconfig.c is available in the user space.
20 * However, if you change this file, please make sure the tools/bootconfig
21 * has no issue on building and running.
22 */
23#include <linux/bootconfig.h>
24#endif
Masami Hiramatsu76db5a22020-01-11 01:03:32 +090025
26/*
27 * Extra Boot Config (XBC) is given as tree-structured ascii text of
28 * key-value pairs on memory.
29 * xbc_parse() parses the text to build a simple tree. Each tree node is
30 * simply a key word or a value. A key node may have a next key node or/and
31 * a child node (both key and value). A value node may have a next value
32 * node (for array).
33 */
34
Masami Hiramatsua91e4f12020-02-07 23:28:17 +090035static struct xbc_node *xbc_nodes __initdata;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +090036static int xbc_node_num __initdata;
37static char *xbc_data __initdata;
38static size_t xbc_data_size __initdata;
39static struct xbc_node *last_parent __initdata;
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +090040static const char *xbc_err_msg __initdata;
41static int xbc_err_pos __initdata;
Masami Hiramatsuead1e19a2020-09-21 18:44:42 +090042static int open_brace[XBC_DEPTH_MAX] __initdata;
43static int brace_index __initdata;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +090044
Masami Hiramatsu4ee1b4c2021-09-17 19:03:16 +090045#ifdef __KERNEL__
Masami Hiramatsu4d1c92a2021-10-25 17:32:37 +090046static inline void * __init xbc_alloc_mem(size_t size)
Masami Hiramatsu4ee1b4c2021-09-17 19:03:16 +090047{
48 return memblock_alloc(size, SMP_CACHE_BYTES);
49}
50
Masami Hiramatsu4d1c92a2021-10-25 17:32:37 +090051static inline void __init xbc_free_mem(void *addr, size_t size)
Masami Hiramatsu4ee1b4c2021-09-17 19:03:16 +090052{
Linus Torvalds512b79312021-11-06 14:08:17 -070053 memblock_free(addr, size);
Masami Hiramatsu4ee1b4c2021-09-17 19:03:16 +090054}
55
56#else /* !__KERNEL__ */
57
58static inline void *xbc_alloc_mem(size_t size)
59{
60 return malloc(size);
61}
62
63static inline void xbc_free_mem(void *addr, size_t size)
64{
65 free(addr);
66}
67#endif
Masami Hiramatsue3062202021-09-16 15:23:29 +090068/**
69 * xbc_get_info() - Get the information of loaded boot config
Masami Hiramatsu010db092021-10-26 21:21:07 +090070 * @node_size: A pointer to store the number of nodes.
71 * @data_size: A pointer to store the size of bootconfig data.
Masami Hiramatsue3062202021-09-16 15:23:29 +090072 *
73 * Get the number of used nodes in @node_size if it is not NULL,
74 * and the size of bootconfig data in @data_size if it is not NULL.
75 * Return 0 if the boot config is initialized, or return -ENODEV.
76 */
77int __init xbc_get_info(int *node_size, size_t *data_size)
78{
79 if (!xbc_data)
80 return -ENODEV;
81
82 if (node_size)
83 *node_size = xbc_node_num;
84 if (data_size)
85 *data_size = xbc_data_size;
86 return 0;
87}
88
Masami Hiramatsu76db5a22020-01-11 01:03:32 +090089static int __init xbc_parse_error(const char *msg, const char *p)
90{
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +090091 xbc_err_msg = msg;
92 xbc_err_pos = (int)(p - xbc_data);
Masami Hiramatsu76db5a22020-01-11 01:03:32 +090093
Masami Hiramatsu76db5a22020-01-11 01:03:32 +090094 return -EINVAL;
95}
96
97/**
98 * xbc_root_node() - Get the root node of extended boot config
99 *
100 * Return the address of root node of extended boot config. If the
101 * extended boot config is not initiized, return NULL.
102 */
103struct xbc_node * __init xbc_root_node(void)
104{
105 if (unlikely(!xbc_data))
106 return NULL;
107
108 return xbc_nodes;
109}
110
111/**
112 * xbc_node_index() - Get the index of XBC node
113 * @node: A target node of getting index.
114 *
115 * Return the index number of @node in XBC node list.
116 */
117int __init xbc_node_index(struct xbc_node *node)
118{
119 return node - &xbc_nodes[0];
120}
121
122/**
123 * xbc_node_get_parent() - Get the parent XBC node
124 * @node: An XBC node.
125 *
126 * Return the parent node of @node. If the node is top node of the tree,
127 * return NULL.
128 */
129struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node)
130{
131 return node->parent == XBC_NODE_MAX ? NULL : &xbc_nodes[node->parent];
132}
133
134/**
135 * xbc_node_get_child() - Get the child XBC node
136 * @node: An XBC node.
137 *
138 * Return the first child node of @node. If the node has no child, return
139 * NULL.
140 */
141struct xbc_node * __init xbc_node_get_child(struct xbc_node *node)
142{
143 return node->child ? &xbc_nodes[node->child] : NULL;
144}
145
146/**
147 * xbc_node_get_next() - Get the next sibling XBC node
148 * @node: An XBC node.
149 *
150 * Return the NEXT sibling node of @node. If the node has no next sibling,
151 * return NULL. Note that even if this returns NULL, it doesn't mean @node
152 * has no siblings. (You also has to check whether the parent's child node
153 * is @node or not.)
154 */
155struct xbc_node * __init xbc_node_get_next(struct xbc_node *node)
156{
157 return node->next ? &xbc_nodes[node->next] : NULL;
158}
159
160/**
161 * xbc_node_get_data() - Get the data of XBC node
162 * @node: An XBC node.
163 *
164 * Return the data (which is always a null terminated string) of @node.
165 * If the node has invalid data, warn and return NULL.
166 */
167const char * __init xbc_node_get_data(struct xbc_node *node)
168{
169 int offset = node->data & ~XBC_VALUE;
170
171 if (WARN_ON(offset >= xbc_data_size))
172 return NULL;
173
174 return xbc_data + offset;
175}
176
177static bool __init
178xbc_node_match_prefix(struct xbc_node *node, const char **prefix)
179{
180 const char *p = xbc_node_get_data(node);
181 int len = strlen(p);
182
183 if (strncmp(*prefix, p, len))
184 return false;
185
186 p = *prefix + len;
187 if (*p == '.')
188 p++;
189 else if (*p != '\0')
190 return false;
191 *prefix = p;
192
193 return true;
194}
195
196/**
Masami Hiramatsu5dfe50b2021-09-09 22:36:38 +0900197 * xbc_node_find_subkey() - Find a subkey node which matches given key
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900198 * @parent: An XBC node.
199 * @key: A key string.
200 *
Masami Hiramatsu5dfe50b2021-09-09 22:36:38 +0900201 * Search a key node under @parent which matches @key. The @key can contain
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900202 * several words jointed with '.'. If @parent is NULL, this searches the
203 * node from whole tree. Return NULL if no node is matched.
204 */
205struct xbc_node * __init
Masami Hiramatsu5dfe50b2021-09-09 22:36:38 +0900206xbc_node_find_subkey(struct xbc_node *parent, const char *key)
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900207{
208 struct xbc_node *node;
209
210 if (parent)
Masami Hiramatsue5efaeb2021-06-02 17:19:07 +0900211 node = xbc_node_get_subkey(parent);
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900212 else
213 node = xbc_root_node();
214
215 while (node && xbc_node_is_key(node)) {
216 if (!xbc_node_match_prefix(node, &key))
217 node = xbc_node_get_next(node);
218 else if (*key != '\0')
Masami Hiramatsue5efaeb2021-06-02 17:19:07 +0900219 node = xbc_node_get_subkey(node);
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900220 else
221 break;
222 }
223
224 return node;
225}
226
227/**
228 * xbc_node_find_value() - Find a value node which matches given key
229 * @parent: An XBC node.
230 * @key: A key string.
231 * @vnode: A container pointer of found XBC node.
232 *
233 * Search a value node under @parent whose (parent) key node matches @key,
234 * store it in *@vnode, and returns the value string.
235 * The @key can contain several words jointed with '.'. If @parent is NULL,
236 * this searches the node from whole tree. Return the value string if a
237 * matched key found, return NULL if no node is matched.
238 * Note that this returns 0-length string and stores NULL in *@vnode if the
239 * key has no value. And also it will return the value of the first entry if
240 * the value is an array.
241 */
242const char * __init
243xbc_node_find_value(struct xbc_node *parent, const char *key,
244 struct xbc_node **vnode)
245{
Masami Hiramatsu5dfe50b2021-09-09 22:36:38 +0900246 struct xbc_node *node = xbc_node_find_subkey(parent, key);
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900247
248 if (!node || !xbc_node_is_key(node))
249 return NULL;
250
251 node = xbc_node_get_child(node);
252 if (node && !xbc_node_is_value(node))
253 return NULL;
254
255 if (vnode)
256 *vnode = node;
257
258 return node ? xbc_node_get_data(node) : "";
259}
260
261/**
262 * xbc_node_compose_key_after() - Compose partial key string of the XBC node
263 * @root: Root XBC node
264 * @node: Target XBC node.
265 * @buf: A buffer to store the key.
266 * @size: The size of the @buf.
267 *
268 * Compose the partial key of the @node into @buf, which is starting right
269 * after @root (@root is not included.) If @root is NULL, this returns full
270 * key words of @node.
271 * Returns the total length of the key stored in @buf. Returns -EINVAL
272 * if @node is NULL or @root is not the ancestor of @node or @root is @node,
273 * or returns -ERANGE if the key depth is deeper than max depth.
274 * This is expected to be used with xbc_find_node() to list up all (child)
275 * keys under given key.
276 */
277int __init xbc_node_compose_key_after(struct xbc_node *root,
278 struct xbc_node *node,
279 char *buf, size_t size)
280{
Masami Hiramatsu4f292c42021-09-17 19:03:08 +0900281 uint16_t keys[XBC_DEPTH_MAX];
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900282 int depth = 0, ret = 0, total = 0;
283
284 if (!node || node == root)
285 return -EINVAL;
286
287 if (xbc_node_is_value(node))
288 node = xbc_node_get_parent(node);
289
290 while (node && node != root) {
291 keys[depth++] = xbc_node_index(node);
292 if (depth == XBC_DEPTH_MAX)
293 return -ERANGE;
294 node = xbc_node_get_parent(node);
295 }
296 if (!node && root)
297 return -EINVAL;
298
299 while (--depth >= 0) {
300 node = xbc_nodes + keys[depth];
301 ret = snprintf(buf, size, "%s%s", xbc_node_get_data(node),
302 depth ? "." : "");
303 if (ret < 0)
304 return ret;
305 if (ret > size) {
306 size = 0;
307 } else {
308 size -= ret;
309 buf += ret;
310 }
311 total += ret;
312 }
313
314 return total;
315}
316
317/**
318 * xbc_node_find_next_leaf() - Find the next leaf node under given node
319 * @root: An XBC root node
320 * @node: An XBC node which starts from.
321 *
322 * Search the next leaf node (which means the terminal key node) of @node
323 * under @root node (including @root node itself).
324 * Return the next node or NULL if next leaf node is not found.
325 */
326struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root,
327 struct xbc_node *node)
328{
Masami Hiramatsue5efaeb2021-06-02 17:19:07 +0900329 struct xbc_node *next;
330
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900331 if (unlikely(!xbc_data))
332 return NULL;
333
334 if (!node) { /* First try */
335 node = root;
336 if (!node)
337 node = xbc_nodes;
338 } else {
Masami Hiramatsue5efaeb2021-06-02 17:19:07 +0900339 /* Leaf node may have a subkey */
340 next = xbc_node_get_subkey(node);
341 if (next) {
342 node = next;
343 goto found;
344 }
345
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900346 if (node == root) /* @root was a leaf, no child node. */
347 return NULL;
348
349 while (!node->next) {
350 node = xbc_node_get_parent(node);
351 if (node == root)
352 return NULL;
353 /* User passed a node which is not uder parent */
354 if (WARN_ON(!node))
355 return NULL;
356 }
357 node = xbc_node_get_next(node);
358 }
359
Masami Hiramatsue5efaeb2021-06-02 17:19:07 +0900360found:
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900361 while (node && !xbc_node_is_leaf(node))
362 node = xbc_node_get_child(node);
363
364 return node;
365}
366
367/**
368 * xbc_node_find_next_key_value() - Find the next key-value pair nodes
369 * @root: An XBC root node
370 * @leaf: A container pointer of XBC node which starts from.
371 *
372 * Search the next leaf node (which means the terminal key node) of *@leaf
373 * under @root node. Returns the value and update *@leaf if next leaf node
374 * is found, or NULL if no next leaf node is found.
375 * Note that this returns 0-length string if the key has no value, or
376 * the value of the first entry if the value is an array.
377 */
378const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
379 struct xbc_node **leaf)
380{
381 /* tip must be passed */
382 if (WARN_ON(!leaf))
383 return NULL;
384
385 *leaf = xbc_node_find_next_leaf(root, *leaf);
386 if (!*leaf)
387 return NULL;
388 if ((*leaf)->child)
389 return xbc_node_get_data(xbc_node_get_child(*leaf));
390 else
391 return ""; /* No value key */
392}
393
394/* XBC parse and tree build */
395
Masami Hiramatsu4f292c42021-09-17 19:03:08 +0900396static int __init xbc_init_node(struct xbc_node *node, char *data, uint32_t flag)
Masami Hiramatsua2de2f82020-07-16 01:00:29 +0900397{
398 unsigned long offset = data - xbc_data;
399
400 if (WARN_ON(offset >= XBC_DATA_MAX))
401 return -EINVAL;
402
Masami Hiramatsu4f292c42021-09-17 19:03:08 +0900403 node->data = (uint16_t)offset | flag;
Masami Hiramatsua2de2f82020-07-16 01:00:29 +0900404 node->child = 0;
405 node->next = 0;
406
407 return 0;
408}
409
Masami Hiramatsu4f292c42021-09-17 19:03:08 +0900410static struct xbc_node * __init xbc_add_node(char *data, uint32_t flag)
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900411{
412 struct xbc_node *node;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900413
414 if (xbc_node_num == XBC_NODE_MAX)
415 return NULL;
416
417 node = &xbc_nodes[xbc_node_num++];
Masami Hiramatsua2de2f82020-07-16 01:00:29 +0900418 if (xbc_init_node(node, data, flag) < 0)
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900419 return NULL;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900420
421 return node;
422}
423
424static inline __init struct xbc_node *xbc_last_sibling(struct xbc_node *node)
425{
426 while (node->next)
427 node = xbc_node_get_next(node);
428
429 return node;
430}
431
Masami Hiramatsuca243062021-06-02 17:18:58 +0900432static inline __init struct xbc_node *xbc_last_child(struct xbc_node *node)
433{
434 while (node->child)
435 node = xbc_node_get_child(node);
436
437 return node;
438}
439
Masami Hiramatsu4f292c42021-09-17 19:03:08 +0900440static struct xbc_node * __init __xbc_add_sibling(char *data, uint32_t flag, bool head)
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900441{
442 struct xbc_node *sib, *node = xbc_add_node(data, flag);
443
444 if (node) {
445 if (!last_parent) {
Masami Hiramatsue5efaeb2021-06-02 17:19:07 +0900446 /* Ignore @head in this case */
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900447 node->parent = XBC_NODE_MAX;
448 sib = xbc_last_sibling(xbc_nodes);
449 sib->next = xbc_node_index(node);
450 } else {
451 node->parent = xbc_node_index(last_parent);
Masami Hiramatsue5efaeb2021-06-02 17:19:07 +0900452 if (!last_parent->child || head) {
453 node->next = last_parent->child;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900454 last_parent->child = xbc_node_index(node);
455 } else {
456 sib = xbc_node_get_child(last_parent);
457 sib = xbc_last_sibling(sib);
458 sib->next = xbc_node_index(node);
459 }
460 }
Masami Hiramatsu597c0e32020-02-05 22:50:04 +0900461 } else
462 xbc_parse_error("Too many nodes", data);
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900463
464 return node;
465}
466
Masami Hiramatsu4f292c42021-09-17 19:03:08 +0900467static inline struct xbc_node * __init xbc_add_sibling(char *data, uint32_t flag)
Masami Hiramatsue5efaeb2021-06-02 17:19:07 +0900468{
469 return __xbc_add_sibling(data, flag, false);
470}
471
Masami Hiramatsu4f292c42021-09-17 19:03:08 +0900472static inline struct xbc_node * __init xbc_add_head_sibling(char *data, uint32_t flag)
Masami Hiramatsue5efaeb2021-06-02 17:19:07 +0900473{
474 return __xbc_add_sibling(data, flag, true);
475}
476
Masami Hiramatsu4f292c42021-09-17 19:03:08 +0900477static inline __init struct xbc_node *xbc_add_child(char *data, uint32_t flag)
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900478{
479 struct xbc_node *node = xbc_add_sibling(data, flag);
480
481 if (node)
482 last_parent = node;
483
484 return node;
485}
486
487static inline __init bool xbc_valid_keyword(char *key)
488{
489 if (key[0] == '\0')
490 return false;
491
492 while (isalnum(*key) || *key == '-' || *key == '_')
493 key++;
494
495 return *key == '\0';
496}
497
498static char *skip_comment(char *p)
499{
500 char *ret;
501
502 ret = strchr(p, '\n');
503 if (!ret)
504 ret = p + strlen(p);
505 else
506 ret++;
507
508 return ret;
509}
510
511static char *skip_spaces_until_newline(char *p)
512{
513 while (isspace(*p) && *p != '\n')
514 p++;
515 return p;
516}
517
Masami Hiramatsuead1e19a2020-09-21 18:44:42 +0900518static int __init __xbc_open_brace(char *p)
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900519{
Masami Hiramatsuead1e19a2020-09-21 18:44:42 +0900520 /* Push the last key as open brace */
521 open_brace[brace_index++] = xbc_node_index(last_parent);
522 if (brace_index >= XBC_DEPTH_MAX)
523 return xbc_parse_error("Exceed max depth of braces", p);
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900524
525 return 0;
526}
527
528static int __init __xbc_close_brace(char *p)
529{
Masami Hiramatsuead1e19a2020-09-21 18:44:42 +0900530 brace_index--;
531 if (!last_parent || brace_index < 0 ||
532 (open_brace[brace_index] != xbc_node_index(last_parent)))
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900533 return xbc_parse_error("Unexpected closing brace", p);
534
Masami Hiramatsuead1e19a2020-09-21 18:44:42 +0900535 if (brace_index == 0)
536 last_parent = NULL;
537 else
538 last_parent = &xbc_nodes[open_brace[brace_index - 1]];
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900539
540 return 0;
541}
542
543/*
544 * Return delimiter or error, no node added. As same as lib/cmdline.c,
545 * you can use " around spaces, but can't escape " for value.
546 */
547static int __init __xbc_parse_value(char **__v, char **__n)
548{
549 char *p, *v = *__v;
550 int c, quotes = 0;
551
552 v = skip_spaces(v);
553 while (*v == '#') {
554 v = skip_comment(v);
555 v = skip_spaces(v);
556 }
557 if (*v == '"' || *v == '\'') {
558 quotes = *v;
559 v++;
560 }
561 p = v - 1;
562 while ((c = *++p)) {
563 if (!isprint(c) && !isspace(c))
564 return xbc_parse_error("Non printable value", p);
565 if (quotes) {
566 if (c != quotes)
567 continue;
568 quotes = 0;
569 *p++ = '\0';
570 p = skip_spaces_until_newline(p);
571 c = *p;
572 if (c && !strchr(",;\n#}", c))
573 return xbc_parse_error("No value delimiter", p);
574 if (*p)
575 p++;
576 break;
577 }
578 if (strchr(",;\n#}", c)) {
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900579 *p++ = '\0';
Masami Hiramatsuc7af4ec2020-09-21 18:44:51 +0900580 v = strim(v);
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900581 break;
582 }
583 }
584 if (quotes)
585 return xbc_parse_error("No closing quotes", p);
586 if (c == '#') {
587 p = skip_comment(p);
588 c = '\n'; /* A comment must be treated as a newline */
589 }
590 *__n = p;
591 *__v = v;
592
593 return c;
594}
595
596static int __init xbc_parse_array(char **__v)
597{
598 struct xbc_node *node;
599 char *next;
600 int c = 0;
601
Masami Hiramatsuca243062021-06-02 17:18:58 +0900602 if (last_parent->child)
603 last_parent = xbc_node_get_child(last_parent);
604
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900605 do {
606 c = __xbc_parse_value(__v, &next);
607 if (c < 0)
608 return c;
609
Masami Hiramatsuca243062021-06-02 17:18:58 +0900610 node = xbc_add_child(*__v, XBC_VALUE);
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900611 if (!node)
612 return -ENOMEM;
613 *__v = next;
614 } while (c == ',');
Masami Hiramatsuca243062021-06-02 17:18:58 +0900615 node->child = 0;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900616
617 return c;
618}
619
620static inline __init
621struct xbc_node *find_match_node(struct xbc_node *node, char *k)
622{
623 while (node) {
624 if (!strcmp(xbc_node_get_data(node), k))
625 break;
626 node = xbc_node_get_next(node);
627 }
628 return node;
629}
630
631static int __init __xbc_add_key(char *k)
632{
Masami Hiramatsua24d2862020-02-20 21:19:12 +0900633 struct xbc_node *node, *child;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900634
635 if (!xbc_valid_keyword(k))
636 return xbc_parse_error("Invalid keyword", k);
637
638 if (unlikely(xbc_node_num == 0))
639 goto add_node;
640
641 if (!last_parent) /* the first level */
642 node = find_match_node(xbc_nodes, k);
Masami Hiramatsua24d2862020-02-20 21:19:12 +0900643 else {
644 child = xbc_node_get_child(last_parent);
Masami Hiramatsue5efaeb2021-06-02 17:19:07 +0900645 /* Since the value node is the first child, skip it. */
Masami Hiramatsua24d2862020-02-20 21:19:12 +0900646 if (child && xbc_node_is_value(child))
Masami Hiramatsue5efaeb2021-06-02 17:19:07 +0900647 child = xbc_node_get_next(child);
Masami Hiramatsua24d2862020-02-20 21:19:12 +0900648 node = find_match_node(child, k);
649 }
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900650
651 if (node)
652 last_parent = node;
653 else {
654add_node:
655 node = xbc_add_child(k, XBC_KEY);
656 if (!node)
657 return -ENOMEM;
658 }
659 return 0;
660}
661
662static int __init __xbc_parse_keys(char *k)
663{
664 char *p;
665 int ret;
666
667 k = strim(k);
668 while ((p = strchr(k, '.'))) {
669 *p++ = '\0';
670 ret = __xbc_add_key(k);
671 if (ret)
672 return ret;
673 k = p;
674 }
675
676 return __xbc_add_key(k);
677}
678
Masami Hiramatsu5f811c52020-02-21 17:13:52 +0900679static int __init xbc_parse_kv(char **k, char *v, int op)
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900680{
681 struct xbc_node *prev_parent = last_parent;
Masami Hiramatsu4e4694d2020-02-21 17:13:42 +0900682 struct xbc_node *child;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900683 char *next;
684 int c, ret;
685
686 ret = __xbc_parse_keys(*k);
687 if (ret)
688 return ret;
689
690 c = __xbc_parse_value(&v, &next);
691 if (c < 0)
692 return c;
693
Masami Hiramatsue5efaeb2021-06-02 17:19:07 +0900694 child = xbc_node_get_child(last_parent);
695 if (child && xbc_node_is_value(child)) {
696 if (op == '=')
697 return xbc_parse_error("Value is redefined", v);
698 if (op == ':') {
699 unsigned short nidx = child->next;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900700
Masami Hiramatsue5efaeb2021-06-02 17:19:07 +0900701 xbc_init_node(child, v, XBC_VALUE);
702 child->next = nidx; /* keep subkeys */
703 goto array;
704 }
705 /* op must be '+' */
706 last_parent = xbc_last_child(child);
707 }
708 /* The value node should always be the first child */
709 if (!xbc_add_head_sibling(v, XBC_VALUE))
710 return -ENOMEM;
711
712array:
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900713 if (c == ',') { /* Array */
714 c = xbc_parse_array(&next);
715 if (c < 0)
716 return c;
717 }
718
719 last_parent = prev_parent;
720
721 if (c == '}') {
722 ret = __xbc_close_brace(next - 1);
723 if (ret < 0)
724 return ret;
725 }
726
727 *k = next;
728
729 return 0;
730}
731
732static int __init xbc_parse_key(char **k, char *n)
733{
734 struct xbc_node *prev_parent = last_parent;
735 int ret;
736
737 *k = strim(*k);
738 if (**k != '\0') {
739 ret = __xbc_parse_keys(*k);
740 if (ret)
741 return ret;
742 last_parent = prev_parent;
743 }
744 *k = n;
745
746 return 0;
747}
748
749static int __init xbc_open_brace(char **k, char *n)
750{
751 int ret;
752
753 ret = __xbc_parse_keys(*k);
754 if (ret)
755 return ret;
756 *k = n;
757
Masami Hiramatsuead1e19a2020-09-21 18:44:42 +0900758 return __xbc_open_brace(n - 1);
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900759}
760
761static int __init xbc_close_brace(char **k, char *n)
762{
763 int ret;
764
765 ret = xbc_parse_key(k, n);
766 if (ret)
767 return ret;
768 /* k is updated in xbc_parse_key() */
769
770 return __xbc_close_brace(n - 1);
771}
772
773static int __init xbc_verify_tree(void)
774{
775 int i, depth, len, wlen;
776 struct xbc_node *n, *m;
777
Masami Hiramatsuead1e19a2020-09-21 18:44:42 +0900778 /* Brace closing */
779 if (brace_index) {
780 n = &xbc_nodes[open_brace[brace_index]];
781 return xbc_parse_error("Brace is not closed",
782 xbc_node_get_data(n));
783 }
784
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900785 /* Empty tree */
Masami Hiramatsu597c0e32020-02-05 22:50:04 +0900786 if (xbc_node_num == 0) {
787 xbc_parse_error("Empty config", xbc_data);
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900788 return -ENOENT;
Masami Hiramatsu597c0e32020-02-05 22:50:04 +0900789 }
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900790
791 for (i = 0; i < xbc_node_num; i++) {
792 if (xbc_nodes[i].next > xbc_node_num) {
793 return xbc_parse_error("No closing brace",
794 xbc_node_get_data(xbc_nodes + i));
795 }
796 }
797
798 /* Key tree limitation check */
799 n = &xbc_nodes[0];
800 depth = 1;
801 len = 0;
802
803 while (n) {
804 wlen = strlen(xbc_node_get_data(n)) + 1;
805 len += wlen;
806 if (len > XBC_KEYLEN_MAX)
807 return xbc_parse_error("Too long key length",
808 xbc_node_get_data(n));
809
810 m = xbc_node_get_child(n);
811 if (m && xbc_node_is_key(m)) {
812 n = m;
813 depth++;
814 if (depth > XBC_DEPTH_MAX)
815 return xbc_parse_error("Too many key words",
816 xbc_node_get_data(n));
817 continue;
818 }
819 len -= wlen;
820 m = xbc_node_get_next(n);
821 while (!m) {
822 n = xbc_node_get_parent(n);
823 if (!n)
824 break;
825 len -= strlen(xbc_node_get_data(n)) + 1;
826 depth--;
827 m = xbc_node_get_next(n);
828 }
829 n = m;
830 }
831
832 return 0;
833}
834
Masami Hiramatsuf3668cd2021-09-17 19:02:46 +0900835/* Need to setup xbc_data and xbc_nodes before call this. */
836static int __init xbc_parse_tree(void)
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900837{
838 char *p, *q;
Steven Rostedt (VMware)39d9c1c2021-10-27 10:57:53 -0400839 int ret = 0, c;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900840
Masami Hiramatsubdac5c22021-09-16 15:23:20 +0900841 last_parent = NULL;
842 p = xbc_data;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900843 do {
Masami Hiramatsua2de2f82020-07-16 01:00:29 +0900844 q = strpbrk(p, "{}=+;:\n#");
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900845 if (!q) {
846 p = skip_spaces(p);
847 if (*p != '\0')
848 ret = xbc_parse_error("No delimiter", p);
849 break;
850 }
851
852 c = *q;
853 *q++ = '\0';
854 switch (c) {
Masami Hiramatsua2de2f82020-07-16 01:00:29 +0900855 case ':':
Masami Hiramatsu5f811c52020-02-21 17:13:52 +0900856 case '+':
857 if (*q++ != '=') {
Masami Hiramatsua2de2f82020-07-16 01:00:29 +0900858 ret = xbc_parse_error(c == '+' ?
859 "Wrong '+' operator" :
860 "Wrong ':' operator",
Masami Hiramatsu5f811c52020-02-21 17:13:52 +0900861 q - 2);
862 break;
863 }
Nick Desaulniers4c1ca832020-11-15 20:35:31 -0800864 fallthrough;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900865 case '=':
Masami Hiramatsu5f811c52020-02-21 17:13:52 +0900866 ret = xbc_parse_kv(&p, q, c);
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900867 break;
868 case '{':
869 ret = xbc_open_brace(&p, q);
870 break;
871 case '#':
872 q = skip_comment(q);
Nick Desaulniers4c1ca832020-11-15 20:35:31 -0800873 fallthrough;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900874 case ';':
875 case '\n':
876 ret = xbc_parse_key(&p, q);
877 break;
878 case '}':
879 ret = xbc_close_brace(&p, q);
880 break;
881 }
882 } while (!ret);
883
Masami Hiramatsuf3668cd2021-09-17 19:02:46 +0900884 return ret;
885}
886
887/**
888 * xbc_exit() - Clean up all parsed bootconfig
889 *
890 * This clears all data structures of parsed bootconfig on memory.
891 * If you need to reuse xbc_init() with new boot config, you can
892 * use this.
893 */
894void __init xbc_exit(void)
895{
Masami Hiramatsu4ee1b4c2021-09-17 19:03:16 +0900896 xbc_free_mem(xbc_data, xbc_data_size);
Masami Hiramatsuf3668cd2021-09-17 19:02:46 +0900897 xbc_data = NULL;
898 xbc_data_size = 0;
899 xbc_node_num = 0;
Masami Hiramatsu4ee1b4c2021-09-17 19:03:16 +0900900 xbc_free_mem(xbc_nodes, sizeof(struct xbc_node) * XBC_NODE_MAX);
Masami Hiramatsuf3668cd2021-09-17 19:02:46 +0900901 xbc_nodes = NULL;
902 brace_index = 0;
903}
904
905/**
906 * xbc_init() - Parse given XBC file and build XBC internal tree
907 * @data: The boot config text original data
908 * @size: The size of @data
909 * @emsg: A pointer of const char * to store the error message
910 * @epos: A pointer of int to store the error position
911 *
912 * This parses the boot config text in @data. @size must be smaller
913 * than XBC_DATA_MAX.
914 * Return the number of stored nodes (>0) if succeeded, or -errno
915 * if there is any error.
916 * In error cases, @emsg will be updated with an error message and
917 * @epos will be updated with the error position which is the byte offset
918 * of @buf. If the error is not a parser error, @epos will be -1.
919 */
920int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos)
921{
922 int ret;
923
924 if (epos)
925 *epos = -1;
926
927 if (xbc_data) {
928 if (emsg)
929 *emsg = "Bootconfig is already initialized";
930 return -EBUSY;
931 }
932 if (size > XBC_DATA_MAX || size == 0) {
933 if (emsg)
934 *emsg = size ? "Config data is too big" :
935 "Config data is empty";
936 return -ERANGE;
937 }
938
Masami Hiramatsu4ee1b4c2021-09-17 19:03:16 +0900939 xbc_data = xbc_alloc_mem(size + 1);
Masami Hiramatsuf3668cd2021-09-17 19:02:46 +0900940 if (!xbc_data) {
941 if (emsg)
942 *emsg = "Failed to allocate bootconfig data";
943 return -ENOMEM;
944 }
945 memcpy(xbc_data, data, size);
946 xbc_data[size] = '\0';
947 xbc_data_size = size + 1;
948
Masami Hiramatsu4ee1b4c2021-09-17 19:03:16 +0900949 xbc_nodes = xbc_alloc_mem(sizeof(struct xbc_node) * XBC_NODE_MAX);
Masami Hiramatsuf3668cd2021-09-17 19:02:46 +0900950 if (!xbc_nodes) {
951 if (emsg)
952 *emsg = "Failed to allocate bootconfig nodes";
953 xbc_exit();
954 return -ENOMEM;
955 }
956 memset(xbc_nodes, 0, sizeof(struct xbc_node) * XBC_NODE_MAX);
957
958 ret = xbc_parse_tree();
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900959 if (!ret)
960 ret = xbc_verify_tree();
961
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900962 if (ret < 0) {
963 if (epos)
964 *epos = xbc_err_pos;
965 if (emsg)
966 *emsg = xbc_err_msg;
Masami Hiramatsu115d4d02021-09-17 19:02:39 +0900967 xbc_exit();
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900968 } else
Masami Hiramatsu0f0d0a72020-02-05 22:50:13 +0900969 ret = xbc_node_num;
Masami Hiramatsu76db5a22020-01-11 01:03:32 +0900970
971 return ret;
972}