Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reimplement boot_counter #14

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
grub2 (2.12-1deepin10) unstable; urgency=medium

* reimplement boot_counter

-- ssk-wh <[email protected]> Wed, 14 Aug 2024 14:52:05 +0800

grub2 (2.12-1deepin9) unstable; urgency=medium

* set GRUB_DEVICE by env when root is overlayfs
Expand Down
176 changes: 176 additions & 0 deletions debian/patches/reimplement_boot_counter.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
Index: grub2-deepin-/Makefile.util.def
===================================================================
--- grub2-deepin-.orig/Makefile.util.def
+++ grub2-deepin-/Makefile.util.def
@@ -473,6 +473,12 @@ script = {
};

script = {
+ name = '08_fallback_counting';
+ common = util/grub.d/08_fallback_counting.in;
+ installdir = grubconf;
+};
+
+script = {
name = '10_windows';
common = util/grub.d/10_windows.in;
installdir = grubconf;
Index: grub2-deepin-/grub-core/Makefile.core.def
===================================================================
--- grub2-deepin-.orig/grub-core/Makefile.core.def
+++ grub2-deepin-/grub-core/Makefile.core.def
@@ -409,6 +409,11 @@ kernel = {
extra_dist = kern/mips/cache_flush.S;
};

+module = {
+ name = increment;
+ common = commands/increment.c;
+};
+
program = {
name = grub-emu;
mansection = 1;
Index: grub2-deepin-/grub-core/commands/increment.c
===================================================================
--- /dev/null
+++ grub2-deepin-/grub-core/commands/increment.c
@@ -0,0 +1,109 @@
+/* increment.c - Commands to increment and decrement variables. */
+/*[]
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2006,2007,2008 Free Software Foundation, Inc.
+ *
+ * GRUB is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GRUB is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/mm.h>
+#include <grub/file.h>
+#include <grub/disk.h>
+#include <grub/misc.h>
+#include <grub/env.h>
+#include <grub/partition.h>
+#include <grub/lib/envblk.h>
+#include <grub/extcmd.h>
+#include <grub/i18n.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+#define UNUSED __attribute__((__unused__))
+
+typedef enum {
+ INCREMENT,
+ DECREMENT,
+} operation;
+
+static grub_err_t
+incr_decr(operation op, int argc, char **args)
+{
+ const char *old;
+ char *new;
+ long value;
+
+ if (argc < 1)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_ ("no variable specified"));
+ if (argc > 1)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_ ("too many arguments"));
+
+ old = grub_env_get (*args);
+ if (!old)
+ return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("No such variable \"%s\""),
+ *args);
+
+ value = grub_strtol (old, NULL, 0);
+ if (grub_errno != GRUB_ERR_NONE)
+ return grub_errno;
+
+ switch (op)
+ {
+ case INCREMENT:
+ value += 1;
+ break;
+ case DECREMENT:
+ value -= 1;
+ break;
+ }
+
+ new = grub_xasprintf ("%ld", value);
+ if (!new)
+ return grub_errno;
+
+ grub_env_set (*args, new);
+ grub_free (new);
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_cmd_incr(struct grub_command *cmd UNUSED,
+ int argc, char **args)
+{
+ return incr_decr(INCREMENT, argc, args);
+}
+
+static grub_err_t
+grub_cmd_decr(struct grub_command *cmd UNUSED,
+ int argc, char **args)
+{
+ return incr_decr(DECREMENT, argc, args);
+}
+
+static grub_command_t cmd_incr, cmd_decr;
+
+GRUB_MOD_INIT(increment)
+{
+ cmd_incr = grub_register_command ("increment", grub_cmd_incr, N_("VARIABLE"),
+ N_("increment VARIABLE"));
+ cmd_decr = grub_register_command ("decrement", grub_cmd_decr, N_("VARIABLE"),
+ N_("decrement VARIABLE"));
+}
+
+GRUB_MOD_FINI(increment)
+{
+ grub_unregister_command (cmd_incr);
+ grub_unregister_command (cmd_decr);
+}
Index: grub2-deepin-/util/grub.d/08_fallback_counting.in
===================================================================
--- /dev/null
+++ grub2-deepin-/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
1 change: 1 addition & 0 deletions debian/patches/series
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,4 @@ deepin/0001-util-grub-mkrescue-use-capitalised-paths-for-removab.patch
0001-loongarch64-able-to-start-on-legacy-firmware.patch
0001-grub-install-loongarch64-efi-also-install-BOOTLOONGA.patch
deepin/0002-probe-grub-device-for-overlay-as-root.patch
reimplement_boot_counter.patch
Loading