2 * Copyright (C) 2006-2008 Gabor Juhos <juhosg@openwrt.org>
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.
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.
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.
25 #include <unistd.h> /* for unlink() */
27 #include <getopt.h> /* for getopt() */
31 #include <endian.h> /* for __BYTE_ORDER */
33 #if defined(__CYGWIN__)
34 # include <byteswap.h>
37 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
38 # define HOST_TO_LE16(x) (x)
39 # define HOST_TO_LE32(x) (x)
41 # define HOST_TO_LE16(x) bswap_16(x)
42 # define HOST_TO_LE32(x) bswap_32(x)
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)
54 uint32_t blocklen; /* length of the block */
57 char *name; /* name of the file */
58 uint32_t size; /* length of the file */
59 uint32_t crc; /* crc value of the file */
62 #define BLOCK_FLAG_HAVEHDR 0x0001
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 */
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 }
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)
86 #define CPX_BOARD_ADM(_did, _flash, _mod, _name, _desc) \
87 CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x10000, 0x10000)
89 #define CPX_BOARD_AR71XX(_did, _flash, _mod, _name, _desc) \
90 CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x20000, 0x8000)
92 #define CPX_BOARD_AR23XX(_did, _flash, _mod, _name, _desc) \
93 CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x10000, 0x10000)
95 #define ALIGN(x,y) ((x)+((y)-1)) & ~((y)-1)
100 uint32_t flash_size = 0;
101 int fw_num_partitions = 0;
102 int fw_num_blocks = 0;
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;
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)",
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"),
148 CPX_BOARD_AR71XX(DEVID_COMPEX_WP543, 2,
149 "WP543", "Compex WP543",
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"),
162 errmsgv(int syserr, const char *fmt, va_list arg_ptr)
167 fprintf(stderr, "[%s] Error: ", progname);
168 vfprintf(stderr, fmt, arg_ptr);
170 fprintf(stderr, ": %s", strerror(save));
172 fprintf(stderr, "\n");
176 errmsg(int syserr, const char *fmt, ...)
179 va_start(arg_ptr, fmt);
180 errmsgv(syserr, fmt, arg_ptr);
185 dbgmsg(int level, const char *fmt, ...)
188 if (verblevel >= level) {
190 va_start(arg_ptr, fmt);
191 vfprintf(stderr, fmt, arg_ptr);
192 fprintf(stderr, "\n");
201 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
202 struct cpx_board *board;
204 fprintf(stream, "Usage: %s [OPTION...] <file>\n", progname);
207 " <file> write output to the <file>\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++){
217 board->model, board->name);
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"
248 * Code to compute the CRC-32 table. Borrowed from
249 * gzip-1.0.3/makecrc.c.
252 static uint32_t crc_32_tab[256];
257 /* Not copyrighted 1990 Mark Adler */
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 */
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};
267 /* Make exclusive-or pattern from polynomial */
269 for (i = 0; i < sizeof(p)/sizeof(int); i++)
270 e |= 1L << (31 - p[i]);
274 for (i = 1; i < 256; i++) {
276 for (k = i | 256; k != 1; k >>= 1) {
277 c = c & 1 ? (c >> 1) ^ e : c >> 1;
287 update_crc(uint8_t *p, uint32_t len, uint32_t *crc)
291 t = *crc ^ 0xFFFFFFFFUL;
293 t = crc_32_tab[(t ^ *p++) & 0xff] ^ (t >> 8);
295 *crc = t ^ 0xFFFFFFFFUL;
300 get_crc(uint8_t *p, uint32_t len)
305 update_crc(p ,len , &crc);
311 str2u32(char *arg, uint32_t *val)
317 t = strtoul(arg, &err, 0);
318 if (errno || (err==arg) || ((err != NULL) && *err)) {
328 str2u16(char *arg, uint16_t *val)
334 t = strtoul(arg, &err, 0);
335 if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
345 find_board(char *model){
346 struct cpx_board *board;
347 struct cpx_board *tmp;
350 for (tmp = boards; tmp->model != NULL; tmp++){
351 if (strcasecmp(model, tmp->model) == 0) {
362 get_file_crc(struct fw_block *ff)
365 uint8_t buf[FILE_BUF_LEN];
366 uint32_t readlen = sizeof(buf);
370 if ((ff->flags & BLOCK_FLAG_HAVEHDR) == 0) {
376 f = fopen(ff->name,"r");
378 errmsg(1,"unable to open file %s", ff->name);
389 fread(buf, readlen, 1, f);
391 errmsg(1,"unable to read from file %s", ff->name);
395 update_crc(buf, readlen, &ff->crc);
415 for (i = 0; i < fw_num_blocks; i++) {
417 if ((b->addr + b->blocklen) > flash_size) {
418 errmsg(0, "block at 0x%08X is too big", b->addr);
424 if (stat(b->name, &st) < 0) {
425 errmsg(0, "stat failed on %s",b->name);
428 if (b->blocklen == 0) {
429 b->blocklen = flash_size - b->addr;
431 if (st.st_size > b->blocklen) {
432 errmsg(0,"file %s is too big",b->name);
436 b->size = st.st_size;
444 process_partitions(void)
446 struct mylo_partition *part;
449 for (i = 0; i < fw_num_partitions; i++) {
450 part = &fw_partitions[i];
452 if (part->addr > flash_size) {
453 errmsg(0, "invalid partition at 0x%08X", part->addr);
457 if ((part->addr + part->size) > flash_size) {
458 errmsg(0, "partition at 0x%08X is too big", part->addr);
468 * routines to write data to the output file
471 write_out_data(FILE *outfile, uint8_t *data, size_t len, uint32_t *crc)
475 fwrite(data, len, 1, outfile);
477 errmsg(1,"unable to write output file");
482 update_crc(data, len, crc);
490 write_out_desc(FILE *outfile, struct mylo_fw_blockdesc *desc, uint32_t *crc)
492 return write_out_data(outfile, (uint8_t *)desc,
498 write_out_padding(FILE *outfile, size_t len, uint8_t padc, uint32_t *crc)
501 size_t buflen = sizeof(buff);
503 memset(buff, padc, buflen);
509 if (write_out_data(outfile, buff, buflen, crc))
520 write_out_file(FILE *outfile, struct fw_block *block, uint32_t *crc)
522 char buff[FILE_BUF_LEN];
523 size_t buflen = sizeof(buff);
529 if (block->name == NULL) {
533 if ((block->flags & BLOCK_FLAG_HAVEHDR) != 0) {
534 struct mylo_partition_header ph;
536 if (get_file_crc(block) != 0)
539 ph.crc = HOST_TO_LE32(block->crc);
540 ph.len = HOST_TO_LE32(block->size);
542 if (write_out_data(outfile, (uint8_t *)&ph, sizeof(ph), crc) != 0)
546 f = fopen(block->name,"r");
548 errmsg(1,"unable to open file: %s", block->name);
557 /* read data from source file */
559 fread(buff, buflen, 1, f);
561 errmsg(1,"unable to read from file: %s",block->name);
565 if (write_out_data(outfile, buff, buflen, crc) != 0)
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))
578 dbgmsg(1,"file %s written out", block->name);
584 write_out_header(FILE *outfile, uint32_t *crc)
586 struct mylo_fw_header hdr;
588 memset(&hdr, 0, sizeof(hdr));
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);
601 if (fseek(outfile, 0, SEEK_SET) != 0) {
602 errmsg(1,"fseek failed on output file");
606 return write_out_data(outfile, (uint8_t *)&hdr, sizeof(hdr), crc);
611 write_out_partitions(FILE *outfile, uint32_t *crc)
613 struct mylo_partition_table p;
614 struct mylo_partition *p1, *p2;
617 if (fw_num_partitions == 0)
620 memset(&p, 0, sizeof(p));
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);
633 return write_out_data(outfile, (uint8_t *)&p, sizeof(p), crc);
638 write_out_blocks(FILE *outfile, uint32_t *crc)
640 struct mylo_fw_blockdesc desc;
646 * if at least one partition specified, write out block descriptor
647 * for the partition table
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);
655 if (write_out_desc(outfile, &desc, crc) != 0)
660 * write out block descriptors for each files
662 for (i = 0; i < fw_num_blocks; i++) {
665 /* detect block size */
667 if ((b->flags & BLOCK_FLAG_HAVEHDR) != 0) {
668 dlen += sizeof(struct mylo_partition_header);
671 /* round up to 4 bytes */
672 dlen = ALIGN(dlen, 4);
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);
680 if (write_out_desc(outfile, &desc, crc) != 0)
685 * write out the null block descriptor
687 memset(&desc, 0, sizeof(desc));
688 if (write_out_desc(outfile, &desc, crc) != 0)
691 if (write_out_partitions(outfile, crc) != 0)
695 * write out data for each blocks
697 for (i = 0; i < fw_num_blocks; i++) {
699 if (write_out_file(outfile, b, crc) != 0)
711 parse_arg(char *arg, char *buf, char *argv[])
720 /* invalid argument string */
730 if (argl >= MAX_ARG_LEN) {
731 /* argument is too long */
732 argl = MAX_ARG_LEN-1;
735 memset(argv, 0, MAX_ARG_COUNT * sizeof(void *));
736 memcpy(buf, arg, argl);
739 for (i = 0; i < MAX_ARG_COUNT; i++) {
740 tok = strsep(ap, ":");
745 else if (tok[0] == '\0') {
758 required_arg(char c, char *arg)
760 if ((optarg != NULL) && (*arg == '-')){
761 errmsg(0,"option %c requires an argument\n", c);
770 is_empty_arg(char *arg)
781 parse_opt_flags(char ch, char *arg)
783 if (required_arg(ch, arg)) {
787 if (str2u32(arg, &fw_header.flags) != 0) {
788 errmsg(0,"invalid firmware flags: %s", arg);
792 dbgmsg(1, "firmware flags set to %X bytes", fw_header.flags);
802 parse_opt_size(char ch, char *arg)
804 if (required_arg(ch, arg)) {
808 if (str2u32(arg, &flash_size) != 0) {
809 errmsg(0,"invalid flash size: %s", arg);
813 dbgmsg(1, "flash size set to %d bytes", flash_size);
823 parse_opt_id(char ch, char *arg)
825 char buf[MAX_ARG_LEN];
826 char *argv[MAX_ARG_COUNT];
830 if (required_arg(ch, arg)) {
834 argc = parse_arg(arg, buf, argv);
836 /* processing vendor ID*/
838 if (is_empty_arg(p)) {
839 errmsg(0,"vendor id is missing from -%c %s",ch, arg);
841 } else if (str2u16(p, &fw_header.vid) != 0) {
842 errmsg(0,"invalid vendor id: %s", p);
846 dbgmsg(1, "vendor id is set to 0x%04X", fw_header.vid);
848 /* processing device ID*/
850 if (is_empty_arg(p)) {
851 errmsg(0,"device id is missing from -%c %s",ch, arg);
853 } else if (str2u16(p, &fw_header.did) != 0) {
854 errmsg(0,"invalid device id: %s", p);
858 dbgmsg(1, "device id is set to 0x%04X", fw_header.did);
860 /* processing sub vendor ID*/
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);
869 dbgmsg(1, "sub vendor id is set to 0x%04X", fw_header.svid);
871 /* processing device ID*/
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);
880 dbgmsg(1, "sub device id is set to 0x%04X", fw_header.sdid);
882 /* processing revision */
884 if (is_empty_arg(p)) {
886 } else if (str2u32(arg, &fw_header.rev) != 0) {
887 errmsg(0,"invalid revision number: %s", p);
891 dbgmsg(1, "board revision is set to 0x%08X", fw_header.rev);
901 parse_opt_block(char ch, char *arg)
903 char buf[MAX_ARG_LEN];
904 char *argv[MAX_ARG_COUNT];
909 if (required_arg(ch, arg)) {
913 if (fw_num_blocks >= MAX_FW_BLOCKS) {
914 errmsg(0,"too many blocks specified");
918 argc = parse_arg(arg, buf, argv);
919 dbgmsg(1,"processing block option %s, count %d", arg, argc);
921 b = &fw_blocks[fw_num_blocks++];
923 /* processing block address */
925 if (is_empty_arg(p)) {
926 errmsg(0,"no block address specified in %s", arg);
928 } else if (str2u32(p, &b->addr) != 0) {
929 errmsg(0,"invalid block address: %s", p);
933 /* processing block length */
935 if (is_empty_arg(p)) {
936 errmsg(0,"no block length specified in %s", arg);
938 } else if (str2u32(p, &b->blocklen) != 0) {
939 errmsg(0,"invalid block length: %s", p);
944 dbgmsg(1,"empty block %s", arg);
948 /* processing flags */
950 if (is_empty_arg(p) == 0) {
951 for ( ; *p != '\0'; p++) {
954 b->flags |= BLOCK_FLAG_HAVEHDR;
957 errmsg(0, "invalid block flag \"%c\"", *p);
963 /* processing file name */
965 if (is_empty_arg(p)) {
966 errmsg(0,"file name missing in %s", arg);
971 if (b->name == NULL) {
972 errmsg(0,"not enough memory");
986 parse_opt_partition(char ch, char *arg)
988 char buf[MAX_ARG_LEN];
989 char *argv[MAX_ARG_COUNT];
993 struct mylo_partition *part;
995 if (required_arg(ch, arg)) {
999 if (fw_num_partitions >= MYLO_MAX_PARTITIONS) {
1000 errmsg(0, "too many partitions specified");
1004 part = &fw_partitions[fw_num_partitions++];
1006 argc = parse_arg(arg, buf, argv);
1008 /* processing partition address */
1010 if (is_empty_arg(p)) {
1011 errmsg(0,"partition address missing in -%c %s",ch, arg);
1013 } else if (str2u32(p, &part->addr) != 0) {
1014 errmsg(0,"invalid partition address: %s", p);
1018 /* processing partition size */
1020 if (is_empty_arg(p)) {
1021 errmsg(0,"partition size missing in -%c %s",ch, arg);
1023 } else if (str2u32(p, &part->size) != 0) {
1024 errmsg(0,"invalid partition size: %s", p);
1028 /* processing partition flags */
1030 if (is_empty_arg(p) == 0) {
1031 for ( ; *p != '\0'; p++) {
1034 part->flags |= PARTITION_FLAG_ACTIVE;
1037 part->flags |= PARTITION_FLAG_PRELOAD;
1040 part->flags |= PARTITION_FLAG_LZMA;
1043 part->flags |= PARTITION_FLAG_HAVEHDR;
1046 errmsg(0, "invalid partition flag \"%c\"", *p);
1052 /* processing partition parameter */
1054 if (is_empty_arg(p)) {
1055 /* set default partition parameter */
1057 } else if (str2u32(p, &part->param) != 0) {
1058 errmsg(0,"invalid partition parameter: %s", p);
1063 if (part->size == 0) {
1064 part->size = flash_size - part->addr;
1067 /* processing file parameter */
1069 if (is_empty_arg(p) == 0) {
1072 if (fw_num_blocks == MAX_FW_BLOCKS) {
1073 errmsg(0,"too many blocks specified", p);
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;
1094 parse_opt_board(char ch, char *arg)
1096 if (required_arg(ch, arg)) {
1100 board = find_board(arg);
1102 errmsg(0,"invalid/unknown board specified: %s", arg);
1106 fw_header.vid = board->vid;
1107 fw_header.did = board->did;
1108 fw_header.svid = board->svid;
1109 fw_header.sdid = board->sdid;
1111 flash_size = board->flash_size;
1121 parse_opt_rev(char ch, char *arg)
1123 if (required_arg(ch, arg)) {
1127 if (str2u32(arg, &fw_header.rev) != 0) {
1128 errmsg(0,"invalid revision number: %s", arg);
1140 main(int argc, char *argv[])
1142 int optinvalid = 0; /* flag for invalid option */
1144 int res = EXIT_FAILURE;
1149 progname=basename(argv[0]);
1151 memset(&fw_header, 0, sizeof(fw_header));
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;
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) {
1167 optinvalid = parse_opt_block(c,optarg);
1170 optinvalid = parse_opt_board(c,optarg);
1173 optinvalid = parse_opt_flags(c,optarg);
1176 usage(EXIT_SUCCESS);
1179 optinvalid = parse_opt_id(c,optarg);
1182 optinvalid = parse_opt_partition(c,optarg);
1185 optinvalid = parse_opt_rev(c,optarg);
1188 optinvalid = parse_opt_size(c,optarg);
1197 if (optinvalid != 0 ){
1198 errmsg(0, "invalid option: -%c", optopt);
1203 if (optind == argc) {
1204 errmsg(0, "no output file specified");
1208 ofname = argv[optind++];
1210 if (optind < argc) {
1211 errmsg(0, "invalid option: %s", argv[optind]);
1216 errmsg(0, "no board specified");
1220 if (flash_size == 0) {
1221 errmsg(0, "no flash size specified");
1225 if (process_files() != 0) {
1229 if (process_partitions() != 0) {
1233 outfile = fopen(ofname, "w");
1234 if (outfile == NULL) {
1235 errmsg(1, "could not open \"%s\" for writing", ofname);
1242 if (write_out_header(outfile, &crc) != 0)
1245 if (write_out_blocks(outfile, &crc) != 0)
1248 fw_header.crc = crc;
1249 if (write_out_header(outfile, NULL) != 0)
1252 dbgmsg(1,"Firmware file %s completed.", ofname);
1259 if (res != EXIT_SUCCESS) {