2 Entry point to a EFI/DXE driver.
4 Copyright (c) 2006, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 // The package level header files this module uses
18 #include <FrameworkSmm.h>
20 // The protocols, PPI and GUID defintions for this module
22 #include <Protocol/LoadedImage.h>
23 #include <Protocol/SmmBase.h>
24 #include <Protocol/DevicePath.h>
26 // The Library classes this module consumes
28 #include <Library/DxeSmmDriverEntryPoint.h>
29 #include <Library/UefiBootServicesTableLib.h>
30 #include <Library/DebugLib.h>
33 EFI_BOOT_SERVICES
*mBS
;
36 This function returns the size, in bytes,
37 of the device path data structure specified by DevicePath.
38 If DevicePath is NULL, then 0 is returned.
40 @param DevicePath A pointer to a device path data structure.
42 @return The size of a device path in bytes.
48 SmmGetDevicePathSize (
49 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
52 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
54 if (DevicePath
== NULL
) {
59 // Search for the end of the device path structure
62 while (!EfiIsDevicePathEnd (DevicePath
)) {
63 DevicePath
= EfiNextDevicePathNode (DevicePath
);
67 // Compute the size and add back in the size of the end device path structure
69 return ((UINTN
) DevicePath
- (UINTN
) Start
) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
73 This function appends the device path SecondDevicePath
74 to every device path instance in FirstDevicePath.
76 @param FirstDevicePath A pointer to a device path data structure.
78 @param SecondDevicePath A pointer to a device path data structure.
80 @return A pointer to the new device path is returned.
81 NULL is returned if space for the new device path could not be allocated from pool.
82 It is up to the caller to free the memory used by FirstDevicePath and SecondDevicePath
83 if they are no longer needed.
86 EFI_DEVICE_PATH_PROTOCOL
*
89 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
,
90 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath
97 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
98 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
100 ASSERT (FirstDevicePath
!= NULL
&& SecondDevicePath
!= NULL
);
103 // Allocate space for the combined device path. It only has one end node of
104 // length EFI_DEVICE_PATH_PROTOCOL
106 Size1
= SmmGetDevicePathSize (FirstDevicePath
);
107 Size2
= SmmGetDevicePathSize (SecondDevicePath
);
108 Size
= Size1
+ Size2
- sizeof (EFI_DEVICE_PATH_PROTOCOL
);
110 Status
= mBS
->AllocatePool (EfiBootServicesData
, Size
, (VOID
**) &NewDevicePath
);
112 if (EFI_SUCCESS
== Status
) {
113 mBS
->CopyMem ((VOID
*) NewDevicePath
, (VOID
*) FirstDevicePath
, Size1
);
115 // Over write Src1 EndNode and do the copy
117 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+ (Size1
- sizeof (EFI_DEVICE_PATH_PROTOCOL
)));
118 mBS
->CopyMem ((VOID
*) DevicePath2
, (VOID
*) SecondDevicePath
, Size2
);
121 return NewDevicePath
;
125 Unload function that is registered in the LoadImage protocol. It un-installs
126 protocols produced and deallocates pool used by the driver. Called by the core
127 when unloading the driver.
129 @param ImageHandle ImageHandle of the unloaded driver
131 @return Status of the ProcessModuleUnloadList.
136 _DriverUnloadHandler (
137 EFI_HANDLE ImageHandle
143 // Call the unload handlers for all the modules
145 Status
= ProcessModuleUnloadList (ImageHandle
);
148 // If the driver specific unload handler does not return an error, then call all of the
149 // library destructors. If the unload handler returned an error, then the driver can not be
150 // unloaded, and the library destructors should not be called
152 if (!EFI_ERROR (Status
)) {
153 ProcessLibraryDestructorList (ImageHandle
, gST
);
157 // Return the status from the driver specific unload handler
163 Enrty point to DXE SMM Driver.
165 @param ImageHandle ImageHandle of the loaded driver.
166 @param SystemTable Pointer to the EFI System Table.
168 @retval EFI_SUCCESS One or more of the drivers returned a success code.
169 @retval !EFI_SUCESS The return status from the last driver entry point in the list.
175 IN EFI_HANDLE ImageHandle
,
176 IN EFI_SYSTEM_TABLE
*SystemTable
180 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
181 EFI_SMM_BASE_PROTOCOL
*SmmBase
;
183 EFI_DEVICE_PATH_PROTOCOL
*CompleteFilePath
;
184 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
188 // Cache a pointer to the Boot Services Table
190 mBS
= SystemTable
->BootServices
;
193 // Retrieve the Loaded Image Protocol
195 Status
= mBS
->HandleProtocol (
197 &gEfiLoadedImageProtocolGuid
,
200 ASSERT_EFI_ERROR (Status
);
203 // Retrieve SMM Base Protocol
205 Status
= mBS
->LocateProtocol (
206 &gEfiSmmBaseProtocolGuid
,
210 ASSERT_EFI_ERROR (Status
);
213 // Check to see if we are already in SMM
215 SmmBase
->InSmm (SmmBase
, &InSmm
);
222 // Retrieve the Device Path Protocol from the DeviceHandle tha this driver was loaded from
224 Status
= mBS
->HandleProtocol (
225 LoadedImage
->DeviceHandle
,
226 &gEfiDevicePathProtocolGuid
,
227 (VOID
*)&ImageDevicePath
229 ASSERT_EFI_ERROR (Status
);
232 // Build the full device path to the currently execuing image
234 CompleteFilePath
= SmmAppendDevicePath (ImageDevicePath
, LoadedImage
->FilePath
);
237 // Load the image in memory to SMRAM; it will automatically generate the
240 Status
= SmmBase
->Register (SmmBase
, CompleteFilePath
, NULL
, 0, &Handle
, FALSE
);
241 ASSERT_EFI_ERROR (Status
);
246 // Call constructor for all libraries
248 ProcessLibraryConstructorList (ImageHandle
, SystemTable
);
251 // Optionally install the unload handler
253 if (_gDriverUnloadImageCount
> 0) {
254 Status
= mBS
->HandleProtocol (
256 &gEfiLoadedImageProtocolGuid
,
257 (VOID
**)&LoadedImage
259 ASSERT_EFI_ERROR (Status
);
260 LoadedImage
->Unload
= _DriverUnloadHandler
;
264 // Call the list of driver entry points
266 Status
= ProcessModuleEntryPointList (ImageHandle
, SystemTable
);
267 if (EFI_ERROR (Status
)) {
268 ProcessLibraryDestructorList (ImageHandle
, SystemTable
);
275 Enrty point wrapper of DXE SMM Driver.
277 @param ImageHandle ImageHandle of the loaded driver.
278 @param SystemTable Pointer to the EFI System Table.
280 @retval EFI_SUCCESS One or more of the drivers returned a success code.
281 @retval !EFI_SUCESS The return status from the last driver entry point in the list.
287 IN EFI_HANDLE ImageHandle
,
288 IN EFI_SYSTEM_TABLE
*SystemTable
291 return _ModuleEntryPoint (ImageHandle
, SystemTable
);