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