diff options
Diffstat (limited to 'tools/bootconfig/main.c')
| -rw-r--r-- | tools/bootconfig/main.c | 84 |
1 files changed, 72 insertions, 12 deletions
diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c index 55d59ed507d5..17d971d47f87 100644 --- a/tools/bootconfig/main.c +++ b/tools/bootconfig/main.c @@ -140,6 +140,9 @@ static int load_xbc_fd(int fd, char **buf, int size) { int ret; + if (size < 0 || size > XBC_DATA_MAX) + return -EINVAL; + *buf = malloc(size + 1); if (!*buf) return -ENOMEM; @@ -162,8 +165,18 @@ static int load_xbc_file(const char *path, char **buf) if (fd < 0) return -errno; ret = fstat(fd, &stat); - if (ret < 0) - return -errno; + if (ret < 0) { + ret = -errno; + close(fd); + return ret; + } + + if (stat.st_size > XBC_DATA_MAX) { + pr_err("%s size is too big\n", path); + ret = -E2BIG; + close(fd); + return ret; + } ret = load_xbc_fd(fd, buf, stat.st_size); @@ -215,7 +228,8 @@ static int load_xbc_from_initrd(int fd, char **buf) csum = le32toh(csum); /* Wrong size error */ - if (stat.st_size < size + BOOTCONFIG_FOOTER_SIZE) { + if (size > XBC_DATA_MAX || + size > stat.st_size - BOOTCONFIG_FOOTER_SIZE) { pr_err("bootconfig size is too big\n"); return -E2BIG; } @@ -283,7 +297,41 @@ static int init_xbc_with_error(char *buf, int len) return ret; } -static int show_xbc(const char *path, bool list) +static int show_xbc_kernel_cmdline(void) +{ + struct xbc_node *root; + char *buf = NULL; + int len, ret; + + root = xbc_find_node("kernel"); + if (!root) + return 0; /* no kernel.* keys: emit empty output */ + + len = xbc_snprint_cmdline(NULL, 0, root); + if (len < 0) { + pr_err("Failed to size cmdline output: %d\n", len); + return len; + } + if (len == 0) + return 0; + + buf = malloc(len + 1); + if (!buf) + return -ENOMEM; + + ret = xbc_snprint_cmdline(buf, len + 1, root); + if (ret < 0) { + pr_err("Failed to render cmdline output: %d\n", ret); + free(buf); + return ret; + } + + fputs(buf, stdout); + free(buf); + return 0; +} + +static int show_xbc(const char *path, bool list, bool render_cmdline) { int ret, fd; char *buf = NULL; @@ -319,11 +367,14 @@ static int show_xbc(const char *path, bool list) if (init_xbc_with_error(buf, ret) < 0) goto out; } - if (list) + if (render_cmdline) + ret = show_xbc_kernel_cmdline(); + else if (list) xbc_show_list(); else xbc_show_compact_tree(); - ret = 0; + if (ret > 0) + ret = 0; out: free(buf); @@ -387,8 +438,10 @@ static int apply_xbc(const char *path, const char *xbc_path) /* Backup the bootconfig data */ data = calloc(size + BOOTCONFIG_ALIGN + BOOTCONFIG_FOOTER_SIZE, 1); - if (!data) + if (!data) { + free(buf); return -ENOMEM; + } memcpy(data, buf, size); /* Check the data format */ @@ -483,7 +536,10 @@ static int usage(void) " Options:\n" " -a <config>: Apply boot config to initrd\n" " -d : Delete boot config file from initrd\n" - " -l : list boot config in initrd or file\n\n" + " -l : list boot config in initrd or file\n" + " -C : render the kernel.* subtree as a flat cmdline\n" + " string (suitable for embedding in a kernel image)\n" + " and print it to stdout\n\n" " If no option is given, show the bootconfig in the given file.\n"); return -1; } @@ -492,10 +548,11 @@ int main(int argc, char **argv) { char *path = NULL; char *apply = NULL; + bool render_cmdline = false; bool delete = false, list = false; int opt; - while ((opt = getopt(argc, argv, "hda:l")) != -1) { + while ((opt = getopt(argc, argv, "hda:lC")) != -1) { switch (opt) { case 'd': delete = true; @@ -506,14 +563,17 @@ int main(int argc, char **argv) case 'l': list = true; break; + case 'C': + render_cmdline = true; + break; case 'h': default: return usage(); } } - if ((apply && delete) || (delete && list) || (apply && list)) { - pr_err("Error: You can give one of -a, -d or -l at once.\n"); + if ((!!apply + !!delete + !!list + !!render_cmdline) > 1) { + pr_err("Error: You can give one of -a, -d, -l or -C at once.\n"); return usage(); } @@ -529,5 +589,5 @@ int main(int argc, char **argv) else if (delete) return delete_xbc(path); - return show_xbc(path, list); + return show_xbc(path, list, render_cmdline); } |
