#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 */
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;
int bl_init(t_lpc *lpc) {
- u8 buf[BUFSIZE];
+ char buf[BUFSIZE];
int len;
/* auto baud sequence */
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);
int read_bcv(t_lpc *lpc) {
- u8 buf[BUFSIZE];
+ char buf[BUFSIZE];
char *ptr;
memcpy(buf,"K\r\n",3);
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);
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;
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);
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;
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;
}
}