forked from belek666/Open-PS2-Loader
-
Notifications
You must be signed in to change notification settings - Fork 4
/
hddsupport.c
616 lines (514 loc) · 19 KB
/
hddsupport.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#include "sys/fcntl.h"
#include "include/opl.h"
#include "include/lang.h"
#include "include/gui.h"
#include "include/supportbase.h"
#include "include/hddsupport.h"
#include "include/util.h"
#include "include/themes.h"
#include "include/textures.h"
#include "include/ioman.h"
#include "include/system.h"
#include "include/extern_irx.h"
#include "include/cheatman.h"
#include "modules/iopcore/common/cdvd_config.h"
#define NEWLIB_PORT_AWARE
#include <fileXio_rpc.h> // fileXioFormat, fileXioMount, fileXioUmount, fileXioDevctl
#include <io_common.h> // FIO_MT_RDWR
#include <hdd-ioctl.h>
#define OPL_HDD_MODE_PS2LOGO_OFFSET 0x17F8
static unsigned char hddForceUpdate = 0;
static unsigned char hddHDProKitDetected = 0;
static unsigned char hddModulesLoaded = 0;
static char *hddPrefix = "pfs0:";
static hdl_games_list_t hddGames;
const char *oplPart = "hdd0:+OPL";
// forward declaration
static item_list_t hddGameList;
static int hddLoadGameListCache(hdl_games_list_t *cache);
static int hddUpdateGameListCache(hdl_games_list_t *cache, hdl_games_list_t *game_list);
static void hddInitModules(void)
{
hddLoadModules();
// update Themes
char path[256];
sprintf(path, "%sTHM", hddPrefix);
thmAddElements(path, "/", hddGameList.mode);
sprintf(path, "%sLNG", hddPrefix);
lngAddLanguages(path, "/", hddGameList.mode);
sbCreateFolders(hddPrefix, 0);
}
// HD Pro Kit is mapping the 1st word in ROM0 seg as a main ATA controller,
// The pseudo ATA controller registers are accessed (input/ouput) by writing
// an id to the main ATA controller
#define HDPROreg_IO8 (*(volatile unsigned char *)0xBFC00000)
#define CDVDreg_STATUS (*(volatile unsigned char *)0xBF40200A)
static int hddCheckHDProKit(void)
{
int ret = 0;
DIntr();
ee_kmode_enter();
// HD Pro IO start commands sequence
HDPROreg_IO8 = 0x72;
CDVDreg_STATUS = 0;
HDPROreg_IO8 = 0x34;
CDVDreg_STATUS = 0;
HDPROreg_IO8 = 0x61;
CDVDreg_STATUS = 0;
u32 res = HDPROreg_IO8;
CDVDreg_STATUS = 0;
// check result
if ((res & 0xff) == 0xe7) {
// HD Pro IO finish commands sequence
HDPROreg_IO8 = 0xf3;
CDVDreg_STATUS = 0;
ret = 1;
}
ee_kmode_exit();
EIntr();
if (ret)
LOG("HDDSUPPORT HD Pro Kit detected!\n");
return ret;
}
//Taken from libhdd:
#define PFS_ZONE_SIZE 8192
#define PFS_FRAGMENT 0x00000000
static int CreateOPLPartition(const char *oplPart, const char *mountpoint)
{
int formatArg[3] = {PFS_ZONE_SIZE, 0x2d66, PFS_FRAGMENT};
int fd, result;
char cmd[43];
sprintf(cmd, "%s,,,128M,PFS", oplPart);
if ((fd = open(cmd, O_CREAT | O_TRUNC | O_WRONLY)) >= 0) {
close(fd);
result = fileXioFormat(mountpoint, oplPart, (const char *)&formatArg, sizeof(formatArg));
} else {
result = fd;
}
return result;
}
void hddLoadModules(void)
{
int ret;
static char hddarg[] = "-o"
"\0"
"4"
"\0"
"-n"
"\0"
"20";
LOG("HDDSUPPORT LoadModules\n");
if (!hddModulesLoaded) {
hddModulesLoaded = 1;
//DEV9 must be loaded, as HDD.IRX depends on it. Even if not required by the I/F (i.e. HDPro)
sysInitDev9();
// try to detect HD Pro Kit (not the connected HDD),
// if detected it loads the specific ATAD module
hddHDProKitDetected = hddCheckHDProKit();
if (hddHDProKitDetected) {
ret = sysLoadModuleBuffer(&hdpro_atad_irx, size_hdpro_atad_irx, 0, NULL);
sysLoadModuleBuffer(&xhdd_irx, size_xhdd_irx, 6, "-hdpro");
} else {
ret = sysLoadModuleBuffer(&ps2atad_irx, size_ps2atad_irx, 0, NULL);
sysLoadModuleBuffer(&xhdd_irx, size_xhdd_irx, 0, NULL);
}
if (ret < 0) {
LOG("HDD: No HardDisk Drive detected.\n");
setErrorMessageWithCode(_STR_HDD_NOT_CONNECTED_ERROR, ERROR_HDD_IF_NOT_DETECTED);
return;
}
ret = sysLoadModuleBuffer(&ps2hdd_irx, size_ps2hdd_irx, sizeof(hddarg), hddarg);
if (ret < 0) {
LOG("HDD: No HardDisk Drive detected.\n");
setErrorMessageWithCode(_STR_HDD_NOT_CONNECTED_ERROR, ERROR_HDD_MODULE_HDD_FAILURE);
return;
}
//Check if a HDD unit is connected
if (hddCheck() < 0) {
LOG("HDD: No HardDisk Drive detected.\n");
setErrorMessageWithCode(_STR_HDD_NOT_CONNECTED_ERROR, ERROR_HDD_NOT_DETECTED);
return;
}
ret = sysLoadModuleBuffer(&ps2fs_irx, size_ps2fs_irx, 0, NULL);
if (ret < 0) {
LOG("HDD: HardDisk Drive not formatted (PFS).\n");
setErrorMessageWithCode(_STR_HDD_NOT_FORMATTED_ERROR, ERROR_HDD_MODULE_PFS_FAILURE);
return;
}
LOG("HDDSUPPORT modules loaded\n");
ret = fileXioMount(hddPrefix, oplPart, FIO_MT_RDWR);
if (ret == -ENOENT) {
//Attempt to create the partition.
if ((CreateOPLPartition(oplPart, hddPrefix)) >= 0)
fileXioMount(hddPrefix, oplPart, FIO_MT_RDWR);
}
}
}
void hddInit(void)
{
LOG("HDDSUPPORT Init\n");
hddForceUpdate = 0; //Use cache at initial startup.
configGetInt(configGetByType(CONFIG_OPL), "hdd_frames_delay", &hddGameList.delay);
ioPutRequest(IO_CUSTOM_SIMPLEACTION, &hddInitModules);
hddGameList.enabled = 1;
}
item_list_t *hddGetObject(int initOnly)
{
if (initOnly && !hddGameList.enabled)
return NULL;
return &hddGameList;
}
static int hddNeedsUpdate(void)
{ /* Auto refresh is disabled by setting HDD_MODE_UPDATE_DELAY to MENU_UPD_DELAY_NOUPDATE, within hddsupport.h.
Hence any update request would be issued by the user, which should be taken as an explicit request to re-scan the HDD. */
return 1;
}
static int hddUpdateGameList(void)
{
hdl_games_list_t hddGamesNew;
int ret;
if (((ret = hddLoadGameListCache(&hddGames)) != 0) || (hddForceUpdate))
{
hddGamesNew.count = 0;
hddGamesNew.games = NULL;
ret = hddGetHDLGamelist(&hddGamesNew);
if (ret == 0)
{
hddUpdateGameListCache(&hddGames, &hddGamesNew);
hddFreeHDLGamelist(&hddGames);
hddGames = hddGamesNew;
}
}
hddForceUpdate = 1; //Subsequent refresh operations will cause the HDD to be scanned.
return (ret == 0 ? hddGames.count : 0);
}
static int hddGetGameCount(void)
{
return hddGames.count;
}
static void *hddGetGame(int id)
{
return (void *)&hddGames.games[id];
}
static char *hddGetGameName(int id)
{
return hddGames.games[id].name;
}
static int hddGetGameNameLength(int id)
{
return HDL_GAME_NAME_MAX + 1;
}
static char *hddGetGameStartup(int id)
{
return hddGames.games[id].startup;
}
static void hddDeleteGame(int id)
{
hddDeleteHDLGame(&hddGames.games[id]);
hddForceUpdate = 1;
}
static void hddRenameGame(int id, char *newName)
{
hdl_game_info_t *game = &hddGames.games[id];
strcpy(game->name, newName);
hddSetHDLGameInfo(&hddGames.games[id]);
hddForceUpdate = 1;
}
static void hddLaunchGame(int id, config_set_t *configSet)
{
int i, size_irx = 0;
int EnablePS2Logo = 0;
int result;
void **irx = NULL;
char filename[32];
hdl_game_info_t *game = &hddGames.games[id];
struct cdvdman_settings_hdd *settings;
apa_sub_t parts[APA_MAXSUB + 1];
char vmc_name[2][32];
int part_valid = 0, size_mcemu_irx = 0, nparts;
hdd_vmc_infos_t hdd_vmc_infos;
memset(&hdd_vmc_infos, 0, sizeof(hdd_vmc_infos_t));
configGetVMC(configSet, vmc_name[0], sizeof(vmc_name[0]), 0);
configGetVMC(configSet, vmc_name[1], sizeof(vmc_name[1]), 1);
if (vmc_name[0][0] || vmc_name[1][0]) {
nparts = hddGetPartitionInfo(oplPart, parts);
if (nparts > 0 && nparts <= 5) {
for (i = 0; i < nparts; i++) {
hdd_vmc_infos.parts[i].start = parts[i].start;
hdd_vmc_infos.parts[i].length = parts[i].length;
LOG("HDDSUPPORT hdd_vmc_infos.parts[%d].start : 0x%X\n", i, hdd_vmc_infos.parts[i].start);
LOG("HDDSUPPORT hdd_vmc_infos.parts[%d].length : 0x%X\n", i, hdd_vmc_infos.parts[i].length);
}
part_valid = 1;
}
}
if (part_valid) {
char vmc_path[256];
int vmc_id, have_error = 0;
vmc_superblock_t vmc_superblock;
pfs_blockinfo_t blocks[11];
for (vmc_id = 0; vmc_id < 2; vmc_id++) {
if (vmc_name[vmc_id][0]) {
have_error = 1;
hdd_vmc_infos.active = 0;
if (sysCheckVMC(hddPrefix, "/", vmc_name[vmc_id], 0, &vmc_superblock) > 0) {
hdd_vmc_infos.flags = vmc_superblock.mc_flag & 0xFF;
hdd_vmc_infos.flags |= 0x100;
hdd_vmc_infos.specs.page_size = vmc_superblock.page_size;
hdd_vmc_infos.specs.block_size = vmc_superblock.pages_per_block;
hdd_vmc_infos.specs.card_size = vmc_superblock.pages_per_cluster * vmc_superblock.clusters_per_card;
// Check vmc inode block chain (write operation can cause damage)
snprintf(vmc_path, sizeof(vmc_path), "%sVMC/%s.bin", hddPrefix, vmc_name[vmc_id]);
if ((nparts = hddGetFileBlockInfo(vmc_path, parts, blocks, 11)) > 0) {
have_error = 0;
hdd_vmc_infos.active = 1;
for (i = 0; i < nparts - 1; i++) {
hdd_vmc_infos.blocks[i].number = blocks[i + 1].number;
hdd_vmc_infos.blocks[i].subpart = blocks[i + 1].subpart;
hdd_vmc_infos.blocks[i].count = blocks[i + 1].count;
LOG("HDDSUPPORT hdd_vmc_infos.blocks[%d].number : 0x%X\n", i, hdd_vmc_infos.blocks[i].number);
LOG("HDDSUPPORT hdd_vmc_infos.blocks[%d].subpart : 0x%X\n", i, hdd_vmc_infos.blocks[i].subpart);
LOG("HDDSUPPORT hdd_vmc_infos.blocks[%d].count : 0x%X\n", i, hdd_vmc_infos.blocks[i].count);
}
} else { // else VMC file is too fragmented
LOG("HDDSUPPORT Block Chain NG\n");
have_error = 2;
}
}
if (have_error) {
char error[256];
if (have_error == 2) //VMC file is fragmented
snprintf(error, sizeof(error), _l(_STR_ERR_VMC_FRAGMENTED_CONTINUE), vmc_name[vmc_id], (vmc_id + 1));
else
snprintf(error, sizeof(error), _l(_STR_ERR_VMC_CONTINUE), vmc_name[vmc_id], (vmc_id + 1));
if (!guiMsgBox(error, 1, NULL))
return;
}
for (i = 0; i < size_hdd_mcemu_irx; i++) {
if (((u32 *)&hdd_mcemu_irx)[i] == (0xC0DEFAC0 + vmc_id)) {
if (hdd_vmc_infos.active)
size_mcemu_irx = size_hdd_mcemu_irx;
memcpy(&((u32 *)&hdd_mcemu_irx)[i], &hdd_vmc_infos, sizeof(hdd_vmc_infos_t));
break;
}
}
}
}
}
if (gRememberLastPlayed) {
configSetStr(configGetByType(CONFIG_LAST), "last_played", game->startup);
saveConfig(CONFIG_LAST, 0);
}
char gid[5];
configGetDiscIDBinary(configSet, gid);
int dmaType = 0, dmaMode = 7, compatMode = 0;
configGetInt(configSet, CONFIG_ITEM_COMPAT, &compatMode);
configGetInt(configSet, CONFIG_ITEM_DMA, &dmaMode);
if (dmaMode < 3)
dmaType = 0x20;
else {
dmaType = 0x40;
dmaMode -= 3;
}
hddSetTransferMode(dmaType, dmaMode);
// gHDDSpindown [0..20] -> spindown [0..240] -> seconds [0..1200]
hddSetIdleTimeout(gHDDSpindown * 12);
if (hddHDProKitDetected) {
size_irx = size_hdd_hdpro_cdvdman_irx;
irx = &hdd_hdpro_cdvdman_irx;
} else {
size_irx = size_hdd_cdvdman_irx;
irx = &hdd_cdvdman_irx;
}
sbPrepare(NULL, configSet, size_irx, irx, &i);
if ((result = sbLoadCheats(hddPrefix, game->startup)) < 0) {
switch (result) {
case -ENOENT:
guiWarning(_l(_STR_NO_CHEATS_FOUND), 10);
break;
default:
guiWarning(_l(_STR_ERR_CHEATS_LOAD_FAILED), 10);
}
}
settings = (struct cdvdman_settings_hdd *)((u8 *)irx + i);
// patch 48bit flag
settings->common.media = hddIs48bit() & 0xff;
// patch start_sector
settings->lba_start = game->start_sector;
if (configGetStrCopy(configSet, CONFIG_ITEM_ALTSTARTUP, filename, sizeof(filename)) == 0)
strcpy(filename, game->startup);
if (gPS2Logo)
EnablePS2Logo = CheckPS2Logo(0, game->start_sector + OPL_HDD_MODE_PS2LOGO_OFFSET);
deinit(NO_EXCEPTION, HDD_MODE); // CAREFUL: deinit will call hddCleanUp, so hddGames/game will be freed
sysLaunchLoaderElf(filename, "HDD_MODE", size_irx, irx, size_mcemu_irx, &hdd_mcemu_irx, EnablePS2Logo, compatMode);
}
static config_set_t *hddGetConfig(int id)
{
char path[256];
hdl_game_info_t *game = &hddGames.games[id];
snprintf(path, sizeof(path), "%s"OPL_FOLDER"/%s.cfg", hddPrefix, game->startup);
config_set_t *config = configAlloc(0, NULL, path);
configRead(config); //Does not matter if the config file exists or not.
configSetStr(config, CONFIG_ITEM_NAME, game->name);
configSetInt(config, CONFIG_ITEM_SIZE, game->total_size_in_kb >> 10);
configSetStr(config, CONFIG_ITEM_FORMAT, "HDL");
configSetStr(config, CONFIG_ITEM_MEDIA, game->disctype == SCECdPS2CD ? "CD" : "DVD");
configSetStr(config, CONFIG_ITEM_STARTUP, game->startup);
return config;
}
static int hddGetImage(char *folder, int isRelative, char *value, char *suffix, GSTEXTURE *resultTex, short psm)
{
char path[256];
if (isRelative)
snprintf(path, sizeof(path), "%s%s/%s_%s", hddPrefix, folder, value, suffix);
else
snprintf(path, sizeof(path), "%s%s_%s", folder, value, suffix);
return texDiscoverLoad(resultTex, path, -1, psm);
}
//This may be called, even if hddInit() was not.
static void hddCleanUp(int exception)
{
LOG("HDDSUPPORT CleanUp\n");
if (hddGameList.enabled) {
hddFreeHDLGamelist(&hddGames);
if ((exception & UNMOUNT_EXCEPTION) == 0)
fileXioUmount(hddPrefix);
}
//UI may have loaded modules outside of HDD mode, so deinitialize regardless of the enabled status.
if (hddModulesLoaded) {
fileXioDevctl("pfs:", PDIOC_CLOSEALL, NULL, 0, NULL, 0);
hddModulesLoaded = 0;
}
}
static int hddCheckVMC(char *name, int createSize)
{
return sysCheckVMC(hddPrefix, "/", name, createSize, NULL);
}
//This may be called, even if hddInit() was not.
static void hddShutdown(void)
{
LOG("HDDSUPPORT Shutdown\n");
if (hddGameList.enabled) {
hddFreeHDLGamelist(&hddGames);
fileXioUmount(hddPrefix);
}
//UI may have loaded modules outside of HDD mode, so deinitialize regardless of the enabled status.
if (hddModulesLoaded) {
/* Close all files */
fileXioDevctl("pfs:", PDIOC_CLOSEALL, NULL, 0, NULL, 0);
//DEV9 will remain active if ETH is in use, so put the HDD in IDLE state.
//The HDD should still enter standby state after 21 minutes & 15 seconds, as per the ATAD defaults.
hddSetIdleImmediate();
//Only shut down dev9 from here, if it was initialized from here before.
sysShutdownDev9();
hddModulesLoaded = 0;
}
}
static int hddLoadGameListCache(hdl_games_list_t *cache)
{
char filename[256];
FILE *file;
hdl_game_info_t *games;
int result, size, count;
if (!gHDDGameListCache)
return 1;
hddFreeHDLGamelist(cache);
sprintf(filename, "%s/games.bin", hddPrefix);
file = fopen(filename, "rb");
if (file != NULL)
{
fseek(file, 0, SEEK_END);
size = ftell(file);
rewind(file);
count = size / sizeof(hdl_game_info_t);
if (count > 0)
{
games = memalign(64, count * sizeof(hdl_game_info_t));
if (games != NULL)
{
if (fread(games, sizeof(hdl_game_info_t), count, file) == count)
{
cache->count = count;
cache->games = games;
LOG("hddLoadGameListCache: %d games loaded.\n", count);
result = 0;
} else {
LOG("hddLoadGameListCache: I/O error.\n");
free(games);
result = EIO;
}
} else {
LOG("hddLoadGameListCache: failed to allocate memory.\n");
result = ENOMEM;
}
} else {
result = -1; //Empty file
}
fclose(file);
} else {
result = ENOENT;
}
return result;
}
static int hddUpdateGameListCache(hdl_games_list_t *cache, hdl_games_list_t *game_list)
{
char filename[256];
FILE *file;
int result, i, j, modified;
if (!gHDDGameListCache)
return 1;
if (cache->count > 0)
{
modified = 0;
for(i = 0; i < cache->count; i++)
{
for (j = 0; j < game_list->count; j++)
{
if (strncmp(cache->games[i].partition_name, game_list->games[j].partition_name, APA_IDMAX+1) == 0)
break;
}
if (j == game_list->count)
{
LOG("hddUpdateGameListCache: game added.\n");
modified = 1;
break;
}
}
if ((!modified) && (game_list->count != cache->count))
{
LOG("hddUpdateGameListCache: game removed.\n");
modified = 1;
}
} else {
modified = (game_list->count > 0) ? 1 : 0;
}
if (!modified)
return 0;
LOG("hddUpdateGameListCache: caching new game list.\n");
sprintf(filename, "%s/games.bin", hddPrefix);
if (game_list->count > 0)
{
file = fopen(filename, "wb");
if (file != NULL)
{
result = (fwrite(game_list->games, sizeof(hdl_game_info_t), game_list->count, file) == game_list->count) ? 0 : EIO;
fclose(file);
} else {
result = EIO;
}
} else {
//Last game deleted.
remove(filename);
result = 0;
}
return result;
}
static void hddGetAppsPath(char *path, int max)
{
snprintf(path, max, "%s/APPS", hddPrefix);
}
static item_list_t hddGameList = {
HDD_MODE, 0, 0, MODE_FLAG_COMPAT_DMA, MENU_MIN_INACTIVE_FRAMES, HDD_MODE_UPDATE_DELAY, "HDD Games", _STR_HDD_GAMES, &hddGetAppsPath, &hddInit, &hddNeedsUpdate, &hddUpdateGameList,
&hddGetGameCount, &hddGetGame, &hddGetGameName, &hddGetGameNameLength, &hddGetGameStartup, &hddDeleteGame, &hddRenameGame,
&hddLaunchGame, &hddGetConfig, &hddGetImage, &hddCleanUp, &hddShutdown, &hddCheckVMC, HDD_ICON
};