lm4flash.c 11.7 KB
Newer Older
Fabio Utzig's avatar
Fabio Utzig committed
1
/* lm4flash - TI Stellaris Launchpad ICDI flasher
Fabio Utzig's avatar
Fabio Utzig committed
2
 * Copyright (C) 2012 Fabio Utzig <fabio@utzig.net>
3
 * Copyright (C) 2012 Peter Stuge <peter@stuge.se>
Fabio Utzig's avatar
Fabio Utzig committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
 */

Fabio Utzig's avatar
Fabio Utzig committed
20
21
#include <stdio.h>
#include <stdlib.h>
22
23
#include <stdint.h>
#include <string.h>
Fabio Utzig's avatar
Fabio Utzig committed
24
25
26
27
28

#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>

Fabio Utzig's avatar
Fabio Utzig committed
29
#include <libusb.h>
Fabio Utzig's avatar
Fabio Utzig committed
30

Fabio Utzig's avatar
Fabio Utzig committed
31
//#define DEBUG 1
32

33
34
35
#define ICDI_VID 0x1cbe
#define ICDI_PID 0x00fd

36
37
38
39
40
41
42
43
// Flash Patch and Breakpoint: see ARM Debug Interface V5 Architecture Specification
static const uint32_t FPB     = 0xe0002000;

// Rom Control: see Stellaris LM4F120H5QR Microcontroller Page XXX
static const uint32_t XXX1CTL  = 0x400fe000;
static const uint32_t XXX2CTL  = 0xe000edf0;
static const uint32_t XXX3CTL  = 0x400fe060;
static const uint32_t XXX4CTL  = 0x400fe1a0;
Fabio Utzig's avatar
Fabio Utzig committed
44
45

// Rom Control: see Stellaris LM4F120H5QR Microcontroller Page 531
46
static const uint32_t ROMCTL  = 0x400fe0f0;
Fabio Utzig's avatar
Fabio Utzig committed
47
48

// Flash Memory Address: see Stellaris LM4F120H5QR Microcontroller Page 497
49
50
51
52
53
54
static const uint32_t FMA     = 0x400fd000;

static const uint8_t INTERFACE_NR = 0x02;
static const uint8_t ENDPOINT_IN  = 0x83;
static const uint8_t ENDPOINT_OUT = 0x02;

55
56
57
58
59
60
61
62
63
64
65
66
#define START "$"
#define END "#"

#ifdef WIN32
#define snprintf _snprintf
#define SNPRINTF_OFFSET 1
#else
#define SNPRINTF_OFFSET 0
#endif

#define START_LEN strlen(START)
#define END_LEN (strlen(END) + 2)
67

68
#define FLASH_BLOCK_SIZE 512
69

70
71
72
73
74
75
76
77
78
79
80
/* Prefix + potentially every flash byte escaped */
#define BUF_SIZE 64 + 2*FLASH_BLOCK_SIZE

static uint8_t flash_block[FLASH_BLOCK_SIZE];
static union {
	char c[BUF_SIZE];
	uint8_t u8[BUF_SIZE];
	uint32_t u32[BUF_SIZE / 4];
} buf;

static uint32_t le32_to_cpu(const uint32_t x)
81
{
82
83
84
85
86
87
88
89
90
	union {
		uint8_t  b8[4];
		uint32_t b32;
	} _tmp;
	_tmp.b8[3] = x >> 24;
	_tmp.b8[2] = x >> 16;
	_tmp.b8[1] = x >> 8;
	_tmp.b8[0] = x & 0xff;
	return _tmp.b32;
91
92
}

Fabio Utzig's avatar
Fabio Utzig committed
93
94
static int do_verify = 0;

95
96
#define cpu_to_le32 le32_to_cpu

