Lee, Chun-Yi 454ab3
From b5123d0553f4ed5e734f6457696cdd30228d1eee Mon Sep 17 00:00:00 2001
Lee, Chun-Yi 454ab3
From: David Howells <dhowells@redhat.com>
Lee, Chun-Yi 454ab3
Date: Tue, 27 Feb 2018 10:04:55 +0000
Lee, Chun-Yi 454ab3
Subject: [PATCH 2/3] efi: Add an EFI_SECURE_BOOT flag to indicate secure
Lee, Chun-Yi 454ab3
 boot mode
Lee, Chun-Yi 454ab3
Patch-mainline: Never, Fedora Core 32
Lee, Chun-Yi 454ab3
References: jsc#SLE-9870
Lee, Chun-Yi 454ab3
Lee, Chun-Yi 454ab3
UEFI machines can be booted in Secure Boot mode.  Add an EFI_SECURE_BOOT
Lee, Chun-Yi 454ab3
flag that can be passed to efi_enabled() to find out whether secure boot is
Lee, Chun-Yi 454ab3
enabled.
Lee, Chun-Yi 454ab3
Lee, Chun-Yi 454ab3
Move the switch-statement in x86's setup_arch() that inteprets the
Lee, Chun-Yi 454ab3
secure_boot boot parameter to generic code and set the bit there.
Lee, Chun-Yi 454ab3
Lee, Chun-Yi 454ab3
Suggested-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Lee, Chun-Yi 454ab3
Signed-off-by: David Howells <dhowells@redhat.com>
Lee, Chun-Yi 454ab3
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Lee, Chun-Yi 454ab3
cc: linux-efi@vger.kernel.org
Lee, Chun-Yi 454ab3
[Rebased for context; efi_is_table_address was moved to arch/x86]
Lee, Chun-Yi 454ab3
Signed-off-by: Jeremy Cline <jcline@redhat.com>
Lee, Chun-Yi 454ab3
Acked-by: Lee, Chun-Yi <jlee@suse.com>
Lee, Chun-Yi 454ab3
Signed-off-by: Chester Lin <clin@suse.com>
Lee, Chun-Yi 454ab3
---
Lee, Chun-Yi 454ab3
 arch/x86/kernel/setup.c           |   14 +-------------
Lee, Chun-Yi 454ab3
 drivers/firmware/efi/Makefile     |    1 +
Lee, Chun-Yi 454ab3
 drivers/firmware/efi/secureboot.c |   38 ++++++++++++++++++++++++++++++++++++++
Lee, Chun-Yi 454ab3
 include/linux/efi.h               |   19 ++++++++++++-------
Lee, Chun-Yi 454ab3
 4 files changed, 52 insertions(+), 20 deletions(-)
Lee, Chun-Yi 454ab3
 create mode 100644 drivers/firmware/efi/secureboot.c
Lee, Chun-Yi 454ab3
Lee, Chun-Yi 454ab3
--- a/arch/x86/kernel/setup.c
Lee, Chun-Yi 454ab3
+++ b/arch/x86/kernel/setup.c
Lee, Chun-Yi 454ab3
@@ -1114,19 +1114,7 @@ void __init setup_arch(char **cmdline_p)
Lee, Chun-Yi 454ab3
 	/* Allocate bigger log buffer */
Lee, Chun-Yi 454ab3
 	setup_log_buf(1);
Lee, Chun-Yi 454ab3
 
Lee, Chun-Yi 454ab3
-	if (efi_enabled(EFI_BOOT)) {
Lee, Chun-Yi 454ab3
-		switch (boot_params.secure_boot) {
Lee, Chun-Yi 454ab3
-		case efi_secureboot_mode_disabled:
Lee, Chun-Yi 454ab3
-			pr_info("Secure boot disabled\n");
Lee, Chun-Yi 454ab3
-			break;
Lee, Chun-Yi 454ab3
-		case efi_secureboot_mode_enabled:
Lee, Chun-Yi 454ab3
-			pr_info("Secure boot enabled\n");
Lee, Chun-Yi 454ab3
-			break;
Lee, Chun-Yi 454ab3
-		default:
Lee, Chun-Yi 454ab3
-			pr_info("Secure boot could not be determined\n");
Lee, Chun-Yi 454ab3
-			break;
Lee, Chun-Yi 454ab3
-		}
Lee, Chun-Yi 454ab3
-	}
Lee, Chun-Yi 454ab3
+	efi_set_secure_boot(boot_params.secure_boot);
Lee, Chun-Yi 454ab3
 
Lee, Chun-Yi 454ab3
 	reserve_initrd();
Lee, Chun-Yi 454ab3
 
Lee, Chun-Yi 454ab3
--- a/drivers/firmware/efi/Makefile
Lee, Chun-Yi 454ab3
+++ b/drivers/firmware/efi/Makefile
Lee, Chun-Yi 454ab3
@@ -27,6 +27,7 @@ obj-$(CONFIG_EFI_FAKE_MEMMAP)		+= fake_m
Lee, Chun-Yi 454ab3
 obj-$(CONFIG_EFI_BOOTLOADER_CONTROL)	+= efibc.o
Lee, Chun-Yi 454ab3
 obj-$(CONFIG_EFI_TEST)			+= test/
Lee, Chun-Yi 454ab3
 obj-$(CONFIG_EFI_DEV_PATH_PARSER)	+= dev-path-parser.o
Lee, Chun-Yi 454ab3
+obj-$(CONFIG_EFI)			+= secureboot.o
Lee, Chun-Yi 454ab3
 obj-$(CONFIG_APPLE_PROPERTIES)		+= apple-properties.o
Lee, Chun-Yi 454ab3
 obj-$(CONFIG_EFI_RCI2_TABLE)		+= rci2-table.o
Lee, Chun-Yi 454ab3
 obj-$(CONFIG_EFI_EMBEDDED_FIRMWARE)	+= embedded-firmware.o
Lee, Chun-Yi 454ab3
--- /dev/null
Lee, Chun-Yi 454ab3
+++ b/drivers/firmware/efi/secureboot.c
Lee, Chun-Yi 454ab3
@@ -0,0 +1,38 @@
Lee, Chun-Yi 454ab3
+/* Core kernel secure boot support.
Lee, Chun-Yi 454ab3
+ *
Lee, Chun-Yi 454ab3
+ * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
Lee, Chun-Yi 454ab3
+ * Written by David Howells (dhowells@redhat.com)
Lee, Chun-Yi 454ab3
+ *
Lee, Chun-Yi 454ab3
+ * This program is free software; you can redistribute it and/or
Lee, Chun-Yi 454ab3
+ * modify it under the terms of the GNU General Public Licence
Lee, Chun-Yi 454ab3
+ * as published by the Free Software Foundation; either version
Lee, Chun-Yi 454ab3
+ * 2 of the Licence, or (at your option) any later version.
Lee, Chun-Yi 454ab3
+ */
Lee, Chun-Yi 454ab3
+
Lee, Chun-Yi 454ab3
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Lee, Chun-Yi 454ab3
+
Lee, Chun-Yi 454ab3
+#include <linux/efi.h>
Lee, Chun-Yi 454ab3
+#include <linux/kernel.h>
Lee, Chun-Yi 454ab3
+#include <linux/printk.h>
Lee, Chun-Yi 454ab3
+
Lee, Chun-Yi 454ab3
+/*
Lee, Chun-Yi 454ab3
+ * Decide what to do when UEFI secure boot mode is enabled.
Lee, Chun-Yi 454ab3
+ */
Lee, Chun-Yi 454ab3
+void __init efi_set_secure_boot(enum efi_secureboot_mode mode)
Lee, Chun-Yi 454ab3
+{
Lee, Chun-Yi 454ab3
+	if (efi_enabled(EFI_BOOT)) {
Lee, Chun-Yi 454ab3
+		switch (mode) {
Lee, Chun-Yi 454ab3
+		case efi_secureboot_mode_disabled:
Lee, Chun-Yi 454ab3
+			pr_info("Secure boot disabled\n");
Lee, Chun-Yi 454ab3
+			break;
Lee, Chun-Yi 454ab3
+		case efi_secureboot_mode_enabled:
Lee, Chun-Yi 454ab3
+			set_bit(EFI_SECURE_BOOT, &efi.flags);
Lee, Chun-Yi 454ab3
+			pr_info("Secure boot enabled\n");
Lee, Chun-Yi 454ab3
+			break;
Lee, Chun-Yi 454ab3
+		default:
Lee, Chun-Yi 454ab3
+			pr_warn("Secure boot could not be determined (mode %u)\n",
Lee, Chun-Yi 454ab3
+				   mode);
Lee, Chun-Yi 454ab3
+			break;
Lee, Chun-Yi 454ab3
+		}
Lee, Chun-Yi 454ab3
+	}
Lee, Chun-Yi 454ab3
+}
Lee, Chun-Yi 454ab3
--- a/include/linux/efi.h
Lee, Chun-Yi 454ab3
+++ b/include/linux/efi.h
Lee, Chun-Yi 454ab3
@@ -847,6 +847,14 @@ extern int __init efi_setup_pcdp_console
Lee, Chun-Yi 454ab3
 #define EFI_MEM_ATTR		10	/* Did firmware publish an EFI_MEMORY_ATTRIBUTES table? */
