efivars: add helper to format efivarfs path

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-04-04 22:07:17 +02:00
parent bd44566c4d
commit 74b3e52b99
2 changed files with 16 additions and 6 deletions

View File

@ -193,6 +193,17 @@ int efi_set_reboot_to_firmware(bool value) {
return 0;
}
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;
}
int efi_get_variable(
sd_id128_t vendor,
const char *name,
@ -211,9 +222,8 @@ int efi_get_variable(
assert(value);
assert(size);
if (asprintf(&p,
"/sys/firmware/efi/efivars/%s-%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
name, SD_ID128_FORMAT_VAL(vendor)) < 0)
p = efi_variable_path(vendor, name);
if (!p)
return -ENOMEM;
fd = open(p, O_RDONLY|O_NOCTTY|O_CLOEXEC);
@ -293,9 +303,8 @@ int efi_set_variable(
assert(name);
assert(value || size == 0);
if (asprintf(&p,
"/sys/firmware/efi/efivars/%s-%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
name, SD_ID128_FORMAT_VAL(vendor)) < 0)
p = efi_variable_path(vendor, name);
if (!p)
return -ENOMEM;
/* Newer efivarfs protects variables that are not in a whitelist with FS_IMMUTABLE_FL by default, to protect

View File

@ -33,6 +33,7 @@ int efi_reboot_to_firmware_supported(void);
int efi_get_reboot_to_firmware(void);
int efi_set_reboot_to_firmware(bool value);
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);