]>
Commit | Line | Data |
---|---|---|
670d495b | 1 | /** @file\r |
2 | Firmware Block Services to support emulating non-volatile variables\r | |
3 | by pretending that a memory buffer is storage for the NV variables.\r | |
4 | \r | |
29a3f139 | 5 | Copyright (c) 2006 - 2010, Intel Corporation\r |
670d495b | 6 | All rights reserved. This program and the accompanying materials\r |
7 | are licensed and made available under the terms and conditions of the BSD License\r | |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | #include "PiDxe.h"\r | |
17 | #include <Guid/EventGroup.h>\r | |
18 | #include <Guid/SystemNvDataGuid.h>\r | |
19 | #include <Guid/VariableFormat.h>\r | |
20 | \r | |
21 | #include <Protocol/FirmwareVolumeBlock.h>\r | |
22 | #include <Protocol/DevicePath.h>\r | |
23 | \r | |
24 | #include <Library/UefiLib.h>\r | |
25 | #include <Library/UefiDriverEntryPoint.h>\r | |
26 | #include <Library/BaseLib.h>\r | |
27 | #include <Library/UefiRuntimeLib.h>\r | |
28 | #include <Library/DebugLib.h>\r | |
29 | #include <Library/BaseMemoryLib.h>\r | |
30 | #include <Library/MemoryAllocationLib.h>\r | |
31 | #include <Library/UefiBootServicesTableLib.h>\r | |
32 | #include <Library/DevicePathLib.h>\r | |
33 | #include <Library/PcdLib.h>\r | |
34 | #include <Library/PlatformFvbLib.h>\r | |
35 | #include "Fvb.h"\r | |
36 | \r | |
37 | //\r | |
38 | // Virtual Address Change Event\r | |
39 | //\r | |
40 | // This is needed for runtime variable access.\r | |
41 | //\r | |
42 | EFI_EVENT mEmuVarsFvbAddrChangeEvent = NULL;\r | |
43 | \r | |
44 | //\r | |
45 | // This is the single instance supported by this driver. It\r | |
46 | // supports the FVB and Device Path protocols.\r | |
47 | //\r | |
48 | EFI_FW_VOL_BLOCK_DEVICE mEmuVarsFvb = {\r | |
49 | FVB_DEVICE_SIGNATURE,\r | |
50 | { // DevicePath\r | |
51 | {\r | |
52 | {\r | |
53 | HARDWARE_DEVICE_PATH,\r | |
54 | HW_MEMMAP_DP,\r | |
55 | {\r | |
56 | sizeof (MEMMAP_DEVICE_PATH),\r | |
57 | 0\r | |
58 | }\r | |
59 | },\r | |
60 | EfiMemoryMappedIO,\r | |
61 | 0,\r | |
62 | 0,\r | |
63 | },\r | |
64 | {\r | |
65 | END_DEVICE_PATH_TYPE,\r | |
66 | END_ENTIRE_DEVICE_PATH_SUBTYPE,\r | |
67 | {\r | |
68 | sizeof (EFI_DEVICE_PATH_PROTOCOL),\r | |
69 | 0\r | |
70 | }\r | |
71 | }\r | |
72 | },\r | |
73 | NULL, // BufferPtr\r | |
74 | FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize), // BlockSize\r | |
75 | 2 * FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize), // Size\r | |
76 | { // FwVolBlockInstance\r | |
77 | FvbProtocolGetAttributes,\r | |
78 | FvbProtocolSetAttributes,\r | |
79 | FvbProtocolGetPhysicalAddress,\r | |
80 | FvbProtocolGetBlockSize,\r | |
81 | FvbProtocolRead,\r | |
82 | FvbProtocolWrite,\r | |
83 | FvbProtocolEraseBlocks,\r | |
84 | NULL\r | |
85 | },\r | |
86 | };\r | |
87 | \r | |
88 | \r | |
89 | /**\r | |
90 | Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r | |
91 | \r | |
92 | This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r | |
93 | It converts pointer to new virtual address.\r | |
94 | \r | |
95 | @param Event Event whose notification function is being invoked.\r | |
96 | @param Context Pointer to the notification function's context.\r | |
97 | \r | |
98 | **/\r | |
99 | VOID\r | |
100 | EFIAPI\r | |
101 | FvbVirtualAddressChangeEvent (\r | |
102 | IN EFI_EVENT Event,\r | |
103 | IN VOID *Context\r | |
104 | )\r | |
105 | {\r | |
106 | EfiConvertPointer (0x0, &mEmuVarsFvb.BufferPtr);\r | |
107 | }\r | |
108 | \r | |
109 | \r | |
110 | //\r | |
111 | // FVB protocol APIs\r | |
112 | //\r | |
113 | \r | |
114 | /**\r | |
115 | The GetPhysicalAddress() function retrieves the base address of\r | |
116 | a memory-mapped firmware volume. This function should be called\r | |
117 | only for memory-mapped firmware volumes.\r | |
118 | \r | |
119 | @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL instance.\r | |
120 | \r | |
121 | @param Address Pointer to a caller-allocated\r | |
122 | EFI_PHYSICAL_ADDRESS that, on successful\r | |
123 | return from GetPhysicalAddress(), contains the\r | |
124 | base address of the firmware volume.\r | |
125 | \r | |
126 | @retval EFI_SUCCESS The firmware volume base address is returned.\r | |
127 | \r | |
128 | @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.\r | |
129 | \r | |
130 | **/\r | |
131 | EFI_STATUS\r | |
132 | EFIAPI\r | |
133 | FvbProtocolGetPhysicalAddress (\r | |
134 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
135 | OUT EFI_PHYSICAL_ADDRESS *Address\r | |
136 | )\r | |
137 | {\r | |
138 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
139 | \r | |
140 | FvbDevice = FVB_DEVICE_FROM_THIS (This);\r | |
141 | \r | |
142 | *Address = (EFI_PHYSICAL_ADDRESS)(UINTN) FvbDevice->BufferPtr;\r | |
143 | \r | |
144 | return EFI_SUCCESS;\r | |
145 | }\r | |
146 | \r | |
147 | \r | |
148 | /**\r | |
149 | The GetBlockSize() function retrieves the size of the requested\r | |
150 | block. It also returns the number of additional blocks with\r | |
151 | the identical size. The GetBlockSize() function is used to\r | |
152 | retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).\r | |
153 | \r | |
154 | \r | |
155 | @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL instance.\r | |
156 | \r | |
157 | @param Lba Indicates the block for which to return the size.\r | |
158 | \r | |
159 | @param BlockSize Pointer to a caller-allocated UINTN in which\r | |
160 | the size of the block is returned.\r | |
161 | \r | |
162 | @param NumberOfBlocks Pointer to a caller-allocated UINTN in\r | |
163 | which the number of consecutive blocks,\r | |
164 | starting with Lba, is returned. All\r | |
165 | blocks in this range have a size of\r | |
166 | BlockSize.\r | |
167 | \r | |
168 | \r | |
169 | @retval EFI_SUCCESS The firmware volume base address is returned.\r | |
170 | \r | |
171 | @retval EFI_INVALID_PARAMETER The requested LBA is out of range.\r | |
172 | \r | |
173 | **/\r | |
174 | EFI_STATUS\r | |
175 | EFIAPI\r | |
176 | FvbProtocolGetBlockSize (\r | |
177 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
178 | IN EFI_LBA Lba,\r | |
179 | OUT UINTN *BlockSize,\r | |
180 | OUT UINTN *NumberOfBlocks\r | |
181 | )\r | |
182 | {\r | |
183 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
184 | \r | |
185 | if (Lba > 1) {\r | |
186 | return EFI_INVALID_PARAMETER;\r | |
187 | }\r | |
188 | \r | |
189 | FvbDevice = FVB_DEVICE_FROM_THIS (This);\r | |
190 | \r | |
191 | *BlockSize = FvbDevice->BlockSize;\r | |
02e12420 | 192 | *NumberOfBlocks = (UINTN) (2 - (UINTN) Lba);\r |
670d495b | 193 | \r |
194 | return EFI_SUCCESS;\r | |
195 | }\r | |
196 | \r | |
197 | \r | |
198 | /**\r | |
199 | The GetAttributes() function retrieves the attributes and\r | |
200 | current settings of the block. Status Codes Returned\r | |
201 | \r | |
202 | @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL instance.\r | |
203 | \r | |
204 | @param Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the\r | |
205 | attributes and current settings are\r | |
206 | returned. Type EFI_FVB_ATTRIBUTES_2 is defined\r | |
207 | in EFI_FIRMWARE_VOLUME_HEADER.\r | |
208 | \r | |
209 | @retval EFI_SUCCESS The firmware volume attributes were\r | |
210 | returned.\r | |
211 | \r | |
212 | **/\r | |
213 | EFI_STATUS\r | |
214 | EFIAPI\r | |
215 | FvbProtocolGetAttributes (\r | |
216 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
217 | OUT EFI_FVB_ATTRIBUTES_2 *Attributes\r | |
218 | )\r | |
219 | {\r | |
220 | *Attributes =\r | |
221 | (EFI_FVB_ATTRIBUTES_2) (\r | |
222 | EFI_FVB2_READ_ENABLED_CAP |\r | |
223 | EFI_FVB2_READ_STATUS |\r | |
224 | EFI_FVB2_WRITE_ENABLED_CAP |\r | |
225 | EFI_FVB2_WRITE_STATUS |\r | |
226 | EFI_FVB2_ERASE_POLARITY\r | |
227 | );\r | |
228 | \r | |
229 | return EFI_SUCCESS;\r | |
230 | }\r | |
231 | \r | |
232 | \r | |
233 | /**\r | |
234 | The SetAttributes() function sets configurable firmware volume\r | |
235 | attributes and returns the new settings of the firmware volume.\r | |
236 | \r | |
237 | @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL instance.\r | |
238 | \r | |
239 | @param Attributes On input, Attributes is a pointer to\r | |
240 | EFI_FVB_ATTRIBUTES_2 that contains the\r | |
241 | desired firmware volume settings. On\r | |
242 | successful return, it contains the new\r | |
243 | settings of the firmware volume. Type\r | |
244 | EFI_FVB_ATTRIBUTES_2 is defined in\r | |
245 | EFI_FIRMWARE_VOLUME_HEADER.\r | |
246 | \r | |
247 | @retval EFI_SUCCESS The firmware volume attributes were returned.\r | |
248 | \r | |
249 | @retval EFI_INVALID_PARAMETER The attributes requested are in\r | |
250 | conflict with the capabilities\r | |
251 | as declared in the firmware\r | |
252 | volume header.\r | |
253 | \r | |
254 | **/\r | |
255 | EFI_STATUS\r | |
256 | EFIAPI\r | |
257 | FvbProtocolSetAttributes (\r | |
258 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
259 | IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes\r | |
260 | )\r | |
261 | {\r | |
262 | return EFI_ACCESS_DENIED;\r | |
263 | }\r | |
264 | \r | |
265 | \r | |
266 | /**\r | |
267 | Erases and initializes a firmware volume block.\r | |
268 | \r | |
269 | The EraseBlocks() function erases one or more blocks as denoted\r | |
270 | by the variable argument list. The entire parameter list of\r | |
271 | blocks must be verified before erasing any blocks. If a block is\r | |
272 | requested that does not exist within the associated firmware\r | |
273 | volume (it has a larger index than the last block of the\r | |
274 | firmware volume), the EraseBlocks() function must return the\r | |
275 | status code EFI_INVALID_PARAMETER without modifying the contents\r | |
276 | of the firmware volume. Implementations should be mindful that\r | |
277 | the firmware volume might be in the WriteDisabled state. If it\r | |
278 | is in this state, the EraseBlocks() function must return the\r | |
279 | status code EFI_ACCESS_DENIED without modifying the contents of\r | |
280 | the firmware volume. All calls to EraseBlocks() must be fully\r | |
281 | flushed to the hardware before the EraseBlocks() service\r | |
282 | returns.\r | |
283 | \r | |
284 | @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL\r | |
285 | instance.\r | |
286 | \r | |
287 | @param ... The variable argument list is a list of tuples.\r | |
288 | Each tuple describes a range of LBAs to erase\r | |
289 | and consists of the following:\r | |
290 | - An EFI_LBA that indicates the starting LBA\r | |
291 | - A UINTN that indicates the number of blocks to\r | |
292 | erase\r | |
293 | \r | |
294 | The list is terminated with an\r | |
295 | EFI_LBA_LIST_TERMINATOR. For example, the\r | |
296 | following indicates that two ranges of blocks\r | |
297 | (5-7 and 10-11) are to be erased: EraseBlocks\r | |
298 | (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);\r | |
299 | \r | |
300 | @retval EFI_SUCCESS The erase request was successfully\r | |
301 | completed.\r | |
302 | \r | |
303 | @retval EFI_ACCESS_DENIED The firmware volume is in the\r | |
304 | WriteDisabled state.\r | |
305 | @retval EFI_DEVICE_ERROR The block device is not functioning\r | |
306 | correctly and could not be written.\r | |
307 | The firmware device may have been\r | |
308 | partially erased.\r | |
309 | @retval EFI_INVALID_PARAMETER One or more of the LBAs listed\r | |
310 | in the variable argument list do\r | |
311 | not exist in the firmware volume. \r | |
312 | \r | |
313 | **/\r | |
314 | EFI_STATUS\r | |
315 | EFIAPI\r | |
316 | FvbProtocolEraseBlocks (\r | |
317 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
318 | ...\r | |
319 | )\r | |
320 | {\r | |
321 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
322 | VA_LIST args;\r | |
323 | EFI_LBA StartingLba;\r | |
324 | UINTN NumOfLba;\r | |
325 | UINT8 Erase;\r | |
326 | VOID *ErasePtr;\r | |
327 | UINTN EraseSize;\r | |
328 | \r | |
329 | FvbDevice = FVB_DEVICE_FROM_THIS (This);\r | |
330 | Erase = 0;\r | |
331 | \r | |
332 | VA_START (args, This);\r | |
333 | \r | |
334 | do {\r | |
335 | StartingLba = VA_ARG (args, EFI_LBA);\r | |
336 | if (StartingLba == EFI_LBA_LIST_TERMINATOR) {\r | |
337 | break;\r | |
338 | }\r | |
339 | \r | |
340 | NumOfLba = VA_ARG (args, UINT32);\r | |
341 | \r | |
342 | //\r | |
343 | // Check input parameters\r | |
344 | //\r | |
345 | if ((NumOfLba == 0) || (StartingLba > 1) || ((StartingLba + NumOfLba) > 2)) {\r | |
346 | VA_END (args);\r | |
347 | return EFI_INVALID_PARAMETER;\r | |
348 | }\r | |
349 | \r | |
350 | if (StartingLba == 0) {\r | |
351 | Erase = (UINT8) (Erase | BIT0);\r | |
352 | }\r | |
353 | if ((StartingLba + NumOfLba) == 2) {\r | |
354 | Erase = (UINT8) (Erase | BIT1);\r | |
355 | }\r | |
356 | \r | |
357 | } while (1);\r | |
358 | \r | |
359 | VA_END (args);\r | |
360 | \r | |
361 | ErasePtr = (UINT8*) FvbDevice->BufferPtr;\r | |
362 | EraseSize = 0;\r | |
363 | \r | |
364 | if ((Erase & BIT0) != 0) {\r | |
365 | EraseSize = EraseSize + FvbDevice->BlockSize;\r | |
366 | } else {\r | |
02e12420 | 367 | ErasePtr = (VOID*) ((UINT8*)ErasePtr + FvbDevice->BlockSize);\r |
670d495b | 368 | }\r |
369 | \r | |
370 | if ((Erase & BIT1) != 0) {\r | |
371 | EraseSize = EraseSize + FvbDevice->BlockSize;\r | |
372 | }\r | |
373 | \r | |
374 | if (EraseSize != 0) {\r | |
375 | SetMem (\r | |
376 | (VOID*) ErasePtr,\r | |
377 | EraseSize,\r | |
378 | ERASED_UINT8\r | |
379 | );\r | |
380 | }\r | |
381 | \r | |
382 | return EFI_SUCCESS;\r | |
383 | }\r | |
384 | \r | |
385 | \r | |
386 | /**\r | |
387 | Writes the specified number of bytes from the input buffer to the block.\r | |
388 | \r | |
389 | The Write() function writes the specified number of bytes from\r | |
390 | the provided buffer to the specified block and offset. If the\r | |
391 | firmware volume is sticky write, the caller must ensure that\r | |
392 | all the bits of the specified range to write are in the\r | |
393 | EFI_FVB_ERASE_POLARITY state before calling the Write()\r | |
394 | function, or else the result will be unpredictable. This\r | |
395 | unpredictability arises because, for a sticky-write firmware\r | |
396 | volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY\r | |
397 | state but cannot flip it back again. In general, before\r | |
398 | calling the Write() function, the caller should call the\r | |
399 | EraseBlocks() function first to erase the specified block to\r | |
400 | write. A block erase cycle will transition bits from the\r | |
401 | (NOT)EFI_FVB_ERASE_POLARITY state back to the\r | |
402 | EFI_FVB_ERASE_POLARITY state. Implementations should be\r | |
403 | mindful that the firmware volume might be in the WriteDisabled\r | |
404 | state. If it is in this state, the Write() function must\r | |
405 | return the status code EFI_ACCESS_DENIED without modifying the\r | |
406 | contents of the firmware volume. The Write() function must\r | |
407 | also prevent spanning block boundaries. If a write is\r | |
408 | requested that spans a block boundary, the write must store up\r | |
409 | to the boundary but not beyond. The output parameter NumBytes\r | |
410 | must be set to correctly indicate the number of bytes actually\r | |
411 | written. The caller must be aware that a write may be\r | |
412 | partially completed. All writes, partial or otherwise, must be\r | |
413 | fully flushed to the hardware before the Write() service\r | |
414 | returns.\r | |
415 | \r | |
416 | @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL instance.\r | |
417 | \r | |
418 | @param Lba The starting logical block index to write to.\r | |
419 | \r | |
420 | @param Offset Offset into the block at which to begin writing.\r | |
421 | \r | |
422 | @param NumBytes Pointer to a UINTN. At entry, *NumBytes\r | |
423 | contains the total size of the buffer. At\r | |
424 | exit, *NumBytes contains the total number of\r | |
425 | bytes actually written.\r | |
426 | \r | |
427 | @param Buffer Pointer to a caller-allocated buffer that\r | |
428 | contains the source for the write.\r | |
429 | \r | |
430 | @retval EFI_SUCCESS The firmware volume was written successfully.\r | |
431 | \r | |
432 | @retval EFI_BAD_BUFFER_SIZE The write was attempted across an\r | |
433 | LBA boundary. On output, NumBytes\r | |
434 | contains the total number of bytes\r | |
435 | actually written.\r | |
436 | \r | |
437 | @retval EFI_ACCESS_DENIED The firmware volume is in the\r | |
438 | WriteDisabled state.\r | |
439 | \r | |
440 | @retval EFI_DEVICE_ERROR The block device is malfunctioning\r | |
441 | and could not be written.\r | |
442 | \r | |
443 | \r | |
444 | **/\r | |
445 | EFI_STATUS\r | |
446 | EFIAPI\r | |
447 | FvbProtocolWrite (\r | |
448 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
449 | IN EFI_LBA Lba,\r | |
450 | IN UINTN Offset,\r | |
451 | IN OUT UINTN *NumBytes,\r | |
452 | IN UINT8 *Buffer\r | |
453 | )\r | |
454 | {\r | |
455 | \r | |
456 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
457 | UINT8 *FvbDataPtr;\r | |
458 | \r | |
459 | FvbDevice = FVB_DEVICE_FROM_THIS (This);\r | |
460 | \r | |
461 | if ((Lba > 1) || (Offset > FvbDevice->BlockSize)) {\r | |
462 | return EFI_INVALID_PARAMETER;\r | |
463 | }\r | |
464 | \r | |
465 | if ((Offset + *NumBytes) > FvbDevice->BlockSize) {\r | |
466 | *NumBytes = FvbDevice->BlockSize - Offset;\r | |
467 | }\r | |
468 | \r | |
469 | FvbDataPtr =\r | |
470 | (UINT8*) FvbDevice->BufferPtr +\r | |
02e12420 | 471 | MultU64x32 (Lba, (UINT32) FvbDevice->BlockSize) +\r |
670d495b | 472 | Offset;\r |
473 | \r | |
474 | if (*NumBytes > 0) {\r | |
475 | CopyMem (FvbDataPtr, Buffer, *NumBytes);\r | |
476 | PlatformFvbDataWritten (This, Lba);\r | |
477 | }\r | |
478 | \r | |
479 | return EFI_SUCCESS;\r | |
480 | }\r | |
481 | \r | |
482 | \r | |
483 | /**\r | |
484 | Reads the specified number of bytes into a buffer from the specified block.\r | |
485 | \r | |
486 | The Read() function reads the requested number of bytes from the\r | |
487 | requested block and stores them in the provided buffer.\r | |
488 | Implementations should be mindful that the firmware volume\r | |
489 | might be in the ReadDisabled state. If it is in this state,\r | |
490 | the Read() function must return the status code\r | |
491 | EFI_ACCESS_DENIED without modifying the contents of the\r | |
492 | buffer. The Read() function must also prevent spanning block\r | |
493 | boundaries. If a read is requested that would span a block\r | |
494 | boundary, the read must read up to the boundary but not\r | |
495 | beyond. The output parameter NumBytes must be set to correctly\r | |
496 | indicate the number of bytes actually read. The caller must be\r | |
497 | aware that a read may be partially completed.\r | |
498 | \r | |
499 | @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL instance.\r | |
500 | \r | |
501 | @param Lba The starting logical block index\r | |
502 | from which to read.\r | |
503 | \r | |
504 | @param Offset Offset into the block at which to begin reading.\r | |
505 | \r | |
506 | @param NumBytes Pointer to a UINTN. At entry, *NumBytes\r | |
507 | contains the total size of the buffer. At\r | |
508 | exit, *NumBytes contains the total number of\r | |
509 | bytes read.\r | |
510 | \r | |
511 | @param Buffer Pointer to a caller-allocated buffer that will\r | |
512 | be used to hold the data that is read.\r | |
513 | \r | |
514 | @retval EFI_SUCCESS The firmware volume was read successfully\r | |
515 | and contents are in Buffer.\r | |
516 | \r | |
517 | @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA\r | |
518 | boundary. On output, NumBytes\r | |
519 | contains the total number of bytes\r | |
520 | returned in Buffer.\r | |
521 | \r | |
522 | @retval EFI_ACCESS_DENIED The firmware volume is in the\r | |
523 | ReadDisabled state.\r | |
524 | \r | |
525 | @retval EFI_DEVICE_ERROR The block device is not\r | |
526 | functioning correctly and could\r | |
527 | not be read.\r | |
528 | \r | |
529 | **/\r | |
530 | EFI_STATUS\r | |
531 | EFIAPI\r | |
532 | FvbProtocolRead (\r | |
533 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
534 | IN EFI_LBA Lba,\r | |
535 | IN UINTN Offset,\r | |
536 | IN OUT UINTN *NumBytes,\r | |
537 | IN OUT UINT8 *Buffer\r | |
538 | )\r | |
539 | {\r | |
540 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
541 | UINT8 *FvbDataPtr;\r | |
542 | \r | |
543 | FvbDevice = FVB_DEVICE_FROM_THIS (This);\r | |
544 | \r | |
545 | if ((Lba > 1) || (Offset > FvbDevice->BlockSize)) {\r | |
546 | return EFI_INVALID_PARAMETER;\r | |
547 | }\r | |
548 | \r | |
549 | if ((Offset + *NumBytes) > FvbDevice->BlockSize) {\r | |
550 | *NumBytes = FvbDevice->BlockSize - Offset;\r | |
551 | }\r | |
552 | \r | |
553 | FvbDataPtr =\r | |
554 | (UINT8*) FvbDevice->BufferPtr +\r | |
02e12420 | 555 | MultU64x32 (Lba, (UINT32) FvbDevice->BlockSize) +\r |
670d495b | 556 | Offset;\r |
557 | \r | |
558 | if (*NumBytes > 0) {\r | |
559 | CopyMem (Buffer, FvbDataPtr, *NumBytes);\r | |
560 | }\r | |
561 | \r | |
562 | return EFI_SUCCESS;\r | |
563 | }\r | |
564 | \r | |
565 | \r | |
566 | /**\r | |
567 | Check the integrity of firmware volume header.\r | |
568 | \r | |
569 | @param[in] FwVolHeader - A pointer to a firmware volume header\r | |
570 | \r | |
571 | @retval EFI_SUCCESS - The firmware volume is consistent\r | |
572 | @retval EFI_NOT_FOUND - The firmware volume has been corrupted.\r | |
573 | \r | |
574 | **/\r | |
575 | EFI_STATUS\r | |
576 | ValidateFvHeader (\r | |
577 | IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader\r | |
578 | )\r | |
579 | {\r | |
580 | UINT16 Checksum;\r | |
581 | \r | |
582 | //\r | |
583 | // Verify the header revision, header signature, length\r | |
584 | // Length of FvBlock cannot be 2**64-1\r | |
585 | // HeaderLength cannot be an odd number\r | |
586 | //\r | |
587 | if ((FwVolHeader->Revision != EFI_FVH_REVISION) ||\r | |
588 | (FwVolHeader->Signature != EFI_FVH_SIGNATURE) ||\r | |
589 | (FwVolHeader->FvLength != EMU_FVB_SIZE) ||\r | |
590 | (FwVolHeader->HeaderLength != EMU_FV_HEADER_LENGTH)\r | |
591 | ) {\r | |
592 | DEBUG ((EFI_D_INFO, "EMU Variable FVB: Basic FV headers were invalid\n"));\r | |
593 | return EFI_NOT_FOUND;\r | |
594 | }\r | |
595 | //\r | |
596 | // Verify the header checksum\r | |
597 | //\r | |
598 | Checksum = CalculateSum16((VOID*) FwVolHeader, FwVolHeader->HeaderLength);\r | |
599 | \r | |
600 | if (Checksum != 0) {\r | |
601 | DEBUG ((EFI_D_INFO, "EMU Variable FVB: FV checksum was invalid\n"));\r | |
602 | return EFI_NOT_FOUND;\r | |
603 | }\r | |
604 | \r | |
605 | return EFI_SUCCESS;\r | |
606 | }\r | |
607 | \r | |
608 | \r | |
609 | /**\r | |
610 | Initializes the FV Header and Variable Store Header\r | |
611 | to support variable operations.\r | |
612 | \r | |
613 | @param[in] Ptr - Location to initialize the headers\r | |
614 | \r | |
615 | **/\r | |
616 | VOID\r | |
617 | InitializeFvAndVariableStoreHeaders (\r | |
618 | IN VOID *Ptr\r | |
619 | )\r | |
620 | {\r | |
621 | STATIC FVB_FV_HDR_AND_VARS_TEMPLATE FvAndVarTemplate = {\r | |
622 | { // EFI_FIRMWARE_VOLUME_HEADER FvHdr;\r | |
623 | // UINT8 ZeroVector[16];\r | |
624 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },\r | |
625 | \r | |
626 | // EFI_GUID FileSystemGuid;\r | |
627 | EFI_SYSTEM_NV_DATA_FV_GUID,\r | |
628 | \r | |
629 | // UINT64 FvLength;\r | |
630 | EMU_FVB_SIZE,\r | |
631 | \r | |
632 | // UINT32 Signature;\r | |
633 | EFI_FVH_SIGNATURE,\r | |
634 | \r | |
635 | // EFI_FVB_ATTRIBUTES_2 Attributes;\r | |
636 | 0x4feff,\r | |
637 | \r | |
638 | // UINT16 HeaderLength;\r | |
639 | EMU_FV_HEADER_LENGTH,\r | |
640 | \r | |
641 | // UINT16 Checksum;\r | |
642 | 0,\r | |
643 | \r | |
644 | // UINT16 ExtHeaderOffset;\r | |
645 | 0,\r | |
646 | \r | |
647 | // UINT8 Reserved[1];\r | |
648 | 0,\r | |
649 | \r | |
650 | // UINT8 Revision;\r | |
651 | EFI_FVH_REVISION,\r | |
652 | \r | |
653 | // EFI_FV_BLOCK_MAP_ENTRY BlockMap[1];\r | |
654 | { 2, // UINT32 NumBlocks;\r | |
655 | EMU_FVB_BLOCK_SIZE // UINT32 Length;\r | |
656 | }\r | |
657 | },\r | |
658 | // EFI_FV_BLOCK_MAP_ENTRY EndBlockMap;\r | |
659 | { 0, 0 }, // End of block map\r | |
660 | { // VARIABLE_STORE_HEADER VarHdr;\r | |
661 | // EFI_GUID Signature;\r | |
662 | EFI_VARIABLE_GUID,\r | |
663 | \r | |
664 | // UINT32 Size;\r | |
665 | (\r | |
666 | FixedPcdGet32 (PcdVariableStoreSize) -\r | |
667 | OFFSET_OF (FVB_FV_HDR_AND_VARS_TEMPLATE, VarHdr)\r | |
668 | ),\r | |
669 | \r | |
670 | // UINT8 Format;\r | |
671 | VARIABLE_STORE_FORMATTED,\r | |
672 | \r | |
673 | // UINT8 State;\r | |
674 | VARIABLE_STORE_HEALTHY,\r | |
675 | \r | |
676 | // UINT16 Reserved;\r | |
677 | 0,\r | |
678 | \r | |
679 | // UINT32 Reserved1;\r | |
680 | 0\r | |
681 | }\r | |
682 | };\r | |
683 | EFI_FIRMWARE_VOLUME_HEADER *Fv;\r | |
684 | \r | |
685 | //\r | |
686 | // Copy the template structure into the location\r | |
687 | //\r | |
688 | CopyMem (Ptr, (VOID*)&FvAndVarTemplate, sizeof (FvAndVarTemplate));\r | |
689 | \r | |
690 | //\r | |
691 | // Update the checksum for the FV header\r | |
692 | //\r | |
693 | Fv = (EFI_FIRMWARE_VOLUME_HEADER*) Ptr;\r | |
694 | Fv->Checksum = CalculateCheckSum16 (Ptr, Fv->HeaderLength);\r | |
695 | }\r | |
696 | \r | |
697 | \r | |
698 | /**\r | |
699 | Initializes the Fault Tolerant Write data structure\r | |
700 | \r | |
701 | This data structure is used by the Fault Tolerant Write driver.\r | |
702 | \r | |
703 | @param[in] Buffer - Location for the FTW data structure\r | |
704 | \r | |
705 | **/\r | |
706 | VOID\r | |
707 | InitializeFtwState (\r | |
708 | IN VOID *Buffer\r | |
709 | )\r | |
710 | {\r | |
711 | EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *Hdr;\r | |
712 | UINT32 TempCrc;\r | |
713 | STATIC EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER DefaultFtw = {\r | |
714 | EFI_SYSTEM_NV_DATA_FV_GUID, // EFI_GUID Signature;\r | |
715 | ERASED_UINT32, // UINT32 Crc;\r | |
716 | ERASED_BIT, // UINT8 WorkingBlockValid : 1;\r | |
717 | ERASED_BIT, // UINT8 WorkingBlockInvalid : 1;\r | |
718 | 0, // UINT8 Reserved : 6;\r | |
719 | { 0, 0, 0 }, // UINT8 Reserved3[3];\r | |
720 | FTW_WRITE_QUEUE_SIZE // UINT64 WriteQueueSize;\r | |
721 | };\r | |
722 | \r | |
723 | CopyMem (Buffer, (VOID*) &DefaultFtw, sizeof (DefaultFtw));\r | |
724 | \r | |
725 | Hdr = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER*) Buffer;\r | |
726 | \r | |
727 | //\r | |
728 | // Calculate checksum.\r | |
729 | //\r | |
730 | // The Crc, WorkingBlockValid and WorkingBlockInvalid bits should\r | |
731 | // be set to the erased state before computing the checksum.\r | |
732 | //\r | |
733 | gBS->CalculateCrc32 (Buffer, sizeof (DefaultFtw), &TempCrc);\r | |
734 | Hdr->Crc = TempCrc;\r | |
735 | \r | |
736 | //\r | |
737 | // Mark as valid.\r | |
738 | //\r | |
739 | Hdr->WorkingBlockValid = NOT_ERASED_BIT;\r | |
740 | }\r | |
741 | \r | |
742 | \r | |
743 | /**\r | |
744 | Main entry point.\r | |
745 | \r | |
746 | @param[in] ImageHandle The firmware allocated handle for the EFI image. \r | |
747 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
748 | \r | |
749 | @retval EFI_SUCCESS Successfully initialized.\r | |
750 | \r | |
751 | **/\r | |
752 | EFI_STATUS\r | |
753 | EFIAPI\r | |
754 | FvbInitialize (\r | |
755 | IN EFI_HANDLE ImageHandle,\r | |
756 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
757 | )\r | |
758 | {\r | |
759 | EFI_STATUS Status;\r | |
760 | VOID *Ptr;\r | |
761 | VOID *SubPtr;\r | |
762 | BOOLEAN Initialize;\r | |
763 | EFI_HANDLE Handle;\r | |
764 | EFI_PHYSICAL_ADDRESS Address;\r | |
765 | \r | |
766 | DEBUG ((EFI_D_INFO, "EMU Variable FVB Started\n"));\r | |
767 | \r | |
768 | //\r | |
769 | // Verify that the PCD's are set correctly.\r | |
770 | //\r | |
771 | if (\r | |
29a3f139 | 772 | (PcdGet32 (PcdVariableStoreSize) +\r |
773 | PcdGet32 (PcdFlashNvStorageFtwWorkingSize)\r | |
670d495b | 774 | ) >\r |
775 | EMU_FVB_BLOCK_SIZE\r | |
776 | ) {\r | |
777 | DEBUG ((EFI_D_ERROR, "EMU Variable invalid PCD sizes\n"));\r | |
778 | return EFI_INVALID_PARAMETER;\r | |
779 | }\r | |
780 | \r | |
781 | //\r | |
782 | // By default we will initialize the FV contents. But, if\r | |
783 | // PcdEmuVariableNvStoreReserved is non-zero, then we will\r | |
784 | // use this location for our buffer.\r | |
785 | //\r | |
786 | // If this location does not have a proper FV header, then\r | |
787 | // we will initialize it.\r | |
788 | //\r | |
789 | Initialize = TRUE;\r | |
790 | if (PcdGet64 (PcdEmuVariableNvStoreReserved) != 0) {\r | |
791 | Ptr = (VOID*)(UINTN) PcdGet64 (PcdEmuVariableNvStoreReserved);\r | |
792 | DEBUG ((\r | |
793 | EFI_D_INFO,\r | |
794 | "EMU Variable FVB: Using pre-reserved block at %p\n",\r | |
795 | Ptr\r | |
796 | ));\r | |
797 | Status = ValidateFvHeader (Ptr);\r | |
798 | if (!EFI_ERROR (Status)) {\r | |
799 | DEBUG ((EFI_D_INFO, "EMU Variable FVB: Found valid pre-existing FV\n"));\r | |
800 | Initialize = FALSE;\r | |
801 | }\r | |
802 | } else {\r | |
803 | Ptr = AllocateAlignedRuntimePages (\r | |
804 | EFI_SIZE_TO_PAGES (EMU_FVB_SIZE),\r | |
805 | SIZE_64KB\r | |
806 | );\r | |
807 | }\r | |
808 | \r | |
809 | mEmuVarsFvb.BufferPtr = Ptr;\r | |
810 | \r | |
811 | //\r | |
812 | // Initialize the main FV header and variable store header\r | |
813 | //\r | |
814 | if (Initialize) {\r | |
815 | SetMem (Ptr, EMU_FVB_SIZE, ERASED_UINT8);\r | |
816 | InitializeFvAndVariableStoreHeaders (Ptr);\r | |
817 | }\r | |
818 | PcdSet32 (PcdFlashNvStorageVariableBase, (UINT32)(UINTN) Ptr);\r | |
819 | \r | |
820 | //\r | |
821 | // Initialize the Fault Tolerant Write data area\r | |
822 | //\r | |
29a3f139 | 823 | SubPtr = (VOID*) ((UINT8*) Ptr + PcdGet32 (PcdVariableStoreSize));\r |
670d495b | 824 | if (Initialize) {\r |
825 | InitializeFtwState (SubPtr);\r | |
826 | }\r | |
827 | PcdSet32 (PcdFlashNvStorageFtwWorkingBase, (UINT32)(UINTN) SubPtr);\r | |
828 | \r | |
829 | //\r | |
830 | // Initialize the Fault Tolerant Write spare block\r | |
831 | //\r | |
832 | SubPtr = (VOID*) ((UINT8*) Ptr + EMU_FVB_BLOCK_SIZE);\r | |
833 | PcdSet32 (PcdFlashNvStorageFtwSpareBase, (UINT32)(UINTN) SubPtr);\r | |
834 | \r | |
835 | //\r | |
836 | // Setup FVB device path\r | |
837 | //\r | |
838 | Address = (EFI_PHYSICAL_ADDRESS)(UINTN) Ptr;\r | |
839 | mEmuVarsFvb.DevicePath.MemMapDevPath.StartingAddress = Address;\r | |
840 | mEmuVarsFvb.DevicePath.MemMapDevPath.EndingAddress = Address + EMU_FVB_SIZE - 1;\r | |
841 | \r | |
842 | //\r | |
843 | // Install the protocols\r | |
844 | //\r | |
845 | DEBUG ((EFI_D_INFO, "Installing FVB for EMU Variable support\n"));\r | |
846 | Handle = 0;\r | |
847 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
848 | &Handle,\r | |
849 | &gEfiFirmwareVolumeBlockProtocolGuid,\r | |
850 | &mEmuVarsFvb.FwVolBlockInstance,\r | |
851 | &gEfiDevicePathProtocolGuid,\r | |
852 | &mEmuVarsFvb.DevicePath,\r | |
853 | NULL\r | |
854 | );\r | |
855 | ASSERT_EFI_ERROR (Status);\r | |
856 | \r | |
857 | //\r | |
858 | // Register for the virtual address change event\r | |
859 | //\r | |
860 | Status = gBS->CreateEventEx (\r | |
861 | EVT_NOTIFY_SIGNAL,\r | |
862 | TPL_NOTIFY,\r | |
863 | FvbVirtualAddressChangeEvent,\r | |
864 | NULL,\r | |
865 | &gEfiEventVirtualAddressChangeGuid,\r | |
866 | &mEmuVarsFvbAddrChangeEvent\r | |
867 | );\r | |
868 | ASSERT_EFI_ERROR (Status);\r | |
869 | \r | |
870 | return EFI_SUCCESS;\r | |
871 | }\r | |
872 | \r | |
873 | \r |