From: hackbard <hackbard@staubsauger.localdomain>
Date: Sat, 1 Sep 2007 22:22:45 +0000 (+0200)
Subject: cksm implemented (still basic flash access not working!)
X-Git-Url: https://hackdaworld.org/gitweb/?a=commitdiff_plain;h=eca427bb7d2ac0dbd337a3797fcea63991c51b38;p=my-code%2Farm.git

cksm implemented (still basic flash access not working!)
---

diff --git a/betty/fwflash.c b/betty/fwflash.c
index c4a2155..2704fd5 100644
--- a/betty/fwflash.c
+++ b/betty/fwflash.c
@@ -295,6 +295,8 @@ void receive_data_and_write_to_flash(u32 addr,u32 datalen) {
 	u8 bank;
 	u32 i;
 	u16 data;
+	u8 byte;
+	u8 cksm;
 
 	/* which bank to program */
 	if(addr<0x82000000)
@@ -306,9 +308,14 @@ void receive_data_and_write_to_flash(u32 addr,u32 datalen) {
 	unlock_bypass(bank);
 
 	/* receive and write data */
+	cksm=0;
 	for(i=0;i<datalen/2;i++) {
-		data=uart0_get_byte();
-		data=uart0_get_byte()<<8;
+		byte=uart0_get_byte();
+		data=byte;
+		cksm+=byte;
+		byte=uart0_get_byte();
+		cksm+=byte;
+		data|=byte<<8;
 		*((unsigned volatile short *)addr)=0xa0;
 		*((unsigned volatile short *)addr)=data;
 		addr+=2;
@@ -316,6 +323,9 @@ void receive_data_and_write_to_flash(u32 addr,u32 datalen) {
 
 	/* relock bypass */
 	unlock_bypass_reset(bank);
+
+	/* send an ack */
+	uart0_send_byte(cksm);
 }
 
 /*
diff --git a/betty/lpcload.c b/betty/lpcload.c
index 4f62a59..748a096 100644
--- a/betty/lpcload.c
+++ b/betty/lpcload.c
@@ -539,31 +539,30 @@ int send_cmd(int sfd,u32 addr,u32 len,u8 cmd) {
 		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 write_to_flash(t_lpc *lpc,u8 *buf,u32 addr,int len) {
 
 	int cnt,size,ret;
+	u8 cksm;
 
 	/* send cmd */
-	send_cmd(lpc->sfd,addr,len,CMD_WRITE);
+	send_cmd(lpc->sfd,addr+lpc->roff,len,CMD_WRITE);
 
 	/* transfer data */
+	cnt=0;
+	cksm=0;
 	while(len) {
 		size=2;
 		while(size) {
@@ -574,7 +573,21 @@ int write_to_flash(t_lpc *lpc,char *buf,u32 addr,int len) {
 			}
 			size-=ret;
 		}
+		cksm+=buf[cnt];
+		cksm+=buf[cnt+1];
 		cnt+=2;
+		len-=2;
+	}
+
+	/* check ack */
+	ret=read(lpc->sfd,buf,1);
+	if(ret<0) {
+		perror("write to flash: ack rx");
+		return ret;
+	}
+	if(buf[0]!=cksm) {
+		printf("FATAL: write to ram: wrong checksum!\n");
+		return -1;
 	}
 
 	return 0;
@@ -647,7 +660,7 @@ int firmware_to_mem(t_lpc *lpc,u8 memtype) {
 				if(memtype==RAM)
 					write_to_ram(lpc,buf,addr,len);
 				else
-					write_to_flash(lpc,buf,addr,len);
+					write_to_flash(lpc,(u8 *)buf,addr,len);
 				break;
 			case 0x04:
 				lpc->roff=((buf[0]<<24)|(buf[1]<<16));
@@ -841,7 +854,8 @@ int main(int argc,char **argv) {
 
 	/* write a firmware to the lpc flash */
 	if(lpc.info&FLASHFW) {
-		send_cmd(lpc.sfd,0,0,CMD_CHIP_ERASE);
+		printf("writing firmware to flash ...\n");
+		send_cmd(lpc.sfd,0,'0',CMD_CHIP_ERASE);
 		lpc.roff=BANK0_ADDR;
 		firmware_to_mem(&lpc,FLASH);
 	}