ICE_TEA_BIOS/Insyde/InsydeCrPkg/CrServiceDxe/CrBdaMemManager.c
LCFC\AiXia.Jiang a870bff2f4 1.Frist commit
2022-09-30 14:59:06 +08:00

191 lines
5.1 KiB
C

/** @file
CrHookDxe driver for memory allocation module
;******************************************************************************
;* Copyright (c) 2012-2017, Insyde Software Corp. All Rights Reserved.
;*
;* You may not reproduce, distribute, publish, display, perform, modify, adapt,
;* transmit, broadcast, present, recite, release, license or otherwise exploit
;* any part of this publication in any form, by any means, without the prior
;* written permission of Insyde Software Corporation.
;*
;******************************************************************************
*/
#include "CrBdaMemManager.h"
#define MAX_ALLOC_ADDR 0x10000
#define DEFAULT_CR_MEM_SIZE_OF_PAGE 1
UINTN mCrMemStart = 0;
/**
Initialize Console Redirection memory manager.
@retval EFI_SUCCESS Console Redirection memory manager Initialize success
@retval others Console Redirection memory manager Initialize fail
**/
EFI_STATUS
InitCrEfiMemManager (
VOID
)
{
EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS MemoryBuf;
CR_EFI_MEM_MANAGER *CrEfiMemoryMgr;
CR_COMMON_AREA_INFO *CrOpromInfo;
//
// Allocate a page space as default CREfiMemory space below 64K
//
MemoryBuf = MAX_ALLOC_ADDR;
Status = gBS->AllocatePages (
AllocateMaxAddress,
EfiRuntimeServicesData,
DEFAULT_CR_MEM_SIZE_OF_PAGE,
&MemoryBuf
);
if (EFI_ERROR (Status)) {
return Status;
}
ZeroMem ((UINT8 *)(UINTN)MemoryBuf, (DEFAULT_CR_MEM_SIZE_OF_PAGE << 12));
mCrMemStart = (UINT32)MemoryBuf;
CrEfiMemoryMgr = (CR_EFI_MEM_MANAGER *) mCrMemStart;
//
// Fill CR Efi memory space Info
//
CrEfiMemoryMgr->Signature = SIGNATURE('$','C','E','I');
CrEfiMemoryMgr->EndOfMemory = (UINT16)(DEFAULT_CR_MEM_SIZE_OF_PAGE << 12);
CrEfiMemoryMgr->UsedOfMemory = (UINT16)sizeof(CR_EFI_MEM_MANAGER);
CrOpromInfo = (CR_COMMON_AREA_INFO*)CrEfiMemAlloc (sizeof (CR_COMMON_AREA_INFO));
if (CrOpromInfo == NULL) {
ASSERT (0);
return EFI_OUT_OF_RESOURCES;
}
CrOpromInfo->Signature = SIGNATURE('$','C','O','I');
CrEfiMemoryMgr->CommonAreaOffset = (UINT16)((UINTN)CrOpromInfo - mCrMemStart);
return EFI_SUCCESS;
}
/**
Allocate a memory space from Console Redirection Memory Manager.
@param Size Size of memory to allocate.
@retval Return allocated address.
**/
VOID*
CrEfiMemAlloc (
IN UINTN Size
)
{
EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS MemoryBuf;
CR_EFI_MEM_MANAGER *CrEfiMemoryMgr;
UINTN RemainMemorySize;
UINTN NeededSizeOfPages;
UINTN OrgSizeOfPages;
VOID *Memory;
if (Size == 0) {
return NULL;
}
if (mCrMemStart == 0) {
Status = InitCrEfiMemManager ();
if (EFI_ERROR (Status)) {
return NULL;
}
}
//[-start-180729-IB08400622-modify]//
Memory = NULL;
if (mCrMemStart != 0) {
CrEfiMemoryMgr = (CR_EFI_MEM_MANAGER *)(mCrMemStart);
RemainMemorySize = CrEfiMemoryMgr->EndOfMemory - CrEfiMemoryMgr->UsedOfMemory;
if (Size > RemainMemorySize) {
OrgSizeOfPages = CrEfiMemoryMgr->EndOfMemory >> 12;
//
// Calculate needed Memory size in Pages
//
NeededSizeOfPages = (Size - RemainMemorySize + 4095) >> 12;
//
// Rellocate CREfiMemory space below 64K
//
MemoryBuf = MAX_ALLOC_ADDR;
Status = gBS->AllocatePages (
AllocateMaxAddress,
EfiRuntimeServicesData,
OrgSizeOfPages + NeededSizeOfPages,
&MemoryBuf
);
if (EFI_ERROR (Status)) {
return NULL;
}
ZeroMem ((UINT8 *)(UINTN)MemoryBuf, ((OrgSizeOfPages + NeededSizeOfPages) << 12));
//
// Copy old location data to new location
//
CopyMem ((UINT8 *)(UINTN)MemoryBuf, (UINT8 *)mCrMemStart, CrEfiMemoryMgr->UsedOfMemory);
//
// Rellocate memory space complete and free old memory space
//
FreeCrEfiMemorySpace ();
//
// Update new location to mCrMemStart
//
mCrMemStart = (UINT32)MemoryBuf;
CrEfiMemoryMgr = (CR_EFI_MEM_MANAGER *)(mCrMemStart);
CrEfiMemoryMgr->EndOfMemory = (UINT16)((OrgSizeOfPages + NeededSizeOfPages) << 12);
}
Memory = (void *)(mCrMemStart + CrEfiMemoryMgr->UsedOfMemory);
CrEfiMemoryMgr->UsedOfMemory = (UINT16)(CrEfiMemoryMgr->UsedOfMemory + Size);
}
//[-end-180729-IB08400622-modify]//
return Memory;
}
/**
Frees the memory space that allocate for Console Redirection Memory Manager.
@retval EFI_SUCCESS Free memory success
@retval EFI_UNSUPPORTED Free memory fail
@retval others Free memory fail
**/
EFI_STATUS
FreeCrEfiMemorySpace (
VOID
)
{
EFI_STATUS Status;
CR_EFI_MEM_MANAGER *CrEfiMemoryMgr;
if (mCrMemStart == 0) {
return EFI_UNSUPPORTED;
}
CrEfiMemoryMgr = (CR_EFI_MEM_MANAGER *)(mCrMemStart);
Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS)mCrMemStart, (CrEfiMemoryMgr->EndOfMemory >> 12));
mCrMemStart = 0;
return Status;
}