bootconfig.c 22 KB

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