📄 flashprg.c
字号:
/*******************************************************************************
File Name : flashprg.c
Description : Eval-TP3 / Eval5500 Flash ROM programming software
(C)Copyright ST Microelectronics 1998
Reference to the origin of this file within the build system repository
\5500ref\flash\burner\flashprg.c
References to related design specifications, tools, required header files etc.
Date Modification Initials
---- ------------ --------
16-Dec-98 When opening the BIN mode files, "r" was being used for
the open parameter. This has been changed to "rb" mode. RO
29-Oct-98 Creation RO
*******************************************************************************/
/* Includes ----------------------------------------------------------------- */
#include <stdlib.h>
#include <kernel.h>
#include <ostime.h>
#include <string.h>
#include "flashprg.h"
/* Private Types ------------------------------------------------------------ */
/* Private Constants -------------------------------------------------------- */
#define PROGRAM_NAME "ROMTOOL, Version 2.2.1"
#define BUFFER_SIZE (100*1024)
static const char *Version = "ROMTOOL:\n"
#if defined (__CORE__)
#if __ICC_VERSION_NUMBER__ == 50400
"\tChain : DCU: Osprey1.5.1\n"
#elif __ICC_VERSION_NUMBER__ == 50500
"\tChain : DCU: Osprey1.6.1\n"
#elif __ICC_VERSION_NUMBER__ == 50600
"\tChain : DCU: Osprey1.6.2\n"
#else
"\tChain : DCU: Osprey?.?.?\n"
#endif
#endif
;
/* Global Variables --------------------------------------------------------- */
#ifdef COMPACT_BUILD
#else
char outputBuffer[MAX_PRINTF_BUFFER] ;
char* printBuf=outputBuffer ;
#endif
BOOL address_override = FALSE ;
int address_load ;
BOOL rom_serial = FALSE; /* ROM serial number update */
ROMTOOL_REPORT rom_report = NULL_REPORT; /* reporting level */
long int rom_datafile_fp; /* datafile descriptor */
int rom_size; /* size of ROM */
FILE_STRING rom_datafile; /* datafile name */
FILE_STRING rom_logfile; /* logfile name */
FILE_STRING rom_serialno; /* Serial number/code */
ROMTOOL_MODE rom_mode = NULL_MODE; /* operational mode */
ROMTOOL_BOARD rom_board = EVAL5500; /* board */
UBYTE hexFileBuffer[BUFFER_SIZE] ;
//UBYTE hexFileBuffer[4] = {0x12, 0x34, 0x56, 0x78};
/********************************INTEL define*******************************************/
#define INTEL_ID 0x0089
#define INTEL_28F800C3_T 0x88c0
#define INTEL_28F160C3_T 0x88c2
/* Private Variables -------------------------------------------------------- */
static char bData[100];
static char bOffset;
static long int bLength;
static int eof;
extern unsigned int mfr_id;
extern unsigned int dev_id;
/* Private Macros ----------------------------------------------------------- */
/* Private Function prototypes ---------------------------------------------- */
extern int FlashCheck(void);
/* Functions ---------------------------------------------------------------- */
int Gets(long int fd, char *dest)
{
int sLength = 1;
while(1)
{
while(bOffset < bLength)
{
if(bData[bOffset] == '\n')
{
bOffset++;
*dest = '\0';
return sLength;
}
*dest = bData[bOffset];
dest++;
bOffset++;
sLength++;
}
if(eof)
return 0;
bLength = debugread(fd, bData, sizeof(bData));
bOffset = 0;
eof = bLength < sizeof(bData);
}
}
char *GetElement(char *src, char *dest) {
if(!src)
return NULL;
for(;;) {
if(*src == ' ') {
*dest = '\0';
return src + 1;
}
if(*src == '\0') {
*dest = '\0';
return NULL;
}
*dest++ = *src++;
}
}
char val2asc(int val)
{
if (val>=0 && val <=9)
return val+'0' ;
else if (val>=10 && val <=16)
return val-10+'a' ;
else
return '0' ;
}
void compactSprintf( char* buf, char* format, ...)
{
unsigned int val ;
char* str ;
va_list args ;
va_start( args, format ) ;
while (*format!='\0') {
if (*format != '%')
{
*buf = *format ;
buf++ ;
}
else
{
format++ ;
if (*format=='s') {
str = va_arg( args, char* ) ;
while (*str!='\0') {
*buf = *str ;
buf++ ;
str++ ;
}
} else if (*format=='x') {
int i ;
val = va_arg( args, unsigned int ) ;
for (i=7; i>=0; i-- ) {
*buf = val2asc( val >> 28 ) ;
buf++ ;
val = val << 4 ;
}
}
}
format++ ;
}
*buf = '\0' ;
va_end( args ) ;
}
int hatoi(char* ptr)
{
int val = 0 ;
if (!ptr)
return 0 ;
while (1) {
if (*ptr=='\0') {
break ;
}
else if (*ptr=='0' && ( ptr[1]=='x' || ptr[1]=='X' ))
ptr++ ;
else if (*ptr>='0' && *ptr<='9')
{
val *= 16 ;
val += *ptr - '0' ;
}
else if (*ptr>='a' && *ptr<='f')
{
val *= 16 ;
val += *ptr - 'a' + 10 ;
}
else if (*ptr>='A' && *ptr<='F')
{
val *= 16 ;
val += *ptr - 'A' + 10 ;
}
ptr++ ;
}
return val ;
}
int ReadHexLine(long int fd, UINT32* address, UBYTE* dest, int *length)
{
char buffer[1024];
char element[64];
char *ptr;
long slen;
slen = Gets(fd, buffer);
if(!slen)
return 1;
ptr = buffer;
/* Get Address */
ptr = GetElement(ptr, element);
*address = hatoi(element) ;
*length = 0;
while(ptr)
{
int tmp;
ptr = GetElement(ptr, element);
tmp = hatoi(element) ;
*dest++ = (unsigned char)tmp;
(*length)++;
}
return 0;
}
char *modestr(ROMTOOL_MODE m)
{
static char *modes[] = {
"NULL_MODE",
"ERROR_MODE",
"ERASE",
"PROGRAM",
"VERIFY",
"DUMP"
};
return(modes[m]);
}
/* Return textual representation of the ROMTOOL_REPORT enumeration */
char *reportstr(ROMTOOL_REPORT r)
{
static char *reports[] = {
"NULL_REPORT",
"ERROR_REPORT",
"FULL",
"REDUCED",
"NONE"
};
return(reports[r]);
}
/************************************************************************
Function : ProgramOrVerify()
Description : Called when a program or verify has been requested. It scans
the input file and programs or verifies the bank of flash memory
corresponding to the address given in the file. To program
a bank it first erases the entire contents of the memory
including the boot block.
Returns :
************************************************************************/
int ProgramOrVerify(BOOL program)
{
int status = TRUE;
int numBytes, TotalNumBytes;
long int fileSize ;
UINT32 addr ;
int nameLength ;
BOOL binFile ;
clock_t StartDate = 0;
nameLength = strlen(rom_datafile) ;
if (rom_datafile[nameLength-1]=='n' &&
rom_datafile[nameLength-2]=='i' &&
rom_datafile[nameLength-3]=='b' &&
rom_datafile[nameLength-4]=='.' )
binFile = TRUE ;
else
binFile = FALSE ;
if (address_override)
addr = address_load ;
else
addr = 0x7ff00000 ;
if(dev_id == 0x88c2 || dev_id == 0x22c4)
addr = 0x7fe00000 ;
/*******************************************
* Create filename and attempt to open it */
rom_datafile_fp = debugopen(rom_datafile, "rb");
if (rom_datafile_fp<=0) {
FULL_REP_MSG((printBuf, "\nCannot open data file %s\n", rom_datafile));
REDUCED_REP_MSG("\nCannot open data file\n" ) ;
return (FALSE) ;
}
fileSize = debugfilesize(rom_datafile_fp);
TotalNumBytes = 0;
FULL_REP_MSG((printBuf, "\nData file '%s' opened (size = %ld bytes) \n",
rom_datafile, fileSize));
StartDate = time_now();
/* loop thru the data file */
while (1)
{
/* Use file extn to select hex or bin mode */
if (binFile)
{
numBytes = (int)debugread((long int)rom_datafile_fp,
(void*)hexFileBuffer, sizeof(hexFileBuffer));
TotalNumBytes += numBytes;
if (program)
{
/* program the bytes read from the data file */
FULL_REP_MSG((printBuf,
"Read %d bytes (bin) from %s file. Checking 0x%lx\n",
numBytes, rom_datafile, addr));
FULL_REP_MSG((printBuf,
"ROMTOOL> Writing (bin) to 0x%lx (%d Kbytes) -> %d %% \r",
addr, numBytes/1024, (TotalNumBytes*100)/(int)fileSize));
status = ProgramFlash((addr, hexFileBuffer, numBytes));
}
else
{
FULL_REP_MSG((printBuf,
"Read %d bytes (bin) from %s file. Checking 0x%lx\n",
numBytes, rom_datafile, addr));
status = VerifyFlash((addr, hexFileBuffer, numBytes));
}
addr += numBytes ;
if (!status)
break ;
if (TotalNumBytes >= fileSize)
break ;
}
else
{
if (ReadHexLine(rom_datafile_fp, &addr, (unsigned char *)hexFileBuffer,
&numBytes))
{
break ;
}
TotalNumBytes += numBytes;
if (program)
{
/* program the bytes read from the data file */
#if 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -