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

📄 cedriver.c

📁 Windows CE 6.0 BSP for VOIP sample phone. Intel PXA270 platform.
💻 C
📖 第 1 页 / 共 5 页
字号:
    PDISK    pDisk = NULL;
    FLStatus status = flOK;
    DWORD retVal = 0;
    
    DBGMSG(ZONE_OPEN, (TEXT("TrueFFS, DSK_Open(): DSK_Open [START]\r\n")));

    // Validate the caller.
    //
    // This driver allows only kernel mode access.
    // So in order to reduce attack surface and the need to marshal embedded pointers,
    // all user-mode access will be blocked here
    if(GetDirectCallerProcessId() != GetCurrentProcessId())
    {
        SetLastError(ERROR_ACCESS_DENIED);
        DBGMSG(ZONE_OPEN | ZONE_ERROR, (TEXT("TrueFFS, Open(): Received user mode request. Returning FALSE.")));
        return 0;        
    }

    pDisk = (PDISK)dwData;

    // Validate the pDisk arguement
    if(IsValidDisk(pDisk) == FALSE)
    {       
        DBGMSG(ZONE_IO | ZONE_ERROR, (TEXT("TrueFFS, Open(): Recieved pDIsk is invalid. Returning FALSE.\r\n")));
        return 0; 
    }

    EnterCriticalSection(&pDisk->cs);    
    
    if((pDisk->diskState == STATE_INITIALIZING) || // Never been mounted or 
       (pDisk->diskState == STATE_CLOSED))         // Closed by all the threads 
    {
        DBGMSG(ZONE_OPEN, (TEXT("TrueFFS, DSK_Open(): Partition was closed, trying to mount it.\r\n")));
        status = mountDisk(pDisk);
        if (status != flOK)
        {
            DBGMSG(ZONE_OPEN | ZONE_ERROR, (TEXT("TrueFFS, DSK_Open(): Failed to mount disk.\r\n")));
            goto Leave;
        }
    }
    else  // Volume has already been successfully opened by another thread 
    {
        DBGMSG(ZONE_OPEN, (TEXT("TrueFFS, DSK_Open(): Volume already open!\r\n"))); 
    }
    
    pDisk->openCount++;
    pDisk->diskState = STATE_OPENED;
    
    DBGMSG(ZONE_OPEN, (TEXT("TrueFFS, DSK_Open(): [END] returning pDisk=0x%lX\r\n"), (DWORD)pDisk));
    
    retVal = (DWORD)pDisk; 

Leave:
    LeaveCriticalSection(&pDisk->cs);    
    return retVal;
}




//----------------------------------------------------------------------------
// Function Name: DSK_Close                                                   
// Purpose......: Close the opened disk                                       
// Returns......: TRUE on sucess, FALSE otherwise                             
//----------------------------------------------------------------------------
BOOL DSK_Close(DWORD Handle)
{
    IOreq ioreq;
    FLStatus status;
    PDISK pDisk = NULL;
    BOOL retVal = FALSE;
    
    DBGMSG(ZONE_OPEN, (TEXT("TrueFFS, DSK_Close(): [START]  pDisk = %ld\r\n"),pDisk));

    // Validate the caller.
    //
    // This driver allows only kernel mode access.
    // So in order to reduce attack surface and the need to marshal embedded pointers,
    // all user-mode access will be blocked here
    if(GetDirectCallerProcessId() != GetCurrentProcessId())
    {
        SetLastError(ERROR_ACCESS_DENIED);
        DBGMSG(ZONE_OPEN | ZONE_ERROR, (TEXT("TrueFFS, Close(): Received user mode request. Returning FALSE.")));
        return FALSE;        
    }

    pDisk = (PDISK)Handle;

    // Validate the pDisk arguement
    if(IsValidDisk(pDisk) == FALSE)
    {       
        DBGMSG(ZONE_OPEN | ZONE_ERROR, (TEXT("TrueFFS, Close(): Recieved pDIsk is invalid. Returning FALSE.\r\n")));
        return FALSE;
    }

    EnterCriticalSection(&pDisk->cs);
    
    if(pDisk->diskState == STATE_OPENED)
    {
        pDisk->openCount--;
        if (pDisk->openCount <= 0)
        {
            pDisk->diskState = STATE_CLOSED;
            pDisk->openCount = 0;

            ioreq.irHandle = BSP_MDOC_USERSTORE_PARTITION_HANDLE;
            status = flDismountVolume(&ioreq);
            if (status != flOK)
            {
                DBGMSG(ZONE_OPEN | ZONE_ERROR, (TEXT("TrueFFS, DSK_Close(): Failed to dismount media.\r\n")));
                goto Leave;
            }
            pDisk->numberOfMounts--;
        }
    }   

    DBGMSG(ZONE_OPEN, (TEXT("TrueFFS, DSK_Close(): [END], returning TRUE\r\n")));

    retVal = TRUE;

Leave:
    LeaveCriticalSection(&pDisk->cs);
    return retVal;
}



