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

📄 fal.cpp

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

    }

    pSG_req->sr_status = ERROR_SUCCESS;
    return TRUE;
}


BOOL Fal::IsInRange (DWORD dwStartSector, DWORD dwNumSectors)
{
    return (dwStartSector >= m_dwStartLogSector &&
           ((dwStartSector + dwNumSectors) <= (m_dwStartLogSector + m_dwNumLogSectors)));
}

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function:       StartupFAL()

Description:    Starts up the FLASH Abstraction Layer (FAL)

Notes:          The FLASH Media Driver (FMD) needs to be completely
                initialized before this function is called.  If
                startup fails, the caller is responsible for "cleaning
                things up" by calling ShutdownFAL().

Returns:        Boolean indicating success.
-------------------------------------------------------------------*/
BOOL Fal::StartupFAL(PFlashRegion pRegion)
{
RETAILMSG(1,(TEXT("Initialize the FAL StartupFAL start:%d\r\n"),GetTickCount() ));

    //----- 1. Cache size properties of the FLASH media -----
    m_dwSectorsPerBlock = pRegion->dwSectorsPerBlock;
    m_pRegion = pRegion;
    m_dwNumLogSectors = pRegion->dwNumLogicalBlocks * m_dwSectorsPerBlock;

    //----- 2. Initialize the LOGICAL -> PHYSICAL sector mapper -----
    if (!m_pMap->Init (pRegion))
    {
        ReportError((TEXT("FLASHDRV.DLL:StartupFAL() - Unable to initialize Logical --> Physical Mapper\r\n")));
        return FALSE;
    }
RETAILMSG(1,(TEXT("Initialize the FAL StartupFAL mid:%d\r\n"),GetTickCount() ));

    //----- 3. Build the logical --> physical lookup table, free list, etc. -----
    if(!BuildupMappingInfo())
    {
        ReportError((TEXT("FLASHDRV.DLL:StartupFAL() - Unable to build the logical --> physical lookup table, free list, etc.!!!\r\n")));
        return FALSE;
    }

RETAILMSG(1,(TEXT("Initialize the FAL StartupFAL stop:%d\r\n"),GetTickCount() ));
    return TRUE;
}



/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function:       ShutdownFAL()

Description:    Shuts down the FLASH Abstraction Layer (FAL)

Notes:          The logical --> physical mapper and sector manager
                are appropriately deinitialized.  Note that the caller
                is responsible for deinitializing the FMD.

Returns:        Boolean indicating success.
-------------------------------------------------------------------*/
BOOL Fal::ShutdownFAL()
{
    //----- 1. Deinitialize the LOGICAL -> PHYSICAL sector mapper -----
    if (!m_pMap->Deinit())
    {
        ReportError((TEXT("FLASHDRV.DLL:ShutdownFAL() - Unable to deinitialize Logical --> Physical Mapper\r\n")));
    }

    //----- 2. Deinitialize the sector manager -----
    if (!m_pSectorMgr->Deinit())
    {
        ReportError((TEXT("FLASHDRV.DLL:ShutdownFAL() - Unable to deinitialize Sector Manager\r\n")));
    }

    return TRUE;
}


BOOL XipFal::StartupFAL(PFlashRegion pRegion)
{

    if (!m_pSectorMgr->Init (pRegion, this, NULL))
    {
        ReportError((TEXT("FLASHDRV.DLL:StartupFAL() - Unable to initialize Sector Manager\r\n")));
        return FALSE;
    }

    return Fal::StartupFAL(pRegion);
}


/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function:       BuildupMappingInfo()

Description:    Reads the sector info for each sector on the media
                and builds up the MAPPING, FREE, and DIRTY sector info.

