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