]> hackdaworld.org Git - my-code/arm.git/blob - betty/lpcload.c
initial checkin of lpc2xxx header file
[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
22 #define TXRX_TYPE_BAUD          0x01
23 #define TXRX_TYPE_SYNC          0x02
24 #define TXRX_TYPE_CMD           0x03
25 #define TXRX_TYPE_DATA          0x04
26 #define TXRX_TYPE_CKSM          0x05
27
28 #define CMD_SUCCESS             "0\r\n"
29 #define INVALID_COMMAND         "1\r\n"
30 #define SRC_ADDR_ERROR          "2\r\n"
31 #define DST_ADDR_ERROR          "3\r\n"
32 #define SRC_ADDR_NOT_MAPPED     "4\r\n"
33 #define DST_ADDR_NOT_MAPPED     "5\r\n"
34 #define COUNT_ERROR             "6\r\n"
35 #define COMPARE_ERROR           "10\r\n"
36 #define BUSY                    "11\r\n"
37 #define PARAM_ERROR             "12\r\n"
38 #define ADDR_ERROR              "13\r\n"
39 #define ADDR_NOT_MAPPED         "14\r\n"
40 #define CMD_LOCKED              "15\r\n"
41 #define INVALID_CODE            "16\r\n"
42 #define INVALID_BAUD_RATE       "17\r\n"
43 #define INVALID_STOP_BIT        "18\r\n"
44
45 #define CRYSTFREQ               "10000"
46 #define RAMOFFSET               0x40000200
47
48 #define BUFSIZE                 128
49
50 typedef unsigned char u8;
51 typedef unsigned short u16;
52 typedef unsigned int u32;
53
54 typedef struct s_lpc {
55         int sfd;                /* serial fd */
56         char sdev[128];         /* seriel device */
57         int fwfd;               /* fimrware fd */
58         char fwfile[128];       /* firmware file */
59         u8 info;                /* info/mode */
60         char freq[8];           /* frequency */
61         u32 hoff;               /* start addr of ihex file */
62         u32 roff;               /* ram offset of uc */
63 } t_lpc;
64
65 void usage(void) {
66
67         printf("possible argv:\n");
68         printf("  -d <serial device>\n");
69         printf("  -f <firmware>\n");
70         printf("  -c <crystal freq>\n");
71         printf("  -r <ram offset>\n");
72         printf("  -v\n");
73
74 }
75
76 int open_serial_device(t_lpc *lpc) {
77
78         struct termios term;
79
80         //memset(&term,0,sizeof(struct termios));
81
82         /* open serial device */
83
84         lpc->sfd=open(lpc->sdev,O_RDWR);
85         if(lpc->sfd<0) {
86                 perror("tts open");
87                 return lpc->sfd;
88         }
89
90         /* configure the serial device */
91
92         tcgetattr(lpc->sfd,&term);
93
94         // input/output baudrate
95
96         cfsetispeed(&term,B9600);
97         cfsetospeed(&term,B9600);
98
99         // control options -> 8n1
100
101         term.c_cflag&=~PARENB;  // no parity
102         term.c_cflag&=~CSTOPB;  // only 1 stop bit
103         term.c_cflag&=~CSIZE;   // no bit mask for data bits
104         term.c_cflag|=CS8;      // 8 data bits
105
106         // line options -> raw input
107         
108         term.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);
109
110         // input options -> enable flow control
111         
112         //term.c_iflag&=~(IXON|IXOFF|IXANY|INLCR|ICRNL);
113         term.c_iflag&=~(INLCR|ICRNL|IXANY);
114         term.c_iflag|=(IXON|IXOFF);
115
116         // more control options -> timeout / flow control
117         
118         term.c_cc[VMIN]=0;
119         term.c_cc[VTIME]=10;    // 1 second timeout
120         term.c_cc[VSTART]=0x11;
121         term.c_cc[VSTOP]=0x13;
122
123         tcsetattr(lpc->sfd,TCSANOW,&term);
124
125         return lpc->sfd;
126 }
127
128 int open_firmware(t_lpc *lpc) {
129
130         int ret;
131         char buf[BUFSIZE];
132
133         /* open firmware file */
134
135         lpc->fwfd=open(lpc->fwfile,O_RDONLY);
136
137         if(lpc->fwfd<0)
138                 perror("fw open");
139
140         /* read hex file offset */
141
142         ret=read(lpc->fwfd,buf,7);
143         if(buf[0]!=':') {
144                 printf("fw open: not an intel hex file?\n");
145                 return -1;
146         }
147         sscanf(buf+3,"%04x",&(lpc->hoff));
148         lseek(lpc->fwfd,0,SEEK_SET);
149
150         return lpc->fwfd;
151 }
152
153 int txrx(t_lpc *lpc,char *buf,int len,u8 type) {
154
155         int ret,cnt;
156         int i;
157
158         /* write */
159
160         if(lpc->info&VERBOSE)
161                 printf("  >> ");
162         cnt=0;
163         while(len) {
164                 ret=write(lpc->sfd,buf+cnt,len);
165                 if(ret<0) {
166                         perror("txrx write");
167                         return ret;
168                 }
169                 if(lpc->info&VERBOSE)
170                         for(i=0;i<ret;i++)
171                                 printf("%c",
172                                        ((buf[cnt+i]>0x19)&(buf[cnt+i]<0x7f))?
173                                        buf[cnt+i]:'.');
174                 len-=ret;
175                 cnt+=ret;
176         }
177         if(lpc->info&VERBOSE) {
178                 printf(" | ");
179                 for(i=0;i<cnt;i++)
180                         printf("%02x ",buf[i]);
181                 printf("| (%d)\n",cnt);
182         }
183
184         /* cut the echo if not of type auto baud */
185
186         if(type!=TXRX_TYPE_BAUD) {
187                 while(cnt) {
188                         ret=read(lpc->sfd,buf,cnt);
189                         if(ret<0) {
190                                 perror("txrx echo cut");
191                                 return ret;
192                         }
193                         cnt-=ret;
194                 }
195         }
196
197         /* return here if type is data */
198
199         if(type==TXRX_TYPE_DATA)
200                 return cnt;
201
202         /* read */
203
204         ret=read(lpc->sfd,buf,1);
205         if(ret<0) {
206                 perror("txrx read (first byte)");
207                 return ret;
208         }
209                 
210         switch(buf[0]) {
211                 case 'S':
212                         cnt=13;
213                         break;
214                 case 'O':
215                         cnt=3;
216                         break;
217                 case 'R':
218                         cnt=7;
219                         break;
220                 case '0':
221                         cnt=2;
222                         break;
223                 default:
224                         printf("txrx read: bad return byte '%02x'\n",buf[0]);
225                         break;
226         }
227
228         ret=1;
229         i=cnt;
230         while(i) {
231                 ret=read(lpc->sfd,buf+1+cnt-i,i);
232                 if(ret<0) {
233                         perror("txrx read (next bytes)");
234                         return ret;
235                 }
236                 i-=ret;
237         }
238         if(lpc->info&VERBOSE) {
239                 printf("  << ");
240                 for(i=0;i<cnt+1;i++)
241                         printf("%c",((buf[i]>0x19)&(buf[i]<0x7f))?
242                                     buf[i]:'.');
243                 printf(" | ");
244                 for(i=0;i<cnt+1;i++)
245                         printf("%02x ",buf[i]);
246                 printf("| (%d)\n",cnt+1);
247         }
248         buf[cnt+1]='\0';
249
250         /* check/strip return code if type is cmd */
251
252         if(type==TXRX_TYPE_CMD) {
253                 ret=strlen(CMD_SUCCESS);
254                 if(!strncmp(buf,CMD_SUCCESS,ret)) {
255                         for(i=ret;i<cnt;i++)
256                                 buf[i-ret]=buf[i];
257                         buf[cnt]='\0';
258                 }
259                 else {
260                         printf("txrx bad return code!\n");
261                         return -1;
262                 }
263         }
264
265         return cnt;
266 }
267
268 int bl_init(t_lpc *lpc) {
269
270         char buf[BUFSIZE];
271         int len;
272
273         /* auto baud sequence */
274         buf[0]='?';
275         txrx(lpc,buf,1,TXRX_TYPE_BAUD);
276         if(strncmp(buf,"Synchronized\r\n",14)) {
277                 printf("auto baud detection failed\n");
278                 return -1;
279         }
280
281         /* tell bl that we are synchronized (it's allready in buf) */
282         txrx(lpc,buf,14,TXRX_TYPE_SYNC);
283         if(strncmp(buf,"OK\r\n",4)) {
284                 printf("sync failed\n");
285                 return -1;
286         }
287
288         /* tell bl the crystal frequency */
289         len=strlen(lpc->freq)+2;
290         strncpy(buf,lpc->freq,BUFSIZE);
291         buf[len-2]='\r';
292         buf[len-1]='\n';
293         txrx(lpc,buf,len,TXRX_TYPE_SYNC);
294         if(strncmp(buf,"OK\r\n",4)) {
295                 printf("freq set failed\n");
296                 return -1;
297         }
298
299         return 0;
300 }
301
302 int unlock_go(t_lpc *lpc) {
303
304         char buf[BUFSIZE];
305         int ret;
306
307         memcpy(buf,"U 23130\r\n",9);
308         ret=txrx(lpc,buf,9,TXRX_TYPE_CMD);
309
310         return ret;
311 }
312
313 int go(t_lpc *lpc) {
314
315         char buf[BUFSIZE];
316         int ret,len;
317
318         snprintf(buf,BUFSIZE,"G %d A\r\n",lpc->roff);
319         len=strlen(buf);
320         ret=txrx(lpc,buf,len,TXRX_TYPE_CMD);
321
322         return ret;
323 }
324
325 int uuencode(u8 *in,u8 *out,int len) {
326
327         out[0]=0x20+len;
328         out[1]=0x20+((in[0]>>2)&0x3f);
329         out[2]=0x20+(((in[0]<<4)|(in[1]>>4))&0x3f);
330         out[3]=0x20+(((in[1]<<2)|(in[2]>>6))&0x3f);
331         out[4]=0x20+(in[2]&0x3f);
332
333         return 0;
334 }
335
336 int write_to_ram(t_lpc *lpc,char *buf,u32 addr,int len) {
337
338         int lcount;
339         u32 checksum;
340         char txrxbuf[BUFSIZE];
341         int count,bcnt;
342         int nlen,slen;
343         int i;
344
345         /* check length */
346         if(len%4) {
347                 printf("ram write: not a multiple of 4\n");
348                 return -1;
349         }
350
351         /* make it a multiple of 3 (reason: uuencode) */
352         nlen=(!(len%3))?len:((len/3+1)*3);
353         if(nlen>BUFSIZE) {
354                 printf("ram write: too much data\n");
355                 return -1;
356         }
357         for(i=len;i<nlen;i++) buf[i]=0;
358
359         /* prepare addr */
360         addr+=(lpc->roff-lpc->hoff);
361
362         /* prepare write command */
363         if(lpc->info&VERBOSE)
364                 printf("writing 0x%02x bytes to 0x%08x\n",len,addr);
365         snprintf(txrxbuf,BUFSIZE,"W %d %d\r\n",addr,len);
366         slen=strlen(txrxbuf);
367
368         /* send command and check return code */
369         txrx(lpc,txrxbuf,slen,TXRX_TYPE_CMD);
370
371         /* send data */
372         lcount=0;
373         bcnt=0;
374         count=0;
375         checksum=0;
376         while(bcnt<nlen) {
377
378                 /* uuencode / prepare data bytes */
379                 uuencode((u8 *)(buf+bcnt),(u8 *)(txrxbuf),
380                          (bcnt==nlen-3)?(len%3?len%3:3):3);
381                 txrxbuf[5]='\r';
382                 txrxbuf[6]='\n';
383
384                 /* checksum */
385                 checksum+=((u8)buf[bcnt]+(u8)buf[bcnt+1]+(u8)buf[bcnt+2]);
386
387                 /* send a data line */
388                 txrx(lpc,txrxbuf,7,TXRX_TYPE_DATA);
389
390                 /* increase counters */
391                 lcount+=1;
392                 bcnt+=3;
393                 count+=3;
394
395                 /* checksum */
396                 if((!(lcount%20))|(bcnt==nlen)) {
397                         /* send backtick */
398                         memcpy(txrxbuf,"`\r\n",3);
399                         //txrx(lpc,txrxbuf,3,TXRX_TYPE_DATA);
400                         /* send checksum */
401                         snprintf(txrxbuf,BUFSIZE,"%d\r\n",checksum);
402                         slen=strlen(txrxbuf);
403                         txrx(lpc,txrxbuf,slen,TXRX_TYPE_CKSM);
404                         if(!strncmp(txrxbuf,"RESE",4)) {
405                                 read(lpc->sfd,txrxbuf+4,4);
406                                 printf("ram write: resending ...\n");
407                                 bcnt-=count;
408                         }
409                         if(strncmp(txrxbuf,"OK\r\n",4)) {
410                                 printf("ram write: bad response\n");
411                                 return -1;
412                         }
413                         /* reset checksum & counter */
414                         checksum=0;
415                         count=0;
416                 }
417
418         }
419
420         return 0;
421 }
422
423 int firmware_to_ram(t_lpc *lpc) {
424
425         char buf[BUFSIZE];
426         u32 addr,len,type;
427         int ret,temp;
428
429         /* read a line */
430         ret=1;
431         while(ret) {
432                 /* sync line */
433                 ret=read(lpc->fwfd,buf,1);
434                 switch(buf[0]) {
435                         case '\r':
436                                 continue;
437                         case '\n':
438                                 continue;
439                         case ':':
440                                 /* start code */
441                                 break;
442                         default:
443                                 printf("fw to ram: no ihex format\n");
444                                 return -1;
445                 }
446                 /* read len */
447                 ret=read(lpc->fwfd,buf,2);
448                 sscanf(buf,"%02x",&len);
449                 if(len%4) {
450                         printf("fw to ram: len not a multiple of 4\n");
451                         return -1;
452                 }
453                 /* read addr */
454                 ret=read(lpc->fwfd,buf,4);
455                 sscanf(buf,"%04x",&addr);
456                 /* read type */
457                 ret=read(lpc->fwfd,buf,2);
458                 sscanf(buf,"%02x",&type);
459                 /* successfull return if type is end of file */
460                 if(type==0x01)
461                         return 0;
462                 /* read data (and cksum) */
463                 ret=read(lpc->fwfd,buf,2*(len+1));
464                 if(ret!=(2*(len+1))) {
465                         printf("fw to ram: data missing\n");
466                                 return -1;
467                 }
468                 for(ret=0;ret<len;ret++) {
469                         sscanf(buf+2*ret,"%02x",&temp);
470                         buf[ret]=temp;
471                 }
472                 /* act according to type */
473                 switch(type) {
474                         case 0x03:
475                                 /* get cs and ip */
476                                 break;
477                         case 0x00:
478                                 write_to_ram(lpc,buf,addr,len);
479                                 break;
480                         default:
481                                 printf("fw to ram: unknown type %02x\n",type);
482                                 return -1;
483                 }
484         }
485
486         return 0;
487 }
488
489 int main(int argc,char **argv) {
490
491         t_lpc lpc;
492         int i;
493
494         /*
495          * initial ... 
496          */
497
498         memset(&lpc,0,sizeof(t_lpc));
499         strncpy(lpc.freq,CRYSTFREQ,7);
500         lpc.roff=RAMOFFSET;
501
502         /* parse argv */
503
504         for(i=1;i<argc;i++) {
505
506                 if(argv[i][0]!='-') {
507                         usage();
508                         return -1;
509                 }
510
511                 switch(argv[i][1]) {
512                         case 'd':
513                                 strncpy(lpc.sdev,argv[++i],127);
514                                 break;
515                         case 'f':
516                                 strncpy(lpc.fwfile,argv[++i],127);
517                                 lpc.info|=FIRMWARE;
518                                 break;
519                         case 'v':
520                                 lpc.info|=VERBOSE;
521                                 break;
522                         case 'c':
523                                 strncpy(lpc.freq,argv[++i],7);
524                                 break;
525                         default:
526                                 usage();
527                                 return -1;
528                 }
529
530         }
531
532         /* open serial port */
533         if(open_serial_device(&lpc)<0)
534                 goto end;
535
536         /* open firmware file */
537         if(open_firmware(&lpc)<0)
538                 goto end;
539
540         /* boot loader init */
541         printf("boot loader init ...\n");
542         if(bl_init(&lpc)<0)
543                 return -1;
544
545         /* parse intel hex file and write to ram */
546         printf("write firmware to ram ...\n");
547         firmware_to_ram(&lpc);
548
549         /* unlock go cmd */
550         printf("unlock go command ...\n");
551         unlock_go(&lpc);
552
553         /* go! */
554         printf("go ...\n");
555         go(&lpc);
556         printf("\n");
557
558 end:
559         close(lpc.sfd);
560         close(lpc.fwfd);
561
562         return 0;
563 }
564