summaryrefslogtreecommitdiffstats
path: root/tools/aisimage.c
blob: 04fb649899bd7beb9698aab044f74eb8125cdd1f (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
/*
 * (C) Copyright 2011
 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include "mkimage.h"
#include "aisimage.h"
#include <image.h>

#define IS_FNC_EXEC(c)	(cmd_table[c].AIS_cmd == AIS_CMD_FNLOAD)
#define WORD_ALIGN0	4
#define WORD_ALIGN(len) (((len)+WORD_ALIGN0-1) & ~(WORD_ALIGN0-1))
#define MAX_CMD_BUFFER	4096

static uint32_t ais_img_size;

/*
 * Supported commands for configuration file
 */
static table_entry_t aisimage_cmds[] = {
	{CMD_DATA,		"DATA",		"Reg Write Data"},
	{CMD_FILL,		"FILL",		"Fill range with pattern"},
	{CMD_CRCON,		"CRCON",	"CRC Enable"},
	{CMD_CRCOFF,		"CRCOFF",	"CRC Disable"},
	{CMD_CRCCHECK,		"CRCCHECK",	"CRC Validate"},
	{CMD_JMPCLOSE,		"JMPCLOSE",	"Jump & Close"},
	{CMD_JMP,		"JMP",		"Jump"},
	{CMD_SEQREAD,		"SEQREAD",	"Sequential read"},
	{CMD_PLL0,		"PLL0",		"PLL0"},
	{CMD_PLL1,		"PLL1",		"PLL1"},
	{CMD_CLK,		"CLK",		"Clock configuration"},
	{CMD_DDR2,		"DDR2",		"DDR2 Configuration"},
	{CMD_EMIFA,		"EMIFA",	"EMIFA"},
	{CMD_EMIFA_ASYNC,	"EMIFA_ASYNC",	"EMIFA Async"},
	{CMD_PLL,		"PLL",		"PLL & Clock configuration"},
	{CMD_PSC,		"PSC",		"PSC setup"},
	{CMD_PINMUX,		"PINMUX",	"Pinmux setup"},
	{CMD_BOOTTABLE,		"BOOT_TABLE",	"Boot table command"},
	{-1,			"",		""},
};

static struct ais_func_exec {
	uint32_t index;
	uint32_t argcnt;
} ais_func_table[] = {
	[CMD_PLL0] = {0, 2},
	[CMD_PLL1] = {1, 2},
	[CMD_CLK] = {2, 1},
	[CMD_DDR2] = {3, 8},
	[CMD_EMIFA] = {4, 5},
	[CMD_EMIFA_ASYNC] = {5, 5},
	[CMD_PLL] = {6, 3},
	[CMD_PSC] = {7, 1},
	[CMD_PINMUX] = {8, 3}
};

static struct cmd_table_t {
	uint32_t nargs;
	uint32_t AIS_cmd;
} cmd_table[] = {
	[CMD_FILL]	 =	{	4,	AIS_CMD_FILL},
	[CMD_CRCON]	=	{	0,	AIS_CMD_ENCRC},
	[CMD_CRCOFF]	=	{	0,	AIS_CMD_DISCRC},
	[CMD_CRCCHECK]	=	{	2,	AIS_CMD_ENCRC},
	[CMD_JMPCLOSE]	=	{	1,	AIS_CMD_JMPCLOSE},
	[CMD_JMP]	=	{	1,	AIS_CMD_JMP},
	[CMD_SEQREAD]	=	{	0,	AIS_CMD_SEQREAD},
	[CMD_PLL0]	=	{	2,	AIS_CMD_FNLOAD},
	[CMD_PLL1]	=	{	2,	AIS_CMD_FNLOAD},
	[CMD_CLK]	=	{	1,	AIS_CMD_FNLOAD},
	[CMD_DDR2]	=	{	8,	AIS_CMD_FNLOAD},
	[CMD_EMIFA]	=	{	5,	AIS_CMD_FNLOAD},
	[CMD_EMIFA_ASYNC] =	{	5,	AIS_CMD_FNLOAD},
	[CMD_PLL]	=	{	3,	AIS_CMD_FNLOAD},
	[CMD_PSC]	=	{	1,	AIS_CMD_FNLOAD},
	[CMD_PINMUX]	=	{	3,	AIS_CMD_FNLOAD},
	[CMD_BOOTTABLE]	=	{	4,	AIS_CMD_BOOTTBL},
};

static uint32_t get_cfg_value(char *token, char *name,  int linenr)
{
	char *endptr;
	uint32_t value;

	errno = 0;
	value = strtoul(token, &endptr, 16);
	if (errno || (token == endptr)) {
		fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
			name,  linenr, token);
		exit(EXIT_FAILURE);
	}
	return value;
}

static int get_ais_table_id(uint32_t *ptr)
{

	int i;
	int func_no;

	for (i = 0; i < ARRAY_SIZE(cmd_table); i++) {
		if (*ptr == cmd_table[i].AIS_cmd) {
			if (cmd_table[i].AIS_cmd != AIS_CMD_FNLOAD)
				return i;

			func_no = ((struct ais_cmd_func *)ptr)->func_args
				& 0xFFFF;
			if (func_no == ais_func_table[i].index)
				return i;
		}
	}

	return -1;
}

static void aisimage_print_header(const void *hdr)
{
	struct ais_header *ais_hdr = (struct ais_header *)hdr;
	uint32_t *ptr;
	struct ais_cmd_load *ais_load;
	int id;

	if (ais_hdr->magic != AIS_MAGIC_WORD) {
		fprintf(stderr, "Error: - AIS Magic Number not found\n");
		return;
	}
	fprintf(stdout, "Image Type:   TI Davinci AIS Boot Image\n");
	fprintf(stdout, "AIS magic :   %08x\n", ais_hdr->magic);
	ptr = (uint32_t *)&ais_hdr->magic;
	ptr++;

	while (*ptr != AIS_CMD_JMPCLOSE) {
		/* Check if we find the image */
		if (*ptr == AIS_CMD_LOAD) {
			ais_load = (struct ais_cmd_load *)ptr;
			fprintf(stdout, "Image at  :   0x%08x size 0x%08x\n",
				ais_load->addr,
				ais_load->size);
			ptr = ais_load->data + ais_load->size / sizeof(*ptr);
			continue;
		}

		id = get_ais_table_id(ptr);
		if (id < 0) {
			fprintf(stderr, "Error: -  AIS Image corrupted\n");
			return;
		}
		fprintf(stdout, "AIS cmd   :   %s\n",
			get_table_entry_name(aisimage_cmds, NULL, id));
		ptr += cmd_table[id].nargs + IS_FNC_EXEC(id) + 1;
		if (((void *)ptr - hdr) > ais_img_size) {
			fprintf(stderr,
				"AIS Image not terminated by JMPCLOSE\n");
			return;
		}
	}
}

static uint32_t *ais_insert_cmd_header(uint32_t cmd, uint32_t nargs,
	uint32_t *parms, struct image_type_params *tparams,
	uint32_t *ptr)
{
	int i;

	*ptr++ = cmd_table[cmd].AIS_cmd;
	if (IS_FNC_EXEC(cmd))
		*ptr++ = ((nargs & 0xFFFF) << 16) + ais_func_table[cmd].index;

	/* Copy parameters */
	for (i = 0; i < nargs; i++)
		*ptr++ = cpu_to_le32(parms[i]);

	return ptr;

}

static uint32_t *ais_alloc_buffer(struct mkimage_params *params)
{
	int dfd;
	struct stat sbuf;
	char *datafile = params->datafile;
	uint32_t *ptr;

	dfd = open(datafile, O_RDONLY|O_BINARY);
	if (dfd < 0) {
		fprintf(stderr, "%s: Can't open %s: %s\n",
			params->cmdname, datafile, strerror(errno));
		exit(EXIT_FAILURE);
	}

	if (fstat(dfd, &sbuf) < 0) {
		fprintf(stderr, "%s: Can't stat %s: %s\n",
			params->cmdname, datafile, strerror(errno));
		exit(EXIT_FAILURE);
	}

	/*
	 * Place for header is allocated. The size is taken from
	 * the size of the datafile, that the ais_image_generate()
	 * will copy into the header. Copying the datafile
	 * is not left to the main program, because after the datafile
	 * the header must be terminated with the Jump & Close command.
	 */
	ais_img_size = WORD_ALIGN(sbuf.st_size) + MAX_CMD_BUFFER;
	ptr = (uint32_t *)malloc(WORD_ALIGN(sbuf.st_size) + MAX_CMD_BUFFER);
	if (!ptr) {
		fprintf(stderr, "%s: malloc return failure: %s\n",
			params->cmdname, strerror(errno));
		exit(EXIT_FAILURE);
	}

	close(dfd);

	return ptr;
}

static uint32_t *ais_copy_image(struct mkimage_params *params,
	uint32_t *aisptr)

{
	int dfd;
	struct stat sbuf;
	char *datafile = params->datafile;
	void *ptr;

	dfd = open(datafile, O_RDONLY|O_BINARY);
	if (dfd < 0) {
		fprintf(stderr, "%s: Can't open %s: %s\n",
			params->cmdname, datafile, strerror(errno));
		exit(EXIT_FAILURE);
	}

	if (fstat(dfd, &sbuf) < 0) {
		fprintf(stderr, "%s: Can't stat %s: %s\n",
			params->cmdname, datafile, strerror(errno));
		exit(EXIT_FAILURE);
	}

	ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
	*aisptr++ = AIS_CMD_LOAD;
	*aisptr++ = params->ep;
	*aisptr++ = sbuf.st_size;
	memcpy((void *)aisptr, ptr, sbuf.st_size);
	aisptr += WORD_ALIGN(sbuf.st_size) / sizeof(uint32_t);

	(void) munmap((void *)ptr, sbuf.st_size);
	(void) close(dfd);

	return aisptr;

}

