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};
40 EFI_FV_FILE_ATTRIBUTES
41 FfsAttributes2FvFileAttributes (
42 IN EFI_FFS_FILE_ATTRIBUTES FfsAttributes
47 Convert the FFS File Attributes to FV File Attributes
50 FfsAttributes - The attributes of UINT8 type.
53 The attributes of EFI_FV_FILE_ATTRIBUTES
57 FfsAttributes
= (EFI_FFS_FILE_ATTRIBUTES
)((FfsAttributes
& FFS_ATTRIB_DATA_ALIGNMENT
) >> 3);
58 ASSERT (FfsAttributes
< 8);
60 return (EFI_FV_FILE_ATTRIBUTES
) mFvAttributes
[FfsAttributes
];
67 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
69 IN OUT EFI_FV_FILETYPE
*FileType
,
70 OUT EFI_GUID
*NameGuid
,
71 OUT EFI_FV_FILE_ATTRIBUTES
*Attributes
,
77 Given the input key, search for the next matching file in the volume.
80 This - Indicates the calling context.
81 FileType - FileType is a pointer to a caller allocated
82 EFI_FV_FILETYPE. The GetNextFile() API can filter it's
83 search for files based on the value of *FileType input.
84 A *FileType input of 0 causes GetNextFile() to search for
85 files of all types. If a file is found, the file's type
86 is returned in *FileType. *FileType is not modified if
88 Key - Key is a pointer to a caller allocated buffer that
89 contains implementation specific data that is used to
90 track where to begin the search for the next file.
91 The size of the buffer must be at least This->KeySize
92 bytes long. To reinitialize the search and begin from
93 the beginning of the firmware volume, the entire buffer
94 must be cleared to zero. Other than clearing the buffer
95 to initiate a new search, the caller must not modify the
96 data in the buffer between calls to GetNextFile().
97 NameGuid - NameGuid is a pointer to a caller allocated EFI_GUID.
98 If a file is found, the file's name is returned in
99 *NameGuid. *NameGuid is not modified if no file is
101 Attributes - Attributes is a pointer to a caller allocated
102 EFI_FV_FILE_ATTRIBUTES. If a file is found, the file's
103 attributes are returned in *Attributes. *Attributes is
104 not modified if no file is found.
105 Size - Size is a pointer to a caller allocated UINTN.
106 If a file is found, the file's size is returned in *Size.
107 *Size is not modified if no file is found.
110 EFI_SUCCESS - Successfully find the file.
111 EFI_DEVICE_ERROR - Device error.
112 EFI_ACCESS_DENIED - Fv could not read.
113 EFI_NOT_FOUND - No matching file found.
114 EFI_INVALID_PARAMETER - Invalid parameter
120 EFI_FV_ATTRIBUTES FvAttributes
;
121 EFI_FFS_FILE_HEADER
*FfsFileHeader
;
124 FFS_FILE_LIST_ENTRY
*FfsFileEntry
;
127 FvDevice
= FV_DEVICE_FROM_THIS (This
);
129 Status
= FvGetVolumeAttributes (This
, &FvAttributes
);
130 if (EFI_ERROR (Status
)){
135 // Check if read operation is enabled
137 if ((FvAttributes
& EFI_FV2_READ_STATUS
) == 0) {
138 return EFI_ACCESS_DENIED
;
141 if (*FileType
> EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
) {
143 // File type needs to be in 0 - 0x0B
145 return EFI_NOT_FOUND
;
148 KeyValue
= (UINTN
*)Key
;
150 if (*KeyValue
== 0) {
152 // Search for 1st matching file
154 Link
= &FvDevice
->FfsFileListHeader
;
157 // Key is pointer to FFsFileEntry, so get next one
159 Link
= (LIST_ENTRY
*)(*KeyValue
);
162 if (Link
->ForwardLink
== &FvDevice
->FfsFileListHeader
) {
164 // Next is end of list so we did not find data
166 return EFI_NOT_FOUND
;
169 FfsFileEntry
= (FFS_FILE_LIST_ENTRY
*)Link
->ForwardLink
;
170 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)FfsFileEntry
->FfsHeader
;
175 *KeyValue
= (UINTN
)FfsFileEntry
;
177 if (FfsFileHeader
->Type
== EFI_FV_FILETYPE_FFS_PAD
) {
179 // we ignore pad files
184 if (*FileType
== EFI_FV_FILETYPE_ALL
) {
186 // Process all file types so we have a match
191 if (*FileType
== FfsFileHeader
->Type
) {
193 // Found a matching file type
201 // Return FileType, NameGuid, and Attributes
203 *FileType
= FfsFileHeader
->Type
;
204 CopyMem (NameGuid
, &FfsFileHeader
->Name
, sizeof (EFI_GUID
));
205 *Attributes
= FfsAttributes2FvFileAttributes (FfsFileHeader
->Attributes
);
208 // Read four bytes out of the 3 byte array and throw out extra data
210 FileLength
= *(UINT32
*)&FfsFileHeader
->Size
[0] & 0x00FFFFFF;
213 // we need to substract the header size
215 *Size
= FileLength
- sizeof(EFI_FFS_FILE_HEADER
);
224 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
225 IN CONST EFI_GUID
*NameGuid
,
226 IN OUT VOID
**Buffer
,
227 IN OUT UINTN
*BufferSize
,
228 OUT EFI_FV_FILETYPE
*FoundType
,
229 OUT EFI_FV_FILE_ATTRIBUTES
*FileAttributes
,
230 OUT UINT32
*AuthenticationStatus
235 Locates a file in the firmware volume and
236 copies it to the supplied buffer.
239 This - Indicates the calling context.
240 NameGuid - Pointer to an EFI_GUID, which is the filename.
241 Buffer - Buffer is a pointer to pointer to a buffer in
242 which the file or section contents or are returned.
243 BufferSize - BufferSize is a pointer to caller allocated
244 UINTN. On input *BufferSize indicates the size
245 in bytes of the memory region pointed to by
246 Buffer. On output, *BufferSize contains the number
247 of bytes required to read the file.
248 FoundType - FoundType is a pointer to a caller allocated
249 EFI_FV_FILETYPE that on successful return from Read()
250 contains the type of file read. This output reflects
251 the file type irrespective of the value of the
253 FileAttributes - FileAttributes is a pointer to a caller allocated
254 EFI_FV_FILE_ATTRIBUTES. On successful return from
255 Read(), *FileAttributes contains the attributes of
257 AuthenticationStatus - AuthenticationStatus is a pointer to a caller
258 allocated UINTN in which the authentication status
261 EFI_SUCCESS - Successfully read to memory buffer.
262 EFI_WARN_BUFFER_TOO_SMALL - Buffer too small.
263 EFI_NOT_FOUND - Not found.
264 EFI_DEVICE_ERROR - Device error.
265 EFI_ACCESS_DENIED - Could not read.
266 EFI_INVALID_PARAMETER - Invalid parameter.
267 EFI_OUT_OF_RESOURCES - Not enough buffer to be allocated.
273 EFI_GUID SearchNameGuid
;
274 EFI_FV_FILETYPE LocalFoundType
;
275 EFI_FV_FILE_ATTRIBUTES LocalAttributes
;
278 EFI_FFS_FILE_HEADER
*FfsHeader
;
279 UINTN InputBufferSize
;
281 if (NULL
== NameGuid
) {
282 return EFI_INVALID_PARAMETER
;
285 FvDevice
= FV_DEVICE_FROM_THIS (This
);
289 // Keep looking until we find the matching NameGuid.
290 // The Key is really an FfsFileEntry
292 FvDevice
->LastKey
= 0;
295 Status
= FvGetNextFile (
303 if (EFI_ERROR (Status
)) {
304 return EFI_NOT_FOUND
;
306 } while (!CompareGuid (&SearchNameGuid
, NameGuid
));
309 // Get a pointer to the header
311 FfsHeader
= FvDevice
->LastKey
->FfsHeader
;
314 // Remember callers buffer size
316 InputBufferSize
= *BufferSize
;
319 // Calculate return values
321 *FoundType
= FfsHeader
->Type
;
322 *FileAttributes
= FfsAttributes2FvFileAttributes (FfsHeader
->Attributes
);
323 *AuthenticationStatus
= 0;
324 *BufferSize
= FileSize
;
326 if (Buffer
== NULL
) {
328 // If Buffer is NULL, we only want to get the information colected so far
334 // Skip over file header
336 SrcPtr
= ((UINT8
*)FfsHeader
) + sizeof (EFI_FFS_FILE_HEADER
);
338 Status
= EFI_SUCCESS
;
339 if (*Buffer
== NULL
) {
341 // Caller passed in a pointer so allocate buffer for them
343 *Buffer
= CoreAllocateBootServicesPool (FileSize
);
344 if (*Buffer
== NULL
) {
345 return EFI_OUT_OF_RESOURCES
;
347 } else if (FileSize
> InputBufferSize
) {
349 // Callers buffer was not big enough
351 Status
= EFI_WARN_BUFFER_TOO_SMALL
;
352 FileSize
= InputBufferSize
;
356 // Copy data into callers buffer
358 CopyMem (*Buffer
, SrcPtr
, FileSize
);
367 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
368 IN CONST EFI_GUID
*NameGuid
,
369 IN EFI_SECTION_TYPE SectionType
,
370 IN UINTN SectionInstance
,
371 IN OUT VOID
**Buffer
,
372 IN OUT UINTN
*BufferSize
,
373 OUT UINT32
*AuthenticationStatus
378 Locates a section in a given FFS File and
379 copies it to the supplied buffer (not including section header).
382 This - Indicates the calling context.
383 NameGuid - Pointer to an EFI_GUID, which is the filename.
384 SectionType - Indicates the section type to return.
385 SectionInstance - Indicates which instance of sections with a type of
386 SectionType to return.
387 Buffer - Buffer is a pointer to pointer to a buffer in which
388 the file or section contents or are returned.
389 BufferSize - BufferSize is a pointer to caller allocated UINTN.
390 AuthenticationStatus -AuthenticationStatus is a pointer to a caller
391 allocated UINT32 in which the authentication status
395 EFI_SUCCESS - Successfully read the file section into buffer.
396 EFI_WARN_BUFFER_TOO_SMALL - Buffer too small.
397 EFI_NOT_FOUND - Section not found.
398 EFI_DEVICE_ERROR - Device error.
399 EFI_ACCESS_DENIED - Could not read.
400 EFI_INVALID_PARAMETER - Invalid parameter.
406 EFI_FV_FILETYPE FileType
;
407 EFI_FV_FILE_ATTRIBUTES FileAttributes
;
410 FFS_FILE_LIST_ENTRY
*FfsEntry
;
412 if (NULL
== NameGuid
|| Buffer
== NULL
) {
413 return EFI_INVALID_PARAMETER
;
416 FvDevice
= FV_DEVICE_FROM_THIS (This
);
419 // Read the whole file into buffer
422 Status
= FvReadFile (
425 (VOID
**)&FileBuffer
,
432 // Get the last key used by our call to FvReadFile as it is the FfsEntry for this file.
434 FfsEntry
= (FFS_FILE_LIST_ENTRY
*)FvDevice
->LastKey
;
436 if (EFI_ERROR (Status
)) {
441 // Check to see that the file actually HAS sections before we go any further.
443 if (FileType
== EFI_FV_FILETYPE_RAW
) {
444 Status
= EFI_NOT_FOUND
;
449 // Use FfsEntry to cache Section Extraction Protocol Inforomation
451 if (FfsEntry
->StreamHandle
== 0) {
452 Status
= OpenSectionStream (
455 &FfsEntry
->StreamHandle
457 if (EFI_ERROR (Status
)) {
463 // If SectionType == 0 We need the whole section stream
465 Status
= GetSection (
466 FfsEntry
->StreamHandle
,
467 (SectionType
== 0) ? NULL
: &SectionType
,
469 (SectionType
== 0) ? 0 : SectionInstance
,
476 // Close of stream defered to close of FfsHeader list to allow SEP to cache data
480 CoreFreePool (FileBuffer
);