]> hackdaworld.org Git - my-code/fpga.git/commitdiff
initial checkin of ee2ihex code
authorhackbard <hackbard>
Mon, 12 Feb 2007 09:43:01 +0000 (09:43 +0000)
committerhackbard <hackbard>
Mon, 12 Feb 2007 09:43:01 +0000 (09:43 +0000)
fx2/ee2ihex.c [new file with mode: 0644]

diff --git a/fx2/ee2ihex.c b/fx2/ee2ihex.c
new file mode 100644 (file)
index 0000000..d438b43
--- /dev/null
@@ -0,0 +1,132 @@
+/*
+ * ee2ihex, convert eeprom content to intel hex
+ *
+ * author: hackbard@hackdaworld.org
+ *
+ * build: gcc -Wall -O3 -o ee2ihex ee2ihex.c
+ * usage: ./ee2ihex foo.iic > foo.ihx
+ *
+ */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+int hexdump(unsigned char *buf,int len) {
+
+       int i;
+
+       for(i=0;i<len;i++)
+               printf("%02x ",buf[i]);
+
+       return 0;
+}
+
+int asciidump(unsigned char *buf,int len) {
+
+       int i;
+
+       for(i=0;i<len;i++)
+               printf("%c ",(buf[i]<0x20)||(buf[i]>0x7e)?'.':buf[i]);
+
+       return 0;
+}
+
+int main(int argc,char **argv) {
+
+       int i;
+       int fd;
+       int ret;
+       int size=0;
+       unsigned short len;
+       unsigned short ihxaddr;
+       unsigned char addrl;
+       unsigned char addrh;
+       unsigned char ihxlen;
+       unsigned char crc;
+       unsigned char buf[1024];
+
+       fd=open(argv[1],O_RDONLY);
+       if(fd<0) {
+               perror("fd open");
+               return fd;
+       }
+
+       /* verify eeprom image */
+       ret=read(fd,buf,8);
+       if(ret!=8) {
+               printf("header too small\n");
+               return -1;
+       }
+
+       if(buf[0]!=0xc2) {
+               printf("wrong boot byte: %02x\n",buf[0]);
+               return -1;
+       }
+
+       printf("# ihex file, source eeprom file: %s\n",argv[1]);
+       printf("#\n");
+       printf("# header:\n");
+       printf("# ");
+       hexdump(buf,8);
+       printf(" | ");
+       asciidump(buf,8);
+       printf("\n");
+       printf("#\n");
+       printf("\n");
+
+       size=0;
+
+       while(ret) {
+               ret=read(fd,buf,4);
+               if(ret!=4) {
+                       perror("read len/addr");
+                       return ret;
+               }
+
+               len=(buf[0]<<8)|(buf[1]);
+               crc=0x00;
+               ihxaddr=(buf[2]<<8)|buf[3];
+
+               if(buf[0]&(1<<7))
+                       goto END;
+
+               while(len) {
+
+                       ihxlen=(len>=0x10)?0x10:len;
+                       addrh=ihxaddr>>8;
+                       addrl=ihxaddr&0xff;
+                       printf(":%02x%02x%02x%02x",ihxlen,addrh,addrl,0);
+                       crc=ihxlen+addrh+addrl;
+
+                       ret=read(fd,buf,ihxlen);
+                       if(ret!=ihxlen) {
+                               perror("read data");
+                               return ret;
+                       }
+
+                       for(i=0;i<ihxlen;i++) {
+                               printf("%02x",buf[i]);
+                               crc+=buf[i];
+                       }
+
+                       crc^=0xff;
+                       crc+=1;
+                       printf("%02x\n",crc);
+
+                       ihxaddr+=ihxlen;
+                       len-=ihxlen;
+               }
+
+       }
+
+END:
+       printf(":00000001ff\n");
+       close(fd);
+
+       return 0;
+}