grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] version: add a module to get GRUB version


From: Paul Menzel
Subject: Re: [PATCH] version: add a module to get GRUB version
Date: Mon, 30 Mar 2020 17:28:49 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0

Dear Flavio,


Thanks a lot for the patch.

Am 30.03.20 um 17:02 schrieb Flavio Suligoi:
Sometimes, writing a custom grub.cfg configuration file,
especially with embedded systems, it is indispensable
to know the version of the running GRUB.
This is essential for every automatic sw update procedure.

Additionaly to the standard GRUB version, I added a

Additionally

supplementary "OEM" version, to keep track of the OEM
custom GRUB version.
For further script test and processing, the version
can be also saved in an environment variable.

I suggest to add one blank line between paragraphs to help legibility.


Command syntax:

grub> help version
Usage: version [OPTIONS]

Print GRUB or GRUB+OEM version.
If --set is specified, the version is set to a variable

-s, --set=VARNAME       Set a variable to return value.
-o, --oem               Add OEM version
-h, --help              Display this help and exit.
-u, --usage             Display the usage of this command and exit.
grub>

Signed-off-by: Flavio Suligoi <address@hidden>
---
  Makefile.am                  |  2 +-
  docs/grub.texi               |  9 +++++
  grub-core/Makefile.core.def  |  5 +++
  grub-core/commands/version.c | 93 ++++++++++++++++++++++++++++++++++++++++++++
  include/oem.h                | 25 ++++++++++++
  5 files changed, 133 insertions(+), 1 deletion(-)
  create mode 100644 grub-core/commands/version.c
  create mode 100644 include/oem.h

diff --git a/Makefile.am b/Makefile.am
index bf9c1ba..151a437 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -425,7 +425,7 @@ if COND_i386_coreboot
  FS_PAYLOAD_MODULES ?= $(shell cat grub-core/fs.lst)
  default_payload.elf: grub-mkstandalone grub-mkimage FORCE
        test -f $@ && rm $@ || true
-       pkgdatadir=. ./grub-mkstandalone --grub-mkimage=./grub-mkimage -O 
i386-coreboot -o $@ --modules='ahci pata ehci uhci ohci usb_keyboard usbms 
part_msdos ext2 fat at_keyboard part_gpt usbserial_usbdebug cbfs' 
--install-modules='ls linux search configfile normal cbtime cbls memrw iorw 
minicmd lsmmap lspci halt reboot hexdump pcidump regexp setpci lsacpi chain 
test serial multiboot cbmemc linux16 gzio echo help syslinuxcfg xnu 
$(FS_PAYLOAD_MODULES) password_pbkdf2 $(EXTRA_PAYLOAD_MODULES)' --fonts= 
--themes= --locales= -d grub-core/ /boot/grub/grub.cfg=$(srcdir)/coreboot.cfg
+       pkgdatadir=. ./grub-mkstandalone --grub-mkimage=./grub-mkimage -O 
i386-coreboot -o $@ --modules='ahci pata ehci uhci ohci usb_keyboard usbms 
part_msdos ext2 fat at_keyboard part_gpt usbserial_usbdebug cbfs' 
--install-modules='ls linux search configfile normal cbtime cbls memrw iorw 
minicmd lsmmap lspci halt reboot hexdump pcidump regexp setpci lsacpi chain 
test serial multiboot cbmemc linux16 gzio echo help syslinuxcfg xnu 
$(FS_PAYLOAD_MODULES) password_pbkdf2 version $(EXTRA_PAYLOAD_MODULES)' 
--fonts= --themes= --locales= -d grub-core/ 
/boot/grub/grub.cfg=$(srcdir)/coreboot.cfg

Out of curiosity, are you using coreboot based firmware?

  endif
endif
diff --git a/docs/grub.texi b/docs/grub.texi
index 83979af..b9302da 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -4013,6 +4013,7 @@ you forget a command, you can run the command 
@command{help}
  * uppermem::                    Set the upper memory size
  @comment * vbeinfo::                     List available video modes
  * verify_detached::             Verify detached digital signature
+* version::                     Show GRUB (or GRUB+OEM) version

