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