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) {
402 pr_err("Failed to get the size of %s\n", path
);
406 /* To align up the total size to BOOTCONFIG_ALIGN, get padding size */
407 total_size
= stat
.st_size
+ size
+ sizeof(u32
) * 2 + BOOTCONFIG_MAGIC_LEN
;
408 pad
= ((total_size
+ BOOTCONFIG_ALIGN
- 1) & (~BOOTCONFIG_ALIGN_MASK
)) - total_size
;
413 *(u32
*)p
= htole32(size
);
416 *(u32
*)p
= htole32(csum
);
419 memcpy(p
, BOOTCONFIG_MAGIC
, BOOTCONFIG_MAGIC_LEN
);
420 p
+= BOOTCONFIG_MAGIC_LEN
;
422 total_size
= p
- data
;
424 ret
= write(fd
, data
, total_size
);
425 if (ret
< total_size
) {
428 pr_err("Failed to apply a boot config: %d\n", ret
);
441 /* Map the partial write to -ENOSPC */
444 if (ftruncate(fd
, stat
.st_size
) < 0) {
446 pr_err("Failed to rollback the write error: %d\n", ret
);
447 pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path
);
452 static int usage(void)
454 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
455 "Or bootconfig <CONFIG>\n"
456 " Apply, delete or show boot config to initrd.\n"
458 " -a <config>: Apply boot config to initrd\n"
459 " -d : Delete boot config file from initrd\n"
460 " -l : list boot config in initrd or file\n\n"
461 " If no option is given, show the bootconfig in the given file.\n");
465 int main(int argc
, char **argv
)
469 bool delete = false, list
= false;
472 while ((opt
= getopt(argc
, argv
, "hda:l")) != -1) {
489 if ((apply
&& delete) || (delete && list
) || (apply
&& list
)) {
490 pr_err("Error: You can give one of -a, -d or -l at once.\n");
494 if (optind
>= argc
) {
495 pr_err("Error: No initrd is specified.\n");
502 return apply_xbc(path
, apply
);
504 return delete_xbc(path
);
506 return show_xbc(path
, list
);