]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Compatibility/FvOnFv2Thunk/FvOnFv2Thunk.c
Maintainers.txt: Remove EdkCompatibilityPkg information
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / FvOnFv2Thunk / FvOnFv2Thunk.c
CommitLineData
0d694539 1/** @file\r
2UEFI PI specification supersedes Inte's Framework Specification.\r
3EFI_FIRMWARE_VOLUME_PROTOCOL defined in Intel Framework Pkg is replaced by\r
4EFI_FIRMWARE_VOLUME2_PROTOCOL in MdePkg.\r
5This module produces FV on top of FV2. This module is used on platform when both of\r
6these two conditions are true:\r
71) Framework module consuming FV is present\r
82) And the platform only produces FV2\r
4259256b 9\r
584d5652
HT
10Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
11This program and the accompanying materials\r
4259256b 12are licensed and made available under the terms and conditions of the BSD License\r
13which accompanies this distribution. The full text of the license may be found at\r
14http://opensource.org/licenses/bsd-license.php\r
15\r
16THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
17WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
18Module Name:\r
19\r
0d694539 20**/\r
4259256b 21\r
22#include <PiDxe.h>\r
23#include <Protocol/FirmwareVolume2.h>\r
24#include <Protocol/FirmwareVolume.h>\r
25#include <Library/BaseLib.h>\r
26#include <Library/DebugLib.h>\r
27#include <Library/UefiBootServicesTableLib.h>\r
28#include <Library/UefiDriverEntryPoint.h>\r
29#include <Library/UefiLib.h>\r
30#include <Library/MemoryAllocationLib.h>\r
31\r
a3a83173 32#define FIRMWARE_VOLUME_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('f', 'v', 't', 'h')\r
4259256b 33\r
34typedef struct {\r
35 UINTN Signature;\r
36 EFI_FIRMWARE_VOLUME_PROTOCOL FirmwareVolume;\r
37 EFI_FIRMWARE_VOLUME2_PROTOCOL *FirmwareVolume2;\r
38} FIRMWARE_VOLUME_PRIVATE_DATA;\r
39\r
40#define FIRMWARE_VOLUME_PRIVATE_DATA_FROM_THIS(a) CR (a, FIRMWARE_VOLUME_PRIVATE_DATA, FirmwareVolume, FIRMWARE_VOLUME_PRIVATE_DATA_SIGNATURE)\r
41\r
90eff005 42/**\r
43 Convert FV attrbiutes to FV2 attributes.\r
44\r
45 @param Fv2Attributes FV2 attributes.\r
46\r
47 @return FV attributes.\r
48\r
49**/\r
19caab51 50FRAMEWORK_EFI_FV_ATTRIBUTES\r
51Fv2AttributesToFvAttributes (\r
90eff005 52 IN EFI_FV_ATTRIBUTES Fv2Attributes\r
19caab51 53 )\r
54{\r
55 //\r
56 // Clear those filed that is not defined in Framework FV spec and Alignment conversion.\r
57 //\r
58 return (Fv2Attributes & 0x1ff) | ((UINTN) EFI_FV_ALIGNMENT_2 << RShiftU64((Fv2Attributes & EFI_FV2_ALIGNMENT), 16));\r
59}\r
60\r
4259256b 61/**\r
62 Retrieves attributes, insures positive polarity of attribute bits, returns\r
26a76fbc 63 resulting attributes in output parameter.\r
4259256b 64\r
65 @param This Calling context\r
66 @param Attributes output buffer which contains attributes\r
67\r
26a76fbc 68 @retval EFI_SUCCESS The firmware volume attributes were returned.\r
4259256b 69\r
70**/\r
71EFI_STATUS\r
72EFIAPI\r
73FvGetVolumeAttributes (\r
74 IN EFI_FIRMWARE_VOLUME_PROTOCOL *This,\r
75 OUT FRAMEWORK_EFI_FV_ATTRIBUTES *Attributes\r
76 )\r
77{\r
78 EFI_STATUS Status;\r
79 FIRMWARE_VOLUME_PRIVATE_DATA *Private;\r
80 EFI_FIRMWARE_VOLUME2_PROTOCOL *FirmwareVolume2;\r
81\r
82 Private = FIRMWARE_VOLUME_PRIVATE_DATA_FROM_THIS (This);\r
83 FirmwareVolume2 = Private->FirmwareVolume2;\r
84\r
85 Status = FirmwareVolume2->GetVolumeAttributes (\r
86 FirmwareVolume2,\r
87 Attributes\r
88 );\r
89 if (!EFI_ERROR (Status)) {\r
19caab51 90 *Attributes = Fv2AttributesToFvAttributes (*Attributes);\r
4259256b 91 }\r
92 return Status;\r
93}\r
94\r
95/**\r
26a76fbc 96 Sets volume attributes.\r
4259256b 97\r
98 @param This Calling context\r
99 @param Attributes Buffer which contains attributes\r
100\r
26a76fbc
LG
101 @retval EFI_INVALID_PARAMETER A bit in Attributes was invalid\r
102 @retval EFI_SUCCESS The requested firmware volume attributes were set \r
103 and the resulting EFI_FV_ATTRIBUTES is returned in\r
104 Attributes.\r
105 @retval EFI_ACCESS_DENIED The Device is locked and does not permit modification. \r
4259256b 106\r
107**/\r
108EFI_STATUS\r
109EFIAPI\r
110FvSetVolumeAttributes (\r
111 IN EFI_FIRMWARE_VOLUME_PROTOCOL *This,\r
112 IN OUT FRAMEWORK_EFI_FV_ATTRIBUTES *Attributes\r
113 )\r
114{\r
115 FIRMWARE_VOLUME_PRIVATE_DATA *Private;\r
116 EFI_FIRMWARE_VOLUME2_PROTOCOL *FirmwareVolume2;\r
4259256b 117 EFI_FV_ATTRIBUTES Fv2Attributes; \r
19caab51 118 EFI_STATUS Status;\r
4259256b 119\r
120 Private = FIRMWARE_VOLUME_PRIVATE_DATA_FROM_THIS (This);\r
121 FirmwareVolume2 = Private->FirmwareVolume2;\r
122\r
123 Fv2Attributes = (*Attributes & 0x1ff);\r
19caab51 124 Status = FirmwareVolume2->SetVolumeAttributes (\r
4259256b 125 FirmwareVolume2,\r
126 &Fv2Attributes\r
127 );\r
19caab51 128\r
129 *Attributes = Fv2AttributesToFvAttributes (Fv2Attributes);\r
130 \r
131 return Status;\r
4259256b 132}\r
133\r
134/**\r
135 Read the requested file (NameGuid) and returns data in Buffer.\r
136\r
137 @param This Calling context\r
138 @param NameGuid Filename identifying which file to read\r
139 @param Buffer Pointer to pointer to buffer in which contents of file are returned.\r
140 <br>\r
141 If Buffer is NULL, only type, attributes, and size are returned as\r
142 there is no output buffer.\r
143 <br>\r
144 If Buffer != NULL and *Buffer == NULL, the output buffer is allocated\r
145 from BS pool by ReadFile\r
146 <br>\r
147 If Buffer != NULL and *Buffer != NULL, the output buffer has been\r
148 allocated by the caller and is being passed in.\r
149 @param BufferSize Indicates the buffer size passed in, and on output the size\r
150 required to complete the read\r
151 @param FoundType Indicates the type of the file who's data is returned\r
152 @param FileAttributes Indicates the attributes of the file who's data is resturned\r
153 @param AuthenticationStatus Indicates the authentication status of the data\r
154\r
26a76fbc
LG
155 @retval EFI_SUCCESS The call completed successfully\r
156 @retval EFI_WARN_BUFFER_TOO_SMALL The buffer is too small to contain the requested output.\r
157 The buffer is filled and the output is truncated.\r
158 @retval EFI_NOT_FOUND NameGuid was not found in the firmware volume.\r
159 @retval EFI_DEVICE_ERROR A hardware error occurred when attempting to access the firmware volume.\r
160 @retval EFI_ACCESS_DENIED The firmware volume is configured to disallow reads.\r
161 @retval EFI_OUT_OF_RESOURCES An allocation failure occurred.\r
4259256b 162\r
163**/\r
164EFI_STATUS\r
165EFIAPI\r
166FvReadFile (\r
167 IN EFI_FIRMWARE_VOLUME_PROTOCOL *This,\r
168 IN EFI_GUID *NameGuid,\r
169 IN OUT VOID **Buffer,\r
170 IN OUT UINTN *BufferSize,\r
171 OUT EFI_FV_FILETYPE *FoundType,\r
172 OUT EFI_FV_FILE_ATTRIBUTES *FileAttributes,\r
173 OUT UINT32 *AuthenticationStatus\r
174 )\r
175{\r
176 FIRMWARE_VOLUME_PRIVATE_DATA *Private;\r
177 EFI_FIRMWARE_VOLUME2_PROTOCOL *FirmwareVolume2;\r
19caab51 178 EFI_STATUS Status;\r
4259256b 179\r
180 Private = FIRMWARE_VOLUME_PRIVATE_DATA_FROM_THIS (This);\r
181 FirmwareVolume2 = Private->FirmwareVolume2;\r
182\r
19caab51 183 Status = FirmwareVolume2->ReadFile (\r
4259256b 184 FirmwareVolume2,\r
185 NameGuid,\r
186 Buffer,\r
187 BufferSize,\r
188 FoundType,\r
189 FileAttributes,\r
190 AuthenticationStatus\r
191 );\r
19caab51 192\r
193 //\r
194 // For Framework FV attrbutes, only alignment fields are valid.\r
195 //\r
196 *FileAttributes = *FileAttributes & EFI_FV_FILE_ATTRIB_ALIGNMENT;\r
197 \r
198 return Status;\r
4259256b 199}\r
200\r
201/**\r
202 Read the requested section from the specified file and returns data in Buffer.\r
203\r
204 @param This Calling context\r
205 @param NameGuid Filename identifying the file from which to read\r
206 @param SectionType Indicates what section type to retrieve\r
207 @param SectionInstance Indicates which instance of SectionType to retrieve\r
208 @param Buffer Pointer to pointer to buffer in which contents of file are returned.\r
209 <br>\r
210 If Buffer is NULL, only type, attributes, and size are returned as\r
211 there is no output buffer.\r
212 <br>\r
213 If Buffer != NULL and *Buffer == NULL, the output buffer is allocated\r
214 from BS pool by ReadFile\r
215 <br>\r
216 If Buffer != NULL and *Buffer != NULL, the output buffer has been\r
217 allocated by the caller and is being passed in.\r
218 @param BufferSize Indicates the buffer size passed in, and on output the size\r
219 required to complete the read\r
220 @param AuthenticationStatus Indicates the authentication status of the data\r
221\r
26a76fbc
LG
222 @retval EFI_SUCCESS The call completed successfully.\r
223 @retval EFI_WARN_BUFFER_TOO_SMALL The buffer is too small to contain the requested output. \r
224 The buffer is filled and the output is truncated.\r
225 @retval EFI_OUT_OF_RESOURCES An allocation failure occurred.\r
226 @retval EFI_NOT_FOUND Name was not found in the firmware volume.\r
227 @retval EFI_DEVICE_ERROR A hardware error occurred when attempting to access the firmware volume.\r
228 @retval EFI_ACCESS_DENIED The firmware volume is configured to disallow reads.\r
4259256b 229\r
230**/\r
231EFI_STATUS\r
232EFIAPI \r
233FvReadSection (\r
234 IN EFI_FIRMWARE_VOLUME_PROTOCOL *This,\r
235 IN EFI_GUID *NameGuid,\r
236 IN EFI_SECTION_TYPE SectionType,\r
237 IN UINTN SectionInstance,\r
238 IN OUT VOID **Buffer,\r
239 IN OUT UINTN *BufferSize,\r
240 OUT UINT32 *AuthenticationStatus\r
241 )\r
242{\r
243 FIRMWARE_VOLUME_PRIVATE_DATA *Private;\r
244 EFI_FIRMWARE_VOLUME2_PROTOCOL *FirmwareVolume2;\r
245\r
246 Private = FIRMWARE_VOLUME_PRIVATE_DATA_FROM_THIS (This);\r
247 FirmwareVolume2 = Private->FirmwareVolume2;\r
248\r
249 return FirmwareVolume2->ReadSection (\r
250 FirmwareVolume2,\r
251 NameGuid,\r
252 SectionType,\r
253 SectionInstance,\r
254 Buffer,\r
255 BufferSize,\r
256 AuthenticationStatus\r
257 );\r
258}\r
259\r
260/**\r
261 Write the supplied file (NameGuid) to the FV.\r
262\r
263 @param This Calling context\r
264 @param NumberOfFiles Indicates the number of file records pointed to by FileData\r
265 @param WritePolicy Indicates the level of reliability of the write with respect to\r
266 things like power failure events.\r
267 @param FileData A pointer to an array of EFI_FV_WRITE_FILE_DATA structures. Each\r
268 element in the array indicates a file to write, and there are\r
269 NumberOfFiles elements in the input array.\r
270\r
26a76fbc
LG
271 @retval EFI_SUCCESS The write completed successfully.\r
272 @retval EFI_OUT_OF_RESOURCES The firmware volume does not have enough free space to store file(s).\r
273 @retval EFI_DEVICE_ERROR A hardware error occurred when attempting to access the firmware volume.\r
274 @retval EFI_WRITE_PROTECTED The firmware volume is configured to disallow writes.\r
275 @retval EFI_NOT_FOUND A delete was requested, but the requested file was not \r
276 found in the firmware volume.\r
277 @retval EFI_INVALID_PARAMETER A delete was requested with a multiple file write.\r
278 An unsupported WritePolicy was requested.\r
279 An unknown file type was specified.\r
280 A file system specific error has occurred.\r
4259256b 281\r
282**/\r
283EFI_STATUS\r
284EFIAPI \r
285FvWriteFile (\r
286 IN EFI_FIRMWARE_VOLUME_PROTOCOL *This,\r
287 IN UINT32 NumberOfFiles,\r
288 IN FRAMEWORK_EFI_FV_WRITE_POLICY WritePolicy,\r
289 IN FRAMEWORK_EFI_FV_WRITE_FILE_DATA *FileData\r
290 )\r
291{\r
292 FIRMWARE_VOLUME_PRIVATE_DATA *Private;\r
293 EFI_FIRMWARE_VOLUME2_PROTOCOL *FirmwareVolume2;\r
19caab51 294 EFI_FV_WRITE_FILE_DATA *PiFileData;\r
295 EFI_STATUS Status;\r
296 UINTN Index;\r
4259256b 297\r
298 Private = FIRMWARE_VOLUME_PRIVATE_DATA_FROM_THIS (This);\r
299 FirmwareVolume2 = Private->FirmwareVolume2;\r
300\r
19caab51 301 PiFileData = AllocateCopyPool (sizeof (EFI_FV_WRITE_FILE_DATA), FileData);\r
771ececd 302 ASSERT (PiFileData != NULL);\r
19caab51 303\r
304 //\r
305 // Framework Spec assume firmware files are Memory-Mapped.\r
306 //\r
307 for (Index = 0; Index < NumberOfFiles; Index++) {\r
308 PiFileData[Index].FileAttributes |= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED;\r
309 }\r
310\r
311 Status = FirmwareVolume2->WriteFile (\r
4259256b 312 FirmwareVolume2,\r
313 NumberOfFiles,\r
314 WritePolicy,\r
315 (EFI_FV_WRITE_FILE_DATA *)FileData\r
316 );\r
19caab51 317\r
318 FreePool (PiFileData);\r
319 return Status;\r
4259256b 320}\r
321\r
322/**\r
323 Given the input key, search for the next matching file in the volume.\r
324\r
325 @param This Calling context\r
326 @param Key Pointer to a caller allocated buffer that contains an implementation\r
327 specific key that is used to track where to begin searching on\r
328 successive calls.\r
329 @param FileType Indicates the file type to filter for\r
330 @param NameGuid Guid filename of the file found\r
331 @param Attributes Attributes of the file found\r
332 @param Size Size in bytes of the file found\r
333\r
26a76fbc
LG
334 @retval EFI_SUCCESS The output parameters are filled with data obtained from \r
335 the first matching file that was found.\r
336 @retval EFI_NOT_FOUND No files of type FileType were found.\r
337 @retval EFI_DEVICE_ERROR A hardware error occurred when attempting to access \r
338 the firmware volume.\r
339 @retval EFI_ACCESS_DENIED The firmware volume is configured to disallow reads.\r
4259256b 340\r
341**/\r
342EFI_STATUS\r
343EFIAPI \r
344FvGetNextFile (\r
345 IN EFI_FIRMWARE_VOLUME_PROTOCOL *This,\r
346 IN OUT VOID *Key,\r
347 IN OUT EFI_FV_FILETYPE *FileType,\r
348 OUT EFI_GUID *NameGuid,\r
349 OUT EFI_FV_FILE_ATTRIBUTES *Attributes,\r
350 OUT UINTN *Size\r
351 )\r
352{\r
353 FIRMWARE_VOLUME_PRIVATE_DATA *Private;\r
354 EFI_FIRMWARE_VOLUME2_PROTOCOL *FirmwareVolume2;\r
19caab51 355 EFI_STATUS Status;\r
4259256b 356\r
357 Private = FIRMWARE_VOLUME_PRIVATE_DATA_FROM_THIS (This);\r
358 FirmwareVolume2 = Private->FirmwareVolume2;\r
359\r
19caab51 360 Status = FirmwareVolume2->GetNextFile (\r
4259256b 361 FirmwareVolume2,\r
362 Key,\r
363 FileType,\r
364 NameGuid,\r
365 Attributes,\r
366 Size\r
367 );\r
19caab51 368\r
369 //\r
370 // For Framework FV attrbutes, only alignment fields are valid.\r
371 //\r
372 *Attributes = *Attributes & EFI_FV_FILE_ATTRIB_ALIGNMENT;\r
373 \r
374 return Status;\r
4259256b 375}\r
26a76fbc
LG
376\r
377//\r
378// Firmware Volume Protocol template\r
379//\r
380EFI_EVENT mFvRegistration;\r
381\r
382FIRMWARE_VOLUME_PRIVATE_DATA gFirmwareVolumePrivateDataTemplate = {\r
383 FIRMWARE_VOLUME_PRIVATE_DATA_SIGNATURE,\r
384 {\r
385 FvGetVolumeAttributes,\r
386 FvSetVolumeAttributes,\r
387 FvReadFile,\r
388 FvReadSection,\r
389 FvWriteFile,\r
390 FvGetNextFile,\r
391 0,\r
392 NULL\r
393 },\r
394 NULL\r
395};\r
396\r
397//\r
398// Module globals\r
399//\r
400/**\r
401 This notification function is invoked when an instance of the\r
402 EFI_FIRMWARE_VOLUME2_PROTOCOL is produced. It installs another instance of the\r
403 EFI_FIRMWARE_VOLUME_PROTOCOL on the same handle.\r
404\r
405 @param Event The event that occured\r
406 @param Context Context of event. Not used in this nofication function.\r
407\r
408**/\r
409VOID\r
410EFIAPI\r
411FvNotificationEvent (\r
412 IN EFI_EVENT Event,\r
413 IN VOID *Context\r
414 )\r
415{\r
416 EFI_STATUS Status;\r
417 UINTN BufferSize;\r
418 EFI_HANDLE Handle;\r
419 FIRMWARE_VOLUME_PRIVATE_DATA *Private;\r
420 EFI_FIRMWARE_VOLUME_PROTOCOL *FirmwareVolume;\r
421\r
422 while (TRUE) {\r
423 BufferSize = sizeof (Handle);\r
424 Status = gBS->LocateHandle (\r
425 ByRegisterNotify,\r
426 &gEfiFirmwareVolume2ProtocolGuid,\r
427 mFvRegistration,\r
428 &BufferSize,\r
429 &Handle\r
430 );\r
431 if (EFI_ERROR (Status)) {\r
432 //\r
433 // Exit Path of While Loop....\r
434 //\r
435 break;\r
436 }\r
437\r
438 //\r
439 // Skip this handle if the Firmware Volume Protocol is already installed\r
440 //\r
441 Status = gBS->HandleProtocol (\r
442 Handle,\r
443 &gEfiFirmwareVolumeProtocolGuid,\r
444 (VOID **)&FirmwareVolume\r
445 );\r
446 if (!EFI_ERROR (Status)) {\r
447 continue;\r
448 }\r
449\r
450 //\r
451 // Allocate private data structure\r
452 //\r
453 Private = AllocateCopyPool (sizeof (FIRMWARE_VOLUME_PRIVATE_DATA), &gFirmwareVolumePrivateDataTemplate);\r
454 if (Private == NULL) {\r
455 continue;\r
456 }\r
457\r
458 //\r
459 // Retrieve the Firmware Volume2 Protocol\r
460 //\r
461 Status = gBS->HandleProtocol (\r
462 Handle,\r
463 &gEfiFirmwareVolume2ProtocolGuid,\r
464 (VOID **)&Private->FirmwareVolume2\r
465 );\r
466 ASSERT_EFI_ERROR (Status);\r
467\r
468 //\r
469 // Fill in rest of private data structure\r
470 //\r
471 Private->FirmwareVolume.KeySize = Private->FirmwareVolume2->KeySize;\r
472 Private->FirmwareVolume.ParentHandle = Private->FirmwareVolume2->ParentHandle;\r
473\r
474 //\r
475 // Install Firmware Volume Protocol onto same handle\r
476 //\r
477 Status = gBS->InstallMultipleProtocolInterfaces (\r
478 &Handle,\r
479 &gEfiFirmwareVolumeProtocolGuid,\r
480 &Private->FirmwareVolume,\r
481 NULL\r
482 );\r
483 ASSERT_EFI_ERROR (Status);\r
484 }\r
485}\r
486\r
487\r
488/**\r
489 The user Entry Point for DXE driver. The user code starts with this function\r
490 as the real entry point for the image goes into a library that calls this \r
491 function.\r
492\r
493 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
494 @param[in] SystemTable A pointer to the EFI System Table.\r
495 \r
496 @retval EFI_SUCCESS The entry point is executed successfully.\r
497 @retval other Some error occurs when executing this entry point.\r
498\r
499**/\r
500EFI_STATUS\r
501EFIAPI\r
502InitializeFirmwareVolume2 (\r
503 IN EFI_HANDLE ImageHandle,\r
504 IN EFI_SYSTEM_TABLE *SystemTable\r
505 )\r
506{\r
507 EfiCreateProtocolNotifyEvent (\r
508 &gEfiFirmwareVolume2ProtocolGuid,\r
509 TPL_CALLBACK,\r
510 FvNotificationEvent,\r
511 NULL,\r
512 &mFvRegistration\r
513 );\r
514 return EFI_SUCCESS;\r
515}\r