From a594435e145dce0b7c69a299da4d7f9873b58b66 Mon Sep 17 00:00:00 2001 From: dborth Date: Tue, 28 Oct 2008 06:52:38 +0000 Subject: [PATCH] add USB/SD hotswap! --- source/ngc/fileop.cpp | 107 +++++++++++++++++++++------------------- source/ngc/filesel.cpp | 9 +++- source/ngc/snes9xGX.cpp | 2 - 3 files changed, 64 insertions(+), 54 deletions(-) diff --git a/source/ngc/fileop.cpp b/source/ngc/fileop.cpp index 0d4e96c..312f29f 100644 --- a/source/ngc/fileop.cpp +++ b/source/ngc/fileop.cpp @@ -31,77 +31,84 @@ FILE * fatfile; /**************************************************************************** - * fat_is_mounted - * to check whether FAT media are detected. + * MountFAT + * Attempts to mount the FAT device specified + * Sets libfat to use the device by default + * Enables read-ahead cache for SD/USB ***************************************************************************/ +bool MountFAT(PARTITION_INTERFACE part) +{ + bool mounted = fatMountNormalInterface(part, 8); -bool FatIsMounted(PARTITION_INTERFACE partition) { - char prefix[] = "fatX:/"; - prefix[3] = partition + '0'; - DIR_ITER *dir = diropen(prefix); - if (dir) { - dirclose(dir); - return true; - } - return false; + if(mounted) + { + fatSetDefaultInterface(part); + #ifdef HW_RVL + if(part == PI_INTERNAL_SD || part == PI_USBSTORAGE) + fatEnableReadAhead (part, 6, 64); + #endif + } + return mounted; } /**************************************************************************** - * changeFATInterface - * Checks if the device (method) specified is available, and - * sets libfat to use the device + * UnmountFAT + * Unmounts the FAT device specified + ***************************************************************************/ +void UnmountFAT(PARTITION_INTERFACE part) +{ + if(!fatUnmount(part)) + fatUnsafeUnmount(part); +} + +/**************************************************************************** + * UnmountAllFAT + * Unmounts all FAT devices + ***************************************************************************/ +void UnmountAllFAT() +{ +#ifdef HW_RVL + UnmountFAT(PI_INTERNAL_SD); + UnmountFAT(PI_USBSTORAGE); +#endif + UnmountFAT(PI_SDGECKO_A); + UnmountFAT(PI_SDGECKO_B); +} + +/**************************************************************************** + * ChangeFATInterface + * Unmounts all devices and attempts to mount/configure the device specified ***************************************************************************/ bool ChangeFATInterface(int method, bool silent) { - bool devFound = false; + bool mounted = false; + + // unmount all FAT devices + UnmountAllFAT(); if(method == METHOD_SD) { - // check which SD device is loaded - #ifdef HW_RVL - if (FatIsMounted(PI_INTERNAL_SD)) - { - devFound = true; - fatSetDefaultInterface(PI_INTERNAL_SD); - fatEnableReadAhead (PI_INTERNAL_SD, 6, 64); - } + mounted = MountFAT(PI_INTERNAL_SD); // try Wii internal SD #endif - if (!devFound && FatIsMounted(PI_SDGECKO_A)) - { - devFound = true; - fatSetDefaultInterface(PI_SDGECKO_A); - } - if(!devFound && FatIsMounted(PI_SDGECKO_B)) - { - devFound = true; - fatSetDefaultInterface(PI_SDGECKO_B); - } - if(!devFound) - { - if(!silent) - WaitPrompt ((char *)"SD card not found!"); - } + if(!mounted) // internal SD not found + mounted = MountFAT(PI_SDGECKO_A); // try SD Gecko on slot A + if(!mounted) // internal SD and SD Gecko (on slot A) not found + mounted = MountFAT(PI_SDGECKO_B); // try SD Gecko on slot B + if(!mounted && !silent) // no SD device found + WaitPrompt ((char *)"SD card not found!"); } else if(method == METHOD_USB) { #ifdef HW_RVL - if(FatIsMounted(PI_USBSTORAGE)) - { - devFound = true; - fatSetDefaultInterface(PI_USBSTORAGE); - fatEnableReadAhead (PI_USBSTORAGE, 6, 64); - } - else - { - if(!silent) - WaitPrompt ((char *)"USB flash drive not found!"); - } + mounted = MountFAT(PI_USBSTORAGE); + if(!mounted && !silent) + WaitPrompt ((char *)"USB drive not found!"); #endif } - return devFound; + return mounted; } /*************************************************************************** diff --git a/source/ngc/filesel.cpp b/source/ngc/filesel.cpp index 937fd03..3b781e5 100644 --- a/source/ngc/filesel.cpp +++ b/source/ngc/filesel.cpp @@ -429,7 +429,7 @@ int FileSelector (int method) if (!maxfiles) { - WaitPrompt ((char*) "Error reading directory !"); + WaitPrompt ((char*) "Error reading directory!"); haverom = 1; // quit menu } } @@ -440,6 +440,11 @@ int FileSelector (int method) } else // this is a file { + // better do another unmount/remount, just in case + if(method == METHOD_SD || method == METHOD_USB) + if(!ChangeFATInterface(method, NOTSILENT)) + return 0; + // 7z file - let's open it up to select a file inside if(IsSz()) { @@ -447,7 +452,7 @@ int FileSelector (int method) if(!MakeROMPath(szpath, method)) { WaitPrompt((char*) "Maximum filepath length reached!"); - return -1; + return 0; } int szfiles = SzParse(szpath, method); if(szfiles) diff --git a/source/ngc/snes9xGX.cpp b/source/ngc/snes9xGX.cpp index 90e389c..22791d9 100644 --- a/source/ngc/snes9xGX.cpp +++ b/source/ngc/snes9xGX.cpp @@ -69,8 +69,6 @@ extern int FrameTimer; extern long long prev; extern unsigned int timediffallowed; -extern void fat_enable_readahead_all(); - /**************************************************************************** * ipl_set_config * lowlevel Qoob Modchip disable