tools/mktplinkfw2: add support to extract bootloader images
authorAlexander Couzens <lynxis@fe80.eu>
Tue, 7 May 2019 14:33:58 +0000 (16:33 +0200)
committerAlexander Couzens <lynxis@fe80.eu>
Mon, 5 Aug 2019 12:01:59 +0000 (14:01 +0200)
tplinkfw2 images from TP-Link contains a bootloader image in most cases.
-x will create a -bootloader.bin file.

Signed-off-by: Alexander Couzens <lynxis@fe80.eu>
tools/firmware-utils/src/mktplinkfw2.c

index 102d72b1cc67f184d9dcdecc03c3ef1280d513de..cec7c871268643d55df6dfac48b9d00b232e5b53 100644 (file)
@@ -205,7 +205,7 @@ static void usage(int status)
 "  -v <version>    set firmware version to <version>\n"
 "  -y <version>    set secondary version to <version>\n"
 "  -i <file>       inspect given firmware file <file>\n"
-"  -x              extract kernel and rootfs while inspecting (requires -i)\n"
+"  -x              extract bootloader, kernel and rootfs while inspecting (requires -i)\n"
 "  -h              show this screen\n"
        );
 
@@ -505,6 +505,23 @@ static int inspect_fw(void)
 
                printf("\n");
 
+               if (hdr->boot_len) {
+                       filename = malloc(strlen(inspect_info.file_name) + 8);
+                       sprintf(filename, "%s-bootloader", inspect_info.file_name);
+                       printf("Extracting bootloader to \"%s\"...\n", filename);
+                       fp = fopen(filename, "w");
+                       if (fp) {
+                               if (!fwrite(buf + sizeof(struct fw_header) + ntohl(hdr->boot_ofs),
+                                       ntohl(hdr->boot_len), 1, fp)) {
+                                       ERR("error in fwrite(): %s", strerror(errno));
+                               }
+                               fclose(fp);
+                       } else {
+                               ERR("error in fopen(): %s", strerror(errno));
+                       }
+                       free(filename);
+               }
+
                filename = malloc(strlen(inspect_info.file_name) + 8);
                sprintf(filename, "%s-kernel", inspect_info.file_name);
                printf("Extracting kernel to \"%s\"...\n", filename);