-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdisk322.pas
More file actions
executable file
·1537 lines (1299 loc) · 36.7 KB
/
pdisk322.pas
File metadata and controls
executable file
·1537 lines (1299 loc) · 36.7 KB
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
{
Copyright 1998, 1999 Colin Davis
This source code is free and you may use it any way you wish. Use
at your own risk. I make no warranty that the code is correct.
Compile with TMT Pascal for DOS/DPMI with the TMTStub (others may work).
You can get the evaluation version from http://www.tmt.com. It will
compile this and is a fully working compiler.
This unit provides the Pascal programmer with the basic disk functions
like the biosdisk() function in BIOS.H from DJGPP and other C libraries.
In addition it provides many other disk functions like LBA to CHS/ CHS
to LBA translation and functions to access disks using either addressing
mode. It can use the int13h extensions to access a drive larger than 8.4GB.
I put in an object type TpartitionTable because the partitions aren't
part of an OS and if you're using a disk library chances are you want to
read or write the partition table(s). The other source code on this
site provides examples of how to use this object and this unit in general.
}
unit pdisk322;
interface
uses dos,go32,smallbuf,dosmem, crt;
const
{ Partition table offsets in the mbr }
part1 = 446;
part2 = part1 + 16;
part3 = part1 + 32;
part4 = part1 + 48;
FAT12 = 1; { on floppies or small hard disks }
fat16 = 6;
EXT = 5; {extended partition table }
EXT95=15; {extended partition table using LBA}
FAT32 = 12; {FAT32 using LBA in the partition table addresses}
FAT32x = 11;
WIN95 = 14; {FAT16 -- Use LBA in the addresses instead of CHS }
DOS4 = 4; {FAT16 }
NTFS = 7;
DBFS = $E0;
PRI = 0;
BR = 0;
PART = 1;
sectorsize = 512;
{BIOS numbers for the physical drives.}
Drive_A = $00;
drive_B= $01;
drive_1 = $80;
drive_2 = $81;
drive_3 = $82;
drive_4 = $83;
{Disk error codes returned from BIOS disk operations}
noerror = 0;
badcommand = 1;
badsector = 2;
writeprotected = 3;
sectornotfound = 4;
diskchange = 6;
invalidmedia = $0C;
controllererror = $20;
seekfailure = $40;
timeout = $80;
NotFAT16 = $FE;
NoDPB = $FF;
NOTEXT = $FD;
NOFREEREC = $FC;
floppies=[drive_A,drive_B];
harddisks=[drive_1,drive_2,drive_3,drive_4];
FATtypes = [FAT12,DOS4,FAT16,WIN95,FAT32x,FAT32];
EXTtypes = [EXT,EXT95];
FAT16types = [DOS4,WIN95,FAT16];
FAT32types = [FAT32x,FAT32];
{From compatibility with TMT Pascal}
_zero = 0;
type
Ponesector = ^onesector;
oneSector = array[0..511] of byte;
Tdrvparams = record
Cylinders:word;
heads,sectors:byte;
end;
TdiskAddressPacket = record {Used in ext BIOS read/write }
packetsize, { should be 16d}
blocks, {blocks to transfer, up to 127}
o,s:word; {segment,offset of buffer}
startlo,starthi:dword; {lo and hi part of LBA *}
end;
{
****************************************************************************
(FROM D1226R6 Technical Report )
Starting logical block address, on the target device, of the data to be
transferred. This is a 64 bit unsigned linear address. If the device
supports LBA addressing this value should be passed unmodified. If the
device does not support LBA addressing the following formula holds true
when the address is converted to a CHS value:
LBA = (C1 * H0 + H1) * S0 + S1 - 1
Where:
C1 = Selected Cylinder Number
H0 = Number of Heads (Maximum Head Number + 1)
H1 = Selected Head Number
S0 = Maximum Sector Number
S1 = Selected Sector Number
*****************************************************************************
}
TpartRec = record { This matches the table in the MBR}
active ,
StartHD:byte;
StartCylSect:word;
PartType,
EndHd:byte;
EndCylSect:word;
StartLBA,
size:dword;
end;
Tpartition = object
DP:Tdrvparams; { Parameters of current drive }
part:Tpartrec;
active:boolean;
startlba,size:dword; { LBA written in the partition table, }
StartPartLBA:dword; { Actual LBA the partition starts at }
startcylinder:word;
starthead,startsector:byte;
endcylinder:word;
endsector,endhead:byte;
partType:String;
function unknownType:boolean;
procedure setactive;
procedure SetPartRec(tablepos:dword;t:byte;StartPos,len:dword);
procedure setStartLCHS(c:word;h,s:byte);
procedure setEndLCHS(c:word;h,s:byte);
procedure setStartLBA(b:dword);
procedure setSize(b:dword);
procedure setpartType(t:byte);
procedure convert(dest:tdrvparams);
procedure start(d:Tdrvparams;p:Tpartrec);
end;
pPartitionTable = ^TpartitionTable;
TpartitionTable = object
partition:array[1..4] of Tpartition;
dp:Tdrvparams;
Location:dword;
PartitionSector:OneSector;
function drivesize:dword;
function TableEmpty:boolean;
function PrimaryExists:boolean;
function TableFull:boolean;
function ExtendedExists:boolean;
function DBFSexists:boolean;
procedure SetEXTpart(start, len:dword);
function primarypart:word;
function ExtendedPart:word;
function DBFSpart:word;
function StartPrimary:dword;
function startExtended:dword;
function startDBFS:dword;
function largestFreeBlocksize:dword;
function largestfreeblockstart:dword;
function PartitionedSpace:dword; {Total partitioned space in this table}
function NextUnpartitionedSector:dword;
procedure convert(ndp:tdrvparams);
procedure UpdatePartitionSector;
function NextTrack(a:dword):dword;
constructor init(d:Tdrvparams;ps:onesector;loc:dword);
destructor finish;
end;
const
mbrMaster:onesector =
(51,192,142,208,188,0,124,251,80,7,80,31,252,190,27,124,191,27,6,80,87,
185,229,1,243,164,203,190,190,7,177,4,56,44,124,9,117,21,131,198,16,226,
245,205,24,139,20,139,238,131,198,16,73,116,22,56,44,116,246,190,16,7,78,
172,60,0,116,250,187,7,0,180,14,205,16,235,242,137,70,37,150,138,70,4,180,
6,60,14,116,17,180,11,60,12,116,5,58,196,117,43,64,198,70,37,6,117,36,187,
170,85,80,180,65,205,19,88,114,22,129,251,85,170,117,16,246,193,1,116,11,
138,224,136,86,36,199,6,161,6,235,30,136,102,4,191,10,0,184,1,2,139,220,
51,201,131,255,5,127,3,139,78,37,3,78,2,205,19,114,41,190,70,7,129,62,254,
125,85,170,116,90,131,239,5,127,218,133,246,117,131,190,39,7,235,138,152,
145,82,153,3,70,8,19,86,10,232,18,0,90,235,213,79,116,228,51,192,205,19,
235,184,0,0,128,72,38,21,86,51,246,86,86,82,80,6,83,81,190,16,0,86,139,
244,80,82,184,0,66,138,86,36,205,19,90,88,141,100,16,114,10,64,117,1,66,
128,199,2,226,247,248,94,195,235,116,73,110,118,97,108,105,100,32,112,97,
114,116,105,116,105,111,110,32,116,97,98,108,101,0,69,114,114,111,114,32,
108,111,97,100,105,110,103,32,111,112,101,114,97,116,105,110,103,32,115,
121,115,116,101,109,0,77,105,115,115,105,110,103,32,111,112,101,114,97,116,
105,110,103,32,115,121,115,116,101,109,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,139,252,30,87,139,245,203,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,245,155,246,145,0,0,128,1,1,0,6,254,63,130,63,0,0,0,132,28,
32,0,0,0,1,131,5,254,127,4,195,28,32,0,2,222,31,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,85,170);
type
TDA = array[drive_1..drive_4] of boolean;
var
ErrorStatus:byte; {should usually match the diskstatus fn}
{should be 21h for d1226r6 report but 20h should work ??}
BIOSextensionS:word; {get this from int 13h, fn 41h (check extensions)}
Extensions:array[drive_1..drive_4] of word;
fixed_disk_ext, drive_lock_and_eject_ext, EDD_ext:tda;
floppy:array[drive_A..drive_B] of Tdrvparams;
harddisk:array[drive_1..drive_4] of Tdrvparams;
procedure getdrvparams;
function DriveSz(d:byte):dword;
function drivesize(dp:tdrvparams):dword;
procedure driveInfo(drv:byte;var hds:byte;var cyl:word;var sec:byte);
function diskstatus:byte;
procedure resetdisk(drive:byte);
procedure readsectorLBA(drv:byte;b:dword;d:ponesector);
procedure writesectorLBA(drv:byte;b:dword;d:ponesector);
procedure writetrackLBA(drv:byte;b:longint;num:byte;dest:pbuf);
procedure readtracklba(drv:byte;b:longint;num:byte; dest:pbuf);
function error:string;
function DrivesAttached:byte;
function driveExists(drive:byte):boolean;
procedure initDOSmembuffer;
procedure removeDOSmembuffer;
{ Mostly private but still need to be in interface part }
function EmptyPartitionSector:onesector;
function lchstoLBA(c:word;h,s:byte;dp:tdrvparams):dword;
procedure LBAtoLchs(b:dword;var c:word;var h,s:byte;dp:tdrvparams);
function heads(drive:byte):byte;
procedure convertCX(valcx:word;var sect:byte;var cyl:word);
function cylinders(drive:byte):word;
function sectors(drive:byte):byte;
function MakeCX(sect:byte; cyl:word):word;
procedure readTrack(drive,num,sector:byte; track,head:word; dest:pbuf);
procedure readsector(drive:byte;track,head:word;sector:byte; dest:pointer);
procedure writeTrack(drive,num,sector:byte; track,head:word; dest:pbuf);
procedure writesector(drive:byte;track,head:word;sector:byte; dest:pointer);
implementation
const transfer_mem=1; DAP_mem=5;
var dosbuf:array[transfer_mem..DAP_mem] of Tdosmem;
diskTransferSeg, DAPseg:word;
procedure init_mem;
begin
dosbuf[transfer_mem].init(smallbufsize);
dosbuf[DAP_mem].init($10);
diskTransferSeg:=dosbuf[transfer_mem].segment;
DAPseg:=dosbuf[DAP_mem].segment;
end;
procedure release_mem;
var i:word;
begin
for i:=transfer_mem to DAP_mem do
dosbuf[i].done;
end;
procedure DAPread(b:dword; len:byte);
var dap:TdiskaddressPacket;
begin
with DAP do
begin
packetsize:=$10;
blocks:=len;
s:=disktransferseg;
o:=0;
startlo:=b;
starthi:=0;
end;
dosbuf[DAP_mem].put(DAP);
// move(DAP,diskaddressptr^,$10);
end;
procedure DAPwrite(b:dword; len:byte; var buf);
var DAP:TdiskAddressPacket;
begin
// Put data in place, in the transfer buffer
dosbuf[transfer_mem].put(buf,512*len);
// move(buf, diskTransferPtr^, 512*len);
with DAP do
begin
packetsize:=$10;
blocks:=len;
s:=DiskTransferSeg;
o:=0;
startlo:=b;
starthi:=0;
end;
dosbuf[DAP_mem].put(DAP);
// move(DAP,diskAddressPtr^,$10);
end;
{ 40:75 is in the table BIOS builds on post to show what hardware has been
found. Here it puts the number of fixed disks. You can then find if
a disk exists easily because there are no gaps in the numbering of BIOS
drives. They start at 80h so if you want to know if drive 83h exists,
just add 80h and drivesattached if it is lower than 83h the drive does not
exist. }
function DrivesAttached:byte;
var d:byte absolute $40:$75;
begin
drivesattached:=d;
end;
function driveExists(drive:byte):boolean;
begin
if drive>127 then drive:=drive-127;
if drive>drivesattached then driveExists:=false else driveExists:=true;
end;
function EmptyPartitionSector:onesector;
var e:onesector;
i:word;
begin
e:=mbrMaster;
for i:=part1 to part4+15 do e[i]:=0;
EmptyPartitionSector:=e;
end;
// Get size using extended int13,48 function
function EXTsize(drv:byte):dword;
Type
FDG_STRUCT = record
bufsize, {size of buffer}
flags : word; {information flags}
cylinders,
heads,
sectorsPerTrack : dword;
sectorsOnDrivelo,
sectorsondrivehi: Dword;
bytesPerSector : word;
end;
const infobufsize:byte=30;
var
r : trealregs;
infobuf : FDG_STRUCT;
begin
fillchar(r, sizeof(r), 0);
fillchar(infobuf, sizeof(FDG_STRUCT), 0);
infobuf.flags := 0;
infobuf.bufsize:=infobufsize;
dosbuf[transfer_mem].put(infobuf,infobufsize);
With r do
Begin
flags := 0;
ah:=$48;
dl:=drv;
ds :=dosbuf[transfer_mem].segment;
si := 0;
end;
realintr($13, r);
dosbuf[transfer_mem].get(infobuf,infobufsize);
EXTsize:=infobuf.sectorsondrivelo;
end;
// For < 8.4 GB drives
function drivesz(d:byte):dword;
var c1,s1,h1:dword;
dp:tdrvparams;
begin
if d in harddisks then dp:=harddisk[d];
if d in floppies then dp:=floppy[d];
s1:=dp.sectors;
h1:=dp.heads;
c1:=dp.cylinders;
if (c1=0) or (s1<2) or (h1<1) then drivesz:=0 else
drivesz:=(s1*(h1+1)*(c1+1)) div 2048;
end;
// Same as above, just takes the params structure
function drivesize(dp:tdrvparams):dword;
var s1,h1:dword;
c1:dword;
begin
s1:=dp.sectors;
h1:=dp.heads;
c1:=dp.cylinders;
if (c1=0) or (s1<2) or (h1<1) then drivesize:=0 else
drivesize:=(s1*(h1+1)*(c1+1));
end;
function disknum(drive:byte):byte;
var regs:TRealRegs;
begin
fillchar(regs, sizeof(regs), 0);
with regs do
begin
ah:=08;
dl:=drive;
Realintr($13,regs);
disknum:=dl;
end;
end;
// Takes a < 8.4GB chs address and converts to LBA
function lchstoLBA(c:word;h,s:byte;dp:tdrvparams):dword;
var s5,c5,h5,cl,hl,sl:dword;
begin
cl:=c;
hl:=h;
sl:=s;
s5:=dp.sectors;
c5:=dp.cylinders+1;
h5:=dp.heads+1;
lchstolba:=((cl * h5+ hl)*s5) + sl - 1;
end;
// Takes an LBA and converts to the < 8.4GB chs addressing scheme
// Do not use on address > 8.4GB into the drive
procedure LBAtoLchs(b:dword;var c:word;var h,s:byte;dp:tdrvparams);
var Newsec,Newhead:byte;
Newcyl:word;
tmp:dword;
begin
with dp do
begin
newcyl := b div ((heads+1) * sectors);
tmp := b mod ((heads+1) * sectors);
newhead := tmp div sectors;
newsec := tmp mod sectors + 1;
end;
c:=newcyl;
h:=newhead;
s:=newsec;
end;
function heads(drive:byte):byte;
var regs:TRealRegs;
begin
fillchar(regs, sizeof(regs), 0);
with regs do
begin
ah:=$08;
dl:=drive;
Realintr($13,regs);
heads:=dh;
end;
end;
procedure convertCX(valcx:word;var sect:byte;var cyl:word);
var c,sec:byte;
x:word;
regs:TRealRegs;
begin
fillchar(regs, sizeof(regs), 0);
with regs do
begin
cx:=valcx;
bx:=valcx;
c:=ch;
cx:=cx and 192;
{ and cx,0000000011000000b}
cx:=cx shl 2;
x:=cx;
cx:=bx;
cl:=cl and 63;
{ and cl,00111111b}
sect:=cl;
cyl:=x+c;
end;
end;
function cylinders(drive:byte):word;
var regs:TRealRegs;
begin
fillchar(regs, sizeof(regs), 0);
with regs do
begin
ah:=$08;
dl:=drive;
Realintr($13,regs);
dx:=0;
dl:=ch;
cx:=cx and 192;
{ and cx,0000000011000000b}
cx:=cx shl 2;
cx:=cx + dx;
cylinders:=cx;
end;
end;
function sectors(drive:byte):byte;
var regs:TRealRegs;
begin
fillchar(regs, sizeof(regs), 0);
with regs do
begin
ah:=$08;
dl:=drive;
Realintr($13,regs);
cl:=cl and 63;
sectors:=cl;
end;
end;
// Strictly for < 8.4GB drives or when no BIOS extensions are available
procedure driveInfo(drv:byte;var hds:byte;var cyl:word;var sec:byte);
begin
hds:=0;
cyl:=0;
sec:=0;
resetdisk(drv);
if diskstatus<>0 then exit;
hds:=heads(drv);
if diskstatus<>0 then exit;
cyl:=cylinders(drv);
if diskstatus<>0 then exit;
sec:=sectors(drv);
if diskstatus<>0 then
begin
sec:=0;
hds:=0;
cyl:=0;
end;
end;
procedure getdrvparams;
var I, dn:byte;
regs:TRealRegs;
begin
resetdisk($80);
for i:=drive_A to drive_B do
with floppy[i] do
driveinfo(i,heads,cylinders,sectors);
for i:=drive_1 to drive_4 do
with harddisk[i] do
if driveExists(i) then
driveinfo(i,heads,cylinders,sectors)
else
begin
heads:=0;
cylinders:=0;
sectors:=0;
end;
// Check for IBM/Microsoft extensions on each drive
BIOSextensions:=0;
for i:=drive_1 to drive_4 do
begin
fixed_disk_ext[i]:=false;
drive_lock_and_eject_ext[i]:=false;
EDD_ext[i]:=false;
end;
for i:=drive_1 to drive_4 do
begin
fillchar(regs, sizeof(regs), 0);
with regs do
begin
ah:=$41; {check extensions fn }
bx:=$55aa; {sys signature word }
dl:=i; { drive 1 to drive 4}
Realintr($13, regs);
if (not odd(flags)) and (bx=$aa55) then
begin
BIOSextensions:=ah;
if odd(cx) then fixed_disk_ext[i]:=true;
if odd(cx shr 1) then drive_lock_and_eject_ext[i]:=true;
if odd(cx shr 2) then EDD_ext[i]:=true;
extensions[i]:=cx;
end else extensions[i]:=0;
end;
end;
end;
{ From D1226r6 Technical Report:
Check extensions present
Entry:
AH - 41h
BX - 55AAh
DL - Drive number
Exit:
carry clear
AH - Version of extensions
AL - Internal use only
BX - AA55h
CX - Interface support bit map (see Table 9Table 12 10)
carry set
AH - error code (01h, Invalid Command)
Table 12109 ? Extension rResult buffer
Bit
Description
0
1 - Fixed disk access subset
1
1 - Drive locking and ejecting subset
2
1 - Enhanced disk drive support subset
3-15
Reserved, must be 0
}
function diskstatus:byte;
var regs:TRealRegs;
begin
fillchar(regs, sizeof(regs), 0);
with regs do
begin
ah:=1;
Realintr($13,regs);
diskstatus:=al;
end;
end;
procedure resetdisk(drive:byte);
var regs:TRealRegs;
begin
with regs do
begin
ah:=ah xor ah;
dl:=drive;
Realintr($13,regs);
end;
end;
function MakeCX(sect:byte; cyl:word):word;
var regs:TRealRegs;
begin
with regs do
begin
bx:=cyl;
ax:=cyl;
ax:=ax shr 2;
ax:=ax and 192;
al:=al + sect;
bx:=bx shl 8;
bx:=bx and 65280;
{ and bx,1111111100000000b}
bx:=bx+ax;
makeCX:=bx;
end;
end;
// Only for drives < 8.4 GB
procedure readTrack(drive,num,sector:byte;track,head:word; dest:pbuf);
var
regs : TRealRegs;
tcx:word;
begin
fillchar(regs, sizeof(regs), 0);
tcx:= makeCX(sector,track);
with regs do
begin
ah:=2;
al:=num;
dl:=drive;
cx:=TCX;
bx:=head;
dh:=bl;
bx:=0;
es:=disktransferseg;
Realintr($13,regs);
end;
dosbuf[transfer_mem].get(dest^,num*512);
// move(dosbufptr^,dest^,num*512);
end;
{ read one sector }
procedure readsector(drive:byte;track,head:word;sector:byte; dest:pointer);
var
regs : TRealRegs;
tcx:word;
begin
fillchar(regs, sizeof(regs), 0);
tcx:= makeCX(sector,track);
with regs do
begin
ah:=2;
al:=1;
dl:=drive;
cx:=TCX;
bx:=head;
dh:=bl;
bx:=0;
es:=disktransferseg;
Realintr($13,regs);
end;
dosbuf[transfer_mem].get(dest^,512);
// move(dosbufptr^,dest^,512);
end;
procedure EXTread(drv:byte; b:dword; d:ponesector);
var regs:TRealRegs;
begin
DAPread(b, 1); {prepare disk address packet}
with regs do
begin
fillchar(regs, sizeof(regs), 0);
ah:=$42; {extended read fn}
al:=0;
dl:=drv;
ds:=DAPseg;
si:=0;
Realintr($13, regs);
if odd(flags) then
begin
if diskstatus<>0 then ErrorStatus:=diskstatus
else Errorstatus:=BadSector;
exit;
end;
end;
dosbuf[transfer_mem].get(d^,512);
//move(diskTransferPtr^, d^, 512);
end;
procedure EXTreadTrack(drv:byte; b:dword; num:byte; d:pbuf);
var regs:TRealRegs;
begin
DAPread(b, num); {prepare disk address packet}
with regs do
begin
fillchar(regs, sizeof(regs), 0);
ah:=$42; {extended read fn}
al:=0;
dl:=drv;
ds:=DAPseg;
si:=0;
Realintr($13, regs);
if odd(flags) then
begin
if diskstatus<>0 then ErrorStatus:=diskstatus
else Errorstatus:=BadSector;
exit;
end;
end;
dosbuf[transfer_mem].get(d^,num*512);
// move(diskTransferPtr^, d^, 512*num);
end;
procedure extWrite(drv:byte; b:dword; d:ponesector);
var regs:TRealRegs;
begin
DAPwrite(b, 1, d^);
fillchar(regs, sizeof(regs), 0);
with regs do
begin
ah:=$43; { ext write fn}
al:=1; { 1 = write with verify off -- faster }
dl:=drv;
ds:=DAPseg;
si:=0;
Realintr($13, regs);
if odd(flags) then
begin
if diskstatus<>0 then ErrorStatus:=diskstatus
else Errorstatus:=BadSector;
exit;
end;
end;
end;
procedure extWriteTrack(drv:byte; b:dword; num:byte; d:pbuf);
var regs:TRealRegs;
begin
DAPwrite(b, num, d^);
fillchar(regs, sizeof(regs), 0);
with regs do
begin
ah:=$43; { ext write fn}
al:=1; { 1 = write with verify off -- faster }
dl:=drv;
ds:=DAPseg;
si:=0;
Realintr($13, regs);
if odd(flags) then
begin
if diskstatus<>0 then ErrorStatus:=diskstatus
else Errorstatus:=BadSector;
exit;
end;
end;
end;
procedure readsectorLBA(drv:byte;b:dword;d:ponesector);
var c:word;
s,h:byte;
CurDrvParams:tdrvparams;
begin
if (drv in harddisks) and fixed_disk_ext[drv] then
ExtRead(drv, b, d)
else begin
if drv in floppies then curdrvparams:=floppy[drv]
else curdrvparams:=hardDisk[drv];
lbatolchs(b,c,h,s,curdrvparams);
readsector(drv,c,h,s,d);
end;
end;
// This procedure hides the messy
// details of writing across track
// boundaries.
procedure readtracklba(drv:byte;b:longint;num:byte;dest:pbuf);
var dp:Tdrvparams;
c,p,tcx:word;
n1,n2,h,s:byte;
tmpptr:pointer;
h1:word;
begin
if (drv in harddisks) and fixed_disk_ext[drv] then
ExtReadTrack(drv, b, num, dest)
else
begin
if drv in floppies then dp:=floppy[drv] else dp:=hardDisk[drv];
lbatolchs(b,c,h,s,dp);
n2:=0;
n1:=dp.sectors-(s-1);
if num<n1 then n1:=num;
if num>n1 then n2:=num-n1;
h1:=h;
readtrack(drv,n1,s,c,h1,dest);
if n2>0 then
begin
b:=b+n1;
lbatolchs(b,c,h,s,dp);
h1:=h;
getmem(tmpptr,n2*512);
readtrack(drv,n2,s,c,h1,tmpptr);
move(tmpptr^,dest^[n1*512],n2*512);
freemem(tmpptr,n2*512);
end;
end;
end;
procedure writeTrack(drive,num,sector:byte; track,head:word; dest:pbuf);
var
regs : TRealRegs;
tcx:word;
begin
fillchar(regs, sizeof(regs), 0);
tcx:= makeCX(sector,track);
with regs do
begin
ah:=3;
al:=num;
dl:=drive;
cx:=TCX;
bx:=head;
dh:=bl;
bx:=0;
es:=disktransferseg;
dosbuf[transfer_mem].put(dest^,num*512);
//move(dest^,dosbufptr^,num*512);
Realintr($13,regs);
end;
end;
procedure writetrackLBA(drv:byte;b:longint;num:byte;dest:pbuf);
var dp:Tdrvparams;
tmpPtr:pointer;
c,p,tcx:word;
n1,n2,h,s:byte;
h1:word;
begin
if (drv in harddisks) and fixed_disk_ext[drv] then
ExtwriteTrack(drv, b, num, dest)
else
begin
if drv in floppies then dp:=floppy[drv] else dp:=hardDisk[drv];
lbatolchs(b,c,h,s,dp);
h1:=h;
n2:=0;
n1:=dp.sectors-(s-1);
if num>n1 then n2:=num-n1;
if num<n1 then n1:=num;
writetrack(drv,n1,s,c,h1,dest);
if n2>0 then
begin
b:=b+n1;
lbatolchs(b,c,h,s,dp);
h1:=h;
getmem(tmpptr,n2*512);
move(dest^[n1*512],tmpptr^,n2*512);
writetrack(drv,n2,s,c,h1,tmpptr);
freemem(tmpptr,n2*512);
end;
end;
end;
procedure writesector(drive:byte;track,head:word;sector:byte; dest:pointer);
var regs : TRealRegs;
tcx:word;
begin
fillchar(regs, sizeof(regs), 0);
tcx:= makeCX(sector,track);
with regs do
begin
ah:=3;
al:=1;
dl:=drive;
cx:=TCX;
bx:=head;
dh:=bl;
bx:=0;
es:=disktransferseg;
dosbuf[transfer_mem].put(dest^,512);
//move(dest^,dosbufptr^,512);
Realintr($13,regs);
end;
end;
procedure writesectorLBA(drv:byte;b:dword;d:ponesector);
var c:word;
s,h:byte;
CurDrvParams:tdrvparams;
begin
if (drv in harddisks) and fixed_disk_ext[drv] then
Extwrite(drv, b, d)