From: Zbigniew Jędrzejewski-Szmek Date: Thu, 4 Apr 2019 20:07:17 +0000 (+0200) Subject: efivars: add helper to format efivarfs path X-Git-Tag: v243-rc1~572^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=74b3e52b9934bfaacf445958764180a02a1abb78;p=thirdparty%2Fsystemd.git efivars: add helper to format efivarfs path --- diff --git a/src/shared/efivars.c b/src/shared/efivars.c index 875b8439e12..fc884dedf06 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 d8f18aae90e..49d1b338fbd 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);