2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 2010 Free Software Foundation, Inc.
5
* GRUB is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* GRUB is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19
#include <grub/relocator.h>
20
#include <grub/relocator_private.h>
21
#include <grub/memory.h>
22
#include <grub/efi/efi.h>
23
#include <grub/efi/api.h>
24
#include <grub/term.h>
26
#define NEXT_MEMORY_DESCRIPTOR(desc, size) \
27
((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
30
grub_relocator_firmware_get_max_events (void)
32
grub_efi_uintn_t mmapsize = 0, descriptor_size = 0;
33
grub_efi_uint32_t descriptor_version = 0;
35
grub_efi_get_memory_map (&mmapsize, NULL, &key, &descriptor_size,
37
/* Since grub_relocator_firmware_fill_events uses malloc
38
we need some reserve. Hence +10. */
39
return 2 * (mmapsize / descriptor_size + 10);
43
grub_relocator_firmware_fill_events (struct grub_relocator_mmap_event *events)
45
grub_efi_uintn_t mmapsize = 0, desc_size = 0;
46
grub_efi_uint32_t descriptor_version = 0;
47
grub_efi_memory_descriptor_t *descs = NULL;
50
grub_efi_memory_descriptor_t *desc;
52
grub_efi_get_memory_map (&mmapsize, NULL, &key, &desc_size,
54
descs = grub_malloc (mmapsize);
58
grub_efi_get_memory_map (&mmapsize, descs, &key, &desc_size,
62
(char *) desc < ((char *) descs + mmapsize);
63
desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
65
if (desc->type != GRUB_EFI_CONVENTIONAL_MEMORY)
67
events[counter].type = REG_FIRMWARE_START;
68
events[counter].pos = desc->physical_start;
70
events[counter].type = REG_FIRMWARE_END;
71
events[counter].pos = desc->physical_start + (desc->num_pages << 12);
79
grub_relocator_firmware_alloc_region (grub_addr_t start, grub_size_t size)
81
grub_efi_boot_services_t *b;
82
grub_efi_physical_address_t address = start;
83
grub_efi_status_t status;
85
if (grub_efi_is_finished)
88
b = grub_efi_system_table->boot_services;
89
status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_ADDRESS,
90
GRUB_EFI_LOADER_DATA, size >> 12, &address);
91
return (status == GRUB_EFI_SUCCESS);
95
grub_relocator_firmware_free_region (grub_addr_t start, grub_size_t size)
97
grub_efi_boot_services_t *b;
99
if (grub_efi_is_finished)
102
b = grub_efi_system_table->boot_services;
103
efi_call_2 (b->free_pages, start, size >> 12);