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

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