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) 191 { 192 memblock_free(addr, size); 193 } 194 195 #else /* !__KERNEL__ */ 196 197 static inline void *xbc_alloc_mem(size_t size) 198 { 199 return calloc(1, size); 200 } 201 202 static inline void xbc_free_mem(void *addr, size_t size) 203 { 204 free(addr); 205 } 206 #endif 207 208 /** 209 * xbc_get_info() - Get the information of loaded boot config 210 * @node_size: A pointer to store the number of nodes. 211 * @data_size: A pointer to store the size of bootconfig data. 212 * 213 * Get the number of used nodes in @node_size if it is not NULL, 214 * and the size of bootconfig data in @data_size if it is not NULL. 215 * Return 0 if the boot config is initialized, or return -ENODEV. 216 */ 217 int __init xbc_get_info(int *node_size, size_t *data_size) 218 { 219 if (!xbc_data) 220 return -ENODEV; 221 222 if (node_size) 223 *node_size = xbc_node_num; 224 if (data_size) 225 *data_size = xbc_data_size; 226 return 0; 227 } 228 229 static int __init xbc_parse_error(const char *msg, const char *p) 230 { 231 xbc_err_msg = msg; 232 xbc_err_pos = (int)(p - xbc_data); 233 234 return -EINVAL; 235 } 236 237 /** 238 * xbc_root_node() - Get the root node of extended boot config 239 * 240 * Return the address of root node of extended boot config. If the 241 * extended boot config is not initialized, return NULL. 242 */ 243 struct xbc_node * __init xbc_root_node(void) 244 { 245 if (unlikely(!xbc_data)) 246 return NULL; 247 248 return xbc_nodes; 249 } 250 251 /** 252 * xbc_node_index() - Get the index of XBC node 253 * @node: A target node of getting index. 254 * 255 * Return the index number of @node in XBC node list. 256 */ 257 uint16_t __init xbc_node_index(struct xbc_node *node) 258 { 259 return (uint16_t)(node - &xbc_nodes[0]); 260 } 261 262 /** 263 * xbc_node_get_parent() - Get the parent XBC node 264 * @node: An XBC node. 265 * 266 * Return the parent node of @node. If the node is top node of the tree, 267 * return NULL. 268 */ 269 struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node) 270 { 271 return node->parent == XBC_NODE_MAX ? NULL : &xbc_nodes[node->parent]; 272 } 273 274 /** 275 * xbc_node_get_child() - Get the child XBC node 276 * @node: An XBC node. 277 * 278 * Return the first child node of @node. If the node has no child, return 279 * NULL. 280 */ 281 struct xbc_node * __init xbc_node_get_child(struct xbc_node *node) 282 { 283 return node->child ? &xbc_nodes[node->child] : NULL; 284 } 285 286 /** 287 * xbc_node_get_next() - Get the next sibling XBC node 288 * @node: An XBC node. 289 * 290 * Return the NEXT sibling node of @node. If the node has no next sibling, 291 * return NULL. Note that even if this returns NULL, it doesn't mean @node 292 * has no siblings. (You also has to check whether the parent's child node 293 * is @node or not.) 294 */ 295 struct xbc_node * __init xbc_node_get_next(struct xbc_node *node) 296 { 297 return node->next ? &xbc_nodes[node->next] : NULL; 298 } 299 300 /** 301 * xbc_node_get_data() - Get the data of XBC node 302 * @node: An XBC node. 303 * 304 * Return the data (which is always a null terminated string) of @node. 305 * If the node has invalid data, warn and return NULL. 306 */ 307 const char * __init xbc_node_get_data(struct xbc_node *node) 308 { 309 size_t offset = node->data & ~XBC_VALUE; 310 311 if (WARN_ON(offset >= xbc_data_size)) 312 return NULL; 313 314 return xbc_data + offset; 315 } 316 317 static bool __init 318 xbc_node_match_prefix(struct xbc_node *node, const char **prefix) 319 { 320 const char *p = xbc_node_get_data(node); 321 size_t len = strlen(p); 322 323 if (strncmp(*prefix, p, len)) 324 return false; 325 326 p = *prefix + len; 327 if (*p == '.') 328 p++; 329 else if (*p != '\0') 330 return false; 331 *prefix = p; 332 333 return true; 334 } 335 336 /** 337 * xbc_node_find_subkey() - Find a subkey node which matches given key 338 * @parent: An XBC node. 339 * @key: A key string. 340 * 341 * Search a key node under @parent which matches @key. The @key can contain 342 * several words jointed with '.'. If @parent is NULL, this searches the 343 * node from whole tree. Return NULL if no node is matched. 344 */ 345 struct xbc_node * __init 346 xbc_node_find_subkey(struct xbc_node *parent, const char *key) 347 { 348 struct xbc_node *node; 349 350 if (parent) 351 node = xbc_node_get_subkey(parent); 352 else 353 node = xbc_root_node(); 354 355 while (node && xbc_node_is_key(node)) { 356 if (!xbc_node_match_prefix(node, &key)) 357 node = xbc_node_get_next(node); 358 else if (*key != '\0') 359 node = xbc_node_get_subkey(node); 360 else 361 break; 362 } 363 364 return node; 365 } 366 367 /** 368 * xbc_node_find_value() - Find a value node which matches given key 369 * @parent: An XBC node. 370 * @key: A key string. 371 * @vnode: A container pointer of found XBC node. 372 * 373 * Search a value node under @parent whose (parent) key node matches @key, 374 * store it in *@vnode, and returns the value string. 375 * The @key can contain several words jointed with '.'. If @parent is NULL, 376 * this searches the node from whole tree. Return the value string if a 377 * matched key found, return NULL if no node is matched. 378 * Note that this returns 0-length string and stores NULL in *@vnode if the 379 * key has no value. And also it will return the value of the first entry if 380 * the value is an array. 381 */ 382 const char * __init 383 xbc_node_find_value(struct xbc_node *parent, const char *key, 384 struct xbc_node **vnode) 385 { 386 struct xbc_node *node = xbc_node_find_subkey(parent, key); 387 388 if (!node || !xbc_node_is_key(node)) 389 return NULL; 390 391 node = xbc_node_get_child(node); 392 if (node && !xbc_node_is_value(node)) 393 return NULL; 394 395 if (vnode) 396 *vnode = node; 397 398 return node ? xbc_node_get_data(node) : ""; 399 } 400 401 /** 402 * xbc_node_compose_key_after() - Compose partial key string of the XBC node 403 * @root: Root XBC node 404 * @node: Target XBC node. 405 * @buf: A buffer to store the key. 406 * @size: The size of the @buf. 407 * 408 * Compose the partial key of the @node into @buf, which is starting right 409 * after @root (@root is not included.) If @root is NULL, this returns full 410 * key words of @node. 411 * Returns the total length of the key stored in @buf. Returns -EINVAL 412 * if @node is NULL or @root is not the ancestor of @node or @root is @node, 413 * or returns -ERANGE if the key depth is deeper than max depth. 414 * This is expected to be used with xbc_find_node() to list up all (child) 415 * keys under given key. 416 */ 417 int __init xbc_node_compose_key_after(struct xbc_node *root, 418 struct xbc_node *node, 419 char *buf, size_t size) 420 { 421 uint16_t keys[XBC_DEPTH_MAX]; 422 int depth = 0, ret = 0, total = 0; 423 424 while (node && xbc_node_is_value(node)) 425 node = xbc_node_get_parent(node); 426 427 if (!node || node == root) 428 return -EINVAL; 429 430 while (node && node != root) { 431 if (depth == XBC_DEPTH_MAX) 432 return -ERANGE; 433 keys[depth++] = xbc_node_index(node); 434 node = xbc_node_get_parent(node); 435 } 436 if (!node && root) 437 return -EINVAL; 438 439 while (--depth >= 0) { 440 node = xbc_nodes + keys[depth]; 441 ret = snprintf(buf, size, "%s%s", xbc_node_get_data(node), 442 depth ? "." : ""); 443 if (ret < 0) 444 return ret; 445 if (ret >= size) { 446 size = 0; 447 } else { 448 size -= ret; 449 buf += ret; 450 } 451 total += ret; 452 } 453 454 return total; 455 } 456 457 /** 458 * xbc_node_find_next_leaf() - Find the next leaf node under given node 459 * @root: An XBC root node 460 * @node: An XBC node which starts from. 461 * 462 * Search the next leaf node (which means the terminal key node) of @node 463 * under @root node (including @root node itself). 464 * Return the next node or NULL if next leaf node is not found. 465 */ 466 struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root, 467 struct xbc_node *node) 468 { 469 struct xbc_node *next; 470 471 if (unlikely(!xbc_data)) 472 return NULL; 473 474 if (!node) { /* First try */ 475 node = root; 476 if (!node) 477 node = xbc_nodes; 478 } else { 479 /* Leaf node may have a subkey */ 480 next = xbc_node_get_subkey(node); 481 if (next) { 482 node = next; 483 goto found; 484 } 485 486 if (node == root) /* @root was a leaf, no child node. */ 487 return NULL; 488 489 while (!node->next) { 490 node = xbc_node_get_parent(node); 491 if (node == root) 492 return NULL; 493 /* User passed a node which is not under parent */ 494 if (WARN_ON(!node)) 495 return NULL; 496 } 497 node = xbc_node_get_next(node); 498 } 499 500 found: 501 while (node && !xbc_node_is_leaf(node)) 502 node = xbc_node_get_child(node); 503 504 return node; 505 } 506 507 /** 508 * xbc_node_find_next_key_value() - Find the next key-value pair nodes 509 * @root: An XBC root node 510 * @leaf: A container pointer of XBC node which starts from. 511 * 512 * Search the next leaf node (which means the terminal key node) of *@leaf 513 * under @root node. Returns the value and update *@leaf if next leaf node 514 * is found, or NULL if no next leaf node is found. 515 * Note that this returns 0-length string if the key has no value, or 516 * the value of the first entry if the value is an array. 517 */ 518 const char * __init xbc_node_find_next_key_value(struct xbc_node *root, 519 struct xbc_node **leaf) 520 { 521 /* tip must be passed */ 522 if (WARN_ON(!leaf)) 523 return NULL; 524 525 *leaf = xbc_node_find_next_leaf(root, *leaf); 526 if (!*leaf) 527 return NULL; 528 if ((*leaf)->child) 529 return xbc_node_get_data(xbc_node_get_child(*leaf)); 530 else 531 return ""; /* No value key */ 532 } 533 534 static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata; 535 536 #define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0) 537 538 /** 539 * xbc_snprint_cmdline_filter() - Render bootconfig keys as cmdline string 540 * @buf: Destination buffer (may be NULL when @size is 0 to query the length) 541 * @size: Size of @buf in bytes 542 * @root: Subtree root whose key=value pairs should be rendered 543 * @filter: Filter callback (returns true to include, false to skip; may be NULL) 544 * @data: Private context passed to @filter 545 * 546 * Walk all key/value pairs under @root and emit them as a space-separated 547 * cmdline string into @buf. Values containing whitespace are quoted with 548 * double quotes. Keys for which @filter returns false are omitted. 549 * Returns the number of bytes that would be written if @buf were large enough 550 * (matching snprintf semantics), or a negative errno on failure. 551 */ 552 int __init xbc_snprint_cmdline_filter(char *buf, size_t size, struct xbc_node *root, 553 xbc_cmdline_filter_fn filter, void *data) 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 if (filter && !filter(knode, xbc_namebuf, data)) 586 continue; 587 588 vnode = xbc_node_get_child(knode); 589 if (!vnode) { 590 ret = snprintf(buf && len < size ? buf + len : NULL, 591 rest(len, size), "%s ", xbc_namebuf); 592 if (ret < 0) 593 return ret; 594 len += ret; 595 continue; 596 } 597 xbc_array_for_each_value(vnode, val) { 598 /* 599 * For prettier and more readable /proc/cmdline, only 600 * quote the value when necessary, i.e. when it contains 601 * whitespace. 602 */ 603 q = strpbrk(val, " \t\r\n") ? "\"" : ""; 604 ret = snprintf(buf && len < size ? buf + len : NULL, 605 rest(len, size), 606 "%s=%s%s%s ", xbc_namebuf, q, val, q); 607 if (ret < 0) 608 return ret; 609 len += ret; 610 } 611 } 612 613 return len; 614 } 615 #undef rest 616 617 /* XBC parse and tree build */ 618 619 static int __init xbc_init_node(struct xbc_node *node, char *data, uint16_t flag) 620 { 621 long offset = data - xbc_data; 622 623 if (WARN_ON(offset < 0 || offset >= XBC_DATA_MAX)) 624 return -EINVAL; 625 626 node->data = (uint16_t)offset | flag; 627 node->child = 0; 628 node->next = 0; 629 630 return 0; 631 } 632 633 static struct xbc_node * __init xbc_add_node(char *data, uint16_t flag) 634 { 635 struct xbc_node *node; 636 637 if (xbc_node_num == XBC_NODE_MAX) 638 return NULL; 639 640 node = &xbc_nodes[xbc_node_num]; 641 if (xbc_init_node(node, data, flag) < 0) 642 return NULL; 643 xbc_node_num++; 644 645 return node; 646 } 647 648 static inline __init struct xbc_node *xbc_last_sibling(struct xbc_node *node) 649 { 650 while (node->next) 651 node = xbc_node_get_next(node); 652 653 return node; 654 } 655 656 static inline __init struct xbc_node *xbc_last_child(struct xbc_node *node) 657 { 658 while (node->child) 659 node = xbc_node_get_child(node); 660 661 return node; 662 } 663 664 static struct xbc_node * __init __xbc_add_sibling(char *data, uint16_t flag, bool head) 665 { 666 struct xbc_node *sib, *node = xbc_add_node(data, flag); 667 668 if (node) { 669 if (!last_parent) { 670 /* Ignore @head in this case */ 671 node->parent = XBC_NODE_MAX; 672 sib = xbc_last_sibling(xbc_nodes); 673 sib->next = xbc_node_index(node); 674 } else { 675 node->parent = xbc_node_index(last_parent); 676 if (!last_parent->child || head) { 677 node->next = last_parent->child; 678 last_parent->child = xbc_node_index(node); 679 } else { 680 sib = xbc_node_get_child(last_parent); 681 sib = xbc_last_sibling(sib); 682 sib->next = xbc_node_index(node); 683 } 684 } 685 } else { 686 xbc_parse_error("Too many nodes", data); 687 } 688 689 return node; 690 } 691 692 static inline struct xbc_node * __init xbc_add_sibling(char *data, uint16_t flag) 693 { 694 return __xbc_add_sibling(data, flag, false); 695 } 696 697 static inline struct xbc_node * __init xbc_add_head_sibling(char *data, uint16_t flag) 698 { 699 return __xbc_add_sibling(data, flag, true); 700 } 701 702 static inline __init struct xbc_node *xbc_add_child(char *data, uint16_t flag) 703 { 704 struct xbc_node *node = xbc_add_sibling(data, flag); 705 706 if (node) 707 last_parent = node; 708 709 return node; 710 } 711 712 static inline __init bool xbc_valid_keyword(char *key) 713 { 714 if (key[0] == '\0') 715 return false; 716 717 while (isalnum(*key) || *key == '-' || *key == '_') 718 key++; 719 720 return *key == '\0'; 721 } 722 723 static char *skip_comment(char *p) 724 { 725 char *ret; 726 727 ret = strchr(p, '\n'); 728 if (!ret) 729 ret = p + strlen(p); 730 else 731 ret++; 732 733 return ret; 734 } 735 736 static char *skip_spaces_until_newline(char *p) 737 { 738 while (isspace(*p) && *p != '\n') 739 p++; 740 return p; 741 } 742 743 static int __init __xbc_open_brace(char *p) 744 { 745 /* Push the last key as open brace */ 746 if (brace_index >= XBC_DEPTH_MAX) 747 return xbc_parse_error("Exceed max depth of braces", p); 748 open_brace[brace_index++] = xbc_node_index(last_parent); 749 750 return 0; 751 } 752 753 static int __init __xbc_close_brace(char *p) 754 { 755 brace_index--; 756 if (!last_parent || brace_index < 0 || 757 (open_brace[brace_index] != xbc_node_index(last_parent))) 758 return xbc_parse_error("Unexpected closing brace", p); 759 760 if (brace_index == 0) 761 last_parent = NULL; 762 else 763 last_parent = &xbc_nodes[open_brace[brace_index - 1]]; 764 765 return 0; 766 } 767 768 /* 769 * Return delimiter or error, no node added. As same as lib/cmdline.c, 770 * you can use " around spaces, but can't escape " for value. 771 * *@__v must point real value string. (not including spaces before value.) 772 */ 773 static int __init __xbc_parse_value(char **__v, char **__n) 774 { 775 char *p, *v = *__v; 776 int c, quotes = 0; 777 778 if (*v == '"' || *v == '\'') { 779 quotes = *v; 780 v++; 781 } 782 p = v - 1; 783 while ((c = *++p)) { 784 if (!isprint(c) && !isspace(c)) 785 return xbc_parse_error("Non printable value", p); 786 if (quotes) { 787 if (c != quotes) 788 continue; 789 quotes = 0; 790 *p++ = '\0'; 791 p = skip_spaces_until_newline(p); 792 c = *p; 793 if (c && !strchr(",;\n#}", c)) 794 return xbc_parse_error("No value delimiter", p); 795 if (*p) 796 p++; 797 goto skip_trim; 798 } 799 if (strchr(",;\n#}", c)) { 800 *p++ = '\0'; 801 break; 802 } 803 } 804 if (quotes) 805 return xbc_parse_error("No closing quotes", p); 806 v = strim(v); 807 skip_trim: 808 if (c == '#') { 809 p = skip_comment(p); 810 c = '\n'; /* A comment must be treated as a newline */ 811 } 812 *__n = p; 813 *__v = v; 814 815 return c; 816 } 817 818 static int __init xbc_parse_array(char **__v) 819 { 820 struct xbc_node *node; 821 char *next; 822 int c = 0; 823 824 if (last_parent->child) 825 last_parent = xbc_node_get_child(last_parent); 826 827 do { 828 /* Search the next array value beyond comments and empty lines */ 829 next = skip_spaces(*__v); 830 while (*next == '#') { 831 next = skip_comment(next); 832 next = skip_spaces(next); 833 } 834 *__v = next; 835 c = __xbc_parse_value(__v, &next); 836 if (c < 0) 837 return c; 838 839 node = xbc_add_child(*__v, XBC_VALUE); 840 if (!node) 841 return -ENOMEM; 842 *__v = next; 843 } while (c == ','); 844 845 return c; 846 } 847 848 static inline __init 849 struct xbc_node *find_match_node(struct xbc_node *node, char *k) 850 { 851 while (node) { 852 if (!strcmp(xbc_node_get_data(node), k)) 853 break; 854 node = xbc_node_get_next(node); 855 } 856 return node; 857 } 858 859 static int __init __xbc_add_key(char *k) 860 { 861 struct xbc_node *node, *child; 862 863 if (!xbc_valid_keyword(k)) 864 return xbc_parse_error("Invalid keyword", k); 865 866 if (unlikely(xbc_node_num == 0)) 867 goto add_node; 868 869 if (!last_parent) { /* the first level */ 870 node = find_match_node(xbc_nodes, k); 871 } else { 872 child = xbc_node_get_child(last_parent); 873 /* Since the value node is the first child, skip it. */ 874 if (child && xbc_node_is_value(child)) 875 child = xbc_node_get_next(child); 876 node = find_match_node(child, k); 877 } 878 879 if (node) { 880 last_parent = node; 881 } else { 882 add_node: 883 node = xbc_add_child(k, XBC_KEY); 884 if (!node) 885 return -ENOMEM; 886 } 887 return 0; 888 } 889 890 static int __init __xbc_parse_keys(char *k) 891 { 892 char *p; 893 int ret; 894 895 k = strim(k); 896 while ((p = strchr(k, '.'))) { 897 *p++ = '\0'; 898 ret = __xbc_add_key(k); 899 if (ret) 900 return ret; 901 k = p; 902 } 903 904 return __xbc_add_key(k); 905 } 906 907 static int __init xbc_parse_kv(char **k, char *v, int op) 908 { 909 struct xbc_node *prev_parent = last_parent; 910 struct xbc_node *child; 911 char *next; 912 int c, ret; 913 914 ret = __xbc_parse_keys(*k); 915 if (ret) 916 return ret; 917 918 v = skip_spaces_until_newline(v); 919 /* If there is a comment, this has an empty value. */ 920 if (*v == '#') { 921 next = skip_comment(v); 922 *v = '\0'; 923 c = '\n'; 924 } else { 925 c = __xbc_parse_value(&v, &next); 926 if (c < 0) 927 return c; 928 } 929 930 child = xbc_node_get_child(last_parent); 931 if (child && xbc_node_is_value(child)) { 932 if (op == '=') 933 return xbc_parse_error("Value is redefined", v); 934 if (op == ':') { 935 unsigned short nidx = child->next; 936 937 if (xbc_init_node(child, v, XBC_VALUE) < 0) 938 return xbc_parse_error("Failed to override value", v); 939 child->next = nidx; /* keep subkeys */ 940 goto array; 941 } 942 /* op must be '+' */ 943 last_parent = xbc_last_child(child); 944 } 945 /* The value node should always be the first child */ 946 if (!xbc_add_head_sibling(v, XBC_VALUE)) 947 return -ENOMEM; 948 949 array: 950 if (c == ',') { /* Array */ 951 c = xbc_parse_array(&next); 952 if (c < 0) 953 return c; 954 } 955 956 last_parent = prev_parent; 957 958 if (c == '}') { 959 ret = __xbc_close_brace(next - 1); 960 if (ret < 0) 961 return ret; 962 } 963 964 *k = next; 965 966 return 0; 967 } 968 969 static int __init xbc_parse_key(char **k, char *n) 970 { 971 struct xbc_node *prev_parent = last_parent; 972 int ret; 973 974 *k = strim(*k); 975 if (**k != '\0') { 976 ret = __xbc_parse_keys(*k); 977 if (ret) 978 return ret; 979 last_parent = prev_parent; 980 } 981 *k = n; 982 983 return 0; 984 } 985 986 static int __init xbc_open_brace(char **k, char *n) 987 { 988 int ret; 989 990 ret = __xbc_parse_keys(*k); 991 if (ret) 992 return ret; 993 *k = n; 994 995 return __xbc_open_brace(n - 1); 996 } 997 998 static int __init xbc_close_brace(char **k, char *n) 999 { 1000 int ret; 1001 1002 ret = xbc_parse_key(k, n); 1003 if (ret) 1004 return ret; 1005 /* k is updated in xbc_parse_key() */ 1006 1007 return __xbc_close_brace(n - 1); 1008 } 1009 1010 #ifndef __KERNEL__ 1011 /* Sanity check for regression: node indices must be within bounds */ 1012 static int __init xbc_sanity_check_tree(void) 1013 { 1014 int i; 1015 1016 for (i = 0; i < xbc_node_num; i++) { 1017 if (xbc_nodes[i].next >= xbc_node_num) { 1018 return xbc_parse_error("No closing brace", 1019 xbc_node_get_data(xbc_nodes + i)); 1020 } 1021 if (xbc_nodes[i].child >= xbc_node_num) { 1022 return xbc_parse_error("Broken child node", 1023 xbc_node_get_data(xbc_nodes + i)); 1024 } 1025 } 1026 1027 return 0; 1028 } 1029 #endif 1030 1031 static int __init xbc_verify_tree(void) 1032 { 1033 int depth; 1034 size_t len, wlen; 1035 struct xbc_node *n, *m; 1036 1037 /* Brace closing */ 1038 if (brace_index) { 1039 n = &xbc_nodes[open_brace[brace_index - 1]]; 1040 return xbc_parse_error("Brace is not closed", 1041 xbc_node_get_data(n)); 1042 } 1043 1044 /* Empty tree */ 1045 if (xbc_node_num == 0) { 1046 xbc_parse_error("Empty config", xbc_data); 1047 return -ENOENT; 1048 } 1049 1050 /* Key tree limitation check */ 1051 n = &xbc_nodes[0]; 1052 depth = 1; 1053 len = 0; 1054 1055 while (n) { 1056 wlen = strlen(xbc_node_get_data(n)) + 1; 1057 len += wlen; 1058 if (len > XBC_KEYLEN_MAX) 1059 return xbc_parse_error("Too long key length", 1060 xbc_node_get_data(n)); 1061 1062 m = xbc_node_get_subkey(n); 1063 if (m) { 1064 n = m; 1065 depth++; 1066 if (depth > XBC_DEPTH_MAX) 1067 return xbc_parse_error("Too many key words", 1068 xbc_node_get_data(n)); 1069 continue; 1070 } 1071 len -= wlen; 1072 m = xbc_node_get_next(n); 1073 while (!m) { 1074 n = xbc_node_get_parent(n); 1075 if (!n) 1076 break; 1077 len -= strlen(xbc_node_get_data(n)) + 1; 1078 depth--; 1079 m = xbc_node_get_next(n); 1080 } 1081 n = m; 1082 } 1083 1084 return 0; 1085 } 1086 1087 /* Need to setup xbc_data and xbc_nodes before call this. */ 1088 static int __init xbc_parse_tree(void) 1089 { 1090 char *p, *q; 1091 int ret = 0, c; 1092 1093 last_parent = NULL; 1094 p = xbc_data; 1095 do { 1096 q = strpbrk(p, "{}=+;:\n#"); 1097 if (!q) { 1098 p = skip_spaces(p); 1099 if (*p != '\0') 1100 ret = xbc_parse_error("No delimiter", p); 1101 break; 1102 } 1103 1104 c = *q; 1105 *q++ = '\0'; 1106 switch (c) { 1107 case ':': 1108 case '+': 1109 if (*q++ != '=') { 1110 ret = xbc_parse_error(c == '+' ? 1111 "Wrong '+' operator" : 1112 "Wrong ':' operator", 1113 q - 2); 1114 break; 1115 } 1116 fallthrough; 1117 case '=': 1118 ret = xbc_parse_kv(&p, q, c); 1119 break; 1120 case '{': 1121 ret = xbc_open_brace(&p, q); 1122 break; 1123 case '#': 1124 q = skip_comment(q); 1125 fallthrough; 1126 case ';': 1127 case '\n': 1128 ret = xbc_parse_key(&p, q); 1129 break; 1130 case '}': 1131 ret = xbc_close_brace(&p, q); 1132 break; 1133 } 1134 } while (!ret); 1135 1136 if (!ret) { 1137 while (p < xbc_data + xbc_data_size - 1 && *p == '\0') 1138 p++; 1139 if (p < xbc_data + xbc_data_size - 1) 1140 ret = xbc_parse_error("Unexpected data after null character", p); 1141 } 1142 1143 return ret; 1144 } 1145 1146 /** 1147 * xbc_exit() - Clean up all parsed bootconfig 1148 * 1149 * This clears all data structures of parsed bootconfig on memory. 1150 * If you need to reuse xbc_init() with new boot config, you can 1151 * use this. 1152 */ 1153 void __init xbc_exit(void) 1154 { 1155 xbc_free_mem(xbc_data, xbc_data_size); 1156 xbc_data = NULL; 1157 xbc_data_size = 0; 1158 xbc_node_num = 0; 1159 xbc_free_mem(xbc_nodes, sizeof(struct xbc_node) * XBC_NODE_MAX); 1160 xbc_nodes = NULL; 1161 brace_index = 0; 1162 } 1163 1164 /** 1165 * xbc_init() - Parse given XBC file and build XBC internal tree 1166 * @data: The boot config text original data 1167 * @size: The size of @data 1168 * @emsg: A pointer of const char * to store the error message 1169 * @epos: A pointer of int to store the error position 1170 * 1171 * This parses the boot config text in @data. @size must be smaller 1172 * than XBC_DATA_MAX. 1173 * Return the number of stored nodes (>0) if succeeded, or -errno 1174 * if there is any error. 1175 * In error cases, @emsg will be updated with an error message and 1176 * @epos will be updated with the error position which is the byte offset 1177 * of @buf. If the error is not a parser error, @epos will be -1. 1178 */ 1179 int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos) 1180 { 1181 int ret; 1182 1183 if (epos) 1184 *epos = -1; 1185 1186 if (xbc_data) { 1187 if (emsg) 1188 *emsg = "Bootconfig is already initialized"; 1189 return -EBUSY; 1190 } 1191 if (size > XBC_DATA_MAX || size == 0) { 1192 if (emsg) 1193 *emsg = size ? "Config data is too big" : 1194 "Config data is empty"; 1195 return -ERANGE; 1196 } 1197 1198 xbc_data = xbc_alloc_mem(size + 1); 1199 if (!xbc_data) { 1200 if (emsg) 1201 *emsg = "Failed to allocate bootconfig data"; 1202 return -ENOMEM; 1203 } 1204 memcpy(xbc_data, data, size); 1205 xbc_data[size] = '\0'; 1206 xbc_data_size = size + 1; 1207 1208 xbc_nodes = xbc_alloc_mem(sizeof(struct xbc_node) * XBC_NODE_MAX); 1209 if (!xbc_nodes) { 1210 if (emsg) 1211 *emsg = "Failed to allocate bootconfig nodes"; 1212 xbc_exit(); 1213 return -ENOMEM; 1214 } 1215 1216 ret = xbc_parse_tree(); 1217 if (!ret) 1218 ret = xbc_verify_tree(); 1219 #ifndef __KERNEL__ 1220 if (!ret) 1221 ret = xbc_sanity_check_tree(); 1222 #endif 1223 1224 if (ret < 0) { 1225 if (epos) 1226 *epos = xbc_err_pos; 1227 if (emsg) 1228 *emsg = xbc_err_msg; 1229 xbc_exit(); 1230 } else { 1231 ret = xbc_node_num; 1232 } 1233 1234 return ret; 1235 } 1236