Skip to content

Commit

Permalink
Enable handling corrupt SD cards
Browse files Browse the repository at this point in the history
'Corrupt' includes unknown file systems and other strange occurences.
  • Loading branch information
d0k3 committed Jan 15, 2016
1 parent fa630f9 commit 2004cb8
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
3 changes: 3 additions & 0 deletions source/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
22 changes: 20 additions & 2 deletions source/emunand9.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions source/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ void DeinitFS()
f_mount(NULL, "0:", 1);
}

bool CheckFS()
{
return fs_ok;
}

bool FileOpen(const char* path)
{
if (!fs_ok)
Expand Down
1 change: 1 addition & 0 deletions source/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

bool InitFS();
void DeinitFS();
bool CheckFS();

/** Opens existing files */
bool FileOpen(const char* path);
Expand Down
9 changes: 7 additions & 2 deletions source/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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" :
Expand Down
8 changes: 6 additions & 2 deletions source/theme.c
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
Expand Down

0 comments on commit 2004cb8

Please sign in to comment.