ReactOS 0.4.15-dev-6644-g539123c
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)
 
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 Size, _In_ TYPE_OF_MEMORY MemoryType)
 
VOID ReserveMemory (_Inout_ PFREELDR_MEMORY_DESCRIPTOR MemoryMap, _In_ ULONG_PTR BaseAddress, _In_ PFN_NUMBER Size, _In_ TYPE_OF_MEMORY MemoryType, _In_ PCHAR Usage)
 
static TYPE_OF_MEMORY UefiConvertToFreeldrDesc (EFI_MEMORY_TYPE EfiMemoryType)
 
PFREELDR_MEMORY_DESCRIPTOR UefiMemGetMemoryMap (ULONG *MemoryMapSize)
 
static VOID UefiExitBootServices (VOID)
 
VOID UefiPrepareForReactOS (VOID)
 

Variables

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

◆ 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 ReserveMemory(), and 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 46 of file uefimem.c.

51{
54 ULONG Count = 0;
55
58 LocMapKey,
59 LocDescriptorSize,
60 LocDescriptorVersion);
61
62 /* Reallocate and retrieve again the needed memory map size (since memory
63 * allocated by AllocatePool() counts in the map), until it's OK. */
64 while (Status != EFI_SUCCESS)
65 {
66 /* Reallocate the memory map buffer */
67 if (EfiMemoryMap)
69
70 /* If MapSize never reports the correct size after the first time, increment */
71 AllocationSize = *LocMapSize + (*LocDescriptorSize * Count);
73 (VOID**)&EfiMemoryMap);
76 LocMapKey,
77 LocDescriptorSize,
78 LocDescriptorVersion);
79 Count++;
80 }
81}
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:322
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:34
EFI_SYSTEM_TABLE * GlobalSystemTable
Definition: uefildr.c:15

Referenced by UefiExitBootServices(), and UefiMemGetMemoryMap().

◆ ReserveMemory()

VOID ReserveMemory ( _Inout_ PFREELDR_MEMORY_DESCRIPTOR  MemoryMap,
_In_ ULONG_PTR  BaseAddress,
_In_ PFN_NUMBER  Size,
_In_ TYPE_OF_MEMORY  MemoryType,
_In_ PCHAR  Usage 
)

Definition at line 105 of file uefimem.c.

111{
112 ULONG_PTR BasePage, PageCount;
113 ULONG i;
114
115 BasePage = BaseAddress / PAGE_SIZE;
117
118 for (i = 0; i < FreeldrDescCount; i++)
119 {
120 /* Check for conflicting descriptor */
121 if ((MemoryMap[i].BasePage < BasePage + PageCount) &&
122 (MemoryMap[i].BasePage + MemoryMap[i].PageCount > BasePage))
123 {
124 /* Check if the memory is free */
125 if (MemoryMap[i].MemoryType != LoaderFree)
126 {
129 __FILE__,
130 __LINE__,
131 "Failed to reserve memory in the range 0x%Ix - 0x%Ix for %s",
133 Size,
134 Usage);
135 }
136 }
137 }
138
139 /* Add the memory descriptor */
142 BasePage,
143 PageCount,
144 MemoryType);
145}
BIOS_MEMORY_MAP MemoryMap[32]
Definition: loader.c:11
VOID FrLdrBugCheckWithMessage(ULONG BugCode, PCHAR File, ULONG Line, PSTR Format,...)
Definition: debug.c:28
@ MEMORY_INIT_FAILURE
Definition: debug.h:143
#define PAGE_SIZE
Definition: env_spec_w32.h:49
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_Must_inspect_result_ _In_ USAGE _In_ USHORT _In_ USAGE Usage
Definition: hidpi.h:384
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
@ LoaderFree
Definition: arc.h:176
uint32_t ULONG_PTR
Definition: typedefs.h:65
UINT32 FreeldrDescCount
Definition: uefimem.c:35
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
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
#define ADDRESS_AND_SIZE_TO_SPAN_PAGES(_Va, _Size)

◆ UefiConvertToFreeldrDesc()

static TYPE_OF_MEMORY UefiConvertToFreeldrDesc ( EFI_MEMORY_TYPE  EfiMemoryType)
static

Definition at line 149 of file uefimem.c.

150{
151 switch (EfiMemoryType)
152 {
154 return LoaderReserve;
155 case EfiLoaderCode:
156 return LoaderLoadedProgram;
157 case EfiLoaderData:
158 return LoaderLoadedProgram;
168 return LoaderFree;
170 return LoaderBad;
173 case EfiACPIMemoryNVS:
174 return LoaderReserve;
176 return LoaderReserve;
178 return LoaderReserve;
179 default:
180 break;
181 }
182 return LoaderReserve;
183}
@ EfiUnusableMemory
@ EfiBootServicesData
@ EfiReservedMemoryType
@ EfiBootServicesCode
@ EfiConventionalMemory
@ EfiACPIMemoryNVS
@ EfiMemoryMappedIOPortSpace
@ EfiACPIReclaimMemory
@ EfiLoaderCode
@ EfiMemoryMappedIO
@ EfiRuntimeServicesCode
@ EfiRuntimeServicesData
@ LoaderBad
Definition: arc.h:177
@ LoaderReserve
Definition: arc.h:198
@ LoaderFirmwareTemporary
Definition: arc.h:179
@ LoaderLoadedProgram
Definition: arc.h:178
@ LoaderFirmwarePermanent
Definition: arc.h:180

Referenced by UefiMemGetMemoryMap().

◆ UefiExitBootServices()

static VOID UefiExitBootServices ( VOID  )
static

Definition at line 272 of file uefimem.c.

273{
274 UINTN MapKey;
275 UINTN MapSize;
278 UINT32 DescriptorVersion;
279
280 TRACE("Attempting to exit bootsevices\n");
281 PUEFI_LoadMemoryMap(&MapKey,
282 &MapSize,
284 &DescriptorVersion);
285
287 /* UEFI spec demands twice! */
288 if (Status != EFI_SUCCESS)
290
291 if (Status != EFI_SUCCESS)
292 {
293 TRACE("Failed to exit boot services with status: %d\n", Status);
294 FrLdrBugCheckWithMessage(EXIT_BOOTSERVICES_FAILURE,
295 __FILE__,
296 __LINE__,
297 "Failed to exit boot services with status: %d",
298 Status);
299 }
300 else
301 {
302 TRACE("Exited bootservices\n");
303 }
304}
unsigned int UINT32
#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:46
EFI_HANDLE GlobalImageHandle
Definition: uefildr.c:14

Referenced by UefiPrepareForReactOS().

◆ UefiMemGetMemoryMap()

PFREELDR_MEMORY_DESCRIPTOR UefiMemGetMemoryMap ( ULONG MemoryMapSize)

Definition at line 186 of file uefimem.c.

187{
188 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
189 UINT32 DescriptorVersion;
190 SIZE_T FreeldrMemMapSize;
193 UINTN MapSize;
194 UINTN MapKey;
196
198 PFREELDR_MEMORY_DESCRIPTOR FreeldrMem = NULL;
199 EFI_MEMORY_DESCRIPTOR* MapEntry = NULL;
200 UINT32 EntryCount = 0;
202
205 (VOID**)&LoadedImage);
206 if (Status != EFI_SUCCESS)
207 {
208 TRACE("Failed to find LoadedImageHandle with status: %d\n", Status);
209 UiMessageBoxCritical("Unable to initialize memory manager.");
210 return NULL;
211 }
212 OsLoaderBase = LoadedImage->ImageBase;
213 OsLoaderSize = LoadedImage->ImageSize;
214 PublicBootHandle = LoadedImage->DeviceHandle;
216
217 TRACE("UefiMemGetMemoryMap: Gather memory map\n");
218 PUEFI_LoadMemoryMap(&MapKey,
219 &MapSize,
221 &DescriptorVersion);
222
223 TRACE("Value of MapKey: %d\n", MapKey);
224 TRACE("Value of MapSize: %d\n", MapSize);
225 TRACE("Value of DescriptorSize: %d\n", DescriptorSize);
226 TRACE("Value of DescriptorVersion: %d\n", DescriptorVersion);
227
228 EntryCount = (MapSize / DescriptorSize);
229
230 FreeldrMemMapSize = (sizeof(FREELDR_MEMORY_DESCRIPTOR) * EntryCount);
232 FreeldrMemMapSize,
233 (void**)&FreeldrMem);
234 if (Status != EFI_SUCCESS)
235 {
236 TRACE("Failed to allocate pool with status %d\n", Status);
237 UiMessageBoxCritical("Unable to initialize memory manager.");
238 return NULL;
239 }
240
241 RtlZeroMemory(FreeldrMem, FreeldrMemMapSize);
242 MapEntry = EfiMemoryMap;
243 for (Index = 0; Index < EntryCount; ++Index)
244 {
245 TYPE_OF_MEMORY MemoryType = UefiConvertToFreeldrDesc(MapEntry->Type);
246 if (MemoryType == LoaderFree)
247 {
250 MapEntry->NumberOfPages,
251 &MapEntry->PhysicalStart);
252 if (Status != EFI_SUCCESS)
253 {
254 /* We failed to reserve the page, so change its type */
255 MemoryType = LoaderFirmwareTemporary;
256 }
257 }
258
259 UefiSetMemory(FreeldrMem,
260 MapEntry->PhysicalStart,
261 MapEntry->NumberOfPages,
262 MemoryType);
263
264 MapEntry = NEXT_MEMORY_DESCRIPTOR(MapEntry, DescriptorSize);
265 }
266
267 *MemoryMapSize = FreeldrDescCount;
268 return FreeldrMem;
269}
#define EFI_LOADED_IMAGE_PROTOCOL_GUID
Definition: LoadedImage.h:21
@ AllocateAddress
Definition: UefiSpec.h:45
EFI_GUID EfiLoadedImageProtocol
Definition: firmware.c:30
struct _FREELDR_MEMORY_DESCRIPTOR FREELDR_MEMORY_DESCRIPTOR
#define NULL
Definition: types.h:112
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
EFI_HANDLE PublicBootHandle
Definition: uefimem.c:38
static TYPE_OF_MEMORY UefiConvertToFreeldrDesc(EFI_MEMORY_TYPE EfiMemoryType)
Definition: uefimem.c:149
static VOID UefiSetMemory(_Inout_ PFREELDR_MEMORY_DESCRIPTOR MemoryMap, _In_ ULONG_PTR BaseAddress, _In_ PFN_COUNT Size, _In_ TYPE_OF_MEMORY MemoryType)
Definition: uefimem.c:85
PVOID OsLoaderBase
Definition: uefimem.c:36
SIZE_T OsLoaderSize
Definition: uefimem.c:37
#define NEXT_MEMORY_DESCRIPTOR(Descriptor, DescriptorSize)
Definition: uefimem.c:15
VOID UiMessageBoxCritical(PCSTR MessageText)
Definition: ui.c:368
_In_ WDFCOLLECTION _In_ ULONG Index

