ramips: fix USW-Flex reversed switch-port order
[openwrt/staging/mkresin.git] / tools / firmware-utils / src / mkcsysimg.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 *
4 * Copyright (C) 2007-2009 Gabor Juhos <juhosg@openwrt.org>
5 *
6 * This program was based on the code found in various Linux
7 * source tarballs released by Edimax for it's devices.
8 * Original author: David Hsu <davidhsu@realtek.com.tw>
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stdint.h>
14 #include <string.h>
15 #include <unistd.h> /* for unlink() */
16 #include <libgen.h>
17 #include <getopt.h> /* for getopt() */
18 #include <stdarg.h>
19 #include <errno.h>
20 #include <sys/stat.h>
21 #include <endian.h> /* for __BYTE_ORDER */
22 #if defined(__CYGWIN__)
23 # include <byteswap.h>
24 #endif
25
26 #include "csysimg.h"
27
28 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
29 # define HOST_TO_LE16(x) (x)
30 # define HOST_TO_LE32(x) (x)
31 # define LE16_TO_HOST(x) (x)
32 # define LE32_TO_HOST(x) (x)
33 #else
34 # define HOST_TO_LE16(x) bswap_16(x)
35 # define HOST_TO_LE32(x) bswap_32(x)
36 # define LE16_TO_HOST(x) bswap_16(x)
37 # define LE32_TO_HOST(x) bswap_32(x)
38 #endif
39
40 #define MAX_NUM_BLOCKS 8
41 #define MAX_ARG_COUNT 32
42 #define MAX_ARG_LEN 1024
43 #define FILE_BUF_LEN (16*1024)
44 #define CSYS_PADC 0xFF
45
46 #define BLOCK_TYPE_BOOT 0
47 #define BLOCK_TYPE_CONF 1
48 #define BLOCK_TYPE_WEBP 2
49 #define BLOCK_TYPE_CODE 3
50 #define BLOCK_TYPE_XTRA 4
51
52 #define DEFAULT_BLOCK_ALIGN 0x10000U
53
54 #define CSUM_SIZE_NONE 0
55 #define CSUM_SIZE_8 1
56 #define CSUM_SIZE_16 2
57
58
59 struct csum_state{
60 int size;
61 uint16_t val;
62 uint16_t tmp;
63 int odd;
64 };
65
66
67 struct csys_block {
68 int type; /* type of the block */
69
70 int need_file;
71 char *file_name; /* name of the file */
72 uint32_t file_size; /* length of the file */
73
74 unsigned char sig[SIG_LEN];
75 uint32_t addr;
76 int addr_set;
77 uint32_t align;
78 int align_set;
79 uint8_t padc;
80
81 uint32_t size;
82 uint32_t size_hdr;
83 uint32_t size_csum;
84 uint32_t size_avail;
85
86 struct csum_state *css;
87 };
88
89
90 struct board_info {
91 char *model;
92 char *name;
93 uint32_t flash_size;
94
95 char sig_boot[SIG_LEN];
96 char sig_conf[SIG_LEN];
97 char sig_webp[SIG_LEN];
98
99 uint32_t boot_size;
100 uint32_t conf_size;
101 uint32_t webp_size;
102 uint32_t webp_size_max;
103 uint32_t code_size;
104
105 uint32_t addr_code;
106 uint32_t addr_webp;
107 };
108
109 #define BOARD(m, n, f, sigb, sigw, bs, cs, ws, ac, aw) {\
110 .model = m, .name = n, .flash_size = f<<20, \
111 .sig_boot = sigb, .sig_conf = SIG_CONF, .sig_webp = sigw, \
112 .boot_size = bs, .conf_size = cs, \
113 .webp_size = ws, .webp_size_max = 3*0x10000, \
114 .addr_code = ac, .addr_webp = aw \
115 }
116
117 #define BOARD_ADM(m,n,f, sigw) BOARD(m,n,f, ADM_BOOT_SIG, sigw, \
118 ADM_BOOT_SIZE, ADM_CONF_SIZE, ADM_WEBP_SIZE, \
119 ADM_CODE_ADDR, ADM_WEBP_ADDR)
120
121
122 /*
123 * Globals
124 */
125 char *progname;
126 char *ofname = NULL;
127 int verblevel = 0;
128 int invalid_causes_error = 1;
129 int keep_invalid_images = 0;
130
131 struct board_info *board = NULL;
132
133 struct csys_block *boot_block = NULL;
134 struct csys_block *conf_block = NULL;
135 struct csys_block *webp_block = NULL;
136 struct csys_block *code_block = NULL;
137
138 struct csys_block blocks[MAX_NUM_BLOCKS];
139 int num_blocks = 0;
140
141 static struct board_info boards[] = {
142 /* The original Edimax products */
143 BOARD_ADM("BR-6104K", "Edimax BR-6104K", 2, SIG_BR6104K),
144 BOARD_ADM("BR-6104KP", "Edimax BR-6104KP", 2, SIG_BR6104KP),
145 BOARD_ADM("BR-6104Wg", "Edimax BR-6104Wg", 2, SIG_BR6104Wg),
146 BOARD_ADM("BR-6114WG", "Edimax BR-6114WG", 2, SIG_BR6114WG),
147 BOARD_ADM("BR-6524K", "Edimax BR-6524K", 2, SIG_BR6524K),
148 BOARD_ADM("BR-6524KP", "Edimax BR-6524KP", 2, SIG_BR6524KP),
149 BOARD_ADM("BR-6524N", "Edimax BR-6524N", 2, SIG_BR6524N),
150 BOARD_ADM("BR-6524WG", "Edimax BR-6524WG", 4, SIG_BR6524WG),
151 BOARD_ADM("BR-6524WP", "Edimax BR-6524WP", 4, SIG_BR6524WP),
152 BOARD_ADM("BR-6541K", "Edimax BR-6541K", 2, SIG_BR6541K),
153 BOARD_ADM("BR-6541KP", "Edimax BR-6541K", 2, SIG_BR6541KP),
154 BOARD_ADM("BR-6541WP", "Edimax BR-6541WP", 4, SIG_BR6541WP),
155 BOARD_ADM("EW-7207APg", "Edimax EW-7207APg", 2, SIG_EW7207APg),
156 BOARD_ADM("PS-1205UWg", "Edimax PS-1205UWg", 2, SIG_PS1205UWg),
157 BOARD_ADM("PS-3205U", "Edimax PS-3205U", 2, SIG_PS3205U),
158 BOARD_ADM("PS-3205UWg", "Edimax PS-3205UWg", 2, SIG_PS3205UWg),
159
160 /* Hawking products */
161 BOARD_ADM("H2BR4", "Hawking H2BR4", 2, SIG_H2BR4),
162 BOARD_ADM("H2WR54G", "Hawking H2WR54G", 4, SIG_H2WR54G),
163
164 /* Planet products */
165 BOARD_ADM("XRT-401D", "Planet XRT-401D", 2, SIG_XRT401D),
166 BOARD_ADM("XRT-402D", "Planet XRT-402D", 2, SIG_XRT402D),
167
168 /* Conceptronic products */
169 BOARD_ADM("C54BSR4", "Conceptronic C54BSR4", 2, SIG_C54BSR4),
170
171 /* OSBRiDGE products */
172 BOARD_ADM("5GXi", "OSBDRiDGE 5GXi", 2, SIG_5GXI),
173
174 {.model = NULL}
175 };
176
177 /*
178 * Message macros
179 */
180 #define ERR(fmt, ...) do { \
181 fflush(0); \
182 fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ ); \
183 } while (0)
184
185 #define ERRS(fmt, ...) do { \
186 int save = errno; \
187 fflush(0); \
188 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", progname, ## __VA_ARGS__ \
189 , strerror(save)); \
190 } while (0)
191
192 #define WARN(fmt, ...) do { \
193 fprintf(stderr, "[%s] *** warning: " fmt "\n", progname, ## __VA_ARGS__ ); \
194 } while (0)
195
196 #define DBG(lev, fmt, ...) do { \
197 if (verblevel < lev) \
198 break;\
199 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
200 } while (0)
201
202 #define ERR_FATAL -1
203 #define ERR_INVALID_IMAGE -2
204
205 void
206 usage(int status)
207 {
208 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
209 struct board_info *board;
210
211 fprintf(stream, "Usage: %s [OPTIONS...] <file>\n", progname);
212 fprintf(stream,
213 "\n"
214 "Options:\n"
215 " -B <board> create image for the board specified with <board>.\n"
216 " valid <board> values:\n"
217 );
218 for (board = boards; board->model != NULL; board++){
219 fprintf(stream,
220 " %-12s: %s\n",
221 board->model, board->name);
222 };
223 fprintf(stream,
224 " -d don't throw error on invalid images\n"
225 " -k keep invalid images\n"
226 " -b <file>[:<align>[:<padc>]]\n"
227 " add boot code to the image\n"
228 " -c <file>[:<align>[:<padc>]]\n"
229 " add configuration settings to the image\n"
230 " -r <file>:[<addr>][:<align>[:<padc>]]\n"
231 " add runtime code to the image\n"
232 " -w [<file>:[<addr>][:<align>[:<padc>]]]\n"
233 " add webpages to the image\n"
234 " -x <file>[:<align>[:<padc>]]\n"
235 " add extra data at the end of the image\n"
236 " -h show this screen\n"
237 "Parameters:\n"
238 " <file> write output to the file <file>\n"
239 );
240
241 exit(status);
242 }
243
244 static inline uint32_t align(uint32_t base, uint32_t alignment)
245 {
246 uint32_t ret;
247
248 if (alignment) {
249 ret = (base + alignment - 1);
250 ret &= ~(alignment-1);
251 } else {
252 ret = base;
253 }
254
255 return ret;
256 }
257
258 /*
259 * argument parsing
260 */
261 int
262 str2u32(char *arg, uint32_t *val)
263 {
264 char *err = NULL;
265 uint32_t t;
266
267 errno=0;
268 t = strtoul(arg, &err, 0);
269 if (errno || (err==arg) || ((err != NULL) && *err)) {
270 return -1;
271 }
272
273 *val = t;
274 return 0;
275 }
276
277
278 int
279 str2u16(char *arg, uint16_t *val)
280 {
281 char *err = NULL;
282 uint32_t t;
283
284 errno=0;
285 t = strtoul(arg, &err, 0);
286 if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
287 return -1;
288 }
289
290 *val = t & 0xFFFF;
291 return 0;
292 }
293
294 int
295 str2u8(char *arg, uint8_t *val)
296 {
297 char *err = NULL;
298 uint32_t t;
299
300 errno=0;
301 t = strtoul(arg, &err, 0);
302 if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x100)) {
303 return -1;
304 }
305
306 *val = t & 0xFF;
307 return 0;
308 }
309
310 int
311 str2sig(char *arg, uint32_t *sig)
312 {
313 if (strlen(arg) != 4)
314 return -1;
315
316 *sig = arg[0] | (arg[1] << 8) | (arg[2] << 16) | (arg[3] << 24);
317
318 return 0;
319 }
320
321
322 int
323 parse_arg(char *arg, char *buf, char *argv[])
324 {
325 int res = 0;
326 size_t argl;
327 char *tok;
328 char **ap = &buf;
329 int i;
330
331 memset(argv, 0, MAX_ARG_COUNT * sizeof(void *));
332
333 if ((arg == NULL)) {
334 /* no arguments */
335 return 0;
336 }
337
338 argl = strlen(arg);
339 if (argl == 0) {
340 /* no arguments */
341 return 0;
342 }
343
344 if (argl >= MAX_ARG_LEN) {
345 /* argument is too long */
346 argl = MAX_ARG_LEN-1;
347 }
348
349 memcpy(buf, arg, argl);
350 buf[argl] = '\0';
351
352 for (i = 0; i < MAX_ARG_COUNT; i++) {
353 tok = strsep(ap, ":");
354 if (tok == NULL) {
355 break;
356 }
357 #if 0
358 else if (tok[0] == '\0') {
359 break;
360 }
361 #endif
362 argv[i] = tok;
363 res++;
364 }
365
366 return res;
367 }
368
369
370 int
371 required_arg(char c, char *arg)
372 {
373 if (arg == NULL || *arg != '-')
374 return 0;
375
376 ERR("option -%c requires an argument\n", c);
377 return ERR_FATAL;
378 }
379
380
381 int
382 is_empty_arg(char *arg)
383 {
384 int ret = 1;
385 if (arg != NULL) {
386 if (*arg) ret = 0;
387 };
388 return ret;
389 }
390
391
392 void
393 csum8_update(uint8_t *p, uint32_t len, struct csum_state *css)
394 {
395 for ( ; len > 0; len --) {
396 css->val += *p++;
397 }
398 }
399
400
401 uint16_t
402 csum8_get(struct csum_state *css)
403 {
404 uint8_t t;
405
406 t = css->val;
407 return ~t + 1;
408 }
409
410
411 void
412 csum16_update(void *data, uint32_t len, struct csum_state *css)
413 {
414 uint8_t *p = data;
415 uint16_t t;
416
417 if (css->odd) {
418 t = css->tmp + (p[0]<<8);
419 css->val += LE16_TO_HOST(t);
420 css->odd = 0;
421 len--;
422 p++;
423 }
424
425 for ( ; len > 1; len -= 2, p +=2 ) {
426 t = p[0] + (p[1] << 8);
427 css->val += LE16_TO_HOST(t);
428 }
429
430 if (len == 1) {
431 css->tmp = p[0];
432 css->odd = 1;
433 }
434 }
435
436
437 uint16_t
438 csum16_get(struct csum_state *css)
439 {
440 char pad = 0;
441
442 csum16_update(&pad, 1, css);
443 return ~css->val + 1;
444 }
445
446
447 void
448 csum_init(struct csum_state *css, int size)
449 {
450 css->val = 0;
451 css->tmp = 0;
452 css->odd = 0;
453 css->size = size;
454 }
455
456
457 void
458 csum_update(void *data, uint32_t len, struct csum_state *css)
459 {
460 uint8_t *p = data;
461
462 switch (css->size) {
463 case CSUM_SIZE_8:
464 csum8_update(p,len,css);
465 break;
466 case CSUM_SIZE_16:
467 csum16_update(p,len,css);
468 break;
469 }
470 }
471
472
473 uint16_t
474 csum_get(struct csum_state *css)
475 {
476 uint16_t ret;
477
478 switch (css->size) {
479 case CSUM_SIZE_8:
480 ret = csum8_get(css);
481 break;
482 case CSUM_SIZE_16:
483 ret = csum16_get(css);
484 break;
485 default:
486 ERR("invalid checksum size\n");
487 return 0;
488 }
489
490 return ret;
491 }
492
493
494 /*
495 * routines to write data to the output file
496 */
497 int
498 write_out_data(FILE *outfile, void *data, size_t len,
499 struct csum_state *css)
500 {
501 uint8_t *ptr = data;
502
503 errno = 0;
504
505 fwrite(ptr, len, 1, outfile);
506 if (errno) {
507 ERRS("unable to write output file");
508 return ERR_FATAL;
509 }
510
511 if (css) {
512 csum_update(ptr, len, css);
513 }
514
515 return 0;
516 }
517
518
519 int
520 write_out_padding(FILE *outfile, size_t len, uint8_t padc,
521 struct csum_state *css)
522 {
523 uint8_t buf[512];
524 size_t buflen = sizeof(buf);
525 int err;
526
527 memset(buf, padc, buflen);
528 while (len > 0) {
529 if (len < buflen)
530 buflen = len;
531
532 err = write_out_data(outfile, buf, buflen, css);
533 if (err)
534 return err;
535
536 len -= buflen;
537 }
538
539 return 0;
540 }
541
542
543 int
544 block_stat_file(struct csys_block *block)
545 {
546 struct stat st;
547 int err;
548
549 if (block->file_name == NULL)
550 return 0;
551
552 err = stat(block->file_name, &st);
553 if (err){
554 ERRS("stat failed on %s", block->file_name);
555 return ERR_FATAL;
556 }
557
558 block->file_size = st.st_size;
559 return 0;
560 }
561
562
563 int
564 block_writeout_hdr(FILE *outfile, struct csys_block *block)
565 {
566 struct csys_header hdr;
567 int res;
568
569 if (block->size_hdr == 0)
570 return 0;
571
572 /* setup header fields */
573 memcpy(hdr.sig, block->sig, 4);
574 hdr.addr = HOST_TO_LE32(block->addr);
575 hdr.size = HOST_TO_LE32(block->size - block->size_hdr - block->size_csum);
576
577 DBG(1,"writing header for block");
578 res = write_out_data(outfile, (uint8_t *)&hdr, sizeof(hdr),NULL);
579 return res;
580
581 }
582
583
584 int
585 block_writeout_file(FILE *outfile, struct csys_block *block)
586 {
587 char buf[FILE_BUF_LEN];
588 size_t buflen = sizeof(buf);
589 FILE *f;
590 size_t len;
591 int res;
592
593 if (block->file_name == NULL)
594 return 0;
595
596 if (block->file_size == 0)
597 return 0;
598
599 errno = 0;
600 f = fopen(block->file_name,"r");
601 if (errno) {
602 ERRS("unable to open file: %s", block->file_name);
603 return ERR_FATAL;
604 }
605
606 len = block->file_size;
607 while (len > 0) {
608 if (len < buflen)
609 buflen = len;
610
611 /* read data from source file */
612 errno = 0;
613 fread(buf, buflen, 1, f);
614 if (errno != 0) {
615 ERRS("unable to read from file: %s", block->file_name);
616 res = ERR_FATAL;
617 break;
618 }
619
620 res = write_out_data(outfile, buf, buflen, block->css);
621 if (res)
622 break;
623
624 len -= buflen;
625 }
626
627 fclose(f);
628 return res;
629 }
630
631
632 int
633 block_writeout_data(FILE *outfile, struct csys_block *block)
634 {
635 int res;
636 size_t padlen;
637
638 res = block_writeout_file(outfile, block);
639 if (res)
640 return res;
641
642 /* write padding data if neccesary */
643 padlen = block->size_avail - block->file_size;
644 DBG(1,"padding block, length=%zu", padlen);
645 res = write_out_padding(outfile, padlen, block->padc, block->css);
646
647 return res;
648 }
649
650
651 int
652 block_writeout_csum(FILE *outfile, struct csys_block *block)
653 {
654 uint16_t csum;
655 int res;
656
657 if (block->size_csum == 0)
658 return 0;
659
660 DBG(1,"writing checksum for block");
661 csum = HOST_TO_LE16(csum_get(block->css));
662 res = write_out_data(outfile, (uint8_t *)&csum, block->size_csum, NULL);
663
664 return res;
665 }
666
667
668 int
669 block_writeout(FILE *outfile, struct csys_block *block)
670 {
671 int res;
672 struct csum_state css;
673
674 res = 0;
675
676 if (block == NULL)
677 return res;
678
679 block->css = NULL;
680
681 DBG(2, "writing block, file=%s, file_size=%d, space=%d",
682 block->file_name, block->file_size, block->size_avail);
683 res = block_writeout_hdr(outfile, block);
684 if (res)
685 return res;
686
687 if (block->size_csum != 0) {
688 block->css = &css;
689 csum_init(&css, block->size_csum);
690 }
691
692 res = block_writeout_data(outfile, block);
693 if (res)
694 return res;
695
696 res = block_writeout_csum(outfile, block);
697 if (res)
698 return res;
699
700 return res;
701 }
702
703
704 int
705 write_out_blocks(FILE *outfile)
706 {
707 struct csys_block *block;
708 int i, res;
709
710 res = block_writeout(outfile, boot_block);
711 if (res)
712 return res;
713
714 res = block_writeout(outfile, conf_block);
715 if (res)
716 return res;
717
718 res = block_writeout(outfile, webp_block);
719 if (res)
720 return res;
721
722 res = block_writeout(outfile, code_block);
723 if (res)
724 return res;
725
726 res = 0;
727 for (i=0; i < num_blocks; i++) {
728 block = &blocks[i];
729
730 if (block->type != BLOCK_TYPE_XTRA)
731 continue;
732
733 res = block_writeout(outfile, block);
734 if (res)
735 break;
736 }
737
738 return res;
739 }
740
741
742 struct board_info *
743 find_board(char *model)
744 {
745 struct board_info *ret;
746 struct board_info *board;
747
748 ret = NULL;
749 for (board = boards; board->model != NULL; board++){
750 if (strcasecmp(model, board->model) == 0) {
751 ret = board;
752 break;
753 }
754 };
755
756 return ret;
757 }
758
759
760 int
761 parse_opt_board(char ch, char *arg)
762 {
763
764 DBG(1,"parsing board option: -%c %s", ch, arg);
765
766 if (board != NULL) {
767 ERR("only one board option allowed");
768 return ERR_FATAL;
769 }
770
771 if (required_arg(ch, arg))
772 return ERR_FATAL;
773
774 board = find_board(arg);
775 if (board == NULL){
776 ERR("invalid/unknown board specified: %s", arg);
777 return ERR_FATAL;
778 }
779
780 return 0;
781 }
782
783
784 int
785 parse_opt_block(char ch, char *arg)
786 {
787 char buf[MAX_ARG_LEN];
788 char *argv[MAX_ARG_COUNT];
789 char *p;
790 struct csys_block *block;
791 int i;
792
793 if ( num_blocks > MAX_NUM_BLOCKS ) {
794 ERR("too many blocks specified");
795 return ERR_FATAL;
796 }
797
798 block = &blocks[num_blocks];
799
800 /* setup default field values */
801 block->need_file = 1;
802 block->padc = 0xFF;
803
804 switch (ch) {
805 case 'b':
806 if (boot_block) {
807 WARN("only one boot block allowed");
808 break;
809 }
810 block->type = BLOCK_TYPE_BOOT;
811 boot_block = block;
812 break;
813 case 'c':
814 if (conf_block) {
815 WARN("only one config block allowed");
816 break;
817 }
818 block->type = BLOCK_TYPE_CONF;
819 conf_block = block;
820 break;
821 case 'w':
822 if (webp_block) {
823 WARN("only one web block allowed");
824 break;
825 }
826 block->type = BLOCK_TYPE_WEBP;
827 block->size_hdr = sizeof(struct csys_header);
828 block->size_csum = CSUM_SIZE_8;
829 block->need_file = 0;
830 webp_block = block;
831 break;
832 case 'r':
833 if (code_block) {
834 WARN("only one runtime block allowed");
835 break;
836 }
837 block->type = BLOCK_TYPE_CODE;
838 block->size_hdr = sizeof(struct csys_header);
839 block->size_csum = CSUM_SIZE_16;
840 code_block = block;
841 break;
842 case 'x':
843 block->type = BLOCK_TYPE_XTRA;
844 break;
845 default:
846 ERR("unknown block type \"%c\"", ch);
847 return ERR_FATAL;
848 }
849
850 parse_arg(arg, buf, argv);
851
852 i = 0;
853 p = argv[i++];
854 if (!is_empty_arg(p)) {
855 block->file_name = strdup(p);
856 if (block->file_name == NULL) {
857 ERR("not enough memory");
858 return ERR_FATAL;
859 }
860 } else if (block->need_file){
861 ERR("no file specified in %s", arg);
862 return ERR_FATAL;
863 }
864
865 if (block->size_hdr) {
866 p = argv[i++];
867 if (!is_empty_arg(p)) {
868 if (str2u32(p, &block->addr) != 0) {
869 ERR("invalid start address in %s", arg);
870 return ERR_FATAL;
871 }
872 block->addr_set = 1;
873 }
874 }
875
876 p = argv[i++];
877 if (!is_empty_arg(p)) {
878 if (str2u32(p, &block->align) != 0) {
879 ERR("invalid alignment value in %s", arg);
880 return ERR_FATAL;
881 }
882 block->align_set = 1;
883 }
884
885 p = argv[i++];
886 if (!is_empty_arg(p) && (str2u8(p, &block->padc) != 0)) {
887 ERR("invalid paddig character in %s", arg);
888 return ERR_FATAL;
889 }
890
891 num_blocks++;
892
893 return 0;
894 }
895
896
897 int
898 process_blocks(void)
899 {
900 struct csys_block *block;
901 uint32_t offs = 0;
902 int i;
903 int res;
904
905 res = 0;
906 /* collecting stats */
907 for (i = 0; i < num_blocks; i++) {
908 block = &blocks[i];
909 res = block_stat_file(block);
910 if (res)
911 return res;
912 }
913
914 /* bootloader */
915 block = boot_block;
916 if (block) {
917 block->size = board->boot_size;
918 if (block->file_size > board->boot_size) {
919 WARN("boot block is too big");
920 res = ERR_INVALID_IMAGE;
921 }
922 }
923 offs += board->boot_size;
924
925 /* configuration data */
926 block = conf_block;
927 if (block) {
928 block->size = board->conf_size;
929 if (block->file_size > board->conf_size) {
930 WARN("config block is too big");
931 res = ERR_INVALID_IMAGE;
932 }
933 }
934 offs += board->conf_size;
935
936 /* webpages */
937 block = webp_block;
938 if (block) {
939
940 memcpy(block->sig, board->sig_webp, 4);
941
942 if (block->addr_set == 0)
943 block->addr = board->addr_webp;
944
945 if (block->align_set == 0)
946 block->align = DEFAULT_BLOCK_ALIGN;
947
948 block->size = align(offs + block->file_size + block->size_hdr +
949 block->size_csum, block->align) - offs;
950
951 if (block->size > board->webp_size_max) {
952 WARN("webpages block is too big");
953 res = ERR_INVALID_IMAGE;
954 }
955
956 DBG(2,"webpages start at %08x, size=%08x", offs,
957 block->size);
958
959 offs += block->size;
960 if (offs > board->flash_size) {
961 WARN("webp block is too big");
962 res = ERR_INVALID_IMAGE;
963 }
964 }
965
966 /* runtime code */
967 block = code_block;
968 if (block) {
969 memcpy(code_block->sig, SIG_CSYS, 4);
970
971 if (block->addr_set == 0)
972 block->addr = board->addr_code;
973
974 if (block->align_set == 0)
975 block->align = DEFAULT_BLOCK_ALIGN;
976
977 block->size = align(offs + block->file_size +
978 block->size_hdr + block->size_csum,
979 block->align) - offs;
980
981 DBG(2,"code block start at %08x, size=%08x", offs,
982 block->size);
983
984 offs += block->size;
985 if (offs > board->flash_size) {
986 WARN("code block is too big");
987 res = ERR_INVALID_IMAGE;
988 }
989 }
990
991 for (i = 0; i < num_blocks; i++) {
992 block = &blocks[i];
993
994 if (block->type != BLOCK_TYPE_XTRA)
995 continue;
996
997 if (block->align_set == 0)
998 block->align = DEFAULT_BLOCK_ALIGN;
999
1000 block->size = align(offs + block->file_size,
1001 block->align) - offs;
1002
1003 DBG(2,"file %s start at %08x, size=%08x, align=%08x",
1004 block->file_name, offs, block->size, block->align);
1005
1006 offs += block->size;
1007 if (offs > board->flash_size) {
1008 WARN("file %s is too big, size=%d, avail=%d",
1009 block->file_name, block->file_size,
1010 board->flash_size - offs);
1011 res = ERR_INVALID_IMAGE;
1012 }
1013 }
1014
1015 for (i = 0; i < num_blocks; i++) {
1016 block = &blocks[i];
1017
1018 block->size_avail = block->size - block->size_hdr -
1019 block->size_csum;
1020
1021 if (block->size_avail < block->file_size) {
1022 WARN("file %s is too big, size=%d, avail=%d",
1023 block->file_name, block->file_size,
1024 block->size_avail);
1025 res = ERR_INVALID_IMAGE;
1026 }
1027 }
1028
1029 return res;
1030 }
1031
1032
1033 int
1034 main(int argc, char *argv[])
1035 {
1036 int optinvalid = 0; /* flag for invalid option */
1037 int c;
1038 int res = ERR_FATAL;
1039
1040 FILE *outfile;
1041
1042 progname=basename(argv[0]);
1043
1044 opterr = 0; /* could not print standard getopt error messages */
1045 while ( 1 ) {
1046 optinvalid = 0;
1047
1048 c = getopt(argc, argv, "b:B:c:dhkr:vw:x:");
1049 if (c == -1)
1050 break;
1051
1052 switch (c) {
1053 case 'b':
1054 case 'c':
1055 case 'r':
1056 case 'x':
1057 optinvalid = parse_opt_block(c,optarg);
1058 break;
1059 case 'w':
1060 if (optarg != NULL && *optarg == '-') {
1061 /* rollback */
1062 optind--;
1063 optarg = NULL;
1064 }
1065 optinvalid = parse_opt_block(c,optarg);
1066 break;
1067 case 'd':
1068 invalid_causes_error = 0;
1069 break;
1070 case 'k':
1071 keep_invalid_images = 1;
1072 break;
1073 case 'B':
1074 optinvalid = parse_opt_board(c,optarg);
1075 break;
1076 case 'v':
1077 verblevel++;
1078 break;
1079 case 'h':
1080 usage(EXIT_SUCCESS);
1081 break;
1082 default:
1083 optinvalid = 1;
1084 break;
1085 }
1086 if (optinvalid != 0 ){
1087 ERR("invalid option: -%c", optopt);
1088 goto out;
1089 }
1090 }
1091
1092 if (board == NULL) {
1093 ERR("no board specified");
1094 goto out;
1095 }
1096
1097 if (optind == argc) {
1098 ERR("no output file specified");
1099 goto out;
1100 }
1101
1102 ofname = argv[optind++];
1103
1104 if (optind < argc) {
1105 ERR("invalid option: %s", argv[optind]);
1106 goto out;
1107 }
1108
1109 res = process_blocks();
1110 if (res == ERR_FATAL)
1111 goto out;
1112
1113 if (res == ERR_INVALID_IMAGE) {
1114 if (invalid_causes_error)
1115 res = ERR_FATAL;
1116
1117 if (keep_invalid_images == 0) {
1118 WARN("generation of invalid images \"%s\" disabled", ofname);
1119 goto out;
1120 }
1121
1122 WARN("generating invalid image: \"%s\"", ofname);
1123 }
1124
1125 outfile = fopen(ofname, "w");
1126 if (outfile == NULL) {
1127 ERRS("could not open \"%s\" for writing", ofname);
1128 res = ERR_FATAL;
1129 goto out;
1130 }
1131
1132 if (write_out_blocks(outfile) != 0) {
1133 res = ERR_FATAL;
1134 goto out_flush;
1135 }
1136
1137 DBG(1,"Image file %s completed.", ofname);
1138
1139 out_flush:
1140 fflush(outfile);
1141 fclose(outfile);
1142 if (res == ERR_FATAL) {
1143 unlink(ofname);
1144 }
1145 out:
1146 if (res == ERR_FATAL)
1147 return EXIT_FAILURE;
1148
1149 return EXIT_SUCCESS;
1150 }