📄 diskio.c
字号:
}
*pT = 0; // Terminate the string
} // FormatSBCS
//#endif // DEBUG
//
// GetDiskInfo - return disk info in response to DISK_IOCTL_GETINFO
//
DWORD
GetDiskInfo(
PDISK pDisk,
PDISK_INFO pInfo
)
{
// RETAILMSG(1, (TEXT("Enter GetDsikInfo\r\n")));
*pInfo = pDisk->d_DiskInfo;
pInfo->di_flags |= DISK_INFO_FLAG_PAGEABLE;
pInfo->di_flags &= ~DISK_INFO_FLAG_UNFORMATTED;
// RETAILMSG(1, (TEXT("Enter GetDsikInfo--pInfo->di_flags = %x\r\n"),pInfo->di_flags ));
return ERROR_SUCCESS;
} // GetDiskInfo
//
// SetDiskInfo - store disk info in response to DISK_IOCTL_SETINFO
//
DWORD
SetDiskInfo(
PDISK pDisk,
PDISK_INFO pInfo
)
{
DWORD sectsize;
// RETAILMSG(1, (TEXT("Enter SetDiskInfo\r\n")));
sectsize = pDisk->d_DiskInfo.di_bytes_per_sect;
pDisk->d_DiskInfo = *pInfo;
//
// Allocate the aligned buffer when we find out the sector size
//
if (pInfo->di_bytes_per_sect != sectsize) {
if (pDisk->d_AlignBuf != NULL) {
LocalFree(pDisk->d_AlignBuf);
pDisk->d_AlignBuf = NULL;
}
}
if (pDisk->d_AlignBuf == NULL) {
pDisk->d_AlignBuf = (PUSHORT)LocalAlloc(LPTR,
pInfo->di_bytes_per_sect);
}
return ERROR_SUCCESS;
} // SetDiskInfo
#ifdef DEBUG
void DumpRegKey( DWORD dwZone, PTSTR szKey, HKEY hKey)
{
DWORD dwIndex=0;
WCHAR szValueName[MAX_PATH];
DWORD dwValueNameSize = MAX_PATH;
BYTE pValueData[256];
DWORD dwType;
DWORD dwValueDataSize = sizeof(pValueData);
DEBUGMSG( dwZone, (TEXT("Dumping registry for key %s \r\n"), szKey));
while(ERROR_SUCCESS == RegEnumValue( hKey, dwIndex, szValueName, &dwValueNameSize, NULL, &dwType, pValueData, &dwValueDataSize)) {
if (REG_SZ == dwType) {
DEBUGMSG( dwZone, (TEXT("\t\t%s = %s\r\n"), szValueName, (LPWSTR)pValueData));
} else
if (REG_DWORD == dwType) {
DEBUGMSG( dwZone, (TEXT("\t\t%s = %08X\r\n"), szValueName, *(PDWORD)pValueData));
}
dwIndex++;
dwValueDataSize = sizeof(pValueData);
dwValueNameSize = MAX_PATH;
}
}
#define DUMPREGKEY(dwZone, szKey, hKey) DumpRegKey(dwZone, szKey, hKey)
#else
#define DUMPREGKEY(dwZone, szKey, hKey)
#endif
//
// Function to open the driver key specified by the active key
//
// The caller is responsible for closing the returned HKEY
//
HKEY
OpenDriverKey(
LPTSTR ActiveKey
)
{
TCHAR DevKey[256];
HKEY hDevKey;
HKEY hActive;
DWORD ValType;
DWORD ValLen;
DWORD status;
//
// Get the device key from active device registry key
//
status = RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
ActiveKey,
0,
0,
&hActive);
if (status) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR,
(TEXT("MMCDISK:OpenDriverKey RegOpenKeyEx(HLM\\%s) returned %d!!!\r\n"),
ActiveKey, status));
return NULL;
}
#ifdef DEBUG
// DUMPREGKEY( ZONE_INIT, ActiveKey, hActive);
#endif
hDevKey = NULL;
ValLen = sizeof(DevKey);
status = RegQueryValueEx(
hActive,
DEVLOAD_DEVKEY_VALNAME,
NULL,
&ValType,
(PUCHAR)DevKey,
&ValLen);
if (status != ERROR_SUCCESS) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR,
(TEXT("MMCDISK:OpenDriverKey - RegQueryValueEx(%s) returned %d\r\n"),
DEVLOAD_DEVKEY_VALNAME, status));
goto odk_fail;
}
//
// Get the geometry values from the device key
//
status = RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
DevKey,
0,
0,
&hDevKey);
if (status) {
hDevKey = NULL;
DEBUGMSG(ZONE_INIT|ZONE_ERROR,
(TEXT("MMCDISK:OpenDriverKey RegOpenKeyEx(HLM\\%s) returned %d!!!\r\n"),
DevKey, status));
}
// DUMPREGKEY( ZONE_INIT, DevKey, hDevKey);
odk_fail:
RegCloseKey(hActive);
return hDevKey;
} // OpenDriverKey
//
// Function to retrieve the folder name value from the driver key. The folder name is
// used by FATFS to name this disk volume.
//
BOOL
GetFolderName(
PDISK pDisk,
LPWSTR FolderName,
DWORD cBytes,
DWORD * pcBytes
)
{
HKEY DriverKey;
DWORD ValType;
DWORD status;
DriverKey = OpenDriverKey(pDisk->d_ActivePath);
if (DriverKey) {
*pcBytes = cBytes;
status = RegQueryValueEx(
DriverKey,
TEXT("Folder"),
NULL,
&ValType,
(PUCHAR)FolderName,
pcBytes);
if (status != ERROR_SUCCESS) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR,
(TEXT("MMCDISK:GetFolderName - RegQueryValueEx(Folder) returned %d\r\n"),
status));
*pcBytes = 0;
} else {
DEBUGMSG(ZONE_INIT|ZONE_ERROR,
(TEXT("MMCDISK:GetFolderName - FolderName = %s, length = %d\r\n"),
FolderName, *pcBytes));
*pcBytes += sizeof(WCHAR); // account for terminating 0.
}
RegCloseKey(DriverKey);
if (status || (*pcBytes == 0)) {
return FALSE;
}
return TRUE;
}
return FALSE;
} // GetFolderName
BOOL
GetStorageID(
PDISK pDisk,
PSTORAGE_IDENTIFICATION psid,
DWORD cBytes,
DWORD * pcBytes
)
{
DRV_GEOMETRY_DESC * pmy_geom_desc;
DRV_GEOMETRY_DESC my_geom_desc;
PCHAR pDstOffset;
WORD str_length;
// RETAILMSG(1, (TEXT("Enter GetStorageID \r\n")));
if (cBytes < sizeof(*psid)) {
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
__try {
psid->dwSize = sizeof(*psid);
psid->dwFlags = 0; // can be or of {MANUFACTUREID,SERIALNUM}_INVALID
psid->dwManufactureIDOffset = 0;
pDstOffset = (PCHAR)(psid + 1);
psid->dwSerialNumOffset = pDstOffset - (PCHAR) psid;
} __except (EXCEPTION_EXECUTE_HANDLER) {
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
pmy_geom_desc=&my_geom_desc;
#if (USE_SPI || USE_SPI_EMULATION)
if (spi_read_serial(0, pmy_geom_desc) == NO) { //go get the serial number
return FALSE;
}
#else
if (mmc_read_serial(0, pmy_geom_desc) == NO) { //go get the serial number
return FALSE;
}
#endif
psid->dwFlags = MANUFACTUREID_INVALID;
str_length=strlen(&my_geom_desc.serialNum[0]);
memcpy(pDstOffset, &my_geom_desc.serialNum[0], str_length + 1);
pDstOffset += (str_length + 1);
psid->dwSize = pDstOffset - (PCHAR)psid; // required size
*pcBytes = min(psid->dwSize, cBytes); // bytes written
return (GetLastError() == ERROR_SUCCESS);
}
//
// InitDisk
//
DWORD InitDisk(
PDISK pDisk
)
{
#if (USE_MEMMODE)
ULONG virt_address;
#endif
PDEVICE_CONTROLLER pc;
pc = &controller_s[0];
// RETAILMSG(1, (TEXT("InitDisk() enter \r\n")));
#if (USE_MEMMODE)
virtreg=NULL;
#endif
#if (USE_MMC)
// if (scan_PCI_config() == FALSE) { //init PCI
// return 1;
// }
#endif
//If using a memory mapped interface, must map the desired memory base
//address to a allocated area in virtual memory.
#if (USE_MEMMODE)
virtreg = VirtualAlloc(
0,
4096,
MEM_RESERVE,
PAGE_NOACCESS);
if (virtreg == NULL) {
DEBUGMSG(ZONE_WARNING,
(TEXT("MMCDISK:mmcinit VirtualAlloc failed %d\r\n"), GetLastError()));
goto init_dfail;
}
#if (USE_MMC)
virt_address=myAdapters[0].portIO | 0x80000000; //must have high bit set
#else
virt_address=SPI_BASE_ADDR | 0x80000000; //must have high bit set
#endif
if (!VirtualCopy(
virtreg,
(PVOID)virt_address,
4096,
PAGE_READWRITE|PAGE_NOCACHE)) {
DEBUGMSG(ZONE_WARNING,
(TEXT("MMCDISK:mmcinit VirtualCopy failed %d\r\n"), GetLastError()));
goto init_dfail;
}
#if (USE_MMC)
myAdapters[0].portIO = (UINT32) virtreg;
#else
pc->register_file_address = (unsigned char * ) virtreg;
#endif
#endif
#if (USE_SPI || USE_SPI_EMULATION)
if (spi_init() == FALSE) { //init MMC
return 1;
}
if (spi_drive_open(0) == FALSE) {
return 1;
}
#else
// RETAILMSG(1, (TEXT("Before Entering mmc_init()\r\n")));
if (mmc_init() == FALSE) { //init MMC
// RETAILMSG(1, (TEXT("mmc_init() return FALSE\r\n")));
return 1;
}
if (mmc_drive_open(0) == FALSE) {
// RETAILMSG(1, (TEXT("mmc_drive_open() return FALSE\r\n")));
return 1;
}
// if (!setWideBus(0, 4)) /* Device 0, 4-bit bus */
// return 1;
// RETAILMSG(1, (TEXT("InitDisk return TRUE\r\n")));
#endif
pDisk->d_fLBAMode = TRUE;
pDisk->d_DiskInfo.di_bytes_per_sect = BYTES_PER_SECTOR; // Start with 512, then go with SetInfo changes
pDisk->d_DiskInfo.di_cylinders = pc->drive[0].num_cylinders;
pDisk->d_DiskInfo.di_heads = pc->drive[0].num_heads;
pDisk->d_DiskInfo.di_sectors = pc->drive[0].sec_p_track;
pDisk->d_DiskInfo.di_total_sectors = pc->drive[0].total_lba;
pDisk->d_DiskInfo.di_flags = DISK_INFO_FLAG_MBR;
return 0;
#if (USE_MEMMODE)
init_dfail:
/*Caller will make call to CloseDisk when we return. That routine will
perform the deallocation of virtreg if necessary */
// if (virtreg) {
// VirtualFree(virtreg, 0, MEM_RELEASE);
// }
return 1;
#endif
} // Initdisk
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -