]> hackdaworld.org Git - my-code/arm.git/blob - betty/lpcload.c
6ec118628d38eb537e43662cc408256159427bce
[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,ret,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+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),bcnt==nlen-3?len%3:3);
380                 txrxbuf[5]='\r';
381                 txrxbuf[6]='\n';
382
383                 /* checksum */
384                 checksum+=((u8)buf[bcnt]+(u8)buf[bcnt+1]+(u8)buf[bcnt+2]);
385
386                 /* send a data line */
387                 txrx(lpc,txrxbuf,6,TXRX_TYPE_DATA);
388
389                 /* increase counters */
390                 lcount+=1;
391                 bcnt+=3;
392                 count+=3;
393
394                 /* checksum */
395                 if((!(lcount%20))|(bcnt==nlen)) {
396                         /* send backtick */
397                         memcpy(txrxbuf,"`\r\n",3);
398                         //txrx(lpc,txrxbuf,3,TXRX_TYPE_DATA);
399                         /* send checksum */
400                         snprintf(txrxbuf,BUFSIZE,"%d\r\n",checksum);
401                         slen=strlen(txrxbuf);
402                         txrx(lpc,txrxbuf,slen,TXRX_TYPE_CKSM);
403                         if(!strncmp(txrxbuf,"RESE",4)) {
404                                 read(lpc->sfd,txrxbuf+4,4);
405                                 printf("ram write: resending ...\n");
406                                 bcnt-=count;
407                         }
408                         if(strncmp(txrxbuf,"OK\r\n",4)) {
409                                 printf("ram write: bad response\n");
410                                 return -1;
411                         }
412                         /* reset checksum & counter */
413                         checksum=0;
414                         count=0;
415                 }
416
417         }
418
419         return 0;
420 }
421
422 int firmware_to_ram(t_lpc *lpc) {
423
424         char buf[BUFSIZE];
425         u32 addr,len,type;
426         int ret,temp;
427
428         /* read a line */
429         ret=1;
430         while(ret) {
431                 /* sync line */
432                 ret=read(lpc->fwfd,buf,1);
433                 switch(buf[0]) {
434                         case '\r':
435                                 continue;
436                         case '\n':
437                                 continue;
438                         case ':':
439                                 /* start code */
440                                 break;
441                         default:
442                                 printf("fw to ram: no ihex format\n");
443                                 return -1;
444                 }
445                 /* read len */
446                 ret=read(lpc->fwfd,buf,2);
447                 sscanf(buf,"%02x",&len);
448                 if(len%4) {
449                         printf("fw to ram: len not a multiple of 4\n");
450                         return -1;
451                 }
452                 /* read addr */
453                 ret=read(lpc->fwfd,buf,4);
454                 sscanf(buf,"%04x",&addr);
455                 /* read type */
456                 ret=read(lpc->fwfd,buf,2);
457                 sscanf(buf,"%02x",&type);
458                 /* successfull return if type is end of file */
459                 if(type==0x01)
460                         return 0;
461                 /* read data (and cksum) */
462                 ret=read(lpc->fwfd,buf,2*(len+1));
463                 if(ret!=(2*(len+1))) {
464                         printf("fw to ram: data missing\n");
465                                 return -1;
466                 }
467                 for(ret=0;ret<len;ret++) {
468                         sscanf(buf+2*ret,"%02x",&temp);
469                         buf[ret]=temp;
470                 }
471                 /* act according to type */
472                 switch(type) {
473                         case 0x03:
474                                 /* get cs and ip */
475                                 break;
476                         case 0x00:
477                                 write_to_ram(lpc,buf,addr,len);
478                                 break;
479                         default:
480                                 printf("fw to ram: unknown type %02x\n",type);
481                                 return -1;
482                 }
483         }
484
485         return 0;
486 }
487
488 int main(int argc,char **argv) {
489
490         t_lpc lpc;
491         int i;
492
493         /*
494          * initial ... 
495          */
496
497         memset(&lpc,0,sizeof(t_lpc));
498         strncpy(lpc.freq,CRYSTFREQ,7);
499         lpc.roff=RAMOFFSET;
500
501         /* parse argv */
502
503         for(i=1;i<argc;i++) {
504
505                 if(argv[i][0]!='-') {
506                         usage();
507                         return -1;
508                 }
509
510                 switch(argv[i][1]) {
511                         case 'd':
512                                 strncpy(lpc.sdev,argv[++i],127);
513                                 break;
514                         case 'f':
515                                 strncpy(lpc.fwfile,argv[++i],127);
516                                 lpc.info|=FIRMWARE;
517                                 break;
518                         case 'v':
519                                 lpc.info|=VERBOSE;
520                                 break;
521                         case 'c':
522                                 strncpy(lpc.freq,argv[++i],7);
523                                 break;
524                         default:
525                                 usage();
526                                 return -1;
527                 }
528
529         }
530
531         /* open serial port */
532         if(open_serial_device(&lpc)<0)
533                 goto end;
534
535         /* open firmware file */
536         if(open_firmware(&lpc)<0)
537                 goto end;
538
539         /* boot loader init */
540         printf("boot loader init ...\n");
541         if(bl_init(&lpc)<0)
542                 return -1;
543
544         /* parse intel hex file and write to ram */
545         printf("write firmware to ram ...\n");
546         firmware_to_ram(&lpc);
547
548         /* unlock go cmd */
549         unlock_go(&lpc);
550
551         /* go! */
552         go(&lpc);
553
554 end:
555         close(lpc.sfd);
556         close(lpc.fwfd);
557
558         return 0;
559 }
560