-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdevice.c
691 lines (606 loc) · 16.3 KB
/
device.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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/* $Id: device.c 49096 2014-07-01 18:48:17Z twilen $
* $Log: device.c $
* Revision 2.5 1999/09/10 22:14:49 Michiel
* Bugfixes etc (1.4)
*
* Revision 2.4 1999/05/07 16:49:00 Michiel
* bugfixes etc
*
* Revision 2.3 1999/05/04 17:59:09 Michiel
* check mode, logfile, search rootblock implemented
* bugfixes
*
* Revision 2.2 1999/05/04 04:27:13 Michiel
* debugged upto buildrext
*
* Revision 2.1 1999/04/30 12:17:58 Michiel
* Accepts OK disks, bitmapfix and hardlink fix works
*
* Revision 1.2 1999/04/22 15:26:05 Michiel
* compiled
*
* Revision 1.1 1999/04/19 22:16:53 Michiel
* Initial revision
*
* Device Access functies, inclusief cache etc.
*/
#define __USE_SYSBASE
#include <string.h>
#include <math.h>
#include <exec/memory.h>
#include <exec/errors.h>
#include <devices/trackdisk.h>
#include <devices/scsidisk.h>
#include <dos/filehandler.h>
#include <proto/exec.h>
#include <clib/alib_protos.h>
#include <stdio.h>
#include <stdlib.h>
#include "pfs3.h"
#include "doctor.h"
#define BLOCKSHIFT (volume.blockshift)
#define BLOCKNATIVESHIFT (volume.blockshift-volume.blocklogshift)
/**************************************
* Proto
**************************************/
static struct cacheline *c_GetCacheLine(uint32 bloknr);
/**************************************
* Globals
**************************************/
struct cache cache;
/**************************************
* Cache
*
* Cache module. Implemented as proxy on device. This module has a problem
* at the end of the disk: it does not take partial cachelines into
* account. This is no real problem since it is only used for the reserved
* area which is located at the start of the partition.
**************************************/
/* create cache
* cacheline size: 4-8-16
* cachelines: 8-16-32-64-128
* size = linesize * nolines * blocksize
*/
error_t InitCache(uint32 linesize, uint32 nolines, uint32 blocksize)
{
int i;
FreeCache();
if (linesize && nolines) {
cache.linesize = linesize;
cache.nolines = nolines;
}
if (blocksize) {
cache.blocksize = blocksize;
}
/* allocate memory for the cache */
if (!(cache.cachelines = pfscalloc(cache.nolines, sizeof(struct cacheline))))
return e_out_of_memory;
NewList((struct List *)&cache.LRUqueue);
NewList((struct List *)&cache.LRUpool);
for (i=0; i<cache.nolines; i++)
{
cache.cachelines[i].blocknr = CL_UNUSED; /* a bloknr that is never used */
cache.cachelines[i].data = AllocBufMem(cache.linesize * cache.blocksize);
if (cache.cachelines[i].data)
{
MinAddHead(&cache.LRUpool, &cache.cachelines[i]);
}
}
return e_none;
}
/* get block (of size 'bytes') from cache. Cache is per >device<.
*/
error_t c_GetBlock(uint8 *data, uint32 bloknr, uint32 bytes)
{
uint32 lineblnr, offset, total;
struct cacheline *cl = NULL;
error_t error = e_none;
__chkabort();
for (total=0; total<bytes; total+=volume.blocksize, bloknr++, data+=volume.blocksize)
{
lineblnr = (bloknr / cache.linesize) * cache.linesize;
offset = (bloknr % cache.linesize);
cl = c_GetCacheLine(lineblnr);
if (!cl)
return e_read_error;
memcpy(data, cl->data + offset*volume.blocksize, volume.blocksize);
}
return error;
}
/* write block (of size 'bytes') to cache
*/
error_t c_WriteBlock(uint8 *data, uint32 bloknr, uint32 bytes)
{
uint32 lineblnr, offset, total;
struct cacheline *cl = NULL;
error_t error = e_none;
__chkabort();
for (total=0; total<bytes; total+=volume.blocksize, bloknr++, data+=volume.blocksize)
{
lineblnr = (bloknr / cache.linesize) * cache.linesize;
offset = (bloknr % cache.linesize);
cl = c_GetCacheLine(lineblnr);
if (!cl)
return e_read_error;
memcpy(cl->data + offset*volume.blocksize, data, volume.blocksize);
cl->dirty = true;
}
return error;
}
/* (private) locale function to search a cacheline
*/
static struct cacheline *c_GetCacheLine(uint32 bloknr)
{
uint32 i;
struct cacheline *cl = NULL;
error_t error;
for (i=0; i<cache.nolines; i++)
{
if (cache.cachelines[i].blocknr == bloknr)
{
cl = &cache.cachelines[i];
break;
}
}
if (!cl)
{
if (IsMinListEmpty(&cache.LRUpool))
{
cl = (struct cacheline *)cache.LRUqueue.mlh_TailPred;
if (cl->dirty)
error = volume.writerawblocks(cl->data, cache.linesize, cl->blocknr);
}
else
{
cl = HeadOf(&cache.LRUpool);
}
error = volume.getrawblocks(cl->data, cache.linesize, bloknr);
if (error)
return NULL;
cl->blocknr = bloknr;
cl->dirty = false;
}
MinRemove(cl);
MinAddHead(&cache.LRUqueue, cl);
return cl;
}
/* update alle dirty blocks in the cache
*/
void UpdateCache(void)
{
struct cacheline *cl;
error_t error;
for (cl = HeadOf(&cache.LRUqueue); cl->next; cl=cl->next)
{
if (cl->dirty)
{
error = volume.writerawblocks(cl->data, cache.linesize, cl->blocknr);
if (error)
adderror("write error");
}
}
}
/* destroy cache
*/
void FreeCache(void)
{
int i;
if (cache.cachelines)
{
UpdateCache();
for (i = 0; i < cache.nolines; i++)
FreeBufMem(cache.cachelines[i].data);
pfsfree(cache.cachelines);
cache.cachelines = NULL;
}
}
/**************************************
* Device functions
**************************************/
/* OpenDiskDevice
*
* Open the diskdevice passed in 'startup' and generate the
* messageport and request structure, returned in 'port'
* and 'request' resp.
*
* in startup
* out port, request, trackdisk
*/
BOOL OpenDiskDevice(struct FileSysStartupMsg *startup, struct MsgPort **port,
struct IOExtTD **request, BOOL *trackdisk)
{
UBYTE name[FNSIZE];
*port = CreateMsgPort();
if(*port)
{
*request = (struct IOExtTD *)CreateIORequest(*port, sizeof(struct IOExtTD));
if(*request)
{
BCPLtoCString(name, (UBYTE *)BADDR(startup->fssm_Device));
*trackdisk = (strcmp(name, "trackdisk.device") == 0) || (strcmp(name, "diskspare.device") == 0);
if(OpenDevice(name, startup->fssm_Unit, (struct IORequest *)*request,
startup->fssm_Flags) == 0)
return TRUE;
}
}
return FALSE;
}
/*
* Get blocks from device. Blocknrs are disk (not partition!) based
*/
error_t dev_GetBlocks(uint8 *buffer, int32 blocks, uint32 blocknr)
{
struct IOExtTD *request;
uint32 io_length, io_transfer, io_offset, io_actual = 0, io_startblock = 0;
uint8 *io_buffer;
int retry = 4;
retry_read:
if(blocknr == -1) // blocknr of uninitialised anode
return 1;
io_length = blocks << volume.blockshift;
io_offset = blocknr << volume.blockshift;
io_buffer = buffer;
if (volume.td64mode || volume.nsdmode) {
// upper 32 bit of offset
io_actual = blocknr >> (32-volume.blockshift);
io_startblock = blocknr;
}
while (io_length > 0)
{
io_transfer = min(io_length, volume.dosenvec->de_MaxTransfer);
io_transfer &= ~(volume.blocksize-1);
if (!io_transfer)
{
return e_read_error;
}
request = volume.request;
request->iotd_Req.io_Command = CMD_READ;
request->iotd_Req.io_Length = io_transfer;
request->iotd_Req.io_Data = io_buffer; // bufmemtype ??
request->iotd_Req.io_Offset = io_offset;
if (volume.td64mode) {
request->iotd_Req.io_Actual = io_actual;
request->iotd_Req.io_Command = TD_READ64;
} else if (volume.nsdmode) {
request->iotd_Req.io_Actual = io_actual;
request->iotd_Req.io_Command = NSCMD_TD_READ64;
}
if (DoIO((struct IORequest*)request))
{
volume.showmsg("READ ERROR\n");
if (!retry--)
return e_read_error;
goto retry_read;
}
io_buffer += io_transfer;
io_length -= io_transfer;
if (volume.td64mode || volume.nsdmode) {
io_startblock += (io_transfer >> volume.blockshift);
io_offset = io_startblock << volume.blockshift;
io_actual = io_startblock >> (32-volume.blockshift);
} else {
io_offset += io_transfer;
}
}
return e_none;
}
error_t dev_GetBlocksDS(uint8 *buffer, int32 blocks, uint32 blocknr)
{
uint8 cmdbuf[10];
uint32 transfer, maxtransfer;
int retry = 4;
retry_read:
if(blocknr == -1) // blocknr of uninitialised anode
return 1;
blocks <<= volume.blocklogshift;
blocknr <<= volume.blocklogshift;
/* chop in maxtransfer chunks */
maxtransfer = volume.dosenvec->de_MaxTransfer >> volume.blockshift;
while (blocks > 0)
{
transfer = min(blocks,maxtransfer);
transfer &= ~7;
if (!transfer)
{
return e_read_error;
}
*((UWORD *)&cmdbuf[0]) = 0x2800;
*((ULONG *)&cmdbuf[2]) = blocknr;
*((ULONG *)&cmdbuf[6]) = transfer<<8;
if (!DoSCSICommand(buffer,transfer<<BLOCKNATIVESHIFT,cmdbuf,10,SCSIF_READ))
{
volume.showmsg("READ ERROR\n");
if (!retry--)
return e_read_error;
goto retry_read;
}
buffer += transfer<<BLOCKNATIVESHIFT;
blocks -= transfer;
blocknr += transfer;
}
return e_none;
}
error_t dev_WriteBlocksDummy(uint8 *buffer, int32 blocks, uint32 blocknr)
{
return e_none;
}
error_t dev_WriteBlocksDS(uint8 *buffer, int32 blocks, uint32 blocknr)
{
UBYTE cmdbuf[10];
ULONG transfer, maxtransfer;
int retry = 4;
retry_write:
if(blocknr == -1)
return 1;
/* chop in maxtransfer chunks */
maxtransfer = volume.dosenvec->de_MaxTransfer >> volume.blockshift;
while (blocks > 0)
{
transfer = min(blocks,maxtransfer);
*((UWORD *)&cmdbuf[0]) = 0x2a00;
*((ULONG *)&cmdbuf[2]) = blocknr;
*((ULONG *)&cmdbuf[6]) = transfer<<8;
if (!DoSCSICommand(buffer,blocks<<BLOCKSHIFT,cmdbuf,10,SCSIF_WRITE))
{
volume.showmsg("WRITE ERROR\n");
if (!retry--)
return e_write_error;
goto retry_write;
}
buffer += transfer<<BLOCKSHIFT;
blocks -= transfer;
blocknr += transfer;
}
return e_none;
}
error_t dev_WriteBlocks(uint8 *buffer, int32 blocks, uint32 blocknr)
{
struct IOExtTD *request;
uint32 io_length, io_transfer, io_offset, io_actual = 0, io_startblock = 0;
uint8 *io_buffer;
int retry = 4;
retry_write:
if(blocknr == -1) // blocknr of uninitialised anode
return e_write_error;
io_length = blocks << volume.blockshift;
io_offset = blocknr << volume.blockshift;
io_buffer = buffer;
if (volume.td64mode || volume.nsdmode) {
// upper 32 bit of offset
io_actual = blocknr >> (32 - BLOCKSHIFT);
io_startblock = blocknr;
}
while(io_length > 0)
{
io_transfer = min(io_length, volume.dosenvec->de_MaxTransfer);
io_transfer &= ~(volume.blocksize-1);
request = volume.request;
request->iotd_Req.io_Command = CMD_WRITE;
request->iotd_Req.io_Length = io_transfer;
request->iotd_Req.io_Data = io_buffer; // bufmemtype ??
request->iotd_Req.io_Offset = io_offset;
if (volume.td64mode) {
request->iotd_Req.io_Actual = io_actual;
request->iotd_Req.io_Command = TD_WRITE64;
} else if (volume.nsdmode) {
request->iotd_Req.io_Actual = io_actual;
request->iotd_Req.io_Command = NSCMD_TD_WRITE64;
}
if (DoIO((struct IORequest*)request))
{
volume.showmsg("WRITE ERROR\n");
if (!retry--)
return e_write_error;
goto retry_write;
}
io_buffer += io_transfer;
io_length -= io_transfer;
if (volume.td64mode || volume.nsdmode) {
io_startblock += (io_transfer >> volume.blockshift);
io_offset = io_startblock << volume.blockshift;
io_actual = io_startblock >> (32-volume.blockshift);
} else {
io_offset += io_transfer;
}
}
return e_none;
}
/*
* Allocation of memory for buffers
*/
void *AllocBufMem(uint32 size)
{
void *buffer;
buffer = AllocVec(size, volume.dosenvec->de_BufMemType|MEMF_CLEAR);
if (((uint32)buffer) & ~volume.dosenvec->de_Mask)
{
volume.showmsg ("Memory mask fails - aborting\n");
FreeVec(buffer);
return NULL;
}
return buffer;
}
void FreeBufMem (void *mem)
{
FreeVec (mem);
}
/*
* Do direct scsi command
*/
static uint8 sense[20];
static struct SCSICmd scsicmd;
int DoSCSICommand(UBYTE *data, ULONG datalen, UBYTE *command,
UWORD commandlen, UBYTE direction)
{
scsicmd.scsi_Data = (UWORD *)data;
scsicmd.scsi_Length = datalen;
scsicmd.scsi_Command = command;
scsicmd.scsi_CmdLength = commandlen;
scsicmd.scsi_Flags = SCSIF_AUTOSENSE | direction; /* SCSIF_READ or SCSIF_WRITE */
scsicmd.scsi_SenseData = sense;
scsicmd.scsi_SenseLength = 18;
scsicmd.scsi_SenseActual = 0;
volume.request->iotd_Req.io_Length = sizeof(struct SCSICmd);
volume.request->iotd_Req.io_Data = (APTR)&scsicmd;
volume.request->iotd_Req.io_Command = HD_SCSICMD;
DoIO((struct IORequest *)volume.request);
if (scsicmd.scsi_Status)
return 0;
else
return 1;
}
/* Convert BCPL/Pascal string to a CString.
* dest: resulting c string
* src: source pascal string (no BPTR!)
*/
UBYTE *BCPLtoCString(STRPTR dest, UBYTE *src)
{
UBYTE len, *to;
len = *(src++);
len = min (len, PATHSIZE);
to = dest;
while (len--)
*(dest++) = *(src++);
*dest = 0x0;
return to;
}
/* ACCESS MODE AUTODETECT */
#define BLOCKSHIFT (volume.blockshift)
#define BLOCKSIZE (volume.blocksize)
static void fillbuffer(UBYTE *buffer, UBYTE data)
{
memset (buffer, data + 1, BLOCKSIZE);
}
/* Check if at least one byte has changed */
static BOOL testbuffer(UBYTE *buffer, UBYTE data)
{
ULONG cnt;
for (cnt = 0; cnt < BLOCKSIZE; cnt++) {
if (buffer[cnt] != data + 1)
return TRUE;
}
return FALSE;
}
static BOOL testread_ds(UBYTE *buffer)
{
UBYTE cmdbuf[10];
UBYTE cnt;
for (cnt = 0; cnt < 2; cnt++) {
ULONG capacity;
fillbuffer(buffer, 0xfe);
/* Read Capacity */
*((UWORD *)&cmdbuf[0]) = 0x2500;
*((ULONG *)&cmdbuf[2]) = 0;
*((ULONG *)&cmdbuf[6]) = 0;
if (!DoSCSICommand(buffer, 8, cmdbuf, 10, SCSIF_READ)) {
return FALSE;
}
capacity = *((ULONG*)buffer);
if (volume.lastblock > capacity) {
return FALSE;
}
fillbuffer(buffer, cnt);
/* Read(10) */
*((UWORD *)&cmdbuf[0]) = 0x2800;
*((ULONG *)&cmdbuf[2]) = volume.lastblock;
*((ULONG *)&cmdbuf[6]) = 1 << 8;
if (!DoSCSICommand(buffer, 1 << BLOCKSHIFT, cmdbuf, 10, SCSIF_READ)) {
return FALSE;
}
if (testbuffer(buffer, cnt)) {
return TRUE;
}
}
return FALSE;
}
static BOOL testread_td(UBYTE *buffer)
{
struct IOExtTD *io = volume.request;
UBYTE cnt;
if (volume.accessmode == ACCESS_NSD) {
struct NSDeviceQueryResult nsdqr;
UWORD *cmdcheck;
nsdqr.SizeAvailable = 0;
nsdqr.DevQueryFormat = 0;
io->iotd_Req.io_Command = NSCMD_DEVICEQUERY;
io->iotd_Req.io_Length = sizeof(nsdqr);
io->iotd_Req.io_Data = (APTR)&nsdqr;
if (DoIO((struct IORequest*)io) != 0)
return FALSE;
if (!((io->iotd_Req.io_Actual >= 16) && (io->iotd_Req.io_Actual <= sizeof(nsdqr)) && (nsdqr.SizeAvailable == io->iotd_Req.io_Actual)))
return FALSE;
if(nsdqr.DeviceType != NSDEVTYPE_TRACKDISK)
return FALSE;
for(cmdcheck = nsdqr.SupportedCommands; *cmdcheck; cmdcheck++) {
if(*cmdcheck == NSCMD_TD_READ64)
break;
}
if (*cmdcheck == 0)
return FALSE;
}
if (volume.accessmode == ACCESS_TD64) {
BYTE err;
io->iotd_Req.io_Command = TD_READ64;
io->iotd_Req.io_Length = 0;
io->iotd_Req.io_Data = 0;
io->iotd_Req.io_Offset = 0;
io->iotd_Req.io_Actual = 0;
err = DoIO((struct IORequest*)io);
if (err != 0 && err != IOERR_BADLENGTH && err != IOERR_BADADDRESS)
return FALSE;
}
for (cnt = 0; cnt < 2; cnt++) {
fillbuffer(buffer, cnt);
io->iotd_Req.io_Command = CMD_READ;
io->iotd_Req.io_Length = BLOCKSIZE;
io->iotd_Req.io_Data = buffer;
io->iotd_Req.io_Offset = volume.lastblock << BLOCKSHIFT;
if (volume.accessmode >= ACCESS_TD64) {
io->iotd_Req.io_Actual = volume.lastblock >> (32 - BLOCKSHIFT);
io->iotd_Req.io_Command = volume.accessmode == ACCESS_NSD ? NSCMD_TD_READ64 : TD_READ64;
}
if (DoIO((struct IORequest*)io) != 0)
return FALSE;
if (io->iotd_Req.io_Actual != BLOCKSIZE)
return FALSE;
if (testbuffer(buffer, cnt))
return TRUE;
}
return TRUE;
}
BOOL DetectAccessmode(UBYTE *buffer, BOOL scsidirectfirst)
{
BOOL inside4G = volume.lastblocknative < (0x80000000ul >> (BLOCKSHIFT - volume.blocklogshift - 1));
if (scsidirectfirst) {
volume.accessmode = ACCESS_DS;
if (testread_ds(buffer))
return TRUE;
}
if (volume.lastblocknative < 262144) {
/* Don't bother with tests if small enough (<128M) */
volume.accessmode = ACCESS_STD;
return TRUE;
}
if (inside4G) {
/* inside first 4G? Try standard CMD_READ first. */
volume.accessmode = ACCESS_STD;
if (testread_td(buffer))
return TRUE;
volume.accessmode = ACCESS_DS;
/* It failed, we may have 1G limit A590 pre-7.0 ROM or CDTV SCSI, try DS */
if (testread_ds(buffer))
return TRUE;
}
/* outside of first 4G, must use TD64, NSD or DS */
volume.accessmode = ACCESS_NSD;
if (testread_td(buffer))
return TRUE;
volume.accessmode = ACCESS_TD64;
if (testread_td(buffer))
return TRUE;
volume.accessmode = ACCESS_DS;
if (testread_ds(buffer))
return TRUE;
volume.accessmode = ACCESS_STD;
return FALSE;
}