1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
#include <string.h>
#include "javr.h"
int jAVR(Jtag &jtag, unsigned int id, char * flashfile, bool verify, bool lock,
const char * eepromfile, const char * fusefile)
{
/*bool menu = (!flashfile && !eepromfile && !fusefile);*/
unsigned short partnum = (id>>12) & 0xffff;
int i;
AVR_Data gDeviceData;
BOOT_Size gDeviceBOOTSize;
int Index;
/* Find Device index*/
Index=UNKNOWN_DEVICE;
for (i=0; i< gAVR_Data[i].jtag_id; i++)
if (gAVR_Data[i].jtag_id == partnum)
{
gDeviceData=gAVR_Data[i];
Index=i;
gDeviceBOOTSize=gBOOT_Size[i];
break;
}
if(Index==UNKNOWN_DEVICE)
{
fprintf(stderr, "Unknown device\n");
return 1;
}
fprintf(stderr, "%s, Rev %c with",gDeviceData.name,((id>>28) & 0xf)+'A');
fprintf(stderr, " %ldK Flash, %u Bytes EEPROM and %u Bytes RAM\r\n",
gDeviceData.flash/1024,
gDeviceData.eeprom, gDeviceData.ram);
ProgAlgAVR alg (jtag, gDeviceData.fp_size);
if (fusefile)
{
//EncodeATMegaFuseBits();
}
else
{
byte fuses[4];
alg.read_fuses(fuses);
fprintf(stderr, "Extended Fuse Byte: 0x%02x High Fuse Byte: 0x%02x"
" Low Fuse Byte: 0x%02x LOCK Byte 0x%02x\n",
fuses[FUSE_EXT], fuses[FUSE_HIGH],
fuses[FUSE_LOW], fuses[FUSE_LOCK]);
}
if (eepromfile)
{
}
else
{
byte eeprom[16];
int i;
alg.read_eeprom(0xff0, eeprom, 16);
fprintf(stderr, "EEPROM at 0xff0:");
for (i=0; i<16; i++)
fprintf(stderr, " %02x ", eeprom[i]);
fprintf(stderr, "\n");
}
if (flashfile)
{
SrecFile file;
if(file.readSrecFile(flashfile, 0) <0)
return 1;
if (file.getLength() == 0)
{
fprintf(stderr, "%s or %s.rom not found or no valid SREC File\n",
flashfile, flashfile);
goto bailout;
}
if (verify)
{
byte buffer[gDeviceData.fp_size];
int count = 0;
unsigned int i, j, k, match;
for (i = file.getStart(); i < file.getEnd() ; i+= gDeviceData.fp_size)
{
unsigned int to_read;
fprintf(stdout, "\rVerify page %4d/%4d", i/gDeviceData.fp_size,
file.getLength()/gDeviceData.fp_size);
fflush(stderr);
if ( i< (file.getEnd() - gDeviceData.fp_size))
to_read = gDeviceData.fp_size;
else
to_read = (file.getEnd() -i);
alg.pageread_flash(i, buffer, to_read);
match = memcmp(buffer, file.getData()+i, to_read);
if (match !=0)
{
fprintf(stderr, "\n");
for (j = 0; j< to_read; j +=32)
{
match = memcmp(buffer+j, file.getData()+i+j, 32);
if (match !=0)
{
fprintf(stderr, "Mismatch at address: 0x%08x\n", i+j);
fprintf(stderr, "Device: ");
for(k =0; k<32; k++)
fprintf(stderr, "%02x ", buffer[j+k]);
fprintf(stderr, "\nFile : ");
for(k =0; k<32; k++)
fprintf(stderr, "%02x ", file.getData()[i+j +k ]);
fprintf(stderr, "\n : ");
for(k =0; k<32; k++)
{
if(buffer[j+k] != file.getData()[i+j+k])
{
fprintf(stderr, "^^^");
count++;
}
else
fprintf(stderr, " ");
}
fprintf(stderr, "\n");
}
}
}
if (count >10)
return 1;
}
if(count)
{
fprintf(stderr, "Chip Verify failed\n");
goto bailout;
}
else
fprintf(stderr, "\tSuccess \n");
}
else
{
unsigned int i;
if (alg.erase())
{
fprintf(stderr, "Chip Erase failed\n");
goto bailout;
}
else
fprintf(stderr, "Chip Erase success\n");
if (file.getStart() & gDeviceData.fp_size)
{
fprintf(stderr, " File doesn't start at Page Border, aborting\n");
goto bailout;
}
for (i= file.getStart(); i < file.getLength()-gDeviceData.fp_size;
i += gDeviceData.fp_size)
{
fprintf(stdout, "\rWriting page %4d/%4d", i/gDeviceData.fp_size,
file.getLength()/gDeviceData.fp_size);
if (alg.pagewrite_flash(i, file.getData()+i, gDeviceData.fp_size))
{
fprintf(stderr, "\nError writing page %d\n",
i/gDeviceData.fp_size);
goto bailout;
}
fflush(stderr);
}
/* eventual last page is not full. Fill it up with FILL_BYTE)*/
if (i != file.getLength())
{
byte *buffer = new byte[gDeviceData.fp_size];
memcpy(buffer, file.getData()+i,file.getLength() -i);
memset(buffer + (file.getLength() -i), FILL_BYTE,
gDeviceData.fp_size- (file.getLength() -i));
if (alg.pagewrite_flash(i, buffer, gDeviceData.fp_size))
{
fprintf(stderr, "\nError writing page %d\n",
i/gDeviceData.fp_size);
goto bailout;
}
else
{
fprintf(stdout, "\rWriting page %4d/%4d",
i/gDeviceData.fp_size,
file.getEnd()/gDeviceData.fp_size);
fflush(stderr);
}
}
fprintf(stderr, " done.\n"
"Bytes from 0x%05x to 0x%05x filled with 0x%02x\n",
file.getEnd(), i+gDeviceData.fp_size -1, FILL_BYTE);
}
}
return 0;
bailout:
return 1;
}
|