nalika / rpms / grub2

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