/
bdmsupport.c
505 lines (421 loc) · 15.4 KB
/
bdmsupport.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
#include "include/opl.h"
#include "include/lang.h"
#include "include/gui.h"
#include "include/supportbase.h"
#include "include/bdmsupport.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"
#include <usbhdfsd-common.h>
#define NEWLIB_PORT_AWARE
#include <fileXio_rpc.h> // fileXioIoctl, fileXioDevctl
static char bdmPrefix[40]; // Contains the full path to the folder where all the games are.
static int bdmULSizePrev = -2;
static time_t bdmModifiedCDPrev;
static time_t bdmModifiedDVDPrev;
static int bdmGameCount = 0;
static base_game_info_t *bdmGames;
static char bdmDriver[5];
static int iLinkModLoaded = 0;
static int mx4sioModLoaded = 0;
// forward declaration
static item_list_t bdmGameList;
// Identifies the partition that the specified file is stored on and generates a full path to it.
int bdmFindPartition(char *target, const char *name, int write)
{
int i, fd;
char path[256];
for (i = 0; i < MAX_BDM_DEVICES; i++) {
if (gBDMPrefix[0] != '\0')
sprintf(path, "mass%d:%s/%s", i, gBDMPrefix, name);
else
sprintf(path, "mass%d:%s", i, name);
if (write)
fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0666);
else
fd = open(path, O_RDONLY);
if (fd >= 0) {
if (gBDMPrefix[0] != '\0')
sprintf(target, "mass%d:%s/", i, gBDMPrefix);
else
sprintf(target, "mass%d:", i);
close(fd);
return 1;
}
}
// default to first partition (for themes, ...)
if (gBDMPrefix[0] != '\0')
sprintf(target, "mass0:%s/", gBDMPrefix);
else
sprintf(target, "mass0:");
return 0;
}
static unsigned int BdmGeneration = 0;
static void bdmEventHandler(void *packet, void *opt)
{
BdmGeneration++;
}
static void bdmLoadBlockDeviceModules(void)
{
if (gEnableILK && !iLinkModLoaded) {
// Load iLink Block Device drivers
sysLoadModuleBuffer(&iLinkman_irx, size_iLinkman_irx, 0, NULL);
sysLoadModuleBuffer(&IEEE1394_bd_irx, size_IEEE1394_bd_irx, 0, NULL);
iLinkModLoaded = 1;
}
if (gEnableMX4SIO && !mx4sioModLoaded) {
// Load MX4SIO Block Device drivers
sysLoadModuleBuffer(&mx4sio_bd_irx, size_mx4sio_bd_irx, 0, NULL);
mx4sioModLoaded = 1;
}
}
void bdmLoadModules(void)
{
LOG("BDMSUPPORT LoadModules\n");
// Load Block Device Manager (BDM)
sysLoadModuleBuffer(&bdm_irx, size_bdm_irx, 0, NULL);
// Load FATFS (mass:) driver
sysLoadModuleBuffer(&bdmfs_fatfs_irx, size_bdmfs_fatfs_irx, 0, NULL);
// Load USB Block Device drivers
sysLoadModuleBuffer(&usbd_irx, size_usbd_irx, 0, NULL);
sysLoadModuleBuffer(&usbmass_bd_irx, size_usbmass_bd_irx, 0, NULL);
// Load Optional Block Device drivers
bdmLoadBlockDeviceModules();
sysLoadModuleBuffer(&bdmevent_irx, size_bdmevent_irx, 0, NULL);
SifAddCmdHandler(0, &bdmEventHandler, NULL);
LOG("BDMSUPPORT Modules loaded\n");
}
void bdmInit(void)
{
LOG("BDMSUPPORT Init\n");
bdmULSizePrev = -2;
bdmModifiedCDPrev = 0;
bdmModifiedDVDPrev = 0;
bdmGameCount = 0;
bdmGames = NULL;
configGetInt(configGetByType(CONFIG_OPL), "usb_frames_delay", &bdmGameList.delay);
bdmGameList.enabled = 1;
}
item_list_t *bdmGetObject(int initOnly)
{
if (initOnly && !bdmGameList.enabled)
return NULL;
return &bdmGameList;
}
static int bdmNeedsUpdate(void)
{
char path[256];
static unsigned int OldGeneration = 0;
static unsigned char ThemesLoaded = 0;
static unsigned char LanguagesLoaded = 0;
int result = 0;
struct stat st;
ioPutRequest(IO_CUSTOM_SIMPLEACTION, &bdmLoadBlockDeviceModules);
if (bdmULSizePrev != -2 && OldGeneration == BdmGeneration)
return 0;
OldGeneration = BdmGeneration;
bdmFindPartition(bdmPrefix, "ul.cfg", 0);
DIR *dir = opendir("mass0:/");
if (dir != NULL) {
int *pBDMDriver = (int *)bdmDriver;
*pBDMDriver = fileXioIoctl(dir->dd_fd, USBMASS_IOCTL_GET_DRIVERNAME, "");
closedir(dir);
}
sprintf(path, "%sCD", bdmPrefix);
if (stat(path, &st) != 0)
st.st_mtime = 0;
if (bdmModifiedCDPrev != st.st_mtime) {
bdmModifiedCDPrev = st.st_mtime;
result = 1;
}
sprintf(path, "%sDVD", bdmPrefix);
if (stat(path, &st) != 0)
st.st_mtime = 0;
if (bdmModifiedDVDPrev != st.st_mtime) {
bdmModifiedDVDPrev = st.st_mtime;
result = 1;
}
if (!sbIsSameSize(bdmPrefix, bdmULSizePrev))
result = 1;
// update Themes
if (!ThemesLoaded) {
sprintf(path, "%sTHM", bdmPrefix);
if (thmAddElements(path, "/", 1) > 0)
ThemesLoaded = 1;
}
// update Languages
if (!LanguagesLoaded) {
sprintf(path, "%sLNG", bdmPrefix);
if (lngAddLanguages(path, "/", bdmGameList.mode) > 0)
LanguagesLoaded = 1;
}
sbCreateFolders(bdmPrefix, 1);
return result;
}
static int bdmUpdateGameList(void)
{
sbReadList(&bdmGames, bdmPrefix, &bdmULSizePrev, &bdmGameCount);
return bdmGameCount;
}
static int bdmGetGameCount(void)
{
return bdmGameCount;
}
static void *bdmGetGame(int id)
{
return (void *)&bdmGames[id];
}
static char *bdmGetGameName(int id)
{
return bdmGames[id].name;
}
static int bdmGetGameNameLength(int id)
{
return ((bdmGames[id].format != GAME_FORMAT_USBLD) ? ISO_GAME_NAME_MAX + 1 : UL_GAME_NAME_MAX + 1);
}
static char *bdmGetGameStartup(int id)
{
return bdmGames[id].startup;
}
static void bdmDeleteGame(int id)
{
sbDelete(&bdmGames, bdmPrefix, "/", bdmGameCount, id);
bdmULSizePrev = -2;
}
static void bdmRenameGame(int id, char *newName)
{
sbRename(&bdmGames, bdmPrefix, "/", bdmGameCount, id, newName);
bdmULSizePrev = -2;
}
static void bdmLaunchGame(int id, config_set_t *configSet)
{
int i, fd, index, compatmask = 0;
int EnablePS2Logo = 0;
int result;
unsigned int start;
unsigned int startCluster;
char partname[256], filename[32];
base_game_info_t *game = &bdmGames[id];
struct cdvdman_settings_bdm *settings;
u32 layer1_start, layer1_offset;
unsigned short int layer1_part;
char vmc_name[32], vmc_path[256], have_error = 0;
int vmc_id, size_mcemu_irx = 0;
bdm_vmc_infos_t bdm_vmc_infos;
vmc_superblock_t vmc_superblock;
for (vmc_id = 0; vmc_id < 2; vmc_id++) {
memset(&bdm_vmc_infos, 0, sizeof(bdm_vmc_infos_t));
configGetVMC(configSet, vmc_name, sizeof(vmc_name), vmc_id);
if (vmc_name[0]) {
have_error = 1;
if (sysCheckVMC(bdmPrefix, "/", vmc_name, 0, &vmc_superblock) > 0) {
bdm_vmc_infos.flags = vmc_superblock.mc_flag & 0xFF;
bdm_vmc_infos.flags |= 0x100;
bdm_vmc_infos.specs.page_size = vmc_superblock.page_size;
bdm_vmc_infos.specs.block_size = vmc_superblock.pages_per_block;
bdm_vmc_infos.specs.card_size = vmc_superblock.pages_per_cluster * vmc_superblock.clusters_per_card;
sprintf(vmc_path, "%sVMC/%s.bin", bdmPrefix, vmc_name);
fd = open(vmc_path, O_RDONLY);
if (fd >= 0) {
if ((start = (unsigned int)fileXioIoctl(fd, USBMASS_IOCTL_GET_LBA, vmc_path)) != 0 && (startCluster = (unsigned int)fileXioIoctl(fd, USBMASS_IOCTL_GET_CLUSTER, vmc_path)) != 0) {
// Check VMC cluster chain for fragmentation (write operation can cause damage to the filesystem).
if (fileXioIoctl(fd, USBMASS_IOCTL_CHECK_CHAIN, "") == 1) {
LOG("BDMSUPPORT Cluster Chain OK\n");
have_error = 0;
bdm_vmc_infos.active = 1;
bdm_vmc_infos.start_sector = start;
LOG("BDMSUPPORT VMC slot %d start: 0x%X\n", vmc_id, start);
} else {
LOG("BDMSUPPORT Cluster Chain NG\n");
have_error = 2;
}
}
close(fd);
}
}
}
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 + 1));
else
snprintf(error, sizeof(error), _l(_STR_ERR_VMC_CONTINUE), vmc_name, (vmc_id + 1));
if (!guiMsgBox(error, 1, NULL)) {
return;
}
}
for (i = 0; i < size_bdm_mcemu_irx; i++) {
if (((u32 *)&bdm_mcemu_irx)[i] == (0xC0DEFAC0 + vmc_id)) {
if (bdm_vmc_infos.active)
size_mcemu_irx = size_bdm_mcemu_irx;
memcpy(&((u32 *)&bdm_mcemu_irx)[i], &bdm_vmc_infos, sizeof(bdm_vmc_infos_t));
break;
}
}
}
void **irx = &bdm_cdvdman_irx;
int irx_size = size_bdm_cdvdman_irx;
compatmask = sbPrepare(game, configSet, irx_size, irx, &index);
settings = (struct cdvdman_settings_bdm *)((u8 *)irx + index);
if (settings == NULL)
return;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-overflow"
memset(&settings->frags[0], 0, sizeof(bd_fragment_t) * BDM_MAX_FRAGS);
#pragma GCC diagnostic pop
u8 iTotalFragCount = 0;
//
// Add ISO as fragfile[0] to fragment list
//
struct cdvdman_fragfile *iso_frag = &settings->fragfile[0];
iso_frag->frag_start = 0;
iso_frag->frag_count = 0;
for (i = 0; i < game->parts; i++) {
// Open file
sbCreatePath(game, partname, bdmPrefix, "/", i);
fd = open(partname, O_RDONLY);
if (fd < 0) {
sbUnprepare(&settings->common);
guiMsgBox(_l(_STR_ERR_FILE_INVALID), 0, NULL);
return;
}
// Get driver - we should only need to do this once
int *pBDMDriver = (int *)bdmDriver;
*pBDMDriver = fileXioIoctl(fd, USBMASS_IOCTL_GET_DRIVERNAME, "");
// Get fragment list
int iFragCount = fileXioIoctl2(fd, USBMASS_IOCTL_GET_FRAGLIST, NULL, 0, (void *)&settings->frags[iTotalFragCount], sizeof(bd_fragment_t) * (BDM_MAX_FRAGS - iTotalFragCount));
if (iFragCount > BDM_MAX_FRAGS) {
// Too many fragments
close(fd);
sbUnprepare(&settings->common);
guiMsgBox(_l(_STR_ERR_FRAGMENTED), 0, NULL);
return;
}
iso_frag->frag_count += iFragCount;
iTotalFragCount += iFragCount;
if ((gPS2Logo) && (i == 0))
EnablePS2Logo = CheckPS2Logo(fd, 0);
close(fd);
}
// Initialize layer 1 information.
sbCreatePath(game, partname, bdmPrefix, "/", 0);
layer1_start = sbGetISO9660MaxLBA(partname);
switch (game->format) {
case GAME_FORMAT_USBLD:
layer1_part = layer1_start / 0x80000;
layer1_offset = layer1_start % 0x80000;
sbCreatePath(game, partname, bdmPrefix, "/", layer1_part);
break;
default: // Raw ISO9660 disc image; one part.
layer1_part = 0;
layer1_offset = layer1_start;
}
if (sbProbeISO9660(partname, game, layer1_offset) != 0) {
layer1_start = 0;
LOG("DVD detected.\n");
} else {
layer1_start -= 16;
LOG("DVD-DL layer 1 @ part %u sector 0x%lx.\n", layer1_part, layer1_offset);
}
settings->common.layer1_start = layer1_start;
// adjust ZSO cache
settings->common.zso_cache = bdmCacheSize;
if ((result = sbLoadCheats(bdmPrefix, game->startup)) < 0) {
switch (result) {
case -ENOENT:
guiWarning(_l(_STR_NO_CHEATS_FOUND), 10);
break;
default:
guiWarning(_l(_STR_ERR_CHEATS_LOAD_FAILED), 10);
}
}
if (gRememberLastPlayed) {
configSetStr(configGetByType(CONFIG_LAST), "last_played", game->startup);
saveConfig(CONFIG_LAST, 0);
}
if (configGetStrCopy(configSet, CONFIG_ITEM_ALTSTARTUP, filename, sizeof(filename)) == 0)
strcpy(filename, game->startup);
deinit(NO_EXCEPTION, BDM_MODE); // CAREFUL: deinit will call bdmCleanUp, so bdmGames/game will be freed
if (!strcmp(bdmDriver, "usb")) {
settings->common.fakemodule_flags |= FAKE_MODULE_FLAG_USBD;
sysLaunchLoaderElf(filename, "BDM_USB_MODE", irx_size, irx, size_mcemu_irx, &bdm_mcemu_irx, EnablePS2Logo, compatmask);
} else if (!strcmp(bdmDriver, "sd") && strlen(bdmDriver) == 2) {
settings->common.fakemodule_flags |= 0 /* TODO! fake ilinkman ? */;
sysLaunchLoaderElf(filename, "BDM_ILK_MODE", irx_size, irx, size_mcemu_irx, &bdm_mcemu_irx, EnablePS2Logo, compatmask);
} else if (!strcmp(bdmDriver, "sdc") && strlen(bdmDriver) == 3) {
settings->common.fakemodule_flags |= 0;
sysLaunchLoaderElf(filename, "BDM_M4S_MODE", irx_size, irx, size_mcemu_irx, &bdm_mcemu_irx, EnablePS2Logo, compatmask);
}
}
static config_set_t *bdmGetConfig(int id)
{
return sbPopulateConfig(&bdmGames[id], bdmPrefix, "/");
}
static int bdmGetImage(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", bdmPrefix, folder, value, suffix);
else
snprintf(path, sizeof(path), "%s%s_%s", folder, value, suffix);
return texDiscoverLoad(resultTex, path, -1);
}
static int bdmGetTextId(void)
{
int mode = _STR_BDM_GAMES;
if (!strcmp(bdmDriver, "usb"))
mode = _STR_USB_GAMES;
else if (!strcmp(bdmDriver, "sd") && strlen(bdmDriver) == 2)
mode = _STR_ILINK_GAMES;
else if (!strcmp(bdmDriver, "sdc") && strlen(bdmDriver) == 3)
mode = _STR_MX4SIO_GAMES;
return mode;
}
static int bdmGetIconId(void)
{
int mode = BDM_ICON;
if (!strcmp(bdmDriver, "usb"))
mode = USB_ICON;
else if (!strcmp(bdmDriver, "sd") && strlen(bdmDriver) == 2)
mode = ILINK_ICON;
else if (!strcmp(bdmDriver, "sdc") && strlen(bdmDriver) == 3)
mode = MX4SIO_ICON;
return mode;
}
// This may be called, even if bdmInit() was not.
static void bdmCleanUp(int exception)
{
if (bdmGameList.enabled) {
LOG("BDMSUPPORT CleanUp\n");
free(bdmGames);
// if ((exception & UNMOUNT_EXCEPTION) == 0)
// ...
}
}
// This may be called, even if bdmInit() was not.
static void bdmShutdown(void)
{
if (bdmGameList.enabled) {
LOG("BDMSUPPORT Shutdown\n");
free(bdmGames);
}
// As required by some (typically 2.5") HDDs, issue the SCSI STOP UNIT command to avoid causing an emergency park.
fileXioDevctl("mass:", USBMASS_DEVCTL_STOP_ALL, NULL, 0, NULL, 0);
}
static int bdmCheckVMC(char *name, int createSize)
{
return sysCheckVMC(bdmPrefix, "/", name, createSize, NULL);
}
static char *bdmGetPrefix(void)
{
return bdmPrefix;
}
static item_list_t bdmGameList = {
BDM_MODE, 2, 0, 0, MENU_MIN_INACTIVE_FRAMES, BDM_MODE_UPDATE_DELAY, &bdmGetTextId, &bdmGetPrefix, &bdmInit, &bdmNeedsUpdate,
&bdmUpdateGameList, &bdmGetGameCount, &bdmGetGame, &bdmGetGameName, &bdmGetGameNameLength, &bdmGetGameStartup, &bdmDeleteGame, &bdmRenameGame,
&bdmLaunchGame, &bdmGetConfig, &bdmGetImage, &bdmCleanUp, &bdmShutdown, &bdmCheckVMC, &bdmGetIconId};