-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.c
269 lines (195 loc) · 6.61 KB
/
main.c
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
#include <kernel.h>
#include <libcdvd.h>
#include <iopheap.h>
#include <iopcontrol.h>
#include <iopcontrol_special.h>
#include <errno.h>
#include <sifrpc.h>
#include <loadfile.h>
#include <libmc.h>
#include <stdio.h>
#include <string.h>
#include <usbhdfsd-common.h>
#include <limits.h>
#include <sys/stat.h>
#include <libgs.h>
#include "sysman/sysinfo.h"
#include "SYSMAN_rpc.h"
#include "main.h"
#include "system.h"
#include "pad.h"
#include "graphics.h"
#include "crc16.h"
#include "libcdvd_add.h"
#include "dvdplayer.h"
#include "OSDInit.h"
#include "ps1.h"
#include "modelname.h"
#include "UI.h"
#include "ident.h"
extern unsigned char SIO2MAN_irx[];
extern unsigned int size_SIO2MAN_irx;
extern unsigned char PADMAN_irx[];
extern unsigned int size_PADMAN_irx;
extern unsigned char MCMAN_irx[];
extern unsigned int size_MCMAN_irx;
extern unsigned char MCSERV_irx[];
extern unsigned int size_MCSERV_irx;
extern unsigned char POWEROFF_irx[];
extern unsigned int size_POWEROFF_irx;
extern unsigned char PS2DEV9_irx[];
extern unsigned int size_PS2DEV9_irx;
extern unsigned char USBD_irx[];
extern unsigned int size_USBD_irx;
extern unsigned char USBHDFSD_irx[];
extern unsigned int size_USBHDFSD_irx;
extern unsigned char USBHDFSDFSV_irx[];
extern unsigned int size_USBHDFSDFSV_irx;
extern unsigned char SYSMAN_irx[];
extern unsigned int size_SYSMAN_irx;
extern unsigned char IOPRP_img[];
extern unsigned int size_IOPRP_img;
extern void *_gp;
static unsigned char ConsoleRegionData[13];
static int LoadEROMDRV(void)
{
char eromdrv[] = "rom1:EROMDRV?";
int result;
//Handle region-specific DVD Player of newer consoles.
if (OSDGetDVDPlayerRegion(&eromdrv[12]) == 0 || eromdrv[12] != '\0')
{
eromdrv[12] = '\0'; //Replace '?' with a NULL.
}
return SifLoadModuleEncrypted(eromdrv, 0, NULL);
}
#define SYSTEM_INIT_THREAD_STACK_SIZE 0x800
struct SystemInitParams
{
struct SystemInformation *SystemInformation;
int InitCompleteSema;
};
static void SystemInitThread(struct SystemInitParams *SystemInitParams)
{
GetRomName(SystemInitParams->SystemInformation->mainboard.romver);
SifExecModuleBuffer(MCSERV_irx, size_MCSERV_irx, 0, NULL, NULL);
SifExecModuleBuffer(PADMAN_irx, size_PADMAN_irx, 0, NULL, NULL);
SifExecModuleBuffer(POWEROFF_irx, size_POWEROFF_irx, 0, NULL, NULL);
SifExecModuleBuffer(PS2DEV9_irx, size_PS2DEV9_irx, 0, NULL, NULL);
SifLoadModule("rom0:ADDDRV", 0, NULL);
SifLoadModule("rom0:ADDROM2", 0, NULL);
//Initialize PlayStation Driver (PS1DRV)
PS1DRVInit();
//Initialize ROM DVD player.
//It is normal for this to fail on consoles that have no DVD ROM chip (i.e. DEX or the SCPH-10000/SCPH-15000).
DVDPlayerInit();
LoadEROMDRV();
/* Must be loaded last, after all devices have been initialized. */
SifExecModuleBuffer(SYSMAN_irx, size_SYSMAN_irx, 0, NULL, NULL);
SysmanInit();
GetPeripheralInformation(SystemInitParams->SystemInformation);
SignalSema(SystemInitParams->InitCompleteSema);
ExitDeleteThread();
}
int VBlankStartSema;
static int VBlankStartHandler(int cause)
{
ee_sema_t sema;
iReferSemaStatus(VBlankStartSema, &sema);
if (sema.count < sema.max_count)
iSignalSema(VBlankStartSema);
return 0;
}
extern int UsbReadyStatus;
static void usb_callback(void *packet, void *common)
{
UsbReadyStatus = (((SifCmdHeader_t *)packet)->opt == USBMASS_DEV_EV_CONN) ? 1 : 0;
}
int main(int argc, char *argv[])
{
static SifCmdHandlerData_t SifCmdbuffer;
static struct SystemInformation SystemInformation;
void *SysInitThreadStack;
ee_sema_t ThreadSema;
int SystemInitSema;
unsigned int FrameNum;
struct SystemInitParams InitThreadParams;
// chdir("mass:/PS2Ident/");
if (argc < 1 || GetBootDeviceID() == BOOT_DEVICE_UNKNOWN)
{
Exit(-1);
}
SifInitRpc(0);
while (!SifIopRebootBuffer(IOPRP_img, size_IOPRP_img)) {};
memset(&SystemInformation, 0, sizeof(SystemInformation));
/* Go gather some information from the EE's peripherals while the IOP reset. */
GetEEInformation(&SystemInformation);
InitCRC16LookupTable();
ThreadSema.init_count = 0;
ThreadSema.max_count = 1;
ThreadSema.attr = ThreadSema.option = 0;
InitThreadParams.InitCompleteSema = SystemInitSema = CreateSema(&ThreadSema);
InitThreadParams.SystemInformation = &SystemInformation;
SysInitThreadStack = memalign(64, SYSTEM_INIT_THREAD_STACK_SIZE);
ThreadSema.init_count = 0;
ThreadSema.max_count = 1;
ThreadSema.attr = ThreadSema.option = 0;
VBlankStartSema = CreateSema(&ThreadSema);
AddIntcHandler(kINTC_VBLANK_START, &VBlankStartHandler, 0);
EnableIntc(kINTC_VBLANK_START);
while (!SifIopSync()) {};
SifInitRpc(0);
SifInitIopHeap();
SifLoadFileInit();
fioInit();
sbv_patch_enable_lmb();
sbv_patch_fileio();
SifExecModuleBuffer(SIO2MAN_irx, size_SIO2MAN_irx, 0, NULL, NULL);
SifExecModuleBuffer(MCMAN_irx, size_MCMAN_irx, 0, NULL, NULL);
SifSetCmdBuffer(&SifCmdbuffer, 1);
SifAddCmdHandler(0, &usb_callback, NULL);
SifExecModuleBuffer(USBD_irx, size_USBD_irx, 0, NULL, NULL);
SifExecModuleBuffer(USBHDFSD_irx, size_USBHDFSD_irx, 0, NULL, NULL);
SifExecModuleBuffer(USBHDFSDFSV_irx, size_USBHDFSDFSV_irx, 0, NULL, NULL);
sceCdInit(SCECdINoD);
cdInitAdd();
//Initialize system paths.
OSDInitSystemPaths();
//Initialize ROM version (must be done first).
OSDInitROMVER();
if (InitializeUI(0) != 0)
{
SifExitRpc();
Exit(-1);
}
DEBUG_PRINTF("Loading database.\n");
PS2IDBMS_LoadDatabase("PS2Ident.db");
DEBUG_PRINTF("Initializing hardware...");
SysCreateThread(&SystemInitThread, SysInitThreadStack, SYSTEM_INIT_THREAD_STACK_SIZE, &InitThreadParams, 0x2);
FrameNum = 0;
while (PollSema(SystemInitSema) != SystemInitSema)
{
RedrawLoadingScreen(FrameNum);
FrameNum++;
}
DeleteSema(SystemInitSema);
free(SysInitThreadStack);
SifLoadFileExit();
SifExitIopHeap();
DEBUG_PRINTF("System init: Initializing RPCs.\n");
PadInitPads();
mcInit(MC_TYPE_XMC);
DEBUG_PRINTF("done!\nEntering main menu.\n");
MainMenu(&SystemInformation);
PadDeinitPads();
DisableIntc(kINTC_VBLANK_START);
RemoveIntcHandler(kINTC_VBLANK_START, 0);
DeleteSema(VBlankStartSema);
SifRemoveCmdHandler(0);
DeinitializeUI();
PS2IDBMS_UnloadDatabase();
sceCdInit(SCECdEXIT);
fioExit();
SysmanDeinit();
SifExitRpc();
return 0;
}