diff --git a/src/basic/efivars.c b/src/basic/efivars.c index 7e1e9e60476..db57189feb6 100644 --- a/src/basic/efivars.c +++ b/src/basic/efivars.c @@ -30,48 +30,22 @@ #define EFI_N_RETRIES_TOTAL 25 #define EFI_RETRY_DELAY (50 * USEC_PER_MSEC) -char* efi_variable_path(sd_id128_t vendor, const char *name) { - char *p; - - if (asprintf(&p, - "/sys/firmware/efi/efivars/%s-" SD_ID128_UUID_FORMAT_STR, - name, SD_ID128_FORMAT_VAL(vendor)) < 0) - return NULL; - - return p; -} - -static char* efi_variable_cache_path(sd_id128_t vendor, const char *name) { - char *p; - - if (asprintf(&p, - "/run/systemd/efivars/%s-" SD_ID128_UUID_FORMAT_STR, - name, SD_ID128_FORMAT_VAL(vendor)) < 0) - return NULL; - - return p; -} - int efi_get_variable( - sd_id128_t vendor, - const char *name, + const char *variable, uint32_t *ret_attribute, void **ret_value, size_t *ret_size) { _cleanup_close_ int fd = -1; - _cleanup_free_ char *p = NULL; _cleanup_free_ void *buf = NULL; struct stat st; usec_t begin = 0; /* Unnecessary initialization to appease gcc */ uint32_t a; ssize_t n; - assert(name); + assert(variable); - p = efi_variable_path(vendor, name); - if (!p) - return -ENOMEM; + const char *p = strjoina("/sys/firmware/efi/efivars/", variable); if (!ret_value && !ret_size && !ret_attribute) { /* If caller is not interested in anything, just check if the variable exists and is @@ -136,7 +110,8 @@ int efi_get_variable( return log_debug_errno(errno, "Failed to read value of EFI variable %s: %m", p); assert(n <= st.st_size - 4); - /* Always NUL terminate (3 bytes, to properly protect UTF-16, even if truncated in the middle of a character) */ + /* Always NUL-terminate (3 bytes, to properly protect UTF-16, even if truncated in the middle + * of a character) */ ((char*) buf)[n] = 0; ((char*) buf)[n + 1] = 0; ((char*) buf)[n + 2] = 0; @@ -150,8 +125,8 @@ int efi_get_variable( end = now(CLOCK_MONOTONIC); if (end > begin + EFI_RETRY_DELAY) - log_debug("Detected slow EFI variable read access on " SD_ID128_FORMAT_STR "-%s: %s", - SD_ID128_FORMAT_VAL(vendor), name, format_timespan(ts, sizeof(ts), end - begin, 1)); + log_debug("Detected slow EFI variable read access on %s: %s", + variable, format_timespan(ts, sizeof(ts), end - begin, 1)); } /* Note that efivarfs interestingly doesn't require ftruncate() to update an existing EFI variable @@ -169,13 +144,13 @@ int efi_get_variable( return 0; } -int efi_get_variable_string(sd_id128_t vendor, const char *name, char **p) { +int efi_get_variable_string(const char *variable, char **p) { _cleanup_free_ void *s = NULL; size_t ss = 0; int r; char *x; - r = efi_get_variable(vendor, name, NULL, &s, &ss); + r = efi_get_variable(variable, NULL, &s, &ss); if (r < 0) return r; @@ -187,28 +162,20 @@ int efi_get_variable_string(sd_id128_t vendor, const char *name, char **p) { return 0; } -int efi_set_variable( - sd_id128_t vendor, - const char *name, - const void *value, - size_t size) { - +int efi_set_variable(const char *variable, const void *value, size_t size) { struct var { uint32_t attr; char buf[]; } _packed_ * _cleanup_free_ buf = NULL; - _cleanup_free_ char *p = NULL; _cleanup_close_ int fd = -1; bool saved_flags_valid = false; unsigned saved_flags; int r; - assert(name); + assert(variable); assert(value || size == 0); - p = efi_variable_path(vendor, name); - if (!p) - return -ENOMEM; + const char *p = strjoina("/sys/firmware/efi/efivars/", variable); /* Newer efivarfs protects variables that are not in an allow list with FS_IMMUTABLE_FL by default, * to protect them for accidental removal and modification. We are not changing these variables @@ -274,14 +241,14 @@ finish: return r; } -int efi_set_variable_string(sd_id128_t vendor, const char *name, const char *v) { +int efi_set_variable_string(const char *variable, const char *value) { _cleanup_free_ char16_t *u16 = NULL; - u16 = utf8_to_utf16(v, strlen(v)); + u16 = utf8_to_utf16(value, strlen(value)); if (!u16) return -ENOMEM; - return efi_set_variable(vendor, name, u16, (char16_strlen(u16) + 1) * sizeof(char16_t)); + return efi_set_variable(variable, u16, (char16_strlen(u16) + 1) * sizeof(char16_t)); } bool is_efi_boot(void) { @@ -300,7 +267,7 @@ bool is_efi_boot(void) { return cache; } -static int read_flag(const char *varname) { +static int read_flag(const char *variable) { _cleanup_free_ void *v = NULL; uint8_t b; size_t s; @@ -309,7 +276,7 @@ static int read_flag(const char *varname) { if (!is_efi_boot()) /* If this is not an EFI boot, assume the queried flags are zero */ return 0; - r = efi_get_variable(EFI_VENDOR_GLOBAL, varname, NULL, &v, &s); + r = efi_get_variable(variable, NULL, &v, &s); if (r < 0) return r; @@ -324,7 +291,7 @@ bool is_efi_secure_boot(void) { static int cache = -1; if (cache < 0) - cache = read_flag("SecureBoot"); + cache = read_flag(EFI_GLOBAL_VARIABLE(SecureBoot)); return cache > 0; } @@ -333,13 +300,13 @@ bool is_efi_secure_boot_setup_mode(void) { static int cache = -1; if (cache < 0) - cache = read_flag("SetupMode"); + cache = read_flag(EFI_GLOBAL_VARIABLE(SetupMode)); return cache > 0; } int cache_efi_options_variable(void) { - _cleanup_free_ char *line = NULL, *cachepath = NULL; + _cleanup_free_ char *line = NULL; int r; /* In SecureBoot mode this is probably not what you want. As your cmdline is cryptographically signed @@ -350,37 +317,27 @@ int cache_efi_options_variable(void) { * (NB: For testing purposes, we still check the $SYSTEMD_EFI_OPTIONS env var before accessing this * cache, even when in SecureBoot mode.) */ if (is_efi_secure_boot()) { - _cleanup_free_ char *k = NULL; - - k = efi_variable_path(EFI_VENDOR_SYSTEMD, "SystemdOptions"); - if (!k) - return -ENOMEM; - /* Let's be helpful with the returned error and check if the variable exists at all. If it * does, let's return a recognizable error (EPERM), and if not ENODATA. */ - if (access(k, F_OK) < 0) + if (access(EFIVAR_PATH(EFI_SYSTEMD_VARIABLE(SystemdOptions)), F_OK) < 0) return errno == ENOENT ? -ENODATA : -errno; return -EPERM; } - r = efi_get_variable_string(EFI_VENDOR_SYSTEMD, "SystemdOptions", &line); + r = efi_get_variable_string(EFI_SYSTEMD_VARIABLE(SystemdOptions), &line); if (r == -ENOENT) return -ENODATA; if (r < 0) return r; - cachepath = efi_variable_cache_path(EFI_VENDOR_SYSTEMD, "SystemdOptions"); - if (!cachepath) - return -ENOMEM; - - return write_string_file(cachepath, line, WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755); + return write_string_file(EFIVAR_CACHE_PATH(EFI_SYSTEMD_VARIABLE(SystemdOptions)), line, + WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755); } int systemd_efi_options_variable(char **line) { const char *e; - _cleanup_free_ char *cachepath = NULL; int r; assert(line); @@ -398,11 +355,7 @@ int systemd_efi_options_variable(char **line) { return 0; } - cachepath = efi_variable_cache_path(EFI_VENDOR_SYSTEMD, "SystemdOptions"); - if (!cachepath) - return -ENOMEM; - - r = read_one_line_file(cachepath, line); + r = read_one_line_file(EFIVAR_CACHE_PATH(EFI_SYSTEMD_VARIABLE(SystemdOptions)), line); if (r == -ENOENT) return -ENODATA; return r; diff --git a/src/basic/efivars.h b/src/basic/efivars.h index dcaf87eacdb..0770cc13fe0 100644 --- a/src/basic/efivars.h +++ b/src/basic/efivars.h @@ -12,20 +12,39 @@ #include "time-util.h" -#define EFI_VENDOR_LOADER SD_ID128_MAKE(4a,67,b0,82,0a,4c,41,cf,b6,c7,44,0b,29,bb,8c,4f) -#define EFI_VENDOR_GLOBAL SD_ID128_MAKE(8b,e4,df,61,93,ca,11,d2,aa,0d,00,e0,98,03,2b,8c) -#define EFI_VENDOR_SYSTEMD SD_ID128_MAKE(8c,f2,64,4b,4b,0b,42,8f,93,87,6d,87,60,50,dc,67) +#define EFI_VENDOR_LOADER SD_ID128_MAKE(4a,67,b0,82,0a,4c,41,cf,b6,c7,44,0b,29,bb,8c,4f) +#define EFI_VENDOR_LOADER_STR SD_ID128_MAKE_UUID_STR(4a,67,b0,82,0a,4c,41,cf,b6,c7,44,0b,29,bb,8c,4f) +#define EFI_VENDOR_GLOBAL SD_ID128_MAKE(8b,e4,df,61,93,ca,11,d2,aa,0d,00,e0,98,03,2b,8c) +#define EFI_VENDOR_GLOBAL_STR SD_ID128_MAKE_UUID_STR(8b,e4,df,61,93,ca,11,d2,aa,0d,00,e0,98,03,2b,8c) +#define EFI_VENDOR_SYSTEMD SD_ID128_MAKE(8c,f2,64,4b,4b,0b,42,8f,93,87,6d,87,60,50,dc,67) +#define EFI_VENDOR_SYSTEMD_STR SD_ID128_MAKE_UUID_STR(8c,f2,64,4b,4b,0b,42,8f,93,87,6d,87,60,50,dc,67) #define EFI_VARIABLE_NON_VOLATILE 0x0000000000000001 #define EFI_VARIABLE_BOOTSERVICE_ACCESS 0x0000000000000002 #define EFI_VARIABLE_RUNTIME_ACCESS 0x0000000000000004 +/* Note that the - naming scheme is an efivarfs convention, i.e. part of the Linux + * API file system implementation for EFI. EFI itself processes UIDS in binary form. + */ + +#define EFI_VENDOR_VARIABLE_STR(vendor, name) name "-" vendor + +#define EFI_GLOBAL_VARIABLE_STR(name) EFI_VENDOR_VARIABLE_STR(EFI_VENDOR_GLOBAL_STR, name) +#define EFI_LOADER_VARIABLE_STR(name) EFI_VENDOR_VARIABLE_STR(EFI_VENDOR_LOADER_STR, name) +#define EFI_SYSTEMD_VARIABLE_STR(name) EFI_VENDOR_VARIABLE_STR(EFI_VENDOR_SYSTEMD_STR, name) + +#define EFI_GLOBAL_VARIABLE(name) EFI_GLOBAL_VARIABLE_STR(STRINGIFY(name)) +#define EFI_LOADER_VARIABLE(name) EFI_LOADER_VARIABLE_STR(STRINGIFY(name)) +#define EFI_SYSTEMD_VARIABLE(name) EFI_SYSTEMD_VARIABLE_STR(STRINGIFY(name)) + +#define EFIVAR_PATH(variable) "/sys/firmware/efi/efivars/" variable +#define EFIVAR_CACHE_PATH(variable) "/run/systemd/efivars/" variable + #if ENABLE_EFI -char* efi_variable_path(sd_id128_t vendor, const char *name); -int efi_get_variable(sd_id128_t vendor, const char *name, uint32_t *attribute, void **value, size_t *size); -int efi_get_variable_string(sd_id128_t vendor, const char *name, char **p); -int efi_set_variable(sd_id128_t vendor, const char *name, const void *value, size_t size); -int efi_set_variable_string(sd_id128_t vendor, const char *name, const char *p); +int efi_get_variable(const char *variable, uint32_t *attribute, void **value, size_t *size); +int efi_get_variable_string(const char *variable, char **p); +int efi_set_variable(const char *variable, const void *value, size_t size); +int efi_set_variable_string(const char *variable, const char *p); bool is_efi_boot(void); bool is_efi_secure_boot(void); @@ -36,23 +55,19 @@ int systemd_efi_options_variable(char **line); #else -static inline char* efi_variable_path(sd_id128_t vendor, const char *name) { - return NULL; -} - -static inline int efi_get_variable(sd_id128_t vendor, const char *name, uint32_t *attribute, void **value, size_t *size) { +static inline int efi_get_variable(const char *variable, uint32_t *attribute, void **value, size_t *size) { return -EOPNOTSUPP; } -static inline int efi_get_variable_string(sd_id128_t vendor, const char *name, char **p) { +static inline int efi_get_variable_string(const char *variable, char **p) { return -EOPNOTSUPP; } -static inline int efi_set_variable(sd_id128_t vendor, const char *name, const void *value, size_t size) { +static inline int efi_set_variable(const char *variable, const void *value, size_t size) { return -EOPNOTSUPP; } -static inline int efi_set_variable_string(sd_id128_t vendor, const char *name, const char *p) { +static inline int efi_set_variable_string(const char *variable, const char *p) { return -EOPNOTSUPP; } @@ -75,5 +90,4 @@ static inline int cache_efi_options_variable(void) { static inline int systemd_efi_options_variable(char **line) { return -ENODATA; } - #endif diff --git a/src/boot/bless-boot-generator.c b/src/boot/bless-boot-generator.c index 44a14f027d4..7209b28e671 100644 --- a/src/boot/bless-boot-generator.c +++ b/src/boot/bless-boot-generator.c @@ -47,7 +47,7 @@ int main(int argc, char *argv[]) { return EXIT_SUCCESS; } - if (access("/sys/firmware/efi/efivars/LoaderBootCountPath-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK) < 0) { + if (access(EFIVAR_PATH(EFI_LOADER_VARIABLE(LoaderBootCountPath)), F_OK) < 0) { if (errno == ENOENT) { log_debug_errno(errno, "Skipping generator, not booted with boot counting in effect."); diff --git a/src/boot/bless-boot.c b/src/boot/bless-boot.c index 97ad1e0cb50..3fc319ca273 100644 --- a/src/boot/bless-boot.c +++ b/src/boot/bless-boot.c @@ -212,7 +212,7 @@ static int acquire_boot_count_path( uint64_t left, done; int r; - r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderBootCountPath", &path); + r = efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderBootCountPath), &path); if (r == -ENOENT) return -EUNATCH; /* in this case, let the caller print a message */ if (r < 0) diff --git a/src/boot/bootctl.c b/src/boot/bootctl.c index d89f4e9b59c..361209a1bcd 100644 --- a/src/boot/bootctl.c +++ b/src/boot/bootctl.c @@ -979,29 +979,29 @@ static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) { } static int remove_loader_variables(void) { - const char *p; + const char *variable; int r = 0; /* Remove all persistent loader variables we define */ - FOREACH_STRING(p, - "LoaderConfigTimeout", - "LoaderConfigTimeoutOneShot", - "LoaderEntryDefault", - "LoaderEntryOneShot", - "LoaderSystemToken") { + FOREACH_STRING(variable, + EFI_LOADER_VARIABLE(LoaderConfigTimeout), + EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot), + EFI_LOADER_VARIABLE(LoaderEntryDefault), + EFI_LOADER_VARIABLE(LoaderEntryOneShot), + EFI_LOADER_VARIABLE(LoaderSystemToken)){ int q; - q = efi_set_variable(EFI_VENDOR_LOADER, p, NULL, 0); + q = efi_set_variable(variable, NULL, 0); if (q == -ENOENT) continue; if (q < 0) { - log_warning_errno(q, "Failed to remove %s variable: %m", p); + log_warning_errno(q, "Failed to remove EFI variable %s: %m", variable); if (r >= 0) r = q; } else - log_info("Removed EFI variable %s.", p); + log_info("Removed EFI variable %s.", variable); } return r; @@ -1224,12 +1224,12 @@ static int parse_argv(int argc, char *argv[]) { return 1; } -static void read_loader_efi_var(const char *name, char **var) { +static void read_efi_var(const char *variable, char **ret) { int r; - r = efi_get_variable_string(EFI_VENDOR_LOADER, name, var); + r = efi_get_variable_string(variable, ret); if (r < 0 && r != -ENOENT) - log_warning_errno(r, "Failed to read EFI variable %s: %m", name); + log_warning_errno(r, "Failed to read EFI variable %s: %m", variable); } static void print_yes_no_line(bool first, bool good, const char *name) { @@ -1296,12 +1296,13 @@ static int verb_status(int argc, char *argv[], void *userdata) { sd_id128_t loader_part_uuid = SD_ID128_NULL; uint64_t loader_features = 0; size_t i; + int have; - read_loader_efi_var("LoaderFirmwareType", &fw_type); - read_loader_efi_var("LoaderFirmwareInfo", &fw_info); - read_loader_efi_var("LoaderInfo", &loader); - read_loader_efi_var("StubInfo", &stub); - read_loader_efi_var("LoaderImageIdentifier", &loader_path); + read_efi_var(EFI_LOADER_VARIABLE(LoaderFirmwareType), &fw_type); + read_efi_var(EFI_LOADER_VARIABLE(LoaderFirmwareInfo), &fw_info); + read_efi_var(EFI_LOADER_VARIABLE(LoaderInfo), &loader); + read_efi_var(EFI_LOADER_VARIABLE(StubInfo), &stub); + read_efi_var(EFI_LOADER_VARIABLE(LoaderImageIdentifier), &loader_path); (void) efi_loader_get_features(&loader_features); if (loader_path) @@ -1354,8 +1355,10 @@ static int verb_status(int argc, char *argv[], void *userdata) { printf("\n"); printf("Random Seed:\n"); - printf(" Passed to OS: %s\n", yes_no(access("/sys/firmware/efi/efivars/LoaderRandomSeed-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK) >= 0)); - printf(" System Token: %s\n", access("/sys/firmware/efi/efivars/LoaderSystemToken-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK) >= 0 ? "set" : "not set"); + have = access(EFIVAR_PATH(EFI_LOADER_VARIABLE(LoaderRandomSeed)), F_OK) >= 0; + printf(" Passed to OS: %s\n", yes_no(have)); + have = access(EFIVAR_PATH(EFI_LOADER_VARIABLE(LoaderSystemToken)), F_OK) >= 0; + printf(" System Token: %s\n", have ? "set" : "not set"); if (arg_esp_path) { _cleanup_free_ char *p = NULL; @@ -1364,7 +1367,8 @@ static int verb_status(int argc, char *argv[], void *userdata) { if (!p) return log_oom(); - printf(" Exists: %s\n", yes_no(access(p, F_OK) >= 0)); + have = access(p, F_OK) >= 0; + printf(" Exists: %s\n", yes_no(have)); } printf("\n"); @@ -1539,7 +1543,7 @@ static int install_random_seed(const char *esp) { return 0; } - r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderSystemToken", NULL, NULL, &token_size); + r = efi_get_variable(EFI_LOADER_VARIABLE(LoaderSystemToken), NULL, NULL, &token_size); if (r == -ENODATA) log_debug_errno(r, "LoaderSystemToken EFI variable is invalid (too short?), replacing."); else if (r < 0) { @@ -1563,7 +1567,7 @@ static int install_random_seed(const char *esp) { * and possibly get identification information or too much insight into the kernel's entropy pool * state. */ RUN_WITH_UMASK(0077) { - r = efi_set_variable(EFI_VENDOR_LOADER, "LoaderSystemToken", buffer, sz); + r = efi_set_variable(EFI_LOADER_VARIABLE(LoaderSystemToken), buffer, sz); if (r < 0) { if (!arg_graceful) return log_error_errno(r, "Failed to write 'LoaderSystemToken' EFI variable: %m"); @@ -1764,15 +1768,15 @@ static int verb_is_installed(int argc, char *argv[], void *userdata) { static int parse_loader_entry_target_arg(const char *arg1, char16_t **ret_target, size_t *ret_target_size) { int r; if (streq(arg1, "@current")) { - r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderEntrySelected", NULL, (void *) ret_target, ret_target_size); + r = efi_get_variable(EFI_LOADER_VARIABLE(LoaderEntrySelected), NULL, (void *) ret_target, ret_target_size); if (r < 0) return log_error_errno(r, "Failed to get EFI variable 'LoaderEntrySelected': %m"); } else if (streq(arg1, "@oneshot")) { - r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderEntryOneShot", NULL, (void *) ret_target, ret_target_size); + r = efi_get_variable(EFI_LOADER_VARIABLE(LoaderEntryOneShot), NULL, (void *) ret_target, ret_target_size); if (r < 0) return log_error_errno(r, "Failed to get EFI variable 'LoaderEntryOneShot': %m"); } else if (streq(arg1, "@default")) { - r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderEntryDefault", NULL, (void *) ret_target, ret_target_size); + r = efi_get_variable(EFI_LOADER_VARIABLE(LoaderEntryDefault), NULL, (void *) ret_target, ret_target_size); if (r < 0) return log_error_errno(r, "Failed to get EFI variable 'LoaderEntryDefault': %m"); } else { @@ -1787,14 +1791,13 @@ static int parse_loader_entry_target_arg(const char *arg1, char16_t **ret_target } static int verb_set_default(int argc, char *argv[], void *userdata) { - const char *name; int r; if (!is_efi_boot()) return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Not booted with UEFI."); - if (access("/sys/firmware/efi/efivars/LoaderInfo-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK) < 0) { + if (access(EFIVAR_PATH(EFI_LOADER_VARIABLE(LoaderInfo)), F_OK) < 0) { if (errno == ENOENT) { log_error_errno(errno, "Not booted with a supported boot loader."); return -EOPNOTSUPP; @@ -1813,12 +1816,13 @@ static int verb_set_default(int argc, char *argv[], void *userdata) { "'%s' operation cannot be combined with --touch-variables=no.", argv[0]); - name = streq(argv[0], "set-default") ? "LoaderEntryDefault" : "LoaderEntryOneShot"; + const char *variable = streq(argv[0], "set-default") ? + EFI_LOADER_VARIABLE(LoaderEntryDefault) : EFI_LOADER_VARIABLE(LoaderEntryOneShot); if (isempty(argv[1])) { - r = efi_set_variable(EFI_VENDOR_LOADER, name, NULL, 0); + r = efi_set_variable(variable, NULL, 0); if (r < 0 && r != -ENOENT) - return log_error_errno(r, "Failed to remove EFI variable '%s': %m", name); + return log_error_errno(r, "Failed to remove EFI variable '%s': %m", variable); } else { _cleanup_free_ char16_t *target = NULL; size_t target_size = 0; @@ -1826,9 +1830,9 @@ static int verb_set_default(int argc, char *argv[], void *userdata) { r = parse_loader_entry_target_arg(argv[1], &target, &target_size); if (r < 0) return r; - r = efi_set_variable(EFI_VENDOR_LOADER, name, target, target_size); + r = efi_set_variable(variable, target, target_size); if (r < 0) - return log_error_errno(r, "Failed to update EFI variable '%s': %m", name); + return log_error_errno(r, "Failed to update EFI variable '%s': %m", variable); } return 0; @@ -1870,7 +1874,7 @@ static int verb_systemd_efi_options(int argc, char *argv[], void *userdata) { puts(line); } else { - r = efi_set_variable_string(EFI_VENDOR_SYSTEMD, "SystemdOptions", argv[1]); + r = efi_set_variable_string(EFI_SYSTEMD_VARIABLE(SystemdOptions), argv[1]); if (r < 0) return log_error_errno(r, "Failed to set SystemdOptions EFI variable: %m"); } diff --git a/src/core/efi-random.c b/src/core/efi-random.c index 94e138b35ba..a0b89d13794 100644 --- a/src/core/efi-random.c +++ b/src/core/efi-random.c @@ -27,8 +27,8 @@ static void lock_down_efi_variables(void) { * identify the system or gain too much insight into what we might have credited to the entropy * pool. */ FOREACH_STRING(p, - "/sys/firmware/efi/efivars/LoaderRandomSeed-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", - "/sys/firmware/efi/efivars/LoaderSystemToken-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f") { + EFIVAR_PATH(EFI_LOADER_VARIABLE(LoaderRandomSeed)), + EFIVAR_PATH(EFI_LOADER_VARIABLE(LoaderSystemToken))) { r = chattr_path(p, 0, FS_IMMUTABLE_FL, NULL); if (r == -ENOENT) @@ -61,7 +61,7 @@ int efi_take_random_seed(void) { return 0; } - r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderRandomSeed", NULL, &value, &size); + r = efi_get_variable(EFI_LOADER_VARIABLE(LoaderRandomSeed), NULL, &value, &size); if (r == -EOPNOTSUPP) { log_debug_errno(r, "System lacks EFI support, not initializing random seed from EFI variable."); return 0; diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 2112e53393a..1754c9dc584 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -2868,12 +2868,12 @@ static int method_set_reboot_to_boot_loader_menu( if (use_efi) { if (x == UINT64_MAX) - r = efi_set_variable(EFI_VENDOR_LOADER, "LoaderConfigTimeoutOneShot", NULL, 0); + r = efi_set_variable(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot), NULL, 0); else { char buf[DECIMAL_STR_MAX(uint64_t) + 1]; xsprintf(buf, "%" PRIu64, DIV_ROUND_UP(x, USEC_PER_SEC)); /* second granularity */ - r = efi_set_variable_string(EFI_VENDOR_LOADER, "LoaderConfigTimeoutOneShot", buf); + r = efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot), buf); } if (r < 0) return r; @@ -3071,9 +3071,9 @@ static int method_set_reboot_to_boot_loader_entry( if (use_efi) { if (isempty(v)) /* Delete item */ - r = efi_set_variable(EFI_VENDOR_LOADER, "LoaderEntryOneShot", NULL, 0); + r = efi_set_variable(EFI_LOADER_VARIABLE(LoaderEntryOneShot), NULL, 0); else - r = efi_set_variable_string(EFI_VENDOR_LOADER, "LoaderEntryOneShot", v); + r = efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderEntryOneShot), v); if (r < 0) return r; } else { diff --git a/src/partition/repart.c b/src/partition/repart.c index f4ead0a31ba..fdb376060e3 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -4419,7 +4419,7 @@ static int parse_efi_variable_factory_reset(void) { if (!in_initrd()) /* Never honour EFI variable factory reset request outside of the initrd */ return 0; - r = efi_get_variable_string(EFI_VENDOR_SYSTEMD, "FactoryReset", &value); + r = efi_get_variable_string(EFI_SYSTEMD_VARIABLE(FactoryReset), &value); if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r)) return 0; if (r < 0) @@ -4439,7 +4439,7 @@ static int parse_efi_variable_factory_reset(void) { static int remove_efi_variable_factory_reset(void) { int r; - r = efi_set_variable(EFI_VENDOR_SYSTEMD, "FactoryReset", NULL, 0); + r = efi_set_variable(EFI_SYSTEMD_VARIABLE(FactoryReset), NULL, 0); if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r)) return 0; if (r < 0) diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index 354eb6828d7..dc334cf1966 100644 --- a/src/shared/bootspec.c +++ b/src/shared/bootspec.c @@ -677,14 +677,14 @@ int boot_entries_load_config( return log_error_errno(r, "Failed to uniquify boot entries: %m"); if (is_efi_boot()) { - r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderEntryOneShot", &config->entry_oneshot); + r = efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderEntryOneShot), &config->entry_oneshot); if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA)) { log_warning_errno(r, "Failed to read EFI variable \"LoaderEntryOneShot\": %m"); if (r == -ENOMEM) return r; } - r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderEntryDefault", &config->entry_default); + r = efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderEntryDefault), &config->entry_default); if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA)) { log_warning_errno(r, "Failed to read EFI variable \"LoaderEntryDefault\": %m"); if (r == -ENOMEM) @@ -776,7 +776,7 @@ int boot_entries_augment_from_loader( break; } - p = efi_variable_path(EFI_VENDOR_LOADER, "LoaderEntries"); + p = strdup(EFIVAR_PATH(EFI_LOADER_VARIABLE(LoaderEntries))); if (!p) return log_oom(); diff --git a/src/shared/efi-loader.c b/src/shared/efi-loader.c index c250bc2d575..55f85e695f5 100644 --- a/src/shared/efi-loader.c +++ b/src/shared/efi-loader.c @@ -82,7 +82,7 @@ int efi_reboot_to_firmware_supported(void) { if (!is_efi_boot()) goto not_supported; - r = efi_get_variable(EFI_VENDOR_GLOBAL, "OsIndicationsSupported", NULL, &v, &s); + r = efi_get_variable(EFI_GLOBAL_VARIABLE(OsIndicationsSupported), NULL, &v, &s); if (r == -ENOENT) goto not_supported; /* variable doesn't exist? it's not supported then */ if (r < 0) @@ -105,7 +105,6 @@ not_supported: static int get_os_indications(uint64_t *ret) { static struct stat cache_stat = {}; _cleanup_free_ void *v = NULL; - _cleanup_free_ char *fn = NULL; static uint64_t cache; struct stat new_stat; size_t s; @@ -118,12 +117,8 @@ static int get_os_indications(uint64_t *ret) { if (r < 0) return r; - fn = efi_variable_path(EFI_VENDOR_GLOBAL, "OsIndications"); - if (!fn) - return -ENOMEM; - /* stat() the EFI variable, to see if the mtime changed. If it did we need to cache again. */ - if (stat(fn, &new_stat) < 0) { + if (stat(EFIVAR_PATH(EFI_GLOBAL_VARIABLE(OsIndications)), &new_stat) < 0) { if (errno != ENOENT) return -errno; @@ -137,7 +132,7 @@ static int get_os_indications(uint64_t *ret) { return 0; } - r = efi_get_variable(EFI_VENDOR_GLOBAL, "OsIndications", NULL, &v, &s); + r = efi_get_variable(EFI_GLOBAL_VARIABLE(OsIndications), NULL, &v, &s); if (r == -ENOENT) { /* Some firmware implementations that do support OsIndications and report that with * OsIndicationsSupported will remove the OsIndications variable when it is unset. Let's @@ -180,7 +175,7 @@ int efi_set_reboot_to_firmware(bool value) { /* Avoid writing to efi vars store if we can due to firmware bugs. */ if (b != b_new) - return efi_set_variable(EFI_VENDOR_GLOBAL, "OsIndications", &b_new, sizeof(uint64_t)); + return efi_set_variable(EFI_GLOBAL_VARIABLE(OsIndications), &b_new, sizeof(uint64_t)); return 0; } @@ -235,7 +230,7 @@ int efi_get_boot_option( char **path, bool *active) { - char boot_id[9]; + char variable[STRLEN(EFI_GLOBAL_VARIABLE_STR("Boot")) + 4 + 1]; _cleanup_free_ uint8_t *buf = NULL; size_t l; struct boot_option *header; @@ -247,8 +242,8 @@ int efi_get_boot_option( if (!is_efi_boot()) return -EOPNOTSUPP; - xsprintf(boot_id, "Boot%04X", id); - r = efi_get_variable(EFI_VENDOR_GLOBAL, boot_id, NULL, (void **)&buf, &l); + xsprintf(variable, EFI_GLOBAL_VARIABLE_STR("Boot%04X"), id); + r = efi_get_variable(variable, NULL, (void **)&buf, &l); if (r < 0) return r; if (l < offsetof(struct boot_option, title)) @@ -372,7 +367,7 @@ int efi_add_boot_option( _cleanup_free_ char *buf = NULL; struct boot_option *option; struct device_path *devicep; - char boot_id[9]; + char variable[STRLEN(EFI_GLOBAL_VARIABLE_STR("Boot")) + 4 + 1]; if (!is_efi_boot()) return -EOPNOTSUPP; @@ -424,18 +419,18 @@ int efi_add_boot_option( devicep->length = offsetof(struct device_path, path); size += devicep->length; - xsprintf(boot_id, "Boot%04X", id); - return efi_set_variable(EFI_VENDOR_GLOBAL, boot_id, buf, size); + xsprintf(variable, EFI_GLOBAL_VARIABLE_STR("Boot%04X"), id); + return efi_set_variable(variable, buf, size); } int efi_remove_boot_option(uint16_t id) { - char boot_id[9]; + char variable[STRLEN(EFI_GLOBAL_VARIABLE_STR("Boot")) + 4 + 1]; if (!is_efi_boot()) return -EOPNOTSUPP; - xsprintf(boot_id, "Boot%04X", id); - return efi_set_variable(EFI_VENDOR_GLOBAL, boot_id, NULL, 0); + xsprintf(variable, EFI_GLOBAL_VARIABLE_STR("Boot%04X"), id); + return efi_set_variable(variable, NULL, 0); } int efi_get_boot_order(uint16_t **order) { @@ -446,7 +441,7 @@ int efi_get_boot_order(uint16_t **order) { if (!is_efi_boot()) return -EOPNOTSUPP; - r = efi_get_variable(EFI_VENDOR_GLOBAL, "BootOrder", NULL, &buf, &l); + r = efi_get_variable(EFI_GLOBAL_VARIABLE(BootOrder), NULL, &buf, &l); if (r < 0) return r; @@ -466,7 +461,7 @@ int efi_set_boot_order(uint16_t *order, size_t n) { if (!is_efi_boot()) return -EOPNOTSUPP; - return efi_set_variable(EFI_VENDOR_GLOBAL, "BootOrder", order, n * sizeof(uint16_t)); + return efi_set_variable(EFI_GLOBAL_VARIABLE(BootOrder), order, n * sizeof(uint16_t)); } static int boot_id_hex(const char s[static 4]) { @@ -500,7 +495,7 @@ int efi_get_boot_options(uint16_t **options) { if (!is_efi_boot()) return -EOPNOTSUPP; - dir = opendir("/sys/firmware/efi/efivars/"); + dir = opendir(EFIVAR_PATH(".")); if (!dir) return -errno; @@ -513,7 +508,7 @@ int efi_get_boot_options(uint16_t **options) { if (strlen(de->d_name) != 45) continue; - if (strcmp(de->d_name + 8, "-8be4df61-93ca-11d2-aa0d-00e098032b8c") != 0) + if (strcmp(de->d_name + 8, EFI_GLOBAL_VARIABLE_STR("")) != 0) /* generate variable suffix using macro */ continue; id = boot_id_hex(de->d_name + 4); @@ -533,15 +528,15 @@ int efi_get_boot_options(uint16_t **options) { return count; } -static int read_usec(sd_id128_t vendor, const char *name, usec_t *u) { +static int read_usec(const char *variable, usec_t *u) { _cleanup_free_ char *j = NULL; int r; uint64_t x = 0; - assert(name); + assert(variable); assert(u); - r = efi_get_variable_string(EFI_VENDOR_LOADER, name, &j); + r = efi_get_variable_string(variable, &j); if (r < 0) return r; @@ -563,11 +558,11 @@ int efi_loader_get_boot_usec(usec_t *firmware, usec_t *loader) { if (!is_efi_boot()) return -EOPNOTSUPP; - r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeInitUSec", &x); + r = read_usec(EFI_LOADER_VARIABLE(LoaderTimeInitUSec), &x); if (r < 0) return log_debug_errno(r, "Failed to read LoaderTimeInitUSec: %m"); - r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeExecUSec", &y); + r = read_usec(EFI_LOADER_VARIABLE(LoaderTimeExecUSec), &y); if (r < 0) return log_debug_errno(r, "Failed to read LoaderTimeExecUSec: %m"); @@ -589,7 +584,7 @@ int efi_loader_get_device_part_uuid(sd_id128_t *u) { if (!is_efi_boot()) return -EOPNOTSUPP; - r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderDevicePartUUID", &p); + r = efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderDevicePartUUID), &p); if (r < 0) return r; @@ -621,7 +616,7 @@ int efi_loader_get_entries(char ***ret) { if (!is_efi_boot()) return -EOPNOTSUPP; - r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderEntries", NULL, (void**) &entries, &size); + r = efi_get_variable(EFI_LOADER_VARIABLE(LoaderEntries), NULL, (void**) &entries, &size); if (r < 0) return r; @@ -673,12 +668,12 @@ int efi_loader_get_features(uint64_t *ret) { return 0; } - r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderFeatures", NULL, &v, &s); + r = efi_get_variable(EFI_LOADER_VARIABLE(LoaderFeatures), NULL, &v, &s); if (r == -ENOENT) { _cleanup_free_ char *info = NULL; /* The new (v240+) LoaderFeatures variable is not supported, let's see if it's systemd-boot at all */ - r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderInfo", &info); + r = efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderInfo), &info); if (r < 0) { if (r != -ENOENT) return r; @@ -713,7 +708,7 @@ int efi_loader_get_features(uint64_t *ret) { } int efi_loader_get_config_timeout_one_shot(usec_t *ret) { - _cleanup_free_ char *v = NULL, *fn = NULL; + _cleanup_free_ char *v = NULL; static struct stat cache_stat = {}; struct stat new_stat; static usec_t cache; @@ -722,12 +717,8 @@ int efi_loader_get_config_timeout_one_shot(usec_t *ret) { assert(ret); - fn = efi_variable_path(EFI_VENDOR_LOADER, "LoaderConfigTimeoutOneShot"); - if (!fn) - return -ENOMEM; - - /* stat() the EFI variable, to see if the mtime changed. If it did we need to cache again. */ - if (stat(fn, &new_stat) < 0) + /* stat() the EFI variable, to see if the mtime changed. If it did, we need to cache again. */ + if (stat(EFIVAR_PATH(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot)), &new_stat) < 0) return -errno; if (stat_inode_unmodified(&new_stat, &cache_stat)) { @@ -735,7 +726,7 @@ int efi_loader_get_config_timeout_one_shot(usec_t *ret) { return 0; } - r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderConfigTimeoutOneShot", &v); + r = efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot), &v); if (r < 0) return r; @@ -751,25 +742,21 @@ int efi_loader_get_config_timeout_one_shot(usec_t *ret) { } int efi_loader_update_entry_one_shot_cache(char **cache, struct stat *cache_stat) { - _cleanup_free_ char *fn = NULL, *v = NULL; + _cleanup_free_ char *v = NULL; struct stat new_stat; int r; assert(cache); assert(cache_stat); - fn = efi_variable_path(EFI_VENDOR_LOADER, "LoaderEntryOneShot"); - if (!fn) - return -ENOMEM; - /* stat() the EFI variable, to see if the mtime changed. If it did we need to cache again. */ - if (stat(fn, &new_stat) < 0) + if (stat(EFIVAR_PATH(EFI_LOADER_VARIABLE(LoaderEntryOneShot)), &new_stat) < 0) return -errno; if (stat_inode_unmodified(&new_stat, cache_stat)) return 0; - r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderEntryOneShot", &v); + r = efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderEntryOneShot), &v); if (r < 0) return r;