📄 wrt54g.c
字号:
static unsigned int ejtag_pracc_read(unsigned int addr)
{
address_register = addr | 0xA0000000; // Force to use uncached segment
data_register = 0x0;
ExecuteDebugModule(pracc_readword_code_module);
return(data_register);
}
void ejtag_pracc_write(unsigned int addr, unsigned int data)
{
address_register = addr | 0xA0000000; // Force to use uncached segment
data_register = data;
ExecuteDebugModule(pracc_writeword_code_module);
}
static unsigned int ejtag_pracc_read_h(unsigned int addr)
{
address_register = addr | 0xA0000000; // Force to use uncached segment
data_register = 0x0;
ExecuteDebugModule(pracc_readhalf_code_module);
return(data_register);
}
void ejtag_pracc_write_h(unsigned int addr, unsigned int data)
{
address_register = addr | 0xA0000000; // Force to use uncached segment
data_register = data;
ExecuteDebugModule(pracc_writehalf_code_module);
}
void ExecuteDebugModule(unsigned int *pmodule)
{
unsigned int ctrl_reg;
unsigned int address;
unsigned int data = 0;
unsigned int offset = 0;
int finished = 0;
int DEBUGMSG = 0;
if (DEBUGMSG) printf("DEBUGMODULE: Start module.\n");
// Feed the chip an array of 32 bit values into the processor via the EJTAG port as instructions.
while (1)
{
// Read the control register. Make sure an access is requested, then do it.
while(1)
{
set_instr(INSTR_CONTROL);
ctrl_reg = ReadWriteData(PRACC | PROBEN | SETDEV);
if (ctrl_reg & PRACC)
break;
if (DEBUGMSG) printf("DEBUGMODULE: No memory access in progress!\n");
}
set_instr(INSTR_ADDRESS);
address = ReadData();
// Check for read or write
if (ctrl_reg & PRNW) // Bit set for a WRITE
{
// Read the data out
set_instr(INSTR_DATA);
data = ReadData();
// Clear the access pending bit (let the processor eat!)
set_instr(INSTR_CONTROL);
ctrl_reg = ReadWriteData(PROBEN | SETDEV);
// Processor is writing to us
if (DEBUGMSG) printf("DEBUGMODULE: Write 0x%08X to address 0x%08X\n", data, address);
// Handle Debug Write
// If processor is writing to one of our psuedo virtual registers then save off data
if (address == MIPS_VIRTUAL_ADDRESS_ACCESS) address_register = data;
if (address == MIPS_VIRTUAL_DATA_ACCESS) data_register = data;
}
else
{
// Check to see if its reading at the debug vector. The first pass through
// the module is always read at the vector, so the first one we allow. When
// the second read from the vector occurs we are done and just exit.
if (address == MIPS_DEBUG_VECTOR_ADDRESS)
{
if (finished++) // Allows ONE pass
{
if (DEBUGMSG) printf("DEBUGMODULE: Finished module.\n");
return;
}
}
// Processor is reading from us
if (address >= MIPS_DEBUG_VECTOR_ADDRESS)
{
// Reading an instruction from our module so fetch the instruction from the module
offset = (address - MIPS_DEBUG_VECTOR_ADDRESS) / 4;
data = *(unsigned int *)(pmodule + offset);
if (DEBUGMSG) printf("DEBUGMODULE: Instruction read at 0x%08X offset -> %04d data -> 0x%08X\n", address, offset, data); //fflush(stdout);
}
else
{
// Reading from our virtual register area
if (DEBUGMSG) printf("DEBUGMODULE: Read address 0x%08X data = 0x%08X\n", address, data);
// Handle Debug Read
// If processor is reading from one of our psuedo virtual registers then give it data
if (address == MIPS_VIRTUAL_ADDRESS_ACCESS) data = address_register;
if (address == MIPS_VIRTUAL_DATA_ACCESS) data = data_register;
}
// Send the data out
set_instr(INSTR_DATA);
data = ReadWriteData(data);
// Clear the access pending bit (let the processor eat!)
set_instr(INSTR_CONTROL);
ctrl_reg = ReadWriteData(PROBEN | SETDEV);
}
}
}
void chip_detect(void)
{
unsigned int id = 0x0;
processor_chip_type* processor_chip = processor_chip_list;
lpt_openport();
printf("Probing bus ... ");
if (skipdetect)
{
// Manual Override CPU Chip ID
test_reset();
instruction_length = instrlen;
set_instr(INSTR_IDCODE);
id = ReadData();
printf("Done\n\n");
printf("Instruction Length set to %d\n\n",instruction_length);
printf("CPU Chip ID: "); ShowData(id); printf("*** CHIP DETECTION OVERRIDDEN ***\n\n");
return;
}
else
{
// Auto Detect CPU Chip ID
while (processor_chip->chip_id)
{
test_reset();
if (instrlen)
instruction_length = instrlen;
else
instruction_length = processor_chip->instr_length;
set_instr(INSTR_IDCODE);
id = ReadData();
if (id == processor_chip->chip_id)
{
printf("Done\n\n");
printf("Instruction Length set to %d\n\n",instruction_length);
printf("CPU Chip ID: "); ShowData(id); printf("*** Found a %s chip ***\n\n", processor_chip->chip_descr);
return;
}
processor_chip++;
}
}
printf("Done\n\n");
printf("Instruction Length set to %d\n\n",instruction_length);
printf("CPU Chip ID: "); ShowData(id); printf("*** Unknown or NO CPU Chip ID Detected ***\n\n");
printf("*** Possible Causes:\n");
printf(" 1) WRT54G/GS is not Connected.\n");
printf(" 2) WRT54G/GS is not Powered On.\n");
printf(" 3) Improper JTAG Cable.\n");
printf(" 4) Unrecognized CPU Chip ID.\n");
chip_shutdown();;
exit(0);
}
void check_ejtag_features()
{
unsigned int features;
set_instr(INSTR_IMPCODE);
features = ReadData();
printf(" - EJTAG IMPCODE ....... : "); ShowData(features);
// EJTAG Version
ejtag_version = (features >> 29) & 7;
printf(" - EJTAG Version ....... : ");
if (ejtag_version == 0) printf("1 or 2.0\n");
else if (ejtag_version == 1) printf("2.5\n");
else if (ejtag_version == 2) printf("2.6\n");
else printf("Unknown (%d is a reserved value)\n", ejtag_version);
// EJTAG DMA Support
USE_DMA = !(features & (1 << 14));
printf(" - EJTAG DMA Support ... : %s\n", USE_DMA ? "Yes" : "No");
if (force_dma) { USE_DMA = 1; printf(" *** DMA Mode Forced On ***\n"); }
if (force_nodma) { USE_DMA = 0; printf(" *** DMA Mode Forced Off ***\n"); }
printf("\n");
}
void chip_shutdown(void)
{
fflush(stdout);
test_reset();
lpt_closeport();
}
void run_backup(char *filename, unsigned int start, unsigned int length)
{
unsigned int addr, data;
FILE *fd;
int counter = 0;
int percent_complete = 0;
char newfilename[128] = "";
time_t start_time = time(0);
time_t end_time, elapsed_seconds;
struct tm* lt = localtime(&start_time);
char time_str[15];
sprintf(time_str, "%04d%02d%02d_%02d%02d%02d",
lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
lt->tm_hour, lt->tm_min, lt->tm_sec
);
printf("*** You Selected to Backup the %s ***\n\n",filename);
strcpy(newfilename,filename);
strcat(newfilename,".SAVED");
if (issue_timestamp)
{
strcat(newfilename,"_");
strcat(newfilename,time_str);
}
fd = fopen(newfilename, "wb" );
if (fd<=0)
{
fprintf(stderr,"Could not open %s for writing\n", newfilename);
exit(1);
}
printf("=========================\n");
printf("Backup Routine Started\n");
printf("=========================\n");
printf("\nSaving %s to Disk...\n",newfilename);
for(addr=start; addr<(start+length); addr+=4)
{
counter += 4;
percent_complete = (counter * 100 / length);
if (!silent_mode)
if ((addr&0xF) == 0) printf("[%3d%% Backed Up] %08x: ", percent_complete, addr);
data = ejtag_read(addr);
fwrite( (unsigned char*) &data, 1, sizeof(data), fd);
if (silent_mode) printf("%4d%% bytes = %d\r", percent_complete, counter);
else printf("%08x%c", data, (addr&0xF)==0xC?'\n':' ');
fflush(stdout);
}
fclose(fd);
printf("Done (%s saved to Disk OK)\n\n",newfilename);
printf("bytes written: %d\n", counter);
printf("=========================\n");
printf("Backup Routine Complete\n");
printf("=========================\n");
time(&end_time);
elapsed_seconds = difftime(end_time, start_time);
printf("elapsed time: %d seconds\n", (int)elapsed_seconds);
}
void run_flash(char *filename, unsigned int start, unsigned int length)
{
unsigned int addr, data ;
FILE *fd ;
int counter = 0;
int percent_complete = 0;
time_t start_time = time(0);
time_t end_time, elapsed_seconds;
printf("*** You Selected to Flash the %s ***\n\n",filename);
fd=fopen(filename, "rb" );
if (fd<=0)
{
fprintf(stderr,"Could not open %s for reading\n", filename);
exit(1);
}
printf("=========================\n");
printf("Flashing Routine Started\n");
printf("=========================\n");
if (issue_erase) sflash_erase_area(start,length);
printf("\nLoading %s to Flash Memory...\n",filename);
for(addr=start; addr<(start+length); addr+=4)
{
counter += 4;
percent_complete = (counter * 100 / length);
if (!silent_mode)
if ((addr&0xF) == 0) printf("[%3d%% Flashed] %08x: ", percent_complete, addr);
fread( (unsigned char*) &data, 1,sizeof(data), fd);
// Erasing Flash Sets addresses to 0xFF's so we can avoid writing these (for speed)
if (issue_erase) {
if (!(data == 0xFFFFFFFF))
sflash_write_word(addr, data);
}
else sflash_write_word(addr, data); // Otherwise we gotta flash it all
if (silent_mode) printf("%4d%% bytes = %d\r", percent_complete, counter);
else printf("%08x%c", data, (addr&0xF)==0xC?'\n':' ');
fflush(stdout);
data = 0xFFFFFFFF; // This is in case file is shorter than expected length
}
fclose(fd);
printf("Done (%s loaded into Flash Memory OK)\n\n",filename);
sflash_reset();
printf("=========================\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -