bool loadIOS(int ios, bool MountDevices)
{
	int CurIOS = IOS_GetVersion();
	bool ret = true;

	if(ios != CurIOS && IOS_GetType(ios) != IOS_TYPE_STUB)
	{
		WDVD_Close();
		gprintf("Reloading into IOS %i from %i...\n", ios, CurIOS);
		ShutdownBeforeExit();
		NandHandle.Patch_AHB(); //No AHBPROT for the next IOS
		ret = IOS_ReloadIOS(ios) == 0;
		gprintf("AHBPROT after IOS Reload: %u\n", AHBRPOT_Patched());
		NandHandle.Init_ISFS();
		WDVD_Init();
	}

	IOS_GetCurrentIOSInfo();
	if(CurrentIOS.Type == IOS_TYPE_HERMES)
		load_ehc_module_ex();
	else if(CurrentIOS.Type == IOS_TYPE_WANIN && CurrentIOS.Revision >= 18)
		load_dip_249();
	DeviceHandle.SetModes();
	if(MountDevices && ios != CurIOS)
		DeviceHandle.MountAll();

	return ret;
}
Exemple #2
0
s32 Disc_Init(void)
{
	/* Init DVD subsystem */
	return WDVD_Init();
}
void *DiHandler::ThreadMain( void *arg )
{
	enum State
	{
		St_Init,
		St_Reset,
		St_WaitForDisc,
		St_CheckDiscType,
		St_OpenPartition,
		St_WaitForDiscEject,	// some error happened, dont do anything until the current disc is ejected
		St_Idle
	};

	State state = St_Init;

	u32 coverState = 0;

	while( !threadExit )
	{
		usleep( 1000 );

		if( threadSleep )
			LWP_SuspendThread( thread );

		if( state == St_Init )
		{
			if( WDVD_Init() )
			{
				instance->ErrorHappened( E_Init, true );
				threadExit = true;
			}
			state = St_Reset;
			continue;
		}
		else if( state == St_Reset )
		{
			if( WDVD_Reset() )
			{
				instance->ErrorHappened( E_Init, false );
				continue;
			}
			state = St_WaitForDisc;
		}

		// check for disc
		u32 cover = 0;
		if( WDVD_GetCoverStatus( &cover ) )
		{
			gprintf( "WDVD_GetCoverStatus() failed\n" );
			WDVD_Close();
			state = St_Init;
			continue;
		}

		// check if disc status is changed
		if( cover != coverState )
		{
			//gprintf( "cover status: %08x %08x\n", cover, coverState );
			if( cover & 2 )// disc is present and wasnt before
			{
				//gprintf( "disc inserted\n" );
				instance->StartingToReadDisc();
				WDVD_Reset();
				state = St_CheckDiscType;
			}
			else if( coverState & 2 )// disc was present before isnt is gone now
			{
				instance->DiscEjected();
				//gprintf( "disc ejected\n" );
				state = St_WaitForDisc;
			}
			coverState = cover;
		}
		if( !( cover & 2 ) )// if theres no disc inserted, then loop
		{
			continue;
		}

		if( state == St_WaitForDiscEject )
		{
			continue;
		}
		else if( state == St_CheckDiscType )
		{
			s32 ret = WDVD_ReadDiskId( (void*)0x80000000 );
			if( ret < 0 )
			{
				gprintf("WDVD_ReadDiskId(): %d\n", ret );
				instance->ErrorHappened( E_DVD_ReadError, false );
				//state = St_WaitForDiscEject;
				WDVD_Close();
				state = St_Init;
				//coverState = 0;
				continue;
			}

			// check disc type
			if( *(u32*)( 0x80000018 ) == 0x5d1c9ea3 )
			{
				//gprintf( "disc is wii\n" );
				state = St_OpenPartition;
				//instance->DiscInserted( T_Wii );
			}
			else if( *(u32*)( 0x8000001c ) == 0xc2339f3d )
			{
				//gprintf( "disc is gamecube\n" );
				instance->DiscInserted( T_GC );
				state = St_Idle;
			}
			else
			{
				//gprintf( "disc is unknown\n" );
				instance->DiscInserted( T_Unknown );
				state = St_WaitForDiscEject;
				//hexdump( (void*)0x80000000, 0x20 );
			}
		}
		else if( state == St_OpenPartition )
		{
			if( WDVD_OpenDataPartition() < 0 )
			{
				instance->ErrorHappened( E_OpenPartition, false );
				state = St_WaitForDiscEject;
				continue;
			}
			//gprintf( "partition is open\n" );

			// search for the opening.bnr
			s32 ret;
			FST_INFO fst_info __attribute(( aligned( 32 ) ));

			//find FST inside partition
			ret = WDVD_Read( (u8*)&fst_info, sizeof( FST_INFO ), 0x420LL );
			if( ret < 0 )
			{
				gprintf("WDVD_Read( fst_info ): %d\n", ret );
				instance->ErrorHappened( E_DVD_ReadError, false );
				state = St_WaitForDiscEject;
				WDVD_ClosePartition();
				continue;
			}
			fst_info.fst_offset <<= 2;
			fst_info.fst_size <<= 2;
			//gprintf( "%s %i\n", __FILE__, __LINE__ );

			fst_buffer = (u8*)memalign( 32, RU( fst_info.fst_size, 0x40 ) );
			if( !fst_buffer )
			{
				instance->ErrorHappened( E_NoMem, true );
				threadExit = true;
				WDVD_ClosePartition();
				continue;
			}
			//gprintf( "%s %i\n", __FILE__, __LINE__ );
			//gprintf( " %p  %08x %08x\n", fst_buffer, fst_info.fst_size, fst_info.fst_offset );

			//read fst into memory
			ret = WDVD_Read( fst_buffer, fst_info.fst_size, fst_info.fst_offset );
			if( ret < 0 )
			{
				gprintf("WDVD_Read( fst_buffer ): %d\n", ret );
				instance->ErrorHappened( E_DVD_ReadError, false );
				state = St_WaitForDiscEject;
				WDVD_ClosePartition();
				continue;
			}
			//gprintf( "%s %i\n", __FILE__, __LINE__ );

			//set the pointers
			fst = (FST_ENTRY *)fst_buffer;
			u32 name_table_offset = fst->filelen * 0xC;
			name_table = (char *)( fst_buffer + name_table_offset );

			//gprintf( "%s %i\n", __FILE__, __LINE__ );
			// find the opening.bnr
			int fd = EntryFromPath( "/opening.bnr", 0 );
			if( fd < 2 )
			{
				instance->ErrorHappened( E_NoOpeningBnr, false );
				instance->DiscInserted( T_Wii );
				FREE( fst );
				name_table = NULL;
				state = St_Idle;
				WDVD_ClosePartition();
				continue;
			}
			//gprintf( "%s %i\n", __FILE__, __LINE__ );
			u32 len = fst[ fd ].filelen;
			u8 *buf = (u8*)memalign( 32, RU( len, 0x40 ) );
			if( !buf )
			{
				instance->ErrorHappened( E_NoMem, true );
				threadExit = true;
				FREE( fst );
				name_table = NULL;
				WDVD_ClosePartition();
				continue;
			}
			//gprintf( "%s %i\n", __FILE__, __LINE__ );
			ret = WDVD_Read( buf, len, (u64)( fst[ fd ].fileoffset ) << 2 );
			if( ret < 0 )
			{
				gprintf("WDVD_Read( opening.bnr ): %d\n", ret );
				instance->ErrorHappened( E_DVD_ReadError, false );
				state = St_WaitForDiscEject;
				FREE( fst );
				name_table = NULL;
				free( buf );
				WDVD_ClosePartition();
				continue;
			}

			//gprintf( "%s %i\n", __FILE__, __LINE__ );
			// done with these
			FREE( fst );
			name_table = NULL;
			WDVD_ClosePartition();

			//gprintf( "%s %i\n", __FILE__, __LINE__ );
			bool rec = false;
			// got the opening.bnr.  send it to whoever cares
			instance->OpeningBnrReady( buf, len, rec );
			if( !rec )
			{
				gprintf( "nobody got the banner.  freeing it\n" );
				free( buf );
			}
			instance->DiscInserted( T_Wii );
			//gprintf( "%s %i\n", __FILE__, __LINE__ );
			state = St_Idle;
		}
	}

	return NULL;
}
Exemple #4
0
int main(int argc, char **argv)
{
	MEM_init(); //Inits both mem1lo and mem2
	mainIOS = DOL_MAIN_IOS;
	__exception_setreload(10);
	Gecko_Init(); //USB Gecko and SD/WiFi buffer
	gprintf(" \nWelcome to %s!\nThis is the debug output.\n", VERSION_STRING.c_str());

	m_vid.init(); // Init video
	DeviceHandle.Init();
	NandHandle.Init();

	char *gameid = NULL;
	bool Emulator_boot = false;
	bool iosOK = true;

	for(u8 i = 0; i < argc; i++)
	{
		if(argv[i] != NULL && strcasestr(argv[i], "ios=") != NULL && strlen(argv[i]) > 4)
		{
			while(argv[i][0] && !isdigit(argv[i][0]))
				argv[i]++;
			if (atoi(argv[i]) < 254 && atoi(argv[i]) > 0)
				mainIOS = atoi(argv[i]);
		}
		else if(strlen(argv[i]) == 6)
		{
			gameid = argv[i];
			for(u8 i = 0; i < 5; i++)
			{
				if(!isalnum(gameid[i]))
					gameid = NULL;
			}
		}
		else if(argv[i] != NULL && strcasestr(argv[i], "EMULATOR_MAGIC") != NULL)
			Emulator_boot = true;
	}
	check_neek2o();
	/* Init ISFS */
	if(neek2o() || Sys_DolphinMode())
		NandHandle.Init_ISFS();
	else
		NandHandle.LoadDefaultIOS(); /* safe reload to preferred IOS */
	/* Maybe new IOS and Port settings */
	if(InternalSave.CheckSave())
		InternalSave.LoadSettings();
	/* Handle (c)IOS Loading */
	if(neek2o() || Sys_DolphinMode()) /* wont reload anythin */
		iosOK = loadIOS(IOS_GetVersion(), false);
	else if(useMainIOS && CustomIOS(IOS_GetType(mainIOS))) /* Requested */
		iosOK = loadIOS(mainIOS, false) && CustomIOS(CurrentIOS.Type);
	// Init
	Sys_Init();
	Sys_ExitTo(EXIT_TO_HBC);

	DeviceHandle.MountAll();
	m_vid.waitMessage(0.15f);

	Open_Inputs();
	mainMenu.init();
	if(CurrentIOS.Version != mainIOS && !neek2o() && !Sys_DolphinMode())
	{
		if(useMainIOS || !DeviceHandle.UsablePartitionMounted())
		{
			useMainIOS = false;
			mainMenu.TempLoadIOS();
			iosOK = CustomIOS(CurrentIOS.Type);
		}
	}
	if(CurrentIOS.Version == mainIOS)
		useMainIOS = true; //Needed for later checks
	if(!iosOK)
		mainMenu.terror("errboot1", L"No cIOS found!\ncIOS d2x 249 base 56 and 250 base 57 are enough for all your games.");
	else if(!DeviceHandle.UsablePartitionMounted())
		mainMenu.terror("errboot2", L"Could not find a device to save configuration files on!");
	else if(WDVD_Init() < 0)
		mainMenu.terror("errboot3", L"Could not initialize the DIP module!");
	else 
	{
		writeStub();
		if(Emulator_boot)
			mainMenu.m_Emulator_boot = true;
		if(gameid != NULL && strlen(gameid) == 6)
			mainMenu.directlaunch(gameid);
		else
			mainMenu.main();
	}
	//Exit WiiFlow, no game booted...
	mainMenu.cleanup();
	ShutdownBeforeExit();
	Sys_Exit();
	return 0;
}