]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[efi] Record cached DHCPACK from loaded image's device handle, if present 265/head
authorMichael Brown <mcb30@ipxe.org>
Wed, 17 Feb 2021 18:11:43 +0000 (18:11 +0000)
committerMichael Brown <mcb30@ipxe.org>
Wed, 17 Feb 2021 18:11:43 +0000 (18:11 +0000)
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>
src/include/ipxe/efi/efi_cachedhcp.h [new file with mode: 0644]
src/include/ipxe/errfile.h
src/interface/efi/efi_cachedhcp.c [new file with mode: 0644]
src/interface/efi/efiprefix.c

diff --git a/src/include/ipxe/efi/efi_cachedhcp.h b/src/include/ipxe/efi/efi_cachedhcp.h
new file mode 100644 (file)
index 0000000..cd60d40
--- /dev/null
@@ -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 */
index a0b7618cc1fc711767b0c984ae64220ac2c51158..e3fc8fa0921540219ca45726d3dcecdb409e4f10 100644 (file)
@@ -386,6 +386,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 #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 (file)
index 0000000..14b531d
--- /dev/null
@@ -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;
+}
index 47fbe79aabff779e6973bfadd922211896acc067..126c813d718b990fc45e2ba6a2b1e9aa1d93547a 100644 (file)
@@ -28,6 +28,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 #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 @@ static void efi_init_application ( void ) {
        /* 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 );
 }