Skip to contentSkip to conversation

Add fwconfig command

PATCH In review Matthew Garrett 2 participants 2 messages Jan 24, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add fwconfig command

Add a command to read values from the qemu fwcfg store. This allows data to be passed from the qemu command line to grub.

Example use:

echo '(hd0,1)' >rootdev
qemu -fw_cfg opt/rootdev,file=rootdev

fwconfig opt/rootdev root

---
 docs/grub.texi                |   6 +++
 grub-core/Makefile.core.def   |   6 +++
 grub-core/commands/fwconfig.c | 121 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 133 insertions(+)
 create mode 100644 grub-core/commands/fwconfig.c
docs/grub.texi +6 -0
index 4469638..4f8a378 100644
@@ -3818,6 +3818,7 @@ you forget a command, you can run the command @command{help}
 * eval::                        Evaluate agruments as GRUB commands
 * export::                      Export an environment variable
 * false::                       Do nothing, unsuccessfully
+* fwconfig::                    Retrieves a value from the qemu fwcfg store
 * getenv::                      Retrieve an EFI firmware variable
 * gettext::                     Translate a string
 * gptsync::                     Fill an MBR based on GPT entries
@@ -4259,6 +4260,11 @@ Do nothing, unsuccessfully.  This is mainly useful in control constructs
 such as @code{if} and @code{while} (@pxref{Shell-like scripting}).
 @end deffn
 
+@node fwconfig
+@subsection fwconig
+@deffn Command fwconfig fwpath envvar
+Retrieves @var{fwpath} from the qemu fwcfg store and stores it in @var{envvar}
+
 @node getenv
 @subsection getenv
 
grub-core/Makefile.core.def +6 -0
index db77a7f..f6b6f38 100644
@@ -2362,3 +2362,9 @@ module = {
   common = loader/i386/xen_file64.c;
   extra_dist = loader/i386/xen_fileXX.c;
 };
+
+module = {
+  name = fwconfig;
+  common = commands/fwconfig.c;
+  enable = x86;
+};
grub-core/commands/fwconfig.c new file +121 -0

121 lines not on this page · raw message

Signature · 1 line
2.9.3
#1 lore raw
Quoted 9 lines · Matthew Garrett

Add a command to read values from the qemu fwcfg store. This allows data to be passed from the qemu command line to grub.

Example use:

echo '(hd0,1)' >rootdev
qemu -fw_cfg opt/rootdev,file=rootdev

fwconfig opt/rootdev root

I think that this should be made clear that fwconfig command should be executed in GRUB not in shell. At least I understand it in that way.

[...]

Quoted 7 lines
grub-core/commands/fwconfig.c new file +7 -0
+static grub_err_t
+grub_cmd_fwconfig (grub_extcmd_context_t ctxt, int argc, char **argv)
+{
+  grub_uint32_t i, j, value = 0;
+  struct grub_qemu_fwcfgfile file;
+  char fwsig[4], signature[4] = { 'Q', 'E', 'M', 'U' };
+

grub-core/commands/fwconfig.c line 68

Quoted 10 lines
grub-core/commands/fwconfig.c new file +10 -0
+  if (argc != 2)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
+
+  /* Verify that we have meaningful hardware here */
+  grub_outw(SIGNATURE_INDEX, SELECTOR);
+  for (i=0; i<sizeof(fwsig); i++)
+      fwsig[i] = grub_inb(DATA);
+
+  if (grub_memcmp(fwsig, signature, sizeof(signature)) != 0)
+    return grub_error (GRUB_ERR_BAD_DEVICE, N_("invalid fwconfig hardware signature: got 0x%x%x%x%x"), fwsig[0], fwsig[1], fwsig[2], fwsig[3]);

Too long line. Could you wrap it?

grub-core/commands/fwconfig.c line 72

Quoted 4 lines
grub-core/commands/fwconfig.c new file +4 -0
+
+  /* Find out how many file entries we have */
+  grub_outw(DIRECTORY_INDEX, SELECTOR);
+  value = grub_inb(DATA) | grub_inb(DATA) << 8 | grub_inb(DATA) << 16 | grub_inb(DATA) << 24;

Ditto.

grub-core/commands/fwconfig.c line 81

Quoted 9 lines
grub-core/commands/fwconfig.c new file +9 -0
+  value = grub_be_to_cpu32(value);
+  /* Read the file description for each file */
+  for (i=0; i<value; i++)
+    {
+      for (j=0; j<sizeof(file); j++)
+	{
+	  ((char *)&file)[j] = grub_inb(DATA);
+	}
+      /* Check whether it matches what we're looking for, and if so read the file */

Ditto.

grub-core/commands/fwconfig.c line 89

Quoted 8 lines
grub-core/commands/fwconfig.c new file +8 -0
+      if (grub_strncmp(file.name, argv[0], sizeof(file.name)) == 0)
+	{
+	  grub_uint32_t filesize = grub_be_to_cpu32(file.size);
+	  grub_uint16_t location = grub_be_to_cpu16(file.select);
+	  char *data = grub_malloc(filesize+1);
+
+	  if (!data)
+	      return grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate buffer for data"));

Ditto.

Quoted 7 lines
grub-core/commands/fwconfig.c new file +7 -0
+
+	  grub_outw(location, SELECTOR);
+	  for (j=0; j<filesize; j++)
+	    {
+	      data[j] = grub_inb(DATA);
+	    }
+

grub-core/commands/fwconfig.c line 106

Quoted 10 lines
grub-core/commands/fwconfig.c new file +10 -0
+	  data[filesize] = '\0';
+
+	  grub_env_set (argv[1], data);
+
+	  grub_free(data);
+	  return 0;
+	}
+    }
+
+  return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("couldn't find entry %s"), argv[0]);

Ditto.

Additionally, could you be more in line with GRUB2 coding style? I know that it is not strictly defined but if you look at a few files you will catch what I mean.

Though in general LGTM but this is not 2.02 material. I am adding this to my radar for next release.

Daniel