From 5494d47944aad4608df08e2d5f73a9569f81b0e1 Mon Sep 17 00:00:00 2001
From: Fedora Ninjas <grub2-owner@fedoraproject.org>
Date: Sat, 13 Jul 2019 10:41:34 +0000
Subject: [PATCH] huawei-revert-0067-Be-more-aggro.patch

---
 grub-core/net/drivers/efi/efinet.c | 125 ++++++-----------------------
 include/grub/efi/api.h             |   4 -
 2 files changed, 26 insertions(+), 103 deletions(-)

diff --git a/grub-core/net/drivers/efi/efinet.c b/grub-core/net/drivers/efi/efinet.c
index 56305f4..2076aeb 100644
--- a/grub-core/net/drivers/efi/efinet.c
+++ b/grub-core/net/drivers/efi/efinet.c
@@ -247,85 +247,46 @@ grub_efinet_get_device_handle (struct grub_net_card *card)
   return card->efi_handle;
 }
 
-static int
-grub_efinet_find_snp_cards (int preferred_only, grub_efi_handle_t preferred,
-			    int *i)
+static void
+grub_efinet_findcards (void)
 {
-  grub_efi_uintn_t num_handles = 0;
-  grub_efi_handle_t *handles = NULL;
+  grub_efi_uintn_t num_handles;
+  grub_efi_handle_t *handles;
   grub_efi_handle_t *handle;
-  grub_efi_device_path_t *pdp = NULL, *pp = NULL, *pc = NULL;
-  int ret = 0;
-
-  if (preferred)
-    {
-      grub_efi_device_path_t *pdpc;
-      pdpc = pdp = grub_efi_get_device_path (preferred);
-      if (pdp == NULL)
-	{
-	  grub_print_error ();
-	  return -1;
-	}
-
-      for (; ! GRUB_EFI_END_ENTIRE_DEVICE_PATH (pdpc);
-	   pdpc = GRUB_EFI_NEXT_DEVICE_PATH (pdpc))
-	{
-	  pp = pc;
-	  pc = pdpc;
-	}
-    }
+  int i = 0;
 
-  /* Find handles which support the SNP interface.  */
+  /* Find handles which support the disk io interface.  */
   handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &net_io_guid,
 				    0, &num_handles);
-
-  for (handle = handles; handle && num_handles--; handle++)
+  if (! handles)
+    return;
+  for (handle = handles; num_handles--; handle++)
     {
       grub_efi_simple_network_t *net;
       struct grub_net_card *card;
       grub_efi_device_path_t *dp, *parent = NULL, *child = NULL;
 
-      /* if we're looking for only the preferred handle, skip anything that
-	 isn't it. */
-      if (preferred_only && preferred != NULL && *handle != preferred)
-	continue;
-
-      /* if we're not looking for the preferred handle, skip it if it's
-	 found. */
-      if (!preferred_only && *handle == preferred)
-	continue;
-
+      /* EDK2 UEFI PXE driver creates IPv4 and IPv6 messaging devices as
+	 children of main MAC messaging device. We only need one device with
+	 bound SNP per physical card, otherwise they compete with each other
+	 when polling for incoming packets.
+       */
       dp = grub_efi_get_device_path (*handle);
       if (!dp)
 	continue;
-
-      for (; ! GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp);
-	   dp = GRUB_EFI_NEXT_DEVICE_PATH (dp))
+      for (; ! GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp); dp = GRUB_EFI_NEXT_DEVICE_PATH (dp))
 	{
 	  parent = child;
 	  child = dp;
 	}
