bootconfig.c 22 KB

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