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

📄 falmain.cpp

📁 基于WINCE的文件系统FAL驱动
💻 CPP
📖 第 1 页 / 共 4 页
字号:
            }
            break;


        case DISK_IOCTL_GETINFO:case  IOCTL_DISK_GETINFO:
            DEBUGMSG(ZONE_FUNCTION,(TEXT("FLASHDRV.DLL:DSK_IOControl(DISK_IOCTL_GETINFO)\r\n")));

            //----- Retrieve the storage information for this FLASH device -----
            if(!(fRet = GetDiskInfo((PDISK_INFO)(dwIoControlCode!=IOCTL_DISK_GETINFO?pInBuf:pOutBuf))))
            {
                ReportError((TEXT("FLASHDRV.DLL:ERROR - Unable to get disk information from FMD!\r\n")));
                goto IO_EXIT;
            }
            break;


        case DISK_IOCTL_SETINFO: case IOCTL_DISK_SETINFO:
            DEBUGMSG(ZONE_FUNCTION,(TEXT("FLASHDRV.DLL:DSK_IOControl(DISK_IOCTL_SETINFO)\r\n")));

            //----- The FLASH media is a fixed size; hence, nothing needs to be done -----
            break;


        case DISK_IOCTL_GETNAME:case IOCTL_DISK_GETNAME:
            SetLastError(ERROR_NOT_SUPPORTED);
            break;


        case DISK_IOCTL_FORMAT_MEDIA:case IOCTL_DISK_FORMAT_MEDIA:
            DEBUGMSG(ZONE_FUNCTION,(TEXT("FLASHDRV.DLL:DSK_IOControl(DISK_IOCTL_FORMAT_MEDIA)\r\n")));

            //----- Format the media on the FLASH device -----
            if(!(fRet = FormatMedia()))
            {
                ReportError((TEXT("FLASHDRV.DLL:ERROR - Unable to format FLASH media!\r\n")));
                goto IO_EXIT;
            }
            break;
        case IOCTL_DISK_DEVICE_INFO:
        {
            fRet = GetDeviceInfo((PSTORAGEDEVICEINFO)pInBuf);
            if (pBytesReturned)
                *pBytesReturned = sizeof(STORAGEDEVICEINFO);
            break;
        }

        case IOCTL_DISK_DELETE_SECTORS:
            DEBUGMSG(ZONE_FUNCTION, (TEXT("FLASHDRV.DLL:DSK_IOControl(IOCTL_DISK_DELETE_SECTORS)\r\n")));
            //----- Delete the requested sectors -----
            if(!(fRet = DeleteSectors((PDELETE_SECTOR_INFO)pInBuf)))
            {
                ReportError((TEXT("FLASHDRV.DLL:ERROR - DeleteSectors() failed.\r\n")));
                goto IO_EXIT;
            }
            break;

