e5981ef6fccdaf4047dba968cb9163b09d37b809
[project/firmware-utils.git] / src / mktplinkfw2.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
4 *
5 * This tool was based on:
6 * TP-Link WR941 V2 firmware checksum fixing tool.
7 * Copyright (C) 2008,2009 Wang Jian <lark@linux.net.cn>
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <byteswap.h>
15 #include <unistd.h> /* for unlink() */
16 #include <libgen.h>
17 #include <getopt.h> /* for getopt() */
18 #include <stdarg.h>
19 #include <errno.h>
20 #include <stdbool.h>
21 #include <endian.h>
22 #include <sys/stat.h>
23
24 #include <arpa/inet.h>
25 #include <netinet/in.h>
26
27 #include "md5.h"
28 #include "mktplinkfw-lib.h"
29
30 struct fw_header {
31 uint32_t version; /* 0x00: header version */
32 char fw_version[48]; /* 0x04: fw version string */
33 uint32_t hw_id; /* 0x34: hardware id */
34 uint32_t hw_rev; /* 0x38: FIXME: hardware revision? */
35 uint32_t hw_ver_add; /* 0x3c: additional hardware version */
36 uint8_t md5sum1[MD5SUM_LEN]; /* 0x40 */
37 uint32_t unk2; /* 0x50: 0x00000000 */
38 uint8_t md5sum2[MD5SUM_LEN]; /* 0x54 */
39 uint32_t unk3; /* 0x64: 0xffffffff */
40
41 uint32_t kernel_la; /* 0x68: kernel load address */
42 uint32_t kernel_ep; /* 0x6c: kernel entry point */
43 uint32_t fw_length; /* 0x70: total length of the image */
44 uint32_t kernel_ofs; /* 0x74: kernel data offset */
45 uint32_t kernel_len; /* 0x78: kernel data length */
46 uint32_t rootfs_ofs; /* 0x7c: rootfs data offset */
47 uint32_t rootfs_len; /* 0x80: rootfs data length */
48 uint32_t boot_ofs; /* 0x84: bootloader offset */
49 uint32_t boot_len; /* 0x88: bootloader length */
50 uint16_t unk4; /* 0x8c: 0x55aa */
51 uint8_t sver_hi; /* 0x8e */
52 uint8_t sver_lo; /* 0x8f */
53 uint8_t unk5; /* 0x90: magic: 0xa5 */
54 uint8_t ver_hi; /* 0x91 */
55 uint8_t ver_mid; /* 0x92 */
56 uint8_t ver_lo; /* 0x93 */
57 uint8_t pad[364];
58 } __attribute__ ((packed));
59
60 #define FLAG_LE_KERNEL_LA_EP 0x00000001 /* Little-endian used for kernel load address & entry point */
61
62 struct board_info {
63 char *id;
64 uint32_t hw_id;
65 uint32_t hw_rev;
66 uint32_t hw_ver_add;
67 char *layout_id;
68 uint32_t hdr_ver;
69 uint32_t flags;
70 };
71
72 /*
73 * Globals
74 */
75 char *ofname;
76 char *progname;
77 static char *vendor = "TP-LINK Technologies";
78 static char *version = "ver. 1.0";
79 static char *fw_ver = "0.0.0";
80 static char *sver = "1.0";
81 static uint32_t hdr_ver = 2;
82
83 static struct board_info custom_board;
84
85 static struct board_info *board;
86 static char *layout_id;
87 struct flash_layout *layout;
88 static char *opt_hw_id;
89 static char *opt_hw_rev;
90 static char *opt_hw_ver_add;
91 static int fw_ver_lo;
92 static int fw_ver_mid;
93 static int fw_ver_hi;
94 static int sver_lo;
95 static int sver_hi;
96 struct file_info kernel_info;
97 static uint32_t kernel_la = 0;
98 static uint32_t kernel_ep = 0;
99 uint32_t kernel_len = 0;
100 struct file_info rootfs_info;
101 uint32_t rootfs_ofs = 0;
102 uint32_t rootfs_align;
103 static struct file_info boot_info = { 0 };
104 int combined;
105 int strip_padding;
106 int add_jffs2_eof;
107
108 static struct file_info inspect_info;
109 static int extract = 0;
110
111 char md5salt_normal[MD5SUM_LEN] = {
112 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
113 0xdc, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x37,
114 };
115
116 char md5salt_boot[MD5SUM_LEN] = {
117 0x8c, 0xef, 0x33, 0x5f, 0xd5, 0xc5, 0xce, 0xfa,
118 0xac, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
119 };
120
121 static struct flash_layout layouts[] = {
122 {
123 .id = "4Mmtk",
124 .fw_max_len = 0x3c0000,
125 .kernel_la = 0x80000000,
126 .kernel_ep = 0x80000000,
127 .rootfs_ofs = 0x140000,
128 }, {
129 .id = "4MLmtk",
130 .fw_max_len = 0x3d0000,
131 .kernel_la = 0x80000000,
132 .kernel_ep = 0x80000000,
133 .rootfs_ofs = 0x140000,
134 }, {
135 .id = "8Mltq",
136 .fw_max_len = 0x7a0000,
137 .kernel_la = 0x80002000,
138 .kernel_ep = 0x80002000,
139 .rootfs_ofs = 0x140000,
140 }, {
141 .id = "16Mltq",
142 .fw_max_len = 0xf90000,
143 .kernel_la = 0x80002000,
144 .kernel_ep = 0x800061b0,
145 .rootfs_ofs = 0x140000,
146 }, {
147 .id = "8Mmtk",
148 .fw_max_len = 0x7a0000,
149 .kernel_la = 0x80000000,
150 .kernel_ep = 0x80000000,
151 .rootfs_ofs = 0x140000,
152 }, {
153 .id = "8MSUmtk", /* Split U-Boot OS */
154 .fw_max_len = 0x770000,
155 .kernel_la = 0x80000000,
156 .kernel_ep = 0x80000000,
157 .rootfs_ofs = 0x140000,
158 }, {
159 .id = "8MLmtk",
160 .fw_max_len = 0x7b0000,
161 .kernel_la = 0x80000000,
162 .kernel_ep = 0x80000000,
163 .rootfs_ofs = 0x140000,
164 }, {
165 .id = "8Mqca",
166 .fw_max_len = 0x7a0000,
167 .kernel_la = 0x80060000,
168 .kernel_ep = 0x80060000,
169 .rootfs_ofs = 0x140000,
170 }, {
171 .id = "16Mqca",
172 .fw_max_len = 0xf90000,
173 .kernel_la = 0x80060000,
174 .kernel_ep = 0x80060000,
175 .rootfs_ofs = 0x140000,
176 }, {
177 /* terminating entry */
178 }
179 };
180
181 static void usage(int status)
182 {
183 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
184
185 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
186 fprintf(stream,
187 "\n"
188 "Options:\n"
189 " -c use combined kernel image\n"
190 " -e swap endianness in kernel load address and entry point\n"
191 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
192 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
193 " -H <hwid> use hardware id specified with <hwid>\n"
194 " -W <hwrev> use hardware revision specified with <hwrev>\n"
195 " -w <hwveradd> use additional hardware version specified with <hwveradd>\n"
196 " -F <id> use flash layout specified with <id>\n"
197 " -k <file> read kernel image from the file <file>\n"
198 " -r <file> read rootfs image from the file <file>\n"
199 " -b <file> read bootloader image from the file <file>\n"
200 " -a <align> align the rootfs start on an <align> bytes boundary\n"
201 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
202 " -o <file> write output to the file <file>\n"
203 " -s strip padding from the end of the image\n"
204 " -j add jffs2 end-of-filesystem markers\n"
205 " -N <vendor> set image vendor to <vendor>\n"
206 " -T <version> set header version to <version>\n"
207 " -V <version> set image version to <version>\n"
208 " -v <version> set firmware version to <version>\n"
209 " -y <version> set secondary version to <version>\n"
210 " -i <file> inspect given firmware file <file>\n"
211 " -x extract bootloader, kernel and rootfs while inspecting (requires -i)\n"
212 " -h show this screen\n"
213 );
214
215 exit(status);
216 }
217
218 static int check_options(void)
219 {
220 int ret;
221
222 if (inspect_info.file_name) {
223 ret = get_file_stat(&inspect_info);
224 if (ret)
225 return ret;
226
227 return 0;
228 } else if (extract) {
229 ERR("no firmware for inspection specified");
230 return -1;
231 }
232
233 if (opt_hw_id == NULL) {
234 ERR("hardware id must be specified");
235 return -1;
236 }
237
238 board = &custom_board;
239
240 if (layout_id == NULL) {
241 ERR("flash layout is not specified");
242 return -1;
243 }
244
245 board->hw_id = strtoul(opt_hw_id, NULL, 0);
246
247 board->hw_rev = 1;
248 board->hw_ver_add = 0;
249
250 if (opt_hw_rev)
251 board->hw_rev = strtoul(opt_hw_rev, NULL, 0);
252 if (opt_hw_ver_add)
253 board->hw_ver_add = strtoul(opt_hw_ver_add, NULL, 0);
254
255 layout = find_layout(layouts, layout_id);
256 if (layout == NULL) {
257 ERR("unknown flash layout \"%s\"", layout_id);
258 return -1;
259 }
260
261 if (!kernel_la)
262 kernel_la = layout->kernel_la;
263 if (!kernel_ep)
264 kernel_ep = layout->kernel_ep;
265 if (!rootfs_ofs)
266 rootfs_ofs = layout->rootfs_ofs;
267
268 /* check bootloader */
269 ret = get_file_stat(&boot_info);
270 if (ret) {
271 ERR("Can not load bootloader image.");
272 return ret;
273 }
274
275 if (boot_info.file_size > 2 * 64 * 1024) {
276 /* the offset in fill_header is hardcoded to 128k */
277 ERR("Bootloader image is bigger than 128k.");
278 return -1;
279 }
280
281 if (kernel_info.file_name == NULL) {
282 ERR("no kernel image specified");
283 return -1;
284 }
285 ret = get_file_stat(&kernel_info);
286 if (ret)
287 return ret;
288
289 kernel_len = kernel_info.file_size;
290
291 if (combined) {
292 if (kernel_info.file_size >
293 layout->fw_max_len - sizeof(struct fw_header)) {
294 ERR("kernel image is too big");
295 return -1;
296 }
297 } else {
298 if (rootfs_info.file_name == NULL) {
299 ERR("no rootfs image specified");
300 return -1;
301 }
302
303 ret = get_file_stat(&rootfs_info);
304 if (ret)
305 return ret;
306
307 if (rootfs_align) {
308 kernel_len += sizeof(struct fw_header);
309 rootfs_ofs = ALIGN(kernel_len, rootfs_align);
310 kernel_len -= sizeof(struct fw_header);
311
312 DBG("rootfs offset aligned to 0x%u", rootfs_ofs);
313
314 if (kernel_len + rootfs_info.file_size >
315 layout->fw_max_len - sizeof(struct fw_header)) {
316 ERR("images are too big");
317 return -1;
318 }
319 } else {
320 if (kernel_info.file_size >
321 rootfs_ofs - sizeof(struct fw_header)) {
322 ERR("kernel image is too big");
323 return -1;
324 }
325
326 if (rootfs_info.file_size >
327 (layout->fw_max_len - rootfs_ofs)) {
328 ERR("rootfs image is too big");
329 return -1;
330 }
331 }
332 }
333
334 if (ofname == NULL) {
335 ERR("no output file specified");
336 return -1;
337 }
338
339 ret = sscanf(fw_ver, "%d.%d.%d", &fw_ver_hi, &fw_ver_mid, &fw_ver_lo);
340 if (ret != 3) {
341 ERR("invalid firmware version '%s'", fw_ver);
342 return -1;
343 }
344
345 ret = sscanf(sver, "%d.%d", &sver_hi, &sver_lo);
346 if (ret != 2) {
347 ERR("invalid secondary version '%s'", sver);
348 return -1;
349 }
350
351 return 0;
352 }
353
354 void fill_header_bootloader(char *buf, int len, int with_bootloader)
355 {
356 struct fw_header *hdr = (struct fw_header *)buf;
357 unsigned ver_len;
358 unsigned int offset = 0;
359
360 if (with_bootloader) {
361 /* ensure the bootloader is padded to 64k */
362 offset = boot_info.file_size & (64 * 1024);
363 if (offset < boot_info.file_size)
364 offset += 64 * 1024;
365
366 offset += sizeof(struct fw_header);
367 }
368
369 memset(hdr, '\xff', sizeof(struct fw_header));
370
371 hdr->version = htonl(bswap_32(hdr_ver));
372 ver_len = strlen(version);
373 if (ver_len > (sizeof(hdr->fw_version) - 1))
374 ver_len = sizeof(hdr->fw_version) - 1;
375
376 memcpy(hdr->fw_version, version, ver_len);
377 hdr->fw_version[ver_len] = 0;
378
379 hdr->hw_id = htonl(board->hw_id);
380 hdr->hw_rev = htonl(board->hw_rev);
381 hdr->hw_ver_add = htonl(board->hw_ver_add);
382
383 if (boot_info.file_size == 0 || !with_bootloader) {
384 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
385 hdr->boot_ofs = htonl(0);
386 hdr->boot_len = htonl(0);
387 } else {
388 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
389 hdr->boot_ofs = htonl(0);
390 hdr->boot_len = htonl(boot_info.file_size);
391 }
392
393 hdr->kernel_la = htonl(kernel_la);
394 hdr->kernel_ep = htonl(kernel_ep);
395 hdr->fw_length = htonl(layout->fw_max_len + offset);
396 hdr->kernel_ofs = htonl(sizeof(struct fw_header) + offset);
397 hdr->kernel_len = htonl(kernel_len);
398 if (!combined) {
399 /* even the bootloader doesnt increased the root_ofs. Unsure if this parser
400 * always ignores the rootfs or only in the 841v13
401 */
402 hdr->rootfs_ofs = htonl(rootfs_ofs);
403 hdr->rootfs_len = htonl(rootfs_info.file_size);
404 }
405
406 hdr->unk2 = htonl(0);
407 hdr->unk3 = htonl(0xffffffff);
408 hdr->unk4 = htons(0x55aa);
409 hdr->unk5 = 0xa5;
410
411 hdr->sver_hi = sver_hi;
412 hdr->sver_lo = sver_lo;
413
414 hdr->ver_hi = fw_ver_hi;
415 hdr->ver_mid = fw_ver_mid;
416 hdr->ver_lo = fw_ver_lo;
417
418 if (board->flags & FLAG_LE_KERNEL_LA_EP) {
419 hdr->kernel_la = bswap_32(hdr->kernel_la);
420 hdr->kernel_ep = bswap_32(hdr->kernel_ep);
421 }
422
423 get_md5(buf, len, hdr->md5sum1);
424 }
425
426 /* fill_header get called by mktplinkfw_lib to fill the header in front of the kernel. */
427 void fill_header(char *buf, int len) {
428 fill_header_bootloader(buf, len, 0);
429 }
430
431 static int inspect_fw(void)
432 {
433 char *buf;
434 struct fw_header *hdr;
435 uint8_t md5sum[MD5SUM_LEN];
436 struct board_info *board;
437 int ret = EXIT_FAILURE;
438
439 buf = malloc(inspect_info.file_size);
440 if (!buf) {
441 ERR("no memory for buffer!\n");
442 goto out;
443 }
444
445 ret = read_to_buf(&inspect_info, buf);
446 if (ret)
447 goto out_free_buf;
448 hdr = (struct fw_header *)buf;
449
450 board = &custom_board;
451
452 if (board->flags & FLAG_LE_KERNEL_LA_EP) {
453 hdr->kernel_la = bswap_32(hdr->kernel_la);
454 hdr->kernel_ep = bswap_32(hdr->kernel_ep);
455 }
456
457 inspect_fw_pstr("File name", inspect_info.file_name);
458 inspect_fw_phexdec("File size", inspect_info.file_size);
459
460 switch(bswap_32(ntohl(hdr->version))) {
461 case 2:
462 case 3:
463 break;
464 default:
465 ERR("file does not seem to have V2/V3 header!\n");
466 goto out_free_buf;
467 }
468
469 inspect_fw_phexdec("Version 2 Header size", sizeof(struct fw_header));
470
471 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
472 if (ntohl(hdr->boot_len) == 0)
473 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
474 else
475 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
476 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
477
478 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
479 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
480 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
481 } else {
482 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
483 }
484 if (ntohl(hdr->unk2) != 0)
485 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
486 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
487 "(purpose yet unknown, unchecked here)");
488
489 if (ntohl(hdr->unk3) != 0xffffffff)
490 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
491
492 if (ntohs(hdr->unk4) != 0x55aa)
493 inspect_fw_phexdec("Unknown value 4", hdr->unk4);
494
495 if (hdr->unk5 != 0xa5)
496 inspect_fw_phexdec("Unknown value 5", hdr->unk5);
497
498 printf("\n");
499
500 inspect_fw_pstr("Firmware version", hdr->fw_version);
501 inspect_fw_phex("Hardware ID", ntohl(hdr->hw_id));
502 inspect_fw_phex("Hardware Revision",
503 ntohl(hdr->hw_rev));
504 inspect_fw_phex("Additional HW Version",
505 ntohl(hdr->hw_ver_add));
506
507 printf("%-23s: %d.%d.%d-%d.%d\n", "Software version",
508 hdr->ver_hi, hdr->ver_mid, hdr->ver_lo,
509 hdr->sver_hi, hdr->sver_lo);
510
511 printf("\n");
512
513 inspect_fw_phexdec("Kernel data offset",
514 ntohl(hdr->kernel_ofs));
515 inspect_fw_phexdec("Kernel data length",
516 ntohl(hdr->kernel_len));
517 inspect_fw_phex("Kernel load address",
518 ntohl(hdr->kernel_la));
519 inspect_fw_phex("Kernel entry point",
520 ntohl(hdr->kernel_ep));
521 inspect_fw_phexdec("Rootfs data offset",
522 ntohl(hdr->rootfs_ofs));
523 inspect_fw_phexdec("Rootfs data length",
524 ntohl(hdr->rootfs_len));
525 inspect_fw_phexdec("Boot loader data offset",
526 ntohl(hdr->boot_ofs));
527 inspect_fw_phexdec("Boot loader data length",
528 ntohl(hdr->boot_len));
529 inspect_fw_phexdec("Total firmware length",
530 ntohl(hdr->fw_length));
531
532 if (extract) {
533 FILE *fp;
534 char *filename;
535
536 printf("\n");
537
538 if (hdr->boot_len) {
539 filename = malloc(strlen(inspect_info.file_name) + 8);
540 sprintf(filename, "%s-bootloader", inspect_info.file_name);
541 printf("Extracting bootloader to \"%s\"...\n", filename);
542 fp = fopen(filename, "w");
543 if (fp) {
544 if (!fwrite(buf + sizeof(struct fw_header) + ntohl(hdr->boot_ofs),
545 ntohl(hdr->boot_len), 1, fp)) {
546 ERR("error in fwrite(): %s", strerror(errno));
547 }
548 fclose(fp);
549 } else {
550 ERR("error in fopen(): %s", strerror(errno));
551 }
552 free(filename);
553 }
554
555 filename = malloc(strlen(inspect_info.file_name) + 8);
556 sprintf(filename, "%s-kernel", inspect_info.file_name);
557 printf("Extracting kernel to \"%s\"...\n", filename);
558 fp = fopen(filename, "w");
559 if (fp) {
560 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
561 ntohl(hdr->kernel_len), 1, fp)) {
562 ERR("error in fwrite(): %s", strerror(errno));
563 }
564 fclose(fp);
565 } else {
566 ERR("error in fopen(): %s", strerror(errno));
567 }
568 free(filename);
569
570 filename = malloc(strlen(inspect_info.file_name) + 8);
571 sprintf(filename, "%s-rootfs", inspect_info.file_name);
572 printf("Extracting rootfs to \"%s\"...\n", filename);
573 fp = fopen(filename, "w");
574 if (fp) {
575 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
576 ntohl(hdr->rootfs_len), 1, fp)) {
577 ERR("error in fwrite(): %s", strerror(errno));
578 }
579 fclose(fp);
580 } else {
581 ERR("error in fopen(): %s", strerror(errno));
582 }
583 free(filename);
584 }
585
586 out_free_buf:
587 free(buf);
588 out:
589 return ret;
590 }
591
592 /* prepend a second image header and the bootloader.
593 *
594 * |------------|
595 * |image header|
596 * |------------|
597 * |bootloader |
598 * |------------|
599 * |optional pad|
600 * |------------|
601 * |image header|
602 * |------------|
603 * |kernel image|
604 * |------------|
605 * |rootfs image|
606 * |------------|
607 *
608 * The padding is optional. The second image header should begin a 64k boundary.
609 */
610 int prepend_bootloader() {
611 unsigned int buflen = 0;
612 int ret = 0, bootloader_padded = 0;
613 char *buf = 0, *p = 0;
614 struct file_info image;
615
616 /* calculate blocks to ensure padding */
617 bootloader_padded = boot_info.file_size & (64 * 1024);
618 if (bootloader_padded < boot_info.file_size)
619 bootloader_padded += 64 * 1024;
620
621 image.file_name = ofname;
622 ret = get_file_stat(&image);
623 if (ret) {
624 ERR("Can not load the output image");
625 return ret;
626 }
627
628 buflen = image.file_size + bootloader_padded + sizeof(struct fw_header);
629 buf = malloc(buflen);
630 if (buf == NULL) {
631 ERR("Can not allocate buffer %d bytes", buflen);
632 return -1;
633 }
634 memset(buf, 0xff, buflen);
635
636 /* load old image */
637 p = buf + bootloader_padded + sizeof(struct fw_header);
638 ret = read_to_buf(&image, p);
639 if (ret) {
640 ERR("Can not read output image");
641 goto out_free_buf;
642 }
643
644 p = buf + sizeof(struct fw_header);
645 ret = read_to_buf(&boot_info, p);
646
647 fill_header_bootloader(buf, buflen, 1);
648
649 ret = write_fw(ofname, buf, buflen);
650 if (ret)
651 goto out_free_buf;
652
653 ret = EXIT_SUCCESS;
654
655 out_free_buf:
656 free(buf);
657
658 return ret;
659 }
660
661 int main(int argc, char *argv[])
662 {
663 int ret = EXIT_FAILURE;
664
665 progname = basename(argv[0]);
666
667 while ( 1 ) {
668 int c;
669
670 c = getopt(argc, argv, "a:b:H:E:F:L:V:N:W:w:ci:k:r:R:o:xhsjv:y:T:e");
671 if (c == -1)
672 break;
673
674 switch (c) {
675 case 'a':
676 sscanf(optarg, "0x%x", &rootfs_align);
677 break;
678 case 'b':
679 boot_info.file_name = optarg;
680 break;
681 case 'H':
682 opt_hw_id = optarg;
683 break;
684 case 'E':
685 sscanf(optarg, "0x%x", &kernel_ep);
686 break;
687 case 'F':
688 layout_id = optarg;
689 break;
690 case 'W':
691 opt_hw_rev = optarg;
692 break;
693 case 'w':
694 opt_hw_ver_add = optarg;
695 break;
696 case 'L':
697 sscanf(optarg, "0x%x", &kernel_la);
698 break;
699 case 'V':
700 version = optarg;
701 break;
702 case 'v':
703 fw_ver = optarg;
704 break;
705 case 'y':
706 sver = optarg;
707 break;
708 case 'N':
709 vendor = optarg;
710 break;
711 case 'c':
712 combined++;
713 break;
714 case 'k':
715 kernel_info.file_name = optarg;
716 break;
717 case 'r':
718 rootfs_info.file_name = optarg;
719 break;
720 case 'R':
721 sscanf(optarg, "0x%x", &rootfs_ofs);
722 break;
723 case 'o':
724 ofname = optarg;
725 break;
726 case 's':
727 strip_padding = 1;
728 break;
729 case 'i':
730 inspect_info.file_name = optarg;
731 break;
732 case 'j':
733 add_jffs2_eof = 1;
734 break;
735 case 'x':
736 extract = 1;
737 break;
738 case 'T':
739 hdr_ver = atoi(optarg);
740 break;
741 case 'e':
742 custom_board.flags = FLAG_LE_KERNEL_LA_EP;
743 break;
744 case 'h':
745 usage(EXIT_SUCCESS);
746 break;
747 default:
748 usage(EXIT_FAILURE);
749 break;
750 }
751 }
752
753 ret = check_options();
754 if (ret)
755 goto out;
756
757 if (!inspect_info.file_name) {
758 ret = build_fw(sizeof(struct fw_header));
759 if (ret == 0 && boot_info.file_size > 0)
760 ret = prepend_bootloader();
761 }
762 else
763 ret = inspect_fw();
764
765 out:
766 return ret;
767 }