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