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