ReactOS  0.4.12-dev-708-g95ed44e
blkalloc.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING.ARM in the top level directory
3  * PROJECT: ReactOS UEFI Boot Library
4  * FILE: boot/environ/lib/mm/blkalloc.c
5  * PURPOSE: Boot Library Memory Manager Block Allocator
6  * PROGRAMMER: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8 
9 /* INCLUDES ******************************************************************/
10 
11 #include "bl.h"
12 
13 /* DATA VARIABLES ************************************************************/
14 
18 
19 typedef struct _BL_BLOCK_DESCRIPTOR
20 {
32 
33 typedef struct _BL_BLOCK_ENTRY
34 {
38 
39 /* FUNCTIONS *****************************************************************/
40 
41 BOOLEAN
46  _In_ PVOID Argument3,
47  _In_ PVOID Argument4
48  )
49 {
51  ULONG BlockId = PtrToUlong(Argument1);
52 
53  /* Check if the block ID matches */
54  return BlockInfo->BlockId == BlockId;
55 }
56 
59  ULONG BlockId
60  )
61 {
62  ULONG EntryId;
63 
64  /* Find the block that matches */
65  EntryId = BlockId;
68  &EntryId,
70  UlongToPtr(EntryId),
71  NULL,
72  NULL,
73  NULL);
74 }
75 
78  _In_ PBL_BLOCK_DESCRIPTOR BlockInfo,
79  _In_ PBL_BLOCK_ENTRY BlockEntry
80  )
81 {
82  /* @TODO FIXME: Later */
83  EfiPrintf(L"Block free not yet implemented\r\n");
85 }
86 
89  _In_ ULONG BlockId
90  )
91 {
92  NTSTATUS Status, LocalStatus;
93  PBL_BLOCK_DESCRIPTOR BlockInfo;
94  PLIST_ENTRY ListHead, NextEntry;
95  PBL_BLOCK_ENTRY BlockEntry;
96 
97  /* Nothing to delete if we're not initialized */
99  {
100  return STATUS_UNSUCCESSFUL;
101  }
102 
103  /* Find the block descriptor */
104  BlockInfo = MmBapFindBlockInformation(BlockId);
105  if (BlockInfo)
106  {
107  /* Assume success for now */
109 
110  /* Do we have at least one reference? */
111  if (BlockInfo->ReferenceCount)
112  {
113  /* Iterate over the allocated blocks */
114  ListHead = &BlockInfo->ListHead;
115  NextEntry = ListHead->Flink;
116  while (NextEntry != ListHead)
117  {
118  /* Free each one */
119  BlockEntry = CONTAINING_RECORD(NextEntry,
121  ListEntry);
122  LocalStatus = MmBapFreeBlockAllocatorDescriptor(BlockInfo,
123  BlockEntry);
124  if (!NT_SUCCESS(LocalStatus))
125  {
126  /* Remember status on failure only */
127  Status = LocalStatus;
128  }
129  }
130 
131  /* Drop a reference */
132  BlockInfo->ReferenceCount--;
133  }
134  else
135  {
136  /* There aren't any references, so why are we being called? */
138  }
139  }
140  else
141  {
142  /* No block exists with this ID */
144  }
145 
146  /* Return back */
147  return Status;
148 }
149 
150 NTSTATUS
152  _In_ PVOID Entry,
154  )
155 {
157  NTSTATUS Status, LocalStatus;
158 
159  /* Assume success */
161 
162  /* Check if there was at least one reference */
163  if (BlockInfo->ReferenceCount > 1)
164  {
165  /* Try to delete the allocator */
166  LocalStatus = BlpMmDeleteBlockAllocator(BlockInfo->BlockId);
167  if (!NT_SUCCESS(LocalStatus))
168  {
169  /* Remember status on failure only */
170  Status = LocalStatus;
171  }
172  }
173 
174  /* Now destroy the allocator's descriptor */
175  LocalStatus = BlMmFreeHeap(BlockInfo);
176  if (!NT_SUCCESS(LocalStatus))
177  {
178  /* Remember status on failure only */
179  Status = LocalStatus;
180  }
181 
182  /* Free the entry, and return failure, if any */
184  return Status;
185 }
186 
187 NTSTATUS
190  )
191 {
194 
195  /* Check if there's a reference on the block descriptor */
196  if (BlockInfo->ReferenceCount)
197  {
198  /* Don't allow purging */
200  }
201  else
202  {
203  /* Free the entry */
205  BlockInfo->BlockId);
206  }
207 
208  /* Return purge status */
209  return Status;
210 }
211 
212 NTSTATUS
214  VOID
215  )
216 {
217  PBL_BLOCK_DESCRIPTOR BlockInfo;
218  ULONG BlockId;
220 
221  /* If the block allocator isn't initialized, bail out */
222  BlockId = -1;
224  {
225  goto Quickie;
226  }
227 
228  /* Allocate a block descriptor and zero it out */
229  BlockInfo = BlMmAllocateHeap(sizeof(*BlockInfo));
230  if (!BlockInfo)
231  {
232  goto Quickie;
233  }
234  RtlZeroMemory(BlockInfo, sizeof(*BlockInfo));
235 
236  /* Setup the block descriptor */
237  BlockInfo->Attributes = 0;
238  BlockInfo->Type = BlLoaderBlockMemory;
239  BlockInfo->Unknown = 1;
240  BlockInfo->Unknown2 = 1;
241  BlockInfo->Size = PAGE_SIZE;
242  BlockInfo->Count = 128;
243  BlockInfo->Count2 = 128;
244  InitializeListHead(&BlockInfo->ListHead);
245 
246  /* Add it to the list of block descriptors */
249  BlockInfo,
250  &BlockId,
252  if (NT_SUCCESS(Status))
253  {
254  /* Add the initial reference and store the block ID */
255  BlockInfo->ReferenceCount = 1;
256  BlockInfo->BlockId = BlockId;
257  }
258 
259 Quickie:
260  /* On failure, free the block descriptor */
261  if (BlockId == -1)
262  {
263  BlMmFreeHeap(BlockInfo);
264  }
265 
266  /* Return the block descriptor ID, or -1 on failure */
267  return BlockId;
268 }
269 
270 NTSTATUS
272  VOID
273  )
274 {
276  ULONG Size;
277 
278  /* Allocate 8 table entries */
283  {
284  /* Zero them out -- we're all done */
288  }
289  else
290  {
291  /* Bail out since we're out of memory */
294  }
295 
296  /* Return initialization status */
297  return Status;
298 }
_IRQL_requires_same_ _In_opt_ PVOID Argument1
Definition: cmtypes.h:694
NTSTATUS MmBaInitialize(VOID)
Definition: blkalloc.c:271
#define STATUS_NOT_IMPLEMENTED
Definition: ntstatus.h:225
NTSTATUS MmBapFreeBlockAllocatorDescriptor(_In_ PBL_BLOCK_DESCRIPTOR BlockInfo, _In_ PBL_BLOCK_ENTRY BlockEntry)
Definition: blkalloc.c:77
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
struct _BL_BLOCK_DESCRIPTOR * PBL_BLOCK_DESCRIPTOR
LONG NTSTATUS
Definition: precomp.h:26
BOOLEAN MmBlockAllocatorInitialized
Definition: blkalloc.c:17
NTSTATUS BlMmFreeHeap(_In_ PVOID Buffer)
Definition: heapalloc.c:663
NTSTATUS BlpMmDeleteBlockAllocator(_In_ ULONG BlockId)
Definition: blkalloc.c:88
NTSTATUS BlpMmCreateBlockAllocator(VOID)
Definition: blkalloc.c:213
ULONG Todo
Definition: blkalloc.c:36
unsigned char BOOLEAN
PVOID * MmBlockAllocatorTable
Definition: blkalloc.c:15
PBL_BLOCK_DESCRIPTOR MmBapFindBlockInformation(ULONG BlockId)
Definition: blkalloc.c:58
ULONG MmBlockAllocatorTableEntries
Definition: blkalloc.c:16
smooth NULL
Definition: ftsmooth.c:416
PVOID BlMmAllocateHeap(_In_ SIZE_T Size)
Definition: heapalloc.c:569
struct _BL_BLOCK_DESCRIPTOR BL_BLOCK_DESCRIPTOR
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
_In_ PVOID Argument2
Definition: classpnp.h:680
#define PtrToUlong(p)
Definition: basetsd.h:83
Definition: blkalloc.c:33
struct _LIST_ENTRY * Flink
Definition: typedefs.h:119
struct _BL_BLOCK_ENTRY BL_BLOCK_ENTRY
#define UlongToPtr(ul)
Definition: basetsd.h:98
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:24
static const UCHAR Index[8]
Definition: usbohci.c:18
PVOID BlTblFindEntry(_In_ PVOID *Table, _In_ ULONG Count, _Out_ PULONG EntryIndex, _In_ PBL_TBL_LOOKUP_ROUTINE Callback, _In_ PVOID Argument1, _In_ PVOID Argument2, _In_ PVOID Argument3, _In_ PVOID Argument4)
Definition: util.c:273
enum _BL_MEMORY_TYPE BL_MEMORY_TYPE
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
static const WCHAR L[]
Definition: oid.c:1250
LIST_ENTRY ListHead
Definition: blkalloc.c:21
NTSTATUS MmBapPurgeBlockAllocatorTableEntry(_In_ PVOID Entry)
Definition: blkalloc.c:188
#define PAGE_SIZE
Definition: env_spec_w32.h:49
Definition: typedefs.h:117
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
Status
Definition: gdiplustypes.h:24
#define _In_
Definition: no_sal2.h:204
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
#define STATUS_NO_MEMORY
Definition: ntstatus.h:246
VOID EfiPrintf(_In_ PWCHAR Format,...)
Definition: firmware.c:126
BOOLEAN MmBapCompareBlockAllocatorTableEntry(_In_ PVOID Entry, _In_ PVOID Argument1, _In_ PVOID Argument2, _In_ PVOID Argument3, _In_ PVOID Argument4)
Definition: blkalloc.c:42
NTSTATUS BlTblSetEntry(_Inout_ PVOID **Table, _Inout_ PULONG Count, _In_ PVOID Entry, _Out_ PULONG EntryIndex, _In_ PBL_TBL_SET_ROUTINE Callback)
Definition: util.c:321
struct _BL_BLOCK_ENTRY * PBL_BLOCK_ENTRY
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
LIST_ENTRY ListEntry
Definition: blkalloc.c:35
return STATUS_SUCCESS
Definition: btrfs.c:2725
NTSTATUS MmBapFreeBlockAllocatorTableEntry(_In_ PVOID Entry, _In_ ULONG Index)
Definition: blkalloc.c:151
BL_MEMORY_TYPE Type
Definition: blkalloc.c:23
base of all file and directory entries
Definition: entries.h:82