[PATCH 05/26] bootscan: fix detection of GPT

Ahmad Fatoum a.fatoum at barebox.org
Fri Jun 26 01:42:16 PDT 2026


The "EFI PART" magic string identifying the GPT starts at the second
LBA. As an LBA is at least 512 byte in size, allocating only that many
bytes means the code below checking for a GPT will never succeed.

Bump up the buffer size to FILE_TYPE_SAFE_BUFSIZE to fix this for 512
byte sectors. Support for larger sectors will be added separately.

While at it, also make use of cdev_read's return value, which indicates
the number of bytes actually read into the buffer.

Fixes: ef5dac9c2bf7 ("Implement bootloader spec support for barebox")
Fixes: 718ae461eb38 ("common: bootscan: add scan_disk callback")
Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
 common/bootscan.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/common/bootscan.c b/common/bootscan.c
index faaceaa22152..1c865c6b1382 100644
--- a/common/bootscan.c
+++ b/common/bootscan.c
@@ -85,7 +85,8 @@ int boot_scan_cdev(struct bootscanner *scanner,
 		   bool autodiscover)
 {
 	int ret, found = 0;
-	void *buf = xzalloc(512);
+	size_t bufsize, readsize;
+	void *buf;
 	enum filetype type, filetype;
 	const char *rootpath;
 
@@ -96,14 +97,20 @@ int boot_scan_cdev(struct bootscanner *scanner,
 		return 0;
 	}
 
-	ret = cdev_read(cdev, buf, 512, 0, 0);
+	bufsize = FILE_TYPE_SAFE_BUFSIZE;
+
+	buf = xzalloc(bufsize);
+
+	ret = cdev_read(cdev, buf, bufsize, 0, 0);
 	if (ret < 0) {
 		free(buf);
 		return ret;
 	}
 
-	type = file_detect_partition_table(buf, 512);
-	filetype = file_detect_type(buf, 512);
+	readsize = ret;
+
+	type = file_detect_partition_table(buf, readsize);
+	filetype = file_detect_type(buf, readsize);
 	free(buf);
 
 	if (type == filetype_mbr || type == filetype_gpt) {
-- 
2.47.3




More information about the barebox mailing list