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