97
98
99
100
101
102
static int send_command(libusb_device_handle *handle, int size)
{
	int transferred = 0;
	int retval;

#ifdef DEBUG
103
104
	int i, col;

Fabio Utzig's avatar
Fabio Utzig committed
105
106
	printf("buffer:\n");
	for (i = 0, col = 1; i < size; i++, col++) {
107
		printf("%02x ", buf.u8[i]);
Fabio Utzig's avatar
Fabio Utzig committed
108
109
110
		if (col == 16) { col = 0; printf("\n"); }
	}
	printf("\n");
111
112
#endif

113
	retval = libusb_bulk_transfer(handle, ENDPOINT_OUT, buf.u8, size, &transferred, 0);
114
115
116
117
118
119
120
121
122
123
	if (retval != 0 || size != transferred) {
		printf("Error transmitting data %d\n", retval);
	}

	return retval;
}

static int wait_response(libusb_device_handle *handle, int *size)
{
	int retval;
124
#ifdef DEBUG
125
	int i;
126
#endif
127

128
	retval = libusb_bulk_transfer(handle, ENDPOINT_IN, buf.u8, BUF_SIZE, size, 0);
129
130
131
132
133
134
135
136
	if (retval != 0) {
		printf("Error receiving data %d\n", retval);
	}

#ifdef DEBUG
	printf("wait_response: size=%d\n", *size);
	printf("buffer: ");
	for (i = 0; i < *size; i++)
137
		printf("0x%02x ", buf.u8[i]);
138
139
140
141
142
143
	printf("\n");
#endif

	return retval;
}

Fabio Utzig's avatar
Fabio Utzig committed
144
static int checksum_and_send(libusb_device_handle *handle, size_t idx, int *xfer)
145
{
146
147
148
	size_t i;
	uint8_t sum = 0;
	int retval, transfered;
149

150
151
	if (idx + SNPRINTF_OFFSET + END_LEN > BUF_SIZE)
		return LIBUSB_ERROR_NO_MEM;
152

153
154
	for (i = 1; i < idx; i++)
		sum += buf.u8[i];
155

156
	idx += sprintf(buf.c + idx, END "%02x", sum);
157
158

	retval = send_command(handle, idx);
159
160
	if (retval)
		return retval;
161

162
163
164
165
	/* wait for ack (+/-) */
	retval = wait_response(handle, &transfered);
	if (retval)
		return retval;
166

167
168
	if (transfered != 1 || buf.c[0] != '+')
		return LIBUSB_ERROR_OTHER;
169

170
171
	/* wait for command response */
	retval = wait_response(handle, &transfered);
Fabio Utzig's avatar
Fabio Utzig committed
172
173
	if (xfer)
		*xfer = transfered;
174

175
	/* FIXME: validate transfered here? */
176
177
178
179

	return retval;
}

Fabio Utzig's avatar
Fabio Utzig committed
180

181
182
183
static int send_u8_hex(libusb_device_handle *handle, const char *prefix, const char *bytes, size_t num_bytes)
{
	size_t i, idx;
184

185
186
187
188
189
	/* Make sure that everything fits!
	 * START + prefix + hex bytes + END + hex checksum + '\0'
	 */
	if (START_LEN + (prefix ? strlen(prefix) : 0) + (2 * num_bytes) + END_LEN + 1 > BUF_SIZE)
		return LIBUSB_ERROR_NO_MEM;
190

191
	idx = sprintf(buf.c, START "%s", prefix);
192

193
194
	for (i = 0; bytes && i < num_bytes; i++)
		idx += sprintf(buf.c + idx, "%02x", bytes[i]);
195

Fabio Utzig's avatar
Fabio Utzig committed
196
	return checksum_and_send(handle, idx, NULL);
197
198
}

199
static int send_u8_binary(libusb_device_handle *handle, const char *prefix, const char *bytes, size_t num_bytes)
200
201
202
{
	size_t idx;

203
204
205
206
207
	/* Make sure that everything fits!
	 * START + prefix + bytes + END + hex checksum + '\0'
	 */
	if (START_LEN + (prefix ? strlen(prefix) : 0) + num_bytes + END_LEN + 1 > BUF_SIZE)
		return LIBUSB_ERROR_NO_MEM;
208

209
	idx = sprintf(buf.c, START "%s", prefix);
210

211
212
	memcpy(buf.c + idx, bytes, num_bytes);
	idx += num_bytes;
213

Fabio Utzig's avatar
Fabio Utzig committed
214
	return checksum_and_send(handle, idx, NULL);
215
216
}

217
static int send_u32(libusb_device_handle *handle, const char *prefix, const uint32_t val, const char *suffix)
218
{
219
220
221
	size_t idx = snprintf(buf.c, BUF_SIZE, START "%s%08x%s",
			prefix ? prefix : "", val,
			suffix ? suffix : "");
222

Fabio Utzig's avatar
Fabio Utzig committed
223
	return checksum_and_send(handle, idx, NULL);
224
225
}

226
static int send_u32_u32(libusb_device_handle *handle, const char *prefix, const uint32_t val1, const char *infix, const uint32_t val2, const char *suffix)
227
{
228
229
230
231
	size_t idx = snprintf(buf.c, BUF_SIZE, START "%s%08x%s%08x%s",
			prefix ? prefix : "", val1,
			infix ? infix : "", val2,
			suffix ? suffix : "");
232

Fabio Utzig's avatar
Fabio Utzig committed
233
	return checksum_and_send(handle, idx, NULL);
234
}
235
236


237
238
239
static int send_mem_write(libusb_device_handle *handle, const uint32_t addr, const uint32_t val)
{
	return send_u32_u32(handle, "X", addr, ",4:", val, NULL);
240
241
}

242
static int send_mem_read(libusb_device_handle *handle, const uint32_t addr, uint32_t *val)
Fabio Utzig's avatar
Fabio Utzig committed
243
{
244
245
246
	int retval = send_u32(handle, "x", addr, ",4");
	if (retval)
		return retval;
Fabio Utzig's avatar
Fabio Utzig committed
247

248
249
	if (val)
		*val = le32_to_cpu(buf.u32[0]);
Fabio Utzig's avatar
Fabio Utzig committed
250

251
252
	return 0;
}
Fabio Utzig's avatar
Fabio Utzig committed
253

254
255
256
257
static int send_flash_erase(libusb_device_handle *handle, const uint32_t start, const uint32_t end)
{
	return send_u32_u32(handle, "vFlashErase:", start, ",", end, NULL);
}
Fabio Utzig's avatar
Fabio Utzig committed
258

259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
static int send_flash_write(libusb_device_handle *handle, const uint32_t addr, const uint8_t *bytes, size_t len)
{
	size_t i;
	char prefix[] = "vFlashWrite:12345678:";
	char by, rawbuf[1024], *buf = rawbuf;

	sprintf(strchr(prefix, ':') + 1, "%08x:", addr);

	for (i = 0; i < len; i++)
		switch (by = bytes[i]) {
		case '#':
		case '$':
		case '}':
			*buf++ = '}';
			by ^= 0x20;
			/* fall through */
		default:
			if (buf >= rawbuf + sizeof(rawbuf))
				return LIBUSB_ERROR_NO_MEM;

			*buf++ = by;
			break;
Fabio Utzig's avatar
Fabio Utzig committed
281
282
		}

283
	i = buf - rawbuf;
Fabio Utzig's avatar
Fabio Utzig committed
284

285
	return send_u8_binary(handle, prefix, rawbuf, i) ? -1 : i;
Fabio Utzig's avatar
Fabio Utzig committed
286
287
}

