summaryrefslogtreecommitdiffstats
path: root/tools/firmware-utils/src/mkzynfw.c
blob: 130d95a8c037dba9fac085bd0644e03d80c300eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
/*
 *  $Id$
 *
 *  Copyright (C) 2007 OpenWrt.org
 *  Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
 *
 *  This code was based on the information of the ZyXEL's firmware
 *  image format written by Kolja Waschk, can be found at:
 *  http://www.ixo.de/info/zyxel_uclinux
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the
 *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA  02110-1301, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>     /* for unlink() */
#include <libgen.h>
#include <getopt.h>     /* for getopt() */
#include <stdarg.h>
#include <errno.h>
#include <sys/stat.h>
#include <endian.h>     /* for __BYTE_ORDER */
#if defined(__CYGWIN__)
#  include <byteswap.h>
#endif

#include "zynos.h"

#if (__BYTE_ORDER == __LITTLE_ENDIAN)
#  define HOST_TO_LE16(x)	(x)
#  define HOST_TO_LE32(x)	(x)
#  define LE16_TO_HOST(x)	(x)
#  define LE32_TO_HOST(x)	(x)
#  define HOST_TO_BE16(x)	bswap_16(x)
#  define HOST_TO_BE32(x)	bswap_32(x)
#  define BE16_TO_HOST(x)	bswap_16(x)
#  define BE32_TO_HOST(x)	bswap_32(x)
#else
#  define HOST_TO_BE16(x)	(x)
#  define HOST_TO_BE32(x)	(x)
#  define BE16_TO_HOST(x)	(x)
#  define BE32_TO_HOST(x)	(x)
#  define HOST_TO_LE16(x)	bswap_16(x)
#  define HOST_TO_LE32(x)	bswap_32(x)
#  define LE16_TO_HOST(x)	bswap_16(x)
#  define LE32_TO_HOST(x)	bswap_32(x)
#endif

#define ALIGN(x,y)	(((x)+((y)-1)) & ~((y)-1))

#define MAX_NUM_BLOCKS	8
#define MAX_ARG_COUNT	32
#define MAX_ARG_LEN	1024
#define FILE_BUF_LEN	(16*1024)


struct csum_state{
	int		odd;
	uint32_t	sum;
	uint32_t	tmp;
};

struct fw_block {
	uint32_t	align;		/* alignment of this block */
	char		*file_name;	/* name of the file */
	uint32_t	file_size;	/* length of the file */
	char		*mmap_name;	/* name in the MMAP table */
	int		type;		/* block type */
	uint32_t	padlen;
	uint8_t		padc;
};

#define BLOCK_TYPE_BOOTEXT	0
#define BLOCK_TYPE_RAW		1

struct fw_mmap {
	uint32_t	addr;
	uint32_t	size;
	uint32_t	user_addr;
	uint32_t	user_size;
};
#define MMAP_DATA_SIZE	1024
#define MMAP_ALIGN	16

struct board_info {
	char *name;		/* model name */
	char *desc;		/* description */
	uint16_t model;		/* model id */
	uint32_t flash_base;	/* flash base address */
	uint32_t flash_size;	/* board flash size */
	uint32_t code_start;	/* code start address */
	uint32_t fw_offs;	/* offset of the firmware within the flash */
};

/*
 * Globals
 */
char *progname;
char *ofname = NULL;
int verblevel = 0;

struct board_info *board = NULL;

struct fw_block blocks[MAX_NUM_BLOCKS];
struct fw_block *bootext_block = NULL;
int num_blocks = 0;

#define ADM5120_FLASH_BASE	0xBFC00000
#define ADM5120_CODE_START	0x80008000

/* TODO: check values for AR7 */
#define AR7_FLASH_BASE		0xB0000000
#define AR7_CODE_START		0x94008000

#define BOARD(n, d, m, fb, fs, cs, fo) { \
	.name = (n), .desc=(d), .model = (m), \
	.flash_base = (fb), .flash_size = (fs)<<20, \
	.code_start = (cs), .fw_offs = (fo) \
	}

#define ADMBOARD1(n, d, m, fs) BOARD(n, d, m, \
	ADM5120_FLASH_BASE, fs, ADM5120_CODE_START, 0x8000)

#define ADMBOARD2(n, d, m, fs) BOARD(n, d, m, \
	ADM5120_FLASH_BASE, fs, ADM5120_CODE_START, 0x10000)

#define AR7BOARD1(n, d, m, fs) BOARD(n, d, m, \
	AR7_FLASH_BASE, fs, AR7_CODE_START, 0x8000)

