1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Secure boot handling.
4  *
5  * Copyright (C) 2013,2014 Linaro Limited
6  *     Roy Franz <roy.franz@linaro.org
7  * Copyright (C) 2013 Red Hat, Inc.
8  *     Mark Salter <msalter@redhat.com>
9  */
10 #include <linux/efi.h>
11 #include <asm/efi.h>
12 
13 #include "efistub.h"
14 
15 /* SHIM variables */
16 static const efi_guid_t shim_guid = EFI_SHIM_LOCK_GUID;
17 static const efi_char16_t shim_MokSBState_name[] = L"MokSBState";
18 
19 static efi_status_t get_var(efi_char16_t *name, efi_guid_t *vendor, u32 *attr,
20 			    unsigned long *data_size, void *data)
21 {
22 	return get_efi_var(name, vendor, attr, data_size, data);
23 }
24 
25 /*
26  * Determine whether we're in secure boot mode.
27  *
28  * Please keep the logic in sync with
29  * arch/x86/xen/efi.c:xen_efi_get_secureboot().
30  */
31 enum efi_secureboot_mode efi_get_secureboot(void)
32 {
33 	u32 attr;
34 	unsigned long size;
35 	enum efi_secureboot_mode mode;
36 	efi_status_t status;
37 	u8 moksbstate;
38 
39 	mode = efi_get_secureboot_mode(get_var);
40 	if (mode == efi_secureboot_mode_unknown) {
41 		efi_err("Could not determine UEFI Secure Boot status.\n");
42 		return efi_secureboot_mode_unknown;
43 	}
44 	if (mode != efi_secureboot_mode_enabled)
45 		return mode;
46 
47 	/*
48 	 * See if a user has put the shim into insecure mode. If so, and if the
49 	 * variable doesn't have the runtime attribute set, we might as well
50 	 * honor that.
51 	 */
52 	size = sizeof(moksbstate);
53 	status = get_efi_var(shim_MokSBState_name, &shim_guid,
54 			     &attr, &size, &moksbstate);
55 
56 	/* If it fails, we don't care why. Default to secure */
57 	if (status != EFI_SUCCESS)
58 		goto secure_boot_enabled;
59 	if (!(attr & EFI_VARIABLE_RUNTIME_ACCESS) && moksbstate == 1)
60 		return efi_secureboot_mode_disabled;
61 
62 secure_boot_enabled:
63 	efi_info("UEFI Secure Boot is enabled.\n");
64 	return efi_secureboot_mode_enabled;
65 }
66