From: hackbard Date: Wed, 1 Aug 2007 12:53:18 +0000 (+0200) Subject: (ntested!) implementation of firmware_to_ram routine X-Git-Url: https://hackdaworld.org/gitweb/?a=commitdiff_plain;h=f5a2aa532e12c8f242bd7b767702ac65f1a7d48d;p=my-code%2Farm.git (ntested!) implementation of firmware_to_ram routine --- diff --git a/betty/lpcload.c b/betty/lpcload.c index 0bf3006..c2b1114 100644 --- a/betty/lpcload.c +++ b/betty/lpcload.c @@ -41,9 +41,11 @@ #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;ifwfd,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;ifwfd,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; } }