3 Implements functions to read firmware file
5 Copyright (c) 2006 - 2008, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 Required Alignment Alignment Value in FFS Alignment Value in
21 (bytes) Attributes Field Firmware Volume Interfaces
36 UINT8 mFvAttributes
[] = {0, 4, 7, 9, 10, 12, 15, 16};
41 Convert the FFS File Attributes to FV File Attributes
43 @param FfsAttributes The attributes of UINT8 type.
45 @return The attributes of EFI_FV_FILE_ATTRIBUTES
49 EFI_FV_FILE_ATTRIBUTES
50 FfsAttributes2FvFileAttributes (
51 IN EFI_FFS_FILE_ATTRIBUTES FfsAttributes
54 FfsAttributes
= (EFI_FFS_FILE_ATTRIBUTES
)((FfsAttributes
& FFS_ATTRIB_DATA_ALIGNMENT
) >> 3);
55 ASSERT (FfsAttributes
< 8);
57 return (EFI_FV_FILE_ATTRIBUTES
) mFvAttributes
[FfsAttributes
];
63 Given the input key, search for the next matching file in the volume.
65 @param This Indicates the calling context.
66 @param Key Key is a pointer to a caller allocated
67 buffer that contains implementation specific
68 data that is used to track where to begin
69 the search for the next file. The size of
70 the buffer must be at least This->KeySize
71 bytes long. To reinitialize the search and
72 begin from the beginning of the firmware
73 volume, the entire buffer must be cleared to
74 zero. Other than clearing the buffer to
75 initiate a new search, the caller must not
76 modify the data in the buffer between calls
78 @param FileType FileType is a pointer to a caller allocated
79 EFI_FV_FILETYPE. The GetNextFile() API can
80 filter it's search for files based on the
81 value of *FileType input. A *FileType input
82 of 0 causes GetNextFile() to search for
83 files of all types. If a file is found, the
84 file's type is returned in *FileType.
85 *FileType is not modified if no file is
87 @param NameGuid NameGuid is a pointer to a caller allocated
88 EFI_GUID. If a file is found, the file's
89 name is returned in *NameGuid. *NameGuid is
90 not modified if no file is found.
91 @param Attributes Attributes is a pointer to a caller
92 allocated EFI_FV_FILE_ATTRIBUTES. If a file
93 is found, the file's attributes are returned
94 in *Attributes. *Attributes is not modified
96 @param Size Size is a pointer to a caller allocated
97 UINTN. If a file is found, the file's size
98 is returned in *Size. *Size is not modified
101 @retval EFI_SUCCESS Successfully find the file.
102 @retval EFI_DEVICE_ERROR Device error.
103 @retval EFI_ACCESS_DENIED Fv could not read.
104 @retval EFI_NOT_FOUND No matching file found.
105 @retval EFI_INVALID_PARAMETER Invalid parameter
111 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
113 IN OUT EFI_FV_FILETYPE
*FileType
,
114 OUT EFI_GUID
*NameGuid
,
115 OUT EFI_FV_FILE_ATTRIBUTES
*Attributes
,
121 EFI_FV_ATTRIBUTES FvAttributes
;
122 EFI_FFS_FILE_HEADER
*FfsFileHeader
;
125 FFS_FILE_LIST_ENTRY
*FfsFileEntry
;
128 FvDevice
= FV_DEVICE_FROM_THIS (This
);
130 Status
= FvGetVolumeAttributes (This
, &FvAttributes
);
131 if (EFI_ERROR (Status
)){
136 // Check if read operation is enabled
138 if ((FvAttributes
& EFI_FV2_READ_STATUS
) == 0) {
139 return EFI_ACCESS_DENIED
;
142 if (*FileType
> EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
) {
144 // File type needs to be in 0 - 0x0B
146 return EFI_NOT_FOUND
;
149 KeyValue
= (UINTN
*)Key
;
151 if (*KeyValue
== 0) {
153 // Search for 1st matching file
155 Link
= &FvDevice
->FfsFileListHeader
;
158 // Key is pointer to FFsFileEntry, so get next one
160 Link
= (LIST_ENTRY
*)(*KeyValue
);
163 if (Link
->ForwardLink
== &FvDevice
->FfsFileListHeader
) {
165 // Next is end of list so we did not find data
167 return EFI_NOT_FOUND
;
170 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*)Link
->ForwardLink
;
171 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)FfsFileEntry
->FfsHeader
;
176 *KeyValue
= (UINTN
)FfsFileEntry
;
178 if (FfsFileHeader
->Type
== EFI_FV_FILETYPE_FFS_PAD
) {
180 // we ignore pad files
185 if (*FileType
== EFI_FV_FILETYPE_ALL
) {
187 // Process all file types so we have a match
192 if (*FileType
== FfsFileHeader
->Type
) {
194 // Found a matching file type
202 // Return FileType, NameGuid, and Attributes
204 *FileType
= FfsFileHeader
->Type
;
205 CopyMem (NameGuid
, &FfsFileHeader
->Name
, sizeof (EFI_GUID
));
206 *Attributes
= FfsAttributes2FvFileAttributes (FfsFileHeader
->Attributes
);
209 // Read four bytes out of the 3 byte array and throw out extra data
211 FileLength
= *(UINT32
*)&FfsFileHeader
->Size
[0] & 0x00FFFFFF;
214 // we need to substract the header size
216 *Size
= FileLength
- sizeof(EFI_FFS_FILE_HEADER
);
224 Locates a file in the firmware volume and
225 copies it to the supplied buffer.
227 @param This Indicates the calling context.
228 @param NameGuid Pointer to an EFI_GUID, which is the
230 @param Buffer Buffer is a pointer to pointer to a buffer
231 in which the file or section contents or are
233 @param BufferSize BufferSize is a pointer to caller allocated
234 UINTN. On input *BufferSize indicates the
235 size in bytes of the memory region pointed
236 to by Buffer. On output, *BufferSize
237 contains the number of bytes required to
239 @param FoundType FoundType is a pointer to a caller allocated
240 EFI_FV_FILETYPE that on successful return
241 from Read() contains the type of file read.
242 This output reflects the file type
243 irrespective of the value of the SectionType
245 @param FileAttributes FileAttributes is a pointer to a caller
246 allocated EFI_FV_FILE_ATTRIBUTES. On
247 successful return from Read(),
248 *FileAttributes contains the attributes of
250 @param AuthenticationStatus AuthenticationStatus is a pointer to a
251 caller allocated UINTN in which the
252 authentication status is returned.
254 @retval EFI_SUCCESS Successfully read to memory buffer.
255 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.
256 @retval EFI_NOT_FOUND Not found.
257 @retval EFI_DEVICE_ERROR Device error.
258 @retval EFI_ACCESS_DENIED Could not read.
259 @retval EFI_INVALID_PARAMETER Invalid parameter.
260 @retval EFI_OUT_OF_RESOURCES Not enough buffer to be allocated.
266 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
267 IN CONST EFI_GUID
*NameGuid
,
268 IN OUT VOID
**Buffer
,
269 IN OUT UINTN
*BufferSize
,
270 OUT EFI_FV_FILETYPE
*FoundType
,
271 OUT EFI_FV_FILE_ATTRIBUTES
*FileAttributes
,
272 OUT UINT32
*AuthenticationStatus
277 EFI_GUID SearchNameGuid
;
278 EFI_FV_FILETYPE LocalFoundType
;
279 EFI_FV_FILE_ATTRIBUTES LocalAttributes
;
282 EFI_FFS_FILE_HEADER
*FfsHeader
;
283 UINTN InputBufferSize
;
285 if (NULL
== NameGuid
) {
286 return EFI_INVALID_PARAMETER
;
289 FvDevice
= FV_DEVICE_FROM_THIS (This
);
293 // Keep looking until we find the matching NameGuid.
294 // The Key is really an FfsFileEntry
296 FvDevice
->LastKey
= 0;
299 Status
= FvGetNextFile (
307 if (EFI_ERROR (Status
)) {
308 return EFI_NOT_FOUND
;
310 } while (!CompareGuid (&SearchNameGuid
, NameGuid
));
313 // Get a pointer to the header
315 FfsHeader
= FvDevice
->LastKey
->FfsHeader
;
318 // Remember callers buffer size
320 InputBufferSize
= *BufferSize
;
323 // Calculate return values
325 *FoundType
= FfsHeader
->Type
;
326 *FileAttributes
= FfsAttributes2FvFileAttributes (FfsHeader
->Attributes
);
327 *AuthenticationStatus
= 0;
328 *BufferSize
= FileSize
;
330 if (Buffer
== NULL
) {
332 // If Buffer is NULL, we only want to get the information colected so far
338 // Skip over file header
340 SrcPtr
= ((UINT8
*)FfsHeader
) + sizeof (EFI_FFS_FILE_HEADER
);
342 Status
= EFI_SUCCESS
;
343 if (*Buffer
== NULL
) {
345 // Caller passed in a pointer so allocate buffer for them
347 *Buffer
= CoreAllocateBootServicesPool (FileSize
);
348 if (*Buffer
== NULL
) {
349 return EFI_OUT_OF_RESOURCES
;
351 } else if (FileSize
> InputBufferSize
) {
353 // Callers buffer was not big enough
355 Status
= EFI_WARN_BUFFER_TOO_SMALL
;
356 FileSize
= InputBufferSize
;
360 // Copy data into callers buffer
362 CopyMem (*Buffer
, SrcPtr
, FileSize
);
370 Locates a section in a given FFS File and
371 copies it to the supplied buffer (not including section header).
373 @param This Indicates the calling context.
374 @param NameGuid Pointer to an EFI_GUID, which is the
376 @param SectionType Indicates the section type to return.
377 @param SectionInstance Indicates which instance of sections with a
378 type of SectionType to return.
379 @param Buffer Buffer is a pointer to pointer to a buffer
380 in which the file or section contents or are
382 @param BufferSize BufferSize is a pointer to caller allocated
384 @param AuthenticationStatus AuthenticationStatus is a pointer to a
385 caller allocated UINT32 in which the
386 authentication status is returned.
388 @retval EFI_SUCCESS Successfully read the file section into
390 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.
391 @retval EFI_NOT_FOUND Section not found.
392 @retval EFI_DEVICE_ERROR Device error.
393 @retval EFI_ACCESS_DENIED Could not read.
394 @retval EFI_INVALID_PARAMETER Invalid parameter.
400 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
401 IN CONST EFI_GUID
*NameGuid
,
402 IN EFI_SECTION_TYPE SectionType
,
403 IN UINTN SectionInstance
,
404 IN OUT VOID
**Buffer
,
405 IN OUT UINTN
*BufferSize
,
406 OUT UINT32
*AuthenticationStatus
411 EFI_FV_FILETYPE FileType
;
412 EFI_FV_FILE_ATTRIBUTES FileAttributes
;
415 FFS_FILE_LIST_ENTRY
*FfsEntry
;
417 if (NULL
== NameGuid
|| Buffer
== NULL
) {
418 return EFI_INVALID_PARAMETER
;
421 FvDevice
= FV_DEVICE_FROM_THIS (This
);
424 // Read the whole file into buffer
427 Status
= FvReadFile (
430 (VOID
**)&FileBuffer
,
437 // Get the last key used by our call to FvReadFile as it is the FfsEntry for this file.
439 FfsEntry
= (FFS_FILE_LIST_ENTRY
*)FvDevice
->LastKey
;
441 if (EFI_ERROR (Status
)) {
446 // Check to see that the file actually HAS sections before we go any further.
448 if (FileType
== EFI_FV_FILETYPE_RAW
) {
449 Status
= EFI_NOT_FOUND
;
454 // Use FfsEntry to cache Section Extraction Protocol Inforomation
456 if (FfsEntry
->StreamHandle
== 0) {
457 Status
= OpenSectionStream (
460 &FfsEntry
->StreamHandle
462 if (EFI_ERROR (Status
)) {
468 // If SectionType == 0 We need the whole section stream
470 Status
= GetSection (
471 FfsEntry
->StreamHandle
,
472 (SectionType
== 0) ? NULL
: &SectionType
,
474 (SectionType
== 0) ? 0 : SectionInstance
,
481 // Close of stream defered to close of FfsHeader list to allow SEP to cache data
485 CoreFreePool (FileBuffer
);