1 // SPDX-License-Identifier: GPL-2.0
3 * Boot config tool for initrd image
14 #include <linux/kernel.h>
15 #include <linux/bootconfig.h>
17 static int xbc_show_value(struct xbc_node *node, bool semicolon)
19 const char *val, *eol;
23 eol = semicolon ? ";\n" : "\n";
24 xbc_array_for_each_value(node, val) {
29 printf("%c%s%c%s", q, val, q, node->next ? ", " : eol);
35 static void xbc_show_compact_tree(void)
37 struct xbc_node *node, *cnode;
40 node = xbc_root_node();
41 while (node && xbc_node_is_key(node)) {
42 for (i = 0; i < depth; i++)
44 cnode = xbc_node_get_child(node);
45 while (cnode && xbc_node_is_key(cnode) && !cnode->next) {
46 printf("%s.", xbc_node_get_data(node));
48 cnode = xbc_node_get_child(node);
50 if (cnode && xbc_node_is_key(cnode)) {
51 printf("%s {\n", xbc_node_get_data(node));
55 } else if (cnode && xbc_node_is_value(cnode)) {
56 printf("%s = ", xbc_node_get_data(node));
57 xbc_show_value(cnode, true);
59 printf("%s;\n", xbc_node_get_data(node));
63 node = xbc_node_get_next(node);
67 node = xbc_node_get_parent(node);
70 if (!xbc_node_get_child(node)->next)
73 for (i = 0; i < depth; i++)
77 node = xbc_node_get_next(node);
81 static void xbc_show_list(void)
83 char key[XBC_KEYLEN_MAX];
84 struct xbc_node *leaf;
88 xbc_for_each_key_value(leaf, val) {
89 ret = xbc_node_compose_key(leaf, key, XBC_KEYLEN_MAX);
93 if (!val || val[0] == '\0') {
97 xbc_show_value(xbc_node_get_child(leaf), false);
101 /* Simple real checksum */
102 static int checksum(unsigned char *buf, int len)
106 for (i = 0; i < len; i++)
112 #define PAGE_SIZE 4096
114 static int load_xbc_fd(int fd, char **buf, int size)
118 *buf = malloc(size + 1);
122 ret = read(fd, *buf, size);
130 /* Return the read size or -errno */
131 static int load_xbc_file(const char *path, char **buf)
136 fd = open(path, O_RDONLY);
139 ret = fstat(fd, &stat);
143 ret = load_xbc_fd(fd, buf, stat.st_size);
150 static int pr_errno(const char *msg, int err)
152 pr_err("%s: %d\n", msg, err);
156 static int load_xbc_from_initrd(int fd, char **buf)
160 u32 size = 0, csum = 0, rcsum;
161 char magic[BOOTCONFIG_MAGIC_LEN];
164 ret = fstat(fd, &stat);
168 if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
171 if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
172 return pr_errno("Failed to lseek for magic", -errno);
174 if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
175 return pr_errno("Failed to read", -errno);
177 /* Check the bootconfig magic bytes */
178 if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
181 if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0)
182 return pr_errno("Failed to lseek for size", -errno);
184 if (read(fd, &size, sizeof(u32)) < 0)
185 return pr_errno("Failed to read size", -errno);
187 if (read(fd, &csum, sizeof(u32)) < 0)
188 return pr_errno("Failed to read checksum", -errno);
190 /* Wrong size error */
191 if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
192 pr_err("bootconfig size is too big\n");
196 if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
198 return pr_errno("Failed to lseek", -errno);
200 ret = load_xbc_fd(fd, buf, size);
205 rcsum = checksum((unsigned char *)*buf, size);
207 pr_err("checksum error: %d != %d\n", csum, rcsum);
211 ret = xbc_init(*buf, &msg, NULL);
214 pr_err("parse error: %s.\n", msg);
221 static void show_xbc_error(const char *data, const char *msg, int pos)
226 pr_err("Error: %s.\n", msg);
230 /* Note that pos starts from 0 but lin and col should start from 1. */
232 for (i = 0; i < pos; i++) {
233 if (data[i] == '\n') {
238 pr_err("Parse Error: %s at %d:%d\n", msg, lin, col);
242 static int init_xbc_with_error(char *buf, int len)
244 char *copy = strdup(buf);
251 ret = xbc_init(buf, &msg, &pos);
253 show_xbc_error(copy, msg, pos);
259 static int show_xbc(const char *path, bool list)
265 ret = stat(path, &st);
268 pr_err("Failed to stat %s: %d\n", path, ret);
272 fd = open(path, O_RDONLY);
275 pr_err("Failed to open initrd %s: %d\n", path, ret);
279 ret = load_xbc_from_initrd(fd, &buf);
282 pr_err("Failed to load a boot config from initrd: %d\n", ret);
285 /* Assume a bootconfig file if it is enough small */
286 if (ret == 0 && st.st_size <= XBC_DATA_MAX) {
287 ret = load_xbc_file(path, &buf);
289 pr_err("Failed to load a boot config: %d\n", ret);
292 if (init_xbc_with_error(buf, ret) < 0)
298 xbc_show_compact_tree();
306 static int delete_xbc(const char *path)
309 int ret = 0, fd, size;
312 fd = open(path, O_RDWR);
315 pr_err("Failed to open initrd %s: %d\n", path, ret);
319 size = load_xbc_from_initrd(fd, &buf);
322 pr_err("Failed to load a boot config from initrd: %d\n", ret);
323 } else if (size > 0) {
324 ret = fstat(fd, &stat);
326 ret = ftruncate(fd, stat.st_size
327 - size - 8 - BOOTCONFIG_MAGIC_LEN);
330 } /* Ignore if there is no boot config in initrd */
338 static int apply_xbc(const char *path, const char *xbc_path)
347 ret = load_xbc_file(xbc_path, &buf);
349 pr_err("Failed to load %s : %d\n", xbc_path, ret);
352 size = strlen(buf) + 1;
353 csum = checksum((unsigned char *)buf, size);
355 /* Prepare xbc_path data */
356 data = malloc(size + 8);
360 *(u32 *)(data + size) = size;
361 *(u32 *)(data + size + 4) = csum;
363 /* Check the data format */
364 ret = xbc_init(buf, &msg, &pos);
366 show_xbc_error(data, msg, pos);
372 printf("Apply %s to %s\n", xbc_path, path);
373 printf("\tNumber of nodes: %d\n", ret);
374 printf("\tSize: %u bytes\n", (unsigned int)size);
375 printf("\tChecksum: %d\n", (unsigned int)csum);
377 /* TODO: Check the options by schema */
381 /* Remove old boot config if exists */
382 ret = delete_xbc(path);
384 pr_err("Failed to delete previous boot config: %d\n", ret);
390 fd = open(path, O_RDWR | O_APPEND);
393 pr_err("Failed to open %s: %d\n", path, ret);
397 /* TODO: Ensure the @path is initramfs/initrd image */
398 if (fstat(fd, &stat) < 0) {
399 pr_err("Failed to get the size of %s\n", path);
402 ret = write(fd, data, size + 8);
403 if (ret < size + 8) {
406 pr_err("Failed to apply a boot config: %d\n", ret);
411 /* Write a magic word of the bootconfig */
412 ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
413 if (ret < BOOTCONFIG_MAGIC_LEN) {
416 pr_err("Failed to apply a boot config magic: %d\n", ret);
427 /* Map the partial write to -ENOSPC */
430 if (ftruncate(fd, stat.st_size) < 0) {
432 pr_err("Failed to rollback the write error: %d\n", ret);
433 pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path);
438 static int usage(void)
440 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
441 "Or bootconfig <CONFIG>\n"
442 " Apply, delete or show boot config to initrd.\n"
444 " -a <config>: Apply boot config to initrd\n"
445 " -d : Delete boot config file from initrd\n"
446 " -l : list boot config in initrd or file\n\n"
447 " If no option is given, show the bootconfig in the given file.\n");
451 int main(int argc, char **argv)
455 bool delete = false, list = false;
458 while ((opt = getopt(argc, argv, "hda:l")) != -1) {
475 if ((apply && delete) || (delete && list) || (apply && list)) {
476 pr_err("Error: You can give one of -a, -d or -l at once.\n");
480 if (optind >= argc) {
481 pr_err("Error: No initrd is specified.\n");
488 return apply_xbc(path, apply);
490 return delete_xbc(path);
492 return show_xbc(path, list);