⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 atamain.cpp

📁 这是运行在windows ce 4.2 版本下的关于硬盘加载的驱动程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                      PDWORD pBytesReturned,//Pointer to a variable that receives the size, in bytes, of the data stored into the buffer pointed to by pOutBuf. 
                      PDWORD pOverlapped)   //Ignored; set to NULL. 
{
    BOOL fRet = FALSE;
    CDisk *pDisk = (CDisk *)pHandle;
    DEBUGMSG( ZONE_MAIN, (TEXT("ATAPI:DSK_IOControl request Handle = %08X dwCode=%ld\r\n"), pHandle, dwIoControlCode));
    EnterCriticalSection( &g_csMain);
    if (!AtaIsValidDisk(pDisk)) {
        pDisk = NULL;
    }
    LeaveCriticalSection( &g_csMain);
    if (pDisk) {
        if (dwIoControlCode == DISK_IOCTL_INITIALIZED) {
            fRet = pDisk->PostInit( (PPOST_INIT_BUF) pInBuf);
        } else {
            IOREQ IOReq;
            
            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();
            
            __try {
                DEBUGMSG( ZONE_IOCTL, (TEXT("Enter ioctl %08X\r\n"), dwIoControlCode));
                fRet = pDisk->PerformIoctl(&IOReq);
                DEBUGMSG( ZONE_IOCTL, (TEXT("Exit ioctl %08X\r\n"), dwIoControlCode));
            } __except(EXCEPTION_EXECUTE_HANDLER) {
                DEBUGMSG( ZONE_ERROR, (TEXT("ATAPI:Exception in PerformIoctl !!!\r\n")));
                fRet = FALSE;
                SetLastError(ERROR_GEN_FAILURE);
            }
        }
    }    
    return fRet;
}

// Not implemented by ATAPI driver ...use IOCTL instead
EXTERN_C DWORD   DSK_Read(DWORD Handle, LPVOID pBuffer, DWORD dwNumBytes)
{   
    SetLastError(ERROR_NOT_SUPPORTED);
    return 0;
}

// Not implemented by ATAPI driver ...use IOCTL instead
EXTERN_C DWORD   DSK_Write(DWORD Handle, LPCVOID pBuffer, DWORD dwNumBytes)
{   
    SetLastError(ERROR_NOT_SUPPORTED);
    return 0;
}

// Not implemented by ATAPI driver ...use IOCTL instead
EXTERN_C DWORD   DSK_Seek(DWORD Handle, long lDistance, DWORD dwMoveMethod)
{
    SetLastError(ERROR_NOT_SUPPORTED);
    return 0;
}

EXTERN_C void    DSK_PowerUp(void)
{
    EnterCriticalSection(&g_csMain);
    CDisk *pDisk = g_pDiskRoot;
    while(pDisk) {
        pDisk->PowerUp();
        pDisk = pDisk->m_pNextDisk;
    }
    LeaveCriticalSection(&g_csMain);
}

EXTERN_C void    DSK_PowerDown(void)
{
    EnterCriticalSection(&g_csMain);
    CDisk *pDisk = g_pDiskRoot;
    while(pDisk) {
        pDisk->PowerDown();
        pDisk = pDisk->m_pNextDisk;
    }
    LeaveCriticalSection(&g_csMain);
}

BOOL ReadIoInfo(HKEY hDevKey, TCHAR *szValueName, PDWORD pdwData)
{
    TCHAR szData[MAX_PATH];
    TCHAR *szStopString;
    DWORD cbData;
    DWORD dwType;
    cbData = sizeof(szData);
    DWORD dwIndex = 0;
    if (ERROR_SUCCESS == RegQueryValueEx( hDevKey, szValueName, NULL, &dwType, (PBYTE)szData, &cbData) && (dwType == REG_MULTI_SZ)) {
        PWSTR szTemp = szData;
        while(szTemp) {
            pdwData[dwIndex] = wcstol( szTemp, &szStopString, 16);
            if (++dwIndex > 4)
                break;
            szTemp += (wcslen(szTemp)+1);
        }
        return (dwIndex == 5);
    }
    return FALSE;
}

DWORD DoIoTranslation(DWORD dwAddress, DWORD dwLen)
{
    PHYSICAL_ADDRESS    PhysicalAddress;
    LPVOID pAddress;

    PhysicalAddress.HighPart = 0;
    PhysicalAddress.LowPart = dwAddress;
    DWORD AddressSpace = 1;
    TransBusAddrToVirtual( PCIBus, 0, PhysicalAddress, dwLen, &AddressSpace, &pAddress);
    return (DWORD)pAddress;
}

