Fixed a bug in the LBA extensions detection code.
[reactos.git] / freeldr / freeldr / mm / init.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2002 Brian Palmer <brianp@sginet.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <freeldr.h>
21 #include <arch.h>
22 #include <mm.h>
23 #include "mem.h"
24 #include <rtl.h>
25 #include <debug.h>
26 #include <ui.h>
27
28
29 ULONG RealFreeLoaderModuleEnd = 0;
30
31 PVOID HeapBaseAddress = NULL;
32 ULONG HeapLengthInBytes = 0;
33 ULONG HeapMemBlockCount = 0;
34 PMEMBLOCK HeapMemBlockArray = NULL;
35 ULONG Stack;
36
37 VOID InitMemoryManager(VOID)
38 {
39 ULONG MemBlocks;
40 ULONG BaseAddress;
41 ULONG Length;
42
43 // Round up to the next page of memory
44 RealFreeLoaderModuleEnd = ROUND_UP(FreeLoaderModuleEnd, 4096);
45 BaseAddress = RealFreeLoaderModuleEnd;
46 Length = (MAXLOWMEMADDR - RealFreeLoaderModuleEnd);
47
48 DbgPrint((DPRINT_MEMORY, "Initializing Memory Manager.\n"));
49 DbgPrint((DPRINT_MEMORY, "Conventional memory size: %d KB\n", GetConventionalMemorySize()));
50 DbgPrint((DPRINT_MEMORY, "Low memory map:\n"));
51 DbgPrint((DPRINT_MEMORY, "00000\t1000\tReserved\n"));
52 DbgPrint((DPRINT_MEMORY, "01000\t6000\t16-bit stack\n"));
53 DbgPrint((DPRINT_MEMORY, "07000\t1000\tUnused\n"));
54 DbgPrint((DPRINT_MEMORY, "08000\t%x\tFreeLoader program code\n", (RealFreeLoaderModuleEnd - 0x8000)));
55 DbgPrint((DPRINT_MEMORY, "%x\t%x\tLow memory heap\n", BaseAddress, Length));
56 DbgPrint((DPRINT_MEMORY, "78000\t8000\t32-bit stack\n"));
57 DbgPrint((DPRINT_MEMORY, "80000\t10000\tFile system read buffer\n"));
58 DbgPrint((DPRINT_MEMORY, "90000\t10000\tDisk read buffer\n"));
59 DbgPrint((DPRINT_MEMORY, "A0000\t60000\tReserved\n"));
60
61 // Calculate how many memory blocks we have
62 MemBlocks = (Length / MEM_BLOCK_SIZE);
63
64 // Adjust the heap length so we can reserve
65 // enough storage space for the MEMBLOCK array
66 Length -= (MemBlocks * sizeof(MEMBLOCK));
67 DbgPrint((DPRINT_MEMORY, "We get here.\n"));
68
69 // Initialize our tracking variables
70 HeapBaseAddress = (PVOID)BaseAddress;
71 HeapLengthInBytes = Length;
72 HeapMemBlockCount = (HeapLengthInBytes / MEM_BLOCK_SIZE);
73 HeapMemBlockArray = (PMEMBLOCK)(HeapBaseAddress + HeapLengthInBytes);
74 DbgPrint((DPRINT_MEMORY, "We get here2.\n"));
75
76 // Clear the memory
77 __asm__("movl %esp,_Stack");
78 DbgPrint((DPRINT_MEMORY, "HeapBaseAddress: 0x%x HeapLengthInBytes: %d Stack: %d.\n", HeapBaseAddress, HeapLengthInBytes, Stack));
79 RtlZeroMemory(HeapBaseAddress, HeapLengthInBytes);
80 DbgPrint((DPRINT_MEMORY, "We get here3.\n"));
81 RtlZeroMemory(HeapMemBlockArray, (HeapMemBlockCount * sizeof(MEMBLOCK)));
82 DbgPrint((DPRINT_MEMORY, "We get here4.\n"));
83
84 #ifdef DEBUG
85 DbgPrint((DPRINT_MEMORY, "Memory Manager initialized. BaseAddress = 0x%x Length = 0x%x. %d blocks in heap.\n", BaseAddress, Length, HeapMemBlockCount));
86 //MemAllocTest();
87 #endif
88 }