d4e36eb5784c56062886120b85025d2b31dd6216
[openwrt/svn-archive/archive.git] / tools / firmware-utils / src / mkmylofw.c
1 /*
2 * Copyright (C) 2006-2008 Gabor Juhos <juhosg@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <unistd.h> /* for unlink() */
26 #include <libgen.h>
27 #include <getopt.h> /* for getopt() */
28 #include <stdarg.h>
29 #include <errno.h>
30 #include <sys/stat.h>
31 #include <endian.h> /* for __BYTE_ORDER */
32
33 #if defined(__CYGWIN__)
34 # include <byteswap.h>
35 #endif
36
37 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
38 # define HOST_TO_LE16(x) (x)
39 # define HOST_TO_LE32(x) (x)
40 #else
41 # define HOST_TO_LE16(x) bswap_16(x)
42 # define HOST_TO_LE32(x) bswap_32(x)
43 #endif
44
45 #include "myloader.h"
46
47 #define MAX_FW_BLOCKS 32
48 #define MAX_ARG_COUNT 32
49 #define MAX_ARG_LEN 1024
50 #define FILE_BUF_LEN (16*1024)
51
52 struct fw_block {
53 uint32_t addr;
54 uint32_t blocklen; /* length of the block */
55 uint32_t flags;
56
57 char *name; /* name of the file */
58 uint32_t size; /* length of the file */
59 uint32_t crc; /* crc value of the file */
60 };
61
62 #define BLOCK_FLAG_HAVEHDR 0x0001
63
64 struct cpx_board {
65 char *model; /* model number*/
66 char *name; /* model name*/
67 char *desc; /* description */
68 uint16_t vid; /* vendor id */
69 uint16_t did; /* device id */
70 uint16_t svid; /* sub vendor id */
71 uint16_t sdid; /* sub device id */
72 uint32_t flash_size; /* size of flash */
73 uint32_t part_offset; /* offset of the partition_table */
74 uint32_t part_size; /* size of the partition_table */
75 };
76
77 #define BOARD(_vid, _did, _svid, _sdid, _flash, _mod, _name, _desc, _po, _ps) { \
78 .model = _mod, .name = _name, .desc = _desc, \
79 .vid = _vid, .did = _did, .svid = _svid, .sdid = _sdid, \
80 .flash_size = (_flash << 20), \
81 .part_offset = _po, .part_size = _ps }
82
83 #define CPX_BOARD(_did, _flash, _mod, _name, _desc, _po, _ps) \
84 BOARD(VENID_COMPEX, _did, VENID_COMPEX, _did, _flash, _mod, _name, _desc, _po, _ps)
85
86 #define CPX_BOARD_ADM(_did, _flash, _mod, _name, _desc) \
87 CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x10000, 0x10000)
88
89 #define CPX_BOARD_AR71XX(_did, _flash, _mod, _name, _desc) \
90 CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x20000, 0x8000)
91
92 #define CPX_BOARD_AR23XX(_did, _flash, _mod, _name, _desc) \
93 CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x10000, 0x10000)
94
95 #define ALIGN(x,y) ((x)+((y)-1)) & ~((y)-1)
96
97 char *progname;
98 char *ofname = NULL;
99
100 uint32_t flash_size = 0;
101 int fw_num_partitions = 0;
102 int fw_num_blocks = 0;
103 int verblevel = 0;
104
105 struct mylo_fw_header fw_header;
106 struct mylo_partition fw_partitions[MYLO_MAX_PARTITIONS];
107 struct fw_block fw_blocks[MAX_FW_BLOCKS];
108 struct cpx_board *board;
109
110 struct cpx_board boards[] = {
111 CPX_BOARD_ADM(DEVID_COMPEX_NP18A, 4,
112 "NP18A", "Compex NetPassage 18A",
113 "Dualband Wireless A+G Internet Gateway"),
114 CPX_BOARD_ADM(DEVID_COMPEX_NP26G8M, 2,
115 "NP26G8M", "Compex NetPassage 26G (8M)",
116 "Wireless-G Broadband Multimedia Gateway"),
117 CPX_BOARD_ADM(DEVID_COMPEX_NP26G16M, 4,
118 "NP26G16M", "Compex NetPassage 26G (16M)",
119 "Wireless-G Broadband Multimedia Gateway"),
120 CPX_BOARD_ADM(DEVID_COMPEX_NP27G, 4,
121 "NP27G", "Compex NetPassage 27G",
122 "Wireless-G 54Mbps eXtended Range Router"),
123 CPX_BOARD_ADM(DEVID_COMPEX_NP28G, 4,
124 "NP28G", "Compex NetPassage 28G",
125 "Wireless 108Mbps Super-G XR Multimedia Router with 4 USB Ports"),
126 CPX_BOARD_ADM(DEVID_COMPEX_NP28GHS, 4,
127 "NP28GHS", "Compex NetPassage 28G (HotSpot)",
128 "HotSpot Solution"),
129 CPX_BOARD_ADM(DEVID_COMPEX_WP18, 4,
130 "WP18", "Compex NetPassage WP18",
131 "Wireless-G 54Mbps A+G Dualband Access Point"),
132 CPX_BOARD_ADM(DEVID_COMPEX_WP54G, 4,
133 "WP54G", "Compex WP54G",
134 "Wireless-G 54Mbps XR Access Point"),
135 CPX_BOARD_ADM(DEVID_COMPEX_WP54Gv1C, 2,
136 "WP54Gv1C", "Compex WP54G rev.1C",
137 "Wireless-G 54Mbps XR Access Point"),
138 CPX_BOARD_ADM(DEVID_COMPEX_WP54AG, 4,
139 "WP54AG", "Compex WP54AG",
140 "Wireless-AG 54Mbps XR Access Point"),
141 CPX_BOARD_ADM(DEVID_COMPEX_WPP54G, 4,
142 "WPP54G", "Compex WPP54G",
143 "Outdoor Access Point"),
144 CPX_BOARD_ADM(DEVID_COMPEX_WPP54AG, 4,
145 "WPP54AG", "Compex WPP54AG",
146 "Outdoor Access Point"),
147
148 CPX_BOARD_AR71XX(DEVID_COMPEX_WP543, 2,
149 "WP543", "Compex WP543",
150 "BareBoard"),
151
152 CPX_BOARD_AR23XX(DEVID_COMPEX_NP25G, 4,
153 "NP25G", "Compex NetPassage 25G",
154 "Wireless 54Mbps XR Router"),
155 CPX_BOARD_AR23XX(DEVID_COMPEX_WPE53G, 4,
156 "WPE53G", "Compex NetPassage 25G",
157 "Wireless 54Mbps XR Access Point"),
158 {.model = NULL}
159 };
160
161 void
162 errmsgv(int syserr, const char *fmt, va_list arg_ptr)
163 {
164 int save = errno;
165
166 fflush(0);
167 fprintf(stderr, "[%s] Error: ", progname);
168 vfprintf(stderr, fmt, arg_ptr);
169 if (syserr != 0) {
170 fprintf(stderr, ": %s", strerror(save));
171 }
172 fprintf(stderr, "\n");
173 }
174
175 void
176 errmsg(int syserr, const char *fmt, ...)
177 {
178 va_list arg_ptr;
179 va_start(arg_ptr, fmt);
180 errmsgv(syserr, fmt, arg_ptr);
181 va_end(arg_ptr);
182 }
183
184 void
185 dbgmsg(int level, const char *fmt, ...)
186 {
187 va_list arg_ptr;
188 if (verblevel >= level) {
189 fflush(0);
190 va_start(arg_ptr, fmt);
191 vfprintf(stderr, fmt, arg_ptr);
192 fprintf(stderr, "\n");
193 va_end(arg_ptr);
194 }
195 }
196
197
198 void
199 usage(int status)
200 {
201 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
202 struct cpx_board *board;
203
204 fprintf(stream, "Usage: %s [OPTION...] <file>\n", progname);
205 fprintf(stream,
206 "\n"
207 " <file> write output to the <file>\n"
208 "\n"
209 "Options:\n"
210 " -B <board> create firmware for the board specified with <board>.\n"
211 " This option set vendor id, device id, subvendor id,\n"
212 " subdevice id, and flash size options to the right value.\n"
213 " valid <board> values:\n");
214 for (board = boards; board->model != NULL; board++){
215 fprintf(stream,
216 " %-12s: %s\n",
217 board->model, board->name);
218 };
219 fprintf(stream,
220 " -i <vid>:<did>[:<svid>[:<sdid>]]\n"
221 " create firmware for board with vendor id <vid>, device\n"
222 " id <did>, subvendor id <svid> and subdevice id <sdid>.\n"
223 " -r <rev> set board revision to <rev>.\n"
224 " -s <size> set flash size to <size>\n"
225 " -b <addr>:<len>[:[<flags>]:<file>]\n"
226 " define block at <addr> with length of <len>.\n"
227 " valid <flag> values:\n"
228 " h : add crc header before the file data.\n"
229 " -p <addr>:<len>[:<flags>[:<param>[:<file>]]]\n"
230 " add partition at <addr>, with size of <len> to the\n"
231 " partition table, set partition flags to <flags> and\n"
232 " partition parameter to <param>. If the <file> is specified\n"
233 " content of the file is also added to the firmware image.\n"
234 " valid <flag> values:\n"
235 " a: this is the active partition. The bootloader loads\n"
236 " the firmware from this partition.\n"
237 " h: the partition data have a header.\n"
238 " l: the partition data uses LZMA compression.\n"
239 " p: the bootloader loads data from this partition to\n"
240 " the RAM before decompress it.\n"
241 " -h show this screen\n"
242 );
243
244 exit(status);
245 }
246
247 /*
248 * Code to compute the CRC-32 table. Borrowed from
249 * gzip-1.0.3/makecrc.c.
250 */
251
252 static uint32_t crc_32_tab[256];
253
254 void
255 init_crc_table(void)
256 {
257 /* Not copyrighted 1990 Mark Adler */
258
259 uint32_t c; /* crc shift register */
260 uint32_t e; /* polynomial exclusive-or pattern */
261 int i; /* counter for all possible eight bit values */
262 int k; /* byte being shifted into crc apparatus */
263
264 /* terms of polynomial defining this crc (except x^32): */
265 static const int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
266
267 /* Make exclusive-or pattern from polynomial */
268 e = 0;
269 for (i = 0; i < sizeof(p)/sizeof(int); i++)
270 e |= 1L << (31 - p[i]);
271
272 crc_32_tab[0] = 0;
273
274 for (i = 1; i < 256; i++) {
275 c = 0;
276 for (k = i | 256; k != 1; k >>= 1) {
277 c = c & 1 ? (c >> 1) ^ e : c >> 1;
278 if (k & 1)
279 c ^= e;
280 }
281 crc_32_tab[i] = c;
282 }
283 }
284
285
286 void
287 update_crc(uint8_t *p, uint32_t len, uint32_t *crc)
288 {
289 uint32_t t;
290
291 t = *crc ^ 0xFFFFFFFFUL;
292 while (len--) {
293 t = crc_32_tab[(t ^ *p++) & 0xff] ^ (t >> 8);
294 }
295 *crc = t ^ 0xFFFFFFFFUL;
296 }
297
298
299 uint32_t
300 get_crc(uint8_t *p, uint32_t len)
301 {
302 uint32_t crc;
303
304 crc = 0;
305 update_crc(p ,len , &crc);
306 return crc;
307 }
308
309
310 int
311 str2u32(char *arg, uint32_t *val)
312 {
313 char *err = NULL;
314 uint32_t t;
315
316 errno=0;
317 t = strtoul(arg, &err, 0);
318 if (errno || (err==arg) || ((err != NULL) && *err)) {
319 return -1;
320 }
321
322 *val = t;
323 return 0;
324 }
325
326
327 int
328 str2u16(char *arg, uint16_t *val)
329 {
330 char *err = NULL;
331 uint32_t t;
332
333 errno=0;
334 t = strtoul(arg, &err, 0);
335 if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
336 return -1;
337 }
338
339 *val = t & 0xFFFF;
340 return 0;
341 }
342
343
344 struct cpx_board *
345 find_board(char *model){
346 struct cpx_board *board;
347 struct cpx_board *tmp;
348
349 board = NULL;
350 for (tmp = boards; tmp->model != NULL; tmp++){
351 if (strcasecmp(model, tmp->model) == 0) {
352 board = tmp;
353 break;
354 }
355 };
356
357 return board;
358 }
359
360
361 int
362 get_file_crc(struct fw_block *ff)
363 {
364 FILE *f;
365 uint8_t buf[FILE_BUF_LEN];
366 uint32_t readlen = sizeof(buf);
367 int res = -1;
368 size_t len;
369
370 if ((ff->flags & BLOCK_FLAG_HAVEHDR) == 0) {
371 res = 0;
372 goto out;
373 }
374
375 errno = 0;
376 f = fopen(ff->name,"r");
377 if (errno) {
378 errmsg(1,"unable to open file %s", ff->name);
379 goto out;
380 }
381
382 ff->crc = 0;
383 len = ff->size;
384 while (len > 0) {
385 if (len < readlen)
386 readlen = len;
387
388 errno = 0;
389 fread(buf, readlen, 1, f);
390 if (errno) {
391 errmsg(1,"unable to read from file %s", ff->name);
392 goto out_close;
393 }
394
395 update_crc(buf, readlen, &ff->crc);
396 len -= readlen;
397 }
398
399 res = 0;
400
401 out_close:
402 fclose(f);
403 out:
404 return res;
405 }
406
407
408 int
409 process_files(void)
410 {
411 struct fw_block *b;
412 struct stat st;
413 int i;
414
415 for (i = 0; i < fw_num_blocks; i++) {
416 b = &fw_blocks[i];
417 if ((b->addr + b->blocklen) > flash_size) {
418 errmsg(0, "block at 0x%08X is too big", b->addr);
419 return -1;
420 }
421 if (b->name == NULL)
422 continue;
423
424 if (stat(b->name, &st) < 0) {
425 errmsg(0, "stat failed on %s",b->name);
426 return -1;
427 }
428 if (b->blocklen == 0) {
429 b->blocklen = flash_size - b->addr;
430 }
431 if (st.st_size > b->blocklen) {
432 errmsg(0,"file %s is too big",b->name);
433 return -1;
434 }
435
436 b->size = st.st_size;
437 }
438
439 return 0;
440 }
441
442
443 int
444 process_partitions(void)
445 {
446 struct mylo_partition *part;
447 int i;
448
449 for (i = 0; i < fw_num_partitions; i++) {
450 part = &fw_partitions[i];
451
452 if (part->addr > flash_size) {
453 errmsg(0, "invalid partition at 0x%08X", part->addr);
454 return -1;
455 }
456
457 if ((part->addr + part->size) > flash_size) {
458 errmsg(0, "partition at 0x%08X is too big", part->addr);
459 return -1;
460 }
461 }
462
463 return 0;
464 }
465
466
467 /*
468 * routines to write data to the output file
469 */
470 int
471 write_out_data(FILE *outfile, uint8_t *data, size_t len, uint32_t *crc)
472 {
473 errno = 0;
474
475 fwrite(data, len, 1, outfile);
476 if (errno) {
477 errmsg(1,"unable to write output file");
478 return -1;
479 }
480
481 if (crc) {
482 update_crc(data, len, crc);
483 }
484
485 return 0;
486 }
487
488
489 int
490 write_out_desc(FILE *outfile, struct mylo_fw_blockdesc *desc, uint32_t *crc)
491 {
492 return write_out_data(outfile, (uint8_t *)desc,
493 sizeof(*desc), crc);
494 }
495
496
497 int
498 write_out_padding(FILE *outfile, size_t len, uint8_t padc, uint32_t *crc)
499 {
500 uint8_t buff[512];
501 size_t buflen = sizeof(buff);
502
503 memset(buff, padc, buflen);
504
505 while (len > 0) {
506 if (len < buflen)
507 buflen = len;
508
509 if (write_out_data(outfile, buff, buflen, crc))
510 return -1;
511
512 len -= buflen;
513 }
514
515 return 0;
516 }
517
518
519 int
520 write_out_file(FILE *outfile, struct fw_block *block, uint32_t *crc)
521 {
522 char buff[FILE_BUF_LEN];
523 size_t buflen = sizeof(buff);
524 FILE *f;
525 size_t len;
526
527 errno = 0;
528
529 if (block->name == NULL) {
530 return 0;
531 }
532
533 if ((block->flags & BLOCK_FLAG_HAVEHDR) != 0) {
534 struct mylo_partition_header ph;
535
536 if (get_file_crc(block) != 0)
537 return -1;
538
539 ph.crc = HOST_TO_LE32(block->crc);
540 ph.len = HOST_TO_LE32(block->size);
541
542 if (write_out_data(outfile, (uint8_t *)&ph, sizeof(ph), crc) != 0)
543 return -1;
544 }
545
546 f = fopen(block->name,"r");
547 if (errno) {
548 errmsg(1,"unable to open file: %s", block->name);
549 return -1;
550 }
551
552 len = block->size;
553 while (len > 0) {
554 if (len < buflen)
555 buflen = len;
556
557 /* read data from source file */
558 errno = 0;
559 fread(buff, buflen, 1, f);
560 if (errno != 0) {
561 errmsg(1,"unable to read from file: %s",block->name);
562 return -1;
563 }
564
565 if (write_out_data(outfile, buff, buflen, crc) != 0)
566 return -1;
567
568 len -= buflen;
569 }
570
571 fclose(f);
572
573 /* align next block on a 4 byte boundary */
574 len = ALIGN(len,4) - block->size;
575 if (write_out_padding(outfile, len, 0xFF, crc))
576 return -1;
577
578 dbgmsg(1,"file %s written out", block->name);
579 return 0;
580 }
581
582
583 int
584 write_out_header(FILE *outfile, uint32_t *crc)
585 {
586 struct mylo_fw_header hdr;
587
588 memset(&hdr, 0, sizeof(hdr));
589
590 hdr.magic = HOST_TO_LE32(MYLO_MAGIC_FIRMWARE);
591 hdr.crc = HOST_TO_LE32(fw_header.crc);
592 hdr.vid = HOST_TO_LE16(fw_header.vid);
593 hdr.did = HOST_TO_LE16(fw_header.did);
594 hdr.svid = HOST_TO_LE16(fw_header.svid);
595 hdr.sdid = HOST_TO_LE16(fw_header.sdid);
596 hdr.rev = HOST_TO_LE32(fw_header.rev);
597 hdr.fwhi = HOST_TO_LE32(fw_header.fwhi);
598 hdr.fwlo = HOST_TO_LE32(fw_header.fwlo);
599 hdr.flags = HOST_TO_LE32(fw_header.flags);
600
601 if (fseek(outfile, 0, SEEK_SET) != 0) {
602 errmsg(1,"fseek failed on output file");
603 return -1;
604 }
605
606 return write_out_data(outfile, (uint8_t *)&hdr, sizeof(hdr), crc);
607 }
608
609
610 int
611 write_out_partitions(FILE *outfile, uint32_t *crc)
612 {
613 struct mylo_partition_table p;
614 struct mylo_partition *p1, *p2;
615 int i;
616
617 if (fw_num_partitions == 0)
618 return 0;
619
620 memset(&p, 0, sizeof(p));
621
622 p.magic = HOST_TO_LE32(MYLO_MAGIC_PARTITIONS);
623 for (i = 0; i < fw_num_partitions; i++) {
624 p1 = &p.partitions[i];
625 p2 = &fw_partitions[i];
626 p1->flags = HOST_TO_LE16(p2->flags);
627 p1->type = HOST_TO_LE16(PARTITION_TYPE_USED);
628 p1->addr = HOST_TO_LE32(p2->addr);
629 p1->size = HOST_TO_LE32(p2->size);
630 p1->param = HOST_TO_LE32(p2->param);
631 }
632
633 return write_out_data(outfile, (uint8_t *)&p, sizeof(p), crc);
634 }
635
636
637 int
638 write_out_blocks(FILE *outfile, uint32_t *crc)
639 {
640 struct mylo_fw_blockdesc desc;
641 struct fw_block *b;
642 uint32_t dlen;
643 int i;
644
645 /*
646 * if at least one partition specified, write out block descriptor
647 * for the partition table
648 */
649 if (fw_num_partitions > 0) {
650 desc.type = HOST_TO_LE32(FW_DESC_TYPE_USED);
651 desc.addr = HOST_TO_LE32(board->part_offset);
652 desc.dlen = HOST_TO_LE32(sizeof(struct mylo_partition_table));
653 desc.blen = HOST_TO_LE32(board->part_size);
654
655 if (write_out_desc(outfile, &desc, crc) != 0)
656 return -1;
657 }
658
659 /*
660 * write out block descriptors for each files
661 */
662 for (i = 0; i < fw_num_blocks; i++) {
663 b = &fw_blocks[i];
664
665 /* detect block size */
666 dlen = b->size;
667 if ((b->flags & BLOCK_FLAG_HAVEHDR) != 0) {
668 dlen += sizeof(struct mylo_partition_header);
669 }
670
671 /* round up to 4 bytes */
672 dlen = ALIGN(dlen, 4);
673
674 /* setup the descriptor */
675 desc.type = HOST_TO_LE32(FW_DESC_TYPE_USED);
676 desc.addr = HOST_TO_LE32(b->addr);
677 desc.dlen = HOST_TO_LE32(dlen);
678 desc.blen = HOST_TO_LE32(b->blocklen);
679
680 if (write_out_desc(outfile, &desc, crc) != 0)
681 return -1;
682 }
683
684 /*
685 * write out the null block descriptor
686 */
687 memset(&desc, 0, sizeof(desc));
688 if (write_out_desc(outfile, &desc, crc) != 0)
689 return -1;
690
691 if (write_out_partitions(outfile, crc) != 0)
692 return -1;
693
694 /*
695 * write out data for each blocks
696 */
697 for (i = 0; i < fw_num_blocks; i++) {
698 b = &fw_blocks[i];
699 if (write_out_file(outfile, b, crc) != 0)
700 return -1;
701 }
702
703 return 0;
704 }
705
706
707 /*
708 * argument parsing
709 */
710 int
711 parse_arg(char *arg, char *buf, char *argv[])
712 {
713 int res = 0;
714 size_t argl;
715 char *tok;
716 char **ap = &buf;
717 int i;
718
719 if ((arg == NULL)) {
720 /* invalid argument string */
721 return -1;
722 }
723
724 argl = strlen(arg);
725 if (argl == 0) {
726 /* no arguments */
727 return res;
728 }
729
730 if (argl >= MAX_ARG_LEN) {
731 /* argument is too long */
732 argl = MAX_ARG_LEN-1;
733 }
734
735 memset(argv, 0, MAX_ARG_COUNT * sizeof(void *));
736 memcpy(buf, arg, argl);
737 buf[argl] = '\0';
738
739 for (i = 0; i < MAX_ARG_COUNT; i++) {
740 tok = strsep(ap, ":");
741 if (tok == NULL) {
742 break;
743 }
744 #if 0
745 else if (tok[0] == '\0') {
746 break;
747 }
748 #endif
749 argv[i] = tok;
750 res++;
751 }
752
753 return res;
754 }
755
756
757 int
758 required_arg(char c, char *arg)
759 {
760 if ((optarg != NULL) && (*arg == '-')){
761 errmsg(0,"option %c requires an argument\n", c);
762 return -1;
763 }
764
765 return 0;
766 }
767
768
769 int
770 is_empty_arg(char *arg)
771 {
772 int ret = 1;
773 if (arg != NULL) {
774 if (*arg) ret = 0;
775 };
776 return ret;
777 }
778
779
780 int
781 parse_opt_flags(char ch, char *arg)
782 {
783 if (required_arg(ch, arg)) {
784 goto err_out;
785 }
786
787 if (str2u32(arg, &fw_header.flags) != 0) {
788 errmsg(0,"invalid firmware flags: %s", arg);
789 goto err_out;
790 }
791
792 dbgmsg(1, "firmware flags set to %X bytes", fw_header.flags);
793
794 return 0;
795
796 err_out:
797 return -1;
798 }
799
800
801 int
802 parse_opt_size(char ch, char *arg)
803 {
804 if (required_arg(ch, arg)) {
805 goto err_out;
806 }
807
808 if (str2u32(arg, &flash_size) != 0) {
809 errmsg(0,"invalid flash size: %s", arg);
810 goto err_out;
811 }
812
813 dbgmsg(1, "flash size set to %d bytes", flash_size);
814
815 return 0;
816
817 err_out:
818 return -1;
819 }
820
821
822 int
823 parse_opt_id(char ch, char *arg)
824 {
825 char buf[MAX_ARG_LEN];
826 char *argv[MAX_ARG_COUNT];
827 int argc;
828 char *p;
829
830 if (required_arg(ch, arg)) {
831 goto err_out;
832 }
833
834 argc = parse_arg(arg, buf, argv);
835
836 /* processing vendor ID*/
837 p = argv[0];
838 if (is_empty_arg(p)) {
839 errmsg(0,"vendor id is missing from -%c %s",ch, arg);
840 goto err_out;
841 } else if (str2u16(p, &fw_header.vid) != 0) {
842 errmsg(0,"invalid vendor id: %s", p);
843 goto err_out;
844 }
845
846 dbgmsg(1, "vendor id is set to 0x%04X", fw_header.vid);
847
848 /* processing device ID*/
849 p = argv[1];
850 if (is_empty_arg(p)) {
851 errmsg(0,"device id is missing from -%c %s",ch, arg);
852 goto err_out;
853 } else if (str2u16(p, &fw_header.did) != 0) {
854 errmsg(0,"invalid device id: %s", p);
855 goto err_out;
856 }
857
858 dbgmsg(1, "device id is set to 0x%04X", fw_header.did);
859
860 /* processing sub vendor ID*/
861 p = argv[2];
862 if (is_empty_arg(p)) {
863 fw_header.svid = fw_header.vid;
864 } else if (str2u16(p, &fw_header.svid) != 0) {
865 errmsg(0,"invalid sub vendor id: %s", p);
866 goto err_out;
867 }
868
869 dbgmsg(1, "sub vendor id is set to 0x%04X", fw_header.svid);
870
871 /* processing device ID*/
872 p = argv[3];
873 if (is_empty_arg(p)) {
874 fw_header.sdid = fw_header.did;
875 } else if (str2u16(p, &fw_header.sdid) != 0) {
876 errmsg(0,"invalid sub device id: %s", p);
877 goto err_out;
878 }
879
880 dbgmsg(1, "sub device id is set to 0x%04X", fw_header.sdid);
881
882 /* processing revision */
883 p = argv[4];
884 if (is_empty_arg(p)) {
885 fw_header.rev = 0;
886 } else if (str2u32(arg, &fw_header.rev) != 0) {
887 errmsg(0,"invalid revision number: %s", p);
888 goto err_out;
889 }
890
891 dbgmsg(1, "board revision is set to 0x%08X", fw_header.rev);
892
893 return 0;
894
895 err_out:
896 return -1;
897 }
898
899
900 int
901 parse_opt_block(char ch, char *arg)
902 {
903 char buf[MAX_ARG_LEN];
904 char *argv[MAX_ARG_COUNT];
905 int argc;
906 struct fw_block *b;
907 char *p;
908
909 if (required_arg(ch, arg)) {
910 goto err_out;
911 }
912
913 if (fw_num_blocks >= MAX_FW_BLOCKS) {
914 errmsg(0,"too many blocks specified");
915 goto err_out;
916 }
917
918 argc = parse_arg(arg, buf, argv);
919 dbgmsg(1,"processing block option %s, count %d", arg, argc);
920
921 b = &fw_blocks[fw_num_blocks++];
922
923 /* processing block address */
924 p = argv[0];
925 if (is_empty_arg(p)) {
926 errmsg(0,"no block address specified in %s", arg);
927 goto err_out;
928 } else if (str2u32(p, &b->addr) != 0) {
929 errmsg(0,"invalid block address: %s", p);
930 goto err_out;
931 }
932
933 /* processing block length */
934 p = argv[1];
935 if (is_empty_arg(p)) {
936 errmsg(0,"no block length specified in %s", arg);
937 goto err_out;
938 } else if (str2u32(p, &b->blocklen) != 0) {
939 errmsg(0,"invalid block length: %s", p);
940 goto err_out;
941 }
942
943 if (argc < 3) {
944 dbgmsg(1,"empty block %s", arg);
945 goto success;
946 }
947
948 /* processing flags */
949 p = argv[2];
950 if (is_empty_arg(p) == 0) {
951 for ( ; *p != '\0'; p++) {
952 switch (*p) {
953 case 'h':
954 b->flags |= BLOCK_FLAG_HAVEHDR;
955 break;
956 default:
957 errmsg(0, "invalid block flag \"%c\"", *p);
958 goto err_out;
959 }
960 }
961 }
962
963 /* processing file name */
964 p = argv[3];
965 if (is_empty_arg(p)) {
966 errmsg(0,"file name missing in %s", arg);
967 goto err_out;
968 }
969
970 b->name = strdup(p);
971 if (b->name == NULL) {
972 errmsg(0,"not enough memory");
973 goto err_out;
974 }
975
976 success:
977
978 return 0;
979
980 err_out:
981 return -1;
982 }
983
984
985 int
986 parse_opt_partition(char ch, char *arg)
987 {
988 char buf[MAX_ARG_LEN];
989 char *argv[MAX_ARG_COUNT];
990 int argc;
991 char *p;
992
993 struct mylo_partition *part;
994
995 if (required_arg(ch, arg)) {
996 goto err_out;
997 }
998
999 if (fw_num_partitions >= MYLO_MAX_PARTITIONS) {
1000 errmsg(0, "too many partitions specified");
1001 goto err_out;
1002 }
1003
1004 part = &fw_partitions[fw_num_partitions++];
1005
1006 argc = parse_arg(arg, buf, argv);
1007
1008 /* processing partition address */
1009 p = argv[0];
1010 if (is_empty_arg(p)) {
1011 errmsg(0,"partition address missing in -%c %s",ch, arg);
1012 goto err_out;
1013 } else if (str2u32(p, &part->addr) != 0) {
1014 errmsg(0,"invalid partition address: %s", p);
1015 goto err_out;
1016 }
1017
1018 /* processing partition size */
1019 p = argv[1];
1020 if (is_empty_arg(p)) {
1021 errmsg(0,"partition size missing in -%c %s",ch, arg);
1022 goto err_out;
1023 } else if (str2u32(p, &part->size) != 0) {
1024 errmsg(0,"invalid partition size: %s", p);
1025 goto err_out;
1026 }
1027
1028 /* processing partition flags */
1029 p = argv[2];
1030 if (is_empty_arg(p) == 0) {
1031 for ( ; *p != '\0'; p++) {
1032 switch (*p) {
1033 case 'a':
1034 part->flags |= PARTITION_FLAG_ACTIVE;
1035 break;
1036 case 'p':
1037 part->flags |= PARTITION_FLAG_PRELOAD;
1038 break;
1039 case 'l':
1040 part->flags |= PARTITION_FLAG_LZMA;
1041 break;
1042 case 'h':
1043 part->flags |= PARTITION_FLAG_HAVEHDR;
1044 break;
1045 default:
1046 errmsg(0, "invalid partition flag \"%c\"", *p);
1047 goto err_out;
1048 }
1049 }
1050 }
1051
1052 /* processing partition parameter */
1053 p = argv[3];
1054 if (is_empty_arg(p)) {
1055 /* set default partition parameter */
1056 part->param = 0;
1057 } else if (str2u32(p, &part->param) != 0) {
1058 errmsg(0,"invalid partition parameter: %s", p);
1059 goto err_out;
1060 }
1061
1062 #if 1
1063 if (part->size == 0) {
1064 part->size = flash_size - part->addr;
1065 }
1066
1067 /* processing file parameter */
1068 p = argv[4];
1069 if (is_empty_arg(p) == 0) {
1070 struct fw_block *b;
1071
1072 if (fw_num_blocks == MAX_FW_BLOCKS) {
1073 errmsg(0,"too many blocks specified", p);
1074 goto err_out;
1075 }
1076 b = &fw_blocks[fw_num_blocks++];
1077 b->name = strdup(p);
1078 b->addr = part->addr;
1079 b->blocklen = part->size;
1080 if (part->flags & PARTITION_FLAG_HAVEHDR) {
1081 b->flags |= BLOCK_FLAG_HAVEHDR;
1082 }
1083 }
1084 #endif
1085
1086 return 0;
1087
1088 err_out:
1089 return -1;
1090 }
1091
1092
1093 int
1094 parse_opt_board(char ch, char *arg)
1095 {
1096 if (required_arg(ch, arg)) {
1097 goto err_out;
1098 }
1099
1100 board = find_board(arg);
1101 if (board == NULL){
1102 errmsg(0,"invalid/unknown board specified: %s", arg);
1103 goto err_out;
1104 }
1105
1106 fw_header.vid = board->vid;
1107 fw_header.did = board->did;
1108 fw_header.svid = board->svid;
1109 fw_header.sdid = board->sdid;
1110
1111 flash_size = board->flash_size;
1112
1113 return 0;
1114
1115 err_out:
1116 return -1;
1117 }
1118
1119
1120 int
1121 parse_opt_rev(char ch, char *arg)
1122 {
1123 if (required_arg(ch, arg)) {
1124 return -1;
1125 }
1126
1127 if (str2u32(arg, &fw_header.rev) != 0) {
1128 errmsg(0,"invalid revision number: %s", arg);
1129 return -1;
1130 }
1131
1132 return 0;
1133 }
1134
1135
1136 /*
1137 * main
1138 */
1139 int
1140 main(int argc, char *argv[])
1141 {
1142 int optinvalid = 0; /* flag for invalid option */
1143 int c;
1144 int res = EXIT_FAILURE;
1145
1146 FILE *outfile;
1147 uint32_t crc;
1148
1149 progname=basename(argv[0]);
1150
1151 memset(&fw_header, 0, sizeof(fw_header));
1152
1153 /* init header defaults */
1154 fw_header.vid = VENID_COMPEX;
1155 fw_header.did = DEVID_COMPEX_WP54G;
1156 fw_header.svid = VENID_COMPEX;
1157 fw_header.sdid = DEVID_COMPEX_WP54G;
1158 fw_header.fwhi = 0x20000;
1159 fw_header.fwlo = 0x20000;
1160 fw_header.flags = 0;
1161
1162 opterr = 0; /* could not print standard getopt error messages */
1163 while ((c = getopt(argc, argv, "b:B:f:hi:p:r:s:v")) != -1) {
1164 optinvalid = 0;
1165 switch (c) {
1166 case 'b':
1167 optinvalid = parse_opt_block(c,optarg);
1168 break;
1169 case 'B':
1170 optinvalid = parse_opt_board(c,optarg);
1171 break;
1172 case 'f':
1173 optinvalid = parse_opt_flags(c,optarg);
1174 break;
1175 case 'h':
1176 usage(EXIT_SUCCESS);
1177 break;
1178 case 'i':
1179 optinvalid = parse_opt_id(c,optarg);
1180 break;
1181 case 'p':
1182 optinvalid = parse_opt_partition(c,optarg);
1183 break;
1184 case 'r':
1185 optinvalid = parse_opt_rev(c,optarg);
1186 break;
1187 case 's':
1188 optinvalid = parse_opt_size(c,optarg);
1189 break;
1190 case 'v':
1191 verblevel++;
1192 break;
1193 default:
1194 optinvalid = 1;
1195 break;
1196 }
1197 if (optinvalid != 0 ){
1198 errmsg(0, "invalid option: -%c", optopt);
1199 goto out;
1200 }
1201 }
1202
1203 if (optind == argc) {
1204 errmsg(0, "no output file specified");
1205 goto out;
1206 }
1207
1208 ofname = argv[optind++];
1209
1210 if (optind < argc) {
1211 errmsg(0, "invalid option: %s", argv[optind]);
1212 goto out;
1213 }
1214
1215 if (!board) {
1216 errmsg(0, "no board specified");
1217 goto out;
1218 }
1219
1220 if (flash_size == 0) {
1221 errmsg(0, "no flash size specified");
1222 goto out;
1223 }
1224
1225 if (process_files() != 0) {
1226 goto out;
1227 }
1228
1229 if (process_partitions() != 0) {
1230 goto out;
1231 }
1232
1233 outfile = fopen(ofname, "w");
1234 if (outfile == NULL) {
1235 errmsg(1, "could not open \"%s\" for writing", ofname);
1236 goto out;
1237 }
1238
1239 crc = 0;
1240 init_crc_table();
1241
1242 if (write_out_header(outfile, &crc) != 0)
1243 goto out_flush;
1244
1245 if (write_out_blocks(outfile, &crc) != 0)
1246 goto out_flush;
1247
1248 fw_header.crc = crc;
1249 if (write_out_header(outfile, NULL) != 0)
1250 goto out_flush;
1251
1252 dbgmsg(1,"Firmware file %s completed.", ofname);
1253
1254 res = EXIT_SUCCESS;
1255
1256 out_flush:
1257 fflush(outfile);
1258 fclose(outfile);
1259 if (res != EXIT_SUCCESS) {
1260 unlink(ofname);
1261 }
1262 out:
1263 return res;
1264 }