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

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