/* ctg
        case IOCTL_DISK_SECURE_WIPE:
            DEBUGMSG(ZONE_FUNCTION, (TEXT("FLASHDRV.DLL:DSK_IOControl(IOCTL_DISK_SECURE_WIPE)\r\n")));
            //----- Secure wipe the flash region  -----
            if(!(fRet = SecureWipe((PDELETE_SECTOR_INFO)pInBuf)))
            {
                ReportError((TEXT("FLASHDRV.DLL:ERROR - SecureWipe() failed.\r\n")));
                goto IO_EXIT;
            }
            break;

        case IOCTL_DISK_SET_SECURE_WIPE_FLAG:
            DEBUGMSG(ZONE_FUNCTION, (TEXT("FLASHDRV.DLL:DSK_IOControl(IOCTL_DISK_SET_SECURE_WIPE_FLAG)\r\n")));
            //----- Set the secure wipe flag for the flash region  -----
            if(!(fRet = SetSecureWipeFlag((PDELETE_SECTOR_INFO)pInBuf)))
            {
                ReportError((TEXT("FLASHDRV.DLL:ERROR - SetSecureWipeFlag() failed.\r\n")));
                goto IO_EXIT;
            }
            break;
*/

        case IOCTL_DISK_GET_SECTOR_ADDR:
        {
            DEBUGMSG(ZONE_FUNCTION,(TEXT("FLASHDRV.DLL:DSK_IOControl(IOCTL_DISK_GET_SECTOR_ADDR)\r\n")));

            //----- Service this scatter/gather READ request -----
            if(!(fRet = GetPhysSectorAddr((PSECTOR_ADDR)pInBuf, (PSECTOR_ADDR)pOutBuf, nInBufSize / sizeof(SECTOR_ADDR))))
            {
                ReportError((TEXT("FLASHDRV.DLL:ReadFromMedia() failed.\r\n")));
            }
            break;
        }
        case IOCTL_POWER_CAPABILITIES:
            DEBUGMSG(ZONE_FUNCTION, (TEXT("FLASHDRV.DLL:DSK_IOControl(IOCTL_POWER_CAPABILITIES)\r\n")));
            if(!(fRet = GetPowerCapabilities((PPOWER_CAPABILITIES)pOutBuf)))
            {
                ReportError((TEXT("FLASHDRV.DLL:ERROR - GetPowerCapabilities() failed.\r\n")));
                goto IO_EXIT;
            }
            *pBytesReturned = sizeof(POWER_CAPABILITIES);
            break;

        case IOCTL_POWER_SET:
            DEBUGMSG(ZONE_FUNCTION, (TEXT("FLASHDRV.DLL:DSK_IOControl(IOCTL_POWER_SET)\r\n")));
            if(!(fRet = SetPowerState((PCEDEVICE_POWER_STATE)pOutBuf)))
            {
                ReportError((TEXT("FLASHDRV.DLL:ERROR - SetPowerState() failed.\r\n")));
                goto IO_EXIT;
            }
            *pBytesReturned = sizeof(CEDEVICE_POWER_STATE);
            break;

        case IOCTL_POWER_GET:
            DEBUGMSG(ZONE_FUNCTION, (TEXT("FLASHDRV.DLL:DSK_IOControl(IOCTL_POWER_GET)\r\n")));
            if(!(fRet = GetPowerState((PCEDEVICE_POWER_STATE)pOutBuf)))
            {
                ReportError((TEXT("FLASHDRV.DLL:ERROR - GetPowerState() failed.\r\n")));
                goto IO_EXIT;
            }
            *pBytesReturned = sizeof(CEDEVICE_POWER_STATE);
            break;

    }
    }
    __except (EXCEPTION_EXECUTE_HANDLER)
    {
        SetLastError(ERROR_INVALID_PARAMETER);
    }

IO_EXIT:

    LeaveCriticalSection(&g_csFlashDevice);
    LeaveCriticalSection(&g_csMain);
//	RETAILMSG(1,(TEXT("DSK_IOControl(%X) stop:%d\r\n"),dwIoControlCode,GetTickCount() ));
    return fRet;

PARAMETER_ERROR:
    SetLastError(ERROR_INVALID_PARAMETER);
    LeaveCriticalSection(&g_csFlashDevice);
    LeaveCriticalSection(&g_csMain);
    return FALSE;

}


/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function:       DSK_PowerUp()

Description:    Performs the necessary powerup procedures.

Returns:        N/A
-------------------------------------------------------------------*/
VOID DSK_PowerUp(VOID)
{
    // Invoke the FMD to perform any necessary powerup procedures...
    FMD.pPowerUp();
}


/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function:       DSK_PowerDown()

Description:    Performs the necessary powerdown procedures.

Returns:        N/A
-------------------------------------------------------------------*/
VOID DSK_PowerDown(VOID)
{
    // Invoke the FMD to perform any necessary powerdown procedures...
    FMD.pPowerDown();
}


//------------------------------- Helper Functions ----------------------------------------


VOID GetFMDInterface(PDEVICE pDevice)
{
    FMD.cbSize = sizeof(FMDInterface);

    // query FMD intrface from the FMD
    FMD.pOEMIoControl = FMD_OEMIoControl;
    if (!FMD_OEMIoControl (IOCTL_FMD_GET_INTERFACE, NULL, 0, (PBYTE)&FMD, sizeof(FMDInterface), NULL))
    {
        // FMD does not support IOCTL_FMD_GET_INTERFACE, so build the FMDInterface
        // structure using the legacy FMD functions
        FMD.pInit = FMD_Init;
        FMD.pDeInit = FMD_Deinit;
        FMD.pGetInfo = FMD_GetInfo;
        FMD.pGetBlockStatus = FMD_GetBlockStatus;
        FMD.pSetBlockStatus = FMD_SetBlockStatus;
        FMD.pReadSector = FMD_ReadSector;
        FMD.pWriteSector = FMD_WriteSector;
        FMD.pEraseBlock = FMD_EraseBlock;
        FMD.pPowerUp = FMD_PowerUp;
        FMD.pPowerDown = FMD_PowerDown;
    }

    // query hook library in case any FMD functions need to be shimmed
    pDevice->hFMDHook = FMDHOOK_HookInterface(&FMD);
}

