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