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