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