From cea72d53f89131e8c60679eeef6644ed5720156d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 22 Oct 2018 19:58:26 +0200 Subject: [PATCH] efivars: add new helper efi_set_variable_string() Let's make it easier to parse an UTF-16 string properly. --- src/shared/efivars.c | 36 +++++++++++++++++++++++------------- src/shared/efivars.h | 9 +++++++-- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/shared/efivars.c b/src/shared/efivars.c index e536f0d94e..360ef03400 100644 --- a/src/shared/efivars.c +++ b/src/shared/efivars.c @@ -246,6 +246,24 @@ int efi_get_variable( return 0; } +int efi_get_variable_string(sd_id128_t vendor, const char *name, char **p) { + _cleanup_free_ void *s = NULL; + size_t ss = 0; + int r; + char *x; + + r = efi_get_variable(vendor, name, NULL, &s, &ss); + if (r < 0) + return r; + + x = utf16_to_utf8(s, ss); + if (!x) + return -ENOMEM; + + *p = x; + return 0; +} + int efi_set_variable( sd_id128_t vendor, const char *name, @@ -326,22 +344,14 @@ finish: return r; } -int efi_get_variable_string(sd_id128_t vendor, const char *name, char **p) { - _cleanup_free_ void *s = NULL; - size_t ss = 0; - int r; - char *x; +int efi_set_variable_string(sd_id128_t vendor, const char *name, const char *v) { + _cleanup_free_ char16_t *u16 = NULL; - r = efi_get_variable(vendor, name, NULL, &s, &ss); - if (r < 0) - return r; - - x = utf16_to_utf8(s, ss); - if (!x) + u16 = utf8_to_utf16(v, strlen(v)); + if (!u16) return -ENOMEM; - *p = x; - return 0; + return efi_set_variable(vendor, name, u16, (char16_strlen(u16) + 1) * sizeof(char16_t)); } static size_t utf16_size(const uint16_t *s) { diff --git a/src/shared/efivars.h b/src/shared/efivars.h index feedbf91a6..51ecf6a279 100644 --- a/src/shared/efivars.h +++ b/src/shared/efivars.h @@ -34,8 +34,9 @@ int efi_get_reboot_to_firmware(void); int efi_set_reboot_to_firmware(bool value); int efi_get_variable(sd_id128_t vendor, const char *name, uint32_t *attribute, void **value, size_t *size); -int efi_set_variable(sd_id128_t vendor, const char *name, const 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_boot_option(uint16_t nr, char **title, sd_id128_t *part_uuid, char **path, bool *active); int efi_add_boot_option(uint16_t id, const char *title, uint32_t part, uint64_t pstart, uint64_t psize, sd_id128_t part_uuid, const char *path); @@ -81,11 +82,15 @@ static inline int efi_get_variable(sd_id128_t vendor, const char *name, uint32_t return -EOPNOTSUPP; } +static inline int efi_get_variable_string(sd_id128_t vendor, const char *name, char **p) { + return -EOPNOTSUPP; +} + static inline int efi_set_variable(sd_id128_t vendor, const char *name, const 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_set_variable_string(sd_id128_t vendor, const char *name, const char *p) { return -EOPNOTSUPP; }