Fal* GetFALObject (DWORD dwStartSector, DWORD dwNumSectors)
{
    for (DWORD iRegion = 0; iRegion < g_pFlashMediaInfo->dwNumRegions; iRegion++) {
        if (g_FalObjects[iRegion]->IsInRange (dwStartSector, dwNumSectors)) {
            return g_FalObjects[iRegion];
        }
    }
    return NULL;
}

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function:       GetDiskInfo()

Description:    Retrieves disk information for the underlying FLASH media.

Notes:          The storage information for the FLASH media is retrieved by
                FMD_GetInfo() at driver initialization.

Returns:        Boolean indicating success.
-------------------------------------------------------------------------------*/
BOOL GetDiskInfo(PDISK_INFO pDiskInfo)
{
    pDiskInfo->di_total_sectors  = g_dwAvailableSectors;
    pDiskInfo->di_bytes_per_sect = g_pFlashMediaInfo->dwDataBytesPerSector;
    // TODO: Calculate correctly
    pDiskInfo->di_cylinders      = g_pFlashMediaInfo->dwNumBlocks;
    pDiskInfo->di_heads          = 1;
    pDiskInfo->di_sectors        = g_pFlashMediaInfo->region[0].dwSectorsPerBlock;
    pDiskInfo->di_flags          = DISK_INFO_FLAG_PAGEABLE;

    return TRUE;
}


BOOL CheckSg (PSG_REQ pSG_req, BOOL fRead, LPBOOL pfCombineSg, LPDWORD pdwTotalSize)
{
    DWORD dwSGBuffNum = 0;
    DWORD dwTotalSize = 0;

    *pfCombineSg = FALSE;
    *pdwTotalSize = 0;

    //----- 1. Parse the scatter/gather request to determine if it can be fulfilled -----
    pSG_req->sr_status = ERROR_IO_PENDING;

    //----- For debugging purposes, print out the READ request parameters... -----
    if (fRead ? ZONE_READ_OPS : ZONE_WRITE_OPS) {
        DEBUGMSG(ZONE_READ_OPS, (TEXT("FLASHDRV.DLL:%s() request.\r\n"), fRead ? TEXT("Read") : TEXT("Write")));
        DEBUGMSG(ZONE_READ_OPS, (TEXT("             Start Sector = %x\r\n"), pSG_req->sr_start));
        DEBUGMSG(ZONE_READ_OPS, (TEXT("             # of Sectors = %d\r\n"), pSG_req->sr_num_sec));
        DEBUGMSG(ZONE_READ_OPS, (TEXT("             # of SG bufs = %d\r\n"), pSG_req->sr_num_sg));
        DEBUGMSG(ZONE_READ_OPS, (TEXT("                 BUFF LEN = %d\r\n"), pSG_req->sr_sglist[0].sb_len));
    }

    if((pSG_req->sr_num_sg <= 0) || (pSG_req->sr_num_sg > MAX_SG_BUF))      // Is the # of scatter/gather buffers legitimate?
    {
        ReportError((TEXT("FLASHDRV.DLL:%s() - invalid parameter: pSG_req->sr_num_sg=%d\r\n"), fRead ? TEXT("Read") : TEXT("Write"), pSG_req->sr_num_sg));
        pSG_req->sr_status = ERROR_INVALID_PARAMETER;
        return FALSE;
    }

    if(pSG_req->sr_num_sec <= 0)                                            // Is the request for 0 sectors?
    {
        ReportError((TEXT("FLASHDRV.DLL:%s() - request for 0 sectors\r\n"), fRead ? TEXT("Read") : TEXT("Write")));
        pSG_req->sr_status = ERROR_INVALID_PARAMETER;
        return FALSE;
    }

    if((pSG_req->sr_start + pSG_req->sr_num_sec) > g_dwAvailableSectors)    // Does this request "run" off the end of the media?
    {
        ReportError((TEXT("FLASHDRV.DLL:%s() - request runs off end of media\r\n"), fRead ? TEXT("Read") : TEXT("Write")));
        pSG_req->sr_status = ERROR_SECTOR_NOT_FOUND;
        return FALSE;
    }


    for(dwSGBuffNum = 0; dwSGBuffNum < pSG_req->sr_num_sg; dwSGBuffNum++)          // Check each scatter/gather buffer...
    {
        if((pSG_req->sr_sglist[dwSGBuffNum].sb_len <= 0) || (pSG_req->sr_sglist[dwSGBuffNum].sb_buf == NULL))
        {
            ReportError((TEXT("FLASHDRV.DLL:%s() - scatter/gather buffer %d isn't valid\r\n"), fRead ? TEXT("Read") : TEXT("Write"), dwSGBuffNum));
            pSG_req->sr_status = ERROR_INVALID_PARAMETER;
            return FALSE;
        }
        if (((pSG_req->sr_sglist[dwSGBuffNum].sb_len % g_pFlashMediaInfo->dwDataBytesPerSector) != 0))
        {
            *pfCombineSg = TRUE;
        }

        if (dwTotalSize + pSG_req->sr_sglist[dwSGBuffNum].sb_len < dwTotalSize)
        {
            // Integer overflow
            ReportError((TEXT("FLASHDRV.DLL - Integer overflow when calculating total buffer size.\r\n")));
            pSG_req->sr_status = ERROR_INVALID_PARAMETER;
            return FALSE;
        }

        dwTotalSize += pSG_req->sr_sglist[dwSGBuffNum].sb_len;
    }

    if (!dwTotalSize || (dwTotalSize % g_pFlashMediaInfo->dwDataBytesPerSector)) {
        ReportError((TEXT("FLASHDRV.DLL:%s() - Sum of buffer lengths (%d) is not a sector multiple.\r\n"), fRead ? TEXT("Read") : TEXT("Write"), dwTotalSize));
        pSG_req->sr_status = ERROR_INVALID_PARAMETER;
        return FALSE;
    }

    if (dwTotalSize / g_pFlashMediaInfo->dwDataBytesPerSector != pSG_req->sr_num_sec)
    {
        ReportError((TEXT("FLASHDRV.DLL:%s() - Sum of buffer lengths (%d) is not equal to number of sectors.\r\n"), fRead ? TEXT("Read") : TEXT("Write"), dwTotalSize));
        pSG_req->sr_status = ERROR_INVALID_PARAMETER;
        return FALSE;
    }

    *pdwTotalSize = dwTotalSize;
    return TRUE;
}


