4 * Copyright (C) 2007-2008 OpenWrt.org
5 * Copyright (C) 2007-2008 Gabor Juhos <juhosg at openwrt.org>
7 * This code was based on the information of the ZyXEL's firmware
8 * image format written by Kolja Waschk, can be found at:
9 * http://www.ixo.de/info/zyxel_uclinux
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 as published
13 * by the Free Software Foundation.
21 #include <unistd.h> /* for unlink() */
23 #include <getopt.h> /* for getopt() */
27 #include <endian.h> /* for __BYTE_ORDER */
28 #if defined(__CYGWIN__)
29 # include <byteswap.h>
34 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
35 # define HOST_TO_LE16(x) (x)
36 # define HOST_TO_LE32(x) (x)
37 # define LE16_TO_HOST(x) (x)
38 # define LE32_TO_HOST(x) (x)
39 # define HOST_TO_BE16(x) bswap_16(x)
40 # define HOST_TO_BE32(x) bswap_32(x)
41 # define BE16_TO_HOST(x) bswap_16(x)
42 # define BE32_TO_HOST(x) bswap_32(x)
44 # define HOST_TO_BE16(x) (x)
45 # define HOST_TO_BE32(x) (x)
46 # define BE16_TO_HOST(x) (x)
47 # define BE32_TO_HOST(x) (x)
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)
54 #define ALIGN(x,y) (((x)+((y)-1)) & ~((y)-1))
56 #define MAX_NUM_BLOCKS 8
57 #define MAX_ARG_COUNT 32
58 #define MAX_ARG_LEN 1024
59 #define FILE_BUF_LEN (16*1024)
69 uint32_t align
; /* alignment of this block */
70 char *file_name
; /* name of the file */
71 uint32_t file_size
; /* length of the file */
72 char *mmap_name
; /* name in the MMAP table */
73 int type
; /* block type */
78 #define BLOCK_TYPE_BOOTEXT 0
79 #define BLOCK_TYPE_RAW 1
87 #define MMAP_DATA_SIZE 1024
91 char *name
; /* model name */
92 char *desc
; /* description */
93 uint16_t vendor
; /* vendor id */
94 uint16_t model
; /* model id */
95 uint32_t flash_base
; /* flash base address */
96 uint32_t flash_size
; /* board flash size */
97 uint32_t code_start
; /* code start address */
98 uint32_t romio_offs
; /* offset of the firmware within the flash */
108 struct board_info
*board
= NULL
;
110 struct fw_block blocks
[MAX_NUM_BLOCKS
];
111 struct fw_block
*bootext_block
= NULL
;
114 #define ADM5120_FLASH_BASE 0xBFC00000
115 #define ADM5120_CODE_START 0x80008000
117 /* TODO: check values for AR7 */
118 #define AR7_FLASH_BASE 0xB0000000
119 #define AR7_CODE_START 0x94008000
121 #define ATHEROS_FLASH_BASE 0xBFC00000
122 #define ATHEROS_CODE_START 0x80e00000
124 #define BOARD(n, d, v, m, fb, fs, cs, fo) { \
125 .name = (n), .desc=(d), \
126 .vendor = (v), .model = (m), \
127 .flash_base = (fb), .flash_size = (fs)<<20, \
128 .code_start = (cs), .romio_offs = (fo) \
131 #define ADMBOARD1(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
132 ADM5120_FLASH_BASE, fs, ADM5120_CODE_START, 0x8000)
134 #define ADMBOARD2(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
135 ADM5120_FLASH_BASE, fs, ADM5120_CODE_START, 0x10000)
137 #define AR7BOARD1(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
138 AR7_FLASH_BASE, fs, AR7_CODE_START, 0x8000)
140 #define ATHEROSBOARD1(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
141 ATHEROS_FLASH_BASE, fs, ATHEROS_CODE_START, 0x30000)
143 static struct board_info boards
[] = {
145 * Infineon/ADMtek ADM5120 based boards
147 ADMBOARD2("ES-2024A", "ZyXEL ES-2024A", ZYNOS_MODEL_ES_2024A
, 4),
148 ADMBOARD2("ES-2024PWR", "ZyXEL ES-2024PWR", ZYNOS_MODEL_ES_2024PWR
, 4),
149 ADMBOARD2("ES-2108", "ZyXEL ES-2108", ZYNOS_MODEL_ES_2108
, 4),
150 ADMBOARD2("ES-2108-F", "ZyXEL ES-2108-F", ZYNOS_MODEL_ES_2108_F
, 4),
151 ADMBOARD2("ES-2108-G", "ZyXEL ES-2108-G", ZYNOS_MODEL_ES_2108_G
, 4),
152 ADMBOARD2("ES-2108-LC", "ZyXEL ES-2108-LC", ZYNOS_MODEL_ES_2108_LC
, 4),
153 ADMBOARD2("ES-2108PWR", "ZyXEL ES-2108PWR", ZYNOS_MODEL_ES_2108PWR
, 4),
154 ADMBOARD1("HS-100", "ZyXEL HomeSafe 100", ZYNOS_MODEL_HS_100
, 2),
155 ADMBOARD1("HS-100W", "ZyXEL HomeSafe 100W", ZYNOS_MODEL_HS_100W
, 2),
156 ADMBOARD1("P-334", "ZyXEL Prestige 334", ZYNOS_MODEL_P_334
, 2),
157 ADMBOARD1("P-334U", "ZyXEL Prestige 334U", ZYNOS_MODEL_P_334U
, 4),
158 ADMBOARD1("P-334W", "ZyXEL Prestige 334W", ZYNOS_MODEL_P_334W
, 2),
159 ADMBOARD1("P-334WH", "ZyXEL Prestige 334WH", ZYNOS_MODEL_P_334WH
, 4),
160 ADMBOARD1("P-334WHD", "ZyXEL Prestige 334WHD", ZYNOS_MODEL_P_334WHD
, 4),
161 ADMBOARD1("P-334WT", "ZyXEL Prestige 334WT", ZYNOS_MODEL_P_334WT
, 4),
162 ADMBOARD1("P-335", "ZyXEL Prestige 335", ZYNOS_MODEL_P_335
, 4),
163 ADMBOARD1("P-335Plus", "ZyXEL Prestige 335Plus", ZYNOS_MODEL_P_335PLUS
, 4),
164 ADMBOARD1("P-335U", "ZyXEL Prestige 335U", ZYNOS_MODEL_P_335U
, 4),
165 ADMBOARD1("P-335WT", "ZyXEL Prestige 335WT", ZYNOS_MODEL_P_335WT
, 4),
168 .name
= "P-2602HW-D1A",
169 .desc
= "ZyXEL P-2602HW-D1A",
170 .vendor
= ZYNOS_VENDOR_ID_ZYXEL
,
171 .model
= ZYNOS_MODEL_P_2602HW_D1A
,
172 .flash_base
= AR7_FLASH_BASE
,
173 .flash_size
= 4*1024*1024,
174 .code_start
= 0x94008000,
175 .romio_offs
= 0x20000,
180 * Texas Instruments AR7 based boards
182 AR7BOARD1("P-660H-61", "ZyXEL P-660H-61", ZYNOS_MODEL_P_660H_61
, 2),
183 AR7BOARD1("P-660H-63", "ZyXEL P-660H-63", ZYNOS_MODEL_P_660H_63
, 2),
184 AR7BOARD1("P-660H-D1", "ZyXEL P-660H-D1", ZYNOS_MODEL_P_660H_D1
, 2),
185 AR7BOARD1("P-660H-D3", "ZyXEL P-660H-D3", ZYNOS_MODEL_P_660H_D3
, 2),
186 AR7BOARD1("P-660HW-61", "ZyXEL P-660HW-61", ZYNOS_MODEL_P_660HW_61
, 2),
187 AR7BOARD1("P-660HW-63", "ZyXEL P-660HW-63", ZYNOS_MODEL_P_660HW_63
, 2),
188 AR7BOARD1("P-660HW-67", "ZyXEL P-660HW-67", ZYNOS_MODEL_P_660HW_67
, 2),
189 AR7BOARD1("P-660HW-D1", "ZyXEL P-660HW-D1", ZYNOS_MODEL_P_660HW_D1
, 2),
190 AR7BOARD1("P-660HW-D3", "ZyXEL P-660HW-D3", ZYNOS_MODEL_P_660HW_D3
, 2),
191 AR7BOARD1("P-660R-61", "ZyXEL P-660R-61", ZYNOS_MODEL_P_660R_61
, 2),
192 AR7BOARD1("P-660R-61C", "ZyXEL P-660R-61C", ZYNOS_MODEL_P_660R_61C
, 2),
193 AR7BOARD1("P-660R-63", "ZyXEL P-660R-63", ZYNOS_MODEL_P_660R_63
, 2),
194 AR7BOARD1("P-660R-63C", "ZyXEL P-660R-63C", ZYNOS_MODEL_P_660R_63C
, 2),
195 AR7BOARD1("P-660R-67", "ZyXEL P-660R-67", ZYNOS_MODEL_P_660R_67
, 2),
196 AR7BOARD1("P-660R-D1", "ZyXEL P-660R-D1", ZYNOS_MODEL_P_660R_D1
, 2),
197 AR7BOARD1("P-660R-D3", "ZyXEL P-660R-D3", ZYNOS_MODEL_P_660R_D3
, 2),
201 .desc
= "O2 DSL Surf & Phone",
202 .vendor
= ZYNOS_VENDOR_ID_O2
,
203 .model
= ZYNOS_MODEL_O2SURF
,
204 .flash_base
= AR7_FLASH_BASE
,
205 .flash_size
= 8*1024*1024,
206 .code_start
= 0x94014000,
207 .romio_offs
= 0x40000,
213 ATHEROSBOARD1("NBG-318S", "ZyXEL NBG-318S", ZYNOS_MODEL_NBG_318S
, 4),
221 #define ERR(fmt, ...) do { \
223 fprintf(stderr, "[%s] *** error: " fmt "\n", \
224 progname, ## __VA_ARGS__ ); \
227 #define ERRS(fmt, ...) do { \
230 fprintf(stderr, "[%s] *** error: " fmt ", %s\n", \
231 progname, ## __VA_ARGS__, strerror(save)); \
234 #define WARN(fmt, ...) do { \
235 fprintf(stderr, "[%s] *** warning: " fmt "\n", \
236 progname, ## __VA_ARGS__ ); \
239 #define DBG(lev, fmt, ...) do { \
240 if (verblevel < lev) \
242 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
246 #define ERR_INVALID_IMAGE -2
254 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
255 struct board_info
*board
;
257 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
261 " -B <board> create image for the board specified with <board>.\n"
262 " valid <board> values:\n"
264 for (board
= boards
; board
->name
!= NULL
; board
++){
267 board
->name
, board
->desc
);
270 " -b <file>[:<align>]\n"
271 " add boot extension block to the image\n"
272 " -r <file>[:<align>]\n"
273 " add raw block to the image\n"
274 " -o <file> write output to the file <file>\n"
275 " -h show this screen\n"
286 str2u32(char *arg
, uint32_t *val
)
292 t
= strtoul(arg
, &err
, 0);
293 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
303 str2u16(char *arg
, uint16_t *val
)
309 t
= strtoul(arg
, &err
, 0);
310 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
319 str2u8(char *arg
, uint8_t *val
)
325 t
= strtoul(arg
, &err
, 0);
326 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
335 str2sig(char *arg
, uint32_t *sig
)
337 if (strlen(arg
) != 4)
340 *sig
= arg
[0] | (arg
[1] << 8) | (arg
[2] << 16) | (arg
[3] << 24);
347 parse_arg(char *arg
, char *buf
, char *argv
[])
355 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
368 if (argl
>= MAX_ARG_LEN
) {
369 /* argument is too long */
370 argl
= MAX_ARG_LEN
-1;
373 memcpy(buf
, arg
, argl
);
376 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
377 tok
= strsep(ap
, ":");
382 else if (tok
[0] == '\0') {
395 required_arg(char c
, char *arg
)
397 if (arg
== NULL
|| *arg
!= '-')
400 ERR("option -%c requires an argument\n", c
);
406 is_empty_arg(char *arg
)
417 csum_init(struct csum_state
*css
)
426 csum_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
432 css
->sum
+= (css
->tmp
<< 8) + p
[0];
433 if (css
->sum
> 0xFFFF) {
442 for ( ; len
> 1; len
-= 2, p
+=2 ) {
443 css
->sum
+= (p
[0] << 8) + p
[1];
444 if (css
->sum
> 0xFFFF) {
458 csum_get(struct csum_state
*css
)
462 csum_update(&pad
, 1, css
);
467 csum_buf(uint8_t *p
, uint32_t len
)
469 struct csum_state css
;
472 csum_update(p
, len
, &css
);
473 return csum_get(&css
);
478 * routines to write data to the output file
481 write_out_data(FILE *outfile
, uint8_t *data
, size_t len
,
482 struct csum_state
*css
)
486 fwrite(data
, len
, 1, outfile
);
488 ERR("unable to write output file");
493 csum_update(data
, len
, css
);
501 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
,
502 struct csum_state
*css
)
505 size_t buflen
= sizeof(buf
);
507 memset(buf
, padc
, buflen
);
512 if (write_out_data(outfile
, buf
, buflen
, css
))
523 write_out_data_align(FILE *outfile
, uint8_t *data
, size_t len
, size_t align
,
524 struct csum_state
*css
)
529 res
= write_out_data(outfile
, data
, len
, css
);
533 padlen
= ALIGN(len
,align
) - len
;
534 res
= write_out_padding(outfile
, padlen
, 0xFF, css
);
541 write_out_header(FILE *outfile
, struct zyn_rombin_hdr
*hdr
)
543 struct zyn_rombin_hdr t
;
546 if (fseek(outfile
, 0, SEEK_SET
) != 0) {
547 ERRS("fseek failed on output file");
551 /* setup temporary header fields */
552 memset(&t
, 0, sizeof(t
));
553 t
.addr
= HOST_TO_BE32(hdr
->addr
);
554 memcpy(&t
.sig
, ROMBIN_SIGNATURE
, ROMBIN_SIG_LEN
);
556 t
.flags
= hdr
->flags
;
557 t
.osize
= HOST_TO_BE32(hdr
->osize
);
558 t
.csize
= HOST_TO_BE32(hdr
->csize
);
559 t
.ocsum
= HOST_TO_BE16(hdr
->ocsum
);
560 t
.ccsum
= HOST_TO_BE16(hdr
->ccsum
);
561 t
.mmap_addr
= HOST_TO_BE32(hdr
->mmap_addr
);
563 return write_out_data(outfile
, (uint8_t *)&t
, sizeof(t
), NULL
);
568 write_out_mmap(FILE *outfile
, struct fw_mmap
*mmap
, struct csum_state
*css
)
570 struct zyn_mmt_hdr
*mh
;
571 uint8_t buf
[MMAP_DATA_SIZE
];
576 memset(buf
, 0, sizeof(buf
));
578 mh
= (struct zyn_mmt_hdr
*)buf
;
580 /* TODO: needs to recreate the memory map too? */
583 /* Build user data section */
584 data
= buf
+sizeof(*mh
);
585 data
+= sprintf(data
, "Vendor 1 %d", board
->vendor
);
587 data
+= sprintf(data
, "Model 1 %d", BE16_TO_HOST(board
->model
));
589 /* TODO: make hardware version configurable? */
590 data
+= sprintf(data
, "HwVerRange 2 %d %d", 0, 0);
593 user_size
= (uint8_t *)data
- buf
;
594 mh
->user_start
= HOST_TO_BE32(mmap
->addr
+sizeof(*mh
));
595 mh
->user_end
= HOST_TO_BE32(mmap
->addr
+user_size
);
596 mh
->csum
= HOST_TO_BE16(csum_buf(buf
+sizeof(*mh
), user_size
));
598 res
= write_out_data(outfile
, buf
, sizeof(buf
), css
);
605 block_stat_file(struct fw_block
*block
)
610 if (block
->file_name
== NULL
)
613 res
= stat(block
->file_name
, &st
);
615 ERRS("stat failed on %s", block
->file_name
);
619 block
->file_size
= st
.st_size
;
625 write_out_file(FILE *outfile
, char *name
, size_t len
, struct csum_state
*css
)
627 char buf
[FILE_BUF_LEN
];
628 size_t buflen
= sizeof(buf
);
632 DBG(2, "writing out file, name=%s, len=%d",
638 ERRS("unable to open file: %s", name
);
646 /* read data from source file */
648 fread(buf
, buflen
, 1, f
);
650 ERRS("unable to read from file: %s",name
);
655 res
= write_out_data(outfile
, buf
, buflen
, css
);
668 write_out_block(FILE *outfile
, struct fw_block
*block
, struct csum_state
*css
)
675 if (block
->file_name
== NULL
)
678 if (block
->file_size
== 0)
681 res
= write_out_file(outfile
, block
->file_name
,
682 block
->file_size
, css
);
688 write_out_image(FILE *outfile
)
690 struct fw_block
*block
;
692 struct zyn_rombin_hdr hdr
;
693 struct csum_state css
;
698 /* setup header fields */
699 memset(&hdr
, 0, sizeof(hdr
));
700 hdr
.addr
= board
->code_start
;
701 hdr
.type
= OBJECT_TYPE_BOOTEXT
;
702 hdr
.flags
= ROMBIN_FLAG_OCSUM
;
704 res
= write_out_header(outfile
, &hdr
);
708 offset
= sizeof(hdr
);
711 res
= write_out_block(outfile
, bootext_block
, &css
);
715 offset
+= bootext_block
->file_size
;
717 padlen
= ALIGN(offset
,MMAP_ALIGN
) - offset
;
718 res
= write_out_padding(outfile
, padlen
, 0xFF, &css
);
724 mmap
.addr
= board
->flash_base
+ board
->romio_offs
+ offset
;
725 hdr
.mmap_addr
= mmap
.addr
;
726 res
= write_out_mmap(outfile
, &mmap
, &css
);
730 offset
+= MMAP_DATA_SIZE
;
731 hdr
.osize
= offset
- sizeof(hdr
);
732 hdr
.ocsum
= csum_get(&css
);
734 for (i
=0; i
< num_blocks
; i
++) {
737 if (block
->type
== BLOCK_TYPE_BOOTEXT
)
740 padlen
= ALIGN(offset
,block
->align
) - offset
;
741 res
= write_out_padding(outfile
, padlen
, 0xFF, NULL
);
746 res
= write_out_block(outfile
, block
, NULL
);
749 offset
+= block
->file_size
;
752 res
= write_out_header(outfile
, &hdr
);
759 find_board(char *name
)
761 struct board_info
*ret
;
762 struct board_info
*board
;
765 for (board
= boards
; board
->name
!= NULL
; board
++){
766 if (strcasecmp(name
, board
->name
) == 0) {
777 parse_opt_board(char ch
, char *arg
)
780 DBG(1,"parsing board option: -%c %s", ch
, arg
);
783 ERR("only one board option allowed");
787 if (required_arg(ch
, arg
))
790 board
= find_board(arg
);
792 ERR("invalid/unknown board specified: %s", arg
);
801 parse_opt_ofname(char ch
, char *arg
)
804 if (ofname
!= NULL
) {
805 ERR("only one output file allowed");
809 if (required_arg(ch
, arg
))
819 parse_opt_block(char ch
, char *arg
)
821 char buf
[MAX_ARG_LEN
];
822 char *argv
[MAX_ARG_COUNT
];
825 struct fw_block
*block
;
828 if ( num_blocks
>= MAX_NUM_BLOCKS
) {
829 ERR("too many blocks specified");
833 block
= &blocks
[num_blocks
++];
835 /* setup default field values */
841 ERR("only one boot extension block allowed");
844 block
->type
= BLOCK_TYPE_BOOTEXT
;
845 bootext_block
= block
;
848 block
->type
= BLOCK_TYPE_RAW
;
852 argc
= parse_arg(arg
, buf
, argv
);
856 if (is_empty_arg(p
)) {
857 ERR("no file specified in %s", arg
);
860 block
->file_name
= strdup(p
);
861 if (block
->file_name
== NULL
) {
862 ERR("not enough memory");
867 if(block
->type
== BLOCK_TYPE_BOOTEXT
)
871 if (!is_empty_arg(p
)) {
872 if (str2u32(p
, &block
->align
) != 0) {
873 ERR("invalid block align in %s", arg
);
883 calc_block_offsets(int type
, uint32_t *offset
)
885 struct fw_block
*block
;
890 DBG(1,"calculating block offsets, starting with %lu",
894 for (i
= 0; i
< num_blocks
; i
++) {
897 if (block
->type
!= type
)
900 next_offs
= ALIGN(*offset
, block
->align
);
901 avail
= board
->flash_size
- board
->romio_offs
- next_offs
;
902 if (next_offs
+ block
->file_size
> avail
) {
903 ERR("file %s is too big, offset = %u, size=%u,"
904 " align = %u", block
->file_name
,
906 (unsigned)block
->file_size
,
907 (unsigned)block
->align
);
912 block
->padlen
= next_offs
- *offset
;
913 *offset
+= block
->file_size
;
922 struct fw_block
*block
;
927 /* collecting file stats */
928 for (i
= 0; i
< num_blocks
; i
++) {
930 res
= block_stat_file(block
);
935 offset
= board
->romio_offs
+ bootext_block
->file_size
;
936 res
= calc_block_offsets(BLOCK_TYPE_RAW
, &offset
);
943 main(int argc
, char *argv
[])
945 int optinvalid
= 0; /* flag for invalid option */
947 int res
= EXIT_FAILURE
;
951 progname
=basename(argv
[0]);
953 opterr
= 0; /* could not print standard getopt error messages */
957 c
= getopt(argc
, argv
, "b:B:ho:r:v");
964 optinvalid
= parse_opt_block(c
,optarg
);
967 optinvalid
= parse_opt_board(c
,optarg
);
970 optinvalid
= parse_opt_ofname(c
,optarg
);
982 if (optinvalid
!= 0 ) {
983 ERR("invalid option: -%c", optopt
);
989 ERR("no board specified");
993 if (ofname
== NULL
) {
994 ERR("no output file specified");
999 ERR("invalid option: %s", argv
[optind
]);
1004 if (process_blocks() != 0) {
1008 outfile
= fopen(ofname
, "w");
1009 if (outfile
== NULL
) {
1010 ERRS("could not open \"%s\" for writing", ofname
);
1014 if (write_out_image(outfile
) != 0)
1017 DBG(1,"Image file %s completed.", ofname
);
1024 if (res
!= EXIT_SUCCESS
) {
This page took 0.11043 seconds and 5 git commands to generate.