Skip to contentSkip to conversation

Update the Linux boot protocol

PATCH Superseded Matthew Garrett 2 participants 5 messages Feb 3, 2012 – Feb 5, 2012

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 Update the Linux boot protocol

The Linux boot header includes information on the kernel's desired load address and alignment. Add support for that.

---
 ChangeLog                 |    6 ++++++
 include/grub/i386/linux.h |   28 +++++++++++++++++++++++-----
 2 files changed, 29 insertions(+), 5 deletions(-)
ChangeLog +6 -0
index e5a5d72..028691d 100644
@@ -1,3 +1,9 @@
+2012-02-03  Matthew Garrett  <[email protected]>
+
+	* include/grub/i386/linux.h (linux_kernel_header): Update to
+	boot protocol 2.10.
+	(linux_kernel_params): Likewise
+
 2012-02-03  Vladimir Serbinenko  <[email protected]>
 
 	* util/grub-mkrescue.in: Use same message as
include/grub/i386/linux.h +23 -5
index 6aa2391..8e27b93 100644
@@ -86,7 +86,7 @@ enum
     GRUB_VIDEO_LINUX_TYPE_SIMPLE = 0x70    /* Linear framebuffer without any additional functions.  */
   };
 
-/* For the Linux/i386 boot protocol version 2.03.  */
+/* For the Linux/i386 boot protocol version 2.10.  */
 struct linux_kernel_header
 {
   grub_uint8_t code1[0x0020];
@@ -131,8 +131,16 @@ struct linux_kernel_header
   grub_uint32_t initrd_addr_max;        /* Highest address for initrd */
   grub_uint32_t kernel_alignment;
   grub_uint8_t relocatable;
-  grub_uint8_t pad[3];
+  grub_uint8_t min_alignment;
+  grub_uint8_t pad[2];
   grub_uint32_t cmdline_size;
+  grub_uint32_t hardware_subarch;
+  grub_uint64_t hardware_subarch_data;
+  grub_uint32_t payload_offset;
+  grub_uint32_t payload_length;
+  grub_uint64_t setup_data;
+  grub_uint64_t pref_address;
+  grub_uint64_t init_size;
 } __attribute__ ((packed));
 
 /* Boot parameters for Linux based on 2.6.12. This is used by the setup
@@ -276,10 +284,20 @@ struct linux_kernel_params
   grub_uint32_t ramdisk_size;		/* initrd size */
   grub_uint32_t bootsect_kludge;	/* obsolete */
   grub_uint16_t heap_end_ptr;		/* Free memory after setup end */
-  grub_uint16_t pad1;			/* Unused */
+  grub_uint8_t ext_loader_ver;		/* Extended loader version */
+  grub_uint8_t ext_loader_type;		/* Extended loader type */  
   grub_uint32_t cmd_line_ptr;		/* Points to the kernel command line */
-
-  grub_uint8_t pad2[164];		/* 22c */
+  grub_uint32_t initrd_addr_max;	/* Maximum initrd address */
+  grub_uint32_t kernel_alignment;	/* Alignment of the kernel */
+  grub_uint8_t relocatable_kernel;	/* Is the kernel relocatable */
+  grub_uint8_t pad1[3];
+  grub_uint32_t cmdline_size;		/* Size of the kernel command line */
+  grub_uint32_t hardware_subarch;
+  grub_uint64_t hardware_subarch_data;
+  grub_uint32_t payload_offset;
+  grub_uint32_t payload_length;
+  grub_uint64_t setup_data;
+  grub_uint8_t pad2[120];		/* 258 */
   struct grub_e820_mmap e820_map[GRUB_E820_MAX_ENTRY];	/* 2d0 */
 
 } __attribute__ ((packed));
Signature · 1 line
1.7.7.6
#1 lore raw

PATCH 2/3 Add support for avoiding firmware inr elocations

EFI and OF both support firmware regions which may be in use during loading. Add support for avoiding these.

---
 ChangeLog                             |   10 +++++++
 grub-core/lib/efi/relocator.c         |   47 +++++++++++++++++++++++++++++++++
 grub-core/lib/ieee1275/relocator.c    |   27 +++++++++++++++++++
 grub-core/lib/relocator.c             |   12 +++++++-
 grub-core/loader/i386/bsd.c           |   19 +++++++-----
 grub-core/loader/i386/bsdXX.c         |    8 +++---
 grub-core/loader/i386/linux.c         |    4 +-
 grub-core/loader/i386/multiboot_mbi.c |    2 +-
 grub-core/loader/i386/pc/freedos.c    |    2 +-
 grub-core/loader/i386/pc/linux.c      |    4 +-
 grub-core/loader/i386/pc/ntldr.c      |    4 +-
 grub-core/loader/mips/linux.c         |    6 ++--
 grub-core/loader/multiboot_elfxx.c    |    2 +-
 grub-core/loader/multiboot_mbi2.c     |    2 +-
 grub-core/loader/xnu.c                |    2 +-
 grub-core/loader/xnu_resume.c         |    2 +-
 include/grub/relocator.h              |    3 +-
 include/grub/relocator_private.h      |    2 +
 18 files changed, 129 insertions(+), 29 deletions(-)
ChangeLog +10 -0
index 028691d..0a94fc0 100644
@@ -1,5 +1,15 @@
 2012-02-03  Matthew Garrett  <[email protected]>
 
+	* grub-core/lib/efi/relocator.c (grub_relocator_alloc_chunk_addr):
+	Add argument to fail allocation when target address overlaps
+	firmware regions. All users updated.
+	* grub-core/lib/efi/relocator.c (grub_relocator_firmware_overlaps):
+	New function
+	* grub-core/lib/ieee1275/relocator.c (grub_relocator_firmware_overlaps):
+	Likewise
+
+2012-02-03  Matthew Garrett  <[email protected]>
+
 	* include/grub/i386/linux.h (linux_kernel_header): Update to
 	boot protocol 2.10.
 	(linux_kernel_params): Likewise
grub-core/lib/efi/relocator.c +47 -0

53 lines not on this page · raw message

grub-core/lib/ieee1275/relocator.c +27 -0
index c09f1e9..31cedcb 100644
@@ -42,6 +42,33 @@ grub_relocator_firmware_get_max_events (void)
   return 2 * counter;
 }
 
+unsigned
+grub_relocator_firmware_overlaps (grub_phys_addr_t target, grub_size_t size)
+{
+  int overlap = 0;
+  auto int NESTED_FUNC_ATTR check (grub_uint64_t addr, grub_uint64_t len,
+				  grub_memory_type_t type);
+  int NESTED_FUNC_ATTR check (grub_uint64_t addr, grub_uint64_t len,
+			     grub_memory_type_t type)
+  {
+    grub_uint64_t end = addr + len;
+
+    if (type == GRUB_MEMORY_AVAILABLE)
+      return 0;
+
+    if (target >= addr && target <= end)
+      overlap = 1;
+
+    if (target + size >= addr && target + size <= end)
+      overlap = 1;
+
+    return 0;
+  }
+
+  grub_machine_mmap_iterate (check);
+  return overlap;
+}
+
 unsigned 
 grub_relocator_firmware_fill_events (struct grub_relocator_mmap_event *events)
 {
grub-core/lib/relocator.c +11 -1
index 6630646..7e202a5 100644
@@ -1203,7 +1203,8 @@ adjust_limits (struct grub_relocator *rel,
 grub_err_t
 grub_relocator_alloc_chunk_addr (struct grub_relocator *rel,
 				 grub_relocator_chunk_t *out,
-				 grub_phys_addr_t target, grub_size_t size)
+				 grub_phys_addr_t target, grub_size_t size,
+				 int avoid_firmware)
 {
   struct grub_relocator_chunk *chunk;
   grub_phys_addr_t min_addr = 0, max_addr;
@@ -1213,6 +1214,15 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel,
 
   adjust_limits (rel, &min_addr, &max_addr, target, target);
 
+  if (avoid_firmware)
+    {
+#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS
+      if (grub_relocator_firmware_overlaps(target, size))
+
+      return grub_error(GRUB_ERR_BAD_ARGUMENT, "target overlaps with firmware");
+#endif
+    }
+
   for (chunk = rel->chunks; chunk; chunk = chunk->next)
     if ((chunk->target <= target && target < chunk->target + chunk->size)
 	|| (target <= chunk->target && chunk->target < target + size))
grub-core/loader/i386/bsd.c +11 -8
index 835f146..f7b89a6 100644
@@ -606,7 +606,7 @@ grub_freebsd_boot (void)
   {
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					   kern_end, p_size);
+					   kern_end, p_size, 0);
     if (err)
       return err;
     p = get_virtual_current_address (ch);
@@ -788,7 +788,7 @@ grub_openbsd_boot (void)
     err = grub_relocator_alloc_chunk_addr (relocator, &ch, buf_target,
 					   tag_buf_len
 					   + sizeof (struct grub_openbsd_bootargs)
-					   + 9 * sizeof (grub_uint32_t));
+					   + 9 * sizeof (grub_uint32_t), 0);
     if (err)
       return err;
     buf0 = get_virtual_current_address (ch);
@@ -1079,7 +1079,8 @@ grub_netbsd_boot (void)
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
 					   arg_target, tag_buf_len
 					   + sizeof (struct grub_netbsd_bootinfo)
-					   + tag_count * sizeof (grub_uint32_t));
+					   + tag_count * sizeof (grub_uint32_t),
+					   0);
     if (err)
       return err;
     curarg = get_virtual_current_address (ch);
@@ -1219,7 +1220,8 @@ grub_bsd_load_aout (grub_file_t file, const char *filename)
     grub_relocator_chunk_t ch;
 
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					   kern_start, kern_end - kern_start);
+					   kern_start, kern_end - kern_start,
+					   0);
     if (err)
       return err;
     kern_chunk_src = get_virtual_current_address (ch);
@@ -1330,7 +1332,8 @@ grub_bsd_load_elf (grub_elf_t elf)
       if (err)
 	return err;
       err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					     kern_start, kern_end - kern_start);
+					     kern_start, kern_end - kern_start,
+					     0);
       if (err)
 	return err;
 
@@ -1373,7 +1376,7 @@ grub_bsd_load_elf (grub_elf_t elf)
 	grub_relocator_chunk_t ch;
 
 	err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start,
-					       kern_end - kern_start);
+					       kern_end - kern_start, 0);
 	if (err)
 	  return err;
 	kern_chunk_src = get_virtual_current_address (ch);
@@ -1837,7 +1840,7 @@ grub_cmd_freebsd_module (grub_command_t cmd __attribute__ ((unused)),
   {
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_end, 
-					   file->size);
+					   file->size, 0);
     if (err)
       goto fail;
     src = get_virtual_current_address (ch);
@@ -1888,7 +1891,7 @@ grub_netbsd_module_load (char *filename, grub_uint32_t type)
   {
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_end, 
-					   file->size);
+					   file->size, 0);
     if (err)
       goto fail;
 
grub-core/loader/i386/bsdXX.c +4 -4
index 7cec7a8..239c633 100644
@@ -106,7 +106,7 @@ SUFFIX (grub_freebsd_load_elfmodule_obj) (struct grub_relocator *relocator,
   {
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					   module, chunk_size);
+					   module, chunk_size, 0);
     if (err)
       return err;
     chunk_src = get_virtual_current_address (ch);
@@ -204,7 +204,7 @@ SUFFIX (grub_freebsd_load_elfmodule) (struct grub_relocator *relocator,
     grub_relocator_chunk_t ch;
 
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					   module, chunk_size);
+					   module, chunk_size, 0);
     if (err)
       return err;
 
@@ -319,7 +319,7 @@ SUFFIX (grub_freebsd_load_elf_meta) (struct grub_relocator *relocator,
   {
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					   symtarget, chunk_size);
+					   symtarget, chunk_size, 0);
     if (err)
       return err;
     sym_chunk = get_virtual_current_address (ch);
@@ -434,7 +434,7 @@ SUFFIX (grub_netbsd_load_elf_meta) (struct grub_relocator *relocator,
   {
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					   symtarget, chunk_size);
+					   symtarget, chunk_size, 0);
     if (err)
       return err;
     sym_chunk = get_virtual_current_address (ch);
grub-core/loader/i386/linux.c +2 -2
index a5bcb24..67a4533 100644
@@ -266,7 +266,7 @@ allocate_pages (grub_size_t prot_size)
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
 					   real_mode_target,
 					   (real_size + mmap_size 
-					    + efi_mmap_size));
+					    + efi_mmap_size), 0);
     if (err)
       goto fail;
     real_mode_mem = get_virtual_current_address (ch);
@@ -278,7 +278,7 @@ allocate_pages (grub_size_t prot_size)
   {
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					   prot_mode_target, prot_size);
+					   prot_mode_target, prot_size, 0);
     if (err)
       goto fail;
     prot_mode_mem = get_virtual_current_address (ch);
grub-core/loader/i386/multiboot_mbi.c +1 -1
index abd4ae9..a95abc9 100644
@@ -119,7 +119,7 @@ grub_multiboot_load (grub_file_t file)
 
       err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
 					     &ch, header->load_addr,
-					     code_size);
+					     code_size, 0);
       if (err)
 	{
 	  grub_dprintf ("multiboot_loader", "Error loading aout kludge\n");
grub-core/loader/i386/pc/freedos.c +1 -1
index 1f088e2..24b6dce 100644
@@ -101,7 +101,7 @@ grub_cmd_freedos (grub_command_t cmd __attribute__ ((unused)),
   {
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_addr (rel, &ch, GRUB_FREEDOS_SEGMENT << 4,
-					   kernelsyssize);
+					   kernelsyssize, 0);
     if (err)
       goto fail;
     kernelsys = get_virtual_current_address (ch);
grub-core/loader/i386/pc/linux.c +2 -2
index 7c4a4be..dee4126 100644
@@ -273,7 +273,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
 					   grub_linux_real_target,
 					   GRUB_LINUX_CL_OFFSET
-					   + maximal_cmdline_size);
+					   + maximal_cmdline_size, 0);
     if (err)
       return err;
     grub_linux_real_chunk = get_virtual_current_address (ch);
@@ -317,7 +317,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
 					   grub_linux_prot_target,
-					   grub_linux16_prot_size);
+					   grub_linux16_prot_size, 0);
     if (err)
       return err;
     grub_linux_prot_chunk = get_virtual_current_address (ch);
grub-core/loader/i386/pc/ntldr.c +2 -2
index 153b605..f613887 100644
@@ -97,7 +97,7 @@ grub_cmd_ntldr (grub_command_t cmd __attribute__ ((unused)),
   {
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_addr (rel, &ch, 0x7C00,
-					   GRUB_DISK_SECTOR_SIZE);
+					   GRUB_DISK_SECTOR_SIZE, 0);
     if (err)
       goto fail;
     bs = get_virtual_current_address (ch);
@@ -124,7 +124,7 @@ grub_cmd_ntldr (grub_command_t cmd __attribute__ ((unused)),
   {
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_addr (rel, &ch, GRUB_NTLDR_SEGMENT << 4,
-					   ntldrsize);
+					   ntldrsize, 0);
     if (err)
       goto fail;
     ntldr = get_virtual_current_address (ch);
grub-core/loader/mips/linux.c +3 -3
index 7fda817..8fba174 100644
@@ -87,7 +87,7 @@ grub_linux_boot (void)
 
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
 					   ((16 << 20) - 264),
-					   grub_strlen (params) + 1 + 8);
+					   grub_strlen (params) + 1 + 8, 0);
     if (err)
       return err;
     memsize = get_virtual_current_address (ch);
@@ -159,7 +159,7 @@ grub_linux_load32 (grub_elf_t elf, void **extra_mem, grub_size_t extra_size)
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
 					   target_addr & 0x1fffffff,
-					   linux_size);
+					   linux_size, 0);
     if (err)
       return err;
     playground = get_virtual_current_address (ch);
@@ -214,7 +214,7 @@ grub_linux_load64 (grub_elf_t elf, void **extra_mem, grub_size_t extra_size)
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
 					   target_addr & 0x1fffffff,
-					   linux_size);
+					   linux_size, 0);
     if (err)
       return err;
     playground = get_virtual_current_address (ch);
grub-core/loader/multiboot_elfxx.c +1 -1
index c6c88f1..9bbc10b 100644
@@ -102,7 +102,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, void *buffer)
 	    grub_relocator_chunk_t ch;
 	    err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
 						   &ch, phdr(i)->p_paddr,
-						   phdr(i)->p_memsz);
+						   phdr(i)->p_memsz, 0);
 	    if (err)
 	      {
 		grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i);
grub-core/loader/multiboot_mbi2.c +1 -1
index 9a81afe..15760d0 100644
@@ -227,7 +227,7 @@ grub_multiboot_load (grub_file_t file)
 
       err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
 					     &ch, addr_tag->load_addr,
-					     code_size);
+					     code_size, 0);
       if (err)
 	{
 	  grub_dprintf ("multiboot_loader", "Error loading aout kludge\n");
grub-core/loader/xnu.c +1 -1
index 44a440b..2d9482d 100644
@@ -60,7 +60,7 @@ grub_xnu_heap_malloc (int size, void **src, grub_addr_t *target)
   
   err = grub_relocator_alloc_chunk_addr (grub_xnu_relocator, &ch,
 					 grub_xnu_heap_target_start
-					 + grub_xnu_heap_size, size);
+					 + grub_xnu_heap_size, size, 0);
   if (err)
     return err;
 
grub-core/loader/xnu_resume.c +1 -1
index d7f9ada..e4fdb95 100644
@@ -110,7 +110,7 @@ grub_xnu_resume (char *imagename)
   {
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_addr (grub_xnu_relocator, &ch, codedest,
-					   codesize + GRUB_XNU_PAGESIZE);
+					   codesize + GRUB_XNU_PAGESIZE, 0);
     if (err)
       {
 	grub_file_close (file);
include/grub/relocator.h +2 -1
index 653a00e..b250512 100644
@@ -33,7 +33,8 @@ struct grub_relocator *grub_relocator_new (void);
 grub_err_t
 grub_relocator_alloc_chunk_addr (struct grub_relocator *rel,
 				 grub_relocator_chunk_t *out,
-				 grub_phys_addr_t target, grub_size_t size);
+				 grub_phys_addr_t target, grub_size_t size,
+				 int avoid_firmware);
 
 void *
 get_virtual_current_address (grub_relocator_chunk_t in);
include/grub/relocator_private.h +2 -0
index 1c563cb..1948af4 100644
@@ -104,6 +104,8 @@ struct grub_relocator_mmap_event
 #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS
 int grub_relocator_firmware_alloc_region (grub_phys_addr_t start,
 					  grub_size_t size);
+unsigned grub_relocator_firmware_overlaps (grub_phys_addr_t start,
+					   grub_size_t size);
 unsigned grub_relocator_firmware_fill_events (struct grub_relocator_mmap_event *events);
 unsigned grub_relocator_firmware_get_max_events (void);
 void grub_relocator_firmware_free_region (grub_phys_addr_t start,
Signature · 1 line
1.7.7.6
#2 lore raw

PATCH 3/3 Update Linux loader to follow the kernel's preferences

In reply to Matthew Garrett

We should attempt to load the kernel at its preferred address, and if we can't do that then we should at least align it correctly. When doing so we should also make sure to avoid putting the kernel on top of any regions being used by the firmware.

---
 ChangeLog                     |    8 +++++
 grub-core/loader/i386/linux.c |   67 +++++++++++++++++++++++++++++++++++-----
 2 files changed, 66 insertions(+), 9 deletions(-)
ChangeLog +8 -0
index 0a94fc0..1e0270b 100644
@@ -1,5 +1,13 @@
 2012-02-03  Matthew Garrett  <[email protected]>
 
+	* grub-core/loader/i386/linux.c (allocate_pages): Attempt to obtain
+	appropriately aligned memory if the desired target is unavailable
+	(grub_cmd_linux): Update to match newer Linux boot protocols, and
+	attempt to load the kernel at its preferred address rather than
+	hardcoding.
+
+2012-02-03  Matthew Garrett  <[email protected]>
+
 	* grub-core/lib/efi/relocator.c (grub_relocator_alloc_chunk_addr):
 	Add argument to fail allocation when target address overlaps
 	firmware regions. All users updated.
grub-core/loader/i386/linux.c +58 -9

124 lines not on this page · raw message

Signature · 1 line
1.7.7.6
#3 lore raw

Re: [PATCH 3/3] Update Linux loader to follow the kernel's preferences

Fails to boot x86_64 Archlinux kernels 3.2.2 and 3.3-rc2 - premature end of file error. The first 2 patches work fine (no noticable difference). Only this patch leads to boot failure. Tested in Tianocore UDK DuetPkg 64-bit firmware. Also a syntax error (missing comma) in the very last change in this patch.

- Keshav

PS: Sent from my mobile. Pls ignore top posting.

Quoted 182 lines · Matthew Garrett

We should attempt to load the kernel at its preferred address, and if we can't do that then we should at least align it correctly. When doing so we should also make sure to avoid putting the kernel on top of any regions being used by the firmware.

---
 ChangeLog                     |    8 +++++
 grub-core/loader/i386/linux.c |   67
+++++++++++++++++++++++++++++++++++-----
 2 files changed, 66 insertions(+), 9 deletions(-)
ChangeLog +8 -0
index 0a94fc0..1e0270b 100644
@@ -1,5 +1,13 @@
 2012-02-03  Matthew Garrett  <[email protected]>
 
+	* grub-core/loader/i386/linux.c (allocate_pages): Attempt to obtain
+	appropriately aligned memory if the desired target is unavailable
+	(grub_cmd_linux): Update to match newer Linux boot protocols, and
+	attempt to load the kernel at its preferred address rather than
+	hardcoding.
+
+2012-02-03  Matthew Garrett  <[email protected]>
+
 	* grub-core/lib/efi/relocator.c (grub_relocator_alloc_chunk_addr):
 	Add argument to fail allocation when target address overlaps
 	firmware regions. All users updated.
grub-core/loader/i386/linux.c +26 -6
index 67a4533..b61749d 100644
@@ -183,13 +183,14 @@ free_pages (void)
   grub_relocator_unload (relocator);
   relocator = NULL;
   real_mode_mem = prot_mode_mem = initrd_mem = 0;
-  real_mode_target = prot_mode_target = initrd_mem_target = 0;
+  real_mode_target = initrd_mem_target = 0;
 }
 
 /* Allocate pages for the real mode code and the protected mode code
    for linux as well as a memory map buffer.  */
 static grub_err_t
-allocate_pages (grub_size_t prot_size)
+allocate_pages (grub_size_t prot_size, grub_size_t *align,
+		grub_size_t min_align)
 {
   grub_size_t real_size, mmap_size;
   grub_err_t err;
@@ -269,18 +270,37 @@ allocate_pages (grub_size_t prot_size)
 					    + efi_mmap_size), 0);
     if (err)
       goto fail;
+
+    grub_errno = GRUB_ERR_NONE;
     real_mode_mem = get_virtual_current_address (ch);
   }
   efi_mmap_buf = (grub_uint8_t *) real_mode_mem + real_size + mmap_size;
 
-  prot_mode_target = GRUB_LINUX_BZIMAGE_ADDR;
-
   {
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					   prot_mode_target, prot_size, 0);
+					   prot_mode_target, prot_size, 1);
+    if (err)
+      {
+	unsigned int i;
+	for (i = *align; i >= min_align; i--)
+	  {
+	    err = grub_relocator_alloc_chunk_align (relocator, &ch,
+						    0x1000000, 0xffffffff,
+						    prot_size, 1 << i,
+						    GRUB_RELOCATOR_PREFERENCE_LOW);
+	    if (!err)
+	      {
+		*align = i;
+		prot_mode_target = get_physical_target_address (ch);
+		break;
+	      }
+	  }
+      }
+
     if (err)
       goto fail;
+
     prot_mode_mem = get_virtual_current_address (ch);
   }
 
@@ -631,7 +651,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__
 ((unused)),
   struct linux_kernel_header lh;
   struct linux_kernel_params *params;
   grub_uint8_t setup_sects;
-  grub_size_t real_size, prot_size;
+  grub_size_t real_size, prot_size, prot_file_size, align = 0, min_align =
 0;
   grub_ssize_t len;
   int i;
grub-core/loader/i386/linux.c +24 -2
@@ -705,9 +725,31 @@ grub_cmd_linux (grub_command_t cmd __attribute__
 ((unused)),
     setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
 
   real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
-  prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE;
 
-  if (allocate_pages (prot_size))
+  if (grub_le_to_cpu16 (lh.version) >= 0x205)
+    {
+      for (align = 0; align < 32; align++)
+	{
+	  if (grub_le_to_cpu32 (lh.kernel_alignment) & (1 << align))
+	    break;
+	}
+    }
+
+  if (grub_le_to_cpu16 (lh.version) >= 0x020a)
+    {
+      min_align = lh.min_alignment;
+      prot_size = grub_le_to_cpu32 (lh.init_size);
+      prot_mode_target = grub_le_to_cpu64 (lh.pref_address);
+    }
+  else
+    {
+      min_align = 0;
+      prot_size = grub_file_size (file) - real_size -
 GRUB_DISK_SECTOR_SIZE;
+      prot_mode_target = grub_le_to_cpu32 (lh.code32_start);
+    }
+
+  prot_file_size = grub_file_size (file) - real_size -
 GRUB_DISK_SECTOR_SIZE;
+  if (allocate_pages (prot_size, &align, min_align))
     goto fail;
   params = (struct linux_kernel_params *) real_mode_mem;
grub-core/loader/i386/linux.c +2 -0
@@ -715,6 +757,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__
 ((unused)),
   grub_memcpy (&params->setup_sects, &lh.setup_sects, sizeof (lh) - 0x1F1);
 
   params->ps_mouse = params->padding10 =  0;
+  params->code32_start = prot_mode_target;
+  params->kernel_alignment = (1 << align);
 
   len = 0x400 - sizeof (lh);
   if (grub_file_read (file, (char *) real_mode_mem + sizeof (lh), len) !=

len)

grub-core/loader/i386/linux.c +1 -1
@@ -774,7 +818,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__
 ((unused)),
   grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
 
   grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
-		(unsigned) real_size, (unsigned) prot_size);
+		(unsigned) real_size, (unsigned) prot_file_size);
 
   /* Look for memory size and video mode specified on the command line.  */
   linux_mem_size = 0;
grub-core/loader/i386/linux.c +5 -0
@@ -916,6 +960,11 @@ grub_cmd_linux (grub_command_t cmd __attribute__
 ((unused)),
     grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
 		argv[0]);
 
+  len = prot_file_size;
+  if (grub_file_read (file, prot_mode_mem, len) != len && !grub_errno)
+    grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s")
+		argv[0]);
+
   if (grub_errno == GRUB_ERR_NONE)
     {

grub_loader_set (grub_linux_boot, grub_linux_unload,
--
1.7.7.6

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw

Re: [PATCH 3/3] Update Linux loader to follow the kernel's preferences

Tested with bzr r3856

Quoted 200 lines · Keshav P R

Fails to boot x86_64 Archlinux kernels 3.2.2 and 3.3-rc2 - premature end of file error. The first 2 patches work fine (no noticable difference). Only this patch leads to boot failure. Tested in Tianocore UDK DuetPkg 64-bit firmware. Also a syntax error (missing comma) in the very last change in this patch.

- Keshav

PS: Sent from my mobile. Pls ignore top posting.

Quoted 188 lines · Matthew Garrett

We should attempt to load the kernel at its preferred address, and if we can't do that then we should at least align it correctly. When doing so we should also make sure to avoid putting the kernel on top of any regions being used by the firmware.

---
 ChangeLog                     |    8 +++++
 grub-core/loader/i386/linux.c |   67
+++++++++++++++++++++++++++++++++++-----
 2 files changed, 66 insertions(+), 9 deletions(-)
ChangeLog +8 -0
index 0a94fc0..1e0270b 100644
@@ -1,5 +1,13 @@
 2012-02-03  Matthew Garrett  <[email protected]>
 
+	* grub-core/loader/i386/linux.c (allocate_pages): Attempt to obtain
+	appropriately aligned memory if the desired target is unavailable
+	(grub_cmd_linux): Update to match newer Linux boot protocols, and
+	attempt to load the kernel at its preferred address rather than
+	hardcoding.
+
+2012-02-03  Matthew Garrett  <[email protected]>
+
 	* grub-core/lib/efi/relocator.c (grub_relocator_alloc_chunk_addr):
 	Add argument to fail allocation when target address overlaps
 	firmware regions. All users updated.
grub-core/loader/i386/linux.c +26 -6
index 67a4533..b61749d 100644
@@ -183,13 +183,14 @@ free_pages (void)
   grub_relocator_unload (relocator);
   relocator = NULL;
   real_mode_mem = prot_mode_mem = initrd_mem = 0;
-  real_mode_target = prot_mode_target = initrd_mem_target = 0;
+  real_mode_target = initrd_mem_target = 0;
 }
 
 /* Allocate pages for the real mode code and the protected mode code
    for linux as well as a memory map buffer.  */
 static grub_err_t
-allocate_pages (grub_size_t prot_size)
+allocate_pages (grub_size_t prot_size, grub_size_t *align,
+		grub_size_t min_align)
 {
   grub_size_t real_size, mmap_size;
   grub_err_t err;
@@ -269,18 +270,37 @@ allocate_pages (grub_size_t prot_size)
 					    + efi_mmap_size), 0);
     if (err)
       goto fail;
+
+    grub_errno = GRUB_ERR_NONE;
     real_mode_mem = get_virtual_current_address (ch);
   }
   efi_mmap_buf = (grub_uint8_t *) real_mode_mem + real_size + mmap_size;
 
-  prot_mode_target = GRUB_LINUX_BZIMAGE_ADDR;
-
   {
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					   prot_mode_target, prot_size, 0);
+					   prot_mode_target, prot_size, 1);
+    if (err)
+      {
+	unsigned int i;
+	for (i = *align; i >= min_align; i--)
+	  {
+	    err = grub_relocator_alloc_chunk_align (relocator, &ch,
+						    0x1000000, 0xffffffff,
+						    prot_size, 1 << i,
+						    GRUB_RELOCATOR_PREFERENCE_LOW);
+	    if (!err)
+	      {
+		*align = i;
+		prot_mode_target = get_physical_target_address (ch);
+		break;
+	      }
+	  }
+      }
+
     if (err)
       goto fail;
+
     prot_mode_mem = get_virtual_current_address (ch);
   }
 
@@ -631,7 +651,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__
 ((unused)),
   struct linux_kernel_header lh;
   struct linux_kernel_params *params;
   grub_uint8_t setup_sects;
-  grub_size_t real_size, prot_size;
+  grub_size_t real_size, prot_size, prot_file_size, align = 0, min_align

=

0;
   grub_ssize_t len;
   int i;
grub-core/loader/i386/linux.c +24 -2
@@ -705,9 +725,31 @@ grub_cmd_linux (grub_command_t cmd __attribute__
 ((unused)),
     setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
 
   real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
-  prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE;
 
-  if (allocate_pages (prot_size))
+  if (grub_le_to_cpu16 (lh.version) >= 0x205)
+    {
+      for (align = 0; align < 32; align++)
+	{
+	  if (grub_le_to_cpu32 (lh.kernel_alignment) & (1 << align))
+	    break;
+	}
+    }
+
+  if (grub_le_to_cpu16 (lh.version) >= 0x020a)
+    {
+      min_align = lh.min_alignment;
+      prot_size = grub_le_to_cpu32 (lh.init_size);
+      prot_mode_target = grub_le_to_cpu64 (lh.pref_address);
+    }
+  else
+    {
+      min_align = 0;
+      prot_size = grub_file_size (file) - real_size -
 GRUB_DISK_SECTOR_SIZE;
+      prot_mode_target = grub_le_to_cpu32 (lh.code32_start);
+    }
+
+  prot_file_size = grub_file_size (file) - real_size -
 GRUB_DISK_SECTOR_SIZE;
+  if (allocate_pages (prot_size, &align, min_align))
     goto fail;
   params = (struct linux_kernel_params *) real_mode_mem;
grub-core/loader/i386/linux.c +2 -0
@@ -715,6 +757,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__
 ((unused)),
   grub_memcpy (&params->setup_sects, &lh.setup_sects, sizeof (lh) -
 0x1F1);
 
   params->ps_mouse = params->padding10 =  0;
+  params->code32_start = prot_mode_target;
+  params->kernel_alignment = (1 << align);
 

len = 0x400 - sizeof (lh);
if (grub_file_read (file, (char *) real_mode_mem + sizeof (lh), len)
!=
len)

grub-core/loader/i386/linux.c +1 -1
@@ -774,7 +818,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__
 ((unused)),
   grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
 
   grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
-		(unsigned) real_size, (unsigned) prot_size);
+		(unsigned) real_size, (unsigned) prot_file_size);
 
   /* Look for memory size and video mode specified on the command line.

*/
linux_mem_size = 0;

grub-core/loader/i386/linux.c +5 -0
@@ -916,6 +960,11 @@ grub_cmd_linux (grub_command_t cmd __attribute__
 ((unused)),
     grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file
 %s"),
 		argv[0]);
 
+  len = prot_file_size;
+  if (grub_file_read (file, prot_mode_mem, len) != len && !grub_errno)
+    grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s")
+		argv[0]);
+
   if (grub_errno == GRUB_ERR_NONE)

{
grub_loader_set (grub_linux_boot, grub_linux_unload,
--
1.7.7.6

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel