📄 diskio.c
字号:
//
// 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 LICENSE.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.
Module Name:
diskio.c
Abstract:
WINCE driver for MultiMediaCard
Functions:
Notes:
--*/
#include <windows.h>
#include <types.h>
#include <tchar.h>
#include <excpt.h>
#include <devload.h>
#include <pkfuncs.h>
#include "atadisk.h"
#include "atapiapi.h"
extern PDEVICE_CONTROLLER g_pcPC;
//SDGLOBAL int scan_PCI_config(void);
//
// This module contains the functions:
// CloseDisk
// CheckMedia
// MMCREAD
// MMCWRITE
// DoDiskIO
// GetDiskInfo
// SetDiskInfo
// InitDisk
//
int IsCardInserted(PDISK pDisk);
HKEY OpenDriverKey(LPTSTR ActiveKey);
int GetFolderName(PDISK pDisk, LPWSTR FolderName, DWORD cBytes, DWORD * pcBytes);
int GetStorageID(PDISK pDisk, PSTORAGE_IDENTIFICATION pOutBuf, DWORD nOutBufSize, DWORD * pBytesReturned);
int mmc_read(INT16 driveno, ULONG sector, UCHAR * buffer, UCOUNT count);
int mmc_write(INT16 driveno, ULONG sector, UCHAR * buffer, UCOUNT count);
//
// CloseDisk - finish and free all outstanding requests and free other resources
// associated with the specified disk
//
VOID
CloseDisk(
PDISK pDisk
)
{
PDISK pd;
DEBUGMSG(ZONE_IO, (TEXT("MMCDISK:CloseDisk closing 0x%x\r\n"), pDisk));
//
// Remove it from the global list of disks
//
EnterCriticalSection(&v_DiskCrit);
if (pDisk == v_DiskList) {
v_DiskList = pDisk->d_next;
} else {
pd = v_DiskList;
while (pd->d_next != NULL) {
if (pd->d_next == pDisk) {
pd->d_next = pDisk->d_next;
break;
}
pd = pd->d_next;
}
}
LeaveCriticalSection(&v_DiskCrit);
DEBUGMSG(ZONE_IO, (TEXT("MMCDISK:CloseDisk - freeing resources\r\n")));
DeleteCriticalSection(&(pDisk->d_DiskCardCrit));
if (pDisk->d_ActivePath) {
LocalFree(pDisk->d_ActivePath);
}
LocalFree(pDisk);
#if USE_MEM_MODE
if (virtreg) {
VirtualFree(virtreg, 0, MEM_RELEASE);
}
#endif
DEBUGMSG(ZONE_IO, (TEXT("MMCDISK:CloseDisk done with 0x%x\r\n"), pDisk));
} // CloseDisk
//
// CheckMedia - Simply check to see if media is present
//
//
//
DWORD
CheckMedia(PDISK pDisk)
{
if (!IsCardInserted(pDisk)) {
DEBUGMSG(ZONE_WARNING|ZONE_ERROR|ZONE_IO,
(TEXT("MMCDISK:CheckMedia - Built-in CF Card no longer present!\r\n")));
if (pDisk->d_DiskCardState != STATE_DEAD) {
pDisk->d_DiskCardState = STATE_REMOVED;
}
return DISK_REMOVED_ERROR;
}
return ERROR_SUCCESS;
} // CheckMedia
//
// MMCREAD - fulfill one scatter/gather read request
//
#if 1
DWORD
ATAREAD(
PDISK pDisk,
PSG_REQ pSgr
)
{
DWORD num_sg;
DWORD bytes_this_sg;
PSG_BUF pSg;
PUSHORT pBuf16;
DWORD sectno;
DWORD endsect;
DWORD error;
PUCHAR pTempBuf;
DWORD dTotLength;
DWORD i;
num_sg = pSgr->sr_num_sg;
pSg = &(pSgr->sr_sglist[0]);
bytes_this_sg = pSg->sb_len;
pBuf16 = (PUSHORT) MapPtrToProcess((LPVOID)pSg->sb_buf, GetCallerProcess());
sectno = pSgr->sr_start;
endsect = sectno + pSgr->sr_num_sec;
if(endsect > pDisk->d_DiskInfo.di_total_sectors)
return(ERROR_SECTOR_NOT_FOUND);
error = ERROR_SUCCESS;
//
// This loop reads multiple sectors into multiple scatter/gather buffers.
// The scatter/gather buffers may be bigger or smaller or same size as a
// sector.
//
if (num_sg == 1)
{
try
{
error = CheckMedia(pDisk);
if ( error == ERROR_SUCCESS)
{
if (AtaDiskRead(sectno, (BYTE *)pBuf16, (UCOUNT)pSgr->sr_num_sec) == 0)
error = ERROR_READ_FAULT;
}
}
except (GetExceptionCode() == STATUS_ACCESS_VIOLATION ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
{
error = ERROR_READ_FAULT;
}
if (error != ERROR_SUCCESS)
{
return error;
}
}
else // if sg_num is greater than 1.
{
RETAILMSG(1,(TEXT("Scatter buffer is coming\r\n")));
for ( i = 0 ,dTotLength = 0; i < num_sg ; i++)
{
dTotLength += pSgr->sr_sglist[i].sb_len;
}
if ( dTotLength < (UCOUNT)pSgr->sr_num_sec * ATA_SECTORSIZE)
{
RETAILMSG(1,(TEXT("Buffer size is smaller than block size\r\n")));
return ERROR_GEN_FAILURE;
}
pTempBuf = (PUCHAR)(malloc((size_t)dTotLength));
if (!pTempBuf)
return ERROR_NOT_ENOUGH_MEMORY;
try
{
error = CheckMedia(pDisk);
if ( error == ERROR_SUCCESS)
{
if (AtaDiskRead(sectno, (BYTE *)pTempBuf, (UCOUNT)pSgr->sr_num_sec) == 0)
error = ERROR_READ_FAULT;
}
}
except (GetExceptionCode() == STATUS_ACCESS_VIOLATION ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
{
error = ERROR_READ_FAULT;
}
if (error != ERROR_SUCCESS)
{
return error;
}
for ( i = 0 ,dTotLength = 0; i < num_sg ; i++)
{
memcpy((PBYTE)pSgr->sr_sglist[i].sb_buf,(UINT32)pTempBuf+dTotLength,pSgr->sr_sglist[i].sb_len);
dTotLength += pSgr->sr_sglist[i].sb_len;
}
free(pTempBuf);
}
return 0;
}
//
// MMCWRITE
//
DWORD
ATAWRITE(
PDISK pDisk,
PSG_REQ pSgr
)
{
DWORD num_sg;
//DWORD bytes_this_int;
DWORD bytes_this_sg;
PSG_BUF pSg;
PUSHORT pBuf16;
DWORD sectno;
DWORD endsect;
DWORD error;
PUCHAR pTempBuf;
DWORD dTotLength;
DWORD i;
num_sg = pSgr->sr_num_sg;
pSg = &(pSgr->sr_sglist[0]);
bytes_this_sg = pSg->sb_len;
//bytes_this_int = pDisk->d_DiskInfo.di_bytes_per_sect;
pBuf16 = (PUSHORT) MapPtrToProcess((LPVOID)pSg->sb_buf, GetCallerProcess());
sectno = pSgr->sr_start;
endsect = sectno + pSgr->sr_num_sec;
if(endsect > pDisk->d_DiskInfo.di_total_sectors)
return(ERROR_SECTOR_NOT_FOUND);
error = ERROR_SUCCESS;
//
// This loop writes data from multiple scatter/gather buffers to multiple
// sectors.
//
if (num_sg == 1)
{
try
{
error = CheckMedia(pDisk);
if ( error == ERROR_SUCCESS)
{
if (AtaDiskWrite(sectno, (BYTE *)pBuf16, (UCOUNT)pSgr->sr_num_sec) == 0)
error = ERROR_WRITE_FAULT;
}
}
except (GetExceptionCode() == STATUS_ACCESS_VIOLATION ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
{
error = ERROR_WRITE_FAULT;
}
if (error != ERROR_SUCCESS)
{
return error;
}
}
else // if sg_num is greater than 1.
{
RETAILMSG(1,(TEXT("###############Scatter buffer is coming\r\n")));
for ( i = 0 ,dTotLength = 0; i < num_sg ; i++)
{
dTotLength += pSgr->sr_sglist[i].sb_len;
}
if ( dTotLength < (UCOUNT)pSgr->sr_num_sec * ATA_SECTORSIZE)
{
RETAILMSG(1,(TEXT("Buffer size is smaller than block size\r\n")));
return ERROR_GEN_FAILURE;
}
if (dTotLength > 0x100000)
RETAILMSG(1,(TEXT("####### pBuffer is big222 0x%d\n"),dTotLength));
pTempBuf = (PUCHAR)(malloc((size_t)dTotLength));
if (!pTempBuf)
return ERROR_NOT_ENOUGH_MEMORY;
for ( i = 0 ,dTotLength = 0; i < num_sg ; i++)
{
memcpy(pTempBuf+dTotLength,(UINT32)pSgr->sr_sglist[i].sb_buf, pSgr->sr_sglist[i].sb_len);
dTotLength += pSgr->sr_sglist[i].sb_len;
}
try
{
error = CheckMedia(pDisk);
if ( error == ERROR_SUCCESS)
{
if (AtaDiskWrite(sectno, (BYTE *)pTempBuf, (UCOUNT)pSgr->sr_num_sec) == 0)
error = ERROR_WRITE_FAULT;
}
}
except (GetExceptionCode() == STATUS_ACCESS_VIOLATION ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
{
error = ERROR_WRITE_FAULT;
}
if (error != ERROR_SUCCESS)
{
return error;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -