All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Add linuxefi module
@ 2014-01-20 23:28 Lubomir Rintel
  2014-01-21  1:46 ` SevenBits
  2014-01-21 16:24 ` Andrey Borzenkov
  0 siblings, 2 replies; 11+ messages in thread
From: Lubomir Rintel @ 2014-01-20 23:28 UTC (permalink / raw)
  To: grub-devel; +Cc: Matthew Garrett

From: Matthew Garrett <matthew.garrett@nebula.com>

This adds linuxefi module that provides a way to load Linux kernel and RAM disk
image via EFI services with linuxefi and initrdefi commands, analogous to linux
and initrd commands.

[lkundrak@v3.sk: Clarify the commit message]
[lkundrak@v3.sk: Add Changelog]
---
Hi,

this is taken from Fedora (and RHEL) package as it is. I've only done minor 
changes (described in commit message).

Please have a look and let me know if there's anything I could do to have his 
mainlined, so that we can get rid of the pile of patches we ship in Fedora.
Other Linux distributions interested in Linux on EFI support may find this 
useful too.

Thank you!
Lubo

 ChangeLog                         |   9 +
 grub-core/Makefile.core.def       |   8 +
 grub-core/kern/efi/mm.c           |  32 ++++
 grub-core/loader/i386/efi/linux.c | 371 ++++++++++++++++++++++++++++++++++++++
 include/grub/efi/efi.h            |   3 +
 include/grub/i386/linux.h         |   1 +
 6 files changed, 424 insertions(+)
 create mode 100644 grub-core/loader/i386/efi/linux.c

