Blame SOURCES/0006-Rework-linux16-command.patch

8e15ce
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8e15ce
From: Matthew Garrett <mjg59@coreos.com>
8e15ce
Date: Sun, 9 Aug 2015 16:20:58 -0700
8e15ce
Subject: [PATCH] Rework linux16 command
8e15ce
8e15ce
We want a single buffer that contains the entire kernel image in order to
8e15ce
perform a TPM measurement. Allocate one and copy the entire kernel int it
8e15ce
before pulling out the individual blocks later on.
8e15ce
8e15ce
Signed-off-by: Matthew Garrett <mjg59@coreos.com>
8e15ce
---
8e15ce
 grub-core/loader/i386/pc/linux.c | 33 +++++++++++++++++++++------------
8e15ce
 1 file changed, 21 insertions(+), 12 deletions(-)
8e15ce
8e15ce
diff --git a/grub-core/loader/i386/pc/linux.c b/grub-core/loader/i386/pc/linux.c
8e15ce
index 8be4c3b3f48..4b1750e360e 100644
8e15ce
--- a/grub-core/loader/i386/pc/linux.c
8e15ce
+++ b/grub-core/loader/i386/pc/linux.c
8e15ce
@@ -124,13 +124,14 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
8e15ce
   grub_file_t file = 0;
8e15ce
   struct linux_i386_kernel_header lh;
8e15ce
   grub_uint8_t setup_sects;
8e15ce
-  grub_size_t real_size;
8e15ce
+  grub_size_t real_size, kernel_offset = 0;
8e15ce
   grub_ssize_t len;
8e15ce
   int i;
8e15ce
   char *grub_linux_prot_chunk;
8e15ce
   int grub_linux_is_bzimage;
8e15ce
   grub_addr_t grub_linux_prot_target;
8e15ce
   grub_err_t err;
8e15ce
+  grub_uint8_t *kernel = NULL;
8e15ce
 
8e15ce
   grub_dl_ref (my_mod);
8e15ce
 
8e15ce
@@ -144,7 +145,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
8e15ce
   if (! file)
8e15ce
     goto fail;
8e15ce
 
8e15ce
-  if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
8e15ce
+  len = grub_file_size (file);
8e15ce
+  kernel = grub_malloc (len);
8e15ce
+  if (!kernel)
8e15ce
+    {
8e15ce
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
8e15ce
+      goto fail;
8e15ce
+    }
8e15ce
+
8e15ce
+  if (grub_file_read (file, kernel, len) != len)
8e15ce
     {
8e15ce
       if (!grub_errno)
8e15ce
 	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
8e15ce
@@ -152,6 +161,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
8e15ce
       goto fail;
8e15ce
     }
8e15ce
 
8e15ce
+  grub_memcpy (&lh, kernel, sizeof (lh));
8e15ce
+  kernel_offset = sizeof (lh);
8e15ce
+
8e15ce
   if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
8e15ce
     {
8e15ce
       grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
8e15ce
@@ -320,13 +332,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
8e15ce
   grub_memmove (grub_linux_real_chunk, &lh, sizeof (lh));
8e15ce
 
8e15ce
   len = real_size + GRUB_DISK_SECTOR_SIZE - sizeof (lh);
8e15ce
-  if (grub_file_read (file, grub_linux_real_chunk + sizeof (lh), len) != len)
8e15ce
-    {
8e15ce
-      if (!grub_errno)
8e15ce
-	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
8e15ce
-		    argv[0]);
8e15ce
-      goto fail;
8e15ce
-    }
8e15ce
+  grub_memcpy (grub_linux_real_chunk + sizeof (lh), kernel + kernel_offset,
8e15ce
+	       len);
8e15ce
+  kernel_offset += len;
8e15ce
 
8e15ce
   if (lh.header != grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
8e15ce
       || grub_le_to_cpu16 (lh.version) < 0x0200)
8e15ce
@@ -364,9 +372,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
8e15ce
   }
8e15ce
 
8e15ce
   len = grub_linux16_prot_size;
8e15ce
-  if (grub_file_read (file, grub_linux_prot_chunk, len) != len && !grub_errno)
8e15ce
-    grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
8e15ce
-		argv[0]);
8e15ce
+  grub_memcpy (grub_linux_prot_chunk, kernel + kernel_offset, len);
8e15ce
+  kernel_offset += len;
8e15ce
 
8e15ce
   if (grub_errno == GRUB_ERR_NONE)
8e15ce
     {
8e15ce
@@ -376,6 +383,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
8e15ce
 
8e15ce
  fail:
8e15ce
 
8e15ce
+  grub_free (kernel);
8e15ce
+
8e15ce
   if (file)
8e15ce
     grub_file_close (file);
8e15ce