📄 diskio.c
字号:
goto ddi_leave;
}
DEBUGMSG(ZONE_IO, (TEXT("MMCDISK:DoDiskIO - working on request @ 0x%x from sector %d to %d\r\n"), pSgr, pSgr->sr_start, pSgr->sr_start + pSgr->sr_num_sec - 1));
if ((Opcode == DISK_IOCTL_READ) || (Opcode == IOCTL_DISK_READ)){
status = MMCREAD(pDisk, pSgr);
} else {
status = MMCWRITE(pDisk, pSgr);
}
#ifdef DEBUG
if (status != ERROR_SUCCESS) {
DWORD i;
DEBUGMSG( ZONE_ERROR, (L"SDMMC Failed with error code = %08X start sector=%ld sgcount=%ld\r\n", status, pSgr->sr_start, pSgr->sr_num_sg));
for(i = 0; i < pSgr->sr_num_sg;i++) {
DEBUGMSG( ZONE_ERROR,(L" Buffer = %08X Size=%ld\r\n", pSgr->sr_sglist[i].sb_buf, pSgr->sr_sglist[i].sb_len));
}
}
#endif
ddi_leave:
LeaveCriticalSection(&(pDisk->d_DiskCardCrit));
ddi_exit:
DEBUGMSG(ZONE_IO, (TEXT("MMCDISK:DoDiskIO done\r\n")));
pSgr->sr_status = status;
return status;
} // DoDiskIO
//#ifdef DEBUG
VOID
FormatSBCS(
PUCHAR p,
LPTSTR pT,
DWORD len
)
{
while (len) {
if ((*p > 0x20) && (*p < 0x7F)) {
*pT = (TCHAR)*p;
} else {
*pT = (TCHAR)'.';
}
pT++;
p++;
len--;
}
*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
)
{
*pInfo = pDisk->d_DiskInfo;
pInfo->di_flags |= DISK_INFO_FLAG_PAGEABLE;
pInfo->di_flags &= ~DISK_INFO_FLAG_UNFORMATTED;
return ERROR_SUCCESS;
} // GetDiskInfo
//
// SetDiskInfo - store disk info in response to DISK_IOCTL_SETINFO
//
DWORD
SetDiskInfo(
PDISK pDisk,
PDISK_INFO pInfo
)
{
DWORD sectsize;
sectsize = pDisk->d_DiskInfo.di_bytes_per_sect;
pDisk->d_DiskInfo = *pInfo;
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)) {
SetLastError( ERROR_NOT_FOUND);
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;
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]);
if ((sizeof(*psid)+str_length+1) > cBytes) {
SetLastError(ERROR_INSUFFICIENT_BUFFER);
} else {
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];
#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
if (mmc_init() == FALSE) { //init MMC
return 1;
}
if (mmc_drive_open(0) == FALSE) {
return 1;
}
// if (!setWideBus(0, 4)) /* Device 0, 4-bit bus */
// return 1;
#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 + -