|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2011-5-12 23:06:39
|
显示全部楼层
自己先顶一下
这个是Jflashmm中关于flash的部分
/******************************************************************************
**
** COPYRIGHT (C) 2000, 2001, 2002 Intel Corporation.
**
** The information in this file is furnished for informational use
** only, is subject to change without notice, and should not be construed as
** a commitment by Intel Corporation. Intel Corporation assumes no
** responsibility or liability for any errors or inaccuracies that may appear
** in this document or any software that may be provided in association with
** this document.
**
** FILENAME: Jflash.cpp
**
** PURPOSE: A utility to program Intel flash devices from a PC parallel port.
**
** LAST MODIFIED: $Modtime: 2/20/04 10:39a $
******************************************************************************/
/*****************************************************************************
Note: Within these files there are porting instructions that are useful for
adding additional platform support to this tool. These guides may be located by
searching for the string "PORTING_GUIDE".
*******************************************************************************/
#include <stdio.h>
#if defined __linux__
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/perm.h>
#include <errno.h>
#include <sys/io.h>
#define _getche getchar
#define _inp(a) inb(a)
#define _outp(a,d) outb(d,a)
#define BOOL bool
#define FALSE false
#define TRUE true
typedef long DWORD;
typedef short WORD;
#include "Compile_switches.h"
#include "Jflash.h"
#include "jtag.h"
#include "Global_Variables.h"
#ifndef DATADIR
#define DATADIR "/usr/share/jflashmm/"
#endif
#define lastchar(s) ((s)[strlen(s)-1])
#elif defined WIN32
#include <windows.h>
#include <time.h>
#include <conio.h>
#include "compile_switches.h"
#include "jflash.h"
#include "jtag.h"
#include "string.h"
#include "Global_Variables.h"
#else
#error "Platform is not defined"
#endif
/*
*******************************************************************************
Forward declarations
*******************************************************************************
*/
int putp(int,int, int); // writes the JTAG data on the parallel port
void id_command(void); // issues the JTAG command to read the device ID for all 3 chips
//void bypass_all(void); // issues the JTAG command to put all 3 device in bypass mode
//void extest(void); // issues the JTAG command EXTEST to the Processor
DWORD access_rom(int, DWORD, DWORD, int); // Passes read/write/setup data for the Flash memory
DWORD access_bus(int, DWORD, DWORD, int); // Read/write access to the Processor pins
void Write_Rom(DWORD address, DWORD data); // Writes to ROM
DWORD Read_Rom(DWORD address); // Reads from ROM
int test_port(void); // Looks for and finds a valid parallel port address
int check_id(char*); // Compares the device IDs for the 3 JTAG chips to expected values
void error_out(char*); // Prints error and exits program
//void erase_flash(DWORD, DWORD, DWORD, DWORD, int);
void program_flash(DWORD, DWORD, DWORD);
void verify_flash(DWORD, DWORD);
void test_logic_reset(void);
//void test_lock_flash(DWORD, DWORD, DWORD, DWORD, int);
void set_lock_flash(DWORD, DWORD, DWORD, DWORD, int);
void set_address (DWORD);
int PZ_scan_code(int, int, int);
int controller_scan_code(int, int, int);
void pre_DRSCAN(void);
void post_DRSCAN(void);
void pre_IRSCAN(void);
void post_IRSCAN(void);
void mem_rw_mode(int);
void mem_data_driver(int);
void mem_write_enable(int);
void mem_output_enable(int);
void clear_chip_selects(void);
void set_chip_select(DWORD);
DWORD shift_data(int);
void set_data(DWORD);
void jtag_test(void);
void dump_chain(void);
void init_workbuf(void);
void invert_workbuf(void);
void set_pin_chip_select(DWORD);
void gpio_unlock_flash(void);
void gpio_lock_flash(void);
void ParseAndLoad(void);
void ParseAndLoadFlashData(void);
DWORD convert_to_dword(char*);
void AnalyzeChain (void);
void InitPinArray (void);
void InitLockUnlock(void);
void InitAddressOrder(void);
void InitInputDataOrder(void);
void InitOutputDataOrder(void);
void InitChipSelectRegions(void);
void InitFlashGlobals(void);
void UnlockAndEraseBlock(DWORD);
DWORD GetChipSelect(DWORD);
void EraseBlocks(DWORD, DWORD);
void check_file_info(DWORD *fsize , DWORD *last_non_zero, DWORD *last_non_ff, DWORD rom_size);
void check_rom_info(DWORD *max_erase_time, DWORD * dsize, DWORD * max_write_buffer );
int other_bypass(int rp, int ct, int length);
void IR_Command(int command);
void usage(void);
void Set_Platform_Global_Variables(void);
void DeclareDefaults(int);
void Integrity_Check(void);
#ifdef __linux__
bool io_access_on(unsigned long port){
int res;
res = ioperm(port, 3, 1);
if (res){ perror("ioperm()"); return false; }
res = ioperm(0x80, 1, 1);
if (res){ perror("ioperm()"); return false; }
return true;
}
void io_access_off(unsigned long port){
ioperm(port, 3, 0);
ioperm(0x80, 1, 0);
return;
}
#else
# define io_access_on(x) (true)
# define io_access_off(x)
#endif
/*
*******************************************************************************
*
* FUNCTION: main
*
* DESCRIPTION: Entry point for utility
*
* INPUT PARAMETERS: uses optional input parameters:
* argv[1] = Platform to which the binary file will be downloaded
* argv[2] = filename to flash (binary files only)
* argv[3] = program options, currently only 'P' for Program
* argv[4] = Byte Address
* argv[5] = INS or PAR (Insight or parallel cable)
argv[6] = Debug mode on or off
* RETURNS: void
*
*******************************************************************************
*/
#if defined WIN32
int main( int argc, char *argv[] )
{
time_t start;
DWORD fsize = 0;
DWORD last_non_zero = 0;
DWORD last_non_ff = 0;
DWORD base_address;
DWORD max_erase_time, dsize, max_write_buffer;
char base_address_string[12];
DWORD hexaddress;
char plat_name[MAX_IN_LENGTH];
char buf[10];
int i = 0;
// int j;
// DWORD block_size;
strcpy(base_address_string, "");
if(argc >= 2)
{
//Copy the first argument into a character array
strcpy(plat_name, argv[1]);
// Convert the name of the platform to uppercase
#if defined __linux__
do {
int res;
char buff[MAX_IN_LENGTH], *s;
struct stat fs;
if (!strchr(plat_name, '/')){
s = buff; // fine in current directory
s += sprintf(s, plat_name);
if (strcmp(s + strlen(s) - 4, ".dat"))
s += sprintf(s, ".dat");
res = stat(buff, &fs);
if (!res){ strcpy(plat_name, buff); break; }
s = buff; // fine in data directory
s += sprintf(s, "%s", DATADIR);
if (lastchar(s) != '/')
s += sprintf(s, "/");
s += sprintf(s, "%s", plat_name);
if (strcmp(s + strlen(s) - 4, ".dat"))
s += sprintf(s, ".dat");
res = stat(buff, &fs);
if (!res){ strcpy(plat_name, buff); break; }
} else { // with '/'
s = buff;
s += sprintf(s, "%s", plat_name);
if (strcmp(s + strlen(s) - 4, ".dat"))
s += sprintf(s, ".dat");
res = stat(buff, &fs);
if (!res){ strcpy(plat_name, buff); break; }
}
} while (0);
sprintf(data_filename, "%s", plat_name);
sprintf(int_data_filename, DATADIR "%s_integrity.dat", plat_name);
#elif defined WIN32
for(unsigned int i = 0; i < strlen(plat_name); i++)
plat_name = toupper(plat_name);
// use the parameter to construct a filename to parse
sprintf(data_filename, "%s.dat", plat_name);
// while we're at it, construct the integrity check filename derived from this.
sprintf(int_data_filename, "%s_integrity.dat", plat_name);
#endif
}
else
{
if(!UsageShown) usage();
printf("\nEnter platform data file name: ");
fgets(plat_name, MAX_IN_LENGTH, stdin); //gets(plat_name);
// Convert the name of the platform to uppercase
for(unsigned int i = 0; i < strlen(plat_name); i++)
plat_name = toupper(plat_name);
// use the parameter to construct a filename to parse
sprintf(data_filename, "%s.dat", plat_name);
// while we're at it, construct the integrity check filename derived from this.
sprintf(int_data_filename, "%s_integrity.dat", plat_name);
}
char DebugMode[4] = "NOD"; // Either NODebug or DEBug
if(argc >= 7)
{
// Copy the sixth argument into a character array
strcpy(DebugMode, argv[6]);
// Convert the parameter to uppercase
for(int i = 0; i < 4; i++)
DebugMode = toupper(DebugMode);
// Compare the mode with list of modes
if(!strcmp("DEB", DebugMode))
{
Debug_Mode = true;
}
else if(!strcmp("NOD", DebugMode))
{
Debug_Mode = false;
}
}
// Inhibit questions from being asked. This is to assist in automation scripts
char AskMe[2] = "A";
if(argc >= 8)
{
// Copy the argument into a character array
strcpy(AskMe, argv[7]);
// Convert the parameter to uppercase
for(int i = 0; i < 2; i++)
AskMe = toupper(AskMe);
// Compare the mode with list of modes
if(!strcmp("A", AskMe))
{
AskQuestions = true;
}
else if(!strcmp("D", AskMe))
{
AskQuestions = false;
}
}
ParseAndLoad();
Set_Platform_Global_Variables();
InitPinArray();
InitLockUnlock();
InitAddressOrder();
InitInputDataOrder();
InitOutputDataOrder();
InitChipSelectRegions();
AnalyzeChain();
printf("\nJFLASH Version %s\n", VERSION);
printf("COPYRIGHT (C) 2000 - 2003 Intel Corporation\n\n");
printf("PLATFORM SELECTION:\n");
printf(" Processor= \t\t%s\n", &WORDARRAY[p_processor][0]);
printf(" Development System= \t%s\n", &WORDARRAY[p_devsys][0]);
printf(" Data Version= \t\t%s\n\n", &WORDARRAY[p_dataver][0]);
//Test operating system, if WinNT or Win2000 then get device driver handle
#ifdef WIN32
OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
if(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
HANDLE h;
h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(h == INVALID_HANDLE_VALUE)
error_out("Couldn't access giveio device");
CloseHandle(h);
}
#endif
lpt_address = test_port(); // find a valid parallel port address
if(!lpt_address)
error_out("Error, unable to find parallel port");
// set the extended control register and others for the parallel port
lpt_ECR = lpt_address + 0x402;
lpt_CTL = lpt_address + 0x02;
lpt_STAT = lpt_address + 0x01;
#define STATUS_READ _inp(lpt_STAT)
#define CONTROL_READ _inp(lpt_CTL)
test_logic_reset();
// Jtag test appears to have a bug that is preventing use of the Blackstone cable.
// {
// // jtag_test(); I want everyone to use blackstone so this needs to be out
// }
if(argc >= 3)
strcpy(filename,argv[2]);
else
{
if(!UsageShown) usage();
printf("Enter binary file name: ");
fgets(filename, MAX_IN_LENGTH, stdin); // gets(filename);
}
char Cable_Type[4] = "PAR"; // Either INS or PAR : added by jako
if(argc >= 6)
{
// Copy the fifth argument into a character array
strcpy(Cable_Type, argv[5]);
// Convert the name of the cable to uppercase
for(int i = 0; i < 4; i++)
Cable_Type = toupper(Cable_Type);
// Compare the selected cable name with all supported cables
if(!strcmp("INS", Cable_Type))
{
CableType = Insight_Jtag;
}
else if(!strcmp("PAR", Cable_Type))
{
CableType = Parallel_Jtag;
}
}
test_logic_reset();
id_command();
//bypass_all();
IR_Command(IR_Bypass);
test_logic_reset();
gpio_unlock_flash();
//check_rom_info(&max_erase_time, &dsize, &max_write_buffer, &block_size, &nblocks);
check_rom_info(&max_erase_time, &dsize, &max_write_buffer);
if( (in_file = fopen(filename, "rb" )) == NULL)
{
error_out("error, can not open binary input file");
}
check_file_info(&fsize , &last_non_zero, &last_non_ff, dsize);
// Don't waste time programming ff's at the end of the file this is the erase state
fsize = last_non_ff;
if(argc >= 5)
{
sscanf(argv[4],"%lx", &hexaddress);
if(hexaddress & 0x3)
{
printf("Start address must be 32 bit aligned!\n");
printf("Changing start address to: %lx\n",hexaddress & 0xFFFFFFFCul);
base_address = (hexaddress & 0xFFFFFFFCul);
}
// adjust address for flash addressing mode
base_address = (hexaddress & 0xFFFFFFFCul)/ADDR_MULT;
}
else
{
base_address = 0;
}
if(100 - (last_non_zero * 100)/last_non_ff > 20)
{
if(AskQuestions)
{
printf("The last %2ld percent of image file is all zeros\n",100 - (last_non_zero * 100)/last_non_ff);
printf("Would you like to save time by not programming that area? [y/n]: ");
if(toupper(_getche()) == 'Y')
fsize = last_non_zero;
}
else
{
fsize = last_non_zero;
}
}
printf("\n");
char option = 'P';
if(argc >= 4)
{
option = toupper(*argv[3]);
}
if(option == 'P')
{
EraseBlocks(base_address, fsize);
program_flash(max_write_buffer, base_address, fsize);
Write_Rom(0, F_READ_ARRAY); // put back into read mode
access_rom(READ, base_address, 0x0L, IGNORE_PORT); //extra read to get the pipeline going
time(&start);
gpio_lock_flash();
verify_flash(base_address, fsize);
}
else if(option == 'I') // just used for identifying the chips without programming anything
{
printf("Program successful completion.\n");
printf("Processor and Flash Memory Identified.\n");
printf("No programming operation performed.\n");
}
else if(option == 'N') // Program only with no verify cycle
{
EraseBlocks(base_address, fsize);
program_flash(max_write_buffer, base_address, fsize);
Write_Rom(0, F_READ_ARRAY); // put back into read mode
access_rom(READ, base_address, 0x0L, IGNORE_PORT); //extra read to get the pipeline going
time(&start);
gpio_lock_flash();
}
else if(option == 'V')
{
Write_Rom(0, F_READ_ARRAY); // put back into read mode
access_rom(READ, base_address, 0x0L, IGNORE_PORT); //extra read to get the pipeline going
time(&start);
gpio_lock_flash();
verify_flash(base_address, fsize);
}
else if(option == 'S')
{
Integrity_Check();
}
else if(option == 'T') // test the flash by attempting to write to all sectors
{
if(AskQuestions)
{
printf("About to test the entire flash memory..... \n");
printf("Is this what you want to do? (Y or N)\n");
if(toupper(_getche()) == 'Y')
{
i = 0;
do
{
test_logic_reset();
gpio_unlock_flash();
base_address = BLOCK_ADDRESS;
EraseBlocks(base_address, fsize);
program_flash(max_write_buffer, base_address, fsize);
// Write_Rom(0, F_READ_ARRAY); // put back into read mode
// access_rom(READ, base_address, 0x0L, IGNORE_PORT); //extra read to get the pipeline going
// gpio_lock_flash();
// verify_flash(base_address, fsize);
i++;
} while (BLOCK_ADDRESS != 0);
i = 0;
do
{
test_logic_reset();
gpio_unlock_flash();
base_address = BLOCK_ADDRESS;
// EraseBlocks(base_address, fsize);
// program_flash(max_write_buffer, base_address, fsize);
Write_Rom(0, F_READ_ARRAY); // put back into read mode
access_rom(READ, base_address, 0x0L, IGNORE_PORT); //extra read to get the pipeline going
gpio_lock_flash();
verify_flash(base_address, fsize);
i++;
} while (BLOCK_ADDRESS != 0);
printf("Successfully Completed a write to all sectors\n");
}
else
{
error_out("Cancelling the test....\n");
}
}
else
{
i = 0;
do
{
test_logic_reset();
gpio_unlock_flash();
base_address = BLOCK_ADDRESS;
EraseBlocks(base_address, fsize);
program_flash(max_write_buffer, base_address, fsize);
// Write_Rom(0, F_READ_ARRAY); // put back into read mode
// access_rom(READ, base_address, 0x0L, IGNORE_PORT); //extra read to get the pipeline going
// gpio_lock_flash();
// verify_flash(base_address, fsize);
i++;
} while (BLOCK_ADDRESS != 0);
i = 0;
do
{
test_logic_reset();
gpio_unlock_flash();
base_address = BLOCK_ADDRESS;
// EraseBlocks(base_address, fsize);
// program_flash(max_write_buffer, base_address, fsize);
Write_Rom(0, F_READ_ARRAY); // put back into read mode
access_rom(READ, base_address, 0x0L, IGNORE_PORT); //extra read to get the pipeline going
gpio_lock_flash();
verify_flash(base_address, fsize);
i++;
} while (BLOCK_ADDRESS != 0);
printf("Successfully Completed a write to all sectors\n");
}
}
else if(option == 'R')
{
Write_Rom(0, F_READ_ARRAY); // put back into read mode
access_rom(READ, base_address, 0x0L, IGNORE_PORT); //extra read to get the pipeline going
if((intercom_file_pointer = fopen("VB2JFLASH_READ_DATA.BIN", "w")) == NULL)
{
printf("Cannot open output file: %s\n", "VB2JFLASH_READ_DATA.BIN");
exit(1);
}
else
{
sprintf(buf, "%lX", Read_Rom(base_address));
fputs(buf,intercom_file_pointer);
fclose (intercom_file_pointer);
//printf("%X\n",Read_Rom(base_address));
}
}
else if(option == 'E')
{
if(AskQuestions)
{
printf("About to erase the entire flash memory..... \n");
printf("Is this what you want to do? (Y or N)\n");
if(toupper(_getche()) == 'Y')
{
fsize = dsize - 1;
EraseBlocks(0, fsize);
}
else
{
error_out("Cancelling the erase....\n");
}
}
else
{
fsize = dsize - 1;
EraseBlocks(0, fsize);
}
}
else
{
printf("error specifying programming option. \n\n");
printf("Commands: \n");
printf("P = Program and verify\n");
printf("V = Verify only\n");
printf("T = Test flash. Program and verify same binary at every block.\n");
printf("I = Identify processor and flash type. No programming performed.\n");
printf("R = Read a location and write to file. RESERVED OPERATION\n");
printf("N = Program only with no verify\n");
printf("E = Erase entire flash memory\n");
// printf("S = Stuck bit search. Uses platform_integrity.dat file.\n");
}
fclose(in_file);
test_logic_reset();
return 0;
}
#endif
/................................................................................
................................................................................. |
|