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>
57 #include <string.h> /* for memset() */
60 * \name Protocol control codes
63 #define XM_SOH 0x01 /**< Start Of Header (128-byte block) */
64 #define XM_STX 0x02 /**< Start Of Header (1024-byte block) */
65 #define XM_EOT 0x04 /**< End Of Transmission */
66 #define XM_ACK 0x06 /**< Acknowledge block */
67 #define XM_NAK 0x15 /**< Negative Acknowledge */
68 #define XM_C 0x43 /**< Request CRC-16 transmission */
69 #define XM_CAN 0x18 /**< CANcel transmission */
72 #if CONFIG_XMODEM_1KCRC == 1
73 #define XM_BUFSIZE 1024 /**< 1024 bytes of block buffer */
75 #define XM_BUFSIZE 128 /**< 128 bytes of block buffer */
79 #if CONFIG_XMODEM_RECV
81 * \brief Receive a file using the XModem protocol.
83 * \param KFile channel to use for transfer
84 * \param fd Destination file
86 * \note This function allocates a large amount of stack (\see XM_BUFSIZE).
88 bool xmodem_recv(KFile *ch, KFile *fd)
90 char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
92 int blocknr = 0, last_block_done = 0, retries = 0;
100 XMODEM_PROGRESS("Starting Transfer...\n");
104 /* Send initial NAK to start transmission */
107 if (XMODEM_CHECK_ABORT)
109 kfile_putc(XM_CAN, ch);
110 kfile_putc(XM_CAN, ch);
111 XMODEM_PROGRESS("Transfer aborted\n");
116 * Discard incoming input until a timeout occurs, then send
117 * a NAK to the transmitter.
124 XMODEM_PROGRESS("Retries %d\n", retries);
126 kfile_resync(ch, 200);
129 if (retries >= CONFIG_XMODEM_MAXRETRIES)
131 kfile_putc(XM_CAN, ch);
132 kfile_putc(XM_CAN, ch);
133 XMODEM_PROGRESS("Transfer aborted\n");
137 /* Transmission start? */
140 if (retries < CONFIG_XMODEM_MAXCRCRETRIES)
142 XMODEM_PROGRESS("Request Tx (CRC)\n");
143 kfile_putc(XM_C, ch);
147 /* Give up with CRC and fall back to checksum */
149 XMODEM_PROGRESS("Request Tx (BCC)\n");
150 kfile_putc(XM_NAK, ch);
154 kfile_putc(XM_NAK, ch);
157 switch (kfile_getc(ch))
159 #if XM_BUFSIZE >= 1024
160 case XM_STX: /* Start of header (1024-byte block) */
165 case XM_SOH: /* Start of header (128-byte block) */
167 /* Needed to avoid warning if XM_BUFSIZE < 1024 */
170 /* Get block number */
173 /* Check complemented block number */
174 if ((~c & 0xff) != kfile_getc(ch))
176 XMODEM_PROGRESS("Bad blk (%d)\n", c);
181 /* Determine which block is being sent */
182 if (c == (blocknr & 0xff))
183 /* Last block repeated */
184 XMODEM_PROGRESS("Repeat blk %d\n", blocknr);
185 else if (c == ((blocknr + 1) & 0xff))
187 XMODEM_PROGRESS("Recv blk %d\n", ++blocknr);
191 XMODEM_PROGRESS("Sync lost (%d/%d)\n", c, blocknr);
196 buf = block_buffer; /* Reset pointer to start of buffer */
199 for (i = 0; i < blocksize; i++)
201 if ((c = kfile_getc(ch)) == EOF)
207 /* Store in buffer */
210 /* Calculate block checksum or CRC */
212 crc = UPDCRC16(c, crc);
220 /* Get the checksum byte or the CRC-16 MSB */
221 if ((c = kfile_getc(ch)) == EOF)
229 crc = UPDCRC16(c, crc);
232 if ((c = kfile_getc(ch)) == EOF)
238 crc = UPDCRC16(c, crc);
242 XMODEM_PROGRESS("Bad CRC: %04x\n", crc);
247 /* Compare the checksum */
248 else if (c != checksum)
250 XMODEM_PROGRESS("Bad sum: %04x/%04x\n", checksum, c);
256 * Avoid flushing the same block twice.
257 * This could happen when the sender does not receive our
258 * acknowledge and resends the same block.
260 if (last_block_done < blocknr)
262 /* Call user function to flush the buffer */
263 if (kfile_write(fd, block_buffer, blocksize))
265 /* Acknowledge block and clear error counter */
266 kfile_putc(XM_ACK, ch);
268 last_block_done = blocknr;
272 /* User callback failed: abort transfer immediately */
273 retries = CONFIG_XMODEM_MAXRETRIES;
279 case XM_EOT: /* End of transmission */
280 kfile_putc(XM_ACK, ch);
281 XMODEM_PROGRESS("Transfer completed\n");
284 case EOF: /* Timeout or serial error */
289 XMODEM_PROGRESS("Skipping garbage\n");
298 #if CONFIG_XMODEM_SEND
300 * \brief Transmit some data using the XModem protocol.
302 * \param KFile channel to use for transfer
303 * \param fd Source file
305 * \note This function allocates a large amount of stack for
306 * the XModem transfer buffer (\see XM_BUFSIZE).
308 bool xmodem_send(KFile *ch, KFile *fd)
310 char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
312 int blocknr = 1, retries = 0, c, i;
313 bool proceed, usecrc = false;
318 * Reading a block can be very slow, so we read the first block early
319 * to avoid receiving double XM_C char.
320 * This could happen if we check for XM_C and then read the block, giving
321 * the receiving device time to send another XM_C char misinterpretating
324 size = kfile_read(fd, block_buffer, XM_BUFSIZE);
327 XMODEM_PROGRESS("Wait remote host\n");
334 if (XMODEM_CHECK_ABORT)
337 switch (c = kfile_getc(ch))
340 XMODEM_PROGRESS("Resend blk %d\n", blocknr);
347 XMODEM_PROGRESS("Tx start (CRC)\n");
351 XMODEM_PROGRESS("Tx start (BCC)\n");
357 /* End of transfer? */
361 /* Call user function to read in one block */
362 size = kfile_read(fd, block_buffer, XM_BUFSIZE);
363 XMODEM_PROGRESS("Send blk %d\n", blocknr);
372 XMODEM_PROGRESS("Retries %d\n", retries);
373 if (retries <= CONFIG_XMODEM_MAXRETRIES)
375 /* falling through! */
378 XMODEM_PROGRESS("Transfer aborted\n");
382 XMODEM_PROGRESS("Skipping garbage\n");
390 kfile_putc(XM_EOT, ch);
394 /* Pad block with 0xFF if it's partially full */
395 memset(block_buffer + size, 0xFF, XM_BUFSIZE - size);
397 /* Send block header (STX, blocknr, ~blocknr) */
398 #if XM_BUFSIZE == 128
399 kfile_putc(XM_SOH, ch);
401 kfile_putc(XM_STX, ch);
403 kfile_putc(blocknr & 0xFF, ch);
404 kfile_putc(~blocknr & 0xFF, ch);
406 /* Send block and compute its CRC/checksum */
409 for (i = 0; i < XM_BUFSIZE; i++)
411 kfile_putc(block_buffer[i], ch);
412 crc = UPDCRC16(block_buffer[i], crc);
413 sum += block_buffer[i];
416 /* Send CRC/Checksum */
419 crc = UPDCRC16(0, crc);
420 crc = UPDCRC16(0, crc);
421 kfile_putc(crc >> 8, ch);
422 kfile_putc(crc & 0xFF, ch);