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