void MC_FormatMode(s32 slot) { int erase = 1; int err; //0 = B wass pressed -> ask again if (!slot){ erase = WaitPromptChoice("Are you sure you want to format memory card in slot A?", "Format", "Cancel"); }else{ erase = WaitPromptChoice("Are you sure you want to format memory card in slot B?", "Format", "Cancel"); } if (!erase){ if (!slot){ erase = WaitPromptChoiceAZ("All contents of memory card in slot A will be erased!", "Format", "Cancel"); }else{ erase = WaitPromptChoiceAZ("All contents of memory card in slot B will be erased!", "Format", "Cancel"); } if (!erase) { /*** Try to mount the card ***/ err = MountCard(slot); if (err < 0) { WaitCardError("MCFormat Mount", err); return; /*** Unable to mount the card ***/ } ShowAction("Formatting card..."); /*** Format the card ***/ CARD_Format(slot); usleep(1000*1000); /*** Try to mount the card ***/ err = MountCard(slot); if (err < 0) { WaitCardError("MCFormat Mount", err); return; /*** Unable to mount the card ***/ }else { WaitPrompt("Format completed successfully"); } CARD_Unmount(slot); return; } } WaitPrompt("Format operation cancelled"); return; }
/**************************************************************************** * CardWriteFile * * Relies on *GOOD* data being placed in the FileBuffer prior to calling. * See ReadSMBImage ****************************************************************************/ int CardWriteFile (int slot) { char company[4]; char gamecode[6]; char filename[CARD_FILENAMELEN]; int err, ret; u32 SectorSize; int offset; int written; int filelen; char txt[128]; //add null char company[2] = gamecode[4] = 0; memset (SysArea, 0, CARD_WORKAREA); memset(filename, 0, CARD_FILENAMELEN); ExtractGCIHeader(); memcpy(company, &gci.company, 2); memcpy(gamecode, &gci.gamecode, 4); memcpy(filename, &gci.filename, CARD_FILENAMELEN); filelen = gci.filesize8 * 8192; /*** Mount the card ***/ err = MountCard(slot); if (err < 0) { WaitCardError("CardMount", err); return 0; /*** Unable to mount the card ***/ } CARD_GetSectorSize (slot, &SectorSize); /*** Initialise for this company & gamecode ***/ CARD_SetCompany(company); CARD_SetGamecode(gamecode); /*** If this file exists, abort ***/ err = CARD_FindFirst (slot, &CardDir, false); while (err != CARD_ERROR_NOFILE) { if ((memcmp(CardDir.gamecode, &gamecode, 4) == 0) && (memcmp(CardDir.company, &company, 2) == 0) && (strcmp ((char *) CardDir.filename, (char *)filename) == 0)) { /*** Found the file - prompt user ***/ sprintf(txt, "Savegame %s(%s%s) already exists. Overwrite?", (char *)filename, gamecode, company); ret = WaitPromptChoice(txt, "Overwrite", "Cancel"); if (!ret){ sprintf(txt, "Are you -SURE- you want to overwrite %s?", (char *)filename); ret = WaitPromptChoiceAZ(txt, "Overwrite", "Cancel"); if(!ret){ err = CARD_Delete(slot, (char *) &filename); if (err < 0) { WaitCardError("MCDel", err); CARD_Unmount (slot); return 0; } err = CARD_FindFirst (slot, &CardDir, false); continue; } } /*** User canceled - abort ***/ CARD_Unmount (slot); WaitCardError("File already exists", err); return 0; } err = CARD_FindNext (&CardDir); } tryagain: /*** Initialise for this company & gamecode ***/ //Again just in case, as this is very important for propper restoring CARD_SetCompany(company); CARD_SetGamecode(gamecode); /*** Now restore the file from backup ***/ err = CARD_Create (slot, (char *) filename, filelen, &CardFile); if (err < 0) { if (err == CARD_ERROR_EXIST) { /*** Found the file - prompt user ***/ sprintf(txt, "File %s(%s%s) already exists. Overwrite?", (char *) filename, gamecode, company); ret = WaitPromptChoice(txt, "Overwrite", "Cancel"); if (!ret){ sprintf(txt, "Are you -SURE- you want to overwrite %s?", (char *) filename); ret = WaitPromptChoiceAZ(txt, "Overwrite", "Cancel"); if(!ret){ err = CARD_Delete(slot, (char *) &filename); if (err < 0) { WaitCardError("MCDel", err); CARD_Unmount (slot); return 0; } goto tryagain; } } } CARD_Unmount (slot); WaitCardError("CardCreate", err); return 0; } //Thanks to Ralf, validate F-zero and PSO savegames FZEROGX_MakeSaveGameValid(slot); PSO_MakeSaveGameValid(slot); /*** Now write the file data, in sector sized chunks ***/ offset = 0; while (offset < filelen) { if ((offset + SectorSize) <= filelen) { written = CARD_Write (&CardFile, FileBuffer + MCDATAOFFSET + offset + OFFSET, SectorSize, offset); } else { written = CARD_Write (&CardFile, FileBuffer + MCDATAOFFSET + offset + OFFSET, ((offset + SectorSize) - filelen), offset); } offset += SectorSize; } OFFSET = 0; #ifdef STATUSOGC /*** Finally, update the status ***/ CARD_SetStatus (slot, CardFile.filenum, &CardStatus); //For some reason this sets the file to Move->allowed, Copy->not allowed, Public file instead of the actual permission value CARD_SetAttributes(slot, CardFile.filenum, &permission); #else __card_setstatusex(slot, CardFile.filenum, &gci); #endif CARD_Close (&CardFile); CARD_Unmount (slot); return 1; }
/**************************************************************************** * RestoreMode * * Restore a file to Memory Card from SD Card ****************************************************************************/ void SD_RestoreMode () { int files; int selected; char buffer[256], text[64]; clearRightPane(); DrawText(380,130,"R e s t o r e M o d e"); DrawText(380,134,"______________________"); writeStatusBar("Reading files... ", ""); files = SDGetFileList (1); setfontsize (14); writeStatusBar("Pick a file using UP or DOWN", "Press A to restore to Memory Card ") ; #ifdef HW_RVL DrawText(40, 60, "Press R/1 to restore ALL savegames"); #else DrawText(40, 60, "Press R to restore ALL savegames"); #endif if (!files) { WaitPrompt ("No saved games in FAT device to restore !"); } else { selected = ShowSelector (1); if (cancel) { WaitPrompt ("Restore action cancelled !"); return; } else if (doall) { doall = WaitPromptChoice("Are you sure you want to restore -ALL- files?", "Yes", "No"); if (!doall) { //Restore All files for ( selected = 0; selected < files; selected++ ) { /*** Restore files ***/ sprintf(buffer, "[%d/%d] Reading from FAT device", selected+1, files); ShowAction(buffer); if (SDLoadMCImage ((char*)filelist[selected])) { sprintf(buffer, "[%d/%d] Saving to MC slot %s", selected+1, files, (MEM_CARD) ? "B" : "A"); ShowAction(buffer); if (!CardWriteFile (MEM_CARD)) { strncpy(text, (char*)filelist[selected], 32); text[32]='\0'; sprintf(buffer, "Error during restore (%s). Continue?", text); doall = WaitPromptChoice(buffer, "Yes", "No"); if (doall) { WaitPrompt ("Restore action cancelled due to error!"); return; } } } else { WaitPrompt ("Error reading image"); return; } } WaitPrompt("Full card restore done!"); return; } } else { ShowAction ("Reading from FAT device"); if (SDLoadMCImage ((char*)filelist[selected])) { ShowAction ("Updating Memory Card"); if (CardWriteFile (MEM_CARD)) { WaitPrompt ("Restore Complete"); return; } else { WaitPrompt ("Error during restore"); return; } } else { WaitPrompt ("Error reading image"); return; } } } return; }
/**************************************************************************** * Main ****************************************************************************/ int main () { int have_sd = 0; #ifdef HW_DOL int *psoid = (int *) 0x80001800; void (*PSOReload) () = (void (*)()) 0x80001800; #endif Initialise (); /*** Start video ***/ FT_Init (); /*** Start FreeType ***/ ClearScreen(); ShowScreen(); #ifdef HW_RVL initialise_power(); have_sd = initFAT(WaitPromptChoice ("Use internal SD or FAT32 USB device?", "USB", "SD")); #else //Returns 1 (memory card in slot B, sd gecko in slot A) if A button was pressed and 0 if B button was pressed MEM_CARD = WaitPromptChoice ("Please select the slot where SD Gecko is inserted", "SLOT B", "SLOT A"); have_sd = initFAT(MEM_CARD); #endif for (;;) { /*** Select Mode ***/ ClearScreen(); setfontsize (FONT_SIZE); freecardbuf(); cancel = 0;/******a global value to track action aborted by user pressing button B********/ doall = 0; mode = SelectMode (); #ifdef HW_RVL if ((mode != 500 ) && (mode != 100) && (mode != 600)){ if (WaitPromptChoice ("Please select a memory card slot", "Slot B", "Slot A") == 1) { MEM_CARD = CARD_SLOTA; }else { MEM_CARD = CARD_SLOTB; } } #endif /*** Mode == 100 for backup, 200 for restore ***/ switch (mode) { case 100 : //User pressed A so keep looping //SMB_BackupMode(); //WaitPrompt ("Inactive"); break; case 200 : //User wants to delete MC_DeleteMode(MEM_CARD); break; case 300 : //User wants to backup if (have_sd) SD_BackupMode(); else WaitPrompt("Reboot aplication with a FAT device"); break; case 400 : //User wants to restore if (have_sd) SD_RestoreMode(); else WaitPrompt("Reboot aplication with a FAT device"); break; case 500 ://exit ShowAction ("Exiting..."); deinitFAT(); #ifdef HW_RVL //if there's a loader stub load it, if not return to wii menu. if (!!*(u32*)0x80001800) exit(1); else SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0); #else if (psoid[0] == PSOSDLOADID) PSOReload (); else SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0); #endif break; //PSO_Reload case 600 : //User wants to backup full card /* if (have_sd) SD_BackupModeAllFiles(); else WaitPrompt("Reboot aplication with an SD card"); */ break; case 700 : //Raw backup mode if (have_sd) { SD_RawBackupMode(); }else { WaitPrompt("Reboot aplication with a FAT device"); } break; case 800 : //Raw restore mode //These two lines are a work around for the second call of CARD_Probe to detect a newly inserted memory card CARD_Probe(MEM_CARD); VIDEO_WaitVSync (); if (CARD_Probe(MEM_CARD) > 0) { if (have_sd) SD_RawRestoreMode(); else WaitPrompt("Reboot aplication with a FAT device"); }else if (MEM_CARD) { WaitPrompt("Please insert a memory card in slot B"); }else { WaitPrompt("Please insert a memory card in slot A"); } break; case 900 : //Format card mode //These two lines are a work around for the second call of CARD_Probe to detect a newly inserted memory card CARD_Probe(MEM_CARD); VIDEO_WaitVSync (); if (CARD_Probe(MEM_CARD) > 0) { clearRightPane(); DrawText(390,224,"____________________"); DrawText(390,248,"F o r m a t C a r d"); DrawText(460,268,"M o d e"); DrawText(390,272,"____________________"); MC_FormatMode(MEM_CARD); }else if (MEM_CARD) { WaitPrompt("Please insert a memory card in slot B"); }else { WaitPrompt("Please insert a memory card in slot A"); } break; } offsetchanged = true; } while (1); return 0; }
/**************************************************************************** * BackupMode -SD Mode * * Perform backup of a memory card file to a SD Card. ****************************************************************************/ void SD_BackupMode () { int memitems; int selected = 0; int bytestodo; char buffer[256], text[64]; clearRightPane(); DrawText(386,130,"B a c k u p M o d e"); DrawText(386,134,"_____________________"); writeStatusBar("Reading memory card... ", ""); /*** Get the directory listing from the memory card ***/ memitems = CardGetDirectory (MEM_CARD); setfontsize (14); writeStatusBar("Pick a file using UP or DOWN ", "Press A to backup savegame") ; #ifdef HW_RVL DrawText(40, 60, "Press R/1 to backup ALL savegames"); #else DrawText(40, 60, "Press R to backup ALL savegames"); #endif /*** If it's a blank card, get out of here ***/ if (!memitems) { WaitPrompt ("No saved games to backup!"); } else { selected = ShowSelector (1); if (cancel) { WaitPrompt ("Backup action cancelled!"); } else if(doall) { doall = WaitPromptChoice("Are you sure you want to backup all files?", "No", "Yes"); if (doall) { //Backup All files for ( selected = 0; selected < memitems; selected++ ) { /*** Backup files ***/ sprintf(buffer, "[%d/%d] Reading from MC slot %s", selected+1, memitems, (MEM_CARD) ? "B" : "A"); ShowAction(buffer); bytestodo = CardReadFile(MEM_CARD, selected); if (bytestodo) { sprintf(buffer, "[%d/%d] Saving to FAT device", selected+1, memitems); ShowAction(buffer); if (!SDSaveMCImage()) { strncpy(text, (char*)filelist[selected], 32); text[32]='\0'; sprintf(buffer, "Error during backup (%s). Continue?", text); doall = WaitPromptChoice(buffer, "Yes", "No"); if (doall) { WaitPrompt ("Backup action cancelled due to error!"); return; } } } else { WaitPrompt ("Error reading MC file"); return; } } WaitPrompt("Full card backup done!"); return; } } else { /*** Backup the file ***/ ShowAction ("Reading File From MC SLOT B"); bytestodo = CardReadFile (MEM_CARD, selected); if (bytestodo) { ShowAction ("Saving to FAT device"); if (SDSaveMCImage()) { WaitPrompt ("Backup complete"); return; } else { WaitPrompt ("Backup failed"); return; } } else { WaitPrompt ("Error reading MC file"); return; } } } return; }