fel.c 49 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * Copyright (C) 2012  Henrik Nordstrom <henrik@henriknordstrom.net>
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

18
19
#include "common.h"
#include "portable_endian.h"
20
#include "fel_lib.h"
21
#include "fel-spiflash.h"
22

23
24
#include <assert.h>
#include <ctype.h>
25
#include <errno.h>
26
27
28
29
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Bernhard Nortmann's avatar
Bernhard Nortmann committed
30
#include <time.h>
31
#include <zlib.h>
32
#include <sys/stat.h>
33

34
static bool verbose = false; /* If set, makes the 'fel' tool more talkative */
35
36
static uint32_t uboot_entry = 0; /* entry point (address) of U-Boot */
static uint32_t uboot_size  = 0; /* size of U-Boot binary */
37
static bool enter_in_aarch64 = false;
38

Bernhard Nortmann's avatar
Bernhard Nortmann committed
39
40
41
/* printf-style output, but only if "verbose" flag is active */
#define pr_info(...) \
	do { if (verbose) printf(__VA_ARGS__); } while (0);
42

43
44
45
46
47
48
/* Constants taken from ${U-BOOT}/include/image.h */
#define IH_MAGIC	0x27051956	/* Image Magic Number	*/
#define IH_ARCH_ARM		2	/* ARM			*/
#define IH_TYPE_INVALID		0	/* Invalid Image	*/
#define IH_TYPE_FIRMWARE	5	/* Firmware Image	*/
#define IH_TYPE_SCRIPT		6	/* Script file		*/
49
#define IH_TYPE_FLATDT		8	/* DTB or FIT image	*/
50
51
52
53
54
#define IH_NMLEN		32	/* Image Name Length	*/

/* Additional error codes, newly introduced for get_image_type() */
#define IH_TYPE_ARCH_MISMATCH	-1

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Legacy format image U-Boot header,
 * all data in network byte order (aka natural aka bigendian).
 * Taken from ${U-BOOT}/include/image.h
 */
typedef struct image_header {
	uint32_t	ih_magic;	/* Image Header Magic Number	*/
	uint32_t	ih_hcrc;	/* Image Header CRC Checksum	*/
	uint32_t	ih_time;	/* Image Creation Timestamp	*/
	uint32_t	ih_size;	/* Image Data Size		*/
	uint32_t	ih_load;	/* Data	 Load  Address		*/
	uint32_t	ih_ep;		/* Entry Point Address		*/
	uint32_t	ih_dcrc;	/* Image Data CRC Checksum	*/
	uint8_t		ih_os;		/* Operating System		*/
	uint8_t		ih_arch;	/* CPU architecture		*/
	uint8_t		ih_type;	/* Image Type			*/
	uint8_t		ih_comp;	/* Compression Type		*/
	uint8_t		ih_name[IH_NMLEN];	/* Image Name		*/
} image_header_t;

#define HEADER_NAME_OFFSET	offsetof(image_header_t, ih_name)
#define HEADER_SIZE		sizeof(image_header_t)
77
78
79
80
81
82
83
84
85
86
87
88
89

/*
 * Utility function to determine the image type from a mkimage-compatible
 * header at given buffer (address).
 *
 * For invalid headers (insufficient size or 'magic' mismatch) the function
 * will return IH_TYPE_INVALID. Negative return values might indicate
 * special error conditions, e.g. IH_TYPE_ARCH_MISMATCH signals that the
 * image doesn't match the expected (ARM) architecture.
 * Otherwise the function will return the "ih_type" field for valid headers.
 */
int get_image_type(const uint8_t *buf, size_t len)
{
90
	image_header_t *hdr = (image_header_t *)buf;
91
92
93

	if (len <= HEADER_SIZE) /* insufficient length/size */
		return IH_TYPE_INVALID;
94

95
96
	if (be32toh(hdr->ih_magic) == 0xd00dfeed)
		return IH_TYPE_FLATDT;
97
	if (be32toh(hdr->ih_magic) != IH_MAGIC) /* signature mismatch */
98
99
		return IH_TYPE_INVALID;
	/* For sunxi, we always expect ARM architecture here */
100
	if (hdr->ih_arch != IH_ARCH_ARM)
101
102
103
		return IH_TYPE_ARCH_MISMATCH;

	/* assume a valid header, and return ih_type */
104
	return hdr->ih_type;
105
106
}

107
void aw_fel_print_version(feldev_handle *dev)
108
{
109
	struct aw_fel_version buf = dev->soc_version;
110
	const char *soc_name = dev->soc_name;
111

112
113
	if (soc_name[0] == '0') /* hexadecimal ID -> unknown SoC */
		soc_name = "unknown";
Henrik Nordstrom's avatar
Henrik Nordstrom committed
114

115
116
117
118
	printf("%.8s soc=%08x(%s) %08x ver=%04x %02x %02x scratchpad=%08x %08x %08x\n",
		buf.signature, buf.soc_id, soc_name, buf.unknown_0a,
		buf.protocol, buf.unknown_12, buf.unknown_13,
		buf.scratchpad, buf.pad[0], buf.pad[1]);
119
120
}

121
/*
122
123
 * This wrapper for the FEL write functionality safeguards against overwriting
 * an already loaded U-Boot binary.
124
125
 * The return value represents elapsed time in seconds (needed for execution).
 */
126
double aw_write_buffer(feldev_handle *dev, void *buf, uint32_t offset,
127
		       size_t len, bool progress)
128
129
130
131
{
	/* safeguard against overwriting an already loaded U-Boot binary */
	if (uboot_size > 0 && offset <= uboot_entry + uboot_size
			   && offset + len >= uboot_entry)
132
133
134
135
136
		pr_fatal("ERROR: Attempt to overwrite U-Boot! "
			 "Request 0x%08X-0x%08X overlaps 0x%08X-0x%08X.\n",
			 offset, (uint32_t)(offset + len),
			 uboot_entry, uboot_entry + uboot_size);

137
	double start = gettime();
138
	aw_fel_write_buffer(dev, buf, offset, len, progress);
139
140
141
	return gettime() - start;
}

142
143
144
145
146
147
void hexdump(void *data, uint32_t offset, size_t size)
{
	size_t j;
	unsigned char *buf = data;
	for (j = 0; j < size; j+=16) {
		size_t i;
148
		printf("%08zx: ", offset + j);
149
		for (i = 0; i < 16; i++) {
150
			if (j + i < size)
151
				printf("%02x ", buf[j+i]);
152
			else
153
154
				printf("__ ");
		}
155
		putchar(' ');
156
		for (i = 0; i < 16; i++) {
157
158
159
160
			if (j + i >= size)
				putchar('.');
			else
				putchar(isprint(buf[j+i]) ? buf[j+i] : '.');
161
		}
162
		putchar('\n');
163
164
	}
}
165

166
167
168
unsigned int file_size(const char *filename)
{
	struct stat st;
169
170
171
172
173
174
	if (stat(filename, &st) != 0)
		pr_fatal("stat() error on file \"%s\": %s\n", filename,
			 strerror(errno));
	if (!S_ISREG(st.st_mode))
		pr_fatal("error: \"%s\" is not a regular file\n", filename);

175
176
177
	return st.st_size;
}

178
179
180
181
int save_file(const char *name, void *data, size_t size)
{
	FILE *out = fopen(name, "wb");
	int rc;
182
	if (!out) {
183
		perror("Failed to open output file");
184
185
		exit(1);
	}
186
187
188
189
190
	rc = fwrite(data, size, 1, out);
	fclose(out);
	return rc;
}

191
192
void *load_file(const char *name, size_t *size)
{
Bernhard Nortmann's avatar
Bernhard Nortmann committed
193
	size_t offset = 0, bufsize = 8192;
194
195
196
197
198
199
	char *buf = malloc(bufsize);
	FILE *in;
	if (strcmp(name, "-") == 0)
		in = stdin;
	else
		in = fopen(name, "rb");
200
	if (!in) {
201
		perror("Failed to open input file");
202
203
		exit(1);
	}
204
	
Bernhard Nortmann's avatar
Bernhard Nortmann committed
205
	while (true) {
Bernhard Nortmann's avatar
Bernhard Nortmann committed
206
207
		size_t len = bufsize - offset;
		size_t n = fread(buf+offset, 1, len, in);
208
		offset += n;
209
		if (n < len)
210
			break;
Bernhard Nortmann's avatar
Bernhard Nortmann committed
211
		bufsize *= 2;
212
		buf = realloc(buf, bufsize);
Bernhard Nortmann's avatar
Bernhard Nortmann committed
213
214
215
216
		if (!buf) {
			perror("Failed to resize load_file() buffer");
			exit(1);
		}
217
218
219
220
221
222
223
224
	}
	if (size) 
		*size = offset;
	if (in != stdin)
		fclose(in);
	return buf;
}

