[FREELDR] Whitespace improvements - no code changes
[reactos.git] / boot / freeldr / freeldr / arch / i386 / xboxmem.c
1 /*
2 * FreeLoader
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 * Note: much of this code was based on knowledge and/or code developed
19 * by the Xbox Linux group: http://www.xbox-linux.org
20 */
21
22 #include <freeldr.h>
23
24 static ULONG InstalledMemoryMb = 0;
25 static ULONG AvailableMemoryMb = 0;
26
27 #define TEST_SIZE 0x200
28 #define TEST_PATTERN1 0xAA
29 #define TEST_PATTERN2 0x55
30
31 VOID
32 XboxMemInit(VOID)
33 {
34 UCHAR ControlRegion[TEST_SIZE];
35 PVOID MembaseTop = (PVOID)(64 * 1024 * 1024);
36 PVOID MembaseLow = (PVOID)0;
37
38 (*(PULONG)(0xfd000000 + 0x100200)) = 0x03070103;
39 (*(PULONG)(0xfd000000 + 0x100204)) = 0x11448000;
40
41 WRITE_PORT_ULONG((ULONG*) 0xcf8, CONFIG_CMD(0, 0, 0x84));
42 WRITE_PORT_ULONG((ULONG*) 0xcfc, 0x7ffffff); /* Prep hardware for 128 Mb */
43
44 InstalledMemoryMb = 64;
45 memset(ControlRegion, TEST_PATTERN1, TEST_SIZE);
46 memset(MembaseTop, TEST_PATTERN1, TEST_SIZE);
47 __wbinvd();
48
49 if (memcmp(MembaseTop, ControlRegion, TEST_SIZE) == 0)
50 {
51 /* Looks like there is memory .. maybe a 128MB box */
52 memset(ControlRegion, TEST_PATTERN2, TEST_SIZE);
53 memset(MembaseTop, TEST_PATTERN2, TEST_SIZE);
54 __wbinvd();
55 if (memcmp(MembaseTop, ControlRegion, TEST_SIZE) == 0)
56 {
57 /* Definitely looks like there is memory */
58 if (memcmp(MembaseLow, ControlRegion, TEST_SIZE) == 0)
59 {
60 /* Hell, we find the Test-string at 0x0 too! */
61 InstalledMemoryMb = 64;
62 }
63 else
64 {
65 InstalledMemoryMb = 128;
66 }
67 }
68 }
69
70 /* Set hardware for amount of memory detected */
71 WRITE_PORT_ULONG((ULONG*) 0xcf8, CONFIG_CMD(0, 0, 0x84));
72 WRITE_PORT_ULONG((ULONG*) 0xcfc, InstalledMemoryMb * 1024 * 1024 - 1);
73
74 AvailableMemoryMb = InstalledMemoryMb;
75 }
76
77 FREELDR_MEMORY_DESCRIPTOR XboxMemoryMap[2];
78
79 PFREELDR_MEMORY_DESCRIPTOR
80 XboxMemGetMemoryMap(ULONG *MemoryMapSize)
81 {
82 /* Synthesize memory map */
83
84 /* Available RAM block */
85 XboxMemoryMap[0].BasePage = 0;
86 XboxMemoryMap[0].PageCount = AvailableMemoryMb * 1024 * 1024 / MM_PAGE_SIZE;
87 XboxMemoryMap[0].MemoryType = LoaderFree;
88
89 /* Video memory */
90 XboxMemoryMap[1].BasePage = AvailableMemoryMb * 1024 * 1024 / MM_PAGE_SIZE;
91 XboxMemoryMap[1].PageCount = (InstalledMemoryMb - AvailableMemoryMb) * 1024 * 1024 / MM_PAGE_SIZE;
92 XboxMemoryMap[1].MemoryType = LoaderFirmwarePermanent;
93
94 *MemoryMapSize = 2;
95 return XboxMemoryMap;
96 }
97
98 PVOID
99 XboxMemReserveMemory(ULONG MbToReserve)
100 {
101 if (InstalledMemoryMb == 0)
102 {
103 /* Hmm, seems we're not initialized yet */
104 XboxMemInit();
105 }
106
107 if (MbToReserve > AvailableMemoryMb)
108 {
109 /* Can't satisfy the request */
110 return NULL;
111 }
112
113 AvailableMemoryMb -= MbToReserve;
114
115 /* Top of available memory points to the space just reserved */
116 return (PVOID)(AvailableMemoryMb * 1024 * 1024);
117 }
118
119 /* EOF */