Fabio Utzig's avatar
Fabio Utzig committed
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
static int send_flash_verify(libusb_device_handle *handle, const uint32_t addr, const uint8_t *bytes, size_t len)
{
	size_t i, j;
	char by, rawbuf[1024], *bp = rawbuf;
	int retval, transfered;

	size_t idx = snprintf(buf.c, BUF_SIZE, START "x%x,%x", addr, (uint32_t)len);

	retval = checksum_and_send(handle, idx, &transfered);

	for (i = 0; i < transfered; i++) {
		switch (by = buf.u8[i]) {
		case '}':
			by = buf.u8[++i] ^ 0x20;
			/* fall through */
		default:
			if (bp >= rawbuf + sizeof(rawbuf))
				return LIBUSB_ERROR_NO_MEM;

			*bp++ = by;
			break;
		}
	}

	if (strncmp(rawbuf, "$OK:", 4) != 0)
		return LIBUSB_ERROR_OTHER;

	for (i = 0, j = strlen("$OK:"); i < len; i++, j++) {
		if (bytes[i] != (uint8_t)rawbuf[j]) {
			printf("Error verifying flash\n");
			return LIBUSB_ERROR_OTHER;
		}
	}

	return 0;
}

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
#define SEND_COMMAND(cmd) do { \
	int r = send_u8_hex(handle, "qRcmd,", (cmd), sizeof((cmd)) - 1); \
	if (r) \
		return r; \
} while (0)

#define SEND_STRING(str) do { \
	int r = send_u8_hex(handle, (str), NULL, 0); \
	if (r) \
		return r; \
} while (0)

#define MEM_WRITE(address, value) do { \
	int r = send_mem_write(handle, (address), (value)); \
	if (r) \
		return r; \
} while (0)

#define MEM_READ(address, value) do { \
	int r = send_mem_read(handle, (address), (value)); \
	if (r) \
		return r; \
} while (0)

#define FLASH_ERASE(start, end) do { \
	int r = send_flash_erase(handle, (start), (end)); \
	if (r) \
		return r; \
} while (0)

#define FLASH_WRITE(address, data, length) do { \
	int r = send_flash_write(handle, (address), (data), (length)); \
	if (r < (length)) \
		return LIBUSB_ERROR_OTHER; \
} while (0)

Fabio Utzig's avatar
Fabio Utzig committed
361
362
363
364
365
/*
 *  This flow is of commands is based on an USB capture of
 *  traffic between LM Flash Programmer and the Stellaris Launchpad
 *  when doing a firmware write
 */
366
static int write_firmware(libusb_device_handle *handle, FILE *f)
367
368
{
	uint32_t val = 0;
Fabio Utzig's avatar
Fabio Utzig committed
369
	uint32_t addr;
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
	size_t rdbytes;

	SEND_COMMAND("debug clock \0");
	SEND_STRING("qSupported");
	SEND_STRING("?");
	MEM_WRITE(FPB, 0x3000000);
	MEM_READ(XXX1CTL, &val);
	MEM_READ(XXX1CTL + 4, &val);
	SEND_STRING("?");
	MEM_READ(XXX2CTL, &val);
	SEND_COMMAND("debug sreset");
	MEM_READ(XXX2CTL, &val);
	MEM_READ(ROMCTL, &val);
	MEM_WRITE(ROMCTL, 0x0);
	MEM_READ(XXX2CTL, &val);
	MEM_READ(XXX3CTL, &val);
	MEM_READ(XXX1CTL, &val);
	MEM_READ(XXX1CTL + 4, &val);
	MEM_READ(XXX1CTL + 8, &val);
	MEM_READ(XXX1CTL, &val);
	MEM_READ(XXX4CTL, &val);
Fabio Utzig's avatar
Fabio Utzig committed
391
392

	/* XXX: Repeated below, why? */
393
394
395
396
397
	MEM_WRITE(FMA, 0x0);
	MEM_READ(XXX2CTL, &val);
	FLASH_ERASE(0, 0);
	SEND_COMMAND("debug creset");
	MEM_READ(XXX2CTL, &val);
Fabio Utzig's avatar
Fabio Utzig committed
398

399
	MEM_WRITE(XXX2CTL, 0x0);
Fabio Utzig's avatar
Fabio Utzig committed
400
401

	/* XXX: this is the same sequence of the above commands? */
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
	MEM_WRITE(FMA, 0x200);
	MEM_READ(XXX2CTL, &val);
	FLASH_ERASE(0, 0);
	SEND_COMMAND("debug creset");
	MEM_READ(XXX2CTL, &val);

	MEM_READ(ROMCTL, &val);
	MEM_WRITE(ROMCTL, 0x0);
	MEM_READ(XXX2CTL, &val);

	for (addr = 0; !feof(f); addr += sizeof(flash_block)) {
		rdbytes = fread(flash_block, 1, sizeof(flash_block), f);

		if (rdbytes < sizeof(flash_block) && !feof(f)) {
			perror("fread");
			return LIBUSB_ERROR_OTHER;
		}

		FLASH_WRITE(addr, flash_block, rdbytes);
	}

Fabio Utzig's avatar
Fabio Utzig committed
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
	if (do_verify) {
		fseek(f, 0, SEEK_SET);

		for (addr = 0; !feof(f); addr += sizeof(flash_block)) {
			rdbytes = fread(flash_block, 1, sizeof(flash_block), f);

			if (rdbytes < sizeof(flash_block) && !feof(f)) {
				perror("fread");
				return LIBUSB_ERROR_OTHER;
			}

			/* On error don't return immediately... finish resetting the board */
			if (send_flash_verify(handle, addr, flash_block, rdbytes) != 0)
				break;
		}
	}

440
441
442
443
444
445
446
447
448
449
	SEND_COMMAND("set vectorcatch 0");
	SEND_COMMAND("debug disable");

	/* reset board */
	MEM_WRITE(FPB, 0x3000000);
	SEND_COMMAND("debug hreset");
	SEND_COMMAND("set vectorcatch 0");
	SEND_COMMAND("debug disable");

	return 0;
450
}
Fabio Utzig's avatar
Fabio Utzig committed
451
452
453

int main(int argc, char *argv[])
{
454
455
456
	libusb_context *ctx = NULL;
	libusb_device_handle *handle = NULL;
	int retval = 1;
457
	FILE *f = NULL;
Fabio Utzig's avatar
Fabio Utzig committed
458
459

	if (argc < 2) {
Fabio Utzig's avatar
Fabio Utzig committed
460
461
		printf("usage: %s [-v] <binary-file>\n", argv[0]);
		printf("\t-v : enables verification after write\n");
462
		goto done;
Fabio Utzig's avatar
Fabio Utzig committed
463
	}
Fabio Utzig's avatar
Fabio Utzig committed
464

Fabio Utzig's avatar
Fabio Utzig committed
465
466
467
	if ((argc == 3) && (strncmp(argv[1], "-v", strlen("-v")) == 0))
		do_verify = 1;

Fabio Utzig's avatar
Fabio Utzig committed
468
469
	if (libusb_init(&ctx) != 0) {
		fprintf(stderr, "Error initializing libusb\n");
470
		goto done;
Fabio Utzig's avatar
Fabio Utzig committed
471
472
	}

473
	/* FIXME: should not be using this function call! */
474
	handle = libusb_open_device_with_vid_pid(ctx, ICDI_VID, ICDI_PID);
Fabio Utzig's avatar
Fabio Utzig committed
475
	if (!handle) {
476
477
		fprintf(stderr, "No ICDI device with USB VID:PID %04x:%04x found!\n",
				ICDI_VID, ICDI_PID);
478
		goto done;
Fabio Utzig's avatar
Fabio Utzig committed
479
480
	}

481
	retval = libusb_claim_interface(handle, INTERFACE_NR);
Fabio Utzig's avatar
Fabio Utzig committed
482
483
	if (retval != 0) {
		printf("Error claiming interface %d\n", retval);
484
		goto done;
Fabio Utzig's avatar
Fabio Utzig committed
485
486
	}

Fabio Utzig's avatar
Fabio Utzig committed
487
	f = fopen(argv[argc - 1], "rb");
488
489
	if (!f) {
		perror("fopen");
490
491
		retval = 1;
		goto done;
Fabio Utzig's avatar
Fabio Utzig committed
492
493
	}

494
	retval = write_firmware(handle, f);
Fabio Utzig's avatar
Fabio Utzig committed
495

496
done:
497
498
	if (f)
		fclose(f);
499
500
501
502
503
504
	if (handle)
		libusb_close(handle);
	if (ctx)
		libusb_exit(ctx);

	return retval;
Fabio Utzig's avatar
Fabio Utzig committed
505
}