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;
116 xbc_for_each_key_value(leaf, val) {
117 ret = xbc_node_compose_key(leaf, key, XBC_KEYLEN_MAX);
119 fprintf(stderr, "Failed to compose key %d\n", ret);
122 printf("%s = ", key);
123 if (!val || val[0] == '\0') {
127 xbc_show_value(xbc_node_get_child(leaf), false);
131 #define PAGE_SIZE 4096
133 static int load_xbc_fd(int fd, char **buf, int size)
137 *buf = malloc(size + 1);
141 ret = read(fd, *buf, size);
149 /* Return the read size or -errno */
150 static int load_xbc_file(const char *path, char **buf)
155 fd = open(path, O_RDONLY);
158 ret = fstat(fd, &stat);
162 ret = load_xbc_fd(fd, buf, stat.st_size);
169 static int pr_errno(const char *msg, int err)
171 pr_err("%s: %d\n", msg, err);
175 static int load_xbc_from_initrd(int fd, char **buf)
179 u32 size = 0, csum = 0, rcsum;
180 char magic[BOOTCONFIG_MAGIC_LEN];
183 ret = fstat(fd, &stat);
187 if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
190 if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
191 return pr_errno("Failed to lseek for magic", -errno);
193 if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
194 return pr_errno("Failed to read", -errno);
196 /* Check the bootconfig magic bytes */
197 if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
200 if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0)
201 return pr_errno("Failed to lseek for size", -errno);
203 if (read(fd, &size, sizeof(u32)) < 0)
204 return pr_errno("Failed to read size", -errno);
205 size = le32toh(size);
207 if (read(fd, &csum, sizeof(u32)) < 0)
208 return pr_errno("Failed to read checksum", -errno);
209 csum = le32toh(csum);
211 /* Wrong size error */
212 if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
213 pr_err("bootconfig size is too big\n");
217 if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
219 return pr_errno("Failed to lseek", -errno);
221 ret = load_xbc_fd(fd, buf, size);
226 rcsum = xbc_calc_checksum(*buf, size);
228 pr_err("checksum error: %d != %d\n", csum, rcsum);
232 ret = xbc_init(*buf, &msg, NULL);
235 pr_err("parse error: %s.\n", msg);
242 static void show_xbc_error(const char *data, const char *msg, int pos)
247 pr_err("Error: %s.\n", msg);
251 /* Note that pos starts from 0 but lin and col should start from 1. */
253 for (i = 0; i < pos; i++) {
254 if (data[i] == '\n') {
259 pr_err("Parse Error: %s at %d:%d\n", msg, lin, col);
263 static int init_xbc_with_error(char *buf, int len)
265 char *copy = strdup(buf);
272 ret = xbc_init(buf, &msg, &pos);
274 show_xbc_error(copy, msg, pos);
280 static int show_xbc(const char *path, bool list)
286 ret = stat(path, &st);
289 pr_err("Failed to stat %s: %d\n", path, ret);
293 fd = open(path, O_RDONLY);
296 pr_err("Failed to open initrd %s: %d\n", path, ret);
300 ret = load_xbc_from_initrd(fd, &buf);
303 pr_err("Failed to load a boot config from initrd: %d\n", ret);
306 /* Assume a bootconfig file if it is enough small */
307 if (ret == 0 && st.st_size <= XBC_DATA_MAX) {
308 ret = load_xbc_file(path, &buf);
310 pr_err("Failed to load a boot config: %d\n", ret);
313 if (init_xbc_with_error(buf, ret) < 0)
319 xbc_show_compact_tree();
327 static int delete_xbc(const char *path)
330 int ret = 0, fd, size;
333 fd = open(path, O_RDWR);
336 pr_err("Failed to open initrd %s: %d\n", path, ret);
340 size = load_xbc_from_initrd(fd, &buf);
343 pr_err("Failed to load a boot config from initrd: %d\n", ret);
344 } else if (size > 0) {
345 ret = fstat(fd, &stat);
347 ret = ftruncate(fd, stat.st_size
348 - size - 8 - BOOTCONFIG_MAGIC_LEN);
351 } /* Ignore if there is no boot config in initrd */
359 static int apply_xbc(const char *path, const char *xbc_path)
361 char *buf, *data, *p;
369 ret = load_xbc_file(xbc_path, &buf);
371 pr_err("Failed to load %s : %d\n", xbc_path, ret);
374 size = strlen(buf) + 1;
375 csum = xbc_calc_checksum(buf, size);
377 /* Backup the bootconfig data */
378 data = calloc(size + BOOTCONFIG_ALIGN +
379 sizeof(u32) + sizeof(u32) + BOOTCONFIG_MAGIC_LEN, 1);
382 memcpy(data, buf, size);
384 /* Check the data format */
385 ret = xbc_init(buf, &msg, &pos);
387 show_xbc_error(data, msg, pos);
393 printf("Apply %s to %s\n", xbc_path, path);
394 printf("\tNumber of nodes: %d\n", ret);
395 printf("\tSize: %u bytes\n", (unsigned int)size);
396 printf("\tChecksum: %d\n", (unsigned int)csum);
398 /* TODO: Check the options by schema */
402 /* Remove old boot config if exists */
403 ret = delete_xbc(path);
405 pr_err("Failed to delete previous boot config: %d\n", ret);
411 fd = open(path, O_RDWR | O_APPEND);
414 pr_err("Failed to open %s: %d\n", path, ret);
418 /* TODO: Ensure the @path is initramfs/initrd image */
419 if (fstat(fd, &stat) < 0) {
421 pr_err("Failed to get the size of %s\n", path);
425 /* To align up the total size to BOOTCONFIG_ALIGN, get padding size */
426 total_size = stat.st_size + size + sizeof(u32) * 2 + BOOTCONFIG_MAGIC_LEN;
427 pad = ((total_size + BOOTCONFIG_ALIGN - 1) & (~BOOTCONFIG_ALIGN_MASK)) - total_size;
432 *(u32 *)p = htole32(size);
435 *(u32 *)p = htole32(csum);
438 memcpy(p, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
439 p += BOOTCONFIG_MAGIC_LEN;
441 total_size = p - data;
443 ret = write(fd, data, total_size);
444 if (ret < total_size) {
447 pr_err("Failed to apply a boot config: %d\n", ret);
460 /* Map the partial write to -ENOSPC */
463 if (ftruncate(fd, stat.st_size) < 0) {
465 pr_err("Failed to rollback the write error: %d\n", ret);
466 pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path);
471 static int usage(void)
473 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
474 "Or bootconfig <CONFIG>\n"
475 " Apply, delete or show boot config to initrd.\n"
477 " -a <config>: Apply boot config to initrd\n"
478 " -d : Delete boot config file from initrd\n"
479 " -l : list boot config in initrd or file\n\n"
480 " If no option is given, show the bootconfig in the given file.\n");
484 int main(int argc, char **argv)
488 bool delete = false, list = false;
491 while ((opt = getopt(argc, argv, "hda:l")) != -1) {
508 if ((apply && delete) || (delete && list) || (apply && list)) {
509 pr_err("Error: You can give one of -a, -d or -l at once.\n");
513 if (optind >= argc) {
514 pr_err("Error: No initrd is specified.\n");
521 return apply_xbc(path, apply);
523 return delete_xbc(path);
525 return show_xbc(path, list);