efifwsetup.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_efi_boolean_t efifwsetup_is_supported (void);
  28. static grub_err_t
  29. grub_cmd_fwsetup (grub_command_t cmd __attribute__ ((unused)),
  30. int argc __attribute__ ((unused)),
  31. char **args __attribute__ ((unused)))
  32. {
  33. grub_efi_uint64_t *old_os_indications;
  34. grub_efi_uint64_t os_indications = GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
  35. grub_err_t status;
  36. grub_size_t oi_size;
  37. static grub_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
  38. if (argc >= 1 && grub_strcmp(args[0], "--is-supported") == 0)
  39. return !efifwsetup_is_supported ();
  40. if (!efifwsetup_is_supported ())
  41. return grub_error (GRUB_ERR_INVALID_COMMAND,
  42. N_("reboot to firmware setup is not supported by the current firmware"));
  43. grub_efi_get_variable ("OsIndications", &global, &oi_size,
  44. (void **) &old_os_indications);
  45. if (old_os_indications != NULL && oi_size == sizeof (os_indications))
  46. os_indications |= *old_os_indications;
  47. grub_free (old_os_indications);
  48. status = grub_efi_set_variable ("OsIndications", &global, &os_indications,
  49. sizeof (os_indications));
  50. if (status != GRUB_ERR_NONE)
  51. return status;
  52. grub_reboot ();
  53. return GRUB_ERR_BUG;
  54. }
  55. static grub_command_t cmd = NULL;
  56. static grub_efi_boolean_t
  57. efifwsetup_is_supported (void)
  58. {
  59. grub_efi_uint64_t *os_indications_supported = NULL;
  60. grub_size_t oi_size = 0;
  61. static grub_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
  62. grub_efi_boolean_t ret = 0;
  63. grub_efi_get_variable ("OsIndicationsSupported", &global, &oi_size,
  64. (void **) &os_indications_supported);
  65. if (!os_indications_supported)
  66. goto done;
  67. if (*os_indications_supported & GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI)
  68. ret = 1;
  69. done:
  70. grub_free (os_indications_supported);
  71. return ret;
  72. }
  73. GRUB_MOD_INIT (efifwsetup)
  74. {
  75. cmd = grub_register_command ("fwsetup", grub_cmd_fwsetup, NULL,
  76. N_("Reboot into firmware setup menu."));
  77. }
  78. GRUB_MOD_FINI (efifwsetup)
  79. {
  80. if (cmd)
  81. grub_unregister_command (cmd);
  82. }