Returns:        Boolean indicating success.
-------------------------------------------------------------------*/
BOOL XipFal::BuildupMappingInfo()
{
    DWORD dwBlockID = 0;
    DWORD dwPhysSector = m_dwStartPhysSector;
    DWORD dwLogSector = m_dwStartLogSector;
    DWORD dwExistingPhysSector;

    DEBUGMSG(1, (TEXT("FLASHDRV.DLL:BuildupMappingInfo() - Enter. \r\n"), dwBlockID));

    for (dwBlockID = m_pRegion->dwStartPhysBlock; dwBlockID < m_pRegion->dwStartPhysBlock + m_pRegion->dwNumPhysBlocks; dwBlockID++)
    {
        DWORD iSector;
        DWORD dwStatus = FMD.pGetBlockStatus (dwBlockID);

        if (dwStatus & (BLOCK_STATUS_RESERVED | BLOCK_STATUS_BAD))
        {
            if(!m_pSectorMgr->MarkBlockUnusable(dwBlockID))
            {
                ReportError((TEXT("FLASHDRV.DLL:BuildupMappingInfo() - SectorMgr::MarkBlockUnusable(%d) failed. \r\n"), dwBlockID));
            }
            dwPhysSector += m_pRegion->dwSectorsPerBlock;
            continue;
        }
        else if (dwStatus & BLOCK_STATUS_READONLY)
        {
            if(!m_pSectorMgr->AddSectorsToList (SectorMgr::ReadOnly, dwLogSector, m_pRegion->dwSectorsPerBlock))
            {
                ReportError((TEXT("FLASHDRV.DLL:BuildupMappingInfo() - Unable to mark sector %x as read only.\r\n"), dwPhysSector));
            }
        }
        else
        {
            if(!m_pSectorMgr->AddSectorsToList (SectorMgr::XIP, dwLogSector, m_pRegion->dwSectorsPerBlock))
            {
                ReportError((TEXT("FLASHDRV.DLL:BuildupMappingInfo() - Unable to mark sector %x as XIP.\r\n"), dwPhysSector));
            }
        }


        // Map logical sectors 1:1 with physical sectors
        for (iSector = 0; iSector < m_pRegion->dwSectorsPerBlock; iSector++)
        {
            if(!m_pMap->MapLogicalSector(dwLogSector + iSector, dwPhysSector + iSector, &dwExistingPhysSector))
            {
                ReportError((TEXT("FLASHDRV.DLL:BuildupMappingInfo() - Unable to map logical sector 0x%08x to physical sector 0x%08x\r\n"),
                                            dwLogSector + iSector, dwPhysSector + iSector));
            }
        }

        dwLogSector += m_pRegion->dwSectorsPerBlock;
        dwPhysSector += m_pRegion->dwSectorsPerBlock;
    }

    return TRUE;
}


DWORD XipFal::InternalWriteToMedia(DWORD dwStartSector, DWORD dwNumSectors, LPBYTE pBuffer)
{
    LPBYTE pbBlock = NULL;
    DWORD dwError = ERROR_GEN_FAILURE;
    DWORD iSector;

    DWORD dwNumSectorsLeft = dwNumSectors;
    DWORD dwCurSector = dwStartSector;

    while (dwNumSectorsLeft)
    {
        DWORD dwPhysSector, dwPhysBlock, dwBlockOffset;
        LPBYTE pTmpBuffer = pBuffer;
        DWORD dwSectorsToWrite = m_pRegion->dwSectorsPerBlock;

        if (!m_pMap->GetPhysicalSectorAddr (dwCurSector, &dwPhysSector))
        {
            ReportError((TEXT("FLASHDRV.DLL:WriteXIPSectors() - Could not get physical sector for logical sector 0x%x.\r\n"), dwCurSector));
            dwError = ERROR_INVALID_PARAMETER;
            goto XIP_ERROR;
        }

        dwPhysBlock = dwPhysSector / m_pRegion->dwSectorsPerBlock;
        dwBlockOffset = dwPhysSector % m_pRegion->dwSectorsPerBlock;

        if (dwBlockOffset || (dwNumSectorsLeft < m_pRegion->dwSectorsPerBlock))
        {
            // If the write does not contain the entire block, read in the block,
            // apply to partial change to the buffer, erase the block, and write
            // it back out.

            if (!pbBlock)
            {
                pbBlock = (LPBYTE)LocalAlloc (LMEM_FIXED, m_pRegion->dwBytesPerBlock);
                if (!pbBlock)
                {
                    dwError = ERROR_OUTOFMEMORY;
                    goto XIP_ERROR;
                }
            }

            pTmpBuffer = pbBlock;

            //
            // Read the block in
            //

            for (iSector = 0; iSector < m_pRegion->dwSectorsPerBlock; iSector++)
            {
                if (!FMD.pReadSector(dwPhysBlock * m_pRegion->dwSectorsPerBlock + iSector, pTmpBuffer, NULL, 1))
                {
                    ReportError((TEXT("FLASHDRV.DLL:WriteXIPSectors() - Read sector failed for 0x%x.\r\n"), dwPhysBlock * m_pRegion->dwSectorsPerBlock + iSector));
                    goto XIP_ERROR;
                }
                pTmpBuffer += g_pFlashMediaInfo->dwDataBytesPerSector;
            }

            dwSectorsToWrite = MIN (dwNumSectorsLeft, m_pRegion->dwSectorsPerBlock - dwBlockOffset);

            memcpy (pbBlock + dwBlockOffset * g_pFlashMediaInfo->dwDataBytesPerSector, pBuffer, dwSectorsToWrite * g_pFlashMediaInfo->dwDataBytesPerSector);

            pTmpBuffer = pbBlock;

        }

        if (!FMD.pEraseBlock (dwPhysBlock))
        {
            ReportError((TEXT("FLASHDRV.DLL:WriteXIPSectors() - Erase block failed for 0x%x.\r\n"), dwPhysBlock));
            goto XIP_ERROR;
        }

        //
        // Write the entire block out
        //

        for (iSector = 0; iSector < m_pRegion->dwSectorsPerBlock; iSector++)
        {
            if (!FMD.pWriteSector(dwPhysBlock * m_pRegion->dwSectorsPerBlock + iSector, pTmpBuffer, NULL, 1))
            {
                ReportError((TEXT("FLASHDRV.DLL:WriteXIPSectors() - Write sector failed for 0x%x.\r\n"), dwPhysBlock * m_pRegion->dwSectorsPerBlock + iSector));
                goto XIP_ERROR;
            }
            pTmpBuffer += g_pFlashMediaInfo->dwDataBytesPerSector;
        }

        pBuffer += dwSectorsToWrite * g_pFlashMediaInfo->dwDataBytesPerSector;
        dwCurSector += dwSectorsToWrite;
        dwNumSectorsLeft -= dwSectorsToWrite;

        DEBUGMSG(1, (TEXT("%d\n"), dwPhysBlock));
    }

    dwError = ERROR_SUCCESS;

XIP_ERROR:

    if (pbBlock)
        LocalFree (pbBlock);

    return dwError;

}

BOOL XipFal::DeleteSectors(DWORD dwStartLogSector, DWORD dwNumSectors)
{
    return FALSE;
}

BOOL XipFal::SecureWipe()
{
    return FALSE;
}

DWORD XipFal::SetSecureWipeFlag(DWORD dwStartBlock)
{
    return INVALID_BLOCK_ID;
}

FileSysFal::FileSysFal (DWORD dwStartLogSector, DWORD dwStartPhysSector, BOOL fReadOnly) :
    Fal(dwStartLogSector, dwStartPhysSector, fReadOnly)
{
    m_pCompactor = new Compactor();
}


/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function:       StartupFAL()

Description:    Starts up the FLASH Abstraction Layer (FAL)

Notes:          The FLASH Media Driver (FMD) needs to be completely
                initialized before this function is called.  If
                startup fails, the caller is responsible for "cleaning
                things up" by calling ShutdownFAL().

Returns:        Boolean indicating success.
-------------------------------------------------------------------*/
BOOL FileSysFal::StartupFAL(PFlashRegion pRegion)
{

    //----- 3. Initialize the sector manager -----
    if (!m_pSectorMgr->Init (pRegion, this, m_pCompactor))
    {
        ReportError((TEXT("FLASHDRV.DLL:StartupFAL() - Unable to initialize Sector Manager\r\n")));
        return FALSE;
    }

⌨️ 快捷键说明

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