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; 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 node = find_match_node(xbc_node_get_child(last_parent), k); 548 549 if (node) 550 last_parent = node; 551 else { 552 add_node: 553 node = xbc_add_child(k, XBC_KEY); 554 if (!node) 555 return -ENOMEM; 556 } 557 return 0; 558 } 559 560 static int __init __xbc_parse_keys(char *k) 561 { 562 char *p; 563 int ret; 564 565 k = strim(k); 566 while ((p = strchr(k, '.'))) { 567 *p++ = '\0'; 568 ret = __xbc_add_key(k); 569 if (ret) 570 return ret; 571 k = p; 572 } 573 574 return __xbc_add_key(k); 575 } 576 577 static int __init xbc_parse_kv(char **k, char *v) 578 { 579 struct xbc_node *prev_parent = last_parent; 580 struct xbc_node *node; 581 char *next; 582 int c, ret; 583 584 ret = __xbc_parse_keys(*k); 585 if (ret) 586 return ret; 587 588 c = __xbc_parse_value(&v, &next); 589 if (c < 0) 590 return c; 591 592 node = xbc_add_sibling(v, XBC_VALUE); 593 if (!node) 594 return -ENOMEM; 595 596 if (c == ',') { /* Array */ 597 c = xbc_parse_array(&next); 598 if (c < 0) 599 return c; 600 } 601 602 last_parent = prev_parent; 603 604 if (c == '}') { 605 ret = __xbc_close_brace(next - 1); 606 if (ret < 0) 607 return ret; 608 } 609 610 *k = next; 611 612 return 0; 613 } 614 615 static int __init xbc_parse_key(char **k, char *n) 616 { 617 struct xbc_node *prev_parent = last_parent; 618 int ret; 619 620 *k = strim(*k); 621 if (**k != '\0') { 622 ret = __xbc_parse_keys(*k); 623 if (ret) 624 return ret; 625 last_parent = prev_parent; 626 } 627 *k = n; 628 629 return 0; 630 } 631 632 static int __init xbc_open_brace(char **k, char *n) 633 { 634 int ret; 635 636 ret = __xbc_parse_keys(*k); 637 if (ret) 638 return ret; 639 *k = n; 640 641 return __xbc_open_brace(); 642 } 643 644 static int __init xbc_close_brace(char **k, char *n) 645 { 646 int ret; 647 648 ret = xbc_parse_key(k, n); 649 if (ret) 650 return ret; 651 /* k is updated in xbc_parse_key() */ 652 653 return __xbc_close_brace(n - 1); 654 } 655 656 static int __init xbc_verify_tree(void) 657 { 658 int i, depth, len, wlen; 659 struct xbc_node *n, *m; 660 661 /* Empty tree */ 662 if (xbc_node_num == 0) { 663 xbc_parse_error("Empty config", xbc_data); 664 return -ENOENT; 665 } 666 667 for (i = 0; i < xbc_node_num; i++) { 668 if (xbc_nodes[i].next > xbc_node_num) { 669 return xbc_parse_error("No closing brace", 670 xbc_node_get_data(xbc_nodes + i)); 671 } 672 } 673 674 /* Key tree limitation check */ 675 n = &xbc_nodes[0]; 676 depth = 1; 677 len = 0; 678 679 while (n) { 680 wlen = strlen(xbc_node_get_data(n)) + 1; 681 len += wlen; 682 if (len > XBC_KEYLEN_MAX) 683 return xbc_parse_error("Too long key length", 684 xbc_node_get_data(n)); 685 686 m = xbc_node_get_child(n); 687 if (m && xbc_node_is_key(m)) { 688 n = m; 689 depth++; 690 if (depth > XBC_DEPTH_MAX) 691 return xbc_parse_error("Too many key words", 692 xbc_node_get_data(n)); 693 continue; 694 } 695 len -= wlen; 696 m = xbc_node_get_next(n); 697 while (!m) { 698 n = xbc_node_get_parent(n); 699 if (!n) 700 break; 701 len -= strlen(xbc_node_get_data(n)) + 1; 702 depth--; 703 m = xbc_node_get_next(n); 704 } 705 n = m; 706 } 707 708 return 0; 709 } 710 711 /** 712 * xbc_destroy_all() - Clean up all parsed bootconfig 713 * 714 * This clears all data structures of parsed bootconfig on memory. 715 * If you need to reuse xbc_init() with new boot config, you can 716 * use this. 717 */ 718 void __init xbc_destroy_all(void) 719 { 720 xbc_data = NULL; 721 xbc_data_size = 0; 722 xbc_node_num = 0; 723 memblock_free(__pa(xbc_nodes), sizeof(struct xbc_node) * XBC_NODE_MAX); 724 xbc_nodes = NULL; 725 } 726 727 /** 728 * xbc_init() - Parse given XBC file and build XBC internal tree 729 * @buf: boot config text 730 * 731 * This parses the boot config text in @buf. @buf must be a 732 * null terminated string and smaller than XBC_DATA_MAX. 733 * Return the number of stored nodes (>0) if succeeded, or -errno 734 * if there is any error. 735 */ 736 int __init xbc_init(char *buf) 737 { 738 char *p, *q; 739 int ret, c; 740 741 if (xbc_data) { 742 pr_err("Error: bootconfig is already initialized.\n"); 743 return -EBUSY; 744 } 745 746 ret = strlen(buf); 747 if (ret > XBC_DATA_MAX - 1 || ret == 0) { 748 pr_err("Error: Config data is %s.\n", 749 ret ? "too big" : "empty"); 750 return -ERANGE; 751 } 752 753 xbc_nodes = memblock_alloc(sizeof(struct xbc_node) * XBC_NODE_MAX, 754 SMP_CACHE_BYTES); 755 if (!xbc_nodes) { 756 pr_err("Failed to allocate memory for bootconfig nodes.\n"); 757 return -ENOMEM; 758 } 759 memset(xbc_nodes, 0, sizeof(struct xbc_node) * XBC_NODE_MAX); 760 xbc_data = buf; 761 xbc_data_size = ret + 1; 762 last_parent = NULL; 763 764 p = buf; 765 do { 766 q = strpbrk(p, "{}=;\n#"); 767 if (!q) { 768 p = skip_spaces(p); 769 if (*p != '\0') 770 ret = xbc_parse_error("No delimiter", p); 771 break; 772 } 773 774 c = *q; 775 *q++ = '\0'; 776 switch (c) { 777 case '=': 778 ret = xbc_parse_kv(&p, q); 779 break; 780 case '{': 781 ret = xbc_open_brace(&p, q); 782 break; 783 case '#': 784 q = skip_comment(q); 785 /* fall through */ 786 case ';': 787 case '\n': 788 ret = xbc_parse_key(&p, q); 789 break; 790 case '}': 791 ret = xbc_close_brace(&p, q); 792 break; 793 } 794 } while (!ret); 795 796 if (!ret) 797 ret = xbc_verify_tree(); 798 799 if (ret < 0) 800 xbc_destroy_all(); 801 else 802 ret = xbc_node_num; 803 804 return ret; 805 } 806 807 /** 808 * xbc_debug_dump() - Dump current XBC node list 809 * 810 * Dump the current XBC node list on printk buffer for debug. 811 */ 812 void __init xbc_debug_dump(void) 813 { 814 int i; 815 816 for (i = 0; i < xbc_node_num; i++) { 817 pr_debug("[%d] %s (%s) .next=%d, .child=%d .parent=%d\n", i, 818 xbc_node_get_data(xbc_nodes + i), 819 xbc_node_is_value(xbc_nodes + i) ? "value" : "key", 820 xbc_nodes[i].next, xbc_nodes[i].child, 821 xbc_nodes[i].parent); 822 } 823 } 824