aa709fb
From 2c7cdc59a8d6cb7800c73b90aa75cc8b21807af6 Mon Sep 17 00:00:00 2001
aa709fb
From: Peter Jones <pjones@redhat.com>
aa709fb
Date: Thu, 24 May 2012 08:37:21 -0400
aa709fb
Subject: [PATCH] Add support for entering the firmware setup screen.
aa709fb
aa709fb
This adds a command "fwsetup", with which you can enter your firmware
aa709fb
setup screen.  The mechanism is to set a global UEFI variable with a
aa709fb
specific value and reboot.
aa709fb
---
aa709fb
 ChangeLog                           |    8 ++++
aa709fb
 grub-core/Makefile.core.def         |    6 +++
aa709fb
 grub-core/commands/efi/efifwsetup.c |   88 +++++++++++++++++++++++++++++++++++
aa709fb
 grub-core/kern/efi/efi.c            |   30 ++++++++++++
aa709fb
 include/grub/efi/api.h              |    2 +
aa709fb
 include/grub/efi/efi.h              |    5 ++
aa709fb
 6 files changed, 139 insertions(+)
aa709fb
 create mode 100644 grub-core/commands/efi/efifwsetup.c
aa709fb
3734d8a
#diff --git a/ChangeLog b/ChangeLog
3734d8a
#index ce52576..29ebcbd 100644
3734d8a
#--- a/ChangeLog
3734d8a
#+++ b/ChangeLog
3734d8a
#@@ -1,3 +1,11 @@
3734d8a
#+2012-05-24  Peter Jones <pjones@redhat.com>
3734d8a
#+
3734d8a
#+	* grub-core/Makefile.core.def: add efifwsetup module
3734d8a
#+	* grub-core/commands/efi/efifwsetup.c: add code for fwsetup command
3734d8a
#+	* grub-core/kern/efi/efi.c (grub_efi_set_variable): New function
3734d8a
#+	* include/grub/efi/api.h: add define for OsIndications variable
3734d8a
#+	* include/grub/efi/efi.h: export grub_efi_set_variable
3734d8a
#+
3734d8a
# 2012-05-31  Vladimir Serbinenko  <phcoder@gmail.com>
3734d8a
# 
3734d8a
# 	* configure.ac: Bump to beta6.
aa709fb
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
aa709fb
index 000cf0d..d0c06d5 100644
aa709fb
--- a/grub-core/Makefile.core.def
aa709fb
+++ b/grub-core/Makefile.core.def
aa709fb
@@ -576,6 +576,12 @@ module = {
aa709fb
 };
aa709fb
 
aa709fb
 module = {
aa709fb
+  name = efifwsetup;
aa709fb
+  efi = commands/efi/efifwsetup.c;
aa709fb
+  enable = efi;
aa709fb
+};
aa709fb
+
aa709fb
+module = {
aa709fb
   name = blocklist;
aa709fb
   common = commands/blocklist.c;
aa709fb
 };
