dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0174-efinet-Setting-network-from-UEFI-device-path.patch

8631a2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8631a2
From: Michael Chang <mchang@suse.com>
8631a2
Date: Sun, 10 Jul 2016 23:46:31 +0800
8631a2
Subject: [PATCH] efinet: Setting network from UEFI device path
8631a2
8631a2
The PXE Base Code protocol used to obtain cached PXE DHCPACK packet is no
8631a2
longer provided for HTTP Boot. Instead, we have to get the HTTP boot
8631a2
information from the device path nodes defined in following UEFI Specification
8631a2
sections.
8631a2
8631a2
 9.3.5.12 IPv4 Device Path
8631a2
 9.3.5.13 IPv6 Device Path
8631a2
 9.3.5.23 Uniform Resource Identifiers (URI) Device Path
8631a2
8631a2
This patch basically does:
8631a2
8631a2
include/grub/efi/api.h:
8631a2
Add new structure of Uniform Resource Identifiers (URI) Device Path
8631a2
8631a2
grub-core/net/drivers/efi/efinet.c:
8631a2
Check if PXE Base Code is available, if not it will try to obtain the netboot
8631a2
information from the device path where the image booted from. The DHCPACK
8631a2
packet is recoverd from the information in device patch and feed into the same
8631a2
DHCP packet processing functions to ensure the network interface is setting up
8631a2
the same way it used to be.
8631a2
8631a2
Signed-off-by: Michael Chang <mchang@suse.com>
8631a2
Signed-off-by: Ken Lin <ken.lin@hpe.com>
8631a2
---
8631a2
 grub-core/net/drivers/efi/efinet.c | 284 +++++++++++++++++++++++++++++++++++--
8631a2
 include/grub/efi/api.h             |  11 ++
8631a2
 2 files changed, 280 insertions(+), 15 deletions(-)
8631a2
8631a2
diff --git a/grub-core/net/drivers/efi/efinet.c b/grub-core/net/drivers/efi/efinet.c
8631a2
index 712b4f85962..3e51e106473 100644
8631a2
--- a/grub-core/net/drivers/efi/efinet.c
8631a2
+++ b/grub-core/net/drivers/efi/efinet.c
8631a2
@@ -27,6 +27,7 @@
8631a2
 #include <grub/i18n.h>
8631a2
 #include <grub/lib/hexdump.h>
8631a2
 #include <grub/types.h>
8631a2
+#include <grub/net/netbuff.h>
8631a2
 
8631a2
 GRUB_MOD_LICENSE ("GPLv3+");
8631a2
 
8631a2
@@ -332,6 +333,227 @@ grub_efinet_findcards (void)
8631a2
   grub_free (handles);
8631a2
 }
8631a2
 
8631a2
+static struct grub_net_buff *
8631a2
+grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *use_ipv6)
8631a2
+{
8631a2
+  grub_efi_uint16_t uri_len;
8631a2
+  grub_efi_device_path_t *ldp, *ddp;
8631a2
+  grub_efi_uri_device_path_t *uri_dp;
8631a2
+  struct grub_net_buff *nb;
8631a2
+  grub_err_t err;
8631a2
+
8631a2
+  ddp = grub_efi_duplicate_device_path (dp);
8631a2
+  if (!ddp)
8631a2
+    return NULL;
8631a2
+
8631a2
+  ldp = grub_efi_find_last_device_path (ddp);
8631a2
+
8631a2
+  if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) != GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
8631a2
+      || GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_URI_DEVICE_PATH_SUBTYPE)
8631a2
+    {
8631a2
+      grub_free (ddp);
8631a2
+      return NULL;
8631a2
+    }
8631a2
+
8631a2
+  uri_len = GRUB_EFI_DEVICE_PATH_LENGTH (ldp) > 4 ? GRUB_EFI_DEVICE_PATH_LENGTH (ldp) - 4  : 0;
8631a2
+
8631a2
+  if (!uri_len)
8631a2
+    {
8631a2
+      grub_free (ddp);
8631a2
+      return NULL;
8631a2
+    }
8631a2
+
8631a2
+  uri_dp = (grub_efi_uri_device_path_t *) ldp;
8631a2
+
8631a2
+  ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
8631a2
+  ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
8631a2
+  ldp->length = sizeof (*ldp);
8631a2
+
8631a2
+  ldp = grub_efi_find_last_device_path (ddp);
8631a2
+
8631a2
+  if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) != GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
8631a2
+      || (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE
8631a2
+          && GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE))
8631a2
+    {
8631a2
+      grub_free (ddp);
8631a2
+      return NULL;
8631a2
+    }
8631a2
+
8631a2
+  nb = grub_netbuff_alloc (512);
8631a2
+  if (!nb)
8631a2
+    {
8631a2
+      grub_free (ddp);
8631a2
+      return NULL;
8631a2
+    }
8631a2
+
8631a2
+  if (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) == GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE)
8631a2
+    {
8631a2
+      grub_efi_ipv4_device_path_t *ipv4 = (grub_efi_ipv4_device_path_t *) ldp;
8631a2
+      struct grub_net_bootp_packet *bp;
8631a2
+      grub_uint8_t *ptr;
8631a2
+
8631a2
+      bp = (struct grub_net_bootp_packet *) nb->tail;
8631a2
+      err = grub_netbuff_put (nb, sizeof (*bp) + 4);
8631a2
+      if (err)
8631a2
+	{
8631a2
+	  grub_free (ddp);
8631a2
+	  grub_netbuff_free (nb);
8631a2
+	  return NULL;
8631a2
+	}
8631a2
+
8631a2
+      if (sizeof(bp->boot_file) < uri_len)
8631a2
+	{
8631a2
+	  grub_free (ddp);
8631a2
+	  grub_netbuff_free (nb);
8631a2
+	  return NULL;
8631a2
+	}
8631a2
+      grub_memcpy (bp->boot_file, uri_dp->uri, uri_len);
8631a2
+      grub_memcpy (&bp->your_ip, ipv4->local_ip_address, sizeof (bp->your_ip));
8631a2
+      grub_memcpy (&bp->server_ip, ipv4->remote_ip_address, sizeof (bp->server_ip));
8631a2
+
8631a2
+      bp->vendor[0] = GRUB_NET_BOOTP_RFC1048_MAGIC_0;
8631a2
+      bp->vendor[1] = GRUB_NET_BOOTP_RFC1048_MAGIC_1;
8631a2
+      bp->vendor[2] = GRUB_NET_BOOTP_RFC1048_MAGIC_2;
8631a2
+      bp->vendor[3] = GRUB_NET_BOOTP_RFC1048_MAGIC_3;
8631a2
+
8631a2
+      ptr = nb->tail;
8631a2
+      err = grub_netbuff_put (nb, sizeof (ipv4->subnet_mask) + 2);
8631a2
+      if (err)
8631a2
+	{
8631a2
+	  grub_free (ddp);
8631a2
+	  grub_netbuff_free (nb);
8631a2
+	  return NULL;
8631a2
+	}
8631a2
+      *ptr++ = GRUB_NET_BOOTP_NETMASK;
8631a2
+      *ptr++ = sizeof (ipv4->subnet_mask);
8631a2
+      grub_memcpy (ptr, ipv4->subnet_mask, sizeof (ipv4->subnet_mask));
8631a2
+
8631a2
+      ptr = nb->tail;
8631a2
+      err = grub_netbuff_put (nb, sizeof (ipv4->gateway_ip_address) + 2);
8631a2
+      if (err)
8631a2
+	{
8631a2
+	  grub_free (ddp);
8631a2
+	  grub_netbuff_free (nb);
8631a2
+	  return NULL;
8631a2
+	}
8631a2
+      *ptr++ = GRUB_NET_BOOTP_ROUTER;
8631a2
+      *ptr++ = sizeof (ipv4->gateway_ip_address);
8631a2
+      grub_memcpy (ptr, ipv4->gateway_ip_address, sizeof (ipv4->gateway_ip_address));
8631a2
+
8631a2
+      ptr = nb->tail;
8631a2
+      err = grub_netbuff_put (nb, sizeof ("HTTPClient") + 1);
8631a2
+      if (err)
8631a2
+	{
8631a2
+	  grub_free (ddp);
8631a2
+	  grub_netbuff_free (nb);
8631a2
+	  return NULL;
8631a2
+	}
8631a2
+      *ptr++ = GRUB_NET_BOOTP_VENDOR_CLASS_IDENTIFIER;
8631a2
+      *ptr++ = sizeof ("HTTPClient") - 1;
8631a2
+      grub_memcpy (ptr, "HTTPClient", sizeof ("HTTPClient") - 1);
8631a2
+
8631a2
+      ptr = nb->tail;
8631a2
+      err = grub_netbuff_put (nb, 1);
8631a2
+      if (err)
8631a2
+	{
8631a2
+	  grub_free (ddp);
8631a2
+	  grub_netbuff_free (nb);
8631a2
+	  return NULL;
8631a2
+	}
8631a2
+      *ptr = GRUB_NET_BOOTP_END;
8631a2
+      *use_ipv6 = 0;
8631a2
+
8631a2
+      ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
8631a2
+      ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
8631a2
+      ldp->length = sizeof (*ldp);
8631a2
+      ldp = grub_efi_find_last_device_path (ddp);
8631a2
+
8631a2
+      if (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) ==  GRUB_EFI_MAC_ADDRESS_DEVICE_PATH_SUBTYPE)
8631a2
+	{
8631a2
+	  grub_efi_mac_address_device_path_t *mac = (grub_efi_mac_address_device_path_t *) ldp;
8631a2
+	  bp->hw_type = mac->if_type;
8631a2
+	  bp->hw_len = sizeof (bp->mac_addr);
8631a2
+	  grub_memcpy (bp->mac_addr, mac->mac_address, bp->hw_len);
8631a2
+	}
8631a2
+    }
8631a2
+  else
8631a2
+    {
8631a2
+      grub_efi_ipv6_device_path_t *ipv6 = (grub_efi_ipv6_device_path_t *) ldp;
8631a2
+
8631a2
+      struct grub_net_dhcp6_packet *d6p;
8631a2
+      struct grub_net_dhcp6_option *opt;
8631a2
+      struct grub_net_dhcp6_option_iana *iana;
8631a2
+      struct grub_net_dhcp6_option_iaaddr *iaaddr;
8631a2
+
8631a2
+      d6p = (struct grub_net_dhcp6_packet *)nb->tail;
8631a2
+      err = grub_netbuff_put (nb, sizeof(*d6p));
8631a2
+      if (err)
8631a2
+	{
8631a2
+	  grub_free (ddp);
8631a2
+	  grub_netbuff_free (nb);
8631a2
+	  return NULL;
8631a2
+	}
8631a2
+      d6p->message_type = GRUB_NET_DHCP6_REPLY;
8631a2
+
8631a2
+      opt = (struct grub_net_dhcp6_option *)nb->tail;
8631a2
+      err = grub_netbuff_put (nb, sizeof(*opt));
8631a2
+      if (err)
8631a2
+	{
8631a2
+	  grub_free (ddp);
8631a2
+	  grub_netbuff_free (nb);
8631a2
+	  return NULL;
8631a2
+	}
8631a2
+      opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IA_NA);
8631a2
+      opt->len = grub_cpu_to_be16_compile_time (sizeof(*iana) + sizeof(*opt) + sizeof(*iaaddr));
8631a2
+
8631a2
+      err = grub_netbuff_put (nb, sizeof(*iana));
8631a2
+      if (err)
8631a2
+	{
8631a2
+	  grub_free (ddp);
8631a2
+	  grub_netbuff_free (nb);
8631a2
+	  return NULL;
8631a2
+	}
8631a2
+
8631a2
+      opt = (struct grub_net_dhcp6_option *)nb->tail;
8631a2
+      err = grub_netbuff_put (nb, sizeof(*opt));
8631a2
+      if (err)
8631a2
+	{
8631a2
+	  grub_free (ddp);
8631a2
+	  grub_netbuff_free (nb);
8631a2
+	  return NULL;
8631a2
+	}
8631a2
+      opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IAADDR);
8631a2
+      opt->len = grub_cpu_to_be16_compile_time (sizeof (*iaaddr));
8631a2
+
8631a2
+      iaaddr = (struct grub_net_dhcp6_option_iaaddr *)nb->tail;
8631a2
+      err = grub_netbuff_put (nb, sizeof(*iaaddr));
8631a2
+      if (err)
8631a2
+	{
8631a2
+	  grub_free (ddp);
8631a2
+	  grub_netbuff_free (nb);
8631a2
+	  return NULL;
8631a2
+	}
8631a2
+      grub_memcpy (iaaddr->addr, ipv6->local_ip_address, sizeof(ipv6->local_ip_address));
8631a2
+
8631a2
+      opt = (struct grub_net_dhcp6_option *)nb->tail;
8631a2
+      err = grub_netbuff_put (nb, sizeof(*opt) + uri_len);
8631a2
+      if (err)
8631a2
+	{
8631a2
+	  grub_free (ddp);
8631a2
+	  grub_netbuff_free (nb);
8631a2
+	  return NULL;
8631a2
+	}
8631a2
+      opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_BOOTFILE_URL);
8631a2
+      opt->len = grub_cpu_to_be16 (uri_len);
8631a2
+      grub_memcpy (opt->data, uri_dp->uri, uri_len);
8631a2
+
8631a2
+      *use_ipv6 = 1;
8631a2
+    }
8631a2
+
8631a2
+  grub_free (ddp);
8631a2
+  return nb;
8631a2
+}
8631a2
+
8631a2
 static void
8631a2
 grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
8631a2
 			  char **path)
8631a2
@@ -347,7 +569,11 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
8631a2
   {
8631a2
     grub_efi_device_path_t *cdp;
8631a2
     struct grub_efi_pxe *pxe;
8631a2
-    struct grub_efi_pxe_mode *pxe_mode;
8631a2
+    struct grub_efi_pxe_mode *pxe_mode = NULL;
8631a2
+    grub_uint8_t *packet_buf;
8631a2
+    grub_size_t packet_bufsz ;
8631a2
+    int ipv6;
8631a2
+    struct grub_net_buff *nb = NULL;
8631a2
 
8631a2
     if (card->driver != &efidriver)
8631a2
       continue;
8631a2
@@ -370,11 +596,21 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
8631a2
          */
8631a2
 	if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) != GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
8631a2
 	    || (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE
8631a2
-		&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE))
8631a2
+		&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE
8631a2
+		&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_URI_DEVICE_PATH_SUBTYPE))
8631a2
 	  continue;
8631a2
 	dup_dp = grub_efi_duplicate_device_path (dp);
8631a2
 	if (!dup_dp)
8631a2
 	  continue;
8631a2
+
8631a2
+	if (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) == GRUB_EFI_URI_DEVICE_PATH_SUBTYPE)
8631a2
+	  {
8631a2
+	    dup_ldp = grub_efi_find_last_device_path (dup_dp);
8631a2
+	    dup_ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
8631a2
+	    dup_ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
8631a2
+	    dup_ldp->length = sizeof (*dup_ldp);
8631a2
+	  }
8631a2
+
8631a2
 	dup_ldp = grub_efi_find_last_device_path (dup_dp);
