Skip to contentSkip to conversation

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

PATCH Superseded Adam Williamson 2 participants 3 messages Oct 15, 2024 – Oct 16, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

NOTE, NOT PART OF COMMIT MESSAGE: I am not at all used to the email- based workflow, I don't typically contribute to projects that use it, I am used to pull requests. I don't have the git email stuff set up at all. This email is manually produced by copy/pasting the 'git show' output. Sorry if this causes any problems. I'm also not subscribed to this list, so please CC me on any replies. I noticed this apparent issue while investigating
https://bugzilla.redhat.com/show_bug.cgi?id=2317048 . Original commit message and patch follow.

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, otherwise it fell through to the later blocks about ns8250 and ieee1275. 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 much later than a call with "com0" would be handled, and 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 right after the exact port name match (and before the mips/x86/EMU/ARC ifdef kicks in), and change the ifdef that is meant to block use of grub_ns8250_spcr_init on inappropriate platforms to *only* wrap the call to grub_ns8250_spcr_init, not *also* wrap the fallback to com0.

grub-core/term/serial.c +14 -13
index 8260dcb7a..d9b39d5da 100644
@@ -154,6 +154,20 @@ grub_serial_find (const char *name)
     if (grub_strcmp (port->name, name) == 0)
       return 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. */
+      port = grub_ns8250_spcr_init ();
+      if (port != NULL)
+        return port;
+#endif
+      /* If not, default to com0.*/
+      FOR_SERIAL_PORTS (port)
+        if (grub_strcmp (port->name, "com0") == 0)
+          return port;
+    }
+
 #if (defined(__mips__) || defined (__i386__) || defined (__x86_64__)) && !defined(GRUB_MACHINE_EMU) && !defined(GRUB_MACHINE_ARC)
   if (grub_strncmp (name, "port", sizeof ("port") - 1) == 0
       && grub_isxdigit (name [sizeof ("port") - 1]))
@@ -209,19 +223,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 · 13 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
#1 lore raw
Quoted 16 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, otherwise it fell through to the later blocks about ns8250 and ieee1275. 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 much later than a call with "com0" would be handled, and 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.

Quoted 6 lines

To fix this, move the handling of "auto" up to happen right after the exact port name match (and before the mips/x86/EMU/ARC ifdef kicks in), and change the ifdef that is meant to block use of grub_ns8250_spcr_init on inappropriate platforms to *only* wrap the call to grub_ns8250_spcr_init, not *also* wrap the fallback to com0.

I am not entirely sure what the "moving up" changes in practice. The various top-level if () blocks in grub_serial_find() all operates on the name.

You are basically moving the

if (name is "auto")

From after to before the

if (name starts with "port")

I don't think that in itself has any impact or changes anything other than being moved outside of the confines of the ifdef. So I assume that's what you are after.

Correct me if I'm wrong but the only "issue" here is that if you are on a target for which:

#if (defined(__i386__) || defined(__x86_64__)) && !defined(GRUB_MACHINE_IEEE1275) && !defined(GRUB_MACHINE_QEMU)

Isn't true, then the new "default" of "auto" will fail to match an existing port in the list called "com0" right ?

In which case a simpler patch would have been maybe to just add to the very top of the function insteadinstead

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 */
      name = "com0";
    }

Don't you think ?

Cheers,
Ben.

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

#2 lore raw
Quoted 40 lines · Benjamin Herrenschmidt
Quoted 16 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, otherwise it fell through to the later blocks about ns8250 and ieee1275. 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 much later than a call with "com0" would be handled, and 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.

Quoted 6 lines

To fix this, move the handling of "auto" up to happen right after the exact port name match (and before the mips/x86/EMU/ARC ifdef kicks in), and change the ifdef that is meant to block use of grub_ns8250_spcr_init on inappropriate platforms to *only* wrap the call to grub_ns8250_spcr_init, not *also* wrap the fallback to com0.

I am not entirely sure what the "moving up" changes in practice. The various top-level if () blocks in grub_serial_find() all operates on the name.

You are basically moving the

if (name is "auto")

From after to before the

if (name starts with "port")

I don't think that in itself has any impact or changes anything other than being moved outside of the confines of the ifdef. So I assume that's what you are after.

Quoted 7 lines

Correct me if I'm wrong but the only "issue" here is that if you are on a target for which:

#if (defined(__i386__) || defined(__x86_64__)) && !defined(GRUB_MACHINE_IEEE1275) && !defined(GRUB_MACHINE_QEMU)

Isn't true, then the new "default" of "auto" will fail to match an existing port in the list called "com0" right ?

Exactly, yeah. I moved it up to get it out of that ifdef.

Quoted 15 lines

In which case a simpler patch would have been maybe to just add to the very top of the function insteadinstead

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 */
      name = "com0";
    }
Quoted 1 line

Don't you think ?

Yeah, I actually thought about something along those lines later this afternoon after I'd sent my patch - using the same FOR_SERIAL_PORTS loop for both cases seems elegant. I agree with that idea.

Signature · 13 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