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