From 5b955bbeed4fdbfd4bd15ab6f4fd553fecc53a89 Mon Sep 17 00:00:00 2001 From: "fix94.1" Date: Sun, 2 Sep 2012 11:51:26 +0000 Subject: [PATCH] -fixed possible bug on usb remount if IOS got changed -proper shutdown of devices on wiiflow end (boot game, exit to, etc) --- source/devicemounter/DeviceHandler.cpp | 7 ++- source/devicemounter/DeviceHandler.hpp | 2 +- source/devicemounter/usbstorage.c | 78 ++++++++++++++------------ source/devicemounter/usbstorage.h | 4 +- source/loader/alt_ios.cpp | 3 - source/loader/external_booter.cpp | 19 +++---- source/loader/external_booter.hpp | 1 + source/main.cpp | 6 +- source/menu/menu_game.cpp | 32 ++--------- 9 files changed, 68 insertions(+), 84 deletions(-) diff --git a/source/devicemounter/DeviceHandler.cpp b/source/devicemounter/DeviceHandler.cpp index fd9b21a5..8a7d0007 100644 --- a/source/devicemounter/DeviceHandler.cpp +++ b/source/devicemounter/DeviceHandler.cpp @@ -62,7 +62,7 @@ void DeviceHandler::MountAll() MountAllUSB(); } -void DeviceHandler::UnMountAll() +void DeviceHandler::UnMountAll(bool ShutdownUSB) { for(u32 i = SD; i < MAXDEVICES; i++) UnMount(i); @@ -77,6 +77,11 @@ void DeviceHandler::UnMountAll() sd = NULL; usb0 = NULL; usb1 = NULL; + + USBStorage2_Deinit(); + SDHC_Close(); + if(ShutdownUSB) + USB_Deinitialize(); } bool DeviceHandler::Mount(int dev) diff --git a/source/devicemounter/DeviceHandler.hpp b/source/devicemounter/DeviceHandler.hpp index 618d5b8a..86372682 100644 --- a/source/devicemounter/DeviceHandler.hpp +++ b/source/devicemounter/DeviceHandler.hpp @@ -72,7 +72,7 @@ class DeviceHandler void SetModes(); void MountAll(); - void UnMountAll(); + void UnMountAll(bool ShutdownUSB = false); bool Mount(int dev); bool IsInserted(int dev); void UnMount(int dev); diff --git a/source/devicemounter/usbstorage.c b/source/devicemounter/usbstorage.c index 2116df54..a434bb11 100644 --- a/source/devicemounter/usbstorage.c +++ b/source/devicemounter/usbstorage.c @@ -66,9 +66,9 @@ static char fs[] ATTRIBUTE_ALIGN(32) = "/dev/usb2"; static char fs2[] ATTRIBUTE_ALIGN(32) = "/dev/usb123"; static char fs3[] ATTRIBUTE_ALIGN(32) = "/dev/usb/ehc"; -static u8 * mem2_ptr = NULL; -static s32 hid = -1, fd = -1; -static u32 usb2_port = -1; //current USB port +u8 *mem2_ptr = NULL; +s32 hid = -1, fd = -1; +s8 usb2_port = -1; //current USB port bool hddInUse[2] = { false, false }; u32 hdd_sector_size[2] = { 512, 512 }; bool first = false; @@ -84,35 +84,36 @@ inline s32 __USBStorage_isMEM2Buffer(const void *buffer) s32 USBStorage2_Init(u32 port) { + /* allocate buf2 */ + if(mem2_ptr == NULL) + mem2_ptr = SYS_AllocArena2MemLo(USB_MEM2_SIZE, 32); + + if(usb_libogc_mode) + { + __io_usbstorage_ogc.startup(); + return (USBStorage2_GetCapacity(port, &hdd_sector_size[port]) == 0) ? IPC_ENOENT : 0; + } + if(hddInUse[port]) return 0; - if(usb_libogc_mode) - __io_usbstorage_ogc.startup(); - else + /* Create heap */ + if(hid < 0) { - /* Create heap */ - if(hid < 0) - { - hid = iosCreateHeap(UMS_HEAPSIZE); - if (hid < 0) return IPC_ENOMEM; - } - /* allocate buf2 */ - if(mem2_ptr == NULL) - mem2_ptr = SYS_AllocArena2MemLo(USB_MEM2_SIZE, 32); - - /* Open USB device */ - if (fd < 0) fd = IOS_Open(fs, 0); - if (fd < 0) fd = IOS_Open(fs2, 0); - if (fd < 0) fd = IOS_Open(fs3, 0); - if (fd < 0) return fd; + hid = iosCreateHeap(UMS_HEAPSIZE); + if (hid < 0) return IPC_ENOMEM; } + /* Open USB device */ + if (fd < 0) fd = IOS_Open(fs, 0); + if (fd < 0) fd = IOS_Open(fs2, 0); + if (fd < 0) fd = IOS_Open(fs3, 0); + if (fd < 0) return fd; + USBStorage2_SetPort(port); /* Initialize USB storage */ - if(!usb_libogc_mode) - IOS_IoctlvFormat(hid, fd, USB_IOCTL_UMS_INIT, ":"); + IOS_IoctlvFormat(hid, fd, USB_IOCTL_UMS_INIT, ":"); /* Get device capacity */ if(USBStorage2_GetCapacity(port, &hdd_sector_size[port]) == 0) @@ -125,24 +126,25 @@ s32 USBStorage2_Init(u32 port) void USBStorage2_Deinit() { - if(usb_libogc_mode) - { - __io_usbstorage_ogc.shutdown(); - return; - } - /* Close USB device */ - if (fd >= 0) - { + if(usb_libogc_mode) + __io_usbstorage_ogc.shutdown(); + else if(fd >= 0) IOS_Close(fd); // not sure to close the fd is needed - fd = -1; + + /* Reset Variables */ + if(usb2_port == 0 || usb2_port == 1) + { + hddInUse[usb2_port] = false; + usb2_port = -1; } + fd = -1; } -s32 USBStorage2_SetPort(u32 port) +s32 USBStorage2_SetPort(s8 port) { //! Port = 2 is handle in the loader, no need to handle it in cIOS - if(port > 1) + if(port > 1 || port < 0) return -1; if(port == usb2_port) @@ -160,7 +162,7 @@ s32 USBStorage2_SetPort(u32 port) return ret; } -s32 USBStorage2_GetPort() +s8 USBStorage2_GetPort() { return usb2_port; } @@ -178,7 +180,7 @@ s32 USBStorage2_GetCapacity(u32 port, u32 *_sector_size) else numSectors = IOS_IoctlvFormat(hid, fd, USB_IOCTL_UMS_GET_CAPACITY, ":i", §orSize); - if(first) + if(first && numSectors && sectorSize) { gprintf(" * * * * * * * * * * * *\n"); gprintf(" * HDD Information\n * Sectors: %lu\n", numSectors); @@ -296,7 +298,9 @@ s32 USBStorage2_WriteSectors(u32 port, u32 sector, u32 numSectors, const void *b s32 USBStorage2_GetSectorSize() { - return hdd_sector_size[usb2_port]; + if(usb2_port == 0 || usb2_port == 1) + return hdd_sector_size[usb2_port]; + return 0; } static bool __usbstorage_Startup(void) diff --git a/source/devicemounter/usbstorage.h b/source/devicemounter/usbstorage.h index 5da07356..4ef85c5f 100644 --- a/source/devicemounter/usbstorage.h +++ b/source/devicemounter/usbstorage.h @@ -21,8 +21,8 @@ s32 USBStorage2_ReadSectors(u32 port, u32 sector, u32 numSectors, void *buffer); s32 USBStorage2_WriteSectors(u32 port, u32 sector, u32 numSectors, const void *buffer); s32 USBStorage2_GetSectorSize(); -s32 USBStorage2_SetPort(u32 port); -s32 USBStorage2_GetPort(); +s32 USBStorage2_SetPort(s8 port); +s8 USBStorage2_GetPort(); extern int usb_libogc_mode; diff --git a/source/loader/alt_ios.cpp b/source/loader/alt_ios.cpp index a162723e..d373a8a4 100644 --- a/source/loader/alt_ios.cpp +++ b/source/loader/alt_ios.cpp @@ -85,11 +85,8 @@ bool loadIOS(int ios, bool MountDevices) bool ret = true; m_music.Stop(); DeviceHandler::Instance()->UnMountAll(); - USBStorage2_Deinit(); - SDHC_Close(); #ifndef DOLPHIN - mload_close(); if(ios != IOS_GetVersion()) { WDVD_Close(); diff --git a/source/loader/external_booter.cpp b/source/loader/external_booter.cpp index c3b57f09..4f318f68 100644 --- a/source/loader/external_booter.cpp +++ b/source/loader/external_booter.cpp @@ -21,8 +21,7 @@ #include "fst.h" #include "wdvd.h" #include "channel/nand.hpp" -#include "devicemounter/sdhc.h" -#include "devicemounter/usbstorage.h" +#include "devicemounter/DeviceHandler.hpp" #include "homebrew/homebrew.h" /* External WiiFlow Game Booter */ @@ -93,14 +92,7 @@ void WiiFlow_ExternalBooter(u8 vidMode, bool vipatch, bool countryString, u8 pat memcpy((void *)0x90000000, &normalCFG, sizeof(the_CFG)); DCFlushRange((void *)(0x90000000), sizeof(the_CFG)); -#ifndef DOLPHIN - USBStorage2_Deinit(); - USB_Deinitialize(); - SDHC_Close(); -#endif - Nand::Instance()->DeInit_ISFS(true); //cIOS loves magic :P - WDVD_Close(); //We init that in the booter anyways - + ShutdownBeforeExit(true); memcpy(EXECUTE_ADDR, wii_game_booter_dol, wii_game_booter_dol_size); DCFlushRange(EXECUTE_ADDR, wii_game_booter_dol_size); BootHomebrew(); @@ -116,3 +108,10 @@ void ExternalBooter_ChannelSetup(void *dolchunkoffset[18], u32 dolchunksize[18], normalCFG.dolchunkcount = dolchunkcount; normalCFG.startPoint = StartPoint; } + +void ShutdownBeforeExit(bool KeepPatches) +{ + DeviceHandler::Instance()->UnMountAll(true); //Shutdown USB as well + Nand::Instance()->DeInit_ISFS(KeepPatches); + WDVD_Close(); +} diff --git a/source/loader/external_booter.hpp b/source/loader/external_booter.hpp index f248dac3..ffac96ed 100644 --- a/source/loader/external_booter.hpp +++ b/source/loader/external_booter.hpp @@ -31,5 +31,6 @@ extern u32 hooktype; void WiiFlow_ExternalBooter(u8 vidMode, bool vipatch, bool countryString, u8 patchVidMode, int aspectRatio, u32 returnTo, u8 BootType); void ExternalBooter_ChannelSetup(void *dolchunkoffset[18], u32 dolchunksize[18], u32 dolchunkcount, u32 StartPoint); +void ShutdownBeforeExit(bool KeepPatches = false); #endif diff --git a/source/main.cpp b/source/main.cpp index 246beeae..a5af34ff 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -12,6 +12,7 @@ #include "gui/video.hpp" #include "gui/text.hpp" #include "homebrew/homebrew.h" +#include "loader/external_booter.hpp" #include "loader/wdvd.h" #include "loader/alt_ios.h" #include "loader/sys.h" @@ -134,10 +135,7 @@ int main(int argc, char **argv) } //Exit WiiFlow, no game booted... mainMenu->cleanup(); - DeviceHandler::Instance()->UnMountAll(); - Nand::Instance()->DeInit_ISFS(); - WDVD_Close(); - + ShutdownBeforeExit(); Sys_Exit(); exit(1); return 0; diff --git a/source/menu/menu_game.cpp b/source/menu/menu_game.cpp index 6e0a16b5..c2976dfe 100644 --- a/source/menu/menu_game.cpp +++ b/source/menu/menu_game.cpp @@ -882,7 +882,6 @@ void CMenu::_launchGC(dir_discHdr *hdr, bool disc) m_cfg.save(true); cleanup(); - DeviceHandler::Instance()->UnMountAll(); GC_SetVideoMode(videoMode, videoSetting); GC_SetLanguage(GClanguage); if(loader == 2) @@ -891,20 +890,9 @@ void CMenu::_launchGC(dir_discHdr *hdr, bool disc) loadIOS(58, false); else //use cIOS instead to make sure Devolution works anyways loadIOS(mainIOS, false); - USBStorage2_Deinit(); - USB_Deinitialize(); - SDHC_Close(); DEVO_SetOptions(path.c_str(), DeviceName[currentPartition], id.c_str(), memcard_emu); } - -#ifndef DOLPHIN - USBStorage2_Deinit(); - USB_Deinitialize(); - SDHC_Close(); -#endif - Nand::Instance()->DeInit_ISFS(); - WDVD_Close(); - + ShutdownBeforeExit(); if(loader == 1 || disc) { DML_New_WriteOptions(); @@ -928,21 +916,14 @@ void CMenu::_launchHomebrew(const char *filepath, vector arguments) Playlog_Delete(); cleanup(); // wifi and sd gecko doesnt work anymore after cleanup - LoadHomebrew(filepath); - DeviceHandler::Instance()->UnMountAll(); //homebrew loaded, we can unmount devices now + LoadHomebrew(filepath); AddBootArgument(filepath); for(u32 i = 0; i < arguments.size(); ++i) AddBootArgument(arguments[i].c_str()); - loadIOS(58, false); -#ifndef DOLPHIN - USBStorage2_Deinit(); - USB_Deinitialize(); - SDHC_Close(); -#endif - Nand::Instance()->DeInit_ISFS(); - WDVD_Close(); + loadIOS(58, false); + ShutdownBeforeExit(); writeStub(); BootHomebrew(); } @@ -1100,7 +1081,7 @@ void CMenu::_launchChannel(dir_discHdr *hdr) error(_t("errneek1", L"Cannot launch neek2o. Verify your neek2o setup")); Sys_LoadMenu(); } - DeviceHandler::Instance()->UnMountAll(); + ShutdownBeforeExit(); Launch_nk(gameTitle, emuPath.size() > 1 ? emuPath.c_str() : NULL); } DeviceHandler::Instance()->UnMountAll(); @@ -1152,8 +1133,7 @@ void CMenu::_launchChannel(dir_discHdr *hdr) } if(forwarder) { - Nand::Instance()->DeInit_ISFS(); - WDVD_Close(); + ShutdownBeforeExit(); WII_Initialize(); if(WII_LaunchTitle(gameTitle) < 0) Sys_LoadMenu();