static struct board_info boards[] = {
	/*
	 * Infineon/ADMtek ADM5120 based boards
	 */
	ADMBOARD2("ES-2024A",	"ZyXEL ES-2024A", ZYNOS_MODEL_ES_2024A, 4),
	ADMBOARD2("ES-2024PWR",	"ZyXEL ES-2024PWR", ZYNOS_MODEL_ES_2024PWR, 4),
	ADMBOARD2("ES-2108",	"ZyXEL ES-2108", ZYNOS_MODEL_ES_2108, 4),
	ADMBOARD2("ES-2108-F",	"ZyXEL ES-2108-F", ZYNOS_MODEL_ES_2108_F, 4),
	ADMBOARD2("ES-2108-G",	"ZyXEL ES-2108-G", ZYNOS_MODEL_ES_2108_G, 4),
	ADMBOARD2("ES-2108-LC",	"ZyXEL ES-2108-LC", ZYNOS_MODEL_ES_2108_LC, 4),
	ADMBOARD2("ES-2108PWR",	"ZyXEL ES-2108PWR", ZYNOS_MODEL_ES_2108PWR, 4),
	ADMBOARD1("HS-100",	"ZyXEL HomeSafe 100", ZYNOS_MODEL_HS_100, 2),
	ADMBOARD1("HS-100W",	"ZyXEL HomeSafe 100W", ZYNOS_MODEL_HS_100W, 2),
	ADMBOARD1("P-334",	"ZyXEL Prestige 334", ZYNOS_MODEL_P_334, 2),
	ADMBOARD1("P-334U",	"ZyXEL Prestige 334U", ZYNOS_MODEL_P_334U, 4),
	ADMBOARD1("P-334W",	"ZyXEL Prestige 334W", ZYNOS_MODEL_P_334W, 2),
	ADMBOARD1("P-334WH",	"ZyXEL Prestige 334WH", ZYNOS_MODEL_P_334WH, 4),
	ADMBOARD1("P-334WHD",	"ZyXEL Prestige 334WHD", ZYNOS_MODEL_P_334WHD, 4),
	ADMBOARD1("P-334WT",	"ZyXEL Prestige 334WT", ZYNOS_MODEL_P_334WT, 4),
	ADMBOARD1("P-335",	"ZyXEL Prestige 335", ZYNOS_MODEL_P_335, 4),
	ADMBOARD1("P-335Plus",	"ZyXEL Prestige 335Plus", ZYNOS_MODEL_P_335PLUS, 4),
	ADMBOARD1("P-335U",	"ZyXEL Prestige 335U", ZYNOS_MODEL_P_335U, 4),
	ADMBOARD1("P-335WT",	"ZyXEL Prestige 335WT", ZYNOS_MODEL_P_335WT, 4),
#if 0
	/*
	 * Texas Instruments AR7 based boards
	 */
	AR7BOARD1("P-660H-61",  "ZyXEL P-660H-61", ZYNOS_MODEL_P_660H_61, 2),
	AR7BOARD1("P-660H-63",  "ZyXEL P-660H-63", ZYNOS_MODEL_P_660H_63, 2),
	AR7BOARD1("P-660H-D1",  "ZyXEL P-660H-D1", ZYNOS_MODEL_P_660H_D1, 2),
	AR7BOARD1("P-660H-D3",  "ZyXEL P-660H-D3", ZYNOS_MODEL_P_660H_D3, 2),
	AR7BOARD1("P-660HW-61", "ZyXEL P-660HW-61", ZYNOS_MODEL_P_660HW_61, 2),
	AR7BOARD1("P-660HW-63", "ZyXEL P-660HW-63", ZYNOS_MODEL_P_660HW_63, 2),
	AR7BOARD1("P-660HW-67", "ZyXEL P-660HW-67", ZYNOS_MODEL_P_660HW_67, 2),
	AR7BOARD1("P-660HW-D1", "ZyXEL P-660HW-D1", ZYNOS_MODEL_P_660HW_D1, 2),
	AR7BOARD1("P-660HW-D3", "ZyXEL P-660HW-D3", ZYNOS_MODEL_P_660HW_D3, 2),
	AR7BOARD1("P-660R-61",  "ZyXEL P-660R-61", ZYNOS_MODEL_P_660R_61, 2),
	AR7BOARD1("P-660R-61C", "ZyXEL P-660R-61C", ZYNOS_MODEL_P_660R_61C, 2),
	AR7BOARD1("P-660R-63",  "ZyXEL P-660R-63", ZYNOS_MODEL_P_660R_63, 2),
	AR7BOARD1("P-660R-63C", "ZyXEL P-660R-63C", ZYNOS_MODEL_P_660R_63C, 2),
	AR7BOARD1("P-660R-67",  "ZyXEL P-660R-67", ZYNOS_MODEL_P_660R_67, 2),
	AR7BOARD1("P-660R-D1",  "ZyXEL P-660R-D1", ZYNOS_MODEL_P_660R_D1, 2),
	AR7BOARD1("P-660R-D3",  "ZyXEL P-660R-D3", ZYNOS_MODEL_P_660R_D3, 2),
#endif
	{.name = NULL}
};

