title image


Smiley Re: In den Bootsektor schreiben
Ich hab dafür ein paar Befehle geschrieben:





//*****************************************************************************

//*

//*

//* DiskIO.cpp

//*

//*

//*****************************************************************************

#include

#include "DiskIO.h"



#define VWIN32_DIOC_DOS_IOCTL 1

#define VWIN32_DIOC_DOS_DRIVEINFO 6



#define IOCTL_CDROM_BASE FILE_DEVICE_CD_ROM

#define IOCTL_CDROM_GET_DRIVE_GEOMETRY CTL_CODE(IOCTL_CDROM_BASE, 0x0013, METHOD_BUFFERED, FILE_READ_ACCESS)



static char iSectors[]=

{

0, // Unknown Format is unknown

15, // F5_1Pt2_512 5.25" 1.2MB 512 bytes/sector

18, // F3_1Pt44_512 3.5" 1.44MB 512 bytes/sector

36, // F3_2Pt88_512 3.5" 2.88MB 512 bytes/sector

0, // F3_20Pt8_512 3.5" 20.8MB 512 bytes/sector

9, // F3_720_512 3.5" 720KB 512 bytes/sector

9, // F5_360_512 5.25" 360KB 512 bytes/sector

8, // F5_320_512 5.25" 320KB 512 bytes/sector

};



static char iTracks[]=

{

80, // Unknown Format is unknown

80, // F5_1Pt2_512 5.25" 1.2MB 512 bytes/sector

80, // F3_1Pt44_512 3.5" 1.44MB 512 bytes/sector

80, // F3_2Pt88_512 3.5" 2.88MB 512 bytes/sector

0, // F3_20Pt8_512 3.5" 20.8MB 512 bytes/sector

80, // F3_720_512 3.5" 720KB 512 bytes/sector

40, // F5_360_512 5.25" 360KB 512 bytes/sector

40, // F5_320_512 5.25" 320KB 512 bytes/sector

};





#pragma pack(1)





typedef struct _ExtGetDskFreSpcStruc

{

WORD wSize;

WORD wLevel;

DWORD dwSectorsPerCluster;

DWORD dwBytessPerSector;

DWORD dwAvailableClusters;

DWORD dwTotalClusters;

DWORD dwAvailablePhysSectors;

DWORD dwTotalPhysSectors;

DWORD dwAvailableAllocationUnits;

DWORD dwTotalAllocationUnits;

DWORD dwRsvd[2];

}ExtGetDskFreSpcStruc;



/*

typedef struct

{

DWORD dwSize;

DWORD dwLevel;

ULARGE_INTEGER ddSectorsPerCluster;

ULARGE_INTEGER ddBytesPerSector;

ULARGE_INTEGER ddAvailableClusters;

ULARGE_INTEGER ddTotalClusters;

ULARGE_INTEGER ddAvailablePhysSectors;

ULARGE_INTEGER ddTotalPhysSectors;

ULARGE_INTEGER ddAvailableAllocationUnits;

ULARGE_INTEGER ddTotalAllocationUnits;

ULARGE_INTEGER ddRsvd;

}ExtGetDskFreSpcStruc;

*/



typedef struct

{

DWORD dwStartingSector ;

WORD wNumberOfSectors ;

LPVOID pBuffer;

}CONTROL_BLOCK;



typedef struct _DIOC_REGISTERS

{

DWORD reg_EBX;

DWORD reg_EDX;

DWORD reg_ECX;

DWORD reg_EAX;

DWORD reg_EDI;

DWORD reg_ESI;

DWORD reg_Flags;

}DIOC_REGISTERS;



typedef struct _FVBLOCK

{

BYTE fvSpecFunc; // special functions = 0

WORD fvHead; // head to format/verify

WORD fvCylinder; // cylinder to format/verify

WORD fvTracks; // tracks to format/verify

}FVBLOCK;





#pragma pack()







//*****************************************************************************

//*

//* IsWinNt

//*

//*****************************************************************************

inline BOOL IsWinNt()

{

static OSVERSIONINFO sInfo={sizeof(OSVERSIONINFO),0};



if(!sInfo.dwMajorVersion)GetVersionEx(&sInfo);



return sInfo.dwMajorVersion>4;

}



//*****************************************************************************

//*

//* ReadSector

//*

//*****************************************************************************

// Auslesen eines oder mehrere logischer Sektoren (immer 512 Byte groß !!!)

// iDrive : Ist die Nummer des Laufwerkes (0=A 1=B 2=C ...)

// dwStart : Nummer des ersten Sektors

// dwCount : Ist die Anzahl der Sektoren (1 bis 0xFFFF)

// pBuffer : Zeiger auf den Puffer in dem die Sektoren geschrieben werden.

// Ergibt TRUE wenn die Sektoren gelesen wurden

BOOL ReadSector(int iDrive, DWORD dwStart, DWORD dwCount,void *pBuffer)

{

HANDLE hDevice;

BOOL bResult;

DWORD dwBytes;









if(IsWinNt())

{

// WinNT Code

char cDeviceName[] = "\\\\.\\A:";

LONG lHigh;



// Creating a handle to iDrive a: using CreateFile () function ..

cDeviceName[4] += iDrive;



hDevice = CreateFile(cDeviceName,GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

if(hDevice == INVALID_HANDLE_VALUE)return NULL;



lHigh=dwStart>>(32-9);

dwStart*=512;

dwCount*=512;



// Setting the pointer to point to the start of the sector we want to read ..

if(SetFilePointer(hDevice, dwStart, &lHigh, FILE_BEGIN)==0xFFFFFFFF)

if(GetLastError())

{

CloseHandle(hDevice);

return FALSE;

}



bResult=ReadFile(hDevice,pBuffer,dwCount,&dwBytes,NULL);

if(!bResult || dwCount!=dwBytes)

{

CloseHandle(hDevice);

return FALSE;

}

}

else{

// Win95/98 Code

DIOC_REGISTERS sReg;

CONTROL_BLOCK sBlock;



hDevice = CreateFile ( "\\\\.\\vwin32",0,0,NULL,0,FILE_FLAG_DELETE_ON_CLOSE,NULL);



sBlock.dwStartingSector = dwStart;

sBlock.wNumberOfSectors = (WORD)dwCount;

sBlock.pBuffer = pBuffer;



//-----------------------------------------------------------

// SI contains read/write mode flags

// SI=0h for read and SI=1h for write

// CX must be equal to ffffh for

// int 21h's 7305h extention

// DS:BX -> base addr of the

// control block structure

// DL must contain the iDrive number

// (01h=A:, 02h=B: etc)

//-----------------------------------------------------------



sReg.reg_ESI = 0x00;

sReg.reg_ECX = -1;

sReg.reg_EBX = (DWORD)(&sBlock);

sReg.reg_EDX = iDrive+1;

sReg.reg_EAX = 0x7305;





bResult = DeviceIoControl(hDevice,VWIN32_DIOC_DOS_DRIVEINFO,&(sReg),sizeof(sReg),&(sReg),sizeof(sReg),&dwBytes,0);

if(!bResult || (sReg.reg_Flags & 0x0001))

{

CloseHandle(hDevice);

return FALSE;

}

}



CloseHandle(hDevice);





return TRUE;

}



//*****************************************************************************

//*

//* WriteSector

//*

//*****************************************************************************

// Schreiben eines oder mehrere logischer Sektoren (immer 512 Byte groß !!!)

// iDrive : Ist die Nummer des Laufwerkes (0=A 1=B 2=C ...)

// dwStart : Nummer des ersten Sektors

// dwCount : Ist die Anzahl der Sektoren (1 bis 0xFFFF)

// pBuffer : Zeiger auf den Puffer in dem die Sektoren Daten stehen werden.

// Ergibt TRUE wenn die Sektoren geschrieben wurden

BOOL WriteSector(int iDrive, DWORD dwStart, DWORD dwCount,void *pBuffer)

{

HANDLE hDevice;

BOOL bResult;

DWORD dwBytes;





if(IsWinNt())

{

// WinNT Code

char cDeviceName[] = "\\\\.\\A:";

LONG lHigh;



// Creating a handle to iDrive a: using CreateFile () function ..

cDeviceName[4] += iDrive;



hDevice = CreateFile(cDeviceName,GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

if(hDevice == INVALID_HANDLE_VALUE)return NULL;



lHigh=dwStart>>(32-9);

dwStart*=512;

dwCount*=512;



// Setting the pointer to point to the start of the sector we want to read ..

if(SetFilePointer(hDevice, dwStart, &lHigh, FILE_BEGIN)==0xFFFFFFFF)

if(GetLastError())

{

CloseHandle(hDevice);

return FALSE;

}



bResult=WriteFile(hDevice,pBuffer,dwCount,&dwBytes,NULL);

if(!bResult || dwCount!=dwBytes)

{

CloseHandle(hDevice);

return FALSE;

}

}

else{

// Win95/98 Code

DIOC_REGISTERS sReg;

CONTROL_BLOCK sBlock;



hDevice = CreateFile ( "\\\\.\\vwin32",0,0,NULL,0,FILE_FLAG_DELETE_ON_CLOSE,NULL);



sBlock.dwStartingSector = dwStart;

sBlock.wNumberOfSectors = (WORD)dwCount;

sBlock.pBuffer = pBuffer;



//-----------------------------------------------------------

// SI contains read/write mode flags

// SI=0h for read and SI=1h for write

// CX must be equal to ffffh for

// int 21h's 7305h extention

// DS:BX -> base addr of the

// control block structure

// DL must contain the iDrive number

// (01h=A:, 02h=B: etc)

//-----------------------------------------------------------



memset(&sReg,0,sizeof(sReg));



sReg.reg_ESI = 0x01;

sReg.reg_ECX = -1;

sReg.reg_EBX = (DWORD)(&sBlock);

sReg.reg_EDX = iDrive+1;

sReg.reg_EAX = 0x7305;





bResult = DeviceIoControl(hDevice,VWIN32_DIOC_DOS_DRIVEINFO,&(sReg),sizeof(sReg),&(sReg),sizeof(sReg),&dwBytes,0);

if(!bResult || (sReg.reg_Flags & 0x0001))

{

CloseHandle(hDevice);

return FALSE;

}

}



CloseHandle(hDevice);





return TRUE;

}



//*****************************************************************************

//*

//* FormatDisk

//*

//*****************************************************************************

// Formatiert eine Diskette

// iDrive : Lafwerksnummer (0=A 1=B 2=C ...)

// iType : Art der Diskkete (z.B. F3_1Pt44_512 F3_720_512)

// Ergibt TRUE wenn die Diskette formatiert wurde.

BOOL FormatDisk(int iDrive,MEDIA_TYPE iType)

{

HANDLE hDevice;

BOOL bResult;

DIOC_REGISTERS sReg;

DWORD dwBytes;

char cDeviceName[] = "\\\\.\\A:";







if(iType=sizeof(iSectors)/sizeof(iSectors[0]))return FALSE;

if(!iSectors[iType])return FALSE;



cDeviceName[4] += iDrive;

if(GetDriveType(cDeviceName+4)!=DRIVE_REMOVABLE)return FALSE;





if(IsWinNt())

{

// WinNT code

HANDLE hDevice;

DWORD dwBytes;

FORMAT_PARAMETERS sFormat;



// Creating a handle to iDrive a: using CreateFile () function ..



hDevice = CreateFile(cDeviceName,GENERIC_ALL,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

if( hDevice == INVALID_HANDLE_VALUE)return FALSE;





sFormat.StartCylinderNumber = 0;

sFormat.StartHeadNumber = 0;

sFormat.EndCylinderNumber = iTracks[iType]-1;

sFormat.EndHeadNumber = 1;

sFormat.MediaType = iType;



if(!DeviceIoControl(hDevice,IOCTL_DISK_FORMAT_TRACKS,&sFormat,sizeof(FORMAT_PARAMETERS),NULL,0,&dwBytes,NULL))

{

CloseHandle(hDevice);

return FALSE;

}

}

else{

FVBLOCK sBlock;

int iHead,iTrack;





hDevice = CreateFile ( "\\\\.\\vwin32",0,0,NULL,0,FILE_FLAG_DELETE_ON_CLOSE,NULL);



memset(&sReg,0,sizeof(sReg));



for(iHead=0;iHead<2;iHead++)

for(iTrack=0;iTrack
{

sBlock.fvSpecFunc = 0; // special functions = 0

sBlock.fvHead = iHead; // head to format/verify

sBlock.fvCylinder = iTrack; // cylinder to format/verify

sBlock.fvTracks = 0; // tracks to format/verify



sReg.reg_EAX = 0x440D; // IOCTL for block devices

sReg.reg_EBX = iDrive+1; // zero-based iDrive ID

sReg.reg_ECX = 0x0842; // Get Media ID command 0x??42

sReg.reg_EDX = (DWORD)&sBlock; // receives media ID info

sReg.reg_Flags=0x0001;



// 6 == VWIN32_DIOC_DOS_DRIVEINFO

bResult = DeviceIoControl(hDevice,VWIN32_DIOC_DOS_IOCTL,&sReg,sizeof(sReg),&(sReg),sizeof(sReg),&dwBytes,0);



if(!bResult || (sReg.reg_Flags & 0x0001))

{

int x=0;

CloseHandle(hDevice);

return FALSE;

}

}

}



CloseHandle(hDevice);



return TRUE;

}





//*****************************************************************************

//*

//* GetSectorCount

//*

//*****************************************************************************

// Ergibt die Größe eines Laufwerks in 512 Byte Sektoren

// ist pSize ungleich Null so wird dort die wirkliche Sektorgröße gespeichert

// Ergibt 0 bei einem Fehler

DWORD GetSectorCount(int iDrive, DWORD *pSize)

{

ULARGE_INTEGER uDummy,uTotal;

HANDLE hDevice;

DWORD dwBytes,dwReturn;

char cDeviceName[] = "\\\\.\\A:";







if(pSize)*pSize=0;

cDeviceName[4] += iDrive;



if(IsWinNt())

{

// WinNT Code

PARTITION_INFORMATION sInfo;

DISK_GEOMETRY sDisk;

BOOL bDisk;





hDevice = CreateFile(cDeviceName,GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

if(hDevice == INVALID_HANDLE_VALUE)return NULL;



bDisk=DeviceIoControl(hDevice,IOCTL_DISK_GET_DRIVE_GEOMETRY,NULL,0,&sDisk,sizeof(sDisk),&dwBytes,NULL);



if(DeviceIoControl(hDevice,IOCTL_DISK_GET_PARTITION_INFO,NULL,0,&sInfo,sizeof(sInfo),&dwBytes,NULL))

{

if(pSize)*pSize=(bDisk)? sDisk.BytesPerSector:512;

return (DWORD)(sInfo.PartitionLength.QuadPart>>9);

}



if(!bDisk)

{

bDisk=DeviceIoControl(hDevice,IOCTL_CDROM_GET_DRIVE_GEOMETRY,NULL,0,&sDisk,sizeof(sDisk),&dwBytes, NULL);

}



if(bDisk)

{

if(pSize)*pSize=sDisk.BytesPerSector;



if(GetDiskFreeSpaceEx(cDeviceName+4,&uDummy,&uTotal,NULL))

{

return (DWORD)(uTotal.QuadPart>>9);

}



return (DWORD)((sDisk.Cylinders.QuadPart*

(__int64)sDisk.TracksPerCylinder*

(__int64)sDisk.SectorsPerTrack*

(__int64)sDisk.BytesPerSector)>>9);

}



if(GetDiskFreeSpaceEx(cDeviceName+4,&uDummy,&uTotal,NULL))

{

if(pSize)*pSize=512;

return (DWORD)(uTotal.QuadPart>>9);

}



if(pSize)*pSize=0;

CloseHandle(hDevice);

return FALSE;

}

else{

// Win95/98 Code

BYTE bBoot[512];



if(GetDriveType(cDeviceName+4)==DRIVE_CDROM)

{

if(!GetDiskFreeSpaceEx(cDeviceName+4,&uDummy,&uTotal,NULL))return 0;

if(pSize)*pSize=2048;

return (DWORD)(uTotal.QuadPart>>9);

}



if(!ReadSector(iDrive,0,1,bBoot))return 0; // Read Bootsector



if(bBoot[510]!=0x55)return 0;

if(bBoot[511]!=0xAA)return 0;



if(!memcmp(bBoot+0x52,"FAT32 ",8))

{

dwReturn=*(DWORD*)(bBoot+0x20);

}

else if(!memcmp(bBoot+0x36,"FAT16 ",8))

{

dwReturn=*(WORD*)(bBoot+0x13);

if(dwReturn==0)dwReturn=*(DWORD*)(bBoot+0x20);

}

else if(!memcmp(bBoot+0x36,"FAT12 ",8))

{

dwReturn=*(WORD*)(bBoot+0x13);

if(dwReturn==0)dwReturn=*(DWORD*)(bBoot+0x20);

}

else{

return 0;

}



if(dwReturn==0)return 0;

if(pSize)*pSize=*(WORD*)(bBoot+0x0B);



return dwReturn;

}



CloseHandle(hDevice);





return TRUE;

}




Schöne Grüße Anton 

Meine Webseite



geschrieben von

Login

E-Mail:
  

Passwort:
  

Beitrag anfügen

Symbol:
 
 
 
 
 
 
 
 
 
 
 
 
 

Überschrift: