ReactOS  0.4.15-dev-1207-g698a8e6
xboxmem.c
Go to the documentation of this file.
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 #include <debug.h>
24 
25 DBG_DEFAULT_CHANNEL(MEMORY);
26 
30 extern ULONG NvBase;
31 extern PVOID FrameBuffer;
32 extern ULONG FrameBufferSize;
33 
34 #define TEST_SIZE 0x200
35 #define TEST_PATTERN1 0xAA
36 #define TEST_PATTERN2 0x55
37 
38 extern VOID
39 SetMemory(
42  SIZE_T Size,
43  TYPE_OF_MEMORY MemoryType);
44 
45 extern VOID
49  SIZE_T Size,
50  TYPE_OF_MEMORY MemoryType,
51  PCHAR Usage);
52 
53 extern ULONG
56 
57 VOID
59 {
60  UCHAR ControlRegion[TEST_SIZE];
61  PVOID MembaseTop = (PVOID)(64 * 1024 * 1024);
62  PVOID MembaseLow = (PVOID)0;
63 
65  WRITE_REGISTER_ULONG((PULONG)(NvBase + NV2A_FB_CFG0 + 4), 0x11448000);
66 
67  /* Prep hardware for 128 Mb */
68  WRITE_PORT_ULONG((PULONG)0xCF8, CONFIG_CMD(0, 0, 0x84));
69  WRITE_PORT_ULONG((PULONG)0xCFC, 0x7FFFFFF);
70 
71  InstalledMemoryMb = 64;
72  memset(ControlRegion, TEST_PATTERN1, TEST_SIZE);
73  memset(MembaseTop, TEST_PATTERN1, TEST_SIZE);
74  __wbinvd();
75 
76  if (memcmp(MembaseTop, ControlRegion, TEST_SIZE) == 0)
77  {
78  /* Looks like there is memory .. maybe a 128MB box */
79  memset(ControlRegion, TEST_PATTERN2, TEST_SIZE);
80  memset(MembaseTop, TEST_PATTERN2, TEST_SIZE);
81  __wbinvd();
82  if (memcmp(MembaseTop, ControlRegion, TEST_SIZE) == 0)
83  {
84  /* Definitely looks like there is memory */
85  if (memcmp(MembaseLow, ControlRegion, TEST_SIZE) == 0)
86  {
87  /* Hell, we find the Test-string at 0x0 too! */
88  InstalledMemoryMb = 64;
89  }
90  else
91  {
92  InstalledMemoryMb = 128;
93  }
94  }
95  }
96 
97  /* Set hardware for amount of memory detected */
98  WRITE_PORT_ULONG((PULONG)0xCF8, CONFIG_CMD(0, 0, 0x84));
99  WRITE_PORT_ULONG((PULONG)0xCFC, InstalledMemoryMb * 1024 * 1024 - 1);
100 
102 }
103 
104 memory_map_t *
106 {
108 
109  if (!MultibootInfoPtr)
110  {
111  ERR("Multiboot info structure not found!\n");
112  return NULL;
113  }
114 
116  {
117  ERR("Multiboot memory map is not passed!\n");
118  return NULL;
119  }
120 
122 
123  if (!MemoryMap ||
125  MultibootInfoPtr->mmap_length % sizeof(memory_map_t) != 0)
126  {
127  ERR("Multiboot memory map structure is malformed!\n");
128  return NULL;
129  }
130 
132  return MemoryMap;
133 }
134 
137 {
138  switch (Type)
139  {
140  case 0: // Video RAM
142  case 1: // Available RAM
143  return LoaderFree;
144  case 3: // ACPI area
146  case 4: // Hibernation area
147  return LoaderSpecialMemory;
148  case 5: // Reserved or invalid memory
149  return LoaderSpecialMemory;
150  default:
152  }
153 }
154 
156 
158 XboxMemGetMemoryMap(ULONG *MemoryMapSize)
159 {
160  memory_map_t * MbMap;
161  INT Count, i;
162 
163  TRACE("XboxMemGetMemoryMap()\n");
164 
166  if (MbMap)
167  {
168  /* Obtain memory map via multiboot spec */
169 
170  for (i = 0; i < Count; i++, MbMap++)
171  {
172  TRACE("i = %d, base_addr_low = 0x%p, length_low = 0x%p\n", i, MbMap->base_addr_low, MbMap->length_low);
173 
174  if (MbMap->base_addr_high > 0 || MbMap->length_high > 0)
175  {
176  ERR("Memory descriptor base or size is greater than 4 GB, should not happen on Xbox!\n");
177  ASSERT(FALSE);
178  }
179 
181  MbMap->base_addr_low,
182  MbMap->length_low,
183  XboxMultibootMemoryType(MbMap->type));
184  }
185  }
186  else
187  {
188  /* Synthesize memory map */
189 
190  /* Available RAM block */
192  0,
193  AvailableMemoryMb * 1024 * 1024,
194  LoaderFree);
195 
196  if (FrameBufferSize != 0)
197  {
198  /* Video memory */
203  "Video memory");
204  }
205  }
206 
207  *MemoryMapSize = PcMemFinalizeMemoryMap(XboxMemoryMap);
208  return XboxMemoryMap;
209 }
210 
211 /* EOF */
signed char * PCHAR
Definition: retypes.h:7
multiboot_info_t * MultibootInfoPtr
NTKERNELAPI VOID NTAPI WRITE_REGISTER_ULONG(IN PULONG Register, IN ULONG Value)
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
#define MAX_BIOS_DESCRIPTORS
Definition: pcbios.h:12
Type
Definition: Type.h:6
unsigned long mmap_addr
Definition: multiboot.h:109
BIOS_MEMORY_MAP MemoryMap[32]
Definition: loader.c:11
_Must_inspect_result_ _In_ USAGE _In_ USHORT _In_ USAGE Usage
Definition: hidpi.h:382
ULONG FrameBufferSize
Definition: xboxvideo.c:29
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1223
#define TEST_SIZE
Definition: xboxmem.c:34
VOID ReserveMemory(PFREELDR_MEMORY_DESCRIPTOR MemoryMap, ULONG_PTR BaseAddress, SIZE_T Size, TYPE_OF_MEMORY MemoryType, PCHAR Usage)
Definition: pcmem.c:484
TYPE_OF_MEMORY XboxMultibootMemoryType(ULONG Type)
Definition: xboxmem.c:136
#define NV2A_FB_CFG0
Definition: xgpu.h:19
int32_t INT
Definition: typedefs.h:58
enum _TYPE_OF_MEMORY TYPE_OF_MEMORY
uint32_t ULONG_PTR
Definition: typedefs.h:65
unsigned long flags
Definition: multiboot.h:96
VOID XboxMemInit(VOID)
Definition: xboxmem.c:58
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
static ULONG AvailableMemoryMb
Definition: xboxmem.c:28
#define TEST_PATTERN1
Definition: xboxmem.c:35
VOID NTAPI WRITE_PORT_ULONG(IN PULONG Port, IN ULONG Value)
Definition: portio.c:123
#define FALSE
Definition: types.h:117
smooth NULL
Definition: ftsmooth.c:416
#define MB_INFO_FLAG_MEMORY_MAP
Definition: multiboot.h:50
void * PVOID
Definition: retypes.h:9
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#define TRACE(s)
Definition: solgame.cpp:4
if(!(yy_init))
Definition: macro.lex.yy.c:714
struct memory_map memory_map_t
PFREELDR_MEMORY_DESCRIPTOR XboxMemGetMemoryMap(ULONG *MemoryMapSize)
Definition: xboxmem.c:158
memory_map_t * XboxGetMultibootMemoryMap(INT *Count)
Definition: xboxmem.c:105
DBG_DEFAULT_CHANNEL(MEMORY)
unsigned long length_high
Definition: multiboot.h:122
PPC_QUAL void __wbinvd(void)
Definition: intrin_ppc.h:759
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
unsigned char UCHAR
Definition: xmlstorage.h:181
#define TEST_PATTERN2
Definition: xboxmem.c:36
unsigned long base_addr_low
Definition: multiboot.h:119
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
unsigned long mmap_length
Definition: multiboot.h:108
FREELDR_MEMORY_DESCRIPTOR XboxMemoryMap[MAX_BIOS_DESCRIPTORS+1]
Definition: xboxmem.c:155
#define ERR(fmt,...)
Definition: debug.h:110
ULONG_PTR SIZE_T
Definition: typedefs.h:80
unsigned long length_low
Definition: multiboot.h:121
unsigned long base_addr_high
Definition: multiboot.h:120
unsigned int * PULONG
Definition: retypes.h:1
#define CONFIG_CMD(bus, dev_fn, where)
Definition: hardware.h:23
static ULONG InstalledMemoryMb
Definition: xboxmem.c:27
ULONG NvBase
Definition: xboxvideo.c:27
unsigned long type
Definition: multiboot.h:123
VOID SetMemory(PFREELDR_MEMORY_DESCRIPTOR MemoryMap, ULONG_PTR BaseAddress, SIZE_T Size, TYPE_OF_MEMORY MemoryType)
Definition: pcmem.c:527
unsigned int ULONG
Definition: retypes.h:1
ULONG PcMemFinalizeMemoryMap(PFREELDR_MEMORY_DESCRIPTOR MemoryMap)
Definition: pcmem.c:547
PVOID FrameBuffer
Definition: xboxvideo.c:28
#define memset(x, y, z)
Definition: compat.h:39