Blame SOURCES/0222-Rework-linux-command.patch

a85e8e
From 8fab682a279979b48f7aa8c665aad7af1f38f6ea Mon Sep 17 00:00:00 2001
a85e8e
From: Matthew Garrett <mjg59@coreos.com>
a85e8e
Date: Sun, 9 Aug 2015 16:12:39 -0700
a85e8e
Subject: [PATCH 222/260] Rework linux command
a85e8e
a85e8e
We want a single buffer that contains the entire kernel image in order to
a85e8e
perform a TPM measurement. Allocate one and copy the entire kernel into it
a85e8e
before pulling out the individual blocks later on.
a85e8e
---
a85e8e
 grub-core/loader/i386/linux.c | 34 +++++++++++++++++++++-------------
a85e8e
 1 file changed, 21 insertions(+), 13 deletions(-)
a85e8e
a85e8e
diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
a85e8e
index bd37c69b5..53f74ae06 100644
a85e8e
--- a/grub-core/loader/i386/linux.c
a85e8e
+++ b/grub-core/loader/i386/linux.c
a85e8e
@@ -682,12 +682,13 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
a85e8e
   grub_file_t file = 0;
a85e8e
   struct linux_kernel_header lh;
a85e8e
   grub_uint8_t setup_sects;
a85e8e
-  grub_size_t real_size, prot_size, prot_file_size;
a85e8e
+  grub_size_t real_size, prot_size, prot_file_size, kernel_offset;
a85e8e
   grub_ssize_t len;
a85e8e
   int i;
a85e8e
   grub_size_t align, min_align;
a85e8e
   int relocatable;
a85e8e
   grub_uint64_t preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
a85e8e
+  grub_uint8_t *kernel = NULL;
a85e8e
 
a85e8e
   grub_dl_ref (my_mod);
a85e8e
 
a85e8e
@@ -701,7 +702,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
a85e8e
   if (! file)
a85e8e
     goto fail;
a85e8e
 
a85e8e
-  if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
a85e8e
+  len = grub_file_size (file);
a85e8e
+  kernel = grub_malloc (len);
a85e8e
+  if (!kernel)
a85e8e
+    {
a85e8e
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
a85e8e
+      goto fail;
a85e8e
+    }
a85e8e
+
a85e8e
+  if (grub_file_read (file, kernel, len) != len)
a85e8e
     {
a85e8e
       if (!grub_errno)
a85e8e
 	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
a85e8e
@@ -709,6 +718,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
a85e8e
       goto fail;
a85e8e
     }
a85e8e
 
a85e8e
+  grub_memcpy (&lh, kernel, sizeof (lh));
a85e8e
+  kernel_offset = sizeof (lh);
a85e8e
+
a85e8e
   if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
a85e8e
     {
a85e8e
       grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
a85e8e
@@ -808,13 +820,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
a85e8e
   linux_params.ps_mouse = linux_params.padding10 =  0;
a85e8e
 
a85e8e
   len = sizeof (linux_params) - sizeof (lh);
a85e8e
-  if (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len)
a85e8e
-    {
a85e8e
-      if (!grub_errno)
a85e8e
-	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
a85e8e
-		    argv[0]);
a85e8e
-      goto fail;
a85e8e
-    }
a85e8e
+
a85e8e
+  grub_memcpy (&linux_params + sizeof (lh), kernel + kernel_offset, len);
a85e8e
+  kernel_offset += len;
a85e8e
 
a85e8e
   linux_params.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
a85e8e
 
a85e8e
@@ -873,7 +881,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
a85e8e
 
a85e8e
   /* The other parameters are filled when booting.  */
a85e8e
 
a85e8e
-  grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
a85e8e
+  kernel_offset = real_size + GRUB_DISK_SECTOR_SIZE;
a85e8e
 
a85e8e
   grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
a85e8e
 		(unsigned) real_size, (unsigned) prot_size);
a85e8e
@@ -1018,9 +1026,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
a85e8e
 			      - (sizeof (LINUX_IMAGE) - 1));
a85e8e
 
a85e8e
   len = prot_file_size;
a85e8e
-  if (grub_file_read (file, prot_mode_mem, len) != len && !grub_errno)
a85e8e
-    grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
a85e8e
-		argv[0]);
a85e8e
+  grub_memcpy (prot_mode_mem, kernel + kernel_offset, len);
a85e8e
 
a85e8e
   if (grub_errno == GRUB_ERR_NONE)
a85e8e
     {
a85e8e
@@ -1031,6 +1037,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
a85e8e
 
a85e8e
  fail:
a85e8e
 
a85e8e
+  grub_free (kernel);
a85e8e
+
a85e8e
   if (file)
a85e8e
     grub_file_close (file);
a85e8e
 
a85e8e
-- 
a85e8e
2.13.0
a85e8e