Lee, Chun-Yi 454ab3
 #define EFI_MEM_NO_SOFT_RESERVE	11	/* Is the kernel configured to ignore soft reservations? */
Lee, Chun-Yi 454ab3
 #define EFI_PRESERVE_BS_REGIONS	12	/* Are EFI boot-services memory segments available? */
Lee, Chun-Yi 454ab3
+#define EFI_SECURE_BOOT		13	/* Are we in Secure Boot mode? */
Lee, Chun-Yi 454ab3
+
Lee, Chun-Yi 454ab3
+enum efi_secureboot_mode {
Lee, Chun-Yi 454ab3
+	efi_secureboot_mode_unset,
Lee, Chun-Yi 454ab3
+	efi_secureboot_mode_unknown,
Lee, Chun-Yi 454ab3
+	efi_secureboot_mode_disabled,
Lee, Chun-Yi 454ab3
+	efi_secureboot_mode_enabled,
Lee, Chun-Yi 454ab3
+};
Lee, Chun-Yi 454ab3
 
Lee, Chun-Yi 454ab3
 #ifdef CONFIG_EFI
Lee, Chun-Yi 454ab3
 /*
Lee, Chun-Yi 454ab3
@@ -871,6 +879,8 @@ static inline bool efi_rt_services_suppo
Lee, Chun-Yi 454ab3
 	return (efi.runtime_supported_mask & mask) == mask;
Lee, Chun-Yi 454ab3
 }
Lee, Chun-Yi 454ab3
 extern void efi_find_mirror(void);
Lee, Chun-Yi 454ab3
+
Lee, Chun-Yi 454ab3
+extern void __init efi_set_secure_boot(enum efi_secureboot_mode mode);
Lee, Chun-Yi 454ab3
 #else
Lee, Chun-Yi 454ab3
 static inline bool efi_enabled(int feature)
Lee, Chun-Yi 454ab3
 {
Lee, Chun-Yi 454ab3
@@ -890,6 +900,8 @@ static inline bool efi_rt_services_suppo
Lee, Chun-Yi 454ab3
 }
Lee, Chun-Yi 454ab3
 
Lee, Chun-Yi 454ab3
 static inline void efi_find_mirror(void) {}
Lee, Chun-Yi 454ab3
+
Lee, Chun-Yi 454ab3
+static inline void efi_set_secure_boot(enum efi_secureboot_mode mode) {}
Lee, Chun-Yi 454ab3
 #endif
Lee, Chun-Yi 454ab3
 
Lee, Chun-Yi 454ab3
 extern int efi_status_to_err(efi_status_t status);
Lee, Chun-Yi 454ab3
@@ -1105,13 +1117,6 @@ static inline bool efi_runtime_disabled(
Lee, Chun-Yi 454ab3
 extern void efi_call_virt_check_flags(unsigned long flags, const char *call);
Lee, Chun-Yi 454ab3
 extern unsigned long efi_call_virt_save_flags(void);
Lee, Chun-Yi 454ab3
 
Lee, Chun-Yi 454ab3
-enum efi_secureboot_mode {
Lee, Chun-Yi 454ab3
-	efi_secureboot_mode_unset,
Lee, Chun-Yi 454ab3
-	efi_secureboot_mode_unknown,
Lee, Chun-Yi 454ab3
-	efi_secureboot_mode_disabled,
Lee, Chun-Yi 454ab3
-	efi_secureboot_mode_enabled,
Lee, Chun-Yi 454ab3
-};
Lee, Chun-Yi 454ab3
-
Lee, Chun-Yi 454ab3
 static inline
Lee, Chun-Yi 454ab3
 enum efi_secureboot_mode efi_get_secureboot_mode(efi_get_variable_t *get_var)
Lee, Chun-Yi 454ab3
 {