2 Common defines and definitions for a FwVolDxe driver.
4 Copyright (c) 2006 - 2017, 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 #ifndef _FWVOL_DRIVER_H_
18 #define _FWVOL_DRIVER_H_
22 #include <Guid/FirmwareFileSystem2.h>
23 #include <Guid/FirmwareFileSystem3.h>
24 #include <Protocol/SectionExtraction.h>
25 #include <Protocol/FaultTolerantWrite.h>
26 #include <Protocol/FirmwareVolume2.h>
27 #include <Protocol/FirmwareVolumeBlock.h>
29 #include <Library/DebugLib.h>
30 #include <Library/UefiDriverEntryPoint.h>
31 #include <Library/UefiLib.h>
32 #include <Library/BaseLib.h>
33 #include <Library/BaseMemoryLib.h>
34 #include <Library/MemoryAllocationLib.h>
35 #include <Library/UefiBootServicesTableLib.h>
36 #include <Library/HobLib.h>
38 #define FV_DEVICE_SIGNATURE SIGNATURE_32 ('_', 'F', 'V', '_')
41 // Define two helper macro to extract the Capability field or Status field in FVB
44 #define EFI_FVB2_CAPABILITIES (EFI_FVB2_READ_DISABLED_CAP | \
45 EFI_FVB2_READ_ENABLED_CAP | \
46 EFI_FVB2_WRITE_DISABLED_CAP | \
47 EFI_FVB2_WRITE_ENABLED_CAP | \
51 #define EFI_FVB2_STATUS (EFI_FVB2_READ_STATUS | EFI_FVB2_WRITE_STATUS | EFI_FVB2_LOCK_STATUS)
56 // Used to calculate from address -> Lba
61 UINT8
*StartingAddress
;
66 // Used to track free space in the Fv
70 UINT8
*StartingAddress
;
75 // Used to track all non-deleted files
80 } FFS_FILE_LIST_ENTRY
;
84 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
85 EFI_FIRMWARE_VOLUME2_PROTOCOL Fv
;
86 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
91 EFI_PHYSICAL_ADDRESS CachedFv
;
93 LIST_ENTRY FreeSpaceHeader
;
94 LIST_ENTRY FfsFileListHeader
;
96 FFS_FILE_LIST_ENTRY
*CurrentFfsFile
;
98 UINT32 AuthenticationStatus
;
101 #define FV_DEVICE_FROM_THIS(a) CR (a, FV_DEVICE, Fv, FV_DEVICE_SIGNATURE)
104 Retrieves attributes, insures positive polarity of attribute bits, returns
105 resulting attributes in output parameter.
107 @param This Calling context
108 @param Attributes output buffer which contains attributes
110 @retval EFI_SUCCESS Successfully got volume attributes
115 FvGetVolumeAttributes (
116 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
117 OUT EFI_FV_ATTRIBUTES
*Attributes
121 Sets current attributes for volume.
123 @param This Calling context
124 @param Attributes On input, FvAttributes is a pointer to
125 an EFI_FV_ATTRIBUTES containing the
126 desired firmware volume settings. On
127 successful return, it contains the new
128 settings of the firmware volume. On
129 unsuccessful return, FvAttributes is not
130 modified and the firmware volume
131 settings are not changed.
133 @retval EFI_SUCCESS The requested firmware volume attributes
134 were set and the resulting
135 EFI_FV_ATTRIBUTES is returned in
137 @retval EFI_ACCESS_DENIED Atrribute is locked down.
138 @retval EFI_INVALID_PARAMETER Atrribute is not valid.
143 FvSetVolumeAttributes (
144 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
145 IN OUT EFI_FV_ATTRIBUTES
*Attributes
149 Given the input key, search for the next matching file in the volume.
151 @param This Indicates the calling context.
152 @param Key Key is a pointer to a caller allocated
153 buffer that contains implementation specific
154 data that is used to track where to begin
155 the search for the next file. The size of
156 the buffer must be at least This->KeySize
157 bytes long. To reinitialize the search and
158 begin from the beginning of the firmware
159 volume, the entire buffer must be cleared to
160 zero. Other than clearing the buffer to
161 initiate a new search, the caller must not
162 modify the data in the buffer between calls
164 @param FileType FileType is a pointer to a caller allocated
165 EFI_FV_FILETYPE. The GetNextFile() API can
166 filter it's search for files based on the
167 value of *FileType input. A *FileType input
168 of 0 causes GetNextFile() to search for
169 files of all types. If a file is found, the
170 file's type is returned in *FileType.
171 *FileType is not modified if no file is
173 @param NameGuid NameGuid is a pointer to a caller allocated
174 EFI_GUID. If a file is found, the file's
175 name is returned in *NameGuid. *NameGuid is
176 not modified if no file is found.
177 @param Attributes Attributes is a pointer to a caller
178 allocated EFI_FV_FILE_ATTRIBUTES. If a file
179 is found, the file's attributes are returned
180 in *Attributes. *Attributes is not modified
182 @param Size Size is a pointer to a caller allocated
183 UINTN. If a file is found, the file's size
184 is returned in *Size. *Size is not modified
187 @retval EFI_SUCCESS Successfully find the file.
188 @retval EFI_DEVICE_ERROR Device error.
189 @retval EFI_ACCESS_DENIED Fv could not read.
190 @retval EFI_NOT_FOUND No matching file found.
191 @retval EFI_INVALID_PARAMETER Invalid parameter
197 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
199 IN OUT EFI_FV_FILETYPE
*FileType
,
200 OUT EFI_GUID
*NameGuid
,
201 OUT EFI_FV_FILE_ATTRIBUTES
*Attributes
,
206 Locates a file in the firmware volume and
207 copies it to the supplied buffer.
209 @param This Indicates the calling context.
210 @param NameGuid Pointer to an EFI_GUID, which is the
212 @param Buffer Buffer is a pointer to pointer to a buffer
213 in which the file or section contents or are
215 @param BufferSize BufferSize is a pointer to caller allocated
216 UINTN. On input *BufferSize indicates the
217 size in bytes of the memory region pointed
218 to by Buffer. On output, *BufferSize
219 contains the number of bytes required to
221 @param FoundType FoundType is a pointer to a caller allocated
222 EFI_FV_FILETYPE that on successful return
223 from Read() contains the type of file read.
224 This output reflects the file type
225 irrespective of the value of the SectionType
227 @param FileAttributes FileAttributes is a pointer to a caller
228 allocated EFI_FV_FILE_ATTRIBUTES. On
229 successful return from Read(),
230 *FileAttributes contains the attributes of
232 @param AuthenticationStatus AuthenticationStatus is a pointer to a
233 caller allocated UINTN in which the
234 authentication status is returned.
236 @retval EFI_SUCCESS Successfully read to memory buffer.
237 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.
238 @retval EFI_NOT_FOUND Not found.
239 @retval EFI_DEVICE_ERROR Device error.
240 @retval EFI_ACCESS_DENIED Could not read.
241 @retval EFI_INVALID_PARAMETER Invalid parameter.
242 @retval EFI_OUT_OF_RESOURCES Not enough buffer to be allocated.
248 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
249 IN CONST EFI_GUID
*NameGuid
,
250 IN OUT VOID
**Buffer
,
251 IN OUT UINTN
*BufferSize
,
252 OUT EFI_FV_FILETYPE
*FoundType
,
253 OUT EFI_FV_FILE_ATTRIBUTES
*FileAttributes
,
254 OUT UINT32
*AuthenticationStatus
258 Locates a section in a given FFS File and
259 copies it to the supplied buffer (not including section header).
261 @param This Indicates the calling context.
262 @param NameGuid Pointer to an EFI_GUID, which is the
264 @param SectionType Indicates the section type to return.
265 @param SectionInstance Indicates which instance of sections with a
266 type of SectionType to return.
267 @param Buffer Buffer is a pointer to pointer to a buffer
268 in which the file or section contents or are
270 @param BufferSize BufferSize is a pointer to caller allocated
272 @param AuthenticationStatus AuthenticationStatus is a pointer to a
273 caller allocated UINT32 in which the
274 authentication status is returned.
276 @retval EFI_SUCCESS Successfully read the file section into
278 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.
279 @retval EFI_NOT_FOUND Section not found.
280 @retval EFI_DEVICE_ERROR Device error.
281 @retval EFI_ACCESS_DENIED Could not read.
282 @retval EFI_INVALID_PARAMETER Invalid parameter.
288 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
289 IN CONST EFI_GUID
*NameGuid
,
290 IN EFI_SECTION_TYPE SectionType
,
291 IN UINTN SectionInstance
,
292 IN OUT VOID
**Buffer
,
293 IN OUT UINTN
*BufferSize
,
294 OUT UINT32
*AuthenticationStatus
298 Writes one or more files to the firmware volume.
300 @param This Indicates the calling context.
301 @param NumberOfFiles Number of files.
302 @param WritePolicy WritePolicy indicates the level of reliability
303 for the write in the event of a power failure or
304 other system failure during the write operation.
305 @param FileData FileData is an pointer to an array of
306 EFI_FV_WRITE_DATA. Each element of array
307 FileData represents a file to be written.
309 @retval EFI_SUCCESS Files successfully written to firmware volume
310 @retval EFI_OUT_OF_RESOURCES Not enough buffer to be allocated.
311 @retval EFI_DEVICE_ERROR Device error.
312 @retval EFI_WRITE_PROTECTED Write protected.
313 @retval EFI_NOT_FOUND Not found.
314 @retval EFI_INVALID_PARAMETER Invalid parameter.
315 @retval EFI_UNSUPPORTED This function not supported.
321 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
322 IN UINT32 NumberOfFiles
,
323 IN EFI_FV_WRITE_POLICY WritePolicy
,
324 IN EFI_FV_WRITE_FILE_DATA
*FileData
328 Return information of type InformationType for the requested firmware
331 @param This Pointer to EFI_FIRMWARE_VOLUME2_PROTOCOL.
332 @param InformationType InformationType for requested.
333 @param BufferSize On input, size of Buffer.On output, the amount of
334 data returned in Buffer.
335 @param Buffer A poniter to the data buffer to return.
337 @return EFI_UNSUPPORTED Could not get.
343 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
344 IN CONST EFI_GUID
*InformationType
,
345 IN OUT UINTN
*BufferSize
,
351 Set information with InformationType into the requested firmware volume.
353 @param This Pointer to EFI_FIRMWARE_VOLUME2_PROTOCOL.
354 @param InformationType InformationType for requested.
355 @param BufferSize Size of Buffer data.
356 @param Buffer A poniter to the data buffer to be set.
358 @retval EFI_UNSUPPORTED Could not set.
364 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
365 IN CONST EFI_GUID
*InformationType
,
367 IN CONST VOID
*Buffer
371 Writes data beginning at Lba:Offset from FV. The write terminates either
372 when *NumBytes of data have been written, or when the firmware end is
373 reached. *NumBytes is updated to reflect the actual number of bytes
376 @param FvDevice Cached Firmware Volume
377 @param Offset Offset in the block at which to begin write
378 @param NumBytes At input, indicates the requested write size.
379 At output, indicates the actual number of bytes written.
380 @param Buffer Buffer containing source data for the write.
382 @retval EFI_SUCCESS Data is successfully written into FV.
383 @return error Data is failed written.
388 IN FV_DEVICE
*FvDevice
,
390 IN OUT UINTN
*NumBytes
,
396 Check if a block of buffer is erased.
398 @param ErasePolarity Erase polarity attribute of the firmware volume
399 @param Buffer The buffer to be checked
400 @param BufferSize Size of the buffer in bytes
402 @retval TRUE The block of buffer is erased
403 @retval FALSE The block of buffer is not erased
408 IN UINT8 ErasePolarity
,
414 Get the FFS file state by checking the highest bit set in the header's state field.
416 @param ErasePolarity Erase polarity attribute of the firmware volume
417 @param FfsHeader Points to the FFS file header
419 @return FFS File state
424 IN UINT8 ErasePolarity
,
425 IN EFI_FFS_FILE_HEADER
*FfsHeader
429 Verify checksum of the firmware volume header.
431 @param FvHeader Points to the firmware volume header to be checked
433 @retval TRUE Checksum verification passed
434 @retval FALSE Checksum verification failed
438 VerifyFvHeaderChecksum (
439 IN EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
443 Check if it's a valid FFS file header.
445 @param ErasePolarity Erase polarity attribute of the firmware volume
446 @param FfsHeader Points to the FFS file header to be checked
448 @retval TRUE Valid FFS file header
449 @retval FALSE Invalid FFS file header
454 IN UINT8 ErasePolarity
,
455 IN EFI_FFS_FILE_HEADER
*FfsHeader
459 Check if it's a valid FFS file.
460 Here we are sure that it has a valid FFS file header since we must call IsValidFfsHeader() first.
462 @param FvDevice Cached FV image.
463 @param FfsHeader Points to the FFS file to be checked
465 @retval TRUE Valid FFS file
466 @retval FALSE Invalid FFS file
471 IN FV_DEVICE
*FvDevice
,
472 IN EFI_FFS_FILE_HEADER
*FfsHeader
476 Given the supplied FW_VOL_BLOCK_PROTOCOL, allocate a buffer for output and
477 copy the real length volume header into it.
479 @param Fvb The FW_VOL_BLOCK_PROTOCOL instance from which to
480 read the volume header
481 @param FwVolHeader Pointer to pointer to allocated buffer in which
482 the volume header is returned.
484 @retval EFI_OUT_OF_RESOURCES No enough buffer could be allocated.
485 @retval EFI_SUCCESS Successfully read volume header to the allocated
487 @retval EFI_ACCESS_DENIED Read status of FV is not enabled.
488 @retval EFI_INVALID_PARAMETER The FV Header signature is not as expected or
489 the file system could not be understood.
493 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
494 OUT EFI_FIRMWARE_VOLUME_HEADER
**FwVolHeader
498 Convert the Buffer Address to LBA Entry Address.
500 @param FvDevice Cached FvDevice
501 @param BufferAddress Address of Buffer
502 @param LbaListEntry Pointer to the got LBA entry that contains the address.
504 @retval EFI_NOT_FOUND Buffer address is out of FvDevice.
505 @retval EFI_SUCCESS LBA entry is found for Buffer address.
510 IN FV_DEVICE
*FvDevice
,
511 IN EFI_PHYSICAL_ADDRESS BufferAddress
,
512 OUT LBA_ENTRY
**LbaListEntry
516 Convert the Buffer Address to LBA Address & Offset.
518 @param FvDevice Cached FvDevice
519 @param BufferAddress Address of Buffer
520 @param Lba Pointer to the gob Lba value
521 @param Offset Pointer to the got Offset
523 @retval EFI_NOT_FOUND Buffer address is out of FvDevice.
524 @retval EFI_SUCCESS LBA and Offset is found for Buffer address.
529 IN FV_DEVICE
*FvDevice
,
530 IN EFI_PHYSICAL_ADDRESS BufferAddress
,
536 Set File State in the FfsHeader.
538 @param State File state to be set into FFS header.
539 @param FfsHeader Points to the FFS file header
545 IN EFI_FFS_FILE_HEADER
*FfsHeader
549 Create a PAD File in the Free Space.
551 @param FvDevice Firmware Volume Device.
552 @param FreeSpaceEntry Indicating in which Free Space(Cache) the Pad file will be inserted.
553 @param Size Pad file Size, not include the header.
554 @param PadFileEntry The Ffs File Entry that points to this Pad File.
556 @retval EFI_SUCCESS Successfully create a PAD file.
557 @retval EFI_OUT_OF_RESOURCES No enough free space to create a PAD file.
558 @retval EFI_INVALID_PARAMETER Size is not 8 byte alignment.
559 @retval EFI_DEVICE_ERROR Free space is not erased.
562 FvCreatePadFileInFreeSpace (
563 IN FV_DEVICE
*FvDevice
,
564 IN FREE_SPACE_ENTRY
*FreeSpaceEntry
,
566 OUT FFS_FILE_LIST_ENTRY
**PadFileEntry
570 Create a new file within a PAD file area.
572 @param FvDevice Firmware Volume Device.
573 @param FfsFileBuffer A buffer that holds an FFS file,(it contains a File Header which is in init state).
574 @param BufferSize The size of FfsFileBuffer.
575 @param ActualFileSize The actual file length, it may not be multiples of 8.
576 @param FileName The FFS File Name.
577 @param FileType The FFS File Type.
578 @param FileAttributes The Attributes of the FFS File to be created.
580 @retval EFI_SUCCESS Successfully create a new file within the found PAD file area.
581 @retval EFI_OUT_OF_RESOURCES No suitable PAD file is found.
582 @retval other errors New file is created failed.
586 FvCreateNewFileInsidePadFile (
587 IN FV_DEVICE
*FvDevice
,
588 IN UINT8
*FfsFileBuffer
,
590 IN UINTN ActualFileSize
,
591 IN EFI_GUID
*FileName
,
592 IN EFI_FV_FILETYPE FileType
,
593 IN EFI_FV_FILE_ATTRIBUTES FileAttributes
597 Write multiple files into FV in reliable method.
599 @param FvDevice Firmware Volume Device.
600 @param NumOfFiles Total File number to be written.
601 @param FileData The array of EFI_FV_WRITE_FILE_DATA structure,
602 used to get name, attributes, type, etc
603 @param FileOperation The array of operation for each file.
605 @retval EFI_SUCCESS Files are added into FV.
606 @retval EFI_OUT_OF_RESOURCES No enough free PAD files to add the input files.
607 @retval EFI_INVALID_PARAMETER File number is less than or equal to 1.
608 @retval EFI_UNSUPPORTED File number exceeds the supported max numbers of files.
612 FvCreateMultipleFiles (
613 IN FV_DEVICE
*FvDevice
,
615 IN EFI_FV_WRITE_FILE_DATA
*FileData
,
616 IN BOOLEAN
*FileOperation
620 Calculate the checksum for the FFS header.
622 @param FfsHeader FFS File Header which needs to calculate the checksum
627 IN EFI_FFS_FILE_HEADER
*FfsHeader
631 Calculate the checksum for the FFS File.
633 @param FfsHeader FFS File Header which needs to calculate the checksum
634 @param ActualFileSize The whole Ffs File Length.
639 IN EFI_FFS_FILE_HEADER
*FfsHeader
,
640 IN UINTN ActualFileSize
644 Get the alignment value from File Attributes.
646 @param FfsAttributes FFS attribute
648 @return Alignment value.
652 GetRequiredAlignment (
653 IN EFI_FV_FILE_ATTRIBUTES FfsAttributes
657 Locate Pad File for writing, this is got from FV Cache.
659 @param FvDevice Cached Firmware Volume.
660 @param Size The required FFS file size.
661 @param RequiredAlignment FFS File Data alignment requirement.
662 @param PadSize Pointer to the size of leading Pad File.
663 @param PadFileEntry Pointer to the Pad File Entry that meets the requirement.
665 @retval EFI_SUCCESS The required pad file is found.
666 @retval EFI_NOT_FOUND The required pad file can't be found.
671 IN FV_DEVICE
*FvDevice
,
673 IN UINTN RequiredAlignment
,
675 OUT FFS_FILE_LIST_ENTRY
**PadFileEntry
679 Locate a suitable pad file for multiple file writing.
681 @param FvDevice Cached Firmware Volume.
682 @param NumOfFiles The number of Files that needed updating
683 @param BufferSize The array of each file size.
684 @param RequiredAlignment The array of of FFS File Data alignment requirement.
685 @param PadSize The array of size of each leading Pad File.
686 @param TotalSizeNeeded The totalsize that can hold these files.
687 @param PadFileEntry Pointer to the Pad File Entry that meets the requirement.
689 @retval EFI_SUCCESS The required pad file is found.
690 @retval EFI_NOT_FOUND The required pad file can't be found.
694 FvSearchSuitablePadFile (
695 IN FV_DEVICE
*FvDevice
,
697 IN UINTN
*BufferSize
,
698 IN UINTN
*RequiredAlignment
,
700 OUT UINTN
*TotalSizeNeeded
,
701 OUT FFS_FILE_LIST_ENTRY
**PadFileEntry
705 Locate a Free Space entry which can hold these files, including
706 meeting the alignment requirements.
708 @param FvDevice Cached Firmware Volume.
709 @param NumOfFiles The number of Files that needed updating
710 @param BufferSize The array of each file size.
711 @param RequiredAlignment The array of of FFS File Data alignment requirement.
712 @param PadSize The array of size of each leading Pad File.
713 @param TotalSizeNeeded The got total size that can hold these files.
714 @param FreeSpaceEntry The Free Space Entry that can hold these files.
716 @retval EFI_SUCCESS The free space entry is found.
717 @retval EFI_NOT_FOUND The free space entry can't be found.
721 FvSearchSuitableFreeSpace (
722 IN FV_DEVICE
*FvDevice
,
724 IN UINTN
*BufferSize
,
725 IN UINTN
*RequiredAlignment
,
727 OUT UINTN
*TotalSizeNeeded
,
728 OUT FREE_SPACE_ENTRY
**FreeSpaceEntry
732 Change FFS file header state and write to FV.
734 @param FvDevice Cached FV image.
735 @param FfsHeader Points to the FFS file header to be updated.
736 @param State FFS file state to be set.
738 @retval EFI_SUCCESS File state is writen into FV.
739 @retval others File state can't be writen into FV.
744 IN FV_DEVICE
*FvDevice
,
745 IN EFI_FFS_FILE_HEADER
*FfsHeader
,
746 IN EFI_FFS_FILE_STATE State
750 Convert EFI_FV_FILE_ATTRIBUTES to FFS_FILE_ATTRIBUTES.
752 @param FvFileAttrib The value of EFI_FV_FILE_ATTRIBUTES
753 @param FfsFileAttrib Pointer to the got FFS_FILE_ATTRIBUTES value.
757 FvFileAttrib2FfsFileAttrib (
758 IN EFI_FV_FILE_ATTRIBUTES FvFileAttrib
,
759 OUT UINT8
*FfsFileAttrib