-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkwuartboot.c
433 lines (362 loc) · 8.83 KB
/
kwuartboot.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
433
/*
* Copyright (C) 2011
* Leigh Brown <[email protected]>
*
* 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., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#define SOH 0x01
#define STX 0x02
#define EOT 0x04
#define ACK 0x06
#define NAK 0x15
#define CAN 0x18
#define CTRLZ 0x1A
#define PATTERN_SEND_INTERVAL (50000) /* 50 milliseconds */
#define PATTERN_SEND_TIMEOUT 20 /* seconds */
#define ONE_SECOND_US (1000000) /* 1 second */
#define RECEIVE_TIMEOUT (60 * 1000000) /* 60 seconds */
#define MAX_RETRANS (10)
const unsigned char boot_pattern[] =
{ 0xbb, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
char *argv0;
void
usage(void)
{
fprintf(stderr,
"Usage: %s <serial device> <filename>\n", argv0);
exit(1);
}
int
read_block(int fd, unsigned char *buf, int size)
{
int tot = 0;
int rem = size;
int got;
while (rem > 0) {
got = read(fd, buf, rem);
if (got == 0)
break;
if (got < 0)
return -1; /* errno will be set to the error */
tot += got;
buf += got;
rem -= got;
}
/* Fill the remainder of the packet with zeroes */
if (rem > 0)
memset(buf, 0, rem);
return tot;
}
int
read_byte(int fd, unsigned int timeout)
{
fd_set rfds;
struct timeval tv;
unsigned char byte;
int got;
int retval;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
tv.tv_sec = timeout / ONE_SECOND_US;
tv.tv_usec = timeout % ONE_SECOND_US;
retval = select(fd + 1, &rfds, NULL, NULL, &tv);
if (retval == 0)
errno = ETIMEDOUT; /* synthesize our own error code for timeout */
if (retval < 1)
return -1;
got = read(fd, &byte, 1);
assert(got != 0);
if (got < 0)
return -1; /* errno will be set to the error */
return (int)byte;
}
static inline int
write_byte(int fd, int c)
{
unsigned char byte;
byte = (unsigned char)c;
return write(fd, &byte, 1);
}
void
rotator(void)
{
static const char rotator[] = { '|', '/', '-', '\\' };
static int pos = 0;
printf("%c\b", rotator[pos]);
fflush(stdout);
pos = (pos + 1) % 4;
}
void
cancel_send(int fd)
{
static const unsigned char cancel[] = { CAN, CAN, CAN };
if (write(fd, cancel, sizeof(cancel)) == sizeof(cancel))
if (!tcdrain(fd))
tcflush(fd, TCIOFLUSH);
}
int
send_boot_pattern(int fd, int timeout)
{
int wrote;
int c;
int done;
time_t start, now;
/* Make a note of the time we start */
start = time(NULL);
/* Flush any pending input or output */
tcflush(fd, TCIOFLUSH);
/* Keep sending the boot pattern until we timeout or we get x-modem NAK */
printf("Sending boot pattern: power on or reset now...");
done = 0;
while (!done) {
rotator();
wrote = write(fd, boot_pattern, sizeof(boot_pattern));
if (wrote == -1)
return -1;
assert(wrote == sizeof(boot_pattern)); /* assumption */
/* Wait for the queue to drain before checking for a response */
if (tcdrain(fd) < 0)
return -1;
/* read any returned characters */
for (;;) {
c = read_byte(fd, PATTERN_SEND_INTERVAL);
if (c == -1) {
if (errno == ETIMEDOUT)
break;
return -1;
}
if (c == NAK) { /* System is waiting for x-modem packet */
done = 1;
break;
}
/* Ignore any echoed characters from the boot pattern itself */
if (memchr(boot_pattern, c, sizeof(boot_pattern)) == NULL) {
printf("*.");
fflush(stdout);
}
}
/* Check for timeout */
now = time(NULL);
if (now - start > timeout) {
printf("timeout\n");
return 1;
}
}
printf("done\n");
return 0;
}
int
wait_for_nak(int fd)
{
int c;
c = read_byte(fd, RECEIVE_TIMEOUT);
if (c == -1) {
fprintf(stderr, "%s: error reading from serial port: %m\n", argv0);
return -1;
}
assert(c >= 0 && c <= 255);
switch (c) {
case 'C':
printf("unexpected CRC-mode character\n");
return -1;
case NAK:
break;
case CAN: /* request by remote to cancel transmission */
/* wait for a second CAN, just in case */
if ((c = read_byte(fd, ONE_SECOND_US)) == CAN) {
write_byte(fd, ACK);
printf("cancelled by remote\n");
return -1;
}
break;
default:
printf("unexpected character %02x, cancelling send\n", c);
cancel_send(fd);
return -1;
}
return 0;
}
int
build_packet(int fd, int packetno, unsigned char packet[], int buflen)
{
int got, i;
assert(buflen > 0);
/* Fill in the header */
packet[0] = SOH;
packet[1] = (unsigned char)packetno;
packet[2] = ~(unsigned char)packetno;
/* Read the next block from the file */
got = read_block(fd, &packet[3], buflen);
if (got == -1) {
fprintf(stderr, "%s: error reading from file: %m\n", argv0);
return -1;
}
if (got == 0)
return 1;
/* Calculate checksum and insert at the end of the packet */
unsigned char chksum = 0;
for (i = 3; i < buflen + 3; ++i) {
chksum += packet[i];
}
packet[buflen + 3] = chksum;
return 0;
}
int
send_packet(int fd, unsigned char packet[], int len)
{
int i;
/* Send the complete packet across the serial line */
for (i = 0; i < len; ++i) {
if (write_byte(fd, packet[i]) == -1)
return -1;
/* Flow control doesn't seem to work very well so we
use tcdrain to ensure we don't send too fast */
if (i % 8 == 0)
if (tcdrain(fd) < 0)
return -1;
}
/* Return the response */
return read_byte(fd, RECEIVE_TIMEOUT);
}
int
xmodem_send(int tty_fd, int in_fd)
{
unsigned char packet[134];
static const int buflen = 128;
int packetno, lastpacket;
int c;
int retry;
int res;
printf("Sending file...");
/* We have already received a NAK, but wait for another to be sure */
if (wait_for_nak(tty_fd) == -1)
return -1;
lastpacket = 0;
packetno = 1;
for (retry = 0; retry < MAX_RETRANS; ++retry) {
rotator();
if (packetno != lastpacket) {
res = build_packet(in_fd, packetno, packet, buflen);
if (res == -1) return -1;
if (res == 1) break;
lastpacket = packetno;
}
else
printf("*%d*.", retry);
c = send_packet(tty_fd, packet, buflen + 4);
if (c == -1) {
if (errno == ETIMEDOUT)
continue; /* retry if timed out */
fprintf(stderr,
"%s: error writing to serial port: %m\n", argv0);
break;
}
switch (c) {
case ACK:
retry = 0;
++packetno;
break;
case CAN:
printf("cancelled by remote\n");
if ((c = read_byte(tty_fd, 1)) == CAN) {
write_byte(tty_fd, ACK);
tcdrain(tty_fd);
return -1;
}
break;
default:
printf("unexpected character %02x\n", c);
case NAK:
break;
}
}
if (retry == MAX_RETRANS) {
fprintf(stderr, "Too many retries, cancelling send\n");
cancel_send(tty_fd);
return -1; /* xmit error */
}
printf("\nFinishing...");
for (retry = 0; retry < MAX_RETRANS; ++retry) {
write_byte(tty_fd, EOT);
tcdrain(tty_fd);
if ((c = read_byte(tty_fd, 1)) == ACK)
break;
}
if (c != ACK) {
puts("failed\n");
return -1;
}
puts("done\n");
return 0;
}
int
main(int argc, char *argv[])
{
struct termios tio;
char *dev;
char *fname;
int tty_fd, file_fd;
int res;
argv0 = argv[0];
if (argc != 3)
usage();
dev = argv[1];
fname = argv[2];
memset(&tio, 0, sizeof(tio));
tio.c_iflag = 0;
tio.c_oflag = 0;
tio.c_cflag = CS8|CREAD|CLOCAL;
tio.c_lflag = 0;
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 5;
tty_fd = open(dev, O_RDWR | O_NONBLOCK);
if (tty_fd < 0) {
fprintf(stderr, "%s: unable to open '%s': %m\n", argv0, dev);
exit(1);
}
cfsetospeed(&tio, B115200);
cfsetispeed(&tio, B115200);
tcsetattr(tty_fd, TCSANOW, &tio);
file_fd = open(fname, O_RDONLY);
if (file_fd < 0) {
fprintf(stderr, "%s: unable to open '%s': %m\n", argv0, dev);
exit(1);
}
res = send_boot_pattern(tty_fd, PATTERN_SEND_TIMEOUT);
switch (res) {
case 0:
xmodem_send(tty_fd, file_fd);
break;
case -1:
fprintf(stderr, "%s: failed to send boot pattern: %m\n", argv0);
case 1:
break;
}
close(tty_fd);
close(file_fd);
return 0;
}