e156f79153ae6625c93ad6e227b3fc847ec19047
[openwrt/staging/hauke.git] / tools / firmware-utils / 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(uint8_t *p, uint32_t len, struct csum_state *css)
427 {
428 uint16_t t;
429
430 if (css->odd) {
431 t = css->tmp + (p[0]<<8);
432 css->val += LE16_TO_HOST(t);
433 css->odd = 0;
434 len--;
435 p++;
436 }
437
438 for ( ; len > 1; len -= 2, p +=2 ) {
439 t = p[0] + (p[1] << 8);
440 css->val += LE16_TO_HOST(t);
441 }
442
443 if (len == 1) {
444 css->tmp = p[0];
445 css->odd = 1;
446 }
447 }
448
449
450 uint16_t
451 csum16_get(struct csum_state *css)
452 {
453 char pad = 0;
454
455 csum16_update(&pad, 1, css);
456 return ~css->val + 1;
457 }
458
459
460 void
461 csum_init(struct csum_state *css, int size)
462 {
463 css->val = 0;
464 css->tmp = 0;
465 css->odd = 0;
466 css->size = size;
467 }
468
469
470 void
471 csum_update(uint8_t *p, uint32_t len, struct csum_state *css)
472 {
473 switch (css->size) {
474 case CSUM_SIZE_8:
475 csum8_update(p,len,css);
476 break;
477 case CSUM_SIZE_16:
478 csum16_update(p,len,css);
479 break;
480 }
481 }
482
483
484 uint16_t
485 csum_get(struct csum_state *css)
486 {
487 uint16_t ret;
488
489 switch (css->size) {
490 case CSUM_SIZE_8:
491 ret = csum8_get(css);
492 break;
493 case CSUM_SIZE_16:
494 ret = csum16_get(css);
495 break;
496 default:
497 ERR("invalid checksum size\n");
498 return 0;
499 }
500
501 return ret;
502 }
503
504
505 /*
506 * routines to write data to the output file
507 */
508 int
509 write_out_data(FILE *outfile, uint8_t *data, size_t len,
510 struct csum_state *css)
511 {
512 errno = 0;
513
514 fwrite(data, len, 1, outfile);
515 if (errno) {
516 ERRS("unable to write output file");
517 return ERR_FATAL;
518 }
519
520 if (css) {
521 csum_update(data, len, css);
522 }
523
524 return 0;
525 }
526
527
528 int
529 write_out_padding(FILE *outfile, size_t len, uint8_t padc,
530 struct csum_state *css)
531 {
532 uint8_t buf[512];
533 size_t buflen = sizeof(buf);
534 int err;
535
536 memset(buf, padc, buflen);
537 while (len > 0) {
538 if (len < buflen)
539 buflen = len;
540
541 err = write_out_data(outfile, buf, buflen, css);
542 if (err)
543 return err;
544
545 len -= buflen;
546 }
547
548 return 0;
549 }
550
551
552 int
553 block_stat_file(struct csys_block *block)
554 {
555 struct stat st;
556 int err;
557
558 if (block->file_name == NULL)
559 return 0;
560
561 err = stat(block->file_name, &st);
562 if (err){
563 ERRS("stat failed on %s", block->file_name);
564 return ERR_FATAL;
565 }
566
567 block->file_size = st.st_size;
568 return 0;
569 }
570
571
572 int
573 block_writeout_hdr(FILE *outfile, struct csys_block *block)
574 {
575 struct csys_header hdr;
576 int res;
577
578 if (block->size_hdr == 0)
579 return 0;
580
581 /* setup header fields */
582 memcpy(hdr.sig, block->sig, 4);
583 hdr.addr = HOST_TO_LE32(block->addr);
584 hdr.size = HOST_TO_LE32(block->size - block->size_hdr - block->size_csum);
585
586 DBG(1,"writing header for block");
587 res = write_out_data(outfile, (uint8_t *)&hdr, sizeof(hdr),NULL);
588 return res;
589
590 }
591
592
593 int
594 block_writeout_file(FILE *outfile, struct csys_block *block)
595 {
596 char buf[FILE_BUF_LEN];
597 size_t buflen = sizeof(buf);
598 FILE *f;
599 size_t len;
600 int res;
601
602 if (block->file_name == NULL)
603 return 0;
604
605 if (block->file_size == 0)
606 return 0;
607
608 errno = 0;
609 f = fopen(block->file_name,"r");
610 if (errno) {
611 ERRS("unable to open file: %s", block->file_name);
612 return ERR_FATAL;
613 }
614
615 len = block->file_size;
616 while (len > 0) {
617 if (len < buflen)
618 buflen = len;
619
620 /* read data from source file */
621 errno = 0;
622 fread(buf, buflen, 1, f);
623 if (errno != 0) {
624 ERRS("unable to read from file: %s", block->file_name);
625 res = ERR_FATAL;
626 break;
627 }
628
629 res = write_out_data(outfile, buf, buflen, block->css);
630 if (res)
631 break;
632
633 len -= buflen;
634 }
635
636 fclose(f);
637 return res;
638 }
639
640
641 int
642 block_writeout_data(FILE *outfile, struct csys_block *block)
643 {
644 int res;
645 size_t padlen;
646
647 res = block_writeout_file(outfile, block);
648 if (res)
649 return res;
650
651 /* write padding data if neccesary */
652 padlen = block->size_avail - block->file_size;
653 DBG(1,"padding block, length=%zu", padlen);
654 res = write_out_padding(outfile, padlen, block->padc, block->css);
655
656 return res;
657 }
658
659
660 int
661 block_writeout_csum(FILE *outfile, struct csys_block *block)
662 {
663 uint16_t csum;
664 int res;
665
666 if (block->size_csum == 0)
667 return 0;
668
669 DBG(1,"writing checksum for block");
670 csum = HOST_TO_LE16(csum_get(block->css));
671 res = write_out_data(outfile, (uint8_t *)&csum, block->size_csum, NULL);
672
673 return res;
674 }
675
676
677 int
678 block_writeout(FILE *outfile, struct csys_block *block)
679 {
680 int res;
681 struct csum_state css;
682
683 res = 0;
684
685 if (block == NULL)
686 return res;
687
688 block->css = NULL;
689
690 DBG(2, "writing block, file=%s, file_size=%d, space=%d",
691 block->file_name, block->file_size, block->size_avail);
692 res = block_writeout_hdr(outfile, block);
693 if (res)
694 return res;
695
696 if (block->size_csum != 0) {
697 block->css = &css;
698 csum_init(&css, block->size_csum);
699 }
700
701 res = block_writeout_data(outfile, block);
702 if (res)
703 return res;
704
705 res = block_writeout_csum(outfile, block);
706 if (res)
707 return res;
708
709 return res;
710 }
711
712
713 int
714 write_out_blocks(FILE *outfile)
715 {
716 struct csys_block *block;
717 int i, res;
718
719 res = block_writeout(outfile, boot_block);
720 if (res)
721 return res;
722
723 res = block_writeout(outfile, conf_block);
724 if (res)
725 return res;
726
727 res = block_writeout(outfile, webp_block);
728 if (res)
729 return res;
730
731 res = block_writeout(outfile, code_block);
732 if (res)
733 return res;
734
735 res = 0;
736 for (i=0; i < num_blocks; i++) {
737 block = &blocks[i];
738
739 if (block->type != BLOCK_TYPE_XTRA)
740 continue;
741
742 res = block_writeout(outfile, block);
743 if (res)
744 break;
745 }
746
747 return res;
748 }
749
750
751 struct board_info *
752 find_board(char *model)
753 {
754 struct board_info *ret;
755 struct board_info *board;
756
757 ret = NULL;
758 for (board = boards; board->model != NULL; board++){
759 if (strcasecmp(model, board->model) == 0) {
760 ret = board;
761 break;
762 }
763 };
764
765 return ret;
766 }
767
768
769 int
770 parse_opt_board(char ch, char *arg)
771 {
772
773 DBG(1,"parsing board option: -%c %s", ch, arg);
774
775 if (board != NULL) {
776 ERR("only one board option allowed");
777 return ERR_FATAL;
778 }
779
780 if (required_arg(ch, arg))
781 return ERR_FATAL;
782
783 board = find_board(arg);
784 if (board == NULL){
785 ERR("invalid/unknown board specified: %s", arg);
786 return ERR_FATAL;
787 }
788
789 return 0;
790 }
791
792
793 int
794 parse_opt_block(char ch, char *arg)
795 {
796 char buf[MAX_ARG_LEN];
797 char *argv[MAX_ARG_COUNT];
798 char *p;
799 struct csys_block *block;
800 int i;
801
802 if ( num_blocks > MAX_NUM_BLOCKS ) {
803 ERR("too many blocks specified");
804 return ERR_FATAL;
805 }
806
807 block = &blocks[num_blocks];
808
809 /* setup default field values */
810 block->need_file = 1;
811 block->padc = 0xFF;
812
813 switch (ch) {
814 case 'b':
815 if (boot_block) {
816 WARN("only one boot block allowed");
817 break;
818 }
819 block->type = BLOCK_TYPE_BOOT;
820 boot_block = block;
821 break;
822 case 'c':
823 if (conf_block) {
824 WARN("only one config block allowed");
825 break;
826 }
827 block->type = BLOCK_TYPE_CONF;
828 conf_block = block;
829 break;
830 case 'w':
831 if (webp_block) {
832 WARN("only one web block allowed");
833 break;
834 }
835 block->type = BLOCK_TYPE_WEBP;
836 block->size_hdr = sizeof(struct csys_header);
837 block->size_csum = CSUM_SIZE_8;
838 block->need_file = 0;
839 webp_block = block;
840 break;
841 case 'r':
842 if (code_block) {
843 WARN("only one runtime block allowed");
844 break;
845 }
846 block->type = BLOCK_TYPE_CODE;
847 block->size_hdr = sizeof(struct csys_header);
848 block->size_csum = CSUM_SIZE_16;
849 code_block = block;
850 break;
851 case 'x':
852 block->type = BLOCK_TYPE_XTRA;
853 break;
854 default:
855 ERR("unknown block type \"%c\"", ch);
856 return ERR_FATAL;
857 }
858
859 parse_arg(arg, buf, argv);
860
861 i = 0;
862 p = argv[i++];
863 if (!is_empty_arg(p)) {
864 block->file_name = strdup(p);
865 if (block->file_name == NULL) {
866 ERR("not enough memory");
867 return ERR_FATAL;
868 }
869 } else if (block->need_file){
870 ERR("no file specified in %s", arg);
871 return ERR_FATAL;
872 }
873
874 if (block->size_hdr) {
875 p = argv[i++];
876 if (!is_empty_arg(p)) {
877 if (str2u32(p, &block->addr) != 0) {
878 ERR("invalid start address in %s", arg);
879 return ERR_FATAL;
880 }
881 block->addr_set = 1;
882 }
883 }
884
885 p = argv[i++];
886 if (!is_empty_arg(p)) {
887 if (str2u32(p, &block->align) != 0) {
888 ERR("invalid alignment value in %s", arg);
889 return ERR_FATAL;
890 }
891 block->align_set = 1;
892 }
893
894 p = argv[i++];
895 if (!is_empty_arg(p) && (str2u8(p, &block->padc) != 0)) {
896 ERR("invalid paddig character in %s", arg);
897 return ERR_FATAL;
898 }
899
900 num_blocks++;
901
902 return 0;
903 }
904
905
906 int
907 process_blocks(void)
908 {
909 struct csys_block *block;
910 uint32_t offs = 0;
911 int i;
912 int res;
913
914 res = 0;
915 /* collecting stats */
916 for (i = 0; i < num_blocks; i++) {
917 block = &blocks[i];
918 res = block_stat_file(block);
919 if (res)
920 return res;
921 }
922
923 /* bootloader */
924 block = boot_block;
925 if (block) {
926 block->size = board->boot_size;
927 if (block->file_size > board->boot_size) {
928 WARN("boot block is too big");
929 res = ERR_INVALID_IMAGE;
930 }
931 }
932 offs += board->boot_size;
933
934 /* configuration data */
935 block = conf_block;
936 if (block) {
937 block->size = board->conf_size;
938 if (block->file_size > board->conf_size) {
939 WARN("config block is too big");
940 res = ERR_INVALID_IMAGE;
941 }
942 }
943 offs += board->conf_size;
944
945 /* webpages */
946 block = webp_block;
947 if (block) {
948
949 memcpy(block->sig, board->sig_webp, 4);
950
951 if (block->addr_set == 0)
952 block->addr = board->addr_webp;
953
954 if (block->align_set == 0)
955 block->align = DEFAULT_BLOCK_ALIGN;
956
957 block->size = align(offs + block->file_size + block->size_hdr +
958 block->size_csum, block->align) - offs;
959
960 if (block->size > board->webp_size_max) {
961 WARN("webpages block is too big");
962 res = ERR_INVALID_IMAGE;
963 }
964
965 DBG(2,"webpages start at %08x, size=%08x", offs,
966 block->size);
967
968 offs += block->size;
969 if (offs > board->flash_size) {
970 WARN("webp block is too big");
971 res = ERR_INVALID_IMAGE;
972 }
973 }
974
975 /* runtime code */
976 block = code_block;
977 if (block) {
978 memcpy(code_block->sig, SIG_CSYS, 4);
979
980 if (block->addr_set == 0)
981 block->addr = board->addr_code;
982
983 if (block->align_set == 0)
984 block->align = DEFAULT_BLOCK_ALIGN;
985
986 block->size = align(offs + block->file_size +
987 block->size_hdr + block->size_csum,
988 block->align) - offs;
989
990 DBG(2,"code block start at %08x, size=%08x", offs,
991 block->size);
992
993 offs += block->size;
994 if (offs > board->flash_size) {
995 WARN("code block is too big");
996 res = ERR_INVALID_IMAGE;
997 }
998 }
999
1000 for (i = 0; i < num_blocks; i++) {
1001 block = &blocks[i];
1002
1003 if (block->type != BLOCK_TYPE_XTRA)
1004 continue;
1005
1006 if (block->align_set == 0)
1007 block->align = DEFAULT_BLOCK_ALIGN;
1008
1009 block->size = align(offs + block->file_size,
1010 block->align) - offs;
1011
1012 DBG(2,"file %s start at %08x, size=%08x, align=%08x",
1013 block->file_name, offs, block->size, block->align);
1014
1015 offs += block->size;
1016 if (offs > board->flash_size) {
1017 WARN("file %s is too big, size=%d, avail=%d",
1018 block->file_name, block->file_size,
1019 board->flash_size - offs);
1020 res = ERR_INVALID_IMAGE;
1021 }
1022 }
1023
1024 for (i = 0; i < num_blocks; i++) {
1025 block = &blocks[i];
1026
1027 block->size_avail = block->size - block->size_hdr -
1028 block->size_csum;
1029
1030 if (block->size_avail < block->file_size) {
1031 WARN("file %s is too big, size=%d, avail=%d",
1032 block->file_name, block->file_size,
1033 block->size_avail);
1034 res = ERR_INVALID_IMAGE;
1035 }
1036 }
1037
1038 return res;
1039 }
1040
1041
1042 int
1043 main(int argc, char *argv[])
1044 {
1045 int optinvalid = 0; /* flag for invalid option */
1046 int c;
1047 int res = ERR_FATAL;
1048
1049 FILE *outfile;
1050
1051 progname=basename(argv[0]);
1052
1053 opterr = 0; /* could not print standard getopt error messages */
1054 while ( 1 ) {
1055 optinvalid = 0;
1056
1057 c = getopt(argc, argv, "b:B:c:dhkr:vw:x:");
1058 if (c == -1)
1059 break;
1060
1061 switch (c) {
1062 case 'b':
1063 case 'c':
1064 case 'r':
1065 case 'x':
1066 optinvalid = parse_opt_block(c,optarg);
1067 break;
1068 case 'w':
1069 if (optarg != NULL && *optarg == '-') {
1070 /* rollback */
1071 optind--;
1072 optarg = NULL;
1073 }
1074 optinvalid = parse_opt_block(c,optarg);
1075 break;
1076 case 'd':
1077 invalid_causes_error = 0;
1078 break;
1079 case 'k':
1080 keep_invalid_images = 1;
1081 break;
1082 case 'B':
1083 optinvalid = parse_opt_board(c,optarg);
1084 break;
1085 case 'v':
1086 verblevel++;
1087 break;
1088 case 'h':
1089 usage(EXIT_SUCCESS);
1090 break;
1091 default:
1092 optinvalid = 1;
1093 break;
1094 }
1095 if (optinvalid != 0 ){
1096 ERR("invalid option: -%c", optopt);
1097 goto out;
1098 }
1099 }
1100
1101 if (board == NULL) {
1102 ERR("no board specified");
1103 goto out;
1104 }
1105
1106 if (optind == argc) {
1107 ERR("no output file specified");
1108 goto out;
1109 }
1110
1111 ofname = argv[optind++];
1112
1113 if (optind < argc) {
1114 ERR("invalid option: %s", argv[optind]);
1115 goto out;
1116 }
1117
1118 res = process_blocks();
1119 if (res == ERR_FATAL)
1120 goto out;
1121
1122 if (res == ERR_INVALID_IMAGE) {
1123 if (invalid_causes_error)
1124 res = ERR_FATAL;
1125
1126 if (keep_invalid_images == 0) {
1127 WARN("generation of invalid images \"%s\" disabled", ofname);
1128 goto out;
1129 }
1130
1131 WARN("generating invalid image: \"%s\"", ofname);
1132 }
1133
1134 outfile = fopen(ofname, "w");
1135 if (outfile == NULL) {
1136 ERRS("could not open \"%s\" for writing", ofname);
1137 res = ERR_FATAL;
1138 goto out;
1139 }
1140
1141 if (write_out_blocks(outfile) != 0) {
1142 res = ERR_FATAL;
1143 goto out_flush;
1144 }
1145
1146 DBG(1,"Image file %s completed.", ofname);
1147
1148 out_flush:
1149 fflush(outfile);
1150 fclose(outfile);
1151 if (res == ERR_FATAL) {
1152 unlink(ofname);
1153 }
1154 out:
1155 if (res == ERR_FATAL)
1156 return EXIT_FAILURE;
1157
1158 return EXIT_SUCCESS;
1159 }