summaryrefslogtreecommitdiffstats
path: root/grub-core/lib/efi
diff options
context:
space:
mode:
Diffstat (limited to 'grub-core/lib/efi')
-rw-r--r--grub-core/lib/efi/datetime.c82
-rw-r--r--grub-core/lib/efi/halt.c41
-rw-r--r--grub-core/lib/efi/relocator.c119
3 files changed, 242 insertions, 0 deletions
diff --git a/grub-core/lib/efi/datetime.c b/grub-core/lib/efi/datetime.c
new file mode 100644
index 0000000..0fd1b5f
--- /dev/null
+++ b/grub-core/lib/efi/datetime.c
@@ -0,0 +1,82 @@
+/* kern/efi/datetime.c - efi datetime function.
+ *
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2008 Free Software Foundation, Inc.
+ *
+ * GRUB 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.
+ *
+ * GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/types.h>
+#include <grub/symbol.h>
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/datetime.h>
+#include <grub/dl.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+grub_err_t
+grub_get_datetime (struct grub_datetime *datetime)
+{
+ grub_efi_status_t status;
+ struct grub_efi_time efi_time;
+
+ status = efi_call_2 (grub_efi_system_table->runtime_services->get_time,
+ &efi_time, 0);
+
+ if (status)
+ return grub_error (GRUB_ERR_INVALID_COMMAND,
+ "can\'t get datetime using efi");
+ else
+ {
+ datetime->year = efi_time.year;
+ datetime->month = efi_time.month;
+ datetime->day = efi_time.day;
+ datetime->hour = efi_time.hour;
+ datetime->minute = efi_time.minute;
+ datetime->second = efi_time.second;
+ }
+
+ return 0;
+}
+
+grub_err_t
+grub_set_datetime (struct grub_datetime *datetime)
+{
+ grub_efi_status_t status;
+ struct grub_efi_time efi_time;
+
+ status = efi_call_2 (grub_efi_system_table->runtime_services->get_time,
+ &efi_time, 0);
+
+ if (status)
+ return grub_error (GRUB_ERR_INVALID_COMMAND,
+ "can\'t get datetime using efi");
+
+ efi_time.year = datetime->year;
+ efi_time.month = datetime->month;
+ efi_time.day = datetime->day;
+ efi_time.hour = datetime->hour;
+ efi_time.minute = datetime->minute;
+ efi_time.second = datetime->second;
+
+ status = efi_call_1 (grub_efi_system_table->runtime_services->set_time,
+ &efi_time);
+
+ if (status)
+ return grub_error (GRUB_ERR_INVALID_COMMAND,
+ "can\'t set datetime using efi");
+
+ return 0;
+}
diff --git a/grub-core/lib/efi/halt.c b/grub-core/lib/efi/halt.c
new file mode 100644
index 0000000..29d4136
--- /dev/null
+++ b/grub-core/lib/efi/halt.c
@@ -0,0 +1,41 @@
+/* efi.c - generic EFI support */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2006,2007,2008,2009,2010 Free Software Foundation, Inc.
+ *
+ * GRUB 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.
+ *
+ * GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/kernel.h>
+#include <grub/acpi.h>
+#include <grub/loader.h>
+
+void
+grub_halt (void)
+{
+ grub_machine_fini (GRUB_LOADER_FLAG_NORETURN |
+ GRUB_LOADER_FLAG_EFI_KEEP_ALLOCATED_MEMORY);
+#if !defined(__ia64__) && !defined(__arm__) && !defined(__aarch64__) && \
+ !defined(__riscv)
+ grub_acpi_halt ();
+#endif
+ efi_call_4 (grub_efi_system_table->runtime_services->reset_system,
+ GRUB_EFI_RESET_SHUTDOWN, GRUB_EFI_SUCCESS, 0, NULL);
+
+ while (1);
+}
diff --git a/grub-core/lib/efi/relocator.c b/grub-core/lib/efi/relocator.c
new file mode 100644
index 0000000..319b69e
--- /dev/null
+++ b/grub-core/lib/efi/relocator.c
@@ -0,0 +1,119 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2010 Free Software Foundation, Inc.
+ *
+ * GRUB 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.
+ *
+ * GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/relocator.h>
+#include <grub/relocator_private.h>
+#include <grub/memory.h>
+#include <grub/efi/efi.h>
+#include <grub/efi/api.h>
+#include <grub/term.h>
+
+#define NEXT_MEMORY_DESCRIPTOR(desc, size) \
+ ((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
+
+unsigned
+grub_relocator_firmware_get_max_events (void)
+{
+ grub_efi_uintn_t mmapsize = 0, descriptor_size = 0;
+ grub_efi_uint32_t descriptor_version = 0;
+ grub_efi_uintn_t key;
+ grub_efi_get_memory_map (&mmapsize, NULL, &key, &descriptor_size,
+ &descriptor_version);
+ /* Since grub_relocator_firmware_fill_events uses malloc
+ we need some reserve. Hence +10. */
+ return 2 * (mmapsize / descriptor_size + 10);
+}
+
+unsigned
+grub_relocator_firmware_fill_events (struct grub_relocator_mmap_event *events)
+{
+ grub_efi_uintn_t mmapsize = 0, desc_size = 0;
+ grub_efi_uint32_t descriptor_version = 0;
+ grub_efi_memory_descriptor_t *descs = NULL;
+ grub_efi_uintn_t key;
+ int counter = 0;
+ grub_efi_memory_descriptor_t *desc;
+
+ grub_efi_get_memory_map (&mmapsize, NULL, &key, &desc_size,
+ &descriptor_version);
+ descs = grub_malloc (mmapsize);
+ if (!descs)
+ return 0;
+
+ grub_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))
+ {
+ grub_uint64_t start = desc->physical_start;
+ grub_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 != GRUB_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
+grub_relocator_firmware_alloc_region (grub_addr_t start, grub_size_t size)
+{
+ grub_efi_boot_services_t *b;
+ grub_efi_physical_address_t address = start;
+ grub_efi_status_t status;
+
+ if (grub_efi_is_finished)
+ return 1;
+#ifdef DEBUG_RELOCATOR_NOMEM_DPRINTF
+ grub_dprintf ("relocator", "EFI alloc: %llx, %llx\n",
+ (unsigned long long) start, (unsigned long long) size);
+#endif
+ b = grub_efi_system_table->boot_services;
+ status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_ADDRESS,
+ GRUB_EFI_LOADER_DATA, size >> 12, &address);
+ return (status == GRUB_EFI_SUCCESS);
+}
+
+void
+grub_relocator_firmware_free_region (grub_addr_t start, grub_size_t size)
+{
+ grub_efi_boot_services_t *b;
+
+ if (grub_efi_is_finished)
+ return;
+
+ b = grub_efi_system_table->boot_services;
+ efi_call_2 (b->free_pages, start, size >> 12);
+}