📄 atamain.cpp
字号:
"Atapi!GetDSKRegistryValueSet> Bad value(%d) for %s in DSK instance key; valid: {0, 1}\r\n"
), pDskReg->dwWriteCache, REG_VAL_DSK_WRITECACHE));
return FALSE;
}
// fetch look-ahead boolean
fRet = AtaGetRegistryValue(hDSKInstanceKey, REG_VAL_DSK_LOOKAHEAD, &pDskReg->dwLookAhead);
if (!fRet) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR, (_T(
"Atapi!GetDSKRegistryValueSet> Failed to read %s from DSK instance key\r\n"
), REG_VAL_DSK_LOOKAHEAD));
return FALSE;
}
if (pDskReg->dwLookAhead >= 2) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR, (_T(
"Atapi!GetDSKRegistryValueSet> Bad value(%d) for %s in DSK instance key; valid: {0, 1}\r\n"
), pDskReg->dwLookAhead, REG_VAL_DSK_LOOKAHEAD));
return FALSE;
}
// fetch transfer mode
fRet = AtaGetRegistryValue(hDSKInstanceKey, REG_VAL_DSK_TRANSFERMODE, &pDskReg->dwTransferMode);
if (!fRet) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR, (_T(
"Atapi!GetDSKRegistryValueSet> Failed to read %s from DSK instance key\r\n"
), REG_VAL_DSK_TRANSFERMODE));
return FALSE;
}
return TRUE;
}
// This function reads the I/O window data from the IDE instance key and builds
// the I/O ports for the primary and secondary IDE controller channels
BOOL
GetIoPort(
HKEY hDevKey,
PTSTR szDevKey,
CIDEBUS *pBus
)
{
BOOL fRet = FALSE;
DEBUGCHK(pBus);
DEBUGCHK(pBus->m_pPrimaryPort);
// TODO: What if the IDE/ATA controller only supports a single device on
// TODO: (con't) a single channel and bus mastering?
// save the base virtual addresss of the device control (RegBase)
// and alternate status (RegAlt) I/O windows
pBus->m_pPrimaryPort->m_dwRegBase = (DWORD)MapAddress(ATAPI_REGBASE, ATAPI_REGSIZE);
pBus->m_pPrimaryPort->m_dwRegAlt = pBus->m_pPrimaryPort->m_dwRegBase;
pBus->m_pPrimaryPort->m_dwAtapiControl = pBus->m_pPrimaryPort->m_dwRegBase;
pBus->m_pPrimaryPort->m_dwAtapiBuffer = (DWORD)MapAddress(ATAPI_BUFFER_BASE, ATAPI_BUFFER_SIZE);
pBus->m_pPrimaryPort->m_fInitialized = TRUE;
fRet = TRUE;
return fRet;
}
// IDE/ATA and ATA/ATAPI device stream interface
/*++
DSK_Init
This function is called as a result of IDE_Init calling ActivateDevice on
HKLM\Drivers\@BUS\@IDEAdapter\DeviceX, to initialize a master or slave
device on a particular IDE/ATA channel of a particular IDE/ATA controller.
That is, an "IDE" driver is a bus driver for devices to one its IDE/ATA
controller's channels.
This function is responsible for creating a CDisk instance to associate
with a device. This function reads the "Object" value from its instance
key to determine which CDisk (sub)type to instantiate and calls Init on the
CDisk instance to initialize the device. If the device is not present, then
Init will fail. The "Object" value maps to a function that creates an
instance of the target CDisk (sub)type.
Note that this driver model is convoluted. A CDisk (sub)type instance
corresponds to both an IDE/ATA controller and an ATA/ATAPI device.
Parameters:
dwContext - pointer to string containing the registry path to the active key
of the associated device; the active key contains a key to the device's instance
key, which stores all of the device's configuration information
Return:
On success, return handle to device (to identify device); this handle is
passed to all subsequent DSK_Xxx calls. Otherwise, return null.
--*/
#define DSKINIT_UNDO_CLS_KEY_ACTIVE 0x1
#define DSKINIT_UNDO_CLS_KEY_DEVICE 0x2
EXTERN_C
DWORD
DSK_Init(
DWORD dwContext
)
{
DWORD dwUndo = 0; // undo bitset
PTSTR szActiveKey = (PTSTR)dwContext; // name of device's active key
HKEY hActiveKey; // handle to device's active key
PTSTR szDevKey = NULL; // name of device's instance key
HKEY hDevKey; // handle to device's instance key
POBJECTFUNCTION pObject = NULL; // pointer to spawn function
CPort *pPort = NULL; // port
DWORD dwDeviceId = 0; // device ID; 0 => master, 1 => slave
CDisk *pDisk = NULL; // return
// guard global data; i.e., g_pDiskRoot
EnterCriticalSection(&g_csMain);
// open device's active key
if ((ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, szActiveKey, 0, 0, &hActiveKey))) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR, (_T(
"Atapi!DSK_Init> Failed to open device's active key(%s)\r\n"
), szActiveKey));
goto exit;
}
dwUndo |= DSKINIT_UNDO_CLS_KEY_ACTIVE;
DUMPREGKEY(ZONE_INIT, szActiveKey, hActiveKey);
// read name of and open device's instance key from device's active key
if (!(hDevKey = AtaLoadRegKey(hActiveKey, &szDevKey))) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR, (_T(
"Atapi!DSK_Init> Failed to fetch/open device's instance key from device's active key(%s)\r\n"
), szActiveKey));
goto exit;
}
dwUndo |= DSKINIT_UNDO_CLS_KEY_DEVICE;
DUMPREGKEY(ZONE_INIT, szDevKey, hDevKey);
// fetch heap address of port instance from device's instance key
if (!AtaGetRegistryValue(hDevKey, REG_VALUE_PORT, (PDWORD)&pPort)) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR, (_T(
"Atapi!DSK_Init> Failed to read address of port instance from device's instance key(%s)\r\n"
), szDevKey));
goto exit;
}
// fetch device ID from device's instance key; this informs the CDisk
// instance as to which device (i.e., master/slave) it is
if (!AtaGetRegistryValue(hDevKey, REG_VAL_DSK_DEVICEID, &dwDeviceId)) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR, (_T(
"Atapi!DSK_Init> Failed to read device ID device's instance key(%s)\r\n"
), szDevKey));
goto exit;
}
// resolve address of spawn function
pObject = (POBJECTFUNCTION)GetProcAddress(g_hInstance, pPort->m_pController->m_pIdeReg->pszSpawnFunction);
if (!pObject) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR, (_T(
"Atapi!DSK_Init> Failed to resolve address of device's spawn function(%s)\r\n"
), pPort->m_pController->m_pIdeReg->pszSpawnFunction));
goto exit;
}
// instantiate CDisk object
pDisk = pObject(hDevKey);
// if successful, write the name of the device's active and instance keys to
// its CDisk instance, and add the CDisk instance to the IDE/ATA bus driver's
// list of active disk devices
if (pDisk) {
// this information is used for ATA/ATAPI power management
pDisk->SetActiveKey(szActiveKey);
pDisk->SetDeviceKey(szDevKey);
// inform the CDisk instance as to which device it is
pDisk->m_pPort = pPort;
pDisk->m_dwDeviceId = dwDeviceId;
pDisk->m_dwDevice = dwDeviceId;
// configure register block
pDisk->ConfigureRegisterBlock(pPort->m_pController->m_pIdeReg->dwRegisterStride);
// initialize device
if (!pDisk->Init(hActiveKey)) {
delete pDisk;
pDisk = NULL;
goto exit;
}
// add CDisk instance to IDE/ATA controller's list of active devices
pDisk->m_pNextDisk = g_pDiskRoot;
g_pDiskRoot = pDisk;
DEBUGMSG(ZONE_INIT, (_T(
"Atapi!DSK_Init> Initialized %s %s on %s\r\n"
), ((pPort == pPort->m_pController->m_pPrimaryPort) ? (_T("PRIMARY")) : (_T("SECONDARY"))),
((dwDeviceId == 0) ? (_T("MASTER")) : (_T("SLAVE"))),
szDevKey
));
}
exit:;
// clean up
if (NULL == pDisk) {
if (dwUndo & DSKINIT_UNDO_CLS_KEY_ACTIVE) {
RegCloseKey(hActiveKey);
}
if (dwUndo & DSKINIT_UNDO_CLS_KEY_DEVICE) {
RegCloseKey(hDevKey);
}
// pPort is deleted in IDE_Deinit
}
if (szDevKey) {
LocalFree(szDevKey);
}
LeaveCriticalSection(&g_csMain);
return (DWORD)pDisk;
}
/*++
DSK_Deinit
This function deallocates the associated CDisk instance.
Parameters:
dwHandle - pointer to associated CDisk instance (initially returned by
DSK_Init)
Return:
This function always succeeds.
--*/
EXTERN_C
BOOL
DSK_Deinit(
DWORD dwHandle
)
{
CDisk *pDiskPrev = NULL;
CDisk *pDiskCur = g_pDiskRoot;
EnterCriticalSection(&g_csMain);
// find the CDisk instance in global CDisk list
while (pDiskCur) {
if (pDiskCur == (CDisk *)dwHandle) {
break;
}
pDiskPrev = pDiskCur;
pDiskCur = pDiskCur->m_pNextDisk;
}
// remove CDisk instance from global CDisk list
if (pDiskCur) {
if (pDiskPrev) {
pDiskPrev = pDiskCur->m_pNextDisk;
}
else {
g_pDiskRoot = pDiskCur->m_pNextDisk;
}
delete pDiskCur;
}
LeaveCriticalSection(&g_csMain);
return TRUE;
}
/*++
DSK_Open
This function opens a CDisk instance for use.
Parameters:
dwHandle - pointer to associated CDisk instance (initially returned by
DSK_Init)
dwAccess - specifes how the caller would like too use the device (read
and/or write) [this argument is ignored]
dwShareMode - specifies how the caller would like this device to be shared
[this argument is ignored]
Return:
On success, return handle to "open" CDisk instance; this handle is the
same as dwHandle. Otherwise, return null.
--*/
EXTERN_C
DWORD
DSK_Open(
HANDLE dwHandle,
DWORD dwAccess,
DWORD dwShareMode
)
{
CDisk *pDisk = (CDisk *)dwHandle;
EnterCriticalSection(&g_csMain);
// validate the CDisk instance
if (!AtaIsValidDisk(pDisk)) {
pDisk = NULL;
}
LeaveCriticalSection(&g_csMain);
// if the CDisk instance is valid, then open; open just increments the
// instance's open count
if (pDisk) {
pDisk->Open();
}
return (DWORD)pDisk;
}
/*++
DSK_Close
This function closes a CDisk instance.
Parameters:
dwHandle - pointer to associated CDisk instance (initially returned by
DSK_Init)
Return:
On success, return true. Otherwise, return false.
--*/
EXTERN_C
BOOL
DSK_Close(
DWORD dwHandle
)
{
CDisk *pDisk = (CDisk *)dwHandle;
EnterCriticalSection(&g_csMain);
// validate the CDisk instance
if (!AtaIsValidDisk(pDisk)) {
pDisk = NULL;
}
LeaveCriticalSection(&g_csMain);
// if CDisk instance is valid, then close; close just decrements the
// instance's open count
if (pDisk) {
pDisk->Close();
}
return (pDisk != NULL);
}
/*++
DSK_IOControl
This function processes an IOCTL_DISK_Xxx/DISK_IOCTL_Xxx I/O control.
Parameters:
dwHandle - pointer to associated CDisk instance (initially returned by
DSK_Init)
dwIOControlCode - I/O control to perform
pInBuf - pointer to buffer containing the input data of the I/O control
nInBufSize - size of pInBuf (bytes)
pOutBuf - pointer to buffer that is to receive the output data of the
I/O control
nOutBufSize - size of pOutBuf (bytes)
pBytesReturned - pointer to DWORD that is to receive the size (bytes) of the
output data of the I/O control
pOverlapped - ignored
Return:
On success, return true. Otherwise, return false.
--*/
EXTERN_C
BOOL
DSK_IOControl(
DWORD dwHandle,
DWORD dwIoControlCode,
PBYTE pInBuf,
DWORD nInBufSize,
PBYTE pOutBuf,
DWORD nOutBufSize,
PDWORD pBytesReturned,
PDWORD pOverlapped)
{
CDisk *pDisk = (CDisk *)dwHandle;
BOOL fRet = FALSE;
if (OEM_CERTIFY_TRUST != PSLGetCallerTrust()) {
SetLastError(ERROR_ACCESS_DENIED);
return FALSE;
}
EnterCriticalSection(&g_csMain);
// validate CDisk instance
if (!AtaIsValidDisk(pDisk)) {
pDisk = NULL;
}
LeaveCriticalSection(&g_csMain);
if (!pDisk) {
return FALSE;
}
// DISK_IOCTL_INITIALIZED is a deprecated IOCTL; what does PostInit do?
if (dwIoControlCode == DISK_IOCTL_INITIALIZED) {
fRet = pDisk->PostInit((PPOST_INIT_BUF)pInBuf);
}
else {
IOREQ IOReq;
// build I/O request structure
memset(&IOReq, 0, sizeof(IOReq));
IOReq.dwCode = dwIoControlCode;
IOReq.pInBuf = pInBuf;
IOReq.dwInBufSize = nInBufSize;
IOReq.pOutBuf = pOutBuf;
IOReq.dwOutBufSize = nOutBufSize;
IOReq.pBytesReturned = pBytesReturned;
IOReq.hProcess = GetCallerProcess();
// perform I/O control
__try {
fRet = pDisk->PerformIoctl(&IOReq);
} __except(EXCEPTION_EXECUTE_HANDLER) {
fRet = FALSE;
SetLastError(ERROR_GEN_FAILURE);
}
}
return fRet;
}
/*++
DSK_PowerUp
This function resumes the device.
Parameters:
None
Return:
On success, return true. Otherwise, return false.
--*/
EXTERN_C
VOID
DSK_PowerUp(
VOID
)
{
EnterCriticalSection(&g_csMain);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -