Blame SOURCES/0265-Support-UEFI-networking-protocols.patch

f731ee
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f731ee
From: Michael Chang <mchang@suse.com>
f731ee
Date: Wed, 22 Feb 2017 14:27:50 +0800
f731ee
Subject: [PATCH] Support UEFI networking protocols
f731ee
f731ee
References: fate#320130, bsc#1015589, bsc#1076132
f731ee
Patch-Mainline: no
f731ee
f731ee
V1:
f731ee
  * Add preliminary support of UEFI networking protocols
f731ee
  * Support UEFI HTTPS Boot
f731ee
f731ee
V2:
f731ee
  * Workaround http data access in firmware
f731ee
  * Fix DNS device path parsing for efinet device
f731ee
  * Relaxed UEFI Protocol requirement
f731ee
  * Support Intel OPA (Omni-Path Architecture) PXE Boot
f731ee
f731ee
V3:
f731ee
  * Fix bufio in calculating address of next_buf
f731ee
  * Check HTTP respond code
f731ee
  * Use HEAD request method to test before GET
f731ee
  * Finish HTTP transaction in one go
f731ee
  * Fix bsc#1076132
f731ee
---
f731ee
 grub-core/Makefile.core.def        |   18 +
f731ee
 grub-core/io/bufio.c               |    2 +-
f731ee
 grub-core/kern/efi/efi.c           |   96 ++-
f731ee
 grub-core/net/drivers/efi/efinet.c |   27 +
f731ee
 grub-core/net/efi/dhcp.c           |  397 ++++++++++
f731ee
 grub-core/net/efi/efi_netfs.c      |   57 ++
f731ee
 grub-core/net/efi/http.c           |  419 +++++++++++
f731ee
 grub-core/net/efi/ip4_config.c     |  398 ++++++++++
f731ee
 grub-core/net/efi/ip6_config.c     |  422 +++++++++++
f731ee
 grub-core/net/efi/net.c            | 1428 ++++++++++++++++++++++++++++++++++++
f731ee
 grub-core/net/efi/pxe.c            |  424 +++++++++++
f731ee
 grub-core/net/net.c                |   74 ++
f731ee
 util/grub-mknetdir.c               |   23 +-
f731ee
 include/grub/efi/api.h             |  180 ++++-
f731ee
 include/grub/efi/dhcp.h            |  343 +++++++++
f731ee
 include/grub/efi/http.h            |  215 ++++++
f731ee
 include/grub/net/efi.h             |  144 ++++
f731ee
 17 files changed, 4626 insertions(+), 41 deletions(-)
f731ee
 create mode 100644 grub-core/net/efi/dhcp.c
f731ee
 create mode 100644 grub-core/net/efi/efi_netfs.c
f731ee
 create mode 100644 grub-core/net/efi/http.c
f731ee
 create mode 100644 grub-core/net/efi/ip4_config.c
f731ee
 create mode 100644 grub-core/net/efi/ip6_config.c
f731ee
 create mode 100644 grub-core/net/efi/net.c
f731ee
 create mode 100644 grub-core/net/efi/pxe.c
f731ee
 create mode 100644 include/grub/efi/dhcp.h
f731ee
 create mode 100644 include/grub/efi/http.h
f731ee
 create mode 100644 include/grub/net/efi.h
f731ee
f731ee
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
f731ee
index b340ceeec7f..00110ae5efd 100644
f731ee
--- a/grub-core/Makefile.core.def
f731ee
+++ b/grub-core/Makefile.core.def
f731ee
@@ -2093,6 +2093,18 @@ module = {
f731ee
   common = hook/datehook.c;
f731ee
 };
f731ee
 
f731ee
+module = {
f731ee
+  name = efi_netfs;
f731ee
+  common = net/efi/efi_netfs.c;
f731ee
+  common = net/efi/net.c;
f731ee
+  common = net/efi/http.c;
f731ee
+  common = net/efi/pxe.c;
f731ee
+  common = net/efi/ip4_config.c;
f731ee
+  common = net/efi/ip6_config.c;
f731ee
+  common = net/efi/dhcp.c;
f731ee
+  enable = efi;
f731ee
+};
f731ee
+
f731ee
 module = {
f731ee
   name = net;
f731ee
   common = net/net.c;
f731ee
@@ -2107,6 +2119,12 @@ module = {
f731ee
   common = net/arp.c;
f731ee
   common = net/netbuff.c;
f731ee
   common = net/url.c;
f731ee
+  efi = net/efi/net.c;
f731ee
+  efi = net/efi/http.c;
f731ee
+  efi = net/efi/pxe.c;
f731ee
+  efi = net/efi/ip4_config.c;
f731ee
+  efi = net/efi/ip6_config.c;
f731ee
+  efi = net/efi/dhcp.c;
f731ee
 };
f731ee
 
f731ee
 module = {
f731ee
diff --git a/grub-core/io/bufio.c b/grub-core/io/bufio.c
f731ee
index 22438277d74..d0b0f71b6ed 100644
f731ee
--- a/grub-core/io/bufio.c
f731ee
+++ b/grub-core/io/bufio.c
f731ee
@@ -132,7 +132,7 @@ grub_bufio_read (grub_file_t file, char *buf, grub_size_t len)
f731ee
     return res;
f731ee
 
f731ee
   /* Need to read some more.  */
f731ee
-  next_buf = (file->offset + res + len - 1) & ~((grub_off_t) bufio->block_size - 1);
f731ee
+  next_buf = (grub_divmod64 (file->offset + res + len - 1, bufio->block_size, NULL)) * bufio->block_size;
f731ee
   /* Now read between file->offset + res and bufio->buffer_at.  */
f731ee
   if (file->offset + res < next_buf)
f731ee
     {
f731ee
diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
f731ee
index 101307fc71e..570535f78d0 100644
f731ee
--- a/grub-core/kern/efi/efi.c
f731ee
+++ b/grub-core/kern/efi/efi.c
f731ee
@@ -674,7 +674,7 @@ grub_efi_print_device_path (grub_efi_device_path_t *dp)
f731ee
 	      {
f731ee
 		grub_efi_ipv4_device_path_t *ipv4
f731ee
 		  = (grub_efi_ipv4_device_path_t *) dp;
f731ee
-		grub_printf ("/IPv4(%u.%u.%u.%u,%u.%u.%u.%u,%u,%u,%x,%x)",
f731ee
+		grub_printf ("/IPv4(%u.%u.%u.%u,%u.%u.%u.%u,%u,%u,%x,%x",
f731ee
 			     (unsigned) ipv4->local_ip_address[0],
f731ee
 			     (unsigned) ipv4->local_ip_address[1],
f731ee
 			     (unsigned) ipv4->local_ip_address[2],
f731ee
@@ -687,33 +687,60 @@ grub_efi_print_device_path (grub_efi_device_path_t *dp)
f731ee
 			     (unsigned) ipv4->remote_port,
f731ee
 			     (unsigned) ipv4->protocol,
f731ee
 			     (unsigned) ipv4->static_ip_address);
f731ee
+		if (len == sizeof (*ipv4))
f731ee
+		  {
f731ee
+		    grub_printf (",%u.%u.%u.%u,%u.%u.%u.%u",
f731ee
+			(unsigned) ipv4->gateway_ip_address[0],
f731ee
+			(unsigned) ipv4->gateway_ip_address[1],
f731ee
+			(unsigned) ipv4->gateway_ip_address[2],
f731ee
+			(unsigned) ipv4->gateway_ip_address[3],
f731ee
+			(unsigned) ipv4->subnet_mask[0],
f731ee
+			(unsigned) ipv4->subnet_mask[1],
f731ee
+			(unsigned) ipv4->subnet_mask[2],
f731ee
+			(unsigned) ipv4->subnet_mask[3]);
f731ee
+		  }
f731ee
+		grub_printf (")");
f731ee
 	      }
f731ee
 	      break;
f731ee
 	    case GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE:
f731ee
 	      {
f731ee
 		grub_efi_ipv6_device_path_t *ipv6
f731ee
 		  = (grub_efi_ipv6_device_path_t *) dp;
f731ee
-		grub_printf ("/IPv6(%x:%x:%x:%x:%x:%x:%x:%x,%x:%x:%x:%x:%x:%x:%x:%x,%u,%u,%x,%x)",
f731ee
-			     (unsigned) ipv6->local_ip_address[0],
f731ee
-			     (unsigned) ipv6->local_ip_address[1],
f731ee
-			     (unsigned) ipv6->local_ip_address[2],
f731ee
-			     (unsigned) ipv6->local_ip_address[3],
f731ee
-			     (unsigned) ipv6->local_ip_address[4],
f731ee
-			     (unsigned) ipv6->local_ip_address[5],
f731ee
-			     (unsigned) ipv6->local_ip_address[6],
f731ee
-			     (unsigned) ipv6->local_ip_address[7],
f731ee
-			     (unsigned) ipv6->remote_ip_address[0],
f731ee
-			     (unsigned) ipv6->remote_ip_address[1],
f731ee
-			     (unsigned) ipv6->remote_ip_address[2],
f731ee
-			     (unsigned) ipv6->remote_ip_address[3],
f731ee
-			     (unsigned) ipv6->remote_ip_address[4],
f731ee
-			     (unsigned) ipv6->remote_ip_address[5],
f731ee
-			     (unsigned) ipv6->remote_ip_address[6],
f731ee
-			     (unsigned) ipv6->remote_ip_address[7],
f731ee
+		grub_printf ("/IPv6(%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x,%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x,%u,%u,%x,%x",
f731ee
+			     (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[0]),
f731ee
+			     (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[1]),
f731ee
+			     (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[2]),
f731ee
+			     (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[3]),
f731ee
+			     (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[4]),
f731ee
+			     (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[5]),
f731ee
+			     (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[6]),
f731ee
+			     (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[7]),
f731ee
+			     (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[0]),
f731ee
+			     (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[1]),
f731ee
+			     (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[2]),
f731ee
+			     (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[3]),
f731ee
+			     (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[4]),
f731ee
+			     (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[5]),
f731ee
+			     (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[6]),
f731ee
+			     (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[7]),
f731ee
 			     (unsigned) ipv6->local_port,
f731ee
 			     (unsigned) ipv6->remote_port,
f731ee
 			     (unsigned) ipv6->protocol,
f731ee
 			     (unsigned) ipv6->static_ip_address);
f731ee
+		if (len == sizeof (*ipv6))
f731ee
+		  {
f731ee
+		    grub_printf (",%u,%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
f731ee
+			(unsigned) ipv6->prefix_length,
f731ee
+			(unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[0]),
f731ee
+			(unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[1]),
f731ee
+			(unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[2]),
f731ee
+			(unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[3]),
f731ee
+			(unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[4]),
f731ee
+			(unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[5]),
f731ee
+			(unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[6]),
f731ee
+			(unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[7]));
f731ee
+		  }
f731ee
+		grub_printf (")");
f731ee
 	      }
f731ee
 	      break;
f731ee
 	    case GRUB_EFI_INFINIBAND_DEVICE_PATH_SUBTYPE:
f731ee
@@ -753,6 +780,39 @@ grub_efi_print_device_path (grub_efi_device_path_t *dp)
f731ee
 	      dump_vendor_path ("Messaging",
f731ee
 				(grub_efi_vendor_device_path_t *) dp);
f731ee
 	      break;
f731ee
+	    case GRUB_EFI_URI_DEVICE_PATH_SUBTYPE:
f731ee
+	      {
f731ee
+		grub_efi_uri_device_path_t *uri
f731ee
+		  = (grub_efi_uri_device_path_t *) dp;
f731ee
+		grub_printf ("/URI(%s)", uri->uri);
f731ee
+	      }
f731ee
+	      break;
f731ee
+	    case GRUB_EFI_DNS_DEVICE_PATH_SUBTYPE:
f731ee
+	      {
f731ee
+		grub_efi_dns_device_path_t *dns
f731ee
+		  = (grub_efi_dns_device_path_t *) dp;
f731ee
+		if (dns->is_ipv6)
f731ee
+		  {
f731ee
+		    grub_printf ("/DNS(%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x)",
f731ee
+			    (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[0]) >> 16),
f731ee
+			    (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[0])),
f731ee
+			    (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[1]) >> 16),
f731ee
+			    (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[1])),
f731ee
+			    (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[2]) >> 16),
f731ee
+			    (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[2])),
f731ee
+			    (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[3]) >> 16),
f731ee
+			    (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[3])));
f731ee
+		  }
f731ee
+		else
f731ee
+		  {
f731ee
+		    grub_printf ("/DNS(%d.%d.%d.%d)",
f731ee
+			  dns->dns_server_ip[0].v4.addr[0],
f731ee
+			  dns->dns_server_ip[0].v4.addr[1],
f731ee
+			  dns->dns_server_ip[0].v4.addr[2],
f731ee
+			  dns->dns_server_ip[0].v4.addr[3]);
f731ee
+		  }
f731ee
+	      }
f731ee
+	      break;
f731ee
 	    default:
f731ee
 	      grub_printf ("/UnknownMessaging(%x)", (unsigned) subtype);
f731ee
 	      break;
f731ee
diff --git a/grub-core/net/drivers/efi/efinet.c b/grub-core/net/drivers/efi/efinet.c
f731ee
index 25616a9c6c8..3bea6221745 100644
f731ee
--- a/grub-core/net/drivers/efi/efinet.c
f731ee
+++ b/grub-core/net/drivers/efi/efinet.c
f731ee
@@ -28,6 +28,7 @@
f731ee
 #include <grub/lib/hexdump.h>
f731ee
 #include <grub/types.h>
f731ee
 #include <grub/net/netbuff.h>
f731ee
+#include <grub/env.h>
f731ee
 
f731ee
 GRUB_MOD_LICENSE ("GPLv3+");
f731ee
 
f731ee
@@ -502,6 +503,17 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u
f731ee
 
f731ee
   ldp = grub_efi_find_last_device_path (ddp);
f731ee
 
f731ee
+  /* Skip the DNS Device */
f731ee
+  if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) == GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
f731ee
+      && GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) == GRUB_EFI_DNS_DEVICE_PATH_SUBTYPE)
f731ee
+    {
f731ee
+      ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
f731ee
+      ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
f731ee
+      ldp->length = sizeof (*ldp);
f731ee
+
f731ee
+      ldp = grub_efi_find_last_device_path (ddp);
f731ee
+    }
f731ee
+
f731ee
   if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) != GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
f731ee
       || (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE
f731ee
           && GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE))
f731ee
@@ -771,6 +783,7 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
f731ee
 	if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) != GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
f731ee
 	    || (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE
f731ee
 		&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE
f731ee
+		&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_DNS_DEVICE_PATH_SUBTYPE
f731ee
 		&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_URI_DEVICE_PATH_SUBTYPE))
f731ee
 	  continue;
f731ee
 	dup_dp = grub_efi_duplicate_device_path (dp);
f731ee
@@ -785,6 +798,15 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
f731ee
 	    dup_ldp->length = sizeof (*dup_ldp);
f731ee
 	  }
f731ee
 
f731ee
+	dup_ldp = grub_efi_find_last_device_path (dup_dp);
f731ee
+	if (GRUB_EFI_DEVICE_PATH_SUBTYPE (dup_ldp) == GRUB_EFI_DNS_DEVICE_PATH_SUBTYPE)
f731ee
+	  {
f731ee
+	    dup_ldp = grub_efi_find_last_device_path (dup_dp);
f731ee
+	    dup_ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
f731ee
+	    dup_ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
f731ee
+	    dup_ldp->length = sizeof (*dup_ldp);
f731ee
+	  }
f731ee
+
f731ee
 	dup_ldp = grub_efi_find_last_device_path (dup_dp);
f731ee
 	dup_ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
f731ee
 	dup_ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
f731ee
@@ -857,6 +879,9 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
f731ee
 
f731ee
 GRUB_MOD_INIT(efinet)
f731ee
 {
f731ee
+  if (grub_efi_net_config)
f731ee
+    return;
f731ee
+
f731ee
   grub_efinet_findcards ();
f731ee
   grub_efi_net_config = grub_efi_net_config_real;
f731ee
 }
f731ee
@@ -868,5 +893,7 @@ GRUB_MOD_FINI(efinet)
f731ee
   FOR_NET_CARDS_SAFE (card, next) 
f731ee
     if (card->driver == &efidriver)
f731ee
       grub_net_card_unregister (card);
f731ee
+
f731ee
+  grub_efi_net_config = NULL;
f731ee
 }
f731ee
 
f731ee
diff --git a/grub-core/net/efi/dhcp.c b/grub-core/net/efi/dhcp.c
f731ee
new file mode 100644
f731ee
index 00000000000..dbef63d8c08
f731ee
--- /dev/null
f731ee
+++ b/grub-core/net/efi/dhcp.c
f731ee
@@ -0,0 +1,397 @@
f731ee
+#include <grub/mm.h>
f731ee
+#include <grub/command.h>
f731ee
+#include <grub/efi/api.h>
f731ee
+#include <grub/efi/efi.h>
f731ee
+#include <grub/misc.h>
f731ee
+#include <grub/net/efi.h>
f731ee
+#include <grub/charset.h>
f731ee
+
f731ee
+#ifdef GRUB_EFI_NET_DEBUG
f731ee
+static void
f731ee
+dhcp4_mode_print (grub_efi_dhcp4_mode_data_t *mode)
f731ee
+{
f731ee
+    switch (mode->state)
f731ee
+      {
f731ee
+	case GRUB_EFI_DHCP4_STOPPED:
f731ee
+	  grub_printf ("STATE: STOPPED\n");
f731ee
+	  break;
f731ee
+	case GRUB_EFI_DHCP4_INIT:
f731ee
+	  grub_printf ("STATE: INIT\n");
f731ee
+	  break;
f731ee
+	case GRUB_EFI_DHCP4_SELECTING:
f731ee
+	  grub_printf ("STATE: SELECTING\n");
f731ee
+	  break;
f731ee
+	case GRUB_EFI_DHCP4_REQUESTING:
f731ee
+	  grub_printf ("STATE: REQUESTING\n");
f731ee
+	  break;
f731ee
+	case GRUB_EFI_DHCP4_BOUND:
f731ee
+	  grub_printf ("STATE: BOUND\n");
f731ee
+	  break;
f731ee
+	case GRUB_EFI_DHCP4_RENEWING:
f731ee
+	  grub_printf ("STATE: RENEWING\n");
f731ee
+	  break;
f731ee
+	case GRUB_EFI_DHCP4_REBINDING:
f731ee
+	  grub_printf ("STATE: REBINDING\n");
f731ee
+	  break;
f731ee
+	case GRUB_EFI_DHCP4_INIT_REBOOT:
f731ee
+	  grub_printf ("STATE: INIT_REBOOT\n");
f731ee
+	  break;
f731ee
+	case GRUB_EFI_DHCP4_REBOOTING:
f731ee
+	  grub_printf ("STATE: REBOOTING\n");
f731ee
+	  break;
f731ee
+	default:
f731ee
+	  grub_printf ("STATE: UNKNOWN\n");
f731ee
+	  break;
f731ee
+      }
f731ee
+
f731ee
+    grub_printf ("CLIENT_ADDRESS: %u.%u.%u.%u\n",
f731ee
+      mode->client_address[0],
f731ee
+      mode->client_address[1],
f731ee
+      mode->client_address[2],
f731ee
+      mode->client_address[3]);
f731ee
+    grub_printf ("SERVER_ADDRESS: %u.%u.%u.%u\n",
f731ee
+      mode->server_address[0],
f731ee
+      mode->server_address[1],
f731ee
+      mode->server_address[2],
f731ee
+      mode->server_address[3]);
f731ee
+    grub_printf ("SUBNET_MASK: %u.%u.%u.%u\n",
f731ee
+      mode->subnet_mask[0],
f731ee
+      mode->subnet_mask[1],
f731ee
+      mode->subnet_mask[2],
f731ee
+      mode->subnet_mask[3]);
f731ee
+    grub_printf ("ROUTER_ADDRESS: %u.%u.%u.%u\n",
f731ee
+      mode->router_address[0],
f731ee
+      mode->router_address[1],
f731ee
+      mode->router_address[2],
f731ee
+      mode->router_address[3]);
f731ee
+}
f731ee
+#endif
f731ee
+
f731ee
+static grub_efi_ipv4_address_t *
f731ee
+grub_efi_dhcp4_parse_dns (grub_efi_dhcp4_protocol_t *dhcp4, grub_efi_dhcp4_packet_t *reply_packet)
f731ee
+{
f731ee
+  grub_efi_dhcp4_packet_option_t **option_list;
f731ee
+  grub_efi_status_t status;
f731ee
+  grub_efi_uint32_t option_count = 0;
f731ee
+  grub_efi_uint32_t i;
f731ee
+
f731ee
+  status = efi_call_4 (dhcp4->parse, dhcp4, reply_packet, &option_count, NULL);
f731ee
+
f731ee
+  if (status != GRUB_EFI_BUFFER_TOO_SMALL)
f731ee
+    return NULL;
f731ee
+
f731ee
+  option_list = grub_malloc (option_count * sizeof(*option_list));
f731ee
+  if (!option_list)
f731ee
+    return NULL;
f731ee
+
f731ee
+  status = efi_call_4 (dhcp4->parse, dhcp4, reply_packet, &option_count, option_list);
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    {
f731ee
+      grub_free (option_list);
f731ee
+      return NULL;
f731ee
+    }
f731ee
+
f731ee
+  for (i = 0; i < option_count; ++i)
f731ee
+    {
f731ee
+      if (option_list[i]->op_code == 6)
f731ee
+	{
f731ee
+	  grub_efi_ipv4_address_t *dns_address;
f731ee
+
f731ee
+	  if (((option_list[i]->length & 0x3) != 0) || (option_list[i]->length == 0))
f731ee
+	    continue;
f731ee
+
f731ee
+	  /* We only contact primary dns */
f731ee
+	  dns_address = grub_malloc (sizeof (*dns_address));
f731ee
+	  if (!dns_address)
f731ee
+	    {
f731ee
+	      grub_free (option_list);
f731ee
+	      return NULL;
f731ee
+	    }
f731ee
+	  grub_memcpy (dns_address, option_list[i]->data, sizeof (dns_address));
f731ee
+	  grub_free (option_list);
f731ee
+	  return dns_address;
f731ee
+	}
f731ee
+    }
f731ee
+
f731ee
+  grub_free (option_list);
f731ee
+  return NULL;
f731ee
+}
f731ee
+
f731ee
+#if 0
f731ee
+/* Somehow this doesn't work ... */
f731ee
+static grub_err_t
f731ee
+grub_cmd_efi_bootp (struct grub_command *cmd __attribute__ ((unused)),
f731ee
+		    int argc __attribute__ ((unused)),
f731ee
+		    char **args __attribute__ ((unused)))
f731ee
+{
f731ee
+  struct grub_efi_net_device *dev;
f731ee
+  for (dev = net_devices; dev; dev = dev->next)
f731ee
+    {
f731ee
+      grub_efi_pxe_t *pxe = dev->ip4_pxe;
f731ee
+      grub_efi_pxe_mode_t *mode = pxe->mode;
f731ee
+      grub_efi_status_t status;
f731ee
+
f731ee
+      if (!mode->started)
f731ee
+	{
f731ee
+	  status = efi_call_2 (pxe->start, pxe, 0);
f731ee
+
f731ee
+	  if (status != GRUB_EFI_SUCCESS)
f731ee
+	      grub_printf ("Couldn't start PXE\n");
f731ee
+	}
f731ee
+
f731ee
+      status = efi_call_2 (pxe->dhcp, pxe, 0);
f731ee
+      if (status != GRUB_EFI_SUCCESS)
f731ee
+	{
f731ee
+	  grub_printf ("dhcp4 configure failed, %d\n", (int)status);
f731ee
+	  continue;
f731ee
+	}
f731ee
+
f731ee
+      dev->prefer_ip6 = 0;
f731ee
+    }
f731ee
+
f731ee
+  return GRUB_ERR_NONE;
f731ee
+}
f731ee
+#endif
f731ee
+
f731ee
+static grub_err_t
f731ee
+grub_cmd_efi_bootp (struct grub_command *cmd __attribute__ ((unused)),
f731ee
+		    int argc,
f731ee
+		    char **args)
f731ee
+{
f731ee
+  struct grub_efi_net_device *netdev;
f731ee
+
f731ee
+  for (netdev = net_devices; netdev; netdev = netdev->next)
f731ee
+    {
f731ee
+      grub_efi_status_t status;
f731ee
+      grub_efi_dhcp4_mode_data_t mode;
f731ee
+      grub_efi_dhcp4_config_data_t config;
f731ee
+      grub_efi_dhcp4_packet_option_t *options;
f731ee
+      grub_efi_ipv4_address_t *dns_address;
f731ee
+      grub_efi_net_ip_manual_address_t net_ip;
f731ee
+      grub_efi_net_ip_address_t ip_addr;
f731ee
+      grub_efi_net_interface_t *inf = NULL;
f731ee
+
f731ee
+      if (argc > 0 && grub_strcmp (netdev->card_name, args[0]) != 0)
f731ee
+	continue;
f731ee
+
f731ee
+      grub_memset (&config, 0, sizeof(config));
f731ee
+
f731ee
+      config.option_count = 1;
f731ee
+      options = grub_malloc (sizeof(*options) + 2);
f731ee
+      /* Parameter request list */
f731ee
+      options->op_code = 55;
f731ee
+      options->length = 3;
f731ee
+      /* subnet mask */
f731ee
+      options->data[0] = 1;
f731ee
+      /* router */
f731ee
+      options->data[1] = 3;
f731ee
+      /* DNS */
f731ee
+      options->data[2] = 6;
f731ee
+      config.option_list = &options;
f731ee
+
f731ee
+      /* FIXME: What if the dhcp has bounded */
f731ee
+      status = efi_call_2 (netdev->dhcp4->configure, netdev->dhcp4, &config);
f731ee
+      grub_free (options);
f731ee
+      if (status != GRUB_EFI_SUCCESS)
f731ee
+	{
f731ee
+	  grub_printf ("dhcp4 configure failed, %d\n", (int)status);
f731ee
+	  continue;
f731ee
+	}
f731ee
+
f731ee
+      status = efi_call_2 (netdev->dhcp4->start, netdev->dhcp4, NULL);
f731ee
+      if (status != GRUB_EFI_SUCCESS)
f731ee
+	{
f731ee
+	  grub_printf ("dhcp4 start failed, %d\n", (int)status);
f731ee
+	  continue;
f731ee
+	}
f731ee
+
f731ee
+      status = efi_call_2 (netdev->dhcp4->get_mode_data, netdev->dhcp4, &mode);
f731ee
+      if (status != GRUB_EFI_SUCCESS)
f731ee
+	{
f731ee
+	  grub_printf ("dhcp4 get mode failed, %d\n", (int)status);
f731ee
+	  continue;
f731ee
+	}
f731ee
+
f731ee
+#ifdef GRUB_EFI_NET_DEBUG
f731ee
+      dhcp4_mode_print (&mode);
f731ee
+#endif
f731ee
+
f731ee
+      for (inf = netdev->net_interfaces; inf; inf = inf->next)
f731ee
+	if (inf->prefer_ip6 == 0)
f731ee
+	  break;
f731ee
+
f731ee
+      grub_memcpy (net_ip.ip4.address, mode.client_address, sizeof (net_ip.ip4.address));
f731ee
+      grub_memcpy (net_ip.ip4.subnet_mask, mode.subnet_mask, sizeof (net_ip.ip4.subnet_mask));
f731ee
+
f731ee
+      if (!inf)
f731ee
+	{
f731ee
+	  char *name = grub_xasprintf ("%s:dhcp", netdev->card_name);
f731ee
+
f731ee
+	  net_ip.is_ip6 = 0;
f731ee
+	  inf = grub_efi_net_create_interface (netdev,
f731ee
+		    name,
f731ee
+		    &net_ip,
f731ee
+		    1);
f731ee
+	  grub_free (name);
f731ee
+	}
f731ee
+      else
f731ee
+	{
f731ee
+	  efi_net_interface_set_address (inf, &net_ip, 1);
f731ee
+	}
f731ee
+
f731ee
+      grub_memcpy (ip_addr.ip4, mode.router_address, sizeof (ip_addr.ip4));
f731ee
+      efi_net_interface_set_gateway (inf, &ip_addr);
f731ee
+
f731ee
+      dns_address = grub_efi_dhcp4_parse_dns (netdev->dhcp4, mode.reply_packet);
f731ee
+      if (dns_address)
f731ee
+	efi_net_interface_set_dns (inf, (grub_efi_net_ip_address_t *)&dns_address);
f731ee
+
f731ee
+    }
f731ee
+
f731ee
+  return GRUB_ERR_NONE;
f731ee
+}
f731ee
+
f731ee
+
f731ee
+static grub_err_t
f731ee
+grub_cmd_efi_bootp6 (struct grub_command *cmd __attribute__ ((unused)),
f731ee
+		    int argc,
f731ee
+		    char **args)
f731ee
+{
f731ee
+  struct grub_efi_net_device *dev;
f731ee
+  grub_efi_uint32_t ia_id;
f731ee
+
f731ee
+  for (dev = net_devices, ia_id = 0; dev; dev = dev->next, ia_id++)
f731ee
+    {
f731ee
+      grub_efi_dhcp6_config_data_t config;
f731ee
+      grub_efi_dhcp6_packet_option_t *option_list[1];
f731ee
+      grub_efi_dhcp6_packet_option_t *opt;
f731ee
+      grub_efi_status_t status;
f731ee
+      grub_efi_dhcp6_mode_data_t mode;
f731ee
+      grub_efi_dhcp6_retransmission_t retrans;
f731ee
+      grub_efi_net_ip_manual_address_t net_ip;
f731ee
+      grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
f731ee
+      grub_efi_net_interface_t *inf = NULL;
f731ee
+
f731ee
+      if (argc > 0 && grub_strcmp (dev->card_name, args[0]) != 0)
f731ee
+	continue;
f731ee
+
f731ee
+      opt = grub_malloc (sizeof(*opt) + 2 * sizeof (grub_efi_uint16_t));
f731ee
+
f731ee
+#define GRUB_EFI_DHCP6_OPT_ORO 6
f731ee
+
f731ee
+      opt->op_code = grub_cpu_to_be16_compile_time (GRUB_EFI_DHCP6_OPT_ORO);
f731ee
+      opt->op_len = grub_cpu_to_be16_compile_time (2 * sizeof (grub_efi_uint16_t));
f731ee
+
f731ee
+#define GRUB_EFI_DHCP6_OPT_BOOT_FILE_URL 59
f731ee
+#define GRUB_EFI_DHCP6_OPT_DNS_SERVERS 23
f731ee
+
f731ee
+      grub_set_unaligned16 (opt->data, grub_cpu_to_be16_compile_time(GRUB_EFI_DHCP6_OPT_BOOT_FILE_URL));
f731ee
+      grub_set_unaligned16 (opt->data + 1 * sizeof (grub_efi_uint16_t),
f731ee
+	      grub_cpu_to_be16_compile_time(GRUB_EFI_DHCP6_OPT_DNS_SERVERS));
f731ee
+
f731ee
+      option_list[0] = opt;
f731ee
+      retrans.irt = 4;
f731ee
+      retrans.mrc = 4;
f731ee
+      retrans.mrt = 32;
f731ee
+      retrans.mrd = 60;
f731ee
+
f731ee
+      config.dhcp6_callback = NULL;
f731ee
+      config.callback_context = NULL;
f731ee
+      config.option_count = 1;
f731ee
+      config.option_list = option_list;
f731ee
+      config.ia_descriptor.ia_id = ia_id;
f731ee
+      config.ia_descriptor.type = GRUB_EFI_DHCP6_IA_TYPE_NA;
f731ee
+      config.ia_info_event = NULL;
f731ee
+      config.reconfigure_accept = 0;
f731ee
+      config.rapid_commit = 0;
f731ee
+      config.solicit_retransmission = &retrans;
f731ee
+
f731ee
+      status = efi_call_2 (dev->dhcp6->configure, dev->dhcp6, &config);
f731ee
+      grub_free (opt);
f731ee
+      if (status != GRUB_EFI_SUCCESS)
f731ee
+	{
f731ee
+	  grub_printf ("dhcp6 configure failed, %d\n", (int)status);
f731ee
+	  continue;
f731ee
+	}
f731ee
+      status = efi_call_1 (dev->dhcp6->start, dev->dhcp6);
f731ee
+      if (status != GRUB_EFI_SUCCESS)
f731ee
+	{
f731ee
+	  grub_printf ("dhcp6 start failed, %d\n", (int)status);
f731ee
+	  continue;
f731ee
+	}
f731ee
+
f731ee
+      status = efi_call_3 (dev->dhcp6->get_mode_data, dev->dhcp6, &mode, NULL);
f731ee
+      if (status != GRUB_EFI_SUCCESS)
f731ee
+	{
f731ee
+	  grub_printf ("dhcp4 get mode failed, %d\n", (int)status);
f731ee
+	  continue;
f731ee
+	}
f731ee
+
f731ee
+      for (inf = dev->net_interfaces; inf; inf = inf->next)
f731ee
+	if (inf->prefer_ip6 == 1)
f731ee
+	  break;
f731ee
+
f731ee
+      grub_memcpy (net_ip.ip6.address, mode.ia->ia_address[0].ip_address, sizeof (net_ip.ip6.address));
f731ee
+      net_ip.ip6.prefix_length = 64;
f731ee
+      net_ip.ip6.is_anycast = 0;
f731ee
+      net_ip.is_ip6 = 1;
f731ee
+
f731ee
+      if (!inf)
f731ee
+	{
f731ee
+	  char *name = grub_xasprintf ("%s:dhcp", dev->card_name);
f731ee
+
f731ee
+	  inf = grub_efi_net_create_interface (dev,
f731ee
+		    name,
f731ee
+		    &net_ip,
f731ee
+		    1);
f731ee
+	  grub_free (name);
f731ee
+	}
f731ee
+      else
f731ee
+	{
f731ee
+	  efi_net_interface_set_address (inf, &net_ip, 1);
f731ee
+	}
f731ee
+
f731ee
+      {
f731ee
+	grub_efi_uint32_t count = 0;
f731ee
+	grub_efi_dhcp6_packet_option_t **options = NULL;
f731ee
+	grub_efi_uint32_t i;
f731ee
+
f731ee
+	status = efi_call_4 (dev->dhcp6->parse, dev->dhcp6, mode.ia->reply_packet, &count, NULL);
f731ee
+
f731ee
+	if (status == GRUB_EFI_BUFFER_TOO_SMALL && count)
f731ee
+	  {
f731ee
+	    options = grub_malloc (count * sizeof(*options));
f731ee
+	    status = efi_call_4 (dev->dhcp6->parse, dev->dhcp6, mode.ia->reply_packet, &count, options);
f731ee
+	  }
f731ee
+
f731ee
+	if (status != GRUB_EFI_SUCCESS)
f731ee
+	  {
f731ee
+	    if (options)
f731ee
+	      grub_free (options);
f731ee
+	    continue;
f731ee
+	  }
f731ee
+
f731ee
+	for (i = 0; i < count; ++i)
f731ee
+	  {
f731ee
+	    if (options[i]->op_code == grub_cpu_to_be16_compile_time(GRUB_EFI_DHCP6_OPT_DNS_SERVERS))
f731ee
+	      {
f731ee
+		grub_efi_net_ip_address_t dns;
f731ee
+		grub_memcpy (dns.ip6, options[i]->data, sizeof(net_ip.ip6));
f731ee
+		efi_net_interface_set_dns (inf, &dns);
f731ee
+		break;
f731ee
+	      }
f731ee
+	  }
f731ee
+
f731ee
+	if (options)
f731ee
+	  grub_free (options);
f731ee
+      }
f731ee
+
f731ee
+      efi_call_1 (b->free_pool, mode.client_id);
f731ee
+      efi_call_1 (b->free_pool, mode.ia);
f731ee
+    }
f731ee
+
f731ee
+  return GRUB_ERR_NONE;
f731ee
+}
f731ee
+
f731ee
+grub_command_func_t grub_efi_net_bootp = grub_cmd_efi_bootp;
f731ee
+grub_command_func_t grub_efi_net_bootp6 = grub_cmd_efi_bootp6;
f731ee
diff --git a/grub-core/net/efi/efi_netfs.c b/grub-core/net/efi/efi_netfs.c
f731ee
new file mode 100644
f731ee
index 00000000000..ef371d885ea
f731ee
--- /dev/null
f731ee
+++ b/grub-core/net/efi/efi_netfs.c
f731ee
@@ -0,0 +1,57 @@
f731ee
+#include <grub/dl.h>
f731ee
+#include <grub/env.h>
f731ee
+#define EFI_NET_CMD_PREFIX "net_efi"
f731ee
+#include <grub/net/efi.h>
f731ee
+
f731ee
+GRUB_MOD_LICENSE ("GPLv3+");
f731ee
+
f731ee
+static grub_command_t cmd_efi_lsroutes;
f731ee
+static grub_command_t cmd_efi_lscards;
f731ee
+static grub_command_t cmd_efi_lsaddrs;
f731ee
+static grub_command_t cmd_efi_addaddr;
f731ee
+static grub_command_t cmd_efi_bootp;
f731ee
+static grub_command_t cmd_efi_bootp6;
f731ee
+
f731ee
+static int initialized;
f731ee
+
f731ee
+GRUB_MOD_INIT(efi_netfs)
f731ee
+{
f731ee
+  if (grub_net_open)
f731ee
+    return;
f731ee
+
f731ee
+  if (grub_efi_net_fs_init ())
f731ee
+    {
f731ee
+      cmd_efi_lsroutes = grub_register_command ("net_efi_ls_routes", grub_efi_net_list_routes,
f731ee
+					    "", N_("list network routes"));
f731ee
+      cmd_efi_lscards = grub_register_command ("net_efi_ls_cards", grub_efi_net_list_cards,
f731ee
+					   "", N_("list network cards"));
f731ee
+      cmd_efi_lsaddrs = grub_register_command ("net_efi_ls_addr", grub_efi_net_list_addrs,
f731ee
+					  "", N_("list network addresses"));
f731ee
+      cmd_efi_addaddr = grub_register_command ("net_efi_add_addr", grub_efi_net_add_addr,
f731ee
+					  N_("SHORTNAME CARD ADDRESS [HWADDRESS]"),
f731ee
+					  N_("Add a network address."));
f731ee
+      cmd_efi_bootp = grub_register_command ("net_efi_bootp", grub_efi_net_bootp,
f731ee
+					 N_("[CARD]"),
f731ee
+					 N_("perform a bootp autoconfiguration"));
f731ee
+      cmd_efi_bootp6 = grub_register_command ("net_efi_bootp6", grub_efi_net_bootp6,
f731ee
+					 N_("[CARD]"),
f731ee
+					 N_("perform a bootp autoconfiguration"));
f731ee
+      initialized = 1;
f731ee
+    }
f731ee
+}
f731ee
+
f731ee
+GRUB_MOD_FINI(efi_netfs)
f731ee
+{
f731ee
+  if (initialized)
f731ee
+    {
f731ee
+      grub_unregister_command (cmd_efi_lsroutes);
f731ee
+      grub_unregister_command (cmd_efi_lscards);
f731ee
+      grub_unregister_command (cmd_efi_lsaddrs);
f731ee
+      grub_unregister_command (cmd_efi_addaddr);
f731ee
+      grub_unregister_command (cmd_efi_bootp);
f731ee
+      grub_unregister_command (cmd_efi_bootp6);
f731ee
+      grub_efi_net_fs_fini ();
f731ee
+      initialized = 0;
f731ee
+      return;
f731ee
+    }
f731ee
+}
f731ee
diff --git a/grub-core/net/efi/http.c b/grub-core/net/efi/http.c
f731ee
new file mode 100644
f731ee
index 00000000000..3f61fd2fa5b
f731ee
--- /dev/null
f731ee
+++ b/grub-core/net/efi/http.c
f731ee
@@ -0,0 +1,419 @@
f731ee
+
f731ee
+#include <grub/efi/api.h>
f731ee
+#include <grub/efi/efi.h>
f731ee
+#include <grub/misc.h>
f731ee
+#include <grub/net/efi.h>
f731ee
+#include <grub/charset.h>
f731ee
+
f731ee
+static void
f731ee
+http_configure (struct grub_efi_net_device *dev, int prefer_ip6)
f731ee
+{
f731ee
+  grub_efi_http_config_data_t http_config;
f731ee
+  grub_efi_httpv4_access_point_t httpv4_node;
f731ee
+  grub_efi_httpv6_access_point_t httpv6_node;
f731ee
+  grub_efi_status_t status;
f731ee
+
f731ee
+  grub_efi_http_t *http = dev->http;
f731ee
+
f731ee
+  grub_memset (&http_config, 0, sizeof(http_config));
f731ee
+  http_config.http_version = GRUB_EFI_HTTPVERSION11;
f731ee
+  http_config.timeout_millisec = 5000;
f731ee
+
f731ee
+  if (prefer_ip6)
f731ee
+    {
f731ee
+      grub_efi_uintn_t sz;
f731ee
+      grub_efi_ip6_config_manual_address_t manual_address;
f731ee
+
f731ee
+      http_config.local_address_is_ipv6 = 1;
f731ee
+      sz = sizeof (manual_address);
f731ee
+      status = efi_call_4 (dev->ip6_config->get_data, dev->ip6_config,
f731ee
+			GRUB_EFI_IP6_CONFIG_DATA_TYPE_MANUAL_ADDRESS,
f731ee
+			&sz, &manual_address);
f731ee
+
f731ee
+      if (status == GRUB_EFI_NOT_FOUND)
f731ee
+	{
f731ee
+	  grub_printf ("The MANUAL ADDRESS is not found\n");
f731ee
+	}
f731ee
+
f731ee
+      /* FIXME: The manual interface would return BUFFER TOO SMALL !!! */
f731ee
+      if (status != GRUB_EFI_SUCCESS)
f731ee
+	{
f731ee
+	  grub_printf ("??? %d\n",(int) status);
f731ee
+	  return;
f731ee
+	}
f731ee
+
f731ee
+      grub_memcpy (httpv6_node.local_address, manual_address.address, sizeof (httpv6_node.local_address));
f731ee
+      httpv6_node.local_port = 0;
f731ee
+      http_config.access_point.ipv6_node = &httpv6_node;
f731ee
+    }
f731ee
+  else
f731ee
+    {
f731ee
+      http_config.local_address_is_ipv6 = 0;
f731ee
+      grub_memset (&httpv4_node, 0, sizeof(httpv4_node));
f731ee
+      httpv4_node.use_default_address = 1;
f731ee
+
f731ee
+      /* Use random port here */
f731ee
+      /* See TcpBind() in edk2/NetworkPkg/TcpDxe/TcpDispatcher.c */
f731ee
+      httpv4_node.local_port = 0;
f731ee
+      http_config.access_point.ipv4_node = &httpv4_node;
f731ee
+    }
f731ee
+
f731ee
+  status = efi_call_2 (http->configure, http, &http_config);
f731ee
+
f731ee
+  if (status == GRUB_EFI_ALREADY_STARTED)
f731ee
+    {
f731ee
+      /* XXX: This hangs HTTPS boot */
f731ee
+#if 0
f731ee
+      if (efi_call_2 (http->configure, http, NULL) != GRUB_EFI_SUCCESS)
f731ee
+	{
f731ee
+	  grub_error (GRUB_ERR_IO, N_("couldn't reset http instance"));
f731ee
+	  grub_print_error ();
f731ee
+	  return;
f731ee
+	}
f731ee
+      status = efi_call_2 (http->configure, http, &http_config);
f731ee
+#endif
f731ee
+      return;
f731ee
+    }
f731ee
+
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    {
f731ee
+      grub_error (GRUB_ERR_IO, N_("couldn't configure http protocol, reason: %d"), (int)status);
f731ee
+      grub_print_error ();
f731ee
+      return ;
f731ee
+    }
f731ee
+}
f731ee
+
f731ee
+static grub_efi_boolean_t request_callback_done;
f731ee
+static grub_efi_boolean_t response_callback_done;
f731ee
+
f731ee
+static void
f731ee
+grub_efi_http_request_callback (grub_efi_event_t event __attribute__ ((unused)),
f731ee
+				void *context __attribute__ ((unused)))
f731ee
+{
f731ee
+  request_callback_done = 1;
f731ee
+}
f731ee
+
f731ee
+static void
f731ee
+grub_efi_http_response_callback (grub_efi_event_t event __attribute__ ((unused)),
f731ee
+				void *context __attribute__ ((unused)))
f731ee
+{
f731ee
+  response_callback_done = 1;
f731ee
+}
f731ee
+
f731ee
+static grub_err_t
f731ee
+efihttp_request (grub_efi_http_t *http, char *server, char *name, int use_https, int headeronly, grub_off_t *file_size)
f731ee
+{
f731ee
+  grub_efi_http_request_data_t request_data;
f731ee
+  grub_efi_http_message_t request_message;
f731ee
+  grub_efi_http_token_t request_token;
f731ee
+  grub_efi_http_response_data_t response_data;
f731ee
+  grub_efi_http_message_t response_message;
f731ee
+  grub_efi_http_token_t response_token;
f731ee
+  grub_efi_http_header_t request_headers[3];
f731ee
+
f731ee
+  grub_efi_status_t status;
f731ee
+  grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
f731ee
+  char *url = NULL;
f731ee
+
f731ee
+  request_headers[0].field_name = (grub_efi_char8_t *)"Host";
f731ee
+  request_headers[0].field_value = (grub_efi_char8_t *)server;
f731ee
+  request_headers[1].field_name = (grub_efi_char8_t *)"Accept";
f731ee
+  request_headers[1].field_value = (grub_efi_char8_t *)"*/*";
f731ee
+  request_headers[2].field_name = (grub_efi_char8_t *)"User-Agent";
f731ee
+  request_headers[2].field_value = (grub_efi_char8_t *)"UefiHttpBoot/1.0";
f731ee
+
f731ee
+  {
f731ee
+    grub_efi_ipv6_address_t address;
f731ee
+    const char *rest;
f731ee
+    grub_efi_char16_t *ucs2_url;
f731ee
+    grub_size_t url_len, ucs2_url_len;
f731ee
+    const char *protocol = (use_https == 1) ? "https" : "http";
f731ee
+
f731ee
+    if (grub_efi_string_to_ip6_address (server, &address, &rest) && *rest == 0)
f731ee
+      url = grub_xasprintf ("%s://[%s]%s", protocol, server, name);
f731ee
+    else
f731ee
+      url = grub_xasprintf ("%s://%s%s", protocol, server, name);
f731ee
+
f731ee
+    if (!url)
f731ee
+      {
f731ee
+	return grub_errno;
f731ee
+      }
f731ee
+
f731ee
+    url_len = grub_strlen (url);
f731ee
+    ucs2_url_len = url_len * GRUB_MAX_UTF16_PER_UTF8;
f731ee
+    ucs2_url = grub_malloc ((ucs2_url_len + 1) * sizeof (ucs2_url[0]));
f731ee
+
f731ee
+    if (!ucs2_url)
f731ee
+      {
f731ee
+	grub_free (url);
f731ee
+	return grub_errno;
f731ee
+      }
f731ee
+
f731ee
+    ucs2_url_len = grub_utf8_to_utf16 (ucs2_url, ucs2_url_len, (grub_uint8_t *)url, url_len, NULL); /* convert string format from ascii to usc2 */
f731ee
+    ucs2_url[ucs2_url_len] = 0;
f731ee
+    grub_free (url);
f731ee
+    request_data.url = ucs2_url;
f731ee
+  }
f731ee
+
f731ee
+  request_data.method = (headeronly > 0) ? GRUB_EFI_HTTPMETHODHEAD : GRUB_EFI_HTTPMETHODGET;
f731ee
+
f731ee
+  request_message.data.request = &request_data;
f731ee
+  request_message.header_count = 3;
f731ee
+  request_message.headers = request_headers;
f731ee
+  request_message.body_length = 0;
f731ee
+  request_message.body = NULL;
f731ee
+
f731ee
+  /* request token */
f731ee
+  request_token.event = NULL;
f731ee
+  request_token.status = GRUB_EFI_NOT_READY;
f731ee
+  request_token.message = &request_message;
f731ee
+
f731ee
+  request_callback_done = 0;
f731ee
+  status = efi_call_5 (b->create_event,
f731ee
+                       GRUB_EFI_EVT_NOTIFY_SIGNAL,
f731ee
+                       GRUB_EFI_TPL_CALLBACK,
f731ee
+                       grub_efi_http_request_callback,
f731ee
+                       NULL,
f731ee
+                       &request_token.event);
f731ee
+
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    {
f731ee
+      grub_free (request_data.url);
f731ee
+      return grub_error (GRUB_ERR_IO, "Fail to create an event! status=0x%x\n", status);
f731ee
+    }
f731ee
+
f731ee
+  status = efi_call_2 (http->request, http, &request_token);
f731ee
+
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    {
f731ee
+      efi_call_1 (b->close_event, request_token.event);
f731ee
+      grub_free (request_data.url);
f731ee
+      return grub_error (GRUB_ERR_IO, "Fail to send a request! status=0x%x\n", status);
f731ee
+    }
f731ee
+  /* TODO: Add Timeout */
f731ee
+  while (!request_callback_done)
f731ee
+    efi_call_1(http->poll, http);
f731ee
+
f731ee
+  response_data.status_code = GRUB_EFI_HTTP_STATUS_UNSUPPORTED_STATUS;
f731ee
+  response_message.data.response = &response_data;
f731ee
+  /* herader_count will be updated by the HTTP driver on response */
f731ee
+  response_message.header_count = 0;
f731ee
+  /* headers will be populated by the driver on response */
f731ee
+  response_message.headers = NULL;
f731ee
+  /* use zero BodyLength to only receive the response headers */
f731ee
+  response_message.body_length = 0;
f731ee
+  response_message.body = NULL;
f731ee
+  response_token.event = NULL;
f731ee
+
f731ee
+  status = efi_call_5 (b->create_event,
f731ee
+              GRUB_EFI_EVT_NOTIFY_SIGNAL,
f731ee
+              GRUB_EFI_TPL_CALLBACK,
f731ee
+              grub_efi_http_response_callback,
f731ee
+              NULL,
f731ee
+              &response_token.event);
f731ee
+
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    {
f731ee
+      efi_call_1 (b->close_event, request_token.event);
f731ee
+      grub_free (request_data.url);
f731ee
+      return grub_error (GRUB_ERR_IO, "Fail to create an event! status=0x%x\n", status);
f731ee
+    }
f731ee
+
f731ee
+  response_token.status = GRUB_EFI_SUCCESS;
f731ee
+  response_token.message = &response_message;
f731ee
+
f731ee
+  /* wait for HTTP response */
f731ee
+  response_callback_done = 0;
f731ee
+  status = efi_call_2 (http->response, http, &response_token);
f731ee
+
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    {
f731ee
+      efi_call_1 (b->close_event, response_token.event);
f731ee
+      efi_call_1 (b->close_event, request_token.event);
f731ee
+      grub_free (request_data.url);
f731ee
+      return grub_error (GRUB_ERR_IO, "Fail to receive a response! status=%d\n", (int)status);
f731ee
+    }
f731ee
+
f731ee
+  /* TODO: Add Timeout */
f731ee
+  while (!response_callback_done)
f731ee
+    efi_call_1 (http->poll, http);
f731ee
+
f731ee
+  if (response_message.data.response->status_code != GRUB_EFI_HTTP_STATUS_200_OK)
f731ee
+    {
f731ee
+      grub_efi_http_status_code_t status_code = response_message.data.response->status_code;
f731ee
+
f731ee
+      if (response_message.headers)
f731ee
+	efi_call_1 (b->free_pool, response_message.headers);
f731ee
+      efi_call_1 (b->close_event, response_token.event);
f731ee
+      efi_call_1 (b->close_event, request_token.event);
f731ee
+      grub_free (request_data.url);
f731ee
+      if (status_code == GRUB_EFI_HTTP_STATUS_404_NOT_FOUND)
f731ee
+	{
f731ee
+	  return grub_error (GRUB_ERR_FILE_NOT_FOUND, _("file `%s' not found"), name);
f731ee
+	}
f731ee
+      else
f731ee
+	{
f731ee
+	  return grub_error (GRUB_ERR_NET_UNKNOWN_ERROR,
f731ee
+		  _("unsupported uefi http status code 0x%x"), status_code);
f731ee
+	}
f731ee
+    }
f731ee
+
f731ee
+  if (file_size)
f731ee
+    {
f731ee
+      int i;
f731ee
+      /* parse the length of the file from the ContentLength header */
f731ee
+      for (*file_size = 0, i = 0; i < (int)response_message.header_count; ++i)
f731ee
+	{
f731ee
+	  if (!grub_strcmp((const char*)response_message.headers[i].field_name, "Content-Length"))
f731ee
+	    {
f731ee
+	      *file_size = grub_strtoul((const char*)response_message.headers[i].field_value, 0, 10);
f731ee
+	      break;
f731ee
+	    }
f731ee
+	}
f731ee
+    }
f731ee
+
f731ee
+  if (response_message.headers)
f731ee
+    efi_call_1 (b->free_pool, response_message.headers);
f731ee
+  efi_call_1 (b->close_event, response_token.event);
f731ee
+  efi_call_1 (b->close_event, request_token.event);
f731ee
+  grub_free (request_data.url);
f731ee
+
f731ee
+  return GRUB_ERR_NONE;
f731ee
+}
f731ee
+
f731ee
+static grub_ssize_t
f731ee
+efihttp_read (struct grub_efi_net_device *dev,
f731ee
+		  char *buf,
f731ee
+		  grub_size_t len)
f731ee
+{
f731ee
+  grub_efi_http_message_t response_message;
f731ee
+  grub_efi_http_token_t response_token;
f731ee
+
f731ee
+  grub_efi_status_t status;
f731ee
+  grub_size_t sum = 0;
f731ee
+  grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
f731ee
+  grub_efi_http_t *http = dev->http;
f731ee
+
f731ee
+  if (!len)
f731ee
+    {
f731ee
+      grub_error (GRUB_ERR_BUG, "Invalid arguments to EFI HTTP Read");
f731ee
+      return -1;
f731ee
+    }
f731ee
+
f731ee
+  efi_call_5 (b->create_event,
f731ee
+              GRUB_EFI_EVT_NOTIFY_SIGNAL,
f731ee
+              GRUB_EFI_TPL_CALLBACK,
f731ee
+              grub_efi_http_response_callback,
f731ee
+              NULL,
f731ee
+              &response_token.event);
f731ee
+
f731ee
+  while (len)
f731ee
+    {
f731ee
+      response_message.data.response = NULL;
f731ee
+      response_message.header_count = 0;
f731ee
+      response_message.headers = NULL;
f731ee
+      response_message.body_length = len;
f731ee
+      response_message.body = buf;
f731ee
+
f731ee
+      response_token.message = &response_message;
f731ee
+      response_token.status = GRUB_EFI_NOT_READY;
f731ee
+
f731ee
+      response_callback_done = 0;
f731ee
+
f731ee
+      status = efi_call_2 (http->response, http, &response_token);
f731ee
+      if (status != GRUB_EFI_SUCCESS)
f731ee
+	{
f731ee
+	  efi_call_1 (b->close_event, response_token.event);
f731ee
+	  grub_error (GRUB_ERR_IO, "Error! status=%d\n", (int)status);
f731ee
+	  return -1;
f731ee
+	}
f731ee
+
f731ee
+      while (!response_callback_done)
f731ee
+	efi_call_1(http->poll, http);
f731ee
+
f731ee
+      sum += response_message.body_length;
f731ee
+      buf += response_message.body_length;
f731ee
+      len -= response_message.body_length;
f731ee
+    }
f731ee
+
f731ee
+  efi_call_1 (b->close_event, response_token.event);
f731ee
+
f731ee
+  return sum;
f731ee
+}
f731ee
+
f731ee
+static grub_err_t
f731ee
+grub_efihttp_open (struct grub_efi_net_device *dev,
f731ee
+		  int prefer_ip6 __attribute__ ((unused)),
f731ee
+		  grub_file_t file,
f731ee
+		  const char *filename __attribute__ ((unused)),
f731ee
+		  int type)
f731ee
+{
f731ee
+  grub_err_t err;
f731ee
+  grub_off_t size;
f731ee
+  char *buf;
f731ee
+
f731ee
+  err = efihttp_request (dev->http, file->device->net->server, file->device->net->name, type, 1, 0);
f731ee
+  if (err != GRUB_ERR_NONE)
f731ee
+    return err;
f731ee
+
f731ee
+  err = efihttp_request (dev->http, file->device->net->server, file->device->net->name, type, 0, &size);
f731ee
+  if (err != GRUB_ERR_NONE)
f731ee
+    return err;
f731ee
+
f731ee
+  buf = grub_malloc (size);
f731ee
+  efihttp_read (dev, buf, size);
f731ee
+
f731ee
+  file->size = size;
f731ee
+  file->data = buf;
f731ee
+  file->not_easily_seekable = 0;
f731ee
+  file->device->net->offset = 0;
f731ee
+
f731ee
+  return GRUB_ERR_NONE;
f731ee
+}
f731ee
+
f731ee
+static grub_err_t
f731ee
+grub_efihttp_close (struct grub_efi_net_device *dev __attribute__ ((unused)),
f731ee
+		    int prefer_ip6 __attribute__ ((unused)),
f731ee
+		    grub_file_t file)
f731ee
+{
f731ee
+  if (file->data)
f731ee
+    grub_free (file->data);
f731ee
+
f731ee
+  file->data = 0;
f731ee
+  file->offset = 0;
f731ee
+  file->size = 0;
f731ee
+  file->device->net->offset = 0;
f731ee
+  return GRUB_ERR_NONE;
f731ee
+}
f731ee
+
f731ee
+static grub_ssize_t
f731ee
+grub_efihttp_read (struct grub_efi_net_device *dev __attribute__((unused)),
f731ee
+		  int prefer_ip6 __attribute__((unused)),
f731ee
+		  grub_file_t file,
f731ee
+		  char *buf,
f731ee
+		  grub_size_t len)
f731ee
+{
f731ee
+  grub_size_t r = len;
f731ee
+
f731ee
+  if (!file->data || !buf || !len)
f731ee
+    return 0;
f731ee
+
f731ee
+  if ((file->device->net->offset + len) > file->size)
f731ee
+    r = file->size - file->device->net->offset;
f731ee
+
f731ee
+  if (r)
f731ee
+    {
f731ee
+      grub_memcpy (buf, (char *)file->data + file->device->net->offset, r);
f731ee
+      file->device->net->offset += r;
f731ee
+    }
f731ee
+
f731ee
+  return r;
f731ee
+}
f731ee
+
f731ee
+struct grub_efi_net_io io_http =
f731ee
+  {
f731ee
+    .configure = http_configure,
f731ee
+    .open = grub_efihttp_open,
f731ee
+    .read = grub_efihttp_read,
f731ee
+    .close = grub_efihttp_close
f731ee
+  };
f731ee
diff --git a/grub-core/net/efi/ip4_config.c b/grub-core/net/efi/ip4_config.c
f731ee
new file mode 100644
f731ee
index 00000000000..b711a5d9457
f731ee
--- /dev/null
f731ee
+++ b/grub-core/net/efi/ip4_config.c
f731ee
@@ -0,0 +1,398 @@
f731ee
+
f731ee
+#include <grub/efi/api.h>
f731ee
+#include <grub/efi/efi.h>
f731ee
+#include <grub/misc.h>
f731ee
+#include <grub/net/efi.h>
f731ee
+#include <grub/charset.h>
f731ee
+
f731ee
+char *
f731ee
+grub_efi_hw_address_to_string (grub_efi_uint32_t hw_address_size, grub_efi_mac_address_t hw_address)
f731ee
+{
f731ee
+  char *hw_addr, *p;
f731ee
+  int sz, s;
f731ee
+  int i;
f731ee
+
f731ee
+  sz = (int)hw_address_size * (sizeof ("XX:") - 1) + 1;
f731ee
+
f731ee
+  hw_addr = grub_malloc (sz);
f731ee
+  if (!hw_addr)
f731ee
+    return NULL;
f731ee
+
f731ee
+  p = hw_addr;
f731ee
+  s = sz;
f731ee
+  for (i = 0; i < (int)hw_address_size; i++)
f731ee
+    {
f731ee
+      grub_snprintf (p, sz, "%02x:", hw_address[i]);
f731ee
+      p +=  sizeof ("XX:") - 1;
f731ee
+      s -=  sizeof ("XX:") - 1;
f731ee
+    }
f731ee
+
f731ee
+  hw_addr[sz - 2] = '\0';
f731ee
+  return hw_addr;
f731ee
+}
f731ee
+
f731ee
+char *
f731ee
+grub_efi_ip4_address_to_string (grub_efi_ipv4_address_t *address)
f731ee
+{
f731ee
+  char *addr;
f731ee
+
f731ee
+  addr = grub_malloc (sizeof ("XXX.XXX.XXX.XXX"));
f731ee
+  if (!addr)
f731ee
+      return NULL;
f731ee
+
f731ee
+  /* FIXME: Use grub_xasprintf ? */
f731ee
+  grub_snprintf (addr,
f731ee
+	  sizeof ("XXX.XXX.XXX.XXX"),
f731ee
+	  "%u.%u.%u.%u",
f731ee
+	  (*address)[0],
f731ee
+	  (*address)[1],
f731ee
+	  (*address)[2],
f731ee
+	  (*address)[3]);
f731ee
+
f731ee
+  return addr;
f731ee
+}
f731ee
+
f731ee
+int
f731ee
+grub_efi_string_to_ip4_address (const char *val, grub_efi_ipv4_address_t *address, const char **rest)
f731ee
+{
f731ee
+  grub_uint32_t newip = 0;
f731ee
+  int i;
f731ee
+  const char *ptr = val;
f731ee
+
f731ee
+  for (i = 0; i < 4; i++)
f731ee
+    {
f731ee
+      unsigned long t;
f731ee
+      t = grub_strtoul (ptr, (char **) &ptr, 0);
f731ee
+      if (grub_errno)
f731ee
+	{
f731ee
+	  grub_errno = GRUB_ERR_NONE;
f731ee
+	  return 0;
f731ee
+	}
f731ee
+      if (*ptr != '.' && i == 0)
f731ee
+	{
f731ee
+	  /* XXX: t is in host byte order */
f731ee
+	  newip = t;
f731ee
+	  break;
f731ee
+	}
f731ee
+      if (t & ~0xff)
f731ee
+	return 0;
f731ee
+      newip <<= 8;
f731ee
+      newip |= t;
f731ee
+      if (i != 3 && *ptr != '.')
f731ee
+	return 0;
f731ee
+      ptr++;
f731ee
+    }
f731ee
+
f731ee
+  newip =  grub_cpu_to_be32 (newip);
f731ee
+
f731ee
+  grub_memcpy (address, &newip, sizeof(*address));
f731ee
+
f731ee
+  if (rest)
f731ee
+    *rest = (ptr - 1);
f731ee
+  return 1;
f731ee
+}
f731ee
+
f731ee
+static grub_efi_ip4_config2_interface_info_t *
f731ee
+efi_ip4_config_interface_info (grub_efi_ip4_config2_protocol_t *ip4_config)
f731ee
+{
f731ee
+  grub_efi_uintn_t sz;
f731ee
+  grub_efi_status_t status;
f731ee
+  grub_efi_ip4_config2_interface_info_t *interface_info;
f731ee
+
f731ee
+  sz = sizeof (*interface_info) + sizeof (*interface_info->route_table);
f731ee
+  interface_info = grub_malloc (sz);
f731ee
+  if (!interface_info)
f731ee
+    return NULL;
f731ee
+
f731ee
+  status = efi_call_4 (ip4_config->get_data, ip4_config,
f731ee
+		GRUB_EFI_IP4_CONFIG2_DATA_TYPE_INTERFACEINFO,
f731ee
+		&sz, interface_info);
f731ee
+
f731ee
+  if (status == GRUB_EFI_BUFFER_TOO_SMALL)
f731ee
+    {
f731ee
+      grub_free (interface_info);
f731ee
+      interface_info = grub_malloc (sz);
f731ee
+      status = efi_call_4 (ip4_config->get_data, ip4_config,
f731ee
+		    GRUB_EFI_IP4_CONFIG2_DATA_TYPE_INTERFACEINFO,
f731ee
+		    &sz, interface_info);
f731ee
+    }
f731ee
+
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    {
f731ee
+      grub_free (interface_info);
f731ee
+      return NULL;
f731ee
+    }
f731ee
+
f731ee
+  return interface_info;
f731ee
+}
f731ee
+
f731ee
+static grub_efi_ip4_config2_manual_address_t *
f731ee
+efi_ip4_config_manual_address (grub_efi_ip4_config2_protocol_t *ip4_config)
f731ee
+{
f731ee
+  grub_efi_uintn_t sz;
f731ee
+  grub_efi_status_t status;
f731ee
+  grub_efi_ip4_config2_manual_address_t *manual_address;
f731ee
+
f731ee
+  sz = sizeof (*manual_address);
f731ee
+  manual_address = grub_malloc (sz);
f731ee
+  if (!manual_address)
f731ee
+    return NULL;
f731ee
+
f731ee
+  status = efi_call_4 (ip4_config->get_data, ip4_config,
f731ee
+		    GRUB_EFI_IP4_CONFIG2_DATA_TYPE_MANUAL_ADDRESS,
f731ee
+		    &sz, manual_address);
f731ee
+
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    {
f731ee
+      grub_free (manual_address);
f731ee
+      return NULL;
f731ee
+    }
f731ee
+
f731ee
+  return manual_address;
f731ee
+}
f731ee
+
f731ee
+char *
f731ee
+grub_efi_ip4_interface_name (struct grub_efi_net_device *dev)
f731ee
+{
f731ee
+  grub_efi_ip4_config2_interface_info_t *interface_info;
f731ee
+  char *name;
f731ee
+
f731ee
+  interface_info = efi_ip4_config_interface_info (dev->ip4_config);
f731ee
+
f731ee
+  if (!interface_info)
f731ee
+    return NULL;
f731ee
+
f731ee
+  name = grub_malloc (GRUB_EFI_IP4_CONFIG2_INTERFACE_INFO_NAME_SIZE
f731ee
+		      * GRUB_MAX_UTF8_PER_UTF16 + 1);
f731ee
+  *grub_utf16_to_utf8 ((grub_uint8_t *)name, interface_info->name,
f731ee
+		      GRUB_EFI_IP4_CONFIG2_INTERFACE_INFO_NAME_SIZE) = 0;
f731ee
+  grub_free (interface_info);
f731ee
+  return name;
f731ee
+}
f731ee
+
f731ee
+static char *
f731ee
+grub_efi_ip4_interface_hw_address (struct grub_efi_net_device *dev)
f731ee
+{
f731ee
+  grub_efi_ip4_config2_interface_info_t *interface_info;
f731ee
+  char *hw_addr;
f731ee
+
f731ee
+  interface_info = efi_ip4_config_interface_info (dev->ip4_config);
f731ee
+
f731ee
+  if (!interface_info)
f731ee
+    return NULL;
f731ee
+
f731ee
+  hw_addr = grub_efi_hw_address_to_string (interface_info->hw_address_size, interface_info->hw_address);
f731ee
+  grub_free (interface_info);
f731ee
+
f731ee
+  return hw_addr;
f731ee
+}
f731ee
+
f731ee
+static char *
f731ee
+grub_efi_ip4_interface_address (struct grub_efi_net_device *dev)
f731ee
+{
f731ee
+  grub_efi_ip4_config2_manual_address_t *manual_address;
f731ee
+  char *addr;
f731ee
+
f731ee
+  manual_address = efi_ip4_config_manual_address (dev->ip4_config);
f731ee
+
f731ee
+  if (!manual_address)
f731ee
+    return NULL;
f731ee
+
f731ee
+  addr = grub_efi_ip4_address_to_string (&manual_address->address);
f731ee
+  grub_free (manual_address);
f731ee
+  return addr;
f731ee
+}
f731ee
+
f731ee
+
f731ee
+static int
f731ee
+address_mask_size (grub_efi_ipv4_address_t *address)
f731ee
+{
f731ee
+  grub_uint8_t i;
f731ee
+  grub_uint32_t u32_addr = grub_be_to_cpu32 (grub_get_unaligned32 (address));
f731ee
+
f731ee
+  if (u32_addr == 0)
f731ee
+    return 0;
f731ee
+
f731ee
+  for (i = 0; i < 32 ; ++i)
f731ee
+    {
f731ee
+      if (u32_addr == ((0xffffffff >> i) << i))
f731ee
+	return (32 - i);
f731ee
+    }
f731ee
+
f731ee
+  return -1;
f731ee
+}
f731ee
+
f731ee
+static char **
f731ee
+grub_efi_ip4_interface_route_table (struct grub_efi_net_device *dev)
f731ee
+{
f731ee
+  grub_efi_ip4_config2_interface_info_t *interface_info;
f731ee
+  char **ret;
f731ee
+  int i, id;
f731ee
+
f731ee
+  interface_info = efi_ip4_config_interface_info (dev->ip4_config);
f731ee
+  if (!interface_info)
f731ee
+    return NULL;
f731ee
+
f731ee
+  ret = grub_malloc (sizeof (*ret) * (interface_info->route_table_size + 1));
f731ee
+
f731ee
+  if (!ret)
f731ee
+    {
f731ee
+      grub_free (interface_info);
f731ee
+      return NULL;
f731ee
+    }
f731ee
+
f731ee
+  id = 0;
f731ee
+  for (i = 0; i < (int)interface_info->route_table_size; i++)
f731ee
+    {
f731ee
+      char *subnet, *gateway, *mask;
f731ee
+      grub_uint32_t u32_subnet, u32_gateway;
f731ee
+      int mask_size;
f731ee
+      grub_efi_ip4_route_table_t *route_table = interface_info->route_table + i;
f731ee
+      grub_efi_net_interface_t *inf;
f731ee
+      char *interface_name = NULL;
f731ee
+
f731ee
+      for (inf = dev->net_interfaces; inf; inf = inf->next)
f731ee
+	if (!inf->prefer_ip6)
f731ee
+	  interface_name = inf->name;
f731ee
+
f731ee
+      u32_gateway = grub_get_unaligned32 (&route_table->gateway_address);
f731ee
+      gateway = grub_efi_ip4_address_to_string (&route_table->gateway_address);
f731ee
+      u32_subnet = grub_get_unaligned32 (&route_table->subnet_address);
f731ee
+      subnet = grub_efi_ip4_address_to_string (&route_table->subnet_address);
f731ee
+      mask_size = address_mask_size (&route_table->subnet_mask);
f731ee
+      mask = grub_efi_ip4_address_to_string (&route_table->subnet_mask);
f731ee
+      if (u32_subnet && !u32_gateway && interface_name)
f731ee
+	ret[id++] = grub_xasprintf ("%s:local %s/%d %s", dev->card_name, subnet, mask_size, interface_name);
f731ee
+      else if (u32_subnet && u32_gateway)
f731ee
+	ret[id++] = grub_xasprintf ("%s:gw %s/%d gw %s", dev->card_name, subnet, mask_size, gateway);
f731ee
+      else if (!u32_subnet && u32_gateway)
f731ee
+	ret[id++] = grub_xasprintf ("%s:default %s/%d gw %s", dev->card_name, subnet, mask_size, gateway);
f731ee
+      grub_free (subnet);
f731ee
+      grub_free (gateway);
f731ee
+      grub_free (mask);
f731ee
+    }
f731ee
+
f731ee
+  ret[id] = NULL;
f731ee
+  grub_free (interface_info);
f731ee
+  return ret;
f731ee
+}
f731ee
+
f731ee
+static grub_efi_net_interface_t *
f731ee
+grub_efi_ip4_interface_match (struct grub_efi_net_device *dev, grub_efi_net_ip_address_t *ip_address)
f731ee
+{
f731ee
+  grub_efi_ip4_config2_interface_info_t *interface_info;
f731ee
+  grub_efi_net_interface_t *inf;
f731ee
+  int i;
f731ee
+  grub_efi_ipv4_address_t *address = &ip_address->ip4;
f731ee
+
f731ee
+  interface_info = efi_ip4_config_interface_info (dev->ip4_config);
f731ee
+  if (!interface_info)
f731ee
+    return NULL;
f731ee
+
f731ee
+  for (i = 0; i < (int)interface_info->route_table_size; i++)
f731ee
+    {
f731ee
+      grub_efi_ip4_route_table_t *route_table = interface_info->route_table + i;
f731ee
+      grub_uint32_t u32_address, u32_mask, u32_subnet;
f731ee
+
f731ee
+      u32_address = grub_get_unaligned32 (address);
f731ee
+      u32_subnet = grub_get_unaligned32 (route_table->subnet_address);
f731ee
+      u32_mask = grub_get_unaligned32 (route_table->subnet_mask);
f731ee
+
f731ee
+      /* SKIP Default GATEWAY */
f731ee
+      if (!u32_subnet && !u32_mask)
f731ee
+	continue;
f731ee
+
f731ee
+      if ((u32_address & u32_mask) == u32_subnet)
f731ee
+	{
f731ee
+	  for (inf = dev->net_interfaces; inf; inf = inf->next)
f731ee
+	    if (!inf->prefer_ip6)
f731ee
+	      {
f731ee
+		grub_free (interface_info);
f731ee
+		return inf;
f731ee
+	      }
f731ee
+	}
f731ee
+    }
f731ee
+
f731ee
+  grub_free (interface_info);
f731ee
+  return NULL;
f731ee
+}
f731ee
+
f731ee
+static int
f731ee
+grub_efi_ip4_interface_set_manual_address (struct grub_efi_net_device *dev,
f731ee
+	    grub_efi_net_ip_manual_address_t *net_ip,
f731ee
+	    int with_subnet)
f731ee
+{
f731ee
+  grub_efi_status_t status;
f731ee
+  grub_efi_ip4_config2_manual_address_t *address = &net_ip->ip4;
f731ee
+
f731ee
+  if (!with_subnet)
f731ee
+    {
f731ee
+      grub_efi_ip4_config2_manual_address_t *manual_address =
f731ee
+      efi_ip4_config_manual_address (dev->ip4_config);
f731ee
+
f731ee
+      if (manual_address)
f731ee
+	{
f731ee
+	  grub_memcpy (address->subnet_mask, manual_address->subnet_mask, sizeof(address->subnet_mask));
f731ee
+	  grub_free (manual_address);
f731ee
+	}
f731ee
+      else
f731ee
+	{
f731ee
+	  /* XXX: */
f731ee
+	  address->subnet_mask[0] = 0xff;
f731ee
+	  address->subnet_mask[1] = 0xff;
f731ee
+	  address->subnet_mask[2] = 0xff;
f731ee
+	  address->subnet_mask[3] = 0;
f731ee
+	}
f731ee
+    }
f731ee
+
f731ee
+  status = efi_call_4 (dev->ip4_config->set_data, dev->ip4_config,
f731ee
+		    GRUB_EFI_IP4_CONFIG2_DATA_TYPE_MANUAL_ADDRESS,
f731ee
+		    sizeof(*address), address);
f731ee
+
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    return 0;
f731ee
+
f731ee
+  return 1;
f731ee
+}
f731ee
+
f731ee
+static int
f731ee
+grub_efi_ip4_interface_set_gateway (struct grub_efi_net_device *dev,
f731ee
+	      grub_efi_net_ip_address_t *address)
f731ee
+{
f731ee
+  grub_efi_status_t status;
f731ee
+
f731ee
+  status = efi_call_4 (dev->ip4_config->set_data, dev->ip4_config,
f731ee
+		GRUB_EFI_IP4_CONFIG2_DATA_TYPE_GATEWAY,
f731ee
+		sizeof (address->ip4), &address->ip4);
f731ee
+
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    return 0;
f731ee
+  return 1;
f731ee
+}
f731ee
+
f731ee
+/* FIXME: Multiple DNS */
f731ee
+static int
f731ee
+grub_efi_ip4_interface_set_dns (struct grub_efi_net_device *dev,
f731ee
+	      grub_efi_net_ip_address_t *address)
f731ee
+{
f731ee
+  grub_efi_status_t status;
f731ee
+
f731ee
+  status = efi_call_4 (dev->ip4_config->set_data, dev->ip4_config,
f731ee
+		GRUB_EFI_IP4_CONFIG2_DATA_TYPE_DNSSERVER,
f731ee
+		sizeof (address->ip4), &address->ip4);
f731ee
+
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    return 0;
f731ee
+  return 1;
f731ee
+}
f731ee
+
f731ee
+grub_efi_net_ip_config_t *efi_net_ip4_config = &(grub_efi_net_ip_config_t)
f731ee
+  {
f731ee
+    .get_hw_address = grub_efi_ip4_interface_hw_address,
f731ee
+    .get_address = grub_efi_ip4_interface_address,
f731ee
+    .get_route_table = grub_efi_ip4_interface_route_table,
f731ee
+    .best_interface = grub_efi_ip4_interface_match,
f731ee
+    .set_address = grub_efi_ip4_interface_set_manual_address,
f731ee
+    .set_gateway = grub_efi_ip4_interface_set_gateway,
f731ee
+    .set_dns = grub_efi_ip4_interface_set_dns
f731ee
+  };
f731ee
diff --git a/grub-core/net/efi/ip6_config.c b/grub-core/net/efi/ip6_config.c
f731ee
new file mode 100644
f731ee
index 00000000000..017c4d05bc7
f731ee
--- /dev/null
f731ee
+++ b/grub-core/net/efi/ip6_config.c
f731ee
@@ -0,0 +1,422 @@
f731ee
+#include <grub/efi/api.h>
f731ee
+#include <grub/efi/efi.h>
f731ee
+#include <grub/misc.h>
f731ee
+#include <grub/net/efi.h>
f731ee
+#include <grub/charset.h>
f731ee
+
f731ee
+char *
f731ee
+grub_efi_ip6_address_to_string (grub_efi_pxe_ipv6_address_t *address)
f731ee
+{
f731ee
+  char *str = grub_malloc (sizeof ("XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX"));
f731ee
+  char *p;
f731ee
+  int i;
f731ee
+  int squash;
f731ee
+
f731ee
+  if (!str)
f731ee
+    return NULL;
f731ee
+
f731ee
+  p = str;
f731ee
+  squash = 0;
f731ee
+  for (i = 0; i < 8; ++i)
f731ee
+    {
f731ee
+      grub_uint16_t addr;
f731ee
+
f731ee
+      if (i == 7)
f731ee
+	squash = 2;
f731ee
+
f731ee
+      addr = grub_get_unaligned16 (address->addr + i * 2);
f731ee
+
f731ee
+      if (grub_be_to_cpu16 (addr))
f731ee
+	{
f731ee
+	  char buf[sizeof ("XXXX")];
f731ee
+	  if (i > 0)
f731ee
+	    *p++ = ':';
f731ee
+	  grub_snprintf (buf, sizeof (buf), "%x", grub_be_to_cpu16 (addr));
f731ee
+	  grub_strcpy (p, buf);
f731ee
+	  p += grub_strlen (buf);
f731ee
+
f731ee
+	  if (squash == 1)
f731ee
+	    squash = 2;
f731ee
+	}
f731ee
+      else
f731ee
+	{
f731ee
+	  if (squash == 0)
f731ee
+	    {
f731ee
+	      *p++ = ':';
f731ee
+	      squash = 1;
f731ee
+	    }
f731ee
+	  else if (squash == 2)
f731ee
+	    {
f731ee
+	      *p++ = ':';
f731ee
+	      *p++ = '0';
f731ee
+	    }
f731ee
+	}
f731ee
+    }
f731ee
+  *p = '\0';
f731ee
+  return str;
f731ee
+}
f731ee
+
f731ee
+int
f731ee
+grub_efi_string_to_ip6_address (const char *val, grub_efi_ipv6_address_t *address, const char **rest)
f731ee
+{
f731ee
+  grub_uint16_t newip[8];
f731ee
+  const char *ptr = val;
f731ee
+  int word, quaddot = -1;
f731ee
+  int bracketed = 0;
f731ee
+
f731ee
+  if (ptr[0] == '[') {
f731ee
+    bracketed = 1;
f731ee
+    ptr++;
f731ee
+  }
f731ee
+
f731ee
+  if (ptr[0] == ':' && ptr[1] != ':')
f731ee
+    return 0;
f731ee
+  if (ptr[0] == ':')
f731ee
+    ptr++;
f731ee
+
f731ee
+  for (word = 0; word < 8; word++)
f731ee
+    {
f731ee
+      unsigned long t;
f731ee
+      if (*ptr == ':')
f731ee
+	{
f731ee
+	  quaddot = word;
f731ee
+	  word--;
f731ee
+	  ptr++;
f731ee
+	  continue;
f731ee
+	}
f731ee
+      t = grub_strtoul (ptr, (char **) &ptr, 16);
f731ee
+      if (grub_errno)
f731ee
+	{
f731ee
+	  grub_errno = GRUB_ERR_NONE;
f731ee
+	  break;
f731ee
+	}
f731ee
+      if (t & ~0xffff)
f731ee
+	return 0;
f731ee
+      newip[word] = grub_cpu_to_be16 (t);
f731ee
+      if (*ptr != ':')
f731ee
+	break;
f731ee
+      ptr++;
f731ee
+    }
f731ee
+  if (quaddot == -1 && word < 7)
f731ee
+    return 0;
f731ee
+  if (quaddot != -1)
f731ee
+    {
f731ee
+      grub_memmove (&newip[quaddot + 7 - word], &newip[quaddot],
f731ee
+		    (word - quaddot + 1) * sizeof (newip[0]));
f731ee
+      grub_memset (&newip[quaddot], 0, (7 - word) * sizeof (newip[0]));
f731ee
+    }
f731ee
+  grub_memcpy (address, newip, 16);
f731ee
+  if (bracketed && *ptr == ']') {
f731ee
+    ptr++;
f731ee
+  }
f731ee
+  if (rest)
f731ee
+    *rest = ptr;
f731ee
+  return 1;
f731ee
+}
f731ee
+
f731ee
+static grub_efi_ip6_config_interface_info_t *
f731ee
+efi_ip6_config_interface_info (grub_efi_ip6_config_protocol_t *ip6_config)
f731ee
+{
f731ee
+  grub_efi_uintn_t sz;
f731ee
+  grub_efi_status_t status;
f731ee
+  grub_efi_ip6_config_interface_info_t *interface_info;
f731ee
+
f731ee
+  sz = sizeof (*interface_info) + sizeof (*interface_info->route_table);
f731ee
+  interface_info = grub_malloc (sz);
f731ee
+
f731ee
+  status = efi_call_4 (ip6_config->get_data, ip6_config,
f731ee
+		GRUB_EFI_IP6_CONFIG_DATA_TYPE_INTERFACEINFO,
f731ee
+		&sz, interface_info);
f731ee
+
f731ee
+  if (status == GRUB_EFI_BUFFER_TOO_SMALL)
f731ee
+    {
f731ee
+      grub_free (interface_info);
f731ee
+      interface_info = grub_malloc (sz);
f731ee
+      status = efi_call_4 (ip6_config->get_data, ip6_config,
f731ee
+		    GRUB_EFI_IP6_CONFIG_DATA_TYPE_INTERFACEINFO,
f731ee
+		    &sz, interface_info);
f731ee
+    }
f731ee
+
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    {
f731ee
+      grub_free (interface_info);
f731ee
+      return NULL;
f731ee
+    }
f731ee
+
f731ee
+  return interface_info;
f731ee
+}
f731ee
+
f731ee
+static grub_efi_ip6_config_manual_address_t *
f731ee
+efi_ip6_config_manual_address (grub_efi_ip6_config_protocol_t *ip6_config)
f731ee
+{
f731ee
+  grub_efi_uintn_t sz;
f731ee
+  grub_efi_status_t status;
f731ee
+  grub_efi_ip6_config_manual_address_t *manual_address;
f731ee
+
f731ee
+  sz = sizeof (*manual_address);
f731ee
+  manual_address = grub_malloc (sz);
f731ee
+  if (!manual_address)
f731ee
+    return NULL;
f731ee
+
f731ee
+  status = efi_call_4 (ip6_config->get_data, ip6_config,
f731ee
+		    GRUB_EFI_IP6_CONFIG_DATA_TYPE_MANUAL_ADDRESS,
f731ee
+		    &sz, manual_address);
f731ee
+
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    {
f731ee
+      grub_free (manual_address);
f731ee
+      return NULL;
f731ee
+    }
f731ee
+
f731ee
+  return manual_address;
f731ee
+}
f731ee
+
f731ee
+char *
f731ee
+grub_efi_ip6_interface_name (struct grub_efi_net_device *dev)
f731ee
+{
f731ee
+  grub_efi_ip6_config_interface_info_t *interface_info;
f731ee
+  char *name;
f731ee
+
f731ee
+  interface_info = efi_ip6_config_interface_info (dev->ip6_config);
f731ee
+
f731ee
+  if (!interface_info)
f731ee
+    return NULL;
f731ee
+
f731ee
+  name = grub_malloc (GRUB_EFI_IP4_CONFIG2_INTERFACE_INFO_NAME_SIZE
f731ee
+		      * GRUB_MAX_UTF8_PER_UTF16 + 1);
f731ee
+  *grub_utf16_to_utf8 ((grub_uint8_t *)name, interface_info->name,
f731ee
+		      GRUB_EFI_IP4_CONFIG2_INTERFACE_INFO_NAME_SIZE) = 0;
f731ee
+  grub_free (interface_info);
f731ee
+  return name;
f731ee
+}
f731ee
+
f731ee
+static char *
f731ee
+grub_efi_ip6_interface_hw_address (struct grub_efi_net_device *dev)
f731ee
+{
f731ee
+  grub_efi_ip6_config_interface_info_t *interface_info;
f731ee
+  char *hw_addr;
f731ee
+
f731ee
+  interface_info = efi_ip6_config_interface_info (dev->ip6_config);
f731ee
+
f731ee
+  if (!interface_info)
f731ee
+    return NULL;
f731ee
+
f731ee
+  hw_addr = grub_efi_hw_address_to_string (interface_info->hw_address_size, interface_info->hw_address);
f731ee
+  grub_free (interface_info);
f731ee
+
f731ee
+  return hw_addr;
f731ee
+}
f731ee
+
f731ee
+static char *
f731ee
+grub_efi_ip6_interface_address (struct grub_efi_net_device *dev)
f731ee
+{
f731ee
+  grub_efi_ip6_config_manual_address_t *manual_address;
f731ee
+  char *addr;
f731ee
+
f731ee
+  manual_address = efi_ip6_config_manual_address (dev->ip6_config);
f731ee
+
f731ee
+  if (!manual_address)
f731ee
+    return NULL;
f731ee
+
f731ee
+  addr = grub_efi_ip6_address_to_string ((grub_efi_pxe_ipv6_address_t *)&manual_address->address);
f731ee
+  grub_free (manual_address);
f731ee
+  return addr;
f731ee
+}
f731ee
+
f731ee
+static char **
f731ee
+grub_efi_ip6_interface_route_table (struct grub_efi_net_device *dev)
f731ee
+{
f731ee
+  grub_efi_ip6_config_interface_info_t *interface_info;
f731ee
+  char **ret;
f731ee
+  int i, id;
f731ee
+
f731ee
+  interface_info = efi_ip6_config_interface_info (dev->ip6_config);
f731ee
+  if (!interface_info)
f731ee
+    return NULL;
f731ee
+
f731ee
+  ret = grub_malloc (sizeof (*ret) * (interface_info->route_count + 1));
f731ee
+
f731ee
+  if (!ret)
f731ee
+    {
f731ee
+      grub_free (interface_info);
f731ee
+      return NULL;
f731ee
+    }
f731ee
+
f731ee
+  id = 0;
f731ee
+  for (i = 0; i < (int)interface_info->route_count ; i++)
f731ee
+    {
f731ee
+      char *gateway, *destination;
f731ee
+      grub_uint64_t u64_gateway[2];
f731ee
+      grub_uint64_t u64_destination[2];
f731ee
+      grub_efi_ip6_route_table_t *route_table = interface_info->route_table + i;
f731ee
+      grub_efi_net_interface_t *inf;
f731ee
+      char *interface_name = NULL;
f731ee
+
f731ee
+      gateway = grub_efi_ip6_address_to_string (&route_table->gateway);
f731ee
+      destination = grub_efi_ip6_address_to_string (&route_table->destination);
f731ee
+
f731ee
+      u64_gateway[0] = grub_get_unaligned64 (route_table->gateway.addr);
f731ee
+      u64_gateway[1] = grub_get_unaligned64 (route_table->gateway.addr + 8);
f731ee
+      u64_destination[0] = grub_get_unaligned64 (route_table->destination.addr);
f731ee
+      u64_destination[1] = grub_get_unaligned64 (route_table->destination.addr + 8);
f731ee
+
f731ee
+      for (inf = dev->net_interfaces; inf; inf = inf->next)
f731ee
+	if (inf->prefer_ip6)
f731ee
+	  interface_name = inf->name;
f731ee
+
f731ee
+      if ((!u64_gateway[0] && !u64_gateway[1])
f731ee
+	  && (u64_destination[0] || u64_destination[1]))
f731ee
+	{
f731ee
+	  if (interface_name)
f731ee
+	    {
f731ee
+	      if ((grub_be_to_cpu64 (u64_destination[0]) == 0xfe80000000000000ULL)
f731ee
+	      && (!u64_destination[1])
f731ee
+	      && (route_table->prefix_length == 64))
f731ee
+		ret[id++] = grub_xasprintf ("%s:link %s/%d %s", dev->card_name, destination, route_table->prefix_length, interface_name);
f731ee
+	      else
f731ee
+		ret[id++] = grub_xasprintf ("%s:local %s/%d %s", dev->card_name, destination, route_table->prefix_length, interface_name);
f731ee
+	    }
f731ee
+	}
f731ee
+      else if ((u64_gateway[0] || u64_gateway[1])
f731ee
+	  && (u64_destination[0] || u64_destination[1]))
f731ee
+	ret[id++] = grub_xasprintf ("%s:gw %s/%d gw %s", dev->card_name, destination, route_table->prefix_length, gateway);
f731ee
+      else if ((u64_gateway[0] || u64_gateway[1])
f731ee
+	  && (!u64_destination[0] && !u64_destination[1]))
f731ee
+	ret[id++] = grub_xasprintf ("%s:default %s/%d gw %s", dev->card_name, destination, route_table->prefix_length, gateway);
f731ee
+
f731ee
+      grub_free (gateway);
f731ee
+      grub_free (destination);
f731ee
+    }
f731ee
+
f731ee
+  ret[id] = NULL;
f731ee
+  grub_free (interface_info);
f731ee
+  return ret;
f731ee
+}
f731ee
+
f731ee
+static grub_efi_net_interface_t *
f731ee
+grub_efi_ip6_interface_match (struct grub_efi_net_device *dev, grub_efi_net_ip_address_t *ip_address)
f731ee
+{
f731ee
+  grub_efi_ip6_config_interface_info_t *interface_info;
f731ee
+  grub_efi_net_interface_t *inf;
f731ee
+  int i;
f731ee
+  grub_efi_ipv6_address_t *address = &ip_address->ip6;
f731ee
+
f731ee
+  interface_info = efi_ip6_config_interface_info (dev->ip6_config);
f731ee
+  if (!interface_info)
f731ee
+    return NULL;
f731ee
+
f731ee
+  for (i = 0; i < (int)interface_info->route_count ; i++)
f731ee
+    {
f731ee
+      grub_uint64_t u64_addr[2];
f731ee
+      grub_uint64_t u64_subnet[2];
f731ee
+      grub_uint64_t u64_mask[2];
f731ee
+
f731ee
+      grub_efi_ip6_route_table_t *route_table = interface_info->route_table + i;
f731ee
+
f731ee
+      /* SKIP Default GATEWAY */
f731ee
+      if (route_table->prefix_length == 0)
f731ee
+	continue;
f731ee
+
f731ee
+      u64_addr[0] = grub_get_unaligned64 (address);
f731ee
+      u64_addr[1] = grub_get_unaligned64 (address + 4);
f731ee
+      u64_subnet[0] = grub_get_unaligned64 (route_table->destination.addr);
f731ee
+      u64_subnet[1] = grub_get_unaligned64 (route_table->destination.addr + 8);
f731ee
+      u64_mask[0] = (route_table->prefix_length <= 64) ?
f731ee
+	    0xffffffffffffffffULL << (64 - route_table->prefix_length) :
f731ee
+	    0xffffffffffffffffULL;
f731ee
+      u64_mask[1] = (route_table->prefix_length <= 64) ?
f731ee
+	    0 :
f731ee
+	    0xffffffffffffffffULL << (128 - route_table->prefix_length);
f731ee
+
f731ee
+      if (((u64_addr[0] & u64_mask[0]) == u64_subnet[0])
f731ee
+	  && ((u64_addr[1] & u64_mask[1]) == u64_subnet[1]))
f731ee
+	{
f731ee
+	  for (inf = dev->net_interfaces; inf; inf = inf->next)
f731ee
+	    if (inf->prefer_ip6)
f731ee
+	      {
f731ee
+		grub_free (interface_info);
f731ee
+		return inf;
f731ee
+	      }
f731ee
+	}
f731ee
+    }
f731ee
+
f731ee
+  grub_free (interface_info);
f731ee
+  return NULL;
f731ee
+}
f731ee
+
f731ee
+static int
f731ee
+grub_efi_ip6_interface_set_manual_address (struct grub_efi_net_device *dev,
f731ee
+	    grub_efi_net_ip_manual_address_t *net_ip,
f731ee
+	    int with_subnet)
f731ee
+{
f731ee
+  grub_efi_status_t status;
f731ee
+  grub_efi_ip6_config_manual_address_t *address = &net_ip->ip6;
f731ee
+
f731ee
+  if (!with_subnet)
f731ee
+    {
f731ee
+      grub_efi_ip6_config_manual_address_t *manual_address =
f731ee
+      efi_ip6_config_manual_address (dev->ip6_config);
f731ee
+
f731ee
+      if (manual_address)
f731ee
+	{
f731ee
+	  address->prefix_length = manual_address->prefix_length;
f731ee
+	  grub_free (manual_address);
f731ee
+	}
f731ee
+      else
f731ee
+	{
f731ee
+	  /* XXX: */
f731ee
+	  address->prefix_length = 64;
f731ee
+	}
f731ee
+    }
f731ee
+
f731ee
+  status = efi_call_4 (dev->ip6_config->set_data, dev->ip6_config,
f731ee
+		    GRUB_EFI_IP6_CONFIG_DATA_TYPE_MANUAL_ADDRESS,
f731ee
+		    sizeof(*address), address);
f731ee
+
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    return 0;
f731ee
+
f731ee
+  return 1;
f731ee
+}
f731ee
+
f731ee
+static int
f731ee
+grub_efi_ip6_interface_set_gateway (struct grub_efi_net_device *dev,
f731ee
+	      grub_efi_net_ip_address_t *address)
f731ee
+{
f731ee
+  grub_efi_status_t status;
f731ee
+
f731ee
+  status = efi_call_4 (dev->ip6_config->set_data, dev->ip6_config,
f731ee
+		GRUB_EFI_IP6_CONFIG_DATA_TYPE_GATEWAY,
f731ee
+		sizeof (address->ip6), &address->ip6);
f731ee
+
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    return 0;
f731ee
+  return 1;
f731ee
+}
f731ee
+
f731ee
+static int
f731ee
+grub_efi_ip6_interface_set_dns (struct grub_efi_net_device *dev,
f731ee
+	      grub_efi_net_ip_address_t *address)
f731ee
+{
f731ee
+
f731ee
+  grub_efi_status_t status;
f731ee
+
f731ee
+  status = efi_call_4 (dev->ip6_config->set_data, dev->ip6_config,
f731ee
+		GRUB_EFI_IP6_CONFIG_DATA_TYPE_DNSSERVER,
f731ee
+		sizeof (address->ip6), &address->ip6);
f731ee
+
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    return 0;
f731ee
+  return 1;
f731ee
+}
f731ee
+
f731ee
+grub_efi_net_ip_config_t *efi_net_ip6_config = &(grub_efi_net_ip_config_t)
f731ee
+  {
f731ee
+    .get_hw_address = grub_efi_ip6_interface_hw_address,
f731ee
+    .get_address = grub_efi_ip6_interface_address,
f731ee
+    .get_route_table = grub_efi_ip6_interface_route_table,
f731ee
+    .best_interface = grub_efi_ip6_interface_match,
f731ee
+    .set_address = grub_efi_ip6_interface_set_manual_address,
f731ee
+    .set_gateway = grub_efi_ip6_interface_set_gateway,
f731ee
+    .set_dns = grub_efi_ip6_interface_set_dns
f731ee
+  };
f731ee
diff --git a/grub-core/net/efi/net.c b/grub-core/net/efi/net.c
f731ee
new file mode 100644
f731ee
index 00000000000..9e0078ac1c6
f731ee
--- /dev/null
f731ee
+++ b/grub-core/net/efi/net.c
f731ee
@@ -0,0 +1,1428 @@
f731ee
+#include <grub/net.h>
f731ee
+#include <grub/env.h>
f731ee
+#include <grub/mm.h>
f731ee
+#include <grub/misc.h>
f731ee
+#include <grub/dl.h>
f731ee
+#include <grub/command.h>
f731ee
+#include <grub/efi/api.h>
f731ee
+#include <grub/efi/efi.h>
f731ee
+#include <grub/i18n.h>
f731ee
+#include <grub/bufio.h>