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

📄 jflash.cpp

📁 wince的boot程序下载:并口JFlashmm源代码。 VC6.0编译。如果用新的flash芯片需要修改源代码后编译。
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/**********************************************************************************  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 bysearching for the string "PORTING_GUIDE".*******************************************************************************/#include <stdio.h>#include <time.h>#include <string.h>#ifdef __linux__#include <stdlib.h>#include <unistd.h>#include <ctype.h>//#include <asm/io.h>#include <sys/io.h>typedef unsigned long DWORD;typedef unsigned short WORD;typedef int INT32;typedef int BOOL;const BOOL FALSE=0;const BOOL TRUE=1;#define _inp(a) inb(a)#define _outp(a,d) outb(d,a)#define _getche() getchar()#else#include <windows.h>#include <conio.h>#endif /* __linux__ */#include "Compile_switches.h"#include "Jflash.h"#include "jtag.h"#include "Global_Variables.h"/********************************************************************************Forward declarations********************************************************************************/int putp(int,int, int); // writes the JTAG data on the parallel portvoid 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 ProcessorDWORD access_rom(int, DWORD, DWORD, int);	// Passes read/write/setup data for the Flash memoryDWORD access_bus(int, DWORD, DWORD, int);	// Read/write access to the Processor pinsvoid Write_Rom(DWORD address, DWORD data);	// Writes to ROMDWORD Read_Rom(DWORD address);				// Reads from ROMint test_port(void);	// Looks for and finds a valid parallel port addressint check_id(char*);	// Compares the device IDs for the 3 JTAG chips to expected valuesvoid 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);inline void port_init(int port){#ifdef __linux__    if (ioperm(port, 3, 1) != 0)    {        perror("Couldn't enable port!");        exit(1);    }#endif /* __linux__ */}inline void port_finish(int port){#ifdef __linux__    ioperm(port, 3, 0); // ignore any errors, don't matter#endif /* __linux__ */}/********************************************************************************** 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*********************************************************************************/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]);#ifndef __linux__		// 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]);#endif		// 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);	}	else	{		if(!UsageShown) usage();		printf("\nEnter platform data file name: ");		fgets(plat_name, sizeof(plat_name)-1, stdin);#ifndef __linux__		// 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]);#endif		// 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[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]);#ifndef __linux__    //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);	}#endif	lpt_address = test_port();	// find a valid parallel port address	if(!lpt_address)		error_out("Error, unable to find parallel port");    port_init(lpt_address); 	// 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, sizeof(filename)-1, stdin);		}	char Cable_Type[4] = "PAR";			// Either INS or PAR	if(argc >= 6)	{		// Copy the fifth argument into a character array		strncpy(Cable_Type, argv[5], 4);		// 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(strncmp("INS", Cable_Type, 4) == 0)		{			CableType = Insight_Jtag;		}		else if(strncmp("PAR", Cable_Type, 4) == 0)		{			 CableType = Parallel_Jtag;		}		else if(strncmp("WIG", Cable_Type, 4) == 0)		{			 CableType = Wiggler_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],"%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(getchar()) == '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

⌨️ 快捷键说明

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