From 20a6585838ca5ae9fc101739ae97e3a101a1c555 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pali=20Roh=C3=A1r?= Date: Fri, 5 Nov 2021 23:29:58 +0100 Subject: [PATCH] tools: kwboot: Fix sending Kirkwood v0 images MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Properly calculate and align image header size to xmodem block size. Kirkwood v0 images do not have stored total size of header in header structure itself like it is for v1 images. So kwbheader_size() calculates size by traversing image structure itself. Aligning is done in kwboot by putting zero padding bytes between the header and data part. Signed-off-by: Pali Rohár Tested-by: Tony Dinh Reviewed-by: Stefan Roese --- tools/kwboot.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/tools/kwboot.c b/tools/kwboot.c index bacca15301..4e4d544efd 100644 --- a/tools/kwboot.c +++ b/tools/kwboot.c @@ -1073,6 +1073,14 @@ kwboot_xmodem(int tty, const void *_img, size_t size, int baudrate) hdrsz = kwbheader_size(img); + /* + * If header size is not aligned to xmodem block size (which applies + * for all images in kwbimage v0 format) then we have to ensure that + * the last xmodem block of header contains beginning of the data + * followed by the header. So align header size to xmodem block size. + */ + hdrsz += (KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ) % KWBOOT_XM_BLKSZ; + kwboot_printv("Waiting 2s and flushing tty\n"); sleep(2); /* flush isn't effective without it */ tcflush(tty, TCIOFLUSH); @@ -1083,12 +1091,17 @@ kwboot_xmodem(int tty, const void *_img, size_t size, int baudrate) if (rc) return rc; - img += hdrsz; - size -= hdrsz; - - rc = kwboot_xmodem_one(tty, &pnum, 0, img, size, 0); - if (rc) - return rc; + /* + * If we have already sent image data as a part of the last + * xmodem header block then we have nothing more to send. + */ + if (hdrsz < size) { + img += hdrsz; + size -= hdrsz; + rc = kwboot_xmodem_one(tty, &pnum, 0, img, size, 0); + if (rc) + return rc; + } rc = kwboot_xm_finish(tty); if (rc) -- 2.39.5