aea7e3595a3bd12445b2120676dc240d9f56374e
[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 "md5.h"
26
27 #if (__BYTE_ORDER == __BIG_ENDIAN)
28 #  define HOST_TO_BE32(x)       (x)
29 #  define BE32_TO_HOST(x)       (x)
30 #else
31 #  define HOST_TO_BE32(x)       bswap_32(x)
32 #  define BE32_TO_HOST(x)       bswap_32(x)
33 #endif
34
35 #define HEADER_VERSION_V1       0x01000000
36 #define HWID_TL_MR3220_V1       0x32200001
37 #define HWID_TL_MR3420_V1       0x34200001
38 #define HWID_TL_WA901ND_V1      0x09010001
39 #define HWID_TL_WR741ND_V1      0x07410001
40 #define HWID_TL_WR841N_V1_5     0x08410002
41 #define HWID_TL_WR841ND_V3      0x08410003
42 #define HWID_TL_WR841ND_V5      0x08410005
43 #define HWID_TL_WR841ND_V7      0x08410007
44 #define HWID_TL_WR941ND_V2      0x09410002
45 #define HWID_TL_WR941ND_V4      0x09410004
46 #define HWID_TL_WR1043ND_V1     0x10430001
47
48 #define MD5SUM_LEN      16
49
50 struct file_info {
51         char            *file_name;     /* name of the file */
52         uint32_t        file_size;      /* length of the file */
53 };
54
55 struct fw_header {
56         uint32_t        version;        /* header version */
57         char            vendor_name[24];
58         char            fw_version[36];
59         uint32_t        hw_id;          /* hardware id */
60         uint32_t        hw_rev;         /* hardware revision */
61         uint32_t        unk1;
62         uint8_t         md5sum1[MD5SUM_LEN];
63         uint32_t        unk2;
64         uint8_t         md5sum2[MD5SUM_LEN];
65         uint32_t        unk3;
66         uint32_t        kernel_la;      /* kernel load address */
67         uint32_t        kernel_ep;      /* kernel entry point */
68         uint32_t        fw_length;      /* total length of the firmware */
69         uint32_t        kernel_ofs;     /* kernel data offset */
70         uint32_t        kernel_len;     /* kernel data length */
71         uint32_t        rootfs_ofs;     /* rootfs data offset */
72         uint32_t        rootfs_len;     /* rootfs data length */
73         uint32_t        boot_ofs;       /* bootloader data offset */
74         uint32_t        boot_len;       /* bootloader data length */
75         uint8_t         pad[360];
76 } __attribute__ ((packed));
77
78 struct board_info {
79         char            *id;
80         uint32_t        hw_id;
81         uint32_t        hw_rev;
82         uint32_t        fw_max_len;
83         uint32_t        kernel_la;
84         uint32_t        kernel_ep;
85         uint32_t        rootfs_ofs;
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
96 static char *board_id;
97 static struct board_info *board;
98 static struct file_info kernel_info;
99 static uint32_t kernel_la = 0;
100 static uint32_t kernel_ep = 0;
101 static struct file_info rootfs_info;
102 static uint32_t rootfs_ofs = 0;
103 static struct file_info boot_info;
104 static int combined;
105 static int strip_padding;
106
107 static struct file_info inspect_info;
108 static int extract = 0;
109
110 char md5salt_normal[MD5SUM_LEN] = {
111         0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
112         0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
113 };
114
115 char md5salt_boot[MD5SUM_LEN] = {
116         0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
117         0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
118 };
119
120 static struct board_info boards[] = {
121         {
122                 .id             = "TL-MR3220v1",
123                 .hw_id          = HWID_TL_MR3220_V1,
124                 .hw_rev         = 1,
125                 .fw_max_len     = 0x3c0000,
126                 .kernel_la      = 0x80060000,
127                 .kernel_ep      = 0x80060000,
128                 .rootfs_ofs     = 0x140000,
129         }, {
130                 .id             = "TL-MR3420v1",
131                 .hw_id          = HWID_TL_MR3420_V1,
132                 .hw_rev         = 1,
133                 .fw_max_len     = 0x3c0000,
134                 .kernel_la      = 0x80060000,
135                 .kernel_ep      = 0x80060000,
136                 .rootfs_ofs     = 0x140000,
137         }, {
138                 .id             = "TL-WA901NDv1",
139                 .hw_id          = HWID_TL_WA901ND_V1,
140                 .hw_rev         = 1,
141                 .fw_max_len     = 0x3c0000,
142                 .kernel_la      = 0x80060000,
143                 .kernel_ep      = 0x80060000,
144                 .rootfs_ofs     = 0x140000,
145         }, {
146                 .id             = "TL-WR741NDv1",
147                 .hw_id          = HWID_TL_WR741ND_V1,
148                 .hw_rev         = 1,
149                 .fw_max_len     = 0x3c0000,
150                 .kernel_la      = 0x80060000,
151                 .kernel_ep      = 0x80060000,
152                 .rootfs_ofs     = 0x140000,
153         }, {
154                 .id             = "TL-WR841Nv1.5",
155                 .hw_id          = HWID_TL_WR841N_V1_5,
156                 .hw_rev         = 2,
157                 .fw_max_len     = 0x3c0000,
158                 .kernel_la      = 0x80060000,
159                 .kernel_ep      = 0x80060000,
160                 .rootfs_ofs     = 0x140000,
161         }, {
162                 .id             = "TL-WR841NDv3",
163                 .hw_id          = HWID_TL_WR841ND_V3,
164                 .hw_rev         = 3,
165                 .fw_max_len     = 0x3c0000,
166                 .kernel_la      = 0x80060000,
167                 .kernel_ep      = 0x80060000,
168                 .rootfs_ofs     = 0x140000,
169         }, {
170                 .id             = "TL-WR841NDv5",
171                 .hw_id          = HWID_TL_WR841ND_V5,
172                 .hw_rev         = 1,
173                 .fw_max_len     = 0x3c0000,
174                 .kernel_la      = 0x80060000,
175                 .kernel_ep      = 0x80060000,
176                 .rootfs_ofs     = 0x140000,
177         }, {
178                 .id             = "TL-WR841NDv7",
179                 .hw_id          = HWID_TL_WR841ND_V7,
180                 .hw_rev         = 1,
181                 .fw_max_len     = 0x3c0000,
182                 .kernel_la      = 0x80060000,
183                 .kernel_ep      = 0x80060000,
184                 .rootfs_ofs     = 0x140000,
185         }, {
186                 .id             = "TL-WR941NDv2",
187                 .hw_id          = HWID_TL_WR941ND_V2,
188                 .hw_rev         = 2,
189                 .fw_max_len     = 0x3c0000,
190                 .kernel_la      = 0x80060000,
191                 .kernel_ep      = 0x80060000,
192                 .rootfs_ofs     = 0x140000,
193         }, {
194                 .id             = "TL-WR941NDv4",
195                 .hw_id          = HWID_TL_WR941ND_V4,
196                 .hw_rev         = 1,
197                 .fw_max_len     = 0x3c0000,
198                 .kernel_la      = 0x80060000,
199                 .kernel_ep      = 0x80060000,
200                 .rootfs_ofs     = 0x140000,
201         }, {
202                 .id             = "TL-WR1043NDv1",
203                 .hw_id          = HWID_TL_WR1043ND_V1,
204                 .hw_rev         = 1,
205                 .fw_max_len     = 0x7c0000,
206                 .kernel_la      = 0x80060000,
207                 .kernel_ep      = 0x80060000,
208                 .rootfs_ofs     = 0x140000,
209         }, {
210                 /* terminating entry */
211         }
212 };
213
214 /*
215  * Message macros
216  */
217 #define ERR(fmt, ...) do { \
218         fflush(0); \
219         fprintf(stderr, "[%s] *** error: " fmt "\n", \
220                         progname, ## __VA_ARGS__ ); \
221 } while (0)
222
223 #define ERRS(fmt, ...) do { \
224         int save = errno; \
225         fflush(0); \
226         fprintf(stderr, "[%s] *** error: " fmt "\n", \
227                         progname, ## __VA_ARGS__, strerror(save)); \
228 } while (0)
229
230 #define DBG(fmt, ...) do { \
231         fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
232 } while (0)
233
234 static struct board_info *find_board(char *id)
235 {
236         struct board_info *ret;
237         struct board_info *board;
238
239         ret = NULL;
240         for (board = boards; board->id != NULL; board++){
241                 if (strcasecmp(id, board->id) == 0) {
242                         ret = board;
243                         break;
244                 }
245         };
246
247         return ret;
248 }
249
250 static struct board_info *find_board_by_hwid(uint32_t hw_id)
251 {
252         struct board_info *board;
253
254         for (board = boards; board->id != NULL; board++) {
255                 if (hw_id == board->hw_id)
256                         return board;
257         };
258
259         return NULL;
260 }
261
262
263 static void usage(int status)
264 {
265         FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
266         struct board_info *board;
267
268         fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
269         fprintf(stream,
270 "\n"
271 "Options:\n"
272 "  -B <board>      create image for the board specified with <board>\n"
273 "  -c              use combined kernel image\n"
274 "  -E <ep>         overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
275 "  -L <la>         overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
276 "  -k <file>       read kernel image from the file <file>\n"
277 "  -r <file>       read rootfs image from the file <file>\n"
278 "  -R <offset>     overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
279 "  -o <file>       write output to the file <file>\n"
280 "  -s              strip padding from the end of the image\n"
281 "  -N <vendor>     set image vendor to <vendor>\n"
282 "  -V <version>    set image version to <version>\n"
283 "  -i <file>       inspect given firmware file <file>\n"
284 "  -x              extract kernel and rootfs while inspecting (requires -i)\n"
285 "  -h              show this screen\n"
286         );
287
288         exit(status);
289 }
290
291 static int get_md5(char *data, int size, char *md5)
292 {
293         MD5_CTX ctx;
294
295         MD5_Init(&ctx);
296         MD5_Update(&ctx, data, size);
297         MD5_Final(md5, &ctx);
298 }
299
300 static int get_file_stat(struct file_info *fdata)
301 {
302         struct stat st;
303         int res;
304
305         if (fdata->file_name == NULL)
306                 return 0;
307
308         res = stat(fdata->file_name, &st);
309         if (res){
310                 ERRS("stat failed on %s", fdata->file_name);
311                 return res;
312         }
313
314         fdata->file_size = st.st_size;
315         return 0;
316 }
317
318 static int read_to_buf(struct file_info *fdata, char *buf)
319 {
320         FILE *f;
321         int ret = EXIT_FAILURE;
322
323         f = fopen(fdata->file_name, "r");
324         if (f == NULL) {
325                 ERRS("could not open \"%s\" for reading", fdata->file_name);
326                 goto out;
327         }
328
329         errno = 0;
330         fread(buf, fdata->file_size, 1, f);
331         if (errno != 0) {
332                 ERRS("unable to read from file \"%s\"", fdata->file_name);
333                 goto out_close;
334         }
335
336         ret = EXIT_SUCCESS;
337
338  out_close:
339         fclose(f);
340  out:
341         return ret;
342 }
343
344 static int check_options(void)
345 {
346         int ret;
347
348         if (inspect_info.file_name) {
349                 ret = get_file_stat(&inspect_info);
350                 if (ret)
351                         return ret;
352
353                 return 0;
354         } else if (extract) {
355                 ERR("no firmware for inspection specified");
356                 return -1;
357         }
358
359         if (board_id == NULL) {
360                 ERR("no board specified");
361                 return -1;
362         }
363
364         board = find_board(board_id);
365         if (board == NULL) {
366                 ERR("unknown/unsupported board id \"%s\"", board_id);
367                 return -1;
368         }
369         if (!kernel_la)
370                 kernel_la = board->kernel_la;
371         if (!kernel_ep)
372                 kernel_ep = board->kernel_ep;
373         if (!rootfs_ofs)
374                 rootfs_ofs = board->rootfs_ofs;
375
376         if (kernel_info.file_name == NULL) {
377                 ERR("no kernel image specified");
378                 return -1;
379         }
380
381         ret = get_file_stat(&kernel_info);
382         if (ret)
383                 return ret;
384
385         if (combined) {
386                 if (kernel_info.file_size >
387                     board->fw_max_len - sizeof(struct fw_header)) {
388                         ERR("kernel image is too big");
389                         return -1;
390                 }
391         } else {
392                 if (kernel_info.file_size >
393                     rootfs_ofs - sizeof(struct fw_header)) {
394                         ERR("kernel image is too big");
395                         return -1;
396                 }
397                 if (rootfs_info.file_name == NULL) {
398                         ERR("no rootfs image specified");
399                         return -1;
400                 }
401
402                 ret = get_file_stat(&rootfs_info);
403                 if (ret)
404                         return ret;
405
406                 if (rootfs_info.file_size >
407                     (board->fw_max_len - rootfs_ofs)) {
408                         ERR("rootfs image is too big");
409                         return -1;
410                 }
411         }
412
413         if (ofname == NULL) {
414                 ERR("no output file specified");
415                 return -1;
416         }
417
418         return 0;
419 }
420
421 static void fill_header(char *buf, int len)
422 {
423         struct fw_header *hdr = (struct fw_header *)buf;
424
425         memset(hdr, 0, sizeof(struct fw_header));
426
427         hdr->version = HOST_TO_BE32(HEADER_VERSION_V1);
428         strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
429         strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
430         hdr->hw_id = HOST_TO_BE32(board->hw_id);
431         hdr->hw_rev = HOST_TO_BE32(board->hw_rev);
432
433         if (boot_info.file_size == 0)
434                 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
435         else
436                 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
437
438         hdr->kernel_la = HOST_TO_BE32(kernel_la);
439         hdr->kernel_ep = HOST_TO_BE32(kernel_ep);
440         hdr->fw_length = HOST_TO_BE32(board->fw_max_len);
441         hdr->kernel_ofs = HOST_TO_BE32(sizeof(struct fw_header));
442         hdr->kernel_len = HOST_TO_BE32(kernel_info.file_size);
443         if (!combined) {
444                 hdr->rootfs_ofs = HOST_TO_BE32(rootfs_ofs);
445                 hdr->rootfs_len = HOST_TO_BE32(rootfs_info.file_size);
446         }
447
448         get_md5(buf, len, hdr->md5sum1);
449 }
450
451 static int write_fw(char *data, int len)
452 {
453         FILE *f;
454         int ret = EXIT_FAILURE;
455
456         f = fopen(ofname, "w");
457         if (f == NULL) {
458                 ERRS("could not open \"%s\" for writing", ofname);
459                 goto out;
460         }
461
462         errno = 0;
463         fwrite(data, len, 1, f);
464         if (errno) {
465                 ERRS("unable to write output file");
466                 goto out_flush;
467         }
468
469         DBG("firmware file \"%s\" completed", ofname);
470
471         ret = EXIT_SUCCESS;
472
473  out_flush:
474         fflush(f);
475         fclose(f);
476         if (ret != EXIT_SUCCESS) {
477                 unlink(ofname);
478         }
479  out:
480         return ret;
481 }
482
483 static int build_fw(void)
484 {
485         int buflen;
486         char *buf;
487         char *p;
488         int ret = EXIT_FAILURE;
489         int writelen = 0;
490
491         buflen = board->fw_max_len;
492
493         buf = malloc(buflen);
494         if (!buf) {
495                 ERR("no memory for buffer\n");
496                 goto out;
497         }
498
499         memset(buf, 0xff, buflen);
500         p = buf + sizeof(struct fw_header);
501         ret = read_to_buf(&kernel_info, p);
502         if (ret)
503                 goto out_free_buf;
504
505         writelen = kernel_info.file_size;
506
507         if (!combined) {
508                 p = buf + rootfs_ofs;
509                 ret = read_to_buf(&rootfs_info, p);
510                 if (ret)
511                         goto out_free_buf;
512
513                 writelen = rootfs_ofs + rootfs_info.file_size;
514         }
515
516         if (!strip_padding)
517                 writelen = buflen;
518
519         fill_header(buf, writelen);
520         ret = write_fw(buf, writelen);
521         if (ret)
522                 goto out_free_buf;
523
524         ret = EXIT_SUCCESS;
525
526  out_free_buf:
527         free(buf);
528  out:
529         return ret;
530 }
531
532 /* Helper functions to inspect_fw() representing different output formats */
533 static inline void inspect_fw_pstr(char *label, char *str)
534 {
535         printf("%-23s: %s\n", label, str);
536 }
537
538 static inline void inspect_fw_phex(char *label, uint32_t val)
539 {
540         printf("%-23s: 0x%08x\n", label, val);
541 }
542
543 static inline void inspect_fw_phexpost(char *label,
544                                        uint32_t val, char *post)
545 {
546         printf("%-23s: 0x%08x (%s)\n", label, val, post);
547 }
548
549 static inline void inspect_fw_phexdef(char *label,
550                                       uint32_t val, uint32_t defval)
551 {
552         printf("%-23s: 0x%08x                  ", label, val);
553
554         if (val == defval)
555                 printf("(== OpenWrt default)\n");
556         else
557                 printf("(OpenWrt default: 0x%08x)\n", defval);
558 }
559
560 static inline void inspect_fw_phexexp(char *label,
561                                       uint32_t val, uint32_t expval)
562 {
563         printf("%-23s: 0x%08x ", label, val);
564
565         if (val == expval)
566                 printf("(ok)\n");
567         else
568                 printf("(expected: 0x%08x)\n", expval);
569 }
570
571 static inline void inspect_fw_phexdec(char *label, uint32_t val)
572 {
573         printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
574 }
575
576 static inline void inspect_fw_phexdecdef(char *label,
577                                          uint32_t val, uint32_t defval)
578 {
579         printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
580
581         if (val == defval)
582                 printf("(== OpenWrt default)\n");
583         else
584                 printf("(OpenWrt default: 0x%08x)\n", defval);
585 }
586
587 static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
588 {
589         int i;
590
591         printf("%-23s:", label);
592         for (i=0; i<MD5SUM_LEN; i++)
593                 printf(" %02x", val[i]);
594         printf(" %s\n", text);
595 }
596
597 static int inspect_fw(void)
598 {
599         char *buf;
600         struct fw_header *hdr;
601         uint8_t md5sum[MD5SUM_LEN];
602         struct board_info *board;
603         int ret = EXIT_FAILURE;
604
605         buf = malloc(inspect_info.file_size);
606         if (!buf) {
607                 ERR("no memory for buffer!\n");
608                 goto out;
609         }
610
611         ret = read_to_buf(&inspect_info, buf);
612         if (ret)
613                 goto out_free_buf;
614         hdr = (struct fw_header *)buf;
615
616         inspect_fw_pstr("File name", inspect_info.file_name);
617         inspect_fw_phexdec("File size", inspect_info.file_size);
618
619         if (BE32_TO_HOST(hdr->version) != HEADER_VERSION_V1) {
620                 ERR("file does not seem to have V1 header!\n");
621                 goto out_free_buf;
622         }
623
624         inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
625
626         if (BE32_TO_HOST(hdr->unk1) != 0)
627                 inspect_fw_phexdec("Unknown value 1", hdr->unk1);
628
629         memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
630         if (BE32_TO_HOST(hdr->boot_len) == 0)
631                 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
632         else
633                 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
634         get_md5(buf, inspect_info.file_size, hdr->md5sum1);
635
636         if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
637                 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
638                 inspect_fw_pmd5sum("          --> expected", hdr->md5sum1, "");
639         } else {
640                 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
641         }
642         if (BE32_TO_HOST(hdr->unk2) != 0)
643                 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
644         inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
645                            "(purpose yet unknown, unchecked here)");
646         if (BE32_TO_HOST(hdr->unk3) != 0)
647                 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
648
649         printf("\n");
650
651         inspect_fw_pstr("Vendor name", hdr->vendor_name);
652         inspect_fw_pstr("Firmware version", hdr->fw_version);
653         board = find_board_by_hwid(BE32_TO_HOST(hdr->hw_id));
654         if (board) {
655                 inspect_fw_phexpost("Hardware ID",
656                                     BE32_TO_HOST(hdr->hw_id), board->id);
657                 inspect_fw_phexexp("Hardware Revision",
658                                    BE32_TO_HOST(hdr->hw_rev), board->hw_rev);
659         } else {
660                 inspect_fw_phexpost("Hardware ID",
661                                     BE32_TO_HOST(hdr->hw_id), "unknown");
662                 inspect_fw_phex("Hardware Revision",
663                                 BE32_TO_HOST(hdr->hw_rev));
664         }
665
666         printf("\n");
667
668         inspect_fw_phexdec("Kernel data offset",
669                            BE32_TO_HOST(hdr->kernel_ofs));
670         inspect_fw_phexdec("Kernel data length",
671                            BE32_TO_HOST(hdr->kernel_len));
672         if (board) {
673                 inspect_fw_phexdef("Kernel load address",
674                                    BE32_TO_HOST(hdr->kernel_la),
675                                    board->kernel_la);
676                 inspect_fw_phexdef("Kernel entry point",
677                                    BE32_TO_HOST(hdr->kernel_ep),
678                                    board->kernel_ep);
679                 inspect_fw_phexdecdef("Rootfs data offset",
680                                       BE32_TO_HOST(hdr->rootfs_ofs),
681                                       board->rootfs_ofs);
682         } else {
683                 inspect_fw_phex("Kernel load address",
684                                 BE32_TO_HOST(hdr->kernel_la));
685                 inspect_fw_phex("Kernel entry point",
686                                 BE32_TO_HOST(hdr->kernel_ep));
687                 inspect_fw_phexdec("Rootfs data offset",
688                                    BE32_TO_HOST(hdr->rootfs_ofs));
689         }
690         inspect_fw_phexdec("Rootfs data length",
691                            BE32_TO_HOST(hdr->rootfs_len));
692         inspect_fw_phexdec("Boot loader data offset",
693                            BE32_TO_HOST(hdr->boot_ofs));
694         inspect_fw_phexdec("Boot loader data length",
695                            BE32_TO_HOST(hdr->boot_len));
696         inspect_fw_phexdec("Total firmware length",
697                            BE32_TO_HOST(hdr->fw_length));
698
699         if (extract) {
700                 FILE *fp;
701                 char *filename;
702
703                 printf("\n");
704
705                 filename = malloc(strlen(inspect_info.file_name) + 8);
706                 sprintf(filename, "%s-kernel", inspect_info.file_name);
707                 printf("Extracting kernel to \"%s\"...\n", filename);
708                 fp = fopen(filename, "w");
709                 if (fp) {
710                         if (!fwrite(buf + BE32_TO_HOST(hdr->kernel_ofs),
711                                     BE32_TO_HOST(hdr->kernel_len), 1, fp)) {
712                                 ERR("error in fwrite(): %s", strerror(errno));
713                         }
714                         fclose(fp);
715                 } else {
716                         ERR("error in fopen(): %s", strerror(errno));
717                 }
718                 free(filename);
719
720                 filename = malloc(strlen(inspect_info.file_name) + 8);
721                 sprintf(filename, "%s-rootfs", inspect_info.file_name);
722                 printf("Extracting rootfs to \"%s\"...\n", filename);
723                 fp = fopen(filename, "w");
724                 if (fp) {
725                         if (!fwrite(buf + BE32_TO_HOST(hdr->rootfs_ofs),
726                                     BE32_TO_HOST(hdr->rootfs_len), 1, fp)) {
727                                 ERR("error in fwrite(): %s", strerror(errno));
728                         }
729                         fclose(fp);
730                 } else {
731                         ERR("error in fopen(): %s", strerror(errno));
732                 }
733                 free(filename);
734         }
735
736  out_free_buf:
737         free(buf);
738  out:
739         return ret;
740 }
741
742 int main(int argc, char *argv[])
743 {
744         int ret = EXIT_FAILURE;
745         int err;
746
747         FILE *outfile;
748
749         progname = basename(argv[0]);
750
751         while ( 1 ) {
752                 int c;
753
754                 c = getopt(argc, argv, "B:E:L:V:N:ci:k:r:R:o:xhs");
755                 if (c == -1)
756                         break;
757
758                 switch (c) {
759                 case 'B':
760                         board_id = optarg;
761                         break;
762                 case 'E':
763                         sscanf(optarg, "0x%x", &kernel_ep);
764                         break;
765                 case 'L':
766                         sscanf(optarg, "0x%x", &kernel_la);
767                         break;
768                 case 'V':
769                         version = optarg;
770                         break;
771                 case 'N':
772                         vendor = optarg;
773                         break;
774                 case 'c':
775                         combined++;
776                         break;
777                 case 'k':
778                         kernel_info.file_name = optarg;
779                         break;
780                 case 'r':
781                         rootfs_info.file_name = optarg;
782                         break;
783                 case 'R':
784                         sscanf(optarg, "0x%x", &rootfs_ofs);
785                         break;
786                 case 'o':
787                         ofname = optarg;
788                         break;
789                 case 's':
790                         strip_padding = 1;
791                         break;
792                 case 'i':
793                         inspect_info.file_name = optarg;
794                         break;
795                 case 'x':
796                         extract = 1;
797                         break;
798                 case 'h':
799                         usage(EXIT_SUCCESS);
800                         break;
801                 default:
802                         usage(EXIT_FAILURE);
803                         break;
804                 }
805         }
806
807         ret = check_options();
808         if (ret)
809                 goto out;
810
811         if (!inspect_info.file_name)
812                 ret = build_fw();
813         else
814                 ret = inspect_fw();
815
816  out:
817         return ret;
818 }
819