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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
|
#include <zup.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#ifdef USE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif // USE_READLINE
#include <sys/stat.h>
#include <unistd.h>
struct command;
enum args_type {
A_END,
A_BYTE,
A_DBYTE,
A_QBYTE,
A_OBYTE,
A_STRING,
A_OPTIONAL = 16
};
typedef int (*fptr)(int, const struct command *, int, char **);
struct command {
char alias;
const char *name;
int *args_spec;
const char *usage;
fptr fptr;
int quit;
};
#define ARGS(...) (int []){__VA_ARGS__}
// FIXME: Assert ranges for integers
static int
parse_args(const struct command *cmd, int argc, char **args, ...)
{
int i = 0;
int opt = 0;
int err = 0;
va_list ap;
va_start(ap, args);
void *ptr = NULL;
int *p = cmd->args_spec;
for ( ; *p != A_END; ++p) {
// Avoid runaway p
assert(i < MAX_ARGS);
if (*p == A_OPTIONAL) {
opt = 1;
continue;
}
if (i == argc) {
if (!opt)
goto syntax_error;
break;
}
ptr = va_arg(ap, void *);
errno = 0;
char *end = NULL;
switch (*p) {
case A_BYTE:
*((uint8_t *)ptr) = strtoul(args[i], &end, 16);
break;
case A_DBYTE:
*((uint16_t *)ptr) = strtoul(args[i], &end, 16);
break;
case A_STRING:
*((const char **)ptr) = args[i];
break;
}
if (errno || args[i] == end) {
fprintf(stderr, "Invalid argument: %s\n", args[i]);
goto syntax_error;
}
++i;
}
va_end(ap);
return i;
syntax_error:
va_end(ap);
fputs(cmd->usage, stderr);
return -1;
}
static int
cmd_boot(int fd, const struct command *cmd, int argc, char **args)
{
int err;
uint8_t bank;
uint16_t address;
if ((err = parse_args(cmd, argc, args, &bank, &address) < 0))
return err;
return z_boot(fd, bank, address);
}
static int
cmd_read(int fd, const struct command *cmd, int argc, char **args)
{
int err = 0;
uint8_t buf[MAX_PACKET_SIZE];
uint8_t bank;
uint16_t address;
uint16_t length;
const char *pathname = NULL;
err = parse_args(cmd, argc, args, &bank, &address, &length, &pathname);
if (err < 0)
return -1;
if ((err = z_read(fd, bank, address, length, buf))) {
print_error(err);
return err;
}
if (param.human_readable) {
hexdump(address, length, buf);
} else if (pathname) {
FILE *fp;
if (!(fp = fopen(pathname, "w"))) {
perror("File could not be opened");
return -1;
}
fwrite(buf, sizeof(uint8_t), length, fp);
fclose(fp);
} else {
fwrite(buf, sizeof(uint8_t), length, stdout);
fflush(stdout);
}
return 0;
}
static int
write_file(int fd, const char *pathname, uint8_t bank, uint16_t addr)
{
int i, err;
uint16_t len = 0;
uint8_t buf[MAX_PACKET_SIZE];
FILE *fp;
off_t n = 0;
struct stat st;
if (!(fp = fopen(pathname, "r")) || (fstat(fileno(fp), &st) < 0))
goto file_error;
printf("Uploading %s :\n[ 0%%]", pathname);
fflush(stdout);
/*
* FIXME: fread MAX_PACKET_SIZE insted of 8
* FIXME: Add attempts to all variants
*/
// Split file into packets
while ((len = fread(buf, sizeof(uint8_t), 8, fp))) {
for (i = 0; i < 50 && (err = z_write(fd, bank, addr + n, len, buf)); i++)
usleep(1000000);
if (err)
break;
n += len;
if (param.show_progress) {
printf("\r[% 3d%%]", 100 * n / st.st_size);
fflush(stdout);
}
}
putchar('\n');
if (!err)
puts("Done!");
fclose(fp);
return err;
file_error:
perror("File could not be opened");
return -1;
}
static int
write_repl(int fd, uint8_t bank, uint16_t addr)
{
int err;
char line[512];
uint8_t buf[MAX_PACKET_SIZE];
uint16_t len = 0;
char *p;
do {
// TODO: Handle fgets error
fgets(line, LEN(line), stdin);
if (line && !strcmp("\n", line))
break;
p = strtok(line, " \n");
while (p && len < 10) {
errno = 0;
buf[len++] = strtoul(p, NULL, 16);
p = strtok(NULL, " \n");
}
if (p) {
fputs("WRITE: Input can't be longer than 256 bytes\n", stderr);
return -1;
}
} while (1);
if ((err = z_write(fd, bank, addr, len, buf))) {
print_error(err);
return err;
}
}
static int
write_stdin(int fd, uint8_t bank, uint16_t addr)
{
int err;
uint16_t len = 0;
uint8_t buf[MAX_PACKET_SIZE];
// FIXME: fread MAX_PACKET_SIZE insted of 8
// Split file into packets
while ((len = fread(buf, sizeof(uint8_t), 8, stdin))) {
if ((err = z_write(fd, bank, addr, len, buf)))
break;
addr += len;
}
}
static int
cmd_write(int fd, const struct command *cmd, int argc, char **args)
{
int err;
uint8_t bank;
uint16_t addr;
const char *pathname = NULL;
err = parse_args(cmd, argc, args, &bank, &addr, &pathname);
if (err < 0)
return -1;
if (pathname)
return write_file(fd, pathname, bank, addr);
else if (param.human_readable)
return write_repl(fd, bank, addr);
else
return write_stdin(fd, bank, addr);
return err;
}
static int
cmd_echo(int fd, const struct command *cmd, int argc, char **args)
{
int err;
char line[512];
char buf[MAX_PACKET_SIZE];
if (!param.human_readable) {
fputs("Echo is a interactive command only", stderr);
return -1;
}
parse_args(cmd, argc, args);
memset(line, 0, sizeof(line));
memset(buf, 0, sizeof(buf));
size_t length = 0;
do {
// TODO: Handle fgets error
fgets(line, LEN(line), stdin);
if (line && !strcmp("\n", line))
break;
size_t len = strlen(line);
if (line[len - 1] == '\n')
line[--len] = '\0';
if (length + len >= MAX_PACKET_SIZE) {
fputs("ECHO: Input can't be longer than 256 bytes\n", stderr);
return -1;
}
strcat(buf, line);
length += len;
} while (1);
if ((err = z_echo(fd, length, (uint8_t *)buf))) {
print_error(err);
return err;
}
fwrite(buf, sizeof(uint8_t), length, stdout);
putchar('\n');
fflush(stdout);
return 0;
}
static int
repl_io_write(int fd, const struct command *cmd, int argc, char **args)
{
return 0;
}
static int
repl_io_read(int fd, const struct command *cmd, int argc, char **args)
{
return 0;
}
/*
static volatile sig_atomic_t quit = 0;
void
interrupt_handler(int signal)
{
puts("Bye!");
quit = 1;
// fclose(stdin);
}
*/
const struct command cmd_table[] = {
{
.alias = 'b',
.name = "boot",
.fptr = cmd_boot,
.quit = 0,
.args_spec = ARGS(A_BYTE, A_DBYTE, A_END),
.usage = "Usage: boot <bank> <address>\n"
},
{
.alias = 'r',
.name = "read",
.fptr = cmd_read,
.quit = 0,
.args_spec = ARGS(A_BYTE, A_DBYTE, A_DBYTE, A_OPTIONAL, A_STRING, A_END),
.usage = "Usage: read <bank> <address> <length> [pathname]\n"
},
{
.alias = 'w',
.name = "write",
.fptr = cmd_write,
.quit = 0,
.args_spec = ARGS(A_BYTE, A_DBYTE, A_OPTIONAL, A_STRING, A_END),
.usage = "Usage: write <bank> <address> [pathname]\n"
},
{
.alias = 'i',
.name = "io_read",
.fptr = NULL,
.quit = 0,
.args_spec = ARGS(A_DBYTE, A_DBYTE, A_END),
.usage = "Usage: io_read <address> <length>\n"
},
{
.alias = 'o',
.name = "io_write",
.fptr = NULL,
.quit = 0,
.args_spec = ARGS(A_DBYTE, A_END),
.usage = "Usage: io_write <address>\n"
},
{
.alias = 'e',
.name = "echo",
.fptr = cmd_echo,
.quit = 0,
.args_spec = ARGS(A_END),
.usage = ""
},
{
.alias = 'q',
.name = "quit",
.fptr = NULL,
.quit = 1,
.args_spec = ARGS(A_END),
.usage = ""
}
};
int
run_line(int fd, char *line, const char *sep)
{
int err;
int argc = 1;
char *args[MAX_ARGS + 1] = { NULL };
fptr p;
// Tokenize command
args[0] = strtok(line, sep);
if (!args[0])
return 0;
while(argc < LEN(args) && (args[argc] = strtok(NULL, sep)))
++argc;
// Match action
int cmdi;
for (cmdi = 0; cmdi < LEN(cmd_table); ++cmdi) {
if ((!args[0][1] && args[0][0] == cmd_table[cmdi].alias)
|| !strcmp(args[0], cmd_table[cmdi].name))
break;
}
// Call action
if (cmdi == LEN(cmd_table)) {
fprintf(stdout, "Unknown command %s\n", args[0]);
return -1;
}
if ((p = cmd_table[cmdi].fptr)
&& (err = p(fd, &cmd_table[cmdi], argc - 1, &args[1]))) {
print_error(err);
return err;
}
return cmd_table[cmdi].quit;
}
// TODO: Handle signals (CTRL-C)
void
repl(int fd)
{
int quit = 0;
#ifdef USE_READLINE
using_history();
#endif
while (!quit) {
#ifdef USE_READLINE
char *line = readline("* ");
if (!line)
break;
add_history(line);
quit = run_line(fd, line, " ");
free(line);
#else
size_t len = 0;
char line[512];
fputs("* ", stdout);
if (!fgets(line, sizeof(line), stdin))
break;
len = strlen(line);
if (line[len - 1] == '\n')
line[len - 1] = '\0';
quit = run_line(fd, line, " ");
#endif
}
}
|