]>
git.proxmox.com Git - mirror_ubuntu-kernels.git/blob - tools/bootconfig/main.c
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 #define pr_err(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
20 static int xbc_show_value(struct xbc_node
*node
, bool semicolon
)
22 const char *val
, *eol
;
26 eol
= semicolon
? ";\n" : "\n";
27 xbc_array_for_each_value(node
, val
) {
32 printf("%c%s%c%s", q
, val
, q
, xbc_node_is_array(node
) ? ", " : eol
);
38 static void xbc_show_compact_tree(void)
40 struct xbc_node
*node
, *cnode
= NULL
, *vnode
;
43 node
= xbc_root_node();
44 while (node
&& xbc_node_is_key(node
)) {
45 for (i
= 0; i
< depth
; i
++)
48 cnode
= xbc_node_get_child(node
);
49 while (cnode
&& xbc_node_is_key(cnode
) && !cnode
->next
) {
50 vnode
= xbc_node_get_child(cnode
);
52 * If @cnode has value and subkeys, this
53 * should show it as below.
56 * key(@cnode) = value;
62 if (vnode
&& xbc_node_is_value(vnode
) && vnode
->next
)
64 printf("%s.", xbc_node_get_data(node
));
68 if (cnode
&& xbc_node_is_key(cnode
)) {
69 printf("%s {\n", xbc_node_get_data(node
));
74 } else if (cnode
&& xbc_node_is_value(cnode
)) {
75 printf("%s = ", xbc_node_get_data(node
));
76 xbc_show_value(cnode
, true);
78 * If @node has value and subkeys, continue
79 * looping on subkeys with same node.
82 cnode
= xbc_node_get_next(cnode
);
86 printf("%s;\n", xbc_node_get_data(node
));
91 node
= xbc_node_get_next(node
);
95 node
= xbc_node_get_parent(node
);
98 if (!xbc_node_get_child(node
)->next
)
102 for (i
= 0; i
< depth
; i
++)
107 node
= xbc_node_get_next(node
);
111 static void xbc_show_list(void)
113 char key
[XBC_KEYLEN_MAX
];
114 struct xbc_node
*leaf
;
118 xbc_for_each_key_value(leaf
, val
) {
119 ret
= xbc_node_compose_key(leaf
, key
, XBC_KEYLEN_MAX
);
121 fprintf(stderr
, "Failed to compose key %d\n", ret
);
124 printf("%s = ", key
);
125 if (!val
|| val
[0] == '\0') {
129 xbc_show_value(xbc_node_get_child(leaf
), false);
133 #define PAGE_SIZE 4096
135 static int load_xbc_fd(int fd
, char **buf
, int size
)
139 *buf
= malloc(size
+ 1);
143 ret
= read(fd
, *buf
, size
);
151 /* Return the read size or -errno */
152 static int load_xbc_file(const char *path
, char **buf
)
157 fd
= open(path
, O_RDONLY
);
160 ret
= fstat(fd
, &stat
);
164 ret
= load_xbc_fd(fd
, buf
, stat
.st_size
);
171 static int pr_errno(const char *msg
, int err
)
173 pr_err("%s: %d\n", msg
, err
);
177 static int load_xbc_from_initrd(int fd
, char **buf
)
181 uint32_t size
= 0, csum
= 0, rcsum
;
182 char magic
[BOOTCONFIG_MAGIC_LEN
];
185 ret
= fstat(fd
, &stat
);
189 if (stat
.st_size
< 8 + BOOTCONFIG_MAGIC_LEN
)
192 if (lseek(fd
, -BOOTCONFIG_MAGIC_LEN
, SEEK_END
) < 0)
193 return pr_errno("Failed to lseek for magic", -errno
);
195 if (read(fd
, magic
, BOOTCONFIG_MAGIC_LEN
) < 0)
196 return pr_errno("Failed to read", -errno
);
198 /* Check the bootconfig magic bytes */
199 if (memcmp(magic
, BOOTCONFIG_MAGIC
, BOOTCONFIG_MAGIC_LEN
) != 0)
202 if (lseek(fd
, -(8 + BOOTCONFIG_MAGIC_LEN
), SEEK_END
) < 0)
203 return pr_errno("Failed to lseek for size", -errno
);
205 if (read(fd
, &size
, sizeof(uint32_t)) < 0)
206 return pr_errno("Failed to read size", -errno
);
207 size
= le32toh(size
);
209 if (read(fd
, &csum
, sizeof(uint32_t)) < 0)
210 return pr_errno("Failed to read checksum", -errno
);
211 csum
= le32toh(csum
);
213 /* Wrong size error */
214 if (stat
.st_size
< size
+ 8 + BOOTCONFIG_MAGIC_LEN
) {
215 pr_err("bootconfig size is too big\n");
219 if (lseek(fd
, stat
.st_size
- (size
+ 8 + BOOTCONFIG_MAGIC_LEN
),
221 return pr_errno("Failed to lseek", -errno
);
223 ret
= load_xbc_fd(fd
, buf
, size
);
228 rcsum
= xbc_calc_checksum(*buf
, size
);
230 pr_err("checksum error: %d != %d\n", csum
, rcsum
);
234 ret
= xbc_init(*buf
, size
, &msg
, NULL
);
237 pr_err("parse error: %s.\n", msg
);
244 static void show_xbc_error(const char *data
, const char *msg
, int pos
)
249 pr_err("Error: %s.\n", msg
);
253 /* Note that pos starts from 0 but lin and col should start from 1. */
255 for (i
= 0; i
< pos
; i
++) {
256 if (data
[i
] == '\n') {
261 pr_err("Parse Error: %s at %d:%d\n", msg
, lin
, col
);
265 static int init_xbc_with_error(char *buf
, int len
)
267 char *copy
= strdup(buf
);
274 ret
= xbc_init(buf
, len
, &msg
, &pos
);
276 show_xbc_error(copy
, msg
, pos
);
282 static int show_xbc(const char *path
, bool list
)
288 ret
= stat(path
, &st
);
291 pr_err("Failed to stat %s: %d\n", path
, ret
);
295 fd
= open(path
, O_RDONLY
);
298 pr_err("Failed to open initrd %s: %d\n", path
, ret
);
302 ret
= load_xbc_from_initrd(fd
, &buf
);
305 pr_err("Failed to load a boot config from initrd: %d\n", ret
);
308 /* Assume a bootconfig file if it is enough small */
309 if (ret
== 0 && st
.st_size
<= XBC_DATA_MAX
) {
310 ret
= load_xbc_file(path
, &buf
);
312 pr_err("Failed to load a boot config: %d\n", ret
);
315 if (init_xbc_with_error(buf
, ret
) < 0)
321 xbc_show_compact_tree();
329 static int delete_xbc(const char *path
)
332 int ret
= 0, fd
, size
;
335 fd
= open(path
, O_RDWR
);
338 pr_err("Failed to open initrd %s: %d\n", path
, ret
);
342 size
= load_xbc_from_initrd(fd
, &buf
);
345 pr_err("Failed to load a boot config from initrd: %d\n", ret
);
346 } else if (size
> 0) {
347 ret
= fstat(fd
, &stat
);
349 ret
= ftruncate(fd
, stat
.st_size
350 - size
- 8 - BOOTCONFIG_MAGIC_LEN
);
353 } /* Ignore if there is no boot config in initrd */
361 static int apply_xbc(const char *path
, const char *xbc_path
)
363 char *buf
, *data
, *p
;
371 ret
= load_xbc_file(xbc_path
, &buf
);
373 pr_err("Failed to load %s : %d\n", xbc_path
, ret
);
376 size
= strlen(buf
) + 1;
377 csum
= xbc_calc_checksum(buf
, size
);
379 /* Backup the bootconfig data */
380 data
= calloc(size
+ BOOTCONFIG_ALIGN
+
381 sizeof(uint32_t) + sizeof(uint32_t) + BOOTCONFIG_MAGIC_LEN
, 1);
384 memcpy(data
, buf
, size
);
386 /* Check the data format */
387 ret
= xbc_init(buf
, size
, &msg
, &pos
);
389 show_xbc_error(data
, msg
, pos
);
395 printf("Apply %s to %s\n", xbc_path
, path
);
396 xbc_get_info(&ret
, NULL
);
397 printf("\tNumber of nodes: %d\n", ret
);
398 printf("\tSize: %u bytes\n", (unsigned int)size
);
399 printf("\tChecksum: %d\n", (unsigned int)csum
);
401 /* TODO: Check the options by schema */
405 /* Remove old boot config if exists */
406 ret
= delete_xbc(path
);
408 pr_err("Failed to delete previous boot config: %d\n", ret
);
414 fd
= open(path
, O_RDWR
| O_APPEND
);
417 pr_err("Failed to open %s: %d\n", path
, ret
);
421 /* TODO: Ensure the @path is initramfs/initrd image */
422 if (fstat(fd
, &stat
) < 0) {
424 pr_err("Failed to get the size of %s\n", path
);
428 /* To align up the total size to BOOTCONFIG_ALIGN, get padding size */
429 total_size
= stat
.st_size
+ size
+ sizeof(uint32_t) * 2 + BOOTCONFIG_MAGIC_LEN
;
430 pad
= ((total_size
+ BOOTCONFIG_ALIGN
- 1) & (~BOOTCONFIG_ALIGN_MASK
)) - total_size
;
435 *(uint32_t *)p
= htole32(size
);
436 p
+= sizeof(uint32_t);
438 *(uint32_t *)p
= htole32(csum
);
439 p
+= sizeof(uint32_t);
441 memcpy(p
, BOOTCONFIG_MAGIC
, BOOTCONFIG_MAGIC_LEN
);
442 p
+= BOOTCONFIG_MAGIC_LEN
;
444 total_size
= p
- data
;
446 ret
= write(fd
, data
, total_size
);
447 if (ret
< total_size
) {
450 pr_err("Failed to apply a boot config: %d\n", ret
);
463 /* Map the partial write to -ENOSPC */
466 if (ftruncate(fd
, stat
.st_size
) < 0) {
468 pr_err("Failed to rollback the write error: %d\n", ret
);
469 pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path
);
474 static int usage(void)
476 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
477 "Or bootconfig <CONFIG>\n"
478 " Apply, delete or show boot config to initrd.\n"
480 " -a <config>: Apply boot config to initrd\n"
481 " -d : Delete boot config file from initrd\n"
482 " -l : list boot config in initrd or file\n\n"
483 " If no option is given, show the bootconfig in the given file.\n");
487 int main(int argc
, char **argv
)
491 bool delete = false, list
= false;
494 while ((opt
= getopt(argc
, argv
, "hda:l")) != -1) {
511 if ((apply
&& delete) || (delete && list
) || (apply
&& list
)) {
512 pr_err("Error: You can give one of -a, -d or -l at once.\n");
516 if (optind
>= argc
) {
517 pr_err("Error: No initrd is specified.\n");
524 return apply_xbc(path
, apply
);
526 return delete_xbc(path
);
528 return show_xbc(path
, list
);