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 Bernie Innocenti <bernie@codewiz.org>
34 * \brief X-Modem serial transmission protocol (implementation)
36 * Supports the CRC-16 and 1K-blocks variants of the standard.
37 * \see ymodem.txt for the protocol description.
39 * \todo Break xmodem_send() and xmodem_recv() in smaller functions.
43 * \author Bernie Innocenti <bernie@codewiz.org>
44 * \author Francesco Sacchi <batt@develer.com>
50 #include "cfg/cfg_xmodem.h"
51 #include <cfg/debug.h>
55 #include <string.h> /* for memset() */
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 #if CONFIG_XMODEM_1KCRC == 1
71 #define XM_BUFSIZE 1024 /**< 1024 bytes of block buffer */
73 #define XM_BUFSIZE 128 /**< 128 bytes of block buffer */
77 #if CONFIG_XMODEM_RECV
79 * \brief Receive a file using the XModem protocol.
81 * \param ch Channel to use for transfer
82 * \param fd Destination file
84 * \note This function allocates a large amount of stack (\see XM_BUFSIZE).
86 bool xmodem_recv(KFile *ch, KFile *fd)
88 char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
90 int blocknr = 0, last_block_done = 0, retries = 0;
98 XMODEM_PROGRESS("Starting Transfer...\n");
102 /* Send initial NAK to start transmission */
105 if (XMODEM_CHECK_ABORT)
107 kfile_putc(XM_CAN, ch);
108 kfile_putc(XM_CAN, ch);
109 XMODEM_PROGRESS("Transfer aborted\n");
114 * Discard incoming input until a timeout occurs, then send
115 * a NAK to the transmitter.
122 XMODEM_PROGRESS("Retries %d\n", retries);
124 kfile_resync(ch, 200);
127 if (retries >= CONFIG_XMODEM_MAXRETRIES)
129 kfile_putc(XM_CAN, ch);
130 kfile_putc(XM_CAN, ch);
131 XMODEM_PROGRESS("Transfer aborted\n");
135 /* Transmission start? */
138 if (retries < CONFIG_XMODEM_MAXCRCRETRIES)
140 XMODEM_PROGRESS("Request Tx (CRC)\n");
141 kfile_putc(XM_C, ch);
145 /* Give up with CRC and fall back to checksum */
147 XMODEM_PROGRESS("Request Tx (BCC)\n");
148 kfile_putc(XM_NAK, ch);
152 kfile_putc(XM_NAK, ch);
155 switch (kfile_getc(ch))
157 #if XM_BUFSIZE >= 1024
158 case XM_STX: /* Start of header (1024-byte block) */
163 case XM_SOH: /* Start of header (128-byte block) */
165 /* Needed to avoid warning if XM_BUFSIZE < 1024 */
168 /* Get block number */
171 /* Check complemented block number */
172 if ((~c & 0xff) != kfile_getc(ch))
174 XMODEM_PROGRESS("Bad blk (%d)\n", c);
179 /* Determine which block is being sent */
180 if (c == (blocknr & 0xff))
181 /* Last block repeated */
182 XMODEM_PROGRESS("Repeat blk %d\n", blocknr);
183 else if (c == ((blocknr + 1) & 0xff))
185 XMODEM_PROGRESS("Recv blk %d\n", ++blocknr);
189 XMODEM_PROGRESS("Sync lost (%d/%d)\n", c, blocknr);
194 buf = block_buffer; /* Reset pointer to start of buffer */
197 for (i = 0; i < blocksize; i++)
199 if ((c = kfile_getc(ch)) == EOF)
205 /* Store in buffer */
208 /* Calculate block checksum or CRC */
210 crc = UPDCRC16(c, crc);
218 /* Get the checksum byte or the CRC-16 MSB */
219 if ((c = kfile_getc(ch)) == EOF)
227 crc = UPDCRC16(c, crc);
230 if ((c = kfile_getc(ch)) == EOF)
236 crc = UPDCRC16(c, crc);
240 XMODEM_PROGRESS("Bad CRC: %04x\n", crc);
245 /* Compare the checksum */
246 else if (c != checksum)
248 XMODEM_PROGRESS("Bad sum: %04x/%04x\n", checksum, c);
254 * Avoid flushing the same block twice.
255 * This could happen when the sender does not receive our
256 * acknowledge and resends the same block.
258 if (last_block_done < blocknr)
260 /* Call user function to flush the buffer */
261 if (kfile_write(fd, block_buffer, blocksize))
263 /* Acknowledge block and clear error counter */
264 kfile_putc(XM_ACK, ch);
266 last_block_done = blocknr;
270 /* User callback failed: abort transfer immediately */
271 retries = CONFIG_XMODEM_MAXRETRIES;
277 case XM_EOT: /* End of transmission */
278 kfile_putc(XM_ACK, ch);
279 XMODEM_PROGRESS("Transfer completed\n");
282 case EOF: /* Timeout or serial error */
287 XMODEM_PROGRESS("Skipping garbage\n");
296 #if CONFIG_XMODEM_SEND
298 * \brief Transmit some data using the XModem protocol.
300 * \param ch Channel to use for transfer
301 * \param fd Source file
303 * \note This function allocates a large amount of stack for
304 * the XModem transfer buffer (\see XM_BUFSIZE).
306 bool xmodem_send(KFile *ch, KFile *fd)
308 char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
310 int blocknr = 1, retries = 0, c, i;
311 bool proceed, usecrc = false;
316 * Reading a block can be very slow, so we read the first block early
317 * to avoid receiving double XM_C char.
318 * This could happen if we check for XM_C and then read the block, giving
319 * the receiving device time to send another XM_C char misinterpretating
322 size = kfile_read(fd, block_buffer, XM_BUFSIZE);
325 XMODEM_PROGRESS("Wait remote host\n");
332 if (XMODEM_CHECK_ABORT)
335 switch (c = kfile_getc(ch))
338 XMODEM_PROGRESS("Resend blk %d\n", blocknr);
345 XMODEM_PROGRESS("Tx start (CRC)\n");
349 XMODEM_PROGRESS("Tx start (BCC)\n");
355 /* End of transfer? */
359 /* Call user function to read in one block */
360 size = kfile_read(fd, block_buffer, XM_BUFSIZE);
361 XMODEM_PROGRESS("Send blk %d\n", blocknr);
370 XMODEM_PROGRESS("Retries %d\n", retries);
371 if (retries <= CONFIG_XMODEM_MAXRETRIES)
373 /* falling through! */
376 XMODEM_PROGRESS("Transfer aborted\n");
380 XMODEM_PROGRESS("Skipping garbage\n");
388 kfile_putc(XM_EOT, ch);
392 /* Pad block with 0xFF if it's partially full */
393 memset(block_buffer + size, 0xFF, XM_BUFSIZE - size);
395 /* Send block header (STX, blocknr, ~blocknr) */
396 #if XM_BUFSIZE == 128
397 kfile_putc(XM_SOH, ch);
399 kfile_putc(XM_STX, ch);
401 kfile_putc(blocknr & 0xFF, ch);
402 kfile_putc(~blocknr & 0xFF, ch);
404 /* Send block and compute its CRC/checksum */
407 for (i = 0; i < XM_BUFSIZE; i++)
409 kfile_putc(block_buffer[i], ch);
410 crc = UPDCRC16(block_buffer[i], crc);
411 sum += block_buffer[i];
414 /* Send CRC/Checksum */
417 crc = UPDCRC16(0, crc);
418 crc = UPDCRC16(0, crc);
419 kfile_putc(crc >> 8, ch);
420 kfile_putc(crc & 0xFF, ch);