]> git.proxmox.com Git - mirror_edk2.git/blobdiff - FatPkg/EnhancedFatDxe/Fat.c
MdeModulePkg/FaultTolerantWriteDxe: implement standalone MM version
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / Fat.c
index 67ee66c026c19a1341c09b90c229da8beb0c774d..e29c64cb0347333f5668bb67b55e42aaea2e56ed 100644 (file)
@@ -1,6 +1,7 @@
-/*++\r
+/** @file\r
+  Fat File System driver routines that support EFI driver model.\r
 \r
-Copyright (c) 2005 - 2007, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2005 - 2014, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials are licensed and made available\r
 under the terms and conditions of the BSD License which accompanies this\r
 distribution. The full text of the license may be found at\r
@@ -9,19 +10,21 @@ http://opensource.org/licenses/bsd-license.php
 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
+**/\r
 \r
-Module Name:\r
+#include "Fat.h"\r
 \r
-  Fat.c\r
+/**\r
 \r
-Abstract:\r
+  Register Driver Binding protocol for this driver.\r
 \r
-  Fat File System driver routines that support EFI driver model\r
+  @param  ImageHandle           - Handle for the image of this driver.\r
+  @param  SystemTable           - Pointer to the EFI System Table.\r
 \r
---*/\r
-\r
-#include "Fat.h"\r
+  @retval EFI_SUCCESS           - Driver loaded.\r
+  @return other                 - Driver not loaded.\r
 \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 FatEntryPoint (\r
@@ -29,12 +32,36 @@ FatEntryPoint (
   IN EFI_SYSTEM_TABLE   *SystemTable\r
   );\r
 \r
+/**\r
+\r
+  Unload function for this image. Uninstall DriverBinding protocol.\r
+\r
+  @param ImageHandle           - Handle for the image of this driver.\r
+\r
+  @retval EFI_SUCCESS           - Driver unloaded successfully.\r
+  @return other                 - Driver can not unloaded.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 FatUnload (\r
   IN EFI_HANDLE         ImageHandle\r
   );\r
 \r
+/**\r
+\r
+  Test to see if this driver can add a file system to ControllerHandle.\r
+  ControllerHandle must support both Disk IO and Block IO protocols.\r
+\r
+  @param  This                  - Protocol instance pointer.\r
+  @param  ControllerHandle      - Handle of device to test.\r
+  @param  RemainingDevicePath   - Not used.\r
+\r
+  @retval EFI_SUCCESS           - This driver supports this device.\r
+  @retval EFI_ALREADY_STARTED   - This driver is already running on this device.\r
+  @return other                 - This driver does not support this device.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 FatDriverBindingSupported (\r
@@ -43,6 +70,22 @@ FatDriverBindingSupported (
   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath\r
   );\r
 \r
+/**\r
+\r
+  Start this driver on ControllerHandle by opening a Block IO and Disk IO\r
+  protocol, reading Device Path. Add a Simple File System protocol to\r
+  ControllerHandle if the media contains a valid file system.\r
+\r
+  @param  This                  - Protocol instance pointer.\r
+  @param  ControllerHandle      - Handle of device to bind driver to.\r
+  @param  RemainingDevicePath   - Not used.\r
+\r
+  @retval EFI_SUCCESS           - This driver is added to DeviceHandle.\r
+  @retval EFI_ALREADY_STARTED   - This driver is already running on DeviceHandle.\r
+  @retval EFI_OUT_OF_RESOURCES  - Can not allocate the memory.\r
+  @return other                 - This driver does not support this device.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 FatDriverBindingStart (\r
@@ -51,6 +94,19 @@ FatDriverBindingStart (
   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath\r
   );\r
 \r
+/**\r
+\r
+  Stop this driver on ControllerHandle.\r
+\r
+  @param  This                  - Protocol instance pointer.\r
+  @param  ControllerHandle      - Handle of device to stop driver on.\r
+  @param  NumberOfChildren      - Not used.\r
+  @param  ChildHandleBuffer     - Not used.\r
+\r
+  @retval EFI_SUCCESS           - This driver is removed DeviceHandle.\r
+  @return other                 - This driver was not removed from this device.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 FatDriverBindingStop (\r
@@ -72,29 +128,23 @@ EFI_DRIVER_BINDING_PROTOCOL gFatDriverBinding = {
   NULL\r
 };\r
 \r
+/**\r
+\r
+  Register Driver Binding protocol for this driver.\r
+\r
+  @param  ImageHandle           - Handle for the image of this driver.\r
+  @param  SystemTable           - Pointer to the EFI System Table.\r
+\r
+  @retval EFI_SUCCESS           - Driver loaded.\r
+  @return other                 - Driver not loaded.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 FatEntryPoint (\r
   IN EFI_HANDLE         ImageHandle,\r
   IN EFI_SYSTEM_TABLE   *SystemTable\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Register Driver Binding protocol for this driver.\r
-\r
-Arguments:\r
-\r
-  ImageHandle           - Handle for the image of this driver.\r
-  SystemTable           - Pointer to the EFI System Table.\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS           - Driver loaded.\r
-  other                 - Driver not loaded.\r
-\r
---*/\r
 {\r
   EFI_STATUS                Status;\r
 \r
@@ -114,32 +164,28 @@ Returns:
   return Status;\r
 }\r
 \r
-EFI_STATUS\r
-EFIAPI\r
-FatUnload (\r
-  IN EFI_HANDLE  ImageHandle\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
+/**\r
 \r
   Unload function for this image. Uninstall DriverBinding protocol.\r
 \r
-Arguments:\r
-\r
-  ImageHandle           - Handle for the image of this driver.\r
-\r
-Returns:\r
+  @param ImageHandle           - Handle for the image of this driver.\r
 \r
-  EFI_SUCCESS           - Driver unloaded successfully.\r
-  other                 - Driver can not unloaded.\r
+  @retval EFI_SUCCESS           - Driver unloaded successfully.\r
+  @return other                 - Driver can not unloaded.\r
 \r
---*/\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FatUnload (\r
+  IN EFI_HANDLE  ImageHandle\r
+  )\r
 {\r
   EFI_STATUS  Status;\r
   EFI_HANDLE  *DeviceHandleBuffer;\r
   UINTN       DeviceHandleCount;\r
   UINTN       Index;\r
+  VOID        *ComponentName;\r
+  VOID        *ComponentName2;\r
 \r
   Status = gBS->LocateHandleBuffer (\r
                   AllHandles,\r
@@ -148,23 +194,94 @@ Returns:
                   &DeviceHandleCount,\r
                   &DeviceHandleBuffer\r
                   );\r
-  if (!EFI_ERROR (Status)) {\r
-    for (Index = 0; Index < DeviceHandleCount; Index++) {\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  for (Index = 0; Index < DeviceHandleCount; Index++) {\r
+    Status = EfiTestManagedDevice (DeviceHandleBuffer[Index], ImageHandle, &gEfiDiskIoProtocolGuid);\r
+    if (!EFI_ERROR (Status)) {\r
       Status = gBS->DisconnectController (\r
                       DeviceHandleBuffer[Index],\r
                       ImageHandle,\r
                       NULL\r
                       );\r
+      if (EFI_ERROR (Status)) {\r
+        break;\r
+      }\r
+    }\r
+  }\r
+\r
+  if (Index == DeviceHandleCount) {\r
+    //\r
+    // Driver is stopped successfully.\r
+    //\r
+    Status = gBS->HandleProtocol (ImageHandle, &gEfiComponentNameProtocolGuid, &ComponentName);\r
+    if (EFI_ERROR (Status)) {\r
+      ComponentName = NULL;\r
+    }\r
+\r
+    Status = gBS->HandleProtocol (ImageHandle, &gEfiComponentName2ProtocolGuid, &ComponentName2);\r
+    if (EFI_ERROR (Status)) {\r
+      ComponentName2 = NULL;\r
     }\r
 \r
-    if (DeviceHandleBuffer != NULL) {\r
-      FreePool (DeviceHandleBuffer);\r
+    if (ComponentName == NULL) {\r
+      if (ComponentName2 == NULL) {\r
+        Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                        ImageHandle,\r
+                        &gEfiDriverBindingProtocolGuid,  &gFatDriverBinding,\r
+                        NULL\r
+                        );\r
+      } else {\r
+        Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                        ImageHandle,\r
+                        &gEfiDriverBindingProtocolGuid,  &gFatDriverBinding,\r
+                        &gEfiComponentName2ProtocolGuid, ComponentName2,\r
+                        NULL\r
+                        );\r
+      }\r
+    } else {\r
+      if (ComponentName2 == NULL) {\r
+        Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                        ImageHandle,\r
+                        &gEfiDriverBindingProtocolGuid,  &gFatDriverBinding,\r
+                        &gEfiComponentNameProtocolGuid,  ComponentName,\r
+                        NULL\r
+                        );\r
+      } else {\r
+        Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                        ImageHandle,\r
+                        &gEfiDriverBindingProtocolGuid,  &gFatDriverBinding,\r
+                        &gEfiComponentNameProtocolGuid,  ComponentName,\r
+                        &gEfiComponentName2ProtocolGuid, ComponentName2,\r
+                        NULL\r
+                        );\r
+      }\r
     }\r
   }\r
 \r
+  if (DeviceHandleBuffer != NULL) {\r
+    FreePool (DeviceHandleBuffer);\r
+  }\r
+\r
   return Status;\r
 }\r
 \r
+/**\r
+\r
+  Test to see if this driver can add a file system to ControllerHandle.\r
+  ControllerHandle must support both Disk IO and Block IO protocols.\r
+\r
+  @param  This                  - Protocol instance pointer.\r
+  @param  ControllerHandle      - Handle of device to test.\r
+  @param  RemainingDevicePath   - Not used.\r
+\r
+  @retval EFI_SUCCESS           - This driver supports this device.\r
+  @retval EFI_ALREADY_STARTED   - This driver is already running on this device.\r
+  @return other                 - This driver does not support this device.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 FatDriverBindingSupported (\r
@@ -172,26 +289,6 @@ FatDriverBindingSupported (
   IN EFI_HANDLE                   ControllerHandle,\r
   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Test to see if this driver can add a file system to ControllerHandle.\r
-  ControllerHandle must support both Disk IO and Block IO protocols.\r
-\r
-Arguments:\r
-\r
-  This                  - Protocol instance pointer.\r
-  ControllerHandle      - Handle of device to test.\r
-  RemainingDevicePath   - Not used.\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS           - This driver supports this device.\r
-  EFI_ALREADY_STARTED   - This driver is already running on this device.\r
-  other                 - This driver does not support this device.\r
-\r
---*/\r
 {\r
   EFI_STATUS            Status;\r
   EFI_DISK_IO_PROTOCOL  *DiskIo;\r
@@ -236,43 +333,49 @@ Returns:
   return Status;\r
 }\r
 \r
-EFI_STATUS\r
-EFIAPI\r
-FatDriverBindingStart (\r
-  IN EFI_DRIVER_BINDING_PROTOCOL  *This,\r
-  IN EFI_HANDLE                   ControllerHandle,\r
-  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
+/**\r
 \r
   Start this driver on ControllerHandle by opening a Block IO and Disk IO\r
   protocol, reading Device Path. Add a Simple File System protocol to\r
   ControllerHandle if the media contains a valid file system.\r
 \r
-Arguments:\r
+  @param  This                  - Protocol instance pointer.\r
+  @param  ControllerHandle      - Handle of device to bind driver to.\r
+  @param  RemainingDevicePath   - Not used.\r
 \r
-  This                  - Protocol instance pointer.\r
-  ControllerHandle      - Handle of device to bind driver to.\r
-  RemainingDevicePath   - Not used.\r
+  @retval EFI_SUCCESS           - This driver is added to DeviceHandle.\r
+  @retval EFI_ALREADY_STARTED   - This driver is already running on DeviceHandle.\r
+  @retval EFI_OUT_OF_RESOURCES  - Can not allocate the memory.\r
+  @return other                 - This driver does not support this device.\r
 \r
-Returns:\r
-\r
-  EFI_SUCCESS           - This driver is added to DeviceHandle.\r
-  EFI_ALREADY_STARTED   - This driver is already running on DeviceHandle.\r
-  EFI_OUT_OF_RESOURCES  - Can not allocate the memory.\r
-  other                 - This driver does not support this device.\r
-\r
---*/\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FatDriverBindingStart (\r
+  IN EFI_DRIVER_BINDING_PROTOCOL  *This,\r
+  IN EFI_HANDLE                   ControllerHandle,\r
+  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath\r
+  )\r
 {\r
   EFI_STATUS            Status;\r
   EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
   EFI_DISK_IO_PROTOCOL  *DiskIo;\r
+  EFI_DISK_IO2_PROTOCOL *DiskIo2;\r
+  BOOLEAN               LockedByMe;\r
+\r
+  LockedByMe = FALSE;\r
+  //\r
+  // Acquire the lock.\r
+  // If caller has already acquired the lock, cannot lock it again.\r
+  //\r
+  Status = FatAcquireLockOrFail ();\r
+  if (!EFI_ERROR (Status)) {\r
+    LockedByMe = TRUE;\r
+  }\r
 \r
   Status = InitializeUnicodeCollationSupport (This->DriverBindingHandle);\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto Exit;\r
   }\r
   //\r
   // Open our required BlockIo and DiskIo\r
@@ -286,7 +389,7 @@ Returns:
                   EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto Exit;\r
   }\r
 \r
   Status = gBS->OpenProtocol (\r
@@ -298,13 +401,26 @@ Returns:
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto Exit;\r
+  }\r
+\r
+  Status = gBS->OpenProtocol (\r
+                  ControllerHandle,\r
+                  &gEfiDiskIo2ProtocolGuid,\r
+                  (VOID **) &DiskIo2,\r
+                  This->DriverBindingHandle,\r
+                  ControllerHandle,\r
+                  EFI_OPEN_PROTOCOL_BY_DRIVER\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    DiskIo2 = NULL;\r
   }\r
+\r
   //\r
   // Allocate Volume structure. In FatAllocateVolume(), Resources\r
   // are allocated with protocol installed and cached initialized\r
   //\r
-  Status = FatAllocateVolume (ControllerHandle, DiskIo, BlockIo);\r
+  Status = FatAllocateVolume (ControllerHandle, DiskIo, DiskIo2, BlockIo);\r
 \r
   //\r
   // When the media changes on a device it will Reinstall the BlockIo interaface.\r
@@ -327,12 +443,38 @@ Returns:
              This->DriverBindingHandle,\r
              ControllerHandle\r
              );\r
+      gBS->CloseProtocol (\r
+             ControllerHandle,\r
+             &gEfiDiskIo2ProtocolGuid,\r
+             This->DriverBindingHandle,\r
+             ControllerHandle\r
+             );\r
     }\r
   }\r
 \r
+Exit:\r
+  //\r
+  // Unlock if locked by myself.\r
+  //\r
+  if (LockedByMe) {\r
+    FatReleaseLock ();\r
+  }\r
   return Status;\r
 }\r
 \r
+/**\r
+\r
+  Stop this driver on ControllerHandle.\r
+\r
+  @param  This                  - Protocol instance pointer.\r
+  @param  ControllerHandle      - Handle of device to stop driver on.\r
+  @param  NumberOfChildren      - Not used.\r
+  @param  ChildHandleBuffer     - Not used.\r
+\r
+  @retval EFI_SUCCESS           - This driver is removed DeviceHandle.\r
+  @return other                 - This driver was not removed from this device.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 FatDriverBindingStop (\r
@@ -341,27 +483,13 @@ FatDriverBindingStop (
   IN  UINTN                         NumberOfChildren,\r
   IN  EFI_HANDLE                    *ChildHandleBuffer\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Stop this driver on ControllerHandle.\r
-\r
-Arguments:\r
-  This                  - Protocol instance pointer.\r
-  ControllerHandle      - Handle of device to stop driver on.\r
-  NumberOfChildren      - Not used.\r
-  ChildHandleBuffer     - Not used.\r
-\r
-Returns:\r
-  EFI_SUCCESS           - This driver is removed DeviceHandle.\r
-  other                 - This driver was not removed from this device.\r
-\r
---*/\r
 {\r
   EFI_STATUS                      Status;\r
   EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem;\r
   FAT_VOLUME                      *Volume;\r
+  EFI_DISK_IO2_PROTOCOL           *DiskIo2;\r
 \r
+  DiskIo2 = NULL;\r
   //\r
   // Get our context back\r
   //\r
@@ -375,19 +503,29 @@ Returns:
                   );\r
 \r
   if (!EFI_ERROR (Status)) {\r
-    Volume = VOLUME_FROM_VOL_INTERFACE (FileSystem);\r
-    Status = FatAbandonVolume (Volume);\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
+    Volume  = VOLUME_FROM_VOL_INTERFACE (FileSystem);\r
+    DiskIo2 = Volume->DiskIo2;\r
+    Status  = FatAbandonVolume (Volume);\r
   }\r
 \r
-  Status = gBS->CloseProtocol (\r
-                  ControllerHandle,\r
-                  &gEfiDiskIoProtocolGuid,\r
-                  This->DriverBindingHandle,\r
-                  ControllerHandle\r
-                  );\r
+  if (!EFI_ERROR (Status)) {\r
+    if (DiskIo2 != NULL) {\r
+      Status = gBS->CloseProtocol (\r
+        ControllerHandle,\r
+        &gEfiDiskIo2ProtocolGuid,\r
+        This->DriverBindingHandle,\r
+        ControllerHandle\r
+        );\r
+      ASSERT_EFI_ERROR (Status);\r
+    }\r
+    Status = gBS->CloseProtocol (\r
+      ControllerHandle,\r
+      &gEfiDiskIoProtocolGuid,\r
+      This->DriverBindingHandle,\r
+      ControllerHandle\r
+      );\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
 \r
   return Status;\r
 }\r