home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / etc / misc / x_modem.txt < prev    next >
Encoding:
Text File  |  2003-06-11  |  40.0 KB  |  1,139 lines

  1. 81
  2. By popular request, I am posting this document describing the XMODEM
  3. protocol, plus the XMODEM/CRC and YMODEM (Batch transmission, etc.)
  4. protocol extensions.
  5.  
  6. Ward Christensen's article on this subject will appear late this year
  7. in Byte Magazine.
  8. .pa
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.                      XMODEM/YMODEM PROTOCOL REFERENCE
  18.                  A compendium of documents describing the
  19.                             XMODEM and YMODEM
  20.                          File Transfer Protocols
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.                          Edited by Chuck Forsberg
  35.  
  36.                  Please distribute as widely as possible.
  37.  
  38.                        Questions to Chuck Forsberg
  39.  
  40.  
  41.  
  42.  
  43.  
  44.                            Omen Technology Inc
  45.                         17505-V Sauvie Island Road
  46.                           Portland Oregon 97231
  47.                            Voice: 503-621-3406
  48.             Modem (Telegodzilla): 503-621-3746 Speed 1200,300
  49.                           Compuserve: 70715,131
  50.                     UUCP: ...!tektronix!reed!omen!caf
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.                                  CONTENTS
  74.  
  75.  
  76. 1.  ROSETTA STONE.....................................................   2
  77.  
  78. 2.  TOWER OF BABEL ??.................................................   3
  79.     2.1  Some Messages from the Pioneer...............................   3
  80.  
  81. 3.  XMODEM PROTOCOL ENHANCEMENTS......................................   6
  82.     3.1  CAN-CAN Abort................................................   6
  83.     3.2  CRC-16 Option................................................   6
  84.     3.3  1024 Byte Packet Option......................................   7
  85.  
  86. 4.  YMODEM Batch File Transmission....................................   8
  87.  
  88. 6.  XMODEM PROTOCOL OVERVIEW..........................................  13
  89.     6.1  Definitions..................................................  13
  90.     6.2  Transmission Medium Level Protocol...........................  13
  91.     6.3  File Level Protocol..........................................  14
  92.     6.4  Programming Tips.............................................  15
  93.  
  94. 7.  XMODEM/CRC Overview...............................................  17
  95.     7.1  CRC Calculation..............................................  17
  96.     7.2  CRC File Level Protocol Changes..............................  18
  97.     7.3  Data Flow Examples with CRC Option...........................  19
  98.  
  99. 8.  MORE INFORMATION..................................................  21
  100.  
  101. 9.  YMODEM Programs...................................................  22
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.                                   - i -
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.                              LIST OF FIGURES
  129.  
  130.  
  131.  Figure 1.  1024 byte Packets.........................................   7
  132.  
  133.  Figure 2.  Mixed 1024 and 128 byte Packets...........................   7
  134.  
  135.  Figure 3.  Batch Transmission Session................................  11
  136.  
  137.  Figure 4.  Filename packet transmitted by sb.........................  11
  138.  
  139.  Figure 5.  Header Information used by YMODEM Implementations.........  11
  140.  
  141.  Figure 6.  XMODEM Message Block Level Protocol.......................  14
  142.  
  143.  Figure 7.  Data flow including Error Recovery........................  15
  144.  
  145.  Figure 8.  Message Block Level Protocol, CRC mode....................  17
  146.  
  147.  Figure 9.  Example of CRC Calculation written in C...................  18
  148.  
  149. Figure 10.  Data Flow: Receiver has CRC Option, Sender Doesn't........  19
  150.  
  151. Figure 11.  Receiver and Sender Both have CRC Option..................  20
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.                                   - ii -
  176.  
  177.  
  178. X/YMODEM Protocol Reference      5-30-85                                 2
  179.  
  180.  
  181. 1.  ROSETTA STONE
  182.  
  183. Here are some definitions which reflect the current vernacular in the
  184. computer media.  The attempt here is identify the file transfer protocol
  185. rather than specific programs.
  186.  
  187. XMODEM  refers to the original 1979 file transfer etiquette introduced by
  188.         Ward Christensen's 1979 MODEM2 program.  It's also called the
  189.         MODEM or MODEM2 protocol.  Some who are unaware of MODEM7's batch
  190.         file mode call it MODEM7.  Other aliases include "CP/M Users's
  191.         Group" and "TERM II FTP 3".  This protocol is supported by every
  192.         serious communications program because of its universality,
  193.         simplicity, and reasonable performance.
  194.  
  195. XMODEM/CRC replaces XMODEM's 1 byte checksum with CRC-16, giving modern
  196.         error detection protection.
  197.  
  198. YMODEM  refers to the XMODEM/CRC protocol with the throughput and batch
  199.         transmission enhancements described below.
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.                                   - 2 -
  233.  
  234. X/YMODEM Protocol Reference      5-30-85                                 3
  235.  
  236.  
  237. 2.  TOWER OF BABEL ??
  238.  
  239. In the interest of fostering compatibility among communications         programs,
  240. part of of the Professional-YAM manual is reproduced here to minimize the
  241. Electronic Tower of Babel.
  242.  
  243. The YMODEM Protocol is supported by the public domain programs YAM (CP/M),
  244. YAM(CP/M-86), YAM(CCPM-86),  MODEM76.ASM (CP/M), rb/sb (Unix, VMS,
  245. Berkeley Unix, Venix, Xenix, Coherent, IDRIS, Regulus) as well as
  246. Professional-YAM.  These programs have been in use since 1981.
  247.  
  248. The protocols described below are enhancements to Ward Christensen's
  249. XMODEM protocol, and are public domain.
  250.  
  251. The 1k packet length capability described below may be used in conjunction
  252. with the Batch Protocol, or with single file transfers identical to the
  253. XMODEM/CRC protocol except for the minimal changes to support 1k packets.
  254.  
  255. To complete this tome, Ward Christensen's original protocol document and
  256. John Byrns's CRC-16 document are included for reference.
  257.  
  258. References to the MODEM or MODEM7 protocol have been changed to XMODEM to
  259. accommodate the vernacular.
  260.  
  261. Watch for an article describing the YMODEM protocol in a more coherent
  262. fashion later this year.  This article will include some interesting
  263. history on the development of microcomputer file transfers.
  264.  
  265.  
  266. 2.1  Some Messages from the Pioneer
  267.  
  268. #: 130940 S0/Communications 25-Apr-85  18:38:47
  269. Sb: my protocol
  270. Fm: Ward Christensen 76703,302 (EDITED)
  271. To: all
  272.  
  273.  
  274. Be aware the article1 DID quote me correctly in terms of the phrases like
  275. "not robust", etc.
  276.  
  277. It was a quick hack I threw together, very unplanned (like everything I
  278. do), to satisfy a personal need to communicate with "some other" people.
  279.  
  280. ONLY the fact that it was done in 8/77, and that I put it in the public
  281. domain immediately, made it become the standard that it is.
  282.  
  283.  
  284. __________
  285.  
  286.  1. Infoworld April 29 p. 16
  287.  
  288.                                    - 3 -
  289.  
  290. X/YMODEM Protocol Reference      5-30-85                                 4
  291.  
  292.  
  293. I think its time for me to
  294.  
  295. (1) document it; (people call me and say "my product is going to include
  296. it - what can I 'reference'", or "I'm writing a paper on it, what do I put
  297. in the bibliography") and
  298.  
  299. (2) propose an "incremental extension" to it, which might take "exactly"
  300. the form of Chuck Forsberg's YAM protocol.  He wrote YAM in C for CP/M and
  301. put it in the public domain, and wrote a batch protocol for Unix called rb
  302. and sb (receive batch, send batch), which was basically XMODEM with
  303.    (a) a record 0 containing filename date time and size
  304.    (b) a 1K block size option
  305.    (c) CRC-16.
  306.  
  307. He did some clever programming to detect false ACK or EOT, but basically
  308. left them the same.
  309.  
  310. People who suggest I make SIGNIFICANT changes to the protocol, such as
  311. "full duplex", "multiple outstanding blocks", "multiple destinations", etc
  312. etc don't understand that the incredible simplicity of the protocol is one
  313. of the reasons it survived to this day in as many machines and programs as
  314. it may be found in!
  315.  
  316. Consider the PC-NET group back in '77 or so - documenting to beat the band
  317. - THEY had a protocol, but it was "extremely complex", because it tried to
  318. be "all things to all people" - i.e. send binary files on a 7-bit system,
  319. etc.  I was not that "benevolent". I (emphasize > I < ) had an 8-bit UART,
  320. so "my protocol was an 8-bit protocol", and I would just say "sorry" to
  321. people who were held back by 7-bit limitations.
  322. ...
  323. Block size: Chuck Forsberg created an extension of my protocol, called
  324. YAM, which is also supported via his public domain programs for UNIX
  325. called rb and sb - receive batch and send batch.  They cleverly send a
  326. "block 0" which contains the filename, date, time, and size.
  327. Unfortunately, its UNIX style, and is a bit weird2 - octal numbers, etc.
  328. BUT, it is a nice way to overcome the kludgy "echo the chars of the name"
  329. introduced with MODEM7.  Further, chuck uses CRC-16 and optional 1K
  330. blocks.  Thus the record 0, 1K, and CRC, make it a "pretty slick new
  331. protocol" which is not significantly different from my own.
  332.  
  333. Also, there is a catchy name - YMODEM.  That means to some that it is the
  334. "next thing after XMODEM", and to others that it is the Y(am)MODEM
  335. protocol.  I don't want to emphasize that too much - out of fear that
  336. other mfgrs might think it is a "competitive" protocol, rather than an
  337.  
  338.  
  339. __________
  340.  
  341.  2. The Unix style stuff (time, file mode) is optional.  The pathname and
  342.     file length may be sent alone if desired.
  343.  
  344.                                   - 4 -
  345. .pa
  346.  
  347. X/YMODEM Protocol Reference      5-30-85                                 5
  348.  
  349.  
  350. "unaffiliated" protocol.  Chuck is currently selling a much-enhanced
  351. version of his CP/M-80 C program YAM, calling it Professional Yam, and its
  352. for the PC - I'm using it right now.  VERY slick!  32K capture buffer,
  353. script, scrolling, previously captured text search, plus built-in commands
  354. for just about everything - directory (sorted every which way), XMODEM,
  355. YMODEM, KERMIT, and ASCII file upload/download, etc.  You can program it
  356. to "behave" with most any system - for example when trying a number for
  357. CIS it detects the "busy" string back from the modem and substitutes a
  358. diff phone # into the dialing string and branches back to try it.
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.                                   - 5 -
  366. .pa
  367.  
  368. X/YMODEM Protocol Reference      5-30-85                                 6
  369.  
  370.  
  371. 3.  XMODEM PROTOCOL ENHANCEMENTS
  372.  
  373. Professional-YAM uses several compatible extensions and logic enhancements
  374. to the widely used Ward Christensen XMODEM protocol.
  375.  
  376. This chapter discusses the protocol extensions to Ward Christensen's 1982
  377. XMODEM protocol description document.
  378.  
  379. YAM does not ask the operator whether he wishes to keep retrying after 10
  380. attempts have failed to correctly transfer a packet.  Virtualeectable errors are corrected within th few retransmissions.  If
  381. the line is so bad that ten attempts are insufficient, there is a
  382. significant danger of undetected errors.  In that case, it's better to
  383. redial for a better connection.
  384.  
  385.  
  386. 3.1  CAN-CAN Abort
  387.  
  388. YAM recognizes a sequence of two consecutive CAN (Hex 18) characters
  389. without modem errors (overrun, framing, etc.) as a transfer abort
  390. command.1 The check for two consecutive CAN characters virtually
  391. eliminates the possibility of a line hit aborting the transfer.  YAM sends
  392. five CAN characters when it aborts a XMODEM protocol file transfer,
  393. followed by five backspaces to delete the CAN characters from the remote's
  394. keyboard input buffer (in case the remote had already aborted the
  395. transfer).
  396.  
  397.  
  398. 3.2  CRC-16 Option
  399.  
  400. The XMODEM protocol uses an optional two character CRC-16 instead of the
  401. one character arithmetic checksum used by the original protocol and by
  402. most commercial implementations.  CRC-16 guarantees detection of all
  403. single and double bit errors,  all errors with an odd number of error
  404. bits, all burst errors of length 16 or less, 99.9969% of all 17-bit error
  405. bursts, and 99.9984 per cent of all possible longer error bursts.  By
  406. contrast, a double bit error, or a burst error of 9 bits or more can sneak
  407. past the XMODEM protocol arithmetic checksum.
  408.  
  409. The XMODEM/CRC protocol is similar to the XMODEM protocol, except that the
  410. receiver specifies CRC-16 by sending C (Hex 43) instead of NAK when
  411. requesting the FIRST packet.  A two byte CRC is sent in place of the one
  412. byte arithmetic checksum.  YAM's c option to the _ r command enables CRC-16
  413. in single file reception, corresponding to the original implementation in
  414.  
  415.  
  416. __________
  417.  
  418.  1. This is recognized when YAM is waiting for the beginning of a packet
  419.     or for an acknowledge to one that has been sent.
  420.  
  421.                                   - 6 -
  422.  
  423. X/YMODEM Protocol Reference      5-30-85                                 7
  424.  
  425.  
  426. the MODEM7 series programs.  Many commercial communications programs and
  427. bulletin board systems still do not support CRC-16, especially those
  428. written in Basic or Pascal.
  429.  
  430. XMODEM protocol with CRC is accurate provided both sender and receiver
  431. both report a successful transmission.  The protocol is robust in the
  432. presence of characters lost by buffer overloading on timesharing systems.
  433.  
  434. Professional-YAM adds several proprietary logic enhancements to XMODEM's
  435. error detection and recovery.  These compatible enhancements eliminate
  436. most of the bad file transfers other programs make when using the XMODEM
  437. protocol under less than ideal conditions.
  438.  
  439.  
  440. 3.3  1024 Byte Packet Option
  441.  
  442. If YAM is sending with the k option, the transmitted packet contains 1024
  443. bytes of data.  An STX (02) replaces the SOH (01) at the beginning of the
  444. transmitted block to notify the receiver of the longer packet length.  The
  445. receiver should be able to accept any mixture of 128 and 1024 byte
  446. packets.  The packet number is incremented by one for each packet.2 
  447.  
  448. If 1024 byte packets are being used, it is possible for a 128 byte file to
  449. "grow" to 1024 bytes on CP/M.  This does not waste disk space on CP/M
  450. because the allocation granularity is 1k.  When 1024 byte packets are used
  451. with batch transmission, the file length transmitted in the file name
  452. packet allows the receiver to discard the padding.
  453.  
  454. CRC-16 should be used with the k option to preserve data integrity over
  455. phone lines.  The k option may be used with batch file transmission, or
  456. with single file transmission.
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471. __________
  472.  
  473.  2. The displayed sector number should be the number of bytes divided by
  474.     128.  This maintains a familiar unit of measurement for the progress
  475.     report and avoids ambiguity caused by rollover of the sector number.
  476.  
  477.                                   - 7 -
  478.  
  479. X/YMODEM Protocol Reference      5-30-85                                 8
  480.  
  481.  
  482.           Figure 1.  1024 byte Packets
  483.  
  484.           SENDER                                  RECEIVER
  485.                                                   "s -k foo.bar"
  486.           "foo.bar open x.x minutes"
  487.                                                   C
  488.           STX 01 FE Data[1024] CRC CRC
  489.                                                   ACK
  490.           STX 02 FD Data[1024] CRC CRC
  491.                                                   ACK
  492.           STX 03 FC Data[1000] CPMEOF[24] CRC CRC
  493.                                                   ACK
  494.           EOT
  495.                                                   ACK
  496.  
  497.           Figure 2.  Mixed 1024 and 128 byte Packets
  498.  
  499.           SENDER                                  RECEIVER
  500.                                                   "s -k foo.bar"
  501.           "foo.bar open x.x minutes"
  502.                                                   C
  503.           STX 01 FE Data[1024] CRC CRC
  504.                                                   ACK
  505.           STX 02 FD Data[1024] CRC CRC
  506.                                                   ACK
  507.           SOH 03 FC Data[128] CRC CRC
  508.                                                   ACK
  509.           SOH 04 FB Data[100] CPMEOF[28] CRC CRC
  510.                                                   ACK
  511.           EOT
  512.                                                   ACK
  513.  
  514. 4.  YMODEM Batch File Transmission
  515.  
  516. The YMODEM Batch protocol is an extension to the XMODEM/CRC protocol that
  517. allows 0 or more files to be transmitted with a single command.  (Zero
  518. files may be sent if none of the requested files is accessible.) The
  519. design approach of the YAM Batch protocol is to use the normal routines
  520. for sending and receiving XMODEM packets in a layered fashion similar to
  521. packet switching methods.
  522.  
  523. Why was it necessary to design a new batch protocol when one already
  524. existed in MODEM7?1 The batch file mode used by MODEM7 is unsuitable
  525.  
  526.  
  527. __________
  528.  
  529.  1. The MODEM7 batch protocol transmitted CP/M FCB bytes f1...f8 and
  530.     t1...t3 one character at a time.  The receiver echoed these bytes as
  531.     received, one at a time.
  532.  
  533.                                   - 8 -
  534.  
  535. X/YMODEM Protocol Reference      5-30-85                                 9
  536.  
  537.  
  538. because it does not permit full pathnames, file length, file date, or
  539. other attribute information to be transmitted.  Such a restrictive design,
  540. hastily implemented with only CP/M in mind, would not have permitted
  541. extensions to current areas of personal computing such as Unix, DOS, and
  542. object oriented systems.
  543.  
  544. As in the case of single a file transfer, the receiver initiates batch
  545. file transmission by sending a "C" character (for CRC-16).
  546.  
  547. The sender opens the first file and sends packet number 0 with the
  548. following information.2
  549.  
  550. Only the pathname (file name) part is required for batch transfers.
  551.  
  552. To maintain upwards compatibility, all unused bytes in packet 0 must be
  553. set to null.
  554.  
  555. Pathname The pathname (conventionally, the file name) is sent as a null
  556.      terminated ASCII string.  This is the filename format used by the
  557.      handle oriented MSDOS(TM) functions and C library fopen functions.
  558.      An assembly language example follows:
  559.                               DB      'foo.bar',0
  560.      No spaces are included in the pathname.  Normally only the file name
  561.      stem (no directory prefix) is transmitted unless the sender has
  562.      selected YAM's f option to send the full pathname.  The source drive
  563.      (A:, B:, etc.) is not sent.
  564.  
  565.      Filename Considerations:
  566.  
  567.         o + File names should be translated to lower case unless the sending
  568.           system supports upper/lower case file names.  This is a
  569.           convenience for users of systems (such as Unix) which store
  570.           filenames in upper and lower case.
  571.  
  572.         o + The receiver should accommodate file names in lower and upper
  573.           case.
  574.  
  575.         o + The rb(1) program on Unix systems normally translates the
  576.           filename to lower case unless one or more letters in the
  577.           filename are already in lower case.
  578.  
  579.         o + When transmitting files between different operating systems,
  580.           file names must be acceptable to both the sender and receiving
  581.           operating systems.
  582.      If directories are included, they are delimited by /; i.e.,
  583.  
  584.  
  585. __________
  586.  
  587.  2. Only the data part of the packet is described here.
  588.  
  589.                                   - 9 -
  590.  
  591. X/YMODEM Protocol Reference      5-30-85                                10
  592.  
  593.  
  594.      "subdir/foo" is acceptable, "subdir\foo" is not.
  595.  
  596. Length The file length and each of the succeeding fields are optional.3
  597.      The length field is stored in the packet as a decimal string.  The
  598.      file length does not include any CPMEOF (^Z) characters the
  599.      communications program might use to pad the last packet.
  600.  
  601.      If the file being transmitted is growing during transmission, the
  602.      length field should be set to at least the final expected file
  603.      length, or not sent.
  604.  
  605.      The receiver stores the specified number of characters, discarding
  606.      any padding added by the sender to fill up the last packet.
  607.  
  608. Mod Date A single space separates the modification date from the file
  609.      length.
  610.  
  611.      The mod date is optional, and the filename and length may be sent
  612.      without requiring the mod date to be sent.
  613.  
  614.      The mod date is sent as an octal number giving the time the contents
  615.      of the file were last changed measured in seconds from Jan 1 1970
  616.      Universal Coordinated Time (GMT).  A date of 0 implies the
  617.      modification date is unknown and should be left as the date the file
  618.      is received.
  619.  
  620.      This standard format was chosen to eliminate ambiguities arising from
  621.      transfers between different time zones.
  622.  
  623.      Two Microsoft blunders complicate the use of modification dates in
  624.      file transfers with MSDOS(TM) systems.  The first is the lack of
  625.      timezone standardization in MS-DOS.  A file's creation time can not
  626.      be known unless the timezone of the system that happened to write the
  627.      file4 is known.  Unix solved this problem (for planet Earth, anyway)
  628.      by stamping files with Universal Time (GMT).  Microsoft would have to
  629.      include the timezone of origin in the directory entries, but does
  630.      not.  YAM gets around this problem by using the z parameter which is
  631.      originate from a different timezone, the -zT option may be used use T
  632.      as the timezone for an individual file transfer.
  633.  
  634.      The second problem is the lack of a separate file creation date in
  635.      DOS.  Since some backup schemes used with DOS rely on the file
  636.  
  637.  
  638. __________
  639.  
  640.  3. Fields may not be skipped.
  641.  
  642.  4. Not necessarily that of the transmitting system!
  643.  
  644.  
  645.                                   - 10 -
  646.  
  647. X/YMODEM Protocol Reference      5-30-85                                11
  648.  
  649.  
  650.      creation date to select files to be copied to the archive, back-
  651.      dating the file modification date could interfere with the safety of
  652.      the transferred files.  For this reason, Professional-YAM does not
  653.      modify the date of received files with the header information unless
  654.      the d parameter is non zero.
  655.  
  656.  
  657. Mode A single space separates the file mode from the modification date.
  658.      The file mode is stored as an octal string.  Unless the file
  659.      originated from a Unix system, the file mode is set to 0.  rb(1)
  660.      checks the file mode for the 0x8000 bit which indicates a Unix type
  661.      regular file.  Files with the 0x8000 bit set are assumed to have been
  662.      sent from another Unix (or similar) system which uses the same file
  663.      conventions.  Such files are not translated in any way.
  664.  
  665.  
  666. Serial Number A single space separates the serial number from the file
  667.      mode.  The serial number of the transmitting program is stored as an
  668.      octal string.  Programs which do not have a serial number should omit
  669.      this field, or set it to 0.  The receiver's use of this field is
  670.      optional.
  671.  
  672. The rest of the packet is set to nulls.  This is essential to preserve
  673. upward compatibility.5 After the filename packet has been received, it is
  674. ACK'ed if the write open is successful.  The receiver then initiates
  675. transfer of the file contents according to the standard XMODEM/CRC
  676. protocol.  If the file cannot be opened for writing, the receiver cancels
  677. the transfer with CAN characters as described above.
  678.  
  679. After the file contents have been transmitted, the receiver again asks for
  680. the next pathname.  Transmission of a null pathname terminates batch file
  681. transmission.  Note that transmission of no files is not necessarily an
  682. error.  This is possible if none of the files requested of the sender
  683. could be opened for reading.
  684.  
  685. In batch transmission, the receiver automatically requests CRC-16.
  686.  
  687. The Unix programs sb(1) and rb(1) included in the source code file
  688. RBSB.QQQ (rbsb.sh) should answer questions about YAM's batch protocol.
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695. __________
  696.  
  697.  5. If, perchance, this information extends beyond 128 bytes (possible
  698.     with Unix 4.2 BSD extended file names), the packet should be sent as a
  699.     1k packet as described above.
  700.  
  701.                                   - 11 -
  702.  
  703. X/YMODEM Protocol Reference      5-30-85                                12
  704.  
  705.  
  706.             Figure 3.  Batch Transmission Session
  707.  
  708.             SENDER                                  RECEIVER
  709.                                                     "sb foo.*"
  710.             "sending in batch mode etc."
  711.                                                     C
  712.             SOH 00 FF foo.c NUL[123] CRC CRC
  713.                                                     ACK
  714.                                                     C
  715.             SOH 01 FE Data[128] CRC CRC
  716.                                                     ACK
  717.             SOH 02 FD Data[1024] CRC CRC
  718.                                                     ACK
  719.             SOH 03 FC Data[128] CRC CRC
  720.                                                     ACK
  721.             SOH 04 FB Data[100] CPMEOF[28] CRC CRC
  722.                                                     ACK
  723.             EOT
  724.                                                     ACK
  725.                                                     C
  726.             SOH 00 FF NUL[128] CRC CRC
  727.                                                     ACK
  728.  
  729.        Figure 4.  Filename packet transmitted by sb
  730.  
  731.                     -rw-r--r--  6347 Jun 17 1984 20:34 bbcsched.txt
  732.  
  733.        00 0100FF62 62637363 6865642E 74787400   |...bbcsched.txt.|
  734.        10 36333437 20333331 34373432 35313320   |6347 3314742513 |
  735.        20 31303036 34340000 00000000 00000000   |100644..........|
  736.        30 00000000 00000000 00000000 00000000
  737.        80 000000CA 56
  738.  
  739.     Figure 5.  Header Information used by YMODEM Implementations
  740.     
  741.     
  742.     Program         Name    Length  Date    Mode    S/N     1k Blocks
  743.     
  744.     Unix rb/sb      yes     yes     yes     yes     no      yes
  745.     VMS rb/sb       yes     yes     no      no      no      yes
  746.     Pro-YAM         yes     yes     yes     no      yes     yes
  747.     cp/M YAM        yes     no      no      no      no      yes
  748.     MODEM76         yes     no      no      no      no      no
  749.  
  750.  
  751.  
  752.  
  753.  
  754.  
  755.  
  756.  
  757.                                   - 12 -
  758.  
  759. X/YMODEM Protocol Reference      5-30-85                                13
  760.  
  761.  
  762. 6.  XMODEM PROTOCOL OVERVIEW
  763.  
  764. 8/9/82 by Ward Christensen.
  765.  
  766. I will maintain a master copy of this.  Please pass on changes or
  767. suggestions via CBBS/Chicago at (312) 545-8086, CBBS/CPMUG (312) 849-1132
  768. or by voice at (312) 849-6279.
  769.  
  770. 6.1  Definitions
  771.  
  772.   <soh> 01H
  773.   <eot> 04H
  774.   <ack> 06H
  775.   <nak> 15H
  776.   <can> 18H
  777.   <C>   43H
  778.  
  779.  
  780. 6.2  Transmission Medium Level Protocol
  781.  
  782. Asynchronous, 8 data bits, no parity, one stop bit.
  783.  
  784. The protocol imposes no restrictions on the contents of the data being
  785. transmitted.  No control characters are looked for in the 128-byte data
  786. messages.  Absolutely any kind of data may be sent - binary, ASCII, etc.
  787. The protocol has not formally been adopted to a 7-bit environment for the
  788. transmission of ASCII-only (or unpacked-hex) data , although it could be
  789. smply by having both ends agree to AND the protocol-dependent data with
  790. 7F hex before validating it.  I specifically am referring to the checksum,
  791. and the block numbers and their ones- complement.
  792.  
  793. Those wishing to maintain compatibility of the CP/M file structure, i.e.
  794. to allow modemming ASCII files to or from CP/M systems should follow this
  795. data format:
  796.  
  797.    o + ASCII tabs used (09H); tabs set every 8.
  798.  
  799.    o + Lines terminated by CR/LF (0DH 0AH)
  800.  
  801.    o + End-of-file indicated by ^Z, 1AH.  (one or more)
  802.  
  803.    o + Data is variable length, i.e. should be considered a continuous
  804.      stream of data bytes, broken into 128-byte chunks purely for the
  805.      purpose of transmission.
  806.  
  807.    o + A CP/M "peculiarity": If the data ends exactly on a 128-byte
  808.      boundary, i.e. CR in 127, and LF in 128, a subsequent sector
  809.      containing the ^Z EOF character(s) is optional, but is preferred.
  810.      Some utilities or user programs still do not handle EOF without ^Zs.
  811.  
  812.  
  813.                                   - 13 -
  814.  
  815. X/YMODEM Protocol Reference      5-30-85                                14
  816.  
  817.  
  818.    o + The last block sent is no different from others, i.e.  there is no
  819.      "short block".
  820.  
  821.               Figure 6.  XMODEM Message Block Level Protocol
  822.  
  823.       Each block of the transfer looks like:
  824.      <SOH><blk #><255-blk #><--128 data bytes--><cksum>
  825.          in which:
  826.      <SOH>       = 01 hex
  827.      <blk #>     = binary number, starts at 01 increments by 1, and
  828.                    wraps 0FFH to 00H (not to 01)
  829.      <255-blk #> = blk # after going thru 8080 "CMA" instr, i.e.
  830.                    each bit complemented in the 8-bit block number.
  831.                    Formally, this is the "ones complement".
  832.      <cksum>     = the sum of the data bytes only.  Toss any carry.
  833.  
  834. 6.3  File Level Protocol
  835.  
  836. 6.3.1
  837. All errors are retried 10 times.  For versions running with an operator
  838. (i.e. NOT with XMODEM), a message is typed after 10 errors asking the
  839. operator whether to "retry or quit".
  840.  
  841. Some versions of the protocol use <can>, ASCII ^X, to cancel transmission.
  842. This was never adopted as a standard, as having a single "abort" character
  843. makes the transmission susceptible to false termination due to an <ack>
  844. <nak> or <soh> being corrupted into a <can> and cancelling transmission.
  845.  
  846. The protocol may be considered "receiver driven", that is, the sender need
  847. not automatically re-transmit, although it does in the current
  848. implementations.
  849.  
  850.  
  851. 6.3.2
  852. The receiver has a 10-second timeout.  It sends a <nak> every time it
  853. times out.  The receiver's first timeout, which sends a <nak>, signals the
  854. transmitter to start.  Optionally, the receiver could send a <nak>
  855. immediately, in case the sender was ready.  This would save the initial 10
  856. second timeout.  However, the receiver MUST continue to timeout every 10
  857. seconds in case the sender wasn't ready.
  858.  
  859. Once into a receiving a block, the receiver goes into a one-second timeout
  860. for each character and the checksum.  If the receiver wishes to <nak> a
  861. block for any reason (invalid header, timeout receiving data), it must
  862. wait for the line to clear.  See "programming tips" for ideas
  863.  
  864. Synchronizing:  If a valid block number is received, it will be: 1) the
  865. expected one, in which case everything is fine; or 2) a repeat of the
  866. previously received block.  This should be considered OK, and only
  867. indicates that the receivers <ack> got glitched, and the sender re-
  868. transmitted; 3) any other block number indicates a fatal loss of
  869.                                   - 14 -
  870.  
  871. X/YMODEM Protocol Reference      5-30-85                                15
  872.  
  873.  
  874. synchronization, such as the rare case of the sender getting a line-glitch
  875. that looked like an <ack>.  Abort the transmission, sending a <can>
  876.  
  877.  
  878. 6.3.3
  879. While waiting for transmission to begin, the sender has only a single very
  880. long timeout, say one minute.  In the current protocol, the sender has a
  881. 10 second timeout before retrying.  I suggest NOT doing this, and letting
  882. the protocol be completely receiver-driven.  This will be compatible with
  883. existing programs.
  884.  
  885. When the sender has no more data, it sends an <eot>, and awaits an <ack>,
  886. resending the <eot> if it doesn't get one.  Again, the protocol could be
  887. receiver-driven, with the sender only having the high-level 1-minute
  888. timeout to abort.
  889.  
  890.  
  891. Here is a sample of the data flow, sending a 3-block message.  It includes
  892. the two most common line hits - a garbaged block, and an <ack> reply
  893. getting garbaged.  <xx> represents the checksum byte.
  894.  
  895.  
  896.        Figure 7.  Data flow including Error Recovery
  897.  
  898.        SENDER                                  RECEIVER
  899.                                        times out after 10 seconds,
  900.                                <---            <nak>
  901.        <soh> 01 FE -data- <xx> --->
  902.                                <---            <ack>
  903.        <soh> 02 FD -data- xx   --->    (data gets line hit)
  904.                                <---            <nak>
  905.        <soh> 02 FD -data- xx   --->
  906.                                <---            <ack>
  907.        <soh> 03 FC -data- xx   --->
  908.           (ack gets garbaged)  <---            <ack>
  909.        <soh> 03 FC -data- xx   --->            <ack>
  910.        <eot>                   --->
  911.                                <---            <ack>
  912.  
  913. 6.4  Programming Tips
  914.  
  915.    o + The character-receive subroutine should be called with a parameter
  916.      specifying the number of seconds to wait.  The receiver should first
  917.      call it with a time of 10, then <nak> and try again, 10 times.
  918.  
  919.      After receiving the <soh>, the receiver should call the character
  920.      receive subroutine with a 1-second timeout, for the remainder of the
  921.      message and the <cksum>.  Since they are sent as a continuous stream,
  922.      timing out of this implies a serious like glitch that caused, say,
  923.      127 characters to be seen instead of 128.
  924.  
  925.                                   - 15 -
  926.  
  927. X/YMODEM Protocol Reference      5-30-85                                16
  928.  
  929.  
  930.    o + When the receiver wishes to <nak>, it should call a "PURGE"
  931.      subroutine, to wait for the line to clear.  Recall the sender tosses
  932.      any characters in its UART buffer immediately upon completing sending
  933.      a block, to ensure no glitches were mis- interpreted.
  934.  
  935.      The most common technique is for "PURGE" to call the character
  936.      receive subroutine, specifying a 1-second timeout,1 and looping back
  937.      to PURGE until a timeout occurs.  The <nak> is then sent, ensuring
  938.      the other end will see it.
  939.  
  940.    o + You may wish to add code recommended by John Mahr to your character
  941.      receive routine - to set an error flag if the UART shows framing
  942.      error, or overrun.  This will help catch a few more glitches - the
  943.      most common of which is a hit in the high bits of the byte in two
  944.      consecutive bytes.  The <cksum> comes out OK since counting in 1-byte
  945.      produces the same result of adding 80H + 80H as with adding 00H +
  946.      00H.
  947.  
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.  
  963.  
  964.  
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977. __________
  978.  
  979.  1. These times should be adjusted for use with timesharing systems.
  980.  
  981.                                   - 16 -
  982.  
  983. X/YMODEM Protocol Reference      5-30-85                                17
  984.  
  985.  
  986. 7.  XMODEM/CRC Overview
  987.  
  988. 1/13/85 by John Byrns -- CRC option.
  989.  
  990. Please pass on any reports of errors in this document or suggestions for
  991. improvement to me via Ward's/CBBS at (312) 849-1132, or by voice at (312)
  992. 885-1105.
  993.  
  994. The CRC used in the Modem Protocol is an alternate form of block check
  995. which provides more robust error detection than the original checksum.
  996. Andrew S. Tanenbaum says in his book, Computer Networks, that the CRC-
  997. CCITT used by the Modem Protocol will detect all single and double bit
  998. errors, all errors with an odd number of bits, all burst errors of length
  999. 16 or less, 99.997% of 17-bit error bursts, and 99.998% of 18-bit and
  1000. longer bursts.
  1001.  
  1002. The changes to the Modem Protocol to replace the checksum with the CRC are
  1003. straight forward. If that were all that we did we would not be able to
  1004. communicate between a program using the old checksum protocol and one
  1005. using the new CRC protocol. An initial handshake was added to solve this
  1006. problem. The handshake allows a receiving program with CRC capability to
  1007. determine whether the sending program supports the CRC option, and to
  1008. switch it to CRC mode if it does. This handshake is designed so that it
  1009. will work properly with programs which implement only the original
  1010. protocol. A description of this handshake is presented in section 10.
  1011.  
  1012.   Figure 8.  Message Block Level Protocol, CRC mode
  1013.  
  1014.   Each block of the transfer in CRC mode looks like:
  1015.  
  1016.   <SOH><blk #><255-blk #><--128 data bytes--><CRC hi><CRC lo>
  1017.       in which:
  1018.   <SOH>             = 01 hex
  1019.   <blk #>     = binary number, starts at 01 increments by 1, and
  1020.                 wraps 0FFH to 00H (not to 01)
  1021.   <255-blk #> = ones complement of blk #.
  1022.   <CRC hi>    = byte containing the 8 hi order coefficients of the CRC.
  1023.   <CRC lo>    = byte containing the 8 lo order coefficients of the CRC.
  1024.                 See the next section for CRC calculation.
  1025.  
  1026. 7.1  CRC Calculation
  1027.  
  1028. 7.1.1 
  1029. To calculate the 16 bit CRC the message bits are considered to be the
  1030. coefficients of a polynomial. This message polynomial is first multiplied
  1031. by X^16 and then divided by the generator polynomial (X^16 + X^12 + X^5 +
  1032. 1) using modulo two arithmetic. The remainder left after the division is
  1033. the desired CRC. Since a message block in the Modem Protocol is 128 bytes
  1034. or 1024 bits, the message polynomial will be of order X^1023. The hi order
  1035. bit of the first byte of the message block is the coefficient of X^1023 in
  1036. the message polynomial.  The lo order bit of the last byte of the message
  1037.                                   - 17 -
  1038.  
  1039. X/YMODEM Protocol Reference      5-30-85                                18
  1040.  
  1041.  
  1042. block is the coefficient of X^0 in the message polynomial.
  1043.  
  1044.  
  1045.    Figure 9.  Example of CRC Calculation written in C
  1046.  
  1047.    /*
  1048.     * This function calculates the CRC used by the XMODEM/CRC Protocol
  1049.     * The first argument is a pointer to the message block.
  1050.     * The second argument is the number of bytes in the message block.
  1051.     * The function returns an integer which contains the CRC.
  1052.     * The low order 16 bits are the coefficients of the CRC.
  1053.     */
  1054.  
  1055.    int calcrc(ptr, count)
  1056.    char *ptr;
  1057.    int count;
  1058.    {
  1059.        int crc, i;
  1060.  
  1061.        crc = 0;
  1062.        while(--count >= 0) {
  1063.            crc = crc ^ (int)*ptr++ << 8;
  1064.            for(i = 0; i < 8; ++i)
  1065.                if(crc & 0x8000)
  1066.                    crc = crc << 1 ^ 0x1021;
  1067.                else
  1068.                    crc = crc << 1;
  1069.            }
  1070.        return (crc & 0xFFFF);
  1071.    }
  1072.  
  1073. 7.2  CRC File Level Protocol Changes
  1074.  
  1075. 7.2.1
  1076. The only change to the File Level Protocol for the CRC option is the
  1077. initial handshake which is used to determine if both the sending and the
  1078. receiving programs support the CRC mode. All Modem Programs should support
  1079. the checksum mode for compatibility with older versions.  A receiving
  1080. program that wishes to receive in CRC mode implements the mode setting
  1081. handshake by sending a <C> in place of the initial <nak>.  If the sending
  1082. program supports CRC mode it will recognize the <C> and will set itself
  1083. into CRC mode, and respond by sending the first block as if a <nak> had
  1084. been received. If the sending program does not support CRC mode it will
  1085. not respond to the <C> at all. After the receiver has sent the <C> it will
  1086. wait up to 3 seconds for the <soh> that starts the first block. If it
  1087. receives a <soh> within 3 seconds it will assume the sender supports CRC
  1088. mode and will proceed with the file exchange in CRC mode. If no <soh> is
  1089. received within 3 seconds the receiver will switch to checksum mode, send
  1090. a <nak>, and proceed in checksum mode. If the receiver wishes to use
  1091. checksum mode it should send an initial <nak> and the sending program
  1092. should respond to the <nak> as defined in the original Modem Protocol.
  1093.                                   - 18 -
  1094.  
  1095. X/YMODEM Protocol Reference      5-30-85                                19
  1096.  
  1097.  
  1098. After the mode has been set by the initial <C> or <nak> the protocol
  1099. follows the original Modem Protocol and is identical whether the checksum
  1100. or CRC is being used.
  1101.  
  1102.  
  1103. 7.2.2
  1104. There are at least 4 things that can go wrong with the mode setting
  1105. handshake.
  1106.  
  1107.   1.  the initial <C> can be garbled or lost.
  1108.  
  1109.   2.  the initial <soh> can be garbled.
  1110.  
  1111.   3.  the initial <C> can be changed to a <nak>.
  1112.  
  1113.   4.  the initial <nak> from a receiver which wants to receive in checksum
  1114.       can be changed to a <C>.
  1115.  
  1116. The first problem can be solved if the receiver sends a second <C> after
  1117. it times out the first time. This process can be repeated several times.
  1118. It must not be repeated too many times before sending a <nak> and
  1119. switching to checksum mode or a sending program without CRC support may
  1120. time out and abort. Repeating the <C> will also fix the second problem if
  1121. the sending program cooperates by responding as if a <nak> were received
  1122. instead of ignoring the extra <C>.
  1123.  
  1124. It is possible to fix problems 3 and 4 but probably not worth the trouble
  1125. since they will occur very infrequently. They could be fixed by switching
  1126. modes in either the sending or the receiving program after a large number
  1127. of successive <nak>s. This solution would risk other problems however.
  1128.  
  1129.  
  1130. 7.2.3
  1131. The sending program should start in the checksum mode. This will insure
  1132. compatibility with checksum only receiving programs. Anytime a <C> is
  1133. received before the first <nak> or <ack> the sending program should set
  1134. itself into CRC mode and respond as if a <nak> were received. The sender
  1135. should respond to additional <C>s as if they were <nak>s until the first
  1136. e
  1137.  
  1138.  
  1139.