]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/FileExplorerLib/FileExplorer.c
MdeModulePkg/CapsuleLib: Follow UEFI 22.2.3 to process FMP.
[mirror_edk2.git] / MdeModulePkg / Library / FileExplorerLib / FileExplorer.c
index c64a94bebe5972b2b3e25ecc9cd477b2809bd520..41a22aa00c08b70c607170dd45ac123b590a045c 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 File explorer related functions.\r
 \r
-Copyright (c) 2004 - 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials are licensed and made available under\r
 the terms and conditions of the BSD License that accompanies this distribution.\r
 The full text of the license may be found at\r
@@ -184,8 +184,6 @@ LibCallback (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
-  *ActionRequest = EFI_BROWSER_ACTION_REQUEST_NONE;\r
-\r
   if (Action == EFI_BROWSER_ACTION_CHANGED) {\r
     if ((Value == NULL) || (ActionRequest == NULL)) {\r
       return EFI_INVALID_PARAMETER;\r
@@ -304,7 +302,9 @@ LibDestroyMenuEntry (
   FileContext = (FILE_CONTEXT *) MenuEntry->VariableContext;\r
 \r
   if (!FileContext->IsRoot) {\r
-    FreePool (FileContext->DevicePath);\r
+    if (FileContext->DevicePath != NULL) {\r
+      FreePool (FileContext->DevicePath);\r
+    }\r
   } else {\r
     if (FileContext->FileHandle != NULL) {\r
       FileContext->FileHandle->Close (FileContext->FileHandle);\r
@@ -317,7 +317,9 @@ LibDestroyMenuEntry (
 \r
   FreePool (FileContext);\r
 \r
-  FreePool (MenuEntry->DisplayString);\r
+  if (MenuEntry->DisplayString != NULL) {\r
+    FreePool (MenuEntry->DisplayString);\r
+  }\r
   if (MenuEntry->HelpString != NULL) {\r
     FreePool (MenuEntry->HelpString);\r
   }\r
@@ -582,6 +584,7 @@ LibIsSupportedFileType (
   }\r
 \r
   TmpStr = AllocateCopyPool (StrSize (InputFileType), InputFileType);\r
+  ASSERT(TmpStr != NULL);\r
   LibToLowerString(TmpStr);\r
 \r
   IsSupported = (StrStr (gFileExplorerPrivate.FileType, TmpStr) == NULL ? FALSE : TRUE);\r
@@ -617,6 +620,14 @@ LibAppendFileName (
 \r
   Size1 = StrSize (Str1);\r
   Size2 = StrSize (Str2);\r
+  \r
+  //\r
+  // Check overflow\r
+  //\r
+  if (((MAX_UINTN - Size1) < Size2) || ((MAX_UINTN - Size1 - Size2) < sizeof(CHAR16))) {\r
+    return NULL;\r
+  }\r
+  \r
   MaxLen = (Size1 + Size2 + sizeof (CHAR16))/ sizeof (CHAR16);\r
   Str   = AllocateZeroPool (Size1 + Size2 + sizeof (CHAR16));\r
   ASSERT (Str != NULL);\r
@@ -772,7 +783,9 @@ LibFindFileSystem (
                                              MenuEntry->DisplayString,\r
                                              NULL\r
                                              );\r
-      FreePool (Info);\r
+\r
+      if (Info != NULL)\r
+        FreePool (Info);\r
 \r
       OptionNumber++;\r
       InsertTailList (&gFileExplorerPrivate.FsOptionMenu->Head, &MenuEntry->Link);\r
@@ -958,6 +971,7 @@ LibGetFileHandleFromDevicePath (
     // the file system support below to be skipped.\r
     //\r
     Status = EFI_OUT_OF_RESOURCES;\r
+    goto Done;\r
   }\r
         \r
   //\r
@@ -987,6 +1001,11 @@ LibGetFileHandleFromDevicePath (
       *ParentFileName = AllocateCopyPool (StrSize (((FILEPATH_DEVICE_PATH *) DevicePathNode)->PathName), ((FILEPATH_DEVICE_PATH *) DevicePathNode)->PathName);\r
     } else {\r
       TempPath = LibAppendFileName (*ParentFileName, ((FILEPATH_DEVICE_PATH *) DevicePathNode)->PathName);\r
+      if (TempPath == NULL) {\r
+        LastHandle->Close (LastHandle);\r
+        Status = EFI_OUT_OF_RESOURCES;\r
+        goto Done;\r
+      }\r
       FreePool (*ParentFileName);\r
       *ParentFileName = TempPath;\r
     }\r
@@ -1062,12 +1081,14 @@ LibFindFiles (
   // Pass 1 to get Directories\r
   // Pass 2 to get files that are EFI images\r
   //\r
+  Status = EFI_SUCCESS;\r
   for (Pass = 1; Pass <= 2; Pass++) {\r
     FileHandle->SetPosition (FileHandle, 0);\r
     for (;;) {\r
       BufferSize  = DirBufferSize;\r
       Status      = FileHandle->Read (FileHandle, &BufferSize, DirInfo);\r
       if (EFI_ERROR (Status) || BufferSize == 0) {\r
+        Status = EFI_SUCCESS;\r
         break;\r
       }\r
 \r
@@ -1090,12 +1111,18 @@ LibFindFiles (
 \r
       NewMenuEntry = LibCreateMenuEntry ();\r
       if (NULL == NewMenuEntry) {\r
-        return EFI_OUT_OF_RESOURCES;\r
+        Status = EFI_OUT_OF_RESOURCES;\r
+        goto Done;\r
       }\r
 \r
       NewFileContext = (FILE_CONTEXT *) NewMenuEntry->VariableContext;\r
       NewFileContext->DeviceHandle = DeviceHandle;\r
       NewFileContext->FileName = LibAppendFileName (FileName, DirInfo->FileName);\r
+      if  (NewFileContext->FileName == NULL) {\r
+        LibDestroyMenuEntry (NewMenuEntry);\r
+        Status = EFI_OUT_OF_RESOURCES;\r
+        goto Done;\r
+      }\r
       NewFileContext->FileHandle = FileHandle;\r
       NewFileContext->DevicePath = FileDevicePath (NewFileContext->DeviceHandle, NewFileContext->FileName);\r
       NewMenuEntry->HelpString = NULL;\r
@@ -1130,9 +1157,11 @@ LibFindFiles (
 \r
   gFileExplorerPrivate.FsOptionMenu->MenuNumber = OptionNumber;\r
 \r
+Done:\r
+\r
   FreePool (DirInfo);\r
 \r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -1359,6 +1388,7 @@ ChooseFile (
   gFileExplorerPrivate.ChooseHandler = ChooseHandler;\r
   if (FileType != NULL) {\r
     gFileExplorerPrivate.FileType = AllocateCopyPool (StrSize (FileType), FileType);\r
+    ASSERT(gFileExplorerPrivate.FileType != NULL);\r
     LibToLowerString(gFileExplorerPrivate.FileType);\r
   } else {\r
     gFileExplorerPrivate.FileType = NULL;\r
@@ -1444,7 +1474,12 @@ FileExplorerLibConstructor (
                   &gFileExplorerPrivate.FeConfigAccess,\r
                   NULL\r
                   );\r
-  ASSERT_EFI_ERROR (Status);\r
+  if (Status == EFI_ALREADY_STARTED) {\r
+    return EFI_SUCCESS;\r
+  }\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
 \r
   //\r
   // Post our File Explorer VFR binary to the HII database.\r
@@ -1488,17 +1523,19 @@ FileExplorerLibDestructor (
 \r
   ASSERT (gHiiVendorDevicePath != NULL);\r
 \r
-  Status = gBS->UninstallMultipleProtocolInterfaces (\r
-                  gFileExplorerPrivate.FeDriverHandle,\r
-                  &gEfiDevicePathProtocolGuid,\r
-                  gHiiVendorDevicePath,\r
-                  &gEfiHiiConfigAccessProtocolGuid,\r
-                  &gFileExplorerPrivate.FeConfigAccess,\r
-                  NULL\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-  \r
-  HiiRemovePackages (gFileExplorerPrivate.FeHiiHandle);\r
+  if (gFileExplorerPrivate.FeDriverHandle != NULL) {\r
+    Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                    gFileExplorerPrivate.FeDriverHandle,\r
+                    &gEfiDevicePathProtocolGuid,\r
+                    gHiiVendorDevicePath,\r
+                    &gEfiHiiConfigAccessProtocolGuid,\r
+                    &gFileExplorerPrivate.FeConfigAccess,\r
+                    NULL\r
+                    );\r
+    ASSERT_EFI_ERROR (Status);\r
+    \r
+    HiiRemovePackages (gFileExplorerPrivate.FeHiiHandle);\r
+  }\r
 \r
   FreePool (gHiiVendorDevicePath);\r
 \r