Skip to contentSkip to conversation

Add efitextmode command

PATCH Superseded Glenn Washburn 2 participants 7 messages May 13, 2022 – Jul 12, 2022

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add efitextmode command

Updates since v1:
* Update documentation language as suggested by Paul

This patch add the efitextmode command which is used for showing all available text output modes and setting a specific mode. Its basically the equivalent of the EFI Shell's "mode" command and its output looks similar. The main difference is that its shows the mode number in the listing and takes a mode number when setting the mode. As a convenience, it can take arguments "min" or "max" to set to the minimum or maximum mode.

Glenn

Glenn Washburn (2):
efi: Add efitextmode command for getting/setting the text mode
resolution
docs: Document efitextmode command

 docs/grub.texi                       |  26 ++++++
 grub-core/Makefile.core.def          |   6 ++
 grub-core/commands/efi/efitextmode.c | 118 +++++++++++++++++++++++++++
 3 files changed, 150 insertions(+)
 create mode 100644 grub-core/commands/efi/efitextmode.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/2 efi: Add efitextmode command for getting/setting the text mode resolution

This command is meant to behave similarly to the 'mode' command of the EFI Shell application. One difference is that to set the mode the mode number is given, not the rows and columns of the desired mode. Also supported are the arguments "min" and "max", which set the mode to the minimum and maximum mode respectively as calculated by the columns * rows of that mode.

---
 grub-core/Makefile.core.def          |   6 ++
 grub-core/commands/efi/efitextmode.c | 118 +++++++++++++++++++++++++++
 2 files changed, 124 insertions(+)
 create mode 100644 grub-core/commands/efi/efitextmode.c
grub-core/Makefile.core.def +6 -0
index 726f51be7..b22e48f0f 100644
@@ -813,6 +813,12 @@ module = {
   enable = efi;
 };
 
+module = {
+  name = efitextmode;
+  efi = commands/efi/efitextmode.c;
+  enable = efi;
+};
+
 module = {
   name = blocklist;
   common = commands/blocklist.c;
grub-core/commands/efi/efitextmode.c new file +118 -0

118 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v2 1/2] efi: Add efitextmode command for getting/setting the text mode resolution

Quoted 38 lines · Glenn Washburn

This command is meant to behave similarly to the 'mode' command of the EFI Shell application. One difference is that to set the mode the mode number is given, not the rows and columns of the desired mode. Also supported are the arguments "min" and "max", which set the mode to the minimum and maximum mode respectively as calculated by the columns * rows of that mode.

---
 grub-core/Makefile.core.def          |   6 ++
 grub-core/commands/efi/efitextmode.c | 118 +++++++++++++++++++++++++++
 2 files changed, 124 insertions(+)
 create mode 100644 grub-core/commands/efi/efitextmode.c
grub-core/Makefile.core.def +6 -0
index 726f51be7..b22e48f0f 100644
@@ -813,6 +813,12 @@ module = {
   enable = efi;
 };
 
+module = {
+  name = efitextmode;
+  efi = commands/efi/efitextmode.c;
+  enable = efi;
+};
+
 module = {
   name = blocklist;
   common = commands/blocklist.c;
grub-core/commands/efi/efitextmode.c new file +0 -0
new file mode 100644
index 000000000..fb72aa6f3

grub-core/commands/efi/efitextmode.c line 4

Quoted 4 lines
grub-core/commands/efi/efitextmode.c new file +4 -0
@@ -0,0 +1,118 @@
+/* efitextmode.c - command to get/set text mode resolution */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2003,2005,2006,2007,2009  Free Software Foundation, Inc.

s/2003,2005,2006,2007,2009/2022/

grub-core/commands/efi/efitextmode.c line 17

Quoted 12 lines
grub-core/commands/efi/efitextmode.c new file +12 -0
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.

Please add one line here describing what this module does. Good example is in grub-core/kern/efi/sb.c.

Quoted 17 lines
grub-core/commands/efi/efitextmode.c new file +17 -0
+ */
+
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/command.h>
+#include <grub/i18n.h>
+#include <grub/efi/efi.h>
+#include <grub/efi/api.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static grub_err_t
+grub_cmd_efitextmode (grub_command_t cmd __attribute__ ((unused)),
+		      int argc, char **args)
+{
+  grub_efi_simple_text_output_interface_t *o;

grub-core/commands/efi/efitextmode.c line 44

Quoted 10 lines
grub-core/commands/efi/efitextmode.c new file +10 -0
+  unsigned long mode;
+  const char *p = NULL;
+  grub_efi_status_t status;
+  grub_efi_uintn_t columns, rows;
+  grub_efi_int32_t i;
+
+  if (argc > 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("at most one argument expected"));
+
+  o = grub_efi_system_table->con_out;

I think you can do it in definition above.

grub-core/commands/efi/efitextmode.c line 51

Quoted 7 lines
grub-core/commands/efi/efitextmode.c new file +7 -0
+
+  if (argc == 1)
+    {
+      if (grub_strcmp (args[0], "min") == 0)
+	mode = 0;
+      else if (grub_strcmp (args[0], "max") == 0)
+	mode = o->mode->max_mode - 1;

Are your sure "o" and "o->mode" are always not NULL?

grub-core/commands/efi/efitextmode.c line 57

Quoted 6 lines
grub-core/commands/efi/efitextmode.c new file +6 -0
+      else
+	{
+	  mode = grub_strtoul (args[0], &p, 0);
+
+	  if (grub_errno != GRUB_ERR_NONE)
+	    return grub_errno;

Please drop this check. It is redundant as we chatted earlier.

Quoted 7 lines
grub-core/commands/efi/efitextmode.c new file +7 -0
+	  if (*args[0] == '\0' || *p != '\0')
+	    return grub_error (GRUB_ERR_BAD_ARGUMENT,
+			       N_("non-numeric or invalid mode `%s'"),
+			       args[0]);
+	}
+
+      if (mode < (unsigned long) o->mode->max_mode)

grub-core/commands/efi/efitextmode.c line 75

Quoted 10 lines
grub-core/commands/efi/efitextmode.c new file +10 -0
+	{
+	  if (mode != (unsigned long) o->mode->mode)
+	    {
+	      status = efi_call_2 (o->set_mode, o, (grub_efi_int32_t) mode);
+	      if (status == GRUB_EFI_SUCCESS)
+		;
+	      else if (status == GRUB_EFI_DEVICE_ERROR)
+		return grub_error (GRUB_ERR_BAD_DEVICE,
+				   N_("device error: could not set requested"
+				      " mode"));

Please do not wrap error msg here. I am OK with lines a bit longer than 80 chars. And I think you can put args[0] in one line with error msg above too.

grub-core/commands/efi/efitextmode.c line 88

Quoted 13 lines
grub-core/commands/efi/efitextmode.c new file +13 -0
+	      else if (status == GRUB_EFI_UNSUPPORTED)
+		return grub_error (GRUB_ERR_OUT_OF_RANGE,
+				   N_("invalid mode: number not valid"));
+	      else
+		return grub_error (GRUB_ERR_BAD_OS,
+				   N_("unexpected EFI error number: `%u'"),
+				   (unsigned) status);
+	    }
+	}
+      else
+	return grub_error (GRUB_ERR_BAD_ARGUMENT,
+			   N_("invalid mode: `%lu' is greater than maximum"
+			      " mode `%lu'"),

Ditto.

grub-core/commands/efi/efitextmode.c line 96

Quoted 8 lines
grub-core/commands/efi/efitextmode.c new file +8 -0
+			   mode, (unsigned long) o->mode->max_mode);
+    }
+
+  if (argc == 0)
+    {
+      grub_printf_ (N_("Available modes for console output device.\n"));
+
+      for (i=0; i < o->mode->max_mode; i++)

i = 0; Missing spaces...

grub-core/commands/efi/efitextmode.c line 100

Quoted 4 lines
grub-core/commands/efi/efitextmode.c new file +4 -0
+	if (GRUB_EFI_SUCCESS == efi_call_4 (o->query_mode, o, i,
+					    &columns, &rows))
+	  grub_printf_ (N_(" [%lu]  Col %5u Row %5u %c\n"),
+			(unsigned long) i, (unsigned) columns, (unsigned) rows,

Could we avoid this casts? Same above. I think we can introduce PRIxGRUB_EFI_UINT32_T, etc. They should land in include/grub/efi/api.h somewhere around grub_efi_uint*_t typedefs.

grub-core/commands/efi/efitextmode.c line 108

Quoted 8 lines
grub-core/commands/efi/efitextmode.c new file +8 -0
+			(i == o->mode->mode) ? '*' : ' ');
+    }
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_command_t cmd;
+

Please drop this ^L.

grub-core/commands/efi/efitextmode.c line 109

Quoted 1 line
grub-core/commands/efi/efitextmode.c new file +1 -0
+GRUB_MOD_INIT(cmp)

GRUB_MOD_INIT (cmp)

grub-core/commands/efi/efitextmode.c line 115

Quoted 6 lines
grub-core/commands/efi/efitextmode.c new file +6 -0
+{
+  cmd = grub_register_command ("efitextmode", grub_cmd_efitextmode,
+			       N_("[min|max|mode_num]"), N_("Get or set EFI text mode."));
+}
+
+GRUB_MOD_FINI(cmp)

GRUB_MOD_FINI (cmp)

grub-core/commands/efi/efitextmode.c line 118

Quoted 3 lines
grub-core/commands/efi/efitextmode.c new file +3 -0
+{
+  grub_unregister_command (cmd);
+}

Daniel

#3 lore raw

PATCH v2 2/2 docs: Document efitextmode command

In reply to Glenn Washburn
---
 docs/grub.texi | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
docs/grub.texi +26 -0
index 5de94d062..178957096 100644
@@ -4049,6 +4049,7 @@ you forget a command, you can run the command @command{help}
 * distrust::                    Remove a pubkey from trusted keys
 * drivemap::                    Map a drive to another
 * echo::                        Display a line of text
+* efitextmode::                 Set/Get text output mode resolution
 * eval::                        Evaluate agruments as GRUB commands
 * export::                      Export an environment variable
 * false::                       Do nothing, unsuccessfully
@@ -4505,6 +4506,31 @@ character will print that character.
 @end deffn
 
 
+@node efitextmode
+@subsection efitextmode
+
+@deffn Command efitextmode [min | max | mode_num]
+When used with no arguments displays all available text output modes. The
+set mode determines the columns and rows of the text display when in
+text mode. An asterisk, @samp{*}, will be at the end of the line of the
+currently set mode.
+
+Otherwise the command only takes a single parameter, which can be
+@samp{min}, @samp{max}, or a mode number given by the listing when run
+with no arguments. These arguments set the mode to the minimum, maximum,
+and particular mode respectively.
+
+By default GRUB will start in whatever mode the EFI firmware defaults to.
+There are firmwares known to set up the default mode such that output
+behaves strangely, for example the cursor in the grub shell never reaches
+the bottom of the screen or, when typing characters at the prompt,
+characters from previous command output are overwritten. Setting the mode
+may fix this.
+
+Note: This command is only available on EFI platforms.
+@end deffn
+
+
 @node eval
 @subsection eval
 
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v2 2/2] docs: Document efitextmode command

Quoted 1 line · Glenn Washburn

I think this patch should be merged with patch #1.

Quoted 17 lines
---
 docs/grub.texi | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
docs/grub.texi +1 -0
index 5de94d062..178957096 100644
@@ -4049,6 +4049,7 @@ you forget a command, you can run the command @command{help}
 * distrust::                    Remove a pubkey from trusted keys
 * drivemap::                    Map a drive to another
 * echo::                        Display a line of text
+* efitextmode::                 Set/Get text output mode resolution
 * eval::                        Evaluate agruments as GRUB commands
 * export::                      Export an environment variable
 * false::                       Do nothing, unsuccessfully

docs/grub.texi character will print that character. · line 4512

Quoted 7 lines
docs/grub.texi +4 -0
@@ -4505,6 +4506,31 @@ character will print that character.
 @end deffn
 
 
+@node efitextmode
+@subsection efitextmode
+
+@deffn Command efitextmode [min | max | mode_num]

s/mode_num/<mode_num>/?

After some thinking it seems to me this interface is not very convenient. My guess is that the same mode may have different <mode_num> on different implementations/platforms. I think EFI shell "mode" command "col" "row" approach is more universal/reliable. Though I think it makes sense to leave min and max as is...

Quoted 8 lines
docs/grub.texi +8 -0
+When used with no arguments displays all available text output modes. The
+set mode determines the columns and rows of the text display when in
+text mode. An asterisk, @samp{*}, will be at the end of the line of the
+currently set mode.
+
+Otherwise the command only takes a single parameter, which can be
+@samp{min}, @samp{max}, or a mode number given by the listing when run
+with no arguments. These arguments set the mode to the minimum, maximum,

docs/grub.texi character will print that character. · line 4530

Quoted 10 lines
docs/grub.texi +10 -0
+and particular mode respectively.
+
+By default GRUB will start in whatever mode the EFI firmware defaults to.
+There are firmwares known to set up the default mode such that output
+behaves strangely, for example the cursor in the grub shell never reaches
+the bottom of the screen or, when typing characters at the prompt,
+characters from previous command output are overwritten. Setting the mode
+may fix this.
+
+Note: This command is only available on EFI platforms.

s/./ and is similar to EFI shell "mode" command./?

Daniel

#5 lore raw

Re: [PATCH v2 2/2] docs: Document efitextmode command

Quoted 32 lines · On Tue, 5 Jul 2022 16:01:04 +0200 Daniel Kiper <[email protected]> wrote:
Quoted 1 line · Glenn Washburn

I think this patch should be merged with patch #1.

Quoted 17 lines
---
 docs/grub.texi | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
docs/grub.texi +1 -0
index 5de94d062..178957096 100644
@@ -4049,6 +4049,7 @@ you forget a command, you can run the command @command{help}
 * distrust::                    Remove a pubkey from trusted keys
 * drivemap::                    Map a drive to another
 * echo::                        Display a line of text
+* efitextmode::                 Set/Get text output mode resolution
 * eval::                        Evaluate agruments as GRUB commands
 * export::                      Export an environment variable
 * false::                       Do nothing, unsuccessfully

docs/grub.texi character will print that character. · line 4512

Quoted 7 lines
docs/grub.texi +4 -0
@@ -4505,6 +4506,31 @@ character will print that character.
 @end deffn
 
 
+@node efitextmode
+@subsection efitextmode
+
+@deffn Command efitextmode [min | max | mode_num]

s/mode_num/<mode_num>/?

Quoted 5 lines

After some thinking it seems to me this interface is not very convenient. My guess is that the same mode may have different <mode_num> on different implementations/platforms. I think EFI shell "mode" command "col" "row" approach is more universal/reliable. Though I think it makes sense to leave min and max as is...

Hmm, not convenient for who? in what scenario? Its more convenient for someone at a grub prompt and wanting to set the mode (type less numbers and when switching between modes easier to go to the next higher or lower mode). It was more convenient for someone to remember which mode they wanted when they want to put it in a grub config, which can take a while because the OS generally needs to boot to edit the config.

The only way I can see specifying cols and rows as convenient is for someone who wants to make sure that they are at a specific textmode resolution. But what is the use case for that? Its also convenient for someone reading a grub config file and wanting to know what resolution is being requested (which may not be the resolution you get, if its not supported). But again, who needs to do that? And this can be remedied by a comment in the config file if desired.

How about keeping the mode number usage if one argument is supplied and adding the cols/rows usage when two numbers are supplied?

Glenn

Quoted 22 lines
Quoted 8 lines
docs/grub.texi +8 -0
+When used with no arguments displays all available text output modes. The
+set mode determines the columns and rows of the text display when in
+text mode. An asterisk, @samp{*}, will be at the end of the line of the
+currently set mode.
+
+Otherwise the command only takes a single parameter, which can be
+@samp{min}, @samp{max}, or a mode number given by the listing when run
+with no arguments. These arguments set the mode to the minimum, maximum,

docs/grub.texi character will print that character. · line 4530

Quoted 10 lines
docs/grub.texi +10 -0
+and particular mode respectively.
+
+By default GRUB will start in whatever mode the EFI firmware defaults to.
+There are firmwares known to set up the default mode such that output
+behaves strangely, for example the cursor in the grub shell never reaches
+the bottom of the screen or, when typing characters at the prompt,
+characters from previous command output are overwritten. Setting the mode
+may fix this.
+
+Note: This command is only available on EFI platforms.

s/./ and is similar to EFI shell "mode" command./?

Daniel

#6 lore raw

Re: [PATCH v2 2/2] docs: Document efitextmode command

Quoted 48 lines · Glenn Washburn
Quoted 32 lines · On Tue, 5 Jul 2022 16:01:04 +0200 Daniel Kiper <[email protected]> wrote:
Quoted 1 line · Glenn Washburn

I think this patch should be merged with patch #1.

Quoted 17 lines
---
 docs/grub.texi | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
docs/grub.texi +1 -0
index 5de94d062..178957096 100644
@@ -4049,6 +4049,7 @@ you forget a command, you can run the command @command{help}
 * distrust::                    Remove a pubkey from trusted keys
 * drivemap::                    Map a drive to another
 * echo::                        Display a line of text
+* efitextmode::                 Set/Get text output mode resolution
 * eval::                        Evaluate agruments as GRUB commands
 * export::                      Export an environment variable
 * false::                       Do nothing, unsuccessfully

docs/grub.texi character will print that character. · line 4512

Quoted 7 lines
docs/grub.texi +4 -0
@@ -4505,6 +4506,31 @@ character will print that character.
 @end deffn
 
 
+@node efitextmode
+@subsection efitextmode
+
+@deffn Command efitextmode [min | max | mode_num]

s/mode_num/<mode_num>/?

Quoted 5 lines

After some thinking it seems to me this interface is not very convenient. My guess is that the same mode may have different <mode_num> on different implementations/platforms. I think EFI shell "mode" command "col" "row" approach is more universal/reliable. Though I think it makes sense to leave min and max as is...

Hmm, not convenient for who? in what scenario? Its more convenient for someone at a grub prompt and wanting to set the mode (type less numbers and when switching between modes easier to go to the next higher or lower mode). It was more convenient for someone to remember which mode they wanted when they want to put it in a grub config, which can take a while because the OS generally needs to boot to edit the config.

Quoted 7 lines

The only way I can see specifying cols and rows as convenient is for someone who wants to make sure that they are at a specific textmode resolution. But what is the use case for that? Its also convenient for someone reading a grub config file and wanting to know what resolution is being requested (which may not be the resolution you get, if its not supported). But again, who needs to do that? And this can be remedied by a comment in the config file if desired.

There is no guarantee the mode at index A is BxC across all machines. So, if somebody wants mode BxC on every machine then they should have a chance to specify it explicitly using "col" "row" arguments.

Quoted 2 lines

How about keeping the mode number usage if one argument is supplied and adding the cols/rows usage when two numbers are supplied?

I am OK with it. However, please add a note to the docs saying nobody can rely on that an index A corresponds to BxC mode on every machine or something like that.

Daniel