From ba1437f5e536189b290ff8b9defbe80704eb70df Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Sat, 28 Feb 2026 02:10:40 +0100
Subject: [PATCH 3/3] libefivar: Remove some code that was copied from edk2's
 ProcessorBind.h.

---
 lib/libefivar/Makefile           |  8 +++-
 lib/libefivar/efi-osdep.h        | 68 +++-----------------------------
 lib/libefivar/efivar-dp-format.c |  4 +-
 lib/libefivar/efivar-dp-parse.c  |  4 +-
 lib/libefivar/efivar-dp-xlate.c  |  7 ++--
 lib/libefivar/uefi-dplib.h       |  6 +--
 6 files changed, 21 insertions(+), 76 deletions(-)

diff --git a/lib/libefivar/Makefile b/lib/libefivar/Makefile
index a320d1fac4b..546718e0fd4 100644
--- a/lib/libefivar/Makefile
+++ b/lib/libefivar/Makefile
@@ -25,7 +25,6 @@
 # $FreeBSD: head/lib/libefivar/Makefile 326458 2017-12-02 07:29:19Z imp $
 
 EFIBOOT=${.CURDIR}/../../stand/boot/efi
-EDK2INC=${.CURDIR}/../../stand/contrib/edk2/MdePkg/Include
 
 .PATH:	${EFIBOOT}/libefi
 
@@ -39,7 +38,12 @@ SHLIB_MAJOR=	1
 MAN=		efivar.3
 
 CFLAGS+=	-I${EFIBOOT}/include
-CFLAGS+=	-I${.CURDIR} -I${EDK2INC}
+CFLAGS+=	-I${.CURDIR}
+CFLAGS+=	-I${.CURDIR}/../../stand/contrib/edk2/MdePkg/Include
+
+.if ${MACHINE_ARCH} == "x86_64"
+CFLAGS+=	-I${.CURDIR}/../../stand/contrib/edk2/MdePkg/Include/X64
+.endif
 
 .PATH:		${.CURDIR}/../../sbin/gpt
 SRCS+=		gpt.c map.c
diff --git a/lib/libefivar/efi-osdep.h b/lib/libefivar/efi-osdep.h
index 4462e758dfb..4f23c030c7b 100644
--- a/lib/libefivar/efi-osdep.h
+++ b/lib/libefivar/efi-osdep.h
@@ -28,49 +28,7 @@
 #ifndef	_EFI_OSDEP_H_
 #define	_EFI_OSDEP_H_
 
-/*
- * Defines to adjust the types that EDK2 uses for FreeBSD so we can
- * use the code and headers mostly unchanged. The headers are imported
- * all into one directory to avoid case issues with filenames and
- * included. The actual code is heavily modified since it has too many
- * annoying dependencies that are difficult to satisfy.
- */
-
-#include <sys/cdefs.h>
 #include <stdlib.h>
-#include <stdint.h>
-#include <uuid.h>
-
-typedef int8_t INT8;
-typedef int16_t INT16;
-typedef int32_t INT32;
-typedef int64_t INT64;
-typedef intptr_t INTN;
-typedef uint8_t UINT8;
-typedef uint16_t UINT16;
-typedef uint32_t UINT32;
-typedef uint64_t UINT64;
-typedef uintptr_t UINTN;
-//typedef uintptr_t EFI_PHYSICAL_ADDRESS;
-//typedef uint32_t EFI_IPv4_ADDRESS;
-//typedef uint8_t EFI_MAC_ADDRESS[6];
-//typedef uint8_t EFI_IPv6_ADDRESS[16];
-typedef uint8_t CHAR8;
-typedef uint16_t CHAR16;
-typedef UINT8 BOOLEAN;
-typedef void VOID;
-//typedef uuid_t GUID;
-//typedef uuid_t EFI_GUID;
-
-/* We can't actually call this stuff, so snip out API syntactic sugar */
-#define INTERFACE_DECL(x)
-#define EFIAPI
-#define IN
-#define OUT
-#define CONST const
-#define OPTIONAL
-//#define TRUE 1
-//#define FALSE 0
 
 /*
  * EDK2 has fine definitions for these, so let it define them.
@@ -78,13 +36,9 @@ typedef void VOID;
 #undef NULL
 #undef EFI_PAGE_SIZE
 #undef EFI_PAGE_MASK
-
-/*
- * Note: the EDK2 code assumed #pragma packed works and PACKED is a
- * workaround for some old toolchain issues for EDK2 that aren't
- * relevent to FreeBSD.
- */
-#define PACKED
+#undef MAX
+#undef MAX_UINTN
+#undef MIN
 
 /*
  * Since we're not compiling for the UEFI boot time (which use ms abi
@@ -93,18 +47,6 @@ typedef void VOID;
  */
 #define NO_MSABI_VA_FUNCS 1
 
