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
, strv_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 $BOOT partition (may be used multiple times)\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
= strv_extend(&arg_path
, optarg
);
79 assert_not_reached("Unknown option");
85 static int acquire_path(void) {
86 _cleanup_free_
char *esp_path
= NULL
, *xbootldr_path
= NULL
;
90 if (!strv_isempty(arg_path
))
93 r
= find_esp_and_warn(NULL
, false, &esp_path
, NULL
, NULL
, NULL
, NULL
);
94 if (r
< 0 && r
!= -ENOKEY
) /* ENOKEY means not found, and is the only error the function won't log about on its own */
97 r
= find_xbootldr_and_warn(NULL
, false, &xbootldr_path
, NULL
);
98 if (r
< 0 && r
!= -ENOKEY
)
101 if (!esp_path
&& !xbootldr_path
)
102 return log_error_errno(SYNTHETIC_ERRNO(ENOENT
),
103 "Couldn't find $BOOT partition. It is recommended to mount it to /boot.\n"
104 "Alternatively, use --path= to specify path to mount point.");
107 a
= strv_new(esp_path
, xbootldr_path
);
109 a
= strv_new(xbootldr_path
);
113 strv_free_and_replace(arg_path
, a
);
116 _cleanup_free_
char *j
;
118 j
= strv_join(arg_path
, ":");
119 log_debug("Using %s as boot loader drop-in search path.", j
);
125 static int parse_counter(
129 uint64_t *ret_done
) {
145 k
= strspn(e
, DIGITS
);
147 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
148 "Can't parse empty 'tries left' counter from LoaderBootCountPath: %s",
152 r
= safe_atou64(z
, &left
);
154 return log_error_errno(r
, "Failed to parse 'tries left' counter from LoaderBootCountPath: %s", path
);
161 k
= strspn(e
, DIGITS
);
162 if (k
== 0) /* If there's a "-" there also needs to be at least one digit */
163 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
164 "Can't parse empty 'tries done' counter from LoaderBootCountPath: %s",
168 r
= safe_atou64(z
, &done
);
170 return log_error_errno(r
, "Failed to parse 'tries done' counter from LoaderBootCountPath: %s", path
);
177 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.");
190 static int acquire_boot_count_path(
197 _cleanup_free_
char *path
= NULL
, *prefix
= NULL
, *suffix
= NULL
;
198 const char *last
, *e
;
202 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderBootCountPath", &path
);
204 return -EUNATCH
; /* in this case, let the caller print a message */
206 return log_error_errno(r
, "Failed to read LoaderBootCountPath EFI variable: %m");
208 efi_tilt_backslashes(path
);
210 if (!path_is_normalized(path
))
211 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
212 "Path read from LoaderBootCountPath is not normalized, refusing: %s",
215 if (!path_is_absolute(path
))
216 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
217 "Path read from LoaderBootCountPath is not absolute, refusing: %s",
220 last
= last_path_component(path
);
221 e
= strrchr(last
, '+');
223 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
224 "Path read from LoaderBootCountPath does not contain a counter, refusing: %s",
228 prefix
= strndup(path
, e
- path
);
233 r
= parse_counter(path
, &e
, &left
, &done
);
242 *ret_suffix
= TAKE_PTR(suffix
);
246 *ret_path
= TAKE_PTR(path
);
248 *ret_prefix
= TAKE_PTR(prefix
);
257 static int make_good(const char *prefix
, const char *suffix
, char **ret
) {
258 _cleanup_free_
char *good
= NULL
;
264 /* Generate the path we'd use on good boots. This one is easy. If we are successful, we simple drop the counter
265 * pair entirely from the name. After all, we know all is good, and the logs will contain information about the
266 * tries we needed to come here, hence it's safe to drop the counters from the name. */
268 good
= strjoin(prefix
, suffix
);
272 *ret
= TAKE_PTR(good
);
276 static int make_bad(const char *prefix
, uint64_t done
, const char *suffix
, char **ret
) {
277 _cleanup_free_
char *bad
= NULL
;
283 /* Generate the path we'd use on bad boots. Let's simply set the 'left' counter to zero, and keep the 'done'
284 * counter. The information might be interesting to boot loaders, after all. */
287 bad
= strjoin(prefix
, "+0", suffix
);
291 if (asprintf(&bad
, "%s+0-%" PRIu64
"%s", prefix
, done
, suffix
) < 0)
295 *ret
= TAKE_PTR(bad
);
299 static const char *skip_slash(const char *path
) {
301 assert(path
[0] == '/');
306 static int verb_status(int argc
, char *argv
[], void *userdata
) {
307 _cleanup_free_
char *path
= NULL
, *prefix
= NULL
, *suffix
= NULL
, *good
= NULL
, *bad
= NULL
;
312 r
= acquire_boot_count_path(&path
, &prefix
, &left
, &done
, &suffix
);
313 if (r
== -EUNATCH
) { /* No boot count in place, then let's consider this a "clean" boot, as "good", "bad" or "indeterminate" don't apply. */
324 r
= make_good(prefix
, suffix
, &good
);
328 r
= make_bad(prefix
, done
, suffix
, &bad
);
332 log_debug("Booted file: %s\n"
333 "The same modified for 'good': %s\n"
334 "The same modified for 'bad': %s\n",
339 log_debug("Tries left: %" PRIu64
"\n"
340 "Tries done: %" PRIu64
"\n",
343 STRV_FOREACH(p
, arg_path
) {
344 _cleanup_close_
int fd
= -1;
346 fd
= open(*p
, O_DIRECTORY
|O_CLOEXEC
|O_RDONLY
);
351 return log_error_errno(errno
, "Failed to open $BOOT partition '%s': %m", *p
);
354 if (faccessat(fd
, skip_slash(path
), F_OK
, 0) >= 0) {
355 puts("indeterminate");
359 return log_error_errno(errno
, "Failed to check if '%s' exists: %m", path
);
361 if (faccessat(fd
, skip_slash(good
), F_OK
, 0) >= 0) {
367 return log_error_errno(errno
, "Failed to check if '%s' exists: %m", good
);
369 if (faccessat(fd
, skip_slash(bad
), F_OK
, 0) >= 0) {
374 return log_error_errno(errno
, "Failed to check if '%s' exists: %m", bad
);
376 /* We didn't find any of the three? If so, let's try the next directory, before we give up. */
379 return log_error_errno(SYNTHETIC_ERRNO(EBUSY
), "Couldn't determine boot state: %m");
382 static int verb_set(int argc
, char *argv
[], void *userdata
) {
383 _cleanup_free_
char *path
= NULL
, *prefix
= NULL
, *suffix
= NULL
, *good
= NULL
, *bad
= NULL
, *parent
= NULL
;
384 const char *target
, *source1
, *source2
;
389 r
= acquire_boot_count_path(&path
, &prefix
, NULL
, &done
, &suffix
);
390 if (r
== -EUNATCH
) /* acquire_boot_count_path() won't log on its own for this specific error */
391 return log_error_errno(r
, "Not booted with boot counting in effect.");
399 r
= make_good(prefix
, suffix
, &good
);
403 r
= make_bad(prefix
, done
, suffix
, &bad
);
407 /* Figure out what rename to what */
408 if (streq(argv
[0], "good")) {
411 source2
= bad
; /* Maybe this boot was previously marked as 'bad'? */
412 } else if (streq(argv
[0], "bad")) {
415 source2
= good
; /* Maybe this boot was previously marked as 'good'? */
417 assert(streq(argv
[0], "indeterminate"));
423 STRV_FOREACH(p
, arg_path
) {
424 _cleanup_close_
int fd
= -1;
426 fd
= open(*p
, O_DIRECTORY
|O_CLOEXEC
|O_RDONLY
);
428 return log_error_errno(errno
, "Failed to open $BOOT partition '%s': %m", *p
);
430 r
= rename_noreplace(fd
, skip_slash(source1
), fd
, skip_slash(target
));
433 else if (r
== -ENOENT
) {
435 r
= rename_noreplace(fd
, skip_slash(source2
), fd
, skip_slash(target
));
438 else if (r
== -ENOENT
) {
440 if (faccessat(fd
, skip_slash(target
), F_OK
, 0) >= 0) /* Hmm, if we can't find either source file, maybe the destination already exists? */
444 return log_error_errno(errno
, "Failed to determine if %s already exists: %m", target
);
446 /* We found none of the snippets here, try the next directory */
449 return log_error_errno(r
, "Failed to rename '%s' to '%s': %m", source2
, target
);
451 log_debug("Successfully renamed '%s' to '%s'.", source2
, target
);
454 return log_error_errno(r
, "Failed to rename '%s' to '%s': %m", source1
, target
);
456 log_debug("Successfully renamed '%s' to '%s'.", source1
, target
);
458 /* First, fsync() the directory these files are located in */
459 parent
= dirname_malloc(target
);
463 r
= fsync_path_at(fd
, skip_slash(parent
));
465 log_debug_errno(errno
, "Failed to synchronize image directory, ignoring: %m");
467 /* Secondly, syncfs() the whole file system these files are located in */
469 log_debug_errno(errno
, "Failed to synchronize $BOOT partition, ignoring: %m");
471 log_info("Marked boot as '%s'. (Boot attempt counter is at %" PRIu64
".)", argv
[0], done
);
474 log_error_errno(SYNTHETIC_ERRNO(EBUSY
), "Can't find boot counter source file for '%s': %m", target
);
478 log_debug("Operation already executed before, not doing anything.");
482 static int run(int argc
, char *argv
[]) {
484 static const Verb verbs
[] = {
485 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
486 { "status", VERB_ANY
, 1, VERB_DEFAULT
, verb_status
},
487 { "good", VERB_ANY
, 1, VERB_MUST_BE_ROOT
, verb_set
},
488 { "bad", VERB_ANY
, 1, VERB_MUST_BE_ROOT
, verb_set
},
489 { "indeterminate", VERB_ANY
, 1, VERB_MUST_BE_ROOT
, verb_set
},
495 log_parse_environment();
498 r
= parse_argv(argc
, argv
);
502 if (detect_container() > 0)
503 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
504 "Marking a boot is not supported in containers.");
507 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
508 "Marking a boot is only supported on EFI systems.");
510 return dispatch_verb(argc
, argv
, verbs
, NULL
);
513 DEFINE_MAIN_FUNCTION(run
);