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