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);
177 ret=tcsetattr(lpc->sfd,TCSANOW,&term);
182 int open_firmware(t_lpc *lpc) {
184 /* open firmware file */
186 lpc->fwfd=open(lpc->fwfile,O_RDONLY);
193 if(lpc->info&FLASHFW) {
194 lpc->ffwfd=open(lpc->ffwfile,O_RDONLY);
204 int open_dumpfiles(t_lpc *lpc) {
208 if(lpc->info&BANK0) {
209 lpc->b0fd=open(lpc->bank0,O_WRONLY|O_CREAT);
211 perror("bank0 dump file open");
216 if(lpc->info&BANK2) {
217 lpc->b2fd=open(lpc->bank2,O_WRONLY|O_CREAT);
219 perror("bank2 dump file open");
225 lpc->blfd=open(lpc->bl,O_WRONLY|O_CREAT);
227 perror("bootloader dump file open");
235 int txrx(t_lpc *lpc,char *buf,int len,u8 type) {
242 if(lpc->info&VERBOSE)
246 ret=write(lpc->sfd,buf+cnt,len);
248 perror("txrx write");
251 if(lpc->info&VERBOSE)
254 ((buf[cnt+i]>0x19)&(buf[cnt+i]<0x7f))?
259 if(lpc->info&VERBOSE) {
262 printf("%02x ",buf[i]);
263 printf("| (%d)\n",cnt);
268 /* cut the echo if not of type auto baud */
270 if(type!=TXRX_TYPE_BAUD) {
272 ret=read(lpc->sfd,buf,cnt);
274 perror("txrx echo cut");
281 /* return if type is go */
283 if(type==TXRX_TYPE_GO)
286 /* return here if type is data */
288 if(type==TXRX_TYPE_DATA)
293 ret=read(lpc->sfd,buf,1);
295 perror("txrx read (first byte)");
313 printf("txrx read: bad return byte '%02x'\n",buf[0]);
320 ret=read(lpc->sfd,buf+1+cnt-i,i);
322 perror("txrx read (next bytes)");
327 if(lpc->info&VERBOSE) {
330 printf("%c",((buf[i]>0x19)&(buf[i]<0x7f))?
334 printf("%02x ",buf[i]);
335 printf("| (%d)\n",cnt+1);
339 /* check/strip return code if type is cmd */
341 if(type==TXRX_TYPE_CMD) {
342 ret=strlen(CMD_SUCCESS);
343 if(!strncmp(buf,CMD_SUCCESS,ret)) {
349 printf("txrx bad return code!\n");
357 int bl_init(t_lpc *lpc) {
362 /* auto baud sequence */
364 txrx(lpc,buf,1,TXRX_TYPE_BAUD);
365 if(strncmp(buf,"Synchronized\r\n",14)) {
366 printf("auto baud detection failed\n");
370 /* tell bl that we are synchronized (it's allready in buf) */
371 txrx(lpc,buf,14,TXRX_TYPE_SYNC);
372 if(strncmp(buf,"OK\r\n",4)) {
373 printf("sync failed\n");
377 /* tell bl the crystal frequency */
378 len=strlen(lpc->freq)+2;
379 strncpy(buf,lpc->freq,BUFSIZE);
382 txrx(lpc,buf,len,TXRX_TYPE_SYNC);
383 if(strncmp(buf,"OK\r\n",4)) {
384 printf("freq set failed\n");
391 int unlock_go(t_lpc *lpc) {
396 memcpy(buf,"U 23130\r\n",9);
397 ret=txrx(lpc,buf,9,TXRX_TYPE_CMD);
407 snprintf(buf,BUFSIZE,"G %d A\r\n",lpc->jaddr);
409 ret=txrx(lpc,buf,len,TXRX_TYPE_GO);
414 int uuencode(u8 *in,u8 *out,int len) {
417 out[1]=0x20+((in[0]>>2)&0x3f);
418 out[2]=0x20+(((in[0]<<4)|(in[1]>>4))&0x3f);
419 out[3]=0x20+(((in[1]<<2)|(in[2]>>6))&0x3f);
420 out[4]=0x20+(in[2]&0x3f);
425 int write_to_ram(t_lpc *lpc,char *buf,u32 addr,int len) {
429 char txrxbuf[BUFSIZE];
436 printf("ram write: not a multiple of 4\n");
440 /* make it a multiple of 3 (reason: uuencode) */
441 nlen=(!(len%3))?len:((len/3+1)*3);
443 printf("ram write: too much data\n");
446 for(i=len;i<nlen;i++) buf[i]=0;
451 /* prepare write command */
452 if(lpc->info&VERBOSE)
453 printf("writing 0x%02x bytes to 0x%08x\n",len,addr);
454 snprintf(txrxbuf,BUFSIZE,"W %d %d\r\n",addr,len);
455 slen=strlen(txrxbuf);
457 /* send command and check return code */
458 txrx(lpc,txrxbuf,slen,TXRX_TYPE_CMD);
467 /* uuencode / prepare data bytes */
468 uuencode((u8 *)(buf+bcnt),(u8 *)(txrxbuf),
469 (bcnt==nlen-3)?(len%3?len%3:3):3);
474 checksum+=((u8)buf[bcnt]+(u8)buf[bcnt+1]+(u8)buf[bcnt+2]);
476 /* send a data line */
477 txrx(lpc,txrxbuf,7,TXRX_TYPE_DATA);
479 /* increase counters */
485 if((!(lcount%20))|(bcnt==nlen)) {
487 memcpy(txrxbuf,"`\r\n",3);
488 //txrx(lpc,txrxbuf,3,TXRX_TYPE_DATA);
490 snprintf(txrxbuf,BUFSIZE,"%d\r\n",checksum);
491 slen=strlen(txrxbuf);
492 txrx(lpc,txrxbuf,slen,TXRX_TYPE_CKSM);
493 if(!strncmp(txrxbuf,"RESE",4)) {
494 read(lpc->sfd,txrxbuf+4,4);
495 printf("ram write: resending ...\n");
498 if(strncmp(txrxbuf,"OK\r\n",4)) {
499 printf("ram write: bad response\n");
502 /* reset checksum & counter */
512 int send_cmd(int sfd,u32 addr,u32 len,u8 cmd) {
529 printf("send cmd: cmd '%02x' not supported\n",cmd);
537 send[1+i]=(addr>>((as-1-i)*8))&0xff;
539 send[1+i+as]=(len>>((ls-1-i)*8))&0xff;
542 printf(" sending cmd: ");
544 ret=write(sfd,send+cnt,size);
546 perror("dump file: send cmd ");
549 for(i=cnt;i<cnt+ret;i++)
550 printf("%02x ",send[i]);
559 int write_to_flash(t_lpc *lpc,char *buf,u32 addr,int len) {
564 send_cmd(lpc->sfd,addr,len,CMD_WRITE);
570 ret=write(lpc->sfd,buf+cnt,2);
572 perror("transmit flash content");
583 int firmware_to_mem(t_lpc *lpc,u8 memtype) {
590 /* prepare for memory type */
593 else if(memtype==FLASH)
612 printf("fw to mem: no ihex format\n");
617 sscanf(buf,"%02x",&len);
620 sscanf(buf,"%04x",&addr);
623 sscanf(buf,"%02x",&type);
624 /* successfull return if type is end of file */
627 /* read data (and cksum) */
628 ret=read(fd,buf,2*(len+1));
629 if(ret!=(2*(len+1))) {
630 printf("fw to mem: data missing\n");
633 for(ret=0;ret<len;ret++) {
634 sscanf(buf+2*ret,"%02x",&temp);
637 /* act according to type */
640 // /* get cs and ip */
644 printf("fw to mem: invalid len\n");
648 write_to_ram(lpc,buf,addr,len);
650 write_to_flash(lpc,buf,addr,len);
653 lpc->roff=((buf[0]<<24)|(buf[1]<<16));
656 lpc->jaddr=((buf[0]<<24)|(buf[1]<<16));
657 lpc->jaddr|=((buf[2]<<8)|buf[3]);
660 printf("fw to mem: unknown type %02x\n",type);
668 int lpc_txbuf_flush(t_lpc *lpc) {
674 printf("flushing lpc tx buffer: ");
676 ret=read(lpc->sfd,buf,16);
678 printf("%02x ",buf[i]);
685 int dump_files(int sfd,int dfd,u32 addr,u32 len) {
692 printf("dumping content (addr=0x%08x, len=0x%08x) ...\n",addr,len);
695 send_cmd(sfd,addr,len,CMD_READ);
697 /* receive data and dump it to file */
700 printf(" receiving data ...\n");
702 ret=read(sfd,buf,16);
704 perror("dump file: read data");
710 ret=write(dfd,buf+cnt,size-cnt);
712 perror("dump file: write data");
723 int main(int argc,char **argv) {
733 memset(&lpc,0,sizeof(t_lpc));
734 strncpy(lpc.freq,CRYSTFREQ,7);
740 for(i=1;i<argc;i++) {
742 if(argv[i][0]!='-') {
749 strncpy(lpc.sdev,argv[++i],127);
752 strncpy(lpc.fwfile,argv[++i],127);
759 strncpy(lpc.freq,argv[++i],7);
762 if(argv[i][2]=='0') {
764 strncpy(lpc.bank0,argv[++i],127);
767 else if(argv[i][2]=='2') {
769 strncpy(lpc.bank2,argv[++i],127);
772 else if(argv[i][2]=='b') {
774 strncpy(lpc.bl,argv[++i],127);
782 strncpy(lpc.ffwfile,argv[++i],127);
792 /* open serial port */
793 if(open_serial_device(&lpc)<0)
796 /* boot loader init */
797 printf("boot loader init ...\n");
801 /* quit if there is no hex file to process */
802 if(!(lpc.info&FIRMWARE)) {
803 printf("no firmware -> aborting\n");
807 /* open firmware file */
808 if(open_firmware(&lpc)<0)
811 /* open dump files */
812 if(open_dumpfiles(&lpc)<0)
815 /* parse intel hex file and write to ram */
816 printf("write firmware to ram ...\n");
817 firmware_to_mem(&lpc,RAM);
820 printf("unlock go command ...\n");
827 /* flush the lpc2220 tx buf */
828 lpc_txbuf_flush(&lpc);
830 /* reconfigure the serial port */
831 if(reconfig_serial_device(&lpc)<0)
834 /* download flash/bootloader content */
836 dump_files(lpc.sfd,lpc.b0fd,BANK0_ADDR,BANK_SIZE);
838 dump_files(lpc.sfd,lpc.b2fd,BANK2_ADDR,BANK_SIZE);
840 dump_files(lpc.sfd,lpc.blfd,BL_ADDR,BL_SIZE);
842 /* write a firmware to the lpc flash */
843 if(lpc.info&FLASHFW) {
844 send_cmd(lpc.sfd,0,0,CMD_CHIP_ERASE);
846 firmware_to_mem(&lpc,FLASH);