da7dce6e13c7fd2ee9b3a4ace4dbabf5f96af325
[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_EFI_ERROR (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_EFI_ERROR (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_FW_VOL_INSTANCE *FwhInstance;
360 EFI_STATUS Status;
361 EFI_STATUS Status1;
362
363 FwhInstance = GetFvbInstance (Instance);
364
365 if ( (NumBytes == NULL) || (Buffer == NULL)) {
366 return (EFI_INVALID_PARAMETER);
367 }
368 if (*NumBytes == 0) {
369 return (EFI_INVALID_PARAMETER);
370 }
371
372 Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL);
373 if (EFI_ERROR(Status)) {
374 return Status;
375 }
376
377 //
378 // Check if the FV is write enabled.
379 //
380 Attributes = FvbGetVolumeAttributes (Instance);
381 if ( (Attributes & EFI_FVB2_WRITE_STATUS) == 0) {
382 return (EFI_ACCESS_DENIED);
383 }
384
385 //
386 // Perform boundary checks and adjust NumBytes.
387 //
388 if (BlockOffset > LbaLength) {
389 return (EFI_INVALID_PARAMETER);
390 }
391
392 if ( LbaLength < ( *NumBytes + BlockOffset ) ) {
393 DEBUG ((EFI_D_ERROR,
394 "FvWriteBlock: Reducing Numbytes from 0x%x to 0x%x\n",
395 *NumBytes,
396 (UINT32)(LbaLength-BlockOffset))
397 );
398 *NumBytes = (UINT32) (LbaLength - BlockOffset);
399 Status = EFI_BAD_BUFFER_SIZE;
400 }
401
402 LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, FALSE);
403
404 Status1 = LibFvbFlashDeviceWrite (LbaAddress + BlockOffset, NumBytes, Buffer);
405
406 LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, TRUE);
407 WriteBackInvalidateDataCacheRange ((VOID *) (LbaAddress + BlockOffset), *NumBytes);
408
409 if ( EFI_ERROR (Status1) ) {
410 return Status1;
411 }
412
413 return Status;
414 }
415
416
417 /**
418 Erases and initializes a firmware volume block.
419
420 @param[in] Instance The FV instance to be erased.
421 @param[in] Lba The logical block index to be erased.
422
423 @retval EFI_SUCCESS The erase request was successfully completed.
424 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
425 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
426 could not be written. Firmware device may have been
427 partially erased.
428 @retval EFI_INVALID_PARAMETER Instance not found.
429
430 **/
431 EFI_STATUS
432 FvbEraseBlock (
433 IN UINTN Instance,
434 IN EFI_LBA Lba
435 )
436 {
437 EFI_FVB_ATTRIBUTES_2 Attributes;
438 UINTN LbaAddress;
439 EFI_FW_VOL_INSTANCE *FwhInstance;
440 UINTN LbaLength;
441 EFI_STATUS Status;
442
443 //
444 // Find the right instance of the FVB private data.
445 //
446 FwhInstance = GetFvbInstance (Instance);
447
448 //
449 // Check if the FV is write enabled.
450 //
451 Attributes = FvbGetVolumeAttributes (Instance);
452
453 if( (Attributes & EFI_FVB2_WRITE_STATUS) == 0) {
454 return (EFI_ACCESS_DENIED);
455 }
456
457 //
458 // Get the starting address of the block for erase.
459 //
460 Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL);
461 if (EFI_ERROR(Status)) {
462 return Status;
463 }
464
465 LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, FALSE);
466
467 Status = LibFvbFlashDeviceBlockErase (LbaAddress, LbaLength);
468
469 LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, TRUE);
470
471 WriteBackInvalidateDataCacheRange ((VOID *) LbaAddress, LbaLength);
472
473 return Status;
474 }
475
476
477 /**
478 Modifies the current settings of the firmware volume according to the
479 input parameter, and returns the new setting of the volume.
480
481 @param[in] Instance The FV instance whose attributes is going to be
482 modified.
483 @param[in] Attributes On input, it is a pointer to EFI_FVB_ATTRIBUTES_2
484 containing the desired firmware volume settings.
485 On successful return, it contains the new settings
486 of the firmware volume.
487
488 @retval EFI_SUCCESS Successfully returns.
489 @retval EFI_ACCESS_DENIED The volume setting is locked and cannot be modified.
490 @retval EFI_INVALID_PARAMETER Instance not found, or The attributes requested are
491 in conflict with the capabilities as declared in the
492 firmware volume header.
493
494 **/
495 STATIC
496 EFI_STATUS
497 FvbSetVolumeAttributes (
498 IN UINTN Instance,
499 IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
500 )
501 {
502 EFI_FW_VOL_INSTANCE *FwhInstance = NULL;
503 EFI_FVB_ATTRIBUTES_2 OldAttributes = 0;
504 EFI_FVB_ATTRIBUTES_2 *AttribPtr = NULL;
505 EFI_FVB_ATTRIBUTES_2 UnchangedAttributes;
506 UINT32 Capabilities;
507 UINT32 OldStatus, NewStatus;
508
509 //
510 // Find the right instance of the FVB private data.
511 //
512 FwhInstance = GetFvbInstance (Instance);
513
514 AttribPtr = (EFI_FVB_ATTRIBUTES_2 *) & (FwhInstance->VolumeHeader.Attributes);
515 ASSERT_EFI_ERROR (AttribPtr != NULL);
516
517 if ( AttribPtr != NULL) {
518 OldAttributes = *AttribPtr;
519 }
520
521 Capabilities = OldAttributes & EFI_FVB2_CAPABILITIES;
522 OldStatus = OldAttributes & EFI_FVB2_STATUS;
523 NewStatus = *Attributes & EFI_FVB2_STATUS;
524
525 UnchangedAttributes = EFI_FVB2_READ_DISABLED_CAP | \
526 EFI_FVB2_READ_ENABLED_CAP | \
527 EFI_FVB2_WRITE_DISABLED_CAP | \
528 EFI_FVB2_WRITE_ENABLED_CAP | \
529 EFI_FVB2_LOCK_CAP | \
530 EFI_FVB2_STICKY_WRITE | \
531 EFI_FVB2_MEMORY_MAPPED | \
532 EFI_FVB2_ERASE_POLARITY | \
533 EFI_FVB2_READ_LOCK_CAP | \
534 EFI_FVB2_WRITE_LOCK_CAP | \
535 EFI_FVB2_ALIGNMENT;
536
537 //
538 // Some attributes of FV is read only can *not* be set.
539 //
540 if ((OldAttributes & UnchangedAttributes) ^ (*Attributes & UnchangedAttributes)) {
541 return EFI_INVALID_PARAMETER;
542 }
543
544 //
545 // If firmware volume is locked, no status bit can be updated.
546 //
547 if ( OldAttributes & EFI_FVB2_LOCK_STATUS ) {
548 if ( OldStatus ^ NewStatus ) {
549 return EFI_ACCESS_DENIED;
550 }
551 }
552
553 //
554 // Test read disable.
555 //
556 if ((Capabilities & EFI_FVB2_READ_DISABLED_CAP) == 0) {
557 if ((NewStatus & EFI_FVB2_READ_STATUS) == 0) {
558 return EFI_INVALID_PARAMETER;
559 }
560 }
561
562 //
563 // Test read enable.
564 //
565 if ((Capabilities & EFI_FVB2_READ_ENABLED_CAP) == 0) {
566 if (NewStatus & EFI_FVB2_READ_STATUS) {
567 return EFI_INVALID_PARAMETER;
568 }
569 }
570
571 //
572 // Test write disable.
573 //
574 if ((Capabilities & EFI_FVB2_WRITE_DISABLED_CAP) == 0) {
575 if ((NewStatus & EFI_FVB2_WRITE_STATUS) == 0) {
576 return EFI_INVALID_PARAMETER;
577 }
578 }
579
580 //
581 // Test write enable.
582 //
583 if ((Capabilities & EFI_FVB2_WRITE_ENABLED_CAP) == 0) {
584 if (NewStatus & EFI_FVB2_WRITE_STATUS) {
585 return EFI_INVALID_PARAMETER;
586 }
587 }
588
589 //
590 // Test lock.
591 //
592 if ((Capabilities & EFI_FVB2_LOCK_CAP) == 0) {
593 if (NewStatus & EFI_FVB2_LOCK_STATUS) {
594 return EFI_INVALID_PARAMETER;
595 }
596 }
597
598 *AttribPtr = (*AttribPtr) & (0xFFFFFFFF & (~EFI_FVB2_STATUS));
599 *AttribPtr = (*AttribPtr) | NewStatus;
600 *Attributes = *AttribPtr;
601
602 return EFI_SUCCESS;
603 }
604
605 //
606 // FVB protocol APIs.
607 //
608 /**
609 Retrieves the physical address of the device.
610
611 @param[in] This A pointer to EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL.
612 @param[out] Address Output buffer containing the address.
613
614 retval EFI_SUCCESS The function always return successfully.
615
616 **/
617 EFI_STATUS
618 EFIAPI
619 FvbProtocolGetPhysicalAddress (
620 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
621 OUT EFI_PHYSICAL_ADDRESS *Address
622 )
623 {
624 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
625 EFI_FW_VOL_INSTANCE *FvInstance;
626
627 FvbDevice = FVB_DEVICE_FROM_THIS (This);
628 FvInstance = GetFvbInstance(FvbDevice->Instance);
629
630 if (FvInstance != NULL) {
631 *Address = FvInstance->FvBase;
632 }
633
634 return EFI_SUCCESS;
635 }
636
637
638 /**
639 Retrieve the size of a logical block.
640
641 @param[in] This Calling context.
642 @param[in] Lba Indicates which block to return the size for.
643 @param[out] BlockSize A pointer to a caller allocated UINTN in which
644 the size of the block is returned.
645 @param[out] NumOfBlocks A pointer to a caller allocated UINTN in which the
646 number of consecutive blocks starting with Lba is
647 returned. All blocks in this range have a size of
648 BlockSize.
649
650 @retval EFI_SUCCESS The function always return successfully.
651
652 **/
653 EFI_STATUS
654 EFIAPI
655 FvbProtocolGetBlockSize (
656 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
657 IN EFI_LBA Lba,
658 OUT UINTN *BlockSize,
659 OUT UINTN *NumOfBlocks
660 )
661 {
662 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
663
664 DEBUG((EFI_D_INFO,
665 "FvbProtocolGetBlockSize: Lba: 0x%lx BlockSize: 0x%x NumOfBlocks: 0x%x\n",
666 Lba,
667 BlockSize,
668 NumOfBlocks)
669 );
670
671 FvbDevice = FVB_DEVICE_FROM_THIS (This);
672
673 return FvbGetLbaAddress (
674 FvbDevice->Instance,
675 Lba,
676 NULL,
677 BlockSize,
678 NumOfBlocks
679 );
680 }
681
682
683 /**
684 Retrieves Volume attributes. No polarity translations are done.
685
686 @param[in] This Calling context.
687 @param[out] Attributes Output buffer which contains attributes.
688
689 @retval EFI_SUCCESS The function always return successfully.
690
691 **/
692 EFI_STATUS
693 EFIAPI
694 FvbProtocolGetAttributes (
695 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
696 OUT EFI_FVB_ATTRIBUTES_2 *Attributes
697 )
698 {
699 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
700
701 FvbDevice = FVB_DEVICE_FROM_THIS (This);
702
703 *Attributes = FvbGetVolumeAttributes (FvbDevice->Instance);
704
705 DEBUG ((EFI_D_INFO,
706 "FvbProtocolGetAttributes: This: 0x%x Attributes: 0x%x\n",
707 This,
708 *Attributes)
709 );
710
711 return EFI_SUCCESS;
712 }
713
714
715 /**
716 Sets Volume attributes. No polarity translations are done.
717
718 @param[in] This Calling context.
719 @param[out] Attributes Output buffer which contains attributes.
720
721 @retval EFI_SUCCESS The function always return successfully.
722
723 **/
724 EFI_STATUS
725 EFIAPI
726 FvbProtocolSetAttributes (
727 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
728 IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
729 )
730 {
731 EFI_STATUS Status;
732 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
733
734 DEBUG((EFI_D_INFO,
735 "FvbProtocolSetAttributes: Before SET - This: 0x%x Attributes: 0x%x\n",
736 This,
737 *Attributes)
738 );
739
740 FvbDevice = FVB_DEVICE_FROM_THIS (This);
741
742 Status = FvbSetVolumeAttributes (FvbDevice->Instance, Attributes);
743
744 DEBUG((EFI_D_INFO,
745 "FvbProtocolSetAttributes: After SET - This: 0x%x Attributes: 0x%x\n",
746 This,
747 *Attributes)
748 );
749
750 return Status;
751 }
752
753
754 /**
755 The EraseBlock() function erases one or more blocks as denoted by the
756 variable argument list. The entire parameter list of blocks must be verified
757 prior to erasing any blocks. If a block is requested that does not exist
758 within the associated firmware volume (it has a larger index than the last
759 block of the firmware volume), the EraseBlock() function must return
760 EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.
761
762 @param[in] This Calling context.
763 @param[in] ... Starting LBA followed by Number of Lba to erase.
764 a -1 to terminate the list.
765
766 @retval EFI_SUCCESS The erase request was successfully completed.
767 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
768 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
769 could not be written. Firmware device may have been
770 partially erased.
771
772 **/
773 EFI_STATUS
774 EFIAPI
775 FvbProtocolEraseBlocks (
776 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
777 ...
778 )
779 {
780 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
781 EFI_FW_VOL_INSTANCE *FwhInstance;
782 UINTN NumOfBlocks = 0;
783 VA_LIST args;
784 EFI_LBA StartingLba;
785 UINTN NumOfLba;
786 EFI_STATUS Status;
787
788 DEBUG((EFI_D_INFO, "FvbProtocolEraseBlocks: \n"));
789 FvbDevice = FVB_DEVICE_FROM_THIS (This);
790
791 FwhInstance = GetFvbInstance (FvbDevice->Instance);
792
793 if (FwhInstance != NULL) {
794 NumOfBlocks = FwhInstance->NumOfBlocks;
795 }
796
797 VA_START (args, This);
798
799 do {
800 StartingLba = VA_ARG (args, EFI_LBA);
801 if ( StartingLba == EFI_LBA_LIST_TERMINATOR ) {
802 break;
803 }
804
805 NumOfLba = VA_ARG (args, UINT32);
806
807 //
808 // Check input parameters.
809 //
810 if (NumOfLba == 0) {
811 VA_END (args);
812 return EFI_INVALID_PARAMETER;
813 }
814
815 if ( ( StartingLba + NumOfLba ) > NumOfBlocks ) {
816 return EFI_INVALID_PARAMETER;
817 }
818 } while ( 1 );
819
820 VA_END (args);
821
822 VA_START (args, This);
823 do {
824 StartingLba = VA_ARG (args, EFI_LBA);
825 if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
826 break;
827 }
828
829 NumOfLba = VA_ARG (args, UINT32);
830
831 while ( NumOfLba > 0 ) {
832 Status = FvbEraseBlock (FvbDevice->Instance, StartingLba);
833 if ( EFI_ERROR(Status)) {
834 VA_END (args);
835 return Status;
836 }
837 StartingLba ++;
838 NumOfLba --;
839 }
840
841 } while ( 1 );
842
843 VA_END (args);
844
845 return EFI_SUCCESS;
846 }
847
848
849 /**
850 Writes data beginning at Lba:Offset from FV. The write terminates either
851 when *NumBytes of data have been written, or when a block boundary is
852 reached. *NumBytes is updated to reflect the actual number of bytes
853 written. The write opertion does not include erase. This routine will
854 attempt to write only the specified bytes. If the writes do not stick,
855 it will return an error.
856
857 @param[in] This Calling context.
858 @param[in] Lba Block in which to begin write.
859 @param[in] Offset Offset in the block at which to begin write.
860 @param[in,out] NumBytes On input, indicates the requested write size. On
861 output, indicates the actual number of bytes written
862 @param[in] Buffer Buffer containing source data for the write.
863
864 @retval EFI_SUCCESS The firmware volume was written successfully.
865 @retval EFI_BAD_BUFFER_SIZE Write attempted across a LBA boundary. On output,
866 NumBytes contains the total number of bytes
867 actually written.
868 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
869 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
870 could not be written.
871 @retval EFI_INVALID_PARAMETER NumBytes or Buffer are NULL.
872
873 **/
874 EFI_STATUS
875 EFIAPI
876 FvbProtocolWrite (
877 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
878 IN EFI_LBA Lba,
879 IN UINTN Offset,
880 IN OUT UINTN *NumBytes,
881 IN UINT8 *Buffer
882 )
883 {
884
885 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
886
887 FvbDevice = FVB_DEVICE_FROM_THIS (This);
888
889 DEBUG((EFI_D_INFO,
890 "FvbProtocolWrite: Lba: 0x%lx Offset: 0x%x NumBytes: 0x%x, Buffer: 0x%x\n",
891 Lba,
892 Offset,
893 *NumBytes,
894 Buffer)
895 );
896
897 return FvbWriteBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer);
898 }
899
900
901 /**
902 Reads data beginning at Lba:Offset from FV. The Read terminates either
903 when *NumBytes of data have been read, or when a block boundary is
904 reached. *NumBytes is updated to reflect the actual number of bytes
905 written. The write opertion does not include erase. This routine will
906 attempt to write only the specified bytes. If the writes do not stick,
907 it will return an error.
908
909 @param[in] This Calling context.
910 @param[in] Lba Block in which to begin write.
911 @param[in] Offset Offset in the block at which to begin write
912 @param[in,out] NumBytes On input, indicates the requested write size. On
913 output, indicates the actual number of bytes written.
914 @param[in] Buffer Buffer containing source data for the write.
915
916
917 Returns:
918 @retval EFI_SUCCESS The firmware volume was read successfully and
919 contents are in Buffer.
920 @retval EFI_BAD_BUFFER_SIZE Read attempted across a LBA boundary. On output,
921 NumBytes contains the total number of bytes returned
922 in Buffer.
923 @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled state
924 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and
925 could not be read.
926 @retval EFI_INVALID_PARAMETER NumBytes or Buffer are NULL.
927
928 **/
929 EFI_STATUS
930 EFIAPI
931 FvbProtocolRead (
932 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
933 IN EFI_LBA Lba,
934 IN UINTN Offset,
935 IN OUT UINTN *NumBytes,
936 OUT UINT8 *Buffer
937 )
938 {
939
940 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
941 EFI_STATUS Status;
942
943 FvbDevice = FVB_DEVICE_FROM_THIS (This);
944 Status = FvbReadBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer);
945 DEBUG((EFI_D_INFO,
946 "FvbProtocolRead: Lba: 0x%lx Offset: 0x%x NumBytes: 0x%x, Buffer: 0x%x\n",
947 Lba,
948 Offset,
949 *NumBytes,
950 Buffer)
951 );
952
953 return Status;
954 }
955
956
957 /**
958 Check the integrity of firmware volume header.
959
960 @param[in] FwVolHeader A pointer to a firmware volume header.
961
962 @retval TRUE The firmware volume is consistent.
963 @retval FALSE The firmware volume has corrupted.
964
965 **/
966 BOOLEAN
967 IsFvHeaderValid (
968 IN EFI_PHYSICAL_ADDRESS FvBase,
969 IN CONST EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader
970 )
971 {
972 if (FvBase == PcdGet32(PcdFlashNvStorageVariableBase)) {
973 if (CompareMem (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid, sizeof(EFI_GUID)) != 0 ) {
974 return FALSE;
975 }
976 } else {
977 if (CompareMem (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem2Guid, sizeof(EFI_GUID)) != 0 ) {
978 return FALSE;
979 }
980 }
981 if ( (FwVolHeader->Revision != EFI_FVH_REVISION) ||
982 (FwVolHeader->Signature != EFI_FVH_SIGNATURE) ||
983 (FwVolHeader->FvLength == ((UINTN) -1)) ||
984 ((FwVolHeader->HeaderLength & 0x01 ) !=0) ) {
985 return FALSE;
986 }
987
988 if (CalculateCheckSum16 ((UINT16 *) FwVolHeader, FwVolHeader->HeaderLength) != 0) {
989 return FALSE;
990 }
991
992 return TRUE;
993 }
994
995
996 /**
997 The function does the necessary initialization work for
998 Firmware Volume Block Driver.
999
1000 @retval EFI_SUCCESS This funtion always return EFI_SUCCESS.
1001 It will ASSERT on errors.
1002
1003 **/
1004 EFI_STATUS
1005 FvbInitialize (
1006 VOID
1007 )
1008 {
1009 EFI_FW_VOL_INSTANCE *FwhInstance;
1010 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
1011 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
1012 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;
1013 EFI_PHYSICAL_ADDRESS BaseAddress;
1014 EFI_STATUS Status;
1015 UINTN BufferSize;
1016 UINTN TmpHeaderLength;
1017 UINTN Idx;
1018 UINT32 MaxLbaSize;
1019 BOOLEAN FvHeaderValid;
1020
1021 //
1022 // Calculate the total size for all firmware volume block instances.
1023 //
1024 BufferSize = 0;
1025 for (Idx = 0; Idx < 1; Idx++) {
1026 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) mPlatformFvBaseAddress[Idx];
1027 BufferSize += (FvHeader->HeaderLength +
1028 sizeof (EFI_FW_VOL_INSTANCE) -
1029 sizeof (EFI_FIRMWARE_VOLUME_HEADER)
1030 );
1031 }
1032
1033 mFvbModuleGlobal.FvInstance = (EFI_FW_VOL_INSTANCE *) AllocateRuntimeZeroPool (BufferSize);
1034 ASSERT (NULL != mFvbModuleGlobal.FvInstance);
1035
1036
1037 MaxLbaSize = 0;
1038 FwhInstance = mFvbModuleGlobal.FvInstance;
1039 mFvbModuleGlobal.NumFv = 0;
1040
1041 for (Idx = 0; Idx < 1; Idx++) {
1042 BaseAddress = mPlatformFvBaseAddress[Idx];
1043 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) BaseAddress;
1044
1045 if (!IsFvHeaderValid (BaseAddress, FwVolHeader)) {
1046 FvHeaderValid = FALSE;
1047 //
1048 // If not valid, get FvbInfo from the information carried in
1049 // FVB driver.
1050 //
1051 DEBUG ((EFI_D_ERROR, "Fvb: FV header @ 0x%lx invalid\n", BaseAddress));
1052 Status = GetFvbInfo (BaseAddress, &FwVolHeader);
1053 ASSERT_EFI_ERROR(Status);
1054 //
1055 // Write back a healthy FV header.
1056 //
1057 DEBUG ((EFI_D_ERROR, "FwBlockService.c: Writing back healthy FV header\n"));
1058 LibFvbFlashDeviceBlockLock ((UINTN)BaseAddress, FwVolHeader->BlockMap->Length, FALSE);
1059
1060 Status = LibFvbFlashDeviceBlockErase ((UINTN)BaseAddress, FwVolHeader->BlockMap->Length);
1061
1062 TmpHeaderLength = (UINTN) FwVolHeader->HeaderLength;
1063 Status = LibFvbFlashDeviceWrite (
1064 (UINTN)BaseAddress,
1065 &TmpHeaderLength,
1066 (UINT8 *) FwVolHeader
1067 );
1068
1069 LibFvbFlashDeviceBlockLock ((UINTN)BaseAddress, FwVolHeader->BlockMap->Length, TRUE);
1070
1071 WriteBackInvalidateDataCacheRange (
1072 (VOID *) (UINTN) BaseAddress,
1073 FwVolHeader->BlockMap->Length
1074 );
1075
1076 }
1077
1078 CopyMem (&(FwhInstance->VolumeHeader), FwVolHeader, FwVolHeader->HeaderLength);
1079
1080 FwVolHeader = &(FwhInstance->VolumeHeader);
1081 FwhInstance->FvBase = (UINTN)BaseAddress;
1082
1083 //
1084 // Process the block map for each FV.
1085 //
1086 FwhInstance->NumOfBlocks = 0;
1087 for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {
1088 //
1089 // Get the maximum size of a block.
1090 //
1091 if (MaxLbaSize < PtrBlockMapEntry->Length) {
1092 MaxLbaSize = PtrBlockMapEntry->Length;
1093 }
1094 FwhInstance->NumOfBlocks += PtrBlockMapEntry->NumBlocks;
1095 }
1096
1097 //
1098 // Add a FVB Protocol Instance.
1099 //
1100 mFvbModuleGlobal.NumFv++;
1101 InstallFvbProtocol (FwhInstance, mFvbModuleGlobal.NumFv - 1);
1102
1103 //
1104 // Move on to the next FwhInstance.
1105 //
1106 FwhInstance = (EFI_FW_VOL_INSTANCE *) ((UINTN)((UINT8 *)FwhInstance) +
1107 FwVolHeader->HeaderLength +
1108 (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER)));
1109
1110 }
1111
1112 return EFI_SUCCESS;
1113 }
1114