1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Extra Boot Config 4 * Masami Hiramatsu <mhiramat@kernel.org> 5 */ 6 7 /* 8 * NOTE: This is only for tools/bootconfig, because tools/bootconfig will 9 * run the parser sanity test. 10 * This does NOT mean lib/bootconfig.c is available in the user space. 11 * However, if you change this file, please make sure the tools/bootconfig 12 * has no issue on building and running. 13 */ 14 #include <linux/bootconfig.h> 15 16 #ifdef __KERNEL__ 17 #include <linux/bug.h> 18 #include <linux/ctype.h> 19 #include <linux/errno.h> 20 #include <linux/cache.h> 21 #include <linux/compiler.h> 22 #include <linux/init.h> 23 #include <linux/moduleparam.h> 24 #include <linux/printk.h> 25 #include <linux/sprintf.h> 26 #include <linux/memblock.h> 27 #include <linux/string.h> 28 #include <asm/setup.h> /* COMMAND_LINE_SIZE */ 29 30 #ifdef CONFIG_BOOT_CONFIG_EMBED 31 /* embedded_bootconfig_data is defined in bootconfig-data.S */ 32 extern __visible const char embedded_bootconfig_data[]; 33 extern __visible const char embedded_bootconfig_data_end[]; 34 35 const char * __init xbc_get_embedded_bootconfig(size_t *size) 36 { 37 *size = embedded_bootconfig_data_end - embedded_bootconfig_data; 38 return (*size) ? embedded_bootconfig_data : NULL; 39 } 40 #endif 41 42 #ifdef CONFIG_CMDLINE_FROM_BOOTCONFIG 43 /* embedded_kernel_cmdline is defined in embedded-cmdline.S */ 44 extern __visible const char embedded_kernel_cmdline[]; 45 extern __visible const char embedded_kernel_cmdline_end[]; 46 47 /* Set once the embedded cmdline has actually been prepended. */ 48 static bool xbc_cmdline_applied __initdata; 49 50 /* 51 * str_prepend() - Prepend @src in front of the string in @dst, in place 52 * @dst: NUL-terminated destination buffer, currently @dst_len bytes long 53 * @dst_len: length of the current @dst string (excluding its NUL) 54 * @src: bytes to prepend (not NUL-terminated) 55 * @src_len: number of bytes from @src to prepend 56 * 57 * The caller must guarantee @dst has room for src_len + dst_len + 1 bytes. 58 * Moving dst_len + 1 bytes carries @dst's NUL terminator too, so an empty 59 * @dst needs no special case. 60 */ 61 static void __init str_prepend(char *dst, size_t dst_len, 62 const char *src, size_t src_len) 63 { 64 memmove(dst + src_len, dst, dst_len + 1); 65 memcpy(dst, src, src_len); 66 } 67 68 /** 69 * xbc_prepend_embedded_cmdline() - Prepend embedded bootconfig cmdline 70 * @dst: cmdline buffer to prepend into (must already contain a NUL byte) 71 * @size: total capacity of @dst in bytes 72 * 73 * Prepend the build-time-rendered "kernel" subtree of the embedded 74 * bootconfig to @dst. The rendered string already ends with a single 75 * space (the xbc_snprint_cmdline() invariant), which serves as the 76 * separator between the embedded keys and any existing content of @dst. 77 * On overflow, log an error and leave @dst untouched rather than 78 * silently truncating: booting without the embedded values is better 79 * than refusing to boot, and the error message tells the user why 80 * their embedded keys are missing. 81 * 82 * Intended to be called from setup_arch() before parse_early_param() so 83 * that early_param() handlers see the embedded values. 84 */ 85 void __init xbc_prepend_embedded_cmdline(char *dst, size_t size) 86 { 87 size_t embed_len = embedded_kernel_cmdline_end - embedded_kernel_cmdline; 88 size_t dst_len; 89 90 if (!size || embed_len <= 1) /* trailing NUL only */ 91 return; 92 embed_len--; /* exclude trailing NUL byte */ 93 94 dst_len = strnlen(dst, size); 95 if (embed_len + dst_len + 1 > size) { 96 pr_err("embedded bootconfig cmdline (%zu bytes) does not fit in COMMAND_LINE_SIZE with %zu bytes already used; ignoring embedded values\n", 97 embed_len, dst_len); 98 return; 99 } 100 101 str_prepend(dst, dst_len, embedded_kernel_cmdline, embed_len); 102 xbc_cmdline_applied = true; 103 } 104 105 /** 106 * xbc_embedded_cmdline_applied() - Did the embedded cmdline get prepended? 107 * 108 * Return true if xbc_prepend_embedded_cmdline() actually prepended the 109 * embedded "kernel" subtree. setup_boot_config() uses this to avoid 110 * rendering the same keys a second time. 111 */ 112 bool __init xbc_embedded_cmdline_applied(void) 113 { 114 return xbc_cmdline_applied; 115 } 116 #endif /* CONFIG_CMDLINE_FROM_BOOTCONFIG */ 117 118 /* parse_args() callback: flag when the "bootconfig" parameter is present. */ 119 static int __init bootconfig_optin(char *param, char *val, 120 const char *unused, void *arg) 121 { 122 if (!strcmp(param, "bootconfig")) 123 *(bool *)arg = true; 124 return 0; 125 } 126 127 /** 128 * bootconfig_cmdline_requested() - Was "bootconfig" passed on the cmdline? 129 * @boot_cmdline: kernel command line to inspect (not modified) 130 * @end_offset: if non-NULL, set to the offset of the init arguments that 131 * follow a "--" separator, or 0 when there is none 132 * 133 * Parse a private copy of @boot_cmdline (parse_args() is destructive) and 134 * report whether "bootconfig" is present before the "--" separator. 135 * setup_arch() uses this to gate prepending the build-time embedded cmdline; 136 * setup_boot_config() uses it for the runtime opt-in and to locate the init 137 * arguments via @end_offset. Sharing one parser keeps the early and late 138 * paths agreeing on what counts as opt-in. CONFIG_BOOT_CONFIG_FORCE is not 139 * folded in here; callers apply it where they need it. 140 */ 141 bool __init bootconfig_cmdline_requested(const char *boot_cmdline, int *end_offset) 142 { 143 static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata; 144 bool found = false; 145 char *err; 146 147 if (end_offset) 148 *end_offset = 0; 149 150 strscpy(tmp_cmdline, boot_cmdline, COMMAND_LINE_SIZE); 151 err = parse_args("bootconfig", tmp_cmdline, NULL, 0, 0, 0, 152 &found, bootconfig_optin); 153 if (IS_ERR(err)) 154 return false; 155 156 /* parse_args() stops at "--" and returns the address of the rest. */ 157 if (end_offset && err) 158 *end_offset = err - tmp_cmdline; 159 160 return found; 161 } 162 163 #endif /* __KERNEL__ */ 164 165 /* 166 * Extra Boot Config (XBC) is given as tree-structured ascii text of 167 * key-value pairs on memory. 168 * xbc_parse() parses the text to build a simple tree. Each tree node is 169 * simply a key word or a value. A key node may have a next key node or/and 170 * a child node (both key and value). A value node may have a next value 171 * node (for array). 172 */ 173 174 static struct xbc_node *xbc_nodes __initdata; 175 static int xbc_node_num __initdata; 176 static char *xbc_data __initdata; 177 static size_t xbc_data_size __initdata; 178 static struct xbc_node *last_parent __initdata; 179 static const char *xbc_err_msg __initdata; 180 static int xbc_err_pos __initdata; 181 static int open_brace[XBC_DEPTH_MAX] __initdata; 182 static int brace_index __initdata; 183 184 #ifdef __KERNEL__ 185 static inline void * __init xbc_alloc_mem(size_t size) 186 { 187 return memblock_alloc(size, SMP_CACHE_BYTES); 188 } 189 190 static inline void __init xbc_free_mem(void *addr, size_t size, bool early) 191 { 192 if (early) 193 memblock_free(addr, size); 194 else if (addr) 195 memblock_free(addr, size); 196 } 197 198 #else /* !__KERNEL__ */ 199 200 static inline void *xbc_alloc_mem(size_t size) 201 { 202 return calloc(1, size); 203 } 204 205 static inline void xbc_free_mem(void *addr, size_t size, bool early) 206 { 207 free(addr); 208 } 209 #endif 210 211 /** 212 * xbc_get_info() - Get the information of loaded boot config 213 * @node_size: A pointer to store the number of nodes. 214 * @data_size: A pointer to store the size of bootconfig data. 215 * 216 * Get the number of used nodes in @node_size if it is not NULL, 217 * and the size of bootconfig data in @data_size if it is not NULL. 218 * Return 0 if the boot config is initialized, or return -ENODEV. 219 */ 220 int __init xbc_get_info(int *node_size, size_t *data_size) 221 { 222 if (!xbc_data) 223 return -ENODEV; 224 225 if (node_size) 226 *node_size = xbc_node_num; 227 if (data_size) 228 *data_size = xbc_data_size; 229 return 0; 230 } 231 232 static int __init xbc_parse_error(const char *msg, const char *p) 233 { 234 xbc_err_msg = msg; 235 xbc_err_pos = (int)(p - xbc_data); 236 237 return -EINVAL; 238 } 239 240 /** 241 * xbc_root_node() - Get the root node of extended boot config 242 * 243 * Return the address of root node of extended boot config. If the 244 * extended boot config is not initialized, return NULL. 245 */ 246 struct xbc_node * __init xbc_root_node(void) 247 { 248 if (unlikely(!xbc_data)) 249 return NULL; 250 251 return xbc_nodes; 252 } 253 254 /** 255 * xbc_node_index() - Get the index of XBC node 256 * @node: A target node of getting index. 257 * 258 * Return the index number of @node in XBC node list. 259 */ 260 uint16_t __init xbc_node_index(struct xbc_node *node) 261 { 262 return (uint16_t)(node - &xbc_nodes[0]); 263 } 264 265 /** 266 * xbc_node_get_parent() - Get the parent XBC node 267 * @node: An XBC node. 268 * 269 * Return the parent node of @node. If the node is top node of the tree, 270 * return NULL. 271 */ 272 struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node) 273 { 274 return node->parent == XBC_NODE_MAX ? NULL : &xbc_nodes[node->parent]; 275 } 276 277 /** 278 * xbc_node_get_child() - Get the child XBC node 279 * @node: An XBC node. 280 * 281 * Return the first child node of @node. If the node has no child, return 282 * NULL. 283 */ 284 struct xbc_node * __init xbc_node_get_child(struct xbc_node *node) 285 { 286 return node->child ? &xbc_nodes[node->child] : NULL; 287 } 288 289 /** 290 * xbc_node_get_next() - Get the next sibling XBC node 291 * @node: An XBC node. 292 * 293 * Return the NEXT sibling node of @node. If the node has no next sibling, 294 * return NULL. Note that even if this returns NULL, it doesn't mean @node 295 * has no siblings. (You also has to check whether the parent's child node 296 * is @node or not.) 297 */ 298 struct xbc_node * __init xbc_node_get_next(struct xbc_node *node) 299 { 300 return node->next ? &xbc_nodes[node->next] : NULL; 301 } 302 303 /** 304 * xbc_node_get_data() - Get the data of XBC node 305 * @node: An XBC node. 306 * 307 * Return the data (which is always a null terminated string) of @node. 308 * If the node has invalid data, warn and return NULL. 309 */ 310 const char * __init xbc_node_get_data(struct xbc_node *node) 311 { 312 size_t offset = node->data & ~XBC_VALUE; 313 314 if (WARN_ON(offset >= xbc_data_size)) 315 return NULL; 316 317 return xbc_data + offset; 318 } 319 320 static bool __init 321 xbc_node_match_prefix(struct xbc_node *node, const char **prefix) 322 { 323 const char *p = xbc_node_get_data(node); 324 size_t len = strlen(p); 325 326 if (strncmp(*prefix, p, len)) 327 return false; 328 329 p = *prefix + len; 330 if (*p == '.') 331 p++; 332 else if (*p != '\0') 333 return false; 334 *prefix = p; 335 336 return true; 337 } 338 339 /** 340 * xbc_node_find_subkey() - Find a subkey node which matches given key 341 * @parent: An XBC node. 342 * @key: A key string. 343 * 344 * Search a key node under @parent which matches @key. The @key can contain 345 * several words jointed with '.'. If @parent is NULL, this searches the 346 * node from whole tree. Return NULL if no node is matched. 347 */ 348 struct xbc_node * __init 349 xbc_node_find_subkey(struct xbc_node *parent, const char *key) 350 { 351 struct xbc_node *node; 352 353 if (parent) 354 node = xbc_node_get_subkey(parent); 355 else 356 node = xbc_root_node(); 357 358 while (node && xbc_node_is_key(node)) { 359 if (!xbc_node_match_prefix(node, &key)) 360 node = xbc_node_get_next(node); 361 else if (*key != '\0') 362 node = xbc_node_get_subkey(node); 363 else 364 break; 365 } 366 367 return node; 368 } 369 370 /** 371 * xbc_node_find_value() - Find a value node which matches given key 372 * @parent: An XBC node. 373 * @key: A key string. 374 * @vnode: A container pointer of found XBC node. 375 * 376 * Search a value node under @parent whose (parent) key node matches @key, 377 * store it in *@vnode, and returns the value string. 378 * The @key can contain several words jointed with '.'. If @parent is NULL, 379 * this searches the node from whole tree. Return the value string if a 380 * matched key found, return NULL if no node is matched. 381 * Note that this returns 0-length string and stores NULL in *@vnode if the 382 * key has no value. And also it will return the value of the first entry if 383 * the value is an array. 384 */ 385 const char * __init 386 xbc_node_find_value(struct xbc_node *parent, const char *key, 387 struct xbc_node **vnode) 388 { 389 struct xbc_node *node = xbc_node_find_subkey(parent, key); 390 391 if (!node || !xbc_node_is_key(node)) 392 return NULL; 393 394 node = xbc_node_get_child(node); 395 if (node && !xbc_node_is_value(node)) 396 return NULL; 397 398 if (vnode) 399 *vnode = node; 400 401 return node ? xbc_node_get_data(node) : ""; 402 } 403 404 /** 405 * xbc_node_compose_key_after() - Compose partial key string of the XBC node 406 * @root: Root XBC node 407 * @node: Target XBC node. 408 * @buf: A buffer to store the key. 409 * @size: The size of the @buf. 410 * 411 * Compose the partial key of the @node into @buf, which is starting right 412 * after @root (@root is not included.) If @root is NULL, this returns full 413 * key words of @node. 414 * Returns the total length of the key stored in @buf. Returns -EINVAL 415 * if @node is NULL or @root is not the ancestor of @node or @root is @node, 416 * or returns -ERANGE if the key depth is deeper than max depth. 417 * This is expected to be used with xbc_find_node() to list up all (child) 418 * keys under given key. 419 */ 420 int __init xbc_node_compose_key_after(struct xbc_node *root, 421 struct xbc_node *node, 422 char *buf, size_t size) 423 { 424 uint16_t keys[XBC_DEPTH_MAX]; 425 int depth = 0, ret = 0, total = 0; 426 427 if (!node || node == root) 428 return -EINVAL; 429 430 if (xbc_node_is_value(node)) 431 node = xbc_node_get_parent(node); 432 433 while (node && node != root) { 434 keys[depth++] = xbc_node_index(node); 435 if (depth == XBC_DEPTH_MAX) 436 return -ERANGE; 437 node = xbc_node_get_parent(node); 438 } 439 if (!node && root) 440 return -EINVAL; 441 442 while (--depth >= 0) { 443 node = xbc_nodes + keys[depth]; 444 ret = snprintf(buf, size, "%s%s", xbc_node_get_data(node), 445 depth ? "." : ""); 446 if (ret < 0) 447 return ret; 448 if (ret >= size) { 449 size = 0; 450 } else { 451 size -= ret; 452 buf += ret; 453 } 454 total += ret; 455 } 456 457 return total; 458 } 459 460 /** 461 * xbc_node_find_next_leaf() - Find the next leaf node under given node 462 * @root: An XBC root node 463 * @node: An XBC node which starts from. 464 * 465 * Search the next leaf node (which means the terminal key node) of @node 466 * under @root node (including @root node itself). 467 * Return the next node or NULL if next leaf node is not found. 468 */ 469 struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root, 470 struct xbc_node *node) 471 { 472 struct xbc_node *next; 473 474 if (unlikely(!xbc_data)) 475 return NULL; 476 477 if (!node) { /* First try */ 478 node = root; 479 if (!node) 480 node = xbc_nodes; 481 } else { 482 /* Leaf node may have a subkey */ 483 next = xbc_node_get_subkey(node); 484 if (next) { 485 node = next; 486 goto found; 487 } 488 489 if (node == root) /* @root was a leaf, no child node. */ 490 return NULL; 491 492 while (!node->next) { 493 node = xbc_node_get_parent(node); 494 if (node == root) 495 return NULL; 496 /* User passed a node which is not under parent */ 497 if (WARN_ON(!node)) 498 return NULL; 499 } 500 node = xbc_node_get_next(node); 501 } 502 503 found: 504 while (node && !xbc_node_is_leaf(node)) 505 node = xbc_node_get_child(node); 506 507 return node; 508 } 509 510 /** 511 * xbc_node_find_next_key_value() - Find the next key-value pair nodes 512 * @root: An XBC root node 513 * @leaf: A container pointer of XBC node which starts from. 514 * 515 * Search the next leaf node (which means the terminal key node) of *@leaf 516 * under @root node. Returns the value and update *@leaf if next leaf node 517 * is found, or NULL if no next leaf node is found. 518 * Note that this returns 0-length string if the key has no value, or 519 * the value of the first entry if the value is an array. 520 */ 521 const char * __init xbc_node_find_next_key_value(struct xbc_node *root, 522 struct xbc_node **leaf) 523 { 524 /* tip must be passed */ 525 if (WARN_ON(!leaf)) 526 return NULL; 527 528 *leaf = xbc_node_find_next_leaf(root, *leaf); 529 if (!*leaf) 530 return NULL; 531 if ((*leaf)->child) 532 return xbc_node_get_data(xbc_node_get_child(*leaf)); 533 else 534 return ""; /* No value key */ 535 } 536 537 static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata; 538 539 #define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0) 540 541 /** 542 * xbc_snprint_cmdline() - Render bootconfig keys under @root as a cmdline string 543 * @buf: Destination buffer (may be NULL when @size is 0 to query the length) 544 * @size: Size of @buf in bytes 545 * @root: Subtree root whose key=value pairs should be rendered 546 * 547 * Walk all key/value pairs under @root and emit them as a space-separated 548 * cmdline string into @buf. Values containing whitespace are quoted with 549 * double quotes. Returns the number of bytes that would be written if @buf 550 * were large enough (matching snprintf semantics), or a negative errno on 551 * failure. 552 */ 553 int __init xbc_snprint_cmdline(char *buf, size_t size, struct xbc_node *root) 554 { 555 struct xbc_node *knode, *vnode; 556 const char *val, *q; 557 size_t len = 0; 558 int ret; 559 560 /* 561 * Track the running written length rather than advancing @buf, so we 562 * never form "buf + size" or "buf += ret" while @buf is NULL (the 563 * size-probe call passes buf=NULL, size=0). NULL pointer arithmetic 564 * is undefined behavior and trips host UBSan / FORTIFY_SOURCE when 565 * this renderer runs at kernel build time. snprintf(NULL, 0, ...) 566 * itself is well defined and returns the would-be length. 567 */ 568 xbc_node_for_each_key_value(root, knode, val) { 569 /* 570 * An empty or value-only @root (e.g. "kernel {}" or 571 * "kernel = x", possibly alongside "kernel.foo = bar") 572 * yields @root itself here. Skip it: composing a key for it 573 * would fail with -EINVAL, yet any real descendant keys must 574 * still be rendered. An entirely empty subtree then renders 575 * nothing and returns 0 rather than an error. 576 */ 577 if (knode == root) 578 continue; 579 580 ret = xbc_node_compose_key_after(root, knode, 581 xbc_namebuf, XBC_KEYLEN_MAX); 582 if (ret < 0) 583 return ret; 584 585 vnode = xbc_node_get_child(knode); 586 if (!vnode) { 587 ret = snprintf(buf ? buf + len : NULL, rest(len, size), 588 "%s ", xbc_namebuf); 589 if (ret < 0) 590 return ret; 591 len += ret; 592 continue; 593 } 594 xbc_array_for_each_value(vnode, val) { 595 /* 596 * For prettier and more readable /proc/cmdline, only 597 * quote the value when necessary, i.e. when it contains 598 * whitespace. 599 */ 600 q = strpbrk(val, " \t\r\n") ? "\"" : ""; 601 ret = snprintf(buf ? buf + len : NULL, rest(len, size), 602 "%s=%s%s%s ", xbc_namebuf, q, val, q); 603 if (ret < 0) 604 return ret; 605 len += ret; 606 } 607 } 608 609 return len; 610 } 611 #undef rest 612 613 /* XBC parse and tree build */ 614 615 static int __init xbc_init_node(struct xbc_node *node, char *data, uint16_t flag) 616 { 617 long offset = data - xbc_data; 618 619 if (WARN_ON(offset < 0 || offset >= XBC_DATA_MAX)) 620 return -EINVAL; 621 622 node->data = (uint16_t)offset | flag; 623 node->child = 0; 624 node->next = 0; 625 626 return 0; 627 } 628 629 static struct xbc_node * __init xbc_add_node(char *data, uint16_t flag) 630 { 631 struct xbc_node *node; 632 633 if (xbc_node_num == XBC_NODE_MAX) 634 return NULL; 635 636 node = &xbc_nodes[xbc_node_num]; 637 if (xbc_init_node(node, data, flag) < 0) 638 return NULL; 639 xbc_node_num++; 640 641 return node; 642 } 643 644 static inline __init struct xbc_node *xbc_last_sibling(struct xbc_node *node) 645 { 646 while (node->next) 647 node = xbc_node_get_next(node); 648 649 return node; 650 } 651 652 static inline __init struct xbc_node *xbc_last_child(struct xbc_node *node) 653 { 654 while (node->child) 655 node = xbc_node_get_child(node); 656 657 return node; 658 } 659 660 static struct xbc_node * __init __xbc_add_sibling(char *data, uint16_t flag, bool head) 661 { 662 struct xbc_node *sib, *node = xbc_add_node(data, flag); 663 664 if (node) { 665 if (!last_parent) { 666 /* Ignore @head in this case */ 667 node->parent = XBC_NODE_MAX; 668 sib = xbc_last_sibling(xbc_nodes); 669 sib->next = xbc_node_index(node); 670 } else { 671 node->parent = xbc_node_index(last_parent); 672 if (!last_parent->child || head) { 673 node->next = last_parent->child; 674 last_parent->child = xbc_node_index(node); 675 } else { 676 sib = xbc_node_get_child(last_parent); 677 sib = xbc_last_sibling(sib); 678 sib->next = xbc_node_index(node); 679 } 680 } 681 } else { 682 xbc_parse_error("Too many nodes", data); 683 } 684 685 return node; 686 } 687 688 static inline struct xbc_node * __init xbc_add_sibling(char *data, uint16_t flag) 689 { 690 return __xbc_add_sibling(data, flag, false); 691 } 692 693 static inline struct xbc_node * __init xbc_add_head_sibling(char *data, uint16_t flag) 694 { 695 return __xbc_add_sibling(data, flag, true); 696 } 697 698 static inline __init struct xbc_node *xbc_add_child(char *data, uint16_t flag) 699 { 700 struct xbc_node *node = xbc_add_sibling(data, flag); 701 702 if (node) 703 last_parent = node; 704 705 return node; 706 } 707 708 static inline __init bool xbc_valid_keyword(char *key) 709 { 710 if (key[0] == '\0') 711 return false; 712 713 while (isalnum(*key) || *key == '-' || *key == '_') 714 key++; 715 716 return *key == '\0'; 717 } 718 719 static char *skip_comment(char *p) 720 { 721 char *ret; 722 723 ret = strchr(p, '\n'); 724 if (!ret) 725 ret = p + strlen(p); 726 else 727 ret++; 728 729 return ret; 730 } 731 732 static char *skip_spaces_until_newline(char *p) 733 { 734 while (isspace(*p) && *p != '\n') 735 p++; 736 return p; 737 } 738 739 static int __init __xbc_open_brace(char *p) 740 { 741 /* Push the last key as open brace */ 742 if (brace_index >= XBC_DEPTH_MAX) 743 return xbc_parse_error("Exceed max depth of braces", p); 744 open_brace[brace_index++] = xbc_node_index(last_parent); 745 746 return 0; 747 } 748 749 static int __init __xbc_close_brace(char *p) 750 { 751 brace_index--; 752 if (!last_parent || brace_index < 0 || 753 (open_brace[brace_index] != xbc_node_index(last_parent))) 754 return xbc_parse_error("Unexpected closing brace", p); 755 756 if (brace_index == 0) 757 last_parent = NULL; 758 else 759 last_parent = &xbc_nodes[open_brace[brace_index - 1]]; 760 761 return 0; 762 } 763 764 /* 765 * Return delimiter or error, no node added. As same as lib/cmdline.c, 766 * you can use " around spaces, but can't escape " for value. 767 * *@__v must point real value string. (not including spaces before value.) 768 */ 769 static int __init __xbc_parse_value(char **__v, char **__n) 770 { 771 char *p, *v = *__v; 772 int c, quotes = 0; 773 774 if (*v == '"' || *v == '\'') { 775 quotes = *v; 776 v++; 777 } 778 p = v - 1; 779 while ((c = *++p)) { 780 if (!isprint(c) && !isspace(c)) 781 return xbc_parse_error("Non printable value", p); 782 if (quotes) { 783 if (c != quotes) 784 continue; 785 quotes = 0; 786 *p++ = '\0'; 787 p = skip_spaces_until_newline(p); 788 c = *p; 789 if (c && !strchr(",;\n#}", c)) 790 return xbc_parse_error("No value delimiter", p); 791 if (*p) 792 p++; 793 break; 794 } 795 if (strchr(",;\n#}", c)) { 796 *p++ = '\0'; 797 v = strim(v); 798 break; 799 } 800 } 801 if (quotes) 802 return xbc_parse_error("No closing quotes", p); 803 if (c == '#') { 804 p = skip_comment(p); 805 c = '\n'; /* A comment must be treated as a newline */ 806 } 807 *__n = p; 808 *__v = v; 809 810 return c; 811 } 812 813 static int __init xbc_parse_array(char **__v) 814 { 815 struct xbc_node *node; 816 char *next; 817 int c = 0; 818 819 if (last_parent->child) 820 last_parent = xbc_node_get_child(last_parent); 821 822 do { 823 /* Search the next array value beyond comments and empty lines */ 824 next = skip_spaces(*__v); 825 while (*next == '#') { 826 next = skip_comment(next); 827 next = skip_spaces(next); 828 } 829 *__v = next; 830 c = __xbc_parse_value(__v, &next); 831 if (c < 0) 832 return c; 833 834 node = xbc_add_child(*__v, XBC_VALUE); 835 if (!node) 836 return -ENOMEM; 837 *__v = next; 838 } while (c == ','); 839 node->child = 0; 840 841 return c; 842 } 843 844 static inline __init 845 struct xbc_node *find_match_node(struct xbc_node *node, char *k) 846 { 847 while (node) { 848 if (!strcmp(xbc_node_get_data(node), k)) 849 break; 850 node = xbc_node_get_next(node); 851 } 852 return node; 853 } 854 855 static int __init __xbc_add_key(char *k) 856 { 857 struct xbc_node *node, *child; 858 859 if (!xbc_valid_keyword(k)) 860 return xbc_parse_error("Invalid keyword", k); 861 862 if (unlikely(xbc_node_num == 0)) 863 goto add_node; 864 865 if (!last_parent) { /* the first level */ 866 node = find_match_node(xbc_nodes, k); 867 } else { 868 child = xbc_node_get_child(last_parent); 869 /* Since the value node is the first child, skip it. */ 870 if (child && xbc_node_is_value(child)) 871 child = xbc_node_get_next(child); 872 node = find_match_node(child, k); 873 } 874 875 if (node) { 876 last_parent = node; 877 } else { 878 add_node: 879 node = xbc_add_child(k, XBC_KEY); 880 if (!node) 881 return -ENOMEM; 882 } 883 return 0; 884 } 885 886 static int __init __xbc_parse_keys(char *k) 887 { 888 char *p; 889 int ret; 890 891 k = strim(k); 892 while ((p = strchr(k, '.'))) { 893 *p++ = '\0'; 894 ret = __xbc_add_key(k); 895 if (ret) 896 return ret; 897 k = p; 898 } 899 900 return __xbc_add_key(k); 901 } 902 903 static int __init xbc_parse_kv(char **k, char *v, int op) 904 { 905 struct xbc_node *prev_parent = last_parent; 906 struct xbc_node *child; 907 char *next; 908 int c, ret; 909 910 ret = __xbc_parse_keys(*k); 911 if (ret) 912 return ret; 913 914 v = skip_spaces_until_newline(v); 915 /* If there is a comment, this has an empty value. */ 916 if (*v == '#') { 917 next = skip_comment(v); 918 *v = '\0'; 919 c = '\n'; 920 } else { 921 c = __xbc_parse_value(&v, &next); 922 if (c < 0) 923 return c; 924 } 925 926 child = xbc_node_get_child(last_parent); 927 if (child && xbc_node_is_value(child)) { 928 if (op == '=') 929 return xbc_parse_error("Value is redefined", v); 930 if (op == ':') { 931 unsigned short nidx = child->next; 932 933 if (xbc_init_node(child, v, XBC_VALUE) < 0) 934 return xbc_parse_error("Failed to override value", v); 935 child->next = nidx; /* keep subkeys */ 936 goto array; 937 } 938 /* op must be '+' */ 939 last_parent = xbc_last_child(child); 940 } 941 /* The value node should always be the first child */ 942 if (!xbc_add_head_sibling(v, XBC_VALUE)) 943 return -ENOMEM; 944 945 array: 946 if (c == ',') { /* Array */ 947 c = xbc_parse_array(&next); 948 if (c < 0) 949 return c; 950 } 951 952 last_parent = prev_parent; 953 954 if (c == '}') { 955 ret = __xbc_close_brace(next - 1); 956 if (ret < 0) 957 return ret; 958 } 959 960 *k = next; 961 962 return 0; 963 } 964 965 static int __init xbc_parse_key(char **k, char *n) 966 { 967 struct xbc_node *prev_parent = last_parent; 968 int ret; 969 970 *k = strim(*k); 971 if (**k != '\0') { 972 ret = __xbc_parse_keys(*k); 973 if (ret) 974 return ret; 975 last_parent = prev_parent; 976 } 977 *k = n; 978 979 return 0; 980 } 981 982 static int __init xbc_open_brace(char **k, char *n) 983 { 984 int ret; 985 986 ret = __xbc_parse_keys(*k); 987 if (ret) 988 return ret; 989 *k = n; 990 991 return __xbc_open_brace(n - 1); 992 } 993 994 static int __init xbc_close_brace(char **k, char *n) 995 { 996 int ret; 997 998 ret = xbc_parse_key(k, n); 999 if (ret) 1000 return ret; 1001 /* k is updated in xbc_parse_key() */ 1002 1003 return __xbc_close_brace(n - 1); 1004 } 1005 1006 static int __init xbc_verify_tree(void) 1007 { 1008 int i, depth; 1009 size_t len, wlen; 1010 struct xbc_node *n, *m; 1011 1012 /* Brace closing */ 1013 if (brace_index) { 1014 n = &xbc_nodes[open_brace[brace_index - 1]]; 1015 return xbc_parse_error("Brace is not closed", 1016 xbc_node_get_data(n)); 1017 } 1018 1019 /* Empty tree */ 1020 if (xbc_node_num == 0) { 1021 xbc_parse_error("Empty config", xbc_data); 1022 return -ENOENT; 1023 } 1024 1025 for (i = 0; i < xbc_node_num; i++) { 1026 if (xbc_nodes[i].next >= xbc_node_num) { 1027 return xbc_parse_error("No closing brace", 1028 xbc_node_get_data(xbc_nodes + i)); 1029 } 1030 if (xbc_nodes[i].child >= xbc_node_num) { 1031 return xbc_parse_error("Broken child node", 1032 xbc_node_get_data(xbc_nodes + i)); 1033 } 1034 } 1035 1036 /* Key tree limitation check */ 1037 n = &xbc_nodes[0]; 1038 depth = 1; 1039 len = 0; 1040 1041 while (n) { 1042 wlen = strlen(xbc_node_get_data(n)) + 1; 1043 len += wlen; 1044 if (len > XBC_KEYLEN_MAX) 1045 return xbc_parse_error("Too long key length", 1046 xbc_node_get_data(n)); 1047 1048 m = xbc_node_get_child(n); 1049 if (m && xbc_node_is_key(m)) { 1050 n = m; 1051 depth++; 1052 if (depth > XBC_DEPTH_MAX) 1053 return xbc_parse_error("Too many key words", 1054 xbc_node_get_data(n)); 1055 continue; 1056 } 1057 len -= wlen; 1058 m = xbc_node_get_next(n); 1059 while (!m) { 1060 n = xbc_node_get_parent(n); 1061 if (!n) 1062 break; 1063 len -= strlen(xbc_node_get_data(n)) + 1; 1064 depth--; 1065 m = xbc_node_get_next(n); 1066 } 1067 n = m; 1068 } 1069 1070 return 0; 1071 } 1072 1073 /* Need to setup xbc_data and xbc_nodes before call this. */ 1074 static int __init xbc_parse_tree(void) 1075 { 1076 char *p, *q; 1077 int ret = 0, c; 1078 1079 last_parent = NULL; 1080 p = xbc_data; 1081 do { 1082 q = strpbrk(p, "{}=+;:\n#"); 1083 if (!q) { 1084 p = skip_spaces(p); 1085 if (*p != '\0') 1086 ret = xbc_parse_error("No delimiter", p); 1087 break; 1088 } 1089 1090 c = *q; 1091 *q++ = '\0'; 1092 switch (c) { 1093 case ':': 1094 case '+': 1095 if (*q++ != '=') { 1096 ret = xbc_parse_error(c == '+' ? 1097 "Wrong '+' operator" : 1098 "Wrong ':' operator", 1099 q - 2); 1100 break; 1101 } 1102 fallthrough; 1103 case '=': 1104 ret = xbc_parse_kv(&p, q, c); 1105 break; 1106 case '{': 1107 ret = xbc_open_brace(&p, q); 1108 break; 1109 case '#': 1110 q = skip_comment(q); 1111 fallthrough; 1112 case ';': 1113 case '\n': 1114 ret = xbc_parse_key(&p, q); 1115 break; 1116 case '}': 1117 ret = xbc_close_brace(&p, q); 1118 break; 1119 } 1120 } while (!ret); 1121 1122 return ret; 1123 } 1124 1125 /** 1126 * _xbc_exit() - Clean up all parsed bootconfig 1127 * @early: Set true if this is called before budy system is initialized. 1128 * 1129 * This clears all data structures of parsed bootconfig on memory. 1130 * If you need to reuse xbc_init() with new boot config, you can 1131 * use this. 1132 */ 1133 void __init _xbc_exit(bool early) 1134 { 1135 xbc_free_mem(xbc_data, xbc_data_size, early); 1136 xbc_data = NULL; 1137 xbc_data_size = 0; 1138 xbc_node_num = 0; 1139 xbc_free_mem(xbc_nodes, sizeof(struct xbc_node) * XBC_NODE_MAX, early); 1140 xbc_nodes = NULL; 1141 brace_index = 0; 1142 } 1143 1144 /** 1145 * xbc_init() - Parse given XBC file and build XBC internal tree 1146 * @data: The boot config text original data 1147 * @size: The size of @data 1148 * @emsg: A pointer of const char * to store the error message 1149 * @epos: A pointer of int to store the error position 1150 * 1151 * This parses the boot config text in @data. @size must be smaller 1152 * than XBC_DATA_MAX. 1153 * Return the number of stored nodes (>0) if succeeded, or -errno 1154 * if there is any error. 1155 * In error cases, @emsg will be updated with an error message and 1156 * @epos will be updated with the error position which is the byte offset 1157 * of @buf. If the error is not a parser error, @epos will be -1. 1158 */ 1159 int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos) 1160 { 1161 int ret; 1162 1163 if (epos) 1164 *epos = -1; 1165 1166 if (xbc_data) { 1167 if (emsg) 1168 *emsg = "Bootconfig is already initialized"; 1169 return -EBUSY; 1170 } 1171 if (size > XBC_DATA_MAX || size == 0) { 1172 if (emsg) 1173 *emsg = size ? "Config data is too big" : 1174 "Config data is empty"; 1175 return -ERANGE; 1176 } 1177 1178 xbc_data = xbc_alloc_mem(size + 1); 1179 if (!xbc_data) { 1180 if (emsg) 1181 *emsg = "Failed to allocate bootconfig data"; 1182 return -ENOMEM; 1183 } 1184 memcpy(xbc_data, data, size); 1185 xbc_data[size] = '\0'; 1186 xbc_data_size = size + 1; 1187 1188 xbc_nodes = xbc_alloc_mem(sizeof(struct xbc_node) * XBC_NODE_MAX); 1189 if (!xbc_nodes) { 1190 if (emsg) 1191 *emsg = "Failed to allocate bootconfig nodes"; 1192 _xbc_exit(true); 1193 return -ENOMEM; 1194 } 1195 1196 ret = xbc_parse_tree(); 1197 if (!ret) 1198 ret = xbc_verify_tree(); 1199 1200 if (ret < 0) { 1201 if (epos) 1202 *epos = xbc_err_pos; 1203 if (emsg) 1204 *emsg = xbc_err_msg; 1205 _xbc_exit(true); 1206 } else { 1207 ret = xbc_node_num; 1208 } 1209 1210 return ret; 1211 } 1212