PDA

View Full Version : فرمت چند سکتور از دیسک



majid_borland
یک شنبه 19 مرداد 1382, 18:59 عصر
سلام.
به دریا علم و معرفت دلفی.
یک سوال ریزه و میزه برای شما دریای علم دارم.
فرمت چند سکتور از دیسک .
این کار را برای نوشتن قفل می خواهم.
خداحافظ.

مهدی کرامتی
یک شنبه 19 مرداد 1382, 21:03 عصر
این روتین رو من تو Win9x استفاده کرده‌ام (اون قدیما). اصولا باید تو NT Based هم کار کنه، اما تست نکرده‌ام:
<div dir="ltr">

unit WinFormatFloppy;

interface

uses
windows;

type
NumBytes = &#40;b128, b256, b512, b1024&#41;;
TSecFormat = packed record
TrackNum &#58; byte;
SideNum &#58; byte;
LogSecNum &#58; byte;
NumBytesInSec &#58; NumBytes;
end;
TSectorsFormat = array&#91;0..0&#93; of TSecFormat;
PSectorsFormat = ^TSectorsFormat;

function FormatTrack&#40;DriveNum, NumSectors &#58; byte;
SectorsFormat &#58; PSectorsFormat&#41; &#58; integer;

function SetMediaTypeForFormat&#40;DriveNum, NumTracks,
SecPrTrack &#58; integer&#41; &#58; integer;

implementation

const
VWIN32_DIOC_DOS_INT13 = 4; // Performs BIOS Interrupt 13h commands

type
PDIOC_REGS = ^TDIOC_REGS;
TDIOC_REGS = packed record
case byte of
0 &#58; &#40; EBX &#58; dword;
EDX &#58; dword;
ECX &#58; dword;
EAX &#58; dword;
EDI &#58; dword;
ESI &#58; dword;
Flags &#58; dword&#41;;

1 &#58; &#40; BX &#58; word; unused1 &#58; word;
DX &#58; word; unused2 &#58; word;
CX &#58; word; unused3 &#58; word;
AX &#58; word; unused4 &#58; word;
DI &#58; word; unused5 &#58; word;
SI &#58; word&#41;;

2 &#58; &#40; BL,BH &#58; byte; unused6 &#58; word;
DL,DH &#58; byte; unused7 &#58; word;
CL,CH &#58; byte; unused8 &#58; word;
AL,AH &#58; byte&#41;;
end;


function DoIOCTL&#40;Regs &#58; PDIOC_REGS; IoControlCode &#58; DWORD&#41; &#58; boolean;
var
hDevice &#58; THANDLE;
cb &#58; DWORD;
begin
hDevice &#58;= CreateFile&#40;'\\.\vwin32',0,0,nil,0,
FILE_FLAG_DELETE_ON_CLOSE,0&#41;;

if hDevice = INVALID_HANDLE_VALUE then
begin
result &#58;= false;
exit;
end;

Regs.Flags &#58;= $0001; // assume error &#40;carry flag set&#41;

result &#58;= DeviceIoControl&#40; hDevice, IoControlCode,
Regs, sizeof&#40;TDIOC_REGS&#41;,
Regs, sizeof&#40;TDIOC_REGS&#41;,
cb, nil&#41;;

CloseHandle&#40;hDevice&#41;;
end;


&#123;
INT 13 FUNCTION 1 - Disk Status
AH = 01

on return&#58;
AL = 00 no error
01 bad command passed to driver
02 address mark not found or bad sector
03 diskette write protect error
04 sector not found
05 fixed disk reset failed
06 diskette changed or removed
07 bad fixed disk parameter table
08 DMA overrun
09 DMA access across 64k boundary
0A bad fixed disk sector flag
0B bad fixed disk cylinder
0C unsupported track/invalid media
0D invalid number of sectors on fixed disk format
0E fixed disk controlled data address mark detected
0F fixed disk DMA arbitration level out of range
10 ECC/CRC error on disk read
11 recoverable fixed disk data error, data fixed by ECC
20 controller error &#40;NEC for floppies&#41;
40 seek failure
80 time out, drive not ready
AA fixed disk drive not ready
BB fixed disk undefined error
CC fixed disk write fault on selected drive
E0 fixed disk status error/Error reg = 0
FF sense operation failed

- codes represent controller status after last disk operation
- returns the status byte located at 40&#58;41 in the ~BIOS Data Area~



INT 13 FUNCTION 18 - Set Media Type for Format &#40;BIOS date specific&#41;

