📄 xmodem.cpp
字号:
/*
* vivi/deriver/serialxmodem.c:
* - an implementation of the xmodem protocol from the spec.
*
* Based on bootldr/xmodem.c (C) 2000 Compaq Computer Coporation.
*
* Copyright (C) 2001 MIZI Research, Inc.
*
*
* 1999-01-xx: Edwin Foo <efoo@crl.dec.com>
* - Initial code
*
* 2001-10-04: Janghoon Lyu <nandy@mizi.com>
* - Modified a little bit.
*
* 2002-07-04: Janghoon Lyu <nandy@mizi.com>
* - get_param_value()
2006-05: jkuang BUPT
some bugs are killed & code optimized
UART related are changed by jkuang. 2006-05
Buffer copy is canceled by jkuang, so this program can works very good with fast speed
Windows terminal. 2006-05
*/
#include <stdio.h>
#include <string.h>
// #include "def.h"
// #include "option.h"
// #include "s3c2410.h"
// #include "2410slib.h"
#include "xmodem.h"
#include "uart.h"
/* XMODEM parameters */
#define BLOCK_SIZE_1 128 /* size of transmit blocks , SOH=0x01??block??*/
#define BLOCK_SIZE_2 1024 /* size of transmit blocks , SOH=0x02??block??*/
#define BLOCK_SIZE block_size
#define RETRIES 20 /* maximum number of RETRIES */
static int block_size = BLOCK_SIZE_1;
/* Line control codes */
#define SOH_1 0x01 /* start of header */
#define SOH_2 0x02 /* 1K xmodem start of header */
#define ACK 0x06 /* Acknowledge */
#define NAK 0x15 /* Negative acknowledge */
#define CAN 0x18 /* Cancel */
#define EOT 0x04 /* end of text */
#define GET_BYTE_TIMEOUT 250000//1000000
//#define GET_BYTE_TIMEOUT (1000 * 5) //?
/* global error variable */
char *xmodem_errtxt = NULL;
/* prototypes of helper functions */
static int get_record(char* bufptr);
static int do_getc(vfuncp idler, unsigned long timeout, char *cp);
static int get_byte(char * cp);
enum
{
SAC_SEND_NAK = 0,
SAC_SENT_NAK = 1,
SAC_PAST_START_NAK = 2
};
static volatile int seen_a_char = SAC_SEND_NAK;
static int one_nak = 0;
static unsigned long xmodem_timeout = GET_BYTE_TIMEOUT;
#ifdef XMODEM_DEBUG
char debugbuf[DEBUGBUF_SIZE];
int db_idx = 0;
/*-------------------------------------------------------------------*/
void bufputs(char *s)
{
size_t len = strlen(s) + 1;
if (len + db_idx > sizeof(debugbuf))
len = sizeof(debugbuf) - db_idx;
if (len) {
memcpy(&debugbuf[db_idx], s, len);
db_idx += len;
}
}
/*-------------------------------------------------------------------*/
void reset_debugbuf(void)
{
memset(debugbuf, 0x0, sizeof(debugbuf));
db_idx = 0;
}
#endif
/*-------------------------------------------------------------------*/
__u32 xmodem_receive(char *dldaddr, size_t len)
{
char ochr;
int r, rx_block_num, error_count;
__u32 foffset = 0;
xmodem_errtxt = NULL;
seen_a_char = 0;
//one_nak = get_param_value("xmodem_one_nak", &ret);
one_nak = 0;
//xmodem_timeout = get_param_value("xmodem_initial_timeout", &ret);
xmodem_timeout = GET_BYTE_TIMEOUT/2;
rx_block_num = 1;
error_count = RETRIES;
do{
if ((r = get_record((char*)(dldaddr+foffset))) == (rx_block_num & 255))
{
error_count = RETRIES;
// xmodem_errtxt = "RX PACKET";
rx_block_num++;
foffset += BLOCK_SIZE;
Uart_SendKey(ACK); //jkuang
continue;
}
//EOT or CAN
if((r == -4)||(r == -5))
{
// xmodem_errtxt = "DONE";
{
//some terminal prog send more chars after 'EOT'
char c;
do
{
Uart_SendKey(ACK);
}while(do_getc(NULL, xmodem_timeout, &c) != -1);
}
break;
}
switch (r)
{
case -1: /* TIMEOUT */
xmodem_errtxt = "TIMEOUT";
ochr = NAK;
break;
case -2: /* Bad block */
xmodem_errtxt = "BAD BLOCK#";
/* eat teh rest of the block */
while (get_byte(&ochr) != -1);
ochr = NAK;
break;
case -3: /* Bad checksum */
xmodem_errtxt = "BAD CHKSUM";
ochr = NAK;
break;
default: /* Block out of sequence */
xmodem_errtxt = "WRONG BLK";
ochr = NAK;
}
error_count--;
Uart_SendKey(ochr);
} while ((r > -3) && error_count);
if((!error_count) || (r != -4)) {
return 0; /* indicate failure to caller */
/*printk("x-modem error: %s\n", xmodem_errtxt); */
}
return foffset;
}
/*-------------------------------------------------------------------*/
/*
* Read a record in the XMODEM protocol, return the block number
* (0-255) if successful, or one of the following return codes:
* -1 = Bad byte
* -2 = Bad block number
* -3 = Bad block checksum
* -4 = End of file
* -5 = Canceled by remote
*/
static int get_record(char* bufptr)
{
char c;
int block_num;
int i;
__u32 check_sum;
/* 1st byte */
if (get_byte(&c)) return -1;
switch (c)
{
case SOH_1: /* Receive packet */
BLOCK_SIZE = BLOCK_SIZE_1;
break;
case SOH_2: /* Receive packet */
BLOCK_SIZE = BLOCK_SIZE_2;
break;
case EOT: /* end of file encountered */
return -4;
case CAN: /* cancel protocol */
default:
return -5;
}
/* 2nd byte */
if (get_byte(&c)) return -1;
block_num = c;
/* 3rd byte */
if (get_byte(&c)) return -1;
/* from 4th -> byte before last */
check_sum = 0;
for(i=0; i<BLOCK_SIZE; i++)
{
if (get_byte(&c)) return -1;
*bufptr = c;
check_sum += c;
bufptr++;
}
/* last byte */
if (get_byte(&c)) return -1;
if((check_sum & 0xff) == c)
return block_num;
return -3;
}
/*-------------------------------------------------------------------*/
static int do_getc(vfuncp idler, unsigned long timeout, char *cp)
{
int rxstat;
while((rxstat = Uart_GetKey(cp)) == -1)
{
if(!(timeout--))
break;
if(idler) idler();
}
return rxstat;
}
/*-------------------------------------------------------------------*/
static int get_byte(char *cp)
{
int rc;
again:
/*
* Reads and returns a character from the serial port
* - Times out after delay iterations checking for presence of character
* - Sets *error_p to UART error bits or - on timeout
* - On timeout, sets *error_p to -1 and returns 0
*/
rc = do_getc(NULL, xmodem_timeout, cp);
if (!rc)
{
seen_a_char = SAC_PAST_START_NAK;
return rc;
}
else
{
if (seen_a_char == SAC_SEND_NAK || !one_nak)
{
// bufputs("timeout nak");
Uart_SendKey(NAK); //jkuang
}
if (seen_a_char < SAC_PAST_START_NAK)
{
// bufputs("goto again");
seen_a_char = SAC_SENT_NAK;
//xmodem_timeout = get_param_value("xmodem_timeout", &ret);
xmodem_timeout = GET_BYTE_TIMEOUT;
goto again;
}
}
return (rc);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -