2 UEFI PI specification supersedes Inte's Framework Specification.
3 EFI_FIRMWARE_VOLUME_PROTOCOL defined in Intel Framework Pkg is replaced by
4 EFI_FIRMWARE_VOLUME2_PROTOCOL in MdePkg.
5 This module produces FV on top of FV2. This module is used on platform when both of
6 these two conditions are true:
7 1) Framework module consuming FV is present
8 2) And the platform only produces FV2
10 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
11 This program and the accompanying materials
12 are licensed and made available under the terms and conditions of the BSD License
13 which accompanies this distribution. The full text of the license may be found at
14 http://opensource.org/licenses/bsd-license.php
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 #include <Protocol/FirmwareVolume2.h>
24 #include <Protocol/FirmwareVolume.h>
25 #include <Library/BaseLib.h>
26 #include <Library/DebugLib.h>
27 #include <Library/UefiBootServicesTableLib.h>
28 #include <Library/UefiDriverEntryPoint.h>
29 #include <Library/UefiLib.h>
30 #include <Library/MemoryAllocationLib.h>
32 #define FIRMWARE_VOLUME_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('f', 'v', 't', 'h')
36 EFI_FIRMWARE_VOLUME_PROTOCOL FirmwareVolume
;
37 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FirmwareVolume2
;
38 } FIRMWARE_VOLUME_PRIVATE_DATA
;
40 #define FIRMWARE_VOLUME_PRIVATE_DATA_FROM_THIS(a) CR (a, FIRMWARE_VOLUME_PRIVATE_DATA, FirmwareVolume, FIRMWARE_VOLUME_PRIVATE_DATA_SIGNATURE)
43 Convert FV attrbiutes to FV2 attributes.
45 @param Fv2Attributes FV2 attributes.
47 @return FV attributes.
50 FRAMEWORK_EFI_FV_ATTRIBUTES
51 Fv2AttributesToFvAttributes (
52 IN EFI_FV_ATTRIBUTES Fv2Attributes
56 // Clear those filed that is not defined in Framework FV spec and Alignment conversion.
58 return (Fv2Attributes
& 0x1ff) | ((UINTN
) EFI_FV_ALIGNMENT_2
<< RShiftU64((Fv2Attributes
& EFI_FV2_ALIGNMENT
), 16));
62 Retrieves attributes, insures positive polarity of attribute bits, returns
63 resulting attributes in output parameter.
65 @param This Calling context
66 @param Attributes output buffer which contains attributes
68 @retval EFI_SUCCESS The firmware volume attributes were returned.
73 FvGetVolumeAttributes (
74 IN EFI_FIRMWARE_VOLUME_PROTOCOL
*This
,
75 OUT FRAMEWORK_EFI_FV_ATTRIBUTES
*Attributes
79 FIRMWARE_VOLUME_PRIVATE_DATA
*Private
;
80 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FirmwareVolume2
;
82 Private
= FIRMWARE_VOLUME_PRIVATE_DATA_FROM_THIS (This
);
83 FirmwareVolume2
= Private
->FirmwareVolume2
;
85 Status
= FirmwareVolume2
->GetVolumeAttributes (
89 if (!EFI_ERROR (Status
)) {
90 *Attributes
= Fv2AttributesToFvAttributes (*Attributes
);
96 Sets volume attributes.
98 @param This Calling context
99 @param Attributes Buffer which contains attributes
101 @retval EFI_INVALID_PARAMETER A bit in Attributes was invalid
102 @retval EFI_SUCCESS The requested firmware volume attributes were set
103 and the resulting EFI_FV_ATTRIBUTES is returned in
105 @retval EFI_ACCESS_DENIED The Device is locked and does not permit modification.
110 FvSetVolumeAttributes (
111 IN EFI_FIRMWARE_VOLUME_PROTOCOL
*This
,
112 IN OUT FRAMEWORK_EFI_FV_ATTRIBUTES
*Attributes
115 FIRMWARE_VOLUME_PRIVATE_DATA
*Private
;
116 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FirmwareVolume2
;
117 EFI_FV_ATTRIBUTES Fv2Attributes
;
120 Private
= FIRMWARE_VOLUME_PRIVATE_DATA_FROM_THIS (This
);
121 FirmwareVolume2
= Private
->FirmwareVolume2
;
123 Fv2Attributes
= (*Attributes
& 0x1ff);
124 Status
= FirmwareVolume2
->SetVolumeAttributes (
129 *Attributes
= Fv2AttributesToFvAttributes (Fv2Attributes
);
135 Read the requested file (NameGuid) and returns data in Buffer.
137 @param This Calling context
138 @param NameGuid Filename identifying which file to read
139 @param Buffer Pointer to pointer to buffer in which contents of file are returned.
141 If Buffer is NULL, only type, attributes, and size are returned as
142 there is no output buffer.
144 If Buffer != NULL and *Buffer == NULL, the output buffer is allocated
145 from BS pool by ReadFile
147 If Buffer != NULL and *Buffer != NULL, the output buffer has been
148 allocated by the caller and is being passed in.
149 @param BufferSize Indicates the buffer size passed in, and on output the size
150 required to complete the read
151 @param FoundType Indicates the type of the file who's data is returned
152 @param FileAttributes Indicates the attributes of the file who's data is resturned
153 @param AuthenticationStatus Indicates the authentication status of the data
155 @retval EFI_SUCCESS The call completed successfully
156 @retval EFI_WARN_BUFFER_TOO_SMALL The buffer is too small to contain the requested output.
157 The buffer is filled and the output is truncated.
158 @retval EFI_NOT_FOUND NameGuid was not found in the firmware volume.
159 @retval EFI_DEVICE_ERROR A hardware error occurred when attempting to access the firmware volume.
160 @retval EFI_ACCESS_DENIED The firmware volume is configured to disallow reads.
161 @retval EFI_OUT_OF_RESOURCES An allocation failure occurred.
167 IN EFI_FIRMWARE_VOLUME_PROTOCOL
*This
,
168 IN EFI_GUID
*NameGuid
,
169 IN OUT VOID
**Buffer
,
170 IN OUT UINTN
*BufferSize
,
171 OUT EFI_FV_FILETYPE
*FoundType
,
172 OUT EFI_FV_FILE_ATTRIBUTES
*FileAttributes
,
173 OUT UINT32
*AuthenticationStatus
176 FIRMWARE_VOLUME_PRIVATE_DATA
*Private
;
177 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FirmwareVolume2
;
180 Private
= FIRMWARE_VOLUME_PRIVATE_DATA_FROM_THIS (This
);
181 FirmwareVolume2
= Private
->FirmwareVolume2
;
183 Status
= FirmwareVolume2
->ReadFile (
194 // For Framework FV attrbutes, only alignment fields are valid.
196 *FileAttributes
= *FileAttributes
& EFI_FV_FILE_ATTRIB_ALIGNMENT
;
202 Read the requested section from the specified file and returns data in Buffer.
204 @param This Calling context
205 @param NameGuid Filename identifying the file from which to read
206 @param SectionType Indicates what section type to retrieve
207 @param SectionInstance Indicates which instance of SectionType to retrieve
208 @param Buffer Pointer to pointer to buffer in which contents of file are returned.
210 If Buffer is NULL, only type, attributes, and size are returned as
211 there is no output buffer.
213 If Buffer != NULL and *Buffer == NULL, the output buffer is allocated
214 from BS pool by ReadFile
216 If Buffer != NULL and *Buffer != NULL, the output buffer has been
217 allocated by the caller and is being passed in.
218 @param BufferSize Indicates the buffer size passed in, and on output the size
219 required to complete the read
220 @param AuthenticationStatus Indicates the authentication status of the data
222 @retval EFI_SUCCESS The call completed successfully.
223 @retval EFI_WARN_BUFFER_TOO_SMALL The buffer is too small to contain the requested output.
224 The buffer is filled and the output is truncated.
225 @retval EFI_OUT_OF_RESOURCES An allocation failure occurred.
226 @retval EFI_NOT_FOUND Name was not found in the firmware volume.
227 @retval EFI_DEVICE_ERROR A hardware error occurred when attempting to access the firmware volume.
228 @retval EFI_ACCESS_DENIED The firmware volume is configured to disallow reads.
234 IN EFI_FIRMWARE_VOLUME_PROTOCOL
*This
,
235 IN EFI_GUID
*NameGuid
,
236 IN EFI_SECTION_TYPE SectionType
,
237 IN UINTN SectionInstance
,
238 IN OUT VOID
**Buffer
,
239 IN OUT UINTN
*BufferSize
,
240 OUT UINT32
*AuthenticationStatus
243 FIRMWARE_VOLUME_PRIVATE_DATA
*Private
;
244 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FirmwareVolume2
;
246 Private
= FIRMWARE_VOLUME_PRIVATE_DATA_FROM_THIS (This
);
247 FirmwareVolume2
= Private
->FirmwareVolume2
;
249 return FirmwareVolume2
->ReadSection (
261 Write the supplied file (NameGuid) to the FV.
263 @param This Calling context
264 @param NumberOfFiles Indicates the number of file records pointed to by FileData
265 @param WritePolicy Indicates the level of reliability of the write with respect to
266 things like power failure events.
267 @param FileData A pointer to an array of EFI_FV_WRITE_FILE_DATA structures. Each
268 element in the array indicates a file to write, and there are
269 NumberOfFiles elements in the input array.
271 @retval EFI_SUCCESS The write completed successfully.
272 @retval EFI_OUT_OF_RESOURCES The firmware volume does not have enough free space to store file(s).
273 @retval EFI_DEVICE_ERROR A hardware error occurred when attempting to access the firmware volume.
274 @retval EFI_WRITE_PROTECTED The firmware volume is configured to disallow writes.
275 @retval EFI_NOT_FOUND A delete was requested, but the requested file was not
276 found in the firmware volume.
277 @retval EFI_INVALID_PARAMETER A delete was requested with a multiple file write.
278 An unsupported WritePolicy was requested.
279 An unknown file type was specified.
280 A file system specific error has occurred.
286 IN EFI_FIRMWARE_VOLUME_PROTOCOL
*This
,
287 IN UINT32 NumberOfFiles
,
288 IN FRAMEWORK_EFI_FV_WRITE_POLICY WritePolicy
,
289 IN FRAMEWORK_EFI_FV_WRITE_FILE_DATA
*FileData
292 FIRMWARE_VOLUME_PRIVATE_DATA
*Private
;
293 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FirmwareVolume2
;
294 EFI_FV_WRITE_FILE_DATA
*PiFileData
;
298 Private
= FIRMWARE_VOLUME_PRIVATE_DATA_FROM_THIS (This
);
299 FirmwareVolume2
= Private
->FirmwareVolume2
;
301 PiFileData
= AllocateCopyPool (sizeof (EFI_FV_WRITE_FILE_DATA
), FileData
);
302 ASSERT (PiFileData
!= NULL
);
305 // Framework Spec assume firmware files are Memory-Mapped.
307 for (Index
= 0; Index
< NumberOfFiles
; Index
++) {
308 PiFileData
[Index
].FileAttributes
|= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED
;
311 Status
= FirmwareVolume2
->WriteFile (
315 (EFI_FV_WRITE_FILE_DATA
*)FileData
318 FreePool (PiFileData
);
323 Given the input key, search for the next matching file in the volume.
325 @param This Calling context
326 @param Key Pointer to a caller allocated buffer that contains an implementation
327 specific key that is used to track where to begin searching on
329 @param FileType Indicates the file type to filter for
330 @param NameGuid Guid filename of the file found
331 @param Attributes Attributes of the file found
332 @param Size Size in bytes of the file found
334 @retval EFI_SUCCESS The output parameters are filled with data obtained from
335 the first matching file that was found.
336 @retval EFI_NOT_FOUND No files of type FileType were found.
337 @retval EFI_DEVICE_ERROR A hardware error occurred when attempting to access
339 @retval EFI_ACCESS_DENIED The firmware volume is configured to disallow reads.
345 IN EFI_FIRMWARE_VOLUME_PROTOCOL
*This
,
347 IN OUT EFI_FV_FILETYPE
*FileType
,
348 OUT EFI_GUID
*NameGuid
,
349 OUT EFI_FV_FILE_ATTRIBUTES
*Attributes
,
353 FIRMWARE_VOLUME_PRIVATE_DATA
*Private
;
354 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FirmwareVolume2
;
357 Private
= FIRMWARE_VOLUME_PRIVATE_DATA_FROM_THIS (This
);
358 FirmwareVolume2
= Private
->FirmwareVolume2
;
360 Status
= FirmwareVolume2
->GetNextFile (
370 // For Framework FV attrbutes, only alignment fields are valid.
372 *Attributes
= *Attributes
& EFI_FV_FILE_ATTRIB_ALIGNMENT
;
378 // Firmware Volume Protocol template
380 EFI_EVENT mFvRegistration
;
382 FIRMWARE_VOLUME_PRIVATE_DATA gFirmwareVolumePrivateDataTemplate
= {
383 FIRMWARE_VOLUME_PRIVATE_DATA_SIGNATURE
,
385 FvGetVolumeAttributes
,
386 FvSetVolumeAttributes
,
401 This notification function is invoked when an instance of the
402 EFI_FIRMWARE_VOLUME2_PROTOCOL is produced. It installs another instance of the
403 EFI_FIRMWARE_VOLUME_PROTOCOL on the same handle.
405 @param Event The event that occured
406 @param Context Context of event. Not used in this nofication function.
411 FvNotificationEvent (
419 FIRMWARE_VOLUME_PRIVATE_DATA
*Private
;
420 EFI_FIRMWARE_VOLUME_PROTOCOL
*FirmwareVolume
;
423 BufferSize
= sizeof (Handle
);
424 Status
= gBS
->LocateHandle (
426 &gEfiFirmwareVolume2ProtocolGuid
,
431 if (EFI_ERROR (Status
)) {
433 // Exit Path of While Loop....
439 // Skip this handle if the Firmware Volume Protocol is already installed
441 Status
= gBS
->HandleProtocol (
443 &gEfiFirmwareVolumeProtocolGuid
,
444 (VOID
**)&FirmwareVolume
446 if (!EFI_ERROR (Status
)) {
451 // Allocate private data structure
453 Private
= AllocateCopyPool (sizeof (FIRMWARE_VOLUME_PRIVATE_DATA
), &gFirmwareVolumePrivateDataTemplate
);
454 if (Private
== NULL
) {
459 // Retrieve the Firmware Volume2 Protocol
461 Status
= gBS
->HandleProtocol (
463 &gEfiFirmwareVolume2ProtocolGuid
,
464 (VOID
**)&Private
->FirmwareVolume2
466 ASSERT_EFI_ERROR (Status
);
469 // Fill in rest of private data structure
471 Private
->FirmwareVolume
.KeySize
= Private
->FirmwareVolume2
->KeySize
;
472 Private
->FirmwareVolume
.ParentHandle
= Private
->FirmwareVolume2
->ParentHandle
;
475 // Install Firmware Volume Protocol onto same handle
477 Status
= gBS
->InstallMultipleProtocolInterfaces (
479 &gEfiFirmwareVolumeProtocolGuid
,
480 &Private
->FirmwareVolume
,
483 ASSERT_EFI_ERROR (Status
);
489 The user Entry Point for DXE driver. The user code starts with this function
490 as the real entry point for the image goes into a library that calls this
493 @param[in] ImageHandle The firmware allocated handle for the EFI image.
494 @param[in] SystemTable A pointer to the EFI System Table.
496 @retval EFI_SUCCESS The entry point is executed successfully.
497 @retval other Some error occurs when executing this entry point.
502 InitializeFirmwareVolume2 (
503 IN EFI_HANDLE ImageHandle
,
504 IN EFI_SYSTEM_TABLE
*SystemTable
507 EfiCreateProtocolNotifyEvent (
508 &gEfiFirmwareVolume2ProtocolGuid
,