1 /**@file
2   Functions related to the Firmware Volume Block service whose
3   implementation is specific to the runtime DXE driver build.
4 
5   Copyright (C) 2015, Red Hat, Inc.
6   Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
7 
8   This program and the accompanying materials are licensed and made available
9   under the terms and conditions of the BSD License which accompanies this
10   distribution.  The full text of the license may be found at
11   http://opensource.org/licenses/bsd-license.php
12 
13   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 **/
16 
17 #include <Guid/EventGroup.h>
18 #include <Library/DebugLib.h>
19 #include <Library/DevicePathLib.h>
20 #include <Library/PcdLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
22 #include <Library/UefiRuntimeLib.h>
23 #include <Protocol/DevicePath.h>
24 #include <Protocol/FirmwareVolumeBlock.h>
25 
26 #include "FwBlockService.h"
27 #include "QemuFlash.h"
28 
29 VOID
InstallProtocolInterfaces(IN EFI_FW_VOL_BLOCK_DEVICE * FvbDevice)30 InstallProtocolInterfaces (
31   IN EFI_FW_VOL_BLOCK_DEVICE *FvbDevice
32   )
33 {
34   EFI_STATUS                         Status;
35   EFI_HANDLE                         FwbHandle;
36   EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *OldFwbInterface;
37 
38   ASSERT (!FeaturePcdGet (PcdSmmSmramRequire));
39 
40   //
41   // Find a handle with a matching device path that has supports FW Block
42   // protocol
43   //
44   Status = gBS->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid,
45                   &FvbDevice->DevicePath, &FwbHandle);
46   if (EFI_ERROR (Status)) {
47     //
48     // LocateDevicePath fails so install a new interface and device path
49     //
50     FwbHandle = NULL;
51     DEBUG ((EFI_D_INFO, "Installing QEMU flash FVB\n"));
52     Status = gBS->InstallMultipleProtocolInterfaces (
53                     &FwbHandle,
54                     &gEfiFirmwareVolumeBlockProtocolGuid,
55                     &FvbDevice->FwVolBlockInstance,
56                     &gEfiDevicePathProtocolGuid,
57                     FvbDevice->DevicePath,
58                     NULL
59                     );
60     ASSERT_EFI_ERROR (Status);
61   } else if (IsDevicePathEnd (FvbDevice->DevicePath)) {
62     //
63     // Device already exists, so reinstall the FVB protocol
64     //
65     Status = gBS->HandleProtocol (
66                     FwbHandle,
67                     &gEfiFirmwareVolumeBlockProtocolGuid,
68                     (VOID**)&OldFwbInterface
69                     );
70     ASSERT_EFI_ERROR (Status);
71 
72     DEBUG ((EFI_D_INFO, "Reinstalling FVB for QEMU flash region\n"));
73     Status = gBS->ReinstallProtocolInterface (
74                     FwbHandle,
75                     &gEfiFirmwareVolumeBlockProtocolGuid,
76                     OldFwbInterface,
77                     &FvbDevice->FwVolBlockInstance
78                     );
79     ASSERT_EFI_ERROR (Status);
80   } else {
81     //
82     // There was a FVB protocol on an End Device Path node
83     //
84     ASSERT (FALSE);
85   }
86 }
87 
88 
89 STATIC
90 VOID
91 EFIAPI
FvbVirtualAddressChangeEvent(IN EFI_EVENT Event,IN VOID * Context)92 FvbVirtualAddressChangeEvent (
93   IN EFI_EVENT        Event,
94   IN VOID             *Context
95   )
96 /*++
97 
98   Routine Description:
99 
100     Fixup internal data so that EFI and SAL can be call in virtual mode.
101     Call the passed in Child Notify event and convert the mFvbModuleGlobal
102     date items to there virtual address.
103 
104   Arguments:
105 
106     (Standard EFI notify event - EFI_EVENT_NOTIFY)
107 
108   Returns:
109 
110     None
111 
112 --*/
113 {
114   EFI_FW_VOL_INSTANCE *FwhInstance;
115   UINTN               Index;
116 
117   FwhInstance = mFvbModuleGlobal->FvInstance;
118   EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal->FvInstance);
119 
120   //
121   // Convert the base address of all the instances
122   //
123   Index       = 0;
124   while (Index < mFvbModuleGlobal->NumFv) {
125     EfiConvertPointer (0x0, (VOID **) &FwhInstance->FvBase);
126     FwhInstance = (EFI_FW_VOL_INSTANCE *)
127       (
128         (UINTN) ((UINT8 *) FwhInstance) +
129         FwhInstance->VolumeHeader.HeaderLength +
130         (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))
131       );
132     Index++;
133   }
134 
135   EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal);
136   QemuFlashConvertPointers ();
137 }
138 
139 
140 VOID
InstallVirtualAddressChangeHandler(VOID)141 InstallVirtualAddressChangeHandler (
142   VOID
143   )
144 {
145   EFI_STATUS Status;
146   EFI_EVENT  VirtualAddressChangeEvent;
147 
148   Status = gBS->CreateEventEx (
149                   EVT_NOTIFY_SIGNAL,
150                   TPL_NOTIFY,
151                   FvbVirtualAddressChangeEvent,
152                   NULL,
153                   &gEfiEventVirtualAddressChangeGuid,
154                   &VirtualAddressChangeEvent
155                   );
156   ASSERT_EFI_ERROR (Status);
157 }
158