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

📄 sysflashloader.c

📁 VxWorks BSP for AT91RM92
💻 C
字号:
/* sysFlashLoader.c - CSB337, Intel 28F640J3A Strata Flash loader library */

/* Copyright 2004 Wind River Systems, Inc. */
#include "copyright_wrs.h"

/*
modification history
--------------------
01a,05oct04,pdr  written.
*/

/*
DESCRIPTION
This library provides tools to be able to update the FLASH.
The flash area is one 16-bits Intel Strata flash (I28F640J3A).

Example (both actions are equivalent):
\cs
    sysFlashUpdateFromFile "host:bootrom.bin", 0

or
    pMem = malloc(0x80000)
    fd = open("host:bootrom.bin", 2, 0)
    nBytes = read(fd,pMem,0x80000) /@ ensure that the file will not exceed 512 KB @/

    sysFlashUpdateFromMemory(pMem, 0x80000, nBytes)

\ce

*/

/* includes */

#include "vxWorks.h"
#include "string.h"
#include "cacheLib.h"
#include "stdio.h"
#include "stdlib.h"
#include "taskLib.h"
#include "intLib.h"
#include "ioLib.h"
#include "tickLib.h"
#include "drv/timer/timerDev.h"

#include "drv/mem/flashDev.h"
#include "drv/mem/flash29.h"

/* defines */

/* revision information */
char* sysFlashLoaderVersion = "$Revision: 1.1 $ $Date: 2004/10/05 15:33:56 $";

/* locals */


/* forward declarations */
IMPORT STATUS sysFlashUpdateFromFile(char *fileName, int offset);
IMPORT STATUS sysFlashUpdateFromMemory(char *pSrc, int offset, int size);

/* externals */
IMPORT STATUS sysSectorErase(FLASH_DEF * pFA, UINT8 flashType);
IMPORT STATUS sysFlashWrite (FLASH_DEF * pFB, int size, int offset, UINT8 flashType, FLASH_DEF value);

/*******************************************************************************
*
* bufcmp - compare one buffer to another
*
* This routine compares the first <nbytes> characters of <buf1> to <buf2>.
*
* RETURNS:
*   NULL if the first <nbytes> of <buf1> and <buf2> are identical,
*   or offset pointer to the first character difference.
*
* NOMANUAL
*/

LOCAL char * bufcmp
    (
    FAST char *buf1,            /* pointer to first buffer    */
    FAST char *buf2,            /* pointer to second buffer   */
    FAST int nbytes             /* number of bytes to compare */
    )
    {
    const unsigned char *p1;
    const unsigned char *p2;

    /* size of memory is zero */

    if (nbytes == 0)
	return (0);

    /* compare array 2 into array 1 */

    p1 = (const unsigned char *)buf1;
    p2 = (const unsigned char *)buf2;

    while (*p1++ == *p2++)
	{
	if (--nbytes == 0)
	    return (0);
        }

    return ((char *) ((UINT32) (--p1) - (UINT32) buf1));
    }

/******************************************************************************
*
* sysFlashUpdateFromFile - update flash memory from a binary file
*
* This routine write a binary file to flash memory.
*
* RETURNS: OK, or ERROR if the file cannot be opened, the write fails or
* the input parameters are out of range.
*
* SEE ALSO: sysFlashSet(), sysFlashSelect()
*
*/

STATUS	sysFlashUpdateFromFile
	(
	char *	fileName,			/* file name of binary file	*/
	int		offset				/* offset on Flash */
	)
	{
	int 	fileDesc;			/* file descriptor */
	int 	fileSize;			/* size of file */
	int		nBytes;				/* number of bytes read */
	int		bufferSize;			/* number of bytes read */
	char *  pBuffer;			/* buffer for binary write to flash */
	STATUS	status;

	printf("sysFlashUpdateFromFile: %s\n", sysFlashLoaderVersion);
	
	if (offset < 0 || offset > FLASH_SIZE)
		{
		printf("Error: offset does not fit into flash !\n");
		return (ERROR);
		}

	/* Open the file */
    if (fileName == NULL)
		{
		printf("Error: File name is NULL !\n");
		return (ERROR);
		}

	if ((fileDesc = open(fileName, O_RDONLY, 0644)) == ERROR)
		{
		printf("Error: cannot open file %s !\n", fileName);
		return (ERROR);
		}

	/* Get file size */
    if ((fileSize = lseek (fileDesc, 0, SEEK_END)) == ERROR)
		{
		printf("Error: cannot get file size !\n");
		close(fileDesc);
		return (ERROR);
		}

	if (fileSize > (FLASH_SIZE - offset))
		{
		printf("Error: file size too big (%i bytes, offset = 0x%x) !\n", fileSize, offset);
		close(fileDesc);
		return (ERROR);
		}
	
    /* aligne buffer on sector size */
    bufferSize = fileSize / FLASH_SECTOR_SIZE * FLASH_SECTOR_SIZE + FLASH_SECTOR_SIZE;

    if ((pBuffer = (char *) malloc(bufferSize)) == NULL)
		{
		printf("Error: cannot reserve memory (%i bytes) for flash updating !\n", bufferSize);
		close(fileDesc);
		return (ERROR);
		}

	memset(pBuffer, 0xFF, bufferSize);

	if (lseek (fileDesc, 0, SEEK_SET) == ERROR)
		{
		printf("Error: cannot go to start of file !\n");
		free(pBuffer);
		close(fileDesc);
		return (ERROR);
		}
	

	/* read data to write */
	if ((nBytes = read(fileDesc, pBuffer, fileSize)) == ERROR)
		{
		printf("\rUpdating...error while reading file !\n");
		close(fileDesc);
		free(pBuffer);
		return (ERROR);
		}
	
	if (nBytes != fileSize)
		{
		printf("\nWarning read only %i bytes instead of %i bytes\n", nBytes, fileSize);
		}

	printf("Loading %s at offset 0x%x\n", fileName, offset);
	status = sysFlashUpdateFromMemory(pBuffer,offset, nBytes);

	(void) free(pBuffer);

	return (status);
	}

/******************************************************************************
*
* sysFlashUpdateFromMemory - update flash memory from a memory area
*
* This routine write an image placed previously in memory (from the emulator)
* to flash memory at offset.
*
* RETURNS: OK, or ERROR if the write fails or the input parameters are out of range.
*
* SEE ALSO: sysFlashSet()
*
*/

STATUS sysFlashUpdateFromMemory
	(
	char 	*pSrc,				/* address source of the image		*/
	int		offset,				/* byte offset into flash memory	*/
	int 	size				/* size of the image to burn 		*/
	)
	{
	int		val;
	int     currentSize;		/* current size to program */
	int		offsetTmp;			/* current offset to write */
    int     sector;             /* current sector */
	char *  pAdrs;				/* current adrs to write to flash */
	ULONG	timeFlash;			/* time of flash writing */
    volatile int flashAdrs = FLASH_BASE_ADRS;
	char space[] = "                                                              ";
	int sectorSize;

	/* update the flash */
	offsetTmp   = offset;
	currentSize = size;
	timeFlash   = tickGet() / sysClkRateGet();
	pAdrs       = pSrc;
	
    printf("\nUpdating...");

	while (currentSize > 0)
		{
        /* erase area */
        printf("\r%s\rUpdating...%i/%i bytes (erasing)", space,
               (size - currentSize), size);

        /* normal sectors */
        sector = (int) offsetTmp / FLASH_SECTOR_SIZE;
        sectorSize = FLASH_SECTOR_SIZE;				

        if (sysSectorErase((FLASH_DEF *)(flashAdrs + (sector * sectorSize)),
                               SYS_FLASH_TYPE) == ERROR)
			{
			printf("\rUpdating...cannot erase at 0x%08X !\n",
                   (sector * sectorSize));
			return (ERROR);
			}

        /* write to flash */
        printf("\r%s\rUpdating...%i/%i bytes (writing)", space,
               (size - currentSize), size);
        
		if (sysFlashWrite((FLASH_DEF *) pAdrs,
                          sectorSize,
                          offsetTmp,
                          SYS_FLASH_TYPE,
                          0) == ERROR)
			{
            printf("\rUpdating...write error at offset 0x%08X !\n", offsetTmp);
			return (ERROR);
			}
		
		/* compare written data */
        printf("\r%s\rUpdating...%i/%i bytes (checking)", space, (size - currentSize), size);
        
        if ((val = (int) bufcmp(pAdrs, (char *)(flashAdrs + offsetTmp), sectorSize)) != 0)
			{
            printf("\rUpdating...comparison error for offset 0x%08X at 0x%08X !\n", offsetTmp, val);
			return (ERROR);
			}

		/* prepare for next write */
        offsetTmp   += sectorSize;
		currentSize -= sectorSize;
		pAdrs       += sectorSize;
		}
	
	timeFlash = (tickGet() / sysClkRateGet()) - timeFlash;

    printf("\n\rUpdating...done\n");
    printf("Wrote %i bytes (%i bytes) in %i seconds\n",
           (size - currentSize),
           size,
           (int) timeFlash);

	return (OK);
	}

⌨️ 快捷键说明

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