From: Rafał Miłecki Date: Mon, 29 Jun 2020 10:38:18 +0000 (+0200) Subject: lxlfw: support extracting image X-Git-Url: http://git.openwrt.org/?a=commitdiff_plain;h=eaf2ea28dbe62f284b98a9238eb6c268aa08f266;p=project%2Ffirmware-utils.git lxlfw: support extracting image This may be useful for flashing image or creating signature for it. Signed-off-by: Rafał Miłecki --- diff --git a/src/lxlfw.c b/src/lxlfw.c index 0dc44d1..62f58f9 100644 --- a/src/lxlfw.c +++ b/src/lxlfw.c @@ -344,6 +344,77 @@ out: return err; } +/************************************************** + * Extract + **************************************************/ + +static int lxlfw_extract(int argc, char **argv) { + struct lxl_hdr hdr; + char *out_path = NULL; + ssize_t bytes; + int err = 0; + FILE *lxl; + FILE *out; + int c; + + if (argc < 3) { + fprintf(stderr, "Missing argument\n"); + err = -EINVAL; + goto out; + } + + optind = 3; + while ((c = getopt(argc, argv, "O:")) != -1) { + switch (c) { + case 'O': + out_path = optarg; + break; + } + } + + if (!out_path) { + fprintf(stderr, "Missing output file path\n"); + err = -EINVAL; + goto out; + } + + lxl = lxlfw_open(argv[2], &hdr); + if (!lxl) { + fprintf(stderr, "Failed to open \"%s\" Luxul firmware\n", argv[2]); + err = -ENOENT; + goto out; + } + + fseek(lxl, le32_to_cpu(hdr.hdr_len), SEEK_SET); + + if (!strcmp(out_path, "-")) { + out = stdout; + } else { + out = fopen(out_path, "w+"); + if (!out) { + fprintf(stderr, "Failed to open \"%s\" file\n", out_path); + err = -EIO; + goto err_close_lxl; + } + } + + bytes = lxlfw_copy_data(lxl, out, 0); + if (bytes < 0) { + fprintf(stderr, "Failed to copy image: %zd\n", bytes); + err = -EIO; + goto err_close_lxl; + } + + if (out != stdout) { + fclose(out); + } + +err_close_lxl: + fclose(lxl); +out: + return err; +} + /************************************************** * Blobs **************************************************/ @@ -771,6 +842,10 @@ static void usage() { printf("Get info about Luxul firmware:\n"); printf("\tlxlfw info \n"); printf("\n"); + printf("Extract image from Luxul firmware:\n"); + printf("\tlxlfw extract [options]\n"); + printf("\t-O file\t\t\t\toutput file (- for stdout)\n"); + printf("\n"); printf("Extract blobs from Luxul firmware:\n"); printf("\tlxlfw blobs [options]\n"); printf("\n"); @@ -789,6 +864,8 @@ int main(int argc, char **argv) { if (argc > 1) { if (!strcmp(argv[1], "info")) return lxlfw_info(argc, argv); + else if (!strcmp(argv[1], "extract")) + return lxlfw_extract(argc, argv); else if (!strcmp(argv[1], "blobs")) return lxlfw_blobs(argc, argv); else if (!strcmp(argv[1], "create"))