Skip to contentSkip to conversation

mvebu: Fix UART booting

PATCH In review Pali Rohár 3 participants 13 messages Mar 23, 2023 – Mar 24, 2023

lore
Share & tools
lore
#0 lore raw

cover mvebu: Fix UART booting

This patch series contains kwboot fixes for booting non-UART-generated images over UART.

Pali Rohár (5):
tools: kwbimage: Fix invalid UART kwbimage v1 headersz
tools: kwboot: Fix invalid UART kwbimage v1 headersz
tools: kwboot: Fix inserting UART data checksum without -B option
tools: kwboot: Fix sending very small images
tools: kwboot: Workaround A38x BootROM bug for images with a gap

 tools/kwbimage.c | 10 ++++++++++
 tools/kwboot.c   | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
Signature · 1 line
2.20.1
#7 lore raw

PATCH 4/5 tools: kwboot: Fix sending very small images

Sending of very small images (smaller than 128 bytes = xmodem block size) cause out-of-bound memory read access. Fix this issue by ensuring that hdrsz when sending image is not larger than total size of the image. Issue was introduced in commit f8017c37799c ("tools: kwboot: Fix sending Kirkwood v0 images"). Special case when total image is smaller than header size aligned to multiply of xmodem size is already handled since that commit.

Fixes f8017c37799c ("tools: kwboot: Fix sending Kirkwood v0 images")
---
 tools/kwboot.c | 2 ++
 1 file changed, 2 insertions(+)
tools/kwboot.c +2 -0
index 23a893a9b9f8..1cf78dda6755 100644
@@ -1458,6 +1458,8 @@ kwboot_xmodem(int tty, const void *_img, size_t size, int baudrate)
 	 * followed by the header. So align header size to xmodem block size.
 	 */
 	hdrsz += (KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ) % KWBOOT_XM_BLKSZ;
+	if (hdrsz > size)
+		hdrsz = size;
 
 	pnum = 1;
 
Signature · 1 line
2.20.1
#3 lore raw

PATCH 2/5 tools: kwboot: Fix invalid UART kwbimage v1 headersz

In reply to Pali Rohár

Ensure that UART aligned header size is always stored into kwbimage v1 header. It is needed for proper UART booting. Calculation of headersz field was broken in commit d656f5a0ee22 ("tools: kwboot: Calculate real used space in kwbimage header when calling kwboot_img_grow_hdr()") which introduced optimization of kwboot_img_grow_hdr() function.

Fixes d656f5a0ee22 ("tools: kwboot: Calculate real used space in kwbimage header when calling kwboot_img_grow_hdr()")
---
 tools/kwboot.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
tools/kwboot.c +11 -0
index c131711444ec..dd894e80db1c 100644
@@ -2171,6 +2171,17 @@ kwboot_img_patch(void *img, size_t *size, int baudrate)
 
 		kwboot_printv("Aligning image header to Xmodem block size\n");
 		kwboot_img_grow_hdr(img, size, grow);
+		hdrsz += grow;
+
+		/*
+		 * kwbimage v1 contains header size field and for UART type it
+		 * must be set to the aligned xmodem header size because BootROM
+		 * rounds header size down to xmodem block size.
+		 */
+		if (kwbimage_version(img) == 1) {
+			hdr->headersz_msb = hdrsz >> 16;
+			hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff);
+		}
 	}
 
 	hdr->checksum = kwboot_hdr_csum8(hdr) - csum;
Signature · 1 line
2.20.1
#5 lore raw

PATCH 3/5 tools: kwboot: Fix inserting UART data checksum without -B option

In reply to Pali Rohár

Commit 7665ed2fa04e ("tools: kwboot: Fix parsing UART image without data checksum") added fixup code to insert place for data checksum if UART image does not have it. Together with option -B (change baudrate), kwboot calculates this checksum. Without option -B, it inserts only place for checksum but does not calculate it.

