Repository navigation
Expand file tree
/
Copy pathkernel_xbox.c
More file actions
331 lines (286 loc) · 11 KB
/
Copy pathkernel_xbox.c
File metadata and controls
331 lines (286 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* kernel_xbox.c - Xbox Identity & Hardware Stubs
*
* Provides Xbox-specific exported data (hardware info, kernel version,
* keys, image filename) and section loading stubs.
*
* Most Xbox-specific hardware data is stubbed with plausible retail
* values. Crypto keys are zeroed since we don't need Xbox Live or
* EEPROM-based encryption on PC.
*/
#if !defined(_WIN32) && !defined(_GNU_SOURCE)
#define _GNU_SOURCE /* localtime_r and timegm */
#endif
#include "kernel.h"
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
#include <time.h>
#endif
/* Keep the virtual console's user video settings independent from the host
* window size. Retail games use these EEPROM flags to choose their own 4:3
* or 16:9 layouts; reporting widescreen unconditionally visibly distorts
* titles that render a native 640x480 presentation. */
static int xbox_video_option_enabled(const char *name)
{
const char *value = getenv(name);
return value != NULL && value[0] != '\0' &&
!(value[0] == '0' && value[1] == '\0');
}
static int xbox_widescreen_override = -1;
void xbox_set_widescreen_enabled(BOOL enabled)
{
xbox_widescreen_override = enabled != FALSE;
}
ULONG xbox_get_user_video_flags(void)
{
ULONG flags = 0;
if (xbox_widescreen_override > 0 ||
(xbox_widescreen_override < 0 &&
xbox_video_option_enabled("XBOXRECOMP_VIDEO_WIDESCREEN")))
flags |= XC_VIDEO_FLAGS_WIDESCREEN;
if (xbox_video_option_enabled("XBOXRECOMP_VIDEO_HDTV"))
flags |= XC_VIDEO_FLAGS_HDTV;
return flags;
}
ULONG xbox_get_av_pack(void)
{
return xbox_video_option_enabled("XBOXRECOMP_VIDEO_HDTV")
? AV_PACK_HDTV : AV_PACK_STANDARD;
}
/* ============================================================================
* Exported Data Objects
*
* These are global variables exported by the Xbox kernel at known ordinals.
* Game code accesses them directly via the thunk table.
* ============================================================================ */
/* Hardware info - report a standard 1.0 retail Xbox */
XBOX_HARDWARE_INFO xbox_HardwareInfo = {
.Flags = 0x00000020, /* Retail Xbox */
.GpuRevision = 0xD2, /* NV2A D2 revision */
.McpRevision = 0xD4, /* MCPX D4 revision */
.Reserved = {0, 0}
};
/* Kernel version - match XDK 5849 (the version Burnout 3 was built against) */
XBOX_KRNL_VERSION xbox_KrnlVersion = {
.Major = 1,
.Minor = 0,
.Build = 5849,
.Qfe = 1
};
/* Crypto keys - zeroed, not needed for PC operation.
* These are unique per-console on real hardware. */
UCHAR xbox_EEPROMKey[16] = {0};
UCHAR xbox_HDKey[16] = {0};
UCHAR xbox_SignatureKey[16] = {0};
UCHAR xbox_LANKey[16] = {0};
UCHAR xbox_AlternateSignatureKeys[16][16] = {{0}};
/* Public key data for Xbox Live signature verification - not needed */
UCHAR xbox_XePublicKeyData[284] = {0};
/* Image filename - the XBE path as seen by the kernel */
static char g_image_filename[] = "\\Device\\CdRom0\\default.xbe";
XBOX_ANSI_STRING xbox_XeImageFileName = {
.Length = sizeof(g_image_filename) - 1,
.MaximumLength = sizeof(g_image_filename),
.Buffer = g_image_filename
};
/* Launch data page - used for title-to-title launches (e.g., Xbox Dashboard → game).
* Allocated dynamically and zeroed for normal game boot. */
static XBOX_LAUNCH_DATA_PAGE g_launch_data_page = {0};
XBOX_LAUNCH_DATA_PAGE* xbox_LaunchDataPage = &g_launch_data_page;
/* ============================================================================
* Section Loading
*
* XeLoadSection/XeUnloadSection manage on-demand loading of XBE sections.
* On Xbox, some sections are demand-paged from disc. In our recompilation,
* the entire executable is loaded into memory, so these are reference-counting
* no-ops.
* ============================================================================ */
NTSTATUS __stdcall xbox_XeLoadSection(PXBE_SECTION_HEADER Section)
{
if (!Section)
return STATUS_INVALID_PARAMETER;
InterlockedIncrement(&Section->SectionReferenceCount);
xbox_log(XBOX_LOG_DEBUG, XBOX_LOG_XBOX,
"XeLoadSection: '%s' at %p (size=%u, refcount=%d)",
Section->SectionName ? Section->SectionName : "<null>",
Section->VirtualAddress, Section->VirtualSize,
Section->SectionReferenceCount);
return STATUS_SUCCESS;
}
NTSTATUS __stdcall xbox_XeUnloadSection(PXBE_SECTION_HEADER Section)
{
if (!Section)
return STATUS_INVALID_PARAMETER;
LONG new_count = InterlockedDecrement(&Section->SectionReferenceCount);
xbox_log(XBOX_LOG_DEBUG, XBOX_LOG_XBOX,
"XeUnloadSection: '%s' (refcount=%d)",
Section->SectionName ? Section->SectionName : "<null>",
new_count);
return STATUS_SUCCESS;
}
/* ============================================================================
* EEPROM / Non-Volatile Settings
*
* ExQueryNonVolatileSetting / ExSaveNonVolatileSetting read and write EEPROM
* settings. On real hardware these are stored in the 256-byte EEPROM on the
* SMBus. For recompilation, we return sensible defaults:
* - Region: North America
* - Video: NTSC, widescreen+HDTV enabled
* - Language: English
* - Audio: Stereo, Dolby Digital
* - DVD Region: Region 1 (North America)
* ============================================================================ */
/* XAPI subtracts this bias from UTC. Use the host's current effective offset
* (including DST), rather than the console's old daylight-saving rules. */
static LONG xbox_current_timezone_bias(void)
{
#ifdef _WIN32
TIME_ZONE_INFORMATION zone;
DWORD state = GetTimeZoneInformation(&zone);
if (state == TIME_ZONE_ID_INVALID)
return 0;
return zone.Bias + (state == TIME_ZONE_ID_DAYLIGHT ? zone.DaylightBias :
state == TIME_ZONE_ID_STANDARD ? zone.StandardBias : 0);
#else
time_t now = time(NULL);
struct tm local;
if (!localtime_r(&now, &local))
return 0;
return (LONG)(difftime(now, timegm(&local)) / 60.0);
#endif
}
/* XAPI's GetLocalTime reads the bulk user section, not XC_TIMEZONE_BIAS.
* Offsets here are relative to the user checksum at EEPROM offset 0x60. */
static void xbox_get_user_settings(ULONG settings[24])
{
memset(settings, 0, 24 * sizeof(*settings));
settings[1] = (ULONG)xbox_current_timezone_bias();
/* Transition dates and additional seasonal biases stay zero: the base
* bias already includes the host's current daylight-saving adjustment. */
settings[12] = 1; /* language: English */
settings[13] = xbox_get_user_video_flags();
settings[14] = 0x00010001; /* audio */
settings[23] = 1; /* DVD region */
}
NTSTATUS __stdcall xbox_ExQueryNonVolatileSetting(
ULONG ValueIndex, PULONG Type, PVOID Value, ULONG ValueLength, PULONG ResultLength)
{
if (!Value)
return STATUS_INVALID_PARAMETER;
xbox_log(XBOX_LOG_DEBUG, XBOX_LOG_XBOX,
"ExQueryNonVolatileSetting: index=0x%02X len=%u", ValueIndex, ValueLength);
switch (ValueIndex) {
case XC_LANGUAGE:
/* English = 1 */
if (ValueLength >= sizeof(ULONG)) {
*(PULONG)Value = 1;
if (Type) *Type = 4; /* REG_DWORD */
if (ResultLength) *ResultLength = sizeof(ULONG);
}
break;
case XC_VIDEO:
/* Retail NTSC 4:3 by default; optional modes model user EEPROM state. */
if (ValueLength >= sizeof(ULONG)) {
*(PULONG)Value = xbox_get_user_video_flags();
if (Type) *Type = 4; /* REG_DWORD */
if (ResultLength) *ResultLength = sizeof(ULONG);
}
break;
case XC_FACTORY_AV_REGION:
/* Retail North American factory video standard. */
if (ValueLength >= sizeof(ULONG)) {
*(PULONG)Value = AV_STANDARD_NTSC_M | AV_FLAGS_60Hz;
if (Type) *Type = 4;
if (ResultLength) *ResultLength = sizeof(ULONG);
}
break;
case XC_AUDIO:
/* Stereo + Dolby Digital enabled (0x00000001 = stereo, 0x00010000 = AC3) */
if (ValueLength >= sizeof(ULONG)) {
*(PULONG)Value = 0x00010001;
if (Type) *Type = 4; /* REG_DWORD */
if (ResultLength) *ResultLength = sizeof(ULONG);
}
break;
case XC_PARENTAL_CONTROL:
/* No parental controls */
if (ValueLength >= sizeof(ULONG)) {
*(PULONG)Value = 0;
if (Type) *Type = 4;
if (ResultLength) *ResultLength = sizeof(ULONG);
}
break;
case XC_DVD_REGION:
/* Region 1 (North America) */
if (ValueLength >= sizeof(ULONG)) {
*(PULONG)Value = 1;
if (Type) *Type = 4;
if (ResultLength) *ResultLength = sizeof(ULONG);
}
break;
case XC_MISC:
/* Misc flags: 0 = no special flags */
if (ValueLength >= sizeof(ULONG)) {
*(PULONG)Value = 0;
if (Type) *Type = 4;
if (ResultLength) *ResultLength = sizeof(ULONG);
}
break;
case XC_MAX_OS: {
ULONG settings[24];
if (Type) *Type = 3; /* REG_BINARY */
if (ResultLength) *ResultLength = sizeof(settings);
if (ValueLength < sizeof(settings))
return (NTSTATUS)0xC0000023L; /* STATUS_BUFFER_TOO_SMALL */
xbox_get_user_settings(settings);
memcpy(Value, settings, sizeof(settings));
break;
}
case XC_TIMEZONE_BIAS:
if (Type) *Type = 4; /* REG_DWORD */
if (ResultLength) *ResultLength = sizeof(LONG);
if (ValueLength < sizeof(LONG))
return (NTSTATUS)0xC0000023L; /* STATUS_BUFFER_TOO_SMALL */
*(PLONG)Value = xbox_current_timezone_bias();
break;
default:
xbox_log(XBOX_LOG_WARN, XBOX_LOG_XBOX,
"ExQueryNonVolatileSetting: unhandled index 0x%02X", ValueIndex);
memset(Value, 0, ValueLength);
if (Type) *Type = 4;
if (ResultLength) *ResultLength = ValueLength;
break;
}
return STATUS_SUCCESS;
}
NTSTATUS __stdcall xbox_ExSaveNonVolatileSetting(
ULONG ValueIndex, ULONG Type, PVOID Value, ULONG ValueLength)
{
(void)Type;
(void)Value;
(void)ValueLength;
xbox_log(XBOX_LOG_INFO, XBOX_LOG_XBOX,
"ExSaveNonVolatileSetting: index=0x%02X len=%u (ignored - read-only on PC)",
ValueIndex, ValueLength);
/* Accept writes silently but don't persist them */
return STATUS_SUCCESS;
}
/* ============================================================================
* Network / PHY
*
* PhyGetLinkState reports Ethernet link status. Xbox had a built-in 100Mbit
* NIC. For PC, we report link-up since we'll handle networking differently.
* ============================================================================ */
ULONG __stdcall xbox_PhyGetLinkState(BOOLEAN Verify)
{
(void)Verify;
/* Return link up, 100 Mbps, full duplex */
return 0x01; /* XNET_ETHERNET_LINK_ACTIVE */
}
NTSTATUS __stdcall xbox_PhyInitialize(BOOLEAN ForceReset, PVOID Param2)
{
(void)ForceReset;
(void)Param2;
return STATUS_SUCCESS;
}