/*
 * Message macros
 */
#define ERR(fmt, ...) do { \
	fflush(0); \
	fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ ); \
} while (0)

#define ERRS(fmt, ...) do { \
	int save = errno; \
	fflush(0); \
	fprintf(stderr, "[%s] *** error: " fmt ", %s\n", progname, ## __VA_ARGS__ \
		, strerror(save)); \
} while (0)

#define WARN(fmt, ...) do { \
	fprintf(stderr, "[%s] *** warning: " fmt "\n", progname, ## __VA_ARGS__ ); \
} while (0)

#define DBG(lev, fmt, ...) do { \
	if (verblevel < lev) \
		break;\
	fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
} while (0)

#define ERR_FATAL		-1
#define ERR_INVALID_IMAGE	-2

/*
 * Helper routines
 */
void
usage(int status)
{
	FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
	struct board_info *board;

	fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
	fprintf(stream,
"\n"
"Options:\n"
"  -B <board>      create image for the board specified with <board>.\n"
"                  valid <board> values:\n"
	);
	for (board = boards; board->name != NULL; board++){
		fprintf(stream,
"                    %-12s= %s\n",
		 board->name, board->desc);
	};
	fprintf(stream,
"  -b <file>[:<align>]\n"
"                  add boot extension block to the image\n"
"  -r <file>[:<align>]\n"
"                  add raw block to the image\n"
"  -o <file>       write output to the file <file>\n"
"  -h              show this screen\n"
	);

	exit(status);
}


/*
 * argument parsing
 */
int
str2u32(char *arg, uint32_t *val)
{
	char *err = NULL;
	uint32_t t;

	errno=0;
	t = strtoul(arg, &err, 0);
	if (errno || (err==arg) || ((err != NULL) && *err)) {
		return -1;
	}

	*val = t;
	return 0;
}


int
str2u16(char *arg, uint16_t *val)
{
	char *err = NULL;
	uint32_t t;

	errno=0;
	t = strtoul(arg, &err, 0);
	if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
		return -1;
	}

	*val = t & 0xFFFF;
	return 0;
}

int
str2u8(char *arg, uint8_t *val)
{
	char *err = NULL;
	uint32_t t;

	errno=0;
	t = strtoul(arg, &err, 0);
	if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x100)) {
		return -1;
	}

	*val = t & 0xFF;
	return 0;
}

int
str2sig(char *arg, uint32_t *sig)
{
	if (strlen(arg) != 4)
		return -1;

	*sig = arg[0] | (arg[1] << 8) | (arg[2] << 16) | (arg[3] << 24);

	return 0;
}


int
parse_arg(char *arg, char *buf, char *argv[])
{
	int res = 0;
	size_t argl;
	char *tok;
	char **ap = &buf;
	int i;

	memset(argv, 0, MAX_ARG_COUNT * sizeof(void *));

	if ((arg == NULL)) {
		/* no arguments */
		return 0;
	}

	argl = strlen(arg);
	if (argl == 0) {
		/* no arguments */
		return 0;
	}

	if (argl >= MAX_ARG_LEN) {
		/* argument is too long */
		argl = MAX_ARG_LEN-1;
	}

	memcpy(buf, arg, argl);
	buf[argl] = '\0';

	for (i = 0; i < MAX_ARG_COUNT; i++) {
		tok = strsep(ap, ":");
		if (tok == NULL) {
			break;
		}
#if 0
		else if (tok[0] == '\0') {
			break;
		}
#endif
		argv[i] = tok;
		res++;
	}

	return res;
}


int
required_arg(char c, char *arg)
{
	if (arg == NULL || *arg != '-')
		return 0;

	ERR("option -%c requires an argument\n", c);
	return -1;
}


