nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone
5593c8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
5593c8
From: Matthew Garrett <mjg59@coreos.com>
5593c8
Date: Sun, 9 Aug 2015 16:12:39 -0700
5593c8
Subject: [PATCH] Rework linux 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 into 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/linux.c | 35 +++++++++++++++++++++++------------
5593c8
 1 file changed, 23 insertions(+), 12 deletions(-)
5593c8
5593c8
diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
d3c3ab
index 9f74a96b19a..dccf3bb3005 100644
5593c8
--- a/grub-core/loader/i386/linux.c
5593c8
+++ b/grub-core/loader/i386/linux.c
5593c8
@@ -649,13 +649,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
5593c8
 {
5593c8
   grub_file_t file = 0;
5593c8
   struct linux_i386_kernel_header lh;
5593c8
+  grub_uint8_t *linux_params_ptr;
5593c8
   grub_uint8_t setup_sects;
5593c8
-  grub_size_t real_size, prot_size, prot_file_size;
5593c8
+  grub_size_t real_size, prot_size, prot_file_size, kernel_offset;
5593c8
   grub_ssize_t len;
5593c8
   int i;
5593c8
   grub_size_t align, min_align;
5593c8
   int relocatable;
5593c8
   grub_uint64_t preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
5593c8
+  grub_uint8_t *kernel = NULL;
5593c8
 
5593c8
   grub_dl_ref (my_mod);
5593c8
 
5593c8
@@ -669,7 +671,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
@@ -677,6 +687,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
@@ -784,13 +797,11 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
5593c8
   /* We've already read lh so there is no need to read it second time. */
5593c8
   len -= sizeof(lh);
5593c8
 
5593c8
-  if ((len > 0) &&
5593c8
-      (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len))
5593c8
+  linux_params_ptr = (void *)&linux_params;
5593c8
+  if (len > 0)
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
+      grub_memcpy (linux_params_ptr + sizeof (lh), kernel + kernel_offset, len);
5593c8
+      kernel_offset += len;
5593c8
     }
5593c8
 
5593c8
   linux_params.code32_start = prot_mode_target + lh.code32_start - GRUB_LINUX_BZIMAGE_ADDR;
5593c8
@@ -853,7 +864,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
5593c8
 
5593c8
   /* The other parameters are filled when booting.  */
5593c8
 
5593c8
-  grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
5593c8
+  kernel_offset = real_size + GRUB_DISK_SECTOR_SIZE;
5593c8
 
5593c8
   grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
5593c8
 		(unsigned) real_size, (unsigned) prot_size);
5593c8
@@ -1007,9 +1018,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
5593c8
   }
5593c8
 
5593c8
   len = prot_file_size;
5593c8
-  if (grub_file_read (file, prot_mode_mem, len) != len && !grub_errno)
5593c8
-    grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
5593c8
-		argv[0]);
5593c8
+  grub_memcpy (prot_mode_mem, kernel + kernel_offset, len);
5593c8
 
5593c8
   if (grub_errno == GRUB_ERR_NONE)
5593c8
     {
5593c8
@@ -1020,6 +1029,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