[BOOTMGFW]:
[reactos.git] / reactos / boot / environ / lib / mm / descriptor.c
1 /*
2 * COPYRIGHT: See COPYING.ARM in the top level directory
3 * PROJECT: ReactOS UEFI Boot Library
4 * FILE: boot/environ/lib/mm/descriptor.c
5 * PURPOSE: Boot Library Memory Manager Descriptor Manager
6 * PROGRAMMER: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include "bl.h"
12
13 /* DATA VARIABLES ************************************************************/
14
15 BL_MEMORY_DESCRIPTOR MmStaticMemoryDescriptors[512];
16 ULONG MmGlobalMemoryDescriptorCount;
17 PBL_MEMORY_DESCRIPTOR MmGlobalMemoryDescriptors;
18 BOOLEAN MmGlobalMemoryDescriptorsUsed;
19 PBL_MEMORY_DESCRIPTOR MmDynamicMemoryDescriptors;
20 ULONG MmDynamicMemoryDescriptorCount;
21
22 /* FUNCTIONS *****************************************************************/
23
24 VOID
25 MmMdpSwitchToDynamicDescriptors (
26 _In_ ULONG Count
27 )
28 {
29 EarlyPrint(L"NOT SUPPORTED!!!\n");
30 while (1);
31 }
32
33 NTSTATUS
34 MmMdFreeDescriptor (
35 _In_ PBL_MEMORY_DESCRIPTOR MemoryDescriptor
36 )
37 {
38 NTSTATUS Status;
39
40 /* Check if this is a valid static descriptor */
41 if (((MmDynamicMemoryDescriptors) &&
42 (MemoryDescriptor >= MmDynamicMemoryDescriptors) &&
43 (MemoryDescriptor < (MmDynamicMemoryDescriptors + MmDynamicMemoryDescriptorCount))) ||
44 ((MemoryDescriptor >= MmStaticMemoryDescriptors) && (MemoryDescriptor < &MmStaticMemoryDescriptors[511])))
45 {
46 /* It's a global/static descriptor, so just zero it */
47 RtlZeroMemory(MemoryDescriptor, sizeof(BL_MEMORY_DESCRIPTOR));
48 Status = STATUS_SUCCESS;
49 }
50 else
51 {
52 /* It's a dynamic descriptor, so free it */
53 EarlyPrint(L"Dynamic descriptors not yet supported\n");
54 Status = STATUS_NOT_IMPLEMENTED;
55 }
56
57 /* Done */
58 return Status;
59 }
60
61 VOID
62 MmMdpSaveCurrentListPointer (
63 _In_ PBL_MEMORY_DESCRIPTOR_LIST MdList,
64 _In_ PLIST_ENTRY Current
65 )
66 {
67 /* Make sure that this is not a global descriptor and not the first one */
68 if (((Current < &MmGlobalMemoryDescriptors->ListEntry) ||
69 (Current >= &MmGlobalMemoryDescriptors[MmGlobalMemoryDescriptorCount].ListEntry)) &&
70 (Current != MdList->First))
71 {
72 /* Save this as the current pointer */
73 MdList->This = Current;
74 }
75 }
76
77 VOID
78 MmMdRemoveDescriptorFromList (
79 _In_ PBL_MEMORY_DESCRIPTOR_LIST MdList,
80 _In_ PBL_MEMORY_DESCRIPTOR Entry
81 )
82 {
83 /* Remove the entry */
84 RemoveEntryList(&Entry->ListEntry);
85
86 /* Check if this was the current link */
87 if (MdList->This == &Entry->ListEntry)
88 {
89 /* Remove the current link and set the next one */
90 MdList->This = NULL;
91 MmMdpSaveCurrentListPointer(MdList, Entry->ListEntry.Blink);
92 }
93 }
94
95 VOID
96 MmMdFreeList(
97 _In_ PBL_MEMORY_DESCRIPTOR_LIST MdList
98 )
99 {
100 PLIST_ENTRY FirstEntry, NextEntry;
101 PBL_MEMORY_DESCRIPTOR Entry;
102
103 /* Go over every descriptor from the top */
104 FirstEntry = MdList->First;
105 NextEntry = FirstEntry->Flink;
106 while (NextEntry != FirstEntry)
107 {
108 /* Remove and free each one */
109 Entry = CONTAINING_RECORD(NextEntry, BL_MEMORY_DESCRIPTOR, ListEntry);
110 NextEntry = NextEntry->Flink;
111 MmMdRemoveDescriptorFromList(MdList, Entry);
112 MmMdFreeDescriptor(Entry);
113 }
114 }
115
116 VOID
117 MmMdInitialize (
118 _In_ ULONG Phase,
119 _In_ PBL_LIBRARY_PARAMETERS LibraryParameters
120 )
121 {
122 /* Are we in phase 1? */
123 if (Phase != 0)
124 {
125 /* Switch to dynamic descriptors if we have too many */
126 if (LibraryParameters->DescriptorCount > RTL_NUMBER_OF(MmStaticMemoryDescriptors))
127 {
128 MmMdpSwitchToDynamicDescriptors(LibraryParameters->DescriptorCount);
129 }
130 }
131 else
132 {
133 /* In phase 0, start with a pool of 512 static descriptors */
134 MmGlobalMemoryDescriptorCount = RTL_NUMBER_OF(MmStaticMemoryDescriptors);
135 MmGlobalMemoryDescriptors = MmStaticMemoryDescriptors;
136 RtlZeroMemory(MmStaticMemoryDescriptors, sizeof(MmStaticMemoryDescriptors));
137 MmGlobalMemoryDescriptorsUsed = FALSE;
138 }
139 }