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

📄 flash.c

📁 WinCE5.0BSP for Renesas SH7770
💻 C
📖 第 1 页 / 共 3 页
字号:
//
//  Copyright(C) Renesas Technology Corp. 2005. All rights reserved.
//
//  Ethernet Boot Loader for ITS-DS7
//
//  FILE      : flash.c
//  CREATED   : 2005.08.01
//  MODIFIED  : 
//  AUTHOR    : Renesas Technology Corp.
//  HARDWARE  : RENESAS ITS-DS7
//  HISTORY   : 
//              2005.08.01
//              - Created release code.
//                (based on EBOOT for ASPEN for WCE5.0)

//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/*

  Copyright(c) 1998-2000 Renesas Technology Corp.

    Module Name:

        flash.c

    Revision History:

        May. 09, 2000       Create (based on S1 platform)

*/

#include <windows.h>
#include <halether.h>
#include <pehdr.h>
#include <romldr.h>
#include "platform.h"
#include "loader.h"

extern BOOL gbStoreSDRAMImageToFlash;
extern BOOL gbJumpToFlash;
extern BOOL gbGotIP;

extern void DumpDwords(PDWORD pdw, int len);

// These are the possible states that a flash block can be in.  The state of a flash block is stored in
//  the global EraseStatus array and can be changed by either FlashErase() or FlashWrite().

typedef enum
{
    eDIRTY,    // The block contains unknown data and has not been erased
    ePENDING,  // An erase command has been issued for the block but it has not yet completed
    eERASED,   // The block has been erased and is ready for programming
    eFLASHED   // The block has been programmed and is ready for use
} EraseStatusType;


static EraseStatusType gStatus[TOTAL_FLASH_BLOCKS];
static DWORD gdwStartBlock;
static DWORD gdwEndBlock;
static DWORD dwBlockIndex;
static DWORD gdwFlashAreaBase;

static int gnErased; 
static int gnBlocks;
static int gnRetries;   // # of retries so far

// These flash parameters are initialized in CheckFLASH().
DWORD dwFlashBankSize;  // bank size of flash memory
DWORD dwFlashBlockSize; // block size of flash memory
DWORD dwFlashPageSize;  // page size of flash memory
DWORD gdwFlashCode;

DWORD gdwSavedPhysStart; // keep actual start address while SDRAM image is written to flash
DWORD gdwSaveLaunchAddr;

UINT16 (*FlashWrite)( DWORD dwPhysStart, DWORD dwPhysLen, char **ppszErrorMsg );
UINT16 (*FlashError)( volatile DWORD *pdwSR, char **ppszErrorMsg );
UINT16 (*FlashClose)( DWORD dwPhysStart, DWORD dwPhysLen, char **ppszErrorMsg );

// Integrated Intel StrataFlash operating functions
UINT16 FlashError_IntelStrata( volatile DWORD *pdwBSR, char **ppszErrorMsg );
UINT16 FlashWrite_IntelStrata( DWORD dwPhysStart, DWORD dwPhysLen, char **ppszErrorMsg );
UINT16 FlashPageWrite_IntelStrata( volatile DWORD *pdwFlash, DWORD *pdwFlashCache, DWORD dwDWORDsLeft, char **ppszErrorMsg );
UINT16 FlashClose_IntelStrata( DWORD dwPhysStart, DWORD dwPhysLen, char **ppszErrorMsg );

void FlashWriteCommand(volatile DWORD *pdwReg, DWORD val);


#define MAX_RETRIES     TOTAL_FLASH_BLOCKS  // max number of errors before declaring failure

// Func

BOOL OEMStartEraseFlash (DWORD dwPhysStart, DWORD dwPhysLen);
void OEMContinueEraseFlash( void );
BOOL OEMFinishEraseFlash( void );
BOOL OEMWriteFlash( DWORD dwPhysStart, DWORD dwPhysLen);
LPBYTE 	OEMMapMemAddr 	( DWORD dwImageStart, DWORD dwAddr);
UINT16 	FlashPageWrite	( volatile DWORD *pdwFlash, 
						  DWORD *pdwFlashCache, 
						  DWORD dwDWORDsLeft, 
						  char **ppszErrorMsg );

