-
Notifications
You must be signed in to change notification settings - Fork 0
/
st-uart-bootloader.c
371 lines (326 loc) · 9.41 KB
/
st-uart-bootloader.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
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <sys/uio.h>
#define RESP_ACK 0x79
#define RESP_NACK 0x1f
static void hexdump(const char* tag, int ascii, const void* p, size_t nb) {
const uint8_t* bs = (const uint8_t*)p;
for (unsigned i = 0; i < nb; i += 16) {
fprintf(stderr, "%s:%03x:", tag, i);
unsigned j;
for (j = 0; i + j < nb && j < 16; j++)
fprintf(stderr, " %02x", bs[i + j]);
if (ascii) {
for (/**/; j < 16; j++)
fputs(" ", stderr);
fputs(" ", stderr);
for (j = 0; i + j < nb && j < 16; j++)
fprintf(stderr, "%c", isprint(bs[i + j]) ? bs[i + j] : '.');
}
fputc('\n', stderr);
}
}
static ssize_t cmd_issue(int fd, uint8_t cmd, uint8_t* bs, size_t nbs) {
struct timeval to;
memset(&to, 0, sizeof(to));
to.tv_sec = 8;
fd_set fds;
FD_ZERO(&fds);
FD_SET(fd, &fds);
int n = select(fd + 1, NULL, &fds, NULL, &to);
if (n < 0)
fprintf(stderr, "%s:%d: select:%d:%s\n",
__func__, __LINE__,
errno, strerror(errno));
fprintf(stderr, "%s:%d: to:%d.%06u\n",
__func__, __LINE__,
to.tv_sec, to.tv_usec);
assert(n == 1);
assert(FD_ISSET(fd, &fds));
/* the read commands are all one byte but all but the switch are
* followed w/ a checksum byte.
*/
uint8_t out[2];
size_t nout;
out[0] = cmd;
if (cmd == 0x7f) {
nout = 1;
} else {
nout = 2;
out[1] = ~cmd;
}
ssize_t io = write(fd, out, nout);
if (io != nout)
fprintf(stderr, "%s:%d: write(%02x:%zu):%d:%s\n",
__func__, __LINE__,
cmd, nout,
errno, strerror(errno));
assert(io == nout);
n = select(fd + 1, &fds, NULL, NULL, &to);
if (n < 0)
fprintf(stderr, "%s:%d: select:%d:%s\n",
__func__, __LINE__,
errno, strerror(errno));
fprintf(stderr, "%s:%d: to:%d.%06u\n",
__func__, __LINE__,
to.tv_sec, to.tv_usec);
assert(n == 1);
assert(FD_ISSET(fd, &fds));
/* reply */
io = read(fd, bs, nbs);
if (io < 0)
fprintf(stderr, "%s:%d: read:%d:%s\n",
__func__, __LINE__,
errno, strerror(errno));
assert(io > 0);
hexdump("ack", 0, bs, io);
return io;
}
static int param_issue(int fd, int prependn,
const uint8_t* param, uint8_t nparam) {
struct timeval to;
memset(&to, 0, sizeof(to));
to.tv_sec = 8;
fd_set fds;
FD_ZERO(&fds);
FD_SET(fd, &fds);
int n = select(fd + 1, NULL, &fds, NULL, &to);
if (n < 0)
fprintf(stderr, "%s:%d: select:%d:%s\n",
__func__, __LINE__,
errno, strerror(errno));
fprintf(stderr, "%s:%d: to:%d.%06u\n",
__func__, __LINE__,
to.tv_sec, to.tv_usec);
assert(n == 1);
assert(FD_ISSET(fd, &fds));
/* the doc mumbles about xor(param bytes) but then says
* for size==1 data that it's the complement. i'm not sure
* how both can be true.
*/
uint8_t temp = prependn ? (nparam - 1) : 0;
uint8_t xsum;
assert(nparam > 0);
if (nparam == 1) {
xsum = ~param[0];
} else {
xsum = temp;
for (int i = 0; i < nparam; i++)
xsum ^= param[i];
}
struct iovec iovs[3];
int i = 0;
if (prependn) {
iovs[i].iov_base = &temp;
iovs[i++].iov_len = 1;
}
iovs[i].iov_base = (void*)param;
iovs[i++].iov_len = nparam;
iovs[i].iov_base = &xsum;
iovs[i++].iov_len = 1;
ssize_t io = writev(fd, iovs, i);
if (io != prependn + nparam + 1)
fprintf(stderr, "%s:%d: write:%d:%s\n",
__func__, __LINE__,
errno, strerror(errno));
assert(io == prependn + nparam + 1);
/* wait for ack/nack */
n = select(fd + 1, &fds, NULL, NULL, &to);
if (n < 0)
fprintf(stderr, "%s:%d: select:%d:%s\n",
__func__, __LINE__,
errno, strerror(errno));
fprintf(stderr, "%s:%d: to:%d.%06u\n",
__func__, __LINE__,
to.tv_sec, to.tv_usec);
assert(n == 1);
assert(FD_ISSET(fd, &fds));
uint8_t ack;
io = read(fd, &ack, 1);
if (io < 0)
fprintf(stderr, "%s:%d: read:%d:%s\n",
__func__, __LINE__,
errno, strerror(errno));
assert(io > 0);
fprintf(stderr, "%s:%d: ack:%02x\n", __func__, __LINE__, ack);
return ack == RESP_ACK;
}
static ssize_t read_n(int fd,
size_t o, size_t end,
uint8_t* bs, size_t nbs) {
while (o < end) {
struct timeval to;
memset(&to, 0, sizeof(to));
to.tv_sec = 8;
fd_set fds;
FD_ZERO(&fds);
FD_SET(fd, &fds);
int n = select(fd + 1, &fds, NULL, NULL, &to);
if (n < 0)
fprintf(stderr, "%s:%d: select:%d:%s\n",
__func__, __LINE__,
errno, strerror(errno));
fprintf(stderr, "%s:%d: to:%d.%06u\n",
__func__, __LINE__,
to.tv_sec, to.tv_usec);
assert(n == 1);
assert(FD_ISSET(fd, &fds));
n = read(fd, bs + o, nbs - o);
assert(n > 0);
o += n;
}
return o;
}
static void cmd_get(int fd, uint8_t* bs, size_t nbs) {
/* get command */
ssize_t nb = cmd_issue(fd, 0x00, bs, nbs);
assert(bs[0] == RESP_ACK);
/* data should look like:
* ack (n-1) version cmds* ack|nack
* where n==folling bytes up to ack|nack
*/
assert(nb > 1);
if (nb < 1 + 1 + bs[1] + 1 + 1)
nb = read_n(fd, nb, 1 + 1 + bs[1] + 1 + 1, bs, nbs);
hexdump("get", 0, bs, nb);
}
static void cmd_get_version_info(int fd, uint8_t* bs, size_t nbs) {
/* get version info command */
ssize_t nb = cmd_issue(fd, 0x01, bs, nbs);
assert(bs[0] == RESP_ACK);
/* data should look like:
* ack version option1 option2 ack|nack
*/
assert(nb > 1);
if (nb < 5)
nb = read_n(fd, nb, 5, bs, nbs);
hexdump("get_version_info", 0, bs, nb);
}
static void cmd_get_id(int fd, uint8_t* bs, size_t nbs) {
/* get id command == debug id code */
ssize_t nb = cmd_issue(fd, 0x02, bs, nbs);
assert(bs[0] == RESP_ACK);
/* data should look like:
* ack (n-1) id* ack|nack
*/
assert(nb > 1);
if (nb < 1 + 1 + bs[1] + 1 + 1)
nb = read_n(fd, nb, 1 + 1 + bs[1] + 1 + 1, bs, nbs);
hexdump("get_id", 0, bs, nb);
}
static void cmd_read_unprotect(int fd, uint8_t* bs, size_t nbs) {
ssize_t nb = cmd_issue(fd, 0x92, bs, nbs);
assert(bs[0] == RESP_ACK);
/* data should look like:
* ack <erasing time> ack|nack
*/
assert(nb >= 1);
while (nb < 1 + 1)
nb = read_n(fd, nb, 1 + 1, bs, nbs);
hexdump("read_unprotect", 0, bs, nb);
}
static void cmd_read_mem(int fd,
uint32_t addr, uint32_t n,
uint8_t* bs, size_t nbs) {
uint8_t temp[4];
assert(nbs >= n);
size_t nb = cmd_issue(fd, 0x11, temp, sizeof(temp));
assert(temp[0] == RESP_ACK);
/* address in big-endian */
temp[0] = (addr >> 24) & 0xff;
temp[1] = (addr >> 16) & 0xff;
temp[2] = (addr >> 8) & 0xff;
temp[3] = (addr >> 0) & 0xff;
int issue = param_issue(fd, 0, temp, 4);
assert(issue);
/* send n-1 to receive n */
assert(n > 0);
assert(n <= 0x100);
temp[0] = n - 1;
issue = param_issue(fd, 0, temp, 1);
assert(issue);
nb = read_n(fd, 0, n, bs, nbs);
hexdump("read_mem", 0, bs, nb);
}
static void cmd_write_mem(int fd,
uint32_t addr, uint32_t n,
const uint8_t* bs) {
assert(n > 0);
assert(n <= 0x100);
assert((n & 3) == 0);
uint8_t temp[4];
size_t nb = cmd_issue(fd, 0x31, temp, sizeof(temp));
assert(temp[0] == RESP_ACK);
/* address in big-endian */
temp[0] = (addr >> 24) & 0xff;
temp[1] = (addr >> 16) & 0xff;
temp[2] = (addr >> 8) & 0xff;
temp[3] = (addr >> 0) & 0xff;
int issue = param_issue(fd, 0, temp, 4);
assert(issue);
issue = param_issue(fd, 1, bs, n);
assert(issue);
}
int main(int argc, char** argv) {
assert(argc > 1);
int fd = open(argv[1], O_RDWR | O_NOCTTY);
if (fd < 0)
fprintf(stderr, "%s:%d: open(%s):%d:%s\n",
__func__, __LINE__,
argv[1],
errno, strerror(errno));
assert(fd > 0);
/* set mostly raw mode */
struct termios tty;
int err;
memset(&tty, 0, sizeof(tty));
tty.c_iflag = IGNBRK;
tty.c_cflag = CS8 | CREAD | CLOCAL | PARENB;
/* min stlink update speed is 1200bps */
speed_t baud = B9600;
if ((err = cfsetspeed(&tty, baud)) < 0)
fprintf(stderr, "%s:%d: cfsetspeed:%d:%s\n",
__func__, __LINE__,
errno, strerror(errno));
assert(err == 0);
if ((err = tcsetattr(fd, TCSANOW, &tty)) < 0)
fprintf(stderr, "%s:%d: tcsetattr:%d:%s\n",
__func__, __LINE__,
errno, strerror(errno));
assert(err == 0);
uint8_t buf[256];
ssize_t nb;
/* bootloader to memory boot mode.
* if it's already switched then it replies w/ a nack
* but is going to go to command processing.
*/
nb = cmd_issue(fd, 0x7f, buf, sizeof(buf));
assert(nb == 1);
assert(buf[0] == RESP_ACK || buf[0] == RESP_NACK);
cmd_get(fd, buf, sizeof(buf));
cmd_get_id(fd, buf, sizeof(buf));
cmd_get_version_info(fd, buf, sizeof(buf));
#if 0
/* i think the doc says that these always read as zero?
* - option bytes should be complements
* - only one unprotect setting
*/
if ((buf[2] != ~buf[3]) || (buf[2] != 0xa5))
cmd_read_unprotect(fd, buf, sizeof(buf));
#endif
cmd_read_mem(fd, 0x20004ff0, 0x10, buf, sizeof(buf));
for (int i = 0; i < 0x10; i++)
buf[i] = ~buf[i];
cmd_write_mem(fd, 0x20004ff0, 0x10, buf);
cmd_read_mem(fd, 0x20004ff0, 0x10, buf, sizeof(buf));
close(fd);
return 0;
}