Blame SOURCES/0278-Reimplement-boot_counter.patch

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