Skip to contentSkip to conversation

multiboot2: Add two extensions

PATCH Superseded Daniel Kiper 3 participants 31 messages Mar 2, 2016 – Mar 14, 2016

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover multiboot2: Add two extensions

Hi,

This patch series:
- enables EFI boot services usage in loaded images by multiboot2 protocol,
- add support for multiboot2 protocol compatible relocatable images.

Earlier versions of this patch series are extensively tested and used internally at least in Oracle. It should be mentioned that this release does not change any functionality introduced by earlier releases. It just takes into account comments posted by various people.

Hmmm... Ugh... Cough... Is it possible to get this stuff into 2.02 train?

Daniel

 grub-core/lib/i386/relocator.c        |   48 +++++++++++++++++++
 grub-core/lib/i386/relocator64.S      |    3 ++
 grub-core/loader/i386/multiboot_mbi.c |    6 ++-
 grub-core/loader/multiboot.c          |   63 ++++++++++++++++++++----
 grub-core/loader/multiboot_elfxx.c    |   28 ++++++++---
 grub-core/loader/multiboot_mbi2.c     |  205 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
 include/grub/i386/multiboot.h         |   11 +++++
 include/grub/i386/relocator.h         |   21 ++++++++
 include/grub/multiboot.h              |    4 +-
 include/multiboot2.h                  |   41 ++++++++++++++++
 10 files changed, 357 insertions(+), 73 deletions(-)

Daniel Kiper (4):
i386/relocator: Add grub_relocator64_efi relocator
multiboot2: Add tags used to pass ImageHandle to loaded image
multiboot2: Do not pass memory maps to image if EFI boot services are enabled
multiboot2: Add support for relocatable images

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#7 lore raw

PATCH v3 2/4 multiboot2: Add tags used to pass ImageHandle to loaded image

Add tags used to pass ImageHandle to loaded image if requested. It is used by at least ExitBootServices() function.

---
v3 - suggestions/fixes:
- mbi EFI related stuff size calculation
should depend on target architecture
(suggested by Konrad Rzeszutek Wilk),
- use plain type instead of pointer
dereference as sizeof() argument
(suggested by Konrad Rzeszutek Wilk),
- improve commit message
(suggested by Konrad Rzeszutek Wilk).

---
 grub-core/loader/multiboot_mbi2.c |   50 ++++++++++++++++++++++++++++++-------
 include/multiboot2.h              |   16 ++++++++++++
 2 files changed, 57 insertions(+), 9 deletions(-)
grub-core/loader/multiboot_mbi2.c +41 -9
index a3dca90..7591edc 100644
@@ -172,6 +172,8 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	      case MULTIBOOT_TAG_TYPE_NETWORK:
 	      case MULTIBOOT_TAG_TYPE_EFI_MMAP:
 	      case MULTIBOOT_TAG_TYPE_EFI_BS:
+	      case MULTIBOOT_TAG_TYPE_EFI32_IH:
+	      case MULTIBOOT_TAG_TYPE_EFI64_IH:
 		break;
 
 	      default:
@@ -407,16 +409,22 @@ grub_multiboot_get_mbi_size (void)
 		 + grub_get_multiboot_mmap_count ()
 		 * sizeof (struct multiboot_mmap_entry)), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer), MULTIBOOT_TAG_ALIGN)
+#ifdef GRUB_MACHINE_EFI
+#ifdef __i386__
     + ALIGN_UP (sizeof (struct multiboot_tag_efi32), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi32_ih), MULTIBOOT_TAG_ALIGN)
+#endif
+#ifdef __x86_64__
     + ALIGN_UP (sizeof (struct multiboot_tag_efi64), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi64_ih), MULTIBOOT_TAG_ALIGN)
+#endif
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
+		+ efi_mmap_size, MULTIBOOT_TAG_ALIGN)
+#endif
     + ALIGN_UP (sizeof (struct multiboot_tag_old_acpi)
 		+ sizeof (struct grub_acpi_rsdp_v10), MULTIBOOT_TAG_ALIGN)
     + acpiv2_size ()
     + net_size ()
-#ifdef GRUB_MACHINE_EFI
-    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
-		+ efi_mmap_size, MULTIBOOT_TAG_ALIGN)
-#endif
     + sizeof (struct multiboot_tag_vbe) + MULTIBOOT_TAG_ALIGN - 1
     + sizeof (struct multiboot_tag_apm) + MULTIBOOT_TAG_ALIGN - 1;
 }
@@ -907,11 +915,35 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
 
   if (keep_bs)
     {
-      struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
-      tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
-      tag->size = sizeof (struct multiboot_tag);
-      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-	/ sizeof (grub_properly_aligned_t);
+      {
+	struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
+	tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
+	tag->size = sizeof (struct multiboot_tag);
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
+      }
+
+#ifdef __i386__
+      {
+	struct multiboot_tag_efi32_ih *tag = (struct multiboot_tag_efi32_ih *) ptrorig;
+	tag->type = MULTIBOOT_TAG_TYPE_EFI32_IH;
+	tag->size = sizeof (struct multiboot_tag_efi32_ih);
+	tag->pointer = (grub_addr_t) grub_efi_image_handle;
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
+      }
+#endif
+
+#ifdef __x86_64__
+      {
+	struct multiboot_tag_efi64_ih *tag = (struct multiboot_tag_efi64_ih *) ptrorig;
+	tag->type = MULTIBOOT_TAG_TYPE_EFI64_IH;
+	tag->size = sizeof (struct multiboot_tag_efi64_ih);
+	tag->pointer = (grub_addr_t) grub_efi_image_handle;
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
+      }
+#endif
     }
 #endif
 
include/multiboot2.h +16 -0
index d96aa40..36a174f 100644
@@ -60,6 +60,8 @@
 #define MULTIBOOT_TAG_TYPE_NETWORK           16
 #define MULTIBOOT_TAG_TYPE_EFI_MMAP          17
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
+#define MULTIBOOT_TAG_TYPE_EFI32_IH          19
+#define MULTIBOOT_TAG_TYPE_EFI64_IH          20
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -371,6 +373,20 @@ struct multiboot_tag_efi_mmap
   multiboot_uint8_t efi_mmap[0];
 }; 
 
+struct multiboot_tag_efi32_ih
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t pointer;
+};
+
+struct multiboot_tag_efi64_ih
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint64_t pointer;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */
Signature · 7 lines
1.7.10.4


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#11 lore raw

PATCH v3 3/4 multiboot2: Do not pass memory maps to image if EFI boot services are enabled

In reply to Daniel Kiper

Do not pass memory maps to image if it asked for EFI boot services. Main reason for not providing maps is because they will likely be invalid. We do a few allocations after filling them, e.g. for relocator needs. Usually we do not care as we would already finish boot services. If we keep boot services then it is easier to not provide maps. However, if image needs memory maps and they are not provided by bootloader then it should get them itself just before ExitBootServices() call.

---
v3 - suggestions/fixes:
- improve commit message
(suggested by Konrad Rzeszutek Wilk and Vladimir 'phcoder' Serbinenko).

---
 grub-core/loader/multiboot_mbi2.c |   71 ++++++++++++++++++-------------------
 1 file changed, 35 insertions(+), 36 deletions(-)
grub-core/loader/multiboot_mbi2.c +35 -36
index 7591edc..ce68f48 100644
@@ -390,7 +390,7 @@ static grub_size_t
 grub_multiboot_get_mbi_size (void)
 {
 #ifdef GRUB_MACHINE_EFI
-  if (!efi_mmap_size)
+  if (!keep_bs && !efi_mmap_size)
     find_efi_mmap_size ();    
 #endif
   return 2 * sizeof (grub_uint32_t) + sizeof (struct multiboot_tag)
@@ -759,12 +759,13 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
       }
   }
 
-  {
-    struct multiboot_tag_mmap *tag = (struct multiboot_tag_mmap *) ptrorig;
-    grub_fill_multiboot_mmap (tag);
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-      / sizeof (grub_properly_aligned_t);
-  }
+  if (!keep_bs)
+    {
+      struct multiboot_tag_mmap *tag = (struct multiboot_tag_mmap *) ptrorig;
+      grub_fill_multiboot_mmap (tag);
+      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	/ sizeof (grub_properly_aligned_t);
+    }
 
   {
     struct multiboot_tag_elf_sections *tag
@@ -780,18 +781,19 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
       / sizeof (grub_properly_aligned_t);
   }
 
-  {
-    struct multiboot_tag_basic_meminfo *tag
-      = (struct multiboot_tag_basic_meminfo *) ptrorig;
-    tag->type = MULTIBOOT_TAG_TYPE_BASIC_MEMINFO;
-    tag->size = sizeof (struct multiboot_tag_basic_meminfo); 
+  if (!keep_bs)
+    {
+      struct multiboot_tag_basic_meminfo *tag
+	= (struct multiboot_tag_basic_meminfo *) ptrorig;
+      tag->type = MULTIBOOT_TAG_TYPE_BASIC_MEMINFO;
+      tag->size = sizeof (struct multiboot_tag_basic_meminfo);
 
-    /* Convert from bytes to kilobytes.  */
-    tag->mem_lower = grub_mmap_get_lower () / 1024;
-    tag->mem_upper = grub_mmap_get_upper () / 1024;
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-       / sizeof (grub_properly_aligned_t);
-  }
+      /* Convert from bytes to kilobytes.  */
+      tag->mem_lower = grub_mmap_get_lower () / 1024;
+      tag->mem_upper = grub_mmap_get_upper () / 1024;
+      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	/ sizeof (grub_properly_aligned_t);
+    }
 
   {
     struct grub_net_network_level_interface *net;
@@ -890,27 +892,24 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
     grub_efi_uintn_t efi_desc_size;
     grub_efi_uint32_t efi_desc_version;
 
-    tag->type = MULTIBOOT_TAG_TYPE_EFI_MMAP;
-    tag->size = sizeof (*tag) + efi_mmap_size;
-
     if (!keep_bs)
-      err = grub_efi_finish_boot_services (&efi_mmap_size, tag->efi_mmap, NULL,
-					   &efi_desc_size, &efi_desc_version);
-    else
       {
-	if (grub_efi_get_memory_map (&efi_mmap_size, (void *) tag->efi_mmap,
-				     NULL,
-				     &efi_desc_size, &efi_desc_version) <= 0)
-	  err = grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
+	tag->type = MULTIBOOT_TAG_TYPE_EFI_MMAP;
+	tag->size = sizeof (*tag) + efi_mmap_size;
+
+	err = grub_efi_finish_boot_services (&efi_mmap_size, tag->efi_mmap, NULL,
+					     &efi_desc_size, &efi_desc_version);
+
+	if (err)
+	  return err;
+
+	tag->descr_size = efi_desc_size;
+	tag->descr_vers = efi_desc_version;
+	tag->size = sizeof (*tag) + efi_mmap_size;
+
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
       }
-    if (err)
-      return err;
-    tag->descr_size = efi_desc_size;
-    tag->descr_vers = efi_desc_version;
-    tag->size = sizeof (*tag) + efi_mmap_size;
-
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-      / sizeof (grub_properly_aligned_t);
   }
 
   if (keep_bs)
Signature · 7 lines
1.7.10.4


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#1 lore raw

PATCH v3 1/4 i386/relocator: Add grub_relocator64_efi relocator

In reply to Daniel Kiper

Add grub_relocator64_efi relocator. It will be used on EFI 64-bit platforms when multiboot2 compatible image requests MULTIBOOT_TAG_TYPE_EFI_BS. Relocator will set lower parts of %rax and %rbx accordingly to multiboot2 specification. On the other hand processor mode, just before jumping into loaded image, will be set accordingly to Unified Extensible Firmware Interface Specification, Version 2.4 Errata B, section 2.3.4, x64 Platforms, boot services. This way loaded image will be able to use EFI boot services without any issues.

If idea is accepted I will prepare grub_relocator32_efi relocator too.

---
v3 - suggestions/fixes:
- reuse grub-core/lib/i386/relocator64.S code instead of creating separate assembly file (suggested by Vladimir 'phcoder' Serbinenko),
- grub_multiboot_boot() cleanup
(suggested by Vladimir 'phcoder' Serbinenko),
- reuse multiboot_header_tag_entry_address struct instead of creating new one for EFI 64-bit entry point (suggested by Vladimir 'phcoder' Serbinenko).

---
 grub-core/lib/i386/relocator.c    |   48 ++++++++++++++++++++++++++++++++++
 grub-core/lib/i386/relocator64.S  |    3 +++
 grub-core/loader/multiboot.c      |   51 +++++++++++++++++++++++++++++++++----
 grub-core/loader/multiboot_mbi2.c |   19 +++++++++++---
 include/grub/i386/multiboot.h     |   11 ++++++++
 include/grub/i386/relocator.h     |   21 +++++++++++++++
 include/multiboot2.h              |    1 +
 7 files changed, 145 insertions(+), 9 deletions(-)
grub-core/lib/i386/relocator.c +48 -0
index 71dd4f0..2b0c260 100644
@@ -69,6 +69,13 @@ extern grub_uint64_t grub_relocator64_rsi;
 extern grub_addr_t grub_relocator64_cr3;
 extern struct grub_i386_idt grub_relocator16_idt;
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+extern grub_uint8_t grub_relocator64_efi_start;
+extern grub_uint8_t grub_relocator64_efi_end;
+#endif
+#endif
+
 #define RELOCATOR_SIZEOF(x)	(&grub_relocator##x##_end - &grub_relocator##x##_start)
 
 grub_err_t
@@ -214,3 +221,44 @@ grub_relocator64_boot (struct grub_relocator *rel,
   /* Not reached.  */
   return GRUB_ERR_NONE;
 }
+
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+grub_err_t
+grub_relocator64_efi_boot (struct grub_relocator *rel,
+			   struct grub_relocator64_efi_state state)
+{
+  grub_err_t err;
+  void *relst;
+  grub_relocator_chunk_t ch;
+
+  err = grub_relocator_alloc_chunk_align (rel, &ch, 0,
+					  0x40000000 - RELOCATOR_SIZEOF (64_efi),
+					  RELOCATOR_SIZEOF (64_efi), 16,
+					  GRUB_RELOCATOR_PREFERENCE_NONE, 1);
+  if (err)
+    return err;
+
+  /* Do not touch %rsp! It points to EFI created stack. */
+  grub_relocator64_rax = state.rax;
+  grub_relocator64_rbx = state.rbx;
+  grub_relocator64_rcx = state.rcx;
+  grub_relocator64_rdx = state.rdx;
+  grub_relocator64_rip = state.rip;
+  grub_relocator64_rsi = state.rsi;
+
+  grub_memmove (get_virtual_current_address (ch), &grub_relocator64_efi_start,
+		RELOCATOR_SIZEOF (64_efi));
+
+  err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
+				       &relst, NULL);
+  if (err)
+    return err;
+
+  ((void (*) (void)) relst) ();
+
+  /* Not reached.  */
+  return GRUB_ERR_NONE;
+}
+#endif
+#endif
grub-core/lib/i386/relocator64.S +3 -0
index e4648d8..7a06b16 100644
@@ -73,6 +73,7 @@ VARIABLE(grub_relocator64_rsp)
 
 	movq	%rax, %rsp
 
+VARIABLE(grub_relocator64_efi_start)
 	/* mov imm64, %rax */
 	.byte 	0x48
 	.byte	0xb8
@@ -120,6 +121,8 @@ LOCAL(jump_addr):
 VARIABLE(grub_relocator64_rip)
 	.quad	0
 
+VARIABLE(grub_relocator64_efi_end)
+
 #ifndef __x86_64__
 	.p2align	4
 LOCAL(gdt):
grub-core/loader/multiboot.c +46 -5

63 lines not on this page · raw message

grub-core/loader/multiboot_mbi2.c +15 -4
index f147d67..a3dca90 100644
@@ -107,8 +107,8 @@ grub_multiboot_load (grub_file_t file, const char *filename)
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0;
-  grub_addr_t entry = 0;
+  int entry_specified = 0, efi_entry_specified = 0;
+  grub_addr_t entry = 0, efi_entry = 0;
   grub_uint32_t console_required = 0;
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
@@ -192,6 +192,13 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	entry = ((struct multiboot_header_tag_entry_address *) tag)->entry_addr;
 	break;
 
+      case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64:
+#if defined (GRUB_MACHINE_EFI) && defined (__x86_64__)
+	efi_entry_specified = 1;
+	efi_entry = ((struct multiboot_header_tag_entry_address *) tag)->entry_addr;
+#endif
+	break;
+
       case MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS:
 	if (!(((struct multiboot_header_tag_console_flags *) tag)->console_flags
 	    & MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED))
@@ -211,7 +218,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	break;
 
       case MULTIBOOT_HEADER_TAG_EFI_BS:
+#ifdef GRUB_MACHINE_EFI
 	keep_bs = 1;
+#endif
 	break;
 
       default:
@@ -224,7 +233,7 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	break;
       }
 
-  if (addr_tag && !entry_specified)
+  if (addr_tag && !entry_specified && !(keep_bs && efi_entry_specified))
     {
       grub_free (buffer);
       return grub_error (GRUB_ERR_UNKNOWN_OS,
@@ -287,7 +296,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	}
     }
 
-  if (entry_specified)
+  if (keep_bs && efi_entry_specified)
+    grub_multiboot_payload_eip = efi_entry;
+  else if (entry_specified)
     grub_multiboot_payload_eip = entry;
 
   if (fbtag)
include/grub/i386/multiboot.h +11 -0
index a1b9488..807a1de 100644
@@ -30,6 +30,17 @@
 #define MULTIBOOT_MBI_REGISTER ebx
 #define MULTIBOOT_ARCHITECTURE_CURRENT MULTIBOOT_ARCHITECTURE_I386
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+#define MULTIBOOT_EFI_INITIAL_STATE  { .rax = MULTIBOOT_BOOTLOADER_MAGIC,	\
+    .rcx = 0,									\
+    .rdx = 0,									\
+      }
+#define MULTIBOOT_EFI_ENTRY_REGISTER rip
+#define MULTIBOOT_EFI_MBI_REGISTER rbx
+#endif
+#endif
+
 #define MULTIBOOT_ELF32_MACHINE EM_386
 #define MULTIBOOT_ELF64_MACHINE EM_X86_64
 
include/grub/i386/relocator.h +21 -0
index 5f89a7e..2a56c3b 100644
@@ -65,6 +65,20 @@ struct grub_relocator64_state
   grub_addr_t cr3;
 };
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+struct grub_relocator64_efi_state
+{
+  grub_uint64_t rax;
+  grub_uint64_t rbx;
+  grub_uint64_t rcx;
+  grub_uint64_t rdx;
+  grub_uint64_t rip;
+  grub_uint64_t rsi;
+};
+#endif
+#endif
+
 grub_err_t grub_relocator16_boot (struct grub_relocator *rel,
 				  struct grub_relocator16_state state);
 
@@ -76,4 +90,11 @@ grub_err_t grub_relocator64_boot (struct grub_relocator *rel,
 				  struct grub_relocator64_state state,
 				  grub_addr_t min_addr, grub_addr_t max_addr);
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+grub_err_t grub_relocator64_efi_boot (struct grub_relocator *rel,
+				      struct grub_relocator64_efi_state state);
+#endif
+#endif
+
 #endif /* ! GRUB_RELOCATOR_CPU_HEADER */
include/multiboot2.h +1 -0
index 9d48627..d96aa40 100644
@@ -69,6 +69,7 @@
 #define MULTIBOOT_HEADER_TAG_FRAMEBUFFER  5
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
+#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
Signature · 7 lines
1.7.10.4


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#14 lore raw

PATCH v3 4/4 multiboot2: Add support for relocatable images

In reply to Daniel Kiper

Currently multiboot2 protocol loads image exactly at address specified in ELF or multiboot2 header. This solution works quite well on legacy BIOS platforms. It is possible because memory regions are placed at predictable addresses (though I was not able to find any spec which says that it is strong requirement, so, it looks that it is just a goodwill of hardware designers). However, EFI platforms are more volatile. Even if required memory regions live at specific addresses then they are sometimes simply not free (e.g. used by boot/runtime services on Dell PowerEdge R820 and OVMF). This means that you are not able to simply set up final image destination on build time. You have to provide method to relocate image contents to real load address which is usually different than load address specified in ELF and multiboot2 headers.

This patch provides all needed machinery to do self relocation in image code. First of all GRUB2 reads min_addr (min. load addr), max_addr (max. load addr), align (required image alignment), preference (it says which memory regions are preferred by image, e.g. none, low, high) from multiboot_header_tag_relocatable header tag contained in binary. Later loader tries to fulfill request (not only that one) and if it succeeds then it informs image about real load address via multiboot_tag_base_addr tag. At this stage GRUB2 role is finished. Starting from now executable must cope with relocations itself using whole static and dynamic knowledge provided by boot loader.

This patch does not provide functionality which could do relocations using ELF relocation data. However, I was asked by Konrad Rzeszutek Wilk and Vladimir 'phcoder' Serbinenko to investigate that thing. It looks that relevant machinery could be added to existing code (including this patch) without huge effort. Additionally, ELF relocation could live in parallel with self relocation provided by this patch. However, during research I realized that first of all we should establish the details how ELF relocatable image should look like and how it should be build. At least to build proper test/example files.

As I saw multiboot2 protocol is able to consume ET_EXEC and ET_DYN ELF files. Potentially we can use ET_DYN file type. It can be build with gcc/ld -pie option. However, it contains a lot of unneeded stuff (e.g. INTERP, DYNAMIC, GNU_EH_FRAME program headers) and it could be quite difficult to drop them (Hmmm... Is it possible to build it properly with custom ld script?). So, I have checked ET_EXEC file type. Sadly in this case linker by default resolves all local symbol relocations and removes relocation related sections. Fortunately it is possible to leave them as is with simple -q/--emit-relocs ld option. However, output file is quite fragile and any operation on it should be done with great care (e.g. strip should be called with --strip-unneeded option). So, this solution is not perfect too. It means that maybe we should look for better solution. However, I think that we should not use any custom tools and focus on functionalities provided by compiler and binutils. In this context ld scripts looks quite promising but maybe you have better solutions. So, what do you think about that?

This patch was tested with Xen image which uses that functionality. However, this Xen feature is still under development and new patchset will be released in about 3-4 weeks.

---
v3 - suggestions/fixes:
- reduce number of casts
(suggested by Konrad Rzeszutek Wilk),
- remove unneeded space at the end of line
(suggested by Konrad Rzeszutek Wilk),
- improve commit message
(suggested by Konrad Rzeszutek Wilk).

---
 grub-core/loader/i386/multiboot_mbi.c |    6 ++-
 grub-core/loader/multiboot.c          |   12 ++++--
 grub-core/loader/multiboot_elfxx.c    |   28 ++++++++++----
 grub-core/loader/multiboot_mbi2.c     |   65 ++++++++++++++++++++++++++++++---
 include/grub/multiboot.h              |    4 +-
 include/multiboot2.h                  |   24 ++++++++++++
 6 files changed, 120 insertions(+), 19 deletions(-)
grub-core/loader/i386/multiboot_mbi.c +4 -2
index f60b702..4fc83ed 100644
@@ -72,7 +72,8 @@ load_kernel (grub_file_t file, const char *filename,
   grub_err_t err;
   if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE)
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+				     GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
       if (err == GRUB_ERR_NONE) {
 	return GRUB_ERR_NONE;
       }
@@ -121,7 +122,8 @@ load_kernel (grub_file_t file, const char *filename,
       return GRUB_ERR_NONE;
     }
 
-  return grub_multiboot_load_elf (file, filename, buffer);
+  return grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+				  GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
 }
 
 static struct multiboot_header *
grub-core/loader/multiboot.c +9 -3
index 18038fd..c0f51b6 100644
@@ -208,12 +208,18 @@ static grub_uint64_t highest_load;
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-			 void *buffer)
+			 void *buffer, int relocatable, grub_uint32_t min_addr,
+			 grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+			 grub_uint32_t *base_addr, int avoid_efi_boot_services)
 {
   if (grub_multiboot_is_elf32 (buffer))
-    return grub_multiboot_load_elf32 (file, filename, buffer);
+    return grub_multiboot_load_elf32 (file, filename, buffer, relocatable,
+				      min_addr, max_addr, align, preference,
+				      base_addr, avoid_efi_boot_services);
   else if (grub_multiboot_is_elf64 (buffer))
-    return grub_multiboot_load_elf64 (file, filename, buffer);
+    return grub_multiboot_load_elf64 (file, filename, buffer, relocatable,
+				      min_addr, max_addr, align, preference,
+				      base_addr, avoid_efi_boot_services);
 
   return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF magic"));
 }
grub-core/loader/multiboot_elfxx.c +21 -7
index e3a39b6..0c01569 100644
@@ -51,7 +51,10 @@ CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
 }
 
 static grub_err_t
-CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, void *buffer)
+CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename,
+				     void *buffer, int relocatable, grub_uint32_t min_addr,
+				     grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+				     grub_uint32_t *base_addr, int avoid_efi_boot_services)
 {
   Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
   char *phdr_base;
@@ -89,19 +92,30 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi
 	  if (phdr(i)->p_paddr + phdr(i)->p_memsz > highest_load)
 	    highest_load = phdr(i)->p_paddr + phdr(i)->p_memsz;
 
-	  grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n",
-			i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
+	  grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx,"
+			"align=0x%lx, relocatable=%d, avoid_efi_boot_services=%d\n", i,
+			(long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr,
+			(long) align, relocatable, avoid_efi_boot_services);
 
 	  {
 	    grub_relocator_chunk_t ch;
-	    err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
-						   &ch, phdr(i)->p_paddr,
-						   phdr(i)->p_memsz);
+
+	    if (relocatable)
+	      err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
+						      min_addr, max_addr - phdr(i)->p_memsz,
+						      phdr(i)->p_memsz, align ? align : 1,
+						      preference, avoid_efi_boot_services);
+	    else
+	      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+						     &ch, phdr(i)->p_paddr,
+						     phdr(i)->p_memsz);
 	    if (err)
 	      {
 		grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i);
 		return err;
 	      }
+	    if (base_addr)
+	      *base_addr = get_physical_target_address (ch);
 	    source = get_virtual_current_address (ch);
 	  }
 
@@ -208,7 +222,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi
 						    + 1, sh->sh_size,
 						    sh->sh_addralign,
 						    GRUB_RELOCATOR_PREFERENCE_NONE,
-						    0);
+						    avoid_efi_boot_services);
 	    if (err)
 	      {
 		grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i);
grub-core/loader/multiboot_mbi2.c +59 -6

128 lines not on this page · raw message

include/grub/multiboot.h +3 -1
index e13c084..ec322b0 100644
@@ -94,7 +94,9 @@ grub_multiboot_load (grub_file_t file, const char *filename);
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-			 void *buffer);
+			 void *buffer, int relocatable, grub_uint32_t min_addr,
+			 grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+			 grub_uint32_t *base_addr, int avoid_efi_boot_services);
 extern grub_size_t grub_multiboot_pure_size;
 extern grub_size_t grub_multiboot_alloc_mbi;
 extern grub_uint32_t grub_multiboot_payload_eip;
include/multiboot2.h +24 -0
index 36a174f..c09bdbc 100644
@@ -62,6 +62,7 @@
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
 #define MULTIBOOT_TAG_TYPE_EFI32_IH          19
 #define MULTIBOOT_TAG_TYPE_EFI64_IH          20
+#define MULTIBOOT_TAG_TYPE_BASE_ADDR         21
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -72,11 +73,16 @@
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
 #define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
+#define MULTIBOOT_HEADER_TAG_RELOCATABLE  10
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
 #define MULTIBOOT_HEADER_TAG_OPTIONAL 1
 
+#define MULTIBOOT_LOAD_PREFERENCE_NONE 0
+#define MULTIBOOT_LOAD_PREFERENCE_LOW 1
+#define MULTIBOOT_LOAD_PREFERENCE_HIGH 2
+
 #define MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED 1
 #define MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED 2
 
@@ -161,6 +167,17 @@ struct multiboot_header_tag_module_align
   multiboot_uint32_t size;
 };
 
+struct multiboot_header_tag_relocatable
+{
+  multiboot_uint16_t type;
+  multiboot_uint16_t flags;
+  multiboot_uint32_t size;
+  multiboot_uint32_t min_addr;
+  multiboot_uint32_t max_addr;
+  multiboot_uint32_t align;
+  multiboot_uint32_t preference;
+};
+
 struct multiboot_color
 {
   multiboot_uint8_t red;
@@ -387,6 +404,13 @@ struct multiboot_tag_efi64_ih
   multiboot_uint64_t pointer;
 };
 
+struct multiboot_tag_base_addr
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t base_addr;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */
Signature · 7 lines
1.7.10.4


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#15 lore raw

Re: [GRUB2 PATCH v3 4/4] multiboot2: Add support for relocatable images

Quoted 73 lines · Daniel Kiper

Currently multiboot2 protocol loads image exactly at address specified in ELF or multiboot2 header. This solution works quite well on legacy BIOS platforms. It is possible because memory regions are placed at predictable addresses (though I was not able to find any spec which says that it is strong requirement, so, it looks that it is just a goodwill of hardware designers). However, EFI platforms are more volatile. Even if required memory regions live at specific addresses then they are sometimes simply not free (e.g. used by boot/runtime services on Dell PowerEdge R820 and OVMF). This means that you are not able to simply set up final image destination on build time. You have to provide method to relocate image contents to real load address which is usually different than load address specified in ELF and multiboot2 headers.

This patch provides all needed machinery to do self relocation in image code. First of all GRUB2 reads min_addr (min. load addr), max_addr (max. load addr), align (required image alignment), preference (it says which memory regions are preferred by image, e.g. none, low, high) from multiboot_header_tag_relocatable header tag contained in binary. Later loader tries to fulfill request (not only that one) and if it succeeds then it informs image about real load address via multiboot_tag_base_addr tag. At this stage GRUB2 role is finished. Starting from now executable must cope with relocations itself using whole static and dynamic knowledge provided by boot loader.

This patch does not provide functionality which could do relocations using ELF relocation data. However, I was asked by Konrad Rzeszutek Wilk and Vladimir 'phcoder' Serbinenko to investigate that thing. It looks that relevant machinery could be added to existing code (including this patch) without huge effort. Additionally, ELF relocation could live in parallel with self relocation provided by this patch. However, during research I realized that first of all we should establish the details how ELF relocatable image should look like and how it should be build. At least to build proper test/example files.

As I saw multiboot2 protocol is able to consume ET_EXEC and ET_DYN ELF files. Potentially we can use ET_DYN file type. It can be build with gcc/ld -pie option. However, it contains a lot of unneeded stuff (e.g. INTERP, DYNAMIC, GNU_EH_FRAME program headers) and it could be quite difficult to drop them (Hmmm... Is it possible to build it properly with custom ld script?). So, I have checked ET_EXEC file type. Sadly in this case linker by default resolves all local symbol relocations and removes relocation related sections. Fortunately it is possible to leave them as is with simple -q/--emit-relocs ld option. However, output file is quite fragile and any operation on it should be done with great care (e.g. strip should be called with --strip-unneeded option). So, this solution is not perfect too. It means that maybe we should look for better solution. However, I think that we should not use any custom tools and focus on functionalities provided by compiler and binutils. In this context ld scripts looks quite promising but maybe you have better solutions. So, what do you think about that?

This patch was tested with Xen image which uses that functionality. However, this Xen feature is still under development and new patchset will be released in about 3-4 weeks.

---
v3 - suggestions/fixes:
- reduce number of casts
(suggested by Konrad Rzeszutek Wilk),
- remove unneeded space at the end of line
(suggested by Konrad Rzeszutek Wilk),
- improve commit message
(suggested by Konrad Rzeszutek Wilk).

---
 grub-core/loader/i386/multiboot_mbi.c |    6 ++-
 grub-core/loader/multiboot.c          |   12 ++++--
 grub-core/loader/multiboot_elfxx.c    |   28 ++++++++++----
 grub-core/loader/multiboot_mbi2.c     |   65 ++++++++++++++++++++++++++++++---
 include/grub/multiboot.h              |    4 +-
 include/multiboot2.h                  |   24 ++++++++++++
 6 files changed, 120 insertions(+), 19 deletions(-)
grub-core/loader/i386/multiboot_mbi.c +0 -0
index f60b702..4fc83ed 100644

grub-core/loader/i386/multiboot_mbi.c load_kernel (grub_file_t file, const char *filename, · line 76

Quoted 6 lines
grub-core/loader/i386/multiboot_mbi.c +2 -1
@@ -72,7 +72,8 @@ load_kernel (grub_file_t file, const char *filename,
   grub_err_t err;
   if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE)
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+				     GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);

Uuh, really? You are adding 7 parameters for the relocatable case. Wouldn't it make more sense to have some kind of structure containing the data you need for relocatable images and pass the pointer to that structure or NULL in the case of non-relocatable images (or have a default structure for the non-relocatable case)?

Juergen

Quoted 308 lines
grub-core/loader/i386/multiboot_mbi.c +2 -1
       if (err == GRUB_ERR_NONE) {
 	return GRUB_ERR_NONE;
       }
@@ -121,7 +122,8 @@ load_kernel (grub_file_t file, const char *filename,
       return GRUB_ERR_NONE;
     }
 
-  return grub_multiboot_load_elf (file, filename, buffer);
+  return grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+				  GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
 }
 
 static struct multiboot_header *
grub-core/loader/multiboot.c +9 -3
index 18038fd..c0f51b6 100644
@@ -208,12 +208,18 @@ static grub_uint64_t highest_load;
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-			 void *buffer)
+			 void *buffer, int relocatable, grub_uint32_t min_addr,
+			 grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+			 grub_uint32_t *base_addr, int avoid_efi_boot_services)
 {
   if (grub_multiboot_is_elf32 (buffer))
-    return grub_multiboot_load_elf32 (file, filename, buffer);
+    return grub_multiboot_load_elf32 (file, filename, buffer, relocatable,
+				      min_addr, max_addr, align, preference,
+				      base_addr, avoid_efi_boot_services);
   else if (grub_multiboot_is_elf64 (buffer))
-    return grub_multiboot_load_elf64 (file, filename, buffer);
+    return grub_multiboot_load_elf64 (file, filename, buffer, relocatable,
+				      min_addr, max_addr, align, preference,
+				      base_addr, avoid_efi_boot_services);
 
   return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF magic"));
 }
grub-core/loader/multiboot_elfxx.c +21 -7
index e3a39b6..0c01569 100644
@@ -51,7 +51,10 @@ CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
 }
 
 static grub_err_t
-CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, void *buffer)
+CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename,
+				     void *buffer, int relocatable, grub_uint32_t min_addr,
+				     grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+				     grub_uint32_t *base_addr, int avoid_efi_boot_services)
 {
   Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
   char *phdr_base;
@@ -89,19 +92,30 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi
 	  if (phdr(i)->p_paddr + phdr(i)->p_memsz > highest_load)
 	    highest_load = phdr(i)->p_paddr + phdr(i)->p_memsz;
 
-	  grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n",
-			i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
+	  grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx,"
+			"align=0x%lx, relocatable=%d, avoid_efi_boot_services=%d\n", i,
+			(long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr,
+			(long) align, relocatable, avoid_efi_boot_services);
 
 	  {
 	    grub_relocator_chunk_t ch;
-	    err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
-						   &ch, phdr(i)->p_paddr,
-						   phdr(i)->p_memsz);
+
+	    if (relocatable)
+	      err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
+						      min_addr, max_addr - phdr(i)->p_memsz,
+						      phdr(i)->p_memsz, align ? align : 1,
+						      preference, avoid_efi_boot_services);
+	    else
+	      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+						     &ch, phdr(i)->p_paddr,
+						     phdr(i)->p_memsz);
 	    if (err)
 	      {
 		grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i);
 		return err;
 	      }
+	    if (base_addr)
+	      *base_addr = get_physical_target_address (ch);
 	    source = get_virtual_current_address (ch);
 	  }
 
@@ -208,7 +222,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi
 						    + 1, sh->sh_size,
 						    sh->sh_addralign,
 						    GRUB_RELOCATOR_PREFERENCE_NONE,
-						    0);
+						    avoid_efi_boot_services);
 	    if (err)
 	      {
 		grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i);
grub-core/loader/multiboot_mbi2.c +59 -6
index ce68f48..03725a1 100644
@@ -68,6 +68,7 @@ static grub_size_t elf_sec_num, elf_sec_entsize;
 static unsigned elf_sec_shstrndx;
 static void *elf_sections;
 static int keep_bs = 0;
+static grub_uint32_t base_addr = 0;
 
 void
 grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize,
@@ -107,11 +108,14 @@ grub_multiboot_load (grub_file_t file, const char *filename)
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0, efi_entry_specified = 0;
+  struct multiboot_header_tag_relocatable *rel_tag;
+  int entry_specified = 0, efi_entry_specified = 0, relocatable = 0;
   grub_addr_t entry = 0, efi_entry = 0;
-  grub_uint32_t console_required = 0;
+  grub_uint32_t console_required = 0, min_addr = 0;
+  grub_uint32_t max_addr = 0, preference = GRUB_RELOCATOR_PREFERENCE_NONE;
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
+  grub_size_t align = 0;
 
   buffer = grub_malloc (MULTIBOOT_SEARCH);
   if (!buffer)
@@ -174,6 +178,7 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	      case MULTIBOOT_TAG_TYPE_EFI_BS:
 	      case MULTIBOOT_TAG_TYPE_EFI32_IH:
 	      case MULTIBOOT_TAG_TYPE_EFI64_IH:
+	      case MULTIBOOT_TAG_TYPE_BASE_ADDR:
 		break;
 
 	      default:
@@ -215,6 +220,27 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	accepted_consoles |= GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER;
 	break;
 
+      case MULTIBOOT_HEADER_TAG_RELOCATABLE:
+	relocatable = 1;
+	rel_tag = (struct multiboot_header_tag_relocatable *) tag;
+	min_addr = rel_tag->min_addr;
+	max_addr = rel_tag->max_addr;
+	align = rel_tag->align;
+	switch (rel_tag->preference)
+	  {
+	  case MULTIBOOT_LOAD_PREFERENCE_LOW:
+	    preference = GRUB_RELOCATOR_PREFERENCE_LOW;
+	    break;
+
+	  case MULTIBOOT_LOAD_PREFERENCE_HIGH:
+	    preference = GRUB_RELOCATOR_PREFERENCE_HIGH;
+	    break;
+
+	  default:
+	    preference = GRUB_RELOCATOR_PREFERENCE_NONE;
+	  }
+	break;
+
 	/* GRUB always page-aligns modules.  */
       case MULTIBOOT_HEADER_TAG_MODULE_ALIGN:
 	break;
@@ -260,15 +286,22 @@ grub_multiboot_load (grub_file_t file, const char *filename)
       else
 	code_size = load_size;
 
-      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
-					     &ch, load_addr,
-					     code_size);
+      if (relocatable)
+	err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
+						min_addr, max_addr - code_size,
+						code_size, align ? align : 1,
+						preference, keep_bs);
+      else
+	err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+					       &ch, load_addr,
+					       code_size);
       if (err)
 	{
 	  grub_dprintf ("multiboot_loader", "Error loading aout kludge\n");
 	  grub_free (buffer);
 	  return err;
 	}
+      base_addr = get_physical_target_address (ch);
       source = get_virtual_current_address (ch);
 
       if ((grub_file_seek (file, offset)) == (grub_off_t) -1)
@@ -290,7 +323,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
     }
   else
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer,
+				     relocatable, min_addr, max_addr,
+				     align, preference, &base_addr, keep_bs);
       if (err)
 	{
 	  grub_free (buffer);
@@ -303,6 +338,14 @@ grub_multiboot_load (grub_file_t file, const char *filename)
   else if (entry_specified)
     grub_multiboot_payload_eip = entry;
 
+  if (relocatable)
+    {
+      if (base_addr > min_addr)
+	grub_multiboot_payload_eip += base_addr - min_addr;
+      else
+	grub_multiboot_payload_eip -= min_addr - base_addr;
+    }
+
   if (fbtag)
     err = grub_multiboot_set_console (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER,
 				      accepted_consoles,
@@ -409,6 +452,7 @@ grub_multiboot_get_mbi_size (void)
 		 + grub_get_multiboot_mmap_count ()
 		 * sizeof (struct multiboot_mmap_entry)), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_base_addr), MULTIBOOT_TAG_ALIGN)
 #ifdef GRUB_MACHINE_EFI
 #ifdef __i386__
     + ALIGN_UP (sizeof (struct multiboot_tag_efi32), MULTIBOOT_TAG_ALIGN)
@@ -698,6 +742,15 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
   ptrorig += (2 * sizeof (grub_uint32_t)) / sizeof (grub_properly_aligned_t);
 
   {
+    struct multiboot_tag_base_addr *tag = (struct multiboot_tag_base_addr *) ptrorig;
+    tag->type = MULTIBOOT_TAG_TYPE_BASE_ADDR;
+    tag->size = sizeof (struct multiboot_tag_base_addr);
+    tag->base_addr = base_addr;
+    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+       / sizeof (grub_properly_aligned_t);
+  }
+
+  {
     struct multiboot_tag_string *tag = (struct multiboot_tag_string *) ptrorig;
     tag->type = MULTIBOOT_TAG_TYPE_CMDLINE;
     tag->size = sizeof (struct multiboot_tag_string) + cmdline_size; 
include/grub/multiboot.h +3 -1
index e13c084..ec322b0 100644
@@ -94,7 +94,9 @@ grub_multiboot_load (grub_file_t file, const char *filename);
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-			 void *buffer);
+			 void *buffer, int relocatable, grub_uint32_t min_addr,
+			 grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+			 grub_uint32_t *base_addr, int avoid_efi_boot_services);
 extern grub_size_t grub_multiboot_pure_size;
 extern grub_size_t grub_multiboot_alloc_mbi;
 extern grub_uint32_t grub_multiboot_payload_eip;
include/multiboot2.h +17 -0
index 36a174f..c09bdbc 100644
@@ -62,6 +62,7 @@
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
 #define MULTIBOOT_TAG_TYPE_EFI32_IH          19
 #define MULTIBOOT_TAG_TYPE_EFI64_IH          20
+#define MULTIBOOT_TAG_TYPE_BASE_ADDR         21
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -72,11 +73,16 @@
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
 #define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
+#define MULTIBOOT_HEADER_TAG_RELOCATABLE  10
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
 #define MULTIBOOT_HEADER_TAG_OPTIONAL 1
 
+#define MULTIBOOT_LOAD_PREFERENCE_NONE 0
+#define MULTIBOOT_LOAD_PREFERENCE_LOW 1
+#define MULTIBOOT_LOAD_PREFERENCE_HIGH 2
+
 #define MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED 1
 #define MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED 2
 
@@ -161,6 +167,17 @@ struct multiboot_header_tag_module_align
   multiboot_uint32_t size;
 };
 
+struct multiboot_header_tag_relocatable
+{
+  multiboot_uint16_t type;
+  multiboot_uint16_t flags;
+  multiboot_uint32_t size;
+  multiboot_uint32_t min_addr;
+  multiboot_uint32_t max_addr;
+  multiboot_uint32_t align;
+  multiboot_uint32_t preference;
+};
+
 struct multiboot_color
 {
   multiboot_uint8_t red;
@@ -387,6 +404,13 @@ struct multiboot_tag_efi64_ih
   multiboot_uint64_t pointer;
 };
 

include/multiboot2.h struct multiboot_tag_efi64_ih · line 416

Quoted 10 lines
include/multiboot2.h +7 -0
+struct multiboot_tag_base_addr
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t base_addr;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#24 lore raw
In reply to Daniel Kiper
Quoted 8 lines · Daniel Kiper

Hi,

This patch series:
- enables EFI boot services usage in loaded images by multiboot2 protocol,
- add support for multiboot2 protocol compatible relocatable images.

Quoted 8 lines

Earlier versions of this patch series are extensively tested and used internally at least in Oracle. It should be mentioned that this release does not change any functionality introduced by earlier releases. It just takes into account comments posted by various people.

Hmmm... Ugh... Cough... Is it possible to get this stuff into 2.02 train?

Andrei, Vladimir, ping?

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#2 lore raw

Re: [GRUB2 PATCH v3 1/4] i386/relocator: Add grub_relocator64_efi relocator

In reply to Daniel Kiper
Quoted 43 lines · Daniel Kiper

Add grub_relocator64_efi relocator. It will be used on EFI 64-bit platforms when multiboot2 compatible image requests MULTIBOOT_TAG_TYPE_EFI_BS. Relocator
will set lower parts of %rax and %rbx accordingly to multiboot2 specification.
On the other hand processor mode, just before jumping into loaded image, will
be set accordingly to Unified Extensible Firmware Interface Specification, Version 2.4 Errata B, section 2.3.4, x64 Platforms, boot services. This way loaded image will be able to use EFI boot services without any issues.

If idea is accepted I will prepare grub_relocator32_efi relocator too.

Signed-off-by Daniel Kiper <[email protected] <javascript:;>>

---
v3 - suggestions/fixes:
- reuse grub-core/lib/i386/relocator64.S code instead of creating separate assembly file (suggested by Vladimir 'phcoder' Serbinenko),
- grub_multiboot_boot() cleanup
(suggested by Vladimir 'phcoder' Serbinenko),
- reuse multiboot_header_tag_entry_address struct instead of creating new one for EFI 64-bit entry point (suggested by Vladimir 'phcoder' Serbinenko).

---
 grub-core/lib/i386/relocator.c    |   48
++++++++++++++++++++++++++++++++++
 grub-core/lib/i386/relocator64.S  |    3 +++
 grub-core/loader/multiboot.c      |   51
+++++++++++++++++++++++++++++++++----
 grub-core/loader/multiboot_mbi2.c |   19 +++++++++++---
 include/grub/i386/multiboot.h     |   11 ++++++++
 include/grub/i386/relocator.h     |   21 +++++++++++++++
 include/multiboot2.h              |    1 +
 7 files changed, 145 insertions(+), 9 deletions(-)
grub-core/lib/i386/relocator.c +0 -0
index 71dd4f0..2b0c260 100644
@@ -69,6 +69,13 @@ extern grub_uint64_t grub_relocator64_rsi;
 extern grub_addr_t grub_relocator64_cr3;

grub-core/lib/i386/relocator.c extern grub_uint64_t grub_relocator64_rsi; · line 79

Quoted 10 lines
grub-core/lib/i386/relocator.c +7 -0
 extern struct grub_i386_idt grub_relocator16_idt;
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+extern grub_uint8_t grub_relocator64_efi_start;
+extern grub_uint8_t grub_relocator64_efi_end;
+#endif
+#endif
+
 

Can we move this and all to a separate file to avoid too many #ifdef ?

Quoted 59 lines
grub-core/lib/i386/relocator.c +0 -0
 #define RELOCATOR_SIZEOF(x)    (&grub_relocator##x##_end -
 &grub_relocator##x##_start)

grub_err_t

grub-core/lib/i386/relocator.c +13 -0
@@ -214,3 +221,44 @@ grub_relocator64_boot (struct grub_relocator *rel,
   /* Not reached.  */
   return GRUB_ERR_NONE;
 }
+
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+grub_err_t
+grub_relocator64_efi_boot (struct grub_relocator *rel,
+                          struct grub_relocator64_efi_state state)
+{
+  grub_err_t err;
+  void *relst;
+  grub_relocator_chunk_t ch;
+
+  err = grub_relocator_alloc_chunk_align (rel, &ch, 0,
+                                         0x40000000 - RELOCATOR_SIZEOF

(64_efi),

grub-core/lib/i386/relocator.c +2 -0
+                                         RELOCATOR_SIZEOF (64_efi), 16,
+                                         GRUB_RELOCATOR_PREFERENCE_NONE,

1);

grub-core/lib/i386/relocator.c +12 -0
+  if (err)
+    return err;
+
+  /* Do not touch %rsp! It points to EFI created stack. */
+  grub_relocator64_rax = state.rax;
+  grub_relocator64_rbx = state.rbx;
+  grub_relocator64_rcx = state.rcx;
+  grub_relocator64_rdx = state.rdx;
+  grub_relocator64_rip = state.rip;
+  grub_relocator64_rsi = state.rsi;
+
+  grub_memmove (get_virtual_current_address (ch),

&grub_relocator64_efi_start,

grub-core/lib/i386/relocator.c +3 -0
+               RELOCATOR_SIZEOF (64_efi));
+
+  err = grub_relocator_prepare_relocs (rel, get_physical_target_address

(ch),

grub-core/lib/i386/relocator.c +11 -0
+                                      &relst, NULL);
+  if (err)
+    return err;
+
+  ((void (*) (void)) relst) ();
+
+  /* Not reached.  */
+  return GRUB_ERR_NONE;
+}
+#endif
+#endif
grub-core/lib/i386/relocator64.S +0 -0
index e4648d8..7a06b16 100644

grub-core/lib/i386/relocator64.S VARIABLE(grub_relocator64_rsp) · line 77

Quoted 5 lines
grub-core/lib/i386/relocator64.S +1 -0
@@ -73,6 +73,7 @@ VARIABLE(grub_relocator64_rsp)
 
        movq    %rax, %rsp
 
+VARIABLE(grub_relocator64_efi_start)
 

Very nice code reuse. Can you add a comment that whoever modifies this file needs to take care to preserve this function-in function structure correctly.

Quoted 232 lines
grub-core/lib/i386/relocator64.S +0 -0
        /* mov imm64, %rax */
        .byte   0x48
        .byte   0xb8
grub-core/lib/i386/relocator64.S +2 -0
@@ -120,6 +121,8 @@ LOCAL(jump_addr):
 VARIABLE(grub_relocator64_rip)
        .quad   0
 
+VARIABLE(grub_relocator64_efi_end)
+
 #ifndef __x86_64__
        .p2align        4
 LOCAL(gdt):
grub-core/loader/multiboot.c +42 -0
index 73aa0aa..18038fd 100644
@@ -118,6 +118,48 @@ grub_multiboot_set_video_mode (void)
   return err;
 }
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+#define grub_relocator_efi_boot                grub_relocator64_efi_boot
+#define grub_relocator_efi_state       grub_relocator64_efi_state
+#endif
+#endif
+
+#ifdef grub_relocator_efi_boot
+static void
+efi_boot (struct grub_relocator *rel,
+         grub_uint32_t target)
+{
+  struct grub_relocator_efi_state state_efi = MULTIBOOT_EFI_INITIAL_STATE;
+
+  state_efi.MULTIBOOT_EFI_ENTRY_REGISTER = grub_multiboot_payload_eip;
+  state_efi.MULTIBOOT_EFI_MBI_REGISTER = target;
+
+  grub_relocator_efi_boot (rel, state_efi);
+}
+#else
+#define grub_efi_is_finished   1
+static void
+efi_boot (struct grub_relocator *rel __attribute__ ((unused)),
+         grub_uint32_t target __attribute__ ((unused)))
+{
+}
+#endif
+
+#if defined (__i386__) || defined (__x86_64__)
+static void
+normal_boot (struct grub_relocator *rel, struct grub_relocator32_state
 state)
+{
+  grub_relocator32_boot (rel, state, 0);
+}
+#else
+static void
+normal_boot (struct grub_relocator *rel, struct grub_relocator32_state
 state)
+{
+  grub_relocator32_boot (rel, state);
+}
+#endif
+
 static grub_err_t

grub_multiboot_boot (void)
{

grub-core/loader/multiboot.c +4 -5
@@ -131,11 +173,10 @@ grub_multiboot_boot (void)
   if (err)
     return err;
 
-#if defined (__i386__) || defined (__x86_64__)
-  grub_relocator32_boot (grub_multiboot_relocator, state, 0);
-#else
-  grub_relocator32_boot (grub_multiboot_relocator, state);
-#endif
+  if (grub_efi_is_finished)
+    normal_boot (grub_multiboot_relocator, state);
+  else
+    efi_boot (grub_multiboot_relocator, state.MULTIBOOT_MBI_REGISTER);
 
   /* Not reached.  */
   return GRUB_ERR_NONE;
grub-core/loader/multiboot_mbi2.c +2 -2
index f147d67..a3dca90 100644
@@ -107,8 +107,8 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0;
-  grub_addr_t entry = 0;
+  int entry_specified = 0, efi_entry_specified = 0;
+  grub_addr_t entry = 0, efi_entry = 0;
   grub_uint32_t console_required = 0;
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
grub-core/loader/multiboot_mbi2.c +7 -0
@@ -192,6 +192,13 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
        entry = ((struct multiboot_header_tag_entry_address *)
 tag)->entry_addr;
        break;
 
+      case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64:
+#if defined (GRUB_MACHINE_EFI) && defined (__x86_64__)
+       efi_entry_specified = 1;
+       efi_entry = ((struct multiboot_header_tag_entry_address *)
 tag)->entry_addr;
+#endif
+       break;
+

case MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS:
if (!(((struct multiboot_header_tag_console_flags *)
tag)->console_flags
& MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED))

grub-core/loader/multiboot_mbi2.c +2 -0
@@ -211,7 +218,9 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
        break;
 
       case MULTIBOOT_HEADER_TAG_EFI_BS:
+#ifdef GRUB_MACHINE_EFI
        keep_bs = 1;
+#endif
        break;
 

default:

grub-core/loader/multiboot_mbi2.c +1 -1
@@ -224,7 +233,7 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
        break;
       }
 
-  if (addr_tag && !entry_specified)
+  if (addr_tag && !entry_specified && !(keep_bs && efi_entry_specified))
     {
       grub_free (buffer);

return grub_error (GRUB_ERR_UNKNOWN_OS,

grub-core/loader/multiboot_mbi2.c +3 -1
@@ -287,7 +296,9 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
        }
     }
 
-  if (entry_specified)
+  if (keep_bs && efi_entry_specified)
+    grub_multiboot_payload_eip = efi_entry;
+  else if (entry_specified)
     grub_multiboot_payload_eip = entry;
 

if (fbtag)

include/grub/i386/multiboot.h +11 -0
index a1b9488..807a1de 100644
@@ -30,6 +30,17 @@
 #define MULTIBOOT_MBI_REGISTER ebx
 #define MULTIBOOT_ARCHITECTURE_CURRENT MULTIBOOT_ARCHITECTURE_I386
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+#define MULTIBOOT_EFI_INITIAL_STATE  { .rax =
 MULTIBOOT_BOOTLOADER_MAGIC,      \
+    .rcx = 0,
      \
+    .rdx = 0,
      \
+      }
+#define MULTIBOOT_EFI_ENTRY_REGISTER rip
+#define MULTIBOOT_EFI_MBI_REGISTER rbx
+#endif
+#endif
+

#define MULTIBOOT_ELF32_MACHINE EM_386
#define MULTIBOOT_ELF64_MACHINE EM_X86_64

include/grub/i386/relocator.h +18 -0
index 5f89a7e..2a56c3b 100644
@@ -65,6 +65,20 @@ struct grub_relocator64_state
   grub_addr_t cr3;
 };
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+struct grub_relocator64_efi_state
+{
+  grub_uint64_t rax;
+  grub_uint64_t rbx;
+  grub_uint64_t rcx;
+  grub_uint64_t rdx;
+  grub_uint64_t rip;
+  grub_uint64_t rsi;
+};
+#endif
+#endif
+
 grub_err_t grub_relocator16_boot (struct grub_relocator *rel,
                                  struct grub_relocator16_state state);
 
@@ -76,4 +90,11 @@ grub_err_t grub_relocator64_boot (struct grub_relocator
 *rel,
                                  struct grub_relocator64_state state,
                                  grub_addr_t min_addr, grub_addr_t
 max_addr);
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+grub_err_t grub_relocator64_efi_boot (struct grub_relocator *rel,
+                                     struct grub_relocator64_efi_state

state);

include/grub/i386/relocator.h +3 -0
+#endif
+#endif
+
 #endif /* ! GRUB_RELOCATOR_CPU_HEADER */
include/multiboot2.h +1 -1
index 9d48627..d96aa40 100644
@@ -69,6 +69,7 @@
 #define MULTIBOOT_HEADER_TAG_FRAMEBUFFER  5
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
+#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
--

1.7.10.4

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#8 lore raw

Re: [GRUB2 PATCH v3 2/4] multiboot2: Add tags used to pass ImageHandle to loaded image

In reply to Daniel Kiper
Quoted 46 lines · Daniel Kiper

Add tags used to pass ImageHandle to loaded image if requested. It is used by at least ExitBootServices() function.

Signed-off-by Daniel Kiper <[email protected] <javascript:;>>

---
v3 - suggestions/fixes:
- mbi EFI related stuff size calculation
should depend on target architecture
(suggested by Konrad Rzeszutek Wilk),
- use plain type instead of pointer
dereference as sizeof() argument
(suggested by Konrad Rzeszutek Wilk),
- improve commit message
(suggested by Konrad Rzeszutek Wilk).

---
 grub-core/loader/multiboot_mbi2.c |   50
++++++++++++++++++++++++++++++-------
 include/multiboot2.h              |   16 ++++++++++++
 2 files changed, 57 insertions(+), 9 deletions(-)
grub-core/loader/multiboot_mbi2.c +2 -0
index a3dca90..7591edc 100644
@@ -172,6 +172,8 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
              case MULTIBOOT_TAG_TYPE_NETWORK:
              case MULTIBOOT_TAG_TYPE_EFI_MMAP:
              case MULTIBOOT_TAG_TYPE_EFI_BS:
+             case MULTIBOOT_TAG_TYPE_EFI32_IH:
+             case MULTIBOOT_TAG_TYPE_EFI64_IH:
                break;
 

default:

grub-core/loader/multiboot_mbi2.c +3 -0
@@ -407,16 +409,22 @@ grub_multiboot_get_mbi_size (void)
                 + grub_get_multiboot_mmap_count ()
                 * sizeof (struct multiboot_mmap_entry)),
 MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer),
 MULTIBOOT_TAG_ALIGN)
+#ifdef GRUB_MACHINE_EFI
+#ifdef __i386__
     + ALIGN_UP (sizeof (struct multiboot_tag_efi32), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi32_ih),
 MULTIBOOT_TAG_ALIGN)

grub-core/loader/multiboot_mbi2.c grub_multiboot_get_mbi_size (void) · line 428

Quoted 10 lines
grub-core/loader/multiboot_mbi2.c +7 -0
+#endif
+#ifdef __x86_64__
     + ALIGN_UP (sizeof (struct multiboot_tag_efi64), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi64_ih),
 MULTIBOOT_TAG_ALIGN)
+#endif
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
+               + efi_mmap_size, MULTIBOOT_TAG_ALIGN)
+#endif
 

It doesn't need to be exact. It's just an upper bound. Feel free to simplify knowing this, removing few #ifdef.

Quoted 59 lines
grub-core/loader/multiboot_mbi2.c +29 -9
     + ALIGN_UP (sizeof (struct multiboot_tag_old_acpi)
                + sizeof (struct grub_acpi_rsdp_v10), MULTIBOOT_TAG_ALIGN)
     + acpiv2_size ()
     + net_size ()
-#ifdef GRUB_MACHINE_EFI
-    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
-               + efi_mmap_size, MULTIBOOT_TAG_ALIGN)
-#endif
     + sizeof (struct multiboot_tag_vbe) + MULTIBOOT_TAG_ALIGN - 1
     + sizeof (struct multiboot_tag_apm) + MULTIBOOT_TAG_ALIGN - 1;
 }
@@ -907,11 +915,35 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
 
   if (keep_bs)
     {
-      struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
-      tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
-      tag->size = sizeof (struct multiboot_tag);
-      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-       / sizeof (grub_properly_aligned_t);
+      {
+       struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
+       tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
+       tag->size = sizeof (struct multiboot_tag);
+       ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+         / sizeof (grub_properly_aligned_t);
+      }
+
+#ifdef __i386__
+      {
+       struct multiboot_tag_efi32_ih *tag = (struct
 multiboot_tag_efi32_ih *) ptrorig;
+       tag->type = MULTIBOOT_TAG_TYPE_EFI32_IH;
+       tag->size = sizeof (struct multiboot_tag_efi32_ih);
+       tag->pointer = (grub_addr_t) grub_efi_image_handle;
+       ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+         / sizeof (grub_properly_aligned_t);
+      }
+#endif
+
+#ifdef __x86_64__
+      {
+       struct multiboot_tag_efi64_ih *tag = (struct
 multiboot_tag_efi64_ih *) ptrorig;
+       tag->type = MULTIBOOT_TAG_TYPE_EFI64_IH;
+       tag->size = sizeof (struct multiboot_tag_efi64_ih);
+       tag->pointer = (grub_addr_t) grub_efi_image_handle;
+       ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+         / sizeof (grub_properly_aligned_t);
+      }
+#endif
     }

#endif

include/multiboot2.h +0 -0
index d96aa40..36a174f 100644

include/multiboot2.h line 65

Quoted 6 lines
include/multiboot2.h +2 -0
@@ -60,6 +60,8 @@
 #define MULTIBOOT_TAG_TYPE_NETWORK           16
 #define MULTIBOOT_TAG_TYPE_EFI_MMAP          17
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
+#define MULTIBOOT_TAG_TYPE_EFI32_IH          19
+#define MULTIBOOT_TAG_TYPE_EFI64_IH          20
 

Is there a followup to define those in texi doc as well?

Quoted 28 lines
include/multiboot2.h +14 -1
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -371,6 +373,20 @@ struct multiboot_tag_efi_mmap
   multiboot_uint8_t efi_mmap[0];
 };
 
+struct multiboot_tag_efi32_ih
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t pointer;
+};
+
+struct multiboot_tag_efi64_ih
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint64_t pointer;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */
--

1.7.10.4

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#12 lore raw

Re: [GRUB2 PATCH v3 3/4] multiboot2: Do not pass memory maps to image if EFI boot services are enabled

In reply to Daniel Kiper
Quoted 8 lines · Daniel Kiper

Do not pass memory maps to image if it asked for EFI boot services. Main reason for not providing maps is because they will likely be invalid. We do a few allocations after filling them, e.g. for relocator needs. Usually we do not care as we would already finish boot services. If we keep boot services then it is easier to not provide maps. However, if image needs memory maps and they are not provided by bootloader then it should get them itself just before ExitBootServices() call.

Patch gets too cluttered. Can you provide 2 variants: normal (for commit) and with ignoring whitespaces (for review)

Quoted 133 lines
Signed-off-by Daniel Kiper <[email protected] <javascript:;>>
Reviewed-by Konrad Rzeszutek Wilk <[email protected] <javascript:;>>

---
v3 - suggestions/fixes:
- improve commit message
(suggested by Konrad Rzeszutek Wilk and Vladimir 'phcoder'
Serbinenko).

---
 grub-core/loader/multiboot_mbi2.c |   71
++++++++++++++++++-------------------
 1 file changed, 35 insertions(+), 36 deletions(-)
grub-core/loader/multiboot_mbi2.c +8 -7
index 7591edc..ce68f48 100644
@@ -390,7 +390,7 @@ static grub_size_t
 grub_multiboot_get_mbi_size (void)
 {
 #ifdef GRUB_MACHINE_EFI
-  if (!efi_mmap_size)
+  if (!keep_bs && !efi_mmap_size)
     find_efi_mmap_size ();
 #endif
   return 2 * sizeof (grub_uint32_t) + sizeof (struct multiboot_tag)
@@ -759,12 +759,13 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
       }
   }
 
-  {
-    struct multiboot_tag_mmap *tag = (struct multiboot_tag_mmap *)
 ptrorig;
-    grub_fill_multiboot_mmap (tag);
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-      / sizeof (grub_properly_aligned_t);
-  }
+  if (!keep_bs)
+    {
+      struct multiboot_tag_mmap *tag = (struct multiboot_tag_mmap *)
 ptrorig;
+      grub_fill_multiboot_mmap (tag);
+      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+       / sizeof (grub_properly_aligned_t);
+    }
 
   {
     struct multiboot_tag_elf_sections *tag
grub-core/loader/multiboot_mbi2.c +27 -30
@@ -780,18 +781,19 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
       / sizeof (grub_properly_aligned_t);
   }
 
-  {
-    struct multiboot_tag_basic_meminfo *tag
-      = (struct multiboot_tag_basic_meminfo *) ptrorig;
-    tag->type = MULTIBOOT_TAG_TYPE_BASIC_MEMINFO;
-    tag->size = sizeof (struct multiboot_tag_basic_meminfo);
+  if (!keep_bs)
+    {
+      struct multiboot_tag_basic_meminfo *tag
+       = (struct multiboot_tag_basic_meminfo *) ptrorig;
+      tag->type = MULTIBOOT_TAG_TYPE_BASIC_MEMINFO;
+      tag->size = sizeof (struct multiboot_tag_basic_meminfo);
 
-    /* Convert from bytes to kilobytes.  */
-    tag->mem_lower = grub_mmap_get_lower () / 1024;
-    tag->mem_upper = grub_mmap_get_upper () / 1024;
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-       / sizeof (grub_properly_aligned_t);
-  }
+      /* Convert from bytes to kilobytes.  */
+      tag->mem_lower = grub_mmap_get_lower () / 1024;
+      tag->mem_upper = grub_mmap_get_upper () / 1024;
+      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+       / sizeof (grub_properly_aligned_t);
+    }
 
   {
     struct grub_net_network_level_interface *net;
@@ -890,27 +892,24 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
     grub_efi_uintn_t efi_desc_size;
     grub_efi_uint32_t efi_desc_version;
 
-    tag->type = MULTIBOOT_TAG_TYPE_EFI_MMAP;
-    tag->size = sizeof (*tag) + efi_mmap_size;
-
     if (!keep_bs)
-      err = grub_efi_finish_boot_services (&efi_mmap_size, tag->efi_mmap,
 NULL,
-                                          &efi_desc_size,
 &efi_desc_version);
-    else
       {
-       if (grub_efi_get_memory_map (&efi_mmap_size, (void *)
 tag->efi_mmap,
-                                    NULL,
-                                    &efi_desc_size, &efi_desc_version) <=
 0)
-         err = grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
+       tag->type = MULTIBOOT_TAG_TYPE_EFI_MMAP;
+       tag->size = sizeof (*tag) + efi_mmap_size;
+
+       err = grub_efi_finish_boot_services (&efi_mmap_size,
 tag->efi_mmap, NULL,
+                                            &efi_desc_size,
 &efi_desc_version);
+
+       if (err)
+         return err;
+
+       tag->descr_size = efi_desc_size;
+       tag->descr_vers = efi_desc_version;
+       tag->size = sizeof (*tag) + efi_mmap_size;
+
+       ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+         / sizeof (grub_properly_aligned_t);
       }
-    if (err)
-      return err;
-    tag->descr_size = efi_desc_size;
-    tag->descr_vers = efi_desc_version;
-    tag->size = sizeof (*tag) + efi_mmap_size;
-
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-      / sizeof (grub_properly_aligned_t);
   }
 
   if (keep_bs)
--

1.7.10.4

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#17 lore raw

Re: [GRUB2 PATCH v3 4/4] multiboot2: Add support for relocatable images

In reply to Daniel Kiper
Quoted 43 lines · Daniel Kiper

Currently multiboot2 protocol loads image exactly at address specified in ELF or multiboot2 header. This solution works quite well on legacy BIOS platforms. It is possible because memory regions are placed at predictable addresses (though I was not able to find any spec which says that it is strong requirement, so, it looks that it is just a goodwill of hardware designers). However, EFI platforms are more volatile. Even if required memory regions live at specific addresses then they are sometimes simply not free (e.g. used by boot/runtime services on Dell PowerEdge R820 and OVMF). This means that you are not able to simply set up final image destination on build time. You have to provide method to relocate image contents to real load address which is usually different than load address specified in ELF and multiboot2 headers.

This patch provides all needed machinery to do self relocation in image code.
First of all GRUB2 reads min_addr (min. load addr), max_addr (max. load addr),
align (required image alignment), preference (it says which memory regions are
preferred by image, e.g. none, low, high) from multiboot_header_tag_relocatable
header tag contained in binary. Later loader tries to fulfill request (not only
that one) and if it succeeds then it informs image about real load address via
multiboot_tag_base_addr tag. At this stage GRUB2 role is finished. Starting from now executable must cope with relocations itself using whole static and dynamic knowledge provided by boot loader.

This patch does not provide functionality which could do relocations using ELF relocation data. However, I was asked by Konrad Rzeszutek Wilk and Vladimir
'phcoder' Serbinenko to investigate that thing. It looks that relevant machinery
could be added to existing code (including this patch) without huge effort. Additionally, ELF relocation could live in parallel with self relocation provided
by this patch. However, during research I realized that first of all we should
establish the details how ELF relocatable image should look like and how it should
be build. At least to build proper test/example files.

Quoted 9 lines

As I saw multiboot2 protocol is able to consume ET_EXEC and ET_DYN ELF files.
Potentially we can use ET_DYN file type. It can be build with gcc/ld -pie option.
However, it contains a lot of unneeded stuff (e.g. INTERP, DYNAMIC, GNU_EH_FRAME
program headers) and it could be quite difficult to drop them (Hmmm... Is it
possible to build it properly with custom ld script?).

How big are they? Are they a real problem?

Quoted 10 lines

So, I have checked ET_EXEC
file type. Sadly in this case linker by default resolves all local symbol relocations
and removes relocation related sections. Fortunately it is possible to leave them
as is with simple -q/--emit-relocs ld option. However, output file is quite fragile
and any operation on it should be done with great care (e.g. strip should be called
with --strip-unneeded option). So, this solution is not perfect too. It

Quoted 9 lines

means that
maybe we should look for better solution. However, I think that we should not use
any custom tools and focus on functionalities provided by compiler and binutils.
In this context ld scripts looks quite promising but maybe you have better solutions.
So, what do you think about that?

Another possibility is to use intermediary .o files like we do for modules
and like Linux does for modules AFAIR.

Quoted 284 lines

This patch was tested with Xen image which uses that functionality. However, this Xen
feature is still under development and new patchset will be released in about 3-4 weeks.

Signed-off-by Daniel Kiper <[email protected] <javascript:;>>

---
v3 - suggestions/fixes:
- reduce number of casts
(suggested by Konrad Rzeszutek Wilk),
- remove unneeded space at the end of line
(suggested by Konrad Rzeszutek Wilk),
- improve commit message
(suggested by Konrad Rzeszutek Wilk).

---
 grub-core/loader/i386/multiboot_mbi.c |    6 ++-
 grub-core/loader/multiboot.c          |   12 ++++--
 grub-core/loader/multiboot_elfxx.c    |   28 ++++++++++----
 grub-core/loader/multiboot_mbi2.c     |   65
++++++++++++++++++++++++++++++---
 include/grub/multiboot.h              |    4 +-
 include/multiboot2.h                  |   24 ++++++++++++
 6 files changed, 120 insertions(+), 19 deletions(-)
grub-core/loader/i386/multiboot_mbi.c +2 -1
index f60b702..4fc83ed 100644
@@ -72,7 +72,8 @@ load_kernel (grub_file_t file, const char *filename,
   grub_err_t err;
   if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE)
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+                                    GRUB_RELOCATOR_PREFERENCE_NONE, NULL,
 0);
       if (err == GRUB_ERR_NONE) {
        return GRUB_ERR_NONE;
       }
grub-core/loader/i386/multiboot_mbi.c +2 -1
@@ -121,7 +122,8 @@ load_kernel (grub_file_t file, const char *filename,
       return GRUB_ERR_NONE;
     }
 
-  return grub_multiboot_load_elf (file, filename, buffer);
+  return grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+                                 GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
 }
 
 static struct multiboot_header *
grub-core/loader/multiboot.c +8 -3
index 18038fd..c0f51b6 100644
@@ -208,12 +208,18 @@ static grub_uint64_t highest_load;
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-                        void *buffer)
+                        void *buffer, int relocatable, grub_uint32_t
 min_addr,
+                        grub_uint32_t max_addr, grub_size_t align,
 grub_uint32_t preference,
+                        grub_uint32_t *base_addr, int
 avoid_efi_boot_services)
 {
   if (grub_multiboot_is_elf32 (buffer))
-    return grub_multiboot_load_elf32 (file, filename, buffer);
+    return grub_multiboot_load_elf32 (file, filename, buffer, relocatable,
+                                     min_addr, max_addr, align,
 preference,
+                                     base_addr, avoid_efi_boot_services);
   else if (grub_multiboot_is_elf64 (buffer))
-    return grub_multiboot_load_elf64 (file, filename, buffer);
+    return grub_multiboot_load_elf64 (file, filename, buffer, relocatable,
+                                     min_addr, max_addr, align,

preference,
+ base_addr, avoid_efi_boot_services);

return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF
magic"));
}

grub-core/loader/multiboot_elfxx.c +3 -1
index e3a39b6..0c01569 100644
@@ -51,7 +51,10 @@ CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
 }
 
 static grub_err_t
-CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char
 *filename, void *buffer)
+CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char
 *filename,
+                                    void *buffer, int relocatable,
 grub_uint32_t min_addr,
+                                    grub_uint32_t max_addr, grub_size_t

align, grub_uint32_t preference,
+ grub_uint32_t *base_addr, int
avoid_efi_boot_services)

 {
   Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
   char *phdr_base;
grub-core/loader/multiboot_elfxx.c +0 -0
@@ -89,19 +92,30 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file,

const char *filename, voi

          if (phdr(i)->p_paddr + phdr(i)->p_memsz > highest_load)
            highest_load = phdr(i)->p_paddr + phdr(i)->p_memsz;

- grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx,
memsz=0x%lx, vaddr=0x%lx\n",
- i, (long) phdr(i)->p_paddr, (long)
phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
+ grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx,
memsz=0x%lx, vaddr=0x%lx,"
+ "align=0x%lx, relocatable=%d,
avoid_efi_boot_services=%d\n", i,
+ (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz,
(long) phdr(i)->p_vaddr,
+ (long) align, relocatable,
avoid_efi_boot_services);

          {
            grub_relocator_chunk_t ch;

- err = grub_relocator_alloc_chunk_addr
(grub_multiboot_relocator,

grub-core/loader/multiboot_elfxx.c +3 -2
-                                                  &ch, phdr(i)->p_paddr,
-                                                  phdr(i)->p_memsz);
+
+           if (relocatable)
+             err = grub_relocator_alloc_chunk_align

(grub_multiboot_relocator, &ch,
+ min_addr, max_addr -
phdr(i)->p_memsz,
+ phdr(i)->p_memsz,
align ? align : 1,
+ preference,
avoid_efi_boot_services);

grub-core/loader/multiboot_elfxx.c +2 -0
+           else
+             err = grub_relocator_alloc_chunk_addr

(grub_multiboot_relocator,

grub-core/loader/multiboot_elfxx.c +2 -0
+                                                    &ch, phdr(i)->p_paddr,
+                                                    phdr(i)->p_memsz);
            if (err)
              {
                grub_dprintf ("multiboot_loader", "Error loading phdr
%d\n", i);
                return err;
              }
grub-core/loader/multiboot_elfxx.c +2 -0
+           if (base_addr)
+             *base_addr = get_physical_target_address (ch);
            source = get_virtual_current_address (ch);
          }
@@ -208,7 +222,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file,

const char *filename, voi
+ 1, sh->sh_size, sh->sh_addralign,

GRUB_RELOCATOR_PREFERENCE_NONE,

grub-core/loader/multiboot_elfxx.c +1 -1
-                                                   0);
+
 avoid_efi_boot_services);
            if (err)
              {
                grub_dprintf ("multiboot_loader", "Error loading shdr

%d\n", i);

grub-core/loader/multiboot_mbi2.c +6 -2
index ce68f48..03725a1 100644
@@ -68,6 +68,7 @@ static grub_size_t elf_sec_num, elf_sec_entsize;
 static unsigned elf_sec_shstrndx;
 static void *elf_sections;
 static int keep_bs = 0;
+static grub_uint32_t base_addr = 0;
 
 void
 grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize,
@@ -107,11 +108,14 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0, efi_entry_specified = 0;
+  struct multiboot_header_tag_relocatable *rel_tag;
+  int entry_specified = 0, efi_entry_specified = 0, relocatable = 0;
   grub_addr_t entry = 0, efi_entry = 0;
-  grub_uint32_t console_required = 0;
+  grub_uint32_t console_required = 0, min_addr = 0;
+  grub_uint32_t max_addr = 0, preference = GRUB_RELOCATOR_PREFERENCE_NONE;
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
+  grub_size_t align = 0;
 
   buffer = grub_malloc (MULTIBOOT_SEARCH);

if (!buffer)

grub-core/loader/multiboot_mbi2.c +1 -0
@@ -174,6 +178,7 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
              case MULTIBOOT_TAG_TYPE_EFI_BS:
              case MULTIBOOT_TAG_TYPE_EFI32_IH:
              case MULTIBOOT_TAG_TYPE_EFI64_IH:
+             case MULTIBOOT_TAG_TYPE_BASE_ADDR:
                break;
 

default:

grub-core/loader/multiboot_mbi2.c +21 -0
@@ -215,6 +220,27 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
        accepted_consoles |= GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER;
        break;
 
+      case MULTIBOOT_HEADER_TAG_RELOCATABLE:
+       relocatable = 1;
+       rel_tag = (struct multiboot_header_tag_relocatable *) tag;
+       min_addr = rel_tag->min_addr;
+       max_addr = rel_tag->max_addr;
+       align = rel_tag->align;
+       switch (rel_tag->preference)
+         {
+         case MULTIBOOT_LOAD_PREFERENCE_LOW:
+           preference = GRUB_RELOCATOR_PREFERENCE_LOW;
+           break;
+
+         case MULTIBOOT_LOAD_PREFERENCE_HIGH:
+           preference = GRUB_RELOCATOR_PREFERENCE_HIGH;
+           break;
+
+         default:
+           preference = GRUB_RELOCATOR_PREFERENCE_NONE;
+         }
+       break;
+
        /* GRUB always page-aligns modules.  */
       case MULTIBOOT_HEADER_TAG_MODULE_ALIGN:
        break;
grub-core/loader/multiboot_mbi2.c +13 -4
@@ -260,15 +286,22 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
       else
        code_size = load_size;
 
-      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
-                                            &ch, load_addr,
-                                            code_size);
+      if (relocatable)
+       err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator,
 &ch,
+                                               min_addr, max_addr -
 code_size,
+                                               code_size, align ? align :
 1,
+                                               preference, keep_bs);
+      else
+       err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+                                              &ch, load_addr,
+                                              code_size);
       if (err)
        {
          grub_dprintf ("multiboot_loader", "Error loading aout kludge\n");
          grub_free (buffer);
          return err;
        }
+      base_addr = get_physical_target_address (ch);
       source = get_virtual_current_address (ch);
 
       if ((grub_file_seek (file, offset)) == (grub_off_t) -1)
@@ -290,7 +323,9 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
     }
   else
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer,
+                                    relocatable, min_addr, max_addr,
+                                    align, preference, &base_addr,
 keep_bs);
       if (err)
        {
          grub_free (buffer);
grub-core/loader/multiboot_mbi2.c +0 -0
@@ -303,6 +338,14 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
   else if (entry_specified)
     grub_multiboot_payload_eip = entry;

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char · line 350

Quoted 10 lines
grub-core/loader/multiboot_mbi2.c +8 -0
 
+  if (relocatable)
+    {
+      if (base_addr > min_addr)
+       grub_multiboot_payload_eip += base_addr - min_addr;
+      else
+       grub_multiboot_payload_eip -= min_addr - base_addr;
+    }
+
 

Why is it relative to min_addr? Sounds like it should be just an offset from base addr. What do ET_DYN files use?

Quoted 117 lines
grub-core/loader/multiboot_mbi2.c +0 -0
   if (fbtag)

err = grub_multiboot_set_console (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER,
accepted_consoles,

grub-core/loader/multiboot_mbi2.c +1 -0
@@ -409,6 +452,7 @@ grub_multiboot_get_mbi_size (void)
                 + grub_get_multiboot_mmap_count ()
                 * sizeof (struct multiboot_mmap_entry)),
 MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer),
 MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_base_addr),
 MULTIBOOT_TAG_ALIGN)

#ifdef GRUB_MACHINE_EFI
#ifdef __i386__
+ ALIGN_UP (sizeof (struct multiboot_tag_efi32), MULTIBOOT_TAG_ALIGN)

grub-core/loader/multiboot_mbi2.c +9 -0
@@ -698,6 +742,15 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
   ptrorig += (2 * sizeof (grub_uint32_t)) / sizeof
 (grub_properly_aligned_t);
 
   {
+    struct multiboot_tag_base_addr *tag = (struct multiboot_tag_base_addr
 *) ptrorig;
+    tag->type = MULTIBOOT_TAG_TYPE_BASE_ADDR;
+    tag->size = sizeof (struct multiboot_tag_base_addr);
+    tag->base_addr = base_addr;
+    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+       / sizeof (grub_properly_aligned_t);
+  }
+
+  {
     struct multiboot_tag_string *tag = (struct multiboot_tag_string *)
ptrorig;
     tag->type = MULTIBOOT_TAG_TYPE_CMDLINE;
     tag->size = sizeof (struct multiboot_tag_string) + cmdline_size;
include/grub/multiboot.h +3 -1
index e13c084..ec322b0 100644
@@ -94,7 +94,9 @@ grub_multiboot_load (grub_file_t file, const char
 *filename);
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-                        void *buffer);
+                        void *buffer, int relocatable, grub_uint32_t
 min_addr,
+                        grub_uint32_t max_addr, grub_size_t align,
 grub_uint32_t preference,
+                        grub_uint32_t *base_addr, int

avoid_efi_boot_services);
extern grub_size_t grub_multiboot_pure_size;
extern grub_size_t grub_multiboot_alloc_mbi;
extern grub_uint32_t grub_multiboot_payload_eip;

include/multiboot2.h +24 -1
index 36a174f..c09bdbc 100644
@@ -62,6 +62,7 @@
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
 #define MULTIBOOT_TAG_TYPE_EFI32_IH          19
 #define MULTIBOOT_TAG_TYPE_EFI64_IH          20
+#define MULTIBOOT_TAG_TYPE_BASE_ADDR         21
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -72,11 +73,16 @@
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
 #define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
+#define MULTIBOOT_HEADER_TAG_RELOCATABLE  10
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
 #define MULTIBOOT_HEADER_TAG_OPTIONAL 1
 
+#define MULTIBOOT_LOAD_PREFERENCE_NONE 0
+#define MULTIBOOT_LOAD_PREFERENCE_LOW 1
+#define MULTIBOOT_LOAD_PREFERENCE_HIGH 2
+
 #define MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED 1
 #define MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED 2
 
@@ -161,6 +167,17 @@ struct multiboot_header_tag_module_align
   multiboot_uint32_t size;
 };
 
+struct multiboot_header_tag_relocatable
+{
+  multiboot_uint16_t type;
+  multiboot_uint16_t flags;
+  multiboot_uint32_t size;
+  multiboot_uint32_t min_addr;
+  multiboot_uint32_t max_addr;
+  multiboot_uint32_t align;
+  multiboot_uint32_t preference;
+};
+
 struct multiboot_color
 {
   multiboot_uint8_t red;
@@ -387,6 +404,13 @@ struct multiboot_tag_efi64_ih
   multiboot_uint64_t pointer;
 };
 
+struct multiboot_tag_base_addr
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t base_addr;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */
--

1.7.10.4

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#16 lore raw

Re: [GRUB2 PATCH v3 4/4] multiboot2: Add support for relocatable images

In reply to Juergen Gross
Quoted 115 lines · Juergen Gross
Quoted 3 lines · Daniel Kiper

Currently multiboot2 protocol loads image exactly at address specified in ELF or multiboot2 header. This solution works quite well on legacy BIOS platforms. It is possible because memory regions are placed at

predictable

Quoted 8 lines

addresses (though I was not able to find any spec which says that it is strong requirement, so, it looks that it is just a goodwill of hardware designers). However, EFI platforms are more volatile. Even if required memory regions live at specific addresses then they are sometimes simply not free (e.g. used by boot/runtime services on Dell PowerEdge R820 and OVMF). This means that you are not able to simply set up final image destination on build time. You have to provide method to relocate image contents to real load address which is usually different than load

address

Quoted 3 lines

specified in ELF and multiboot2 headers.

This patch provides all needed machinery to do self relocation in image

code.

Quoted 1 line

First of all GRUB2 reads min_addr (min. load addr), max_addr (max. load

addr),

Quoted 1 line

align (required image alignment), preference (it says which memory

regions are

Quoted 1 line

preferred by image, e.g. none, low, high) from

multiboot_header_tag_relocatable

Quoted 1 line

header tag contained in binary. Later loader tries to fulfill request

(not only

Quoted 1 line

that one) and if it succeeds then it informs image about real load

address via

Quoted 1 line

multiboot_tag_base_addr tag. At this stage GRUB2 role is finished.

Starting

Quoted 4 lines

from now executable must cope with relocations itself using whole static and dynamic knowledge provided by boot loader.

This patch does not provide functionality which could do relocations

using

Quoted 1 line

ELF relocation data. However, I was asked by Konrad Rzeszutek Wilk and

Vladimir

Quoted 1 line

'phcoder' Serbinenko to investigate that thing. It looks that relevant

machinery

Quoted 1 line

could be added to existing code (including this patch) without huge

effort.

Quoted 1 line

Additionally, ELF relocation could live in parallel with self relocation

provided

Quoted 1 line

by this patch. However, during research I realized that first of all we

should

Quoted 1 line

establish the details how ELF relocatable image should look like and how

it should

Quoted 3 lines

be build. At least to build proper test/example files.

As I saw multiboot2 protocol is able to consume ET_EXEC and ET_DYN ELF

files.

Quoted 1 line

Potentially we can use ET_DYN file type. It can be build with gcc/ld

-pie option.

Quoted 1 line

However, it contains a lot of unneeded stuff (e.g. INTERP, DYNAMIC,

GNU_EH_FRAME

Quoted 1 line

program headers) and it could be quite difficult to drop them (Hmmm...

Is it

Quoted 1 line

possible to build it properly with custom ld script?). So, I have

checked ET_EXEC

Quoted 1 line

file type. Sadly in this case linker by default resolves all local

symbol relocations

Quoted 1 line

and removes relocation related sections. Fortunately it is possible to

leave them

Quoted 1 line

as is with simple -q/--emit-relocs ld option. However, output file is

quite fragile

Quoted 1 line

and any operation on it should be done with great care (e.g. strip

should be called

Quoted 1 line

with --strip-unneeded option). So, this solution is not perfect too. It

means that

Quoted 1 line

maybe we should look for better solution. However, I think that we

should not use

Quoted 1 line

any custom tools and focus on functionalities provided by compiler and

binutils.

Quoted 1 line

In this context ld scripts looks quite promising but maybe you have

better solutions.

Quoted 3 lines

So, what do you think about that?

This patch was tested with Xen image which uses that functionality.

However, this Xen

Quoted 1 line

feature is still under development and new patchset will be released in

about 3-4 weeks.

Quoted 10 lines
Signed-off-by Daniel Kiper <[email protected] <javascript:;>>

---
v3 - suggestions/fixes:
- reduce number of casts
(suggested by Konrad Rzeszutek Wilk),
- remove unneeded space at the end of line
(suggested by Konrad Rzeszutek Wilk),
- improve commit message
(suggested by Konrad Rzeszutek Wilk).

Quoted 5 lines
---
 grub-core/loader/i386/multiboot_mbi.c |    6 ++-
 grub-core/loader/multiboot.c          |   12 ++++--
 grub-core/loader/multiboot_elfxx.c    |   28 ++++++++++----
 grub-core/loader/multiboot_mbi2.c     |   65

++++++++++++++++++++++++++++++---

Review

Quoted 5 lines
 include/grub/multiboot.h              |    4 +-
 include/multiboot2.h                  |   24 ++++++++++++
 6 files changed, 120 insertions(+), 19 deletions(-)
(quoted diff) +0 -0

b/grub-core/loader/i386/multiboot_mbi.c

grub-core/loader/i386/multiboot_mbi.c load_kernel (grub_file_t file, const char *filename, · line 76

Quoted 10 lines

index f60b702..4fc83ed 100644

grub-core/loader/i386/multiboot_mbi.c +2 -1
@@ -72,7 +72,8 @@ load_kernel (grub_file_t file, const char *filename,
   grub_err_t err;
   if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE)
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+                                  GRUB_RELOCATOR_PREFERENCE_NONE, NULL,

0);

Quoted 6 lines

Uuh, really? You are adding 7 parameters for the relocatable case. Wouldn't it make more sense to have some kind of structure containing the data you need for relocatable images and pass the pointer to that structure or NULL in the case of non-relocatable images (or have a default structure for the non-relocatable case)?

I second this. Feel free to encapsulate more image-related stuff into same structure

Quoted 380 lines

Juergen

Quoted 18 lines
grub-core/loader/i386/multiboot_mbi.c +2 -1
       if (err == GRUB_ERR_NONE) {
      return GRUB_ERR_NONE;
       }
@@ -121,7 +122,8 @@ load_kernel (grub_file_t file, const char *filename,
       return GRUB_ERR_NONE;
     }
 
-  return grub_multiboot_load_elf (file, filename, buffer);
+  return grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+                               GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
 }
 
 static struct multiboot_header *
grub-core/loader/multiboot.c +0 -0
index 18038fd..c0f51b6 100644

grub-core/loader/multiboot.c static grub_uint64_t highest_load; · line 211

Quoted 5 lines
grub-core/loader/multiboot.c +1 -1
@@ -208,12 +208,18 @@ static grub_uint64_t highest_load;
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-                      void *buffer)
+                      void *buffer, int relocatable, grub_uint32_t

min_addr,

grub-core/loader/multiboot.c static grub_uint64_t highest_load; · line 212

Quoted 1 line
grub-core/loader/multiboot.c +1 -0
+                      grub_uint32_t max_addr, grub_size_t align,

grub_uint32_t preference,

grub-core/loader/multiboot.c static grub_uint64_t highest_load; · line 213

Quoted 1 line
grub-core/loader/multiboot.c +1 -0
+                      grub_uint32_t *base_addr, int

avoid_efi_boot_services)

grub-core/loader/multiboot.c static grub_uint64_t highest_load;

Quoted 4 lines
grub-core/loader/multiboot.c +1 -1
 {
   if (grub_multiboot_is_elf32 (buffer))
-    return grub_multiboot_load_elf32 (file, filename, buffer);
+    return grub_multiboot_load_elf32 (file, filename, buffer,

relocatable,

grub-core/loader/multiboot.c static grub_uint64_t highest_load; · line 217

Quoted 1 line
grub-core/loader/multiboot.c +1 -0
+                                   min_addr, max_addr, align,

preference,

grub-core/loader/multiboot.c static grub_uint64_t highest_load;

Quoted 4 lines
grub-core/loader/multiboot.c +2 -1
+                                   base_addr, avoid_efi_boot_services);
   else if (grub_multiboot_is_elf64 (buffer))
-    return grub_multiboot_load_elf64 (file, filename, buffer);
+    return grub_multiboot_load_elf64 (file, filename, buffer,

relocatable,

grub-core/loader/multiboot.c static grub_uint64_t highest_load; · line 221

Quoted 1 line
grub-core/loader/multiboot.c +1 -0
+                                   min_addr, max_addr, align,

preference,

grub-core/loader/multiboot.c static grub_uint64_t highest_load;

Quoted 3 lines
grub-core/loader/multiboot.c +1 -0
+                                   base_addr, avoid_efi_boot_services);
 
   return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent

ELF magic"));

Review

Quoted 2 lines
grub-core/loader/multiboot.c +0 -0
 }
(quoted diff) +0 -0

b/grub-core/loader/multiboot_elfxx.c

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_is_elf, XX) (void *buffer) · line 54

Quoted 8 lines

index e3a39b6..0c01569 100644

grub-core/loader/multiboot_elfxx.c +0 -1
@@ -51,7 +51,10 @@ CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
 }
 
 static grub_err_t
-CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char

*filename, void *buffer)

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_is_elf, XX) (void *buffer) · line 54

Quoted 1 line
grub-core/loader/multiboot_elfxx.c +1 -0
+CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char

*filename,

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_is_elf, XX) (void *buffer) · line 55

Quoted 1 line
grub-core/loader/multiboot_elfxx.c +1 -0
+                                  void *buffer, int relocatable,

grub_uint32_t min_addr,

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_is_elf, XX) (void *buffer) · line 56

Quoted 1 line
grub-core/loader/multiboot_elfxx.c +1 -0
+                                  grub_uint32_t max_addr, grub_size_t

align, grub_uint32_t preference,

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_is_elf, XX) (void *buffer) · line 57

Quoted 1 line
grub-core/loader/multiboot_elfxx.c +1 -0
+                                  grub_uint32_t *base_addr, int

avoid_efi_boot_services)

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t

Quoted 4 lines
grub-core/loader/multiboot_elfxx.c +0 -0
 {
   Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
   char *phdr_base;
@@ -89,19 +92,30 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t

file, const char *filename, voi

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi

Quoted 4 lines
grub-core/loader/multiboot_elfxx.c +0 -1
        if (phdr(i)->p_paddr + phdr(i)->p_memsz > highest_load)
          highest_load = phdr(i)->p_paddr + phdr(i)->p_memsz;
 
-       grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx,

memsz=0x%lx, vaddr=0x%lx\n",

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t · line 93

Quoted 1 line
grub-core/loader/multiboot_elfxx.c +0 -1
-                     i, (long) phdr(i)->p_paddr, (long)

phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t · line 95

Quoted 1 line
grub-core/loader/multiboot_elfxx.c +1 -0
+       grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx,

memsz=0x%lx, vaddr=0x%lx,"

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t · line 96

Quoted 1 line
grub-core/loader/multiboot_elfxx.c +1 -0
+                     "align=0x%lx, relocatable=%d,

avoid_efi_boot_services=%d\n", i,

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t · line 97

Quoted 1 line
grub-core/loader/multiboot_elfxx.c +1 -0
+                     (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz,

(long) phdr(i)->p_vaddr,

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t · line 98

Quoted 1 line
grub-core/loader/multiboot_elfxx.c +1 -0
+                     (long) align, relocatable,

avoid_efi_boot_services);

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi

Quoted 4 lines
grub-core/loader/multiboot_elfxx.c +0 -1
        {
          grub_relocator_chunk_t ch;
-         err = grub_relocator_alloc_chunk_addr

(grub_multiboot_relocator,

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi

Quoted 5 lines
grub-core/loader/multiboot_elfxx.c +3 -2
-                                                &ch, phdr(i)->p_paddr,
-                                                phdr(i)->p_memsz);
+
+         if (relocatable)
+           err = grub_relocator_alloc_chunk_align

(grub_multiboot_relocator, &ch,

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t · line 104

Quoted 1 line
grub-core/loader/multiboot_elfxx.c +1 -0
+                                                   min_addr, max_addr -

phdr(i)->p_memsz,

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t · line 105

Quoted 1 line
grub-core/loader/multiboot_elfxx.c +1 -0
+                                                   phdr(i)->p_memsz,

align ? align : 1,

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t · line 106

Quoted 1 line
grub-core/loader/multiboot_elfxx.c +1 -0
+                                                   preference,

avoid_efi_boot_services);

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t · line 108

Quoted 2 lines
grub-core/loader/multiboot_elfxx.c +2 -0
+         else
+           err = grub_relocator_alloc_chunk_addr

(grub_multiboot_relocator,

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi

Quoted 5 lines
grub-core/loader/multiboot_elfxx.c +2 -0
+                                                  &ch, phdr(i)->p_paddr,
+                                                  phdr(i)->p_memsz);
          if (err)
            {
              grub_dprintf ("multiboot_loader", "Error loading phdr

%d\n", i);

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t

Quoted 8 lines
grub-core/loader/multiboot_elfxx.c +2 -0
              return err;
            }
+         if (base_addr)
+           *base_addr = get_physical_target_address (ch);
          source = get_virtual_current_address (ch);
        }
 
@@ -208,7 +222,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t

file, const char *filename, voi

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi

Quoted 3 lines
grub-core/loader/multiboot_elfxx.c +0 -0
                                                  + 1, sh->sh_size,
                                                  sh->sh_addralign,
 

GRUB_RELOCATOR_PREFERENCE_NONE,

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t · line 225

Quoted 2 lines
grub-core/loader/multiboot_elfxx.c +1 -1
-                                                 0);
+

avoid_efi_boot_services);

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi

Quoted 3 lines
grub-core/loader/multiboot_elfxx.c +0 -0
          if (err)
            {
              grub_dprintf ("multiboot_loader", "Error loading shdr

%d\n", i);

Review

Quoted 1 line
(quoted diff) +0 -0

b/grub-core/loader/multiboot_mbi2.c

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char

Quoted 12 lines

index ce68f48..03725a1 100644

grub-core/loader/multiboot_mbi2.c +1 -0
@@ -68,6 +68,7 @@ static grub_size_t elf_sec_num, elf_sec_entsize;
 static unsigned elf_sec_shstrndx;
 static void *elf_sections;
 static int keep_bs = 0;
+static grub_uint32_t base_addr = 0;
 
 void
 grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize,
@@ -107,11 +108,14 @@ grub_multiboot_load (grub_file_t file, const char

*filename)

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char *filename)

Quoted 10 lines
grub-core/loader/multiboot_mbi2.c +4 -2
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0, efi_entry_specified = 0;
+  struct multiboot_header_tag_relocatable *rel_tag;
+  int entry_specified = 0, efi_entry_specified = 0, relocatable = 0;
   grub_addr_t entry = 0, efi_entry = 0;
-  grub_uint32_t console_required = 0;
+  grub_uint32_t console_required = 0, min_addr = 0;
+  grub_uint32_t max_addr = 0, preference =

GRUB_RELOCATOR_PREFERENCE_NONE;

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char

Quoted 7 lines
grub-core/loader/multiboot_mbi2.c +1 -0
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
+  grub_size_t align = 0;
 
   buffer = grub_malloc (MULTIBOOT_SEARCH);
   if (!buffer)
@@ -174,6 +178,7 @@ grub_multiboot_load (grub_file_t file, const char

*filename)

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char

Quoted 8 lines
grub-core/loader/multiboot_mbi2.c +1 -0
            case MULTIBOOT_TAG_TYPE_EFI_BS:
            case MULTIBOOT_TAG_TYPE_EFI32_IH:
            case MULTIBOOT_TAG_TYPE_EFI64_IH:
+           case MULTIBOOT_TAG_TYPE_BASE_ADDR:
              break;
 
            default:
@@ -215,6 +220,27 @@ grub_multiboot_load (grub_file_t file, const char

*filename)

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char

Quoted 28 lines
grub-core/loader/multiboot_mbi2.c +21 -0
      accepted_consoles |= GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER;
      break;
 
+      case MULTIBOOT_HEADER_TAG_RELOCATABLE:
+     relocatable = 1;
+     rel_tag = (struct multiboot_header_tag_relocatable *) tag;
+     min_addr = rel_tag->min_addr;
+     max_addr = rel_tag->max_addr;
+     align = rel_tag->align;
+     switch (rel_tag->preference)
+       {
+       case MULTIBOOT_LOAD_PREFERENCE_LOW:
+         preference = GRUB_RELOCATOR_PREFERENCE_LOW;
+         break;
+
+       case MULTIBOOT_LOAD_PREFERENCE_HIGH:
+         preference = GRUB_RELOCATOR_PREFERENCE_HIGH;
+         break;
+
+       default:
+         preference = GRUB_RELOCATOR_PREFERENCE_NONE;
+       }
+     break;
+
      /* GRUB always page-aligns modules.  */
       case MULTIBOOT_HEADER_TAG_MODULE_ALIGN:
      break;
@@ -260,15 +286,22 @@ grub_multiboot_load (grub_file_t file, const char

*filename)

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char *filename)

Quoted 8 lines
grub-core/loader/multiboot_mbi2.c +2 -3
       else
      code_size = load_size;
 
-      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
-                                          &ch, load_addr,
-                                          code_size);
+      if (relocatable)
+     err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator,

&ch,

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char · line 291

Quoted 1 line
grub-core/loader/multiboot_mbi2.c +1 -0
+                                             min_addr, max_addr -

code_size,

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char · line 292

Quoted 1 line
grub-core/loader/multiboot_mbi2.c +1 -0
+                                             code_size, align ? align :

1,

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char

Quoted 16 lines
grub-core/loader/multiboot_mbi2.c +6 -0
+                                             preference, keep_bs);
+      else
+     err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+                                            &ch, load_addr,
+                                            code_size);
       if (err)
      {
        grub_dprintf ("multiboot_loader", "Error loading aout kludge\n");
        grub_free (buffer);
        return err;
      }
+      base_addr = get_physical_target_address (ch);
       source = get_virtual_current_address (ch);
 
       if ((grub_file_seek (file, offset)) == (grub_off_t) -1)
@@ -290,7 +323,9 @@ grub_multiboot_load (grub_file_t file, const char

*filename)

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char *filename)

Quoted 7 lines
grub-core/loader/multiboot_mbi2.c +3 -1
     }
   else
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer,
+                                  relocatable, min_addr, max_addr,
+                                  align, preference, &base_addr,

keep_bs);

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char

Quoted 4 lines
grub-core/loader/multiboot_mbi2.c +0 -0
       if (err)
      {
        grub_free (buffer);
@@ -303,6 +338,14 @@ grub_multiboot_load (grub_file_t file, const char

*filename)

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char *filename)

Quoted 13 lines
grub-core/loader/multiboot_mbi2.c +8 -0
   else if (entry_specified)
     grub_multiboot_payload_eip = entry;
 
+  if (relocatable)
+    {
+      if (base_addr > min_addr)
+     grub_multiboot_payload_eip += base_addr - min_addr;
+      else
+     grub_multiboot_payload_eip -= min_addr - base_addr;
+    }
+
   if (fbtag)
     err = grub_multiboot_set_console

(GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER,

grub-core/loader/multiboot_mbi2.c grub_multiboot_get_mbi_size (void) · line 453

Quoted 4 lines
grub-core/loader/multiboot_mbi2.c +0 -0
                                    accepted_consoles,
@@ -409,6 +452,7 @@ grub_multiboot_get_mbi_size (void)
               + grub_get_multiboot_mmap_count ()
               * sizeof (struct multiboot_mmap_entry)),

MULTIBOOT_TAG_ALIGN)

grub-core/loader/multiboot_mbi2.c grub_multiboot_get_mbi_size (void) · line 454

Quoted 1 line
grub-core/loader/multiboot_mbi2.c +0 -0
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer),

MULTIBOOT_TAG_ALIGN)

grub-core/loader/multiboot_mbi2.c grub_multiboot_get_mbi_size (void) · line 455

Quoted 1 line
grub-core/loader/multiboot_mbi2.c +1 -0
+    + ALIGN_UP (sizeof (struct multiboot_tag_base_addr),

MULTIBOOT_TAG_ALIGN)

grub-core/loader/multiboot_mbi2.c grub_multiboot_get_mbi_size (void)

Quoted 3 lines
grub-core/loader/multiboot_mbi2.c +0 -0
 #ifdef GRUB_MACHINE_EFI
 #ifdef __i386__
     + ALIGN_UP (sizeof (struct multiboot_tag_efi32),

MULTIBOOT_TAG_ALIGN)

grub-core/loader/multiboot_mbi2.c grub_multiboot_make_mbi (grub_uint32_t *target) · line 742

Quoted 2 lines
grub-core/loader/multiboot_mbi2.c +0 -0
@@ -698,6 +742,15 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
   ptrorig += (2 * sizeof (grub_uint32_t)) / sizeof

(grub_properly_aligned_t);

grub-core/loader/multiboot_mbi2.c grub_multiboot_make_mbi (grub_uint32_t *target) · line 744

Quoted 3 lines
grub-core/loader/multiboot_mbi2.c +1 -0
   {
+    struct multiboot_tag_base_addr *tag = (struct

multiboot_tag_base_addr *) ptrorig;

grub-core/loader/multiboot_mbi2.c grub_multiboot_make_mbi (grub_uint32_t *target)

Quoted 9 lines
grub-core/loader/multiboot_mbi2.c +8 -0
+    tag->type = MULTIBOOT_TAG_TYPE_BASE_ADDR;
+    tag->size = sizeof (struct multiboot_tag_base_addr);
+    tag->base_addr = base_addr;
+    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+       / sizeof (grub_properly_aligned_t);
+  }
+
+  {
     struct multiboot_tag_string *tag = (struct multiboot_tag_string *)

ptrorig;

include/grub/multiboot.h grub_multiboot_load (grub_file_t file, const char

Quoted 7 lines
grub-core/loader/multiboot_mbi2.c +0 -0
     tag->type = MULTIBOOT_TAG_TYPE_CMDLINE;
     tag->size = sizeof (struct multiboot_tag_string) + cmdline_size;
include/grub/multiboot.h +0 -0
index e13c084..ec322b0 100644
@@ -94,7 +94,9 @@ grub_multiboot_load (grub_file_t file, const char

*filename);

include/grub/multiboot.h grub_multiboot_load (grub_file_t file, const char *filename);

Quoted 5 lines
include/grub/multiboot.h +1 -1
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-                      void *buffer);
+                      void *buffer, int relocatable, grub_uint32_t

min_addr,

include/grub/multiboot.h grub_multiboot_load (grub_file_t file, const char · line 98

Quoted 1 line
include/grub/multiboot.h +1 -0
+                      grub_uint32_t max_addr, grub_size_t align,

grub_uint32_t preference,

include/grub/multiboot.h grub_multiboot_load (grub_file_t file, const char · line 99

Quoted 1 line
include/grub/multiboot.h +1 -0
+                      grub_uint32_t *base_addr, int

avoid_efi_boot_services);

Quoted 65 lines
include/grub/multiboot.h +0 -0
 extern grub_size_t grub_multiboot_pure_size;
 extern grub_size_t grub_multiboot_alloc_mbi;
 extern grub_uint32_t grub_multiboot_payload_eip;
include/multiboot2.h +24 -0
index 36a174f..c09bdbc 100644
@@ -62,6 +62,7 @@
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
 #define MULTIBOOT_TAG_TYPE_EFI32_IH          19
 #define MULTIBOOT_TAG_TYPE_EFI64_IH          20
+#define MULTIBOOT_TAG_TYPE_BASE_ADDR         21
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -72,11 +73,16 @@
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
 #define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
+#define MULTIBOOT_HEADER_TAG_RELOCATABLE  10
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
 #define MULTIBOOT_HEADER_TAG_OPTIONAL 1
 
+#define MULTIBOOT_LOAD_PREFERENCE_NONE 0
+#define MULTIBOOT_LOAD_PREFERENCE_LOW 1
+#define MULTIBOOT_LOAD_PREFERENCE_HIGH 2
+
 #define MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED 1
 #define MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED 2
 
@@ -161,6 +167,17 @@ struct multiboot_header_tag_module_align
   multiboot_uint32_t size;
 };
 
+struct multiboot_header_tag_relocatable
+{
+  multiboot_uint16_t type;
+  multiboot_uint16_t flags;
+  multiboot_uint32_t size;
+  multiboot_uint32_t min_addr;
+  multiboot_uint32_t max_addr;
+  multiboot_uint32_t align;
+  multiboot_uint32_t preference;
+};
+
 struct multiboot_color
 {
   multiboot_uint8_t red;
@@ -387,6 +404,13 @@ struct multiboot_tag_efi64_ih
   multiboot_uint64_t pointer;
 };
 
+struct multiboot_tag_base_addr
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t base_addr;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */
Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#22 lore raw

Re: [GRUB2 PATCH v3 4/4] multiboot2: Add support for relocatable images

In reply to Daniel Kiper
Quoted 29 lines · Daniel Kiper

Currently multiboot2 protocol loads image exactly at address specified in ELF or multiboot2 header. This solution works quite well on legacy BIOS platforms. It is possible because memory regions are placed at predictable addresses (though I was not able to find any spec which says that it is strong requirement, so, it looks that it is just a goodwill of hardware designers). However, EFI platforms are more volatile. Even if required memory regions live at specific addresses then they are sometimes simply not free (e.g. used by boot/runtime services on Dell PowerEdge R820 and OVMF). This means that you are not able to simply set up final image destination on build time. You have to provide method to relocate image contents to real load address which is usually different than load address specified in ELF and multiboot2 headers.

This patch provides all needed machinery to do self relocation in image code.
First of all GRUB2 reads min_addr (min. load addr), max_addr (max. load addr),
align (required image alignment), preference (it says which memory regions are
preferred by image, e.g. none, low, high) from multiboot_header_tag_relocatable
header tag contained in binary. Later loader tries to fulfill request (not only
that one) and if it succeeds then it informs image about real load address via
multiboot_tag_base_addr tag. At this stage GRUB2 role is finished. Starting from now executable must cope with relocations itself using whole static and dynamic knowledge provided by boot loader.

Quoted 2 lines

This patch does not provide functionality which could do relocations using ELF relocation data.

Can you add a check that image doesn't have any relocation entries? So that we fail nicely rather than loading half-working binary?

Quoted 449 lines

However, I was asked by Konrad Rzeszutek Wilk and Vladimir 'phcoder' Serbinenko to investigate that thing. It looks that relevant machinery
could be added to existing code (including this patch) without huge effort. Additionally, ELF relocation could live in parallel with self relocation provided
by this patch. However, during research I realized that first of all we should
establish the details how ELF relocatable image should look like and how it should
be build. At least to build proper test/example files.

As I saw multiboot2 protocol is able to consume ET_EXEC and ET_DYN ELF files.
Potentially we can use ET_DYN file type. It can be build with gcc/ld -pie option.
However, it contains a lot of unneeded stuff (e.g. INTERP, DYNAMIC, GNU_EH_FRAME
program headers) and it could be quite difficult to drop them (Hmmm... Is it
possible to build it properly with custom ld script?). So, I have checked ET_EXEC
file type. Sadly in this case linker by default resolves all local symbol relocations
and removes relocation related sections. Fortunately it is possible to leave them
as is with simple -q/--emit-relocs ld option. However, output file is quite fragile
and any operation on it should be done with great care (e.g. strip should be called
with --strip-unneeded option). So, this solution is not perfect too. It means that
maybe we should look for better solution. However, I think that we should not use
any custom tools and focus on functionalities provided by compiler and binutils.
In this context ld scripts looks quite promising but maybe you have better solutions.
So, what do you think about that?

This patch was tested with Xen image which uses that functionality. However, this Xen
feature is still under development and new patchset will be released in about 3-4 weeks.

Signed-off-by Daniel Kiper <[email protected] <javascript:;>>

---
v3 - suggestions/fixes:
- reduce number of casts
(suggested by Konrad Rzeszutek Wilk),
- remove unneeded space at the end of line
(suggested by Konrad Rzeszutek Wilk),
- improve commit message
(suggested by Konrad Rzeszutek Wilk).

---
 grub-core/loader/i386/multiboot_mbi.c |    6 ++-
 grub-core/loader/multiboot.c          |   12 ++++--
 grub-core/loader/multiboot_elfxx.c    |   28 ++++++++++----
 grub-core/loader/multiboot_mbi2.c     |   65
++++++++++++++++++++++++++++++---
 include/grub/multiboot.h              |    4 +-
 include/multiboot2.h                  |   24 ++++++++++++
 6 files changed, 120 insertions(+), 19 deletions(-)
grub-core/loader/i386/multiboot_mbi.c +2 -1
index f60b702..4fc83ed 100644
@@ -72,7 +72,8 @@ load_kernel (grub_file_t file, const char *filename,
   grub_err_t err;
   if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE)
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+                                    GRUB_RELOCATOR_PREFERENCE_NONE, NULL,
 0);
       if (err == GRUB_ERR_NONE) {
        return GRUB_ERR_NONE;
       }
grub-core/loader/i386/multiboot_mbi.c +2 -1
@@ -121,7 +122,8 @@ load_kernel (grub_file_t file, const char *filename,
       return GRUB_ERR_NONE;
     }
 
-  return grub_multiboot_load_elf (file, filename, buffer);
+  return grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+                                 GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
 }
 
 static struct multiboot_header *
grub-core/loader/multiboot.c +8 -3
index 18038fd..c0f51b6 100644
@@ -208,12 +208,18 @@ static grub_uint64_t highest_load;
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-                        void *buffer)
+                        void *buffer, int relocatable, grub_uint32_t
 min_addr,
+                        grub_uint32_t max_addr, grub_size_t align,
 grub_uint32_t preference,
+                        grub_uint32_t *base_addr, int
 avoid_efi_boot_services)
 {
   if (grub_multiboot_is_elf32 (buffer))
-    return grub_multiboot_load_elf32 (file, filename, buffer);
+    return grub_multiboot_load_elf32 (file, filename, buffer, relocatable,
+                                     min_addr, max_addr, align,
 preference,
+                                     base_addr, avoid_efi_boot_services);
   else if (grub_multiboot_is_elf64 (buffer))
-    return grub_multiboot_load_elf64 (file, filename, buffer);
+    return grub_multiboot_load_elf64 (file, filename, buffer, relocatable,
+                                     min_addr, max_addr, align,

preference,
+ base_addr, avoid_efi_boot_services);

return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF
magic"));
}

grub-core/loader/multiboot_elfxx.c +3 -1
index e3a39b6..0c01569 100644
@@ -51,7 +51,10 @@ CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
 }
 
 static grub_err_t
-CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char
 *filename, void *buffer)
+CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char
 *filename,
+                                    void *buffer, int relocatable,
 grub_uint32_t min_addr,
+                                    grub_uint32_t max_addr, grub_size_t

align, grub_uint32_t preference,
+ grub_uint32_t *base_addr, int
avoid_efi_boot_services)

 {
   Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
   char *phdr_base;
grub-core/loader/multiboot_elfxx.c +0 -0
@@ -89,19 +92,30 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file,

const char *filename, voi

          if (phdr(i)->p_paddr + phdr(i)->p_memsz > highest_load)
            highest_load = phdr(i)->p_paddr + phdr(i)->p_memsz;

- grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx,
memsz=0x%lx, vaddr=0x%lx\n",
- i, (long) phdr(i)->p_paddr, (long)
phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
+ grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx,
memsz=0x%lx, vaddr=0x%lx,"
+ "align=0x%lx, relocatable=%d,
avoid_efi_boot_services=%d\n", i,
+ (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz,
(long) phdr(i)->p_vaddr,
+ (long) align, relocatable,
avoid_efi_boot_services);

          {
            grub_relocator_chunk_t ch;

- err = grub_relocator_alloc_chunk_addr
(grub_multiboot_relocator,

grub-core/loader/multiboot_elfxx.c +3 -2
-                                                  &ch, phdr(i)->p_paddr,
-                                                  phdr(i)->p_memsz);
+
+           if (relocatable)
+             err = grub_relocator_alloc_chunk_align

(grub_multiboot_relocator, &ch,
+ min_addr, max_addr -
phdr(i)->p_memsz,
+ phdr(i)->p_memsz,
align ? align : 1,
+ preference,
avoid_efi_boot_services);

grub-core/loader/multiboot_elfxx.c +2 -0
+           else
+             err = grub_relocator_alloc_chunk_addr

(grub_multiboot_relocator,

grub-core/loader/multiboot_elfxx.c +2 -0
+                                                    &ch, phdr(i)->p_paddr,
+                                                    phdr(i)->p_memsz);
            if (err)
              {
                grub_dprintf ("multiboot_loader", "Error loading phdr
%d\n", i);
                return err;
              }
grub-core/loader/multiboot_elfxx.c +2 -0
+           if (base_addr)
+             *base_addr = get_physical_target_address (ch);
            source = get_virtual_current_address (ch);
          }
@@ -208,7 +222,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file,

const char *filename, voi
+ 1, sh->sh_size, sh->sh_addralign,

GRUB_RELOCATOR_PREFERENCE_NONE,

grub-core/loader/multiboot_elfxx.c +1 -1
-                                                   0);
+
 avoid_efi_boot_services);
            if (err)
              {
                grub_dprintf ("multiboot_loader", "Error loading shdr

%d\n", i);

grub-core/loader/multiboot_mbi2.c +6 -2
index ce68f48..03725a1 100644
@@ -68,6 +68,7 @@ static grub_size_t elf_sec_num, elf_sec_entsize;
 static unsigned elf_sec_shstrndx;
 static void *elf_sections;
 static int keep_bs = 0;
+static grub_uint32_t base_addr = 0;
 
 void
 grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize,
@@ -107,11 +108,14 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0, efi_entry_specified = 0;
+  struct multiboot_header_tag_relocatable *rel_tag;
+  int entry_specified = 0, efi_entry_specified = 0, relocatable = 0;
   grub_addr_t entry = 0, efi_entry = 0;
-  grub_uint32_t console_required = 0;
+  grub_uint32_t console_required = 0, min_addr = 0;
+  grub_uint32_t max_addr = 0, preference = GRUB_RELOCATOR_PREFERENCE_NONE;
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
+  grub_size_t align = 0;
 
   buffer = grub_malloc (MULTIBOOT_SEARCH);

if (!buffer)

grub-core/loader/multiboot_mbi2.c +1 -0
@@ -174,6 +178,7 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
              case MULTIBOOT_TAG_TYPE_EFI_BS:
              case MULTIBOOT_TAG_TYPE_EFI32_IH:
              case MULTIBOOT_TAG_TYPE_EFI64_IH:
+             case MULTIBOOT_TAG_TYPE_BASE_ADDR:
                break;
 

default:

grub-core/loader/multiboot_mbi2.c +21 -0
@@ -215,6 +220,27 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
        accepted_consoles |= GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER;
        break;
 
+      case MULTIBOOT_HEADER_TAG_RELOCATABLE:
+       relocatable = 1;
+       rel_tag = (struct multiboot_header_tag_relocatable *) tag;
+       min_addr = rel_tag->min_addr;
+       max_addr = rel_tag->max_addr;
+       align = rel_tag->align;
+       switch (rel_tag->preference)
+         {
+         case MULTIBOOT_LOAD_PREFERENCE_LOW:
+           preference = GRUB_RELOCATOR_PREFERENCE_LOW;
+           break;
+
+         case MULTIBOOT_LOAD_PREFERENCE_HIGH:
+           preference = GRUB_RELOCATOR_PREFERENCE_HIGH;
+           break;
+
+         default:
+           preference = GRUB_RELOCATOR_PREFERENCE_NONE;
+         }
+       break;
+
        /* GRUB always page-aligns modules.  */
       case MULTIBOOT_HEADER_TAG_MODULE_ALIGN:
        break;
grub-core/loader/multiboot_mbi2.c +13 -4
@@ -260,15 +286,22 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
       else
        code_size = load_size;
 
-      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
-                                            &ch, load_addr,
-                                            code_size);
+      if (relocatable)
+       err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator,
 &ch,
+                                               min_addr, max_addr -
 code_size,
+                                               code_size, align ? align :
 1,
+                                               preference, keep_bs);
+      else
+       err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+                                              &ch, load_addr,
+                                              code_size);
       if (err)
        {
          grub_dprintf ("multiboot_loader", "Error loading aout kludge\n");
          grub_free (buffer);
          return err;
        }
+      base_addr = get_physical_target_address (ch);
       source = get_virtual_current_address (ch);
 
       if ((grub_file_seek (file, offset)) == (grub_off_t) -1)
@@ -290,7 +323,9 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
     }
   else
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer,
+                                    relocatable, min_addr, max_addr,
+                                    align, preference, &base_addr,
 keep_bs);
       if (err)
        {
          grub_free (buffer);
grub-core/loader/multiboot_mbi2.c +8 -0
@@ -303,6 +338,14 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
   else if (entry_specified)
     grub_multiboot_payload_eip = entry;
 
+  if (relocatable)
+    {
+      if (base_addr > min_addr)
+       grub_multiboot_payload_eip += base_addr - min_addr;
+      else
+       grub_multiboot_payload_eip -= min_addr - base_addr;
+    }
+
   if (fbtag)
     err = grub_multiboot_set_console (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER,

accepted_consoles,

grub-core/loader/multiboot_mbi2.c +1 -0
@@ -409,6 +452,7 @@ grub_multiboot_get_mbi_size (void)
                 + grub_get_multiboot_mmap_count ()
                 * sizeof (struct multiboot_mmap_entry)),
 MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer),
 MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_base_addr),
 MULTIBOOT_TAG_ALIGN)

#ifdef GRUB_MACHINE_EFI
#ifdef __i386__
+ ALIGN_UP (sizeof (struct multiboot_tag_efi32), MULTIBOOT_TAG_ALIGN)

grub-core/loader/multiboot_mbi2.c +9 -0
@@ -698,6 +742,15 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
   ptrorig += (2 * sizeof (grub_uint32_t)) / sizeof
 (grub_properly_aligned_t);
 
   {
+    struct multiboot_tag_base_addr *tag = (struct multiboot_tag_base_addr
 *) ptrorig;
+    tag->type = MULTIBOOT_TAG_TYPE_BASE_ADDR;
+    tag->size = sizeof (struct multiboot_tag_base_addr);
+    tag->base_addr = base_addr;
+    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+       / sizeof (grub_properly_aligned_t);
+  }
+
+  {
     struct multiboot_tag_string *tag = (struct multiboot_tag_string *)
ptrorig;
     tag->type = MULTIBOOT_TAG_TYPE_CMDLINE;
     tag->size = sizeof (struct multiboot_tag_string) + cmdline_size;
include/grub/multiboot.h +3 -1
index e13c084..ec322b0 100644
@@ -94,7 +94,9 @@ grub_multiboot_load (grub_file_t file, const char
 *filename);
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-                        void *buffer);
+                        void *buffer, int relocatable, grub_uint32_t
 min_addr,
+                        grub_uint32_t max_addr, grub_size_t align,
 grub_uint32_t preference,
+                        grub_uint32_t *base_addr, int

avoid_efi_boot_services);
extern grub_size_t grub_multiboot_pure_size;
extern grub_size_t grub_multiboot_alloc_mbi;
extern grub_uint32_t grub_multiboot_payload_eip;

include/multiboot2.h +24 -1
index 36a174f..c09bdbc 100644
@@ -62,6 +62,7 @@
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
 #define MULTIBOOT_TAG_TYPE_EFI32_IH          19
 #define MULTIBOOT_TAG_TYPE_EFI64_IH          20
+#define MULTIBOOT_TAG_TYPE_BASE_ADDR         21
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -72,11 +73,16 @@
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
 #define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
+#define MULTIBOOT_HEADER_TAG_RELOCATABLE  10
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
 #define MULTIBOOT_HEADER_TAG_OPTIONAL 1
 
+#define MULTIBOOT_LOAD_PREFERENCE_NONE 0
+#define MULTIBOOT_LOAD_PREFERENCE_LOW 1
+#define MULTIBOOT_LOAD_PREFERENCE_HIGH 2
+
 #define MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED 1
 #define MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED 2
 
@@ -161,6 +167,17 @@ struct multiboot_header_tag_module_align
   multiboot_uint32_t size;
 };
 
+struct multiboot_header_tag_relocatable
+{
+  multiboot_uint16_t type;
+  multiboot_uint16_t flags;
+  multiboot_uint32_t size;
+  multiboot_uint32_t min_addr;
+  multiboot_uint32_t max_addr;
+  multiboot_uint32_t align;
+  multiboot_uint32_t preference;
+};
+
 struct multiboot_color
 {
   multiboot_uint8_t red;
@@ -387,6 +404,13 @@ struct multiboot_tag_efi64_ih
   multiboot_uint64_t pointer;
 };
 
+struct multiboot_tag_base_addr
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t base_addr;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */
--

1.7.10.4

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#25 lore raw
In reply to Daniel Kiper
Quoted 15 lines · Daniel Kiper
Quoted 14 lines · Daniel Kiper

Hi,

This patch series:
- enables EFI boot services usage in loaded images by multiboot2 protocol,
- add support for multiboot2 protocol compatible relocatable images.

Earlier versions of this patch series are extensively tested and used internally at least in Oracle. It should be mentioned that this release does not change any functionality introduced by earlier releases. It just takes into account comments posted by various people.

Quoted 5 lines
Quoted 2 lines

Hmmm... Ugh... Cough... Is it possible to get this stuff into 2.02 train?

Andrei, Vladimir, ping?

They look reasonable for 2.02. Strictly speaking they're features but code is well isolated and the alternative is painful. I answered the individual mails with review. Is there a followup with updating multiboot2 docs?

Quoted 3 lines

Daniel

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#26 lore raw
Quoted 17 lines · Vladimir 'phcoder' Serbinenko
Quoted 15 lines · Daniel Kiper
Quoted 14 lines · Daniel Kiper

Hi,

This patch series:
- enables EFI boot services usage in loaded images by multiboot2 protocol,
- add support for multiboot2 protocol compatible relocatable images.

Earlier versions of this patch series are extensively tested and used internally at least in Oracle. It should be mentioned that this release does not change any functionality introduced by earlier releases. It just takes into account comments posted by various people.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

Hmmm... Ugh... Cough... Is it possible to get this stuff into 2.02 train?

Andrei, Vladimir, ping?

They look reasonable for 2.02. Strictly speaking they're features but code

Great! Thanks a lot!

Quoted 2 lines

is well isolated and the alternative is painful. I answered the individual mails with review.

I have some questions which I send you shortly.

Quoted 1 line

Is there a followup with updating multiboot2 docs?

Do you want to have updated docs in 2.02?

By the way, where is multiboot2 doc file, which I should update, in git tree?

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#3 lore raw

Re: [GRUB2 PATCH v3 1/4] i386/relocator: Add grub_relocator64_efi relocator

In reply to Vladimir 'phcoder' Serbinenko
Quoted 13 lines · Vladimir 'phcoder' Serbinenko
Quoted 11 lines · Daniel Kiper

Add grub_relocator64_efi relocator. It will be used on EFI 64-bit platforms when multiboot2 compatible image requests MULTIBOOT_TAG_TYPE_EFI_BS. Relocator
will set lower parts of %rax and %rbx accordingly to multiboot2 specification.
On the other hand processor mode, just before jumping into loaded image, will
be set accordingly to Unified Extensible Firmware Interface Specification, Version 2.4 Errata B, section 2.3.4, x64 Platforms, boot services. This way loaded image will be able to use EFI boot services without any issues.

Quoted 1 line
Quoted 1 line

If idea is accepted I will prepare grub_relocator32_efi relocator too.

OK, as I can see idea in general is accepted. Do you want grub_relocator32_efi in 2.02 or 2.03 is OK?

Quoted 32 lines
Quoted 32 lines
Signed-off-by Daniel Kiper <[email protected] <javascript:;>>

---
v3 - suggestions/fixes:
- reuse grub-core/lib/i386/relocator64.S code instead of creating separate assembly file (suggested by Vladimir 'phcoder' Serbinenko),
- grub_multiboot_boot() cleanup
(suggested by Vladimir 'phcoder' Serbinenko),
- reuse multiboot_header_tag_entry_address struct instead of creating new one for EFI 64-bit entry point (suggested by Vladimir 'phcoder' Serbinenko).

---
 grub-core/lib/i386/relocator.c    |   48
++++++++++++++++++++++++++++++++++
 grub-core/lib/i386/relocator64.S  |    3 +++
 grub-core/loader/multiboot.c      |   51
+++++++++++++++++++++++++++++++++----
 grub-core/loader/multiboot_mbi2.c |   19 +++++++++++---
 include/grub/i386/multiboot.h     |   11 ++++++++
 include/grub/i386/relocator.h     |   21 +++++++++++++++
 include/multiboot2.h              |    1 +
 7 files changed, 145 insertions(+), 9 deletions(-)
grub-core/lib/i386/relocator.c +0 -0
index 71dd4f0..2b0c260 100644
@@ -69,6 +69,13 @@ extern grub_uint64_t grub_relocator64_rsi;
 extern grub_addr_t grub_relocator64_cr3;
 extern struct grub_i386_idt grub_relocator16_idt;
 
Quoted 9 lines

grub-core/lib/i386/relocator.c extern grub_uint64_t grub_relocator64_rsi; · line 80

Quoted 8 lines
grub-core/lib/i386/relocator.c +7 -0
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+extern grub_uint8_t grub_relocator64_efi_start;
+extern grub_uint8_t grub_relocator64_efi_end;
+#endif
+#endif
+
 

Can we move this and all to a separate file to avoid too many #ifdef ?

Do you think about grub-core/lib/i386/relocator-efi.c or something like that? If yes then I do not think it is a problem.

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#9 lore raw

Re: [GRUB2 PATCH v3 2/4] multiboot2: Add tags used to pass ImageHandle to loaded image

In reply to Vladimir 'phcoder' Serbinenko
Quoted 52 lines · Vladimir 'phcoder' Serbinenko
Quoted 50 lines · Daniel Kiper

Add tags used to pass ImageHandle to loaded image if requested. It is used by at least ExitBootServices() function.

Signed-off-by Daniel Kiper <[email protected] <javascript:;>>

---
v3 - suggestions/fixes:
- mbi EFI related stuff size calculation
should depend on target architecture
(suggested by Konrad Rzeszutek Wilk),
- use plain type instead of pointer
dereference as sizeof() argument
(suggested by Konrad Rzeszutek Wilk),
- improve commit message
(suggested by Konrad Rzeszutek Wilk).

---
 grub-core/loader/multiboot_mbi2.c |   50
++++++++++++++++++++++++++++++-------
 include/multiboot2.h              |   16 ++++++++++++
 2 files changed, 57 insertions(+), 9 deletions(-)
grub-core/loader/multiboot_mbi2.c +2 -0
index a3dca90..7591edc 100644
@@ -172,6 +172,8 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
              case MULTIBOOT_TAG_TYPE_NETWORK:
              case MULTIBOOT_TAG_TYPE_EFI_MMAP:
              case MULTIBOOT_TAG_TYPE_EFI_BS:
+             case MULTIBOOT_TAG_TYPE_EFI32_IH:
+             case MULTIBOOT_TAG_TYPE_EFI64_IH:
                break;
 

default:

grub-core/loader/multiboot_mbi2.c +6 -0
@@ -407,16 +409,22 @@ grub_multiboot_get_mbi_size (void)
                 + grub_get_multiboot_mmap_count ()
                 * sizeof (struct multiboot_mmap_entry)),
 MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer),
 MULTIBOOT_TAG_ALIGN)
+#ifdef GRUB_MACHINE_EFI
+#ifdef __i386__
     + ALIGN_UP (sizeof (struct multiboot_tag_efi32), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi32_ih),
 MULTIBOOT_TAG_ALIGN)
+#endif
+#ifdef __x86_64__
     + ALIGN_UP (sizeof (struct multiboot_tag_efi64), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi64_ih),
Quoted 8 lines

grub-core/loader/multiboot_mbi2.c grub_multiboot_get_mbi_size (void)

Quoted 6 lines
grub-core/loader/multiboot_mbi2.c +4 -0
 MULTIBOOT_TAG_ALIGN)
+#endif
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
+               + efi_mmap_size, MULTIBOOT_TAG_ALIGN)
+#endif
 

It doesn't need to be exact. It's just an upper bound. Feel free to simplify knowing this, removing few #ifdef.

OK.

Quoted 54 lines
Quoted 54 lines
grub-core/loader/multiboot_mbi2.c +29 -9
     + ALIGN_UP (sizeof (struct multiboot_tag_old_acpi)
                + sizeof (struct grub_acpi_rsdp_v10), MULTIBOOT_TAG_ALIGN)
     + acpiv2_size ()
     + net_size ()
-#ifdef GRUB_MACHINE_EFI
-    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
-               + efi_mmap_size, MULTIBOOT_TAG_ALIGN)
-#endif
     + sizeof (struct multiboot_tag_vbe) + MULTIBOOT_TAG_ALIGN - 1
     + sizeof (struct multiboot_tag_apm) + MULTIBOOT_TAG_ALIGN - 1;
 }
@@ -907,11 +915,35 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
 
   if (keep_bs)
     {
-      struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
-      tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
-      tag->size = sizeof (struct multiboot_tag);
-      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-       / sizeof (grub_properly_aligned_t);
+      {
+       struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
+       tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
+       tag->size = sizeof (struct multiboot_tag);
+       ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+         / sizeof (grub_properly_aligned_t);
+      }
+
+#ifdef __i386__
+      {
+       struct multiboot_tag_efi32_ih *tag = (struct
 multiboot_tag_efi32_ih *) ptrorig;
+       tag->type = MULTIBOOT_TAG_TYPE_EFI32_IH;
+       tag->size = sizeof (struct multiboot_tag_efi32_ih);
+       tag->pointer = (grub_addr_t) grub_efi_image_handle;
+       ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+         / sizeof (grub_properly_aligned_t);
+      }
+#endif
+
+#ifdef __x86_64__
+      {
+       struct multiboot_tag_efi64_ih *tag = (struct
 multiboot_tag_efi64_ih *) ptrorig;
+       tag->type = MULTIBOOT_TAG_TYPE_EFI64_IH;
+       tag->size = sizeof (struct multiboot_tag_efi64_ih);
+       tag->pointer = (grub_addr_t) grub_efi_image_handle;
+       ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+         / sizeof (grub_properly_aligned_t);
+      }
+#endif
     }

#endif

Quoted 12 lines

include/multiboot2.h line 65

Quoted 11 lines
include/multiboot2.h +2 -0
index d96aa40..36a174f 100644
@@ -60,6 +60,8 @@
 #define MULTIBOOT_TAG_TYPE_NETWORK           16
 #define MULTIBOOT_TAG_TYPE_EFI_MMAP          17
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
+#define MULTIBOOT_TAG_TYPE_EFI32_IH          19
+#define MULTIBOOT_TAG_TYPE_EFI64_IH          20
 

Is there a followup to define those in texi doc as well?

No problem. Which one should I update?

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#13 lore raw

Re: [GRUB2 PATCH v3 3/4] multiboot2: Do not pass memory maps to image if EFI boot services are enabled

In reply to Vladimir 'phcoder' Serbinenko
Quoted 10 lines · Vladimir 'phcoder' Serbinenko
Quoted 8 lines · Daniel Kiper

Do not pass memory maps to image if it asked for EFI boot services. Main reason for not providing maps is because they will likely be invalid. We do a few allocations after filling them, e.g. for relocator needs. Usually we do not care as we would already finish boot services. If we keep boot services then it is easier to not provide maps. However, if image needs memory maps and they are not provided by bootloader then it should get them itself just before ExitBootServices() call.

Quoted 2 lines

Patch gets too cluttered. Can you provide 2 variants: normal (for commit) and with ignoring whitespaces (for review)

Sure thing! I will prepare whole updated patch series next week. I hope that it is OK for you.

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#10 lore raw

Re: [GRUB2 PATCH v3 2/4] multiboot2: Add tags used to pass ImageHandle to loaded image

In reply to Daniel Kiper
Quoted 137 lines · Daniel Kiper
Quoted 1 line · Vladimir 'phcoder' Serbinenko

On Wednesday, March 2, 2016, Daniel Kiper <[email protected]

Quoted 5 lines · <javascript:;>> wrote:
Quoted 5 lines

Add tags used to pass ImageHandle to loaded image if requested. It is used by at least ExitBootServices() function.

Signed-off-by Daniel Kiper <[email protected] <javascript:;>

<javascript:;>>

Quoted 30 lines
Quoted 30 lines

---
v3 - suggestions/fixes:
- mbi EFI related stuff size calculation
should depend on target architecture
(suggested by Konrad Rzeszutek Wilk),
- use plain type instead of pointer
dereference as sizeof() argument
(suggested by Konrad Rzeszutek Wilk),
- improve commit message
(suggested by Konrad Rzeszutek Wilk).

---
 grub-core/loader/multiboot_mbi2.c |   50
++++++++++++++++++++++++++++++-------
 include/multiboot2.h              |   16 ++++++++++++
 2 files changed, 57 insertions(+), 9 deletions(-)
grub-core/loader/multiboot_mbi2.c +2 -0
index a3dca90..7591edc 100644
@@ -172,6 +172,8 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
              case MULTIBOOT_TAG_TYPE_NETWORK:
              case MULTIBOOT_TAG_TYPE_EFI_MMAP:
              case MULTIBOOT_TAG_TYPE_EFI_BS:
+             case MULTIBOOT_TAG_TYPE_EFI32_IH:
+             case MULTIBOOT_TAG_TYPE_EFI64_IH:
                break;
 
Quoted 10 lines

grub-core/loader/multiboot_mbi2.c grub_multiboot_get_mbi_size (void) · line 416

Quoted 10 lines

default:

grub-core/loader/multiboot_mbi2.c +2 -0
@@ -407,16 +409,22 @@ grub_multiboot_get_mbi_size (void)
                 + grub_get_multiboot_mmap_count ()
                 * sizeof (struct multiboot_mmap_entry)),
 MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer),
 MULTIBOOT_TAG_ALIGN)
+#ifdef GRUB_MACHINE_EFI
+#ifdef __i386__
     + ALIGN_UP (sizeof (struct multiboot_tag_efi32),

MULTIBOOT_TAG_ALIGN)

Quoted 5 lines

grub-core/loader/multiboot_mbi2.c grub_multiboot_get_mbi_size (void) · line 421

Quoted 5 lines
grub-core/loader/multiboot_mbi2.c +3 -0
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi32_ih),
 MULTIBOOT_TAG_ALIGN)
+#endif
+#ifdef __x86_64__
     + ALIGN_UP (sizeof (struct multiboot_tag_efi64),

MULTIBOOT_TAG_ALIGN)

Quoted 9 lines

grub-core/loader/multiboot_mbi2.c grub_multiboot_get_mbi_size (void)

Quoted 7 lines
grub-core/loader/multiboot_mbi2.c +5 -0
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi64_ih),
 MULTIBOOT_TAG_ALIGN)
+#endif
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
+               + efi_mmap_size, MULTIBOOT_TAG_ALIGN)
+#endif
 

It doesn't need to be exact. It's just an upper bound. Feel free to simplify knowing this, removing few #ifdef.

OK.

Quoted 2 lines

grub-core/loader/multiboot_mbi2.c grub_multiboot_get_mbi_size (void) · line 430

Quoted 2 lines
grub-core/loader/multiboot_mbi2.c +0 -0
     + ALIGN_UP (sizeof (struct multiboot_tag_old_acpi)
                + sizeof (struct grub_acpi_rsdp_v10),

MULTIBOOT_TAG_ALIGN)

Quoted 52 lines
Quoted 52 lines
grub-core/loader/multiboot_mbi2.c +29 -9
     + acpiv2_size ()
     + net_size ()
-#ifdef GRUB_MACHINE_EFI
-    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
-               + efi_mmap_size, MULTIBOOT_TAG_ALIGN)
-#endif
     + sizeof (struct multiboot_tag_vbe) + MULTIBOOT_TAG_ALIGN - 1
     + sizeof (struct multiboot_tag_apm) + MULTIBOOT_TAG_ALIGN - 1;
 }
@@ -907,11 +915,35 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
 
   if (keep_bs)
     {
-      struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
-      tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
-      tag->size = sizeof (struct multiboot_tag);
-      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-       / sizeof (grub_properly_aligned_t);
+      {
+       struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
+       tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
+       tag->size = sizeof (struct multiboot_tag);
+       ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+         / sizeof (grub_properly_aligned_t);
+      }
+
+#ifdef __i386__
+      {
+       struct multiboot_tag_efi32_ih *tag = (struct
 multiboot_tag_efi32_ih *) ptrorig;
+       tag->type = MULTIBOOT_TAG_TYPE_EFI32_IH;
+       tag->size = sizeof (struct multiboot_tag_efi32_ih);
+       tag->pointer = (grub_addr_t) grub_efi_image_handle;
+       ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+         / sizeof (grub_properly_aligned_t);
+      }
+#endif
+
+#ifdef __x86_64__
+      {
+       struct multiboot_tag_efi64_ih *tag = (struct
 multiboot_tag_efi64_ih *) ptrorig;
+       tag->type = MULTIBOOT_TAG_TYPE_EFI64_IH;
+       tag->size = sizeof (struct multiboot_tag_efi64_ih);
+       tag->pointer = (grub_addr_t) grub_efi_image_handle;
+       ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+         / sizeof (grub_properly_aligned_t);
+      }
+#endif
     }

#endif

Quoted 12 lines

include/multiboot2.h line 65

Quoted 11 lines
include/multiboot2.h +2 -0
index d96aa40..36a174f 100644
@@ -60,6 +60,8 @@
 #define MULTIBOOT_TAG_TYPE_NETWORK           16
 #define MULTIBOOT_TAG_TYPE_EFI_MMAP          17
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
+#define MULTIBOOT_TAG_TYPE_EFI32_IH          19
+#define MULTIBOOT_TAG_TYPE_EFI64_IH          20
 

Is there a followup to define those in texi doc as well?

Quoted 2 lines

No problem. Which one should I update?

It's in the branch 'multiboot2' in git:
http://git.savannah.gnu.org/cgit/grub.git/tree/doc/multiboot.texi?h=multiboot2

Quoted 3 lines

Daniel

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#4 lore raw

Re: [GRUB2 PATCH v3 1/4] i386/relocator: Add grub_relocator64_efi relocator

In reply to Daniel Kiper
Quoted 68 lines · Daniel Kiper
Quoted 1 line · Vladimir 'phcoder' Serbinenko

On Wednesday, March 2, 2016, Daniel Kiper <[email protected]

Quoted 2 lines · <javascript:;>> wrote:
Quoted 2 lines

Add grub_relocator64_efi relocator. It will be used on EFI 64-bit

platforms

Quoted 5 lines
Quoted 5 lines

when multiboot2 compatible image requests MULTIBOOT_TAG_TYPE_EFI_BS. Relocator
will set lower parts of %rax and %rbx accordingly to multiboot2 specification.
On the other hand processor mode, just before jumping into loaded

image,

Quoted 2 lines
Quoted 2 lines

will
be set accordingly to Unified Extensible Firmware Interface

Specification,

Quoted 1 line
Quoted 1 line

Version 2.4 Errata B, section 2.3.4, x64 Platforms, boot services.

This way

Quoted 3 lines
Quoted 3 lines

loaded image will be able to use EFI boot services without any issues.

If idea is accepted I will prepare grub_relocator32_efi relocator too.

OK, as I can see idea in general is accepted. Do you want grub_relocator32_efi in 2.02 or 2.03 is OK?

Quoted 1 line
Quoted 1 line
Signed-off-by Daniel Kiper <[email protected] <javascript:;>

<javascript:;>>

Quoted 31 lines
Quoted 31 lines

---
v3 - suggestions/fixes:
- reuse grub-core/lib/i386/relocator64.S code instead of creating separate assembly file (suggested by Vladimir 'phcoder' Serbinenko),
- grub_multiboot_boot() cleanup
(suggested by Vladimir 'phcoder' Serbinenko),
- reuse multiboot_header_tag_entry_address struct instead of creating new one for EFI 64-bit entry point (suggested by Vladimir 'phcoder' Serbinenko).

---
 grub-core/lib/i386/relocator.c    |   48
++++++++++++++++++++++++++++++++++
 grub-core/lib/i386/relocator64.S  |    3 +++
 grub-core/loader/multiboot.c      |   51
+++++++++++++++++++++++++++++++++----
 grub-core/loader/multiboot_mbi2.c |   19 +++++++++++---
 include/grub/i386/multiboot.h     |   11 ++++++++
 include/grub/i386/relocator.h     |   21 +++++++++++++++
 include/multiboot2.h              |    1 +
 7 files changed, 145 insertions(+), 9 deletions(-)
grub-core/lib/i386/relocator.c +0 -0
index 71dd4f0..2b0c260 100644
@@ -69,6 +69,13 @@ extern grub_uint64_t grub_relocator64_rsi;
 extern grub_addr_t grub_relocator64_cr3;
 extern struct grub_i386_idt grub_relocator16_idt;
 
Quoted 9 lines

grub-core/lib/i386/relocator.c extern grub_uint64_t grub_relocator64_rsi; · line 80

Quoted 8 lines
grub-core/lib/i386/relocator.c +7 -0
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+extern grub_uint8_t grub_relocator64_efi_start;
+extern grub_uint8_t grub_relocator64_efi_end;
+#endif
+#endif
+
 

Can we move this and all to a separate file to avoid too many #ifdef ?

Quoted 3 lines

Do you think about grub-core/lib/i386/relocator-efi.c or something like that?

grub-core/lib/x86_64/efi/relocator.c would probably fit in naming scheme
the best

Quoted 4 lines

If yes then I do not think it is a problem.

Daniel

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#5 lore raw

Re: [GRUB2 PATCH v3 1/4] i386/relocator: Add grub_relocator64_efi relocator

In reply to Daniel Kiper
Quoted 16 lines · Daniel Kiper
Quoted 1 line · Vladimir 'phcoder' Serbinenko

On Wednesday, March 2, 2016, Daniel Kiper <[email protected]

Quoted 2 lines · <javascript:;>> wrote:
Quoted 2 lines

Add grub_relocator64_efi relocator. It will be used on EFI 64-bit

platforms

Quoted 5 lines
Quoted 5 lines

when multiboot2 compatible image requests MULTIBOOT_TAG_TYPE_EFI_BS. Relocator
will set lower parts of %rax and %rbx accordingly to multiboot2 specification.
On the other hand processor mode, just before jumping into loaded

image,

Quoted 2 lines
Quoted 2 lines

will
be set accordingly to Unified Extensible Firmware Interface

Specification,

Quoted 9 lines
Quoted 1 line
Quoted 1 line

Version 2.4 Errata B, section 2.3.4, x64 Platforms, boot services.

This way

Quoted 3 lines
Quoted 3 lines

loaded image will be able to use EFI boot services without any issues.

If idea is accepted I will prepare grub_relocator32_efi relocator too.

OK, as I can see idea in general is accepted. Do you want grub_relocator32_efi in 2.02 or 2.03 is OK?

Is there already a user for it? Or someone who is expected to adopt it shortly?

Quoted 50 lines
Quoted 2 lines
Quoted 2 lines
Signed-off-by Daniel Kiper <[email protected] <javascript:;>

<javascript:;>>

Quoted 31 lines
Quoted 31 lines

---
v3 - suggestions/fixes:
- reuse grub-core/lib/i386/relocator64.S code instead of creating separate assembly file (suggested by Vladimir 'phcoder' Serbinenko),
- grub_multiboot_boot() cleanup
(suggested by Vladimir 'phcoder' Serbinenko),
- reuse multiboot_header_tag_entry_address struct instead of creating new one for EFI 64-bit entry point (suggested by Vladimir 'phcoder' Serbinenko).

---
 grub-core/lib/i386/relocator.c    |   48
++++++++++++++++++++++++++++++++++
 grub-core/lib/i386/relocator64.S  |    3 +++
 grub-core/loader/multiboot.c      |   51
+++++++++++++++++++++++++++++++++----
 grub-core/loader/multiboot_mbi2.c |   19 +++++++++++---
 include/grub/i386/multiboot.h     |   11 ++++++++
 include/grub/i386/relocator.h     |   21 +++++++++++++++
 include/multiboot2.h              |    1 +
 7 files changed, 145 insertions(+), 9 deletions(-)
grub-core/lib/i386/relocator.c +0 -0
index 71dd4f0..2b0c260 100644
@@ -69,6 +69,13 @@ extern grub_uint64_t grub_relocator64_rsi;
 extern grub_addr_t grub_relocator64_cr3;
 extern struct grub_i386_idt grub_relocator16_idt;
 
Quoted 9 lines

grub-core/lib/i386/relocator.c extern grub_uint64_t grub_relocator64_rsi; · line 80

Quoted 8 lines
grub-core/lib/i386/relocator.c +7 -0
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+extern grub_uint8_t grub_relocator64_efi_start;
+extern grub_uint8_t grub_relocator64_efi_end;
+#endif
+#endif
+
 

Can we move this and all to a separate file to avoid too many #ifdef ?

Do you think about grub-core/lib/i386/relocator-efi.c or something like that?
If yes then I do not think it is a problem.

Daniel

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#27 lore raw
In reply to Daniel Kiper
Quoted 31 lines · Daniel Kiper
Quoted 1 line · Vladimir 'phcoder' Serbinenko

On Wednesday, March 9, 2016, Daniel Kiper <[email protected]

Quoted 16 lines · <javascript:;>> wrote:
Quoted 16 lines · Daniel Kiper
Quoted 14 lines

Hi,

This patch series:
- enables EFI boot services usage in loaded images by multiboot2 protocol,
- add support for multiboot2 protocol compatible relocatable images.

Earlier versions of this patch series are extensively tested and used internally at least in Oracle. It should be mentioned that this release does not change any functionality introduced by earlier releases. It just takes into account comments posted by various people.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

Hmmm... Ugh... Cough... Is it possible to get this stuff into 2.02 train?

Andrei, Vladimir, ping?

They look reasonable for 2.02. Strictly speaking they're features but

code

Great! Thanks a lot!

Quoted 1 line

is well isolated and the alternative is painful. I answered the

Quoted 9 lines

individual

Quoted 1 line

mails with review.

I have some questions which I send you shortly.

Quoted 1 line

Is there a followup with updating multiboot2 docs?

Do you want to have updated docs in 2.02?

multiboot2 spec is a rolling update.

Quoted 6 lines

By the way, where is multiboot2 doc file, which I should update, in git tree?

Daniel

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#18 lore raw

Re: [GRUB2 PATCH v3 4/4] multiboot2: Add support for relocatable images

In reply to Vladimir 'phcoder' Serbinenko
Quoted 55 lines · Vladimir 'phcoder' Serbinenko
Quoted 43 lines · Daniel Kiper

Currently multiboot2 protocol loads image exactly at address specified in ELF or multiboot2 header. This solution works quite well on legacy BIOS platforms. It is possible because memory regions are placed at predictable addresses (though I was not able to find any spec which says that it is strong requirement, so, it looks that it is just a goodwill of hardware designers). However, EFI platforms are more volatile. Even if required memory regions live at specific addresses then they are sometimes simply not free (e.g. used by boot/runtime services on Dell PowerEdge R820 and OVMF). This means that you are not able to simply set up final image destination on build time. You have to provide method to relocate image contents to real load address which is usually different than load address specified in ELF and multiboot2 headers.

This patch provides all needed machinery to do self relocation in image code.
First of all GRUB2 reads min_addr (min. load addr), max_addr (max. load addr),
align (required image alignment), preference (it says which memory regions are
preferred by image, e.g. none, low, high) from multiboot_header_tag_relocatable
header tag contained in binary. Later loader tries to fulfill request (not only
that one) and if it succeeds then it informs image about real load address via
multiboot_tag_base_addr tag. At this stage GRUB2 role is finished. Starting from now executable must cope with relocations itself using whole static and dynamic knowledge provided by boot loader.

This patch does not provide functionality which could do relocations using ELF relocation data. However, I was asked by Konrad Rzeszutek Wilk and Vladimir
'phcoder' Serbinenko to investigate that thing. It looks that relevant machinery
could be added to existing code (including this patch) without huge effort. Additionally, ELF relocation could live in parallel with self relocation provided
by this patch. However, during research I realized that first of all we should
establish the details how ELF relocatable image should look like and how it should
be build. At least to build proper test/example files.

Quoted 9 lines

As I saw multiboot2 protocol is able to consume ET_EXEC and ET_DYN ELF files.
Potentially we can use ET_DYN file type. It can be build with gcc/ld -pie option.
However, it contains a lot of unneeded stuff (e.g. INTERP, DYNAMIC, GNU_EH_FRAME
program headers) and it could be quite difficult to drop them (Hmmm... Is it
possible to build it properly with custom ld script?).

Quoted 1 line

How big are they? Are they a real problem?

ET_DYN file is ~2.5 times bigger then normal ET_EXEC (I has checked multiboot2.elf from GRUB2). There is a chance that we can ignore most of stuff in ET_DYN, however, it does not look nice. IMO, image should have only what is needed by loader.

Quoted 19 lines
Quoted 10 lines

So, I have checked ET_EXEC
file type. Sadly in this case linker by default resolves all local symbol relocations
and removes relocation related sections. Fortunately it is possible to leave them
as is with simple -q/--emit-relocs ld option. However, output file is quite fragile
and any operation on it should be done with great care (e.g. strip should be called
with --strip-unneeded option). So, this solution is not perfect too. It

Quoted 9 lines

means that
maybe we should look for better solution. However, I think that we should not use
any custom tools and focus on functionalities provided by compiler and binutils.
In this context ld scripts looks quite promising but maybe you have better solutions.
So, what do you think about that?

Quoted 2 lines

Another possibility is to use intermediary .o files like we do for modules
and like Linux does for modules AFAIR.

Correct but I think that it would be better to have normal ET_EXEC or ET_DYN file.

Quoted 284 lines
Quoted 284 lines

This patch was tested with Xen image which uses that functionality. However, this Xen
feature is still under development and new patchset will be released in about 3-4 weeks.

Signed-off-by Daniel Kiper <[email protected] <javascript:;>>

---
v3 - suggestions/fixes:
- reduce number of casts
(suggested by Konrad Rzeszutek Wilk),
- remove unneeded space at the end of line
(suggested by Konrad Rzeszutek Wilk),
- improve commit message
(suggested by Konrad Rzeszutek Wilk).

---
 grub-core/loader/i386/multiboot_mbi.c |    6 ++-
 grub-core/loader/multiboot.c          |   12 ++++--
 grub-core/loader/multiboot_elfxx.c    |   28 ++++++++++----
 grub-core/loader/multiboot_mbi2.c     |   65
++++++++++++++++++++++++++++++---
 include/grub/multiboot.h              |    4 +-
 include/multiboot2.h                  |   24 ++++++++++++
 6 files changed, 120 insertions(+), 19 deletions(-)
grub-core/loader/i386/multiboot_mbi.c +2 -1
index f60b702..4fc83ed 100644
@@ -72,7 +72,8 @@ load_kernel (grub_file_t file, const char *filename,
   grub_err_t err;
   if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE)
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+                                    GRUB_RELOCATOR_PREFERENCE_NONE, NULL,
 0);
       if (err == GRUB_ERR_NONE) {
        return GRUB_ERR_NONE;
       }
grub-core/loader/i386/multiboot_mbi.c +2 -1
@@ -121,7 +122,8 @@ load_kernel (grub_file_t file, const char *filename,
       return GRUB_ERR_NONE;
     }
 
-  return grub_multiboot_load_elf (file, filename, buffer);
+  return grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+                                 GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
 }
 
 static struct multiboot_header *
grub-core/loader/multiboot.c +8 -3
index 18038fd..c0f51b6 100644
@@ -208,12 +208,18 @@ static grub_uint64_t highest_load;
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-                        void *buffer)
+                        void *buffer, int relocatable, grub_uint32_t
 min_addr,
+                        grub_uint32_t max_addr, grub_size_t align,
 grub_uint32_t preference,
+                        grub_uint32_t *base_addr, int
 avoid_efi_boot_services)
 {
   if (grub_multiboot_is_elf32 (buffer))
-    return grub_multiboot_load_elf32 (file, filename, buffer);
+    return grub_multiboot_load_elf32 (file, filename, buffer, relocatable,
+                                     min_addr, max_addr, align,
 preference,
+                                     base_addr, avoid_efi_boot_services);
   else if (grub_multiboot_is_elf64 (buffer))
-    return grub_multiboot_load_elf64 (file, filename, buffer);
+    return grub_multiboot_load_elf64 (file, filename, buffer, relocatable,
+                                     min_addr, max_addr, align,

preference,
+ base_addr, avoid_efi_boot_services);

return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF
magic"));
}

grub-core/loader/multiboot_elfxx.c +3 -1
index e3a39b6..0c01569 100644
@@ -51,7 +51,10 @@ CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
 }
 
 static grub_err_t
-CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char
 *filename, void *buffer)
+CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char
 *filename,
+                                    void *buffer, int relocatable,
 grub_uint32_t min_addr,
+                                    grub_uint32_t max_addr, grub_size_t

align, grub_uint32_t preference,
+ grub_uint32_t *base_addr, int
avoid_efi_boot_services)

 {
   Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
   char *phdr_base;
grub-core/loader/multiboot_elfxx.c +0 -0
@@ -89,19 +92,30 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file,

const char *filename, voi

          if (phdr(i)->p_paddr + phdr(i)->p_memsz > highest_load)
            highest_load = phdr(i)->p_paddr + phdr(i)->p_memsz;

- grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx,
memsz=0x%lx, vaddr=0x%lx\n",
- i, (long) phdr(i)->p_paddr, (long)
phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
+ grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx,
memsz=0x%lx, vaddr=0x%lx,"
+ "align=0x%lx, relocatable=%d,
avoid_efi_boot_services=%d\n", i,
+ (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz,
(long) phdr(i)->p_vaddr,
+ (long) align, relocatable,
avoid_efi_boot_services);

          {
            grub_relocator_chunk_t ch;

- err = grub_relocator_alloc_chunk_addr
(grub_multiboot_relocator,

grub-core/loader/multiboot_elfxx.c +3 -2
-                                                  &ch, phdr(i)->p_paddr,
-                                                  phdr(i)->p_memsz);
+
+           if (relocatable)
+             err = grub_relocator_alloc_chunk_align

(grub_multiboot_relocator, &ch,
+ min_addr, max_addr -
phdr(i)->p_memsz,
+ phdr(i)->p_memsz,
align ? align : 1,
+ preference,
avoid_efi_boot_services);

grub-core/loader/multiboot_elfxx.c +2 -0
+           else
+             err = grub_relocator_alloc_chunk_addr

(grub_multiboot_relocator,

grub-core/loader/multiboot_elfxx.c +2 -0
+                                                    &ch, phdr(i)->p_paddr,
+                                                    phdr(i)->p_memsz);
            if (err)
              {
                grub_dprintf ("multiboot_loader", "Error loading phdr
%d\n", i);
                return err;
              }
grub-core/loader/multiboot_elfxx.c +2 -0
+           if (base_addr)
+             *base_addr = get_physical_target_address (ch);
            source = get_virtual_current_address (ch);
          }
@@ -208,7 +222,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file,

const char *filename, voi
+ 1, sh->sh_size, sh->sh_addralign,

GRUB_RELOCATOR_PREFERENCE_NONE,

grub-core/loader/multiboot_elfxx.c +1 -1
-                                                   0);
+
 avoid_efi_boot_services);
            if (err)
              {
                grub_dprintf ("multiboot_loader", "Error loading shdr

%d\n", i);

grub-core/loader/multiboot_mbi2.c +6 -2
index ce68f48..03725a1 100644
@@ -68,6 +68,7 @@ static grub_size_t elf_sec_num, elf_sec_entsize;
 static unsigned elf_sec_shstrndx;
 static void *elf_sections;
 static int keep_bs = 0;
+static grub_uint32_t base_addr = 0;
 
 void
 grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize,
@@ -107,11 +108,14 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0, efi_entry_specified = 0;
+  struct multiboot_header_tag_relocatable *rel_tag;
+  int entry_specified = 0, efi_entry_specified = 0, relocatable = 0;
   grub_addr_t entry = 0, efi_entry = 0;
-  grub_uint32_t console_required = 0;
+  grub_uint32_t console_required = 0, min_addr = 0;
+  grub_uint32_t max_addr = 0, preference = GRUB_RELOCATOR_PREFERENCE_NONE;
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
+  grub_size_t align = 0;
 
   buffer = grub_malloc (MULTIBOOT_SEARCH);

if (!buffer)

grub-core/loader/multiboot_mbi2.c +1 -0
@@ -174,6 +178,7 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
              case MULTIBOOT_TAG_TYPE_EFI_BS:
              case MULTIBOOT_TAG_TYPE_EFI32_IH:
              case MULTIBOOT_TAG_TYPE_EFI64_IH:
+             case MULTIBOOT_TAG_TYPE_BASE_ADDR:
                break;
 

default:

grub-core/loader/multiboot_mbi2.c +21 -0
@@ -215,6 +220,27 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
        accepted_consoles |= GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER;
        break;
 
+      case MULTIBOOT_HEADER_TAG_RELOCATABLE:
+       relocatable = 1;
+       rel_tag = (struct multiboot_header_tag_relocatable *) tag;
+       min_addr = rel_tag->min_addr;
+       max_addr = rel_tag->max_addr;
+       align = rel_tag->align;
+       switch (rel_tag->preference)
+         {
+         case MULTIBOOT_LOAD_PREFERENCE_LOW:
+           preference = GRUB_RELOCATOR_PREFERENCE_LOW;
+           break;
+
+         case MULTIBOOT_LOAD_PREFERENCE_HIGH:
+           preference = GRUB_RELOCATOR_PREFERENCE_HIGH;
+           break;
+
+         default:
+           preference = GRUB_RELOCATOR_PREFERENCE_NONE;
+         }
+       break;
+
        /* GRUB always page-aligns modules.  */
       case MULTIBOOT_HEADER_TAG_MODULE_ALIGN:
        break;
grub-core/loader/multiboot_mbi2.c +13 -4
@@ -260,15 +286,22 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
       else
        code_size = load_size;
 
-      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
-                                            &ch, load_addr,
-                                            code_size);
+      if (relocatable)
+       err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator,
 &ch,
+                                               min_addr, max_addr -
 code_size,
+                                               code_size, align ? align :
 1,
+                                               preference, keep_bs);
+      else
+       err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+                                              &ch, load_addr,
+                                              code_size);
       if (err)
        {
          grub_dprintf ("multiboot_loader", "Error loading aout kludge\n");
          grub_free (buffer);
          return err;
        }
+      base_addr = get_physical_target_address (ch);
       source = get_virtual_current_address (ch);
 
       if ((grub_file_seek (file, offset)) == (grub_off_t) -1)
@@ -290,7 +323,9 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
     }
   else
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer,
+                                    relocatable, min_addr, max_addr,
+                                    align, preference, &base_addr,
 keep_bs);
       if (err)
        {
          grub_free (buffer);
grub-core/loader/multiboot_mbi2.c +0 -0
@@ -303,6 +338,14 @@ grub_multiboot_load (grub_file_t file, const char
 *filename)
   else if (entry_specified)
     grub_multiboot_payload_eip = entry;
 
Quoted 10 lines

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char *filename)

Quoted 9 lines
grub-core/loader/multiboot_mbi2.c +8 -0
+  if (relocatable)
+    {
+      if (base_addr > min_addr)
+       grub_multiboot_payload_eip += base_addr - min_addr;
+      else
+       grub_multiboot_payload_eip -= min_addr - base_addr;
+    }
+
 

Why is it relative to min_addr? Sounds like it should be just an offset

Ugh... IIRC, it has meaning but I forgot what. I will check it. However, this means that I must put comment here.

Quoted 1 line

from base addr. What do ET_DYN files use?

I will take a look.

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#19 lore raw

Re: [GRUB2 PATCH v3 4/4] multiboot2: Add support for relocatable images

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
grub-core/loader/multiboot_mbi2.c +8 -0
+  if (relocatable)
+    {
+      if (base_addr > min_addr)
+       grub_multiboot_payload_eip += base_addr - min_addr;
+      else
+       grub_multiboot_payload_eip -= min_addr - base_addr;
+    }
+
Quoted 5 lines
Quoted 1 line

Why is it relative to min_addr? Sounds like it should be just an offset

Ugh... IIRC, it has meaning but I forgot what. I will check it. However, this means that I must put comment here.

Is it possible that you have confused link address and minimal loading address? How is entry usually specified in ELF? How do you suggest it should be done in mb headers?

Quoted 7 lines
Quoted 2 lines

from base addr. What do ET_DYN files use?

I will take a look.

Daniel

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#23 lore raw

Re: [GRUB2 PATCH v3 4/4] multiboot2: Add support for relocatable images

In reply to Vladimir 'phcoder' Serbinenko
Quoted 31 lines · Vladimir 'phcoder' Serbinenko
Quoted 29 lines · Daniel Kiper

Currently multiboot2 protocol loads image exactly at address specified in ELF or multiboot2 header. This solution works quite well on legacy BIOS platforms. It is possible because memory regions are placed at predictable addresses (though I was not able to find any spec which says that it is strong requirement, so, it looks that it is just a goodwill of hardware designers). However, EFI platforms are more volatile. Even if required memory regions live at specific addresses then they are sometimes simply not free (e.g. used by boot/runtime services on Dell PowerEdge R820 and OVMF). This means that you are not able to simply set up final image destination on build time. You have to provide method to relocate image contents to real load address which is usually different than load address specified in ELF and multiboot2 headers.

This patch provides all needed machinery to do self relocation in image code.
First of all GRUB2 reads min_addr (min. load addr), max_addr (max. load addr),
align (required image alignment), preference (it says which memory regions are
preferred by image, e.g. none, low, high) from multiboot_header_tag_relocatable
header tag contained in binary. Later loader tries to fulfill request (not only
that one) and if it succeeds then it informs image about real load address via
multiboot_tag_base_addr tag. At this stage GRUB2 role is finished. Starting from now executable must cope with relocations itself using whole static and dynamic knowledge provided by boot loader.

Quoted 5 lines
Quoted 2 lines

This patch does not provide functionality which could do relocations using ELF relocation data.

Can you add a check that image doesn't have any relocation entries? So that we fail nicely rather than loading half-working binary?

Make sense. I will do that.

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#28 lore raw
In reply to Vladimir 'phcoder' Serbinenko

On Fri, Mar 11, 2016 at 04:44:00PM +0100, Vladimir 'phcoder' Serbinenko wrote:

[...]

Quoted 1 line

multiboot2 spec is a rolling update.

OK. So, I think that we should do this in that way:
- apply this patch series after polishing; I hope that it will happen next week,
- update multiboot2 doc,
- add grub_relocator32_efi;
should it be 2.02 goal?
- agree and add support for ELF relocations; should it be 2.02 goal?

Does it make sense?

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#6 lore raw

Re: [GRUB2 PATCH v3 1/4] i386/relocator: Add grub_relocator64_efi relocator

In reply to Vladimir 'phcoder' Serbinenko
Quoted 22 lines · Vladimir 'phcoder' Serbinenko
Quoted 20 lines · Daniel Kiper
Quoted 1 line · Vladimir 'phcoder' Serbinenko

On Wednesday, March 2, 2016, Daniel Kiper <[email protected]

Quoted 2 lines · <javascript:;>> wrote:
Quoted 2 lines

Add grub_relocator64_efi relocator. It will be used on EFI 64-bit

platforms

Quoted 5 lines
Quoted 5 lines

when multiboot2 compatible image requests MULTIBOOT_TAG_TYPE_EFI_BS. Relocator
will set lower parts of %rax and %rbx accordingly to multiboot2 specification.
On the other hand processor mode, just before jumping into loaded

image,

Quoted 2 lines
Quoted 2 lines

will
be set accordingly to Unified Extensible Firmware Interface

Specification,

Quoted 1 line
Quoted 1 line

Version 2.4 Errata B, section 2.3.4, x64 Platforms, boot services.

This way

Quoted 2 lines
Quoted 2 lines

loaded image will be able to use EFI boot services without any issues.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

If idea is accepted I will prepare grub_relocator32_efi relocator too.

OK, as I can see idea in general is accepted. Do you want grub_relocator32_efi in 2.02 or 2.03 is OK?

Is there already a user for it? Or someone who is expected to adopt it shortly?

I have not heard anything about that. So, safely we can defer it to 2.03. We should just reserve relevant numbers for required constants and say somewhere that this feature is not implemented yet.

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#29 lore raw
In reply to Daniel Kiper
Quoted 10 lines · Daniel Kiper

On Fri, Mar 11, 2016 at 04:44:00PM +0100, Vladimir 'phcoder' Serbinenko wrote:

[...]

Quoted 1 line

multiboot2 spec is a rolling update.

OK. So, I think that we should do this in that way:
- apply this patch series after polishing; I hope that it will happen next week,

- update multiboot2 doc,

Quoted 3 lines

- add grub_relocator32_efi;
should it be 2.02 goal?

Only if it's small

Quoted 3 lines

- agree and add support for ELF relocations;
should it be 2.02 goal?

Full ELF relocations are definitely not for 2.02

Quoted 3 lines

Does it make sense?

yes

Quoted 3 lines

Daniel

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#30 lore raw
Quoted 22 lines · Vladimir 'phcoder' Serbinenko
Quoted 10 lines · Daniel Kiper

On Fri, Mar 11, 2016 at 04:44:00PM +0100, Vladimir 'phcoder' Serbinenko wrote:

[...]

Quoted 1 line

multiboot2 spec is a rolling update.

OK. So, I think that we should do this in that way:
- apply this patch series after polishing; I hope that it will happen next week,

- update multiboot2 doc,

Quoted 3 lines

- add grub_relocator32_efi;
should it be 2.02 goal?

Only if it's small

Quoted 3 lines

- agree and add support for ELF relocations;
should it be 2.02 goal?

Quoted 6 lines

Full ELF relocations are definitely not for 2.02

Quoted 3 lines

Does it make sense?

yes

Great! Stay tuned... I will polish and repost this patch series next week.

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#20 lore raw

Re: [GRUB2 PATCH v3 4/4] multiboot2: Add support for relocatable images

In reply to Vladimir 'phcoder' Serbinenko
Quoted 9 lines · Vladimir 'phcoder' Serbinenko
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
grub-core/loader/multiboot_mbi2.c +8 -0
+  if (relocatable)
+    {
+      if (base_addr > min_addr)
+       grub_multiboot_payload_eip += base_addr - min_addr;
+      else
+       grub_multiboot_payload_eip -= min_addr - base_addr;
+    }
+
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Why is it relative to min_addr? Sounds like it should be just an offset

Ugh... IIRC, it has meaning but I forgot what. I will check it. However, this means that I must put comment here.

Is it possible that you have confused link address and minimal loading address?

Yep, you are right. Fortunately it is quite easy to fix and probably do not require any changes in Xen image.

Quoted 1 line

How is entry usually specified in ELF?

IIRC, there is no such thing per se. However, I think that we should calculate link base address using following formula:

link_base_addr = ~0;

for (i = 0; i < ehdr->e_phnum; i++)
link_base_addr = min(link_base_addr, phdr(i)->p_paddr);

Quoted 1 line

How do you suggest it should be done in mb headers?

I think that we can use multiboot_header_tag_address.load_addr as link_base_addr.

Daniel

#21 lore raw

Re: [GRUB2 PATCH v3 4/4] multiboot2: Add support for relocatable images

In reply to Vladimir 'phcoder' Serbinenko
Quoted 9 lines · Vladimir 'phcoder' Serbinenko
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
grub-core/loader/multiboot_mbi2.c +8 -0
+  if (relocatable)
+    {
+      if (base_addr > min_addr)
+       grub_multiboot_payload_eip += base_addr - min_addr;
+      else
+       grub_multiboot_payload_eip -= min_addr - base_addr;
+    }
+
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Why is it relative to min_addr? Sounds like it should be just an offset

Ugh... IIRC, it has meaning but I forgot what. I will check it. However, this means that I must put comment here.

Is it possible that you have confused link address and minimal loading address?

Yep, you are right. Fortunately it is quite easy to fix and probably do not require any changes in Xen image.

Quoted 1 line

How is entry usually specified in ELF?

IIRC, there is no such thing per se. However, I think that we should calculate link base address using following formula:

link_base_addr = ~0;

for (i = 0; i < ehdr->e_phnum; i++)
link_base_addr = min(link_base_addr, phdr(i)->p_paddr);

Quoted 1 line

How do you suggest it should be done in mb headers?

I think that we can use multiboot_header_tag_address.load_addr as link_base_addr.

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel