ReactOS 0.4.15-dev-8100-g1887773
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
34
42
44
45/* FUNCTIONS *****************************************************************/
46
47static
48VOID
50 _Out_ UINTN* LocMapKey,
51 _Out_ UINTN* LocMapSize,
52 _Out_ UINTN* LocDescriptorSize,
53 _Out_ UINT32* LocDescriptorVersion)
54{
57 ULONG Count = 0;
58
61 LocMapKey,
62 LocDescriptorSize,
63 LocDescriptorVersion);
64
65 /* Reallocate and retrieve again the needed memory map size (since memory
66 * allocated by AllocatePool() counts in the map), until it's OK. */
67 while (Status != EFI_SUCCESS)
68 {
69 /* Reallocate the memory map buffer */
70 if (EfiMemoryMap)
72
73 /* If MapSize never reports the correct size after the first time, increment */
74 AllocationSize = *LocMapSize + (*LocDescriptorSize * Count);
76 (VOID**)&EfiMemoryMap);
79 LocMapKey,
80 LocDescriptorSize,
81 LocDescriptorVersion);
82 Count++;
83 }
84}
85
86static
87VOID
91 _In_ PFN_COUNT SizeInPages,
92 _In_ TYPE_OF_MEMORY MemoryType)
93{
94 ULONG_PTR BasePage, PageCount;
95
96 BasePage = BaseAddress / EFI_PAGE_SIZE;
97 PageCount = SizeInPages;
98
99 /* Add the memory descriptor */
102 BasePage,
103 PageCount,
104 MemoryType);
105}
106
107static
110{
111 switch (EfiMemoryType)
112 {
114 return LoaderReserve;
115 case EfiLoaderCode:
116 return LoaderLoadedProgram;
117 case EfiLoaderData:
118 return LoaderLoadedProgram;
128 return LoaderFree;
130 return LoaderBad;
133 case EfiACPIMemoryNVS:
134 return LoaderReserve;
136 return LoaderReserve;
138 return LoaderReserve;
139 default:
140 break;
141 }
142 return LoaderReserve;
143}
144
147{
148 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
149 UINT32 DescriptorVersion;
150 SIZE_T FreeldrMemMapSize;
153 UINTN MapSize;
154 UINTN MapKey;
156
158 PFREELDR_MEMORY_DESCRIPTOR FreeldrMem = NULL;
159 EFI_MEMORY_DESCRIPTOR* MapEntry = NULL;
160 UINT32 EntryCount = 0;
162
165 (VOID**)&LoadedImage);
166 if (Status != EFI_SUCCESS)
167 {
168 TRACE("Failed to find LoadedImageHandle with status: %d\n", Status);
169 UiMessageBoxCritical("Unable to initialize memory manager.");
170 return NULL;
171 }
172 OsLoaderBase = LoadedImage->ImageBase;
173 OsLoaderSize = LoadedImage->ImageSize;
174 PublicBootHandle = LoadedImage->DeviceHandle;
176
177 TRACE("UefiMemGetMemoryMap: Gather memory map\n");
178 PUEFI_LoadMemoryMap(&MapKey,
179 &MapSize,
181 &DescriptorVersion);
182
183 TRACE("Value of MapKey: %d\n", MapKey);
184 TRACE("Value of MapSize: %d\n", MapSize);
185 TRACE("Value of DescriptorSize: %d\n", DescriptorSize);
186 TRACE("Value of DescriptorVersion: %d\n", DescriptorVersion);
187
188 EntryCount = (MapSize / DescriptorSize);
189
190 FreeldrMemMapSize = (sizeof(FREELDR_MEMORY_DESCRIPTOR) * EntryCount);
192 FreeldrMemMapSize,
193 (void**)&FreeldrMem);
194 if (Status != EFI_SUCCESS)
195 {
196 TRACE("Failed to allocate pool with status %d\n", Status);
197 UiMessageBoxCritical("Unable to initialize memory manager.");
198 return NULL;
199 }
200
201 RtlZeroMemory(FreeldrMem, FreeldrMemMapSize);
202 MapEntry = EfiMemoryMap;
203 for (Index = 0; Index < EntryCount; ++Index)
204 {
205 TYPE_OF_MEMORY MemoryType = UefiConvertToFreeldrDesc(MapEntry->Type);
206 if (MemoryType == LoaderFree)
207 {
210 MapEntry->NumberOfPages,
211 &MapEntry->PhysicalStart);
212 if (Status != EFI_SUCCESS)
213 {
214 /* We failed to reserve the page, so change its type */
215 MemoryType = LoaderFirmwareTemporary;
216 }
217 }
218
219 /* Sometimes our loader can be loaded into higher memory than we ever allocate */
220 if (MemoryType == LoaderLoadedProgram)
221 {
222 if (((MapEntry->PhysicalStart + (MapEntry->NumberOfPages * PAGE_SIZE)) >> EFI_PAGE_SHIFT) > LoaderPagesSpanned)
223 {
224 /* This value needs to be adjusted if this occurs */
225 LoaderPagesSpanned = ((MapEntry->PhysicalStart + (MapEntry->NumberOfPages * PAGE_SIZE)) >> EFI_PAGE_SHIFT);
226 }
227 }
228
229 /* We really don't want to touch these reserved spots at all */
230 if (MemoryType != LoaderReserve)
231 {
232 UefiSetMemory(FreeldrMem,
233 MapEntry->PhysicalStart,
234 MapEntry->NumberOfPages,
235 MemoryType);
236 }
237
238 MapEntry = NEXT_MEMORY_DESCRIPTOR(MapEntry, DescriptorSize);
239 }
240
241 /* Windows expects the first page to be reserved, otherwise it asserts.
242 * However it can be just a free page on some UEFI systems. */
243 UefiSetMemory(FreeldrMem, 0x000000, 1, LoaderFirmwarePermanent);
244 *MemoryMapSize = FreeldrDescCount;
245 return FreeldrMem;
246}
247
248VOID
250{
251 UINTN MapKey;
252 UINTN MapSize;
255 UINT32 DescriptorVersion;
256
257 TRACE("Attempting to exit bootsevices\n");
258 PUEFI_LoadMemoryMap(&MapKey,
259 &MapSize,
261 &DescriptorVersion);
262
264 /* UEFI spec demands twice! */
265 if (Status != EFI_SUCCESS)
267
268 if (Status != EFI_SUCCESS)
269 {
270 TRACE("Failed to exit boot services with status: %d\n", Status);
271 FrLdrBugCheckWithMessage(EXIT_BOOTSERVICES_FAILURE,
272 __FILE__,
273 __LINE__,
274 "Failed to exit boot services with status: %d",
275 Status);
276 }
277 else
278 {
279 TRACE("Exited bootservices\n");
280 }
281}
282
283VOID
285{
286 _exituefi();
287}
#define WARNING
Definition: BusLogic958.h:56
#define EFI_LOADED_IMAGE_PROTOCOL_GUID
Definition: LoadedImage.h:21
UINT32 UINTN
unsigned int UINT32
#define EFI_PAGE_SIZE
Definition: UefiBaseType.h:189
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:31
#define EFI_PAGE_SHIFT
Definition: UefiBaseType.h:191
#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
EFI_GUID EfiLoadedImageProtocol
Definition: firmware.c:30
BIOS_MEMORY_MAP MemoryMap[32]
Definition: loader.c:11
VOID FrLdrBugCheckWithMessage(ULONG BugCode, PCHAR File, ULONG Line, PSTR Format,...)
Definition: debug.c:28
#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
#define PAGE_SIZE
Definition: env_spec_w32.h:49
IN PFCB IN PFILE_OBJECT FileObject IN ULONG AllocationSize
Definition: fatprocs.h:322
Status
Definition: gdiplustypes.h:25
#define _Inout_
Definition: ms_sal.h:378
#define _Out_
Definition: ms_sal.h:345
#define _In_
Definition: ms_sal.h:308
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
int Count
Definition: noreturn.cpp:7
@ LoaderBad
Definition: arc.h:177
@ LoaderReserve
Definition: arc.h:198
@ LoaderFree
Definition: arc.h:176
@ LoaderFirmwareTemporary
Definition: arc.h:179
@ LoaderLoadedProgram
Definition: arc.h:178
@ LoaderFirmwarePermanent
Definition: arc.h:180
enum _TYPE_OF_MEMORY TYPE_OF_MEMORY
ULONG PFN_NUMBER
Definition: ke.h:9
#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 ULONG
Definition: typedefs.h:59
UINT32 FreeldrDescCount
Definition: uefimem.c:36
VOID UefiExitBootServices(VOID)
Definition: uefimem.c:249
EFI_MEMORY_DESCRIPTOR * EfiMemoryMap
Definition: uefimem.c:35
PVOID EndofExitStack
Definition: uefimem.c:41
static VOID PUEFI_LoadMemoryMap(_Out_ UINTN *LocMapKey, _Out_ UINTN *LocMapSize, _Out_ UINTN *LocDescriptorSize, _Out_ UINT32 *LocDescriptorVersion)
Definition: uefimem.c:49
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:39
EFI_SYSTEM_TABLE * GlobalSystemTable
Definition: uefildr.c:16
REACTOS_INTERNAL_BGCONTEXT framebufferData
Definition: uefivid.c:25
static TYPE_OF_MEMORY UefiConvertToFreeldrDesc(EFI_MEMORY_TYPE EfiMemoryType)
Definition: uefimem.c:109
PVOID ExitStack
Definition: uefimem.c:40
PFREELDR_MEMORY_DESCRIPTOR UefiMemGetMemoryMap(ULONG *MemoryMapSize)
Definition: uefimem.c:146
PVOID OsLoaderBase
Definition: uefimem.c:37
static VOID UefiSetMemory(_Inout_ PFREELDR_MEMORY_DESCRIPTOR MemoryMap, _In_ ULONG_PTR BaseAddress, _In_ PFN_COUNT SizeInPages, _In_ TYPE_OF_MEMORY MemoryType)
Definition: uefimem.c:88
EFI_HANDLE GlobalImageHandle
Definition: uefildr.c:15
SIZE_T OsLoaderSize
Definition: uefimem.c:38
ULONG LoaderPagesSpanned
Definition: mm.c:29
#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:284
_In_ WDFCOLLECTION _In_ ULONG Index
_Must_inspect_result_ _In_ WDFCMRESLIST List
Definition: wdfresource.h:550
ULONG PFN_COUNT
Definition: mmtypes.h:102