]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/FwVolBlock/FwVolBlock.c
retire gEfiFirmwareVolumeDispatcherProtocolGuid as its original design is used to...
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / FwVolBlock / FwVolBlock.c
1 /** @file
2 Implementations for Firmware Volume Block protocol.
3
4 It consumes FV HOBs and creates read-lonly Firmare Volume Block protocol
5 instances for each of them.
6
7 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17
18 #include "DxeMain.h"
19 #include "FwVolBlock.h"
20
21
22 EFI_FW_VOL_BLOCK_DEVICE mFwVolBlock = {
23 FVB_DEVICE_SIGNATURE,
24 NULL,
25 {
26 {
27 {
28 HARDWARE_DEVICE_PATH,
29 HW_MEMMAP_DP,
30 {
31 (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),
32 (UINT8)(sizeof (MEMMAP_DEVICE_PATH) >> 8)
33 }
34 },
35 EfiMemoryMappedIO,
36 (EFI_PHYSICAL_ADDRESS) 0,
37 (EFI_PHYSICAL_ADDRESS) 0,
38 },
39 {
40 END_DEVICE_PATH_TYPE,
41 END_ENTIRE_DEVICE_PATH_SUBTYPE,
42 {
43 END_DEVICE_PATH_LENGTH,
44 0
45 }
46 },
47 },
48 {
49 FwVolBlockGetAttributes,
50 (EFI_FVB_SET_ATTRIBUTES)FwVolBlockSetAttributes,
51 FwVolBlockGetPhysicalAddress,
52 FwVolBlockGetBlockSize,
53 FwVolBlockReadBlock,
54 (EFI_FVB_WRITE)FwVolBlockWriteBlock,
55 (EFI_FVB_ERASE_BLOCKS)FwVolBlockEraseBlock,
56 NULL
57 },
58 0,
59 NULL,
60 0,
61 0
62 };
63
64
65
66 /**
67 Retrieves Volume attributes. No polarity translations are done.
68
69 @param This Calling context
70 @param Attributes output buffer which contains attributes
71
72 @retval EFI_SUCCESS The firmware volume attributes were returned.
73
74 **/
75 EFI_STATUS
76 EFIAPI
77 FwVolBlockGetAttributes (
78 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
79 OUT EFI_FVB_ATTRIBUTES_2 *Attributes
80 )
81 {
82 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
83
84 FvbDevice = FVB_DEVICE_FROM_THIS (This);
85
86 //
87 // Since we are read only, it's safe to get attributes data from our in-memory copy.
88 //
89 *Attributes = FvbDevice->FvbAttributes;
90
91 return EFI_SUCCESS;
92 }
93
94
95
96 /**
97 Modifies the current settings of the firmware volume according to the input parameter.
98
99 @param This Calling context
100 @param Attributes input buffer which contains attributes
101
102 @retval EFI_SUCCESS The firmware volume attributes were returned.
103 @retval EFI_INVALID_PARAMETER The attributes requested are in conflict with
104 the capabilities as declared in the firmware
105 volume header.
106 @retval EFI_UNSUPPORTED Not supported.
107
108 **/
109 EFI_STATUS
110 EFIAPI
111 FwVolBlockSetAttributes (
112 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
113 IN CONST EFI_FVB_ATTRIBUTES_2 *Attributes
114 )
115 {
116 return EFI_UNSUPPORTED;
117 }
118
119
120
121 /**
122 The EraseBlock() function erases one or more blocks as denoted by the
123 variable argument list. The entire parameter list of blocks must be verified
124 prior to erasing any blocks. If a block is requested that does not exist
125 within the associated firmware volume (it has a larger index than the last
126 block of the firmware volume), the EraseBlock() function must return
127 EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.
128
129 @param This Calling context
130 @param ... Starting LBA followed by Number of Lba to erase.
131 a -1 to terminate the list.
132
133 @retval EFI_SUCCESS The erase request was successfully completed.
134 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled
135 state.
136 @retval EFI_DEVICE_ERROR The block device is not functioning correctly
137 and could not be written. The firmware device
138 may have been partially erased.
139 @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the variable
140 argument list do
141 @retval EFI_UNSUPPORTED Not supported.
142
143 **/
144 EFI_STATUS
145 EFIAPI
146 FwVolBlockEraseBlock (
147 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
148 ...
149 )
150 {
151 return EFI_UNSUPPORTED;
152 }
153
154
155
156 /**
157 Read the specified number of bytes from the block to the input buffer.
158
159 @param This Indicates the calling context.
160 @param Lba The starting logical block index to read.
161 @param Offset Offset into the block at which to begin reading.
162 @param NumBytes Pointer to a UINT32. At entry, *NumBytes
163 contains the total size of the buffer. At exit,
164 *NumBytes contains the total number of bytes
165 actually read.
166 @param Buffer Pinter to a caller-allocated buffer that
167 contains the destine for the read.
168
169 @retval EFI_SUCCESS The firmware volume was read successfully.
170 @retval EFI_BAD_BUFFER_SIZE The read was attempted across an LBA boundary.
171 @retval EFI_ACCESS_DENIED Access denied.
172 @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not
173 be read.
174
175 **/
176 EFI_STATUS
177 EFIAPI
178 FwVolBlockReadBlock (
179 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
180 IN CONST EFI_LBA Lba,
181 IN CONST UINTN Offset,
182 IN OUT UINTN *NumBytes,
183 IN OUT UINT8 *Buffer
184 )
185 {
186 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
187 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
188 UINT8 *LbaOffset;
189 UINTN LbaStart;
190 UINTN NumOfBytesRead;
191 UINTN LbaIndex;
192
193 FvbDevice = FVB_DEVICE_FROM_THIS (This);
194
195 //
196 // Check if This FW can be read
197 //
198 if ((FvbDevice->FvbAttributes & EFI_FVB2_READ_STATUS) == 0) {
199 return EFI_ACCESS_DENIED;
200 }
201
202 LbaIndex = (UINTN) Lba;
203 if (LbaIndex >= FvbDevice->NumBlocks) {
204 //
205 // Invalid Lba, read nothing.
206 //
207 *NumBytes = 0;
208 return EFI_BAD_BUFFER_SIZE;
209 }
210
211 if (Offset > FvbDevice->LbaCache[LbaIndex].Length) {
212 //
213 // all exceed boundry, read nothing.
214 //
215 *NumBytes = 0;
216 return EFI_BAD_BUFFER_SIZE;
217 }
218
219 NumOfBytesRead = *NumBytes;
220 if (Offset + NumOfBytesRead > FvbDevice->LbaCache[LbaIndex].Length) {
221 //
222 // partial exceed boundry, read data from current postion to end.
223 //
224 NumOfBytesRead = FvbDevice->LbaCache[LbaIndex].Length - Offset;
225 }
226
227 LbaStart = FvbDevice->LbaCache[LbaIndex].Base;
228 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)((UINTN) FvbDevice->BaseAddress);
229 LbaOffset = (UINT8 *) FwVolHeader + LbaStart + Offset;
230
231 //
232 // Perform read operation
233 //
234 CopyMem (Buffer, LbaOffset, NumOfBytesRead);
235
236 if (NumOfBytesRead == *NumBytes) {
237 return EFI_SUCCESS;
238 }
239
240 *NumBytes = NumOfBytesRead;
241 return EFI_BAD_BUFFER_SIZE;
242 }
243
244
245
246 /**
247 Writes the specified number of bytes from the input buffer to the block.
248
249 @param This Indicates the calling context.
250 @param Lba The starting logical block index to write to.
251 @param Offset Offset into the block at which to begin writing.
252 @param NumBytes Pointer to a UINT32. At entry, *NumBytes
253 contains the total size of the buffer. At exit,
254 *NumBytes contains the total number of bytes
255 actually written.
256 @param Buffer Pinter to a caller-allocated buffer that
257 contains the source for the write.
258
259 @retval EFI_SUCCESS The firmware volume was written successfully.
260 @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
261 On output, NumBytes contains the total number of
262 bytes actually written.
263 @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled
264 state.
265 @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not
266 be written.
267 @retval EFI_UNSUPPORTED Not supported.
268
269 **/
270 EFI_STATUS
271 EFIAPI
272 FwVolBlockWriteBlock (
273 IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
274 IN EFI_LBA Lba,
275 IN UINTN Offset,
276 IN OUT UINTN *NumBytes,
277 IN UINT8 *Buffer
278 )
279 {
280 return EFI_UNSUPPORTED;
281 }
282
283
284
285 /**
286 Get Fvb's base address.
287
288 @param This Indicates the calling context.
289 @param Address Fvb device base address.
290
291 @retval EFI_SUCCESS Successfully got Fvb's base address.
292 @retval EFI_UNSUPPORTED Not supported.
293
294 **/
295 EFI_STATUS
296 EFIAPI
297 FwVolBlockGetPhysicalAddress (
298 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
299 OUT EFI_PHYSICAL_ADDRESS *Address
300 )
301 {
302 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
303
304 FvbDevice = FVB_DEVICE_FROM_THIS (This);
305
306 if ((FvbDevice->FvbAttributes & EFI_FVB2_MEMORY_MAPPED) != 0) {
307 *Address = FvbDevice->BaseAddress;
308 return EFI_SUCCESS;
309 }
310
311 return EFI_UNSUPPORTED;
312 }
313
314
315
316 /**
317 Retrieves the size in bytes of a specific block within a firmware volume.
318
319 @param This Indicates the calling context.
320 @param Lba Indicates the block for which to return the
321 size.
322 @param BlockSize Pointer to a caller-allocated UINTN in which the
323 size of the block is returned.
324 @param NumberOfBlocks Pointer to a caller-allocated UINTN in which the
325 number of consecutive blocks starting with Lba
326 is returned. All blocks in this range have a
327 size of BlockSize.
328
329 @retval EFI_SUCCESS The firmware volume base address is returned.
330 @retval EFI_INVALID_PARAMETER The requested LBA is out of range.
331
332 **/
333 EFI_STATUS
334 EFIAPI
335 FwVolBlockGetBlockSize (
336 IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,
337 IN CONST EFI_LBA Lba,
338 IN OUT UINTN *BlockSize,
339 IN OUT UINTN *NumberOfBlocks
340 )
341 {
342 UINTN TotalBlocks;
343 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
344 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;
345 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
346
347 FvbDevice = FVB_DEVICE_FROM_THIS (This);
348
349 //
350 // Do parameter checking
351 //
352 if (Lba >= FvbDevice->NumBlocks) {
353 return EFI_INVALID_PARAMETER;
354 }
355
356 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)((UINTN)FvbDevice->BaseAddress);
357
358 PtrBlockMapEntry = FwVolHeader->BlockMap;
359
360 //
361 // Search the block map for the given block
362 //
363 TotalBlocks = 0;
364 while ((PtrBlockMapEntry->NumBlocks != 0) || (PtrBlockMapEntry->Length !=0 )) {
365 TotalBlocks += PtrBlockMapEntry->NumBlocks;
366 if (Lba < TotalBlocks) {
367 //
368 // We find the range
369 //
370 break;
371 }
372
373 PtrBlockMapEntry++;
374 }
375
376 *BlockSize = PtrBlockMapEntry->Length;
377 *NumberOfBlocks = TotalBlocks - (UINTN)Lba;
378
379 return EFI_SUCCESS;
380 }
381
382
383
384 /**
385 This routine produces a firmware volume block protocol on a given
386 buffer.
387
388 @param BaseAddress base address of the firmware volume image
389 @param Length length of the firmware volume image
390 @param ParentHandle handle of parent firmware volume, if this image
391 came from an FV image file in another firmware
392 volume (ala capsules)
393 @param FvProtocol Firmware volume block protocol produced.
394
395 @retval EFI_VOLUME_CORRUPTED Volume corrupted.
396 @retval EFI_OUT_OF_RESOURCES No enough buffer to be allocated.
397 @retval EFI_SUCCESS Successfully produced a FVB protocol on given
398 buffer.
399
400 **/
401 EFI_STATUS
402 ProduceFVBProtocolOnBuffer (
403 IN EFI_PHYSICAL_ADDRESS BaseAddress,
404 IN UINT64 Length,
405 IN EFI_HANDLE ParentHandle,
406 OUT EFI_HANDLE *FvProtocol OPTIONAL
407 )
408 {
409 EFI_STATUS Status;
410 EFI_FW_VOL_BLOCK_DEVICE *FvbDev;
411 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
412 UINTN BlockIndex;
413 UINTN BlockIndex2;
414 UINTN LinearOffset;
415 UINT32 FvAlignment;
416 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;
417
418 FvAlignment = 0;
419 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN) BaseAddress;
420 //
421 // Validate FV Header, if not as expected, return
422 //
423 if (FwVolHeader->Signature != EFI_FVH_SIGNATURE) {
424 return EFI_VOLUME_CORRUPTED;
425 }
426 //
427 // Get FvHeader alignment
428 //
429 FvAlignment = 1 << ((FwVolHeader->Attributes & EFI_FVB2_ALIGNMENT) >> 16);
430 if (FvAlignment < 8) {
431 FvAlignment = 8;
432 }
433 if ((UINTN)BaseAddress % FvAlignment != 0) {
434 //
435 // FvImage buffer is not at its required alignment.
436 //
437 return EFI_VOLUME_CORRUPTED;
438 }
439 //
440 // Allocate EFI_FW_VOL_BLOCK_DEVICE
441 //
442 FvbDev = AllocateCopyPool (sizeof (EFI_FW_VOL_BLOCK_DEVICE), &mFwVolBlock);
443 if (FvbDev == NULL) {
444 return EFI_OUT_OF_RESOURCES;
445 }
446
447 FvbDev->BaseAddress = BaseAddress;
448 FvbDev->FvbAttributes = FwVolHeader->Attributes;
449 FvbDev->FwVolBlockInstance.ParentHandle = ParentHandle;
450
451 //
452 // Init the block caching fields of the device
453 // First, count the number of blocks
454 //
455 FvbDev->NumBlocks = 0;
456 for (PtrBlockMapEntry = FwVolHeader->BlockMap;
457 PtrBlockMapEntry->NumBlocks != 0;
458 PtrBlockMapEntry++) {
459 FvbDev->NumBlocks += PtrBlockMapEntry->NumBlocks;
460 }
461 //
462 // Second, allocate the cache
463 //
464 FvbDev->LbaCache = AllocatePool (FvbDev->NumBlocks * sizeof (LBA_CACHE));
465 if (FvbDev->LbaCache == NULL) {
466 CoreFreePool (FvbDev);
467 return EFI_OUT_OF_RESOURCES;
468 }
469 //
470 // Last, fill in the cache with the linear address of the blocks
471 //
472 BlockIndex = 0;
473 LinearOffset = 0;
474 for (PtrBlockMapEntry = FwVolHeader->BlockMap;
475 PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {
476 for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {
477 FvbDev->LbaCache[BlockIndex].Base = LinearOffset;
478 FvbDev->LbaCache[BlockIndex].Length = PtrBlockMapEntry->Length;
479 LinearOffset += PtrBlockMapEntry->Length;
480 BlockIndex++;
481 }
482 }
483
484 //
485 // Set up the devicepath
486 //
487 FvbDev->DevicePath.MemMapDevPath.StartingAddress = BaseAddress;
488 FvbDev->DevicePath.MemMapDevPath.EndingAddress = BaseAddress + FwVolHeader->FvLength - 1;
489
490 //
491 //
492 // Attach FvVolBlock Protocol to new handle
493 //
494 Status = CoreInstallMultipleProtocolInterfaces (
495 &FvbDev->Handle,
496 &gEfiFirmwareVolumeBlockProtocolGuid, &FvbDev->FwVolBlockInstance,
497 &gEfiDevicePathProtocolGuid, &FvbDev->DevicePath,
498 NULL
499 );
500
501 //
502 // If they want the handle back, set it.
503 //
504 if (FvProtocol != NULL) {
505 *FvProtocol = FvbDev->Handle;
506 }
507
508 return Status;
509 }
510
511
512
513 /**
514 This routine consumes FV hobs and produces instances of FW_VOL_BLOCK_PROTOCOL as appropriate.
515
516 @param ImageHandle The image handle.
517 @param SystemTable The system table.
518
519 @retval EFI_SUCCESS Successfully initialized firmware volume block
520 driver.
521
522 **/
523 EFI_STATUS
524 EFIAPI
525 FwVolBlockDriverInit (
526 IN EFI_HANDLE ImageHandle,
527 IN EFI_SYSTEM_TABLE *SystemTable
528 )
529 {
530 EFI_PEI_HOB_POINTERS FvHob;
531
532 //
533 // Core Needs Firmware Volumes to function
534 //
535 FvHob.Raw = GetHobList ();
536 while ((FvHob.Raw = GetNextHob (EFI_HOB_TYPE_FV, FvHob.Raw)) != NULL) {
537 //
538 // Produce an FVB protocol for it
539 //
540 ProduceFVBProtocolOnBuffer (FvHob.FirmwareVolume->BaseAddress, FvHob.FirmwareVolume->Length, NULL, NULL);
541 FvHob.Raw = GET_NEXT_HOB (FvHob);
542 }
543
544 return EFI_SUCCESS;
545 }
546
547
548
549 /**
550 This DXE service routine is used to process a firmware volume. In
551 particular, it can be called by BDS to process a single firmware
552 volume found in a capsule.
553
554 @param FvHeader pointer to a firmware volume header
555 @param Size the size of the buffer pointed to by FvHeader
556 @param FVProtocolHandle the handle on which a firmware volume protocol
557 was produced for the firmware volume passed in.
558
559 @retval EFI_OUT_OF_RESOURCES if an FVB could not be produced due to lack of
560 system resources
561 @retval EFI_VOLUME_CORRUPTED if the volume was corrupted
562 @retval EFI_SUCCESS a firmware volume protocol was produced for the
563 firmware volume
564
565 **/
566 EFI_STATUS
567 EFIAPI
568 CoreProcessFirmwareVolume (
569 IN VOID *FvHeader,
570 IN UINTN Size,
571 OUT EFI_HANDLE *FVProtocolHandle
572 )
573 {
574 VOID *Ptr;
575 EFI_STATUS Status;
576
577 *FVProtocolHandle = NULL;
578 Status = ProduceFVBProtocolOnBuffer (
579 (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,
580 (UINT64)Size,
581 NULL,
582 FVProtocolHandle
583 );
584 //
585 // Since in our implementation we use register-protocol-notify to put a
586 // FV protocol on the FVB protocol handle, we can't directly verify that
587 // the FV protocol was produced. Therefore here we will check the handle
588 // and make sure an FV protocol is on it. This indicates that all went
589 // well. Otherwise we have to assume that the volume was corrupted
590 // somehow.
591 //
592 if (!EFI_ERROR(Status)) {
593 Ptr = NULL;
594 Status = CoreHandleProtocol (*FVProtocolHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **) &Ptr);
595 if (EFI_ERROR(Status) || (Ptr == NULL)) {
596 return EFI_VOLUME_CORRUPTED;
597 }
598 return EFI_SUCCESS;
599 }
600 return Status;
601 }
602
603
604