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 load_xbc_from_initrd(int fd, char **buf)
154 u32 size = 0, csum = 0, rcsum;
155 char magic[BOOTCONFIG_MAGIC_LEN];
158 ret = fstat(fd, &stat);
162 if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
165 if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0) {
166 pr_err("Failed to lseek: %d\n", -errno);
169 if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
171 /* Check the bootconfig magic bytes */
172 if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
175 if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0) {
176 pr_err("Failed to lseek: %d\n", -errno);
180 if (read(fd, &size, sizeof(u32)) < 0)
183 if (read(fd, &csum, sizeof(u32)) < 0)
186 /* Wrong size error */
187 if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
188 pr_err("bootconfig size is too big\n");
192 if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
194 pr_err("Failed to lseek: %d\n", -errno);
198 ret = load_xbc_fd(fd, buf, size);
203 rcsum = checksum((unsigned char *)*buf, size);
205 pr_err("checksum error: %d != %d\n", csum, rcsum);
209 ret = xbc_init(*buf, &msg, NULL);
212 pr_err("parse error: %s.\n", msg);
219 static void show_xbc_error(const char *data, const char *msg, int pos)
224 pr_err("Error: %s.\n", msg);
228 /* Note that pos starts from 0 but lin and col should start from 1. */
230 for (i = 0; i < pos; i++) {
231 if (data[i] == '\n') {
236 pr_err("Parse Error: %s at %d:%d\n", msg, lin, col);
240 static int init_xbc_with_error(char *buf, int len)
242 char *copy = strdup(buf);
249 ret = xbc_init(buf, &msg, &pos);
251 show_xbc_error(copy, msg, pos);
257 static int show_xbc(const char *path, bool list)
263 ret = stat(path, &st);
265 pr_err("Failed to stat %s: %d\n", path, -errno);
269 fd = open(path, O_RDONLY);
271 pr_err("Failed to open initrd %s: %d\n", path, fd);
275 ret = load_xbc_from_initrd(fd, &buf);
278 pr_err("Failed to load a boot config from initrd: %d\n", ret);
281 /* Assume a bootconfig file if it is enough small */
282 if (ret == 0 && st.st_size <= XBC_DATA_MAX) {
283 ret = load_xbc_file(path, &buf);
285 pr_err("Failed to load a boot config: %d\n", ret);
288 if (init_xbc_with_error(buf, ret) < 0)
294 xbc_show_compact_tree();
302 static int delete_xbc(const char *path)
305 int ret = 0, fd, size;
308 fd = open(path, O_RDWR);
310 pr_err("Failed to open initrd %s: %d\n", path, fd);
314 size = load_xbc_from_initrd(fd, &buf);
317 pr_err("Failed to load a boot config from initrd: %d\n", ret);
318 } else if (size > 0) {
319 ret = fstat(fd, &stat);
321 ret = ftruncate(fd, stat.st_size
322 - size - 8 - BOOTCONFIG_MAGIC_LEN);
325 } /* Ignore if there is no boot config in initrd */
333 static int apply_xbc(const char *path, const char *xbc_path)
341 ret = load_xbc_file(xbc_path, &buf);
343 pr_err("Failed to load %s : %d\n", xbc_path, ret);
346 size = strlen(buf) + 1;
347 csum = checksum((unsigned char *)buf, size);
349 /* Prepare xbc_path data */
350 data = malloc(size + 8);
354 *(u32 *)(data + size) = size;
355 *(u32 *)(data + size + 4) = csum;
357 /* Check the data format */
358 ret = xbc_init(buf, &msg, &pos);
360 show_xbc_error(data, msg, pos);
366 printf("Apply %s to %s\n", xbc_path, path);
367 printf("\tNumber of nodes: %d\n", ret);
368 printf("\tSize: %u bytes\n", (unsigned int)size);
369 printf("\tChecksum: %d\n", (unsigned int)csum);
371 /* TODO: Check the options by schema */
375 /* Remove old boot config if exists */
376 ret = delete_xbc(path);
378 pr_err("Failed to delete previous boot config: %d\n", ret);
384 fd = open(path, O_RDWR | O_APPEND);
386 pr_err("Failed to open %s: %d\n", path, fd);
390 /* TODO: Ensure the @path is initramfs/initrd image */
391 ret = write(fd, data, size + 8);
393 pr_err("Failed to apply a boot config: %d\n", ret);
396 /* Write a magic word of the bootconfig */
397 ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
399 pr_err("Failed to apply a boot config magic: %d\n", ret);
410 static int usage(void)
412 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
413 "Or bootconfig <CONFIG>\n"
414 " Apply, delete or show boot config to initrd.\n"
416 " -a <config>: Apply boot config to initrd\n"
417 " -d : Delete boot config file from initrd\n"
418 " -l : list boot config in initrd or file\n\n"
419 " If no option is given, show the bootconfig in the given file.\n");
423 int main(int argc, char **argv)
427 bool delete = false, list = false;
430 while ((opt = getopt(argc, argv, "hda:l")) != -1) {
447 if ((apply && delete) || (delete && list) || (apply && list)) {
448 pr_err("Error: You can give one of -a, -d or -l at once.\n");
452 if (optind >= argc) {
453 pr_err("Error: No initrd is specified.\n");
460 return apply_xbc(path, apply);
462 return delete_xbc(path);
464 return show_xbc(path, list);