main.c 11 KB

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