mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 11/26] filetype: don't hardcode sector size in file_detect_partition_table
Date: Fri, 26 Jun 2026 10:42:22 +0200	[thread overview]
Message-ID: <20260626084608.1388806-12-a.fatoum@barebox.org> (raw)
In-Reply-To: <20260626084608.1388806-1-a.fatoum@barebox.org>

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@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




  parent reply	other threads:[~2026-06-26  8:51 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-26  8:42 [PATCH 00/26] block: add support for non-512 byte sectors Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 01/26] nvme: use barebox-appropriate 64-bit type for timeouts Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 02/26] nvme: fix buffer advancement when chunking due to max_hw_sectors Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 03/26] nvme: allow flush opcode Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 04/26] nvme: honor namespace block size for I/O Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 05/26] bootscan: fix detection of GPT Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 06/26] block: clarify that writebuffer_io_len returns sector counts Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 07/26] block: fix wrong type for discard_start/size byte ranges Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 08/26] block: fix discard zeroing too little memory Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 09/26] block: use logical block size for reparse checks Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 10/26] block: require lower bound of sector size to be 512 bytes Ahmad Fatoum
2026-06-26  8:42 ` Ahmad Fatoum [this message]
2026-06-26  8:42 ` [PATCH 12/26] block: define helpers for non-512-byte sector support Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 13/26] bootscan: use block size for partition table probe Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 14/26] ramdisk: validate exported sector size Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 15/26] efi: block: fix sector size mismatch in block device registration and ops Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 16/26] efi: loader: disk: report block device size in Block I/O Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 17/26] efi: loader: file: report cdev block size in file info Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 18/26] partitions: use byte offset for first partition policy Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 19/26] partitions: dos: allocate correctly sized buffer for dos_partition_desc Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 20/26] partition: support non-512 byte sectors Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 21/26] fs: fat: fix garbage read when writing with bigger block size Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 22/26] fs: fat: support larger block device sectors Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 23/26] usb-storage: preserve READ CAPACITY sector size Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 24/26] fuzz: add 4K-sector partition ramdisk target Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 25/26] commands: parted: prepare use of non-512-byte sectors Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 26/26] commands: parted: exit if block size if not 512 Ahmad Fatoum

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260626084608.1388806-12-a.fatoum@barebox.org \
    --to=a.fatoum@barebox.org \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox