[efi] Record cached DHCPACK from loaded image's device handle, if present

Record the cached DHCPACK obtained from the EFI_PXE_BASE_CODE_PROTOCOL
instance installed on the loaded image's device handle, if present.

This allows a chainloaded UEFI iPXE to reuse the IPv4 address and DHCP
options previously obtained by the built-in PXE stack, as is already
done for a chainloaded BIOS iPXE.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
diff --git a/src/include/ipxe/efi/efi_cachedhcp.h b/src/include/ipxe/efi/efi_cachedhcp.h
new file mode 100644
index 0000000..cd60d40
--- /dev/null
+++ b/src/include/ipxe/efi/efi_cachedhcp.h
@@ -0,0 +1,16 @@
+#ifndef _IPXE_EFI_CACHEDHCP_H
+#define _IPXE_EFI_CACHEDHCP_H
+
+/** @file
+ *
+ * EFI cached DHCP packet
+ *
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
+
+#include <ipxe/efi/efi.h>
+
+extern int efi_cachedhcp_record ( EFI_HANDLE device );
+
+#endif /* _IPXE_EFI_CACHEDHCP_H */
diff --git a/src/include/ipxe/errfile.h b/src/include/ipxe/errfile.h
index a0b7618..e3fc8fa 100644
--- a/src/include/ipxe/errfile.h
+++ b/src/include/ipxe/errfile.h
@@ -386,6 +386,7 @@
 #define ERRFILE_efi_veto	      ( ERRFILE_OTHER | 0x00520000 )
 #define ERRFILE_efi_autoboot	      ( ERRFILE_OTHER | 0x00530000 )
 #define ERRFILE_efi_autoexec	      ( ERRFILE_OTHER | 0x00540000 )
+#define ERRFILE_efi_cachedhcp	      ( ERRFILE_OTHER | 0x00550000 )
 
 /** @} */
 
diff --git a/src/interface/efi/efi_cachedhcp.c b/src/interface/efi/efi_cachedhcp.c
new file mode 100644
index 0000000..14b531d
--- /dev/null
+++ b/src/interface/efi/efi_cachedhcp.c
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2021 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * You can also choose to distribute this program under the terms of
+ * the Unmodified Binary Distribution Licence (as given in the file
+ * COPYING.UBDL), provided that you have satisfied its requirements.
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
+
+#include <string.h>
+#include <errno.h>
+#include <ipxe/cachedhcp.h>
+#include <ipxe/efi/efi.h>
+#include <ipxe/efi/efi_cachedhcp.h>
+#include <ipxe/efi/Protocol/PxeBaseCode.h>
+
+/** @file
+ *
+ * EFI cached DHCP packet
+ *
+ */
+
+/**
+ * Record cached DHCP packet
+ *
+ * @v device		Device handle
+ * @ret rc		Return status code
+ */
+int efi_cachedhcp_record ( EFI_HANDLE device ) {
+		EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
+	union {
+		EFI_PXE_BASE_CODE_PROTOCOL *pxe;
+		void *interface;
+	} pxe;
+	EFI_PXE_BASE_CODE_MODE *mode;
+	EFI_STATUS efirc;
+	int rc;
+
+	/* Look for a PXE base code instance on the image's device handle */
+	if ( ( efirc = bs->OpenProtocol ( device,
+					  &efi_pxe_base_code_protocol_guid,
+					  &pxe.interface, efi_image_handle,
+					  NULL,
+					  EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
+		rc = -EEFI ( efirc );
+		DBGC ( device, "EFI %s has no PXE base code instance: %s\n",
+		       efi_handle_name ( device ), strerror ( rc ) );
+		goto err_open;
+	}
+
+	/* Do not attempt to cache IPv6 packets */
+	mode = pxe.pxe->Mode;
+	if ( mode->UsingIpv6 ) {
+		rc = -ENOTSUP;
+		DBGC ( device, "EFI %s has IPv6 PXE base code\n",
+		       efi_handle_name ( device ) );
+		goto err_ipv6;
+	}
+
+	/* Record DHCPACK, if present */
+	if ( mode->DhcpAckReceived &&
+	     ( ( rc = cachedhcp_record ( virt_to_user ( &mode->DhcpAck ),
+					 sizeof ( mode->DhcpAck ) ) ) != 0 ) ) {
+		DBGC ( device, "EFI %s could not record DHCPACK: %s\n",
+		       efi_handle_name ( device ), strerror ( rc ) );
+		goto err_record;
+	}
+
+	/* Success */
+	rc = 0;
+
+ err_record:
+ err_ipv6:
+	bs->CloseProtocol ( device, &efi_pxe_base_code_protocol_guid,
+			    efi_image_handle, NULL );
+ err_open:
+	return rc;
+}
diff --git a/src/interface/efi/efiprefix.c b/src/interface/efi/efiprefix.c
index 47fbe79..126c813 100644
--- a/src/interface/efi/efiprefix.c
+++ b/src/interface/efi/efiprefix.c
@@ -28,6 +28,7 @@
 #include <ipxe/efi/efi_snp.h>
 #include <ipxe/efi/efi_autoboot.h>
 #include <ipxe/efi/efi_autoexec.h>
+#include <ipxe/efi/efi_cachedhcp.h>
 #include <ipxe/efi/efi_watchdog.h>
 #include <ipxe/efi/efi_veto.h>
 
@@ -81,6 +82,9 @@
 	/* Identify autoboot device, if any */
 	efi_set_autoboot_ll_addr ( device );
 
+	/* Store cached DHCP packet, if any */
+	efi_cachedhcp_record ( device );
+
 	/* Load autoexec script, if any */
 	efi_autoexec_load ( device );
 }