diff options
Diffstat (limited to 'MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c')
| -rw-r--r-- | MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c | 60 |
1 files changed, 37 insertions, 23 deletions
diff --git a/MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c b/MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c index 203d24411afc..1bac116c022a 100644 --- a/MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c +++ b/MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c @@ -67,11 +67,11 @@ SmmMemoryAllocationLibConstructor ( // // Get SMRAM range information // - Size = 0; + Size = 0; Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL); ASSERT (Status == EFI_BUFFER_TOO_SMALL); - mSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size); + mSmramRanges = (EFI_SMRAM_DESCRIPTOR *)AllocatePool (Size); ASSERT (mSmramRanges != NULL); Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges); @@ -114,14 +114,15 @@ SmmMemoryAllocationLibDestructor ( BOOLEAN EFIAPI BufferInSmram ( - IN VOID *Buffer + IN VOID *Buffer ) { UINTN Index; - for (Index = 0; Index < mSmramRangeCount; Index ++) { - if (((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer >= mSmramRanges[Index].CpuStart) && - ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer < (mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize))) { + for (Index = 0; Index < mSmramRangeCount; Index++) { + if (((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer >= mSmramRanges[Index].CpuStart) && + ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer < (mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize))) + { return TRUE; } } @@ -160,7 +161,8 @@ InternalAllocatePages ( if (EFI_ERROR (Status)) { return NULL; } - return (VOID *) (UINTN) Memory; + + return (VOID *)(UINTN)Memory; } /** @@ -261,14 +263,15 @@ FreePages ( // When Buffer is in SMRAM range, it should be allocated by gSmst->SmmAllocatePages() service. // So, gSmst->SmmFreePages() service is used to free it. // - Status = gSmst->SmmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages); + Status = gSmst->SmmFreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages); } else { // // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service. // So, gBS->FreePages() service is used to free it. // - Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages); + Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages); } + ASSERT_EFI_ERROR (Status); } @@ -313,23 +316,25 @@ InternalAllocateAlignedPages ( if (Pages == 0) { return NULL; } + if (Alignment > EFI_PAGE_SIZE) { // // Calculate the total number of pages since alignment is larger than page size. // - AlignmentMask = Alignment - 1; - RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment); + AlignmentMask = Alignment - 1; + RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment) - 1; // // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow. // ASSERT (RealPages > Pages); - Status = gSmst->SmmAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory); + Status = gSmst->SmmAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory); if (EFI_ERROR (Status)) { return NULL; } - AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask; - UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory); + + AlignedMemory = ((UINTN)Memory + AlignmentMask) & ~AlignmentMask; + UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN)Memory); if (UnalignedPages > 0) { // // Free first unaligned page(s). @@ -337,6 +342,7 @@ InternalAllocateAlignedPages ( Status = gSmst->SmmFreePages (Memory, UnalignedPages); ASSERT_EFI_ERROR (Status); } + Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages); UnalignedPages = RealPages - Pages - UnalignedPages; if (UnalignedPages > 0) { @@ -354,9 +360,11 @@ InternalAllocateAlignedPages ( if (EFI_ERROR (Status)) { return NULL; } - AlignedMemory = (UINTN) Memory; + + AlignedMemory = (UINTN)Memory; } - return (VOID *) AlignedMemory; + + return (VOID *)AlignedMemory; } /** @@ -478,14 +486,15 @@ FreeAlignedPages ( // When Buffer is in SMRAM range, it should be allocated by gSmst->SmmAllocatePages() service. // So, gSmst->SmmFreePages() service is used to free it. // - Status = gSmst->SmmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages); + Status = gSmst->SmmFreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages); } else { // // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service. // So, gBS->FreePages() service is used to free it. // - Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages); + Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages); } + ASSERT_EFI_ERROR (Status); } @@ -516,6 +525,7 @@ InternalAllocatePool ( if (EFI_ERROR (Status)) { Memory = NULL; } + return Memory; } @@ -611,6 +621,7 @@ InternalAllocateZeroPool ( if (Memory != NULL) { Memory = ZeroMem (Memory, AllocationSize); } + return Memory; } @@ -707,12 +718,13 @@ InternalAllocateCopyPool ( VOID *Memory; ASSERT (Buffer != NULL); - ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1)); + ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN)Buffer + 1)); Memory = InternalAllocatePool (PoolType, AllocationSize); if (Memory != NULL) { - Memory = CopyMem (Memory, Buffer, AllocationSize); + Memory = CopyMem (Memory, Buffer, AllocationSize); } + return Memory; } @@ -833,10 +845,11 @@ InternalReallocatePool ( VOID *NewBuffer; NewBuffer = InternalAllocateZeroPool (PoolType, NewSize); - if (NewBuffer != NULL && OldBuffer != NULL) { + if ((NewBuffer != NULL) && (OldBuffer != NULL)) { CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize)); FreePool (OldBuffer); } + return NewBuffer; } @@ -954,10 +967,10 @@ ReallocateReservedPool ( VOID EFIAPI FreePool ( - IN VOID *Buffer + IN VOID *Buffer ) { - EFI_STATUS Status; + EFI_STATUS Status; if (BufferInSmram (Buffer)) { // @@ -972,5 +985,6 @@ FreePool ( // Status = gBS->FreePool (Buffer); } + ASSERT_EFI_ERROR (Status); } |
