]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Image/ImageFile.c
Add comments and DoxyGen format for these files.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Image / ImageFile.c
index 029f7cf2a7830d2ec62898fd9861e2b6c56c058e..7ff867e18105f35218c6c17b051124b958eda0f6 100644 (file)
@@ -1,27 +1,17 @@
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
-\r
-Module Name:\r
-  \r
-  ImageFile.c\r
-\r
-\r
-Abstract:\r
-\r
+/** @file\r
   \r
+  Handle services to image file. \r
+   \r
+Copyright (c) 2006 - 2008, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
 \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
-Revision History\r
-\r
---*/\r
+**/\r
 \r
 #include <DxeMain.h>\r
 \r
@@ -30,7 +20,7 @@ CoreOpenImageFile (
   IN BOOLEAN                        BootPolicy,\r
   IN VOID                           *SourceBuffer   OPTIONAL,\r
   IN UINTN                          SourceSize,\r
-  IN OUT EFI_DEVICE_PATH_PROTOCOL   *FilePath,\r
+  IN OUT EFI_DEVICE_PATH_PROTOCOL   **FilePath,\r
   OUT EFI_HANDLE                    *DeviceHandle,\r
   IN IMAGE_FILE_HANDLE              *ImageFileHandle,\r
   OUT UINT32                        *AuthenticationStatus\r
@@ -52,16 +42,16 @@ Arguments:
   FilePath      - The specific file path from which the image is loaded\r
   DeviceHandle  - Pointer to the return device handle.\r
   ImageFileHandle      - Pointer to the image file handle.\r
-  AuthenticationStatus - Pointer to a caller-allocated UINT32 in which the authentication status is returned. \r
-    \r
+  AuthenticationStatus - Pointer to a caller-allocated UINT32 in which the authentication status is returned.\r
+\r
 Returns:\r
 \r
     EFI_SUCCESS     - Image file successfully opened.\r
-    \r
+\r
     EFI_LOAD_ERROR  - If the caller passed a copy of the file, and SourceSize is 0.\r
-    \r
+\r
     EFI_INVALID_PARAMETER   - File path is not valid.\r
-    \r
+\r
     EFI_NOT_FOUND   - File not found.\r
 \r
 --*/\r
@@ -83,7 +73,9 @@ Returns:
   EFI_FILE_INFO                     *FileInfo;\r
   UINTN                             FileInfoSize;\r
   EFI_GUID                          *NameGuid;\r
+  FILEPATH_DEVICE_PATH              *OriginalFilePathNode;\r
 \r
+  OriginalFilePathNode = NULL;\r
   *AuthenticationStatus = 0;\r
   ZeroMem (ImageFileHandle, sizeof (IMAGE_FILE_HANDLE));\r
   ImageFileHandle->Signature = IMAGE_FILE_HANDLE_SIGNATURE;\r
@@ -95,6 +87,7 @@ Returns:
     ImageFileHandle->Source     = SourceBuffer;\r
     ImageFileHandle->SourceSize = SourceSize;\r
     *DeviceHandle     = NULL;\r
+    CoreLocateDevicePath (&gEfiDevicePathProtocolGuid, FilePath, DeviceHandle);\r
     if (SourceSize > 0) {\r
       Status = EFI_SUCCESS;\r
     } else {\r
@@ -106,18 +99,18 @@ Returns:
   //\r
   // Make sure FilePath is valid\r
   //\r
-  if (FilePath == NULL) {\r
+  if (*FilePath == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   //\r
   // Check to see if it's in a Firmware Volume\r
   //\r
-  FwVolFilePathNode = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)FilePath;\r
+  FwVolFilePathNode = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) *FilePath;\r
   Status = CoreDevicePathToInterface (\r
-            &gEfiFirmwareVolume2ProtocolGuid, \r
-            (EFI_DEVICE_PATH_PROTOCOL **)&FwVolFilePathNode, \r
-            (VOID*)&FwVol, \r
+            &gEfiFirmwareVolume2ProtocolGuid,\r
+            (EFI_DEVICE_PATH_PROTOCOL **)&FwVolFilePathNode,\r
+            (VOID*)&FwVol,\r
             DeviceHandle\r
             );\r
   if (!EFI_ERROR (Status)) {\r
@@ -130,9 +123,9 @@ Returns:
       SectionType = EFI_SECTION_PE32;\r
       Pe32Buffer  = NULL;\r
       Status = FwVol->ReadSection (\r
-                        FwVol, \r
-                        NameGuid,  \r
-                        SectionType,   \r
+                        FwVol,\r
+                        NameGuid,\r
+                        SectionType,\r
                         0,\r
                         (VOID **)&Pe32Buffer,\r
                         &Pe32BufferSize,\r
@@ -148,8 +141,8 @@ Returns:
         }\r
         Pe32Buffer = NULL;\r
         Status = FwVol->ReadFile (\r
-                          FwVol, \r
-                          NameGuid, \r
+                          FwVol,\r
+                          NameGuid,\r
                           (VOID **)&Pe32Buffer,\r
                           &Pe32BufferSize,\r
                           &Type,\r
@@ -157,7 +150,7 @@ Returns:
                           AuthenticationStatus\r
                           );\r
       }\r
-            \r
+\r
       if (!EFI_ERROR (Status)) {\r
         //\r
         // One of the reads passed so we are done\r
@@ -173,11 +166,11 @@ Returns:
   //\r
   // Attempt to access the file via a file system interface\r
   //\r
-  FilePathNode = (FILEPATH_DEVICE_PATH *) FilePath;\r
+  FilePathNode = (FILEPATH_DEVICE_PATH *) *FilePath;\r
   Status = CoreDevicePathToInterface (\r
-            &gEfiSimpleFileSystemProtocolGuid, \r
-            (EFI_DEVICE_PATH_PROTOCOL **)&FilePathNode, \r
-            (VOID*)&Volume, \r
+            &gEfiSimpleFileSystemProtocolGuid,\r
+            (EFI_DEVICE_PATH_PROTOCOL **)&FilePathNode,\r
+            (VOID*)&Volume,\r
             DeviceHandle\r
             );\r
   if (!EFI_ERROR (Status)) {\r
@@ -186,41 +179,57 @@ Returns:
     //\r
     Status = Volume->OpenVolume (Volume, &FileHandle);\r
     if (!EFI_ERROR (Status)) {\r
-     \r
       //\r
-      // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the\r
-      //  directory information and filename can be seperate. The goal is to inch\r
-      //  our way down each device path node and close the previous node\r
+      // Duplicate the device path to avoid the access to unaligned device path node.\r
+      // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH\r
+      // nodes, It assures the fields in device path nodes are 2 byte aligned. \r
       //\r
-      while (!IsDevicePathEnd (&FilePathNode->Header)) {\r
-        if (DevicePathType (&FilePathNode->Header) != MEDIA_DEVICE_PATH ||\r
-            DevicePathSubType (&FilePathNode->Header) != MEDIA_FILEPATH_DP) {\r
-          Status = EFI_UNSUPPORTED;\r
-        }\r
+      FilePathNode = (FILEPATH_DEVICE_PATH *)CoreDuplicateDevicePath((EFI_DEVICE_PATH_PROTOCOL *)(UINTN)FilePathNode);\r
+      if (FilePathNode == NULL) {\r
+        FileHandle->Close (FileHandle);\r
+        Status = EFI_OUT_OF_RESOURCES;\r
+      } else {\r
+        OriginalFilePathNode = FilePathNode;\r
+        //\r
+        // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the\r
+        // directory information and filename can be seperate. The goal is to inch\r
+        // our way down each device path node and close the previous node\r
+        //\r
+        while (!IsDevicePathEnd (&FilePathNode->Header)) {\r
+          if (DevicePathType (&FilePathNode->Header) != MEDIA_DEVICE_PATH ||\r
+              DevicePathSubType (&FilePathNode->Header) != MEDIA_FILEPATH_DP) {\r
+            Status = EFI_UNSUPPORTED;\r
+          }\r
+\r
+          if (EFI_ERROR (Status)) {\r
+            //\r
+            // Exit loop on Error\r
+            //\r
+            break;\r
+          }\r
+\r
+          LastHandle = FileHandle;\r
+          FileHandle = NULL;\r
+\r
+          Status = LastHandle->Open (\r
+                                LastHandle,\r
+                                &FileHandle,\r
+                                FilePathNode->PathName,\r
+                                EFI_FILE_MODE_READ,\r
+                                0\r
+                                );\r
 \r
-        if (EFI_ERROR (Status)) {\r
           //\r
-          // Exit loop on Error\r
+          // Close the previous node\r
           //\r
-          break;\r
-        }\r
-\r
-        LastHandle = FileHandle;\r
-        FileHandle = NULL;\r
-        Status = LastHandle->Open (\r
-                              LastHandle,\r
-                              &FileHandle,\r
-                              FilePathNode->PathName,\r
-                              EFI_FILE_MODE_READ,\r
-                              0\r
-                              );\r
+          LastHandle->Close (LastHandle);\r
 \r
+          FilePathNode = (FILEPATH_DEVICE_PATH *) NextDevicePathNode (&FilePathNode->Header);\r
+        }\r
         //\r
-        // Close the previous node\r
+        // Free the allocated memory pool \r
         //\r
-        LastHandle->Close (LastHandle);\r
-\r
-        FilePathNode = (FILEPATH_DEVICE_PATH *) NextDevicePathNode (&FilePathNode->Header);\r
+        CoreFreePool(OriginalFilePathNode);\r
       }\r
 \r
       if (!EFI_ERROR (Status)) {\r
@@ -238,7 +247,7 @@ Returns:
                                 FileHandle,\r
                                 &gEfiFileInfoGuid,\r
                                 &FileInfoSize,\r
-                                FileInfo                               \r
+                                FileInfo\r
                                 );\r
         }\r
         if (!EFI_ERROR (Status)) {\r
@@ -266,14 +275,14 @@ Returns:
         }\r
       }\r
     }\r
-  } \r
+  }\r
 \r
 \r
   //\r
   // Try LoadFile style\r
   //\r
 \r
-  TempFilePath = FilePath;\r
+  TempFilePath = *FilePath;\r
   Status = CoreDevicePathToInterface (\r
               &gEfiLoadFileProtocolGuid,\r
               &TempFilePath,\r
@@ -349,12 +358,12 @@ Routine Description:
 Arguments:\r
 \r
   UserHandle      - Image file handle\r
-  \r
+\r
   Offset          - Offset to the source file\r
-  \r
+\r
   ReadSize        - For input, pointer of size to read;\r
                     For output, pointer of size actually read.\r
-  \r
+\r
   Buffer          - Buffer to write into\r
 \r
 Returns:\r
@@ -375,7 +384,7 @@ Returns:
   EndPosition = Offset + *ReadSize;\r
   if (EndPosition > FHand->SourceSize) {\r
     *ReadSize = (UINT32)(FHand->SourceSize - Offset);\r
-  }  \r
+  }\r
   if (Offset >= FHand->SourceSize) {\r
       *ReadSize = 0;\r
   }\r
@@ -401,13 +410,13 @@ Routine Description:
 Arguments:\r
 \r
   Protocol      - The protocol to search for\r
-  \r
+\r
   FilePath      - The specified device path\r
-  \r
+\r
   Interface     - Interface of the protocol on the handle\r
-  \r
+\r
   Handle        - The handle to the device on the specified device path that supports the protocol.\r
-  \r
+\r
 Returns:\r
 \r
   Status code.\r
@@ -434,7 +443,7 @@ CoreGrowBuffer (
 Routine Description:\r
 \r
     Helper function called as part of the code needed\r
-    to allocate the proper sized buffer for various \r
+    to allocate the proper sized buffer for various\r
     EFI interfaces.\r
 \r
 Arguments:\r
@@ -444,10 +453,10 @@ Arguments:
     Buffer      - Current allocated buffer, or NULL\r
 \r
     BufferSize  - Current buffer size needed\r
-    \r
+\r
 Returns:\r
-    \r
-    TRUE - if the buffer was reallocated and the caller \r
+\r
+    TRUE - if the buffer was reallocated and the caller\r
     should try the API again.\r
 \r
     FALSE - buffer could not be allocated and the caller\r
@@ -472,7 +481,7 @@ Returns:
   //\r
   // If the status code is "buffer too small", resize the buffer\r
   //\r
-      \r
+\r
   if (*Status == EFI_BUFFER_TOO_SMALL) {\r
     if (*Buffer != NULL) {\r
       CoreFreePool (*Buffer);\r
@@ -481,9 +490,9 @@ Returns:
     *Buffer = CoreAllocateBootServicesPool (BufferSize);\r
     if (*Buffer != NULL) {\r
       TryAgain = TRUE;\r
-    } else {    \r
+    } else {\r
       *Status = EFI_OUT_OF_RESOURCES;\r
-    } \r
+    }\r
   }\r
 \r
   //\r