vaseboot/VasEBoot-core/lib/efi/relocator.c

120 lines
3.8 KiB
C

/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2010 Free Software Foundation, Inc.
*
* VAS_EBOOT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VAS_EBOOT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/relocator.h>
#include <VasEBoot/relocator_private.h>
#include <VasEBoot/memory.h>
#include <VasEBoot/efi/efi.h>
#include <VasEBoot/efi/api.h>
#include <VasEBoot/term.h>
#define NEXT_MEMORY_DESCRIPTOR(desc, size) \
((VasEBoot_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
unsigned
VasEBoot_relocator_firmware_get_max_events (void)
{
VasEBoot_efi_uintn_t mmapsize = 0, descriptor_size = 0;
VasEBoot_efi_uint32_t descriptor_version = 0;
VasEBoot_efi_uintn_t key;
VasEBoot_efi_get_memory_map (&mmapsize, NULL, &key, &descriptor_size,
&descriptor_version);
/* Since VasEBoot_relocator_firmware_fill_events uses malloc
we need some reserve. Hence +10. */
return 2 * (mmapsize / descriptor_size + 10);
}
unsigned
VasEBoot_relocator_firmware_fill_events (struct VasEBoot_relocator_mmap_event *events)
{
VasEBoot_efi_uintn_t mmapsize = 0, desc_size = 0;
VasEBoot_efi_uint32_t descriptor_version = 0;
VasEBoot_efi_memory_descriptor_t *descs = NULL;
VasEBoot_efi_uintn_t key;
int counter = 0;
VasEBoot_efi_memory_descriptor_t *desc;
VasEBoot_efi_get_memory_map (&mmapsize, NULL, &key, &desc_size,
&descriptor_version);
descs = VasEBoot_malloc (mmapsize);
if (!descs)
return 0;
VasEBoot_efi_get_memory_map (&mmapsize, descs, &key, &desc_size,
&descriptor_version);
for (desc = descs;
(char *) desc < ((char *) descs + mmapsize);
desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
{
VasEBoot_uint64_t start = desc->physical_start;
VasEBoot_uint64_t end = desc->physical_start + (desc->num_pages << 12);
/* post-4G addresses are never supported on 32-bit EFI.
Moreover it has been reported that some 64-bit EFI contrary to the
spec don't map post-4G pages. So if you enable post-4G allocations,
map pages manually or check that they are mapped.
*/
if (end >= 0x100000000ULL)
end = 0x100000000ULL;
if (end <= start)
continue;
if (desc->type != VAS_EBOOT_EFI_CONVENTIONAL_MEMORY)
continue;
events[counter].type = REG_FIRMWARE_START;
events[counter].pos = start;
counter++;
events[counter].type = REG_FIRMWARE_END;
events[counter].pos = end;
counter++;
}
return counter;
}
int
VasEBoot_relocator_firmware_alloc_region (VasEBoot_addr_t start, VasEBoot_size_t size)
{
VasEBoot_efi_boot_services_t *b;
VasEBoot_efi_physical_address_t address = start;
VasEBoot_efi_status_t status;
if (VasEBoot_efi_is_finished)
return 1;
#ifdef DEBUG_RELOCATOR_NOMEM_DPRINTF
VasEBoot_dprintf ("relocator", "EFI alloc: %llx, %llx\n",
(unsigned long long) start, (unsigned long long) size);
#endif
b = VasEBoot_efi_system_table->boot_services;
status = b->allocate_pages (VAS_EBOOT_EFI_ALLOCATE_ADDRESS,
VAS_EBOOT_EFI_LOADER_DATA, size >> 12, &address);
return (status == VAS_EBOOT_EFI_SUCCESS);
}
void
VasEBoot_relocator_firmware_free_region (VasEBoot_addr_t start, VasEBoot_size_t size)
{
VasEBoot_efi_boot_services_t *b;
if (VasEBoot_efi_is_finished)
return;
b = VasEBoot_efi_system_table->boot_services;
b->free_pages (start, size >> 12);
}