//----------------------------------------------------------------------------
// Function Name: DSK_Deinit                                                  
// Purpose......: Uninitialize                                                
// Returns......: TRUE                                                        
//----------------------------------------------------------------------------
BOOL DSK_Deinit(DWORD dwData)
{
    PDISK pDisk = NULL;

    DBGMSG(ZONE_INIT, (TEXT("TrueFFS, DSK_INIT(): DSK_Deinit\r\n")));

    // Validate the caller.
    //
    // This driver allows only kernel mode access.
    // So in order to reduce attack surface and the need to marshal embedded pointers,
    // all user-mode access will be blocked here
    if(GetDirectCallerProcessId() != GetCurrentProcessId())
    {
        SetLastError(ERROR_ACCESS_DENIED);
        DBGMSG(ZONE_INIT | ZONE_ERROR, (TEXT("TrueFFS, DeInit(): Received user mode request. Returning FALSE.")));
        return FALSE;        
    }

    pDisk = (PDISK) dwData;

    // Validate the pDisk arguement
    if(IsValidDisk(pDisk) == FALSE)
    {       
        DBGMSG(ZONE_INIT | ZONE_ERROR, (TEXT("TrueFFS, DeInit(): Recieved pDIsk is invalid. Returning FALSE.\r\n")));
        return FALSE;
    }
    
    while (pDisk->openCount > 0)
        DSK_Close((DWORD)pDisk);

    pDisk->diskState = STATE_UNINITIALIZED;
    
    DBGMSG(ZONE_INIT, (TEXT("TrueFFS, DSK_INIT(): DSK_Deinit [END]\r\n")));
    
    return TRUE;
}


