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