a5bd9f6
From 9e9382ece3511ff530cfb3e1c1e7a7dbaa3416b6 Mon Sep 17 00:00:00 2001
aa709fb
From: Peter Jones <pjones@redhat.com>
a5bd9f6
Date: Sat, 8 Sep 2012 09:40:24 +0200
a5bd9f6
Subject: [PATCH 033/364] 	* grub-core/Makefile.core.def (efifwsetup):
a5bd9f6
 New module. 	* grub-core/commands/efi/efifwsetup.c: New file. 	*
a5bd9f6
 grub-core/kern/efi/efi.c (grub_efi_set_variable): New function 	*
a5bd9f6
 include/grub/efi/api.h (GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI): 	New
a5bd9f6
 define. 	* include/grub/efi/efi.h (grub_efi_set_variable): New proto.
aa709fb
aa709fb
---
a5bd9f6
 ChangeLog                           |  9 ++++
a5bd9f6
 grub-core/Makefile.core.def         |  6 +++
a5bd9f6
 grub-core/commands/efi/efifwsetup.c | 90 +++++++++++++++++++++++++++++++++++++
a5bd9f6
 grub-core/kern/efi/efi.c            | 30 +++++++++++++
a5bd9f6
 include/grub/efi/api.h              |  2 +
a5bd9f6
 include/grub/efi/efi.h              |  5 +++
a5bd9f6
 6 files changed, 142 insertions(+)
aa709fb
 create mode 100644 grub-core/commands/efi/efifwsetup.c
aa709fb
a5bd9f6
diff --git a/ChangeLog b/ChangeLog
a5bd9f6
index 10b1ab3..e8f0577 100644
a5bd9f6
--- a/ChangeLog
a5bd9f6
+++ b/ChangeLog
a5bd9f6
@@ -1,3 +1,12 @@
a5bd9f6
+2012-09-08  Peter Jones <pjones@redhat.com>
a5bd9f6
+
a5bd9f6
+	* grub-core/Makefile.core.def (efifwsetup): New module.
a5bd9f6
+	* grub-core/commands/efi/efifwsetup.c: New file.
a5bd9f6
+	* grub-core/kern/efi/efi.c (grub_efi_set_variable): New function
a5bd9f6
+	* include/grub/efi/api.h (GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI):
a5bd9f6
+	New define.
a5bd9f6
+	* include/grub/efi/efi.h (grub_efi_set_variable): New proto.
a5bd9f6
+
a5bd9f6
 2012-09-05  Jiri Slaby <jslaby@suse.cz>
a5bd9f6
 
a5bd9f6
 	* configure.ac: Add SuSe path.
aa709fb
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
a5bd9f6
index de702d6..7a7b97a 100644
aa709fb
--- a/grub-core/Makefile.core.def
aa709fb
+++ b/grub-core/Makefile.core.def
a5bd9f6
@@ -575,6 +575,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
a5bd9f6
index 0000000..7a137a7
aa709fb
--- /dev/null
aa709fb
+++ b/grub-core/commands/efi/efifwsetup.c
a5bd9f6
@@ -0,0 +1,90 @@
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
+ */
a5bd9f6
+
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>
a5bd9f6
+#include <grub/i18n.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
+
a5bd9f6
+  old_os_indications = grub_efi_get_variable ("OsIndications", &global,
a5bd9f6
+					      &oi_size);
aa709fb
+
a5bd9f6
+  if (old_os_indications != NULL && oi_size == sizeof (os_indications))
aa709fb
+    os_indications |= *old_os_indications;
aa709fb
+
a5bd9f6
+  status = grub_efi_set_variable ("OsIndications", &global, &os_indications,
a5bd9f6
+				  sizeof (os_indications));
aa709fb
+  if (status != GRUB_ERR_NONE)
aa709fb
+    return status;
aa709fb
+
a5bd9f6
+  grub_reboot ();
aa709fb
+
aa709fb
+  return GRUB_ERR_BUG;
aa709fb
+}
aa709fb
+
aa709fb
+static grub_command_t cmd = NULL;
aa709fb
+
aa709fb
+static grub_efi_boolean_t
a5bd9f6
+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
+
a5bd9f6
+  os_indications_supported = grub_efi_get_variable ("OsIndicationsSupported",
a5bd9f6
+						    &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
+
a5bd9f6
+GRUB_MOD_INIT (efifwsetup)
aa709fb
+{
a5bd9f6
+  if (efifwsetup_is_supported ())
a5bd9f6
+    cmd = grub_register_command ("fwsetup", grub_cmd_fwsetup, NULL,
a5bd9f6
+				 N_("Reboot into firmware setup menu."));
aa709fb
+
aa709fb
+}
aa709fb
+
a5bd9f6
+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
a5bd9f6
index 02d2f9a..e8a62ec 100644
aa709fb
--- a/grub-core/kern/efi/efi.c
aa709fb
+++ b/grub-core/kern/efi/efi.c
a5bd9f6
@@ -181,6 +181,36 @@ grub_efi_set_virtual_address_map (grub_efi_uintn_t memory_map_size,
a5bd9f6
   return grub_error (GRUB_ERR_IO, "set_virtual_address_map failed");
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
+
a5bd9f6
+  status = efi_call_5 (r->set_variable, var16, guid, 
a5bd9f6
+		       (GRUB_EFI_VARIABLE_NON_VOLATILE
a5bd9f6
+			| GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS
a5bd9f6
+			| GRUB_EFI_VARIABLE_RUNTIME_ACCESS),
a5bd9f6
+		       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
+
a5bd9f6
 void *
a5bd9f6
 grub_efi_get_variable (const char *var, const grub_efi_guid_t *guid,
a5bd9f6
 		       grub_size_t *datasize_out)
aa709fb
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
a5bd9f6
index 9e7a8d8..ae61730 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
-- 
a5bd9f6
1.8.1.4
aa709fb