1*823f0493SLoGin #include <dragonstub/dragonstub.h>
2*823f0493SLoGin
3*823f0493SLoGin /* SHIM variables */
4*823f0493SLoGin static const efi_guid_t shim_guid = EFI_SHIM_LOCK_GUID;
5*823f0493SLoGin static const efi_char16_t shim_MokSBState_name[] = L"MokSBStateRT";
6*823f0493SLoGin
get_var(efi_char16_t * name,efi_guid_t * vendor,u32 * attr,unsigned long * data_size,void * data)7*823f0493SLoGin static efi_status_t get_var(efi_char16_t *name, efi_guid_t *vendor, u32 *attr,
8*823f0493SLoGin unsigned long *data_size, void *data)
9*823f0493SLoGin {
10*823f0493SLoGin return get_efi_var(name, vendor, attr, data_size, data);
11*823f0493SLoGin }
12*823f0493SLoGin
13*823f0493SLoGin /*
14*823f0493SLoGin * Determine whether we're in secure boot mode.
15*823f0493SLoGin */
efi_get_secureboot(void)16*823f0493SLoGin enum efi_secureboot_mode efi_get_secureboot(void)
17*823f0493SLoGin {
18*823f0493SLoGin u32 attr;
19*823f0493SLoGin unsigned long size;
20*823f0493SLoGin enum efi_secureboot_mode mode;
21*823f0493SLoGin efi_status_t status;
22*823f0493SLoGin u8 moksbstate;
23*823f0493SLoGin
24*823f0493SLoGin mode = efi_get_secureboot_mode(get_var);
25*823f0493SLoGin if (mode == efi_secureboot_mode_unknown) {
26*823f0493SLoGin efi_err("Could not determine UEFI Secure Boot status.\n");
27*823f0493SLoGin return efi_secureboot_mode_unknown;
28*823f0493SLoGin }
29*823f0493SLoGin if (mode != efi_secureboot_mode_enabled)
30*823f0493SLoGin return mode;
31*823f0493SLoGin
32*823f0493SLoGin /*
33*823f0493SLoGin * See if a user has put the shim into insecure mode. If so, and if the
34*823f0493SLoGin * variable doesn't have the non-volatile attribute set, we might as
35*823f0493SLoGin * well honor that.
36*823f0493SLoGin */
37*823f0493SLoGin size = sizeof(moksbstate);
38*823f0493SLoGin status = get_efi_var(shim_MokSBState_name, &shim_guid, &attr, &size,
39*823f0493SLoGin &moksbstate);
40*823f0493SLoGin
41*823f0493SLoGin /* If it fails, we don't care why. Default to secure */
42*823f0493SLoGin if (status != EFI_SUCCESS)
43*823f0493SLoGin goto secure_boot_enabled;
44*823f0493SLoGin if (!(attr & EFI_VARIABLE_NON_VOLATILE) && moksbstate == 1)
45*823f0493SLoGin return efi_secureboot_mode_disabled;
46*823f0493SLoGin
47*823f0493SLoGin secure_boot_enabled:
48*823f0493SLoGin efi_info("UEFI Secure Boot is enabled.\n");
49*823f0493SLoGin return efi_secureboot_mode_enabled;
50*823f0493SLoGin }
51*823f0493SLoGin
52*823f0493SLoGin /// @brief 打印efi_secureboot_mode
print_efi_secureboot_mode(enum efi_secureboot_mode mode)53*823f0493SLoGin void print_efi_secureboot_mode(enum efi_secureboot_mode mode)
54*823f0493SLoGin {
55*823f0493SLoGin switch (mode) {
56*823f0493SLoGin case efi_secureboot_mode_unknown:
57*823f0493SLoGin efi_info("efi_secureboot_mode: efi_secureboot_mode_unknown\n");
58*823f0493SLoGin break;
59*823f0493SLoGin case efi_secureboot_mode_disabled:
60*823f0493SLoGin efi_info("efi_secureboot_mode: efi_secureboot_mode_disabled\n");
61*823f0493SLoGin break;
62*823f0493SLoGin case efi_secureboot_mode_enabled:
63*823f0493SLoGin efi_info("efi_secureboot_mode: efi_secureboot_mode_enabled\n");
64*823f0493SLoGin break;
65*823f0493SLoGin case efi_secureboot_mode_unset:
66*823f0493SLoGin efi_info("efi_secureboot_mode: efi_secureboot_mode_unset\n");
67*823f0493SLoGin break;
68*823f0493SLoGin default:
69*823f0493SLoGin break;
70*823f0493SLoGin }
71*823f0493SLoGin }