aa709fb
diff --git a/grub-core/commands/efi/efifwsetup.c b/grub-core/commands/efi/efifwsetup.c
aa709fb
new file mode 100644
aa709fb
index 0000000..756a14c
aa709fb
--- /dev/null
aa709fb
+++ b/grub-core/commands/efi/efifwsetup.c
aa709fb
@@ -0,0 +1,88 @@
aa709fb
+/* fwsetup.c - Reboot into firmware setup menu. */
aa709fb
+/*
aa709fb
+ *  GRUB  --  GRand Unified Bootloader
aa709fb
+ *  Copyright (C) 2012  Free Software Foundation, Inc.
aa709fb
+ *
aa709fb
+ *  GRUB is free software: you can redistribute it and/or modify
aa709fb
+ *  it under the terms of the GNU General Public License as published by
aa709fb
+ *  the Free Software Foundation, either version 3 of the License, or
aa709fb
+ *  (at your option) any later version.
aa709fb
+ *
aa709fb
+ *  GRUB is distributed in the hope that it will be useful,
aa709fb
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
aa709fb
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
aa709fb
+ *  GNU General Public License for more details.
aa709fb
+ *
aa709fb
+ *  You should have received a copy of the GNU General Public License
aa709fb
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
aa709fb
+ */
aa709fb
+#include <grub/types.h>
aa709fb
+#include <grub/mm.h>
aa709fb
+#include <grub/misc.h>
aa709fb
+#include <grub/efi/api.h>
aa709fb
+#include <grub/efi/efi.h>
aa709fb
+#include <grub/command.h>
aa709fb
+
aa709fb
+GRUB_MOD_LICENSE ("GPLv3+");
aa709fb
+
aa709fb
+static grub_err_t
aa709fb
+grub_cmd_fwsetup (grub_command_t cmd __attribute__ ((unused)),
aa709fb
+		  int argc __attribute__ ((unused)),
aa709fb
+		  char **args __attribute__ ((unused)))
aa709fb
+{
aa709fb
+  grub_efi_uint64_t *old_os_indications;
aa709fb
+  grub_efi_uint64_t os_indications = GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
aa709fb
+  grub_err_t status;
aa709fb
+  grub_size_t oi_size;
aa709fb
+  grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
aa709fb
+
aa709fb
+  old_os_indications = grub_efi_get_variable("OsIndications", &global,
aa709fb
+					     &oi_size);
aa709fb
+
aa709fb
+  if (old_os_indications != NULL && oi_size == sizeof(*old_os_indications))
aa709fb
+    os_indications |= *old_os_indications;
aa709fb
+
aa709fb
+  status = grub_efi_set_variable("OsIndications", &global, &os_indications,
aa709fb
+				 sizeof (os_indications));
aa709fb
+  if (status != GRUB_ERR_NONE)
aa709fb
+    return status;
aa709fb
+
aa709fb
+  grub_reboot();
aa709fb
+
aa709fb
+  return GRUB_ERR_BUG;
aa709fb
+}
aa709fb
+
aa709fb
+static grub_command_t cmd = NULL;
aa709fb
+
aa709fb
+static grub_efi_boolean_t
aa709fb
+efifwsetup_is_supported(void)
aa709fb
+{
aa709fb
+  grub_efi_uint64_t *os_indications_supported = NULL;
aa709fb
+  grub_size_t oi_size = 0;
aa709fb
+  grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
aa709fb
+
aa709fb
+  os_indications_supported = grub_efi_get_variable("OsIndicationsSupported",
aa709fb
+						  &global, &oi_size);
aa709fb
+
aa709fb
+  if (!os_indications_supported)
aa709fb
+    return 0;
aa709fb
+
aa709fb
+  if (*os_indications_supported & GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI)
aa709fb
+    return 1;
aa709fb
+
aa709fb
+  return 0;
aa709fb
+}
aa709fb
+
aa709fb
+GRUB_MOD_INIT(efifwsetup)
aa709fb
+{
aa709fb
+  if (efifwsetup_is_supported())
aa709fb
+    cmd = grub_register_command("fwsetup", grub_cmd_fwsetup, "",
aa709fb
+				"Reboot into firmware setup menu.");
aa709fb
+
aa709fb
+}
aa709fb
+
aa709fb
+GRUB_MOD_FINI(efifwsetup)
aa709fb
+{
aa709fb
+  if (cmd)
aa709fb
+    grub_unregister_command (cmd);
aa709fb
+}
aa709fb
diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
aa709fb
index 6f12c76..7a418a6 100644
aa709fb
--- a/grub-core/kern/efi/efi.c
aa709fb
+++ b/grub-core/kern/efi/efi.c
aa709fb
@@ -230,6 +230,36 @@ grub_efi_get_variable (const char *var, const grub_efi_guid_t *guid,
aa709fb
   return NULL;
aa709fb
 }
aa709fb
 
