grub-devel
[Top][All Lists]
Advanced

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

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


From: Flavio Suligoi
Subject: [PATCH v2] version: add a module to get GRUB version
Date: Wed, 15 Apr 2020 14:04:36 +0200

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.

Additionally to the standard GRUB version, I added a
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.

Command syntax:

grub> help version
Usage: version [OPTIONS]

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

-s, --set=VARNAME       Assign return value to variable VARNAME.
-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>
Reviewed-by: Paul Menzel <address@hidden>
---

Changes in v2: cosmetic changes
Changes in v1: cosmetic changes

 Makefile.am                  |  2 +-
 docs/grub.texi               |  9 +++++
 grub-core/Makefile.core.def  |  5 +++
 grub-core/commands/version.c | 92 ++++++++++++++++++++++++++++++++++++++++++++
 include/oem.h                | 25 ++++++++++++
 5 files changed, 132 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
 endif
 
 endif
diff --git a/docs/grub.texi b/docs/grub.texi
index 8e6f9ac..9c0b2d8 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -4034,6 +4034,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
 * videoinfo::                   List available video modes
 @comment * xen_*::              Xen boot commands for AArch64
 * wrmsr::                       Write values to model-specific registers
@@ -5395,6 +5396,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 given, store the version instead of printing it.
+@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..1581232
--- /dev/null
+++ b/grub-core/commands/version.c
@@ -0,0 +1,92 @@
+/* version.c - 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_("Assign return value to variable VARNAME."),
+    N_("VARNAME"), ARG_TYPE_STRING },
+  { "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 "
+                              "assigned to variable.")), options);
+}
+
+GRUB_MOD_FINI(version)
+{
+  grub_unregister_extcmd (cmd);
+}
diff --git a/include/oem.h b/include/oem.h
new file mode 100644
index 0000000..d38d589
--- /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 */
-- 
2.7.4




reply via email to

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