]>
Commit | Line | Data |
---|---|---|
9071550e | 1 | /**@file\r |
2 | Copyright (c) 2007, Intel Corporation \r | |
3 | All rights reserved. This program and the accompanying materials \r | |
4 | are licensed and made available under the terms and conditions of the BSD License \r | |
5 | which accompanies this distribution. The full text of the license may be found at \r | |
6 | http://opensource.org/licenses/bsd-license.php \r | |
7 | \r | |
8 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
9 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
10 | \r | |
11 | Module Name:\r | |
12 | \r | |
13 | FWBlockService.c\r | |
14 | \r | |
15 | Abstract:\r | |
16 | \r | |
17 | Revision History\r | |
18 | \r | |
19 | **/\r | |
20 | #include "FWBlockService.h"\r | |
21 | #include "EfiFlashMap.h"\r | |
22 | #include "FileIo.h"\r | |
23 | #include "FlashLayout.h"\r | |
24 | \r | |
25 | ESAL_FWB_GLOBAL *mFvbModuleGlobal;\r | |
26 | VOID *mSFSRegistration;\r | |
27 | #define TRY_ASSIGN(var, value) if(var != NULL) {*var = value;}\r | |
28 | \r | |
29 | EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate = {\r | |
30 | FVB_DEVICE_SIGNATURE,\r | |
31 | {\r | |
32 | {\r | |
33 | {\r | |
34 | HARDWARE_DEVICE_PATH,\r | |
35 | HW_MEMMAP_DP,\r | |
36 | {\r | |
37 | sizeof (MEMMAP_DEVICE_PATH),\r | |
38 | 0\r | |
39 | }\r | |
40 | },\r | |
41 | EfiMemoryMappedIO,\r | |
42 | 0,\r | |
43 | 0,\r | |
44 | },\r | |
45 | {\r | |
46 | END_DEVICE_PATH_TYPE,\r | |
47 | END_ENTIRE_DEVICE_PATH_SUBTYPE,\r | |
48 | {\r | |
49 | sizeof (EFI_DEVICE_PATH_PROTOCOL),\r | |
50 | 0\r | |
51 | }\r | |
52 | }\r | |
53 | },\r | |
54 | 0,\r | |
55 | {\r | |
56 | FvbProtocolGetAttributes,\r | |
57 | FvbProtocolSetAttributes,\r | |
58 | FvbProtocolGetPhysicalAddress,\r | |
59 | FvbProtocolGetBlockSize,\r | |
60 | FvbProtocolRead,\r | |
61 | FvbProtocolWrite,\r | |
62 | FvbProtocolEraseBlocks,\r | |
63 | NULL\r | |
64 | },\r | |
65 | {\r | |
66 | FvbExtendProtocolEraseCustomBlockRange\r | |
67 | }\r | |
68 | };\r | |
69 | \r | |
70 | \r | |
71 | EFI_STATUS\r | |
72 | FlashFdWrite (\r | |
73 | IN UINTN Address,\r | |
74 | IN EFI_FW_VOL_INSTANCE *FwhInstance,\r | |
75 | IN OUT UINTN *NumBytes,\r | |
76 | IN UINT8 *Buffer\r | |
77 | )\r | |
78 | /*++\r | |
79 | \r | |
80 | Routine Description:\r | |
81 | Writes specified number of bytes from the input buffer to the address\r | |
82 | \r | |
83 | Arguments:\r | |
84 | \r | |
85 | Returns: \r | |
86 | \r | |
87 | --*/\r | |
88 | {\r | |
89 | EFI_STATUS Status;\r | |
90 | EFI_FILE *File;\r | |
91 | UINTN FileOffset;\r | |
92 | UINTN BufferForFile;\r | |
93 | UINTN Length;\r | |
94 | \r | |
95 | Status = EFI_SUCCESS;\r | |
96 | CopyMem ((VOID *) Address, Buffer, *NumBytes);\r | |
97 | \r | |
98 | if (!EfiAtRuntime () && (FwhInstance->Device != NULL)) {\r | |
99 | Status = FileOpen (FwhInstance->Device, FwhInstance->MappedFile, &File, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE);\r | |
100 | ASSERT_EFI_ERROR (Status);\r | |
101 | if (!EFI_ERROR (Status)) {\r | |
102 | if (Address - FwhInstance->FvBase[FVB_PHYSICAL] < FwhInstance->Offset) {\r | |
103 | FileOffset = 0;\r | |
104 | BufferForFile = FwhInstance->FvBase[FVB_PHYSICAL] + FwhInstance->Offset;\r | |
105 | Length = *NumBytes - (FwhInstance->Offset - (Address - FwhInstance->FvBase[FVB_PHYSICAL]));\r | |
106 | } else {\r | |
107 | FileOffset = Address - FwhInstance->FvBase[FVB_PHYSICAL] - FwhInstance->Offset;\r | |
108 | BufferForFile = Address;\r | |
109 | Length = *NumBytes;\r | |
110 | }\r | |
111 | \r | |
112 | Status = FileWrite (File, FileOffset, BufferForFile, Length);\r | |
113 | ASSERT_EFI_ERROR (Status);\r | |
114 | FileClose (File);\r | |
115 | }\r | |
116 | }\r | |
117 | return Status;\r | |
118 | }\r | |
119 | \r | |
120 | EFI_STATUS\r | |
121 | FlashFdErase (\r | |
122 | IN UINTN Address,\r | |
123 | IN EFI_FW_VOL_INSTANCE *FwhInstance,\r | |
124 | IN UINTN LbaLength\r | |
125 | )\r | |
126 | /*++\r | |
127 | \r | |
128 | Routine Description:\r | |
129 | Erase a certain block from address LbaWriteAddress\r | |
130 | \r | |
131 | Arguments:\r | |
132 | \r | |
133 | Returns: \r | |
134 | \r | |
135 | --*/\r | |
136 | {\r | |
137 | EFI_STATUS Status;\r | |
138 | EFI_FILE *File;\r | |
139 | UINTN FileOffset;\r | |
140 | UINTN BufferForFile;\r | |
141 | UINTN Length;\r | |
142 | \r | |
143 | Status = EFI_SUCCESS;\r | |
144 | \r | |
145 | SetMem ((VOID *)Address, LbaLength, 0xff);\r | |
146 | \r | |
147 | if (!EfiAtRuntime () && (FwhInstance->Device != NULL)) {\r | |
148 | Status = FileOpen (FwhInstance->Device, FwhInstance->MappedFile, &File, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE);\r | |
149 | ASSERT_EFI_ERROR (Status);\r | |
150 | if (!EFI_ERROR (Status)) {\r | |
151 | if (Address - FwhInstance->FvBase[FVB_PHYSICAL] < FwhInstance->Offset) {\r | |
152 | FileOffset = 0;\r | |
153 | BufferForFile = FwhInstance->FvBase[FVB_PHYSICAL] + FwhInstance->Offset;\r | |
154 | Length = LbaLength - (FwhInstance->Offset - (Address - FwhInstance->FvBase[FVB_PHYSICAL]));\r | |
155 | } else {\r | |
156 | FileOffset = Address - FwhInstance->FvBase[FVB_PHYSICAL] - FwhInstance->Offset;\r | |
157 | BufferForFile = Address;\r | |
158 | Length = LbaLength;\r | |
159 | }\r | |
160 | \r | |
161 | Status = FileWrite (File, FileOffset, BufferForFile, Length);\r | |
162 | ASSERT_EFI_ERROR (Status);\r | |
163 | FileClose (File);\r | |
164 | }\r | |
165 | }\r | |
166 | return Status;\r | |
167 | }\r | |
168 | \r | |
169 | VOID\r | |
170 | EFIAPI\r | |
171 | FvbVirtualddressChangeEvent (\r | |
172 | IN EFI_EVENT Event,\r | |
173 | IN VOID *Context\r | |
174 | )\r | |
175 | /*++\r | |
176 | \r | |
177 | Routine Description:\r | |
178 | \r | |
179 | Fixup internal data so that EFI and SAL can be call in virtual mode.\r | |
180 | Call the passed in Child Notify event and convert the mFvbModuleGlobal\r | |
181 | date items to there virtual address.\r | |
182 | \r | |
183 | mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] - Physical copy of instance data\r | |
184 | mFvbModuleGlobal->FvInstance[FVB_VIRTUAL] - Virtual pointer to common \r | |
185 | instance data.\r | |
186 | \r | |
187 | Arguments:\r | |
188 | \r | |
189 | (Standard EFI notify event - EFI_EVENT_NOTIFY)\r | |
190 | \r | |
191 | Returns: \r | |
192 | \r | |
193 | None\r | |
194 | \r | |
195 | --*/\r | |
196 | {\r | |
197 | EFI_FW_VOL_INSTANCE *FwhInstance;\r | |
198 | UINTN Index;\r | |
199 | \r | |
200 | EfiConvertPointer (0, (VOID **) &mFvbModuleGlobal->FvInstance[FVB_VIRTUAL]);\r | |
201 | \r | |
202 | //\r | |
203 | // Convert the base address of all the instances\r | |
204 | //\r | |
205 | Index = 0;\r | |
206 | FwhInstance = mFvbModuleGlobal->FvInstance[FVB_PHYSICAL];\r | |
207 | while (Index < mFvbModuleGlobal->NumFv) {\r | |
208 | EfiConvertPointer (0, (VOID **) &FwhInstance->FvBase[FVB_VIRTUAL]);\r | |
209 | FwhInstance = (EFI_FW_VOL_INSTANCE *) ((UINTN)((UINT8 *)FwhInstance) + FwhInstance->VolumeHeader.HeaderLength\r | |
210 | + (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER)));\r | |
211 | Index++;\r | |
212 | }\r | |
213 | \r | |
214 | EfiConvertPointer (0, (VOID **) &mFvbModuleGlobal->FvbScratchSpace[FVB_VIRTUAL]);\r | |
215 | EfiConvertPointer (0, (VOID **) &mFvbModuleGlobal);\r | |
216 | }\r | |
217 | \r | |
218 | EFI_STATUS\r | |
219 | GetFvbInstance (\r | |
220 | IN UINTN Instance,\r | |
221 | IN ESAL_FWB_GLOBAL *Global,\r | |
222 | OUT EFI_FW_VOL_INSTANCE **FwhInstance,\r | |
223 | IN BOOLEAN Virtual\r | |
224 | )\r | |
225 | /*++\r | |
226 | \r | |
227 | Routine Description:\r | |
228 | Retrieves the physical address of a memory mapped FV\r | |
229 | \r | |
230 | Arguments:\r | |
231 | Instance - The FV instance whose base address is going to be\r | |
232 | returned\r | |
233 | Global - Pointer to ESAL_FWB_GLOBAL that contains all\r | |
234 | instance data\r | |
235 | FwhInstance - The EFI_FW_VOL_INSTANCE fimrware instance structure\r | |
236 | Virtual - Whether CPU is in virtual or physical mode\r | |
237 | \r | |
238 | Returns: \r | |
239 | EFI_SUCCESS - Successfully returns\r | |
240 | EFI_INVALID_PARAMETER - Instance not found\r | |
241 | \r | |
242 | --*/\r | |
243 | {\r | |
244 | EFI_FW_VOL_INSTANCE *FwhRecord;\r | |
245 | \r | |
246 | if (Instance >= Global->NumFv) {\r | |
247 | return EFI_INVALID_PARAMETER;\r | |
248 | }\r | |
249 | //\r | |
250 | // Find the right instance of the FVB private data\r | |
251 | //\r | |
252 | FwhRecord = Global->FvInstance[Virtual];\r | |
253 | while (Instance > 0) {\r | |
254 | FwhRecord = (EFI_FW_VOL_INSTANCE *) ((UINTN)((UINT8 *)FwhRecord) + FwhRecord->VolumeHeader.HeaderLength \r | |
255 | + (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER)));\r | |
256 | Instance--;\r | |
257 | }\r | |
258 | \r | |
259 | *FwhInstance = FwhRecord;\r | |
260 | \r | |
261 | return EFI_SUCCESS;\r | |
262 | }\r | |
263 | \r | |
264 | EFI_STATUS\r | |
265 | FvbGetPhysicalAddress (\r | |
266 | IN UINTN Instance,\r | |
267 | OUT EFI_PHYSICAL_ADDRESS *Address,\r | |
268 | IN ESAL_FWB_GLOBAL *Global,\r | |
269 | IN BOOLEAN Virtual\r | |
270 | )\r | |
271 | /*++\r | |
272 | \r | |
273 | Routine Description:\r | |
274 | Retrieves the physical address of a memory mapped FV\r | |
275 | \r | |
276 | Arguments:\r | |
277 | Instance - The FV instance whose base address is going to be\r | |
278 | returned\r | |
279 | Address - Pointer to a caller allocated EFI_PHYSICAL_ADDRESS \r | |
280 | that on successful return, contains the base address\r | |
281 | of the firmware volume. \r | |
282 | Global - Pointer to ESAL_FWB_GLOBAL that contains all\r | |
283 | instance data\r | |
284 | Virtual - Whether CPU is in virtual or physical mode\r | |
285 | \r | |
286 | Returns: \r | |
287 | EFI_SUCCESS - Successfully returns\r | |
288 | EFI_INVALID_PARAMETER - Instance not found\r | |
289 | \r | |
290 | --*/\r | |
291 | {\r | |
292 | EFI_FW_VOL_INSTANCE *FwhInstance;\r | |
293 | EFI_STATUS Status;\r | |
294 | \r | |
295 | //\r | |
296 | // Find the right instance of the FVB private data\r | |
297 | //\r | |
298 | Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);\r | |
299 | ASSERT_EFI_ERROR (Status);\r | |
300 | *Address = FwhInstance->FvBase[Virtual];\r | |
301 | \r | |
302 | return EFI_SUCCESS;\r | |
303 | }\r | |
304 | \r | |
305 | EFI_STATUS\r | |
306 | FvbGetVolumeAttributes (\r | |
307 | IN UINTN Instance,\r | |
8ee3a199 | 308 | OUT EFI_FVB_ATTRIBUTES_2 *Attributes,\r |
9071550e | 309 | IN ESAL_FWB_GLOBAL *Global,\r |
310 | IN BOOLEAN Virtual\r | |
311 | )\r | |
312 | /*++\r | |
313 | \r | |
314 | Routine Description:\r | |
315 | Retrieves attributes, insures positive polarity of attribute bits, returns\r | |
316 | resulting attributes in output parameter\r | |
317 | \r | |
318 | Arguments:\r | |
319 | Instance - The FV instance whose attributes is going to be \r | |
320 | returned\r | |
321 | Attributes - Output buffer which contains attributes\r | |
322 | Global - Pointer to ESAL_FWB_GLOBAL that contains all\r | |
323 | instance data\r | |
324 | Virtual - Whether CPU is in virtual or physical mode\r | |
325 | \r | |
326 | Returns: \r | |
327 | EFI_SUCCESS - Successfully returns\r | |
328 | EFI_INVALID_PARAMETER - Instance not found\r | |
329 | \r | |
330 | --*/\r | |
331 | {\r | |
332 | EFI_FW_VOL_INSTANCE *FwhInstance;\r | |
333 | EFI_STATUS Status;\r | |
334 | \r | |
335 | //\r | |
336 | // Find the right instance of the FVB private data\r | |
337 | //\r | |
338 | Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);\r | |
339 | ASSERT_EFI_ERROR (Status);\r | |
340 | *Attributes = FwhInstance->VolumeHeader.Attributes;\r | |
341 | \r | |
342 | return EFI_SUCCESS;\r | |
343 | }\r | |
344 | \r | |
345 | EFI_STATUS\r | |
346 | FvbGetLbaAddress (\r | |
347 | IN UINTN Instance,\r | |
348 | IN EFI_LBA Lba,\r | |
349 | OUT UINTN *LbaAddress OPTIONAL,\r | |
350 | OUT UINTN *LbaLength OPTIONAL,\r | |
351 | OUT UINTN *NumOfBlocks OPTIONAL,\r | |
352 | IN ESAL_FWB_GLOBAL *Global,\r | |
353 | IN BOOLEAN Virtual\r | |
354 | )\r | |
355 | /*++\r | |
356 | \r | |
357 | Routine Description:\r | |
358 | Retrieves the starting address of an LBA in an FV\r | |
359 | \r | |
360 | Arguments:\r | |
361 | Instance - The FV instance which the Lba belongs to\r | |
362 | Lba - The logical block address\r | |
363 | LbaAddress - On output, contains the physical starting address\r | |
364 | of the Lba for writing\r | |
365 | LbaLength - On output, contains the length of the block\r | |
366 | NumOfBlocks - A pointer to a caller allocated UINTN in which the\r | |
367 | number of consecutive blocks starting with Lba is\r | |
368 | returned. All blocks in this range have a size of\r | |
369 | BlockSize\r | |
370 | Global - Pointer to ESAL_FWB_GLOBAL that contains all\r | |
371 | instance data\r | |
372 | Virtual - Whether CPU is in virtual or physical mode\r | |
373 | \r | |
374 | Returns: \r | |
375 | EFI_SUCCESS - Successfully returns\r | |
376 | EFI_INVALID_PARAMETER - Instance not found\r | |
377 | \r | |
378 | --*/\r | |
379 | {\r | |
380 | UINT32 NumBlocks;\r | |
381 | UINT32 BlockLength;\r | |
382 | UINTN Offset;\r | |
383 | EFI_LBA StartLba;\r | |
384 | EFI_LBA NextLba;\r | |
385 | EFI_FW_VOL_INSTANCE *FwhInstance;\r | |
386 | EFI_FV_BLOCK_MAP_ENTRY *BlockMap;\r | |
387 | EFI_STATUS Status;\r | |
388 | \r | |
389 | //\r | |
390 | // Find the right instance of the FVB private data\r | |
391 | //\r | |
392 | Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);\r | |
393 | ASSERT_EFI_ERROR (Status);\r | |
394 | \r | |
395 | StartLba = 0;\r | |
396 | Offset = 0;\r | |
397 | BlockMap = &(FwhInstance->VolumeHeader.BlockMap[0]);\r | |
398 | \r | |
399 | //\r | |
400 | // Parse the blockmap of the FV to find which map entry the Lba belongs to\r | |
401 | //\r | |
402 | while (TRUE) {\r | |
403 | NumBlocks = BlockMap->NumBlocks;\r | |
404 | BlockLength = BlockMap->Length;\r | |
405 | \r | |
406 | if (NumBlocks == 0 || BlockLength == 0) {\r | |
407 | return EFI_INVALID_PARAMETER;\r | |
408 | }\r | |
409 | \r | |
410 | NextLba = StartLba + NumBlocks;\r | |
411 | \r | |
412 | //\r | |
413 | // The map entry found\r | |
414 | //\r | |
415 | if (Lba >= StartLba && Lba < NextLba) {\r | |
416 | Offset = Offset + (UINTN) MultU64x32 ((Lba - StartLba), BlockLength);\r | |
417 | \r | |
418 | if (LbaAddress) {\r | |
419 | *LbaAddress = FwhInstance->FvBase[Virtual] + Offset;\r | |
420 | }\r | |
421 | \r | |
422 | if (LbaLength) {\r | |
423 | *LbaLength = BlockLength;\r | |
424 | }\r | |
425 | \r | |
426 | if (NumOfBlocks) {\r | |
427 | *NumOfBlocks = (UINTN) (NextLba - Lba);\r | |
428 | }\r | |
429 | \r | |
430 | return EFI_SUCCESS;\r | |
431 | }\r | |
432 | \r | |
433 | StartLba = NextLba;\r | |
434 | Offset = Offset + NumBlocks * BlockLength;\r | |
435 | BlockMap++;\r | |
436 | }\r | |
437 | }\r | |
438 | \r | |
439 | EFI_STATUS\r | |
440 | FvbReadBlock (\r | |
441 | IN UINTN Instance,\r | |
442 | IN EFI_LBA Lba,\r | |
443 | IN UINTN BlockOffset,\r | |
444 | IN OUT UINTN *NumBytes,\r | |
445 | IN UINT8 *Buffer,\r | |
446 | IN ESAL_FWB_GLOBAL *Global,\r | |
447 | IN BOOLEAN Virtual\r | |
448 | )\r | |
449 | /*++\r | |
450 | \r | |
451 | Routine Description:\r | |
452 | Reads specified number of bytes into a buffer from the specified block\r | |
453 | \r | |
454 | Arguments:\r | |
455 | Instance - The FV instance to be read from\r | |
456 | Lba - The logical block address to be read from\r | |
457 | BlockOffset - Offset into the block at which to begin reading\r | |
458 | NumBytes - Pointer that on input contains the total size of\r | |
459 | the buffer. On output, it contains the total number\r | |
460 | of bytes read\r | |
461 | Buffer - Pointer to a caller allocated buffer that will be\r | |
462 | used to hold the data read\r | |
463 | Global - Pointer to ESAL_FWB_GLOBAL that contains all\r | |
464 | instance data\r | |
465 | Virtual - Whether CPU is in virtual or physical mode\r | |
466 | \r | |
467 | Returns: \r | |
468 | EFI_SUCCESS - The firmware volume was read successfully and \r | |
469 | contents are in Buffer\r | |
470 | EFI_BAD_BUFFER_SIZE - Read attempted across a LBA boundary. On output,\r | |
471 | NumBytes contains the total number of bytes returned\r | |
472 | in Buffer\r | |
473 | EFI_ACCESS_DENIED - The firmware volume is in the ReadDisabled state\r | |
474 | EFI_DEVICE_ERROR - The block device is not functioning correctly and \r | |
475 | could not be read\r | |
476 | EFI_INVALID_PARAMETER - Instance not found, or NumBytes, Buffer are NULL\r | |
477 | \r | |
478 | --*/\r | |
479 | {\r | |
8ee3a199 | 480 | EFI_FVB_ATTRIBUTES_2 Attributes;\r |
481 | UINTN LbaAddress;\r | |
482 | UINTN LbaLength;\r | |
483 | EFI_STATUS Status;\r | |
9071550e | 484 | \r |
485 | //\r | |
486 | // Check for invalid conditions\r | |
487 | //\r | |
488 | if ((NumBytes == NULL) || (Buffer == NULL)) {\r | |
489 | return EFI_INVALID_PARAMETER;\r | |
490 | }\r | |
491 | \r | |
492 | if (*NumBytes == 0) {\r | |
493 | return EFI_INVALID_PARAMETER;\r | |
494 | }\r | |
495 | \r | |
496 | Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL, Global, Virtual);\r | |
497 | if (EFI_ERROR (Status)) {\r | |
498 | return Status;\r | |
499 | }\r | |
500 | //\r | |
501 | // Check if the FV is read enabled\r | |
502 | //\r | |
503 | FvbGetVolumeAttributes (Instance, &Attributes, Global, Virtual);\r | |
504 | \r | |
505 | if ((Attributes & EFI_FVB2_READ_STATUS) == 0) {\r | |
506 | return EFI_ACCESS_DENIED;\r | |
507 | }\r | |
508 | //\r | |
509 | // Perform boundary checks and adjust NumBytes\r | |
510 | //\r | |
511 | if (BlockOffset > LbaLength) {\r | |
512 | return EFI_INVALID_PARAMETER;\r | |
513 | }\r | |
514 | \r | |
515 | if (LbaLength < (*NumBytes + BlockOffset)) {\r | |
516 | *NumBytes = (UINT32) (LbaLength - BlockOffset);\r | |
517 | Status = EFI_BAD_BUFFER_SIZE;\r | |
518 | }\r | |
519 | \r | |
520 | CopyMem (Buffer, (VOID *) (LbaAddress + BlockOffset), (UINTN) *NumBytes);\r | |
521 | \r | |
522 | return Status;\r | |
523 | }\r | |
524 | EFI_STATUS\r | |
525 | FvbWriteBlock (\r | |
526 | IN UINTN Instance,\r | |
527 | IN EFI_LBA Lba,\r | |
528 | IN UINTN BlockOffset,\r | |
529 | IN OUT UINTN *NumBytes,\r | |
530 | IN UINT8 *Buffer,\r | |
531 | IN ESAL_FWB_GLOBAL *Global,\r | |
532 | IN BOOLEAN Virtual\r | |
533 | )\r | |
534 | /*++\r | |
535 | \r | |
536 | Routine Description:\r | |
537 | Writes specified number of bytes from the input buffer to the block\r | |
538 | \r | |
539 | Arguments:\r | |
540 | Instance - The FV instance to be written to\r | |
541 | Lba - The starting logical block index to write to\r | |
542 | BlockOffset - Offset into the block at which to begin writing\r | |
543 | NumBytes - Pointer that on input contains the total size of\r | |
544 | the buffer. On output, it contains the total number\r | |
545 | of bytes actually written\r | |
546 | Buffer - Pointer to a caller allocated buffer that contains\r | |
547 | the source for the write\r | |
548 | Global - Pointer to ESAL_FWB_GLOBAL that contains all\r | |
549 | instance data\r | |
550 | Virtual - Whether CPU is in virtual or physical mode\r | |
551 | \r | |
552 | Returns: \r | |
553 | EFI_SUCCESS - The firmware volume was written successfully\r | |
554 | EFI_BAD_BUFFER_SIZE - Write attempted across a LBA boundary. On output,\r | |
555 | NumBytes contains the total number of bytes\r | |
556 | actually written\r | |
557 | EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r | |
558 | EFI_DEVICE_ERROR - The block device is not functioning correctly and \r | |
559 | could not be written\r | |
560 | EFI_INVALID_PARAMETER - Instance not found, or NumBytes, Buffer are NULL\r | |
561 | \r | |
562 | --*/\r | |
563 | {\r | |
8ee3a199 | 564 | EFI_FVB_ATTRIBUTES_2 Attributes;\r |
565 | UINTN LbaAddress;\r | |
566 | UINTN LbaLength;\r | |
567 | EFI_FW_VOL_INSTANCE *FwhInstance;\r | |
568 | EFI_STATUS Status;\r | |
569 | EFI_STATUS ReturnStatus;\r | |
9071550e | 570 | \r |
571 | //\r | |
572 | // Find the right instance of the FVB private data\r | |
573 | //\r | |
574 | Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);\r | |
575 | ASSERT_EFI_ERROR (Status);\r | |
576 | \r | |
577 | //\r | |
578 | // Writes are enabled in the init routine itself\r | |
579 | //\r | |
580 | if (!FwhInstance->WriteEnabled) {\r | |
581 | return EFI_ACCESS_DENIED;\r | |
582 | }\r | |
583 | //\r | |
584 | // Check for invalid conditions\r | |
585 | //\r | |
586 | if ((NumBytes == NULL) || (Buffer == NULL)) {\r | |
587 | return EFI_INVALID_PARAMETER;\r | |
588 | }\r | |
589 | \r | |
590 | if (*NumBytes == 0) {\r | |
591 | return EFI_INVALID_PARAMETER;\r | |
592 | }\r | |
593 | \r | |
594 | Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL, Global, Virtual);\r | |
595 | if (EFI_ERROR (Status)) {\r | |
596 | return Status;\r | |
597 | }\r | |
598 | //\r | |
599 | // Check if the FV is write enabled\r | |
600 | //\r | |
601 | FvbGetVolumeAttributes (Instance, &Attributes, Global, Virtual);\r | |
602 | \r | |
603 | if ((Attributes & EFI_FVB2_WRITE_STATUS) == 0) {\r | |
604 | return EFI_ACCESS_DENIED;\r | |
605 | }\r | |
606 | //\r | |
607 | // Perform boundary checks and adjust NumBytes\r | |
608 | //\r | |
609 | if (BlockOffset > LbaLength) {\r | |
610 | return EFI_INVALID_PARAMETER;\r | |
611 | }\r | |
612 | \r | |
613 | if (LbaLength < (*NumBytes + BlockOffset)) {\r | |
614 | *NumBytes = (UINT32) (LbaLength - BlockOffset);\r | |
615 | Status = EFI_BAD_BUFFER_SIZE;\r | |
616 | }\r | |
617 | \r | |
618 | ReturnStatus = FlashFdWrite (\r | |
619 | LbaAddress + BlockOffset,\r | |
620 | FwhInstance,\r | |
621 | NumBytes,\r | |
622 | Buffer\r | |
623 | );\r | |
624 | if (EFI_ERROR (ReturnStatus)) {\r | |
625 | return ReturnStatus;\r | |
626 | }\r | |
627 | \r | |
628 | return Status;\r | |
629 | }\r | |
630 | \r | |
631 | EFI_STATUS\r | |
632 | FvbEraseBlock (\r | |
633 | IN UINTN Instance,\r | |
634 | IN EFI_LBA Lba,\r | |
635 | IN ESAL_FWB_GLOBAL *Global,\r | |
636 | IN BOOLEAN Virtual\r | |
637 | )\r | |
638 | /*++\r | |
639 | \r | |
640 | Routine Description:\r | |
641 | Erases and initializes a firmware volume block\r | |
642 | \r | |
643 | Arguments:\r | |
644 | Instance - The FV instance to be erased\r | |
645 | Lba - The logical block index to be erased\r | |
646 | Global - Pointer to ESAL_FWB_GLOBAL that contains all\r | |
647 | instance data\r | |
648 | Virtual - Whether CPU is in virtual or physical mode\r | |
649 | \r | |
650 | Returns: \r | |
651 | EFI_SUCCESS - The erase request was successfully completed\r | |
652 | EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r | |
653 | EFI_DEVICE_ERROR - The block device is not functioning correctly and \r | |
654 | could not be written. Firmware device may have been\r | |
655 | partially erased\r | |
656 | EFI_INVALID_PARAMETER - Instance not found\r | |
657 | \r | |
658 | --*/\r | |
659 | {\r | |
660 | \r | |
8ee3a199 | 661 | EFI_FVB_ATTRIBUTES_2 Attributes;\r |
662 | UINTN LbaAddress;\r | |
663 | EFI_FW_VOL_INSTANCE *FwhInstance;\r | |
664 | UINTN LbaLength;\r | |
665 | EFI_STATUS Status;\r | |
9071550e | 666 | \r |
667 | //\r | |
668 | // Find the right instance of the FVB private data\r | |
669 | //\r | |
670 | Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);\r | |
671 | ASSERT_EFI_ERROR (Status);\r | |
672 | \r | |
673 | //\r | |
674 | // Writes are enabled in the init routine itself\r | |
675 | //\r | |
676 | if (!FwhInstance->WriteEnabled) {\r | |
677 | return EFI_ACCESS_DENIED;\r | |
678 | }\r | |
679 | //\r | |
680 | // Check if the FV is write enabled\r | |
681 | //\r | |
682 | FvbGetVolumeAttributes (Instance, &Attributes, Global, Virtual);\r | |
683 | \r | |
684 | if ((Attributes & EFI_FVB2_WRITE_STATUS) == 0) {\r | |
685 | return EFI_ACCESS_DENIED;\r | |
686 | }\r | |
687 | //\r | |
688 | // Get the starting address of the block for erase. For debug reasons,\r | |
689 | // LbaWriteAddress may not be the same as LbaAddress.\r | |
690 | //\r | |
691 | Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL, Global, Virtual);\r | |
692 | if (EFI_ERROR (Status)) {\r | |
693 | return Status;\r | |
694 | }\r | |
695 | \r | |
696 | return FlashFdErase (\r | |
697 | LbaAddress,\r | |
698 | FwhInstance,\r | |
699 | LbaLength\r | |
700 | );\r | |
701 | }\r | |
702 | \r | |
703 | EFI_STATUS\r | |
704 | FvbEraseCustomBlockRange (\r | |
705 | IN UINTN Instance,\r | |
706 | IN EFI_LBA StartLba,\r | |
707 | IN UINTN OffsetStartLba,\r | |
708 | IN EFI_LBA LastLba,\r | |
709 | IN UINTN OffsetLastLba,\r | |
710 | IN ESAL_FWB_GLOBAL *Global,\r | |
711 | IN BOOLEAN Virtual\r | |
712 | )\r | |
713 | /*++\r | |
714 | \r | |
715 | Routine Description:\r | |
716 | Erases and initializes a specified range of a firmware volume\r | |
717 | \r | |
718 | Arguments:\r | |
719 | Instance - The FV instance to be erased\r | |
720 | StartLba - The starting logical block index to be erased\r | |
721 | OffsetStartLba - Offset into the starting block at which to \r | |
722 | begin erasing\r | |
723 | LastLba - The last logical block index to be erased\r | |
724 | OffsetStartLba - Offset into the last block at which to end erasing\r | |
725 | Global - Pointer to ESAL_FWB_GLOBAL that contains all\r | |
726 | instance data\r | |
727 | Virtual - Whether CPU is in virtual or physical mode\r | |
728 | \r | |
729 | Returns: \r | |
730 | EFI_SUCCESS - The firmware volume was erased successfully\r | |
731 | EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r | |
732 | EFI_DEVICE_ERROR - The block device is not functioning correctly and \r | |
733 | could not be written. Firmware device may have been\r | |
734 | partially erased\r | |
735 | EFI_INVALID_PARAMETER - Instance not found\r | |
736 | \r | |
737 | --*/\r | |
738 | {\r | |
739 | EFI_LBA Index;\r | |
740 | UINTN LbaSize;\r | |
741 | UINTN ScratchLbaSizeData;\r | |
742 | \r | |
743 | //\r | |
744 | // First LBA.\r | |
745 | //\r | |
746 | FvbGetLbaAddress (Instance, StartLba, NULL, &LbaSize, NULL, Global, Virtual);\r | |
747 | \r | |
748 | //\r | |
749 | // Use the scratch space as the intermediate buffer to transfer data\r | |
750 | // Back up the first LBA in scratch space.\r | |
751 | //\r | |
752 | FvbReadBlock (Instance, StartLba, 0, &LbaSize, Global->FvbScratchSpace[Virtual], Global, Virtual);\r | |
753 | \r | |
754 | //\r | |
755 | // erase now\r | |
756 | //\r | |
757 | FvbEraseBlock (Instance, StartLba, Global, Virtual);\r | |
758 | ScratchLbaSizeData = OffsetStartLba;\r | |
759 | \r | |
760 | //\r | |
761 | // write the data back to the first block\r | |
762 | //\r | |
763 | if (ScratchLbaSizeData > 0) {\r | |
764 | FvbWriteBlock (Instance, StartLba, 0, &ScratchLbaSizeData, Global->FvbScratchSpace[Virtual], Global, Virtual);\r | |
765 | }\r | |
766 | //\r | |
767 | // Middle LBAs\r | |
768 | //\r | |
769 | if (LastLba > (StartLba + 1)) {\r | |
770 | for (Index = (StartLba + 1); Index <= (LastLba - 1); Index++) {\r | |
771 | FvbEraseBlock (Instance, Index, Global, Virtual);\r | |
772 | }\r | |
773 | }\r | |
774 | //\r | |
775 | // Last LBAs, the same as first LBAs\r | |
776 | //\r | |
777 | if (LastLba > StartLba) {\r | |
778 | FvbGetLbaAddress (Instance, LastLba, NULL, &LbaSize, NULL, Global, Virtual);\r | |
779 | FvbReadBlock (Instance, LastLba, 0, &LbaSize, Global->FvbScratchSpace[Virtual], Global, Virtual);\r | |
780 | FvbEraseBlock (Instance, LastLba, Global, Virtual);\r | |
781 | }\r | |
782 | \r | |
783 | ScratchLbaSizeData = LbaSize - (OffsetStartLba + 1);\r | |
784 | \r | |
785 | return FvbWriteBlock (\r | |
786 | Instance,\r | |
787 | LastLba,\r | |
788 | (OffsetLastLba + 1),\r | |
789 | &ScratchLbaSizeData,\r | |
790 | Global->FvbScratchSpace[Virtual],\r | |
791 | Global,\r | |
792 | Virtual\r | |
793 | );\r | |
794 | }\r | |
795 | \r | |
796 | EFI_STATUS\r | |
797 | FvbSetVolumeAttributes (\r | |
798 | IN UINTN Instance,\r | |
8ee3a199 | 799 | IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes,\r |
9071550e | 800 | IN ESAL_FWB_GLOBAL *Global,\r |
801 | IN BOOLEAN Virtual\r | |
802 | )\r | |
803 | /*++\r | |
804 | \r | |
805 | Routine Description:\r | |
806 | Modifies the current settings of the firmware volume according to the \r | |
807 | input parameter, and returns the new setting of the volume\r | |
808 | \r | |
809 | Arguments:\r | |
810 | Instance - The FV instance whose attributes is going to be \r | |
811 | modified\r | |
8ee3a199 | 812 | Attributes - On input, it is a pointer to EFI_FVB_ATTRIBUTES_2 \r |
9071550e | 813 | containing the desired firmware volume settings.\r |
814 | On successful return, it contains the new settings\r | |
815 | of the firmware volume\r | |
816 | Global - Pointer to ESAL_FWB_GLOBAL that contains all\r | |
817 | instance data\r | |
818 | Virtual - Whether CPU is in virtual or physical mode\r | |
819 | \r | |
820 | Returns: \r | |
821 | EFI_SUCCESS - Successfully returns\r | |
822 | EFI_ACCESS_DENIED - The volume setting is locked and cannot be modified\r | |
823 | EFI_INVALID_PARAMETER - Instance not found, or The attributes requested are\r | |
824 | in conflict with the capabilities as declared in the\r | |
825 | firmware volume header\r | |
826 | \r | |
827 | --*/\r | |
828 | {\r | |
8ee3a199 | 829 | EFI_FW_VOL_INSTANCE *FwhInstance;\r |
830 | EFI_FVB_ATTRIBUTES_2 OldAttributes;\r | |
831 | EFI_FVB_ATTRIBUTES_2 *AttribPtr;\r | |
832 | UINT32 Capabilities;\r | |
833 | UINT32 OldStatus;\r | |
834 | UINT32 NewStatus;\r | |
835 | EFI_STATUS Status;\r | |
9071550e | 836 | \r |
837 | //\r | |
838 | // Find the right instance of the FVB private data\r | |
839 | //\r | |
840 | Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);\r | |
841 | ASSERT_EFI_ERROR (Status);\r | |
842 | \r | |
8ee3a199 | 843 | AttribPtr = (EFI_FVB_ATTRIBUTES_2 *) &(FwhInstance->VolumeHeader.Attributes);\r |
9071550e | 844 | OldAttributes = *AttribPtr;\r |
845 | Capabilities = OldAttributes & EFI_FVB2_CAPABILITIES;\r | |
846 | OldStatus = OldAttributes & EFI_FVB2_STATUS;\r | |
847 | NewStatus = *Attributes & EFI_FVB2_STATUS;\r | |
848 | \r | |
849 | //\r | |
850 | // If firmware volume is locked, no status bit can be updated\r | |
851 | //\r | |
852 | if (OldAttributes & EFI_FVB2_LOCK_STATUS) {\r | |
853 | if (OldStatus ^ NewStatus) {\r | |
854 | return EFI_ACCESS_DENIED;\r | |
855 | }\r | |
856 | }\r | |
857 | //\r | |
858 | // Test read disable\r | |
859 | //\r | |
860 | if ((Capabilities & EFI_FVB2_READ_DISABLED_CAP) == 0) {\r | |
861 | if ((NewStatus & EFI_FVB2_READ_STATUS) == 0) {\r | |
862 | return EFI_INVALID_PARAMETER;\r | |
863 | }\r | |
864 | }\r | |
865 | //\r | |
866 | // Test read enable\r | |
867 | //\r | |
868 | if ((Capabilities & EFI_FVB2_READ_ENABLED_CAP) == 0) {\r | |
869 | if (NewStatus & EFI_FVB2_READ_STATUS) {\r | |
870 | return EFI_INVALID_PARAMETER;\r | |
871 | }\r | |
872 | }\r | |
873 | //\r | |
874 | // Test write disable\r | |
875 | //\r | |
876 | if ((Capabilities & EFI_FVB2_WRITE_DISABLED_CAP) == 0) {\r | |
877 | if ((NewStatus & EFI_FVB2_WRITE_STATUS) == 0) {\r | |
878 | return EFI_INVALID_PARAMETER;\r | |
879 | }\r | |
880 | }\r | |
881 | //\r | |
882 | // Test write enable\r | |
883 | //\r | |
884 | if ((Capabilities & EFI_FVB2_WRITE_ENABLED_CAP) == 0) {\r | |
885 | if (NewStatus & EFI_FVB2_WRITE_STATUS) {\r | |
886 | return EFI_INVALID_PARAMETER;\r | |
887 | }\r | |
888 | }\r | |
889 | //\r | |
890 | // Test lock\r | |
891 | //\r | |
892 | if ((Capabilities & EFI_FVB2_LOCK_CAP) == 0) {\r | |
893 | if (NewStatus & EFI_FVB2_LOCK_STATUS) {\r | |
894 | return EFI_INVALID_PARAMETER;\r | |
895 | }\r | |
896 | }\r | |
897 | \r | |
898 | *AttribPtr = (*AttribPtr) & (0xFFFFFFFF & (~EFI_FVB2_STATUS));\r | |
899 | *AttribPtr = (*AttribPtr) | NewStatus;\r | |
900 | *Attributes = *AttribPtr;\r | |
901 | \r | |
902 | return EFI_SUCCESS;\r | |
903 | }\r | |
904 | //\r | |
905 | // FVB protocol APIs\r | |
906 | //\r | |
907 | EFI_STATUS\r | |
908 | EFIAPI\r | |
909 | FvbProtocolGetPhysicalAddress (\r | |
910 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
911 | OUT EFI_PHYSICAL_ADDRESS *Address\r | |
912 | )\r | |
913 | /*++\r | |
914 | \r | |
915 | Routine Description:\r | |
916 | \r | |
917 | Retrieves the physical address of the device.\r | |
918 | \r | |
919 | Arguments:\r | |
920 | \r | |
921 | This - Calling context\r | |
922 | Address - Output buffer containing the address.\r | |
923 | \r | |
924 | Returns:\r | |
925 | \r | |
926 | Returns: \r | |
927 | EFI_SUCCESS - Successfully returns\r | |
928 | \r | |
929 | --*/\r | |
930 | {\r | |
931 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
932 | \r | |
933 | FvbDevice = FVB_DEVICE_FROM_THIS (This);\r | |
934 | \r | |
935 | return FvbGetPhysicalAddress (FvbDevice->Instance, Address, mFvbModuleGlobal, EfiGoneVirtual ());\r | |
936 | }\r | |
937 | \r | |
938 | EFI_STATUS\r | |
939 | EFIAPI\r | |
940 | FvbProtocolGetBlockSize (\r | |
941 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
942 | IN EFI_LBA Lba,\r | |
943 | OUT UINTN *BlockSize,\r | |
944 | OUT UINTN *NumOfBlocks\r | |
945 | )\r | |
946 | /*++\r | |
947 | \r | |
948 | Routine Description:\r | |
949 | Retrieve the size of a logical block\r | |
950 | \r | |
951 | Arguments:\r | |
952 | This - Calling context\r | |
953 | Lba - Indicates which block to return the size for.\r | |
954 | BlockSize - A pointer to a caller allocated UINTN in which\r | |
955 | the size of the block is returned\r | |
956 | NumOfBlocks - a pointer to a caller allocated UINTN in which the\r | |
957 | number of consecutive blocks starting with Lba is\r | |
958 | returned. All blocks in this range have a size of\r | |
959 | BlockSize\r | |
960 | \r | |
961 | Returns: \r | |
962 | EFI_SUCCESS - The firmware volume was read successfully and \r | |
963 | contents are in Buffer\r | |
964 | \r | |
965 | --*/\r | |
966 | {\r | |
967 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
968 | \r | |
969 | FvbDevice = FVB_DEVICE_FROM_THIS (This);\r | |
970 | \r | |
971 | return FvbGetLbaAddress (\r | |
972 | FvbDevice->Instance,\r | |
973 | Lba,\r | |
974 | NULL,\r | |
975 | BlockSize,\r | |
976 | NumOfBlocks,\r | |
977 | mFvbModuleGlobal,\r | |
978 | EfiGoneVirtual ()\r | |
979 | );\r | |
980 | }\r | |
981 | \r | |
982 | EFI_STATUS\r | |
983 | EFIAPI\r | |
984 | FvbProtocolGetAttributes (\r | |
985 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
8ee3a199 | 986 | OUT EFI_FVB_ATTRIBUTES_2 *Attributes\r |
9071550e | 987 | )\r |
988 | /*++\r | |
989 | \r | |
990 | Routine Description:\r | |
991 | Retrieves Volume attributes. No polarity translations are done.\r | |
992 | \r | |
993 | Arguments:\r | |
994 | This - Calling context\r | |
995 | Attributes - output buffer which contains attributes\r | |
996 | \r | |
997 | Returns: \r | |
998 | EFI_SUCCESS - Successfully returns\r | |
999 | \r | |
1000 | --*/\r | |
1001 | {\r | |
1002 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
1003 | \r | |
1004 | FvbDevice = FVB_DEVICE_FROM_THIS (This);\r | |
1005 | \r | |
1006 | return FvbGetVolumeAttributes (FvbDevice->Instance, Attributes, mFvbModuleGlobal, EfiGoneVirtual ());\r | |
1007 | }\r | |
1008 | \r | |
1009 | EFI_STATUS\r | |
1010 | EFIAPI\r | |
1011 | FvbProtocolSetAttributes (\r | |
1012 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
8ee3a199 | 1013 | IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes\r |
9071550e | 1014 | )\r |
1015 | /*++\r | |
1016 | \r | |
1017 | Routine Description:\r | |
1018 | Sets Volume attributes. No polarity translations are done.\r | |
1019 | \r | |
1020 | Arguments:\r | |
1021 | This - Calling context\r | |
1022 | Attributes - output buffer which contains attributes\r | |
1023 | \r | |
1024 | Returns: \r | |
1025 | EFI_SUCCESS - Successfully returns\r | |
1026 | \r | |
1027 | --*/\r | |
1028 | {\r | |
1029 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
1030 | \r | |
1031 | FvbDevice = FVB_DEVICE_FROM_THIS (This);\r | |
1032 | \r | |
1033 | return FvbSetVolumeAttributes (FvbDevice->Instance, Attributes, mFvbModuleGlobal, EfiGoneVirtual ());\r | |
1034 | }\r | |
1035 | \r | |
1036 | EFI_STATUS\r | |
1037 | EFIAPI\r | |
1038 | FvbProtocolEraseBlocks (\r | |
1039 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
1040 | ... \r | |
1041 | )\r | |
1042 | /*++\r | |
1043 | \r | |
1044 | Routine Description:\r | |
1045 | \r | |
1046 | The EraseBlock() function erases one or more blocks as denoted by the \r | |
1047 | variable argument list. The entire parameter list of blocks must be verified\r | |
1048 | prior to erasing any blocks. If a block is requested that does not exist \r | |
1049 | within the associated firmware volume (it has a larger index than the last \r | |
1050 | block of the firmware volume), the EraseBlock() function must return\r | |
1051 | EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.\r | |
1052 | \r | |
1053 | Arguments:\r | |
1054 | This - Calling context\r | |
1055 | ... - Starting LBA followed by Number of Lba to erase. \r | |
1056 | a -1 to terminate the list.\r | |
1057 | \r | |
1058 | Returns: \r | |
1059 | EFI_SUCCESS - The erase request was successfully completed\r | |
1060 | EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r | |
1061 | EFI_DEVICE_ERROR - The block device is not functioning correctly and \r | |
1062 | could not be written. Firmware device may have been\r | |
1063 | partially erased\r | |
1064 | \r | |
1065 | --*/\r | |
1066 | {\r | |
1067 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
1068 | EFI_FW_VOL_INSTANCE *FwhInstance;\r | |
1069 | UINTN NumOfBlocks;\r | |
1070 | VA_LIST args;\r | |
1071 | EFI_LBA StartingLba;\r | |
1072 | UINTN NumOfLba;\r | |
1073 | EFI_STATUS Status;\r | |
1074 | \r | |
1075 | FvbDevice = FVB_DEVICE_FROM_THIS (This);\r | |
1076 | \r | |
1077 | Status = GetFvbInstance (FvbDevice->Instance, mFvbModuleGlobal, &FwhInstance, EfiGoneVirtual ());\r | |
1078 | ASSERT_EFI_ERROR (Status);\r | |
1079 | \r | |
1080 | NumOfBlocks = FwhInstance->NumOfBlocks;\r | |
1081 | \r | |
1082 | VA_START (args, This);\r | |
1083 | \r | |
1084 | do {\r | |
1085 | StartingLba = VA_ARG (args, EFI_LBA);\r | |
1086 | if (StartingLba == EFI_LBA_LIST_TERMINATOR) {\r | |
1087 | break;\r | |
1088 | }\r | |
1089 | \r | |
1090 | NumOfLba = VA_ARG (args, UINT32);\r | |
1091 | \r | |
1092 | //\r | |
1093 | // Check input parameters\r | |
1094 | //\r | |
1095 | if ((NumOfLba == 0) || ((StartingLba + NumOfLba) > NumOfBlocks)) {\r | |
1096 | VA_END (args);\r | |
1097 | return EFI_INVALID_PARAMETER;\r | |
1098 | }\r | |
1099 | } while (1);\r | |
1100 | \r | |
1101 | VA_END (args);\r | |
1102 | \r | |
1103 | VA_START (args, This);\r | |
1104 | do {\r | |
1105 | StartingLba = VA_ARG (args, EFI_LBA);\r | |
1106 | if (StartingLba == EFI_LBA_LIST_TERMINATOR) {\r | |
1107 | break;\r | |
1108 | }\r | |
1109 | \r | |
1110 | NumOfLba = VA_ARG (args, UINT32);\r | |
1111 | \r | |
1112 | while (NumOfLba > 0) {\r | |
1113 | Status = FvbEraseBlock (FvbDevice->Instance, StartingLba, mFvbModuleGlobal, EfiGoneVirtual ());\r | |
1114 | if (EFI_ERROR (Status)) {\r | |
1115 | VA_END (args);\r | |
1116 | return Status;\r | |
1117 | }\r | |
1118 | \r | |
1119 | StartingLba++;\r | |
1120 | NumOfLba--;\r | |
1121 | }\r | |
1122 | \r | |
1123 | } while (1);\r | |
1124 | \r | |
1125 | VA_END (args);\r | |
1126 | \r | |
1127 | return EFI_SUCCESS;\r | |
1128 | }\r | |
1129 | \r | |
1130 | EFI_STATUS\r | |
1131 | EFIAPI\r | |
1132 | FvbProtocolWrite (\r | |
1133 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
1134 | IN EFI_LBA Lba,\r | |
1135 | IN UINTN Offset,\r | |
1136 | IN OUT UINTN *NumBytes,\r | |
1137 | IN UINT8 *Buffer\r | |
1138 | )\r | |
1139 | /*++\r | |
1140 | \r | |
1141 | Routine Description:\r | |
1142 | \r | |
1143 | Writes data beginning at Lba:Offset from FV. The write terminates either\r | |
1144 | when *NumBytes of data have been written, or when a block boundary is\r | |
1145 | reached. *NumBytes is updated to reflect the actual number of bytes\r | |
1146 | written. The write opertion does not include erase. This routine will\r | |
1147 | attempt to write only the specified bytes. If the writes do not stick,\r | |
1148 | it will return an error.\r | |
1149 | \r | |
1150 | Arguments:\r | |
1151 | This - Calling context\r | |
1152 | Lba - Block in which to begin write\r | |
1153 | Offset - Offset in the block at which to begin write\r | |
1154 | NumBytes - On input, indicates the requested write size. On\r | |
1155 | output, indicates the actual number of bytes written\r | |
1156 | Buffer - Buffer containing source data for the write.\r | |
1157 | \r | |
1158 | Returns: \r | |
1159 | EFI_SUCCESS - The firmware volume was written successfully\r | |
1160 | EFI_BAD_BUFFER_SIZE - Write attempted across a LBA boundary. On output,\r | |
1161 | NumBytes contains the total number of bytes\r | |
1162 | actually written\r | |
1163 | EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r | |
1164 | EFI_DEVICE_ERROR - The block device is not functioning correctly and \r | |
1165 | could not be written\r | |
1166 | EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL\r | |
1167 | \r | |
1168 | --*/\r | |
1169 | {\r | |
1170 | \r | |
1171 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
1172 | \r | |
1173 | FvbDevice = FVB_DEVICE_FROM_THIS (This);\r | |
1174 | \r | |
1175 | return FvbWriteBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer, mFvbModuleGlobal, EfiGoneVirtual ());\r | |
1176 | }\r | |
1177 | \r | |
1178 | EFI_STATUS\r | |
1179 | EFIAPI\r | |
1180 | FvbProtocolRead (\r | |
1181 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
1182 | IN EFI_LBA Lba,\r | |
1183 | IN UINTN Offset,\r | |
1184 | IN OUT UINTN *NumBytes,\r | |
1185 | IN UINT8 *Buffer\r | |
1186 | )\r | |
1187 | /*++\r | |
1188 | \r | |
1189 | Routine Description:\r | |
1190 | \r | |
1191 | Reads data beginning at Lba:Offset from FV. The Read terminates either\r | |
1192 | when *NumBytes of data have been read, or when a block boundary is\r | |
1193 | reached. *NumBytes is updated to reflect the actual number of bytes\r | |
1194 | written. The write opertion does not include erase. This routine will\r | |
1195 | attempt to write only the specified bytes. If the writes do not stick,\r | |
1196 | it will return an error.\r | |
1197 | \r | |
1198 | Arguments:\r | |
1199 | This - Calling context\r | |
1200 | Lba - Block in which to begin Read\r | |
1201 | Offset - Offset in the block at which to begin Read\r | |
1202 | NumBytes - On input, indicates the requested write size. On\r | |
1203 | output, indicates the actual number of bytes Read\r | |
1204 | Buffer - Buffer containing source data for the Read.\r | |
1205 | \r | |
1206 | Returns: \r | |
1207 | EFI_SUCCESS - The firmware volume was read successfully and \r | |
1208 | contents are in Buffer\r | |
1209 | EFI_BAD_BUFFER_SIZE - Read attempted across a LBA boundary. On output,\r | |
1210 | NumBytes contains the total number of bytes returned\r | |
1211 | in Buffer\r | |
1212 | EFI_ACCESS_DENIED - The firmware volume is in the ReadDisabled state\r | |
1213 | EFI_DEVICE_ERROR - The block device is not functioning correctly and \r | |
1214 | could not be read\r | |
1215 | EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL\r | |
1216 | \r | |
1217 | --*/\r | |
1218 | {\r | |
1219 | \r | |
1220 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
1221 | \r | |
1222 | FvbDevice = FVB_DEVICE_FROM_THIS (This);\r | |
1223 | \r | |
1224 | return FvbReadBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer, mFvbModuleGlobal, EfiGoneVirtual ());\r | |
1225 | }\r | |
1226 | //\r | |
1227 | // FVB Extension Protocols\r | |
1228 | //\r | |
1229 | EFI_STATUS\r | |
1230 | EFIAPI\r | |
1231 | FvbExtendProtocolEraseCustomBlockRange (\r | |
1232 | IN EFI_FVB_EXTENSION_PROTOCOL *This,\r | |
1233 | IN EFI_LBA StartLba,\r | |
1234 | IN UINTN OffsetStartLba,\r | |
1235 | IN EFI_LBA LastLba,\r | |
1236 | IN UINTN OffsetLastLba\r | |
1237 | )\r | |
1238 | /*++\r | |
1239 | \r | |
1240 | Routine Description:\r | |
1241 | Erases and initializes a specified range of a firmware volume\r | |
1242 | \r | |
1243 | Arguments:\r | |
1244 | This - Calling context\r | |
1245 | StartLba - The starting logical block index to be erased\r | |
1246 | OffsetStartLba - Offset into the starting block at which to \r | |
1247 | begin erasing\r | |
1248 | LastLba - The last logical block index to be erased\r | |
1249 | OffsetStartLba - Offset into the last block at which to end erasing\r | |
1250 | \r | |
1251 | Returns: \r | |
1252 | EFI_SUCCESS - The firmware volume was erased successfully\r | |
1253 | EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r | |
1254 | EFI_DEVICE_ERROR - The block device is not functioning correctly and \r | |
1255 | could not be written. Firmware device may have been\r | |
1256 | partially erased\r | |
1257 | \r | |
1258 | --*/\r | |
1259 | {\r | |
1260 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
1261 | \r | |
1262 | FvbDevice = FVB_EXTEND_DEVICE_FROM_THIS (This);\r | |
1263 | \r | |
1264 | return FvbEraseCustomBlockRange (\r | |
1265 | FvbDevice->Instance,\r | |
1266 | StartLba,\r | |
1267 | OffsetStartLba,\r | |
1268 | LastLba,\r | |
1269 | OffsetLastLba,\r | |
1270 | mFvbModuleGlobal,\r | |
1271 | EfiGoneVirtual ()\r | |
1272 | );\r | |
1273 | }\r | |
1274 | \r | |
1275 | STATIC\r | |
1276 | EFI_STATUS\r | |
1277 | ValidateFvHeader (\r | |
1278 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader\r | |
1279 | )\r | |
1280 | /*++\r | |
1281 | \r | |
1282 | Routine Description:\r | |
1283 | Check the integrity of firmware volume header\r | |
1284 | \r | |
1285 | Arguments:\r | |
1286 | FwVolHeader - A pointer to a firmware volume header\r | |
1287 | \r | |
1288 | Returns: \r | |
1289 | EFI_SUCCESS - The firmware volume is consistent\r | |
1290 | EFI_NOT_FOUND - The firmware volume has corrupted. So it is not an FV\r | |
1291 | \r | |
1292 | --*/\r | |
1293 | {\r | |
1294 | UINT16 *Ptr;\r | |
1295 | UINT16 HeaderLength;\r | |
1296 | UINT16 Checksum;\r | |
1297 | \r | |
1298 | //\r | |
1299 | // Verify the header revision, header signature, length\r | |
1300 | // Length of FvBlock cannot be 2**64-1\r | |
1301 | // HeaderLength cannot be an odd number\r | |
1302 | //\r | |
1303 | if ((FwVolHeader->Revision != EFI_FVH_REVISION) ||\r | |
1304 | (FwVolHeader->Signature != EFI_FVH_SIGNATURE) ||\r | |
1305 | (FwVolHeader->FvLength == ((UINTN) -1)) ||\r | |
1306 | ((FwVolHeader->HeaderLength & 0x01) != 0)\r | |
1307 | ) {\r | |
1308 | return EFI_NOT_FOUND;\r | |
1309 | }\r | |
1310 | //\r | |
1311 | // Verify the header checksum\r | |
1312 | //\r | |
1313 | HeaderLength = (UINT16) (FwVolHeader->HeaderLength / 2);\r | |
1314 | Ptr = (UINT16 *) FwVolHeader;\r | |
1315 | Checksum = 0;\r | |
1316 | while (HeaderLength > 0) {\r | |
1317 | Checksum = Checksum + (*Ptr);\r | |
1318 | HeaderLength--;\r | |
1319 | Ptr++;\r | |
1320 | }\r | |
1321 | \r | |
1322 | if (Checksum != 0) {\r | |
1323 | return EFI_NOT_FOUND;\r | |
1324 | }\r | |
1325 | \r | |
1326 | return EFI_SUCCESS;\r | |
1327 | }\r | |
1328 | \r | |
1329 | \r | |
1330 | EFI_STATUS\r | |
1331 | GetFvbHeader (\r | |
eb16e240 | 1332 | IN OUT EFI_PEI_HOB_POINTERS *HobList,\r |
9071550e | 1333 | OUT EFI_FIRMWARE_VOLUME_HEADER **FwVolHeader,\r |
1334 | OUT EFI_PHYSICAL_ADDRESS *BaseAddress OPTIONAL,\r | |
1335 | OUT UINT32 *VolumeId OPTIONAL,\r | |
1336 | OUT CHAR16 **MappedFile OPTIONAL,\r | |
1337 | OUT UINT32 *ActuralSize OPTIONAL,\r | |
1338 | OUT UINT32 *Offset OPTIONAL,\r | |
1339 | OUT BOOLEAN *WriteBack OPTIONAL\r | |
1340 | )\r | |
1341 | {\r | |
1342 | EFI_STATUS Status;\r | |
9071550e | 1343 | EFI_FLASH_MAP_FS_ENTRY_DATA *FlashMapEntry;\r |
1344 | EFI_FLASH_SUBAREA_ENTRY *FlashMapSubEntry;\r | |
1345 | \r | |
1346 | Status = EFI_SUCCESS;\r | |
1347 | *FwVolHeader = NULL;\r | |
1348 | TRY_ASSIGN (WriteBack, FALSE);\r | |
1349 | \r | |
eb16e240 | 1350 | DEBUG ((EFI_D_INFO, "Hob start is 0x%x\n", (UINTN)(*HobList).Raw));\r |
1351 | (*HobList).Raw = GetNextGuidHob (&gEfiFlashMapHobGuid, (*HobList).Raw);\r | |
1352 | if ((*HobList).Raw == NULL) {\r | |
9071550e | 1353 | return EFI_NOT_FOUND;\r |
1354 | }\r | |
1355 | \r | |
eb16e240 | 1356 | FlashMapEntry = (EFI_FLASH_MAP_FS_ENTRY_DATA *) GET_GUID_HOB_DATA ((*HobList).Guid);\r |
9071550e | 1357 | FlashMapSubEntry = &FlashMapEntry->Entries[0];\r |
eb16e240 | 1358 | \r |
9071550e | 1359 | //\r |
1360 | // Check if it is a "FVB" area\r | |
1361 | //\r | |
1362 | if (!CompareGuid (&FlashMapSubEntry->FileSystem, &gEfiFirmwareVolumeBlockProtocolGuid)) {\r | |
1363 | return Status;\r | |
1364 | }\r | |
1365 | //\r | |
1366 | // Check if it is a "real" flash\r | |
1367 | //\r | |
1368 | if (FlashMapSubEntry->Attributes != (EFI_FLASH_AREA_FV | EFI_FLASH_AREA_MEMMAPPED_FV)) {\r | |
1369 | return Status;\r | |
1370 | }\r | |
1371 | \r | |
1372 | TRY_ASSIGN (BaseAddress, FlashMapSubEntry->Base);\r | |
1373 | \r | |
1374 | //\r | |
1375 | // Cast buffer to FLASH_AREA_INFO to get extra information related to the special FVB driver\r | |
1376 | //\r | |
1377 | TRY_ASSIGN (VolumeId, FlashMapEntry->VolumeId);\r | |
1378 | TRY_ASSIGN (ActuralSize, FlashMapEntry->ActuralSize);\r | |
1379 | TRY_ASSIGN (MappedFile, ((CHAR16 *) FlashMapEntry->FilePath));\r | |
1380 | TRY_ASSIGN (Offset, FlashMapEntry->Offset);\r | |
1381 | \r | |
1382 | DEBUG ((\r | |
be768885 | 1383 | EFI_D_INFO, \r |
9071550e | 1384 | "FlashMap HOB: BaseAddress = 0x%x, Length = 0x%x, ActuralLength = 0x%x, Offset = 0x%x\n", \r |
1385 | (UINTN) FlashMapSubEntry->Base, (UINTN) FlashMapSubEntry->Length, \r | |
1386 | (UINTN) FlashMapEntry->ActuralSize, (UINTN) FlashMapEntry->Offset\r | |
1387 | ));\r | |
1388 | DEBUG ((\r | |
be768885 | 1389 | EFI_D_INFO,\r |
9071550e | 1390 | "FlashMap HOB: VolumeId = 0x%lx, MappedFile = %s\n",\r |
1391 | (UINTN) FlashMapEntry->VolumeId, (UINTN) FlashMapEntry->FilePath\r | |
1392 | ));\r | |
1393 | *FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (FlashMapSubEntry->Base);\r | |
1394 | Status = ValidateFvHeader (*FwVolHeader);\r | |
1395 | if (EFI_ERROR (Status)) {\r | |
1396 | //\r | |
1397 | // Get FvbInfo\r | |
1398 | //\r | |
1399 | TRY_ASSIGN (WriteBack, TRUE);\r | |
1400 | Status = GetFvbInfo (FlashMapSubEntry->Length, FwVolHeader);\r | |
1401 | DEBUG ((EFI_D_ERROR, "Fvb: FV header invalid, GetFvbInfo - %r\n", Status));\r | |
1402 | ASSERT_EFI_ERROR (Status);\r | |
1403 | }\r | |
1404 | \r | |
1405 | return EFI_SUCCESS;\r | |
1406 | }\r | |
1407 | \r | |
1408 | STATIC\r | |
1409 | VOID\r | |
1410 | EFIAPI\r | |
1411 | OnSimpleFileSystemInstall (\r | |
1412 | IN EFI_EVENT Event,\r | |
1413 | IN VOID *Context\r | |
1414 | )\r | |
1415 | {\r | |
1416 | EFI_STATUS Status;\r | |
1417 | UINTN HandleSize;\r | |
1418 | EFI_HANDLE Handle;\r | |
1419 | UINTN Instance;\r | |
1420 | EFI_DEVICE_PATH_PROTOCOL *Device;\r | |
1421 | EFI_FILE *File;\r | |
1422 | EFI_FW_VOL_INSTANCE *FwhInstance;\r | |
1423 | while (TRUE) {\r | |
1424 | HandleSize = sizeof (EFI_HANDLE);\r | |
1425 | Status = gBS->LocateHandle (\r | |
1426 | ByRegisterNotify,\r | |
1427 | NULL,\r | |
1428 | mSFSRegistration,\r | |
1429 | &HandleSize,\r | |
1430 | &Handle\r | |
1431 | );\r | |
1432 | if (Status == EFI_NOT_FOUND) {\r | |
1433 | break;\r | |
1434 | }\r | |
1435 | DEBUG ((EFI_D_ERROR, "Fwh: New FileSystem Installed!\n"));\r | |
1436 | ASSERT_EFI_ERROR (Status);\r | |
1437 | //\r | |
1438 | // Check if this is the storage we care about, and store it in FwhInstance\r | |
1439 | //\r | |
1440 | for (Instance = 0; Instance < mFvbModuleGlobal->NumFv; ++Instance) {\r | |
1441 | Status = GetFvbInstance (Instance, mFvbModuleGlobal, &FwhInstance, FALSE);\r | |
1442 | ASSERT_EFI_ERROR (Status);\r | |
1443 | \r | |
1444 | if (FwhInstance->MappedFile[0] == L'\0') {\r | |
1445 | //\r | |
1446 | // The instance of FVB isn't mapped to file.\r | |
1447 | //\r | |
1448 | continue;\r | |
1449 | }\r | |
1450 | \r | |
1451 | if ((FwhInstance->Device != NULL) && \r | |
1452 | !EFI_ERROR (CheckStoreExists (FwhInstance->Device))\r | |
1453 | ) {\r | |
1454 | //\r | |
1455 | // The instance of FVB has already associated to a device\r | |
1456 | // and the device is not removed from system.\r | |
1457 | //\r | |
1458 | DEBUG ((\r | |
1459 | EFI_D_ERROR, "Fwh: MappedFile FVB (0x%x:0x%x) - Already mapped, Skip!\n", \r | |
1460 | (UINTN) FwhInstance->FvBase[FVB_PHYSICAL],\r | |
1461 | (UINTN) FwhInstance->Offset\r | |
1462 | ));\r | |
1463 | continue;\r | |
1464 | }\r | |
1465 | \r | |
1466 | Status = CheckStore (Handle, FwhInstance->VolumeId, &Device);\r | |
1467 | if (!EFI_ERROR (Status)) {\r | |
1468 | //\r | |
1469 | // Write back memory content to file\r | |
1470 | //\r | |
1471 | Status = FileOpen (Device, FwhInstance->MappedFile, &File, EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ | EFI_FILE_MODE_CREATE);\r | |
1472 | ASSERT_EFI_ERROR (Status); \r | |
1473 | if (!EFI_ERROR (Status)) {\r | |
1474 | DEBUG ((\r | |
1475 | EFI_D_ERROR, "Fwh: MappedFile FVB (0x%x:0x%x) - Write back to mapped file!\n", \r | |
1476 | (UINTN) FwhInstance->FvBase[FVB_PHYSICAL],\r | |
1477 | (UINTN) FwhInstance->Offset\r | |
1478 | ));\r | |
1479 | Status = FileWrite (\r | |
1480 | File, \r | |
1481 | 0, \r | |
1482 | FwhInstance->FvBase[FVB_PHYSICAL] + FwhInstance->Offset, \r | |
1483 | FwhInstance->ActuralSize - FwhInstance->Offset\r | |
1484 | );\r | |
1485 | ASSERT_EFI_ERROR (Status); \r | |
1486 | if (!EFI_ERROR (Status)) {\r | |
1487 | if (FwhInstance->Device != NULL) {\r | |
1488 | gBS->FreePool (FwhInstance->Device);\r | |
1489 | }\r | |
1490 | FwhInstance->Device = Device;\r | |
1491 | DEBUG ((\r | |
1492 | EFI_D_ERROR, "Fwh: MappedFile FVB (0x%x:0x%x) - Mapped!\n",\r | |
1493 | (UINTN) FwhInstance->FvBase[FVB_PHYSICAL],\r | |
1494 | (UINTN) FwhInstance->Offset\r | |
1495 | ));\r | |
1496 | }\r | |
1497 | FileClose (File);\r | |
1498 | }\r | |
1499 | }\r | |
1500 | }\r | |
1501 | }\r | |
1502 | }\r | |
1503 | \r | |
1504 | STATIC\r | |
1505 | VOID\r | |
1506 | FvbInstallSfsNotify (\r | |
1507 | VOID\r | |
1508 | )\r | |
1509 | {\r | |
1510 | EFI_STATUS Status;\r | |
1511 | EFI_EVENT Event;\r | |
1512 | \r | |
1513 | Status = gBS->CreateEvent (\r | |
e53a6ea9 | 1514 | EVT_NOTIFY_SIGNAL,\r |
9071550e | 1515 | TPL_CALLBACK,\r |
1516 | OnSimpleFileSystemInstall,\r | |
1517 | NULL,\r | |
1518 | &Event\r | |
1519 | );\r | |
1520 | ASSERT_EFI_ERROR (Status);\r | |
1521 | \r | |
1522 | Status = gBS->RegisterProtocolNotify (\r | |
1523 | &gEfiSimpleFileSystemProtocolGuid,\r | |
1524 | Event,\r | |
1525 | &mSFSRegistration\r | |
1526 | );\r | |
1527 | ASSERT_EFI_ERROR (Status);\r | |
1528 | }\r | |
1529 | \r | |
1530 | \r | |
1531 | EFI_STATUS\r | |
1532 | EFIAPI\r | |
1533 | FvbInitialize (\r | |
1534 | IN EFI_HANDLE ImageHandle,\r | |
1535 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
1536 | )\r | |
1537 | /*++\r | |
1538 | \r | |
1539 | Routine Description:\r | |
1540 | This function does common initialization for FVB services\r | |
1541 | \r | |
1542 | Arguments:\r | |
1543 | \r | |
1544 | Returns:\r | |
1545 | \r | |
1546 | --*/\r | |
1547 | {\r | |
1548 | EFI_STATUS Status;\r | |
1549 | EFI_FW_VOL_INSTANCE *FwhInstance;\r | |
1550 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r | |
eb16e240 | 1551 | EFI_PEI_HOB_POINTERS FirmwareVolumeHobList;\r |
9071550e | 1552 | UINT32 BufferSize;\r |
1553 | EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r | |
1554 | UINTN LbaAddress;\r | |
1555 | EFI_HANDLE FwbHandle;\r | |
1556 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
1557 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *OldFwbInterface;\r | |
1558 | EFI_DEVICE_PATH_PROTOCOL *TempFwbDevicePath;\r | |
1559 | FV_DEVICE_PATH TempFvbDevicePathData;\r | |
1560 | UINT32 MaxLbaSize;\r | |
1561 | EFI_PHYSICAL_ADDRESS BaseAddress;\r | |
1562 | UINT32 VolumeId;\r | |
1563 | CHAR16 *MappedFile;\r | |
1564 | UINT32 ActuralSize;\r | |
1565 | UINT32 Offset;\r | |
1566 | BOOLEAN WriteBack;\r | |
1567 | UINTN NumOfBlocks;\r | |
1568 | UINTN HeaderLength;\r | |
1569 | BOOLEAN InstallSfsNotify;\r | |
1570 | \r | |
9071550e | 1571 | HeaderLength = 0;\r |
1572 | InstallSfsNotify = FALSE;\r | |
1573 | \r | |
1574 | //\r | |
1575 | // Allocate runtime services data for global variable, which contains\r | |
1576 | // the private data of all firmware volume block instances\r | |
1577 | //\r | |
1578 | Status = gBS->AllocatePool (\r | |
1579 | EfiRuntimeServicesData,\r | |
1580 | sizeof (ESAL_FWB_GLOBAL),\r | |
1581 | &mFvbModuleGlobal\r | |
1582 | );\r | |
1583 | ASSERT_EFI_ERROR (Status);\r | |
1584 | //\r | |
1585 | // Calculate the total size for all firmware volume block instances\r | |
1586 | //\r | |
1587 | BufferSize = 0;\r | |
eb16e240 | 1588 | FirmwareVolumeHobList.Raw = GetHobList();\r |
9071550e | 1589 | do {\r |
1590 | Status = GetFvbHeader (&FirmwareVolumeHobList, &FwVolHeader, NULL, NULL, NULL, NULL, NULL, NULL);\r | |
1591 | if (EFI_ERROR (Status)) {\r | |
1592 | break;\r | |
1593 | }\r | |
eb16e240 | 1594 | FirmwareVolumeHobList.Raw = GET_NEXT_HOB (FirmwareVolumeHobList);\r |
9071550e | 1595 | \r |
1596 | if (FwVolHeader) {\r | |
1597 | BufferSize += (FwVolHeader->HeaderLength + sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER));\r | |
1598 | }\r | |
1599 | } while (TRUE);\r | |
1600 | \r | |
1601 | //\r | |
1602 | // Only need to allocate once. There is only one copy of physical memory for\r | |
1603 | // the private data of each FV instance. But in virtual mode or in physical\r | |
1604 | // mode, the address of the the physical memory may be different.\r | |
1605 | //\r | |
1606 | Status = gBS->AllocatePool (\r | |
1607 | EfiRuntimeServicesData,\r | |
1608 | BufferSize,\r | |
1609 | &mFvbModuleGlobal->FvInstance[FVB_PHYSICAL]\r | |
1610 | );\r | |
1611 | ASSERT_EFI_ERROR (Status);\r | |
1612 | \r | |
1613 | //\r | |
1614 | // Make a virtual copy of the FvInstance pointer.\r | |
1615 | //\r | |
1616 | FwhInstance = mFvbModuleGlobal->FvInstance[FVB_PHYSICAL];\r | |
1617 | mFvbModuleGlobal->FvInstance[FVB_VIRTUAL] = FwhInstance;\r | |
1618 | \r | |
eb16e240 | 1619 | mFvbModuleGlobal->NumFv = 0;\r |
1620 | FirmwareVolumeHobList.Raw = GetHobList();\r | |
1621 | MaxLbaSize = 0;\r | |
9071550e | 1622 | \r |
1623 | //\r | |
1624 | // Fill in the private data of each firmware volume block instance\r | |
1625 | //\r | |
1626 | do {\r | |
1627 | Status = GetFvbHeader (\r | |
1628 | &FirmwareVolumeHobList, &FwVolHeader, \r | |
1629 | &BaseAddress, &VolumeId, &MappedFile, &ActuralSize, &Offset,\r | |
1630 | &WriteBack\r | |
1631 | );\r | |
1632 | if (EFI_ERROR (Status)) {\r | |
1633 | break;\r | |
1634 | }\r | |
eb16e240 | 1635 | FirmwareVolumeHobList.Raw = GET_NEXT_HOB (FirmwareVolumeHobList);\r |
9071550e | 1636 | \r |
1637 | if (!FwVolHeader) {\r | |
1638 | continue;\r | |
1639 | }\r | |
be768885 | 1640 | \r |
9071550e | 1641 | CopyMem ((UINTN *) &(FwhInstance->VolumeHeader), (UINTN *) FwVolHeader, FwVolHeader->HeaderLength);\r |
1642 | FwVolHeader = &(FwhInstance->VolumeHeader);\r | |
1643 | \r | |
1644 | FwhInstance->FvBase[FVB_PHYSICAL] = (UINTN) BaseAddress;\r | |
1645 | FwhInstance->FvBase[FVB_VIRTUAL] = (UINTN) BaseAddress;\r | |
1646 | FwhInstance->Device = NULL;\r | |
1647 | FwhInstance->Offset = Offset;\r | |
1648 | \r | |
1649 | if (*MappedFile != '\0') {\r | |
1650 | FwhInstance->VolumeId = VolumeId;\r | |
1651 | FwhInstance->ActuralSize = ActuralSize;\r | |
1652 | StrCpy (FwhInstance->MappedFile, MappedFile);\r | |
1653 | \r | |
1654 | InstallSfsNotify = TRUE;\r | |
1655 | } else {\r | |
1656 | FwhInstance->VolumeId = (UINT32) -1;\r | |
1657 | FwhInstance->ActuralSize = (UINT32) -1;\r | |
1658 | FwhInstance->MappedFile[0] = L'\0';\r | |
1659 | }\r | |
1660 | \r | |
be768885 | 1661 | DEBUG ((EFI_D_INFO, "FirmVolume Found! BaseAddress=0x%lx, VolumeId=0x%x, MappedFile=%s, Size=0x%x\n",\r |
1662 | (UINTN) BaseAddress, VolumeId, MappedFile, ActuralSize));\r | |
9071550e | 1663 | //\r |
1664 | // We may expose readonly FVB in future.\r | |
1665 | //\r | |
be768885 | 1666 | FwhInstance->WriteEnabled = TRUE; // Ken: Why enable write?\r |
9071550e | 1667 | EfiInitializeLock (&(FwhInstance->FvbDevLock), TPL_HIGH_LEVEL);\r |
1668 | \r | |
1669 | LbaAddress = (UINTN) FwhInstance->FvBase[0];\r | |
1670 | NumOfBlocks = 0;\r | |
1671 | \r | |
1672 | if (FwhInstance->WriteEnabled) {\r | |
1673 | for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r | |
1674 | \r | |
1675 | LbaAddress += PtrBlockMapEntry->NumBlocks * PtrBlockMapEntry->Length;\r | |
1676 | //\r | |
1677 | // Get the maximum size of a block. The size will be used to allocate\r | |
1678 | // buffer for Scratch space, the intermediate buffer for FVB extension\r | |
1679 | // protocol\r | |
1680 | //\r | |
1681 | if (MaxLbaSize < PtrBlockMapEntry->Length) {\r | |
1682 | MaxLbaSize = PtrBlockMapEntry->Length;\r | |
1683 | }\r | |
1684 | \r | |
1685 | NumOfBlocks += PtrBlockMapEntry->NumBlocks;\r | |
1686 | }\r | |
1687 | //\r | |
1688 | // Write back a healthy FV header\r | |
1689 | //\r | |
1690 | if (WriteBack) {\r | |
1691 | Status = FlashFdErase (\r | |
1692 | (UINTN) FwhInstance->FvBase[0],\r | |
1693 | FwhInstance,\r | |
1694 | FwVolHeader->BlockMap->Length\r | |
1695 | );\r | |
1696 | \r | |
1697 | HeaderLength = (UINTN) FwVolHeader->HeaderLength;\r | |
1698 | \r | |
1699 | Status = FlashFdWrite (\r | |
1700 | (UINTN) FwhInstance->FvBase[0],\r | |
1701 | FwhInstance,\r | |
1702 | (UINTN *) &HeaderLength,\r | |
1703 | (UINT8 *) FwVolHeader\r | |
1704 | );\r | |
1705 | \r | |
1706 | FwVolHeader->HeaderLength = (UINT16) HeaderLength;\r | |
1707 | \r | |
1708 | DEBUG ((EFI_D_ERROR, "Fvb (0x%x): FV header invalid, write back - %r\n", (UINTN) FwhInstance->FvBase[0], Status));\r | |
1709 | }\r | |
1710 | }\r | |
1711 | //\r | |
1712 | // The total number of blocks in the FV.\r | |
1713 | //\r | |
1714 | FwhInstance->NumOfBlocks = NumOfBlocks;\r | |
1715 | \r | |
1716 | //\r | |
1717 | // Add a FVB Protocol Instance\r | |
1718 | //\r | |
1719 | Status = gBS->AllocatePool (\r | |
1720 | EfiRuntimeServicesData,\r | |
1721 | sizeof (EFI_FW_VOL_BLOCK_DEVICE),\r | |
1722 | &FvbDevice\r | |
1723 | );\r | |
1724 | ASSERT_EFI_ERROR (Status);\r | |
1725 | \r | |
1726 | CopyMem (FvbDevice, &mFvbDeviceTemplate, sizeof (EFI_FW_VOL_BLOCK_DEVICE));\r | |
1727 | \r | |
1728 | FvbDevice->Instance = mFvbModuleGlobal->NumFv;\r | |
1729 | mFvbModuleGlobal->NumFv++;\r | |
1730 | \r | |
1731 | //\r | |
1732 | // Set up the devicepath\r | |
1733 | //\r | |
1734 | FvbDevice->DevicePath.MemMapDevPath.StartingAddress = BaseAddress;\r | |
1735 | FvbDevice->DevicePath.MemMapDevPath.EndingAddress = BaseAddress + (FwVolHeader->FvLength - 1);\r | |
1736 | \r | |
1737 | //\r | |
1738 | // Find a handle with a matching device path that has supports FW Block protocol\r | |
1739 | //\r | |
1740 | TempFwbDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) &TempFvbDevicePathData;\r | |
1741 | CopyMem (TempFwbDevicePath, &FvbDevice->DevicePath, sizeof (FV_DEVICE_PATH));\r | |
1742 | Status = gBS->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid, &TempFwbDevicePath, &FwbHandle);\r | |
1743 | if (EFI_ERROR (Status)) {\r | |
1744 | //\r | |
1745 | // LocateDevicePath fails so install a new interface and device path\r | |
1746 | //\r | |
1747 | FwbHandle = NULL;\r | |
1748 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
1749 | &FwbHandle,\r | |
1750 | &gEfiFirmwareVolumeBlockProtocolGuid,\r | |
1751 | &FvbDevice->FwVolBlockInstance,\r | |
1752 | &gEfiDevicePathProtocolGuid,\r | |
1753 | &FvbDevice->DevicePath,\r | |
1754 | NULL\r | |
1755 | );\r | |
1756 | ASSERT_EFI_ERROR (Status);\r | |
1757 | } else if (EfiIsDevicePathEnd (TempFwbDevicePath)) {\r | |
1758 | //\r | |
1759 | // Device allready exists, so reinstall the FVB protocol\r | |
1760 | //\r | |
1761 | Status = gBS->HandleProtocol (\r | |
1762 | FwbHandle,\r | |
1763 | &gEfiFirmwareVolumeBlockProtocolGuid,\r | |
1764 | &OldFwbInterface\r | |
1765 | );\r | |
1766 | ASSERT_EFI_ERROR (Status);\r | |
1767 | \r | |
1768 | Status = gBS->ReinstallProtocolInterface (\r | |
1769 | FwbHandle,\r | |
1770 | &gEfiFirmwareVolumeBlockProtocolGuid,\r | |
1771 | OldFwbInterface,\r | |
1772 | &FvbDevice->FwVolBlockInstance\r | |
1773 | );\r | |
1774 | ASSERT_EFI_ERROR (Status);\r | |
1775 | \r | |
1776 | } else {\r | |
1777 | //\r | |
1778 | // There was a FVB protocol on an End Device Path node\r | |
1779 | //\r | |
1780 | ASSERT (FALSE);\r | |
1781 | }\r | |
1782 | //\r | |
1783 | // Install FVB Extension Protocol on the same handle\r | |
1784 | //\r | |
1785 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
1786 | &FwbHandle,\r | |
1787 | &gEfiFvbExtensionProtocolGuid,\r | |
1788 | &FvbDevice->FvbExtension,\r | |
1789 | &gEfiAlternateFvBlockGuid,\r | |
1790 | NULL,\r | |
1791 | NULL\r | |
1792 | );\r | |
1793 | \r | |
1794 | ASSERT_EFI_ERROR (Status);\r | |
1795 | \r | |
1796 | FwhInstance = (EFI_FW_VOL_INSTANCE *)\r | |
1797 | (\r | |
1798 | (UINTN) ((UINT8 *) FwhInstance) + FwVolHeader->HeaderLength +\r | |
1799 | (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))\r | |
1800 | );\r | |
1801 | } while (TRUE);\r | |
1802 | \r | |
1803 | //\r | |
1804 | // Allocate for scratch space, an intermediate buffer for FVB extention\r | |
1805 | //\r | |
1806 | Status = gBS->AllocatePool (\r | |
1807 | EfiRuntimeServicesData,\r | |
1808 | MaxLbaSize,\r | |
1809 | &mFvbModuleGlobal->FvbScratchSpace[FVB_PHYSICAL]\r | |
1810 | );\r | |
1811 | ASSERT_EFI_ERROR (Status);\r | |
1812 | \r | |
1813 | mFvbModuleGlobal->FvbScratchSpace[FVB_VIRTUAL] = mFvbModuleGlobal->FvbScratchSpace[FVB_PHYSICAL];\r | |
1814 | \r | |
1815 | if (InstallSfsNotify) {\r | |
1816 | FvbInstallSfsNotify ();\r | |
1817 | }\r | |
1818 | return EFI_SUCCESS;\r | |
1819 | }\r |