225
void aw_fel_hexdump(feldev_handle *dev, uint32_t offset, size_t size)
226
{
227
228
229
230
231
	if (size > 0) {
		unsigned char buf[size];
		aw_fel_read(dev, offset, buf, size);
		hexdump(buf, offset, size);
	}
232
233
}

234
void aw_fel_dump(feldev_handle *dev, uint32_t offset, size_t size)
235
{
236
237
238
239
240
	if (size > 0) {
		unsigned char buf[size];
		aw_fel_read(dev, offset, buf, size);
		fwrite(buf, size, 1, stdout);
	}
241
}
242
void aw_fel_fill(feldev_handle *dev, uint32_t offset, size_t size, unsigned char value)
243
{
244
245
246
247
248
	if (size > 0) {
		unsigned char buf[size];
		memset(buf, value, size);
		aw_write_buffer(dev, buf, offset, size, false);
	}
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
/*
 * Upload a function (implemented in native ARM code) to the device and
 * prepare for executing it. Use a subset of 32-bit ARM AAPCS calling
 * conventions: all arguments are integer 32-bit values, and an optional
 * return value is a 32-bit integer too. The function code needs to be
 * compiled in the ARM mode (Thumb2 is not supported), it also must be
 * a position independent leaf function (have no calls to anything else)
 * and have no references to any global variables.
 *
 * 'stack_size'    - the required stack size for the function (can be
 *                   calculated using the '-fstack-usage' GCC option)
 * 'arm_code'      - a pointer to the memory buffer with the function code
 * 'arm_code_size' - the size of the function code
 * 'num_args'      - the number of 32-bit function arguments
 * 'args'          - an array with the function argument values
 *
 * Note: once uploaded, the function can be executed multiple times with
 *       exactly the same arguments. If some internal state needs to be
 *       updated between function calls, then it's best to pass a pointer
 *       to some state structure located elsewhere in SRAM as one of the
 *       function arguments.
 */

bool aw_fel_remotefunc_prepare(feldev_handle *dev,
			       size_t                stack_size,
			       void                 *arm_code,
			       size_t                arm_code_size,
			       size_t                num_args,
			       uint32_t             *args)
{
	size_t idx, i;
	size_t tmp_buf_size;
	soc_info_t *soc_info = dev->soc_info;
	uint32_t *tmp_buf;
	uint32_t new_sp, num_args_on_stack = (num_args <= 4 ? 0 : num_args - 4);
	uint32_t entry_code[] = {
		htole32(0xe58fe040), /*    0:    str      lr, [pc, #64]         */
		htole32(0xe58fd040), /*    4:    str      sp, [pc, #64]         */
		htole32(0xe59fd040), /*    8:    ldr      sp, [pc, #64]         */
		htole32(0xe28fc040), /*    c:    add      ip, pc, #64           */
		htole32(0xe1a0200d), /*   10:    mov      r2, sp                */
		htole32(0xe49c0004), /*   14:    ldr      r0, [ip], #4          */
		htole32(0xe3500000), /*   18:    cmp      r0, #0                */
		htole32(0x0a000003), /*   1c:    beq      30 <entry+0x30>       */
		htole32(0xe49c1004), /*   20:    ldr      r1, [ip], #4          */
		htole32(0xe4821004), /*   24:    str      r1, [r2], #4          */
		htole32(0xe2500001), /*   28:    subs     r0, r0, #1            */
		htole32(0x1afffffb), /*   2c:    bne      20 <entry+0x20>       */
		htole32(0xe8bc000f), /*   30:    ldm      ip!, {r0, r1, r2, r3} */
		htole32(0xe12fff3c), /*   34:    blx      ip                    */
		htole32(0xe59fe008), /*   38:    ldr      lr, [pc, #8]          */
		htole32(0xe59fd008), /*   3c:    ldr      sp, [pc, #8]          */
		htole32(0xe58f0000), /*   40:    str      r0, [pc]              */
		htole32(0xe12fff1e), /*   44:    bx       lr                    */
		htole32(0x00000000), /*   48:    .word    0x00000000            */
		htole32(0x00000000), /*   4c:    .word    0x00000000            */
	};

	if (!soc_info)
		return false;

	/* Calculate the stack location */
	new_sp = soc_info->scratch_addr +
		 sizeof(entry_code) +
		 2 * 4 +
		 num_args_on_stack * 4 +
		 4 * 4 +
		 arm_code_size +
		 stack_size;
	new_sp = (new_sp + 7) & ~7;

	tmp_buf_size = new_sp - soc_info->scratch_addr;
	tmp_buf = calloc(tmp_buf_size, 1);
	memcpy(tmp_buf, entry_code, sizeof(entry_code));
	idx = sizeof(entry_code) / 4;
	tmp_buf[idx++] = htole32(new_sp);
	tmp_buf[idx++] = htole32(num_args_on_stack);
	for (i = num_args - num_args_on_stack; i < num_args; i++)
		tmp_buf[idx++] = htole32(args[i]);
	for (i = 0; i < 4; i++)
		tmp_buf[idx++] = (i < num_args ? htole32(args[i]) : 0);
	memcpy(tmp_buf + idx, arm_code, arm_code_size);

	aw_fel_write(dev, tmp_buf, soc_info->scratch_addr, tmp_buf_size);
	free(tmp_buf);
	return true;
}

/*
 * Execute the previously uploaded function. The 'result' pointer allows to
 * retrieve the return value.
 */
bool aw_fel_remotefunc_execute(feldev_handle *dev, uint32_t *result)
{
	soc_info_t *soc_info = dev->soc_info;
	if (!soc_info)
		return false;
	aw_fel_execute(dev, soc_info->scratch_addr);
	if (result) {
		aw_fel_read(dev, soc_info->scratch_addr + 0x48, result, sizeof(uint32_t));
		*result = le32toh(*result);
	}
	return true;
}

356
static uint32_t fel_to_spl_thunk[] = {
357
	#include "thunks/fel-to-spl-thunk.h"
358
359
};

360
361
362
#define	DRAM_BASE		0x40000000
#define	DRAM_SIZE		0x80000000

363
uint32_t aw_read_arm_cp_reg(feldev_handle *dev, soc_info_t *soc_info,
364
365
366
367
			    uint32_t coproc, uint32_t opc1, uint32_t crn,
			    uint32_t crm, uint32_t opc2)
{
	uint32_t val = 0;
Bernhard Nortmann's avatar
Bernhard Nortmann committed
368
369
370
371
	uint32_t opcode = 0xEE000000 | (1 << 20) | (1 << 4)
			  | ((opc1 & 0x7) << 21) | ((crn & 0xF) << 16)
			  | ((coproc & 0xF) << 8) | ((opc2 & 0x7) << 5)
			  | (crm & 0xF);
372
373
374
375
376
	uint32_t arm_code[] = {
		htole32(opcode),     /* mrc  coproc, opc1, r0, crn, crm, opc2 */
		htole32(0xe58f0000), /* str  r0, [pc]                         */
		htole32(0xe12fff1e), /* bx   lr                               */
	};
377
378
379
	aw_fel_write(dev, arm_code, soc_info->scratch_addr, sizeof(arm_code));
	aw_fel_execute(dev, soc_info->scratch_addr);
	aw_fel_read(dev, soc_info->scratch_addr + 12, &val, sizeof(val));
380
381
382
	return le32toh(val);
}

383
void aw_write_arm_cp_reg(feldev_handle *dev, soc_info_t *soc_info,
384
385
386
			 uint32_t coproc, uint32_t opc1, uint32_t crn,
			 uint32_t crm, uint32_t opc2, uint32_t val)
{
Bernhard Nortmann's avatar
Bernhard Nortmann committed
387
388
389
390
	uint32_t opcode = 0xEE000000 | (0 << 20) | (1 << 4)
			  | ((opc1 & 0x7) << 21) | ((crn & 0xF) << 16)
			  | ((coproc & 0xF) << 8) | ((opc2 & 7) << 5)
			  | (crm & 0xF);
391
392
393
394
395
396
397
398
	uint32_t arm_code[] = {
		htole32(0xe59f000c), /* ldr  r0, [pc, #12]                    */
		htole32(opcode),     /* mcr  coproc, opc1, r0, crn, crm, opc2 */
		htole32(0xf57ff04f), /* dsb  sy                               */
		htole32(0xf57ff06f), /* isb  sy                               */
		htole32(0xe12fff1e), /* bx   lr                               */
		htole32(val)
	};
399
400
	aw_fel_write(dev, arm_code, soc_info->scratch_addr, sizeof(arm_code));
	aw_fel_execute(dev, soc_info->scratch_addr);
401
402
}

403
/* "readl" of a single value */
404
uint32_t fel_readl(feldev_handle *dev, uint32_t addr)
405
406
{
	uint32_t val;
407
	fel_readl_n(dev, addr, &val, 1);
408
409
410
411
	return val;
}

/* "writel" of a single value */
412
void fel_writel(feldev_handle *dev, uint32_t addr, uint32_t val)
413
{
414
	fel_writel_n(dev, addr, &val, 1);
415
416
}

417
void aw_fel_print_sid(feldev_handle *dev, bool force_workaround)
418
{
419
	uint32_t key[4];
420
	soc_info_t *soc_info = dev->soc_info;
Icenowy Zheng's avatar
Icenowy Zheng committed
421

422
	if (!soc_info->sid_base) {
423
424
		printf("SID registers for your SoC (%s) are unknown or inaccessible.\n",
			dev->soc_name);
425
		return;
426
	}
427
428
429
430
431
432
433
434
435
436
437
438
439

	if (soc_info->sid_fix || force_workaround) {
		pr_info("Read SID key via registers, base = 0x%08X\n",
			soc_info->sid_base);
	} else {
		pr_info("SID key (e-fuses) at 0x%08X\n",
			soc_info->sid_base + soc_info->sid_offset);
	}
	fel_get_sid_root_key(dev, key, force_workaround);

	/* output SID in "xxxxxxxx:xxxxxxxx:xxxxxxxx:xxxxxxxx" format */
	for (unsigned i = 0; i <= 3; i++)
		printf("%08x%c", key[i], i < 3 ? ':' : '\n');
440
441
}

442
void aw_enable_l2_cache(feldev_handle *dev, soc_info_t *soc_info)
443
444
445
446
447
448
449
450
{
	uint32_t arm_code[] = {
		htole32(0xee112f30), /* mrc        15, 0, r2, cr1, cr0, {1}  */
		htole32(0xe3822002), /* orr        r2, r2, #2                */
		htole32(0xee012f30), /* mcr        15, 0, r2, cr1, cr0, {1}  */
		htole32(0xe12fff1e), /* bx         lr                        */
	};

451
452
	aw_fel_write(dev, arm_code, soc_info->scratch_addr, sizeof(arm_code));
	aw_fel_execute(dev, soc_info->scratch_addr);
453
454
}

455
void aw_get_stackinfo(feldev_handle *dev, soc_info_t *soc_info,
456
                      uint32_t *sp_irq, uint32_t *sp)
457
458
459
460
461
462
463
464
465
466
467
{
	uint32_t results[2] = { 0 };
#if 0
	/* Does not work on Cortex-A8 (needs Virtualization Extensions) */
	uint32_t arm_code[] = {
		htole32(0xe1010300), /* mrs        r0, SP_irq                */
		htole32(0xe58f0004), /* str        r0, [pc, #4]              */
		htole32(0xe58fd004), /* str        sp, [pc, #4]              */
		htole32(0xe12fff1e), /* bx         lr                        */
	};

468
469
470
	aw_fel_write(dev, arm_code, soc_info->scratch_addr, sizeof(arm_code));
	aw_fel_execute(dev, soc_info->scratch_addr);
	aw_fel_read(dev, soc_info->scratch_addr + 0x10, results, 8);
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#else
	/* Works everywhere */
	uint32_t arm_code[] = {
		htole32(0xe10f0000), /* mrs        r0, CPSR                  */
		htole32(0xe3c0101f), /* bic        r1, r0, #31               */
		htole32(0xe3811012), /* orr        r1, r1, #18               */
		htole32(0xe121f001), /* msr        CPSR_c, r1                */
		htole32(0xe1a0100d), /* mov        r1, sp                    */
		htole32(0xe121f000), /* msr        CPSR_c, r0                */
		htole32(0xe58f1004), /* str        r1, [pc, #4]              */
		htole32(0xe58fd004), /* str        sp, [pc, #4]              */
		htole32(0xe12fff1e), /* bx         lr                        */
	};

485
486
487
	aw_fel_write(dev, arm_code, soc_info->scratch_addr, sizeof(arm_code));
	aw_fel_execute(dev, soc_info->scratch_addr);
	aw_fel_read(dev, soc_info->scratch_addr + 0x24, results, 8);
488
489
490
491
492
#endif
	*sp_irq = le32toh(results[0]);
	*sp     = le32toh(results[1]);
}

493
uint32_t aw_get_ttbr0(feldev_handle *dev, soc_info_t *soc_info)
494
{
495
	return aw_read_arm_cp_reg(dev, soc_info, 15, 0, 2, 0, 0);
496
497
}

498
uint32_t aw_get_ttbcr(feldev_handle *dev, soc_info_t *soc_info)
499
{
500
	return aw_read_arm_cp_reg(dev, soc_info, 15, 0, 2, 0, 2);
501
502
}

503
uint32_t aw_get_dacr(feldev_handle *dev, soc_info_t *soc_info)
504
{
505
	return aw_read_arm_cp_reg(dev, soc_info, 15, 0, 3, 0, 0);
506
507
}

508
uint32_t aw_get_sctlr(feldev_handle *dev, soc_info_t *soc_info)
509
{
510
	return aw_read_arm_cp_reg(dev, soc_info, 15, 0, 1, 0, 0);
511
512
}

513
void aw_set_ttbr0(feldev_handle *dev, soc_info_t *soc_info,
514
515
		  uint32_t ttbr0)
{
516
	return aw_write_arm_cp_reg(dev, soc_info, 15, 0, 2, 0, 0, ttbr0);
517
518
}

519
void aw_set_ttbcr(feldev_handle *dev, soc_info_t *soc_info,
520
521
		  uint32_t ttbcr)
{
522
	return aw_write_arm_cp_reg(dev, soc_info, 15, 0, 2, 0, 2, ttbcr);
523
524
}

525
void aw_set_dacr(feldev_handle *dev, soc_info_t *soc_info,
526
527
		 uint32_t dacr)
{
528
	aw_write_arm_cp_reg(dev, soc_info, 15, 0, 3, 0, 0, dacr);
529
530
}

531
void aw_set_sctlr(feldev_handle *dev, soc_info_t *soc_info,
532
533
		  uint32_t sctlr)
{
534
	aw_write_arm_cp_reg(dev, soc_info, 15, 0, 1, 0, 0, sctlr);
535
536
}

537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
 * Issue a "smc #0" instruction. This brings a SoC booted in "secure boot"
 * state from the default non-secure FEL into secure FEL.
 * This crashes on devices using "non-secure boot", as the BROM does not
 * provide a handler address in MVBAR. So we have a runtime check.
 */
void aw_apply_smc_workaround(feldev_handle *dev)
{
	soc_info_t *soc_info = dev->soc_info;
	uint32_t val;
	uint32_t arm_code[] = {
		htole32(0xe1600070), /* smc	#0	*/
		htole32(0xe12fff1e), /* bx	lr	*/
	};

	/* Return if the SoC does not need this workaround */
	if (!soc_info->needs_smc_workaround_if_zero_word_at_addr)
		return;

	/* This has less overhead than fel_readl_n() and may be good enough */
	aw_fel_read(dev, soc_info->needs_smc_workaround_if_zero_word_at_addr,
	            &val, sizeof(val));

	/* Return if the workaround is not needed or has been already applied */
	if (val != 0)
		return;

	pr_info("Applying SMC workaround... ");
	aw_fel_write(dev, arm_code, soc_info->scratch_addr, sizeof(arm_code));
	aw_fel_execute(dev, soc_info->scratch_addr);
	pr_info(" done.\n");
}

570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
 * Reconstruct the same MMU translation table as used by the A20 BROM.
 * We are basically reverting the changes, introduced in newer SoC
 * variants. This works fine for the SoC variants with the memory
 * layout similar to A20 (the SRAM is in the first megabyte of the
 * address space and the BROM is in the last megabyte of the address
 * space).
 */
uint32_t *aw_generate_mmu_translation_table(void)
{
	uint32_t *tt = malloc(4096 * sizeof(uint32_t));
	uint32_t i;

	/*
	 * Direct mapping using 1MB sections with TEXCB=00000 (Strongly
	 * ordered) for all memory except the first and the last sections,
	 * which have TEXCB=00100 (Normal). Domain bits are set to 1111
	 * and AP bits are set to 11, but this is mostly irrelevant.
	 */
	for (i = 0; i < 4096; i++)
		tt[i] = 0x00000DE2 | (i << 20);
	tt[0x000] |= 0x1000;
	tt[0xFFF] |= 0x1000;

	return tt;
}

597
uint32_t *aw_backup_and_disable_mmu(feldev_handle *dev,
598
                                    soc_info_t *soc_info)
599
{
600
	uint32_t *tt = NULL;
601
	uint32_t sctlr, ttbr0, ttbcr, dacr;
602
603
604
	uint32_t i;

	uint32_t arm_code[] = {
605
		/* Disable I-cache, MMU and branch prediction */
606
607
		htole32(0xee110f10), /* mrc        15, 0, r0, cr1, cr0, {0}  */
		htole32(0xe3c00001), /* bic        r0, r0, #1                */
Bernhard Nortmann's avatar
Bernhard Nortmann committed
608
		htole32(0xe3c00b06), /* bic        r0, r0, #0x1800           */
609
610
611
612
613
		htole32(0xee010f10), /* mcr        15, 0, r0, cr1, cr0, {0}  */
		/* Return back to FEL */
		htole32(0xe12fff1e), /* bx         lr                        */
	};

614
615
616
617
618
619
620
621
622
623
624
	/*
	 * Below are some checks for the register values, which are known
	 * to be initialized in this particular way by the existing BROM
	 * implementations. We don't strictly need them to exactly match,
	 * but still have these safety guards in place in order to detect
	 * and review any potential configuration changes in future SoC
	 * variants (if one of these checks fails, then it is not a serious
	 * problem but more likely just an indication that one of these
	 * checks needs to be relaxed).
	 */

625
	/* Basically, ignore M/Z/I/V/UNK bits and expect no TEX remap */
626
	sctlr = aw_get_sctlr(dev, soc_info);
627
628
	if ((sctlr & ~((0x7 << 11) | (1 << 6) | 1)) != 0x00C50038)
		pr_fatal("Unexpected SCTLR (%08X)\n", sctlr);
629

630
	if (!(sctlr & 1)) {
631
632
		pr_info("MMU is not enabled by BROM\n");
		return NULL;
633
634
	}

635
	dacr = aw_get_dacr(dev, soc_info);
636
637
	if (dacr != 0x55555555)
		pr_fatal("Unexpected DACR (%08X)\n", dacr);
638

639
	ttbcr = aw_get_ttbcr(dev, soc_info);
640
641
	if (ttbcr != 0x00000000)
		pr_fatal("Unexpected TTBCR (%08X)\n", ttbcr);
642

643
	ttbr0 = aw_get_ttbr0(dev, soc_info);
644
645
	if (ttbr0 & 0x3FFF)
		pr_fatal("Unexpected TTBR0 (%08X)\n", ttbr0);
646

647
	tt = malloc(16 * 1024);
648
	pr_info("Reading the MMU translation table from 0x%08X\n", ttbr0);
649
	aw_fel_read(dev, ttbr0, tt, 16 * 1024);
650
651
652
653
654
	for (i = 0; i < 4096; i++)
		tt[i] = le32toh(tt[i]);

	/* Basic sanity checks to be sure that this is a valid table */
	for (i = 0; i < 4096; i++) {
655
656
657
658
		if (((tt[i] >> 1) & 1) != 1 || ((tt[i] >> 18) & 1) != 0)
			pr_fatal("MMU: not a section descriptor\n");
		if ((tt[i] >> 20) != i)
			pr_fatal("MMU: not a direct mapping\n");
659
660
	}

661
	pr_info("Disabling I-cache, MMU and branch prediction...");
662
663
	aw_fel_write(dev, arm_code, soc_info->scratch_addr, sizeof(arm_code));
	aw_fel_execute(dev, soc_info->scratch_addr);
664
665
666
667
668
	pr_info(" done.\n");

	return tt;
}

669
void aw_restore_and_enable_mmu(feldev_handle *dev,
670
                               soc_info_t *soc_info,
671
                               uint32_t *tt)
672
673
{
	uint32_t i;
674
	uint32_t ttbr0 = aw_get_ttbr0(dev, soc_info);
675
676

	uint32_t arm_code[] = {
677
678
679
680
681
682
683
684
		/* Invalidate I-cache, TLB and BTB */
		htole32(0xe3a00000), /* mov        r0, #0                    */
		htole32(0xee080f17), /* mcr        15, 0, r0, cr8, cr7, {0}  */
		htole32(0xee070f15), /* mcr        15, 0, r0, cr7, cr5, {0}  */
		htole32(0xee070fd5), /* mcr        15, 0, r0, cr7, cr5, {6}  */
		htole32(0xf57ff04f), /* dsb        sy                        */
		htole32(0xf57ff06f), /* isb        sy                        */
		/* Enable I-cache, MMU and branch prediction */
685
686
		htole32(0xee110f10), /* mrc        15, 0, r0, cr1, cr0, {0}  */
		htole32(0xe3800001), /* orr        r0, r0, #1                */
Bernhard Nortmann's avatar
Bernhard Nortmann committed
687
		htole32(0xe3800b06), /* orr        r0, r0, #0x1800           */
688
689
690
691
692
		htole32(0xee010f10), /* mcr        15, 0, r0, cr1, cr0, {0}  */
		/* Return back to FEL */
		htole32(0xe12fff1e), /* bx         lr                        */
	};

693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
	pr_info("Setting write-combine mapping for DRAM.\n");
	for (i = (DRAM_BASE >> 20); i < ((DRAM_BASE + DRAM_SIZE) >> 20); i++) {
		/* Clear TEXCB bits */
		tt[i] &= ~((7 << 12) | (1 << 3) | (1 << 2));
		/* Set TEXCB to 00100 (Normal uncached mapping) */
		tt[i] |= (1 << 12);
	}

	pr_info("Setting cached mapping for BROM.\n");
	/* Clear TEXCB bits first */
	tt[0xFFF] &= ~((7 << 12) | (1 << 3) | (1 << 2));
	/* Set TEXCB to 00111 (Normal write-back cached mapping) */
	tt[0xFFF] |= (1 << 12) | /* TEX */
		     (1 << 3)  | /* C */
		     (1 << 2);   /* B */

709
710
711
	pr_info("Writing back the MMU translation table.\n");
	for (i = 0; i < 4096; i++)
		tt[i] = htole32(tt[i]);
712
	aw_fel_write(dev, tt, ttbr0, 16 * 1024);
713

714
	pr_info("Enabling I-cache, MMU and branch prediction...");
715
716
	aw_fel_write(dev, arm_code, soc_info->scratch_addr, sizeof(arm_code));
	aw_fel_execute(dev, soc_info->scratch_addr);
717
718
719
720
721
	pr_info(" done.\n");

	free(tt);
}

722
723
/* Minimum offset of the main U-Boot image within u-boot-sunxi-with-spl.bin. */
#define SPL_MIN_OFFSET 0x8000
724

725
uint32_t aw_fel_write_and_execute_spl(feldev_handle *dev, uint8_t *buf, size_t len)
726
{
727
	soc_info_t *soc_info = dev->soc_info;
728
729
730
731
	sram_swap_buffers *swap_buffers;
	char header_signature[9] = { 0 };
	size_t i, thunk_size;
	uint32_t *thunk_buf;
732
	uint32_t sp, sp_irq;
733
	uint32_t spl_checksum, spl_len, spl_len_limit;
734
	uint32_t *buf32 = (uint32_t *)buf;
735
	uint32_t cur_addr = soc_info->spl_addr;
736
	uint32_t *tt = NULL;
737

738
739
740
741
	if (!soc_info || !soc_info->swap_buffers)
		pr_fatal("SPL: Unsupported SoC type\n");
	if (len < 32 || memcmp(buf + 4, "eGON.BT0", 8) != 0)
		pr_fatal("SPL: eGON header is not found\n");
742
743
744
745

	spl_checksum = 2 * le32toh(buf32[3]) - 0x5F0A6C39;
	spl_len = le32toh(buf32[4]);

746
747
	if (spl_len > len || (spl_len % 4) != 0)
		pr_fatal("SPL: bad length in the eGON header\n");
748
749
750
751
752

	len = spl_len;
	for (i = 0; i < len / 4; i++)
		spl_checksum -= le32toh(buf32[i]);

753
754
	if (spl_checksum != 0)
		pr_fatal("SPL: checksum check failed\n");
755

756
	if (soc_info->needs_l2en) {
757
		pr_info("Enabling the L2 cache\n");
758
		aw_enable_l2_cache(dev, soc_info);
759
760
	}

761
	aw_get_stackinfo(dev, soc_info, &sp_irq, &sp);
762
763
	pr_info("Stack pointers: sp_irq=0x%08X, sp=0x%08X\n", sp_irq, sp);

764
	tt = aw_backup_and_disable_mmu(dev, soc_info);
765
	if (!tt && soc_info->mmu_tt_addr) {
766
767
		if (soc_info->mmu_tt_addr & 0x3FFF)
			pr_fatal("SPL: 'mmu_tt_addr' must be 16K aligned\n");
768
		pr_info("Generating the new MMU translation table at 0x%08X\n",
769
			soc_info->mmu_tt_addr);
770
771
772
773
774
775
776
777
778
779
		/*
		 * These settings are used by the BROM in A10/A13/A20 and
		 * we replicate them here when enabling the MMU. The DACR
		 * value 0x55555555 means that accesses are checked against
		 * the permission bits in the translation tables for all
		 * domains. The TTBCR value 0x00000000 means that the short
		 * descriptor translation table format is used, TTBR0 is used
		 * for all the possible virtual addresses (N=0) and that the
		 * translation table must be aligned at a 16K boundary.
		 */
780
781
782
		aw_set_dacr(dev, soc_info, 0x55555555);
		aw_set_ttbcr(dev, soc_info, 0x00000000);
		aw_set_ttbr0(dev, soc_info, soc_info->mmu_tt_addr);
783
784
		tt = aw_generate_mmu_translation_table();
	}
785

786
787
	spl_len_limit = soc_info->sram_size;

788
	swap_buffers = soc_info->swap_buffers;
789
	for (i = 0; swap_buffers[i].size; i++) {
790
791
792
		if ((swap_buffers[i].buf2 >= soc_info->spl_addr) &&
		    (swap_buffers[i].buf2 < soc_info->spl_addr + spl_len_limit))
			spl_len_limit = swap_buffers[i].buf2 - soc_info->spl_addr;
793
794
		if (len > 0 && cur_addr < swap_buffers[i].buf1) {
			uint32_t tmp = swap_buffers[i].buf1 - cur_addr;
795
796
			if (tmp > len)
				tmp = len;
797
			aw_fel_write(dev, buf, cur_addr, tmp);
798
			cur_addr += tmp;
799
800
801
			buf += tmp;
			len -= tmp;
		}
802
		if (len > 0 && cur_addr == swap_buffers[i].buf1) {
803
804
805
			uint32_t tmp = swap_buffers[i].size;
			if (tmp > len)
				tmp = len;
806
			aw_fel_write(dev, buf, swap_buffers[i].buf2, tmp);
807
			cur_addr += tmp;
808
809
810
811
812
813
			buf += tmp;
			len -= tmp;
		}
	}

	/* Clarify the SPL size limitations, and bail out if they are not met */
814
815
	if (soc_info->thunk_addr - soc_info->spl_addr < spl_len_limit)
		spl_len_limit = soc_info->thunk_addr - soc_info->spl_addr;
816

817
818
819
	if (spl_len > spl_len_limit)
		pr_fatal("SPL: too large (need %u, have %u)\n",
			 spl_len, spl_len_limit);
820
821
822

	/* Write the remaining part of the SPL */
	if (len > 0)
823
		aw_fel_write(dev, buf, cur_addr, len);
824

825
	thunk_size = sizeof(fel_to_spl_thunk) + sizeof(soc_info->spl_addr) +
826
		     (i + 1) * sizeof(*swap_buffers);
827

828
829
830
	if (thunk_size > soc_info->thunk_size)
		pr_fatal("SPL: bad thunk size (need %d, have %d)\n",
			 (int)sizeof(fel_to_spl_thunk), soc_info->thunk_size);
831
832
833
834

	thunk_buf = malloc(thunk_size);
	memcpy(thunk_buf, fel_to_spl_thunk, sizeof(fel_to_spl_thunk));
	memcpy(thunk_buf + sizeof(fel_to_spl_thunk) / sizeof(uint32_t),
835
	       &soc_info->spl_addr, sizeof(soc_info->spl_addr));
836
	memcpy(thunk_buf + sizeof(fel_to_spl_thunk) / sizeof(uint32_t) + 1,
837
838
839
840
841
	       swap_buffers, (i + 1) * sizeof(*swap_buffers));

	for (i = 0; i < thunk_size / sizeof(uint32_t); i++)
		thunk_buf[i] = htole32(thunk_buf[i]);

842
	pr_info("=> Executing the SPL...");
843
844
	aw_fel_write(dev, thunk_buf, soc_info->thunk_addr, thunk_size);
	aw_fel_execute(dev, soc_info->thunk_addr);
845
	pr_info(" done.\n");
846
847
848
849

	free(thunk_buf);

	/* TODO: Try to find and fix the bug, which needs this workaround */
Bernhard Nortmann's avatar
Bernhard Nortmann committed
850
851
	struct timespec req = { .tv_nsec = 250000000 }; /* 250ms */
	nanosleep(&req, NULL);
852
853

	/* Read back the result and check if everything was fine */
854
	aw_fel_read(dev, soc_info->spl_addr + 4, header_signature, 8);
855
856
	if (strcmp(header_signature, "eGON.FEL") != 0)
		pr_fatal("SPL: failure code '%s'\n", header_signature);
857

858
	/* re-enable the MMU if it was enabled by BROM */
Bernhard Nortmann's avatar
Bernhard Nortmann committed
859
	if (tt != NULL)
860
		aw_restore_and_enable_mmu(dev, soc_info, tt);
861
862

	return spl_len;
863
864
}

865
866
867
868
869
870
/*
 * This function tests a given buffer address and length for a valid U-Boot
 * image. Upon success, the image data gets transferred to the default memory
 * address stored within the image header; and the function preserves the
 * U-Boot entry point (offset) and size values.
 */
Andre Przywara's avatar
Andre Przywara committed
871
872
static void aw_fel_write_uboot_image(feldev_handle *dev, uint8_t *buf,
				     size_t len, const char *dt_name)
873
874
875
876
{
	if (len <= HEADER_SIZE)
		return; /* Insufficient size (no actual data), just bail out */

877
	image_header_t hdr = *(image_header_t *)buf;
878

879
880
881
882
883
	/* Check for a valid mkimage header */
	int image_type = get_image_type(buf, len);
	if (image_type <= IH_TYPE_INVALID) {
		switch (image_type) {
		case IH_TYPE_INVALID:
884
			pr_error("Invalid U-Boot image: bad size or signature\n");
885
886
			break;
		case IH_TYPE_ARCH_MISMATCH:
887
			pr_error("Invalid U-Boot image: wrong architecture\n");
888
889
			break;
		default:
890
891
			pr_error("Invalid U-Boot image: error code %d\n",
				 image_type);
892
		}
893
894
		exit(1);
	}
895
896
897
898
899
	if (image_type == IH_TYPE_FLATDT) {		/* FIT image */
		pr_error("FIT image not yet supported.\n");
		exit(1);
	}

900
901
902
903
	if (image_type != IH_TYPE_FIRMWARE)
		pr_fatal("U-Boot image type mismatch: "
			 "expected IH_TYPE_FIRMWARE, got %02X\n", image_type);

904
905
906
907
908
909
910
911
	/* The CRC is calculated on the whole header but the CRC itself */
	uint32_t hcrc = be32toh(hdr.ih_hcrc);
	hdr.ih_hcrc = 0;
	uint32_t computed_hcrc = crc32(0, (const uint8_t *) &hdr, HEADER_SIZE);
	if (hcrc != computed_hcrc)
		pr_fatal("U-Boot header CRC mismatch: expected %x, got %x\n",
			 hcrc, computed_hcrc);

912
913
	uint32_t data_size = be32toh(hdr.ih_size); /* Image Data Size */
	uint32_t load_addr = be32toh(hdr.ih_load); /* Data Load Address */
914
915
916
917
918
919
920
921
922
923
	if (data_size > len - HEADER_SIZE)
		pr_fatal("U-Boot image data trucated: "
			 "expected %zu bytes, got %u\n",
			 len - HEADER_SIZE, data_size);

	uint32_t dcrc = be32toh(hdr.ih_dcrc);
	uint32_t computed_dcrc = crc32(0, buf + HEADER_SIZE, data_size);
	if (dcrc != computed_dcrc)
		pr_fatal("U-Boot data CRC mismatch: expected %x, got %x\n",
			 dcrc, computed_dcrc);
924
925
926
927
928

	/* If we get here, we're "good to go" (i.e. actually write the data) */
	pr_info("Writing image \"%.*s\", %u bytes @ 0x%08X.\n",
		IH_NMLEN, buf + HEADER_NAME_OFFSET, data_size, load_addr);

929
	aw_write_buffer(dev, buf + HEADER_SIZE, load_addr, data_size, false);
930
931
932
933
934
935

	/* keep track of U-Boot memory region in global vars */
	uboot_entry = load_addr;
	uboot_size = data_size;
}

Andre Przywara's avatar
Andre Przywara committed
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
static const char *spl_get_dtb_name(uint8_t *spl_buf)
{
	uint32_t dt_offset;

	if (memcmp(spl_buf + 4, "eGON.BT0", 8))
		return NULL;

	if (memcmp(spl_buf + 0x14, "SPL", 3))
		return NULL;

	if (spl_buf[0x17] < 0x2)			/* only since v0.2 */
		return NULL;

	memcpy(&dt_offset, spl_buf + 0x20, 4);
	dt_offset = le32toh(dt_offset);

	if (verbose)
		printf("found DT name in SPL header: %s\n", spl_buf + dt_offset);

	return (char *)spl_buf + dt_offset;
}

958
959
960
/*
 * This function handles the common part of both "spl" and "uboot" commands.
 */
961
void aw_fel_process_spl_and_uboot(feldev_handle *dev, const char *filename)
962
963
{
	size_t size;
964
965
	uint32_t offset;
	/* load file into memory buffer */
966
	uint8_t *buf = load_file(filename, &size);
Andre Przywara's avatar
Andre Przywara committed
967
	const char *dt_name = spl_get_dtb_name(buf);
968

969
	/* write and execute the SPL from the buffer */
970
	offset = aw_fel_write_and_execute_spl(dev, buf, size);
971
	/* check for optional main U-Boot binary (and transfer it, if applicable) */
972
973
	if (size > offset) {
		/* U-Boot pads to at least 32KB */
974
975
		if (offset < SPL_MIN_OFFSET)
			offset = SPL_MIN_OFFSET;
Andre Przywara's avatar
Andre Przywara committed
976
977
		aw_fel_write_uboot_image(dev, buf + offset, size - offset,
					 dt_name);
978
	}
979
	free(buf);
980
981
}

982
983
984
985
986
987
988
989
/*
 * Test the SPL header for our "sunxi" variant. We want to make sure that
 * we can safely use specific header fields to pass information to U-Boot.
 * In case of a missing signature (e.g. Allwinner boot0) or header version
 * mismatch, this function will return "false". If all seems fine,
 * the result is "true".
 */
#define SPL_SIGNATURE			"SPL" /* marks "sunxi" header */
990
991
992
993
994
995
996
#define SPL_MAJOR_BITS			3
#define SPL_MINOR_BITS			5
#define SPL_VERSION(maj, min)		\
	((((maj) & ((1U << SPL_MAJOR_BITS) - 1)) << SPL_MINOR_BITS) | \
	((min) & ((1U << SPL_MINOR_BITS) - 1)))
#define SPL_MIN_VERSION			SPL_VERSION(0, 1)
#define SPL_MAX_VERSION			SPL_VERSION(0, 31)
997
bool have_sunxi_spl(feldev_handle *dev, uint32_t spl_addr)
998
999
1000
{
	uint8_t spl_signature[4];

1001
	aw_fel_read(dev, spl_addr + 0x14,
1002
1003
1004
		&spl_signature, sizeof(spl_signature));

	if (memcmp(spl_signature, SPL_SIGNATURE, 3) != 0)
1005
		return false; /* signature mismatch, no "sunxi" SPL */
1006
1007

	if (spl_signature[3] < SPL_MIN_VERSION) {
1008
1009
1010
1011
		pr_error("sunxi SPL version mismatch: "
			 "found 0x%02X < required minimum 0x%02X\n",
			 spl_signature[3], SPL_MIN_VERSION);
		pr_error("You need to update your U-Boot (mksunxiboot) to a more recent version.\n");
1012
		return false;
1013
1014
	}
	if (spl_signature[3] > SPL_MAX_VERSION) {
1015
1016
1017
1018
		pr_error("sunxi SPL version mismatch: "
			 "found 0x%02X > maximum supported 0x%02X\n",
			 spl_signature[3], SPL_MAX_VERSION);
		pr_error("You need a more recent version of this (sunxi-tools) fel utility.\n");
1019
		return false;
1020
	}
1021
	return true; /* sunxi SPL and suitable version */
1022
1023
1024
1025
}

/*
 * Pass information to U-Boot via specialized fields in the SPL header
1026
1027
 * (see "boot_file_head" in ${U-BOOT}/arch/arm/include/asm/arch-sunxi/spl.h),
 * providing the boot script address (DRAM location of boot.scr).
1028
 */
1029
void pass_fel_information(feldev_handle *dev,
1030
			  uint32_t script_address, uint32_t uEnv_length)
1031
{
1032
	soc_info_t *soc_info = dev->soc_info;
1033
1034

	/* write something _only_ if we have a suitable SPL header */
1035
	if (have_sunxi_spl(dev, soc_info->spl_addr)) {
1036
1037
1038
1039
1040
1041
1042
		pr_info("Passing boot info via sunxi SPL: "
			"script address = 0x%08X, uEnv length = %u\n",
			script_address, uEnv_length);
		uint32_t transfer[] = {
			htole32(script_address),
			htole32(uEnv_length)
		};
1043
		aw_fel_write(dev, transfer,
1044
			soc_info->spl_addr + 0x18, sizeof(transfer));
1045
1046
1047
	}
}

1048
1049
1050
1051
1052
1053
1054
1055
1056
/*
 * This function stores a given entry point to the RVBAR address for CPU0,
 * and then writes the Reset Management Register to request a warm boot.
 * It is useful with some AArch64 transitions, e.g. when passing control to
 * ARM Trusted Firmware (ATF) during the boot process of Pine64.
 *
 * The code was inspired by
 * https://github.com/apritzel/u-boot/commit/fda6bd1bf285c44f30ea15c7e6231bf53c31d4a8
 */
1057
void aw_rmr_request(feldev_handle *dev, uint32_t entry_point, bool aarch64)
1058
{
1059
	soc_info_t *soc_info = dev->soc_info;
1060
	if (!soc_info->rvbar_reg) {
1061
1062
1063
		pr_error("ERROR: Can't issue RMR request!\n"
			 "RVBAR is not supported or unknown for your SoC (%s).\n",
			 dev->soc_name);
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
		return;
	}

	uint32_t rmr_mode = (1 << 1) | (aarch64 ? 1 : 0); /* RR, AA64 flag */
	uint32_t arm_code[] = {
		htole32(0xe59f0028), /* ldr        r0, [rvbar_reg]          */
		htole32(0xe59f1028), /* ldr        r1, [entry_point]        */
		htole32(0xe5801000), /* str        r1, [r0]                 */
		htole32(0xf57ff04f), /* dsb        sy                       */
		htole32(0xf57ff06f), /* isb        sy                       */

		htole32(0xe59f101c), /* ldr        r1, [rmr_mode]           */
		htole32(0xee1c0f50), /* mrc        15, 0, r0, cr12, cr0, {2}*/
		htole32(0xe1800001), /* orr        r0, r0, r1               */
		htole32(0xee0c0f50), /* mcr        15, 0, r0, cr12, cr0, {2}*/
		htole32(0xf57ff06f), /* isb        sy                       */

		htole32(0xe320f003), /* loop:      wfi                      */
		htole32(0xeafffffd), /* b          <loop>                   */

		htole32(soc_info->rvbar_reg),
		htole32(entry_point),
		htole32(rmr_mode)
	};
	/* scratch buffer setup: transfers ARM code and parameter values */
1089
	aw_fel_write(dev, arm_code, soc_info->scratch_addr, sizeof(arm_code));
1090
1091
1092
1093
	/* execute the thunk code (triggering a warm reset on the SoC) */
	pr_info("Store entry point 0x%08X to RVBAR 0x%08X, "
		"and request warm reset with RMR mode %u...",
		entry_point, soc_info->rvbar_reg, rmr_mode);
1094
	aw_fel_execute(dev, soc_info->scratch_addr);
1095
1096
1097
	pr_info(" done.\n");
}

1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
/* Use the watchdog to simply reboot.  Useful to get out of fel without
 * power cycling or plugging.
 */
void aw_wd_reset(feldev_handle *dev)
{
	const watchdog_info *wd = dev->soc_info->watchdog;
	if (!wd) {
		pr_error("No watchdog information available (yet) for soc: %s\n", dev->soc_info->name);
		return;
	}
	fel_writel(dev, wd->reg_mode, wd->reg_mode_value);
	pr_info("Requested watchdog reset\n");
}

1112
1113
1114
1115
1116
1117
1118
1119
/* check buffer for magic "#=uEnv", indicating uEnv.txt compatible format */
static bool is_uEnv(void *buffer, size_t size)
{
	if (size <= 6)
		return false; /* insufficient size */
	return memcmp(buffer, "#=uEnv", 6) == 0;
}

1120
/* private helper function, gets used for "write*" and "multi*" transfers */
1121
static unsigned int file_upload(feldev_handle *dev, size_t count,
1122
				size_t argc, char **argv, progress_cb_t callback)
1123
{
1124
1125
1126
	if (argc < count * 2)
		pr_fatal("error: too few arguments for uploading %zu files\n",
			 count);
1127
1128
1129
1130
1131
1132
1133

	/* get all file sizes, keeping track of total bytes */
	size_t size = 0;
	unsigned int i;
	for (i = 0; i < count; i++)
		size += file_size(argv[i * 2 + 1]);

1134
	progress_start(callback, size); /* set total size and progress callback */
1135
1136
1137
1138
1139
1140

	/* now transfer each file in turn */
	for (i = 0; i < count; i++) {
		void *buf = load_file(argv[i * 2 + 1], &size);
		if (size > 0) {
			uint32_t offset = strtoul(argv[i * 2], NULL, 0);
1141
			aw_write_buffer(dev, buf, offset, size, callback != NULL);
1142

Bernhard Nortmann's avatar
Bernhard Nortmann committed
1143
			/* If we transferred a script, try to inform U-Boot about its address. */
1144
			if (get_image_type(buf, size) == IH_TYPE_SCRIPT)
1145
				pass_fel_information(dev, offset, 0);
1146
			if (is_uEnv(buf, size)) /* uEnv-style data */
1147
				pass_fel_information(dev, offset, size);
1148
1149
1150
1151
		}
		free(buf);
	}

Bernhard Nortmann's avatar
Bernhard Nortmann committed
1152
	return i; /* return number of files that were processed */
1153
1154
}

1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
static void felusb_list_devices(void)
{
	size_t devices; /* FEL device count */
	feldev_list_entry *list, *entry;

	list = list_fel_devices(&devices);
	for (entry = list; entry->soc_version.soc_id; entry++) {
		printf("USB device %03d:%03d   Allwinner %-8s",
			entry->busnum, entry->devnum, entry->soc_name);
		/* output SID only if non-zero */
		if (entry->SID[0] | entry->SID[1] | entry->SID[2] | entry->SID[3])
			printf("%08x:%08x:%08x:%08x",
			       entry->SID[0], entry->SID[1], entry->SID[2], entry->SID[3]);
		putchar('\n');
	}
	free(list);

	if (verbose && devices == 0)
1173
		pr_error("No Allwinner devices in FEL mode detected.\n");
1174
1175
1176
1177
1178

	feldev_done(NULL);
	exit(devices > 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}

1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
static void select_by_sid(const char *sid_arg, int *busnum, int *devnum)
{
	char sid[36];
	feldev_list_entry *list, *entry;

	list = list_fel_devices(NULL);
	for (entry = list; entry->soc_version.soc_id; entry++) {
		snprintf(sid, sizeof(sid), "%08x:%08x:%08x:%08x",
			entry->SID[0], entry->SID[1], entry->SID[2], entry->SID[3]);
		if (strcmp(sid, sid_arg) == 0) {
			*busnum = entry->busnum;
			*devnum = entry->devnum;
			break;
		}
	}
	free(list);
}

1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
void usage(const char *cmd) {
	puts("sunxi-fel " VERSION "\n");
	printf("Usage: %s [options] command arguments... [command...]\n"
		"	-h, --help			Print this usage summary and exit\n"
		"	-v, --verbose			Verbose logging\n"
		"	-p, --progress			\"write\" transfers show a progress bar\n"
		"	-l, --list			Enumerate all (USB) FEL devices and exit\n"
		"	-d, --dev bus:devnum		Use specific USB bus and device number\n"
		"	    --sid SID			Select device by SID key (exact match)\n"
		"\n"
		"	spl file			Load and execute U-Boot SPL\n"
		"		If file additionally contains a main U-Boot binary\n"
		"		(u-boot-sunxi-with-spl.bin), this command also transfers that\n"
		"		to memory (default address from image), but won't execute it.\n"
		"\n"
		"	uboot file-with-spl		like \"spl\", but actually starts U-Boot\n"
		"		U-Boot execution will take place when the fel utility exits.\n"
		"		This allows combining \"uboot\" with further \"write\" commands\n"
		"		(to transfer other files needed for the boot).\n"
		"\n"
		"	hex[dump] address length	Dumps memory region in hex\n"
		"	dump address length		Binary memory dump\n"
		"	exe[cute] address		Call function address\n"
		"	reset64 address			RMR request for AArch64 warm boot\n"
1221
		"	wdreset				Reboot via watchdog\n"
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
		"	memmove dest source size	Copy <size> bytes within device memory\n"
		"	readl address			Read 32-bit value from device memory\n"
		"	writel address value		Write 32-bit value to device memory\n"
		"	read address length file	Write memory contents into file\n"
		"	write address file		Store file contents into memory\n"
		"	write-with-progress addr file	\"write\" with progress bar\n"
		"	write-with-gauge addr file	Output progress for \"dialog --gauge\"\n"
		"	write-with-xgauge addr file	Extended gauge output (updates prompt)\n"
		"	multi[write] # addr file ...	\"write-with-progress\" multiple files,\n"
		"					sharing a common progress status\n"
		"	multi[write]-with-gauge ...	like their \"write-with-*\" counterpart,\n"
		"	multi[write]-with-xgauge ...	  but following the 'multi' syntax:\n"
		"					  <#> addr file [addr file [...]]\n"
		"	echo-gauge \"some text\"		Update prompt/caption for gauge output\n"
		"	ver[sion]			Show BROM version\n"
		"	sid				Retrieve and output 128-bit SID key\n"
		"	clear address length		Clear memory\n"
		"	fill address length value	Fill memory\n"
		, cmd);
1241
1242
	printf("\n");
	aw_fel_spiflash_help();
1243
1244
1245
	exit(0);
}

1246
1247
int main(int argc, char **argv)
{
1248
	bool uboot_autostart = false; /* flag for "uboot" command = U-Boot autostart */
1249
	bool pflag_active = false; /* -p switch, causing "write" to output progress */
1250
	bool device_list = false; /* -l switch, prints device list and exits */
1251
	feldev_handle *handle;
1252
	int busnum = -1, devnum = -1;
1253
	char *sid_arg = NULL;
1254

1255
1256
	if (argc <= 1)
		usage(argv[0]);
1257

1258
1259
	/* process all "prefix"-type arguments first */
	while (argc > 1) {
1260
1261
1262
		if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)
			usage(argv[0]);
		else if (strcmp(argv[1], "--verbose") == 0 || strcmp(argv[1], "-v") == 0)
1263
1264
1265
			verbose = true;
		else if (strcmp(argv[1], "--progress") == 0 || strcmp(argv[1], "-p") == 0)
			pflag_active = true;
1266
1267
1268
		else if (strcmp(argv[1], "--list") == 0 || strcmp(argv[1], "-l") == 0
			 || strcmp(argv[1], "list") == 0)
			device_list = true;
1269
1270
1271
1272
1273
1274
1275
1276
1277
		else if (strncmp(argv[1], "--dev", 5) == 0 || strncmp(argv[1], "-d", 2) == 0) {
			char *dev_arg = argv[1];
			dev_arg += strspn(dev_arg, "-dev="); /* skip option chars, ignore '=' */
			if (*dev_arg == 0 && argc > 2) { /* at end of argument, use the next one instead */
				dev_arg = argv[2];
				argc -= 1;
				argv += 1;
			}
			if (sscanf(dev_arg, "%d:%d", &busnum, &devnum) != 2
1278
1279
			    || busnum <= 0 || devnum <= 0)
				pr_fatal("ERROR: Expected 'bus:devnum', got '%s'.\n", dev_arg);
1280
			pr_info("Selecting USB Bus %03d Device %03d\n", busnum, devnum);
1281
1282
1283
1284
1285
		}
		else if (strcmp(argv[1], "--sid") == 0 && argc > 2) {
			sid_arg = argv[2];
			argc -= 1;
			argv += 1;
1286
1287
1288
1289
		} else
			break; /* no valid (prefix) option detected, exit loop */
		argc -= 1;
		argv += 1;
1290
	}
1291

1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
	/*
	 * If any option-style arguments remain (starting with '-') we know that
	 * we won't recognize them later (at best yielding "Invalid command").
	 * However this would only happen _AFTER_ trying to open a FEL device,
	 * which might fail with "Allwinner USB FEL device not found". To avoid
	 * confusing the user, bail out here - with a more descriptive message.
	 */
	int i;
	for (i = 1; i < argc; i++)
		if (*argv[i] == '-')
			pr_fatal("Invalid option %s\n", argv[i]);

	/* Process options that don't require a FEL device handle */
1305
1306
	if (device_list)
		felusb_list_devices(); /* and exit program afterwards */
1307
1308
1309
	if (sid_arg) {
		/* try to set busnum and devnum according to "--sid" option */
		select_by_sid(sid_arg, &busnum, &devnum);
1310
1311
1312
		if (busnum <= 0 || devnum <= 0)
			pr_fatal("No matching FEL device found for SID '%s'\n",
				 sid_arg);
1313
1314
		pr_info("Selecting FEL device %03d:%03d by SID\n", busnum, devnum);
	}
1315

1316
1317
1318
1319
	/*
	 * Open FEL device - either specified by busnum:devnum, or
	 * the first one matching the given USB vendor/procduct ID.
	 */
1320
	handle = feldev_open(busnum, devnum, AW_USB_VENDOR_ID, AW_USB_PRODUCT_ID);
1321

1322
1323
1324
	/* Some SoCs need the SMC workaround to enter the secure boot mode */
	aw_apply_smc_workaround(handle);

1325
	/* Handle command-style arguments, in order of appearance */
1326
1327
	while (argc > 1 ) {
		int skip = 1;
1328

1329
		if (strncmp(argv[1], "hex", 3) == 0 && argc > 3) {
1330
1331
1332
1333
1334
			aw_fel_hexdump(handle, strtoul(argv[2], NULL, 0), strtoul(argv[3], NULL, 0));
			skip = 3;
		} else if (strncmp(argv[1], "dump", 4) == 0 && argc > 3) {
			aw_fel_dump(handle, strtoul(argv[2], NULL, 0), strtoul(argv[3], NULL, 0));
			skip = 3;
Bernhard Nortmann's avatar
Bernhard Nortmann committed
1335
1336
1337
1338
1339
		} else if (strcmp(argv[1], "memmove") == 0 && argc > 4) {
			/* three parameters: destination addr, source addr, byte count */
			fel_memmove(handle, strtoul(argv[2], NULL, 0),
				    strtoul(argv[3], NULL, 0), strtoul(argv[4], NULL, 0));
			skip = 4;
1340
		} else if (strcmp(argv[1], "readl") == 0 && argc > 2) {
1341
			printf("0x%08x\n", fel_readl(handle, strtoul(argv[2], NULL, 0)));
1342
1343
			skip = 2;
		} else if (strcmp(argv[1], "writel") == 0 && argc > 3) {
1344
			fel_writel(handle, strtoul(argv[2], NULL, 0), strtoul(argv[3], NULL, 0));
1345
			skip = 3;
1346
		} else if (strncmp(argv[1], "exe", 3) == 0 && argc > 2) {
1347
1348
			aw_fel_execute(handle, strtoul(argv[2], NULL, 0));
			skip=3;
1349
1350
1351
1352
1353
		} else if (strcmp(argv[1], "reset64") == 0 && argc > 2) {
			aw_rmr_request(handle, strtoul(argv[2], NULL, 0), true);
			/* Cancel U-Boot autostart, and stop processing args */
			uboot_autostart = false;
			break;
1354
1355
		} else if (strcmp(argv[1], "wdreset") == 0) {
			aw_wd_reset(handle);
1356
		} else if (strncmp(argv[1], "ver", 3) == 0) {
1357
			aw_fel_print_version(handle);
1358
		} else if (strcmp(argv[1], "sid") == 0) {
1359
1360
1361
			aw_fel_print_sid(handle, false);
		} else if (strcmp(argv[1], "sid-registers") == 0) {
			aw_fel_print_sid(handle, true); /* enforce register access */
1362
		} else if (strcmp(argv[1], "write") == 0 && argc > 3) {
1363
1364
			skip += 2 * file_upload(handle, 1, argc - 2, argv + 2,
					pflag_active ? progress_bar : NULL);
1365
1366
1367
		} else if (strcmp(argv[1], "write-with-progress") == 0 && argc > 3) {
			skip += 2 * file_upload(handle, 1, argc - 2, argv + 2,
						progress_bar);
1368
1369
1370
1371
1372
1373
		} else if (strcmp(argv[1], "write-with-gauge") == 0 && argc > 3) {
			skip += 2 * file_upload(handle, 1, argc - 2, argv + 2,
						progress_gauge);
		} else if (strcmp(argv[1], "write-with-xgauge") == 0 && argc > 3) {
			skip += 2 * file_upload(handle, 1, argc - 2, argv + 2,
						progress_gauge_xxx);
1374
1375
1376
1377
1378
		} else if ((strcmp(argv[1], "multiwrite") == 0 ||
			    strcmp(argv[1], "multi") == 0) && argc > 4) {
			size_t count = strtoul(argv[2], NULL, 0); /* file count */
			skip = 2 + 2 * file_upload(handle, count, argc - 3,
						   argv + 3, progress_bar);
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
		} else if ((strcmp(argv[1], "multiwrite-with-gauge") == 0 ||
			    strcmp(argv[1], "multi-with-gauge") == 0) && argc > 4) {
			size_t count = strtoul(argv[2], NULL, 0); /* file count */
			skip = 2 + 2 * file_upload(handle, count, argc - 3,
						   argv + 3, progress_gauge);
		} else if ((strcmp(argv[1], "multiwrite-with-xgauge") == 0 ||
			    strcmp(argv[1], "multi-with-xgauge") == 0) && argc > 4) {
			size_t count = strtoul(argv[2], NULL, 0); /* file count */
			skip = 2 + 2 * file_upload(handle, count, argc - 3,
						   argv + 3, progress_gauge_xxx);
1389
1390
1391
1392
		} else if ((strcmp(argv[1], "echo-gauge") == 0) && argc > 2) {
			skip = 2;
			printf("XXX\n0\n%s\nXXX\n", argv[2]);
			fflush(stdout);
1393
1394
1395
1396
1397
1398
1399
		} else if (strcmp(argv[1], "read") == 0 && argc > 4) {
			size_t size = strtoul(argv[3], NULL, 0);
			void *buf = malloc(size);
			aw_fel_read(handle, strtoul(argv[2], NULL, 0), buf, size);
			save_file(argv[4], buf, size);
			free(buf);
			skip=4;
1400
		} else if (strcmp(argv[1], "clear") == 0 && argc > 2) {
Henrik Nordstrom's avatar
Henrik Nordstrom committed
1401
			aw_fel_fill(handle, strtoul(argv[2], NULL, 0), strtoul(argv[3], NULL, 0), 0);
1402
			skip=3;
Henrik Nordstrom's avatar
Henrik Nordstrom committed
1403
1404
1405
		} else if (strcmp(argv[1], "fill") == 0 && argc > 3) {
			aw_fel_fill(handle, strtoul(argv[2], NULL, 0), strtoul(argv[3], NULL, 0), (unsigned char)strtoul(argv[4], NULL, 0));
			skip=4;
1406
		} else if (strcmp(argv[1], "spl") == 0 && argc > 2) {
1407
1408
1409
1410
			aw_fel_process_spl_and_uboot(handle, argv[2]);
			skip=2;
		} else if (strcmp(argv[1], "uboot") == 0 && argc > 2) {
			aw_fel_process_spl_and_uboot(handle, argv[2]);
1411
1412
			uboot_autostart = (uboot_entry > 0 && uboot_size > 0);
			if (!uboot_autostart)
1413
				printf("Warning: \"uboot\" command failed to detect image! Can't execute U-Boot.\n");
1414
			skip=2;
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
		} else if (strcmp(argv[1], "spiflash-info") == 0) {
			aw_fel_spiflash_info(handle);
		} else if (strcmp(argv[1], "spiflash-read") == 0 && argc > 4) {
			size_t size = strtoul(argv[3], NULL, 0);
			void *buf = malloc(size);
			aw_fel_spiflash_read(handle, strtoul(argv[2], NULL, 0), buf, size,
					     pflag_active ? progress_bar : NULL);
			save_file(argv[4], buf, size);
			free(buf);
			skip=4;
		} else if (strcmp(argv[1], "spiflash-write") == 0 && argc > 3) {
			size_t size;
			void *buf = load_file(argv[3], &size);
			aw_fel_spiflash_write(handle, strtoul(argv[2], NULL, 0), buf, size,
					      pflag_active ? progress_bar : NULL);
			free(buf);
			skip=3;
1432
		} else {
1433
			pr_fatal("Invalid command %s\n", argv[1]);
1434
1435
1436
1437
1438
		}
		argc-=skip;
		argv+=skip;
	}

Bernhard Nortmann's avatar
Bernhard Nortmann committed
1439
	/* auto-start U-Boot if requested (by the "uboot" command) */
1440
	if (uboot_autostart) {
1441
		pr_info("Starting U-Boot (0x%08X).\n", uboot_entry);
1442
1443
1444
1445
		if (enter_in_aarch64)
			aw_rmr_request(handle, uboot_entry, true);
		else
			aw_fel_execute(handle, uboot_entry);
1446
1447
	}

1448
	feldev_done(handle);
1449

1450
1451
	return 0;
}