1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2007-2008 OpenWrt.org
5 * Copyright (C) 2007-2008 Gabor Juhos <juhosg at openwrt.org>
13 #include <unistd.h> /* for unlink() */
15 #include <getopt.h> /* for getopt() */
19 #include <endian.h> /* for __BYTE_ORDER */
20 #if defined(__CYGWIN__)
21 # include <byteswap.h>
27 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
28 # define HOST_TO_LE16(x) (x)
29 # define HOST_TO_LE32(x) (x)
30 # define LE16_TO_HOST(x) (x)
31 # define LE32_TO_HOST(x) (x)
32 # define HOST_TO_BE16(x) bswap_16(x)
33 # define HOST_TO_BE32(x) bswap_32(x)
34 # define BE16_TO_HOST(x) bswap_16(x)
35 # define BE32_TO_HOST(x) bswap_32(x)
37 # define HOST_TO_BE16(x) (x)
38 # define HOST_TO_BE32(x) (x)
39 # define BE16_TO_HOST(x) (x)
40 # define BE32_TO_HOST(x) (x)
41 # define HOST_TO_LE16(x) bswap_16(x)
42 # define HOST_TO_LE32(x) bswap_32(x)
43 # define LE16_TO_HOST(x) bswap_16(x)
44 # define LE32_TO_HOST(x) bswap_32(x)
47 #define ALIGN(x,y) (((x)+((y)-1)) & ~((y)-1))
49 #define MAX_NUM_BLOCKS 8
50 #define MAX_ARG_COUNT 32
51 #define MAX_ARG_LEN 1024
52 #define FILE_BUF_LEN (16*1024)
62 uint32_t align
; /* alignment of this block */
63 char *file_name
; /* name of the file */
64 uint32_t file_size
; /* length of the file */
65 char *mmap_name
; /* name in the MMAP table */
66 int type
; /* block type */
71 #define BLOCK_TYPE_BOOTEXT 0
72 #define BLOCK_TYPE_RAW 1
80 #define MMAP_DATA_SIZE 1024
84 char *name
; /* model name */
85 char *desc
; /* description */
86 uint16_t vendor
; /* vendor id */
87 uint16_t model
; /* model id */
88 uint32_t flash_base
; /* flash base address */
89 uint32_t flash_size
; /* board flash size */
90 uint32_t code_start
; /* code start address */
91 uint32_t romio_offs
; /* offset of the firmware within the flash */
92 uint32_t bootext_size
; /* maximum size of bootext block */
102 struct board_info
*board
= NULL
;
104 struct fw_block blocks
[MAX_NUM_BLOCKS
];
105 struct fw_block
*bootext_block
= NULL
;
108 #define ADM5120_FLASH_BASE 0xBFC00000
109 #define ADM5120_CODE_START 0x80008000
111 /* TODO: check values for AR7 */
112 #define AR7_FLASH_BASE 0xB0000000
113 #define AR7_CODE_START 0x94008000
115 #define ATHEROS_FLASH_BASE 0xBFC00000
116 #define ATHEROS_CODE_START 0x80e00000
118 #define AR71XX_FLASH_BASE 0xBFC00000
119 #define AR71XX_CODE_START 0x81E00000
121 #define BOARD(n, d, v, m, fb, fs, cs, fo) { \
122 .name = (n), .desc=(d), \
123 .vendor = (v), .model = (m), \
124 .flash_base = (fb), .flash_size = (fs)<<20, \
125 .code_start = (cs), .romio_offs = (fo), \
126 .bootext_size = BOOTEXT_DEF_SIZE \
129 #define ADMBOARD1(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
130 ADM5120_FLASH_BASE, fs, ADM5120_CODE_START, 0x8000)
132 #define ADMBOARD2(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
133 ADM5120_FLASH_BASE, fs, ADM5120_CODE_START, 0x10000)
135 #define AR7BOARD1(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
136 AR7_FLASH_BASE, fs, AR7_CODE_START, 0x8000)
138 #define ATHEROSBOARD1(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
139 ATHEROS_FLASH_BASE, fs, ATHEROS_CODE_START, 0x30000)
141 #define AR71XXBOARD1(n, d, m, fs) { \
142 .name = (n), .desc=(d), \
143 .vendor = (ZYNOS_VENDOR_ID_ZYXEL), .model = (m), \
144 .flash_base = (AR71XX_FLASH_BASE), .flash_size = (fs)<<20, \
145 .code_start = (AR71XX_CODE_START), .romio_offs = (0x40000), \
146 .bootext_size = 0x30000 \
150 static struct board_info boards
[] = {
152 * Infineon/ADMtek ADM5120 based boards
154 ADMBOARD2("ES-2024A", "ZyXEL ES-2024A", ZYNOS_MODEL_ES_2024A
, 4),
155 ADMBOARD2("ES-2024PWR", "ZyXEL ES-2024PWR", ZYNOS_MODEL_ES_2024PWR
, 4),
156 ADMBOARD2("ES-2108", "ZyXEL ES-2108", ZYNOS_MODEL_ES_2108
, 4),
157 ADMBOARD2("ES-2108-F", "ZyXEL ES-2108-F", ZYNOS_MODEL_ES_2108_F
, 4),
158 ADMBOARD2("ES-2108-G", "ZyXEL ES-2108-G", ZYNOS_MODEL_ES_2108_G
, 4),
159 ADMBOARD2("ES-2108-LC", "ZyXEL ES-2108-LC", ZYNOS_MODEL_ES_2108_LC
, 4),
160 ADMBOARD2("ES-2108PWR", "ZyXEL ES-2108PWR", ZYNOS_MODEL_ES_2108PWR
, 4),
161 ADMBOARD1("HS-100", "ZyXEL HomeSafe 100", ZYNOS_MODEL_HS_100
, 2),
162 ADMBOARD1("HS-100W", "ZyXEL HomeSafe 100W", ZYNOS_MODEL_HS_100W
, 2),
163 ADMBOARD1("P-334", "ZyXEL Prestige 334", ZYNOS_MODEL_P_334
, 2),
164 ADMBOARD1("P-334U", "ZyXEL Prestige 334U", ZYNOS_MODEL_P_334U
, 4),
165 ADMBOARD1("P-334W", "ZyXEL Prestige 334W", ZYNOS_MODEL_P_334W
, 2),
166 ADMBOARD1("P-334WH", "ZyXEL Prestige 334WH", ZYNOS_MODEL_P_334WH
, 4),
167 ADMBOARD1("P-334WHD", "ZyXEL Prestige 334WHD", ZYNOS_MODEL_P_334WHD
, 4),
168 ADMBOARD1("P-334WT", "ZyXEL Prestige 334WT", ZYNOS_MODEL_P_334WT
, 4),
169 ADMBOARD1("P-335", "ZyXEL Prestige 335", ZYNOS_MODEL_P_335
, 4),
170 ADMBOARD1("P-335Plus", "ZyXEL Prestige 335Plus", ZYNOS_MODEL_P_335PLUS
, 4),
171 ADMBOARD1("P-335U", "ZyXEL Prestige 335U", ZYNOS_MODEL_P_335U
, 4),
172 ADMBOARD1("P-335WT", "ZyXEL Prestige 335WT", ZYNOS_MODEL_P_335WT
, 4),
175 .name
= "P-2602HW-D1A",
176 .desc
= "ZyXEL P-2602HW-D1A",
177 .vendor
= ZYNOS_VENDOR_ID_ZYXEL
,
178 .model
= ZYNOS_MODEL_P_2602HW_D1A
,
179 .flash_base
= AR7_FLASH_BASE
,
180 .flash_size
= 4*1024*1024,
181 .code_start
= 0x94008000,
182 .romio_offs
= 0x20000,
183 .bootext_size
= BOOTEXT_DEF_SIZE
,
188 * Texas Instruments AR7 based boards
190 AR7BOARD1("P-660H-61", "ZyXEL P-660H-61", ZYNOS_MODEL_P_660H_61
, 2),
191 AR7BOARD1("P-660H-63", "ZyXEL P-660H-63", ZYNOS_MODEL_P_660H_63
, 2),
192 AR7BOARD1("P-660H-D1", "ZyXEL P-660H-D1", ZYNOS_MODEL_P_660H_D1
, 2),
193 AR7BOARD1("P-660H-D3", "ZyXEL P-660H-D3", ZYNOS_MODEL_P_660H_D3
, 2),
194 AR7BOARD1("P-660HW-61", "ZyXEL P-660HW-61", ZYNOS_MODEL_P_660HW_61
, 2),
195 AR7BOARD1("P-660HW-63", "ZyXEL P-660HW-63", ZYNOS_MODEL_P_660HW_63
, 2),
196 AR7BOARD1("P-660HW-67", "ZyXEL P-660HW-67", ZYNOS_MODEL_P_660HW_67
, 2),
197 AR7BOARD1("P-660HW-D1", "ZyXEL P-660HW-D1", ZYNOS_MODEL_P_660HW_D1
, 2),
198 AR7BOARD1("P-660HW-D3", "ZyXEL P-660HW-D3", ZYNOS_MODEL_P_660HW_D3
, 2),
199 AR7BOARD1("P-660R-61", "ZyXEL P-660R-61", ZYNOS_MODEL_P_660R_61
, 2),
200 AR7BOARD1("P-660R-61C", "ZyXEL P-660R-61C", ZYNOS_MODEL_P_660R_61C
, 2),
201 AR7BOARD1("P-660R-63", "ZyXEL P-660R-63", ZYNOS_MODEL_P_660R_63
, 2),
202 AR7BOARD1("P-660R-63C", "ZyXEL P-660R-63C", ZYNOS_MODEL_P_660R_63C
, 2),
203 AR7BOARD1("P-660R-67", "ZyXEL P-660R-67", ZYNOS_MODEL_P_660R_67
, 2),
204 AR7BOARD1("P-660R-D1", "ZyXEL P-660R-D1", ZYNOS_MODEL_P_660R_D1
, 2),
205 AR7BOARD1("P-660R-D3", "ZyXEL P-660R-D3", ZYNOS_MODEL_P_660R_D3
, 2),
209 .desc
= "O2 DSL Surf & Phone",
210 .vendor
= ZYNOS_VENDOR_ID_O2
,
211 .model
= ZYNOS_MODEL_O2SURF
,
212 .flash_base
= AR7_FLASH_BASE
,
213 .flash_size
= 8*1024*1024,
214 .code_start
= 0x94014000,
215 .romio_offs
= 0x40000,
216 .bootext_size
= BOOTEXT_DEF_SIZE
,
222 ATHEROSBOARD1("NBG-318S", "ZyXEL NBG-318S", ZYNOS_MODEL_NBG_318S
, 4),
225 * Atheros ar71xx based boards
227 AR71XXBOARD1("NBG-460N", "ZyXEL NBG-460N", ZYNOS_MODEL_NBG_460N
, 4),
235 #define ERR(fmt, ...) do { \
237 fprintf(stderr, "[%s] *** error: " fmt "\n", \
238 progname, ## __VA_ARGS__ ); \
241 #define ERRS(fmt, ...) do { \
244 fprintf(stderr, "[%s] *** error: " fmt ", %s\n", \
245 progname, ## __VA_ARGS__, strerror(save)); \
248 #define WARN(fmt, ...) do { \
249 fprintf(stderr, "[%s] *** warning: " fmt "\n", \
250 progname, ## __VA_ARGS__ ); \
253 #define DBG(lev, fmt, ...) do { \
254 if (verblevel < lev) \
256 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
260 #define ERR_INVALID_IMAGE -2
268 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
269 struct board_info
*board
;
271 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
275 " -B <board> create image for the board specified with <board>.\n"
276 " valid <board> values:\n"
278 for (board
= boards
; board
->name
!= NULL
; board
++){
281 board
->name
, board
->desc
);
284 " -b <file>[:<align>]\n"
285 " add boot extension block to the image\n"
286 " -r <file>[:<align>]\n"
287 " add raw block to the image\n"
288 " -o <file> write output to the file <file>\n"
289 " -h show this screen\n"
300 str2u32(char *arg
, uint32_t *val
)
306 t
= strtoul(arg
, &err
, 0);
307 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
317 str2u16(char *arg
, uint16_t *val
)
323 t
= strtoul(arg
, &err
, 0);
324 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
333 str2u8(char *arg
, uint8_t *val
)
339 t
= strtoul(arg
, &err
, 0);
340 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
349 str2sig(char *arg
, uint32_t *sig
)
351 if (strlen(arg
) != 4)
354 *sig
= arg
[0] | (arg
[1] << 8) | (arg
[2] << 16) | (arg
[3] << 24);
361 parse_arg(char *arg
, char *buf
, char *argv
[])
369 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
382 if (argl
>= MAX_ARG_LEN
) {
383 /* argument is too long */
384 argl
= MAX_ARG_LEN
-1;
387 memcpy(buf
, arg
, argl
);
390 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
391 tok
= strsep(ap
, ":");
396 else if (tok
[0] == '\0') {
409 required_arg(char c
, char *arg
)
411 if (arg
== NULL
|| *arg
!= '-')
414 ERR("option -%c requires an argument\n", c
);
420 is_empty_arg(char *arg
)
431 csum_init(struct csum_state
*css
)
440 csum_update(void *data
, uint32_t len
, struct csum_state
*css
)
448 css
->sum
+= (css
->tmp
<< 8) + p
[0];
449 if (css
->sum
> 0xFFFF) {
458 for ( ; len
> 1; len
-= 2, p
+=2 ) {
459 css
->sum
+= (p
[0] << 8) + p
[1];
460 if (css
->sum
> 0xFFFF) {
474 csum_get(struct csum_state
*css
)
478 csum_update(&pad
, 1, css
);
483 csum_buf(uint8_t *p
, uint32_t len
)
485 struct csum_state css
;
488 csum_update(p
, len
, &css
);
489 return csum_get(&css
);
494 * routines to write data to the output file
497 write_out_data(FILE *outfile
, void *data
, size_t len
,
498 struct csum_state
*css
)
504 fwrite(ptr
, len
, 1, outfile
);
506 ERR("unable to write output file");
511 csum_update(ptr
, len
, css
);
519 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
,
520 struct csum_state
*css
)
523 size_t buflen
= sizeof(buf
);
525 memset(buf
, padc
, buflen
);
530 if (write_out_data(outfile
, buf
, buflen
, css
))
541 write_out_data_align(FILE *outfile
, void *data
, size_t len
, size_t align
,
542 struct csum_state
*css
)
547 res
= write_out_data(outfile
, data
, len
, css
);
551 padlen
= ALIGN(len
,align
) - len
;
552 res
= write_out_padding(outfile
, padlen
, 0xFF, css
);
559 write_out_header(FILE *outfile
, struct zyn_rombin_hdr
*hdr
)
561 struct zyn_rombin_hdr t
;
564 if (fseek(outfile
, 0, SEEK_SET
) != 0) {
565 ERRS("fseek failed on output file");
569 /* setup temporary header fields */
570 memset(&t
, 0, sizeof(t
));
571 t
.addr
= HOST_TO_BE32(hdr
->addr
);
572 memcpy(&t
.sig
, ROMBIN_SIGNATURE
, ROMBIN_SIG_LEN
);
574 t
.flags
= hdr
->flags
;
575 t
.osize
= HOST_TO_BE32(hdr
->osize
);
576 t
.csize
= HOST_TO_BE32(hdr
->csize
);
577 t
.ocsum
= HOST_TO_BE16(hdr
->ocsum
);
578 t
.ccsum
= HOST_TO_BE16(hdr
->ccsum
);
579 t
.mmap_addr
= HOST_TO_BE32(hdr
->mmap_addr
);
581 DBG(2, "hdr.addr = 0x%08x", hdr
->addr
);
582 DBG(2, "hdr.type = 0x%02x", hdr
->type
);
583 DBG(2, "hdr.osize = 0x%08x", hdr
->osize
);
584 DBG(2, "hdr.csize = 0x%08x", hdr
->csize
);
585 DBG(2, "hdr.flags = 0x%02x", hdr
->flags
);
586 DBG(2, "hdr.ocsum = 0x%04x", hdr
->ocsum
);
587 DBG(2, "hdr.ccsum = 0x%04x", hdr
->ccsum
);
588 DBG(2, "hdr.mmap_addr = 0x%08x", hdr
->mmap_addr
);
590 return write_out_data(outfile
, (uint8_t *)&t
, sizeof(t
), NULL
);
595 write_out_mmap(FILE *outfile
, struct fw_mmap
*mmap
, struct csum_state
*css
)
597 struct zyn_mmt_hdr
*mh
;
598 uint8_t buf
[MMAP_DATA_SIZE
];
603 memset(buf
, 0, sizeof(buf
));
605 mh
= (struct zyn_mmt_hdr
*)buf
;
607 /* TODO: needs to recreate the memory map too? */
610 /* Build user data section */
611 data
= (char *)buf
+ sizeof(*mh
);
612 data
+= sprintf(data
, "Vendor 1 %d", board
->vendor
);
614 data
+= sprintf(data
, "Model 1 %d", BE16_TO_HOST(board
->model
));
616 /* TODO: make hardware version configurable? */
617 data
+= sprintf(data
, "HwVerRange 2 %d %d", 0, 0);
620 user_size
= (uint8_t *)data
- buf
;
621 mh
->user_start
= HOST_TO_BE32(mmap
->addr
+sizeof(*mh
));
622 mh
->user_end
= HOST_TO_BE32(mmap
->addr
+user_size
);
623 mh
->csum
= HOST_TO_BE16(csum_buf(buf
+sizeof(*mh
), user_size
));
625 res
= write_out_data(outfile
, buf
, sizeof(buf
), css
);
632 block_stat_file(struct fw_block
*block
)
637 if (block
->file_name
== NULL
)
640 res
= stat(block
->file_name
, &st
);
642 ERRS("stat failed on %s", block
->file_name
);
646 block
->file_size
= st
.st_size
;
652 read_magic(uint16_t *magic
)
658 f
= fopen(bootext_block
->file_name
,"r");
660 ERRS("unable to open file: %s", bootext_block
->file_name
);
665 fread(magic
, 2, 1, f
);
667 ERRS("unable to read from file: %s", bootext_block
->file_name
);
681 write_out_file(FILE *outfile
, char *name
, size_t len
, struct csum_state
*css
)
683 char buf
[FILE_BUF_LEN
];
684 size_t buflen
= sizeof(buf
);
688 DBG(2, "writing out file, name=%s, len=%zu",
694 ERRS("unable to open file: %s", name
);
702 /* read data from source file */
704 fread(buf
, buflen
, 1, f
);
706 ERRS("unable to read from file: %s",name
);
711 res
= write_out_data(outfile
, buf
, buflen
, css
);
724 write_out_block(FILE *outfile
, struct fw_block
*block
, struct csum_state
*css
)
731 if (block
->file_name
== NULL
)
734 if (block
->file_size
== 0)
737 res
= write_out_file(outfile
, block
->file_name
,
738 block
->file_size
, css
);
744 write_out_image(FILE *outfile
)
746 struct fw_block
*block
;
748 struct zyn_rombin_hdr hdr
;
749 struct csum_state css
;
756 /* setup header fields */
757 memset(&hdr
, 0, sizeof(hdr
));
758 hdr
.addr
= board
->code_start
;
759 hdr
.type
= OBJECT_TYPE_BOOTEXT
;
760 hdr
.flags
= ROMBIN_FLAG_OCSUM
;
762 offset
= board
->romio_offs
;
764 res
= write_out_header(outfile
, &hdr
);
768 offset
+= sizeof(hdr
);
771 res
= write_out_block(outfile
, bootext_block
, &css
);
775 offset
+= bootext_block
->file_size
;
776 if (offset
> (board
->romio_offs
+ board
->bootext_size
)) {
777 ERR("bootext file '%s' is too big", bootext_block
->file_name
);
781 padlen
= ALIGN(offset
, MMAP_ALIGN
) - offset
;
782 res
= write_out_padding(outfile
, padlen
, 0xFF, &css
);
788 mmap
.addr
= board
->flash_base
+ offset
;
789 res
= write_out_mmap(outfile
, &mmap
, &css
);
793 offset
+= MMAP_DATA_SIZE
;
795 if ((offset
- board
->romio_offs
) < board
->bootext_size
) {
796 padlen
= board
->romio_offs
+ board
->bootext_size
- offset
;
797 res
= write_out_padding(outfile
, padlen
, 0xFF, &css
);
802 DBG(2, "bootext end at %08x", offset
);
805 for (i
= 0; i
< num_blocks
; i
++) {
808 if (block
->type
== BLOCK_TYPE_BOOTEXT
)
811 padlen
= ALIGN(offset
, block
->align
) - offset
;
812 res
= write_out_padding(outfile
, padlen
, 0xFF, &css
);
817 res
= write_out_block(outfile
, block
, &css
);
820 offset
+= block
->file_size
;
823 padlen
= ALIGN(offset
, 4) - offset
;
824 res
= write_out_padding(outfile
, padlen
, 0xFF, &css
);
829 csum
= csum_get(&css
);
830 hdr
.mmap_addr
= mmap
.addr
;
833 res
= read_magic(&hdr
.ocsum
);
836 hdr
.ocsum
= BE16_TO_HOST(hdr
.ocsum
);
838 if (csum
<= hdr
.ocsum
)
839 t
= hdr
.ocsum
- csum
;
841 t
= hdr
.ocsum
- csum
- 1;
843 DBG(2, "ocsum=%04x, csum=%04x, fix=%04x", hdr
.ocsum
, csum
, t
);
846 res
= write_out_data(outfile
, (uint8_t *)&t
, 2, NULL
);
851 res
= write_out_header(outfile
, &hdr
);
858 find_board(char *name
)
860 struct board_info
*ret
;
861 struct board_info
*board
;
864 for (board
= boards
; board
->name
!= NULL
; board
++){
865 if (strcasecmp(name
, board
->name
) == 0) {
876 parse_opt_board(char ch
, char *arg
)
879 DBG(1,"parsing board option: -%c %s", ch
, arg
);
882 ERR("only one board option allowed");
886 if (required_arg(ch
, arg
))
889 board
= find_board(arg
);
891 ERR("invalid/unknown board specified: %s", arg
);
900 parse_opt_ofname(char ch
, char *arg
)
903 if (ofname
!= NULL
) {
904 ERR("only one output file allowed");
908 if (required_arg(ch
, arg
))
918 parse_opt_block(char ch
, char *arg
)
920 char buf
[MAX_ARG_LEN
];
921 char *argv
[MAX_ARG_COUNT
];
923 struct fw_block
*block
;
926 if ( num_blocks
>= MAX_NUM_BLOCKS
) {
927 ERR("too many blocks specified");
931 block
= &blocks
[num_blocks
++];
933 /* setup default field values */
939 ERR("only one boot extension block allowed");
942 block
->type
= BLOCK_TYPE_BOOTEXT
;
943 bootext_block
= block
;
946 block
->type
= BLOCK_TYPE_RAW
;
950 parse_arg(arg
, buf
, argv
);
954 if (is_empty_arg(p
)) {
955 ERR("no file specified in %s", arg
);
958 block
->file_name
= strdup(p
);
959 if (block
->file_name
== NULL
) {
960 ERR("not enough memory");
965 if (block
->type
== BLOCK_TYPE_BOOTEXT
)
969 if (!is_empty_arg(p
)) {
970 if (str2u32(p
, &block
->align
) != 0) {
971 ERR("invalid block align in %s", arg
);
981 calc_block_offsets(int type
, uint32_t *offset
)
983 struct fw_block
*block
;
988 DBG(1,"calculating block offsets, starting with %" PRIu32
,
992 for (i
= 0; i
< num_blocks
; i
++) {
995 if (block
->type
!= type
)
998 next_offs
= ALIGN(*offset
, block
->align
);
999 avail
= board
->flash_size
- next_offs
;
1000 if (block
->file_size
> avail
) {
1001 ERR("file %s is too big, offset = %u, size=%u,"
1002 " avail = %u, align = %u", block
->file_name
,
1003 (unsigned)next_offs
,
1004 (unsigned)block
->file_size
,
1006 (unsigned)block
->align
);
1011 block
->padlen
= next_offs
- *offset
;
1012 *offset
+= block
->file_size
;
1019 process_blocks(void)
1021 struct fw_block
*block
;
1026 /* collecting file stats */
1027 for (i
= 0; i
< num_blocks
; i
++) {
1029 res
= block_stat_file(block
);
1034 offset
= board
->romio_offs
+ bootext_block
->file_size
;
1035 res
= calc_block_offsets(BLOCK_TYPE_RAW
, &offset
);
1042 main(int argc
, char *argv
[])
1044 int optinvalid
= 0; /* flag for invalid option */
1046 int res
= EXIT_FAILURE
;
1050 progname
=basename(argv
[0]);
1052 opterr
= 0; /* could not print standard getopt error messages */
1056 c
= getopt(argc
, argv
, "b:B:ho:r:v");
1063 optinvalid
= parse_opt_block(c
,optarg
);
1066 optinvalid
= parse_opt_board(c
,optarg
);
1069 optinvalid
= parse_opt_ofname(c
,optarg
);
1075 usage(EXIT_SUCCESS
);
1081 if (optinvalid
!= 0 ) {
1082 ERR("invalid option: -%c", optopt
);
1087 if (board
== NULL
) {
1088 ERR("no board specified");
1092 if (ofname
== NULL
) {
1093 ERR("no output file specified");
1097 if (optind
< argc
) {
1098 ERR("invalid option: %s", argv
[optind
]);
1102 if (process_blocks() != 0) {
1106 outfile
= fopen(ofname
, "w");
1107 if (outfile
== NULL
) {
1108 ERRS("could not open \"%s\" for writing", ofname
);
1112 if (write_out_image(outfile
) != 0)
1115 DBG(1,"Image file %s completed.", ofname
);
1122 if (res
!= EXIT_SUCCESS
) {