📄 download.c
字号:
// Indicate that there was an error.
//
bError = 1;
//
// Stop trying to send this block.
//
break;
}
//
//if the Flash or EEPROM program fail,
//
if(cChar == '^')
{
bError = 1;
printf("During programming,the %x-%x data fail\n",lIdx,lIdx+1024);
break;
}
//
//if the Flash or EEPROM block checksum is bad,
//
if(cChar == '@')
{
bError = 1;
printf("During programming,the %x-%x block checksum bad \n",lIdx,lIdx+1024);
}
}
while(cChar != '#');
//
// If there was an error, then quit sending data.
//
if(bError)
{
break;
}
//
// Print out our progress.
//
printf("\b\b\b\b\b%3d%%)",
(((lIdx + 1024) / 1024) * 100) / (lFileSize / 1024));
}
printf("\r \r");
//
// If there has not been an error thus far, read the final status from the
// target.
//
if(!bError)
{
//
// Read characters from the serial port until we receive a '*'.
//
while(1)
{
//
// Read a character from the serial port.
//
cChar = ReceiveChar(0);
//
// If the character is a '^', then there was an error programming
// the FLASH.
//
if(cChar == '^')
{
bError = 1;
}
//
// If the character is a '*', then we are done.
//
if(cChar == '*')
{
break;
}
}
}
}
//
// Tell the user we are done.
//
if(!bError)
{
if(bHaveFile)
{
printf("Successfully programmed '%s'.\n", argv[argc - 1]);
}
}
else
{
if(bHaveFile)
{
printf("Failed to program '%s'.\n", argv[argc - 1]);
}
}
//
//
//
if(bMAC)
{
//printf("\rWrite MAC Address into EEPROM or Read MAC Address from EEPROM.");
/////////////////////////////////////////////////////////////////////////////////////
//
// Send the program FLASH command.
//
switch (lStorage)
{
case 1:
SendChar('F');
printf("\rProgram the nor FLASH ----error.");
break;
case 2:
if(bReadEEPROM==1)
{
//if read eeprom image ,the offset and length need to be change
//the defaule value ,only read the MAC address in 0x1000.
fprintf(stderr, "Read MAC Address.\n\n");
lOffset = 0x1000;//0;
lFileSize=0x20;//1024;
SendChar('R');
//printf("\rRead MAC Address from the spi EEPROM.Base=%x,Len=%x.",lOffset,lFileSize);
}
else
{
lOffset = 0x1000;//mOffset;
lFileSize=1024;//mFileSize;
SendChar('S');
//printf("\rProgram the spi EEPROM.Base=%x,Len=%x.",lOffset,lFileSize);
}
break;
default:
printf("\rNo storage device was specified and flashing G&R LED.");
break;
}
//
// Read the next character from the serial port.
//
cChar = ReceiveChar(0);
//
// See if this is a 'X'.
//
if(cChar == 'A')
{
printf("\rFind an AMD nor flash.");
} else if (cChar == 'I')
{
printf("\rFind an Intel nor flash.");
} else if (cChar == 'S')
{
iEEPORMType = 1;
printf("\rFind an SST EEPROM.");
}else if (cChar == 'O')
{
iEEPORMType = 0;
printf("\rFind an AT EEPROM.");
}
else if(cChar == 'X')
{
fprintf(stderr, "The board contains an unknown %s.\n",
bEEPROM ? "EEPROM" : "FLASH");
ClosePort(lPort,bUSBPort);
return(1);
} else
{
}
//
// Send the starting offset.
//
SendChar((char)(lOffset & 0xFF));
SendChar((char)((lOffset >> 8) & 0xFF));
SendChar((char)((lOffset >> 16) & 0xFF));
SendChar((char)((lOffset >> 24) & 0xFF));
//
// Send the length of the data file.
//
SendChar((char)(lFileSize & 0xFF));
SendChar((char)((lFileSize >> 8) & 0xFF));
SendChar((char)((lFileSize >> 16) & 0xFF));
SendChar((char)((lFileSize >> 24) & 0xFF));
//
// Tell the user that we are erasing the FLASH.
//
if((bHaveFile)||(bReadEEPROM==1))
{
if(iEEPORMType==0)
SendChar('N');
//printf("\r The EEPROM had been erased .Base=%x,Len=%x.",lOffset,lFileSize);
else if (iEEPORMType==1)
{
SendChar('R');
//printf("\r The EEPROM need to be erased .Base=%x,Len=%x.",lOffset,lFileSize);
}
}
else
{
SendChar('R');
//printf("\r The EEPROM need to be erased .Base=%x,Len=%x.",lOffset,lFileSize);
}
if(bReadEEPROM==0)
{
printf("\rErasing the %s... ...", bEEPROM ? "EEPROM" : "FLASH");
}
//
// Wait until we receive a '!' indicating that the FLASH has been erased.
//
while(1)
{
//
// Read the next character from the serial port.
//
cChar = ReceiveChar(0);
//
// Quit waiting if this is a '!'.
//
if(cChar == '!')
{
break;
}
//
// See if this is a '&'.
//
if(cChar == '&')
{
printf("\r \r");
fprintf(stderr, "The image is too large for the %s.\n",
bEEPROM ? "EEPROM" : "FLASH");
ClosePort(lPort,bUSBPort);
return(1);
}
//
// See if this is a '%'.
//
if(cChar == '%')
{
printf("\r \r");
fprintf(stderr, "The offset '%ld' is not valid for the %s.\n",
lOffset, bEEPROM ? "EEPROM" : "FLASH");
ClosePort(lPort,bUSBPort);
return(1);
}
}
//
// Tell the user that we are downloading the file data.
//
if(bReadEEPROM==0)
{
printf("\r \r");
printf("\rProgramming the %s...( 0%%)", bEEPROM ? "EEPROM" : "FLASH");
}
else
{
printf("\rReading the %s... ", bEEPROM ? "EEPROM" : "FLASH");
for(lIdx = 0; lIdx < lFileSize; lIdx ++)
{
cBuffer[lIdx] = 0;
cBuffer[lIdx] = ReceiveChar(10);
//printf("%x",ReceiveChar(100));
}
//for(lIdx = 0; lIdx < 0x20; lIdx ++)
//{
// printf("%c",cBuffer[lIdx]);
//}
printf("\r \r");
printf("\rMAC Address : ");
for(lIdx = 4; lIdx < 10; lIdx ++)
{
//if((cBuffer[lIdx]&0xff)!=0)
//{
printf("%02x",cBuffer[lIdx]&0xff);
//}
//else
//{
// printf("00");
//}
}
printf("\n");
return 1;
}
//
// Send the actual data in the file.
//
for(lIdx = 0; lIdx < lFileSize; lIdx += 1024)
{
//
// Clear the data buffer.
//
memset(bBuffer, 0, 1024);
lLoop = 1024;
//
// See if the Ethernet MAC address is to be programmed, and the correct
// memory location has been reached.
//
{
//
// The first four bytes should be "EMAC", the tag to indicate a
// Ethernet MAC block.
//
bBuffer[0] = 'E';
bBuffer[1] = 'M';
bBuffer[2] = 'A';
bBuffer[3] = 'C';
//
// The next six bytes are the Ethernet MAC address.
//
//cBuffer[4] = 0x00;
//cBuffer[5] = 0xdc;
//cBuffer[6] = 0x6c;
//cBuffer[7] = lType - 9300;
//cBuffer[8] = lNumber / 256;
//cBuffer[9] = lNumber & 255;
bBuffer[4] = 0;
bBuffer[5] = 0;
bBuffer[6] = 0;
bBuffer[7] = 0;
bBuffer[8] = 0;
bBuffer[9] = 0;
{
int i;
for(i=0;i<strlen(MACString);i++)
{
if((MACString[i]>='0')&&(MACString[i]<='9'))
{
bBuffer[4+i/2]=bBuffer[4+i/2]*(i%2)*16+(MACString[i]-'0');
}
else if((MACString[i]>='a')&&(MACString[i]<='f'))
{
bBuffer[4+i/2]=(bBuffer[4+i/2]*(i%2))*16+(MACString[i]-'a'+10);
}
else if((MACString[i]>='A')&&(MACString[i]<='F'))
{
bBuffer[4+i/2]=(bBuffer[4+i/2]*(i%2))*16+(MACString[i]-'A'+10);
}
}
if(strlen(MACString)%2==1)
{
bBuffer[4+i/2]=bBuffer[4+i/2]*(i%2)*16;
}
//printf("strlen(MACString)=%x\n",strlen(MACString));
//printf("bBuffer[4]=%x\n",bBuffer[4]);
//printf("bBuffer[5]=%x\n",bBuffer[5]);
//printf("bBuffer[6]=%x\n",bBuffer[6]);
//printf("bBuffer[7]=%x\n",bBuffer[7]);
//printf("bBuffer[8]=%x\n",bBuffer[8]);
//printf("bBuffer[9]=%x\n",bBuffer[9]);
}
//
// The next six bytes are reserved.
//
bBuffer[10] = 0x00;
bBuffer[11] = 0x00;
bBuffer[12] = 0x00;
bBuffer[13] = 0x00;
bBuffer[14] = 0x00;
bBuffer[15] = 0x00;
//
// The next 16 bytes are the string name for this board.
//
//bBuffer[16] = 'e';
//bBuffer[17] = 'd';
//bBuffer[18] = 'b';
//bBuffer[19] = '0' + (lType / 1000);
//bBuffer[20] = '0' + ((lType / 100) % 10);
//bBuffer[21] = '0' + ((lType / 10) % 10);
//bBuffer[22] = '0' + (lType % 10);
//bBuffer[23] = '-';
//bBuffer[24] = '0' + (lNumber / 10000);
//bBuffer[25] = '0' + ((lNumber / 1000) % 10);
//bBuffer[26] = '0' + ((lNumber / 100) % 10);
//bBuffer[27] = '0' + ((lNumber / 10) % 10);
//bBuffer[28] = '0' + (lNumber % 10);
//bBuffer[29] = 0x00;
//bBuffer[30] = 0x00;
//bBuffer[31] = 0x00;
//
// The next 16 bytes are the string name for this board.
//
bBuffer[16] = 0x00;
bBuffer[17] = 0x00;
bBuffer[18] = 0x00;
bBuffer[19] = 0x00;
bBuffer[20] = 0x00;
bBuffer[21] = 0x00;
bBuffer[22] = 0x00;
bBuffer[23] = 0x00;
bBuffer[24] = 0x00;
bBuffer[25] = 0x00;
bBuffer[26] = 0x00;
bBuffer[27] = 0x00;
bBuffer[28] = 0x00;
bBuffer[29] = 0x00;
bBuffer[30] = 0x00;
bBuffer[31] = 0x00;
{
int i;
for(i=0;i<strlen(MACString);i++)
{
bBuffer[i+16]= MACString[i];
//bBuffer[i+24]= MACString[i];
//printf("bBuffer[9]=%x\n",bBuffer[i+20]);
}
}
//printf("------------------\n");
}
//
// If we could not read 1K from the file, then fill the remainder of
// the buffer with zeros.
//
for(; lLoop < 1024; lLoop++)
{
bBuffer[lLoop] = 0;
}
//
// Send this block of data until it is correctly received and
// programmed into the FLASH.
//
do
{
//
// Send the data for this block.
//
for(lLoop = 0; lLoop < 1024; lLoop++)
{
SendChar(bBuffer[lLoop]);
if(bUSBPort==1)
{
if((lLoop & 15) == 15)
{
//
// Wait until the transmit buffer is empty.
//
WaitTillEmpty();
}
}
}
//
// Compute the checksum for this block.
//
for(lLoop = 0, lSum = 0; lLoop < 1024;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -