]> hackdaworld.org Git - my-code/arm.git/commitdiff
(ntested!) implementation of firmware_to_ram routine
authorhackbard <hackbard@sage.physik.uni-augsburg.de>
Wed, 1 Aug 2007 12:53:18 +0000 (14:53 +0200)
committerhackbard <hackbard@sage.physik.uni-augsburg.de>
Wed, 1 Aug 2007 12:53:18 +0000 (14:53 +0200)
betty/lpcload.c

index 0bf3006aa7dd262c3ce0872bd309cd7e4c1626ed..c2b111425f4befe3372996584b3a32b6eb1d0d86 100644 (file)
 
 #define CRYSTFREQ              "10000"
 
-#define BUFSIZE                        64
+#define BUFSIZE                        128
 
 typedef unsigned char u8;
+typedef unsigned short u16;
+typedef unsigned int u32;
 
 typedef struct s_lpc {
        int sfd;                /* serial fd */
@@ -126,7 +128,7 @@ int open_firmware(t_lpc *lpc) {
        return lpc->fwfd;
 }
 
-int txrx(t_lpc *lpc,u8 *buf,int len,u8 type) {
+int txrx(t_lpc *lpc,char *buf,int len,u8 type) {
 
        int ret,cnt;
        int i;
@@ -218,7 +220,7 @@ int txrx(t_lpc *lpc,u8 *buf,int len,u8 type) {
 
 int bl_init(t_lpc *lpc) {
 
-       u8 buf[BUFSIZE];
+       char buf[BUFSIZE];
        int len;
 
        /* auto baud sequence */
@@ -252,7 +254,7 @@ int bl_init(t_lpc *lpc) {
 
 int read_part_id(t_lpc *lpc) {
 
-       u8 buf[BUFSIZE];
+       char buf[BUFSIZE];
 
        memcpy(buf,"J\r\n",3);
        txrx(lpc,buf,3,TXRX_TYPE_CMD);
@@ -263,7 +265,7 @@ int read_part_id(t_lpc *lpc) {
 
 int read_bcv(t_lpc *lpc) {
 
-       u8 buf[BUFSIZE];
+       char buf[BUFSIZE];
        char *ptr;
 
        memcpy(buf,"K\r\n",3);
@@ -276,7 +278,7 @@ int read_bcv(t_lpc *lpc) {
        return 0;
 }
 
-int uuencode(u8 *in,u8 *out) {
+int uuencode(u8 *in,char *out) {
 
        out[0]=0x20+((in[0]>>2)&0x3f);
        out[1]=0x20+(((in[0]<<4)|(in[1]>>4))&0x3f);
@@ -286,11 +288,11 @@ int uuencode(u8 *in,u8 *out) {
        return 0;
 }
 
-int write_to_ram(t_lpc *lpc,u8 *buf,int addr,int len) {
+int write_to_ram(t_lpc *lpc,u8 *buf,u32 addr,int len) {
 
        int lcount;
-       u8 checksum;
-       u8 txrxbuf[BUFSIZE];
+       u32 checksum;
+       char txrxbuf[BUFSIZE];
        int count,bcnt;
        int nlen,slen;
        int i;
@@ -310,11 +312,8 @@ int write_to_ram(t_lpc *lpc,u8 *buf,int addr,int len) {
        for(i=len;i<nlen;i++) buf[i]=0;
 
        /* prepare write command */
-       snprintf(txrxbuf,BUFSIZE,"W %d %d",addr,len);
+       snprintf(txrxbuf,BUFSIZE,"W %d %d\r\n",addr,len);
        slen=strlen(txrxbuf);
-       txrxbuf[slen]='\r';
-       txrxbuf[slen+1]='\0';
-       slen+=2;
 
        /* send command and check return code */
        txrx(lpc,txrxbuf,slen,TXRX_TYPE_CMD);
@@ -347,10 +346,11 @@ int write_to_ram(t_lpc *lpc,u8 *buf,int addr,int len) {
                count+=3;
 
                /* checksum */
-               if(!(lcount%20)) {
+               if((!(lcount%20))|(bcnt==nlen)) {
                        /* send checksum */
-                       txrxbuf[0]=checksum;
-                       txrx(lpc,txrxbuf,1,TXRX_TYPE_CMD);
+                       snprintf(txrxbuf,BUFSIZE,"%d\r\n",checksum);
+                       slen=strlen(txrxbuf);
+                       txrx(lpc,txrxbuf,slen,TXRX_TYPE_CMD);
                        if(!strncmp(txrxbuf,"RESEND\r\n",8)) {
                                printf("ram write: resending ...\n");
                                bcnt-=count;
@@ -371,25 +371,74 @@ int write_to_ram(t_lpc *lpc,u8 *buf,int addr,int len) {
 
 int firmware_to_ram(t_lpc *lpc) {
 
-       char line[BUFSIZE];
        char buf[BUFSIZE];
+       u8 data[BUFSIZE];
+       u32 addr,len,type,val;
+       u8 cksum;
+       int ret,i;
 
-       /* read file */
+       /* read a line */
        while(ret) {
-               /* read line */
-               while(ret) {
-                       ret=read(lpc->fwfd,line,1);
-                       if(line[0]!=':') {
+               /* sync line */
+               ret=read(lpc->fwfd,buf,1);
+               switch(buf[0]) {
+                       case '\r':
+                               continue;
+                       case '\n':
+                               continue;
+                       case ':':
+                               /* start code */
+                               break;
+                       default:
                                printf("fw to ram: no ihex format\n");
                                return -1;
-                       }
-                       ret=read(lpc->fwfd,line+1,2);
-                       line[3]='\0';
-                       len=atoi(line+1);
-                       ret=read(lpc->fwfd,line+3,4);
-                       line[7]='\0';
-                       addr=strtol(line+3);
-
+               }
+               /* read len */
+               ret=read(lpc->fwfd,buf,2);
+               sscanf(buf,"%02x",&len);
+               if(len%4) {
+                       printf("fw to ram: len not a multiple of 4\n");
+                       return -1;
+               }
+               /* read addr */
+               ret=read(lpc->fwfd,buf,4);
+               sscanf(buf,"%04x",&addr);
+               /* read type */
+               ret=read(lpc->fwfd,buf,2);
+               sscanf(buf,"%02x",&type);
+               /* successfull return if type is end of file */
+               if(type==0x01)
+                       return 0;
+               /* read data */
+               ret=read(lpc->fwfd,buf,2*len);
+               if(ret!=(2*len)) {
+                       printf("fw to ram: data missing\n");
+                               return -1;
+               }
+               /* checksum */
+               cksum=0;
+               for(i=0;i<len;i++) {
+                       sscanf(buf+2*i,"%02x",&val);
+                       data[i]=val;
+                       cksum+=data[i];
+               }
+               ret=read(lpc->fwfd,buf,2);
+               sscanf(buf,"%02x",&val);
+               if(val+cksum!=0x100) {
+                       printf("fw to ram: wrong checksum\n");
+                       return -1;
+               }
+               /* act according to type */
+               switch(type) {
+                       case 0x03:
+                               /* get cs and ip */
+                               break;
+                       case 0x00:
+                               write_to_ram(lpc,data,addr,len);
+                               break;
+                       default:
+                               printf("fw to ram: unknown type %02x\n",type);
+                               return -1;
                }
        }