All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] grub-core: modify sector by sysfs as disk sector
@ 2023-02-03  4:40 Mukesh Kumar Chaurasiya
  2023-02-08 18:49 ` Daniel Kiper
  0 siblings, 1 reply; 2+ messages in thread
From: Mukesh Kumar Chaurasiya @ 2023-02-03  4:40 UTC (permalink / raw)
  To: grub-devel; +Cc: mamatha4, avnish.chouhan, 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.

Signed-off-by: Mukesh Kumar Chaurasiya <mchauras@linux.vnet.ibm.com>
---
 grub-core/osdep/linux/hostdisk.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/grub-core/osdep/linux/hostdisk.c b/grub-core/osdep/linux/hostdisk.c
index 07058f63a..13e4e2feb 100644
--- a/grub-core/osdep/linux/hostdisk.c
+++ b/grub-core/osdep/linux/hostdisk.c
@@ -197,8 +197,15 @@ have_devfs (void)
 
 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
 
+static inline grub_disk_addr_t
+transform_sector (grub_disk_t disk, grub_disk_addr_t sector)
+{
+  return sector >> (disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
+}
+
 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 +270,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 = transform_sector (disk,
+                                 grub_util_find_partition_start_os (real_dev));
       /* We don't care about errors here.  */
       grub_errno = GRUB_ERR_NONE;
 
@@ -344,7 +352,8 @@ 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;
       }
-- 
2.31.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] grub-core: modify sector by sysfs as disk sector
  2023-02-03  4:40 [PATCH] grub-core: modify sector by sysfs as disk sector Mukesh Kumar Chaurasiya
@ 2023-02-08 18:49 ` Daniel Kiper
  0 siblings, 0 replies; 2+ messages in thread
From: Daniel Kiper @ 2023-02-08 18:49 UTC (permalink / raw)
  To: Mukesh Kumar Chaurasiya; +Cc: grub-devel, mamatha4, avnish.chouhan

On Fri, Feb 03, 2023 at 10:10:43AM +0530, Mukesh Kumar Chaurasiya wrote:
> 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.
>
> Signed-off-by: Mukesh Kumar Chaurasiya <mchauras@linux.vnet.ibm.com>
> ---
>  grub-core/osdep/linux/hostdisk.c | 15 ++++++++++++---
>  1 file changed, 12 insertions(+), 3 deletions(-)
>
> diff --git a/grub-core/osdep/linux/hostdisk.c b/grub-core/osdep/linux/hostdisk.c
> index 07058f63a..13e4e2feb 100644
> --- a/grub-core/osdep/linux/hostdisk.c
> +++ b/grub-core/osdep/linux/hostdisk.c
> @@ -197,8 +197,15 @@ have_devfs (void)
>
>  #pragma GCC diagnostic ignored "-Wformat-nonliteral"
>
> +static inline grub_disk_addr_t
> +transform_sector (grub_disk_t disk, grub_disk_addr_t sector)
> +{
> +  return sector >> (disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
> +}

Please take a look at include/grub/disk.h:grub_disk_from_native_sector()
and define grub_disk_to_native_sector() next to it.

> +
>  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 +270,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 = transform_sector (disk,
> +                                 grub_util_find_partition_start_os (real_dev));

You do not need to wrap lines which are a bit longer than 80 characters.

>        /* We don't care about errors here.  */
>        grub_errno = GRUB_ERR_NONE;
>
> @@ -344,7 +352,8 @@ 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);

Ditto.

Daniel


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-02-08 18:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-03  4:40 [PATCH] grub-core: modify sector by sysfs as disk sector Mukesh Kumar Chaurasiya
2023-02-08 18:49 ` Daniel Kiper

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.