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
)
23 xbc_array_for_each_value(node
, val
) {
28 printf("%c%s%c%s", q
, val
, q
, node
->next
? ", " : ";\n");
34 static void xbc_show_compact_tree(void)
36 struct xbc_node
*node
, *cnode
;
39 node
= xbc_root_node();
40 while (node
&& xbc_node_is_key(node
)) {
41 for (i
= 0; i
< depth
; i
++)
43 cnode
= xbc_node_get_child(node
);
44 while (cnode
&& xbc_node_is_key(cnode
) && !cnode
->next
) {
45 printf("%s.", xbc_node_get_data(node
));
47 cnode
= xbc_node_get_child(node
);
49 if (cnode
&& xbc_node_is_key(cnode
)) {
50 printf("%s {\n", xbc_node_get_data(node
));
54 } else if (cnode
&& xbc_node_is_value(cnode
)) {
55 printf("%s = ", xbc_node_get_data(node
));
56 xbc_show_value(cnode
);
58 printf("%s;\n", xbc_node_get_data(node
));
62 node
= xbc_node_get_next(node
);
66 node
= xbc_node_get_parent(node
);
69 if (!xbc_node_get_child(node
)->next
)
72 for (i
= 0; i
< depth
; i
++)
76 node
= xbc_node_get_next(node
);
80 /* Simple real checksum */
81 int checksum(unsigned char *buf
, int len
)
85 for (i
= 0; i
< len
; i
++)
91 #define PAGE_SIZE 4096
93 int load_xbc_fd(int fd
, char **buf
, int size
)
97 *buf
= malloc(size
+ 1);
101 ret
= read(fd
, *buf
, size
);
109 /* Return the read size or -errno */
110 int load_xbc_file(const char *path
, char **buf
)
115 fd
= open(path
, O_RDONLY
);
118 ret
= fstat(fd
, &stat
);
122 ret
= load_xbc_fd(fd
, buf
, stat
.st_size
);
129 int load_xbc_from_initrd(int fd
, char **buf
)
133 u32 size
= 0, csum
= 0, rcsum
;
134 char magic
[BOOTCONFIG_MAGIC_LEN
];
137 ret
= fstat(fd
, &stat
);
141 if (stat
.st_size
< 8 + BOOTCONFIG_MAGIC_LEN
)
144 if (lseek(fd
, -BOOTCONFIG_MAGIC_LEN
, SEEK_END
) < 0) {
145 pr_err("Failed to lseek: %d\n", -errno
);
148 if (read(fd
, magic
, BOOTCONFIG_MAGIC_LEN
) < 0)
150 /* Check the bootconfig magic bytes */
151 if (memcmp(magic
, BOOTCONFIG_MAGIC
, BOOTCONFIG_MAGIC_LEN
) != 0)
154 if (lseek(fd
, -(8 + BOOTCONFIG_MAGIC_LEN
), SEEK_END
) < 0) {
155 pr_err("Failed to lseek: %d\n", -errno
);
159 if (read(fd
, &size
, sizeof(u32
)) < 0)
162 if (read(fd
, &csum
, sizeof(u32
)) < 0)
165 /* Wrong size error */
166 if (stat
.st_size
< size
+ 8 + BOOTCONFIG_MAGIC_LEN
) {
167 pr_err("bootconfig size is too big\n");
171 if (lseek(fd
, stat
.st_size
- (size
+ 8 + BOOTCONFIG_MAGIC_LEN
),
173 pr_err("Failed to lseek: %d\n", -errno
);
177 ret
= load_xbc_fd(fd
, buf
, size
);
182 rcsum
= checksum((unsigned char *)*buf
, size
);
184 pr_err("checksum error: %d != %d\n", csum
, rcsum
);
188 ret
= xbc_init(*buf
, &msg
, NULL
);
191 pr_err("parse error: %s.\n", msg
);
198 int show_xbc(const char *path
)
203 fd
= open(path
, O_RDONLY
);
205 pr_err("Failed to open initrd %s: %d\n", path
, fd
);
209 ret
= load_xbc_from_initrd(fd
, &buf
);
211 pr_err("Failed to load a boot config from initrd: %d\n", ret
);
214 xbc_show_compact_tree();
223 int delete_xbc(const char *path
)
226 int ret
= 0, fd
, size
;
229 fd
= open(path
, O_RDWR
);
231 pr_err("Failed to open initrd %s: %d\n", path
, fd
);
235 size
= load_xbc_from_initrd(fd
, &buf
);
238 pr_err("Failed to load a boot config from initrd: %d\n", ret
);
239 } else if (size
> 0) {
240 ret
= fstat(fd
, &stat
);
242 ret
= ftruncate(fd
, stat
.st_size
243 - size
- 8 - BOOTCONFIG_MAGIC_LEN
);
246 } /* Ignore if there is no boot config in initrd */
254 static void show_xbc_error(const char *data
, const char *msg
, int pos
)
259 pr_err("Error: %s.\n", msg
);
263 /* Note that pos starts from 0 but lin and col should start from 1. */
265 for (i
= 0; i
< pos
; i
++) {
266 if (data
[i
] == '\n') {
271 pr_err("Parse Error: %s at %d:%d\n", msg
, lin
, col
);
275 int apply_xbc(const char *path
, const char *xbc_path
)
283 ret
= load_xbc_file(xbc_path
, &buf
);
285 pr_err("Failed to load %s : %d\n", xbc_path
, ret
);
288 size
= strlen(buf
) + 1;
289 csum
= checksum((unsigned char *)buf
, size
);
291 /* Prepare xbc_path data */
292 data
= malloc(size
+ 8);
296 *(u32
*)(data
+ size
) = size
;
297 *(u32
*)(data
+ size
+ 4) = csum
;
299 /* Check the data format */
300 ret
= xbc_init(buf
, &msg
, &pos
);
302 show_xbc_error(data
, msg
, pos
);
308 printf("Apply %s to %s\n", xbc_path
, path
);
309 printf("\tNumber of nodes: %d\n", ret
);
310 printf("\tSize: %u bytes\n", (unsigned int)size
);
311 printf("\tChecksum: %d\n", (unsigned int)csum
);
313 /* TODO: Check the options by schema */
317 /* Remove old boot config if exists */
318 ret
= delete_xbc(path
);
320 pr_err("Failed to delete previous boot config: %d\n", ret
);
326 fd
= open(path
, O_RDWR
| O_APPEND
);
328 pr_err("Failed to open %s: %d\n", path
, fd
);
332 /* TODO: Ensure the @path is initramfs/initrd image */
333 ret
= write(fd
, data
, size
+ 8);
335 pr_err("Failed to apply a boot config: %d\n", ret
);
338 /* Write a magic word of the bootconfig */
339 ret
= write(fd
, BOOTCONFIG_MAGIC
, BOOTCONFIG_MAGIC_LEN
);
341 pr_err("Failed to apply a boot config magic: %d\n", ret
);
354 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
355 " Apply, delete or show boot config to initrd.\n"
357 " -a <config>: Apply boot config to initrd\n"
358 " -d : Delete boot config file from initrd\n\n"
359 " If no option is given, show current applied boot config.\n");
363 int main(int argc
, char **argv
)
370 while ((opt
= getopt(argc
, argv
, "hda:")) != -1) {
384 if (apply
&& delete) {
385 pr_err("Error: You can not specify both -a and -d at once.\n");
389 if (optind
>= argc
) {
390 pr_err("Error: No initrd is specified.\n");
397 return apply_xbc(path
, apply
);
399 return delete_xbc(path
);
401 return show_xbc(path
);