All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] efinet: correct closing of SNP protocol
@ 2021-11-29 15:00 Heinrich Schuchardt
  2021-11-29 15:00 ` [PATCH v2 1/2] " Heinrich Schuchardt
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Heinrich Schuchardt @ 2021-11-29 15:00 UTC (permalink / raw)
  To: Daniel Kiper
  Cc: Andrei Borzenkov, Vladimir 'phcoder' Serbinenko,
	Nikita Ermakov, Ard Biesheuvel, Atish Patra, Leif Lindholm,
	Andreas Schwab, GRUB development mailing list,
	Heinrich Schuchardt

In the context of the implementation of the EFI_LOAD_FILE2_PROTOCOL for
the initial ramdisk it was observed that opening the SNP protocol failed
(https://lists.gnu.org/archive/html/grub-devel/2021-10/msg00020.html).
This is due to an incorrect call to CloseProtocol().

This is corrected in the first patch.

The second patch provides a new function grub_efi_close_protocol() to
simplify the coding.

v2:
	do not open SNP protocol non-exclusively
	adjust code style

Heinrich Schuchardt (2):
  efinet: correct closing of SNP protocol
  efi: library function grub_efi_close_protocol()

 grub-core/kern/efi/efi.c           | 12 ++++++++++++
 grub-core/net/drivers/efi/efinet.c | 29 +++++++++++++++++++----------
 include/grub/efi/efi.h             |  3 +++
 3 files changed, 34 insertions(+), 10 deletions(-)

-- 
2.32.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] efinet: correct closing of SNP protocol
  2021-11-29 15:00 [PATCH v2 0/2] efinet: correct closing of SNP protocol Heinrich Schuchardt
@ 2021-11-29 15:00 ` Heinrich Schuchardt
  2021-11-29 15:00 ` [PATCH v2 2/2] efi: library function grub_efi_close_protocol() Heinrich Schuchardt
  2021-11-30 17:05 ` [PATCH v2 0/2] efinet: correct closing of SNP protocol Daniel Kiper
  2 siblings, 0 replies; 4+ messages in thread
From: Heinrich Schuchardt @ 2021-11-29 15:00 UTC (permalink / raw)
  To: Daniel Kiper
  Cc: Andrei Borzenkov, Vladimir 'phcoder' Serbinenko,
	Nikita Ermakov, Ard Biesheuvel, Atish Patra, Leif Lindholm,
	Andreas Schwab, GRUB development mailing list,
	Heinrich Schuchardt

In the context of the implementation of the EFI_LOAD_FILE2_PROTOCOL for
the initial ramdisk it was observed that opening the SNP protocol failed.
https://lists.gnu.org/archive/html/grub-devel/2021-10/msg00020.html
This is due to an incorrect call to CloseProtocol().

The first parameter of CloseProtocol() is the handle, not the interface.

We call OpenProtocol() with ControllerHandle = NULL. Hence we must also
call CloseProtcol with ControllerHandel = NULL.

Each call of OpenProtocol() for the same network card handle is expected to
return the same interface pointer. If we want to close the protocol which
we opened non-exclusively when searching for a card, we have to do this
before opening the protocol exclusively.

As there is no guarantee that we successfully open the protocol add checks
in the transmit and receive functions.

Reported-by: Andreas Schwab <schwab@linux-m68k.org>
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
---
v2:
	do not open SNP protocol non-exclusively
	adjust code style
---
 grub-core/net/drivers/efi/efinet.c | 31 +++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)

diff --git a/grub-core/net/drivers/efi/efinet.c b/grub-core/net/drivers/efi/efinet.c
index 5388f952b..bcd812ff5 100644
--- a/grub-core/net/drivers/efi/efinet.c
+++ b/grub-core/net/drivers/efi/efinet.c
@@ -39,6 +39,9 @@ send_card_buffer (struct grub_net_card *dev,
   grub_uint64_t limit_time = grub_get_time_ms () + 4000;
   void *txbuf;
 
+  if (net == NULL)
+    return grub_error (GRUB_ERR_IO,
+		       N_("network protocol not available, can't send packet"));
   if (dev->txbusy)
     while (1)
       {
@@ -101,6 +104,9 @@ get_card_packet (struct grub_net_card *dev)
   struct grub_net_buff *nb;
   int i;
 
+  if (net == NULL)
+    return NULL;
+
   for (i = 0; i < 2; i++)
     {
       if (!dev->rcvbuf)
@@ -148,12 +154,20 @@ open_card (struct grub_net_card *dev)
 {
   grub_efi_simple_network_t *net;
 
-  /* Try to reopen SNP exlusively to close any active MNP protocol instance
-     that may compete for packet polling
+  if (dev->efi_net != NULL)
+    {
+      efi_call_4 (grub_efi_system_table->boot_services->close_protocol,
+		  dev->efi_handle, &net_io_guid,
+		  grub_efi_image_handle, NULL);
+      dev->efi_net = NULL;
+    }
+  /*
+   * Try to reopen SNP exlusively to close any active MNP protocol instance
+   * that may compete for packet polling
    */
   net = grub_efi_open_protocol (dev->efi_handle, &net_io_guid,
 				GRUB_EFI_OPEN_PROTOCOL_BY_EXCLUSIVE);
-  if (net)
+  if (net != NULL)
     {
       if (net->mode->state == GRUB_EFI_NETWORK_STOPPED
 	  && efi_call_1 (net->start, net) != GRUB_EFI_SUCCESS)
@@ -192,13 +206,12 @@ open_card (struct grub_net_card *dev)
 	  efi_call_6 (net->receive_filters, net, filters, 0, 0, 0, NULL);
 	}
 
-      efi_call_4 (grub_efi_system_table->boot_services->close_protocol,
-		  dev->efi_net, &net_io_guid,
-		  grub_efi_image_handle, dev->efi_handle);
       dev->efi_net = net;
+    } else {
+      return grub_error (GRUB_ERR_NET_NO_CARD, "%s: can't open protocol",
+			 dev->name);
     }
 
-  /* If it failed we just try to run as best as we can */
   return GRUB_ERR_NONE;
 }
 
@@ -208,8 +221,8 @@ close_card (struct grub_net_card *dev)
   efi_call_1 (dev->efi_net->shutdown, dev->efi_net);
   efi_call_1 (dev->efi_net->stop, dev->efi_net);
   efi_call_4 (grub_efi_system_table->boot_services->close_protocol,
-	      dev->efi_net, &net_io_guid,
-	      grub_efi_image_handle, dev->efi_handle);
+	      dev->efi_handle, &net_io_guid,
+	      grub_efi_image_handle, 0);
 }
 
 static struct grub_net_card_driver efidriver =
