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