int
is_empty_arg(char *arg)
{
	int ret = 1;
	if (arg != NULL) {
		if (*arg) ret = 0;
	};
	return ret;
}


void
csum_init(struct csum_state *css)
{
	css->odd = 0;
	css->sum = 0;
	css->tmp = 0;
}


void
csum_update(uint8_t *p, uint32_t len, struct csum_state *css)
{
	if (len == 0)
		return;

	if (css->odd) {
		css->sum += (css->tmp << 8) + p[0];
		if (css->sum > 0xFFFF) {
			css->sum += 1;
			css->sum &= 0xFFFF;
		}
		css->odd = 0;
		len--;
		p++;
	}

	for ( ; len > 1; len -= 2, p +=2 ) {
		css->sum  += (p[0] << 8) + p[1];
		if (css->sum > 0xFFFF) {
			css->sum += 1;
			css->sum &= 0xFFFF;
		}
	}

	if (len == 1){
		css->tmp = p[0];
		css->odd = 1;
	}
}


uint16_t
csum_get(struct csum_state *css)
{
	char pad = 0;

	csum_update(&pad, 1, css);
	return css->sum;
}

uint16_t
csum_buf(uint8_t *p, uint32_t len)
{
	struct csum_state css;

	csum_init(&css);
	csum_update(p, len, &css);
	return csum_get(&css);

}

/*
 * routines to write data to the output file
 */
int
write_out_data(FILE *outfile, uint8_t *data, size_t len,
		struct csum_state *css)
{
	errno = 0;

	fwrite(data, len, 1, outfile);
	if (errno) {
		ERR("unable to write output file");
		return -1;
	}

	if (css) {
		csum_update(data, len, css);
	}

	return 0;
}


int
write_out_padding(FILE *outfile, size_t len, uint8_t padc,
		 struct csum_state *css)
{
	uint8_t buf[512];
	size_t buflen = sizeof(buf);

	memset(buf, padc, buflen);
	while (len > 0) {
		if (len < buflen)
			buflen = len;

		if (write_out_data(outfile, buf, buflen, css))
			return -1;

		len -= buflen;
	}

	return 0;
}


int
write_out_data_align(FILE *outfile, uint8_t *data, size_t len, size_t align,
		struct csum_state *css)
{
	size_t padlen;
	int res;

	res = write_out_data(outfile, data, len, css);
	if (res)
		return res;

	padlen = ALIGN(len,align) - len;
	res = write_out_padding(outfile, padlen, 0xFF, css);

	return res;
}


int
write_out_header(FILE *outfile, struct zyn_rombin_hdr *hdr)
{
	struct zyn_rombin_hdr t;

	errno = 0;
	if (fseek(outfile, 0, SEEK_SET) != 0) {
		ERRS("fseek failed on output file");
		return -1;
	}

	/* setup temporary header fields */
	memset(&t,0, sizeof(t));
	t.addr = HOST_TO_BE32(hdr->addr);
	memcpy(&t.sig, ROMBIN_SIGNATURE, ROMBIN_SIG_LEN);
	t.type = hdr->type;
	t.flags = hdr->flags;
	t.osize = HOST_TO_BE32(hdr->osize);
	t.csize = HOST_TO_BE32(hdr->csize);
	t.ocsum = HOST_TO_BE16(hdr->ocsum);
	t.ccsum = HOST_TO_BE16(hdr->ccsum);
	t.mmap_addr = HOST_TO_BE32(hdr->mmap_addr);

	return write_out_data(outfile, (uint8_t *)&t, sizeof(t), NULL);
}


int
write_out_mmap(FILE *outfile, struct fw_mmap *mmap, struct csum_state *css)
{
	struct zyn_mmt_hdr *mh;
	uint8_t buf[MMAP_DATA_SIZE];
	uint32_t user_size;
	char *data;
	int res;

	memset(buf,0,sizeof(buf));

	mh = (struct zyn_mmt_hdr *)buf;

	/* TODO: needs to recreate the memory map too? */
	mh->count=0;

	/* Build user data section */
	data = buf+sizeof(*mh);
	data += sprintf(data,"Model 1 %d", BE16_TO_HOST(board->model));
	*data++ = '\0';
	/* TODO: make hardware version configurable? */
	data += sprintf(data,"HwVerRange 2 %d %d", 0, 0);
	*data++ = '\0';

	user_size = (uint8_t *)data - buf;
	mh->user_start= HOST_TO_BE32(mmap->addr+sizeof(*mh));
	mh->user_end= HOST_TO_BE32(mmap->addr+user_size);
	mh->csum = HOST_TO_BE16(csum_buf(buf+sizeof(*mh), user_size));

	res = write_out_data(outfile, buf, sizeof(buf), css);

	return res;
}


