📄 atamain.cpp
字号:
NULL, // lpClass; not required
NULL, // lpcbClass; lpClass is NULL; hence, NULL
NULL // lpftLastWriteTime; set to NULL
)) {
dwIndex += 1;
dwNewKeySize = (sizeof(szNewKey) / sizeof(TCHAR));
pDskReg = NULL;
// open the DeviceX subkey; copy configuration information from the
// IDE/ATA controller's instance key to the device's DeviceX key
if (ERROR_SUCCESS != RegOpenKeyEx(hDevKey, szNewKey, 0, 0, &hKey)) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR, (_T(
"Atapi!IDE_Init> Failed to open DeviceX subkey; device key(%s)\r\n"
), szDevKey));
goto exit;
}
if (
(NULL == pBus->m_pSecondaryPort) &&
((0 == wcscmp(szNewKey, REG_KEY_SECONDARY_MASTER)) || (0 == wcscmp(szNewKey, REG_KEY_SECONDARY_SLAVE)))
) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR, (_T(
"Atapi!IDE_Init> Secondary channel does not exist, but Device2 and Device3 subkeys exist in %s; ignoring\r\n"
), szDevKey));
dwIndex -= 1;
continue;
}
if (
(0 != wcscmp(szNewKey, REG_KEY_PRIMARY_MASTER)) && (0 != wcscmp(szNewKey, REG_KEY_PRIMARY_SLAVE)) &&
(0 != wcscmp(szNewKey, REG_KEY_SECONDARY_MASTER)) && (0 != wcscmp(szNewKey, REG_KEY_SECONDARY_SLAVE))
) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR, (_T(
"Atapi!IDE_Init> Found bad DeviceX subkey(%s) in device's(%s) key; ignoring\r\n"
), szNewKey, szDevKey));
dwIndex -= 1;
continue;
}
// fetch the device's registry value set
pDskReg = (PDSKREG)LocalAlloc(LPTR, sizeof(DSKREG));
if (!pDskReg) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR, (_T(
"Atapi!IDE_Init> Failed to allocate DSK_ registry value set; device key(%s)\r\n"
), szNewKey));
goto exit;
}
dwUndo |= IDEINIT_UNDO_DEL_REG_DSK;
if (!GetDSKRegistryValueSet(hKey, pDskReg)) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR, (_T(
"Atapi!IDE_Init> Failed to read DSK_ registry value set from registry; device key(%s)\r\n"
), szNewKey));
goto exit;
}
// resolve DeviceX subkey's "DeviceId" to (0, 1), so a CDisk instance can
// reference the correct m_pBus->m_p(Primary, Secondary)Port->(m_pDisk, m_pDskReg)
// array element
dwDeviceId = pDskReg->dwDeviceId; // store the original value
pDskReg->dwDeviceId &= 0x01;
// write the new device ID value back to the device's instance key
if (!AtaSetRegistryValue(hKey, REG_VAL_DSK_DEVICEID, pDskReg->dwDeviceId)) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR, (_T(
"Atapi!IDE_Init> Failed to write %s(%d) DSK_ registry value to device's instance key(%s)\r\n"
), REG_VAL_DSK_DEVICEID, dwDeviceId, szNewKey));
goto exit;
}
// the master and slave CDisk instances of a particular channel have to
// share the port instance associated with the channel; write the heap
// address of the port instance to the device's instance key
if ((0 == wcscmp(szNewKey, REG_KEY_PRIMARY_MASTER)) || (0 == wcscmp(szNewKey, REG_KEY_PRIMARY_SLAVE))) {
// store the DSK_ register value set of the master/slave device in
// the appropriate slot of the port instance
pBus->m_pPrimaryPort->m_pDskReg[pDskReg->dwDeviceId] = pDskReg;
if (!AtaSetRegistryValue(hKey, REG_VALUE_PORT, (DWORD)pBus->m_pPrimaryPort)) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR, (_T(
"Atapi!IDE_Init> Failed to write address of primary port instance to device's(%s) DeviceX subkey(%s)\r\n"
), szDevKey, szNewKey));
goto exit;
}
}
else if ((0 == wcscmp(szNewKey, REG_KEY_SECONDARY_MASTER)) || (0 == wcscmp(szNewKey, REG_KEY_SECONDARY_SLAVE))) {
// store the DSK_ register value set of the master/slave device in
// the appropriate slot of the port instance
pBus->m_pSecondaryPort->m_pDskReg[pDskReg->dwDeviceId] = pDskReg;
if (!AtaSetRegistryValue(hKey, REG_VALUE_PORT, (DWORD)pBus->m_pSecondaryPort)) {
DEBUGMSG(ZONE_INIT|ZONE_ERROR, (_T(
"Atapi!IDE_Init> Failed to write address of secondary port instance to device's(%s) DeviceX subkey(%s)\r\n"
), szDevKey, szNewKey));
goto exit;
}
}
if (!pBus->m_szDevice[dwDeviceId]) {
// save name of device's full registry key path; when we've finished
// enumerating the "bus", we'll call ActivateDevice against all of
// these paths
pBus->m_szDevice[dwDeviceId] = new TCHAR[wcslen(szDevKey) + wcslen(szNewKey) + 10];
wcscpy(pBus->m_szDevice[dwDeviceId], szDevKey);
wcscat(pBus->m_szDevice[dwDeviceId], L"\\");
wcscat(pBus->m_szDevice[dwDeviceId], szNewKey);
DEBUGMSG(ZONE_INIT, (_T(
"Atapi!IDE_Init> Enumerated IDE/ATA device %s\r\n"
), pBus->m_szDevice[dwDeviceId]));
}
} // while
DEBUGMSG(ZONE_INIT, (_T(
"Atapi!IDE_Init> End of IDE/ATA device enumeration\r\n"
)));
// initialize enumerated devices; it's imperative that we activate the
// channel master before the channel slave
for (dwDeviceId = 0; dwDeviceId < 4; dwDeviceId += 1) {
if (pBus->m_szDevice[dwDeviceId]) {
DEBUGMSG(ZONE_INIT, (_T(
"Atapi!IDE_Init> Activating IDE/ATA device %s\r\n"
), pBus->m_szDevice[dwDeviceId]));
pBus->m_hDevice[dwDeviceId] = ActivateDeviceEx(pBus->m_szDevice[dwDeviceId], NULL, 0, NULL);
}
}
dwUndo &= ~IDEINIT_UNDO_DEL_BUS;
dwUndo &= ~IDEINIT_UNDO_DEL_PORT_PRI;
dwUndo &= ~IDEINIT_UNDO_DEL_PORT_SEC;
exit:;
if (dwUndo & IDEINIT_UNDO_CLS_KEY_ACTIVE) {
RegCloseKey(hActiveKey);
}
if (dwUndo & IDEINIT_UNDO_CLS_KEY_DEVICE) {
RegCloseKey(hDevKey);
}
if (szDevKey) {
LocalFree(szDevKey);
}
if ((NULL != pBus) && (dwUndo & IDEINIT_UNDO_DEL_BUS)) {
delete pBus;
pBus = NULL;
}
return (DWORD)pBus;
}
/*++
IDE_Deinit
This function deallocates the associated IDE/ATA controller ("bus") instance.
Parameters:
dwHandle - pointer to associated bus instance (initially returned by
IDE_Init)
Return:
This function always succeeds.
--*/
EXTERN_C
BOOL
IDE_Deinit(
DWORD dwHandle
)
{
CIDEBUS *pBus = (CIDEBUS *)dwHandle;
DEBUGCHK(pBus != NULL);
delete pBus;
return TRUE;
}
/*++
IDE_Open
This function is not supported.
Parameters:
N/A
Return:
This function always fails.
--*/
EXTERN_C
DWORD
IDE_Open(
HANDLE dwHandle,
DWORD dwAccess,
DWORD dwShareMode
)
{
SetLastError(ERROR_NOT_SUPPORTED);
return NULL;
}
/*++
IDE_Close
This function is not supported.
Parameters:
N/A
Return:
This function always fails.
--*/
EXTERN_C
BOOL
IDE_Close(
DWORD dwHandle
)
{
SetLastError(ERROR_NOT_SUPPORTED);
return FALSE;
}
/*++
IDE_IOControl
This function is not supported.
Parameters:
N/A
Return:
This function always fails.
--*/
EXTERN_C
BOOL
IDE_IOControl(
DWORD dwHandle,
DWORD dwIoControlCode,
PBYTE pInBuf,
DWORD nInBufSize,
PBYTE pOutBuf,
DWORD nOutBufSize,
PDWORD pBytesReturned,
PDWORD pOverlapped
)
{
SetLastError(ERROR_NOT_SUPPORTED);
return FALSE;
}
/*++
DllMain
This function is the main ATAPI.DLL entry point.
Parameters:
hInstance - a handle to the dll; this value is the base address of the DLL
dwReason - the reason for the DLL is being entered
lpReserved - not used
Return:
On success, return true. Otherwise, return false.
--*/
BOOL
WINAPI
DllMain(
HANDLE hInstance,
DWORD dwReason,
LPVOID lpReserved
)
{
switch (dwReason) {
case DLL_PROCESS_ATTACH:
// initialize global data
g_hInstance = (HINSTANCE)hInstance;
InitializeCriticalSection(&g_csMain);
// register debug zones
RegisterDbgZones((HMODULE)hInstance, &dpCurSettings);
DisableThreadLibraryCalls((HMODULE)hInstance);
DEBUGMSG(ZONE_INIT, (_T("ATAPI DLL_PROCESS_ATTACH\r\n")));
break;
case DLL_PROCESS_DETACH:
// deinitialize global data
DeleteCriticalSection(&g_csMain);
DEBUGMSG(ZONE_INIT, (TEXT("ATAPI DLL_PROCESS_DETACH\r\n")));
break;
}
return TRUE;
}
/*
#define inportw(p) (*(volatile unsigned short *)(p))
#define outportw(d, p) (*(volatile unsigned char *)(p)=(d))
#define inportb(p) (*(volatile unsigned char *)(p))
#define STATUS_BUSY (1<<7)
#define STATUS_READY (1<<6)
#define STATUS_FAULT (1<<5)
#define STATUS_DSC (1<<4)
#define STATUS_DRQ (1<<3)
#define STATUS_ERR (1<<0)
BOOL WaitToReady(DWORD port,int time,char bit)
{
while(time --)
{
Sleep(200);
//printf("%x \n",inportw(port));
//if(time ==1)
printf("%x \n",(inportb(port)) & (bit | STATUS_BUSY));
if(((inportb(port)) & (bit | STATUS_BUSY)) == bit) return TRUE;
}
return FALSE;
}
void GetIdeInfo(DWORD base,BOOL bPhy)
{
DWORD pDAT ;
if(bPhy)
{
pDAT=(DWORD) VirtualAlloc(0,0x10,MEM_RESERVE,PAGE_READWRITE|PAGE_NOCACHE);
VirtualCopy((LPVOID)pDAT,(LPVOID)base,0x10,PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL);
}
else pDAT = base;
if(pDAT)
{
DWORD pCMD = pDAT+0xE;
printf("cmd %x data %x\n",pCMD,pDAT);
int i;
unsigned short buffer[256];
WaitToReady(pCMD,10,STATUS_READY);
{
outportw(0x20,pCMD);
WaitToReady(pCMD,10,STATUS_DRQ);
{
for(i=0; i<64 ; i++)
{
buffer[i] = inportw(pDAT);
printf(" %04x %02X",buffer[i],inportb(pCMD));
if((i & 0x7)== 0x7) printf("\n");
//Sleep(1);
}
/*
DisplayInfo(" Drive serail number:",buffer+10,10);
DisplayInfo("\n Fireware version:",buffer+23,4);
DisplayInfo("\n Model number:",buffer+27,20);
printf("\n number of logical cylinders: 0x%04x\n",buffer[1]);
printf(" number of logical headers: 0x%04x\n",buffer[3]);
printf(" number of sector per track: 0x%04x\n",buffer[6]);
printf(" current cylinder: 0x%04x\n",buffer[54]);
printf(" current header: 0x%04x\n",buffer[55]);
printf(" current sector: 0x%04x\n",buffer[56]);
printf(" buffer size %dKB \n",buffer[21]>>1);
*
}
}
if(bPhy) VirtualFree((LPVOID) pDAT,0,MEM_RELEASE);
}
}
void ShowReg(DWORD base)
{
int i = 8;
while(i--)
{
printf("reg %08x %x\n",base,inportb(base));
base +=2;
}
printf("\n");
}
//check ide's status,util it's ready
void DisplayInfo(char * title,short * info,int size)
{
printf(title);
while(size--)
{
printf("%c%c",(*info) >> 8,*info);
info++;
}
}
void ShowPage(DWORD value,DWORD mask)
{
PDWORD pp = (PDWORD) 0xFFFD0000;
int i;
for(i = 0;i<4096;i++,pp++) if((*pp & mask) == value) printf("%08x %08x\n",i<<20,*pp);
}
void ResetIDE()
{
outportw(0,0xB680000C);
::StallExecution(25);
outportw(4,0xB680000C);
::StallExecution(25);
outportw(0,0xB680000C);
Sleep(5);
WaitToReady(0xB680000C,10,0);
}
EXTERN_C
LPTSTR
DetectATADisk(
CARD_SOCKET_HANDLE hSock,
UCHAR DevType,
LPTSTR DevKey,
DWORD DevKeyLen
)
{
DEBUGMSG(1,(TEXT("DetectATADisk DevKey %s \r\n"),DevKey));
//ShowPage(0x0C000000,0x0F000000);
//outportd(inportd(0xa5300010)|(1 << 21),0xa5300010);
//Sleep(5);
//ShowReg(0xB6810000);
//outportd(1 << 21,0xa5300028);
//Sleep(5);
//outportd(1 << 21,0xa530001C);
{
BYTE b;
do
{
b = inportb(0xB681000E);
printf(".");
Sleep(1000);
}
while(b == 0 || b == 0x80);
printf("\n");
}
//ResetIDE();
//Sleep(1000);
//ShowReg(0xB6810000);
//ShowReg(0xA2000010);
//ShowReg(0xB6800000);
//GetIdeInfo(0x04000010);
//GetIdeInfo(0x0C010000,1);
//GetIdeInfo(0xA2000020,0);
GetIdeInfo(0xB6810000,0);
//GetIdeInfo(0xB6800000,0);
/*
if (DevType == PCCARD_TYPE_FIXED_DISK) {
if (ATADetect(hSock) == TRUE) {
_tcscpy(DevKey, TEXT("ATADisk"));
return DevKey;
}
}*
return NULL;
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -