nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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