nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0436-util-mkimage-Refactor-section-setup-to-use-a-helper.patch

b1bcb2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b1bcb2
From: Peter Jones <pjones@redhat.com>
b1bcb2
Date: Fri, 26 Feb 2021 01:30:37 +0100
b1bcb2
Subject: [PATCH] util/mkimage: Refactor section setup to use a helper
b1bcb2
b1bcb2
Add a init_pe_section() helper function to setup PE sections. This makes
b1bcb2
the code simpler and easier to read.
b1bcb2
b1bcb2
Signed-off-by: Peter Jones <pjones@redhat.com>
b1bcb2
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
b1bcb2
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
b1bcb2
---
b1bcb2
 util/mkimage.c | 141 +++++++++++++++++++++++++++++++--------------------------
b1bcb2
 1 file changed, 77 insertions(+), 64 deletions(-)
b1bcb2
b1bcb2
diff --git a/util/mkimage.c b/util/mkimage.c
b1bcb2
index f23fceef8f7..938a06a2d3f 100644
b1bcb2
--- a/util/mkimage.c
b1bcb2
+++ b/util/mkimage.c
b1bcb2
@@ -978,6 +978,38 @@ grub_install_get_image_targets_string (void)
b1bcb2
   return formats;
b1bcb2
 }
b1bcb2
 
b1bcb2
+/*
b1bcb2
+ * The image_target parameter is used by the grub_host_to_target32() macro.
b1bcb2
+ */
b1bcb2
+static struct grub_pe32_section_table *
b1bcb2
+init_pe_section(const struct grub_install_image_target_desc *image_target,
b1bcb2
+		struct grub_pe32_section_table *section,
b1bcb2
+		const char * const name,
b1bcb2
+		grub_uint32_t *vma, grub_uint32_t vsz, grub_uint32_t valign,
b1bcb2
+		grub_uint32_t *rda, grub_uint32_t rsz,
b1bcb2
+		grub_uint32_t characteristics)
b1bcb2
+{
b1bcb2
+  size_t len = strlen (name);
b1bcb2
+
b1bcb2
+  if (len > sizeof (section->name))
b1bcb2
+    grub_util_error (_("section name %s length is bigger than %lu"),
b1bcb2
+                    name, (unsigned long) sizeof (section->name));
b1bcb2
+
b1bcb2
+  memcpy (section->name, name, len);
b1bcb2
+
b1bcb2
+  section->virtual_address = grub_host_to_target32 (*vma);
b1bcb2
+  section->virtual_size = grub_host_to_target32 (vsz);
b1bcb2
+  (*vma) = ALIGN_UP (*vma + vsz, valign);
b1bcb2
+
b1bcb2
+  section->raw_data_offset = grub_host_to_target32 (*rda);
b1bcb2
+  section->raw_data_size = grub_host_to_target32 (rsz);
b1bcb2
+  (*rda) = ALIGN_UP (*rda + rsz, GRUB_PE32_FILE_ALIGNMENT);
b1bcb2
+
b1bcb2
+  section->characteristics = grub_host_to_target32 (characteristics);
b1bcb2
+
b1bcb2
+  return section + 1;
b1bcb2
+}
b1bcb2
+
b1bcb2
 /*
b1bcb2
  * tmp_ is just here so the compiler knows we'll never derefernce a NULL.
b1bcb2
  * It should get fully optimized away.
b1bcb2
@@ -1414,17 +1446,13 @@ grub_install_generate_image (const char *dir, const char *prefix,
b1bcb2
       break;
b1bcb2
     case IMAGE_EFI:
b1bcb2
       {
b1bcb2
-	void *pe_img;
b1bcb2
-	grub_uint8_t *header;
b1bcb2
-	void *sections;
b1bcb2
+	char *pe_img, *header;
b1bcb2
+	struct grub_pe32_section_table *section;
b1bcb2
 	size_t scn_size;
b1bcb2
-	size_t pe_size;
b1bcb2
+	grub_uint32_t vma, raw_data;
b1bcb2
+	size_t pe_size, header_size;
b1bcb2
 	struct grub_pe32_coff_header *c;
b1bcb2
-	struct grub_pe32_section_table *text_section, *data_section;
b1bcb2
-	struct grub_pe32_section_table *mods_section, *reloc_section;
b1bcb2
 	static const grub_uint8_t stub[] = GRUB_PE32_MSDOS_STUB;
b1bcb2
-	int header_size;
b1bcb2
-	int reloc_addr;
b1bcb2
 	struct grub_pe32_optional_header *o32 = NULL;
b1bcb2
 	struct grub_pe64_optional_header *o64 = NULL;
b1bcb2
 
b1bcb2
@@ -1433,16 +1461,12 @@ grub_install_generate_image (const char *dir, const char *prefix,
b1bcb2
 	else
b1bcb2
 	  header_size = EFI64_HEADER_SIZE;
b1bcb2
 
b1bcb2
-	reloc_addr = ALIGN_UP (header_size + core_size,
b1bcb2
-			       image_target->section_align);
b1bcb2
+	vma = raw_data = header_size;
b1bcb2
+	pe_size = ALIGN_UP (header_size + core_size, GRUB_PE32_FILE_ALIGNMENT) +
b1bcb2
+          ALIGN_UP (layout.reloc_size, GRUB_PE32_FILE_ALIGNMENT);
b1bcb2
+	header = pe_img = xcalloc (1, pe_size);
b1bcb2
 
b1bcb2
-	pe_size = ALIGN_UP (reloc_addr + reloc_size,
b1bcb2
-			    image_target->section_align);
b1bcb2
-	pe_img = xmalloc (reloc_addr + reloc_size);
b1bcb2
-	memset (pe_img, 0, header_size);
b1bcb2
-	memcpy ((char *) pe_img + header_size, core_img, core_size);
b1bcb2
-	memcpy ((char *) pe_img + reloc_addr, rel_section, reloc_size);
b1bcb2
-	header = pe_img;
b1bcb2
+	memcpy (pe_img + raw_data, core_img, core_size);
b1bcb2
 
b1bcb2
 	/* The magic.  */
b1bcb2
 	memcpy (header, stub, GRUB_PE32_MSDOS_STUB_SIZE);
b1bcb2
@@ -1475,18 +1499,17 @@ grub_install_generate_image (const char *dir, const char *prefix,
b1bcb2
 	    o32->magic = grub_host_to_target16 (GRUB_PE32_PE32_MAGIC);
b1bcb2
 	    o32->data_base = grub_host_to_target32 (header_size + exec_size);
b1bcb2
 
b1bcb2
-	    sections = o32 + 1;
b1bcb2
+	    section = (struct grub_pe32_section_table *)(o32 + 1);
b1bcb2
 	  }
b1bcb2
 	else
b1bcb2
 	  {
b1bcb2
 	    c->optional_header_size = grub_host_to_target16 (sizeof (struct grub_pe64_optional_header));
b1bcb2
-
b1bcb2
 	    o64 = (struct grub_pe64_optional_header *)
b1bcb2
 	      (header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE +
b1bcb2
 	       sizeof (struct grub_pe32_coff_header));
b1bcb2
 	    o64->magic = grub_host_to_target16 (GRUB_PE32_PE64_MAGIC);
b1bcb2
 
b1bcb2
-	    sections = o64 + 1;
b1bcb2
+	    section = (struct grub_pe32_section_table *)(o64 + 1);
b1bcb2
 	  }
b1bcb2
 
b1bcb2
 	PE_OHDR (o32, o64, header_size) = grub_host_to_target32 (header_size);
b1bcb2
@@ -1507,58 +1530,48 @@ grub_install_generate_image (const char *dir, const char *prefix,
b1bcb2
 	PE_OHDR (o32, o64, num_data_directories) = grub_host_to_target32 (GRUB_PE32_NUM_DATA_DIRECTORIES);
b1bcb2
 
b1bcb2
 	/* The sections.  */
b1bcb2
-	PE_OHDR (o32, o64, code_base) = grub_host_to_target32 (header_size);
b1bcb2
+	PE_OHDR (o32, o64, code_base) = grub_host_to_target32 (vma);
b1bcb2
 	PE_OHDR (o32, o64, code_size) = grub_host_to_target32 (exec_size);
b1bcb2
-	text_section = sections;
b1bcb2
-	strcpy (text_section->name, ".text");
b1bcb2
-	text_section->virtual_size = grub_host_to_target32 (exec_size);
b1bcb2
-	text_section->virtual_address = grub_host_to_target32 (header_size);
b1bcb2
-	text_section->raw_data_size = grub_host_to_target32 (exec_size);
b1bcb2
-	text_section->raw_data_offset = grub_host_to_target32 (header_size);
b1bcb2
-	text_section->characteristics = grub_cpu_to_le32_compile_time (
b1bcb2
-						  GRUB_PE32_SCN_CNT_CODE
b1bcb2
-						| GRUB_PE32_SCN_MEM_EXECUTE
b1bcb2
-						| GRUB_PE32_SCN_MEM_READ);
b1bcb2
+
b1bcb2
+	section = init_pe_section (image_target, section, ".text",
b1bcb2
+				   &vma, exec_size,
b1bcb2
+				   image_target->section_align,
b1bcb2
+				   &raw_data, exec_size,
b1bcb2
+				   GRUB_PE32_SCN_CNT_CODE |
b1bcb2
+				   GRUB_PE32_SCN_MEM_EXECUTE |
b1bcb2
+				   GRUB_PE32_SCN_MEM_READ);
b1bcb2
 
b1bcb2
 	scn_size = ALIGN_UP (kernel_size - exec_size, GRUB_PE32_FILE_ALIGNMENT);
b1bcb2
 	PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (scn_size +
b1bcb2
 							       ALIGN_UP (total_module_size,
b1bcb2
 
b1bcb2
 
b1bcb2
-	data_section = text_section + 1;
b1bcb2
-	strcpy (data_section->name, ".data");
b1bcb2
-	data_section->virtual_size = grub_host_to_target32 (kernel_size - exec_size);
b1bcb2
-	data_section->virtual_address = grub_host_to_target32 (header_size + exec_size);
b1bcb2
-	data_section->raw_data_size = grub_host_to_target32 (kernel_size - exec_size);
b1bcb2
-	data_section->raw_data_offset = grub_host_to_target32 (header_size + exec_size);
b1bcb2
-	data_section->characteristics
b1bcb2
-	  = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
b1bcb2
-			      | GRUB_PE32_SCN_MEM_READ
b1bcb2
-			      | GRUB_PE32_SCN_MEM_WRITE);
b1bcb2
-    
b1bcb2
-	mods_section = data_section + 1;
b1bcb2
-	strcpy (mods_section->name, "mods");
b1bcb2
-	mods_section->virtual_size = grub_host_to_target32 (reloc_addr - kernel_size - header_size);
b1bcb2
-	mods_section->virtual_address = grub_host_to_target32 (header_size + kernel_size + bss_size);
b1bcb2
-	mods_section->raw_data_size = grub_host_to_target32 (reloc_addr - kernel_size - header_size);
b1bcb2
-	mods_section->raw_data_offset = grub_host_to_target32 (header_size + kernel_size);
b1bcb2
-	mods_section->characteristics
b1bcb2
-	  = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
b1bcb2
-			      | GRUB_PE32_SCN_MEM_READ
b1bcb2
-			      | GRUB_PE32_SCN_MEM_WRITE);
b1bcb2
+	section = init_pe_section (image_target, section, ".data",
b1bcb2
+				   &vma, scn_size, image_target->section_align,
b1bcb2
+				   &raw_data, scn_size,
b1bcb2
+				   GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
b1bcb2
+				   GRUB_PE32_SCN_MEM_READ |
b1bcb2
+				   GRUB_PE32_SCN_MEM_WRITE);
b1bcb2
+
b1bcb2
+	scn_size = pe_size - reloc_size - raw_data;
b1bcb2
+	section = init_pe_section (image_target, section, "mods",
b1bcb2
+				   &vma, scn_size, image_target->section_align,
b1bcb2
+				   &raw_data, scn_size,
b1bcb2
+				   GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
b1bcb2
+				   GRUB_PE32_SCN_MEM_READ |
b1bcb2
+				   GRUB_PE32_SCN_MEM_WRITE);
b1bcb2
+
b1bcb2
+	scn_size = reloc_size;
b1bcb2
+	PE_OHDR (o32, o64, base_relocation_table.rva) = grub_host_to_target32 (vma);
b1bcb2
+	PE_OHDR (o32, o64, base_relocation_table.size) = grub_host_to_target32 (scn_size);
b1bcb2
+	memcpy (pe_img + raw_data, rel_section, scn_size);
b1bcb2
+	init_pe_section (image_target, section, ".reloc",
b1bcb2
+			 &vma, scn_size, image_target->section_align,
b1bcb2
+			 &raw_data, scn_size,
b1bcb2
+			 GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
b1bcb2
+			 GRUB_PE32_SCN_MEM_DISCARDABLE |
b1bcb2
+			 GRUB_PE32_SCN_MEM_READ);
b1bcb2
 
b1bcb2
-	PE_OHDR (o32, o64, base_relocation_table.rva) = grub_host_to_target32 (reloc_addr);
b1bcb2
-	PE_OHDR (o32, o64, base_relocation_table.size) = grub_host_to_target32 (reloc_size);
b1bcb2
-	reloc_section = mods_section + 1;
b1bcb2
-	strcpy (reloc_section->name, ".reloc");
b1bcb2
-	reloc_section->virtual_size = grub_host_to_target32 (reloc_size);
b1bcb2
-	reloc_section->virtual_address = grub_host_to_target32 (reloc_addr + bss_size);
b1bcb2
-	reloc_section->raw_data_size = grub_host_to_target32 (reloc_size);
b1bcb2
-	reloc_section->raw_data_offset = grub_host_to_target32 (reloc_addr);
b1bcb2
-	reloc_section->characteristics
b1bcb2
-	  = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
b1bcb2
-			      | GRUB_PE32_SCN_MEM_DISCARDABLE
b1bcb2
-			      | GRUB_PE32_SCN_MEM_READ);
b1bcb2
 	free (core_img);
b1bcb2
 	core_img = pe_img;
b1bcb2
 	core_size = pe_size;