nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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