dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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