Of course. Here is a precise, step-by-step breakdown of how the YMODEM protocol works during a file transfer, which is how a "file update" is accomplished.
YMODEM doesn't have a specific "update" command; it performs a file transfer. The receiver's operating system then handles the creation of a new file or the overwriting of an existing file with the same name.
This explanation details the YMODEM-1k variant, which uses 1024-byte data blocks and CRC-16 for error checking, as this is the most common implementation.
Key Control Characters
First, let's define the single-byte control characters used throughout the process.
| Character | Hex Value | ASCII | Name | Description |
|---|
| SOH | 0x01 | SOH | Start of Header | Marks the beginning of a 128-byte data packet. |
| STX | 0x02 | STX | Start of Text | Marks the beginning of a 1024-byte (1k) data packet. |
| EOT | 0x04 | EOT | End of Transmission | Sent by the sender to mark the end of a single file's data. |
| ACK | 0x06 | ACK | Acknowledge | Sent by the receiver to confirm successful receipt of a packet. |
| NAK | 0x15 | NAK | Negative Acknowledge | Sent by the receiver to request re-transmission of a failed packet. |
| 'C' | 0x43 | C | "C" | Sent by the receiver to start the transfer and indicate it's ready for CRC mode. |
| CAN | 0x18 | CAN | Cancel | Sent to abort the transfer session entirely. |
The YMODEM Transfer Process: Data and Order
Here is the exact sequence of events and data exchanged between the Sender (the device sending the file) and the Receiver (the device receiving the file).
Phase 1: Session Initiation
This phase establishes the connection and prepares for the first file.
- Receiver Initiates: The process starts with the Receiver. It signals to the Sender that it is ready to receive files using the CRC-16 error-checking method. It does this by repeatedly sending the 'C' character until it receives a packet from the sender.
Receiver -> Sender: 'C'
Receiver -> Sender: 'C' (continues periodically, e.g., every 3-10 seconds)
Phase 2: Metadata Transfer (Packet 0)
This is the key feature of YMODEM. The first packet does not contain file data; it contains metadata about the file to be sent.
-
Sender Responds with Packet 0: Upon receiving the 'C', the Sender sends a special packet with packet number 0. This packet is always 128 bytes long.
-
Packet 0 Structure: SOH | 0x00 | 0xFF | [128 bytes of metadata] | [2 bytes of CRC-16]
-
Breakdown of the 128-byte metadata block:
- Filename: The first part is the filename, terminated by a null character (
0x00). Example: firmware.bin\0
- File Size: Immediately following the filename's null terminator is the file size in bytes, represented as a text string, also terminated by a null character. Example:
131072\0
- Padding: The rest of the 128-byte block is padded with null characters (
0x00).
-
Example Metadata Block: For a file named image.dat that is 8192 bytes long, the block would look like this (in ASCII representation, \0 is the null character):
image.dat\08192\0\0\0\0... (padded with nulls to 128 bytes total)
-
Sender -> Receiver: SOH | 0x00 | 0xFF | [filename\0filesize\0...padding...] | CRC-Hi | CRC-Lo
-
Receiver Acknowledges Packet 0: The Receiver gets Packet 0. It validates the CRC. If valid, it parses the filename and file size. It can now open a local file with that name and prepare to receive the specified number of bytes. It acknowledges the packet.
-
Receiver Signals Readiness for Data: After acknowledging the metadata, the Receiver must again signal that it's ready, this time for the actual file content.
Phase 3: File Data Transmission
This phase transfers the actual content of the file and is very similar to the XMODEM-1k protocol.
-
Sender Sends Data Packets: Upon receiving the second 'C', the Sender begins sending the file's content in numbered packets, starting with Packet #1. It uses 1024-byte packets for efficiency.
-
Data Packet Structure: STX | Packet # | 255 - Packet # | [1024 bytes of file data] | [2 bytes of CRC-16]
-
The Packet # starts at 1 and increments for each packet, wrapping from 255 back to 0.
-
The 255 - Packet # is a simple checksum on the packet number itself.
-
Sender -> Receiver: STX | 0x01 | 0xFE | [bytes 1-1024 of file] | CRC-Hi | CRC-Lo
-
Receiver Acknowledges Each Packet: For each correctly received packet (correct packet number, valid CRC), the Receiver writes the data to the file and sends an ACK.
-
Sender Continues: The sender waits for the ACK before sending the next packet in the sequence.
Sender -> Receiver: STX | 0x02 | 0xFD | [bytes 1025-2048 of file] | CRC-Hi | CRC-Lo
Receiver -> Sender: ACK
- ...and so on.
-
Handling the Last Packet: If the file size is not a perfect multiple of 1024, the last packet will be shorter. The protocol specifies that this final, partial packet should be a 128-byte SOH packet. The unused bytes in the data block are padded with a CTRL-Z character (0x1A).
Sender -> Receiver: SOH | N | 255-N | [remaining file data + padding] | CRC-Hi | CRC-Lo
Receiver -> Sender: ACK
Phase 4: End of Single File Transmission
This is a specific two-step handshake to confirm the file is complete.
-
Sender Signals End of File: After sending the last data packet and receiving its ACK, the Sender sends an EOT character.
-
Receiver Acknowledges EOT and Prepares for Next File: The Receiver gets the EOT. It acknowledges this by sending a NAK. This is a unique part of the protocol handshake; it is not an error report.
-
Sender Confirms EOT: The Sender expects this NAK in response to its first EOT. To confirm the end-of-file, it sends a second EOT.
-
Receiver Finalizes File: The Receiver gets the second EOT. It now knows the file transfer is successfully finished. It closes the file on its local disk and sends a final ACK.
Phase 5: Batch Operation (Next File or Session End)
This phase determines if another file will be sent or if the entire batch session is over.
-
Receiver Asks for Next File: Immediately after sending the final ACK for the completed file, the Receiver signals it's ready for another one by sending a 'C'.
-
Sender's Response:
- If more files exist: The Sender goes back to Phase 2, Step 2 and sends a new Packet 0 containing the filename and size of the next file. The entire process repeats.
- If no more files exist: The Sender must explicitly terminate the session. It does this by sending a "null" Packet 0. This is a valid 128-byte packet where the data field is completely empty (all 128 bytes are null
0x00).
Sender -> Receiver: SOH | 0x00 | 0xFF | [128 null bytes] | CRC-Hi | CRC-Lo
-
Session Termination: The Receiver gets this null packet. It understands this as the "End of Batch" signal. It acknowledges it, and the session is complete.
Error Handling and Cancellation
- Packet Corruption: If the receiver gets a packet with a bad CRC or incorrect packet number, it sends a
NAK. The sender must re-transmit the last packet. This can be repeated a set number of times (e.g., 10) before the transfer aborts.
- Cancellation: Either side can abort the entire session at any time by sending two
CAN characters in a row (CAN-CAN).