summaryrefslogtreecommitdiffstats
path: root/scripts/zynq_mkimage.c
blob: a096b834d1568806dac50245e4d2e08f2eb3f69a (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
/*
 * Copyright (C) 2013 Steffen Trumtrar <s.trumtrar@pengutronix.de>
 *
 * 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.
 */

#include <endian.h>
#include <errno.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

static void usage(char *name)
{
	printf("Usage: %s barebox-flash-image <outfile>\n", name);
}

int main(int argc, char *argv[])
{
	FILE *ifile, *ofile;
	unsigned int *buf;
	const char *infile;
	const char *outfile;
	struct stat st;
	unsigned int i;
	unsigned long sum = 0;

	if (argc != 3) {
		usage(argv[0]);
		exit(1);
	}

	infile = argv[1];
	outfile = argv[2];

	if (stat(infile, &st) == -1) {
		perror("stat");
		exit(EXIT_FAILURE);
	}

	buf = malloc(st.st_size);
	if (!buf) {
		fprintf(stderr, "Unable to allocate buffer\n");
		return -1;
	}
	ifile = fopen(infile, "rb");
	if (!ifile) {
		fprintf(stderr, "Cannot open %s for reading\n",
			infile);
		free(buf);
		exit(EXIT_FAILURE);
	}
	ofile = fopen(outfile, "wb");
	if (!ofile) {
		fprintf(stderr, "Cannot open %s for writing\n",
			outfile);
		fclose(ifile);
		free(buf);
		exit(EXIT_FAILURE);
	}

	fread(buf, 4, st.st_size, ifile);

	for (i = 0x8; i < 0x12; i++)
		sum += htole32(buf[i]);

	sum = ~sum;
	buf[i] = sum;

	fwrite(buf, st.st_size / 4, 4, ofile);

	fclose(ofile);
	fclose(ifile);
	free(buf);

	return 0;
}