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