2 Implements functions to read firmware file
4 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
5 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
= FvGetVolumeAttributes (This
, &FvAttributes
);
135 if (EFI_ERROR (Status
)){
140 // Check if read operation is enabled
142 if ((FvAttributes
& EFI_FV2_READ_STATUS
) == 0) {
143 return EFI_ACCESS_DENIED
;
146 if (*FileType
> EFI_FV_FILETYPE_SMM_CORE
) {
148 // File type needs to be in 0 - 0x0D
150 return EFI_NOT_FOUND
;
153 KeyValue
= (UINTN
*)Key
;
155 if (*KeyValue
== 0) {
157 // Search for 1st matching file
159 Link
= &FvDevice
->FfsFileListHeader
;
162 // Key is pointer to FFsFileEntry, so get next one
164 Link
= (LIST_ENTRY
*)(*KeyValue
);
167 if (Link
->ForwardLink
== &FvDevice
->FfsFileListHeader
) {
169 // Next is end of list so we did not find data
171 return EFI_NOT_FOUND
;
174 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*)Link
->ForwardLink
;
175 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)FfsFileEntry
->FfsHeader
;
180 *KeyValue
= (UINTN
)FfsFileEntry
;
182 if (FfsFileHeader
->Type
== EFI_FV_FILETYPE_FFS_PAD
) {
184 // we ignore pad files
189 if (*FileType
== EFI_FV_FILETYPE_ALL
) {
191 // Process all file types so we have a match
196 if (*FileType
== FfsFileHeader
->Type
) {
198 // Found a matching file type
206 // Return FileType, NameGuid, and Attributes
208 *FileType
= FfsFileHeader
->Type
;
209 CopyGuid (NameGuid
, &FfsFileHeader
->Name
);
210 *Attributes
= FfsAttributes2FvFileAttributes (FfsFileHeader
->Attributes
);
211 if ((FvDevice
->FwVolHeader
->Attributes
& EFI_FVB2_MEMORY_MAPPED
) == EFI_FVB2_MEMORY_MAPPED
) {
212 *Attributes
|= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED
;
216 // we need to substract the header size
218 if (IS_FFS_FILE2 (FfsFileHeader
)) {
219 *Size
= FFS_FILE2_SIZE (FfsFileHeader
) - sizeof (EFI_FFS_FILE_HEADER2
);
221 *Size
= FFS_FILE_SIZE (FfsFileHeader
) - sizeof (EFI_FFS_FILE_HEADER
);
230 Locates a file in the firmware volume and
231 copies it to the supplied buffer.
233 @param This Indicates the calling context.
234 @param NameGuid Pointer to an EFI_GUID, which is the
236 @param Buffer Buffer is a pointer to pointer to a buffer
237 in which the file or section contents or are
239 @param BufferSize BufferSize is a pointer to caller allocated
240 UINTN. On input *BufferSize indicates the
241 size in bytes of the memory region pointed
242 to by Buffer. On output, *BufferSize
243 contains the number of bytes required to
245 @param FoundType FoundType is a pointer to a caller allocated
246 EFI_FV_FILETYPE that on successful return
247 from Read() contains the type of file read.
248 This output reflects the file type
249 irrespective of the value of the SectionType
251 @param FileAttributes FileAttributes is a pointer to a caller
252 allocated EFI_FV_FILE_ATTRIBUTES. On
253 successful return from Read(),
254 *FileAttributes contains the attributes of
256 @param AuthenticationStatus AuthenticationStatus is a pointer to a
257 caller allocated UINTN in which the
258 authentication status is returned.
260 @retval EFI_SUCCESS Successfully read to memory buffer.
261 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.
262 @retval EFI_NOT_FOUND Not found.
263 @retval EFI_DEVICE_ERROR Device error.
264 @retval EFI_ACCESS_DENIED Could not read.
265 @retval EFI_INVALID_PARAMETER Invalid parameter.
266 @retval EFI_OUT_OF_RESOURCES Not enough buffer to be allocated.
272 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
273 IN CONST EFI_GUID
*NameGuid
,
274 IN OUT VOID
**Buffer
,
275 IN OUT UINTN
*BufferSize
,
276 OUT EFI_FV_FILETYPE
*FoundType
,
277 OUT EFI_FV_FILE_ATTRIBUTES
*FileAttributes
,
278 OUT UINT32
*AuthenticationStatus
283 EFI_GUID SearchNameGuid
;
284 EFI_FV_FILETYPE LocalFoundType
;
285 EFI_FV_FILE_ATTRIBUTES LocalAttributes
;
288 EFI_FFS_FILE_HEADER
*FfsHeader
;
289 UINTN InputBufferSize
;
292 if (NameGuid
== NULL
) {
293 return EFI_INVALID_PARAMETER
;
296 FvDevice
= FV_DEVICE_FROM_THIS (This
);
300 // Keep looking until we find the matching NameGuid.
301 // The Key is really a FfsFileEntry
303 FvDevice
->LastKey
= 0;
306 Status
= FvGetNextFile (
314 if (EFI_ERROR (Status
)) {
315 return EFI_NOT_FOUND
;
317 } while (!CompareGuid (&SearchNameGuid
, NameGuid
));
320 // Get a pointer to the header
322 FfsHeader
= FvDevice
->LastKey
->FfsHeader
;
323 if (FvDevice
->IsMemoryMapped
) {
325 // Memory mapped FV has not been cached, so here is to cache by file.
327 if (!FvDevice
->LastKey
->FileCached
) {
329 // Cache FFS file to memory buffer.
331 WholeFileSize
= IS_FFS_FILE2 (FfsHeader
) ? FFS_FILE2_SIZE (FfsHeader
): FFS_FILE_SIZE (FfsHeader
);
332 FfsHeader
= AllocateCopyPool (WholeFileSize
, FfsHeader
);
333 if (FfsHeader
== NULL
) {
334 return EFI_OUT_OF_RESOURCES
;
337 // Let FfsHeader in FfsFileEntry point to the cached file buffer.
339 FvDevice
->LastKey
->FfsHeader
= FfsHeader
;
340 FvDevice
->LastKey
->FileCached
= TRUE
;
345 // Remember callers buffer size
347 InputBufferSize
= *BufferSize
;
350 // Calculate return values
352 *FoundType
= FfsHeader
->Type
;
353 *FileAttributes
= FfsAttributes2FvFileAttributes (FfsHeader
->Attributes
);
354 if ((FvDevice
->FwVolHeader
->Attributes
& EFI_FVB2_MEMORY_MAPPED
) == EFI_FVB2_MEMORY_MAPPED
) {
355 *FileAttributes
|= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED
;
358 // Inherit the authentication status.
360 *AuthenticationStatus
= FvDevice
->AuthenticationStatus
;
361 *BufferSize
= FileSize
;
363 if (Buffer
== NULL
) {
365 // If Buffer is NULL, we only want to get the information collected so far
371 // Skip over file header
373 if (IS_FFS_FILE2 (FfsHeader
)) {
374 SrcPtr
= ((UINT8
*) FfsHeader
) + sizeof (EFI_FFS_FILE_HEADER2
);
376 SrcPtr
= ((UINT8
*) FfsHeader
) + sizeof (EFI_FFS_FILE_HEADER
);
379 Status
= EFI_SUCCESS
;
380 if (*Buffer
== NULL
) {
382 // Caller passed in a pointer so allocate buffer for them
384 *Buffer
= AllocatePool (FileSize
);
385 if (*Buffer
== NULL
) {
386 return EFI_OUT_OF_RESOURCES
;
388 } else if (FileSize
> InputBufferSize
) {
390 // Callers buffer was not big enough
392 Status
= EFI_WARN_BUFFER_TOO_SMALL
;
393 FileSize
= InputBufferSize
;
397 // Copy data into callers buffer
399 CopyMem (*Buffer
, SrcPtr
, FileSize
);
407 Locates a section in a given FFS File and
408 copies it to the supplied buffer (not including section header).
410 @param This Indicates the calling context.
411 @param NameGuid Pointer to an EFI_GUID, which is the
413 @param SectionType Indicates the section type to return.
414 @param SectionInstance Indicates which instance of sections with a
415 type of SectionType to return.
416 @param Buffer Buffer is a pointer to pointer to a buffer
417 in which the file or section contents or are
419 @param BufferSize BufferSize is a pointer to caller allocated
421 @param AuthenticationStatus AuthenticationStatus is a pointer to a
422 caller allocated UINT32 in which the
423 authentication status is returned.
425 @retval EFI_SUCCESS Successfully read the file section into
427 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.
428 @retval EFI_NOT_FOUND Section not found.
429 @retval EFI_DEVICE_ERROR Device error.
430 @retval EFI_ACCESS_DENIED Could not read.
431 @retval EFI_INVALID_PARAMETER Invalid parameter.
437 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
438 IN CONST EFI_GUID
*NameGuid
,
439 IN EFI_SECTION_TYPE SectionType
,
440 IN UINTN SectionInstance
,
441 IN OUT VOID
**Buffer
,
442 IN OUT UINTN
*BufferSize
,
443 OUT UINT32
*AuthenticationStatus
448 EFI_FV_FILETYPE FileType
;
449 EFI_FV_FILE_ATTRIBUTES FileAttributes
;
452 FFS_FILE_LIST_ENTRY
*FfsEntry
;
454 if (NameGuid
== NULL
|| Buffer
== NULL
) {
455 return EFI_INVALID_PARAMETER
;
458 FvDevice
= FV_DEVICE_FROM_THIS (This
);
463 Status
= FvReadFile (
473 // Get the last key used by our call to FvReadFile as it is the FfsEntry for this file.
475 FfsEntry
= (FFS_FILE_LIST_ENTRY
*) FvDevice
->LastKey
;
477 if (EFI_ERROR (Status
)) {
480 if (IS_FFS_FILE2 (FfsEntry
->FfsHeader
)) {
481 FileBuffer
= ((UINT8
*) FfsEntry
->FfsHeader
) + sizeof (EFI_FFS_FILE_HEADER2
);
483 FileBuffer
= ((UINT8
*) FfsEntry
->FfsHeader
) + sizeof (EFI_FFS_FILE_HEADER
);
486 // Check to see that the file actually HAS sections before we go any further.
488 if (FileType
== EFI_FV_FILETYPE_RAW
) {
489 Status
= EFI_NOT_FOUND
;
494 // Use FfsEntry to cache Section Extraction Protocol Information
496 if (FfsEntry
->StreamHandle
== 0) {
497 Status
= OpenSectionStream (
500 &FfsEntry
->StreamHandle
502 if (EFI_ERROR (Status
)) {
508 // If SectionType == 0 We need the whole section stream
510 Status
= GetSection (
511 FfsEntry
->StreamHandle
,
512 (SectionType
== 0) ? NULL
: &SectionType
,
514 (SectionType
== 0) ? 0 : SectionInstance
,
517 AuthenticationStatus
,
521 if (!EFI_ERROR (Status
)) {
523 // Inherit the authentication status.
525 *AuthenticationStatus
|= FvDevice
->AuthenticationStatus
;
529 // Close of stream defered to close of FfsHeader list to allow SEP to cache data