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.
41 * \author Bernie Innocenti <bernie@codewiz.org>
42 * \author Francesco Sacchi <batt@develer.com>
48 #include "cfg/cfg_xmodem.h"
50 #include <cfg/debug.h>
51 // Define log settings for cfg/log.h
52 #define LOG_LEVEL CONFIG_XMODEM_LOG_LEVEL
53 #define LOG_FORMAT CONFIG_XMODEM_LOG_FORMAT
59 #include <string.h> /* for memset() */
62 * \name Protocol control codes
65 #define XM_SOH 0x01 /**< Start Of Header (128-byte block) */
66 #define XM_STX 0x02 /**< Start Of Header (1024-byte block) */
67 #define XM_EOT 0x04 /**< End Of Transmission */
68 #define XM_ACK 0x06 /**< Acknowledge block */
69 #define XM_NAK 0x15 /**< Negative Acknowledge */
70 #define XM_C 0x43 /**< Request CRC-16 transmission */
71 #define XM_CAN 0x18 /**< CANcel transmission */
74 #if CONFIG_XMODEM_1KCRC == 1
75 #define XM_BUFSIZE 1024 /**< 1024 bytes of block buffer */
77 #define XM_BUFSIZE 128 /**< 128 bytes of block buffer */
81 #if CONFIG_XMODEM_RECV
83 * \brief Receive a file using the XModem protocol.
85 * \param ch Channel to use for transfer
86 * \param fd Destination file
88 * \note This function allocates a large amount of stack (\see XM_BUFSIZE).
90 bool xmodem_recv(KFile *ch, KFile *fd)
92 char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
94 int blocknr = 0, last_block_done = 0, retries = 0;
102 LOG_INFO("Starting Transfer...\n");
106 /* Send initial NAK to start transmission */
109 if (XMODEM_CHECK_ABORT)
111 kfile_putc(XM_CAN, ch);
112 kfile_putc(XM_CAN, ch);
113 LOG_INFO("Transfer aborted\n");
118 * Discard incoming input until a timeout occurs, then send
119 * a NAK to the transmitter.
127 LOG_ERR("Retries %d\n", retries);
130 kfile_resync(ch, 200);
133 if (retries >= CONFIG_XMODEM_MAXRETRIES)
135 kfile_putc(XM_CAN, ch);
136 kfile_putc(XM_CAN, ch);
137 LOG_INFO("Transfer aborted\n");
141 /* Transmission start? */
144 if (retries < CONFIG_XMODEM_MAXCRCRETRIES)
146 LOG_INFO("Request Tx (CRC)\n");
147 kfile_putc(XM_C, ch);
151 /* Give up with CRC and fall back to checksum */
153 LOG_INFO("Request Tx (BCC)\n");
154 kfile_putc(XM_NAK, ch);
158 kfile_putc(XM_NAK, ch);
161 switch (kfile_getc(ch))
163 #if XM_BUFSIZE >= 1024
164 case XM_STX: /* Start of header (1024-byte block) */
169 case XM_SOH: /* Start of header (128-byte block) */
171 /* Needed to avoid warning if XM_BUFSIZE < 1024 */
174 /* Get block number */
177 /* Check complemented block number */
178 if ((~c & 0xff) != kfile_getc(ch))
180 LOG_WARN("Bad blk (%d)\n", c);
185 /* Determine which block is being sent */
186 if (c == (blocknr & 0xff))
188 /* Last block repeated */
189 LOG_INFO("Repeat blk %d\n", blocknr);
191 else if (c == ((blocknr + 1) & 0xff))
194 LOG_INFO("Recv blk %d\n", ++blocknr);
199 LOG_WARN("Sync lost (%d/%d)\n", c, blocknr);
204 buf = block_buffer; /* Reset pointer to start of buffer */
207 for (i = 0; i < blocksize; i++)
209 if ((c = kfile_getc(ch)) == EOF)
215 /* Store in buffer */
218 /* Calculate block checksum or CRC */
220 crc = UPDCRC16(c, crc);
228 /* Get the checksum byte or the CRC-16 MSB */
229 if ((c = kfile_getc(ch)) == EOF)
237 crc = UPDCRC16(c, crc);
240 if ((c = kfile_getc(ch)) == EOF)
246 crc = UPDCRC16(c, crc);
250 LOG_ERR("Bad CRC: %04x\n", crc);
255 /* Compare the checksum */
256 else if (c != checksum)
258 LOG_ERR("Bad sum: %04x/%04x\n", checksum, c);
264 * Avoid flushing the same block twice.
265 * This could happen when the sender does not receive our
266 * acknowledge and resends the same block.
268 if (last_block_done < blocknr)
270 /* Call user function to flush the buffer */
271 if (kfile_write(fd, block_buffer, blocksize) == (size_t)blocksize)
273 /* Acknowledge block and clear error counter */
274 kfile_putc(XM_ACK, ch);
276 last_block_done = blocknr;
280 /* User callback failed: abort transfer immediately */
281 retries = CONFIG_XMODEM_MAXRETRIES;
287 case XM_EOT: /* End of transmission */
288 kfile_putc(XM_ACK, ch);
289 LOG_INFO("Transfer completed\n");
292 case EOF: /* Timeout or serial error */
297 LOG_INFO("Skipping garbage\n");
306 #if CONFIG_XMODEM_SEND
308 * \brief Transmit some data using the XModem protocol.
310 * \param ch Channel to use for transfer
311 * \param fd Source file
313 * \note This function allocates a large amount of stack for
314 * the XModem transfer buffer (\see XM_BUFSIZE).
316 bool xmodem_send(KFile *ch, KFile *fd)
318 char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
320 int blocknr = 1, retries = 0, c, i;
321 bool proceed, usecrc = false;
326 * Reading a block can be very slow, so we read the first block early
327 * to avoid receiving double XM_C char.
328 * This could happen if we check for XM_C and then read the block, giving
329 * the receiving device time to send another XM_C char misinterpretating
332 size = kfile_read(fd, block_buffer, XM_BUFSIZE);
335 LOG_INFO("Wait remote host\n");
342 if (XMODEM_CHECK_ABORT)
345 switch (c = kfile_getc(ch))
348 LOG_INFO("Resend blk %d\n", blocknr);
355 LOG_INFO("Tx start (CRC)\n");
360 LOG_INFO("Tx start (BCC)\n");
367 /* End of transfer? */
371 /* Call user function to read in one block */
372 size = kfile_read(fd, block_buffer, XM_BUFSIZE);
373 LOG_INFO("Send blk %d\n", blocknr);
382 LOG_INFO("Retries %d\n", retries);
383 if (retries <= CONFIG_XMODEM_MAXRETRIES)
385 /* falling through! */
388 LOG_INFO("Transfer aborted\n");
392 LOG_INFO("Skipping garbage\n");
400 kfile_putc(XM_EOT, ch);
404 /* Pad block with 0xFF if it's partially full */
405 memset(block_buffer + size, 0xFF, XM_BUFSIZE - size);
407 /* Send block header (STX, blocknr, ~blocknr) */
408 #if XM_BUFSIZE == 128
409 kfile_putc(XM_SOH, ch);
411 kfile_putc(XM_STX, ch);
413 kfile_putc(blocknr & 0xFF, ch);
414 kfile_putc(~blocknr & 0xFF, ch);
416 /* Send block and compute its CRC/checksum */
419 for (i = 0; i < XM_BUFSIZE; i++)
421 kfile_putc(block_buffer[i], ch);
422 crc = UPDCRC16(block_buffer[i], crc);
423 sum += block_buffer[i];
426 /* Send CRC/Checksum */
429 kfile_putc(crc >> 8, ch);
430 kfile_putc(crc & 0xFF, ch);