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<BR>
7 All rights reserved. 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/DxeSmmDriverEntryPoint.h>
25 #include <Library/UefiBootServicesTableLib.h>
26 #include <Library/DebugLib.h>
29 EFI_BOOT_SERVICES
*mBS
;
32 This function returns the size, in bytes,
33 of the device path data structure specified by DevicePath.
34 If DevicePath is NULL, then 0 is returned.
36 @param DevicePath A pointer to a device path data structure.
38 @return The size of a device path in bytes.
44 SmmGetDevicePathSize (
45 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
48 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
50 if (DevicePath
== NULL
) {
55 // Search for the end of the device path structure
58 while (!EfiIsDevicePathEnd (DevicePath
)) {
59 DevicePath
= EfiNextDevicePathNode (DevicePath
);
63 // Compute the size and add back in the size of the end device path structure
65 return ((UINTN
) DevicePath
- (UINTN
) Start
) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
69 This function appends the device path SecondDevicePath
70 to every device path instance in FirstDevicePath.
72 @param FirstDevicePath A pointer to a device path data structure.
74 @param SecondDevicePath A pointer to a device path data structure.
76 @return A pointer to the new device path is returned.
77 NULL is returned if space for the new device path could not be allocated from pool.
78 It is up to the caller to free the memory used by FirstDevicePath and SecondDevicePath
79 if they are no longer needed.
82 EFI_DEVICE_PATH_PROTOCOL
*
85 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
,
86 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath
93 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
94 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
96 ASSERT (FirstDevicePath
!= NULL
&& SecondDevicePath
!= NULL
);
99 // Allocate space for the combined device path. It only has one end node of
100 // length EFI_DEVICE_PATH_PROTOCOL
102 Size1
= SmmGetDevicePathSize (FirstDevicePath
);
103 Size2
= SmmGetDevicePathSize (SecondDevicePath
);
104 Size
= Size1
+ Size2
- sizeof (EFI_DEVICE_PATH_PROTOCOL
);
106 Status
= mBS
->AllocatePool (EfiBootServicesData
, Size
, (VOID
**) &NewDevicePath
);
108 if (EFI_SUCCESS
== Status
) {
109 mBS
->CopyMem ((VOID
*) NewDevicePath
, (VOID
*) FirstDevicePath
, Size1
);
111 // Over write Src1 EndNode and do the copy
113 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+ (Size1
- sizeof (EFI_DEVICE_PATH_PROTOCOL
)));
114 mBS
->CopyMem ((VOID
*) DevicePath2
, (VOID
*) SecondDevicePath
, Size2
);
117 return NewDevicePath
;
121 Unload function that is registered in the LoadImage protocol. It un-installs
122 protocols produced and deallocates pool used by the driver. Called by the core
123 when unloading the driver.
125 @param ImageHandle ImageHandle of the unloaded driver
127 @return Status of the ProcessModuleUnloadList.
132 _DriverUnloadHandler (
133 EFI_HANDLE ImageHandle
139 // Call the unload handlers for all the modules
141 Status
= ProcessModuleUnloadList (ImageHandle
);
144 // If the driver specific unload handler does not return an error, then call all of the
145 // library destructors. If the unload handler returned an error, then the driver can not be
146 // unloaded, and the library destructors should not be called
148 if (!EFI_ERROR (Status
)) {
149 ProcessLibraryDestructorList (ImageHandle
, gST
);
153 // Return the status from the driver specific unload handler
159 Enrty point to DXE SMM Driver.
161 @param ImageHandle ImageHandle of the loaded driver.
162 @param SystemTable Pointer to the EFI System Table.
164 @retval EFI_SUCCESS One or more of the drivers returned a success code.
165 @retval !EFI_SUCESS The return status from the last driver entry point in the list.
171 IN EFI_HANDLE ImageHandle
,
172 IN EFI_SYSTEM_TABLE
*SystemTable
176 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
177 EFI_SMM_BASE_PROTOCOL
*SmmBase
;
179 EFI_DEVICE_PATH_PROTOCOL
*CompleteFilePath
;
180 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
184 // Cache a pointer to the Boot Services Table
186 mBS
= SystemTable
->BootServices
;
189 // Retrieve the Loaded Image Protocol
191 Status
= mBS
->HandleProtocol (
193 &gEfiLoadedImageProtocolGuid
,
196 ASSERT_EFI_ERROR (Status
);
199 // Retrieve SMM Base Protocol
201 Status
= mBS
->LocateProtocol (
202 &gEfiSmmBaseProtocolGuid
,
206 ASSERT_EFI_ERROR (Status
);
209 // Check to see if we are already in SMM
211 SmmBase
->InSmm (SmmBase
, &InSmm
);
218 // Retrieve the Device Path Protocol from the DeviceHandle tha this driver was loaded from
220 Status
= mBS
->HandleProtocol (
221 LoadedImage
->DeviceHandle
,
222 &gEfiDevicePathProtocolGuid
,
223 (VOID
*)&ImageDevicePath
225 ASSERT_EFI_ERROR (Status
);
228 // Build the full device path to the currently execuing image
230 CompleteFilePath
= SmmAppendDevicePath (ImageDevicePath
, LoadedImage
->FilePath
);
233 // Load the image in memory to SMRAM; it will automatically generate the
236 Status
= SmmBase
->Register (SmmBase
, CompleteFilePath
, NULL
, 0, &Handle
, FALSE
);
237 ASSERT_EFI_ERROR (Status
);
242 // Call constructor for all libraries
244 ProcessLibraryConstructorList (ImageHandle
, SystemTable
);
247 // Optionally install the unload handler
249 if (_gDriverUnloadImageCount
> 0) {
250 Status
= mBS
->HandleProtocol (
252 &gEfiLoadedImageProtocolGuid
,
253 (VOID
**)&LoadedImage
255 ASSERT_EFI_ERROR (Status
);
256 LoadedImage
->Unload
= _DriverUnloadHandler
;
260 // Call the list of driver entry points
262 Status
= ProcessModuleEntryPointList (ImageHandle
, SystemTable
);
263 if (EFI_ERROR (Status
)) {
264 ProcessLibraryDestructorList (ImageHandle
, SystemTable
);
271 Enrty point wrapper of DXE SMM Driver.
273 @param ImageHandle ImageHandle of the loaded driver.
274 @param SystemTable Pointer to the EFI System Table.
276 @retval EFI_SUCCESS One or more of the drivers returned a success code.
277 @retval !EFI_SUCESS The return status from the last driver entry point in the list.
283 IN EFI_HANDLE ImageHandle
,
284 IN EFI_SYSTEM_TABLE
*SystemTable
287 return _ModuleEntryPoint (ImageHandle
, SystemTable
);