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

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