-- 
2.32.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] efi: library function grub_efi_close_protocol()
  2021-11-29 15:00 [PATCH v2 0/2] efinet: correct closing of SNP protocol Heinrich Schuchardt
  2021-11-29 15:00 ` [PATCH v2 1/2] " Heinrich Schuchardt
@ 2021-11-29 15:00 ` Heinrich Schuchardt
  2021-11-30 17:05 ` [PATCH v2 0/2] efinet: correct closing of SNP protocol Daniel Kiper
  2 siblings, 0 replies; 4+ messages in thread
From: Heinrich Schuchardt @ 2021-11-29 15:00 UTC (permalink / raw)
  To: Daniel Kiper
  Cc: Andrei Borzenkov, Vladimir 'phcoder' Serbinenko,
	Nikita Ermakov, Ard Biesheuvel, Atish Patra, Leif Lindholm,
	Andreas Schwab, GRUB development mailing list,
	Heinrich Schuchardt

Create a library function for CloseProtocol() and use it for the SNP
driver.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
---
v2:
	adjust code style
---
 grub-core/kern/efi/efi.c           | 12 ++++++++++++
 grub-core/net/drivers/efi/efinet.c |  8 ++------
 include/grub/efi/efi.h             |  3 +++
 3 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
index 8cff7be02..d4268d062 100644
--- a/grub-core/kern/efi/efi.c
+++ b/grub-core/kern/efi/efi.c
@@ -117,6 +117,18 @@ grub_efi_open_protocol (grub_efi_handle_t handle,
   return interface;
 }
 
+grub_efi_status_t
+grub_efi_close_protocol (grub_efi_handle_t handle, grub_efi_guid_t *protocol)
+{
+  grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
+  grub_efi_status_t status;
+
+  status = efi_call_4 (b->close_protocol, handle, protocol,
+		       grub_efi_image_handle, NULL);
+
+  return status;
+}
+
 int
 grub_efi_set_text_mode (int on)
 {
diff --git a/grub-core/net/drivers/efi/efinet.c b/grub-core/net/drivers/efi/efinet.c
index bcd812ff5..186a8a03c 100644
--- a/grub-core/net/drivers/efi/efinet.c
+++ b/grub-core/net/drivers/efi/efinet.c
@@ -156,9 +156,7 @@ open_card (struct grub_net_card *dev)
 
   if (dev->efi_net != NULL)
     {
-      efi_call_4 (grub_efi_system_table->boot_services->close_protocol,
-		  dev->efi_handle, &net_io_guid,
-		  grub_efi_image_handle, NULL);
+      grub_efi_close_protocol (dev->efi_handle, &net_io_guid);
       dev->efi_net = NULL;
     }
   /*
@@ -220,9 +218,7 @@ close_card (struct grub_net_card *dev)
 {
   efi_call_1 (dev->efi_net->shutdown, dev->efi_net);
   efi_call_1 (dev->efi_net->stop, dev->efi_net);
-  efi_call_4 (grub_efi_system_table->boot_services->close_protocol,
-	      dev->efi_handle, &net_io_guid,
-	      grub_efi_image_handle, 0);
+  grub_efi_close_protocol (dev->efi_handle, &net_io_guid);
 }
 
 static struct grub_net_card_driver efidriver =
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
index 83d958f99..12d041f2e 100644
--- a/include/grub/efi/efi.h
+++ b/include/grub/efi/efi.h
@@ -35,6 +35,9 @@ EXPORT_FUNC(grub_efi_locate_handle) (grub_efi_locate_search_type_t search_type,
 void *EXPORT_FUNC(grub_efi_open_protocol) (grub_efi_handle_t handle,
 					   grub_efi_guid_t *protocol,
 					   grub_efi_uint32_t attributes);
+grub_efi_status_t
+EXPORT_FUNC(grub_efi_close_protocol) (grub_efi_handle_t handle,
+				      grub_efi_guid_t *protocol);
 int EXPORT_FUNC(grub_efi_set_text_mode) (int on);
 void EXPORT_FUNC(grub_efi_stall) (grub_efi_uintn_t microseconds);
 void *
-- 
2.32.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 0/2] efinet: correct closing of SNP protocol
  2021-11-29 15:00 [PATCH v2 0/2] efinet: correct closing of SNP protocol Heinrich Schuchardt
  2021-11-29 15:00 ` [PATCH v2 1/2] " Heinrich Schuchardt
  2021-11-29 15:00 ` [PATCH v2 2/2] efi: library function grub_efi_close_protocol() Heinrich Schuchardt
@ 2021-11-30 17:05 ` Daniel Kiper
  2 siblings, 0 replies; 4+ messages in thread
From: Daniel Kiper @ 2021-11-30 17:05 UTC (permalink / raw)
  To: Heinrich Schuchardt
  Cc: Andrei Borzenkov, Vladimir 'phcoder' Serbinenko,
	Nikita Ermakov, Ard Biesheuvel, Atish Patra, Leif Lindholm,
	Andreas Schwab, GRUB development mailing list

On Mon, Nov 29, 2021 at 04:00:27PM +0100, Heinrich Schuchardt wrote:
> In the context of the implementation of the EFI_LOAD_FILE2_PROTOCOL for
> the initial ramdisk it was observed that opening the SNP protocol failed
> (https://lists.gnu.org/archive/html/grub-devel/2021-10/msg00020.html).
> This is due to an incorrect call to CloseProtocol().
>
> This is corrected in the first patch.
>
> The second patch provides a new function grub_efi_close_protocol() to
> simplify the coding.
>
> v2:
> 	do not open SNP protocol non-exclusively
> 	adjust code style
>
> Heinrich Schuchardt (2):
>   efinet: correct closing of SNP protocol
>   efi: library function grub_efi_close_protocol()

For both Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>

Thank you for fixing this issue.

Daniel


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-11-30 17:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-29 15:00 [PATCH v2 0/2] efinet: correct closing of SNP protocol Heinrich Schuchardt
2021-11-29 15:00 ` [PATCH v2 1/2] " Heinrich Schuchardt
2021-11-29 15:00 ` [PATCH v2 2/2] efi: library function grub_efi_close_protocol() Heinrich Schuchardt
2021-11-30 17:05 ` [PATCH v2 0/2] efinet: correct closing of SNP protocol Daniel Kiper

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.