more verbose logging
[pandora-x-loader.git] / common / cmd_load.c
1 /*
2  * (C) Copyright 2000-2004
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 /*
25  * Serial up- and download support
26  */
27 #include <common.h>
28
29 #define putc serial_putc
30 #define tstc serial_tstc
31
32 /*******************************************************
33  * Routine: delay
34  * Description: spinning delay to use before udelay works
35  ******************************************************/
36 static inline void delay(unsigned long loops)
37 {
38         __asm__ volatile ("1:\n" "subs %0, %1, #1\n"
39                           "bne 1b":"=r" (loops):"0"(loops));
40 }
41 #ifndef CFG_UDELAY
42 static inline
43 #endif
44 void udelay(unsigned long us)
45 {
46         delay(us * 200); /* approximate */
47 }
48
49 #ifdef CFG_CMD_FAT
50 extern void * memcpy(void * dest,const void *src,size_t count);
51 #else
52 void * memcpy(void * dest,const void *src,size_t count)
53 {
54         char *tmp = (char *) dest, *s = (char *) src;
55
56         while (count--)
57                 *tmp++ = *s++;
58
59         return dest;
60 }
61 #endif
62
63 /* -------------------------------------------------------------------- */
64
65 #define XON_CHAR        17
66 #define XOFF_CHAR       19
67 #define START_CHAR      0x01
68 #define ETX_CHAR        0x03
69 #define END_CHAR        0x0D
70 #define SPACE           0x20
71 #define K_ESCAPE        0x23
72 #define SEND_TYPE       'S'
73 #define DATA_TYPE       'D'
74 #define ACK_TYPE        'Y'
75 #define NACK_TYPE       'N'
76 #define BREAK_TYPE      'B'
77 #define tochar(x) ((char) (((x) + SPACE) & 0xff))
78 #define untochar(x) ((int) (((x) - SPACE) & 0xff))
79
80 extern int os_data_count;
81 extern int os_data_header[8];
82
83 static void set_kerm_bin_mode(unsigned long *);
84 static int k_recv(void);
85 static ulong load_serial_bin (ulong offset);
86
87
88 char his_eol;        /* character he needs at end of packet */
89 int  his_pad_count;  /* number of pad chars he needs */
90 char his_pad_char;   /* pad chars he needs */
91 char his_quote;      /* quote chars he'll use */
92
93 int do_load_serial_bin (ulong offset, int baudrate)
94 {
95         ulong addr;
96         int rcode = 0;
97
98         printf ("## Ready for binary (kermit) download "
99                 "to 0x%08lX at %d bps...\n",
100                 offset,
101                 baudrate);
102         addr = load_serial_bin (offset);
103         if (addr == ~0) {
104                 printf ("## Binary (kermit) download aborted\n");
105                 rcode = 1;
106         } else {
107                 printf ("## Start Addr      = 0x%08lX\n", addr);
108         }
109
110         return rcode;
111 }
112
113
114 static ulong load_serial_bin (ulong offset)
115 {
116         int size, i;
117
118         set_kerm_bin_mode ((ulong *) offset);
119         size = k_recv ();
120
121         /*
122          * Gather any trailing characters (for instance, the ^D which
123          * is sent by 'cu' after sending a file), and give the
124          * box some time (100 * 1 ms)
125          */
126         for (i=0; i<100; ++i) {
127                 if (tstc()) {
128                         (void) getc();
129                 }
130                 udelay(1000);
131         }
132
133         printf("## Total Size      = 0x%08x = %d Bytes\n", size, size);
134
135         return offset;
136 }
137
138 void send_pad (void)
139 {
140         int count = his_pad_count;
141
142         while (count-- > 0)
143                 putc (his_pad_char);
144 }
145
146 /* converts escaped kermit char to binary char */
147 char ktrans (char in)
148 {
149         if ((in & 0x60) == 0x40) {
150                 return (char) (in & ~0x40);
151         } else if ((in & 0x7f) == 0x3f) {
152                 return (char) (in | 0x40);
153         } else
154                 return in;
155 }
156
157 int chk1 (char *buffer)
158 {
159         int total = 0;
160
161         while (*buffer) {
162                 total += *buffer++;
163         }
164         return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
165 }
166
167 void s1_sendpacket (char *packet)
168 {
169         send_pad ();
170         while (*packet) {
171                 putc (*packet++);
172         }
173 }
174
175 static char a_b[24];
176 void send_ack (int n)
177 {
178         a_b[0] = START_CHAR;
179         a_b[1] = tochar (3);
180         a_b[2] = tochar (n);
181         a_b[3] = ACK_TYPE;
182         a_b[4] = '\0';
183         a_b[4] = tochar (chk1 (&a_b[1]));
184         a_b[5] = his_eol;
185         a_b[6] = '\0';
186         s1_sendpacket (a_b);
187 }
188
189 void send_nack (int n)
190 {
191         a_b[0] = START_CHAR;
192         a_b[1] = tochar (3);
193         a_b[2] = tochar (n);
194         a_b[3] = NACK_TYPE;
195         a_b[4] = '\0';
196         a_b[4] = tochar (chk1 (&a_b[1]));
197         a_b[5] = his_eol;
198         a_b[6] = '\0';
199         s1_sendpacket (a_b);
200 }
201
202
203 /* os_data_* takes an OS Open image and puts it into memory, and
204    puts the boot header in an array named os_data_header
205
206    if image is binary, no header is stored in os_data_header.
207 */
208 void (*os_data_init) (void);
209 void (*os_data_char) (char new_char);
210 static int os_data_state, os_data_state_saved;
211 int os_data_count;
212 static int os_data_count_saved;
213 static char *os_data_addr, *os_data_addr_saved;
214 static char *bin_start_address;
215 int os_data_header[8];
216 static void bin_data_init (void)
217 {
218         os_data_state = 0;
219         os_data_count = 0;
220         os_data_addr = bin_start_address;
221 }
222 static void os_data_save (void)
223 {
224         os_data_state_saved = os_data_state;
225         os_data_count_saved = os_data_count;
226         os_data_addr_saved = os_data_addr;
227 }
228 static void os_data_restore (void)
229 {
230         os_data_state = os_data_state_saved;
231         os_data_count = os_data_count_saved;
232         os_data_addr = os_data_addr_saved;
233 }
234 static void bin_data_char (char new_char)
235 {
236         switch (os_data_state) {
237         case 0:                                 /* data */
238                 *os_data_addr++ = new_char;
239                 --os_data_count;
240                 break;
241         }
242 }
243 static void set_kerm_bin_mode (unsigned long *addr)
244 {
245         bin_start_address = (char *) addr;
246         os_data_init = bin_data_init;
247         os_data_char = bin_data_char;
248 }
249
250
251 /* k_data_* simply handles the kermit escape translations */
252 static int k_data_escape, k_data_escape_saved;
253 void k_data_init (void)
254 {
255         k_data_escape = 0;
256         os_data_init ();
257 }
258 void k_data_save (void)
259 {
260         k_data_escape_saved = k_data_escape;
261         os_data_save ();
262 }
263 void k_data_restore (void)
264 {
265         k_data_escape = k_data_escape_saved;
266         os_data_restore ();
267 }
268 void k_data_char (char new_char)
269 {
270         if (k_data_escape) {
271                 /* last char was escape - translate this character */
272                 os_data_char (ktrans (new_char));
273                 k_data_escape = 0;
274         } else {
275                 if (new_char == his_quote) {
276                         /* this char is escape - remember */
277                         k_data_escape = 1;
278                 } else {
279                         /* otherwise send this char as-is */
280                         os_data_char (new_char);
281                 }
282         }
283 }
284
285 #define SEND_DATA_SIZE  20
286 char send_parms[SEND_DATA_SIZE];
287 char *send_ptr;
288
289 /* handle_send_packet interprits the protocol info and builds and
290    sends an appropriate ack for what we can do */
291 void handle_send_packet (int n)
292 {
293         int length = 3;
294         int bytes;
295
296         /* initialize some protocol parameters */
297         his_eol = END_CHAR;             /* default end of line character */
298         his_pad_count = 0;
299         his_pad_char = '\0';
300         his_quote = K_ESCAPE;
301
302         /* ignore last character if it filled the buffer */
303         if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
304                 --send_ptr;
305         bytes = send_ptr - send_parms;  /* how many bytes we'll process */
306         do {
307                 if (bytes-- <= 0)
308                         break;
309                 /* handle MAXL - max length */
310                 /* ignore what he says - most I'll take (here) is 94 */
311                 a_b[++length] = tochar (94);
312                 if (bytes-- <= 0)
313                         break;
314                 /* handle TIME - time you should wait for my packets */
315                 /* ignore what he says - don't wait for my ack longer than 1 second */
316                 a_b[++length] = tochar (1);
317                 if (bytes-- <= 0)
318                         break;
319                 /* handle NPAD - number of pad chars I need */
320                 /* remember what he says - I need none */
321                 his_pad_count = untochar (send_parms[2]);
322                 a_b[++length] = tochar (0);
323                 if (bytes-- <= 0)
324                         break;
325                 /* handle PADC - pad chars I need */
326                 /* remember what he says - I need none */
327                 his_pad_char = ktrans (send_parms[3]);
328                 a_b[++length] = 0x40;   /* He should ignore this */
329                 if (bytes-- <= 0)
330                         break;
331                 /* handle EOL - end of line he needs */
332                 /* remember what he says - I need CR */
333                 his_eol = untochar (send_parms[4]);
334                 a_b[++length] = tochar (END_CHAR);
335                 if (bytes-- <= 0)
336                         break;
337                 /* handle QCTL - quote control char he'll use */
338                 /* remember what he says - I'll use '#' */
339                 his_quote = send_parms[5];
340                 a_b[++length] = '#';
341                 if (bytes-- <= 0)
342                         break;
343                 /* handle QBIN - 8-th bit prefixing */
344                 /* ignore what he says - I refuse */
345                 a_b[++length] = 'N';
346                 if (bytes-- <= 0)
347                         break;
348                 /* handle CHKT - the clock check type */
349                 /* ignore what he says - I do type 1 (for now) */
350                 a_b[++length] = '1';
351                 if (bytes-- <= 0)
352                         break;
353                 /* handle REPT - the repeat prefix */
354                 /* ignore what he says - I refuse (for now) */
355                 a_b[++length] = 'N';
356                 if (bytes-- <= 0)
357                         break;
358                 /* handle CAPAS - the capabilities mask */
359                 /* ignore what he says - I only do long packets - I don't do windows */
360                 a_b[++length] = tochar (2);     /* only long packets */
361                 a_b[++length] = tochar (0);     /* no windows */
362                 a_b[++length] = tochar (94);    /* large packet msb */
363                 a_b[++length] = tochar (94);    /* large packet lsb */
364         } while (0);
365
366         a_b[0] = START_CHAR;
367         a_b[1] = tochar (length);
368         a_b[2] = tochar (n);
369         a_b[3] = ACK_TYPE;
370         a_b[++length] = '\0';
371         a_b[length] = tochar (chk1 (&a_b[1]));
372         a_b[++length] = his_eol;
373         a_b[++length] = '\0';
374         s1_sendpacket (a_b);
375 }
376
377 /* k_recv receives a OS Open image file over kermit line */
378 static int k_recv (void)
379 {
380         char new_char;
381         char k_state, k_state_saved;
382         int sum;
383         int done;
384         int length;
385         int n, last_n;
386         int z = 0;
387         int len_lo, len_hi;
388
389         /* initialize some protocol parameters */
390         his_eol = END_CHAR;             /* default end of line character */
391         his_pad_count = 0;
392         his_pad_char = '\0';
393         his_quote = K_ESCAPE;
394
395         /* initialize the k_recv and k_data state machine */
396         done = 0;
397         k_state = 0;
398         k_data_init ();
399         k_state_saved = k_state;
400         k_data_save ();
401         n = 0;                          /* just to get rid of a warning */
402         last_n = -1;
403
404         /* expect this "type" sequence (but don't check):
405            S: send initiate
406            F: file header
407            D: data (multiple)
408            Z: end of file
409            B: break transmission
410          */
411
412         /* enter main loop */
413         while (!done) {
414                 /* set the send packet pointer to begining of send packet parms */
415                 send_ptr = send_parms;
416
417                 /* With each packet, start summing the bytes starting with the length.
418                    Save the current sequence number.
419                    Note the type of the packet.
420                    If a character less than SPACE (0x20) is received - error.
421                  */
422
423 #if 0
424                 /* OLD CODE, Prior to checking sequence numbers */
425                 /* first have all state machines save current states */
426                 k_state_saved = k_state;
427                 k_data_save ();
428 #endif
429
430                 /* get a packet */
431                 /* wait for the starting character or ^C */
432                 for (;;) {
433                         switch (getc ()) {
434                         case START_CHAR:        /* start packet */
435                                 goto START;
436                         case ETX_CHAR:          /* ^C waiting for packet */
437                                 return (0);
438                         default:
439                                 ;
440                         }
441                 }
442 START:
443                 /* get length of packet */
444                 sum = 0;
445                 new_char = getc ();
446                 if ((new_char & 0xE0) == 0)
447                         goto packet_error;
448                 sum += new_char & 0xff;
449                 length = untochar (new_char);
450                 /* get sequence number */
451                 new_char = getc ();
452                 if ((new_char & 0xE0) == 0)
453                         goto packet_error;
454                 sum += new_char & 0xff;
455                 n = untochar (new_char);
456                 --length;
457
458                 /* NEW CODE - check sequence numbers for retried packets */
459                 /* Note - this new code assumes that the sequence number is correctly
460                  * received.  Handling an invalid sequence number adds another layer
461                  * of complexity that may not be needed - yet!  At this time, I'm hoping
462                  * that I don't need to buffer the incoming data packets and can write
463                  * the data into memory in real time.
464                  */
465                 if (n == last_n) {
466                         /* same sequence number, restore the previous state */
467                         k_state = k_state_saved;
468                         k_data_restore ();
469                 } else {
470                         /* new sequence number, checkpoint the download */
471                         last_n = n;
472                         k_state_saved = k_state;
473                         k_data_save ();
474                 }
475                 /* END NEW CODE */
476
477                 /* get packet type */
478                 new_char = getc ();
479                 if ((new_char & 0xE0) == 0)
480                         goto packet_error;
481                 sum += new_char & 0xff;
482                 k_state = new_char;
483                 --length;
484                 /* check for extended length */
485                 if (length == -2) {
486                         /* (length byte was 0, decremented twice) */
487                         /* get the two length bytes */
488                         new_char = getc ();
489                         if ((new_char & 0xE0) == 0)
490                                 goto packet_error;
491                         sum += new_char & 0xff;
492                         len_hi = untochar (new_char);
493                         new_char = getc ();
494                         if ((new_char & 0xE0) == 0)
495                                 goto packet_error;
496                         sum += new_char & 0xff;
497                         len_lo = untochar (new_char);
498                         length = len_hi * 95 + len_lo;
499                         /* check header checksum */
500                         new_char = getc ();
501                         if ((new_char & 0xE0) == 0)
502                                 goto packet_error;
503                         if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
504                                 goto packet_error;
505                         sum += new_char & 0xff;
506 /* --length; */ /* new length includes only data and block check to come */
507                 }
508                 /* bring in rest of packet */
509                 while (length > 1) {
510                         new_char = getc ();
511                         if ((new_char & 0xE0) == 0)
512                                 goto packet_error;
513                         sum += new_char & 0xff;
514                         --length;
515                         if (k_state == DATA_TYPE) {
516                                 /* pass on the data if this is a data packet */
517                                 k_data_char (new_char);
518                         } else if (k_state == SEND_TYPE) {
519                                 /* save send pack in buffer as is */
520                                 *send_ptr++ = new_char;
521                                 /* if too much data, back off the pointer */
522                                 if (send_ptr >= &send_parms[SEND_DATA_SIZE])
523                                         --send_ptr;
524                         }
525                 }
526                 /* get and validate checksum character */
527                 new_char = getc ();
528                 if ((new_char & 0xE0) == 0)
529                         goto packet_error;
530                 if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
531                         goto packet_error;
532                 /* get END_CHAR */
533                 new_char = getc ();
534                 if (new_char != END_CHAR) {
535                   packet_error:
536                         /* restore state machines */
537                         k_state = k_state_saved;
538                         k_data_restore ();
539                         /* send a negative acknowledge packet in */
540                         send_nack (n);
541                 } else if (k_state == SEND_TYPE) {
542                         /* crack the protocol parms, build an appropriate ack packet */
543                         handle_send_packet (n);
544                 } else {
545                         /* send simple acknowledge packet in */
546                         send_ack (n);
547                         /* quit if end of transmission */
548                         if (k_state == BREAK_TYPE)
549                                 done = 1;
550                 }
551                 ++z;
552         }
553         return ((ulong) os_data_addr - (ulong) bin_start_address);
554 }