From 0e65272731c95b5963a6ed1f1b5c11a10b3d577f Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Mon, 1 Apr 2019 19:42:03 +0200
Subject: [PATCH 3/3] Full EDK2.

---
 lib/libefivar/Makefile                        |   2 +
 lib/libefivar/efivar.c                        |   6 +-
 lib/libefivar/libefivar.c                     |  14 +-
 lib/libefivar/libefivar_int.h                 |   4 +-
 sbin/sysctl/Makefile                          |   4 +-
 sbin/sysctl/sysctl.c                          |  31 ++--
 share/man/man4/efidev.4                       |  23 +--
 share/man/man9/efirt.9                        |  16 +--
 sys/boot/efi/boot1/boot1.c                    |  16 +--
 sys/boot/efi/boot1/ufs_module.c               |   2 +-
 sys/boot/efi/libefi/efinet.c                  |   8 +-
 sys/boot/efi/loader/arch/x86_64/framebuffer.c |   8 +-
 sys/boot/efi/loader/bootinfo.c                |   6 +-
 sys/boot/efi/loader/copy.c                    |   4 +-
 sys/boot/efi/loader/main.c                    |   8 +-
 sys/conf/kern.pre.mk                          |   3 +
 sys/contrib/edk2/Include/Uefi/UefiBaseType.h  |   4 +-
 sys/contrib/edk2/Include/X64/ProcessorBind.h  |   4 +-
 sys/dev/misc/efirt/Makefile                   |   2 +
 sys/dev/misc/efirt/efidev.c                   |  30 ++--
 sys/platform/pc64/include/efi.h               |  28 ++--
 sys/platform/pc64/x86_64/efirt.c              | 114 ++++++++-------
 sys/platform/pc64/x86_64/machdep.c            |  54 +++----
 sys/sys/efi.h                                 | 135 +-----------------
 sys/sys/efiio.h                               |   6 +-
 usr.bin/kdump/Makefile                        |   2 +
 usr.bin/kdump/mkioctls                        |   2 +-
 usr.bin/truss/Makefile                        |   2 +
 usr.sbin/efivar/Makefile                      |   2 +
 29 files changed, 204 insertions(+), 336 deletions(-)

diff --git a/lib/libefivar/Makefile b/lib/libefivar/Makefile
index d4df2e5ba2..3a81300544 100644
--- a/lib/libefivar/Makefile
+++ b/lib/libefivar/Makefile
@@ -27,6 +27,8 @@
 PACKAGE=lib${LIB}
 LIB=		efivar
 SRCS=		efivar.c libefivar.c
+CFLAGS+=	-I${.CURDIR}/../../sys/contrib/edk2/Include
+CFLAGS+=	-I${.CURDIR}/../../sys/contrib/edk2/Include/X64
 INCS=		efivar.h
 SHLIB_MAJOR=	1
 MAN=		efivar.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/libefivar.c b/lib/libefivar/libefivar.c
index e8d167b5f7..760a4f5571 100644
--- a/lib/libefivar/libefivar.c
+++ b/lib/libefivar/libefivar.c
@@ -45,10 +45,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 +65,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 +113,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 +172,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..d3c14d9a2e 100644
--- a/sbin/sysctl/Makefile
+++ b/sbin/sysctl/Makefile
@@ -1,9 +1,11 @@
 #	@(#)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
+CFLAGS+=-I${.CURDIR}/../../sys/contrib/edk2/Include/X64
+
 NO_STRICT_ALIASING=
 
 .include <bsd.prog.mk>
diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c
index edfbe34bc0..935d7f0a52 100644
--- a/sbin/sysctl/sysctl.c
+++ b/sbin/sysctl/sysctl.c
@@ -562,7 +562,7 @@ static int
 S_efi_map(size_t l2, void *p)
 {
 	struct efi_map_header *efihdr;
-	struct efi_md *map;
+	EFI_MEMORY_DESCRIPTOR *map;
 	const char *type;
 	size_t efisz;
 	int ndesc, i;
@@ -582,6 +582,7 @@ S_efi_map(size_t l2, void *p)
 		"MemoryMappedIO",
 		"MemoryMappedIOPortSpace",
 		"PalCode"
+		/* XXX "PersistentMemory" */
 	};
 
 	/*
@@ -594,7 +595,7 @@ S_efi_map(size_t l2, void *p)
 	}
 	efihdr = p;
 	efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
-	map = (struct efi_md *)((uint8_t *)efihdr + efisz);
+	map = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)efihdr + efisz);
 
 	if (efihdr->descriptor_size == 0)
 		return (0);
@@ -610,29 +611,29 @@ S_efi_map(size_t l2, void *p)
 
 	for (i = 0; i < ndesc; i++,
 	    map = efi_next_descriptor(map, efihdr->descriptor_size)) {
-		if (map->md_type <= EFI_MD_TYPE_PALCODE)
-			type = types[map->md_type];
+		if (map->Type <= EfiPalCode)
+			type = types[map->Type];
 		else
 			type = "<INVALID>";
-		printf("\n%23s %012lx %12p %08lx ", type, map->md_phys,
-		    map->md_virt, map->md_pages);
-		if (map->md_attr & EFI_MD_ATTR_UC)
+		printf("\n%23s %012lx %12lx %08lx ", type, map->PhysicalStart,
+		    map->VirtualStart, map->NumberOfPages);
+		if (map->Attribute & EFI_MEMORY_UC)
 			printf("UC ");
-		if (map->md_attr & EFI_MD_ATTR_WC)
+		if (map->Attribute & EFI_MEMORY_WC)
 			printf("WC ");
-		if (map->md_attr & EFI_MD_ATTR_WT)
+		if (map->Attribute & EFI_MEMORY_WT)
 			printf("WT ");
-		if (map->md_attr & EFI_MD_ATTR_WB)
+		if (map->Attribute & EFI_MEMORY_WB)
 			printf("WB ");
-		if (map->md_attr & EFI_MD_ATTR_UCE)
+		if (map->Attribute & EFI_MEMORY_UCE)
 			printf("UCE ");
-		if (map->md_attr & EFI_MD_ATTR_WP)
+		if (map->Attribute & EFI_MEMORY_WP)
 			printf("WP ");
-		if (map->md_attr & EFI_MD_ATTR_RP)
+		if (map->Attribute & EFI_MEMORY_RP)
 			printf("RP ");
-		if (map->md_attr & EFI_MD_ATTR_XP)
+		if (map->Attribute & EFI_MEMORY_XP)
 			printf("XP ");
-		if (map->md_attr & EFI_MD_ATTR_RT)
+		if (map->Attribute & EFI_MEMORY_RUNTIME)
 			printf("RUNTIME");
 	}
 	return (0);
diff --git a/share/man/man4/efidev.4 b/share/man/man4/efidev.4
index 2c17b79996..b1d4c83ba3 100644
--- a/share/man/man4/efidev.4
+++ b/share/man/man4/efidev.4
@@ -78,27 +78,12 @@ struct efi_get_table_ioc {
 	void *ptr;
 };
 .Ed
-.It Dv EFIIOC_GET_TIME Pq Vt "struct efi_tm"
+.It Dv EFIIOC_GET_TIME Pq Vt "EFI_TIME"
 Get the time from the RTC, if the RTC is available.
 The
-.Vt struct efi_tm
+.Vt EFI_TIME
 passed is populated with the current time, unless an error occurs.
-.Bd -literal -offset indent
-struct efi_tm {
-	uint16_t	tm_year;
-	uint8_t		tm_mon
-	uint8_t		tm_mday
-	uint8_t		tm_hour;
-	uint8_t		tm_min;
-	uint8_t		tm_sec;
-	uint8_t		 __pad1;
-	uint32_t	tm_nsec;
-	int16_t		tm_tz;
-	uint8_t		tm_dst;
-	uint8_t		__pad2;
-};
-.Ed
-.It Dv EFIIOC_SET_TIME Pq Vt "struct efi_tm"
+.It Dv EFIIOC_SET_TIME Pq Vt "EFI_TIME"
 Sets the time stored by the RTC, if the RTC is available.
 .It Dv EFIIOC_VAR_GET Pq Vt "struct efi_var_ioc"
 Gets data from the variable described by the vendor and name fields of the
@@ -112,7 +97,7 @@ will also populate the
 field.
 .Bd -literal
 struct efi_var_ioc {
-	efi_char	*name;
+	CHAR16	*name;
 	size_t		 namesize;
 	struct uuid	 vendor;
 	uint32_t	 attrib;
diff --git a/share/man/man9/efirt.9 b/share/man/man9/efirt.9
index 76908c2f92..e948a3b153 100644
--- a/share/man/man9/efirt.9
+++ b/share/man/man9/efirt.9
@@ -51,17 +51,17 @@
 .Ft int
 .Fn efi_get_table "struct uuid *uuid" "void **ptr"
 .Ft int
-.Fn efi_get_time "struct efi_tm *tm"
+.Fn efi_get_time "EFI_TIME *tm"
 .\".Ft int
-.\".Fn efi_get_time_locked "struct efi_tm *tm"
+.\".Fn efi_get_time_locked "EFI_TIME *tm"
 .\".Ft int
 .\".Fn efi_get_time_capabilities "struct efi_tmcap *tmcap"
 .Ft int
 .Fn efi_reset_system "void"
 .Ft int
-.Fn efi_set_time "struct efi_tm *tm"
+.Fn efi_set_time "EFI_TIME *tm"
 .\".Ft int
-.\".Fn efi_set_time_locked "struct efi_tm *tm"
+.\".Fn efi_set_time_locked "EFI_TIME *tm"
 .Ft int
 .Fn efi_var_get "uint16_t *name" "struct uuid *vendor" "uint32_t *attrib" \
     "size_t *datasize" "void *data"
@@ -98,12 +98,12 @@ The
 .Fn efi_get_time
 function gets the current time from the RTC, if available.
 Returns 0 and populates the
-.Vt struct efi_tm
+.Vt EFI_TIME
 on success.
 Returns
 .Er EINVAL
 if the
-.Vt struct efi_tm
+.Vt EFI_TIME
 is
 .Dv NULL ,
 or
@@ -119,7 +119,7 @@ if the time could not be retrieved due to a hardware error.
 .\"Returns
 .\".Er EINVAL
 .\"if the
-.\".Vt struct efi_tm
+.\".Vt EFI_TIME
 .\"is
 .\".Dv NULL ,
 .\"or
@@ -133,7 +133,7 @@ function requests a warm reset and reboot of the system.
 The
 .Fn efi_set_time
 function sets the time on the RTC to the time described by the
-.Vt struct efi_tm
+.Vt EFI_TIME
 passed in.
 Returns 0 on success,
 .Er EINVAL
diff --git a/sys/boot/efi/boot1/boot1.c b/sys/boot/efi/boot1/boot1.c
index 1c2c043679..044ecf044d 100644
--- a/sys/boot/efi/boot1/boot1.c
+++ b/sys/boot/efi/boot1/boot1.c
@@ -420,14 +420,14 @@ try_boot(void)
 
 	if ((status = bs->LoadImage(true, image, devpath_last(dev->devpath),
 	    loaderbuf, loadersize, &loaderhandle)) != EFI_SUCCESS) {
-		printf("Failed to load image provided by %s, size: %zu, (%llu)\n",
+		printf("Failed to load image provided by %s, size: %zu, (%lu)\n",
 		     mod->name, loadersize, status);
 		goto errout;
 	}
 
 	if ((status = bs->HandleProtocol(loaderhandle, &LoadedImageGUID,
 	    (VOID**)&loaded_image)) != EFI_SUCCESS) {
-		printf("Failed to query LoadedImage provided by %s (%llu)\n",
+		printf("Failed to query LoadedImage provided by %s (%lu)\n",
 		    mod->name, status);
 		goto errout;
 	}
@@ -453,7 +453,7 @@ try_boot(void)
 
 	if ((status = bs->StartImage(loaderhandle, NULL, NULL)) !=
 	    EFI_SUCCESS) {
-		printf("Failed to start image provided by %s (%llu)\n",
+		printf("Failed to start image provided by %s (%lu)\n",
 		    mod->name, status);
 		loaded_image->LoadOptionsSize = 0;
 		loaded_image->LoadOptions = NULL;
@@ -640,7 +640,7 @@ efi_main(EFI_HANDLE Ximage, EFI_SYSTEM_TABLE *Xsystab)
 	hsize = (UINTN)NUM_HANDLES_INIT * sizeof(EFI_HANDLE);
 	if ((status = bs->AllocatePool(EfiLoaderData, hsize, (void **)&handles))
 	    != EFI_SUCCESS)
-		panic("Failed to allocate %d handles (%llu)", NUM_HANDLES_INIT,
+		panic("Failed to allocate %d handles (%lu)", NUM_HANDLES_INIT,
 		    status);
 
 	status = bs->LocateHandle(ByProtocol, &BlockIoProtocolGUID, NULL,
@@ -652,23 +652,23 @@ efi_main(EFI_HANDLE Ximage, EFI_SYSTEM_TABLE *Xsystab)
 		(void)bs->FreePool(handles);
 		if ((status = bs->AllocatePool(EfiLoaderData, hsize,
 		    (void **)&handles)) != EFI_SUCCESS) {
-			panic("Failed to allocate %llu handles (%llu)", hsize /
+			panic("Failed to allocate %lu handles (%lu)", hsize /
 			    sizeof(*handles), status);
 		}
 		status = bs->LocateHandle(ByProtocol, &BlockIoProtocolGUID,
 		    NULL, &hsize, handles);
 		if (status != EFI_SUCCESS)
-			panic("Failed to get device handles (%llu)\n",
+			panic("Failed to get device handles (%lu)\n",
 			    status);
 		break;
 	default:
-		panic("Failed to get device handles (%llu)",
+		panic("Failed to get device handles (%lu)",
 		    status);
 	}
 
 	/* Scan all partitions, probing with all modules. */
 	nhandles = hsize / sizeof(*handles);
-	printf("   Probing %llu block devices...", nhandles);
+	printf("   Probing %lu block devices...", nhandles);
 	DPRINTF("\n");
 
 	/* Determine the devpath of our image so we can prefer it. */
