teknoraver / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone

Blame SOURCES/0046-boot-Manually-convert-filepaths-if-needed.patch

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