ReactOS 0.4.16-dev-297-gc569aee
uefimem.c File Reference
#include <uefildr.h>
#include <debug.h>
Include dependency graph for uefimem.c:

Go to the source code of this file.

Macros

#define NEXT_MEMORY_DESCRIPTOR(Descriptor, DescriptorSize)    (EFI_MEMORY_DESCRIPTOR*)((char*)(Descriptor) + (DescriptorSize))
 
#define EXIT_STACK_SIZE   0x1000
 
#define UNUSED_MAX_DESCRIPTOR_COUNT   10000
 

Functions

 DBG_DEFAULT_CHANNEL (WARNING)
 
ULONG AddMemoryDescriptor (_Inout_ PFREELDR_MEMORY_DESCRIPTOR List, _In_ ULONG MaxCount, _In_ PFN_NUMBER BasePage, _In_ PFN_NUMBER PageCount, _In_ TYPE_OF_MEMORY MemoryType)
 
void _exituefi (VOID)
 
static VOID PUEFI_LoadMemoryMap (_Out_ UINTN *LocMapKey, _Out_ UINTN *LocMapSize, _Out_ UINTN *LocDescriptorSize, _Out_ UINT32 *LocDescriptorVersion)
 
static VOID UefiSetMemory (_Inout_ PFREELDR_MEMORY_DESCRIPTOR MemoryMap, _In_ ULONG_PTR BaseAddress, _In_ PFN_COUNT SizeInPages, _In_ TYPE_OF_MEMORY MemoryType)
 
static TYPE_OF_MEMORY UefiConvertToFreeldrDesc (EFI_MEMORY_TYPE EfiMemoryType)
 
PFREELDR_MEMORY_DESCRIPTOR UefiMemGetMemoryMap (ULONG *MemoryMapSize)
 
VOID UefiExitBootServices (VOID)
 
VOID UefiPrepareForReactOS (VOID)
 

Variables

ULONG LoaderPagesSpanned
 
EFI_SYSTEM_TABLEGlobalSystemTable
 
EFI_HANDLE GlobalImageHandle
 
REACTOS_INTERNAL_BGCONTEXT framebufferData
 
EFI_MEMORY_DESCRIPTOREfiMemoryMap = NULL
 
UINT32 FreeldrDescCount
 
PVOID OsLoaderBase
 
SIZE_T OsLoaderSize
 
EFI_HANDLE PublicBootHandle
 
PVOID ExitStack
 
PVOID EndofExitStack
 

Macro Definition Documentation

◆ EXIT_STACK_SIZE

#define EXIT_STACK_SIZE   0x1000

Definition at line 17 of file uefimem.c.

◆ NEXT_MEMORY_DESCRIPTOR

#define NEXT_MEMORY_DESCRIPTOR (   Descriptor,
  DescriptorSize 
)     (EFI_MEMORY_DESCRIPTOR*)((char*)(Descriptor) + (DescriptorSize))

Definition at line 15 of file uefimem.c.

◆ UNUSED_MAX_DESCRIPTOR_COUNT

#define UNUSED_MAX_DESCRIPTOR_COUNT   10000

Definition at line 18 of file uefimem.c.

Function Documentation

◆ _exituefi()

void _exituefi ( VOID  )

Referenced by UefiPrepareForReactOS().

◆ AddMemoryDescriptor()

ULONG AddMemoryDescriptor ( _Inout_ PFREELDR_MEMORY_DESCRIPTOR  List,
_In_ ULONG  MaxCount,
_In_ PFN_NUMBER  BasePage,
_In_ PFN_NUMBER  PageCount,
_In_ TYPE_OF_MEMORY  MemoryType 
)

Referenced by UefiSetMemory().

◆ DBG_DEFAULT_CHANNEL()

DBG_DEFAULT_CHANNEL ( WARNING  )

◆ PUEFI_LoadMemoryMap()

