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