main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Boot config tool for initrd image
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <endian.h>
  14. #include <linux/bootconfig.h>
  15. #define pr_err(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
  16. static int xbc_show_value(struct xbc_node *node, bool semicolon)
  17. {
  18. const char *val, *eol;
  19. char q;
  20. int i = 0;
  21. eol = semicolon ? ";\n" : "\n";
  22. xbc_array_for_each_value(node, val) {
  23. if (strchr(val, '"'))
  24. q = '\'';
  25. else
  26. q = '"';
  27. printf("%c%s%c%s", q, val, q, xbc_node_is_array(node) ? ", " : eol);
  28. i++;
  29. }
  30. return i;
  31. }
  32. static void xbc_show_compact_tree(void)
  33. {
  34. struct xbc_node *node, *cnode = NULL, *vnode;
  35. int depth = 0, i;
  36. node = xbc_root_node();
  37. while (node && xbc_node_is_key(node)) {
  38. for (i = 0; i < depth; i++)
  39. printf("\t");
  40. if (!cnode)
  41. cnode = xbc_node_get_child(node);
  42. while (cnode && xbc_node_is_key(cnode) && !cnode->next) {
  43. vnode = xbc_node_get_child(cnode);
  44. /*
  45. * If @cnode has value and subkeys, this
  46. * should show it as below.
  47. *
  48. * key(@node) {
  49. * key(@cnode) = value;
  50. * key(@cnode) {
  51. * subkeys;
  52. * }
  53. * }
  54. */
  55. if (vnode && xbc_node_is_value(vnode) && vnode->next)
  56. break;
  57. printf("%s.", xbc_node_get_data(node));
  58. node = cnode;
  59. cnode = vnode;
  60. }
  61. if (cnode && xbc_node_is_key(cnode)) {
  62. printf("%s {\n", xbc_node_get_data(node));
  63. depth++;
  64. node = cnode;
  65. cnode = NULL;
  66. continue;
  67. } else if (cnode && xbc_node_is_value(cnode)) {
  68. printf("%s = ", xbc_node_get_data(node));
  69. xbc_show_value(cnode, true);
  70. /*
  71. * If @node has value and subkeys, continue
  72. * looping on subkeys with same node.
  73. */
  74. if (cnode->next) {
  75. cnode = xbc_node_get_next(cnode);
  76. continue;
  77. }
  78. } else {
  79. printf("%s;\n", xbc_node_get_data(node));
  80. }
  81. cnode = NULL;
  82. if (node->next) {
  83. node = xbc_node_get_next(node);
  84. continue;
  85. }
  86. while (!node->next) {
  87. node = xbc_node_get_parent(node);
  88. if (!node)
  89. return;
  90. if (!xbc_node_get_child(node)->next)
  91. continue;
  92. if (depth) {
  93. depth--;
  94. for (i = 0; i < depth; i++)
  95. printf("\t");
  96. printf("}\n");
  97. }
  98. }
  99. node = xbc_node_get_next(node);
  100. }
  101. }
  102. static void xbc_show_list(void)
  103. {
  104. char key[XBC_KEYLEN_MAX];
  105. struct xbc_node *leaf;
  106. const char *val;
  107. int ret;
  108. xbc_for_each_key_value(leaf, val) {
  109. ret = xbc_node_compose_key(leaf, key, XBC_KEYLEN_MAX);
  110. if (ret < 0) {
  111. fprintf(stderr, "Failed to compose key %d\n", ret);
  112. break;
  113. }
  114. printf("%s = ", key);
  115. if (!val || val[0] == '\0') {
  116. printf("\"\"\n");
  117. continue;
  118. }
  119. xbc_show_value(xbc_node_get_child(leaf), false);
  120. }
  121. }
  122. #define PAGE_SIZE 4096
  123. static int load_xbc_fd(int fd, char **buf, int size)
  124. {
  125. int ret;
  126. *buf = malloc(size + 1);
  127. if (!*buf)
  128. return -ENOMEM;
  129. ret = read(fd, *buf, size);
  130. if (ret < 0)
  131. return -errno;
  132. (*buf)[size] = '\0';
  133. return ret;
  134. }
  135. /* Return the read size or -errno */
  136. static int load_xbc_file(const char *path, char **buf)
  137. {
  138. struct stat stat;
  139. int fd, ret;
  140. fd = open(path, O_RDONLY);
  141. if (fd < 0)
  142. return -errno;
  143. ret = fstat(fd, &stat);
  144. if (ret < 0)
  145. return -errno;
  146. ret = load_xbc_fd(fd, buf, stat.st_size);
  147. close(fd);
  148. return ret;
  149. }
  150. static int pr_errno(const char *msg, int err)
  151. {
  152. pr_err("%s: %d\n", msg, err);
  153. return err;
  154. }
  155. static int load_xbc_from_initrd(int fd, char **buf)
  156. {
  157. struct stat stat;
  158. int ret;
  159. uint32_t size = 0, csum = 0, rcsum;
  160. char magic[BOOTCONFIG_MAGIC_LEN];
  161. const char *msg;
  162. ret = fstat(fd, &stat);
  163. if (ret < 0)
  164. return -errno;
  165. if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
  166. return 0;
  167. if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
  168. return pr_errno("Failed to lseek for magic", -errno);
  169. if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
  170. return pr_errno("Failed to read", -errno);
  171. /* Check the bootconfig magic bytes */
  172. if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
  173. return 0;
  174. if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0)
  175. return pr_errno("Failed to lseek for size", -errno);
  176. if (read(fd, &size, sizeof(uint32_t)) < 0)
  177. return pr_errno("Failed to read size", -errno);
  178. size = le32toh(size);
  179. if (read(fd, &csum, sizeof(uint32_t)) < 0)
  180. return pr_errno("Failed to read checksum", -errno);
  181. csum = le32toh(csum);
  182. /* Wrong size error */
  183. if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
  184. pr_err("bootconfig size is too big\n");
  185. return -E2BIG;
  186. }
  187. if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
  188. SEEK_SET) < 0)
  189. return pr_errno("Failed to lseek", -errno);
  190. ret = load_xbc_fd(fd, buf, size);
  191. if (ret < 0)
  192. return ret;
  193. /* Wrong Checksum */
  194. rcsum = xbc_calc_checksum(*buf, size);
  195. if (csum != rcsum) {
  196. pr_err("checksum error: %d != %d\n", csum, rcsum);
  197. return -EINVAL;
  198. }
  199. ret = xbc_init(*buf, size, &msg, NULL);
  200. /* Wrong data */
  201. if (ret < 0) {
  202. pr_err("parse error: %s.\n", msg);
  203. return ret;
  204. }
  205. return size;
  206. }
  207. static void show_xbc_error(const char *data, const char *msg, int pos)
  208. {
  209. int lin = 1, col, i;
  210. if (pos < 0) {
  211. pr_err("Error: %s.\n", msg);
  212. return;
  213. }
  214. /* Note that pos starts from 0 but lin and col should start from 1. */
  215. col = pos + 1;
  216. for (i = 0; i < pos; i++) {
  217. if (data[i] == '\n') {
  218. lin++;
  219. col = pos - i;
  220. }
  221. }
  222. pr_err("Parse Error: %s at %d:%d\n", msg, lin, col);
  223. }
  224. static int init_xbc_with_error(char *buf, int len)
  225. {
  226. char *copy = strdup(buf);
  227. const char *msg;
  228. int ret, pos;
  229. if (!copy)
  230. return -ENOMEM;
  231. ret = xbc_init(buf, len, &msg, &pos);
  232. if (ret < 0)
  233. show_xbc_error(copy, msg, pos);
  234. free(copy);
  235. return ret;
  236. }
  237. static int show_xbc(const char *path, bool list)
  238. {
  239. int ret, fd;
  240. char *buf = NULL;
  241. struct stat st;
  242. ret = stat(path, &st);
  243. if (ret < 0) {
  244. ret = -errno;
  245. pr_err("Failed to stat %s: %d\n", path, ret);
  246. return ret;
  247. }
  248. fd = open(path, O_RDONLY);
  249. if (fd < 0) {
  250. ret = -errno;
  251. pr_err("Failed to open initrd %s: %d\n", path, ret);
  252. return ret;
  253. }
  254. ret = load_xbc_from_initrd(fd, &buf);
  255. close(fd);
  256. if (ret < 0) {
  257. pr_err("Failed to load a boot config from initrd: %d\n", ret);
  258. goto out;
  259. }
  260. /* Assume a bootconfig file if it is enough small */
  261. if (ret == 0 && st.st_size <= XBC_DATA_MAX) {
  262. ret = load_xbc_file(path, &buf);
  263. if (ret < 0) {
  264. pr_err("Failed to load a boot config: %d\n", ret);
  265. goto out;
  266. }
  267. if (init_xbc_with_error(buf, ret) < 0)
  268. goto out;
  269. }
  270. if (list)
  271. xbc_show_list();
  272. else
  273. xbc_show_compact_tree();
  274. ret = 0;
  275. out:
  276. free(buf);
  277. return ret;
  278. }
  279. static int delete_xbc(const char *path)
  280. {
  281. struct stat stat;
  282. int ret = 0, fd, size;
  283. char *buf = NULL;
  284. fd = open(path, O_RDWR);
  285. if (fd < 0) {
  286. ret = -errno;
  287. pr_err("Failed to open initrd %s: %d\n", path, ret);
  288. return ret;
  289. }
  290. size = load_xbc_from_initrd(fd, &buf);
  291. if (size < 0) {
  292. ret = size;
  293. pr_err("Failed to load a boot config from initrd: %d\n", ret);
  294. } else if (size > 0) {
  295. ret = fstat(fd, &stat);
  296. if (!ret)
  297. ret = ftruncate(fd, stat.st_size
  298. - size - 8 - BOOTCONFIG_MAGIC_LEN);
  299. if (ret)
  300. ret = -errno;
  301. } /* Ignore if there is no boot config in initrd */
  302. close(fd);
  303. free(buf);
  304. return ret;
  305. }
  306. static int apply_xbc(const char *path, const char *xbc_path)
  307. {
  308. char *buf, *data, *p;
  309. size_t total_size;
  310. struct stat stat;
  311. const char *msg;
  312. uint32_t size, csum;
  313. int pos, pad;
  314. int ret, fd;
  315. ret = load_xbc_file(xbc_path, &buf);
  316. if (ret < 0) {
  317. pr_err("Failed to load %s : %d\n", xbc_path, ret);
  318. return ret;
  319. }
  320. size = strlen(buf) + 1;
  321. csum = xbc_calc_checksum(buf, size);
  322. /* Backup the bootconfig data */
  323. data = calloc(size + BOOTCONFIG_ALIGN +
  324. sizeof(uint32_t) + sizeof(uint32_t) + BOOTCONFIG_MAGIC_LEN, 1);
  325. if (!data)
  326. return -ENOMEM;
  327. memcpy(data, buf, size);
  328. /* Check the data format */
  329. ret = xbc_init(buf, size, &msg, &pos);
  330. if (ret < 0) {
  331. show_xbc_error(data, msg, pos);
  332. free(data);
  333. free(buf);
  334. return ret;
  335. }
  336. printf("Apply %s to %s\n", xbc_path, path);
  337. xbc_get_info(&ret, NULL);
  338. printf("\tNumber of nodes: %d\n", ret);
  339. printf("\tSize: %u bytes\n", (unsigned int)size);
  340. printf("\tChecksum: %d\n", (unsigned int)csum);
  341. /* TODO: Check the options by schema */
  342. xbc_exit();
  343. free(buf);
  344. /* Remove old boot config if exists */
  345. ret = delete_xbc(path);
  346. if (ret < 0) {
  347. pr_err("Failed to delete previous boot config: %d\n", ret);
  348. free(data);
  349. return ret;
  350. }
  351. /* Apply new one */
  352. fd = open(path, O_RDWR | O_APPEND);
  353. if (fd < 0) {
  354. ret = -errno;
  355. pr_err("Failed to open %s: %d\n", path, ret);
  356. free(data);
  357. return ret;
  358. }
  359. /* TODO: Ensure the @path is initramfs/initrd image */
  360. if (fstat(fd, &stat) < 0) {
  361. ret = -errno;
  362. pr_err("Failed to get the size of %s\n", path);
  363. goto out;
  364. }
  365. /* To align up the total size to BOOTCONFIG_ALIGN, get padding size */
  366. total_size = stat.st_size + size + sizeof(uint32_t) * 2 + BOOTCONFIG_MAGIC_LEN;
  367. pad = ((total_size + BOOTCONFIG_ALIGN - 1) & (~BOOTCONFIG_ALIGN_MASK)) - total_size;
  368. size += pad;
  369. /* Add a footer */
  370. p = data + size;
  371. *(uint32_t *)p = htole32(size);
  372. p += sizeof(uint32_t);
  373. *(uint32_t *)p = htole32(csum);
  374. p += sizeof(uint32_t);
  375. memcpy(p, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
  376. p += BOOTCONFIG_MAGIC_LEN;
  377. total_size = p - data;
  378. ret = write(fd, data, total_size);
  379. if (ret < total_size) {
  380. if (ret < 0)
  381. ret = -errno;
  382. pr_err("Failed to apply a boot config: %d\n", ret);
  383. if (ret >= 0)
  384. goto out_rollback;
  385. } else
  386. ret = 0;
  387. out:
  388. close(fd);
  389. free(data);
  390. return ret;
  391. out_rollback:
  392. /* Map the partial write to -ENOSPC */
  393. if (ret >= 0)
  394. ret = -ENOSPC;
  395. if (ftruncate(fd, stat.st_size) < 0) {
  396. ret = -errno;
  397. pr_err("Failed to rollback the write error: %d\n", ret);
  398. pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path);
  399. }
  400. goto out;
  401. }
  402. static int usage(void)
  403. {
  404. printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
  405. "Or bootconfig <CONFIG>\n"
  406. " Apply, delete or show boot config to initrd.\n"
  407. " Options:\n"
  408. " -a <config>: Apply boot config to initrd\n"
  409. " -d : Delete boot config file from initrd\n"
  410. " -l : list boot config in initrd or file\n\n"
  411. " If no option is given, show the bootconfig in the given file.\n");
  412. return -1;
  413. }
  414. int main(int argc, char **argv)
  415. {
  416. char *path = NULL;
  417. char *apply = NULL;
  418. bool delete = false, list = false;
  419. int opt;
  420. while ((opt = getopt(argc, argv, "hda:l")) != -1) {
  421. switch (opt) {
  422. case 'd':
  423. delete = true;
  424. break;
  425. case 'a':
  426. apply = optarg;
  427. break;
  428. case 'l':
  429. list = true;
  430. break;
  431. case 'h':
  432. default:
  433. return usage();
  434. }
  435. }
  436. if ((apply && delete) || (delete && list) || (apply && list)) {
  437. pr_err("Error: You can give one of -a, -d or -l at once.\n");
  438. return usage();
  439. }
  440. if (optind >= argc) {
  441. pr_err("Error: No initrd is specified.\n");
  442. return usage();
  443. }
  444. path = argv[optind];
  445. if (apply)
  446. return apply_xbc(path, apply);
  447. else if (delete)
  448. return delete_xbc(path);
  449. return show_xbc(path, list);
  450. }