]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/XenPvBlkDxe/XenPvBlkDxe.c
MdeModulePkg: Add missing instances for build only
[mirror_edk2.git] / OvmfPkg / XenPvBlkDxe / XenPvBlkDxe.c
index a5a69537ee137bcb219431037abdb3539c3d8ac3..bfe7b1a754cf208031f8f651350fe6e1d6ecc61d 100644 (file)
@@ -8,18 +8,14 @@
 \r
   Copyright (C) 2014, Citrix Ltd.\r
 \r
-  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
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include "XenPvBlkDxe.h"\r
 \r
+#include "BlockFront.h"\r
+\r
 \r
 ///\r
 /// Driver Binding Protocol instance\r
@@ -258,6 +254,8 @@ XenPvBlkDxeDriverBindingStart (
 {\r
   EFI_STATUS Status;\r
   XENBUS_PROTOCOL *XenBusIo;\r
+  XEN_BLOCK_FRONT_DEVICE *Dev;\r
+  EFI_BLOCK_IO_MEDIA *Media;\r
 \r
   Status = gBS->OpenProtocol (\r
                 ControllerHandle,\r
@@ -271,7 +269,54 @@ XenPvBlkDxeDriverBindingStart (
     return Status;\r
   }\r
 \r
+  Status = XenPvBlockFrontInitialization (XenBusIo, XenBusIo->Node, &Dev);\r
+  if (EFI_ERROR (Status)) {\r
+    goto CloseProtocol;\r
+  }\r
+\r
+  CopyMem (&Dev->BlockIo, &gXenPvBlkDxeBlockIo, sizeof (EFI_BLOCK_IO_PROTOCOL));\r
+  Media = AllocateCopyPool (sizeof (EFI_BLOCK_IO_MEDIA),\r
+                            &gXenPvBlkDxeBlockIoMedia);\r
+  if (Dev->MediaInfo.VDiskInfo & VDISK_REMOVABLE) {\r
+    Media->RemovableMedia = TRUE;\r
+  }\r
+  Media->MediaPresent = TRUE;\r
+  Media->ReadOnly = !Dev->MediaInfo.ReadWrite;\r
+  if (Dev->MediaInfo.CdRom) {\r
+    //\r
+    // If it's a cdrom, the blocksize value need to be 2048 for OVMF to\r
+    // recognize it as a cdrom:\r
+    //    MdeModulePkg/Universal/Disk/PartitionDxe/ElTorito.c\r
+    //\r
+    Media->BlockSize = 2048;\r
+    Media->LastBlock = DivU64x32 (Dev->MediaInfo.Sectors,\r
+                                  Media->BlockSize / Dev->MediaInfo.SectorSize) - 1;\r
+  } else {\r
+    Media->BlockSize = Dev->MediaInfo.SectorSize;\r
+    Media->LastBlock = Dev->MediaInfo.Sectors - 1;\r
+  }\r
+  ASSERT (Media->BlockSize % 512 == 0);\r
+  Dev->BlockIo.Media = Media;\r
+\r
+  Status = gBS->InstallMultipleProtocolInterfaces (\r
+                    &ControllerHandle,\r
+                    &gEfiBlockIoProtocolGuid, &Dev->BlockIo,\r
+                    NULL\r
+                    );\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "XenPvBlk: install protocol fail: %r\n", Status));\r
+    goto UninitBlockFront;\r
+  }\r
+\r
   return EFI_SUCCESS;\r
+\r
+UninitBlockFront:\r
+  FreePool (Media);\r
+  XenPvBlockFrontShutdown (Dev);\r
+CloseProtocol:\r
+  gBS->CloseProtocol (ControllerHandle, &gXenBusProtocolGuid,\r
+                      This->DriverBindingHandle, ControllerHandle);\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -309,6 +354,33 @@ XenPvBlkDxeDriverBindingStop (
   IN EFI_HANDLE                   *ChildHandleBuffer OPTIONAL\r
   )\r
 {\r
+  EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
+  XEN_BLOCK_FRONT_DEVICE *Dev;\r
+  EFI_BLOCK_IO_MEDIA *Media;\r
+  EFI_STATUS Status;\r
+\r
+  Status = gBS->OpenProtocol (\r
+                  ControllerHandle, &gEfiBlockIoProtocolGuid,\r
+                  (VOID **)&BlockIo,\r
+                  This->DriverBindingHandle, ControllerHandle,\r
+                  EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Status = gBS->UninstallProtocolInterface (ControllerHandle,\r
+                  &gEfiBlockIoProtocolGuid, BlockIo);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Media = BlockIo->Media;\r
+  Dev = XEN_BLOCK_FRONT_FROM_BLOCK_IO (BlockIo);\r
+  XenPvBlockFrontShutdown (Dev);\r
+\r
+  FreePool (Media);\r
+\r
   gBS->CloseProtocol (ControllerHandle, &gXenBusProtocolGuid,\r
          This->DriverBindingHandle, ControllerHandle);\r
 \r