]> TLD Linux GIT Repositories - packages/grub2.git/blob - grub2-linuxefi.patch
73a72a0da009ebceea3589a77256f73913a0a8eb
[packages/grub2.git] / grub2-linuxefi.patch
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
5
6 ---
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
14
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 = {
20  };
21  
22  module = {
23 +  name = linuxefi;
24 +  efi = loader/i386/efi/linux.c;
25 +  efi = lib/cmdline.c;
26 +  enable = i386_efi;
27 +  enable = x86_64_efi;
28 +};
29 +
30 +module = {
31    name = chain;
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;
41  
42 +/* Allocate pages below a specified address */
43 +void *
44 +grub_efi_allocate_pages_max (grub_efi_physical_address_t max,
45 +                            grub_efi_uintn_t pages)
46 +{
47 +  grub_efi_status_t status;
48 +  grub_efi_boot_services_t *b;
49 +  grub_efi_physical_address_t address = max;
50 +
51 +  if (max > 0xffffffff)
52 +    return 0;
53 +
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);
56 +
57 +  if (status != GRUB_EFI_SUCCESS)
58 +    return 0;
59 +
60 +  if (address == 0)
61 +    {
62 +      /* Uggh, the address 0 was allocated... This is too annoying,
63 +        so reallocate another one.  */
64 +      address = max;
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)
68 +       return 0;
69 +    }
70 +
71 +  return (void *) ((grub_addr_t) address);
72 +}
73 +
74  /* Allocate pages. Return the pointer to the first of allocated pages.  */
75  void *
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
78 new file mode 100644
79 index 0000000..b79e632
80 --- /dev/null
81 +++ b/grub-core/loader/i386/efi/linux.c
82 @@ -0,0 +1,371 @@
83 +/*
84 + *  GRUB  --  GRand Unified Bootloader
85 + *  Copyright (C) 2012  Free Software Foundation, Inc.
86 + *
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.
91 + *
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.
96 + *
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/>.
99 + */
100 +
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>
111 +
112 +GRUB_MOD_LICENSE ("GPLv3+");
113 +
114 +static grub_dl_t my_mod;
115 +static int loaded;
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;
122 +
123 +#define BYTES_TO_PAGES(bytes)   (((bytes) + 0xfff) >> 12)
124 +
125 +#define SHIM_LOCK_GUID \
126 +  { 0x605dab50, 0xe046, 0x4300, {0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23} }
127 +
128 +struct grub_efi_shim_lock
129 +{
130 +  grub_efi_status_t (*verify) (void *buffer, grub_uint32_t size);
131 +};
132 +typedef struct grub_efi_shim_lock grub_efi_shim_lock_t;
133 +
134 +static grub_efi_boolean_t
135 +grub_linuxefi_secure_validate (void *data, grub_uint32_t size)
136 +{
137 +  grub_efi_guid_t guid = SHIM_LOCK_GUID;
138 +  grub_efi_shim_lock_t *shim_lock;
139 +
140 +  shim_lock = grub_efi_locate_protocol(&guid, NULL);
141 +
142 +  if (!shim_lock)
143 +    return 1;
144 +
145 +  if (shim_lock->verify(data, size) == GRUB_EFI_SUCCESS)
146 +    return 1;
147 +
148 +  return 0;
149 +}
150 +
151 +typedef void(*handover_func)(void *, grub_efi_system_table_t *, struct linux_kernel_params *);
152 +
153 +static grub_err_t
154 +grub_linuxefi_boot (void)
155 +{
156 +  handover_func hf;
157 +  int offset = 0;
158 +
159 +#ifdef __x86_64__
160 +  offset = 512;
161 +#endif
162 +
163 +  hf = (handover_func)((char *)kernel_mem + handover_offset + offset);
164 +
165 +  asm volatile ("cli");
166 +
167 +  hf (grub_efi_image_handle, grub_efi_system_table, params);
168 +
169 +  /* Not reached */
170 +  return GRUB_ERR_NONE;
171 +}
172 +
173 +static grub_err_t
174 +grub_linuxefi_unload (void)
175 +{
176 +  grub_dl_unref (my_mod);
177 +  loaded = 0;
178 +  if (initrd_mem)
179 +    grub_efi_free_pages((grub_efi_physical_address_t)initrd_mem, BYTES_TO_PAGES(params->ramdisk_size));
180 +  if (linux_cmdline)
181 +    grub_efi_free_pages((grub_efi_physical_address_t)linux_cmdline, BYTES_TO_PAGES(params->cmdline_size + 1));
182 +  if (kernel_mem)
183 +    grub_efi_free_pages((grub_efi_physical_address_t)kernel_mem, BYTES_TO_PAGES(kernel_size));
184 +  if (params)
185 +    grub_efi_free_pages((grub_efi_physical_address_t)params, BYTES_TO_PAGES(16384));
186 +  return GRUB_ERR_NONE;
187 +}
188 +
189 +static grub_err_t
190 +grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
191 +                 int argc, char *argv[])
192 +{
193 +  grub_file_t *files = 0;
194 +  int i, nfiles = 0;
195 +  grub_size_t size = 0;
196 +  grub_uint8_t *ptr;
197 +
198 +  if (argc == 0)
199 +    {
200 +      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
201 +      goto fail;
202 +    }
203 +
204 +  if (!loaded)
205 +    {
206 +      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first"));
207 +      goto fail;
208 +    }
209 +
210 +  files = grub_zalloc (argc * sizeof (files[0]));
211 +  if (!files)
212 +    goto fail;
213 +
214 +  for (i = 0; i < argc; i++)
215 +    {
216 +      grub_file_filter_disable_compression ();
217 +      files[i] = grub_file_open (argv[i]);
218 +      if (! files[i])
219 +        goto fail;
220 +      nfiles++;
221 +      size += ALIGN_UP (grub_file_size (files[i]), 4);
222 +    }
223 +
224 +  initrd_mem = grub_efi_allocate_pages_max (0x3fffffff, BYTES_TO_PAGES(size));
225 +
226 +  if (!initrd_mem)
227 +    {
228 +      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate initrd"));
229 +      goto fail;
230 +    }
231 +
232 +  params->ramdisk_size = size;
233 +  params->ramdisk_image = (grub_uint32_t)(grub_uint64_t) initrd_mem;
234 +
235 +  ptr = initrd_mem;
236 +
237 +  for (i = 0; i < nfiles; i++)
238 +    {
239 +      grub_ssize_t cursize = grub_file_size (files[i]);
240 +      if (grub_file_read (files[i], ptr, cursize) != cursize)
241 +        {
242 +          if (!grub_errno)
243 +            grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
244 +                        argv[i]);
245 +          goto fail;
246 +        }
247 +      ptr += cursize;
248 +      grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
249 +      ptr += ALIGN_UP_OVERHEAD (cursize, 4);
250 +    }
251 +
252 +  params->ramdisk_size = size;
253 +
254 + fail:
255 +  for (i = 0; i < nfiles; i++)
256 +    grub_file_close (files[i]);
257 +  grub_free (files);
258 +
259 +  if (initrd_mem && grub_errno)
260 +    grub_efi_free_pages((grub_efi_physical_address_t)initrd_mem, BYTES_TO_PAGES(size));
261 +
262 +  return grub_errno;
263 +}
264 +
265 +static grub_err_t
266 +grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
267 +               int argc, char *argv[])
268 +{
269 +  grub_file_t file = 0;
270 +  struct linux_kernel_header lh;
271 +  grub_ssize_t len, start, filelen;
272 +  void *kernel;
273 +
274 +  grub_dl_ref (my_mod);
275 +
276 +  if (argc == 0)
277 +    {
278 +      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
279 +      goto fail;
280 +    }
281 +
282 +  file = grub_file_open (argv[0]);
283 +  if (! file)
284 +    goto fail;
285 +
286 +  filelen = grub_file_size (file);
287 +
288 +  kernel = grub_malloc(filelen);
289 +
290 +  if (!kernel)
291 +    {
292 +      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
293 +      goto fail;
294 +    }
295 +
296 +  if (grub_file_read (file, kernel, filelen) != filelen)
297 +    {
298 +      grub_error (GRUB_ERR_FILE_READ_ERROR, N_("Can't read kernel %s"), argv[0]);
299 +      goto fail;
300 +    }
301 +
302 +  if (! grub_linuxefi_secure_validate (kernel, filelen))
303 +    {
304 +      grub_error (GRUB_ERR_INVALID_COMMAND, N_("%s has invalid signature"), argv[0]);
305 +      grub_free (kernel);
306 +      goto fail;
307 +    }
308 +
309 +  grub_file_seek (file, 0);
310 +
311 +  grub_free(kernel);
312 +
313 +  params = grub_efi_allocate_pages_max (0x3fffffff, BYTES_TO_PAGES(16384));
314 +
315 +  if (! params)
316 +    {
317 +      grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate kernel parameters");
318 +      goto fail;
319 +    }
320 +
321 +  memset (params, 0, 16384);
322 +
323 +  if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
324 +    {
325 +      if (!grub_errno)
326 +       grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
327 +                   argv[0]);
328 +      goto fail;
329 +    }
330 +
331 +  if (lh.boot_flag != grub_cpu_to_le16 (0xaa55))
332 +    {
333 +      grub_error (GRUB_ERR_BAD_OS, N_("invalid magic number"));
334 +      goto fail;
335 +    }
336 +
337 +  if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
338 +    {
339 +      grub_error (GRUB_ERR_BAD_OS, N_("too many setup sectors"));
340 +      goto fail;
341 +    }
342 +
343 +  if (lh.version < grub_cpu_to_le16 (0x020b))
344 +    {
345 +      grub_error (GRUB_ERR_BAD_OS, N_("kernel too old"));
346 +      goto fail;
347 +    }
348 +
349 +  if (!lh.handover_offset)
350 +    {
351 +      grub_error (GRUB_ERR_BAD_OS, N_("kernel doesn't support EFI handover"));
352 +      goto fail;
353 +    }
354 +
355 +  linux_cmdline = grub_efi_allocate_pages_max(0x3fffffff,
356 +                                        BYTES_TO_PAGES(lh.cmdline_size + 1));
357 +
358 +  if (!linux_cmdline)
359 +    {
360 +      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate cmdline"));
361 +      goto fail;
362 +    }
363 +
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));
368 +
369 +  lh.cmd_line_ptr = (grub_uint32_t)(grub_uint64_t)linux_cmdline;
370 +
371 +  handover_offset = lh.handover_offset;
372 +
373 +  start = (lh.setup_sects + 1) * 512;
374 +  len = grub_file_size(file) - start;
375 +
376 +  kernel_mem = grub_efi_allocate_pages(lh.pref_address,
377 +                                      BYTES_TO_PAGES(lh.init_size));
378 +
379 +  if (!kernel_mem)
380 +    kernel_mem = grub_efi_allocate_pages_max(0x3fffffff,
381 +                                            BYTES_TO_PAGES(lh.init_size));
382 +
383 +  if (!kernel_mem)
384 +    {
385 +      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate kernel"));
386 +      goto fail;
387 +    }
388 +
389 +  if (grub_file_seek (file, start) == (grub_off_t) -1)
390 +    {
391 +      grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
392 +                 argv[0]);
393 +      goto fail;
394 +    }
395 +
396 +  if (grub_file_read (file, kernel_mem, len) != len && !grub_errno)
397 +    {
398 +      grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
399 +                 argv[0]);
400 +    }
401 +
402 +  if (grub_errno == GRUB_ERR_NONE)
403 +    {
404 +      grub_loader_set (grub_linuxefi_boot, grub_linuxefi_unload, 0);
405 +      loaded = 1;
406 +      lh.code32_start = (grub_uint32_t)(grub_uint64_t) kernel_mem;
407 +    }
408 +
409 +  memcpy(params, &lh, 2 * 512);
410 +
411 +  params->type_of_loader = 0x21;
412 +
413 + fail:
414 +
415 +  if (file)
416 +    grub_file_close (file);
417 +
418 +  if (grub_errno != GRUB_ERR_NONE)
419 +    {
420 +      grub_dl_unref (my_mod);
421 +      loaded = 0;
422 +    }
423 +
424 +  if (linux_cmdline && !loaded)
425 +    grub_efi_free_pages((grub_efi_physical_address_t)linux_cmdline, BYTES_TO_PAGES(lh.cmdline_size + 1));
426 +
427 +  if (kernel_mem && !loaded)
428 +    grub_efi_free_pages((grub_efi_physical_address_t)kernel_mem, BYTES_TO_PAGES(kernel_size));
429 +
430 +  if (params && !loaded)
431 +    grub_efi_free_pages((grub_efi_physical_address_t)params, BYTES_TO_PAGES(16384));
432 +
433 +  return grub_errno;
434 +}
435 +
436 +static grub_command_t cmd_linux, cmd_initrd;
437 +
438 +GRUB_MOD_INIT(linuxefi)
439 +{
440 +  cmd_linux =
441 +    grub_register_command ("linuxefi", grub_cmd_linux,
442 +                           0, N_("Load Linux."));
443 +  cmd_initrd =
444 +    grub_register_command ("initrdefi", grub_cmd_initrd,
445 +                           0, N_("Load initrd."));
446 +  my_mod = mod;
447 +}
448 +
449 +GRUB_MOD_FINI(linuxefi)
450 +{
451 +  grub_unregister_command (cmd_linux);
452 +  grub_unregister_command (cmd_initrd);
453 +}
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);
459  void *
460  EXPORT_FUNC(grub_efi_allocate_pages) (grub_efi_physical_address_t address,
461                                       grub_efi_uintn_t pages);
462 +void *
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);
467  int
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));
478  
479  /* Boot parameters for Linux based on 2.6.12. This is used by the setup
480 -- 
481 1.8.2.1
482