SecurityPkg: Fix assert when setting key from eMMC/SD/USB
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / SecureBootConfigDxe / SecureBootConfigFileExplorer.c
index 1b6f888..2a26c20 100644 (file)
@@ -123,6 +123,8 @@ OpenFileByDevicePath(
   EFI_FILE_PROTOCOL               *Handle1;\r
   EFI_FILE_PROTOCOL               *Handle2;\r
   EFI_HANDLE                      DeviceHandle;\r
+  CHAR16                          *PathName;\r
+  UINTN                           PathLength;\r
 \r
   if ((FilePath == NULL || FileHandle == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -173,6 +175,11 @@ OpenFileByDevicePath(
     //\r
     Handle2  = Handle1;\r
     Handle1 = NULL;\r
+    PathLength = DevicePathNodeLength (*FilePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
+    PathName = AllocateCopyPool (PathLength, ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName);\r
+    if (PathName == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
 \r
     //\r
     // Try to test opening an existing file\r
@@ -180,7 +187,7 @@ OpenFileByDevicePath(
     Status = Handle2->Open (\r
                           Handle2,\r
                           &Handle1,\r
-                          ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,\r
+                          PathName,\r
                           OpenMode &~EFI_FILE_MODE_CREATE,\r
                           0\r
                          );\r
@@ -192,7 +199,7 @@ OpenFileByDevicePath(
       Status = Handle2->Open (\r
                             Handle2,\r
                             &Handle1,\r
-                            ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,\r
+                            PathName,\r
                             OpenMode,\r
                             Attributes\r
                            );\r
@@ -202,6 +209,8 @@ OpenFileByDevicePath(
     //\r
     Handle2->Close (Handle2);\r
 \r
+    FreePool (PathName);\r
+\r
     if (EFI_ERROR(Status)) {\r
       return (Status);\r
     }\r