2 Implements functions to read firmware file
4 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "FwVolDriver.h"
19 Required Alignment Alignment Value in FFS FFS_ATTRIB_DATA_ALIGNMENT2 Alignment Value in
20 (bytes) Attributes Field in FFS Attributes Field Firmware Volume Interfaces
38 UINT8 mFvAttributes
[] = {0, 4, 7, 9, 10, 12, 15, 16};
39 UINT8 mFvAttributes2
[] = {17, 18, 19, 20, 21, 22, 23, 24};
42 Convert the FFS File Attributes to FV File Attributes
44 @param FfsAttributes The attributes of UINT8 type.
46 @return The attributes of EFI_FV_FILE_ATTRIBUTES
49 EFI_FV_FILE_ATTRIBUTES
50 FfsAttributes2FvFileAttributes (
51 IN EFI_FFS_FILE_ATTRIBUTES FfsAttributes
55 EFI_FV_FILE_ATTRIBUTES FileAttribute
;
57 DataAlignment
= (UINT8
) ((FfsAttributes
& FFS_ATTRIB_DATA_ALIGNMENT
) >> 3);
58 ASSERT (DataAlignment
< 8);
60 if ((FfsAttributes
& FFS_ATTRIB_DATA_ALIGNMENT_2
) != 0) {
61 FileAttribute
= (EFI_FV_FILE_ATTRIBUTES
) mFvAttributes2
[DataAlignment
];
63 FileAttribute
= (EFI_FV_FILE_ATTRIBUTES
) mFvAttributes
[DataAlignment
];
66 if ((FfsAttributes
& FFS_ATTRIB_FIXED
) == FFS_ATTRIB_FIXED
) {
67 FileAttribute
|= EFI_FV_FILE_ATTRIB_FIXED
;
74 Given the input key, search for the next matching file in the volume.
76 @param This Indicates the calling context.
77 @param Key Key is a pointer to a caller allocated
78 buffer that contains implementation specific
79 data that is used to track where to begin
80 the search for the next file. The size of
81 the buffer must be at least This->KeySize
82 bytes long. To reinitialize the search and
83 begin from the beginning of the firmware
84 volume, the entire buffer must be cleared to
85 zero. Other than clearing the buffer to
86 initiate a new search, the caller must not
87 modify the data in the buffer between calls
89 @param FileType FileType is a pointer to a caller allocated
90 EFI_FV_FILETYPE. The GetNextFile() API can
91 filter it's search for files based on the
92 value of *FileType input. A *FileType input
93 of 0 causes GetNextFile() to search for
94 files of all types. If a file is found, the
95 file's type is returned in *FileType.
96 *FileType is not modified if no file is
98 @param NameGuid NameGuid is a pointer to a caller allocated
99 EFI_GUID. If a file is found, the file's
100 name is returned in *NameGuid. *NameGuid is
101 not modified if no file is found.
102 @param Attributes Attributes is a pointer to a caller
103 allocated EFI_FV_FILE_ATTRIBUTES. If a file
104 is found, the file's attributes are returned
105 in *Attributes. *Attributes is not modified
107 @param Size Size is a pointer to a caller allocated
108 UINTN. If a file is found, the file's size
109 is returned in *Size. *Size is not modified
112 @retval EFI_SUCCESS Successfully find the file.
113 @retval EFI_DEVICE_ERROR Device error.
114 @retval EFI_ACCESS_DENIED Fv could not read.
115 @retval EFI_NOT_FOUND No matching file found.
116 @retval EFI_INVALID_PARAMETER Invalid parameter
122 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
124 IN OUT EFI_FV_FILETYPE
*FileType
,
125 OUT EFI_GUID
*NameGuid
,
126 OUT EFI_FV_FILE_ATTRIBUTES
*Attributes
,
132 EFI_FV_ATTRIBUTES FvAttributes
;
133 EFI_FFS_FILE_HEADER
*FfsFileHeader
;
136 FFS_FILE_LIST_ENTRY
*FfsFileEntry
;
138 FvDevice
= FV_DEVICE_FROM_THIS (This
);
140 Status
= FvGetVolumeAttributes (This
, &FvAttributes
);
141 if (EFI_ERROR (Status
)){
146 // Check if read operation is enabled
148 if ((FvAttributes
& EFI_FV2_READ_STATUS
) == 0) {
149 return EFI_ACCESS_DENIED
;
152 if (*FileType
> EFI_FV_FILETYPE_SMM_CORE
) {
154 // File type needs to be in 0 - 0x0D
156 return EFI_NOT_FOUND
;
159 KeyValue
= (UINTN
*)Key
;
161 if (*KeyValue
== 0) {
163 // Search for 1st matching file
165 Link
= &FvDevice
->FfsFileListHeader
;
168 // Key is pointer to FFsFileEntry, so get next one
170 Link
= (LIST_ENTRY
*)(*KeyValue
);
173 if (Link
->ForwardLink
== &FvDevice
->FfsFileListHeader
) {
175 // Next is end of list so we did not find data
177 return EFI_NOT_FOUND
;
180 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*)Link
->ForwardLink
;
181 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)FfsFileEntry
->FfsHeader
;
186 *KeyValue
= (UINTN
)FfsFileEntry
;
188 if (FfsFileHeader
->Type
== EFI_FV_FILETYPE_FFS_PAD
) {
190 // we ignore pad files
195 if (*FileType
== EFI_FV_FILETYPE_ALL
) {
197 // Process all file types so we have a match
202 if (*FileType
== FfsFileHeader
->Type
) {
204 // Found a matching file type
212 // Return FileType, NameGuid, and Attributes
214 *FileType
= FfsFileHeader
->Type
;
215 CopyGuid (NameGuid
, &FfsFileHeader
->Name
);
216 *Attributes
= FfsAttributes2FvFileAttributes (FfsFileHeader
->Attributes
);
217 if ((FvDevice
->FwVolHeader
->Attributes
& EFI_FVB2_MEMORY_MAPPED
) == EFI_FVB2_MEMORY_MAPPED
) {
218 *Attributes
|= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED
;
222 // we need to substract the header size
224 if (IS_FFS_FILE2 (FfsFileHeader
)) {
225 *Size
= FFS_FILE2_SIZE (FfsFileHeader
) - sizeof (EFI_FFS_FILE_HEADER2
);
227 *Size
= FFS_FILE_SIZE (FfsFileHeader
) - sizeof (EFI_FFS_FILE_HEADER
);
236 Locates a file in the firmware volume and
237 copies it to the supplied buffer.
239 @param This Indicates the calling context.
240 @param NameGuid Pointer to an EFI_GUID, which is the
242 @param Buffer Buffer is a pointer to pointer to a buffer
243 in which the file or section contents or are
245 @param BufferSize BufferSize is a pointer to caller allocated
246 UINTN. On input *BufferSize indicates the
247 size in bytes of the memory region pointed
248 to by Buffer. On output, *BufferSize
249 contains the number of bytes required to
251 @param FoundType FoundType is a pointer to a caller allocated
252 EFI_FV_FILETYPE that on successful return
253 from Read() contains the type of file read.
254 This output reflects the file type
255 irrespective of the value of the SectionType
257 @param FileAttributes FileAttributes is a pointer to a caller
258 allocated EFI_FV_FILE_ATTRIBUTES. On
259 successful return from Read(),
260 *FileAttributes contains the attributes of
262 @param AuthenticationStatus AuthenticationStatus is a pointer to a
263 caller allocated UINTN in which the
264 authentication status is returned.
266 @retval EFI_SUCCESS Successfully read to memory buffer.
267 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.
268 @retval EFI_NOT_FOUND Not found.
269 @retval EFI_DEVICE_ERROR Device error.
270 @retval EFI_ACCESS_DENIED Could not read.
271 @retval EFI_INVALID_PARAMETER Invalid parameter.
272 @retval EFI_OUT_OF_RESOURCES Not enough buffer to be allocated.
278 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
279 IN CONST EFI_GUID
*NameGuid
,
280 IN OUT VOID
**Buffer
,
281 IN OUT UINTN
*BufferSize
,
282 OUT EFI_FV_FILETYPE
*FoundType
,
283 OUT EFI_FV_FILE_ATTRIBUTES
*FileAttributes
,
284 OUT UINT32
*AuthenticationStatus
289 EFI_GUID SearchNameGuid
;
290 EFI_FV_FILETYPE LocalFoundType
;
291 EFI_FV_FILE_ATTRIBUTES LocalAttributes
;
294 EFI_FFS_FILE_HEADER
*FfsHeader
;
295 UINTN InputBufferSize
;
298 if (NameGuid
== NULL
) {
299 return EFI_INVALID_PARAMETER
;
302 FvDevice
= FV_DEVICE_FROM_THIS (This
);
306 // Keep looking until we find the matching NameGuid.
307 // The Key is really a FfsFileEntry
309 FvDevice
->LastKey
= 0;
312 Status
= FvGetNextFile (
320 if (EFI_ERROR (Status
)) {
321 return EFI_NOT_FOUND
;
323 } while (!CompareGuid (&SearchNameGuid
, NameGuid
));
326 // Get a pointer to the header
328 FfsHeader
= FvDevice
->LastKey
->FfsHeader
;
329 if (FvDevice
->IsMemoryMapped
) {
331 // Memory mapped FV has not been cached, so here is to cache by file.
333 if (!FvDevice
->LastKey
->FileCached
) {
335 // Cache FFS file to memory buffer.
337 WholeFileSize
= IS_FFS_FILE2 (FfsHeader
) ? FFS_FILE2_SIZE (FfsHeader
): FFS_FILE_SIZE (FfsHeader
);
338 FfsHeader
= AllocateCopyPool (WholeFileSize
, FfsHeader
);
339 if (FfsHeader
== NULL
) {
340 return EFI_OUT_OF_RESOURCES
;
343 // Let FfsHeader in FfsFileEntry point to the cached file buffer.
345 FvDevice
->LastKey
->FfsHeader
= FfsHeader
;
346 FvDevice
->LastKey
->FileCached
= TRUE
;
351 // Remember callers buffer size
353 InputBufferSize
= *BufferSize
;
356 // Calculate return values
358 *FoundType
= FfsHeader
->Type
;
359 *FileAttributes
= FfsAttributes2FvFileAttributes (FfsHeader
->Attributes
);
360 if ((FvDevice
->FwVolHeader
->Attributes
& EFI_FVB2_MEMORY_MAPPED
) == EFI_FVB2_MEMORY_MAPPED
) {
361 *FileAttributes
|= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED
;
364 // Inherit the authentication status.
366 *AuthenticationStatus
= FvDevice
->AuthenticationStatus
;
367 *BufferSize
= FileSize
;
369 if (Buffer
== NULL
) {
371 // If Buffer is NULL, we only want to get the information collected so far
377 // Skip over file header
379 if (IS_FFS_FILE2 (FfsHeader
)) {
380 SrcPtr
= ((UINT8
*) FfsHeader
) + sizeof (EFI_FFS_FILE_HEADER2
);
382 SrcPtr
= ((UINT8
*) FfsHeader
) + sizeof (EFI_FFS_FILE_HEADER
);
385 Status
= EFI_SUCCESS
;
386 if (*Buffer
== NULL
) {
388 // Caller passed in a pointer so allocate buffer for them
390 *Buffer
= AllocatePool (FileSize
);
391 if (*Buffer
== NULL
) {
392 return EFI_OUT_OF_RESOURCES
;
394 } else if (FileSize
> InputBufferSize
) {
396 // Callers buffer was not big enough
398 Status
= EFI_WARN_BUFFER_TOO_SMALL
;
399 FileSize
= InputBufferSize
;
403 // Copy data into callers buffer
405 CopyMem (*Buffer
, SrcPtr
, FileSize
);
413 Locates a section in a given FFS File and
414 copies it to the supplied buffer (not including section header).
416 @param This Indicates the calling context.
417 @param NameGuid Pointer to an EFI_GUID, which is the
419 @param SectionType Indicates the section type to return.
420 @param SectionInstance Indicates which instance of sections with a
421 type of SectionType to return.
422 @param Buffer Buffer is a pointer to pointer to a buffer
423 in which the file or section contents or are
425 @param BufferSize BufferSize is a pointer to caller allocated
427 @param AuthenticationStatus AuthenticationStatus is a pointer to a
428 caller allocated UINT32 in which the
429 authentication status is returned.
431 @retval EFI_SUCCESS Successfully read the file section into
433 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.
434 @retval EFI_NOT_FOUND Section not found.
435 @retval EFI_DEVICE_ERROR Device error.
436 @retval EFI_ACCESS_DENIED Could not read.
437 @retval EFI_INVALID_PARAMETER Invalid parameter.
443 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
444 IN CONST EFI_GUID
*NameGuid
,
445 IN EFI_SECTION_TYPE SectionType
,
446 IN UINTN SectionInstance
,
447 IN OUT VOID
**Buffer
,
448 IN OUT UINTN
*BufferSize
,
449 OUT UINT32
*AuthenticationStatus
454 EFI_FV_FILETYPE FileType
;
455 EFI_FV_FILE_ATTRIBUTES FileAttributes
;
458 FFS_FILE_LIST_ENTRY
*FfsEntry
;
460 if (NameGuid
== NULL
|| Buffer
== NULL
) {
461 return EFI_INVALID_PARAMETER
;
464 FvDevice
= FV_DEVICE_FROM_THIS (This
);
469 Status
= FvReadFile (
479 // Get the last key used by our call to FvReadFile as it is the FfsEntry for this file.
481 FfsEntry
= (FFS_FILE_LIST_ENTRY
*) FvDevice
->LastKey
;
483 if (EFI_ERROR (Status
)) {
486 if (IS_FFS_FILE2 (FfsEntry
->FfsHeader
)) {
487 FileBuffer
= ((UINT8
*) FfsEntry
->FfsHeader
) + sizeof (EFI_FFS_FILE_HEADER2
);
489 FileBuffer
= ((UINT8
*) FfsEntry
->FfsHeader
) + sizeof (EFI_FFS_FILE_HEADER
);
492 // Check to see that the file actually HAS sections before we go any further.
494 if (FileType
== EFI_FV_FILETYPE_RAW
) {
495 Status
= EFI_NOT_FOUND
;
500 // Use FfsEntry to cache Section Extraction Protocol Information
502 if (FfsEntry
->StreamHandle
== 0) {
503 Status
= OpenSectionStream (
506 &FfsEntry
->StreamHandle
508 if (EFI_ERROR (Status
)) {
514 // If SectionType == 0 We need the whole section stream
516 Status
= GetSection (
517 FfsEntry
->StreamHandle
,
518 (SectionType
== 0) ? NULL
: &SectionType
,
520 (SectionType
== 0) ? 0 : SectionInstance
,
523 AuthenticationStatus
,
527 if (!EFI_ERROR (Status
)) {
529 // Inherit the authentication status.
531 *AuthenticationStatus
|= FvDevice
->AuthenticationStatus
;
535 // Close of stream defered to close of FfsHeader list to allow SEP to cache data