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