db4a996
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
db4a996
From: Peter Jones <pjones@redhat.com>
db4a996
Date: Thu, 4 Oct 2018 14:22:09 -0400
db4a996
Subject: [PATCH] Reimplement boot_counter
db4a996
db4a996
This adds "increment" and "decrement" commands, and uses them to maintain our
db4a996
variables in 01_fallback_counter.  It also simplifies the counter logic, so
db4a996
that there are no nested tests that conflict with each other.
db4a996
db4a996
Apparently, this *really* wasn't tested well enough.
db4a996
db4a996
Resolves: rhbz#1614637
db4a996
Signed-off-by: Peter Jones <pjones@redhat.com>
db4a996
---
db4a996
 Makefile.util.def                   |   6 +++
db4a996
 grub-core/Makefile.core.def         |   5 ++
db4a996
 grub-core/commands/increment.c      | 105 ++++++++++++++++++++++++++++++++++++
db4a996
 util/grub.d/01_fallback_counting.in |  15 ++++++
db4a996
 4 files changed, 131 insertions(+)
db4a996
 create mode 100644 grub-core/commands/increment.c
db4a996
 create mode 100644 util/grub.d/01_fallback_counting.in
db4a996
db4a996
diff --git a/Makefile.util.def b/Makefile.util.def
db4a996
index cba4d500198..c8cb91308d9 100644
db4a996
--- a/Makefile.util.def
db4a996
+++ b/Makefile.util.def
db4a996
@@ -448,6 +448,12 @@ script = {
db4a996
   installdir = grubconf;
db4a996
 };
db4a996
 
