summaryrefslogtreecommitdiff
path: root/Core/CORE_DXE/FwVolBlock.c
blob: 9f409ff68eb25397ff575302bffe288f2a3df882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
/*++

Copyright (c) 2004 - 2008, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution.  The full text of the license may be found at        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

Module Name:

  FwVolBlock.c

Abstract:

  Firmware Volume Block protocol..  Consumes FV hobs and creates
  appropriate block protocols.

  Also consumes NT_NON_MM_FV envinronment variable and produces appropriate
  block protocols fro them also... (this is TBD)

--*/

#include "Tiano.h"
#include "FwVolBlock.h"
//*** AMI PORTING BEGIN ***//
// This header contains FFS definitions that do not exist in in FFS.h and other Aptio4 headers
#include "EfiFirmwareVolumeHeader.h"
#include <Protocol/FlashProtocol.h>
//*** AMI PORTING END *****//  

EFI_FW_VOL_BLOCK_DEVICE  mFwVolBlock = {
  FVB_DEVICE_SIGNATURE,
  NULL,
  {
    {
      HARDWARE_DEVICE_PATH,
      HW_MEMMAP_DP,
      (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),
      (UINT8)(sizeof (MEMMAP_DEVICE_PATH) >> 8),
      EfiMemoryMappedIO,
      (EFI_PHYSICAL_ADDRESS)0,
      (EFI_PHYSICAL_ADDRESS)0,
    },
    {
      END_DEVICE_PATH_TYPE,
      END_ENTIRE_DEVICE_PATH_SUBTYPE,
      END_DEVICE_PATH_LENGTH,
      0      
    },
  },
  {
    FwVolBlockGetAttributes,
    (EFI_FVB_SET_ATTRIBUTES)FwVolBlockSetAttributes,
    FwVolBlockGetPhysicalAddress,
    FwVolBlockGetBlockSize,
    FwVolBlockReadBlock,
    (EFI_FVB_WRITE)FwVolBlockWriteBlock,
    (EFI_FVB_ERASE_BLOCKS)FwVolBlockEraseBlock,
    NULL      
  },
  0,
  NULL,
  0,
  0
};




EFI_STATUS
EFIAPI
FwVolBlockGetAttributes (
  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL   *This,
  OUT EFI_FVB_ATTRIBUTES                          *Attributes
  )
/*++

Routine Description:
    Retrieves Volume attributes.  No polarity translations are done.

Arguments:
    This - Calling context
    Attributes - output buffer which contains attributes

Returns:
    EFI_SUCCESS - The firmware volume attributes were returned.

--*/
{
  EFI_FW_VOL_BLOCK_DEVICE               *FvbDevice;
  
  FvbDevice = FVB_DEVICE_FROM_THIS (This);

  //
  // Since we are read only, it's safe to get attributes data from our in-memory copy.
  //
  *Attributes = FvbDevice->FvbAttributes;

  return EFI_SUCCESS;
}


EFI_STATUS
EFIAPI
FwVolBlockSetAttributes (
  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL   *This,
  IN EFI_FVB_ATTRIBUTES                   *Attributes
  )
/*++

Routine Description:
  Modifies the current settings of the firmware volume according to the input parameter.

Arguments:
  This - Calling context
  Attributes - input buffer which contains attributes

Returns:
  EFI_SUCCESS -  The firmware volume attributes were returned.
  EFI_INVALID_PARAMETER  -  The attributes requested are in conflict with the capabilities as
                             declared in the firmware volume header.
  EFI_UNSUPPORTED        -  Not supported.
--*/
{
  return EFI_UNSUPPORTED;
}


EFI_STATUS
EFIAPI
FwVolBlockEraseBlock (
  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL    *This,
  ...
  )