diff --git a/sys/boot/efi/boot1/ufs_module.c b/sys/boot/efi/boot1/ufs_module.c
index 97dfeaa5fd..604978f176 100644
--- a/sys/boot/efi/boot1/ufs_module.c
+++ b/sys/boot/efi/boot1/ufs_module.c
@@ -148,7 +148,7 @@ load(const char *filepath, dev_info_t *dev, void **bufp, size_t *bufsize)
 
 	if ((status = bs->AllocatePool(EfiLoaderData, size, &buf)) !=
 	    EFI_SUCCESS) {
-		printf("Failed to allocate read buffer %zu for '%s' (%llu)\n",
+		printf("Failed to allocate read buffer %zu for '%s' (%lu)\n",
 		    size, filepath, status);
 		return (status);
 	}
diff --git a/sys/boot/efi/libefi/efinet.c b/sys/boot/efi/libefi/efinet.c
index 0b6d666769..5cd48d444d 100644
--- a/sys/boot/efi/libefi/efinet.c
+++ b/sys/boot/efi/libefi/efinet.c
@@ -194,7 +194,7 @@ efinet_init(struct iodesc *desc, void *machdep_hint)
 	h = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
 	status = BS->HandleProtocol(h, &sn_guid, (VOID **)&nif->nif_devdata);
 	if (status != EFI_SUCCESS) {
-		printf("net%d: cannot start interface (status=%llu)\n",
+		printf("net%d: cannot start interface (status=%lu)\n",
 		    nif->nif_unit, status);
 		return;
 	}
@@ -203,7 +203,7 @@ efinet_init(struct iodesc *desc, void *machdep_hint)
 	if (net->Mode->State == EfiSimpleNetworkStopped) {
 		status = net->Start(net);
 		if (status != EFI_SUCCESS) {
-			printf("net%d: cannot start interface (status=%llu)\n",
+			printf("net%d: cannot start interface (status=%lu)\n",
 			    nif->nif_unit, status);
 			return;
 		}
@@ -212,7 +212,7 @@ efinet_init(struct iodesc *desc, void *machdep_hint)
 	if (net->Mode->State != EfiSimpleNetworkInitialized) {
 		status = net->Initialize(net, 0, 0);
 		if (status != EFI_SUCCESS) {
-			printf("net%d: cannot init. interface (status=%llu)\n",
+			printf("net%d: cannot init. interface (status=%lu)\n",
 			    nif->nif_unit, status);
 			return;
 		}
@@ -224,7 +224,7 @@ efinet_init(struct iodesc *desc, void *machdep_hint)
 
 		status = net->ReceiveFilters(net, mask, 0, false, 0, 0);
 		if (status != EFI_SUCCESS) {
-			printf("net%d: cannot set rx. filters (status=%llu)\n",
+			printf("net%d: cannot set rx. filters (status=%lu)\n",
 			    nif->nif_unit, status);
 			return;
 		}
diff --git a/sys/boot/efi/loader/arch/x86_64/framebuffer.c b/sys/boot/efi/loader/arch/x86_64/framebuffer.c
index 2a672f45a5..d9eb3f9ba1 100644
--- a/sys/boot/efi/loader/arch/x86_64/framebuffer.c
+++ b/sys/boot/efi/loader/arch/x86_64/framebuffer.c
@@ -178,7 +178,7 @@ efifb_uga_find_pixel(EFI_UGA_DRAW_PROTOCOL *uga, u_int line,
 	printf("No change detected in frame buffer");
 
  fail:
-	printf(" -- error %llu\n", status);
+	printf(" -- error %lu\n", status);
 	free(data1);
 	return (-1);
 }
@@ -473,7 +473,7 @@ command_gop(int argc, char *argv[])
 	status = BS->LocateProtocol(&gop_guid, NULL, (VOID **)&gop);
 	if (EFI_ERROR(status)) {
 		snprintf(command_errbuf, sizeof(command_errbuf),
-		    "%s: Graphics Output Protocol not present (error=%llu)",
+		    "%s: Graphics Output Protocol not present (error=%lu)",
 		    argv[0], status);
 		return (CMD_ERROR);
 	}
@@ -494,7 +494,7 @@ command_gop(int argc, char *argv[])
 		status = gop->SetMode(gop, mode);
 		if (EFI_ERROR(status)) {
 			snprintf(command_errbuf, sizeof(command_errbuf),
-			    "%s: Unable to set mode to %u (error=%llu)",
+			    "%s: Unable to set mode to %u (error=%lu)",
 			    argv[0], mode, status);
 			return (CMD_ERROR);
 		}
@@ -542,7 +542,7 @@ command_uga(int argc, char *argv[])
 	status = BS->LocateProtocol(&uga_guid, NULL, (VOID **)&uga);
 	if (EFI_ERROR(status)) {
 		snprintf(command_errbuf, sizeof(command_errbuf),
-		    "%s: UGA Protocol not present (error=%llu)",
+		    "%s: UGA Protocol not present (error=%lu)",
 		    argv[0], status);
 		return (CMD_ERROR);
 	}
diff --git a/sys/boot/efi/loader/bootinfo.c b/sys/boot/efi/loader/bootinfo.c
index d053e14f42..e566bb256b 100644
--- a/sys/boot/efi/loader/bootinfo.c
+++ b/sys/boot/efi/loader/bootinfo.c
@@ -295,7 +295,7 @@ bi_load_efi_data(struct preloaded_file *kfp)
 		status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
 		     pages, &addr);
 		if (EFI_ERROR(status)) {
-			printf("%s: AllocatePages error %llu\n", __func__,
+			printf("%s: AllocatePages error %lu\n", __func__,
 			    status);
 			return (ENOMEM);
 		}
@@ -311,7 +311,7 @@ bi_load_efi_data(struct preloaded_file *kfp)
 
 		status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &mmsz, &mmver);
 		if (EFI_ERROR(status)) {
-			printf("%s: GetMemoryMap error %llu\n", __func__,
+			printf("%s: GetMemoryMap error %lu\n", __func__,
 			    status);
 			return (EINVAL);
 		}
@@ -326,7 +326,7 @@ bi_load_efi_data(struct preloaded_file *kfp)
 		}
 		BS->FreePages(addr, pages);
 	}
-	printf("ExitBootServices error %llu\n", status);
+	printf("ExitBootServices error %lu\n", status);
 	return (EINVAL);
 }
 
diff --git a/sys/boot/efi/loader/copy.c b/sys/boot/efi/loader/copy.c
index 3200018bda..7266d79057 100644
--- a/sys/boot/efi/loader/copy.c
+++ b/sys/boot/efi/loader/copy.c
@@ -58,7 +58,7 @@ efi_copy_init(void)
 	status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
 	    pages, &staging);
 	if (EFI_ERROR(status)) {
-		printf("failed to allocate %uMB staging area: %llu\n",
+		printf("failed to allocate %uMB staging area: %lu\n",
 		    EFI_STAGING_SIZE, status);
 		printf("retrying with smaller %uMB allocation\n",
 		    EFI_STAGING_SIZE/2);
@@ -67,7 +67,7 @@ efi_copy_init(void)
 		    pages, &staging);
 	}
 	if (EFI_ERROR(status)) {
-		printf("failed to allocate staging area: %llu\n",
+		printf("failed to allocate staging area: %lu\n",
 		    status);
 		return (status);
 	}
diff --git a/sys/boot/efi/loader/main.c b/sys/boot/efi/loader/main.c
index c54bbd9260..d927b64461 100644
--- a/sys/boot/efi/loader/main.c
+++ b/sys/boot/efi/loader/main.c
@@ -652,14 +652,14 @@ efi_print_var(CHAR16 *varnamearg, EFI_GUID *matchguid, int lflag)
 	status = RS->GetVariable(varnamearg, matchguid, &attr,
 	    &datasz, NULL);
 	if (status != EFI_BUFFER_TOO_SMALL) {
-		printf("Can't get the variable: error %#llx\n", status);
+		printf("Can't get the variable: error %#lx\n", status);
 		return (CMD_ERROR);
 	}
 	data = malloc(datasz);
 	status = RS->GetVariable(varnamearg, matchguid, &attr,
 	    &datasz, data);
 	if (status != EFI_SUCCESS) {
-		printf("Can't get the variable: error %#llx\n", status);
+		printf("Can't get the variable: error %#lx\n", status);
 		return (CMD_ERROR);
 	}
 	uuid_to_string((uuid_t *)matchguid, &str, &uuid_status);
@@ -896,7 +896,7 @@ command_efi_set(int argc, char *argv[])
 	    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,
 	    strlen(val) + 1, val);
 	if (EFI_ERROR(err)) {
-		printf("Failed to set variable: error %llu\n", err);
+		printf("Failed to set variable: error %lu\n", err);
 		return (CMD_ERROR);
 	}
 	return (CMD_OK);
@@ -927,7 +927,7 @@ command_efi_unset(int argc, char *argv[])
 	cpy8to16(var, wvar, sizeof(wvar));
 	err = RS->SetVariable(wvar, &guid, 0, 0, NULL);
 	if (EFI_ERROR(err)) {
-		printf("Failed to unset variable: error %llu\n", err);
+		printf("Failed to unset variable: error %lu\n", err);
 		return (CMD_ERROR);
 	}
 	return (CMD_OK);
diff --git a/sys/conf/kern.pre.mk b/sys/conf/kern.pre.mk
index 1dd4c33bf7..71f4075c97 100644
--- a/sys/conf/kern.pre.mk
+++ b/sys/conf/kern.pre.mk
@@ -54,6 +54,9 @@ INCLUDES+= -I$S/../crypto/libressl/include
 .include "$S/conf/acpi.mk"
 INCLUDES+= -I${.OBJDIR} -I"$S/${OSACPI_MI_DIR}" -I"$S/${ACPICA_DIR}/include"
 
+# Similarly for EDK2 includes
+INCLUDES+= -I$S/contrib/edk2/Include -I$S/contrib/edk2/Include/X64
+
 # ... and the same for Atheros HAL
 INCLUDES+= -I$S/dev/netif/ath/ath_hal -I$S/contrib/dev/ath/ath_hal
 
diff --git a/sys/contrib/edk2/Include/Uefi/UefiBaseType.h b/sys/contrib/edk2/Include/Uefi/UefiBaseType.h
index 10dc6c4489..6335295447 100644
--- a/sys/contrib/edk2/Include/Uefi/UefiBaseType.h
+++ b/sys/contrib/edk2/Include/Uefi/UefiBaseType.h
@@ -107,8 +107,8 @@ typedef struct {
 ///
 typedef union {
   UINT32            Addr[4];
-  EFI_IPv4_ADDRESS  v4;
-  EFI_IPv6_ADDRESS  v6;
+  EFI_IPv4_ADDRESS  _v4;
+  EFI_IPv6_ADDRESS  _v6;
 } EFI_IP_ADDRESS;
 
 
diff --git a/sys/contrib/edk2/Include/X64/ProcessorBind.h b/sys/contrib/edk2/Include/X64/ProcessorBind.h
index 1b4b19e4ad..11b5993e92 100644
--- a/sys/contrib/edk2/Include/X64/ProcessorBind.h
+++ b/sys/contrib/edk2/Include/X64/ProcessorBind.h
@@ -185,11 +185,11 @@
   ///
   /// 8-byte unsigned value
   ///
-  typedef unsigned long long  UINT64;
+  typedef unsigned long       UINT64;
   ///
   /// 8-byte signed value
   ///
-  typedef long long           INT64;
+  typedef long                INT64;
   ///
   /// 4-byte unsigned value
   ///
diff --git a/sys/dev/misc/efirt/Makefile b/sys/dev/misc/efirt/Makefile
index 32de94fd9c..0f408a3ce6 100644
--- a/sys/dev/misc/efirt/Makefile
+++ b/sys/dev/misc/efirt/Makefile
@@ -5,5 +5,7 @@
 KMOD=	efirt
 SRCS=	efirt.c efidev.c
 SRCS+=  device_if.h bus_if.h
+KCFLAGS+=-I${.CURDIR}/../../../contrib/edk2/Include
+KCFLAGS+=-I${.CURDIR}/../../../contrib/edk2/Include/X64
 
 .include <bsd.kmod.mk>
diff --git a/sys/dev/misc/efirt/efidev.c b/sys/dev/misc/efirt/efidev.c
index 549094f033..918dd607e9 100644
--- a/sys/dev/misc/efirt/efidev.c
+++ b/sys/dev/misc/efirt/efidev.c
@@ -35,9 +35,10 @@
 #include <sys/malloc.h>
 #include <sys/module.h>
 #include <sys/device.h>
+#include <sys/efi.h>
+#include <sys/efiio.h>
 
 #include <machine/efi.h>
-#include <sys/efiio.h>
 
 static struct lock efidev_lock = LOCK_INITIALIZER("efidev", 0, 0);
 
@@ -79,19 +80,19 @@ efidev_ioctl(struct dev_ioctl_args *ap)
 		struct efi_get_table_ioc *egtioc =
 		    (struct efi_get_table_ioc *)addr;
 
-		error = efi_get_table(&egtioc->uuid, &egtioc->ptr);
+		error = efi_get_table((EFI_GUID *)&egtioc->uuid, &egtioc->ptr);
 		break;
 	}
 	case EFIIOC_GET_TIME:
 	{
-		struct efi_tm *tm = (struct efi_tm *)addr;
+		EFI_TIME *tm = (EFI_TIME *)addr;
 
 		error = efi_get_time(tm);
 		break;
 	}
 	case EFIIOC_SET_TIME:
 	{
-		struct efi_tm *tm = (struct efi_tm *)addr;
+		EFI_TIME *tm = (EFI_TIME *)addr;
 
 		error = efi_set_time(tm);
 		break;
@@ -100,20 +101,20 @@ efidev_ioctl(struct dev_ioctl_args *ap)
 	{
 		struct efi_var_ioc *ev = (struct efi_var_ioc *)addr;
 		void *data;
-		efi_char *name;
+		CHAR16 *name;
 
 		data = kmalloc(ev->datasize, M_TEMP, M_WAITOK);
 		name = kmalloc(ev->namesize, M_TEMP, M_WAITOK);
 		error = copyin(ev->name, name, ev->namesize);
 		if (error)
 			goto vg_out;
-		if (name[ev->namesize / sizeof(efi_char) - 1] != 0) {
+		if (name[ev->namesize / sizeof(CHAR16) - 1] != 0) {
 			error = EINVAL;
 			goto vg_out;
 		}
 
-		error = efi_var_get(name, &ev->vendor, &ev->attrib,
-		    &ev->datasize, data);
+		error = efi_var_get(name, (EFI_GUID *)&ev->vendor, &ev->attrib,
+		    (UINTN *)&ev->datasize, data);
 
 		if (error == 0) {
 			error = copyout(data, ev->data, ev->datasize);
@@ -135,7 +136,7 @@ efidev_ioctl(struct dev_ioctl_args *ap)
 	case EFIIOC_VAR_NEXT:
 	{
 		struct efi_var_ioc *ev = (struct efi_var_ioc *)addr;
-		efi_char *name;
+		CHAR16 *name;
 
 		name = kmalloc(ev->namesize, M_TEMP, M_WAITOK);
 		error = copyin(ev->name, name, ev->namesize);
@@ -143,7 +144,8 @@ efidev_ioctl(struct dev_ioctl_args *ap)
 			goto vn_out;
 		/* Note: namesize is the buffer size, not the string lenght */
 
-		error = efi_var_nextname(&ev->namesize, name, &ev->vendor);
+		error = efi_var_nextname((UINTN *)&ev->namesize, name,
+		    (EFI_GUID *)&ev->vendor);
 		if (error == 0) {
 			error = copyout(name, ev->name, ev->namesize);
 		} else if (error == EOVERFLOW) {
@@ -158,7 +160,7 @@ efidev_ioctl(struct dev_ioctl_args *ap)
 	{
 		struct efi_var_ioc *ev = (struct efi_var_ioc *)addr;
 		void *data = NULL;
-		efi_char *name;
+		CHAR16 *name;
 
 		/* datasize == 0 -> delete (more or less) */
 		if (ev->datasize > 0)
@@ -172,13 +174,13 @@ efidev_ioctl(struct dev_ioctl_args *ap)
 		error = copyin(ev->name, name, ev->namesize);
 		if (error)
 			goto vs_out;
-		if (name[ev->namesize / sizeof(efi_char) - 1] != 0) {
+		if (name[ev->namesize / sizeof(CHAR16) - 1] != 0) {
 			error = EINVAL;
 			goto vs_out;
 		}
 
-		error = efi_var_set(name, &ev->vendor, ev->attrib, ev->datasize,
-		    data);
+		error = efi_var_set(name, (EFI_GUID *)&ev->vendor, ev->attrib,
+		    ev->datasize, data);
 vs_out:
 		kfree(data, M_TEMP);
 		kfree(name, M_TEMP);
diff --git a/sys/platform/pc64/include/efi.h b/sys/platform/pc64/include/efi.h
index 3c475006aa..84f1b46fd8 100644
--- a/sys/platform/pc64/include/efi.h
+++ b/sys/platform/pc64/include/efi.h
@@ -32,27 +32,17 @@
 #ifndef __AMD64_INCLUDE_EFI_H_
 #define __AMD64_INCLUDE_EFI_H_
 
-/*
- * XXX: from gcc 6.2 manual:
- * Note, the ms_abi attribute for Microsoft Windows 64-bit targets
- * currently requires the -maccumulate-outgoing-args option.
- */
-#define	EFIABI_ATTR	__attribute__((ms_abi))
-
 #ifdef _KERNEL
-struct uuid;
-struct efi_tm;
-
-int efi_get_table(struct uuid *uuid, void **ptr);
-int efi_get_time(struct efi_tm *tm);
-int efi_get_time_locked(struct efi_tm *tm);
+int efi_get_table(EFI_GUID *uuid, void **ptr);
+int efi_get_time(EFI_TIME *tm);
+int efi_get_time_locked(EFI_TIME *tm);
 int efi_reset_system(void);
-int efi_set_time(struct efi_tm *tm);
-int efi_set_time_locked(struct efi_tm *tm);
-int efi_var_get(uint16_t *name, struct uuid *vendor, uint32_t *attrib,
-    size_t *datasize, void *data);
-int efi_var_nextname(size_t *namesize, uint16_t *name, struct uuid *vendor);
-int efi_var_set(uint16_t *name, struct uuid *vendor, uint32_t attrib,
+int efi_set_time(EFI_TIME *tm);
+int efi_set_time_locked(EFI_TIME *tm);
+int efi_var_get(CHAR16 *name, EFI_GUID *vendor, uint32_t *attrib,
+    UINTN *datasize, void *data);
+int efi_var_nextname(UINTN *namesize, CHAR16 *name, EFI_GUID *vendor);
+int efi_var_set(CHAR16 *name, EFI_GUID *vendor, uint32_t attrib,
     size_t datasize, void *data);
 #endif
 
diff --git a/sys/platform/pc64/x86_64/efirt.c b/sys/platform/pc64/x86_64/efirt.c
index ac53e0b178..2d8941da3a 100644
--- a/sys/platform/pc64/x86_64/efirt.c
+++ b/sys/platform/pc64/x86_64/efirt.c
@@ -61,9 +61,9 @@
 #include <machine/smp.h>
 #include <machine/vmparam.h>
 
-static struct efi_systbl *efi_systbl;
-static struct efi_cfgtbl *efi_cfgtbl;
-static struct efi_rt *efi_runtime;
+static EFI_SYSTEM_TABLE *efi_systbl;
+static EFI_CONFIGURATION_TABLE *efi_cfgtbl;
+static EFI_RUNTIME_SERVICES *efi_runtime;
 
 static int efi_status2err[25] = {
 	0,		/* EFI_SUCCESS */
@@ -96,7 +96,7 @@ static int efi_status2err[25] = {
 MALLOC_DEFINE(M_EFI, "efi", "EFI BIOS");
 
 static int
-efi_status_to_errno(efi_status status)
+efi_status_to_errno(EFI_STATUS status)
 {
 	u_long code;
 
@@ -110,7 +110,7 @@ static mcontext_t efi_ctx;
 static struct vmspace *efi_savevm;
 static struct vmspace *efi_vmspace;
 static vm_object_t efi_obj;
-static struct efi_md *efi_map;
+static EFI_MEMORY_DESCRIPTOR *efi_map;
 static int efi_ndesc;
 static int efi_descsz;
 
@@ -193,10 +193,10 @@ static struct cdev_pager_ops efi_pager_ops = {
 };
 
 static bool
-efi_create_1t1_map(struct efi_md *map, int ndesc, int descsz)
+efi_create_1t1_map(EFI_MEMORY_DESCRIPTOR *map, int ndesc, int descsz)
 {
 	vm_page_t m;
-	struct efi_md *p;
+	EFI_MEMORY_DESCRIPTOR *p;
 	int i;
 	int count;
 	int result;
@@ -232,38 +232,38 @@ efi_create_1t1_map(struct efi_md *map, int ndesc, int descsz)
 		uint64_t idx;
 		int mode;
 
-		if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
+		if ((p->Attribute & EFI_MEMORY_RUNTIME) == 0)
 			continue;
-		if (p->md_virt != NULL) {
+		if (p->VirtualStart != 0) {
 			if (bootverbose)
 				kprintf("EFI Runtime entry %d is mapped\n", i);
 			goto fail;
 		}
-		if ((p->md_phys & EFI_PAGE_MASK) != 0) {
+		if ((p->PhysicalStart & EFI_PAGE_MASK) != 0) {
 			if (bootverbose)
 				kprintf("EFI Runtime entry %d is not aligned\n",
 				    i);
 			goto fail;
 		}
-		if (p->md_phys + p->md_pages * EFI_PAGE_SIZE < p->md_phys ||
-		    p->md_phys + p->md_pages * EFI_PAGE_SIZE >=
+		if (p->PhysicalStart + p->NumberOfPages * EFI_PAGE_SIZE < p->PhysicalStart ||
+		    p->PhysicalStart + p->NumberOfPages * EFI_PAGE_SIZE >=
 		    VM_MAX_USER_ADDRESS) {
 			kprintf("EFI Runtime entry %d is not in mappable for RT:"
 			    "base %#016jx %#jx pages\n",
-			    i, (uintmax_t)p->md_phys,
-			    (uintmax_t)p->md_pages);
+			    i, (uintmax_t)p->PhysicalStart,
+			    (uintmax_t)p->NumberOfPages);
 			goto fail;
 		}
 
-		if ((p->md_attr & EFI_MD_ATTR_WB) != 0)
+		if ((p->Attribute & EFI_MEMORY_WB) != 0)
 			mode = VM_MEMATTR_WRITE_BACK;
-		else if ((p->md_attr & EFI_MD_ATTR_WT) != 0)
+		else if ((p->Attribute & EFI_MEMORY_WT) != 0)
 			mode = VM_MEMATTR_WRITE_THROUGH;
-		else if ((p->md_attr & EFI_MD_ATTR_WC) != 0)
+		else if ((p->Attribute & EFI_MEMORY_WC) != 0)
 			mode = VM_MEMATTR_WRITE_COMBINING;
-		else if ((p->md_attr & EFI_MD_ATTR_WP) != 0)
+		else if ((p->Attribute & EFI_MEMORY_WP) != 0)
 			mode = VM_MEMATTR_WRITE_PROTECTED;
-		else if ((p->md_attr & EFI_MD_ATTR_UC) != 0)
+		else if ((p->Attribute & EFI_MEMORY_UC) != 0)
 			mode = VM_MEMATTR_UNCACHEABLE;
 		else {
 			if (bootverbose)
@@ -273,12 +273,12 @@ efi_create_1t1_map(struct efi_md *map, int ndesc, int descsz)
 		}
 
 		if (bootverbose) {
-			kprintf("efirt: map %016jx-%016jx\n",
-				p->md_phys,
-				p->md_phys + IDX_TO_OFF(p->md_pages));
+			kprintf("efirt: map %016lx-%016lx\n",
+				p->PhysicalStart,
+				p->PhysicalStart + IDX_TO_OFF(p->NumberOfPages));
 		}
 
-		for (va = p->md_phys, idx = 0; idx < p->md_pages; idx++,
+		for (va = p->PhysicalStart, idx = 0; idx < p->NumberOfPages; idx++,
 		    va += PAGE_SIZE) {
 			m = kmalloc(sizeof(*m), M_EFI, M_WAITOK | M_ZERO);
 			/*m->flags |= PG_WRITEABLE;*/
@@ -361,7 +361,7 @@ static int
 efi_init(void)
 {
 	struct efi_map_header *efihdr;
-	struct efi_md *map;
+	EFI_MEMORY_DESCRIPTOR *map;
 	caddr_t kmdp;
 	size_t efisz;
 
@@ -373,15 +373,14 @@ efi_init(void)
 			kprintf("EFI systbl not available\n");
 		return (ENXIO);
 	}
-	efi_systbl = (struct efi_systbl *)PHYS_TO_DMAP(efi_systbl_phys);
-	if (efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
+	efi_systbl = (EFI_SYSTEM_TABLE *)PHYS_TO_DMAP(efi_systbl_phys);
+	if (efi_systbl->Hdr.Signature != EFI_SYSTEM_TABLE_SIGNATURE) {
 		efi_systbl = NULL;
 		if (bootverbose)
 			kprintf("EFI systbl signature invalid\n");
 		return (ENXIO);
 	}
-	efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
-	    (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
+	efi_cfgtbl = efi_systbl->ConfigurationTable;
 	if (efi_cfgtbl == NULL) {
 		if (bootverbose)
 			kprintf("EFI config table is not present\n");
@@ -398,7 +397,7 @@ efi_init(void)
 		return (ENXIO);
 	}
 	efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
-	map = (struct efi_md *)((uint8_t *)efihdr + efisz);
+	map = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)efihdr + efisz);
 	if (efihdr->descriptor_size == 0)
 		return (ENOMEM);
 
@@ -409,8 +408,7 @@ efi_init(void)
 		return (ENOMEM);
 	}
 
-	efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
-			(struct efi_rt *)efi_systbl->st_rt;
+	efi_runtime = efi_systbl->RuntimeServices;
 	if (efi_runtime == NULL) {
 		if (bootverbose)
 			kprintf("EFI runtime services table is not present\n");
@@ -435,18 +433,18 @@ efi_uninit(void)
 }
 
 int
-efi_get_table(struct uuid *uuid, void **ptr)
+efi_get_table(EFI_GUID *uuid, void **ptr)
 {
-	struct efi_cfgtbl *ct;
+	EFI_CONFIGURATION_TABLE *ct;
 	u_long count;
 
 	if (efi_cfgtbl == NULL)
 		return (ENXIO);
-	count = efi_systbl->st_entries;
+	count = efi_systbl->NumberOfTableEntries;
 	ct = efi_cfgtbl;
 	while (count--) {
-		if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
-			*ptr = (void *)PHYS_TO_DMAP(ct->ct_data);
+		if (!bcmp(&ct->VendorGuid, uuid, sizeof(*uuid))) {
+			*ptr = (void *)PHYS_TO_DMAP(ct->VendorTable);
 			return (0);
 		}
 		ct++;
@@ -457,16 +455,16 @@ efi_get_table(struct uuid *uuid, void **ptr)
 char SaveCode[1024];
 
 int
-efi_get_time_locked(struct efi_tm *tm)
+efi_get_time_locked(EFI_TIME *tm)
 {
-	efi_status status;
+	EFI_STATUS status;
 	int error;
 
 	KKASSERT(lockowned(&resettodr_lock) != 0);
 	error = efi_enter();
 	if (error != 0)
 		return (error);
-	status = efi_runtime->rt_gettime(tm, NULL);
+	status = efi_runtime->GetTime(tm, NULL);
 	efi_leave();
 	error = efi_status_to_errno(status);
 
@@ -474,7 +472,7 @@ efi_get_time_locked(struct efi_tm *tm)
 }
 
 int
-efi_get_time(struct efi_tm *tm)
+efi_get_time(EFI_TIME *tm)
 {
 	int error;
 
@@ -495,29 +493,29 @@ efi_reset_system(void)
 	error = efi_enter();
 	if (error != 0)
 		return (error);
-	efi_runtime->rt_reset(EFI_RESET_WARM, 0, 0, NULL);
+	efi_runtime->ResetSystem(EfiResetWarm, 0, 0, NULL);
 	efi_leave();
 	return (EIO);
 }
 
 int
-efi_set_time_locked(struct efi_tm *tm)
+efi_set_time_locked(EFI_TIME *tm)
 {
-	efi_status status;
+	EFI_STATUS status;
 	int error;
 
 	KKASSERT(lockowned(&resettodr_lock) != 0);
 	error = efi_enter();
 	if (error != 0)
 		return (error);
-	status = efi_runtime->rt_settime(tm);
+	status = efi_runtime->SetTime(tm);
 	efi_leave();
 	error = efi_status_to_errno(status);
 	return (error);
 }
 
 int
-efi_set_time(struct efi_tm *tm)
+efi_set_time(EFI_TIME *tm)
 {
 	int error;
 
@@ -530,47 +528,47 @@ efi_set_time(struct efi_tm *tm)
 }
 
 int
-efi_var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
-    size_t *datasize, void *data)
+efi_var_get(CHAR16 *name, EFI_GUID *vendor, uint32_t *attrib,
+    UINTN *datasize, void *data)
 {
-	efi_status status;
+	EFI_STATUS status;
 	int error;
 
 	error = efi_enter();
 	if (error != 0)
 		return (error);
-	status = efi_runtime->rt_getvar(name, vendor, attrib, datasize, data);
+	status = efi_runtime->GetVariable(name, vendor, attrib, datasize, data);
 	efi_leave();
 	error = efi_status_to_errno(status);
 	return (error);
 }
 
 int
-efi_var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
+efi_var_nextname(UINTN *namesize, CHAR16 *name, EFI_GUID *vendor)
 {
-	efi_status status;
+	EFI_STATUS status;
 	int error;
 
 	error = efi_enter();
 	if (error != 0)
 		return (error);
-	status = efi_runtime->rt_scanvar(namesize, name, vendor);
+	status = efi_runtime->GetNextVariableName(namesize, name, vendor);
 	efi_leave();
 	error = efi_status_to_errno(status);
 	return (error);
 }
 
 int
-efi_var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
+efi_var_set(CHAR16 *name, EFI_GUID *vendor, uint32_t attrib,
     size_t datasize, void *data)
 {
-	efi_status status;
+	EFI_STATUS status;
 	int error;
 
 	error = efi_enter();
 	if (error != 0)
 		return (error);
-	status = efi_runtime->rt_setvar(name, vendor, attrib, datasize, data);
+	status = efi_runtime->SetVariable(name, vendor, attrib, datasize, data);
 	efi_leave();
 	error = efi_status_to_errno(status);
 	return (error);
@@ -610,7 +608,7 @@ MODULE_VERSION(efirt, 1);
 static int
 efi_time_sysctl_handler(SYSCTL_HANDLER_ARGS)
 {
-	struct efi_tm tm;
+	EFI_TIME tm;
 	int error, val;
 
 	val = 0;
@@ -620,8 +618,8 @@ efi_time_sysctl_handler(SYSCTL_HANDLER_ARGS)
 	error = efi_get_time(&tm);
 	if (error == 0) {
 		uprintf("EFI reports: Year %d Month %d Day %d Hour %d Min %d "
-		    "Sec %d\n", tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour,
-		    tm.tm_min, tm.tm_sec);
+		    "Sec %d\n", tm.Year, tm.Month, tm.Day, tm.Hour,
+		    tm.Minute, tm.Second);
 	}
 	return (error);
 }
diff --git a/sys/platform/pc64/x86_64/machdep.c b/sys/platform/pc64/x86_64/machdep.c
index 7fea86a970..4f2757cd35 100644
--- a/sys/platform/pc64/x86_64/machdep.c
+++ b/sys/platform/pc64/x86_64/machdep.c
@@ -1875,7 +1875,7 @@ add_smap_entries(int *physmap_idx)
 static void
 add_efi_map_entries(int *physmap_idx)
 {
-	struct efi_md *map, *p;
+	EFI_MEMORY_DESCRIPTOR *map, *p;
 	const char *type;
 	size_t efisz;
 	int i, ndesc;
@@ -1895,6 +1895,7 @@ add_efi_map_entries(int *physmap_idx)
 		"MemoryMappedIO",
 		"MemoryMappedIOPortSpace",
 		"PalCode"
+		/* XXX "PersistentMemory" */
 	 };
 
 	/*
@@ -1902,7 +1903,7 @@ add_efi_map_entries(int *physmap_idx)
 	 * Boot Services API.
 	 */
 	efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
-	map = (struct efi_md *)((uint8_t *)efihdrbase + efisz);
+	map = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)efihdrbase + efisz);
 
 	if (efihdrbase->descriptor_size == 0)
 		return;
@@ -1915,39 +1916,40 @@ add_efi_map_entries(int *physmap_idx)
 	for (i = 0, p = map; i < ndesc; i++,
 	    p = efi_next_descriptor(p, efihdrbase->descriptor_size)) {
 		if (boothowto & RB_VERBOSE) {
-			if (p->md_type <= EFI_MD_TYPE_PALCODE)
-				type = types[p->md_type];
+			if (p->Type <= EfiPalCode)
+				type = types[p->Type];
 			else
 				type = "<INVALID>";
-			kprintf("%23s %012lx %12p %08lx ", type, p->md_phys,
-			    p->md_virt, p->md_pages);
-			if (p->md_attr & EFI_MD_ATTR_UC)
+			kprintf("%23s %012lx %12lx %08lx ", type,
+			    p->PhysicalStart, p->VirtualStart,
+			    p->NumberOfPages);
+			if (p->Attribute & EFI_MEMORY_UC)
 				kprintf("UC ");
-			if (p->md_attr & EFI_MD_ATTR_WC)
+			if (p->Attribute & EFI_MEMORY_WC)
 				kprintf("WC ");
-			if (p->md_attr & EFI_MD_ATTR_WT)
+			if (p->Attribute & EFI_MEMORY_WT)
 				kprintf("WT ");
-			if (p->md_attr & EFI_MD_ATTR_WB)
+			if (p->Attribute & EFI_MEMORY_WB)
 				kprintf("WB ");
-			if (p->md_attr & EFI_MD_ATTR_UCE)
+			if (p->Attribute & EFI_MEMORY_UCE)
 				kprintf("UCE ");
-			if (p->md_attr & EFI_MD_ATTR_WP)
+			if (p->Attribute & EFI_MEMORY_WP)
 				kprintf("WP ");
-			if (p->md_attr & EFI_MD_ATTR_RP)
+			if (p->Attribute & EFI_MEMORY_RP)
 				kprintf("RP ");
-			if (p->md_attr & EFI_MD_ATTR_XP)
+			if (p->Attribute & EFI_MEMORY_XP)
 				kprintf("XP ");
-			if (p->md_attr & EFI_MD_ATTR_RT)
+			if (p->Attribute & EFI_MEMORY_RUNTIME)
 				kprintf("RUNTIME");
 			kprintf("\n");
 		}
 
-		switch (p->md_type) {
-		case EFI_MD_TYPE_CODE:
-		case EFI_MD_TYPE_DATA:
-		case EFI_MD_TYPE_BS_CODE:
-		case EFI_MD_TYPE_BS_DATA:
-		case EFI_MD_TYPE_FREE:
+		switch (p->Type) {
+		case EfiLoaderCode:
+		case EfiLoaderData:
+		case EfiBootServicesCode:
+		case EfiBootServicesData:
+		case EfiConventionalMemory:
 			/*
 			 * We're allowed to use any entry with these types.
 			 */
@@ -1956,10 +1958,10 @@ add_efi_map_entries(int *physmap_idx)
 			continue;
 		}
 
-		Realmem += p->md_pages * PAGE_SIZE;
+		Realmem += p->NumberOfPages * PAGE_SIZE;
 
-		if (p->md_phys == physmap[*physmap_idx + 1]) {
-			physmap[*physmap_idx + 1] += p->md_pages * PAGE_SIZE;
+		if (p->PhysicalStart == physmap[*physmap_idx + 1]) {
+			physmap[*physmap_idx + 1] += p->NumberOfPages * PAGE_SIZE;
 			continue;
 		}
 
@@ -1969,8 +1971,8 @@ add_efi_map_entries(int *physmap_idx)
 				"address map, giving up\n");
 			break;
 		}
-		physmap[*physmap_idx] = p->md_phys;
-		physmap[*physmap_idx + 1] = p->md_phys + p->md_pages * PAGE_SIZE;
+		physmap[*physmap_idx] = p->PhysicalStart;
+		physmap[*physmap_idx + 1] = p->PhysicalStart + p->NumberOfPages * PAGE_SIZE;
 	 }
 }
 
diff --git a/sys/sys/efi.h b/sys/sys/efi.h
index 946b191fcb..451d3dc810 100644
--- a/sys/sys/efi.h
+++ b/sys/sys/efi.h
@@ -29,138 +29,13 @@
 #ifndef _SYS_EFI_H_
 #define _SYS_EFI_H_
 
-#include <sys/uuid.h>
-
-#define	EFI_PAGE_SHIFT		12
-#define	EFI_PAGE_SIZE		(1 << EFI_PAGE_SHIFT)
-#define	EFI_PAGE_MASK		(EFI_PAGE_SIZE - 1)
-
-#define	EFI_TABLE_ACPI20			\
-	{0x8868e871,0xe4f1,0x11d3,0xbc,0x22,{0x00,0x80,0xc7,0x3c,0x88,0x81}}
-#define	EFI_TABLE_SAL				\
-	{0xeb9d2d32,0x2d88,0x11d3,0x9a,0x16,{0x00,0x90,0x27,0x3f,0xc1,0x4d}}
-
-enum efi_reset {
-	EFI_RESET_COLD,
-	EFI_RESET_WARM
-};
-
-typedef uint16_t	efi_char;
-typedef unsigned long efi_status;
-
-struct efi_cfgtbl {
-	struct uuid	ct_uuid;
-	uint64_t	ct_data;
-};
-
-struct efi_md {
-	uint32_t	md_type;
-#define	EFI_MD_TYPE_NULL	0
-#define	EFI_MD_TYPE_CODE	1	/* Loader text. */
-#define	EFI_MD_TYPE_DATA	2	/* Loader data. */
-#define	EFI_MD_TYPE_BS_CODE	3	/* Boot services text. */
-#define	EFI_MD_TYPE_BS_DATA	4	/* Boot services data. */
-#define	EFI_MD_TYPE_RT_CODE	5	/* Runtime services text. */
-#define	EFI_MD_TYPE_RT_DATA	6	/* Runtime services data. */
-#define	EFI_MD_TYPE_FREE	7	/* Unused/free memory. */
-#define	EFI_MD_TYPE_BAD		8	/* Bad memory */
-#define	EFI_MD_TYPE_RECLAIM	9	/* ACPI reclaimable memory. */
-#define	EFI_MD_TYPE_FIRMWARE	10	/* ACPI NV memory */
-#define	EFI_MD_TYPE_IOMEM	11	/* Memory-mapped I/O. */
-#define	EFI_MD_TYPE_IOPORT	12	/* I/O port space. */
-#define	EFI_MD_TYPE_PALCODE	13	/* PAL */
-	uint32_t	__pad;
-	uint64_t	md_phys;
-	void		*md_virt;
-	uint64_t	md_pages;
-	uint64_t	md_attr;
-#define	EFI_MD_ATTR_UC		0x0000000000000001UL
-#define	EFI_MD_ATTR_WC		0x0000000000000002UL
-#define	EFI_MD_ATTR_WT		0x0000000000000004UL
-#define	EFI_MD_ATTR_WB		0x0000000000000008UL
-#define	EFI_MD_ATTR_UCE		0x0000000000000010UL
-#define	EFI_MD_ATTR_WP		0x0000000000001000UL
-#define	EFI_MD_ATTR_RP		0x0000000000002000UL
-#define	EFI_MD_ATTR_XP		0x0000000000004000UL
-#define	EFI_MD_ATTR_RT		0x8000000000000000UL
-};
+#include <Uefi.h>
+#if defined(__GNUC__) && defined(__pic__) && !defined(USING_LTO)
+#pragma GCC visibility pop
+#endif
 
 #define efi_next_descriptor(ptr, size) \
-    ((struct efi_md *)(((uint8_t *)(ptr)) + (size)))
-
-struct efi_tm {
-	uint16_t	tm_year;		/* 1998 - 20XX */
-	uint8_t		tm_mon;			/* 1 - 12 */
-	uint8_t		tm_mday;		/* 1 - 31 */
-	uint8_t		tm_hour;		/* 0 - 23 */
-	uint8_t		tm_min;			/* 0 - 59 */
-	uint8_t		tm_sec;			/* 0 - 59 */
-	uint8_t		__pad1;
-	uint32_t	tm_nsec;		/* 0 - 999,999,999 */
-	int16_t		tm_tz;			/* -1440 to 1440 or 2047 */
-	uint8_t		tm_dst;
-	uint8_t		__pad2;
-};
-
-struct efi_tmcap {
-	uint32_t	tc_res;		/* 1e-6 parts per million */
-	uint32_t	tc_prec;	/* hertz */
-	uint8_t		tc_stz;		/* Set clears sub-second time */
-};
-
-struct efi_tblhdr {
-	uint64_t	th_sig;
-	uint32_t	th_rev;
-	uint32_t	th_hdrsz;
-	uint32_t	th_crc32;
-	uint32_t	__res;
-};
-
-#define EFIABI	__attribute__((ms_abi))
-
-struct efi_rt {
-	struct efi_tblhdr rt_hdr;
-	efi_status	(*rt_gettime)(struct efi_tm *, struct efi_tmcap *)
-				EFIABI;
-	efi_status	(*rt_settime)(struct efi_tm *)
-				EFIABI;
-	efi_status	(*rt_getwaketime)(uint8_t *, uint8_t *,
-				struct efi_tm *) EFIABI;
-	efi_status	(*rt_setwaketime)(uint8_t, struct efi_tm *)
-				EFIABI;
-	efi_status	(*rt_setvirtual)(u_long, u_long, uint32_t,
-				struct efi_md *) EFIABI;
-	efi_status	(*rt_cvtptr)(u_long, void **)
-				EFIABI;
-	efi_status	(*rt_getvar)(efi_char *, struct uuid *, uint32_t *,
-				u_long *, void *) EFIABI;
-	efi_status	(*rt_scanvar)(u_long *, efi_char *, struct uuid *)
-				EFIABI;
-	efi_status	(*rt_setvar)(efi_char *, struct uuid *, uint32_t,
-				u_long, void *) EFIABI;
-	efi_status	(*rt_gethicnt)(uint32_t *)
-				EFIABI;
-	efi_status	(*rt_reset)(enum efi_reset, efi_status, u_long,
-				efi_char *) EFIABI;
-};
-
-struct efi_systbl {
-	struct efi_tblhdr st_hdr;
-#define	EFI_SYSTBL_SIG	0x5453595320494249UL
-	efi_char	*st_fwvendor;
-	uint32_t	st_fwrev;
-	uint32_t	__pad;
-	void		*st_cin;
-	void		*st_cinif;
-	void		*st_cout;
-	void		*st_coutif;
-	void		*st_cerr;
-	void		*st_cerrif;
-	uint64_t	st_rt;
-	void		*st_bs;
-	u_long		st_entries;
-	uint64_t	st_cfgtbl;
-};
+    ((EFI_MEMORY_DESCRIPTOR *)(((uint8_t *)(ptr)) + (size)))
 
 #ifdef _KERNEL
 extern vm_paddr_t efi_systbl_phys;
diff --git a/sys/sys/efiio.h b/sys/sys/efiio.h
index e4db866d97..668bb8cbe2 100644
--- a/sys/sys/efiio.h
+++ b/sys/sys/efiio.h
@@ -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 */
@@ -50,8 +50,8 @@ struct efi_var_ioc
 };
 
 #define EFIIOC_GET_TABLE	_IOWR('E',  1, struct efi_get_table_ioc)
-#define EFIIOC_GET_TIME		_IOR('E',   2, struct efi_tm)
-#define EFIIOC_SET_TIME		_IOW('E',   3, struct efi_tm)
+#define EFIIOC_GET_TIME		_IOR('E',   2, EFI_TIME)
+#define EFIIOC_SET_TIME		_IOW('E',   3, EFI_TIME)
 #define EFIIOC_VAR_GET		_IOWR('E',  4, struct efi_var_ioc)
 #define EFIIOC_VAR_NEXT		_IOWR('E',  5, struct efi_var_ioc)
 #define EFIIOC_VAR_SET		_IOWR('E',  6, struct efi_var_ioc)
diff --git a/usr.bin/kdump/Makefile b/usr.bin/kdump/Makefile
index 89ed4a7cb0..1a5293538c 100644
--- a/usr.bin/kdump/Makefile
+++ b/usr.bin/kdump/Makefile
@@ -8,6 +8,8 @@ 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
+CFLAGS+=	-I${.CURDIR}/../../sys/contrib/edk2/Include/X64
 
 CLEANFILES=	ioctl.c kdump_subr.c
 
diff --git a/usr.bin/kdump/mkioctls b/usr.bin/kdump/mkioctls
index 9c6bc84eba..637dcac281 100644
--- a/usr.bin/kdump/mkioctls
+++ b/usr.bin/kdump/mkioctls
@@ -34,7 +34,7 @@ ioctl_includes=`
 `
 
 awk -v x="$ioctl_includes" 'BEGIN {print x}' |
-	gcc -D_KERNEL_STRUCTURES -E -I$1 -I$2/../../sys -I$2/../../sys/dev/drm/include -dM - |
+	gcc -D_KERNEL_STRUCTURES -E -I$1 -I$2/../../sys -I$2/../../sys/dev/drm/include -I$2/../../sys/contrib/edk2/Include -I$2/../../sys/contrib/edk2/Include/X64 -dM - |
 	awk -v ioctl_includes="$ioctl_includes" -v use_switch="$use_switch" '
 BEGIN {
 	print "/* XXX obnoxious prerequisites. */"
diff --git a/usr.bin/truss/Makefile b/usr.bin/truss/Makefile
index 531af2fc79..9aabb6ac69 100644
--- a/usr.bin/truss/Makefile
+++ b/usr.bin/truss/Makefile
@@ -6,6 +6,8 @@ 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
+CFLAGS+= -I${.CURDIR}/../../sys/contrib/edk2/Include/X64
 
 CLEANFILES+=syscalls.master syscalls.h ioctl.c
 
diff --git a/usr.sbin/efivar/Makefile b/usr.sbin/efivar/Makefile
index 1c6fe20524..116480e2ae 100644
--- a/usr.sbin/efivar/Makefile
+++ b/usr.sbin/efivar/Makefile
@@ -1,6 +1,8 @@
 # $FreeBSD: head/usr.sbin/efivar/Makefile 307072 2016-10-11 22:31:45Z imp $
 
 PROG=	efivar
+CFLAGS+=-I${.CURDIR}/../../sys/contrib/edk2/Include
+CFLAGS+=-I${.CURDIR}/../../sys/contrib/edk2/Include/X64
 MAN=	efivar.8
 
 LDADD=	-lefivar
-- 
2.21.0

