@@ -230,9 +230,11 @@ static void mcStoreDir(void* arg)
230230int mcInit (int type )
231231{
232232 int ret = 0 ;
233+ int err = 0 ;
233234 mcRpcStat_t * rpcStat = (mcRpcStat_t * )UNCACHED_SEG (& g_rdata .rpcStat );
234235 static int _rb_count = 0 ;
235236
237+ (void )type ;
236238 if (_rb_count != _iop_reboot_count )
237239 {
238240 _rb_count = _iop_reboot_count ;
@@ -244,9 +246,6 @@ int mcInit(int type)
244246
245247 sceSifInitRpc (0 );
246248
247- // set which modules to use
248- g_mcType = type ;
249-
250249 // bind to mc rpc on iop
251250 do
252251 {
@@ -266,65 +265,98 @@ int mcInit(int type)
266265 // for some reason calling this init sif function with 'mcserv' makes all other
267266 // functions not work properly. although NOT calling it means that detecting
268267 // whether or not cards are formatted doesnt seem to work :P
269- if (g_mcType == MC_TYPE_MC )
270- {
268+
269+ // Start with calling flush with an invalid fd (so it sets the return value to
270+ // sceMcResDeniedPermit, which MC_RPCCMD_INIT will not return)
271+ g_descParam .fd = 0xFFFFFFFF ;
272+ sceSifCallRpc (& g_cdata , mcRpcCmd [MC_TYPE_XMC ][MC_RPCCMD_FLUSH ], 0 , & g_descParam , sizeof (g_descParam ), & g_rdata , 4 , NULL , NULL );
273+ sceSifCallRpc (& g_cdata , mcRpcCmd [MC_TYPE_MC ][MC_RPCCMD_FLUSH ], 0 , & g_descParam , sizeof (g_descParam ), & g_rdata , 4 , NULL , NULL );
271274#ifdef MC_DEBUG
272- printf ("libmc: using MCMAN & MCSERV\n" );
275+ printf ("libmc: using XMCMAN & XMCSERV\n" );
276+ #endif
273277
278+ // Try XMCSERV RPC
279+ g_mcType = MC_TYPE_XMC ;
280+ // call init function
281+ if ((ret = sceSifCallRpc (& g_cdata , mcRpcCmd [g_mcType ][MC_RPCCMD_INIT ], 0 , & g_descParam , sizeof (g_descParam ), & g_rdata , 12 , NULL , NULL )) < 0 )
282+ {
283+ // init error
284+ #ifdef MC_DEBUG
285+ printf ("libmc: initialisation error\n" );
274286#endif
275- g_descParam .offset = -217 ;
287+ err = ret - 100 ;
288+ }
276289
277- // call init function
278- if ((ret = sceSifCallRpc (& g_cdata , mcRpcCmd [g_mcType ][MC_RPCCMD_INIT ], 0 , & g_descParam , sizeof (g_descParam ), & g_rdata , 4 , NULL , NULL ))>=0 )
290+ // If result was sceMcResDeniedPermit, RPC was unhandled
291+ if (!err && rpcStat -> result == sceMcResDeniedPermit )
292+ {
293+ err = -122 ;
294+ }
295+
296+ if (!err )
297+ {
298+ // check if old version of mcserv loaded
299+ if (rpcStat -> mcserv_version < 0x205 )
279300 {
280- ret = g_rdata .result ;
301+ #ifdef MC_DEBUG
302+ printf ("libmc: mcserv is too old (%x)\n" , rpcStat -> mcserv_version );
303+ #endif
304+ err = -120 ;
281305 }
282- else {
283- // init error
306+
307+ // check if old version of mcman loaded
308+ if (rpcStat -> mcman_version < 0x206 )
309+ {
284310#ifdef MC_DEBUG
285- printf ("libmc: initialisation error \n" );
311+ printf ("libmc: mcman is too old (%x) \n" , rpcStat -> mcman_version );
286312#endif
287- g_mclibInited = 0 ;
288- return g_rdata .result - 100 ;
313+ err = -121 ;
289314 }
290315 }
291- else if (g_mcType == MC_TYPE_XMC )
316+
317+ if (!err )
318+ {
319+ ret = rpcStat -> result ;
320+ }
321+
322+ if (err && rpcStat -> result == sceMcResDeniedPermit )
292323 {
324+ err = 0 ;
325+
326+ // Try MCSERV RPC
327+ g_mcType = MC_TYPE_MC ;
293328#ifdef MC_DEBUG
294- printf ("libmc: using XMCMAN & XMCSERV \n" );
329+ printf ("libmc: using MCMAN & MCSERV \n" );
295330#endif
296331
332+ g_descParam .offset = -217 ;
333+
297334 // call init function
298- if ((ret = sceSifCallRpc (& g_cdata , mcRpcCmd [g_mcType ][MC_RPCCMD_INIT ], 0 , & g_descParam , sizeof (g_descParam ), & g_rdata , 12 , NULL , NULL )) < 0 )
335+ if ((ret = sceSifCallRpc (& g_cdata , mcRpcCmd [g_mcType ][MC_RPCCMD_INIT ], 0 , & g_descParam , sizeof (g_descParam ), & g_rdata , 4 , NULL , NULL )) < 0 )
299336 {
300337 // init error
301338#ifdef MC_DEBUG
302339 printf ("libmc: initialisation error\n" );
303340#endif
304- g_mclibInited = 0 ;
305- return ret - 100 ;
341+ err = ret - 100 ;
306342 }
307343
308- // check if old version of mcserv loaded
309- if ( rpcStat -> mcserv_version < 0x205 )
344+ // If result was sceMcResDeniedPermit, RPC was unhandled
345+ if (! err && rpcStat -> result == sceMcResDeniedPermit )
310346 {
311- #ifdef MC_DEBUG
312- printf ("libmc: mcserv is too old (%x)\n" , rpcStat -> mcserv_version );
313- #endif
314- g_mclibInited = 0 ;
315- return -120 ;
347+ err = -122 ;
316348 }
317349
318- // check if old version of mcman loaded
319- if (rpcStat -> mcman_version < 0x206 )
350+ if (!err )
320351 {
321- #ifdef MC_DEBUG
322- printf ("libmc: mcman is too old (%x)\n" , rpcStat -> mcman_version );
323- #endif
324- g_mclibInited = 0 ;
325- return -121 ;
352+ ret = g_rdata .result ;
326353 }
327- ret = rpcStat -> result ;
354+ }
355+
356+ if (err )
357+ {
358+ g_mclibInited = 0 ;
359+ return err ;
328360 }
329361
330362 // successfully inited
0 commit comments