Skip to content

Commit a5eb128

Browse files
Merge pull request #13 from NathanNeurotic/bdminit
Upstream
2 parents 6002f57 + 3e0ab1f commit a5eb128

7 files changed

Lines changed: 317 additions & 16 deletions

File tree

include/bdmsupport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ void bdmInitSemaphore();
5454
void bdmEnumerateDevices();
5555

5656
void bdmResolveLBA_UDMA(bdm_device_data_t *pDeviceData);
57+
int bdmWaitForDevice(int deviceId, u32 timeoutMs);
58+
int bdmHDDIsPresent();
5759
int bdmResolveDeviceRoot(char *target, int targetLength, const char *driverName, int massDeviceIndex, int massSlot);
5860

61+
int bdmFindPartition(char *target, const char *name, int write);
5962
#endif

include/hddsupport.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ typedef struct
4848
vmc_spec_t specs; /* Card specifications */
4949
} hdd_vmc_infos_t;
5050

51+
typedef enum {
52+
HDD_LOADMODULES_STATUS_ERROR = -2,
53+
HDD_LOADMODULES_STATUS_UNK = -1,
54+
HDD_LOADMODULES_STATUS_NOERROR,
55+
HDD_LOADMODULES_STATUS_ALREADYLOADED,
56+
HDD_LOADMODULES_STATUS_BUSYLOADING,
57+
HDD_LOADMODULES_STATUS_COUNT,
58+
} hdd_loadmodules_status;
59+
5160
int hddCheck(void);
5261
u32 hddGetTotalSectors(void);
5362
int hddIs48bit(void);
@@ -61,7 +70,7 @@ int hddDeleteHDLGame(hdl_game_info_t *ginfo);
6170

6271
void hddInit(item_list_t *itemList);
6372
item_list_t *hddGetObject(int initOnly);
64-
void hddLoadModules(void);
73+
int hddLoadModules(void);
6574
void hddLoadSupportModules(void);
6675
void hddLaunchGame(item_list_t *itemList, int id, config_set_t *configSet);
6776

pc/opl2iso/src/opl2iso.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ int exportGame(const char *gameid)
266266
return EXIT_FAILURE;
267267
}
268268
}
269+
fclose(fsrc);
269270
}
270271

271272
fclose(fdest);
272-
fclose(fsrc);
273273

274274
printf("* All parts processed...\n");
275275

src/bdmsupport.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <ps2sdkapi.h>
1919
#define NEWLIB_PORT_AWARE
2020
#include <fileXio_rpc.h> // fileXioIoctl, fileXioDevctl
21+
#include <delaythread.h>
2122

2223
static int iUSBModLoaded = 0;
2324
static int iLinkModLoaded = 0;
@@ -1038,3 +1039,70 @@ int bdmUpdateDeviceData(item_list_t *itemList)
10381039
fileXioDclose(dir);
10391040
return 0;
10401041
}
1042+
1043+
int bdmWaitForDevice(int deviceId, u32 timeoutMs)
1044+
{
1045+
const int RETRY_DELAY = 100;
1046+
char path[16];
1047+
1048+
u32 start = GetTimerSystemTime();
1049+
sprintf(path, "mass%d:/", deviceId);
1050+
1051+
while (1) {
1052+
int dir = fileXioDopen(path);
1053+
1054+
if (dir >= 0) {
1055+
fileXioDclose(dir);
1056+
return 1; // ready
1057+
}
1058+
1059+
u32 now = GetTimerSystemTime();
1060+
u32 elapsed_ms = (now - start) / (kBUSCLK / 1000);
1061+
1062+
if (elapsed_ms > timeoutMs) {
1063+
return 0; // timeout
1064+
}
1065+
1066+
DelayThread(RETRY_DELAY * 1000);
1067+
}
1068+
}
1069+
1070+
int bdmHDDIsPresent()
1071+
{
1072+
// the only thing that currently uses ata_device_identify is ATA_DEVCTL_GET_HIGHEST_UDMA_MODE, so this is the best method to check for presence via xhdd (for now anyways)
1073+
// ideally, we'd only have ata_device_identify
1074+
return fileXioDevctl("xhdd0:", ATA_DEVCTL_GET_HIGHEST_UDMA_MODE, NULL, 0, NULL, 0) >= 0;
1075+
}
1076+
1077+
1078+
int bdmFindPartition(char *target, const char *name, int write)
1079+
{
1080+
int i, fd;
1081+
char path[256];
1082+
1083+
for (i = 0; i < MAX_BDM_DEVICES; i++) {
1084+
if (gBDMPrefix[0] != '\0')
1085+
sprintf(path, "mass0:/", i, gBDMPrefix, name);
1086+
else
1087+
sprintf(path, "mass0:", i, name);
1088+
if (write)
1089+
fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0666);
1090+
else
1091+
fd = open(path, O_RDONLY);
1092+
1093+
if (fd >= 0) {
1094+
if (gBDMPrefix[0] != '\0')
1095+
sprintf(target, "mass0:/", i, gBDMPrefix);
1096+
else
1097+
sprintf(target, "mass0:", i);
1098+
close(fd);
1099+
return 1;
1100+
}
1101+
}
1102+
1103+
if (gBDMPrefix[0] != '\0')
1104+
sprintf(target, "mass0:/", gBDMPrefix);
1105+
else
1106+
sprintf(target, "mass0:");
1107+
return 0;
1108+
}

