firmware-utils/mktplinkfw: allow to align the rootfs on a specified boundary
[openwrt/svn-archive/archive.git] / tools / firmware-utils / src / mktplinkfw.c
1 /*
2 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
3 *
4 * This tool was based on:
5 * TP-Link WR941 V2 firmware checksum fixing tool.
6 * Copyright (C) 2008,2009 Wang Jian <lark@linux.net.cn>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
11 *
12 */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stdint.h>
17 #include <string.h>
18 #include <unistd.h> /* for unlink() */
19 #include <libgen.h>
20 #include <getopt.h> /* for getopt() */
21 #include <stdarg.h>
22 #include <errno.h>
23 #include <sys/stat.h>
24
25 #include <arpa/inet.h>
26 #include <netinet/in.h>
27
28 #include "md5.h"
29
30 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
31
32 #define HEADER_VERSION_V1 0x01000000
33 #define HWID_TL_MR3220_V1 0x32200001
34 #define HWID_TL_MR3420_V1 0x34200001
35 #define HWID_TL_WA901ND_V1 0x09010001
36 #define HWID_TL_WA901ND_V2 0x09010002
37 #define HWID_TL_WR703N_V1 0x07030101
38 #define HWID_TL_WR741ND_V1 0x07410001
39 #define HWID_TL_WR741ND_V4 0x07410004
40 #define HWID_TL_WR740N_V1 0x07400001
41 #define HWID_TL_WR740N_V3 0x07400003
42 #define HWID_TL_WR743ND_V1 0x07430001
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 uint32_t kernel_len = 0;
105 static struct file_info rootfs_info;
106 static uint32_t rootfs_ofs = 0;
107 static uint32_t rootfs_align;
108 static struct file_info boot_info;
109 static int combined;
110 static int strip_padding;
111
112 static struct file_info inspect_info;
113 static int extract = 0;
114
115 char md5salt_normal[MD5SUM_LEN] = {
116 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
117 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
118 };
119
120 char md5salt_boot[MD5SUM_LEN] = {
121 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
122 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
123 };
124
125 static struct board_info boards[] = {
126 {
127 .id = "TL-MR3220v1",
128 .hw_id = HWID_TL_MR3220_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-MR3420v1",
136 .hw_id = HWID_TL_MR3420_V1,
137 .hw_rev = 1,
138 .fw_max_len = 0x3c0000,
139 .kernel_la = 0x80060000,
140 .kernel_ep = 0x80060000,
141 .rootfs_ofs = 0x140000,
142 }, {
143 .id = "TL-WA901NDv1",
144 .hw_id = HWID_TL_WA901ND_V1,
145 .hw_rev = 1,
146 .fw_max_len = 0x3c0000,
147 .kernel_la = 0x80060000,
148 .kernel_ep = 0x80060000,
149 .rootfs_ofs = 0x140000,
150 }, {
151 .id = "TL-WA901NDv2",
152 .hw_id = HWID_TL_WA901ND_V2,
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-WR741NDv1",
160 .hw_id = HWID_TL_WR741ND_V1,
161 .hw_rev = 1,
162 .fw_max_len = 0x3c0000,
163 .kernel_la = 0x80060000,
164 .kernel_ep = 0x80060000,
165 .rootfs_ofs = 0x140000,
166 }, {
167 .id = "TL-WR741NDv4",
168 .hw_id = HWID_TL_WR741ND_V4,
169 .hw_rev = 1,
170 .fw_max_len = 0x3c0000,
171 .kernel_la = 0x80060000,
172 .kernel_ep = 0x80060000,
173 .rootfs_ofs = 0x100000,
174 }, {
175 .id = "TL-WR740Nv1",
176 .hw_id = HWID_TL_WR740N_V1,
177 .hw_rev = 1,
178 .fw_max_len = 0x3c0000,
179 .kernel_la = 0x80060000,
180 .kernel_ep = 0x80060000,
181 .rootfs_ofs = 0x140000,
182 }, {
183 .id = "TL-WR740Nv3",
184 .hw_id = HWID_TL_WR740N_V3,
185 .hw_rev = 1,
186 .fw_max_len = 0x3c0000,
187 .kernel_la = 0x80060000,
188 .kernel_ep = 0x80060000,
189 .rootfs_ofs = 0x140000,
190 }, {
191 .id = "TL-WR743NDv1",
192 .hw_id = HWID_TL_WR743ND_V1,
193 .hw_rev = 1,
194 .fw_max_len = 0x3c0000,
195 .kernel_la = 0x80060000,
196 .kernel_ep = 0x80060000,
197 .rootfs_ofs = 0x140000,
198 }, {
199 .id = "TL-WR841Nv1.5",
200 .hw_id = HWID_TL_WR841N_V1_5,
201 .hw_rev = 2,
202 .fw_max_len = 0x3c0000,
203 .kernel_la = 0x80060000,
204 .kernel_ep = 0x80060000,
205 .rootfs_ofs = 0x140000,
206 }, {
207 .id = "TL-WR841NDv3",
208 .hw_id = HWID_TL_WR841ND_V3,
209 .hw_rev = 3,
210 .fw_max_len = 0x3c0000,
211 .kernel_la = 0x80060000,
212 .kernel_ep = 0x80060000,
213 .rootfs_ofs = 0x140000,
214 }, {
215 .id = "TL-WR841NDv5",
216 .hw_id = HWID_TL_WR841ND_V5,
217 .hw_rev = 1,
218 .fw_max_len = 0x3c0000,
219 .kernel_la = 0x80060000,
220 .kernel_ep = 0x80060000,
221 .rootfs_ofs = 0x140000,
222 }, {
223 .id = "TL-WR841NDv7",
224 .hw_id = HWID_TL_WR841ND_V7,
225 .hw_rev = 1,
226 .fw_max_len = 0x3c0000,
227 .kernel_la = 0x80060000,
228 .kernel_ep = 0x80060000,
229 .rootfs_ofs = 0x140000,
230 }, {
231 .id = "TL-WR941NDv2",
232 .hw_id = HWID_TL_WR941ND_V2,
233 .hw_rev = 2,
234 .fw_max_len = 0x3c0000,
235 .kernel_la = 0x80060000,
236 .kernel_ep = 0x80060000,
237 .rootfs_ofs = 0x140000,
238 }, {
239 .id = "TL-WR941NDv4",
240 .hw_id = HWID_TL_WR941ND_V4,
241 .hw_rev = 1,
242 .fw_max_len = 0x3c0000,
243 .kernel_la = 0x80060000,
244 .kernel_ep = 0x80060000,
245 .rootfs_ofs = 0x140000,
246 }, {
247 .id = "TL-WR1043NDv1",
248 .hw_id = HWID_TL_WR1043ND_V1,
249 .hw_rev = 1,
250 .fw_max_len = 0x7c0000,
251 .kernel_la = 0x80060000,
252 .kernel_ep = 0x80060000,
253 .rootfs_ofs = 0x140000,
254 }, {
255 .id = "TL-WR703Nv1",
256 .hw_id = HWID_TL_WR703N_V1,
257 .hw_rev = 1,
258 .fw_max_len = 0x3c0000,
259 .kernel_la = 0x80060000,
260 .kernel_ep = 0x80060000,
261 .rootfs_ofs = 0x100000,
262 }, {
263 /* terminating entry */
264 }
265 };
266
267 /*
268 * Message macros
269 */
270 #define ERR(fmt, ...) do { \
271 fflush(0); \
272 fprintf(stderr, "[%s] *** error: " fmt "\n", \
273 progname, ## __VA_ARGS__ ); \
274 } while (0)
275
276 #define ERRS(fmt, ...) do { \
277 int save = errno; \
278 fflush(0); \
279 fprintf(stderr, "[%s] *** error: " fmt "\n", \
280 progname, ## __VA_ARGS__, strerror(save)); \
281 } while (0)
282
283 #define DBG(fmt, ...) do { \
284 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
285 } while (0)
286
287 static struct board_info *find_board(char *id)
288 {
289 struct board_info *ret;
290 struct board_info *board;
291
292 ret = NULL;
293 for (board = boards; board->id != NULL; board++){
294 if (strcasecmp(id, board->id) == 0) {
295 ret = board;
296 break;
297 }
298 };
299
300 return ret;
301 }
302
303 static struct board_info *find_board_by_hwid(uint32_t hw_id)
304 {
305 struct board_info *board;
306
307 for (board = boards; board->id != NULL; board++) {
308 if (hw_id == board->hw_id)
309 return board;
310 };
311
312 return NULL;
313 }
314
315
316 static void usage(int status)
317 {
318 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
319 struct board_info *board;
320
321 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
322 fprintf(stream,
323 "\n"
324 "Options:\n"
325 " -B <board> create image for the board specified with <board>\n"
326 " -c use combined kernel image\n"
327 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
328 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
329 " -k <file> read kernel image from the file <file>\n"
330 " -r <file> read rootfs image from the file <file>\n"
331 " -a <align> align the rootfs start on an <align> bytes boundary\n"
332 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
333 " -o <file> write output to the file <file>\n"
334 " -s strip padding from the end of the image\n"
335 " -N <vendor> set image vendor to <vendor>\n"
336 " -V <version> set image version to <version>\n"
337 " -i <file> inspect given firmware file <file>\n"
338 " -x extract kernel and rootfs while inspecting (requires -i)\n"
339 " -h show this screen\n"
340 );
341
342 exit(status);
343 }
344
345 static int get_md5(char *data, int size, char *md5)
346 {
347 MD5_CTX ctx;
348
349 MD5_Init(&ctx);
350 MD5_Update(&ctx, data, size);
351 MD5_Final(md5, &ctx);
352 }
353
354 static int get_file_stat(struct file_info *fdata)
355 {
356 struct stat st;
357 int res;
358
359 if (fdata->file_name == NULL)
360 return 0;
361
362 res = stat(fdata->file_name, &st);
363 if (res){
364 ERRS("stat failed on %s", fdata->file_name);
365 return res;
366 }
367
368 fdata->file_size = st.st_size;
369 return 0;
370 }
371
372 static int read_to_buf(struct file_info *fdata, char *buf)
373 {
374 FILE *f;
375 int ret = EXIT_FAILURE;
376
377 f = fopen(fdata->file_name, "r");
378 if (f == NULL) {
379 ERRS("could not open \"%s\" for reading", fdata->file_name);
380 goto out;
381 }
382
383 errno = 0;
384 fread(buf, fdata->file_size, 1, f);
385 if (errno != 0) {
386 ERRS("unable to read from file \"%s\"", fdata->file_name);
387 goto out_close;
388 }
389
390 ret = EXIT_SUCCESS;
391
392 out_close:
393 fclose(f);
394 out:
395 return ret;
396 }
397
398 static int check_options(void)
399 {
400 int ret;
401
402 if (inspect_info.file_name) {
403 ret = get_file_stat(&inspect_info);
404 if (ret)
405 return ret;
406
407 return 0;
408 } else if (extract) {
409 ERR("no firmware for inspection specified");
410 return -1;
411 }
412
413 if (board_id == NULL) {
414 ERR("no board specified");
415 return -1;
416 }
417
418 board = find_board(board_id);
419 if (board == NULL) {
420 ERR("unknown/unsupported board id \"%s\"", board_id);
421 return -1;
422 }
423 if (!kernel_la)
424 kernel_la = board->kernel_la;
425 if (!kernel_ep)
426 kernel_ep = board->kernel_ep;
427 if (!rootfs_ofs)
428 rootfs_ofs = board->rootfs_ofs;
429
430 if (kernel_info.file_name == NULL) {
431 ERR("no kernel image specified");
432 return -1;
433 }
434
435 ret = get_file_stat(&kernel_info);
436 if (ret)
437 return ret;
438
439 kernel_len = kernel_info.file_size;
440
441 if (combined) {
442 if (kernel_info.file_size >
443 board->fw_max_len - sizeof(struct fw_header)) {
444 ERR("kernel image is too big");
445 return -1;
446 }
447 } else {
448 if (rootfs_info.file_name == NULL) {
449 ERR("no rootfs image specified");
450 return -1;
451 }
452
453 ret = get_file_stat(&rootfs_info);
454 if (ret)
455 return ret;
456
457 if (rootfs_align) {
458 kernel_len += sizeof(struct fw_header);
459 kernel_len = ALIGN(kernel_len, rootfs_align);
460 kernel_len -= sizeof(struct fw_header);
461
462 DBG("kernel length aligned to %u", kernel_len);
463
464 if (kernel_len + rootfs_info.file_size >
465 board->fw_max_len - sizeof(struct fw_header)) {
466 ERR("images are too big");
467 return -1;
468 }
469 } else {
470 if (kernel_info.file_size >
471 rootfs_ofs - sizeof(struct fw_header)) {
472 ERR("kernel image is too big");
473 return -1;
474 }
475
476 if (rootfs_info.file_size >
477 (board->fw_max_len - rootfs_ofs)) {
478 ERR("rootfs image is too big");
479 return -1;
480 }
481 }
482 }
483
484 if (ofname == NULL) {
485 ERR("no output file specified");
486 return -1;
487 }
488
489 return 0;
490 }
491
492 static void fill_header(char *buf, int len)
493 {
494 struct fw_header *hdr = (struct fw_header *)buf;
495
496 memset(hdr, 0, sizeof(struct fw_header));
497
498 hdr->version = htonl(HEADER_VERSION_V1);
499 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
500 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
501 hdr->hw_id = htonl(board->hw_id);
502 hdr->hw_rev = htonl(board->hw_rev);
503
504 if (boot_info.file_size == 0)
505 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
506 else
507 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
508
509 hdr->kernel_la = htonl(kernel_la);
510 hdr->kernel_ep = htonl(kernel_ep);
511 hdr->fw_length = htonl(board->fw_max_len);
512 hdr->kernel_ofs = htonl(sizeof(struct fw_header));
513 hdr->kernel_len = htonl(kernel_len);
514 if (!combined) {
515 hdr->rootfs_ofs = htonl(rootfs_ofs);
516 hdr->rootfs_len = htonl(rootfs_info.file_size);
517 }
518
519 get_md5(buf, len, hdr->md5sum1);
520 }
521
522 static int write_fw(char *data, int len)
523 {
524 FILE *f;
525 int ret = EXIT_FAILURE;
526
527 f = fopen(ofname, "w");
528 if (f == NULL) {
529 ERRS("could not open \"%s\" for writing", ofname);
530 goto out;
531 }
532
533 errno = 0;
534 fwrite(data, len, 1, f);
535 if (errno) {
536 ERRS("unable to write output file");
537 goto out_flush;
538 }
539
540 DBG("firmware file \"%s\" completed", ofname);
541
542 ret = EXIT_SUCCESS;
543
544 out_flush:
545 fflush(f);
546 fclose(f);
547 if (ret != EXIT_SUCCESS) {
548 unlink(ofname);
549 }
550 out:
551 return ret;
552 }
553
554 static int build_fw(void)
555 {
556 int buflen;
557 char *buf;
558 char *p;
559 int ret = EXIT_FAILURE;
560 int writelen = 0;
561
562 buflen = board->fw_max_len;
563
564 buf = malloc(buflen);
565 if (!buf) {
566 ERR("no memory for buffer\n");
567 goto out;
568 }
569
570 memset(buf, 0xff, buflen);
571 p = buf + sizeof(struct fw_header);
572 ret = read_to_buf(&kernel_info, p);
573 if (ret)
574 goto out_free_buf;
575
576 writelen = sizeof(struct fw_header) + kernel_len;
577
578 if (!combined) {
579 if (rootfs_align)
580 p = buf + writelen;
581 else
582 p = buf + rootfs_ofs;
583 ret = read_to_buf(&rootfs_info, p);
584 if (ret)
585 goto out_free_buf;
586
587 if (rootfs_align)
588 writelen += rootfs_info.file_size;
589 else
590 writelen = rootfs_ofs + rootfs_info.file_size;
591 }
592
593 if (!strip_padding)
594 writelen = buflen;
595
596 fill_header(buf, writelen);
597 ret = write_fw(buf, writelen);
598 if (ret)
599 goto out_free_buf;
600
601 ret = EXIT_SUCCESS;
602
603 out_free_buf:
604 free(buf);
605 out:
606 return ret;
607 }
608
609 /* Helper functions to inspect_fw() representing different output formats */
610 static inline void inspect_fw_pstr(char *label, char *str)
611 {
612 printf("%-23s: %s\n", label, str);
613 }
614
615 static inline void inspect_fw_phex(char *label, uint32_t val)
616 {
617 printf("%-23s: 0x%08x\n", label, val);
618 }
619
620 static inline void inspect_fw_phexpost(char *label,
621 uint32_t val, char *post)
622 {
623 printf("%-23s: 0x%08x (%s)\n", label, val, post);
624 }
625
626 static inline void inspect_fw_phexdef(char *label,
627 uint32_t val, uint32_t defval)
628 {
629 printf("%-23s: 0x%08x ", label, val);
630
631 if (val == defval)
632 printf("(== OpenWrt default)\n");
633 else
634 printf("(OpenWrt default: 0x%08x)\n", defval);
635 }
636
637 static inline void inspect_fw_phexexp(char *label,
638 uint32_t val, uint32_t expval)
639 {
640 printf("%-23s: 0x%08x ", label, val);
641
642 if (val == expval)
643 printf("(ok)\n");
644 else
645 printf("(expected: 0x%08x)\n", expval);
646 }
647
648 static inline void inspect_fw_phexdec(char *label, uint32_t val)
649 {
650 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
651 }
652
653 static inline void inspect_fw_phexdecdef(char *label,
654 uint32_t val, uint32_t defval)
655 {
656 printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
657
658 if (val == defval)
659 printf("(== OpenWrt default)\n");
660 else
661 printf("(OpenWrt default: 0x%08x)\n", defval);
662 }
663
664 static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
665 {
666 int i;
667
668 printf("%-23s:", label);
669 for (i=0; i<MD5SUM_LEN; i++)
670 printf(" %02x", val[i]);
671 printf(" %s\n", text);
672 }
673
674 static int inspect_fw(void)
675 {
676 char *buf;
677 struct fw_header *hdr;
678 uint8_t md5sum[MD5SUM_LEN];
679 struct board_info *board;
680 int ret = EXIT_FAILURE;
681
682 buf = malloc(inspect_info.file_size);
683 if (!buf) {
684 ERR("no memory for buffer!\n");
685 goto out;
686 }
687
688 ret = read_to_buf(&inspect_info, buf);
689 if (ret)
690 goto out_free_buf;
691 hdr = (struct fw_header *)buf;
692
693 inspect_fw_pstr("File name", inspect_info.file_name);
694 inspect_fw_phexdec("File size", inspect_info.file_size);
695
696 if (ntohl(hdr->version) != HEADER_VERSION_V1) {
697 ERR("file does not seem to have V1 header!\n");
698 goto out_free_buf;
699 }
700
701 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
702
703 if (ntohl(hdr->unk1) != 0)
704 inspect_fw_phexdec("Unknown value 1", hdr->unk1);
705
706 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
707 if (ntohl(hdr->boot_len) == 0)
708 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
709 else
710 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
711 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
712
713 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
714 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
715 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
716 } else {
717 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
718 }
719 if (ntohl(hdr->unk2) != 0)
720 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
721 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
722 "(purpose yet unknown, unchecked here)");
723 if (ntohl(hdr->unk3) != 0)
724 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
725
726 printf("\n");
727
728 inspect_fw_pstr("Vendor name", hdr->vendor_name);
729 inspect_fw_pstr("Firmware version", hdr->fw_version);
730 board = find_board_by_hwid(ntohl(hdr->hw_id));
731 if (board) {
732 inspect_fw_phexpost("Hardware ID",
733 ntohl(hdr->hw_id), board->id);
734 inspect_fw_phexexp("Hardware Revision",
735 ntohl(hdr->hw_rev), board->hw_rev);
736 } else {
737 inspect_fw_phexpost("Hardware ID",
738 ntohl(hdr->hw_id), "unknown");
739 inspect_fw_phex("Hardware Revision",
740 ntohl(hdr->hw_rev));
741 }
742
743 printf("\n");
744
745 inspect_fw_phexdec("Kernel data offset",
746 ntohl(hdr->kernel_ofs));
747 inspect_fw_phexdec("Kernel data length",
748 ntohl(hdr->kernel_len));
749 if (board) {
750 inspect_fw_phexdef("Kernel load address",
751 ntohl(hdr->kernel_la),
752 board->kernel_la);
753 inspect_fw_phexdef("Kernel entry point",
754 ntohl(hdr->kernel_ep),
755 board->kernel_ep);
756 inspect_fw_phexdecdef("Rootfs data offset",
757 ntohl(hdr->rootfs_ofs),
758 board->rootfs_ofs);
759 } else {
760 inspect_fw_phex("Kernel load address",
761 ntohl(hdr->kernel_la));
762 inspect_fw_phex("Kernel entry point",
763 ntohl(hdr->kernel_ep));
764 inspect_fw_phexdec("Rootfs data offset",
765 ntohl(hdr->rootfs_ofs));
766 }
767 inspect_fw_phexdec("Rootfs data length",
768 ntohl(hdr->rootfs_len));
769 inspect_fw_phexdec("Boot loader data offset",
770 ntohl(hdr->boot_ofs));
771 inspect_fw_phexdec("Boot loader data length",
772 ntohl(hdr->boot_len));
773 inspect_fw_phexdec("Total firmware length",
774 ntohl(hdr->fw_length));
775
776 if (extract) {
777 FILE *fp;
778 char *filename;
779
780 printf("\n");
781
782 filename = malloc(strlen(inspect_info.file_name) + 8);
783 sprintf(filename, "%s-kernel", inspect_info.file_name);
784 printf("Extracting kernel to \"%s\"...\n", filename);
785 fp = fopen(filename, "w");
786 if (fp) {
787 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
788 ntohl(hdr->kernel_len), 1, fp)) {
789 ERR("error in fwrite(): %s", strerror(errno));
790 }
791 fclose(fp);
792 } else {
793 ERR("error in fopen(): %s", strerror(errno));
794 }
795 free(filename);
796
797 filename = malloc(strlen(inspect_info.file_name) + 8);
798 sprintf(filename, "%s-rootfs", inspect_info.file_name);
799 printf("Extracting rootfs to \"%s\"...\n", filename);
800 fp = fopen(filename, "w");
801 if (fp) {
802 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
803 ntohl(hdr->rootfs_len), 1, fp)) {
804 ERR("error in fwrite(): %s", strerror(errno));
805 }
806 fclose(fp);
807 } else {
808 ERR("error in fopen(): %s", strerror(errno));
809 }
810 free(filename);
811 }
812
813 out_free_buf:
814 free(buf);
815 out:
816 return ret;
817 }
818
819 int main(int argc, char *argv[])
820 {
821 int ret = EXIT_FAILURE;
822 int err;
823
824 FILE *outfile;
825
826 progname = basename(argv[0]);
827
828 while ( 1 ) {
829 int c;
830
831 c = getopt(argc, argv, "a:B:E:L:V:N:ci:k:r:R:o:xhs");
832 if (c == -1)
833 break;
834
835 switch (c) {
836 case 'a':
837 sscanf(optarg, "0x%x", &rootfs_align);
838 break;
839 case 'B':
840 board_id = optarg;
841 break;
842 case 'E':
843 sscanf(optarg, "0x%x", &kernel_ep);
844 break;
845 case 'L':
846 sscanf(optarg, "0x%x", &kernel_la);
847 break;
848 case 'V':
849 version = optarg;
850 break;
851 case 'N':
852 vendor = optarg;
853 break;
854 case 'c':
855 combined++;
856 break;
857 case 'k':
858 kernel_info.file_name = optarg;
859 break;
860 case 'r':
861 rootfs_info.file_name = optarg;
862 break;
863 case 'R':
864 sscanf(optarg, "0x%x", &rootfs_ofs);
865 break;
866 case 'o':
867 ofname = optarg;
868 break;
869 case 's':
870 strip_padding = 1;
871 break;
872 case 'i':
873 inspect_info.file_name = optarg;
874 break;
875 case 'x':
876 extract = 1;
877 break;
878 case 'h':
879 usage(EXIT_SUCCESS);
880 break;
881 default:
882 usage(EXIT_FAILURE);
883 break;
884 }
885 }
886
887 ret = check_options();
888 if (ret)
889 goto out;
890
891 if (!inspect_info.file_name)
892 ret = build_fw();
893 else
894 ret = inspect_fw();
895
896 out:
897 return ret;
898 }
899