1 // SPDX-License-Identifier: GPL-2.0
3 * Boot config tool for initrd image
15 #include <linux/bootconfig.h>
17 #define pr_err(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
19 static int xbc_show_value(struct xbc_node
*node
, bool semicolon
)
21 const char *val
, *eol
;
25 eol
= semicolon
? ";\n" : "\n";
26 xbc_array_for_each_value(node
, val
) {
31 printf("%c%s%c%s", q
, val
, q
, xbc_node_is_array(node
) ? ", " : eol
);
37 static void xbc_show_compact_tree(void)
39 struct xbc_node
*node
, *cnode
= NULL
, *vnode
;
42 node
= xbc_root_node();
43 while (node
&& xbc_node_is_key(node
)) {
44 for (i
= 0; i
< depth
; i
++)
47 cnode
= xbc_node_get_child(node
);
48 while (cnode
&& xbc_node_is_key(cnode
) && !cnode
->next
) {
49 vnode
= xbc_node_get_child(cnode
);
51 * If @cnode has value and subkeys, this
52 * should show it as below.
55 * key(@cnode) = value;
61 if (vnode
&& xbc_node_is_value(vnode
) && vnode
->next
)
63 printf("%s.", xbc_node_get_data(node
));
67 if (cnode
&& xbc_node_is_key(cnode
)) {
68 printf("%s {\n", xbc_node_get_data(node
));
73 } else if (cnode
&& xbc_node_is_value(cnode
)) {
74 printf("%s = ", xbc_node_get_data(node
));
75 xbc_show_value(cnode
, true);
77 * If @node has value and subkeys, continue
78 * looping on subkeys with same node.
81 cnode
= xbc_node_get_next(cnode
);
85 printf("%s;\n", xbc_node_get_data(node
));
90 node
= xbc_node_get_next(node
);
94 node
= xbc_node_get_parent(node
);
97 if (!xbc_node_get_child(node
)->next
)
101 for (i
= 0; i
< depth
; i
++)
106 node
= xbc_node_get_next(node
);
110 static void xbc_show_list(void)
112 char key
[XBC_KEYLEN_MAX
];
113 struct xbc_node
*leaf
;
117 xbc_for_each_key_value(leaf
, val
) {
118 ret
= xbc_node_compose_key(leaf
, key
, XBC_KEYLEN_MAX
);
120 fprintf(stderr
, "Failed to compose key %d\n", ret
);
123 printf("%s = ", key
);
124 if (!val
|| val
[0] == '\0') {
128 xbc_show_value(xbc_node_get_child(leaf
), false);
132 #define PAGE_SIZE 4096
134 static int load_xbc_fd(int fd
, char **buf
, int size
)
138 *buf
= malloc(size
+ 1);
142 ret
= read(fd
, *buf
, size
);
150 /* Return the read size or -errno */
151 static int load_xbc_file(const char *path
, char **buf
)
156 fd
= open(path
, O_RDONLY
);
159 ret
= fstat(fd
, &stat
);
163 ret
= load_xbc_fd(fd
, buf
, stat
.st_size
);
170 static int pr_errno(const char *msg
, int err
)
172 pr_err("%s: %d\n", msg
, err
);
176 static int load_xbc_from_initrd(int fd
, char **buf
)
180 uint32_t size
= 0, csum
= 0, rcsum
;
181 char magic
[BOOTCONFIG_MAGIC_LEN
];
184 ret
= fstat(fd
, &stat
);
188 if (stat
.st_size
< 8 + BOOTCONFIG_MAGIC_LEN
)
191 if (lseek(fd
, -BOOTCONFIG_MAGIC_LEN
, SEEK_END
) < 0)
192 return pr_errno("Failed to lseek for magic", -errno
);
194 if (read(fd
, magic
, BOOTCONFIG_MAGIC_LEN
) < 0)
195 return pr_errno("Failed to read", -errno
);
197 /* Check the bootconfig magic bytes */
198 if (memcmp(magic
, BOOTCONFIG_MAGIC
, BOOTCONFIG_MAGIC_LEN
) != 0)
201 if (lseek(fd
, -(8 + BOOTCONFIG_MAGIC_LEN
), SEEK_END
) < 0)
202 return pr_errno("Failed to lseek for size", -errno
);
204 if (read(fd
, &size
, sizeof(uint32_t)) < 0)
205 return pr_errno("Failed to read size", -errno
);
206 size
= le32toh(size
);
208 if (read(fd
, &csum
, sizeof(uint32_t)) < 0)
209 return pr_errno("Failed to read checksum", -errno
);
210 csum
= le32toh(csum
);
212 /* Wrong size error */
213 if (stat
.st_size
< size
+ 8 + BOOTCONFIG_MAGIC_LEN
) {
214 pr_err("bootconfig size is too big\n");
218 if (lseek(fd
, stat
.st_size
- (size
+ 8 + BOOTCONFIG_MAGIC_LEN
),
220 return pr_errno("Failed to lseek", -errno
);
222 ret
= load_xbc_fd(fd
, buf
, size
);
227 rcsum
= xbc_calc_checksum(*buf
, size
);
229 pr_err("checksum error: %d != %d\n", csum
, rcsum
);
233 ret
= xbc_init(*buf
, size
, &msg
, NULL
);
236 pr_err("parse error: %s.\n", msg
);
243 static void show_xbc_error(const char *data
, const char *msg
, int pos
)
248 pr_err("Error: %s.\n", msg
);
252 /* Note that pos starts from 0 but lin and col should start from 1. */
254 for (i
= 0; i
< pos
; i
++) {
255 if (data
[i
] == '\n') {
260 pr_err("Parse Error: %s at %d:%d\n", msg
, lin
, col
);
264 static int init_xbc_with_error(char *buf
, int len
)
266 char *copy
= strdup(buf
);
273 ret
= xbc_init(buf
, len
, &msg
, &pos
);
275 show_xbc_error(copy
, msg
, pos
);
281 static int show_xbc(const char *path
, bool list
)
287 ret
= stat(path
, &st
);
290 pr_err("Failed to stat %s: %d\n", path
, ret
);
294 fd
= open(path
, O_RDONLY
);
297 pr_err("Failed to open initrd %s: %d\n", path
, ret
);
301 ret
= load_xbc_from_initrd(fd
, &buf
);
304 pr_err("Failed to load a boot config from initrd: %d\n", ret
);
307 /* Assume a bootconfig file if it is enough small */
308 if (ret
== 0 && st
.st_size
<= XBC_DATA_MAX
) {
309 ret
= load_xbc_file(path
, &buf
);
311 pr_err("Failed to load a boot config: %d\n", ret
);
314 if (init_xbc_with_error(buf
, ret
) < 0)
320 xbc_show_compact_tree();
328 static int delete_xbc(const char *path
)
331 int ret
= 0, fd
, size
;
334 fd
= open(path
, O_RDWR
);
337 pr_err("Failed to open initrd %s: %d\n", path
, ret
);
341 size
= load_xbc_from_initrd(fd
, &buf
);
344 pr_err("Failed to load a boot config from initrd: %d\n", ret
);
345 } else if (size
> 0) {
346 ret
= fstat(fd
, &stat
);
348 ret
= ftruncate(fd
, stat
.st_size
349 - size
- 8 - BOOTCONFIG_MAGIC_LEN
);
352 } /* Ignore if there is no boot config in initrd */
360 static int apply_xbc(const char *path
, const char *xbc_path
)
362 char *buf
, *data
, *p
;
370 ret
= load_xbc_file(xbc_path
, &buf
);
372 pr_err("Failed to load %s : %d\n", xbc_path
, ret
);
375 size
= strlen(buf
) + 1;
376 csum
= xbc_calc_checksum(buf
, size
);
378 /* Backup the bootconfig data */
379 data
= calloc(size
+ BOOTCONFIG_ALIGN
+
380 sizeof(uint32_t) + sizeof(uint32_t) + BOOTCONFIG_MAGIC_LEN
, 1);
383 memcpy(data
, buf
, size
);
385 /* Check the data format */
386 ret
= xbc_init(buf
, size
, &msg
, &pos
);
388 show_xbc_error(data
, msg
, pos
);
394 printf("Apply %s to %s\n", xbc_path
, path
);
395 xbc_get_info(&ret
, NULL
);
396 printf("\tNumber of nodes: %d\n", ret
);
397 printf("\tSize: %u bytes\n", (unsigned int)size
);
398 printf("\tChecksum: %d\n", (unsigned int)csum
);
400 /* TODO: Check the options by schema */
404 /* Remove old boot config if exists */
405 ret
= delete_xbc(path
);
407 pr_err("Failed to delete previous boot config: %d\n", ret
);
413 fd
= open(path
, O_RDWR
| O_APPEND
);
416 pr_err("Failed to open %s: %d\n", path
, ret
);
420 /* TODO: Ensure the @path is initramfs/initrd image */
421 if (fstat(fd
, &stat
) < 0) {
423 pr_err("Failed to get the size of %s\n", path
);
427 /* To align up the total size to BOOTCONFIG_ALIGN, get padding size */
428 total_size
= stat
.st_size
+ size
+ sizeof(uint32_t) * 2 + BOOTCONFIG_MAGIC_LEN
;
429 pad
= ((total_size
+ BOOTCONFIG_ALIGN
- 1) & (~BOOTCONFIG_ALIGN_MASK
)) - total_size
;
434 *(uint32_t *)p
= htole32(size
);
435 p
+= sizeof(uint32_t);
437 *(uint32_t *)p
= htole32(csum
);
438 p
+= sizeof(uint32_t);
440 memcpy(p
, BOOTCONFIG_MAGIC
, BOOTCONFIG_MAGIC_LEN
);
441 p
+= BOOTCONFIG_MAGIC_LEN
;
443 total_size
= p
- data
;
445 ret
= write(fd
, data
, total_size
);
446 if (ret
< total_size
) {
449 pr_err("Failed to apply a boot config: %d\n", ret
);
462 /* Map the partial write to -ENOSPC */
465 if (ftruncate(fd
, stat
.st_size
) < 0) {
467 pr_err("Failed to rollback the write error: %d\n", ret
);
468 pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path
);
473 static int usage(void)
475 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
476 "Or bootconfig <CONFIG>\n"
477 " Apply, delete or show boot config to initrd.\n"
479 " -a <config>: Apply boot config to initrd\n"
480 " -d : Delete boot config file from initrd\n"
481 " -l : list boot config in initrd or file\n\n"
482 " If no option is given, show the bootconfig in the given file.\n");
486 int main(int argc
, char **argv
)
490 bool delete = false, list
= false;
493 while ((opt
= getopt(argc
, argv
, "hda:l")) != -1) {
510 if ((apply
&& delete) || (delete && list
) || (apply
&& list
)) {
511 pr_err("Error: You can give one of -a, -d or -l at once.\n");
515 if (optind
>= argc
) {
516 pr_err("Error: No initrd is specified.\n");
523 return apply_xbc(path
, apply
);
525 return delete_xbc(path
);
527 return show_xbc(path
, list
);