VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/FwBlockService.c@ 72519

最後變更 在這個檔案從72519是 72519,由 vboxsync 提交於 7 年 前

EFI: Improved flash compatibility.

  • 屬性 svn:eol-style 設為 native
檔案大小: 35.3 KB
 
1/**@file
2
3Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
4This program and the accompanying materials
5are licensed and made available under the terms and conditions of the BSD License
6which accompanies this distribution. The full text of the license may be found at
7http://opensource.org/licenses/bsd-license.php
8
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12Module Name:
13
14 FWBlockService.c
15
16Abstract:
17
18Revision History
19
20**/
21
22//
23// The package level header files this module uses
24//
25#include <PiDxe.h>
26
27//
28// The protocols, PPI and GUID defintions for this module
29//
30#include <Guid/EventGroup.h>
31#include <Protocol/FirmwareVolumeBlock.h>
32#include <Protocol/DevicePath.h>
33
34//
35// The Library classes this module consumes
36//
37#include <Library/UefiLib.h>
38#include <Library/UefiDriverEntryPoint.h>
39#include <Library/BaseLib.h>
40#include <Library/DxeServicesTableLib.h>
41#include <Library/UefiRuntimeLib.h>
42#include <Library/DebugLib.h>
43#include <Library/BaseMemoryLib.h>
44#include <Library/MemoryAllocationLib.h>
45#include <Library/UefiBootServicesTableLib.h>
46#include <Library/DevicePathLib.h>
47
48#include "FwBlockService.h"
49#include "QemuFlash.h"
50
51#define EFI_FVB2_STATUS (EFI_FVB2_READ_STATUS | EFI_FVB2_WRITE_STATUS | EFI_FVB2_LOCK_STATUS)
52
53ESAL_FWB_GLOBAL *mFvbModuleGlobal;
54
55FV_MEMMAP_DEVICE_PATH mFvMemmapDevicePathTemplate = {
56 {
57 {
58 HARDWARE_DEVICE_PATH,
59 HW_MEMMAP_DP,
60 {
61 (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),
62 (UINT8)(sizeof (MEMMAP_DEVICE_PATH) >> 8)
63 }
64 },
65 EfiMemoryMappedIO,
66 (EFI_PHYSICAL_ADDRESS) 0,
67 (EFI_PHYSICAL_ADDRESS) 0,
68 },
69 {
70 END_DEVICE_PATH_TYPE,
71 END_ENTIRE_DEVICE_PATH_SUBTYPE,
72 {
73 END_DEVICE_PATH_LENGTH,
74 0
75 }
76 }
77};
78
79FV_PIWG_DEVICE_PATH mFvPIWGDevicePathTemplate = {
80 {
81 {
82 MEDIA_DEVICE_PATH,
83 MEDIA_PIWG_FW_VOL_DP,
84 {
85 (UINT8)(sizeof (MEDIA_FW_VOL_DEVICE_PATH)),
86 (UINT8)(sizeof (MEDIA_FW_VOL_DEVICE_PATH) >> 8)
87 }
88 },
89 { 0 }
90 },
91 {
92 END_DEVICE_PATH_TYPE,
93 END_ENTIRE_DEVICE_PATH_SUBTYPE,
94 {
95 END_DEVICE_PATH_LENGTH,
96 0
97 }
98 }
99};
100
101EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate = {
102 FVB_DEVICE_SIGNATURE,
103 NULL,
104 0,
105 {
106 FvbProtocolGetAttributes,
107 FvbProtocolSetAttributes,
108 FvbProtocolGetPhysicalAddress,
109 FvbProtocolGetBlockSize,
110 FvbProtocolRead,
111 FvbProtocolWrite,
112 FvbProtocolEraseBlocks,
113 NULL
114 }
115};
116
117
118
119VOID
120EFIAPI
121FvbVirtualddressChangeEvent (
122 IN EFI_EVENT Event,
123 IN VOID *Context
124 )
125/*++
126
127Routine Description:
128
129 Fixup internal data so that EFI and SAL can be call in virtual mode.
130 Call the passed in Child Notify event and convert the mFvbModuleGlobal
131 date items to there virtual address.
132
133 mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] - Physical copy of instance data
134 mFvbModuleGlobal->FvInstance[FVB_VIRTUAL] - Virtual pointer to common
135 instance data.
136
137Arguments:
138
139 (Standard EFI notify event - EFI_EVENT_NOTIFY)
140
141Returns:
142
143 None
144
145--*/
146{
147 EFI_FW_VOL_INSTANCE *FwhInstance;
148 UINTN Index;
149
150 EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal->FvInstance[FVB_VIRTUAL]);
151
152 //
153 // Convert the base address of all the instances
154 //
155 Index = 0;
156 FwhInstance = mFvbModuleGlobal->FvInstance[FVB_PHYSICAL];
157 while (Index < mFvbModuleGlobal->NumFv) {
158 EfiConvertPointer (0x0, (VOID **) &FwhInstance->FvBase[FVB_VIRTUAL]);
159 FwhInstance = (EFI_FW_VOL_INSTANCE *)
160 (
161 (UINTN) ((UINT8 *) FwhInstance) + FwhInstance->VolumeHeader.HeaderLength +
162 (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))
163 );
164 Index++;
165 }
166
167 EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal->FvbScratchSpace[FVB_VIRTUAL]);
168 EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal);
169 QemuFlashConvertPointers ();
170}
171
172EFI_STATUS
173GetFvbInstance (
174 IN UINTN Instance,
175 IN ESAL_FWB_GLOBAL *Global,
176 OUT EFI_FW_VOL_INSTANCE **FwhInstance,
177 IN BOOLEAN Virtual
178 )
179/*++
180
181Routine Description:
182 Retrieves the physical address of a memory mapped FV
183
184Arguments:
185 Instance - The FV instance whose base address is going to be
186 returned
187 Global - Pointer to ESAL_FWB_GLOBAL that contains all
188 instance data
189 FwhInstance - The EFI_FW_VOL_INSTANCE fimrware instance structure
190 Virtual - Whether CPU is in virtual or physical mode
191
192Returns:
193 EFI_SUCCESS - Successfully returns
194 EFI_INVALID_PARAMETER - Instance not found
195
196--*/
197{
198 EFI_FW_VOL_INSTANCE *FwhRecord;
199
200 *FwhInstance = NULL;
201 if (Instance >= Global->NumFv) {
202 return EFI_INVALID_PARAMETER;
203 }
204 //
205 // Find the right instance of the FVB private data
206 //
207 FwhRecord = Global->FvInstance[Virtual];
208 while (Instance > 0) {
209 FwhRecord = (EFI_FW_VOL_INSTANCE *)
210 (
211 (UINTN) ((UINT8 *) FwhRecord) + FwhRecord->VolumeHeader.HeaderLength +
212 (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))
213 );
214 Instance--;
215 }
216
217 *FwhInstance = FwhRecord;
218
219 return EFI_SUCCESS;
220}
221
222EFI_STATUS
223FvbGetPhysicalAddress (
224 IN UINTN Instance,
225 OUT EFI_PHYSICAL_ADDRESS *Address,
226 IN ESAL_FWB_GLOBAL *Global,
227 IN BOOLEAN Virtual
228 )
229/*++
230
231Routine Description:
232 Retrieves the physical address of a memory mapped FV
233
234Arguments:
235 Instance - The FV instance whose base address is going to be
236 returned
237 Address - Pointer to a caller allocated EFI_PHYSICAL_ADDRESS
238 that on successful return, contains the base address
239 of the firmware volume.
240 Global - Pointer to ESAL_FWB_GLOBAL that contains all
241 instance data
242 Virtual - Whether CPU is in virtual or physical mode
243
244Returns:
245 EFI_SUCCESS - Successfully returns
246 EFI_INVALID_PARAMETER - Instance not found
247
248--*/
249{
250 EFI_FW_VOL_INSTANCE *FwhInstance;
251 EFI_STATUS Status;
252
253 //
254 // Find the right instance of the FVB private data
255 //
256 Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);
257 ASSERT_EFI_ERROR (Status);
258 *Address = FwhInstance->FvBase[Virtual];
259
260 return EFI_SUCCESS;
261}
262
263EFI_STATUS
264FvbGetVolumeAttributes (
265 IN UINTN Instance,
266 OUT EFI_FVB_ATTRIBUTES_2 *Attributes,
267 IN ESAL_FWB_GLOBAL *Global,
268 IN BOOLEAN Virtual
269 )
270/*++
271
272Routine Description:
273 Retrieves attributes, insures positive polarity of attribute bits, returns
274 resulting attributes in output parameter
275
276Arguments:
277 Instance - The FV instance whose attributes is going to be
278 returned
279 Attributes - Output buffer which contains attributes
280 Global - Pointer to ESAL_FWB_GLOBAL that contains all
281 instance data
282 Virtual - Whether CPU is in virtual or physical mode
283
284Returns:
285 EFI_SUCCESS - Successfully returns
286 EFI_INVALID_PARAMETER - Instance not found
287
288--*/
289{
290 EFI_FW_VOL_INSTANCE *FwhInstance;
291 EFI_STATUS Status;
292
293 //
294 // Find the right instance of the FVB private data
295 //
296 Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);
297 ASSERT_EFI_ERROR (Status);
298 *Attributes = FwhInstance->VolumeHeader.Attributes;
299
300 return EFI_SUCCESS;
301}
302
303EFI_STATUS
304FvbGetLbaAddress (
305 IN UINTN Instance,
306 IN EFI_LBA Lba,
307 OUT UINTN *LbaAddress,
308 OUT UINTN *LbaLength,
309 OUT UINTN *NumOfBlocks,
310 IN ESAL_FWB_GLOBAL *Global,
311 IN BOOLEAN Virtual
312 )
313/*++
314
315Routine Description:
316 Retrieves the starting address of an LBA in an FV
317
318Arguments:
319 Instance - The FV instance which the Lba belongs to
320 Lba - The logical block address
321 LbaAddress - On output, contains the physical starting address
322 of the Lba
323 LbaLength - On output, contains the length of the block
324 NumOfBlocks - A pointer to a caller allocated UINTN in which the
325 number of consecutive blocks starting with Lba is
326 returned. All blocks in this range have a size of
327 BlockSize
328 Global - Pointer to ESAL_FWB_GLOBAL that contains all
329 instance data
330 Virtual - Whether CPU is in virtual or physical mode
331
332Returns:
333 EFI_SUCCESS - Successfully returns
334 EFI_INVALID_PARAMETER - Instance not found
335
336--*/
337{
338 UINT32 NumBlocks;
339 UINT32 BlockLength;
340 UINTN Offset;
341 EFI_LBA StartLba;
342 EFI_LBA NextLba;
343 EFI_FW_VOL_INSTANCE *FwhInstance;
344 EFI_FV_BLOCK_MAP_ENTRY *BlockMap;
345 EFI_STATUS Status;
346
347 //
348 // Find the right instance of the FVB private data
349 //
350 Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);
351 ASSERT_EFI_ERROR (Status);
352
353 StartLba = 0;
354 Offset = 0;
355 BlockMap = &(FwhInstance->VolumeHeader.BlockMap[0]);
356
357 //
358 // Parse the blockmap of the FV to find which map entry the Lba belongs to
359 //
360 while (TRUE) {
361 NumBlocks = BlockMap->NumBlocks;
362 BlockLength = BlockMap->Length;
363
364 if (NumBlocks == 0 || BlockLength == 0) {
365 return EFI_INVALID_PARAMETER;
366 }
367
368 NextLba = StartLba + NumBlocks;
369
370 //
371 // The map entry found
372 //
373 if (Lba >= StartLba && Lba < NextLba) {
374 Offset = Offset + (UINTN) MultU64x32 ((Lba - StartLba), BlockLength);
375 if (LbaAddress != NULL) {
376 *LbaAddress = FwhInstance->FvBase[Virtual] + Offset;
377 }
378
379 if (LbaLength != NULL) {
380 *LbaLength = BlockLength;
381 }
382
383 if (NumOfBlocks != NULL) {
384 *NumOfBlocks = (UINTN) (NextLba - Lba);
385 }
386
387 return EFI_SUCCESS;
388 }
389
390 StartLba = NextLba;
391 Offset = Offset + NumBlocks * BlockLength;
392 BlockMap++;
393 }
394}
395
396EFI_STATUS
397FvbSetVolumeAttributes (
398 IN UINTN Instance,
399 IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes,
400 IN ESAL_FWB_GLOBAL *Global,
401 IN BOOLEAN Virtual
402 )
403/*++
404
405Routine Description:
406 Modifies the current settings of the firmware volume according to the
407 input parameter, and returns the new setting of the volume
408
409Arguments:
410 Instance - The FV instance whose attributes is going to be
411 modified
412 Attributes - On input, it is a pointer to EFI_FVB_ATTRIBUTES_2
413 containing the desired firmware volume settings.
414 On successful return, it contains the new settings
415 of the firmware volume
416 Global - Pointer to ESAL_FWB_GLOBAL that contains all
417 instance data
418 Virtual - Whether CPU is in virtual or physical mode
419
420Returns:
421 EFI_SUCCESS - Successfully returns
422 EFI_ACCESS_DENIED - The volume setting is locked and cannot be modified
423 EFI_INVALID_PARAMETER - Instance not found, or The attributes requested are
424 in conflict with the capabilities as declared in the
425 firmware volume header
426
427--*/
428{
429 EFI_FW_VOL_INSTANCE *FwhInstance;
430 EFI_FVB_ATTRIBUTES_2 OldAttributes;
431 EFI_FVB_ATTRIBUTES_2 *AttribPtr;
432 UINT32 Capabilities;
433 UINT32 OldStatus;
434 UINT32 NewStatus;
435 EFI_STATUS Status;
436 EFI_FVB_ATTRIBUTES_2 UnchangedAttributes;
437
438 //
439 // Find the right instance of the FVB private data
440 //
441 Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);
442 ASSERT_EFI_ERROR (Status);
443
444 AttribPtr = (EFI_FVB_ATTRIBUTES_2 *) &(FwhInstance->VolumeHeader.Attributes);
445 OldAttributes = *AttribPtr;
446 Capabilities = OldAttributes & (EFI_FVB2_READ_DISABLED_CAP | \
447 EFI_FVB2_READ_ENABLED_CAP | \
448 EFI_FVB2_WRITE_DISABLED_CAP | \
449 EFI_FVB2_WRITE_ENABLED_CAP | \
450 EFI_FVB2_LOCK_CAP \
451 );
452 OldStatus = OldAttributes & EFI_FVB2_STATUS;
453 NewStatus = *Attributes & EFI_FVB2_STATUS;
454
455 UnchangedAttributes = EFI_FVB2_READ_DISABLED_CAP | \
456 EFI_FVB2_READ_ENABLED_CAP | \
457 EFI_FVB2_WRITE_DISABLED_CAP | \
458 EFI_FVB2_WRITE_ENABLED_CAP | \
459 EFI_FVB2_LOCK_CAP | \
460 EFI_FVB2_STICKY_WRITE | \
461 EFI_FVB2_MEMORY_MAPPED | \
462 EFI_FVB2_ERASE_POLARITY | \
463 EFI_FVB2_READ_LOCK_CAP | \
464 EFI_FVB2_WRITE_LOCK_CAP | \
465 EFI_FVB2_ALIGNMENT;
466
467 //
468 // Some attributes of FV is read only can *not* be set
469 //
470 if ((OldAttributes & UnchangedAttributes) ^ (*Attributes & UnchangedAttributes)) {
471 return EFI_INVALID_PARAMETER;
472 }
473 //
474 // If firmware volume is locked, no status bit can be updated
475 //
476 if (OldAttributes & EFI_FVB2_LOCK_STATUS) {
477 if (OldStatus ^ NewStatus) {
478 return EFI_ACCESS_DENIED;
479 }
480 }
481 //
482 // Test read disable
483 //
484 if ((Capabilities & EFI_FVB2_READ_DISABLED_CAP) == 0) {
485 if ((NewStatus & EFI_FVB2_READ_STATUS) == 0) {
486 return EFI_INVALID_PARAMETER;
487 }
488 }
489 //
490 // Test read enable
491 //
492 if ((Capabilities & EFI_FVB2_READ_ENABLED_CAP) == 0) {
493 if (NewStatus & EFI_FVB2_READ_STATUS) {
494 return EFI_INVALID_PARAMETER;
495 }
496 }
497 //
498 // Test write disable
499 //
500 if ((Capabilities & EFI_FVB2_WRITE_DISABLED_CAP) == 0) {
501 if ((NewStatus & EFI_FVB2_WRITE_STATUS) == 0) {
502 return EFI_INVALID_PARAMETER;
503 }
504 }
505 //
506 // Test write enable
507 //
508 if ((Capabilities & EFI_FVB2_WRITE_ENABLED_CAP) == 0) {
509 if (NewStatus & EFI_FVB2_WRITE_STATUS) {
510 return EFI_INVALID_PARAMETER;
511 }
512 }
513 //
514 // Test lock
515 //
516 if ((Capabilities & EFI_FVB2_LOCK_CAP) == 0) {
517 if (NewStatus & EFI_FVB2_LOCK_STATUS) {
518 return EFI_INVALID_PARAMETER;
519 }
520 }
521
522 *AttribPtr = (*AttribPtr) & (0xFFFFFFFF & (~EFI_FVB2_STATUS));
523 *AttribPtr = (*AttribPtr) | NewStatus;
524 *Attributes = *AttribPtr;
525
526 return EFI_SUCCESS;
527}
528
529//
530// FVB protocol APIs
531//
532EFI_STATUS
533EFIAPI
534FvbProtocolGetPhysicalAddress (
535 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
536 OUT EFI_PHYSICAL_ADDRESS *Address
537 )
538/*++
539
540Routine Description:
541
542 Retrieves the physical address of the device.
543
544Arguments:
545
546 This - Calling context
547 Address - Output buffer containing the address.
548
549Returns:
550
551Returns:
552 EFI_SUCCESS - Successfully returns
553
554--*/
555{
556 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
557
558 FvbDevice = FVB_DEVICE_FROM_THIS (This);
559
560 return FvbGetPhysicalAddress (FvbDevice->Instance, Address, mFvbModuleGlobal, EfiGoneVirtual ());
561}
562
563EFI_STATUS
564EFIAPI
565FvbProtocolGetBlockSize (
566 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
567 IN CONST EFI_LBA Lba,
568 OUT UINTN *BlockSize,
569 OUT UINTN *NumOfBlocks
570 )
571/*++
572
573Routine Description:
574 Retrieve the size of a logical block
575
576Arguments:
577 This - Calling context
578 Lba - Indicates which block to return the size for.
579 BlockSize - A pointer to a caller allocated UINTN in which
580 the size of the block is returned
581 NumOfBlocks - a pointer to a caller allocated UINTN in which the
582 number of consecutive blocks starting with Lba is
583 returned. All blocks in this range have a size of
584 BlockSize
585
586Returns:
587 EFI_SUCCESS - The firmware volume was read successfully and
588 contents are in Buffer
589
590--*/
591{
592 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
593
594 FvbDevice = FVB_DEVICE_FROM_THIS (This);
595
596 return FvbGetLbaAddress (
597 FvbDevice->Instance,
598 Lba,
599 NULL,
600 BlockSize,
601 NumOfBlocks,
602 mFvbModuleGlobal,
603 EfiGoneVirtual ()
604 );
605}
606
607EFI_STATUS
608EFIAPI
609FvbProtocolGetAttributes (
610 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
611 OUT EFI_FVB_ATTRIBUTES_2 *Attributes
612 )
613/*++
614
615Routine Description:
616 Retrieves Volume attributes. No polarity translations are done.
617
618Arguments:
619 This - Calling context
620 Attributes - output buffer which contains attributes
621
622Returns:
623 EFI_SUCCESS - Successfully returns
624
625--*/
626{
627 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
628
629 FvbDevice = FVB_DEVICE_FROM_THIS (This);
630
631 return FvbGetVolumeAttributes (FvbDevice->Instance, Attributes, mFvbModuleGlobal, EfiGoneVirtual ());
632}
633
634EFI_STATUS
635EFIAPI
636FvbProtocolSetAttributes (
637 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
638 IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
639 )
640/*++
641
642Routine Description:
643 Sets Volume attributes. No polarity translations are done.
644
645Arguments:
646 This - Calling context
647 Attributes - output buffer which contains attributes
648
649Returns:
650 EFI_SUCCESS - Successfully returns
651
652--*/
653{
654 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
655
656 FvbDevice = FVB_DEVICE_FROM_THIS (This);
657
658 return FvbSetVolumeAttributes (FvbDevice->Instance, Attributes, mFvbModuleGlobal, EfiGoneVirtual ());
659}
660
661EFI_STATUS
662EFIAPI
663FvbProtocolEraseBlocks (
664 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
665 ...
666 )
667/*++
668
669Routine Description:
670
671 The EraseBlock() function erases one or more blocks as denoted by the
672 variable argument list. The entire parameter list of blocks must be verified
673 prior to erasing any blocks. If a block is requested that does not exist
674 within the associated firmware volume (it has a larger index than the last
675 block of the firmware volume), the EraseBlock() function must return
676 EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.
677
678Arguments:
679 This - Calling context
680 ... - Starting LBA followed by Number of Lba to erase.
681 a -1 to terminate the list.
682
683Returns:
684 EFI_SUCCESS - The erase request was successfully completed
685 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
686 EFI_DEVICE_ERROR - The block device is not functioning correctly and
687 could not be written. Firmware device may have been
688 partially erased
689
690--*/
691{
692 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
693 EFI_FW_VOL_INSTANCE *FwhInstance;
694 UINTN NumOfBlocks;
695 VA_LIST args;
696 EFI_LBA StartingLba;
697 UINTN NumOfLba;
698 EFI_STATUS Status;
699
700 FvbDevice = FVB_DEVICE_FROM_THIS (This);
701
702 Status = GetFvbInstance (FvbDevice->Instance, mFvbModuleGlobal, &FwhInstance, EfiGoneVirtual ());
703 ASSERT_EFI_ERROR (Status);
704
705 NumOfBlocks = FwhInstance->NumOfBlocks;
706
707 VA_START (args, This);
708
709 do {
710 StartingLba = VA_ARG (args, EFI_LBA);
711 if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
712 break;
713 }
714
715 NumOfLba = VA_ARG (args, UINT32);
716
717 //
718 // Check input parameters
719 //
720 if ((NumOfLba == 0) || ((StartingLba + NumOfLba) > NumOfBlocks)) {
721 VA_END (args);
722 return EFI_INVALID_PARAMETER;
723 }
724 } while (1);
725
726 VA_END (args);
727
728 VA_START (args, This);
729 do {
730 StartingLba = VA_ARG (args, EFI_LBA);
731 if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
732 break;
733 }
734
735 NumOfLba = VA_ARG (args, UINT32);
736
737 while (NumOfLba > 0) {
738 Status = QemuFlashEraseBlock (StartingLba);
739 if (EFI_ERROR (Status)) {
740 VA_END (args);
741 return Status;
742 }
743
744 StartingLba++;
745 NumOfLba--;
746 }
747
748 } while (1);
749
750 VA_END (args);
751
752 return EFI_SUCCESS;
753}
754
755EFI_STATUS
756EFIAPI
757FvbProtocolWrite (
758 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
759 IN EFI_LBA Lba,
760 IN UINTN Offset,
761 IN OUT UINTN *NumBytes,
762 IN UINT8 *Buffer
763 )
764/*++
765
766Routine Description:
767
768 Writes data beginning at Lba:Offset from FV. The write terminates either
769 when *NumBytes of data have been written, or when a block boundary is
770 reached. *NumBytes is updated to reflect the actual number of bytes
771 written. The write opertion does not include erase. This routine will
772 attempt to write only the specified bytes. If the writes do not stick,
773 it will return an error.
774
775Arguments:
776 This - Calling context
777 Lba - Block in which to begin write
778 Offset - Offset in the block at which to begin write
779 NumBytes - On input, indicates the requested write size. On
780 output, indicates the actual number of bytes written
781 Buffer - Buffer containing source data for the write.
782
783Returns:
784 EFI_SUCCESS - The firmware volume was written successfully
785 EFI_BAD_BUFFER_SIZE - Write attempted across a LBA boundary. On output,
786 NumBytes contains the total number of bytes
787 actually written
788 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state
789 EFI_DEVICE_ERROR - The block device is not functioning correctly and
790 could not be written
791 EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL
792
793--*/
794{
795 return QemuFlashWrite ((EFI_LBA)Lba, (UINTN)Offset, NumBytes, (UINT8 *)Buffer);
796}
797
798EFI_STATUS
799EFIAPI
800FvbProtocolRead (
801 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
802 IN CONST EFI_LBA Lba,
803 IN CONST UINTN Offset,
804 IN OUT UINTN *NumBytes,
805 IN UINT8 *Buffer
806 )
807/*++
808
809Routine Description:
810
811 Reads data beginning at Lba:Offset from FV. The Read terminates either
812 when *NumBytes of data have been read, or when a block boundary is
813 reached. *NumBytes is updated to reflect the actual number of bytes
814 written. The write opertion does not include erase. This routine will
815 attempt to write only the specified bytes. If the writes do not stick,
816 it will return an error.
817
818Arguments:
819 This - Calling context
820 Lba - Block in which to begin Read
821 Offset - Offset in the block at which to begin Read
822 NumBytes - On input, indicates the requested write size. On
823 output, indicates the actual number of bytes Read
824 Buffer - Buffer containing source data for the Read.
825
826Returns:
827 EFI_SUCCESS - The firmware volume was read successfully and
828 contents are in Buffer
829 EFI_BAD_BUFFER_SIZE - Read attempted across a LBA boundary. On output,
830 NumBytes contains the total number of bytes returned
831 in Buffer
832 EFI_ACCESS_DENIED - The firmware volume is in the ReadDisabled state
833 EFI_DEVICE_ERROR - The block device is not functioning correctly and
834 could not be read
835 EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL
836
837--*/
838{
839 return QemuFlashRead ((EFI_LBA)Lba, (UINTN)Offset, NumBytes, (UINT8 *)Buffer);
840}
841
842EFI_STATUS
843ValidateFvHeader (
844 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader
845 )
846/*++
847
848Routine Description:
849 Check the integrity of firmware volume header
850
851Arguments:
852 FwVolHeader - A pointer to a firmware volume header
853
854Returns:
855 EFI_SUCCESS - The firmware volume is consistent
856 EFI_NOT_FOUND - The firmware volume has corrupted. So it is not an FV
857
858--*/
859{
860 UINT16 Checksum;
861
862 //
863 // Verify the header revision, header signature, length
864 // Length of FvBlock cannot be 2**64-1
865 // HeaderLength cannot be an odd number
866 //
867 if ((FwVolHeader->Revision != EFI_FVH_REVISION) ||
868 (FwVolHeader->Signature != EFI_FVH_SIGNATURE) ||
869 (FwVolHeader->FvLength == ((UINTN) -1)) ||
870 ((FwVolHeader->HeaderLength & 0x01) != 0)
871 ) {
872 return EFI_NOT_FOUND;
873 }
874
875 //
876 // Verify the header checksum
877 //
878
879 Checksum = CalculateSum16 ((UINT16 *) FwVolHeader, FwVolHeader->HeaderLength);
880 if (Checksum != 0) {
881 UINT16 Expected;
882
883 Expected = ((UINTN) FwVolHeader->Checksum + 0x10000 - Checksum) & 0xffff;
884
885 DEBUG ((EFI_D_INFO, "FV@%p Checksum is 0x%x, expected 0x%x\n",
886 FwVolHeader, FwVolHeader->Checksum, Expected));
887 return EFI_NOT_FOUND;
888 }
889
890 return EFI_SUCCESS;
891}
892
893STATIC
894EFI_STATUS
895MarkMemoryRangeForRuntimeAccess (
896 EFI_PHYSICAL_ADDRESS BaseAddress,
897 UINT64 Length
898 )
899{
900 EFI_STATUS Status;
901
902 //
903 // Mark flash region as runtime memory
904 //
905 Status = gDS->RemoveMemorySpace (
906 BaseAddress,
907 Length
908 );
909
910 Status = gDS->AddMemorySpace (
911#ifdef VBOX
912 EfiGcdMemoryTypeMemoryMappedIo, /* Must be MMIO to ensure OS can't move the region. */
913#else
914 EfiGcdMemoryTypeSystemMemory,
915#endif
916 BaseAddress,
917 Length,
918 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
919 );
920 ASSERT_EFI_ERROR (Status);
921
922 Status = gBS->AllocatePages (
923 AllocateAddress,
924 EfiRuntimeServicesData,
925 (UINTN) EFI_SIZE_TO_PAGES (Length),
926 &BaseAddress
927 );
928 ASSERT_EFI_ERROR (Status);
929
930 return Status;
931}
932
933STATIC
934EFI_STATUS
935InitializeVariableFvHeader (
936 VOID
937 )
938{
939 EFI_STATUS Status;
940 EFI_FIRMWARE_VOLUME_HEADER *GoodFwVolHeader;
941 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
942 UINTN Length;
943 UINTN WriteLength;
944 UINTN BlockSize;
945
946 FwVolHeader =
947 (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN)
948 PcdGet32 (PcdOvmfFlashNvStorageVariableBase);
949
950 Length =
951 (FixedPcdGet32 (PcdFlashNvStorageVariableSize) +
952 FixedPcdGet32 (PcdFlashNvStorageFtwWorkingSize) +
953 FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize) +
954 FixedPcdGet32 (PcdOvmfFlashNvStorageEventLogSize));
955
956 BlockSize = PcdGet32 (PcdOvmfFirmwareBlockSize);
957
958 Status = ValidateFvHeader (FwVolHeader);
959 if (!EFI_ERROR (Status)) {
960 if (FwVolHeader->FvLength != Length ||
961 FwVolHeader->BlockMap[0].Length != BlockSize) {
962 Status = EFI_VOLUME_CORRUPTED;
963 }
964 }
965 if (EFI_ERROR (Status)) {
966 UINTN Offset;
967 UINTN Start;
968
969 DEBUG ((EFI_D_INFO, "Variable FV header is not valid. It will be reinitialized.\n"));
970
971 //
972 // Get FvbInfo to provide in FwhInstance.
973 //
974 Status = GetFvbInfo (Length, &GoodFwVolHeader);
975 ASSERT (!EFI_ERROR (Status));
976
977 Start = (UINTN)(UINT8*) FwVolHeader - PcdGet32 (PcdOvmfFdBaseAddress);
978 ASSERT (Start % BlockSize == 0 && Length % BlockSize == 0);
979 ASSERT (GoodFwVolHeader->HeaderLength <= BlockSize);
980
981 //
982 // Erase all the blocks
983 //
984 for (Offset = Start; Offset < Start + Length; Offset += BlockSize) {
985 Status = QemuFlashEraseBlock ((EFI_LBA) Offset / BlockSize);
986 ASSERT_EFI_ERROR (Status);
987 }
988
989 //
990 // Write good FV header
991 //
992 WriteLength = GoodFwVolHeader->HeaderLength;
993 Status = QemuFlashWrite (
994 (EFI_LBA) Start / BlockSize,
995 0,
996 &WriteLength,
997 (UINT8 *) GoodFwVolHeader);
998 ASSERT_EFI_ERROR (Status);
999 ASSERT (WriteLength == GoodFwVolHeader->HeaderLength);
1000 }
1001
1002 return Status;
1003}
1004
1005EFI_STATUS
1006EFIAPI
1007FvbInitialize (
1008 IN EFI_HANDLE ImageHandle,
1009 IN EFI_SYSTEM_TABLE *SystemTable
1010 )
1011/*++
1012
1013Routine Description:
1014 This function does common initialization for FVB services
1015
1016Arguments:
1017
1018Returns:
1019
1020--*/
1021{
1022 EFI_STATUS Status;
1023 EFI_FW_VOL_INSTANCE *FwhInstance;
1024 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
1025 UINT32 BufferSize;
1026 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;
1027 EFI_HANDLE FwbHandle;
1028 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
1029 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *OldFwbInterface;
1030 UINT32 MaxLbaSize;
1031 EFI_PHYSICAL_ADDRESS BaseAddress;
1032 UINT64 Length;
1033 UINTN NumOfBlocks;
1034 EFI_EVENT VirtualAddressChangeEvent;
1035
1036 if (EFI_ERROR (QemuFlashInitialize ())) {
1037 //
1038 // Return an error so image will be unloaded
1039 //
1040 DEBUG ((EFI_D_INFO, "QEMU flash was not detected. Writable FVB is not being installed.\n"));
1041 return EFI_WRITE_PROTECTED;
1042 }
1043
1044 //
1045 // Allocate runtime services data for global variable, which contains
1046 // the private data of all firmware volume block instances
1047 //
1048 mFvbModuleGlobal = AllocateRuntimePool (sizeof (ESAL_FWB_GLOBAL));
1049 ASSERT (mFvbModuleGlobal != NULL);
1050
1051 BaseAddress = (UINTN) PcdGet32 (PcdOvmfFdBaseAddress);
1052 Length = PcdGet32 (PcdOvmfFirmwareFdSize);
1053
1054 Status = InitializeVariableFvHeader ();
1055 if (EFI_ERROR (Status)) {
1056 DEBUG ((EFI_D_INFO, "QEMU Flash: Unable to initialize variable FV header\n"));
1057 return EFI_WRITE_PROTECTED;
1058 }
1059
1060 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) BaseAddress;
1061 Status = ValidateFvHeader (FwVolHeader);
1062 if (EFI_ERROR (Status)) {
1063 //
1064 // Get FvbInfo
1065 //
1066 Status = GetFvbInfo (Length, &FwVolHeader);
1067 if (EFI_ERROR (Status)) {
1068 DEBUG ((EFI_D_INFO, "EFI_ERROR (GetFvbInfo (Length, &FwVolHeader))\n"));
1069 return EFI_WRITE_PROTECTED;
1070 }
1071 }
1072
1073 BufferSize = (sizeof (EFI_FW_VOL_INSTANCE) + FwVolHeader->HeaderLength - sizeof (EFI_FIRMWARE_VOLUME_HEADER));
1074
1075 //
1076 // Only need to allocate once. There is only one copy of physical memory for
1077 // the private data of each FV instance. But in virtual mode or in physical
1078 // mode, the address of the the physical memory may be different.
1079 //
1080 mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] = AllocateRuntimePool (BufferSize);
1081 ASSERT (mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] != NULL);
1082
1083 //
1084 // Make a virtual copy of the FvInstance pointer.
1085 //
1086 FwhInstance = mFvbModuleGlobal->FvInstance[FVB_PHYSICAL];
1087 mFvbModuleGlobal->FvInstance[FVB_VIRTUAL] = FwhInstance;
1088
1089 mFvbModuleGlobal->NumFv = 0;
1090 MaxLbaSize = 0;
1091
1092 FwVolHeader =
1093 (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN)
1094 PcdGet32 (PcdOvmfFlashNvStorageVariableBase);
1095
1096 FwhInstance->FvBase[FVB_PHYSICAL] = (UINTN) BaseAddress;
1097 FwhInstance->FvBase[FVB_VIRTUAL] = (UINTN) BaseAddress;
1098
1099 CopyMem ((UINTN *) &(FwhInstance->VolumeHeader), (UINTN *) FwVolHeader, FwVolHeader->HeaderLength);
1100 FwVolHeader = &(FwhInstance->VolumeHeader);
1101 EfiInitializeLock (&(FwhInstance->FvbDevLock), TPL_HIGH_LEVEL);
1102
1103 NumOfBlocks = 0;
1104
1105 for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {
1106 //
1107 // Get the maximum size of a block.
1108 //
1109 if (MaxLbaSize < PtrBlockMapEntry->Length) {
1110 MaxLbaSize = PtrBlockMapEntry->Length;
1111 }
1112
1113 NumOfBlocks = NumOfBlocks + PtrBlockMapEntry->NumBlocks;
1114 }
1115
1116 //
1117 // The total number of blocks in the FV.
1118 //
1119 FwhInstance->NumOfBlocks = NumOfBlocks;
1120
1121 //
1122 // Add a FVB Protocol Instance
1123 //
1124 FvbDevice = AllocateRuntimePool (sizeof (EFI_FW_VOL_BLOCK_DEVICE));
1125 ASSERT (FvbDevice != NULL);
1126
1127 CopyMem (FvbDevice, &mFvbDeviceTemplate, sizeof (EFI_FW_VOL_BLOCK_DEVICE));
1128
1129 FvbDevice->Instance = mFvbModuleGlobal->NumFv;
1130 mFvbModuleGlobal->NumFv++;
1131
1132 //
1133 // Set up the devicepath
1134 //
1135 if (FwVolHeader->ExtHeaderOffset == 0) {
1136 //
1137 // FV does not contains extension header, then produce MEMMAP_DEVICE_PATH
1138 //
1139 FvbDevice->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) AllocateCopyPool (sizeof (FV_MEMMAP_DEVICE_PATH), &mFvMemmapDevicePathTemplate);
1140 ((FV_MEMMAP_DEVICE_PATH *) FvbDevice->DevicePath)->MemMapDevPath.StartingAddress = BaseAddress;
1141 ((FV_MEMMAP_DEVICE_PATH *) FvbDevice->DevicePath)->MemMapDevPath.EndingAddress = BaseAddress + FwVolHeader->FvLength - 1;
1142 } else {
1143 FvbDevice->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) AllocateCopyPool (sizeof (FV_PIWG_DEVICE_PATH), &mFvPIWGDevicePathTemplate);
1144 CopyGuid (
1145 &((FV_PIWG_DEVICE_PATH *)FvbDevice->DevicePath)->FvDevPath.FvName,
1146 (GUID *)(UINTN)(BaseAddress + FwVolHeader->ExtHeaderOffset)
1147 );
1148 }
1149
1150 //
1151 // Find a handle with a matching device path that has supports FW Block protocol
1152 //
1153 Status = gBS->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid, &FvbDevice->DevicePath, &FwbHandle);
1154 if (EFI_ERROR (Status)) {
1155 //
1156 // LocateDevicePath fails so install a new interface and device path
1157 //
1158 FwbHandle = NULL;
1159 DEBUG ((EFI_D_INFO, "Installing QEMU flash FVB\n"));
1160 Status = gBS->InstallMultipleProtocolInterfaces (
1161 &FwbHandle,
1162 &gEfiFirmwareVolumeBlockProtocolGuid,
1163 &FvbDevice->FwVolBlockInstance,
1164 &gEfiDevicePathProtocolGuid,
1165 FvbDevice->DevicePath,
1166 NULL
1167 );
1168 ASSERT_EFI_ERROR (Status);
1169 } else if (IsDevicePathEnd (FvbDevice->DevicePath)) {
1170 //
1171 // Device already exists, so reinstall the FVB protocol
1172 //
1173 Status = gBS->HandleProtocol (
1174 FwbHandle,
1175 &gEfiFirmwareVolumeBlockProtocolGuid,
1176 (VOID**)&OldFwbInterface
1177 );
1178 ASSERT_EFI_ERROR (Status);
1179
1180 DEBUG ((EFI_D_INFO, "Reinstalling FVB for QEMU flash region\n"));
1181 Status = gBS->ReinstallProtocolInterface (
1182 FwbHandle,
1183 &gEfiFirmwareVolumeBlockProtocolGuid,
1184 OldFwbInterface,
1185 &FvbDevice->FwVolBlockInstance
1186 );
1187 ASSERT_EFI_ERROR (Status);
1188 } else {
1189 //
1190 // There was a FVB protocol on an End Device Path node
1191 //
1192 ASSERT (FALSE);
1193 }
1194
1195 MarkMemoryRangeForRuntimeAccess (BaseAddress, Length);
1196
1197 //
1198 // Set several PCD values to point to flash
1199 //
1200 PcdSet64 (
1201 PcdFlashNvStorageVariableBase64,
1202 (UINTN) PcdGet32 (PcdOvmfFlashNvStorageVariableBase)
1203 );
1204 PcdSet32 (
1205 PcdFlashNvStorageFtwWorkingBase,
1206 PcdGet32 (PcdOvmfFlashNvStorageFtwWorkingBase)
1207 );
1208 PcdSet32 (
1209 PcdFlashNvStorageFtwSpareBase,
1210 PcdGet32 (PcdOvmfFlashNvStorageFtwSpareBase)
1211 );
1212
1213 FwhInstance = (EFI_FW_VOL_INSTANCE *)
1214 (
1215 (UINTN) ((UINT8 *) FwhInstance) + FwVolHeader->HeaderLength +
1216 (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))
1217 );
1218
1219 VirtualAddressChangeEvent = NULL;
1220 Status = gBS->CreateEventEx (
1221 EVT_NOTIFY_SIGNAL,
1222 TPL_NOTIFY,
1223 FvbVirtualddressChangeEvent,
1224 NULL,
1225 &gEfiEventVirtualAddressChangeGuid,
1226 &VirtualAddressChangeEvent
1227 );
1228 ASSERT_EFI_ERROR (Status);
1229
1230 PcdSetBool (PcdOvmfFlashVariablesEnable, TRUE);
1231 return EFI_SUCCESS;
1232}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette