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