static VOID PUEFI_LoadMemoryMap ( _Out_ UINTN LocMapKey,
_Out_ UINTN LocMapSize,
_Out_ UINTN LocDescriptorSize,
_Out_ UINT32 LocDescriptorVersion 
)
static

Definition at line 49 of file uefimem.c.

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}
UINT32 UINTN
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:31
#define EFI_SUCCESS
Definition: UefiBaseType.h:120
@ EfiLoaderData
IN PFCB IN PFILE_OBJECT FileObject IN ULONG AllocationSize
Definition: fatprocs.h:323
Status
Definition: gdiplustypes.h:25
int Count
Definition: noreturn.cpp:7
EFI_FREE_POOL FreePool
Definition: UefiSpec.h:1814
EFI_ALLOCATE_POOL AllocatePool
Definition: UefiSpec.h:1813
EFI_GET_MEMORY_MAP GetMemoryMap
Definition: UefiSpec.h:1812
EFI_BOOT_SERVICES * BootServices
Definition: UefiSpec.h:1959
uint32_t ULONG
Definition: typedefs.h:59
EFI_MEMORY_DESCRIPTOR * EfiMemoryMap
Definition: uefimem.c:35
EFI_SYSTEM_TABLE * GlobalSystemTable
Definition: uefildr.c:16

Referenced by UefiExitBootServices(), and UefiMemGetMemoryMap().

◆ UefiConvertToFreeldrDesc()

static TYPE_OF_MEMORY UefiConvertToFreeldrDesc ( EFI_MEMORY_TYPE  EfiMemoryType)
static

Definition at line 109 of file uefimem.c.

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}
@ EfiUnusableMemory
@ EfiBootServicesData
@ EfiReservedMemoryType
@ EfiBootServicesCode
@ EfiConventionalMemory
@ EfiACPIMemoryNVS
@ EfiMemoryMappedIOPortSpace
@ EfiACPIReclaimMemory
@ EfiLoaderCode
@ EfiMemoryMappedIO
@ EfiRuntimeServicesCode
@ EfiRuntimeServicesData
@ 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

Referenced by UefiMemGetMemoryMap().

◆ UefiExitBootServices()

VOID UefiExitBootServices ( VOID  )

Definition at line 249 of file uefimem.c.

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}
unsigned int UINT32
VOID FrLdrBugCheckWithMessage(ULONG BugCode, PCHAR File, ULONG Line, PSTR Format,...)
Definition: debug.c:28
#define TRACE(s)
Definition: solgame.cpp:4
EFI_EXIT_BOOT_SERVICES ExitBootServices
Definition: UefiSpec.h:1846
_In_ SIZE_T DescriptorSize
Definition: nls.c:40
static VOID PUEFI_LoadMemoryMap(_Out_ UINTN *LocMapKey, _Out_ UINTN *LocMapSize, _Out_ UINTN *LocDescriptorSize, _Out_ UINT32 *LocDescriptorVersion)
Definition: uefimem.c:49
EFI_HANDLE GlobalImageHandle
Definition: uefildr.c:15

◆ UefiMemGetMemoryMap()

PFREELDR_MEMORY_DESCRIPTOR UefiMemGetMemoryMap ( ULONG MemoryMapSize)

