Skip to contentSkip to conversation

grub-core: modify sector by sysfs as disk sector

PATCH In review Mukesh Kumar Chaurasiya 3 participants 4 messages Feb 9, 2023 – Feb 14, 2023 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH v2 1/1 grub-core: modify sector by sysfs as disk sector

The disk sector size provided by sysfs file system considers the sector size of 512 irrespective of disk sector size, Thus causing the read by grub to an incorrect offset from what was originally intended.

Considering the 512 sector size of sysfs data the actual sector needs to be modified corresponding to disk sector size.

---
 grub-core/osdep/linux/hostdisk.c | 8 +++++---
 include/grub/disk.h              | 7 +++++++
 2 files changed, 12 insertions(+), 3 deletions(-)
grub-core/osdep/linux/hostdisk.c +5 -3
index 07058f63a..fd0769e3b 100644
@@ -198,7 +198,8 @@ have_devfs (void)
 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
 
 static int
-grub_hostdisk_linux_find_partition (char *dev, grub_disk_addr_t sector)
+grub_hostdisk_linux_find_partition (const grub_disk_t disk, char *dev,
+                                    grub_disk_addr_t sector)
 {
   size_t len = strlen (dev);
   const char *format;
@@ -263,7 +264,8 @@ grub_hostdisk_linux_find_partition (char *dev, grub_disk_addr_t sector)
       if (fstat (fd, &st) < 0
 	  || !grub_util_device_is_mapped_stat (&st)
 	  || !grub_util_get_dm_node_linear_info (st.st_rdev, 0, 0, &start))
-	start = grub_util_find_partition_start_os (real_dev);
+        start = grub_disk_to_native_sector (disk,
+                                            grub_util_find_partition_start_os (real_dev));
       /* We don't care about errors here.  */
       grub_errno = GRUB_ERR_NONE;
 
@@ -344,7 +346,7 @@ grub_util_fd_open_device (const grub_disk_t disk, grub_disk_addr_t sector, int f
 	&& strncmp (dev, "/dev/", 5) == 0)
       {
 	if (sector >= part_start)
-	  is_partition = grub_hostdisk_linux_find_partition (dev, part_start);
+	  is_partition = grub_hostdisk_linux_find_partition (disk, dev, part_start);
 	else
 	  *max = part_start - sector;
       }
include/grub/disk.h +7 -0
index 25c141ea2..071b2f7df 100644
@@ -208,6 +208,13 @@ grub_disk_from_native_sector (grub_disk_t disk, grub_disk_addr_t sector)
   return sector << (disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
 }
 
+/* Convert from GRUB native disk sized sector to disk sized sector. */
+static inline grub_disk_addr_t
+grub_disk_to_native_sector (grub_disk_t disk, grub_disk_addr_t sector)
+{
+  return sector >> (disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
+}
+
 /* This is called from the memory manager.  */
 void grub_disk_cache_invalidate_all (void);
 
Signature · 1 line
2.31.1
Reviewed-by on v2 1/1
#2 lore raw
Quoted 9 lines · Mukesh Kumar Chaurasiya

The disk sector size provided by sysfs file system considers the sector size of 512 irrespective of disk sector size, Thus causing the read by grub to an incorrect offset from what was originally intended.

Considering the 512 sector size of sysfs data the actual sector needs to be modified corresponding to disk sector size.

Daniel

#3 lore raw
In reply to Mukesh Kumar Chaurasiya
Quoted 37 lines · On Thu, 9 Feb 2023 13:09:16 +0530 Mukesh Kumar Chaurasiya…

The disk sector size provided by sysfs file system considers the sector size of 512 irrespective of disk sector size, Thus causing the read by grub to an incorrect offset from what was originally intended.

Considering the 512 sector size of sysfs data the actual sector needs to be modified corresponding to disk sector size.

---
 grub-core/osdep/linux/hostdisk.c | 8 +++++---
 include/grub/disk.h              | 7 +++++++
 2 files changed, 12 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/grub-core/osdep/linux/hostdisk.c index 07058f63a..fd0769e3b 100644

grub-core/osdep/linux/hostdisk.c +1 -1
@@ -198,7 +198,8 @@ have_devfs (void)
 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
 
 static int
-grub_hostdisk_linux_find_partition (char *dev, grub_disk_addr_t
 sector) +grub_hostdisk_linux_find_partition (const grub_disk_t disk,
 char *dev,
+                                    grub_disk_addr_t sector)
 {
   size_t len = strlen (dev);
   const char *format;
@@ -263,7 +264,8 @@ grub_hostdisk_linux_find_partition (char *dev,

grub_disk_addr_t sector) if (fstat (fd, &st) < 0

grub-core/osdep/linux/hostdisk.c +0 -0
 	  || !grub_util_device_is_mapped_stat (&st)
 	  || !grub_util_get_dm_node_linear_info (st.st_rdev, 0, 0,

&start))

grub-core/osdep/linux/hostdisk.c +2 -1
-	start = grub_util_find_partition_start_os (real_dev);
+        start = grub_disk_to_native_sector (disk,
+
Quoted 2 lines

grub_util_find_partition_start_os (real_dev)); /* We don't care about errors here. */

There needs to be a space to tab conversion here on both lines. Perhaps Daniel you can do this when merging.

Glenn

Quoted 30 lines
   grub_errno = GRUB_ERR_NONE;
grub-core/osdep/linux/hostdisk.c +1 -1
@@ -344,7 +346,7 @@ grub_util_fd_open_device (const grub_disk_t disk,
 grub_disk_addr_t sector, int f && strncmp (dev, "/dev/", 5) == 0)
       {
 	if (sector >= part_start)
-	  is_partition = grub_hostdisk_linux_find_partition (dev,
 part_start);
+	  is_partition = grub_hostdisk_linux_find_partition (disk,

dev, part_start); else

grub-core/osdep/linux/hostdisk.c +0 -0
 	  *max = part_start - sector;
       }
include/grub/disk.h +0 -0
index 25c141ea2..071b2f7df 100644
@@ -208,6 +208,13 @@ grub_disk_from_native_sector (grub_disk_t disk,

grub_disk_addr_t sector) return sector << (disk->log_sector_size - GRUB_DISK_SECTOR_BITS); }

+/* Convert from GRUB native disk sized sector to disk sized sector. */ +static inline grub_disk_addr_t +grub_disk_to_native_sector (grub_disk_t disk, grub_disk_addr_t sector) +{

include/grub/disk.h +3 -0
+  return sector >> (disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
+}
+
 /* This is called from the memory manager.  */
 void grub_disk_cache_invalidate_all (void);
#4 lore raw
Quoted 37 lines · Glenn Washburn
Quoted 34 lines · On Thu, 9 Feb 2023 13:09:16 +0530 Mukesh Kumar Chaurasiya…

The disk sector size provided by sysfs file system considers the sector size of 512 irrespective of disk sector size, Thus causing the read by grub to an incorrect offset from what was originally intended.

Considering the 512 sector size of sysfs data the actual sector needs to be modified corresponding to disk sector size.

---
 grub-core/osdep/linux/hostdisk.c | 8 +++++---
 include/grub/disk.h              | 7 +++++++
 2 files changed, 12 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/grub-core/osdep/linux/hostdisk.c index 07058f63a..fd0769e3b 100644

grub-core/osdep/linux/hostdisk.c +1 -1
@@ -198,7 +198,8 @@ have_devfs (void)
 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
 
 static int
-grub_hostdisk_linux_find_partition (char *dev, grub_disk_addr_t
 sector) +grub_hostdisk_linux_find_partition (const grub_disk_t disk,
 char *dev,
+                                    grub_disk_addr_t sector)
 {
   size_t len = strlen (dev);
   const char *format;
@@ -263,7 +264,8 @@ grub_hostdisk_linux_find_partition (char *dev,

grub_disk_addr_t sector) if (fstat (fd, &st) < 0

grub-core/osdep/linux/hostdisk.c +0 -0
 	  || !grub_util_device_is_mapped_stat (&st)
 	  || !grub_util_get_dm_node_linear_info (st.st_rdev, 0, 0,

&start))

Quoted 8 lines
Quoted 5 lines
grub-core/osdep/linux/hostdisk.c +2 -1
-	start = grub_util_find_partition_start_os (real_dev);
+        start = grub_disk_to_native_sector (disk,
+

grub_util_find_partition_start_os (real_dev)); /* We don't care about errors here. */

There needs to be a space to tab conversion here on both lines. Perhaps Daniel you can do this when merging.

Good point! I will do it when I merge the patch.

Thanks,

Daniel