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