I’d add spaces around the + sign.

  * videoinfo::                   List available video modes
  @comment * xen_*::              Xen boot commands for AArch64
  * wrmsr::                       Write values to model-specific registers
@@ -5374,6 +5375,14 @@ successfully.  If validation fails, it is set to a 
non-zero value.
  @xref{Using digital signatures}, for more information.
  @end deffn
+@node version
+@subsection version
+@deffn Command version [@option{--set} var] [@option{--oem}]
+Display GRUB version. If option @option{--oem} is given, display also
+the OEM version.
+If option @option{--set} is give, store the version instead of printing it.

give*n*

+@end deffn
+
  @node videoinfo
  @subsection videoinfo
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 48b82e7..357d750 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -2534,3 +2534,8 @@ module = {
    common = commands/i386/wrmsr.c;
    enable = x86;
  };
+
+module = {
+  name = version;
+  common = commands/version.c;
+};
diff --git a/grub-core/commands/version.c b/grub-core/commands/version.c
new file mode 100644
index 0000000..07458be
--- /dev/null
+++ b/grub-core/commands/version.c
@@ -0,0 +1,93 @@
+/* version.c - command to print GRUB version and GRUB+OEM version  */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2020  Free Software Foundation, Inc.
+ *
+ *  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/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/command.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/env.h>
+#include <grub/extcmd.h>
+#include <grub/i18n.h>
+#include <oem.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static const struct grub_arg_option options[] =
+{
+  { "set", 's', 0, N_("Set a variable to return value."), N_("VARNAME"),
+    ARG_TYPE_STRING },

Assign return value to variable

+  { "oem", 'o', 0, N_("Add OEM version"), 0, 0 },
+  { 0, 0, 0, 0, 0, 0 }
+};
+
+static grub_err_t
+grub_cmd_version (grub_extcmd_context_t ctxt,
+                  int argc,
+                  char **args __attribute__ ((unused)))
+{
+  char *buf = 0;
+  unsigned int package_len, oem_len;
+
+  if (argc > 0) {
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("no argument(s) required"));
+  }
+
+  package_len = sizeof (PACKAGE_VERSION);
+  oem_len = sizeof (PACKAGE_VERSION_OEM);
+
+  buf = grub_malloc (package_len + oem_len + 1);
+  if (!buf)
+    return grub_errno;
+
+  /* Package version */
+  grub_strcpy (buf, PACKAGE_VERSION);
+
+  /* OEM version required? */
+  if (ctxt->state[1].set) {
+    grub_strncpy (buf + package_len -1, "_", 1);
+    grub_strncpy (buf + package_len, PACKAGE_VERSION_OEM, oem_len);
+  }
+
+  /* Set env variable required? */
+  if (ctxt->state[0].set) {
+    grub_env_set (ctxt->state[0].arg, buf);
+  } else {
+    grub_printf ("%s", buf);
+  }
+
+  grub_free(buf);
+
+  return 0;
+}
+
+static grub_extcmd_t cmd;
+
+GRUB_MOD_INIT(version)
+{
+  cmd = grub_register_extcmd ("version", grub_cmd_version, 0,
+                              N_("[OPTIONS]"),
+                              N_("\nPrint GRUB or GRUB+OEM version.\n"
+                              N_("If --set is specified, the version is set "
+                              "to a variable")), options);

assigned to

+}
+
+GRUB_MOD_FINI(version)
+{
+  grub_unregister_extcmd (cmd);
+}
diff --git a/include/oem.h b/include/oem.h
new file mode 100644
index 0000000..1aa342d
--- /dev/null
+++ b/include/oem.h
@@ -0,0 +1,25 @@
+/* oem.h - Misc info for OEM customization  */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2020  Free Software Foundation, Inc.
+ *
+ *  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/>.
+ */
+
+#ifndef GRUB_OEM_CUSTOMIZATION_HEADER
+#define GRUB_OEM_CUSTOMIZATION_HEADER  1
+
+#define PACKAGE_VERSION_OEM    "v1.0.0"
+
+#endif /* ! GRUB_OEM_CUSTOMIZATION_HEADER */
\ No newline at end of file

Please add a newline at the end.


Kind regards,

Paul



reply via email to

[Prev in Thread] Current Thread [Next in Thread]