#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
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;
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
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;
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
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) {
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;
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;
static int cache = -1;
if (cache < 0)
- cache = read_flag("SecureBoot");
+ cache = read_flag(EFI_GLOBAL_VARIABLE(SecureBoot));
return cache > 0;
}
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
* (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);
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;
#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 <lowercaseuuid>-<varname> 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);
#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;
}
static inline int systemd_efi_options_variable(char **line) {
return -ENODATA;
}
-
#endif
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.");
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)
}
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;
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) {
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)
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;
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");
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) {
* 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");
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 {
}
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;
"'%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;
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;
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");
}
* 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)
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;
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;
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 {
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)
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)
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)
break;
}
- p = efi_variable_path(EFI_VENDOR_LOADER, "LoaderEntries");
+ p = strdup(EFIVAR_PATH(EFI_LOADER_VARIABLE(LoaderEntries)));
if (!p)
return log_oom();
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)
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;
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;
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
/* 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;
}
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;
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))
_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;
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) {
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;
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]) {
if (!is_efi_boot())
return -EOPNOTSUPP;
- dir = opendir("/sys/firmware/efi/efivars/");
+ dir = opendir(EFIVAR_PATH("."));
if (!dir)
return -errno;
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);
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;
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");
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;
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;
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;
}
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;
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)) {
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;
}
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;