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