From da26129db9e007294b3b35dc75ef74c5e516afff Mon Sep 17 00:00:00 2001
From: Peter Jones <pjones@redhat.com>
Date: Thu, 13 Sep 2018 14:42:34 -0400
Subject: [PATCH 154/220] x86-efi: Re-arrange grub_cmd_linux() a little bit.

This just helps the next patch be easier to read.

Signed-off-by: Peter Jones <pjones@redhat.com>
---
 grub-core/loader/i386/efi/linux.c | 75 +++++++++++++++++++++------------------
 1 file changed, 41 insertions(+), 34 deletions(-)

diff --git a/grub-core/loader/i386/efi/linux.c b/grub-core/loader/i386/efi/linux.c
index 2f03368..5f48fa5 100644
--- a/grub-core/loader/i386/efi/linux.c
+++ b/grub-core/loader/i386/efi/linux.c
@@ -243,32 +243,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
       goto fail;
     }
 
-  params = grub_efi_allocate_pages_max (GRUB_EFI_MAX_ALLOCATION_ADDRESS,
-					BYTES_TO_PAGES(sizeof(*params)));
-  if (!params)
-    params = grub_efi_allocate_pages_max (GRUB_EFI_MAX_USABLE_ADDRESS,
-					  BYTES_TO_PAGES(sizeof(*params)));
-  if (! params)
-    {
-      grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate kernel parameters");
-      goto fail;
-    }
-
-  grub_dprintf ("linux", "params = %p\n", params);
-
-  grub_memset (params, 0, sizeof(*params));
+  lh = (struct linux_i386_kernel_header *)kernel;
+  grub_dprintf ("linux", "original lh is at %p\n", kernel);
 
-  setup_header_end_offset = *((grub_uint8_t *)kernel + 0x201);
-  grub_dprintf ("linux", "copying %lu bytes from %p to %p\n",
-		MIN((grub_size_t)0x202+setup_header_end_offset,
-		    sizeof (*params)) - 0x1f1,
-		(grub_uint8_t *)kernel + 0x1f1,
-		(grub_uint8_t *)params + 0x1f1);
-  grub_memcpy ((grub_uint8_t *)params + 0x1f1,
-	       (grub_uint8_t *)kernel + 0x1f1,
-		MIN((grub_size_t)0x202+setup_header_end_offset,sizeof (*params)) - 0x1f1);
-  lh = (struct linux_i386_kernel_header *)params;
-  grub_dprintf ("linux", "lh is at %p\n", lh);
   grub_dprintf ("linux", "checking lh->boot_flag\n");
   if (lh->boot_flag != grub_cpu_to_le16 (0xaa55))
     {
@@ -316,6 +293,34 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
     }
 #endif
 
+  params = grub_efi_allocate_pages_max (GRUB_EFI_MAX_ALLOCATION_ADDRESS,
+					BYTES_TO_PAGES(sizeof(*params)));
+  if (!params)
+    params = grub_efi_allocate_pages_max (GRUB_EFI_MAX_USABLE_ADDRESS,
+					  BYTES_TO_PAGES(sizeof(*params)));
+  if (! params)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate kernel parameters");
+      goto fail;
+    }
+
+  grub_dprintf ("linux", "params = %p\n", params);
+
+  grub_memset (params, 0, sizeof(*params));
+
+  setup_header_end_offset = *((grub_uint8_t *)kernel + 0x201);
+  grub_dprintf ("linux", "copying %lu bytes from %p to %p\n",
+		MIN((grub_size_t)0x202+setup_header_end_offset,
+		    sizeof (*params)) - 0x1f1,
+		(grub_uint8_t *)kernel + 0x1f1,
+		(grub_uint8_t *)params + 0x1f1);
+  grub_memcpy ((grub_uint8_t *)params + 0x1f1,
+	       (grub_uint8_t *)kernel + 0x1f1,
+		MIN((grub_size_t)0x202+setup_header_end_offset,sizeof (*params)) - 0x1f1);
+
+  lh = (struct linux_i386_kernel_header *)params;
+  grub_dprintf ("linux", "new lh is at %p\n", lh);
+
   grub_dprintf ("linux", "setting up cmdline\n");
   linux_cmdline = grub_efi_allocate_pages_max(GRUB_EFI_MAX_ALLOCATION_ADDRESS,
 					      BYTES_TO_PAGES(lh->cmdline_size + 1));
@@ -341,8 +346,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
   grub_dprintf ("linux", "setting lh->cmd_line_ptr\n");
   lh->cmd_line_ptr = (grub_uint32_t)(grub_addr_t)linux_cmdline;
 
-  grub_dprintf ("linux", "computing handover offset\n");
   handover_offset = lh->handover_offset;
+  grub_dprintf("linux", "handover_offset: %08x\n", handover_offset);
 
   start = (lh->setup_sects + 1) * 512;
 
@@ -359,26 +364,28 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
       grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate kernel"));
       goto fail;
     }
-
-  grub_dprintf ("linux", "kernel_mem = %lx\n", (unsigned long) kernel_mem);
+  grub_dprintf("linux", "kernel_mem = %p\n", kernel_mem);
 
   grub_loader_set (grub_linuxefi_boot, grub_linuxefi_unload, 0);
-  loaded=1;
+
+  loaded = 1;
+
   grub_dprintf ("linux", "setting lh->code32_start to %p\n", kernel_mem);
   lh->code32_start = (grub_uint32_t)(grub_addr_t) kernel_mem;
 
   grub_memcpy (kernel_mem, (char *)kernel + start, filelen - start);
 
-  grub_dprintf ("linux", "setting lh->type_of_loader\n");
   lh->type_of_loader = 0x6;
+  grub_dprintf ("linux", "setting lh->type_of_loader = 0x%02x\n",
+		lh->type_of_loader);
 
-  grub_dprintf ("linux", "setting lh->ext_loader_{type,ver}\n");
   params->ext_loader_type = 0;
   params->ext_loader_ver = 2;
-  grub_dprintf("linux", "kernel_mem: %p handover_offset: %08x\n",
-	       kernel_mem, handover_offset);
+  grub_dprintf ("linux",
+		"setting lh->ext_loader_{type,ver} = {0x%02x,0x%02x}\n",
+		params->ext_loader_type, params->ext_loader_ver);
 
- fail:
+fail:
   if (file)
     grub_file_close (file);
 
-- 
1.8.3.1

