[kernel] the hash_conntrack is no longer broken on ARM since 2.6.25.4
[openwrt.git] / tools / firmware-utils / src / mkzynfw.c
1 /*
2 * $Id$
3 *
4 * Copyright (C) 2007-2008 OpenWrt.org
5 * Copyright (C) 2007-2008 Gabor Juhos <juhosg at openwrt.org>
6 *
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
10 *
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.
14 *
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdint.h>
20 #include <string.h>
21 #include <unistd.h> /* for unlink() */
22 #include <libgen.h>
23 #include <getopt.h> /* for getopt() */
24 #include <stdarg.h>
25 #include <errno.h>
26 #include <sys/stat.h>
27 #include <endian.h> /* for __BYTE_ORDER */
28 #if defined(__CYGWIN__)
29 # include <byteswap.h>
30 #endif
31
32 #include "zynos.h"
33
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)
43 #else
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)
52 #endif
53
54 #define ALIGN(x,y) (((x)+((y)-1)) & ~((y)-1))
55
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)
60
61
62 struct csum_state{
63 int odd;
64 uint32_t sum;
65 uint32_t tmp;
66 };
67
68 struct fw_block {
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 */
74 uint32_t padlen;
75 uint8_t padc;
76 };
77
78 #define BLOCK_TYPE_BOOTEXT 0
79 #define BLOCK_TYPE_RAW 1
80
81 struct fw_mmap {
82 uint32_t addr;
83 uint32_t size;
84 uint32_t user_addr;
85 uint32_t user_size;
86 };
87 #define MMAP_DATA_SIZE 1024
88 #define MMAP_ALIGN 16
89
90 struct board_info {
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 */
99 };
100
101 /*
102 * Globals
103 */
104 char *progname;
105 char *ofname = NULL;
106 int verblevel = 0;
107
108 struct board_info *board = NULL;
109
110 struct fw_block blocks[MAX_NUM_BLOCKS];
111 struct fw_block *bootext_block = NULL;
112 int num_blocks = 0;
113
114 #define ADM5120_FLASH_BASE 0xBFC00000
115 #define ADM5120_CODE_START 0x80008000
116
117 /* TODO: check values for AR7 */
118 #define AR7_FLASH_BASE 0xB0000000
119 #define AR7_CODE_START 0x94008000
120
121 #define ATHEROS_FLASH_BASE 0xBFC00000
122 #define ATHEROS_CODE_START 0x80e00000
123
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) \
129 }
130
131 #define ADMBOARD1(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
132 ADM5120_FLASH_BASE, fs, ADM5120_CODE_START, 0x8000)
133
134 #define ADMBOARD2(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
135 ADM5120_FLASH_BASE, fs, ADM5120_CODE_START, 0x10000)
136
137 #define AR7BOARD1(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
138 AR7_FLASH_BASE, fs, AR7_CODE_START, 0x8000)
139
140 #define ATHEROSBOARD1(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
141 ATHEROS_FLASH_BASE, fs, ATHEROS_CODE_START, 0x30000)
142
143 static struct board_info boards[] = {
144 /*
145 * Infineon/ADMtek ADM5120 based boards
146 */
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),
166
167 {
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,
176 },
177
178 #if 0
179 /*
180 * Texas Instruments AR7 based boards
181 */
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),
198 #endif
199 {
200 .name = "O2SURF",
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,
208 },
209
210 /*
211 :x
212 */
213 ATHEROSBOARD1("NBG-318S", "ZyXEL NBG-318S", ZYNOS_MODEL_NBG_318S, 4),
214
215 {.name = NULL}
216 };
217
218 /*
219 * Message macros
220 */
221 #define ERR(fmt, ...) do { \
222 fflush(0); \
223 fprintf(stderr, "[%s] *** error: " fmt "\n", \
224 progname, ## __VA_ARGS__ ); \
225 } while (0)
226
227 #define ERRS(fmt, ...) do { \
228 int save = errno; \
229 fflush(0); \
230 fprintf(stderr, "[%s] *** error: " fmt ", %s\n", \
231 progname, ## __VA_ARGS__, strerror(save)); \
232 } while (0)
233
234 #define WARN(fmt, ...) do { \
235 fprintf(stderr, "[%s] *** warning: " fmt "\n", \
236 progname, ## __VA_ARGS__ ); \
237 } while (0)
238
239 #define DBG(lev, fmt, ...) do { \
240 if (verblevel < lev) \
241 break;\
242 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
243 } while (0)
244
245 #define ERR_FATAL -1
246 #define ERR_INVALID_IMAGE -2
247
248 /*
249 * Helper routines
250 */
251 void
252 usage(int status)
253 {
254 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
255 struct board_info *board;
256
257 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
258 fprintf(stream,
259 "\n"
260 "Options:\n"
261 " -B <board> create image for the board specified with <board>.\n"
262 " valid <board> values:\n"
263 );
264 for (board = boards; board->name != NULL; board++){
265 fprintf(stream,
266 " %-12s= %s\n",
267 board->name, board->desc);
268 };
269 fprintf(stream,
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"
276 );
277
278 exit(status);
279 }
280
281
282 /*
283 * argument parsing
284 */
285 int
286 str2u32(char *arg, uint32_t *val)
287 {
288 char *err = NULL;
289 uint32_t t;
290
291 errno=0;
292 t = strtoul(arg, &err, 0);
293 if (errno || (err==arg) || ((err != NULL) && *err)) {
294 return -1;
295 }
296
297 *val = t;
298 return 0;
299 }
300
301
302 int
303 str2u16(char *arg, uint16_t *val)
304 {
305 char *err = NULL;
306 uint32_t t;
307
308 errno=0;
309 t = strtoul(arg, &err, 0);
310 if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
311 return -1;
312 }
313
314 *val = t & 0xFFFF;
315 return 0;
316 }
317
318 int
319 str2u8(char *arg, uint8_t *val)
320 {
321 char *err = NULL;
322 uint32_t t;
323
324 errno=0;
325 t = strtoul(arg, &err, 0);
326 if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x100)) {
327 return -1;
328 }
329
330 *val = t & 0xFF;
331 return 0;
332 }
333
334 int
335 str2sig(char *arg, uint32_t *sig)
336 {
337 if (strlen(arg) != 4)
338 return -1;
339
340 *sig = arg[0] | (arg[1] << 8) | (arg[2] << 16) | (arg[3] << 24);
341
342 return 0;
343 }
344
345
346 int
347 parse_arg(char *arg, char *buf, char *argv[])
348 {
349 int res = 0;
350 size_t argl;
351 char *tok;
352 char **ap = &buf;
353 int i;
354
355 memset(argv, 0, MAX_ARG_COUNT * sizeof(void *));
356
357 if ((arg == NULL)) {
358 /* no arguments */
359 return 0;
360 }
361
362 argl = strlen(arg);
363 if (argl == 0) {
364 /* no arguments */
365 return 0;
366 }
367
368 if (argl >= MAX_ARG_LEN) {
369 /* argument is too long */
370 argl = MAX_ARG_LEN-1;
371 }
372
373 memcpy(buf, arg, argl);
374 buf[argl] = '\0';
375
376 for (i = 0; i < MAX_ARG_COUNT; i++) {
377 tok = strsep(ap, ":");
378 if (tok == NULL) {
379 break;
380 }
381 #if 0
382 else if (tok[0] == '\0') {
383 break;
384 }
385 #endif
386 argv[i] = tok;
387 res++;
388 }
389
390 return res;
391 }
392
393
394 int
395 required_arg(char c, char *arg)
396 {
397 if (arg == NULL || *arg != '-')
398 return 0;
399
400 ERR("option -%c requires an argument\n", c);
401 return -1;
402 }
403
404
405 int
406 is_empty_arg(char *arg)
407 {
408 int ret = 1;
409 if (arg != NULL) {
410 if (*arg) ret = 0;
411 };
412 return ret;
413 }
414
415
416 void
417 csum_init(struct csum_state *css)
418 {
419 css->odd = 0;
420 css->sum = 0;
421 css->tmp = 0;
422 }
423
424
425 void
426 csum_update(uint8_t *p, uint32_t len, struct csum_state *css)
427 {
428 if (len == 0)
429 return;
430
431 if (css->odd) {
432 css->sum += (css->tmp << 8) + p[0];
433 if (css->sum > 0xFFFF) {
434 css->sum += 1;
435 css->sum &= 0xFFFF;
436 }
437 css->odd = 0;
438 len--;
439 p++;
440 }
441
442 for ( ; len > 1; len -= 2, p +=2 ) {
443 css->sum += (p[0] << 8) + p[1];
444 if (css->sum > 0xFFFF) {
445 css->sum += 1;
446 css->sum &= 0xFFFF;
447 }
448 }
449
450 if (len == 1){
451 css->tmp = p[0];
452 css->odd = 1;
453 }
454 }
455
456
457 uint16_t
458 csum_get(struct csum_state *css)
459 {
460 char pad = 0;
461
462 csum_update(&pad, 1, css);
463 return css->sum;
464 }
465
466 uint16_t
467 csum_buf(uint8_t *p, uint32_t len)
468 {
469 struct csum_state css;
470
471 csum_init(&css);
472 csum_update(p, len, &css);
473 return csum_get(&css);
474
475 }
476
477 /*
478 * routines to write data to the output file
479 */
480 int
481 write_out_data(FILE *outfile, uint8_t *data, size_t len,
482 struct csum_state *css)
483 {
484 errno = 0;
485
486 fwrite(data, len, 1, outfile);
487 if (errno) {
488 ERR("unable to write output file");
489 return -1;
490 }
491
492 if (css) {
493 csum_update(data, len, css);
494 }
495
496 return 0;
497 }
498
499
500 int
501 write_out_padding(FILE *outfile, size_t len, uint8_t padc,
502 struct csum_state *css)
503 {
504 uint8_t buf[512];
505 size_t buflen = sizeof(buf);
506
507 memset(buf, padc, buflen);
508 while (len > 0) {
509 if (len < buflen)
510 buflen = len;
511
512 if (write_out_data(outfile, buf, buflen, css))
513 return -1;
514
515 len -= buflen;
516 }
517
518 return 0;
519 }
520
521
522 int
523 write_out_data_align(FILE *outfile, uint8_t *data, size_t len, size_t align,
524 struct csum_state *css)
525 {
526 size_t padlen;
527 int res;
528
529 res = write_out_data(outfile, data, len, css);
530 if (res)
531 return res;
532
533 padlen = ALIGN(len,align) - len;
534 res = write_out_padding(outfile, padlen, 0xFF, css);
535
536 return res;
537 }
538
539
540 int
541 write_out_header(FILE *outfile, struct zyn_rombin_hdr *hdr)
542 {
543 struct zyn_rombin_hdr t;
544
545 errno = 0;
546 if (fseek(outfile, 0, SEEK_SET) != 0) {
547 ERRS("fseek failed on output file");
548 return -1;
549 }
550
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);
555 t.type = hdr->type;
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);
562
563 return write_out_data(outfile, (uint8_t *)&t, sizeof(t), NULL);
564 }
565
566
567 int
568 write_out_mmap(FILE *outfile, struct fw_mmap *mmap, struct csum_state *css)
569 {
570 struct zyn_mmt_hdr *mh;
571 uint8_t buf[MMAP_DATA_SIZE];
572 uint32_t user_size;
573 char *data;
574 int res;
575
576 memset(buf, 0, sizeof(buf));
577
578 mh = (struct zyn_mmt_hdr *)buf;
579
580 /* TODO: needs to recreate the memory map too? */
581 mh->count=0;
582
583 /* Build user data section */
584 data = buf+sizeof(*mh);
585 data += sprintf(data, "Vendor 1 %d", board->vendor);
586 *data++ = '\0';
587 data += sprintf(data, "Model 1 %d", BE16_TO_HOST(board->model));
588 *data++ = '\0';
589 /* TODO: make hardware version configurable? */
590 data += sprintf(data, "HwVerRange 2 %d %d", 0, 0);
591 *data++ = '\0';
592
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));
597
598 res = write_out_data(outfile, buf, sizeof(buf), css);
599
600 return res;
601 }
602
603
604 int
605 block_stat_file(struct fw_block *block)
606 {
607 struct stat st;
608 int res;
609
610 if (block->file_name == NULL)
611 return 0;
612
613 res = stat(block->file_name, &st);
614 if (res){
615 ERRS("stat failed on %s", block->file_name);
616 return res;
617 }
618
619 block->file_size = st.st_size;
620 return 0;
621 }
622
623
624 int
625 write_out_file(FILE *outfile, char *name, size_t len, struct csum_state *css)
626 {
627 char buf[FILE_BUF_LEN];
628 size_t buflen = sizeof(buf);
629 FILE *f;
630 int res;
631
632 DBG(2, "writing out file, name=%s, len=%d",
633 name, len);
634
635 errno = 0;
636 f = fopen(name,"r");
637 if (errno) {
638 ERRS("unable to open file: %s", name);
639 return -1;
640 }
641
642 while (len > 0) {
643 if (len < buflen)
644 buflen = len;
645
646 /* read data from source file */
647 errno = 0;
648 fread(buf, buflen, 1, f);
649 if (errno != 0) {
650 ERRS("unable to read from file: %s",name);
651 res = -1;
652 break;
653 }
654
655 res = write_out_data(outfile, buf, buflen, css);
656 if (res)
657 break;
658
659 len -= buflen;
660 }
661
662 fclose(f);
663 return res;
664 }
665
666
667 int
668 write_out_block(FILE *outfile, struct fw_block *block, struct csum_state *css)
669 {
670 int res;
671
672 if (block == NULL)
673 return 0;
674
675 if (block->file_name == NULL)
676 return 0;
677
678 if (block->file_size == 0)
679 return 0;
680
681 res = write_out_file(outfile, block->file_name,
682 block->file_size, css);
683 return res;
684 }
685
686
687 int
688 write_out_image(FILE *outfile)
689 {
690 struct fw_block *block;
691 struct fw_mmap mmap;
692 struct zyn_rombin_hdr hdr;
693 struct csum_state css;
694 int i, res;
695 uint32_t offset;
696 uint32_t padlen;
697
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;
703
704 res = write_out_header(outfile, &hdr);
705 if (res)
706 return res;
707
708 offset = sizeof(hdr);
709
710 csum_init(&css);
711 res = write_out_block(outfile, bootext_block, &css);
712 if (res)
713 return res;
714
715 offset += bootext_block->file_size;
716
717 padlen = ALIGN(offset,MMAP_ALIGN) - offset;
718 res = write_out_padding(outfile, padlen, 0xFF, &css);
719 if (res)
720 return res;
721
722 offset += padlen;
723
724 mmap.addr = board->flash_base + board->romio_offs + offset;
725 hdr.mmap_addr = mmap.addr;
726 res = write_out_mmap(outfile, &mmap, &css);
727 if (res)
728 return res;
729
730 offset += MMAP_DATA_SIZE;
731 hdr.osize = offset - sizeof(hdr);
732 hdr.ocsum = csum_get(&css);
733
734 for (i=0; i < num_blocks; i++) {
735 block = &blocks[i];
736
737 if (block->type == BLOCK_TYPE_BOOTEXT)
738 continue;
739
740 padlen = ALIGN(offset,block->align) - offset;
741 res = write_out_padding(outfile, padlen, 0xFF, NULL);
742 if (res)
743 return res;
744 offset += padlen;
745
746 res = write_out_block(outfile, block, NULL);
747 if (res)
748 return res;
749 offset += block->file_size;
750 }
751
752 res = write_out_header(outfile, &hdr);
753
754 return res;
755 }
756
757
758 struct board_info *
759 find_board(char *name)
760 {
761 struct board_info *ret;
762 struct board_info *board;
763
764 ret = NULL;
765 for (board = boards; board->name != NULL; board++){
766 if (strcasecmp(name, board->name) == 0) {
767 ret = board;
768 break;
769 }
770 };
771
772 return ret;
773 }
774
775
776 int
777 parse_opt_board(char ch, char *arg)
778 {
779
780 DBG(1,"parsing board option: -%c %s", ch, arg);
781
782 if (board != NULL) {
783 ERR("only one board option allowed");
784 return -1;
785 }
786
787 if (required_arg(ch, arg))
788 return -1;
789
790 board = find_board(arg);
791 if (board == NULL){
792 ERR("invalid/unknown board specified: %s", arg);
793 return -1;
794 }
795
796 return 0;
797 }
798
799
800 int
801 parse_opt_ofname(char ch, char *arg)
802 {
803
804 if (ofname != NULL) {
805 ERR("only one output file allowed");
806 return -1;
807 }
808
809 if (required_arg(ch, arg))
810 return -1;
811
812 ofname = arg;
813
814 return 0;
815 }
816
817
818 int
819 parse_opt_block(char ch, char *arg)
820 {
821 char buf[MAX_ARG_LEN];
822 char *argv[MAX_ARG_COUNT];
823 int argc;
824 char *p;
825 struct fw_block *block;
826 int i;
827
828 if ( num_blocks >= MAX_NUM_BLOCKS ) {
829 ERR("too many blocks specified");
830 return -1;
831 }
832
833 block = &blocks[num_blocks++];
834
835 /* setup default field values */
836 block->padc = 0xFF;
837
838 switch(ch) {
839 case 'b':
840 if (bootext_block) {
841 ERR("only one boot extension block allowed");
842 break;
843 }
844 block->type = BLOCK_TYPE_BOOTEXT;
845 bootext_block = block;
846 break;
847 case 'r':
848 block->type = BLOCK_TYPE_RAW;
849 break;
850 }
851
852 argc = parse_arg(arg, buf, argv);
853
854 i = 0;
855 p = argv[i++];
856 if (is_empty_arg(p)) {
857 ERR("no file specified in %s", arg);
858 return -1;
859 } else {
860 block->file_name = strdup(p);
861 if (block->file_name == NULL) {
862 ERR("not enough memory");
863 return -1;
864 }
865 }
866
867 if(block->type == BLOCK_TYPE_BOOTEXT)
868 return 0;
869
870 p = argv[i++];
871 if (!is_empty_arg(p)) {
872 if (str2u32(p, &block->align) != 0) {
873 ERR("invalid block align in %s", arg);
874 return -1;
875 }
876 }
877
878 return 0;
879 }
880
881
882 int
883 calc_block_offsets(int type, uint32_t *offset)
884 {
885 struct fw_block *block;
886 uint32_t next_offs;
887 uint32_t avail;
888 int i, res;
889
890 DBG(1,"calculating block offsets, starting with %lu",
891 *offset);
892
893 res = 0;
894 for (i = 0; i < num_blocks; i++) {
895 block = &blocks[i];
896
897 if (block->type != type)
898 continue;
899
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,
905 (unsigned)next_offs,
906 (unsigned)block->file_size,
907 (unsigned)block->align);
908 res = -1;
909 break;
910 }
911
912 block->padlen = next_offs - *offset;
913 *offset += block->file_size;
914 }
915
916 return res;
917 }
918
919 int
920 process_blocks(void)
921 {
922 struct fw_block *block;
923 uint32_t offset;
924 int i;
925 int res;
926
927 /* collecting file stats */
928 for (i = 0; i < num_blocks; i++) {
929 block = &blocks[i];
930 res = block_stat_file(block);
931 if (res)
932 return res;
933 }
934
935 offset = board->romio_offs + bootext_block->file_size;
936 res = calc_block_offsets(BLOCK_TYPE_RAW, &offset);
937
938 return res;
939 }
940
941
942 int
943 main(int argc, char *argv[])
944 {
945 int optinvalid = 0; /* flag for invalid option */
946 int c;
947 int res = EXIT_FAILURE;
948
949 FILE *outfile;
950
951 progname=basename(argv[0]);
952
953 opterr = 0; /* could not print standard getopt error messages */
954 while ( 1 ) {
955 optinvalid = 0;
956
957 c = getopt(argc, argv, "b:B:ho:r:v");
958 if (c == -1)
959 break;
960
961 switch (c) {
962 case 'b':
963 case 'r':
964 optinvalid = parse_opt_block(c,optarg);
965 break;
966 case 'B':
967 optinvalid = parse_opt_board(c,optarg);
968 break;
969 case 'o':
970 optinvalid = parse_opt_ofname(c,optarg);
971 break;
972 case 'v':
973 verblevel++;
974 break;
975 case 'h':
976 usage(EXIT_SUCCESS);
977 break;
978 default:
979 optinvalid = 1;
980 break;
981 }
982 if (optinvalid != 0 ) {
983 ERR("invalid option: -%c", optopt);
984 goto out;
985 }
986 }
987
988 if (board == NULL) {
989 ERR("no board specified");
990 goto out;
991 }
992
993 if (ofname == NULL) {
994 ERR("no output file specified");
995 goto out;
996 }
997
998 if (optind < argc) {
999 ERR("invalid option: %s", argv[optind]);
1000 goto out;
1001 }
1002
1003
1004 if (process_blocks() != 0) {
1005 goto out;
1006 }
1007
1008 outfile = fopen(ofname, "w");
1009 if (outfile == NULL) {
1010 ERRS("could not open \"%s\" for writing", ofname);
1011 goto out;
1012 }
1013
1014 if (write_out_image(outfile) != 0)
1015 goto out_flush;
1016
1017 DBG(1,"Image file %s completed.", ofname);
1018
1019 res = EXIT_SUCCESS;
1020
1021 out_flush:
1022 fflush(outfile);
1023 fclose(outfile);
1024 if (res != EXIT_SUCCESS) {
1025 unlink(ofname);
1026 }
1027 out:
1028 return res;
1029 }
This page took 0.123037 seconds and 5 git commands to generate.