Program SD Cards into 4-bit mode (support for this is required in the spec). This...
[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. All rights reserved.<BR>
7 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/UefiDriverEntryPoint.h>
25 #include <Library/UefiBootServicesTableLib.h>
26 #include <Library/DebugLib.h>
27 #include <Library/DevicePathLib.h>
28
29 /**
30 This function returns the size, in bytes,
31 of the device path data structure specified by DevicePath.
32 If DevicePath is NULL, then 0 is returned.
33
34 @param DevicePath A pointer to a device path data structure.
35
36 @return The size of a device path in bytes.
37
38 **/
39 UINTN
40 EFIAPI
41 SmmGetDevicePathSize (
42 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
43 )
44 {
45 CONST EFI_DEVICE_PATH_PROTOCOL *Start;
46
47 if (DevicePath == NULL) {
48 return 0;
49 }
50
51 //
52 // Search for the end of the device path structure
53 //
54 Start = DevicePath;
55 while (!IsDevicePathEnd (DevicePath)) {
56 DevicePath = NextDevicePathNode (DevicePath);
57 }
58
59 //
60 // Compute the size and add back in the size of the end device path structure
61 //
62 return ((UINTN) DevicePath - (UINTN) Start) + sizeof (EFI_DEVICE_PATH_PROTOCOL);
63 }
64
65 /**
66 This function appends the device path SecondDevicePath
67 to every device path instance in FirstDevicePath.
68
69 @param FirstDevicePath A pointer to a device path data structure.
70
71 @param SecondDevicePath A pointer to a device path data structure.
72
73 @return A pointer to the new device path is returned.
74 NULL is returned if space for the new device path could not be allocated from pool.
75 It is up to the caller to free the memory used by FirstDevicePath and SecondDevicePath
76 if they are no longer needed.
77
78 **/
79 EFI_DEVICE_PATH_PROTOCOL *
80 EFIAPI
81 SmmAppendDevicePath (
82 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath,
83 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath
84 )
85 {
86 EFI_STATUS Status;
87 UINTN Size;
88 UINTN Size1;
89 UINTN Size2;
90 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
91 EFI_DEVICE_PATH_PROTOCOL *DevicePath2;
92
93 ASSERT (FirstDevicePath != NULL && SecondDevicePath != NULL);
94
95 //
96 // Allocate space for the combined device path. It only has one end node of
97 // length EFI_DEVICE_PATH_PROTOCOL
98 //
99 Size1 = SmmGetDevicePathSize (FirstDevicePath);
100 Size2 = SmmGetDevicePathSize (SecondDevicePath);
101 Size = Size1 + Size2 - sizeof (EFI_DEVICE_PATH_PROTOCOL);
102
103 Status = gBS->AllocatePool (EfiBootServicesData, Size, (VOID **) &NewDevicePath);
104
105 if (EFI_SUCCESS == Status) {
106 //
107 // CopyMem in gBS is used as this service should always be ready. We didn't choose
108 // to use a BaseMemoryLib function as such library instance may have constructor.
109 //
110 gBS->CopyMem ((VOID *) NewDevicePath, (VOID *) FirstDevicePath, Size1);
111 //
112 // Over write Src1 EndNode and do the copy
113 //
114 DevicePath2 = (EFI_DEVICE_PATH_PROTOCOL *) ((CHAR8 *) NewDevicePath + (Size1 - sizeof (EFI_DEVICE_PATH_PROTOCOL)));
115 gBS->CopyMem ((VOID *) DevicePath2, (VOID *) SecondDevicePath, Size2);
116 }
117
118 return NewDevicePath;
119 }
120
121 /**
122 Unload function that is registered in the LoadImage protocol. It un-installs
123 protocols produced and deallocates pool used by the driver. Called by the core
124 when unloading the driver.
125
126 @param ImageHandle ImageHandle of the unloaded driver
127
128 @return Status of the ProcessModuleUnloadList.
129
130 **/
131 EFI_STATUS
132 EFIAPI
133 _DriverUnloadHandler (
134 EFI_HANDLE ImageHandle
135 )
136 {
137 //
138 // Call the unload handlers for all the modules.
139 //
140 // Note: All libraries were constructed in SMM space,
141 // therefore we can not destruct them in Unload
142 // handler.
143 //
144 return ProcessModuleUnloadList (ImageHandle);
145 }
146
147 /**
148 Enrty point to DXE SMM Driver.
149
150 @param ImageHandle ImageHandle of the loaded driver.
151 @param SystemTable Pointer to the EFI System Table.
152
153 @retval EFI_SUCCESS One or more of the drivers returned a success code.
154 @retval !EFI_SUCESS The return status from the last driver entry point in the list.
155
156 **/
157 EFI_STATUS
158 EFIAPI
159 _ModuleEntryPoint (
160 IN EFI_HANDLE ImageHandle,
161 IN EFI_SYSTEM_TABLE *SystemTable
162 )
163 {
164 EFI_STATUS Status;
165 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
166 EFI_SMM_BASE_PROTOCOL *SmmBase;
167 BOOLEAN InSmm;
168 EFI_DEVICE_PATH_PROTOCOL *CompleteFilePath;
169 EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;
170 EFI_HANDLE Handle;
171
172 //
173 // Cache a pointer to the Boot Services Table
174 //
175 gBS = SystemTable->BootServices;
176
177 //
178 // Retrieve SMM Base Protocol
179 //
180 Status = gBS->LocateProtocol (
181 &gEfiSmmBaseProtocolGuid,
182 NULL,
183 (VOID **) &SmmBase
184 );
185 ASSERT_EFI_ERROR (Status);
186
187 //
188 // Check to see if we are already in SMM
189 //
190 SmmBase->InSmm (SmmBase, &InSmm);
191
192 //
193 //
194 //
195 if (!InSmm) {
196 //
197 // Retrieve the Loaded Image Protocol
198 //
199 Status = gBS->HandleProtocol (
200 ImageHandle,
201 &gEfiLoadedImageProtocolGuid,
202 (VOID*)&LoadedImage
203 );
204 ASSERT_EFI_ERROR (Status);
205 //
206 // Retrieve the Device Path Protocol from the DeviceHandle from which this driver was loaded
207 //
208 Status = gBS->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, LoadedImage->ImageBase, 0, &Handle, FALSE);
225 ASSERT_EFI_ERROR (Status);
226 //
227 // Optionally install the unload handler
228 //
229 if (_gDriverUnloadImageCount > 0) {
230 Status = gBS->HandleProtocol (
231 ImageHandle,
232 &gEfiLoadedImageProtocolGuid,
233 (VOID **)&LoadedImage
234 );
235 ASSERT_EFI_ERROR (Status);
236 LoadedImage->Unload = _DriverUnloadHandler;
237 }
238
239 return Status;
240 }
241
242 //
243 // Call constructor for all libraries
244 //
245 ProcessLibraryConstructorList (ImageHandle, SystemTable);
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 }