4 * This file is part of BeRTOS.
6 * Bertos 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2004, 2005, 2006, 2007 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 1999, 2001 Bernardo Innocenti <bernie@develer.com>
33 * \brief X-Modem serial transmission protocol (implementation)
35 * Supports the CRC-16 and 1K-blocks variants of the standard.
36 * \see ymodem.txt for the protocol description.
38 * \todo Break xmodem_send() and xmodem_recv() in smaller functions.
41 * \author Bernardo Innocenti <bernie@develer.com>
42 * \author Francesco Sacchi <batt@develer.com>
48 #include <appconfig.h>
49 #include <string.h> /* for memset() */
52 #include <cfg/debug.h>
58 * \name Protocol control codes
61 #define XM_SOH 0x01 /**< Start Of Header (128-byte block) */
62 #define XM_STX 0x02 /**< Start Of Header (1024-byte block) */
63 #define XM_EOT 0x04 /**< End Of Transmission */
64 #define XM_ACK 0x06 /**< Acknowledge block */
65 #define XM_NAK 0x15 /**< Negative Acknowledge */
66 #define XM_C 0x43 /**< Request CRC-16 transmission */
67 #define XM_CAN 0x18 /**< CANcel transmission */
70 #define XM_MAXRETRIES 15 /**< Max retries before giving up */
71 #define XM_MAXCRCRETRIES 7 /**< Max retries before switching to BCC */
73 #if CONFIG_XMODEM_1KCRC == 1
74 #define XM_BUFSIZE 1024 /**< 1024 bytes of block buffer */
76 #define XM_BUFSIZE 128 /**< 128 bytes of block buffer */
80 #if CONFIG_XMODEM_RECV
82 * \brief Receive a file using the XModem protocol.
84 * \param port Serial port to use for transfer
85 * \param fd Destination file
87 * \note This function allocates a large amount of stack (\see XM_BUFSIZE).
89 bool xmodem_recv(struct KFileSerial *port, KFile *fd)
91 char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
93 int blocknr = 0, last_block_done = 0, retries = 0;
101 XMODEM_PROGRESS("Starting Transfer...\n");
105 /* Send initial NAK to start transmission */
108 if (XMODEM_CHECK_ABORT)
110 kfile_putc(XM_CAN, &port->fd);
111 kfile_putc(XM_CAN, &port->fd);
112 XMODEM_PROGRESS("Transfer aborted\n");
117 * Discard incoming input until a timeout occurs, then send
118 * a NAK to the transmitter.
124 if (kfile_error(&port->fd))
125 XMODEM_PROGRESS("Retries %d\n", retries);
127 ser_resync(port, 200);
130 if (retries >= XM_MAXRETRIES)
132 kfile_putc(XM_CAN, &port->fd);
133 kfile_putc(XM_CAN, &port->fd);
134 XMODEM_PROGRESS("Transfer aborted\n");
138 /* Transmission start? */
141 if (retries < XM_MAXCRCRETRIES)
143 XMODEM_PROGRESS("Request Tx (CRC)\n");
144 kfile_putc(XM_C, &port->fd);
148 /* Give up with CRC and fall back to checksum */
150 XMODEM_PROGRESS("Request Tx (BCC)\n");
151 kfile_putc(XM_NAK, &port->fd);
155 kfile_putc(XM_NAK, &port->fd);
158 switch (kfile_getc(&port->fd))
160 #if XM_BUFSIZE >= 1024
161 case XM_STX: /* Start of header (1024-byte block) */
166 case XM_SOH: /* Start of header (128-byte block) */
168 /* Needed to avoid warning if XM_BUFSIZE < 1024 */
171 /* Get block number */
172 c = kfile_getc(&port->fd);
174 /* Check complemented block number */
175 if ((~c & 0xff) != kfile_getc(&port->fd))
177 XMODEM_PROGRESS("Bad blk (%d)\n", c);
182 /* Determine which block is being sent */
183 if (c == (blocknr & 0xff))
184 /* Last block repeated */
185 XMODEM_PROGRESS("Repeat blk %d\n", blocknr);
186 else if (c == ((blocknr + 1) & 0xff))
188 XMODEM_PROGRESS("Recv blk %d\n", ++blocknr);
192 XMODEM_PROGRESS("Sync lost (%d/%d)\n", c, blocknr);
197 buf = block_buffer; /* Reset pointer to start of buffer */
200 for (i = 0; i < blocksize; i++)
202 if ((c = kfile_getc(&port->fd)) == EOF)
208 /* Store in buffer */
211 /* Calculate block checksum or CRC */
213 crc = UPDCRC16(c, crc);
221 /* Get the checksum byte or the CRC-16 MSB */
222 if ((c = kfile_getc(&port->fd)) == EOF)
230 crc = UPDCRC16(c, crc);
233 if ((c = kfile_getc(&port->fd)) == EOF)
239 crc = UPDCRC16(c, crc);
243 XMODEM_PROGRESS("Bad CRC: %04x\n", crc);
248 /* Compare the checksum */
249 else if (c != checksum)
251 XMODEM_PROGRESS("Bad sum: %04x/%04x\n", checksum, c);
257 * Avoid flushing the same block twice.
258 * This could happen when the sender does not receive our
259 * acknowledge and resends the same block.
261 if (last_block_done < blocknr)
263 /* Call user function to flush the buffer */
264 if (kfile_write(fd, block_buffer, blocksize))
266 /* Acknowledge block and clear error counter */
267 kfile_putc(XM_ACK, &port->fd);
269 last_block_done = blocknr;
273 /* User callback failed: abort transfer immediately */
274 retries = XM_MAXRETRIES;
280 case XM_EOT: /* End of transmission */
281 kfile_putchar(XM_ACK, &port->fd);
282 XMODEM_PROGRESS("Transfer completed\n");
285 case EOF: /* Timeout or serial error */
290 XMODEM_PROGRESS("Skipping garbage\n");
299 #if CONFIG_XMODEM_SEND
301 * \brief Transmit some data using the XModem protocol.
303 * \param port Serial port to use for transfer
304 * \param fd Source file
306 * \note This function allocates a large amount of stack for
307 * the XModem transfer buffer (\see XM_BUFSIZE).
309 bool xmodem_send(struct KFileSerial *port, KFile *fd)
311 char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
313 int blocknr = 1, retries = 0, c, i;
314 bool proceed, usecrc = false;
319 * Reading a block can be very slow, so we read the first block early
320 * to avoid receiving double XM_C char.
321 * This could happen if we check for XM_C and then read the block, giving
322 * the receiving device time to send another XM_C char misinterpretating
325 size = kfile_read(fd, block_buffer, XM_BUFSIZE);
327 kfile_clearerr(&port->fd);
329 XMODEM_PROGRESS("Wait remote host\n");
336 if (XMODEM_CHECK_ABORT)
339 switch (c = kfile_getc(&port->fd))
342 XMODEM_PROGRESS("Resend blk %d\n", blocknr);
349 XMODEM_PROGRESS("Tx start (CRC)\n");
353 XMODEM_PROGRESS("Tx start (BCC)\n");
359 /* End of transfer? */
363 /* Call user function to read in one block */
364 size = kfile_read(fd, block_buffer, XM_BUFSIZE);
365 XMODEM_PROGRESS("Send blk %d\n", blocknr);
372 kfile_clearerr(&port->fd);
374 XMODEM_PROGRESS("Retries %d\n", retries);
375 if (retries <= XM_MAXRETRIES)
377 /* falling through! */
380 XMODEM_PROGRESS("Transfer aborted\n");
384 XMODEM_PROGRESS("Skipping garbage\n");
392 kfile_putc(XM_EOT, &port->fd);
396 /* Pad block with 0xFF if it's partially full */
397 memset(block_buffer + size, 0xFF, XM_BUFSIZE - size);
399 /* Send block header (STX, blocknr, ~blocknr) */
400 #if XM_BUFSIZE == 128
401 kfile_putc(XM_SOH, &port->fd);
403 kfile_putc(XM_STX, &port->fd);
405 kfile_putc(blocknr & 0xFF, &port->fd);
406 kfile_putc(~blocknr & 0xFF, &port->fd);
408 /* Send block and compute its CRC/checksum */
411 for (i = 0; i < XM_BUFSIZE; i++)
413 kfile_putc(block_buffer[i], &port->fd);
414 crc = UPDCRC16(block_buffer[i], crc);
415 sum += block_buffer[i];
418 /* Send CRC/Checksum */
421 crc = UPDCRC16(0, crc);
422 crc = UPDCRC16(0, crc);
423 ser_putchar(crc >> 8, port);
424 ser_putchar(crc & 0xFF, port);
427 kfile_putc(sum, &port->fd);