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