2 Common defines and definitions for a FwVolDxe driver.
4 Copyright (c) 2006 - 2015, 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>
37 #define FV_DEVICE_SIGNATURE SIGNATURE_32 ('_', 'F', 'V', '_')
40 // Define two helper macro to extract the Capability field or Status field in FVB
43 #define EFI_FVB2_CAPABILITIES (EFI_FVB2_READ_DISABLED_CAP | \
44 EFI_FVB2_READ_ENABLED_CAP | \
45 EFI_FVB2_WRITE_DISABLED_CAP | \
46 EFI_FVB2_WRITE_ENABLED_CAP | \
50 #define EFI_FVB2_STATUS (EFI_FVB2_READ_STATUS | EFI_FVB2_WRITE_STATUS | EFI_FVB2_LOCK_STATUS)
55 // Used to calculate from address -> Lba
60 UINT8
*StartingAddress
;
65 // Used to track free space in the Fv
69 UINT8
*StartingAddress
;
74 // Used to track all non-deleted files
79 } FFS_FILE_LIST_ENTRY
;
83 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
84 EFI_FIRMWARE_VOLUME2_PROTOCOL Fv
;
85 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
90 EFI_PHYSICAL_ADDRESS CachedFv
;
92 LIST_ENTRY FreeSpaceHeader
;
93 LIST_ENTRY FfsFileListHeader
;
95 FFS_FILE_LIST_ENTRY
*CurrentFfsFile
;
97 UINT32 AuthenticationStatus
;
100 #define FV_DEVICE_FROM_THIS(a) CR (a, FV_DEVICE, Fv, FV_DEVICE_SIGNATURE)
103 Retrieves attributes, insures positive polarity of attribute bits, returns
104 resulting attributes in output parameter.
106 @param This Calling context
107 @param Attributes output buffer which contains attributes
109 @retval EFI_SUCCESS Successfully got volume attributes
114 FvGetVolumeAttributes (
115 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
116 OUT EFI_FV_ATTRIBUTES
*Attributes
120 Sets current attributes for volume.
122 @param This Calling context
123 @param Attributes On input, FvAttributes is a pointer to
124 an EFI_FV_ATTRIBUTES containing the
125 desired firmware volume settings. On
126 successful return, it contains the new
127 settings of the firmware volume. On
128 unsuccessful return, FvAttributes is not
129 modified and the firmware volume
130 settings are not changed.
132 @retval EFI_SUCCESS The requested firmware volume attributes
133 were set and the resulting
134 EFI_FV_ATTRIBUTES is returned in
136 @retval EFI_ACCESS_DENIED Atrribute is locked down.
137 @retval EFI_INVALID_PARAMETER Atrribute is not valid.
142 FvSetVolumeAttributes (
143 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
144 IN OUT EFI_FV_ATTRIBUTES
*Attributes
148 Given the input key, search for the next matching file in the volume.
150 @param This Indicates the calling context.
151 @param Key Key is a pointer to a caller allocated
152 buffer that contains implementation specific
153 data that is used to track where to begin
154 the search for the next file. The size of
155 the buffer must be at least This->KeySize
156 bytes long. To reinitialize the search and
157 begin from the beginning of the firmware
158 volume, the entire buffer must be cleared to
159 zero. Other than clearing the buffer to
160 initiate a new search, the caller must not
161 modify the data in the buffer between calls
163 @param FileType FileType is a pointer to a caller allocated
164 EFI_FV_FILETYPE. The GetNextFile() API can
165 filter it's search for files based on the
166 value of *FileType input. A *FileType input
167 of 0 causes GetNextFile() to search for
168 files of all types. If a file is found, the
169 file's type is returned in *FileType.
170 *FileType is not modified if no file is
172 @param NameGuid NameGuid is a pointer to a caller allocated
173 EFI_GUID. If a file is found, the file's
174 name is returned in *NameGuid. *NameGuid is
175 not modified if no file is found.
176 @param Attributes Attributes is a pointer to a caller
177 allocated EFI_FV_FILE_ATTRIBUTES. If a file
178 is found, the file's attributes are returned
179 in *Attributes. *Attributes is not modified
181 @param Size Size is a pointer to a caller allocated
182 UINTN. If a file is found, the file's size
183 is returned in *Size. *Size is not modified
186 @retval EFI_SUCCESS Successfully find the file.
187 @retval EFI_DEVICE_ERROR Device error.
188 @retval EFI_ACCESS_DENIED Fv could not read.
189 @retval EFI_NOT_FOUND No matching file found.
190 @retval EFI_INVALID_PARAMETER Invalid parameter
196 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
198 IN OUT EFI_FV_FILETYPE
*FileType
,
199 OUT EFI_GUID
*NameGuid
,
200 OUT EFI_FV_FILE_ATTRIBUTES
*Attributes
,
205 Locates a file in the firmware volume and
206 copies it to the supplied buffer.
208 @param This Indicates the calling context.
209 @param NameGuid Pointer to an EFI_GUID, which is the
211 @param Buffer Buffer is a pointer to pointer to a buffer
212 in which the file or section contents or are
214 @param BufferSize BufferSize is a pointer to caller allocated
215 UINTN. On input *BufferSize indicates the
216 size in bytes of the memory region pointed
217 to by Buffer. On output, *BufferSize
218 contains the number of bytes required to
220 @param FoundType FoundType is a pointer to a caller allocated
221 EFI_FV_FILETYPE that on successful return
222 from Read() contains the type of file read.
223 This output reflects the file type
224 irrespective of the value of the SectionType
226 @param FileAttributes FileAttributes is a pointer to a caller
227 allocated EFI_FV_FILE_ATTRIBUTES. On
228 successful return from Read(),
229 *FileAttributes contains the attributes of
231 @param AuthenticationStatus AuthenticationStatus is a pointer to a
232 caller allocated UINTN in which the
233 authentication status is returned.
235 @retval EFI_SUCCESS Successfully read to memory buffer.
236 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.
237 @retval EFI_NOT_FOUND Not found.
238 @retval EFI_DEVICE_ERROR Device error.
239 @retval EFI_ACCESS_DENIED Could not read.
240 @retval EFI_INVALID_PARAMETER Invalid parameter.
241 @retval EFI_OUT_OF_RESOURCES Not enough buffer to be allocated.
247 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
248 IN CONST EFI_GUID
*NameGuid
,
249 IN OUT VOID
**Buffer
,
250 IN OUT UINTN
*BufferSize
,
251 OUT EFI_FV_FILETYPE
*FoundType
,
252 OUT EFI_FV_FILE_ATTRIBUTES
*FileAttributes
,
253 OUT UINT32
*AuthenticationStatus
257 Locates a section in a given FFS File and
258 copies it to the supplied buffer (not including section header).
260 @param This Indicates the calling context.
261 @param NameGuid Pointer to an EFI_GUID, which is the
263 @param SectionType Indicates the section type to return.
264 @param SectionInstance Indicates which instance of sections with a
265 type of SectionType to return.
266 @param Buffer Buffer is a pointer to pointer to a buffer
267 in which the file or section contents or are
269 @param BufferSize BufferSize is a pointer to caller allocated
271 @param AuthenticationStatus AuthenticationStatus is a pointer to a
272 caller allocated UINT32 in which the
273 authentication status is returned.
275 @retval EFI_SUCCESS Successfully read the file section into
277 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.
278 @retval EFI_NOT_FOUND Section not found.
279 @retval EFI_DEVICE_ERROR Device error.
280 @retval EFI_ACCESS_DENIED Could not read.
281 @retval EFI_INVALID_PARAMETER Invalid parameter.
287 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
288 IN CONST EFI_GUID
*NameGuid
,
289 IN EFI_SECTION_TYPE SectionType
,
290 IN UINTN SectionInstance
,
291 IN OUT VOID
**Buffer
,
292 IN OUT UINTN
*BufferSize
,
293 OUT UINT32
*AuthenticationStatus
297 Writes one or more files to the firmware volume.
299 @param This Indicates the calling context.
300 @param NumberOfFiles Number of files.
301 @param WritePolicy WritePolicy indicates the level of reliability
302 for the write in the event of a power failure or
303 other system failure during the write operation.
304 @param FileData FileData is an pointer to an array of
305 EFI_FV_WRITE_DATA. Each element of array
306 FileData represents a file to be written.
308 @retval EFI_SUCCESS Files successfully written to firmware volume
309 @retval EFI_OUT_OF_RESOURCES Not enough buffer to be allocated.
310 @retval EFI_DEVICE_ERROR Device error.
311 @retval EFI_WRITE_PROTECTED Write protected.
312 @retval EFI_NOT_FOUND Not found.
313 @retval EFI_INVALID_PARAMETER Invalid parameter.
314 @retval EFI_UNSUPPORTED This function not supported.
320 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
321 IN UINT32 NumberOfFiles
,
322 IN EFI_FV_WRITE_POLICY WritePolicy
,
323 IN EFI_FV_WRITE_FILE_DATA
*FileData
327 Return information of type InformationType for the requested firmware
330 @param This Pointer to EFI_FIRMWARE_VOLUME2_PROTOCOL.
331 @param InformationType InformationType for requested.
332 @param BufferSize On input, size of Buffer.On output, the amount of
333 data returned in Buffer.
334 @param Buffer A poniter to the data buffer to return.
336 @return EFI_UNSUPPORTED Could not get.
342 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
343 IN CONST EFI_GUID
*InformationType
,
344 IN OUT UINTN
*BufferSize
,
350 Set information with InformationType into the requested firmware volume.
352 @param This Pointer to EFI_FIRMWARE_VOLUME2_PROTOCOL.
353 @param InformationType InformationType for requested.
354 @param BufferSize Size of Buffer data.
355 @param Buffer A poniter to the data buffer to be set.
357 @retval EFI_UNSUPPORTED Could not set.
363 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
364 IN CONST EFI_GUID
*InformationType
,
366 IN CONST VOID
*Buffer
370 Writes data beginning at Lba:Offset from FV. The write terminates either
371 when *NumBytes of data have been written, or when the firmware end is
372 reached. *NumBytes is updated to reflect the actual number of bytes
375 @param FvDevice Cached Firmware Volume
376 @param Offset Offset in the block at which to begin write
377 @param NumBytes At input, indicates the requested write size.
378 At output, indicates the actual number of bytes written.
379 @param Buffer Buffer containing source data for the write.
381 @retval EFI_SUCCESS Data is successfully written into FV.
382 @return error Data is failed written.
387 IN FV_DEVICE
*FvDevice
,
389 IN OUT UINTN
*NumBytes
,
395 Check if a block of buffer is erased.
397 @param ErasePolarity Erase polarity attribute of the firmware volume
398 @param Buffer The buffer to be checked
399 @param BufferSize Size of the buffer in bytes
401 @retval TRUE The block of buffer is erased
402 @retval FALSE The block of buffer is not erased
407 IN UINT8 ErasePolarity
,
413 Get the FFS file state by checking the highest bit set in the header's state field.
415 @param ErasePolarity Erase polarity attribute of the firmware volume
416 @param FfsHeader Points to the FFS file header
418 @return FFS File state
423 IN UINT8 ErasePolarity
,
424 IN EFI_FFS_FILE_HEADER
*FfsHeader
428 Verify checksum of the firmware volume header.
430 @param FvHeader Points to the firmware volume header to be checked
432 @retval TRUE Checksum verification passed
433 @retval FALSE Checksum verification failed
437 VerifyFvHeaderChecksum (
438 IN EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
442 Check if it's a valid FFS file header.
444 @param ErasePolarity Erase polarity attribute of the firmware volume
445 @param FfsHeader Points to the FFS file header to be checked
447 @retval TRUE Valid FFS file header
448 @retval FALSE Invalid FFS file header
453 IN UINT8 ErasePolarity
,
454 IN EFI_FFS_FILE_HEADER
*FfsHeader
458 Check if it's a valid FFS file.
459 Here we are sure that it has a valid FFS file header since we must call IsValidFfsHeader() first.
461 @param FvDevice Cached FV image.
462 @param FfsHeader Points to the FFS file to be checked
464 @retval TRUE Valid FFS file
465 @retval FALSE Invalid FFS file
470 IN FV_DEVICE
*FvDevice
,
471 IN EFI_FFS_FILE_HEADER
*FfsHeader
475 Given the supplied FW_VOL_BLOCK_PROTOCOL, allocate a buffer for output and
476 copy the real length volume header into it.
478 @param Fvb The FW_VOL_BLOCK_PROTOCOL instance from which to
479 read the volume header
480 @param FwVolHeader Pointer to pointer to allocated buffer in which
481 the volume header is returned.
483 @retval EFI_OUT_OF_RESOURCES No enough buffer could be allocated.
484 @retval EFI_SUCCESS Successfully read volume header to the allocated
486 @retval EFI_ACCESS_DENIED Read status of FV is not enabled.
487 @retval EFI_INVALID_PARAMETER The FV Header signature is not as expected or
488 the file system could not be understood.
492 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
493 OUT EFI_FIRMWARE_VOLUME_HEADER
**FwVolHeader
497 Convert the Buffer Address to LBA Entry Address.
499 @param FvDevice Cached FvDevice
500 @param BufferAddress Address of Buffer
501 @param LbaListEntry Pointer to the got LBA entry that contains the address.
503 @retval EFI_NOT_FOUND Buffer address is out of FvDevice.
504 @retval EFI_SUCCESS LBA entry is found for Buffer address.
509 IN FV_DEVICE
*FvDevice
,
510 IN EFI_PHYSICAL_ADDRESS BufferAddress
,
511 OUT LBA_ENTRY
**LbaListEntry
515 Convert the Buffer Address to LBA Address & Offset.
517 @param FvDevice Cached FvDevice
518 @param BufferAddress Address of Buffer
519 @param Lba Pointer to the gob Lba value
520 @param Offset Pointer to the got Offset
522 @retval EFI_NOT_FOUND Buffer address is out of FvDevice.
523 @retval EFI_SUCCESS LBA and Offset is found for Buffer address.
528 IN FV_DEVICE
*FvDevice
,
529 IN EFI_PHYSICAL_ADDRESS BufferAddress
,
535 Set File State in the FfsHeader.
537 @param State File state to be set into FFS header.
538 @param FfsHeader Points to the FFS file header
544 IN EFI_FFS_FILE_HEADER
*FfsHeader
548 Create a PAD File in the Free Space.
550 @param FvDevice Firmware Volume Device.
551 @param FreeSpaceEntry Indicating in which Free Space(Cache) the Pad file will be inserted.
552 @param Size Pad file Size, not include the header.
553 @param PadFileEntry The Ffs File Entry that points to this Pad File.
555 @retval EFI_SUCCESS Successfully create a PAD file.
556 @retval EFI_OUT_OF_RESOURCES No enough free space to create a PAD file.
557 @retval EFI_INVALID_PARAMETER Size is not 8 byte alignment.
558 @retval EFI_DEVICE_ERROR Free space is not erased.
561 FvCreatePadFileInFreeSpace (
562 IN FV_DEVICE
*FvDevice
,
563 IN FREE_SPACE_ENTRY
*FreeSpaceEntry
,
565 OUT FFS_FILE_LIST_ENTRY
**PadFileEntry
569 Create a new file within a PAD file area.
571 @param FvDevice Firmware Volume Device.
572 @param FfsFileBuffer A buffer that holds an FFS file,(it contains a File Header which is in init state).
573 @param BufferSize The size of FfsFileBuffer.
574 @param ActualFileSize The actual file length, it may not be multiples of 8.
575 @param FileName The FFS File Name.
576 @param FileType The FFS File Type.
577 @param FileAttributes The Attributes of the FFS File to be created.
579 @retval EFI_SUCCESS Successfully create a new file within the found PAD file area.
580 @retval EFI_OUT_OF_RESOURCES No suitable PAD file is found.
581 @retval other errors New file is created failed.
585 FvCreateNewFileInsidePadFile (
586 IN FV_DEVICE
*FvDevice
,
587 IN UINT8
*FfsFileBuffer
,
589 IN UINTN ActualFileSize
,
590 IN EFI_GUID
*FileName
,
591 IN EFI_FV_FILETYPE FileType
,
592 IN EFI_FV_FILE_ATTRIBUTES FileAttributes
596 Write multiple files into FV in reliable method.
598 @param FvDevice Firmware Volume Device.
599 @param NumOfFiles Total File number to be written.
600 @param FileData The array of EFI_FV_WRITE_FILE_DATA structure,
601 used to get name, attributes, type, etc
602 @param FileOperation The array of operation for each file.
604 @retval EFI_SUCCESS Files are added into FV.
605 @retval EFI_OUT_OF_RESOURCES No enough free PAD files to add the input files.
606 @retval EFI_INVALID_PARAMETER File number is less than or equal to 1.
607 @retval EFI_UNSUPPORTED File number exceeds the supported max numbers of files.
611 FvCreateMultipleFiles (
612 IN FV_DEVICE
*FvDevice
,
614 IN EFI_FV_WRITE_FILE_DATA
*FileData
,
615 IN BOOLEAN
*FileOperation
619 Calculate the checksum for the FFS header.
621 @param FfsHeader FFS File Header which needs to calculate the checksum
626 IN EFI_FFS_FILE_HEADER
*FfsHeader
630 Calculate the checksum for the FFS File.
632 @param FfsHeader FFS File Header which needs to calculate the checksum
633 @param ActualFileSize The whole Ffs File Length.
638 IN EFI_FFS_FILE_HEADER
*FfsHeader
,
639 IN UINTN ActualFileSize
643 Get the alignment value from File Attributes.
645 @param FfsAttributes FFS attribute
647 @return Alignment value.
651 GetRequiredAlignment (
652 IN EFI_FV_FILE_ATTRIBUTES FfsAttributes
656 Locate Pad File for writing, this is got from FV Cache.
658 @param FvDevice Cached Firmware Volume.
659 @param Size The required FFS file size.
660 @param RequiredAlignment FFS File Data alignment requirement.
661 @param PadSize Pointer to the size of leading Pad File.
662 @param PadFileEntry Pointer to the Pad File Entry that meets the requirement.
664 @retval EFI_SUCCESS The required pad file is found.
665 @retval EFI_NOT_FOUND The required pad file can't be found.
670 IN FV_DEVICE
*FvDevice
,
672 IN UINTN RequiredAlignment
,
674 OUT FFS_FILE_LIST_ENTRY
**PadFileEntry
678 Locate a suitable pad file for multiple file writing.
680 @param FvDevice Cached Firmware Volume.
681 @param NumOfFiles The number of Files that needed updating
682 @param BufferSize The array of each file size.
683 @param RequiredAlignment The array of of FFS File Data alignment requirement.
684 @param PadSize The array of size of each leading Pad File.
685 @param TotalSizeNeeded The totalsize that can hold these files.
686 @param PadFileEntry Pointer to the Pad File Entry that meets the requirement.
688 @retval EFI_SUCCESS The required pad file is found.
689 @retval EFI_NOT_FOUND The required pad file can't be found.
693 FvSearchSuitablePadFile (
694 IN FV_DEVICE
*FvDevice
,
696 IN UINTN
*BufferSize
,
697 IN UINTN
*RequiredAlignment
,
699 OUT UINTN
*TotalSizeNeeded
,
700 OUT FFS_FILE_LIST_ENTRY
**PadFileEntry
704 Locate a Free Space entry which can hold these files, including
705 meeting the alignment requirements.
707 @param FvDevice Cached Firmware Volume.
708 @param NumOfFiles The number of Files that needed updating
709 @param BufferSize The array of each file size.
710 @param RequiredAlignment The array of of FFS File Data alignment requirement.
711 @param PadSize The array of size of each leading Pad File.
712 @param TotalSizeNeeded The got total size that can hold these files.
713 @param FreeSpaceEntry The Free Space Entry that can hold these files.
715 @retval EFI_SUCCESS The free space entry is found.
716 @retval EFI_NOT_FOUND The free space entry can't be found.
720 FvSearchSuitableFreeSpace (
721 IN FV_DEVICE
*FvDevice
,
723 IN UINTN
*BufferSize
,
724 IN UINTN
*RequiredAlignment
,
726 OUT UINTN
*TotalSizeNeeded
,
727 OUT FREE_SPACE_ENTRY
**FreeSpaceEntry
731 Change FFS file header state and write to FV.
733 @param FvDevice Cached FV image.
734 @param FfsHeader Points to the FFS file header to be updated.
735 @param State FFS file state to be set.
737 @retval EFI_SUCCESS File state is writen into FV.
738 @retval others File state can't be writen into FV.
743 IN FV_DEVICE
*FvDevice
,
744 IN EFI_FFS_FILE_HEADER
*FfsHeader
,
745 IN EFI_FFS_FILE_STATE State
749 Convert EFI_FV_FILE_ATTRIBUTES to FFS_FILE_ATTRIBUTES.
751 @param FvFileAttrib The value of EFI_FV_FILE_ATTRIBUTES
752 @param FfsFileAttrib Pointer to the got FFS_FILE_ATTRIBUTES value.
756 FvFileAttrib2FfsFileAttrib (
757 IN EFI_FV_FILE_ATTRIBUTES FvFileAttrib
,
758 OUT UINT8
*FfsFileAttrib