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