]> www.hackdaworld.org Git - my-code/fpga.git/blob - fx2/ee2ihex.c
now empty line seems to be allowd ...
[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
81         size=0;
82
83         while(ret) {
84                 ret=read(fd,buf,4);
85                 if(ret!=4) {
86                         perror("read len/addr");
87                         return ret;
88                 }
89
90                 len=(buf[0]<<8)|(buf[1]);
91                 crc=0x00;
92                 ihxaddr=(buf[2]<<8)|buf[3];
93
94                 if(buf[0]&(1<<7))
95                         goto END;
96
97                 while(len) {
98
99                         ihxlen=(len>=0x10)?0x10:len;
100                         addrh=ihxaddr>>8;
101                         addrl=ihxaddr&0xff;
102                         printf(":%02x%02x%02x%02x",ihxlen,addrh,addrl,0);
103                         crc=ihxlen+addrh+addrl;
104
105                         ret=read(fd,buf,ihxlen);
106                         if(ret!=ihxlen) {
107                                 perror("read data");
108                                 return ret;
109                         }
110
111                         for(i=0;i<ihxlen;i++) {
112                                 printf("%02x",buf[i]);
113                                 crc+=buf[i];
114                         }
115
116                         crc^=0xff;
117                         crc+=1;
118                         printf("%02x\n",crc);
119
120                         ihxaddr+=ihxlen;
121                         len-=ihxlen;
122                 }
123
124         }
125
126 END:
127         printf(":00000001ff\n");
128         close(fd);
129
130         return 0;
131 }