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, node->next ? ", " : eol);
36 static void xbc_show_compact_tree(void)
38 struct xbc_node *node, *cnode;
41 node = xbc_root_node();
42 while (node && xbc_node_is_key(node)) {
43 for (i = 0; i < depth; i++)
45 cnode = xbc_node_get_child(node);
46 while (cnode && xbc_node_is_key(cnode) && !cnode->next) {
47 printf("%s.", xbc_node_get_data(node));
49 cnode = xbc_node_get_child(node);
51 if (cnode && xbc_node_is_key(cnode)) {
52 printf("%s {\n", xbc_node_get_data(node));
56 } else if (cnode && xbc_node_is_value(cnode)) {
57 printf("%s = ", xbc_node_get_data(node));
58 xbc_show_value(cnode, true);
60 printf("%s;\n", xbc_node_get_data(node));
64 node = xbc_node_get_next(node);
68 node = xbc_node_get_parent(node);
71 if (!xbc_node_get_child(node)->next)
74 for (i = 0; i < depth; i++)
78 node = xbc_node_get_next(node);
82 static void xbc_show_list(void)
84 char key[XBC_KEYLEN_MAX];
85 struct xbc_node *leaf;
89 xbc_for_each_key_value(leaf, val) {
90 ret = xbc_node_compose_key(leaf, key, XBC_KEYLEN_MAX);
94 if (!val || val[0] == '\0') {
98 xbc_show_value(xbc_node_get_child(leaf), false);
102 /* Simple real checksum */
103 static int checksum(unsigned char *buf, int len)
107 for (i = 0; i < len; i++)
113 #define PAGE_SIZE 4096
115 static int load_xbc_fd(int fd, char **buf, int size)
119 *buf = malloc(size + 1);
123 ret = read(fd, *buf, size);
131 /* Return the read size or -errno */
132 static int load_xbc_file(const char *path, char **buf)
137 fd = open(path, O_RDONLY);
140 ret = fstat(fd, &stat);
144 ret = load_xbc_fd(fd, buf, stat.st_size);
151 static int pr_errno(const char *msg, int err)
153 pr_err("%s: %d\n", msg, err);
157 static int load_xbc_from_initrd(int fd, char **buf)
161 u32 size = 0, csum = 0, rcsum;
162 char magic[BOOTCONFIG_MAGIC_LEN];
165 ret = fstat(fd, &stat);
169 if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
172 if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
173 return pr_errno("Failed to lseek for magic", -errno);
175 if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
176 return pr_errno("Failed to read", -errno);
178 /* Check the bootconfig magic bytes */
179 if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
182 if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0)
183 return pr_errno("Failed to lseek for size", -errno);
185 if (read(fd, &size, sizeof(u32)) < 0)
186 return pr_errno("Failed to read size", -errno);
187 size = le32toh(size);
189 if (read(fd, &csum, sizeof(u32)) < 0)
190 return pr_errno("Failed to read checksum", -errno);
191 csum = le32toh(csum);
193 /* Wrong size error */
194 if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
195 pr_err("bootconfig size is too big\n");
199 if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
201 return pr_errno("Failed to lseek", -errno);
203 ret = load_xbc_fd(fd, buf, size);
208 rcsum = checksum((unsigned char *)*buf, size);
210 pr_err("checksum error: %d != %d\n", csum, rcsum);
214 ret = xbc_init(*buf, &msg, NULL);
217 pr_err("parse error: %s.\n", msg);
224 static void show_xbc_error(const char *data, const char *msg, int pos)
229 pr_err("Error: %s.\n", msg);
233 /* Note that pos starts from 0 but lin and col should start from 1. */
235 for (i = 0; i < pos; i++) {
236 if (data[i] == '\n') {
241 pr_err("Parse Error: %s at %d:%d\n", msg, lin, col);
245 static int init_xbc_with_error(char *buf, int len)
247 char *copy = strdup(buf);
254 ret = xbc_init(buf, &msg, &pos);
256 show_xbc_error(copy, msg, pos);
262 static int show_xbc(const char *path, bool list)
268 ret = stat(path, &st);
271 pr_err("Failed to stat %s: %d\n", path, ret);
275 fd = open(path, O_RDONLY);
278 pr_err("Failed to open initrd %s: %d\n", path, ret);
282 ret = load_xbc_from_initrd(fd, &buf);
285 pr_err("Failed to load a boot config from initrd: %d\n", ret);
288 /* Assume a bootconfig file if it is enough small */
289 if (ret == 0 && st.st_size <= XBC_DATA_MAX) {
290 ret = load_xbc_file(path, &buf);
292 pr_err("Failed to load a boot config: %d\n", ret);
295 if (init_xbc_with_error(buf, ret) < 0)
301 xbc_show_compact_tree();
309 static int delete_xbc(const char *path)
312 int ret = 0, fd, size;
315 fd = open(path, O_RDWR);
318 pr_err("Failed to open initrd %s: %d\n", path, ret);
322 size = load_xbc_from_initrd(fd, &buf);
325 pr_err("Failed to load a boot config from initrd: %d\n", ret);
326 } else if (size > 0) {
327 ret = fstat(fd, &stat);
329 ret = ftruncate(fd, stat.st_size
330 - size - 8 - BOOTCONFIG_MAGIC_LEN);
333 } /* Ignore if there is no boot config in initrd */
341 static int apply_xbc(const char *path, const char *xbc_path)
343 char *buf, *data, *p;
351 ret = load_xbc_file(xbc_path, &buf);
353 pr_err("Failed to load %s : %d\n", xbc_path, ret);
356 size = strlen(buf) + 1;
357 csum = checksum((unsigned char *)buf, size);
359 /* Backup the bootconfig data */
360 data = calloc(size + BOOTCONFIG_ALIGN +
361 sizeof(u32) + sizeof(u32) + BOOTCONFIG_MAGIC_LEN, 1);
364 memcpy(data, buf, size);
366 /* Check the data format */
367 ret = xbc_init(buf, &msg, &pos);
369 show_xbc_error(data, msg, pos);
375 printf("Apply %s to %s\n", xbc_path, path);
376 printf("\tNumber of nodes: %d\n", ret);
377 printf("\tSize: %u bytes\n", (unsigned int)size);
378 printf("\tChecksum: %d\n", (unsigned int)csum);
380 /* TODO: Check the options by schema */
384 /* Remove old boot config if exists */
385 ret = delete_xbc(path);
387 pr_err("Failed to delete previous boot config: %d\n", ret);
393 fd = open(path, O_RDWR | O_APPEND);
396 pr_err("Failed to open %s: %d\n", path, ret);
400 /* TODO: Ensure the @path is initramfs/initrd image */
401 if (fstat(fd, &stat) < 0) {
403 pr_err("Failed to get the size of %s\n", path);
407 /* To align up the total size to BOOTCONFIG_ALIGN, get padding size */
408 total_size = stat.st_size + size + sizeof(u32) * 2 + BOOTCONFIG_MAGIC_LEN;
409 pad = ((total_size + BOOTCONFIG_ALIGN - 1) & (~BOOTCONFIG_ALIGN_MASK)) - total_size;
414 *(u32 *)p = htole32(size);
417 *(u32 *)p = htole32(csum);
420 memcpy(p, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
421 p += BOOTCONFIG_MAGIC_LEN;
423 total_size = p - data;
425 ret = write(fd, data, total_size);
426 if (ret < total_size) {
429 pr_err("Failed to apply a boot config: %d\n", ret);
442 /* Map the partial write to -ENOSPC */
445 if (ftruncate(fd, stat.st_size) < 0) {
447 pr_err("Failed to rollback the write error: %d\n", ret);
448 pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path);
453 static int usage(void)
455 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
456 "Or bootconfig <CONFIG>\n"
457 " Apply, delete or show boot config to initrd.\n"
459 " -a <config>: Apply boot config to initrd\n"
460 " -d : Delete boot config file from initrd\n"
461 " -l : list boot config in initrd or file\n\n"
462 " If no option is given, show the bootconfig in the given file.\n");
466 int main(int argc, char **argv)
470 bool delete = false, list = false;
473 while ((opt = getopt(argc, argv, "hda:l")) != -1) {
490 if ((apply && delete) || (delete && list) || (apply && list)) {
491 pr_err("Error: You can give one of -a, -d or -l at once.\n");
495 if (optind >= argc) {
496 pr_err("Error: No initrd is specified.\n");
503 return apply_xbc(path, apply);
505 return delete_xbc(path);
507 return show_xbc(path, list);