Example #1
0
//------------------------------------------------------------------------------
//
//  Function:  OEMWriteFlash
//
//  This function is called by the bootloader to write the image that may
//  be stored in a RAM file cache area to flash memory.
//
BOOL OEMWriteFlash(ULONG address, ULONG size) 
{
    BOOL rc = FALSE;
    VOID *pSource;

    OALMSG(OAL_FUNC, (L"+OEMWriteFlash(0x%08x, 0x%08x\r\n", address, size));

    // First get location where data are    
    pSource = OEMMapMemAddr(address, address);
    if (pSource == NULL) {
        OALMSG(OAL_ERROR, (
            L"ERROR: OEMMapMemAddr failed for 0x%08x\r\n", address
        ));
        goto cleanUp;
    }        

    OALMSG(OAL_WARN, (
        L"Write flash memory at 0x%08x size 0x%08x...", address, size
    ));

    rc = OALFlashWrite(
        OALPAtoUA(IMAGE_FLASH_PA_START), OALPAtoUA((VOID*)address), size, pSource
    );
    if (!rc) {
        OALMSG(OAL_ERROR, (L"\r\nERROR: Flash write failed\r\n"));
        goto cleanUp;
    }

    OALMSG(OAL_WARN, (L" Done\r\n"));
    
cleanUp:
    OALMSG(OAL_FUNC, (L"-OEMWriteFlash(rc = %d)\r\n", rc));
    return rc;
}
BOOL
WriteFlashNK(
    UINT32 address,
    UINT32 size
    )
{
	BOOL rc = FALSE;
	UCHAR *pData;
	void *pBase = OALPAtoVA(g_ulFlashBase, FALSE);
	void *pStart = (void *)((UINT32)pBase + IMAGE_WINCE_NOR_OFFSET);

	OALLog(L"\r\nWriting NK image to flash memory\r\n");

	// Get data location
	pData = OEMMapMemAddr(address, address);

	// Verify that we get CE image
	if (!VerifyImage(pData, NULL))
	{
		OALMSG(OAL_ERROR, (L"ERROR: OEMWriteFlash: NK image signature not found\r\n"));
		goto cleanUp;
	}

	// Unlock blocks
	if (!OALFlashLock(pBase, pStart, size, FALSE))
	{
		OALMSG(OAL_ERROR, (L"ERROR: OEMWriteFlash: NK blocks could not be unlocked\r\n"));
		goto cleanUp;
	}

	// Erase blocks
	if (!OALFlashErase(pBase, pStart, size))
	{
		OALMSG(OAL_ERROR, (L"ERROR: OEMWriteFlash: NK blocks could not be erased\r\n"));
		goto cleanUp;
	}

	// Write blocks
	if (!OALFlashWrite(pBase, pStart, size, pData))
	{
		OALMSG(OAL_ERROR, (L"ERROR: OEMWriteFlash: NK blocks could not be written\r\n"));
		goto cleanUp;
	}

	OALLog(L"NK image written\r\n");

	// Change boot device to NOR
	g_bootCfg.bootDevLoc.IfcType = Internal;
	g_bootCfg.bootDevLoc.LogicalLoc = BSP_NOR_REGS_PA;

	// Done
	rc = TRUE;

cleanUp:
	return rc;
}
BOOL
WriteFlashEBOOT(
    UINT32 address,
    UINT32 size
    )
{
	void *pData;
	void *pBase = OALPAtoVA(g_ulFlashBase, FALSE);
	void *pStart = (void *)((UINT32)pBase + IMAGE_EBOOT_NOR_OFFSET);
	BOOL rc = FALSE;

    OALMSG(OAL_INFO, (L"\r\nWriting EBOOT image to flash memory\r\n"));

    // Check if image fit (last sector used for configuration)
    if (size > IMAGE_EBOOT_CODE_SIZE)
	{
		OALMSG(OAL_ERROR, (L"ERROR: OEMWriteFlash: EBOOT image too big (size 0x%08x, maximum size 0x%08x)\r\n", size, IMAGE_EBOOT_CODE_SIZE));
		goto cleanUp;
	}

	// Get data location
	pData = OEMMapMemAddr(address, address);

	// Verify that we get CE image
	if (!VerifyImage(pData, NULL))
	{
		OALMSG(OAL_ERROR, (L"ERROR: OEMWriteFlash: EBOOT image signature not found\r\n"));
		goto cleanUp;
	}

	// Fill unused space with 0xFF
	if (size < IMAGE_EBOOT_CODE_SIZE)
	{
		memset((void *)((UINT32)pData + size), 0xFF, IMAGE_EBOOT_CODE_SIZE - size);
	}

	// Unlock blocks
	if (!OALFlashLock(pBase, pStart, size, FALSE))
	{
		OALMSG(OAL_ERROR, (L"ERROR: OEMWriteFlash: EBOOT blocks could not be unlocked\r\n"));
		goto cleanUp;
	}

	// Erase blocks
	if (!OALFlashErase(pBase, pStart, size))
	{
		OALMSG(OAL_ERROR, (L"ERROR: OEMWriteFlash: EBOOT blocks could not be erased\r\n"));
		goto cleanUp;
	}

	// Write blocks
	if (!OALFlashWrite(pBase, pStart, size, pData))
	{
		OALMSG(OAL_ERROR, (L"ERROR: OEMWriteFlash: EBOOT blocks could not be written\r\n"));
		goto cleanUp;
	}

	OALMSG(OAL_INFO, (L"EBOOT image written\r\n"));

    // Done
    rc = TRUE;

cleanUp:
    return rc;
}
BOOL
WriteFlashXLDR(
    UINT32 address,
    UINT32 size
    )
{
	ROMHDR *pTOC;
	UINT8 *pData;
	void *pBase = OALPAtoVA(g_ulFlashBase, FALSE);
	void *pStart = (void *)((UINT32)pBase + IMAGE_XLDR_NOR_OFFSET);
	UINT32 xldrSize;
	BOOL rc = FALSE;

	OALMSG(OAL_INFO, (L"\r\nWriting XLDR image to flash memory\r\n"));

	// Get data location
	pData = OEMMapMemAddr(address, address);

	// Verify image
	if (!VerifyImage(pData, &pTOC))
	{
		OALMSG(OAL_ERROR, (L"ERROR: OEMWriteFlash: XLDR image signature not found\r\n"));
		goto cleanUp;
	}

	// Verify that this is XLDR image
	if (pTOC->numfiles > 0 || pTOC->nummods > 1)
	{
		OALMSG(OAL_ERROR, (L"ERROR: OEMWriteFlash: XLDR image must have only one module and no file\r\n"));
		goto cleanUp;
	}

	// Check for maximal XLRD size
	xldrSize = pTOC->physlast - pTOC->physfirst;
	if (xldrSize > IMAGE_XLDR_CODE_SIZE)
	{
		OALMSG(OAL_ERROR, (L"ERROR: OEMWriteFlash: XLDR image size 0x%04x doesn't fit to limit 0x%04x\r\n", size, IMAGE_XLDR_CODE_SIZE));
		goto cleanUp;
	}

	// Unlock blocks
	if (!OALFlashLock(pBase, pStart, size, FALSE))
	{
		OALMSG(OAL_ERROR, (L"ERROR: OEMWriteFlash: XLDR blocks could not be unlocked\r\n"));
		goto cleanUp;
	}

	// Erase blocks
	if (!OALFlashErase(pBase, pStart, size))
	{
		OALMSG(OAL_ERROR, (L"ERROR: OEMWriteFlash: XLDR blocks could not be erased\r\n"));
		goto cleanUp;
	}

	// Write blocks
	if (!OALFlashWrite(pBase, pStart, size, pData))
	{
		OALMSG(OAL_ERROR, (L"ERROR: OEMWriteFlash: XLDR blocks could not be written\r\n"));
		goto cleanUp;
	}

    // Done
    rc = TRUE;

cleanUp:
    return rc;
}