]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/EdkFvbServiceLib/Ia32/Fvb.c
Patch so the tree will compile
[mirror_edk2.git] / MdeModulePkg / Library / EdkFvbServiceLib / Ia32 / 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 "Fvb.h"
25
26 //
27 // Event for Exit Boot Services Callback
28 //
29 STATIC EFI_EVENT mExitBootServicesEvent = NULL;
30
31 //
32 // Lib will ASSERT if more FVB devices than this are added to the system.
33 //
34 STATIC FVB_ENTRY *mFvbEntry;
35 STATIC EFI_EVENT mFvbRegistration;
36 STATIC BOOLEAN mEfiFvbInitialized = FALSE;
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_EXIT_BOOT_SERVICES,
316 TPL_NOTIFY,
317 FvbVirtualAddressChangeNotifyEvent,
318 NULL,
319 &mExitBootServicesEvent
320 );
321 ASSERT_EFI_ERROR (Status);
322
323 mEfiFvbInitialized = TRUE;
324
325 return EFI_SUCCESS;
326 }
327
328 //
329 // =============================================================================
330 // The following functions wrap Fvb protocol in the Runtime Lib functions.
331 // The Instance translates into Fvb instance. The Fvb order defined by HOBs and
332 // thus the sequence of FVB protocol addition define Instance.
333 //
334 // EfiFvbInitialize () must be called before any of the following functions
335 // must be called.
336 // =============================================================================
337 //
338
339 /**
340 Reads specified number of bytes into a buffer from the specified block
341
342 @param Instance The FV instance to be read from.
343 @param Lba The logical block address to be read from
344 @param Offset Offset into the block at which to begin reading
345 @param NumBytes Pointer that on input contains the total size of
346 the buffer. On output, it contains the total number
347 of bytes read
348 @param Buffer Pointer to a caller allocated buffer that will be
349 used to hold the data read
350
351 @retval EFI_INVALID_PARAMETER Invalid parameter
352 @retval EFI_SUCESS Sucess to Read block
353 @retval Others Fail to read block
354 **/
355 EFI_STATUS
356 EfiFvbReadBlock (
357 IN UINTN Instance,
358 IN EFI_LBA Lba,
359 IN UINTN Offset,
360 IN OUT UINTN *NumBytes,
361 IN UINT8 *Buffer
362 )
363 {
364 if (Instance >= mFvbCount) {
365 return EFI_INVALID_PARAMETER;
366 }
367
368 if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {
369 return EFI_INVALID_PARAMETER;
370 }
371
372 return mFvbEntry[Instance].Fvb->Read (mFvbEntry[Instance].Fvb, Lba, Offset, NumBytes, Buffer);
373 }
374
375 /**
376 Writes specified number of bytes from the input buffer to the block
377
378 @param Instance The FV instance to be written to
379 @param Lba The starting logical block index to write to
380 @param Offset Offset into the block at which to begin writing
381 @param NumBytes Pointer that on input contains the total size of
382 the buffer. On output, it contains the total number
383 of bytes actually written
384 @param Buffer Pointer to a caller allocated buffer that contains
385 the source for the write
386
387 @retval EFI_INVALID_PARAMETER Invalid parameter
388 @retval EFI_SUCESS Sucess to write block
389 @retval Others Fail to write block
390 **/
391 EFI_STATUS
392 EfiFvbWriteBlock (
393 IN UINTN Instance,
394 IN EFI_LBA Lba,
395 IN UINTN Offset,
396 IN OUT UINTN *NumBytes,
397 IN UINT8 *Buffer
398 )
399 {
400 if (Instance >= mFvbCount) {
401 return EFI_INVALID_PARAMETER;
402 }
403
404 if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {
405 return EFI_INVALID_PARAMETER;
406 }
407
408 return mFvbEntry[Instance].Fvb->Write (mFvbEntry[Instance].Fvb, Lba, Offset, NumBytes, Buffer);
409 }
410
411 /**
412 Erases and initializes a firmware volume block
413
414 @param Instance The FV instance to be erased
415 @param Lba The logical block index to be erased
416
417 @retval EFI_INVALID_PARAMETER Invalid parameter
418 @retval EFI_SUCESS Sucess to erase block
419 @retval Others Fail to erase block
420 **/
421 EFI_STATUS
422 EfiFvbEraseBlock (
423 IN UINTN Instance,
424 IN EFI_LBA Lba
425 )
426 {
427 if (Instance >= mFvbCount) {
428 return EFI_INVALID_PARAMETER;
429 }
430
431 if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {
432 return EFI_INVALID_PARAMETER;
433 }
434
435 return mFvbEntry[Instance].Fvb->EraseBlocks (mFvbEntry[Instance].Fvb, Lba, -1);
436 }
437
438 /**
439 Retrieves attributes, insures positive polarity of attribute bits, returns
440 resulting attributes in output parameter
441
442 @param Instance The FV instance whose attributes is going to be returned
443 @param Attributes Output buffer which contains attributes
444
445 @retval EFI_INVALID_PARAMETER Invalid parameter
446 @retval EFI_SUCESS Sucess to get Fv attribute
447 @retval Others Fail to get Fv attribute
448 **/
449 EFI_STATUS
450 EfiFvbGetVolumeAttributes (
451 IN UINTN Instance,
452 OUT EFI_FVB_ATTRIBUTES *Attributes
453 )
454 {
455 if (Instance >= mFvbCount) {
456 return EFI_INVALID_PARAMETER;
457 }
458
459 if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {
460 return EFI_INVALID_PARAMETER;
461 }
462
463 return mFvbEntry[Instance].Fvb->GetAttributes (mFvbEntry[Instance].Fvb, Attributes);
464 }
465
466 /**
467 Modifies the current settings of the firmware volume according to the
468 input parameter, and returns the new setting of the volume
469
470 @param Instance The FV instance whose attributes is going to be
471 modified
472 @param Attributes On input, it is a pointer to EFI_FVB_ATTRIBUTES
473 containing the desired firmware volume settings.
474 On successful return, it contains the new settings
475 of the firmware volume
476
477 @retval EFI_INVALID_PARAMETER Invalid parameter
478 @retval EFI_SUCESS Sucess to set Fv attribute
479 @retval Others Fail to set Fv attribute
480 **/
481 EFI_STATUS
482 EfiFvbSetVolumeAttributes (
483 IN UINTN Instance,
484 IN EFI_FVB_ATTRIBUTES Attributes
485 )
486 {
487 if (Instance >= mFvbCount) {
488 return EFI_INVALID_PARAMETER;
489 }
490
491 if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {
492 return EFI_INVALID_PARAMETER;
493 }
494
495 return mFvbEntry[Instance].Fvb->SetAttributes (mFvbEntry[Instance].Fvb, &Attributes);
496 }
497
498 /**
499 Retrieves the physical address of a memory mapped FV
500
501 @param Instance The FV instance whose base address is going to be
502 returned
503 @param BaseAddress Pointer to a caller allocated EFI_PHYSICAL_ADDRESS
504 that on successful return, contains the base address
505 of the firmware volume.
506
507 @retval EFI_INVALID_PARAMETER Invalid parameter
508 @retval EFI_SUCESS Sucess to get physical address
509 @retval Others Fail to get physical address
510 **/
511 EFI_STATUS
512 EfiFvbGetPhysicalAddress (
513 IN UINTN Instance,
514 OUT EFI_PHYSICAL_ADDRESS *BaseAddress
515 )
516 {
517 if (Instance >= mFvbCount) {
518 return EFI_INVALID_PARAMETER;
519 }
520
521 if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {
522 return EFI_INVALID_PARAMETER;
523 }
524
525 return mFvbEntry[Instance].Fvb->GetPhysicalAddress (mFvbEntry[Instance].Fvb, BaseAddress);
526 }
527
528 /**
529 Retrieve the size of a logical block
530
531 @param Instance The FV instance whose block size is going to be
532 returned
533 @param Lba Indicates which block to return the size for.
534 @param BlockSize A pointer to a caller allocated UINTN in which
535 the size of the block is returned
536 @param NumOfBlocks a pointer to a caller allocated UINTN in which the
537 number of consecutive blocks starting with Lba is
538 returned. All blocks in this range have a size of
539 BlockSize
540
541 @retval EFI_INVALID_PARAMETER Invalid parameter
542 @retval EFI_SUCESS Sucess to get block size
543 @retval Others Fail to get block size
544 **/
545 EFI_STATUS
546 EfiFvbGetBlockSize (
547 IN UINTN Instance,
548 IN EFI_LBA Lba,
549 OUT UINTN *BlockSize,
550 OUT UINTN *NumOfBlocks
551 )
552 {
553 if (Instance >= mFvbCount) {
554 return EFI_INVALID_PARAMETER;
555 }
556
557 if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {
558 return EFI_INVALID_PARAMETER;
559 }
560
561 return mFvbEntry[Instance].Fvb->GetBlockSize (mFvbEntry[Instance].Fvb, Lba, BlockSize, NumOfBlocks);
562 }
563
564 /**
565 Erases and initializes a specified range of a firmware volume
566
567 @param Instance The FV instance to be erased
568 @param StartLba The starting logical block index to be erased
569 @param OffsetStartLba Offset into the starting block at which to
570 begin erasing
571 @param LastLba The last logical block index to be erased
572 @param OffsetLastLba Offset into the last block at which to end erasing
573
574 @retval EFI_INVALID_PARAMETER Invalid parameter
575 @retval EFI_SUCESS Sucess to erase custom block range
576 @retval Others Fail to erase custom block range
577 **/
578 EFI_STATUS
579 EfiFvbEraseCustomBlockRange (
580 IN UINTN Instance,
581 IN EFI_LBA StartLba,
582 IN UINTN OffsetStartLba,
583 IN EFI_LBA LastLba,
584 IN UINTN OffsetLastLba
585 )
586 {
587 if (Instance >= mFvbCount) {
588 return EFI_INVALID_PARAMETER;
589 }
590
591 if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {
592 return EFI_INVALID_PARAMETER;
593 }
594
595 if (!(mFvbEntry[Instance].FvbExtension)) {
596 return EFI_UNSUPPORTED;
597 }
598
599 if (!(mFvbEntry[Instance].FvbExtension->EraseFvbCustomBlock)) {
600 return EFI_UNSUPPORTED;
601 }
602
603 return mFvbEntry[Instance].FvbExtension->EraseFvbCustomBlock (
604 mFvbEntry[Instance].FvbExtension,
605 StartLba,
606 OffsetStartLba,
607 LastLba,
608 OffsetLastLba
609 );
610 }