firmware-utils: mktplinkfw: rework combined image option
[openwrt/openwrt.git] / tools / firmware-utils / src / mktplinkfw.c
1 /*
2 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
3 *
4 * This tool was based on:
5 * TP-Link WR941 V2 firmware checksum fixing tool.
6 * Copyright (C) 2008,2009 Wang Jian <lark@linux.net.cn>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
11 *
12 */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stdint.h>
17 #include <string.h>
18 #include <unistd.h> /* for unlink() */
19 #include <libgen.h>
20 #include <getopt.h> /* for getopt() */
21 #include <stdarg.h>
22 #include <stdbool.h>
23 #include <endian.h>
24 #include <errno.h>
25 #include <sys/stat.h>
26
27 #include <arpa/inet.h>
28 #include <netinet/in.h>
29
30 #include "md5.h"
31
32 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
33 #define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
34
35 #define HEADER_VERSION_V1 0x01000000
36 #define HEADER_VERSION_V2 0x02000000
37
38 #define MD5SUM_LEN 16
39
40 struct file_info {
41 char *file_name; /* name of the file */
42 uint32_t file_size; /* length of the file */
43 };
44
45 struct fw_header {
46 uint32_t version; /* header version */
47 char vendor_name[24];
48 char fw_version[36];
49 uint32_t hw_id; /* hardware id */
50 uint32_t hw_rev; /* hardware revision */
51 uint32_t region_code; /* region code */
52 uint8_t md5sum1[MD5SUM_LEN];
53 uint32_t unk2;
54 uint8_t md5sum2[MD5SUM_LEN];
55 uint32_t unk3;
56 uint32_t kernel_la; /* kernel load address */
57 uint32_t kernel_ep; /* kernel entry point */
58 uint32_t fw_length; /* total length of the firmware */
59 uint32_t kernel_ofs; /* kernel data offset */
60 uint32_t kernel_len; /* kernel data length */
61 uint32_t rootfs_ofs; /* rootfs data offset */
62 uint32_t rootfs_len; /* rootfs data length */
63 uint32_t boot_ofs; /* bootloader data offset */
64 uint32_t boot_len; /* bootloader data length */
65 uint16_t ver_hi;
66 uint16_t ver_mid;
67 uint16_t ver_lo;
68 uint8_t pad[130];
69 char region_str1[32];
70 char region_str2[32];
71 uint8_t pad2[160];
72 } __attribute__ ((packed));
73
74 struct flash_layout {
75 char *id;
76 uint32_t fw_max_len;
77 uint32_t kernel_la;
78 uint32_t kernel_ep;
79 uint32_t rootfs_ofs;
80 };
81
82 struct fw_region {
83 char name[4];
84 uint32_t code;
85 };
86
87
88 /*
89 * Globals
90 */
91 static char *ofname;
92 static char *progname;
93 static char *vendor = "TP-LINK Technologies";
94 static char *version = "ver. 1.0";
95 static char *fw_ver = "0.0.0";
96 static uint32_t hdr_ver = HEADER_VERSION_V1;
97
98 static char *layout_id;
99 static struct flash_layout *layout;
100 static char *opt_hw_id;
101 static uint32_t hw_id;
102 static char *opt_hw_rev;
103 static uint32_t hw_rev;
104 static uint32_t opt_hdr_ver = 1;
105 static char *country;
106 static const struct fw_region *region;
107 static int fw_ver_lo;
108 static int fw_ver_mid;
109 static int fw_ver_hi;
110 static struct file_info kernel_info;
111 static uint32_t kernel_la = 0;
112 static uint32_t kernel_ep = 0;
113 static uint32_t kernel_len = 0;
114 static struct file_info rootfs_info;
115 static uint32_t rootfs_ofs = 0;
116 static uint32_t rootfs_align;
117 static struct file_info boot_info;
118 static int combined;
119 static int strip_padding;
120 static int add_jffs2_eof;
121 static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
122 static uint32_t fw_max_len;
123 static uint32_t reserved_space;
124
125 static struct file_info inspect_info;
126 static int extract = 0;
127 static bool endian_swap = false;
128
129 static const char md5salt_normal[MD5SUM_LEN] = {
130 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
131 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
132 };
133
134 static const char md5salt_boot[MD5SUM_LEN] = {
135 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
136 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
137 };
138
139 static struct flash_layout layouts[] = {
140 {
141 .id = "4M",
142 .fw_max_len = 0x3c0000,
143 .kernel_la = 0x80060000,
144 .kernel_ep = 0x80060000,
145 .rootfs_ofs = 0x140000,
146 }, {
147 .id = "4Mlzma",
148 .fw_max_len = 0x3c0000,
149 .kernel_la = 0x80060000,
150 .kernel_ep = 0x80060000,
151 .rootfs_ofs = 0x100000,
152 }, {
153 .id = "8M",
154 .fw_max_len = 0x7c0000,
155 .kernel_la = 0x80060000,
156 .kernel_ep = 0x80060000,
157 .rootfs_ofs = 0x140000,
158 }, {
159 .id = "8Mlzma",
160 .fw_max_len = 0x7c0000,
161 .kernel_la = 0x80060000,
162 .kernel_ep = 0x80060000,
163 .rootfs_ofs = 0x100000,
164 }, {
165 .id = "16M",
166 .fw_max_len = 0xf80000,
167 .kernel_la = 0x80060000,
168 .kernel_ep = 0x80060000,
169 .rootfs_ofs = 0x140000,
170 }, {
171 .id = "16Mlzma",
172 .fw_max_len = 0xf80000,
173 .kernel_la = 0x80060000,
174 .kernel_ep = 0x80060000,
175 .rootfs_ofs = 0x100000,
176 }, {
177 .id = "16Mppc",
178 .fw_max_len = 0xf80000,
179 .kernel_la = 0x00000000 ,
180 .kernel_ep = 0xc0000000,
181 .rootfs_ofs = 0x2a0000,
182 }, {
183 /* terminating entry */
184 }
185 };
186
187 static const struct fw_region regions[] = {
188 /* Default region (universal) uses code 0 as well */
189 {"US", 1},
190 {"EU", 0},
191 };
192
193 /*
194 * Message macros
195 */
196 #define ERR(fmt, ...) do { \
197 fflush(0); \
198 fprintf(stderr, "[%s] *** error: " fmt "\n", \
199 progname, ## __VA_ARGS__ ); \
200 } while (0)
201
202 #define ERRS(fmt, ...) do { \
203 int save = errno; \
204 fflush(0); \
205 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
206 progname, ## __VA_ARGS__, strerror(save)); \
207 } while (0)
208
209 #define DBG(fmt, ...) do { \
210 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
211 } while (0)
212
213 static struct flash_layout *find_layout(const char *id)
214 {
215 struct flash_layout *ret;
216 struct flash_layout *l;
217
218 ret = NULL;
219 for (l = layouts; l->id != NULL; l++){
220 if (strcasecmp(id, l->id) == 0) {
221 ret = l;
222 break;
223 }
224 };
225
226 return ret;
227 }
228
229 static const struct fw_region * find_region(const char *country) {
230 size_t i;
231
232 for (i = 0; i < ARRAY_SIZE(regions); i++) {
233 if (strcasecmp(regions[i].name, country) == 0)
234 return &regions[i];
235 }
236
237 return NULL;
238 }
239
240 static void usage(int status)
241 {
242 fprintf(stderr, "Usage: %s [OPTIONS...]\n", progname);
243 fprintf(stderr,
244 "\n"
245 "Options:\n"
246 " -c use combined kernel image\n"
247 " -e swap endianness in kernel load address and entry point\n"
248 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
249 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
250 " -H <hwid> use hardware id specified with <hwid>\n"
251 " -W <hwrev> use hardware revision specified with <hwrev>\n"
252 " -C <country> set region code to <country>\n"
253 " -F <id> use flash layout specified with <id>\n"
254 " -k <file> read kernel image from the file <file>\n"
255 " -r <file> read rootfs image from the file <file>\n"
256 " -a <align> align the rootfs start on an <align> bytes boundary\n"
257 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
258 " -o <file> write output to the file <file>\n"
259 " -s strip padding from the end of the image\n"
260 " -j add jffs2 end-of-filesystem markers\n"
261 " -N <vendor> set image vendor to <vendor>\n"
262 " -V <version> set image version to <version>\n"
263 " -v <version> set firmware version to <version>\n"
264 " -m <version> set header version to <version>\n"
265 " -i <file> inspect given firmware file <file>\n"
266 " -x extract kernel and rootfs while inspecting (requires -i)\n"
267 " -X <size> reserve <size> bytes in the firmware image (hexval prefixed with 0x)\n"
268 " -h show this screen\n"
269 );
270
271 exit(status);
272 }
273
274 static void get_md5(const char *data, int size, uint8_t *md5)
275 {
276 MD5_CTX ctx;
277
278 MD5_Init(&ctx);
279 MD5_Update(&ctx, data, size);
280 MD5_Final(md5, &ctx);
281 }
282
283 static int get_file_stat(struct file_info *fdata)
284 {
285 struct stat st;
286 int res;
287
288 if (fdata->file_name == NULL)
289 return 0;
290
291 res = stat(fdata->file_name, &st);
292 if (res){
293 ERRS("stat failed on %s", fdata->file_name);
294 return res;
295 }
296
297 fdata->file_size = st.st_size;
298 return 0;
299 }
300
301 static int read_to_buf(const struct file_info *fdata, char *buf)
302 {
303 FILE *f;
304 int ret = EXIT_FAILURE;
305
306 f = fopen(fdata->file_name, "r");
307 if (f == NULL) {
308 ERRS("could not open \"%s\" for reading", fdata->file_name);
309 goto out;
310 }
311
312 errno = 0;
313 fread(buf, fdata->file_size, 1, f);
314 if (errno != 0) {
315 ERRS("unable to read from file \"%s\"", fdata->file_name);
316 goto out_close;
317 }
318
319 ret = EXIT_SUCCESS;
320
321 out_close:
322 fclose(f);
323 out:
324 return ret;
325 }
326
327 static int check_options(void)
328 {
329 int ret;
330 int exceed_bytes;
331
332 if (inspect_info.file_name) {
333 ret = get_file_stat(&inspect_info);
334 if (ret)
335 return ret;
336
337 return 0;
338 } else if (extract) {
339 ERR("no firmware for inspection specified");
340 return -1;
341 }
342
343 if (opt_hw_id == NULL) {
344 ERR("hardware id not specified");
345 return -1;
346 }
347 hw_id = strtoul(opt_hw_id, NULL, 0);
348
349 if (!combined && layout_id == NULL) {
350 ERR("flash layout is not specified");
351 return -1;
352 }
353
354 if (opt_hw_rev)
355 hw_rev = strtoul(opt_hw_rev, NULL, 0);
356 else
357 hw_rev = 1;
358
359 if (country) {
360 region = find_region(country);
361 if (!region) {
362 ERR("unknown region code \"%s\"", country);
363 return -1;
364 }
365 }
366
367 if (combined) {
368 if (!kernel_la || !kernel_ep) {
369 ERR("kernel loading address and entry point must be specified for combined image");
370 return -1;
371 }
372 } else {
373 layout = find_layout(layout_id);
374 if (layout == NULL) {
375 ERR("unknown flash layout \"%s\"", layout_id);
376 return -1;
377 }
378
379 if (!kernel_la)
380 kernel_la = layout->kernel_la;
381 if (!kernel_ep)
382 kernel_ep = layout->kernel_ep;
383 if (!rootfs_ofs)
384 rootfs_ofs = layout->rootfs_ofs;
385
386 if (reserved_space > layout->fw_max_len) {
387 ERR("reserved space is not valid");
388 return -1;
389 }
390 }
391
392 if (kernel_info.file_name == NULL) {
393 ERR("no kernel image specified");
394 return -1;
395 }
396
397 ret = get_file_stat(&kernel_info);
398 if (ret)
399 return ret;
400
401 kernel_len = kernel_info.file_size;
402
403 if (!combined) {
404 fw_max_len = layout->fw_max_len - reserved_space;
405
406 if (rootfs_info.file_name == NULL) {
407 ERR("no rootfs image specified");
408 return -1;
409 }
410
411 ret = get_file_stat(&rootfs_info);
412 if (ret)
413 return ret;
414
415 if (rootfs_align) {
416 kernel_len += sizeof(struct fw_header);
417 kernel_len = ALIGN(kernel_len, rootfs_align);
418 kernel_len -= sizeof(struct fw_header);
419
420 DBG("kernel length aligned to %u", kernel_len);
421
422 exceed_bytes = kernel_len + rootfs_info.file_size - (fw_max_len - sizeof(struct fw_header));
423 if (exceed_bytes > 0) {
424 ERR("images are too big by %i bytes", exceed_bytes);
425 return -1;
426 }
427 } else {
428 exceed_bytes = kernel_info.file_size - (rootfs_ofs - sizeof(struct fw_header));
429 if (exceed_bytes > 0) {
430 ERR("kernel image is too big by %i bytes", exceed_bytes);
431 return -1;
432 }
433
434 exceed_bytes = rootfs_info.file_size - (fw_max_len - rootfs_ofs);
435 if (exceed_bytes > 0) {
436 ERR("rootfs image is too big by %i bytes", exceed_bytes);
437 return -1;
438 }
439 }
440 }
441
442 if (ofname == NULL) {
443 ERR("no output file specified");
444 return -1;
445 }
446
447 ret = sscanf(fw_ver, "%d.%d.%d", &fw_ver_hi, &fw_ver_mid, &fw_ver_lo);
448 if (ret != 3) {
449 ERR("invalid firmware version '%s'", fw_ver);
450 return -1;
451 }
452
453 if (opt_hdr_ver == 1) {
454 hdr_ver = HEADER_VERSION_V1;
455 } else if (opt_hdr_ver == 2) {
456 hdr_ver = HEADER_VERSION_V2;
457 } else {
458 ERR("invalid header version '%u'", opt_hdr_ver);
459 return -1;
460 }
461
462 return 0;
463 }
464
465 static void fill_header(char *buf, int len)
466 {
467 struct fw_header *hdr = (struct fw_header *)buf;
468
469 memset(hdr, 0, sizeof(struct fw_header));
470
471 hdr->version = htonl(hdr_ver);
472 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
473 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
474 hdr->hw_id = htonl(hw_id);
475 hdr->hw_rev = htonl(hw_rev);
476
477 hdr->kernel_la = htonl(kernel_la);
478 hdr->kernel_ep = htonl(kernel_ep);
479 hdr->kernel_ofs = htonl(sizeof(struct fw_header));
480 hdr->kernel_len = htonl(kernel_len);
481
482 if (!combined) {
483 if (boot_info.file_size == 0)
484 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
485 else
486 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
487
488 hdr->fw_length = htonl(layout->fw_max_len);
489 hdr->rootfs_ofs = htonl(rootfs_ofs);
490 hdr->rootfs_len = htonl(rootfs_info.file_size);
491 }
492
493 hdr->ver_hi = htons(fw_ver_hi);
494 hdr->ver_mid = htons(fw_ver_mid);
495 hdr->ver_lo = htons(fw_ver_lo);
496
497 if (region) {
498 hdr->region_code = htonl(region->code);
499 snprintf(
500 hdr->region_str1, sizeof(hdr->region_str1), "00000000;%02X%02X%02X%02X;",
501 region->name[0], region->name[1], region->name[2], region->name[3]
502 );
503 snprintf(
504 hdr->region_str2, sizeof(hdr->region_str2), "%02X%02X%02X%02X",
505 region->name[0], region->name[1], region->name[2], region->name[3]
506 );
507 }
508
509 if (endian_swap) {
510 hdr->kernel_la = bswap_32(hdr->kernel_la);
511 hdr->kernel_ep = bswap_32(hdr->kernel_ep);
512 }
513
514 if (!combined)
515 get_md5(buf, len, hdr->md5sum1);
516 }
517
518 static int pad_jffs2(char *buf, int currlen)
519 {
520 int len;
521 uint32_t pad_mask;
522
523 len = currlen;
524 pad_mask = (64 * 1024);
525 while ((len < layout->fw_max_len) && (pad_mask != 0)) {
526 uint32_t mask;
527 int i;
528
529 for (i = 10; i < 32; i++) {
530 mask = 1 << i;
531 if (pad_mask & mask)
532 break;
533 }
534
535 len = ALIGN(len, mask);
536
537 for (i = 10; i < 32; i++) {
538 mask = 1 << i;
539 if ((len & (mask - 1)) == 0)
540 pad_mask &= ~mask;
541 }
542
543 for (i = 0; i < sizeof(jffs2_eof_mark); i++)
544 buf[len + i] = jffs2_eof_mark[i];
545
546 len += sizeof(jffs2_eof_mark);
547 }
548
549 return len;
550 }
551
552 static int write_fw(const char *data, int len)
553 {
554 FILE *f;
555 int ret = EXIT_FAILURE;
556
557 f = fopen(ofname, "w");
558 if (f == NULL) {
559 ERRS("could not open \"%s\" for writing", ofname);
560 goto out;
561 }
562
563 errno = 0;
564 fwrite(data, len, 1, f);
565 if (errno) {
566 ERRS("unable to write output file");
567 goto out_flush;
568 }
569
570 DBG("firmware file \"%s\" completed", ofname);
571
572 ret = EXIT_SUCCESS;
573
574 out_flush:
575 fflush(f);
576 fclose(f);
577 if (ret != EXIT_SUCCESS) {
578 unlink(ofname);
579 }
580 out:
581 return ret;
582 }
583
584 static int build_fw(void)
585 {
586 int buflen;
587 char *buf;
588 char *p;
589 int ret = EXIT_FAILURE;
590 int writelen = 0;
591
592 writelen = sizeof(struct fw_header) + kernel_len;
593
594 if (combined)
595 buflen = writelen;
596 else
597 buflen = layout->fw_max_len;
598
599 buf = malloc(buflen);
600 if (!buf) {
601 ERR("no memory for buffer\n");
602 goto out;
603 }
604
605 memset(buf, 0xff, buflen);
606 p = buf + sizeof(struct fw_header);
607 ret = read_to_buf(&kernel_info, p);
608 if (ret)
609 goto out_free_buf;
610
611
612 if (!combined) {
613 if (rootfs_align)
614 p = buf + writelen;
615 else
616 p = buf + rootfs_ofs;
617
618 ret = read_to_buf(&rootfs_info, p);
619 if (ret)
620 goto out_free_buf;
621
622 if (rootfs_align)
623 writelen += rootfs_info.file_size;
624 else
625 writelen = rootfs_ofs + rootfs_info.file_size;
626
627 if (add_jffs2_eof)
628 writelen = pad_jffs2(buf, writelen);
629 }
630
631 if (!strip_padding)
632 writelen = buflen;
633
634 fill_header(buf, writelen);
635 ret = write_fw(buf, writelen);
636 if (ret)
637 goto out_free_buf;
638
639 ret = EXIT_SUCCESS;
640
641 out_free_buf:
642 free(buf);
643 out:
644 return ret;
645 }
646
647 /* Helper functions to inspect_fw() representing different output formats */
648 static inline void inspect_fw_pstr(const char *label, const char *str)
649 {
650 printf("%-23s: %s\n", label, str);
651 }
652
653 static inline void inspect_fw_phex(const char *label, uint32_t val)
654 {
655 printf("%-23s: 0x%08x\n", label, val);
656 }
657
658 static inline void inspect_fw_phexdec(const char *label, uint32_t val)
659 {
660 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
661 }
662
663 static inline void inspect_fw_pmd5sum(const char *label, const uint8_t *val, const char *text)
664 {
665 int i;
666
667 printf("%-23s:", label);
668 for (i=0; i<MD5SUM_LEN; i++)
669 printf(" %02x", val[i]);
670 printf(" %s\n", text);
671 }
672
673 static int inspect_fw(void)
674 {
675 char *buf;
676 struct fw_header *hdr;
677 uint8_t md5sum[MD5SUM_LEN];
678 int ret = EXIT_FAILURE;
679
680 buf = malloc(inspect_info.file_size);
681 if (!buf) {
682 ERR("no memory for buffer!\n");
683 goto out;
684 }
685
686 ret = read_to_buf(&inspect_info, buf);
687 if (ret)
688 goto out_free_buf;
689 hdr = (struct fw_header *)buf;
690
691 inspect_fw_pstr("File name", inspect_info.file_name);
692 inspect_fw_phexdec("File size", inspect_info.file_size);
693
694 if ((ntohl(hdr->version) != HEADER_VERSION_V1) &&
695 (ntohl(hdr->version) != HEADER_VERSION_V2)) {
696 ERR("file does not seem to have V1/V2 header!\n");
697 goto out_free_buf;
698 }
699
700 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
701
702 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
703 if (ntohl(hdr->boot_len) == 0)
704 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
705 else
706 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
707 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
708
709 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
710 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
711 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
712 } else {
713 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
714 }
715 if (ntohl(hdr->unk2) != 0)
716 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
717 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
718 "(purpose yet unknown, unchecked here)");
719 if (ntohl(hdr->unk3) != 0)
720 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
721
722 printf("\n");
723
724 inspect_fw_pstr("Vendor name", hdr->vendor_name);
725 inspect_fw_pstr("Firmware version", hdr->fw_version);
726 inspect_fw_phex("Hardware ID", ntohl(hdr->hw_id));
727 inspect_fw_phex("Hardware Revision", ntohl(hdr->hw_rev));
728 inspect_fw_phex("Region code", ntohl(hdr->region_code));
729
730 printf("\n");
731
732 inspect_fw_phexdec("Kernel data offset",
733 ntohl(hdr->kernel_ofs));
734 inspect_fw_phexdec("Kernel data length",
735 ntohl(hdr->kernel_len));
736 inspect_fw_phex("Kernel load address",
737 ntohl(hdr->kernel_la));
738 inspect_fw_phex("Kernel entry point",
739 ntohl(hdr->kernel_ep));
740 inspect_fw_phexdec("Rootfs data offset",
741 ntohl(hdr->rootfs_ofs));
742 inspect_fw_phexdec("Rootfs data length",
743 ntohl(hdr->rootfs_len));
744 inspect_fw_phexdec("Boot loader data offset",
745 ntohl(hdr->boot_ofs));
746 inspect_fw_phexdec("Boot loader data length",
747 ntohl(hdr->boot_len));
748 inspect_fw_phexdec("Total firmware length",
749 ntohl(hdr->fw_length));
750
751 if (extract) {
752 FILE *fp;
753 char *filename;
754
755 printf("\n");
756
757 filename = malloc(strlen(inspect_info.file_name) + 8);
758 sprintf(filename, "%s-kernel", inspect_info.file_name);
759 printf("Extracting kernel to \"%s\"...\n", filename);
760 fp = fopen(filename, "w");
761 if (fp) {
762 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
763 ntohl(hdr->kernel_len), 1, fp)) {
764 ERR("error in fwrite(): %s", strerror(errno));
765 }
766 fclose(fp);
767 } else {
768 ERR("error in fopen(): %s", strerror(errno));
769 }
770 free(filename);
771
772 filename = malloc(strlen(inspect_info.file_name) + 8);
773 sprintf(filename, "%s-rootfs", inspect_info.file_name);
774 printf("Extracting rootfs to \"%s\"...\n", filename);
775 fp = fopen(filename, "w");
776 if (fp) {
777 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
778 ntohl(hdr->rootfs_len), 1, fp)) {
779 ERR("error in fwrite(): %s", strerror(errno));
780 }
781 fclose(fp);
782 } else {
783 ERR("error in fopen(): %s", strerror(errno));
784 }
785 free(filename);
786 }
787
788 out_free_buf:
789 free(buf);
790 out:
791 return ret;
792 }
793
794 int main(int argc, char *argv[])
795 {
796 int ret = EXIT_FAILURE;
797
798 progname = basename(argv[0]);
799
800 while ( 1 ) {
801 int c;
802
803 c = getopt(argc, argv, "a:H:E:F:L:m:V:N:W:C:ci:k:r:R:o:xX:ehsjv:");
804 if (c == -1)
805 break;
806
807 switch (c) {
808 case 'a':
809 sscanf(optarg, "0x%x", &rootfs_align);
810 break;
811 case 'H':
812 opt_hw_id = optarg;
813 break;
814 case 'E':
815 sscanf(optarg, "0x%x", &kernel_ep);
816 break;
817 case 'F':
818 layout_id = optarg;
819 break;
820 case 'W':
821 opt_hw_rev = optarg;
822 break;
823 case 'C':
824 country = optarg;
825 break;
826 case 'L':
827 sscanf(optarg, "0x%x", &kernel_la);
828 break;
829 case 'm':
830 sscanf(optarg, "%u", &opt_hdr_ver);
831 break;
832 case 'V':
833 version = optarg;
834 break;
835 case 'v':
836 fw_ver = optarg;
837 break;
838 case 'N':
839 vendor = optarg;
840 break;
841 case 'c':
842 combined++;
843 break;
844 case 'k':
845 kernel_info.file_name = optarg;
846 break;
847 case 'r':
848 rootfs_info.file_name = optarg;
849 break;
850 case 'R':
851 sscanf(optarg, "0x%x", &rootfs_ofs);
852 break;
853 case 'o':
854 ofname = optarg;
855 break;
856 case 's':
857 strip_padding = 1;
858 break;
859 case 'i':
860 inspect_info.file_name = optarg;
861 break;
862 case 'j':
863 add_jffs2_eof = 1;
864 break;
865 case 'x':
866 extract = 1;
867 break;
868 case 'e':
869 endian_swap = true;
870 break;
871 case 'h':
872 usage(EXIT_SUCCESS);
873 break;
874 case 'X':
875 sscanf(optarg, "0x%x", &reserved_space);
876 break;
877 default:
878 usage(EXIT_FAILURE);
879 break;
880 }
881 }
882
883 ret = check_options();
884 if (ret)
885 goto out;
886
887 if (!inspect_info.file_name)
888 ret = build_fw();
889 else
890 ret = inspect_fw();
891
892 out:
893 return ret;
894 }