On Sat, 2022-03-12 at 09:34 +0200, Timo Lindfors wrote:
> On Fri, 11 Mar 2022, Łukasz Hawryłko wrote:
> > I see that you have quite complex environment for testing tboot, if I
> > find my old GRUB patch and prepare patch for tboot that combined should
> > fix the issue, will you be able to run tests?
> 
> Yes, I'm happy to run tests :)

I am attaching two patches:

GRUB:
multiboot2__Implement_the_new_module_load_and_preferences_tag.patch

tboot:
Use_multiboot2_module_load_preference_tag.patch

On my test platform they fix the issue. Please check how they work on
your environment.

Thanks,
Lukasz
# HG changeset patch
# User Zide Chen <zide.c...@intel.com>
# Date 1629108172 -7200
#      Mon Aug 16 12:02:52 2021 +0200
# Node ID e774651a58f48e6f816c55ed4bf076043aa12a78
# Parent  30c0c2ae5167bcbfb867d6a35b2295662117fcaa
multiboot2: Implement the new module load and preferences tag
In contrast to Mulitboot, in Mulitboot2, there is currently no way to
control where to load the modules to. This could be a problem, e.g., the
OS loaded by Multiboot2 needs to reserve low address for some particular
purposes and not for loading modules.

This new tag gives it flexibility to control the modules load addresses,
which is independent to whether the kernel relocatable tag is present or
not.

Signed-off-by: Zide Chen <zide.c...@intel.com>
Signed-off-by: Lukasz Hawrylko <luk...@hawrylko.pl>

diff -r 30c0c2ae5167 -r e774651a58f4 grub-core/loader/multiboot.c
--- a/grub-core/loader/multiboot.c	Wed Mar 09 15:31:51 2022 -0500
+++ b/grub-core/loader/multiboot.c	Mon Aug 16 12:02:52 2021 +0200
@@ -363,12 +363,14 @@
 		 int argc, char *argv[])
 {
   grub_file_t file = 0;
-  grub_ssize_t size;
+  grub_size_t size;
   void *module = NULL;
   grub_addr_t target;
   grub_err_t err;
   int nounzip = 0;
-  grub_uint64_t lowest_addr = 0;
+  grub_uint64_t lowest_addr = 0, max_addr;
+  grub_size_t align = MULTIBOOT_MOD_ALIGN;
+  int preference = GRUB_RELOCATOR_PREFERENCE_NONE;
 
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
@@ -401,11 +403,31 @@
   size = grub_file_size (file);
   if (size)
   {
+    max_addr = UP_TO_TOP32 (size);
+
+#ifdef GRUB_USE_MULTIBOOT2
+    if (grub_multiboot2_mlp.relocatable)
+      {
+        lowest_addr = grub_multiboot2_mlp.min_addr;
+        max_addr = grub_multiboot2_mlp.max_addr;
+        preference = grub_multiboot2_mlp.preference;
+
+        if (grub_multiboot2_mlp.align)
+          align = grub_max (grub_multiboot2_mlp.align, align);
+
+        if (size > max_addr || lowest_addr > max_addr - size)
+          {
+            grub_file_close (file);
+            return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid min/max address and/or load size for modules");
+          }
+      }
+#endif
+
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_align (GRUB_MULTIBOOT (relocator), &ch,
-					    lowest_addr, UP_TO_TOP32 (size),
-					    size, MULTIBOOT_MOD_ALIGN,
-					    GRUB_RELOCATOR_PREFERENCE_NONE, 1);
+					    lowest_addr, max_addr,
+					    size, align,
+					    preference, 1);
     if (err)
       {
 	grub_file_close (file);
@@ -427,7 +449,7 @@
       return err;
     }
 
-  if (size && grub_file_read (file, module, size) != size)
+  if (size && grub_file_read (file, module, size) != (grub_ssize_t)size)
     {
       grub_file_close (file);
       if (!grub_errno)
diff -r 30c0c2ae5167 -r e774651a58f4 grub-core/loader/multiboot_mbi2.c
--- a/grub-core/loader/multiboot_mbi2.c	Wed Mar 09 15:31:51 2022 -0500
+++ b/grub-core/loader/multiboot_mbi2.c	Mon Aug 16 12:02:52 2021 +0200
@@ -75,6 +75,7 @@
 static void *elf_sections;
 static int keep_bs = 0;
 static grub_uint32_t load_base_addr;
+struct module_load_preferences grub_multiboot2_mlp;
 
 void
 grub_multiboot2_add_elfsyms (grub_size_t num, grub_size_t entsize,
@@ -114,6 +115,7 @@
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
   struct multiboot_header_tag_relocatable *rel_tag;
+  struct multiboot_header_tag_module_load_preferences *mod_load_tag;
   int entry_specified = 0, efi_entry_specified = 0;
   grub_addr_t entry = 0, efi_entry = 0;
   grub_uint32_t console_required = 0;
@@ -123,6 +125,7 @@
 
   mld.mbi_ver = 2;
   mld.relocatable = 0;
+  grub_multiboot2_mlp.relocatable = 0;
 
   mld.buffer = grub_malloc (MULTIBOOT_SEARCH);
   if (!mld.buffer)
@@ -248,6 +251,27 @@
 	  }
 	break;
 
+      case MULTIBOOT_HEADER_TAG_MODULE_LOAD_PREFERENCES:
+	grub_multiboot2_mlp.relocatable = 1;
+	mod_load_tag = (struct multiboot_header_tag_module_load_preferences *) tag;
+	grub_multiboot2_mlp.min_addr = mod_load_tag->min_addr;
+	grub_multiboot2_mlp.max_addr = mod_load_tag->max_addr;
+	grub_multiboot2_mlp.align = mod_load_tag->align;
+	switch (mod_load_tag->preference)
+	  {
+	  case MULTIBOOT_LOAD_PREFERENCE_LOW:
+	    grub_multiboot2_mlp.preference = GRUB_RELOCATOR_PREFERENCE_LOW;
+	    break;
+
+	  case MULTIBOOT_LOAD_PREFERENCE_HIGH:
+	    grub_multiboot2_mlp.preference = GRUB_RELOCATOR_PREFERENCE_HIGH;
+	    break;
+
+	  default:
+	    grub_multiboot2_mlp.preference = GRUB_RELOCATOR_PREFERENCE_NONE;
+	  }
+	break;
+
 	/* GRUB always page-aligns modules.  */
       case MULTIBOOT_HEADER_TAG_MODULE_ALIGN:
 	break;
diff -r 30c0c2ae5167 -r e774651a58f4 include/grub/multiboot2.h
--- a/include/grub/multiboot2.h	Wed Mar 09 15:31:51 2022 -0500
+++ b/include/grub/multiboot2.h	Mon Aug 16 12:02:52 2021 +0200
@@ -92,6 +92,15 @@
 };
 typedef struct mbi_load_data mbi_load_data_t;
 
+struct module_load_preferences
+{
+  int relocatable;
+  grub_uint32_t min_addr;
+  grub_uint32_t max_addr;
+  grub_uint32_t align;
+  grub_uint32_t preference;
+};
+
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot2_load_elf (mbi_load_data_t *mld);
@@ -99,6 +108,7 @@
 extern grub_size_t grub_multiboot2_pure_size;
 extern grub_size_t grub_multiboot2_alloc_mbi;
 extern grub_uint32_t grub_multiboot2_payload_eip;
+extern struct module_load_preferences grub_multiboot2_mlp;
 
 
 #endif /* ! GRUB_MULTIBOOT_HEADER */
diff -r 30c0c2ae5167 -r e774651a58f4 include/multiboot2.h
--- a/include/multiboot2.h	Wed Mar 09 15:31:51 2022 -0500
+++ b/include/multiboot2.h	Mon Aug 16 12:02:52 2021 +0200
@@ -74,6 +74,7 @@
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
 #define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
 #define MULTIBOOT_HEADER_TAG_RELOCATABLE  10
+#define MULTIBOOT_HEADER_TAG_MODULE_LOAD_PREFERENCES  11
 
 #define MULTIBOOT2_ARCHITECTURE_I386  0
 #define MULTIBOOT2_ARCHITECTURE_MIPS32  4
@@ -178,6 +179,17 @@
   multiboot_uint32_t preference;
 };
 
