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