Merge ~beidl/grub:focal-devel-bootcounter into ~ubuntu-core-dev/grub/+git/ubuntu:focal-devel

Proposed by Alfred E. Neumayer
Status: Needs review
Proposed branch: ~beidl/grub:focal-devel-bootcounter
Merge into: ~ubuntu-core-dev/grub/+git/ubuntu:focal-devel
Diff against target: 163 lines (+149/-0)
2 files modified
debian/patches/rhboot-based-failover-counter.patch (+148/-0)
debian/patches/series (+1/-0)
Reviewer Review Type Date Requested Status
Ubuntu Core Development Team Pending
Review via email: mp+409920@code.launchpad.net

Description of the change

Implements "increment" and "decrement" commands as an additional module for counting boot attempts.
Can be used in automatic failover mechanisms where booted userspace can reset the counter
while the bootloader can switch to a different target system in case of exceeding the attempts.

Based on a patch by Peter Jones at Red Hat.
Original patch:
https://git.centos.org/rpms/grub2/blob/98a0ca1734941a1b031d6af57480496779e0d0ca/f/SOURCES/0278-Reimplement-boot_counter.patch

I intend to upstream the patch ASAP, though the legal situation regarding
the CLA needs to be cleared in a dialog first.

To post a comment you must log in.

Unmerged commits

d34d54d... by Alfred Neumayer

debian: Import RH patch for boot increment/decrement command

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/debian/patches/rhboot-based-failover-counter.patch b/debian/patches/rhboot-based-failover-counter.patch
2new file mode 100644
3index 0000000..be7d4af
4--- /dev/null
5+++ b/debian/patches/rhboot-based-failover-counter.patch
6@@ -0,0 +1,148 @@
7+From ad2d70e4104937aaba9fb95eb3af98ebbe767520 Mon Sep 17 00:00:00 2001
8+From: Alfred Neumayer <a.neumayer@mattig-schauer.at>
9+Date: Mon, 19 Apr 2021 09:10:14 +0200
10+Subject: [PATCH] grub-core: Include bootcounter patch from RH
11+
12+Original message:
13+This adds "increment" and "decrement" commands, and uses them to maintain our
14+variables in 01_fallback_counter. It also simplifies the counter logic, so
15+that there are no nested tests that conflict with each other.
16+
17+This patch is based on work by Peter Jones, based on this source patch:
18+https://git.centos.org/rpms/grub2/blob/98a0ca1734941a1b031d6af57480496779e0d0ca/f/SOURCES/0278-Reimplement-boot_counter.patch
19+---
20+ grub-core/Makefile.core.def | 5 ++
21+ grub-core/commands/increment.c | 105 ++++++++++++++++++++++++++++
22+ 4 files changed, 131 insertions(+)
23+ create mode 100644 grub-core/commands/increment.c
24+
25+diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
26+index 9b20f3335..16f723431 100644
27+--- a/grub-core/Makefile.core.def
28++++ b/grub-core/Makefile.core.def
29+@@ -387,6 +387,11 @@ kernel = {
30+ extra_dist = kern/mips/cache_flush.S;
31+ };
32+
33++module = {
34++ name = increment;
35++ common = commands/increment.c;
36++};
37++
38+ program = {
39+ name = grub-emu;
40+ mansection = 1;
41+diff --git a/grub-core/commands/increment.c b/grub-core/commands/increment.c
42+new file mode 100644
43+index 000000000..f877fd04d
44+--- /dev/null
45++++ b/grub-core/commands/increment.c
46+@@ -0,0 +1,105 @@
47++/* increment.c - Commands to increment and decrement variables. */
48++/*
49++ * GRUB -- GRand Unified Bootloader
50++ * Copyright (C) 2006,2007,2008 Free Software Foundation, Inc.
51++ *
52++ * GRUB is free software: you can redistribute it and/or modify
53++ * it under the terms of the GNU General Public License as published by
54++ * the Free Software Foundation, either version 3 of the License, or
55++ * (at your option) any later version.
56++ *
57++ * GRUB is distributed in the hope that it will be useful,
58++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
59++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60++ * GNU General Public License for more details.
61++ *
62++ * You should have received a copy of the GNU General Public License
63++ * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
64++ */
65++
66++#include <grub/dl.h>
67++#include <grub/term.h>
68++#include <grub/time.h>
69++#include <grub/types.h>
70++#include <grub/misc.h>
71++#include <grub/extcmd.h>
72++#include <grub/i18n.h>
73++#include <grub/env.h>
74++
75++GRUB_MOD_LICENSE ("GPLv3+");
76++
77++typedef enum {
78++ INCREMENT,
79++ DECREMENT,
80++} operation;
81++
82++static grub_err_t
83++incr_decr(operation op, int argc, char **args)
84++{
85++ const char *old;
86++ char *new;
87++ long value;
88++
89++ if (argc < 1)
90++ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_ ("no variable specified"));
91++ if (argc > 1)
92++ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_ ("too many arguments"));
93++
94++ old = grub_env_get (*args);
95++ if (!old)
96++ return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("No such variable \"%s\""),
97++ *args);
98++
99++ value = grub_strtol (old, NULL, 0);
100++ if (grub_errno != GRUB_ERR_NONE)
101++ return grub_errno;
102++
103++ switch (op)
104++ {
105++ case INCREMENT:
106++ value += 1;
107++ break;
108++ case DECREMENT:
109++ value -= 1;
110++ break;
111++ }
112++
113++ new = grub_xasprintf ("%ld", value);
114++ if (!new)
115++ return grub_errno;
116++
117++ grub_env_set (*args, new);
118++ grub_free (new);
119++
120++ return GRUB_ERR_NONE;
121++}
122++
123++static grub_err_t
124++grub_cmd_incr(struct grub_command *cmd __attribute__((unused)),
125++ int argc, char **args)
126++{
127++ return incr_decr(INCREMENT, argc, args);
128++}
129++
130++static grub_err_t
131++grub_cmd_decr(struct grub_command *cmd __attribute__((unused)),
132++ int argc, char **args)
133++{
134++ return incr_decr(DECREMENT, argc, args);
135++}
136++
137++static grub_command_t cmd_incr, cmd_decr;
138++
139++GRUB_MOD_INIT(increment)
140++{
141++ cmd_incr = grub_register_command ("increment", grub_cmd_incr, N_("VARIABLE"),
142++ N_("increment VARIABLE"));
143++ cmd_decr = grub_register_command ("decrement", grub_cmd_decr, N_("VARIABLE"),
144++ N_("decrement VARIABLE"));
145++}
146++
147++GRUB_MOD_FINI(increment)
148++{
149++ grub_unregister_command (cmd_incr);
150++ grub_unregister_command (cmd_decr);
151++}
152+--
153+2.25.1
154+
155diff --git a/debian/patches/series b/debian/patches/series
156index a9458c8..1c36a79 100644
157--- a/debian/patches/series
158+++ b/debian/patches/series
159@@ -81,3 +81,4 @@ ubuntu-speed-zsys-history.patch
160 ubuntu-flavour-order.patch
161 ubuntu-dont-verify-loopback-images.patch
162 ubuntu-recovery-dis_ucode_ldr.patch
163+rhboot-based-failover-counter.patch

Subscribers

People subscribed via source and target branches