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, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <FrameworkSmm.h>
20 #include <Protocol/LoadedImage.h>
21 #include <Protocol/SmmBase.h>
22 #include <Protocol/DevicePath.h>
24 #include <Library/UefiDriverEntryPoint.h>
25 #include <Library/UefiBootServicesTableLib.h>
26 #include <Library/DebugLib.h>
27 #include <Library/DevicePathLib.h>
30 This function returns the size, in bytes,
31 of the device path data structure specified by DevicePath.
32 If DevicePath is NULL, then 0 is returned.
34 @param DevicePath A pointer to a device path data structure.
36 @return The size of a device path in bytes.
41 SmmGetDevicePathSize (
42 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
45 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
47 if (DevicePath
== NULL
) {
52 // Search for the end of the device path structure
55 while (!IsDevicePathEnd (DevicePath
)) {
56 DevicePath
= NextDevicePathNode (DevicePath
);
60 // Compute the size and add back in the size of the end device path structure
62 return ((UINTN
) DevicePath
- (UINTN
) Start
) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
66 This function appends the device path SecondDevicePath
67 to every device path instance in FirstDevicePath.
69 @param FirstDevicePath A pointer to a device path data structure.
71 @param SecondDevicePath A pointer to a device path data structure.
73 @return A pointer to the new device path is returned.
74 NULL is returned if space for the new device path could not be allocated from pool.
75 It is up to the caller to free the memory used by FirstDevicePath and SecondDevicePath
76 if they are no longer needed.
79 EFI_DEVICE_PATH_PROTOCOL
*
82 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
,
83 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath
90 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
91 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
93 ASSERT (FirstDevicePath
!= NULL
&& SecondDevicePath
!= NULL
);
96 // Allocate space for the combined device path. It only has one end node of
97 // length EFI_DEVICE_PATH_PROTOCOL
99 Size1
= SmmGetDevicePathSize (FirstDevicePath
);
100 Size2
= SmmGetDevicePathSize (SecondDevicePath
);
101 Size
= Size1
+ Size2
- sizeof (EFI_DEVICE_PATH_PROTOCOL
);
103 Status
= gBS
->AllocatePool (EfiBootServicesData
, Size
, (VOID
**) &NewDevicePath
);
105 if (EFI_SUCCESS
== Status
) {
107 // CopyMem in gBS is used as this service should always be ready. We didn't choose
108 // to use a BaseMemoryLib function as such library instance may have constructor.
110 gBS
->CopyMem ((VOID
*) NewDevicePath
, (VOID
*) FirstDevicePath
, Size1
);
112 // Over write Src1 EndNode and do the copy
114 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+ (Size1
- sizeof (EFI_DEVICE_PATH_PROTOCOL
)));
115 gBS
->CopyMem ((VOID
*) DevicePath2
, (VOID
*) SecondDevicePath
, Size2
);
118 return NewDevicePath
;
122 Unload function that is registered in the LoadImage protocol. It un-installs
123 protocols produced and deallocates pool used by the driver. Called by the core
124 when unloading the driver.
126 @param ImageHandle ImageHandle of the unloaded driver
128 @return Status of the ProcessModuleUnloadList.
133 _DriverUnloadHandler (
134 EFI_HANDLE ImageHandle
138 // Call the unload handlers for all the modules.
140 // Note: All libraries were constructed in SMM space,
141 // therefore we can not destruct them in Unload
144 return ProcessModuleUnloadList (ImageHandle
);
148 Enrty point to DXE SMM Driver.
150 @param ImageHandle ImageHandle of the loaded driver.
151 @param SystemTable Pointer to the EFI System Table.
153 @retval EFI_SUCCESS One or more of the drivers returned a success code.
154 @retval !EFI_SUCESS The return status from the last driver entry point in the list.
160 IN EFI_HANDLE ImageHandle
,
161 IN EFI_SYSTEM_TABLE
*SystemTable
165 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
166 EFI_SMM_BASE_PROTOCOL
*SmmBase
;
168 EFI_DEVICE_PATH_PROTOCOL
*CompleteFilePath
;
169 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
173 // Cache a pointer to the Boot Services Table
175 gBS
= SystemTable
->BootServices
;
178 // Retrieve SMM Base Protocol
180 Status
= gBS
->LocateProtocol (
181 &gEfiSmmBaseProtocolGuid
,
185 ASSERT_EFI_ERROR (Status
);
188 // Check to see if we are already in SMM
190 SmmBase
->InSmm (SmmBase
, &InSmm
);
197 // Retrieve the Loaded Image Protocol
199 Status
= gBS
->HandleProtocol (
201 &gEfiLoadedImageProtocolGuid
,
204 ASSERT_EFI_ERROR (Status
);
206 // Retrieve the Device Path Protocol from the DeviceHandle from which this driver was loaded
208 Status
= gBS
->HandleProtocol (
209 LoadedImage
->DeviceHandle
,
210 &gEfiDevicePathProtocolGuid
,
211 (VOID
*)&ImageDevicePath
213 ASSERT_EFI_ERROR (Status
);
216 // Build the full device path to the currently execuing image
218 CompleteFilePath
= SmmAppendDevicePath (ImageDevicePath
, LoadedImage
->FilePath
);
221 // Load the image in memory to SMRAM; it will automatically generate the
224 Status
= SmmBase
->Register (SmmBase
, CompleteFilePath
, LoadedImage
->ImageBase
, 0, &Handle
, FALSE
);
225 ASSERT_EFI_ERROR (Status
);
227 // Optionally install the unload handler
229 if (_gDriverUnloadImageCount
> 0) {
230 Status
= gBS
->HandleProtocol (
232 &gEfiLoadedImageProtocolGuid
,
233 (VOID
**)&LoadedImage
235 ASSERT_EFI_ERROR (Status
);
236 LoadedImage
->Unload
= _DriverUnloadHandler
;
243 // Call constructor for all libraries
245 ProcessLibraryConstructorList (ImageHandle
, SystemTable
);
248 // Call the list of driver entry points
250 Status
= ProcessModuleEntryPointList (ImageHandle
, SystemTable
);
251 if (EFI_ERROR (Status
)) {
252 ProcessLibraryDestructorList (ImageHandle
, SystemTable
);
259 Enrty point wrapper of DXE SMM Driver.
261 @param ImageHandle ImageHandle of the loaded driver.
262 @param SystemTable Pointer to the EFI System Table.
264 @retval EFI_SUCCESS One or more of the drivers returned a success code.
265 @retval !EFI_SUCESS The return status from the last driver entry point in the list.
271 IN EFI_HANDLE ImageHandle
,
272 IN EFI_SYSTEM_TABLE
*SystemTable
275 return _ModuleEntryPoint (ImageHandle
, SystemTable
);