From 74b3e52b9934bfaacf445958764180a02a1abb78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 4 Apr 2019 22:07:17 +0200 Subject: [PATCH] efivars: add helper to format efivarfs path --- src/shared/efivars.c | 21 +++++++++++++++------ src/shared/efivars.h | 1 + 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/shared/efivars.c b/src/shared/efivars.c index 875b8439e1..fc884dedf0 100644 --- a/src/shared/efivars.c +++ b/src/shared/efivars.c @@ -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 diff --git a/src/shared/efivars.h b/src/shared/efivars.h index d8f18aae90..49d1b338fb 100644 --- a/src/shared/efivars.h +++ b/src/shared/efivars.h @@ -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);