nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone
28f7f8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
27a4da
From: Michael Chang <mchang@suse.com>
27a4da
Date: Thu, 14 Jul 2016 17:48:45 +0800
28f7f8
Subject: [PATCH] efinet: Setting DNS server from UEFI protocol
27a4da
27a4da
In the URI device path node, any name rahter than address can be used for
27a4da
looking up the resources so that DNS service become needed to get answer of the
27a4da
name's address. Unfortunately the DNS is not defined in any of the device path
27a4da
nodes so that we use the EFI_IP4_CONFIG2_PROTOCOL and EFI_IP6_CONFIG_PROTOCOL
27a4da
to obtain it.
27a4da
27a4da
These two protcols are defined the sections of UEFI specification.
27a4da
27a4da
 27.5 EFI IPv4 Configuration II Protocol
27a4da
 27.7 EFI IPv6 Configuration Protocol
27a4da
27a4da
include/grub/efi/api.h:
27a4da
Add new structure and protocol UUID of EFI_IP4_CONFIG2_PROTOCOL and
27a4da
EFI_IP6_CONFIG_PROTOCOL.
27a4da
27a4da
grub-core/net/drivers/efi/efinet.c:
27a4da
Use the EFI_IP4_CONFIG2_PROTOCOL and EFI_IP6_CONFIG_PROTOCOL to obtain the list
27a4da
of DNS server address for IPv4 and IPv6 respectively. The address of DNS
27a4da
servers is structured into DHCPACK packet and feed into the same DHCP packet
27a4da
processing functions to ensure the network interface is setting up the same way
27a4da
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
---
28f7f8
 grub-core/net/drivers/efi/efinet.c | 163 +++++++++++++++++++++++++++++++++++++
27a4da
 include/grub/efi/api.h             |  76 +++++++++++++++++
28f7f8
 2 files changed, 239 insertions(+)
27a4da
27a4da
diff --git a/grub-core/net/drivers/efi/efinet.c b/grub-core/net/drivers/efi/efinet.c
28f7f8
index 6f75ff1f9d1..25616a9c6c8 100644
27a4da
--- a/grub-core/net/drivers/efi/efinet.c
27a4da
+++ b/grub-core/net/drivers/efi/efinet.c
27a4da
@@ -34,6 +34,8 @@ GRUB_MOD_LICENSE ("GPLv3+");
27a4da
 /* GUID.  */
27a4da
 static grub_efi_guid_t net_io_guid = GRUB_EFI_SIMPLE_NETWORK_GUID;
27a4da
 static grub_efi_guid_t pxe_io_guid = GRUB_EFI_PXE_GUID;
27a4da
+static grub_efi_guid_t ip4_config_guid = GRUB_EFI_IP4_CONFIG2_PROTOCOL_GUID;
27a4da
+static grub_efi_guid_t ip6_config_guid = GRUB_EFI_IP6_CONFIG_PROTOCOL_GUID;
27a4da
 
27a4da
 static grub_err_t
27a4da
 send_card_buffer (struct grub_net_card *dev,
27a4da
@@ -343,6 +345,125 @@ grub_efinet_findcards (void)
27a4da
   grub_free (handles);
27a4da
 }
27a4da
 
27a4da
+static grub_efi_handle_t
27a4da
+grub_efi_locate_device_path (grub_efi_guid_t *protocol, grub_efi_device_path_t *device_path,
27a4da
+			    grub_efi_device_path_t **r_device_path)
27a4da
+{
27a4da
+  grub_efi_handle_t handle;
27a4da
+  grub_efi_status_t status;
27a4da
+
27a4da
+  status = efi_call_3 (grub_efi_system_table->boot_services->locate_device_path,
27a4da
+		      protocol, &device_path, &handle);
27a4da
+
27a4da
+  if (status != GRUB_EFI_SUCCESS)
27a4da
+    return 0;
27a4da
+
27a4da
+  if (r_device_path)
27a4da
+    *r_device_path = device_path;
27a4da
+
27a4da
+  return handle;
27a4da
+}
27a4da
+
27a4da
+static grub_efi_ipv4_address_t *
27a4da
+grub_dns_server_ip4_address (grub_efi_device_path_t *dp, grub_efi_uintn_t *num_dns)
27a4da
+{
27a4da
+  grub_efi_handle_t hnd;
27a4da
+  grub_efi_status_t status;
27a4da
+  grub_efi_ip4_config2_protocol_t *conf;
27a4da
+  grub_efi_ipv4_address_t *addrs;
27a4da
+  grub_efi_uintn_t data_size = 1 * sizeof (grub_efi_ipv4_address_t);
27a4da
+
27a4da
+  hnd = grub_efi_locate_device_path (&ip4_config_guid, dp, NULL);
27a4da
+
27a4da
+  if (!hnd)
27a4da
+    return 0;
27a4da
+
27a4da
+  conf = grub_efi_open_protocol (hnd, &ip4_config_guid,
27a4da
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
27a4da
+
27a4da
+  if (!conf)
27a4da
+    return 0;
27a4da
+
27a4da
+  addrs  = grub_malloc (data_size);
27a4da
+  if (!addrs)
27a4da
+    return 0;
27a4da
+
27a4da
+  status = efi_call_4 (conf->get_data, conf,
27a4da
+		      GRUB_EFI_IP4_CONFIG2_DATA_TYPE_DNSSERVER,
27a4da
+		      &data_size, addrs);
27a4da
+
27a4da
+  if (status == GRUB_EFI_BUFFER_TOO_SMALL)
27a4da
+    {
27a4da
+      grub_free (addrs);
27a4da
+      addrs  = grub_malloc (data_size);
27a4da
+      if (!addrs)
27a4da
+	return 0;
27a4da
+
27a4da
+      status = efi_call_4 (conf->get_data,  conf,
27a4da
+			  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_DNSSERVER,
27a4da
+			  &data_size, addrs);
27a4da
+    }
27a4da
+
27a4da
+  if (status != GRUB_EFI_SUCCESS)
27a4da
+    {
27a4da
+      grub_free (addrs);
27a4da
+      return 0;
27a4da
+    }
27a4da
+
27a4da
+  *num_dns = data_size / sizeof (grub_efi_ipv4_address_t);
27a4da
+  return addrs;
27a4da
+}
27a4da
+
27a4da
+static grub_efi_ipv6_address_t *
27a4da
+grub_dns_server_ip6_address (grub_efi_device_path_t *dp, grub_efi_uintn_t *num_dns)
27a4da
+{
27a4da
+  grub_efi_handle_t hnd;
27a4da
+  grub_efi_status_t status;
27a4da
+  grub_efi_ip6_config_protocol_t *conf;
27a4da
+  grub_efi_ipv6_address_t *addrs;
27a4da
+  grub_efi_uintn_t data_size = 1 * sizeof (grub_efi_ipv6_address_t);
27a4da
+
27a4da
+  hnd = grub_efi_locate_device_path (&ip6_config_guid, dp, NULL);
27a4da
+
27a4da
+  if (!hnd)
27a4da
+    return 0;
27a4da
+
27a4da
+  conf = grub_efi_open_protocol (hnd, &ip6_config_guid,
27a4da
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
27a4da
+
27a4da
+  if (!conf)
27a4da
+    return 0;
27a4da
+
27a4da
+  addrs  = grub_malloc (data_size);
27a4da
+  if (!addrs)
27a4da
+    return 0;
27a4da
+
27a4da
+  status = efi_call_4 (conf->get_data, conf,
27a4da
+		      GRUB_EFI_IP6_CONFIG_DATA_TYPE_DNSSERVER,
27a4da
+		      &data_size, addrs);
27a4da
+
27a4da
+  if (status == GRUB_EFI_BUFFER_TOO_SMALL)
27a4da
+    {
27a4da
+      grub_free (addrs);
27a4da
+      addrs  = grub_malloc (data_size);
27a4da
+      if (!addrs)
27a4da
+	return 0;
27a4da
+
27a4da
+      status = efi_call_4 (conf->get_data,  conf,
27a4da
+			  GRUB_EFI_IP6_CONFIG_DATA_TYPE_DNSSERVER,
27a4da
+			  &data_size, addrs);
27a4da
+    }
27a4da
+
27a4da
+  if (status != GRUB_EFI_SUCCESS)
27a4da
+    {
27a4da
+      grub_free (addrs);
27a4da
+      return 0;
27a4da
+    }
27a4da
+
27a4da
+  *num_dns = data_size / sizeof (grub_efi_ipv6_address_t);
27a4da
+  return addrs;
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
 {
28f7f8
@@ -401,6 +522,8 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u
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
+      grub_efi_ipv4_address_t *dns;
27a4da
+      grub_efi_uintn_t num_dns;
27a4da
 
27a4da
       bp = (struct grub_net_bootp_packet *) nb->tail;
27a4da
       err = grub_netbuff_put (nb, sizeof (*bp) + 4);
28f7f8
@@ -462,6 +585,25 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u
27a4da
       *ptr++ = sizeof ("HTTPClient") - 1;
27a4da
       grub_memcpy (ptr, "HTTPClient", sizeof ("HTTPClient") - 1);
27a4da
 
27a4da
+      dns = grub_dns_server_ip4_address (dp, &num_dns);
27a4da
+      if (dns)
27a4da
+	{
27a4da
+	  grub_efi_uintn_t size_dns = sizeof (*dns) * num_dns;
27a4da
+
27a4da
+	  ptr = nb->tail;
27a4da
+	  err = grub_netbuff_put (nb, size_dns + 2);
27a4da
+	  if (err)
27a4da
+	    {
27a4da
+	      grub_free (ddp);
27a4da
+	      grub_netbuff_free (nb);
27a4da
+	      return NULL;
27a4da
+	    }
27a4da
+	  *ptr++ = GRUB_NET_BOOTP_DNS;
27a4da
+	  *ptr++ = size_dns;
27a4da
+	  grub_memcpy (ptr, dns, size_dns);
27a4da
+	  grub_free (dns);
27a4da
+	}
27a4da
+
27a4da
       ptr = nb->tail;
27a4da
       err = grub_netbuff_put (nb, 1);
27a4da
       if (err)
28f7f8
@@ -494,6 +636,8 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u
27a4da
       struct grub_net_dhcp6_option *opt;
27a4da
       struct grub_net_dhcp6_option_iana *iana;
27a4da
       struct grub_net_dhcp6_option_iaaddr *iaaddr;
27a4da
+      grub_efi_ipv6_address_t *dns;
27a4da
+      grub_efi_uintn_t num_dns;
27a4da
 
27a4da
       d6p = (struct grub_net_dhcp6_packet *)nb->tail;
27a4da
       err = grub_netbuff_put (nb, sizeof(*d6p));
28f7f8
@@ -557,6 +701,25 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u
27a4da
       opt->len = grub_cpu_to_be16 (uri_len);
27a4da
       grub_memcpy (opt->data, uri_dp->uri, uri_len);
27a4da
 
27a4da
+      dns = grub_dns_server_ip6_address (dp, &num_dns);
27a4da
+      if (dns)
27a4da
+	{
27a4da
+	  grub_efi_uintn_t size_dns = sizeof (*dns) * num_dns;
27a4da
+
27a4da
+	  opt = (struct grub_net_dhcp6_option *)nb->tail;
27a4da
+	  err = grub_netbuff_put (nb, sizeof(*opt) + size_dns);
27a4da
+	  if (err)
27a4da
+	  {
27a4da
+	    grub_free (ddp);
27a4da
+	    grub_netbuff_free (nb);
27a4da
+	    return NULL;
27a4da
+	  }
27a4da
+	  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_DNS_SERVERS);
27a4da
+	  opt->len = grub_cpu_to_be16 (size_dns);
27a4da
+	  grub_memcpy (opt->data, dns, size_dns);
27a4da
+	  grub_free (dns);
27a4da
+	}
27a4da
+
27a4da
       *use_ipv6 = 1;
27a4da
     }
27a4da
 
27a4da
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
28f7f8
index 76ad56aa2b0..852daddfb0f 100644
27a4da
--- a/include/grub/efi/api.h
27a4da
+++ b/include/grub/efi/api.h
27a4da
@@ -289,6 +289,16 @@
27a4da
       { 0x8B, 0x8C, 0xE2, 0x1B, 0x01, 0xAE, 0xF2, 0xB7 } \
27a4da
   }
27a4da
 
27a4da
+#define GRUB_EFI_IP4_CONFIG2_PROTOCOL_GUID \
27a4da
+  { 0x5b446ed1, 0xe30b, 0x4faa, \
27a4da
+      { 0x87, 0x1a, 0x36, 0x54, 0xec, 0xa3, 0x60, 0x80 } \
27a4da
+  }
27a4da
+
27a4da
+#define GRUB_EFI_IP6_CONFIG_PROTOCOL_GUID \
27a4da
+  { 0x937fe521, 0x95ae, 0x4d1a, \
27a4da
+      { 0x89, 0x29, 0x48, 0xbc, 0xd9, 0x0a, 0xd3, 0x1a } \
27a4da
+  }
27a4da
+
27a4da
 struct grub_efi_sal_system_table
27a4da
 {
27a4da
   grub_uint32_t signature;
28f7f8
@@ -1785,6 +1795,72 @@ struct grub_efi_block_io
27a4da
 };
27a4da
 typedef struct grub_efi_block_io grub_efi_block_io_t;
27a4da
 
27a4da
+enum grub_efi_ip4_config2_data_type {
27a4da
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_INTERFACEINFO,
27a4da
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_POLICY,
27a4da
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_MANUAL_ADDRESS,
27a4da
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_GATEWAY,
27a4da
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_DNSSERVER,
27a4da
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_MAXIMUM
27a4da
+};
27a4da
+typedef enum grub_efi_ip4_config2_data_type grub_efi_ip4_config2_data_type_t;
27a4da
+
27a4da
+struct grub_efi_ip4_config2_protocol
27a4da
+{
27a4da
+  grub_efi_status_t (*set_data) (struct grub_efi_ip4_config2_protocol *this,
27a4da
+				 grub_efi_ip4_config2_data_type_t data_type,
27a4da
+				 grub_efi_uintn_t data_size,
27a4da
+				 void *data);
27a4da
+
27a4da
+  grub_efi_status_t (*get_data) (struct grub_efi_ip4_config2_protocol *this,
27a4da
+				 grub_efi_ip4_config2_data_type_t data_type,
27a4da
+				 grub_efi_uintn_t *data_size,
27a4da
+				 void *data);
27a4da
+
27a4da
+  grub_efi_status_t (*register_data_notify) (struct grub_efi_ip4_config2_protocol *this,
27a4da
+					     grub_efi_ip4_config2_data_type_t data_type,
27a4da
+					     grub_efi_event_t event);
27a4da
+
27a4da
+  grub_efi_status_t (*unregister_datanotify) (struct grub_efi_ip4_config2_protocol *this,
27a4da
+					     grub_efi_ip4_config2_data_type_t data_type,
27a4da
+					     grub_efi_event_t event);
27a4da
+};
27a4da
+typedef struct grub_efi_ip4_config2_protocol grub_efi_ip4_config2_protocol_t;
27a4da
+
27a4da
+enum grub_efi_ip6_config_data_type {
27a4da
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_INTERFACEINFO,
27a4da
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_ALT_INTERFACEID,
27a4da
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_POLICY,
27a4da
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_DUP_ADDR_DETECT_TRANSMITS,
27a4da
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_MANUAL_ADDRESS,
27a4da
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_GATEWAY,
27a4da
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_DNSSERVER,
27a4da
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_MAXIMUM
27a4da
+};
27a4da
+typedef enum grub_efi_ip6_config_data_type grub_efi_ip6_config_data_type_t;
27a4da
+
27a4da
+struct grub_efi_ip6_config_protocol
27a4da
+{
27a4da
+  grub_efi_status_t (*set_data) (struct grub_efi_ip6_config_protocol *this,
27a4da
+				 grub_efi_ip6_config_data_type_t data_type,
27a4da
+				 grub_efi_uintn_t data_size,
27a4da
+				 void *data);
27a4da
+
27a4da
+  grub_efi_status_t (*get_data) (struct grub_efi_ip6_config_protocol *this,
27a4da
+				 grub_efi_ip6_config_data_type_t data_type,
27a4da
+				 grub_efi_uintn_t *data_size,
27a4da
+				 void *data);
27a4da
+
27a4da
+  grub_efi_status_t (*register_data_notify) (struct grub_efi_ip6_config_protocol *this,
27a4da
+					     grub_efi_ip6_config_data_type_t data_type,
27a4da
+					     grub_efi_event_t event);
27a4da
+
27a4da
+  grub_efi_status_t (*unregister_datanotify) (struct grub_efi_ip6_config_protocol *this,
27a4da
+					     grub_efi_ip6_config_data_type_t data_type,
27a4da
+					     grub_efi_event_t event);
27a4da
+};
27a4da
+typedef struct grub_efi_ip6_config_protocol grub_efi_ip6_config_protocol_t;
27a4da
+
27a4da
 #if (GRUB_TARGET_SIZEOF_VOID_P == 4) || defined (__ia64__) \
27a4da
   || defined (__aarch64__) || defined (__MINGW64__) || defined (__CYGWIN__)
27a4da