2 This file implement EfiMain() for library class DxeSmmDriverEntryPoint.
3 EfiMain() is common driver entry point for all SMM driver who uses DxeSmmDriverEntryPoint
6 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <FrameworkSmm.h>
14 #include <Protocol/LoadedImage.h>
15 #include <Protocol/SmmBase.h>
16 #include <Protocol/DevicePath.h>
18 #include <Library/UefiDriverEntryPoint.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/DevicePathLib.h>
24 This function returns the size, in bytes,
25 of the device path data structure specified by DevicePath.
26 If DevicePath is NULL, then 0 is returned.
28 @param DevicePath A pointer to a device path data structure.
30 @return The size of a device path in bytes.
35 SmmGetDevicePathSize (
36 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
39 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
41 if (DevicePath
== NULL
) {
46 // Search for the end of the device path structure
49 while (!IsDevicePathEnd (DevicePath
)) {
50 DevicePath
= NextDevicePathNode (DevicePath
);
54 // Compute the size and add back in the size of the end device path structure
56 return ((UINTN
) DevicePath
- (UINTN
) Start
) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
60 This function appends the device path SecondDevicePath
61 to every device path instance in FirstDevicePath.
63 @param FirstDevicePath A pointer to a device path data structure.
65 @param SecondDevicePath A pointer to a device path data structure.
67 @return A pointer to the new device path is returned.
68 NULL is returned if space for the new device path could not be allocated from pool.
69 It is up to the caller to free the memory used by FirstDevicePath and SecondDevicePath
70 if they are no longer needed.
73 EFI_DEVICE_PATH_PROTOCOL
*
76 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
,
77 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath
84 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
85 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
87 ASSERT (FirstDevicePath
!= NULL
&& SecondDevicePath
!= NULL
);
90 // Allocate space for the combined device path. It only has one end node of
91 // length EFI_DEVICE_PATH_PROTOCOL
93 Size1
= SmmGetDevicePathSize (FirstDevicePath
);
94 Size2
= SmmGetDevicePathSize (SecondDevicePath
);
95 Size
= Size1
+ Size2
- sizeof (EFI_DEVICE_PATH_PROTOCOL
);
97 Status
= gBS
->AllocatePool (EfiBootServicesData
, Size
, (VOID
**) &NewDevicePath
);
99 if (EFI_SUCCESS
== Status
) {
101 // CopyMem in gBS is used as this service should always be ready. We didn't choose
102 // to use a BaseMemoryLib function as such library instance may have constructor.
104 gBS
->CopyMem ((VOID
*) NewDevicePath
, (VOID
*) FirstDevicePath
, Size1
);
106 // Over write Src1 EndNode and do the copy
108 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+ (Size1
- sizeof (EFI_DEVICE_PATH_PROTOCOL
)));
109 gBS
->CopyMem ((VOID
*) DevicePath2
, (VOID
*) SecondDevicePath
, Size2
);
112 return NewDevicePath
;
116 Unload function that is registered in the LoadImage protocol. It un-installs
117 protocols produced and deallocates pool used by the driver. Called by the core
118 when unloading the driver.
120 @param ImageHandle ImageHandle of the unloaded driver
122 @return Status of the ProcessModuleUnloadList.
127 _DriverUnloadHandler (
128 EFI_HANDLE ImageHandle
132 // Call the unload handlers for all the modules.
134 // Note: All libraries were constructed in SMM space,
135 // therefore we can not destruct them in Unload
138 return ProcessModuleUnloadList (ImageHandle
);
142 Enrty point to DXE SMM Driver.
144 @param ImageHandle ImageHandle of the loaded driver.
145 @param SystemTable Pointer to the EFI System Table.
147 @retval EFI_SUCCESS One or more of the drivers returned a success code.
148 @retval !EFI_SUCESS The return status from the last driver entry point in the list.
154 IN EFI_HANDLE ImageHandle
,
155 IN EFI_SYSTEM_TABLE
*SystemTable
159 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
160 EFI_SMM_BASE_PROTOCOL
*SmmBase
;
162 EFI_DEVICE_PATH_PROTOCOL
*CompleteFilePath
;
163 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
167 // Cache a pointer to the Boot Services Table
169 gBS
= SystemTable
->BootServices
;
172 // Retrieve SMM Base Protocol
174 Status
= gBS
->LocateProtocol (
175 &gEfiSmmBaseProtocolGuid
,
179 ASSERT_EFI_ERROR (Status
);
182 // Check to see if we are already in SMM
184 SmmBase
->InSmm (SmmBase
, &InSmm
);
191 // Retrieve the Loaded Image Protocol
193 Status
= gBS
->HandleProtocol (
195 &gEfiLoadedImageProtocolGuid
,
198 ASSERT_EFI_ERROR (Status
);
200 // Retrieve the Device Path Protocol from the DeviceHandle from which this driver was loaded
202 Status
= gBS
->HandleProtocol (
203 LoadedImage
->DeviceHandle
,
204 &gEfiDevicePathProtocolGuid
,
205 (VOID
*)&ImageDevicePath
207 ASSERT_EFI_ERROR (Status
);
210 // Build the full device path to the currently execuing image
212 CompleteFilePath
= SmmAppendDevicePath (ImageDevicePath
, LoadedImage
->FilePath
);
215 // Load the image in memory to SMRAM; it will automatically generate the
218 Status
= SmmBase
->Register (SmmBase
, CompleteFilePath
, LoadedImage
->ImageBase
, 0, &Handle
, FALSE
);
219 ASSERT_EFI_ERROR (Status
);
221 // Optionally install the unload handler
223 if (_gDriverUnloadImageCount
> 0) {
224 Status
= gBS
->HandleProtocol (
226 &gEfiLoadedImageProtocolGuid
,
227 (VOID
**)&LoadedImage
229 ASSERT_EFI_ERROR (Status
);
230 LoadedImage
->Unload
= _DriverUnloadHandler
;
237 // Call constructor for all libraries
239 ProcessLibraryConstructorList (ImageHandle
, SystemTable
);
242 // Call the list of driver entry points
244 Status
= ProcessModuleEntryPointList (ImageHandle
, SystemTable
);
245 if (EFI_ERROR (Status
)) {
246 ProcessLibraryDestructorList (ImageHandle
, SystemTable
);
253 Enrty point wrapper of DXE SMM Driver.
255 @param ImageHandle ImageHandle of the loaded driver.
256 @param SystemTable Pointer to the EFI System Table.
258 @retval EFI_SUCCESS One or more of the drivers returned a success code.
259 @retval !EFI_SUCESS The return status from the last driver entry point in the list.
265 IN EFI_HANDLE ImageHandle
,
266 IN EFI_SYSTEM_TABLE
*SystemTable
269 return _ModuleEntryPoint (ImageHandle
, SystemTable
);