ReactOS 0.4.16-dev-2284-g3529151
uefimem.c
Go to the documentation of this file.
1/*
2 * PROJECT: FreeLoader UEFI Support
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Memory Management Functions
5 * COPYRIGHT: Copyright 2022 Justin Miller <justinmiller100@gmail.com>
6 */
7
8/* INCLUDES ******************************************************************/
9
10#include <uefildr.h>
11
12#include <debug.h>
14
15#define NEXT_MEMORY_DESCRIPTOR(Descriptor, DescriptorSize) \
16 (EFI_MEMORY_DESCRIPTOR*)((char*)(Descriptor) + (DescriptorSize))
17#define EXIT_STACK_SIZE 0x1000
18#define UNUSED_MAX_DESCRIPTOR_COUNT 10000
19
23 _In_ ULONG MaxCount,
24 _In_ PFN_NUMBER BasePage,
25 _In_ PFN_NUMBER PageCount,
26 _In_ TYPE_OF_MEMORY MemoryType);
27
28/* GLOBALS *******************************************************************/
29
32
40
42
43/* FUNCTIONS *****************************************************************/
44
45static
46VOID
48 _Out_ UINTN* LocMapKey,
49 _Out_ UINTN* LocMapSize,
50 _Out_ UINTN* LocDescriptorSize,
51 _Out_ UINT32* LocDescriptorVersion)
52{
55 ULONG Count = 0;
56
59 LocMapKey,
60 LocDescriptorSize,
61 LocDescriptorVersion);
62
63 /* Reallocate and retrieve again the needed memory map size (since memory
64 * allocated by AllocatePool() counts in the map), until it's OK. */
65 while (Status != EFI_SUCCESS)
66 {
67 /* Reallocate the memory map buffer */
68 if (EfiMemoryMap)
70
71 /* If MapSize never reports the correct size after the first time, increment */
72 AllocationSize = *LocMapSize + (*LocDescriptorSize * Count);
74 (VOID**)&EfiMemoryMap);
77 LocMapKey,
78 LocDescriptorSize,
79 LocDescriptorVersion);
80 Count++;
81 }
82}
83
84static
85VOID
89 _In_ PFN_COUNT SizeInPages,
90 _In_ TYPE_OF_MEMORY MemoryType)
91{
92 ULONG_PTR BasePage, PageCount;
93
94 BasePage = BaseAddress / EFI_PAGE_SIZE;
95 PageCount = SizeInPages;
96
97 /* Add the memory descriptor */
100 BasePage,
101 PageCount,
102 MemoryType);
103}
104
105static
108{
109 switch (EfiMemoryType)
110 {
112 return LoaderReserve;
113 case EfiLoaderCode:
114 return LoaderLoadedProgram;
115 case EfiLoaderData:
116 return LoaderLoadedProgram;
126 return LoaderFree;
128 return LoaderBad;
131 case EfiACPIMemoryNVS:
132 return LoaderReserve;
134 return LoaderReserve;
136 return LoaderReserve;
137 default:
138 break;
139 }
140 return LoaderReserve;
141}
142
145{
146 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
147 UINT32 DescriptorVersion;
148 SIZE_T FreeldrMemMapSize;
151 UINTN MapSize;
152 UINTN MapKey;
154
155 EFI_GUID EfiLoadedImageProtocol = EFI_LOADED_IMAGE_PROTOCOL_GUID;
156 PFREELDR_MEMORY_DESCRIPTOR FreeldrMem = NULL;
157 EFI_MEMORY_DESCRIPTOR* MapEntry = NULL;
158 UINT32 EntryCount = 0;
160
162 &EfiLoadedImageProtocol,
163 (VOID**)&LoadedImage);
164 if (Status != EFI_SUCCESS)
165 {
166 TRACE("Failed to find LoadedImageHandle with status: %d\n", Status);
167 UiMessageBoxCritical("Unable to initialize memory manager.");
168 return NULL;
169 }
170 OsLoaderBase = LoadedImage->ImageBase;
171 OsLoaderSize = LoadedImage->ImageSize;
172 PublicBootHandle = LoadedImage->DeviceHandle;
174
175 TRACE("UefiMemGetMemoryMap: Gather memory map\n");
176 PUEFI_LoadMemoryMap(&MapKey,
177 &MapSize,
179 &DescriptorVersion);
180
181 TRACE("Value of MapKey: %d\n", MapKey);
182 TRACE("Value of MapSize: %d\n", MapSize);
183 TRACE("Value of DescriptorSize: %d\n", DescriptorSize);
184 TRACE("Value of DescriptorVersion: %d\n", DescriptorVersion);
185
186 EntryCount = (MapSize / DescriptorSize);
187
188 FreeldrMemMapSize = (sizeof(FREELDR_MEMORY_DESCRIPTOR) * EntryCount);
190 FreeldrMemMapSize,
191 (void**)&FreeldrMem);
192 if (Status != EFI_SUCCESS)
193 {
194 TRACE("Failed to allocate pool with status %d\n", Status);
195 UiMessageBoxCritical("Unable to initialize memory manager.");
196 return NULL;
197 }
198
199 RtlZeroMemory(FreeldrMem, FreeldrMemMapSize);
200 MapEntry = EfiMemoryMap;
201 for (Index = 0; Index < EntryCount; ++Index)
202 {
203 TYPE_OF_MEMORY MemoryType = UefiConvertToFreeldrDesc(MapEntry->Type);
204 if (MemoryType == LoaderFree)
205 {
208 MapEntry->NumberOfPages,
209 &MapEntry->PhysicalStart);
210 if (Status != EFI_SUCCESS)
211 {
212 /* We failed to reserve the page, so change its type */
213 MemoryType = LoaderFirmwareTemporary;
214 }
215 }
216
217 /* We really don't want to touch these reserved spots at all */
218 if (MemoryType != LoaderReserve)
219 {
220 UefiSetMemory(FreeldrMem,
221 MapEntry->PhysicalStart,
222 MapEntry->NumberOfPages,
223 MemoryType);
224 }
225
226 MapEntry = NEXT_MEMORY_DESCRIPTOR(MapEntry, DescriptorSize);
227 }
228
229 /* Windows expects the first page to be reserved, otherwise it asserts.
230 * However it can be just a free page on some UEFI systems. */
231 UefiSetMemory(FreeldrMem, 0x000000, 1, LoaderFirmwarePermanent);
232 *MemoryMapSize = FreeldrDescCount;
233 return FreeldrMem;
234}
235
236VOID
238{
239 UINTN MapKey;
240 UINTN MapSize;
243 UINT32 DescriptorVersion;
244
245 TRACE("Attempting to exit bootsevices\n");
246 PUEFI_LoadMemoryMap(&MapKey,
247 &MapSize,
249 &DescriptorVersion);
250
252 /* UEFI spec demands twice! */
253 if (Status != EFI_SUCCESS)
255
256 if (Status != EFI_SUCCESS)
257 {
258 TRACE("Failed to exit boot services with status: %d\n", Status);
259 FrLdrBugCheckWithMessage(EXIT_BOOTSERVICES_FAILURE,
260 __FILE__,
261 __LINE__,
262 "Failed to exit boot services with status: %d",
263 Status);
264 }
265 else
266 {
267 TRACE("Exited bootservices\n");
268 }
269}
270
271VOID
273{
274 _exituefi();
275}
#define WARNING
Definition: BusLogic958.h:56
#define EFI_LOADED_IMAGE_PROTOCOL_GUID
Definition: LoadedImage.h:21
ULONG_PTR PFN_NUMBER
UINT32 UINTN
#define EFI_PAGE_SIZE
Definition: UefiBaseType.h:189
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:31
#define EFI_SUCCESS
Definition: UefiBaseType.h:120
EFI_MEMORY_TYPE
@ EfiUnusableMemory
@ EfiBootServicesData
@ EfiReservedMemoryType
@ EfiBootServicesCode
@ EfiConventionalMemory
@ EfiLoaderData
@ EfiACPIMemoryNVS
@ EfiMemoryMappedIOPortSpace
@ EfiACPIReclaimMemory
@ EfiLoaderCode
@ EfiMemoryMappedIO
@ EfiRuntimeServicesCode
@ EfiRuntimeServicesData
@ AllocateAddress
Definition: UefiSpec.h:45
BIOS_MEMORY_MAP MemoryMap[32]
Definition: loader.c:11
DECLSPEC_NORETURN VOID FrLdrBugCheckWithMessage(ULONG BugCode, PCHAR File, ULONG Line, PSTR Format,...)
Definition: debug.c:29
#define DBG_DEFAULT_CHANNEL(ch)
Definition: debug.h:106
struct _FREELDR_MEMORY_DESCRIPTOR FREELDR_MEMORY_DESCRIPTOR
VOID UiMessageBoxCritical(_In_ PCSTR MessageText)
Definition: ui.c:372
#define NULL
Definition: types.h:112
IN PFCB IN PFILE_OBJECT FileObject IN ULONG AllocationSize
Definition: fatprocs.h:323
Status
Definition: gdiplustypes.h:25
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#define _Inout_
Definition: no_sal2.h:162
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
int Count
Definition: noreturn.cpp:7
@ LoaderBad
Definition: arc.h:296
@ LoaderReserve
Definition: arc.h:317
@ LoaderFree
Definition: arc.h:295
@ LoaderFirmwareTemporary
Definition: arc.h:298
@ LoaderLoadedProgram
Definition: arc.h:297
@ LoaderFirmwarePermanent
Definition: arc.h:299
enum _TYPE_OF_MEMORY TYPE_OF_MEMORY
#define TRACE(s)
Definition: solgame.cpp:4
EFI_ALLOCATE_PAGES AllocatePages
Definition: UefiSpec.h:1810
EFI_FREE_POOL FreePool
Definition: UefiSpec.h:1814
EFI_HANDLE_PROTOCOL HandleProtocol
Definition: UefiSpec.h:1832
EFI_EXIT_BOOT_SERVICES ExitBootServices
Definition: UefiSpec.h:1846
EFI_ALLOCATE_POOL AllocatePool
Definition: UefiSpec.h:1813
EFI_GET_MEMORY_MAP GetMemoryMap
Definition: UefiSpec.h:1812
EFI_HANDLE DeviceHandle
The device handle that the EFI Image was loaded from.
Definition: LoadedImage.h:59
UINT64 ImageSize
The size in bytes of the loaded image.
Definition: LoadedImage.h:74
VOID * ImageBase
The base address at which the image was loaded.
Definition: LoadedImage.h:73
EFI_PHYSICAL_ADDRESS PhysicalStart
Definition: UefiSpec.h:99
EFI_BOOT_SERVICES * BootServices
Definition: UefiSpec.h:1959
_In_ SIZE_T DescriptorSize
Definition: nls.c:40
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t UINT32
Definition: typedefs.h:59
uint32_t ULONG
Definition: typedefs.h:59
UINT32 FreeldrDescCount
Definition: uefimem.c:34
VOID UefiExitBootServices(VOID)
Definition: uefimem.c:237
EFI_MEMORY_DESCRIPTOR * EfiMemoryMap
Definition: uefimem.c:33
PVOID EndofExitStack
Definition: uefimem.c:39
static VOID PUEFI_LoadMemoryMap(_Out_ UINTN *LocMapKey, _Out_ UINTN *LocMapSize, _Out_ UINTN *LocDescriptorSize, _Out_ UINT32 *LocDescriptorVersion)
Definition: uefimem.c:47
ULONG AddMemoryDescriptor(_Inout_ PFREELDR_MEMORY_DESCRIPTOR List, _In_ ULONG MaxCount, _In_ PFN_NUMBER BasePage, _In_ PFN_NUMBER PageCount, _In_ TYPE_OF_MEMORY MemoryType)
EFI_HANDLE PublicBootHandle
Definition: uefimem.c:37
EFI_SYSTEM_TABLE * GlobalSystemTable
Definition: uefildr.c:16
static TYPE_OF_MEMORY UefiConvertToFreeldrDesc(EFI_MEMORY_TYPE EfiMemoryType)
Definition: uefimem.c:107
PVOID ExitStack
Definition: uefimem.c:38
PFREELDR_MEMORY_DESCRIPTOR UefiMemGetMemoryMap(ULONG *MemoryMapSize)
Definition: uefimem.c:144
PVOID OsLoaderBase
Definition: uefimem.c:35
static VOID UefiSetMemory(_Inout_ PFREELDR_MEMORY_DESCRIPTOR MemoryMap, _In_ ULONG_PTR BaseAddress, _In_ PFN_COUNT SizeInPages, _In_ TYPE_OF_MEMORY MemoryType)
Definition: uefimem.c:86
EFI_HANDLE GlobalImageHandle
Definition: uefildr.c:15
SIZE_T OsLoaderSize
Definition: uefimem.c:36
#define UNUSED_MAX_DESCRIPTOR_COUNT
Definition: uefimem.c:18
#define NEXT_MEMORY_DESCRIPTOR(Descriptor, DescriptorSize)
Definition: uefimem.c:15
void _exituefi(VOID)
VOID UefiPrepareForReactOS(VOID)
Definition: uefimem.c:272
_In_ WDFCOLLECTION _In_ ULONG Index
_Must_inspect_result_ _In_ WDFCMRESLIST List
Definition: wdfresource.h:550
ULONG PFN_COUNT
Definition: mmtypes.h:102