ReactOS 0.4.16-dev-2300-g249be9e
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

EFI_SYSTEM_TABLEGlobalSystemTable
 
EFI_HANDLE GlobalImageHandle
 
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 47 of file uefimem.c.

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}
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:33
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 107 of file uefimem.c.

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

Referenced by UefiMemGetMemoryMap().

◆ UefiExitBootServices()

VOID UefiExitBootServices ( VOID  )

Definition at line 237 of file uefimem.c.

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

◆ UefiMemGetMemoryMap()

PFREELDR_MEMORY_DESCRIPTOR UefiMemGetMemoryMap ( ULONG MemoryMapSize)

Definition at line 144 of file uefimem.c.

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}
#define EFI_LOADED_IMAGE_PROTOCOL_GUID
Definition: LoadedImage.h:21
@ AllocateAddress
Definition: UefiSpec.h:45
struct _FREELDR_MEMORY_DESCRIPTOR FREELDR_MEMORY_DESCRIPTOR
VOID UiMessageBoxCritical(_In_ PCSTR MessageText)
Definition: ui.c:372
#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
UINT32 FreeldrDescCount
Definition: uefimem.c:34
EFI_HANDLE PublicBootHandle
Definition: uefimem.c:37
static TYPE_OF_MEMORY UefiConvertToFreeldrDesc(EFI_MEMORY_TYPE EfiMemoryType)
Definition: uefimem.c:107
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
SIZE_T OsLoaderSize
Definition: uefimem.c:36
#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 272 of file uefimem.c.

273{
274 _exituefi();
275}
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 86 of file uefimem.c.

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}
#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

Definition at line 33 of file uefimem.c.

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

◆ EndofExitStack

PVOID EndofExitStack

Definition at line 39 of file uefimem.c.

◆ ExitStack

PVOID ExitStack

Definition at line 38 of file uefimem.c.

◆ FreeldrDescCount

UINT32 FreeldrDescCount

Definition at line 34 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().

◆ OsLoaderBase

PVOID OsLoaderBase

Definition at line 35 of file uefimem.c.

Referenced by UefiMemGetMemoryMap(), and WinLdrSetupMemoryLayout().

◆ OsLoaderSize

SIZE_T OsLoaderSize

Definition at line 36 of file uefimem.c.

Referenced by UefiMemGetMemoryMap(), and WinLdrSetupMemoryLayout().

◆ PublicBootHandle

EFI_HANDLE PublicBootHandle

Definition at line 37 of file uefimem.c.

Referenced by UefiMemGetMemoryMap(), and UefiSetupBlockDevices().