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