dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0232-efidisk-Respect-block_io_protocol-buffer-alignment.patch

28f7f8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
27a4da
From: Leif Lindholm <leif.lindholm@linaro.org>
27a4da
Date: Sat, 27 Feb 2016 13:44:59 +0100
28f7f8
Subject: [PATCH] efidisk: Respect block_io_protocol buffer alignment
27a4da
27a4da
Returned from the OpenProtocol operation, the grub_efi_block_io_media
27a4da
structure contains the io_align field, specifying the minimum alignment
27a4da
required for buffers used in any data transfers with the device.
27a4da
27a4da
Make grub_efidisk_readwrite() allocate a temporary buffer, aligned to
27a4da
this boundary, if the buffer passed to it does not already meet the
27a4da
requirements.
27a4da
27a4da
Also sanity check the io_align field in grub_efidisk_open() for
27a4da
power-of-two-ness and bail if invalid.
27a4da
27a4da
Cherry-picked-from: 51f375d688529b7c1819ba40188ee52b9333887c
27a4da
Resolves: rhbz#1300009
27a4da
---
27a4da
 grub-core/disk/efi/efidisk.c | 48 +++++++++++++++++++++++++++++++++++++-------
27a4da
 1 file changed, 41 insertions(+), 7 deletions(-)
27a4da
27a4da
diff --git a/grub-core/disk/efi/efidisk.c b/grub-core/disk/efi/efidisk.c
28f7f8
index 845c66fa9ab..f04f20b84ff 100644
27a4da
--- a/grub-core/disk/efi/efidisk.c
27a4da
+++ b/grub-core/disk/efi/efidisk.c
27a4da
@@ -487,8 +487,15 @@ grub_efidisk_open (const char *name, struct grub_disk *disk)
27a4da
   m = d->block_io->media;
27a4da
   /* FIXME: Probably it is better to store the block size in the disk,
27a4da
      and total sectors should be replaced with total blocks.  */
27a4da
-  grub_dprintf ("efidisk", "m = %p, last block = %llx, block size = %x\n",
27a4da
-		m, (unsigned long long) m->last_block, m->block_size);
27a4da
+  grub_dprintf ("efidisk",
27a4da
+		"m = %p, last block = %llx, block size = %x, io align = %x\n",
27a4da
+		m, (unsigned long long) m->last_block, m->block_size,
27a4da
+		m->io_align);
27a4da
+
27a4da
+  /* Ensure required buffer alignment is a power of two (or is zero). */
27a4da
+  if (m->io_align & (m->io_align - 1))
27a4da
+    return grub_error (GRUB_ERR_IO, "invalid buffer alignment %d", m->io_align);
27a4da
+
27a4da
   disk->total_sectors = m->last_block + 1;
27a4da
   /* Don't increase this value due to bug in some EFI.  */
27a4da
   disk->max_agglomerate = 0xa0000 >> (GRUB_DISK_CACHE_BITS + GRUB_DISK_SECTOR_BITS);
27a4da
@@ -518,15 +525,42 @@ grub_efidisk_readwrite (struct grub_disk *disk, grub_disk_addr_t sector,
27a4da
 {
27a4da
   struct grub_efidisk_data *d;
27a4da
   grub_efi_block_io_t *bio;
27a4da
+  grub_efi_status_t status;
27a4da
+  grub_size_t io_align, num_bytes;
27a4da
+  char *aligned_buf;
27a4da
 
27a4da
   d = disk->data;
27a4da
   bio = d->block_io;
27a4da
 
27a4da
-  return efi_call_5 ((wr ? bio->write_blocks : bio->read_blocks), bio,
27a4da
-		     bio->media->media_id,
27a4da
-		     (grub_efi_uint64_t) sector,
27a4da
-		     (grub_efi_uintn_t) size << disk->log_sector_size,
27a4da
-		     buf);
27a4da
+  /* Set alignment to 1 if 0 specified */
27a4da
+  io_align = bio->media->io_align ? bio->media->io_align : 1;
27a4da
+  num_bytes = size << disk->log_sector_size;
27a4da
+
27a4da
+  if ((grub_addr_t) buf & (io_align - 1))
27a4da
+    {
27a4da
+      aligned_buf = grub_memalign (io_align, num_bytes);
27a4da
+      if (! aligned_buf)
27a4da
+	return GRUB_EFI_OUT_OF_RESOURCES;
27a4da
+      if (wr)
27a4da
+	grub_memcpy (aligned_buf, buf, num_bytes);
27a4da
+    }
27a4da
+  else
27a4da
+    {
27a4da
+      aligned_buf = buf;
27a4da
+    }
27a4da
+
27a4da
+  status =  efi_call_5 ((wr ? bio->write_blocks : bio->read_blocks), bio,
27a4da
+			bio->media->media_id, (grub_efi_uint64_t) sector,
27a4da
+			(grub_efi_uintn_t) num_bytes, aligned_buf);
27a4da
+
27a4da
+  if ((grub_addr_t) buf & (io_align - 1))
27a4da
+    {
27a4da
+      if (!wr)
27a4da
+	grub_memcpy (buf, aligned_buf, num_bytes);
27a4da
+      grub_free (aligned_buf);
27a4da
+    }
27a4da
+
27a4da
+  return status;
27a4da
 }
27a4da
 
27a4da
 static grub_err_t