[Concept,02/13] disk: part_efi: Check the block size in part_test_efi()

Message ID 20260220001926.2366140-3-sjg@u-boot.org
State New
Headers
Series ulib: Enable the Rust demo on more architectures |

Commit Message

Simon Glass Feb. 20, 2026, 12:19 a.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

The ALLOC_CACHE_ALIGN_BUFFER_PAD() macro divides by desc->blksz to
compute alignment padding, causing a Divide Error when blksz is 0.
This happens when bootmeth_rauc scans block devices that have no
block size set. This happens on qemu-x86, for example.

Return -ENOENT early when blksz is zero, since a device without a
block size cannot have a valid EFI partition table.

Co-developed-by: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 disk/part_efi.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
  

Patch

diff --git a/disk/part_efi.c b/disk/part_efi.c
index fbed515b303..b48ff518b03 100644
--- a/disk/part_efi.c
+++ b/disk/part_efi.c
@@ -314,9 +314,18 @@  static int __maybe_unused part_get_info_efi(struct blk_desc *desc, int part,
 
 static int part_test_efi(struct blk_desc *desc)
 {
-	ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, desc->blksz);
 	long ret;
 
+	/*
+	 * An ATAPI device (e.g. CD-ROM) may have blksz == 0 if the
+	 * capacity query failed or no media is present.  The buffer
+	 * macro below divides by blksz, so bail out early.
+	 */
+	if (!desc->blksz)
+		return -ENOENT;
+
+	ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, desc->blksz);
+
 	/* Read legacy MBR from block 0 and validate it */
 	ret = blk_dread(desc, 0, 1, (ulong *)legacymbr);
 	if (IS_ERR_VALUE(ret))