2aacef
From 806165285b822436023df84ca0a3e5b28a3099d6 Mon Sep 17 00:00:00 2001
2aacef
From: Jan Janssen <medhefgo@web.de>
2aacef
Date: Mon, 14 Nov 2022 15:24:32 +0100
2aacef
Subject: [PATCH] boot: Manually convert filepaths if needed
2aacef
2aacef
The conversion of a filepath device path to text is needed for the stub
2aacef
loader to find credential files.
2aacef
2aacef
(cherry picked from commit 679007044fbbcf82c66cf20b99f2f5086b7df6b4)
2aacef
2aacef
Related: #2138081
2aacef
---
2aacef
 src/boot/efi/util.c | 40 ++++++++++++++++++++++++++++++++++++----
2aacef
 1 file changed, 36 insertions(+), 4 deletions(-)
2aacef
2aacef
diff --git a/src/boot/efi/util.c b/src/boot/efi/util.c
2aacef
index 5547d288de..57436dbf0c 100644
2aacef
--- a/src/boot/efi/util.c
2aacef
+++ b/src/boot/efi/util.c
2aacef
@@ -772,19 +772,51 @@ EFI_STATUS make_file_device_path(EFI_HANDLE device, const char16_t *file, EFI_DE
2aacef
 EFI_STATUS device_path_to_str(const EFI_DEVICE_PATH *dp, char16_t **ret) {
2aacef
         EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *dp_to_text;
2aacef
         EFI_STATUS err;
2aacef
+        _cleanup_free_ char16_t *str = NULL;
2aacef
 
2aacef
         assert(dp);
2aacef
         assert(ret);
2aacef
 
2aacef
         err = BS->LocateProtocol(&(EFI_GUID) EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID, NULL, (void **) &dp_to_text);
2aacef
-        if (err != EFI_SUCCESS)
2aacef
-                return err;
2aacef
+        if (err != EFI_SUCCESS) {
2aacef
+                /* If the device path to text protocol is not available we can still do a best-effort attempt
2aacef
+                 * to convert it ourselves if we are given filepath-only device path. */
2aacef
+
2aacef
+                size_t size = 0;
2aacef
+                for (const EFI_DEVICE_PATH *node = dp; !IsDevicePathEnd(node);
2aacef
+                     node = NextDevicePathNode(node)) {
2aacef
+
2aacef
+                        if (DevicePathType(node) != MEDIA_DEVICE_PATH ||
2aacef
+                            DevicePathSubType(node) != MEDIA_FILEPATH_DP)
2aacef
+                                return err;
2aacef
+
2aacef
+                        size_t path_size = DevicePathNodeLength(node);
2aacef
+                        if (path_size <= offsetof(FILEPATH_DEVICE_PATH, PathName) || path_size % sizeof(char16_t))
2aacef
+                                return EFI_INVALID_PARAMETER;
2aacef
+                        path_size -= offsetof(FILEPATH_DEVICE_PATH, PathName);
2aacef
+
2aacef
+                        _cleanup_free_ char16_t *old = str;
2aacef
+                        str = xmalloc(size + path_size);
2aacef
+                        if (old) {
2aacef
+                                memcpy(str, old, size);
2aacef
+                                str[size / sizeof(char16_t) - 1] = '\\';
2aacef
+                        }
2aacef
+
2aacef
+                        memcpy(str + (size / sizeof(char16_t)),
2aacef
+                               ((uint8_t *) node) + offsetof(FILEPATH_DEVICE_PATH, PathName),
2aacef
+                               path_size);
2aacef
+                        size += path_size;
2aacef
+                }
2aacef
+
2aacef
+                *ret = TAKE_PTR(str);
2aacef
+                return EFI_SUCCESS;
2aacef
+        }
2aacef
 
2aacef
-        char16_t *str = dp_to_text->ConvertDevicePathToText(dp, false, false);
2aacef
+        str = dp_to_text->ConvertDevicePathToText(dp, false, false);
2aacef
         if (!str)
2aacef
                 return EFI_OUT_OF_RESOURCES;
2aacef
 
2aacef
-        *ret = str;
2aacef
+        *ret = TAKE_PTR(str);
2aacef
         return EFI_SUCCESS;
2aacef
 }
2aacef