]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/DxeSmmDriverEntryPoint/DriverEntryPoint.c
Remove EdkPeCoffLoaderLibX64 library from EdkModulePkg
[mirror_edk2.git] / MdePkg / Library / DxeSmmDriverEntryPoint / DriverEntryPoint.c
CommitLineData
878ddf1f 1/** @file\r
2 Entry point to a EFI/DXE driver.\r
3\r
4Copyright (c) 2006, Intel Corporation<BR>\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15EFI_BOOT_SERVICES *mBS;\r
16\r
17/**\r
18 This function returns the size, in bytes, \r
19 of the device path data structure specified by DevicePath.\r
20 If DevicePath is NULL, then 0 is returned.\r
21\r
22 @param DevicePath A pointer to a device path data structure.\r
23\r
24 @return The size of a device path in bytes.\r
25\r
26**/\r
27STATIC\r
28UINTN\r
29EFIAPI\r
30SmmGetDevicePathSize (\r
31 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
32 )\r
33{\r
34 CONST EFI_DEVICE_PATH_PROTOCOL *Start;\r
35\r
36 if (DevicePath == NULL) {\r
37 return 0;\r
38 }\r
39\r
40 //\r
41 // Search for the end of the device path structure\r
42 //\r
43 Start = DevicePath;\r
44 while (!EfiIsDevicePathEnd (DevicePath)) {\r
45 DevicePath = EfiNextDevicePathNode (DevicePath);\r
46 }\r
47\r
48 //\r
49 // Compute the size and add back in the size of the end device path structure\r
50 //\r
51 return ((UINTN) DevicePath - (UINTN) Start) + sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
52}\r
53\r
54/**\r
55 This function appends the device path SecondDevicePath\r
56 to every device path instance in FirstDevicePath. \r
57\r
58 @param FirstDevicePath A pointer to a device path data structure.\r
59 \r
60 @param SecondDevicePath A pointer to a device path data structure.\r
61\r
62 @return A pointer to the new device path is returned.\r
63 NULL is returned if space for the new device path could not be allocated from pool.\r
64 It is up to the caller to free the memory used by FirstDevicePath and SecondDevicePath\r
65 if they are no longer needed.\r
66\r
67**/\r
68EFI_DEVICE_PATH_PROTOCOL *\r
69EFIAPI\r
70SmmAppendDevicePath (\r
71 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath,\r
72 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath\r
73 )\r
74{\r
75 EFI_STATUS Status;\r
76 UINTN Size;\r
77 UINTN Size1;\r
78 UINTN Size2;\r
79 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
80 EFI_DEVICE_PATH_PROTOCOL *DevicePath2;\r
81\r
82 ASSERT (FirstDevicePath != NULL && SecondDevicePath != NULL);\r
83\r
84 //\r
85 // Allocate space for the combined device path. It only has one end node of\r
86 // length EFI_DEVICE_PATH_PROTOCOL\r
87 //\r
88 Size1 = SmmGetDevicePathSize (FirstDevicePath);\r
89 Size2 = SmmGetDevicePathSize (SecondDevicePath);\r
90 Size = Size1 + Size2 - sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
91\r
92 Status = mBS->AllocatePool (EfiBootServicesData, Size, (VOID **) &NewDevicePath);\r
93\r
94 if (EFI_SUCCESS == Status) {\r
95 mBS->CopyMem ((VOID *) NewDevicePath, (VOID *) FirstDevicePath, Size1);\r
96 //\r
97 // Over write Src1 EndNode and do the copy\r
98 //\r
99 DevicePath2 = (EFI_DEVICE_PATH_PROTOCOL *) ((CHAR8 *) NewDevicePath + (Size1 - sizeof (EFI_DEVICE_PATH_PROTOCOL)));\r
100 mBS->CopyMem ((VOID *) DevicePath2, (VOID *) SecondDevicePath, Size2);\r
101 }\r
102\r
103 return NewDevicePath;\r
104}\r
105\r
106/**\r
107 Unload function that is registered in the LoadImage protocol. It un-installs\r
108 protocols produced and deallocates pool used by the driver. Called by the core\r
109 when unloading the driver.\r
110\r
111 @param ImageHandle ImageHandle of the unloaded driver\r
112\r
113 @return Status of the ProcessModuleUnloadList.\r
114\r
115**/\r
116EFI_STATUS\r
117EFIAPI\r
118_DriverUnloadHandler (\r
119 EFI_HANDLE ImageHandle\r
120 )\r
121{\r
122 EFI_STATUS Status;\r
123\r
124 //\r
125 // Call the unload handlers for all the modules\r
126 //\r
127 Status = ProcessModuleUnloadList (ImageHandle);\r
128\r
129 //\r
130 // If the driver specific unload handler does not return an error, then call all of the\r
131 // library destructors. If the unload handler returned an error, then the driver can not be\r
132 // unloaded, and the library destructors should not be called\r
133 //\r
134 if (!EFI_ERROR (Status)) {\r
135 ProcessLibraryDestructorList (ImageHandle, gST);\r
136 }\r
137\r
138 //\r
139 // Return the status from the driver specific unload handler\r
140 //\r
141 return Status;\r
142}\r
143\r
144/**\r
145 Enrty point to DXE SMM Driver.\r
146\r
147 @param ImageHandle ImageHandle of the loaded driver.\r
148 @param SystemTable Pointer to the EFI System Table.\r
149\r
150 @retval EFI_SUCCESS One or more of the drivers returned a success code.\r
151 @retval !EFI_SUCESS The return status from the last driver entry point in the list.\r
152\r
153**/\r
154EFI_STATUS\r
155EFIAPI\r
156_ModuleEntryPoint (\r
157 IN EFI_HANDLE ImageHandle,\r
158 IN EFI_SYSTEM_TABLE *SystemTable\r
159 )\r
160{\r
161 EFI_STATUS Status;\r
162 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
163 EFI_SMM_BASE_PROTOCOL *SmmBase;\r
164 BOOLEAN InSmm;\r
165 EFI_DEVICE_PATH_PROTOCOL *CompleteFilePath;\r
166 EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;\r
167 EFI_HANDLE Handle;\r
168\r
169 //\r
170 // Cache a pointer to the Boot Services Table \r
171 //\r
172 mBS = SystemTable->BootServices;\r
173\r
174 //\r
175 // Retrieve the Loaded Image Protocol\r
176 //\r
177 Status = mBS->HandleProtocol (\r
178 ImageHandle, \r
179 &gEfiLoadedImageProtocolGuid,\r
180 (VOID*)&LoadedImage\r
181 );\r
182 ASSERT_EFI_ERROR (Status);\r
183\r
184 //\r
185 // Retrieve SMM Base Protocol\r
186 //\r
187 Status = mBS->LocateProtocol (\r
188 &gEfiSmmBaseProtocolGuid, \r
189 NULL, \r
190 (VOID **) &SmmBase\r
191 );\r
192 ASSERT_EFI_ERROR (Status);\r
193\r
194 //\r
195 // Check to see if we are already in SMM\r
196 //\r
197 SmmBase->InSmm (SmmBase, &InSmm);\r
198\r
199 //\r
200 //\r
201 //\r
202 if (!InSmm) {\r
203 //\r
204 // Retrieve the Device Path Protocol from the DeviceHandle tha this driver was loaded from\r
205 //\r
206 Status = mBS->HandleProtocol (\r
207 LoadedImage->DeviceHandle, \r
208 &gEfiDevicePathProtocolGuid,\r
209 (VOID*)&ImageDevicePath\r
210 );\r
211 ASSERT_EFI_ERROR (Status);\r
212\r
213 //\r
214 // Build the full device path to the currently execuing image\r
215 //\r
216 CompleteFilePath = SmmAppendDevicePath (ImageDevicePath, LoadedImage->FilePath);\r
217\r
218 //\r
219 // Load the image in memory to SMRAM; it will automatically generate the\r
220 // SMI.\r
221 //\r
222 Status = SmmBase->Register (SmmBase, CompleteFilePath, NULL, 0, &Handle, FALSE);\r
223 ASSERT_EFI_ERROR (Status);\r
224 return Status;\r
225 }\r
226\r
227 //\r
228 // Call constructor for all libraries\r
229 //\r
230 ProcessLibraryConstructorList (ImageHandle, SystemTable);\r
231\r
232 //\r
233 // Optionally install the unload handler\r
234 //\r
235 if (_gDriverUnloadImageCount > 0) {\r
236 Status = mBS->HandleProtocol (\r
237 ImageHandle,\r
238 &gEfiLoadedImageProtocolGuid,\r
239 (VOID **)&LoadedImage\r
240 );\r
241 ASSERT_EFI_ERROR (Status);\r
242 LoadedImage->Unload = _DriverUnloadHandler;\r
243 }\r
244\r
245 //\r
246 // Call the list of driver entry points\r
247 //\r
248 Status = ProcessModuleEntryPointList (ImageHandle, SystemTable);\r
249 if (EFI_ERROR (Status)) {\r
250 ProcessLibraryDestructorList (ImageHandle, SystemTable);\r
251 }\r
252\r
253 return Status;\r
254}\r
255\r
256/**\r
257 Enrty point wrapper of DXE SMM Driver.\r
258\r
259 @param ImageHandle ImageHandle of the loaded driver.\r
260 @param SystemTable Pointer to the EFI System Table.\r
261\r
262 @retval EFI_SUCCESS One or more of the drivers returned a success code.\r
263 @retval !EFI_SUCESS The return status from the last driver entry point in the list.\r
264\r
265**/\r
266EFI_STATUS\r
267EFIAPI\r
268EfiMain (\r
269 IN EFI_HANDLE ImageHandle,\r
270 IN EFI_SYSTEM_TABLE *SystemTable\r
271 )\r
272{\r
273 return _ModuleEntryPoint (ImageHandle, SystemTable);\r
274}\r