7bea883
From c8218e9b3c38fcd36a2d06eec09952a0c6cee9e0 Mon Sep 17 00:00:00 2001
8eb3db7
From: Peter Jones <pjones@redhat.com>
8eb3db7
Date: Mon, 2 Oct 2017 18:22:13 -0400
8eb3db7
Subject: [PATCH 2/3] Add efi_status_to_str() and rework efi_status_to_err().
8eb3db7
8eb3db7
This adds efi_status_to_str() for use when printing efi_status_t
8eb3db7
messages, and reworks efi_status_to_err() so that the two use a common
8eb3db7
list of errors.
8eb3db7
8eb3db7
Signed-off-by: Peter Jones <pjones@redhat.com>
8eb3db7
---
8eb3db7
 include/linux/efi.h        |   3 ++
7bea883
 drivers/firmware/efi/efi.c | 122 ++++++++++++++++++++++++++++++++++-----------
7bea883
 2 files changed, 95 insertions(+), 30 deletions(-)
8eb3db7
7bea883
diff --git a/include/linux/efi.h b/include/linux/efi.h
7bea883
index 18b16bf5ce1..436b3c93c3d 100644
7bea883
--- a/include/linux/efi.h
7bea883
+++ b/include/linux/efi.h
7bea883
@@ -42,6 +42,8 @@
7bea883
 #define EFI_ABORTED		(21 | (1UL << (BITS_PER_LONG-1)))
7bea883
 #define EFI_SECURITY_VIOLATION	(26 | (1UL << (BITS_PER_LONG-1)))
7bea883
 
7bea883
+#define EFI_IS_ERROR(x)		((x) & (1UL << (BITS_PER_LONG-1)))
7bea883
+
7bea883
 typedef unsigned long efi_status_t;
7bea883
 typedef u8 efi_bool_t;
7bea883
 typedef u16 efi_char16_t;		/* UNICODE character */
7bea883
@@ -1183,6 +1185,7 @@ static inline void efi_set_secure_boot(enum efi_secureboot_mode mode) {}
7bea883
 #endif
7bea883
 
7bea883
 extern int efi_status_to_err(efi_status_t status);
7bea883
+extern const char *efi_status_to_str(efi_status_t status);
7bea883
 
7bea883
 /*
7bea883
  * Variable Attributes
8eb3db7
diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c
7bea883
index 557a47829d0..e8f9c7d84e9 100644
8eb3db7
--- a/drivers/firmware/efi/efi.c
8eb3db7
+++ b/drivers/firmware/efi/efi.c
8eb3db7
@@ -31,6 +31,7 @@
8eb3db7
 #include <linux/acpi.h>
8eb3db7
 #include <linux/ucs2_string.h>
8eb3db7
 #include <linux/memblock.h>
8eb3db7
+#include <linux/bsearch.h>
8eb3db7
 
8eb3db7
 #include <asm/early_ioremap.h>
8eb3db7
 
7bea883
@@ -865,40 +866,101 @@ int efi_mem_type(unsigned long phys_addr)
8eb3db7
 }
8eb3db7
 #endif
8eb3db7
 
8eb3db7
+struct efi_error_code {
8eb3db7
+	efi_status_t status;
8eb3db7
+	int errno;
7bea883
+	const char *description;
8eb3db7
+};
8eb3db7
+
7bea883
+static const struct efi_error_code efi_error_codes[] = {
8eb3db7
+	{ EFI_SUCCESS, 0, "Success"},
8eb3db7
+#if 0
7bea883
+	{ EFI_LOAD_ERROR, -EPICK_AN_ERRNO, "Load Error"},
8eb3db7
+#endif
8eb3db7
+	{ EFI_INVALID_PARAMETER, -EINVAL, "Invalid Parameter"},
8eb3db7
+	{ EFI_UNSUPPORTED, -ENOSYS, "Unsupported"},
8eb3db7
+	{ EFI_BAD_BUFFER_SIZE, -ENOSPC, "Bad Buffer Size"},
8eb3db7
+	{ EFI_BUFFER_TOO_SMALL, -ENOSPC, "Buffer Too Small"},
8eb3db7
+	{ EFI_NOT_READY, -EAGAIN, "Not Ready"},
8eb3db7
+	{ EFI_DEVICE_ERROR, -EIO, "Device Error"},
8eb3db7
+	{ EFI_WRITE_PROTECTED, -EROFS, "Write Protected"},
8eb3db7
+	{ EFI_OUT_OF_RESOURCES, -ENOMEM, "Out of Resources"},
8eb3db7
+#if 0
7bea883
+	{ EFI_VOLUME_CORRUPTED, -EPICK_AN_ERRNO, "Volume Corrupt"},
7bea883
+	{ EFI_VOLUME_FULL, -EPICK_AN_ERRNO, "Volume Full"},
7bea883
+	{ EFI_NO_MEDIA, -EPICK_AN_ERRNO, "No Media"},
7bea883
+	{ EFI_MEDIA_CHANGED, -EPICK_AN_ERRNO, "Media changed"},
8eb3db7
+#endif
8eb3db7
+	{ EFI_NOT_FOUND, -ENOENT, "Not Found"},
8eb3db7
+#if 0
7bea883
+	{ EFI_ACCESS_DENIED, -EPICK_AN_ERRNO, "Access Denied"},
7bea883
+	{ EFI_NO_RESPONSE, -EPICK_AN_ERRNO, "No Response"},
7bea883
+	{ EFI_NO_MAPPING, -EPICK_AN_ERRNO, "No mapping"},
7bea883
+	{ EFI_TIMEOUT, -EPICK_AN_ERRNO, "Time out"},
7bea883
+	{ EFI_NOT_STARTED, -EPICK_AN_ERRNO, "Not started"},
7bea883
+	{ EFI_ALREADY_STARTED, -EPICK_AN_ERRNO, "Already started"},
8eb3db7
+#endif
8eb3db7
+	{ EFI_ABORTED, -EINTR, "Aborted"},
8eb3db7
+#if 0
7bea883
+	{ EFI_ICMP_ERROR, -EPICK_AN_ERRNO, "ICMP Error"},
7bea883
+	{ EFI_TFTP_ERROR, -EPICK_AN_ERRNO, "TFTP Error"},
7bea883
+	{ EFI_PROTOCOL_ERROR, -EPICK_AN_ERRNO, "Protocol Error"},
7bea883
+	{ EFI_INCOMPATIBLE_VERSION, -EPICK_AN_ERRNO, "Incompatible Version"},
8eb3db7
+#endif
8eb3db7
+	{ EFI_SECURITY_VIOLATION, -EACCES, "Security Policy Violation"},
8eb3db7
+#if 0
7bea883
+	{ EFI_CRC_ERROR, -EPICK_AN_ERRNO, "CRC Error"},
7bea883
+	{ EFI_END_OF_MEDIA, -EPICK_AN_ERRNO, "End of Media"},
7bea883
+	{ EFI_END_OF_FILE, -EPICK_AN_ERRNO, "End of File"},
7bea883
+	{ EFI_INVALID_LANGUAGE, -EPICK_AN_ERRNO, "Invalid Languages"},
7bea883
+	{ EFI_COMPROMISED_DATA, -EPICK_AN_ERRNO, "Compromised Data"},
8eb3db7
+
8eb3db7
+	// warnings
7bea883
+	{ EFI_WARN_UNKOWN_GLYPH, -EPICK_AN_ERRNO, "Warning Unknown Glyph"},
7bea883
+	{ EFI_WARN_DELETE_FAILURE, -EPICK_AN_ERRNO, "Warning Delete Failure"},
7bea883
+	{ EFI_WARN_WRITE_FAILURE, -EPICK_AN_ERRNO, "Warning Write Failure"},
7bea883
+	{ EFI_WARN_BUFFER_TOO_SMALL, -EPICK_AN_ERRNO, "Warning Buffer Too Small"},
8eb3db7
+#endif
8eb3db7
+};
8eb3db7
+
8eb3db7
+static int
8eb3db7
+efi_status_cmp_bsearch(const void *key, const void *item)
8eb3db7
+{
8eb3db7
+	u64 status = (u64)(uintptr_t)key;
8eb3db7
+	struct efi_error_code *code = (struct efi_error_code *)item;
8eb3db7
+
8eb3db7
+	if (status < code->status)
8eb3db7
+		return -1;
8eb3db7
+	if (status > code->status)
8eb3db7
+		return 1;
8eb3db7
+	return 0;
8eb3db7
+}
8eb3db7
+
8eb3db7
 int efi_status_to_err(efi_status_t status)
8eb3db7
 {
8eb3db7
-	int err;
7bea883
+	struct efi_error_code *found;
7bea883
+	size_t num = sizeof(efi_error_codes) / sizeof(struct efi_error_code);
7bea883
 
8eb3db7
-	switch (status) {
8eb3db7
-	case EFI_SUCCESS:
8eb3db7
-		err = 0;
8eb3db7
-		break;
8eb3db7
-	case EFI_INVALID_PARAMETER:
8eb3db7
-		err = -EINVAL;
8eb3db7
-		break;
8eb3db7
-	case EFI_OUT_OF_RESOURCES:
8eb3db7
-		err = -ENOSPC;
8eb3db7
-		break;
8eb3db7
-	case EFI_DEVICE_ERROR:
8eb3db7
-		err = -EIO;
8eb3db7
-		break;
8eb3db7
-	case EFI_WRITE_PROTECTED:
8eb3db7
-		err = -EROFS;
8eb3db7
-		break;
8eb3db7
-	case EFI_SECURITY_VIOLATION:
8eb3db7
-		err = -EACCES;
8eb3db7
-		break;
8eb3db7
-	case EFI_NOT_FOUND:
8eb3db7
-		err = -ENOENT;
8eb3db7
-		break;
8eb3db7
-	case EFI_ABORTED:
8eb3db7
-		err = -EINTR;
8eb3db7
-		break;
8eb3db7
-	default:
8eb3db7
-		err = -EINVAL;
8eb3db7
-	}
8eb3db7
+	found = bsearch((void *)(uintptr_t)status, efi_error_codes,
8eb3db7
+			sizeof(struct efi_error_code), num,
8eb3db7
+			efi_status_cmp_bsearch);
8eb3db7
+	if (!found)
8eb3db7
+		return -EINVAL;
8eb3db7
+	return found->errno;
8eb3db7
+}
7bea883
 
7bea883
-	return err;
8eb3db7
+const char *
8eb3db7
+efi_status_to_str(efi_status_t status)
8eb3db7
+{
8eb3db7
+	struct efi_error_code *found;
8eb3db7
+	size_t num = sizeof(efi_error_codes) / sizeof(struct efi_error_code);
8eb3db7
+
8eb3db7
+	found = bsearch((void *)(uintptr_t)status, efi_error_codes,
8eb3db7
+			sizeof(struct efi_error_code), num,
8eb3db7
+			efi_status_cmp_bsearch);
8eb3db7
+	if (!found)
8eb3db7
+		return "Unknown error code";
8eb3db7
+	return found->description;
8eb3db7
 }
8eb3db7
 
8eb3db7
 bool efi_is_table_address(unsigned long phys_addr)
8eb3db7
-- 
7bea883
2.15.0
8eb3db7