nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0222-Rework-linux-command.patch

27a4da
From 8fab682a279979b48f7aa8c665aad7af1f38f6ea Mon Sep 17 00:00:00 2001
27a4da
From: Matthew Garrett <mjg59@coreos.com>
27a4da
Date: Sun, 9 Aug 2015 16:12:39 -0700
27a4da
Subject: [PATCH 222/260] Rework linux command
27a4da
27a4da
We want a single buffer that contains the entire kernel image in order to
27a4da
perform a TPM measurement. Allocate one and copy the entire kernel into it
27a4da
before pulling out the individual blocks later on.
27a4da
---
27a4da
 grub-core/loader/i386/linux.c | 34 +++++++++++++++++++++-------------
27a4da
 1 file changed, 21 insertions(+), 13 deletions(-)
27a4da
27a4da
diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
27a4da
index bd37c69b5..53f74ae06 100644
27a4da
--- a/grub-core/loader/i386/linux.c
27a4da
+++ b/grub-core/loader/i386/linux.c
27a4da
@@ -682,12 +682,13 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
27a4da
   grub_file_t file = 0;
27a4da
   struct linux_kernel_header lh;
27a4da
   grub_uint8_t setup_sects;
27a4da
-  grub_size_t real_size, prot_size, prot_file_size;
27a4da
+  grub_size_t real_size, prot_size, prot_file_size, kernel_offset;
27a4da
   grub_ssize_t len;
27a4da
   int i;
27a4da
   grub_size_t align, min_align;
27a4da
   int relocatable;
27a4da
   grub_uint64_t preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
27a4da
+  grub_uint8_t *kernel = NULL;
27a4da
 
27a4da
   grub_dl_ref (my_mod);
27a4da
 
27a4da
@@ -701,7 +702,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
27a4da
   if (! file)
27a4da
     goto fail;
27a4da
 
27a4da
-  if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
27a4da
+  len = grub_file_size (file);
27a4da
+  kernel = grub_malloc (len);
27a4da
+  if (!kernel)
27a4da
+    {
27a4da
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
27a4da
+      goto fail;
27a4da
+    }
27a4da
+
27a4da
+  if (grub_file_read (file, kernel, len) != len)
27a4da
     {
27a4da
       if (!grub_errno)
27a4da
 	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
27a4da
@@ -709,6 +718,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
27a4da
       goto fail;
27a4da
     }
27a4da
 
27a4da
+  grub_memcpy (&lh, kernel, sizeof (lh));
27a4da
+  kernel_offset = sizeof (lh);
27a4da
+
27a4da
   if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
27a4da
     {
27a4da
       grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
27a4da
@@ -808,13 +820,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
27a4da
   linux_params.ps_mouse = linux_params.padding10 =  0;
27a4da
 
27a4da
   len = sizeof (linux_params) - sizeof (lh);
27a4da
-  if (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len)
27a4da
-    {
27a4da
-      if (!grub_errno)
27a4da
-	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
27a4da
-		    argv[0]);
27a4da
-      goto fail;
27a4da
-    }
27a4da
+
27a4da
+  grub_memcpy (&linux_params + sizeof (lh), kernel + kernel_offset, len);
27a4da
+  kernel_offset += len;
27a4da
 
27a4da
   linux_params.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
27a4da
 
27a4da
@@ -873,7 +881,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
27a4da
 
27a4da
   /* The other parameters are filled when booting.  */
27a4da
 
27a4da
-  grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
27a4da
+  kernel_offset = real_size + GRUB_DISK_SECTOR_SIZE;
27a4da
 
27a4da
   grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
27a4da
 		(unsigned) real_size, (unsigned) prot_size);
27a4da
@@ -1018,9 +1026,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
27a4da
 			      - (sizeof (LINUX_IMAGE) - 1));
27a4da
 
27a4da
   len = prot_file_size;
27a4da
-  if (grub_file_read (file, prot_mode_mem, len) != len && !grub_errno)
27a4da
-    grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
27a4da
-		argv[0]);
27a4da
+  grub_memcpy (prot_mode_mem, kernel + kernel_offset, len);
27a4da
 
27a4da
   if (grub_errno == GRUB_ERR_NONE)
27a4da
     {
27a4da
@@ -1031,6 +1037,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
27a4da
 
27a4da
  fail:
27a4da
 
27a4da
+  grub_free (kernel);
27a4da
+
27a4da
   if (file)
27a4da
     grub_file_close (file);
27a4da
 
27a4da
-- 
27a4da
2.13.0
27a4da