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