-/*
- * Finally, we need to define the processor we are in EDK2 terms.
- */
-#if defined(__i386__)
-#define MDE_CPU_IA32
-#elif defined(__amd64__)
-#define MDE_CPU_X64
-#elif defined(__arm__)
-#define MDE_CPU_ARM
-#elif defined(__aarch64__)
-#define MDE_CPU_AARCH64
-#endif
-/* FreeBSD doesn't have/use MDE_CPU_EBC or MDE_CPU_IPF (ia64) */
+#include <ProcessorBind.h>
 
-#endif /* _EFI_OSDEP_H_ */
+#endif /* !_EFI_OSDEP_H_ */
diff --git a/lib/libefivar/efivar-dp-format.c b/lib/libefivar/efivar-dp-format.c
index ff4ab70cde4..d8f2cd6aa42 100644
--- a/lib/libefivar/efivar-dp-format.c
+++ b/lib/libefivar/efivar-dp-format.c
@@ -34,9 +34,11 @@
 #include <stdio.h>
 #include <string.h>
 
+/* XXX: must come first */
+#include "efi-osdep.h"
+
 #include "libefivar_int.h"
 
-#include "efi-osdep.h"
 #include "efivar-dp.h"
 
 #include "uefi-dplib.h"
diff --git a/lib/libefivar/efivar-dp-parse.c b/lib/libefivar/efivar-dp-parse.c
index 0549aedbfa6..f604451a71a 100644
--- a/lib/libefivar/efivar-dp-parse.c
+++ b/lib/libefivar/efivar-dp-parse.c
@@ -36,9 +36,11 @@
 #include <string.h>
 #include <wchar.h>
 
+/* XXX: must come first */
+#include "efi-osdep.h"
+
 #include "libefivar_int.h"
 
-#include "efi-osdep.h"
 #include "efivar-dp.h"
 
 #include "uefi-dplib.h"
diff --git a/lib/libefivar/efivar-dp-xlate.c b/lib/libefivar/efivar-dp-xlate.c
index 686aa7e8a46..bf0280f33f2 100644
--- a/lib/libefivar/efivar-dp-xlate.c
+++ b/lib/libefivar/efivar-dp-xlate.c
@@ -30,9 +30,6 @@
 #include <sys/mount.h>
 #include <sys/sysctl.h>
 
-#undef MAX
-#undef MIN
-
 #include <assert.h>
 #include <efivar.h>
 #include <errno.h>
@@ -41,9 +38,11 @@
 #include <string.h>
 #include <uuid.h>
 
+/* XXX: must come first */
+#include "efi-osdep.h"
+
 #include "libefivar_int.h"
 
-#include "efi-osdep.h"
 #include "efivar-dp.h"
 
 #include "uefi-dplib.h"
diff --git a/lib/libefivar/uefi-dplib.h b/lib/libefivar/uefi-dplib.h
index 44440d01d77..6bc9639c785 100644
--- a/lib/libefivar/uefi-dplib.h
+++ b/lib/libefivar/uefi-dplib.h
@@ -496,7 +496,6 @@ UefiDevicePathLibConvertTextToDevicePath (
 #include <ctype.h>
 
 #define _PCD_GET_MODE_32_PcdMaximumDevicePathNodeCount 1000
-#define MAX_UINTN UINTPTR_MAX
 
 #define AllocatePool(x) malloc(x)
 #define AllocateZeroPool(x) calloc(1,x)
@@ -632,7 +631,4 @@ guid_str(const GUID *g)
 #define G(x) guid_str((const GUID *)(const void *)x)
 #endif
 
-#undef GLOBAL_REMOVE_IF_UNREFERENCED
-#define GLOBAL_REMOVE_IF_UNREFERENCED static
-
-#endif
+#endif /* !_UEFI_DPLIB_H_ */
-- 
2.45.2

