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.
15 #include <FrameworkDxe.h>
16 #include <Library/UefiBootServicesTableLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/DxeSmmDriverEntryPoint.h>
20 #include <Protocol/LoadedImage.h>
21 #include <Protocol/SmmBase.h>
23 EFI_BOOT_SERVICES
*mBS
;
26 This function returns the size, in bytes,
27 of the device path data structure specified by DevicePath.
28 If DevicePath is NULL, then 0 is returned.
30 @param DevicePath A pointer to a device path data structure.
32 @return The size of a device path in bytes.
38 SmmGetDevicePathSize (
39 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
42 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
44 if (DevicePath
== NULL
) {
49 // Search for the end of the device path structure
52 while (!EfiIsDevicePathEnd (DevicePath
)) {
53 DevicePath
= EfiNextDevicePathNode (DevicePath
);
57 // Compute the size and add back in the size of the end device path structure
59 return ((UINTN
) DevicePath
- (UINTN
) Start
) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
63 This function appends the device path SecondDevicePath
64 to every device path instance in FirstDevicePath.
66 @param FirstDevicePath A pointer to a device path data structure.
68 @param SecondDevicePath A pointer to a device path data structure.
70 @return A pointer to the new device path is returned.
71 NULL is returned if space for the new device path could not be allocated from pool.
72 It is up to the caller to free the memory used by FirstDevicePath and SecondDevicePath
73 if they are no longer needed.
76 EFI_DEVICE_PATH_PROTOCOL
*
79 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
,
80 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath
87 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
88 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
90 ASSERT (FirstDevicePath
!= NULL
&& SecondDevicePath
!= NULL
);
93 // Allocate space for the combined device path. It only has one end node of
94 // length EFI_DEVICE_PATH_PROTOCOL
96 Size1
= SmmGetDevicePathSize (FirstDevicePath
);
97 Size2
= SmmGetDevicePathSize (SecondDevicePath
);
98 Size
= Size1
+ Size2
- sizeof (EFI_DEVICE_PATH_PROTOCOL
);
100 Status
= mBS
->AllocatePool (EfiBootServicesData
, Size
, (VOID
**) &NewDevicePath
);
102 if (EFI_SUCCESS
== Status
) {
103 mBS
->CopyMem ((VOID
*) NewDevicePath
, (VOID
*) FirstDevicePath
, Size1
);
105 // Over write Src1 EndNode and do the copy
107 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+ (Size1
- sizeof (EFI_DEVICE_PATH_PROTOCOL
)));
108 mBS
->CopyMem ((VOID
*) DevicePath2
, (VOID
*) SecondDevicePath
, Size2
);
111 return NewDevicePath
;
115 Unload function that is registered in the LoadImage protocol. It un-installs
116 protocols produced and deallocates pool used by the driver. Called by the core
117 when unloading the driver.
119 @param ImageHandle ImageHandle of the unloaded driver
121 @return Status of the ProcessModuleUnloadList.
126 _DriverUnloadHandler (
127 EFI_HANDLE ImageHandle
133 // Call the unload handlers for all the modules
135 Status
= ProcessModuleUnloadList (ImageHandle
);
138 // If the driver specific unload handler does not return an error, then call all of the
139 // library destructors. If the unload handler returned an error, then the driver can not be
140 // unloaded, and the library destructors should not be called
142 if (!EFI_ERROR (Status
)) {
143 ProcessLibraryDestructorList (ImageHandle
, gST
);
147 // Return the status from the driver specific unload handler
153 Enrty point to DXE SMM Driver.
155 @param ImageHandle ImageHandle of the loaded driver.
156 @param SystemTable Pointer to the EFI System Table.
158 @retval EFI_SUCCESS One or more of the drivers returned a success code.
159 @retval !EFI_SUCESS The return status from the last driver entry point in the list.
165 IN EFI_HANDLE ImageHandle
,
166 IN EFI_SYSTEM_TABLE
*SystemTable
170 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
171 EFI_SMM_BASE_PROTOCOL
*SmmBase
;
173 EFI_DEVICE_PATH_PROTOCOL
*CompleteFilePath
;
174 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
178 // Cache a pointer to the Boot Services Table
180 mBS
= SystemTable
->BootServices
;
183 // Retrieve the Loaded Image Protocol
185 Status
= mBS
->HandleProtocol (
187 &gEfiLoadedImageProtocolGuid
,
190 ASSERT_EFI_ERROR (Status
);
193 // Retrieve SMM Base Protocol
195 Status
= mBS
->LocateProtocol (
196 &gEfiSmmBaseProtocolGuid
,
200 ASSERT_EFI_ERROR (Status
);
203 // Check to see if we are already in SMM
205 SmmBase
->InSmm (SmmBase
, &InSmm
);
212 // Retrieve the Device Path Protocol from the DeviceHandle tha this driver was loaded from
214 Status
= mBS
->HandleProtocol (
215 LoadedImage
->DeviceHandle
,
216 &gEfiDevicePathProtocolGuid
,
217 (VOID
*)&ImageDevicePath
219 ASSERT_EFI_ERROR (Status
);
222 // Build the full device path to the currently execuing image
224 CompleteFilePath
= SmmAppendDevicePath (ImageDevicePath
, LoadedImage
->FilePath
);
227 // Load the image in memory to SMRAM; it will automatically generate the
230 Status
= SmmBase
->Register (SmmBase
, CompleteFilePath
, NULL
, 0, &Handle
, FALSE
);
231 ASSERT_EFI_ERROR (Status
);
236 // Call constructor for all libraries
238 ProcessLibraryConstructorList (ImageHandle
, SystemTable
);
241 // Optionally install the unload handler
243 if (_gDriverUnloadImageCount
> 0) {
244 Status
= mBS
->HandleProtocol (
246 &gEfiLoadedImageProtocolGuid
,
247 (VOID
**)&LoadedImage
249 ASSERT_EFI_ERROR (Status
);
250 LoadedImage
->Unload
= _DriverUnloadHandler
;
254 // Call the list of driver entry points
256 Status
= ProcessModuleEntryPointList (ImageHandle
, SystemTable
);
257 if (EFI_ERROR (Status
)) {
258 ProcessLibraryDestructorList (ImageHandle
, SystemTable
);
265 Enrty point wrapper of DXE SMM Driver.
267 @param ImageHandle ImageHandle of the loaded driver.
268 @param SystemTable Pointer to the EFI System Table.
270 @retval EFI_SUCCESS One or more of the drivers returned a success code.
271 @retval !EFI_SUCESS The return status from the last driver entry point in the list.
277 IN EFI_HANDLE ImageHandle
,
278 IN EFI_SYSTEM_TABLE
*SystemTable
281 return _ModuleEntryPoint (ImageHandle
, SystemTable
);