-
Notifications
You must be signed in to change notification settings - Fork 0
/
lfn.cpp
3561 lines (3341 loc) · 85.3 KB
/
lfn.cpp
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* lfn.cpp : Longfilenames for DOS
* compile in LARGE modell (FAR pointers)
*
* Copyright (C) 1999 Ortwin Glueck
*
* GNU General Public License applies
*
*/
//Define language
#define LANG_EN
//#define LANG_DE
//vmemory.h present ?
#define VMEMORY
// includes
#include <stdio.h>
#include <dos.h>
#include "unicode2.inc"
#include <dos.h>
#include <direct.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#ifdef VMEMORY
#include <vmemory.h>
#endif
#include "lfn.h"
#ifdef LANG_DE
#include "lfnmsgde.inc"
#endif
#ifdef LANG_EN
#include "lfnmsgen.inc"
#endif
#define LOG
//Functions
void initLFNLib(void) {
initUnicodeLib();
initCache();
}
#pragma optimize("gel",off) //cannot optimize asm
void getDOSver(struct dosver *v)
{
byte a,b,c;
__asm{
mov ax,0x3000
int 0x21
mov [a],al
mov [b],ah
mov [c],bh
}
v->main=a;
v->sub=b;
v->oem=c;
}
int directAccessDenied(char drive) {
//tell if drive is remote and if we can make direct i/o
//0: allowed
//1: notallowed
int att,e;
e=1;
__asm{
mov ax,0x4409
mov bl, [drive]
inc bl
int 0x21
jc err
mov [att],dx
mov [e],0
err:
nop
}
if (e==1) {
return 1; //error
}
else {
return (att & 0x8100); //bit 9 prohibits direct i/o, bit 15 is a subst
}
}
int readSectorP(char drive,dword sector,word count, void *buffer) //extended version
//Read a physical sector (uncached)
{
struct secblk x;
x.sector=sector;
x.no_sectors=count;
x.pBuffer=buffer;
int cy;
if (win98==1) //use fat32 compatible read
{
__asm{
mov si,0 //read
mov cx, 0xffff
lea bx, [x]
mov dl, [drive]
inc dl
mov ax, 0x7305
int 0x21
mov [cy],ax
jc err
mov [cy],0
err:
nop
}
if (cy!=0)
{
if (cy==1) win98=0; //function not supported
else
{
printf(msg[0],drive,sector,sector+count-1);
return 1;
}
}
else return 0;
}
//use normal read, also if first fails. (NO ELSE)
__asm{
mov al,[drive]
mov cx,0xffff
lea bx,[x]
int 0x0025
add sp,2
jc err2
mov [cy],0
jmp quit
err2:
mov [cy],1
quit:
nop
}
if (cy==1)
{
printf(msg[1],drive,sector,sector+count-1);
return 1;
}
else return 0;
}
int writeSectorP(char drive,dword sector,word count, void *buffer,word typ) //extended version
//write a physical sector (uncached)
//type: 0x2000 =FAT, 0x4000 =DIR, 0x6000 =FILE
{
struct secblk x;
x.sector=sector;
x.no_sectors=count;
x.pBuffer=buffer;
int cy;
if (win98==1) //use fat32 compatible write
{
__asm{
mov cx, [typ]
or cx,0x0001 //write
mov si,cx
mov cx, 0xffff
lea bx, [x]
mov dl, [drive]
inc dl
mov ax, 0x7305
int 0x21
mov [cy],ax
jc err
mov [cy],0
err:
nop
}
if (cy!=0)
{
if (cy==1) win98=0; //function not supported
else
{
printf(msg[2],drive,sector,sector+count-1);
return 1;
}
}
else return 0;
}
__asm{
mov al,[drive]
mov cx,0xffff
lea bx,[x]
int 0x0026
add sp,2
jc err2
mov [cy],0
jmp quit
err2:
mov [cy],1
quit:
nop
}
if (cy)
{
printf(msg[3],drive,sector,sector+count-1);
return 1;
}
return 0;
}
//Cache
int no_swaps=0;
#ifdef VMEMORY
cachepage __far* getCacheArea(int i) {
if ((current_area_no!=i) && (current_area_no!=-1)) {
_vunlock(cacheArea[current_area_no], current_area_dirty);
}
if ((current_area_no==i) && (current_area_no!=-1)) {
return current_area;
}
current_area_no=i;
current_area_dirty=_VM_CLEAN;
current_area=(cachepage __far*) _vlock(cacheArea[i]);
no_swaps++;
if (current_area==NULL) {
printf(msg[53]);
_vheapterm();
exit(1); //panic
}
return current_area;
}
#endif
//Cache must be initialized BEFORE any call to makeDPB for a CD-ROM!
void initCache(void)
{
int i,j;
if (bypassCache==1) return;
if (cacheWasSetup==1) return;
//virtual memory
#ifdef VMEMORY
if (useVMCache!=0) {
useVMCache=_vheapinit(2000,4096, _VM_EMS | _VM_XMS); //use max 64K DOS mem
if (useVMCache!=0) {
no_cache_areas=0;
for (i=0;i<MAX_CACHE_AREAS;i++) {
cacheArea[i]=_vmalloc(MAX_CACHE_AREA_SIZE); //allocate space
unsigned long size=_vmsize(cacheArea[i]);
if (cacheArea[i]==_VM_NULL) {
if (i==0) {
printf(msg[52]);
_vheapterm();
useVMCache=0;
goto noVM_label;
}
break;
}
no_cache_areas++;
}//for
//create quick lookup table
if ((long)no_pages_per_area*no_cache_areas>32766) {
printf(msg[54]);
_vheapterm();
exit(1);
}
int no_pages=no_pages_per_area*no_cache_areas;
vcachelookup = (vcacheinfo*) malloc(sizeof(vcacheinfo)*no_pages);
//printf("%lu mem",(unsigned long)sizeof(vcacheinfo)*no_pages);
if (vcachelookup==NULL) {
printf(msg[4]);
_vheapterm();
exit(1); //panic
}
for (i=0;i<no_pages;i++) {
vcachelookup[i].empty=1;
}
current_area_no=-1;
current_area=NULL;
current_area_dirty=_VM_CLEAN;
for (i=0;i<no_cache_areas;i++) {
unsigned long size=_vmsize(cacheArea[i]);
cachepage __far *p = getCacheArea(i);
for (j=0;j<no_pages_per_area;j++) {
p[j].dirty=EMPTY;
}
current_area_dirty=_VM_DIRTY;
}
no_free_pages=no_pages_per_area*no_cache_areas;
total_cache_pages = no_free_pages;
}
}
noVM_label:
#else
useVMCache = 0;
#endif
// no virtual memory
if (useVMCache==0) {
for (i=0;i<MAX_CACHE_PAGES;i++) {
cache[i]=(struct cachepage*)malloc(sizeof(cachepage));
if (cache[i]==NULL) {
printf(msg[4]);
exit(1);
}
cache[i]->dirty=EMPTY;
}
no_free_pages=MAX_CACHE_PAGES;
total_cache_pages=no_free_pages;
}
for (i=0;i<MAX_DRIVES;i++) bpsd[i]=MAX_SECTOR_SIZE;
timestamp=0;
cacheWasSetup=1;
}
int isInCache(char drive, dword sector,word *page)
//1 if sector is in cache
{
int i;
if (useVMCache!=0) {
#ifdef VMEMORY
int no_pages=no_pages_per_area*no_cache_areas;
for (i=0;i<no_pages;i++) {
if ((vcachelookup[i].empty==0) && (vcachelookup[i].drive==drive) && (vcachelookup[i].sector==sector)) {
*page=i;
return 1;
}
}
#endif
}
else {
//no VMcache
for (i=0;i<MAX_CACHE_PAGES;i++)
{
if ((cache[i]->dirty!=EMPTY) && (cache[i]->drive==drive) && (cache[i]->sector==sector))
{
*page=i;
return 1;
}
}
}//useVMcache
return 0;
}
word getEmptyPage(void)
{
int i,j;
if (no_free_pages==0) return 0xFFFF;
if (useVMCache!=0) {
#ifdef VMEMORY
//seek in current area
cachepage __far *p = current_area;
if (p!=NULL) {
for (j=0;j<no_pages_per_area;j++) {
if (p[j].dirty==EMPTY) return no_pages_per_area*current_area_no+j;
}
}
//seek all areas
int no_pages=no_pages_per_area*no_cache_areas;
for (i=0;i<no_pages;i++) {
if (vcachelookup[i].empty==1) {
return i;
}
}
#endif
}
else {
//no VMcache
for (i=0;i<MAX_CACHE_PAGES;i++)
if (cache[i]->dirty==EMPTY) return i;
}
return 0xFFFF;
}
void discardPage(word page)
{
int i,j;
if (useVMCache!=0) {
#ifdef VMEMORY
i=page/no_pages_per_area;
j=page % no_pages_per_area;
cachepage __far *p=getCacheArea(i);
if (p[j].dirty==EMPTY) return;
if (p[j].dirty==DIRTY) {
writeSectorP(p[j].drive, p[j].sector,1,p[j].data, p[j].typ);
}
p[j].dirty=EMPTY;
current_area_dirty=_VM_DIRTY;
vcachelookup[page].empty=1;
#endif
}
else {
//no VMcache
if (cache[page]->dirty==EMPTY) return;
if (cache[page]->dirty==DIRTY)
writeSectorP(cache[page]->drive, cache[page]->sector,1,cache[page]->data, cache[page]->typ);
cache[page]->dirty=EMPTY;
}
no_free_pages++;
}
word discardOldestPage(void)
{
word page=0xFFFF;
dword time=0xFFFFFFFF;
int i;
if (useVMCache!=0) {
#ifdef VMEMORY
int no_pages=no_pages_per_area*no_cache_areas;
for (i=0;i<no_pages;i++) {
if ((vcachelookup[i].empty==0) && (vcachelookup[i].timestamp<time)) {
time=vcachelookup[i].timestamp;
page=i;
}
}
#endif
}
else {
//no VMcache
for (i=0;i<MAX_CACHE_PAGES;i++)
{
if ((cache[i]->dirty!=EMPTY) && (cache[i]->timestamp<time))
{
time=cache[i]->timestamp;
page=i;
}
}
}
if (page!=0xFFFF) discardPage(page);
return page;
}
//discards the oldest 20% of the pages. Call if cache is full and you need space.
word discardOldPages(void)
{
int toclear = total_cache_pages / 5;
word page = 0xFFFF;
for (int i=0; i<toclear; i++) {
page = discardOldestPage();
}
return page;
}
void flushCache(void)
{
int i;
if (cacheWasSetup==0) return;
if (useVMCache!=0) {
#ifdef VMEMORY
for (i=0;i<no_cache_areas*no_pages_per_area;i++) discardPage(i);
#endif
}
else {
//no VMcache
for (i=0;i<MAX_CACHE_PAGES;i++) discardPage(i);
}
}
void killCache(void)
{
int i;
if (cacheWasSetup==0) return;
flushCache();
if (useVMCache!=0) {
#ifdef VMEMORY
if (current_area_no!=-1) {
_vunlock(cacheArea[current_area_no], current_area_dirty);
current_area_no=-1;
current_area=NULL;
current_area_dirty=_VM_CLEAN;
}
for (i=0;i<no_cache_areas;i++) _vfree(cacheArea[i]);
_vheapterm();
no_cache_areas=0;
#endif
}
else {
//no VMcache
for (i=0;i<MAX_CACHE_PAGES;i++)
if (cache[i]!=NULL) free(cache[i]);
}
cacheWasSetup=0;
}
int readSector(char drive,dword sector,word count, void *buffer)
//read a cached sector
{
word h,i,j;
word page;
if ((bypassCache==1) || (cacheWasSetup==0))
{
return readSectorP(drive,sector,count,buffer);
}
for (i=0;i<count;i++,sector++)
{
if (isInCache(drive, sector, &page))
{
if (useVMCache!=0) {
#ifdef VMEMORY
h=page/no_pages_per_area;
j=page % no_pages_per_area;
cachepage __far *p=getCacheArea(h);
memcpy((void*)&(((char*)buffer)[i*bpsd[drive]]),p[j].data,bpsd[drive]);
timestamp++;
p[j].timestamp=timestamp;
current_area_dirty=_VM_DIRTY;
vcachelookup[page].timestamp=timestamp;
#endif
}
else {
//no VMcache
memcpy((void*)&(((char*)buffer)[i*bpsd[drive]]),cache[page]->data,bpsd[drive]);
timestamp++;
cache[page]->timestamp=timestamp;
}
}
else
{
page=getEmptyPage();
if (page==0xFFFF) page=discardOldPages();
no_free_pages--;
if (useVMCache!=0) {
#ifdef VMEMORY
h=page/no_pages_per_area;
j=page % no_pages_per_area;
cachepage __far *p=getCacheArea(h);
if (readSectorP(drive,sector,1,&p[j].data)!=0) {
flushCache();
return 1;
}
p[j].drive=drive;
p[j].sector=sector;
p[j].dirty=CLEAN;
timestamp++;
p[j].timestamp=timestamp;
current_area_dirty=_VM_DIRTY;
vcachelookup[page].empty=0;
vcachelookup[page].drive=drive;
vcachelookup[page].sector=sector;
vcachelookup[page].timestamp=timestamp;
memcpy((void*)&(((char*)buffer)[i*bpsd[drive]]),p[j].data,bpsd[drive]);
#endif
}
else {
//no VMcache
if (readSectorP(drive,sector,1,&cache[page]->data)!=0)
{
flushCache();
return 1;
}
cache[page]->drive=drive;
cache[page]->sector=sector;
cache[page]->dirty=CLEAN;
timestamp++;
cache[page]->timestamp=timestamp;
memcpy((void*)&(((char*)buffer)[i*bpsd[drive]]),cache[page]->data,bpsd[drive]);
}
}
}
return 0;
}
int writeSector(char drive,dword sector,word count, void *buffer,word typ)
//write a cached sector
{
word h,i,j;
word page;
if ((bypassCache==1) || (cacheWasSetup==0))
{
return writeSectorP(drive,sector,count,buffer,typ);
}
for (i=0;i<count;i++,sector++)
{
if (isInCache(drive, sector, &page))
{
if (useVMCache!=0) {
#ifdef VMEMORY
h=page/no_pages_per_area;
j=page % no_pages_per_area;
cachepage __far *p=getCacheArea(h);
memcpy(p[j].data,(void*)&(((char*)buffer)[i*bpsd[drive]]),bpsd[drive]);
timestamp++;
p[j].timestamp=timestamp;
p[j].dirty=DIRTY;
p[j].typ=typ; //we may have a page in cache whose type we do not know
current_area_dirty=_VM_DIRTY;
vcachelookup[page].timestamp=timestamp;
#endif
}
else {
//no VMcache
memcpy(cache[page]->data,(void*)&(((char*)buffer)[i*bpsd[drive]]),bpsd[drive]);
timestamp++;
cache[page]->timestamp=timestamp;
cache[page]->dirty=DIRTY;
cache[page]->typ=typ; //we may have a page in cache whose type we do not know
}
}
else
{
page=getEmptyPage();
if (page==0xFFFF) page=discardOldPages();
no_free_pages--;
if (useVMCache!=0) {
#ifdef VMEMORY
h=page/no_pages_per_area;
j=page % no_pages_per_area;
cachepage __far *p=getCacheArea(h);
memcpy(p[j].data,(void*)&(((char*)buffer)[i*bpsd[drive]]),bpsd[drive]);
p[j].drive=drive;
p[j].sector=sector;
p[j].dirty=DIRTY;
timestamp++;
p[j].timestamp=timestamp;
p[j].typ=typ;
current_area_dirty=_VM_DIRTY;
vcachelookup[page].empty=0;
vcachelookup[page].drive=drive;
vcachelookup[page].sector=sector;
vcachelookup[page].timestamp=timestamp;
#endif
}
else {
//no VMcache
memcpy(cache[page]->data,(void*)&(((char*)buffer)[i*bpsd[drive]]),bpsd[drive]);
cache[page]->drive=drive;
cache[page]->sector=sector;
cache[page]->dirty=DIRTY;
timestamp++;
cache[page]->timestamp=timestamp;
cache[page]->typ=typ;
}
}
}
return 0;
}
int lockDrive(byte drv)
{
int cy;
if (noLocking==1) return 0; //not supported
__asm{
mov ax, 0x440D
mov bh, 0
mov bl, [drv]
inc bl
mov cx, 0x084A
mov dx, 0x0000
int 0x21
jc error
mov [cy],0
jmp quit
error:
mov [cy],1
quit:
nop
}
if (cy==1)
{
printf(msg[5],drv+'A');
exit(1);
}
return 0;
}
int unlockDrive(byte drv)
{
int cy;
flushCache();
if (noLocking==1) return 0; //not supported
__asm{
mov ax, 0x440D
mov bl, [drv]
inc bl
mov cx, 0x086A ; device category (must be 08h)
int 0x21
jc error
mov [cy],0
jmp quit
error:
mov [cy],1
quit:
nop
}
if (cy==1)
{
printf(msg[6],drv);
return 1;
}
return 0;
}
int isCDRomSupported(word drv)
//return nonzero if drv is a supported cd-rom drive
{
int stat;
__asm{
mov ax,0x150B
mov cx,[drv]
int 0x2f
cmp bx,0xADAD
je mscd
xor ax,ax
mscd:
mov [stat],ax
}
return stat;
}
int isCDRom(word drv)
//return 1 if drv is a cd-rom drive
{
word start,num;
__asm{
mov ax,0x1500
mov bx,0
int 0x2f
mov [num],bx
mov [start],cx
}
if ((drv>=start) && (drv<start+num)) return 1;
else return 0;
}
dword rba2lba(RedBookAddress rba)
{
return (dword)rba.minute*4500+rba.second*75+rba.frame-150;
}
void InitReqHeader(BasicReqHeader *brh,byte len,byte cmd,byte unit)
{
brh->length=len;
brh->sub_unit=unit;
brh->command=cmd;
brh->status=0;
}
void InitInputHeader(InputReqHeader *irh,void _far *cb,word length,byte unit)
{
InitReqHeader((BasicReqHeader*) irh,26,3,unit);
irh->media_descriptor=0;
irh->transfer_address=(dword)cb;
irh->size=length;
irh->start=0;
irh->error=0;
}
void InitAudioDiskInfo(AudioDiskInfo *adi)
{
adi->sub_function=0x0A;
}
void InitAudioTrackInfo(AudioTrackInfo *ati, byte track)
{
ati->sub_function=0x0B;
ati->track=track;
};
void SendDDRequest(char drive,void _far *rh)
{
_asm
{
mov ax,0x1510
mov cx,0
mov cl,[drive]
les bx,[rh]
int 0x2F
}
}
int GetAudioDiskInfo(char drive, unsigned char subunit,AudioDiskInfo *adi)
{
struct InputReqHeader i;
unsigned int status;
char error;
InitAudioDiskInfo(adi);
InitInputHeader(&i, (void _far*)adi, sizeof(struct AudioDiskInfo), subunit);
SendDDRequest(drive, (void _far*)&i);
status = i.basic.status;
if ((status & 0x8000) !=0)
{
error = status & 0x0F;
printf(msg[7], error);
return 0;
}
if ((status & 0x0200) !=0)
{
printf(msg[8]);
return 0;
}
return 1;
}
int GetAudioTrackInfo(char drive, byte subunit, byte trackNo,AudioTrackInfo *ati)
{
struct InputReqHeader i;
unsigned int status;
char error;
InitAudioTrackInfo(ati, trackNo);
InitInputHeader(&i, (void far*)ati, sizeof(struct AudioTrackInfo), subunit);
SendDDRequest(drive, (void far*)&i);
status = i.basic.status;
if ((status & 0x8000) !=0)
{
error = status & 0x0F;
printf(msg[9], error);
return 0;
}
if ((status & 0x0200) !=0)
{
printf(msg[8]);
return 0;
}
return 1;
}
dword DetermineVolStart(byte drive)
{
AudioDiskInfo adi;
AudioTrackInfo ati;
byte track, dataTrack;
if (GetAudioDiskInfo(drive,0,&adi)!=0)
{
if (forceTrack!=0)
{
if (forceTrack>adi.lastTrack)
{
printf(msg[10],forceTrack);
flushCache();
exit(1);
return 0;//quiet compiler
}
GetAudioTrackInfo(drive,0,forceTrack,&ati);
if ((ati.trackInfo & DATA_TRACK)==0)
{
printf(msg[11],forceTrack);
flushCache();
exit(1);
return 0;//quiet compiler
}
return rba2lba(ati.start);
}
if (adi.lastTrack>=adi.firstTrack)
{
dataTrack=0;
for (track=adi.firstTrack;track<=adi.lastTrack;track++) //Mixed-Mode
{
GetAudioTrackInfo(drive,0,track,&ati);
if ((ati.trackInfo & DATA_TRACK)!=0) dataTrack=track;
}
if (dataTrack!=0)
{
GetAudioTrackInfo(drive,0,dataTrack,&ati);
return rba2lba(ati.start);
}
else
{
printf(msg[12],'A'+drive);
flushCache();
exit(1);
return 0; //quiet compiler
}
}
else //CD-Extra
{
return rba2lba(adi.leadOutTrack)+11250; //Gap is 2:30.00
}
}
flushCache();
exit(1);
return 0; //quiet compiler
}
int readCDSectorsP(const struct DPB *b,dword sector,word count,void _far *buffer)
//does not work under Windows! (but we are in DOS mode, remember?)
{
byte cy;
word drive=b->drive;
__asm{
mov ax,0x1508
mov cx,[drive]
les bx,[buffer]
mov di,word ptr [sector]
mov si,word ptr [sector+2]
mov dx,[count]
int 0x2F
jc err
mov [cy],0
jmp noerr
err:
mov [cy],1
noerr:
nop
}
if (cy!=0)
{
printf(msg[13]);
return 1;
}
else return 0;
}
int readCDSectors(const struct DPB* b,dword psector,word count,void _far *buffer)
//cached
//a CD sector can consist of several cache pages since CD sectors are usually larger than MAX_SECTOR_SIZE
{
word h,i,j;
word page,tpage;
word pages_per_sector;
dword csector, sector=psector;
void *sbuf;
if (bpsd[b->drive]>MAX_SECTOR_SIZE) {
pages_per_sector=bpsd[b->drive] / MAX_SECTOR_SIZE;
sector=psector*pages_per_sector; //span cd sector across several pages
}
else pages_per_sector=1;
if ((bypassCache==1) || (cacheWasSetup==0)) {
return readCDSectorsP(b,psector,count,buffer);
}
for (i=0;i<count;i++)
{
for (word p=0;p<pages_per_sector;p++,sector++)
{
if (isInCache(b->drive, sector, &page))
{
if (useVMCache!=0) {
#ifdef VMEMORY
h=page/no_pages_per_area;
j=page % no_pages_per_area;
cachepage __far *cp=getCacheArea(h);
memcpy((void*)&(((char*)buffer)[i*bpsd[b->drive]+p*MAX_SECTOR_SIZE]),cp[j].data,MAX_SECTOR_SIZE);
timestamp++;
cp[j].timestamp=timestamp;
current_area_dirty=_VM_DIRTY;
vcachelookup[page].timestamp=timestamp;
#endif
}
else {
//no VMcache
memcpy((void*)&(((char*)buffer)[i*bpsd[b->drive]+p*MAX_SECTOR_SIZE]),cache[page]->data,MAX_SECTOR_SIZE);
timestamp++;
cache[page]->timestamp=timestamp;
}
}
else
{//Read sector and put in cache
sbuf=malloc(b->bytes_per_sector);
if (readCDSectorsP(b,psector,1,sbuf)!=0)
{
flushCache();
free(sbuf);
return 1;
}
csector=psector*pages_per_sector;
for (word q=0;q<pages_per_sector;q++)
{
//put the whole sector into the cache but only pages which are not yet in
if (!isInCache(b->drive,csector+q,&tpage)) {
tpage=getEmptyPage();
if (tpage==0xFFFF) tpage=discardOldestPage();
no_free_pages--;
if (useVMCache!=0) {
#ifdef VMEMORY
h=tpage/no_pages_per_area;
j=tpage % no_pages_per_area;
cachepage __far *cp=getCacheArea(h);
cp[j].drive=b->drive;
cp[j].sector=csector+q;
cp[j].dirty=CLEAN;