]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
efivars: add new helper efi_set_variable_string()
authorLennart Poettering <lennart@poettering.net>
Mon, 22 Oct 2018 17:58:26 +0000 (19:58 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 16 Nov 2018 14:52:22 +0000 (15:52 +0100)
Let's make it easier to parse an UTF-16 string properly.

src/shared/efivars.c
src/shared/efivars.h

index e536f0d94ea8d24fe0532a7340575085f0da5273..360ef0340084aeaf2cbeb07510eb9e7bf25ce952 100644 (file)
@@ -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) {
index feedbf91a6577079ab62458d8ae0afaed36f5519..51ecf6a27978668edd41167f326d4b00a15f31d0 100644 (file)
@@ -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;
 }