From 14c9e03d7209e0282dfa972eb2c92d1ee13561da Mon Sep 17 00:00:00 2001 From: ssk-wh Date: Thu, 1 Aug 2024 14:58:24 +0800 Subject: [PATCH] feat: reimplement boot_counter --- debian/changelog | 6 + debian/patches/reimplement_boot_counter.patch | 176 ++++++++++++++++++ debian/patches/series | 1 + 3 files changed, 183 insertions(+) create mode 100644 debian/patches/reimplement_boot_counter.patch diff --git a/debian/changelog b/debian/changelog index 45f5962..dc57324 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +grub2 (2.12-1deepin10) unstable; urgency=medium + + * reimplement boot_counter + + -- ssk-wh Wed, 14 Aug 2024 14:52:05 +0800 + grub2 (2.12-1deepin9) unstable; urgency=medium * set GRUB_DEVICE by env when root is overlayfs diff --git a/debian/patches/reimplement_boot_counter.patch b/debian/patches/reimplement_boot_counter.patch new file mode 100644 index 0000000..8a54449 --- /dev/null +++ b/debian/patches/reimplement_boot_counter.patch @@ -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 . ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++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 diff --git a/debian/patches/series b/debian/patches/series index fcb11c0..483394f 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -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