]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwLite.h
Initial import.
[mirror_edk2.git] / EdkModulePkg / Universal / FirmwareVolume / FaultTolerantWriteLite / Dxe / FtwLite.h
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12
13 Module Name:
14
15 FtwLite.h
16
17 Abstract:
18
19 This is a simple fault tolerant write driver, based on PlatformFd library.
20 And it only supports write BufferSize <= SpareAreaLength.
21
22 This boot service only protocol provides fault tolerant write capability for
23 block devices. The protocol has internal non-volatile intermediate storage
24 of the data and private information. It should be able to recover
25 automatically from a critical fault, such as power failure.
26
27 --*/
28
29 #ifndef _EFI_FAULT_TOLERANT_WRITE_LITE_H_
30 #define _EFI_FAULT_TOLERANT_WRITE_LITE_H_
31
32 #include <Common/FlashMap.h>
33 #include <Common/WorkingBlockHeader.h>
34
35 #define EFI_D_FTW_LITE EFI_D_ERROR
36 #define EFI_D_FTW_INFO EFI_D_INFO
37
38 //
39 // Flash erase polarity is 1
40 //
41 #define FTW_ERASE_POLARITY 1
42
43 #define FTW_VALID_STATE 0
44 #define FTW_INVALID_STATE 1
45
46 #define FTW_ERASED_BYTE ((UINT8) (255))
47 #define FTW_POLARITY_REVERT ((UINT8) (255))
48
49 typedef struct {
50 UINT8 WriteAllocated : 1;
51 UINT8 SpareCompleted : 1;
52 UINT8 WriteCompleted : 1;
53 UINT8 Reserved : 5;
54 #define WRITE_ALLOCATED 0x1
55 #define SPARE_COMPLETED 0x2
56 #define WRITE_COMPLETED 0x4
57
58 EFI_DEV_PATH DevPath;
59 EFI_LBA Lba;
60 UINTN Offset;
61 UINTN NumBytes;
62 //
63 // UINTN SpareAreaOffset;
64 //
65 } EFI_FTW_LITE_RECORD;
66
67 #define FTW_LITE_DEVICE_SIGNATURE EFI_SIGNATURE_32 ('F', 'T', 'W', 'L')
68
69 //
70 // MACRO for Block size.
71 // Flash Erasing will do in block granularity.
72 //
73 #ifdef FV_BLOCK_SIZE
74 #define FTW_BLOCK_SIZE FV_BLOCK_SIZE
75 #else
76 #define FV_BLOCK_SIZE 0x10000
77 #define FTW_BLOCK_SIZE FV_BLOCK_SIZE
78 #endif
79 //
80 // MACRO for FTW WORK SPACE Base & Size
81 //
82 #ifdef EFI_FTW_WORKING_OFFSET
83 #define FTW_WORK_SPACE_BASE EFI_FTW_WORKING_OFFSET
84 #else
85 #define FTW_WORK_SPACE_BASE 0x00E000
86 #endif
87
88 #ifdef EFI_FTW_WORKING_LENGTH
89 #define FTW_WORK_SPACE_SIZE EFI_FTW_WORKING_LENGTH
90 #else
91 #define FTW_WORK_SPACE_SIZE 0x002000
92 #endif
93 //
94 // MACRO for FTW header and record
95 //
96 #define FTW_WORKING_QUEUE_SIZE (FTW_WORK_SPACE_SIZE - sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER))
97 #define FTW_LITE_RECORD_SIZE (sizeof (EFI_FTW_LITE_RECORD))
98 #define WRITE_TOTAL_SIZE FTW_LITE_RECORD_SIZE
99
100 //
101 // EFI Fault tolerant protocol private data structure
102 //
103 typedef struct {
104 UINTN Signature;
105 EFI_HANDLE Handle;
106 EFI_FTW_LITE_PROTOCOL FtwLiteInstance;
107 EFI_PHYSICAL_ADDRESS WorkSpaceAddress;
108 UINTN WorkSpaceLength;
109 EFI_PHYSICAL_ADDRESS SpareAreaAddress;
110 UINTN SpareAreaLength;
111 UINTN NumberOfSpareBlock; // Number of the blocks in spare block
112 UINTN SizeOfSpareBlock; // Block size in bytes of the blocks in spare block
113 EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *FtwWorkSpaceHeader;
114 EFI_FTW_LITE_RECORD *FtwLastRecord;
115 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FtwFvBlock; // FVB of working block
116 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FtwBackupFvb; // FVB of spare block
117 EFI_LBA FtwSpareLba;
118 EFI_LBA FtwWorkBlockLba; // Start LBA of working block
119 EFI_LBA FtwWorkSpaceLba; // Start LBA of working space
120 UINTN FtwWorkSpaceBase; // Offset from LBA start addr
121 UINTN FtwWorkSpaceSize;
122 UINT8 *FtwWorkSpace;
123 //
124 // Following a buffer of FtwWorkSpace[FTW_WORK_SPACE_SIZE],
125 // Allocated with EFI_FTW_LITE_DEVICE.
126 //
127 } EFI_FTW_LITE_DEVICE;
128
129 #define FTW_LITE_CONTEXT_FROM_THIS(a) CR (a, EFI_FTW_LITE_DEVICE, FtwLiteInstance, FTW_LITE_DEVICE_SIGNATURE)
130
131 //
132 // Driver entry point
133 //
134 EFI_STATUS
135 EFIAPI
136 InitializeFtwLite (
137 IN EFI_HANDLE ImageHandle,
138 IN EFI_SYSTEM_TABLE *SystemTable
139 )
140 /*++
141
142 Routine Description:
143 This function is the entry point of the Fault Tolerant Write driver.
144
145 Arguments:
146 ImageHandle - EFI_HANDLE: A handle for the image that is initializing
147 this driver
148 SystemTable - EFI_SYSTEM_TABLE: A pointer to the EFI system table
149
150 Returns:
151 EFI_SUCCESS - FTW has finished the initialization
152 EFI_ABORTED - FTW initialization error
153
154 --*/
155 ;
156
157 //
158 // Fault Tolerant Write Protocol API
159 //
160 EFI_STATUS
161 EFIAPI
162 FtwLiteWrite (
163 IN EFI_FTW_LITE_PROTOCOL *This,
164 IN EFI_HANDLE FvbHandle,
165 IN EFI_LBA Lba,
166 IN UINTN Offset,
167 IN UINTN *NumBytes,
168 IN VOID *Buffer
169 )
170 /*++
171
172 Routine Description:
173 Starts a target block update. This function will record data about write
174 in fault tolerant storage and will complete the write in a recoverable
175 manner, ensuring at all times that either the original contents or
176 the modified contents are available.
177
178 Arguments:
179 This - Calling context
180 FvbHandle - The handle of FVB protocol that provides services for
181 reading, writing, and erasing the target block.
182 Lba - The logical block address of the target block.
183 Offset - The offset within the target block to place the data.
184 NumBytes - The number of bytes to write to the target block.
185 Buffer - The data to write.
186
187 Returns:
188 EFI_SUCCESS - The function completed successfully
189 EFI_BAD_BUFFER_SIZE - The write would span a target block, which is not
190 a valid action.
191 EFI_ACCESS_DENIED - No writes have been allocated.
192 EFI_NOT_FOUND - Cannot find FVB by handle.
193 EFI_OUT_OF_RESOURCES - Cannot allocate memory.
194 EFI_ABORTED - The function could not complete successfully.
195
196 --*/
197 ;
198
199 //
200 // Internal functions
201 //
202 EFI_STATUS
203 FtwRestart (
204 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
205 )
206 /*++
207
208 Routine Description:
209 Restarts a previously interrupted write. The caller must provide the
210 block protocol needed to complete the interrupted write.
211
212 Arguments:
213 FtwLiteDevice - The private data of FTW_LITE driver
214 FvbHandle - The handle of FVB protocol that provides services for
215 reading, writing, and erasing the target block.
216
217 Returns:
218 EFI_SUCCESS - The function completed successfully
219 EFI_ACCESS_DENIED - No pending writes exist
220 EFI_NOT_FOUND - FVB protocol not found by the handle
221 EFI_ABORTED - The function could not complete successfully
222
223 --*/
224 ;
225
226 EFI_STATUS
227 FtwAbort (
228 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
229 )
230 /*++
231
232 Routine Description:
233 Aborts all previous allocated writes.
234
235 Arguments:
236 FtwLiteDevice - The private data of FTW_LITE driver
237
238 Returns:
239 EFI_SUCCESS - The function completed successfully
240 EFI_ABORTED - The function could not complete successfully.
241 EFI_NOT_FOUND - No allocated writes exist.
242
243 --*/
244 ;
245
246
247 EFI_STATUS
248 FtwWriteRecord (
249 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
250 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb
251 )
252 /*++
253
254 Routine Description:
255 Write a record with fault tolerant mannaer.
256 Since the content has already backuped in spare block, the write is
257 guaranteed to be completed with fault tolerant manner.
258
259 Arguments:
260 FtwLiteDevice - The private data of FTW_LITE driver
261 Fvb - The FVB protocol that provides services for
262 reading, writing, and erasing the target block.
263
264 Returns:
265 EFI_SUCCESS - The function completed successfully
266 EFI_ABORTED - The function could not complete successfully
267
268 --*/
269 ;
270
271 EFI_STATUS
272 FtwEraseBlock (
273 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
274 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
275 EFI_LBA Lba
276 )
277 /*++
278
279 Routine Description:
280 To Erase one block. The size is FTW_BLOCK_SIZE
281
282 Arguments:
283 FtwLiteDevice - Calling context
284 FvBlock - FVB Protocol interface
285 Lba - Lba of the firmware block
286
287 Returns:
288 EFI_SUCCESS - Block LBA is Erased successfully
289 Others - Error occurs
290
291 --*/
292 ;
293
294 EFI_STATUS
295 FtwEraseSpareBlock (
296 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
297 )
298 /*++
299
300 Routine Description:
301
302 Erase spare block.
303
304 Arguments:
305
306 FtwLiteDevice - Calling context
307
308 Returns:
309
310 Status code
311
312 --*/
313 ;
314
315 EFI_STATUS
316 FtwGetFvbByHandle (
317 IN EFI_HANDLE FvBlockHandle,
318 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
319 )
320 /*++
321
322 Routine Description:
323 Retrive the proper FVB protocol interface by HANDLE.
324
325 Arguments:
326 FvBlockHandle - The handle of FVB protocol that provides services for
327 reading, writing, and erasing the target block.
328 FvBlock - The interface of FVB protocol
329
330 Returns:
331 EFI_SUCCESS - The function completed successfully
332 EFI_ABORTED - The function could not complete successfully
333 --*/
334 ;
335
336 EFI_STATUS
337 GetFvbByAddress (
338 IN EFI_PHYSICAL_ADDRESS Address,
339 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
340 )
341 /*++
342
343 Routine Description:
344
345 Get firmware block by address.
346
347 Arguments:
348
349 Address - Address specified the block
350 FvBlock - The block caller wanted
351
352 Returns:
353
354 Status code
355
356 EFI_NOT_FOUND - Block not found
357
358 --*/
359 ;
360
361 BOOLEAN
362 IsInWorkingBlock (
363 EFI_FTW_LITE_DEVICE *FtwLiteDevice,
364 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
365 EFI_LBA Lba
366 )
367 /*++
368
369 Routine Description:
370
371 Is it in working block?
372
373 Arguments:
374
375 FtwLiteDevice - Calling context
376 FvBlock - Fvb protocol instance
377 Lba - The block specified
378
379 Returns:
380
381 In working block or not
382
383 --*/
384 ;
385
386 BOOLEAN
387 IsBootBlock (
388 EFI_FTW_LITE_DEVICE *FtwLiteDevice,
389 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
390 EFI_LBA Lba
391 )
392 /*++
393
394 Routine Description:
395
396 Check whether the block is a boot block.
397
398 Arguments:
399
400 FtwLiteDevice - Calling context
401 FvBlock - Fvb protocol instance
402 Lba - Lba value
403
404 Returns:
405
406 Is a boot block or not
407
408 --*/
409 ;
410
411 EFI_STATUS
412 FlushSpareBlockToTargetBlock (
413 EFI_FTW_LITE_DEVICE *FtwLiteDevice,
414 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
415 EFI_LBA Lba
416 )
417 /*++
418
419 Routine Description:
420 Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.
421 Spare block is accessed by FTW backup FVB protocol interface. LBA is
422 FtwLiteDevice->FtwSpareLba.
423 Target block is accessed by FvBlock protocol interface. LBA is Lba.
424
425 Arguments:
426 FtwLiteDevice - The private data of FTW_LITE driver
427 FvBlock - FVB Protocol interface to access target block
428 Lba - Lba of the target block
429
430 Returns:
431 EFI_SUCCESS - Spare block content is copied to target block
432 EFI_INVALID_PARAMETER - Input parameter error
433 EFI_OUT_OF_RESOURCES - Allocate memory error
434 EFI_ABORTED - The function could not complete successfully
435
436 --*/
437 ;
438
439 EFI_STATUS
440 FlushSpareBlockToWorkingBlock (
441 EFI_FTW_LITE_DEVICE *FtwLiteDevice
442 )
443 /*++
444
445 Routine Description:
446 Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.
447 Spare block is accessed by FTW backup FVB protocol interface. LBA is
448 FtwLiteDevice->FtwSpareLba.
449 Working block is accessed by FTW working FVB protocol interface. LBA is
450 FtwLiteDevice->FtwWorkBlockLba.
451
452 Arguments:
453 FtwLiteDevice - The private data of FTW_LITE driver
454
455 Returns:
456 EFI_SUCCESS - Spare block content is copied to target block
457 EFI_OUT_OF_RESOURCES - Allocate memory error
458 EFI_ABORTED - The function could not complete successfully
459
460 Notes:
461 Since the working block header is important when FTW initializes, the
462 state of the operation should be handled carefully. The Crc value is
463 calculated without STATE element.
464
465 --*/
466 ;
467
468 EFI_STATUS
469 FlushSpareBlockToBootBlock (
470 EFI_FTW_LITE_DEVICE *FtwLiteDevice
471 )
472 /*++
473
474 Routine Description:
475 Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.
476 Spare block is accessed by FTW backup FVB protocol interface. LBA is
477 FtwLiteDevice->FtwSpareLba.
478 Boot block is accessed by BootFvb protocol interface. LBA is 0.
479
480 Arguments:
481 FtwLiteDevice - The private data of FTW_LITE driver
482
483 Returns:
484 EFI_SUCCESS - Spare block content is copied to boot block
485 EFI_INVALID_PARAMETER - Input parameter error
486 EFI_OUT_OF_RESOURCES - Allocate memory error
487 EFI_ABORTED - The function could not complete successfully
488
489 Notes:
490
491 --*/
492 ;
493
494 EFI_STATUS
495 FtwUpdateFvState (
496 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
497 IN EFI_LBA Lba,
498 IN UINTN Offset,
499 IN UINT8 NewBit
500 )
501 /*++
502
503 Routine Description:
504 Update a bit of state on a block device. The location of the bit is
505 calculated by the (Lba, Offset, bit). Here bit is determined by the
506 the name of a certain bit.
507
508 Arguments:
509 FvBlock - FVB Protocol interface to access SrcBlock and DestBlock
510 Lba - Lba of a block
511 Offset - Offset on the Lba
512 NewBit - New value that will override the old value if it can be change
513
514 Returns:
515 EFI_SUCCESS - A state bit has been updated successfully
516 Others - Access block device error.
517
518 Notes:
519 Assume all bits of State are inside the same BYTE.
520
521 EFI_ABORTED - Read block fail
522 --*/
523 ;
524
525 EFI_STATUS
526 FtwGetLastRecord (
527 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
528 OUT EFI_FTW_LITE_RECORD **FtwLastRecord
529 )
530 /*++
531
532 Routine Description:
533 Get the last Write record pointer.
534 The last record is the record whose 'complete' state hasn't been set.
535 After all, this header may be a EMPTY header entry for next Allocate.
536
537 Arguments:
538 FtwLiteDevice - Private data of this driver
539 FtwLastRecord - Pointer to retrieve the last write record
540
541 Returns:
542 EFI_SUCCESS - Get the last write record successfully
543 EFI_ABORTED - The FTW work space is damaged
544
545 --*/
546 ;
547
548 BOOLEAN
549 IsErasedFlashBuffer (
550 IN BOOLEAN Polarity,
551 IN UINT8 *Buffer,
552 IN UINTN BufferSize
553 )
554 /*++
555
556 Routine Description:
557
558 Check whether a flash buffer is erased.
559
560 Arguments:
561
562 Polarity - All 1 or all 0
563 Buffer - Buffer to check
564 BufferSize - Size of the buffer
565
566 Returns:
567
568 Erased or not.
569
570 --*/
571 ;
572
573 EFI_STATUS
574 InitWorkSpaceHeader (
575 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader
576 )
577 /*++
578
579 Routine Description:
580 Initialize a work space when there is no work space.
581
582 Arguments:
583 WorkingHeader - Pointer of working block header
584
585 Returns:
586 EFI_SUCCESS - The function completed successfully
587 EFI_ABORTED - The function could not complete successfully.
588
589 --*/
590 ;
591
592 EFI_STATUS
593 WorkSpaceRefresh (
594 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
595 )
596 /*++
597
598 Routine Description:
599 Read from working block to refresh the work space in memory.
600
601 Arguments:
602 FtwLiteDevice - Point to private data of FTW driver
603
604 Returns:
605 EFI_SUCCESS - The function completed successfully
606 EFI_ABORTED - The function could not complete successfully.
607
608 --*/
609 ;
610
611 BOOLEAN
612 IsValidWorkSpace (
613 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader
614 )
615 /*++
616
617 Routine Description:
618 Check to see if it is a valid work space.
619
620 Arguments:
621 WorkingHeader - Pointer of working block header
622
623 Returns:
624 EFI_SUCCESS - The function completed successfully
625 EFI_ABORTED - The function could not complete successfully.
626
627 --*/
628 ;
629
630 EFI_STATUS
631 CleanupWorkSpace (
632 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
633 IN OUT UINT8 *BlockBuffer,
634 IN UINTN BufferSize
635 )
636 /*++
637
638 Routine Description:
639 Reclaim the work space. Get rid of all the completed write records
640 and write records in the Fault Tolerant work space.
641
642 Arguments:
643 FtwLiteDevice - Point to private data of FTW driver
644 FtwSpaceBuffer - Buffer to contain the reclaimed clean data
645 BufferSize - Size of the FtwSpaceBuffer
646
647 Returns:
648 EFI_SUCCESS - The function completed successfully
649 EFI_BUFFER_TOO_SMALL - The FtwSpaceBuffer is too small
650 EFI_ABORTED - The function could not complete successfully.
651
652 --*/
653 ;
654
655 EFI_STATUS
656 FtwReclaimWorkSpace (
657 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
658 )
659 /*++
660
661 Routine Description:
662 Reclaim the work space on the working block.
663
664 Arguments:
665 FtwLiteDevice - Point to private data of FTW driver
666
667 Returns:
668 EFI_SUCCESS - The function completed successfully
669 EFI_OUT_OF_RESOURCES - Allocate memory error
670 EFI_ABORTED - The function could not complete successfully
671
672 --*/
673 ;
674
675 #endif