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