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