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, int len, bool pad)
62 unsigned short crc = 0;
65 crc = (crc << 8) ^ crctab[(crc >> 8) ^ *p++];
66 if (pad && len2 < 128) {
69 crc = (crc << 8) ^ crctab[(crc >> 8) ^ 0x1a];
75 /* Start up the transmit process. Reset state variables. Wait for receiver to
76 send NAK or CRC request. */
87 for (i = 1; i <= 10; i++)
100 /* fprintf_unfiltered (gdb_stderr, "xmodem_init_xfer: Got unexpected character %c (0%o)\n", c, c); */
102 case CANCEL: /* target aborted load */
103 /* fprintf_unfiltered (gdb_stderr, "Got a CANCEL from the target.\n"); */
108 /*error ("xmodem_init_xfer: Too many unexpected characters.");*/
112 /* Take 128 bytes of data and make a packet out of it.
114 * Each packet looks like this:
115 * +-----+-------+-------+------+-----+
116 * | SOH | Seq1. | Seq2. | data | SUM |
117 * +-----+-------+-------+------+-----+
119 * Seq1 = The sequence number.
120 * Seq2 = The complement of the sequence number.
121 * Data = A 128 bytes of data.
122 * SUM = Add the contents of the 128 bytes and use the low-order
123 * 8 bits of the result.
125 * send_xmodem_packet fills in the XMODEM fields of PACKET and sends it to the
126 * remote system. PACKET must be XMODEM_PACKETSIZE bytes long. The data must
127 * start 3 bytes after the beginning of the packet to leave room for the
128 * XMODEM header. LEN is the length of the data portion of the packet (and
129 * must be <= 128 bytes). If it is < 128 bytes, ^Z padding will be added.
133 xmodem_send_packet (const unsigned char *packet, int len)
140 if (len > XMODEM_DATASIZE) {
145 for (retries = 3; retries >= 0; retries--) {
147 /* send the packet header */
148 send_string("\x01"); /* SOH */
149 p = blknum; serial_write(&p, 1);
150 p = ~blknum; serial_write(&p, 1);
152 serial_write(packet, len); /* Send data bytes */
155 ptr = (char*)(packet+len);
156 end = (char*)(packet+128);
157 for (; ptr < end; ptr++) {
158 send_string("\x1A"); /* pad with ^Z */
161 /* Add ^Z padding if packet < 128 (or 1024) bytes */
165 crc = docrc ((unsigned char*)packet, len, 1);
175 for (i = 0; i < len; i++)
193 /* error ("xmodem_send_packet: Transfer aborted by receiver."); */
197 /*fprintf_unfiltered (gdb_stderr, "xmodem_send_packet: Got unexpected character %c (0%o)\n", c, c);*/
198 /* mtearle is evil */
204 //serial_write ("\004", 1); /* Send an EOT */
206 /* error ("xmodem_send_packet: Excessive retries."); */
214 /* Finish off the transfer. Send out the EOT, and wait for an ACK. */
217 xmodem_finish_xfer ()
221 for (retries = 10; retries >= 0; retries--)
225 serial_write ("\004", 1); /* Send an EOT */
236 /* error ("xmodem_finish_xfer: Transfer aborted by receiver."); */
238 /* fprintf_unfiltered (gdb_stderr, "xmodem_send_packet: Got unexpected character %c (0%o)\n", c, c); */
245 /* error ("xmodem_finish_xfer: Excessive retries."); */