Skip to content

Commit

Permalink
grub-core/loader/multiboot_elfxx.c: Fix calculating MLE size
Browse files Browse the repository at this point in the history
To avoid mismatch between OS SINIT data MLE size and MLE size from
MLE header, take the mle_size from MLE header and do not align it
to MPR size, which is 2MB.

Signed-off-by: Michał Żygowski <[email protected]>
  • Loading branch information
miczyg1 committed May 23, 2024
1 parent f6dfae5 commit 2d821fa
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions grub-core/loader/multiboot_elfxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld)
int i;
void *source = NULL;
struct grub_slaunch_params *slparams = grub_slaunch_params();
grub_size_t total_size;
grub_uint32_t mle_hdr_offset;
struct grub_txt_mle_header *mle_hdr;

if (ehdr->e_ident[EI_MAG0] != ELFMAG0
|| ehdr->e_ident[EI_MAG1] != ELFMAG1
Expand Down Expand Up @@ -106,25 +106,16 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld)
{
load_size = highest_load - mld->link_base_addr;

grub_dprintf ("multiboot_loader", "align=0x%lx, preference=0x%x, "
"load_size=0x%x, avoid_efi_boot_services=%d\n",
(long) mld->align, mld->preference, load_size,
mld->avoid_efi_boot_services);

if (grub_slaunch_platform_type () != SLP_NONE)
{
#ifndef GRUB_USE_MULTIBOOT2
return grub_error (GRUB_ERR_BAD_OS, "Only multiboot2 supported for slaunch");
#else
/*
* We allocate the the binary together with page tables to make one
* contiguous block for MLE. We have to align up to PMR (2MB).
* contiguous block for MLE.
*/
total_size = ALIGN_UP(load_size, GRUB_TXT_PMR_ALIGN);

slparams->mle_size = total_size;

slparams->mle_ptab_size = grub_txt_get_mle_ptab_size (total_size);
slparams->mle_ptab_size = grub_txt_get_mle_ptab_size (load_size);
slparams->mle_ptab_size = ALIGN_UP (slparams->mle_ptab_size, GRUB_TXT_PMR_ALIGN);

/* Do not go below GRUB_TXT_PMR_ALIGN. */
Expand All @@ -134,16 +125,20 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld)
}
else
{
total_size = load_size;
slparams->mle_ptab_size = 0;
}

if (total_size > mld->max_addr || mld->min_addr > mld->max_addr - total_size)
grub_dprintf ("multiboot_loader", "align=0x%lx, preference=0x%x, "
"load_size=0x%x, avoid_efi_boot_services=%d\n",
(long) mld->align, mld->preference, load_size,
mld->avoid_efi_boot_services);

if (load_size > mld->max_addr || mld->min_addr > mld->max_addr - load_size)
return grub_error (GRUB_ERR_BAD_OS, "invalid min/max address and/or load size");

err = grub_relocator_alloc_chunk_align_safe (GRUB_MULTIBOOT (relocator), &ch,
mld->min_addr, mld->max_addr - total_size,
total_size, mld->align ? mld->align : 1,
mld->min_addr, mld->max_addr,
load_size, mld->align ? mld->align : 1,
mld->preference, mld->avoid_efi_boot_services);

if (err)
Expand Down Expand Up @@ -266,6 +261,9 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld)

grub_dprintf ("slaunch", "slparams->mle_header_offset: 0x%08x\n",
slparams->mle_header_offset);

mle_hdr = (struct grub_txt_mle_header *)((grub_addr_t)source + mle_hdr_offset);
slparams->mle_size = mle_hdr->mle_end - mle_hdr->mle_start;
}

for (i = 0; i < ehdr->e_phnum; i++)
Expand Down

0 comments on commit 2d821fa

Please sign in to comment.