Ymodem
Ymodem
Ymodem
1. ROSETTA STONE
Here are some definitions which reflect the current vernacular in the
computer media. The attempt here is identify the file transfer protocol
rather than specific programs.
YMODEM refers to the XMODEM/CRC protocol with the throughput and/or batch
transmission enhancements described below.
Since its development half a decade ago, the Ward Christensen modem
protocol has enabled a wide variety of computer systems to interchange
data. There is hardly a communications program that doesn't at least
claim to support this protocol.
+ The short block length caused throughput to suffer when used with
timesharing systems, packet switched networks, satellite circuits,
and buffered (error correcting) modems.
+ Only one file could be sent per command. The file name had to be
given twice, first to the sending program and then again to the
receiving program.
A number of other protocols have been developed over the years, but none
have displaced XMODEM to date:
Chapter 2
__________
Chapter 2
Be aware the article2 DID quote me correctly in terms of the phrases like
"not robust", etc.
ONLY the fact that it was done in 8/77, and that I put it in the public
domain immediately, made it become the standard that it is.
(1) document it; (people call me and say "my product is going to include
it - what can I 'reference'", or "I'm writing a paper on it, what do I put
in the bibliography") and
Consider the PC-NET group back in '77 or so - documenting to beat the band
- THEY had a protocol, but it was "extremely complex", because it tried to
be "all things to all people" - i.e. send binary files on a 7-bit system,
etc. I was not that "benevolent". I (emphasize > I < ) had an 8-bit
UART,
__________
2. Infoworld April 29 p. 16
Chapter 2
so "my protocol was an 8-bit protocol", and I would just say "sorry" to
people who were held back by 7-bit limitations. ...
Also, there is a catchy name - YMODEM. That means to some that it is the
"next thing after XMODEM", and to others that it is the Y(am)MODEM
protocol. I don't want to emphasize that too much - out of fear that
other mfgrs might think it is a "competitive" protocol, rather than an
"unaffiliated" protocol. Chuck is currently selling a much-enhanced
version of his CP/M-80 C program YAM, calling it Professional Yam, and its
for the PC - I'm using it right now. VERY slick! 32K capture buffer,
script, scrolling, previously captured text search, plus built-in commands
for just about everything - directory (sorted every which way), XMODEM,
YMODEM, KERMIT, and ASCII file upload/download, etc. You can program it
to "behave" with most any system - for example when trying a number for
CIS it detects the "busy" string back from the modem and substitutes a
diff phone # into the dialing string and branches back to try it.
__________
4. The file length, time, and file mode are optional. The pathname and
file length may be sent alone if desired.
The XMODEM protocol uses an optional two character CRC-16 instead of the
one character arithmetic checksum used by the original protocol and by
most commercial implementations. CRC-16 guarantees detection of all
single and double bit errors, all errors with an odd number of error
bits, all burst errors of length 16 or less, 99.9969% of all 17-bit error
bursts, and 99.9984 per cent of all possible longer error bursts. By
contrast, a double bit error, or a burst error of 9 bits or more can sneak
past the XMODEM protocol arithmetic checksum.
The XMODEM/CRC protocol is similar to the XMODEM protocol, except that the
receiver specifies CRC-16 by sending C (Hex 43) instead of NAK when
requesting the FIRST packet. A two byte CRC is sent in place of the one
byte arithmetic checksum.
XMODEM protocol with CRC is accurate provided both sender and receiver
both report a successful transmission. The protocol is robust in the
presence of characters lost by buffer overloading on timesharing systems.
XMODEM and YMODEM are half duplex protocols which do not attempt to
transmit information and control signals in both directions at the same
__________
time. This avoids buffer overrun problems that have been reported by
users attempting to exploit full duplex aynchronous file transfer
protocols such as Blast.
The choice to use 1024 byte packets is expressed to the sending program
on
its command line or selection menu.
Programs using the Hoff protocol use a two character sequence emitted by
the receiver (CK) to automatically trigger the use of 1024 byte packets as
an alternative to specifying this option on this command line. Although
this two character sequence works well on single process micros in direct
communication, timesharing systems and packet switched networks can
separate the successive characters by several seconds, rendering this
method unreliable.
An STX (02) replaces the SOH (01) at the beginning of the transmitted
block to notify the receiver of the longer packet length. The transmitted
packet contains 1024 bytes of data. The receiver should be able to accept
any mixture of 128 and 1024 byte packets. The packet number is
incremented by one for each packet regardless of the packet length.
The sender must not change between 128 and 1024 byte packet lengths if it
has not received a valid ACK for the current packet. Failure to observe
this restriction allows certain transmission errors to pass undetected.
If 1024 byte packets are being used, it is possible for a file to "grow"
up to the next multiple of 1024 bytes. This does not waste disk space if
the allocation granularity is 1k or greater. When 1024 byte packets are
used with YMODEM batch transmission, the file length transmitted in the
file name packet allows the receiver to discard the padding, preserving
the exact file length and contents.
CRC-16 should be used with the k option to preserve data integrity over
phone lines.2 1024 byte packets may be used with batch file transmission
or with single file transmission.
__________
SENDER RECEIVER
"s -k foo.bar"
"foo.bar open x.x minutes"
C
STX 01 FE Data[1024] CRC CRC
ACK
STX 02 FD Data[1024] CRC CRC
ACK
STX 03 FC Data[1000] CPMEOF[24] CRC CRC
ACK
EOT
ACK
SENDER RECEIVER
"s -k foo.bar"
"foo.bar open x.x minutes"
C
STX 01 FE Data[1024] CRC CRC
ACK
STX 02 FD Data[1024] CRC CRC
ACK
SOH 03 FC Data[128] CRC CRC
ACK
SOH 04 FB Data[100] CPMEOF[28] CRC CRC
ACK
EOT
ACK
Why was it necessary to design a new batch protocol when one already
existed in MODEM7?1 The batch file mode used by MODEM7 is unsuitable
__________
1. The MODEM7 batch protocol transmitted CP/M FCB bytes f1...f8 and
t1...t3 one character at a time. The receiver echoed these bytes as
received, one at a time.
because it does not permit full pathnames, file length, file date,
or
other attribute information to be transmitted. Such a restrictive design,
hastily implemented with only CP/M in mind, would not have permitted
extensions to current areas of personal computing such as Unix, DOS, and
object oriented systems. In addition, the MODEM7 batch file mode is
somewhat susceptible to transmission impairments.
The sender opens the first file and sends packet number 0 with the
following information.2
Only the pathname (file name) part is required for batch transfers.
Filename Considerations:
__________
Length The file length and each of the succeeding fields are optional.3
The length field is stored in the packet as a decimal string counting
the number of data bytes in the file. The file length does not
include any CPMEOF (^Z) characters used to pad the last packet.
Modification Date A single space separates the modification date from the
file length.
The mod date is optional, and the filename and length may be sent
without requiring the mod date to be sent.
The mod date is sent as an octal number giving the time the contents
of the file were last changed measured in seconds from Jan 1 1970
Universal Coordinated Time (GMT). A date of 0 implies the
modification date is unknown and should be left as the date the file
is received.
Mode A single space separates the file mode from the modification date.
The file mode is stored as an octal string. Unless the file
originated from a Unix system, the file mode is set to 0. rb(1)
checks the file mode for the 0x8000 bit which indicates a Unix type
regular file. Files with the 0x8000 bit set are assumed to have been
sent from another Unix (or similar) system which uses the same file
conventions. Such files are not translated in any way.
Serial Number A single space separates the serial number from the file
mode. The serial number of the transmitting program is stored as an
octal string. Programs which do not have a serial number should omit
this field, or set it to 0. The receiver's use of this field is
optional.
After the file contents have been transmitted, the receiver again asks for
the next pathname. Transmission of a null pathname terminates batch file
transmission. Note that transmission of no files is not necessarily an
error. This is possible if none of the files requested of the sender
could be opened for reading.
In batch transmission, the receiver automatically requests CRC-16.
The Unix programs sb(1) and rb(1) included in the source code file
RBSB.SHQ (rbsb.sh) should answer other questions about YMODEM batch
protocol.
__________
SENDER RECEIVER
"sb foo.*<CR>"
"sending in batch mode etc."
C (command:rb)
SOH 00 FF foo.c NUL[123] CRC CRC
ACK
C
SOH 01 FE Data[128] CRC CRC
ACK
SOH 02 FD Data[1024] CRC CRC
ACK
SOH 03 FC Data[128] CRC CRC
ACK
SOH 04 FB Data[100] CPMEOF[28] CRC CRC
ACK
EOT
NAK
EOT
ACK
C
SOH 00 FF NUL[128] CRC CRC
ACK
_____________________________________________________________________
| Program | Batch | Length | Date | Mode | S/N | 1k-Blk | g-Option |
|___________|_______|________|______|______|_____|________|__________|
|Unix rb/sb | yes | yes | yes | yes | no | yes | sb only |
|___________|_______|________|______|______|_____|________|__________|
|VMS rb/sb | yes | yes | no | no | no | yes | no |
|___________|_______|________|______|______|_____|________|__________|
|Pro-YAM | yes | yes | yes | no | yes | yes | yes |
|___________|_______|________|______|______|_____|________|__________|
|CP/M YAM | yes | no | no | no | no | yes | no |
|___________|_______|________|______|______|_____|________|__________|
|KMD/IMP | yes | no | no | no | no | yes | no |
|___________|_______|________|______|______|_____|________|__________|
|MEX | no | no | no | no | no | yes | no |
|___________|_______|________|______|______|_____|________|__________|
Due to programming constraints, these programs do not send the file length
as described above. Instead, they send (and look for) the CP/M record
count stored in the last two bytes of the header packet. The least
significant bits are stored in the penultimate byte.
KMD and IMP use the record count to allow the receiving program to
display
the file size and estimated transmission time; the file length is
determined by the actual number of records sent.
SENDER RECEIVER
"sb foo.*<CR>"
"sending in batch mode etc..."
G (command:rb -g)
SOH 00 FF foo.c NUL[123] CRC CRC
G
SOH 01 FE Data[128] CRC CRC
SOH 02 FD Data[1024] CRC CRC
SOH 03 FC Data[128] CRC CRC
SOH 04 FB Data[100] CPMEOF[28] CRC CRC
EOT
ACK
G
SOH 00 FF NUL[128] CRC CRC
6.1 Definitions
<soh> 01H
<eot> 04H
<ack> 06H
<nak> 15H
<can> 18H
<C> 43H
6.3.1 Common_to_Both_Sender_and_Receiver
All errors are retried 10 times. For versions running with an operator
(i.e. NOT with XMODEM), a message is typed after 10 errors asking the
operator whether to "retry or quit".
Some versions of the protocol use <can>, ASCII ^X, to cancel transmission.
This was never adopted as a standard, as having a single "abort" character
makes the transmission susceptible to false termination due to an <ack>
<nak> or <soh> being corrupted into a <can> and cancelling transmission.
The protocol may be considered "receiver driven", that is, the sender need
not automatically re-transmit, although it does in the current
implementations.
Once into a receiving a block, the receiver goes into a one-second timeout
for each character and the checksum. If the receiver wishes to <nak> a
block for any reason (invalid header, timeout receiving data), it must
wait for the line to clear. See "programming tips" for ideas
6.3.3 Sending_program_considerations
While waiting for transmission to begin, the sender has only a single very
long timeout, say one minute. In the current protocol, the sender has a
10 second timeout before retrying. I suggest NOT doing this, and letting
the protocol be completely receiver-driven. This will be compatible with
existing programs.
When the sender has no more data, it sends an <eot>, and awaits an <ack>,
resending the <eot> if it doesn't get one. Again, the protocol could be
receiver-driven, with the sender only having the high-level 1-minute
timeout to abort.
SENDER RECEIVER
times out after 10 seconds,
<--- <nak>
<soh> 01 FE -data- <xx> --->
<--- <ack>
<soh> 02 FD -data- xx ---> (data gets line hit)
<--- <nak>
<soh> 02 FD -data- xx --->
<--- <ack>
<soh> 03 FC -data- xx --->
(ack gets garbaged) <--- <ack>
<soh> 03 FC -data- xx ---> <ack>
<eot> --->
<--- <anything except ack>
<eot> --->
<--- <ack>
(finished)
After receiving the <soh>, the receiver should call the character
receive subroutine with a 1-second timeout, for the remainder of the
message and the <cksum>. Since they are sent as a continuous stream,
timing out of this implies a serious like glitch that caused, say,
127 characters to be seen instead of 128.
+ You may wish to add code recommended by John Mahr to your character
receive routine - to set an error flag if the UART shows framing
error, or overrun. This will help catch a few more glitches - the
__________
most common of which is a hit in the high bits of the byte in two
consecutive bytes. The <cksum> comes out OK since counting in 1-byte
produces the same result of adding 80H + 80H as with adding 00H +
00H.
7. XMODEM/CRC Overview
The CRC used in the Modem Protocol is an alternate form of block check
which provides more robust error detection than the original checksum.
Andrew S. Tanenbaum says in his book, Computer Networks, that the CRC-
CCITT used by the Modem Protocol will detect all single and double bit
errors, all errors with an odd number of bits, all burst errors of length
16 or less, 99.997% of 17-bit error bursts, and 99.998% of 18-bit and
longer bursts.
The changes to the Modem Protocol to replace the checksum with the CRC are
straight forward. If that were all that we did we would not be able to
communicate between a program using the old checksum protocol and one
using the new CRC protocol. An initial handshake was added to solve this
problem. The handshake allows a receiving program with CRC capability to
determine whether the sending program supports the CRC option, and to
switch it to CRC mode if it does. This handshake is designed so that it
will work properly with programs which implement only the original
protocol. A description of this handshake is presented in section 10.
7.1.1 Formal_Definition
To calculate the 16 bit CRC the message bits are considered to be the
coefficients of a polynomial. This message polynomial is first multiplied
by X^16 and then divided by the generator polynomial (X^16 + X^12 + X^5 +
1) using modulo two arithmetic. The remainder left after the division is
the desired CRC. Since a message block in the Modem Protocol is 128 bytes
or 1024 bits, the message polynomial will be of order X^1023. The hi order
bit of the first byte of the message block is the coefficient of X^1023 in
the message polynomial. The lo order bit of the last byte of the message
block is the coefficient of X^0 in the message polynomial.
/*
* This function calculates the CRC used by the XMODEM/CRC Protocol
* The first argument is a pointer to the message block.
* The second argument is the number of bytes in the message block.
* The function returns an integer which contains the CRC.
* The low order 16 bits are the coefficients of the CRC.
*/
int calcrc(ptr, count)
char *ptr;
int count;
{
int crc, i;
crc = 0;
while (--count >= 0) {
crc = crc ^ (int)*ptr++ << 8;
for (i = 0; i < 8; ++i)
if (crc & 0x8000)
crc = crc << 1 ^ 0x1021;
else
crc = crc << 1;
}
return (crc & 0xFFFF);
}
7.2.1 Common_to_Both_Sender_and_Receiver
The only change to the File Level Protocol for the CRC option is the
initial handshake which is used to determine if both the sending and the
receiving programs support the CRC mode. All Modem Programs should support
the checksum mode for compatibility with older versions. A receiving
program that wishes to receive in CRC mode implements the mode setting
handshake by sending a <C> in place of the initial <nak>. If the sending
program supports CRC mode it will recognize the <C> and will set itself
into CRC mode, and respond by sending the first block as if a <nak> had
been received. If the sending program does not support CRC mode it will
not respond to the <C> at all. After the receiver has sent the <C> it will
wait up to 3 seconds for the <soh> that starts the first block. If it
receives a <soh> within 3 seconds it will assume the sender supports CRC
mode and will proceed with the file exchange in CRC mode. If no <soh> is
received within 3 seconds the receiver will switch to checksum mode, send
7.2.2 Receive_Program_Considerations
There are at least 4 things that can go wrong with the mode setting
handshake.
The first problem can be solved if the receiver sends a second <C> after
it times out the first time. This process can be repeated several times.
It must not be repeated too many times before sending a <nak> and
switching to checksum mode or a sending program without CRC support
may
time out and abort. Repeating the <C> will also fix the second problem if
the sending program cooperates by responding as if a <nak> were received
instead of ignoring the extra <C>.
It is possible to fix problems 3 and 4 but probably not worth the trouble
since they will occur very infrequently. They could be fixed by switching
modes in either the sending or the receiving program after a large number
of successive <nak>s. This solution would risk other problems however.
7.2.3 Sending_Program_Considerations
The sending program should start in the checksum mode. This will insure
compatibility with checksum only receiving programs. Anytime a <C> is
received before the first <nak> or <ack> the sending program should set
itself into CRC mode and respond as if a <nak> were received. The sender
should respond to additional <C>s as if they were <nak>s until the first
<ack> is received. This will assist the receiving program in determining
the correct mode when the <soh> is lost or garbled. After the first <ack>
is received the sending program should ignore <C>s.
Here is a data flow example for the case where the receiver requests
transmission in the CRC mode but the sender does not support the CRC
option. This example also includes various transmission errors. <xx>
represents the checksum byte.
Figure 11. Data Flow: Receiver has CRC Option, Sender Doesn't
SENDER RECEIVER
<--- <C>
times out after 3 seconds,
<--- <C>
times out after 3 seconds,
<--- <C>
times out after 3 seconds,
<--- <C>
times out after
3 seconds,
<--- <nak>
<soh> 01 FE -data- <xx> --->
<--- <ack>
<soh> 02 FD -data- <xx> ---> (data gets line hit)
<--- <nak>
<soh> 02 FD -data- <xx> --->
<--- <ack>
<soh> 03 FC -data- <xx> --->
(ack gets garbaged) <--- <ack>
times out after 10 seconds,
<--- <nak>
<soh> 03 FC -data- <xx> --->
<--- <ack>
<eot> --->
<--- <ack>
Here is a data flow example for the case where the receiver requests
transmission in the CRC mode and the sender supports the CRC option. This
example also includes various transmission errors. <xxxx> represents the
2 CRC bytes.
SENDER RECEIVER
<--- <C>
<soh> 01 FE -data- <xxxx> --->
<--- <ack>
<soh> 02 FD -data- <xxxx> ---> (data gets line hit)
<--- <nak>
<soh> 02 FD -data- <xxxx> --->
<--- <ack>
<soh> 03 FC -data- <xxxx>
--->
(ack gets garbaged) <--- <ack>
times out after 10 seconds,
<--- <nak>
<soh> 03 FC -data- <xxxx> --->
<--- <ack>
<eot> --->
<--- <ack>
8. MORE INFORMATION
omen Any ACU 1200 1-503-621-3746 se:--se: link ord: Giznoid in:--in: uucp
9. YMODEM Programs
Irv Hoff has added YMODEM 1k packets and YMODEM batch transfers to the KMD
and IMP series programs, which replace the XMODEM and MODEM7/MDM7xx series
respectively. Overlays are available for a wide variety of CP/M systems.
CONTENTS
1. ROSETTA STONE..................................................... 2
7. XMODEM/CRC Overview............................................... 18
7.1 CRC Calculation.............................................. 18
7.2 CRC File Level Protocol Changes.............................. 19
7.3 Data Flow Examples with CRC Option........................... 21
8. MORE INFORMATION.................................................. 22
9. YMODEM Programs................................................... 23
- i -
LIST OF FIGURES
Figure 11. Data Flow: Receiver has CRC Option, Sender Doesn't........
21