1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Extra Boot Config 4 * Masami Hiramatsu <mhiramat@kernel.org> 5 */ 6 7 #ifdef __KERNEL__ 8 #include <linux/bootconfig.h> 9 #include <linux/bug.h> 10 #include <linux/ctype.h> 11 #include <linux/errno.h> 12 #include <linux/kernel.h> 13 #include <linux/memblock.h> 14 #include <linux/string.h> 15 #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 25 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 35 static struct xbc_node *xbc_nodes __initdata; 36 static int xbc_node_num __initdata; 37 static char *xbc_data __initdata; 38 static size_t xbc_data_size __initdata; 39 static struct xbc_node *last_parent __initdata; 40 static const char *xbc_err_msg __initdata; 41 static int xbc_err_pos __initdata; 42 static int open_brace[XBC_DEPTH_MAX] __initdata; 43 static int brace_index __initdata; 44 45 #ifdef __KERNEL__ 46 static inline void * __init xbc_alloc_mem(size_t size) 47 { 48 return memblock_alloc(size, SMP_CACHE_BYTES); 49 } 50 51 static inline void __init xbc_free_mem(void *addr, size_t size) 52 { 53 memblock_free(addr, size); 54 } 55 56 #else /* !__KERNEL__ */ 57 58 static inline void *xbc_alloc_mem(size_t size) 59 { 60 return malloc(size); 61 } 62 63 static inline void xbc_free_mem(void *addr, size_t size) 64 { 65 free(addr); 66 } 67 #endif 68 /** 69 * xbc_get_info() - Get the information of loaded boot config 70 * @node_size: A pointer to store the number of nodes. 71 * @data_size: A pointer to store the size of bootconfig data. 72 * 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 */ 77 int __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 89 static int __init xbc_parse_error(const char *msg, const char *p) 90 { 91 xbc_err_msg = msg; 92 xbc_err_pos = (int)(p - xbc_data); 93 94 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 */ 103 struct 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 */ 117 int __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 */ 129 struct 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 */ 141 struct 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 */ 155 struct 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 */ 167 const 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 177 static bool __init 178 xbc_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 /** 197 * xbc_node_find_subkey() - Find a subkey node which matches given key 198 * @parent: An XBC node. 199 * @key: A key string. 200 * 201 * Search a key node under @parent which matches @key. The @key can contain 202 * several words jointed with '.'. If @parent is NULL, this searches the 203 * node from whole tree. Return NULL if no node is matched. 204 */ 205 struct xbc_node * __init 206 xbc_node_find_subkey(struct xbc_node *parent, const char *key) 207 { 208 struct xbc_node *node; 209 210 if (parent) 211 node = xbc_node_get_subkey(parent); 212 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') 219 node = xbc_node_get_subkey(node); 220 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 */ 242 const char * __init 243 xbc_node_find_value(struct xbc_node *parent, const char *key, 244 struct xbc_node **vnode) 245 { 246 struct xbc_node *node = xbc_node_find_subkey(parent, key); 247 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 */ 277 int __init xbc_node_compose_key_after(struct xbc_node *root, 278 struct xbc_node *node, 279 char *buf, size_t size) 280 { 281 uint16_t keys[XBC_DEPTH_MAX]; 282 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 */ 326 struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root, 327 struct xbc_node *node) 328 { 329 struct xbc_node *next; 330 331 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 { 339 /* Leaf node may have a subkey */ 340 next = xbc_node_get_subkey(node); 341 if (next) { 342 node = next; 343 goto found; 344 } 345 346 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 360 found: 361 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 */ 378 const 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 396 static int __init xbc_init_node(struct xbc_node *node, char *data, uint32_t flag) 397 { 398 unsigned long offset = data - xbc_data; 399 400 if (WARN_ON(offset >= XBC_DATA_MAX)) 401 return -EINVAL; 402 403 node->data = (uint16_t)offset | flag; 404 node->child = 0; 405 node->next = 0; 406 407 return 0; 408 } 409 410 static struct xbc_node * __init xbc_add_node(char *data, uint32_t flag) 411 { 412 struct xbc_node *node; 413 414 if (xbc_node_num == XBC_NODE_MAX) 415 return NULL; 416 417 node = &xbc_nodes[xbc_node_num++]; 418 if (xbc_init_node(node, data, flag) < 0) 419 return NULL; 420 421 return node; 422 } 423 424 static 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 432 static 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 440 static struct xbc_node * __init __xbc_add_sibling(char *data, uint32_t flag, bool head) 441 { 442 struct xbc_node *sib, *node = xbc_add_node(data, flag); 443 444 if (node) { 445 if (!last_parent) { 446 /* Ignore @head in this case */ 447 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); 452 if (!last_parent->child || head) { 453 node->next = last_parent->child; 454 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 } 461 } else 462 xbc_parse_error("Too many nodes", data); 463 464 return node; 465 } 466 467 static inline struct xbc_node * __init xbc_add_sibling(char *data, uint32_t flag) 468 { 469 return __xbc_add_sibling(data, flag, false); 470 } 471 472 static inline struct xbc_node * __init xbc_add_head_sibling(char *data, uint32_t flag) 473 { 474 return __xbc_add_sibling(data, flag, true); 475 } 476 477 static inline __init struct xbc_node *xbc_add_child(char *data, uint32_t flag) 478 { 479 struct xbc_node *node = xbc_add_sibling(data, flag); 480 481 if (node) 482 last_parent = node; 483 484 return node; 485 } 486 487 static 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 498 static 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 511 static char *skip_spaces_until_newline(char *p) 512 { 513 while (isspace(*p) && *p != '\n') 514 p++; 515 return p; 516 } 517 518 static int __init __xbc_open_brace(char *p) 519 { 520 /* 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); 524 525 return 0; 526 } 527 528 static int __init __xbc_close_brace(char *p) 529 { 530 brace_index--; 531 if (!last_parent || brace_index < 0 || 532 (open_brace[brace_index] != xbc_node_index(last_parent))) 533 return xbc_parse_error("Unexpected closing brace", p); 534 535 if (brace_index == 0) 536 last_parent = NULL; 537 else 538 last_parent = &xbc_nodes[open_brace[brace_index - 1]]; 539 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 */ 547 static 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)) { 579 *p++ = '\0'; 580 v = strim(v); 581 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 596 static int __init xbc_parse_array(char **__v) 597 { 598 struct xbc_node *node; 599 char *next; 600 int c = 0; 601 602 if (last_parent->child) 603 last_parent = xbc_node_get_child(last_parent); 604 605 do { 606 c = __xbc_parse_value(__v, &next); 607 if (c < 0) 608 return c; 609 610 node = xbc_add_child(*__v, XBC_VALUE); 611 if (!node) 612 return -ENOMEM; 613 *__v = next; 614 } while (c == ','); 615 node->child = 0; 616 617 return c; 618 } 619 620 static inline __init 621 struct 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 631 static int __init __xbc_add_key(char *k) 632 { 633 struct xbc_node *node, *child; 634 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); 643 else { 644 child = xbc_node_get_child(last_parent); 645 /* Since the value node is the first child, skip it. */ 646 if (child && xbc_node_is_value(child)) 647 child = xbc_node_get_next(child); 648 node = find_match_node(child, k); 649 } 650 651 if (node) 652 last_parent = node; 653 else { 654 add_node: 655 node = xbc_add_child(k, XBC_KEY); 656 if (!node) 657 return -ENOMEM; 658 } 659 return 0; 660 } 661 662 static 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 679 static int __init xbc_parse_kv(char **k, char *v, int op) 680 { 681 struct xbc_node *prev_parent = last_parent; 682 struct xbc_node *child; 683 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 694 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; 700 701 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 712 array: 713 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 732 static 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 749 static 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 758 return __xbc_open_brace(n - 1); 759 } 760 761 static 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 773 static int __init xbc_verify_tree(void) 774 { 775 int i, depth, len, wlen; 776 struct xbc_node *n, *m; 777 778 /* 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 785 /* Empty tree */ 786 if (xbc_node_num == 0) { 787 xbc_parse_error("Empty config", xbc_data); 788 return -ENOENT; 789 } 790 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 835 /* Need to setup xbc_data and xbc_nodes before call this. */ 836 static int __init xbc_parse_tree(void) 837 { 838 char *p, *q; 839 int ret = 0, c; 840 841 last_parent = NULL; 842 p = xbc_data; 843 do { 844 q = strpbrk(p, "{}=+;:\n#"); 845 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) { 855 case ':': 856 case '+': 857 if (*q++ != '=') { 858 ret = xbc_parse_error(c == '+' ? 859 "Wrong '+' operator" : 860 "Wrong ':' operator", 861 q - 2); 862 break; 863 } 864 fallthrough; 865 case '=': 866 ret = xbc_parse_kv(&p, q, c); 867 break; 868 case '{': 869 ret = xbc_open_brace(&p, q); 870 break; 871 case '#': 872 q = skip_comment(q); 873 fallthrough; 874 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 884 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 */ 894 void __init xbc_exit(void) 895 { 896 xbc_free_mem(xbc_data, xbc_data_size); 897 xbc_data = NULL; 898 xbc_data_size = 0; 899 xbc_node_num = 0; 900 xbc_free_mem(xbc_nodes, sizeof(struct xbc_node) * XBC_NODE_MAX); 901 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 */ 920 int __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 939 xbc_data = xbc_alloc_mem(size + 1); 940 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 949 xbc_nodes = xbc_alloc_mem(sizeof(struct xbc_node) * XBC_NODE_MAX); 950 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(); 959 if (!ret) 960 ret = xbc_verify_tree(); 961 962 if (ret < 0) { 963 if (epos) 964 *epos = xbc_err_pos; 965 if (emsg) 966 *emsg = xbc_err_msg; 967 xbc_exit(); 968 } else 969 ret = xbc_node_num; 970 971 return ret; 972 } 973