Blame SOURCES/0107-Reimplement-boot_counter.patch

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