1 /* XMODEM support for GDB, the GNU debugger.
2 Copyright 1995, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 /* These definitions are for xmodem protocol. */
35 static int blknum; /* XMODEM block number */
36 static int crcflag; /* Sez we are using CRC's instead of cksums */
39 readchar (int timeout)
43 c = serial_readchar (timeout);
48 if (c == SERIAL_TIMEOUT)
50 /*error ("Timeout reading from remote system.")*/;
55 /* Calculate a CRC-16 for the LEN byte message pointed at by P. */
56 /* Pads with ^Z up to 128 bytes if told to */
59 docrc (unsigned char *p, short len, bool pad, unsigned short crc)
64 crc = (crc << 8) ^ (crctab[(crc >> 8) ^ *p]);
67 if (pad && len2 < 128) {
70 crc = (crc << 8) ^ crctab[(crc >> 8) ^ 0x1a];
76 /* Start up the transmit process. Reset state variables. Wait for receiver to
77 send NAK or CRC request. */
88 for (i = 1; i <= 10; i++)
101 /* fprintf_unfiltered (gdb_stderr, "xmodem_init_xfer: Got unexpected character %c (0%o)\n", c, c); */
103 case CANCEL: /* target aborted load */
104 /* fprintf_unfiltered (gdb_stderr, "Got a CANCEL from the target.\n"); */
109 /*error ("xmodem_init_xfer: Too many unexpected characters.");*/
113 /* Take 128 bytes of data and make a packet out of it.
115 * Each packet looks like this:
116 * +-----+-------+-------+------+-----+
117 * | SOH | Seq1. | Seq2. | data | SUM |
118 * +-----+-------+-------+------+-----+
120 * Seq1 = The sequence number.
121 * Seq2 = The complement of the sequence number.
122 * Data = A 128 bytes of data.
123 * SUM = Add the contents of the 128 bytes and use the low-order
124 * 8 bits of the result.
126 * send_xmodem_packet fills in the XMODEM fields of PACKET and sends it to the
127 * remote system. PACKET must be XMODEM_PACKETSIZE bytes long. The data must
128 * start 3 bytes after the beginning of the packet to leave room for the
129 * XMODEM header. LEN is the length of the data portion of the packet (and
130 * must be <= 128 bytes). If it is < 128 bytes, ^Z padding will be added.
134 xmodem_send_packet (const unsigned char *packet, int len)
141 if (len > XMODEM_DATASIZE) {
146 for (retries = 3; retries >= 0; retries--) {
148 /* send the packet header */
149 send_string("\x01"); /* SOH */
150 p = blknum; serial_write(&p, 1);
151 p = ~blknum; serial_write(&p, 1);
153 serial_write(packet, len); /* Send data bytes */
156 ptr = (char*)(packet+len);
157 end = (char*)(packet+128);
158 for (; ptr < end; ptr++) {
159 send_string("\x1A"); /* pad with ^Z */
162 /* Add ^Z padding if packet < 128 (or 1024) bytes */
166 crc = docrc ((unsigned char*)packet, len, 1, 0);
176 for (i = 0; i < len; i++)
194 /* error ("xmodem_send_packet: Transfer aborted by receiver."); */
198 /*fprintf_unfiltered (gdb_stderr, "xmodem_send_packet: Got unexpected character %c (0%o)\n", c, c);*/
199 /* mtearle is evil */
205 //serial_write ("\004", 1); /* Send an EOT */
207 /* error ("xmodem_send_packet: Excessive retries."); */
215 /* Finish off the transfer. Send out the EOT, and wait for an ACK. */
218 xmodem_finish_xfer ()
222 for (retries = 10; retries >= 0; retries--)
226 serial_write ("\004", 1); /* Send an EOT */
237 /* error ("xmodem_finish_xfer: Transfer aborted by receiver."); */
239 /* fprintf_unfiltered (gdb_stderr, "xmodem_send_packet: Got unexpected character %c (0%o)\n", c, c); */
246 /* error ("xmodem_finish_xfer: Excessive retries."); */