]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/EdkFvbServiceLib/X64/Fvb.c
add ia32 and x64 direcotry for EdkFvbServiceLib
[mirror_edk2.git] / MdeModulePkg / Library / EdkFvbServiceLib / X64 / Fvb.c
CommitLineData
a190687a 1/**@file\r
2\r
3 Firmware Volume Block Protocol Runtime Abstraction\r
4\r
5 mFvbEntry is an array of Handle Fvb pairs. The Fvb Lib Instance matches the\r
6 index in the mFvbEntry array. This should be the same sequence as the FVB's\r
7 were described in the HOB. We have to remember the handle so we can tell if\r
8 the protocol has been reinstalled and it needs updateing.\r
9\r
10 If you are using any of these lib functions.you must first call FvbInitialize ().\r
11\r
12Copyright (c) 2006, Intel Corporation\r
13All rights reserved. This program and the accompanying materials\r
14are licensed and made available under the terms and conditions of the BSD License\r
15which accompanies this distribution. The full text of the license may be found at\r
16http://opensource.org/licenses/bsd-license.php\r
17\r
18THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
19WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
20\r
21**/\r
22\r
23//\r
24// Include common header file for this module.\r
25//\r
26#include "CommonHeader.h"\r
27\r
28#include "Fvb.h"\r
29\r
30//\r
31// Event for Exit Boot Services Callback\r
32//\r
33STATIC EFI_EVENT mExitBootServicesEvent = NULL;\r
34\r
35//\r
36// Lib will ASSERT if more FVB devices than this are added to the system.\r
37//\r
38STATIC FVB_ENTRY *mFvbEntry;\r
39STATIC EFI_EVENT mFvbRegistration;\r
40STATIC BOOLEAN mEfiFvbInitialized = FALSE;\r
41STATIC UINTN mFvbCount;\r
42\r
43/**\r
44 Check whether an address is runtime memory or not.\r
45\r
46 @param Address The Address being checked.\r
47\r
48 @retval TRUE The address is runtime memory.\r
49 @retval FALSE The address is not runtime memory.\r
50**/\r
51BOOLEAN\r
52IsRuntimeMemory (\r
53 IN VOID *Address\r
54 )\r
55{\r
56 EFI_STATUS Status;\r
57 UINT8 TmpMemoryMap[1];\r
58 UINTN MapKey;\r
59 UINTN DescriptorSize;\r
60 UINT32 DescriptorVersion;\r
61 UINTN MemoryMapSize;\r
62 EFI_MEMORY_DESCRIPTOR *MemoryMap;\r
63 EFI_MEMORY_DESCRIPTOR *MemoryMapPtr;\r
64 BOOLEAN IsRuntime;\r
65 UINTN Index;\r
66\r
67 IsRuntime = FALSE;\r
68\r
69 //\r
70 // Get System MemoryMapSize\r
71 //\r
72 MemoryMapSize = 1;\r
73 Status = gBS->GetMemoryMap (\r
74 &MemoryMapSize,\r
75 (EFI_MEMORY_DESCRIPTOR *)TmpMemoryMap,\r
76 &MapKey,\r
77 &DescriptorSize,\r
78 &DescriptorVersion\r
79 );\r
80 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
81 //\r
82 // Enlarge space here, because we will allocate pool now.\r
83 //\r
84 MemoryMapSize += EFI_PAGE_SIZE;\r
85 Status = gBS->AllocatePool (\r
86 EfiBootServicesData,\r
87 MemoryMapSize,\r
88 (VOID**)&MemoryMap\r
89 );\r
90 ASSERT_EFI_ERROR (Status);\r
91\r
92 //\r
93 // Get System MemoryMap\r
94 //\r
95 Status = gBS->GetMemoryMap (\r
96 &MemoryMapSize,\r
97 MemoryMap,\r
98 &MapKey,\r
99 &DescriptorSize,\r
100 &DescriptorVersion\r
101 );\r
102 ASSERT_EFI_ERROR (Status);\r
103\r
104 MemoryMapPtr = MemoryMap;\r
105 //\r
106 // Search the request Address\r
107 //\r
108 for (Index = 0; Index < (MemoryMapSize / DescriptorSize); Index++) {\r
109 if (((EFI_PHYSICAL_ADDRESS)(UINTN)Address >= MemoryMap->PhysicalStart) &&\r
110 ((EFI_PHYSICAL_ADDRESS)(UINTN)Address < MemoryMap->PhysicalStart\r
111 + LShiftU64 (MemoryMap->NumberOfPages, EFI_PAGE_SHIFT))) {\r
112 //\r
113 // Found it\r
114 //\r
115 if (MemoryMap->Attribute & EFI_MEMORY_RUNTIME) {\r
116 IsRuntime = TRUE;\r
117 }\r
118 break;\r
119 }\r
120 //\r
121 // Get next item\r
122 //\r
123 MemoryMap = (EFI_MEMORY_DESCRIPTOR *)((UINTN)MemoryMap + DescriptorSize);\r
124 }\r
125\r
126 //\r
127 // Done\r
128 //\r
129 gBS->FreePool (MemoryMapPtr);\r
130\r
131 return IsRuntime;\r
132}\r
133\r
134/**\r
135 Update mFvbEntry. Add new entry, or update existing entry if Fvb protocol is\r
136 reinstalled.\r
137\r
138 @param Event The Event that is being processed\r
139 @param Context Event Context\r
140\r
141**/\r
142STATIC\r
143VOID\r
144EFIAPI\r
145FvbNotificationEvent (\r
146 IN EFI_EVENT Event,\r
147 IN VOID *Context\r
148 )\r
149{\r
150 EFI_STATUS Status;\r
151 UINTN BufferSize;\r
152 EFI_HANDLE Handle;\r
153 UINTN Index;\r
154 UINTN UpdateIndex;\r
155\r
156 while (TRUE) {\r
157 BufferSize = sizeof (Handle);\r
158 Status = gBS->LocateHandle (\r
159 ByRegisterNotify,\r
160 &gEfiFirmwareVolumeBlockProtocolGuid,\r
161 mFvbRegistration,\r
162 &BufferSize,\r
163 &Handle\r
164 );\r
165 if (EFI_ERROR (Status)) {\r
166 //\r
167 // Exit Path of While Loop....\r
168 //\r
169 break;\r
170 }\r
171\r
172 UpdateIndex = MAX_FVB_COUNT;\r
173 for (Index = 0; Index < mFvbCount; Index++) {\r
174 if (mFvbEntry[Index].Handle == Handle) {\r
175 //\r
176 // If the handle is already in the table just update the protocol\r
177 //\r
178 UpdateIndex = Index;\r
179 break;\r
180 }\r
181 }\r
182\r
183 if (UpdateIndex == MAX_FVB_COUNT) {\r
184 //\r
185 // Use the next free slot for a new entry\r
186 //\r
187 UpdateIndex = mFvbCount++;\r
188 //\r
189 // Check the UpdateIndex whether exceed the maximum value.\r
190 //\r
191 ASSERT (UpdateIndex < MAX_FVB_COUNT);\r
192 mFvbEntry[UpdateIndex].Handle = Handle;\r
193 }\r
194 //\r
195 // The array does not have enough entries\r
196 //\r
197 ASSERT (UpdateIndex < MAX_FVB_COUNT);\r
198\r
199 //\r
200 // Get the interface pointer and if it's ours, skip it\r
201 //\r
202 Status = gBS->HandleProtocol (\r
203 Handle,\r
204 &gEfiFirmwareVolumeBlockProtocolGuid,\r
205 (VOID **) &mFvbEntry[UpdateIndex].Fvb\r
206 );\r
207 ASSERT_EFI_ERROR (Status);\r
208\r
209 Status = gBS->HandleProtocol (\r
210 Handle,\r
211 &gEfiFvbExtensionProtocolGuid,\r
212 (VOID **) &mFvbEntry[UpdateIndex].FvbExtension\r
213 );\r
214 if (Status != EFI_SUCCESS) {\r
215 mFvbEntry[UpdateIndex].FvbExtension = NULL;\r
216 }\r
217\r
218 //\r
219 // Check the FVB can be accessed in RUNTIME, The FVBs in FVB handle list comes\r
220 // from two way:\r
221 // 1) Dxe Core. (FVB information is transferred from FV HOB).\r
222 // 2) FVB driver.\r
223 // The FVB produced Dxe core is used for discoverying DXE driver and dispatch. These\r
224 // FVBs can only be accessed in boot time.\r
225 // FVB driver will discovery all FV in FLASH and these FVBs can be accessed in runtime.\r
226 // The FVB itself produced by FVB driver is allocated in runtime memory. So we can\r
227 // determine the what FVB can be accessed in RUNTIME by judging whether FVB itself is allocated\r
228 // in RUNTIME memory.\r
229 //\r
230 mFvbEntry[UpdateIndex].IsRuntimeAccess = IsRuntimeMemory (mFvbEntry[UpdateIndex].Fvb);\r
231 }\r
232}\r
233\r
234/**\r
235 Convert all pointers in mFvbEntry after ExitBootServices.\r
236\r
237 @param Event The Event that is being processed\r
238 @param Context Event Context\r
239\r
240**/\r
241VOID\r
242EFIAPI\r
243FvbVirtualAddressChangeNotifyEvent (\r
244 IN EFI_EVENT Event,\r
245 IN VOID *Context\r
246 )\r
247{\r
248 UINTN Index;\r
249 if (mFvbEntry != NULL) {\r
250 for (Index = 0; Index < MAX_FVB_COUNT; Index++) {\r
251 if (!mFvbEntry[Index].IsRuntimeAccess) {\r
252 continue;\r
253 }\r
254\r
255 if (NULL != mFvbEntry[Index].Fvb) {\r
256 EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].Fvb->GetBlockSize);\r
257 EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].Fvb->GetPhysicalAddress);\r
258 EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].Fvb->GetVolumeAttributes);\r
259 EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].Fvb->SetVolumeAttributes);\r
260 EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].Fvb->Read);\r
261 EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].Fvb->Write);\r
262 EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].Fvb->EraseBlocks);\r
263 EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].Fvb);\r
264 }\r
265\r
266 if (NULL != mFvbEntry[Index].FvbExtension) {\r
267 EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].FvbExtension->EraseFvbCustomBlock);\r
268 EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].FvbExtension);\r
269 }\r
270 }\r
271\r
272 EfiConvertPointer (0x0, (VOID **) &mFvbEntry);\r
273 }\r
274}\r
275\r
276/**\r
277 Library constructor function entry.\r
278\r
279 @param ImageHandle The handle of image who call this libary.\r
280 @param SystemTable The point of System Table.\r
281\r
282 @retval EFI_SUCESS Sucess construct this library.\r
283 @retval Others Fail to contruct this libary.\r
284**/\r
285EFI_STATUS\r
286EFIAPI\r
287FvbLibInitialize (\r
288 IN EFI_HANDLE ImageHandle,\r
289 IN EFI_SYSTEM_TABLE *SystemTable\r
290 )\r
291{\r
292 UINTN Status;\r
293 mFvbCount = 0;\r
294\r
295 Status = gBS->AllocatePool (\r
296 EfiRuntimeServicesData,\r
297 (UINTN) sizeof (FVB_ENTRY) * MAX_FVB_COUNT,\r
298 (VOID *) &mFvbEntry\r
299 );\r
300\r
301 if (EFI_ERROR (Status)) {\r
302 return Status;\r
303 }\r
304\r
305 ZeroMem (mFvbEntry, sizeof (FVB_ENTRY) * MAX_FVB_COUNT);\r
306\r
307 EfiCreateProtocolNotifyEvent (\r
308 &gEfiFirmwareVolumeBlockProtocolGuid,\r
309 TPL_CALLBACK,\r
310 FvbNotificationEvent,\r
311 NULL,\r
312 &mFvbRegistration\r
313 );\r
314\r
315 //\r
316 // Register SetVirtualAddressMap () notify function\r
317 //\r
318 Status = gBS->CreateEvent (\r
319 EVT_SIGNAL_EXIT_BOOT_SERVICES,\r
320 TPL_NOTIFY,\r
321 FvbVirtualAddressChangeNotifyEvent,\r
322 NULL,\r
323 &mExitBootServicesEvent\r
324 );\r
325 ASSERT_EFI_ERROR (Status);\r
326\r
327 mEfiFvbInitialized = TRUE;\r
328\r
329 return EFI_SUCCESS;\r
330}\r
331\r
332//\r
333// =============================================================================\r
334// The following functions wrap Fvb protocol in the Runtime Lib functions.\r
335// The Instance translates into Fvb instance. The Fvb order defined by HOBs and\r
336// thus the sequence of FVB protocol addition define Instance.\r
337//\r
338// EfiFvbInitialize () must be called before any of the following functions\r
339// must be called.\r
340// =============================================================================\r
341//\r
342\r
343/**\r
344 Reads specified number of bytes into a buffer from the specified block\r
345\r
346 @param Instance The FV instance to be read from.\r
347 @param Lba The logical block address to be read from\r
348 @param Offset Offset into the block at which to begin reading\r
349 @param NumBytes Pointer that on input contains the total size of\r
350 the buffer. On output, it contains the total number\r
351 of bytes read\r
352 @param Buffer Pointer to a caller allocated buffer that will be\r
353 used to hold the data read\r
354\r
355 @retval EFI_INVALID_PARAMETER Invalid parameter\r
356 @retval EFI_SUCESS Sucess to Read block\r
357 @retval Others Fail to read block\r
358**/\r
359EFI_STATUS\r
360EfiFvbReadBlock (\r
361 IN UINTN Instance,\r
362 IN EFI_LBA Lba,\r
363 IN UINTN Offset,\r
364 IN OUT UINTN *NumBytes,\r
365 IN UINT8 *Buffer\r
366 )\r
367{\r
368 if (Instance >= mFvbCount) {\r
369 return EFI_INVALID_PARAMETER;\r
370 }\r
371\r
372 if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
373 return EFI_INVALID_PARAMETER;\r
374 }\r
375\r
376 return mFvbEntry[Instance].Fvb->Read (mFvbEntry[Instance].Fvb, Lba, Offset, NumBytes, Buffer);\r
377}\r
378\r
379/**\r
380 Writes specified number of bytes from the input buffer to the block\r
381\r
382 @param Instance The FV instance to be written to\r
383 @param Lba The starting logical block index to write to\r
384 @param Offset Offset into the block at which to begin writing\r
385 @param NumBytes Pointer that on input contains the total size of\r
386 the buffer. On output, it contains the total number\r
387 of bytes actually written\r
388 @param Buffer Pointer to a caller allocated buffer that contains\r
389 the source for the write\r
390\r
391 @retval EFI_INVALID_PARAMETER Invalid parameter\r
392 @retval EFI_SUCESS Sucess to write block\r
393 @retval Others Fail to write block\r
394**/\r
395EFI_STATUS\r
396EfiFvbWriteBlock (\r
397 IN UINTN Instance,\r
398 IN EFI_LBA Lba,\r
399 IN UINTN Offset,\r
400 IN OUT UINTN *NumBytes,\r
401 IN UINT8 *Buffer\r
402 )\r
403{\r
404 if (Instance >= mFvbCount) {\r
405 return EFI_INVALID_PARAMETER;\r
406 }\r
407\r
408 if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
409 return EFI_INVALID_PARAMETER;\r
410 }\r
411\r
412 return mFvbEntry[Instance].Fvb->Write (mFvbEntry[Instance].Fvb, Lba, Offset, NumBytes, Buffer);\r
413}\r
414\r
415/**\r
416 Erases and initializes a firmware volume block\r
417\r
418 @param Instance The FV instance to be erased\r
419 @param Lba The logical block index to be erased\r
420\r
421 @retval EFI_INVALID_PARAMETER Invalid parameter\r
422 @retval EFI_SUCESS Sucess to erase block\r
423 @retval Others Fail to erase block\r
424**/\r
425EFI_STATUS\r
426EfiFvbEraseBlock (\r
427 IN UINTN Instance,\r
428 IN EFI_LBA Lba\r
429 )\r
430{\r
431 if (Instance >= mFvbCount) {\r
432 return EFI_INVALID_PARAMETER;\r
433 }\r
434\r
435 if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
436 return EFI_INVALID_PARAMETER;\r
437 }\r
438\r
439 return mFvbEntry[Instance].Fvb->EraseBlocks (mFvbEntry[Instance].Fvb, Lba, -1);\r
440}\r
441\r
442/**\r
443 Retrieves attributes, insures positive polarity of attribute bits, returns\r
444 resulting attributes in output parameter\r
445\r
446 @param Instance The FV instance whose attributes is going to be returned\r
447 @param Attributes Output buffer which contains attributes\r
448\r
449 @retval EFI_INVALID_PARAMETER Invalid parameter\r
450 @retval EFI_SUCESS Sucess to get Fv attribute\r
451 @retval Others Fail to get Fv attribute\r
452**/\r
453EFI_STATUS\r
454EfiFvbGetVolumeAttributes (\r
455 IN UINTN Instance,\r
456 OUT EFI_FVB_ATTRIBUTES *Attributes\r
457 )\r
458{\r
459 if (Instance >= mFvbCount) {\r
460 return EFI_INVALID_PARAMETER;\r
461 }\r
462\r
463 if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
464 return EFI_INVALID_PARAMETER;\r
465 }\r
466\r
467 return mFvbEntry[Instance].Fvb->GetVolumeAttributes (mFvbEntry[Instance].Fvb, Attributes);\r
468}\r
469\r
470/**\r
471 Modifies the current settings of the firmware volume according to the\r
472 input parameter, and returns the new setting of the volume\r
473\r
474 @param Instance The FV instance whose attributes is going to be\r
475 modified\r
476 @param Attributes On input, it is a pointer to EFI_FVB_ATTRIBUTES\r
477 containing the desired firmware volume settings.\r
478 On successful return, it contains the new settings\r
479 of the firmware volume\r
480\r
481 @retval EFI_INVALID_PARAMETER Invalid parameter\r
482 @retval EFI_SUCESS Sucess to set Fv attribute\r
483 @retval Others Fail to set Fv attribute\r
484**/\r
485EFI_STATUS\r
486EfiFvbSetVolumeAttributes (\r
487 IN UINTN Instance,\r
488 IN EFI_FVB_ATTRIBUTES Attributes\r
489 )\r
490{\r
491 if (Instance >= mFvbCount) {\r
492 return EFI_INVALID_PARAMETER;\r
493 }\r
494\r
495 if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
496 return EFI_INVALID_PARAMETER;\r
497 }\r
498\r
499 return mFvbEntry[Instance].Fvb->SetVolumeAttributes (mFvbEntry[Instance].Fvb, &Attributes);\r
500}\r
501\r
502/**\r
503 Retrieves the physical address of a memory mapped FV\r
504\r
505 @param Instance The FV instance whose base address is going to be\r
506 returned\r
507 @param BaseAddress Pointer to a caller allocated EFI_PHYSICAL_ADDRESS\r
508 that on successful return, contains the base address\r
509 of the firmware volume.\r
510\r
511 @retval EFI_INVALID_PARAMETER Invalid parameter\r
512 @retval EFI_SUCESS Sucess to get physical address\r
513 @retval Others Fail to get physical address\r
514**/\r
515EFI_STATUS\r
516EfiFvbGetPhysicalAddress (\r
517 IN UINTN Instance,\r
518 OUT EFI_PHYSICAL_ADDRESS *BaseAddress\r
519 )\r
520{\r
521 if (Instance >= mFvbCount) {\r
522 return EFI_INVALID_PARAMETER;\r
523 }\r
524\r
525 if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
526 return EFI_INVALID_PARAMETER;\r
527 }\r
528\r
529 return mFvbEntry[Instance].Fvb->GetPhysicalAddress (mFvbEntry[Instance].Fvb, BaseAddress);\r
530}\r
531\r
532/**\r
533 Retrieve the size of a logical block\r
534\r
535 @param Instance The FV instance whose block size is going to be\r
536 returned\r
537 @param Lba Indicates which block to return the size for.\r
538 @param BlockSize A pointer to a caller allocated UINTN in which\r
539 the size of the block is returned\r
540 @param NumOfBlocks a pointer to a caller allocated UINTN in which the\r
541 number of consecutive blocks starting with Lba is\r
542 returned. All blocks in this range have a size of\r
543 BlockSize\r
544\r
545 @retval EFI_INVALID_PARAMETER Invalid parameter\r
546 @retval EFI_SUCESS Sucess to get block size\r
547 @retval Others Fail to get block size\r
548**/\r
549EFI_STATUS\r
550EfiFvbGetBlockSize (\r
551 IN UINTN Instance,\r
552 IN EFI_LBA Lba,\r
553 OUT UINTN *BlockSize,\r
554 OUT UINTN *NumOfBlocks\r
555 )\r
556{\r
557 if (Instance >= mFvbCount) {\r
558 return EFI_INVALID_PARAMETER;\r
559 }\r
560\r
561 if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
562 return EFI_INVALID_PARAMETER;\r
563 }\r
564\r
565 return mFvbEntry[Instance].Fvb->GetBlockSize (mFvbEntry[Instance].Fvb, Lba, BlockSize, NumOfBlocks);\r
566}\r
567\r
568/**\r
569 Erases and initializes a specified range of a firmware volume\r
570\r
571 @param Instance The FV instance to be erased\r
572 @param StartLba The starting logical block index to be erased\r
573 @param OffsetStartLba Offset into the starting block at which to\r
574 begin erasing\r
575 @param LastLba The last logical block index to be erased\r
576 @param OffsetLastLba Offset into the last block at which to end erasing\r
577\r
578 @retval EFI_INVALID_PARAMETER Invalid parameter\r
579 @retval EFI_SUCESS Sucess to erase custom block range\r
580 @retval Others Fail to erase custom block range\r
581**/\r
582EFI_STATUS\r
583EfiFvbEraseCustomBlockRange (\r
584 IN UINTN Instance,\r
585 IN EFI_LBA StartLba,\r
586 IN UINTN OffsetStartLba,\r
587 IN EFI_LBA LastLba,\r
588 IN UINTN OffsetLastLba\r
589 )\r
590{\r
591 if (Instance >= mFvbCount) {\r
592 return EFI_INVALID_PARAMETER;\r
593 }\r
594\r
595 if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
596 return EFI_INVALID_PARAMETER;\r
597 }\r
598\r
599 if (!(mFvbEntry[Instance].FvbExtension)) {\r
600 return EFI_UNSUPPORTED;\r
601 }\r
602\r
603 if (!(mFvbEntry[Instance].FvbExtension->EraseFvbCustomBlock)) {\r
604 return EFI_UNSUPPORTED;\r
605 }\r
606\r
607 return mFvbEntry[Instance].FvbExtension->EraseFvbCustomBlock (\r
608 mFvbEntry[Instance].FvbExtension,\r
609 StartLba,\r
610 OffsetStartLba,\r
611 LastLba,\r
612 OffsetLastLba\r
613 );\r
614}\r