-
-      if (!preferred_only)
-	{
-	  if (pp && pc
-	      && grub_efi_compare_device_paths (pp, parent) == 0
-	      && grub_efi_compare_device_paths (pc, child) == 0)
-	    continue;
-
-	  if (child
-	      && (GRUB_EFI_DEVICE_PATH_IS_TYPE(child,
-					  GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE,
-					  GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE) ||
-		  GRUB_EFI_DEVICE_PATH_IS_TYPE(child,
-					  GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE,
-					  GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE))
-	      && parent
-	      && (GRUB_EFI_DEVICE_PATH_IS_TYPE(parent,
-				    GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE,
-				    GRUB_EFI_MAC_ADDRESS_DEVICE_PATH_SUBTYPE)))
-	    continue;
-	}
+      if (child
+	  && GRUB_EFI_DEVICE_PATH_TYPE (child) == GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
+	  && (GRUB_EFI_DEVICE_PATH_SUBTYPE (child) == GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE
+	      || GRUB_EFI_DEVICE_PATH_SUBTYPE (child) == GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE)
+	  && parent
+	  && GRUB_EFI_DEVICE_PATH_TYPE (parent) == GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
+	  && GRUB_EFI_DEVICE_PATH_SUBTYPE (parent) == GRUB_EFI_MAC_ADDRESS_DEVICE_PATH_SUBTYPE)
+	continue;
 
       net = grub_efi_open_protocol (*handle, &net_io_guid,
 				    GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
@@ -352,7 +313,7 @@ grub_efinet_find_snp_cards (int preferred_only, grub_efi_handle_t preferred,
 	{
 	  grub_print_error ();
 	  grub_free (handles);
-	  return -1;
+	  return;
 	}
 
       card->mtu = net->mode->max_packet_size;
@@ -363,14 +324,13 @@ grub_efinet_find_snp_cards (int preferred_only, grub_efi_handle_t preferred,
 	  grub_print_error ();
 	  grub_free (handles);
 	  grub_free (card);
-	  return -1;
+	  return;
 	}
       card->txbusy = 0;
 
       card->rcvbufsize = ALIGN_UP (card->mtu, 64) + 256;
 
-      card->name = grub_xasprintf ("efinet%d", *i);
-      *i = (*i)+1;
+      card->name = grub_xasprintf ("efinet%d", i++);
       card->driver = &efidriver;
       card->flags = 0;
       card->default_address.type = net->mode->if_type;
@@ -382,38 +342,8 @@ grub_efinet_find_snp_cards (int preferred_only, grub_efi_handle_t preferred,
       card->efi_handle = *handle;
 
       grub_net_card_register (card);
-      ret++;
     }
   grub_free (handles);
-
-  return ret;
-}
-
-static void
-grub_efinet_findcards (void)
-{
-  grub_efi_loaded_image_t *image = NULL;
-  int rc;
-  int efinet_number = 0;
-
-  image = grub_efi_get_loaded_image (grub_efi_image_handle);
-
-  if (image && image->device_handle)
-    {
-      rc = grub_efinet_find_snp_cards (1, image->device_handle, &efinet_number);
-      if (rc < 0)
-	return;
-
-      rc = grub_efinet_find_snp_cards (0, image->device_handle, &efinet_number);
-      if (rc < 0)
-	return;
-    }
-  else
-    {
-      rc = grub_efinet_find_snp_cards (0, NULL, &efinet_number);
-      if (rc < 0)
-	return;
-    }
 }
 
 static grub_efi_handle_t
@@ -833,9 +763,6 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
     if (card->driver != &efidriver)
       continue;
 
-    if (hnd != card->efi_handle)
-      continue;
-
     cdp = grub_efi_get_device_path (card->efi_handle);
     if (! cdp)
       continue;
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
index 5f62a2d..b337e1a 100644
--- a/include/grub/efi/api.h
+++ b/include/grub/efi/api.h
@@ -663,10 +663,6 @@ typedef struct grub_efi_device_path grub_efi_device_path_t;
    It seems to be identical to EFI_DEVICE_PATH.  */
 typedef struct grub_efi_device_path grub_efi_device_path_protocol_t;
 
-#define GRUB_EFI_DEVICE_PATH_IS_TYPE(dp, type, subtype) \
-	((GRUB_EFI_DEVICE_PATH_TYPE(dp) == (type)) && \
-	 (GRUB_EFI_DEVICE_PATH_SUBTYPE(dp) == (subtype)))
-
 #define GRUB_EFI_DEVICE_PATH_TYPE(dp)		((dp)->type & 0x7f)
 #define GRUB_EFI_DEVICE_PATH_SUBTYPE(dp)	((dp)->subtype)
 #define GRUB_EFI_DEVICE_PATH_LENGTH(dp)		((dp)->length)
-- 
2.19.1

