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>
Tue, 19 Sep 2023 18:01:06 +0000 (20: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>
src/mktplinkfw2.c

index f25355a2f9bb519d86f966b3356c2f64ee40a7c5..d51e9ca00d6ac56a6463df581a6105464cc41699 100644 (file)
@@ -207,7 +207,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"
        );
 
@@ -507,6 +507,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);