]> git.proxmox.com Git - mirror_edk2.git/blob - Vlv2TbltDevicePkg/FvbRuntimeDxe/FvbService.c
CryptoPkg/BaseCryptLib: Fix mismatched memory allocation/free
[mirror_edk2.git] / Vlv2TbltDevicePkg / FvbRuntimeDxe / FvbService.c
1 /** @file
2 Firmware Volume Block Driver for Lakeport Platform.
3
4 Firmware volume block driver for FWH or SPI device.
5 It depends on which Flash Device Library to be linked with this driver.
6
7 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
8
9 This program and the accompanying materials are licensed and made available under
10 the terms and conditions of the BSD License that accompanies this distribution.
11 The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php.
13
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16
17
18 **/
19
20 #include "FvbService.h"
21
22 //
23 // Global variable for this FVB driver which contains
24 // the private data of all firmware volume block instances.
25 //
26 FWB_GLOBAL mFvbModuleGlobal;
27
28 //
29 // This platform driver knows there are 3 FVs on
30 // FD, which are FvRecovery, FvMain and FvNvStorage.
31 //
32 UINT32 mPlatformFvBaseAddress[] = {
33 FixedPcdGet32(PcdFlashNvStorageVariableBase),
34 };
35
36 FV_MEMMAP_DEVICE_PATH mFvMemmapDevicePathTemplate = {
37 {
38 {
39 HARDWARE_DEVICE_PATH,
40 HW_MEMMAP_DP,
41 {
42 (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),
43 (UINT8)(sizeof (MEMMAP_DEVICE_PATH) >> 8)
44 }
45 },
46 EfiMemoryMappedIO,
47 (EFI_PHYSICAL_ADDRESS) 0,
48 (EFI_PHYSICAL_ADDRESS) 0,
49 },
50 {
51 END_DEVICE_PATH_TYPE,
52 END_ENTIRE_DEVICE_PATH_SUBTYPE,
53 {
54 END_DEVICE_PATH_LENGTH,
55 0
56 }
57 }
58 };
59
60 FV_PIWG_DEVICE_PATH mFvPIWGDevicePathTemplate = {
61 {
62 {
63 MEDIA_DEVICE_PATH,
64 MEDIA_PIWG_FW_VOL_DP,
65 {
66 (UINT8)(sizeof (MEDIA_FW_VOL_DEVICE_PATH)),
67 (UINT8)(sizeof (MEDIA_FW_VOL_DEVICE_PATH) >> 8)
68 }
69 },
70 { 0 }
71 },
72 {
73 END_DEVICE_PATH_TYPE,
74 END_ENTIRE_DEVICE_PATH_SUBTYPE,
75 {
76 END_DEVICE_PATH_LENGTH,
77 0
78 }
79 }
80 };
81
82 //
83 // Template structure used when installing FVB protocol.
84 //
85 EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate = {
86 FVB_DEVICE_SIGNATURE,
87 NULL,
88 0, // Instance
89 {
90 FvbProtocolGetAttributes,
91 FvbProtocolSetAttributes,
92 FvbProtocolGetPhysicalAddress,
93 FvbProtocolGetBlockSize,
94 FvbProtocolRead,
95 FvbProtocolWrite,
96 FvbProtocolEraseBlocks,
97 NULL
98 } // FwVolBlockInstance
99 };
100
101
102 /**
103 Get the pointer to EFI_FW_VOL_INSTANCE from the buffer pointed
104 by mFvbModuleGlobal.FvInstance based on a index.
105 Each EFI_FW_VOL_INSTANCE is with variable length as
106 we have a block map at the end of the EFI_FIRMWARE_VOLUME_HEADER.
107
108 @param[in] Instance The index of the EFI_FW_VOL_INSTANCE.
109
110 @return A pointer to EFI_FW_VOL_INSTANCE.
111
112 **/
113 EFI_FW_VOL_INSTANCE *
114 GetFvbInstance (
115 IN UINTN Instance
116 )
117 {
118 EFI_FW_VOL_INSTANCE *FwhRecord;
119
120 if ( Instance >= mFvbModuleGlobal.NumFv ) {
121 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER);
122 return NULL;
123 }
124
125 //
126 // Find the right instance of the FVB private data.
127 //
128 FwhRecord = mFvbModuleGlobal.FvInstance;
129 while ( Instance > 0 ) {
130 FwhRecord = (EFI_FW_VOL_INSTANCE *) ((UINTN)((UINT8 *)FwhRecord) +
131 FwhRecord->VolumeHeader.HeaderLength +
132 (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER)));
133 Instance --;
134 }
135
136 return FwhRecord;
137
138 }
139
140
141 /**
142 Get the EFI_FVB_ATTRIBUTES_2 of a FV.
143
144 @param[in] The index of the EFI_FW_VOL_INSTANCE.
145
146 @return EFI_FVB_ATTRIBUTES_2 of the FV identified by Instance.
147
148 **/
149 STATIC
150 EFI_FVB_ATTRIBUTES_2
151 FvbGetVolumeAttributes (
152 IN UINTN Instance
153 )
154 {
155 EFI_FW_VOL_INSTANCE * FwInstance = NULL;
156 FwInstance = GetFvbInstance(Instance);
157 ASSERT (FwInstance != NULL);
158
159 if ( FwInstance != NULL ) {
160 return FwInstance->VolumeHeader.Attributes;
161 } else {
162 return 0;
163 }
164 }
165
166
167 /**
168 Retrieves the starting address of an LBA in an FV. It also
169 return a few other attribut of the FV.
170
171 @param[in] Instance The index of the EFI_FW_VOL_INSTANCE.
172 @param[in] Lba The logical block address.
173 @param[out] LbaAddress On output, contains the physical starting address
174 of the Lba.
175 @param[out] LbaLength On output, contains the length of the block.
176 @param[out] NumOfBlocks A pointer to a caller allocated UINTN in which the
177 number of consecutive blocks starting with Lba is
178 returned. All blocks in this range have a size of
179 BlockSize.
180
181 @retval EFI_SUCCESS Successfully returns.
182 @retval EFI_INVALID_PARAMETER Instance not found.
183
184 **/
185 STATIC
186 EFI_STATUS
187 FvbGetLbaAddress (
188 IN UINTN Instance,
189 IN EFI_LBA Lba,
190 OUT UINTN *LbaAddress,
191 OUT UINTN *LbaLength,
192 OUT UINTN *NumOfBlocks
193 )
194 {
195 UINT32 NumBlocks = 0;
196 UINT32 BlockLength = 0;
197 UINTN Offset;
198 EFI_LBA StartLba;
199 EFI_LBA NextLba;
200 EFI_FW_VOL_INSTANCE *FwhInstance;
201 EFI_FV_BLOCK_MAP_ENTRY *BlockMap = NULL;
202
203 //
204 // Find the right instance of the FVB private data.
205 //
206 FwhInstance = GetFvbInstance (Instance);
207
208 StartLba = 0;
209 Offset = 0;
210 BlockMap = &(FwhInstance->VolumeHeader.BlockMap[0]);
211 ASSERT (BlockMap != NULL);
212
213 //
214 // Parse the blockmap of the FV to find which map entry the Lba belongs to.
215 //
216 while (TRUE) {
217 if ( BlockMap != NULL) {
218 NumBlocks = BlockMap->NumBlocks;
219 BlockLength = BlockMap->Length;
220 }
221
222 if ( NumBlocks == 0 || BlockLength == 0) {
223 return EFI_INVALID_PARAMETER;
224 }
225
226 NextLba = StartLba + NumBlocks;
227
228 //
229 // The map entry found.
230 //
231 if (Lba >= StartLba && Lba < NextLba) {
232 Offset = Offset + (UINTN)MultU64x32((Lba - StartLba), BlockLength);
233 if ( LbaAddress && FwhInstance ) {
234 *LbaAddress = FwhInstance->FvBase + Offset;
235 }
236
237 if (LbaLength ) {
238 *LbaLength = BlockLength;
239 }
240
241 if (NumOfBlocks ) {
242 *NumOfBlocks = (UINTN)(NextLba - Lba);
243 }
244 return EFI_SUCCESS;
245 }
246
247 StartLba = NextLba;
248 Offset = Offset + NumBlocks * BlockLength;
249 BlockMap++;
250 }
251 }
252
253
254 /**
255 Reads specified number of bytes into a buffer from the specified block.
256
257 @param[in] Instance The FV instance to be read from.
258 @param[in] Lba The logical block address to be read from.
259 @param[in] BlockOffset Offset into the block at which to begin reading.
260 @param[in] NumBytes Pointer that on input contains the total size of
261 the buffer. On output, it contains the total number
262 of bytes read.
263 @param[in] Buffer Pointer to a caller allocated buffer that will be
264 used to hold the data read.
265
266
267 @retval EFI_SUCCESS The firmware volume was read successfully and
268 contents are in Buffer.
269 @retval EFI_BAD_BUFFER_SIZE Read attempted across a LBA boundary. On output,
270 NumBytes contains the total number of bytes returned
271 in Buffer.
272 @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state.
273 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
274 could not be read.
275 @retval EFI_INVALID_PARAMETER Instance not found, or NumBytes, Buffer are NULL.
276
277 **/
278 STATIC
279 EFI_STATUS
280 FvbReadBlock (
281 IN UINTN Instance,
282 IN EFI_LBA Lba,
283 IN UINTN BlockOffset,
284 IN OUT UINTN *NumBytes,
285 IN UINT8 *Buffer
286 )
287 {
288 EFI_FVB_ATTRIBUTES_2 Attributes;
289 UINTN LbaAddress;
290 UINTN LbaLength;
291 EFI_STATUS Status;
292
293 if ( (NumBytes == NULL) || (Buffer == NULL)) {
294 return (EFI_INVALID_PARAMETER);
295 }
296 if (*NumBytes == 0) {
297 return (EFI_INVALID_PARAMETER);
298 }
299
300 Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL);
301 if (EFI_ERROR(Status)) {
302 return Status;
303 }
304
305 Attributes = FvbGetVolumeAttributes (Instance);
306
307 if ( (Attributes & EFI_FVB2_READ_STATUS) == 0) {
308 return (EFI_ACCESS_DENIED);
309 }
310
311 if (BlockOffset > LbaLength) {
312 return (EFI_INVALID_PARAMETER);
313 }
314
315 if (LbaLength < ( *NumBytes + BlockOffset ) ) {
316 *NumBytes = (UINT32) (LbaLength - BlockOffset);
317 Status = EFI_BAD_BUFFER_SIZE;
318 }
319
320 LibFvbFlashDeviceRead (LbaAddress + BlockOffset, NumBytes, Buffer);
321
322 return Status;
323 }
324
325
326 /**
327 Writes specified number of bytes from the input buffer to the block.
328
329 @param[in] Instance The FV instance to be written to.
330 @param[in] Lba The starting logical block index to write to.
331 @param[in] BlockOffset Offset into the block at which to begin writing.
332 @param[in] NumBytes Pointer that on input contains the total size of
333 the buffer. On output, it contains the total number
334 of bytes actually written.
335 @param[in] Buffer Pointer to a caller allocated buffer that contains
336 the source for the write.
337 @retval EFI_SUCCESS The firmware volume was written successfully.
338 @retval EFI_BAD_BUFFER_SIZE Write attempted across a LBA boundary. On output,
339 NumBytes contains the total number of bytes
340 actually writte.
341 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
342 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
343 could not be written.
344 @retval EFI_INVALID_PARAMETER Instance not found, or NumBytes, Buffer are NULL.
345
346 **/
347 EFI_STATUS
348 FvbWriteBlock (
349 IN UINTN Instance,
350 IN EFI_LBA Lba,
351 IN UINTN BlockOffset,
352 IN OUT UINTN *NumBytes,
353 IN UINT8 *Buffer
354 )
355 {
356 EFI_FVB_ATTRIBUTES_2 Attributes;
357 UINTN LbaAddress;
358 UINTN LbaLength;
359 EFI_STATUS Status;
360 EFI_STATUS Status1;
361
362 if ( (NumBytes == NULL) || (Buffer == NULL)) {
363 return (EFI_INVALID_PARAMETER);
364 }
365 if (*NumBytes == 0) {
366 return (EFI_INVALID_PARAMETER);
367 }
368
369 Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL);
370 if (EFI_ERROR(Status)) {
371 return Status;
372 }
373
374 //
375 // Check if the FV is write enabled.
376 //
377 Attributes = FvbGetVolumeAttributes (Instance);
378 if ( (Attributes & EFI_FVB2_WRITE_STATUS) == 0) {
379 return (EFI_ACCESS_DENIED);
380 }
381
382 //
383 // Perform boundary checks and adjust NumBytes.
384 //
385 if (BlockOffset > LbaLength) {
386 return (EFI_INVALID_PARAMETER);
387 }
388
389 if ( LbaLength < ( *NumBytes + BlockOffset ) ) {
390 DEBUG ((EFI_D_ERROR,
391 "FvWriteBlock: Reducing Numbytes from 0x%x to 0x%x\n",
392 *NumBytes,
393 (UINT32)(LbaLength-BlockOffset))
394 );
395 *NumBytes = (UINT32) (LbaLength - BlockOffset);
396 Status = EFI_BAD_BUFFER_SIZE;
397 }
398
399 LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, FALSE);
400
401 Status1 = LibFvbFlashDeviceWrite (LbaAddress + BlockOffset, NumBytes, Buffer);
402
403 LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, TRUE);
404 WriteBackInvalidateDataCacheRange ((VOID *) (LbaAddress + BlockOffset), *NumBytes);
405
406 if ( EFI_ERROR (Status1) ) {
407 return Status1;
408 }
409
410 return Status;
411 }
412
413
414 /**
415 Erases and initializes a firmware volume block.
416
417 @param[in] Instance The FV instance to be erased.
418 @param[in] Lba The logical block index to be erased.
419
420 @retval EFI_SUCCESS The erase request was successfully completed.
421 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
422 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
423 could not be written. Firmware device may have been
424 partially erased.
425 @retval EFI_INVALID_PARAMETER Instance not found.
426
427 **/
428 EFI_STATUS
429 FvbEraseBlock (
430 IN UINTN Instance,
431 IN EFI_LBA Lba
432 )
433 {
434 EFI_FVB_ATTRIBUTES_2 Attributes;
435 UINTN LbaAddress;
436 UINTN LbaLength;
437 EFI_STATUS Status;
438
439 //
440 // Check if the FV is write enabled.
441 //
442 Attributes = FvbGetVolumeAttributes (Instance);
443
444 if( (Attributes & EFI_FVB2_WRITE_STATUS) == 0) {
445 return (EFI_ACCESS_DENIED);
446 }
447
448 //
449 // Get the starting address of the block for erase.
450 //
451 Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL);
452 if (EFI_ERROR(Status)) {
453 return Status;
454 }
455
456 LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, FALSE);
457
458 Status = LibFvbFlashDeviceBlockErase (LbaAddress, LbaLength);
459
460 LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, TRUE);
461
462 WriteBackInvalidateDataCacheRange ((VOID *) LbaAddress, LbaLength);
463
464 return Status;
465 }
466
467
468 /**
469 Modifies the current settings of the firmware volume according to the
470 input parameter, and returns the new setting of the volume.
471
472 @param[in] Instance The FV instance whose attributes is going to be
473 modified.
474 @param[in] Attributes On input, it is a pointer to EFI_FVB_ATTRIBUTES_2
475 containing the desired firmware volume settings.
476 On successful return, it contains the new settings
477 of the firmware volume.
478
479 @retval EFI_SUCCESS Successfully returns.
480 @retval EFI_ACCESS_DENIED The volume setting is locked and cannot be modified.
481 @retval EFI_INVALID_PARAMETER Instance not found, or The attributes requested are
482 in conflict with the capabilities as declared in the
483 firmware volume header.
484
485 **/
486 STATIC
487 EFI_STATUS
488 FvbSetVolumeAttributes (
489 IN UINTN Instance,
490 IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
491 )
492 {
493 EFI_FW_VOL_INSTANCE *FwhInstance = NULL;
494 EFI_FVB_ATTRIBUTES_2 OldAttributes = 0;
495 EFI_FVB_ATTRIBUTES_2 *AttribPtr = NULL;
496 EFI_FVB_ATTRIBUTES_2 UnchangedAttributes;
497 UINT32 Capabilities;
498 UINT32 OldStatus, NewStatus;
499
500 //
501 // Find the right instance of the FVB private data.
502 //
503 FwhInstance = GetFvbInstance (Instance);
504
505 AttribPtr = (EFI_FVB_ATTRIBUTES_2 *) & (FwhInstance->VolumeHeader.Attributes);
506 ASSERT (AttribPtr != NULL);
507
508 if ( AttribPtr != NULL) {
509 OldAttributes = *AttribPtr;
510 }
511
512 Capabilities = OldAttributes & EFI_FVB2_CAPABILITIES;
513 OldStatus = OldAttributes & EFI_FVB2_STATUS;
514 NewStatus = *Attributes & EFI_FVB2_STATUS;
515
516 UnchangedAttributes = EFI_FVB2_READ_DISABLED_CAP | \
517 EFI_FVB2_READ_ENABLED_CAP | \
518 EFI_FVB2_WRITE_DISABLED_CAP | \
519 EFI_FVB2_WRITE_ENABLED_CAP | \
520 EFI_FVB2_LOCK_CAP | \
521 EFI_FVB2_STICKY_WRITE | \
522 EFI_FVB2_MEMORY_MAPPED | \
523 EFI_FVB2_ERASE_POLARITY | \
524 EFI_FVB2_READ_LOCK_CAP | \
525 EFI_FVB2_WRITE_LOCK_CAP | \
526 EFI_FVB2_ALIGNMENT;
527
528 //
529 // Some attributes of FV is read only can *not* be set.
530 //
531 if ((OldAttributes & UnchangedAttributes) ^ (*Attributes & UnchangedAttributes)) {
532 return EFI_INVALID_PARAMETER;
533 }
534
535 //
536 // If firmware volume is locked, no status bit can be updated.
537 //
538 if ( OldAttributes & EFI_FVB2_LOCK_STATUS ) {
539 if ( OldStatus ^ NewStatus ) {
540 return EFI_ACCESS_DENIED;
541 }
542 }
543
544 //
545 // Test read disable.
546 //
547 if ((Capabilities & EFI_FVB2_READ_DISABLED_CAP) == 0) {
548 if ((NewStatus & EFI_FVB2_READ_STATUS) == 0) {
549 return EFI_INVALID_PARAMETER;
550 }
551 }
552
553 //
554 // Test read enable.
555 //
556 if ((Capabilities & EFI_FVB2_READ_ENABLED_CAP) == 0) {
557 if (NewStatus & EFI_FVB2_READ_STATUS) {
558 return EFI_INVALID_PARAMETER;
559 }
560 }
561
562 //
563 // Test write disable.
564 //
565 if ((Capabilities & EFI_FVB2_WRITE_DISABLED_CAP) == 0) {
566 if ((NewStatus & EFI_FVB2_WRITE_STATUS) == 0) {
567 return EFI_INVALID_PARAMETER;
568 }
569 }
570
571 //
572 // Test write enable.
573 //
574 if ((Capabilities & EFI_FVB2_WRITE_ENABLED_CAP) == 0) {
575 if (NewStatus & EFI_FVB2_WRITE_STATUS) {
576 return EFI_INVALID_PARAMETER;
577 }
578 }
579
580 //
581 // Test lock.
582 //
583 if ((Capabilities & EFI_FVB2_LOCK_CAP) == 0) {
584 if (NewStatus & EFI_FVB2_LOCK_STATUS) {
585 return EFI_INVALID_PARAMETER;
586 }
587 }
588
589 *AttribPtr = (*AttribPtr) & (0xFFFFFFFF & (~EFI_FVB2_STATUS));
590 *AttribPtr = (*AttribPtr) | NewStatus;
591 *Attributes = *AttribPtr;
592
593 return EFI_SUCCESS;
594 }
595
596 //
597 // FVB protocol APIs.
598 //
599 /**
600 Retrieves the physical address of the device.
601
602 @param[in] This A pointer to EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL.
603 @param[out] Address Output buffer containing the address.
604
605 retval EFI_SUCCESS The function always return successfully.
606
607 **/
608 EFI_STATUS
609 EFIAPI
610 FvbProtocolGetPhysicalAddress (
611 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
612 OUT EFI_PHYSICAL_ADDRESS *Address
613 )
614 {
615 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
616 EFI_FW_VOL_INSTANCE *FvInstance;
617
618 FvbDevice = FVB_DEVICE_FROM_THIS (This);
619 FvInstance = GetFvbInstance(FvbDevice->Instance);
620
621 if (FvInstance != NULL) {
622 *Address = FvInstance->FvBase;
623 }
624
625 return EFI_SUCCESS;
626 }
627
628
629 /**
630 Retrieve the size of a logical block.
631
632 @param[in] This Calling context.
633 @param[in] Lba Indicates which block to return the size for.
634 @param[out] BlockSize A pointer to a caller allocated UINTN in which
635 the size of the block is returned.
636 @param[out] NumOfBlocks A pointer to a caller allocated UINTN in which the
637 number of consecutive blocks starting with Lba is
638 returned. All blocks in this range have a size of
639 BlockSize.
640
641 @retval EFI_SUCCESS The function always return successfully.
642
643 **/
644 EFI_STATUS
645 EFIAPI
646 FvbProtocolGetBlockSize (
647 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
648 IN EFI_LBA Lba,
649 OUT UINTN *BlockSize,
650 OUT UINTN *NumOfBlocks
651 )
652 {
653 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
654
655 DEBUG((EFI_D_INFO,
656 "FvbProtocolGetBlockSize: Lba: 0x%lx BlockSize: 0x%x NumOfBlocks: 0x%x\n",
657 Lba,
658 BlockSize,
659 NumOfBlocks)
660 );
661
662 FvbDevice = FVB_DEVICE_FROM_THIS (This);
663
664 return FvbGetLbaAddress (
665 FvbDevice->Instance,
666 Lba,
667 NULL,
668 BlockSize,
669 NumOfBlocks
670 );
671 }
672
673
674 /**
675 Retrieves Volume attributes. No polarity translations are done.
676
677 @param[in] This Calling context.
678 @param[out] Attributes Output buffer which contains attributes.
679
680 @retval EFI_SUCCESS The function always return successfully.
681
682 **/
683 EFI_STATUS
684 EFIAPI
685 FvbProtocolGetAttributes (
686 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
687 OUT EFI_FVB_ATTRIBUTES_2 *Attributes
688 )
689 {
690 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
691
692 FvbDevice = FVB_DEVICE_FROM_THIS (This);
693
694 *Attributes = FvbGetVolumeAttributes (FvbDevice->Instance);
695
696 DEBUG ((EFI_D_INFO,
697 "FvbProtocolGetAttributes: This: 0x%x Attributes: 0x%x\n",
698 This,
699 *Attributes)
700 );
701
702 return EFI_SUCCESS;
703 }
704
705
706 /**
707 Sets Volume attributes. No polarity translations are done.
708
709 @param[in] This Calling context.
710 @param[out] Attributes Output buffer which contains attributes.
711
712 @retval EFI_SUCCESS The function always return successfully.
713
714 **/
715 EFI_STATUS
716 EFIAPI
717 FvbProtocolSetAttributes (
718 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
719 IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
720 )
721 {
722 EFI_STATUS Status;
723 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
724
725 DEBUG((EFI_D_INFO,
726 "FvbProtocolSetAttributes: Before SET - This: 0x%x Attributes: 0x%x\n",
727 This,
728 *Attributes)
729 );
730
731 FvbDevice = FVB_DEVICE_FROM_THIS (This);
732
733 Status = FvbSetVolumeAttributes (FvbDevice->Instance, Attributes);
734
735 DEBUG((EFI_D_INFO,
736 "FvbProtocolSetAttributes: After SET - This: 0x%x Attributes: 0x%x\n",
737 This,
738 *Attributes)
739 );
740
741 return Status;
742 }
743
744
745 /**
746 The EraseBlock() function erases one or more blocks as denoted by the
747 variable argument list. The entire parameter list of blocks must be verified
748 prior to erasing any blocks. If a block is requested that does not exist
749 within the associated firmware volume (it has a larger index than the last
750 block of the firmware volume), the EraseBlock() function must return
751 EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.
752
753 @param[in] This Calling context.
754 @param[in] ... Starting LBA followed by Number of Lba to erase.
755 a -1 to terminate the list.
756
757 @retval EFI_SUCCESS The erase request was successfully completed.
758 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
759 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
760 could not be written. Firmware device may have been
761 partially erased.
762
763 **/
764 EFI_STATUS
765 EFIAPI
766 FvbProtocolEraseBlocks (
767 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
768 ...
769 )
770 {
771 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
772 EFI_FW_VOL_INSTANCE *FwhInstance;
773 UINTN NumOfBlocks = 0;
774 VA_LIST args;
775 EFI_LBA StartingLba;
776 UINTN NumOfLba;
777 EFI_STATUS Status;
778
779 DEBUG((EFI_D_INFO, "FvbProtocolEraseBlocks: \n"));
780 FvbDevice = FVB_DEVICE_FROM_THIS (This);
781
782 FwhInstance = GetFvbInstance (FvbDevice->Instance);
783
784 if (FwhInstance != NULL) {
785 NumOfBlocks = FwhInstance->NumOfBlocks;
786 }
787
788 VA_START (args, This);
789
790 do {
791 StartingLba = VA_ARG (args, EFI_LBA);
792 if ( StartingLba == EFI_LBA_LIST_TERMINATOR ) {
793 break;
794 }
795
796 NumOfLba = VA_ARG (args, UINTN);
797
798 //
799 // Check input parameters.
800 //
801 if (NumOfLba == 0) {
802 VA_END (args);
803 return EFI_INVALID_PARAMETER;
804 }
805
806 if ( ( StartingLba + NumOfLba ) > NumOfBlocks ) {
807 return EFI_INVALID_PARAMETER;
808 }
809 } while ( 1 );
810
811 VA_END (args);
812
813 VA_START (args, This);
814 do {
815 StartingLba = VA_ARG (args, EFI_LBA);
816 if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
817 break;
818 }
819
820 NumOfLba = VA_ARG (args, UINTN);
821
822 while ( NumOfLba > 0 ) {
823 Status = FvbEraseBlock (FvbDevice->Instance, StartingLba);
824 if ( EFI_ERROR(Status)) {
825 VA_END (args);
826 return Status;
827 }
828 StartingLba ++;
829 NumOfLba --;
830 }
831
832 } while ( 1 );
833
834 VA_END (args);
835
836 return EFI_SUCCESS;
837 }
838
839
840 /**
841 Writes data beginning at Lba:Offset from FV. The write terminates either
842 when *NumBytes of data have been written, or when a block boundary is
843 reached. *NumBytes is updated to reflect the actual number of bytes
844 written. The write opertion does not include erase. This routine will
845 attempt to write only the specified bytes. If the writes do not stick,
846 it will return an error.
847
848 @param[in] This Calling context.
849 @param[in] Lba Block in which to begin write.
850 @param[in] Offset Offset in the block at which to begin write.
851 @param[in,out] NumBytes On input, indicates the requested write size. On
852 output, indicates the actual number of bytes written
853 @param[in] Buffer Buffer containing source data for the write.
854
855 @retval EFI_SUCCESS The firmware volume was written successfully.
856 @retval EFI_BAD_BUFFER_SIZE Write attempted across a LBA boundary. On output,
857 NumBytes contains the total number of bytes
858 actually written.
859 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
860 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
861 could not be written.
862 @retval EFI_INVALID_PARAMETER NumBytes or Buffer are NULL.
863
864 **/
865 EFI_STATUS
866 EFIAPI
867 FvbProtocolWrite (
868 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
869 IN EFI_LBA Lba,
870 IN UINTN Offset,
871 IN OUT UINTN *NumBytes,
872 IN UINT8 *Buffer
873 )
874 {
875
876 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
877
878 FvbDevice = FVB_DEVICE_FROM_THIS (This);
879
880 DEBUG((EFI_D_INFO,
881 "FvbProtocolWrite: Lba: 0x%lx Offset: 0x%x NumBytes: 0x%x, Buffer: 0x%x\n",
882 Lba,
883 Offset,
884 *NumBytes,
885 Buffer)
886 );
887
888 return FvbWriteBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer);
889 }
890
891
892 /**
893 Reads data beginning at Lba:Offset from FV. The Read terminates either
894 when *NumBytes of data have been read, or when a block boundary is
895 reached. *NumBytes is updated to reflect the actual number of bytes
896 written. The write opertion does not include erase. This routine will
897 attempt to write only the specified bytes. If the writes do not stick,
898 it will return an error.
899
900 @param[in] This Calling context.
901 @param[in] Lba Block in which to begin write.
902 @param[in] Offset Offset in the block at which to begin write
903 @param[in,out] NumBytes On input, indicates the requested write size. On
904 output, indicates the actual number of bytes written.
905 @param[in] Buffer Buffer containing source data for the write.
906
907
908 Returns:
909 @retval EFI_SUCCESS The firmware volume was read successfully and
910 contents are in Buffer.
911 @retval EFI_BAD_BUFFER_SIZE Read attempted across a LBA boundary. On output,
912 NumBytes contains the total number of bytes returned
913 in Buffer.
914 @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state
915 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
916 could not be read.
917 @retval EFI_INVALID_PARAMETER NumBytes or Buffer are NULL.
918
919 **/
920 EFI_STATUS
921 EFIAPI
922 FvbProtocolRead (
923 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
924 IN EFI_LBA Lba,
925 IN UINTN Offset,
926 IN OUT UINTN *NumBytes,
927 OUT UINT8 *Buffer
928 )
929 {
930
931 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
932 EFI_STATUS Status;
933
934 FvbDevice = FVB_DEVICE_FROM_THIS (This);
935 Status = FvbReadBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer);
936 DEBUG((EFI_D_INFO,
937 "FvbProtocolRead: Lba: 0x%lx Offset: 0x%x NumBytes: 0x%x, Buffer: 0x%x\n",
938 Lba,
939 Offset,
940 *NumBytes,
941 Buffer)
942 );
943
944 return Status;
945 }
946
947
948 /**
949 Check the integrity of firmware volume header.
950
951 @param[in] FwVolHeader A pointer to a firmware volume header.
952
953 @retval TRUE The firmware volume is consistent.
954 @retval FALSE The firmware volume has corrupted.
955
956 **/
957 BOOLEAN
958 IsFvHeaderValid (
959 IN EFI_PHYSICAL_ADDRESS FvBase,
960 IN CONST EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader
961 )
962 {
963 if (FvBase == PcdGet32(PcdFlashNvStorageVariableBase)) {
964 if (CompareMem (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid, sizeof(EFI_GUID)) != 0 ) {
965 return FALSE;
966 }
967 } else {
968 if (CompareMem (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem2Guid, sizeof(EFI_GUID)) != 0 ) {
969 return FALSE;
970 }
971 }
972 if ( (FwVolHeader->Revision != EFI_FVH_REVISION) ||
973 (FwVolHeader->Signature != EFI_FVH_SIGNATURE) ||
974 (FwVolHeader->FvLength == ((UINTN) -1)) ||
975 ((FwVolHeader->HeaderLength & 0x01 ) !=0) ) {
976 return FALSE;
977 }
978
979 if (CalculateCheckSum16 ((UINT16 *) FwVolHeader, FwVolHeader->HeaderLength) != 0) {
980 return FALSE;
981 }
982
983 return TRUE;
984 }
985
986
987 /**
988 The function does the necessary initialization work for
989 Firmware Volume Block Driver.
990
991 @retval EFI_SUCCESS This funtion always return EFI_SUCCESS.
992 It will ASSERT on errors.
993
994 **/
995 EFI_STATUS
996 FvbInitialize (
997 VOID
998 )
999 {
1000 EFI_FW_VOL_INSTANCE *FwhInstance;
1001 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
1002 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
1003 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;
1004 EFI_PHYSICAL_ADDRESS BaseAddress;
1005 EFI_STATUS Status;
1006 UINTN BufferSize;
1007 UINTN TmpHeaderLength;
1008 UINTN Idx;
1009 UINT32 MaxLbaSize;
1010
1011 //
1012 // Calculate the total size for all firmware volume block instances.
1013 //
1014 BufferSize = 0;
1015 for (Idx = 0; Idx < 1; Idx++) {
1016 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) mPlatformFvBaseAddress[Idx];
1017 BufferSize += (FvHeader->HeaderLength +
1018 sizeof (EFI_FW_VOL_INSTANCE) -
1019 sizeof (EFI_FIRMWARE_VOLUME_HEADER)
1020 );
1021 }
1022
1023 mFvbModuleGlobal.FvInstance = (EFI_FW_VOL_INSTANCE *) AllocateRuntimeZeroPool (BufferSize);
1024 ASSERT (NULL != mFvbModuleGlobal.FvInstance);
1025
1026
1027 MaxLbaSize = 0;
1028 FwhInstance = mFvbModuleGlobal.FvInstance;
1029 mFvbModuleGlobal.NumFv = 0;
1030
1031 for (Idx = 0; Idx < 1; Idx++) {
1032 BaseAddress = mPlatformFvBaseAddress[Idx];
1033 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) BaseAddress;
1034
1035 if (!IsFvHeaderValid (BaseAddress, FwVolHeader)) {
1036 //
1037 // If not valid, get FvbInfo from the information carried in
1038 // FVB driver.
1039 //
1040 DEBUG ((EFI_D_ERROR, "Fvb: FV header @ 0x%lx invalid\n", BaseAddress));
1041 Status = GetFvbInfo (BaseAddress, &FwVolHeader);
1042 ASSERT_EFI_ERROR(Status);
1043 //
1044 // Write back a healthy FV header.
1045 //
1046 DEBUG ((EFI_D_ERROR, "FwBlockService.c: Writing back healthy FV header\n"));
1047 LibFvbFlashDeviceBlockLock ((UINTN)BaseAddress, FwVolHeader->BlockMap->Length, FALSE);
1048
1049 Status = LibFvbFlashDeviceBlockErase ((UINTN)BaseAddress, FwVolHeader->BlockMap->Length);
1050
1051 TmpHeaderLength = (UINTN) FwVolHeader->HeaderLength;
1052 Status = LibFvbFlashDeviceWrite (
1053 (UINTN)BaseAddress,
1054 &TmpHeaderLength,
1055 (UINT8 *) FwVolHeader
1056 );
1057
1058 LibFvbFlashDeviceBlockLock ((UINTN)BaseAddress, FwVolHeader->BlockMap->Length, TRUE);
1059
1060 WriteBackInvalidateDataCacheRange (
1061 (VOID *) (UINTN) BaseAddress,
1062 FwVolHeader->BlockMap->Length
1063 );
1064
1065 }
1066
1067 CopyMem (&(FwhInstance->VolumeHeader), FwVolHeader, FwVolHeader->HeaderLength);
1068
1069 FwVolHeader = &(FwhInstance->VolumeHeader);
1070 FwhInstance->FvBase = (UINTN)BaseAddress;
1071
1072 //
1073 // Process the block map for each FV.
1074 //
1075 FwhInstance->NumOfBlocks = 0;
1076 for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {
1077 //
1078 // Get the maximum size of a block.
1079 //
1080 if (MaxLbaSize < PtrBlockMapEntry->Length) {
1081 MaxLbaSize = PtrBlockMapEntry->Length;
1082 }
1083 FwhInstance->NumOfBlocks += PtrBlockMapEntry->NumBlocks;
1084 }
1085
1086 //
1087 // Add a FVB Protocol Instance.
1088 //
1089 mFvbModuleGlobal.NumFv++;
1090 InstallFvbProtocol (FwhInstance, mFvbModuleGlobal.NumFv - 1);
1091
1092 //
1093 // Move on to the next FwhInstance.
1094 //
1095 FwhInstance = (EFI_FW_VOL_INSTANCE *) ((UINTN)((UINT8 *)FwhInstance) +
1096 FwVolHeader->HeaderLength +
1097 (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER)));
1098
1099 }
1100
1101 return EFI_SUCCESS;
1102 }
1103