/*++

Routine Description:
  The EraseBlock() function erases one or more blocks as denoted by the 
variable argument list. The entire parameter list of blocks must be verified
prior to erasing any blocks.  If a block is requested that does not exist 
within the associated firmware volume (it has a larger index than the last 
block of the firmware volume), the EraseBlock() function must return
EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.

Arguments:
  This - Calling context
  ...  - Starting LBA followed by Number of Lba to erase. a -1 to terminate
           the list.
    
Returns:
  EFI_SUCCESS   -  The erase request was successfully completed.
  EFI_ACCESS_DENIED   -  The firmware volume is in the WriteDisabled state.
  EFI_DEVICE_ERROR    -  The block device is not functioning correctly and could not be
                         written. The firmware device may have been partially erased.
  EFI_INVALID_PARAMETER  -  One or more of the LBAs listed in the variable argument list do
  EFI_UNSUPPORTED        -  Not supported.
    
--*/
{
//*** AMI PORTING BEGIN ***//
  EFI_FW_VOL_BLOCK_DEVICE       *FvbDevice;
  FLASH_PROTOCOL                *FlashProtocol;
  EFI_STATUS                    Status;
  VA_LIST                       Marker;
  EFI_LBA                       StartLba;
  UINTN                         NumOfLba;
  UINT64                        i;
  UINT8                         *EraseAddress;
  UINTN                         EraseSize;

  Status = CoreLocateProtocol(&gFlashProtocolGuid, NULL, &FlashProtocol);
  if(EFI_ERROR(Status))
    return EFI_UNSUPPORTED;

  FvbDevice = FVB_DEVICE_FROM_THIS (This);
  //
  // Check if This FW can be erased
  //
  if ((FvbDevice->FvbAttributes & EFI_FVB_WRITE_STATUS) == 0) {
    return EFI_ACCESS_DENIED;
  }

  VA_START(Marker, This);
  do {
    StartLba = VA_ARG(Marker, EFI_LBA);
    if(StartLba == EFI_LBA_LIST_TERMINATOR)
      break;
    
    NumOfLba = VA_ARG(Marker, UINTN);
    if(NumOfLba == 0)
      return EFI_INVALID_PARAMETER;

    for(i = StartLba; i < StartLba + NumOfLba; i++) {
      if(i >= FvbDevice->NumBlocks)
        return EFI_INVALID_PARAMETER;

      EraseAddress = (UINT8 *)((UINTN)FvbDevice->BaseAddress + FvbDevice->LbaCache[(UINT32)i].Base);
      EraseSize = FvbDevice->LbaCache[(UINT32)i].Length;
      Status = FlashProtocol->Erase(EraseAddress, EraseSize);
      if(EFI_ERROR(Status))
        return EFI_DEVICE_ERROR;
    }
  } while(Status == EFI_SUCCESS);
  VA_END(Marker);
  return Status;
//*** AMI PORTING END ***//
}


EFI_STATUS
EFIAPI
FwVolBlockReadBlock (
  IN     EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL     *This,
  IN     EFI_LBA                                Lba,
  IN     UINTN                                  Offset,
  IN OUT UINTN                                  *NumBytes,
  IN     UINT8                                  *Buffer
  )
/*++

Routine Description:
  Read the specified number of bytes from the block to the input buffer.

Arguments:
  This          -  Indicates the calling context.
  Lba           -  The starting logical block index to read.
  Offset        -  Offset into the block at which to begin reading.
  NumBytes      -  Pointer to a UINT32. At entry, *NumBytes contains the
                   total size of the buffer. At exit, *NumBytes contains the
                   total number of bytes actually read.
  Buffer        -  Pinter to a caller-allocated buffer that contains the destine
                   for the read.    

Returns:      
  EFI_SUCCESS  -  The firmware volume was read successfully.
  EFI_BAD_BUFFER_SIZE -  The read was attempted across an LBA boundary.
  EFI_ACCESS_DENIED  -  Access denied.
  EFI_DEVICE_ERROR   -  The block device is malfunctioning and could not be read.
--*/
{
  EFI_FW_VOL_BLOCK_DEVICE               *FvbDevice;
  EFI_FIRMWARE_VOLUME_HEADER            *FwVolHeader;
  UINT8                                 *LbaOffset;
  UINTN                                 LbaStart;
  UINTN                                 NumOfBytesRead;
  UINTN                                 LbaIndex;
  
  FvbDevice = FVB_DEVICE_FROM_THIS (This);

  //
  // Check if This FW can be read
  //
  if ((FvbDevice->FvbAttributes & EFI_FVB_READ_STATUS) == 0) {
    return EFI_ACCESS_DENIED;
  }
  
  LbaIndex = (UINTN)Lba;
  if (LbaIndex >= FvbDevice->NumBlocks) {
    //
    // Invalid Lba, read nothing.
    //
    *NumBytes = 0;
    return EFI_BAD_BUFFER_SIZE;
  }
  
  if (Offset > FvbDevice->LbaCache[LbaIndex].Length) {
    //
    // all exceed boundry, read nothing.
    //
    *NumBytes = 0;
    return EFI_BAD_BUFFER_SIZE;
  }
  
  NumOfBytesRead = *NumBytes;
  if (Offset + NumOfBytesRead > FvbDevice->LbaCache[LbaIndex].Length) {
    //
    // partial exceed boundry, read data from current postion to end.
    //
    NumOfBytesRead = FvbDevice->LbaCache[LbaIndex].Length - Offset;
  }
  
  LbaStart = FvbDevice->LbaCache[LbaIndex].Base;
  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)((UINTN)FvbDevice->BaseAddress);
  LbaOffset = (UINT8 *)FwVolHeader + LbaStart + Offset;

  //
  // Perform read operation
  //
  EfiCommonLibCopyMem (Buffer, LbaOffset, NumOfBytesRead);
  
  if (NumOfBytesRead == *NumBytes) {
    return EFI_SUCCESS;
  }
    
  *NumBytes = NumOfBytesRead;
  return EFI_BAD_BUFFER_SIZE;
}
  

EFI_STATUS
EFIAPI
FwVolBlockWriteBlock (
  IN     EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL   *This,
  IN     EFI_LBA                              Lba,
  IN     UINTN                                Offset,
  IN OUT UINTN                                *NumBytes,
  IN     UINT8                                *Buffer
  )
/*++

Routine Description:
  Writes the specified number of bytes from the input buffer to the block.

Arguments:
  This          -  Indicates the calling context.
  Lba           -  The starting logical block index to write to.
  Offset        -  Offset into the block at which to begin writing.
  NumBytes      -  Pointer to a UINT32. At entry, *NumBytes contains the
                   total size of the buffer. At exit, *NumBytes contains the
                   total number of bytes actually written.
  Buffer        -  Pinter to a caller-allocated buffer that contains the source
                   for the write.    

Returns:     
  EFI_SUCCESS  -  The firmware volume was written successfully.
  EFI_BAD_BUFFER_SIZE -  The write was attempted across an LBA boundary. On output,
                         NumBytes contains the total number of bytes actually written.
  EFI_ACCESS_DENIED  -  The firmware volume is in the WriteDisabled state.
  EFI_DEVICE_ERROR   -  The block device is malfunctioning and could not be written.
  EFI_UNSUPPORTED    -  Not supported.
--*/
{
//*** AMI PORTING BEGIN ***//
  EFI_FW_VOL_BLOCK_DEVICE               *FvbDevice;
  EFI_FIRMWARE_VOLUME_HEADER            *FwVolHeader;
  UINT8                                 *LbaOffset;
  UINTN                                 LbaStart;
  UINTN                                 NumOfBytesWritten;
  UINTN                                 LbaIndex;
  EFI_STATUS                            Status;
  FLASH_PROTOCOL                        *FlashProtocol;

  Status = CoreLocateProtocol(&gFlashProtocolGuid, NULL, &FlashProtocol);
  if(EFI_ERROR(Status))
    return EFI_UNSUPPORTED;
  
  FvbDevice = FVB_DEVICE_FROM_THIS (This);

  //
  // Check if This FW can be written
  //
  if ((FvbDevice->FvbAttributes & EFI_FVB_WRITE_STATUS) == 0) {
    return EFI_ACCESS_DENIED;
  }
  
  LbaIndex = (UINTN)Lba;
  if (LbaIndex >= FvbDevice->NumBlocks) {
    //
    // Invalid Lba, write nothing.
    //
    *NumBytes = 0;
    return EFI_BAD_BUFFER_SIZE;
  }
  
  if (Offset > FvbDevice->LbaCache[LbaIndex].Length) {
    //
    // all exceed boundry, write nothing.
    //
    *NumBytes = 0;
    return EFI_BAD_BUFFER_SIZE;
  }
  
  NumOfBytesWritten = *NumBytes;
  if (Offset + NumOfBytesWritten > FvbDevice->LbaCache[LbaIndex].Length) {
    //
    // partial exceed boundry, write data from current postion to end.
    //
    NumOfBytesWritten = FvbDevice->LbaCache[LbaIndex].Length - Offset;
  }
  
  LbaStart = FvbDevice->LbaCache[LbaIndex].Base;
  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)((UINTN)FvbDevice->BaseAddress);
  LbaOffset = (UINT8 *)FwVolHeader + LbaStart + Offset;

  //
  // Perform write operation
  //
  Status = FlashProtocol->Write(LbaOffset, NumOfBytesWritten, Buffer);
  if(EFI_ERROR(Status))
    return EFI_DEVICE_ERROR;
  
  if (NumOfBytesWritten == *NumBytes) {
    return EFI_SUCCESS;
  }
    
  *NumBytes = NumOfBytesWritten;
  return EFI_BAD_BUFFER_SIZE;
//*** AMI PORTING END ***//
}
 

EFI_STATUS
EFIAPI
FwVolBlockGetPhysicalAddress (
  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *This,
  OUT EFI_PHYSICAL_ADDRESS                        *Address
  )
/*++

Routine Description:
  Get Fvb's base address.

Arguments:
  This          -  Indicates the calling context.
  Address       -  Fvb device base address.

Returns:     
  EFI_SUCCESS  -  Successfully got Fvb's base address.
  EFI_UNSUPPORTED -  Not supported.
--*/
{
  EFI_FW_VOL_BLOCK_DEVICE               *FvbDevice;
  
  FvbDevice = FVB_DEVICE_FROM_THIS (This);
  
  if (FvbDevice->FvbAttributes & EFI_FVB_MEMORY_MAPPED) {
    *Address = FvbDevice->BaseAddress;
    return EFI_SUCCESS;
  }
  
  return EFI_UNSUPPORTED;
}


EFI_STATUS
EFIAPI
FwVolBlockGetBlockSize (
  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL   *This,
  IN EFI_LBA                              Lba,
  OUT UINTN                               *BlockSize,
  OUT UINTN                               *NumberOfBlocks
  )
/*++

Routine Description:
  Retrieves the size in bytes of a specific block within a firmware volume.

Arguments:
  This            -  Indicates the calling context.
  Lba             -  Indicates the block for which to return the size.
  BlockSize       -  Pointer to a caller-allocated UINTN in which the size of the
                     block is returned.
  NumberOfBlocks  -  Pointer to a caller-allocated UINTN in which the number of
                     consecutive blocks starting with Lba is returned. All blocks
                     in this range have a size of BlockSize.   
Returns:
  EFI_SUCCESS  -  The firmware volume base address is returned.
  EFI_INVALID_PARAMETER  -  The requested LBA is out of range.
--*/
{
  UINTN                                 TotalBlocks;
  EFI_FW_VOL_BLOCK_DEVICE               *FvbDevice;
  EFI_FV_BLOCK_MAP_ENTRY                *PtrBlockMapEntry;
  EFI_FIRMWARE_VOLUME_HEADER            *FwVolHeader;
  
  FvbDevice = FVB_DEVICE_FROM_THIS (This);
  
  //
  // Do parameter checking
  //
  if (Lba >= FvbDevice->NumBlocks) {
    return EFI_INVALID_PARAMETER;
  }
  
  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)((UINTN)FvbDevice->BaseAddress);
  
  PtrBlockMapEntry = FwVolHeader->FvBlockMap;
  
  //
  // Search the block map for the given block
  //
  TotalBlocks = 0;
  while ((PtrBlockMapEntry->NumBlocks != 0) || (PtrBlockMapEntry->BlockLength !=0 )) {
    TotalBlocks += PtrBlockMapEntry->NumBlocks;
    if (Lba < TotalBlocks) {
      //
      // We find the range
      //
      break;
    }
    
    PtrBlockMapEntry++;
  }
  
  *BlockSize = PtrBlockMapEntry->BlockLength;
  *NumberOfBlocks = TotalBlocks - (UINTN)Lba;
  
  return EFI_SUCCESS;
}


#define SANE_MAX_FW_VOL_SIZE      0x40000000 //1GB
#define SANE_MAX_FW_VOL_NO_BLOCKS SANE_MAX_FW_VOL_SIZE / sizeof(LBA_CACHE)


EFI_STATUS
ProduceFVBProtocolOnBuffer (
  IN EFI_PHYSICAL_ADDRESS   BaseAddress,
  IN UINT64                 Length,
  IN EFI_HANDLE             ParentHandle,
  OUT EFI_HANDLE            *FvProtocol  OPTIONAL
  )
/*++

Routine Description:
    This routine produces a firmware volume block protocol on a given
    buffer. 

Arguments:
    BaseAddress     - base address of the firmware volume image
    Length          - length of the firmware volume image
    ParentHandle    - handle of parent firmware volume, if this
                      image came from an FV image file in another
                      firmware volume (ala capsules)
    FvProtocol      - Firmware volume block protocol produced.
    
Returns:
    EFI_VOLUME_CORRUPTED    - Volume corrupted.
    EFI_OUT_OF_RESOURCES    - No enough buffer to be allocated.
    EFI_SUCCESS             - Successfully produced a FVB protocol on given buffer.
                     
--*/
{
  EFI_STATUS                    Status;
  EFI_FW_VOL_BLOCK_DEVICE       *FvbDev;
  EFI_FIRMWARE_VOLUME_HEADER    *FwVolHeader;
  UINTN                         BlockIndex;
  UINTN                         BlockIndex2;
  UINTN                         LinearOffset;
  EFI_FV_BLOCK_MAP_ENTRY        *PtrBlockMapEntry;

  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)BaseAddress;
  //
  // Validate FV Header, if not as expected, return
  //
  if (FwVolHeader->Signature != EFI_FVH_SIGNATURE || FwVolHeader->FvLength > SANE_MAX_FW_VOL_SIZE) {
    return EFI_VOLUME_CORRUPTED;
  }
  //
  // Allocate EFI_FW_VOL_BLOCK_DEVICE 
  //
  FvbDev = CoreAllocateCopyPool (sizeof (EFI_FW_VOL_BLOCK_DEVICE), &mFwVolBlock);
  if (FvbDev == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  FvbDev->BaseAddress   = BaseAddress;
  FvbDev->FvbAttributes = FwVolHeader->Attributes;
  FvbDev->FwVolBlockInstance.ParentHandle = ParentHandle;

  //
  // Init the block caching fields of the device
  // First, count the number of blocks
  //
  FvbDev->NumBlocks = 0;
  for (PtrBlockMapEntry = FwVolHeader->FvBlockMap;
        PtrBlockMapEntry->NumBlocks != 0;
        PtrBlockMapEntry++) {
    if(PtrBlockMapEntry->NumBlocks < SANE_MAX_FW_VOL_NO_BLOCKS - FvbDev->NumBlocks)
      FvbDev->NumBlocks += PtrBlockMapEntry->NumBlocks;
    else {
      CoreFreePool (FvbDev);
      return EFI_VOLUME_CORRUPTED;
    }
  }
  //
  // Second, allocate the cache
  //
  FvbDev->LbaCache = CoreAllocateBootServicesPool (FvbDev->NumBlocks * sizeof (LBA_CACHE));
  if (FvbDev->LbaCache == NULL) {
    CoreFreePool (FvbDev);
    return EFI_OUT_OF_RESOURCES;
  }
  //
  // Last, fill in the cache with the linear address of the blocks
  //
  BlockIndex = 0;
  LinearOffset = 0;
  for (PtrBlockMapEntry = FwVolHeader->FvBlockMap;
        PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {
    for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {
      FvbDev->LbaCache[BlockIndex].Base = LinearOffset;
      FvbDev->LbaCache[BlockIndex].Length = PtrBlockMapEntry->BlockLength;
      if(PtrBlockMapEntry->BlockLength <= FwVolHeader->FvLength - LinearOffset) {
        LinearOffset += PtrBlockMapEntry->BlockLength;
        BlockIndex++;
      } else {
        CoreFreePool (FvbDev->LbaCache);
        CoreFreePool (FvbDev);
        return EFI_VOLUME_CORRUPTED;
      }
    }
  }

  //
  // Set up the devicepath
  //
  FvbDev->DevicePath.MemMapDevPath.StartingAddress = BaseAddress;
  FvbDev->DevicePath.MemMapDevPath.EndingAddress = BaseAddress + FwVolHeader->FvLength - 1;

  //
  //
  // Attach FvVolBlock Protocol to new handle
  //
  Status = CoreInstallMultipleProtocolInterfaces (
            &FvbDev->Handle,
            &gEfiFirmwareVolumeBlockProtocolGuid,     &FvbDev->FwVolBlockInstance,
            &gEfiDevicePathProtocolGuid,              &FvbDev->DevicePath,
            &gEfiFirmwareVolumeDispatchProtocolGuid,  NULL,
            NULL
            );

  //
  // If they want the handle back, set it.
  //
  if (FvProtocol != NULL) {
    *FvProtocol = FvbDev->Handle;
  }

  return Status;
}


EFI_STATUS
EFIAPI
FwVolBlockDriverInit (
  IN EFI_HANDLE                 ImageHandle,
  IN EFI_SYSTEM_TABLE           *SystemTable
  )
/*++

Routine Description:
    This routine is the driver initialization entry point.  It initializes the
    libraries, consumes FV hobs and NT_NON_MM_FV environment variable and
    produces instances of FW_VOL_BLOCK_PROTOCOL as appropriate.
Arguments:
    ImageHandle   - The image handle.
    SystemTable   - The system table.
Returns:
    EFI_SUCCESS   - Successfully initialized firmware volume block driver.
--*/
{
  EFI_STATUS                    Status;
  VOID                          *HobList;
  EFI_PHYSICAL_ADDRESS          BaseAddress;
  UINT64                        Length;
  EFI_STATUS                    HobStatus;
#if (PI_SPECIFICATION_VERSION >= 0x00010000)
  EFI_GUID                      FileName;
#endif
  //
  // First walk hobs and create appropriate FVs.
  //
  Status = CoreGetConfigTable (&gEfiHobListGuid, &HobList);
  //
  // Core Needs Firmware Volumes to function
  //
  ASSERT_EFI_ERROR (Status);

  BaseAddress = 0;
  Length      = 0;
  HobStatus = GetNextFirmwareVolumeHob (
                  &HobList,
                  &BaseAddress,
                  &Length
                  );
  while (!EFI_ERROR (HobStatus)) {
    //
    // Produce an FVB protocol for it
    //
    ProduceFVBProtocolOnBuffer (BaseAddress, Length, NULL, NULL);
    HobStatus = GetNextFirmwareVolumeHob (
                  &HobList,
                  &BaseAddress,
                  &Length
                  );
  }
  
#if (PI_SPECIFICATION_VERSION >= 0x00010000)
  //
  // First walk hobs and create appropriate FVs.
  //
  Status = CoreGetConfigTable (&gEfiHobListGuid, &HobList);
  //
  // Core Needs Firmware Volumes to function
  //
  ASSERT_EFI_ERROR (Status);

  BaseAddress = 0;
  Length      = 0;

  HobStatus = GetNextFirmwareVolume2Hob (
                  &HobList,
                  &BaseAddress,
                  &Length,
                  &FileName
                  );
  while (!EFI_ERROR (HobStatus)) {
    //
    // Produce an FVB protocol for it
    //
    ProduceFVBProtocolOnBuffer (BaseAddress, Length, NULL, NULL);
    HobStatus = GetNextFirmwareVolume2Hob (
                  &HobList,
                  &BaseAddress,
                  &Length,
                  &FileName
                  );
  }
#endif
  
  return EFI_SUCCESS;
}


EFI_STATUS
CoreProcessFirmwareVolume (
  IN VOID                             *FvHeader,
  IN UINTN                            Size, 
  OUT EFI_HANDLE                      *FVProtocolHandle
  )
/*++

Routine Description:
    This DXE service routine is used to process a firmware volume. In
    particular, it can be called by BDS to process a single firmware
    volume found in a capsule. 

Arguments:
    FvHeader              - pointer to a firmware volume header
    Size                  - the size of the buffer pointed to by FvHeader
    FVProtocolHandle      - the handle on which a firmware volume protocol
                            was produced for the firmware volume passed in.

Returns:
    EFI_OUT_OF_RESOURCES  - if an FVB could not be produced due to lack of 
                            system resources
    EFI_VOLUME_CORRUPTED  - if the volume was corrupted
    EFI_SUCCESS           - a firmware volume protocol was produced for the
                            firmware volume

--*/
{
  VOID        *Ptr;
  EFI_STATUS  Status;

  *FVProtocolHandle = NULL;
  Status = ProduceFVBProtocolOnBuffer ( 
            (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader, 
            (UINT64)Size, 
            NULL, 
            FVProtocolHandle
            );
  //
  // Since in our implementation we use register-protocol-notify to put a
  // FV protocol on the FVB protocol handle, we can't directly verify that
  // the FV protocol was produced. Therefore here we will check the handle
  // and make sure an FV protocol is on it. This indicates that all went 
  // well. Otherwise we have to assume that the volume was corrupted 
  // somehow.
  //
  if (!EFI_ERROR(Status)) {
    Ptr = NULL;
  #if (PI_SPECIFICATION_VERSION < 0x00010000)
    Status = CoreHandleProtocol (*FVProtocolHandle, &gEfiFirmwareVolumeProtocolGuid, &Ptr);
  #else
    Status = CoreHandleProtocol (*FVProtocolHandle, &gEfiFirmwareVolume2ProtocolGuid, &Ptr);
  #endif
    if (EFI_ERROR(Status) || (Ptr == NULL)) {
      return EFI_VOLUME_CORRUPTED;
    }
    return EFI_SUCCESS;
  }
  return Status;
}