2 Implements functions to read firmware file.
4 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "FwVolDriver.h"
13 Required Alignment Alignment Value in FFS FFS_ATTRIB_DATA_ALIGNMENT2 Alignment Value in
14 (bytes) Attributes Field in FFS Attributes Field Firmware Volume Interfaces
32 UINT8 mFvAttributes
[] = {0, 4, 7, 9, 10, 12, 15, 16};
33 UINT8 mFvAttributes2
[] = {17, 18, 19, 20, 21, 22, 23, 24};
36 Convert the FFS File Attributes to FV File Attributes.
38 @param FfsAttributes The attributes of UINT8 type.
40 @return The attributes of EFI_FV_FILE_ATTRIBUTES
43 EFI_FV_FILE_ATTRIBUTES
44 FfsAttributes2FvFileAttributes (
45 IN EFI_FFS_FILE_ATTRIBUTES FfsAttributes
49 EFI_FV_FILE_ATTRIBUTES FileAttribute
;
51 DataAlignment
= (UINT8
) ((FfsAttributes
& FFS_ATTRIB_DATA_ALIGNMENT
) >> 3);
52 ASSERT (DataAlignment
< 8);
54 if ((FfsAttributes
& FFS_ATTRIB_DATA_ALIGNMENT_2
) != 0) {
55 FileAttribute
= (EFI_FV_FILE_ATTRIBUTES
) mFvAttributes2
[DataAlignment
];
57 FileAttribute
= (EFI_FV_FILE_ATTRIBUTES
) mFvAttributes
[DataAlignment
];
60 if ((FfsAttributes
& FFS_ATTRIB_FIXED
) == FFS_ATTRIB_FIXED
) {
61 FileAttribute
|= EFI_FV_FILE_ATTRIB_FIXED
;
68 Given the input key, search for the next matching file in the volume.
70 @param This Indicates the calling context.
71 @param Key Key is a pointer to a caller allocated
72 buffer that contains implementation specific
73 data that is used to track where to begin
74 the search for the next file. The size of
75 the buffer must be at least This->KeySize
76 bytes long. To reinitialize the search and
77 begin from the beginning of the firmware
78 volume, the entire buffer must be cleared to
79 zero. Other than clearing the buffer to
80 initiate a new search, the caller must not
81 modify the data in the buffer between calls
83 @param FileType FileType is a pointer to a caller allocated
84 EFI_FV_FILETYPE. The GetNextFile() API can
85 filter it's search for files based on the
86 value of *FileType input. A *FileType input
87 of 0 causes GetNextFile() to search for
88 files of all types. If a file is found, the
89 file's type is returned in *FileType.
90 *FileType is not modified if no file is
92 @param NameGuid NameGuid is a pointer to a caller allocated
93 EFI_GUID. If a file is found, the file's
94 name is returned in *NameGuid. *NameGuid is
95 not modified if no file is found.
96 @param Attributes Attributes is a pointer to a caller
97 allocated EFI_FV_FILE_ATTRIBUTES. If a file
98 is found, the file's attributes are returned
99 in *Attributes. *Attributes is not modified
101 @param Size Size is a pointer to a caller allocated
102 UINTN. If a file is found, the file's size
103 is returned in *Size. *Size is not modified
106 @retval EFI_SUCCESS Successfully find the file.
107 @retval EFI_DEVICE_ERROR Device error.
108 @retval EFI_ACCESS_DENIED Fv could not read.
109 @retval EFI_NOT_FOUND No matching file found.
110 @retval EFI_INVALID_PARAMETER Invalid parameter
116 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
118 IN OUT EFI_FV_FILETYPE
*FileType
,
119 OUT EFI_GUID
*NameGuid
,
120 OUT EFI_FV_FILE_ATTRIBUTES
*Attributes
,
126 EFI_FV_ATTRIBUTES FvAttributes
;
127 EFI_FFS_FILE_HEADER
*FfsFileHeader
;
130 FFS_FILE_LIST_ENTRY
*FfsFileEntry
;
132 FvDevice
= FV_DEVICE_FROM_THIS (This
);
134 Status
= This
->GetVolumeAttributes (This
, &FvAttributes
);
135 if (EFI_ERROR (Status
)) {
139 KeyValue
= (UINTN
*) Key
;
140 FfsFileHeader
= NULL
;
143 // Check if read operation is enabled
145 if ((FvAttributes
& EFI_FV2_READ_STATUS
) == 0) {
146 return EFI_ACCESS_DENIED
;
149 if (*FileType
> EFI_FV_FILETYPE_SMM_CORE
) {
151 // File type needs to be in 0 - 0x0D
153 return EFI_NOT_FOUND
;
157 if (*KeyValue
== 0) {
159 // Search for 1st matching file
161 Link
= &FvDevice
->FfsFileListHeader
;
162 if (Link
->ForwardLink
== &FvDevice
->FfsFileListHeader
) {
163 return EFI_NOT_FOUND
;
166 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*) Link
->ForwardLink
;
167 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) FfsFileEntry
->FfsHeader
;
172 *KeyValue
= (UINTN
) FfsFileEntry
;
175 // we ignore pad files
177 if (FfsFileHeader
->Type
== EFI_FV_FILETYPE_FFS_PAD
) {
181 if (*FileType
== 0) {
185 if (*FileType
== FfsFileHeader
->Type
) {
191 // Getting link from last Ffs
193 Link
= (LIST_ENTRY
*) (*KeyValue
);
194 if (Link
->ForwardLink
== &FvDevice
->FfsFileListHeader
) {
195 return EFI_NOT_FOUND
;
198 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*) Link
->ForwardLink
;
199 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) FfsFileEntry
->FfsHeader
;
204 *KeyValue
= (UINTN
) FfsFileEntry
;
207 // we ignore pad files
209 if (FfsFileHeader
->Type
== EFI_FV_FILETYPE_FFS_PAD
) {
213 if (*FileType
== EFI_FV_FILETYPE_ALL
) {
217 if (*FileType
== FfsFileHeader
->Type
) {
221 } while (Link
->ForwardLink
!= &FvDevice
->FfsFileListHeader
);
224 // Cache this file entry
226 FvDevice
->CurrentFfsFile
= FfsFileEntry
;
228 *FileType
= FfsFileHeader
->Type
;
229 CopyGuid (NameGuid
, &FfsFileHeader
->Name
);
230 *Attributes
= FfsAttributes2FvFileAttributes (FfsFileHeader
->Attributes
);
231 if ((FvDevice
->FwVolHeader
->Attributes
& EFI_FVB2_MEMORY_MAPPED
) == EFI_FVB2_MEMORY_MAPPED
) {
232 *Attributes
|= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED
;
236 // we need to substract the header size
238 if (IS_FFS_FILE2 (FfsFileHeader
)) {
239 *Size
= FFS_FILE2_SIZE (FfsFileHeader
) - sizeof (EFI_FFS_FILE_HEADER2
);
241 *Size
= FFS_FILE_SIZE (FfsFileHeader
) - sizeof (EFI_FFS_FILE_HEADER
);
244 if (CompareGuid (&gEfiFirmwareVolumeTopFileGuid
, NameGuid
)) {
246 // specially deal with VTF file
251 if (IS_FFS_FILE2 (FfsFileHeader
)) {
252 SrcPtr
= ((UINT8
*) FfsFileHeader
) + sizeof (EFI_FFS_FILE_HEADER2
);
254 SrcPtr
= ((UINT8
*) FfsFileHeader
) + sizeof (EFI_FFS_FILE_HEADER
);
258 Tmp
= *(UINT32
*) SrcPtr
;
272 Locates a file in the firmware volume and
273 copies it to the supplied buffer.
275 @param This Indicates the calling context.
276 @param NameGuid Pointer to an EFI_GUID, which is the
278 @param Buffer Buffer is a pointer to pointer to a buffer
279 in which the file or section contents or are
281 @param BufferSize BufferSize is a pointer to caller allocated
282 UINTN. On input *BufferSize indicates the
283 size in bytes of the memory region pointed
284 to by Buffer. On output, *BufferSize
285 contains the number of bytes required to
287 @param FoundType FoundType is a pointer to a caller allocated
288 EFI_FV_FILETYPE that on successful return
289 from Read() contains the type of file read.
290 This output reflects the file type
291 irrespective of the value of the SectionType
293 @param FileAttributes FileAttributes is a pointer to a caller
294 allocated EFI_FV_FILE_ATTRIBUTES. On
295 successful return from Read(),
296 *FileAttributes contains the attributes of
298 @param AuthenticationStatus AuthenticationStatus is a pointer to a
299 caller allocated UINTN in which the
300 authentication status is returned.
302 @retval EFI_SUCCESS Successfully read to memory buffer.
303 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.
304 @retval EFI_NOT_FOUND Not found.
305 @retval EFI_DEVICE_ERROR Device error.
306 @retval EFI_ACCESS_DENIED Could not read.
307 @retval EFI_INVALID_PARAMETER Invalid parameter.
308 @retval EFI_OUT_OF_RESOURCES Not enough buffer to be allocated.
314 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
315 IN CONST EFI_GUID
*NameGuid
,
316 IN OUT VOID
**Buffer
,
317 IN OUT UINTN
*BufferSize
,
318 OUT EFI_FV_FILETYPE
*FoundType
,
319 OUT EFI_FV_FILE_ATTRIBUTES
*FileAttributes
,
320 OUT UINT32
*AuthenticationStatus
326 EFI_GUID SearchNameGuid
;
327 EFI_FV_ATTRIBUTES FvAttributes
;
328 EFI_FV_FILETYPE LocalFoundType
;
329 EFI_FV_FILE_ATTRIBUTES LocalAttributes
;
332 FFS_FILE_LIST_ENTRY
*FfsFileEntry
;
333 EFI_FFS_FILE_HEADER
*FfsHeader
;
336 if (NULL
== This
|| NULL
== NameGuid
) {
337 return EFI_INVALID_PARAMETER
;
340 FvDevice
= FV_DEVICE_FROM_THIS (This
);
342 Status
= This
->GetVolumeAttributes (This
, &FvAttributes
);
343 if (EFI_ERROR (Status
)) {
347 // First check to see that FV is enabled for reads...
349 if (0 == (FvAttributes
& EFI_FV2_READ_STATUS
)) {
350 return EFI_ACCESS_DENIED
;
356 // Check if the file was read last time.
358 FfsFileEntry
= FvDevice
->CurrentFfsFile
;
360 if (FfsFileEntry
!= NULL
) {
361 FfsHeader
= (EFI_FFS_FILE_HEADER
*) FfsFileEntry
->FfsHeader
;
364 if ((FfsFileEntry
== NULL
) || (!CompareGuid (&FfsHeader
->Name
, NameGuid
))) {
366 // If not match or no file cached, search this file
371 Status
= This
->GetNextFile (
379 if (EFI_ERROR (Status
)) {
380 return EFI_NOT_FOUND
;
382 } while (!CompareGuid (&SearchNameGuid
, NameGuid
));
387 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*) Key
;
392 FvDevice
->CurrentFfsFile
= FfsFileEntry
;
394 FfsHeader
= (EFI_FFS_FILE_HEADER
*) FfsFileEntry
->FfsHeader
;
398 // Get File Size of the cached file
400 if (IS_FFS_FILE2 (FfsHeader
)) {
401 FileSize
= FFS_FILE2_SIZE (FfsHeader
) - sizeof (EFI_FFS_FILE_HEADER2
);
403 FileSize
= FFS_FILE_SIZE (FfsHeader
) - sizeof (EFI_FFS_FILE_HEADER
);
409 *FoundType
= FfsHeader
->Type
;
410 *FileAttributes
= FfsAttributes2FvFileAttributes (FfsHeader
->Attributes
);
411 if ((FvDevice
->FwVolHeader
->Attributes
& EFI_FVB2_MEMORY_MAPPED
) == EFI_FVB2_MEMORY_MAPPED
) {
412 *FileAttributes
|= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED
;
415 // Inherit the authentication status.
417 *AuthenticationStatus
= FvDevice
->AuthenticationStatus
;
420 // If Buffer is NULL, we only want to get some information
422 if (Buffer
== NULL
) {
423 *BufferSize
= FileSize
;
427 if (IS_FFS_FILE2 (FfsHeader
)) {
428 SrcPtr
= ((UINT8
*) FfsHeader
) + sizeof (EFI_FFS_FILE_HEADER2
);
430 SrcPtr
= ((UINT8
*) FfsHeader
) + sizeof (EFI_FFS_FILE_HEADER
);
433 if (CompareGuid (&gEfiFirmwareVolumeTopFileGuid
, NameGuid
)) {
435 // specially deal with VTF file
439 while (FileSize
>= 4) {
440 Tmp
= *(UINT32
*) SrcPtr
;
450 // If we drop out of the above loop, we've found the correct file header...
452 if (*Buffer
== NULL
) {
453 FileBuffer
= AllocateCopyPool (FileSize
, SrcPtr
);
454 if (FileBuffer
== NULL
) {
455 return EFI_OUT_OF_RESOURCES
;
458 *BufferSize
= FileSize
;
459 *Buffer
= FileBuffer
;
464 // If the user's buffer is smaller than the file size, then copy as much
465 // as we can and return an appropriate status.
467 if (FileSize
> *BufferSize
) {
468 CopyMem (*Buffer
, SrcPtr
, *BufferSize
);
469 *BufferSize
= FileSize
;
470 return EFI_WARN_BUFFER_TOO_SMALL
;
473 // User's buffer size is ok, so copy the entire file to their buffer.
475 *BufferSize
= FileSize
;
476 CopyMem (*Buffer
, SrcPtr
, *BufferSize
);
482 Locates a section in a given FFS File and
483 copies it to the supplied buffer (not including section header).
485 @param This Indicates the calling context.
486 @param NameGuid Pointer to an EFI_GUID, which is the
488 @param SectionType Indicates the section type to return.
489 @param SectionInstance Indicates which instance of sections with a
490 type of SectionType to return.
491 @param Buffer Buffer is a pointer to pointer to a buffer
492 in which the file or section contents or are
494 @param BufferSize BufferSize is a pointer to caller allocated
496 @param AuthenticationStatus AuthenticationStatus is a pointer to a
497 caller allocated UINT32 in which the
498 authentication status is returned.
500 @retval EFI_SUCCESS Successfully read the file section into
502 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.
503 @retval EFI_NOT_FOUND Section not found.
504 @retval EFI_DEVICE_ERROR Device error.
505 @retval EFI_ACCESS_DENIED Could not read.
506 @retval EFI_INVALID_PARAMETER Invalid parameter.
512 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
513 IN CONST EFI_GUID
*NameGuid
,
514 IN EFI_SECTION_TYPE SectionType
,
515 IN UINTN SectionInstance
,
516 IN OUT VOID
**Buffer
,
517 IN OUT UINTN
*BufferSize
,
518 OUT UINT32
*AuthenticationStatus
523 EFI_FV_ATTRIBUTES FvAttributes
;
524 EFI_FV_FILETYPE FileType
;
525 EFI_FV_FILE_ATTRIBUTES FileAttributes
;
528 EFI_SECTION_EXTRACTION_PROTOCOL
*Sep
;
531 if (NULL
== This
|| NULL
== NameGuid
|| Buffer
== NULL
) {
532 return EFI_INVALID_PARAMETER
;
535 FvDevice
= FV_DEVICE_FROM_THIS (This
);
537 Status
= This
->GetVolumeAttributes (This
, &FvAttributes
);
538 if (EFI_ERROR (Status
)) {
542 // First check to see that FV is enabled for reads...
544 if (0 == (FvAttributes
& EFI_FV2_READ_STATUS
)) {
545 return EFI_ACCESS_DENIED
;
548 // Read the whole file into buffer
551 Status
= This
->ReadFile (
554 (VOID
**) &FileBuffer
,
561 if (EFI_ERROR (Status
)) {
565 // Check to see that the file actually HAS sections before we go any further.
567 if (FileType
== EFI_FV_FILETYPE_RAW
) {
568 FreePool (FileBuffer
);
569 return EFI_NOT_FOUND
;
572 // Located the protocol
574 Status
= gBS
->LocateProtocol (
575 &gEfiSectionExtractionProtocolGuid
,
579 if (EFI_ERROR (Status
)) {
580 FreePool (FileBuffer
);
584 Status
= Sep
->OpenSectionStream (
591 if (EFI_ERROR (Status
)) {
592 FreePool (FileBuffer
);
596 if (SectionType
== 0) {
598 // We need the whole section stream
600 Status
= Sep
->GetSection (
611 Status
= Sep
->GetSection (
623 if (!EFI_ERROR (Status
)) {
625 // Inherit the authentication status.
627 *AuthenticationStatus
|= FvDevice
->AuthenticationStatus
;
631 // Handle AuthenticationStatus if necessary
633 Sep
->CloseSectionStream (Sep
, StreamHandle
);
635 FreePool (FileBuffer
);