Skip to contentSkip to conversation

Re-order grub_serial_find to fix default to com0 on some platforms

PATCH In review Adam Williamson 3 participants 3 messages Oct 16, 2024 – Oct 24, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Re-order grub_serial_find to fix default to com0 on some platforms

7b192ec4c rejigged the serial port detection code when no port is explicitly specified. Before 7b192ec4c we did grub_serial_find ("com0") in this case, which on *any* platform would return a port called "com0" if one was found. 7b192ec4c changed this so we do grub_serial_find ("auto"), and added a block to handle the "auto" string which tries to detect a port from the SPCR by calling grub_ns8250_spcr_init (), then falls through to com0 if that doesn't work.

However, that block was placed such that it was wrapped in two ifdefs that limit its scope. This means that we would no longer default to com0 unless the requirements of *both* ifdefs are met.

To fix this, move the handling of "auto" up to happen first, before the ifdef around "port" handling kicks in. If we don't find an SPCR, change the name to "com0", so it will be handled by the existing "exact match by name" block that comes next, just as it was before 7b192ec4c. In the handling of "auto", have the ifdef that is meant to block grub_ns8250_spcr_init on inappropriate platforms *only* wrap that call, not *also* wrap the fallback to com0.

grub-core/term/serial.c +13 -14
index 8260dcb7a..8f37afd5c 100644
@@ -145,8 +145,20 @@ grub_serial_find (const char *name)
 {
   struct grub_serial_port *port;
 
+  if (grub_strcmp (name, "auto") == 0)
+    {
+#if (defined(__i386__) || defined(__x86_64__)) && !defined(GRUB_MACHINE_IEEE1275) && !defined(GRUB_MACHINE_QEMU)
+      /* Look for an SPCR if any. If not, default to com0. */
+      port = grub_ns8250_spcr_init ();
+      if (port != NULL)
+        return port;
+#endif
+      /* Legacy default, will be handled by next step */
+      name = "com0";
+    }
+
   /*
-   * First look for an exact match by name, this will take care of
+   * Look for an exact match by name, this will take care of
    * things like "com0" which have already been created and that
    * this function cannot re-create.
    */
@@ -209,19 +221,6 @@ grub_serial_find (const char *name)
       if (port != NULL)
         return port;
     }
-
-#if (defined(__i386__) || defined(__x86_64__)) && !defined(GRUB_MACHINE_IEEE1275) && !defined(GRUB_MACHINE_QEMU)
-  if (grub_strcmp (name, "auto") == 0)
-    {
-      /* Look for an SPCR if any. If not, default to com0. */
-      port = grub_ns8250_spcr_init ();
-      if (port != NULL)
-        return port;
-      FOR_SERIAL_PORTS (port)
-        if (grub_strcmp (port->name, "com0") == 0)
-          return port;
-    }
-#endif
 #endif
 
 #ifdef GRUB_MACHINE_IEEE1275
Signature · 10 lines
Adam Williamson (he/him/his)
Fedora QA
Fedora Chat: @adamwill:fedora.im | Mastodon: @[email protected]
https://www.happyassassin.net


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v2 1/1
#1 lore raw

On Wed, 2024-10-16 at 10:09 -0700, Adam Williamson wrote:

.../...

Quoted 8 lines

To fix this, move the handling of "auto" up to happen first, before the ifdef around "port" handling kicks in. If we don't find an SPCR, change the name to "com0", so it will be handled by the existing "exact match by name" block that comes next, just as it was before 7b192ec4c. In the handling of "auto", have the ifdef that is meant to block grub_ns8250_spcr_init on inappropriate platforms *only* wrap that call, not *also* wrap the fallback to com0.

This also nicely avoid duplicating the loop...

Quoted 1 line

I wrote the original bug so:

Quoted 44 lines
grub-core/term/serial.c +0 -0
index 8260dcb7a..8f37afd5c 100644
@@ -145,8 +145,20 @@ grub_serial_find (const char *name)
  {
    struct grub_serial_port *port;


+  if (grub_strcmp (name, "auto") == 0)
+    {

grub-core/term/serial.c +10 -0
+#if (defined(__i386__) || defined(__x86_64__)) && !defined(GRUB_MACHINE_IEEE1275) && !defined(GRUB_MACHINE_QEMU)
+      /* Look for an SPCR if any. If not, default to com0. */
+      port = grub_ns8250_spcr_init ();
+      if (port != NULL)
+        return port;
+#endif
+      /* Legacy default, will be handled by next step */
+      name = "com0";
+    }
+

   /*
-   * First look for an exact match by name, this will take care of +   * Look for an exact match by name, this will take care of
    * things like "com0" which have already been created and that
    * this function cannot re-create.
    */

grub-core/term/serial.c +0 -13
@@ -209,19 +221,6 @@ grub_serial_find (const char *name)
        if (port != NULL)
          return port;
      }
-
-#if (defined(__i386__) || defined(__x86_64__)) && !defined(GRUB_MACHINE_IEEE1275) && !defined(GRUB_MACHINE_QEMU)
-  if (grub_strcmp (name, "auto") == 0)
-    {
-      /* Look for an SPCR if any. If not, default to com0. */
-      port = grub_ns8250_spcr_init ();
-      if (port != NULL)
-        return port;
-      FOR_SERIAL_PORTS (port)
-        if (grub_strcmp (port->name, "com0") == 0)
-          return port;
-    }
-#endif
  #endif
Quoted 2 lines


 #ifdef GRUB_MACHINE_IEEE1275

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#2 lore raw
In reply to Adam Williamson
Quoted 22 lines · Adam Williamson

7b192ec4c rejigged the serial port detection code when no port is explicitly specified. Before 7b192ec4c we did grub_serial_find ("com0") in this case, which on *any* platform would return a port called "com0" if one was found. 7b192ec4c changed this so we do grub_serial_find ("auto"), and added a block to handle the "auto" string which tries to detect a port from the SPCR by calling grub_ns8250_spcr_init (), then falls through to com0 if that doesn't work.

However, that block was placed such that it was wrapped in two ifdefs that limit its scope. This means that we would no longer default to com0 unless the requirements of *both* ifdefs are met.

To fix this, move the handling of "auto" up to happen first, before the ifdef around "port" handling kicks in. If we don't find an SPCR, change the name to "com0", so it will be handled by the existing "exact match by name" block that comes next, just as it was before 7b192ec4c. In the handling of "auto", have the ifdef that is meant to block grub_ns8250_spcr_init on inappropriate platforms *only* wrap that call, not *also* wrap the fallback to com0.

Quoted 1 line

It seems to me this patch is not based on latest upstream master. Please rebase and resend...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel