2 Implements functions to read firmware file.
4 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions
8 of the BSD License which accompanies this distribution. The
9 full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "FwVolDriver.h"
20 Required Alignment Alignment Value in FFS Alignment Value in
21 (bytes) Attributes Field Firmware Volume Interfaces
31 UINT8 mFvAttributes
[] = {0, 4, 7, 9, 10, 12, 15, 16};
34 Convert the FFS File Attributes to FV File Attributes.
36 @param FfsAttributes The attributes of UINT8 type.
38 @return The attributes of EFI_FV_FILE_ATTRIBUTES
41 EFI_FV_FILE_ATTRIBUTES
42 FfsAttributes2FvFileAttributes (
43 IN EFI_FFS_FILE_ATTRIBUTES FfsAttributes
47 EFI_FV_FILE_ATTRIBUTES FileAttribute
;
49 DataAlignment
= (UINT8
) ((FfsAttributes
& FFS_ATTRIB_DATA_ALIGNMENT
) >> 3);
50 ASSERT (DataAlignment
< 8);
52 FileAttribute
= (EFI_FV_FILE_ATTRIBUTES
) mFvAttributes
[DataAlignment
];
54 if ((FfsAttributes
& FFS_ATTRIB_FIXED
) == FFS_ATTRIB_FIXED
) {
55 FileAttribute
|= EFI_FV_FILE_ATTRIB_FIXED
;
62 Given the input key, search for the next matching file in the volume.
64 @param This Indicates the calling context.
65 @param Key Key is a pointer to a caller allocated
66 buffer that contains implementation specific
67 data that is used to track where to begin
68 the search for the next file. The size of
69 the buffer must be at least This->KeySize
70 bytes long. To reinitialize the search and
71 begin from the beginning of the firmware
72 volume, the entire buffer must be cleared to
73 zero. Other than clearing the buffer to
74 initiate a new search, the caller must not
75 modify the data in the buffer between calls
77 @param FileType FileType is a pointer to a caller allocated
78 EFI_FV_FILETYPE. The GetNextFile() API can
79 filter it's search for files based on the
80 value of *FileType input. A *FileType input
81 of 0 causes GetNextFile() to search for
82 files of all types. If a file is found, the
83 file's type is returned in *FileType.
84 *FileType is not modified if no file is
86 @param NameGuid NameGuid is a pointer to a caller allocated
87 EFI_GUID. If a file is found, the file's
88 name is returned in *NameGuid. *NameGuid is
89 not modified if no file is found.
90 @param Attributes Attributes is a pointer to a caller
91 allocated EFI_FV_FILE_ATTRIBUTES. If a file
92 is found, the file's attributes are returned
93 in *Attributes. *Attributes is not modified
95 @param Size Size is a pointer to a caller allocated
96 UINTN. If a file is found, the file's size
97 is returned in *Size. *Size is not modified
100 @retval EFI_SUCCESS Successfully find the file.
101 @retval EFI_DEVICE_ERROR Device error.
102 @retval EFI_ACCESS_DENIED Fv could not read.
103 @retval EFI_NOT_FOUND No matching file found.
104 @retval EFI_INVALID_PARAMETER Invalid parameter
110 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
112 IN OUT EFI_FV_FILETYPE
*FileType
,
113 OUT EFI_GUID
*NameGuid
,
114 OUT EFI_FV_FILE_ATTRIBUTES
*Attributes
,
120 EFI_FV_ATTRIBUTES FvAttributes
;
121 EFI_FFS_FILE_HEADER
*FfsFileHeader
;
124 FFS_FILE_LIST_ENTRY
*FfsFileEntry
;
126 FvDevice
= FV_DEVICE_FROM_THIS (This
);
128 Status
= This
->GetVolumeAttributes (This
, &FvAttributes
);
129 if (EFI_ERROR (Status
)) {
133 KeyValue
= (UINTN
*) Key
;
134 FfsFileHeader
= NULL
;
137 // Check if read operation is enabled
139 if ((FvAttributes
& EFI_FV2_READ_STATUS
) == 0) {
140 return EFI_ACCESS_DENIED
;
143 if (*FileType
> EFI_FV_FILETYPE_SMM_CORE
) {
145 // File type needs to be in 0 - 0x0D
147 return EFI_NOT_FOUND
;
151 if (*KeyValue
== 0) {
153 // Search for 1st matching file
155 Link
= &FvDevice
->FfsFileListHeader
;
156 if (Link
->ForwardLink
== &FvDevice
->FfsFileListHeader
) {
157 return EFI_NOT_FOUND
;
160 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*) Link
->ForwardLink
;
161 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) FfsFileEntry
->FfsHeader
;
166 *KeyValue
= (UINTN
) FfsFileEntry
;
169 // we ignore pad files
171 if (FfsFileHeader
->Type
== EFI_FV_FILETYPE_FFS_PAD
) {
175 if (*FileType
== 0) {
179 if (*FileType
== FfsFileHeader
->Type
) {
185 // Getting link from last Ffs
187 Link
= (LIST_ENTRY
*) (*KeyValue
);
188 if (Link
->ForwardLink
== &FvDevice
->FfsFileListHeader
) {
189 return EFI_NOT_FOUND
;
192 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*) Link
->ForwardLink
;
193 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) FfsFileEntry
->FfsHeader
;
198 *KeyValue
= (UINTN
) FfsFileEntry
;
201 // we ignore pad files
203 if (FfsFileHeader
->Type
== EFI_FV_FILETYPE_FFS_PAD
) {
207 if (*FileType
== EFI_FV_FILETYPE_ALL
) {
211 if (*FileType
== FfsFileHeader
->Type
) {
215 } while (Link
->ForwardLink
!= &FvDevice
->FfsFileListHeader
);
218 // Cache this file entry
220 FvDevice
->CurrentFfsFile
= FfsFileEntry
;
222 *FileType
= FfsFileHeader
->Type
;
223 CopyGuid (NameGuid
, &FfsFileHeader
->Name
);
224 *Attributes
= FfsAttributes2FvFileAttributes (FfsFileHeader
->Attributes
);
225 if ((FvDevice
->FwVolHeader
->Attributes
& EFI_FVB2_MEMORY_MAPPED
) == EFI_FVB2_MEMORY_MAPPED
) {
226 *Attributes
|= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED
;
230 // we need to substract the header size
232 if (IS_FFS_FILE2 (FfsFileHeader
)) {
233 *Size
= FFS_FILE2_SIZE (FfsFileHeader
) - sizeof (EFI_FFS_FILE_HEADER2
);
235 *Size
= FFS_FILE_SIZE (FfsFileHeader
) - sizeof (EFI_FFS_FILE_HEADER
);
238 if (CompareGuid (&gEfiFirmwareVolumeTopFileGuid
, NameGuid
)) {
240 // specially deal with VTF file
245 if (IS_FFS_FILE2 (FfsFileHeader
)) {
246 SrcPtr
= ((UINT8
*) FfsFileHeader
) + sizeof (EFI_FFS_FILE_HEADER2
);
248 SrcPtr
= ((UINT8
*) FfsFileHeader
) + sizeof (EFI_FFS_FILE_HEADER
);
252 Tmp
= *(UINT32
*) SrcPtr
;
266 Locates a file in the firmware volume and
267 copies it to the supplied buffer.
269 @param This Indicates the calling context.
270 @param NameGuid Pointer to an EFI_GUID, which is the
272 @param Buffer Buffer is a pointer to pointer to a buffer
273 in which the file or section contents or are
275 @param BufferSize BufferSize is a pointer to caller allocated
276 UINTN. On input *BufferSize indicates the
277 size in bytes of the memory region pointed
278 to by Buffer. On output, *BufferSize
279 contains the number of bytes required to
281 @param FoundType FoundType is a pointer to a caller allocated
282 EFI_FV_FILETYPE that on successful return
283 from Read() contains the type of file read.
284 This output reflects the file type
285 irrespective of the value of the SectionType
287 @param FileAttributes FileAttributes is a pointer to a caller
288 allocated EFI_FV_FILE_ATTRIBUTES. On
289 successful return from Read(),
290 *FileAttributes contains the attributes of
292 @param AuthenticationStatus AuthenticationStatus is a pointer to a
293 caller allocated UINTN in which the
294 authentication status is returned.
296 @retval EFI_SUCCESS Successfully read to memory buffer.
297 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.
298 @retval EFI_NOT_FOUND Not found.
299 @retval EFI_DEVICE_ERROR Device error.
300 @retval EFI_ACCESS_DENIED Could not read.
301 @retval EFI_INVALID_PARAMETER Invalid parameter.
302 @retval EFI_OUT_OF_RESOURCES Not enough buffer to be allocated.
308 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
309 IN CONST EFI_GUID
*NameGuid
,
310 IN OUT VOID
**Buffer
,
311 IN OUT UINTN
*BufferSize
,
312 OUT EFI_FV_FILETYPE
*FoundType
,
313 OUT EFI_FV_FILE_ATTRIBUTES
*FileAttributes
,
314 OUT UINT32
*AuthenticationStatus
320 EFI_GUID SearchNameGuid
;
321 EFI_FV_ATTRIBUTES FvAttributes
;
322 EFI_FV_FILETYPE LocalFoundType
;
323 EFI_FV_FILE_ATTRIBUTES LocalAttributes
;
326 FFS_FILE_LIST_ENTRY
*FfsFileEntry
;
327 EFI_FFS_FILE_HEADER
*FfsHeader
;
330 if (NULL
== This
|| NULL
== NameGuid
) {
331 return EFI_INVALID_PARAMETER
;
334 FvDevice
= FV_DEVICE_FROM_THIS (This
);
336 Status
= This
->GetVolumeAttributes (This
, &FvAttributes
);
337 if (EFI_ERROR (Status
)) {
341 // First check to see that FV is enabled for reads...
343 if (0 == (FvAttributes
& EFI_FV2_READ_STATUS
)) {
344 return EFI_ACCESS_DENIED
;
350 // Check if the file was read last time.
352 FfsFileEntry
= FvDevice
->CurrentFfsFile
;
354 if (FfsFileEntry
!= NULL
) {
355 FfsHeader
= (EFI_FFS_FILE_HEADER
*) FfsFileEntry
->FfsHeader
;
358 if ((FfsFileEntry
== NULL
) || (!CompareGuid (&FfsHeader
->Name
, NameGuid
))) {
360 // If not match or no file cached, search this file
365 Status
= This
->GetNextFile (
373 if (EFI_ERROR (Status
)) {
374 return EFI_NOT_FOUND
;
376 } while (!CompareGuid (&SearchNameGuid
, NameGuid
));
381 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*) Key
;
386 FvDevice
->CurrentFfsFile
= FfsFileEntry
;
388 FfsHeader
= (EFI_FFS_FILE_HEADER
*) FfsFileEntry
->FfsHeader
;
392 // Get File Size of the cached file
394 if (IS_FFS_FILE2 (FfsHeader
)) {
395 FileSize
= FFS_FILE2_SIZE (FfsHeader
) - sizeof (EFI_FFS_FILE_HEADER2
);
397 FileSize
= FFS_FILE_SIZE (FfsHeader
) - sizeof (EFI_FFS_FILE_HEADER
);
403 *FoundType
= FfsHeader
->Type
;
404 *FileAttributes
= FfsAttributes2FvFileAttributes (FfsHeader
->Attributes
);
405 if ((FvDevice
->FwVolHeader
->Attributes
& EFI_FVB2_MEMORY_MAPPED
) == EFI_FVB2_MEMORY_MAPPED
) {
406 *FileAttributes
|= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED
;
408 *AuthenticationStatus
= 0;
411 // If Buffer is NULL, we only want to get some information
413 if (Buffer
== NULL
) {
414 *BufferSize
= FileSize
;
418 if (IS_FFS_FILE2 (FfsHeader
)) {
419 SrcPtr
= ((UINT8
*) FfsHeader
) + sizeof (EFI_FFS_FILE_HEADER2
);
421 SrcPtr
= ((UINT8
*) FfsHeader
) + sizeof (EFI_FFS_FILE_HEADER
);
424 if (CompareGuid (&gEfiFirmwareVolumeTopFileGuid
, NameGuid
)) {
426 // specially deal with VTF file
430 while (FileSize
>= 4) {
431 Tmp
= *(UINT32
*) SrcPtr
;
441 // If we drop out of the above loop, we've found the correct file header...
443 if (*Buffer
== NULL
) {
444 FileBuffer
= AllocateCopyPool (FileSize
, SrcPtr
);
445 if (FileBuffer
== NULL
) {
446 return EFI_OUT_OF_RESOURCES
;
449 *BufferSize
= FileSize
;
450 *Buffer
= FileBuffer
;
455 // If the user's buffer is smaller than the file size, then copy as much
456 // as we can and return an appropriate status.
458 if (FileSize
> *BufferSize
) {
459 CopyMem (*Buffer
, SrcPtr
, *BufferSize
);
460 *BufferSize
= FileSize
;
461 return EFI_WARN_BUFFER_TOO_SMALL
;
464 // User's buffer size is ok, so copy the entire file to their buffer.
466 *BufferSize
= FileSize
;
467 CopyMem (*Buffer
, SrcPtr
, *BufferSize
);
473 Locates a section in a given FFS File and
474 copies it to the supplied buffer (not including section header).
476 @param This Indicates the calling context.
477 @param NameGuid Pointer to an EFI_GUID, which is the
479 @param SectionType Indicates the section type to return.
480 @param SectionInstance Indicates which instance of sections with a
481 type of SectionType to return.
482 @param Buffer Buffer is a pointer to pointer to a buffer
483 in which the file or section contents or are
485 @param BufferSize BufferSize is a pointer to caller allocated
487 @param AuthenticationStatus AuthenticationStatus is a pointer to a
488 caller allocated UINT32 in which the
489 authentication status is returned.
491 @retval EFI_SUCCESS Successfully read the file section into
493 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.
494 @retval EFI_NOT_FOUND Section not found.
495 @retval EFI_DEVICE_ERROR Device error.
496 @retval EFI_ACCESS_DENIED Could not read.
497 @retval EFI_INVALID_PARAMETER Invalid parameter.
503 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
504 IN CONST EFI_GUID
*NameGuid
,
505 IN EFI_SECTION_TYPE SectionType
,
506 IN UINTN SectionInstance
,
507 IN OUT VOID
**Buffer
,
508 IN OUT UINTN
*BufferSize
,
509 OUT UINT32
*AuthenticationStatus
514 EFI_FV_ATTRIBUTES FvAttributes
;
515 EFI_FV_FILETYPE FileType
;
516 EFI_FV_FILE_ATTRIBUTES FileAttributes
;
519 EFI_SECTION_EXTRACTION_PROTOCOL
*Sep
;
522 if (NULL
== This
|| NULL
== NameGuid
|| Buffer
== NULL
) {
523 return EFI_INVALID_PARAMETER
;
526 FvDevice
= FV_DEVICE_FROM_THIS (This
);
528 Status
= This
->GetVolumeAttributes (This
, &FvAttributes
);
529 if (EFI_ERROR (Status
)) {
533 // First check to see that FV is enabled for reads...
535 if (0 == (FvAttributes
& EFI_FV2_READ_STATUS
)) {
536 return EFI_ACCESS_DENIED
;
539 // Read the whole file into buffer
542 Status
= This
->ReadFile (
545 (VOID
**) &FileBuffer
,
552 if (EFI_ERROR (Status
)) {
556 // Check to see that the file actually HAS sections before we go any further.
558 if (FileType
== EFI_FV_FILETYPE_RAW
) {
559 FreePool (FileBuffer
);
560 return EFI_NOT_FOUND
;
563 // Located the protocol
565 Status
= gBS
->LocateProtocol (
566 &gEfiSectionExtractionProtocolGuid
,
570 if (EFI_ERROR (Status
)) {
571 FreePool (FileBuffer
);
575 Status
= Sep
->OpenSectionStream (
582 if (EFI_ERROR (Status
)) {
583 FreePool (FileBuffer
);
587 if (SectionType
== 0) {
589 // We need the whole section stream
591 Status
= Sep
->GetSection (
602 Status
= Sep
->GetSection (
614 if (!EFI_ERROR (Status
)) {
616 // Inherit the authentication status.
618 *AuthenticationStatus
|= FvDevice
->AuthenticationStatus
;
622 // Handle AuthenticationStatus if necessary
624 Sep
->CloseSectionStream (Sep
, StreamHandle
);
626 FreePool (FileBuffer
);