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