8631a2
 	dup_ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
8631a2
 	dup_ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
8631a2
@@ -387,23 +623,37 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
8631a2
 
8631a2
     pxe = grub_efi_open_protocol (hnd, &pxe_io_guid,
8631a2
 				  GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
8631a2
-    if (! pxe)
8631a2
-      continue;
8631a2
+    if (!pxe)
8631a2
+      {
8631a2
+	nb = grub_efinet_create_dhcp_ack_from_device_path (dp, &ipv6);
8631a2
+	if (!nb)
8631a2
+	  {
8631a2
+	    grub_print_error ();
8631a2
+	    continue;
8631a2
+	  }
8631a2
+	packet_buf = nb->head;
8631a2
+	packet_bufsz = nb->tail - nb->head;
8631a2
+      }
8631a2
+    else
8631a2
+      {
8631a2
+	pxe_mode = pxe->mode;
8631a2
+	packet_buf = (grub_uint8_t *) &pxe_mode->dhcp_ack;
8631a2
+	packet_bufsz = sizeof (pxe_mode->dhcp_ack);
8631a2
+	ipv6 = pxe_mode->using_ipv6;
8631a2
+      }
8631a2
 
8631a2
-    pxe_mode = pxe->mode;
8631a2
-    if (pxe_mode->using_ipv6)
8631a2
+    if (ipv6)
8631a2
       {
8631a2
 	grub_dprintf ("efinet", "using ipv6 and dhcpv6\n");
8631a2
-	grub_dprintf ("efinet", "dhcp_ack_received: %s%s\n",
8631a2
-		      pxe_mode->dhcp_ack_received ? "yes" : "no",
8631a2
-		      pxe_mode->dhcp_ack_received ? "" : " cannot continue");
8631a2
-	if (!pxe_mode->dhcp_ack_received)
8631a2
-	  continue;
8631a2
+	if (pxe_mode)
8631a2
+	  grub_dprintf ("efinet", "dhcp_ack_received: %s%s\n",
8631a2
+			pxe_mode->dhcp_ack_received ? "yes" : "no",
8631a2
+			pxe_mode->dhcp_ack_received ? "" : " cannot continue");
8631a2
 
8631a2
 	grub_net_configure_by_dhcpv6_reply (card->name, card, 0,
8631a2
 					    (struct grub_net_dhcp6_packet *)
8631a2
-					    &pxe_mode->dhcp_ack,
8631a2
-					    sizeof (pxe_mode->dhcp_ack),
8631a2
+					    packet_buf,
8631a2
+					    packet_bufsz,
8631a2
 					    1, device, path);
8631a2
 	if (grub_errno)
8631a2
 	  grub_print_error ();
8631a2
@@ -417,11 +667,15 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
8631a2
 	grub_dprintf ("efinet", "using ipv4 and dhcp\n");
8631a2
 	grub_net_configure_by_dhcp_ack (card->name, card, 0,
8631a2
 					(struct grub_net_bootp_packet *)
8631a2
-					&pxe_mode->dhcp_ack,
8631a2
-					sizeof (pxe_mode->dhcp_ack),
8631a2
+					packet_buf,
8631a2
+					packet_bufsz,
8631a2
 					1, device, path);
8631a2
 	grub_dprintf ("efinet", "device: `%s' path: `%s'\n", *device, *path);
8631a2
       }
8631a2
+
8631a2
+    if (nb)
8631a2
+      grub_netbuff_free (nb);
8631a2
+
8631a2
     return;
8631a2
   }
8631a2
 }
8631a2
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
8631a2
index 2f164d4209c..eb6bb50857c 100644
8631a2
--- a/include/grub/efi/api.h
8631a2
+++ b/include/grub/efi/api.h
8631a2
@@ -839,6 +839,8 @@ struct grub_efi_ipv4_device_path
8631a2
   grub_efi_uint16_t remote_port;
8631a2
   grub_efi_uint16_t protocol;
8631a2
   grub_efi_uint8_t static_ip_address;
8631a2
+  grub_efi_ipv4_address_t gateway_ip_address;
8631a2
+  grub_efi_ipv4_address_t subnet_mask;
8631a2
 } GRUB_PACKED;
8631a2
 typedef struct grub_efi_ipv4_device_path grub_efi_ipv4_device_path_t;
8631a2
 
8631a2
@@ -893,6 +895,15 @@ struct grub_efi_sata_device_path
8631a2
 } GRUB_PACKED;
8631a2
 typedef struct grub_efi_sata_device_path grub_efi_sata_device_path_t;
8631a2
 
8631a2
+#define GRUB_EFI_URI_DEVICE_PATH_SUBTYPE		24
8631a2
+
8631a2
+struct grub_efi_uri_device_path
8631a2
+{
8631a2
+  grub_efi_device_path_t header;
8631a2
+  grub_efi_uint8_t uri[0];
8631a2
+} GRUB_PACKED;
8631a2
+typedef struct grub_efi_uri_device_path grub_efi_uri_device_path_t;
8631a2
+
8631a2
 #define GRUB_EFI_VENDOR_MESSAGING_DEVICE_PATH_SUBTYPE	10
8631a2
 
8631a2
 /* Media Device Path.  */