-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver.c
627 lines (527 loc) · 18.2 KB
/
driver.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#pragma noroot
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gsos.h>
#include "driver.h"
#include "driverwrapper.h"
#include "session.h"
#include "seturl.h"
#include "http.h"
#include "readtcp.h"
#include "tcpconnection.h"
#include "asmglue.h"
#include "mounturl.h"
#include "systemservices.h"
#include "version.h"
#include "endian.h"
#define BLOCK_SIZE 512
/* Disk II-style track/sector layout (relevant to DOS-order images) */
#define TRACK_SIZE (BLOCK_SIZE * 8)
#define SECTOR_SIZE 256
#define DISK_II_DISK_SIZE (TRACK_SIZE * 35L)
/* The sectors making up each block within a track (using DOS 3.3 numbering) */
static const int sectorMap[2][8] = {{ 0, 13, 11, 9, 7, 5, 3, 1},
{14, 12, 10, 8, 6, 4, 2, 15}};
#define APPLE_35_DISK_SIZE (800L * 1024)
struct DIB dibs[NDIBS] = {0};
struct DIBList dibList = {NDIBS};
struct GSOSDP *gsosDP = (void*)0x00BD00; /* GS/OS direct page ptr */
static Word DoMountURL(struct GSOSDP *dp);
static Word DoRead(struct GSOSDP *dp);
static Word DoStatus(struct GSOSDP *dp);
static Word DoEject(struct GSOSDP *dp);
static Word DoSwitchToDOSOrder(struct GSOSDP *dp);
static Word DoShutdown(struct GSOSDP *dp);
void InitDIBs(void) {
for (unsigned i = 0; i < NDIBS; i++) {
dibs[i].linkPtr = (i < NDIBS-1) ? &dibs[i+1] : NULL;
dibs[i].entryPtr = DriverWrapper;
/* speed-independent, block device, read allowed, removable */
dibs[i].characteristics = 0x03A4;
dibs[i].blockCount = 0;
int nameLen = sprintf(dibs[i].devName + 1, "NETDISK%u", i+1);
dibs[i].devName[0] = nameLen;
for (unsigned j = nameLen + 1; j < sizeof(dibs[i].devName); j++) {
dibs[i].devName[j] = ' ';
}
dibs[i].slotNum = 0x8003;
dibs[i].unitNum = i+1;
dibs[i].version = DRIVER_VERSION;
dibs[i].deviceID = DEVICE_MFM_DRIVE;
dibList.dibPointers[i] = &dibs[i];
}
}
#pragma databank 1
Word DriverDispatch(Word callNum, struct GSOSDP *dp) {
Word stateReg = ForceRomIn();
Word retVal = 0;
switch (callNum) {
case Driver_Startup:
gsosDP = dp;
break;
case Driver_Open:
case Driver_Close:
/* Only applicable to character devices, but no error for block devs */
break;
case Driver_Read:
retVal = DoRead(dp);
break;
case Driver_Write:
//TODO Maybe do disk-switched logic and/or block size validation?
retVal = drvrWrtProt;
dp->transferCount = 0;
break;
case Driver_Status:
switch (dp->statusCode) {
case Get_Device_Status:
retVal = DoStatus(dp);
break;
case Get_Config_Parameters:
if (dp->requestCount < 2) {
dp->transferCount = 0;
retVal = drvrBadParm;
break;
}
/* config list has length 0 */
*(Word*)dp->statusListPtr = 0;
dp->transferCount = 2;
break;
case Get_Wait_Status:
if (dp->requestCount != 2) {
dp->transferCount = 0;
retVal = drvrBadParm;
break;
}
/* always in wait mode */
*(Word*)dp->statusListPtr = 0;
dp->transferCount = 2;
break;
case Get_Format_Options:
/* no format options */
dp->transferCount = 0;
break;
case Get_Partition_Map:
/* no partition map */
dp->transferCount = 0;
break;
default:
dp->transferCount = 0;
retVal = drvrBadCode;
break;
}
break;
case Driver_Control:
switch (dp->controlCode) {
case eject:
retVal = DoEject(dp);
break;
case setConfigParameters:
dp->transferCount = 0;
if (dp->requestCount < 2) {
retVal = drvrBadParm;
break;
}
/* config list should be empty (zero length) */
if (*(Word*)dp->controlListPtr != 0) {
retVal = drvrBadParm;
break;
}
break;
case setWaitStatus:
dp->transferCount = 0;
if (dp->requestCount != 2) {
retVal = drvrBadParm;
break;
}
/* only wait mode is valid */
if (*(Word*)dp->controlListPtr != 0) {
retVal = drvrBadParm;
break;
}
break;
case resetDevice:
case formatDevice:
case setFormatOptions:
case assignPartitionOwner:
case armSignal:
case disarmSignal:
case setPartitionMap:
/* do nothing, and return no error */
dp->transferCount = 0;
break;
case Mount_URL:
retVal = DoMountURL(dp);
break;
case Switch_To_DOS_Order:
retVal = DoSwitchToDOSOrder(dp);
break;
default:
dp->transferCount = 0;
retVal = drvrBadCode;
break;
}
break;
case Driver_Flush:
/* Only applicable to character devices; error for block devices */
retVal = drvrBadReq;
break;
case Driver_Shutdown:
retVal = DoShutdown(dp);
break;
default:
retVal = drvrBadReq;
break;
}
RestoreStateReg(stateReg);
return retVal;
}
#pragma databank 0
/*
* Check for a valid DiskCopy 4.2 header, and process it if found.
* Returns a non-zero error code for an invalid or unsupported DC42 file,
* OPERATION_SUCCESSFUL otherwise (whether it's a DC42 file or not).
*/
static enum NetDiskError CheckDiskCopy42(Session *sess) {
struct DiskCopy42Header *hdr = &sess->fileHeader.diskCopy42Header;
if (sess->totalLength == DISK_II_DISK_SIZE)
return OPERATION_SUCCESSFUL;
if (sess->totalLength == APPLE_35_DISK_SIZE)
return OPERATION_SUCCESSFUL;
if (hdr->private != DC42_MAGIC)
return OPERATION_SUCCESSFUL;
if (hdr->dataSize == 0 || ntohl(hdr->dataSize) % BLOCK_SIZE != 0)
return OPERATION_SUCCESSFUL;
if ((unsigned char)hdr->diskName[0] >= DC42_DISK_NAME_LEN)
return OPERATION_SUCCESSFUL;
if (ntohl(hdr->dataSize) + ntohl(hdr->tagSize) + DC42_DATA_OFFSET
!= sess->totalLength)
return OPERATION_SUCCESSFUL;
sess->dataOffset = DC42_DATA_OFFSET;
sess->dataLength = ntohl(hdr->dataSize);
return OPERATION_SUCCESSFUL;
}
/*
* Check for a 2IMG file, and process its header if one is found.
* Returns a non-zero error code for an invalid or unsupported 2IMG file,
* OPERATION_SUCCESSFUL otherwise (whether it's a 2IMG file or not).
*/
static enum NetDiskError CheckTwoImg(Session *sess) {
struct TwoImgHeader *hdr = &sess->fileHeader.twoImgHeader;
/* Check if it's a 2IMG file */
if (hdr->twoImgID != TWO_IMG_MAGIC)
return OPERATION_SUCCESSFUL;
/* It's a 2IMG file, but is it one we can handle? */
if (hdr->version > 1)
return UNSUPPORTED_2IMG_FILE;
if (hdr->dataLength % BLOCK_SIZE != 0)
return UNSUPPORTED_2IMG_FILE;
if (hdr->imgFormat == IMAGE_FORMAT_DOS_33_ORDER) {
if (hdr->dataLength % TRACK_SIZE != 0)
return UNSUPPORTED_2IMG_FILE;
sess->dosOrder = TRUE;
} else if (hdr->imgFormat != IMAGE_FORMAT_PRODOS_ORDER) {
return UNSUPPORTED_2IMG_FILE;
}
sess->dataOffset = hdr->dataOffset;
sess->dataLength = hdr->dataLength;
/* Sweet16 apparently may generate images with erroneous dataLength of 0 */
if (hdr->dataLength == 0) {
sess->dataLength = hdr->nBlocks * BLOCK_SIZE;
}
return OPERATION_SUCCESSFUL;
}
static Word DoMountURL(struct GSOSDP *dp) {
enum NetDiskError err;
struct MountURLRec *mountURLRec = (struct MountURLRec *)dp->controlListPtr;
if (mountURLRec->byteCount != sizeof(struct MountURLRec)
|| mountURLRec->byteCount != dp->requestCount)
{
dp->transferCount = 0;
return drvrBadParm;
}
if (dp->dibPointer->extendedDIBPtr != NULL) {
dp->transferCount = 0;
mountURLRec->result = DISK_ALREADY_MOUNTED;
return drvrBusy;
}
Session *sess = calloc(sizeof(*sess), 1);
if (sess == NULL) {
dp->transferCount = 0;
mountURLRec->result = OUT_OF_MEMORY;
return drvrNoResrc;
}
sess->useCache = (mountURLRec->flags & flgUseCache);
err = SetURL(sess, mountURLRec->url, TRUE, FALSE);
if (err != OPERATION_SUCCESSFUL) {
EndNetDiskSession(sess);
dp->transferCount = 0;
mountURLRec->result = err;
return drvrIOError;
}
err = DoHTTPRequest(sess, 0, sizeof(sess->fileHeader) - 1);
if (err != OPERATION_SUCCESSFUL) {
EndNetDiskSession(sess);
dp->transferCount = 0;
mountURLRec->result = err;
return drvrIOError;
}
ReadStatus readStatus;
InitReadTCP(sess, sizeof(sess->fileHeader.buf), &sess->fileHeader.buf);
while ((readStatus = TryReadTCP(sess)) == rsWaiting)
/* keep reading */ ;
if (readStatus != rsDone) {
EndNetDiskSession(sess);
dp->transferCount = 0;
mountURLRec->result = NETWORK_ERROR;
return drvrIOError;
}
if (mountURLRec->format == formatAutoDetect
|| mountURLRec->format == format2mg)
{
err = CheckTwoImg(sess);
if (err != OPERATION_SUCCESSFUL) {
EndNetDiskSession(sess);
dp->transferCount = 0;
mountURLRec->result = err;
return drvrIOError;
}
if (sess->dataOffset != 0) {
mountURLRec->format = format2mg;
} else if (mountURLRec->format == format2mg) {
dp->transferCount = 0;
mountURLRec->result = NOT_SPECIFIED_IMAGE_TYPE;
return drvrIOError;
}
}
if (mountURLRec->format == formatAutoDetect
|| mountURLRec->format == formatDiskCopy42)
{
err = CheckDiskCopy42(sess);
if (err != OPERATION_SUCCESSFUL) {
EndNetDiskSession(sess);
dp->transferCount = 0;
mountURLRec->result = err;
return drvrIOError;
}
if (sess->dataOffset == DC42_DATA_OFFSET) {
mountURLRec->format = formatDiskCopy42;
} else if (mountURLRec->format == formatDiskCopy42) {
dp->transferCount = 0;
mountURLRec->result = NOT_SPECIFIED_IMAGE_TYPE;
return drvrIOError;
}
}
if (sess->dataOffset == 0) {
/* No encapsulating disk image - treat this as raw blocks */
sess->dataLength = sess->totalLength;
}
if (mountURLRec->format == formatDOSOrder) {
if (sess->dataLength % TRACK_SIZE != 0) {
dp->transferCount = 0;
mountURLRec->result = NOT_SPECIFIED_IMAGE_TYPE;
return drvrIOError;
}
sess->dosOrder = TRUE;
}
if (sess->dataLength % BLOCK_SIZE != 0) {
dp->transferCount = 0;
if (mountURLRec->format == formatAutoDetect) {
mountURLRec->result = NOT_MULTIPLE_OF_BLOCK_SIZE;
} else {
mountURLRec->result = NOT_SPECIFIED_IMAGE_TYPE;
}
return drvrIOError;
}
if (mountURLRec->format == formatAutoDetect) {
if (sess->dataLength != DISK_II_DISK_SIZE) {
mountURLRec->format = formatRaw;
}
}
dp->dibPointer->blockCount = sess->dataLength / BLOCK_SIZE;
dp->dibPointer->extendedDIBPtr = sess;
SetDiskSw();
mountURLRec->result = OPERATION_SUCCESSFUL;
mountURLRec->devNum = dp->dibPointer->DIBDevNum;
return 0;
}
static Word DoRead(struct GSOSDP *dp) {
Session *sess = dp->dibPointer->extendedDIBPtr;
if (sess == NULL) {
dp->transferCount = 0;
return drvrOffLine;
}
//TODO disk-switched logic
unsigned long readStart = dp->blockNum * dp->blockSize + sess->dataOffset;
unsigned long readEnd = readStart + dp->requestCount - 1;
/* Do sanity checks of input values */
if (dp->blockSize == 0) {
dp->transferCount = 0;
return drvrBadBlock;
}
if (dp->requestCount % dp->blockSize != 0) {
dp->transferCount = 0;
return drvrBadCount;
}
if (readEnd < readStart || readStart / dp->blockSize != dp->blockNum)
{
dp->transferCount = 0;
return drvrBadBlock;
}
LongWord firstBlockNum;
LongWord endBlockNum;
unsigned char *bufferPtr;
Boolean useCache = sess->useCache
&& (int)dp->cachePriority > 0
&& (dp->fstNum & 0x8000) == 0
&& dp->blockSize == BLOCK_SIZE;
if (useCache) {
firstBlockNum = dp->blockNum;
endBlockNum = firstBlockNum + dp->requestCount / BLOCK_SIZE;
bufferPtr = dp->bufferPtr;
for (; dp->blockNum < endBlockNum; dp->blockNum++) {
if (!CacheFindBlk()) {
goto skipCache;
}
memmove(bufferPtr, dp->cachePointer, BLOCK_SIZE);
bufferPtr += BLOCK_SIZE;
}
dp->blockNum = firstBlockNum;
dp->transferCount = dp->requestCount;
return 0;
skipCache:
useCache = (dp->blockNum == firstBlockNum);
dp->blockNum = firstBlockNum;
}
enum NetDiskError err;
ReadStatus readStatus;
if (!sess->dosOrder) {
err = DoHTTPRequest(sess, readStart, readEnd);
if (err != OPERATION_SUCCESSFUL) {
dp->transferCount = 0;
if (err == DIFFERENT_LENGTH) {
if ((sess->totalLength - sess->dataOffset) % BLOCK_SIZE != 0) {
return drvrIOError;
} else {
sess->dataLength = sess->totalLength - sess->dataOffset;
dp->dibPointer->blockCount = sess->dataLength / BLOCK_SIZE;
sess->expectedLength = sess->totalLength;
return drvrDiskSwitch;
}
} else {
return drvrIOError;
}
}
InitReadTCP(sess, dp->requestCount, dp->bufferPtr);
while ((readStatus = TryReadTCP(sess)) == rsWaiting)
/* keep reading */ ;
dp->transferCount = dp->requestCount - sess->readCount;
if (readStatus != rsDone) {
return drvrIOError;
}
} else {
if (dp->blockSize != BLOCK_SIZE) {
dp->transferCount = 0;
return drvrBadBlock;
}
firstBlockNum = dp->blockNum;
endBlockNum = firstBlockNum + dp->requestCount / BLOCK_SIZE;
int half = 0;
dp->transferCount = 0;
while (dp->blockNum < endBlockNum) {
readStart = (dp->blockNum >> 3) * TRACK_SIZE
+ sectorMap[half][dp->blockNum & 0x7] * SECTOR_SIZE
+ sess->dataOffset;
readEnd = readStart + (SECTOR_SIZE - 1);
err = DoHTTPRequest(sess, readStart, readEnd);
if (err != OPERATION_SUCCESSFUL) {
dp->blockNum = firstBlockNum;
return drvrIOError;
}
InitReadTCP(sess, SECTOR_SIZE, dp->bufferPtr + dp->transferCount);
while ((readStatus = TryReadTCP(sess)) == rsWaiting)
/* keep reading */ ;
if (readStatus != rsDone || sess->readCount != 0) {
dp->blockNum = firstBlockNum;
return drvrIOError;
}
dp->transferCount += SECTOR_SIZE;
half ^= 1;
if (half == 0)
dp->blockNum++;
}
dp->blockNum = firstBlockNum;
}
if (useCache && sess->readCount == 0) {
bufferPtr = dp->bufferPtr;
dp->blockNum = firstBlockNum;
for (; dp->blockNum < endBlockNum; dp->blockNum++) {
if (!CacheAddBlk()) {
break;
}
memmove(dp->cachePointer, bufferPtr, BLOCK_SIZE);
bufferPtr += BLOCK_SIZE;
}
dp->blockNum = firstBlockNum;
}
return 0;
}
/* This implements the Get_Device_Status subcall of Driver_Status */
static Word DoStatus(struct GSOSDP *dp) {
DeviceStatusRec *dsRec = (DeviceStatusRec*)dp->statusListPtr;
if (dp->requestCount < 2) {
dp->transferCount = 0;
return drvrBadParm;
}
//TODO disk-switched logic
if (dp->dibPointer->extendedDIBPtr != NULL) {
dsRec->statusWord = 0x0014;
} else {
dsRec->statusWord = 0;
}
if (dp->requestCount < 6) {
dp->transferCount = 2;
return 0;
}
dsRec->numBlocks = dp->dibPointer->blockCount;
if (dsRec->numBlocks == 0) {
dsRec->statusWord |= 0x8000;
}
dp->transferCount = 6;
return 0;
}
static Word DoEject(struct GSOSDP *dp) {
EndNetDiskSession(dp->dibPointer->extendedDIBPtr);
dp->dibPointer->extendedDIBPtr = NULL;
dp->dibPointer->blockCount = 0;
dp->transferCount = 0;
return 0;
}
static Word DoSwitchToDOSOrder(struct GSOSDP *dp) {
dp->transferCount = 0;
if (dp->dibPointer == NULL)
return drvrBadParm;
Session *sess = (Session*)dp->dibPointer->extendedDIBPtr;
if (sess == NULL)
return drvrBadParm;
if (sess->dataLength % TRACK_SIZE != 0)
return drvrBadParm;
sess->dosOrder = TRUE;
SetDiskSw();
return 0;
}
static Word DoShutdown(struct GSOSDP *dp) {
/*
* We don't actually end the session here, because this may just be
* a switch to P8. If so, this driver won't work within P8, but if we
* leave things in place it should work again once we're back in GS/OS.
* Do end the current TCP connection, since it would probably time out.
*/
EndTCPConnection(dp->dibPointer->extendedDIBPtr);
/*
* Return error to indicate we shouldn't be purged.
* (I don't think we would be anyhow, since this isn't an
* actual device driver file, but let's do this to be safe.)
*/
return drvrIOError;
}