4 * Copyright (C) 2007 OpenWrt.org
5 * Copyright (C) 2007 Gabor Juhos <juhosg@freemail.hu>
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
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
31 #include <unistd.h> /* for unlink() */
33 #include <getopt.h> /* for getopt() */
37 #include <endian.h> /* for __BYTE_ORDER */
38 #if defined(__CYGWIN__)
39 # include <byteswap.h>
44 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
45 # define HOST_TO_LE16(x) (x)
46 # define HOST_TO_LE32(x) (x)
47 # define LE16_TO_HOST(x) (x)
48 # define LE32_TO_HOST(x) (x)
49 # define HOST_TO_BE16(x) bswap_16(x)
50 # define HOST_TO_BE32(x) bswap_32(x)
51 # define BE16_TO_HOST(x) bswap_16(x)
52 # define BE32_TO_HOST(x) bswap_32(x)
54 # define HOST_TO_BE16(x) (x)
55 # define HOST_TO_BE32(x) (x)
56 # define BE16_TO_HOST(x) (x)
57 # define BE32_TO_HOST(x) (x)
58 # define HOST_TO_LE16(x) bswap_16(x)
59 # define HOST_TO_LE32(x) bswap_32(x)
60 # define LE16_TO_HOST(x) bswap_16(x)
61 # define LE32_TO_HOST(x) bswap_32(x)
64 #define ALIGN(x,y) (((x)+((y)-1)) & ~((y)-1))
66 #define MAX_NUM_BLOCKS 8
67 #define MAX_ARG_COUNT 32
68 #define MAX_ARG_LEN 1024
69 #define FILE_BUF_LEN (16*1024)
79 uint32_t align
; /* alignment of this block */
80 char *file_name
; /* name of the file */
81 uint32_t file_size
; /* length of the file */
82 char *mmap_name
; /* name in the MMAP table */
83 int type
; /* block type */
88 #define BLOCK_TYPE_BOOTEXT 0
89 #define BLOCK_TYPE_RAW 1
97 #define MMAP_DATA_SIZE 1024
101 char *name
; /* model name */
102 char *desc
; /* description */
103 uint16_t model
; /* model id */
104 uint32_t flash_base
; /* flash base address */
105 uint32_t flash_size
; /* board flash size */
106 uint32_t code_start
; /* code start address */
107 uint32_t fw_offs
; /* offset of the firmware within the flash */
117 struct board_info
*board
= NULL
;
119 struct fw_block blocks
[MAX_NUM_BLOCKS
];
120 struct fw_block
*bootext_block
= NULL
;
123 #define ADM5120_FLASH_BASE 0xBFC00000
124 #define ADM5120_CODE_START 0x80008000
126 /* TODO: check values for AR7 */
127 #define AR7_FLASH_BASE 0xB0000000
128 #define AR7_CODE_START 0x94008000
130 #define BOARD(n, d, m, fb, fs, cs, fo) { \
131 .name = (n), .desc=(d), .model = (m), \
132 .flash_base = (fb), .flash_size = (fs)<<20, \
133 .code_start = (cs), .fw_offs = (fo) \
136 #define ADMBOARD1(n, d, m, fs) BOARD(n, d, m, \
137 ADM5120_FLASH_BASE, fs, ADM5120_CODE_START, 0x8000)
139 #define ADMBOARD2(n, d, m, fs) BOARD(n, d, m, \
140 ADM5120_FLASH_BASE, fs, ADM5120_CODE_START, 0x10000)
142 #define AR7BOARD1(n, d, m, fs) BOARD(n, d, m, \
143 AR7_FLASH_BASE, fs, AR7_CODE_START, 0x8000)
145 static struct board_info boards
[] = {
147 * Infineon/ADMtek ADM5120 based boards
149 ADMBOARD2("ES-2024A", "ZyXEL ES-2024A", ZYNOS_MODEL_ES_2024A
, 4),
150 ADMBOARD2("ES-2024PWR", "ZyXEL ES-2024PWR", ZYNOS_MODEL_ES_2024PWR
, 4),
151 ADMBOARD2("ES-2108", "ZyXEL ES-2108", ZYNOS_MODEL_ES_2108
, 4),
152 ADMBOARD2("ES-2108-F", "ZyXEL ES-2108-F", ZYNOS_MODEL_ES_2108_F
, 4),
153 ADMBOARD2("ES-2108-G", "ZyXEL ES-2108-G", ZYNOS_MODEL_ES_2108_G
, 4),
154 ADMBOARD2("ES-2108-LC", "ZyXEL ES-2108-LC", ZYNOS_MODEL_ES_2108_LC
, 4),
155 ADMBOARD2("ES-2108PWR", "ZyXEL ES-2108PWR", ZYNOS_MODEL_ES_2108PWR
, 4),
156 ADMBOARD1("HS-100", "ZyXEL HomeSafe 100", ZYNOS_MODEL_HS_100
, 2),
157 ADMBOARD1("HS-100W", "ZyXEL HomeSafe 100W", ZYNOS_MODEL_HS_100W
, 2),
158 ADMBOARD1("P-334", "ZyXEL Prestige 334", ZYNOS_MODEL_P_334
, 2),
159 ADMBOARD1("P-334U", "ZyXEL Prestige 334U", ZYNOS_MODEL_P_334U
, 4),
160 ADMBOARD1("P-334W", "ZyXEL Prestige 334W", ZYNOS_MODEL_P_334W
, 2),
161 ADMBOARD1("P-334WH", "ZyXEL Prestige 334WH", ZYNOS_MODEL_P_334WH
, 4),
162 ADMBOARD1("P-334WHD", "ZyXEL Prestige 334WHD", ZYNOS_MODEL_P_334WHD
, 4),
163 ADMBOARD1("P-334WT", "ZyXEL Prestige 334WT", ZYNOS_MODEL_P_334WT
, 4),
164 ADMBOARD1("P-335", "ZyXEL Prestige 335", ZYNOS_MODEL_P_335
, 4),
165 ADMBOARD1("P-335Plus", "ZyXEL Prestige 335Plus", ZYNOS_MODEL_P_335PLUS
, 4),
166 ADMBOARD1("P-335U", "ZyXEL Prestige 335U", ZYNOS_MODEL_P_335U
, 4),
167 ADMBOARD1("P-335WT", "ZyXEL Prestige 335WT", ZYNOS_MODEL_P_335WT
, 4),
170 * Texas Instruments AR7 based boards
172 AR7BOARD1("P-660H-61", "ZyXEL P-660H-61", ZYNOS_MODEL_P_660H_61
, 2),
173 AR7BOARD1("P-660H-63", "ZyXEL P-660H-63", ZYNOS_MODEL_P_660H_63
, 2),
174 AR7BOARD1("P-660H-D1", "ZyXEL P-660H-D1", ZYNOS_MODEL_P_660H_D1
, 2),
175 AR7BOARD1("P-660H-D3", "ZyXEL P-660H-D3", ZYNOS_MODEL_P_660H_D3
, 2),
176 AR7BOARD1("P-660HW-61", "ZyXEL P-660HW-61", ZYNOS_MODEL_P_660HW_61
, 2),
177 AR7BOARD1("P-660HW-63", "ZyXEL P-660HW-63", ZYNOS_MODEL_P_660HW_63
, 2),
178 AR7BOARD1("P-660HW-67", "ZyXEL P-660HW-67", ZYNOS_MODEL_P_660HW_67
, 2),
179 AR7BOARD1("P-660HW-D1", "ZyXEL P-660HW-D1", ZYNOS_MODEL_P_660HW_D1
, 2),
180 AR7BOARD1("P-660HW-D3", "ZyXEL P-660HW-D3", ZYNOS_MODEL_P_660HW_D3
, 2),
181 AR7BOARD1("P-660R-61", "ZyXEL P-660R-61", ZYNOS_MODEL_P_660R_61
, 2),
182 AR7BOARD1("P-660R-61C", "ZyXEL P-660R-61C", ZYNOS_MODEL_P_660R_61C
, 2),
183 AR7BOARD1("P-660R-63", "ZyXEL P-660R-63", ZYNOS_MODEL_P_660R_63
, 2),
184 AR7BOARD1("P-660R-63C", "ZyXEL P-660R-63C", ZYNOS_MODEL_P_660R_63C
, 2),
185 AR7BOARD1("P-660R-67", "ZyXEL P-660R-67", ZYNOS_MODEL_P_660R_67
, 2),
186 AR7BOARD1("P-660R-D1", "ZyXEL P-660R-D1", ZYNOS_MODEL_P_660R_D1
, 2),
187 AR7BOARD1("P-660R-D3", "ZyXEL P-660R-D3", ZYNOS_MODEL_P_660R_D3
, 2),
195 #define ERR(fmt, ...) do { \
197 fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ ); \
200 #define ERRS(fmt, ...) do { \
203 fprintf(stderr, "[%s] *** error: " fmt ", %s\n", progname, ## __VA_ARGS__ \
207 #define WARN(fmt, ...) do { \
208 fprintf(stderr, "[%s] *** warning: " fmt "\n", progname, ## __VA_ARGS__ ); \
211 #define DBG(lev, fmt, ...) do { \
212 if (verblevel < lev) \
214 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
218 #define ERR_INVALID_IMAGE -2
226 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
227 struct board_info
*board
;
229 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
233 " -B <board> create image for the board specified with <board>.\n"
234 " valid <board> values:\n"
236 for (board
= boards
; board
->name
!= NULL
; board
++){
239 board
->name
, board
->desc
);
242 " -b <file>[:<align>]\n"
243 " add boot extension block to the image\n"
244 " -r <file>[:<align>]\n"
245 " add raw block to the image\n"
246 " -o <file> write output to the file <file>\n"
247 " -h show this screen\n"
258 str2u32(char *arg
, uint32_t *val
)
264 t
= strtoul(arg
, &err
, 0);
265 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
275 str2u16(char *arg
, uint16_t *val
)
281 t
= strtoul(arg
, &err
, 0);
282 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
291 str2u8(char *arg
, uint8_t *val
)
297 t
= strtoul(arg
, &err
, 0);
298 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
307 str2sig(char *arg
, uint32_t *sig
)
309 if (strlen(arg
) != 4)
312 *sig
= arg
[0] | (arg
[1] << 8) | (arg
[2] << 16) | (arg
[3] << 24);
319 parse_arg(char *arg
, char *buf
, char *argv
[])
327 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
340 if (argl
>= MAX_ARG_LEN
) {
341 /* argument is too long */
342 argl
= MAX_ARG_LEN
-1;
345 memcpy(buf
, arg
, argl
);
348 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
349 tok
= strsep(ap
, ":");
354 else if (tok
[0] == '\0') {
367 required_arg(char c
, char *arg
)
369 if (arg
== NULL
|| *arg
!= '-')
372 ERR("option -%c requires an argument\n", c
);
378 is_empty_arg(char *arg
)
389 csum_init(struct csum_state
*css
)
398 csum_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
404 css
->sum
+= (css
->tmp
<< 8) + p
[0];
405 if (css
->sum
> 0xFFFF) {
414 for ( ; len
> 1; len
-= 2, p
+=2 ) {
415 css
->sum
+= (p
[0] << 8) + p
[1];
416 if (css
->sum
> 0xFFFF) {
430 csum_get(struct csum_state
*css
)
434 csum_update(&pad
, 1, css
);
439 csum_buf(uint8_t *p
, uint32_t len
)
441 struct csum_state css
;
444 csum_update(p
, len
, &css
);
445 return csum_get(&css
);
450 * routines to write data to the output file
453 write_out_data(FILE *outfile
, uint8_t *data
, size_t len
,
454 struct csum_state
*css
)
458 fwrite(data
, len
, 1, outfile
);
460 ERR("unable to write output file");
465 csum_update(data
, len
, css
);
473 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
,
474 struct csum_state
*css
)
477 size_t buflen
= sizeof(buf
);
479 memset(buf
, padc
, buflen
);
484 if (write_out_data(outfile
, buf
, buflen
, css
))
495 write_out_data_align(FILE *outfile
, uint8_t *data
, size_t len
, size_t align
,
496 struct csum_state
*css
)
501 res
= write_out_data(outfile
, data
, len
, css
);
505 padlen
= ALIGN(len
,align
) - len
;
506 res
= write_out_padding(outfile
, padlen
, 0xFF, css
);
513 write_out_header(FILE *outfile
, struct zyn_rombin_hdr
*hdr
)
515 struct zyn_rombin_hdr t
;
518 if (fseek(outfile
, 0, SEEK_SET
) != 0) {
519 ERRS("fseek failed on output file");
523 /* setup temporary header fields */
524 memset(&t
,0, sizeof(t
));
525 t
.addr
= HOST_TO_BE32(hdr
->addr
);
526 memcpy(&t
.sig
, ROMBIN_SIGNATURE
, ROMBIN_SIG_LEN
);
528 t
.flags
= hdr
->flags
;
529 t
.osize
= HOST_TO_BE32(hdr
->osize
);
530 t
.csize
= HOST_TO_BE32(hdr
->csize
);
531 t
.ocsum
= HOST_TO_BE16(hdr
->ocsum
);
532 t
.ccsum
= HOST_TO_BE16(hdr
->ccsum
);
533 t
.mmap_addr
= HOST_TO_BE32(hdr
->mmap_addr
);
535 return write_out_data(outfile
, (uint8_t *)&t
, sizeof(t
), NULL
);
540 write_out_mmap(FILE *outfile
, struct fw_mmap
*mmap
, struct csum_state
*css
)
542 struct zyn_mmt_hdr
*mh
;
543 uint8_t buf
[MMAP_DATA_SIZE
];
548 memset(buf
,0,sizeof(buf
));
550 mh
= (struct zyn_mmt_hdr
*)buf
;
552 /* TODO: needs to recreate the memory map too? */
555 /* Build user data section */
556 data
= buf
+sizeof(*mh
);
557 data
+= sprintf(data
,"Model 1 %d", BE16_TO_HOST(board
->model
));
559 /* TODO: make hardware version configurable? */
560 data
+= sprintf(data
,"HwVerRange 2 %d %d", 0, 0);
563 user_size
= (uint8_t *)data
- buf
;
564 mh
->user_start
= HOST_TO_BE32(mmap
->addr
+sizeof(*mh
));
565 mh
->user_end
= HOST_TO_BE32(mmap
->addr
+user_size
);
566 mh
->csum
= HOST_TO_BE16(csum_buf(buf
+sizeof(*mh
), user_size
));
568 res
= write_out_data(outfile
, buf
, sizeof(buf
), css
);
575 block_stat_file(struct fw_block
*block
)
580 if (block
->file_name
== NULL
)
583 res
= stat(block
->file_name
, &st
);
585 ERRS("stat failed on %s", block
->file_name
);
589 block
->file_size
= st
.st_size
;
595 write_out_file(FILE *outfile
, char *name
, size_t len
, struct csum_state
*css
)
597 char buf
[FILE_BUF_LEN
];
598 size_t buflen
= sizeof(buf
);
602 DBG(2, "writing out file, name=%s, len=%d",
608 ERRS("unable to open file: %s", name
);
616 /* read data from source file */
618 fread(buf
, buflen
, 1, f
);
620 ERRS("unable to read from file: %s",name
);
625 res
= write_out_data(outfile
, buf
, buflen
, css
);
638 write_out_block(FILE *outfile
, struct fw_block
*block
, struct csum_state
*css
)
645 if (block
->file_name
== NULL
)
648 if (block
->file_size
== 0)
651 res
= write_out_file(outfile
, block
->file_name
,
652 block
->file_size
, css
);
658 write_out_image(FILE *outfile
)
660 struct fw_block
*block
;
662 struct zyn_rombin_hdr hdr
;
663 struct csum_state css
;
668 /* setup header fields */
669 hdr
.addr
= board
->code_start
;
670 hdr
.type
= OBJECT_TYPE_BOOTEXT
;
671 hdr
.flags
= ROMBIN_FLAG_40
;
673 res
= write_out_header(outfile
, &hdr
);
677 offset
= sizeof(hdr
);
680 res
= write_out_block(outfile
, bootext_block
, &css
);
684 offset
+= bootext_block
->file_size
;
686 padlen
= ALIGN(offset
,MMAP_ALIGN
) - offset
;
687 res
= write_out_padding(outfile
, padlen
, 0xFF, &css
);
693 mmap
.addr
= board
->flash_base
+ board
->fw_offs
+ offset
;
694 hdr
.mmap_addr
= mmap
.addr
;
695 res
= write_out_mmap(outfile
, &mmap
, &css
);
699 offset
+= MMAP_DATA_SIZE
;
700 hdr
.osize
= offset
- sizeof(hdr
);
701 hdr
.ocsum
= csum_get(&css
);
703 for (i
=0; i
< num_blocks
; i
++) {
706 if (block
->type
== BLOCK_TYPE_BOOTEXT
)
709 padlen
= ALIGN(offset
,block
->align
) - offset
;
710 res
= write_out_padding(outfile
, padlen
, 0xFF, NULL
);
715 res
= write_out_block(outfile
, block
, NULL
);
718 offset
+= block
->file_size
;
721 res
= write_out_header(outfile
, &hdr
);
728 find_board(char *name
)
730 struct board_info
*ret
;
731 struct board_info
*board
;
734 for (board
= boards
; board
->name
!= NULL
; board
++){
735 if (strcasecmp(name
, board
->name
) == 0) {
746 parse_opt_board(char ch
, char *arg
)
749 DBG(1,"parsing board option: -%c %s", ch
, arg
);
752 ERR("only one board option allowed");
756 if (required_arg(ch
, arg
))
759 board
= find_board(arg
);
761 ERR("invalid/unknown board specified: %s", arg
);
770 parse_opt_ofname(char ch
, char *arg
)
773 if (ofname
!= NULL
) {
774 ERR("only one output file allowed");
778 if (required_arg(ch
, arg
))
788 parse_opt_block(char ch
, char *arg
)
790 char buf
[MAX_ARG_LEN
];
791 char *argv
[MAX_ARG_COUNT
];
794 struct fw_block
*block
;
797 if ( num_blocks
> MAX_NUM_BLOCKS
) {
798 ERR("too many blocks specified");
802 block
= &blocks
[num_blocks
++];
804 /* setup default field values */
810 ERR("only one boot extension block allowed");
813 block
->type
= BLOCK_TYPE_BOOTEXT
;
814 bootext_block
= block
;
817 block
->type
= BLOCK_TYPE_RAW
;
821 argc
= parse_arg(arg
, buf
, argv
);
825 if (is_empty_arg(p
)) {
826 ERR("no file specified in %s", arg
);
829 block
->file_name
= strdup(p
);
830 if (block
->file_name
== NULL
) {
831 ERR("not enough memory");
836 if(block
->type
== BLOCK_TYPE_BOOTEXT
)
840 if (!is_empty_arg(p
)) {
841 if (str2u32(p
, &block
->align
) != 0) {
842 ERR("invalid block align in %s", arg
);
852 calc_block_offsets(int type
, uint32_t *offset
)
854 struct fw_block
*block
;
859 DBG(1,"calculating block offsets, starting with %lu",
863 for (i
= 0; i
< num_blocks
; i
++) {
866 if (block
->type
!= type
)
869 next_offs
= ALIGN(*offset
, block
->align
);
870 avail
= board
->flash_size
- board
->fw_offs
- next_offs
;
871 if (next_offs
+ block
->file_size
> avail
) {
872 ERR("file %s is too big, offset = %u, size=%u,"
873 " align = %u", block
->file_name
,
875 (unsigned)block
->file_size
,
876 (unsigned)block
->align
);
881 block
->padlen
= next_offs
- *offset
;
882 *offset
+= block
->file_size
;
891 struct fw_block
*block
;
896 /* collecting file stats */
897 for (i
= 0; i
< num_blocks
; i
++) {
899 res
= block_stat_file(block
);
904 offset
= board
->fw_offs
+ bootext_block
->file_size
;
905 res
= calc_block_offsets(BLOCK_TYPE_RAW
, &offset
);
912 main(int argc
, char *argv
[])
914 int optinvalid
= 0; /* flag for invalid option */
916 int res
= EXIT_FAILURE
;
920 progname
=basename(argv
[0]);
922 opterr
= 0; /* could not print standard getopt error messages */
926 c
= getopt(argc
, argv
, "b:B:ho:r:v");
933 optinvalid
= parse_opt_block(c
,optarg
);
936 optinvalid
= parse_opt_board(c
,optarg
);
939 optinvalid
= parse_opt_ofname(c
,optarg
);
951 if (optinvalid
!= 0 ) {
952 ERR("invalid option: -%c", optopt
);
958 ERR("no board specified");
962 if (ofname
== NULL
) {
963 ERR("no output file specified");
968 ERR("invalid option: %s", argv
[optind
]);
973 if (process_blocks() != 0) {
977 outfile
= fopen(ofname
, "w");
978 if (outfile
== NULL
) {
979 ERRS("could not open \"%s\" for writing", ofname
);
983 if (write_out_image(outfile
) != 0)
986 DBG(1,"Image file %s completed.", ofname
);
993 if (res
!= EXIT_SUCCESS
) {