diff --git a/source/fs.c b/source/fs.c index 8745720..3f38509 100644 --- a/source/fs.c +++ b/source/fs.c @@ -28,7 +28,7 @@ bool InitSDCardFS() { return fs_mounted[0]; } -bool InitNandFS() { +bool InitExtFS() { if (!fs_mounted[0]) return false; for (u32 i = 1; i < NORM_FS; i++) { @@ -40,7 +40,7 @@ bool InitNandFS() { return true; } -void DeinitNandFS() { +void DeinitExtFS() { for (u32 i = NORM_FS; i > 0; i--) { if (fs_mounted[i]) { char fsname[8]; @@ -67,6 +67,11 @@ int PathToNumFS(const char* path) { return fsnum; } +bool IsMountedFS(const char* path) { + int fsnum = PathToNumFS(path); + return ((fsnum >= 0) && (fsnum < NORM_FS)) ? fs_mounted[fsnum] : false; +} + bool CheckWritePermissions(const char* path) { int pdrv = PathToNumFS(path); if (pdrv < 0) { @@ -198,7 +203,7 @@ bool PathCopyVirtual(const char* destdir, const char* orig) { if ((dvfile.keyslot == ovfile.keyslot) && (dvfile.offset == ovfile.offset)) // this improves copy times dvfile.keyslot = ovfile.keyslot = 0xFF; - DeinitNandFS(); + DeinitExtFS(); if (!ShowProgress(0, 0, orig)) ret = false; for (size_t pos = 0; (pos < osize) && ret; pos += MAIN_BUFFER_SIZE) { UINT read_bytes = min(MAIN_BUFFER_SIZE, osize - pos); @@ -210,7 +215,7 @@ bool PathCopyVirtual(const char* destdir, const char* orig) { ret = false; } ShowProgress(1, 1, orig); - InitNandFS(); + InitExtFS(); } else if (IsVirtualPath(dest)) { // SD card to virtual (other FAT not allowed!) VirtualFile dvfile; FIL ofile; @@ -250,7 +255,7 @@ bool PathCopyVirtual(const char* destdir, const char* orig) { } } - DeinitNandFS(); + DeinitExtFS(); if (!ShowProgress(0, 0, orig)) ret = false; for (size_t pos = 0; (pos < osize) && ret; pos += MAIN_BUFFER_SIZE) { UINT bytes_read = 0; @@ -263,8 +268,8 @@ bool PathCopyVirtual(const char* destdir, const char* orig) { } ShowProgress(1, 1, orig); f_close(&ofile); - InitNandFS(); - } else if (IsVirtualPath(orig)) { // virtual to SD card (other FAT not allowed) + InitExtFS(); + } else if (IsVirtualPath(orig)) { // virtual to any file system VirtualFile ovfile; FIL dfile; u32 osize; diff --git a/source/fs.h b/source/fs.h index 73771d3..0f470b8 100644 --- a/source/fs.h +++ b/source/fs.h @@ -25,8 +25,8 @@ typedef struct { } DirStruct; bool InitSDCardFS(); -bool InitNandFS(); -void DeinitNandFS(); +bool InitExtFS(); +void DeinitExtFS(); void DeinitSDCardFS(); /** Check if writing to this path is allowed **/ @@ -74,5 +74,8 @@ uint64_t GetPartitionOffsetSector(const char* path); /** Helper function to get drive number from path */ int PathToNumFS(const char* path); +/** Check if drive is mounted */ +bool IsMountedFS(const char* path); + /** Helper function for copying DirEntry structs */ void DirEntryCpy(DirEntry* dest, const DirEntry* orig); diff --git a/source/godmode.c b/source/godmode.c index 38baad9..056f876 100644 --- a/source/godmode.c +++ b/source/godmode.c @@ -150,11 +150,11 @@ u32 GodMode() { } InitEmuNandBase(); InitNandCrypto(); - InitNandFS(); + InitExtFS(); if ((GetUnitPlatform() == PLATFORM_N3DS) && !CheckSlot0x05Crypto()) { if (!ShowPrompt(true, "Warning: slot0x05 crypto fail\nslot0x05keyY.bin is either corrupt\nor does not exist. Continue?")) { - DeinitNandFS(); + DeinitExtFS(); DeinitSDCardFS(); return exit_mode; } @@ -189,15 +189,21 @@ u32 GodMode() { (PathToNumFS(current_dir->entry[cursor].path) == 0)) { // try to mount image u32 file_type = IdentifyImage(current_dir->entry[cursor].path); if (file_type && ShowPrompt(true, "This looks like a %s image\nTry to mount it?", (file_type == IMG_NAND) ? "NAND" : "FAT")) { - if (!MountImage(current_dir->entry[cursor].path)) { + DeinitExtFS(); + u32 mount_state = MountImage(current_dir->entry[cursor].path); + InitExtFS(); + if (!mount_state || !(IsMountedFS("7:")|IsMountedFS("8:")|IsMountedFS("9:"))) { ShowPrompt(false, "Mounting image: failed"); + DeinitExtFS(); + MountImage(NULL); + InitExtFS(); } else { - DeinitNandFS(); - InitNandFS(); *current_path = '\0'; GetDirContents(current_dir, current_path); cursor = 0; } + if (clipboard->n_entries && (strcspn(clipboard->entry[0].path, "789I") == 0)) + clipboard->n_entries = 0; // remove invalid clipboard stuff } } else if ((pad_state & BUTTON_B) && *current_path) { // one level down char old_path[256]; @@ -212,7 +218,7 @@ u32 GodMode() { scroll = 0; } } else if ((pad_state & BUTTON_B) && (pad_state & BUTTON_R1)) { // unmount SD card - DeinitNandFS(); + DeinitExtFS(); DeinitSDCardFS(); clipboard->n_entries = 0; ShowPrompt(false, "SD card unmounted, you can eject now.\nPut it back in before you press ."); @@ -221,7 +227,7 @@ u32 GodMode() { return exit_mode; } InitEmuNandBase(); - InitNandFS(); + InitExtFS(); GetDirContents(current_dir, current_path); if (cursor >= current_dir->n_entries) cursor = 0; } else if ((pad_state & BUTTON_DOWN) && (cursor + 1 < current_dir->n_entries)) { // cursor up @@ -361,7 +367,7 @@ u32 GodMode() { } } - DeinitNandFS(); + DeinitExtFS(); DeinitSDCardFS(); return exit_mode;