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