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_array(struct xbc_node
*node
)
22 xbc_array_for_each_value(node
, val
) {
23 printf("\"%s\"%s", val
, node
->next
? ", " : ";\n");
29 static void xbc_show_compact_tree(void)
31 struct xbc_node
*node
, *cnode
;
34 node
= xbc_root_node();
35 while (node
&& xbc_node_is_key(node
)) {
36 for (i
= 0; i
< depth
; i
++)
38 cnode
= xbc_node_get_child(node
);
39 while (cnode
&& xbc_node_is_key(cnode
) && !cnode
->next
) {
40 printf("%s.", xbc_node_get_data(node
));
42 cnode
= xbc_node_get_child(node
);
44 if (cnode
&& xbc_node_is_key(cnode
)) {
45 printf("%s {\n", xbc_node_get_data(node
));
49 } else if (cnode
&& xbc_node_is_value(cnode
)) {
50 printf("%s = ", xbc_node_get_data(node
));
52 xbc_show_array(cnode
);
54 printf("\"%s\";\n", xbc_node_get_data(cnode
));
56 printf("%s;\n", xbc_node_get_data(node
));
60 node
= xbc_node_get_next(node
);
64 node
= xbc_node_get_parent(node
);
67 if (!xbc_node_get_child(node
)->next
)
70 for (i
= 0; i
< depth
; i
++)
74 node
= xbc_node_get_next(node
);
78 /* Simple real checksum */
79 int checksum(unsigned char *buf
, int len
)
83 for (i
= 0; i
< len
; i
++)
89 #define PAGE_SIZE 4096
91 int load_xbc_fd(int fd
, char **buf
, int size
)
95 *buf
= malloc(size
+ 1);
99 ret
= read(fd
, *buf
, size
);
107 /* Return the read size or -errno */
108 int load_xbc_file(const char *path
, char **buf
)
113 fd
= open(path
, O_RDONLY
);
116 ret
= fstat(fd
, &stat
);
120 ret
= load_xbc_fd(fd
, buf
, stat
.st_size
);
127 int load_xbc_from_initrd(int fd
, char **buf
)
131 u32 size
= 0, csum
= 0, rcsum
;
132 char magic
[BOOTCONFIG_MAGIC_LEN
];
134 ret
= fstat(fd
, &stat
);
138 if (stat
.st_size
< 8 + BOOTCONFIG_MAGIC_LEN
)
141 if (lseek(fd
, -BOOTCONFIG_MAGIC_LEN
, SEEK_END
) < 0) {
142 pr_err("Failed to lseek: %d\n", -errno
);
145 if (read(fd
, magic
, BOOTCONFIG_MAGIC_LEN
) < 0)
147 /* Check the bootconfig magic bytes */
148 if (memcmp(magic
, BOOTCONFIG_MAGIC
, BOOTCONFIG_MAGIC_LEN
) != 0)
151 if (lseek(fd
, -(8 + BOOTCONFIG_MAGIC_LEN
), SEEK_END
) < 0) {
152 pr_err("Failed to lseek: %d\n", -errno
);
156 if (read(fd
, &size
, sizeof(u32
)) < 0)
159 if (read(fd
, &csum
, sizeof(u32
)) < 0)
162 /* Wrong size error */
163 if (stat
.st_size
< size
+ 8 + BOOTCONFIG_MAGIC_LEN
) {
164 pr_err("bootconfig size is too big\n");
168 if (lseek(fd
, stat
.st_size
- (size
+ 8 + BOOTCONFIG_MAGIC_LEN
),
170 pr_err("Failed to lseek: %d\n", -errno
);
174 ret
= load_xbc_fd(fd
, buf
, size
);
179 rcsum
= checksum((unsigned char *)*buf
, size
);
181 pr_err("checksum error: %d != %d\n", csum
, rcsum
);
185 ret
= xbc_init(*buf
);
193 int show_xbc(const char *path
)
198 fd
= open(path
, O_RDONLY
);
200 pr_err("Failed to open initrd %s: %d\n", path
, fd
);
204 ret
= load_xbc_from_initrd(fd
, &buf
);
206 pr_err("Failed to load a boot config from initrd: %d\n", ret
);
208 xbc_show_compact_tree();
216 int delete_xbc(const char *path
)
219 int ret
= 0, fd
, size
;
222 fd
= open(path
, O_RDWR
);
224 pr_err("Failed to open initrd %s: %d\n", path
, fd
);
228 size
= load_xbc_from_initrd(fd
, &buf
);
231 pr_err("Failed to load a boot config from initrd: %d\n", ret
);
232 } else if (size
> 0) {
233 ret
= fstat(fd
, &stat
);
235 ret
= ftruncate(fd
, stat
.st_size
236 - size
- 8 - BOOTCONFIG_MAGIC_LEN
);
239 } /* Ignore if there is no boot config in initrd */
247 int apply_xbc(const char *path
, const char *xbc_path
)
253 ret
= load_xbc_file(xbc_path
, &buf
);
255 pr_err("Failed to load %s : %d\n", xbc_path
, ret
);
258 size
= strlen(buf
) + 1;
259 csum
= checksum((unsigned char *)buf
, size
);
261 /* Prepare xbc_path data */
262 data
= malloc(size
+ 8);
266 *(u32
*)(data
+ size
) = size
;
267 *(u32
*)(data
+ size
+ 4) = csum
;
269 /* Check the data format */
272 pr_err("Failed to parse %s: %d\n", xbc_path
, ret
);
277 printf("Apply %s to %s\n", xbc_path
, path
);
278 printf("\tNumber of nodes: %d\n", ret
);
279 printf("\tSize: %u bytes\n", (unsigned int)size
);
280 printf("\tChecksum: %d\n", (unsigned int)csum
);
282 /* TODO: Check the options by schema */
286 /* Remove old boot config if exists */
287 ret
= delete_xbc(path
);
289 pr_err("Failed to delete previous boot config: %d\n", ret
);
294 fd
= open(path
, O_RDWR
| O_APPEND
);
296 pr_err("Failed to open %s: %d\n", path
, fd
);
299 /* TODO: Ensure the @path is initramfs/initrd image */
300 ret
= write(fd
, data
, size
+ 8);
302 pr_err("Failed to apply a boot config: %d\n", ret
);
305 /* Write a magic word of the bootconfig */
306 ret
= write(fd
, BOOTCONFIG_MAGIC
, BOOTCONFIG_MAGIC_LEN
);
308 pr_err("Failed to apply a boot config magic: %d\n", ret
);
319 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
320 " Apply, delete or show boot config to initrd.\n"
322 " -a <config>: Apply boot config to initrd\n"
323 " -d : Delete boot config file from initrd\n\n"
324 " If no option is given, show current applied boot config.\n");
328 int main(int argc
, char **argv
)
335 while ((opt
= getopt(argc
, argv
, "hda:")) != -1) {
349 if (apply
&& delete) {
350 pr_err("Error: You can not specify both -a and -d at once.\n");
354 if (optind
>= argc
) {
355 pr_err("Error: No initrd is specified.\n");
362 return apply_xbc(path
, apply
);
364 return delete_xbc(path
);
366 return show_xbc(path
);