[PATCH] uefi: add helper function for efi test module init

Ivan Hu ivan.hu at canonical.com
Thu Mar 26 08:39:56 UTC 2020


Signed-off-by: Ivan Hu <ivan.hu at canonical.com>
---
 src/lib/include/fwts_efi_module.h        |  1 +
 src/lib/src/fwts_efi_module.c            | 31 ++++++++++++++++++++++++
 src/uefi/securebootcert/securebootcert.c | 22 +++--------------
 src/uefi/uefirtauthvar/uefirtauthvar.c   | 19 +--------------
 src/uefi/uefirtmisc/uefirtmisc.c         | 19 +--------------
 src/uefi/uefirttime/uefirttime.c         | 19 +--------------
 src/uefi/uefirtvariable/uefirtvariable.c | 19 +--------------
 src/uefi/uefivarinfo/uefivarinfo.c       | 20 +--------------
 8 files changed, 40 insertions(+), 110 deletions(-)

diff --git a/src/lib/include/fwts_efi_module.h b/src/lib/include/fwts_efi_module.h
index 7b40332a..824699a3 100644
--- a/src/lib/include/fwts_efi_module.h
+++ b/src/lib/include/fwts_efi_module.h
@@ -25,5 +25,6 @@ int fwts_lib_efi_runtime_unload_module(fwts_framework *fw);
 int fwts_lib_efi_runtime_open(void);
 int fwts_lib_efi_runtime_close(int fd);
 int fwts_lib_efi_runtime_kernel_lockdown(fwts_framework *fw);
+int fwts_lib_efi_runtime_module_init(fwts_framework *fw, int *fd);
 
 #endif
diff --git a/src/lib/src/fwts_efi_module.c b/src/lib/src/fwts_efi_module.c
index 3c21b6d3..0767787b 100644
--- a/src/lib/src/fwts_efi_module.c
+++ b/src/lib/src/fwts_efi_module.c
@@ -205,3 +205,34 @@ int fwts_lib_efi_runtime_kernel_lockdown(fwts_framework *fw)
 	free(data);
 	return FWTS_OK;
 }
+
+/*
+ *  fwts_lib_efi_runtime_module_init()
+ *  check the EFI firmware support and load efi_test driver
+ *  also check if the kernel is in lockdown mode.
+ */
+int fwts_lib_efi_runtime_module_init(fwts_framework *fw, int *fd)
+{
+
+	if (fwts_firmware_detect() != FWTS_FIRMWARE_UEFI) {
+		fwts_log_info(fw, "Cannot detect any UEFI firmware. Aborted.");
+		return FWTS_ABORTED;
+	}
+
+	if (fwts_lib_efi_runtime_kernel_lockdown(fw) == FWTS_ABORTED) {
+		return FWTS_ABORTED;
+	}
+
+	if (fwts_lib_efi_runtime_load_module(fw) != FWTS_OK) {
+		fwts_log_info(fw, "Cannot load efi_runtime module. Aborted.");
+		return FWTS_ABORTED;
+	}
+
+	*fd = fwts_lib_efi_runtime_open();
+	if (*fd == -1) {
+		fwts_log_info(fw, "Cannot open EFI test driver. Aborted.");
+		return FWTS_ABORTED;
+	}
+
+	return FWTS_OK;
+}
diff --git a/src/uefi/securebootcert/securebootcert.c b/src/uefi/securebootcert/securebootcert.c
index 87ace3e5..fdf5e482 100644
--- a/src/uefi/securebootcert/securebootcert.c
+++ b/src/uefi/securebootcert/securebootcert.c
@@ -417,28 +417,12 @@ static void securebootcert_var(fwts_framework *fw, fwts_uefi_var *var)
 
 static int securebootcert_init(fwts_framework *fw)
 {
-	if (fwts_firmware_detect() != FWTS_FIRMWARE_UEFI) {
-		fwts_log_info(fw, "Cannot detect any UEFI firmware. Aborted.");
-		return FWTS_SKIP;
-	}
 
-	if (!fwts_uefi_efivars_iface_exist()) {
-		fwts_log_info(fw, "Cannot detect efivars interface. Aborted.");
+	if (fwts_lib_efi_runtime_module_init(fw, &fd) == FWTS_ABORTED)
 		return FWTS_ABORTED;
-	}
 
-	if (fwts_lib_efi_runtime_kernel_lockdown(fw) == FWTS_ABORTED) {
-		return FWTS_ABORTED;
-	}
-
-	if (fwts_lib_efi_runtime_load_module(fw) != FWTS_OK) {
-		fwts_log_info(fw, "Cannot load efi_runtime module. Aborted.");
-		return FWTS_ABORTED;
-	}
-
-	fd = fwts_lib_efi_runtime_open();
-	if (fd == -1) {
-		fwts_log_info(fw, "Cannot open EFI test driver. Aborted.");
+	if (!fwts_uefi_efivars_iface_exist()) {
+		fwts_log_info(fw, "Cannot detect efivars interface. Aborted.");
 		return FWTS_ABORTED;
 	}
 
diff --git a/src/uefi/uefirtauthvar/uefirtauthvar.c b/src/uefi/uefirtauthvar/uefirtauthvar.c
index a2a88d77..ca72fa24 100644
--- a/src/uefi/uefirtauthvar/uefirtauthvar.c
+++ b/src/uefi/uefirtauthvar/uefirtauthvar.c
@@ -115,25 +115,8 @@ static void uefirtvariable_env_cleanup(void)
 
 static int uefirtauthvar_init(fwts_framework *fw)
 {
-	if (fwts_firmware_detect() != FWTS_FIRMWARE_UEFI) {
-		fwts_log_info(fw, "Cannot detect any UEFI firmware. Aborted.");
+	if (fwts_lib_efi_runtime_module_init(fw, &fd) == FWTS_ABORTED)
 		return FWTS_ABORTED;
-	}
-
-	if (fwts_lib_efi_runtime_kernel_lockdown(fw) == FWTS_ABORTED) {
-		return FWTS_ABORTED;
-	}
-
-	if (fwts_lib_efi_runtime_load_module(fw) != FWTS_OK) {
-		fwts_log_info(fw, "Cannot load efi_runtime module. Aborted.");
-		return FWTS_ABORTED;
-	}
-
-	fd = fwts_lib_efi_runtime_open();
-	if (fd == -1) {
-		fwts_log_info(fw, "Cannot open EFI test driver. Aborted.");
-		return FWTS_ABORTED;
-	}
 
 	uefirtvariable_env_cleanup();
 
diff --git a/src/uefi/uefirtmisc/uefirtmisc.c b/src/uefi/uefirtmisc/uefirtmisc.c
index c4176992..a2dc6967 100644
--- a/src/uefi/uefirtmisc/uefirtmisc.c
+++ b/src/uefi/uefirtmisc/uefirtmisc.c
@@ -45,25 +45,8 @@ static EFI_GUID gEfiCapsuleHeaderGuid = EFI_CAPSULE_GUID;
 
 static int uefirtmisc_init(fwts_framework *fw)
 {
-	if (fwts_firmware_detect() != FWTS_FIRMWARE_UEFI) {
-		fwts_log_info(fw, "Cannot detect any UEFI firmware. Aborted.");
+	if (fwts_lib_efi_runtime_module_init(fw, &fd) == FWTS_ABORTED)
 		return FWTS_ABORTED;
-	}
-
-	if (fwts_lib_efi_runtime_kernel_lockdown(fw) == FWTS_ABORTED) {
-		return FWTS_ABORTED;
-	}
-
-	if (fwts_lib_efi_runtime_load_module(fw) != FWTS_OK) {
-		fwts_log_info(fw, "Cannot load efi_runtime module. Aborted.");
-		return FWTS_ABORTED;
-	}
-
-	fd = fwts_lib_efi_runtime_open();
-	if (fd == -1) {
-		fwts_log_info(fw, "Cannot open EFI test driver. Aborted.");
-		return FWTS_ABORTED;
-	}
 
 	return FWTS_OK;
 }
diff --git a/src/uefi/uefirttime/uefirttime.c b/src/uefi/uefirttime/uefirttime.c
index ea8c3577..e0aa7071 100644
--- a/src/uefi/uefirttime/uefirttime.c
+++ b/src/uefi/uefirttime/uefirttime.c
@@ -169,25 +169,8 @@ static void addonehour(EFI_TIME *time)
 
 static int uefirttime_init(fwts_framework *fw)
 {
-	if (fwts_firmware_detect() != FWTS_FIRMWARE_UEFI) {
-		fwts_log_info(fw, "Cannot detect any UEFI firmware. Aborted.");
+	if (fwts_lib_efi_runtime_module_init(fw, &fd) == FWTS_ABORTED)
 		return FWTS_ABORTED;
-	}
-
-	if (fwts_lib_efi_runtime_kernel_lockdown(fw) == FWTS_ABORTED) {
-		return FWTS_ABORTED;
-	}
-
-	if (fwts_lib_efi_runtime_load_module(fw) != FWTS_OK) {
-		fwts_log_info(fw, "Cannot load efi_runtime module. Aborted.");
-		return FWTS_ABORTED;
-	}
-
-	fd = fwts_lib_efi_runtime_open();
-	if (fd == -1) {
-		fwts_log_info(fw, "Cannot open EFI test driver. Aborted.");
-		return FWTS_ABORTED;
-	}
 
 	return FWTS_OK;
 }
diff --git a/src/uefi/uefirtvariable/uefirtvariable.c b/src/uefi/uefirtvariable/uefirtvariable.c
index 3986d1d3..5a25e3ca 100644
--- a/src/uefi/uefirtvariable/uefirtvariable.c
+++ b/src/uefi/uefirtvariable/uefirtvariable.c
@@ -95,25 +95,8 @@ static void uefirtvariable_env_cleanup(void)
 
 static int uefirtvariable_init(fwts_framework *fw)
 {
-	if (fwts_firmware_detect() != FWTS_FIRMWARE_UEFI) {
-		fwts_log_info(fw, "Cannot detect any UEFI firmware. Aborted.");
+	if (fwts_lib_efi_runtime_module_init(fw, &fd) == FWTS_ABORTED)
 		return FWTS_ABORTED;
-	}
-
-	if (fwts_lib_efi_runtime_kernel_lockdown(fw) == FWTS_ABORTED) {
-		return FWTS_ABORTED;
-	}
-
-	if (fwts_lib_efi_runtime_load_module(fw) != FWTS_OK) {
-		fwts_log_info(fw, "Cannot load efi_runtime module. Aborted.");
-		return FWTS_ABORTED;
-	}
-
-	fd = fwts_lib_efi_runtime_open();
-	if (fd == -1) {
-		fwts_log_info(fw, "Cannot open EFI test driver. Aborted.");
-		return FWTS_ABORTED;
-	}
 
 	uefirtvariable_env_cleanup();
 
diff --git a/src/uefi/uefivarinfo/uefivarinfo.c b/src/uefi/uefivarinfo/uefivarinfo.c
index b407f5c6..6a26b425 100644
--- a/src/uefi/uefivarinfo/uefivarinfo.c
+++ b/src/uefi/uefivarinfo/uefivarinfo.c
@@ -36,26 +36,8 @@ static int fd;
 
 static int uefivarinfo_init(fwts_framework *fw)
 {
-	if (fwts_firmware_detect() != FWTS_FIRMWARE_UEFI) {
-		fwts_log_info(fw, "Cannot detect any UEFI firmware. Aborted.");
+	if (fwts_lib_efi_runtime_module_init(fw, &fd) == FWTS_ABORTED)
 		return FWTS_ABORTED;
-	}
-
-	if (fwts_lib_efi_runtime_kernel_lockdown(fw) == FWTS_ABORTED) {
-		return FWTS_ABORTED;
-	}
-
-	if (fwts_lib_efi_runtime_load_module(fw) != FWTS_OK) {
-		fwts_log_info(fw, "Cannot load efi_runtime module. Aborted.");
-		return FWTS_ABORTED;
-	}
-
-	fd = fwts_lib_efi_runtime_open();
-	if (fd == -1) {
-		fwts_log_info(fw, "Cannot open EFI test driver. Aborted.");
-		return FWTS_ABORTED;
-	}
-
 	return FWTS_OK;
 }
 
-- 
2.17.1




More information about the fwts-devel mailing list