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