nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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