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