summaryrefslogtreecommitdiffstats
path: root/sdma-gen-image.c
blob: 69a2b0a1327ecbd1d12f6654a62bab9d68a93b00 (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
/*
 * sdma-gen-image - generate Freescale i.MX SDMA firmware images suitable
 * for Linux
 *
 * Copyright (c) 2010 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <endian.h>

#include "sdma-firmware.h"

static struct sdma_firmware *firmwares[] = {
	&sdma_imx25_to1,
	&sdma_imx31_to1,
	&sdma_imx31_to2,
	&sdma_imx35_to1,
	&sdma_imx35_to2,
	&sdma_imx51,
	&sdma_imx53,
};

static int gen_one(struct sdma_firmware *fw)
{
	struct sdma_firmware_header header;
	int ret;
	char *name;
	FILE *f;

	ret = asprintf(&name, "%s.bin", fw->name);
	if (ret < 0)
		return ret;

	printf("generating %s\n", name);

	header.magic = htole32(SDMA_FIRMWARE_MAGIC);
	header.version_major = htole32(1);
	header.version_minor = htole32(fw->version);
	header.script_addrs_start = htole32(sizeof(header));
	header.num_script_addrs = htole32(sizeof(struct sdma_script_start_addrs) / sizeof(uint32_t));
	header.ram_code_start = htole32(header.script_addrs_start + sizeof(struct sdma_script_start_addrs));
	header.ram_code_size = htole32(fw->blob_size * sizeof(uint16_t));

	f = fopen(name, "w");
	if (!f) {
		perror("fopen");
		ret = -1;
		goto out_free;
	}

	ret = fwrite(&header, sizeof(header), 1, f);
	if (ret != 1)
		goto out_free;
	ret = fwrite(fw->addr, sizeof(struct sdma_script_start_addrs), 1, f);
	if (ret != 1)
		goto out_free;
	ret = fwrite(fw->blob, header.ram_code_size, 1, f);
	if (ret != 1)
		goto out_free;

	fclose(f);

	return 0;

out_free:
	free(name);

	return ret;
}

int main(void)
{
	int i, ret;

	for (i = 0; i < ARRAY_SIZE(firmwares); i++) {
		ret = gen_one(firmwares[i]);
		if (ret)
			exit(1);
	}

	return 0;
}