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