mirror of
https://git.proxmox.com/git/grub2
synced 2025-07-27 17:36:30 +00:00
util/mkimage: Refactor section setup to use a helper
Add a init_pe_section() helper function to setup PE sections. This makes the code simpler and easier to read. Signed-off-by: Peter Jones <pjones@redhat.com> Signed-off-by: Javier Martinez Canillas <javierm@redhat.com> Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
parent
ff406eff25
commit
f60ba9e594
139
util/mkimage.c
139
util/mkimage.c
@ -816,6 +816,38 @@ grub_install_get_image_targets_string (void)
|
|||||||
return formats;
|
return formats;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The image_target parameter is used by the grub_host_to_target32() macro.
|
||||||
|
*/
|
||||||
|
static struct grub_pe32_section_table *
|
||||||
|
init_pe_section(const struct grub_install_image_target_desc *image_target,
|
||||||
|
struct grub_pe32_section_table *section,
|
||||||
|
const char * const name,
|
||||||
|
grub_uint32_t *vma, grub_uint32_t vsz, grub_uint32_t valign,
|
||||||
|
grub_uint32_t *rda, grub_uint32_t rsz,
|
||||||
|
grub_uint32_t characteristics)
|
||||||
|
{
|
||||||
|
size_t len = strlen (name);
|
||||||
|
|
||||||
|
if (len > sizeof (section->name))
|
||||||
|
grub_util_error (_("section name %s length is bigger than %lu"),
|
||||||
|
name, (unsigned long) sizeof (section->name));
|
||||||
|
|
||||||
|
memcpy (section->name, name, len);
|
||||||
|
|
||||||
|
section->virtual_address = grub_host_to_target32 (*vma);
|
||||||
|
section->virtual_size = grub_host_to_target32 (vsz);
|
||||||
|
(*vma) = ALIGN_UP (*vma + vsz, valign);
|
||||||
|
|
||||||
|
section->raw_data_offset = grub_host_to_target32 (*rda);
|
||||||
|
section->raw_data_size = grub_host_to_target32 (rsz);
|
||||||
|
(*rda) = ALIGN_UP (*rda + rsz, GRUB_PE32_FILE_ALIGNMENT);
|
||||||
|
|
||||||
|
section->characteristics = grub_host_to_target32 (characteristics);
|
||||||
|
|
||||||
|
return section + 1;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* tmp_ is just here so the compiler knows we'll never derefernce a NULL.
|
* tmp_ is just here so the compiler knows we'll never derefernce a NULL.
|
||||||
* It should get fully optimized away.
|
* It should get fully optimized away.
|
||||||
@ -1257,17 +1289,13 @@ grub_install_generate_image (const char *dir, const char *prefix,
|
|||||||
break;
|
break;
|
||||||
case IMAGE_EFI:
|
case IMAGE_EFI:
|
||||||
{
|
{
|
||||||
void *pe_img;
|
char *pe_img, *header;
|
||||||
grub_uint8_t *header;
|
struct grub_pe32_section_table *section;
|
||||||
void *sections;
|
|
||||||
size_t scn_size;
|
size_t scn_size;
|
||||||
size_t pe_size;
|
grub_uint32_t vma, raw_data;
|
||||||
|
size_t pe_size, header_size;
|
||||||
struct grub_pe32_coff_header *c;
|
struct grub_pe32_coff_header *c;
|
||||||
struct grub_pe32_section_table *text_section, *data_section;
|
|
||||||
struct grub_pe32_section_table *mods_section, *reloc_section;
|
|
||||||
static const grub_uint8_t stub[] = GRUB_PE32_MSDOS_STUB;
|
static const grub_uint8_t stub[] = GRUB_PE32_MSDOS_STUB;
|
||||||
int header_size;
|
|
||||||
int reloc_addr;
|
|
||||||
struct grub_pe32_optional_header *o32 = NULL;
|
struct grub_pe32_optional_header *o32 = NULL;
|
||||||
struct grub_pe64_optional_header *o64 = NULL;
|
struct grub_pe64_optional_header *o64 = NULL;
|
||||||
|
|
||||||
@ -1276,17 +1304,12 @@ grub_install_generate_image (const char *dir, const char *prefix,
|
|||||||
else
|
else
|
||||||
header_size = EFI64_HEADER_SIZE;
|
header_size = EFI64_HEADER_SIZE;
|
||||||
|
|
||||||
reloc_addr = ALIGN_UP (header_size + core_size,
|
vma = raw_data = header_size;
|
||||||
GRUB_PE32_FILE_ALIGNMENT);
|
pe_size = ALIGN_UP (header_size + core_size, GRUB_PE32_FILE_ALIGNMENT) +
|
||||||
|
ALIGN_UP (layout.reloc_size, GRUB_PE32_FILE_ALIGNMENT);
|
||||||
|
header = pe_img = xcalloc (1, pe_size);
|
||||||
|
|
||||||
pe_size = ALIGN_UP (reloc_addr + layout.reloc_size,
|
memcpy (pe_img + raw_data, core_img, core_size);
|
||||||
GRUB_PE32_FILE_ALIGNMENT);
|
|
||||||
pe_img = xmalloc (reloc_addr + layout.reloc_size);
|
|
||||||
memset (pe_img, 0, header_size);
|
|
||||||
memcpy ((char *) pe_img + header_size, core_img, core_size);
|
|
||||||
memset ((char *) pe_img + header_size + core_size, 0, reloc_addr - (header_size + core_size));
|
|
||||||
memcpy ((char *) pe_img + reloc_addr, layout.reloc_section, layout.reloc_size);
|
|
||||||
header = pe_img;
|
|
||||||
|
|
||||||
/* The magic. */
|
/* The magic. */
|
||||||
memcpy (header, stub, GRUB_PE32_MSDOS_STUB_SIZE);
|
memcpy (header, stub, GRUB_PE32_MSDOS_STUB_SIZE);
|
||||||
@ -1319,18 +1342,17 @@ grub_install_generate_image (const char *dir, const char *prefix,
|
|||||||
o32->magic = grub_host_to_target16 (GRUB_PE32_PE32_MAGIC);
|
o32->magic = grub_host_to_target16 (GRUB_PE32_PE32_MAGIC);
|
||||||
o32->data_base = grub_host_to_target32 (header_size + layout.exec_size);
|
o32->data_base = grub_host_to_target32 (header_size + layout.exec_size);
|
||||||
|
|
||||||
sections = o32 + 1;
|
section = (struct grub_pe32_section_table *)(o32 + 1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
c->optional_header_size = grub_host_to_target16 (sizeof (struct grub_pe64_optional_header));
|
c->optional_header_size = grub_host_to_target16 (sizeof (struct grub_pe64_optional_header));
|
||||||
|
|
||||||
o64 = (struct grub_pe64_optional_header *)
|
o64 = (struct grub_pe64_optional_header *)
|
||||||
(header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE +
|
(header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE +
|
||||||
sizeof (struct grub_pe32_coff_header));
|
sizeof (struct grub_pe32_coff_header));
|
||||||
o64->magic = grub_host_to_target16 (GRUB_PE32_PE64_MAGIC);
|
o64->magic = grub_host_to_target16 (GRUB_PE32_PE64_MAGIC);
|
||||||
|
|
||||||
sections = o64 + 1;
|
section = (struct grub_pe32_section_table *)(o64 + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
PE_OHDR (o32, o64, header_size) = grub_host_to_target32 (header_size);
|
PE_OHDR (o32, o64, header_size) = grub_host_to_target32 (header_size);
|
||||||
@ -1350,58 +1372,47 @@ grub_install_generate_image (const char *dir, const char *prefix,
|
|||||||
PE_OHDR (o32, o64, num_data_directories) = grub_host_to_target32 (GRUB_PE32_NUM_DATA_DIRECTORIES);
|
PE_OHDR (o32, o64, num_data_directories) = grub_host_to_target32 (GRUB_PE32_NUM_DATA_DIRECTORIES);
|
||||||
|
|
||||||
/* The sections. */
|
/* The sections. */
|
||||||
PE_OHDR (o32, o64, code_base) = grub_host_to_target32 (header_size);
|
PE_OHDR (o32, o64, code_base) = grub_host_to_target32 (vma);
|
||||||
PE_OHDR (o32, o64, code_size) = grub_host_to_target32 (layout.exec_size);
|
PE_OHDR (o32, o64, code_size) = grub_host_to_target32 (layout.exec_size);
|
||||||
text_section = sections;
|
section = init_pe_section (image_target, section, ".text",
|
||||||
strcpy (text_section->name, ".text");
|
&vma, layout.exec_size,
|
||||||
text_section->virtual_size = grub_host_to_target32 (layout.exec_size);
|
image_target->section_align,
|
||||||
text_section->virtual_address = grub_host_to_target32 (header_size);
|
&raw_data, layout.exec_size,
|
||||||
text_section->raw_data_size = grub_host_to_target32 (layout.exec_size);
|
GRUB_PE32_SCN_CNT_CODE |
|
||||||
text_section->raw_data_offset = grub_host_to_target32 (header_size);
|
GRUB_PE32_SCN_MEM_EXECUTE |
|
||||||
text_section->characteristics = grub_cpu_to_le32_compile_time (
|
GRUB_PE32_SCN_MEM_READ);
|
||||||
GRUB_PE32_SCN_CNT_CODE
|
|
||||||
| GRUB_PE32_SCN_MEM_EXECUTE
|
|
||||||
| GRUB_PE32_SCN_MEM_READ);
|
|
||||||
|
|
||||||
scn_size = ALIGN_UP (layout.kernel_size - layout.exec_size, GRUB_PE32_FILE_ALIGNMENT);
|
scn_size = ALIGN_UP (layout.kernel_size - layout.exec_size, GRUB_PE32_FILE_ALIGNMENT);
|
||||||
PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (scn_size +
|
PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (scn_size +
|
||||||
ALIGN_UP (total_module_size,
|
ALIGN_UP (total_module_size,
|
||||||
GRUB_PE32_FILE_ALIGNMENT));
|
GRUB_PE32_FILE_ALIGNMENT));
|
||||||
|
|
||||||
data_section = text_section + 1;
|
section = init_pe_section (image_target, section, ".data",
|
||||||
strcpy (data_section->name, ".data");
|
&vma, scn_size, image_target->section_align,
|
||||||
data_section->virtual_size = grub_host_to_target32 (layout.kernel_size - layout.exec_size);
|
&raw_data, scn_size,
|
||||||
data_section->virtual_address = grub_host_to_target32 (header_size + layout.exec_size);
|
GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
|
||||||
data_section->raw_data_size = grub_host_to_target32 (layout.kernel_size - layout.exec_size);
|
GRUB_PE32_SCN_MEM_READ |
|
||||||
data_section->raw_data_offset = grub_host_to_target32 (header_size + layout.exec_size);
|
GRUB_PE32_SCN_MEM_WRITE);
|
||||||
data_section->characteristics
|
|
||||||
= grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
|
|
||||||
| GRUB_PE32_SCN_MEM_READ
|
|
||||||
| GRUB_PE32_SCN_MEM_WRITE);
|
|
||||||
|
|
||||||
mods_section = data_section + 1;
|
scn_size = pe_size - layout.reloc_size - raw_data;
|
||||||
strcpy (mods_section->name, "mods");
|
section = init_pe_section (image_target, section, "mods",
|
||||||
mods_section->virtual_size = grub_host_to_target32 (reloc_addr - layout.kernel_size - header_size);
|
&vma, scn_size, image_target->section_align,
|
||||||
mods_section->virtual_address = grub_host_to_target32 (header_size + layout.kernel_size + layout.bss_size);
|
&raw_data, scn_size,
|
||||||
mods_section->raw_data_size = grub_host_to_target32 (reloc_addr - layout.kernel_size - header_size);
|
GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
|
||||||
mods_section->raw_data_offset = grub_host_to_target32 (header_size + layout.kernel_size);
|
GRUB_PE32_SCN_MEM_READ |
|
||||||
mods_section->characteristics
|
GRUB_PE32_SCN_MEM_WRITE);
|
||||||
= grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
|
|
||||||
| GRUB_PE32_SCN_MEM_READ
|
scn_size = layout.reloc_size;
|
||||||
| GRUB_PE32_SCN_MEM_WRITE);
|
PE_OHDR (o32, o64, base_relocation_table.rva) = grub_host_to_target32 (vma);
|
||||||
|
PE_OHDR (o32, o64, base_relocation_table.size) = grub_host_to_target32 (scn_size);
|
||||||
|
memcpy (pe_img + raw_data, layout.reloc_section, scn_size);
|
||||||
|
init_pe_section (image_target, section, ".reloc",
|
||||||
|
&vma, scn_size, image_target->section_align,
|
||||||
|
&raw_data, scn_size,
|
||||||
|
GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
|
||||||
|
GRUB_PE32_SCN_MEM_DISCARDABLE |
|
||||||
|
GRUB_PE32_SCN_MEM_READ);
|
||||||
|
|
||||||
PE_OHDR (o32, o64, base_relocation_table.rva) = grub_host_to_target32 (reloc_addr);
|
|
||||||
PE_OHDR (o32, o64, base_relocation_table.size) = grub_host_to_target32 (layout.reloc_size);
|
|
||||||
reloc_section = mods_section + 1;
|
|
||||||
strcpy (reloc_section->name, ".reloc");
|
|
||||||
reloc_section->virtual_size = grub_host_to_target32 (layout.reloc_size);
|
|
||||||
reloc_section->virtual_address = grub_host_to_target32 (reloc_addr + layout.bss_size);
|
|
||||||
reloc_section->raw_data_size = grub_host_to_target32 (layout.reloc_size);
|
|
||||||
reloc_section->raw_data_offset = grub_host_to_target32 (reloc_addr);
|
|
||||||
reloc_section->characteristics
|
|
||||||
= grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
|
|
||||||
| GRUB_PE32_SCN_MEM_DISCARDABLE
|
|
||||||
| GRUB_PE32_SCN_MEM_READ);
|
|
||||||
free (core_img);
|
free (core_img);
|
||||||
core_img = pe_img;
|
core_img = pe_img;
|
||||||
core_size = pe_size;
|
core_size = pe_size;
|
||||||
|
Loading…
Reference in New Issue
Block a user