int
block_stat_file(struct fw_block *block)
{
	struct stat st;
	int res;

	if (block->file_name == NULL)
		return 0;

	res = stat(block->file_name, &st);
	if (res){
		ERRS("stat failed on %s", block->file_name);
		return res;
	}

	block->file_size = st.st_size;
	return 0;
}


int
write_out_file(FILE *outfile, char *name, size_t len, struct csum_state *css)
{
	char buf[FILE_BUF_LEN];
	size_t buflen = sizeof(buf);
	FILE *f;
	int res;

	DBG(2, "writing out file, name=%s, len=%d",
		name, len);

	errno = 0;
	f = fopen(name,"r");
	if (errno) {
		ERRS("unable to open file: %s", name);
		return -1;
	}

	while (len > 0) {
		if (len < buflen)
			buflen = len;

		/* read data from source file */
		errno = 0;
		fread(buf, buflen, 1, f);
		if (errno != 0) {
			ERRS("unable to read from file: %s",name);
			res = -1;
			break;
		}

		res = write_out_data(outfile, buf, buflen, css);
		if (res)
			break;

		len -= buflen;
	}

	fclose(f);
	return res;
}


int
write_out_block(FILE *outfile, struct fw_block *block, struct csum_state *css)
{
	int res;

	if (block == NULL)
		return 0;

	if (block->file_name == NULL)
		return 0;

	if (block->file_size == 0)
		return 0;

	res = write_out_file(outfile, block->file_name,
			block->file_size, css);
	return res;
}


int
write_out_image(FILE *outfile)
{
	struct fw_block *block;
	struct fw_mmap mmap;
	struct zyn_rombin_hdr hdr;
	struct csum_state css;
	int i, res;
	uint32_t offset;
	uint32_t padlen;

	/* setup header fields */
	hdr.addr = board->code_start;
	hdr.type = OBJECT_TYPE_BOOTEXT;
	hdr.flags = ROMBIN_FLAG_OCSUM;

	res = write_out_header(outfile, &hdr);
	if (res)
		return res;

	offset = sizeof(hdr);

	csum_init(&css);
	res = write_out_block(outfile, bootext_block, &css);
	if (res)
		return res;

	offset += bootext_block->file_size;

	padlen = ALIGN(offset,MMAP_ALIGN) - offset;
	res = write_out_padding(outfile, padlen, 0xFF, &css);
	if (res)
		return res;

	offset += padlen;

	mmap.addr = board->flash_base + board->fw_offs + offset;
	hdr.mmap_addr = mmap.addr;
	res = write_out_mmap(outfile, &mmap, &css);
	if (res)
		return res;

	offset += MMAP_DATA_SIZE;
	hdr.osize = offset - sizeof(hdr);
	hdr.ocsum = csum_get(&css);

	for (i=0; i < num_blocks; i++) {
		block = &blocks[i];

		if (block->type == BLOCK_TYPE_BOOTEXT)
			continue;

		padlen = ALIGN(offset,block->align) - offset;
		res = write_out_padding(outfile, padlen, 0xFF, NULL);
		if (res)
			return res;
		offset += padlen;

		res = write_out_block(outfile, block, NULL);
		if (res)
			return res;
		offset += block->file_size;
	}

	res = write_out_header(outfile, &hdr);

	return res;
}


struct board_info *
find_board(char *name)
{
	struct board_info *ret;
	struct board_info *board;

	ret = NULL;
	for (board = boards; board->name != NULL; board++){
		if (strcasecmp(name, board->name) == 0) {
			ret = board;
			break;
		}
	};

	return ret;
}


int
parse_opt_board(char ch, char *arg)
{

	DBG(1,"parsing board option: -%c %s", ch, arg);

	if (board != NULL) {
		ERR("only one board option allowed");
		return -1;
	}

	if (required_arg(ch, arg))
		return -1;

	board = find_board(arg);
	if (board == NULL){
		ERR("invalid/unknown board specified: %s", arg);
		return -1;
	}

	return 0;
}


int
parse_opt_ofname(char ch, char *arg)
{

	if (ofname != NULL) {
		ERR("only one output file allowed");
		return -1;
	}

	if (required_arg(ch, arg))
		return -1;

	ofname = arg;

	return 0;
}


