#define BANK0 (1<<2)
#define BANK2 (1<<3)
#define BL (1<<4)
+#define FLASHFW (1<<5)
#define BANK0_ADDR 0x80000000
#define BANK2_ADDR 0x82000000
#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
#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
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 */
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;
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");
}
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;
}
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;
/* 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++) {
// 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));
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;
}
}
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;
usage();
return -1;
}
-
+ case 'w':
+ strncpy(lpc.ffwfile,argv[++i],127);
+ lpc.info|=FLASHFW;
+ break;
default:
usage();
return -1;
/* 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");
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);
close(lpc.b2fd);
if(lpc.blfd)
close(lpc.blfd);
+ if(lpc.ffwfd)
+ close(lpc.ffwfd);
return 0;
}