]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/FwVolBlock/FwVolBlock.c
Add check to the allocated pool.
[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 if (FvAlignment < 8) {
454 FvAlignment = 8;
455 }
456 if ((UINTN)BaseAddress % FvAlignment != 0) {
457 //
458 // FvImage buffer is not at its required alignment.
459 //
460 return EFI_VOLUME_CORRUPTED;
461 }
462 //
463 // Allocate EFI_FW_VOL_BLOCK_DEVICE
464 //
465 FvbDev = AllocateCopyPool (sizeof (EFI_FW_VOL_BLOCK_DEVICE), &mFwVolBlock);
466 if (FvbDev == NULL) {
467 return EFI_OUT_OF_RESOURCES;
468 }
469
470 FvbDev->BaseAddress = BaseAddress;
471 FvbDev->FvbAttributes = FwVolHeader->Attributes;
472 FvbDev->FwVolBlockInstance.ParentHandle = ParentHandle;
473
474 //
475 // Init the block caching fields of the device
476 // First, count the number of blocks
477 //
478 FvbDev->NumBlocks = 0;
479 for (PtrBlockMapEntry = FwVolHeader->BlockMap;
480 PtrBlockMapEntry->NumBlocks != 0;
481 PtrBlockMapEntry++) {
482 FvbDev->NumBlocks += PtrBlockMapEntry->NumBlocks;
483 }
484 //
485 // Second, allocate the cache
486 //
487 FvbDev->LbaCache = AllocatePool (FvbDev->NumBlocks * sizeof (LBA_CACHE));
488 if (FvbDev->LbaCache == NULL) {
489 CoreFreePool (FvbDev);
490 return EFI_OUT_OF_RESOURCES;
491 }
492
493 //
494 // Last, fill in the cache with the linear address of the blocks
495 //
496 BlockIndex = 0;
497 LinearOffset = 0;
498 for (PtrBlockMapEntry = FwVolHeader->BlockMap;
499 PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {
500 for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {
501 FvbDev->LbaCache[BlockIndex].Base = LinearOffset;
502 FvbDev->LbaCache[BlockIndex].Length = PtrBlockMapEntry->Length;
503 LinearOffset += PtrBlockMapEntry->Length;
504 BlockIndex++;
505 }
506 }
507
508 //
509 // Judget whether FV name guid is produced in Fv extension header
510 //
511 if (FwVolHeader->ExtHeaderOffset == 0) {
512 //
513 // FV does not contains extension header, then produce MEMMAP_DEVICE_PATH
514 //
515 FvbDev->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) AllocateCopyPool (sizeof (FV_MEMMAP_DEVICE_PATH), &mFvMemmapDevicePathTemplate);
516 if (FvbDev->DevicePath == NULL) {
517 FreePool (FvbDev);
518 return EFI_OUT_OF_RESOURCES;
519 }
520 ((FV_MEMMAP_DEVICE_PATH *) FvbDev->DevicePath)->MemMapDevPath.StartingAddress = BaseAddress;
521 ((FV_MEMMAP_DEVICE_PATH *) FvbDev->DevicePath)->MemMapDevPath.EndingAddress = BaseAddress + FwVolHeader->FvLength - 1;
522 } else {
523 //
524 // FV contains extension header, then produce MEDIA_FW_VOL_DEVICE_PATH
525 //
526 FvbDev->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) AllocateCopyPool (sizeof (FV_PIWG_DEVICE_PATH), &mFvPIWGDevicePathTemplate);
527 if (FvbDev->DevicePath == NULL) {
528 FreePool (FvbDev);
529 return EFI_OUT_OF_RESOURCES;
530 }
531 CopyGuid (
532 &((FV_PIWG_DEVICE_PATH *)FvbDev->DevicePath)->FvDevPath.FvName,
533 (GUID *)(UINTN)(BaseAddress + FwVolHeader->ExtHeaderOffset)
534 );
535 }
536
537 //
538 //
539 // Attach FvVolBlock Protocol to new handle
540 //
541 Status = CoreInstallMultipleProtocolInterfaces (
542 &FvbDev->Handle,
543 &gEfiFirmwareVolumeBlockProtocolGuid, &FvbDev->FwVolBlockInstance,
544 &gEfiDevicePathProtocolGuid, FvbDev->DevicePath,
545 NULL
546 );
547
548 //
549 // If they want the handle back, set it.
550 //
551 if (FvProtocol != NULL) {
552 *FvProtocol = FvbDev->Handle;
553 }
554
555 return Status;
556 }
557
558
559
560 /**
561 This routine consumes FV hobs and produces instances of FW_VOL_BLOCK_PROTOCOL as appropriate.
562
563 @param ImageHandle The image handle.
564 @param SystemTable The system table.
565
566 @retval EFI_SUCCESS Successfully initialized firmware volume block
567 driver.
568
569 **/
570 EFI_STATUS
571 EFIAPI
572 FwVolBlockDriverInit (
573 IN EFI_HANDLE ImageHandle,
574 IN EFI_SYSTEM_TABLE *SystemTable
575 )
576 {
577 EFI_PEI_HOB_POINTERS FvHob;
578
579 //
580 // Core Needs Firmware Volumes to function
581 //
582 FvHob.Raw = GetHobList ();
583 while ((FvHob.Raw = GetNextHob (EFI_HOB_TYPE_FV, FvHob.Raw)) != NULL) {
584 //
585 // Produce an FVB protocol for it
586 //
587 ProduceFVBProtocolOnBuffer (FvHob.FirmwareVolume->BaseAddress, FvHob.FirmwareVolume->Length, NULL, NULL);
588 FvHob.Raw = GET_NEXT_HOB (FvHob);
589 }
590
591 return EFI_SUCCESS;
592 }
593
594
595
596 /**
597 This DXE service routine is used to process a firmware volume. In
598 particular, it can be called by BDS to process a single firmware
599 volume found in a capsule.
600
601 @param FvHeader pointer to a firmware volume header
602 @param Size the size of the buffer pointed to by FvHeader
603 @param FVProtocolHandle the handle on which a firmware volume protocol
604 was produced for the firmware volume passed in.
605
606 @retval EFI_OUT_OF_RESOURCES if an FVB could not be produced due to lack of
607 system resources
608 @retval EFI_VOLUME_CORRUPTED if the volume was corrupted
609 @retval EFI_SUCCESS a firmware volume protocol was produced for the
610 firmware volume
611
612 **/
613 EFI_STATUS
614 EFIAPI
615 CoreProcessFirmwareVolume (
616 IN VOID *FvHeader,
617 IN UINTN Size,
618 OUT EFI_HANDLE *FVProtocolHandle
619 )
620 {
621 VOID *Ptr;
622 EFI_STATUS Status;
623
624 *FVProtocolHandle = NULL;
625 Status = ProduceFVBProtocolOnBuffer (
626 (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,
627 (UINT64)Size,
628 NULL,
629 FVProtocolHandle
630 );
631 //
632 // Since in our implementation we use register-protocol-notify to put a
633 // FV protocol on the FVB protocol handle, we can't directly verify that
634 // the FV protocol was produced. Therefore here we will check the handle
635 // and make sure an FV protocol is on it. This indicates that all went
636 // well. Otherwise we have to assume that the volume was corrupted
637 // somehow.
638 //
639 if (!EFI_ERROR(Status)) {
640 Ptr = NULL;
641 Status = CoreHandleProtocol (*FVProtocolHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **) &Ptr);
642 if (EFI_ERROR(Status) || (Ptr == NULL)) {
643 return EFI_VOLUME_CORRUPTED;
644 }
645 return EFI_SUCCESS;
646 }
647 return Status;
648 }
649
650
651