Share via


Implementing the OEMWriteFlash Function (Windows Embedded CE 6.0)

1/5/2010

The OEMWriteFlash function writes the data temporarily cached in RAM to its final location in flash memory.

This function should also verify that the write operation occurred successfully.

Following the write operation, the OEMWriteFlash function can also be implemented to lock the flash memory blocks to protect against accidental erasure.

To implement the OEMWriteFlash function

  • Edit the file Flash.c by adding the code necessary to fully implement the OEMWriteFlash function.

    The following code example shows the implementation of the OEMWriteFlash function for the hardware platform used in this boot loader example.

    BOOL OEMWriteFlash(DWORD dwImageStart, DWORD dwImageLength)
    {
        DWORD dwFlashAddr, dwExtraBytes = 0;
        LPBYTE pbCache = NULL;
        UCHAR nNumBlocks = 0;
    
        //
        // Make sure the start and end addresses are in flash.
        //
        if (!OEMIsFlashAddr(dwImageStart) || !OEMIsFlashAddr(dwImageStart + dwImageLength - 1))
        {
            EdbgOutputDebugString("ERROR: OEMWriteFlash - not a flash address (0x%x or 0x%x).\r\n", dwImageStart, (dwImageStart + dwImageLength - 1));
            return(FALSE);
        }
    
        //
        // Make sure image start is on a block boundary.
        //
        if (dwImageStart % FLASH_BLOCK_SIZE)
        {
            EdbgOutputDebugString("ERROR: OEMWriteFlash - image start address isn't on a flash block boundary (0x%x).\r\n", dwImageStart);
            return(FALSE);
        }
    
        EdbgOutputDebugString ("INFO: Writing to FLASH: Start = 0x%X  Length = 0x%X...\r\n", dwImageStart, dwImageLength);
    
        nNumBlocks   = (UCHAR)(dwImageLength / FLASH_BLOCK_SIZE);
        dwExtraBytes = (dwImageLength % FLASH_BLOCK_SIZE);
        dwFlashAddr  = dwImageStart;
        pbCache      = OEMMapMemAddr (dwImageStart, dwFlashAddr);
    
        //
        // Write flash blocks.
        // 
        while(nNumBlocks)
        { 
            EdbgOutputDebugString("Writing to Block %d of %d:\r\n", BLOCK_NUM(dwFlashAddr), gnBlocks);
    
            if (CFI_Write_Block((unsigned32*)dwFlashAddr, (unsigned32*)pbCache, FLASH_BLOCK_SIZE, NULL) != PASS)
            {
                EdbgOutputDebugString("ERROR: OEMWriteFlash - unable to write to block (block address=0x%x).\r\n", dwFlashAddr);
                return(FALSE);
            }
    
            dwFlashAddr += FLASH_BLOCK_SIZE;
            pbCache = OEMMapMemAddr (dwImageStart, dwFlashAddr);
            --nNumBlocks;
        }
    
        //
        // If a partial block remains, write it to memory.
        //
        if (dwExtraBytes)
        {
            if (CFI_Write_Block((unsigned32*)dwFlashAddr, (unsigned32*)pbCache, dwExtraBytes, NULL) != PASS)
            {
                EdbgOutputDebugString("ERROR: OEMWriteFlash - unable to write to block (block address=0x%x).\r\n", dwFlashAddr);
                return(FALSE);
            }
        }
    
        return(TRUE);
    }
    

See Also

Tasks

How to Develop a Boot Loader