-
Notifications
You must be signed in to change notification settings - Fork 6
/
jh7100-recover.c
467 lines (379 loc) · 9.24 KB
/
jh7100-recover.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
* This simple tool developed for the StarFive JH7100 SoC. The BootROM
* XMODEM protocol implimentation has a bug that prevents standard
* lrzsz tools to send files over XMODEM.
*
* Copyright (c) 2021 Kali Prasad <[email protected]>
*
* License: MIT
*/
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <termios.h>
#include <unistd.h>
//#define DEBUG
#ifdef DEBUG
#define debug(format, ...) printf("Debug:" format "\n", ##__VA_ARGS__);
#else
#define debug(format, ...) ;
#endif
#define SOH 0x01
#define ACK 0x06
#define NAK 0x15
#define EOT 0x04
#define min(a, b) ((a) < (b) ? (a) : (b))
#define PBSTR "########################################"
#define PBWIDTH 40
#define DEBUG_BAUD 9600
#define XMODEM_PAYLOAD_LEN 128 /* the length of xmodem packet payload*/
#define BUFF_SIZE 128 /* the length of buffer*/
struct xmodem_packet {
uint8_t start;
uint8_t block;
uint8_t block_neg;
uint8_t payload[XMODEM_PAYLOAD_LEN];
uint16_t crc;
} __attribute__((packed));
static const char bootrom_str[] = "(C)SiFive\r\n";
static const char success_str[] = "updata success\r\n";
static const char xmodem_str[] = "send a file by xmodem\r\n";
static const char *serial_device;
static const char *progname;
static void progress_bar(double percentage, size_t len, size_t len_f)
{
int val = (int) (percentage * 100);
int lpad = (int) (percentage * PBWIDTH);
int rpad = PBWIDTH - lpad;
printf("\r[%.*s%*s] %3d%%", lpad, PBSTR, rpad, "", val);
printf(" %zu/%zu Bytes", len, len_f);
fflush(stdout);
}
/* for CRC */
static uint16_t crc_update(uint16_t crc_in, int incr)
{
uint16_t xor = crc_in >> 15;
uint16_t out = crc_in << 1;
if (incr)
out++;
if (xor)
out ^= 0x1021;
return out;
}
static uint16_t swap16(uint16_t in)
{
return (in >> 8) | ((in & 0xff) << 8);
}
static uint16_t crc16(const uint8_t *data, uint16_t size)
{
uint16_t crc, i;
for (crc = 0; size > 0; size--, data++)
for (i = 0x80; i; i >>= 1)
crc = crc_update(crc, *data & i);
for (i = 0; i < 16; i++)
crc = crc_update(crc, 0);
return crc;
}
/* for CRC */
static int xmodem_send(int serial_f, const char *filename)
{
int ret, fd;
size_t len, len_f;
uint8_t response;
struct stat stat;
uint8_t eot = EOT;
const uint8_t *buf;
struct xmodem_packet packet;
double progress;
fd = open(filename, O_RDWR | O_SYNC);
if (fd < 0) {
fprintf(stderr, "Can NOT open file: %s", filename);
return -errno;
}
fstat(fd, &stat);
len = len_f = stat.st_size;
buf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
if (!buf) {
perror("mmap");
return -errno;
}
printf("\tWaiting for XMODEM request[C]...\n");
fflush(stdout);
debug("[probing C]:");
do {
ret = read(serial_f, &response, sizeof(response));
if (ret != sizeof(response)) {
perror("read");
return -errno;
}
debug("%c", response);
} while (response != 'C');
debug("Got C");
printf("\tSending %s \n", filename);
packet.block = 1;
packet.start = SOH;
while (len) {
size_t z = 0;
int next = 0;
// char status;
z = min(len, sizeof(packet.payload));
memcpy(packet.payload, buf, z);
memset(packet.payload + z, 0xff, sizeof(packet.payload) - z);
packet.crc = swap16(crc16(packet.payload, sizeof(packet.payload)));
packet.block_neg = 0xff - packet.block;
ret = write(serial_f, &packet, sizeof(packet));
if (ret != sizeof(packet))
return -errno;
ret = read(serial_f, &response, sizeof(response));
if (ret != sizeof(response))
return -errno;
switch (response) {
case NAK:
// status = 'N';
break;
case ACK:
// status = '.';
next = 1;
break;
default:
// status = '?';
next = 1;
break;
}
progress = (len_f - len) / (len_f / 100);
if(len<XMODEM_PAYLOAD_LEN)
progress_bar(1, len_f, len_f);
else
progress_bar(progress / 100, (len_f - len), len_f);
if (next) {
packet.block++;
len -= z;
buf += z;
}
}
ret = write(serial_f, &eot, sizeof(eot));
if (ret != sizeof(eot))
return -errno;
printf("\n");
close(fd);
return 0;
}
static int open_serial(const char *path, int baud, int canonical)
{
int fd;
struct termios tty;
fd = open(path, O_RDWR | O_SYNC);
if (fd < 0) {
perror("Can NOT open serial port.");
return -errno;
}
memset(&tty, 0, sizeof(tty));
if (tcgetattr(fd, &tty) != 0) {
perror("tcgetattr");
return -errno;
}
cfsetospeed(&tty, baud);
cfsetispeed(&tty, baud);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
if(canonical)
tty.c_lflag |= ICANON;
else
tty.c_lflag = 0;
tty.c_oflag = 0;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 10;
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_cflag |= (CLOCAL | CREAD);
tty.c_cflag &= ~(PARENB | PARODD);
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
perror("tcsetattr");
return -errno;
}
return fd;
}
static void check_success(void)
{
int ret, serial_f;
char buf[BUFF_SIZE];
printf("Awaiting confirmation...\n");
serial_f = open_serial(serial_device, DEBUG_BAUD, 1);
if (serial_f < 0)
exit(EXIT_FAILURE);
do {
ret = read(serial_f, buf, sizeof(buf));
buf[ret] = '\0';
debug("GOT[%d]:%s", ret, buf);
} while(strcmp(success_str, buf));
debug("Hit: updata success");
printf("done.\n\n");
close(serial_f);
}
static void initialize(void)
{
int ret, serial_f;
char buf[BUFF_SIZE];
serial_f = open_serial(serial_device, DEBUG_BAUD, 1);
if (serial_f < 0)
exit(EXIT_FAILURE);
printf("Waiting for bootloader mode on %s...\n", serial_device);
do {
ret = read(serial_f, buf, BUFF_SIZE);
buf[ret] = '\0';
debug("GOT[%d]:%s", ret, buf);
} while(strcmp(bootrom_str, buf));
debug("Hit: (C)SiFive");
printf("Bootloader mode active\n\n");
close(serial_f);
}
static void send_recovery(const char *filename)
{
int ret, serial_f;
char cmd1[] = "load 0x18000000\n";
char cmd2[] = "do 0x18000000\n";
serial_f = open_serial(serial_device, DEBUG_BAUD, 0);
if (serial_f < 0)
exit(EXIT_FAILURE);
ret = write(serial_f, &cmd1, sizeof(cmd1));
if (ret != sizeof(cmd1))
exit(EXIT_FAILURE);
ret = xmodem_send(serial_f, filename);
if (ret < 0)
exit(EXIT_FAILURE);
ret = write(serial_f, &cmd2, sizeof(cmd2));
if (ret != sizeof(cmd2))
exit(EXIT_FAILURE);
fflush(stdout);
close(serial_f);
}
static void select_update_option(int option)
{
int ret, serial_f;
char buf[BUFF_SIZE];
uint8_t new_line[] = {13,10};
char o1[] = "0\r\n";
char o2[] = "1\r\n";
serial_f = open_serial(serial_device, DEBUG_BAUD, 1);
if (serial_f < 0)
exit(EXIT_FAILURE);
if(option) {
ret = write(serial_f, &o2, sizeof(o2));
if (ret != sizeof(o2))
exit(EXIT_FAILURE);
debug("SEND[%lu]:%s", sizeof(o2), o2);
} else {
ret = write(serial_f, &o1, sizeof(o1));
if (ret != sizeof(o1))
exit(EXIT_FAILURE);
debug("SEND[%lu]:%s", sizeof(o1), o1);
}
ret = write(serial_f, &new_line, sizeof(new_line));
if (ret != sizeof(new_line))
exit(EXIT_FAILURE);
debug("SEND[%lu]:CR LF", sizeof(new_line));
do {
ret = read(serial_f, buf, BUFF_SIZE);
buf[ret] = '\0';
debug("GOT[%d]:%s", ret, buf);
} while(strcmp(xmodem_str, buf));
debug("Hit: send a file by xmodem");
close(serial_f);
}
static void update_firmware(const char *filename)
{
int ret, serial_f;
serial_f = open_serial(serial_device, DEBUG_BAUD, 0);
if (serial_f < 0)
exit(EXIT_FAILURE);
ret = xmodem_send(serial_f, filename);
if (ret < 0)
exit(EXIT_FAILURE);
close(serial_f);
check_success();
}
static void usage(void)
{
fprintf(stderr,
"Usage: %s [OPTION]... \n"
"-D, --device <tty device> : Serial tty device path.\n"
"-r, --recovery <filename> : Bootloader recovery firmware.\n"
"-b, --bootloader <filename> : Second stage bootloader.\n"
"-d, --ddrinit <filename> : DRAM initialization firmware.\n"
"-h, --help : Show this help.\n", progname);
}
static const char *optstring = "-hD:r:b:d:";
static const struct option long_options[] = {
{ "device", 1, NULL, 'D' },
{ "recovery", 1, NULL, 'r' },
{ "bootloader", 1, NULL, 'b' },
{ "ddrinit", 1, NULL, 'd' },
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 },
};
int main(int argc, char **argv)
{
char *recovery_f = NULL, *bootloader_f = NULL, *ddr_init_f = NULL;
int c;
progname = argv[0];
while ((c = getopt_long(argc, argv, optstring, long_options, NULL)) >= 0) {
switch (c) {
case 'r':
recovery_f = optarg;
break;
case 'b':
bootloader_f = optarg;
break;
case 'd':
ddr_init_f = optarg;
break;
case 'D':
serial_device = optarg;
break;
case 'h':
usage();
exit(EXIT_SUCCESS);
break;
default:
goto error;
}
}
if(serial_device) {
initialize();
} else {
fprintf(stderr, "Need serial device path.\n");
goto error;
}
if(recovery_f) {
printf("Uploading recovery binary...\n");
send_recovery(recovery_f);
printf("\n----------Enter recovery mode----------\n");
} else {
fprintf(stderr, "Need recovery file.\n");
goto error;
}
if (bootloader_f || ddr_init_f) {
if(bootloader_f) {
printf("Updating bootloader...\n");
select_update_option(0);
update_firmware(bootloader_f);
}
if(ddr_init_f) {
printf("Updating dduinit...\n");
select_update_option(1);
update_firmware(ddr_init_f);
}
printf("\nFirmware update completed!\n");
exit(EXIT_SUCCESS);
}
error:
usage();
exit(EXIT_FAILURE);
}