📄 drive.c
字号:
//**********************************************************************
//
// Filename: drive.c
//
// Description:
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Use of this source code is subject to the terms of the Cirrus end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to
// use this source code. For a copy of the EULA, please see the
// EULA.RTF on your install media.
//
// Copyright(c) Cirrus Logic Corporation 2005, All Rights Reserved
//
//**********************************************************************
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the EULA.RTF on your
// install media.
//
//------------------------------------------------------------------------------
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
//------------------------------------------------------------------------------
#include <windows.h>
#include <halether.h>
#include "eboot.h"
//#include <bootarg.h>
#include "fat.h"
//#include "bldr.h"
#define SERPRINT EdbgOutputDebugString
#define TO_UPPER(a) (a >= 0x61 ? a - 0x20 : a)
#define MIN(a, b) (a < b ? a : b)
// This data supplements the BPB information - used to save time.
extern t_FAT_PARMS g_FATParms;
extern BYTE BIOS_ReadSectors(UCHAR Drive, ULONG dwStartSector, UCHAR nSectors, PUCHAR pBuffer);
//BYTE BIOS_ResetDisk(UCHAR Drive);
int ide_reset(void);
//
// BIOS INT13H error number-to-text mapping.
//
struct
{
BYTE nErrVal;
LPCSTR pErrText;
} DiskErrs[] =
{ { 0x01, "Invalid function in AH or invalid parameter" },
{ 0x02, "Address mark not found" },
{ 0x03, "Disk write-protected" },
{ 0x04, "Sector not found/read error" },
{ 0x05, "Reset failed (hard disk)" },
{ 0x06, "Disk changed (floppy)" },
{ 0x07, "Drive parameter activity failed (hard disk)" },
{ 0x08, "DMA overrun" },
{ 0x09, "Data boundary error (attempted DMA across a 64K boundary)" },
{ 0x0A, "Bad sector detected (hard disk)" },
{ 0x0B, "Bad track detected (hard disk)" },
{ 0x0C, "Unsupported track or invalid media" },
{ 0x0D, "Invalid number of sectors on format (PS/2 hard disk)" },
{ 0x0E, "Control data address mark detected (hard disk)" },
{ 0x0F, "DMA arbitration level out of range (hard disk)" },
{ 0x10, "Uncorrectable CRC or ECC error on read" },
{ 0x11, "Data ECC corrected (hard disk)" },
{ 0x20, "Controller failure" },
{ 0x31, "No media in drive (IBM/MS INT 13 extensions)" },
{ 0x32, "Incorrect drive type stored in CMOS" },
{ 0x40, "Seek failed" },
{ 0x80, "Timeout (not ready)" },
{ 0xAA, "Drive not ready (hard disk)" },
{ 0xB0, "Volume not locked in drive (INT 13 extensions)" },
{ 0xB1, "Volume locked in drive (INT 13 extensions)" },
{ 0xB2, "Volume not removable (INT 13 extensions)" },
{ 0xB3, "Volume in use (INT 13 extensions)" },
{ 0xB4, "Lock count exceeded (INT 13 extensions)" },
{ 0xB5, "Valid eject request failed (INT 13 extensions)" },
{ 0xB6, "Volume present but read protected (INT 13 extensions)" },
{ 0xBB, "Undefined error (hard disk)" },
{ 0xCC, "Write fault (hard disk)" },
{ 0xE0, "Status register error (hard disk)" },
{ 0xFF, "Sense operation failed (hard disk)" },
{ 0, "<unknown>" } };
//
// Convert the error value returned by the BIOS INT13h call into a text message.
//
LPCSTR GetErrorText(BYTE nErrVal)
{
BYTE nCount;
for (nCount = 0 ; DiskErrs[nCount].nErrVal ; nCount++)
{
if (DiskErrs[nCount].nErrVal == nErrVal)
break;
}
return(DiskErrs[nCount].pErrText);
}
//
// TODO TODO TODO figure out the new LBA addresses.
//
//
// Convert an LBA value to a physical CHS value (used by the BIOS).
//
ULONG LBA2PCHS(ULONG LBA,
PUSHORT pC,
PUCHAR pH,
PUCHAR pS)
{
USHORT Temp = 0;
#if !defined( NDEF_DWF_NEW_LBA_2_CHS )
USHORT sectorsPerCylinder;
#endif // ( NDEF_DWF_NEW_LBA_2_CHS )
if (pC == NULL || pH == NULL || pS == NULL)
return(-1);
// Do the math...
#if !defined( NDEF_DWF_NEW_LBA_2_CHS )
sectorsPerCylinder = g_FATParms.phy_heads * g_FATParms.phy_sectspertrk;
*pC = (USHORT)(LBA / sectorsPerCylinder);
Temp = (USHORT)(LBA % sectorsPerCylinder);
*pH = (UCHAR)(Temp / g_FATParms.phy_sectspertrk);
Temp = (USHORT)(Temp % g_FATParms.phy_sectspertrk);
*pS = (UCHAR)(Temp % g_FATParms.phy_sectspertrk) + 1;
#else // ( NDEF_DWF_NEW_LBA_2_CHS )
*pC = (USHORT)(LBA / (g_FATParms.NumHeads * g_FATParms.SectsPerTrack));
Temp = (USHORT)(LBA % (g_FATParms.NumHeads * g_FATParms.SectsPerTrack));
*pH = (UCHAR)(Temp / g_FATParms.SectsPerTrack);
*pS = (UCHAR)(Temp % g_FATParms.SectsPerTrack) + 1;
#endif // ( NDEF_DWF_NEW_LBA_2_CHS )
return(0);
}
//
// Read the specified number of sectors from the Drive/LBA combination specified.
//
BOOLEAN ReadSectors(UCHAR Drive, ULONG LBA, USHORT nSectors, PUCHAR pBuffer)
{
USHORT uMaxSector = 1;
USHORT uReadSector;
UCHAR Status = 0;
// Check arguments.
//
if (!pBuffer)
return(FALSE);
// SERPRINT("ReadSectors %d %d.\r\n", LBA, nSectors);
// Clear memory before read...
memset( pBuffer, 0, (nSectors * g_FATParms.BytesPerSect) );
// Call the BIOS to read a sector.
//
while(nSectors>0)
{
uReadSector = MIN(nSectors,uMaxSector);
if ((Status = BIOS_ReadSectors(Drive, LBA, (BYTE)uReadSector, pBuffer)) != 0)
{
// If the read failed, retry the operation and reset the controller each time...
//if (BIOS_ResetDisk(Drive))
if ( ide_reset() )
{
SERPRINT("ERROR: Unable to reset disk controller (Drive=0x%x).\r\n", Drive);
break;
}
}
nSectors -= uReadSector;
LBA += uReadSector;
pBuffer += uReadSector * g_FATParms.BytesPerSect;
}
if (Status)
{
// SERPRINT("ERROR: Unable to read %d sector(s) at CHS = 0x%x:0x%x:0x%x (Status=0x%x).\r\n", nSectors, c, h, s, Status);
SERPRINT("ERROR: Status 0x%x: \"%s\".\r\n", Status, GetErrorText(Status));
return(FALSE);
}
return(TRUE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -