📄 flashutilmgr.c
字号:
/* FlashUtilMgr.c - User Interface for utility for reprogramming flash *//* Copyright 1999 Intel Corp. *//*modification history--------------------01a,08Jul99,jdg created*//*DESCRIPTIONThis file contains a semi-generic user interface for reprogramming flash.It depends on flashUtil.c to provide the actual routines for manipulatingthe flash. It must be #included in a preamble file that defines certaintypes and constants. In particular, it needs:#define MAKE_PAGES_WRITABLE(start_addr, end_addr) ... Routine to make the pages between start_addr and (end_addr-1) writable. If these pages are already writable, then it can be made empty.#define SERPORTNAME "string" Name to be passed to fopen to get a file pointer to the serial port used to interact with the user.#define FLASH_TOTAL_SIZE const Size in bytes of Flash part.#define FLASH_WIDTH const Width in bytes of Flash part. Valid values are 1, 2, or 4.typedef UINT8, UINT16, UINT32 Types evaluating to 8-, 16-, and 32-bit unsigned intstypedef STATUS#define OK, ERROR Type defining return type of functions. It can return OK or ERROR. This may be defined as an enumeration.#define FLASH_BASE addr Address of start of Flash#define FLASH_ADRS addr Optional address of block containing NVRAM parameters that should not be overwritten.#define ALT_FLASH_BASE addr Address of alternate start of Flash, when the flash is connected to CE1 rather than CE0.*/#include <stdio.h>#include "flashUtil.h"#include "flashUtil.c"UINT8 *fuBuf = NULL;#ifdef FLASH_ADRS#define NVRAM_OFFSET (FLASH_ADRS - FLASH_BASE)#endif#define PAGESIZE (4 * 1024)FILE* fuFp = NULL;extern UINT32 flashBase;#define FILE_GRAN 0x10000/********************************************************************************* fuFRead - Flash Utility: Read file into buffer** This routine reads a file into a buffer while printing a status* report on fuFp.** RETURNS: N/A** NOMANUAL*/static UINT32 fuFRead(UINT8 *buf, UINT32 size, FILE *fp) { UINT8 *lbuf; UINT32 read, count = 0; fprintf(fuFp,"Reading file:\n"); for (lbuf = buf; lbuf < buf + size; ) { fprintf(fuFp,"\r%08X bytes",count); fflush(fuFp); read = fread(lbuf, sizeof(char), FILE_GRAN, fp); count += read; if (feof(fp) || ferror(fp)) break; lbuf = buf + count; } fprintf(fuFp,"\r%08X bytes ... done\n",count); return count; }/********************************************************************************* fuReadFile - Flash Utility: Read a file into a buffer** This routine allocates a buffer and reads a file into it.** RETURNS: malloc'ed buffer or NULL if error** NOMANUAL*/static UINT8* fuReadFile(char *name, UINT32 *size) { UINT8 *buf; FILE *fp; int read; buf = malloc(FLASH_TOTAL_SIZE); if (buf == NULL) { fprintf(fuFp,"Can't malloc buffer.\n"); return NULL; } fprintf(fuFp,"Opening file '%s' ... ", name); fflush(fuFp); fp = fopen(name,"rb"); if (fp == NULL) { fprintf(fuFp,"Can't open file: '%s'\n",name); free(buf); return NULL; } else { fprintf(fuFp,"done\n"); } bfill(buf, FLASH_TOTAL_SIZE, 0); read = fuFRead(buf, FLASH_TOTAL_SIZE, fp); if (ferror(fp)) { fprintf(fuFp,"Error reading file\n"); free(buf); return NULL; } if (EOF != getc(fp)) { fprintf(fuFp,"Not at EOF?!!!!\n"); free(buf); return NULL; } fclose(fp); if (size != NULL) *size = read; return buf; }/********************************************************************************* fuSanityCheck - Flash Utility: Try to sanity check a buffer** This routine tries to sanity check a buffer to see if it contains* a viable ARM raw binary image. ** RETURNS: OK or ERROR** NOMANUAL*/static STATUS fuSanityCheck(UINT8* buf) { /* Not too many sanity checks we can make */ if (((*((UINT32*)buf) & 0xFF000000) != 0xEA000000) && ((*((UINT32*)buf) & 0xFF00F000) != 0xE500F000)) { fprintf(fuFp,"First instruction is not a branch instruction.\n"); return ERROR; } return OK;}/********************************************************************************* fuIsWhite - Flash Utility: Check a buffer to see if it contains white space** This routine checks a buffer to see if it contains only spaces or tabs* up to the end of the buffer or the first line-feed.** RETURNS: TRUE if white, FALSE otherwise** NOMANUAL*/static BOOL fuIsWhite(char *buf) { while(1) { if ((*buf == '\0') || (*buf == '\n')) return TRUE; if ((*buf != ' ') && (*buf != '\t')) return FALSE; buf++; } }/********************************************************************************* fuPromptHex - Flash Utility: prompt for a hex value** This routine displays a prompt with a default value, and then reads a* response. If the response is not white (blank), it parses the response* as a hex value and updates the default.** If a period is entered, then the value is not changed, and an "exit"* flag is returned to the caller.** RETURNS: ERROR if exit value specified (period), OK otherwise** NOMANUAL*/static STATUS fuPromptHex(char *prompt, UINT32 *val) { char buf[160]; UINT32 newval; while (1) { fprintf(fuFp,"%s (%06X): ",prompt,*val); fgets(buf,160,fuFp); if ((buf[0] == '.') && (buf[1] == '\n')) { return ERROR; } else if (fuIsWhite(buf)) { return OK; } else if (1 == sscanf(buf, "%X", &newval)) { *val = newval; return OK; } else { fprintf(fuFp,"Invalid hex value entered: '%s'.\n",buf); } } }/********************************************************************************* fuPromptString - Flash Utility: prompt for a string value** This routine displays a prompt with a default value, and then reads a* response. If the response is not white (blank), it parses the response* as a string value and updates the default.** If a period is entered, then the value is not changed, and an "exit"* flag is returned to the caller.** RETURNS: ERROR if exit value specified (period), OK otherwise** NOMANUAL*/static STATUS fuPromptString(char *prompt, char* *val) { static char buf[160]; char buf2[160]; fprintf(fuFp,"%s (%s): ",prompt,*val); fgets(buf2,160,fuFp); if ((buf2[0] == '.') && (buf2[1] == '\n')) { return ERROR; } else if (fuIsWhite(buf2)) { return OK; } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -