1 | /** @file
|
---|
2 |
|
---|
3 | Internal generic functions to operate flash block.
|
---|
4 |
|
---|
5 | Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
|
---|
6 | This program and the accompanying materials
|
---|
7 | are licensed and made available under the terms and conditions of the BSD License
|
---|
8 | which accompanies this distribution. The full text of the license may be found at
|
---|
9 | http://opensource.org/licenses/bsd-license.php
|
---|
10 |
|
---|
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
---|
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
---|
13 |
|
---|
14 | **/
|
---|
15 |
|
---|
16 | #include "FaultTolerantWrite.h"
|
---|
17 |
|
---|
18 | /**
|
---|
19 |
|
---|
20 | Check whether a flash buffer is erased.
|
---|
21 |
|
---|
22 | @param Buffer Buffer to check
|
---|
23 | @param BufferSize Size of the buffer
|
---|
24 |
|
---|
25 | @return A BOOLEAN value indicating erased or not.
|
---|
26 |
|
---|
27 | **/
|
---|
28 | BOOLEAN
|
---|
29 | IsErasedFlashBuffer (
|
---|
30 | IN UINT8 *Buffer,
|
---|
31 | IN UINTN BufferSize
|
---|
32 | )
|
---|
33 | {
|
---|
34 | BOOLEAN IsEmpty;
|
---|
35 | UINT8 *Ptr;
|
---|
36 | UINTN Index;
|
---|
37 |
|
---|
38 | Ptr = Buffer;
|
---|
39 | IsEmpty = TRUE;
|
---|
40 | for (Index = 0; Index < BufferSize; Index += 1) {
|
---|
41 | if (*Ptr++ != FTW_ERASED_BYTE) {
|
---|
42 | IsEmpty = FALSE;
|
---|
43 | break;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | return IsEmpty;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | To erase the block with the spare block size.
|
---|
52 |
|
---|
53 |
|
---|
54 | @param FtwDevice The private data of FTW driver
|
---|
55 | @param FvBlock FVB Protocol interface
|
---|
56 | @param Lba Lba of the firmware block
|
---|
57 |
|
---|
58 | @retval EFI_SUCCESS Block LBA is Erased successfully
|
---|
59 | @retval Others Error occurs
|
---|
60 |
|
---|
61 | **/
|
---|
62 | EFI_STATUS
|
---|
63 | FtwEraseBlock (
|
---|
64 | IN EFI_FTW_DEVICE *FtwDevice,
|
---|
65 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
|
---|
66 | EFI_LBA Lba
|
---|
67 | )
|
---|
68 | {
|
---|
69 | return FvBlock->EraseBlocks (
|
---|
70 | FvBlock,
|
---|
71 | Lba,
|
---|
72 | FtwDevice->NumberOfSpareBlock,
|
---|
73 | EFI_LBA_LIST_TERMINATOR
|
---|
74 | );
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | Erase spare block.
|
---|
79 |
|
---|
80 | @param FtwDevice The private data of FTW driver
|
---|
81 |
|
---|
82 | @retval EFI_SUCCESS The erase request was successfully completed.
|
---|
83 | @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled state.
|
---|
84 | @retval EFI_DEVICE_ERROR The block device is not functioning
|
---|
85 | correctly and could not be written.
|
---|
86 | The firmware device may have been
|
---|
87 | partially erased.
|
---|
88 | @retval EFI_INVALID_PARAMETER One or more of the LBAs listed
|
---|
89 | in the variable argument list do
|
---|
90 | not exist in the firmware volume.
|
---|
91 |
|
---|
92 |
|
---|
93 | **/
|
---|
94 | EFI_STATUS
|
---|
95 | FtwEraseSpareBlock (
|
---|
96 | IN EFI_FTW_DEVICE *FtwDevice
|
---|
97 | )
|
---|
98 | {
|
---|
99 | return FtwDevice->FtwBackupFvb->EraseBlocks (
|
---|
100 | FtwDevice->FtwBackupFvb,
|
---|
101 | FtwDevice->FtwSpareLba,
|
---|
102 | FtwDevice->NumberOfSpareBlock,
|
---|
103 | EFI_LBA_LIST_TERMINATOR
|
---|
104 | );
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 |
|
---|
109 | Is it in working block?
|
---|
110 |
|
---|
111 | @param FtwDevice The private data of FTW driver
|
---|
112 | @param FvBlock Fvb protocol instance
|
---|
113 | @param Lba The block specified
|
---|
114 |
|
---|
115 | @return A BOOLEAN value indicating in working block or not.
|
---|
116 |
|
---|
117 | **/
|
---|
118 | BOOLEAN
|
---|
119 | IsWorkingBlock (
|
---|
120 | EFI_FTW_DEVICE *FtwDevice,
|
---|
121 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
|
---|
122 | EFI_LBA Lba
|
---|
123 | )
|
---|
124 | {
|
---|
125 | //
|
---|
126 | // If matching the following condition, the target block is in working block.
|
---|
127 | // 1. Target block is on the FV of working block (Using the same FVB protocol instance).
|
---|
128 | // 2. Lba falls into the range of working block.
|
---|
129 | //
|
---|
130 | return (BOOLEAN)
|
---|
131 | (
|
---|
132 | (FvBlock == FtwDevice->FtwFvBlock) &&
|
---|
133 | (Lba >= FtwDevice->FtwWorkBlockLba) &&
|
---|
134 | (Lba <= FtwDevice->FtwWorkSpaceLba)
|
---|
135 | );
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 |
|
---|
140 | Get firmware block by address.
|
---|
141 |
|
---|
142 |
|
---|
143 | @param Address Address specified the block
|
---|
144 | @param FvBlock The block caller wanted
|
---|
145 |
|
---|
146 | @retval EFI_SUCCESS The protocol instance if found.
|
---|
147 | @retval EFI_NOT_FOUND Block not found
|
---|
148 |
|
---|
149 | **/
|
---|
150 | EFI_HANDLE
|
---|
151 | GetFvbByAddress (
|
---|
152 | IN EFI_PHYSICAL_ADDRESS Address,
|
---|
153 | OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
|
---|
154 | )
|
---|
155 | {
|
---|
156 | EFI_STATUS Status;
|
---|
157 | EFI_HANDLE *HandleBuffer;
|
---|
158 | UINTN HandleCount;
|
---|
159 | UINTN Index;
|
---|
160 | EFI_PHYSICAL_ADDRESS FvbBaseAddress;
|
---|
161 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
|
---|
162 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
|
---|
163 | EFI_HANDLE FvbHandle;
|
---|
164 |
|
---|
165 | *FvBlock = NULL;
|
---|
166 | FvbHandle = NULL;
|
---|
167 | HandleBuffer = NULL;
|
---|
168 | //
|
---|
169 | // Locate all handles of Fvb protocol
|
---|
170 | //
|
---|
171 | Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);
|
---|
172 | if (EFI_ERROR (Status)) {
|
---|
173 | return NULL;
|
---|
174 | }
|
---|
175 | //
|
---|
176 | // Get the FVB to access variable store
|
---|
177 | //
|
---|
178 | for (Index = 0; Index < HandleCount; Index += 1) {
|
---|
179 | Status = FtwGetFvbByHandle (HandleBuffer[Index], &Fvb);
|
---|
180 | if (EFI_ERROR (Status)) {
|
---|
181 | break;
|
---|
182 | }
|
---|
183 | //
|
---|
184 | // Compare the address and select the right one
|
---|
185 | //
|
---|
186 | Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);
|
---|
187 | if (EFI_ERROR (Status)) {
|
---|
188 | continue;
|
---|
189 | }
|
---|
190 |
|
---|
191 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);
|
---|
192 | if ((Address >= FvbBaseAddress) && (Address <= (FvbBaseAddress + (FwVolHeader->FvLength - 1)))) {
|
---|
193 | *FvBlock = Fvb;
|
---|
194 | FvbHandle = HandleBuffer[Index];
|
---|
195 | break;
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | FreePool (HandleBuffer);
|
---|
200 | return FvbHandle;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 |
|
---|
205 | Is it in boot block?
|
---|
206 |
|
---|
207 | @param FtwDevice The private data of FTW driver
|
---|
208 | @param FvBlock Fvb protocol instance
|
---|
209 | @param Lba The block specified
|
---|
210 |
|
---|
211 | @return A BOOLEAN value indicating in boot block or not.
|
---|
212 |
|
---|
213 | **/
|
---|
214 | BOOLEAN
|
---|
215 | IsBootBlock (
|
---|
216 | EFI_FTW_DEVICE *FtwDevice,
|
---|
217 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
|
---|
218 | EFI_LBA Lba
|
---|
219 | )
|
---|
220 | {
|
---|
221 | EFI_STATUS Status;
|
---|
222 | EFI_SWAP_ADDRESS_RANGE_PROTOCOL *SarProtocol;
|
---|
223 | EFI_PHYSICAL_ADDRESS BootBlockBase;
|
---|
224 | UINTN BootBlockSize;
|
---|
225 | EFI_PHYSICAL_ADDRESS BackupBlockBase;
|
---|
226 | UINTN BackupBlockSize;
|
---|
227 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *BootFvb;
|
---|
228 | BOOLEAN IsSwapped;
|
---|
229 | EFI_HANDLE FvbHandle;
|
---|
230 |
|
---|
231 | if (!FeaturePcdGet(PcdFullFtwServiceEnable)) {
|
---|
232 | return FALSE;
|
---|
233 | }
|
---|
234 |
|
---|
235 | Status = FtwGetSarProtocol ((VOID **) &SarProtocol);
|
---|
236 | if (EFI_ERROR (Status)) {
|
---|
237 | return FALSE;
|
---|
238 | }
|
---|
239 | //
|
---|
240 | // Get the boot block range
|
---|
241 | //
|
---|
242 | Status = SarProtocol->GetRangeLocation (
|
---|
243 | SarProtocol,
|
---|
244 | &BootBlockBase,
|
---|
245 | &BootBlockSize,
|
---|
246 | &BackupBlockBase,
|
---|
247 | &BackupBlockSize
|
---|
248 | );
|
---|
249 | if (EFI_ERROR (Status)) {
|
---|
250 | return FALSE;
|
---|
251 | }
|
---|
252 |
|
---|
253 | Status = SarProtocol->GetSwapState (SarProtocol, &IsSwapped);
|
---|
254 | if (EFI_ERROR (Status)) {
|
---|
255 | return FALSE;
|
---|
256 | }
|
---|
257 | //
|
---|
258 | // Get FVB by address
|
---|
259 | //
|
---|
260 | if (!IsSwapped) {
|
---|
261 | FvbHandle = GetFvbByAddress (BootBlockBase, &BootFvb);
|
---|
262 | } else {
|
---|
263 | FvbHandle = GetFvbByAddress (BackupBlockBase, &BootFvb);
|
---|
264 | }
|
---|
265 |
|
---|
266 | if (FvbHandle == NULL) {
|
---|
267 | return FALSE;
|
---|
268 | }
|
---|
269 | //
|
---|
270 | // Compare the Fvb
|
---|
271 | //
|
---|
272 | return (BOOLEAN) (FvBlock == BootFvb);
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.
|
---|
277 | Spare block is accessed by FTW working FVB protocol interface. LBA is 1.
|
---|
278 | Target block is accessed by FvbBlock protocol interface. LBA is Lba.
|
---|
279 |
|
---|
280 | FTW will do extra work on boot block update.
|
---|
281 | FTW should depend on a protocol of EFI_ADDRESS_RANGE_SWAP_PROTOCOL,
|
---|
282 | which is produced by a chipset driver.
|
---|
283 | FTW updating boot block steps may be:
|
---|
284 | 1. GetRangeLocation(), if the Range is inside the boot block, FTW know
|
---|
285 | that boot block will be update. It shall add a FLAG in the working block.
|
---|
286 | 2. When spare block is ready,
|
---|
287 | 3. SetSwapState(EFI_SWAPPED)
|
---|
288 | 4. erasing boot block,
|
---|
289 | 5. programming boot block until the boot block is ok.
|
---|
290 | 6. SetSwapState(UNSWAPPED)
|
---|
291 | FTW shall not allow to update boot block when battery state is error.
|
---|
292 |
|
---|
293 | @param FtwDevice The private data of FTW driver
|
---|
294 |
|
---|
295 | @retval EFI_SUCCESS Spare block content is copied to boot block
|
---|
296 | @retval EFI_INVALID_PARAMETER Input parameter error
|
---|
297 | @retval EFI_OUT_OF_RESOURCES Allocate memory error
|
---|
298 | @retval EFI_ABORTED The function could not complete successfully
|
---|
299 |
|
---|
300 | **/
|
---|
301 | EFI_STATUS
|
---|
302 | FlushSpareBlockToBootBlock (
|
---|
303 | EFI_FTW_DEVICE *FtwDevice
|
---|
304 | )
|
---|
305 | {
|
---|
306 | EFI_STATUS Status;
|
---|
307 | UINTN Length;
|
---|
308 | UINT8 *Buffer;
|
---|
309 | UINTN Count;
|
---|
310 | UINT8 *Ptr;
|
---|
311 | UINTN Index;
|
---|
312 | BOOLEAN TopSwap;
|
---|
313 | EFI_SWAP_ADDRESS_RANGE_PROTOCOL *SarProtocol;
|
---|
314 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *BootFvb;
|
---|
315 | EFI_LBA BootLba;
|
---|
316 |
|
---|
317 | if (!FeaturePcdGet(PcdFullFtwServiceEnable)) {
|
---|
318 | return EFI_UNSUPPORTED;
|
---|
319 | }
|
---|
320 |
|
---|
321 | //
|
---|
322 | // Locate swap address range protocol
|
---|
323 | //
|
---|
324 | Status = FtwGetSarProtocol ((VOID **) &SarProtocol);
|
---|
325 | if (EFI_ERROR (Status)) {
|
---|
326 | return Status;
|
---|
327 | }
|
---|
328 | //
|
---|
329 | // Allocate a memory buffer
|
---|
330 | //
|
---|
331 | Length = FtwDevice->SpareAreaLength;
|
---|
332 | Buffer = AllocatePool (Length);
|
---|
333 | if (Buffer == NULL) {
|
---|
334 | return EFI_OUT_OF_RESOURCES;
|
---|
335 | }
|
---|
336 | //
|
---|
337 | // Get TopSwap bit state
|
---|
338 | //
|
---|
339 | Status = SarProtocol->GetSwapState (SarProtocol, &TopSwap);
|
---|
340 | if (EFI_ERROR (Status)) {
|
---|
341 | DEBUG ((EFI_D_ERROR, "Ftw: Get Top Swapped status - %r\n", Status));
|
---|
342 | FreePool (Buffer);
|
---|
343 | return EFI_ABORTED;
|
---|
344 | }
|
---|
345 |
|
---|
346 | if (TopSwap) {
|
---|
347 | //
|
---|
348 | // Get FVB of current boot block
|
---|
349 | //
|
---|
350 | if (GetFvbByAddress (FtwDevice->SpareAreaAddress + FtwDevice->SpareAreaLength, &BootFvb) == NULL) {
|
---|
351 | FreePool (Buffer);
|
---|
352 | return EFI_ABORTED;
|
---|
353 | }
|
---|
354 | //
|
---|
355 | // Read data from current boot block
|
---|
356 | //
|
---|
357 | BootLba = 0;
|
---|
358 | Ptr = Buffer;
|
---|
359 | for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
|
---|
360 | Count = FtwDevice->BlockSize;
|
---|
361 | Status = BootFvb->Read (
|
---|
362 | BootFvb,
|
---|
363 | BootLba + Index,
|
---|
364 | 0,
|
---|
365 | &Count,
|
---|
366 | Ptr
|
---|
367 | );
|
---|
368 | if (EFI_ERROR (Status)) {
|
---|
369 | FreePool (Buffer);
|
---|
370 | return Status;
|
---|
371 | }
|
---|
372 |
|
---|
373 | Ptr += Count;
|
---|
374 | }
|
---|
375 | } else {
|
---|
376 | //
|
---|
377 | // Read data from spare block
|
---|
378 | //
|
---|
379 | Ptr = Buffer;
|
---|
380 | for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
|
---|
381 | Count = FtwDevice->BlockSize;
|
---|
382 | Status = FtwDevice->FtwBackupFvb->Read (
|
---|
383 | FtwDevice->FtwBackupFvb,
|
---|
384 | FtwDevice->FtwSpareLba + Index,
|
---|
385 | 0,
|
---|
386 | &Count,
|
---|
387 | Ptr
|
---|
388 | );
|
---|
389 | if (EFI_ERROR (Status)) {
|
---|
390 | FreePool (Buffer);
|
---|
391 | return Status;
|
---|
392 | }
|
---|
393 |
|
---|
394 | Ptr += Count;
|
---|
395 | }
|
---|
396 | //
|
---|
397 | // Set TopSwap bit
|
---|
398 | //
|
---|
399 | Status = SarProtocol->SetSwapState (SarProtocol, TRUE);
|
---|
400 | if (EFI_ERROR (Status)) {
|
---|
401 | FreePool (Buffer);
|
---|
402 | return Status;
|
---|
403 | }
|
---|
404 | }
|
---|
405 | //
|
---|
406 | // Erase current spare block
|
---|
407 | // Because TopSwap is set, this actually erase the top block (boot block)!
|
---|
408 | //
|
---|
409 | Status = FtwEraseSpareBlock (FtwDevice);
|
---|
410 | if (EFI_ERROR (Status)) {
|
---|
411 | FreePool (Buffer);
|
---|
412 | return EFI_ABORTED;
|
---|
413 | }
|
---|
414 | //
|
---|
415 | // Write memory buffer to current spare block. Still top block.
|
---|
416 | //
|
---|
417 | Ptr = Buffer;
|
---|
418 | for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
|
---|
419 | Count = FtwDevice->BlockSize;
|
---|
420 | Status = FtwDevice->FtwBackupFvb->Write (
|
---|
421 | FtwDevice->FtwBackupFvb,
|
---|
422 | FtwDevice->FtwSpareLba + Index,
|
---|
423 | 0,
|
---|
424 | &Count,
|
---|
425 | Ptr
|
---|
426 | );
|
---|
427 | if (EFI_ERROR (Status)) {
|
---|
428 | DEBUG ((EFI_D_ERROR, "Ftw: FVB Write boot block - %r\n", Status));
|
---|
429 | FreePool (Buffer);
|
---|
430 | return Status;
|
---|
431 | }
|
---|
432 |
|
---|
433 | Ptr += Count;
|
---|
434 | }
|
---|
435 |
|
---|
436 | FreePool (Buffer);
|
---|
437 |
|
---|
438 | //
|
---|
439 | // Clear TopSwap bit
|
---|
440 | //
|
---|
441 | Status = SarProtocol->SetSwapState (SarProtocol, FALSE);
|
---|
442 |
|
---|
443 | return Status;
|
---|
444 | }
|
---|
445 |
|
---|
446 | /**
|
---|
447 | Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.
|
---|
448 | Spare block is accessed by FTW backup FVB protocol interface. LBA is 1.
|
---|
449 | Target block is accessed by FvbBlock protocol interface. LBA is Lba.
|
---|
450 |
|
---|
451 |
|
---|
452 | @param FtwDevice The private data of FTW driver
|
---|
453 | @param FvBlock FVB Protocol interface to access target block
|
---|
454 | @param Lba Lba of the target block
|
---|
455 |
|
---|
456 | @retval EFI_SUCCESS Spare block content is copied to target block
|
---|
457 | @retval EFI_INVALID_PARAMETER Input parameter error
|
---|
458 | @retval EFI_OUT_OF_RESOURCES Allocate memory error
|
---|
459 | @retval EFI_ABORTED The function could not complete successfully
|
---|
460 |
|
---|
461 | **/
|
---|
462 | EFI_STATUS
|
---|
463 | FlushSpareBlockToTargetBlock (
|
---|
464 | EFI_FTW_DEVICE *FtwDevice,
|
---|
465 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
|
---|
466 | EFI_LBA Lba
|
---|
467 | )
|
---|
468 | {
|
---|
469 | EFI_STATUS Status;
|
---|
470 | UINTN Length;
|
---|
471 | UINT8 *Buffer;
|
---|
472 | UINTN Count;
|
---|
473 | UINT8 *Ptr;
|
---|
474 | UINTN Index;
|
---|
475 |
|
---|
476 | if ((FtwDevice == NULL) || (FvBlock == NULL)) {
|
---|
477 | return EFI_INVALID_PARAMETER;
|
---|
478 | }
|
---|
479 | //
|
---|
480 | // Allocate a memory buffer
|
---|
481 | //
|
---|
482 | Length = FtwDevice->SpareAreaLength;
|
---|
483 | Buffer = AllocatePool (Length);
|
---|
484 | if (Buffer == NULL) {
|
---|
485 | return EFI_OUT_OF_RESOURCES;
|
---|
486 | }
|
---|
487 | //
|
---|
488 | // Read all content of spare block to memory buffer
|
---|
489 | //
|
---|
490 | Ptr = Buffer;
|
---|
491 | for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
|
---|
492 | Count = FtwDevice->BlockSize;
|
---|
493 | Status = FtwDevice->FtwBackupFvb->Read (
|
---|
494 | FtwDevice->FtwBackupFvb,
|
---|
495 | FtwDevice->FtwSpareLba + Index,
|
---|
496 | 0,
|
---|
497 | &Count,
|
---|
498 | Ptr
|
---|
499 | );
|
---|
500 | if (EFI_ERROR (Status)) {
|
---|
501 | FreePool (Buffer);
|
---|
502 | return Status;
|
---|
503 | }
|
---|
504 |
|
---|
505 | Ptr += Count;
|
---|
506 | }
|
---|
507 | //
|
---|
508 | // Erase the target block
|
---|
509 | //
|
---|
510 | Status = FtwEraseBlock (FtwDevice, FvBlock, Lba);
|
---|
511 | if (EFI_ERROR (Status)) {
|
---|
512 | FreePool (Buffer);
|
---|
513 | return EFI_ABORTED;
|
---|
514 | }
|
---|
515 | //
|
---|
516 | // Write memory buffer to block, using the FvbBlock protocol interface
|
---|
517 | //
|
---|
518 | Ptr = Buffer;
|
---|
519 | for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
|
---|
520 | Count = FtwDevice->BlockSize;
|
---|
521 | Status = FvBlock->Write (FvBlock, Lba + Index, 0, &Count, Ptr);
|
---|
522 | if (EFI_ERROR (Status)) {
|
---|
523 | DEBUG ((EFI_D_ERROR, "Ftw: FVB Write block - %r\n", Status));
|
---|
524 | FreePool (Buffer);
|
---|
525 | return Status;
|
---|
526 | }
|
---|
527 |
|
---|
528 | Ptr += Count;
|
---|
529 | }
|
---|
530 |
|
---|
531 | FreePool (Buffer);
|
---|
532 |
|
---|
533 | return Status;
|
---|
534 | }
|
---|
535 |
|
---|
536 | /**
|
---|
537 | Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.
|
---|
538 | Spare block is accessed by FTW backup FVB protocol interface. LBA is
|
---|
539 | FtwDevice->FtwSpareLba.
|
---|
540 | Working block is accessed by FTW working FVB protocol interface. LBA is
|
---|
541 | FtwDevice->FtwWorkBlockLba.
|
---|
542 |
|
---|
543 | Since the working block header is important when FTW initializes, the
|
---|
544 | state of the operation should be handled carefully. The Crc value is
|
---|
545 | calculated without STATE element.
|
---|
546 |
|
---|
547 | @param FtwDevice The private data of FTW driver
|
---|
548 |
|
---|
549 | @retval EFI_SUCCESS Spare block content is copied to target block
|
---|
550 | @retval EFI_OUT_OF_RESOURCES Allocate memory error
|
---|
551 | @retval EFI_ABORTED The function could not complete successfully
|
---|
552 |
|
---|
553 | **/
|
---|
554 | EFI_STATUS
|
---|
555 | FlushSpareBlockToWorkingBlock (
|
---|
556 | EFI_FTW_DEVICE *FtwDevice
|
---|
557 | )
|
---|
558 | {
|
---|
559 | EFI_STATUS Status;
|
---|
560 | UINTN Length;
|
---|
561 | UINT8 *Buffer;
|
---|
562 | EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingBlockHeader;
|
---|
563 | UINTN Count;
|
---|
564 | UINT8 *Ptr;
|
---|
565 | UINTN Index;
|
---|
566 | EFI_LBA WorkSpaceLbaOffset;
|
---|
567 |
|
---|
568 | //
|
---|
569 | // Allocate a memory buffer
|
---|
570 | //
|
---|
571 | Length = FtwDevice->SpareAreaLength;
|
---|
572 | Buffer = AllocatePool (Length);
|
---|
573 | if (Buffer == NULL) {
|
---|
574 | return EFI_OUT_OF_RESOURCES;
|
---|
575 | }
|
---|
576 |
|
---|
577 | WorkSpaceLbaOffset = FtwDevice->FtwWorkSpaceLba - FtwDevice->FtwWorkBlockLba;
|
---|
578 |
|
---|
579 | //
|
---|
580 | // To guarantee that the WorkingBlockValid is set on spare block
|
---|
581 | //
|
---|
582 | // Offset = OFFSET_OF(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER,
|
---|
583 | // WorkingBlockValid);
|
---|
584 | // To skip Signature and Crc: sizeof(EFI_GUID)+sizeof(UINT32).
|
---|
585 | //
|
---|
586 | FtwUpdateFvState (
|
---|
587 | FtwDevice->FtwBackupFvb,
|
---|
588 | FtwDevice->FtwSpareLba + WorkSpaceLbaOffset,
|
---|
589 | FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),
|
---|
590 | WORKING_BLOCK_VALID
|
---|
591 | );
|
---|
592 | //
|
---|
593 | // Read from spare block to memory buffer
|
---|
594 | //
|
---|
595 | Ptr = Buffer;
|
---|
596 | for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
|
---|
597 | Count = FtwDevice->BlockSize;
|
---|
598 | Status = FtwDevice->FtwBackupFvb->Read (
|
---|
599 | FtwDevice->FtwBackupFvb,
|
---|
600 | FtwDevice->FtwSpareLba + Index,
|
---|
601 | 0,
|
---|
602 | &Count,
|
---|
603 | Ptr
|
---|
604 | );
|
---|
605 | if (EFI_ERROR (Status)) {
|
---|
606 | FreePool (Buffer);
|
---|
607 | return Status;
|
---|
608 | }
|
---|
609 |
|
---|
610 | Ptr += Count;
|
---|
611 | }
|
---|
612 | //
|
---|
613 | // Clear the CRC and STATE, copy data from spare to working block.
|
---|
614 | //
|
---|
615 | WorkingBlockHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) (Buffer + (UINTN) WorkSpaceLbaOffset * FtwDevice->BlockSize + FtwDevice->FtwWorkSpaceBase);
|
---|
616 | InitWorkSpaceHeader (WorkingBlockHeader);
|
---|
617 | WorkingBlockHeader->WorkingBlockValid = FTW_ERASE_POLARITY;
|
---|
618 | WorkingBlockHeader->WorkingBlockInvalid = FTW_ERASE_POLARITY;
|
---|
619 |
|
---|
620 | //
|
---|
621 | // target block is working block, then
|
---|
622 | // Set WorkingBlockInvalid in EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
|
---|
623 | // before erase the working block.
|
---|
624 | //
|
---|
625 | // Offset = OFFSET_OF(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER,
|
---|
626 | // WorkingBlockInvalid);
|
---|
627 | // So hardcode offset as sizeof(EFI_GUID)+sizeof(UINT32) to
|
---|
628 | // skip Signature and Crc.
|
---|
629 | //
|
---|
630 | Status = FtwUpdateFvState (
|
---|
631 | FtwDevice->FtwFvBlock,
|
---|
632 | FtwDevice->FtwWorkSpaceLba,
|
---|
633 | FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),
|
---|
634 | WORKING_BLOCK_INVALID
|
---|
635 | );
|
---|
636 | if (EFI_ERROR (Status)) {
|
---|
637 | FreePool (Buffer);
|
---|
638 | return EFI_ABORTED;
|
---|
639 | }
|
---|
640 |
|
---|
641 | FtwDevice->FtwWorkSpaceHeader->WorkingBlockInvalid = FTW_VALID_STATE;
|
---|
642 |
|
---|
643 | //
|
---|
644 | // Erase the working block
|
---|
645 | //
|
---|
646 | Status = FtwEraseBlock (FtwDevice, FtwDevice->FtwFvBlock, FtwDevice->FtwWorkBlockLba);
|
---|
647 | if (EFI_ERROR (Status)) {
|
---|
648 | FreePool (Buffer);
|
---|
649 | return EFI_ABORTED;
|
---|
650 | }
|
---|
651 | //
|
---|
652 | // Write memory buffer to working block, using the FvbBlock protocol interface
|
---|
653 | //
|
---|
654 | Ptr = Buffer;
|
---|
655 | for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
|
---|
656 | Count = FtwDevice->BlockSize;
|
---|
657 | Status = FtwDevice->FtwFvBlock->Write (
|
---|
658 | FtwDevice->FtwFvBlock,
|
---|
659 | FtwDevice->FtwWorkBlockLba + Index,
|
---|
660 | 0,
|
---|
661 | &Count,
|
---|
662 | Ptr
|
---|
663 | );
|
---|
664 | if (EFI_ERROR (Status)) {
|
---|
665 | DEBUG ((EFI_D_ERROR, "Ftw: FVB Write block - %r\n", Status));
|
---|
666 | FreePool (Buffer);
|
---|
667 | return Status;
|
---|
668 | }
|
---|
669 |
|
---|
670 | Ptr += Count;
|
---|
671 | }
|
---|
672 | //
|
---|
673 | // Since the memory buffer will not be used, free memory Buffer.
|
---|
674 | //
|
---|
675 | FreePool (Buffer);
|
---|
676 |
|
---|
677 | //
|
---|
678 | // Update the VALID of the working block
|
---|
679 | //
|
---|
680 | // Offset = OFFSET_OF(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER, WorkingBlockValid);
|
---|
681 | // So hardcode offset as sizeof(EFI_GUID)+sizeof(UINT32) to skip Signature and Crc.
|
---|
682 | //
|
---|
683 | Status = FtwUpdateFvState (
|
---|
684 | FtwDevice->FtwFvBlock,
|
---|
685 | FtwDevice->FtwWorkSpaceLba,
|
---|
686 | FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),
|
---|
687 | WORKING_BLOCK_VALID
|
---|
688 | );
|
---|
689 | if (EFI_ERROR (Status)) {
|
---|
690 | return EFI_ABORTED;
|
---|
691 | }
|
---|
692 |
|
---|
693 | FtwDevice->FtwWorkSpaceHeader->WorkingBlockInvalid = FTW_INVALID_STATE;
|
---|
694 | FtwDevice->FtwWorkSpaceHeader->WorkingBlockValid = FTW_VALID_STATE;
|
---|
695 |
|
---|
696 | return EFI_SUCCESS;
|
---|
697 | }
|
---|
698 |
|
---|
699 | /**
|
---|
700 | Update a bit of state on a block device. The location of the bit is
|
---|
701 | calculated by the (Lba, Offset, bit). Here bit is determined by the
|
---|
702 | the name of a certain bit.
|
---|
703 |
|
---|
704 |
|
---|
705 | @param FvBlock FVB Protocol interface to access SrcBlock and DestBlock
|
---|
706 | @param Lba Lba of a block
|
---|
707 | @param Offset Offset on the Lba
|
---|
708 | @param NewBit New value that will override the old value if it can be change
|
---|
709 |
|
---|
710 | @retval EFI_SUCCESS A state bit has been updated successfully
|
---|
711 | @retval Others Access block device error.
|
---|
712 | Notes:
|
---|
713 | Assume all bits of State are inside the same BYTE.
|
---|
714 | @retval EFI_ABORTED Read block fail
|
---|
715 |
|
---|
716 | **/
|
---|
717 | EFI_STATUS
|
---|
718 | FtwUpdateFvState (
|
---|
719 | IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
|
---|
720 | IN EFI_LBA Lba,
|
---|
721 | IN UINTN Offset,
|
---|
722 | IN UINT8 NewBit
|
---|
723 | )
|
---|
724 | {
|
---|
725 | EFI_STATUS Status;
|
---|
726 | UINT8 State;
|
---|
727 | UINTN Length;
|
---|
728 |
|
---|
729 | //
|
---|
730 | // Read state from device, assume State is only one byte.
|
---|
731 | //
|
---|
732 | Length = sizeof (UINT8);
|
---|
733 | Status = FvBlock->Read (FvBlock, Lba, Offset, &Length, &State);
|
---|
734 | if (EFI_ERROR (Status)) {
|
---|
735 | return EFI_ABORTED;
|
---|
736 | }
|
---|
737 |
|
---|
738 | State ^= FTW_POLARITY_REVERT;
|
---|
739 | State = (UINT8) (State | NewBit);
|
---|
740 | State ^= FTW_POLARITY_REVERT;
|
---|
741 |
|
---|
742 | //
|
---|
743 | // Write state back to device
|
---|
744 | //
|
---|
745 | Length = sizeof (UINT8);
|
---|
746 | Status = FvBlock->Write (FvBlock, Lba, Offset, &Length, &State);
|
---|
747 |
|
---|
748 | return Status;
|
---|
749 | }
|
---|
750 |
|
---|
751 | /**
|
---|
752 | Get the last Write Header pointer.
|
---|
753 | The last write header is the header whose 'complete' state hasn't been set.
|
---|
754 | After all, this header may be a EMPTY header entry for next Allocate.
|
---|
755 |
|
---|
756 |
|
---|
757 | @param FtwWorkSpaceHeader Pointer of the working block header
|
---|
758 | @param FtwWorkSpaceSize Size of the work space
|
---|
759 | @param FtwWriteHeader Pointer to retrieve the last write header
|
---|
760 |
|
---|
761 | @retval EFI_SUCCESS Get the last write record successfully
|
---|
762 | @retval EFI_ABORTED The FTW work space is damaged
|
---|
763 |
|
---|
764 | **/
|
---|
765 | EFI_STATUS
|
---|
766 | FtwGetLastWriteHeader (
|
---|
767 | IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *FtwWorkSpaceHeader,
|
---|
768 | IN UINTN FtwWorkSpaceSize,
|
---|
769 | OUT EFI_FAULT_TOLERANT_WRITE_HEADER **FtwWriteHeader
|
---|
770 | )
|
---|
771 | {
|
---|
772 | UINTN Offset;
|
---|
773 | EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader;
|
---|
774 |
|
---|
775 | *FtwWriteHeader = NULL;
|
---|
776 | FtwHeader = (EFI_FAULT_TOLERANT_WRITE_HEADER *) (FtwWorkSpaceHeader + 1);
|
---|
777 | Offset = sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER);
|
---|
778 |
|
---|
779 | while (FtwHeader->Complete == FTW_VALID_STATE) {
|
---|
780 | Offset += FTW_WRITE_TOTAL_SIZE (FtwHeader->NumberOfWrites, FtwHeader->PrivateDataSize);
|
---|
781 | //
|
---|
782 | // If Offset exceed the FTW work space boudary, return error.
|
---|
783 | //
|
---|
784 | if (Offset >= FtwWorkSpaceSize) {
|
---|
785 | *FtwWriteHeader = FtwHeader;
|
---|
786 | return EFI_ABORTED;
|
---|
787 | }
|
---|
788 |
|
---|
789 | FtwHeader = (EFI_FAULT_TOLERANT_WRITE_HEADER *) ((UINT8 *) FtwWorkSpaceHeader + Offset);
|
---|
790 | }
|
---|
791 | //
|
---|
792 | // Last write header is found
|
---|
793 | //
|
---|
794 | *FtwWriteHeader = FtwHeader;
|
---|
795 |
|
---|
796 | return EFI_SUCCESS;
|
---|
797 | }
|
---|
798 |
|
---|
799 | /**
|
---|
800 | Get the last Write Record pointer. The last write Record is the Record
|
---|
801 | whose DestinationCompleted state hasn't been set. After all, this Record
|
---|
802 | may be a EMPTY record entry for next write.
|
---|
803 |
|
---|
804 |
|
---|
805 | @param FtwWriteHeader Pointer to the write record header
|
---|
806 | @param FtwWriteRecord Pointer to retrieve the last write record
|
---|
807 |
|
---|
808 | @retval EFI_SUCCESS Get the last write record successfully
|
---|
809 | @retval EFI_ABORTED The FTW work space is damaged
|
---|
810 |
|
---|
811 | **/
|
---|
812 | EFI_STATUS
|
---|
813 | FtwGetLastWriteRecord (
|
---|
814 | IN EFI_FAULT_TOLERANT_WRITE_HEADER *FtwWriteHeader,
|
---|
815 | OUT EFI_FAULT_TOLERANT_WRITE_RECORD **FtwWriteRecord
|
---|
816 | )
|
---|
817 | {
|
---|
818 | UINTN Index;
|
---|
819 | EFI_FAULT_TOLERANT_WRITE_RECORD *FtwRecord;
|
---|
820 |
|
---|
821 | *FtwWriteRecord = NULL;
|
---|
822 | FtwRecord = (EFI_FAULT_TOLERANT_WRITE_RECORD *) (FtwWriteHeader + 1);
|
---|
823 |
|
---|
824 | //
|
---|
825 | // Try to find the last write record "that has not completed"
|
---|
826 | //
|
---|
827 | for (Index = 0; Index < FtwWriteHeader->NumberOfWrites; Index += 1) {
|
---|
828 | if (FtwRecord->DestinationComplete != FTW_VALID_STATE) {
|
---|
829 | //
|
---|
830 | // The last write record is found
|
---|
831 | //
|
---|
832 | *FtwWriteRecord = FtwRecord;
|
---|
833 | return EFI_SUCCESS;
|
---|
834 | }
|
---|
835 |
|
---|
836 | FtwRecord++;
|
---|
837 |
|
---|
838 | if (FtwWriteHeader->PrivateDataSize != 0) {
|
---|
839 | FtwRecord = (EFI_FAULT_TOLERANT_WRITE_RECORD *) ((UINTN) FtwRecord + (UINTN) FtwWriteHeader->PrivateDataSize);
|
---|
840 | }
|
---|
841 | }
|
---|
842 | //
|
---|
843 | // if Index == NumberOfWrites, then
|
---|
844 | // the last record has been written successfully,
|
---|
845 | // but the Header->Complete Flag has not been set.
|
---|
846 | // also return the last record.
|
---|
847 | //
|
---|
848 | if (Index == FtwWriteHeader->NumberOfWrites) {
|
---|
849 | *FtwWriteRecord = (EFI_FAULT_TOLERANT_WRITE_RECORD *) ((UINTN) FtwRecord - FTW_RECORD_SIZE (FtwWriteHeader->PrivateDataSize));
|
---|
850 | return EFI_SUCCESS;
|
---|
851 | }
|
---|
852 |
|
---|
853 | return EFI_ABORTED;
|
---|
854 | }
|
---|
855 |
|
---|
856 | /**
|
---|
857 | To check if FtwRecord is the first record of FtwHeader.
|
---|
858 |
|
---|
859 | @param FtwHeader Pointer to the write record header
|
---|
860 | @param FtwRecord Pointer to the write record
|
---|
861 |
|
---|
862 | @retval TRUE FtwRecord is the first Record of the FtwHeader
|
---|
863 | @retval FALSE FtwRecord is not the first Record of the FtwHeader
|
---|
864 |
|
---|
865 | **/
|
---|
866 | BOOLEAN
|
---|
867 | IsFirstRecordOfWrites (
|
---|
868 | IN EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader,
|
---|
869 | IN EFI_FAULT_TOLERANT_WRITE_RECORD *FtwRecord
|
---|
870 | )
|
---|
871 | {
|
---|
872 | UINT8 *Head;
|
---|
873 | UINT8 *Ptr;
|
---|
874 |
|
---|
875 | Head = (UINT8 *) FtwHeader;
|
---|
876 | Ptr = (UINT8 *) FtwRecord;
|
---|
877 |
|
---|
878 | Head += sizeof (EFI_FAULT_TOLERANT_WRITE_HEADER);
|
---|
879 | return (BOOLEAN) (Head == Ptr);
|
---|
880 | }
|
---|
881 |
|
---|
882 | /**
|
---|
883 | To check if FtwRecord is the last record of FtwHeader. Because the
|
---|
884 | FtwHeader has NumberOfWrites & PrivateDataSize, the FtwRecord can be
|
---|
885 | determined if it is the last record of FtwHeader.
|
---|
886 |
|
---|
887 | @param FtwHeader Pointer to the write record header
|
---|
888 | @param FtwRecord Pointer to the write record
|
---|
889 |
|
---|
890 | @retval TRUE FtwRecord is the last Record of the FtwHeader
|
---|
891 | @retval FALSE FtwRecord is not the last Record of the FtwHeader
|
---|
892 |
|
---|
893 | **/
|
---|
894 | BOOLEAN
|
---|
895 | IsLastRecordOfWrites (
|
---|
896 | IN EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader,
|
---|
897 | IN EFI_FAULT_TOLERANT_WRITE_RECORD *FtwRecord
|
---|
898 | )
|
---|
899 | {
|
---|
900 | UINT8 *Head;
|
---|
901 | UINT8 *Ptr;
|
---|
902 |
|
---|
903 | Head = (UINT8 *) FtwHeader;
|
---|
904 | Ptr = (UINT8 *) FtwRecord;
|
---|
905 |
|
---|
906 | Head += FTW_WRITE_TOTAL_SIZE (FtwHeader->NumberOfWrites - 1, FtwHeader->PrivateDataSize);
|
---|
907 | return (BOOLEAN) (Head == Ptr);
|
---|
908 | }
|
---|
909 |
|
---|
910 | /**
|
---|
911 | To check if FtwRecord is the first record of FtwHeader.
|
---|
912 |
|
---|
913 | @param FtwHeader Pointer to the write record header
|
---|
914 | @param FtwRecord Pointer to retrieve the previous write record
|
---|
915 |
|
---|
916 | @retval EFI_ACCESS_DENIED Input record is the first record, no previous record is return.
|
---|
917 | @retval EFI_SUCCESS The previous write record is found.
|
---|
918 |
|
---|
919 | **/
|
---|
920 | EFI_STATUS
|
---|
921 | GetPreviousRecordOfWrites (
|
---|
922 | IN EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader,
|
---|
923 | IN OUT EFI_FAULT_TOLERANT_WRITE_RECORD **FtwRecord
|
---|
924 | )
|
---|
925 | {
|
---|
926 | UINT8 *Ptr;
|
---|
927 |
|
---|
928 | if (IsFirstRecordOfWrites (FtwHeader, *FtwRecord)) {
|
---|
929 | *FtwRecord = NULL;
|
---|
930 | return EFI_ACCESS_DENIED;
|
---|
931 | }
|
---|
932 |
|
---|
933 | Ptr = (UINT8 *) (*FtwRecord);
|
---|
934 | Ptr -= FTW_RECORD_SIZE (FtwHeader->PrivateDataSize);
|
---|
935 | *FtwRecord = (EFI_FAULT_TOLERANT_WRITE_RECORD *) Ptr;
|
---|
936 | return EFI_SUCCESS;
|
---|
937 | }
|
---|
938 |
|
---|
939 | /**
|
---|
940 | Allocate private data for FTW driver and initialize it.
|
---|
941 |
|
---|
942 | @param[out] FtwData Pointer to the FTW device structure
|
---|
943 |
|
---|
944 | @retval EFI_SUCCESS Initialize the FTW device successfully.
|
---|
945 | @retval EFI_OUT_OF_RESOURCES Allocate memory error
|
---|
946 | @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist
|
---|
947 |
|
---|
948 | **/
|
---|
949 | EFI_STATUS
|
---|
950 | InitFtwDevice (
|
---|
951 | OUT EFI_FTW_DEVICE **FtwData
|
---|
952 | )
|
---|
953 | {
|
---|
954 | EFI_FTW_DEVICE *FtwDevice;
|
---|
955 |
|
---|
956 | //
|
---|
957 | // Allocate private data of this driver,
|
---|
958 | // Including the FtwWorkSpace[FTW_WORK_SPACE_SIZE].
|
---|
959 | //
|
---|
960 | FtwDevice = AllocateZeroPool (sizeof (EFI_FTW_DEVICE) + PcdGet32 (PcdFlashNvStorageFtwWorkingSize));
|
---|
961 | if (FtwDevice == NULL) {
|
---|
962 | return EFI_OUT_OF_RESOURCES;
|
---|
963 | }
|
---|
964 |
|
---|
965 | //
|
---|
966 | // Initialize other parameters, and set WorkSpace as FTW_ERASED_BYTE.
|
---|
967 | //
|
---|
968 | FtwDevice->WorkSpaceLength = (UINTN) PcdGet32 (PcdFlashNvStorageFtwWorkingSize);
|
---|
969 | FtwDevice->SpareAreaLength = (UINTN) PcdGet32 (PcdFlashNvStorageFtwSpareSize);
|
---|
970 | if ((FtwDevice->WorkSpaceLength == 0) || (FtwDevice->SpareAreaLength == 0)) {
|
---|
971 | DEBUG ((EFI_D_ERROR, "Ftw: Workspace or Spare block does not exist!\n"));
|
---|
972 | FreePool (FtwDevice);
|
---|
973 | return EFI_INVALID_PARAMETER;
|
---|
974 | }
|
---|
975 |
|
---|
976 | FtwDevice->Signature = FTW_DEVICE_SIGNATURE;
|
---|
977 | FtwDevice->FtwFvBlock = NULL;
|
---|
978 | FtwDevice->FtwBackupFvb = NULL;
|
---|
979 | FtwDevice->FtwWorkSpaceLba = (EFI_LBA) (-1);
|
---|
980 | FtwDevice->FtwSpareLba = (EFI_LBA) (-1);
|
---|
981 |
|
---|
982 | FtwDevice->WorkSpaceAddress = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageFtwWorkingBase64);
|
---|
983 | if (FtwDevice->WorkSpaceAddress == 0) {
|
---|
984 | FtwDevice->WorkSpaceAddress = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageFtwWorkingBase);
|
---|
985 | }
|
---|
986 |
|
---|
987 | FtwDevice->SpareAreaAddress = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageFtwSpareBase64);
|
---|
988 | if (FtwDevice->SpareAreaAddress == 0) {
|
---|
989 | FtwDevice->SpareAreaAddress = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageFtwSpareBase);
|
---|
990 | }
|
---|
991 |
|
---|
992 | *FtwData = FtwDevice;
|
---|
993 | return EFI_SUCCESS;
|
---|
994 | }
|
---|
995 |
|
---|
996 |
|
---|
997 | /**
|
---|
998 | Find the proper Firmware Volume Block protocol for FTW operation.
|
---|
999 |
|
---|
1000 | @param[in, out] FtwDevice Pointer to the FTW device structure
|
---|
1001 |
|
---|
1002 | @retval EFI_SUCCESS Find the FVB protocol successfully.
|
---|
1003 | @retval EFI_NOT_FOUND No proper FVB protocol was found.
|
---|
1004 | @retval EFI_ABORTED Some data can not be got or be invalid.
|
---|
1005 |
|
---|
1006 | **/
|
---|
1007 | EFI_STATUS
|
---|
1008 | FindFvbForFtw (
|
---|
1009 | IN OUT EFI_FTW_DEVICE *FtwDevice
|
---|
1010 | )
|
---|
1011 | {
|
---|
1012 | EFI_STATUS Status;
|
---|
1013 | EFI_HANDLE *HandleBuffer;
|
---|
1014 | UINTN HandleCount;
|
---|
1015 | UINTN Index;
|
---|
1016 | EFI_PHYSICAL_ADDRESS FvbBaseAddress;
|
---|
1017 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
|
---|
1018 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
|
---|
1019 | EFI_FVB_ATTRIBUTES_2 Attributes;
|
---|
1020 | EFI_FV_BLOCK_MAP_ENTRY *FvbMapEntry;
|
---|
1021 | UINT32 LbaIndex;
|
---|
1022 |
|
---|
1023 | HandleBuffer = NULL;
|
---|
1024 |
|
---|
1025 | //
|
---|
1026 | // Get all FVB handle.
|
---|
1027 | //
|
---|
1028 | Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);
|
---|
1029 | if (EFI_ERROR (Status)) {
|
---|
1030 | return EFI_NOT_FOUND;
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 | //
|
---|
1034 | // Get the FVB to access variable store
|
---|
1035 | //
|
---|
1036 | Fvb = NULL;
|
---|
1037 | for (Index = 0; Index < HandleCount; Index += 1) {
|
---|
1038 | Status = FtwGetFvbByHandle (HandleBuffer[Index], &Fvb);
|
---|
1039 | if (EFI_ERROR (Status)) {
|
---|
1040 | Status = EFI_NOT_FOUND;
|
---|
1041 | break;
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | //
|
---|
1045 | // Ensure this FVB protocol support Write operation.
|
---|
1046 | //
|
---|
1047 | Status = Fvb->GetAttributes (Fvb, &Attributes);
|
---|
1048 | if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {
|
---|
1049 | continue;
|
---|
1050 | }
|
---|
1051 | //
|
---|
1052 | // Compare the address and select the right one
|
---|
1053 | //
|
---|
1054 | Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);
|
---|
1055 | if (EFI_ERROR (Status)) {
|
---|
1056 | continue;
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);
|
---|
1060 | if ((FtwDevice->FtwFvBlock == NULL) && (FtwDevice->WorkSpaceAddress >= FvbBaseAddress) &&
|
---|
1061 | ((FtwDevice->WorkSpaceAddress + FtwDevice->WorkSpaceLength) <= (FvbBaseAddress + FwVolHeader->FvLength))
|
---|
1062 | ) {
|
---|
1063 | FtwDevice->FtwFvBlock = Fvb;
|
---|
1064 | //
|
---|
1065 | // To get the LBA of work space
|
---|
1066 | //
|
---|
1067 | if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {
|
---|
1068 | //
|
---|
1069 | // Now, one FV has one type of BlockLength
|
---|
1070 | //
|
---|
1071 | FvbMapEntry = &FwVolHeader->BlockMap[0];
|
---|
1072 | for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {
|
---|
1073 | if ((FtwDevice->WorkSpaceAddress >= (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)))
|
---|
1074 | && (FtwDevice->WorkSpaceAddress < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex))) {
|
---|
1075 | FtwDevice->FtwWorkSpaceLba = LbaIndex - 1;
|
---|
1076 | //
|
---|
1077 | // Get the Work space size and Base(Offset)
|
---|
1078 | //
|
---|
1079 | FtwDevice->FtwWorkSpaceSize = FtwDevice->WorkSpaceLength;
|
---|
1080 | FtwDevice->FtwWorkSpaceBase = (UINTN) (FtwDevice->WorkSpaceAddress - (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));
|
---|
1081 | break;
|
---|
1082 | }
|
---|
1083 | }
|
---|
1084 | }
|
---|
1085 | }
|
---|
1086 |
|
---|
1087 | if ((FtwDevice->FtwBackupFvb == NULL) && (FtwDevice->SpareAreaAddress >= FvbBaseAddress) &&
|
---|
1088 | ((FtwDevice->SpareAreaAddress + FtwDevice->SpareAreaLength) <= (FvbBaseAddress + FwVolHeader->FvLength))
|
---|
1089 | ) {
|
---|
1090 | FtwDevice->FtwBackupFvb = Fvb;
|
---|
1091 | //
|
---|
1092 | // To get the LBA of spare
|
---|
1093 | //
|
---|
1094 | if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {
|
---|
1095 | //
|
---|
1096 | // Now, one FV has one type of BlockLength
|
---|
1097 | //
|
---|
1098 | FvbMapEntry = &FwVolHeader->BlockMap[0];
|
---|
1099 | for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {
|
---|
1100 | if ((FtwDevice->SpareAreaAddress >= (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)))
|
---|
1101 | && (FtwDevice->SpareAreaAddress < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex))) {
|
---|
1102 | //
|
---|
1103 | // Get the NumberOfSpareBlock and BlockSize
|
---|
1104 | //
|
---|
1105 | FtwDevice->FtwSpareLba = LbaIndex - 1;
|
---|
1106 | FtwDevice->BlockSize = FvbMapEntry->Length;
|
---|
1107 | FtwDevice->NumberOfSpareBlock = FtwDevice->SpareAreaLength / FtwDevice->BlockSize;
|
---|
1108 | //
|
---|
1109 | // Check the range of spare area to make sure that it's in FV range
|
---|
1110 | //
|
---|
1111 | if ((FtwDevice->FtwSpareLba + FtwDevice->NumberOfSpareBlock) > FvbMapEntry->NumBlocks) {
|
---|
1112 | DEBUG ((EFI_D_ERROR, "Ftw: Spare area is out of FV range\n"));
|
---|
1113 | FreePool (HandleBuffer);
|
---|
1114 | ASSERT (FALSE);
|
---|
1115 | return EFI_ABORTED;
|
---|
1116 | }
|
---|
1117 | //
|
---|
1118 | // Check the alignment of spare area address and length, they should be block size aligned
|
---|
1119 | //
|
---|
1120 | if (((FtwDevice->SpareAreaAddress & (FtwDevice->BlockSize - 1)) != 0) ||
|
---|
1121 | ((FtwDevice->SpareAreaLength & (FtwDevice->BlockSize - 1)) != 0)) {
|
---|
1122 | DEBUG ((EFI_D_ERROR, "Ftw: Spare area address or length is not block size aligned\n"));
|
---|
1123 | FreePool (HandleBuffer);
|
---|
1124 | //
|
---|
1125 | // Report Status Code EFI_SW_EC_ABORTED.
|
---|
1126 | //
|
---|
1127 | REPORT_STATUS_CODE ( (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED), (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ABORTED));
|
---|
1128 | ASSERT (FALSE);
|
---|
1129 | CpuDeadLoop ();
|
---|
1130 | }
|
---|
1131 | break;
|
---|
1132 | }
|
---|
1133 | }
|
---|
1134 | }
|
---|
1135 | }
|
---|
1136 | }
|
---|
1137 | FreePool (HandleBuffer);
|
---|
1138 |
|
---|
1139 | if ((FtwDevice->FtwBackupFvb == NULL) || (FtwDevice->FtwFvBlock == NULL) ||
|
---|
1140 | (FtwDevice->FtwWorkSpaceLba == (EFI_LBA) (-1)) || (FtwDevice->FtwSpareLba == (EFI_LBA) (-1))) {
|
---|
1141 | return EFI_ABORTED;
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 | return EFI_SUCCESS;
|
---|
1145 | }
|
---|
1146 |
|
---|
1147 |
|
---|
1148 | /**
|
---|
1149 | Initialization for Fault Tolerant Write protocol.
|
---|
1150 |
|
---|
1151 | @param[in, out] FtwDevice Pointer to the FTW device structure
|
---|
1152 |
|
---|
1153 | @retval EFI_SUCCESS Initialize the FTW protocol successfully.
|
---|
1154 | @retval EFI_NOT_FOUND No proper FVB protocol was found.
|
---|
1155 |
|
---|
1156 | **/
|
---|
1157 | EFI_STATUS
|
---|
1158 | InitFtwProtocol (
|
---|
1159 | IN OUT EFI_FTW_DEVICE *FtwDevice
|
---|
1160 | )
|
---|
1161 | {
|
---|
1162 | EFI_STATUS Status;
|
---|
1163 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
|
---|
1164 | UINTN Length;
|
---|
1165 | EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader;
|
---|
1166 | UINTN Offset;
|
---|
1167 | EFI_HANDLE FvbHandle;
|
---|
1168 | EFI_LBA WorkSpaceLbaOffset;
|
---|
1169 |
|
---|
1170 | //
|
---|
1171 | // Find the right SMM Fvb protocol instance for FTW.
|
---|
1172 | //
|
---|
1173 | Status = FindFvbForFtw (FtwDevice);
|
---|
1174 | if (EFI_ERROR (Status)) {
|
---|
1175 | return EFI_NOT_FOUND;
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 | //
|
---|
1179 | // Calculate the start LBA of working block. Working block is an area which
|
---|
1180 | // contains working space in its last block and has the same size as spare
|
---|
1181 | // block, unless there are not enough blocks before the block that contains
|
---|
1182 | // working space.
|
---|
1183 | //
|
---|
1184 | FtwDevice->FtwWorkBlockLba = FtwDevice->FtwWorkSpaceLba - FtwDevice->NumberOfSpareBlock + 1;
|
---|
1185 | ASSERT ((INT64) (FtwDevice->FtwWorkBlockLba) >= 0);
|
---|
1186 |
|
---|
1187 | //
|
---|
1188 | // Initialize other parameters, and set WorkSpace as FTW_ERASED_BYTE.
|
---|
1189 | //
|
---|
1190 | FtwDevice->FtwWorkSpace = (UINT8 *) (FtwDevice + 1);
|
---|
1191 | FtwDevice->FtwWorkSpaceHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) FtwDevice->FtwWorkSpace;
|
---|
1192 |
|
---|
1193 | FtwDevice->FtwLastWriteHeader = NULL;
|
---|
1194 | FtwDevice->FtwLastWriteRecord = NULL;
|
---|
1195 |
|
---|
1196 | InitializeLocalWorkSpaceHeader ();
|
---|
1197 |
|
---|
1198 | //
|
---|
1199 | // Refresh the working space data from working block
|
---|
1200 | //
|
---|
1201 | Status = WorkSpaceRefresh (FtwDevice);
|
---|
1202 | ASSERT_EFI_ERROR (Status);
|
---|
1203 | //
|
---|
1204 | // If the working block workspace is not valid, try the spare block
|
---|
1205 | //
|
---|
1206 | if (!IsValidWorkSpace (FtwDevice->FtwWorkSpaceHeader)) {
|
---|
1207 | //
|
---|
1208 | // Read from spare block
|
---|
1209 | //
|
---|
1210 | WorkSpaceLbaOffset = FtwDevice->FtwWorkSpaceLba - FtwDevice->FtwWorkBlockLba;
|
---|
1211 | Length = FtwDevice->FtwWorkSpaceSize;
|
---|
1212 | Status = FtwDevice->FtwBackupFvb->Read (
|
---|
1213 | FtwDevice->FtwBackupFvb,
|
---|
1214 | FtwDevice->FtwSpareLba + WorkSpaceLbaOffset,
|
---|
1215 | FtwDevice->FtwWorkSpaceBase,
|
---|
1216 | &Length,
|
---|
1217 | FtwDevice->FtwWorkSpace
|
---|
1218 | );
|
---|
1219 | ASSERT_EFI_ERROR (Status);
|
---|
1220 |
|
---|
1221 | //
|
---|
1222 | // If spare block is valid, then replace working block content.
|
---|
1223 | //
|
---|
1224 | if (IsValidWorkSpace (FtwDevice->FtwWorkSpaceHeader)) {
|
---|
1225 | Status = FlushSpareBlockToWorkingBlock (FtwDevice);
|
---|
1226 | DEBUG ((EFI_D_ERROR, "Ftw: Restart working block update in InitFtwProtocol() - %r\n", Status));
|
---|
1227 | FtwAbort (&FtwDevice->FtwInstance);
|
---|
1228 | //
|
---|
1229 | // Refresh work space.
|
---|
1230 | //
|
---|
1231 | Status = WorkSpaceRefresh (FtwDevice);
|
---|
1232 | ASSERT_EFI_ERROR (Status);
|
---|
1233 | } else {
|
---|
1234 | DEBUG ((EFI_D_ERROR, "Ftw: Both are invalid, init workspace\n"));
|
---|
1235 | //
|
---|
1236 | // If both are invalid, then initialize work space.
|
---|
1237 | //
|
---|
1238 | SetMem (
|
---|
1239 | FtwDevice->FtwWorkSpace,
|
---|
1240 | FtwDevice->FtwWorkSpaceSize,
|
---|
1241 | FTW_ERASED_BYTE
|
---|
1242 | );
|
---|
1243 | InitWorkSpaceHeader (FtwDevice->FtwWorkSpaceHeader);
|
---|
1244 | //
|
---|
1245 | // Initialize the work space
|
---|
1246 | //
|
---|
1247 | Status = FtwReclaimWorkSpace (FtwDevice, FALSE);
|
---|
1248 | ASSERT_EFI_ERROR (Status);
|
---|
1249 | }
|
---|
1250 | }
|
---|
1251 | //
|
---|
1252 | // If the FtwDevice->FtwLastWriteRecord is 1st record of write header &&
|
---|
1253 | // (! SpareComplete) THEN call Abort().
|
---|
1254 | //
|
---|
1255 | if ((FtwDevice->FtwLastWriteHeader->HeaderAllocated == FTW_VALID_STATE) &&
|
---|
1256 | (FtwDevice->FtwLastWriteRecord->SpareComplete != FTW_VALID_STATE) &&
|
---|
1257 | IsFirstRecordOfWrites (FtwDevice->FtwLastWriteHeader, FtwDevice->FtwLastWriteRecord)
|
---|
1258 | ) {
|
---|
1259 | DEBUG ((EFI_D_ERROR, "Ftw: Init.. find first record not SpareCompleted, abort()\n"));
|
---|
1260 | FtwAbort (&FtwDevice->FtwInstance);
|
---|
1261 | }
|
---|
1262 | //
|
---|
1263 | // If Header is incompleted and the last record has completed, then
|
---|
1264 | // call Abort() to set the Header->Complete FLAG.
|
---|
1265 | //
|
---|
1266 | if ((FtwDevice->FtwLastWriteHeader->Complete != FTW_VALID_STATE) &&
|
---|
1267 | (FtwDevice->FtwLastWriteRecord->DestinationComplete == FTW_VALID_STATE) &&
|
---|
1268 | IsLastRecordOfWrites (FtwDevice->FtwLastWriteHeader, FtwDevice->FtwLastWriteRecord)
|
---|
1269 | ) {
|
---|
1270 | DEBUG ((EFI_D_ERROR, "Ftw: Init.. find last record completed but header not, abort()\n"));
|
---|
1271 | FtwAbort (&FtwDevice->FtwInstance);
|
---|
1272 | }
|
---|
1273 | //
|
---|
1274 | // To check the workspace buffer following last Write header/records is EMPTY or not.
|
---|
1275 | // If it's not EMPTY, FTW also need to call reclaim().
|
---|
1276 | //
|
---|
1277 | FtwHeader = FtwDevice->FtwLastWriteHeader;
|
---|
1278 | Offset = (UINT8 *) FtwHeader - FtwDevice->FtwWorkSpace;
|
---|
1279 | if (FtwDevice->FtwWorkSpace[Offset] != FTW_ERASED_BYTE) {
|
---|
1280 | Offset += FTW_WRITE_TOTAL_SIZE (FtwHeader->NumberOfWrites, FtwHeader->PrivateDataSize);
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | if (!IsErasedFlashBuffer (FtwDevice->FtwWorkSpace + Offset, FtwDevice->FtwWorkSpaceSize - Offset)) {
|
---|
1284 | Status = FtwReclaimWorkSpace (FtwDevice, TRUE);
|
---|
1285 | ASSERT_EFI_ERROR (Status);
|
---|
1286 | }
|
---|
1287 |
|
---|
1288 | //
|
---|
1289 | // Restart if it's boot block
|
---|
1290 | //
|
---|
1291 | if ((FtwDevice->FtwLastWriteHeader->Complete != FTW_VALID_STATE) &&
|
---|
1292 | (FtwDevice->FtwLastWriteRecord->SpareComplete == FTW_VALID_STATE)
|
---|
1293 | ) {
|
---|
1294 | if (FtwDevice->FtwLastWriteRecord->BootBlockUpdate == FTW_VALID_STATE) {
|
---|
1295 | Status = FlushSpareBlockToBootBlock (FtwDevice);
|
---|
1296 | DEBUG ((EFI_D_ERROR, "Ftw: Restart boot block update - %r\n", Status));
|
---|
1297 | ASSERT_EFI_ERROR (Status);
|
---|
1298 | FtwAbort (&FtwDevice->FtwInstance);
|
---|
1299 | } else {
|
---|
1300 | //
|
---|
1301 | // if (SpareCompleted) THEN Restart to fault tolerant write.
|
---|
1302 | //
|
---|
1303 | FvbHandle = NULL;
|
---|
1304 | FvbHandle = GetFvbByAddress ((EFI_PHYSICAL_ADDRESS) (UINTN) ((INT64) FtwDevice->SpareAreaAddress + FtwDevice->FtwLastWriteRecord->RelativeOffset), &Fvb);
|
---|
1305 | if (FvbHandle != NULL) {
|
---|
1306 | Status = FtwRestart (&FtwDevice->FtwInstance, FvbHandle);
|
---|
1307 | DEBUG ((EFI_D_ERROR, "FtwLite: Restart last write - %r\n", Status));
|
---|
1308 | ASSERT_EFI_ERROR (Status);
|
---|
1309 | }
|
---|
1310 | FtwAbort (&FtwDevice->FtwInstance);
|
---|
1311 | }
|
---|
1312 | }
|
---|
1313 | //
|
---|
1314 | // Hook the protocol API
|
---|
1315 | //
|
---|
1316 | FtwDevice->FtwInstance.GetMaxBlockSize = FtwGetMaxBlockSize;
|
---|
1317 | FtwDevice->FtwInstance.Allocate = FtwAllocate;
|
---|
1318 | FtwDevice->FtwInstance.Write = FtwWrite;
|
---|
1319 | FtwDevice->FtwInstance.Restart = FtwRestart;
|
---|
1320 | FtwDevice->FtwInstance.Abort = FtwAbort;
|
---|
1321 | FtwDevice->FtwInstance.GetLastWrite = FtwGetLastWrite;
|
---|
1322 |
|
---|
1323 | return EFI_SUCCESS;
|
---|
1324 | }
|
---|
1325 |
|
---|