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
137 // Call the unload handlers for all the modules.
139 // Note: All libraries were constructed in SMM space,
140 // therefore we can not destruct them in Unload
143 return ProcessModuleUnloadList (ImageHandle
);
147 Enrty point to DXE SMM Driver.
149 @param ImageHandle ImageHandle of the loaded driver.
150 @param SystemTable Pointer to the EFI System Table.
152 @retval EFI_SUCCESS One or more of the drivers returned a success code.
153 @retval !EFI_SUCESS The return status from the last driver entry point in the list.
159 IN EFI_HANDLE ImageHandle
,
160 IN EFI_SYSTEM_TABLE
*SystemTable
164 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
165 EFI_SMM_BASE_PROTOCOL
*SmmBase
;
167 EFI_DEVICE_PATH_PROTOCOL
*CompleteFilePath
;
168 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
172 // Cache a pointer to the Boot Services Table
174 mBS
= SystemTable
->BootServices
;
177 // Retrieve the Loaded Image Protocol
179 Status
= mBS
->HandleProtocol (
181 &gEfiLoadedImageProtocolGuid
,
184 ASSERT_EFI_ERROR (Status
);
187 // Retrieve SMM Base Protocol
189 Status
= mBS
->LocateProtocol (
190 &gEfiSmmBaseProtocolGuid
,
194 ASSERT_EFI_ERROR (Status
);
197 // Check to see if we are already in SMM
199 SmmBase
->InSmm (SmmBase
, &InSmm
);
206 // Retrieve the Device Path Protocol from the DeviceHandle tha this driver was loaded from
208 Status
= mBS
->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
, NULL
, 0, &Handle
, FALSE
);
225 ASSERT_EFI_ERROR (Status
);
230 // Call constructor for all libraries
232 ProcessLibraryConstructorList (ImageHandle
, SystemTable
);
235 // Optionally install the unload handler
237 if (_gDriverUnloadImageCount
> 0) {
238 Status
= mBS
->HandleProtocol (
240 &gEfiLoadedImageProtocolGuid
,
241 (VOID
**)&LoadedImage
243 ASSERT_EFI_ERROR (Status
);
244 LoadedImage
->Unload
= _DriverUnloadHandler
;
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
);