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