Definition at line 146 of file uefimem.c.

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}
#define EFI_LOADED_IMAGE_PROTOCOL_GUID
Definition: LoadedImage.h:21
#define EFI_PAGE_SHIFT
Definition: UefiBaseType.h:191
@ AllocateAddress
Definition: UefiSpec.h:45
EFI_GUID EfiLoadedImageProtocol
Definition: firmware.c:30
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
enum _TYPE_OF_MEMORY TYPE_OF_MEMORY
EFI_ALLOCATE_PAGES AllocatePages
Definition: UefiSpec.h:1810
EFI_HANDLE_PROTOCOL HandleProtocol
Definition: UefiSpec.h:1832
EFI_PHYSICAL_ADDRESS PhysicalStart
Definition: UefiSpec.h:99
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
UINT32 FreeldrDescCount
Definition: uefimem.c:36
EFI_HANDLE PublicBootHandle
Definition: uefimem.c:39
static TYPE_OF_MEMORY UefiConvertToFreeldrDesc(EFI_MEMORY_TYPE EfiMemoryType)
Definition: uefimem.c:109
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
SIZE_T OsLoaderSize
Definition: uefimem.c:38
ULONG LoaderPagesSpanned
Definition: mm.c:29
#define NEXT_MEMORY_DESCRIPTOR(Descriptor, DescriptorSize)
Definition: uefimem.c:15
_In_ WDFCOLLECTION _In_ ULONG Index

Referenced by MachInit().

◆ UefiPrepareForReactOS()

VOID UefiPrepareForReactOS ( VOID  )

Definition at line 284 of file uefimem.c.

285{
286 _exituefi();
287}
void _exituefi(VOID)

Referenced by MachInit().

◆ UefiSetMemory()

static VOID UefiSetMemory ( _Inout_ PFREELDR_MEMORY_DESCRIPTOR  MemoryMap,
_In_ ULONG_PTR  BaseAddress,
_In_ PFN_COUNT  SizeInPages,
_In_ TYPE_OF_MEMORY  MemoryType 
)
static

Definition at line 88 of file uefimem.c.

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}
#define EFI_PAGE_SIZE
Definition: UefiBaseType.h:189
BIOS_MEMORY_MAP MemoryMap[32]
Definition: loader.c:11
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
uint32_t ULONG_PTR
Definition: typedefs.h:65
ULONG AddMemoryDescriptor(_Inout_ PFREELDR_MEMORY_DESCRIPTOR List, _In_ ULONG MaxCount, _In_ PFN_NUMBER BasePage, _In_ PFN_NUMBER PageCount, _In_ TYPE_OF_MEMORY MemoryType)
#define UNUSED_MAX_DESCRIPTOR_COUNT
Definition: uefimem.c:18

Referenced by UefiMemGetMemoryMap().

Variable Documentation

◆ EfiMemoryMap

EFI_MEMORY_DESCRIPTOR* EfiMemoryMap = NULL

◆ EndofExitStack

PVOID EndofExitStack

Definition at line 41 of file uefimem.c.

◆ ExitStack

PVOID ExitStack

Definition at line 40 of file uefimem.c.

◆ framebufferData

◆ FreeldrDescCount

UINT32 FreeldrDescCount

Definition at line 36 of file uefimem.c.

Referenced by DetectAcpiBios(), UefiMemGetMemoryMap(), and UefiSetMemory().

◆ GlobalImageHandle

EFI_HANDLE GlobalImageHandle
extern

Definition at line 15 of file uefildr.c.

Referenced by UefiExitBootServices(), and UefiMemGetMemoryMap().

◆ GlobalSystemTable

EFI_SYSTEM_TABLE* GlobalSystemTable
extern

Definition at line 16 of file uefildr.c.

Referenced by PUEFI_LoadMemoryMap(), UefiExitBootServices(), and UefiMemGetMemoryMap().

◆ LoaderPagesSpanned

ULONG LoaderPagesSpanned
extern

Definition at line 29 of file mm.c.

Referenced by UefiMemGetMemoryMap().

◆ OsLoaderBase

PVOID OsLoaderBase

Definition at line 37 of file uefimem.c.

Referenced by UefiMemGetMemoryMap().

◆ OsLoaderSize

SIZE_T OsLoaderSize

Definition at line 38 of file uefimem.c.

Referenced by UefiMemGetMemoryMap().

◆ PublicBootHandle

EFI_HANDLE PublicBootHandle

Definition at line 39 of file uefimem.c.

Referenced by UefiMemGetMemoryMap(), and UefiSetupBlockDevices().