This commit fix above logic and calculate data checksum also when kwboot is used without -B option.

Fixes 7665ed2fa04e ("tools: kwboot: Fix parsing UART image without data checksum")
---
 tools/kwboot.c | 2 ++
 1 file changed, 2 insertions(+)
tools/kwboot.c +2 -0
index dd894e80db1c..23a893a9b9f8 100644
@@ -2082,6 +2082,8 @@ kwboot_img_patch(void *img, size_t *size, int baudrate)
 			goto err;
 		}
 		kwboot_img_grow_data_right(img, size, sizeof(uint32_t));
+		/* Update the 32-bit data checksum */
+		*kwboot_img_csum32_ptr(img) = kwboot_img_csum32(img);
 	}
 
 	if (!kwboot_img_has_ddr_init(img) &&
Signature · 1 line
2.20.1
#9 lore raw

PATCH 5/5 tools: kwboot: Workaround A38x BootROM bug for images with a gap

In reply to Pali Rohár

A38x BootROM has a bug which cause that BootROM loads data part of UART image into RAM target address increased by one byte when source address and header size stored in the image header are not same.

Workaround this bug by completely removing a gap between header and data part of the UART image. Without gap, this BootROM bug is not triggered.

This gap can be present in SDIO or SATA image types which have aligned start of the data part to the media sector size. With this workaround kwboot should be able to convert and send SDIO or SATA images for UART booting.

---
 tools/kwboot.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
tools/kwboot.c +23 -0
index 1cf78dda6755..2b92966919da 100644
@@ -78,6 +78,17 @@
  *
  * - IBR_HDR_UART_ID (0x69):
  *   UART image can be transfered via xmodem protocol over first UART.
+ *   Unlike all other image types, header size stored in the image must be
+ *   multiply of the 128 bytes (for all other image types it can be any size)
+ *   and data part of the image does not have to contain 32-bit checksum
+ *   (all other image types must have valid 32-bit checksum in its data part).
+ *   And data size stored in the image is ignored. A38x BootROM determinates
+ *   size of the data part implicitly by the end of the xmodem transfer.
+ *   A38x BootROM has a bug which cause that BootROM loads data part of UART
+ *   image into RAM target address increased by one byte when source address
+ *   and header size stored in the image header are not same. So UART image
+ *   should be constructed in a way that there is no gap between header and
+ *   data part.
  *
  * - IBR_HDR_I2C_ID (0x4D):
  *   It is unknown for what kind of storage is used this image. It is not
@@ -2188,6 +2199,18 @@ kwboot_img_patch(void *img, size_t *size, int baudrate)
 		}
 	}
 
+	/* Header size and source address must be same for UART type due to A38x BootROM bug */
+	if (hdrsz != le32_to_cpu(hdr->srcaddr)) {
+		if (is_secure) {
+			fprintf(stderr, "Cannot align image with secure header\n");
+			goto err;
+		}
+
+		kwboot_printv("Removing gap between image header and data\n");
+		memmove(img + hdrsz, img + le32_to_cpu(hdr->srcaddr), le32_to_cpu(hdr->blocksize));
+		hdr->srcaddr = cpu_to_le32(hdrsz);
+	}
+
 	hdr->checksum = kwboot_hdr_csum8(hdr) - csum;
 
 	*size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize);
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/5 tools: kwbimage: Fix invalid UART kwbimage v1 headersz

In reply to Pali Rohár

Armada 385 BootROM ignores low 7 bits of headersz when parsing kwbimage header of UART type, which effectively means that headersz is rounded down to multiply of 128 bytes. For all other image types BootROM reads and use all bits of headersz. Therefore fill into UART type of kwbimage v1 headersz aligned to 128 bytes.

Fixes 2b0980c24027 ("tools: kwbimage: Fill the real header size into the main header")
---
 tools/kwbimage.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/kwbimage.c +10 -0
index 309657a5637b..177084adf825 100644
@@ -1231,6 +1231,16 @@ static size_t image_headersz_v1(int *hasext)
 	if (count > 0)
 		headersz += sizeof(struct register_set_hdr_v1) + 8 * count + 4;
 
+	/*
+	 * For all images except UART, headersz stored in header itself should
+	 * contains header size without padding. For UART image BootROM rounds
+	 * down headersz to multiply of 128 bytes. Therefore align UART headersz
+	 * to multiply of 128 bytes to ensure that remaining UART header bytes
+	 * are not ignored by BootROM.
+	 */
+	if (image_get_bootfrom() == IBR_HDR_UART_ID)
+		headersz = ALIGN(headersz, 128);
+
 	return headersz;
 }
 
Signature · 1 line
2.20.1
Tested-by on the v1 series
#11 lore raw
In reply to Pali Rohár
Quoted 11 lines · Pali Rohár

This patch series contains kwboot fixes for booting non-UART-generated images over UART.

Pali Rohár (5):
tools: kwbimage: Fix invalid UART kwbimage v1 headersz
tools: kwboot: Fix invalid UART kwbimage v1 headersz
tools: kwboot: Fix inserting UART data checksum without -B option
tools: kwboot: Fix sending very small images
tools: kwboot: Workaround A38x BootROM bug for images with a gap

Quoted 7 lines
 tools/kwbimage.c | 10 ++++++++++
 tools/kwboot.c   | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)

--
2.20.1

Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH u-boot-mvebu 1/5] tools: kwbimage: Fix invalid UART kwbimage v1 headersz

In reply to Pali Rohár
Quoted 8 lines · Pali Rohár

Armada 385 BootROM ignores low 7 bits of headersz when parsing kwbimage header of UART type, which effectively means that headersz is rounded down to multiply of 128 bytes. For all other image types BootROM reads and use all bits of headersz. Therefore fill into UART type of kwbimage v1 headersz aligned to 128 bytes.

Fixes 2b0980c24027 ("tools: kwbimage: Fill the real header size into the main header")

Thanks,
Stefan

Quoted 15 lines
---
  tools/kwbimage.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
tools/kwbimage.c +3 -0
index 309657a5637b..177084adf825 100644
@@ -1231,6 +1231,16 @@ static size_t image_headersz_v1(int *hasext)
  	if (count > 0)
  		headersz += sizeof(struct register_set_hdr_v1) + 8 * count + 4;
 
+	/*
+	 * For all images except UART, headersz stored in header itself should
+	 * contains header size without padding. For UART image BootROM rounds

tools/kwbimage.c static size_t image_headersz_v1(int *hasext) · line 1246

Quoted 10 lines
tools/kwbimage.c +7 -0
+	 * down headersz to multiply of 128 bytes. Therefore align UART headersz
+	 * to multiply of 128 bytes to ensure that remaining UART header bytes
+	 * are not ignored by BootROM.
+	 */
+	if (image_get_bootfrom() == IBR_HDR_UART_ID)
+		headersz = ALIGN(headersz, 128);
+
  	return headersz;
  }
 

Viele Grüße,
Stefan Roese

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH u-boot-mvebu 2/5] tools: kwboot: Fix invalid UART kwbimage v1 headersz

In reply to Pali Rohár
Quoted 8 lines · Pali Rohár

Ensure that UART aligned header size is always stored into kwbimage v1 header. It is needed for proper UART booting. Calculation of headersz field was broken in commit d656f5a0ee22 ("tools: kwboot: Calculate real used space in kwbimage header when calling kwboot_img_grow_hdr()") which introduced optimization of kwboot_img_grow_hdr() function.

Fixes d656f5a0ee22 ("tools: kwboot: Calculate real used space in kwbimage header when calling kwboot_img_grow_hdr()")

Thanks,
Stefan

Quoted 16 lines
---
  tools/kwboot.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
tools/kwboot.c +4 -0
index c131711444ec..dd894e80db1c 100644
@@ -2171,6 +2171,17 @@ kwboot_img_patch(void *img, size_t *size, int baudrate)
 
  		kwboot_printv("Aligning image header to Xmodem block size\n");
  		kwboot_img_grow_hdr(img, size, grow);
+		hdrsz += grow;
+
+		/*
+		 * kwbimage v1 contains header size field and for UART type it

tools/kwboot.c kwboot_img_patch(void *img, size_t *size, int baudrate) · line 2187

Quoted 10 lines
tools/kwboot.c +7 -0
+		 * must be set to the aligned xmodem header size because BootROM
+		 * rounds header size down to xmodem block size.
+		 */
+		if (kwbimage_version(img) == 1) {
+			hdr->headersz_msb = hdrsz >> 16;
+			hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff);
+		}
  	}
 
  	hdr->checksum = kwboot_hdr_csum8(hdr) - csum;

Viele Grüße,
Stefan Roese

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
Reviewed-by on v1 3/5
#6 lore raw

Re: [PATCH u-boot-mvebu 3/5] tools: kwboot: Fix inserting UART data checksum without -B option

In reply to Pali Rohár
Quoted 11 lines · Pali Rohár

Commit 7665ed2fa04e ("tools: kwboot: Fix parsing UART image without data checksum") added fixup code to insert place for data checksum if UART image does not have it. Together with option -B (change baudrate), kwboot calculates this checksum. Without option -B, it inserts only place for checksum but does not calculate it.

This commit fix above logic and calculate data checksum also when kwboot is used without -B option.

Fixes 7665ed2fa04e ("tools: kwboot: Fix parsing UART image without data checksum")

Thanks,
Stefan

Quoted 9 lines
---
  tools/kwboot.c | 2 ++
  1 file changed, 2 insertions(+)
tools/kwboot.c +0 -0
index dd894e80db1c..23a893a9b9f8 100644

tools/kwboot.c kwboot_img_patch(void *img, size_t *size, int baudrate) · line 2089

Quoted 8 lines
tools/kwboot.c +2 -0
@@ -2082,6 +2082,8 @@ kwboot_img_patch(void *img, size_t *size, int baudrate)
  			goto err;
  		}
  		kwboot_img_grow_data_right(img, size, sizeof(uint32_t));
+		/* Update the 32-bit data checksum */
+		*kwboot_img_csum32_ptr(img) = kwboot_img_csum32(img);
  	}
 
  	if (!kwboot_img_has_ddr_init(img) &&

Viele Grüße,
Stefan Roese

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
Reviewed-by on v1 4/5
#8 lore raw

Re: [PATCH u-boot-mvebu 4/5] tools: kwboot: Fix sending very small images

In reply to Pali Rohár
Quoted 10 lines · Pali Rohár

Sending of very small images (smaller than 128 bytes = xmodem block size) cause out-of-bound memory read access. Fix this issue by ensuring that hdrsz when sending image is not larger than total size of the image. Issue was introduced in commit f8017c37799c ("tools: kwboot: Fix sending Kirkwood v0 images"). Special case when total image is smaller than header size aligned to multiply of xmodem size is already handled since that commit.

Fixes f8017c37799c ("tools: kwboot: Fix sending Kirkwood v0 images")

Thanks,
Stefan

Quoted 9 lines
---
  tools/kwboot.c | 2 ++
  1 file changed, 2 insertions(+)
tools/kwboot.c +0 -0
index 23a893a9b9f8..1cf78dda6755 100644

tools/kwboot.c kwboot_xmodem(int tty, const void *_img, size_t size, int baudrate) · line 1465

Quoted 8 lines
tools/kwboot.c +2 -0
@@ -1458,6 +1458,8 @@ kwboot_xmodem(int tty, const void *_img, size_t size, int baudrate)
  	 * followed by the header. So align header size to xmodem block size.
  	 */
  	hdrsz += (KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ) % KWBOOT_XM_BLKSZ;
+	if (hdrsz > size)
+		hdrsz = size;
 
  	pnum = 1;
 

Viele Grüße,
Stefan Roese

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
Reviewed-by on v1 5/5
#10 lore raw

Re: [PATCH u-boot-mvebu 5/5] tools: kwboot: Workaround A38x BootROM bug for images with a gap

In reply to Pali Rohár
Quoted 7 lines · Pali Rohár

A38x BootROM has a bug which cause that BootROM loads data part of UART image into RAM target address increased by one byte when source address and header size stored in the image header are not same.

Workaround this bug by completely removing a gap between header and data part of the UART image. Without gap, this BootROM bug is not triggered.

Quoted 6 lines

This gap can be present in SDIO or SATA image types which have aligned start of the data part to the media sector size. With this workaround kwboot should be able to convert and send SDIO or SATA images for UART booting.

Thanks,
Stefan

Quoted 35 lines
---
  tools/kwboot.c | 23 +++++++++++++++++++++++
  1 file changed, 23 insertions(+)
tools/kwboot.c +16 -0
index 1cf78dda6755..2b92966919da 100644
@@ -78,6 +78,17 @@
   *
   * - IBR_HDR_UART_ID (0x69):
   *   UART image can be transfered via xmodem protocol over first UART.
+ *   Unlike all other image types, header size stored in the image must be
+ *   multiply of the 128 bytes (for all other image types it can be any size)
+ *   and data part of the image does not have to contain 32-bit checksum
+ *   (all other image types must have valid 32-bit checksum in its data part).
+ *   And data size stored in the image is ignored. A38x BootROM determinates
+ *   size of the data part implicitly by the end of the xmodem transfer.
+ *   A38x BootROM has a bug which cause that BootROM loads data part of UART
+ *   image into RAM target address increased by one byte when source address
+ *   and header size stored in the image header are not same. So UART image
+ *   should be constructed in a way that there is no gap between header and
+ *   data part.
   *
   * - IBR_HDR_I2C_ID (0x4D):
   *   It is unknown for what kind of storage is used this image. It is not
@@ -2188,6 +2199,18 @@ kwboot_img_patch(void *img, size_t *size, int baudrate)
  		}
  	}
 
+	/* Header size and source address must be same for UART type due to A38x BootROM bug */
+	if (hdrsz != le32_to_cpu(hdr->srcaddr)) {
+		if (is_secure) {
+			fprintf(stderr, "Cannot align image with secure header\n");
+			goto err;

tools/kwboot.c kwboot_img_patch(void *img, size_t *size, int baudrate) · line 2216

Quoted 10 lines
tools/kwboot.c +7 -0
+		}
+
+		kwboot_printv("Removing gap between image header and data\n");
+		memmove(img + hdrsz, img + le32_to_cpu(hdr->srcaddr), le32_to_cpu(hdr->blocksize));
+		hdr->srcaddr = cpu_to_le32(hdrsz);
+	}
+
  	hdr->checksum = kwboot_hdr_csum8(hdr) - csum;
 
  	*size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize);

Viele Grüße,
Stefan Roese

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
#12 lore raw
In reply to Pali Rohár
Quoted 10 lines · Pali Rohár

This patch series contains kwboot fixes for booting non-UART-generated images over UART.

Pali Rohár (5):
tools: kwbimage: Fix invalid UART kwbimage v1 headersz
tools: kwboot: Fix invalid UART kwbimage v1 headersz
tools: kwboot: Fix inserting UART data checksum without -B option
tools: kwboot: Fix sending very small images
tools: kwboot: Workaround A38x BootROM bug for images with a gap

Quoted 4 lines
  tools/kwbimage.c | 10 ++++++++++
  tools/kwboot.c   | 38 ++++++++++++++++++++++++++++++++++++++
  2 files changed, 48 insertions(+)

Applied to u-boot-marvell/next

Thanks,
Stefan