-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.c
259 lines (205 loc) · 6.08 KB
/
console.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "mk20dx128.h"
#include "util.h"
#include "usbcommon.h"
#include "usbserial.h"
#include "sdio.h"
#include "fusion.h"
#include "log.h"
typedef enum {
NONE = 0,
IDENTIFIER,
INTEGER,
END_OF_STATEMENT,
} TokenType;
typedef enum {
PRIMING = 0,
PRIMED,
FIRED,
ERROR,
} ConsoleState;
typedef enum {
BREAK,
CARD,
RESET,
INITIALIZE,
BLOCKS,
RECORD,
LIST,
LOG,
REPLAY,
TEST,
IDENTIFIERS_N,
} Identifier;
static char *identifiers[IDENTIFIERS_N] = {
[BREAK] = "break",
[CARD] = "card",
[RESET] = "reset",
[INITIALIZE] = "initialize",
[BLOCKS] = "blocks",
[RECORD] = "record",
[LIST] = "list",
[LOG] = "log",
[REPLAY] = "replay",
[TEST] = "test",
};
static volatile uint32_t tokens[4];
static volatile uint8_t tokens_n;
static volatile ConsoleState state = PRIMING;
static uint8_t *dump_block(SDTransactionStatus status,
uint8_t *buffer,
void *userdata)
{
int i, j;
uint32_t *n;
assert(status != SDIO_FAILED);
for (i = 0 ; i < 16 ; i += 1) {
for (j = 0 ; j < 32 ; j += 1) {
usbserial_printf("%2x ", buffer[i * 32 + j]);
}
usbserial_printf("\n");
}
usbserial_printf("\n");
n = (uint32_t *)userdata;
if ((*n -= 1) > 0) {
return buffer;
} else {
return NULL;
}
}
static void usb_data_in (uint8_t *data, int length)
{
static TokenType type;
static uint32_t token;
int i;
/* Disregard any further input once a command has been read and
* until it is executed. */
if (state == PRIMED) {
return;
} else if (state == FIRED) {
memset((void *)tokens, 0, sizeof(tokens));
tokens_n = 0; state = PRIMING;
}
/* printf (">> type = %d, j = %d\n", type, j); */
for(i = 0 ; i < length ; i += 1) {
/* Parse the token. */
if (type == NONE) {
/* Determine the token type. */
if (isblank((int)data[i])) {
continue;
}
if (isalpha((int)data[i]) || data[i] == '_') {
type = IDENTIFIER;
} else if (isdigit((int)data[i])) {
type = INTEGER;
} else if (data[i] == '\r' || data[i] == '\n' || data[i] == ',') {
type = END_OF_STATEMENT;
} else {
usbserial_printf("Can't tokenize '%c'.\n", data[i]);
break;
}
}
switch (type) {
case END_OF_STATEMENT:
type = NONE; state = (state == ERROR) ? FIRED : PRIMED;
break;
case IDENTIFIER: {
static uint8_t j;
if (!isalnum((int)data[i]) && data[i] != '_') {
/* Match bits are stored inverted (set bit signifies
* mismatch (for a reason). */
token = ~token & ((1 << IDENTIFIERS_N) - 1);
/* Consume the token. */
if (token == 0) {
usbserial_printf("Unknown identifier.\n");
tokens[tokens_n] = 0; state = ERROR;
} else if ((token & (token - 1)) != 0) {
int k;
/* More than one bit set. Command is
* ambiguous. */
usbserial_printf("Command ambiguous. "
"Possible candidates:\n");
for (k = 0 ; k < IDENTIFIERS_N ; k += 1) {
if (token & (1 << k)) {
usbserial_printf("%s ", identifiers[k]);
}
}
usbserial_printf("\n");
tokens[tokens_n] = 0; state = ERROR;
} else {
tokens[tokens_n] = ffs(token) - 1;
}
/* Reset. */
tokens_n += 1; type = NONE; token = j = 0; i -= 1;
} else {
int k;
for (k = 0 ; k < IDENTIFIERS_N ; k += 1) {
if (token & (1 << k)) {
continue;
}
if (j > strlen(identifiers[k]) ||
data[i] != identifiers[k][j]) {
token |= (1 << k);
}
}
j += 1;
}
break;
}
case INTEGER:
if (!isdigit((int)data[i])) {
/* Consume the token and reset. */
tokens[tokens_n] = token;
tokens_n += 1; type = NONE; token = 0; i -= 1;
} else {
token = 10 * token + (data[i] - '0');
}
break;
default:
assert(0);
}
}
}
void console_initialize()
{
usb_set_data_in_callback(usb_data_in);
usbserial_set_state(SERIAL_STATE_DSR);
}
void console_enter()
{
while (1) {
sleep_while(state != PRIMED || tokens_n == 0);
switch(tokens[0]) {
case BREAK: request_reboot(); break;
case RESET: request_reset(); break;
case INITIALIZE: log_initialize(); break;
case CARD: usbserial_printf("%x\n", sdio_get_status()); break;
case BLOCKS: {
/* switch(tokens_n) { */
/* case 1: break; */
/* case 2: dump_blocks(tokens[1], tokens[1]); break; */
/* case 3: dump_blocks(tokens[1], tokens[2]); break; */
/* } */
uint8_t buffer[512];
uint32_t n = 3;
sdio_read_multiple_blocks(0, buffer, dump_block, &n);
sleep_while(sdio_is_busy());
}
break;
case RECORD: log_record(tokens[1]); break;
case LIST: log_list(); break;
case LOG: /* usbserial_printf("log %d\n", tokens[1]); */ break;
case REPLAY:
log_replay(tokens[1], tokens[2]);
break;
case TEST:
break;
default: assert(0);
}
state = FIRED;
}
}