]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkPkg/Library/DxeSmmDriverEntryPoint/DriverEntryPoint.c
Bug Fix in DxeSmmDriverEntryLib.
[mirror_edk2.git] / IntelFrameworkPkg / Library / DxeSmmDriverEntryPoint / DriverEntryPoint.c
1 /** @file
2 This file implement EfiMain() for library class DxeSmmDriverEntryPoint.
3 EfiMain() is common driver entry point for all SMM driver who uses DxeSmmDriverEntryPoint
4 library class.
5
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
11
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.
14
15 **/
16
17
18 #include <FrameworkSmm.h>
19
20 #include <Protocol/LoadedImage.h>
21 #include <Protocol/SmmBase.h>
22 #include <Protocol/DevicePath.h>
23
24 #include <Library/DxeSmmDriverEntryPoint.h>
25 #include <Library/UefiBootServicesTableLib.h>
26 #include <Library/DebugLib.h>
27
28
29 EFI_BOOT_SERVICES *mBS;
30
31 /**
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.
35
36 @param DevicePath A pointer to a device path data structure.
37
38 @return The size of a device path in bytes.
39
40 **/
41 STATIC
42 UINTN
43 EFIAPI
44 SmmGetDevicePathSize (
45 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
46 )
47 {
48 CONST EFI_DEVICE_PATH_PROTOCOL *Start;
49
50 if (DevicePath == NULL) {
51 return 0;
52 }
53
54 //
55 // Search for the end of the device path structure
56 //
57 Start = DevicePath;
58 while (!EfiIsDevicePathEnd (DevicePath)) {
59 DevicePath = EfiNextDevicePathNode (DevicePath);
60 }
61
62 //
63 // Compute the size and add back in the size of the end device path structure
64 //
65 return ((UINTN) DevicePath - (UINTN) Start) + sizeof (EFI_DEVICE_PATH_PROTOCOL);
66 }
67
68 /**
69 This function appends the device path SecondDevicePath
70 to every device path instance in FirstDevicePath.
71
72 @param FirstDevicePath A pointer to a device path data structure.
73
74 @param SecondDevicePath A pointer to a device path data structure.
75
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.
80
81 **/
82 EFI_DEVICE_PATH_PROTOCOL *
83 EFIAPI
84 SmmAppendDevicePath (
85 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath,
86 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath
87 )
88 {
89 EFI_STATUS Status;
90 UINTN Size;
91 UINTN Size1;
92 UINTN Size2;
93 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
94 EFI_DEVICE_PATH_PROTOCOL *DevicePath2;
95
96 ASSERT (FirstDevicePath != NULL && SecondDevicePath != NULL);
97
98 //
99 // Allocate space for the combined device path. It only has one end node of
100 // length EFI_DEVICE_PATH_PROTOCOL
101 //
102 Size1 = SmmGetDevicePathSize (FirstDevicePath);
103 Size2 = SmmGetDevicePathSize (SecondDevicePath);
104 Size = Size1 + Size2 - sizeof (EFI_DEVICE_PATH_PROTOCOL);
105
106 Status = mBS->AllocatePool (EfiBootServicesData, Size, (VOID **) &NewDevicePath);
107
108 if (EFI_SUCCESS == Status) {
109 mBS->CopyMem ((VOID *) NewDevicePath, (VOID *) FirstDevicePath, Size1);
110 //
111 // Over write Src1 EndNode and do the copy
112 //
113 DevicePath2 = (EFI_DEVICE_PATH_PROTOCOL *) ((CHAR8 *) NewDevicePath + (Size1 - sizeof (EFI_DEVICE_PATH_PROTOCOL)));
114 mBS->CopyMem ((VOID *) DevicePath2, (VOID *) SecondDevicePath, Size2);
115 }
116
117 return NewDevicePath;
118 }
119
120 /**
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.
124
125 @param ImageHandle ImageHandle of the unloaded driver
126
127 @return Status of the ProcessModuleUnloadList.
128
129 **/
130 EFI_STATUS
131 EFIAPI
132 _DriverUnloadHandler (
133 EFI_HANDLE ImageHandle
134 )
135 {
136 //
137 // Call the unload handlers for all the modules.
138 //
139 // Note: All libraries were constructed in SMM space,
140 // therefore we can not destruct them in Unload
141 // handler.
142 //
143 return ProcessModuleUnloadList (ImageHandle);
144 }
145
146 /**
147 Enrty point to DXE SMM Driver.
148
149 @param ImageHandle ImageHandle of the loaded driver.
150 @param SystemTable Pointer to the EFI System Table.
151
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.
154
155 **/
156 EFI_STATUS
157 EFIAPI
158 _ModuleEntryPoint (
159 IN EFI_HANDLE ImageHandle,
160 IN EFI_SYSTEM_TABLE *SystemTable
161 )
162 {
163 EFI_STATUS Status;
164 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
165 EFI_SMM_BASE_PROTOCOL *SmmBase;
166 BOOLEAN InSmm;
167 EFI_DEVICE_PATH_PROTOCOL *CompleteFilePath;
168 EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;
169 EFI_HANDLE Handle;
170
171 //
172 // Cache a pointer to the Boot Services Table
173 //
174 mBS = SystemTable->BootServices;
175
176 //
177 // Retrieve the Loaded Image Protocol
178 //
179 Status = mBS->HandleProtocol (
180 ImageHandle,
181 &gEfiLoadedImageProtocolGuid,
182 (VOID*)&LoadedImage
183 );
184 ASSERT_EFI_ERROR (Status);
185
186 //
187 // Retrieve SMM Base Protocol
188 //
189 Status = mBS->LocateProtocol (
190 &gEfiSmmBaseProtocolGuid,
191 NULL,
192 (VOID **) &SmmBase
193 );
194 ASSERT_EFI_ERROR (Status);
195
196 //
197 // Check to see if we are already in SMM
198 //
199 SmmBase->InSmm (SmmBase, &InSmm);
200
201 //
202 //
203 //
204 if (!InSmm) {
205 //
206 // Retrieve the Device Path Protocol from the DeviceHandle tha this driver was loaded from
207 //
208 Status = mBS->HandleProtocol (
209 LoadedImage->DeviceHandle,
210 &gEfiDevicePathProtocolGuid,
211 (VOID*)&ImageDevicePath
212 );
213 ASSERT_EFI_ERROR (Status);
214
215 //
216 // Build the full device path to the currently execuing image
217 //
218 CompleteFilePath = SmmAppendDevicePath (ImageDevicePath, LoadedImage->FilePath);
219
220 //
221 // Load the image in memory to SMRAM; it will automatically generate the
222 // SMI.
223 //
224 Status = SmmBase->Register (SmmBase, CompleteFilePath, NULL, 0, &Handle, FALSE);
225 ASSERT_EFI_ERROR (Status);
226 return Status;
227 }
228
229 //
230 // Call constructor for all libraries
231 //
232 ProcessLibraryConstructorList (ImageHandle, SystemTable);
233
234 //
235 // Optionally install the unload handler
236 //
237 if (_gDriverUnloadImageCount > 0) {
238 Status = mBS->HandleProtocol (
239 ImageHandle,
240 &gEfiLoadedImageProtocolGuid,
241 (VOID **)&LoadedImage
242 );
243 ASSERT_EFI_ERROR (Status);
244 LoadedImage->Unload = _DriverUnloadHandler;
245 }
246
247 //
248 // Call the list of driver entry points
249 //
250 Status = ProcessModuleEntryPointList (ImageHandle, SystemTable);
251 if (EFI_ERROR (Status)) {
252 ProcessLibraryDestructorList (ImageHandle, SystemTable);
253 }
254
255 return Status;
256 }
257
258 /**
259 Enrty point wrapper of DXE SMM Driver.
260
261 @param ImageHandle ImageHandle of the loaded driver.
262 @param SystemTable Pointer to the EFI System Table.
263
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.
266
267 **/
268 EFI_STATUS
269 EFIAPI
270 EfiMain (
271 IN EFI_HANDLE ImageHandle,
272 IN EFI_SYSTEM_TABLE *SystemTable
273 )
274 {
275 return _ModuleEntryPoint (ImageHandle, SystemTable);
276 }