1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
12 #include "main-func.h"
13 #include "parse-util.h"
14 #include "path-util.h"
19 static char *arg_path
= NULL
;
21 STATIC_DESTRUCTOR_REGISTER(arg_path
, freep
);
23 static int help(int argc
, char *argv
[], void *userdata
) {
25 printf("%s [COMMAND] [OPTIONS...]\n"
27 "Mark the boot process as good or bad.\n\n"
28 " -h --help Show this help\n"
29 " --version Print version\n"
30 " --path=PATH Path to the EFI System Partition (ESP)\n"
33 " good Mark this boot as good\n"
34 " bad Mark this boot as bad\n"
35 " indeterminate Undo any marking as good or bad\n",
36 program_invocation_short_name
);
41 static int parse_argv(int argc
, char *argv
[]) {
47 static const struct option options
[] = {
48 { "help", no_argument
, NULL
, 'h' },
49 { "version", no_argument
, NULL
, ARG_VERSION
},
50 { "path", required_argument
, NULL
, ARG_PATH
},
59 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
70 r
= free_and_strdup(&arg_path
, optarg
);
79 assert_not_reached("Unknown option");
85 static int acquire_esp(void) {
86 _cleanup_free_
char *np
= NULL
;
89 r
= find_esp_and_warn(arg_path
, false, &np
, NULL
, NULL
, NULL
, NULL
);
90 if (r
== -ENOKEY
) /* find_esp_and_warn() doesn't warn in this one error case, but in all others */
91 return log_error_errno(r
,
92 "Couldn't find EFI system partition. It is recommended to mount it to /boot or /efi.\n"
93 "Alternatively, use --path= to specify path to mount point.");
97 free_and_replace(arg_path
, np
);
98 log_debug("Using EFI System Partition at %s.", arg_path
);
103 static int parse_counter(
107 uint64_t *ret_done
) {
123 k
= strspn(e
, DIGITS
);
125 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
126 "Can't parse empty 'tries left' counter from LoaderBootCountPath: %s",
130 r
= safe_atou64(z
, &left
);
132 return log_error_errno(r
, "Failed to parse 'tries left' counter from LoaderBootCountPath: %s", path
);
139 k
= strspn(e
, DIGITS
);
140 if (k
== 0) /* If there's a "-" there also needs to be at least one digit */
141 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
142 "Can't parse empty 'tries done' counter from LoaderBootCountPath: %s",
146 r
= safe_atou64(z
, &done
);
148 return log_error_errno(r
, "Failed to parse 'tries done' counter from LoaderBootCountPath: %s", path
);
155 log_warning("The 'tries done' counter is currently at zero. This can't really be, after all we are running, and this boot must hence count as one. Proceeding anyway.");
168 static int acquire_boot_count_path(
175 _cleanup_free_
char *path
= NULL
, *prefix
= NULL
, *suffix
= NULL
;
176 const char *last
, *e
;
180 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderBootCountPath", &path
);
182 return -EUNATCH
; /* in this case, let the caller print a message */
184 return log_error_errno(r
, "Failed to read LoaderBootCountPath EFI variable: %m");
186 efi_tilt_backslashes(path
);
188 if (!path_is_normalized(path
))
189 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
190 "Path read from LoaderBootCountPath is not normalized, refusing: %s",
193 if (!path_is_absolute(path
))
194 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
195 "Path read from LoaderBootCountPath is not absolute, refusing: %s",
198 last
= last_path_component(path
);
199 e
= strrchr(last
, '+');
201 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
202 "Path read from LoaderBootCountPath does not contain a counter, refusing: %s",
206 prefix
= strndup(path
, e
- path
);
211 r
= parse_counter(path
, &e
, &left
, &done
);
220 *ret_suffix
= TAKE_PTR(suffix
);
224 *ret_path
= TAKE_PTR(path
);
226 *ret_prefix
= TAKE_PTR(prefix
);
235 static int make_good(const char *prefix
, const char *suffix
, char **ret
) {
236 _cleanup_free_
char *good
= NULL
;
242 /* Generate the path we'd use on good boots. This one is easy. If we are successful, we simple drop the counter
243 * pair entirely from the name. After all, we know all is good, and the logs will contain information about the
244 * tries we needed to come here, hence it's safe to drop the counters from the name. */
246 good
= strjoin(prefix
, suffix
);
250 *ret
= TAKE_PTR(good
);
254 static int make_bad(const char *prefix
, uint64_t done
, const char *suffix
, char **ret
) {
255 _cleanup_free_
char *bad
= NULL
;
261 /* Generate the path we'd use on bad boots. Let's simply set the 'left' counter to zero, and keep the 'done'
262 * counter. The information might be interesting to boot loaders, after all. */
265 bad
= strjoin(prefix
, "+0", suffix
);
269 if (asprintf(&bad
, "%s+0-%" PRIu64
"%s", prefix
, done
, suffix
) < 0)
273 *ret
= TAKE_PTR(bad
);
277 static const char *skip_slash(const char *path
) {
279 assert(path
[0] == '/');
284 static int verb_status(int argc
, char *argv
[], void *userdata
) {
286 _cleanup_free_
char *path
= NULL
, *prefix
= NULL
, *suffix
= NULL
, *good
= NULL
, *bad
= NULL
;
287 _cleanup_close_
int fd
= -1;
291 r
= acquire_boot_count_path(&path
, &prefix
, &left
, &done
, &suffix
);
292 if (r
== -EUNATCH
) { /* No boot count in place, then let's consider this a "clean" boot, as "good", "bad" or "indeterminate" don't apply. */
303 r
= make_good(prefix
, suffix
, &good
);
307 r
= make_bad(prefix
, done
, suffix
, &bad
);
311 log_debug("Booted file: %s%s\n"
312 "The same modified for 'good': %s%s\n"
313 "The same modified for 'bad': %s%s\n",
318 log_debug("Tries left: %" PRIu64
"\n"
319 "Tries done: %" PRIu64
"\n",
322 fd
= open(arg_path
, O_DIRECTORY
|O_CLOEXEC
|O_RDONLY
);
324 return log_error_errno(errno
, "Failed to open ESP '%s': %m", arg_path
);
326 if (faccessat(fd
, skip_slash(path
), F_OK
, 0) >= 0) {
327 puts("indeterminate");
331 return log_error_errno(errno
, "Failed to check if '%s' exists: %m", path
);
333 if (faccessat(fd
, skip_slash(good
), F_OK
, 0) >= 0) {
338 return log_error_errno(errno
, "Failed to check if '%s' exists: %m", good
);
340 if (faccessat(fd
, skip_slash(bad
), F_OK
, 0) >= 0) {
345 return log_error_errno(errno
, "Failed to check if '%s' exists: %m", bad
);
347 return log_error_errno(errno
, "Couldn't determine boot state: %m");
350 static int verb_set(int argc
, char *argv
[], void *userdata
) {
351 _cleanup_free_
char *path
= NULL
, *prefix
= NULL
, *suffix
= NULL
, *good
= NULL
, *bad
= NULL
, *parent
= NULL
;
352 const char *target
, *source1
, *source2
;
353 _cleanup_close_
int fd
= -1;
357 r
= acquire_boot_count_path(&path
, &prefix
, NULL
, &done
, &suffix
);
358 if (r
== -EUNATCH
) /* acquire_boot_count_path() won't log on its own for this specific error */
359 return log_error_errno(r
, "Not booted with boot counting in effect.");
367 r
= make_good(prefix
, suffix
, &good
);
371 r
= make_bad(prefix
, done
, suffix
, &bad
);
375 fd
= open(arg_path
, O_DIRECTORY
|O_CLOEXEC
|O_RDONLY
);
377 return log_error_errno(errno
, "Failed to open ESP '%s': %m", arg_path
);
379 /* Figure out what rename to what */
380 if (streq(argv
[0], "good")) {
383 source2
= bad
; /* Maybe this boot was previously marked as 'bad'? */
384 } else if (streq(argv
[0], "bad")) {
387 source2
= good
; /* Maybe this boot was previously marked as 'good'? */
389 assert(streq(argv
[0], "indeterminate"));
395 r
= rename_noreplace(fd
, skip_slash(source1
), fd
, skip_slash(target
));
398 else if (r
== -ENOENT
) {
400 r
= rename_noreplace(fd
, skip_slash(source2
), fd
, skip_slash(target
));
403 else if (r
== -ENOENT
) {
405 if (access(target
, F_OK
) >= 0) /* Hmm, if we can't find either source file, maybe the destination already exists? */
408 return log_error_errno(r
, "Can't find boot counter source file for '%s': %m", target
);
410 return log_error_errno(r
, "Failed to rename '%s' to '%s': %m", source2
, target
);
412 log_debug("Successfully renamed '%s' to '%s'.", source2
, target
);
415 return log_error_errno(r
, "Failed to rename '%s' to '%s': %m", source1
, target
);
417 log_debug("Successfully renamed '%s' to '%s'.", source1
, target
);
419 /* First, fsync() the directory these files are located in */
420 parent
= dirname_malloc(path
);
424 r
= fsync_path_at(fd
, skip_slash(parent
));
426 log_debug_errno(errno
, "Failed to synchronize image directory, ignoring: %m");
428 /* Secondly, syncfs() the whole file system these files are located in */
430 log_debug_errno(errno
, "Failed to synchronize ESP, ignoring: %m");
432 log_info("Marked boot as '%s'. (Boot attempt counter is at %" PRIu64
".)", argv
[0], done
);
437 log_debug("Operation already executed before, not doing anything.");
441 static int run(int argc
, char *argv
[]) {
443 static const Verb verbs
[] = {
444 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
445 { "status", VERB_ANY
, 1, VERB_DEFAULT
, verb_status
},
446 { "good", VERB_ANY
, 1, VERB_MUST_BE_ROOT
, verb_set
},
447 { "bad", VERB_ANY
, 1, VERB_MUST_BE_ROOT
, verb_set
},
448 { "indeterminate", VERB_ANY
, 1, VERB_MUST_BE_ROOT
, verb_set
},
454 log_parse_environment();
457 r
= parse_argv(argc
, argv
);
461 if (detect_container() > 0)
462 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
463 "Marking a boot is not supported in containers.");
466 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
467 "Marking a boot is only supported on EFI systems.");
469 return dispatch_verb(argc
, argv
, verbs
, NULL
);
472 DEFINE_MAIN_FUNCTION(run
);