1 // SPDX-License-Identifier: GPL-2.0
3 * Boot config tool for initrd image
15 #include <linux/kernel.h>
16 #include <linux/bootconfig.h>
18 static int xbc_show_value(struct xbc_node *node, bool semicolon)
20 const char *val, *eol;
24 eol = semicolon ? ";\n" : "\n";
25 xbc_array_for_each_value(node, val) {
30 printf("%c%s%c%s", q, val, q, xbc_node_is_array(node) ? ", " : eol);
36 static void xbc_show_compact_tree(void)
38 struct xbc_node *node, *cnode = NULL, *vnode;
41 node = xbc_root_node();
42 while (node && xbc_node_is_key(node)) {
43 for (i = 0; i < depth; i++)
46 cnode = xbc_node_get_child(node);
47 while (cnode && xbc_node_is_key(cnode) && !cnode->next) {
48 vnode = xbc_node_get_child(cnode);
50 * If @cnode has value and subkeys, this
51 * should show it as below.
54 * key(@cnode) = value;
60 if (vnode && xbc_node_is_value(vnode) && vnode->next)
62 printf("%s.", xbc_node_get_data(node));
66 if (cnode && xbc_node_is_key(cnode)) {
67 printf("%s {\n", xbc_node_get_data(node));
72 } else if (cnode && xbc_node_is_value(cnode)) {
73 printf("%s = ", xbc_node_get_data(node));
74 xbc_show_value(cnode, true);
76 * If @node has value and subkeys, continue
77 * looping on subkeys with same node.
80 cnode = xbc_node_get_next(cnode);
84 printf("%s;\n", xbc_node_get_data(node));
89 node = xbc_node_get_next(node);
93 node = xbc_node_get_parent(node);
96 if (!xbc_node_get_child(node)->next)
100 for (i = 0; i < depth; i++)
105 node = xbc_node_get_next(node);
109 static void xbc_show_list(void)
111 char key[XBC_KEYLEN_MAX];
112 struct xbc_node *leaf;
115 xbc_for_each_key_value(leaf, val) {
116 if (xbc_node_compose_key(leaf, key, XBC_KEYLEN_MAX) < 0) {
117 fprintf(stderr, "Failed to compose key %d\n", ret);
120 printf("%s = ", key);
121 if (!val || val[0] == '\0') {
125 xbc_show_value(xbc_node_get_child(leaf), false);
129 #define PAGE_SIZE 4096
131 static int load_xbc_fd(int fd, char **buf, int size)
135 *buf = malloc(size + 1);
139 ret = read(fd, *buf, size);
147 /* Return the read size or -errno */
148 static int load_xbc_file(const char *path, char **buf)
153 fd = open(path, O_RDONLY);
156 ret = fstat(fd, &stat);
160 ret = load_xbc_fd(fd, buf, stat.st_size);
167 static int pr_errno(const char *msg, int err)
169 pr_err("%s: %d\n", msg, err);
173 static int load_xbc_from_initrd(int fd, char **buf)
177 u32 size = 0, csum = 0, rcsum;
178 char magic[BOOTCONFIG_MAGIC_LEN];
181 ret = fstat(fd, &stat);
185 if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
188 if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
189 return pr_errno("Failed to lseek for magic", -errno);
191 if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
192 return pr_errno("Failed to read", -errno);
194 /* Check the bootconfig magic bytes */
195 if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
198 if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0)
199 return pr_errno("Failed to lseek for size", -errno);
201 if (read(fd, &size, sizeof(u32)) < 0)
202 return pr_errno("Failed to read size", -errno);
203 size = le32toh(size);
205 if (read(fd, &csum, sizeof(u32)) < 0)
206 return pr_errno("Failed to read checksum", -errno);
207 csum = le32toh(csum);
209 /* Wrong size error */
210 if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
211 pr_err("bootconfig size is too big\n");
215 if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
217 return pr_errno("Failed to lseek", -errno);
219 ret = load_xbc_fd(fd, buf, size);
224 rcsum = xbc_calc_checksum(*buf, size);
226 pr_err("checksum error: %d != %d\n", csum, rcsum);
230 ret = xbc_init(*buf, &msg, NULL);
233 pr_err("parse error: %s.\n", msg);
240 static void show_xbc_error(const char *data, const char *msg, int pos)
245 pr_err("Error: %s.\n", msg);
249 /* Note that pos starts from 0 but lin and col should start from 1. */
251 for (i = 0; i < pos; i++) {
252 if (data[i] == '\n') {
257 pr_err("Parse Error: %s at %d:%d\n", msg, lin, col);
261 static int init_xbc_with_error(char *buf, int len)
263 char *copy = strdup(buf);
270 ret = xbc_init(buf, &msg, &pos);
272 show_xbc_error(copy, msg, pos);
278 static int show_xbc(const char *path, bool list)
284 ret = stat(path, &st);
287 pr_err("Failed to stat %s: %d\n", path, ret);
291 fd = open(path, O_RDONLY);
294 pr_err("Failed to open initrd %s: %d\n", path, ret);
298 ret = load_xbc_from_initrd(fd, &buf);
301 pr_err("Failed to load a boot config from initrd: %d\n", ret);
304 /* Assume a bootconfig file if it is enough small */
305 if (ret == 0 && st.st_size <= XBC_DATA_MAX) {
306 ret = load_xbc_file(path, &buf);
308 pr_err("Failed to load a boot config: %d\n", ret);
311 if (init_xbc_with_error(buf, ret) < 0)
317 xbc_show_compact_tree();
325 static int delete_xbc(const char *path)
328 int ret = 0, fd, size;
331 fd = open(path, O_RDWR);
334 pr_err("Failed to open initrd %s: %d\n", path, ret);
338 size = load_xbc_from_initrd(fd, &buf);
341 pr_err("Failed to load a boot config from initrd: %d\n", ret);
342 } else if (size > 0) {
343 ret = fstat(fd, &stat);
345 ret = ftruncate(fd, stat.st_size
346 - size - 8 - BOOTCONFIG_MAGIC_LEN);
349 } /* Ignore if there is no boot config in initrd */
357 static int apply_xbc(const char *path, const char *xbc_path)
359 char *buf, *data, *p;
367 ret = load_xbc_file(xbc_path, &buf);
369 pr_err("Failed to load %s : %d\n", xbc_path, ret);
372 size = strlen(buf) + 1;
373 csum = xbc_calc_checksum(buf, size);
375 /* Backup the bootconfig data */
376 data = calloc(size + BOOTCONFIG_ALIGN +
377 sizeof(u32) + sizeof(u32) + BOOTCONFIG_MAGIC_LEN, 1);
380 memcpy(data, buf, size);
382 /* Check the data format */
383 ret = xbc_init(buf, &msg, &pos);
385 show_xbc_error(data, msg, pos);
391 printf("Apply %s to %s\n", xbc_path, path);
392 printf("\tNumber of nodes: %d\n", ret);
393 printf("\tSize: %u bytes\n", (unsigned int)size);
394 printf("\tChecksum: %d\n", (unsigned int)csum);
396 /* TODO: Check the options by schema */
400 /* Remove old boot config if exists */
401 ret = delete_xbc(path);
403 pr_err("Failed to delete previous boot config: %d\n", ret);
409 fd = open(path, O_RDWR | O_APPEND);
412 pr_err("Failed to open %s: %d\n", path, ret);
416 /* TODO: Ensure the @path is initramfs/initrd image */
417 if (fstat(fd, &stat) < 0) {
419 pr_err("Failed to get the size of %s\n", path);
423 /* To align up the total size to BOOTCONFIG_ALIGN, get padding size */
424 total_size = stat.st_size + size + sizeof(u32) * 2 + BOOTCONFIG_MAGIC_LEN;
425 pad = ((total_size + BOOTCONFIG_ALIGN - 1) & (~BOOTCONFIG_ALIGN_MASK)) - total_size;
430 *(u32 *)p = htole32(size);
433 *(u32 *)p = htole32(csum);
436 memcpy(p, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
437 p += BOOTCONFIG_MAGIC_LEN;
439 total_size = p - data;
441 ret = write(fd, data, total_size);
442 if (ret < total_size) {
445 pr_err("Failed to apply a boot config: %d\n", ret);
458 /* Map the partial write to -ENOSPC */
461 if (ftruncate(fd, stat.st_size) < 0) {
463 pr_err("Failed to rollback the write error: %d\n", ret);
464 pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path);
469 static int usage(void)
471 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
472 "Or bootconfig <CONFIG>\n"
473 " Apply, delete or show boot config to initrd.\n"
475 " -a <config>: Apply boot config to initrd\n"
476 " -d : Delete boot config file from initrd\n"
477 " -l : list boot config in initrd or file\n\n"
478 " If no option is given, show the bootconfig in the given file.\n");
482 int main(int argc, char **argv)
486 bool delete = false, list = false;
489 while ((opt = getopt(argc, argv, "hda:l")) != -1) {
506 if ((apply && delete) || (delete && list) || (apply && list)) {
507 pr_err("Error: You can give one of -a, -d or -l at once.\n");
511 if (optind >= argc) {
512 pr_err("Error: No initrd is specified.\n");
519 return apply_xbc(path, apply);
521 return delete_xbc(path);
523 return show_xbc(path, list);