//----------------------------------------------------------------------------
// Function Name: DSK_IOControl                                               
// Purpose......: Handle IOCTL requests                                       
//                The read and write requesrs take a scatter/gather list in   
//                pInBuf                                                      
// Returns......: TRUE on success, FALSE otherwise                            
//----------------------------------------------------------------------------
BOOL DSK_IOControl(DWORD  Handle,
                   DWORD  dwIoControlCode,
                   PBYTE  pInBuf,
                   DWORD  dwInBufSize,
                   PBYTE  pOutBuf,
                   DWORD  dwOutBufSize,
                   PDWORD pBytesReturned)
{
    PDISK pDisk;
    DWORD status;
    BOOL retStatus = FALSE;
    IOreq ioreq;
    FLStatus flStatus;
        
    DBGMSG(ZONE_IO, (TEXT("TrueFFS, IO: dwIoControlCode=%d \r\n"), dwIoControlCode));

    // Validate the caller.
    //
    // This driver allows only kernel mode access.
    // So in order to reduce attack surface and the need to marshal embedded pointers,
    // all user-mode access will be blocked here
    if(GetDirectCallerProcessId() != GetCurrentProcessId())
    {
        SetLastError(ERROR_ACCESS_DENIED);
        DBGMSG(ZONE_IO | ZONE_ERROR, (TEXT("TrueFFS, IO(): Received user mode request, command=%d. Returning FALSE."), dwIoControlCode));
        return FALSE;        
    }

    pDisk = (PDISK)Handle;

    // Validate the pDisk arguement
    if(IsValidDisk(pDisk) == FALSE)
    {       
        DBGMSG(ZONE_IO | ZONE_ERROR, (TEXT("TrueFFS, IO(): Recieved pDIsk is invalid. Returning FALSE.\r\n")));
        return FALSE;
    }

    EnterCriticalSection(&pDisk->cs);        
    
    if(pDisk->diskState != STATE_OPENED)
    {
        SetLastError(ERROR_GEN_FAILURE);
        DBGMSG(ZONE_IO | ZONE_ERROR, (TEXT("TrueFFS, IO():  Recieved PDISK is not opened. Returning FALSE.\r\n")));
        goto Leave;
    }

    // Take care of the called IOCTL
    switch(dwIoControlCode)
    {
        case IOCTL_DISK_DEVICE_INFO:
        {
            long keylength;
            PSTORAGEDEVICEINFO  psdi;
            DBGMSG(ZONE_IO, (TEXT("TrueFFS, IO: processing IOCTL_DISK_DEVICE_INFO\r\n")));

            if (dwInBufSize < sizeof (STORAGEDEVICEINFO) ||
                pInBuf==NULL)
            {
                DBGMSG(ZONE_IO | ZONE_ERROR, (TEXT("TrueFFS, checkCommandParams(): IOCTL_DISK_DEVICE_INFO Recieved bad parameters.\r\n")));
                status = ERROR_INVALID_PARAMETER;
                break;
            }
            
            try
            {
                psdi  = (PSTORAGEDEVICEINFO)pInBuf;
                psdi->cbSize = sizeof(STORAGEDEVICEINFO);
                if (!getRegValue(pDisk->registryPath,(PUCHAR)psdi->szProfile, PROFILENAMESIZE, &keylength, PROFILE_REGISTRY_NAME))
                {
                    wcscpy( psdi->szProfile, TRUEFFS_DEFAULT_PROFILE);            
                }
                psdi->dwDeviceClass = STORAGE_DEVICE_CLASS_BLOCK;
                psdi->dwDeviceType = STORAGE_DEVICE_TYPE_DOC;
                psdi->dwDeviceFlags = STORAGE_DEVICE_FLAG_READWRITE;

                if (pBytesReturned != NULL)
                {
                    *pBytesReturned = sizeof(STORAGEDEVICEINFO);
                }

                SetLastError(ERROR_SUCCESS);
                retStatus = TRUE;
            }
            except(EXCEPTION_EXECUTE_HANDLER)
            {
                DBGMSG(ZONE_ERROR, (TEXT("TrueFFS, IO: IOCTL_DISK_DEVICE_INFO Recieved exception. probably due to bad output pointer .\r\n")));
                SetLastError(ERROR_INVALID_PARAMETER);
                retStatus = FALSE;
            }
            break;
        }

        case IOCTL_DISK_GET_STORAGEID:
        {
            PSTORAGE_IDENTIFICATION sid;
            DBGMSG(ZONE_IO, (TEXT("TrueFFS, IO: processing IOCTL_DISK_GET_STORAGEID\r\n")));

            if (dwOutBufSize < sizeof(STORAGE_IDENTIFICATION) || 
                pOutBuf == NULL)
            {
                DBGMSG(ZONE_IO | ZONE_ERROR, (TEXT("TrueFFS, checkCommandParams(): IOCTL_DISK_GET_STORAGEID Recieved bad parameters.\r\n")));
                status = ERROR_INVALID_PARAMETER;
                break;
            }
                
            try
            {
                sid = (PSTORAGE_IDENTIFICATION)pOutBuf;
                sid->dwFlags = MANUFACTUREID_INVALID | SERIALNUM_INVALID;
                sid->dwManufactureIDOffset = 0; 
                sid->dwSerialNumOffset =0;

                if (pBytesReturned != NULL)
                {
                    *pBytesReturned = sizeof(STORAGE_IDENTIFICATION);
                }

                SetLastError(ERROR_SUCCESS);
                retStatus = TRUE;
            }
            except(EXCEPTION_EXECUTE_HANDLER)
            {

⌨️ 快捷键说明

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