2aacef
From 57474bf255aafb683f4bb38e5bcb88cf48f07882 Mon Sep 17 00:00:00 2001
2aacef
From: Jan Janssen <medhefgo@web.de>
2aacef
Date: Tue, 22 Nov 2022 16:30:44 +0100
2aacef
Subject: [PATCH] boot: Use xstr8_to_16 for path conversion
2aacef
2aacef
(cherry picked from commit 7444e10611671abac35be3ab9fe9697cd4c90d62)
2aacef
2aacef
Related: #2138081
2aacef
---
2aacef
 src/boot/efi/boot.c |   8 ++--
2aacef
 src/boot/efi/cpio.c |  19 +--------
2aacef
 src/boot/efi/util.c | 102 ++++++++------------------------------------
2aacef
 src/boot/efi/util.h |   3 +-
2aacef
 4 files changed, 24 insertions(+), 108 deletions(-)
2aacef
2aacef
diff --git a/src/boot/efi/boot.c b/src/boot/efi/boot.c
2aacef
index 013df67e49..581043df01 100644
2aacef
--- a/src/boot/efi/boot.c
2aacef
+++ b/src/boot/efi/boot.c
2aacef
@@ -1465,7 +1465,7 @@ static void config_entry_add_type1(
2aacef
                 if (streq8(key, "linux")) {
2aacef
                         free(entry->loader);
2aacef
                         entry->type = LOADER_LINUX;
2aacef
-                        entry->loader = xstra_to_path(value);
2aacef
+                        entry->loader = xstr8_to_path(value);
2aacef
                         entry->key = 'l';
2aacef
                         continue;
2aacef
                 }
2aacef
@@ -1473,7 +1473,7 @@ static void config_entry_add_type1(
2aacef
                 if (streq8(key, "efi")) {
2aacef
                         entry->type = LOADER_EFI;
2aacef
                         free(entry->loader);
2aacef
-                        entry->loader = xstra_to_path(value);
2aacef
+                        entry->loader = xstr8_to_path(value);
2aacef
 
2aacef
                         /* do not add an entry for ourselves */
2aacef
                         if (strcaseeq16(entry->loader, loaded_image_path)) {
2aacef
@@ -1494,7 +1494,7 @@ static void config_entry_add_type1(
2aacef
 
2aacef
                 if (streq8(key, "devicetree")) {
2aacef
                         free(entry->devicetree);
2aacef
-                        entry->devicetree = xstra_to_path(value);
2aacef
+                        entry->devicetree = xstr8_to_path(value);
2aacef
                         continue;
2aacef
                 }
2aacef
 
2aacef
@@ -1503,7 +1503,7 @@ static void config_entry_add_type1(
2aacef
                                 entry->initrd,
2aacef
                                 n_initrd == 0 ? 0 : (n_initrd + 1) * sizeof(uint16_t *),
2aacef
                                 (n_initrd + 2) * sizeof(uint16_t *));
2aacef
-                        entry->initrd[n_initrd++] = xstra_to_path(value);
2aacef
+                        entry->initrd[n_initrd++] = xstr8_to_path(value);
2aacef
                         entry->initrd[n_initrd] = NULL;
2aacef
                         continue;
2aacef
                 }
2aacef
diff --git a/src/boot/efi/cpio.c b/src/boot/efi/cpio.c
2aacef
index 1dbfe5f380..79b5d4327b 100644
2aacef
--- a/src/boot/efi/cpio.c
2aacef
+++ b/src/boot/efi/cpio.c
2aacef
@@ -359,24 +359,7 @@ static char16_t *get_dropin_dir(const EFI_DEVICE_PATH *file_path) {
2aacef
         if (device_path_to_str(file_path, &file_path_str) != EFI_SUCCESS)
2aacef
                 return NULL;
2aacef
 
2aacef
-        for (char16_t *i = file_path_str, *fixed = i;; i++) {
2aacef
-                if (*i == '\0') {
2aacef
-                        *fixed = '\0';
2aacef
-                        break;
2aacef
-                }
2aacef
-
2aacef
-                /* Fix device path node separator. */
2aacef
-                if (*i == '/')
2aacef
-                        *i = '\\';
2aacef
-
2aacef
-                /* Double '\' is not allowed in EFI file paths. */
2aacef
-                if (fixed != file_path_str && fixed[-1] == '\\' && *i == '\\')
2aacef
-                        continue;
2aacef
-
2aacef
-                *fixed = *i;
2aacef
-                fixed++;
2aacef
-        }
2aacef
-
2aacef
+        convert_efi_path(file_path_str);
2aacef
         return xpool_print(u"%s.extra.d", file_path_str);
2aacef
 }
2aacef
 
2aacef
diff --git a/src/boot/efi/util.c b/src/boot/efi/util.c
2aacef
index b727d6de7e..3268c511d0 100644
2aacef
--- a/src/boot/efi/util.c
2aacef
+++ b/src/boot/efi/util.c
2aacef
@@ -249,97 +249,29 @@ void efivar_set_time_usec(const EFI_GUID *vendor, const char16_t *name, uint64_t
2aacef
         efivar_set(vendor, name, str, 0);
2aacef
 }
2aacef
 
2aacef
-static int utf8_to_16(const char *stra, char16_t *c) {
2aacef
-        char16_t unichar;
2aacef
-        UINTN len;
2aacef
-
2aacef
-        assert(stra);
2aacef
-        assert(c);
2aacef
-
2aacef
-        if (!(stra[0] & 0x80))
2aacef
-                len = 1;
2aacef
-        else if ((stra[0] & 0xe0) == 0xc0)
2aacef
-                len = 2;
2aacef
-        else if ((stra[0] & 0xf0) == 0xe0)
2aacef
-                len = 3;
2aacef
-        else if ((stra[0] & 0xf8) == 0xf0)
2aacef
-                len = 4;
2aacef
-        else if ((stra[0] & 0xfc) == 0xf8)
2aacef
-                len = 5;
2aacef
-        else if ((stra[0] & 0xfe) == 0xfc)
2aacef
-                len = 6;
2aacef
-        else
2aacef
-                return -1;
2aacef
-
2aacef
-        switch (len) {
2aacef
-        case 1:
2aacef
-                unichar = stra[0];
2aacef
-                break;
2aacef
-        case 2:
2aacef
-                unichar = stra[0] & 0x1f;
2aacef
-                break;
2aacef
-        case 3:
2aacef
-                unichar = stra[0] & 0x0f;
2aacef
-                break;
2aacef
-        case 4:
2aacef
-                unichar = stra[0] & 0x07;
2aacef
-                break;
2aacef
-        case 5:
2aacef
-                unichar = stra[0] & 0x03;
2aacef
-                break;
2aacef
-        case 6:
2aacef
-                unichar = stra[0] & 0x01;
2aacef
-                break;
2aacef
-        }
2aacef
-
2aacef
-        for (UINTN i = 1; i < len; i++) {
2aacef
-                if ((stra[i] & 0xc0) != 0x80)
2aacef
-                        return -1;
2aacef
-                unichar <<= 6;
2aacef
-                unichar |= stra[i] & 0x3f;
2aacef
-        }
2aacef
-
2aacef
-        *c = unichar;
2aacef
-        return len;
2aacef
-}
2aacef
-
2aacef
-char16_t *xstra_to_path(const char *stra) {
2aacef
-        char16_t *str;
2aacef
-        UINTN strlen;
2aacef
-        UINTN len;
2aacef
-        UINTN i;
2aacef
-
2aacef
-        assert(stra);
2aacef
+void convert_efi_path(char16_t *path) {
2aacef
+        assert(path);
2aacef
 
2aacef
-        len = strlen8(stra);
2aacef
-        str = xnew(char16_t, len + 2);
2aacef
+        for (size_t i = 0, fixed = 0;; i++) {
2aacef
+                /* Fix device path node separator. */
2aacef
+                path[fixed] = (path[i] == '/') ? '\\' : path[i];
2aacef
 
2aacef
-        str[0] = '\\';
2aacef
-        strlen = 1;
2aacef
-        i = 0;
2aacef
-        while (i < len) {
2aacef
-                int utf8len;
2aacef
-
2aacef
-                utf8len = utf8_to_16(stra + i, str + strlen);
2aacef
-                if (utf8len <= 0) {
2aacef
-                        /* invalid utf8 sequence, skip the garbage */
2aacef
-                        i++;
2aacef
+                /* Double '\' is not allowed in EFI file paths. */
2aacef
+                if (fixed > 0 && path[fixed - 1] == '\\' && path[fixed] == '\\')
2aacef
                         continue;
2aacef
-                }
2aacef
 
2aacef
-                if (str[strlen] == '/')
2aacef
-                        str[strlen] = '\\';
2aacef
-                if (str[strlen] == '\\' && str[strlen-1] == '\\') {
2aacef
-                        /* skip double slashes */
2aacef
-                        i += utf8len;
2aacef
-                        continue;
2aacef
-                }
2aacef
+                if (path[i] == '\0')
2aacef
+                        break;
2aacef
 
2aacef
-                strlen++;
2aacef
-                i += utf8len;
2aacef
+                fixed++;
2aacef
         }
2aacef
-        str[strlen] = '\0';
2aacef
-        return str;
2aacef
+}
2aacef
+
2aacef
+char16_t *xstr8_to_path(const char *str8) {
2aacef
+        assert(str8);
2aacef
+        char16_t *path = xstr8_to_16(str8);
2aacef
+        convert_efi_path(path);
2aacef
+        return path;
2aacef
 }
2aacef
 
2aacef
 EFI_STATUS file_read(EFI_FILE *dir, const char16_t *name, UINTN off, UINTN size, char **ret, UINTN *ret_size) {
2aacef
diff --git a/src/boot/efi/util.h b/src/boot/efi/util.h
2aacef
index d78feac39c..e4ab8138c4 100644
2aacef
--- a/src/boot/efi/util.h
2aacef
+++ b/src/boot/efi/util.h
2aacef
@@ -112,7 +112,8 @@ EFI_STATUS efivar_get_uint32_le(const EFI_GUID *vendor, const char16_t *name, ui
2aacef
 EFI_STATUS efivar_get_uint64_le(const EFI_GUID *vendor, const char16_t *name, uint64_t *ret);
2aacef
 EFI_STATUS efivar_get_boolean_u8(const EFI_GUID *vendor, const char16_t *name, bool *ret);
2aacef
 
2aacef
-char16_t *xstra_to_path(const char *stra);
2aacef
+void convert_efi_path(char16_t *path);
2aacef
+char16_t *xstr8_to_path(const char *stra);
2aacef
 
2aacef
 EFI_STATUS file_read(EFI_FILE *dir, const char16_t *name, UINTN off, UINTN size, char **content, UINTN *content_size);
2aacef