d438b43092984da06cfee6f8cc89754b1c456d8e
[my-code/fpga.git] / fx2 / ee2ihex.c
1 /*
2  * ee2ihex, convert eeprom content to intel hex
3  *
4  * author: hackbard@hackdaworld.org
5  *
6  * build: gcc -Wall -O3 -o ee2ihex ee2ihex.c
7  * usage: ./ee2ihex foo.iic > foo.ihx
8  *
9  */
10
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18
19 int hexdump(unsigned char *buf,int len) {
20
21         int i;
22
23         for(i=0;i<len;i++)
24                 printf("%02x ",buf[i]);
25
26         return 0;
27 }
28
29 int asciidump(unsigned char *buf,int len) {
30
31         int i;
32
33         for(i=0;i<len;i++)
34                 printf("%c ",(buf[i]<0x20)||(buf[i]>0x7e)?'.':buf[i]);
35
36         return 0;
37 }
38
39 int main(int argc,char **argv) {
40
41         int i;
42         int fd;
43         int ret;
44         int size=0;
45         unsigned short len;
46         unsigned short ihxaddr;
47         unsigned char addrl;
48         unsigned char addrh;
49         unsigned char ihxlen;
50         unsigned char crc;
51         unsigned char buf[1024];
52
53         fd=open(argv[1],O_RDONLY);
54         if(fd<0) {
55                 perror("fd open");
56                 return fd;
57         }
58
59         /* verify eeprom image */
60         ret=read(fd,buf,8);
61         if(ret!=8) {
62                 printf("header too small\n");
63                 return -1;
64         }
65
66         if(buf[0]!=0xc2) {
67                 printf("wrong boot byte: %02x\n",buf[0]);
68                 return -1;
69         }
70
71         printf("# ihex file, source eeprom file: %s\n",argv[1]);
72         printf("#\n");
73         printf("# header:\n");
74         printf("# ");
75         hexdump(buf,8);
76         printf(" | ");
77         asciidump(buf,8);
78         printf("\n");
79         printf("#\n");
80         printf("\n");
81
82         size=0;
83
84         while(ret) {
85                 ret=read(fd,buf,4);
86                 if(ret!=4) {
87                         perror("read len/addr");
88                         return ret;
89                 }
90
91                 len=(buf[0]<<8)|(buf[1]);
92                 crc=0x00;
93                 ihxaddr=(buf[2]<<8)|buf[3];
94
95                 if(buf[0]&(1<<7))
96                         goto END;
97
98                 while(len) {
99
100                         ihxlen=(len>=0x10)?0x10:len;
101                         addrh=ihxaddr>>8;
102                         addrl=ihxaddr&0xff;
103                         printf(":%02x%02x%02x%02x",ihxlen,addrh,addrl,0);
104                         crc=ihxlen+addrh+addrl;
105
106                         ret=read(fd,buf,ihxlen);
107                         if(ret!=ihxlen) {
108                                 perror("read data");
109                                 return ret;
110                         }
111
112                         for(i=0;i<ihxlen;i++) {
113                                 printf("%02x",buf[i]);
114                                 crc+=buf[i];
115                         }
116
117                         crc^=0xff;
118                         crc+=1;
119                         printf("%02x\n",crc);
120
121                         ihxaddr+=ihxlen;
122                         len-=ihxlen;
123                 }
124
125         }
126
127 END:
128         printf(":00000001ff\n");
129         close(fd);
130
131         return 0;
132 }