Skip to content

Commit

Permalink
ReservedMem: Add additional field Type
Browse files Browse the repository at this point in the history
Implement ability to specify MemoryType for allocation reserved memory.
  • Loading branch information
savvamitrofanov committed Sep 23, 2020
1 parent 8acdb17 commit 76bf9bd
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 36 deletions.
14 changes: 14 additions & 0 deletions Docs/Sample.plist
Original file line number Diff line number Diff line change
Expand Up @@ -1108,13 +1108,27 @@
<dict>
<key>Address</key>
<integer>268435456</integer>
<key>Type</key>
<string>Reserved</string>
<key>Comment</key>
<string>HD3000: IGPU memory corruption errata</string>
<key>Enabled</key>
<false/>
<key>Size</key>
<integer>268435456</integer>
</dict>
<dict>
<key>Address</key>
<integer>569344</integer>
<key>Type</key>
<string>RuntimeCode</string>
<key>Comment</key>
<string>Fix black screen on wake from hibernation for Lenovo Thinkpad T490</string>
<key>Enabled</key>
<false/>
<key>Size</key>
<integer>4096</integer>
</dict>
</array>
</dict>
</dict>
Expand Down
2 changes: 2 additions & 0 deletions Docs/SampleCustom.plist
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,8 @@
<dict>
<key>Address</key>
<integer>268435456</integer>
<key>Type</key>
<string>Reserved</string>
<key>Comment</key>
<string>HD3000: IGPU memory corruption errata</string>
<key>Enabled</key>
Expand Down
1 change: 1 addition & 0 deletions Include/Acidanthera/Library/OcConfigurationLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ typedef enum {
_(UINT64 , Address , , 0 , () ) \
_(UINT64 , Size , , 0 , () ) \
_(BOOLEAN , Enabled , , FALSE , () ) \
_(OC_STRING , Type , , OC_STRING_CONSTR ("Reserved", _, __), OC_DESTR (OC_STRING) ) \
_(OC_STRING , Comment , , OC_STRING_CONSTR ("", _, __), OC_DESTR (OC_STRING) )
OC_DECLARE (OC_UEFI_RSVD_ENTRY)

Expand Down
88 changes: 87 additions & 1 deletion Include/Acidanthera/Library/OcMemoryLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,76 @@
**/
#define OC_DEFAULT_MEMORY_MAP_SIZE (EFI_PAGE_SIZE*3)

#define OC_MEMORY_TYPE_DESC_COUNT 16

typedef struct {
CHAR8 *Name;
EFI_MEMORY_TYPE Type;
} OC_MEMORY_TYPE_DESC;

STATIC OC_MEMORY_TYPE_DESC OcMemoryTypeString [OC_MEMORY_TYPE_DESC_COUNT] = {
{
"Reserved",
EfiReservedMemoryType
},
{
"LoaderCode",
EfiLoaderCode
},
{
"LoaderData",
EfiLoaderData
},
{
"BootServiceCode",
EfiBootServicesCode
},
{
"BootServiceData",
EfiBootServicesData
},
{
"RuntimeCode",
EfiRuntimeServicesCode
},
{
"RuntimeData",
EfiRuntimeServicesData
},
{
"Available",
EfiConventionalMemory
},
{
"Persistent",
EfiPersistentMemory
},
{
"UnusableMemory",
EfiUnusableMemory
},
{
"ACPIReclaimMemory",
EfiACPIReclaimMemory
},
{
"ACPIMemoryNVS",
EfiACPIMemoryNVS
},
{
"MemoryMappedIO",
EfiMemoryMappedIO
},
{
"MemoryMappedIOPortSpace",
EfiMemoryMappedIOPortSpace
},
{
"PalCode",
EfiPalCode
}
};

/**
Lock the legacy region specified to enable modification.
Expand Down Expand Up @@ -322,7 +392,7 @@ OcGetMemoryAttributes (
Refresh memory attributes entry containing the specified address.
@param[in] Address Address contained in the updated entry.
@param[in] GetMemoryMap
@param[in] GetMemoryMap
@retval EFI_SUCCESS on success.
@retval EFI_NOT_FOUND no entry contains the specified address.
Expand Down Expand Up @@ -395,6 +465,22 @@ OcGetPhysicalAddress (
OUT EFI_PHYSICAL_ADDRESS *PhysicalAddr
);

/**
Return EFI memory type for given type description
@param[in] MemoryTypeDesc Memory type string representation.
@param[out] MemoryType EFI memory type to return.
@retval EFI_NOT_FOUND on unsuccessful lookup.
@retval EFI_INVALID_PARAMETER on wrong passed agruments.
@retval EFI_SUCCESS on successful lookup.
**/
EFI_STATUS
OcDescToMemoryType (
IN CHAR8 *MemoryTypeDesc,
OUT EFI_MEMORY_TYPE *MemoryType
);

/**
Virtual memory context
**/
Expand Down
1 change: 1 addition & 0 deletions Library/OcConfigurationLib/OcConfigurationLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ mUefiReservedMemoryEntrySchema[] = {
OC_SCHEMA_STRING_IN ("Comment", OC_UEFI_RSVD_ENTRY, Comment),
OC_SCHEMA_BOOLEAN_IN ("Enabled", OC_UEFI_RSVD_ENTRY, Enabled),
OC_SCHEMA_INTEGER_IN ("Size", OC_UEFI_RSVD_ENTRY, Size),
OC_SCHEMA_STRING_IN ("Type", OC_UEFI_RSVD_ENTRY, Type),
};

STATIC
Expand Down
26 changes: 26 additions & 0 deletions Library/OcMemoryLib/VirtualMemory.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@
#include <Library/OcDebugLogLib.h>
#include <Library/OcMemoryLib.h>


EFI_STATUS
OcDescToMemoryType (
IN CHAR8 *MemoryTypeDesc,
OUT EFI_MEMORY_TYPE *MemoryType
)
{
UINTN Index;
EFI_STATUS Status = EFI_INVALID_PARAMETER;

if (MemoryTypeDesc != NULL && MemoryType !=NULL) {
for (Index = 0; Index < OC_MEMORY_TYPE_DESC_COUNT; Index++) {
if (AsciiStrCmp (MemoryTypeDesc, OcMemoryTypeString[Index].Name) == 0) {
Status = EFI_SUCCESS;
*MemoryType=OcMemoryTypeString[Index].Type;
break;
}
}
if (EFI_ERROR (Status)) {
Status = EFI_NOT_FOUND;
}
}

return Status;
}

PAGE_MAP_AND_DIRECTORY_POINTER *
OcGetCurrentPageTable (
OUT UINTN *Flags OPTIONAL
Expand Down
8 changes: 4 additions & 4 deletions OpenCorePkg.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
# Copyright (C) 2018, Download-Fritz. All rights reserved.<BR>
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
#
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
##
Expand Down
4 changes: 2 additions & 2 deletions Platform/OpenCore/OpenCoreMisc.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ ProduceDebugReport (
Status = AcpiDumpTables (SubReport);
SubReport->Close (SubReport);
}
DEBUG ((DEBUG_INFO, "OC: ACPI dumping - %r\n", Status));
DEBUG ((DEBUG_INFO, "OC: ACPI dumping - %r\n", Status));

Status = SafeFileOpen (
SysReport,
Expand All @@ -154,7 +154,7 @@ ProduceDebugReport (
Status = OcSmbiosDump (SubReport);
SubReport->Close (SubReport);
}
DEBUG ((DEBUG_INFO, "OC: ACPI dumping - %r\n", Status));
DEBUG ((DEBUG_INFO, "OC: ACPI dumping - %r\n", Status));

SysReport->Close (SysReport);
Fs->Close (Fs);
Expand Down
83 changes: 54 additions & 29 deletions Platform/OpenCore/OpenCoreUefi.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include <Library/OcFirmwareVolumeLib.h>
#include <Library/OcHashServicesLib.h>
#include <Library/OcMiscLib.h>
#include <Library/OcMemoryLib.h>
#include <Library/OcRtcLib.h>
#include <Library/OcSmcLib.h>
#include <Library/OcOSInfoLib.h>
Expand Down Expand Up @@ -498,21 +499,67 @@ OcLoadBooterUefiSupport (
OcAbcInitialize (&AbcSettings);
}


VOID
OcReserveMemory (
IN OC_GLOBAL_CONFIG *Config
)
{
EFI_STATUS Status;
UINTN Index;
EFI_PHYSICAL_ADDRESS ReservedAddress;
EFI_MEMORY_TYPE RsvdMemoryType;
CHAR8 *RsvdMemoryTypeStr;

for (Index = 0; Index < Config->Uefi.ReservedMemory.Count; ++Index) {
if (!Config->Uefi.ReservedMemory.Values[Index]->Enabled) {
continue;
}

if ((Config->Uefi.ReservedMemory.Values[Index]->Address & (BASE_4KB - 1)) != 0
|| (Config->Uefi.ReservedMemory.Values[Index]->Size & (BASE_4KB - 1)) != 0) {
Status = EFI_INVALID_PARAMETER;
} else {
RsvdMemoryTypeStr = OC_BLOB_GET (&Config->Uefi.ReservedMemory.Values[Index]->Type);

Status = OcDescToMemoryType (RsvdMemoryTypeStr, &RsvdMemoryType);
if (EFI_ERROR (Status)){
DEBUG ((DEBUG_INFO, "OC: Invalid ReservedMemory Type: %a\n", RsvdMemoryTypeStr));
RsvdMemoryType = EfiReservedMemoryType;
}

ReservedAddress = Config->Uefi.ReservedMemory.Values[Index]->Address;
Status = gBS->AllocatePages (
AllocateAddress,
RsvdMemoryType,
(UINTN) EFI_SIZE_TO_PAGES (Config->Uefi.ReservedMemory.Values[Index]->Size),
&ReservedAddress
);
}

DEBUG ((
DEBUG_INFO,
"OC: Reserving region %Lx of %Lx size - %r\n",
Config->Uefi.ReservedMemory.Values[Index]->Address,
Config->Uefi.ReservedMemory.Values[Index]->Size,
Status
));
}
}


VOID
OcLoadUefiSupport (
IN OC_STORAGE_CONTEXT *Storage,
IN OC_GLOBAL_CONFIG *Config,
IN OC_CPU_INFO *CpuInfo
)
{
EFI_STATUS Status;
EFI_HANDLE *DriversToConnect;
UINTN Index;
UINT16 *BootOrder;
UINTN BootOrderCount;
BOOLEAN BootOrderChanged;
EFI_EVENT Event;
EFI_PHYSICAL_ADDRESS ReservedAddress;

OcReinstallProtocols (Config);

Expand Down Expand Up @@ -584,32 +631,10 @@ OcLoadUefiSupport (

OcMiscUefiQuirksLoaded (Config);

for (Index = 0; Index < Config->Uefi.ReservedMemory.Count; ++Index) {
if (!Config->Uefi.ReservedMemory.Values[Index]->Enabled) {
continue;
}

if ((Config->Uefi.ReservedMemory.Values[Index]->Address & (BASE_4KB - 1)) != 0
|| (Config->Uefi.ReservedMemory.Values[Index]->Size & (BASE_4KB - 1)) != 0) {
Status = EFI_INVALID_PARAMETER;
} else {
ReservedAddress = Config->Uefi.ReservedMemory.Values[Index]->Address;
Status = gBS->AllocatePages (
AllocateAddress,
EfiReservedMemoryType,
(UINTN) EFI_SIZE_TO_PAGES (Config->Uefi.ReservedMemory.Values[Index]->Size),
&ReservedAddress
);
}

DEBUG ((
DEBUG_INFO,
"OC: Reserving region %Lx of %Lx size - %r\n",
Config->Uefi.ReservedMemory.Values[Index]->Address,
Config->Uefi.ReservedMemory.Values[Index]->Size,
Status
));
}
//
// Reserve requested memory regions
//
OcReserveMemory (Config);

if (Config->Uefi.ConnectDrivers) {
OcLoadDrivers (Storage, Config, &DriversToConnect);
Expand Down

0 comments on commit 76bf9bd

Please sign in to comment.