int
parse_opt_block(char ch, char *arg)
{
	char buf[MAX_ARG_LEN];
	char *argv[MAX_ARG_COUNT];
	int argc;
	char *p;
	struct fw_block *block;
	int i;

	if ( num_blocks > MAX_NUM_BLOCKS ) {
		ERR("too many blocks specified");
		return -1;
	}

	block = &blocks[num_blocks++];

	/* setup default field values */
	block->padc = 0xFF;

	switch(ch) {
	case 'b':
		if (bootext_block) {
			ERR("only one boot extension block allowed");
			break;
		}
		block->type = BLOCK_TYPE_BOOTEXT;
		bootext_block = block;
		break;
	case 'r':
		block->type = BLOCK_TYPE_RAW;
		break;
	}

	argc = parse_arg(arg, buf, argv);

	i = 0;
	p = argv[i++];
	if (is_empty_arg(p)) {
		ERR("no file specified in %s", arg);
		return -1;
	} else {
		block->file_name = strdup(p);
		if (block->file_name == NULL) {
			ERR("not enough memory");
			return -1;
		}
	}

	if(block->type == BLOCK_TYPE_BOOTEXT)
		return 0;

	p = argv[i++];
	if (!is_empty_arg(p)) {
		if (str2u32(p, &block->align) != 0) {
			ERR("invalid block align in %s", arg);
			return -1;
		}
	}

	return 0;
}


int
calc_block_offsets(int type, uint32_t *offset)
{
	struct fw_block *block;
	uint32_t next_offs;
	uint32_t avail;
	int i, res;

	DBG(1,"calculating block offsets, starting with %lu",
		*offset);

	res = 0;
	for (i = 0; i < num_blocks; i++) {
		block = &blocks[i];

		if (block->type != type)
			continue;

		next_offs = ALIGN(*offset, block->align);
		avail = board->flash_size - board->fw_offs - next_offs;
		if (next_offs + block->file_size > avail) {
			ERR("file %s is too big, offset = %u, size=%u,"
				" align = %u", block->file_name,
				(unsigned)next_offs,
				(unsigned)block->file_size,
				(unsigned)block->align);
			res = -1;
			break;
		}

		block->padlen = next_offs - *offset;
		*offset += block->file_size;
	}

	return res;
}

int
process_blocks(void)
{
	struct fw_block *block;
	uint32_t offset;
	int i;
	int res;

	/* collecting file stats */
	for (i = 0; i < num_blocks; i++) {
		block = &blocks[i];
		res = block_stat_file(block);
		if (res)
			return res;
	}

	offset = board->fw_offs + bootext_block->file_size;
	res = calc_block_offsets(BLOCK_TYPE_RAW, &offset);

	return res;
}


int
main(int argc, char *argv[])
{
	int optinvalid = 0;   /* flag for invalid option */
	int c;
	int res = EXIT_FAILURE;

	FILE *outfile;

	progname=basename(argv[0]);

	opterr = 0;  /* could not print standard getopt error messages */
	while ( 1 ) {
		optinvalid = 0;

		c = getopt(argc, argv, "b:B:ho:r:v");
		if (c == -1)
			break;

		switch (c) {
		case 'b':
		case 'r':
			optinvalid = parse_opt_block(c,optarg);
			break;
		case 'B':
			optinvalid = parse_opt_board(c,optarg);
			break;
		case 'o':
			optinvalid = parse_opt_ofname(c,optarg);
			break;
		case 'v':
			verblevel++;
			break;
		case 'h':
			usage(EXIT_SUCCESS);
			break;
		default:
			optinvalid = 1;
			break;
		}
		if (optinvalid != 0 ) {
			ERR("invalid option: -%c", optopt);
			goto out;
		}
	}

	if (board == NULL) {
		ERR("no board specified");
		goto out;
	}

	if (ofname == NULL) {
		ERR("no output file specified");
		goto out;
	}

	if (optind < argc) {
		ERR("invalid option: %s", argv[optind]);
		goto out;
	}


	if (process_blocks() != 0) {
		goto out;
	}

	outfile = fopen(ofname, "w");
	if (outfile == NULL) {
		ERRS("could not open \"%s\" for writing", ofname);
		goto out;
	}

	if (write_out_image(outfile) != 0)
		goto out_flush;

	DBG(1,"Image file %s completed.", ofname);

	res = EXIT_SUCCESS;

out_flush:
	fflush(outfile);
	fclose(outfile);
	if (res != EXIT_SUCCESS) {
		unlink(ofname);
	}
out:
	return res;
}