From: Mihai Moldovan <ionic@ionic.de>
To: The development of GNU GRUB <grub-devel@gnu.org>
Subject: [PATCH 7/7] gpt: respect native sector size if set/detected
Date: Sun, 24 May 2020 13:43:08 +0200 [thread overview]
Message-ID: <20200524114308.1009-8-ionic@ionic.de> (raw)
In-Reply-To: <20200524114308.1009-1-ionic@ionic.de>
The GPT parsing code includes some autodetection magic for non-default
sector sizes.
This is working fine if the underlaying disk abstraction uses the
default sector size of 512 bytes, but will return wrong information (and
read invalid data!) if it is not.
Fix this by skipping the sector size autodetection if the disk's sector
size is not the default one and trust it to be correct for subsequent
sector calculations.
---
grub-core/partmap/gpt.c | 35 +++++++++++++++++++++++++++++------
1 file changed, 29 insertions(+), 6 deletions(-)
diff --git a/grub-core/partmap/gpt.c b/grub-core/partmap/gpt.c
index 4b968529a..51d752522 100644
--- a/grub-core/partmap/gpt.c
+++ b/grub-core/partmap/gpt.c
@@ -58,7 +58,7 @@ grub_gpt_partition_map_iterate (grub_disk_t disk,
grub_uint64_t entries;
unsigned int i;
int last_offset = 0;
- int sector_log = 0;
+ unsigned int sector_log = 0;
/* Read the protective MBR. */
if (grub_disk_read (disk, 0, 0, sizeof (mbr), &mbr))
@@ -76,16 +76,39 @@ grub_gpt_partition_map_iterate (grub_disk_t disk,
return grub_error (GRUB_ERR_BAD_PART_TABLE, "no GPT partition map found");
/* Read the GPT header. */
- for (sector_log = 0; sector_log < MAX_SECTOR_LOG; sector_log++)
+ if (disk->log_sector_size > GRUB_DISK_SECTOR_BITS)
{
+ /*
+ * If the disk sector size has been set to value different from the
+ * default/internal one, it means that *something* autodetected the
+ * native sector size and we don't have to and shouldn't try to use a
+ * sector size heuristic.
+ */
+ sector_log = disk->log_sector_size - GRUB_DISK_SECTOR_BITS;
+
if (grub_disk_read (disk, 1 << sector_log, 0, sizeof (gpt), &gpt))
return grub_errno;
- if (grub_memcmp (gpt.magic, grub_gpt_magic, sizeof (grub_gpt_magic)) == 0)
- break;
+ if (grub_memcmp (gpt.magic, grub_gpt_magic, sizeof (grub_gpt_magic)) != 0)
+ return grub_error (GRUB_ERR_BAD_PART_TABLE, "no valid GPT header");
+ }
+ else
+ {
+ /*
+ * Otherwise, the native sector size might be wrong, so try to
+ * autodetect.
+ */
+ for (sector_log = 0; sector_log < MAX_SECTOR_LOG; sector_log++)
+ {
+ if (grub_disk_read (disk, 1 << sector_log, 0, sizeof (gpt), &gpt))
+ return grub_errno;
+
+ if (grub_memcmp (gpt.magic, grub_gpt_magic, sizeof (grub_gpt_magic)) == 0)
+ break;
+ }
+ if (sector_log == MAX_SECTOR_LOG)
+ return grub_error (GRUB_ERR_BAD_PART_TABLE, "no valid GPT header");
}
- if (sector_log == MAX_SECTOR_LOG)
- return grub_error (GRUB_ERR_BAD_PART_TABLE, "no valid GPT header");
grub_dprintf ("gpt", "Read a valid GPT header\n");
--
2.25.1
prev parent reply other threads:[~2020-05-24 11:43 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-24 11:43 [PATCH 0/7] support >512b sector disks with old/buggy firmware Mihai Moldovan
2020-05-24 11:43 ` [PATCH 1/7] biosdisk: autodetect hardware sector size (opt-in) Mihai Moldovan
2020-05-24 11:43 ` [PATCH 2/7] biosdisk: restore LBA mode after read/write failures Mihai Moldovan
2020-05-24 11:43 ` [PATCH 3/7] setup: add support for native sector addressing w/ 512-bytes lengths Mihai Moldovan
2020-05-24 11:43 ` [PATCH 4/7] grub-install: hook up --emu-512b to sector size autodetection in biosdisk Mihai Moldovan
2020-05-24 11:43 ` [PATCH 5/7] docs/grub: document --emu-512b install option Mihai Moldovan
2020-05-24 11:43 ` [PATCH 6/7] diskfilter: write out currently scanned partition Mihai Moldovan
2020-05-24 11:43 ` Mihai Moldovan [this message]
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=20200524114308.1009-8-ionic@ionic.de \
--to=ionic@ionic.de \
--cc=grub-devel@gnu.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.