]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwLite.h
Code clean for Ftw driver, remove the obsolete logic for boot block. Correct some...
[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 <Guid/SystemNvDataGuid.h>
24 #include <Protocol/FaultTolerantWriteLite.h>
25 #include <Protocol/FirmwareVolumeBlock.h>
26
27 #include <Library/PcdLib.h>
28 #include <Library/DebugLib.h>
29 #include <Library/UefiDriverEntryPoint.h>
30 #include <Library/BaseMemoryLib.h>
31 #include <Library/MemoryAllocationLib.h>
32 #include <Library/UefiBootServicesTableLib.h>
33 #include <Library/DevicePathLib.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 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_LITE_RECORD_SIZE (sizeof (EFI_FTW_LITE_RECORD))
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 /**
135 This function is the entry point of the Fault Tolerant Write driver.
136
137
138 @param ImageHandle A handle for the image that is initializing
139 this driver
140 @param SystemTable A pointer to the EFI system table
141
142 @retval EFI_SUCCESS FTW has finished the initialization
143 @retval EFI_ABORTED FTW initialization error
144
145 **/
146 EFI_STATUS
147 EFIAPI
148 InitializeFtwLite (
149 IN EFI_HANDLE ImageHandle,
150 IN EFI_SYSTEM_TABLE *SystemTable
151 );
152
153 //
154 // Fault Tolerant Write Protocol API
155 //
156 /**
157 Starts a target block update. This function will record data about write
158 in fault tolerant storage and will complete the write in a recoverable
159 manner, ensuring at all times that either the original contents or
160 the modified contents are available.
161
162
163 @param This Calling context
164 @param FvbHandle The handle of FVB protocol that provides services for
165 reading, writing, and erasing the target block.
166 @param Lba The logical block address of the target block.
167 @param Offset The offset within the target block to place the data.
168 @param NumBytes The number of bytes to write to the target block.
169 @param Buffer The data to write.
170
171 @retval EFI_SUCCESS The function completed successfully
172 @retval EFI_BAD_BUFFER_SIZE The write would span a target block, which is not
173 a valid action.
174 @retval EFI_ACCESS_DENIED No writes have been allocated.
175 @retval EFI_NOT_FOUND Cannot find FVB by handle.
176 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
177 @retval EFI_ABORTED The function could not complete successfully.
178
179 **/
180 EFI_STATUS
181 EFIAPI
182 FtwLiteWrite (
183 IN EFI_FTW_LITE_PROTOCOL *This,
184 IN EFI_HANDLE FvbHandle,
185 IN EFI_LBA Lba,
186 IN UINTN Offset,
187 IN OUT UINTN *NumBytes,
188 IN VOID *Buffer
189 );
190
191 //
192 // Internal functions
193 //
194 /**
195 Restarts a previously interrupted write. The caller must provide the
196 block protocol needed to complete the interrupted write.
197
198
199 @param FtwLiteDevice The private data of FTW_LITE driver
200 FvbHandle - The handle of FVB protocol that provides services for
201 reading, writing, and erasing the target block.
202
203 @retval EFI_SUCCESS The function completed successfully
204 @retval EFI_ACCESS_DENIED No pending writes exist
205 @retval EFI_NOT_FOUND FVB protocol not found by the handle
206 @retval EFI_ABORTED The function could not complete successfully
207
208 **/
209 EFI_STATUS
210 FtwRestart (
211 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
212 );
213
214 /**
215 Aborts all previous allocated writes.
216
217
218 @param FtwLiteDevice The private data of FTW_LITE driver
219
220 @retval EFI_SUCCESS The function completed successfully
221 @retval EFI_ABORTED The function could not complete successfully.
222 @retval EFI_NOT_FOUND No allocated writes exist.
223
224 **/
225 EFI_STATUS
226 FtwAbort (
227 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
228 );
229
230
231 /**
232 Write a record with fault tolerant mannaer.
233 Since the content has already backuped in spare block, the write is
234 guaranteed to be completed with fault tolerant manner.
235
236
237 @param FtwLiteDevice The private data of FTW_LITE driver
238 @param Fvb The FVB protocol that provides services for
239 reading, writing, and erasing the target block.
240
241 @retval EFI_SUCCESS The function completed successfully
242 @retval EFI_ABORTED The function could not complete successfully
243
244 **/
245 EFI_STATUS
246 FtwWriteRecord (
247 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
248 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb
249 );
250
251 /**
252 To Erase one block. The size is FTW_BLOCK_SIZE
253
254
255 @param FtwLiteDevice Calling context
256 @param FvBlock FVB Protocol interface
257 @param Lba Lba of the firmware block
258
259 @retval EFI_SUCCESS Block LBA is Erased successfully
260 @retval Others Error occurs
261
262 **/
263 EFI_STATUS
264 FtwEraseBlock (
265 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
266 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
267 EFI_LBA Lba
268 );
269
270 /**
271
272 Erase spare block.
273
274
275 @param FtwLiteDevice Calling context
276
277 @retval EFI_SUCCESS The erase request was successfully
278 completed.
279
280 @retval EFI_ACCESS_DENIED The firmware volume is in the
281 WriteDisabled state.
282 @retval EFI_DEVICE_ERROR The block device is not functioning
283 correctly and could not be written.
284 The firmware device may have been
285 partially erased.
286 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed
287 in the variable argument list do
288 not exist in the firmware volume.
289
290 **/
291 EFI_STATUS
292 FtwEraseSpareBlock (
293 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
294 );
295
296 /**
297 Retrive the proper FVB protocol interface by HANDLE.
298
299
300 @param FvBlockHandle The handle of FVB protocol that provides services for
301 reading, writing, and erasing the target block.
302 @param FvBlock The interface of FVB protocol
303
304 @retval EFI_SUCCESS The function completed successfully
305 @retval EFI_ABORTED The function could not complete successfully
306
307 **/
308 EFI_STATUS
309 FtwGetFvbByHandle (
310 IN EFI_HANDLE FvBlockHandle,
311 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
312 );
313
314 /**
315
316 Get firmware block by address.
317
318
319 @param Address Address specified the block
320 @param FvBlock The block caller wanted
321
322 @retval EFI_SUCCESS The protocol instance if found.
323 @retval EFI_NOT_FOUND Block not found
324
325 **/
326 EFI_STATUS
327 GetFvbByAddress (
328 IN EFI_PHYSICAL_ADDRESS Address,
329 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
330 );
331
332 /**
333
334 Is it in working block?
335
336
337 @param FtwLiteDevice Calling context
338 @param FvBlock Fvb protocol instance
339 @param Lba The block specified
340
341 @return A BOOLEAN value indicating in working block or not.
342
343 **/
344 BOOLEAN
345 IsInWorkingBlock (
346 EFI_FTW_LITE_DEVICE *FtwLiteDevice,
347 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
348 EFI_LBA Lba
349 );
350
351 /**
352
353 Check whether the block is a boot block.
354
355
356 @param FtwLiteDevice Calling context
357 @param FvBlock Fvb protocol instance
358 @param Lba Lba value
359
360 @retval FALSE This is a boot block.
361 @retval TRUE This is not a boot block.
362
363 **/
364 BOOLEAN
365 IsBootBlock (
366 EFI_FTW_LITE_DEVICE *FtwLiteDevice,
367 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
368 EFI_LBA Lba
369 );
370
371 /**
372 Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.
373 Spare block is accessed by FTW backup FVB protocol interface. LBA is
374 FtwLiteDevice->FtwSpareLba.
375 Target block is accessed by FvBlock protocol interface. LBA is Lba.
376
377
378 @param FtwLiteDevice The private data of FTW_LITE driver
379 @param FvBlock FVB Protocol interface to access target block
380 @param Lba Lba of the target block
381
382 @retval EFI_SUCCESS Spare block content is copied to target block
383 @retval EFI_INVALID_PARAMETER Input parameter error
384 @retval EFI_OUT_OF_RESOURCES Allocate memory error
385 @retval EFI_ABORTED The function could not complete successfully
386
387 **/
388 EFI_STATUS
389 FlushSpareBlockToTargetBlock (
390 EFI_FTW_LITE_DEVICE *FtwLiteDevice,
391 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
392 EFI_LBA Lba
393 );
394
395 /**
396 Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.
397 Spare block is accessed by FTW backup FVB protocol interface. LBA is
398 FtwLiteDevice->FtwSpareLba.
399 Working block is accessed by FTW working FVB protocol interface. LBA is
400 FtwLiteDevice->FtwWorkBlockLba.
401
402
403 @param FtwLiteDevice The private data of FTW_LITE driver
404
405 @retval EFI_SUCCESS Spare block content is copied to target block
406 @retval EFI_OUT_OF_RESOURCES Allocate memory error
407 @retval EFI_ABORTED The function could not complete successfully
408 Notes:
409 Since the working block header is important when FTW initializes, the
410 state of the operation should be handled carefully. The Crc value is
411 calculated without STATE element.
412
413 **/
414 EFI_STATUS
415 FlushSpareBlockToWorkingBlock (
416 EFI_FTW_LITE_DEVICE *FtwLiteDevice
417 );
418
419 /**
420 Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.
421 Spare block is accessed by FTW backup FVB protocol interface. LBA is
422 FtwLiteDevice->FtwSpareLba.
423 Boot block is accessed by BootFvb protocol interface. LBA is 0.
424
425
426 @param FtwLiteDevice The private data of FTW_LITE driver
427
428 @retval EFI_SUCCESS Spare block content is copied to boot block
429 @retval EFI_INVALID_PARAMETER Input parameter error
430 @retval EFI_OUT_OF_RESOURCES Allocate memory error
431 @retval EFI_ABORTED The function could not complete successfully
432 Notes:
433
434 **/
435 EFI_STATUS
436 FlushSpareBlockToBootBlock (
437 EFI_FTW_LITE_DEVICE *FtwLiteDevice
438 );
439
440 /**
441 Update a bit of state on a block device. The location of the bit is
442 calculated by the (Lba, Offset, bit). Here bit is determined by the
443 the name of a certain bit.
444
445
446 @param FvBlock FVB Protocol interface to access SrcBlock and DestBlock
447 @param Lba Lba of a block
448 @param Offset Offset on the Lba
449 @param NewBit New value that will override the old value if it can be change
450
451 @retval EFI_SUCCESS A state bit has been updated successfully
452 @retval Others Access block device error.
453 Notes:
454 Assume all bits of State are inside the same BYTE.
455 @retval EFI_ABORTED Read block fail
456
457 **/
458 EFI_STATUS
459 FtwUpdateFvState (
460 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
461 IN EFI_LBA Lba,
462 IN UINTN Offset,
463 IN UINT8 NewBit
464 );
465
466 /**
467 Get the last Write record pointer.
468 The last record is the record whose 'complete' state hasn't been set.
469 After all, this header may be a EMPTY header entry for next Allocate.
470
471
472 @param FtwLiteDevice Private data of this driver
473 @param FtwLastRecord Pointer to retrieve the last write record
474
475 @retval EFI_SUCCESS Get the last write record successfully
476 @retval EFI_ABORTED The FTW work space is damaged
477
478 **/
479 EFI_STATUS
480 FtwGetLastRecord (
481 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
482 OUT EFI_FTW_LITE_RECORD **FtwLastRecord
483 );
484
485 /**
486
487 Check whether a flash buffer is erased.
488
489
490 @param Polarity All 1 or all 0
491 @param Buffer Buffer to check
492 @param BufferSize Size of the buffer
493
494 @return A BOOLEAN value indicating erased or not.
495
496 **/
497 BOOLEAN
498 IsErasedFlashBuffer (
499 IN BOOLEAN Polarity,
500 IN UINT8 *Buffer,
501 IN UINTN BufferSize
502 );
503
504 /**
505 Initialize a work space when there is no work space.
506
507
508 @param WorkingHeader Pointer of working block header
509
510 @retval EFI_SUCCESS The function completed successfully
511 @retval EFI_ABORTED The function could not complete successfully.
512
513 **/
514 EFI_STATUS
515 InitWorkSpaceHeader (
516 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader
517 );
518
519 /**
520 Read from working block to refresh the work space in memory.
521
522
523 @param FtwLiteDevice Point to private data of FTW driver
524
525 @retval EFI_SUCCESS The function completed successfully
526 @retval EFI_ABORTED The function could not complete successfully.
527
528 **/
529 EFI_STATUS
530 WorkSpaceRefresh (
531 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice
532 );
533
534 /**
535 Check to see if it is a valid work space.
536
537
538 @param WorkingHeader Pointer of working block header
539
540 @retval EFI_SUCCESS The function completed successfully
541 @retval EFI_ABORTED The function could not complete successfully.
542
543 **/
544 BOOLEAN
545 IsValidWorkSpace (
546 IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader
547 );
548
549 /**
550 Reclaim the work space on the working block.
551
552
553 @param FtwLiteDevice Point to private data of FTW driver
554 @param PreserveRecord Whether to preserve the working record is needed
555
556 @retval EFI_SUCCESS The function completed successfully
557 @retval EFI_OUT_OF_RESOURCES Allocate memory error
558 @retval EFI_ABORTED The function could not complete successfully
559
560 **/
561 EFI_STATUS
562 FtwReclaimWorkSpace (
563 IN EFI_FTW_LITE_DEVICE *FtwLiteDevice,
564 IN BOOLEAN PreserveRecord
565 );
566
567 #endif