2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
25 * Serial up- and download support
29 #define putc serial_putc
30 #define tstc serial_tstc
32 /*******************************************************
34 * Description: spinning delay to use before udelay works
35 ******************************************************/
36 static inline void delay(unsigned long loops)
38 __asm__ volatile ("1:\n" "subs %0, %1, #1\n"
39 "bne 1b":"=r" (loops):"0"(loops));
44 void udelay(unsigned long us)
46 delay(us * 200); /* approximate */
50 extern void * memcpy(void * dest,const void *src,size_t count);
52 void * memcpy(void * dest,const void *src,size_t count)
54 char *tmp = (char *) dest, *s = (char *) src;
63 /* -------------------------------------------------------------------- */
67 #define START_CHAR 0x01
76 #define BREAK_TYPE 'B'
77 #define tochar(x) ((char) (((x) + SPACE) & 0xff))
78 #define untochar(x) ((int) (((x) - SPACE) & 0xff))
80 extern int os_data_count;
81 extern int os_data_header[8];
83 static void set_kerm_bin_mode(unsigned long *);
84 static int k_recv(void);
85 static ulong load_serial_bin (ulong offset);
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 */
93 int do_load_serial_bin (ulong offset, int baudrate)
98 printf ("## Ready for binary (kermit) download "
99 "to 0x%08lX at %d bps...\n",
102 addr = load_serial_bin (offset);
104 printf ("## Binary (kermit) download aborted\n");
107 printf ("## Start Addr = 0x%08lX\n", addr);
114 static ulong load_serial_bin (ulong offset)
118 set_kerm_bin_mode ((ulong *) offset);
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)
126 for (i=0; i<100; ++i) {
133 printf("## Total Size = 0x%08x = %d Bytes\n", size, size);
140 int count = his_pad_count;
146 /* converts escaped kermit char to binary char */
147 char ktrans (char in)
149 if ((in & 0x60) == 0x40) {
150 return (char) (in & ~0x40);
151 } else if ((in & 0x7f) == 0x3f) {
152 return (char) (in | 0x40);
157 int chk1 (char *buffer)
164 return (int) ((total + ((total >> 6) & 0x03)) & 0x3f);
167 void s1_sendpacket (char *packet)
176 void send_ack (int n)
183 a_b[4] = tochar (chk1 (&a_b[1]));
189 void send_nack (int n)
196 a_b[4] = tochar (chk1 (&a_b[1]));
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
206 if image is binary, no header is stored in os_data_header.
208 void (*os_data_init) (void);
209 void (*os_data_char) (char new_char);
210 static int os_data_state, os_data_state_saved;
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)
220 os_data_addr = bin_start_address;
222 static void os_data_save (void)
224 os_data_state_saved = os_data_state;
225 os_data_count_saved = os_data_count;
226 os_data_addr_saved = os_data_addr;
228 static void os_data_restore (void)
230 os_data_state = os_data_state_saved;
231 os_data_count = os_data_count_saved;
232 os_data_addr = os_data_addr_saved;
234 static void bin_data_char (char new_char)
236 switch (os_data_state) {
238 *os_data_addr++ = new_char;
243 static void set_kerm_bin_mode (unsigned long *addr)
245 bin_start_address = (char *) addr;
246 os_data_init = bin_data_init;
247 os_data_char = bin_data_char;
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)
258 void k_data_save (void)
260 k_data_escape_saved = k_data_escape;
263 void k_data_restore (void)
265 k_data_escape = k_data_escape_saved;
268 void k_data_char (char new_char)
271 /* last char was escape - translate this character */
272 os_data_char (ktrans (new_char));
275 if (new_char == his_quote) {
276 /* this char is escape - remember */
279 /* otherwise send this char as-is */
280 os_data_char (new_char);
285 #define SEND_DATA_SIZE 20
286 char send_parms[SEND_DATA_SIZE];
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)
296 /* initialize some protocol parameters */
297 his_eol = END_CHAR; /* default end of line character */
300 his_quote = K_ESCAPE;
302 /* ignore last character if it filled the buffer */
303 if (send_ptr == &send_parms[SEND_DATA_SIZE - 1])
305 bytes = send_ptr - send_parms; /* how many bytes we'll process */
309 /* handle MAXL - max length */
310 /* ignore what he says - most I'll take (here) is 94 */
311 a_b[++length] = tochar (94);
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);
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);
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 */
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);
337 /* handle QCTL - quote control char he'll use */
338 /* remember what he says - I'll use '#' */
339 his_quote = send_parms[5];
343 /* handle QBIN - 8-th bit prefixing */
344 /* ignore what he says - I refuse */
348 /* handle CHKT - the clock check type */
349 /* ignore what he says - I do type 1 (for now) */
353 /* handle REPT - the repeat prefix */
354 /* ignore what he says - I refuse (for now) */
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 */
367 a_b[1] = tochar (length);
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';
377 /* k_recv receives a OS Open image file over kermit line */
378 static int k_recv (void)
381 char k_state, k_state_saved;
389 /* initialize some protocol parameters */
390 his_eol = END_CHAR; /* default end of line character */
393 his_quote = K_ESCAPE;
395 /* initialize the k_recv and k_data state machine */
399 k_state_saved = k_state;
401 n = 0; /* just to get rid of a warning */
404 /* expect this "type" sequence (but don't check):
409 B: break transmission
412 /* enter main loop */
414 /* set the send packet pointer to begining of send packet parms */
415 send_ptr = send_parms;
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.
424 /* OLD CODE, Prior to checking sequence numbers */
425 /* first have all state machines save current states */
426 k_state_saved = k_state;
431 /* wait for the starting character or ^C */
434 case START_CHAR: /* start packet */
436 case ETX_CHAR: /* ^C waiting for packet */
443 /* get length of packet */
446 if ((new_char & 0xE0) == 0)
448 sum += new_char & 0xff;
449 length = untochar (new_char);
450 /* get sequence number */
452 if ((new_char & 0xE0) == 0)
454 sum += new_char & 0xff;
455 n = untochar (new_char);
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.
466 /* same sequence number, restore the previous state */
467 k_state = k_state_saved;
470 /* new sequence number, checkpoint the download */
472 k_state_saved = k_state;
477 /* get packet type */
479 if ((new_char & 0xE0) == 0)
481 sum += new_char & 0xff;
484 /* check for extended length */
486 /* (length byte was 0, decremented twice) */
487 /* get the two length bytes */
489 if ((new_char & 0xE0) == 0)
491 sum += new_char & 0xff;
492 len_hi = untochar (new_char);
494 if ((new_char & 0xE0) == 0)
496 sum += new_char & 0xff;
497 len_lo = untochar (new_char);
498 length = len_hi * 95 + len_lo;
499 /* check header checksum */
501 if ((new_char & 0xE0) == 0)
503 if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
505 sum += new_char & 0xff;
506 /* --length; */ /* new length includes only data and block check to come */
508 /* bring in rest of packet */
511 if ((new_char & 0xE0) == 0)
513 sum += new_char & 0xff;
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])
526 /* get and validate checksum character */
528 if ((new_char & 0xE0) == 0)
530 if (new_char != tochar ((sum + ((sum >> 6) & 0x03)) & 0x3f))
534 if (new_char != END_CHAR) {
536 /* restore state machines */
537 k_state = k_state_saved;
539 /* send a negative acknowledge packet in */
541 } else if (k_state == SEND_TYPE) {
542 /* crack the protocol parms, build an appropriate ack packet */
543 handle_send_packet (n);
545 /* send simple acknowledge packet in */
547 /* quit if end of transmission */
548 if (k_state == BREAK_TYPE)
553 return ((ulong) os_data_addr - (ulong) bin_start_address);