diff --git a/ChangeLog b/ChangeLog
index 10abfe2..432c786 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2014-01-20  Matthew Garrett  <matthew.garrett@nebula.com>
+
+	* grub-core/Makefile.core.def: Add linuxefi module.
+	* grub-core/kern/efi/mm.c (grub_efi_allocate_pages_max): Add.
+	* grub-core/loader/i386/efi/linux.c: Add.
+	* include/grub/efi/efi.h: Prototype for grub_efi_allocate_pages_max.
+	* include/grub/i386/linux.h (struct linux_kernel_header): Add
+	handover_offset.
+
 2014-01-19  Colin Watson  <cjwatson@ubuntu.com>
 
 	* grub-core/osdep/freebsd/hostdisk.c (grub_util_fd_open): Ignore
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 42443bc..ec46506 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1706,6 +1706,14 @@ module = {
 };
 
 module = {
+  name = linuxefi;
+  efi = loader/i386/efi/linux.c;
+  efi = lib/cmdline.c;
+  enable = i386_efi;
+  enable = x86_64_efi;
+};
+
+module = {
   name = chain;
   efi = loader/efi/chainloader.c;
   i386_pc = loader/i386/pc/chainloader.c;
diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
index be37afd..ddeca60 100644
--- a/grub-core/kern/efi/mm.c
+++ b/grub-core/kern/efi/mm.c
@@ -49,6 +49,38 @@ static grub_efi_uintn_t finish_desc_size;
 static grub_efi_uint32_t finish_desc_version;
 int grub_efi_is_finished = 0;
 
+/* Allocate pages below a specified address */
+void *
+grub_efi_allocate_pages_max (grub_efi_physical_address_t max,
+			     grub_efi_uintn_t pages)
+{
+  grub_efi_status_t status;
+  grub_efi_boot_services_t *b;
+  grub_efi_physical_address_t address = max;
+
+  if (max > 0xffffffff)
+    return 0;
+
+  b = grub_efi_system_table->boot_services;
+  status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS, GRUB_EFI_LOADER_DATA, pages, &address);
+
+  if (status != GRUB_EFI_SUCCESS)
+    return 0;
+
+  if (address == 0)
+    {
+      /* Uggh, the address 0 was allocated... This is too annoying,
+	 so reallocate another one.  */
+      address = max;
+      status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS, GRUB_EFI_LOADER_DATA, pages, &address);
+      grub_efi_free_pages (0, pages);
+      if (status != GRUB_EFI_SUCCESS)
+	return 0;
+    }
+
+  return (void *) ((grub_addr_t) address);
+}
+
 /* Allocate pages. Return the pointer to the first of allocated pages.  */
 void *
 grub_efi_allocate_pages (grub_efi_physical_address_t address,
diff --git a/grub-core/loader/i386/efi/linux.c b/grub-core/loader/i386/efi/linux.c
new file mode 100644
index 0000000..b79e632
--- /dev/null
+++ b/grub-core/loader/i386/efi/linux.c
@@ -0,0 +1,371 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2012  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/loader.h>
+#include <grub/file.h>
+#include <grub/err.h>
+#include <grub/types.h>
+#include <grub/mm.h>
+#include <grub/cpu/linux.h>
+#include <grub/command.h>
+#include <grub/i18n.h>
+#include <grub/lib/cmdline.h>
+#include <grub/efi/efi.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static grub_dl_t my_mod;
+static int loaded;
+static void *kernel_mem;
+static grub_uint64_t kernel_size;
+static grub_uint8_t *initrd_mem;
+static grub_uint32_t handover_offset;
+struct linux_kernel_params *params;
+static char *linux_cmdline;
+
+#define BYTES_TO_PAGES(bytes)   (((bytes) + 0xfff) >> 12)
+
+#define SHIM_LOCK_GUID \
+  { 0x605dab50, 0xe046, 0x4300, {0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23} }
+
+struct grub_efi_shim_lock
+{
+  grub_efi_status_t (*verify) (void *buffer, grub_uint32_t size);
+};
+typedef struct grub_efi_shim_lock grub_efi_shim_lock_t;
+
+static grub_efi_boolean_t
+grub_linuxefi_secure_validate (void *data, grub_uint32_t size)
+{
+  grub_efi_guid_t guid = SHIM_LOCK_GUID;
+  grub_efi_shim_lock_t *shim_lock;
+
+  shim_lock = grub_efi_locate_protocol(&guid, NULL);
+
+  if (!shim_lock)
+    return 1;
+
+  if (shim_lock->verify(data, size) == GRUB_EFI_SUCCESS)
+    return 1;
+
+  return 0;
+}
+
+typedef void(*handover_func)(void *, grub_efi_system_table_t *, struct linux_kernel_params *);
+
+static grub_err_t
+grub_linuxefi_boot (void)
+{
+  handover_func hf;
+  int offset = 0;
+
+#ifdef __x86_64__
+  offset = 512;
+#endif
+
+  hf = (handover_func)((char *)kernel_mem + handover_offset + offset);
+
+  asm volatile ("cli");
+
+  hf (grub_efi_image_handle, grub_efi_system_table, params);
+
+  /* Not reached */
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_linuxefi_unload (void)
+{
+  grub_dl_unref (my_mod);
+  loaded = 0;
+  if (initrd_mem)
+    grub_efi_free_pages((grub_efi_physical_address_t)initrd_mem, BYTES_TO_PAGES(params->ramdisk_size));
+  if (linux_cmdline)
+    grub_efi_free_pages((grub_efi_physical_address_t)linux_cmdline, BYTES_TO_PAGES(params->cmdline_size + 1));
+  if (kernel_mem)
+    grub_efi_free_pages((grub_efi_physical_address_t)kernel_mem, BYTES_TO_PAGES(kernel_size));
+  if (params)
+    grub_efi_free_pages((grub_efi_physical_address_t)params, BYTES_TO_PAGES(16384));
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
+                 int argc, char *argv[])
+{
+  grub_file_t *files = 0;
+  int i, nfiles = 0;
+  grub_size_t size = 0;
+  grub_uint8_t *ptr;
+
+  if (argc == 0)
+    {
+      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
+      goto fail;
+    }
+
+  if (!loaded)
+    {
+      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first"));
+      goto fail;
+    }
+
+  files = grub_zalloc (argc * sizeof (files[0]));
+  if (!files)
+    goto fail;
+
+  for (i = 0; i < argc; i++)
+    {
+      grub_file_filter_disable_compression ();
+      files[i] = grub_file_open (argv[i]);
+      if (! files[i])
+        goto fail;
+      nfiles++;
+      size += ALIGN_UP (grub_file_size (files[i]), 4);
+    }
+
+  initrd_mem = grub_efi_allocate_pages_max (0x3fffffff, BYTES_TO_PAGES(size));
+
+  if (!initrd_mem)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate initrd"));
+      goto fail;
+    }
+
+  params->ramdisk_size = size;
+  params->ramdisk_image = (grub_uint32_t)(grub_uint64_t) initrd_mem;
+
+  ptr = initrd_mem;
+
+  for (i = 0; i < nfiles; i++)
+    {
+      grub_ssize_t cursize = grub_file_size (files[i]);
+      if (grub_file_read (files[i], ptr, cursize) != cursize)
+        {
+          if (!grub_errno)
+            grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
+                        argv[i]);
+          goto fail;
+        }
+      ptr += cursize;
+      grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
+      ptr += ALIGN_UP_OVERHEAD (cursize, 4);
+    }
+
+  params->ramdisk_size = size;
+
+ fail:
+  for (i = 0; i < nfiles; i++)
+    grub_file_close (files[i]);
+  grub_free (files);
+
+  if (initrd_mem && grub_errno)
+    grub_efi_free_pages((grub_efi_physical_address_t)initrd_mem, BYTES_TO_PAGES(size));
+
+  return grub_errno;
+}
+
+static grub_err_t
+grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
+		int argc, char *argv[])
+{
+  grub_file_t file = 0;
+  struct linux_kernel_header lh;
+  grub_ssize_t len, start, filelen;
+  void *kernel;
+
+  grub_dl_ref (my_mod);
+
+  if (argc == 0)
+    {
+      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
+      goto fail;
+    }
+
+  file = grub_file_open (argv[0]);
+  if (! file)
+    goto fail;
+
+  filelen = grub_file_size (file);
+
+  kernel = grub_malloc(filelen);
+
+  if (!kernel)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
+      goto fail;
+    }
+
+  if (grub_file_read (file, kernel, filelen) != filelen)
+    {
+      grub_error (GRUB_ERR_FILE_READ_ERROR, N_("Can't read kernel %s"), argv[0]);
+      goto fail;
+    }
+
+  if (! grub_linuxefi_secure_validate (kernel, filelen))
+    {
+      grub_error (GRUB_ERR_INVALID_COMMAND, N_("%s has invalid signature"), argv[0]);
+      grub_free (kernel);
+      goto fail;
+    }
+
+  grub_file_seek (file, 0);
+
+  grub_free(kernel);
+
+  params = grub_efi_allocate_pages_max (0x3fffffff, BYTES_TO_PAGES(16384));
+
+  if (! params)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate kernel parameters");
+      goto fail;
+    }
+
+  memset (params, 0, 16384);
+
+  if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
+    {
+      if (!grub_errno)
+	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
+		    argv[0]);
+      goto fail;
+    }
+
+  if (lh.boot_flag != grub_cpu_to_le16 (0xaa55))
+    {
+      grub_error (GRUB_ERR_BAD_OS, N_("invalid magic number"));
+      goto fail;
+    }
+
+  if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
+    {
+      grub_error (GRUB_ERR_BAD_OS, N_("too many setup sectors"));
+      goto fail;
+    }
+
+  if (lh.version < grub_cpu_to_le16 (0x020b))
+    {
+      grub_error (GRUB_ERR_BAD_OS, N_("kernel too old"));
+      goto fail;
+    }
+
+  if (!lh.handover_offset)
+    {
+      grub_error (GRUB_ERR_BAD_OS, N_("kernel doesn't support EFI handover"));
+      goto fail;
+    }
+
+  linux_cmdline = grub_efi_allocate_pages_max(0x3fffffff,
+					 BYTES_TO_PAGES(lh.cmdline_size + 1));
+
+  if (!linux_cmdline)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate cmdline"));
+      goto fail;
+    }
+
+  grub_memcpy (linux_cmdline, LINUX_IMAGE, sizeof (LINUX_IMAGE));
+  grub_create_loader_cmdline (argc, argv,
+                              linux_cmdline + sizeof (LINUX_IMAGE) - 1,
+			      lh.cmdline_size - (sizeof (LINUX_IMAGE) - 1));
+
+  lh.cmd_line_ptr = (grub_uint32_t)(grub_uint64_t)linux_cmdline;
+
+  handover_offset = lh.handover_offset;
+
+  start = (lh.setup_sects + 1) * 512;
+  len = grub_file_size(file) - start;
+
+  kernel_mem = grub_efi_allocate_pages(lh.pref_address,
+				       BYTES_TO_PAGES(lh.init_size));
+
+  if (!kernel_mem)
+    kernel_mem = grub_efi_allocate_pages_max(0x3fffffff,
+					     BYTES_TO_PAGES(lh.init_size));
+
+  if (!kernel_mem)
+    {
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate kernel"));
+      goto fail;
+    }
+
+  if (grub_file_seek (file, start) == (grub_off_t) -1)
+    {
+      grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
+		  argv[0]);
+      goto fail;
+    }
+
+  if (grub_file_read (file, kernel_mem, len) != len && !grub_errno)
+    {
+      grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
+		  argv[0]);
+    }
+
+  if (grub_errno == GRUB_ERR_NONE)
+    {
+      grub_loader_set (grub_linuxefi_boot, grub_linuxefi_unload, 0);
+      loaded = 1;
+      lh.code32_start = (grub_uint32_t)(grub_uint64_t) kernel_mem;
+    }
+
+  memcpy(params, &lh, 2 * 512);
+
+  params->type_of_loader = 0x21;
+
+ fail:
+
+  if (file)
+    grub_file_close (file);
+
+  if (grub_errno != GRUB_ERR_NONE)
+    {
+      grub_dl_unref (my_mod);
+      loaded = 0;
+    }
+
+  if (linux_cmdline && !loaded)
+    grub_efi_free_pages((grub_efi_physical_address_t)linux_cmdline, BYTES_TO_PAGES(lh.cmdline_size + 1));
+
+  if (kernel_mem && !loaded)
+    grub_efi_free_pages((grub_efi_physical_address_t)kernel_mem, BYTES_TO_PAGES(kernel_size));
+
+  if (params && !loaded)
+    grub_efi_free_pages((grub_efi_physical_address_t)params, BYTES_TO_PAGES(16384));
+
+  return grub_errno;
+}
+
+static grub_command_t cmd_linux, cmd_initrd;
+
+GRUB_MOD_INIT(linuxefi)
+{
+  cmd_linux =
+    grub_register_command ("linuxefi", grub_cmd_linux,
+                           0, N_("Load Linux."));
+  cmd_initrd =
+    grub_register_command ("initrdefi", grub_cmd_initrd,
+                           0, N_("Load initrd."));
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI(linuxefi)
+{
+  grub_unregister_command (cmd_linux);
+  grub_unregister_command (cmd_initrd);
+}
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
index 489cf9e..9370fd5 100644
--- a/include/grub/efi/efi.h
+++ b/include/grub/efi/efi.h
@@ -40,6 +40,9 @@ void EXPORT_FUNC(grub_efi_stall) (grub_efi_uintn_t microseconds);
 void *
 EXPORT_FUNC(grub_efi_allocate_pages) (grub_efi_physical_address_t address,
 				      grub_efi_uintn_t pages);
+void *
+EXPORT_FUNC(grub_efi_allocate_pages_max) (grub_efi_physical_address_t max,
+					  grub_efi_uintn_t pages);
 void EXPORT_FUNC(grub_efi_free_pages) (grub_efi_physical_address_t address,
 				       grub_efi_uintn_t pages);
 int
diff --git a/include/grub/i386/linux.h b/include/grub/i386/linux.h
index da0ca3b..fc36bda 100644
--- a/include/grub/i386/linux.h
+++ b/include/grub/i386/linux.h
@@ -139,6 +139,7 @@ struct linux_kernel_header
   grub_uint64_t setup_data;
   grub_uint64_t pref_address;
   grub_uint32_t init_size;
+  grub_uint32_t handover_offset;
 } GRUB_PACKED;
 
 /* Boot parameters for Linux based on 2.6.12. This is used by the setup
-- 
1.8.3.1



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add linuxefi module
  2014-01-20 23:28 [PATCH] Add linuxefi module Lubomir Rintel
@ 2014-01-21  1:46 ` SevenBits
  2014-01-21 13:43   ` Colin Watson
  2014-01-21 16:24 ` Andrey Borzenkov
  1 sibling, 1 reply; 11+ messages in thread
From: SevenBits @ 2014-01-21  1:46 UTC (permalink / raw)
  To: The development of GNU GRUB

[-- Attachment #1: Type: text/plain, Size: 14560 bytes --]

On Monday, January 20, 2014, Lubomir Rintel <lkundrak@v3.sk> wrote:

> From: Matthew Garrett <matthew.garrett@nebula.com>
>
> This adds linuxefi module that provides a way to load Linux kernel and RAM
> disk
> image via EFI services with linuxefi and initrdefi commands, analogous to
> linux
> and initrd commands.


Why? What's wrong with the standard linux and initrd commands? They work
just fine under UEFI.


>
> [lkundrak@v3.sk: Clarify the commit message]
> [lkundrak@v3.sk: Add Changelog]
> ---
> Hi,
>
> this is taken from Fedora (and RHEL) package as it is. I've only done minor
> changes (described in commit message).
>
> Please have a look and let me know if there's anything I could do to have
> his
> mainlined, so that we can get rid of the pile of patches we ship in Fedora.
> Other Linux distributions interested in Linux on EFI support may find this
> useful too.
>
> Thank you!
> Lubo
>
>  ChangeLog                         |   9 +
>  grub-core/Makefile.core.def       |   8 +
>  grub-core/kern/efi/mm.c           |  32 ++++
>  grub-core/loader/i386/efi/linux.c | 371
> ++++++++++++++++++++++++++++++++++++++
>  include/grub/efi/efi.h            |   3 +
>  include/grub/i386/linux.h         |   1 +
>  6 files changed, 424 insertions(+)
>  create mode 100644 grub-core/loader/i386/efi/linux.c
>
> diff --git a/ChangeLog b/ChangeLog
> index 10abfe2..432c786 100644
> --- a/ChangeLog
> +++ b/ChangeLog
> @@ -1,3 +1,12 @@
> +2014-01-20  Matthew Garrett  <matthew.garrett@nebula.com>
> +
> +       * grub-core/Makefile.core.def: Add linuxefi module.
> +       * grub-core/kern/efi/mm.c (grub_efi_allocate_pages_max): Add.
> +       * grub-core/loader/i386/efi/linux.c: Add.
> +       * include/grub/efi/efi.h: Prototype for
> grub_efi_allocate_pages_max.
> +       * include/grub/i386/linux.h (struct linux_kernel_header): Add
> +       handover_offset.
> +
>  2014-01-19  Colin Watson  <cjwatson@ubuntu.com>
>
>         * grub-core/osdep/freebsd/hostdisk.c (grub_util_fd_open): Ignore
> diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
> index 42443bc..ec46506 100644
> --- a/grub-core/Makefile.core.def
> +++ b/grub-core/Makefile.core.def
> @@ -1706,6 +1706,14 @@ module = {
>  };
>
>  module = {
> +  name = linuxefi;
> +  efi = loader/i386/efi/linux.c;
> +  efi = lib/cmdline.c;
> +  enable = i386_efi;
> +  enable = x86_64_efi;
> +};
> +
> +module = {
>    name = chain;
>    efi = loader/efi/chainloader.c;
>    i386_pc = loader/i386/pc/chainloader.c;
> diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
> index be37afd..ddeca60 100644
> --- a/grub-core/kern/efi/mm.c
> +++ b/grub-core/kern/efi/mm.c
> @@ -49,6 +49,38 @@ static grub_efi_uintn_t finish_desc_size;
>  static grub_efi_uint32_t finish_desc_version;
>  int grub_efi_is_finished = 0;
>
> +/* Allocate pages below a specified address */
> +void *
> +grub_efi_allocate_pages_max (grub_efi_physical_address_t max,
> +                            grub_efi_uintn_t pages)
> +{
> +  grub_efi_status_t status;
> +  grub_efi_boot_services_t *b;
> +  grub_efi_physical_address_t address = max;
> +
> +  if (max > 0xffffffff)
> +    return 0;
> +
> +  b = grub_efi_system_table->boot_services;
> +  status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS,
> GRUB_EFI_LOADER_DATA, pages, &address);
> +
> +  if (status != GRUB_EFI_SUCCESS)
> +    return 0;
> +
> +  if (address == 0)
> +    {
> +      /* Uggh, the address 0 was allocated... This is too annoying,
> +        so reallocate another one.  */
> +      address = max;
> +      status = efi_call_4 (b->allocate_pages,
> GRUB_EFI_ALLOCATE_MAX_ADDRESS, GRUB_EFI_LOADER_DATA, pages, &address);
> +      grub_efi_free_pages (0, pages);
> +      if (status != GRUB_EFI_SUCCESS)
> +       return 0;
> +    }
> +
> +  return (void *) ((grub_addr_t) address);
> +}
> +
>  /* Allocate pages. Return the pointer to the first of allocated pages.  */
>  void *
>  grub_efi_allocate_pages (grub_efi_physical_address_t address,
> diff --git a/grub-core/loader/i386/efi/linux.c
> b/grub-core/loader/i386/efi/linux.c
> new file mode 100644
> index 0000000..b79e632
> --- /dev/null
> +++ b/grub-core/loader/i386/efi/linux.c
> @@ -0,0 +1,371 @@
> +/*
> + *  GRUB  --  GRand Unified Bootloader
> + *  Copyright (C) 2012  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/loader.h>
> +#include <grub/file.h>
> +#include <grub/err.h>
> +#include <grub/types.h>
> +#include <grub/mm.h>
> +#include <grub/cpu/linux.h>
> +#include <grub/command.h>
> +#include <grub/i18n.h>
> +#include <grub/lib/cmdline.h>
> +#include <grub/efi/efi.h>
> +
> +GRUB_MOD_LICENSE ("GPLv3+");
> +
> +static grub_dl_t my_mod;
> +static int loaded;
> +static void *kernel_mem;
> +static grub_uint64_t kernel_size;
> +static grub_uint8_t *initrd_mem;
> +static grub_uint32_t handover_offset;
> +struct linux_kernel_params *params;
> +static char *linux_cmdline;
> +
> +#define BYTES_TO_PAGES(bytes)   (((bytes) + 0xfff) >> 12)
> +
> +#define SHIM_LOCK_GUID \
> +  { 0x605dab50, 0xe046, 0x4300, {0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd,
> 0x8b, 0x23} }
> +
> +struct grub_efi_shim_lock
> +{
> +  grub_efi_status_t (*verify) (void *buffer, grub_uint32_t size);
> +};
> +typedef struct grub_efi_shim_lock grub_efi_shim_lock_t;
> +
> +static grub_efi_boolean_t
> +grub_linuxefi_secure_validate (void *data, grub_uint32_t size)
> +{
> +  grub_efi_guid_t guid = SHIM_LOCK_GUID;
> +  grub_efi_shim_lock_t *shim_lock;
> +
> +  shim_lock = grub_efi_locate_protocol(&guid, NULL);
> +
> +  if (!shim_lock)
> +    return 1;
> +
> +  if (shim_lock->verify(data, size) == GRUB_EFI_SUCCESS)
> +    return 1;
> +
> +  return 0;
> +}
> +
> +typedef void(*handover_func)(void *, grub_efi_system_table_t *, struct
> linux_kernel_params *);
> +
> +static grub_err_t
> +grub_linuxefi_boot (void)
> +{
> +  handover_func hf;
> +  int offset = 0;
> +
> +#ifdef __x86_64__
> +  offset = 512;
> +#endif
> +
> +  hf = (handover_func)((char *)kernel_mem + handover_offset + offset);
> +
> +  asm volatile ("cli");
> +
> +  hf (grub_efi_image_handle, grub_efi_system_table, params);
> +
> +  /* Not reached */
> +  return GRUB_ERR_NONE;
> +}
> +
> +static grub_err_t
> +grub_linuxefi_unload (void)
> +{
> +  grub_dl_unref (my_mod);
> +  loaded = 0;
> +  if (initrd_mem)
> +    grub_efi_free_pages((grub_efi_physical_address_t)initrd_mem,
> BYTES_TO_PAGES(params->ramdisk_size));
> +  if (linux_cmdline)
> +    grub_efi_free_pages((grub_efi_physical_address_t)linux_cmdline,
> BYTES_TO_PAGES(params->cmdline_size + 1));
> +  if (kernel_mem)
> +    grub_efi_free_pages((grub_efi_physical_address_t)kernel_mem,
> BYTES_TO_PAGES(kernel_size));
> +  if (params)
> +    grub_efi_free_pages((grub_efi_physical_address_t)params,
> BYTES_TO_PAGES(16384));
> +  return GRUB_ERR_NONE;
> +}
> +
> +static grub_err_t
> +grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
> +                 int argc, char *argv[])
> +{
> +  grub_file_t *files = 0;
> +  int i, nfiles = 0;
> +  grub_size_t size = 0;
> +  grub_uint8_t *ptr;
> +
> +  if (argc == 0)
> +    {
> +      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
> +      goto fail;
> +    }
> +
> +  if (!loaded)
> +    {
> +      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel
> first"));
> +      goto fail;
> +    }
> +
> +  files = grub_zalloc (argc * sizeof (files[0]));
> +  if (!files)
> +    goto fail;
> +
> +  for (i = 0; i < argc; i++)
> +    {
> +      grub_file_filter_disable_compression ();
> +      files[i] = grub_file_open (argv[i]);
> +      if (! files[i])
> +        goto fail;
> +      nfiles++;
> +      size += ALIGN_UP (grub_file_size (files[i]), 4);
> +    }
> +
> +  initrd_mem = grub_efi_allocate_pages_max (0x3fffffff,
> BYTES_TO_PAGES(size));
> +
> +  if (!initrd_mem)
> +    {
> +      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate initrd"));
> +      goto fail;
> +    }
> +
> +  params->ramdisk_size = size;
> +  params->ramdisk_image = (grub_uint32_t)(grub_uint64_t) initrd_mem;
> +
> +  ptr = initrd_mem;
> +
> +  for (i = 0; i < nfiles; i++)
> +    {
> +      grub_ssize_t cursize = grub_file_size (files[i]);
> +      if (grub_file_read (files[i], ptr, cursize) != cursize)
> +        {
> +          if (!grub_errno)
> +            grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of
> file %s"),
> +                        argv[i]);
> +          goto fail;
> +        }
> +      ptr += cursize;
> +      grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
> +      ptr += ALIGN_UP_OVERHEAD (cursize, 4);
> +    }
> +
> +  params->ramdisk_size = size;
> +
> + fail:
> +  for (i = 0; i < nfiles; i++)
> +    grub_file_close (files[i]);
> +  grub_free (files);
> +
> +  if (initrd_mem && grub_errno)
> +    grub_efi_free_pages((grub_efi_physical_address_t)initrd_mem,
> BYTES_TO_PAGES(size));
> +
> +  return grub_errno;
> +}
> +
> +static grub_err_t
> +grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
> +               int argc, char *argv[])
> +{
> +  grub_file_t file = 0;
> +  struct linux_kernel_header lh;
> +  grub_ssize_t len, start, filelen;
> +  void *kernel;
> +
> +  grub_dl_ref (my_mod);
> +
> +  if (argc == 0)
> +    {
> +      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
> +      goto fail;
> +    }
> +
> +  file = grub_file_open (argv[0]);
> +  if (! file)
> +    goto fail;
> +
> +  filelen = grub_file_size (file);
> +
> +  kernel = grub_malloc(filelen);
> +
> +  if (!kernel)
> +    {
> +      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel
> buffer"));
> +      goto fail;
> +    }
> +
> +  if (grub_file_read (file, kernel, filelen) != filelen)
> +    {
> +      grub_error (GRUB_ERR_FILE_READ_ERROR, N_("Can't read kernel %s"),
> argv[0]);
> +      goto fail;
> +    }
> +
> +  if (! grub_linuxefi_secure_validate (kernel, filelen))
> +    {
> +      grub_error (GRUB_ERR_INVALID_COMMAND, N_("%s has invalid
> signature"), argv[0]);
> +      grub_free (kernel);
> +      goto fail;
> +    }
> +
> +  grub_file_seek (file, 0);
> +
> +  grub_free(kernel);
> +
> +  params = grub_efi_allocate_pages_max (0x3fffffff,
> BYTES_TO_PAGES(16384));
> +
> +  if (! params)
> +    {
> +      grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate kernel
> parameters");
> +      goto fail;
> +    }
> +
> +  memset (params, 0, 16384);
> +
> +  if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
> +    {
> +      if (!grub_errno)
> +       grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
> +                   argv[0]);
> +      goto fail;
> +    }
> +
> +  if (lh.boot_flag != grub_cpu_to_le16 (0xaa55))
> +    {
> +      grub_error (GRUB_ERR_BAD_OS, N_("invalid magic number"));
> +      goto fail;
> +    }
> +
> +  if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
> +    {
> +      grub_error (GRUB_ERR_BAD_OS, N_("too many setup sectors"));
> +      goto fail;
> +    }
> +
> +  if (lh.version < grub_cpu_to_le16 (0x020b))
> +    {
> +      grub_error (GRUB_ERR_BAD_OS, N_("kernel too old"));
> +      goto fail;
> +    }
> +
> +  if (!lh.handover_offset)
> +    {
> +      grub_error (GRUB_ERR_BAD_OS, N_("kernel doesn't support EFI
> handover"));
> +      goto fail;
> +    }
> +
> +  linux_cmdline = grub_efi_allocate_pages_max(0x3fffffff,
> +                                        BYTES_TO_PAGES(lh.cmdline_size +
> 1));
> +
> +  if (!linux_cmdline)
> +    {
> +      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate cmdline"));
> +      goto fail;
> +    }
> +
> +  grub_memcpy (linux_cmdline, LINUX_IMAGE, sizeof (LINUX_IMAGE));
> +  grub_create_loader_cmdline (argc, argv,
> +                              linux_cmdline + sizeof (LINUX_IMAGE) - 1,
> +                             lh.cmdline_size - (sizeof (LINUX_IMAGE) -
> 1));
> +
> +  lh.cmd_line_ptr = (grub_uint32_t)(grub_uint64_t)linux_cmdline;
> +
> +  handover_offset = lh.handover_offset;
> +
> +  start = (lh.setup_sects + 1) * 512;
> +  len = grub_file_size(file) - start;
> +
> +  kernel_mem = grub_efi_allocate_pages(lh.pref_address,
> +                                      BYTES_TO_PAGES(lh.init_size));
> +
> +  if (!kernel_mem)
> +    kernel_mem = grub_efi_allocate_pages_max(0x3fffffff,
> +                                            BYTES_TO_PAGES(lh.init_size));
> +
> +  if (!kernel_mem)
> +    {
> +      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate kernel"));
> +      goto fail;
> +    }
> +
> +  if (grub_file_seek (file, start) == (grub_off_t) -1)
> +    {
> +      grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
> +                 argv[0]);
> +      goto fail;
> +    }
> +
> +  if (grub_file_read (file, kernel_mem, len) != len && !grub_errno)
> +    {
> +      grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
> +                 argv[0]);
> +    }
> +
> +  if (grub_errno == GRUB_ERR_NONE)
> +    {
> +      grub_loader_set (grub_linuxefi_boot, grub_linuxefi_unload, 0);
> +      loaded = 1;
> +      lh.code32_start = (grub_uint32_t)(grub_uint64_t) kernel_mem;
> +    }
> +
> +  memcpy(params, &lh, 2 * 512);
> +
> +  params->type_of_loader = 0x21;
> +
> + fail:
> +
> +  if (file)
> +    grub_file_close (file);
> +
> +  if (grub_errno != GRUB_ERR_NONE)
> +    {
> +      grub_dl_unref (my_mod);
> +      loaded = 0;
> +    }
> +
> +  if (linux_cmdline && !loaded)
> +    grub_efi_free_pages((grub_efi_physical_address_t)linux_cmdline,
> BYTES_TO_PAGES(lh.cmdline_size + 1));
> +
> +  if (kernel_mem && !loaded)
> +    grub_efi_free_pages((grub_efi_physical_address_t)kernel_mem,
> BYTES_TO_PAGES(kernel_size));
> +
> +  if (params && !loaded)
> +    grub_efi_free_pages((grub_efi_physical_address_t)params,
> BYTES_TO_PAGES(16384));
> +
> +--
> 1.8.3.1
>
>
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org <javascript:;>
> https://lists.gnu.org/mailman/listinfo/grub-devel
>

[-- Attachment #2: Type: text/html, Size: 16926 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add linuxefi module
  2014-01-21  1:46 ` SevenBits
@ 2014-01-21 13:43   ` Colin Watson
  2014-01-21 13:53     ` Vladimir 'φ-coder/phcoder' Serbinenko
  2014-01-21 15:24     ` SevenBits
  0 siblings, 2 replies; 11+ messages in thread
From: Colin Watson @ 2014-01-21 13:43 UTC (permalink / raw)
  To: grub-devel

On Mon, Jan 20, 2014 at 08:46:48PM -0500, SevenBits wrote:
> On Monday, January 20, 2014, Lubomir Rintel <lkundrak@v3.sk> wrote:
> > From: Matthew Garrett <matthew.garrett@nebula.com>
> >
> > This adds linuxefi module that provides a way to load Linux kernel
> > and RAM disk image via EFI services with linuxefi and initrdefi
> > commands, analogous to linux and initrd commands.
> 
> Why? What's wrong with the standard linux and initrd commands? They work
> just fine under UEFI.

The background to this is that if conditions permit it's helpful to hand
over to the kernel without calling ExitBootServices first, because it
allows the kernel to do more of its own quirks handling.  If shim is
present then it's used for signature verification first, since UEFI
Secure Boot forbids executing unsigned code before ExitBootServices;
although this patch is configured such that if shim is missing then no
signature check is performed (which is probably reasonable for
upstreaming).

We're carrying this patch in Debian/Ubuntu too, although I had to
disable it on i386_efi - I think it failed tests there.  It's a while
since I checked, and that patch might be obsolete now.  I also have an
additional fairly trivial patch to add more debugging printfs to
linuxefi, which I could apply if this is accepted.

I would be inclined to say that linuxefi should be essentially an
internal implementation detail, and that linux should forward to
linuxefi if appropriate.  I was never a particular fan of Matthew's
approach of adding an entirely new set of commands for it, and we don't
expose those in the configuration we generate in the Debian/Ubuntu
packaging.

-- 
Colin Watson                                       [cjwatson@ubuntu.com]


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add linuxefi module
  2014-01-21 13:43   ` Colin Watson
@ 2014-01-21 13:53     ` Vladimir 'φ-coder/phcoder' Serbinenko
  2014-01-21 15:24     ` SevenBits
  1 sibling, 0 replies; 11+ messages in thread
From: Vladimir 'φ-coder/phcoder' Serbinenko @ 2014-01-21 13:53 UTC (permalink / raw)
  To: The development of GNU GRUB

[-- Attachment #1: Type: text/plain, Size: 622 bytes --]

On 21.01.2014 14:43, Colin Watson wrote:
> I would be inclined to say that linuxefi should be essentially an
> internal implementation detail, and that linux should forward to
> linuxefi if appropriate. 
Current 32-bit protocol is very useful on platforms like coreboot.
Removing its use in GRUB combined with Linux devs like hpa who are
hostile to coreboot (and blocked introduction of 32-bit interface for
years until bowing to EFI), I fear that 32-bit entry point might become
unusable very quickly.
Speaking of quirk handling, when I asked about mjg details he was very
vague and details are important.



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 274 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add linuxefi module
  2014-01-21 13:43   ` Colin Watson
  2014-01-21 13:53     ` Vladimir 'φ-coder/phcoder' Serbinenko
@ 2014-01-21 15:24     ` SevenBits
  1 sibling, 0 replies; 11+ messages in thread
From: SevenBits @ 2014-01-21 15:24 UTC (permalink / raw)
  To: The development of GNU GRUB

[-- Attachment #1: Type: text/plain, Size: 2598 bytes --]

On Jan 21, 2014, at 8:43 AM, Colin Watson <cjwatson@ubuntu.com> wrote:

> On Mon, Jan 20, 2014 at 08:46:48PM -0500, SevenBits wrote:
>> On Monday, January 20, 2014, Lubomir Rintel <lkundrak@v3.sk> wrote:
>>> From: Matthew Garrett <matthew.garrett@nebula.com>
>>> 
>>> This adds linuxefi module that provides a way to load Linux kernel
>>> and RAM disk image via EFI services with linuxefi and initrdefi
>>> commands, analogous to linux and initrd commands.
>> 
>> Why? What's wrong with the standard linux and initrd commands? They work
>> just fine under UEFI.
> 
> The background to this is that if conditions permit it's helpful to hand
> over to the kernel without calling ExitBootServices first, because it
> allows the kernel to do more of its own quirks handling.  If shim is
> present then it's used for signature verification first, since UEFI
> Secure Boot forbids executing unsigned code before ExitBootServices;
> although this patch is configured such that if shim is missing then no
> signature check is performed (which is probably reasonable for
> upstreaming).
> 
> We're carrying this patch in Debian/Ubuntu too, although I had to
> disable it on i386_efi - I think it failed tests there.  It's a while
> since I checked, and that patch might be obsolete now.  I also have an
> additional fairly trivial patch to add more debugging printfs to
> linuxefi, which I could apply if this is accepted.
> 
> I would be inclined to say that linuxefi should be essentially an
> internal implementation detail, and that linux should forward to
> linuxefi if appropriate.  I was never a particular fan of Matthew's
> approach of adding an entirely new set of commands for it, and we don't
> expose those in the configuration we generate in the Debian/Ubuntu
> packaging.

I would leave it separate, but I guess it doesn’t really matter - let the project managers decide, after all - but I know that I prefer the ability to set my preferences one way or the other on issues like this rather than have the programmers decide for me. If GRUB users, be it end users or Linux distribution vendors, want to boot Linux in the way you describe, then let them with your patch, but I see no reason to force them to by making making it an “implementation detail” when the traditional approach works just fine on UEFI.

> 
> -- 
> Colin Watson                                       [cjwatson@ubuntu.com]
> 
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel


[-- Attachment #2: Message signed with OpenPGP using GPGMail --]
[-- Type: application/pgp-signature, Size: 535 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add linuxefi module
  2014-01-20 23:28 [PATCH] Add linuxefi module Lubomir Rintel
  2014-01-21  1:46 ` SevenBits
@ 2014-01-21 16:24 ` Andrey Borzenkov
  2014-01-21 16:29   ` Vladimir 'φ-coder/phcoder' Serbinenko
  2014-01-21 16:30   ` Matthew Garrett
  1 sibling, 2 replies; 11+ messages in thread
From: Andrey Borzenkov @ 2014-01-21 16:24 UTC (permalink / raw)
  To: The development of GNU GRUB; +Cc: lkundrak, Matthew Garrett

В Tue, 21 Jan 2014 00:28:08 +0100
Lubomir Rintel <lkundrak@v3.sk> пишет:

>  
>  module = {
> +  name = linuxefi;
> +  efi = loader/i386/efi/linux.c;
> +  efi = lib/cmdline.c;
> +  enable = i386_efi;
> +  enable = x86_64_efi;
> +};
> +

Is it relevant for arm64-efi? 

> +static grub_err_t
> +grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
> +		int argc, char *argv[])
> +{
> +  grub_file_t file = 0;
> +  struct linux_kernel_header lh;
> +  grub_ssize_t len, start, filelen;
> +  void *kernel;
> +
> +  grub_dl_ref (my_mod);
> +
> +  if (argc == 0)
> +    {
> +      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
> +      goto fail;
> +    }
> +
> +  file = grub_file_open (argv[0]);
> +  if (! file)
> +    goto fail;
> +
> +  filelen = grub_file_size (file);
> +
> +  kernel = grub_malloc(filelen);
> +
> +  if (!kernel)
> +    {
> +      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
> +      goto fail;
> +    }
> +
> +  if (grub_file_read (file, kernel, filelen) != filelen)
> +    {
> +      grub_error (GRUB_ERR_FILE_READ_ERROR, N_("Can't read kernel %s"), argv[0]);
> +      goto fail;
> +    }
> +
> +  if (! grub_linuxefi_secure_validate (kernel, filelen))
> +    {
> +      grub_error (GRUB_ERR_INVALID_COMMAND, N_("%s has invalid signature"), argv[0]);
> +      grub_free (kernel);
> +      goto fail;
> +    }
> +
> +  grub_file_seek (file, 0);
> +
> +  grub_free(kernel);
> +

This leaves possibility to modify file after it was verified. It
should continue to use in-memory content. 


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add linuxefi module
  2014-01-21 16:24 ` Andrey Borzenkov
@ 2014-01-21 16:29   ` Vladimir 'φ-coder/phcoder' Serbinenko
  2014-01-21 23:29     ` Colin Watson
  2014-01-21 16:30   ` Matthew Garrett
  1 sibling, 1 reply; 11+ messages in thread
From: Vladimir 'φ-coder/phcoder' Serbinenko @ 2014-01-21 16:29 UTC (permalink / raw)
  To: The development of GNU GRUB

[-- Attachment #1: Type: text/plain, Size: 2129 bytes --]

On 21.01.2014 17:24, Andrey Borzenkov wrote:
> В Tue, 21 Jan 2014 00:28:08 +0100
> Lubomir Rintel <lkundrak@v3.sk> пишет:
> 
>>  
>>  module = {
>> +  name = linuxefi;
>> +  efi = loader/i386/efi/linux.c;
>> +  efi = lib/cmdline.c;
>> +  enable = i386_efi;
>> +  enable = x86_64_efi;
>> +};
>> +
> 
> Is it relevant for arm64-efi? 
> 
>> +static grub_err_t
>> +grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
>> +		int argc, char *argv[])
>> +{
>> +  grub_file_t file = 0;
>> +  struct linux_kernel_header lh;
>> +  grub_ssize_t len, start, filelen;
>> +  void *kernel;
>> +
>> +  grub_dl_ref (my_mod);
>> +
>> +  if (argc == 0)
>> +    {
>> +      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
>> +      goto fail;
>> +    }
>> +
>> +  file = grub_file_open (argv[0]);
>> +  if (! file)
>> +    goto fail;
>> +
>> +  filelen = grub_file_size (file);
>> +
>> +  kernel = grub_malloc(filelen);
>> +
>> +  if (!kernel)
>> +    {
>> +      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
>> +      goto fail;
>> +    }
>> +
>> +  if (grub_file_read (file, kernel, filelen) != filelen)
>> +    {
>> +      grub_error (GRUB_ERR_FILE_READ_ERROR, N_("Can't read kernel %s"), argv[0]);
>> +      goto fail;
>> +    }
>> +
>> +  if (! grub_linuxefi_secure_validate (kernel, filelen))
>> +    {
>> +      grub_error (GRUB_ERR_INVALID_COMMAND, N_("%s has invalid signature"), argv[0]);
>> +      grub_free (kernel);
>> +      goto fail;
>> +    }
>> +
>> +  grub_file_seek (file, 0);
>> +
>> +  grub_free(kernel);
>> +
> 
> This leaves possibility to modify file after it was verified. It
> should continue to use in-memory content. 
> 
This part is from RH "Secureboot" patch. Few things are right about that
patch. Whatever signature verifications would need to be integrated with
signatures framework (I have some scratch in phcoder/file_types)
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
> 



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 274 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add linuxefi module
  2014-01-21 16:24 ` Andrey Borzenkov
  2014-01-21 16:29   ` Vladimir 'φ-coder/phcoder' Serbinenko
@ 2014-01-21 16:30   ` Matthew Garrett
  2014-01-21 19:20     ` Leif Lindholm
  1 sibling, 1 reply; 11+ messages in thread
From: Matthew Garrett @ 2014-01-21 16:30 UTC (permalink / raw)
  To: arvidjaar; +Cc: grub-devel, lkundrak

On Tue, 2014-01-21 at 20:24 +0400, Andrey Borzenkov wrote:
> В Tue, 21 Jan 2014 00:28:08 +0100
> Lubomir Rintel <lkundrak@v3.sk> пишет:
> 
> >  
> >  module = {
> > +  name = linuxefi;
> > +  efi = loader/i386/efi/linux.c;
> > +  efi = lib/cmdline.c;
> > +  enable = i386_efi;
> > +  enable = x86_64_efi;
> > +};
> > +
> 
> Is it relevant for arm64-efi? 

Not at the moment - it still requires architecture-specific knowledge of
the boot protocol, and I don't think that's well-defined for arm64-efi
yet.

-- 
Matthew Garrett <matthew.garrett@nebula.com>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add linuxefi module
  2014-01-21 16:30   ` Matthew Garrett
@ 2014-01-21 19:20     ` Leif Lindholm
  0 siblings, 0 replies; 11+ messages in thread
From: Leif Lindholm @ 2014-01-21 19:20 UTC (permalink / raw)
  To: The development of GNU GRUB; +Cc: arvidjaar, lkundrak

On Tue, Jan 21, 2014 at 04:30:13PM +0000, Matthew Garrett wrote:
> > Is it relevant for arm64-efi? 
> 
> Not at the moment - it still requires architecture-specific knowledge of
> the boot protocol, and I don't think that's well-defined for arm64-efi
> yet.

Depends which bit of the boot protocol you mean.
The kernel interface is pretty well set.
Where we go with regards to UEFI Secure Boot is an open question.

I did have a look at turning the linuxefi patches into a generic
variant with architecture-specific hooks where required, but:
1) I still have hope of not needing the shim solution in the aarch64
   ecosystem.
2) I didn't have a gnu-efi port to build the shim with.

/
    Leif


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add linuxefi module
  2014-01-21 16:29   ` Vladimir 'φ-coder/phcoder' Serbinenko
@ 2014-01-21 23:29     ` Colin Watson
  2014-01-22 15:00       ` Vladimir 'φ-coder/phcoder' Serbinenko
  0 siblings, 1 reply; 11+ messages in thread
From: Colin Watson @ 2014-01-21 23:29 UTC (permalink / raw)
  To: grub-devel

On Tue, Jan 21, 2014 at 05:29:03PM +0100, Vladimir 'φ-coder/phcoder' Serbinenko wrote:
> This part is from RH "Secureboot" patch. Few things are right about that
> patch. Whatever signature verifications would need to be integrated with
> signatures framework (I have some scratch in phcoder/file_types)

The RH SB patch is not ideal from a pure GRUB point of view.  But
realistically, in order to actually be useful in the (unfortunate) SB
ecosystem that exists today where Microsoft is the effective root of
trust on most mass-market hardware, we need to have a non-GPLv3
component that is what the firmware actually loads directly, it needs to
be able to do signature checking in order to chain to GRUB, and it's
unlikely to be helpful for the signature checking to be implemented in
two places - so the scheme where GRUB calls out to shim seems to be an
uncomfortable necessity there.

I have no objection to there being some more native mechanism in GRUB
that works when users take control of their own trust chain; that seems
entirely consistent with the FSF's goals regarding UEFI.  But I'm having
trouble seeing how we could make use of it effectively in order to
bootstrap free operating systems on firmware that only has the Microsoft
keys in place, which I think is just as important now as the ability to
run GNU software on proprietary Unixes was back in the 1980s.

(Unless, of course, you mean that there ought to be something integrated
into GRUB's signatures framework that would let it optionally call out
to shim; that would be an interesting possibility.)

-- 
Colin Watson                                       [cjwatson@ubuntu.com]


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add linuxefi module
  2014-01-21 23:29     ` Colin Watson
@ 2014-01-22 15:00       ` Vladimir 'φ-coder/phcoder' Serbinenko
  0 siblings, 0 replies; 11+ messages in thread
From: Vladimir 'φ-coder/phcoder' Serbinenko @ 2014-01-22 15:00 UTC (permalink / raw)
  To: The development of GNU GRUB

[-- Attachment #1: Type: text/plain, Size: 2039 bytes --]

On 22.01.2014 00:29, Colin Watson wrote:
> On Tue, Jan 21, 2014 at 05:29:03PM +0100, Vladimir 'φ-coder/phcoder' Serbinenko wrote:
>> This part is from RH "Secureboot" patch. Few things are right about that
>> patch. Whatever signature verifications would need to be integrated with
>> signatures framework (I have some scratch in phcoder/file_types)
> 
> The RH SB patch is not ideal from a pure GRUB point of view.  But
> realistically, in order to actually be useful in the (unfortunate) SB
> ecosystem that exists today where Microsoft is the effective root of
> trust on most mass-market hardware, we need to have a non-GPLv3
> component that is what the firmware actually loads directly, it needs to
> be able to do signature checking in order to chain to GRUB, and it's
> unlikely to be helpful for the signature checking to be implemented in
> two places - so the scheme where GRUB calls out to shim seems to be an
> uncomfortable necessity there.
> 
Distros start shipping signed kernels with signing in EFI way, including
Ubuntu. Similar proposal to add GnuPG signatures was met with scepticism
(if I remember correctly, including from you). On coreboot systems it
can be interesting to verify that kernel came from Ubuntu and the only
current way to do so is EFI-style signature.
> I have no objection to there being some more native mechanism in GRUB
> that works when users take control of their own trust chain; that seems
> entirely consistent with the FSF's goals regarding UEFI.  But I'm having
> trouble seeing how we could make use of it effectively in order to
> bootstrap free operating systems on firmware that only has the Microsoft
> keys in place, which I think is just as important now as the ability to
> run GNU software on proprietary Unixes was back in the 1980s.
> 
> (Unless, of course, you mean that there ought to be something integrated
> into GRUB's signatures framework that would let it optionally call out
> to shim; that would be an interesting possibility.)
> 



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 274 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2014-01-22 15:00 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-20 23:28 [PATCH] Add linuxefi module Lubomir Rintel
2014-01-21  1:46 ` SevenBits
2014-01-21 13:43   ` Colin Watson
2014-01-21 13:53     ` Vladimir 'φ-coder/phcoder' Serbinenko
2014-01-21 15:24     ` SevenBits
2014-01-21 16:24 ` Andrey Borzenkov
2014-01-21 16:29   ` Vladimir 'φ-coder/phcoder' Serbinenko
2014-01-21 23:29     ` Colin Watson
2014-01-22 15:00       ` Vladimir 'φ-coder/phcoder' Serbinenko
2014-01-21 16:30   ` Matthew Garrett
2014-01-21 19:20     ` Leif Lindholm

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.