nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0118-x86-efi-Use-bounce-buffers-for-reading-to-addresses-.patch

8e15ce
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8e15ce
From: Peter Jones <pjones@redhat.com>
8e15ce
Date: Fri, 12 Jul 2019 09:53:32 +0200
8e15ce
Subject: [PATCH] x86-efi: Use bounce buffers for reading to addresses > 4GB
8e15ce
8e15ce
Lots of machines apparently can't DMA correctly above 4GB during UEFI,
8e15ce
so use bounce buffers for the initramfs read.
8e15ce
8e15ce
Signed-off-by: Peter Jones <pjones@redhat.com>
8e15ce
---
8e15ce
 grub-core/loader/i386/efi/linux.c | 52 +++++++++++++++++++++++++++++++++------
8e15ce
 1 file changed, 45 insertions(+), 7 deletions(-)
8e15ce
8e15ce
diff --git a/grub-core/loader/i386/efi/linux.c b/grub-core/loader/i386/efi/linux.c
8e15ce
index 33e981e76e7..2f0336809e7 100644
8e15ce
--- a/grub-core/loader/i386/efi/linux.c
8e15ce
+++ b/grub-core/loader/i386/efi/linux.c
8e15ce
@@ -35,11 +35,16 @@ static grub_dl_t my_mod;
8e15ce
 static int loaded;
8e15ce
 static void *kernel_mem;
8e15ce
 static grub_uint64_t kernel_size;
8e15ce
-static grub_uint8_t *initrd_mem;
8e15ce
+static void *initrd_mem;
8e15ce
 static grub_uint32_t handover_offset;
8e15ce
 struct linux_kernel_params *params;
8e15ce
 static char *linux_cmdline;
8e15ce
 
8e15ce
+#define MIN(a, b) \
8e15ce
+  ({ typeof (a) _a = (a); \
8e15ce
+     typeof (b) _b = (b); \
8e15ce
+     _a < _b ? _a : _b; })
8e15ce
+
8e15ce
 #define BYTES_TO_PAGES(bytes)   (((bytes) + 0xfff) >> 12)
8e15ce
 
8e15ce
 static grub_err_t
8e15ce
@@ -73,6 +78,44 @@ grub_linuxefi_unload (void)
8e15ce
   return GRUB_ERR_NONE;
8e15ce
 }
8e15ce
 
8e15ce
+#define BOUNCE_BUFFER_MAX 0x10000000ull
8e15ce
+
8e15ce
+static grub_ssize_t
8e15ce
+read(grub_file_t file, grub_uint8_t *bufp, grub_size_t len)
8e15ce
+{
8e15ce
+  grub_ssize_t bufpos = 0;
8e15ce
+  static grub_size_t bbufsz = 0;
8e15ce
+  static char *bbuf = NULL;
8e15ce
+
8e15ce
+  if (bbufsz == 0)
8e15ce
+    bbufsz = MIN(BOUNCE_BUFFER_MAX, len);
8e15ce
+
8e15ce
+  while (!bbuf && bbufsz)
8e15ce
+    {
8e15ce
+      bbuf = grub_malloc(bbufsz);
8e15ce
+      if (!bbuf)
8e15ce
+	bbufsz >>= 1;
8e15ce
+    }
8e15ce
+  if (!bbuf)
8e15ce
+    grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate bounce buffer"));
8e15ce
+
8e15ce
+  while (bufpos < (long long)len)
8e15ce
+    {
8e15ce
+      grub_ssize_t sz;
8e15ce
+
8e15ce
+      sz = grub_file_read (file, bbuf, MIN(bbufsz, len - bufpos));
8e15ce
+      if (sz < 0)
8e15ce
+	return sz;
8e15ce
+      if (sz == 0)
8e15ce
+	break;
8e15ce
+
8e15ce
+      grub_memcpy(bufp + bufpos, bbuf, sz);
8e15ce
+      bufpos += sz;
8e15ce
+    }
8e15ce
+
8e15ce
+  return bufpos;
8e15ce
+}
8e15ce
+
8e15ce
 static grub_err_t
8e15ce
 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
8e15ce
                  int argc, char *argv[])
8e15ce
@@ -126,7 +169,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
8e15ce
   for (i = 0; i < nfiles; i++)
8e15ce
     {
8e15ce
       grub_ssize_t cursize = grub_file_size (files[i]);
8e15ce
-      if (grub_file_read (files[i], ptr, cursize) != cursize)
8e15ce
+      if (read (files[i], ptr, cursize) != cursize)
8e15ce
         {
8e15ce
           if (!grub_errno)
8e15ce
             grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
8e15ce
@@ -152,11 +195,6 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
8e15ce
   return grub_errno;
8e15ce
 }
8e15ce
 
8e15ce
-#define MIN(a, b) \
8e15ce
-  ({ typeof (a) _a = (a);  \
8e15ce
-     typeof (b) _b = (b); \
8e15ce
-     _a < _b ? _a : _b; })
8e15ce
-
8e15ce
 static grub_err_t
8e15ce
 grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
8e15ce
 		int argc, char *argv[])