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