From 38564b287058ab948ffc022051d9b970c5f54012 Mon Sep 17 00:00:00 2001 From: jief666 Date: Mon, 8 Jan 2024 11:28:32 +0100 Subject: [PATCH] Improve MemoryTacker. Use gBS->FreePool() instead of FreePool for firmware allocated memory. --- OpenCorePkg | 2 +- rEFIt_UEFI/Platform/BdsConnect.cpp | 15 +++++--------- rEFIt_UEFI/Platform/DataHubCpu.cpp | 1 - rEFIt_UEFI/Platform/Nvram.cpp | 33 ++++++++++++++---------------- rEFIt_UEFI/refit/lib.cpp | 2 +- rEFIt_UEFI/refit/main.cpp | 17 +++++---------- 6 files changed, 27 insertions(+), 43 deletions(-) diff --git a/OpenCorePkg b/OpenCorePkg index 0fcc050a4..8270a6fb8 160000 --- a/OpenCorePkg +++ b/OpenCorePkg @@ -1 +1 @@ -Subproject commit 0fcc050a44f6fa06788b349e65b4e10fcdabde67 +Subproject commit 8270a6fb81b95abf957d2b7acd945fcc039ffde6 diff --git a/rEFIt_UEFI/Platform/BdsConnect.cpp b/rEFIt_UEFI/Platform/BdsConnect.cpp index 7b6dd2a59..4196c50cd 100644 --- a/rEFIt_UEFI/Platform/BdsConnect.cpp +++ b/rEFIt_UEFI/Platform/BdsConnect.cpp @@ -390,15 +390,10 @@ EFI_STATUS ScanDeviceHandles(EFI_HANDLE ControllerHandle, } } -//MsgLog("ScanDeviceHandles FreePool(OpenInfo)\n"); - FreePool(OpenInfo); -//MsgLog("ScanDeviceHandles FreePool(OpenInfo) after\n"); + gBS->FreePool(OpenInfo); // use gBS->FreePool instead of FreePool to avoid message from MemoryTracker } } -//MsgLog("ScanDeviceHandles FreePool(ProtocolGuidArray)\n"); - - FreePool(ProtocolGuidArray); -//MsgLog("ScanDeviceHandles FreePool(ProtocolGuidArray) after\n"); + gBS->FreePool(ProtocolGuidArray); // use gBS->FreePool instead of FreePool to avoid message from MemoryTracker } } @@ -410,7 +405,7 @@ Error: } if (*HandleBuffer != NULL) { - FreePool(*HandleBuffer); + gBS->FreePool(*HandleBuffer); // use gBS->FreePool instead of FreePool to avoid message from MemoryTracker } *HandleCount = 0; @@ -477,12 +472,12 @@ EFI_STATUS BdsLibConnectMostlyAllEfi() } } - FreePool(HandleBuffer); + gBS->FreePool(HandleBuffer); // use gBS->FreePool instead of FreePool to avoid message from MemoryTracker FreePool(HandleType); } Done: - FreePool(AllHandleBuffer); + gBS->FreePool(AllHandleBuffer); // use gBS->FreePool instead of FreePool to avoid message from MemoryTracker return Status; } diff --git a/rEFIt_UEFI/Platform/DataHubCpu.cpp b/rEFIt_UEFI/Platform/DataHubCpu.cpp index 6301a4a71..a4e323c55 100644 --- a/rEFIt_UEFI/Platform/DataHubCpu.cpp +++ b/rEFIt_UEFI/Platform/DataHubCpu.cpp @@ -501,7 +501,6 @@ AddSMCkey(SMC_KEY Key, SMC_DATA_SIZE Size, SMC_KEY_TYPE Type, SMC_DATA *Data) { if (gAppleSmc && (gAppleSmc->Signature == NON_APPLE_SMC_SIGNATURE)) { - MemoryStopRecord msr; // Do not record allocation made by SmcAddKey gAppleSmc->SmcAddKey(gAppleSmc, Key, Size, Type, 0xC0); gAppleSmc->SmcWriteValue(gAppleSmc, Key, Size, Data); } diff --git a/rEFIt_UEFI/Platform/Nvram.cpp b/rEFIt_UEFI/Platform/Nvram.cpp index 30fe503fb..3991237c6 100644 --- a/rEFIt_UEFI/Platform/Nvram.cpp +++ b/rEFIt_UEFI/Platform/Nvram.cpp @@ -558,24 +558,21 @@ GetSmcKeys (XBool WriteToSMC) NKey[2] = (NumKey >> 8) & 0xFF; //key, size, type, attr DBG("Registered %lld SMC keys\n", NumKey); - { - MemoryStopRecord msr; - Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("#KEY"), 4, SmcKeyTypeUint32, 0xC0); - if (!EFI_ERROR(Status)) { - Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("#KEY"), 4, (SMC_DATA *)&NKey); - } - Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("$Adr"), 4, SmcKeyTypeUint32, 0x08); - if (!EFI_ERROR(Status)) { - Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("$Adr"), 4, (SMC_DATA *)&SAdr); - } - Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("$Num"), 1, SmcKeyTypeUint8, 0x08); - if (!EFI_ERROR(Status)) { - Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("$Num"), 1, (SMC_DATA *)&SNum); - } - Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("RMde"), 1, SmcKeyTypeChar, 0xC0); - if (!EFI_ERROR(Status)) { - Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("RMde"), 1, (SMC_DATA *)&Mode); - } + Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("#KEY"), 4, SmcKeyTypeUint32, 0xC0); + if (!EFI_ERROR(Status)) { + Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("#KEY"), 4, (SMC_DATA *)&NKey); + } + Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("$Adr"), 4, SmcKeyTypeUint32, 0x08); + if (!EFI_ERROR(Status)) { + Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("$Adr"), 4, (SMC_DATA *)&SAdr); + } + Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("$Num"), 1, SmcKeyTypeUint8, 0x08); + if (!EFI_ERROR(Status)) { + Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("$Num"), 1, (SMC_DATA *)&SNum); + } + Status = gAppleSmc->SmcAddKey(gAppleSmc, FourCharKey("RMde"), 1, SmcKeyTypeChar, 0xC0); + if (!EFI_ERROR(Status)) { + Status = gAppleSmc->SmcWriteValue(gAppleSmc, FourCharKey("RMde"), 1, (SMC_DATA *)&Mode); } } FreePool(Name); diff --git a/rEFIt_UEFI/refit/lib.cpp b/rEFIt_UEFI/refit/lib.cpp index 89982d6ea..5725965ab 100644 --- a/rEFIt_UEFI/refit/lib.cpp +++ b/rEFIt_UEFI/refit/lib.cpp @@ -1023,7 +1023,7 @@ void ScanVolumes(void) FreePool(Volume); } } - FreePool(Handles); + gBS->FreePool(Handles); // use gBS->FreePool instead of FreePool to avoid message from MemoryTracker // DBG("Found %d volumes\n", VolumesCount); if (SelfVolume == NULL){ DBG(" WARNING: SelfVolume not found"); //Slice - and what? diff --git a/rEFIt_UEFI/refit/main.cpp b/rEFIt_UEFI/refit/main.cpp index dfd06f15e..866a72f86 100644 --- a/rEFIt_UEFI/refit/main.cpp +++ b/rEFIt_UEFI/refit/main.cpp @@ -1860,8 +1860,6 @@ void LOADER_ENTRY::StartLoader() if (SavePreBootLog) { Status = SaveBooterLog(&self.getCloverDir(), PREBOOT_LOG); } - gBS->FreePages (ExtraSpace, 90000); - AllocSmallBlocks(); // shrink memory map; #ifdef JIEF_DEBUG @@ -2262,9 +2260,9 @@ void DisconnectInvalidDiskIoChildDrivers(void) DBG(" - Handle %llx with DiskIo, is Partition, no Fs, BY_DRIVER Agent: %llx, Disconnect: %s\n", (uintptr_t)Handles[Index], (uintptr_t)(OpenInfo[OpenInfoIndex].AgentHandle), efiStrError(Status)); } } - FreePool(OpenInfo); + gBS->FreePool(OpenInfo); // use gBS->FreePool instead of FreePool to avoid message from MemoryTracker } - FreePool(Handles); + gBS->FreePool(Handles); // use gBS->FreePool instead of FreePool to avoid message from MemoryTracker if (!Found) { DBG(" not found, all ok\n"); @@ -2371,10 +2369,10 @@ void DisconnectSomeDevices(void) } } } - FreePool(Handles); + gBS->FreePool(Handles); // use gBS->FreePool instead of FreePool to avoid message from MemoryTracker } // DBG("\n"); - FreePool(ControllerHandles); + gBS->FreePool(ControllerHandles); // use gBS->FreePool instead of FreePool to avoid message from MemoryTracker } @@ -2851,11 +2849,6 @@ RefitMainMain (IN EFI_HANDLE ImageHandle, /*Status = */EfiGetSystemConfigurationTable (&gEfiDxeServicesTableGuid, (void **) &gDS); - Status = gBS->AllocatePages(AllocateMaxAddress, - EfiACPIReclaimMemory, - 90000, - &ExtraSpace); - InitBooterLog(); // ConsoleInHandle = SystemTable->ConsoleInHandle; @@ -3004,7 +2997,7 @@ RefitMainMain (IN EFI_HANDLE ImageHandle, gConf.InitialisePlatform(); #ifdef JIEF_DEBUG - DumpNvram(); + //DumpNvram(); #endif /*