4 * Copyright (C) 2007 OpenWrt.org
5 * Copyright (C) 2007 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 fw_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 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), .fw_offs = (fo) \
128 #define ADMBOARD1(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
129 ADM5120_FLASH_BASE, fs, ADM5120_CODE_START, 0x8000)
131 #define ADMBOARD2(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
132 ADM5120_FLASH_BASE, fs, ADM5120_CODE_START, 0x10000)
134 #define AR7BOARD1(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
135 AR7_FLASH_BASE, fs, AR7_CODE_START, 0x8000)
137 static struct board_info boards
[] = {
139 * Infineon/ADMtek ADM5120 based boards
141 ADMBOARD2("ES-2024A", "ZyXEL ES-2024A", ZYNOS_MODEL_ES_2024A
, 4),
142 ADMBOARD2("ES-2024PWR", "ZyXEL ES-2024PWR", ZYNOS_MODEL_ES_2024PWR
, 4),
143 ADMBOARD2("ES-2108", "ZyXEL ES-2108", ZYNOS_MODEL_ES_2108
, 4),
144 ADMBOARD2("ES-2108-F", "ZyXEL ES-2108-F", ZYNOS_MODEL_ES_2108_F
, 4),
145 ADMBOARD2("ES-2108-G", "ZyXEL ES-2108-G", ZYNOS_MODEL_ES_2108_G
, 4),
146 ADMBOARD2("ES-2108-LC", "ZyXEL ES-2108-LC", ZYNOS_MODEL_ES_2108_LC
, 4),
147 ADMBOARD2("ES-2108PWR", "ZyXEL ES-2108PWR", ZYNOS_MODEL_ES_2108PWR
, 4),
148 ADMBOARD1("HS-100", "ZyXEL HomeSafe 100", ZYNOS_MODEL_HS_100
, 2),
149 ADMBOARD1("HS-100W", "ZyXEL HomeSafe 100W", ZYNOS_MODEL_HS_100W
, 2),
150 ADMBOARD1("P-334", "ZyXEL Prestige 334", ZYNOS_MODEL_P_334
, 2),
151 ADMBOARD1("P-334U", "ZyXEL Prestige 334U", ZYNOS_MODEL_P_334U
, 4),
152 ADMBOARD1("P-334W", "ZyXEL Prestige 334W", ZYNOS_MODEL_P_334W
, 2),
153 ADMBOARD1("P-334WH", "ZyXEL Prestige 334WH", ZYNOS_MODEL_P_334WH
, 4),
154 ADMBOARD1("P-334WHD", "ZyXEL Prestige 334WHD", ZYNOS_MODEL_P_334WHD
, 4),
155 ADMBOARD1("P-334WT", "ZyXEL Prestige 334WT", ZYNOS_MODEL_P_334WT
, 4),
156 ADMBOARD1("P-335", "ZyXEL Prestige 335", ZYNOS_MODEL_P_335
, 4),
157 ADMBOARD1("P-335Plus", "ZyXEL Prestige 335Plus", ZYNOS_MODEL_P_335PLUS
, 4),
158 ADMBOARD1("P-335U", "ZyXEL Prestige 335U", ZYNOS_MODEL_P_335U
, 4),
159 ADMBOARD1("P-335WT", "ZyXEL Prestige 335WT", ZYNOS_MODEL_P_335WT
, 4),
163 * Texas Instruments AR7 based boards
165 AR7BOARD1("P-660H-61", "ZyXEL P-660H-61", ZYNOS_MODEL_P_660H_61
, 2),
166 AR7BOARD1("P-660H-63", "ZyXEL P-660H-63", ZYNOS_MODEL_P_660H_63
, 2),
167 AR7BOARD1("P-660H-D1", "ZyXEL P-660H-D1", ZYNOS_MODEL_P_660H_D1
, 2),
168 AR7BOARD1("P-660H-D3", "ZyXEL P-660H-D3", ZYNOS_MODEL_P_660H_D3
, 2),
169 AR7BOARD1("P-660HW-61", "ZyXEL P-660HW-61", ZYNOS_MODEL_P_660HW_61
, 2),
170 AR7BOARD1("P-660HW-63", "ZyXEL P-660HW-63", ZYNOS_MODEL_P_660HW_63
, 2),
171 AR7BOARD1("P-660HW-67", "ZyXEL P-660HW-67", ZYNOS_MODEL_P_660HW_67
, 2),
172 AR7BOARD1("P-660HW-D1", "ZyXEL P-660HW-D1", ZYNOS_MODEL_P_660HW_D1
, 2),
173 AR7BOARD1("P-660HW-D3", "ZyXEL P-660HW-D3", ZYNOS_MODEL_P_660HW_D3
, 2),
174 AR7BOARD1("P-660R-61", "ZyXEL P-660R-61", ZYNOS_MODEL_P_660R_61
, 2),
175 AR7BOARD1("P-660R-61C", "ZyXEL P-660R-61C", ZYNOS_MODEL_P_660R_61C
, 2),
176 AR7BOARD1("P-660R-63", "ZyXEL P-660R-63", ZYNOS_MODEL_P_660R_63
, 2),
177 AR7BOARD1("P-660R-63C", "ZyXEL P-660R-63C", ZYNOS_MODEL_P_660R_63C
, 2),
178 AR7BOARD1("P-660R-67", "ZyXEL P-660R-67", ZYNOS_MODEL_P_660R_67
, 2),
179 AR7BOARD1("P-660R-D1", "ZyXEL P-660R-D1", ZYNOS_MODEL_P_660R_D1
, 2),
180 AR7BOARD1("P-660R-D3", "ZyXEL P-660R-D3", ZYNOS_MODEL_P_660R_D3
, 2),
184 .desc
= "O2 DSL Surf & Phone",
185 .vendor
= ZYNOS_VENDOR_ID_O2
,
186 .model
= ZYNOS_MODEL_O2SURF
,
187 .flash_base
= AR7_FLASH_BASE
,
188 .flash_size
= 8*1024*1024,
189 .code_start
= 0x94014000,
199 #define ERR(fmt, ...) do { \
201 fprintf(stderr, "[%s] *** error: " fmt "\n", \
202 progname, ## __VA_ARGS__ ); \
205 #define ERRS(fmt, ...) do { \
208 fprintf(stderr, "[%s] *** error: " fmt ", %s\n", \
209 progname, ## __VA_ARGS__, strerror(save)); \
212 #define WARN(fmt, ...) do { \
213 fprintf(stderr, "[%s] *** warning: " fmt "\n", \
214 progname, ## __VA_ARGS__ ); \
217 #define DBG(lev, fmt, ...) do { \
218 if (verblevel < lev) \
220 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
224 #define ERR_INVALID_IMAGE -2
232 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
233 struct board_info
*board
;
235 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
239 " -B <board> create image for the board specified with <board>.\n"
240 " valid <board> values:\n"
242 for (board
= boards
; board
->name
!= NULL
; board
++){
245 board
->name
, board
->desc
);
248 " -b <file>[:<align>]\n"
249 " add boot extension block to the image\n"
250 " -r <file>[:<align>]\n"
251 " add raw block to the image\n"
252 " -o <file> write output to the file <file>\n"
253 " -h show this screen\n"
264 str2u32(char *arg
, uint32_t *val
)
270 t
= strtoul(arg
, &err
, 0);
271 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
281 str2u16(char *arg
, uint16_t *val
)
287 t
= strtoul(arg
, &err
, 0);
288 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
297 str2u8(char *arg
, uint8_t *val
)
303 t
= strtoul(arg
, &err
, 0);
304 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
313 str2sig(char *arg
, uint32_t *sig
)
315 if (strlen(arg
) != 4)
318 *sig
= arg
[0] | (arg
[1] << 8) | (arg
[2] << 16) | (arg
[3] << 24);
325 parse_arg(char *arg
, char *buf
, char *argv
[])
333 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
346 if (argl
>= MAX_ARG_LEN
) {
347 /* argument is too long */
348 argl
= MAX_ARG_LEN
-1;
351 memcpy(buf
, arg
, argl
);
354 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
355 tok
= strsep(ap
, ":");
360 else if (tok
[0] == '\0') {
373 required_arg(char c
, char *arg
)
375 if (arg
== NULL
|| *arg
!= '-')
378 ERR("option -%c requires an argument\n", c
);
384 is_empty_arg(char *arg
)
395 csum_init(struct csum_state
*css
)
404 csum_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
410 css
->sum
+= (css
->tmp
<< 8) + p
[0];
411 if (css
->sum
> 0xFFFF) {
420 for ( ; len
> 1; len
-= 2, p
+=2 ) {
421 css
->sum
+= (p
[0] << 8) + p
[1];
422 if (css
->sum
> 0xFFFF) {
436 csum_get(struct csum_state
*css
)
440 csum_update(&pad
, 1, css
);
445 csum_buf(uint8_t *p
, uint32_t len
)
447 struct csum_state css
;
450 csum_update(p
, len
, &css
);
451 return csum_get(&css
);
456 * routines to write data to the output file
459 write_out_data(FILE *outfile
, uint8_t *data
, size_t len
,
460 struct csum_state
*css
)
464 fwrite(data
, len
, 1, outfile
);
466 ERR("unable to write output file");
471 csum_update(data
, len
, css
);
479 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
,
480 struct csum_state
*css
)
483 size_t buflen
= sizeof(buf
);
485 memset(buf
, padc
, buflen
);
490 if (write_out_data(outfile
, buf
, buflen
, css
))
501 write_out_data_align(FILE *outfile
, uint8_t *data
, size_t len
, size_t align
,
502 struct csum_state
*css
)
507 res
= write_out_data(outfile
, data
, len
, css
);
511 padlen
= ALIGN(len
,align
) - len
;
512 res
= write_out_padding(outfile
, padlen
, 0xFF, css
);
519 write_out_header(FILE *outfile
, struct zyn_rombin_hdr
*hdr
)
521 struct zyn_rombin_hdr t
;
524 if (fseek(outfile
, 0, SEEK_SET
) != 0) {
525 ERRS("fseek failed on output file");
529 /* setup temporary header fields */
530 memset(&t
, 0, sizeof(t
));
531 t
.addr
= HOST_TO_BE32(hdr
->addr
);
532 memcpy(&t
.sig
, ROMBIN_SIGNATURE
, ROMBIN_SIG_LEN
);
534 t
.flags
= hdr
->flags
;
535 t
.osize
= HOST_TO_BE32(hdr
->osize
);
536 t
.csize
= HOST_TO_BE32(hdr
->csize
);
537 t
.ocsum
= HOST_TO_BE16(hdr
->ocsum
);
538 t
.ccsum
= HOST_TO_BE16(hdr
->ccsum
);
539 t
.mmap_addr
= HOST_TO_BE32(hdr
->mmap_addr
);
541 return write_out_data(outfile
, (uint8_t *)&t
, sizeof(t
), NULL
);
546 write_out_mmap(FILE *outfile
, struct fw_mmap
*mmap
, struct csum_state
*css
)
548 struct zyn_mmt_hdr
*mh
;
549 uint8_t buf
[MMAP_DATA_SIZE
];
554 memset(buf
, 0, sizeof(buf
));
556 mh
= (struct zyn_mmt_hdr
*)buf
;
558 /* TODO: needs to recreate the memory map too? */
561 /* Build user data section */
562 data
= buf
+sizeof(*mh
);
563 data
+= sprintf(data
, "Vendor 1 %d", board
->vendor
);
565 data
+= sprintf(data
, "Model 1 %d", BE16_TO_HOST(board
->model
));
567 /* TODO: make hardware version configurable? */
568 data
+= sprintf(data
, "HwVerRange 2 %d %d", 0, 0);
571 user_size
= (uint8_t *)data
- buf
;
572 mh
->user_start
= HOST_TO_BE32(mmap
->addr
+sizeof(*mh
));
573 mh
->user_end
= HOST_TO_BE32(mmap
->addr
+user_size
);
574 mh
->csum
= HOST_TO_BE16(csum_buf(buf
+sizeof(*mh
), user_size
));
576 res
= write_out_data(outfile
, buf
, sizeof(buf
), css
);
583 block_stat_file(struct fw_block
*block
)
588 if (block
->file_name
== NULL
)
591 res
= stat(block
->file_name
, &st
);
593 ERRS("stat failed on %s", block
->file_name
);
597 block
->file_size
= st
.st_size
;
603 write_out_file(FILE *outfile
, char *name
, size_t len
, struct csum_state
*css
)
605 char buf
[FILE_BUF_LEN
];
606 size_t buflen
= sizeof(buf
);
610 DBG(2, "writing out file, name=%s, len=%d",
616 ERRS("unable to open file: %s", name
);
624 /* read data from source file */
626 fread(buf
, buflen
, 1, f
);
628 ERRS("unable to read from file: %s",name
);
633 res
= write_out_data(outfile
, buf
, buflen
, css
);
646 write_out_block(FILE *outfile
, struct fw_block
*block
, struct csum_state
*css
)
653 if (block
->file_name
== NULL
)
656 if (block
->file_size
== 0)
659 res
= write_out_file(outfile
, block
->file_name
,
660 block
->file_size
, css
);
666 write_out_image(FILE *outfile
)
668 struct fw_block
*block
;
670 struct zyn_rombin_hdr hdr
;
671 struct csum_state css
;
676 /* setup header fields */
677 memset(&hdr
, 0, sizeof(hdr
));
678 hdr
.addr
= board
->code_start
;
679 hdr
.type
= OBJECT_TYPE_BOOTEXT
;
680 hdr
.flags
= ROMBIN_FLAG_OCSUM
;
682 res
= write_out_header(outfile
, &hdr
);
686 offset
= sizeof(hdr
);
689 res
= write_out_block(outfile
, bootext_block
, &css
);
693 offset
+= bootext_block
->file_size
;
695 padlen
= ALIGN(offset
,MMAP_ALIGN
) - offset
;
696 res
= write_out_padding(outfile
, padlen
, 0xFF, &css
);
702 mmap
.addr
= board
->flash_base
+ board
->fw_offs
+ offset
;
703 hdr
.mmap_addr
= mmap
.addr
;
704 res
= write_out_mmap(outfile
, &mmap
, &css
);
708 offset
+= MMAP_DATA_SIZE
;
709 hdr
.osize
= offset
- sizeof(hdr
);
710 hdr
.ocsum
= csum_get(&css
);
712 for (i
=0; i
< num_blocks
; i
++) {
715 if (block
->type
== BLOCK_TYPE_BOOTEXT
)
718 padlen
= ALIGN(offset
,block
->align
) - offset
;
719 res
= write_out_padding(outfile
, padlen
, 0xFF, NULL
);
724 res
= write_out_block(outfile
, block
, NULL
);
727 offset
+= block
->file_size
;
730 res
= write_out_header(outfile
, &hdr
);
737 find_board(char *name
)
739 struct board_info
*ret
;
740 struct board_info
*board
;
743 for (board
= boards
; board
->name
!= NULL
; board
++){
744 if (strcasecmp(name
, board
->name
) == 0) {
755 parse_opt_board(char ch
, char *arg
)
758 DBG(1,"parsing board option: -%c %s", ch
, arg
);
761 ERR("only one board option allowed");
765 if (required_arg(ch
, arg
))
768 board
= find_board(arg
);
770 ERR("invalid/unknown board specified: %s", arg
);
779 parse_opt_ofname(char ch
, char *arg
)
782 if (ofname
!= NULL
) {
783 ERR("only one output file allowed");
787 if (required_arg(ch
, arg
))
797 parse_opt_block(char ch
, char *arg
)
799 char buf
[MAX_ARG_LEN
];
800 char *argv
[MAX_ARG_COUNT
];
803 struct fw_block
*block
;
806 if ( num_blocks
>= MAX_NUM_BLOCKS
) {
807 ERR("too many blocks specified");
811 block
= &blocks
[num_blocks
++];
813 /* setup default field values */
819 ERR("only one boot extension block allowed");
822 block
->type
= BLOCK_TYPE_BOOTEXT
;
823 bootext_block
= block
;
826 block
->type
= BLOCK_TYPE_RAW
;
830 argc
= parse_arg(arg
, buf
, argv
);
834 if (is_empty_arg(p
)) {
835 ERR("no file specified in %s", arg
);
838 block
->file_name
= strdup(p
);
839 if (block
->file_name
== NULL
) {
840 ERR("not enough memory");
845 if(block
->type
== BLOCK_TYPE_BOOTEXT
)
849 if (!is_empty_arg(p
)) {
850 if (str2u32(p
, &block
->align
) != 0) {
851 ERR("invalid block align in %s", arg
);
861 calc_block_offsets(int type
, uint32_t *offset
)
863 struct fw_block
*block
;
868 DBG(1,"calculating block offsets, starting with %lu",
872 for (i
= 0; i
< num_blocks
; i
++) {
875 if (block
->type
!= type
)
878 next_offs
= ALIGN(*offset
, block
->align
);
879 avail
= board
->flash_size
- board
->fw_offs
- next_offs
;
880 if (next_offs
+ block
->file_size
> avail
) {
881 ERR("file %s is too big, offset = %u, size=%u,"
882 " align = %u", block
->file_name
,
884 (unsigned)block
->file_size
,
885 (unsigned)block
->align
);
890 block
->padlen
= next_offs
- *offset
;
891 *offset
+= block
->file_size
;
900 struct fw_block
*block
;
905 /* collecting file stats */
906 for (i
= 0; i
< num_blocks
; i
++) {
908 res
= block_stat_file(block
);
913 offset
= board
->fw_offs
+ bootext_block
->file_size
;
914 res
= calc_block_offsets(BLOCK_TYPE_RAW
, &offset
);
921 main(int argc
, char *argv
[])
923 int optinvalid
= 0; /* flag for invalid option */
925 int res
= EXIT_FAILURE
;
929 progname
=basename(argv
[0]);
931 opterr
= 0; /* could not print standard getopt error messages */
935 c
= getopt(argc
, argv
, "b:B:ho:r:v");
942 optinvalid
= parse_opt_block(c
,optarg
);
945 optinvalid
= parse_opt_board(c
,optarg
);
948 optinvalid
= parse_opt_ofname(c
,optarg
);
960 if (optinvalid
!= 0 ) {
961 ERR("invalid option: -%c", optopt
);
967 ERR("no board specified");
971 if (ofname
== NULL
) {
972 ERR("no output file specified");
977 ERR("invalid option: %s", argv
[optind
]);
982 if (process_blocks() != 0) {
986 outfile
= fopen(ofname
, "w");
987 if (outfile
== NULL
) {
988 ERRS("could not open \"%s\" for writing", ofname
);
992 if (write_out_image(outfile
) != 0)
995 DBG(1,"Image file %s completed.", ofname
);
1002 if (res
!= EXIT_SUCCESS
) {
This page took 0.098687 seconds and 5 git commands to generate.