DWORD DoStaticTranslation(DWORD dwAddress, DWORD dwLen)
{
    PHYSICAL_ADDRESS    PhysicalAddress;
    LPVOID pAddress;

    PhysicalAddress.HighPart = 0;
    PhysicalAddress.LowPart = dwAddress;
    DWORD AddressSpace = 1;
    TransBusAddrToStatic( PCIBus, 0, PhysicalAddress, dwLen, &AddressSpace, &pAddress);
    return (DWORD)pAddress;
}

EXTERN_C DWORD IDE_Init(DWORD dwContext)
{   
    PTSTR szActiveKey = (PTSTR)dwContext;
    DWORD dwOption;
    HKEY hActiveKey = NULL;
    IDEBUS *pBus  = NULL;
    PTSTR szDevKey = NULL;
    HKEY hDevKey = NULL;
    DEBUGMSG( ZONE_MAIN, (TEXT("ATAPI: IDE_Init ActiveKey = %s\r\n"), szActiveKey));
    if (!g_ControllerTable) {
        g_ControllerTable = new CController[MAX_ATA_CNTRL];
        g_PortTable = new CPort[MAX_ATA_PORT];
        if (!g_ControllerTable || !g_PortTable) {
            return 0;
        }   
        g_dwControllerIndex = 0;
    } else {
        g_dwControllerIndex += 2;
    }
    DWORD dwError = RegOpenKeyEx( HKEY_LOCAL_MACHINE, szActiveKey, 0, 0, &hActiveKey);
    if  ((ERROR_SUCCESS == dwError) && hActiveKey){
        DUMPREGKEY( ZONE_INIT, szActiveKey, hActiveKey);
        if ((hDevKey = AtaLoadRegKey( hActiveKey, &szDevKey)) && szDevKey) {
            DUMPREGKEY( ZONE_INIT, szDevKey, hDevKey);
            pBus = new IDEBUS;
            if (!pBus) {
                goto AbortInit;
            }
            memset(pBus, 0, sizeof(IDEBUS));
            if (ReadIoInfo( hDevKey, L"IoBase", pBus->dwIoBase) && ReadIoInfo( hDevKey, L"IoLen", pBus->dwIoLen)) {
                g_ControllerTable[g_dwControllerIndex].m_dwRegBase= DoIoTranslation(pBus->dwIoBase[0], pBus->dwIoLen[0]);
                g_ControllerTable[g_dwControllerIndex].m_dwRegAlt  = DoIoTranslation(pBus->dwIoBase[1], pBus->dwIoLen[1]);
                g_ControllerTable[g_dwControllerIndex].m_dwBMR = DoIoTranslation(pBus->dwIoBase[4], pBus->dwIoLen[4]);
                g_ControllerTable[g_dwControllerIndex].m_dwBMRStatic= DoStaticTranslation(pBus->dwIoBase[4], pBus->dwIoLen[4]);
                g_ControllerTable[g_dwControllerIndex+1].m_dwRegBase= DoIoTranslation(pBus->dwIoBase[2], pBus->dwIoLen[2]);
                g_ControllerTable[g_dwControllerIndex+1].m_dwRegAlt  = DoIoTranslation(pBus->dwIoBase[3], pBus->dwIoLen[3]);
                g_ControllerTable[g_dwControllerIndex+1].m_dwBMR = g_ControllerTable[g_dwControllerIndex].m_dwBMR+8;
                g_ControllerTable[g_dwControllerIndex+1].m_dwBMRStatic = g_ControllerTable[g_dwControllerIndex].m_dwBMRStatic+8;
//
                if (AtaGetRegistryValue( hDevKey, L"Irq", &g_ControllerTable[g_dwControllerIndex].m_dwIrq) && g_ControllerTable[g_dwControllerIndex].m_dwIrq) {
                    if (!AtaGetRegistryValue( hDevKey, L"SysIntr", &g_ControllerTable[g_dwControllerIndex].m_dwSysIntr)) {
                        g_ControllerTable[g_dwControllerIndex].m_dwSysIntr = 0;
                    }
                } else {
                    g_ControllerTable[g_dwControllerIndex].m_dwIrq = 0;
                    g_ControllerTable[g_dwControllerIndex].m_dwSysIntr = 0;
                }
                if (AtaGetRegistryValue(hDevKey, L"Legacy", &dwOption) && (dwOption == 1)) {
                    DEBUGMSG( ZONE_INIT, (L"ATAPI!IDE_INIT - Using Legacy mode IRQ setup \r\n"));
                    if (g_ControllerTable[g_dwControllerIndex].m_dwIrq )
                        g_ControllerTable[g_dwControllerIndex+1].m_dwIrq = g_ControllerTable[g_dwControllerIndex].m_dwIrq + 1;
                } else {
                    g_ControllerTable[g_dwControllerIndex+1].m_dwIrq = g_ControllerTable[g_dwControllerIndex].m_dwIrq;
                }   
                g_ControllerTable[g_dwControllerIndex+1].m_dwSysIntr = 0;

            
                
                HKEY hKey;
                DWORD dwIndex = 0;
                DWORD dwDeviceId;
                TCHAR szNewKey[MAX_PATH];
                DWORD dwNewKeySize = sizeof(szNewKey)/sizeof(TCHAR);
                DWORD dwVendorID = 0;
                TCHAR *szTemp;

               
                if (!AtaGetRegistryValue(hDevKey, L"VendorID", &dwVendorID)) {
                    dwVendorID = 0;
                }
                while(ERROR_SUCCESS == RegEnumKeyEx( hDevKey, dwIndex, szNewKey, &dwNewKeySize, NULL, NULL, NULL, NULL)) {
                    dwIndex++;
                    dwNewKeySize = sizeof(szNewKey)/sizeof(TCHAR);
                    if (ERROR_SUCCESS == RegOpenKeyEx( hDevKey, szNewKey, 0, 0, &hKey)) {
                        szTemp = NULL;
                        if (AtaGetRegistryString(hDevKey, L"IsrDll", &szTemp) && szTemp) {
                            AtaSetRegistryString(hKey, L"IsrDll", szTemp);
                            LocalFree( szTemp);
                        }
                        szTemp = NULL;
                        if (AtaGetRegistryString(hDevKey, L"IsrHandler", &szTemp) && szTemp) {
                            AtaSetRegistryString(hKey, L"IsrHandler", szTemp);
                            LocalFree( szTemp);
                        }
                        szTemp = NULL;
                        if (AtaGetRegistryString(hDevKey, L"Object", &szTemp) || AtaGetRegistryString(hKey, L"Object", &szTemp)) {
                            LocalFree( szTemp);
                        } else {
                            switch(dwVendorID) {
                                case 0x105A:
                                    AtaSetRegistryString(hKey, L"Object", L"CreatePromise");
                                    break;
                                case 0x10B9:                                
                                case 0x1106:
    //                                AtaSetRegistryString(hKey, L"Object", L"CreateAli");
    //                                break;
                                default:
                                    AtaSetRegistryString(hKey, L"Object", L"CreateGeneric");
                                    break;
                            }
                        }    
                        if (!AtaGetRegistryValue(hKey, L"DeviceId", &dwDeviceId)) {
                            dwDeviceId = 0;
                        }
                        AtaSetRegistryValue( hKey, L"DeviceId", dwDeviceId | (g_dwControllerIndex << 1)); // PCI
                        dwDeviceId = dwDeviceId & 0x3;
                        if (!pBus->szDevice[dwDeviceId]) {
                            pBus->szDevice[dwDeviceId] = new TCHAR[wcslen(szDevKey)+wcslen(szNewKey)+10];
                            wcscpy( pBus->szDevice[dwDeviceId], szDevKey);
                            wcscat( pBus->szDevice[dwDeviceId], L"\\");
                            wcscat( pBus->szDevice[dwDeviceId], szNewKey);
                            DEBUGMSG( ZONE_INIT, (L"ATAPI!IDE_Init: Found storage device %s\r\n", pBus->szDevice[dwDeviceId]));
                        }   
                    }   
                }
                for (dwDeviceId=0; dwDeviceId < 4; dwDeviceId++) {
                    if (pBus->szDevice[dwDeviceId]) {
                        DEBUGMSG( ZONE_INIT, (L"Activating device %ld\r\n", dwDeviceId));
                        pBus->hDevice[dwDeviceId] = (DWORD)ActivateDevice(pBus->szDevice[dwDeviceId], 0);
                    }
                }   
            } else {    
                delete pBus;
                pBus = NULL;
            } 
        }
    }
AbortInit:    
    if (szDevKey)
        LocalFree( szDevKey); 
    if (hActiveKey) 
        RegCloseKey( hActiveKey);
    if (hDevKey) 
        RegCloseKey( hDevKey);
    return ((DWORD)pBus);
}


