From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1jcph4-0007nY-81 for mharc-grub-devel@gnu.org; Sun, 24 May 2020 08:25:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35352) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jcph0-0007jy-Fz for grub-devel@gnu.org; Sun, 24 May 2020 08:25:26 -0400 Received: from ionic.de ([87.98.244.45]:55037 helo=mail.ionic.de) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jcpgz-0004kf-Mf for grub-devel@gnu.org; Sun, 24 May 2020 08:25:26 -0400 Received: from apgunner.local.home.ionic.de (home.ionic.de [217.92.117.31]) by mail.ionic.de (Postfix) with ESMTPSA id 9EC044F0BA19 for ; Sun, 24 May 2020 12:25:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=ionic.de; s=default; t=1590323123; bh=ksR9Q+XyOCUuJnb7m/6rZO4Y6HsPUVfaQuGvoW/i1YE=; h=From:To:Subject:Date:In-Reply-To:References:From; b=lMzceSO3pzJjAnILp5d3TrHTFq2RQsnlaTDRZhBpYVIMAe0dDbJTFVJFnZ0ELjsBn 46521xsLmEIkW6kvWnGPNVlWzGD6zjmjLg8hYhC1ncLtfhpYqRrVHoi+Nzktu4mpR6 Sl5Gu62nWF65eDsCLbm7KRPDR//BecqjcPHcPi4Y= From: Mihai Moldovan To: The development of GNU GRUB Subject: [PATCH v2 7/7] gpt: respect native sector size if set/detected Date: Sun, 24 May 2020 14:25:17 +0200 Message-Id: <20200524122517.5010-8-ionic@ionic.de> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200524122517.5010-1-ionic@ionic.de> References: <20200524122517.5010-1-ionic@ionic.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=87.98.244.45; envelope-from=ionic@ionic.de; helo=mail.ionic.de X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/24 07:43:16 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 24 May 2020 12:25:26 -0000 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..38721275f 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 a value different from the + * default/internal one, it means that *something* autodetected the + * native sector size, we don't have to and also 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