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