[PATCH 11/26] filetype: don't hardcode sector size in file_detect_partition_table

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


The GPT is identified by the second LBA starting with "EFI PART".
file_detect_partition_table() checks for that, but implicitly assumes LBA
size to be 512 byte, which precludes using it to detect partitions
tables on block device with e.g. 4K block sizes.

In preparation for fixing that, adapt file_detect_partition_table() to
always take a sector size.

No functional change.

Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
 commands/createnv.c     |  2 +-
 common/bootscan.c       |  3 ++-
 common/filetype.c       | 16 +++++++++-------
 common/partitions.c     |  4 ++--
 common/partitions/efi.c |  3 ++-
 include/filetype.h      |  3 ++-
 6 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/commands/createnv.c b/commands/createnv.c
index df03664be112..0c74637a114c 100644
--- a/commands/createnv.c
+++ b/commands/createnv.c
@@ -77,7 +77,7 @@ static int do_createnv(int argc, char *argv[])
 		goto err;
 	}
 
-	filetype = file_detect_partition_table(buf, 2 * SECTOR_SIZE);
+	filetype = file_detect_partition_table(buf, 2 * SECTOR_SIZE, SECTOR_SIZE);
 
 	switch (filetype) {
 	case filetype_gpt:
diff --git a/common/bootscan.c b/common/bootscan.c
index 1c865c6b1382..5b70024a75aa 100644
--- a/common/bootscan.c
+++ b/common/bootscan.c
@@ -4,6 +4,7 @@
 #include <driver.h>
 #include <xfuncs.h>
 #include <block.h>
+#include <disks.h>
 #include <fs.h>
 #include <linux/stat.h>
 #include <linux/err.h>
@@ -109,7 +110,7 @@ int boot_scan_cdev(struct bootscanner *scanner,
 
 	readsize = ret;
 
-	type = file_detect_partition_table(buf, readsize);
+	type = file_detect_partition_table(buf, readsize, SECTOR_SIZE);
 	filetype = file_detect_type(buf, readsize);
 	free(buf);
 
diff --git a/common/filetype.c b/common/filetype.c
index 80b7e1d98ce0..01cef7f1fbb7 100644
--- a/common/filetype.c
+++ b/common/filetype.c
@@ -157,16 +157,16 @@ static inline int pmbr_part_valid(const uint8_t *buf)
  * Validity depends on three things:
  *  1) MSDOS signature is in the last two bytes of the MBR
  *  2) One partition of type 0xEE is found
- *  3) EFI GPT signature is at offset 512
+ *  3) EFI GPT signature is at the next logical block
  */
-static int is_gpt_valid(const uint8_t *buf)
+static int is_gpt_valid(const uint8_t *buf, unsigned int sector_size)
 {
 	int i;
 
 	if (get_unaligned_le16(&buf[BS_55AA]) != 0xAA55)
 		return 0;
 
-	if (strncmp(&buf[512], "EFI PART", 8))
+	if (strncmp(buf + sector_size, "EFI PART", 8))
 		return 0;
 
 	buf += MBR_Table;
@@ -268,19 +268,21 @@ enum filetype is_fat_or_mbr(const unsigned char *sector, unsigned long *bootsec)
 	return filetype_mbr;
 }
 
-enum filetype file_detect_partition_table(const void *_buf, size_t bufsize)
+enum filetype file_detect_partition_table(const void *_buf,
+					  size_t bufsize,
+					  unsigned int sector_size)
 {
 	const u8 *buf8 = _buf;
 	enum filetype type;
 
-	if (bufsize < 512)
+	if (bufsize < MIN_SECTOR_SIZE || sector_size < MIN_SECTOR_SIZE)
 		return filetype_unknown;
 
 	/*
 	 * EFI GPT need to be detected before MBR otherwise
 	 * we will detect a MBR
 	 */
-	if (bufsize >= 520 && is_gpt_valid(buf8))
+	if (bufsize >= sector_size + 8 && is_gpt_valid(buf8, sector_size))
 		return filetype_gpt;
 
 	type = is_fat_or_mbr(buf8, NULL);
@@ -468,7 +470,7 @@ enum filetype file_detect_type(const void *_buf, size_t bufsize)
 	if (bufsize < 512)
 		return filetype_unknown;
 
-	type = file_detect_partition_table(_buf, bufsize);
+	type = file_detect_partition_table(_buf, bufsize, SECTOR_SIZE);
 	if (type != filetype_unknown)
 		return type;
 
diff --git a/common/partitions.c b/common/partitions.c
index 40f4c629e1ac..ff977516aa1f 100644
--- a/common/partitions.c
+++ b/common/partitions.c
@@ -97,7 +97,7 @@ static struct partition_parser *partition_parser_get_by_filetype(uint8_t *buf)
 	struct partition_parser *parser;
 
 	/* first new partition table as EFI GPT */
-	type = file_detect_partition_table(buf, SECTOR_SIZE * 2);
+	type = file_detect_partition_table(buf, SECTOR_SIZE * 2, SECTOR_SIZE);
 
 	list_for_each_entry(parser, &partition_parser_list, list) {
 		if (parser->type == type)
@@ -108,7 +108,7 @@ static struct partition_parser *partition_parser_get_by_filetype(uint8_t *buf)
 	 * so if EFI GPT not enable take it as MBR
 	 * useful for compatibility
 	 */
-	type = file_detect_partition_table(buf, SECTOR_SIZE);
+	type = file_detect_partition_table(buf, SECTOR_SIZE, SECTOR_SIZE);
 	if (type == filetype_fat && !is_fat_boot_sector(buf))
 		type = filetype_mbr;
 
diff --git a/common/partitions/efi.c b/common/partitions/efi.c
index 674f920743d3..4847f88fa551 100644
--- a/common/partitions/efi.c
+++ b/common/partitions/efi.c
@@ -505,7 +505,8 @@ static int find_valid_gpt(struct efi_partition_desc *epd, void *buf)
 	lastlba = last_lba(blk);
 	if (force_gpt) {
 		/* This will be added to the EFI Spec. per Intel after v1.02. */
-		if (file_detect_partition_table(buf, SECTOR_SIZE * 2) != filetype_gpt)
+		if (file_detect_partition_table(buf, SECTOR_SIZE * 2,
+						SECTOR_SIZE) != filetype_gpt)
 			goto fail;
 	}
 
diff --git a/include/filetype.h b/include/filetype.h
index 759ef9c2a343..2fd0e2217729 100644
--- a/include/filetype.h
+++ b/include/filetype.h
@@ -81,7 +81,8 @@ struct cdev;
 
 const char *file_type_to_string(enum filetype f);
 const char *file_type_to_short_string(enum filetype f);
-enum filetype file_detect_partition_table(const void *_buf, size_t bufsize);
+enum filetype file_detect_partition_table(const void *_buf, size_t bufsize,
+					  unsigned int sector_size);
 enum filetype file_detect_compression_type(const void *_buf, size_t bufsize);
 enum filetype file_detect_fs_type(const void *_buf, size_t bufsize);
 enum filetype file_detect_type(const void *_buf, size_t bufsize);
-- 
2.47.3




More information about the barebox mailing list