summaryrefslogtreecommitdiffstats
path: root/grub-core/commands/efi/efifwsetup.c
blob: eaca0328388619278c2f1f4ee962fbd48037230f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* fwsetup.c - Reboot into firmware setup menu. */
/*
 *  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/types.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/efi/api.h>
#include <grub/efi/efi.h>
#include <grub/command.h>
#include <grub/i18n.h>

GRUB_MOD_LICENSE ("GPLv3+");

static grub_err_t
grub_cmd_fwsetup (grub_command_t cmd __attribute__ ((unused)),
		  int argc __attribute__ ((unused)),
		  char **args __attribute__ ((unused)))
{
  grub_efi_uint64_t *old_os_indications;
  grub_efi_uint64_t os_indications = GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
  grub_err_t status;
  grub_size_t oi_size;
  grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;

  grub_efi_get_variable ("OsIndications", &global, &oi_size,
			 (void **) &old_os_indications);

  if (old_os_indications != NULL && oi_size == sizeof (os_indications))
    os_indications |= *old_os_indications;

  status = grub_efi_set_variable ("OsIndications", &global, &os_indications,
				  sizeof (os_indications));
  if (status != GRUB_ERR_NONE)
    return status;

  grub_reboot ();

  return GRUB_ERR_BUG;
}

static grub_command_t cmd = NULL;

static grub_efi_boolean_t
efifwsetup_is_supported (void)
{
  grub_efi_uint64_t *os_indications_supported = NULL;
  grub_size_t oi_size = 0;
  grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;

  grub_efi_get_variable ("OsIndicationsSupported", &global, &oi_size,
			 (void **) &os_indications_supported);

  if (!os_indications_supported)
    return 0;

  if (*os_indications_supported & GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI)
    return 1;

  return 0;
}

GRUB_MOD_INIT (efifwsetup)
{
  if (efifwsetup_is_supported ())
    cmd = grub_register_command ("fwsetup", grub_cmd_fwsetup, NULL,
				 N_("Reboot into firmware setup menu."));

}

GRUB_MOD_FINI (efifwsetup)
{
  if (cmd)
    grub_unregister_command (cmd);
}