]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/DxeServicesLib/DxeServicesLib.c
Synchronize function comment in
[mirror_edk2.git] / MdePkg / Library / DxeServicesLib / DxeServicesLib.c
1 /** @file
2 MDE DXE Services Library provides functions that simplify the development of DXE Drivers.
3 These functions help access data from sections of FFS files.
4
5 Copyright (c) 2007 - 2008, Intel Corporation<BR>
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include <PiDxe.h>
17 #include <Library/DebugLib.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/DxeServicesLib.h>
21 #include <Protocol/FirmwareVolume2.h>
22 #include <Protocol/LoadedImage.h>
23
24
25 /**
26 Identify the device handle from which the Image is loaded from. As this device handle is passed to
27 GetSectionFromFv as the identifier for a Firmware Volume, an EFI_FIRMWARE_VOLUME2_PROTOCOL
28 protocol instance should be located succesfully by calling gBS->HandleProtocol ().
29
30 This function locates the EFI_LOADED_IMAGE_PROTOCOL instance installed
31 on ImageHandle. It then returns EFI_LOADED_IMAGE_PROTOCOL.DeviceHandle.
32
33 If ImageHandle is NULL, then ASSERT ();
34 If failed to locate a EFI_LOADED_IMAGE_PROTOCOL on ImageHandle, then ASSERT ();
35
36 @param ImageHandle The firmware allocated handle for UEFI image.
37
38 @retval EFI_HANDLE The device handle from which the Image is loaded from.
39
40 **/
41 EFI_HANDLE
42 InternalImageHandleToFvHandle (
43 EFI_HANDLE ImageHandle
44 )
45 {
46 EFI_STATUS Status;
47 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
48
49 ASSERT (ImageHandle != NULL);
50
51 Status = gBS->HandleProtocol (
52 (EFI_HANDLE *) ImageHandle,
53 &gEfiLoadedImageProtocolGuid,
54 (VOID **) &LoadedImage
55 );
56
57 ASSERT_EFI_ERROR (Status);
58
59 return LoadedImage->DeviceHandle;
60
61 }
62
63 /**
64 Allocate and fill a buffer from a Firmware Section identified by a Firmware File GUID name, a Firmware
65 Section type and instance number from the specified Firmware Volume.
66
67 This functions first locate the EFI_FIRMWARE_VOLUME2_PROTOCOL protocol instance on FvHandle in order to
68 carry out the Firmware Volume read operation. The function then reads the Firmware Section found sepcifed
69 by NameGuid, SectionType and SectionInstance.
70
71 The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection ()
72 found in PI Specification.
73
74 If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section
75 is not found, EFI_SECTION_PE32 will be used to try the search again. If no EFI_SECTION_PE32 section is found, EFI_NOT_FOUND
76 is returned.
77
78 The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated
79 by this function. This function can be only called at TPL_NOTIFY and below.
80
81 If FvHandle is NULL, then ASSERT ();
82 If NameGuid is NULL, then ASSERT();
83 If Buffer is NULL, then ASSERT();
84 If Size is NULL, then ASSERT().
85
86 @param FvHandle The device handle that contains a instance of EFI_FIRMWARE_VOLUME2_PROTOCOL instance.
87 @param NameGuid The GUID name of a Firmware File.
88 @param SectionType The Firmware Section type.
89 @param SectionInstance The instance number of Firmware Section to read from starting from 0.
90 @param Buffer On output, Buffer contains the the data read from the section in the Firmware File found.
91 @param Size On output, the size of Buffer.
92
93 @retval EFI_SUCCESS The image is found and data and size is returned.
94 @retval EFI_UNSUPPORTED FvHandle does not support EFI_FIRMWARE_VOLUME2_PROTOCOL.
95 @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.
96 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.
97 @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.
98 @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.
99
100 **/
101 EFI_STATUS
102 InternalGetSectionFromFv (
103 IN EFI_HANDLE FvHandle,
104 IN CONST EFI_GUID *NameGuid,
105 IN EFI_SECTION_TYPE SectionType,
106 IN UINTN SectionInstance,
107 OUT VOID **Buffer,
108 OUT UINTN *Size
109 )
110 {
111 EFI_STATUS Status;
112 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
113 UINT32 AuthenticationStatus;
114
115 ASSERT (NameGuid != NULL);
116 ASSERT (Buffer != NULL);
117 ASSERT (Size != NULL);
118
119 ASSERT (FvHandle != NULL);
120
121 Status = gBS->HandleProtocol (
122 FvHandle,
123 &gEfiFirmwareVolume2ProtocolGuid,
124 (VOID **) &Fv
125 );
126 if (EFI_ERROR (Status)) {
127 return Status;
128 }
129
130 //
131 // Read desired section content in NameGuid file
132 //
133 *Buffer = NULL;
134 *Size = 0;
135 Status = Fv->ReadSection (
136 Fv,
137 NameGuid,
138 SectionType,
139 SectionInstance,
140 Buffer,
141 Size,
142 &AuthenticationStatus
143 );
144
145 if (EFI_ERROR (Status) && (SectionType == EFI_SECTION_TE)) {
146 //
147 // Try reading PE32 section, if the required section is TE type
148 //
149 *Buffer = NULL;
150 *Size = 0;
151 Status = Fv->ReadSection (
152 Fv,
153 NameGuid,
154 EFI_SECTION_PE32,
155 SectionInstance,
156 Buffer,
157 Size,
158 &AuthenticationStatus
159 );
160 }
161
162 return Status;
163 }
164
165
166
167 /**
168 Searches all the availables firmware volumes and returns the first matching FFS section.
169
170 This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid.
171 The order that the firmware volumes is searched is not deterministic. For each FFS file found a search
172 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances
173 of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
174 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
175 It is the caller's responsibility to use FreePool() to free the allocated buffer.
176 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections
177 are retrieved from an FFS file based on SectionType and SectionInstance.
178
179 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
180 the search will be retried with a section type of EFI_SECTION_PE32.
181 This function must be called with a TPL <= TPL_NOTIFY.
182
183 If NameGuid is NULL, then ASSERT().
184 If Buffer is NULL, then ASSERT().
185 If Size is NULL, then ASSERT().
186
187
188 @param NameGuid A pointer to to the FFS filename GUID to search for within
189 any of the firmware volumes in the platform.
190 @param SectionType Indicates the FFS section type to search for within the FFS file specified by NameGuid.
191 @param SectionInstance Indicates which section instance within the FFS file specified by NameGuid to retrieve.
192 @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found.
193 Is it the caller's responsibility to free this buffer using FreePool().
194 @param Size On output, a pointer to the size, in bytes, of Buffer.
195
196 @retval EFI_SUCCESS The specified FFS section was returned.
197 @retval EFI_NOT_FOUND The specified FFS section could not be found.
198 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.
199 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.
200 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that
201 contains the matching FFS section does not allow reads.
202 **/
203 EFI_STATUS
204 EFIAPI
205 GetSectionFromAnyFv (
206 IN CONST EFI_GUID *NameGuid,
207 IN EFI_SECTION_TYPE SectionType,
208 IN UINTN SectionInstance,
209 OUT VOID **Buffer,
210 OUT UINTN *Size
211 )
212 {
213 EFI_STATUS Status;
214 EFI_HANDLE *HandleBuffer;
215 UINTN HandleCount;
216 UINTN Index;
217 EFI_HANDLE FvHandle;
218
219 //
220 // Search the FV that contain the caller's FFS first.
221 // FV builder can choose to build FFS into the this FV
222 // so that this implementation of GetSectionFromAnyFv
223 // will locate the FFS faster.
224 //
225 FvHandle = InternalImageHandleToFvHandle (gImageHandle);
226 Status = InternalGetSectionFromFv (
227 FvHandle,
228 NameGuid,
229 SectionType,
230 SectionInstance,
231 Buffer,
232 Size
233 );
234 if (!EFI_ERROR (Status)) {
235 return EFI_SUCCESS;
236 }
237
238 HandleBuffer = NULL;
239 Status = gBS->LocateHandleBuffer (
240 ByProtocol,
241 &gEfiFirmwareVolume2ProtocolGuid,
242 NULL,
243 &HandleCount,
244 &HandleBuffer
245 );
246 if (EFI_ERROR (Status)) {
247 goto Done;
248 }
249
250 for (Index = 0; Index < HandleCount; Index++) {
251 //
252 // Skip the FV that contain the caller's FFS
253 //
254 if (HandleBuffer[Index] != FvHandle) {
255 Status = InternalGetSectionFromFv (
256 HandleBuffer[Index],
257 NameGuid,
258 SectionType,
259 SectionInstance,
260 Buffer,
261 Size
262 );
263
264 if (!EFI_ERROR (Status)) {
265 goto Done;
266 }
267 }
268
269 }
270
271 if (Index == HandleCount) {
272 Status = EFI_NOT_FOUND;
273 }
274
275 Done:
276
277 if (HandleBuffer != NULL) {
278 FreePool(HandleBuffer);
279 }
280 return Status;
281
282 }
283
284 /**
285 Searches the firmware volume that the currently executing module was loaded from and returns the first matching FFS section.
286
287 This function searches the firmware volume that the currently executing module was loaded
288 from for an FFS file with an FFS filename specified by NameGuid. If the FFS file is found a search
289 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance
290 instances of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
291 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
292 It is the caller's responsibility to use FreePool() to free the allocated buffer.
293 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections are retrieved from
294 an FFS file based on SectionType and SectionInstance.
295
296 If the currently executing module was not loaded from a firmware volume, then EFI_NOT_FOUND is returned.
297 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
298 the search will be retried with a section type of EFI_SECTION_PE32.
299
300 This function must be called with a TPL <= TPL_NOTIFY.
301 If NameGuid is NULL, then ASSERT().
302 If Buffer is NULL, then ASSERT().
303 If Size is NULL, then ASSERT().
304
305 @param NameGuid A pointer to to the FFS filename GUID to search for within
306 the firmware volumes that the currently executing module was loaded from.
307 @param SectionType Indicates the FFS section type to search for within the FFS file specified by NameGuid.
308 @param SectionInstance Indicates which section instance within the FFS file specified by NameGuid to retrieve.
309 @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found.
310 Is it the caller's responsibility to free this buffer using FreePool().
311 @param Size On output, a pointer to the size, in bytes, of Buffer.
312
313
314 @retval EFI_SUCCESS The specified FFS section was returned.
315 @retval EFI_NOT_FOUND The specified FFS section could not be found.
316 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.
317 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.
318 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that
319 contains the matching FFS section does not allow reads.
320 **/
321 EFI_STATUS
322 EFIAPI
323 GetSectionFromFv (
324 IN CONST EFI_GUID *NameGuid,
325 IN EFI_SECTION_TYPE SectionType,
326 IN UINTN SectionInstance,
327 OUT VOID **Buffer,
328 OUT UINTN *Size
329 )
330 {
331 return InternalGetSectionFromFv (
332 InternalImageHandleToFvHandle(gImageHandle),
333 NameGuid,
334 SectionType,
335 SectionInstance,
336 Buffer,
337 Size
338 );
339 }
340
341
342 /**
343 Searches the FFS file the the currently executing module was loaded from and returns the first matching FFS section.
344
345 This function searches the FFS file that the currently executing module was loaded from for a FFS sections of type SectionType.
346 If the FFS file contains at least SectionInstance instances of the FFS section specified by SectionType,
347 then the SectionInstance instance is returned in Buffer. Buffer is allocated using AllocatePool(),
348 and the size of the allocated buffer is returned in Size. It is the caller's responsibility
349 to use FreePool() to free the allocated buffer. See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for
350 details on how sections are retrieved from an FFS file based on SectionType and SectionInstance.
351
352 If the currently executing module was not loaded from an FFS file, then EFI_NOT_FOUND is returned.
353 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
354 the search will be retried with a section type of EFI_SECTION_PE32.
355 This function must be called with a TPL <= TPL_NOTIFY.
356
357 If Buffer is NULL, then ASSERT().
358 If Size is NULL, then ASSERT().
359
360
361 @param SectionType Indicates the FFS section type to search for within the FFS file
362 that the currently executing module was loaded from.
363 @param SectionInstance Indicates which section instance to retrieve within the FFS file
364 that the currently executing module was loaded from.
365 @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found.
366 Is it the caller's responsibility to free this buffer using FreePool().
367 @param Size On output, a pointer to the size, in bytes, of Buffer.
368
369 @retval EFI_SUCCESS The specified FFS section was returned.
370 @retval EFI_NOT_FOUND The specified FFS section could not be found.
371 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.
372 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.
373 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that
374 contains the matching FFS section does not allow reads.
375
376 **/
377 EFI_STATUS
378 EFIAPI
379 GetSectionFromFfs (
380 IN EFI_SECTION_TYPE SectionType,
381 IN UINTN SectionInstance,
382 OUT VOID **Buffer,
383 OUT UINTN *Size
384 )
385 {
386 return InternalGetSectionFromFv(
387 InternalImageHandleToFvHandle(gImageHandle),
388 &gEfiCallerIdGuid,
389 SectionType,
390 SectionInstance,
391 Buffer,
392 Size
393 );
394 }
395