Skip to content

Commit 8161284

Browse files
committed
mcman: Proper setting and checking of mcdi->cardform
Avoids the situation where a crash may occur when accessing an unformatted memory card
1 parent 3db25da commit 8161284

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

iop/memorycard/mcman/src/main.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ int McGetFreeClusters(int port, int slot) // Export #38
229229
DPRINTF("McGetFreeClusters port%d slot%d\n", port, slot);
230230

231231
mcfree = 0;
232-
if (mcdi->cardform) {
232+
if (mcdi->cardform > 0) {
233233
switch (mcdi->cardtype) {
234234
case sceMcTypePS2:
235235
mcfree = mcman_findfree2(port, slot, 0);
@@ -1268,10 +1268,14 @@ int mcman_setdevinfos(int port, int slot)
12681268

12691269
mcman_wmemset((void *)mcdi, sizeof(MCDevInfo), 0);
12701270

1271+
mcdi->cardform = 0;
1272+
12711273
r = mcman_setdevspec(port, slot);
12721274
if (r != sceMcResSucceed)
12731275
return -49;
12741276

1277+
mcdi->cardform = -1;
1278+
12751279
r = McReadPage(port, slot, 0, &mcman_pagebuf);
12761280
if (r == sceMcResNoFormat)
12771281
return sceMcResNoFormat; // should rebuild a valid superblock here
@@ -2454,6 +2458,8 @@ int mcman_setPS1devinfos(int port, int slot)
24542458
return -15;
24552459
#endif
24562460

2461+
mcdi->cardform = -1;
2462+
24572463
if (mcman_PS1PDApagebuf.byte[0] != 0x4d)
24582464
return sceMcResNoFormat;
24592465

iop/memorycard/mcman/src/mcsio2.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -808,14 +808,14 @@ int mcman_probePS2Card2(int port, int slot)
808808

809809
if (p[3] == 0x5a) {
810810
r = McGetFormat(port, slot);
811-
if (r > 0) {
811+
if (r > 0)
812+
{
812813
DPRINTF("mcman_probePS2Card2 succeeded\n");
813814

814815
return sceMcResSucceed;
815816
}
816-
817-
r = McGetFormat(port, slot);
818-
if (r < 0) {
817+
else if (r < 0)
818+
{
819819
DPRINTF("mcman_probePS2Card2 sio2cmd failed (no format)\n");
820820

821821
return sceMcResNoFormat;
@@ -842,7 +842,6 @@ int mcman_probePS2Card2(int port, int slot)
842842
int mcman_probePS2Card(int port, int slot) //2
843843
{
844844
register int r;
845-
register MCDevInfo *mcdi;
846845
#ifndef BUILDING_XFROMMAN
847846
register int retries;
848847
u8 *p = mcman_sio2packet.out_dma.addr;
@@ -853,11 +852,18 @@ int mcman_probePS2Card(int port, int slot) //2
853852
r = mcman_cardchanged(port, slot);
854853
if (r == sceMcResSucceed) {
855854
r = McGetFormat(port, slot);
856-
if (r != 0) {
855+
if (r > 0)
856+
{
857857
DPRINTF("mcman_probePS2Card sio2cmd succeeded\n");
858858

859859
return sceMcResSucceed;
860860
}
861+
else if (r < 0)
862+
{
863+
DPRINTF("mcman_probePS2Card sio2cmd failed (no format)\n");
864+
865+
return sceMcResNoFormat;
866+
}
861867
}
862868

863869
#ifndef BUILDING_XFROMMAN
@@ -926,9 +932,6 @@ int mcman_probePS2Card(int port, int slot) //2
926932
return sceMcResFailDetect2;
927933
}
928934

929-
mcdi = &mcman_devinfos[port][slot];
930-
mcdi->cardform = r;
931-
932935
DPRINTF("mcman_probePS2Card sio2cmd succeeded\n");
933936

934937
return r;
@@ -979,7 +982,7 @@ int mcman_probePS1Card2(int port, int slot)
979982
if (mcman_sio2outbufs_PS1PDA[1] == 0) {
980983
if (mcdi->cardform > 0)
981984
return sceMcResSucceed;
982-
if (mcdi->cardform < 0)
985+
else if (mcdi->cardform < 0)
983986
return sceMcResNoFormat;
984987
}
985988
else if (mcman_sio2outbufs_PS1PDA[1] != 8) {
@@ -1039,6 +1042,8 @@ int mcman_probePS1Card(int port, int slot)
10391042
if (mcman_sio2outbufs_PS1PDA[1] == 0) {
10401043
if (mcdi->cardform != 0)
10411044
return sceMcResSucceed;
1045+
else
1046+
return sceMcResNoFormat;
10421047
}
10431048
else if (mcman_sio2outbufs_PS1PDA[1] != 8) {
10441049
return -12;
@@ -1058,8 +1063,6 @@ int mcman_probePS1Card(int port, int slot)
10581063
if (r == 0)
10591064
return sceMcResChangedCard;
10601065

1061-
mcdi->cardform = r;
1062-
10631066
return r;
10641067
#endif
10651068
#ifdef BUILDING_XFROMMAN

iop/memorycard/mcman/src/ps1mc_fio.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ int mcman_format1(int port, int slot)
2929

3030
mcman_invhandles(port, slot);
3131

32+
mcdi->cardform = -1;
33+
3234
for (i = 0; i < 56; i++) {
3335
r = mcman_PS1pagetest(port, slot, i);
3436
if (r == 0)

iop/memorycard/mcman/src/ps2mc_fio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int mcman_format2(int port, int slot)
4747

4848
DPRINTF("mcman_format2 port%d, slot%d cardform %d\n", port, slot, mcdi->cardform);
4949

50-
if (mcdi->cardform == sceMcResNoFormat) {
50+
if (mcdi->cardform < 0) {
5151
for (i = 0; i < 32; i++)
5252
mcdi->bad_block_list[i] = -1;
5353
mcdi->rootdir_cluster = 0;

0 commit comments

Comments
 (0)