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

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