-
Notifications
You must be signed in to change notification settings - Fork 42
/
drivewire.c
336 lines (308 loc) · 5.83 KB
/
drivewire.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include "drivewire.h"
#define DW_IDLE 0
#define DW_DATA_OUT 1
#define DW_DATA_IN 2
#define DW_ERR_OUT 3
#define DW_CSUM_IN_1 4
#define DW_CSUM_IN_2 5
#define DW_DRIVES 16
static uint8_t dw_mode = DW_IDLE;
static uint8_t dw_cmd;
static uint8_t dw_err;
static uint16_t dw_csum;
static uint16_t dw_rcsum;
static uint8_t dw_buf[262];
static uint8_t *dw_ptr = dw_buf;
static int dw_fd[DW_DRIVES];
static unsigned dw_len;
/*
* Produce a time block
*/
static void dw_setup_time(void)
{
time_t t;
struct tm *tm;
time(&t);
tm = localtime(&t);
if (tm == NULL) {
memset(dw_buf, 0, 7);
return;
}
dw_buf[0] = tm->tm_year;
dw_buf[1] = tm->tm_mon;
dw_buf[2] = tm->tm_mday;
dw_buf[3] = tm->tm_hour;
dw_buf[4] = tm->tm_min;
dw_buf[5] = tm->tm_sec;
dw_buf[6] = tm->tm_wday;
}
/*
* Start a block receive
*/
static void dw_rx_block(unsigned len)
{
dw_len = len;
dw_ptr = dw_buf;
dw_mode = DW_DATA_IN;
}
/*
* Ditto transmit
*/
static void dw_tx_block(unsigned len)
{
dw_len = len;
dw_ptr = dw_buf;
dw_mode = DW_DATA_OUT;
drivewire_byte_pending();
}
/*
* Emulate a microcontroller running drivewire (maybe this wants extracting into its own
* files). Need to implement idle timeouts (250ms)
*/
static void dw_command(uint8_t c)
{
dw_cmd = c;
switch(c) {
case 0xFE: /* Reset */
case 0xFF:
/* Meh */
break;
case 0x00: /* Nop */
break;
case 0x49: /* Initialze */
/* Meh */
break;
case 0x54: /* Terminate (client is rebooting etc */
/* Meh */
break;
case 0xF2: /* Re-read but it's the same functionality so .. */
case 0xD2:
dw_rx_block(4); /* Should now get a 4 byte request block */
break;
case 0x47: /* Getstat, setstat */
dw_rx_block(2);
break;
case 0x53:
case 0x77: /* Write */
case 0x57:
dw_tx_block(262); /* drive, LSN, 256 bytes, checksum */
break;
case 0x23: /* Get date */
dw_len = 7;
/* Fill in buf */
dw_setup_time();
dw_tx_block(7);
break;
case 0x50: /* Print */
dw_rx_block(1);
break;
case 0x46: /* Print flush */
break;
default:
break;
}
}
static uint16_t dw_checksum(uint8_t *p, unsigned len)
{
uint16_t sum = 0;
while(len--)
sum += *p;
return sum;
}
static void dw_csum_1(uint8_t c)
{
dw_rcsum = c;
}
static void dw_csum_2(uint8_t c)
{
dw_rcsum <<= 8;
dw_rcsum |= c;
/* Checksum done, report accordingly then go idle */
if (dw_csum != dw_rcsum)
dw_err = 0xF3;
dw_mode = DW_ERR_OUT;
drivewire_byte_pending();
}
static int dw_prepare(int *fd)
{
uint32_t lsn;
unsigned drive = dw_buf[0];
if (drive >= DW_DRIVES || dw_fd[drive] == -1)
return -1;
lsn = dw_buf[1] << 24;
lsn |= dw_buf[2] << 16;
lsn |= dw_buf[3] << 8;
*fd = dw_fd[drive];
if (lseek(*fd, lsn, SEEK_SET) < 0)
return -1;
return 0;
}
static void dw_read(void)
{
int fd;
/* Bytes in buffer 0: drive, 1-3 LSN. Seek disk and get ready */
if (dw_prepare(&fd))
memset(dw_buf, 0, 256); /* Send zeros on error */
else if (read(fd, dw_buf, 256) != 256) {
memset(dw_buf, 0, 256);
dw_err = 0xF5;
} else
dw_err = 0x00;
/* Now stream the bytes to the client */
dw_len = 256;
dw_ptr = dw_buf;
dw_mode = DW_DATA_OUT;
/* Weirdly the checksum is sent by the client and checked, not sent by server so adds
extra turn arounds and latency */
dw_csum = dw_checksum(dw_buf, 256);
}
/* The block to write has arrived. Process it, set up for an error
return and move to the err return state */
static void dw_write(void)
{
int fd;
dw_csum = dw_checksum(dw_buf + 4, 256);
if ((dw_csum >> 8) != dw_buf[260] ||
(dw_csum & 0xFF) != dw_buf[261]) {
dw_err = 0xF3;
return;
}
if (dw_prepare(&fd))
return;
if (write(fd, dw_buf + 4, 256) != 256) {
dw_err = 0xF5;
return;
}
dw_err = 0;
}
/* We received the block we were supposed to */
static void dw_in_done(void)
{
switch(dw_cmd) {
case 0xF2:
case 0xD2:
/* Read the data info block, set error */
dw_read();
/* Now send the 256 bytes */
dw_len = 256;
dw_mode = DW_DATA_OUT;
drivewire_byte_pending();
break;
case 0x57:
case 0x77:
/* Data arrived. Do disk write */
dw_write();
dw_mode = DW_ERR_OUT;
drivewire_byte_pending();
break;
default:
dw_mode = DW_IDLE;
}
}
/* We finished outputting a block.. now what ? */
static void dw_out_done(void)
{
switch(dw_cmd) {
case 0xD2:
case 0xF2:
/* We sent 256 bytes */
/* Wait for checksum */
dw_mode = DW_CSUM_IN_1;
break;
default:
dw_mode = DW_IDLE;
}
}
static void dw_data(uint8_t c)
{
if (dw_len) {
*dw_ptr++ = c;
dw_len--;
}
if (dw_len == 0)
dw_in_done();
}
/* Last byte acked, client wants the next */
uint8_t drivewire_tx(void)
{
uint8_t r;
switch(dw_mode) {
case DW_DATA_OUT:
r = *dw_ptr;
dw_len--;
if (dw_len == 0)
dw_out_done();
else
drivewire_byte_pending(); /* Byte waiting */
return r;
case DW_ERR_OUT:
r = dw_err;
dw_mode = DW_IDLE;
return r;
default:
return 0xFF;
}
}
void drivewire_rx(uint8_t c)
{
switch(dw_mode) {
case DW_IDLE:
dw_command(c);
break;
case DW_DATA_IN:
dw_data(c);
break;
case DW_CSUM_IN_1:
dw_csum_1(c);
break;
case DW_CSUM_IN_2:
dw_csum_2(c);
break;
}
/* Immediately ack the byte */
drivewire_byte_read();
}
void drivewire_init(void)
{
unsigned i;
dw_mode = DW_IDLE;
for (i = 0; i < DW_DRIVES; i++)
dw_fd[i] = -1;
}
void drivewire_shutdown(void)
{
unsigned i;
for (i = 0; i < DW_DRIVES; i++) {
if (dw_fd[i] != -1) {
close(dw_fd[i]);
dw_fd[i] = -1;
}
}
}
int drivewire_attach(unsigned drive, const char *path, unsigned ro)
{
if (drive >= DW_DRIVES)
return -1;
if (ro)
dw_fd[drive] = open(path, O_RDONLY);
else
dw_fd[drive] = open(path, O_RDWR);
return dw_fd[drive] >= 0 ? 0 : -1;
}
void drivewire_detach(unsigned drive)
{
if (drive >= DW_DRIVES)
return;
if (dw_fd[drive] != -1) {
close(dw_fd[drive]);
dw_fd[drive] = -1;
}
}