2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2010 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/relocator.h>
2N/A#include <grub/relocator_private.h>
2N/A#include <grub/memory.h>
2N/A#include <grub/efi/efi.h>
2N/A#include <grub/efi/api.h>
2N/A#include <grub/term.h>
2N/A
2N/A#define NEXT_MEMORY_DESCRIPTOR(desc, size) \
2N/A ((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
2N/A
2N/Aunsigned
2N/Agrub_relocator_firmware_get_max_events (void)
2N/A{
2N/A grub_efi_uintn_t mmapsize = 0, descriptor_size = 0;
2N/A grub_efi_uint32_t descriptor_version = 0;
2N/A grub_efi_uintn_t key;
2N/A grub_efi_get_memory_map (&mmapsize, NULL, &key, &descriptor_size,
2N/A &descriptor_version);
2N/A /* Since grub_relocator_firmware_fill_events uses malloc
2N/A we need some reserve. Hence +10. */
2N/A return 2 * (mmapsize / descriptor_size + 10);
2N/A}
2N/A
2N/Aunsigned
2N/Agrub_relocator_firmware_fill_events (struct grub_relocator_mmap_event *events)
2N/A{
2N/A grub_efi_uintn_t mmapsize = 0, desc_size = 0;
2N/A grub_efi_uint32_t descriptor_version = 0;
2N/A grub_efi_memory_descriptor_t *descs = NULL;
2N/A grub_efi_uintn_t key;
2N/A int counter = 0;
2N/A grub_efi_memory_descriptor_t *desc;
2N/A
2N/A grub_efi_get_memory_map (&mmapsize, NULL, &key, &desc_size,
2N/A &descriptor_version);
2N/A descs = grub_malloc (mmapsize);
2N/A if (!descs)
2N/A return 0;
2N/A
2N/A grub_efi_get_memory_map (&mmapsize, descs, &key, &desc_size,
2N/A &descriptor_version);
2N/A
2N/A for (desc = descs;
2N/A (char *) desc < ((char *) descs + mmapsize);
2N/A desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
2N/A {
2N/A grub_uint64_t start = desc->physical_start;
2N/A grub_uint64_t end = desc->physical_start + (desc->num_pages << 12);
2N/A
2N/A /* post-4G addresses are never supported on 32-bit EFI.
2N/A Moreover it has been reported that some 64-bit EFI contrary to the
2N/A spec don't map post-4G pages. So if you enable post-4G allocations,
2N/A map pages manually or check that they are mapped.
2N/A */
2N/A if (end >= 0x100000000ULL)
2N/A end = 0x100000000ULL;
2N/A if (end <= start)
2N/A continue;
2N/A if (desc->type != GRUB_EFI_CONVENTIONAL_MEMORY)
2N/A continue;
2N/A events[counter].type = REG_FIRMWARE_START;
2N/A events[counter].pos = start;
2N/A counter++;
2N/A events[counter].type = REG_FIRMWARE_END;
2N/A events[counter].pos = end;
2N/A counter++;
2N/A }
2N/A
2N/A return counter;
2N/A}
2N/A
2N/Aint
2N/Agrub_relocator_firmware_alloc_region (grub_addr_t start, grub_size_t size)
2N/A{
2N/A grub_efi_boot_services_t *b;
2N/A grub_efi_physical_address_t address = start;
2N/A grub_efi_status_t status;
2N/A
2N/A if (grub_efi_is_finished)
2N/A return 1;
2N/A
2N/A grub_dprintf ("relocator", "EFI alloc: %llx, %llx\n",
2N/A (unsigned long long) start, (unsigned long long) size);
2N/A
2N/A b = grub_efi_system_table->boot_services;
2N/A status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_ADDRESS,
2N/A GRUB_EFI_LOADER_DATA, size >> 12, &address);
2N/A return (status == GRUB_EFI_SUCCESS);
2N/A}
2N/A
2N/Avoid
2N/Agrub_relocator_firmware_free_region (grub_addr_t start, grub_size_t size)
2N/A{
2N/A grub_efi_boot_services_t *b;
2N/A
2N/A if (grub_efi_is_finished)
2N/A return;
2N/A
2N/A b = grub_efi_system_table->boot_services;
2N/A efi_call_2 (b->free_pages, start, size >> 12);
2N/A}