]> git.proxmox.com Git - mirror_edk2.git/blame - Nt32Pkg/FvbServicesRuntimeDxe/FWBlockService.c
Replace the FlashMapHob with PCD defined in FDF on Nt32 platform. Currently, the...
[mirror_edk2.git] / Nt32Pkg / FvbServicesRuntimeDxe / FWBlockService.c
CommitLineData
55e6660f 1/*++\r
2\r
3Copyright (c) 2006 - 2007, Intel Corporation\r
4All rights reserved. This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12Module Name:\r
13\r
14 FWBlockService.c\r
15\r
16Abstract:\r
17\r
18Revision History\r
19\r
20--*/\r
21\r
22//\r
23// The package level header files this module uses\r
24//\r
25#include <PiDxe.h>\r
26#include <WinNtDxe.h>\r
27//\r
28// The protocols, PPI and GUID defintions for this module\r
29//\r
30#include <Guid/EventGroup.h>\r
31#include <Protocol/FvbExtension.h>\r
32#include <Protocol/FirmwareVolumeBlock.h>\r
33#include <Guid/AlternateFvBlock.h>\r
34#include <Protocol/DevicePath.h>\r
35//\r
36// The Library classes this module consumes\r
37//\r
38#include <Library/UefiLib.h>\r
39#include <Library/UefiDriverEntryPoint.h>\r
40#include <Library/BaseLib.h>\r
41#include <Library/DxeServicesTableLib.h>\r
42#include <Library/UefiRuntimeLib.h>\r
43#include <Library/DebugLib.h>\r
44#include <Library/HobLib.h>\r
45#include <Library/BaseMemoryLib.h>\r
46#include <Library/MemoryAllocationLib.h>\r
47#include <Library/UefiBootServicesTableLib.h>\r
48\r
49#include "FWBlockService.h"\r
50\r
51#define EFI_FVB2_STATUS (EFI_FVB2_READ_STATUS | EFI_FVB2_WRITE_STATUS | EFI_FVB2_LOCK_STATUS)\r
52\r
53ESAL_FWB_GLOBAL *mFvbModuleGlobal;\r
54\r
55EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate = {\r
56 FVB_DEVICE_SIGNATURE,\r
57 {\r
58 {\r
59 {\r
60 HARDWARE_DEVICE_PATH,\r
61 HW_MEMMAP_DP,\r
62 {\r
63 sizeof (MEMMAP_DEVICE_PATH),\r
64 0\r
65 }\r
66 },\r
67 EfiMemoryMappedIO,\r
68 0,\r
69 0,\r
70 },\r
71 {\r
72 END_DEVICE_PATH_TYPE,\r
73 END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
74 {\r
75 sizeof (EFI_DEVICE_PATH_PROTOCOL),\r
76 0\r
77 }\r
78 }\r
79 },\r
80 0,\r
81 {\r
82 FvbProtocolGetAttributes,\r
83 FvbProtocolSetAttributes,\r
84 FvbProtocolGetPhysicalAddress,\r
85 FvbProtocolGetBlockSize,\r
86 FvbProtocolRead,\r
87 FvbProtocolWrite,\r
88 FvbProtocolEraseBlocks,\r
89 NULL\r
90 },\r
91 {\r
92 FvbExtendProtocolEraseCustomBlockRange\r
93 }\r
94};\r
95\r
96\r
97\r
98VOID\r
99EFIAPI\r
100FvbVirtualddressChangeEvent (\r
101 IN EFI_EVENT Event,\r
102 IN VOID *Context\r
103 )\r
104/*++\r
105\r
106Routine Description:\r
107\r
108 Fixup internal data so that EFI and SAL can be call in virtual mode.\r
109 Call the passed in Child Notify event and convert the mFvbModuleGlobal\r
110 date items to there virtual address.\r
111\r
112 mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] - Physical copy of instance data\r
113 mFvbModuleGlobal->FvInstance[FVB_VIRTUAL] - Virtual pointer to common\r
114 instance data.\r
115\r
116Arguments:\r
117\r
118 (Standard EFI notify event - EFI_EVENT_NOTIFY)\r
119\r
120Returns:\r
121\r
122 None\r
123\r
124--*/\r
125{\r
126 EFI_FW_VOL_INSTANCE *FwhInstance;\r
127 UINTN Index;\r
128\r
129 EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal->FvInstance[FVB_VIRTUAL]);\r
130\r
131 //\r
132 // Convert the base address of all the instances\r
133 //\r
134 Index = 0;\r
135 FwhInstance = mFvbModuleGlobal->FvInstance[FVB_PHYSICAL];\r
136 while (Index < mFvbModuleGlobal->NumFv) {\r
137 EfiConvertPointer (0x0, (VOID **) &FwhInstance->FvBase[FVB_VIRTUAL]);\r
138 FwhInstance = (EFI_FW_VOL_INSTANCE *)\r
139 (\r
140 (UINTN) ((UINT8 *) FwhInstance) + FwhInstance->VolumeHeader.HeaderLength +\r
141 (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))\r
142 );\r
143 Index++;\r
144 }\r
145\r
146 EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal->FvbScratchSpace[FVB_VIRTUAL]);\r
147 EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal);\r
148}\r
149\r
150EFI_STATUS\r
151GetFvbInstance (\r
152 IN UINTN Instance,\r
153 IN ESAL_FWB_GLOBAL *Global,\r
154 OUT EFI_FW_VOL_INSTANCE **FwhInstance,\r
155 IN BOOLEAN Virtual\r
156 )\r
157/*++\r
158\r
159Routine Description:\r
160 Retrieves the physical address of a memory mapped FV\r
161\r
162Arguments:\r
163 Instance - The FV instance whose base address is going to be\r
164 returned\r
165 Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
166 instance data\r
167 FwhInstance - The EFI_FW_VOL_INSTANCE fimrware instance structure\r
168 Virtual - Whether CPU is in virtual or physical mode\r
169\r
170Returns:\r
171 EFI_SUCCESS - Successfully returns\r
172 EFI_INVALID_PARAMETER - Instance not found\r
173\r
174--*/\r
175{\r
176 EFI_FW_VOL_INSTANCE *FwhRecord;\r
177\r
178 if (Instance >= Global->NumFv) {\r
179 return EFI_INVALID_PARAMETER;\r
180 }\r
181 //\r
182 // Find the right instance of the FVB private data\r
183 //\r
184 FwhRecord = Global->FvInstance[Virtual];\r
185 while (Instance > 0) {\r
186 FwhRecord = (EFI_FW_VOL_INSTANCE *)\r
187 (\r
188 (UINTN) ((UINT8 *) FwhRecord) + FwhRecord->VolumeHeader.HeaderLength +\r
189 (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))\r
190 );\r
191 Instance--;\r
192 }\r
193\r
194 *FwhInstance = FwhRecord;\r
195\r
196 return EFI_SUCCESS;\r
197}\r
198\r
199EFI_STATUS\r
200FvbGetPhysicalAddress (\r
201 IN UINTN Instance,\r
202 OUT EFI_PHYSICAL_ADDRESS *Address,\r
203 IN ESAL_FWB_GLOBAL *Global,\r
204 IN BOOLEAN Virtual\r
205 )\r
206/*++\r
207\r
208Routine Description:\r
209 Retrieves the physical address of a memory mapped FV\r
210\r
211Arguments:\r
212 Instance - The FV instance whose base address is going to be\r
213 returned\r
214 Address - Pointer to a caller allocated EFI_PHYSICAL_ADDRESS\r
215 that on successful return, contains the base address\r
216 of the firmware volume.\r
217 Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
218 instance data\r
219 Virtual - Whether CPU is in virtual or physical mode\r
220\r
221Returns:\r
222 EFI_SUCCESS - Successfully returns\r
223 EFI_INVALID_PARAMETER - Instance not found\r
224\r
225--*/\r
226{\r
227 EFI_FW_VOL_INSTANCE *FwhInstance;\r
228 EFI_STATUS Status;\r
229\r
230 //\r
231 // Find the right instance of the FVB private data\r
232 //\r
233 Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);\r
234 ASSERT_EFI_ERROR (Status);\r
235 *Address = FwhInstance->FvBase[Virtual];\r
236\r
237 return EFI_SUCCESS;\r
238}\r
239\r
240EFI_STATUS\r
241FvbGetVolumeAttributes (\r
242 IN UINTN Instance,\r
243 OUT EFI_FVB_ATTRIBUTES *Attributes,\r
244 IN ESAL_FWB_GLOBAL *Global,\r
245 IN BOOLEAN Virtual\r
246 )\r
247/*++\r
248\r
249Routine Description:\r
250 Retrieves attributes, insures positive polarity of attribute bits, returns\r
251 resulting attributes in output parameter\r
252\r
253Arguments:\r
254 Instance - The FV instance whose attributes is going to be\r
255 returned\r
256 Attributes - Output buffer which contains attributes\r
257 Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
258 instance data\r
259 Virtual - Whether CPU is in virtual or physical mode\r
260\r
261Returns:\r
262 EFI_SUCCESS - Successfully returns\r
263 EFI_INVALID_PARAMETER - Instance not found\r
264\r
265--*/\r
266{\r
267 EFI_FW_VOL_INSTANCE *FwhInstance;\r
268 EFI_STATUS Status;\r
269\r
270 //\r
271 // Find the right instance of the FVB private data\r
272 //\r
273 Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);\r
274 ASSERT_EFI_ERROR (Status);\r
275 *Attributes = FwhInstance->VolumeHeader.Attributes;\r
276\r
277 return EFI_SUCCESS;\r
278}\r
279\r
280EFI_STATUS\r
281FvbGetLbaAddress (\r
282 IN UINTN Instance,\r
283 IN EFI_LBA Lba,\r
284 OUT UINTN *LbaAddress,\r
285 OUT UINTN *LbaLength,\r
286 OUT UINTN *NumOfBlocks,\r
287 IN ESAL_FWB_GLOBAL *Global,\r
288 IN BOOLEAN Virtual\r
289 )\r
290/*++\r
291\r
292Routine Description:\r
293 Retrieves the starting address of an LBA in an FV\r
294\r
295Arguments:\r
296 Instance - The FV instance which the Lba belongs to\r
297 Lba - The logical block address\r
298 LbaAddress - On output, contains the physical starting address\r
299 of the Lba\r
300 LbaLength - On output, contains the length of the block\r
301 NumOfBlocks - A pointer to a caller allocated UINTN in which the\r
302 number of consecutive blocks starting with Lba is\r
303 returned. All blocks in this range have a size of\r
304 BlockSize\r
305 Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
306 instance data\r
307 Virtual - Whether CPU is in virtual or physical mode\r
308\r
309Returns:\r
310 EFI_SUCCESS - Successfully returns\r
311 EFI_INVALID_PARAMETER - Instance not found\r
312\r
313--*/\r
314{\r
315 UINT32 NumBlocks;\r
316 UINT32 BlockLength;\r
317 UINTN Offset;\r
318 EFI_LBA StartLba;\r
319 EFI_LBA NextLba;\r
320 EFI_FW_VOL_INSTANCE *FwhInstance;\r
321 EFI_FV_BLOCK_MAP_ENTRY *BlockMap;\r
322 EFI_STATUS Status;\r
323\r
324 //\r
325 // Find the right instance of the FVB private data\r
326 //\r
327 Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);\r
328 ASSERT_EFI_ERROR (Status);\r
329\r
330 StartLba = 0;\r
331 Offset = 0;\r
332 BlockMap = &(FwhInstance->VolumeHeader.BlockMap[0]);\r
333\r
334 //\r
335 // Parse the blockmap of the FV to find which map entry the Lba belongs to\r
336 //\r
337 while (TRUE) {\r
338 NumBlocks = BlockMap->NumBlocks;\r
339 BlockLength = BlockMap->Length;\r
340\r
341 if (NumBlocks == 0 || BlockLength == 0) {\r
342 return EFI_INVALID_PARAMETER;\r
343 }\r
344\r
345 NextLba = StartLba + NumBlocks;\r
346\r
347 //\r
348 // The map entry found\r
349 //\r
350 if (Lba >= StartLba && Lba < NextLba) {\r
351 Offset = Offset + (UINTN) MultU64x32 ((Lba - StartLba), BlockLength);\r
352 if (LbaAddress != NULL) {\r
353 *LbaAddress = FwhInstance->FvBase[Virtual] + Offset;\r
354 }\r
355\r
356 if (LbaLength != NULL) {\r
357 *LbaLength = BlockLength;\r
358 }\r
359\r
360 if (NumOfBlocks != NULL) {\r
361 *NumOfBlocks = (UINTN) (NextLba - Lba);\r
362 }\r
363\r
364 return EFI_SUCCESS;\r
365 }\r
366\r
367 StartLba = NextLba;\r
368 Offset = Offset + NumBlocks * BlockLength;\r
369 BlockMap++;\r
370 }\r
371}\r
372\r
373EFI_STATUS\r
374FvbReadBlock (\r
375 IN UINTN Instance,\r
376 IN EFI_LBA Lba,\r
377 IN UINTN BlockOffset,\r
378 IN OUT UINTN *NumBytes,\r
379 IN UINT8 *Buffer,\r
380 IN ESAL_FWB_GLOBAL *Global,\r
381 IN BOOLEAN Virtual\r
382 )\r
383/*++\r
384\r
385Routine Description:\r
386 Reads specified number of bytes into a buffer from the specified block\r
387\r
388Arguments:\r
389 Instance - The FV instance to be read from\r
390 Lba - The logical block address to be read from\r
391 BlockOffset - Offset into the block at which to begin reading\r
392 NumBytes - Pointer that on input contains the total size of\r
393 the buffer. On output, it contains the total number\r
394 of bytes read\r
395 Buffer - Pointer to a caller allocated buffer that will be\r
396 used to hold the data read\r
397 Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
398 instance data\r
399 Virtual - Whether CPU is in virtual or physical mode\r
400\r
401Returns:\r
402 EFI_SUCCESS - The firmware volume was read successfully and\r
403 contents are in Buffer\r
404 EFI_BAD_BUFFER_SIZE - Read attempted across a LBA boundary. On output,\r
405 NumBytes contains the total number of bytes returned\r
406 in Buffer\r
407 EFI_ACCESS_DENIED - The firmware volume is in the ReadDisabled state\r
408 EFI_DEVICE_ERROR - The block device is not functioning correctly and\r
409 could not be read\r
410 EFI_INVALID_PARAMETER - Instance not found, or NumBytes, Buffer are NULL\r
411\r
412--*/\r
413{\r
414 EFI_FVB_ATTRIBUTES Attributes;\r
415 UINTN LbaAddress;\r
416 UINTN LbaLength;\r
417 EFI_STATUS Status;\r
418\r
419 //\r
420 // Check for invalid conditions\r
421 //\r
422 if ((NumBytes == NULL) || (Buffer == NULL)) {\r
423 return EFI_INVALID_PARAMETER;\r
424 }\r
425\r
426 if (*NumBytes == 0) {\r
427 return EFI_INVALID_PARAMETER;\r
428 }\r
429\r
430 Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL, Global, Virtual);\r
431 if (EFI_ERROR (Status)) {\r
432 return Status;\r
433 }\r
434 //\r
435 // Check if the FV is read enabled\r
436 //\r
437 FvbGetVolumeAttributes (Instance, &Attributes, Global, Virtual);\r
438\r
439 if ((Attributes & EFI_FVB2_READ_STATUS) == 0) {\r
440 return EFI_ACCESS_DENIED;\r
441 }\r
442 //\r
443 // Perform boundary checks and adjust NumBytes\r
444 //\r
445 if (BlockOffset > LbaLength) {\r
446 return EFI_INVALID_PARAMETER;\r
447 }\r
448\r
449 if (LbaLength < (*NumBytes + BlockOffset)) {\r
450 *NumBytes = (UINT32) (LbaLength - BlockOffset);\r
451 Status = EFI_BAD_BUFFER_SIZE;\r
452 }\r
453\r
454 CopyMem (Buffer, (UINT8 *) (LbaAddress + BlockOffset), (UINTN) (*NumBytes));\r
455\r
456 return Status;\r
457}\r
458\r
459EFI_STATUS\r
460FvbWriteBlock (\r
461 IN UINTN Instance,\r
462 IN EFI_LBA Lba,\r
463 IN UINTN BlockOffset,\r
464 IN OUT UINTN *NumBytes,\r
465 IN UINT8 *Buffer,\r
466 IN ESAL_FWB_GLOBAL *Global,\r
467 IN BOOLEAN Virtual\r
468 )\r
469/*++\r
470\r
471Routine Description:\r
472 Writes specified number of bytes from the input buffer to the block\r
473\r
474Arguments:\r
475 Instance - The FV instance to be written to\r
476 Lba - The starting logical block index to write to\r
477 BlockOffset - Offset into the block at which to begin writing\r
478 NumBytes - Pointer that on input contains the total size of\r
479 the buffer. On output, it contains the total number\r
480 of bytes actually written\r
481 Buffer - Pointer to a caller allocated buffer that contains\r
482 the source for the write\r
483 Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
484 instance data\r
485 Virtual - Whether CPU is in virtual or physical mode\r
486\r
487Returns:\r
488 EFI_SUCCESS - The firmware volume was written successfully\r
489 EFI_BAD_BUFFER_SIZE - Write attempted across a LBA boundary. On output,\r
490 NumBytes contains the total number of bytes\r
491 actually written\r
492 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r
493 EFI_DEVICE_ERROR - The block device is not functioning correctly and\r
494 could not be written\r
495 EFI_INVALID_PARAMETER - Instance not found, or NumBytes, Buffer are NULL\r
496\r
497--*/\r
498{\r
499 EFI_FVB_ATTRIBUTES Attributes;\r
500 UINTN LbaAddress;\r
501 UINTN LbaLength;\r
502 EFI_STATUS Status;\r
503\r
504 //\r
505 // Check for invalid conditions\r
506 //\r
507 if ((NumBytes == NULL) || (Buffer == NULL)) {\r
508 return EFI_INVALID_PARAMETER;\r
509 }\r
510\r
511 if (*NumBytes == 0) {\r
512 return EFI_INVALID_PARAMETER;\r
513 }\r
514\r
515 Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL, Global, Virtual);\r
516 if (EFI_ERROR (Status)) {\r
517 return Status;\r
518 }\r
519 //\r
520 // Check if the FV is write enabled\r
521 //\r
522 FvbGetVolumeAttributes (Instance, &Attributes, Global, Virtual);\r
523\r
524 if ((Attributes & EFI_FVB2_WRITE_STATUS) == 0) {\r
525 return EFI_ACCESS_DENIED;\r
526 }\r
527 //\r
528 // Perform boundary checks and adjust NumBytes\r
529 //\r
530 if (BlockOffset > LbaLength) {\r
531 return EFI_INVALID_PARAMETER;\r
532 }\r
533\r
534 if (LbaLength < (*NumBytes + BlockOffset)) {\r
535 *NumBytes = (UINT32) (LbaLength - BlockOffset);\r
536 Status = EFI_BAD_BUFFER_SIZE;\r
537 }\r
538 //\r
539 // Write data\r
540 //\r
541 CopyMem ((UINT8 *) (LbaAddress + BlockOffset), Buffer, (UINTN) (*NumBytes));\r
542\r
543 return Status;\r
544}\r
545\r
546EFI_STATUS\r
547FvbEraseBlock (\r
548 IN UINTN Instance,\r
549 IN EFI_LBA Lba,\r
550 IN ESAL_FWB_GLOBAL *Global,\r
551 IN BOOLEAN Virtual\r
552 )\r
553/*++\r
554\r
555Routine Description:\r
556 Erases and initializes a firmware volume block\r
557\r
558Arguments:\r
559 Instance - The FV instance to be erased\r
560 Lba - The logical block index to be erased\r
561 Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
562 instance data\r
563 Virtual - Whether CPU is in virtual or physical mode\r
564\r
565Returns:\r
566 EFI_SUCCESS - The erase request was successfully completed\r
567 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r
568 EFI_DEVICE_ERROR - The block device is not functioning correctly and\r
569 could not be written. Firmware device may have been\r
570 partially erased\r
571 EFI_INVALID_PARAMETER - Instance not found\r
572\r
573--*/\r
574{\r
575\r
576 EFI_FVB_ATTRIBUTES Attributes;\r
577 UINTN LbaAddress;\r
578 UINTN LbaLength;\r
579 EFI_STATUS Status;\r
580 UINT8 Data;\r
581\r
582 //\r
583 // Check if the FV is write enabled\r
584 //\r
585 FvbGetVolumeAttributes (Instance, &Attributes, Global, Virtual);\r
586\r
587 if ((Attributes & EFI_FVB2_WRITE_STATUS) == 0) {\r
588 return EFI_ACCESS_DENIED;\r
589 }\r
590 //\r
591 // Get the starting address of the block for erase.\r
592 //\r
593 Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL, Global, Virtual);\r
594\r
595 if (EFI_ERROR (Status)) {\r
596 return Status;\r
597 }\r
598\r
599 if ((Attributes & EFI_FVB2_ERASE_POLARITY) != 0) {\r
600 Data = 0xFF;\r
601 } else {\r
602 Data = 0x0;\r
603 }\r
604\r
605 SetMem ((UINT8 *) LbaAddress, LbaLength, Data);\r
606\r
607 return EFI_SUCCESS;\r
608}\r
609\r
610EFI_STATUS\r
611FvbEraseCustomBlockRange (\r
612 IN UINTN Instance,\r
613 IN EFI_LBA StartLba,\r
614 IN UINTN OffsetStartLba,\r
615 IN EFI_LBA LastLba,\r
616 IN UINTN OffsetLastLba,\r
617 IN ESAL_FWB_GLOBAL *Global,\r
618 IN BOOLEAN Virtual\r
619 )\r
620/*++\r
621\r
622Routine Description:\r
623 Erases and initializes a specified range of a firmware volume\r
624\r
625Arguments:\r
626 Instance - The FV instance to be erased\r
627 StartLba - The starting logical block index to be erased\r
628 OffsetStartLba - Offset into the starting block at which to\r
629 begin erasing\r
630 LastLba - The last logical block index to be erased\r
631 OffsetStartLba - Offset into the last block at which to end erasing\r
632 Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
633 instance data\r
634 Virtual - Whether CPU is in virtual or physical mode\r
635\r
636Returns:\r
637 EFI_SUCCESS - The firmware volume was erased successfully\r
638 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r
639 EFI_DEVICE_ERROR - The block device is not functioning correctly and\r
640 could not be written. Firmware device may have been\r
641 partially erased\r
642 EFI_INVALID_PARAMETER - Instance not found\r
643\r
644--*/\r
645{\r
646 EFI_LBA Index;\r
647 UINTN LbaSize;\r
648 UINTN ScratchLbaSizeData;\r
649\r
650 //\r
651 // First LBA\r
652 //\r
653 FvbGetLbaAddress (Instance, StartLba, NULL, &LbaSize, NULL, Global, Virtual);\r
654\r
655 //\r
656 // Use the scratch space as the intermediate buffer to transfer data\r
657 // Back up the first LBA in scratch space.\r
658 //\r
659 FvbReadBlock (Instance, StartLba, 0, &LbaSize, Global->FvbScratchSpace[Virtual], Global, Virtual);\r
660\r
661 //\r
662 // erase now\r
663 //\r
664 FvbEraseBlock (Instance, StartLba, Global, Virtual);\r
665 ScratchLbaSizeData = OffsetStartLba;\r
666\r
667 //\r
668 // write the data back to the first block\r
669 //\r
670 if (ScratchLbaSizeData > 0) {\r
671 FvbWriteBlock (Instance, StartLba, 0, &ScratchLbaSizeData, Global->FvbScratchSpace[Virtual], Global, Virtual);\r
672 }\r
673 //\r
674 // Middle LBAs\r
675 //\r
676 if (LastLba > (StartLba + 1)) {\r
677 for (Index = (StartLba + 1); Index <= (LastLba - 1); Index++) {\r
678 FvbEraseBlock (Instance, Index, Global, Virtual);\r
679 }\r
680 }\r
681 //\r
682 // Last LBAs, the same as first LBAs\r
683 //\r
684 if (LastLba > StartLba) {\r
685 FvbGetLbaAddress (Instance, LastLba, NULL, &LbaSize, NULL, Global, Virtual);\r
686 FvbReadBlock (Instance, LastLba, 0, &LbaSize, Global->FvbScratchSpace[Virtual], Global, Virtual);\r
687 FvbEraseBlock (Instance, LastLba, Global, Virtual);\r
688 }\r
689\r
690 ScratchLbaSizeData = LbaSize - (OffsetStartLba + 1);\r
691\r
692 return FvbWriteBlock (\r
693 Instance,\r
694 LastLba,\r
695 (OffsetLastLba + 1),\r
696 &ScratchLbaSizeData,\r
697 Global->FvbScratchSpace[Virtual],\r
698 Global,\r
699 Virtual\r
700 );\r
701}\r
702\r
703EFI_STATUS\r
704FvbSetVolumeAttributes (\r
705 IN UINTN Instance,\r
706 IN OUT EFI_FVB_ATTRIBUTES *Attributes,\r
707 IN ESAL_FWB_GLOBAL *Global,\r
708 IN BOOLEAN Virtual\r
709 )\r
710/*++\r
711\r
712Routine Description:\r
713 Modifies the current settings of the firmware volume according to the\r
714 input parameter, and returns the new setting of the volume\r
715\r
716Arguments:\r
717 Instance - The FV instance whose attributes is going to be\r
718 modified\r
719 Attributes - On input, it is a pointer to EFI_FVB_ATTRIBUTES\r
720 containing the desired firmware volume settings.\r
721 On successful return, it contains the new settings\r
722 of the firmware volume\r
723 Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
724 instance data\r
725 Virtual - Whether CPU is in virtual or physical mode\r
726\r
727Returns:\r
728 EFI_SUCCESS - Successfully returns\r
729 EFI_ACCESS_DENIED - The volume setting is locked and cannot be modified\r
730 EFI_INVALID_PARAMETER - Instance not found, or The attributes requested are\r
731 in conflict with the capabilities as declared in the\r
732 firmware volume header\r
733\r
734--*/\r
735{\r
736 EFI_FW_VOL_INSTANCE *FwhInstance;\r
737 EFI_FVB_ATTRIBUTES OldAttributes;\r
738 EFI_FVB_ATTRIBUTES *AttribPtr;\r
739 UINT32 Capabilities;\r
740 UINT32 OldStatus;\r
741 UINT32 NewStatus;\r
742 EFI_STATUS Status;\r
743\r
744 //\r
745 // Find the right instance of the FVB private data\r
746 //\r
747 Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);\r
748 ASSERT_EFI_ERROR (Status);\r
749\r
750 AttribPtr = (EFI_FVB_ATTRIBUTES *) &(FwhInstance->VolumeHeader.Attributes);\r
751 OldAttributes = *AttribPtr;\r
752 Capabilities = OldAttributes & (EFI_FVB2_READ_DISABLED_CAP | \\r
753 EFI_FVB2_READ_ENABLED_CAP | \\r
754 EFI_FVB2_WRITE_DISABLED_CAP | \\r
755 EFI_FVB2_WRITE_ENABLED_CAP | \\r
756 EFI_FVB2_LOCK_CAP \\r
757 );\r
758 OldStatus = OldAttributes & EFI_FVB2_STATUS;\r
759 NewStatus = *Attributes & EFI_FVB2_STATUS;\r
760\r
761 //\r
762 // If firmware volume is locked, no status bit can be updated\r
763 //\r
764 if (OldAttributes & EFI_FVB2_LOCK_STATUS) {\r
765 if (OldStatus ^ NewStatus) {\r
766 return EFI_ACCESS_DENIED;\r
767 }\r
768 }\r
769 //\r
770 // Test read disable\r
771 //\r
772 if ((Capabilities & EFI_FVB2_READ_DISABLED_CAP) == 0) {\r
773 if ((NewStatus & EFI_FVB2_READ_STATUS) == 0) {\r
774 return EFI_INVALID_PARAMETER;\r
775 }\r
776 }\r
777 //\r
778 // Test read enable\r
779 //\r
780 if ((Capabilities & EFI_FVB2_READ_ENABLED_CAP) == 0) {\r
781 if (NewStatus & EFI_FVB2_READ_STATUS) {\r
782 return EFI_INVALID_PARAMETER;\r
783 }\r
784 }\r
785 //\r
786 // Test write disable\r
787 //\r
788 if ((Capabilities & EFI_FVB2_WRITE_DISABLED_CAP) == 0) {\r
789 if ((NewStatus & EFI_FVB2_WRITE_STATUS) == 0) {\r
790 return EFI_INVALID_PARAMETER;\r
791 }\r
792 }\r
793 //\r
794 // Test write enable\r
795 //\r
796 if ((Capabilities & EFI_FVB2_WRITE_ENABLED_CAP) == 0) {\r
797 if (NewStatus & EFI_FVB2_WRITE_STATUS) {\r
798 return EFI_INVALID_PARAMETER;\r
799 }\r
800 }\r
801 //\r
802 // Test lock\r
803 //\r
804 if ((Capabilities & EFI_FVB2_LOCK_CAP) == 0) {\r
805 if (NewStatus & EFI_FVB2_LOCK_STATUS) {\r
806 return EFI_INVALID_PARAMETER;\r
807 }\r
808 }\r
809\r
810 *AttribPtr = (*AttribPtr) & (0xFFFFFFFF & (~EFI_FVB2_STATUS));\r
811 *AttribPtr = (*AttribPtr) | NewStatus;\r
812 *Attributes = *AttribPtr;\r
813\r
814 return EFI_SUCCESS;\r
815}\r
816//\r
817// FVB protocol APIs\r
818//\r
819EFI_STATUS\r
820EFIAPI\r
821FvbProtocolGetPhysicalAddress (\r
822 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r
823 OUT EFI_PHYSICAL_ADDRESS *Address\r
824 )\r
825/*++\r
826\r
827Routine Description:\r
828\r
829 Retrieves the physical address of the device.\r
830\r
831Arguments:\r
832\r
833 This - Calling context\r
834 Address - Output buffer containing the address.\r
835\r
836Returns:\r
837\r
838Returns:\r
839 EFI_SUCCESS - Successfully returns\r
840\r
841--*/\r
842{\r
843 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
844\r
845 FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
846\r
847 return FvbGetPhysicalAddress (FvbDevice->Instance, Address, mFvbModuleGlobal, EfiGoneVirtual ());\r
848}\r
849\r
850EFI_STATUS\r
851EFIAPI\r
852FvbProtocolGetBlockSize (\r
853 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r
854 IN CONST EFI_LBA Lba,\r
855 OUT UINTN *BlockSize,\r
856 OUT UINTN *NumOfBlocks\r
857 )\r
858/*++\r
859\r
860Routine Description:\r
861 Retrieve the size of a logical block\r
862\r
863Arguments:\r
864 This - Calling context\r
865 Lba - Indicates which block to return the size for.\r
866 BlockSize - A pointer to a caller allocated UINTN in which\r
867 the size of the block is returned\r
868 NumOfBlocks - a pointer to a caller allocated UINTN in which the\r
869 number of consecutive blocks starting with Lba is\r
870 returned. All blocks in this range have a size of\r
871 BlockSize\r
872\r
873Returns:\r
874 EFI_SUCCESS - The firmware volume was read successfully and\r
875 contents are in Buffer\r
876\r
877--*/\r
878{\r
879 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
880\r
881 FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
882\r
883 return FvbGetLbaAddress (\r
884 FvbDevice->Instance,\r
885 Lba,\r
886 NULL,\r
887 BlockSize,\r
888 NumOfBlocks,\r
889 mFvbModuleGlobal,\r
890 EfiGoneVirtual ()\r
891 );\r
892}\r
893\r
894EFI_STATUS\r
895EFIAPI\r
896FvbProtocolGetAttributes (\r
897 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r
898 OUT EFI_FVB_ATTRIBUTES *Attributes\r
899 )\r
900/*++\r
901\r
902Routine Description:\r
903 Retrieves Volume attributes. No polarity translations are done.\r
904\r
905Arguments:\r
906 This - Calling context\r
907 Attributes - output buffer which contains attributes\r
908\r
909Returns:\r
910 EFI_SUCCESS - Successfully returns\r
911\r
912--*/\r
913{\r
914 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
915\r
916 FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
917\r
918 return FvbGetVolumeAttributes (FvbDevice->Instance, Attributes, mFvbModuleGlobal, EfiGoneVirtual ());\r
919}\r
920\r
921EFI_STATUS\r
922EFIAPI\r
923FvbProtocolSetAttributes (\r
924 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r
925 IN OUT EFI_FVB_ATTRIBUTES *Attributes\r
926 )\r
927/*++\r
928\r
929Routine Description:\r
930 Sets Volume attributes. No polarity translations are done.\r
931\r
932Arguments:\r
933 This - Calling context\r
934 Attributes - output buffer which contains attributes\r
935\r
936Returns:\r
937 EFI_SUCCESS - Successfully returns\r
938\r
939--*/\r
940{\r
941 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
942\r
943 FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
944\r
945 return FvbSetVolumeAttributes (FvbDevice->Instance, Attributes, mFvbModuleGlobal, EfiGoneVirtual ());\r
946}\r
947\r
948EFI_STATUS\r
949EFIAPI\r
950FvbProtocolEraseBlocks (\r
951 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r
952 ...\r
953 )\r
954/*++\r
955\r
956Routine Description:\r
957\r
958 The EraseBlock() function erases one or more blocks as denoted by the\r
959 variable argument list. The entire parameter list of blocks must be verified\r
960 prior to erasing any blocks. If a block is requested that does not exist\r
961 within the associated firmware volume (it has a larger index than the last\r
962 block of the firmware volume), the EraseBlock() function must return\r
963 EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.\r
964\r
965Arguments:\r
966 This - Calling context\r
967 ... - Starting LBA followed by Number of Lba to erase.\r
968 a -1 to terminate the list.\r
969\r
970Returns:\r
971 EFI_SUCCESS - The erase request was successfully completed\r
972 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r
973 EFI_DEVICE_ERROR - The block device is not functioning correctly and\r
974 could not be written. Firmware device may have been\r
975 partially erased\r
976\r
977--*/\r
978{\r
979 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
980 EFI_FW_VOL_INSTANCE *FwhInstance;\r
981 UINTN NumOfBlocks;\r
982 VA_LIST args;\r
983 EFI_LBA StartingLba;\r
984 UINTN NumOfLba;\r
985 EFI_STATUS Status;\r
986\r
987 FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
988\r
989 Status = GetFvbInstance (FvbDevice->Instance, mFvbModuleGlobal, &FwhInstance, EfiGoneVirtual ());\r
990 ASSERT_EFI_ERROR (Status);\r
991\r
992 NumOfBlocks = FwhInstance->NumOfBlocks;\r
993\r
994 VA_START (args, This);\r
995\r
996 do {\r
997 StartingLba = VA_ARG (args, EFI_LBA);\r
998 if (StartingLba == EFI_LBA_LIST_TERMINATOR) {\r
999 break;\r
1000 }\r
1001\r
1002 NumOfLba = VA_ARG (args, UINT32);\r
1003\r
1004 //\r
1005 // Check input parameters\r
1006 //\r
1007 if (NumOfLba == 0) {\r
1008 VA_END (args);\r
1009 return EFI_INVALID_PARAMETER;\r
1010 }\r
1011\r
1012 if ((StartingLba + NumOfLba) > NumOfBlocks) {\r
1013 return EFI_INVALID_PARAMETER;\r
1014 }\r
1015 } while (1);\r
1016\r
1017 VA_END (args);\r
1018\r
1019 VA_START (args, This);\r
1020 do {\r
1021 StartingLba = VA_ARG (args, EFI_LBA);\r
1022 if (StartingLba == EFI_LBA_LIST_TERMINATOR) {\r
1023 break;\r
1024 }\r
1025\r
1026 NumOfLba = VA_ARG (args, UINT32);\r
1027\r
1028 while (NumOfLba > 0) {\r
1029 Status = FvbEraseBlock (FvbDevice->Instance, StartingLba, mFvbModuleGlobal, EfiGoneVirtual ());\r
1030 if (EFI_ERROR (Status)) {\r
1031 VA_END (args);\r
1032 return Status;\r
1033 }\r
1034\r
1035 StartingLba++;\r
1036 NumOfLba--;\r
1037 }\r
1038\r
1039 } while (1);\r
1040\r
1041 VA_END (args);\r
1042\r
1043 return EFI_SUCCESS;\r
1044}\r
1045\r
1046EFI_STATUS\r
1047EFIAPI\r
1048FvbProtocolWrite (\r
1049 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r
1050 IN CONST EFI_LBA Lba,\r
1051 IN CONST UINTN Offset,\r
1052 IN OUT UINTN *NumBytes,\r
1053 IN CONST UINT8 *Buffer\r
1054 )\r
1055/*++\r
1056\r
1057Routine Description:\r
1058\r
1059 Writes data beginning at Lba:Offset from FV. The write terminates either\r
1060 when *NumBytes of data have been written, or when a block boundary is\r
1061 reached. *NumBytes is updated to reflect the actual number of bytes\r
1062 written. The write opertion does not include erase. This routine will\r
1063 attempt to write only the specified bytes. If the writes do not stick,\r
1064 it will return an error.\r
1065\r
1066Arguments:\r
1067 This - Calling context\r
1068 Lba - Block in which to begin write\r
1069 Offset - Offset in the block at which to begin write\r
1070 NumBytes - On input, indicates the requested write size. On\r
1071 output, indicates the actual number of bytes written\r
1072 Buffer - Buffer containing source data for the write.\r
1073\r
1074Returns:\r
1075 EFI_SUCCESS - The firmware volume was written successfully\r
1076 EFI_BAD_BUFFER_SIZE - Write attempted across a LBA boundary. On output,\r
1077 NumBytes contains the total number of bytes\r
1078 actually written\r
1079 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r
1080 EFI_DEVICE_ERROR - The block device is not functioning correctly and\r
1081 could not be written\r
1082 EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL\r
1083\r
1084--*/\r
1085{\r
1086\r
1087 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
1088\r
1089 FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
1090\r
1091 return FvbWriteBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer, mFvbModuleGlobal, EfiGoneVirtual ());\r
1092}\r
1093\r
1094EFI_STATUS\r
1095EFIAPI\r
1096FvbProtocolRead (\r
1097 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r
1098 IN CONST EFI_LBA Lba,\r
1099 IN CONST UINTN Offset,\r
1100 IN OUT UINTN *NumBytes,\r
1101 IN UINT8 *Buffer\r
1102 )\r
1103/*++\r
1104\r
1105Routine Description:\r
1106\r
1107 Reads data beginning at Lba:Offset from FV. The Read terminates either\r
1108 when *NumBytes of data have been read, or when a block boundary is\r
1109 reached. *NumBytes is updated to reflect the actual number of bytes\r
1110 written. The write opertion does not include erase. This routine will\r
1111 attempt to write only the specified bytes. If the writes do not stick,\r
1112 it will return an error.\r
1113\r
1114Arguments:\r
1115 This - Calling context\r
1116 Lba - Block in which to begin Read\r
1117 Offset - Offset in the block at which to begin Read\r
1118 NumBytes - On input, indicates the requested write size. On\r
1119 output, indicates the actual number of bytes Read\r
1120 Buffer - Buffer containing source data for the Read.\r
1121\r
1122Returns:\r
1123 EFI_SUCCESS - The firmware volume was read successfully and\r
1124 contents are in Buffer\r
1125 EFI_BAD_BUFFER_SIZE - Read attempted across a LBA boundary. On output,\r
1126 NumBytes contains the total number of bytes returned\r
1127 in Buffer\r
1128 EFI_ACCESS_DENIED - The firmware volume is in the ReadDisabled state\r
1129 EFI_DEVICE_ERROR - The block device is not functioning correctly and\r
1130 could not be read\r
1131 EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL\r
1132\r
1133--*/\r
1134{\r
1135\r
1136 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
1137\r
1138 FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
1139\r
1140 return FvbReadBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer, mFvbModuleGlobal, EfiGoneVirtual ());\r
1141}\r
1142//\r
1143// FVB Extension Protocols\r
1144//\r
1145EFI_STATUS\r
1146EFIAPI\r
1147FvbExtendProtocolEraseCustomBlockRange (\r
1148 IN EFI_FVB_EXTENSION_PROTOCOL *This,\r
1149 IN EFI_LBA StartLba,\r
1150 IN UINTN OffsetStartLba,\r
1151 IN EFI_LBA LastLba,\r
1152 IN UINTN OffsetLastLba\r
1153 )\r
1154/*++\r
1155\r
1156Routine Description:\r
1157 Erases and initializes a specified range of a firmware volume\r
1158\r
1159Arguments:\r
1160 This - Calling context\r
1161 StartLba - The starting logical block index to be erased\r
1162 OffsetStartLba - Offset into the starting block at which to\r
1163 begin erasing\r
1164 LastLba - The last logical block index to be erased\r
1165 OffsetStartLba - Offset into the last block at which to end erasing\r
1166\r
1167Returns:\r
1168 EFI_SUCCESS - The firmware volume was erased successfully\r
1169 EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r
1170 EFI_DEVICE_ERROR - The block device is not functioning correctly and\r
1171 could not be written. Firmware device may have been\r
1172 partially erased\r
1173\r
1174--*/\r
1175{\r
1176 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
1177\r
1178 FvbDevice = FVB_EXTEND_DEVICE_FROM_THIS (This);\r
1179\r
1180 return FvbEraseCustomBlockRange (\r
1181 FvbDevice->Instance,\r
1182 StartLba,\r
1183 OffsetStartLba,\r
1184 LastLba,\r
1185 OffsetLastLba,\r
1186 mFvbModuleGlobal,\r
1187 EfiGoneVirtual ()\r
1188 );\r
1189}\r
1190\r
1191STATIC\r
1192EFI_STATUS\r
1193ValidateFvHeader (\r
1194 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader\r
1195 )\r
1196/*++\r
1197\r
1198Routine Description:\r
1199 Check the integrity of firmware volume header\r
1200\r
1201Arguments:\r
1202 FwVolHeader - A pointer to a firmware volume header\r
1203\r
1204Returns:\r
1205 EFI_SUCCESS - The firmware volume is consistent\r
1206 EFI_NOT_FOUND - The firmware volume has corrupted. So it is not an FV\r
1207\r
1208--*/\r
1209{\r
1210 UINT16 *Ptr;\r
1211 UINT16 HeaderLength;\r
1212 UINT16 Checksum;\r
1213\r
1214 //\r
1215 // Verify the header revision, header signature, length\r
1216 // Length of FvBlock cannot be 2**64-1\r
1217 // HeaderLength cannot be an odd number\r
1218 //\r
1219 if ((FwVolHeader->Revision != EFI_FVH_REVISION) ||\r
1220 (FwVolHeader->Signature != EFI_FVH_SIGNATURE) ||\r
1221 (FwVolHeader->FvLength == ((UINTN) -1)) ||\r
1222 ((FwVolHeader->HeaderLength & 0x01) != 0)\r
1223 ) {\r
1224 return EFI_NOT_FOUND;\r
1225 }\r
1226 //\r
1227 // Verify the header checksum\r
1228 //\r
1229 HeaderLength = (UINT16) (FwVolHeader->HeaderLength / 2);\r
1230 Ptr = (UINT16 *) FwVolHeader;\r
1231 Checksum = 0;\r
1232 while (HeaderLength > 0) {\r
1233 Checksum = Checksum + (*Ptr);\r
1234 HeaderLength--;\r
1235 Ptr++;\r
1236 }\r
1237\r
1238 if (Checksum != 0) {\r
1239 return EFI_NOT_FOUND;\r
1240 }\r
1241\r
1242 return EFI_SUCCESS;\r
1243}\r
1244\r
1245EFI_STATUS\r
1246EFIAPI\r
1247FvbInitialize (\r
1248 IN EFI_HANDLE ImageHandle,\r
1249 IN EFI_SYSTEM_TABLE *SystemTable\r
1250 )\r
1251/*++\r
1252\r
1253Routine Description:\r
1254 This function does common initialization for FVB services\r
1255\r
1256Arguments:\r
1257\r
1258Returns:\r
1259\r
1260--*/\r
1261{\r
1262 EFI_STATUS Status;\r
1263 EFI_FW_VOL_INSTANCE *FwhInstance;\r
1264 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
1265 EFI_DXE_SERVICES *DxeServices;\r
1266 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor;\r
1267 UINT32 BufferSize;\r
1268 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r
1269 EFI_HANDLE FwbHandle;\r
1270 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
1271 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *OldFwbInterface;\r
1272 EFI_DEVICE_PATH_PROTOCOL *TempFwbDevicePath;\r
1273 FV_DEVICE_PATH TempFvbDevicePathData;\r
1274 UINT32 MaxLbaSize;\r
1275 EFI_PHYSICAL_ADDRESS BaseAddress;\r
1276 UINT64 Length;\r
1277 UINTN NumOfBlocks;\r
1278 EFI_PEI_HOB_POINTERS FvHob;\r
1279\r
1280 //\r
1281 // Get the DXE services table\r
1282 //\r
1283 DxeServices = gDS;\r
1284\r
1285 //\r
1286 // Allocate runtime services data for global variable, which contains\r
1287 // the private data of all firmware volume block instances\r
1288 //\r
1289 mFvbModuleGlobal = AllocateRuntimePool (sizeof (ESAL_FWB_GLOBAL));\r
1290 ASSERT (mFvbModuleGlobal != NULL);\r
1291\r
1292 //\r
1293 // Calculate the total size for all firmware volume block instances\r
1294 //\r
1295 BufferSize = 0;\r
1296\r
1297 FvHob.Raw = GetHobList ();\r
1298 while ((FvHob.Raw = GetNextHob (EFI_HOB_TYPE_FV, FvHob.Raw)) != NULL) {\r
1299 BaseAddress = FvHob.FirmwareVolume->BaseAddress;\r
1300 Length = FvHob.FirmwareVolume->Length;\r
1301 //\r
1302 // Check if it is a "real" flash\r
1303 //\r
1304 Status = DxeServices->GetMemorySpaceDescriptor (\r
1305 BaseAddress,\r
1306 &Descriptor\r
1307 );\r
1308 if (EFI_ERROR (Status)) {\r
1309 break;\r
1310 }\r
1311\r
1312 if (Descriptor.GcdMemoryType != EfiGcdMemoryTypeMemoryMappedIo) {\r
1313 FvHob.Raw = GET_NEXT_HOB (FvHob);\r
1314 continue;\r
1315 }\r
1316\r
1317 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) BaseAddress;\r
1318 Status = ValidateFvHeader (FwVolHeader);\r
1319 if (EFI_ERROR (Status)) {\r
1320 //\r
1321 // Get FvbInfo\r
1322 //\r
1323 Status = GetFvbInfo (Length, &FwVolHeader);\r
1324 if (EFI_ERROR (Status)) {\r
1325 FvHob.Raw = GET_NEXT_HOB (FvHob);\r
1326 continue;\r
1327 }\r
1328 }\r
1329\r
1330 BufferSize += (sizeof (EFI_FW_VOL_INSTANCE) + FwVolHeader->HeaderLength - sizeof (EFI_FIRMWARE_VOLUME_HEADER));\r
1331 FvHob.Raw = GET_NEXT_HOB (FvHob);\r
1332 }\r
1333\r
1334 //\r
1335 // Only need to allocate once. There is only one copy of physical memory for\r
1336 // the private data of each FV instance. But in virtual mode or in physical\r
1337 // mode, the address of the the physical memory may be different.\r
1338 //\r
1339 mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] = AllocateRuntimePool (BufferSize);\r
1340 ASSERT (mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] != NULL);\r
1341\r
1342 //\r
1343 // Make a virtual copy of the FvInstance pointer.\r
1344 //\r
1345 FwhInstance = mFvbModuleGlobal->FvInstance[FVB_PHYSICAL];\r
1346 mFvbModuleGlobal->FvInstance[FVB_VIRTUAL] = FwhInstance;\r
1347\r
1348 mFvbModuleGlobal->NumFv = 0;\r
1349 MaxLbaSize = 0;\r
1350\r
1351 FvHob.Raw = GetHobList ();\r
1352 while (NULL != (FvHob.Raw = GetNextHob (EFI_HOB_TYPE_FV, FvHob.Raw))) {\r
1353 BaseAddress = FvHob.FirmwareVolume->BaseAddress;\r
1354 Length = FvHob.FirmwareVolume->Length;\r
1355 //\r
1356 // Check if it is a "real" flash\r
1357 //\r
1358 Status = DxeServices->GetMemorySpaceDescriptor (\r
1359 BaseAddress,\r
1360 &Descriptor\r
1361 );\r
1362 if (EFI_ERROR (Status)) {\r
1363 break;\r
1364 }\r
1365\r
1366 if (Descriptor.GcdMemoryType != EfiGcdMemoryTypeMemoryMappedIo) {\r
1367 FvHob.Raw = GET_NEXT_HOB (FvHob);\r
1368 continue;\r
1369 }\r
1370\r
1371 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) BaseAddress;\r
1372 Status = ValidateFvHeader (FwVolHeader);\r
1373 if (EFI_ERROR (Status)) {\r
1374 //\r
1375 // Get FvbInfo to provide in FwhInstance.\r
1376 //\r
1377 Status = GetFvbInfo (Length, &FwVolHeader);\r
1378 if (EFI_ERROR (Status)) {\r
1379 FvHob.Raw = GET_NEXT_HOB (FvHob);\r
1380 continue;\r
1381 }\r
1382 //\r
1383 // Write healthy FV header back.\r
1384 //\r
1385 CopyMem (\r
1386 (VOID *) (UINTN) BaseAddress,\r
1387 (VOID *) FwVolHeader,\r
1388 FwVolHeader->HeaderLength\r
1389 );\r
1390 }\r
1391\r
1392 FwhInstance->FvBase[FVB_PHYSICAL] = (UINTN) BaseAddress;\r
1393 FwhInstance->FvBase[FVB_VIRTUAL] = (UINTN) BaseAddress;\r
1394\r
1395 CopyMem ((UINTN *) &(FwhInstance->VolumeHeader), (UINTN *) FwVolHeader, FwVolHeader->HeaderLength);\r
1396 FwVolHeader = &(FwhInstance->VolumeHeader);\r
1397 EfiInitializeLock (&(FwhInstance->FvbDevLock), TPL_HIGH_LEVEL);\r
1398\r
1399 NumOfBlocks = 0;\r
1400\r
1401 for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r
1402 //\r
1403 // Get the maximum size of a block. The size will be used to allocate\r
1404 // buffer for Scratch space, the intermediate buffer for FVB extension\r
1405 // protocol\r
1406 //\r
1407 if (MaxLbaSize < PtrBlockMapEntry->Length) {\r
1408 MaxLbaSize = PtrBlockMapEntry->Length;\r
1409 }\r
1410\r
1411 NumOfBlocks = NumOfBlocks + PtrBlockMapEntry->NumBlocks;\r
1412 }\r
1413 //\r
1414 // The total number of blocks in the FV.\r
1415 //\r
1416 FwhInstance->NumOfBlocks = NumOfBlocks;\r
1417\r
1418 //\r
1419 // Add a FVB Protocol Instance\r
1420 //\r
1421 FvbDevice = AllocateRuntimePool (sizeof (EFI_FW_VOL_BLOCK_DEVICE));\r
1422 ASSERT (FvbDevice != NULL);\r
1423\r
1424 CopyMem (FvbDevice, &mFvbDeviceTemplate, sizeof (EFI_FW_VOL_BLOCK_DEVICE));\r
1425\r
1426 FvbDevice->Instance = mFvbModuleGlobal->NumFv;\r
1427 mFvbModuleGlobal->NumFv++;\r
1428\r
1429 //\r
1430 // Set up the devicepath\r
1431 //\r
1432 FvbDevice->DevicePath.MemMapDevPath.StartingAddress = BaseAddress;\r
1433 FvbDevice->DevicePath.MemMapDevPath.EndingAddress = BaseAddress + (FwVolHeader->FvLength - 1);\r
1434\r
1435 //\r
1436 // Find a handle with a matching device path that has supports FW Block protocol\r
1437 //\r
1438 TempFwbDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) &TempFvbDevicePathData;\r
1439 CopyMem (TempFwbDevicePath, &FvbDevice->DevicePath, sizeof (FV_DEVICE_PATH));\r
1440 Status = gBS->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid, &TempFwbDevicePath, &FwbHandle);\r
1441 if (EFI_ERROR (Status)) {\r
1442 //\r
1443 // LocateDevicePath fails so install a new interface and device path\r
1444 //\r
1445 FwbHandle = NULL;\r
1446 Status = gBS->InstallMultipleProtocolInterfaces (\r
1447 &FwbHandle,\r
1448 &gEfiFirmwareVolumeBlockProtocolGuid,\r
1449 &FvbDevice->FwVolBlockInstance,\r
1450 &gEfiDevicePathProtocolGuid,\r
1451 &FvbDevice->DevicePath,\r
1452 NULL\r
1453 );\r
1454 ASSERT_EFI_ERROR (Status);\r
1455 } else if (EfiIsDevicePathEnd (TempFwbDevicePath)) {\r
1456 //\r
1457 // Device allready exists, so reinstall the FVB protocol\r
1458 //\r
1459 Status = gBS->HandleProtocol (\r
1460 FwbHandle,\r
1461 &gEfiFirmwareVolumeBlockProtocolGuid,\r
1462 &OldFwbInterface\r
1463 );\r
1464 ASSERT_EFI_ERROR (Status);\r
1465\r
1466 Status = gBS->ReinstallProtocolInterface (\r
1467 FwbHandle,\r
1468 &gEfiFirmwareVolumeBlockProtocolGuid,\r
1469 OldFwbInterface,\r
1470 &FvbDevice->FwVolBlockInstance\r
1471 );\r
1472 ASSERT_EFI_ERROR (Status);\r
1473\r
1474 } else {\r
1475 //\r
1476 // There was a FVB protocol on an End Device Path node\r
1477 //\r
1478 ASSERT (FALSE);\r
1479 }\r
1480 //\r
1481 // Install FVB Extension Protocol on the same handle\r
1482 //\r
1483 Status = gBS->InstallMultipleProtocolInterfaces (\r
1484 &FwbHandle,\r
1485 &gEfiFvbExtensionProtocolGuid,\r
1486 &FvbDevice->FvbExtension,\r
1487 &gEfiAlternateFvBlockGuid,\r
1488 NULL,\r
1489 NULL\r
1490 );\r
1491\r
1492 ASSERT_EFI_ERROR (Status);\r
1493\r
1494 FwhInstance = (EFI_FW_VOL_INSTANCE *)\r
1495 (\r
1496 (UINTN) ((UINT8 *) FwhInstance) + FwVolHeader->HeaderLength +\r
1497 (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))\r
1498 );\r
1499\r
1500 FvHob.Raw = GET_NEXT_HOB (FvHob);\r
1501 }\r
1502\r
1503 //\r
1504 // Allocate for scratch space, an intermediate buffer for FVB extention\r
1505 //\r
1506 mFvbModuleGlobal->FvbScratchSpace[FVB_PHYSICAL] = AllocateRuntimePool (MaxLbaSize);\r
1507 ASSERT (mFvbModuleGlobal->FvbScratchSpace[FVB_PHYSICAL] != NULL);\r
1508\r
1509 mFvbModuleGlobal->FvbScratchSpace[FVB_VIRTUAL] = mFvbModuleGlobal->FvbScratchSpace[FVB_PHYSICAL];\r
1510\r
1511 return EFI_SUCCESS;\r
1512}\r