]>
Commit | Line | Data |
---|---|---|
1c280088 | 1 | /** @file\r |
b75a165d | 2 | MDE DXE Services Library provides functions that simplify the development of DXE Drivers. \r |
84716614 | 3 | These functions help access data from sections of FFS files or from file path.\r |
1c280088 | 4 | \r |
19388d29 HT |
5 | Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>\r |
6 | This program and the accompanying materials\r | |
1c280088 | 7 | are licensed and made available under the terms and conditions of the BSD License\r |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
2fc59a00 | 9 | http://opensource.org/licenses/bsd-license.php.\r |
1c280088 | 10 | \r |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | #include <PiDxe.h>\r | |
17 | #include <Library/DebugLib.h>\r | |
18 | #include <Library/MemoryAllocationLib.h>\r | |
19 | #include <Library/UefiBootServicesTableLib.h>\r | |
84716614 LG |
20 | #include <Library/DevicePathLib.h>\r |
21 | #include <Library/UefiLib.h>\r | |
eb9dd4d0 | 22 | #include <Library/DxeServicesLib.h>\r |
1c280088 | 23 | #include <Protocol/FirmwareVolume2.h>\r |
24 | #include <Protocol/LoadedImage.h>\r | |
84716614 LG |
25 | #include <Protocol/LoadFile2.h>\r |
26 | #include <Protocol/LoadFile.h>\r | |
27 | #include <Protocol/SimpleFileSystem.h>\r | |
28 | #include <Guid/FileInfo.h>\r | |
1c280088 | 29 | \r |
30 | /**\r | |
166152e8 | 31 | Identify the device handle from which the Image is loaded from. As this device handle is passed to\r |
32 | GetSectionFromFv as the identifier for a Firmware Volume, an EFI_FIRMWARE_VOLUME2_PROTOCOL \r | |
33 | protocol instance should be located succesfully by calling gBS->HandleProtocol ().\r | |
1c280088 | 34 | \r |
166152e8 | 35 | This function locates the EFI_LOADED_IMAGE_PROTOCOL instance installed\r |
36 | on ImageHandle. It then returns EFI_LOADED_IMAGE_PROTOCOL.DeviceHandle.\r | |
37 | \r | |
38 | If ImageHandle is NULL, then ASSERT ();\r | |
39 | If failed to locate a EFI_LOADED_IMAGE_PROTOCOL on ImageHandle, then ASSERT ();\r | |
40 | \r | |
41 | @param ImageHandle The firmware allocated handle for UEFI image.\r | |
1c280088 | 42 | \r |
b75a165d | 43 | @retval EFI_HANDLE The device handle from which the Image is loaded from.\r |
1c280088 | 44 | \r |
45 | **/\r | |
166152e8 | 46 | EFI_HANDLE\r |
47 | InternalImageHandleToFvHandle (\r | |
48 | EFI_HANDLE ImageHandle\r | |
49 | )\r | |
50 | {\r | |
51 | EFI_STATUS Status;\r | |
52 | EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r | |
53 | \r | |
54 | ASSERT (ImageHandle != NULL);\r | |
55 | \r | |
56 | Status = gBS->HandleProtocol (\r | |
57 | (EFI_HANDLE *) ImageHandle,\r | |
58 | &gEfiLoadedImageProtocolGuid,\r | |
59 | (VOID **) &LoadedImage\r | |
60 | );\r | |
61 | \r | |
62 | ASSERT_EFI_ERROR (Status);\r | |
63 | \r | |
64 | return LoadedImage->DeviceHandle;\r | |
65 | \r | |
66 | }\r | |
67 | \r | |
68 | /**\r | |
b75a165d LG |
69 | Allocate and fill a buffer from a Firmware Section identified by a Firmware File GUID name, a Firmware \r |
70 | Section type and instance number from the specified Firmware Volume.\r | |
71 | \r | |
72 | This functions first locate the EFI_FIRMWARE_VOLUME2_PROTOCOL protocol instance on FvHandle in order to \r | |
73 | carry out the Firmware Volume read operation. The function then reads the Firmware Section found sepcifed \r | |
74 | by NameGuid, SectionType and SectionInstance. \r | |
75 | \r | |
76 | The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () \r | |
77 | found in PI Specification.\r | |
78 | \r | |
79 | If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r | |
80 | 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 \r | |
81 | is returned.\r | |
166152e8 | 82 | \r |
b75a165d LG |
83 | The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r |
84 | by this function. This function can be only called at TPL_NOTIFY and below.\r | |
166152e8 | 85 | \r |
b75a165d LG |
86 | If FvHandle is NULL, then ASSERT ();\r |
87 | If NameGuid is NULL, then ASSERT();\r | |
88 | If Buffer is NULL, then ASSERT();\r | |
89 | If Size is NULL, then ASSERT().\r | |
90 | \r | |
91 | @param FvHandle The device handle that contains a instance of EFI_FIRMWARE_VOLUME2_PROTOCOL instance.\r | |
92 | @param NameGuid The GUID name of a Firmware File.\r | |
93 | @param SectionType The Firmware Section type.\r | |
94 | @param SectionInstance The instance number of Firmware Section to read from starting from 0.\r | |
95 | @param Buffer On output, Buffer contains the the data read from the section in the Firmware File found.\r | |
96 | @param Size On output, the size of Buffer.\r | |
97 | \r | |
98 | @retval EFI_SUCCESS The image is found and data and size is returned.\r | |
b75a165d LG |
99 | @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.\r |
100 | @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r | |
101 | @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r | |
102 | @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.\r | |
166152e8 | 103 | \r |
f80b0830 | 104 | **/\r |
1c280088 | 105 | EFI_STATUS\r |
eb9dd4d0 | 106 | InternalGetSectionFromFv (\r |
166152e8 | 107 | IN EFI_HANDLE FvHandle,\r |
108 | IN CONST EFI_GUID *NameGuid,\r | |
109 | IN EFI_SECTION_TYPE SectionType,\r | |
ff197efb | 110 | IN UINTN SectionInstance,\r |
166152e8 | 111 | OUT VOID **Buffer,\r |
112 | OUT UINTN *Size\r | |
1c280088 | 113 | )\r |
114 | {\r | |
166152e8 | 115 | EFI_STATUS Status;\r |
116 | EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r | |
117 | UINT32 AuthenticationStatus;\r | |
118 | \r | |
8cf43dd7 | 119 | ASSERT (NameGuid != NULL);\r |
120 | ASSERT (Buffer != NULL);\r | |
121 | ASSERT (Size != NULL);\r | |
122 | \r | |
166152e8 | 123 | ASSERT (FvHandle != NULL);\r |
124 | \r | |
125 | Status = gBS->HandleProtocol (\r | |
126 | FvHandle,\r | |
127 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
128 | (VOID **) &Fv\r | |
129 | );\r | |
130 | if (EFI_ERROR (Status)) {\r | |
7ca066f9 | 131 | return EFI_NOT_FOUND;\r |
166152e8 | 132 | }\r |
1c280088 | 133 | \r |
134 | //\r | |
135 | // Read desired section content in NameGuid file\r | |
136 | //\r | |
137 | *Buffer = NULL;\r | |
138 | *Size = 0;\r | |
139 | Status = Fv->ReadSection (\r | |
140 | Fv,\r | |
141 | NameGuid,\r | |
142 | SectionType,\r | |
ff197efb | 143 | SectionInstance,\r |
1c280088 | 144 | Buffer,\r |
145 | Size,\r | |
146 | &AuthenticationStatus\r | |
147 | );\r | |
148 | \r | |
149 | if (EFI_ERROR (Status) && (SectionType == EFI_SECTION_TE)) {\r | |
150 | //\r | |
166152e8 | 151 | // Try reading PE32 section, if the required section is TE type \r |
1c280088 | 152 | //\r |
153 | *Buffer = NULL;\r | |
154 | *Size = 0;\r | |
155 | Status = Fv->ReadSection (\r | |
156 | Fv,\r | |
157 | NameGuid,\r | |
158 | EFI_SECTION_PE32,\r | |
ff197efb | 159 | SectionInstance,\r |
1c280088 | 160 | Buffer,\r |
161 | Size,\r | |
162 | &AuthenticationStatus\r | |
163 | );\r | |
164 | }\r | |
165 | \r | |
1c280088 | 166 | return Status;\r |
167 | }\r | |
168 | \r | |
682cee4c LG |
169 | /**\r |
170 | Searches all the available firmware volumes and returns the first matching FFS section. \r | |
171 | \r | |
172 | This function searches all the firmware volumes for FFS files with FV file type specified by FileType\r | |
173 | The order that the firmware volumes is searched is not deterministic. For each available FV a search \r | |
174 | is made for FFS file of type FileType. If the FV contains more than one FFS file with the same FileType, \r | |
175 | the FileInstance instance will be the matched FFS file. For each FFS file found a search \r | |
176 | is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances \r | |
177 | of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer. \r | |
178 | Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size. \r | |
179 | It is the caller's responsibility to use FreePool() to free the allocated buffer. \r | |
180 | See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections \r | |
181 | are retrieved from an FFS file based on SectionType and SectionInstance.\r | |
182 | \r | |
183 | If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r | |
184 | the search will be retried with a section type of EFI_SECTION_PE32.\r | |
185 | This function must be called with a TPL <= TPL_NOTIFY.\r | |
186 | \r | |
187 | If Buffer is NULL, then ASSERT().\r | |
188 | If Size is NULL, then ASSERT().\r | |
189 | \r | |
190 | @param FileType Indicates the FV file type to search for within all available FVs.\r | |
191 | @param FileInstance Indicates which file instance within all available FVs specified by FileType.\r | |
192 | FileInstance starts from zero.\r | |
193 | @param SectionType Indicates the FFS section type to search for within the FFS file \r | |
194 | specified by FileType with FileInstance.\r | |
195 | @param SectionInstance Indicates which section instance within the FFS file \r | |
196 | specified by FileType with FileInstance to retrieve. SectionInstance starts from zero.\r | |
197 | @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found.\r | |
198 | Is it the caller's responsibility to free this buffer using FreePool().\r | |
199 | @param Size On output, a pointer to the size, in bytes, of Buffer.\r | |
200 | \r | |
201 | @retval EFI_SUCCESS The specified FFS section was returned.\r | |
202 | @retval EFI_NOT_FOUND The specified FFS section could not be found.\r | |
203 | @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.\r | |
204 | @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.\r | |
205 | @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that \r | |
206 | contains the matching FFS section does not allow reads.\r | |
207 | **/\r | |
208 | EFI_STATUS\r | |
209 | EFIAPI\r | |
210 | GetSectionFromAnyFvByFileType (\r | |
211 | IN EFI_FV_FILETYPE FileType,\r | |
212 | IN UINTN FileInstance,\r | |
213 | IN EFI_SECTION_TYPE SectionType,\r | |
214 | IN UINTN SectionInstance,\r | |
215 | OUT VOID **Buffer,\r | |
216 | OUT UINTN *Size\r | |
217 | )\r | |
218 | {\r | |
219 | EFI_STATUS Status;\r | |
220 | EFI_HANDLE *HandleBuffer;\r | |
221 | UINTN HandleCount;\r | |
222 | UINTN IndexFv;\r | |
223 | UINTN IndexFile;\r | |
224 | UINTN Key;\r | |
225 | EFI_GUID NameGuid;\r | |
226 | EFI_FV_FILE_ATTRIBUTES Attributes;\r | |
227 | EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r | |
228 | \r | |
229 | //\r | |
230 | // Locate all available FVs.\r | |
231 | //\r | |
232 | HandleBuffer = NULL;\r | |
233 | Status = gBS->LocateHandleBuffer (\r | |
234 | ByProtocol,\r | |
235 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
236 | NULL,\r | |
237 | &HandleCount,\r | |
238 | &HandleBuffer\r | |
239 | );\r | |
240 | if (EFI_ERROR (Status)) {\r | |
241 | return Status;\r | |
242 | }\r | |
243 | \r | |
244 | //\r | |
245 | // Go through FVs one by one to find the required section data.\r | |
246 | //\r | |
247 | for (IndexFv = 0; IndexFv < HandleCount; IndexFv++) {\r | |
248 | Status = gBS->HandleProtocol (\r | |
249 | HandleBuffer[IndexFv],\r | |
250 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
251 | (VOID **)&Fv\r | |
252 | );\r | |
253 | if (EFI_ERROR (Status)) {\r | |
254 | continue;\r | |
255 | }\r | |
256 | \r | |
257 | //\r | |
258 | // Use Firmware Volume 2 Protocol to search for a file of type FileType in all FVs.\r | |
259 | //\r | |
260 | IndexFile = FileInstance + 1;\r | |
261 | Key = 0;\r | |
262 | do {\r | |
263 | Status = Fv->GetNextFile (Fv, &Key, &FileType, &NameGuid, &Attributes, Size);\r | |
264 | if (EFI_ERROR (Status)) {\r | |
265 | break;\r | |
266 | }\r | |
267 | IndexFile --;\r | |
268 | } while (IndexFile > 0);\r | |
b0d803fe | 269 | \r |
682cee4c LG |
270 | //\r |
271 | // Fv File with the required FV file type is found.\r | |
272 | // Search the section file in the found FV file.\r | |
273 | //\r | |
274 | if (IndexFile == 0) {\r | |
275 | Status = InternalGetSectionFromFv (\r | |
276 | HandleBuffer[IndexFv], \r | |
277 | &NameGuid,\r | |
278 | SectionType,\r | |
279 | SectionInstance,\r | |
280 | Buffer,\r | |
281 | Size\r | |
282 | );\r | |
283 | \r | |
284 | if (!EFI_ERROR (Status)) {\r | |
285 | goto Done;\r | |
286 | }\r | |
287 | }\r | |
288 | }\r | |
289 | \r | |
290 | //\r | |
291 | // The required FFS section file is not found. \r | |
292 | //\r | |
293 | if (IndexFv == HandleCount) {\r | |
294 | Status = EFI_NOT_FOUND;\r | |
295 | }\r | |
296 | \r | |
297 | Done:\r | |
298 | if (HandleBuffer != NULL) { \r | |
299 | FreePool(HandleBuffer);\r | |
300 | }\r | |
301 | \r | |
302 | return Status;\r | |
303 | }\r | |
b0d803fe | 304 | \r |
f80b0830 | 305 | /**\r |
b75a165d LG |
306 | Searches all the availables firmware volumes and returns the first matching FFS section. \r |
307 | \r | |
308 | This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid. \r | |
309 | The order that the firmware volumes is searched is not deterministic. For each FFS file found a search \r | |
310 | is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances \r | |
311 | of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer. \r | |
312 | Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size. \r | |
313 | It is the caller's responsibility to use FreePool() to free the allocated buffer. \r | |
314 | See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections \r | |
315 | are retrieved from an FFS file based on SectionType and SectionInstance.\r | |
316 | \r | |
317 | If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r | |
318 | the search will be retried with a section type of EFI_SECTION_PE32.\r | |
319 | This function must be called with a TPL <= TPL_NOTIFY.\r | |
320 | \r | |
321 | If NameGuid is NULL, then ASSERT().\r | |
322 | If Buffer is NULL, then ASSERT().\r | |
ff197efb | 323 | If Size is NULL, then ASSERT().\r |
324 | \r | |
f80b0830 | 325 | \r |
b75a165d LG |
326 | @param NameGuid A pointer to to the FFS filename GUID to search for within \r |
327 | any of the firmware volumes in the platform. \r | |
328 | @param SectionType Indicates the FFS section type to search for within the FFS file specified by NameGuid.\r | |
329 | @param SectionInstance Indicates which section instance within the FFS file specified by NameGuid to retrieve.\r | |
330 | @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found. \r | |
3e5c3238 | 331 | Is it the caller's responsibility to free this buffer using FreePool().\r |
b75a165d LG |
332 | @param Size On output, a pointer to the size, in bytes, of Buffer.\r |
333 | \r | |
334 | @retval EFI_SUCCESS The specified FFS section was returned.\r | |
335 | @retval EFI_NOT_FOUND The specified FFS section could not be found.\r | |
3e5c3238 | 336 | @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.\r |
b75a165d LG |
337 | @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.\r |
338 | @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that \r | |
339 | contains the matching FFS section does not allow reads.\r | |
f80b0830 | 340 | **/\r |
b0d803fe | 341 | EFI_STATUS\r |
342 | EFIAPI\r | |
eb9dd4d0 | 343 | GetSectionFromAnyFv (\r |
b0d803fe | 344 | IN CONST EFI_GUID *NameGuid,\r |
345 | IN EFI_SECTION_TYPE SectionType,\r | |
ff197efb | 346 | IN UINTN SectionInstance,\r |
b0d803fe | 347 | OUT VOID **Buffer,\r |
348 | OUT UINTN *Size\r | |
349 | )\r | |
350 | {\r | |
351 | EFI_STATUS Status;\r | |
352 | EFI_HANDLE *HandleBuffer;\r | |
353 | UINTN HandleCount;\r | |
354 | UINTN Index;\r | |
355 | EFI_HANDLE FvHandle;\r | |
b0d803fe | 356 | \r |
357 | //\r | |
358 | // Search the FV that contain the caller's FFS first.\r | |
359 | // FV builder can choose to build FFS into the this FV\r | |
360 | // so that this implementation of GetSectionFromAnyFv\r | |
361 | // will locate the FFS faster.\r | |
362 | //\r | |
166152e8 | 363 | FvHandle = InternalImageHandleToFvHandle (gImageHandle);\r |
eb9dd4d0 | 364 | Status = InternalGetSectionFromFv (\r |
b0d803fe | 365 | FvHandle,\r |
366 | NameGuid,\r | |
367 | SectionType,\r | |
ff197efb | 368 | SectionInstance,\r |
b0d803fe | 369 | Buffer,\r |
370 | Size\r | |
371 | );\r | |
372 | if (!EFI_ERROR (Status)) {\r | |
373 | return EFI_SUCCESS;\r | |
374 | }\r | |
375 | \r | |
b0d803fe | 376 | HandleBuffer = NULL;\r |
377 | Status = gBS->LocateHandleBuffer (\r | |
378 | ByProtocol,\r | |
379 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
380 | NULL,\r | |
381 | &HandleCount,\r | |
382 | &HandleBuffer\r | |
383 | );\r | |
384 | if (EFI_ERROR (Status)) {\r | |
385 | goto Done;\r | |
386 | }\r | |
387 | \r | |
ff197efb | 388 | for (Index = 0; Index < HandleCount; Index++) {\r |
b0d803fe | 389 | //\r |
390 | // Skip the FV that contain the caller's FFS\r | |
391 | //\r | |
ff197efb | 392 | if (HandleBuffer[Index] != FvHandle) {\r |
eb9dd4d0 | 393 | Status = InternalGetSectionFromFv (\r |
ff197efb | 394 | HandleBuffer[Index], \r |
395 | NameGuid, \r | |
396 | SectionType, \r | |
397 | SectionInstance,\r | |
398 | Buffer, \r | |
399 | Size\r | |
400 | );\r | |
401 | \r | |
402 | if (!EFI_ERROR (Status)) {\r | |
403 | goto Done;\r | |
404 | }\r | |
b0d803fe | 405 | }\r |
406 | \r | |
b0d803fe | 407 | }\r |
408 | \r | |
409 | if (Index == HandleCount) {\r | |
410 | Status = EFI_NOT_FOUND;\r | |
411 | }\r | |
412 | \r | |
413 | Done:\r | |
414 | \r | |
b0d803fe | 415 | if (HandleBuffer != NULL) { \r |
416 | FreePool(HandleBuffer);\r | |
417 | }\r | |
418 | return Status;\r | |
419 | \r | |
420 | }\r | |
421 | \r | |
f80b0830 | 422 | /**\r |
b75a165d LG |
423 | Searches the firmware volume that the currently executing module was loaded from and returns the first matching FFS section. \r |
424 | \r | |
425 | This function searches the firmware volume that the currently executing module was loaded \r | |
426 | from for an FFS file with an FFS filename specified by NameGuid. If the FFS file is found a search \r | |
427 | is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance \r | |
428 | instances of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.\r | |
429 | Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size. \r | |
430 | It is the caller's responsibility to use FreePool() to free the allocated buffer. \r | |
431 | See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections are retrieved from \r | |
432 | an FFS file based on SectionType and SectionInstance.\r | |
433 | \r | |
434 | If the currently executing module was not loaded from a firmware volume, then EFI_NOT_FOUND is returned.\r | |
435 | If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r | |
436 | the search will be retried with a section type of EFI_SECTION_PE32.\r | |
437 | \r | |
438 | This function must be called with a TPL <= TPL_NOTIFY.\r | |
439 | If NameGuid is NULL, then ASSERT().\r | |
440 | If Buffer is NULL, then ASSERT().\r | |
ff197efb | 441 | If Size is NULL, then ASSERT().\r |
442 | \r | |
b75a165d LG |
443 | @param NameGuid A pointer to to the FFS filename GUID to search for within \r |
444 | the firmware volumes that the currently executing module was loaded from.\r | |
445 | @param SectionType Indicates the FFS section type to search for within the FFS file specified by NameGuid.\r | |
446 | @param SectionInstance Indicates which section instance within the FFS file specified by NameGuid to retrieve.\r | |
447 | @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found. \r | |
3e5c3238 | 448 | Is it the caller's responsibility to free this buffer using FreePool().\r |
b75a165d LG |
449 | @param Size On output, a pointer to the size, in bytes, of Buffer.\r |
450 | \r | |
451 | \r | |
452 | @retval EFI_SUCCESS The specified FFS section was returned.\r | |
453 | @retval EFI_NOT_FOUND The specified FFS section could not be found.\r | |
3e5c3238 | 454 | @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.\r |
b75a165d LG |
455 | @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.\r |
456 | @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that \r | |
457 | contains the matching FFS section does not allow reads. \r | |
f80b0830 | 458 | **/\r |
b0d803fe | 459 | EFI_STATUS\r |
460 | EFIAPI\r | |
eb9dd4d0 | 461 | GetSectionFromFv (\r |
b0d803fe | 462 | IN CONST EFI_GUID *NameGuid,\r |
463 | IN EFI_SECTION_TYPE SectionType,\r | |
ff197efb | 464 | IN UINTN SectionInstance,\r |
b0d803fe | 465 | OUT VOID **Buffer,\r |
466 | OUT UINTN *Size\r | |
467 | )\r | |
468 | {\r | |
eb9dd4d0 | 469 | return InternalGetSectionFromFv (\r |
470 | InternalImageHandleToFvHandle(gImageHandle),\r | |
471 | NameGuid,\r | |
472 | SectionType,\r | |
473 | SectionInstance,\r | |
474 | Buffer,\r | |
475 | Size\r | |
476 | );\r | |
b0d803fe | 477 | }\r |
478 | \r | |
479 | \r | |
f80b0830 | 480 | /**\r |
b75a165d LG |
481 | Searches the FFS file the the currently executing module was loaded from and returns the first matching FFS section.\r |
482 | \r | |
483 | This function searches the FFS file that the currently executing module was loaded from for a FFS sections of type SectionType.\r | |
484 | If the FFS file contains at least SectionInstance instances of the FFS section specified by SectionType, \r | |
485 | then the SectionInstance instance is returned in Buffer. Buffer is allocated using AllocatePool(), \r | |
486 | and the size of the allocated buffer is returned in Size. It is the caller's responsibility \r | |
487 | to use FreePool() to free the allocated buffer. See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for \r | |
488 | details on how sections are retrieved from an FFS file based on SectionType and SectionInstance.\r | |
489 | \r | |
490 | If the currently executing module was not loaded from an FFS file, then EFI_NOT_FOUND is returned.\r | |
491 | If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r | |
492 | the search will be retried with a section type of EFI_SECTION_PE32.\r | |
493 | This function must be called with a TPL <= TPL_NOTIFY.\r | |
494 | \r | |
495 | If Buffer is NULL, then ASSERT().\r | |
ff197efb | 496 | If Size is NULL, then ASSERT().\r |
497 | \r | |
b75a165d LG |
498 | \r |
499 | @param SectionType Indicates the FFS section type to search for within the FFS file \r | |
500 | that the currently executing module was loaded from.\r | |
501 | @param SectionInstance Indicates which section instance to retrieve within the FFS file \r | |
502 | that the currently executing module was loaded from.\r | |
503 | @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found. \r | |
3e5c3238 | 504 | Is it the caller's responsibility to free this buffer using FreePool().\r |
b75a165d LG |
505 | @param Size On output, a pointer to the size, in bytes, of Buffer.\r |
506 | \r | |
507 | @retval EFI_SUCCESS The specified FFS section was returned.\r | |
508 | @retval EFI_NOT_FOUND The specified FFS section could not be found.\r | |
3e5c3238 | 509 | @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.\r |
b75a165d LG |
510 | @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.\r |
511 | @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that \r | |
512 | contains the matching FFS section does not allow reads. \r | |
f80b0830 | 513 | \r |
514 | **/\r | |
b0d803fe | 515 | EFI_STATUS\r |
516 | EFIAPI\r | |
eb9dd4d0 | 517 | GetSectionFromFfs (\r |
b0d803fe | 518 | IN EFI_SECTION_TYPE SectionType,\r |
ff197efb | 519 | IN UINTN SectionInstance,\r |
b0d803fe | 520 | OUT VOID **Buffer,\r |
521 | OUT UINTN *Size\r | |
522 | )\r | |
523 | {\r | |
eb9dd4d0 | 524 | return InternalGetSectionFromFv(\r |
525 | InternalImageHandleToFvHandle(gImageHandle),\r | |
526 | &gEfiCallerIdGuid,\r | |
527 | SectionType,\r | |
528 | SectionInstance,\r | |
529 | Buffer,\r | |
530 | Size\r | |
531 | );\r | |
b0d803fe | 532 | }\r |
533 | \r | |
84716614 LG |
534 | \r |
535 | /**\r | |
536 | Get the image file buffer data and buffer size by its device path. \r | |
537 | \r | |
ea6898b9 | 538 | Access the file either from a firmware volume, from a file system interface, \r |
84716614 LG |
539 | or from the load file interface.\r |
540 | \r | |
541 | Allocate memory to store the found image. The caller is responsible to free memory.\r | |
542 | \r | |
543 | If File is NULL, then NULL is returned.\r | |
544 | If FileSize is NULL, then NULL is returned.\r | |
545 | If AuthenticationStatus is NULL, then NULL is returned.\r | |
546 | \r | |
ea6898b9 | 547 | @param[in] BootPolicy Policy for Open Image File.If TRUE, indicates that the request \r |
548 | originates from the boot manager, and that the boot manager is\r | |
549 | attempting to load FilePath as a boot selection. If FALSE, \r | |
550 | then FilePath must match an exact file to be loaded.\r | |
2fc59a00 | 551 | @param[in] FilePath The pointer to the device path of the file that is absracted to\r |
ea6898b9 | 552 | the file buffer.\r |
2fc59a00 | 553 | @param[out] FileSize The pointer to the size of the abstracted file buffer.\r |
554 | @param[out] AuthenticationStatus The pointer to a caller-allocated UINT32 in which the authentication\r | |
ea6898b9 | 555 | status is returned.\r |
84716614 LG |
556 | \r |
557 | @retval NULL File is NULL, or FileSize is NULL. Or the file can't be found.\r | |
558 | @retval other The abstracted file buffer. The caller is responsible to free memory.\r | |
559 | **/\r | |
560 | VOID *\r | |
561 | EFIAPI\r | |
562 | GetFileBufferByFilePath (\r | |
563 | IN BOOLEAN BootPolicy,\r | |
564 | IN CONST EFI_DEVICE_PATH_PROTOCOL *FilePath,\r | |
565 | OUT UINTN *FileSize,\r | |
566 | OUT UINT32 *AuthenticationStatus\r | |
567 | )\r | |
568 | {\r | |
569 | EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r | |
570 | EFI_DEVICE_PATH_PROTOCOL *OrigDevicePathNode;\r | |
95dd343a | 571 | EFI_DEVICE_PATH_PROTOCOL *TempDevicePathNode;\r |
84716614 LG |
572 | EFI_HANDLE Handle;\r |
573 | EFI_GUID *FvNameGuid;\r | |
574 | EFI_FIRMWARE_VOLUME2_PROTOCOL *FwVol;\r | |
575 | EFI_SECTION_TYPE SectionType;\r | |
576 | UINT8 *ImageBuffer;\r | |
577 | UINTN ImageBufferSize;\r | |
578 | EFI_FV_FILETYPE Type;\r | |
579 | EFI_FV_FILE_ATTRIBUTES Attrib;\r | |
580 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r | |
581 | EFI_FILE_HANDLE FileHandle;\r | |
582 | EFI_FILE_HANDLE LastHandle;\r | |
583 | EFI_FILE_INFO *FileInfo;\r | |
584 | UINTN FileInfoSize;\r | |
585 | EFI_LOAD_FILE_PROTOCOL *LoadFile;\r | |
586 | EFI_LOAD_FILE2_PROTOCOL *LoadFile2;\r | |
587 | EFI_STATUS Status;\r | |
588 | \r | |
589 | //\r | |
590 | // Check input File device path.\r | |
591 | //\r | |
592 | if (FilePath == NULL || FileSize == NULL || AuthenticationStatus == NULL) {\r | |
593 | return NULL;\r | |
594 | }\r | |
595 | \r | |
596 | //\r | |
597 | // Init local variable\r | |
598 | //\r | |
95dd343a | 599 | TempDevicePathNode = NULL;\r |
84716614 LG |
600 | FvNameGuid = NULL;\r |
601 | FileInfo = NULL;\r | |
602 | FileHandle = NULL;\r | |
603 | ImageBuffer = NULL;\r | |
604 | ImageBufferSize = 0;\r | |
605 | *AuthenticationStatus = 0;\r | |
606 | \r | |
607 | //\r | |
608 | // Copy File Device Path\r | |
609 | //\r | |
610 | OrigDevicePathNode = DuplicateDevicePath (FilePath);\r | |
611 | if (OrigDevicePathNode == NULL) {\r | |
612 | return NULL;\r | |
613 | }\r | |
614 | \r | |
615 | //\r | |
616 | // Check whether this device path support FV2 protocol.\r | |
617 | // Is so, this device path may contain a Image.\r | |
618 | //\r | |
619 | DevicePathNode = OrigDevicePathNode;\r | |
620 | Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &DevicePathNode, &Handle);\r | |
621 | if (!EFI_ERROR (Status)) {\r | |
622 | //\r | |
623 | // For FwVol File system there is only a single file name that is a GUID.\r | |
624 | //\r | |
625 | FvNameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePathNode);\r | |
626 | if (FvNameGuid == NULL) {\r | |
627 | Status = EFI_INVALID_PARAMETER;\r | |
628 | } else {\r | |
629 | //\r | |
630 | // Read image from the firmware file\r | |
631 | //\r | |
632 | Status = gBS->HandleProtocol (Handle, &gEfiFirmwareVolume2ProtocolGuid, (VOID**)&FwVol);\r | |
633 | if (!EFI_ERROR (Status)) {\r | |
634 | SectionType = EFI_SECTION_PE32;\r | |
635 | ImageBuffer = NULL;\r | |
636 | Status = FwVol->ReadSection (\r | |
637 | FwVol,\r | |
638 | FvNameGuid,\r | |
639 | SectionType,\r | |
640 | 0,\r | |
641 | (VOID **)&ImageBuffer,\r | |
642 | &ImageBufferSize,\r | |
643 | AuthenticationStatus\r | |
644 | );\r | |
645 | if (EFI_ERROR (Status)) {\r | |
646 | //\r | |
647 | // Try a raw file, since a PE32 SECTION does not exist\r | |
648 | //\r | |
649 | if (ImageBuffer != NULL) {\r | |
650 | FreePool (ImageBuffer);\r | |
651 | *AuthenticationStatus = 0;\r | |
652 | }\r | |
653 | ImageBuffer = NULL;\r | |
654 | Status = FwVol->ReadFile (\r | |
655 | FwVol,\r | |
656 | FvNameGuid,\r | |
657 | (VOID **)&ImageBuffer,\r | |
658 | &ImageBufferSize,\r | |
659 | &Type,\r | |
660 | &Attrib,\r | |
661 | AuthenticationStatus\r | |
662 | );\r | |
663 | }\r | |
664 | }\r | |
665 | }\r | |
666 | goto Finish;\r | |
667 | }\r | |
668 | \r | |
669 | //\r | |
670 | // Attempt to access the file via a file system interface\r | |
671 | //\r | |
672 | DevicePathNode = OrigDevicePathNode;\r | |
673 | Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &DevicePathNode, &Handle);\r | |
674 | if (!EFI_ERROR (Status)) {\r | |
675 | Status = gBS->HandleProtocol (Handle, &gEfiSimpleFileSystemProtocolGuid, (VOID**)&Volume);\r | |
676 | if (!EFI_ERROR (Status)) {\r | |
677 | //\r | |
678 | // Open the Volume to get the File System handle\r | |
679 | //\r | |
680 | Status = Volume->OpenVolume (Volume, &FileHandle);\r | |
681 | if (!EFI_ERROR (Status)) {\r | |
95dd343a LG |
682 | //\r |
683 | // Duplicate the device path to avoid the access to unaligned device path node.\r | |
684 | // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH\r | |
685 | // nodes, It assures the fields in device path nodes are 2 byte aligned.\r | |
686 | //\r | |
687 | TempDevicePathNode = DuplicateDevicePath (DevicePathNode);\r | |
688 | if (TempDevicePathNode == NULL) {\r | |
689 | FileHandle->Close (FileHandle);\r | |
690 | Status = EFI_OUT_OF_RESOURCES;\r | |
691 | goto Finish;\r | |
692 | }\r | |
84716614 LG |
693 | //\r |
694 | // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the\r | |
695 | // directory information and filename can be seperate. The goal is to inch\r | |
696 | // our way down each device path node and close the previous node\r | |
697 | //\r | |
95dd343a | 698 | DevicePathNode = TempDevicePathNode;\r |
84716614 LG |
699 | while (!IsDevicePathEnd (DevicePathNode) && !EFI_ERROR (Status)) {\r |
700 | if (DevicePathType (DevicePathNode) != MEDIA_DEVICE_PATH ||\r | |
701 | DevicePathSubType (DevicePathNode) != MEDIA_FILEPATH_DP) {\r | |
702 | Status = EFI_UNSUPPORTED;\r | |
703 | break;\r | |
704 | }\r | |
705 | \r | |
706 | LastHandle = FileHandle;\r | |
707 | FileHandle = NULL;\r | |
708 | \r | |
709 | Status = LastHandle->Open (\r | |
710 | LastHandle,\r | |
711 | &FileHandle,\r | |
712 | ((FILEPATH_DEVICE_PATH *) DevicePathNode)->PathName,\r | |
713 | EFI_FILE_MODE_READ,\r | |
714 | 0\r | |
715 | );\r | |
716 | \r | |
717 | //\r | |
718 | // Close the previous node\r | |
719 | //\r | |
720 | LastHandle->Close (LastHandle);\r | |
721 | \r | |
722 | DevicePathNode = NextDevicePathNode (DevicePathNode);\r | |
723 | }\r | |
724 | \r | |
725 | if (!EFI_ERROR (Status)) {\r | |
726 | //\r | |
727 | // We have found the file. Now we need to read it. Before we can read the file we need to\r | |
728 | // figure out how big the file is.\r | |
729 | //\r | |
730 | FileInfo = NULL;\r | |
731 | FileInfoSize = 0;\r | |
732 | Status = FileHandle->GetInfo (\r | |
733 | FileHandle,\r | |
734 | &gEfiFileInfoGuid,\r | |
735 | &FileInfoSize,\r | |
736 | FileInfo\r | |
737 | );\r | |
738 | \r | |
739 | if (Status == EFI_BUFFER_TOO_SMALL) {\r | |
740 | FileInfo = AllocatePool (FileInfoSize);\r | |
741 | if (FileInfo == NULL) {\r | |
742 | Status = EFI_OUT_OF_RESOURCES;\r | |
743 | } else {\r | |
744 | Status = FileHandle->GetInfo (\r | |
745 | FileHandle,\r | |
746 | &gEfiFileInfoGuid,\r | |
747 | &FileInfoSize,\r | |
748 | FileInfo\r | |
749 | );\r | |
750 | }\r | |
751 | }\r | |
752 | \r | |
e191bb14 | 753 | if (!EFI_ERROR (Status) && (FileInfo != NULL)) {\r |
84716614 LG |
754 | //\r |
755 | // Allocate space for the file\r | |
756 | //\r | |
757 | ImageBuffer = AllocatePool ((UINTN)FileInfo->FileSize);\r | |
758 | if (ImageBuffer == NULL) {\r | |
759 | Status = EFI_OUT_OF_RESOURCES;\r | |
760 | } else {\r | |
761 | //\r | |
762 | // Read the file into the buffer we allocated\r | |
763 | //\r | |
764 | ImageBufferSize = (UINTN)FileInfo->FileSize;\r | |
765 | Status = FileHandle->Read (FileHandle, &ImageBufferSize, ImageBuffer);\r | |
766 | }\r | |
767 | }\r | |
768 | }\r | |
769 | //\r | |
95dd343a | 770 | // Close the file and Free FileInfo and TempDevicePathNode since we are done\r |
84716614 LG |
771 | // \r |
772 | if (FileInfo != NULL) {\r | |
773 | FreePool (FileInfo);\r | |
774 | }\r | |
775 | if (FileHandle != NULL) {\r | |
776 | FileHandle->Close (FileHandle);\r | |
777 | }\r | |
95dd343a | 778 | FreePool (TempDevicePathNode);\r |
84716614 LG |
779 | }\r |
780 | }\r | |
781 | goto Finish;\r | |
782 | }\r | |
783 | \r | |
784 | //\r | |
785 | // Attempt to access the file via LoadFile2 interface\r | |
786 | //\r | |
787 | if (!BootPolicy) {\r | |
788 | DevicePathNode = OrigDevicePathNode;\r | |
789 | Status = gBS->LocateDevicePath (&gEfiLoadFile2ProtocolGuid, &DevicePathNode, &Handle);\r | |
790 | if (!EFI_ERROR (Status)) {\r | |
791 | Status = gBS->HandleProtocol (Handle, &gEfiLoadFile2ProtocolGuid, (VOID**)&LoadFile2);\r | |
792 | if (!EFI_ERROR (Status)) {\r | |
793 | //\r | |
794 | // Call LoadFile2 with the correct buffer size\r | |
795 | //\r | |
796 | ImageBufferSize = 0;\r | |
797 | ImageBuffer = NULL;\r | |
798 | Status = LoadFile2->LoadFile (\r | |
799 | LoadFile2,\r | |
800 | DevicePathNode,\r | |
801 | FALSE,\r | |
802 | &ImageBufferSize,\r | |
803 | ImageBuffer\r | |
804 | );\r | |
805 | if (Status == EFI_BUFFER_TOO_SMALL) {\r | |
806 | ImageBuffer = AllocatePool (ImageBufferSize);\r | |
807 | if (ImageBuffer == NULL) {\r | |
808 | Status = EFI_OUT_OF_RESOURCES;\r | |
809 | } else {\r | |
810 | Status = LoadFile2->LoadFile (\r | |
811 | LoadFile2,\r | |
812 | DevicePathNode,\r | |
c083f7ca | 813 | FALSE,\r |
84716614 LG |
814 | &ImageBufferSize,\r |
815 | ImageBuffer\r | |
816 | );\r | |
817 | }\r | |
818 | }\r | |
819 | }\r | |
820 | goto Finish;\r | |
821 | }\r | |
822 | }\r | |
823 | \r | |
824 | //\r | |
825 | // Attempt to access the file via LoadFile interface\r | |
826 | //\r | |
827 | DevicePathNode = OrigDevicePathNode;\r | |
828 | Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &DevicePathNode, &Handle);\r | |
829 | if (!EFI_ERROR (Status)) {\r | |
830 | Status = gBS->HandleProtocol (Handle, &gEfiLoadFileProtocolGuid, (VOID**)&LoadFile);\r | |
831 | if (!EFI_ERROR (Status)) {\r | |
832 | //\r | |
833 | // Call LoadFile with the correct buffer size\r | |
834 | //\r | |
835 | ImageBufferSize = 0;\r | |
836 | ImageBuffer = NULL;\r | |
837 | Status = LoadFile->LoadFile (\r | |
838 | LoadFile,\r | |
839 | DevicePathNode,\r | |
840 | BootPolicy,\r | |
841 | &ImageBufferSize,\r | |
842 | ImageBuffer\r | |
843 | );\r | |
844 | if (Status == EFI_BUFFER_TOO_SMALL) {\r | |
845 | ImageBuffer = AllocatePool (ImageBufferSize);\r | |
846 | if (ImageBuffer == NULL) {\r | |
847 | Status = EFI_OUT_OF_RESOURCES;\r | |
848 | } else {\r | |
849 | Status = LoadFile->LoadFile (\r | |
850 | LoadFile,\r | |
851 | DevicePathNode,\r | |
852 | BootPolicy,\r | |
853 | &ImageBufferSize,\r | |
854 | ImageBuffer\r | |
855 | );\r | |
856 | }\r | |
857 | }\r | |
858 | }\r | |
859 | }\r | |
860 | \r | |
861 | Finish:\r | |
862 | \r | |
863 | if (EFI_ERROR (Status)) {\r | |
864 | if (ImageBuffer != NULL) {\r | |
865 | FreePool (ImageBuffer);\r | |
866 | ImageBuffer = NULL;\r | |
867 | }\r | |
868 | *FileSize = 0;\r | |
869 | } else {\r | |
870 | *FileSize = ImageBufferSize;\r | |
871 | }\r | |
872 | \r | |
873 | FreePool (OrigDevicePathNode);\r | |
874 | \r | |
875 | return ImageBuffer;\r | |
876 | }\r |