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

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