static int aisimage_generate(struct mkimage_params *params,
	struct image_type_params *tparams)
{
	FILE *fd = NULL;
	char *line = NULL;
	char *token, *saveptr1, *saveptr2;
	int lineno = 0;
	int fld;
	size_t len;
	int32_t cmd;
	uint32_t nargs, cmd_parms[10];
	uint32_t value, size;
	char *name = params->imagename;
	uint32_t *aishdr;

	fd = fopen(name, "r");
	if (fd == 0) {
		fprintf(stderr,
			"Error: %s - Can't open AIS configuration\n", name);
		exit(EXIT_FAILURE);
	}

	/*
	 * the size of the header is variable and is computed
	 * scanning the configuration file.
	 */
	tparams->header_size = 0;

	/*
	 * Start allocating a buffer suitable for most command
	 * The buffer is then reallocated if it is too small
	 */
	aishdr = ais_alloc_buffer(params);
	tparams->hdr = aishdr;
	*aishdr++ = AIS_MAGIC_WORD;

	/* Very simple parsing, line starting with # are comments
	 * and are dropped
	 */
	while ((getline(&line, &len, fd)) > 0) {
		lineno++;

		token = strtok_r(line, "\r\n", &saveptr1);
		if (token == NULL)
			continue;

		/* Check inside the single line */
		line = token;
		fld = CFG_COMMAND;
		cmd = CMD_INVALID;
		nargs = 0;
		while (token != NULL) {
			token = strtok_r(line, " \t", &saveptr2);
			if (token == NULL)
				break;

			/* Drop all text starting with '#' as comments */
			if (token[0] == '#')
				break;

			switch (fld) {
			case CFG_COMMAND:
				cmd = get_table_entry_id(aisimage_cmds,
					"aisimage commands", token);
				if (cmd < 0) {
					fprintf(stderr,
					"Error: %s[%d] - Invalid command"
					"(%s)\n", name, lineno, token);

					exit(EXIT_FAILURE);
				}
				break;
			case CFG_VALUE:
				value = get_cfg_value(token, name, lineno);
				cmd_parms[nargs++] = value;
				if (nargs > cmd_table[cmd].nargs) {
					fprintf(stderr,
					 "Error: %s[%d] - too much arguments:"
						"(%s) for command %s\n", name,
						lineno, token,
						aisimage_cmds[cmd].sname);
					exit(EXIT_FAILURE);
				}
				break;
			}
			line = NULL;
			fld = CFG_VALUE;
		}
		if (cmd != CMD_INVALID) {
			/* Now insert the command into the header */
			aishdr = ais_insert_cmd_header(cmd, nargs, cmd_parms,
				tparams, aishdr);
		}

	}
	fclose(fd);

	aishdr = ais_copy_image(params, aishdr);

	/* Add Jmp & Close */
	*aishdr++ = AIS_CMD_JMPCLOSE;
	*aishdr++ = params->ep;

	size = (aishdr - (uint32_t *)tparams->hdr) * sizeof(uint32_t);
	tparams->header_size = size;

	return 0;
}

static int aisimage_check_image_types(uint8_t type)
{
	if (type == IH_TYPE_AISIMAGE)
		return EXIT_SUCCESS;
	else
		return EXIT_FAILURE;
}

static int aisimage_verify_header(unsigned char *ptr, int image_size,
			struct mkimage_params *params)
{
	struct ais_header *ais_hdr = (struct ais_header *)ptr;

	if (ais_hdr->magic != AIS_MAGIC_WORD)
		return -FDT_ERR_BADSTRUCTURE;

	/* Store the total size to remember in print_hdr */
	ais_img_size = image_size;

	return 0;
}

static void aisimage_set_header(void *ptr, struct stat *sbuf, int ifd,
				struct mkimage_params *params)
{
}

int aisimage_check_params(struct mkimage_params *params)
{
	if (!params)
		return CFG_INVALID;
	if (!strlen(params->imagename)) {
		fprintf(stderr, "Error: %s - Configuration file not specified, "
			"it is needed for aisimage generation\n",
			params->cmdname);
		return CFG_INVALID;
	}
	/*
	 * Check parameters:
	 * XIP is not allowed and verify that incompatible
	 * parameters are not sent at the same time
	 * For example, if list is required a data image must not be provided
	 */
	return	(params->dflag && (params->fflag || params->lflag)) ||
		(params->fflag && (params->dflag || params->lflag)) ||
		(params->lflag && (params->dflag || params->fflag)) ||
		(params->xflag) || !(strlen(params->imagename));
}

/*
 * aisimage parameters
 */
static struct image_type_params aisimage_params = {
	.name		= "TI Davinci AIS Boot Image support",
	.header_size	= 0,
	.hdr		= NULL,
	.check_image_type = aisimage_check_image_types,
	.verify_header	= aisimage_verify_header,
	.print_header	= aisimage_print_header,
	.set_header	= aisimage_set_header,
	.check_params	= aisimage_check_params,
	.vrec_header	= aisimage_generate,
};

void init_ais_image_type(void)
{
	mkimage_register(&aisimage_params);
}