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

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