nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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