git: f28dff43ad62 - main - stand/efi/eficom: better handling of absent device

From: Warner Losh <imp_at_FreeBSD.org>
Date: Thu, 25 May 2023 04:34:02 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=f28dff43ad62ee7396abde1f8449ead1ece98053

commit f28dff43ad62ee7396abde1f8449ead1ece98053
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2023-05-24 22:34:31 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-05-25 04:33:53 +0000

    stand/efi/eficom: better handling of absent device
    
    Don't even have a comc_port when the port doesn't exist: always free it
    if we probe that it's not there. Also, when it's not present, clear the
    flags indicating presence to avoid disturbing other flags.
    
    Sponsored by:           Netflix
    Reviewed by:            tsoome, kevans
    Differential Revision:  https://reviews.freebsd.org/D40220
---
 stand/efi/libefi/eficom.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/stand/efi/libefi/eficom.c b/stand/efi/libefi/eficom.c
index b1c84399a05d..ca08458c2ca2 100644
--- a/stand/efi/libefi/eficom.c
+++ b/stand/efi/libefi/eficom.c
@@ -332,9 +332,12 @@ comc_probe(struct console *sc)
 	env_setenv("efi_com_speed", EV_VOLATILE, value,
 	    comc_speed_set, env_nounset);
 
-	eficom.c_flags = 0;
 	if (comc_setup()) {
 		sc->c_flags = C_PRESENTIN | C_PRESENTOUT;
+	} else {
+		sc->c_flags &= ~(C_PRESENTIN | C_PRESENTOUT);
+		free(comc_port);
+		comc_port = NULL;
 	}
 }
 
@@ -349,14 +352,20 @@ comc_probe_compat(struct console *sc)
 }
 #endif
 
+/*
+ * Called when the console is selected in cons_change. If we didn't detect the
+ * device, comc_port will be NULL, and comc_setup will fail. It may be called
+ * even when the device isn't present as a 'fallback' console or when listed
+ * specifically in console env, so we have to reset the c_flags in those case to
+ * say it's not present.
+ */
 static int
 comc_init(int arg __unused)
 {
-
 	if (comc_setup())
 		return (CMD_OK);
 
-	eficom.c_flags = 0;
+	eficom.c_flags &= ~(C_ACTIVEIN | C_ACTIVEOUT);
 	return (CMD_ERROR);
 }
 
@@ -516,8 +525,10 @@ comc_setup(void)
 	EFI_STATUS status;
 	char *ev;
 
-	/* port is not usable */
-	if (comc_port->sio == NULL)
+	/*
+	 * If the device isn't active, or there's no port present.
+	 */
+	if ((eficom.c_flags & (C_ACTIVEIN | C_ACTIVEOUT)) == 0 || comc_port == NULL)
 		return (false);
 
 	if (comc_port->sio->Reset != NULL) {