fwflash taking over fwbc, lpcload taking over fwdump
[my-code/arm.git] / betty / lpcload.c
1 /*
2  * lpcload.c - load firmware into ram of lpc2220 via uart0
3  *
4  * author: hackbard@hackdaworld.org, rolf.anders@physik.uni-augsburg.de
5  *
6  * build: make
7  * usage: sudo ./lpcload -d /dev/ttyS0 -f firmware.hex [-v]
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <termios.h>
18
19 #define VERBOSE                 (1<<0)
20 #define FIRMWARE                (1<<1)
21 #define BANK0                   (1<<2)
22 #define BANK2                   (1<<3)
23 #define BL                      (1<<4)
24
25 #define BANK0_ADDR              0x80000000
26 #define BANK2_ADDR              0x82000000
27 #define BANK_SIZE               0x00100000
28 #define BL_ADDR                 0x7fffe000
29 #define BL_SIZE                 0x00000800
30
31 #define CMD_READ                'R'             // stay compatible to fwflash!
32
33 #define TXRX_TYPE_SYNC          0x00
34 #define TXRX_TYPE_CKSM          0x00
35 #define TXRX_TYPE_BAUD          0x01
36 #define TXRX_TYPE_CMD           0x02
37 #define TXRX_TYPE_DATA          0x03
38 #define TXRX_TYPE_GO            0x04
39
40 #define CMD_SUCCESS             "0\r\n"
41 #define INVALID_COMMAND         "1\r\n"
42 #define SRC_ADDR_ERROR          "2\r\n"
43 #define DST_ADDR_ERROR          "3\r\n"
44 #define SRC_ADDR_NOT_MAPPED     "4\r\n"
45 #define DST_ADDR_NOT_MAPPED     "5\r\n"
46 #define COUNT_ERROR             "6\r\n"
47 #define COMPARE_ERROR           "10\r\n"
48 #define BUSY                    "11\r\n"
49 #define PARAM_ERROR             "12\r\n"
50 #define ADDR_ERROR              "13\r\n"
51 #define ADDR_NOT_MAPPED         "14\r\n"
52 #define CMD_LOCKED              "15\r\n"
53 #define INVALID_CODE            "16\r\n"
54 #define INVALID_BAUD_RATE       "17\r\n"
55 #define INVALID_STOP_BIT        "18\r\n"
56
57 #define CRYSTFREQ               "10000"
58 #define RAMOFFSET               0x40000200
59
60 #define BUFSIZE                 128
61
62 typedef unsigned char u8;
63 typedef unsigned short u16;
64 typedef unsigned int u32;
65
66 typedef struct s_lpc {
67         int sfd;                /* serial fd */
68         char sdev[128];         /* seriel device */
69         int fwfd;               /* fimrware fd */
70         char fwfile[128];       /* firmware file */
71         u8 info;                /* info/mode */
72         char freq[8];           /* frequency */
73         char bank0[127];        /* flash dump bank0 */
74         int b0fd;               /* dumpfile fd bank0 */
75         char bank2[127];        /* flash dump bank2 */
76         int b2fd;               /* dumpfile fd bank0 */
77         char bl[127];           /* flash dump bootloader */
78         int blfd;               /* dumpfile fd bootloader */
79         u32 roff;               /* ram offset of uc */
80         u32 jaddr;              /* addr for the jump */
81 } t_lpc;
82
83 void usage(void) {
84
85         printf("possible argv:\n");
86         printf("  -d  <serial device>\n");
87         printf("  -f  <firmware>\n");
88         printf("  -c  <crystal freq>\n");
89         printf("  -Dx <filename>\n");
90         printf("      x=0: bank0, x=2: bank2, x=b: bootloader\n");
91         printf("  -v\n");
92
93 }
94
95 int open_serial_device(t_lpc *lpc) {
96
97         struct termios term;
98
99         //memset(&term,0,sizeof(struct termios));
100
101         /* open serial device */
102
103         lpc->sfd=open(lpc->sdev,O_RDWR);
104         if(lpc->sfd<0) {
105                 perror("tts open");
106                 return lpc->sfd;
107         }
108
109         /* configure the serial device */
110
111         tcgetattr(lpc->sfd,&term);
112
113         // input/output baudrate
114
115         cfsetispeed(&term,B38400);
116         cfsetospeed(&term,B38400);
117
118         // control options -> 8n1
119
120         term.c_cflag&=~PARENB;  // no parity
121         term.c_cflag&=~CSTOPB;  // only 1 stop bit
122         term.c_cflag&=~CSIZE;   // no bit mask for data bits
123         term.c_cflag|=CS8;      // 8 data bits
124
125         // line options -> raw input
126         
127         term.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);
128
129         // input options -> enable flow control
130         
131         //term.c_iflag&=~(IXON|IXOFF|IXANY|INLCR|ICRNL);
132         term.c_iflag&=~(INLCR|ICRNL|IXANY);
133         term.c_iflag|=(IXON|IXOFF);
134         
135         // output options
136
137         term.c_oflag=0;
138
139         // more control options -> timeout / flow control
140         
141         term.c_cc[VMIN]=0;
142         term.c_cc[VTIME]=10;    // 1 second timeout
143         term.c_cc[VSTART]=0x11;
144         term.c_cc[VSTOP]=0x13;
145
146         tcsetattr(lpc->sfd,TCSANOW,&term);
147
148         return lpc->sfd;
149 }
150
151 int open_firmware(t_lpc *lpc) {
152
153         /* open firmware file */
154
155         lpc->fwfd=open(lpc->fwfile,O_RDONLY);
156
157         if(lpc->fwfd<0)
158                 perror("fw open");
159
160         return lpc->fwfd;
161 }
162
163 int open_dumpfiles(t_lpc *lpc) {
164
165         /* open dumpfiles */
166
167         if(lpc->info&BANK0) {
168                 lpc->b0fd=open(lpc->bank0,O_WRONLY|O_CREAT);
169                 if(lpc->b0fd<0) {
170                         perror("bank0 dump file open");
171                         return lpc->b0fd;
172                 }
173         }
174
175         if(lpc->info&BANK2) {
176                 lpc->b2fd=open(lpc->bank2,O_WRONLY|O_CREAT);
177                 if(lpc->b2fd<0) {
178                         perror("bank2 dump file open");
179                         return lpc->b2fd;
180                 }
181         }
182
183         if(lpc->info&BL) {
184                 lpc->blfd=open(lpc->bl,O_WRONLY|O_CREAT);
185                 if(lpc->blfd<0) {
186                         perror("bootloader dump file open");
187                         return lpc->blfd;
188                 }
189         }
190
191         return 0;
192
193 }
194 int txrx(t_lpc *lpc,char *buf,int len,u8 type) {
195
196         int ret,cnt;
197         int i;
198
199         /* write */
200
201         if(lpc->info&VERBOSE)
202                 printf("  >> ");
203         cnt=0;
204         while(len) {
205                 ret=write(lpc->sfd,buf+cnt,len);
206                 if(ret<0) {
207                         perror("txrx write");
208                         return ret;
209                 }
210                 if(lpc->info&VERBOSE)
211                         for(i=0;i<ret;i++)
212                                 printf("%c",
213                                        ((buf[cnt+i]>0x19)&(buf[cnt+i]<0x7f))?
214                                        buf[cnt+i]:'.');
215                 len-=ret;
216                 cnt+=ret;
217         }
218         if(lpc->info&VERBOSE) {
219                 printf(" | ");
220                 for(i=0;i<cnt;i++)
221                         printf("%02x ",buf[i]);
222                 printf("| (%d)\n",cnt);
223         }
224
225
226
227         /* cut the echo if not of type auto baud */
228
229         if(type!=TXRX_TYPE_BAUD) {
230                 while(cnt) {
231                         ret=read(lpc->sfd,buf,cnt);
232                         if(ret<0) {
233                                 perror("txrx echo cut");
234                                 return ret;
235                         }
236                         cnt-=ret;
237                 }
238         }
239
240         /* return if type is go */
241
242         if(type==TXRX_TYPE_GO)
243                 return cnt;
244
245         /* return here if type is data */
246
247         if(type==TXRX_TYPE_DATA)
248                 return cnt;
249
250         /* read */
251
252         ret=read(lpc->sfd,buf,1);
253         if(ret<0) {
254                 perror("txrx read (first byte)");
255                 return ret;
256         }
257                 
258         switch(buf[0]) {
259                 case 'S':
260                         cnt=13;
261                         break;
262                 case 'O':
263                         cnt=3;
264                         break;
265                 case 'R':
266                         cnt=7;
267                         break;
268                 case '0':
269                         cnt=2;
270                         break;
271                 default:
272                         printf("txrx read: bad return byte '%02x'\n",buf[0]);
273                         break;
274         }
275
276         ret=1;
277         i=cnt;
278         while(i) {
279                 ret=read(lpc->sfd,buf+1+cnt-i,i);
280                 if(ret<0) {
281                         perror("txrx read (next bytes)");
282                         return ret;
283                 }
284                 i-=ret;
285         }
286         if(lpc->info&VERBOSE) {
287                 printf("  << ");
288                 for(i=0;i<cnt+1;i++)
289                         printf("%c",((buf[i]>0x19)&(buf[i]<0x7f))?
290                                     buf[i]:'.');
291                 printf(" | ");
292                 for(i=0;i<cnt+1;i++)
293                         printf("%02x ",buf[i]);
294                 printf("| (%d)\n",cnt+1);
295         }
296         buf[cnt+1]='\0';
297
298         /* check/strip return code if type is cmd */
299
300         if(type==TXRX_TYPE_CMD) {
301                 ret=strlen(CMD_SUCCESS);
302                 if(!strncmp(buf,CMD_SUCCESS,ret)) {
303                         for(i=ret;i<cnt;i++)
304                                 buf[i-ret]=buf[i];
305                         buf[cnt]='\0';
306                 }
307                 else {
308                         printf("txrx bad return code!\n");
309                         return -1;
310                 }
311         }
312
313         return cnt;
314 }
315
316 int bl_init(t_lpc *lpc) {
317
318         char buf[BUFSIZE];
319         int len;
320
321         /* auto baud sequence */
322         buf[0]='?';
323         txrx(lpc,buf,1,TXRX_TYPE_BAUD);
324         if(strncmp(buf,"Synchronized\r\n",14)) {
325                 printf("auto baud detection failed\n");
326                 return -1;
327         }
328
329         /* tell bl that we are synchronized (it's allready in buf) */
330         txrx(lpc,buf,14,TXRX_TYPE_SYNC);
331         if(strncmp(buf,"OK\r\n",4)) {
332                 printf("sync failed\n");
333                 return -1;
334         }
335
336         /* tell bl the crystal frequency */
337         len=strlen(lpc->freq)+2;
338         strncpy(buf,lpc->freq,BUFSIZE);
339         buf[len-2]='\r';
340         buf[len-1]='\n';
341         txrx(lpc,buf,len,TXRX_TYPE_SYNC);
342         if(strncmp(buf,"OK\r\n",4)) {
343                 printf("freq set failed\n");
344                 return -1;
345         }
346
347         return 0;
348 }
349
350 int unlock_go(t_lpc *lpc) {
351
352         char buf[BUFSIZE];
353         int ret;
354
355         memcpy(buf,"U 23130\r\n",9);
356         ret=txrx(lpc,buf,9,TXRX_TYPE_CMD);
357
358         return ret;
359 }
360
361 int go(t_lpc *lpc) {
362
363         char buf[BUFSIZE];
364         int ret,len;
365
366         snprintf(buf,BUFSIZE,"G %d A\r\n",lpc->jaddr);
367         len=strlen(buf);
368         ret=txrx(lpc,buf,len,TXRX_TYPE_GO);
369
370         return ret;
371 }
372
373 int uuencode(u8 *in,u8 *out,int len) {
374
375         out[0]=0x20+len;
376         out[1]=0x20+((in[0]>>2)&0x3f);
377         out[2]=0x20+(((in[0]<<4)|(in[1]>>4))&0x3f);
378         out[3]=0x20+(((in[1]<<2)|(in[2]>>6))&0x3f);
379         out[4]=0x20+(in[2]&0x3f);
380
381         return 0;
382 }
383
384 int write_to_ram(t_lpc *lpc,char *buf,u32 addr,int len) {
385
386         int lcount;
387         u32 checksum;
388         char txrxbuf[BUFSIZE];
389         int count,bcnt;
390         int nlen,slen;
391         int i;
392
393         /* check length */
394         if(len%4) {
395                 printf("ram write: not a multiple of 4\n");
396                 return -1;
397         }
398
399         /* make it a multiple of 3 (reason: uuencode) */
400         nlen=(!(len%3))?len:((len/3+1)*3);
401         if(nlen>BUFSIZE) {
402                 printf("ram write: too much data\n");
403                 return -1;
404         }
405         for(i=len;i<nlen;i++) buf[i]=0;
406
407         /* prepare addr */
408         addr+=lpc->roff;
409
410         /* prepare write command */
411         if(lpc->info&VERBOSE)
412                 printf("writing 0x%02x bytes to 0x%08x\n",len,addr);
413         snprintf(txrxbuf,BUFSIZE,"W %d %d\r\n",addr,len);
414         slen=strlen(txrxbuf);
415
416         /* send command and check return code */
417         txrx(lpc,txrxbuf,slen,TXRX_TYPE_CMD);
418
419         /* send data */
420         lcount=0;
421         bcnt=0;
422         count=0;
423         checksum=0;
424         while(bcnt<nlen) {
425
426                 /* uuencode / prepare data bytes */
427                 uuencode((u8 *)(buf+bcnt),(u8 *)(txrxbuf),
428                          (bcnt==nlen-3)?(len%3?len%3:3):3);
429                 txrxbuf[5]='\r';
430                 txrxbuf[6]='\n';
431
432                 /* checksum */
433                 checksum+=((u8)buf[bcnt]+(u8)buf[bcnt+1]+(u8)buf[bcnt+2]);
434
435                 /* send a data line */
436                 txrx(lpc,txrxbuf,7,TXRX_TYPE_DATA);
437
438                 /* increase counters */
439                 lcount+=1;
440                 bcnt+=3;
441                 count+=3;
442
443                 /* checksum */
444                 if((!(lcount%20))|(bcnt==nlen)) {
445                         /* send backtick */
446                         memcpy(txrxbuf,"`\r\n",3);
447                         //txrx(lpc,txrxbuf,3,TXRX_TYPE_DATA);
448                         /* send checksum */
449                         snprintf(txrxbuf,BUFSIZE,"%d\r\n",checksum);
450                         slen=strlen(txrxbuf);
451                         txrx(lpc,txrxbuf,slen,TXRX_TYPE_CKSM);
452                         if(!strncmp(txrxbuf,"RESE",4)) {
453                                 read(lpc->sfd,txrxbuf+4,4);
454                                 printf("ram write: resending ...\n");
455                                 bcnt-=count;
456                         }
457                         if(strncmp(txrxbuf,"OK\r\n",4)) {
458                                 printf("ram write: bad response\n");
459                                 return -1;
460                         }
461                         /* reset checksum & counter */
462                         checksum=0;
463                         count=0;
464                 }
465
466         }
467
468         return 0;
469 }
470
471 int firmware_to_ram(t_lpc *lpc) {
472
473         char buf[BUFSIZE];
474         u32 addr,len,type;
475         int ret,temp;
476
477         /* read a line */
478         ret=1;
479         while(ret) {
480                 /* sync line */
481                 ret=read(lpc->fwfd,buf,1);
482                 switch(buf[0]) {
483                         case '\r':
484                                 continue;
485                         case '\n':
486                                 continue;
487                         case ':':
488                                 /* start code */
489                                 break;
490                         default:
491                                 printf("fw to ram: no ihex format\n");
492                                 return -1;
493                 }
494                 /* read len */
495                 ret=read(lpc->fwfd,buf,2);
496                 sscanf(buf,"%02x",&len);
497                 /* read addr */
498                 ret=read(lpc->fwfd,buf,4);
499                 sscanf(buf,"%04x",&addr);
500                 /* read type */
501                 ret=read(lpc->fwfd,buf,2);
502                 sscanf(buf,"%02x",&type);
503                 /* successfull return if type is end of file */
504                 if(type==0x01)
505                         return 0;
506                 /* read data (and cksum) */
507                 ret=read(lpc->fwfd,buf,2*(len+1));
508                 if(ret!=(2*(len+1))) {
509                         printf("fw to ram: data missing\n");
510                                 return -1;
511                 }
512                 for(ret=0;ret<len;ret++) {
513                         sscanf(buf+2*ret,"%02x",&temp);
514                         buf[ret]=temp;
515                 }
516                 /* act according to type */
517                 switch(type) {
518                         //case 0x03:
519                         //      /* get cs and ip */
520                         //      break;
521                         case 0x00:
522                                 if(len%4) {
523                                         printf("fw to ram: invalid len\n");
524                                         return -1;
525                                 }
526                                 write_to_ram(lpc,buf,addr,len);
527                                 break;
528                         case 0x04:
529                                 lpc->roff=((buf[0]<<24)|(buf[1]<<16));
530                                 break;
531                         case 0x05:
532                                 lpc->jaddr=((buf[0]<<24)|(buf[1]<<16));
533                                 lpc->jaddr|=((buf[2]<<8)|buf[3]);
534                                 break;
535                         default:
536                                 printf("fw to ram: unknown type %02x\n",type);
537                                 return -1;
538                 }
539         }
540
541         return 0;
542 }
543
544 int lpc_txbuf_flush(t_lpc *lpc) {
545
546         int i,ret;
547         u8 buf[16];
548
549         ret=1;
550         printf("flushing lpc tx buffer: ");
551         while(ret) {
552                 ret=read(lpc->sfd,buf,16);
553                 for(i=0;i<ret;i++)
554                         printf("%02x ",buf[i]);
555         }
556         printf("\n");
557
558         return 0;
559 }
560
561 int dump_files(int sfd,int dfd,u32 addr,u32 len) {
562
563         int ret;
564         int size;
565         int cnt;
566         int i;
567         u8 buf[16];
568
569         printf("dumping content (addr=0x%08x, len=0x%08x) ...\n",addr,len);
570
571         /* send cmd */
572         size=1+4+4;
573         cnt=0;
574         buf[0]=CMD_READ;
575         buf[1]=(addr>>24)&0xff;
576         buf[2]=(addr>>16)&0xff;
577         buf[3]=(addr>>8)&0xff;
578         buf[4]=addr&0xff;
579         buf[5]=(len>>24)&0xff;
580         buf[6]=(len>>16)&0xff;
581         buf[7]=(len>>8)&0xff;
582         buf[8]=len&0xff;
583         printf("  sending cmd: ");
584         while(size) {
585                 ret=write(sfd,buf+cnt,size);
586                 for(i=cnt;i<cnt+ret;i++)
587                         printf("%02x ",buf[i]);
588                 if(ret<0) {
589                         perror("dump file: send cmd ");
590                         return ret;
591                 }
592                 size-=ret;
593                 cnt+=ret;
594         }
595         printf("\n");
596
597         /* receive data and dump it to file */
598         ret=1;
599         cnt=0;
600         printf("  receiving data ...\n");
601         while(ret) {
602                 ret=read(sfd,buf,16);
603                 if(ret<0) {
604                         perror("dump file: read data");
605                         return ret;
606                 }
607                 size=ret;
608                 cnt=0;
609                 while(size) {
610                         ret=write(dfd,buf+cnt,size-cnt);
611                         if(ret<0) {
612                                 perror("dump file: write data");
613                                 return ret;
614                         }
615                         cnt+=ret;
616                         size-=ret;
617                 }
618         }
619
620         return 0;
621 }
622
623 int main(int argc,char **argv) {
624
625         t_lpc lpc;
626         int i;
627         int ret;
628
629         /*
630          * initial ... 
631          */
632
633         memset(&lpc,0,sizeof(t_lpc));
634         strncpy(lpc.freq,CRYSTFREQ,7);
635         lpc.roff=RAMOFFSET;
636         lpc.jaddr=RAMOFFSET;
637
638         /* parse argv */
639
640         for(i=1;i<argc;i++) {
641
642                 if(argv[i][0]!='-') {
643                         usage();
644                         return -1;
645                 }
646
647                 switch(argv[i][1]) {
648                         case 'd':
649                                 strncpy(lpc.sdev,argv[++i],127);
650                                 break;
651                         case 'f':
652                                 strncpy(lpc.fwfile,argv[++i],127);
653                                 lpc.info|=FIRMWARE;
654                                 break;
655                         case 'v':
656                                 lpc.info|=VERBOSE;
657                                 break;
658                         case 'c':
659                                 strncpy(lpc.freq,argv[++i],7);
660                                 break;
661                         case 'D':
662                                 if(argv[i][2]=='0') {
663                                         lpc.info|=BANK0;
664                                         strncpy(lpc.bank0,argv[++i],127);
665                                         break;
666                                 }
667                                 else if(argv[i][2]=='2') {
668                                         lpc.info|=BANK2;
669                                         strncpy(lpc.bank2,argv[++i],127);
670                                         break;
671                                 }
672                                 else if(argv[i][2]=='b') {
673                                         lpc.info|=BANK0;
674                                         strncpy(lpc.bl,argv[++i],127);
675                                         break;
676                                 }
677                                 else {
678                                         usage();
679                                         return -1;
680                                 }
681                                 
682                         default:
683                                 usage();
684                                 return -1;
685                 }
686
687         }
688
689         /* open serial port */
690         if(open_serial_device(&lpc)<0)
691                 goto end;
692
693         /* boot loader init */
694         printf("boot loader init ...\n");
695         if(bl_init(&lpc)<0)
696                 return -1;
697
698         /* quit if there is no hex file to process */
699         if(!(lpc.info&FIRMWARE)) {
700                 printf("no firmware -> aborting\n");
701                 goto end;
702         }
703
704         /* open firmware file */
705         if(open_firmware(&lpc)<0)
706                 goto end;
707
708         /* open dump files */
709         if(open_dumpfiles(&lpc)<0)
710                 goto end;
711
712         /* parse intel hex file and write to ram */
713         printf("write firmware to ram ...\n");
714         firmware_to_ram(&lpc);
715
716         /* unlock go cmd */
717         printf("unlock go command ...\n");
718         unlock_go(&lpc);
719
720         /* go! */
721         printf("go ...\n");
722         ret=go(&lpc);
723
724         /* flush the lpc2220 tx buf */
725         lpc_txbuf_flush(&lpc);
726
727         /* download flash/bootloader content */
728         if(lpc.info&BANK0)
729                 dump_files(lpc.sfd,lpc.b0fd,BANK0_ADDR,BANK_SIZE);
730         if(lpc.info&BANK2)
731                 dump_files(lpc.sfd,lpc.b2fd,BANK2_ADDR,BANK_SIZE);
732         if(lpc.info&BL)
733                 dump_files(lpc.sfd,lpc.blfd,BL_ADDR,BL_SIZE);
734
735 end:
736         if(lpc.sfd)
737                 close(lpc.sfd);
738         if(lpc.fwfd)
739                 close(lpc.fwfd);
740         if(lpc.b0fd)
741                 close(lpc.b0fd);
742         if(lpc.b2fd)
743                 close(lpc.b2fd);
744         if(lpc.blfd)
745                 close(lpc.blfd);
746
747         return 0;
748 }
749