efifwsetup.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* fwsetup.c - Reboot into firmware setup menu. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2012 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/types.h>
  20. #include <grub/mm.h>
  21. #include <grub/misc.h>
  22. #include <grub/efi/api.h>
  23. #include <grub/efi/efi.h>
  24. #include <grub/command.h>
  25. #include <grub/i18n.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. static grub_err_t
  28. grub_cmd_fwsetup (grub_command_t cmd __attribute__ ((unused)),
  29. int argc __attribute__ ((unused)),
  30. char **args __attribute__ ((unused)))
  31. {
  32. grub_efi_uint64_t *old_os_indications;
  33. grub_efi_uint64_t os_indications = GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
  34. grub_err_t status;
  35. grub_size_t oi_size;
  36. grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
  37. old_os_indications = grub_efi_get_variable ("OsIndications", &global,
  38. &oi_size);
  39. if (old_os_indications != NULL && oi_size == sizeof (os_indications))
  40. os_indications |= *old_os_indications;
  41. status = grub_efi_set_variable ("OsIndications", &global, &os_indications,
  42. sizeof (os_indications));
  43. if (status != GRUB_ERR_NONE)
  44. return status;
  45. grub_reboot ();
  46. return GRUB_ERR_BUG;
  47. }
  48. static grub_command_t cmd = NULL;
  49. static grub_efi_boolean_t
  50. efifwsetup_is_supported (void)
  51. {
  52. grub_efi_uint64_t *os_indications_supported = NULL;
  53. grub_size_t oi_size = 0;
  54. grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
  55. os_indications_supported = grub_efi_get_variable ("OsIndicationsSupported",
  56. &global, &oi_size);
  57. if (!os_indications_supported)
  58. return 0;
  59. if (*os_indications_supported & GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI)
  60. return 1;
  61. return 0;
  62. }
  63. GRUB_MOD_INIT (efifwsetup)
  64. {
  65. if (efifwsetup_is_supported ())
  66. cmd = grub_register_command ("fwsetup", grub_cmd_fwsetup, NULL,
  67. N_("Reboot into firmware setup menu."));
  68. }
  69. GRUB_MOD_FINI (efifwsetup)
  70. {
  71. if (cmd)
  72. grub_unregister_command (cmd);
  73. }