bootconfig.c 20 KB

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