]> hackdaworld.org Git - my-code/arm.git/commitdiff
lpcload, added support for writing data to flash (untested!)
authorhackbard <hackbard@staubsauger.localdomain>
Sat, 1 Sep 2007 20:03:17 +0000 (22:03 +0200)
committerhackbard <hackbard@staubsauger.localdomain>
Sat, 1 Sep 2007 20:03:17 +0000 (22:03 +0200)
betty/lpcload.c

index 1cbeb07eed461e100a67772abab31b7321bbd698..4f62a59253febac2fd3f716dce996f3b69eb2f16 100644 (file)
@@ -21,6 +21,7 @@
 #define BANK0                  (1<<2)
 #define BANK2                  (1<<3)
 #define BL                     (1<<4)
+#define FLASHFW                        (1<<5)
 
 #define BANK0_ADDR             0x80000000
 #define BANK2_ADDR             0x82000000
@@ -29,6 +30,8 @@
 #define BL_SIZE                        0x00002000
 
 #define CMD_READ               'R'             // stay compatible to fwflash!
+#define CMD_WRITE              'W'
+#define CMD_CHIP_ERASE         'E'
 
 #define TXRX_TYPE_SYNC         0x00
 #define TXRX_TYPE_CKSM         0x00
@@ -54,6 +57,9 @@
 #define INVALID_BAUD_RATE      "17\r\n"
 #define INVALID_STOP_BIT       "18\r\n"
 
+#define RAM                    0x01
+#define FLASH                  0x02
+
 #define CRYSTFREQ              "10000"
 #define RAMOFFSET              0x40000200
 
@@ -68,7 +74,7 @@ typedef struct s_lpc {
        char sdev[128];         /* seriel device */
        int fwfd;               /* fimrware fd */
        char fwfile[128];       /* firmware file */
-       u8 info;                /* info/mode */
+       u32 info;               /* info/mode */
        char freq[8];           /* frequency */
        char bank0[127];        /* flash dump bank0 */
        int b0fd;               /* dumpfile fd bank0 */
@@ -76,6 +82,8 @@ typedef struct s_lpc {
        int b2fd;               /* dumpfile fd bank0 */
        char bl[127];           /* flash dump bootloader */
        int blfd;               /* dumpfile fd bootloader */
+       char ffwfile[127];      /* file with firmware to be flashed */
+       int ffwfd;              /* fd of the above */
        u32 roff;               /* ram offset of uc */
        u32 jaddr;              /* addr for the jump */
 } t_lpc;
@@ -88,6 +96,7 @@ void usage(void) {
        printf("  -c  <crystal freq>\n");
        printf("  -Dx <filename>\n");
        printf("      x=0: bank0, x=2: bank2, x=b: bootloader\n");
+       printf("  -w <firmware which goes to flash>\n");
        printf("  -v\n");
 
 }
@@ -176,8 +185,18 @@ int open_firmware(t_lpc *lpc) {
 
        lpc->fwfd=open(lpc->fwfile,O_RDONLY);
 
-       if(lpc->fwfd<0)
+       if(lpc->fwfd<0) {
                perror("fw open");
+               return lpc->fwfd;
+       }
+
+       if(lpc->info&FLASHFW) {
+               lpc->ffwfd=open(lpc->ffwfile,O_RDONLY);
+               if(lpc->ffwfd<0) {
+                       perror("ffw open");
+                       return lpc->ffwfd;
+               }
+       }
 
        return lpc->fwfd;
 }
@@ -490,17 +509,97 @@ int write_to_ram(t_lpc *lpc,char *buf,u32 addr,int len) {
        return 0;
 }
 
-int firmware_to_ram(t_lpc *lpc) {
+int send_cmd(int sfd,u32 addr,u32 len,u8 cmd) {
+
+       int ret,i,cnt,size;
+       int as,ls;
+       u8 send[9];
+
+       switch(cmd) {
+               case CMD_READ:
+               case CMD_WRITE:
+                       as=4;
+                       ls=4;
+                       break;
+               case CMD_CHIP_ERASE:
+                       as=0;
+                       ls=1;
+                       break;
+               default:
+                       printf("send cmd: cmd '%02x' not supported\n",cmd);
+                       return -1;
+       }
+
+       size=1+as+ls;
+       send[0]=cmd;
+
+       for(i=0;i<as;i++)
+               send[1+i]=(addr>>((as-1-i)*8))&0xff;
+       for(i=0;i<ls;i++)
+               send[1+i+as]=(len>>((ls-1-i)*8))&0xff;
+
+       cnt=0;
+       printf("  sending cmd: ");
+       while(size) {
+               ret=write(sfd,send+cnt,size);
+               if(ret<0) {
+                       perror("dump file: send cmd ");
+                       return ret;
+               }
+               for(i=cnt;i<cnt+ret;i++)
+                       printf("%02x ",send[i]);
+               size-=ret;
+               cnt+=ret;
+       }
+       printf("\n");
+
+       return 0;
+}
+
+int write_to_flash(t_lpc *lpc,char *buf,u32 addr,int len) {
+
+       int cnt,size,ret;
+
+       /* send cmd */
+       send_cmd(lpc->sfd,addr,len,CMD_WRITE);
+
+       /* transfer data */
+       while(len) {
+               size=2;
+               while(size) {
+                       ret=write(lpc->sfd,buf+cnt,2);
+                       if(ret<0) {
+                               perror("transmit flash content");
+                               return ret;
+                       }
+                       size-=ret;
+               }
+               cnt+=2;
+       }
+
+       return 0;
+}
+
+int firmware_to_mem(t_lpc *lpc,u8 memtype) {
 
        char buf[BUFSIZE];
        u32 addr,len,type;
        int ret,temp;
+       int fd;
+
+       /* prepare for memory type */
+       if(memtype==RAM)
+               fd=lpc->fwfd;
+       else if(memtype==FLASH)
+               fd=lpc->ffwfd;
+       else
+               return -1;
 
        /* read a line */
        ret=1;
        while(ret) {
                /* sync line */
-               ret=read(lpc->fwfd,buf,1);
+               ret=read(fd,buf,1);
                switch(buf[0]) {
                        case '\r':
                                continue;
@@ -510,25 +609,25 @@ int firmware_to_ram(t_lpc *lpc) {
                                /* start code */
                                break;
                        default:
-                               printf("fw to ram: no ihex format\n");
+                               printf("fw to mem: no ihex format\n");
                                return -1;
                }
                /* read len */
-               ret=read(lpc->fwfd,buf,2);
+               ret=read(fd,buf,2);
                sscanf(buf,"%02x",&len);
                /* read addr */
-               ret=read(lpc->fwfd,buf,4);
+               ret=read(fd,buf,4);
                sscanf(buf,"%04x",&addr);
                /* read type */
-               ret=read(lpc->fwfd,buf,2);
+               ret=read(fd,buf,2);
                sscanf(buf,"%02x",&type);
                /* successfull return if type is end of file */
                if(type==0x01)
                        return 0;
                /* read data (and cksum) */
-               ret=read(lpc->fwfd,buf,2*(len+1));
+               ret=read(fd,buf,2*(len+1));
                if(ret!=(2*(len+1))) {
-                       printf("fw to ram: data missing\n");
+                       printf("fw to mem: data missing\n");
                                return -1;
                }
                for(ret=0;ret<len;ret++) {
@@ -542,10 +641,13 @@ int firmware_to_ram(t_lpc *lpc) {
                        //      break;
                        case 0x00:
                                if(len%4) {
-                                       printf("fw to ram: invalid len\n");
+                                       printf("fw to mem: invalid len\n");
                                        return -1;
                                }
-                               write_to_ram(lpc,buf,addr,len);
+                               if(memtype==RAM)
+                                       write_to_ram(lpc,buf,addr,len);
+                               else
+                                       write_to_flash(lpc,buf,addr,len);
                                break;
                        case 0x04:
                                lpc->roff=((buf[0]<<24)|(buf[1]<<16));
@@ -555,7 +657,7 @@ int firmware_to_ram(t_lpc *lpc) {
                                lpc->jaddr|=((buf[2]<<8)|buf[3]);
                                break;
                        default:
-                               printf("fw to ram: unknown type %02x\n",type);
+                               printf("fw to mem: unknown type %02x\n",type);
                                return -1;
                }
        }
@@ -585,36 +687,12 @@ int dump_files(int sfd,int dfd,u32 addr,u32 len) {
        int ret;
        int size;
        int cnt;
-       int i;
        u8 buf[16];
 
        printf("dumping content (addr=0x%08x, len=0x%08x) ...\n",addr,len);
 
-       /* send cmd */
-       size=1+4+4;
-       cnt=0;
-       buf[0]=CMD_READ;
-       buf[1]=(addr>>24)&0xff;
-       buf[2]=(addr>>16)&0xff;
-       buf[3]=(addr>>8)&0xff;
-       buf[4]=addr&0xff;
-       buf[5]=(len>>24)&0xff;
-       buf[6]=(len>>16)&0xff;
-       buf[7]=(len>>8)&0xff;
-       buf[8]=len&0xff;
-       printf("  sending cmd: ");
-       while(size) {
-               ret=write(sfd,buf+cnt,size);
-               for(i=cnt;i<cnt+ret;i++)
-                       printf("%02x ",buf[i]);
-               if(ret<0) {
-                       perror("dump file: send cmd ");
-                       return ret;
-               }
-               size-=ret;
-               cnt+=ret;
-       }
-       printf("\n");
+       /* send the cmd */
+       send_cmd(sfd,addr,len,CMD_READ);
 
        /* receive data and dump it to file */
        ret=1;
@@ -700,7 +778,10 @@ int main(int argc,char **argv) {
                                        usage();
                                        return -1;
                                }
-                               
+                       case 'w':
+                               strncpy(lpc.ffwfile,argv[++i],127);
+                               lpc.info|=FLASHFW;
+                               break;
                        default:
                                usage();
                                return -1;
@@ -733,7 +814,7 @@ int main(int argc,char **argv) {
 
        /* parse intel hex file and write to ram */
        printf("write firmware to ram ...\n");
-       firmware_to_ram(&lpc);
+       firmware_to_mem(&lpc,RAM);
 
        /* unlock go cmd */
        printf("unlock go command ...\n");
@@ -758,6 +839,13 @@ int main(int argc,char **argv) {
        if(lpc.info&BL)
                dump_files(lpc.sfd,lpc.blfd,BL_ADDR,BL_SIZE);
 
+       /* write a firmware to the lpc flash */
+       if(lpc.info&FLASHFW) {
+               send_cmd(lpc.sfd,0,0,CMD_CHIP_ERASE);
+               lpc.roff=BANK0_ADDR;
+               firmware_to_mem(&lpc,FLASH);
+       }
+
 end:
        if(lpc.sfd)
                close(lpc.sfd);
@@ -769,6 +857,8 @@ end:
                close(lpc.b2fd);
        if(lpc.blfd)
                close(lpc.blfd);
+       if(lpc.ffwfd)
+               close(lpc.ffwfd);
 
        return 0;
 }