2 Common defines and definitions for a FwVolDxe driver.
4 Copyright (c) 2006 - 2011, 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 caculate 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
;
99 #define FV_DEVICE_FROM_THIS(a) CR (a, FV_DEVICE, Fv, FV_DEVICE_SIGNATURE)
102 Retrieves attributes, insures positive polarity of attribute bits, returns
103 resulting attributes in output parameter.
105 @param This Calling context
106 @param Attributes output buffer which contains attributes
108 @retval EFI_SUCCESS Successfully got volume attributes
113 FvGetVolumeAttributes (
114 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
115 OUT EFI_FV_ATTRIBUTES
*Attributes
119 Sets current attributes for volume.
121 @param This Calling context
122 @param Attributes On input, FvAttributes is a pointer to
123 an EFI_FV_ATTRIBUTES containing the
124 desired firmware volume settings. On
125 successful return, it contains the new
126 settings of the firmware volume. On
127 unsuccessful return, FvAttributes is not
128 modified and the firmware volume
129 settings are not changed.
131 @retval EFI_SUCCESS The requested firmware volume attributes
132 were set and the resulting
133 EFI_FV_ATTRIBUTES is returned in
135 @retval EFI_ACCESS_DENIED Atrribute is locked down.
136 @retval EFI_INVALID_PARAMETER Atrribute is not valid.
141 FvSetVolumeAttributes (
142 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
143 IN OUT EFI_FV_ATTRIBUTES
*Attributes
147 Given the input key, search for the next matching file in the volume.
149 @param This Indicates the calling context.
150 @param Key Key is a pointer to a caller allocated
151 buffer that contains implementation specific
152 data that is used to track where to begin
153 the search for the next file. The size of
154 the buffer must be at least This->KeySize
155 bytes long. To reinitialize the search and
156 begin from the beginning of the firmware
157 volume, the entire buffer must be cleared to
158 zero. Other than clearing the buffer to
159 initiate a new search, the caller must not
160 modify the data in the buffer between calls
162 @param FileType FileType is a pointer to a caller allocated
163 EFI_FV_FILETYPE. The GetNextFile() API can
164 filter it's search for files based on the
165 value of *FileType input. A *FileType input
166 of 0 causes GetNextFile() to search for
167 files of all types. If a file is found, the
168 file's type is returned in *FileType.
169 *FileType is not modified if no file is
171 @param NameGuid NameGuid is a pointer to a caller allocated
172 EFI_GUID. If a file is found, the file's
173 name is returned in *NameGuid. *NameGuid is
174 not modified if no file is found.
175 @param Attributes Attributes is a pointer to a caller
176 allocated EFI_FV_FILE_ATTRIBUTES. If a file
177 is found, the file's attributes are returned
178 in *Attributes. *Attributes is not modified
180 @param Size Size is a pointer to a caller allocated
181 UINTN. If a file is found, the file's size
182 is returned in *Size. *Size is not modified
185 @retval EFI_SUCCESS Successfully find the file.
186 @retval EFI_DEVICE_ERROR Device error.
187 @retval EFI_ACCESS_DENIED Fv could not read.
188 @retval EFI_NOT_FOUND No matching file found.
189 @retval EFI_INVALID_PARAMETER Invalid parameter
195 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
197 IN OUT EFI_FV_FILETYPE
*FileType
,
198 OUT EFI_GUID
*NameGuid
,
199 OUT EFI_FV_FILE_ATTRIBUTES
*Attributes
,
204 Locates a file in the firmware volume and
205 copies it to the supplied buffer.
207 @param This Indicates the calling context.
208 @param NameGuid Pointer to an EFI_GUID, which is the
210 @param Buffer Buffer is a pointer to pointer to a buffer
211 in which the file or section contents or are
213 @param BufferSize BufferSize is a pointer to caller allocated
214 UINTN. On input *BufferSize indicates the
215 size in bytes of the memory region pointed
216 to by Buffer. On output, *BufferSize
217 contains the number of bytes required to
219 @param FoundType FoundType is a pointer to a caller allocated
220 EFI_FV_FILETYPE that on successful return
221 from Read() contains the type of file read.
222 This output reflects the file type
223 irrespective of the value of the SectionType
225 @param FileAttributes FileAttributes is a pointer to a caller
226 allocated EFI_FV_FILE_ATTRIBUTES. On
227 successful return from Read(),
228 *FileAttributes contains the attributes of
230 @param AuthenticationStatus AuthenticationStatus is a pointer to a
231 caller allocated UINTN in which the
232 authentication status is returned.
234 @retval EFI_SUCCESS Successfully read to memory buffer.
235 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.
236 @retval EFI_NOT_FOUND Not found.
237 @retval EFI_DEVICE_ERROR Device error.
238 @retval EFI_ACCESS_DENIED Could not read.
239 @retval EFI_INVALID_PARAMETER Invalid parameter.
240 @retval EFI_OUT_OF_RESOURCES Not enough buffer to be allocated.
246 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
247 IN CONST EFI_GUID
*NameGuid
,
248 IN OUT VOID
**Buffer
,
249 IN OUT UINTN
*BufferSize
,
250 OUT EFI_FV_FILETYPE
*FoundType
,
251 OUT EFI_FV_FILE_ATTRIBUTES
*FileAttributes
,
252 OUT UINT32
*AuthenticationStatus
256 Locates a section in a given FFS File and
257 copies it to the supplied buffer (not including section header).
259 @param This Indicates the calling context.
260 @param NameGuid Pointer to an EFI_GUID, which is the
262 @param SectionType Indicates the section type to return.
263 @param SectionInstance Indicates which instance of sections with a
264 type of SectionType to return.
265 @param Buffer Buffer is a pointer to pointer to a buffer
266 in which the file or section contents or are
268 @param BufferSize BufferSize is a pointer to caller allocated
270 @param AuthenticationStatus AuthenticationStatus is a pointer to a
271 caller allocated UINT32 in which the
272 authentication status is returned.
274 @retval EFI_SUCCESS Successfully read the file section into
276 @retval EFI_WARN_BUFFER_TOO_SMALL Buffer too small.
277 @retval EFI_NOT_FOUND Section not found.
278 @retval EFI_DEVICE_ERROR Device error.
279 @retval EFI_ACCESS_DENIED Could not read.
280 @retval EFI_INVALID_PARAMETER Invalid parameter.
286 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
287 IN CONST EFI_GUID
*NameGuid
,
288 IN EFI_SECTION_TYPE SectionType
,
289 IN UINTN SectionInstance
,
290 IN OUT VOID
**Buffer
,
291 IN OUT UINTN
*BufferSize
,
292 OUT UINT32
*AuthenticationStatus
296 Writes one or more files to the firmware volume.
298 @param This Indicates the calling context.
299 @param NumberOfFiles Number of files.
300 @param WritePolicy WritePolicy indicates the level of reliability
301 for the write in the event of a power failure or
302 other system failure during the write operation.
303 @param FileData FileData is an pointer to an array of
304 EFI_FV_WRITE_DATA. Each element of array
305 FileData represents a file to be written.
307 @retval EFI_SUCCESS Files successfully written to firmware volume
308 @retval EFI_OUT_OF_RESOURCES Not enough buffer to be allocated.
309 @retval EFI_DEVICE_ERROR Device error.
310 @retval EFI_WRITE_PROTECTED Write protected.
311 @retval EFI_NOT_FOUND Not found.
312 @retval EFI_INVALID_PARAMETER Invalid parameter.
313 @retval EFI_UNSUPPORTED This function not supported.
319 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
320 IN UINT32 NumberOfFiles
,
321 IN EFI_FV_WRITE_POLICY WritePolicy
,
322 IN EFI_FV_WRITE_FILE_DATA
*FileData
326 Return information of type InformationType for the requested firmware
329 @param This Pointer to EFI_FIRMWARE_VOLUME2_PROTOCOL.
330 @param InformationType InformationType for requested.
331 @param BufferSize On input, size of Buffer.On output, the amount of
332 data returned in Buffer.
333 @param Buffer A poniter to the data buffer to return.
335 @return EFI_UNSUPPORTED Could not get.
341 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
342 IN CONST EFI_GUID
*InformationType
,
343 IN OUT UINTN
*BufferSize
,
349 Set information with InformationType into the requested firmware volume.
351 @param This Pointer to EFI_FIRMWARE_VOLUME2_PROTOCOL.
352 @param InformationType InformationType for requested.
353 @param BufferSize Size of Buffer data.
354 @param Buffer A poniter to the data buffer to be set.
356 @retval EFI_UNSUPPORTED Could not set.
362 IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL
*This
,
363 IN CONST EFI_GUID
*InformationType
,
365 IN CONST VOID
*Buffer
369 Writes data beginning at Lba:Offset from FV. The write terminates either
370 when *NumBytes of data have been written, or when the firmware end is
371 reached. *NumBytes is updated to reflect the actual number of bytes
374 @param FvDevice Cached Firmware Volume
375 @param Offset Offset in the block at which to begin write
376 @param NumBytes At input, indicates the requested write size.
377 At output, indicates the actual number of bytes written.
378 @param Buffer Buffer containing source data for the write.
380 @retval EFI_SUCCESS Data is successfully written into FV.
381 @return error Data is failed written.
386 IN FV_DEVICE
*FvDevice
,
388 IN OUT UINTN
*NumBytes
,
394 Check if a block of buffer is erased.
396 @param ErasePolarity Erase polarity attribute of the firmware volume
397 @param Buffer The buffer to be checked
398 @param BufferSize Size of the buffer in bytes
400 @retval TRUE The block of buffer is erased
401 @retval FALSE The block of buffer is not erased
406 IN UINT8 ErasePolarity
,
412 Get the FFS file state by checking the highest bit set in the header's state field.
414 @param ErasePolarity Erase polarity attribute of the firmware volume
415 @param FfsHeader Points to the FFS file header
417 @return FFS File state
422 IN UINT8 ErasePolarity
,
423 IN EFI_FFS_FILE_HEADER
*FfsHeader
427 Verify checksum of the firmware volume header.
429 @param FvHeader Points to the firmware volume header to be checked
431 @retval TRUE Checksum verification passed
432 @retval FALSE Checksum verification failed
436 VerifyFvHeaderChecksum (
437 IN EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
441 Check if it's a valid FFS file header.
443 @param ErasePolarity Erase polarity attribute of the firmware volume
444 @param FfsHeader Points to the FFS file header to be checked
446 @retval TRUE Valid FFS file header
447 @retval FALSE Invalid FFS file header
452 IN UINT8 ErasePolarity
,
453 IN EFI_FFS_FILE_HEADER
*FfsHeader
457 Check if it's a valid FFS file.
458 Here we are sure that it has a valid FFS file header since we must call IsValidFfsHeader() first.
460 @param FvDevice Cached FV image.
461 @param FfsHeader Points to the FFS file to be checked
463 @retval TRUE Valid FFS file
464 @retval FALSE Invalid FFS file
469 IN FV_DEVICE
*FvDevice
,
470 IN EFI_FFS_FILE_HEADER
*FfsHeader
474 Given the supplied FW_VOL_BLOCK_PROTOCOL, allocate a buffer for output and
475 copy the real length volume header into it.
477 @param Fvb The FW_VOL_BLOCK_PROTOCOL instance from which to
478 read the volume header
479 @param FwVolHeader Pointer to pointer to allocated buffer in which
480 the volume header is returned.
482 @retval EFI_OUT_OF_RESOURCES No enough buffer could be allocated.
483 @retval EFI_SUCCESS Successfully read volume header to the allocated
485 @retval EFI_ACCESS_DENIED Read status of FV is not enabled.
489 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
,
490 OUT EFI_FIRMWARE_VOLUME_HEADER
**FwVolHeader
494 Convert the Buffer Address to LBA Entry Address.
496 @param FvDevice Cached FvDevice
497 @param BufferAddress Address of Buffer
498 @param LbaListEntry Pointer to the got LBA entry that contains the address.
500 @retval EFI_NOT_FOUND Buffer address is out of FvDevice.
501 @retval EFI_SUCCESS LBA entry is found for Buffer address.
506 IN FV_DEVICE
*FvDevice
,
507 IN EFI_PHYSICAL_ADDRESS BufferAddress
,
508 OUT LBA_ENTRY
**LbaListEntry
512 Convert the Buffer Address to LBA Address & Offset.
514 @param FvDevice Cached FvDevice
515 @param BufferAddress Address of Buffer
516 @param Lba Pointer to the gob Lba value
517 @param Offset Pointer to the got Offset
519 @retval EFI_NOT_FOUND Buffer address is out of FvDevice.
520 @retval EFI_SUCCESS LBA and Offset is found for Buffer address.
525 IN FV_DEVICE
*FvDevice
,
526 IN EFI_PHYSICAL_ADDRESS BufferAddress
,
532 Set File State in the FfsHeader.
534 @param State File state to be set into FFS header.
535 @param FfsHeader Points to the FFS file header
541 IN EFI_FFS_FILE_HEADER
*FfsHeader
545 Create a PAD File in the Free Space.
547 @param FvDevice Firmware Volume Device.
548 @param FreeSpaceEntry Indicating in which Free Space(Cache) the Pad file will be inserted.
549 @param Size Pad file Size, not include the header.
550 @param PadFileEntry The Ffs File Entry that points to this Pad File.
552 @retval EFI_SUCCESS Successfully create a PAD file.
553 @retval EFI_OUT_OF_RESOURCES No enough free space to create a PAD file.
554 @retval EFI_INVALID_PARAMETER Size is not 8 byte alignment.
555 @retval EFI_DEVICE_ERROR Free space is not erased.
558 FvCreatePadFileInFreeSpace (
559 IN FV_DEVICE
*FvDevice
,
560 IN FREE_SPACE_ENTRY
*FreeSpaceEntry
,
562 OUT FFS_FILE_LIST_ENTRY
**PadFileEntry
566 Create a new file within a PAD file area.
568 @param FvDevice Firmware Volume Device.
569 @param FfsFileBuffer A buffer that holds an FFS file,(it contains a File Header which is in init state).
570 @param BufferSize The size of FfsFileBuffer.
571 @param ActualFileSize The actual file length, it may not be multiples of 8.
572 @param FileName The FFS File Name.
573 @param FileType The FFS File Type.
574 @param FileAttributes The Attributes of the FFS File to be created.
576 @retval EFI_SUCCESS Successfully create a new file within the found PAD file area.
577 @retval EFI_OUT_OF_RESOURCES No suitable PAD file is found.
578 @retval other errors New file is created failed.
582 FvCreateNewFileInsidePadFile (
583 IN FV_DEVICE
*FvDevice
,
584 IN UINT8
*FfsFileBuffer
,
586 IN UINTN ActualFileSize
,
587 IN EFI_GUID
*FileName
,
588 IN EFI_FV_FILETYPE FileType
,
589 IN EFI_FV_FILE_ATTRIBUTES FileAttributes
593 Write multiple files into FV in reliable method.
595 @param FvDevice Firmware Volume Device.
596 @param NumOfFiles Total File number to be written.
597 @param FileData The array of EFI_FV_WRITE_FILE_DATA structure,
598 used to get name, attributes, type, etc
599 @param FileOperation The array of operation for each file.
601 @retval EFI_SUCCESS Files are added into FV.
602 @retval EFI_OUT_OF_RESOURCES No enough free PAD files to add the input files.
603 @retval EFI_INVALID_PARAMETER File number is less than or equal to 1.
604 @retval EFI_UNSUPPORTED File number exceeds the supported max numbers of files.
608 FvCreateMultipleFiles (
609 IN FV_DEVICE
*FvDevice
,
611 IN EFI_FV_WRITE_FILE_DATA
*FileData
,
612 IN BOOLEAN
*FileOperation
616 Caculate the checksum for the FFS header.
618 @param FfsHeader FFS File Header which needs to caculate the checksum
623 IN EFI_FFS_FILE_HEADER
*FfsHeader
627 Caculate the checksum for the FFS File.
629 @param FfsHeader FFS File Header which needs to caculate the checksum
630 @param ActualFileSize The whole Ffs File Length.
635 IN EFI_FFS_FILE_HEADER
*FfsHeader
,
636 IN UINTN ActualFileSize
640 Get the alignment value from File Attributes.
642 @param FfsAttributes FFS attribute
644 @return Alignment value.
648 GetRequiredAlignment (
649 IN EFI_FV_FILE_ATTRIBUTES FfsAttributes
653 Locate Pad File for writing, this is got from FV Cache.
655 @param FvDevice Cached Firmware Volume.
656 @param Size The required FFS file size.
657 @param RequiredAlignment FFS File Data alignment requirement.
658 @param PadSize Pointer to the size of leading Pad File.
659 @param PadFileEntry Pointer to the Pad File Entry that meets the requirement.
661 @retval EFI_SUCCESS The required pad file is found.
662 @retval EFI_NOT_FOUND The required pad file can't be found.
667 IN FV_DEVICE
*FvDevice
,
669 IN UINTN RequiredAlignment
,
671 OUT FFS_FILE_LIST_ENTRY
**PadFileEntry
675 Locate a suitable pad file for multiple file writing.
677 @param FvDevice Cached Firmware Volume.
678 @param NumOfFiles The number of Files that needed updating
679 @param BufferSize The array of each file size.
680 @param RequiredAlignment The array of of FFS File Data alignment requirement.
681 @param PadSize The array of size of each leading Pad File.
682 @param TotalSizeNeeded The totalsize that can hold these files.
683 @param PadFileEntry Pointer to the Pad File Entry that meets the requirement.
685 @retval EFI_SUCCESS The required pad file is found.
686 @retval EFI_NOT_FOUND The required pad file can't be found.
690 FvSearchSuitablePadFile (
691 IN FV_DEVICE
*FvDevice
,
693 IN UINTN
*BufferSize
,
694 IN UINTN
*RequiredAlignment
,
696 OUT UINTN
*TotalSizeNeeded
,
697 OUT FFS_FILE_LIST_ENTRY
**PadFileEntry
701 Locate a Free Space entry which can hold these files, including
702 meeting the alignment requirements.
704 @param FvDevice Cached Firmware Volume.
705 @param NumOfFiles The number of Files that needed updating
706 @param BufferSize The array of each file size.
707 @param RequiredAlignment The array of of FFS File Data alignment requirement.
708 @param PadSize The array of size of each leading Pad File.
709 @param TotalSizeNeeded The got total size that can hold these files.
710 @param FreeSpaceEntry The Free Space Entry that can hold these files.
712 @retval EFI_SUCCESS The free space entry is found.
713 @retval EFI_NOT_FOUND The free space entry can't be found.
717 FvSearchSuitableFreeSpace (
718 IN FV_DEVICE
*FvDevice
,
720 IN UINTN
*BufferSize
,
721 IN UINTN
*RequiredAlignment
,
723 OUT UINTN
*TotalSizeNeeded
,
724 OUT FREE_SPACE_ENTRY
**FreeSpaceEntry
728 Change FFS file header state and write to FV.
730 @param FvDevice Cached FV image.
731 @param FfsHeader Points to the FFS file header to be updated.
732 @param State FFS file state to be set.
734 @retval EFI_SUCCESS File state is writen into FV.
735 @retval others File state can't be writen into FV.
740 IN FV_DEVICE
*FvDevice
,
741 IN EFI_FFS_FILE_HEADER
*FfsHeader
,
742 IN EFI_FFS_FILE_STATE State
746 Convert EFI_FV_FILE_ATTRIBUTES to FFS_FILE_ATTRIBUTES.
748 @param FvFileAttrib The value of EFI_FV_FILE_ATTRIBUTES
749 @param FfsFileAttrib Pointer to the got FFS_FILE_ATTRIBUTES value.
753 FvFileAttrib2FfsFileAttrib (
754 IN EFI_FV_FILE_ATTRIBUTES FvFileAttrib
,
755 OUT UINT8
*FfsFileAttrib