Referenced by MachInit().

◆ UefiPrepareForReactOS()

VOID UefiPrepareForReactOS ( VOID  )

Definition at line 307 of file uefimem.c.

308{
312}
PVOID MmAllocateMemoryWithType(SIZE_T MemorySize, TYPE_OF_MEMORY MemoryType)
Definition: mm.c:31
@ LoaderOsloaderStack
Definition: arc.h:182
void * PVOID
Definition: typedefs.h:50
#define EXIT_STACK_SIZE
Definition: uefimem.c:17
PVOID EndofExitStack
Definition: uefimem.c:40
PVOID ExitStack
Definition: uefimem.c:39
static VOID UefiExitBootServices(VOID)
Definition: uefimem.c:272

Referenced by MachInit().

◆ UefiSetMemory()

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

Definition at line 85 of file uefimem.c.

90{
91 ULONG_PTR BasePage, PageCount;
92
93 BasePage = BaseAddress / EFI_PAGE_SIZE;
94 PageCount = Size;
95
96 /* Add the memory descriptor */
99 BasePage,
100 PageCount,
101 MemoryType);
102}
#define EFI_PAGE_SIZE
Definition: UefiBaseType.h:189

Referenced by UefiMemGetMemoryMap().

Variable Documentation

◆ EfiMemoryMap

EFI_MEMORY_DESCRIPTOR* EfiMemoryMap = NULL

Definition at line 34 of file uefimem.c.

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

◆ EndofExitStack

PVOID EndofExitStack

Definition at line 40 of file uefimem.c.

Referenced by UefiPrepareForReactOS().

◆ ExitStack

PVOID ExitStack

Definition at line 39 of file uefimem.c.

Referenced by UefiPrepareForReactOS().

◆ framebufferData

◆ FreeldrDescCount

UINT32 FreeldrDescCount

Definition at line 35 of file uefimem.c.

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

◆ GlobalImageHandle

EFI_HANDLE GlobalImageHandle
extern

Definition at line 14 of file uefildr.c.

Referenced by UefiExitBootServices(), and UefiMemGetMemoryMap().

◆ GlobalSystemTable

EFI_SYSTEM_TABLE* GlobalSystemTable
extern

Definition at line 15 of file uefildr.c.

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

◆ OsLoaderBase

PVOID OsLoaderBase

Definition at line 36 of file uefimem.c.

Referenced by UefiMemGetMemoryMap().

◆ OsLoaderSize

SIZE_T OsLoaderSize

Definition at line 37 of file uefimem.c.

Referenced by UefiMemGetMemoryMap().

◆ PublicBootHandle

EFI_HANDLE PublicBootHandle

Definition at line 38 of file uefimem.c.

Referenced by UefiMemGetMemoryMap(), and UefiSetupBlockDevices().