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. */
25 /* These definitions are for xmodem protocol. */
34 static int blknum; /* XMODEM block number */
35 static int crcflag; /* Sez we are using CRC's instead of cksums */
38 readchar (int timeout)
42 c = serial_readchar (timeout);
47 if (c == SERIAL_TIMEOUT)
49 /*error ("Timeout reading from remote system.")*/;
54 #define CRC16 0x1021 /* Generator polynomial (X^16 + X^12 + X^5 + 1) */
57 /* Call this to init the fast CRC-16 calculation table. */
59 static short crctab(u8 val) {
65 for (i = 0; i < 8; ++i)
76 /* Calculate a CRC-16 for the LEN byte message pointed at by P. */
77 /* Pads with ^Z if necessary */
80 docrc (unsigned char *p, int len)
83 unsigned short crc = 0;
86 crc = (crc << 8) ^ crctab((crc >> 8) ^ *p++);
90 crc = (crc << 8) ^ crctab((crc >> 8) ^ 0x1a);
96 /* Start up the transmit process. Reset state variables. Wait for receiver to
97 send NAK or CRC request. */
108 for (i = 1; i <= 10; i++)
121 /* fprintf_unfiltered (gdb_stderr, "xmodem_init_xfer: Got unexpected character %c (0%o)\n", c, c); */
123 case CANCEL: /* target aborted load */
124 /* fprintf_unfiltered (gdb_stderr, "Got a CANCEL from the target.\n"); */
129 /*error ("xmodem_init_xfer: Too many unexpected characters.");*/
133 /* Take 128 bytes of data and make a packet out of it.
135 * Each packet looks like this:
136 * +-----+-------+-------+------+-----+
137 * | SOH | Seq1. | Seq2. | data | SUM |
138 * +-----+-------+-------+------+-----+
140 * Seq1 = The sequence number.
141 * Seq2 = The complement of the sequence number.
142 * Data = A 128 bytes of data.
143 * SUM = Add the contents of the 128 bytes and use the low-order
144 * 8 bits of the result.
146 * send_xmodem_packet fills in the XMODEM fields of PACKET and sends it to the
147 * remote system. PACKET must be XMODEM_PACKETSIZE bytes long. The data must
148 * start 3 bytes after the beginning of the packet to leave room for the
149 * XMODEM header. LEN is the length of the data portion of the packet (and
150 * must be <= 128 bytes). If it is < 128 bytes, ^Z padding will be added.
154 xmodem_send_packet (const unsigned char *packet, int len)
161 if (len > XMODEM_DATASIZE) {
166 for (retries = 3; retries >= 0; retries--) {
168 /* send the packet header */
169 send_string("\x01"); /* SOH */
170 p = blknum; serial_write(&p, 1);
171 p = ~blknum; serial_write(&p, 1);
173 serial_write(packet, len); /* Send data bytes */
176 ptr = (char*)(packet+len);
177 end = (char*)(packet+128);
178 for (; ptr < end; ptr++) {
179 send_string("\x1A"); /* pad with ^Z */
182 /* Add ^Z padding if packet < 128 (or 1024) bytes */
186 crc = docrc ((unsigned char*)packet, len);
196 for (i = 0; i < len; i++)
214 /* error ("xmodem_send_packet: Transfer aborted by receiver."); */
218 /*fprintf_unfiltered (gdb_stderr, "xmodem_send_packet: Got unexpected character %c (0%o)\n", c, c);*/
219 /* mtearle is evil */
225 //serial_write ("\004", 1); /* Send an EOT */
227 /* error ("xmodem_send_packet: Excessive retries."); */
235 /* Finish off the transfer. Send out the EOT, and wait for an ACK. */
238 xmodem_finish_xfer ()
242 for (retries = 10; retries >= 0; retries--)
246 serial_write ("\004", 1); /* Send an EOT */
257 /* error ("xmodem_finish_xfer: Transfer aborted by receiver."); */
259 /* fprintf_unfiltered (gdb_stderr, "xmodem_send_packet: Got unexpected character %c (0%o)\n", c, c); */
266 /* error ("xmodem_finish_xfer: Excessive retries."); */