[PATCH 1/2 v2] efi: add a function to convert the status code to a string

Lee, Chun-Yi joeyli.kernel at gmail.com
Fri Dec 13 09:06:45 UTC 2019


This function can be used to convert EFI status code to a string
to improve the readability of log.

v2:
Moved the convert function to efi.c

Signed-off-by: "Lee, Chun-Yi" <jlee at suse.com>
---
 drivers/firmware/efi/efi.c | 32 ++++++++++++++++++++++++++++++++
 include/linux/efi.h        |  1 +
 2 files changed, 33 insertions(+)

diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c
index e98bbf8e56d9..8bdc1c17eb5d 100644
--- a/drivers/firmware/efi/efi.c
+++ b/drivers/firmware/efi/efi.c
@@ -954,6 +954,38 @@ int efi_status_to_err(efi_status_t status)
 	return err;
 }
 
+#define EFI_STATUS_STR(_status) \
+	EFI_##_status : return "EFI_" __stringify(_status)
+
+const char *efi_status_to_str(efi_status_t status)
+{
+	switch (status) {
+	case EFI_STATUS_STR(SUCCESS);
+	case EFI_STATUS_STR(LOAD_ERROR);
+	case EFI_STATUS_STR(INVALID_PARAMETER);
+	case EFI_STATUS_STR(UNSUPPORTED);
+	case EFI_STATUS_STR(BAD_BUFFER_SIZE);
+	case EFI_STATUS_STR(BUFFER_TOO_SMALL);
+	case EFI_STATUS_STR(NOT_READY);
+	case EFI_STATUS_STR(DEVICE_ERROR);
+	case EFI_STATUS_STR(WRITE_PROTECTED);
+	case EFI_STATUS_STR(OUT_OF_RESOURCES);
+	case EFI_STATUS_STR(NOT_FOUND);
+	case EFI_STATUS_STR(ABORTED);
+	case EFI_STATUS_STR(SECURITY_VIOLATION);
+	}
+	/*
+	 * There are two possibilities for this message to be exposed:
+	 * - Caller feeds a unknown status code from firmware.
+	 * - A new status code be defined in efi.h but we forgot to update
+	 *   this function.
+	 */
+	pr_warn("Unknown efi status: 0x%lx\n", status);
+
+	return "Unknown efi status";
+}
+EXPORT_SYMBOL(efi_status_to_str);
+
 static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock);
 static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init;
 
diff --git a/include/linux/efi.h b/include/linux/efi.h
index d87acf62958e..2c6848d2b112 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -1228,6 +1228,7 @@ efi_capsule_pending(int *reset_type)
 #endif
 
 extern int efi_status_to_err(efi_status_t status);
+extern const char *efi_status_to_str(efi_status_t status);
 
 /*
  * Variable Attributes
-- 
2.16.4



More information about the Linux-security-module-archive mailing list