All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] version: add a module to get GRUB version
@ 2020-04-15 12:04 Flavio Suligoi
  2020-04-15 13:35 ` Lennart Sorensen
  2020-04-16 14:53 ` Daniel Kiper
  0 siblings, 2 replies; 5+ messages in thread
From: Flavio Suligoi @ 2020-04-15 12:04 UTC (permalink / raw)
  To: The development of GNU GRUB, Paul Menzel; +Cc: 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.

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 <f.suligoi@asem.it>
Reviewed-by: Paul Menzel <pmenzel@molgen.mpg.de>
---

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



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] version: add a module to get GRUB version
  2020-04-15 12:04 [PATCH v2] version: add a module to get GRUB version Flavio Suligoi
@ 2020-04-15 13:35 ` Lennart Sorensen
  2020-04-15 14:26   ` Flavio Suligoi
  2020-04-16 14:53 ` Daniel Kiper
  1 sibling, 1 reply; 5+ messages in thread
From: Lennart Sorensen @ 2020-04-15 13:35 UTC (permalink / raw)
  To: The development of GNU GRUB; +Cc: Paul Menzel, Flavio Suligoi

On Wed, Apr 15, 2020 at 02:04:36PM +0200, Flavio Suligoi wrote:
> 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.

You can't actually assume that the grub binary on the running system is
the same version as the one currently booting the system.  It probably
tends to be if the distribution takes care to auto upgrade it when a
new grub is installed, but it doesn't always happen.

Of course in the case of a distribution, it's package management would
already have that information.

I am not saying adding this is a bad idea, I just don't believe it does
anything useful.  I just believe it is trying to solve a problem that
doesn't actually exist.

-- 
Len Sorensen


^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [PATCH v2] version: add a module to get GRUB version
  2020-04-15 13:35 ` Lennart Sorensen
@ 2020-04-15 14:26   ` Flavio Suligoi
  0 siblings, 0 replies; 5+ messages in thread
From: Flavio Suligoi @ 2020-04-15 14:26 UTC (permalink / raw)
  To: Lennart Sorensen, The development of GNU GRUB; +Cc: Paul Menzel

Hi Len,
 
> On Wed, Apr 15, 2020 at 02:04:36PM +0200, Flavio Suligoi wrote:
> > 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.
> 
> You can't actually assume that the grub binary on the running system is
> the same version as the one currently booting the system.  It probably
> tends to be if the distribution takes care to auto upgrade it when a
> new grub is installed, but it doesn't always happen.
> 
> Of course in the case of a distribution, it's package management would
> already have that information.

Our embedded distro doesn't have any package manager,
we update the whole rootfs.
We use swupdate and the storage has a redundant rootfs,
with rootfs-A and rootfs-B.

> I am not saying adding this is a bad idea, I just don't believe it does
> anything useful.  I just believe it is trying to solve a problem that
> doesn't actually exist.

We use swupdate to update both the rootfs and grub.

So, at every boot, the grub.cfg script, before running the kernel,
saves the grub version in an environment variable.

Then, after the boot, the update application, using the swupdate
command "fw_printenv", reads the version and then decides
about the grub updating.

For this reason, the "version" command is so essential
in our application.

Flavio



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] version: add a module to get GRUB version
  2020-04-15 12:04 [PATCH v2] version: add a module to get GRUB version Flavio Suligoi
  2020-04-15 13:35 ` Lennart Sorensen
@ 2020-04-16 14:53 ` Daniel Kiper
  2020-04-16 15:12   ` Flavio Suligoi
  1 sibling, 1 reply; 5+ messages in thread
From: Daniel Kiper @ 2020-04-16 14:53 UTC (permalink / raw)
  To: Flavio Suligoi; +Cc: The development of GNU GRUB, Paul Menzel

On Wed, Apr 15, 2020 at 02:04:36PM +0200, Flavio Suligoi wrote:
> 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>

Do we really need a module for that? I do not think so. I would prefer
if the GRUB sets a number of variables immediately after load. Please
take a look at grub_cpu or grub_platform variables currently available
in the GRUB. They are good examples. Going that way we should have, e.g.
grub_ver variable. Additionally, I would add for convenience grub_major,
grub_minor, grub_commit and grub_oem. So, let's assume that we have 2.06
version. Then grub_major should be "2" and grub_minor "06". grub_commit
and grub_oem should not exists. If we have 2.06.d5a32255d then
grub_major should be "2", grub_minor "06" and grub_commit "d5a32255d".
grub_oem should not exists. If we have 2.06.d5a32255d_1.0.0, then ...
and grub_oem should be "1.0.0". There should not be any requirement to
have grub_commit nor grub_oem. So, e.g. 2.06_1.0.0 would be a valid
version too.

grub_commit and grub_oem should be enabled at ./configure call. By
default grub_major and grub_minor should be set only. I think that
AC_DEFINE() autoconf macro could be useful for you.

Last but not, version displayed at the GRUB load have to be consistent
with what is set in the variables.

If you have any questions drop me a line.

Daniel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [PATCH v2] version: add a module to get GRUB version
  2020-04-16 14:53 ` Daniel Kiper
@ 2020-04-16 15:12   ` Flavio Suligoi
  0 siblings, 0 replies; 5+ messages in thread
From: Flavio Suligoi @ 2020-04-16 15:12 UTC (permalink / raw)
  To: Daniel Kiper; +Cc: The development of GNU GRUB, Paul Menzel

Hi Daniel,

> > 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>
> 
> Do we really need a module for that? I do not think so. I would prefer
> if the GRUB sets a number of variables immediately after load. Please
> take a look at grub_cpu or grub_platform variables currently available
> in the GRUB. They are good examples. Going that way we should have, e.g.
> grub_ver variable. Additionally, I would add for convenience grub_major,
> grub_minor, grub_commit and grub_oem. So, let's assume that we have 2.06
> version. Then grub_major should be "2" and grub_minor "06". grub_commit
> and grub_oem should not exists. If we have 2.06.d5a32255d then
> grub_major should be "2", grub_minor "06" and grub_commit "d5a32255d".
> grub_oem should not exists. If we have 2.06.d5a32255d_1.0.0, then ...
> and grub_oem should be "1.0.0". There should not be any requirement to
> have grub_commit nor grub_oem. So, e.g. 2.06_1.0.0 would be a valid
> version too.
> 
> grub_commit and grub_oem should be enabled at ./configure call. By
> default grub_major and grub_minor should be set only. I think that
> AC_DEFINE() autoconf macro could be useful for you.
> 
> Last but not, version displayed at the GRUB load have to be consistent
> with what is set in the variables.

Good idea, I like your suggestion!

Thank you!

Flavio


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-04-16 15:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-15 12:04 [PATCH v2] version: add a module to get GRUB version Flavio Suligoi
2020-04-15 13:35 ` Lennart Sorensen
2020-04-15 14:26   ` Flavio Suligoi
2020-04-16 14:53 ` Daniel Kiper
2020-04-16 15:12   ` Flavio Suligoi

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.