]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwLite.c
modify coding style to pass ecc tool
[mirror_edk2.git] / MdeModulePkg / Universal / FirmwareVolume / FaultTolerantWriteDxe / FtwLite.c
1 /** @file
2
3 This is a simple fault tolerant write driver.
4 And it only supports write BufferSize <= SpareAreaLength.
5
6 This boot service only protocol provides fault tolerant write capability for
7 block devices. The protocol has internal non-volatile intermediate storage
8 of the data and private information. It should be able to recover
9 automatically from a critical fault, such as power failure.
10
11 The implementation uses an FTW Lite (Fault Tolerant Write) Work Space.
12 This work space is a memory copy of the work space on the Woring Block,
13 the size of the work space is the FTW_WORK_SPACE_SIZE bytes.
14
15 Copyright (c) 2006 - 2008, Intel Corporation
16 All rights reserved. This program and the accompanying materials
17 are licensed and made available under the terms and conditions of the BSD License
18 which accompanies this distribution. The full text of the license may be found at
19 http://opensource.org/licenses/bsd-license.php
20
21 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
22 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23
24 **/
25
26 #include <FtwLite.h>
27
28 //
29 // In write function, we should check the target range to prevent the user
30 // from writing Spare block and Working space directly.
31 //
32 //
33 // Fault Tolerant Write Protocol API
34 //
35 /**
36 Starts a target block update. This function will record data about write
37 in fault tolerant storage and will complete the write in a recoverable
38 manner, ensuring at all times that either the original contents or
39 the modified contents are available.
40
41
42 @param This Calling context
43 @param FvbHandle The handle of FVB protocol that provides services for
44 reading, writing, and erasing the target block.
45 @param Lba The logical block address of the target block.
46 @param Offset The offset within the target block to place the data.
47 @param NumBytes The number of bytes to write to the target block.
48 @param Buffer The data to write.
49
50 @retval EFI_SUCCESS The function completed successfully
51 @retval EFI_BAD_BUFFER_SIZE The write would span a target block, which is not
52 a valid action.
53 @retval EFI_ACCESS_DENIED No writes have been allocated.
54 @retval EFI_NOT_FOUND Cannot find FVB by handle.
55 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
56 @retval EFI_ABORTED The function could not complete successfully.
57
58 **/
59 EFI_STATUS
60 EFIAPI
61 FtwLiteWrite (
62 IN EFI_FTW_LITE_PROTOCOL *This,
63 IN EFI_HANDLE FvbHandle,
64 IN EFI_LBA Lba,
65 IN UINTN Offset,
66 IN OUT UINTN *NumBytes,
67 IN VOID *Buffer
68 )
69 {
70 EFI_STATUS Status;
71 EFI_FTW_LITE_DEVICE *FtwLiteDevice;
72 EFI_FTW_LITE_RECORD *Record;
73 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
74 EFI_PHYSICAL_ADDRESS FvbPhysicalAddress;
75 UINTN MyLength;
76 UINTN MyOffset;
77 UINTN MyBufferSize;
78 UINT8 *MyBuffer;
79 UINTN SpareBufferSize;
80 UINT8 *SpareBuffer;
81 UINTN Index;
82 UINT8 *Ptr;
83 EFI_DEV_PATH_PTR DevPtr;
84
85 //
86 // Refresh work space and get last record
87 //
88 FtwLiteDevice = FTW_LITE_CONTEXT_FROM_THIS (This);
89 Status = WorkSpaceRefresh (FtwLiteDevice);
90 if (EFI_ERROR (Status)) {
91 return EFI_ABORTED;
92 }
93
94 Record = FtwLiteDevice->FtwLastRecord;
95
96 //
97 // Check the flags of last write record
98 //
99 if ((Record->WriteAllocated == FTW_VALID_STATE) || (Record->SpareCompleted == FTW_VALID_STATE)) {
100 return EFI_ACCESS_DENIED;
101 }
102 //
103 // IF former record has completed, THEN use next record
104 //
105 if (Record->WriteCompleted == FTW_VALID_STATE) {
106 Record++;
107 FtwLiteDevice->FtwLastRecord = Record;
108 }
109
110 MyOffset = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;
111
112 //
113 // Check if the input data can fit within the target block
114 //
115 if ((Offset +*NumBytes) > FtwLiteDevice->SpareAreaLength) {
116 return EFI_BAD_BUFFER_SIZE;
117 }
118 //
119 // Check if there is enough free space for allocate a record
120 //
121 if ((MyOffset + WRITE_TOTAL_SIZE) > FtwLiteDevice->FtwWorkSpaceSize) {
122 Status = FtwReclaimWorkSpace (FtwLiteDevice);
123 if (EFI_ERROR (Status)) {
124 DEBUG ((EFI_D_ERROR, "FtwLite: Reclaim work space - %r", Status));
125 return EFI_ABORTED;
126 }
127 }
128 //
129 // Get the FVB protocol by handle
130 //
131 Status = FtwGetFvbByHandle (FvbHandle, &Fvb);
132 if (EFI_ERROR (Status)) {
133 return EFI_NOT_FOUND;
134 }
135 //
136 // Allocate a write record in workspace.
137 // Update Header->WriteAllocated as VALID
138 //
139 Status = FtwUpdateFvState (
140 FtwLiteDevice->FtwFvBlock,
141 FtwLiteDevice->FtwWorkSpaceLba,
142 FtwLiteDevice->FtwWorkSpaceBase + MyOffset,
143 WRITE_ALLOCATED
144 );
145
146 if (EFI_ERROR (Status)) {
147 DEBUG ((EFI_D_FTW_LITE, "FtwLite: Allocate record - %r\n", Status));
148 return EFI_ABORTED;
149 }
150
151 Record->WriteAllocated = FTW_VALID_STATE;
152
153 //
154 // Prepare data of write record, filling DevPath with memory mapped address.
155 //
156 DevPtr.MemMap = (MEMMAP_DEVICE_PATH *) &Record->DevPath;
157 DevPtr.MemMap->Header.Type = HARDWARE_DEVICE_PATH;
158 DevPtr.MemMap->Header.SubType = HW_MEMMAP_DP;
159 SetDevicePathNodeLength (&DevPtr.MemMap->Header, sizeof (MEMMAP_DEVICE_PATH));
160
161 Status = Fvb->GetPhysicalAddress (Fvb, &FvbPhysicalAddress);
162 if (EFI_ERROR (Status)) {
163 DEBUG ((EFI_D_FTW_LITE, "FtwLite: Get FVB physical address - %r\n", Status));
164 return EFI_ABORTED;
165 }
166
167 DevPtr.MemMap->MemoryType = EfiMemoryMappedIO;
168 DevPtr.MemMap->StartingAddress = FvbPhysicalAddress;
169 DevPtr.MemMap->EndingAddress = FvbPhysicalAddress +*NumBytes;
170 //
171 // ignored!
172 //
173 Record->Lba = Lba;
174 Record->Offset = Offset;
175 Record->NumBytes = *NumBytes;
176
177 //
178 // Write the record to the work space.
179 //
180 MyOffset = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;
181 MyLength = FTW_LITE_RECORD_SIZE;
182
183 Status = FtwLiteDevice->FtwFvBlock->Write (
184 FtwLiteDevice->FtwFvBlock,
185 FtwLiteDevice->FtwWorkSpaceLba,
186 FtwLiteDevice->FtwWorkSpaceBase + MyOffset,
187 &MyLength,
188 (UINT8 *) Record
189 );
190 if (EFI_ERROR (Status)) {
191 return EFI_ABORTED;
192 }
193 //
194 // Record has been written to working block, then write data.
195 //
196 //
197 // Allocate a memory buffer
198 //
199 MyBufferSize = FtwLiteDevice->SpareAreaLength;
200 MyBuffer = AllocatePool (MyBufferSize);
201 if (MyBuffer == NULL) {
202 return EFI_OUT_OF_RESOURCES;
203 }
204 //
205 // Starting at Lba, if the number of the rest blocks on Fvb is less
206 // than NumberOfSpareBlock.
207 //
208 //
209 // Read all original data from target block to memory buffer
210 //
211 if (IsInWorkingBlock (FtwLiteDevice, Fvb, Lba)) {
212 //
213 // If target block falls into working block, we must follow the process of
214 // updating working block.
215 //
216 Ptr = MyBuffer;
217 for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {
218 MyLength = FtwLiteDevice->SizeOfSpareBlock;
219 Status = FtwLiteDevice->FtwFvBlock->Read (
220 FtwLiteDevice->FtwFvBlock,
221 FtwLiteDevice->FtwWorkBlockLba + Index,
222 0,
223 &MyLength,
224 Ptr
225 );
226 if (EFI_ERROR (Status)) {
227 FreePool (MyBuffer);
228 return EFI_ABORTED;
229 }
230
231 Ptr += MyLength;
232 }
233 //
234 // Update Offset by adding the offset from the start LBA of working block to
235 // the target LBA. The target block can not span working block!
236 //
237 Offset = (((UINTN) (Lba - FtwLiteDevice->FtwWorkBlockLba)) * FtwLiteDevice->SizeOfSpareBlock + Offset);
238 ASSERT ((Offset +*NumBytes) <= FtwLiteDevice->SpareAreaLength);
239
240 } else {
241
242 Ptr = MyBuffer;
243 for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {
244 MyLength = FtwLiteDevice->SizeOfSpareBlock;
245 Status = Fvb->Read (Fvb, Lba + Index, 0, &MyLength, Ptr);
246 if (EFI_ERROR (Status)) {
247 FreePool (MyBuffer);
248 return EFI_ABORTED;
249 }
250
251 Ptr += MyLength;
252 }
253 }
254 //
255 // Overwrite the updating range data with
256 // the input buffer content
257 //
258 CopyMem (MyBuffer + Offset, Buffer, *NumBytes);
259
260 //
261 // Try to keep the content of spare block
262 // Save spare block into a spare backup memory buffer (Sparebuffer)
263 //
264 SpareBufferSize = FtwLiteDevice->SpareAreaLength;
265 SpareBuffer = AllocatePool (SpareBufferSize);
266 if (SpareBuffer == NULL) {
267 FreePool (MyBuffer);
268 return EFI_OUT_OF_RESOURCES;
269 }
270
271 Ptr = SpareBuffer;
272 for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {
273 MyLength = FtwLiteDevice->SizeOfSpareBlock;
274 Status = FtwLiteDevice->FtwBackupFvb->Read (
275 FtwLiteDevice->FtwBackupFvb,
276 FtwLiteDevice->FtwSpareLba + Index,
277 0,
278 &MyLength,
279 Ptr
280 );
281 if (EFI_ERROR (Status)) {
282 FreePool (MyBuffer);
283 FreePool (SpareBuffer);
284 return EFI_ABORTED;
285 }
286
287 Ptr += MyLength;
288 }
289 //
290 // Write the memory buffer to spare block
291 // Don't forget to erase Flash first.
292 //
293 Status = FtwEraseSpareBlock (FtwLiteDevice);
294 Ptr = MyBuffer;
295 for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {
296 MyLength = FtwLiteDevice->SizeOfSpareBlock;
297 Status = FtwLiteDevice->FtwBackupFvb->Write (
298 FtwLiteDevice->FtwBackupFvb,
299 FtwLiteDevice->FtwSpareLba + Index,
300 0,
301 &MyLength,
302 Ptr
303 );
304 if (EFI_ERROR (Status)) {
305 FreePool (MyBuffer);
306 FreePool (SpareBuffer);
307 return EFI_ABORTED;
308 }
309
310 Ptr += MyLength;
311 }
312 //
313 // Free MyBuffer
314 //
315 FreePool (MyBuffer);
316
317 //
318 // Set the SpareCompleteD in the FTW record,
319 //
320 MyOffset = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;
321 Status = FtwUpdateFvState (
322 FtwLiteDevice->FtwFvBlock,
323 FtwLiteDevice->FtwWorkSpaceLba,
324 FtwLiteDevice->FtwWorkSpaceBase + MyOffset,
325 SPARE_COMPLETED
326 );
327 if (EFI_ERROR (Status)) {
328 FreePool (SpareBuffer);
329 return EFI_ABORTED;
330 }
331
332 Record->SpareCompleted = FTW_VALID_STATE;
333
334 //
335 // Since the content has already backuped in spare block, the write is
336 // guaranteed to be completed with fault tolerant manner.
337 //
338 Status = FtwWriteRecord (FtwLiteDevice, Fvb);
339 if (EFI_ERROR (Status)) {
340 FreePool (SpareBuffer);
341 return EFI_ABORTED;
342 }
343
344 Record++;
345 FtwLiteDevice->FtwLastRecord = Record;
346
347 //
348 // Restore spare backup buffer into spare block , if no failure happened during FtwWrite.
349 //
350 Status = FtwEraseSpareBlock (FtwLiteDevice);
351 Ptr = SpareBuffer;
352 for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {
353 MyLength = FtwLiteDevice->SizeOfSpareBlock;
354 Status = FtwLiteDevice->FtwBackupFvb->Write (
355 FtwLiteDevice->FtwBackupFvb,
356 FtwLiteDevice->FtwSpareLba + Index,
357 0,
358 &MyLength,
359 Ptr
360 );
361 if (EFI_ERROR (Status)) {
362 FreePool (SpareBuffer);
363 return EFI_ABORTED;
364 }
365
366 Ptr += MyLength;
367 }
368 //
369 // All success.
370 //
371 FreePool (SpareBuffer);
372
373 DEBUG (
374 (EFI_D_FTW_LITE,
375 "FtwLite: Write() success, (Lba:Offset)=(%lx:0x%x), NumBytes: 0x%x\n",
376 Lba,
377 Offset,
378 *NumBytes)
379 );
380
381 return EFI_SUCCESS;
382 }
383
384
385 /**
386 Write a record with fault tolerant mannaer.
387 Since the content has already backuped in spare block, the write is
388 guaranteed to be completed with fault tolerant manner.
389
390
391 @param FtwLiteDevice The private data of FTW_LITE driver
392 @param Fvb The FVB protocol that provides services for
393 reading, writing, and erasing the target block.
394
395 @retval EFI_SUCCESS The function completed successfully
396 @retval EFI_ABORTED The function could not complete successfully
397
398 **/
399 EFI_STATUS
400 FtwWriteRecord (
401 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
402 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb
403 )
404 {
405 EFI_STATUS Status;
406 EFI_FTW_LITE_RECORD *Record;
407 EFI_LBA WorkSpaceLbaOffset;
408 UINTN Offset;
409
410 //
411 // Spare Complete but Destination not complete,
412 // Recover the targt block with the spare block.
413 //
414 Record = FtwLiteDevice->FtwLastRecord;
415
416 //
417 // IF target block is working block, THEN Flush Spare Block To Working Block;
418 // ELSE IF target block is boot block, THEN Flush Spare Block To boot Block;
419 // ELSE flush spare block to normal target block.ENDIF
420 //
421 if (IsInWorkingBlock (FtwLiteDevice, Fvb, Record->Lba)) {
422 //
423 // If target block is working block, Attention:
424 // it's required to set SPARE_COMPLETED to spare block.
425 //
426 WorkSpaceLbaOffset = FtwLiteDevice->FtwWorkSpaceLba - FtwLiteDevice->FtwWorkBlockLba;
427 Offset = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;
428 Status = FtwUpdateFvState (
429 FtwLiteDevice->FtwBackupFvb,
430 FtwLiteDevice->FtwSpareLba + WorkSpaceLbaOffset,
431 FtwLiteDevice->FtwWorkSpaceBase + Offset,
432 SPARE_COMPLETED
433 );
434 ASSERT_EFI_ERROR (Status);
435
436 Status = FlushSpareBlockToWorkingBlock (FtwLiteDevice);
437 } else if (IsBootBlock (FtwLiteDevice, Fvb, Record->Lba)) {
438 //
439 // Update boot block
440 //
441 Status = FlushSpareBlockToBootBlock (FtwLiteDevice);
442 } else {
443 //
444 // Update blocks other than working block or boot block
445 //
446 Status = FlushSpareBlockToTargetBlock (FtwLiteDevice, Fvb, Record->Lba);
447 }
448
449 ASSERT_EFI_ERROR (Status);
450
451 //
452 // Set WriteCompleted flag in record
453 //
454 Offset = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;
455 Status = FtwUpdateFvState (
456 FtwLiteDevice->FtwFvBlock,
457 FtwLiteDevice->FtwWorkSpaceLba,
458 FtwLiteDevice->FtwWorkSpaceBase + Offset,
459 WRITE_COMPLETED
460 );
461 ASSERT_EFI_ERROR (Status);
462
463 Record->WriteCompleted = FTW_VALID_STATE;
464 return EFI_SUCCESS;
465 }
466
467
468 /**
469 Restarts a previously interrupted write. The caller must provide the
470 block protocol needed to complete the interrupted write.
471
472
473 @param FtwLiteDevice The private data of FTW_LITE driver
474 FvbHandle - The handle of FVB protocol that provides services for
475 reading, writing, and erasing the target block.
476
477 @retval EFI_SUCCESS The function completed successfully
478 @retval EFI_ACCESS_DENIED No pending writes exist
479 @retval EFI_NOT_FOUND FVB protocol not found by the handle
480 @retval EFI_ABORTED The function could not complete successfully
481
482 **/
483 EFI_STATUS
484 FtwRestart (
485 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
486 )
487 {
488 EFI_STATUS Status;
489 EFI_FTW_LITE_RECORD *Record;
490 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
491 EFI_DEV_PATH_PTR DevPathPtr;
492
493 //
494 // Spare Completed but Destination not complete,
495 // Recover the targt block with the spare block.
496 //
497 Record = FtwLiteDevice->FtwLastRecord;
498
499 //
500 // Only support memory mapped FVB device path by now.
501 //
502 DevPathPtr.MemMap = (MEMMAP_DEVICE_PATH *) &Record->DevPath;
503 if (!((DevPathPtr.MemMap->Header.Type == HARDWARE_DEVICE_PATH) && (DevPathPtr.MemMap->Header.SubType == HW_MEMMAP_DP))
504 ) {
505 DEBUG ((EFI_D_FTW_LITE, "FtwLite: FVB Device Path is not memory mapped\n"));
506 return EFI_ABORTED;
507 }
508
509 Status = GetFvbByAddress (DevPathPtr.MemMap->StartingAddress, &Fvb);
510 if (EFI_ERROR (Status)) {
511 return EFI_NOT_FOUND;
512 }
513 //
514 // Since the content has already backuped in spare block, the write is
515 // guaranteed to be completed with fault tolerant manner.
516 //
517 Status = FtwWriteRecord (FtwLiteDevice, Fvb);
518 DEBUG ((EFI_D_FTW_INFO, "FtwLite: Restart() - %r\n", Status));
519
520 Record++;
521 FtwLiteDevice->FtwLastRecord = Record;
522
523 //
524 // Erase Spare block
525 // This is restart, no need to keep spareblock content.
526 //
527 FtwEraseSpareBlock (FtwLiteDevice);
528
529 return Status;
530 }
531
532
533 /**
534 Aborts all previous allocated writes.
535
536
537 @param FtwLiteDevice The private data of FTW_LITE driver
538
539 @retval EFI_SUCCESS The function completed successfully
540 @retval EFI_ABORTED The function could not complete successfully.
541 @retval EFI_NOT_FOUND No allocated writes exist.
542
543 **/
544 EFI_STATUS
545 FtwAbort (
546 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
547 )
548 {
549 EFI_STATUS Status;
550 UINTN Offset;
551
552 if (FtwLiteDevice->FtwLastRecord->WriteCompleted == FTW_VALID_STATE) {
553 return EFI_NOT_FOUND;
554 }
555 //
556 // Update the complete state of the header as VALID and abort.
557 //
558 Offset = (UINT8 *) FtwLiteDevice->FtwLastRecord - FtwLiteDevice->FtwWorkSpace;
559 Status = FtwUpdateFvState (
560 FtwLiteDevice->FtwFvBlock,
561 FtwLiteDevice->FtwWorkSpaceLba,
562 FtwLiteDevice->FtwWorkSpaceBase + Offset,
563 WRITE_COMPLETED
564 );
565 if (EFI_ERROR (Status)) {
566 return EFI_ABORTED;
567 }
568
569 FtwLiteDevice->FtwLastRecord->WriteCompleted = FTW_VALID_STATE;
570
571 Status = FtwGetLastRecord (FtwLiteDevice, &FtwLiteDevice->FtwLastRecord);
572
573 //
574 // Erase the spare block
575 //
576 Status = FtwEraseSpareBlock (FtwLiteDevice);
577
578 DEBUG ((EFI_D_FTW_INFO, "FtwLite: Abort() success \n"));
579 return EFI_SUCCESS;
580 }
581
582 /**
583 This function is the entry point of the Fault Tolerant Write driver.
584
585
586 @param ImageHandle EFI_HANDLE: A handle for the image that is initializing
587 this driver
588 @param SystemTable EFI_SYSTEM_TABLE: A pointer to the EFI system table
589
590 @retval EFI_SUCCESS FTW has finished the initialization
591 @retval EFI_ABORTED FTW initialization error
592
593 **/
594 EFI_STATUS
595 EFIAPI
596 InitializeFtwLite (
597 IN EFI_HANDLE ImageHandle,
598 IN EFI_SYSTEM_TABLE *SystemTable
599 )
600 {
601 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
602 UINTN Index;
603 EFI_HANDLE *HandleBuffer;
604 UINTN HandleCount;
605 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
606 EFI_PHYSICAL_ADDRESS BaseAddress;
607 EFI_FTW_LITE_DEVICE *FtwLiteDevice;
608 EFI_FTW_LITE_RECORD *Record;
609 UINTN Length;
610 EFI_STATUS Status;
611 UINTN Offset;
612 EFI_FV_BLOCK_MAP_ENTRY *FvbMapEntry;
613 UINT32 LbaIndex;
614
615 //
616 // Allocate Private data of this driver,
617 // INCLUDING THE FtwWorkSpace[FTW_WORK_SPACE_SIZE].
618 //
619 FtwLiteDevice = NULL;
620 FtwLiteDevice = AllocatePool (sizeof (EFI_FTW_LITE_DEVICE) + FTW_WORK_SPACE_SIZE);
621 if (FtwLiteDevice != NULL) {
622 Status = EFI_SUCCESS;
623 } else {
624 Status = EFI_OUT_OF_RESOURCES;
625 }
626
627 ASSERT_EFI_ERROR (Status);
628
629 ZeroMem (FtwLiteDevice, sizeof (EFI_FTW_LITE_DEVICE));
630 FtwLiteDevice->Signature = FTW_LITE_DEVICE_SIGNATURE;
631
632 //
633 // Initialize other parameters, and set WorkSpace as FTW_ERASED_BYTE.
634 //
635 FtwLiteDevice->FtwWorkSpace = (UINT8 *) (FtwLiteDevice + 1);
636 FtwLiteDevice->FtwWorkSpaceSize = FTW_WORK_SPACE_SIZE;
637 SetMem (
638 FtwLiteDevice->FtwWorkSpace,
639 FtwLiteDevice->FtwWorkSpaceSize,
640 FTW_ERASED_BYTE
641 );
642 FtwLiteDevice->FtwWorkSpaceHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) FtwLiteDevice->FtwWorkSpace;
643
644 FtwLiteDevice->FtwLastRecord = NULL;
645
646 FtwLiteDevice->WorkSpaceAddress = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageFtwWorkingBase);
647 FtwLiteDevice->WorkSpaceLength = (UINTN) PcdGet32 (PcdFlashNvStorageFtwWorkingSize);
648
649 FtwLiteDevice->SpareAreaAddress = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageFtwSpareBase);
650 FtwLiteDevice->SpareAreaLength = (UINTN) PcdGet32 (PcdFlashNvStorageFtwSpareSize);
651
652 ASSERT ((FtwLiteDevice->WorkSpaceLength != 0) && (FtwLiteDevice->SpareAreaLength != 0));
653
654 //
655 // Locate FVB protocol
656 //
657 Status = gBS->LocateHandleBuffer (
658 ByProtocol,
659 &gEfiFirmwareVolumeBlockProtocolGuid,
660 NULL,
661 &HandleCount,
662 &HandleBuffer
663 );
664 ASSERT_EFI_ERROR (Status);
665
666 ASSERT (HandleCount > 0);
667
668 FtwLiteDevice->FtwFvBlock = NULL;
669 FtwLiteDevice->FtwBackupFvb = NULL;
670 FtwLiteDevice->FtwWorkSpaceLba = (EFI_LBA) (-1);
671 FtwLiteDevice->FtwSpareLba = (EFI_LBA) (-1);
672 for (Index = 0; Index < HandleCount; Index += 1) {
673 Status = gBS->HandleProtocol (
674 HandleBuffer[Index],
675 &gEfiFirmwareVolumeBlockProtocolGuid,
676 (VOID **) &Fvb
677 );
678 ASSERT_EFI_ERROR (Status);
679
680 Status = Fvb->GetPhysicalAddress (Fvb, &BaseAddress);
681 if (EFI_ERROR (Status)) {
682 continue;
683 }
684
685 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) BaseAddress);
686
687 if ((FtwLiteDevice->WorkSpaceAddress >= BaseAddress) &&
688 (FtwLiteDevice->WorkSpaceAddress <= (BaseAddress + FwVolHeader->FvLength))
689 ) {
690 FtwLiteDevice->FtwFvBlock = Fvb;
691 //
692 // To get the LBA of work space
693 //
694 if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {
695 //
696 // FV may have multiple types of BlockLength
697 //
698 FvbMapEntry = &FwVolHeader->BlockMap[0];
699 while (!((FvbMapEntry->NumBlocks == 0) && (FvbMapEntry->Length == 0))) {
700 for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {
701 if (FtwLiteDevice->WorkSpaceAddress < (BaseAddress + FvbMapEntry->Length * LbaIndex)) {
702 FtwLiteDevice->FtwWorkSpaceLba = LbaIndex - 1;
703 //
704 // Get the Work space size and Base(Offset)
705 //
706 FtwLiteDevice->FtwWorkSpaceSize = FtwLiteDevice->WorkSpaceLength;
707 FtwLiteDevice->FtwWorkSpaceBase = (UINTN) (FtwLiteDevice->WorkSpaceAddress - (BaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));
708 break;
709 }
710 }
711 //
712 // end for
713 //
714 FvbMapEntry++;
715 }
716 //
717 // end while
718 //
719 }
720 }
721
722 if ((FtwLiteDevice->SpareAreaAddress >= BaseAddress) &&
723 (FtwLiteDevice->SpareAreaAddress < (BaseAddress + FwVolHeader->FvLength))
724 ) {
725 FtwLiteDevice->FtwBackupFvb = Fvb;
726 //
727 // To get the LBA of spare
728 //
729 if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {
730 //
731 // FV may have multiple types of BlockLength
732 //
733 FvbMapEntry = &FwVolHeader->BlockMap[0];
734 while (!((FvbMapEntry->NumBlocks == 0) && (FvbMapEntry->Length == 0))) {
735 for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {
736 if (FtwLiteDevice->SpareAreaAddress < (BaseAddress + FvbMapEntry->Length * LbaIndex)) {
737 //
738 // Get the NumberOfSpareBlock and SizeOfSpareBlock
739 //
740 FtwLiteDevice->FtwSpareLba = LbaIndex - 1;
741 FtwLiteDevice->SizeOfSpareBlock = FvbMapEntry->Length;
742 FtwLiteDevice->NumberOfSpareBlock = FtwLiteDevice->SpareAreaLength / FtwLiteDevice->SizeOfSpareBlock;
743 //
744 // Check the range of spare area to make sure that it's in FV range
745 //
746 ASSERT ((FtwLiteDevice->FtwSpareLba + FtwLiteDevice->NumberOfSpareBlock) <= FvbMapEntry->NumBlocks);
747 break;
748 }
749 }
750
751 FvbMapEntry++;
752 }
753 //
754 // end while
755 //
756 }
757 }
758 }
759 //
760 // Calculate the start LBA of working block. Working block is an area which
761 // contains working space in its last block and has the same size as spare
762 // block, unless there are not enough blocks before the block that contains
763 // working space.
764 //
765 FtwLiteDevice->FtwWorkBlockLba = FtwLiteDevice->FtwWorkSpaceLba - FtwLiteDevice->NumberOfSpareBlock + 1;
766 if ((INT64) (FtwLiteDevice->FtwWorkBlockLba) < 0) {
767 FtwLiteDevice->FtwWorkBlockLba = 0;
768 }
769
770 if ((FtwLiteDevice->FtwFvBlock == NULL) ||
771 (FtwLiteDevice->FtwBackupFvb == NULL) ||
772 (FtwLiteDevice->FtwWorkSpaceLba == (EFI_LBA) (-1)) ||
773 (FtwLiteDevice->FtwSpareLba == (EFI_LBA) (-1))
774 ) {
775 DEBUG ((EFI_D_ERROR, "FtwLite: Working or spare FVB not ready\n"));
776 ASSERT_EFI_ERROR (Status);
777 }
778 //
779 // Refresh workspace data from working block
780 //
781 Status = WorkSpaceRefresh (FtwLiteDevice);
782 ASSERT_EFI_ERROR (Status);
783
784 //
785 // If the working block workspace is not valid, try the spare block
786 //
787 if (!IsValidWorkSpace (FtwLiteDevice->FtwWorkSpaceHeader)) {
788 DEBUG ((EFI_D_FTW_LITE, "FtwLite: Workspace invalid, read from backup\n"));
789 //
790 // Read from spare block
791 //
792 Length = FtwLiteDevice->FtwWorkSpaceSize;
793 Status = FtwLiteDevice->FtwBackupFvb->Read (
794 FtwLiteDevice->FtwBackupFvb,
795 FtwLiteDevice->FtwSpareLba,
796 FtwLiteDevice->FtwWorkSpaceBase,
797 &Length,
798 FtwLiteDevice->FtwWorkSpace
799 );
800 ASSERT_EFI_ERROR (Status);
801
802 //
803 // If spare block is valid, then replace working block content.
804 //
805 if (IsValidWorkSpace (FtwLiteDevice->FtwWorkSpaceHeader)) {
806 Status = FlushSpareBlockToWorkingBlock (FtwLiteDevice);
807 DEBUG ((EFI_D_FTW_LITE, "FtwLite: Restart working block in Init() - %r\n", Status));
808 ASSERT_EFI_ERROR (Status);
809
810 FtwAbort (FtwLiteDevice);
811 //
812 // Refresh work space.
813 //
814 Status = WorkSpaceRefresh (FtwLiteDevice);
815 if (EFI_ERROR (Status)) {
816 return EFI_ABORTED;
817 }
818 } else {
819 DEBUG ((EFI_D_FTW_LITE, "FtwLite: Both are invalid, init workspace\n"));
820 //
821 // If both are invalid, then initialize work space.
822 //
823 SetMem (
824 FtwLiteDevice->FtwWorkSpace,
825 FtwLiteDevice->FtwWorkSpaceSize,
826 FTW_ERASED_BYTE
827 );
828 InitWorkSpaceHeader (FtwLiteDevice->FtwWorkSpaceHeader);
829 //
830 // Write to work space on the working block
831 //
832 Length = FtwLiteDevice->FtwWorkSpaceSize;
833 Status = FtwLiteDevice->FtwFvBlock->Write (
834 FtwLiteDevice->FtwFvBlock,
835 FtwLiteDevice->FtwWorkSpaceLba,
836 FtwLiteDevice->FtwWorkSpaceBase,
837 &Length,
838 FtwLiteDevice->FtwWorkSpace
839 );
840 if (EFI_ERROR (Status)) {
841 return EFI_ABORTED;
842 }
843 }
844 }
845 //
846 // Hook the protocol API
847 //
848 FtwLiteDevice->FtwLiteInstance.Write = FtwLiteWrite;
849
850 //
851 // Install protocol interface
852 //
853 Status = gBS->InstallProtocolInterface (
854 &FtwLiteDevice->Handle,
855 &gEfiFaultTolerantWriteLiteProtocolGuid,
856 EFI_NATIVE_INTERFACE,
857 &FtwLiteDevice->FtwLiteInstance
858 );
859 if (EFI_ERROR (Status)) {
860 return EFI_ABORTED;
861 }
862 //
863 // If (!SpareCompleted) THEN Abort to rollback.
864 //
865 if ((FtwLiteDevice->FtwLastRecord->WriteAllocated == FTW_VALID_STATE) &&
866 (FtwLiteDevice->FtwLastRecord->SpareCompleted != FTW_VALID_STATE)
867 ) {
868 DEBUG ((EFI_D_FTW_LITE, "FtwLite: Init.. record not SpareCompleted, abort()\n"));
869 FtwAbort (FtwLiteDevice);
870 }
871 //
872 // if (SpareCompleted) THEN Restart to fault tolerant write.
873 //
874 if ((FtwLiteDevice->FtwLastRecord->SpareCompleted == FTW_VALID_STATE) &&
875 (FtwLiteDevice->FtwLastRecord->WriteCompleted != FTW_VALID_STATE)
876 ) {
877
878 Status = FtwRestart (FtwLiteDevice);
879 DEBUG ((EFI_D_FTW_LITE, "FtwLite: Restart last write - %r\n", Status));
880 if (EFI_ERROR (Status)) {
881 return Status;
882 }
883 }
884 //
885 // To check the workspace buffer behind last records is EMPTY or not.
886 // If it's not EMPTY, FTW_LITE also need to call reclaim().
887 //
888 Record = FtwLiteDevice->FtwLastRecord;
889 Offset = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;
890 if (FtwLiteDevice->FtwWorkSpace[Offset] != FTW_ERASED_BYTE) {
891 Offset += WRITE_TOTAL_SIZE;
892 }
893
894 if (!IsErasedFlashBuffer (
895 FTW_ERASE_POLARITY,
896 FtwLiteDevice->FtwWorkSpace + Offset,
897 FtwLiteDevice->FtwWorkSpaceSize - Offset
898 )) {
899 DEBUG ((EFI_D_FTW_LITE, "FtwLite: Workspace is dirty, call reclaim...\n"));
900 Status = FtwReclaimWorkSpace (FtwLiteDevice);
901 if (EFI_ERROR (Status)) {
902 DEBUG ((EFI_D_FTW_LITE, "FtwLite: Workspace reclaim - %r\n", Status));
903 return EFI_ABORTED;
904 }
905 }
906
907 return EFI_SUCCESS;
908 }