1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
8 #include "efi-loader.h"
13 #include "main-func.h"
14 #include "parse-util.h"
15 #include "path-util.h"
16 #include "pretty-print.h"
17 #include "terminal-util.h"
22 static char **arg_path
= NULL
;
24 STATIC_DESTRUCTOR_REGISTER(arg_path
, strv_freep
);
26 static int help(int argc
, char *argv
[], void *userdata
) {
27 _cleanup_free_
char *link
= NULL
;
30 r
= terminal_urlify_man("systemd-bless-boot.service", "8", &link
);
34 printf("%s [OPTIONS...] COMMAND\n"
35 "\n%sMark the boot process as good or bad.%s\n"
37 " status Show status of current boot loader entry\n"
38 " good Mark this boot as good\n"
39 " bad Mark this boot as bad\n"
40 " indeterminate Undo any marking as good or bad\n"
42 " -h --help Show this help\n"
43 " --version Print version\n"
44 " --path=PATH Path to the $BOOT partition (may be used multiple times)\n"
45 "\nSee the %s for details.\n"
46 , program_invocation_short_name
55 static int parse_argv(int argc
, char *argv
[]) {
61 static const struct option options
[] = {
62 { "help", no_argument
, NULL
, 'h' },
63 { "version", no_argument
, NULL
, ARG_VERSION
},
64 { "path", required_argument
, NULL
, ARG_PATH
},
73 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
84 r
= strv_extend(&arg_path
, optarg
);
93 assert_not_reached("Unknown option");
99 static int acquire_path(void) {
100 _cleanup_free_
char *esp_path
= NULL
, *xbootldr_path
= NULL
;
104 if (!strv_isempty(arg_path
))
107 r
= find_esp_and_warn(NULL
, false, &esp_path
, NULL
, NULL
, NULL
, NULL
);
108 if (r
< 0 && r
!= -ENOKEY
) /* ENOKEY means not found, and is the only error the function won't log about on its own */
111 r
= find_xbootldr_and_warn(NULL
, false, &xbootldr_path
, NULL
);
112 if (r
< 0 && r
!= -ENOKEY
)
115 if (!esp_path
&& !xbootldr_path
)
116 return log_error_errno(SYNTHETIC_ERRNO(ENOENT
),
117 "Couldn't find $BOOT partition. It is recommended to mount it to /boot.\n"
118 "Alternatively, use --path= to specify path to mount point.");
121 a
= strv_new(esp_path
, xbootldr_path
);
123 a
= strv_new(xbootldr_path
);
127 strv_free_and_replace(arg_path
, a
);
130 _cleanup_free_
char *j
;
132 j
= strv_join(arg_path
, ":");
133 log_debug("Using %s as boot loader drop-in search path.", j
);
139 static int parse_counter(
143 uint64_t *ret_done
) {
159 k
= strspn(e
, DIGITS
);
161 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
162 "Can't parse empty 'tries left' counter from LoaderBootCountPath: %s",
166 r
= safe_atou64(z
, &left
);
168 return log_error_errno(r
, "Failed to parse 'tries left' counter from LoaderBootCountPath: %s", path
);
175 k
= strspn(e
, DIGITS
);
176 if (k
== 0) /* If there's a "-" there also needs to be at least one digit */
177 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
178 "Can't parse empty 'tries done' counter from LoaderBootCountPath: %s",
182 r
= safe_atou64(z
, &done
);
184 return log_error_errno(r
, "Failed to parse 'tries done' counter from LoaderBootCountPath: %s", path
);
191 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.");
204 static int acquire_boot_count_path(
211 _cleanup_free_
char *path
= NULL
, *prefix
= NULL
, *suffix
= NULL
;
212 const char *last
, *e
;
216 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderBootCountPath", &path
);
218 return -EUNATCH
; /* in this case, let the caller print a message */
220 return log_error_errno(r
, "Failed to read LoaderBootCountPath EFI variable: %m");
222 efi_tilt_backslashes(path
);
224 if (!path_is_normalized(path
))
225 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
226 "Path read from LoaderBootCountPath is not normalized, refusing: %s",
229 if (!path_is_absolute(path
))
230 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
231 "Path read from LoaderBootCountPath is not absolute, refusing: %s",
234 last
= last_path_component(path
);
235 e
= strrchr(last
, '+');
237 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
238 "Path read from LoaderBootCountPath does not contain a counter, refusing: %s",
242 prefix
= strndup(path
, e
- path
);
247 r
= parse_counter(path
, &e
, &left
, &done
);
256 *ret_suffix
= TAKE_PTR(suffix
);
260 *ret_path
= TAKE_PTR(path
);
262 *ret_prefix
= TAKE_PTR(prefix
);
271 static int make_good(const char *prefix
, const char *suffix
, char **ret
) {
272 _cleanup_free_
char *good
= NULL
;
278 /* Generate the path we'd use on good boots. This one is easy. If we are successful, we simple drop the counter
279 * pair entirely from the name. After all, we know all is good, and the logs will contain information about the
280 * tries we needed to come here, hence it's safe to drop the counters from the name. */
282 good
= strjoin(prefix
, suffix
);
286 *ret
= TAKE_PTR(good
);
290 static int make_bad(const char *prefix
, uint64_t done
, const char *suffix
, char **ret
) {
291 _cleanup_free_
char *bad
= NULL
;
297 /* Generate the path we'd use on bad boots. Let's simply set the 'left' counter to zero, and keep the 'done'
298 * counter. The information might be interesting to boot loaders, after all. */
301 bad
= strjoin(prefix
, "+0", suffix
);
305 if (asprintf(&bad
, "%s+0-%" PRIu64
"%s", prefix
, done
, suffix
) < 0)
309 *ret
= TAKE_PTR(bad
);
313 static const char *skip_slash(const char *path
) {
315 assert(path
[0] == '/');
320 static int verb_status(int argc
, char *argv
[], void *userdata
) {
321 _cleanup_free_
char *path
= NULL
, *prefix
= NULL
, *suffix
= NULL
, *good
= NULL
, *bad
= NULL
;
326 r
= acquire_boot_count_path(&path
, &prefix
, &left
, &done
, &suffix
);
327 if (r
== -EUNATCH
) { /* No boot count in place, then let's consider this a "clean" boot, as "good", "bad" or "indeterminate" don't apply. */
338 r
= make_good(prefix
, suffix
, &good
);
342 r
= make_bad(prefix
, done
, suffix
, &bad
);
346 log_debug("Booted file: %s\n"
347 "The same modified for 'good': %s\n"
348 "The same modified for 'bad': %s\n",
353 log_debug("Tries left: %" PRIu64
"\n"
354 "Tries done: %" PRIu64
"\n",
357 STRV_FOREACH(p
, arg_path
) {
358 _cleanup_close_
int fd
= -1;
360 fd
= open(*p
, O_DIRECTORY
|O_CLOEXEC
|O_RDONLY
);
365 return log_error_errno(errno
, "Failed to open $BOOT partition '%s': %m", *p
);
368 if (faccessat(fd
, skip_slash(path
), F_OK
, 0) >= 0) {
369 puts("indeterminate");
373 return log_error_errno(errno
, "Failed to check if '%s' exists: %m", path
);
375 if (faccessat(fd
, skip_slash(good
), F_OK
, 0) >= 0) {
381 return log_error_errno(errno
, "Failed to check if '%s' exists: %m", good
);
383 if (faccessat(fd
, skip_slash(bad
), F_OK
, 0) >= 0) {
388 return log_error_errno(errno
, "Failed to check if '%s' exists: %m", bad
);
390 /* We didn't find any of the three? If so, let's try the next directory, before we give up. */
393 return log_error_errno(SYNTHETIC_ERRNO(EBUSY
), "Couldn't determine boot state: %m");
396 static int verb_set(int argc
, char *argv
[], void *userdata
) {
397 _cleanup_free_
char *path
= NULL
, *prefix
= NULL
, *suffix
= NULL
, *good
= NULL
, *bad
= NULL
, *parent
= NULL
;
398 const char *target
, *source1
, *source2
;
403 r
= acquire_boot_count_path(&path
, &prefix
, NULL
, &done
, &suffix
);
404 if (r
== -EUNATCH
) /* acquire_boot_count_path() won't log on its own for this specific error */
405 return log_error_errno(r
, "Not booted with boot counting in effect.");
413 r
= make_good(prefix
, suffix
, &good
);
417 r
= make_bad(prefix
, done
, suffix
, &bad
);
421 /* Figure out what rename to what */
422 if (streq(argv
[0], "good")) {
425 source2
= bad
; /* Maybe this boot was previously marked as 'bad'? */
426 } else if (streq(argv
[0], "bad")) {
429 source2
= good
; /* Maybe this boot was previously marked as 'good'? */
431 assert(streq(argv
[0], "indeterminate"));
437 STRV_FOREACH(p
, arg_path
) {
438 _cleanup_close_
int fd
= -1;
440 fd
= open(*p
, O_DIRECTORY
|O_CLOEXEC
|O_RDONLY
);
442 return log_error_errno(errno
, "Failed to open $BOOT partition '%s': %m", *p
);
444 r
= rename_noreplace(fd
, skip_slash(source1
), fd
, skip_slash(target
));
447 else if (r
== -ENOENT
) {
449 r
= rename_noreplace(fd
, skip_slash(source2
), fd
, skip_slash(target
));
452 else if (r
== -ENOENT
) {
454 if (faccessat(fd
, skip_slash(target
), F_OK
, 0) >= 0) /* Hmm, if we can't find either source file, maybe the destination already exists? */
458 return log_error_errno(errno
, "Failed to determine if %s already exists: %m", target
);
460 /* We found none of the snippets here, try the next directory */
463 return log_error_errno(r
, "Failed to rename '%s' to '%s': %m", source2
, target
);
465 log_debug("Successfully renamed '%s' to '%s'.", source2
, target
);
468 return log_error_errno(r
, "Failed to rename '%s' to '%s': %m", source1
, target
);
470 log_debug("Successfully renamed '%s' to '%s'.", source1
, target
);
472 /* First, fsync() the directory these files are located in */
473 parent
= dirname_malloc(target
);
477 r
= fsync_path_at(fd
, skip_slash(parent
));
479 log_debug_errno(errno
, "Failed to synchronize image directory, ignoring: %m");
481 /* Secondly, syncfs() the whole file system these files are located in */
483 log_debug_errno(errno
, "Failed to synchronize $BOOT partition, ignoring: %m");
485 log_info("Marked boot as '%s'. (Boot attempt counter is at %" PRIu64
".)", argv
[0], done
);
488 log_error_errno(SYNTHETIC_ERRNO(EBUSY
), "Can't find boot counter source file for '%s': %m", target
);
492 log_debug("Operation already executed before, not doing anything.");
496 static int run(int argc
, char *argv
[]) {
497 static const Verb verbs
[] = {
498 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
499 { "status", VERB_ANY
, 1, VERB_DEFAULT
, verb_status
},
500 { "good", VERB_ANY
, 1, 0, verb_set
},
501 { "bad", VERB_ANY
, 1, 0, verb_set
},
502 { "indeterminate", VERB_ANY
, 1, 0, verb_set
},
508 log_parse_environment();
511 r
= parse_argv(argc
, argv
);
515 if (detect_container() > 0)
516 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
517 "Marking a boot is not supported in containers.");
520 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
521 "Marking a boot is only supported on EFI systems.");
523 return dispatch_verb(argc
, argv
, verbs
, NULL
);
526 DEFINE_MAIN_FUNCTION(run
);