AH = 18h
CH = lower 8 bits of number of tracks &#40;0-1023 dec., see below&#41;
CL = sectors per track &#40;1-17 dec., see below&#41;
DL = drive number &#40;0=A&#58;, 1=2nd floppy, 80h=drive 0, 81h=drive 1&#41;

on return&#58;
ES&#58;DI = pointer to 11-byte ~Disk Base Table~ &#40;DBT&#41;
AH = 00h if requested combination supported
= 01h if function not available
= 0Ch if not supported or drive type unknown
= 80h if there is no media in the drive
CF = 0 if successful
= 1 if error

- valid only for XT BIOS dated after 1/10/86, AT after 11/15/86,
XT 286 and the PS/2 line
- only disk number is checked for validity

- track number is a 10 bit value taken from the 2 high order
bits of CL and the 8 bits in CH &#40;low order 8 bits of track&#41;&#58;

¦F¦E¦D¦C¦B¦A¦9¦8¦7¦6¦5¦4¦3¦2¦1¦0¦ CX
¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ +-------------- sectors per track count
¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ +-------------- high order 2 bits track/cyl count
+-------------------------- low order 8 bits of track/cyl count
&#125;

function SetMediaTypeForFormat&#40;DriveNum, NumTracks,
SecPrTrack &#58; integer&#41; &#58; integer;
var
regs &#58; TDIOC_REGS;
begin
fillchar&#40;regs,sizeof&#40;TDIOC_REGS&#41;,0&#41;;

regs.DL &#58;= DriveNum;
regs.CH &#58;= NumTracks;
regs.CL &#58;= SecPrTrack;
regs.AH &#58;= $18;

if DoIOCTL&#40;@regs, VWIN32_DIOC_DOS_INT13&#41; then
result &#58;= regs.AH
else
result &#58;= -1;
end;


&#123;
INT 13 FUNCTION 5 - Format Disk Track

AH = 05
AL = Number of sectors to create on this track
CX = track/cylinder number &#40;see below for format&#41;
DH = head number &#40;0-15 dec.&#41;
DL = drive number &#40;0=A&#58;, 1=2nd floppy, 80h=drive 0, 81h=drive 1&#41;
ES&#58;BX = pointer to block of "track address fields" containing four
byte fields for each sector to be formatted of the form&#58;

1 byte track number
1 byte head number Size #
1 byte sector number Codes Bytes
1 byte sector size code
0 128
1 256
2 512
3 1024

on return&#58;
AH = status &#40;see ~INT 13,STATUS~&#41;
CF = 0 if successful
= 1 if error

- BIOS disk write attempts should reset the controller on error
- ~INT 13,17~ should be called to set the DASD type
- this function is capable of doing great damage if the parameters
are incorrectly specified; only the drive number is checked
- initializes disk address fields and data sectors
- after INT 13 disk format, if the disk is to be used with DOS the
DOS data structure must be written
- only the disk number is checked for validity
- the parameters in CX change depending on the number of cylinders;

the track/cylinder number is a 10 bit value taken from the 2 high
order bits of CL and the 8 bits in CH &#40;low order 8 bits of track&#41;&#58;

¦F¦E¦D¦C¦B¦A¦9¦8¦7¦6¦5-0¦ CX &#40;cylinder value 0-1023 dec.&#41;
¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ +----- unused
¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ +--------- high order 2 bits of track/cylinder
+------------------------ low order 8 bits of track/cyl number
&#125;

function FormatTrack&#40;DriveNum,NumSectors &#58; byte;
SectorsFormat &#58; PSectorsFormat&#41; &#58; integer;
var
regs &#58; TDIOC_REGS;
begin
fillchar&#40;regs,sizeof&#40;TDIOC_REGS&#41;,0&#41;;

regs.EBX &#58;= DWORD&#40;SectorsFormat&#41;;
regs.DL &#58;= DriveNum;
regs.DH &#58;= SectorsFormat&#91;0&#93;.SideNum;
regs.CH &#58;= SectorsFormat&#91;0&#93;.TrackNum;
regs.AL &#58;= NumSectors;
regs.AH &#58;= $05;

if DoIOCTL&#40;@regs, VWIN32_DIOC_DOS_INT13&#41; then
result &#58;= regs.AH
else
result &#58;= -1;
end;

end.
</div>
موفق باشید.

majid_borland
دوشنبه 20 مرداد 1382, 20:50 عصر
سلام.
ممنوع از جوابت.
اما لطفا یک مثال در مورد فرمت کردن با استاندارد 1024بایت توضیح بده.
و در مورد خواندن و نوشتن روی بد سکتور 1024 بایت توضیح بدهید.
خداحافظ.