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

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