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 // Include common header file for this module.
18 #include "CommonHeader.h"
20 #include <FrameworkDxe.h>
21 #include <Library/UefiBootServicesTableLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/DxeSmmDriverEntryPoint.h>
25 #include <Protocol/LoadedImage.h>
26 #include <Protocol/SmmBase.h>
28 EFI_BOOT_SERVICES
*mBS
;
31 This function returns the size, in bytes,
32 of the device path data structure specified by DevicePath.
33 If DevicePath is NULL, then 0 is returned.
35 @param DevicePath A pointer to a device path data structure.
37 @return The size of a device path in bytes.
43 SmmGetDevicePathSize (
44 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
47 CONST EFI_DEVICE_PATH_PROTOCOL
*Start
;
49 if (DevicePath
== NULL
) {
54 // Search for the end of the device path structure
57 while (!EfiIsDevicePathEnd (DevicePath
)) {
58 DevicePath
= EfiNextDevicePathNode (DevicePath
);
62 // Compute the size and add back in the size of the end device path structure
64 return ((UINTN
) DevicePath
- (UINTN
) Start
) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
68 This function appends the device path SecondDevicePath
69 to every device path instance in FirstDevicePath.
71 @param FirstDevicePath A pointer to a device path data structure.
73 @param SecondDevicePath A pointer to a device path data structure.
75 @return A pointer to the new device path is returned.
76 NULL is returned if space for the new device path could not be allocated from pool.
77 It is up to the caller to free the memory used by FirstDevicePath and SecondDevicePath
78 if they are no longer needed.
81 EFI_DEVICE_PATH_PROTOCOL
*
84 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
,
85 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath
92 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
93 EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
;
95 ASSERT (FirstDevicePath
!= NULL
&& SecondDevicePath
!= NULL
);
98 // Allocate space for the combined device path. It only has one end node of
99 // length EFI_DEVICE_PATH_PROTOCOL
101 Size1
= SmmGetDevicePathSize (FirstDevicePath
);
102 Size2
= SmmGetDevicePathSize (SecondDevicePath
);
103 Size
= Size1
+ Size2
- sizeof (EFI_DEVICE_PATH_PROTOCOL
);
105 Status
= mBS
->AllocatePool (EfiBootServicesData
, Size
, (VOID
**) &NewDevicePath
);
107 if (EFI_SUCCESS
== Status
) {
108 mBS
->CopyMem ((VOID
*) NewDevicePath
, (VOID
*) FirstDevicePath
, Size1
);
110 // Over write Src1 EndNode and do the copy
112 DevicePath2
= (EFI_DEVICE_PATH_PROTOCOL
*) ((CHAR8
*) NewDevicePath
+ (Size1
- sizeof (EFI_DEVICE_PATH_PROTOCOL
)));
113 mBS
->CopyMem ((VOID
*) DevicePath2
, (VOID
*) SecondDevicePath
, Size2
);
116 return NewDevicePath
;
120 Unload function that is registered in the LoadImage protocol. It un-installs
121 protocols produced and deallocates pool used by the driver. Called by the core
122 when unloading the driver.
124 @param ImageHandle ImageHandle of the unloaded driver
126 @return Status of the ProcessModuleUnloadList.
131 _DriverUnloadHandler (
132 EFI_HANDLE ImageHandle
138 // Call the unload handlers for all the modules
140 Status
= ProcessModuleUnloadList (ImageHandle
);
143 // If the driver specific unload handler does not return an error, then call all of the
144 // library destructors. If the unload handler returned an error, then the driver can not be
145 // unloaded, and the library destructors should not be called
147 if (!EFI_ERROR (Status
)) {
148 ProcessLibraryDestructorList (ImageHandle
, gST
);
152 // Return the status from the driver specific unload handler
158 Enrty point to DXE SMM Driver.
160 @param ImageHandle ImageHandle of the loaded driver.
161 @param SystemTable Pointer to the EFI System Table.
163 @retval EFI_SUCCESS One or more of the drivers returned a success code.
164 @retval !EFI_SUCESS The return status from the last driver entry point in the list.
170 IN EFI_HANDLE ImageHandle
,
171 IN EFI_SYSTEM_TABLE
*SystemTable
175 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
176 EFI_SMM_BASE_PROTOCOL
*SmmBase
;
178 EFI_DEVICE_PATH_PROTOCOL
*CompleteFilePath
;
179 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
183 // Cache a pointer to the Boot Services Table
185 mBS
= SystemTable
->BootServices
;
188 // Retrieve the Loaded Image Protocol
190 Status
= mBS
->HandleProtocol (
192 &gEfiLoadedImageProtocolGuid
,
195 ASSERT_EFI_ERROR (Status
);
198 // Retrieve SMM Base Protocol
200 Status
= mBS
->LocateProtocol (
201 &gEfiSmmBaseProtocolGuid
,
205 ASSERT_EFI_ERROR (Status
);
208 // Check to see if we are already in SMM
210 SmmBase
->InSmm (SmmBase
, &InSmm
);
217 // Retrieve the Device Path Protocol from the DeviceHandle tha this driver was loaded from
219 Status
= mBS
->HandleProtocol (
220 LoadedImage
->DeviceHandle
,
221 &gEfiDevicePathProtocolGuid
,
222 (VOID
*)&ImageDevicePath
224 ASSERT_EFI_ERROR (Status
);
227 // Build the full device path to the currently execuing image
229 CompleteFilePath
= SmmAppendDevicePath (ImageDevicePath
, LoadedImage
->FilePath
);
232 // Load the image in memory to SMRAM; it will automatically generate the
235 Status
= SmmBase
->Register (SmmBase
, CompleteFilePath
, NULL
, 0, &Handle
, FALSE
);
236 ASSERT_EFI_ERROR (Status
);
241 // Call constructor for all libraries
243 ProcessLibraryConstructorList (ImageHandle
, SystemTable
);
246 // Optionally install the unload handler
248 if (_gDriverUnloadImageCount
> 0) {
249 Status
= mBS
->HandleProtocol (
251 &gEfiLoadedImageProtocolGuid
,
252 (VOID
**)&LoadedImage
254 ASSERT_EFI_ERROR (Status
);
255 LoadedImage
->Unload
= _DriverUnloadHandler
;
259 // Call the list of driver entry points
261 Status
= ProcessModuleEntryPointList (ImageHandle
, SystemTable
);
262 if (EFI_ERROR (Status
)) {
263 ProcessLibraryDestructorList (ImageHandle
, SystemTable
);
270 Enrty point wrapper of DXE SMM Driver.
272 @param ImageHandle ImageHandle of the loaded driver.
273 @param SystemTable Pointer to the EFI System Table.
275 @retval EFI_SUCCESS One or more of the drivers returned a success code.
276 @retval !EFI_SUCESS The return status from the last driver entry point in the list.
282 IN EFI_HANDLE ImageHandle
,
283 IN EFI_SYSTEM_TABLE
*SystemTable
286 return _ModuleEntryPoint (ImageHandle
, SystemTable
);