dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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