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 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), .fw_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, 0x8000)
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),
169 * Texas Instruments AR7 based boards
171 AR7BOARD1("P-660H-61", "ZyXEL P-660H-61", ZYNOS_MODEL_P_660H_61, 2),
172 AR7BOARD1("P-660H-63", "ZyXEL P-660H-63", ZYNOS_MODEL_P_660H_63, 2),
173 AR7BOARD1("P-660H-D1", "ZyXEL P-660H-D1", ZYNOS_MODEL_P_660H_D1, 2),
174 AR7BOARD1("P-660H-D3", "ZyXEL P-660H-D3", ZYNOS_MODEL_P_660H_D3, 2),
175 AR7BOARD1("P-660HW-61", "ZyXEL P-660HW-61", ZYNOS_MODEL_P_660HW_61, 2),
176 AR7BOARD1("P-660HW-63", "ZyXEL P-660HW-63", ZYNOS_MODEL_P_660HW_63, 2),
177 AR7BOARD1("P-660HW-67", "ZyXEL P-660HW-67", ZYNOS_MODEL_P_660HW_67, 2),
178 AR7BOARD1("P-660HW-D1", "ZyXEL P-660HW-D1", ZYNOS_MODEL_P_660HW_D1, 2),
179 AR7BOARD1("P-660HW-D3", "ZyXEL P-660HW-D3", ZYNOS_MODEL_P_660HW_D3, 2),
180 AR7BOARD1("P-660R-61", "ZyXEL P-660R-61", ZYNOS_MODEL_P_660R_61, 2),
181 AR7BOARD1("P-660R-61C", "ZyXEL P-660R-61C", ZYNOS_MODEL_P_660R_61C, 2),
182 AR7BOARD1("P-660R-63", "ZyXEL P-660R-63", ZYNOS_MODEL_P_660R_63, 2),
183 AR7BOARD1("P-660R-63C", "ZyXEL P-660R-63C", ZYNOS_MODEL_P_660R_63C, 2),
184 AR7BOARD1("P-660R-67", "ZyXEL P-660R-67", ZYNOS_MODEL_P_660R_67, 2),
185 AR7BOARD1("P-660R-D1", "ZyXEL P-660R-D1", ZYNOS_MODEL_P_660R_D1, 2),
186 AR7BOARD1("P-660R-D3", "ZyXEL P-660R-D3", ZYNOS_MODEL_P_660R_D3, 2),
190 .desc = "O2 DSL Surf & Phone",
191 .vendor = ZYNOS_VENDOR_ID_O2,
192 .model = ZYNOS_MODEL_O2SURF,
193 .flash_base = AR7_FLASH_BASE,
194 .flash_size = 8*1024*1024,
195 .code_start = 0x94014000,
202 ATHEROSBOARD1("NBG-318S", "ZyXEL NBG-318S", ZYNOS_MODEL_NBG_318S, 4),
210 #define ERR(fmt, ...) do { \
212 fprintf(stderr, "[%s] *** error: " fmt "\n", \
213 progname, ## __VA_ARGS__ ); \
216 #define ERRS(fmt, ...) do { \
219 fprintf(stderr, "[%s] *** error: " fmt ", %s\n", \
220 progname, ## __VA_ARGS__, strerror(save)); \
223 #define WARN(fmt, ...) do { \
224 fprintf(stderr, "[%s] *** warning: " fmt "\n", \
225 progname, ## __VA_ARGS__ ); \
228 #define DBG(lev, fmt, ...) do { \
229 if (verblevel < lev) \
231 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
235 #define ERR_INVALID_IMAGE -2
243 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
244 struct board_info *board;
246 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
250 " -B <board> create image for the board specified with <board>.\n"
251 " valid <board> values:\n"
253 for (board = boards; board->name != NULL; board++){
256 board->name, board->desc);
259 " -b <file>[:<align>]\n"
260 " add boot extension block to the image\n"
261 " -r <file>[:<align>]\n"
262 " add raw block to the image\n"
263 " -o <file> write output to the file <file>\n"
264 " -h show this screen\n"
275 str2u32(char *arg, uint32_t *val)
281 t = strtoul(arg, &err, 0);
282 if (errno || (err==arg) || ((err != NULL) && *err)) {
292 str2u16(char *arg, uint16_t *val)
298 t = strtoul(arg, &err, 0);
299 if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
308 str2u8(char *arg, uint8_t *val)
314 t = strtoul(arg, &err, 0);
315 if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x100)) {
324 str2sig(char *arg, uint32_t *sig)
326 if (strlen(arg) != 4)
329 *sig = arg[0] | (arg[1] << 8) | (arg[2] << 16) | (arg[3] << 24);
336 parse_arg(char *arg, char *buf, char *argv[])
344 memset(argv, 0, MAX_ARG_COUNT * sizeof(void *));
357 if (argl >= MAX_ARG_LEN) {
358 /* argument is too long */
359 argl = MAX_ARG_LEN-1;
362 memcpy(buf, arg, argl);
365 for (i = 0; i < MAX_ARG_COUNT; i++) {
366 tok = strsep(ap, ":");
371 else if (tok[0] == '\0') {
384 required_arg(char c, char *arg)
386 if (arg == NULL || *arg != '-')
389 ERR("option -%c requires an argument\n", c);
395 is_empty_arg(char *arg)
406 csum_init(struct csum_state *css)
415 csum_update(uint8_t *p, uint32_t len, struct csum_state *css)
421 css->sum += (css->tmp << 8) + p[0];
422 if (css->sum > 0xFFFF) {
431 for ( ; len > 1; len -= 2, p +=2 ) {
432 css->sum += (p[0] << 8) + p[1];
433 if (css->sum > 0xFFFF) {
447 csum_get(struct csum_state *css)
451 csum_update(&pad, 1, css);
456 csum_buf(uint8_t *p, uint32_t len)
458 struct csum_state css;
461 csum_update(p, len, &css);
462 return csum_get(&css);
467 * routines to write data to the output file
470 write_out_data(FILE *outfile, uint8_t *data, size_t len,
471 struct csum_state *css)
475 fwrite(data, len, 1, outfile);
477 ERR("unable to write output file");
482 csum_update(data, len, css);
490 write_out_padding(FILE *outfile, size_t len, uint8_t padc,
491 struct csum_state *css)
494 size_t buflen = sizeof(buf);
496 memset(buf, padc, buflen);
501 if (write_out_data(outfile, buf, buflen, css))
512 write_out_data_align(FILE *outfile, uint8_t *data, size_t len, size_t align,
513 struct csum_state *css)
518 res = write_out_data(outfile, data, len, css);
522 padlen = ALIGN(len,align) - len;
523 res = write_out_padding(outfile, padlen, 0xFF, css);
530 write_out_header(FILE *outfile, struct zyn_rombin_hdr *hdr)
532 struct zyn_rombin_hdr t;
535 if (fseek(outfile, 0, SEEK_SET) != 0) {
536 ERRS("fseek failed on output file");
540 /* setup temporary header fields */
541 memset(&t, 0, sizeof(t));
542 t.addr = HOST_TO_BE32(hdr->addr);
543 memcpy(&t.sig, ROMBIN_SIGNATURE, ROMBIN_SIG_LEN);
545 t.flags = hdr->flags;
546 t.osize = HOST_TO_BE32(hdr->osize);
547 t.csize = HOST_TO_BE32(hdr->csize);
548 t.ocsum = HOST_TO_BE16(hdr->ocsum);
549 t.ccsum = HOST_TO_BE16(hdr->ccsum);
550 t.mmap_addr = HOST_TO_BE32(hdr->mmap_addr);
552 return write_out_data(outfile, (uint8_t *)&t, sizeof(t), NULL);
557 write_out_mmap(FILE *outfile, struct fw_mmap *mmap, struct csum_state *css)
559 struct zyn_mmt_hdr *mh;
560 uint8_t buf[MMAP_DATA_SIZE];
565 memset(buf, 0, sizeof(buf));
567 mh = (struct zyn_mmt_hdr *)buf;
569 /* TODO: needs to recreate the memory map too? */
572 /* Build user data section */
573 data = buf+sizeof(*mh);
574 data += sprintf(data, "Vendor 1 %d", board->vendor);
576 data += sprintf(data, "Model 1 %d", BE16_TO_HOST(board->model));
578 /* TODO: make hardware version configurable? */
579 data += sprintf(data, "HwVerRange 2 %d %d", 0, 0);
582 user_size = (uint8_t *)data - buf;
583 mh->user_start= HOST_TO_BE32(mmap->addr+sizeof(*mh));
584 mh->user_end= HOST_TO_BE32(mmap->addr+user_size);
585 mh->csum = HOST_TO_BE16(csum_buf(buf+sizeof(*mh), user_size));
587 res = write_out_data(outfile, buf, sizeof(buf), css);
594 block_stat_file(struct fw_block *block)
599 if (block->file_name == NULL)
602 res = stat(block->file_name, &st);
604 ERRS("stat failed on %s", block->file_name);
608 block->file_size = st.st_size;
614 write_out_file(FILE *outfile, char *name, size_t len, struct csum_state *css)
616 char buf[FILE_BUF_LEN];
617 size_t buflen = sizeof(buf);
621 DBG(2, "writing out file, name=%s, len=%d",
627 ERRS("unable to open file: %s", name);
635 /* read data from source file */
637 fread(buf, buflen, 1, f);
639 ERRS("unable to read from file: %s",name);
644 res = write_out_data(outfile, buf, buflen, css);
657 write_out_block(FILE *outfile, struct fw_block *block, struct csum_state *css)
664 if (block->file_name == NULL)
667 if (block->file_size == 0)
670 res = write_out_file(outfile, block->file_name,
671 block->file_size, css);
677 write_out_image(FILE *outfile)
679 struct fw_block *block;
681 struct zyn_rombin_hdr hdr;
682 struct csum_state css;
687 /* setup header fields */
688 memset(&hdr, 0, sizeof(hdr));
689 hdr.addr = board->code_start;
690 hdr.type = OBJECT_TYPE_BOOTEXT;
691 hdr.flags = ROMBIN_FLAG_OCSUM;
693 res = write_out_header(outfile, &hdr);
697 offset = sizeof(hdr);
700 res = write_out_block(outfile, bootext_block, &css);
704 offset += bootext_block->file_size;
706 padlen = ALIGN(offset,MMAP_ALIGN) - offset;
707 res = write_out_padding(outfile, padlen, 0xFF, &css);
713 mmap.addr = board->flash_base + board->fw_offs + offset;
714 hdr.mmap_addr = mmap.addr;
715 res = write_out_mmap(outfile, &mmap, &css);
719 offset += MMAP_DATA_SIZE;
720 hdr.osize = offset - sizeof(hdr);
721 hdr.ocsum = csum_get(&css);
723 for (i=0; i < num_blocks; i++) {
726 if (block->type == BLOCK_TYPE_BOOTEXT)
729 padlen = ALIGN(offset,block->align) - offset;
730 res = write_out_padding(outfile, padlen, 0xFF, NULL);
735 res = write_out_block(outfile, block, NULL);
738 offset += block->file_size;
741 res = write_out_header(outfile, &hdr);
748 find_board(char *name)
750 struct board_info *ret;
751 struct board_info *board;
754 for (board = boards; board->name != NULL; board++){
755 if (strcasecmp(name, board->name) == 0) {
766 parse_opt_board(char ch, char *arg)
769 DBG(1,"parsing board option: -%c %s", ch, arg);
772 ERR("only one board option allowed");
776 if (required_arg(ch, arg))
779 board = find_board(arg);
781 ERR("invalid/unknown board specified: %s", arg);
790 parse_opt_ofname(char ch, char *arg)
793 if (ofname != NULL) {
794 ERR("only one output file allowed");
798 if (required_arg(ch, arg))
808 parse_opt_block(char ch, char *arg)
810 char buf[MAX_ARG_LEN];
811 char *argv[MAX_ARG_COUNT];
814 struct fw_block *block;
817 if ( num_blocks >= MAX_NUM_BLOCKS ) {
818 ERR("too many blocks specified");
822 block = &blocks[num_blocks++];
824 /* setup default field values */
830 ERR("only one boot extension block allowed");
833 block->type = BLOCK_TYPE_BOOTEXT;
834 bootext_block = block;
837 block->type = BLOCK_TYPE_RAW;
841 argc = parse_arg(arg, buf, argv);
845 if (is_empty_arg(p)) {
846 ERR("no file specified in %s", arg);
849 block->file_name = strdup(p);
850 if (block->file_name == NULL) {
851 ERR("not enough memory");
856 if(block->type == BLOCK_TYPE_BOOTEXT)
860 if (!is_empty_arg(p)) {
861 if (str2u32(p, &block->align) != 0) {
862 ERR("invalid block align in %s", arg);
872 calc_block_offsets(int type, uint32_t *offset)
874 struct fw_block *block;
879 DBG(1,"calculating block offsets, starting with %lu",
883 for (i = 0; i < num_blocks; i++) {
886 if (block->type != type)
889 next_offs = ALIGN(*offset, block->align);
890 avail = board->flash_size - board->fw_offs - next_offs;
891 if (next_offs + block->file_size > avail) {
892 ERR("file %s is too big, offset = %u, size=%u,"
893 " align = %u", block->file_name,
895 (unsigned)block->file_size,
896 (unsigned)block->align);
901 block->padlen = next_offs - *offset;
902 *offset += block->file_size;
911 struct fw_block *block;
916 /* collecting file stats */
917 for (i = 0; i < num_blocks; i++) {
919 res = block_stat_file(block);
924 offset = board->fw_offs + bootext_block->file_size;
925 res = calc_block_offsets(BLOCK_TYPE_RAW, &offset);
932 main(int argc, char *argv[])
934 int optinvalid = 0; /* flag for invalid option */
936 int res = EXIT_FAILURE;
940 progname=basename(argv[0]);
942 opterr = 0; /* could not print standard getopt error messages */
946 c = getopt(argc, argv, "b:B:ho:r:v");
953 optinvalid = parse_opt_block(c,optarg);
956 optinvalid = parse_opt_board(c,optarg);
959 optinvalid = parse_opt_ofname(c,optarg);
971 if (optinvalid != 0 ) {
972 ERR("invalid option: -%c", optopt);
978 ERR("no board specified");
982 if (ofname == NULL) {
983 ERR("no output file specified");
988 ERR("invalid option: %s", argv[optind]);
993 if (process_blocks() != 0) {
997 outfile = fopen(ofname, "w");
998 if (outfile == NULL) {
999 ERRS("could not open \"%s\" for writing", ofname);
1003 if (write_out_image(outfile) != 0)
1006 DBG(1,"Image file %s completed.", ofname);
1013 if (res != EXIT_SUCCESS) {