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