📄 jflash.cpp
字号:
/******************************************************************************
**
** 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/27/03 11:31a $
******************************************************************************/
/*****************************************************************************
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>
#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"
extern unsigned long dbg_handler[MAX_HANDLER_SIZE];
unsigned long debug_buf[1024];
extern unsigned int dh_size;
unsigned long dh_pos;
unsigned long r14;
unsigned long ispbuf[128];
int dstbit;
int dstidx;
int rdstat;
struct {
DWORD addr;
DWORD romdat;
DWORD filedat;
} verify_error;
/*
*******************************************************************************
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 xilinx_mode(int reset);
void itp_mode(void);
void get_time_scale(void);
void ms_delay(DWORD milliseconds);
int xscale_tx_rd(unsigned long *dst);
void xscale_load_line(unsigned long addr, unsigned long *l);
void jtag_dr_wr(unsigned long *dst, unsigned long *src, int bitcount);
void jtag_rti(void);
unsigned long scan(int bits, unsigned long tms, unsigned long tdi);
int calc_parity(unsigned int v);
int xscale_init_handler(void);
unsigned long xscale_dcsr(unsigned long v, int xbreak, int hold_rst);
int xscale_rx_wr(unsigned long d);
int xscale_rx_wr_nir(unsigned long d);
bool xscale_program_flash(FILE *in_file, DWORD start_addr, DWORD rom_size, int bus_width);
bool SetGPIOState(int nGPIO, int nState);
int xscale_mem_wr(unsigned long addr, unsigned long data);
int xscale_mem_rd(unsigned long addr, unsigned long *dst);
void IssueProcInst( int command );
//<wcy
void InitDebugPar(void);
void Debug_Bus_Gpio_Pin(void);
DWORD nPWE_OUT=0;
bool is_jtag_debug=false;
//wcy>
/*
*******************************************************************************
*
* 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
//<wcy
argv[7] = if need to request user in certain situation
argv[8] = if retry 10 times when the error of flash verify is occured
argv[9] = if return immediately when the error of flash verify is occured
//wcy>
* RETURNS: void
*
*******************************************************************************
*/
void 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]="hx_pxa255";
int i = 0;
//DWORD block_size;
strcpy(base_address_string, "");
/*
//<wcy
char temp_ifdbg1[MAX_IN_LENGTH];
if(argc >= 11)
{
//Copy the first argument into a character array
strcpy(temp_ifdbg1, argv[10]);
// Convert the name of the platform to uppercase
for(unsigned int i = 0; i < strlen(temp_ifdbg1); i++)
temp_ifdbg1[i] = toupper(temp_ifdbg1[i]);
// use the parameter to construct a filename to parse
if(!strcmp("D", temp_ifdbg1))
{
sprintf(debug_data_filename, "%s.dbg", plat_name);
Debug_Bus_Gpio_Pin();
}
}
//wcy>
*/
//<wcy
char IfErrorReturn[5] = "N";
if(argc >= 10)
{
strcpy(IfErrorReturn,argv[9]);
IfErrorReturn[0] = toupper(IfErrorReturn[0]);
if(!strcmp("Y", IfErrorReturn))
ErrorReturn=true;
}
char IfRetry[5] = "N";
if(argc >= 9)
{
strcpy(IfRetry,argv[8]);
IfRetry[0] = toupper(IfRetry[0]);
if(!strcmp("Y", IfRetry))
VerifyRetry=true;
}
//wcy>
if(argc >= 2)
{
//Copy the first argument into a character array
strcpy(plat_name, argv[1]);
// Convert the name of the platform to uppercase
for(unsigned int i = 0; i < strlen(plat_name); i++)
plat_name[i] = toupper(plat_name[i]);
// use the parameter to construct a filename to parse
sprintf(data_filename, "%s.dat", plat_name);
}
else
{
if(!UsageShown) usage();
printf("\nEnter platform data file name: ");
gets(plat_name);
// Convert the name of the platform to uppercase
for(unsigned int i = 0; i < strlen(plat_name); i++)
plat_name[i] = toupper(plat_name[i]);
// use the parameter to construct a filename to parse
sprintf(data_filename, "%s.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[i] = toupper(DebugMode[i]);
// 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[i] = toupper(AskMe[i]);
// 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]);
// Use the function below for 100% OS independence
//get_time_scale(); // test the speed of this computer to compute a loop delay
//Test operating system, if WinNT or Win2000 then get device driver handle
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);
}
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: ");
gets(filename);
}
char Cable_Type[4] = "INS"; // Either INS or PAR
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[i] = toupper(Cable_Type[i]);
// 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();
//<wcy
char temp_ifdbg[MAX_IN_LENGTH];
if(argc >= 11)
{
//Copy the first argument into a character array
strcpy(temp_ifdbg, argv[10]);
// Convert the name of the platform to uppercase
for(unsigned int i = 0; i < strlen(temp_ifdbg); i++)
temp_ifdbg[i] = toupper(temp_ifdbg[i]);
// use the parameter to construct a filename to parse
if(!strcmp("D", temp_ifdbg))
{
sprintf(debug_data_filename, "%s.dbg", plat_name);
is_jtag_debug=true;
Debug_Bus_Gpio_Pin();
}
}
//wcy>
//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],"%x", &hexaddress);
if(hexaddress & 0x3)
{
printf("Start address must be 32 bit aligned!\n");
printf("Changing start address to: %x\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);
// test_logic_reset();
Write_Rom(0, F_READ_ARRAY); // put back into read mode
// try forcing asynchronous read mode
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -