| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <uchar.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "bootctl.h" |
| 7 |
#include "bootctl-set-efivar.h" |
| 8 |
#include "bootctl-util.h" |
| 9 |
#include "efi-loader.h" |
| 10 |
#include "efivars.h" |
| 11 |
#include "log.h" |
| 12 |
#include "stdio-util.h" |
| 13 |
#include "time-util.h" |
| 14 |
#include "utf8.h" |
| 15 |
|
| 16 |
static int parse_timeout(const char *arg1, char16_t **ret_timeout, size_t *ret_timeout_size) { |
| 17 |
char buf[DECIMAL_STR_MAX(usec_t)]; |
| 18 |
usec_t timeout; |
| 19 |
uint64_t loader_features = 0; |
| 20 |
int r; |
| 21 |
|
| 22 |
assert(arg1); |
| 23 |
assert(ret_timeout); |
| 24 |
assert(ret_timeout_size); |
| 25 |
|
| 26 |
assert_cc(STRLEN("menu-force") < ELEMENTSOF(buf)); |
| 27 |
assert_cc(STRLEN("menu-hidden") < ELEMENTSOF(buf)); |
| 28 |
assert_cc(STRLEN("menu-disabled") < ELEMENTSOF(buf)); |
| 29 |
|
| 30 |
/* Use feature EFI_LOADER_FEATURE_MENU_DISABLE as a mark that the boot loader supports the other |
| 31 |
* string values too. When unsupported, convert to the timeout with the closest meaning. |
| 32 |
*/ |
| 33 |
|
| 34 |
if (streq(arg1, "menu-force")) { |
| 35 |
(void) efi_loader_get_features(&loader_features); |
| 36 |
|
| 37 |
if (!(loader_features & EFI_LOADER_FEATURE_MENU_DISABLE)) { |
| 38 |
log_debug("Using maximum timeout instead of '%s'.", arg1); |
| 39 |
timeout = USEC_INFINITY; |
| 40 |
arg1 = NULL; |
| 41 |
} |
| 42 |
|
| 43 |
} else if (streq(arg1, "menu-hidden")) { |
| 44 |
(void) efi_loader_get_features(&loader_features); |
| 45 |
|
| 46 |
if (!(loader_features & EFI_LOADER_FEATURE_MENU_DISABLE)) { |
| 47 |
log_debug("Using zero timeout instead of '%s'.", arg1); |
| 48 |
timeout = 0; |
| 49 |
arg1 = NULL; /* replace the arg by printed timeout value later */ |
| 50 |
} |
| 51 |
|
| 52 |
} else if (streq(arg1, "menu-disabled")) { |
| 53 |
(void) efi_loader_get_features(&loader_features); |
| 54 |
|
| 55 |
if (!(loader_features & EFI_LOADER_FEATURE_MENU_DISABLE)) { |
| 56 |
if (arg_graceful() == ARG_GRACEFUL_NO) |
| 57 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 58 |
"Loader does not support '%s'.", arg1); |
| 59 |
log_warning("Using zero timeout instead of '%s'.", arg1); |
| 60 |
timeout = 0; |
| 61 |
arg1 = NULL; |
| 62 |
} |
| 63 |
|
| 64 |
} else { |
| 65 |
r = parse_time(arg1, &timeout, USEC_PER_SEC); |
| 66 |
if (r < 0) |
| 67 |
return log_error_errno(r, "Failed to parse timeout '%s': %m", arg1); |
| 68 |
|
| 69 |
assert_cc(USEC_INFINITY > UINT32_MAX * USEC_PER_SEC); |
| 70 |
if (timeout > UINT32_MAX * USEC_PER_SEC && timeout != USEC_INFINITY) |
| 71 |
log_debug("Timeout is too long and will be clamped to maximum timeout."); |
| 72 |
|
| 73 |
arg1 = NULL; |
| 74 |
} |
| 75 |
|
| 76 |
if (!arg1) { |
| 77 |
timeout = DIV_ROUND_UP(timeout, USEC_PER_SEC); |
| 78 |
xsprintf(buf, USEC_FMT, MIN(timeout, UINT32_MAX)); |
| 79 |
} |
| 80 |
|
| 81 |
char16_t *encoded = utf8_to_utf16(arg1 ?: buf, SIZE_MAX); |
| 82 |
if (!encoded) |
| 83 |
return log_oom(); |
| 84 |
|
| 85 |
*ret_timeout = encoded; |
| 86 |
*ret_timeout_size = char16_strlen(encoded) * 2 + 2; |
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
static int parse_loader_entry_target_arg(const char *arg1, char16_t **ret_target, size_t *ret_target_size) { |
| 91 |
char16_t *encoded = NULL; |
| 92 |
int r; |
| 93 |
|
| 94 |
assert(arg1); |
| 95 |
assert(ret_target); |
| 96 |
assert(ret_target_size); |
| 97 |
|
| 98 |
if (streq(arg1, "@current")) { |
| 99 |
r = efi_get_variable(EFI_LOADER_VARIABLE_STR("LoaderEntrySelected"), NULL, (void *) ret_target, ret_target_size); |
| 100 |
if (r < 0) |
| 101 |
return log_error_errno(r, "Failed to get EFI variable 'LoaderEntrySelected': %m"); |
| 102 |
|
| 103 |
} else if (streq(arg1, "@oneshot")) { |
| 104 |
r = efi_get_variable(EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"), NULL, (void *) ret_target, ret_target_size); |
| 105 |
if (r < 0) |
| 106 |
return log_error_errno(r, "Failed to get EFI variable 'LoaderEntryOneShot': %m"); |
| 107 |
|
| 108 |
} else if (streq(arg1, "@default")) { |
| 109 |
r = efi_get_variable(EFI_LOADER_VARIABLE_STR("LoaderEntryDefault"), NULL, (void *) ret_target, ret_target_size); |
| 110 |
if (r < 0) |
| 111 |
return log_error_errno(r, "Failed to get EFI variable 'LoaderEntryDefault': %m"); |
| 112 |
|
| 113 |
} else if (streq(arg1, "@sysfail")) { |
| 114 |
r = efi_get_variable(EFI_LOADER_VARIABLE_STR("LoaderEntrySysFail"), NULL, (void *) ret_target, ret_target_size); |
| 115 |
if (r < 0) |
| 116 |
return log_error_errno(r, "Failed to get EFI variable 'LoaderEntrySysFail': %m"); |
| 117 |
|
| 118 |
} else if (arg1[0] == '@' && !streq(arg1, "@saved")) |
| 119 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unsupported special entry identifier: %s", arg1); |
| 120 |
else { |
| 121 |
encoded = utf8_to_utf16(arg1, SIZE_MAX); |
| 122 |
if (!encoded) |
| 123 |
return log_oom(); |
| 124 |
|
| 125 |
*ret_target = encoded; |
| 126 |
*ret_target_size = char16_strlen(encoded) * 2 + 2; |
| 127 |
} |
| 128 |
|
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
int verb_set_efivar(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 133 |
int r; |
| 134 |
|
| 135 |
r = verify_touch_variables_allowed(argv[0]); |
| 136 |
if (r < 0) |
| 137 |
return r; |
| 138 |
|
| 139 |
const char *variable; |
| 140 |
int (* arg_parser)(const char *, char16_t **, size_t *); |
| 141 |
|
| 142 |
if (streq(argv[0], "set-default")) { |
| 143 |
variable = EFI_LOADER_VARIABLE_STR("LoaderEntryDefault"); |
| 144 |
arg_parser = parse_loader_entry_target_arg; |
| 145 |
} else if (streq(argv[0], "set-preferred")) { |
| 146 |
variable = EFI_LOADER_VARIABLE_STR("LoaderEntryPreferred"); |
| 147 |
arg_parser = parse_loader_entry_target_arg; |
| 148 |
} else if (streq(argv[0], "set-sysfail")) { |
| 149 |
variable = EFI_LOADER_VARIABLE_STR("LoaderEntrySysFail"); |
| 150 |
arg_parser = parse_loader_entry_target_arg; |
| 151 |
} else if (streq(argv[0], "set-oneshot")) { |
| 152 |
variable = EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"); |
| 153 |
arg_parser = parse_loader_entry_target_arg; |
| 154 |
} else if (streq(argv[0], "set-timeout")) { |
| 155 |
variable = EFI_LOADER_VARIABLE_STR("LoaderConfigTimeout"); |
| 156 |
arg_parser = parse_timeout; |
| 157 |
} else if (streq(argv[0], "set-timeout-oneshot")) { |
| 158 |
variable = EFI_LOADER_VARIABLE_STR("LoaderConfigTimeoutOneShot"); |
| 159 |
arg_parser = parse_timeout; |
| 160 |
} else |
| 161 |
assert_not_reached(); |
| 162 |
|
| 163 |
if (isempty(argv[1])) { |
| 164 |
r = efi_set_variable(variable, NULL, 0); |
| 165 |
if (r < 0 && r != -ENOENT) |
| 166 |
return log_error_errno(r, "Failed to remove EFI variable '%s': %m", variable); |
| 167 |
} else { |
| 168 |
_cleanup_free_ char16_t *value = NULL; |
| 169 |
size_t value_size = 0; |
| 170 |
|
| 171 |
r = arg_parser(argv[1], &value, &value_size); |
| 172 |
if (r < 0) |
| 173 |
return r; |
| 174 |
r = efi_set_variable(variable, value, value_size); |
| 175 |
if (r < 0) |
| 176 |
return log_error_errno(r, "Failed to update EFI variable '%s': %m", variable); |
| 177 |
} |
| 178 |
|
| 179 |
return 0; |
| 180 |
} |