BOOL GetPhysSectorAddr (PSECTOR_ADDR pLogicalSectors, PSECTOR_ADDR pPhysAddrs, DWORD dwNumSectors)
{
    SECTOR_ADDR physicalSectorAddr;
    DWORD i;

    Fal* pFAL = GetFALObject (pLogicalSectors[0], 1);
    if (!pFAL) {
        ReportError((TEXT("FLASHDRV.DLL:GetPhysSectorAddress() - GetFALObject failed\r\n")));
        return FALSE;
    }

    for (i = 0; i < dwNumSectors; i++)
    {
        if (pFAL->m_pMap->GetPhysicalSectorAddr(pLogicalSectors[i], &physicalSectorAddr) && (physicalSectorAddr != UNMAPPED_LOGICAL_SECTOR) &&
            FMD.pGetPhysSectorAddr)
        {
            FMD.pGetPhysSectorAddr(physicalSectorAddr, &pPhysAddrs[i]);
        }
        else
        {
            pPhysAddrs[i] = UNMAPPED_LOGICAL_SECTOR;
        }
    }
    return TRUE;
}

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function:       InternalReadFromMedia()

Description:    Performs the specified scatter/gather READ request from the media.

Notes:          After parsing the parameters, the actual READ request is handled
                by the FLASH Media Driver (FMD).

Returns:        Boolean indicating success.
-------------------------------------------------------------------------------*/
BOOL ReadFromMedia(PSG_REQ pSG_req)
{
    static PUCHAR       pBuff             = NULL;
    static SG_REQ         sgNew;
    static DWORD        dwSGBuffNum     = 0;
    DWORD                dwTotalSize        = 0;
    BOOL                 fRet             = FALSE;
    BOOL                 fCombineSg            = FALSE;
    Fal* pFAL = NULL;

    if (!CheckSg (pSG_req, TRUE, &fCombineSg, &dwTotalSize)) {
        goto READ_ERROR;
    }

    pFAL = GetFALObject (pSG_req->sr_start, pSG_req->sr_num_sec);
    if (!pFAL) {
        ReportError((TEXT("FLASHDRV.DLL:ReadFromMedia() - GetFALObject failed\r\n")));
        pSG_req->sr_status = ERROR_INVALID_PARAMETER;
        goto READ_ERROR;

⌨️ 快捷键说明

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