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