1 // SPDX-License-Identifier: GPL-2.0
3 * Boot config tool for initrd image
14 #include <linux/kernel.h>
15 #include <linux/bootconfig.h>
19 static int xbc_show_array(struct xbc_node *node)
24 xbc_array_for_each_value(node, val) {
25 printf("\"%s\"%s", val, node->next ? ", " : ";\n");
31 static void xbc_show_compact_tree(void)
33 struct xbc_node *node, *cnode;
36 node = xbc_root_node();
37 while (node && xbc_node_is_key(node)) {
38 for (i = 0; i < depth; i++)
40 cnode = xbc_node_get_child(node);
41 while (cnode && xbc_node_is_key(cnode) && !cnode->next) {
42 printf("%s.", xbc_node_get_data(node));
44 cnode = xbc_node_get_child(node);
46 if (cnode && xbc_node_is_key(cnode)) {
47 printf("%s {\n", xbc_node_get_data(node));
51 } else if (cnode && xbc_node_is_value(cnode)) {
52 printf("%s = ", xbc_node_get_data(node));
54 xbc_show_array(cnode);
56 printf("\"%s\";\n", xbc_node_get_data(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;
135 ret = fstat(fd, &stat);
139 if (stat.st_size < 8)
142 if (lseek(fd, -8, SEEK_END) < 0) {
143 pr_err("Failed to lseek: %d\n", -errno);
147 if (read(fd, &size, sizeof(u32)) < 0)
150 if (read(fd, &csum, sizeof(u32)) < 0)
153 /* Wrong size, maybe no boot config here */
154 if (stat.st_size < size + 8)
157 if (lseek(fd, stat.st_size - 8 - size, SEEK_SET) < 0) {
158 pr_err("Failed to lseek: %d\n", -errno);
162 ret = load_xbc_fd(fd, buf, size);
166 /* Wrong Checksum, maybe no boot config here */
167 rcsum = checksum((unsigned char *)*buf, size);
169 pr_err("checksum error: %d != %d\n", csum, rcsum);
173 ret = xbc_init(*buf);
174 /* Wrong data, maybe no boot config here */
181 int show_xbc(const char *path)
186 fd = open(path, O_RDONLY);
188 pr_err("Failed to open initrd %s: %d\n", path, fd);
192 ret = load_xbc_from_initrd(fd, &buf);
194 pr_err("Failed to load a boot config from initrd: %d\n", ret);
196 xbc_show_compact_tree();
204 int delete_xbc(const char *path)
207 int ret = 0, fd, size;
210 fd = open(path, O_RDWR);
212 pr_err("Failed to open initrd %s: %d\n", path, fd);
217 * Suppress error messages in xbc_init() because it can be just a
218 * data which concidentally matches the size and checksum footer.
221 size = load_xbc_from_initrd(fd, &buf);
225 pr_err("Failed to load a boot config from initrd: %d\n", ret);
226 } else if (size > 0) {
227 ret = fstat(fd, &stat);
229 ret = ftruncate(fd, stat.st_size - size - 8);
232 } /* Ignore if there is no boot config in initrd */
240 int apply_xbc(const char *path, const char *xbc_path)
246 ret = load_xbc_file(xbc_path, &buf);
248 pr_err("Failed to load %s : %d\n", xbc_path, ret);
251 size = strlen(buf) + 1;
252 csum = checksum((unsigned char *)buf, size);
254 /* Prepare xbc_path data */
255 data = malloc(size + 8);
259 *(u32 *)(data + size) = size;
260 *(u32 *)(data + size + 4) = csum;
262 /* Check the data format */
265 pr_err("Failed to parse %s: %d\n", xbc_path, ret);
270 printf("Apply %s to %s\n", xbc_path, path);
271 printf("\tNumber of nodes: %d\n", ret);
272 printf("\tSize: %u bytes\n", (unsigned int)size);
273 printf("\tChecksum: %d\n", (unsigned int)csum);
275 /* TODO: Check the options by schema */
279 /* Remove old boot config if exists */
280 ret = delete_xbc(path);
282 pr_err("Failed to delete previous boot config: %d\n", ret);
287 fd = open(path, O_RDWR | O_APPEND);
289 pr_err("Failed to open %s: %d\n", path, fd);
292 /* TODO: Ensure the @path is initramfs/initrd image */
293 ret = write(fd, data, size + 8);
295 pr_err("Failed to apply a boot config: %d\n", ret);
306 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
307 " Apply, delete or show boot config to initrd.\n"
309 " -a <config>: Apply boot config to initrd\n"
310 " -d : Delete boot config file from initrd\n\n"
311 " If no option is given, show current applied boot config.\n");
315 int main(int argc, char **argv)
322 while ((opt = getopt(argc, argv, "hda:")) != -1) {
336 if (apply && delete) {
337 pr_err("Error: You can not specify both -a and -d at once.\n");
341 if (optind >= argc) {
342 pr_err("Error: No initrd is specified.\n");
349 return apply_xbc(path, apply);
351 return delete_xbc(path);
353 return show_xbc(path);