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