2 * lpcload.c - load firmware into ram of lpc2220 via uart0
4 * author: hackbard@hackdaworld.org, rolf.anders@physik.uni-augsburg.de
7 * usage: sudo ./lpcload -d /dev/ttyS0 -f firmware.hex -D0 fw0.bin [-v]
14 #include <sys/types.h>
19 #define VERBOSE (1<<0)
20 #define FIRMWARE (1<<1)
24 #define FLASHFW (1<<5)
26 #define BANK0_ADDR 0x80000000
27 #define BANK2_ADDR 0x82000000
28 #define BANK_SIZE 0x00100000
29 #define BL_ADDR 0x7fffe000
30 #define BL_SIZE 0x00002000
32 #define CMD_READ 'R' // stay compatible to fwflash!
34 #define CMD_CHIP_ERASE 'E'
36 #define TXRX_TYPE_SYNC 0x00
37 #define TXRX_TYPE_CKSM 0x00
38 #define TXRX_TYPE_BAUD 0x01
39 #define TXRX_TYPE_CMD 0x02
40 #define TXRX_TYPE_DATA 0x03
41 #define TXRX_TYPE_GO 0x04
43 #define CMD_SUCCESS "0\r\n"
44 #define INVALID_COMMAND "1\r\n"
45 #define SRC_ADDR_ERROR "2\r\n"
46 #define DST_ADDR_ERROR "3\r\n"
47 #define SRC_ADDR_NOT_MAPPED "4\r\n"
48 #define DST_ADDR_NOT_MAPPED "5\r\n"
49 #define COUNT_ERROR "6\r\n"
50 #define COMPARE_ERROR "10\r\n"
52 #define PARAM_ERROR "12\r\n"
53 #define ADDR_ERROR "13\r\n"
54 #define ADDR_NOT_MAPPED "14\r\n"
55 #define CMD_LOCKED "15\r\n"
56 #define INVALID_CODE "16\r\n"
57 #define INVALID_BAUD_RATE "17\r\n"
58 #define INVALID_STOP_BIT "18\r\n"
63 #define CRYSTFREQ "10000"
64 #define RAMOFFSET 0x40000200
68 typedef unsigned char u8;
69 typedef unsigned short u16;
70 typedef unsigned int u32;
72 typedef struct s_lpc {
73 int sfd; /* serial fd */
74 char sdev[128]; /* seriel device */
75 int fwfd; /* fimrware fd */
76 char fwfile[128]; /* firmware file */
77 u32 info; /* info/mode */
78 char freq[8]; /* frequency */
79 char bank0[127]; /* flash dump bank0 */
80 int b0fd; /* dumpfile fd bank0 */
81 char bank2[127]; /* flash dump bank2 */
82 int b2fd; /* dumpfile fd bank0 */
83 char bl[127]; /* flash dump bootloader */
84 int blfd; /* dumpfile fd bootloader */
85 char ffwfile[127]; /* file with firmware to be flashed */
86 int ffwfd; /* fd of the above */
87 u32 roff; /* ram offset of uc */
88 u32 jaddr; /* addr for the jump */
93 printf("possible argv:\n");
94 printf(" -d <serial device>\n");
95 printf(" -f <firmware>\n");
96 printf(" -c <crystal freq>\n");
97 printf(" -Dx <filename>\n");
98 printf(" x=0: bank0, x=2: bank2, x=b: bootloader\n");
99 printf(" -w <firmware which goes to flash>\n");
104 int open_serial_device(t_lpc *lpc) {
108 //memset(&term,0,sizeof(struct termios));
110 /* open serial device */
112 lpc->sfd=open(lpc->sdev,O_RDWR);
118 /* configure the serial device */
120 tcgetattr(lpc->sfd,&term);
122 // input/output baudrate
124 cfsetispeed(&term,B38400);
125 cfsetospeed(&term,B38400);
127 // control options -> 8n1
129 term.c_cflag&=~PARENB; // no parity
130 term.c_cflag&=~CSTOPB; // only 1 stop bit
131 term.c_cflag&=~CSIZE; // no bit mask for data bits
132 term.c_cflag|=CS8; // 8 data bits
134 // line options -> raw input
136 term.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);
138 // input options -> enable flow control
140 term.c_iflag&=~(INLCR|ICRNL|IXANY);
141 term.c_iflag|=(IXON|IXOFF);
147 // more control options -> timeout / flow control
150 term.c_cc[VTIME]=20; // 2 seconds timeout
151 //term.c_cc[VSTART]=0x11;
152 //term.c_cc[VSTOP]=0x13;
154 tcsetattr(lpc->sfd,TCSANOW,&term);
159 int reconfig_serial_device(t_lpc *lpc) {
164 /* reconfigure the serial device for our lousy loader tool */
166 tcgetattr(lpc->sfd,&term);
168 // disable flow control
170 term.c_iflag&=~(IXON|IXOFF|IXANY|INLCR|ICRNL);
174 cfsetispeed(&term,B115200);
175 cfsetospeed(&term,B115200);
180 term.c_cc[VTIME]=100; // 10 seconds timeout
182 ret=tcsetattr(lpc->sfd,TCSANOW,&term);
187 int open_firmware(t_lpc *lpc) {
189 /* open firmware file */
191 lpc->fwfd=open(lpc->fwfile,O_RDONLY);
198 if(lpc->info&FLASHFW) {
199 lpc->ffwfd=open(lpc->ffwfile,O_RDONLY);
209 int open_dumpfiles(t_lpc *lpc) {
213 if(lpc->info&BANK0) {
214 lpc->b0fd=open(lpc->bank0,O_WRONLY|O_CREAT);
216 perror("bank0 dump file open");
221 if(lpc->info&BANK2) {
222 lpc->b2fd=open(lpc->bank2,O_WRONLY|O_CREAT);
224 perror("bank2 dump file open");
230 lpc->blfd=open(lpc->bl,O_WRONLY|O_CREAT);
232 perror("bootloader dump file open");
240 int txrx(t_lpc *lpc,char *buf,int len,u8 type) {
247 if(lpc->info&VERBOSE)
251 ret=write(lpc->sfd,buf+cnt,len);
253 perror("txrx write");
256 if(lpc->info&VERBOSE)
259 ((buf[cnt+i]>0x19)&(buf[cnt+i]<0x7f))?
264 if(lpc->info&VERBOSE) {
267 printf("%02x ",buf[i]);
268 printf("| (%d)\n",cnt);
273 /* cut the echo if not of type auto baud */
275 if(type!=TXRX_TYPE_BAUD) {
277 ret=read(lpc->sfd,buf,cnt);
279 perror("txrx echo cut");
286 /* return if type is go */
288 if(type==TXRX_TYPE_GO)
291 /* return here if type is data */
293 if(type==TXRX_TYPE_DATA)
298 ret=read(lpc->sfd,buf,1);
300 perror("txrx read (first byte)");
318 printf("txrx read: bad return byte '%02x'\n",buf[0]);
325 ret=read(lpc->sfd,buf+1+cnt-i,i);
327 perror("txrx read (next bytes)");
332 if(lpc->info&VERBOSE) {
335 printf("%c",((buf[i]>0x19)&(buf[i]<0x7f))?
339 printf("%02x ",buf[i]);
340 printf("| (%d)\n",cnt+1);
344 /* check/strip return code if type is cmd */
346 if(type==TXRX_TYPE_CMD) {
347 ret=strlen(CMD_SUCCESS);
348 if(!strncmp(buf,CMD_SUCCESS,ret)) {
354 printf("txrx bad return code!\n");
362 int bl_init(t_lpc *lpc) {
367 /* auto baud sequence */
369 txrx(lpc,buf,1,TXRX_TYPE_BAUD);
370 if(strncmp(buf,"Synchronized\r\n",14)) {
371 printf("auto baud detection failed\n");
375 /* tell bl that we are synchronized (it's allready in buf) */
376 txrx(lpc,buf,14,TXRX_TYPE_SYNC);
377 if(strncmp(buf,"OK\r\n",4)) {
378 printf("sync failed\n");
382 /* tell bl the crystal frequency */
383 len=strlen(lpc->freq)+2;
384 strncpy(buf,lpc->freq,BUFSIZE);
387 txrx(lpc,buf,len,TXRX_TYPE_SYNC);
388 if(strncmp(buf,"OK\r\n",4)) {
389 printf("freq set failed\n");
396 int unlock_go(t_lpc *lpc) {
401 memcpy(buf,"U 23130\r\n",9);
402 ret=txrx(lpc,buf,9,TXRX_TYPE_CMD);
412 snprintf(buf,BUFSIZE,"G %d A\r\n",lpc->jaddr);
414 ret=txrx(lpc,buf,len,TXRX_TYPE_GO);
419 int uuencode(u8 *in,u8 *out,int len) {
422 out[1]=0x20+((in[0]>>2)&0x3f);
423 out[2]=0x20+(((in[0]<<4)|(in[1]>>4))&0x3f);
424 out[3]=0x20+(((in[1]<<2)|(in[2]>>6))&0x3f);
425 out[4]=0x20+(in[2]&0x3f);
430 int write_to_ram(t_lpc *lpc,char *buf,u32 addr,int len) {
434 char txrxbuf[BUFSIZE];
441 printf("ram write: not a multiple of 4\n");
445 /* make it a multiple of 3 (reason: uuencode) */
446 nlen=(!(len%3))?len:((len/3+1)*3);
448 printf("ram write: too much data\n");
451 for(i=len;i<nlen;i++) buf[i]=0;
456 /* prepare write command */
457 if(lpc->info&VERBOSE)
458 printf("writing 0x%02x bytes to 0x%08x\n",len,addr);
459 snprintf(txrxbuf,BUFSIZE,"W %d %d\r\n",addr,len);
460 slen=strlen(txrxbuf);
462 /* send command and check return code */
463 txrx(lpc,txrxbuf,slen,TXRX_TYPE_CMD);
472 /* uuencode / prepare data bytes */
473 uuencode((u8 *)(buf+bcnt),(u8 *)(txrxbuf),
474 (bcnt==nlen-3)?(len%3?len%3:3):3);
479 checksum+=((u8)buf[bcnt]+(u8)buf[bcnt+1]+(u8)buf[bcnt+2]);
481 /* send a data line */
482 txrx(lpc,txrxbuf,7,TXRX_TYPE_DATA);
484 /* increase counters */
490 if((!(lcount%20))|(bcnt==nlen)) {
492 memcpy(txrxbuf,"`\r\n",3);
493 //txrx(lpc,txrxbuf,3,TXRX_TYPE_DATA);
495 snprintf(txrxbuf,BUFSIZE,"%d\r\n",checksum);
496 slen=strlen(txrxbuf);
497 txrx(lpc,txrxbuf,slen,TXRX_TYPE_CKSM);
498 if(!strncmp(txrxbuf,"RESE",4)) {
499 read(lpc->sfd,txrxbuf+4,4);
500 printf("ram write: resending ...\n");
503 if(strncmp(txrxbuf,"OK\r\n",4)) {
504 printf("ram write: bad response\n");
507 /* reset checksum & counter */
517 int send_cmd(int sfd,u32 addr,u32 len,u8 cmd) {
534 printf("send cmd: cmd '%02x' not supported\n",cmd);
542 send[1+i]=(addr>>((as-1-i)*8))&0xff;
544 send[1+i+as]=(len>>((ls-1-i)*8))&0xff;
548 ret=write(sfd,send+cnt,size);
550 perror("dump file: send cmd ");
560 int write_to_flash(t_lpc *lpc,u8 *buf,u32 addr,int len) {
571 send_cmd(lpc->sfd,addr,len,CMD_WRITE);
579 ret=write(lpc->sfd,buf+cnt+i,1);
581 perror("transmit flash content (w)");
588 ret=read(lpc->sfd,&check,1);
590 perror("transmit flash content (r)");
596 if(buf[cnt+i]!=check)
597 printf("FATAL: write to flash (transfer)\n");
607 ret=read(lpc->sfd,&cksmr,1);
609 perror("write to flash: read cksm");
615 printf("FATAL: wrong checksum or failure in flash write!\n");
617 printf(" -> most probably due to flash write!\n");
619 printf(" -> most probably failure in transfer!\n");
620 printf(" addr:0x%08x l:%02x r:%02x\n",addr,cksml,cksmr);
626 int firmware_to_mem(t_lpc *lpc,u8 memtype) {
633 /* prepare for memory type */
636 else if(memtype==FLASH)
655 printf("fw to mem: no ihex format\n");
660 sscanf(buf,"%02x",&len);
663 sscanf(buf,"%04x",&addr);
666 sscanf(buf,"%02x",&type);
667 /* successfull return if type is end of file */
670 /* read data (and cksum) */
671 ret=read(fd,buf,2*(len+1));
672 if(ret!=(2*(len+1))) {
673 printf("fw to mem: data missing\n");
676 for(ret=0;ret<len;ret++) {
677 sscanf(buf+2*ret,"%02x",&temp);
680 /* act according to type */
683 // /* get cs and ip */
687 printf("fw to mem: invalid len\n");
691 write_to_ram(lpc,buf,addr,len);
693 write_to_flash(lpc,(u8 *)buf,addr,len);
696 lpc->roff=((buf[0]<<24)|(buf[1]<<16));
699 lpc->jaddr=((buf[0]<<24)|(buf[1]<<16));
700 lpc->jaddr|=((buf[2]<<8)|buf[3]);
703 printf("fw to mem: unknown type %02x\n",type);
711 int lpc_txbuf_flush(t_lpc *lpc) {
717 printf("flushing lpc tx buffer: ");
719 ret=read(lpc->sfd,buf,16);
721 printf("%02x ",buf[i]);
728 int dump_files(int sfd,int dfd,u32 addr,u32 len) {
735 printf("dumping content (addr=0x%08x, len=0x%08x) ...\n",addr,len);
738 send_cmd(sfd,addr,len,CMD_READ);
740 /* receive data and dump it to file */
743 printf(" receiving data ...\n");
745 ret=read(sfd,buf,16);
747 perror("dump file: read data");
753 ret=write(dfd,buf+cnt,size-cnt);
755 perror("dump file: write data");
766 int main(int argc,char **argv) {
776 memset(&lpc,0,sizeof(t_lpc));
777 strncpy(lpc.freq,CRYSTFREQ,7);
783 for(i=1;i<argc;i++) {
785 if(argv[i][0]!='-') {
792 strncpy(lpc.sdev,argv[++i],127);
795 strncpy(lpc.fwfile,argv[++i],127);
802 strncpy(lpc.freq,argv[++i],7);
805 if(argv[i][2]=='0') {
807 strncpy(lpc.bank0,argv[++i],127);
810 else if(argv[i][2]=='2') {
812 strncpy(lpc.bank2,argv[++i],127);
815 else if(argv[i][2]=='b') {
817 strncpy(lpc.bl,argv[++i],127);
825 strncpy(lpc.ffwfile,argv[++i],127);
835 /* open serial port */
836 if(open_serial_device(&lpc)<0)
839 /* boot loader init */
840 printf("boot loader init ...\n");
844 /* quit if there is no hex file to process */
845 if(!(lpc.info&FIRMWARE)) {
846 printf("no firmware -> aborting\n");
850 /* open firmware file */
851 if(open_firmware(&lpc)<0)
854 /* open dump files */
855 if(open_dumpfiles(&lpc)<0)
858 /* parse intel hex file and write to ram */
859 printf("write firmware to ram ...\n");
860 firmware_to_mem(&lpc,RAM);
863 printf("unlock go command ...\n");
870 /* flush the lpc2220 tx buf */
871 lpc_txbuf_flush(&lpc);
873 /* reconfigure the serial port */
874 if(reconfig_serial_device(&lpc)<0)
877 /* download flash/bootloader content */
879 dump_files(lpc.sfd,lpc.b0fd,BANK0_ADDR,BANK_SIZE);
881 dump_files(lpc.sfd,lpc.b2fd,BANK2_ADDR,BANK_SIZE);
883 dump_files(lpc.sfd,lpc.blfd,BL_ADDR,BL_SIZE);
885 /* write a firmware to the lpc flash */
886 if(lpc.info&FLASHFW) {
887 printf("writing firmware to flash ...\n");
888 send_cmd(lpc.sfd,0,'0',CMD_CHIP_ERASE);
890 firmware_to_mem(&lpc,FLASH);