-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathftdieeprom.c
104 lines (82 loc) · 2.31 KB
/
ftdieeprom.c
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
/*
Read and Write the eeprom of an ftdi device via libftdi
Copyright Ricardo Ribalda - 2014 - [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdint.h>
#include <ftdi.h>
#include <libusb.h>
#define EEPROM_SIZE 256
enum {WRITE,READ};
void use(char *name){
fprintf(stdout,"%s [R|W] file\n",name);
return;
}
static int fix_csum(unsigned char *eeprom,int len){
int i;
uint16_t csum=0xaaaa;
uint16_t old_csum=eeprom[len-1]<<8 | eeprom[len-2];
for (i=0;i<(len/2)-1;i++){
uint16_t value;
value=eeprom[i*2]+((eeprom[i*2+1])<<8);
csum=value^csum;
csum= (csum<<1)|(csum>>15);
}
if (csum!=old_csum){
fprintf(stderr,"Old csum 0x%.4x, new csum 0x%4.x\n",old_csum,csum);
eeprom[len-2]=csum;
eeprom[len-1]=csum>>8;
}
return 0;
}
int main(int argc, char *argv[]){
struct ftdi_context ftdi;
unsigned char eeprom_buf[EEPROM_SIZE];
int ret;
FILE *file;
int mode=READ;
if (argc!=3){
use(argv[0]);
return -1;
}
if ((argv[1][0]=='w') || (argv[1][0]=='W'))
mode=WRITE;
file=fopen(argv[2],(mode==READ)?"w":"r");
if (!file){
perror(argv[2]);
return -1;
}
ftdi_init(&ftdi);
ftdi.eeprom_size=EEPROM_SIZE;
ret=ftdi_usb_open(&ftdi, 0x0403, 0x6010);
if (ret){
printf("Error: %s\n", ftdi.error_str);
fclose(file);
return -1;
}
if (mode==READ){
ret=ftdi_read_eeprom(&ftdi, eeprom_buf);
fwrite(eeprom_buf,sizeof(eeprom_buf),1,file);
}
else{
fread(eeprom_buf,sizeof(eeprom_buf),1,file);
fix_csum(eeprom_buf,sizeof(eeprom_buf));
ftdi_erase_eeprom(&ftdi);
ftdi_write_eeprom(&ftdi, eeprom_buf);
usb_reset(ftdi.usb_dev);
}
fclose(file);
ftdi_deinit (&ftdi);
return 0;
}