EXTERN_C BOOL    IDE_Deinit(DWORD pHandle)
{   
    return TRUE;
}   

EXTERN_C DWORD IDE_Open( HANDLE pHandle,
               DWORD dwAccess,
               DWORD dwShareMode)
{
    SetLastError(ERROR_NOT_SUPPORTED);
    return FALSE;
}

EXTERN_C BOOL    IDE_Close(DWORD pHandle)
{
    SetLastError(ERROR_NOT_SUPPORTED);
    return FALSE;
}   

EXTERN_C BOOL    IDE_IOControl(DWORD pHandle,        //Handle to the device 
                      DWORD dwIoControlCode,//Specifies the control code 
                      PBYTE pInBuf,         //Pointer to a buffer that contains the data required to perform the operation.
                      DWORD nInBufSize,     //Size, in bytes, of pInBuf
                      PBYTE pOutBuf,        //Pointer to a buffer that receives the operation's output data. 
                      DWORD nOutBufSize,    //Size, in bytes, of pOutBuf
                      PDWORD pBytesReturned,//Pointer to a variable that receives the size, in bytes, of the data stored into the buffer pointed to by pOutBuf. 
                      PDWORD pOverlapped)   //Ignored; set to NULL. 
{
    SetLastError(ERROR_NOT_SUPPORTED);
    return FALSE;
}

// Not implemented by ATAPI driver ...use IOCTL instead
EXTERN_C DWORD   IDE_Read(DWORD Handle, LPVOID pBuffer, DWORD dwNumBytes)
{   
    SetLastError(ERROR_NOT_SUPPORTED);
    return 0;
}

// Not implemented by ATAPI driver ...use IOCTL instead
EXTERN_C DWORD   IDE_Write(DWORD Handle, LPCVOID pBuffer, DWORD dwNumBytes)
{   
    SetLastError(ERROR_NOT_SUPPORTED);
    return 0;
}

// Not implemented by ATAPI driver ...use IOCTL instead
EXTERN_C DWORD   IDE_Seek(DWORD Handle, long lDistance, DWORD dwMoveMethod)
{
    SetLastError(ERROR_NOT_SUPPORTED);
    return 0;
}

EXTERN_C void    IDE_PowerUp(void)
{
}

EXTERN_C void    IDE_PowerDown(void)
{
}


//--------------------------------------------------------------------------    
//
//  ATAPIMain - The Main Dll load entry point
//
//  Input:  DllInstance -  a Handle to the DLL. The value is the base address of the DLL. 
//              The HINSTANCE of a DLL is the same as the HMODULE of the DLL, so hinstDLL 
//              can be used in subsequent calls to the GetModuleFileName function and other 
//              functions that require a module handle. 
//
//          dwReason -  a flag indicating why the DLL entry-point function is being called. 
//                      This parameter can be one of the following values: 
//  Return: TRUE    -   ok. 
//          FALSE   -   error. 
//  
//  NOTES   Register Debug Zones only.
//          This function is an optional method of entry into a dynamic-link library (DLL). 
//          If the function is used, it is called by the system when processes and threads 
//          are initialized and terminated, or upon calls to the LoadLibrary and FreeLibrary 
//          functions. 
//          DllMain is a placeholder for the library-defined function name. 
//          You must specify the actual name you use when you build your DLL. 
//          For more information, see the documentation included with your development tools. 
//
//--------------------------------------------------------------------------    

BOOL WINAPI DllMain(HANDLE hInstance, DWORD dwReason, LPVOID lpReserved)
{
    
    switch (dwReason)
    {
    case DLL_PROCESS_ATTACH:
        g_hInstance = (HINSTANCE)hInstance;
        InitializeCriticalSection(&g_csMain);
        DEBUGMSG( ZONE_INIT, (TEXT("ATAPI DLL_PROCESS_ATTACH\r\n")));
        DEBUGREGISTER((HINSTANCE)hInstance);
    DisableThreadLibraryCalls((HMODULE) hInstance);
        break;
        
    case DLL_PROCESS_DETACH:
        //if (g_ControllerTable) {
            delete [] g_ControllerTable;
            delete [] g_PortTable;
        //}   
        DeleteCriticalSection(&g_csMain);
        DEBUGMSG( ZONE_INIT, (TEXT("ATAPI DLL_PROCESS_DETACH\r\n")));
        break;
    }
    return TRUE;
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -