the Magicbox doesn't have gigabit ethernet
[openwrt.git] / tools / firmware-utils / src / mkcasfw.c
1 /*
2 * $Id$
3 *
4 * Copyright (C) 2007 OpenWrt.org
5 * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published
9 * by the Free Software Foundation.
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdint.h>
16 #include <string.h>
17 #include <unistd.h> /* for unlink() */
18 #include <libgen.h>
19 #include <getopt.h> /* for getopt() */
20 #include <stdarg.h>
21 #include <errno.h>
22 #include <sys/stat.h>
23 #include <endian.h> /* for __BYTE_ORDER */
24 #if defined(__CYGWIN__)
25 # include <byteswap.h>
26 #endif
27
28 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
29 # define HOST_TO_LE16(x) (x)
30 # define HOST_TO_LE32(x) (x)
31 # define LE16_TO_HOST(x) (x)
32 # define LE32_TO_HOST(x) (x)
33 #else
34 # define HOST_TO_LE16(x) bswap_16(x)
35 # define HOST_TO_LE32(x) bswap_32(x)
36 # define LE16_TO_HOST(x) bswap_16(x)
37 # define LE32_TO_HOST(x) bswap_32(x)
38 #endif
39
40 #define MAX_NUM_BLOCKS 2
41 #define MAX_ARG_COUNT 32
42 #define MAX_ARG_LEN 1024
43 #define FILE_BUF_LEN (16*1024)
44 #define DEFAULT_PADC 0xFF
45
46 #define DEFAULT_BLOCK_ALIGN 0x10000U
47
48 #define CSUM_TYPE_NONE 0
49 #define CSUM_TYPE_8 1
50 #define CSUM_TYPE_16 2
51 #define CSUM_TYPE_32 3
52
53 struct csum_state{
54 int size;
55 uint32_t val;
56 uint32_t tmp;
57 int odd;
58 };
59
60 struct image_desc {
61 int need_file;
62 char *file_name; /* name of the file */
63 uint32_t file_size; /* length of the file */
64
65 uint32_t csum;
66 uint32_t out_size;
67 uint8_t padc;
68 };
69
70 struct fwhdr_nfs {
71 uint32_t type;
72 uint32_t kernel_offs;
73 uint32_t kernel_size;
74 uint32_t fs_offs;
75 uint32_t fs_size;
76 uint32_t kernel_csum;
77 uint32_t fs_csum;
78 uint32_t id;
79 } __attribute__ ((packed));
80
81 struct fwhdr_cas {
82 uint32_t type;
83 uint32_t kernel_offs;
84 uint32_t kernel_size;
85 uint32_t id;
86 uint32_t kernel_csum;
87 uint32_t magic1;
88 uint32_t magic2;
89 uint32_t magic3;
90 } __attribute__ ((packed));
91
92 union file_hdr {
93 struct fwhdr_cas cas;
94 struct fwhdr_nfs nfs;
95 };
96
97 struct board_info {
98 char *model;
99 char *name;
100 int header_type;
101 uint32_t id;
102 uint32_t max_kernel_size;
103 uint32_t max_fs_size;
104 };
105
106 #define HEADER_TYPE_NFS 0
107 #define HEADER_TYPE_CAS 1
108
109 #define KERNEL_SIZE_CAS (61*64*1024)
110 #define KERNEL_SIZE_NFS (52*64*1024)
111 #define FS_SIZE_NFS (9*64*1024)
112
113 #define CAS_MAGIC1 0x5241AA55
114 #define CAS_MAGIC2 0x524F4741
115 #define CAS_MAGIC3 0xD3F22D4E
116
117 /* Cellvision/SparkLAN products */
118 #define MODEL_CAS_630 0x01000000
119 #define MODEL_CAS_630W 0x01000000
120 #define MODEL_CAS_670 0x01000000
121 #define MODEL_CAS_670W 0x01000000
122 #define MODEL_NFS_101U 0x01000000
123 #define MODEL_NFS_101WU 0x01000003
124 #define MODEL_NFS_202U 0x01000001
125 #define MODEL_NFS_202WU 0x01000002
126
127 /* Corega products */
128 #define MODEL_CG_NSADP 0x01000020 /* NFS-101U */
129 #define MODEL_CG_NSADPCR 0x01000021 /* NFS-202U */
130
131 /* D-Link products */
132 #define MODEL_DCS_950 0x01010102 /* CAS-630 */
133 #define MODEL_DCS_950G 0x01020102 /* CAS-630W */
134 #define MODEL_DNS_120 0x01000030 /* NFS-101U */
135 #define MODEL_DNS_G120 0x01000032 /* NFS-101WU */
136
137 /* Digitus products */
138 #define MODEL_DN_16021 MODEL_CAS_630
139 #define MODEL_DN_16022 MODEL_CAS_630W
140 #define MODEL_DN_16030 MODEL_CAS_670
141 #define MODEL_DN_16031 MODEL_CAS_670W
142 #define MODEL_DN_7013 MODEL_NFS_101U
143
144 /* Lobos products */
145 #define MODEL_LB_SS01TXU 0x00000000
146
147 /* Neu-Fusion products */
148
149 /* Ovislink products */
150 #define MODEL_MU_5000FS 0x01000040 /* NFS-101U */
151
152 /* Repotec products */
153
154 /* Sitecom products */
155 #define MODEL_LN_350 /* unknown */
156 #define MODEL_LN_403 0x01020402
157 #define MODEL_WL_401 0x01010402
158
159 /* Surecom products */
160 #define MODEL_EP_4001_MM 0x01030A02 /* CAS-630 */
161 #define MODEL_EP_4002_MM 0x01020A02 /* CAS-630W */
162 #define MODEL_EP_4011_MM 0x01010A02 /* CAS-670 */
163 #define MODEL_EP_4012_MM 0x01000A02 /* CAS-670W */
164 #define MODEL_EP_9812_U /* unknown */
165
166 /* Trendnet products */
167 #define MODEL_TN_U100 0x01000081 /* NFS-101U */
168 #define MODEL_TN_U200 0x01000081 /* NFS-202U */
169
170 /*
171 * Globals
172 */
173 char *progname;
174 char *ofname;
175 int verblevel;
176 int keep_invalid_images;
177 int invalid_causes_error = 1;
178 union file_hdr header;
179
180 struct image_desc kernel_image;
181 struct image_desc fs_image;
182
183 struct board_info *board = NULL;
184
185 #define BOARD(m, n, i, ks, fs, h) { \
186 .model = (m), \
187 .name = (n), \
188 .id = (i), \
189 .max_kernel_size = (ks), \
190 .max_fs_size = (fs), \
191 .header_type = (h) \
192 }
193
194 #define BOARD_CAS(m,n,i) \
195 BOARD(m, n, i, KERNEL_SIZE_CAS, 0, HEADER_TYPE_CAS)
196 #define BOARD_NFS(m,n,i) \
197 BOARD(m, n, i, KERNEL_SIZE_NFS, FS_SIZE_NFS, HEADER_TYPE_NFS)
198
199 static struct board_info boards[] = {
200 /* Cellvision/Sparklan products */
201 BOARD_CAS("CAS-630", "Cellvision CAS-630", MODEL_CAS_630),
202 BOARD_CAS("CAS-630W", "Cellvision CAS-630W", MODEL_CAS_630W),
203 BOARD_CAS("CAS-670", "Cellvision CAS-670", MODEL_CAS_670),
204 BOARD_CAS("CAS-670W", "Cellvision CAS-670W", MODEL_CAS_670W),
205 BOARD_NFS("NFS-101U", "Cellvision NFS-101U", MODEL_NFS_101U),
206 BOARD_NFS("NFS-101WU", "Cellvision NFS-101WU", MODEL_NFS_101WU),
207 BOARD_NFS("NFS-202U", "Cellvision NFS-202U", MODEL_NFS_202U),
208 BOARD_NFS("NFS-202WU", "Cellvision NFS-202WU", MODEL_NFS_202WU),
209
210 /* Corega products */
211 BOARD_NFS("CG-NSADP", "Corega CG-NSADP", MODEL_CG_NSADP),
212 BOARD_NFS("CG-NSADPCR", "Corega CG-NSADPCR", MODEL_CG_NSADPCR),
213
214 /* D-Link products */
215 BOARD_CAS("DCS-950", "D-Link DCS-950", MODEL_DCS_950),
216 BOARD_CAS("DCS-950G", "D-Link DCS-950G", MODEL_DCS_950G),
217 BOARD_NFS("DNS-120", "D-Link DNS-120", MODEL_DNS_120),
218 BOARD_NFS("DNS-G120", "D-Link DNS-G120", MODEL_DNS_G120),
219
220 /* Digitus products */
221 BOARD_NFS("DN-7013", "Digitus DN-7013", MODEL_DN_7013),
222
223 /* Lobos products */
224 BOARD_NFS("LB-SS01TXU", "Lobos LB-SS01TXU", MODEL_LB_SS01TXU),
225
226 /* Ovislink products */
227 BOARD_NFS("MU-5000FS", "Ovislink MU-5000FS", MODEL_MU_5000FS),
228
229 /* Sitecom products */
230 BOARD_CAS("LN-403", "Sitecom LN-403", MODEL_LN_403),
231 BOARD_CAS("WL-401", "Sitecom WL-401", MODEL_WL_401),
232
233 /* Surecom products */
234 BOARD_CAS("EP-4001-MM", "Surecom EP-4001-MM", MODEL_EP_4001_MM),
235 BOARD_CAS("EP-4002-MM", "Surecom EP-4002-MM", MODEL_EP_4002_MM),
236 BOARD_CAS("EP-4011-MM", "Surecom EP-4011-MM", MODEL_EP_4011_MM),
237 BOARD_CAS("EP-4012-MM", "Surecom EP-4012-MM", MODEL_EP_4012_MM),
238
239 /* TrendNET products */
240 BOARD_NFS("TN-U100", "TrendNET TN-U100", MODEL_TN_U100),
241 BOARD_NFS("TN-U200", "TrendNET TN-U200", MODEL_TN_U200),
242
243 {.model = NULL}
244 };
245
246 /*
247 * Message macros
248 */
249 #define ERR(fmt, ...) do { \
250 fflush(0); \
251 fprintf(stderr, "[%s] *** error: " fmt "\n", \
252 progname, ## __VA_ARGS__ ); \
253 } while (0)
254
255 #define ERRS(fmt, ...) do { \
256 int save = errno; \
257 fflush(0); \
258 fprintf(stderr, "[%s] *** error: " fmt "\n", \
259 progname, ## __VA_ARGS__, strerror(save)); \
260 } while (0)
261
262 #define WARN(fmt, ...) do { \
263 fprintf(stderr, "[%s] *** warning: " fmt "\n", \
264 progname, ## __VA_ARGS__ ); \
265 } while (0)
266
267 #define DBG(lev, fmt, ...) do { \
268 if (verblevel < lev) \
269 break;\
270 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
271 } while (0)
272
273 #define ERR_FATAL -1
274 #define ERR_INVALID_IMAGE -2
275
276 void
277 usage(int status)
278 {
279 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
280 struct board_info *board;
281
282 fprintf(stream, "Usage: %s [OPTIONS...] <file>\n", progname);
283 fprintf(stream,
284 "\n"
285 "Options:\n"
286 " -B <board> create image for the board specified with <board>.\n"
287 " valid <board> values:\n"
288 );
289 for (board = boards; board->model != NULL; board++){
290 fprintf(stream,
291 " %-12s: %s\n",
292 board->model, board->name);
293 };
294 fprintf(stream,
295 " -d don't throw error on invalid images\n"
296 " -k keep invalid images\n"
297 " -K <file> add kernel to the image\n"
298 " -C <file> add custom filesystem to the image\n"
299 " -h show this screen\n"
300 "Parameters:\n"
301 " <file> write output to the file <file>\n"
302 );
303
304 exit(status);
305 }
306
307 static inline uint32_t align(uint32_t base, uint32_t alignment)
308 {
309 uint32_t ret;
310
311 if (alignment) {
312 ret = (base + alignment - 1);
313 ret &= ~(alignment-1);
314 } else {
315 ret = base;
316 }
317
318 return ret;
319 }
320
321 /*
322 * argument parsing
323 */
324 int
325 str2u32(char *arg, uint32_t *val)
326 {
327 char *err = NULL;
328 uint32_t t;
329
330 errno=0;
331 t = strtoul(arg, &err, 0);
332 if (errno || (err==arg) || ((err != NULL) && *err)) {
333 return -1;
334 }
335
336 *val = t;
337 return 0;
338 }
339
340
341 int
342 str2u16(char *arg, uint16_t *val)
343 {
344 char *err = NULL;
345 uint32_t t;
346
347 errno=0;
348 t = strtoul(arg, &err, 0);
349 if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
350 return -1;
351 }
352
353 *val = t & 0xFFFF;
354 return 0;
355 }
356
357 int
358 str2u8(char *arg, uint8_t *val)
359 {
360 char *err = NULL;
361 uint32_t t;
362
363 errno=0;
364 t = strtoul(arg, &err, 0);
365 if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x100)) {
366 return -1;
367 }
368
369 *val = t & 0xFF;
370 return 0;
371 }
372
373 int
374 parse_arg(char *arg, char *buf, char *argv[])
375 {
376 int res = 0;
377 size_t argl;
378 char *tok;
379 char **ap = &buf;
380 int i;
381
382 memset(argv, 0, MAX_ARG_COUNT * sizeof(void *));
383
384 if ((arg == NULL)) {
385 /* no arguments */
386 return 0;
387 }
388
389 argl = strlen(arg);
390 if (argl == 0) {
391 /* no arguments */
392 return 0;
393 }
394
395 if (argl >= MAX_ARG_LEN) {
396 /* argument is too long */
397 argl = MAX_ARG_LEN-1;
398 }
399
400 memcpy(buf, arg, argl);
401 buf[argl] = '\0';
402
403 for (i = 0; i < MAX_ARG_COUNT; i++) {
404 tok = strsep(ap, ":");
405 if (tok == NULL) {
406 break;
407 }
408 #if 0
409 else if (tok[0] == '\0') {
410 break;
411 }
412 #endif
413 argv[i] = tok;
414 res++;
415 }
416
417 return res;
418 }
419
420
421 int
422 required_arg(char c, char *arg)
423 {
424 if (arg == NULL || *arg != '-')
425 return 0;
426
427 ERR("option -%c requires an argument\n", c);
428 return ERR_FATAL;
429 }
430
431
432 int
433 is_empty_arg(char *arg)
434 {
435 int ret = 1;
436 if (arg != NULL) {
437 if (*arg) ret = 0;
438 };
439 return ret;
440 }
441
442
443 void
444 csum8_update(uint8_t *p, uint32_t len, struct csum_state *css)
445 {
446 for ( ; len > 0; len --) {
447 css->val += *p++;
448 }
449 }
450
451
452 uint16_t
453 csum8_get(struct csum_state *css)
454 {
455 uint8_t t;
456
457 t = css->val;
458 return ~t + 1;
459 }
460
461
462 void
463 csum16_update(uint8_t *p, uint32_t len, struct csum_state *css)
464 {
465 uint16_t t;
466
467 if (css->odd) {
468 t = css->tmp + (p[0]<<8);
469 css->val += LE16_TO_HOST(t);
470 css->odd = 0;
471 len--;
472 p++;
473 }
474
475 for ( ; len > 1; len -= 2, p +=2 ) {
476 t = p[0] + (p[1] << 8);
477 css->val += LE16_TO_HOST(t);
478 }
479
480 if (len == 1) {
481 css->tmp = p[0];
482 css->odd = 1;
483 }
484 }
485
486
487 uint16_t
488 csum16_get(struct csum_state *css)
489 {
490 char pad = 0;
491
492 csum16_update(&pad, 1, css);
493 return ~css->val + 1;
494 }
495
496 void
497 csum32_update(uint8_t *p, uint32_t len, struct csum_state *css)
498 {
499 uint32_t t;
500
501 for ( ; len > 3; len -= 4, p += 4 ) {
502 t = p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24);
503 css->val ^= t;
504 }
505 }
506
507 uint32_t
508 csum32_get(struct csum_state *css)
509 {
510 return css->val;
511 }
512
513
514 void
515 csum_init(struct csum_state *css, int size)
516 {
517 css->val = 0;
518 css->tmp = 0;
519 css->odd = 0;
520 css->size = size;
521 }
522
523 void
524 csum_update(uint8_t *p, uint32_t len, struct csum_state *css)
525 {
526 switch (css->size) {
527 case CSUM_TYPE_8:
528 csum8_update(p,len,css);
529 break;
530 case CSUM_TYPE_16:
531 csum16_update(p,len,css);
532 break;
533 case CSUM_TYPE_32:
534 csum32_update(p,len,css);
535 break;
536 }
537 }
538
539
540 uint32_t
541 csum_get(struct csum_state *css)
542 {
543 uint32_t ret;
544
545 switch (css->size) {
546 case CSUM_TYPE_8:
547 ret = csum8_get(css);
548 break;
549 case CSUM_TYPE_16:
550 ret = csum16_get(css);
551 break;
552 case CSUM_TYPE_32:
553 ret = csum32_get(css);
554 }
555
556 return ret;
557 }
558
559
560 /*
561 * routines to write data to the output file
562 */
563 int
564 write_out_data(FILE *outfile, uint8_t *data, size_t len,
565 struct csum_state *css)
566 {
567 errno = 0;
568
569 fwrite(data, len, 1, outfile);
570 if (errno) {
571 ERRS("unable to write output file");
572 return ERR_FATAL;
573 }
574
575 if (css) {
576 csum_update(data, len, css);
577 }
578
579 return 0;
580 }
581
582
583 int
584 write_out_padding(FILE *outfile, size_t len, uint8_t padc,
585 struct csum_state *css)
586 {
587 uint8_t buf[512];
588 size_t buflen = sizeof(buf);
589 int err;
590
591 memset(buf, padc, buflen);
592 while (len > 0) {
593 if (len < buflen)
594 buflen = len;
595
596 err = write_out_data(outfile, buf, buflen, css);
597 if (err)
598 return err;
599
600 len -= buflen;
601 }
602
603 return 0;
604 }
605
606
607 int
608 image_stat_file(struct image_desc *desc)
609 {
610 struct stat st;
611 int err;
612
613 if (desc->file_name == NULL)
614 return 0;
615
616 err = stat(desc->file_name, &st);
617 if (err){
618 ERRS("stat failed on %s", desc->file_name);
619 return ERR_FATAL;
620 }
621
622 if (st.st_size > desc->out_size) {
623 WARN("file %s is too big, will be truncated to %d bytes\n",
624 desc->file_name, desc->out_size);
625 desc->file_size = desc->out_size;
626 return ERR_INVALID_IMAGE;
627 }
628
629
630 desc->file_size = st.st_size;
631 desc->out_size = align(desc->file_size,1);
632 return 0;
633 }
634
635
636 int
637 image_writeout_file(FILE *outfile, struct image_desc *desc,
638 struct csum_state *css)
639 {
640 char buf[FILE_BUF_LEN];
641 size_t buflen = sizeof(buf);
642 FILE *f;
643 size_t len;
644 int res;
645
646 if (desc->file_name == NULL)
647 return 0;
648
649 if (desc->file_size == 0)
650 return 0;
651
652 errno = 0;
653 f = fopen(desc->file_name,"r");
654 if (errno) {
655 ERRS("unable to open file: %s", desc->file_name);
656 return ERR_FATAL;
657 }
658
659 len = desc->file_size;
660 while (len > 0) {
661 if (len < buflen)
662 buflen = len;
663
664 /* read data from source file */
665 errno = 0;
666 fread(buf, buflen, 1, f);
667 if (errno != 0) {
668 ERRS("unable to read from file: %s", desc->file_name);
669 res = ERR_FATAL;
670 break;
671 }
672
673 res = write_out_data(outfile, buf, buflen, css);
674 if (res)
675 break;
676
677 len -= buflen;
678 }
679
680 fclose(f);
681 return res;
682 }
683
684
685 int
686 image_writeout(FILE *outfile, struct image_desc *desc)
687 {
688 int res;
689 struct csum_state css;
690 size_t padlen;
691
692 res = 0;
693
694 if (!desc->file_size)
695 return 0;
696
697 DBG(2, "writing image, file=%s, file_size=%d\n",
698 desc->file_name, desc->file_size);
699
700 csum_init(&css, CSUM_TYPE_32);
701
702 res = image_writeout_file(outfile, desc, &css);
703 if (res)
704 return res;
705
706 /* write padding data if neccesary */
707 padlen = desc->out_size - desc->file_size;
708 DBG(1,"padding desc, length=%d", padlen);
709 res = write_out_padding(outfile, padlen, desc->padc, &css);
710
711 desc->csum = csum_get(&css);
712
713 return res;
714 }
715
716
717 int
718 write_out_header(FILE *outfile)
719 {
720 union file_hdr tmp;
721 int res;
722
723 errno = 0;
724 if (fseek(outfile, 0, SEEK_SET) != 0) {
725 ERRS("fseek failed on output file");
726 return ERR_FATAL;
727 }
728
729 switch (board->header_type) {
730 case HEADER_TYPE_CAS:
731 tmp.cas.type = HOST_TO_LE32(header.cas.type);
732 tmp.cas.id = HOST_TO_LE32(header.cas.id);
733 tmp.cas.kernel_offs = HOST_TO_LE32(sizeof(tmp.cas));
734 tmp.cas.kernel_size = HOST_TO_LE32(kernel_image.out_size);
735 tmp.cas.kernel_csum = HOST_TO_LE32(kernel_image.csum);
736 tmp.cas.magic1 = HOST_TO_LE32(CAS_MAGIC1);
737 tmp.cas.magic2 = HOST_TO_LE32(CAS_MAGIC2);
738 tmp.cas.magic3 = HOST_TO_LE32(CAS_MAGIC3);
739 res = write_out_data(outfile, (uint8_t *)&tmp.cas,
740 sizeof(tmp.cas), NULL);
741 break;
742 case HEADER_TYPE_NFS:
743 tmp.nfs.type = HOST_TO_LE32(header.nfs.type);
744 tmp.nfs.id = HOST_TO_LE32(header.nfs.id);
745 tmp.nfs.kernel_offs = HOST_TO_LE32(sizeof(tmp.nfs));
746 tmp.nfs.kernel_size = HOST_TO_LE32(kernel_image.out_size);
747 tmp.nfs.kernel_csum = HOST_TO_LE32(kernel_image.csum);
748 tmp.nfs.fs_offs = HOST_TO_LE32(sizeof(tmp.nfs)
749 + kernel_image.out_size);
750 tmp.nfs.fs_size = HOST_TO_LE32(fs_image.out_size);
751 tmp.nfs.fs_csum = HOST_TO_LE32(fs_image.csum);
752 res = write_out_data(outfile, (uint8_t *)&tmp.nfs,
753 sizeof(tmp.nfs), NULL);
754 break;
755 }
756
757 return res;
758 }
759
760 int
761 write_out_images(FILE *outfile)
762 {
763 struct image_desc *desc;
764 int i, res;
765
766 res = image_writeout(outfile, &kernel_image);
767 if (res)
768 return res;
769
770 res = image_writeout(outfile, &fs_image);
771 if (res)
772 return res;
773
774 return 0;
775 }
776
777
778 struct board_info *
779 find_board(char *model)
780 {
781 struct board_info *ret;
782 struct board_info *board;
783
784 ret = NULL;
785 for (board = boards; board->model != NULL; board++){
786 if (strcasecmp(model, board->model) == 0) {
787 ret = board;
788 break;
789 }
790 };
791
792 return ret;
793 }
794
795
796 int
797 parse_opt_board(char ch, char *arg)
798 {
799
800 DBG(1,"parsing board option: -%c %s", ch, arg);
801
802 if (board != NULL) {
803 ERR("only one board option allowed");
804 return ERR_FATAL;
805 }
806
807 if (required_arg(ch, arg))
808 return ERR_FATAL;
809
810 board = find_board(arg);
811 if (board == NULL){
812 ERR("invalid/unknown board specified: %s", arg);
813 return ERR_FATAL;
814 }
815
816 switch (board->header_type) {
817 case HEADER_TYPE_CAS:
818 header.cas.type = HEADER_TYPE_CAS;
819 header.cas.id = board->id;
820 break;
821 case HEADER_TYPE_NFS:
822 header.nfs.type = HEADER_TYPE_NFS;
823 header.nfs.id = board->id;
824 break;
825 default:
826 ERR("internal error, unknown header type\n");
827 return ERR_FATAL;
828 }
829
830 return 0;
831 }
832
833
834 int
835 parse_opt_image(char ch, char *arg)
836 {
837 char buf[MAX_ARG_LEN];
838 char *argv[MAX_ARG_COUNT];
839 int argc;
840 char *p;
841 struct image_desc *desc = NULL;
842 int i;
843
844 switch (ch) {
845 case 'K':
846 if (kernel_image.file_name) {
847 WARN("only one kernel option allowed");
848 break;
849 }
850 desc = &kernel_image;
851 break;
852 case 'F':
853 if (fs_image.file_name) {
854 WARN("only one fs option allowed");
855 break;
856 }
857 desc = &fs_image;
858 break;
859 }
860
861 if (!desc)
862 return ERR_FATAL;
863
864 argc = parse_arg(arg, buf, argv);
865
866 i = 0;
867 p = argv[i++];
868 if (!is_empty_arg(p)) {
869 desc->file_name = strdup(p);
870 if (desc->file_name == NULL) {
871 ERR("not enough memory");
872 return ERR_FATAL;
873 }
874 } else {
875 ERR("no file specified for option %c", ch);
876 return ERR_FATAL;
877 }
878
879 return 0;
880 }
881
882
883 int
884 process_images(void)
885 {
886 struct image_desc *desc;
887 uint32_t offs = 0;
888 int i;
889 int res;
890
891 kernel_image.out_size = board->max_kernel_size;
892 kernel_image.padc = DEFAULT_PADC;
893 res = image_stat_file(&kernel_image);
894 if (res)
895 return res;
896
897 if (!fs_image.file_name)
898 return 0;
899
900 fs_image.out_size = board->max_fs_size;
901 fs_image.padc = DEFAULT_PADC;
902 res = image_stat_file(&fs_image);
903 if (res)
904 return res;
905
906 return 0;
907 }
908
909
910 int
911 main(int argc, char *argv[])
912 {
913 int optinvalid = 0; /* flag for invalid option */
914 int c;
915 int res = ERR_FATAL;
916
917 FILE *outfile;
918
919 progname=basename(argv[0]);
920
921 opterr = 0; /* could not print standard getopt error messages */
922 while ( 1 ) {
923 optinvalid = 0;
924
925 c = getopt(argc, argv, "B:C:dhK:r:vw:x:");
926 if (c == -1)
927 break;
928
929 switch (c) {
930 case 'B':
931 optinvalid = parse_opt_board(c,optarg);
932 break;
933 case 'd':
934 invalid_causes_error = 0;
935 break;
936 case 'C':
937 case 'K':
938 optinvalid = parse_opt_image(c,optarg);
939 break;
940 case 'k':
941 keep_invalid_images = 1;
942 break;
943 case 'v':
944 verblevel++;
945 break;
946 case 'h':
947 usage(EXIT_SUCCESS);
948 break;
949 default:
950 optinvalid = 1;
951 break;
952 }
953 if (optinvalid != 0 ){
954 ERR("invalid option: -%c", optopt);
955 goto out;
956 }
957 }
958
959 if (board == NULL) {
960 ERR("no board specified");
961 goto out;
962 }
963
964 if (optind == argc) {
965 ERR("no output file specified");
966 goto out;
967 }
968
969 ofname = argv[optind++];
970
971 if (optind < argc) {
972 ERR("invalid option: %s", argv[optind]);
973 goto out;
974 }
975
976 res = process_images();
977 if (res == ERR_FATAL)
978 goto out;
979
980 if (res == ERR_INVALID_IMAGE) {
981 if (invalid_causes_error)
982 res = ERR_FATAL;
983
984 if (keep_invalid_images == 0) {
985 WARN("generation of invalid images disabled", ofname);
986 goto out;
987 }
988
989 WARN("generating invalid image", ofname);
990 }
991
992 outfile = fopen(ofname, "w");
993 if (outfile == NULL) {
994 ERRS("could not open \"%s\" for writing", ofname);
995 res = ERR_FATAL;
996 goto out;
997 }
998
999 if (write_out_header(outfile) != 0) {
1000 res = ERR_FATAL;
1001 goto out_flush;
1002 }
1003
1004 if (write_out_images(outfile) != 0) {
1005 res = ERR_FATAL;
1006 goto out_flush;
1007 }
1008
1009 if (write_out_header(outfile) != 0) {
1010 res = ERR_FATAL;
1011 goto out_flush;
1012 }
1013
1014 DBG(1,"Image file %s completed.", ofname);
1015
1016 out_flush:
1017 fflush(outfile);
1018 fclose(outfile);
1019 if (res == ERR_FATAL) {
1020 unlink(ofname);
1021 }
1022 out:
1023 if (res == ERR_FATAL)
1024 return EXIT_FAILURE;
1025
1026 return EXIT_SUCCESS;
1027 }
This page took 0.107087 seconds and 5 git commands to generate.