1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 " good Mark this boot as good\n"
38 " bad Mark this boot as bad\n"
39 " indeterminate Undo any marking as good or bad\n"
41 " -h --help Show this help\n"
42 " --version Print version\n"
43 " --path=PATH Path to the $BOOT partition (may be used multiple times)\n"
44 "\nSee the %s for details.\n"
45 , program_invocation_short_name
54 static int parse_argv(int argc
, char *argv
[]) {
60 static const struct option options
[] = {
61 { "help", no_argument
, NULL
, 'h' },
62 { "version", no_argument
, NULL
, ARG_VERSION
},
63 { "path", required_argument
, NULL
, ARG_PATH
},
72 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
83 r
= strv_extend(&arg_path
, optarg
);
92 assert_not_reached("Unknown option");
98 static int acquire_path(void) {
99 _cleanup_free_
char *esp_path
= NULL
, *xbootldr_path
= NULL
;
103 if (!strv_isempty(arg_path
))
106 r
= find_esp_and_warn(NULL
, false, &esp_path
, NULL
, NULL
, NULL
, NULL
);
107 if (r
< 0 && r
!= -ENOKEY
) /* ENOKEY means not found, and is the only error the function won't log about on its own */
110 r
= find_xbootldr_and_warn(NULL
, false, &xbootldr_path
, NULL
);
111 if (r
< 0 && r
!= -ENOKEY
)
114 if (!esp_path
&& !xbootldr_path
)
115 return log_error_errno(SYNTHETIC_ERRNO(ENOENT
),
116 "Couldn't find $BOOT partition. It is recommended to mount it to /boot.\n"
117 "Alternatively, use --path= to specify path to mount point.");
120 a
= strv_new(esp_path
, xbootldr_path
);
122 a
= strv_new(xbootldr_path
);
126 strv_free_and_replace(arg_path
, a
);
129 _cleanup_free_
char *j
;
131 j
= strv_join(arg_path
, ":");
132 log_debug("Using %s as boot loader drop-in search path.", j
);
138 static int parse_counter(
142 uint64_t *ret_done
) {
158 k
= strspn(e
, DIGITS
);
160 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
161 "Can't parse empty 'tries left' counter from LoaderBootCountPath: %s",
165 r
= safe_atou64(z
, &left
);
167 return log_error_errno(r
, "Failed to parse 'tries left' counter from LoaderBootCountPath: %s", path
);
174 k
= strspn(e
, DIGITS
);
175 if (k
== 0) /* If there's a "-" there also needs to be at least one digit */
176 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
177 "Can't parse empty 'tries done' counter from LoaderBootCountPath: %s",
181 r
= safe_atou64(z
, &done
);
183 return log_error_errno(r
, "Failed to parse 'tries done' counter from LoaderBootCountPath: %s", path
);
190 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.");
203 static int acquire_boot_count_path(
210 _cleanup_free_
char *path
= NULL
, *prefix
= NULL
, *suffix
= NULL
;
211 const char *last
, *e
;
215 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderBootCountPath", &path
);
217 return -EUNATCH
; /* in this case, let the caller print a message */
219 return log_error_errno(r
, "Failed to read LoaderBootCountPath EFI variable: %m");
221 efi_tilt_backslashes(path
);
223 if (!path_is_normalized(path
))
224 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
225 "Path read from LoaderBootCountPath is not normalized, refusing: %s",
228 if (!path_is_absolute(path
))
229 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
230 "Path read from LoaderBootCountPath is not absolute, refusing: %s",
233 last
= last_path_component(path
);
234 e
= strrchr(last
, '+');
236 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
237 "Path read from LoaderBootCountPath does not contain a counter, refusing: %s",
241 prefix
= strndup(path
, e
- path
);
246 r
= parse_counter(path
, &e
, &left
, &done
);
255 *ret_suffix
= TAKE_PTR(suffix
);
259 *ret_path
= TAKE_PTR(path
);
261 *ret_prefix
= TAKE_PTR(prefix
);
270 static int make_good(const char *prefix
, const char *suffix
, char **ret
) {
271 _cleanup_free_
char *good
= NULL
;
277 /* Generate the path we'd use on good boots. This one is easy. If we are successful, we simple drop the counter
278 * pair entirely from the name. After all, we know all is good, and the logs will contain information about the
279 * tries we needed to come here, hence it's safe to drop the counters from the name. */
281 good
= strjoin(prefix
, suffix
);
285 *ret
= TAKE_PTR(good
);
289 static int make_bad(const char *prefix
, uint64_t done
, const char *suffix
, char **ret
) {
290 _cleanup_free_
char *bad
= NULL
;
296 /* Generate the path we'd use on bad boots. Let's simply set the 'left' counter to zero, and keep the 'done'
297 * counter. The information might be interesting to boot loaders, after all. */
300 bad
= strjoin(prefix
, "+0", suffix
);
304 if (asprintf(&bad
, "%s+0-%" PRIu64
"%s", prefix
, done
, suffix
) < 0)
308 *ret
= TAKE_PTR(bad
);
312 static const char *skip_slash(const char *path
) {
314 assert(path
[0] == '/');
319 static int verb_status(int argc
, char *argv
[], void *userdata
) {
320 _cleanup_free_
char *path
= NULL
, *prefix
= NULL
, *suffix
= NULL
, *good
= NULL
, *bad
= NULL
;
325 r
= acquire_boot_count_path(&path
, &prefix
, &left
, &done
, &suffix
);
326 if (r
== -EUNATCH
) { /* No boot count in place, then let's consider this a "clean" boot, as "good", "bad" or "indeterminate" don't apply. */
337 r
= make_good(prefix
, suffix
, &good
);
341 r
= make_bad(prefix
, done
, suffix
, &bad
);
345 log_debug("Booted file: %s\n"
346 "The same modified for 'good': %s\n"
347 "The same modified for 'bad': %s\n",
352 log_debug("Tries left: %" PRIu64
"\n"
353 "Tries done: %" PRIu64
"\n",
356 STRV_FOREACH(p
, arg_path
) {
357 _cleanup_close_
int fd
= -1;
359 fd
= open(*p
, O_DIRECTORY
|O_CLOEXEC
|O_RDONLY
);
364 return log_error_errno(errno
, "Failed to open $BOOT partition '%s': %m", *p
);
367 if (faccessat(fd
, skip_slash(path
), F_OK
, 0) >= 0) {
368 puts("indeterminate");
372 return log_error_errno(errno
, "Failed to check if '%s' exists: %m", path
);
374 if (faccessat(fd
, skip_slash(good
), F_OK
, 0) >= 0) {
380 return log_error_errno(errno
, "Failed to check if '%s' exists: %m", good
);
382 if (faccessat(fd
, skip_slash(bad
), F_OK
, 0) >= 0) {
387 return log_error_errno(errno
, "Failed to check if '%s' exists: %m", bad
);
389 /* We didn't find any of the three? If so, let's try the next directory, before we give up. */
392 return log_error_errno(SYNTHETIC_ERRNO(EBUSY
), "Couldn't determine boot state: %m");
395 static int verb_set(int argc
, char *argv
[], void *userdata
) {
396 _cleanup_free_
char *path
= NULL
, *prefix
= NULL
, *suffix
= NULL
, *good
= NULL
, *bad
= NULL
, *parent
= NULL
;
397 const char *target
, *source1
, *source2
;
402 r
= acquire_boot_count_path(&path
, &prefix
, NULL
, &done
, &suffix
);
403 if (r
== -EUNATCH
) /* acquire_boot_count_path() won't log on its own for this specific error */
404 return log_error_errno(r
, "Not booted with boot counting in effect.");
412 r
= make_good(prefix
, suffix
, &good
);
416 r
= make_bad(prefix
, done
, suffix
, &bad
);
420 /* Figure out what rename to what */
421 if (streq(argv
[0], "good")) {
424 source2
= bad
; /* Maybe this boot was previously marked as 'bad'? */
425 } else if (streq(argv
[0], "bad")) {
428 source2
= good
; /* Maybe this boot was previously marked as 'good'? */
430 assert(streq(argv
[0], "indeterminate"));
436 STRV_FOREACH(p
, arg_path
) {
437 _cleanup_close_
int fd
= -1;
439 fd
= open(*p
, O_DIRECTORY
|O_CLOEXEC
|O_RDONLY
);
441 return log_error_errno(errno
, "Failed to open $BOOT partition '%s': %m", *p
);
443 r
= rename_noreplace(fd
, skip_slash(source1
), fd
, skip_slash(target
));
446 else if (r
== -ENOENT
) {
448 r
= rename_noreplace(fd
, skip_slash(source2
), fd
, skip_slash(target
));
451 else if (r
== -ENOENT
) {
453 if (faccessat(fd
, skip_slash(target
), F_OK
, 0) >= 0) /* Hmm, if we can't find either source file, maybe the destination already exists? */
457 return log_error_errno(errno
, "Failed to determine if %s already exists: %m", target
);
459 /* We found none of the snippets here, try the next directory */
462 return log_error_errno(r
, "Failed to rename '%s' to '%s': %m", source2
, target
);
464 log_debug("Successfully renamed '%s' to '%s'.", source2
, target
);
467 return log_error_errno(r
, "Failed to rename '%s' to '%s': %m", source1
, target
);
469 log_debug("Successfully renamed '%s' to '%s'.", source1
, target
);
471 /* First, fsync() the directory these files are located in */
472 parent
= dirname_malloc(target
);
476 r
= fsync_path_at(fd
, skip_slash(parent
));
478 log_debug_errno(errno
, "Failed to synchronize image directory, ignoring: %m");
480 /* Secondly, syncfs() the whole file system these files are located in */
482 log_debug_errno(errno
, "Failed to synchronize $BOOT partition, ignoring: %m");
484 log_info("Marked boot as '%s'. (Boot attempt counter is at %" PRIu64
".)", argv
[0], done
);
487 log_error_errno(SYNTHETIC_ERRNO(EBUSY
), "Can't find boot counter source file for '%s': %m", target
);
491 log_debug("Operation already executed before, not doing anything.");
495 static int run(int argc
, char *argv
[]) {
496 static const Verb verbs
[] = {
497 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
498 { "status", VERB_ANY
, 1, VERB_DEFAULT
, verb_status
},
499 { "good", VERB_ANY
, 1, 0, verb_set
},
500 { "bad", VERB_ANY
, 1, 0, verb_set
},
501 { "indeterminate", VERB_ANY
, 1, 0, verb_set
},
507 log_parse_environment();
510 r
= parse_argv(argc
, argv
);
514 if (detect_container() > 0)
515 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
516 "Marking a boot is not supported in containers.");
519 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
520 "Marking a boot is only supported on EFI systems.");
522 return dispatch_verb(argc
, argv
, verbs
, NULL
);
525 DEFINE_MAIN_FUNCTION(run
);