+struct multiboot_header_tag_module_load_preferences
+{
+  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;
# HG changeset patch
# User Łukasz Hawryłko <luk...@hawrylko.pl>
# Date 1629375460 -7200
#      Thu Aug 19 14:17:40 2021 +0200
# Node ID b8d18df5adc26c39a8cbfc5b6babe946b762e741
# Parent  8ab1cfa6301f1f892d7d8e82532c6609e136765c
Use multiboot2 module load preference tag

This is new a tag added recently to GRUB that allows to specify memory region
where GRUB should load multiboot2 modules. Thanks to this, TBOOT can now be sure
that none of the modules overlaps memory intended for memory log.

This patch, together with new GRUB version, fixes issue when multiple SINITs
has been loaded in multiboot2 and some of them may get overwritten by TBOOT's
memory log.

Signed-off-by: Lukasz Hawrylko <luk...@hawrylko.pl>

diff -r 8ab1cfa6301f -r b8d18df5adc2 tboot/common/boot.S
--- a/tboot/common/boot.S	Sun Dec 12 01:55:53 2021 +0100
+++ b/tboot/common/boot.S	Thu Aug 19 14:17:40 2021 +0200
@@ -113,6 +113,13 @@
                    FB_MAX_VRES,   /* height */ \
                    FB_BPP         /* depth */
 
+        /* Module load preference tag */
+        mb2ht_init MB2_HDR_TAG_MOD_LOAD_PREFERENCE, MB2_HDR_TAG_OPTIONAL, \
+                   0x900000,      /* min_addr */ \
+                   0xFFFFFFFF,    /* max_addr */ \
+                   0x1000,        /* align */ \
+                   0              /* preference */
+
         /* Multiboot2 header end tag. */
         mb2ht_init MB2_HDR_TAG_END, MB2_HDR_TAG_REQUIRED
 
diff -r 8ab1cfa6301f -r b8d18df5adc2 tboot/include/multiboot.h
--- a/tboot/include/multiboot.h	Sun Dec 12 01:55:53 2021 +0100
+++ b/tboot/include/multiboot.h	Thu Aug 19 14:17:40 2021 +0200
@@ -77,6 +77,7 @@
 #define MB2_HDR_TAG_CONSOLE_FLAGS	  4
 #define MB2_HDR_TAG_FRAMEBUFFER		  5
 #define MB2_HDR_TAG_MOD_ALIGN		  6
+#define MB2_HDR_TAG_MOD_LOAD_PREFERENCE 11
 
 #define MB2_HDR_TAG_REQUIRED      0
 #define MB2_HDR_TAG_OPTIONAL		  1
_______________________________________________
tboot-devel mailing list
tboot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tboot-devel

Reply via email to