xyz-modem: only strip trailing ^Z bytes from the final XMODEM block
PATCH 1/1 xyz-modem: only strip trailing ^Z bytes from the final XMODEM block
XMODEM carries no file length, so the sender pads the final block with ^Z and the receiver has to guess where the data ends. This is a fundamental ambiguity in XMODEM which can't be resolved. With that in mind:
xyzModem_stream_read() previously checked every block for trailing ^Z^Z^Z (three EOFs in a row) and stripped all trailing ^Zs for blocks matching this pattern. Two problems:
- A final block padded with one or two ^Zs keeps its padding. This is subject to the design ambiguity in XMODEM and is left alone here.
- In intermediate (non-terminal) blocks, 3 or more ^Zs that are part of the data are removed mid-stream. Each block still passes its CRC check, so loadx reports success. This is unambiguously wrong and is what this patch fixes.
Count and withhold trailing ^Z characters in a block until the next frame arrives. Another data block means they were data; EOT means they were padding. YMODEM transfers usually carry a file length and won't take this path.
Because the bug is keyed by three or more trailing ^Zs in a block, it's unusual (1 in 2^24 per block, assuming random data). We bumped into this with a gzipped disk image with a block ending in seven 0x1a bytes.
---
This patch follows the GNU/RedBoot style used in this file (and hence it sets off checkpatch - happy to follow u-boot conventions instead if you prefer.)
--- common/xyzModem.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-)
common/xyzModem.c +26 -3
@@ -47,6 +47,8 @@ static struct {int *__chan;unsigned char pkt[1024 ], *bufp; +int pad_pending;/* trailing ^Z run withheld from the previous block */ +int pad_deliver;/* ^Z bytes to emit before the next data */ unsigned char blk, cblk, crc1, crc2;unsigned char next_blk;/* Expected block */ int len, mode, total_retries;
@@ -445,6 +447,8 @@ xyzModem_stream_open (connection_info_t * info, int *err)int dummy =0 ; xyz.__chan = &dummy; xyz.len =0 ; + xyz.pad_pending =0 ; + xyz.pad_deliver =0 ; xyz.crc_mode = true; xyz.at_eof = false; xyz.tx_ack = false;
@@ -524,7 +528,7 @@ xyzModem_stream_read (char *buf, int size, int *err)/* Try and get 'size' bytes into the buffer */ while (!xyz.at_eof && xyz.len >=0 && (size >0 )) { -if (xyz.len ==0 ) +if (xyz.len ==0 && xyz.pad_deliver ==0 ) { retries = xyzModem_MAX_RETRIES;while (retries-- >0 )
@@ -551,8 +555,12 @@ xyzModem_stream_read (char *buf, int size, int *err)if (xyz.mode == xyzModem_xmodem || xyz.file_length ==0 ) { -/* Data blocks can be padded with ^Z (EOF) characters */ -/* This code tries to detect and remove them */ +/* Without a file length, EOF padding can't be +* distinguished from block data until the next +* frame arrives and we can check for EOT/EOF. +*/ + xyz.pad_deliver = xyz.pad_pending; + xyz.pad_pending =0 ;if ((xyz.bufp[xyz.len -1 ] ==EOF ) && (xyz.bufp[xyz.len -2 ] ==EOF ) && (xyz.bufp[xyz.len -3 ] ==EOF ))
@@ -561,6 +569,7 @@ xyzModem_stream_read (char *buf, int size, int *err) && (xyz.bufp[xyz.len - 1] == EOF)) { xyz.len--; + xyz.pad_pending++; } } }
@@ -612,6 +621,8 @@ xyzModem_stream_read (char *buf, int size, int *err) }else stat =0 ; + + xyz.pad_pending =0 ;/* EOT: withheld ^Z run was padding */ xyz.at_eof = true;break ; }
@@ -626,6 +637,18 @@ xyzModem_stream_read (char *buf, int size, int *err)return total; } } +/* A ^Z run withheld from the previous block turned out to be data */ +if (xyz.pad_deliver >0 ) + { + len = xyz.pad_deliver; +if (size < len) + len = size; +memset (buf,EOF , len); + size -= len; + buf += len; + total += len; + xyz.pad_deliver -= len; + }/* Don't "read" data from the EOF protocol package */ if (!xyz.at_eof && xyz.len >0 ) {
Signature · 1 line
2.47.3
Quoted 10 lines · Graeme Smecher
XMODEM carries no file length, so the sender pads the final block with ^Z and the receiver has to guess where the data ends. This is a fundamental ambiguity in XMODEM which can't be resolved. With that in mind:
xyzModem_stream_read()previously checked every block for trailing ^Z^Z^Z (three EOFs in a row) and stripped all trailing ^Zs for blocks matching this pattern. Two problems:[...]
Applied to u-boot/main, thanks!
[1/1] xyz-modem: only strip trailing ^Z bytes from the final XMODEM block
commit: 03e0ee96b1cf21a20def03c079ca05427ebff208
Signature · 1 line
Tom