Skip to contentSkip to conversation

disk/i386/pc/biosdisk: Avoid repeated get_diskinfo_int13_extensions calls

PATCH In review Ondrej Zary via Grub-devel 1 participant 2 messages Nov 11

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 disk/i386/pc/biosdisk: Avoid repeated get_diskinfo_int13_extensions calls

Synology DS411+II BIOS breaks when get_diskinfo_int13_extensions is called 16 times. All subsequent int13 disk accesses will fail.

As GRUB opens and closes disks multiple times, biosdisk calls that function repeatedly, resulting in failure before loading normal.mod.

Call the function only once for each hard drive and cache the output.

---
 grub-core/disk/i386/pc/biosdisk.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
grub-core/disk/i386/pc/biosdisk.c +28 -1
index 4b9bae0d1..a8d1ce8c5 100644
@@ -34,6 +34,16 @@ GRUB_MOD_LICENSE ("GPLv3+");
 static int cd_drive = 0;
 static int grub_biosdisk_rw_int13_extensions (int ah, int drive, void *dap);
 
+/* Synology DS411+II BIOS breaks when get_diskinfo_int13_extensions is called
+   16 times. Call it only once for each hard drive and cache the output here. */
+struct hdd_params_cache_entry
+{
+  grub_uint64_t total_sectors;
+  grub_uint16_t bytes_per_sector;
+};
+/* for drives 0x80..0x8f */
+static struct hdd_params_cache_entry hdd_params_cache[16];
+
 static int grub_biosdisk_get_num_floppies (void)
 {
   struct grub_bios_int_registers regs;
@@ -366,13 +376,21 @@ grub_biosdisk_open (const char *name, grub_disk_t disk)
       version = grub_biosdisk_check_int13_extensions (drive);
       if (version)
 	{
+	  struct hdd_params_cache_entry *cached = NULL;
 	  struct grub_biosdisk_drp *drp
 	    = (struct grub_biosdisk_drp *) grub_absolute_pointer (GRUB_MEMORY_MACHINE_SCRATCH_ADDR);
 
 	  /* Clear out the DRP.  */
 	  grub_memset (drp, 0, sizeof (*drp));
 	  drp->size = sizeof (*drp);
-	  if (! grub_biosdisk_get_diskinfo_int13_extensions (drive, drp))
+	  /* Use cached data if cache entry is valid */
+	  if (drive >= 0x80 && drive < 0x90 && hdd_params_cache[drive - 0x80].total_sectors)
+	    {
+	      cached = &hdd_params_cache[drive - 0x80];
+	      drp->total_sectors = cached->total_sectors;
+	      drp->bytes_per_sector = cached->bytes_per_sector;
+	    }
+	  if (cached || ! grub_biosdisk_get_diskinfo_int13_extensions (drive, drp))
 	    {
 	      data->flags = GRUB_BIOSDISK_FLAG_LBA;
 
@@ -389,6 +407,13 @@ grub_biosdisk_open (const char *name, grub_disk_t disk)
 		  && drp->bytes_per_sector >= 512
 		  && drp->bytes_per_sector <= 16384)
 		disk->log_sector_size = grub_log2ull (drp->bytes_per_sector);
+	      /* Cache the data */
+	      if (drive >= 0x80 && drive < 0x90)
+		{
+		  cached = &hdd_params_cache[drive - 0x80];
+		  cached->total_sectors = total_sectors;
+		  cached->bytes_per_sector = 1 << disk->log_sector_size;
+		}
 	    }
 	}
     }
@@ -686,6 +711,8 @@ GRUB_MOD_INIT(biosdisk)
   if (boot_drive >= 0x90)
     cd_drive = boot_drive;
 
+  grub_memset (hdd_params_cache, 0, sizeof (hdd_params_cache));
+
   grub_disk_dev_register (&grub_biosdisk_dev);
 }
 
Signature · 7 lines
2.47.3


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw

The project has moved to GitLab and contributions go into standard MRs. Check the following file

https://gitlab.freedesktop.org/gnu-grub/grub/-/blob/master/CONTRIBUTING

Leo

Quoted 94 lines · Ondrej Zary via Grub-devel

Synology DS411+II BIOS breaks when get_diskinfo_int13_extensions is called 16 times. All subsequent int13 disk accesses will fail.

As GRUB opens and closes disks multiple times, biosdisk calls that function repeatedly, resulting in failure before loading normal.mod.

Call the function only once for each hard drive and cache the output.

---
 grub-core/disk/i386/pc/biosdisk.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
grub-core/disk/i386/pc/biosdisk.c +1 -0
index 4b9bae0d1..a8d1ce8c5 100644
@@ -34,6 +34,16 @@ GRUB_MOD_LICENSE ("GPLv3+");
 static int cd_drive = 0;
 static int grub_biosdisk_rw_int13_extensions (int ah, int drive, void
 *dap);
 
+/* Synology DS411+II BIOS breaks when get_diskinfo_int13_extensions is

called
+ 16 times. Call it only once for each hard drive and cache the output
here. */

grub-core/disk/i386/pc/biosdisk.c +17 -1
+struct hdd_params_cache_entry
+{
+  grub_uint64_t total_sectors;
+  grub_uint16_t bytes_per_sector;
+};
+/* for drives 0x80..0x8f */
+static struct hdd_params_cache_entry hdd_params_cache[16];
+
 static int grub_biosdisk_get_num_floppies (void)
 {
   struct grub_bios_int_registers regs;
@@ -366,13 +376,21 @@ grub_biosdisk_open (const char *name, grub_disk_t
 disk)
       version = grub_biosdisk_check_int13_extensions (drive);
       if (version)
        {
+         struct hdd_params_cache_entry *cached = NULL;
          struct grub_biosdisk_drp *drp
            = (struct grub_biosdisk_drp *) grub_absolute_pointer
 (GRUB_MEMORY_MACHINE_SCRATCH_ADDR);
 
          /* Clear out the DRP.  */
          grub_memset (drp, 0, sizeof (*drp));
          drp->size = sizeof (*drp);
-         if (! grub_biosdisk_get_diskinfo_int13_extensions (drive, drp))
+         /* Use cached data if cache entry is valid */
+         if (drive >= 0x80 && drive < 0x90 && hdd_params_cache[drive -
 0x80].total_sectors)
+           {
+             cached = &hdd_params_cache[drive - 0x80];
+             drp->total_sectors = cached->total_sectors;
+             drp->bytes_per_sector = cached->bytes_per_sector;
+           }
+         if (cached || ! grub_biosdisk_get_diskinfo_int13_extensions

(drive, drp))

            {
              data->flags = GRUB_BIOSDISK_FLAG_LBA;
grub-core/disk/i386/pc/biosdisk.c +7 -0
@@ -389,6 +407,13 @@ grub_biosdisk_open (const char *name, grub_disk_t
 disk)
                  && drp->bytes_per_sector >= 512
                  && drp->bytes_per_sector <= 16384)
                disk->log_sector_size = grub_log2ull
 (drp->bytes_per_sector);
+             /* Cache the data */
+             if (drive >= 0x80 && drive < 0x90)
+               {
+                 cached = &hdd_params_cache[drive - 0x80];
+                 cached->total_sectors = total_sectors;
+                 cached->bytes_per_sector = 1 << disk->log_sector_size;
+               }
            }
        }
     }
grub-core/disk/i386/pc/biosdisk.c +2 -1
@@ -686,6 +711,8 @@ GRUB_MOD_INIT(biosdisk)
   if (boot_drive >= 0x90)
     cd_drive = boot_drive;
 
+  grub_memset (hdd_params_cache, 0, sizeof (hdd_params_cache));
+
   grub_disk_dev_register (&grub_biosdisk_dev);
 }
 
--

2.47.3

Quoted 6 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel