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