db4a996
+script = {
db4a996
+  name = '01_fallback_counting';
db4a996
+  common = util/grub.d/01_fallback_counting.in;
db4a996
+  installdir = grubconf;
db4a996
+};
db4a996
+
db4a996
 script = {
db4a996
   name = '01_menu_auto_hide';
db4a996
   common = util/grub.d/01_menu_auto_hide.in;
db4a996
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
db4a996
index 30c2ac8f3c8..826e27af900 100644
db4a996
--- a/grub-core/Makefile.core.def
db4a996
+++ b/grub-core/Makefile.core.def
db4a996
@@ -362,6 +362,11 @@ kernel = {
db4a996
   extra_dist = kern/mips/cache_flush.S;
db4a996
 };
db4a996
 
db4a996
+module = {
db4a996
+  name = increment;
db4a996
+  common = commands/increment.c;
db4a996
+};
db4a996
+
db4a996
 program = {
db4a996
   name = grub-emu;
db4a996
   mansection = 1;
db4a996
diff --git a/grub-core/commands/increment.c b/grub-core/commands/increment.c
db4a996
new file mode 100644
db4a996
index 00000000000..79cf137656c
db4a996
--- /dev/null
db4a996
+++ b/grub-core/commands/increment.c
db4a996
@@ -0,0 +1,105 @@
db4a996
+/* increment.c - Commands to increment and decrement variables. */
db4a996
+/*
db4a996
+ *  GRUB  --  GRand Unified Bootloader
db4a996
+ *  Copyright (C) 2006,2007,2008  Free Software Foundation, Inc.
db4a996
+ *
db4a996
+ *  GRUB is free software: you can redistribute it and/or modify
db4a996
+ *  it under the terms of the GNU General Public License as published by
db4a996
+ *  the Free Software Foundation, either version 3 of the License, or
db4a996
+ *  (at your option) any later version.
db4a996
+ *
db4a996
+ *  GRUB is distributed in the hope that it will be useful,
db4a996
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
db4a996
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
db4a996
+ *  GNU General Public License for more details.
db4a996
+ *
db4a996
+ *  You should have received a copy of the GNU General Public License
db4a996
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
db4a996
+ */
db4a996
+
db4a996
+#include <grub/dl.h>
db4a996
+#include <grub/term.h>
db4a996
+#include <grub/time.h>
db4a996
+#include <grub/types.h>
db4a996
+#include <grub/misc.h>
db4a996
+#include <grub/extcmd.h>
db4a996
+#include <grub/i18n.h>
db4a996
+#include <grub/env.h>
db4a996
+
db4a996
+GRUB_MOD_LICENSE ("GPLv3+");
db4a996
+
db4a996
+typedef enum {
db4a996
+    INCREMENT,
db4a996
+    DECREMENT,
db4a996
+} operation;
db4a996
+
db4a996
+static grub_err_t
db4a996
+incr_decr(operation op, int argc, char **args)
db4a996
+{
db4a996
+  const char *old;
db4a996
+  char *new;
db4a996
+  long value;
db4a996
+
db4a996
+  if (argc < 1)
db4a996
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_ ("no variable specified"));
db4a996
+  if (argc > 1)
db4a996
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_ ("too many arguments"));
db4a996
+
db4a996
+  old = grub_env_get (*args);
db4a996
+  if (!old)
db4a996
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("No such variable \"%s\""),
db4a996
+		       *args);
db4a996
+
db4a996
+  value = grub_strtol (old, NULL, 0);
db4a996
+  if (grub_errno != GRUB_ERR_NONE)
db4a996
+    return grub_errno;
db4a996
+
db4a996
+  switch (op)
db4a996
+    {
db4a996
+    case INCREMENT:
db4a996
+      value += 1;
db4a996
+      break;
db4a996
+    case DECREMENT:
db4a996
+      value -= 1;
db4a996
+      break;
db4a996
+    }
db4a996
+
db4a996
+  new = grub_xasprintf ("%ld", value);
db4a996
+  if (!new)
db4a996
+    return grub_errno;
db4a996
+
db4a996
+  grub_env_set (*args, new);
db4a996
+  grub_free (new);
db4a996
+
db4a996
+  return GRUB_ERR_NONE;
db4a996
+}
db4a996
+
db4a996
+static grub_err_t
db4a996
+grub_cmd_incr(struct grub_command *cmd UNUSED,
db4a996
+              int argc, char **args)
db4a996
+{
db4a996
+  return incr_decr(INCREMENT, argc, args);
db4a996
+}
db4a996
+
db4a996
+static grub_err_t
db4a996
+grub_cmd_decr(struct grub_command *cmd UNUSED,
db4a996
+              int argc, char **args)
db4a996
+{
db4a996
+  return incr_decr(DECREMENT, argc, args);
db4a996
+}
db4a996
+
db4a996
+static grub_command_t cmd_incr, cmd_decr;
db4a996
+
db4a996
+GRUB_MOD_INIT(increment)
db4a996
+{
db4a996
+  cmd_incr = grub_register_command ("increment", grub_cmd_incr, N_("VARIABLE"),
db4a996
+                                    N_("increment VARIABLE"));
db4a996
+  cmd_decr = grub_register_command ("decrement", grub_cmd_decr, N_("VARIABLE"),
db4a996
+                                    N_("decrement VARIABLE"));
db4a996
+}
db4a996
+
db4a996
+GRUB_MOD_FINI(increment)
db4a996
+{
db4a996
+  grub_unregister_command (cmd_incr);
db4a996
+  grub_unregister_command (cmd_decr);
db4a996
+}
db4a996
diff --git a/util/grub.d/01_fallback_counting.in b/util/grub.d/01_fallback_counting.in
db4a996
new file mode 100644
db4a996
index 00000000000..6ca13da03df
db4a996
--- /dev/null
db4a996
+++ b/util/grub.d/01_fallback_counting.in
db4a996
@@ -0,0 +1,15 @@
db4a996
+#! /bin/sh -e
db4a996
+
db4a996
+# Boot Counting
db4a996
+cat << EOF
db4a996
+insmod increment
db4a996
+if [ -z "\${boot_counter}" ]; then
db4a996
+  set boot_counter=0
db4a996
+elif [ "\${boot_counter}" = "0" -o "\${boot_counter}" = "-1" ]; then
db4a996
+  increment default
db4a996
+  set boot_counter=-1
db4a996
+else
db4a996
+  decrement boot_counter
db4a996
+fi
db4a996
+save_env boot_counter
db4a996
+EOF