1From f60ba9e5945892e835e53f0619406d96002f7f70 Mon Sep 17 00:00:00 2001
2From: Peter Jones <pjones@redhat.com>
3Date: Mon, 15 Feb 2021 14:58:06 +0100
4Subject: [PATCH] util/mkimage: Refactor section setup to use a helper
5
6Add a init_pe_section() helper function to setup PE sections. This makes
7the code simpler and easier to read.
8
9Signed-off-by: Peter Jones <pjones@redhat.com>
10Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
11Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
12Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
13---
14 util/mkimage.c | 143 +++++++++++++++++++++++++++++++--------------------------
15 1 file changed, 77 insertions(+), 66 deletions(-)
16
17diff --git a/util/mkimage.c b/util/mkimage.c
18index 853a521..8b475a6 100644
19--- a/util/mkimage.c
20+++ b/util/mkimage.c
21@@ -816,6 +816,38 @@ grub_install_get_image_targets_string (void)
22   return formats;
23 }
24
25+/*
26+ * The image_target parameter is used by the grub_host_to_target32() macro.
27+ */
28+static struct grub_pe32_section_table *
29+init_pe_section(const struct grub_install_image_target_desc *image_target,
30+		struct grub_pe32_section_table *section,
31+		const char * const name,
32+		grub_uint32_t *vma, grub_uint32_t vsz, grub_uint32_t valign,
33+		grub_uint32_t *rda, grub_uint32_t rsz,
34+		grub_uint32_t characteristics)
35+{
36+  size_t len = strlen (name);
37+
38+  if (len > sizeof (section->name))
39+    grub_util_error (_("section name %s length is bigger than %lu"),
40+		     name, (unsigned long) sizeof (section->name));
41+
42+  memcpy (section->name, name, len);
43+
44+  section->virtual_address = grub_host_to_target32 (*vma);
45+  section->virtual_size = grub_host_to_target32 (vsz);
46+  (*vma) = ALIGN_UP (*vma + vsz, valign);
47+
48+  section->raw_data_offset = grub_host_to_target32 (*rda);
49+  section->raw_data_size = grub_host_to_target32 (rsz);
50+  (*rda) = ALIGN_UP (*rda + rsz, GRUB_PE32_FILE_ALIGNMENT);
51+
52+  section->characteristics = grub_host_to_target32 (characteristics);
53+
54+  return section + 1;
55+}
56+
57 /*
58  * tmp_ is just here so the compiler knows we'll never derefernce a NULL.
59  * It should get fully optimized away.
60@@ -1257,17 +1289,13 @@ grub_install_generate_image (const char *dir, const char *prefix,
61       break;
62     case IMAGE_EFI:
63       {
64-	void *pe_img;
65-	grub_uint8_t *header;
66-	void *sections;
67+	char *pe_img, *header;
68+	struct grub_pe32_section_table *section;
69 	size_t scn_size;
70-	size_t pe_size;
71+	grub_uint32_t vma, raw_data;
72+	size_t pe_size, header_size;
73 	struct grub_pe32_coff_header *c;
74-	struct grub_pe32_section_table *text_section, *data_section;
75-	struct grub_pe32_section_table *mods_section, *reloc_section;
76 	static const grub_uint8_t stub[] = GRUB_PE32_MSDOS_STUB;
77-	int header_size;
78-	int reloc_addr;
79 	struct grub_pe32_optional_header *o32 = NULL;
80 	struct grub_pe64_optional_header *o64 = NULL;
81
82@@ -1276,17 +1304,12 @@ grub_install_generate_image (const char *dir, const char *prefix,
83 	else
84 	  header_size = EFI64_HEADER_SIZE;
85
86-	reloc_addr = ALIGN_UP (header_size + core_size,
87-			       GRUB_PE32_FILE_ALIGNMENT);
88+	vma = raw_data = header_size;
89+	pe_size = ALIGN_UP (header_size + core_size, GRUB_PE32_FILE_ALIGNMENT) +
90+          ALIGN_UP (layout.reloc_size, GRUB_PE32_FILE_ALIGNMENT);
91+	header = pe_img = xcalloc (1, pe_size);
92
93-	pe_size = ALIGN_UP (reloc_addr + layout.reloc_size,
94-			    GRUB_PE32_FILE_ALIGNMENT);
95-	pe_img = xmalloc (reloc_addr + layout.reloc_size);
96-	memset (pe_img, 0, header_size);
97-	memcpy ((char *) pe_img + header_size, core_img, core_size);
98-	memset ((char *) pe_img + header_size + core_size, 0, reloc_addr - (header_size + core_size));
99-	memcpy ((char *) pe_img + reloc_addr, layout.reloc_section, layout.reloc_size);
100-	header = pe_img;
101+	memcpy (pe_img + raw_data, core_img, core_size);
102
103 	/* The magic.  */
104 	memcpy (header, stub, GRUB_PE32_MSDOS_STUB_SIZE);
105@@ -1319,18 +1342,17 @@ grub_install_generate_image (const char *dir, const char *prefix,
106 	    o32->magic = grub_host_to_target16 (GRUB_PE32_PE32_MAGIC);
107 	    o32->data_base = grub_host_to_target32 (header_size + layout.exec_size);
108
109-	    sections = o32 + 1;
110+	    section = (struct grub_pe32_section_table *)(o32 + 1);
111 	  }
112 	else
113 	  {
114 	    c->optional_header_size = grub_host_to_target16 (sizeof (struct grub_pe64_optional_header));
115-
116 	    o64 = (struct grub_pe64_optional_header *)
117 		  (header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE +
118                    sizeof (struct grub_pe32_coff_header));
119 	    o64->magic = grub_host_to_target16 (GRUB_PE32_PE64_MAGIC);
120
121-	    sections = o64 + 1;
122+	    section = (struct grub_pe32_section_table *)(o64 + 1);
123 	  }
124
125 	PE_OHDR (o32, o64, header_size) = grub_host_to_target32 (header_size);
126@@ -1350,58 +1372,47 @@ grub_install_generate_image (const char *dir, const char *prefix,
127 	PE_OHDR (o32, o64, num_data_directories) = grub_host_to_target32 (GRUB_PE32_NUM_DATA_DIRECTORIES);
128
129 	/* The sections.  */
130-	PE_OHDR (o32, o64, code_base) = grub_host_to_target32 (header_size);
131+	PE_OHDR (o32, o64, code_base) = grub_host_to_target32 (vma);
132 	PE_OHDR (o32, o64, code_size) = grub_host_to_target32 (layout.exec_size);
133-	text_section = sections;
134-	strcpy (text_section->name, ".text");
135-	text_section->virtual_size = grub_host_to_target32 (layout.exec_size);
136-	text_section->virtual_address = grub_host_to_target32 (header_size);
137-	text_section->raw_data_size = grub_host_to_target32 (layout.exec_size);
138-	text_section->raw_data_offset = grub_host_to_target32 (header_size);
139-	text_section->characteristics = grub_cpu_to_le32_compile_time (
140-						  GRUB_PE32_SCN_CNT_CODE
141-						| GRUB_PE32_SCN_MEM_EXECUTE
142-						| GRUB_PE32_SCN_MEM_READ);
143+	section = init_pe_section (image_target, section, ".text",
144+				   &vma, layout.exec_size,
145+				   image_target->section_align,
146+				   &raw_data, layout.exec_size,
147+				   GRUB_PE32_SCN_CNT_CODE |
148+				   GRUB_PE32_SCN_MEM_EXECUTE |
149+				   GRUB_PE32_SCN_MEM_READ);
150
151 	scn_size = ALIGN_UP (layout.kernel_size - layout.exec_size, GRUB_PE32_FILE_ALIGNMENT);
152 	PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (scn_size +
153 							       ALIGN_UP (total_module_size,
154 									 GRUB_PE32_FILE_ALIGNMENT));
155
156-	data_section = text_section + 1;
157-	strcpy (data_section->name, ".data");
158-	data_section->virtual_size = grub_host_to_target32 (layout.kernel_size - layout.exec_size);
159-	data_section->virtual_address = grub_host_to_target32 (header_size + layout.exec_size);
160-	data_section->raw_data_size = grub_host_to_target32 (layout.kernel_size - layout.exec_size);
161-	data_section->raw_data_offset = grub_host_to_target32 (header_size + layout.exec_size);
162-	data_section->characteristics
163-	  = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
164-			      | GRUB_PE32_SCN_MEM_READ
165-			      | GRUB_PE32_SCN_MEM_WRITE);
166-
167-	mods_section = data_section + 1;
168-	strcpy (mods_section->name, "mods");
169-	mods_section->virtual_size = grub_host_to_target32 (reloc_addr - layout.kernel_size - header_size);
170-	mods_section->virtual_address = grub_host_to_target32 (header_size + layout.kernel_size + layout.bss_size);
171-	mods_section->raw_data_size = grub_host_to_target32 (reloc_addr - layout.kernel_size - header_size);
172-	mods_section->raw_data_offset = grub_host_to_target32 (header_size + layout.kernel_size);
173-	mods_section->characteristics
174-	  = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
175-			      | GRUB_PE32_SCN_MEM_READ
176-			      | GRUB_PE32_SCN_MEM_WRITE);
177-
178-	PE_OHDR (o32, o64, base_relocation_table.rva) = grub_host_to_target32 (reloc_addr);
179-	PE_OHDR (o32, o64, base_relocation_table.size) = grub_host_to_target32 (layout.reloc_size);
180-	reloc_section = mods_section + 1;
181-	strcpy (reloc_section->name, ".reloc");
182-	reloc_section->virtual_size = grub_host_to_target32 (layout.reloc_size);
183-	reloc_section->virtual_address = grub_host_to_target32 (reloc_addr + layout.bss_size);
184-	reloc_section->raw_data_size = grub_host_to_target32 (layout.reloc_size);
185-	reloc_section->raw_data_offset = grub_host_to_target32 (reloc_addr);
186-	reloc_section->characteristics
187-	  = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
188-			      | GRUB_PE32_SCN_MEM_DISCARDABLE
189-			      | GRUB_PE32_SCN_MEM_READ);
190+	section = init_pe_section (image_target, section, ".data",
191+				   &vma, scn_size, image_target->section_align,
192+				   &raw_data, scn_size,
193+				   GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
194+				   GRUB_PE32_SCN_MEM_READ |
195+				   GRUB_PE32_SCN_MEM_WRITE);
196+
197+	scn_size = pe_size - layout.reloc_size - raw_data;
198+	section = init_pe_section (image_target, section, "mods",
199+				   &vma, scn_size, image_target->section_align,
200+				   &raw_data, scn_size,
201+				   GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
202+				   GRUB_PE32_SCN_MEM_READ |
203+				   GRUB_PE32_SCN_MEM_WRITE);
204+
205+	scn_size = layout.reloc_size;
206+	PE_OHDR (o32, o64, base_relocation_table.rva) = grub_host_to_target32 (vma);
207+	PE_OHDR (o32, o64, base_relocation_table.size) = grub_host_to_target32 (scn_size);
208+	memcpy (pe_img + raw_data, layout.reloc_section, scn_size);
209+	init_pe_section (image_target, section, ".reloc",
210+			 &vma, scn_size, image_target->section_align,
211+			 &raw_data, scn_size,
212+			 GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
213+			 GRUB_PE32_SCN_MEM_DISCARDABLE |
214+			 GRUB_PE32_SCN_MEM_READ);
215+
216 	free (core_img);
217 	core_img = pe_img;
218 	core_size = pe_size;
219--
2202.14.2
221
222