summaryrefslogtreecommitdiffstats
path: root/scripts/mk-am35xx-spi-image.c
blob: ec311fdd4d76d2a9921c69b80caaf935bf8f051a (plain) (blame)
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
/*
 * mk-am35xx-spi-image.c - convert a barebox image for SPI loading on AM35xx
 *
 * Copyright (C) 2012 Jan Luebbe <j.luebbe@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.
 *
 * 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.
 */
/**
 * @file
 * @brief convert a barebox image for SPI loading on AM35xx
 *
 * FileName: scripts/mk-am35xx-spi-image.c
 *
 * Booting from SPI on an AM35xx (and possibly other TI SOCs) requires
 * a special format:
 *
 * - 32 bit image size in big-endian
 * - 32 bit load address in big-endian
 * - binary image converted from little- to big-endian
 *
 * This tool converts barebox.bin to the required format.
 */

#define _BSD_SOURCE

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <limits.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <endian.h>

void usage(char *prgname)
{
	printf("usage: %s [OPTION] FILE > IMAGE\n"
	       "\n"
	       "options:\n"
	       "  -a <address> memory address for the loaded image in SRAM\n",
	       prgname);
}

int main(int argc, char *argv[])
{
	FILE *input;
	int opt;
	off_t pos;
	size_t size;
	uint32_t addr = 0x40200000;
	uint32_t temp;

	while((opt = getopt(argc, argv, "a:")) != -1) {
		switch (opt) {
		case 'a':
			addr = strtoul(optarg, NULL, 0);
			break;
		}
	}

	if (optind >= argc) {
		usage(argv[0]);
		exit(1);
	}

	input = fopen(argv[optind], "r");
	if (input == NULL) {
		perror("fopen");
		exit(EXIT_FAILURE);
	}

	if (fseeko(input, 0, SEEK_END) == -1) {
		perror("fseeko");
		exit(EXIT_FAILURE);
	}

	pos = ftello(input);
	if (pos == -1) {
		perror("ftello");
		exit(EXIT_FAILURE);
	}
	if (pos % 4) {
		printf("error: image size must be a multiple of 4 bytes\n");
		exit(EXIT_FAILURE);
	}
	if (pos > 0x100000) {
		printf("error: image should be smaller than 1 MiB\n");
		exit(EXIT_FAILURE);
	}

	if (fseeko(input, 0, SEEK_SET) == -1) {
		perror("fseeko");
		exit(EXIT_FAILURE);
	}

	/* image size */
	temp = htobe32((uint32_t)pos);
	fwrite(&temp, sizeof(uint32_t), 1, stdout);

	/* memory address */
	temp = htobe32(addr);
	fwrite(&temp, sizeof(uint32_t), 1, stdout);

	for (;;) {
		size = fread(&temp, 1, sizeof(uint32_t), input);
		if (!size)
			break;
		if (size != 4) {
			perror("fread");
			exit(EXIT_FAILURE);
		}
		temp = htobe32(le32toh(temp));
		if (fwrite(&temp, 1, sizeof(uint32_t), stdout) != 4) {
			perror("fwrite");
			exit(EXIT_FAILURE);
		}
	}

	if (fclose(input) != 0) {
		perror("fclose");
		exit(EXIT_FAILURE);
	}

	exit(EXIT_SUCCESS);
}