summaryrefslogtreecommitdiffstats
path: root/qemu/roms/u-boot/tools/kwbimage.c
blob: 109d61686ec69fc958bd3b66995c83c412164eaa (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
/*
 * (C) Copyright 2008
 * Marvell Semiconductor <www.marvell.com>
 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include "imagetool.h"
#include <image.h>
#include "kwbimage.h"

/*
 * Supported commands for configuration file
 */
static table_entry_t kwbimage_cmds[] = {
	{CMD_BOOT_FROM,		"BOOT_FROM",		"boot command",	},
	{CMD_NAND_ECC_MODE,	"NAND_ECC_MODE",	"NAND mode",	},
	{CMD_NAND_PAGE_SIZE,	"NAND_PAGE_SIZE",	"NAND size",	},
	{CMD_SATA_PIO_MODE,	"SATA_PIO_MODE",	"SATA mode",	},
	{CMD_DDR_INIT_DELAY,	"DDR_INIT_DELAY",	"DDR init dly",	},
	{CMD_DATA,		"DATA",			"Reg Write Data", },
	{CMD_INVALID,		"",			"",	},
};

/*
 * Supported Boot options for configuration file
 */
static table_entry_t kwbimage_bootops[] = {
	{IBR_HDR_SPI_ID,	"spi",		"SPI Flash",	},
	{IBR_HDR_NAND_ID,	"nand",		"NAND Flash",	},
	{IBR_HDR_SATA_ID,	"sata",		"Sata port",	},
	{IBR_HDR_PEX_ID,	"pex",		"PCIe port",	},
	{IBR_HDR_UART_ID,	"uart",		"Serial port",	},
	{-1,			"",		"Invalid",	},
};

/*
 * Supported NAND ecc options configuration file
 */
static table_entry_t kwbimage_eccmodes[] = {
	{IBR_HDR_ECC_DEFAULT,		"default",	"Default mode",	},
	{IBR_HDR_ECC_FORCED_HAMMING,	"hamming",	"Hamming mode",	},
	{IBR_HDR_ECC_FORCED_RS,		"rs",		"RS mode",	},
	{IBR_HDR_ECC_DISABLED,		"disabled",	"ECC Disabled",	},
	{-1,				"",		"",	},
};

static struct kwb_header kwbimage_header;
static int datacmd_cnt = 0;
static char * fname = "Unknown";
static int lineno = -1;

/*
 * Report Error if xflag is set in addition to default
 */
static int kwbimage_check_params(struct image_tool_params *params)
{
	if (!strlen (params->imagename)) {
		printf ("Error:%s - Configuration file not specified, "
			"it is needed for kwbimage generation\n",
			params->cmdname);
		return CFG_INVALID;
	}
	return	((params->dflag && (params->fflag || params->lflag)) ||
		(params->fflag && (params->dflag || params->lflag)) ||
		(params->lflag && (params->dflag || params->fflag)) ||
		(params->xflag) || !(strlen (params->imagename)));
}

static uint32_t check_get_hexval (char *token)
{
	uint32_t hexval;

	if (!sscanf (token, "%x", &hexval)) {
		printf ("Error:%s[%d] - Invalid hex data(%s)\n", fname,
			lineno, token);
		exit (EXIT_FAILURE);
	}
	return hexval;
}

/*
 * Generates 8 bit checksum
 */
static uint8_t kwbimage_checksum8 (void *start, uint32_t len, uint8_t csum)
{
	register uint8_t sum = csum;
	volatile uint8_t *p = (volatile uint8_t *)start;

	/* check len and return zero checksum if invalid */
	if (!len)
		return 0;

	do {
		sum += *p;
		p++;
	} while (--len);
	return (sum);
}

/*
 * Generates 32 bit checksum
 */
static uint32_t kwbimage_checksum32 (uint32_t *start, uint32_t len, uint32_t csum)
{
	register uint32_t sum = csum;
	volatile uint32_t *p = start;

	/* check len and return zero checksum if invalid */
	if (!len)
		return 0;

	if (len % sizeof(uint32_t)) {
		printf ("Error:%s[%d] - length is not in multiple of %zu\n",
			__FUNCTION__, len, sizeof(uint32_t));
		return 0;
	}

	do {
		sum += *p;
		p++;
		len -= sizeof(uint32_t);
	} while (len > 0);
	return (sum);
}

static void kwbimage_check_cfgdata (char *token, enum kwbimage_cmd cmdsw,
					struct kwb_header *kwbhdr)
{
	bhr_t *mhdr = &kwbhdr->kwb_hdr;
	extbhr_t *exthdr = &kwbhdr->kwb_exthdr;
	int i;

	switch (cmdsw) {
	case CMD_BOOT_FROM:
		i = get_table_entry_id (kwbimage_bootops,
				"Kwbimage boot option", token);

		if (i < 0)
			goto INVL_DATA;

		mhdr->blockid = i;
		printf ("Preparing kirkwood boot image to boot "
			"from %s\n", token);
		break;
	case CMD_NAND_ECC_MODE:
		i = get_table_entry_id (kwbimage_eccmodes,
			"NAND ecc mode", token);

		if (i < 0)
			goto INVL_DATA;

		mhdr->nandeccmode = i;
		printf ("Nand ECC mode = %s\n", token);
		break;
	case CMD_NAND_PAGE_SIZE:
		mhdr->nandpagesize =
			(uint16_t) check_get_hexval (token);
		printf ("Nand page size = 0x%x\n", mhdr->nandpagesize);
		break;
	case CMD_SATA_PIO_MODE:
		mhdr->satapiomode =
			(uint8_t) check_get_hexval (token);
		printf ("Sata PIO mode = 0x%x\n",
				mhdr->satapiomode);
		break;
	case CMD_DDR_INIT_DELAY:
		mhdr->ddrinitdelay =
			(uint16_t) check_get_hexval (token);
		printf ("DDR init delay = %d msec\n", mhdr->ddrinitdelay);
		break;
	case CMD_DATA:
		exthdr->rcfg[datacmd_cnt].raddr =
			check_get_hexval (token);

		break;
	case CMD_INVALID:
		goto INVL_DATA;
	default:
		goto INVL_DATA;
	}
	return;

INVL_DATA:
	printf ("Error:%s[%d] - Invalid data\n", fname, lineno);
	exit (EXIT_FAILURE);
}

/*
 * this function sets the kwbimage header by-
 * 	1. Abstracting input command line arguments data
 *	2. parses the kwbimage configuration file and update extebded header data
 *	3. calculates header, extended header and image checksums
 */
static void kwdimage_set_ext_header (struct kwb_header *kwbhdr, char* name) {
	bhr_t *mhdr = &kwbhdr->kwb_hdr;
	extbhr_t *exthdr = &kwbhdr->kwb_exthdr;
	FILE *fd = NULL;
	int j;
	char *line = NULL;
	char * token, *saveptr1, *saveptr2;
	size_t len = 0;
	enum kwbimage_cmd cmd;

	fname = name;
	/* set dram register offset */
	exthdr->dramregsoffs = (intptr_t)&exthdr->rcfg - (intptr_t)mhdr;

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

	/* Simple kwimage.cfg file parser */
	lineno=0;
	while ((getline (&line, &len, fd)) > 0) {
		lineno++;
		token = strtok_r (line, "\r\n", &saveptr1);
		/* drop all lines with zero tokens (= empty lines) */
		if (token == NULL)
			continue;

		for (j = 0, cmd = CMD_INVALID, line = token; ; line = NULL) {
			token = strtok_r (line, " \t", &saveptr2);
			if (token == NULL)
			break;
			/* Drop all text starting with '#' as comments */
			if (token[0] == '#')
				break;

			/* Process rest as valid config command line */
			switch (j) {
			case CFG_COMMAND:
				cmd = get_table_entry_id (kwbimage_cmds,
						"Kwbimage command", token);

				if (cmd == CMD_INVALID)
					goto INVL_CMD;
				break;

			case CFG_DATA0:
				kwbimage_check_cfgdata (token, cmd, kwbhdr);
				break;

			case CFG_DATA1:
				if (cmd != CMD_DATA)
					goto INVL_CMD;

				exthdr->rcfg[datacmd_cnt].rdata =
						check_get_hexval (token);

				if (datacmd_cnt > KWBIMAGE_MAX_CONFIG ) {
					printf ("Error:%s[%d] - Found more "
						"than max(%zd) allowed "
						"data configurations\n",
						fname, lineno,
						KWBIMAGE_MAX_CONFIG);
				exit (EXIT_FAILURE);
				} else
					datacmd_cnt++;
				break;

			default:
				goto INVL_CMD;
			}
			j++;
		}
	}
	if (line)
		free (line);

	fclose (fd);
	return;

/*
 * Invalid Command error reporring
 *
 * command CMD_DATA needs three strings on a line
 * whereas other commands need only two.
 *
 * if more than two/three (as per command type) are observed,
 * then error will be reported
 */
INVL_CMD:
	printf ("Error:%s[%d] - Invalid command\n", fname, lineno);
	exit (EXIT_FAILURE);
}

static void kwbimage_set_header (void *ptr, struct stat *sbuf, int ifd,
				struct image_tool_params *params)
{
	struct kwb_header *hdr = (struct kwb_header *)ptr;
	bhr_t *mhdr = &hdr->kwb_hdr;
	extbhr_t *exthdr = &hdr->kwb_exthdr;
	uint32_t checksum;
	int size;

	/* Build and add image checksum header */
	checksum = kwbimage_checksum32 ((uint32_t *)ptr, sbuf->st_size, 0);

	size = write (ifd, &checksum, sizeof(uint32_t));
	if (size != sizeof(uint32_t)) {
		printf ("Error:%s - Checksum write %d bytes %s\n",
			params->cmdname, size, params->imagefile);
		exit (EXIT_FAILURE);
	}

	sbuf->st_size += sizeof(uint32_t);

	mhdr->blocksize = sbuf->st_size - sizeof(struct kwb_header);
	mhdr->srcaddr = sizeof(struct kwb_header);
	mhdr->destaddr= params->addr;
	mhdr->execaddr =params->ep;
	mhdr->ext = 0x1; /* header extension appended */

	kwdimage_set_ext_header (hdr, params->imagename);
	/* calculate checksums */
	mhdr->checkSum = kwbimage_checksum8 ((void *)mhdr, sizeof(bhr_t), 0);
	exthdr->checkSum = kwbimage_checksum8 ((void *)exthdr,
						sizeof(extbhr_t), 0);
}

static int kwbimage_verify_header (unsigned char *ptr, int image_size,
			struct image_tool_params *params)
{
	struct kwb_header *hdr = (struct kwb_header *)ptr;
	bhr_t *mhdr = &hdr->kwb_hdr;
	extbhr_t *exthdr = &hdr->kwb_exthdr;
	uint8_t calc_hdrcsum;
	uint8_t calc_exthdrcsum;

	calc_hdrcsum = kwbimage_checksum8 ((void *)mhdr,
			sizeof(bhr_t) - sizeof(uint8_t), 0);
	if (calc_hdrcsum != mhdr->checkSum)
		return -FDT_ERR_BADSTRUCTURE;	/* mhdr csum not matched */

	calc_exthdrcsum = kwbimage_checksum8 ((void *)exthdr,
			sizeof(extbhr_t) - sizeof(uint8_t), 0);
	if (calc_exthdrcsum != exthdr->checkSum)
		return -FDT_ERR_BADSTRUCTURE; /* exthdr csum not matched */

	return 0;
}

static void kwbimage_print_header (const void *ptr)
{
	struct kwb_header *hdr = (struct kwb_header *) ptr;
	bhr_t *mhdr = &hdr->kwb_hdr;
	char *name = get_table_entry_name (kwbimage_bootops,
				"Kwbimage boot option",
				(int) mhdr->blockid);

	printf ("Image Type:   Kirkwood Boot from %s Image\n", name);
	printf ("Data Size:    ");
	genimg_print_size (mhdr->blocksize - sizeof(uint32_t));
	printf ("Load Address: %08x\n", mhdr->destaddr);
	printf ("Entry Point:  %08x\n", mhdr->execaddr);
}

static int kwbimage_check_image_types (uint8_t type)
{
	if (type == IH_TYPE_KWBIMAGE)
		return EXIT_SUCCESS;
	else
		return EXIT_FAILURE;
}

/*
 * kwbimage type parameters definition
 */
static struct image_type_params kwbimage_params = {
	.name = "Kirkwood Boot Image support",
	.header_size = sizeof(struct kwb_header),
	.hdr = (void*)&kwbimage_header,
	.check_image_type = kwbimage_check_image_types,
	.verify_header = kwbimage_verify_header,
	.print_header = kwbimage_print_header,
	.set_header = kwbimage_set_header,
	.check_params = kwbimage_check_params,
};

void init_kwb_image_type (void)
{
	register_image_type(&kwbimage_params);
}