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