From 2004cb8368dfb709bba8ff43433d55c07589c8b9 Mon Sep 17 00:00:00 2001 From: d0k3 Date: Fri, 15 Jan 2016 01:39:07 +0100 Subject: [PATCH] Enable handling corrupt SD cards 'Corrupt' includes unknown file systems and other strange occurences. --- source/draw.c | 3 +++ source/emunand9.c | 22 ++++++++++++++++++++-- source/fs.c | 5 +++++ source/fs.h | 1 + source/menu.c | 9 +++++++-- source/theme.c | 8 ++++++-- 6 files changed, 42 insertions(+), 6 deletions(-) diff --git a/source/draw.c b/source/draw.c index 5278adb..3dba04d 100644 --- a/source/draw.c +++ b/source/draw.c @@ -114,6 +114,9 @@ void Screenshot(const char* path) }; static u32 n = 0; + if (!CheckFS()) // not mounted, nothing to do + return; + if (path == NULL) { for (; n < 1000; n++) { char filename[16]; diff --git a/source/emunand9.c b/source/emunand9.c index fb8b96d..af3b0ce 100644 --- a/source/emunand9.c +++ b/source/emunand9.c @@ -227,7 +227,12 @@ u32 FormatSdCard(u32 param) u32 fat_size_sectors = 0; // copy starter.bin to memory for autosetup - if (starter_size && FileOpen("starter.bin")) { + if (starter_size && !CheckFS()) { + Debug("File system is corrupt or unknown"); + Debug("Continuing without starter.bin..."); + Debug(""); + starter_size = 0; + } else if (starter_size && FileOpen("starter.bin")) { Debug("Copying starter.bin to memory..."); starter_size = FileGetSize(); if (starter_size > ((u32)(FCRAM_END - buffer))) { @@ -237,6 +242,7 @@ u32 FormatSdCard(u32 param) } else if (starter_size > MAX_STARTER_SIZE) { Debug("File is %ikB (recom. max: %ikB)", starter_size / 1024, MAX_STARTER_SIZE / 1024); Debug("This could be too big. Still continue?"); + Debug("(A to continue, B to cancel)"); while (true) { u32 pad_state = InputWait(); if (pad_state & BUTTON_A) break; @@ -291,7 +297,11 @@ u32 FormatSdCard(u32 param) // this is the point of no return Debug("Total SD card size: %lluMB", ((u64) sd_size_sectors * 0x200) / (1024 * 1024)); - Debug("Storage: %lluMB free / %lluMB total", RemainingStorageSpace() / (1024*1024), TotalStorageSpace() / (1024*1024)); + if (CheckFS()) { + Debug("Storage: %lluMB free / %lluMB total", RemainingStorageSpace() / (1024*1024), TotalStorageSpace() / (1024*1024)); + } else { + Debug("Unknown or corrupt file system"); + } switch(CheckEmuNand()) { case RES_EMUNAND_READY: Debug("Already formatted for EmuNAND"); @@ -354,6 +364,14 @@ u32 FormatSdCard(u32 param) DeinitFS(); InitFS(); + // check if it went well + if (!CheckFS()) { + Debug("Unknown error / something went wrong here"); + Debug("Format this SD card from your PC first"); + Debug("Then try this again"); + return 1; + } + // try creating the working directory #ifdef WORK_DIR DirMake(WORK_DIR); diff --git a/source/fs.c b/source/fs.c index 1c736aa..bb98833 100644 --- a/source/fs.c +++ b/source/fs.c @@ -38,6 +38,11 @@ void DeinitFS() f_mount(NULL, "0:", 1); } +bool CheckFS() +{ + return fs_ok; +} + bool FileOpen(const char* path) { if (!fs_ok) diff --git a/source/fs.h b/source/fs.h index 5ea387c..34516d8 100644 --- a/source/fs.h +++ b/source/fs.h @@ -4,6 +4,7 @@ bool InitFS(); void DeinitFS(); +bool CheckFS(); /** Opens existing files */ bool FileOpen(const char* path); diff --git a/source/menu.c b/source/menu.c index 752acac..ca16228 100644 --- a/source/menu.c +++ b/source/menu.c @@ -24,9 +24,10 @@ u32 UnmountSd() Debug("(B to return, START to reboot)"); while (true) { pad_state = InputWait(); - if (((pad_state & BUTTON_B) && InitFS()) || (pad_state & BUTTON_START)) + if (pad_state & (BUTTON_B | BUTTON_START)) break; } + InitFS(); return pad_state; } @@ -48,7 +49,11 @@ void DrawMenu(MenuInfo* currMenu, u32 index, bool fullDraw, bool subMenu) DrawStringF(menublock_x0, menublock_y1 + 10, top_screen, (subMenu) ? "A: Choose B: Return" : "A: Choose"); DrawStringF(menublock_x0, menublock_y1 + 20, top_screen, "SELECT: Unmount SD"); DrawStringF(menublock_x0, menublock_y1 + 30, top_screen, "START: Reboot"); - DrawStringF(menublock_x1, SCREEN_HEIGHT - 20, top_screen, "SD storage: %lluMB / %lluMB", RemainingStorageSpace() / (1024*1024), TotalStorageSpace() / (1024*1024)); + if (CheckFS()) { + DrawStringF(menublock_x1, SCREEN_HEIGHT - 20, top_screen, "SD storage: %lluMB / %lluMB", RemainingStorageSpace() / (1024*1024), TotalStorageSpace() / (1024*1024)); + } else { + DrawStringF(menublock_x1, SCREEN_HEIGHT - 20, top_screen, "SD storage: unknown filesystem"); + } DrawStringF(menublock_x1, SCREEN_HEIGHT - 30, top_screen, "EmuNAND: %s", (emunand_state == RES_EMUNAND_READY) ? "SD is ready" : (emunand_state == RES_EMUNAND_GATEWAY) ? "GW EmuNAND" : diff --git a/source/theme.c b/source/theme.c index e5732d2..03aaab4 100644 --- a/source/theme.c +++ b/source/theme.c @@ -38,8 +38,12 @@ void LoadThemeGfxLogo(void) { u32 emunand_state = CheckEmuNand(); LoadThemeGfx(GFX_LOGO, LOGO_TOP); #if defined LOGO_TEXT_X && defined LOGO_TEXT_Y - DrawStringF(LOGO_TEXT_X, LOGO_TEXT_Y - 0, LOGO_TOP, "SD storage: %lluMB / %lluMB", RemainingStorageSpace() / (1024*1024), TotalStorageSpace() / (1024*1024)); - DrawStringF(LOGO_TEXT_X, LOGO_TEXT_Y - 0, LOGO_TOP, "EmuNAND: %s", + if (CheckFS()) { + DrawStringF(LOGO_TEXT_X, LOGO_TEXT_Y - 0, LOGO_TOP, "SD storage: %lluMB / %lluMB", RemainingStorageSpace() / (1024*1024), TotalStorageSpace() / (1024*1024)); + } else { + DrawStringF(LOGO_TEXT_X, LOGO_TEXT_Y - 0, LOGO_TOP, "SD storage: unknown filesystem"); + } + DrawStringF(LOGO_TEXT_X, LOGO_TEXT_Y - 10, LOGO_TOP, "EmuNAND: %s", (emunand_state == RES_EMUNAND_READY) ? "SD is ready" : (emunand_state == RES_EMUNAND_GATEWAY) ? "GW EmuNAND" : (emunand_state == RES_EMUNAND_GATEWAY) ? "RedNAND" : "SD not ready" );