-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheeprog.c
319 lines (288 loc) · 8.98 KB
/
eeprog.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
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <strings.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#define PAGE_SIZE 256
void usage(char *argv[]) {
printf("Read & write SPI EEPROM (W25Q80BV or similar) via zeeprog on Arduino\n");
printf("Usage:\n");
//printf(" %s erase <port>\n", argv[0]);
printf(" %s upload <filename> <port> <addr>\n", argv[0]);
printf(" %s download <filename> <port> <addr> <size>\n", argv[0]);
printf(" %s verify <filename> <port> <addr>\n", argv[0]);
//printf(" %s info <port>\n", argv[0]);
printf("\n");
}
// quick rush copy/paste & fixup from pda/Arduino-ARMSID-configurator
int open_serial(char *name) {
struct termios term;
int file = open(name, O_RDWR | O_NOCTTY);
if (file == -1) {
fprintf(stderr, "Error\n");
return -1;
}
if (tcgetattr(file, &term) != 0) {
fprintf(stderr, "Error getting device state\n");
close(file);
return -1;
}
cfmakeraw(&term);
cfsetspeed(&term, B115200);
term.c_cc[VMIN] = 1; // read doesn't return until at least N bytes
term.c_cc[VTIME] = 0; // read timeout between bytes (0 = no timeout)
if (tcsetattr(file, TCSANOW, &term) != 0) {
fprintf(stderr, "Error setting device parameters\n");
close(file);
return -1;
}
return file;
}
int expect(int serial, char *str) {
char *match_ptr = str;
do {
char byte_in;
int r = read(serial, &byte_in, 1);
if (r == -1) {
perror("expect read");
return -1;
} else if (r == 0) {
continue;
}
printf("%c", byte_in);
if (byte_in == *match_ptr) {
match_ptr++; // byte matches, prepare to test the next one
} else {
match_ptr = str;
}
} while (*match_ptr != 0);
return 0;
}
int cmd_upload(char *srcfile, char *port, char *addr_str) {
FILE *file = fopen(srcfile, "r");
if (file < 0) {
fprintf(stderr, "error opening file %s\n", srcfile);
return(-1);
}
struct stat stat;
if (fstat(fileno(file), &stat) == -1) {
perror("fstat");
return(-1);
}
uint32_t size = stat.st_size;
int serial = open_serial(port);
if (serial < 0) {
fprintf(stderr, "error opening serial port %s\n", port);
return(-1);
}
uint32_t addr_from = strtol(addr_str, NULL, 0);
uint32_t addr_to = addr_from + size;
printf("Write %s → %s 0x%06X–0x%06X (%d bytes)\n", srcfile, port, addr_from, addr_to, size);
char *data = "\x03"; // End-of-text (etx); Ctrl-C
if (write(serial, data, 1) == -1) {
perror("writing ^C to serial");
return(-1);
}
tcdrain(serial);
expect(serial, "\r\nEEPROM> ");
char * reset_disable = "reset disable\n";
write(serial, reset_disable, strlen(reset_disable));
expect(serial, "RESET is disabled; Hi-Z");
expect(serial, "\r\nEEPROM> ");
char cmd[32];
snprintf(cmd, sizeof(cmd), "write 0x%06X %d\n", addr_from, size);
int cmdlen = strlen(cmd);
//printf("%s", cmd);
int bytes_written = write(serial, cmd, cmdlen);
if (bytes_written != cmdlen) {
fprintf(stderr, "short write %d != %d\n", bytes_written, cmdlen);
}
expect(serial, "\r\nready for data\r\n");
const int chunk_size = 64;
uint8_t buf[chunk_size];
while (!feof(file)) {
int bytes_read = fread(&buf[0], 1, chunk_size, file);
if (bytes_read != chunk_size && !feof(file)) {
fprintf(stderr, "short read %d != %d\n", bytes_read, chunk_size);
}
if (ferror(file) != 0) {
printf("file error: %d\n", ferror(file));
}
int bytes_written = write(serial, buf, bytes_read);
tcdrain(serial);
if (bytes_written != bytes_read) {
fprintf(stderr, "short write %d != %d\n", bytes_written, bytes_read);
}
if (!feof(file)) expect(serial, ".");
}
expect(serial, "bytes written!");
char * reset_now = "reset\n";
write(serial, reset_now, strlen(reset_now));
expect(serial, "\r\nEEPROM> ");
printf("\n");
return 0;
}
int cmd_download(char *dstfile, char * port, char * addr_str, char * size_str) {
FILE *file = fopen(dstfile, "w");
if (file < 0) {
fprintf(stderr, "error opening file %s\n", dstfile);
return(-1);
}
int serial = open_serial(port);
if (serial < 0) {
fprintf(stderr, "error opening serial port %s\n", port);
return(-1);
}
uint32_t addr_from = strtol(addr_str, NULL, 0);
uint32_t size = strtol(size_str, NULL, 0);
uint32_t addr_to = addr_from + size;
printf("Download %s ← %s 0x%06X–0x%06X (%d bytes)\n", dstfile, port, addr_from, addr_to, size);
char *data = "\x03"; // End-of-text (etx); Ctrl-C
if (write(serial, data, 1) == -1) {
perror("writing ^C to serial");
return(-1);
}
tcdrain(serial);
expect(serial, "\r\nEEPROM> ");
//char * reset_hold = "reset hold\n";
//write(serial, reset_hold, strlen(reset_hold));
//expect(serial, "RESET is held LOW");
//expect(serial, "\r\nEEPROM> ");
char cmd[32];
snprintf(cmd, sizeof(cmd), "read 0x%06X %d\n", addr_from, size);
int cmdlen = strlen(cmd);
int bytes_written = write(serial, cmd, cmdlen);
if (bytes_written != cmdlen) {
fprintf(stderr, "short write %d != %d\n", bytes_written, cmdlen);
}
expect(serial, ":\r\n"); // "Reading X bytes from 0xXXXXXXXX:\r\n"
for (int i = 0; i < size; i++) {
uint8_t byte_from_serial;
// TODO: fread() per byte is probably shit, but probably not the bottleneck.
if (read(serial, &byte_from_serial, 1) != 1) {
fprintf(stderr, "read from serial failed\n");
}
fwrite(&byte_from_serial, 1, 1, file);
}
if (fclose(file) != 0) {
perror("fclose");
return -1;
}
expect(serial, "EEPROM> ");
//char * reset_release = "reset release\n";
//write(serial, reset_release, strlen(reset_release));
//expect(serial, "RESET is released to Hi-Z");
printf("\n");
return 0;
}
int cmd_verify(char *srcfile, char *port, char *addr_str) {
FILE *file = fopen(srcfile, "r");
if (file < 0) {
fprintf(stderr, "error opening file %s\n", srcfile);
return(-1);
}
struct stat stat;
if (fstat(fileno(file), &stat) == -1) {
perror("fstat");
return(-1);
}
uint32_t size = stat.st_size;
int serial = open_serial(port);
if (serial < 0) {
fprintf(stderr, "error opening serial port %s\n", port);
return(-1);
}
uint32_t addr_from = strtol(addr_str, NULL, 0);
uint32_t addr_to = addr_from + size;
printf("Verify %s ← %s 0x%06X–0x%06X (%d bytes)\n", srcfile, port, addr_from, addr_to, size);
char *data = "\x03"; // End-of-text (etx); Ctrl-C
if (write(serial, data, 1) == -1) {
perror("writing ^C to serial");
return(-1);
}
tcdrain(serial);
expect(serial, "\r\nEEPROM> ");
char * reset_hold = "reset hold\n";
write(serial, reset_hold, strlen(reset_hold));
expect(serial, "RESET is held LOW");
expect(serial, "\r\nEEPROM> ");
char cmd[32];
snprintf(cmd, sizeof(cmd), "read 0x%06X %d\n", addr_from, size);
int cmdlen = strlen(cmd);
int bytes_written = write(serial, cmd, cmdlen);
if (bytes_written != cmdlen) {
fprintf(stderr, "short write %d != %d\n", bytes_written, cmdlen);
}
expect(serial, ":\r\n"); // "Reading X bytes from 0xXXXXXXXX:\r\n"
uint32_t fail_count = 0;
for (int i = 0; i < size; i++) {
uint8_t byte_from_file, byte_from_serial;
// TODO: fread() per byte is probably shit, but probably not the bottleneck.
if (fread(&byte_from_file, 1, 1, file) < 1) {
fprintf(stderr, "fread from file failed\n");
};
if (read(serial, &byte_from_serial, 1) != 1) {
fprintf(stderr, "read from serial failed\n");
}
if (byte_from_serial != byte_from_file) {
fail_count++;
//printf("%06X:%02X!=%02X ", addr_from+i, byte_from_serial, byte_from_file);
//if (fail_count % 8 == 0) printf("\n");
}
}
//if (fail_count > 0 && fail_count % 8 != 0) printf("\n");
if (fclose(file) != 0) {
perror("fclose");
return -1;
}
expect(serial, "EEPROM> ");
char * reset_release = "reset release\n";
write(serial, reset_release, strlen(reset_release));
expect(serial, "RESET is released to Hi-Z");
printf("\n");
if (fail_count == 0) {
printf("✔ verify successful: %s\n", srcfile);
} else {
fprintf(stderr, "⨯ verify failed; %d of %d bytes differ\n", fail_count, size);
return 1;
}
return 0;
}
int cmd_info() {
fprintf(stderr, "TODO\n");
return -1;
}
/**
* Usage:
* eeprog upload <filename>
* eeprog test
*/
int main(int argc, char *argv[]) {
if (argc == 5 && strcmp(argv[1], "upload") == 0) {
if (cmd_upload(argv[2], argv[3], argv[4]) != 0) {
return EXIT_FAILURE;
}
} else if (argc == 6 && strcmp(argv[1], "download") == 0) {
if (cmd_download(argv[2], argv[3], argv[4], argv[5]) != 0) {
return EXIT_FAILURE;
}
} else if (argc == 5 && strcmp(argv[1], "verify") == 0) {
int ret;
if ((ret = cmd_verify(argv[2], argv[3], argv[4])) != 0) {
return EXIT_FAILURE;
}
} else if (argc == 2 && strcmp("info", argv[1]) == 0) {
if (cmd_info() != 0) {
return EXIT_FAILURE;
}
} else if (argc >= 2 && strcmp("help", argv[1]) == 0) {
usage(argv);
} else {
usage(argv);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}