//------------------------------------------------------------------------------
//
//  Function:  BLFlashDownload
//
//  This function download image from flash memory to RAM.
//
UINT32 BLFlashDownload(LPDWORD pdwImageStart, LPDWORD pdwImageLength, LPDWORD pdwLaunchAddr)
{
    DWORD dwCopySrc, dwCopyDest; // used to restore SDRAM image from flash memory
	DWORD dwPhysLen;		// image physical length
	DWORD dwLaunchAddr;

    // SDRAM image is stored in flash memory. First we need to copy it into SDRAM.
    dwCopySrc = FLASH_IMAGE_START_UNCACHED; // top of image stored blocks
    dwCopyDest = *(DWORD*)dwCopySrc;
    dwPhysLen = *(DWORD*)(dwCopySrc + sizeof(DWORD));
    dwLaunchAddr = *(DWORD*)(dwCopySrc + sizeof(DWORD)*2); // This is seemed unavailable...
    dwCopySrc += SDRAM_IMAGE_HEADER_SIZE; // skip header
    EdbgOutputDebugString("Copying image from flash memory to SDRAM...\r\n");
    EdbgOutputDebugString("Src=%x Dest=%x Length=%x\r\n", dwCopySrc, dwCopyDest, dwPhysLen);
    memcpy((VOID*)dwCopyDest, (VOID*)dwCopySrc, dwPhysLen);
	*pdwImageStart = dwCopyDest;
	*pdwImageLength = dwPhysLen;
	*pdwLaunchAddr = dwLaunchAddr;

	return 1;
}


//------------------------------------------------------------------------------
//  Function:  name( args )
//
//  Desc...
//

BOOL OEMIsFlashAddr( DWORD dwPhysStart )
{

    BOOL retval;

    if( (dwPhysStart & 0x0FFFFFFFUL) <  (FLASH_ADDR_START & 0x0FFFFFFFUL) ||
        (dwPhysStart & 0x0FFFFFFFUL) >= (FLASH_ADDR_END   & 0x0FFFFFFFUL) )
	{
        retval = FALSE;
	}
    else
	{
        retval = TRUE;
	}

	if(gbStoreSDRAMImageToFlash){
		return TRUE;
	}

    return retval;
}

BOOL OEMWriteFlash( DWORD dwPhysStart, DWORD dwPhysLen) 
{
    char *pszErrorMsg;
    DWORD *pdwFlashCache;

	if(gbStoreSDRAMImageToFlash){
		pdwFlashCache = (DWORD*)FLASH_CACHE;
	    *pdwFlashCache++ = gdwSavedPhysStart;
	    *pdwFlashCache++ = dwPhysLen;
	    *pdwFlashCache = gdwSaveLaunchAddr;
		
	    dwPhysStart = FLASH_IMAGE_START_UNCACHED; // top of image stored blocks
		dwPhysLen += SDRAM_IMAGE_HEADER_SIZE;
	}

    EdbgOutputDebugString( "+OEMWriteFlash( 0x%x, 0x%x )\r\n", dwPhysStart, dwPhysLen );

	if(FlashWrite( dwPhysStart, dwPhysLen, &pszErrorMsg) == 0) return TRUE;
	else return FALSE;
}

//------------------------------------------------------------------------------
//  Function:  name( args )
//
//  Desc...
//

LPBYTE OEMMapMemAddr (DWORD dwImageStart, DWORD dwAddr)
{
    if( OEMIsFlashAddr (dwAddr))
    {
		if(gbStoreSDRAMImageToFlash){
	        dwAddr += (FLASH_CACHE - dwImageStart + SDRAM_IMAGE_HEADER_SIZE);
		}
		else{
	        dwAddr += FLASH_CACHE - dwImageStart;
		}
    }
    else{
        dwAddr |=0x20000000L; // Using uncached address.
	}
    return(LPBYTE) dwAddr;
}

//------------------------------------------------------------------------------
//  Function:  name( args )
//
//  Desc...
//

// If DIP switch S1-3 is set, writing impossible to FLASH (write protected).

UINT16 CheckFlashWriteProtect(void)
{
	return 0;
}

//------------------------------------------------------------------------------
//  Function:  name( args )
//
//  Desc...
//

void FlashWriteCommand(volatile DWORD *pdwReg, DWORD val)
{
//  EdbgOutputDebugString( "W    (%Xh,%Xh)=%X%Xh\r\n",pdwReg,pdwReg+1,val,val);

    *(pdwReg  ) = val;
}

//------------------------------------------------------------------------------
//  Function:  name( args )
//
//  if (SR & val) == 0 then TRUE else FLASE
//

DWORD FlashSR0(volatile DWORD *pdwSR, DWORD val)
{
	DWORD dwSR1;

	dwSR1 = *(pdwSR);
	if((dwSR1 & val) == 0)
		return TRUE;
	else
		return FALSE;
}

//------------------------------------------------------------------------------
//  Function:  name( args )
//
//  if (SR & val) == val then TRUE else FLASE
//

DWORD FlashSR1(volatile DWORD *pdwSR, DWORD val)
{
	DWORD dwSR1;

	dwSR1 = *(pdwSR);
	if((dwSR1 & val) == val)
		return TRUE;
	else
		return FALSE;
}

//------------------------------------------------------------------------------
//  Function:  name( args )
//
//  Desc...
//
BOOL OEMStartEraseFlash (DWORD dwPhysStart, DWORD dwPhysLen)
{
    DWORD dwPhysEnd;
    DWORD i;
	volatile DWORD *pdwSR;

    EdbgOutputDebugString( "+OEMStartEraseFlash\r\n" );

    if( CheckFlashWriteProtect() )
    {
        EdbgOutputDebugString( "ERROR: OEMStartEraseFlash: flash write protected.\r\n");
        return FALSE;
    }

	if(gbStoreSDRAMImageToFlash){
	    dwPhysStart = FLASH_IMAGE_START_UNCACHED; // top of image stored blocks
	}

    dwPhysEnd = dwPhysStart + dwPhysLen;
    gdwFlashAreaBase = (dwPhysStart & 0x1C000000) | UNCACHED_BASE; // top of each area

    // compute starting block number and number of blocks

    gdwStartBlock = (dwPhysStart & (0x04000000 - dwFlashBlockSize)) / dwFlashBlockSize;
    gdwEndBlock = (dwPhysEnd & (0x04000000 - dwFlashBlockSize)) / dwFlashBlockSize;
    gnBlocks = (int) (gdwEndBlock - gdwStartBlock + 1);
    gnErased = 0;

    EdbgOutputDebugString( "dwStartBlock=%d dwEndBlock=%d \r\n",gdwStartBlock,gdwEndBlock);

    // protect BLOCK 0 (ethernet bootloader block)
    //          for( i = dwStartBlock; i <= dwEndBlock; i++ ) {
    //              if(i == 0) {
    //                  *ppszErrorMsg = "Error : Block 0 is the bootloader block. Check the image start address.";
    //                  return 1;
    //              }
    //          }

    // protect BLOCK 0 (ethernet bootloader block)
    if (gdwStartBlock == 0 && gdwFlashAreaBase == (AREA_0 + UNCACHED_BASE)) {
        return FALSE;
    }

    // Initialize status table

    for( i = gdwStartBlock; i < gdwEndBlock; i ++ )
    {
        gStatus[i] = eDIRTY;
    }

    dwBlockIndex=gdwStartBlock;
    pdwSR= (DWORD *)(gdwFlashAreaBase + dwBlockIndex * dwFlashBlockSize);

    // Reset the status flags
    FlashWriteCommand(pdwSR, 0x50505050UL); // Clear Status Register command

    // Start the first erase command

    OEMContinueEraseFlash();

⌨️ 快捷键说明

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