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