aa709fb
+grub_err_t
aa709fb
+grub_efi_set_variable(const char *var, const grub_efi_guid_t *guid,
aa709fb
+		      void *data, grub_size_t datasize)
aa709fb
+{
aa709fb
+  grub_efi_status_t status;
aa709fb
+  grub_efi_runtime_services_t *r;
aa709fb
+  grub_efi_char16_t *var16;
aa709fb
+  grub_size_t len, len16;
aa709fb
+
aa709fb
+  len = grub_strlen (var);
aa709fb
+  len16 = len * GRUB_MAX_UTF16_PER_UTF8;
aa709fb
+  var16 = grub_malloc ((len16 + 1) * sizeof (var16[0]));
aa709fb
+  if (!var16)
aa709fb
+    return grub_errno;
aa709fb
+  len16 = grub_utf8_to_utf16 (var16, len16, (grub_uint8_t *) var, len, NULL);
aa709fb
+  var16[len16] = 0;
aa709fb
+
aa709fb
+  r = grub_efi_system_table->runtime_services;
aa709fb
+
aa709fb
+  grub_efi_uint32_t attributes = GRUB_EFI_VARIABLE_NON_VOLATILE |
aa709fb
+				 GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS |
aa709fb
+				 GRUB_EFI_VARIABLE_RUNTIME_ACCESS;
aa709fb
+
aa709fb
+  status = efi_call_5 (r->set_variable, var16, guid, attributes, datasize,data);
aa709fb
+  if (status == GRUB_EFI_SUCCESS)
aa709fb
+    return GRUB_ERR_NONE;
aa709fb
+
aa709fb
+  return grub_error (GRUB_ERR_IO, "could not set EFI variable `%s'", var);
aa709fb
+}
aa709fb
+
3734d8a
 #pragma GCC diagnostic ignored "-Wcast-align"
3734d8a
 
3734d8a
 /* Search the mods section from the PE32/PE32+ image. This code uses
aa709fb
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
aa709fb
index 26127de..a47a4e3 100644
aa709fb
--- a/include/grub/efi/api.h
aa709fb
+++ b/include/grub/efi/api.h
aa709fb
@@ -58,6 +58,8 @@
aa709fb
 #define GRUB_EFI_OPEN_PROTOCOL_BY_DRIVER		0x00000010
aa709fb
 #define GRUB_EFI_OPEN_PROTOCOL_BY_EXCLUSIVE		0x00000020
aa709fb
 
aa709fb
+#define GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI	0x0000000000000001ULL
aa709fb
+
aa709fb
 #define GRUB_EFI_VARIABLE_NON_VOLATILE		0x0000000000000001
aa709fb
 #define GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS	0x0000000000000002
aa709fb
 #define GRUB_EFI_VARIABLE_RUNTIME_ACCESS	0x0000000000000004
aa709fb
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
aa709fb
index e67d92b..489cf9e 100644
aa709fb
--- a/include/grub/efi/efi.h
aa709fb
+++ b/include/grub/efi/efi.h
aa709fb
@@ -64,6 +64,11 @@ grub_err_t EXPORT_FUNC (grub_efi_set_virtual_address_map) (grub_efi_uintn_t memo
aa709fb
 void *EXPORT_FUNC (grub_efi_get_variable) (const char *variable,
aa709fb
 					   const grub_efi_guid_t *guid,
aa709fb
 					   grub_size_t *datasize_out);
aa709fb
+grub_err_t
aa709fb
+EXPORT_FUNC (grub_efi_set_variable) (const char *var,
aa709fb
+				     const grub_efi_guid_t *guid,
aa709fb
+				     void *data,
aa709fb
+				     grub_size_t datasize);
aa709fb
 int
aa709fb
 EXPORT_FUNC (grub_efi_compare_device_paths) (const grub_efi_device_path_t *dp1,
aa709fb
 					     const grub_efi_device_path_t *dp2);
aa709fb
-- 
aa709fb
1.7.10.1
aa709fb