src/hddsupport.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern u8 IOBuffer[2048];
3030
static unsigned char hddForceUpdate = 0;
3131
static unsigned char hddHDProKitDetected = 0;
3232
static unsigned char hddModulesLoadCount = 0;
33+
static unsigned char hddModulesLoaded = 0;
3334
static unsigned char hddSupportModulesLoaded = 0;
3435

3536
static char *hddPrefix = "pfs0:";
@@ -174,12 +175,16 @@ static int hddCreateOPLPartition(const char *name)
174175
return result;
175176
}
176177

177-
void hddLoadModules(void)
178+
int hddLoadModules(void)
178179
{
179-
int ret;
180+
int retLoadModule;
181+
int retStatus = HDD_LOADMODULES_STATUS_UNK;
180182

181183
LOG("HDDSUPPORT LoadModules %d\n", hddModulesLoadCount);
182184

185+
if (hddModulesLoaded)
186+
retStatus = HDD_LOADMODULES_STATUS_ALREADYLOADED;
187+
183188
if (hddModulesLoadCount == 0) {
184189
// Increment the load count as soon as possible to prevent thread scheduling from allowing another thread to
185190
// call into here and try to double load modules.
@@ -193,27 +198,34 @@ void hddLoadModules(void)
193198
hddHDProKitDetected = hddCheckHDProKit();
194199
if (hddHDProKitDetected) {
195200
LOG("[ATAD_HDPRO]:\n");
196-
ret = sysLoadModuleBuffer(&hdpro_atad_irx, size_hdpro_atad_irx, 0, NULL);
201+
retLoadModule = sysLoadModuleBuffer(&hdpro_atad_irx, size_hdpro_atad_irx, 0, NULL);
197202
LOG("[XHDD]:\n");
198203
sysLoadModuleBuffer(&xhdd_irx, size_xhdd_irx, 6, "-hdpro");
199204
} else {
200205
LOG("[BDM]:\n");
201206
sysLoadModuleBuffer(&bdm_irx, size_bdm_irx, 0, NULL);
202207
LOG("[ATAD]:\n");
203-
ret = sysLoadModuleBuffer(&ps2atad_irx, size_ps2atad_irx, 0, NULL);
208+
retLoadModule = sysLoadModuleBuffer(&ps2atad_irx, size_ps2atad_irx, 0, NULL);
204209
LOG("[XHDD]:\n");
205210
sysLoadModuleBuffer(&xhdd_irx, size_xhdd_irx, 0, NULL);
206211
}
207212

208-
if (ret < 0) {
213+
if (retLoadModule < 0) {
209214
LOG("HDD: No HardDisk Drive detected.\n");
210215
setErrorMessageWithCode(_STR_HDD_NOT_CONNECTED_ERROR, ERROR_HDD_IF_NOT_DETECTED);
211-
return;
216+
retStatus = HDD_LOADMODULES_STATUS_ERROR;
217+
} else {
218+
retStatus = HDD_LOADMODULES_STATUS_NOERROR;
219+
hddModulesLoaded = 1;
212220
}
213-
} else
221+
} else {
214222
hddModulesLoadCount++;
223+
if (!hddModulesLoaded)
224+
retStatus = HDD_LOADMODULES_STATUS_BUSYLOADING;
225+
}
215226

216227
LOG("HDDSUPPORT LoadModules done\n");
228+
return retStatus;
217229
}
218230

219231
// Returns 1 for MBR/GPT, 0 for APA, and -1 if an error occured

0 commit comments

Comments
 (0)