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