Blame SOURCES/0319-osdep-linux-hostdisk-Modify-sector-by-sysfs-as-disk-.patch

b35c50
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b35c50
From: Mukesh Kumar Chaurasiya <mchauras@linux.vnet.ibm.com>
b35c50
Date: Thu, 9 Feb 2023 13:09:16 +0530
b35c50
Subject: [PATCH] osdep/linux/hostdisk: Modify sector by sysfs as disk sector
b35c50
b35c50
The disk sector size provided by sysfs file system considers the sector
b35c50
size of 512 irrespective of disk sector size, thus causing the read by
b35c50
the GRUB to an incorrect offset from what was originally intended.
b35c50
b35c50
Considering the 512 sector size of sysfs data the actual sector needs to
b35c50
be modified corresponding to disk sector size.
b35c50
b35c50
Signed-off-by: Mukesh Kumar Chaurasiya <mchauras@linux.vnet.ibm.com>
b35c50
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
b35c50
(cherry picked from commit f7564844f82b57078d601befadc438b5bc1fa01b)
b35c50
---
b35c50
 grub-core/osdep/linux/hostdisk.c | 7 ++++---
b35c50
 include/grub/disk.h              | 7 +++++++
b35c50
 2 files changed, 11 insertions(+), 3 deletions(-)
b35c50
b35c50
diff --git a/grub-core/osdep/linux/hostdisk.c b/grub-core/osdep/linux/hostdisk.c
b35c50
index 7bc99ac1c1..a9ea0bb465 100644
b35c50
--- a/grub-core/osdep/linux/hostdisk.c
b35c50
+++ b/grub-core/osdep/linux/hostdisk.c
b35c50
@@ -240,7 +240,8 @@ have_devfs (void)
b35c50
 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
b35c50
 
b35c50
 static int
b35c50
-grub_hostdisk_linux_find_partition (char *dev, grub_disk_addr_t sector)
b35c50
+grub_hostdisk_linux_find_partition (const grub_disk_t disk, char *dev,
b35c50
+                                    grub_disk_addr_t sector)
b35c50
 {
b35c50
   size_t len = strlen (dev);
b35c50
   const char *format;
b35c50
@@ -305,7 +306,7 @@ grub_hostdisk_linux_find_partition (char *dev, grub_disk_addr_t sector)
b35c50
       if (fstat (fd, &st) < 0
b35c50
 	  || !grub_util_device_is_mapped_stat (&st)
b35c50
 	  || !grub_util_get_dm_node_linear_info (st.st_rdev, 0, 0, &start))
b35c50
-	start = grub_util_find_partition_start_os (real_dev);
b35c50
+	start = grub_disk_to_native_sector (disk, grub_util_find_partition_start_os (real_dev));
b35c50
       /* We don't care about errors here.  */
b35c50
       grub_errno = GRUB_ERR_NONE;
b35c50
 
b35c50
@@ -386,7 +387,7 @@ grub_util_fd_open_device (const grub_disk_t disk, grub_disk_addr_t sector, int f
b35c50
 	&& strncmp (dev, "/dev/", 5) == 0)
b35c50
       {
b35c50
 	if (sector >= part_start)
b35c50
-	  is_partition = grub_hostdisk_linux_find_partition (dev, part_start);
b35c50
+	  is_partition = grub_hostdisk_linux_find_partition (disk, dev, part_start);
b35c50
 	else
b35c50
 	  *max = part_start - sector;
b35c50
       }
b35c50
diff --git a/include/grub/disk.h b/include/grub/disk.h
b35c50
index 06210a7049..881addcc77 100644
b35c50
--- a/include/grub/disk.h
b35c50
+++ b/include/grub/disk.h
b35c50
@@ -208,6 +208,13 @@ grub_disk_from_native_sector (grub_disk_t disk, grub_disk_addr_t sector)
b35c50
   return sector << (disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
b35c50
 }
b35c50
 
b35c50
+/* Convert from GRUB native disk sized sector to disk sized sector. */
b35c50
+static inline grub_disk_addr_t
b35c50
+grub_disk_to_native_sector (grub_disk_t disk, grub_disk_addr_t sector)
b35c50
+{
b35c50
+  return sector >> (disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
b35c50
+}
b35c50
+
b35c50
 /* This is called from the memory manager.  */
b35c50
 void grub_disk_cache_invalidate_all (void);
b35c50