Blame SOURCES/0435-util-mkimage-Improve-data_size-value-calculation.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:27:42 +0100
b1bcb2
Subject: [PATCH] util/mkimage: Improve data_size value calculation
b1bcb2
b1bcb2
According to "Microsoft Portable Executable and Common Object File Format
b1bcb2
Specification", the Optional Header SizeOfInitializedData field contains:
b1bcb2
b1bcb2
  Size of the initialized data section, or the sum of all such sections if
b1bcb2
  there are multiple data sections.
b1bcb2
b1bcb2
Make this explicit by adding the GRUB kernel data size to the sum of all
b1bcb2
the modules sizes. The ALIGN_UP() is not required by the PE spec but do
b1bcb2
it to avoid alignment issues.
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 | 6 +++++-
b1bcb2
 1 file changed, 5 insertions(+), 1 deletion(-)
b1bcb2
b1bcb2
diff --git a/util/mkimage.c b/util/mkimage.c
b1bcb2
index ca88b0ff96a..f23fceef8f7 100644
b1bcb2
--- a/util/mkimage.c
b1bcb2
+++ b/util/mkimage.c
b1bcb2
@@ -1417,6 +1417,7 @@ grub_install_generate_image (const char *dir, const char *prefix,
b1bcb2
 	void *pe_img;
b1bcb2
 	grub_uint8_t *header;
b1bcb2
 	void *sections;
b1bcb2
+	size_t scn_size;
b1bcb2
 	size_t pe_size;
b1bcb2
 	struct grub_pe32_coff_header *c;
b1bcb2
 	struct grub_pe32_section_table *text_section, *data_section;
b1bcb2
@@ -1519,7 +1520,10 @@ grub_install_generate_image (const char *dir, const char *prefix,
b1bcb2
 						| GRUB_PE32_SCN_MEM_EXECUTE
b1bcb2
 						| GRUB_PE32_SCN_MEM_READ);
b1bcb2
 
b1bcb2
-	PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (reloc_addr - exec_size - header_size);
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");