From d371c69fed702b24a9025309c5773abddc18d3d3 Mon Sep 17 00:00:00 2001
From: Christian Glombek <lorbus@fedoraproject.org>
Date: Tue, 2 Apr 2019 16:22:21 +0200
Subject: [PATCH 169/220] grub.d: Split out boot success reset from menu auto
 hide script

Also rename fallback and menu auto hide script to be executed
before and after boot success reset script.
In menu auto hide script, rename last_boot_ok var to menu_hide_ok
---
 Makefile.util.def                    | 14 ++++++++---
 util/grub.d/01_fallback_counting.in  | 22 -----------------
 util/grub.d/01_menu_auto_hide.in     | 48 ------------------------------------
 util/grub.d/08_fallback_counting.in  | 24 ++++++++++++++++++
 util/grub.d/10_reset_boot_success.in | 25 +++++++++++++++++++
 util/grub.d/12_menu_auto_hide.in     | 35 ++++++++++++++++++++++++++
 6 files changed, 94 insertions(+), 74 deletions(-)
 delete mode 100644 util/grub.d/01_fallback_counting.in
 delete mode 100644 util/grub.d/01_menu_auto_hide.in
 create mode 100644 util/grub.d/08_fallback_counting.in
 create mode 100644 util/grub.d/10_reset_boot_success.in
 create mode 100644 util/grub.d/12_menu_auto_hide.in

diff --git a/Makefile.util.def b/Makefile.util.def
index 2019ebd..1fa92ca 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -458,14 +458,14 @@ script = {
 };
 
 script = {
-  name = '01_fallback_counting';
-  common = util/grub.d/01_fallback_counting.in;
+  name = '08_fallback_counting';
+  common = util/grub.d/08_fallback_counting.in;
   installdir = grubconf;
 };
 
 script = {
-  name = '01_menu_auto_hide';
-  common = util/grub.d/01_menu_auto_hide.in;
+  name = '12_menu_auto_hide';
+  common = util/grub.d/12_menu_auto_hide.in;
   installdir = grubconf;
 };
 
@@ -525,6 +525,12 @@ script = {
 };
 
 script = {
+  name = '10_reset_boot_success';
+  common = util/grub.d/10_reset_boot_success.in;
+  installdir = grubconf;
+};
+
+script = {
   name = '10_xnu';
   common = util/grub.d/10_xnu.in;
   installdir = grubconf;
diff --git a/util/grub.d/01_fallback_counting.in b/util/grub.d/01_fallback_counting.in
deleted file mode 100644
index be0e770..0000000
--- a/util/grub.d/01_fallback_counting.in
+++ /dev/null
@@ -1,22 +0,0 @@
-#! /bin/sh -e
-
-# Boot Counting
-# The boot_counter env var can be used to count down boot attempts after an
-# OSTree upgrade and choose the rollback deployment when 0 is reached.  Both
-# boot_counter and boot_success need to be (re-)set from userspace.
-cat << EOF
-insmod increment
-# Check if boot_counter exists and boot_success=0 to activate this behaviour.
-if [ -n "\${boot_counter}" -a "\${boot_success}" = "0" ]; then
-  # if countdown has ended, choose to boot rollback deployment (default=1 on
-  # OSTree-based systems)
-  if  [ "\${boot_counter}" = "0" -o "\${boot_counter}" = "-1" ]; then
-    set default=1
-    set boot_counter=-1
-  # otherwise decrement boot_counter
-  else
-    decrement boot_counter
-  fi
-  save_env boot_counter
-fi
-EOF
diff --git a/util/grub.d/01_menu_auto_hide.in b/util/grub.d/01_menu_auto_hide.in
deleted file mode 100644
index ad17587..0000000
--- a/util/grub.d/01_menu_auto_hide.in
+++ /dev/null
@@ -1,48 +0,0 @@
-#! /bin/sh
-
-# Disable / skip generating menu-auto-hide config parts on serial terminals
-for x in ${GRUB_TERMINAL_INPUT} ${GRUB_TERMINAL_OUTPUT}; do
-  case "$x" in
-    serial*)
-      exit 0
-      ;;
-  esac
-done
-
-cat << EOF
-if [ "\${boot_success}" = "1" -o "\${boot_indeterminate}" = "1" ]; then
-  set last_boot_ok=1
-else
-  set last_boot_ok=0
-fi
-
-# Reset boot_indeterminate after a successful boot
-if [ "\${boot_success}" = "1" ] ; then
-  set boot_indeterminate=0
-# Avoid boot_indeterminate causing the menu to be hidden more then once
-elif [ "\${boot_indeterminate}" = "1" ]; then
-  set boot_indeterminate=2
-fi
-set boot_success=0
-save_env boot_success boot_indeterminate
-
-if [ x\$feature_timeout_style = xy ] ; then
-  if [ "\${menu_show_once}" ]; then
-    unset menu_show_once
-    save_env menu_show_once
-    set timeout_style=menu
-    set timeout=60
-  elif [ "\${menu_auto_hide}" -a "\${last_boot_ok}" = "1" ]; then
-    set orig_timeout_style=\${timeout_style}
-    set orig_timeout=\${timeout}
-    if [ "\${fastboot}" = "1" ]; then
-      # timeout_style=menu + timeout=0 avoids the countdown code keypress check
-      set timeout_style=menu
-      set timeout=0
-    else
-      set timeout_style=hidden
-      set timeout=1
-    fi
-  fi
-fi
-EOF
diff --git a/util/grub.d/08_fallback_counting.in b/util/grub.d/08_fallback_counting.in
new file mode 100644
index 0000000..2e2c3ff
--- /dev/null
+++ b/util/grub.d/08_fallback_counting.in
@@ -0,0 +1,24 @@
+#! /bin/sh -e
+# Fallback Countdown
+#
+# This snippet depends on 10_reset_boot_success and needs to be kept in sync.
+#
+# The boot_counter env var can be used to count down boot attempts after an
+# OSTree upgrade and choose the rollback deployment when 0 is reached.
+# Both boot_counter=X and boot_success=1 need to be set from userspace.
+cat << EOF
+insmod increment
+# Check if boot_counter exists and boot_success=0 to activate this behaviour.
+if [ -n "\${boot_counter}" -a "\${boot_success}" = "0" ]; then
+  # if countdown has ended, choose to boot rollback deployment,
+  # i.e. default=1 on OSTree-based systems.
+  if  [ "\${boot_counter}" = "0" -o "\${boot_counter}" = "-1" ]; then
+    set default=1
+    set boot_counter=-1
+  # otherwise decrement boot_counter
+  else
+    decrement boot_counter
+  fi
+  save_env boot_counter
+fi
+EOF
diff --git a/util/grub.d/10_reset_boot_success.in b/util/grub.d/10_reset_boot_success.in
new file mode 100644
index 0000000..6c88d93
--- /dev/null
+++ b/util/grub.d/10_reset_boot_success.in
@@ -0,0 +1,25 @@
+#! /bin/sh -e
+# Reset Boot Success
+#
+# The 08_fallback_counting and 12_menu_auto_hide snippets rely on this one
+# and need to be kept in sync.
+#
+# The boot_success var needs to be set to 1 from userspace to mark a boot successful.
+cat << EOF
+insmod increment
+# Hiding the menu is ok if last boot was ok or if this is a first boot attempt to boot the entry
+if [ "\${boot_success}" = "1" -o "\${boot_indeterminate}" = "1" ]; then
+  set menu_hide_ok=1
+else
+  set menu_hide_ok=0 
+fi
+# Reset boot_indeterminate after a successful boot, increment otherwise
+if [ "\${boot_success}" = "1" ] ; then
+  set boot_indeterminate=0
+else
+  increment boot_indeterminate
+fi
+# Reset boot_success for current boot 
+set boot_success=0
+save_env boot_success boot_indeterminate
+EOF
diff --git a/util/grub.d/12_menu_auto_hide.in b/util/grub.d/12_menu_auto_hide.in
new file mode 100644
index 0000000..6a7c0fa
--- /dev/null
+++ b/util/grub.d/12_menu_auto_hide.in
@@ -0,0 +1,35 @@
+#! /bin/sh
+# Menu Auto Hide
+#
+# This snippet depends on 10_reset_boot_success and needs to be kept in sync.
+#
+# Disable / skip generating menu-auto-hide config parts on serial terminals
+for x in ${GRUB_TERMINAL_INPUT} ${GRUB_TERMINAL_OUTPUT}; do
+  case "$x" in
+    serial*)
+      exit 0
+      ;;
+  esac
+done
+
+cat << EOF
+if [ x\$feature_timeout_style = xy ] ; then
+  if [ "\${menu_show_once}" ]; then
+    unset menu_show_once
+    save_env menu_show_once
+    set timeout_style=menu
+    set timeout=60
+  elif [ "\${menu_auto_hide}" -a "\${menu_hide_ok}" = "1" ]; then
+    set orig_timeout_style=\${timeout_style}
+    set orig_timeout=\${timeout}
+    if [ "\${fastboot}" = "1" ]; then
+      # timeout_style=menu + timeout=0 avoids the countdown code keypress check
+      set timeout_style=menu
+      set timeout=0
+    else
+      set timeout_style=hidden
+      set timeout=1
+    fi
+  fi
+fi
+EOF
-- 
1.8.3.1

