-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbsl.c
432 lines (349 loc) · 8.92 KB
/
bsl.c
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
432
/* MSPDebug - debugging tool for the eZ430
* Copyright (C) 2009, 2010 Daniel Beer
*
* 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
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include "bsl.h"
#include "util.h"
#include "output.h"
#include "fet_error.h"
#if defined(__APPLE__) || defined(__OpenBSD__)
#define B460800 460800
#endif
struct bsl_device {
struct device base;
int serial_fd;
uint8_t reply_buf[256];
int reply_len;
};
#define DATA_HDR 0x80
#define DATA_ACK 0x90
#define DATA_NAK 0xA0
static int bsl_ack(struct bsl_device *dev)
{
uint8_t reply;
if (read_with_timeout(dev->serial_fd, &reply, 1) < 0) {
printc_err("bsl: failed to receive reply\n");
return -1;
}
if (reply == DATA_NAK) {
printc_err("bsl: received NAK\n");
return -1;
}
if (reply != DATA_ACK) {
printc_err("bsl: bad ack character: %x\n", reply);
return -1;
}
return 0;
}
static int bsl_sync(struct bsl_device *dev)
{
static const uint8_t c = DATA_HDR;
int tries = 2;
if (tcflush(dev->serial_fd, TCIFLUSH) < 0) {
pr_error("bsl: tcflush");
return -1;
}
while (tries--)
if (!(write_all(dev->serial_fd, &c, 1) || bsl_ack(dev)))
return 0;
printc_err("bsl: sync failed\n");
return -1;
}
static int send_command(struct bsl_device *dev,
int code, uint16_t addr,
const uint8_t *data, int len)
{
uint8_t pktbuf[256];
uint8_t cklow = 0xff;
uint8_t ckhigh = 0xff;
int pktlen = data ? len + 4 : 4;
int i;
if (pktlen + 6 > sizeof(pktbuf)) {
printc_err("bsl: payload too large: %d\n", len);
return -1;
}
pktbuf[0] = DATA_HDR;
pktbuf[1] = code;
pktbuf[2] = pktlen;
pktbuf[3] = pktlen;
pktbuf[4] = addr & 0xff;
pktbuf[5] = addr >> 8;
pktbuf[6] = len & 0xff;
pktbuf[7] = len >> 8;
if (data)
memcpy(pktbuf + 8, data, len);
for (i = 0; i < pktlen + 4; i += 2)
cklow ^= pktbuf[i];
for (i = 1; i < pktlen + 4; i += 2)
ckhigh ^= pktbuf[i];
pktbuf[pktlen + 4] = cklow;
pktbuf[pktlen + 5] = ckhigh;
return write_all(dev->serial_fd, pktbuf, pktlen + 6);
}
static int verify_checksum(struct bsl_device *dev)
{
uint8_t cklow = 0xff;
uint8_t ckhigh = 0xff;
int i;
for (i = 0; i < dev->reply_len; i += 2)
cklow ^= dev->reply_buf[i];
for (i = 1; i < dev->reply_len; i += 2)
ckhigh ^= dev->reply_buf[i];
if (cklow || ckhigh) {
printc_err("bsl: checksum invalid (%02x %02x)\n",
cklow, ckhigh);
return -1;
}
return 0;
}
static int fetch_reply(struct bsl_device *dev)
{
dev->reply_len = 0;
for (;;) {
int r = read_with_timeout(dev->serial_fd,
dev->reply_buf + dev->reply_len,
sizeof(dev->reply_buf) -
dev->reply_len);
if (r < 0)
return -1;
dev->reply_len += r;
if (dev->reply_buf[0] == DATA_ACK) {
return 0;
} else if (dev->reply_buf[0] == DATA_HDR) {
if (dev->reply_len >= 6 &&
dev->reply_len == dev->reply_buf[2] + 6)
return verify_checksum(dev);
} else if (dev->reply_buf[0] == DATA_NAK) {
printc_err("bsl: received NAK\n");
return -1;
} else {
printc_err("bsl: unknown reply type: %02x\n",
dev->reply_buf[0]);
return -1;
}
if (dev->reply_len >= sizeof(dev->reply_buf)) {
printc_err("bsl: reply buffer overflow\n");
return -1;
}
}
}
static int bsl_xfer(struct bsl_device *dev,
int command_code, uint16_t addr, const uint8_t *txdata,
int len)
{
if (bsl_sync(dev) < 0 ||
send_command(dev, command_code, addr, txdata, len) < 0 ||
fetch_reply(dev) < 0) {
printc_err("bsl: failed on command 0x%02x "
"(addr = 0x%04x, len = 0x%04x)\n",
command_code, addr, len);
return -1;
}
return 0;
}
#define CMD_TX_DATA 0x38
#define CMD_ERASE 0x39
#define CMD_RX_DATA 0x3a
#define CMD_RESET 0x3b
static void bsl_destroy(device_t dev_base)
{
struct bsl_device *dev = (struct bsl_device *)dev_base;
bsl_xfer(dev, CMD_RESET, 0, NULL, 0);
close(dev->serial_fd);
free(dev);
}
static int bsl_ctl(device_t dev_base, device_ctl_t type)
{
switch (type) {
case DEVICE_CTL_HALT:
/* Ignore halt requests */
return 0;
case DEVICE_CTL_RESET:
/* Ignore reset requests */
return 0;
default:
printc_err("bsl: CPU control is not possible\n");
}
return -1;
}
static device_status_t bsl_poll(device_t dev_base)
{
return DEVICE_STATUS_HALTED;
}
static int bsl_getregs(device_t dev_base, address_t *regs)
{
printc_err("bsl: register fetch is not implemented\n");
return -1;
}
static int bsl_setregs(device_t dev_base, const address_t *regs)
{
printc_err("bsl: register store is not implemented\n");
return -1;
}
static int bsl_writemem(device_t dev_base,
address_t addr, const uint8_t *mem, address_t len)
{
struct bsl_device *dev = (struct bsl_device *)dev_base;
if (addr >= 0x10000 || len > 0x10000 || addr + len > 0x10000) {
printc_err("bsl: memory write out of range\n");
return -1;
}
while (len) {
int wlen = len > 100 ? 100 : len;
int r;
r = bsl_xfer(dev, CMD_RX_DATA, addr, mem, wlen);
if (r < 0) {
printc_err("bsl: failed to write to 0x%04x\n",
addr);
return -1;
}
mem += wlen;
len -= wlen;
addr += wlen;
}
return 0;
}
static int bsl_readmem(device_t dev_base,
address_t addr, uint8_t *mem, address_t len)
{
struct bsl_device *dev = (struct bsl_device *)dev_base;
if (addr >= 0x10000 || len > 0x10000 || addr + len > 0x10000) {
printc_err("bsl: memory read out of range\n");
return -1;
}
while (len) {
address_t count = len;
if (count > 128)
count = 128;
if (bsl_xfer(dev, CMD_TX_DATA, addr, NULL, count) < 0) {
printc_err("bsl: failed to read memory\n");
return -1;
}
if (count > dev->reply_buf[2])
count = dev->reply_buf[2];
memcpy(mem, dev->reply_buf + 4, count);
mem += count;
len -= count;
}
return 0;
}
static int bsl_erase(device_t dev_base, device_erase_type_t type,
address_t addr)
{
struct bsl_device *dev = (struct bsl_device *)dev_base;
if (type != DEVICE_ERASE_MAIN) {
printc_err("bsl: only main erase is supported\n");
return -1;
}
/* Constants found from viewing gdbproxy's activities */
return bsl_xfer(dev, CMD_ERASE, 0x2500, NULL, 0x0069);
}
static int enter_via_fet(struct bsl_device *dev)
{
uint8_t buf[16];
uint8_t *data = buf;
int len = 8;
/* Enter bootloader command */
if (write_all(dev->serial_fd,
(uint8_t *)"\x7e\x24\x01\x9d\x5a\x7e", 6)) {
printc_err("bsl: couldn't write bootloader transition "
"command\n");
return -1;
}
/* Wait for reply */
while (len) {
int r = read_with_timeout(dev->serial_fd, data, len);
if (r < 0) {
printc_err("bsl: couldn't read bootloader "
"transition acknowledgement\n");
return -1;
}
data += r;
len -= r;
}
/* Check that it's what we expect */
if (memcmp(buf, "\x06\x00\x24\x00\x00\x00\x61\x01", 8)) {
printc_err("bsl: bootloader start returned error "
"%d (%s)\n", buf[5], fet_error(buf[5]));
return -1;
}
return 0;
}
static device_t bsl_open(const struct device_args *args)
{
struct bsl_device *dev;
if (!(args->flags & DEVICE_FLAG_TTY)) {
printc_err("This driver does not support raw USB access.\n");
return NULL;
}
dev = malloc(sizeof(*dev));
if (!dev) {
pr_error("bsl: can't allocate memory");
return NULL;
}
memset(dev, 0, sizeof(*dev));
dev->base.type = &device_bsl;
dev->serial_fd = open_serial(args->path, B460800);
if (dev->serial_fd < 0) {
printc_err("bsl: can't open %s: %s\n",
args->path, strerror(errno));
free(dev);
return NULL;
}
if (enter_via_fet(dev) < 0)
return NULL;
usleep(500000);
/* Show chip info */
if (bsl_xfer(dev, CMD_TX_DATA, 0xff0, NULL, 0x10) < 0) {
printc_err("bsl: failed to read chip info\n");
goto fail;
}
if (dev->reply_len < 0x16) {
printc_err("bsl: missing chip info\n");
goto fail;
}
printc_dbg("Device ID: 0x%02x%02x\n",
dev->reply_buf[4], dev->reply_buf[5]);
printc_dbg("BSL version is %x.%02x\n", dev->reply_buf[14],
dev->reply_buf[15]);
return (device_t)dev;
fail:
close(dev->serial_fd);
free(dev);
return NULL;
}
const struct device_class device_bsl = {
.name = "uif-bsl",
.help = "TI FET430UIF bootloader.",
.open = bsl_open,
.destroy = bsl_destroy,
.readmem = bsl_readmem,
.writemem = bsl_writemem,
.erase = bsl_erase,
.getregs = bsl_getregs,
.setregs = bsl_setregs,
.ctl = bsl_ctl,
.poll = bsl_poll
};