From ca14964892597cc786914ecbc5b0b50c8344232a Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Thu, 31 May 2018 11:16:18 +0200
Subject: [PATCH] WIP try to switch libefivar to EDK2 headers.

---
 lib/libefivar/Makefile          |  6 +++++-
 lib/libefivar/efivar.c          |  6 +++---
 lib/libefivar/efivar.h          |  2 +-
 lib/libefivar/libefivar.c       | 21 ++++++++++-----------
 lib/libefivar/libefivar_int.h   |  4 ++--
 sbin/sysctl/Makefile            |  7 ++++++-
 sbin/sysctl/sysctl.c            |  2 +-
 sys/boot/efi/include/efi.h      |  8 --------
 sys/contrib/edk2/Include/Base.h |  4 ++++
 sys/sys/efiio.h                 |  4 ++--
 usr.bin/kdump/Makefile          |  4 ++++
 usr.bin/truss/Makefile          |  4 ++++
 usr.sbin/efivar/Makefile        |  5 +++++
 13 files changed, 47 insertions(+), 30 deletions(-)

diff --git a/lib/libefivar/Makefile b/lib/libefivar/Makefile
index d4df2e5ba2..1bafc0be26 100644
--- a/lib/libefivar/Makefile
+++ b/lib/libefivar/Makefile
@@ -24,13 +24,17 @@
 #
 # $FreeBSD: head/lib/libefivar/Makefile 307071 2016-10-11 22:30:41Z imp $
 
-PACKAGE=lib${LIB}
 LIB=		efivar
 SRCS=		efivar.c libefivar.c
 INCS=		efivar.h
 SHLIB_MAJOR=	1
 MAN=		efivar.3
 
+CFLAGS+=	-I${.CURDIR}/../../sys/contrib/edk2/Include
+.if ${MACHINE_ARCH} == "x86_64"
+CFLAGS+=	-I${.CURDIR}/../../sys/contrib/edk2/Include/X64
+.endif
+
 MLINKS+=efivar.3 efi_variables_supported.3 \
 	efivar.3 efi_del_variable.3 \
 	efivar.3 efi_get_variable.3 \
diff --git a/lib/libefivar/efivar.c b/lib/libefivar/efivar.c
index 6936e738ed..953c3fde67 100644
--- a/lib/libefivar/efivar.c
+++ b/lib/libefivar/efivar.c
@@ -213,8 +213,8 @@ efi_get_next_variable_name(efi_guid_t **guid, char **name)
 {
 	struct efi_var_ioc var;
 	int rv;
-	static efi_char *buf;
-	static size_t buflen = 256 * sizeof(efi_char);
+	static CHAR16 *buf;
+	static size_t buflen = 256 * sizeof(CHAR16);
 	static efi_guid_t retguid;
 	size_t size;
 
@@ -256,7 +256,7 @@ efi_get_next_variable_name(efi_guid_t **guid, char **name)
 
 	if (rv == 0) {
 		*name = NULL; /* XXX */
-		var.name[var.namesize / sizeof(efi_char)] = 0;	/* EFI doesn't NUL terminate */
+		var.name[var.namesize / sizeof(CHAR16)] = 0;	/* EFI doesn't NUL terminate */
 		rv = libefi_ucs2_to_utf8(var.name, name);
 		if (rv != 0)
 			goto errout;
diff --git a/lib/libefivar/efivar.h b/lib/libefivar/efivar.h
index 822cd3c125..f3186bc765 100644
--- a/lib/libefivar/efivar.h
+++ b/lib/libefivar/efivar.h
@@ -30,7 +30,7 @@
 #define	_EFIVAR_H_
 
 #include <uuid.h>
-#include <sys/efi.h>
+#include <Uefi.h>
 #include <sys/endian.h>
 #include <stdint.h>
 
diff --git a/lib/libefivar/libefivar.c b/lib/libefivar/libefivar.c
index e8d167b5f7..4ea4fd339a 100644
--- a/lib/libefivar/libefivar.c
+++ b/lib/libefivar/libefivar.c
@@ -22,18 +22,17 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: head/lib/libefivar/libefivar.c 307071 2016-10-11 22:30:41Z imp $
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/libefivar/libefivar.c 307071 2016-10-11 22:30:41Z imp $");
-
 #include <sys/types.h>
 #include <errno.h>
 #include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <sys/efi.h>
+#include <Uefi.h>
 #include <machine/efi.h>
 
 #include "libefivar_int.h"
@@ -45,10 +44,10 @@ __FBSDID("$FreeBSD: head/lib/libefivar/libefivar.c 307071 2016-10-11 22:30:41Z i
  * return on the resulting string?
  */
 static size_t
-utf8_len_of_ucs2(const efi_char *nm)
+utf8_len_of_ucs2(const CHAR16 *nm)
 {
 	size_t len;
-	efi_char c;
+	CHAR16 c;
 
 	len = 0;
 	while (*nm) {
@@ -65,10 +64,10 @@ utf8_len_of_ucs2(const efi_char *nm)
 }
 
 int
-libefi_ucs2_to_utf8(const efi_char *nm, char **name)
+libefi_ucs2_to_utf8(const CHAR16 *nm, char **name)
 {
 	size_t len, sz;
-	efi_char c;
+	CHAR16 c;
 	char *cp;
 	int freeit = *name == NULL;
 
@@ -113,9 +112,9 @@ libefi_ucs2_to_utf8(const efi_char *nm, char **name)
 }
 
 int
-libefi_utf8_to_ucs2(const char *name, efi_char **nmp, size_t *len)
+libefi_utf8_to_ucs2(const char *name, CHAR16 **nmp, size_t *len)
 {
-	efi_char *nm;
+	CHAR16 *nm;
 	size_t sz;
 	uint32_t ucs4;
 	int c, bytes;
@@ -172,7 +171,7 @@ libefi_utf8_to_ucs2(const char *name, efi_char **nmp, size_t *len)
 					free(nm);
 				return (EILSEQ);
 			}
-			*nm++ = (efi_char)ucs4;
+			*nm++ = (CHAR16)ucs4;
 			sz -= 2;
 		}
 	}
diff --git a/lib/libefivar/libefivar_int.h b/lib/libefivar/libefivar_int.h
index 2bebd41841..2c0fccdf5e 100644
--- a/lib/libefivar/libefivar_int.h
+++ b/lib/libefivar/libefivar_int.h
@@ -29,7 +29,7 @@
 #ifndef _LIBEFI_INT_H_
 #define	_LIBEFI_INT_H_
 
-int libefi_ucs2_to_utf8(const efi_char *, char **);
-int libefi_utf8_to_ucs2(const char *, efi_char **, size_t *);
+int libefi_ucs2_to_utf8(const CHAR16 *, char **);
+int libefi_utf8_to_ucs2(const char *, CHAR16 **, size_t *);
 
 #endif /* _LIBEFI_INT_H_ */
diff --git a/sbin/sysctl/Makefile b/sbin/sysctl/Makefile
index 14144b90de..3db043b6ef 100644
--- a/sbin/sysctl/Makefile
+++ b/sbin/sysctl/Makefile
@@ -1,9 +1,14 @@
 #	@(#)Makefile	8.1 (Berkeley) 6/6/93
 # $FreeBSD: src/sbin/sysctl/Makefile,v 1.5.2.1 2001/04/25 10:58:53 ru Exp $
-# $DragonFly: src/sbin/sysctl/Makefile,v 1.4 2006/10/17 00:55:44 pavalos Exp $
 
 PROG=	sysctl
 MAN=	sysctl.8
+
+CFLAGS+=	-I${.CURDIR}/../../sys/contrib/edk2/Include
+.if ${MACHINE_ARCH} == "x86_64"
+CFLAGS+=	-I${.CURDIR}/../../sys/contrib/edk2/Include/X64
+.endif
+
 NO_STRICT_ALIASING=
 
 .include <bsd.prog.mk>
diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c
index 64785fabf6..218ec75f9e 100644
--- a/sbin/sysctl/sysctl.c
+++ b/sbin/sysctl/sysctl.c
@@ -39,7 +39,7 @@
 #include <sys/param.h>
 
 #ifdef __x86_64__
-#include <sys/efi.h>
+#include <Uefi.h>
 #include <machine/metadata.h>
 #endif
 
diff --git a/sys/boot/efi/include/efi.h b/sys/boot/efi/include/efi.h
index 42f6c04b13..18f550c041 100644
--- a/sys/boot/efi/include/efi.h
+++ b/sys/boot/efi/include/efi.h
@@ -16,14 +16,6 @@
 #define	EFIAPI	__attribute__((ms_abi))
 #endif
 
-/*
- * The following macros are defined unconditionally in the EDK II headers,
- * so get our definitions out of the way.
- */
-#undef NULL
-#undef MIN
-#undef MAX
-
 #include <Uefi.h>
 #include <Guid/Acpi.h>
 #include <Guid/DebugImageInfoTable.h>
diff --git a/sys/contrib/edk2/Include/Base.h b/sys/contrib/edk2/Include/Base.h
index dd727c464c..eb2f6a10a0 100644
--- a/sys/contrib/edk2/Include/Base.h
+++ b/sys/contrib/edk2/Include/Base.h
@@ -354,10 +354,12 @@ struct _LIST_ENTRY {
 ///
 #define FALSE ((BOOLEAN)(0==1))
 
+#ifndef __DragonFly__
 ///
 /// NULL pointer (VOID *)
 ///
 #define NULL  ((VOID *) 0)
+#endif
 
 //
 // Null character
@@ -875,6 +877,7 @@ typedef UINTN  *BASE_LIST;
 #define ALIGN_VARIABLE(Value)  ALIGN_VALUE ((Value), sizeof (UINTN))
 
 
+#ifndef __DragonFly__
 /**
   Return the maximum of two operands.
 
@@ -905,6 +908,7 @@ typedef UINTN  *BASE_LIST;
 **/
 #define MIN(a, b)                       \
   (((a) < (b)) ? (a) : (b))
+#endif
 
 /**
   Return the absolute value of a signed operand.
diff --git a/sys/sys/efiio.h b/sys/sys/efiio.h
index e4db866d97..a0492fb80e 100644
--- a/sys/sys/efiio.h
+++ b/sys/sys/efiio.h
@@ -31,7 +31,7 @@
 
 #include <sys/ioccom.h>
 #include <sys/uuid.h>
-#include <sys/efi.h>
+#include <Uefi.h>
 
 struct efi_get_table_ioc
 {
@@ -41,7 +41,7 @@ struct efi_get_table_ioc
 
 struct efi_var_ioc
 {
-	efi_char *name;		/* User pointer to name, in wide chars */
+	CHAR16 *name;		/* User pointer to name, in wide chars */
 	size_t namesize;	/* Number of wide characters in name */
 	struct uuid vendor;	/* Vendor's UUID for variable */
 	uint32_t attrib;	/* Attributes */
diff --git a/usr.bin/kdump/Makefile b/usr.bin/kdump/Makefile
index 89ed4a7cb0..1bcdc25360 100644
--- a/usr.bin/kdump/Makefile
+++ b/usr.bin/kdump/Makefile
@@ -8,6 +8,10 @@ SRCS=		kdump.c ioctl.c kdump_subr.c subr.c
 CFLAGS+=	-I${.CURDIR}/../ktrace -I${.CURDIR} -I${.CURDIR}/../.. -I${.CURDIR}/../../sys
 CFLAGS+=	-I${.CURDIR}/../../sys/dev/drm/include
 CFLAGS+=	-I${.CURDIR}/../../sys/contrib/dev/acpica/source/include
+CFLAGS+=	-I${.CURDIR}/../../sys/contrib/edk2/Include
+.if ${MACHINE_ARCH} == "x86_64"
+CFLAGS+=	-I${.CURDIR}/../../sys/contrib/edk2/Include/X64
+.endif
 
 CLEANFILES=	ioctl.c kdump_subr.c
 
diff --git a/usr.bin/truss/Makefile b/usr.bin/truss/Makefile
index 531af2fc79..fe8275d5c3 100644
--- a/usr.bin/truss/Makefile
+++ b/usr.bin/truss/Makefile
@@ -6,6 +6,10 @@ SRCS=	main.c setup.c syscalls.c syscalls.h ioctl.c ${MACHINE_ARCH}-fbsd.c
 CFLAGS+= -I${.CURDIR} -I${.CURDIR}/../../sys
 CFLAGS+= -I${.CURDIR}/../../sys/dev/drm/include -I.
 CFLAGS+= -I${.CURDIR}/../../sys/contrib/dev/acpica/source/include
+CFLAGS+=	-I${.CURDIR}/../../sys/contrib/edk2/Include
+.if ${MACHINE_ARCH} == "x86_64"
+CFLAGS+=	-I${.CURDIR}/../../sys/contrib/edk2/Include/X64
+.endif
 
 CLEANFILES+=syscalls.master syscalls.h ioctl.c
 
diff --git a/usr.sbin/efivar/Makefile b/usr.sbin/efivar/Makefile
index 1c6fe20524..9b0c9b0847 100644
--- a/usr.sbin/efivar/Makefile
+++ b/usr.sbin/efivar/Makefile
@@ -6,4 +6,9 @@ MAN=	efivar.8
 LDADD=	-lefivar
 DPADD=	${LIBEFIVAR}
 
+CFLAGS+=	-I${.CURDIR}/../../sys/contrib/edk2/Include
+.if ${MACHINE_ARCH} == "x86_64"
+CFLAGS+=	-I${.CURDIR}/../../sys/contrib/edk2/Include/X64
+.endif
+
 .include <bsd.prog.mk>
-- 
2.17.0

