1 From 9f8104db535a8d2f9ef42824669431bdd86b3fe8 Mon Sep 17 00:00:00 2001
2 From: Matthew Garrett <mjg@redhat.com>
3 Date: Tue, 10 Jul 2012 11:58:52 -0400
4 Subject: [PATCH 446/482] Add support for linuxefi
7 grub-core/Makefile.core.def | 8 +
8 grub-core/kern/efi/mm.c | 32 ++++
9 grub-core/loader/i386/efi/linux.c | 371 ++++++++++++++++++++++++++++++++++++++
10 include/grub/efi/efi.h | 3 +
11 include/grub/i386/linux.h | 1 +
12 5 files changed, 415 insertions(+)
13 create mode 100644 grub-core/loader/i386/efi/linux.c
15 diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
16 index bb62dce..def6606 100644
17 --- a/grub-core/Makefile.core.def
18 +++ b/grub-core/Makefile.core.def
19 @@ -1534,6 +1534,14 @@ module = {
24 + efi = loader/i386/efi/linux.c;
25 + efi = lib/cmdline.c;
27 + enable = x86_64_efi;
32 efi = loader/efi/chainloader.c;
33 i386_pc = loader/i386/pc/chainloader.c;
34 diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
35 index 77c9384..025d665 100644
36 --- a/grub-core/kern/efi/mm.c
37 +++ b/grub-core/kern/efi/mm.c
38 @@ -47,6 +47,38 @@ static grub_efi_uintn_t finish_desc_size;
39 static grub_efi_uint32_t finish_desc_version;
40 int grub_efi_is_finished = 0;
42 +/* Allocate pages below a specified address */
44 +grub_efi_allocate_pages_max (grub_efi_physical_address_t max,
45 + grub_efi_uintn_t pages)
47 + grub_efi_status_t status;
48 + grub_efi_boot_services_t *b;
49 + grub_efi_physical_address_t address = max;
51 + if (max > 0xffffffff)
54 + b = grub_efi_system_table->boot_services;
55 + status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS, GRUB_EFI_LOADER_DATA, pages, &address);
57 + if (status != GRUB_EFI_SUCCESS)
62 + /* Uggh, the address 0 was allocated... This is too annoying,
63 + so reallocate another one. */
65 + status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS, GRUB_EFI_LOADER_DATA, pages, &address);
66 + grub_efi_free_pages (0, pages);
67 + if (status != GRUB_EFI_SUCCESS)
71 + return (void *) ((grub_addr_t) address);
74 /* Allocate pages. Return the pointer to the first of allocated pages. */
76 grub_efi_allocate_pages (grub_efi_physical_address_t address,
77 diff --git a/grub-core/loader/i386/efi/linux.c b/grub-core/loader/i386/efi/linux.c
79 index 0000000..b79e632
81 +++ b/grub-core/loader/i386/efi/linux.c
84 + * GRUB -- GRand Unified Bootloader
85 + * Copyright (C) 2012 Free Software Foundation, Inc.
87 + * GRUB is free software: you can redistribute it and/or modify
88 + * it under the terms of the GNU General Public License as published by
89 + * the Free Software Foundation, either version 3 of the License, or
90 + * (at your option) any later version.
92 + * GRUB is distributed in the hope that it will be useful,
93 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
94 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
95 + * GNU General Public License for more details.
97 + * You should have received a copy of the GNU General Public License
98 + * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
101 +#include <grub/loader.h>
102 +#include <grub/file.h>
103 +#include <grub/err.h>
104 +#include <grub/types.h>
105 +#include <grub/mm.h>
106 +#include <grub/cpu/linux.h>
107 +#include <grub/command.h>
108 +#include <grub/i18n.h>
109 +#include <grub/lib/cmdline.h>
110 +#include <grub/efi/efi.h>
112 +GRUB_MOD_LICENSE ("GPLv3+");
114 +static grub_dl_t my_mod;
116 +static void *kernel_mem;
117 +static grub_uint64_t kernel_size;
118 +static grub_uint8_t *initrd_mem;
119 +static grub_uint32_t handover_offset;
120 +struct linux_kernel_params *params;
121 +static char *linux_cmdline;
123 +#define BYTES_TO_PAGES(bytes) (((bytes) + 0xfff) >> 12)
125 +#define SHIM_LOCK_GUID \
126 + { 0x605dab50, 0xe046, 0x4300, {0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23} }
128 +struct grub_efi_shim_lock
130 + grub_efi_status_t (*verify) (void *buffer, grub_uint32_t size);
132 +typedef struct grub_efi_shim_lock grub_efi_shim_lock_t;
134 +static grub_efi_boolean_t
135 +grub_linuxefi_secure_validate (void *data, grub_uint32_t size)
137 + grub_efi_guid_t guid = SHIM_LOCK_GUID;
138 + grub_efi_shim_lock_t *shim_lock;
140 + shim_lock = grub_efi_locate_protocol(&guid, NULL);
145 + if (shim_lock->verify(data, size) == GRUB_EFI_SUCCESS)
151 +typedef void(*handover_func)(void *, grub_efi_system_table_t *, struct linux_kernel_params *);
154 +grub_linuxefi_boot (void)
163 + hf = (handover_func)((char *)kernel_mem + handover_offset + offset);
165 + asm volatile ("cli");
167 + hf (grub_efi_image_handle, grub_efi_system_table, params);
170 + return GRUB_ERR_NONE;
174 +grub_linuxefi_unload (void)
176 + grub_dl_unref (my_mod);
179 + grub_efi_free_pages((grub_efi_physical_address_t)initrd_mem, BYTES_TO_PAGES(params->ramdisk_size));
181 + grub_efi_free_pages((grub_efi_physical_address_t)linux_cmdline, BYTES_TO_PAGES(params->cmdline_size + 1));
183 + grub_efi_free_pages((grub_efi_physical_address_t)kernel_mem, BYTES_TO_PAGES(kernel_size));
185 + grub_efi_free_pages((grub_efi_physical_address_t)params, BYTES_TO_PAGES(16384));
186 + return GRUB_ERR_NONE;
190 +grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
191 + int argc, char *argv[])
193 + grub_file_t *files = 0;
195 + grub_size_t size = 0;
200 + grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
206 + grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first"));
210 + files = grub_zalloc (argc * sizeof (files[0]));
214 + for (i = 0; i < argc; i++)
216 + grub_file_filter_disable_compression ();
217 + files[i] = grub_file_open (argv[i]);
221 + size += ALIGN_UP (grub_file_size (files[i]), 4);
224 + initrd_mem = grub_efi_allocate_pages_max (0x3fffffff, BYTES_TO_PAGES(size));
228 + grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate initrd"));
232 + params->ramdisk_size = size;
233 + params->ramdisk_image = (grub_uint32_t)(grub_uint64_t) initrd_mem;
237 + for (i = 0; i < nfiles; i++)
239 + grub_ssize_t cursize = grub_file_size (files[i]);
240 + if (grub_file_read (files[i], ptr, cursize) != cursize)
243 + grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
248 + grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
249 + ptr += ALIGN_UP_OVERHEAD (cursize, 4);
252 + params->ramdisk_size = size;
255 + for (i = 0; i < nfiles; i++)
256 + grub_file_close (files[i]);
259 + if (initrd_mem && grub_errno)
260 + grub_efi_free_pages((grub_efi_physical_address_t)initrd_mem, BYTES_TO_PAGES(size));
266 +grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
267 + int argc, char *argv[])
269 + grub_file_t file = 0;
270 + struct linux_kernel_header lh;
271 + grub_ssize_t len, start, filelen;
274 + grub_dl_ref (my_mod);
278 + grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
282 + file = grub_file_open (argv[0]);
286 + filelen = grub_file_size (file);
288 + kernel = grub_malloc(filelen);
292 + grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
296 + if (grub_file_read (file, kernel, filelen) != filelen)
298 + grub_error (GRUB_ERR_FILE_READ_ERROR, N_("Can't read kernel %s"), argv[0]);
302 + if (! grub_linuxefi_secure_validate (kernel, filelen))
304 + grub_error (GRUB_ERR_INVALID_COMMAND, N_("%s has invalid signature"), argv[0]);
305 + grub_free (kernel);
309 + grub_file_seek (file, 0);
313 + params = grub_efi_allocate_pages_max (0x3fffffff, BYTES_TO_PAGES(16384));
317 + grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate kernel parameters");
321 + memset (params, 0, 16384);
323 + if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
326 + grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
331 + if (lh.boot_flag != grub_cpu_to_le16 (0xaa55))
333 + grub_error (GRUB_ERR_BAD_OS, N_("invalid magic number"));
337 + if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
339 + grub_error (GRUB_ERR_BAD_OS, N_("too many setup sectors"));
343 + if (lh.version < grub_cpu_to_le16 (0x020b))
345 + grub_error (GRUB_ERR_BAD_OS, N_("kernel too old"));
349 + if (!lh.handover_offset)
351 + grub_error (GRUB_ERR_BAD_OS, N_("kernel doesn't support EFI handover"));
355 + linux_cmdline = grub_efi_allocate_pages_max(0x3fffffff,
356 + BYTES_TO_PAGES(lh.cmdline_size + 1));
358 + if (!linux_cmdline)
360 + grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate cmdline"));
364 + grub_memcpy (linux_cmdline, LINUX_IMAGE, sizeof (LINUX_IMAGE));
365 + grub_create_loader_cmdline (argc, argv,
366 + linux_cmdline + sizeof (LINUX_IMAGE) - 1,
367 + lh.cmdline_size - (sizeof (LINUX_IMAGE) - 1));
369 + lh.cmd_line_ptr = (grub_uint32_t)(grub_uint64_t)linux_cmdline;
371 + handover_offset = lh.handover_offset;
373 + start = (lh.setup_sects + 1) * 512;
374 + len = grub_file_size(file) - start;
376 + kernel_mem = grub_efi_allocate_pages(lh.pref_address,
377 + BYTES_TO_PAGES(lh.init_size));
380 + kernel_mem = grub_efi_allocate_pages_max(0x3fffffff,
381 + BYTES_TO_PAGES(lh.init_size));
385 + grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate kernel"));
389 + if (grub_file_seek (file, start) == (grub_off_t) -1)
391 + grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
396 + if (grub_file_read (file, kernel_mem, len) != len && !grub_errno)
398 + grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
402 + if (grub_errno == GRUB_ERR_NONE)
404 + grub_loader_set (grub_linuxefi_boot, grub_linuxefi_unload, 0);
406 + lh.code32_start = (grub_uint32_t)(grub_uint64_t) kernel_mem;
409 + memcpy(params, &lh, 2 * 512);
411 + params->type_of_loader = 0x21;
416 + grub_file_close (file);
418 + if (grub_errno != GRUB_ERR_NONE)
420 + grub_dl_unref (my_mod);
424 + if (linux_cmdline && !loaded)
425 + grub_efi_free_pages((grub_efi_physical_address_t)linux_cmdline, BYTES_TO_PAGES(lh.cmdline_size + 1));
427 + if (kernel_mem && !loaded)
428 + grub_efi_free_pages((grub_efi_physical_address_t)kernel_mem, BYTES_TO_PAGES(kernel_size));
430 + if (params && !loaded)
431 + grub_efi_free_pages((grub_efi_physical_address_t)params, BYTES_TO_PAGES(16384));
436 +static grub_command_t cmd_linux, cmd_initrd;
438 +GRUB_MOD_INIT(linuxefi)
441 + grub_register_command ("linuxefi", grub_cmd_linux,
442 + 0, N_("Load Linux."));
444 + grub_register_command ("initrdefi", grub_cmd_initrd,
445 + 0, N_("Load initrd."));
449 +GRUB_MOD_FINI(linuxefi)
451 + grub_unregister_command (cmd_linux);
452 + grub_unregister_command (cmd_initrd);
454 diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
455 index 489cf9e..9370fd5 100644
456 --- a/include/grub/efi/efi.h
457 +++ b/include/grub/efi/efi.h
458 @@ -40,6 +40,9 @@ void EXPORT_FUNC(grub_efi_stall) (grub_efi_uintn_t microseconds);
460 EXPORT_FUNC(grub_efi_allocate_pages) (grub_efi_physical_address_t address,
461 grub_efi_uintn_t pages);
463 +EXPORT_FUNC(grub_efi_allocate_pages_max) (grub_efi_physical_address_t max,
464 + grub_efi_uintn_t pages);
465 void EXPORT_FUNC(grub_efi_free_pages) (grub_efi_physical_address_t address,
466 grub_efi_uintn_t pages);
468 diff --git a/include/grub/i386/linux.h b/include/grub/i386/linux.h
469 index 9d064c8..c29c5af 100644
470 --- a/include/grub/i386/linux.h
471 +++ b/include/grub/i386/linux.h
472 @@ -139,6 +139,7 @@ struct linux_kernel_header
473 grub_uint64_t setup_data;
474 grub_uint64_t pref_address;
475 grub_uint32_t init_size;
476 + grub_uint32_t handover_offset;
477 } __attribute__ ((packed));
479 /* Boot parameters for Linux based on 2.6.12. This is used by the setup