📄 atamain.cpp
字号:
//
// 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 LICENCE.RTF on your
// install media.
//
#include <atamain.h>
#include "ep931xide.h"
// #include <atapipci.h>
CDisk *g_pDiskRoot = NULL;
CEP931xPort *g_pPort = NULL;
CRITICAL_SECTION g_csMain;
HINSTANCE g_hInstance=NULL;
typedef CDisk *(* POBJECTFUNCTION)(HKEY hKey);
CDisk * gpDisk;
HKEY AtaLoadRegKey(HKEY hActiveKey, TCHAR **pszDevKey)
{
DWORD dwType;
PTSTR szDevKey = NULL;
DWORD dwValueLen = 0;
HKEY hDevKey = NULL;
// Find out how big the name of the device key is
// Since dwValueLen is 0 and the name is NULL we expect a ERROR_SUCCESS although the doc's
// say that it should return ERROR_MORE_DATA
if (ERROR_SUCCESS == RegQueryValueEx( hActiveKey, DEVLOAD_DEVKEY_VALNAME, NULL, &dwType, NULL, &dwValueLen)) {
szDevKey = (PTSTR)LocalAlloc( LPTR, dwValueLen);
// Read in the KeyName of the device
RegQueryValueEx( hActiveKey, DEVLOAD_DEVKEY_VALNAME, NULL, &dwType, (PBYTE)szDevKey, &dwValueLen);
// Now do the actual open of the key
if (ERROR_SUCCESS != RegOpenKeyEx( HKEY_LOCAL_MACHINE, szDevKey, 0, 0, &hDevKey)) {
hDevKey = NULL;
DEBUGMSG( ZONE_INIT, (TEXT("Found a device key name %s but could not open it !!!\r\n"), szDevKey));
}
}
if (!hDevKey) {
LocalFree( szDevKey);
*pszDevKey = NULL;
} else {
*pszDevKey = szDevKey;
}
return hDevKey;
}
BOOL AtaIsValidDisk(CDisk *pDisk)
{
CDisk *pTemp = g_pDiskRoot;
while(pTemp) {
if (pTemp == pDisk)
return TRUE;
pTemp = pTemp->m_pNextDisk;
}
return FALSE;
}
//--------------------------------------------------------------------------
// DSK_Init - This function is called by the Device Manager to initialize a device.
//
// Input: Specifies a pointer to a string containing the registry path to the active
// key for the stream interface driver.
//
// Return: Handle - This handle is passed to the DSK_Open,
// (DSK_PowerDown, DSK_PowerUp,) and DSK_Deinit functions.
// NULL - error.
//
// NOTES The Device Manager calls this function as a result of a call to
// the RegisterDevice function. When the user starts using a device,
// such as when a PC Card is inserted, the Device Manager calls this function
// to initialize the device. This function is not called by applications.
//
// It checks the corresponding ATAPI controller and and the associated device.
// There must be an entry point in the registry for each device.
// The registry entry must contain the PortNumber and the DeviceNumber (0/1)
//
// The Device Manager specifies a pointer to a string containing the registry path
// to the active key of the specific device in the dwContext parameter.
// Usually, the string contains the registry path to a numbered subkey of
// the HKEY_LOCAL_MACHINE\Drivers\Active key. Your initialization function uses
// this information to access data stored in the registry.
//--------------------------------------------------------------------------
EXTERN_C DWORD DSK_Init(DWORD dwContext)
{
PTSTR szActiveKey = (PTSTR)dwContext;
DWORD dwHandle = 0;
HKEY hActiveKey;
CDisk *pDisk = NULL;
DWORD dwError = ERROR_SUCCESS;
DEBUGMSG( ZONE_MAIN, (TEXT("ATAPI: DSK_Init ActiveKey = %s\r\n"), szActiveKey));
EnterCriticalSection( &g_csMain);
//
// Create only one instance of the port class.
//
if(!g_pPort)
{
BOOL bRet;
g_pPort = new CEP931xPort();
//
// Initialize the port data structure.
//
if(g_pPort)
{
bRet = g_pPort->Init();
}
//
// Make sure that the port classs Initialized correctly
//
if(!bRet && g_pPort)
{
dwError = ERROR_NOT_ENOUGH_MEMORY;
}
}
if(ERROR_SUCCESS == dwError)
{
dwError = RegOpenKeyEx( HKEY_LOCAL_MACHINE, szActiveKey, 0, 0, &hActiveKey);
}
if (ERROR_SUCCESS == dwError)
{
PTSTR szDevKey = NULL;
HKEY hDevKey;
DUMPREGKEY( ZONE_INIT, szActiveKey, hActiveKey);
if (hDevKey = AtaLoadRegKey( hActiveKey, &szDevKey))
{
DWORD dwLen = sizeof(DWORD);
DUMPREGKEY( ZONE_INIT, szDevKey, hDevKey);
TCHAR *szObject=NULL;
POBJECTFUNCTION pObject = NULL;
pDisk = new CEP931xDisk( g_pPort, hDevKey);
//
// Just check again that this is ok.
//
if (pDisk && szActiveKey && szDevKey)
{
pDisk->SetActiveKey(szActiveKey);
pDisk->SetDeviceKey(szDevKey);
//
// Go head and setup the chain so in case AtaIsValidDisk is accessed
//
pDisk->m_pNextDisk = g_pDiskRoot;
g_pDiskRoot = pDisk;
if (pDisk->Init( hActiveKey))
{
}
else
{
//
// Reset the root back...
//
g_pDiskRoot = pDisk->m_pNextDisk;
delete pDisk;
pDisk = NULL;
}
}
}
if (szDevKey)
LocalFree( szDevKey);
RegCloseKey( hActiveKey);
}
LeaveCriticalSection( &g_csMain);
return ((DWORD)pDisk);
}
//--------------------------------------------------------------------------
//
// DSK_Deinit: Process Device Closing command.This function is called by
// the Device Manager to deinitialize previously initialized device.
//
// When the user stops using a device, such as when a PC Card
// is removed from its socket,the Device Manager calls it.
// This function is not called by applications.
// The Device Manager calls DSK_Deinit as a result
// of the call to the DeregisterDevice function.
//
// Input: pHandle - Pointer to Device Handle returned by DSK_Init.
//
// Return: TRUE - ok.
// FALSE - error.
//
// Notes: At the moment we do nothing, except resetting device flag.
//
//--------------------------------------------------------------------------
EXTERN_C BOOL DSK_Deinit(DWORD pHandle)
{
CDisk *pDiskPrev=NULL, *pDiskCur=g_pDiskRoot;
DEBUGMSG( ZONE_MAIN, (TEXT("ATAPI: DSK_Deinit pHandle = %08X\r\n"), pHandle));
EnterCriticalSection (&g_csMain);
while(pDiskCur)
{
if(pDiskCur == (CDisk *)pHandle)
break;
pDiskPrev = pDiskCur;
pDiskCur = pDiskCur->m_pNextDisk;
}
if (pDiskCur)
{
if (pDiskPrev)
pDiskPrev = pDiskCur->m_pNextDisk;
else
g_pDiskRoot = pDiskCur->m_pNextDisk;
delete pDiskCur;
}
LeaveCriticalSection( &g_csMain);
return TRUE;
}
//--------------------------------------------------------------------------
//
// DSK_Open - ATAPIPCI.DLL open command processing entry point
//
// Input: pHandle - Pointer to a Device Handle. The DSK_Init function
// creates and returns this handle.
//
// dwAccess - Specifies the requested access code of the device.
// The access is a combination of read and write.
//
// dwSharedMode - File share mode. The share mode is a combination
// of file read and write sharing
//
// Return: HANDLE - ok.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -