1From 1710452aca05ccdd21e74390ec08c63fdf0ee10a Mon Sep 17 00:00:00 2001
2From: Peter Jones <pjones@redhat.com>
3Date: Mon, 15 Feb 2021 13:59:21 +0100
4Subject: [PATCH] util/mkimage: Use grub_host_to_target32() instead of
5 grub_cpu_to_le32()
6
7The latter doesn't take into account the target image endianness. There is
8a grub_cpu_to_le32_compile_time() but no compile time variant for function
9grub_host_to_target32(). So, let's keep using the other one for this case.
10
11Signed-off-by: Peter Jones <pjones@redhat.com>
12Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
13Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
14Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
15---
16 util/mkimage.c | 44 ++++++++++++++++++++++----------------------
17 1 file changed, 22 insertions(+), 22 deletions(-)
18
19diff --git a/util/mkimage.c b/util/mkimage.c
20index 32bb8ea..02944f2 100644
21--- a/util/mkimage.c
22+++ b/util/mkimage.c
23@@ -1302,10 +1302,10 @@ grub_install_generate_image (const char *dir, const char *prefix,
24 	       + sizeof (struct grub_pe32_coff_header));
25 	    o->magic = grub_host_to_target16 (GRUB_PE32_PE32_MAGIC);
26 	    o->code_size = grub_host_to_target32 (layout.exec_size);
27-	    o->data_size = grub_cpu_to_le32 (reloc_addr - layout.exec_size
28+	    o->data_size = grub_host_to_target32 (reloc_addr - layout.exec_size
29 					     - header_size);
30-	    o->entry_addr = grub_cpu_to_le32 (layout.start_address);
31-	    o->code_base = grub_cpu_to_le32 (header_size);
32+	    o->entry_addr = grub_host_to_target32 (layout.start_address);
33+	    o->code_base = grub_host_to_target32 (header_size);
34
35 	    o->data_base = grub_host_to_target32 (header_size + layout.exec_size);
36
37@@ -1339,10 +1339,10 @@ grub_install_generate_image (const char *dir, const char *prefix,
38 	       + sizeof (struct grub_pe32_coff_header));
39 	    o->magic = grub_host_to_target16 (GRUB_PE32_PE64_MAGIC);
40 	    o->code_size = grub_host_to_target32 (layout.exec_size);
41-	    o->data_size = grub_cpu_to_le32 (reloc_addr - layout.exec_size
42+	    o->data_size = grub_host_to_target32 (reloc_addr - layout.exec_size
43 					     - header_size);
44-	    o->entry_addr = grub_cpu_to_le32 (layout.start_address);
45-	    o->code_base = grub_cpu_to_le32 (header_size);
46+	    o->entry_addr = grub_host_to_target32 (layout.start_address);
47+	    o->code_base = grub_host_to_target32 (header_size);
48 	    o->image_base = 0;
49 	    o->section_alignment = grub_host_to_target32 (image_target->section_align);
50 	    o->file_alignment = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);
51@@ -1366,10 +1366,10 @@ grub_install_generate_image (const char *dir, const char *prefix,
52 	/* The sections.  */
53 	text_section = sections;
54 	strcpy (text_section->name, ".text");
55-	text_section->virtual_size = grub_cpu_to_le32 (layout.exec_size);
56-	text_section->virtual_address = grub_cpu_to_le32 (header_size);
57-	text_section->raw_data_size = grub_cpu_to_le32 (layout.exec_size);
58-	text_section->raw_data_offset = grub_cpu_to_le32 (header_size);
59+	text_section->virtual_size = grub_host_to_target32 (layout.exec_size);
60+	text_section->virtual_address = grub_host_to_target32 (header_size);
61+	text_section->raw_data_size = grub_host_to_target32 (layout.exec_size);
62+	text_section->raw_data_offset = grub_host_to_target32 (header_size);
63 	text_section->characteristics = grub_cpu_to_le32_compile_time (
64 						  GRUB_PE32_SCN_CNT_CODE
65 						| GRUB_PE32_SCN_MEM_EXECUTE
66@@ -1377,10 +1377,10 @@ grub_install_generate_image (const char *dir, const char *prefix,
67
68 	data_section = text_section + 1;
69 	strcpy (data_section->name, ".data");
70-	data_section->virtual_size = grub_cpu_to_le32 (layout.kernel_size - layout.exec_size);
71-	data_section->virtual_address = grub_cpu_to_le32 (header_size + layout.exec_size);
72-	data_section->raw_data_size = grub_cpu_to_le32 (layout.kernel_size - layout.exec_size);
73-	data_section->raw_data_offset = grub_cpu_to_le32 (header_size + layout.exec_size);
74+	data_section->virtual_size = grub_host_to_target32 (layout.kernel_size - layout.exec_size);
75+	data_section->virtual_address = grub_host_to_target32 (header_size + layout.exec_size);
76+	data_section->raw_data_size = grub_host_to_target32 (layout.kernel_size - layout.exec_size);
77+	data_section->raw_data_offset = grub_host_to_target32 (header_size + layout.exec_size);
78 	data_section->characteristics
79 	  = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
80 			      | GRUB_PE32_SCN_MEM_READ
81@@ -1388,10 +1388,10 @@ grub_install_generate_image (const char *dir, const char *prefix,
82
83 	mods_section = data_section + 1;
84 	strcpy (mods_section->name, "mods");
85-	mods_section->virtual_size = grub_cpu_to_le32 (reloc_addr - layout.kernel_size - header_size);
86-	mods_section->virtual_address = grub_cpu_to_le32 (header_size + layout.kernel_size + layout.bss_size);
87-	mods_section->raw_data_size = grub_cpu_to_le32 (reloc_addr - layout.kernel_size - header_size);
88-	mods_section->raw_data_offset = grub_cpu_to_le32 (header_size + layout.kernel_size);
89+	mods_section->virtual_size = grub_host_to_target32 (reloc_addr - layout.kernel_size - header_size);
90+	mods_section->virtual_address = grub_host_to_target32 (header_size + layout.kernel_size + layout.bss_size);
91+	mods_section->raw_data_size = grub_host_to_target32 (reloc_addr - layout.kernel_size - header_size);
92+	mods_section->raw_data_offset = grub_host_to_target32 (header_size + layout.kernel_size);
93 	mods_section->characteristics
94 	  = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
95 			      | GRUB_PE32_SCN_MEM_READ
96@@ -1399,10 +1399,10 @@ grub_install_generate_image (const char *dir, const char *prefix,
97
98 	reloc_section = mods_section + 1;
99 	strcpy (reloc_section->name, ".reloc");
100-	reloc_section->virtual_size = grub_cpu_to_le32 (layout.reloc_size);
101-	reloc_section->virtual_address = grub_cpu_to_le32 (reloc_addr + layout.bss_size);
102-	reloc_section->raw_data_size = grub_cpu_to_le32 (layout.reloc_size);
103-	reloc_section->raw_data_offset = grub_cpu_to_le32 (reloc_addr);
104+	reloc_section->virtual_size = grub_host_to_target32 (layout.reloc_size);
105+	reloc_section->virtual_address = grub_host_to_target32 (reloc_addr + layout.bss_size);
106+	reloc_section->raw_data_size = grub_host_to_target32 (layout.reloc_size);
107+	reloc_section->raw_data_offset = grub_host_to_target32 (reloc_addr);
108 	reloc_section->characteristics
109 	  = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
110 			      | GRUB_PE32_SCN_MEM_DISCARDABLE
111--
1122.14.2
113
114