Beispiel #1
0
int slot_save(int slot, int device)
{
  char filename[MAXPATHLEN];
  int filesize, done = 0;
  int offset = device ? 2112 : 0;
  u8 *savebuffer;

  if (slot > 0)
  {
    /* allocate buffer */
    savebuffer = (u8 *)memalign(32,STATE_SIZE);
    if (!savebuffer)
    {
      GUI_WaitPrompt("Error","Unable to allocate memory !");
      return 0;
    }

    GUI_MsgBoxOpen("Information","Saving State ...",1);
    filesize = state_save(&savebuffer[offset]);
  }
  else
  {
    if (!sram.on)
    {
       GUI_WaitPrompt("Error","SRAM is disabled !");
       return 0;
    }

    /* allocate buffer */
    savebuffer = (u8 *)memalign(32,0x10000+offset);
    if (!savebuffer)
    {
      GUI_WaitPrompt("Error","Unable to allocate memory !");
      return 0;
    }

    GUI_MsgBoxOpen("Information","Saving SRAM ...",1);
    memcpy(&savebuffer[offset], sram.sram, 0x10000);
    sram.crc = crc32(0, sram.sram, 0x10000);
    filesize = 0x10000;
  }

  if (!device)
  {
    /* FAT support */
    if (slot > 0)
      sprintf(filename, "%s/saves/%s.gp%d", DEFAULT_PATH, rom_filename, slot - 1);
    else
      sprintf(filename, "%s/saves/%s.srm", DEFAULT_PATH, rom_filename);

    /* Open file */
    FILE *fp = fopen(filename, "wb");
    if (!fp)
    {
      GUI_WaitPrompt("Error","Unable to open file !");
      free(savebuffer);
      return 0;
    }

    /* Write from buffer (2k blocks) */
    while (filesize > FILECHUNK)
    {
      fwrite(savebuffer + done, FILECHUNK, 1, fp);
      done += FILECHUNK;
      filesize -= FILECHUNK;
    }

    /* Write remaining bytes */
    fwrite(savebuffer + done, filesize, 1, fp);
    done += filesize;
    fclose(fp);
  }
  else
  {
    /* Memory Card support */
    if (slot > 0)
      sprintf(filename, "MD-%04X.gp%d", rominfo.realchecksum, slot - 1);
    else
      sprintf(filename, "MD-%04X.srm", rominfo.realchecksum);

    /* Initialise the CARD system */
    char action[64];
    memset(&SysArea, 0, CARD_WORKAREA);
    CARD_Init("GENP", "00");

    /* CARD slot */
    device--;

    /* Attempt to mount the card */
    if (!CardMount(device))
    {
      GUI_WaitPrompt("Error","Unable to mount memory card");
      free(savebuffer);
      return 0;
    }

    /* Retrieve the sector size */
    u32 SectorSize = 0;
    int CardError = CARD_GetSectorSize(device, &SectorSize);
    if (!SectorSize)
    {
      sprintf(action, "Invalid sector size (%d)", CardError);
      GUI_WaitPrompt("Error",action);
      CARD_Unmount(device);
      free(savebuffer);
      return 0;
    }

    /* Build the output buffer */
    char comment[2][32] = { {"Genesis Plus GX"}, {"SRAM Save"} };
    strcpy (comment[1], filename);
    memcpy (&savebuffer[0], &icon, 2048);
    memcpy (&savebuffer[2048], &comment[0], 64);

    /* Adjust file size */
    filesize += 2112;
    if (filesize % SectorSize)
      filesize = ((filesize / SectorSize) + 1) * SectorSize;

    /* Check if file already exists */
    card_file CardFile;
    if (CARD_Open(device, filename, &CardFile) == CARD_ERROR_READY)
    {
      int size = filesize - CardFile.len;
      CARD_Close(&CardFile);
      memset(&CardFile,0,sizeof(CardFile));

      /* Check file new size */
      if (size > 0)
      {
        CardError = CARD_Create(device, "TEMP", size, &CardFile);
        if (CardError)
        {
          sprintf(action, "Unable to increase file size (%d)", CardError);
          GUI_WaitPrompt("Error",action);
          CARD_Unmount(device);
          free(savebuffer);
          return 0;
        }

        /* delete temporary file */
        CARD_Close(&CardFile);
        memset(&CardFile,0,sizeof(CardFile));
        CARD_Delete(device, "TEMP");
      }

      /* delete previously existing file */
      CARD_Delete(device, filename);
    }

    /* Create a new file */
    CardError = CARD_Create(device, filename, filesize, &CardFile);
    if (CardError)
    {
      sprintf(action, "Unable to create file (%d)", CardError);
      GUI_WaitPrompt("Error",action);
      CARD_Unmount(device);
      free(savebuffer);
      return 0;
    }

    /* Update file informations */
    time_t rawtime;
    time(&rawtime);
    card_stat CardStatus;
    CARD_GetStatus(device, CardFile.filenum, &CardStatus);
    CardStatus.icon_addr = 0x0;
    CardStatus.icon_fmt = 2;
    CardStatus.icon_speed = 1;
    CardStatus.comment_addr = 2048;
    CardStatus.time = rawtime;
    CARD_SetStatus(device, CardFile.filenum, &CardStatus);

    /* Write file sectors */
    while (filesize > 0)
    {
      CARD_Write(&CardFile, &savebuffer[done], SectorSize, done);
      filesize -= SectorSize;
      done += SectorSize;
    }

    /* Close file */
    CARD_Close(&CardFile);
    CARD_Unmount(device);
  }

  GUI_MsgBoxClose();
  free(savebuffer);

  /* Save screenshot */
  if (slot && !device)
  {
    sprintf(filename,"%s/saves/%s__%d.png", DEFAULT_PATH, rom_filename, slot - 1);
    gxSaveScreenshot(filename);
  }

  return 1;
}
Beispiel #2
0
/****************************************************************************
 * SaveMCFile
 * Write savebuffer to Memory Card file
 ***************************************************************************/
int
SaveMCFile (char *buf, int slot, char *filename, int datasize, bool silent)
{
	card_file CardFile;
	card_stat CardStatus;
	int CardError;
	unsigned int blocks;
	unsigned int SectorSize;
	int byteswritten = 0;
	int bytesleft = 0;

	if(datasize <= 0)
		return 0;

	// add save icon and comments
	memmove(buf+sizeof(saveicon)+64, buf, datasize);
	memcpy(buf, saveicon, sizeof(saveicon));
	memcpy(buf+sizeof(saveicon), savecomments, 64);
	datasize += (sizeof(saveicon)+64);

	// Try to mount the card
	CardError = MountMC(slot, NOTSILENT);

	if (CardError == 0)
	{
		// Get Sector Size
		CARD_GetSectorSize (slot, &SectorSize);

		// Calculate number of blocks required
		blocks = (datasize / SectorSize) * SectorSize;
		if (datasize % SectorSize)
			blocks += SectorSize;

		// Delete existing file (if present)
		memset(&CardStatus, 0, sizeof(card_stat));
		CardError = CARD_Open (slot, filename, &CardFile);

		if(CardError == 0)
		{
			CARD_Close (&CardFile);
			CardError = CARD_Delete(slot, filename);
			if (CardError)
			{
				ErrorPrompt("Unable to delete existing file!");
				goto done;
			}
		}

		// Create new file
		memset(&CardStatus, 0, sizeof(card_stat));
		CardError = CARD_Create (slot, filename, blocks, &CardFile);
		if (CardError)
		{
			if (CardError == CARD_ERROR_INSSPACE)
				ErrorPrompt("Insufficient space to create file!");
			else
				ErrorPrompt("Unable to create card file!");
			goto done;
		}

		// Now, have an open file handle, ready to send out the data
		CARD_GetStatus (slot, CardFile.filenum, &CardStatus);
		CardStatus.icon_addr = 0x0;
		CardStatus.icon_fmt = 2;
		CardStatus.icon_speed = 1;
		CardStatus.comment_addr = 2048;
		CARD_SetStatus (slot, CardFile.filenum, &CardStatus);

		bytesleft = blocks;

		while (bytesleft > 0)
		{
			CardError =
				CARD_Write (&CardFile, buf + byteswritten, SectorSize, byteswritten);

			if(CardError)
			{
				ErrorPrompt("Error writing file!");
				byteswritten = 0;
				break;
			}

			bytesleft -= SectorSize;
			byteswritten += SectorSize;

			ShowProgress ("Saving...", byteswritten, blocks);
		}
		CARD_Close (&CardFile);
		CancelAction();

		if (byteswritten > 0 && GCSettings.VerifySaves)
		{
			// Verify the written file
			if (!VerifyMCFile (buf, slot, filename, byteswritten) )
				byteswritten = 0;
		}
done:
		CARD_Unmount (slot);
	}

	free(SysArea);
	return byteswritten;
}
// Only accepts a .gci file
int deviceHandler_CARD_writeFile(file_handle* file, void* data, unsigned int length) {
	card_stat CardStatus;
	card_dir CardDir;
	card_file CardFile;
	GCI gci; 
	int err = 0;
	unsigned int SectorSize = 0, slot = file->fileBase>>24;
	char tmpBuf[5];
	
	/*** Clear out the status ***/
	memset(&CardStatus, 0, sizeof(card_stat));
	memcpy(&gci, data, sizeof(GCI));

	memcpy(&CardStatus.gamecode, &gci.gamecode, 4);
	memcpy(&CardStatus.company, &gci.company, 2);
	CardStatus.banner_fmt = gci.banner_fmt;
	memcpy(&CardStatus.filename, &gci.filename, CARD_FILENAMELEN);
	CardStatus.time = gci.time;
	CardStatus.icon_addr = gci.icon_addr;
	CardStatus.icon_fmt = gci.icon_fmt;
	CardStatus.icon_speed = gci.icon_speed;
	CardStatus.len = gci.filesize8 * 8192;
	CardStatus.comment_addr = gci.comment_addr;
	
	CARD_Init((char*)gci.gamecode,(char*)gci.company);
	memcpy(&tmpBuf[0],(char*)gci.gamecode,4);
	tmpBuf[4]='\0';
	CARD_SetGamecode(&tmpBuf[0]);
	memcpy(&tmpBuf[0],(char*)gci.company,2);
	tmpBuf[2]='\0';
	CARD_SetCompany(&tmpBuf[0]);
	err = CARD_Mount (slot, sys_area, NULL);
	if (err) return -1;

  	CARD_GetSectorSize (slot, &SectorSize);
  	
  	/*** If this file exists, abort ***/
	err = CARD_FindFirst (slot, &CardDir, false);
	while (err != CARD_ERROR_NOFILE){
	    if (strcmp ((char *) CardDir.filename, (char *)gci.filename) == 0){
			/*** Found the file - abort ***/
		    CARD_Unmount (slot);
		    return -2;
		}
    	err = CARD_FindNext (&CardDir);
	}
    /*** Now restore the file from backup ***/
  	err = CARD_Create (slot, (char *) gci.filename, gci.filesize8 * 8192, &CardFile);
  	if (err){
    	CARD_Unmount (slot);
      	return -3;
  	}
  	
  	/*** Now write the file data, in sector sized chunks ***/
  	int offset = 0;
  	while (offset < (gci.filesize8 * 8192)){
    	if ((offset + SectorSize) <= (gci.filesize8 * 8192)) {
        	CARD_Write (&CardFile, data + 0x40 + offset, SectorSize, offset);
    	}
     	else {	
     		CARD_Write (&CardFile, data + 0x40 + offset, ((offset + SectorSize) - (gci.filesize8 * 8192)), offset);
     	}
        offset += SectorSize;
  	}
  
	/*** Finally, update the status ***/
	CARD_SetStatus (slot, CardFile.filenum, &CardStatus);
	
	CARD_Close (&CardFile);
	CARD_Unmount (slot);

	return length;
}
Beispiel #4
0
/****************************************************************************
* 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;
}
Beispiel #5
0
/****************************************************************************
 * ManageState
 *
 * Here is the main Freeze File Management stuff.
 * The output file contains an icon (2K), 64 bytes comment and the STATE (~128k)
 *
 * direction == 0 save, 1 load.
 ****************************************************************************/
int ManageState (u8 direction, u8 device)
{
  if (!smsromsize)
    return 0;

  char filename[MAXJOLIET];

  /* clean buffer */
  memset(savebuffer, 0, 0x12000);

  if (direction)
    ShowAction ("Loading State ...");
  else
    ShowAction ("Saving State ...");

  if (device == 0)
  {
    /* FAT support */
    sprintf (filename, "%s.spz", rom_filename);
    return FAT_ManageFile(filename,direction);
  }

  /* Memory CARD support */
  char action[80];
  int CardError;
  unsigned int SectorSize = 0;
  int blocks;
  char comment[2][32] = { {"SMS Plus 1.3 [FRZ]"}, {"Freeze State"} };
  int outbytes = 0;
  int sbo;
  int state_size = 0;

  /* First, build a filename */
  sprintf (filename, "%08X.spz", (u32)crc32 (0, &cart.rom[0], smsromsize));
  strcpy (comment[1], filename);

  /* set MCARD slot nr. */
  u8 CARDSLOT = device - 1;

  /* Saving */
  if (direction == 0)
  {
    /*** Build the output buffer ***/
    memcpy (&savebuffer, &icon, 2048);
    memcpy (&savebuffer[2048], &comment[0], 64);
    state_size = system_save_state(&savebuffer[2112]);
  }

  outbytes = 2048 + 64 + state_size;

  /*** Initialise the CARD system ***/
  memset (&SysArea, 0, CARD_WORKAREA);
  CARD_Init ("SMSP", "00");

  /*** Attempt to mount the card ***/
  CardError = MountTheCard (CARDSLOT);

  if (CardError)
  {
    /*** Retrieve the sector size ***/
    CardError = CARD_GetSectorSize (CARDSLOT, &SectorSize);

    if (SectorSize)
    {
      switch (direction)
      {
        case 0: /*** Saving ***/
          /*** Determine number of blocks on this card ***/
          blocks = (outbytes / SectorSize) * SectorSize;
          if (outbytes % SectorSize) blocks += SectorSize;

          /*** Check if a previous save exists ***/
          if (CardFileExists (filename,CARDSLOT))
          {
            CardError = CARD_Open (CARDSLOT, filename, &CardFile);
            if (CardError)
            {
              sprintf (action, "Error Open : %d", CardError);
              WaitPrompt (action);
              CARD_Unmount (CARDSLOT);
              return 0;
            }

            int size = CardFile.len;
            CARD_Close (&CardFile);

            if (size < blocks)
            {
              /* new size is bigger: check if there is enough space left */
              CardError = CARD_Create (CARDSLOT, "TEMP", blocks-size, &CardFile);
              if (CardError)
              {
                sprintf (action, "Error Update : %d", CardError);
                WaitPrompt (action);
                CARD_Unmount (CARDSLOT);
                return 0;
              }
              CARD_Close (&CardFile);
              CARD_Delete(CARDSLOT, "TEMP");
            }

            /* always delete existing slot */
            CARD_Delete(CARDSLOT, filename);
          }

          /*** Create a new slot ***/
          CardError = CARD_Create (CARDSLOT, filename, blocks, &CardFile);
          if (CardError)
          {
            sprintf (action, "Error create : %d %d", CardError, CARDSLOT);
            WaitPrompt (action);
            CARD_Unmount (CARDSLOT);
            return 0;
          }

          /*** Continue and save ***/
          CARD_GetStatus (CARDSLOT, CardFile.filenum, &CardStatus);
          CardStatus.icon_addr = 0x0;
          CardStatus.icon_fmt = 2;
          CardStatus.icon_speed = 1;
          CardStatus.comment_addr = 2048;
          CARD_SetStatus (CARDSLOT, CardFile.filenum, &CardStatus);

          /*** And write the blocks out ***/
          sbo = 0;
          while (outbytes > 0)
          {
            CardError = CARD_Write (&CardFile, &savebuffer[sbo], SectorSize, sbo);
            outbytes -= SectorSize;
            sbo += SectorSize;
          }

          CARD_Close (&CardFile);
          CARD_Unmount (CARDSLOT);
          sprintf (action, "Saved %d bytes successfully", blocks);
          WaitPrompt (action);
          return 1;

      default: /*** Loading ***/
        if (!CardFileExists (filename, CARDSLOT))
        {
          WaitPrompt ("No Savestate Found");
          CARD_Unmount (CARDSLOT);
          return 0;
        }

        memset (&CardFile, 0, sizeof (CardFile));
        CardError = CARD_Open (CARDSLOT, filename, &CardFile);
        if (CardError)
        {
          sprintf (action, "Error Open : %d", CardError);
          WaitPrompt (action);
          CARD_Unmount (CARDSLOT);
          return 0;
        }

        blocks = CardFile.len;
        if (blocks < SectorSize) blocks = SectorSize;
        if (blocks % SectorSize) blocks++;

        /*** Just read the file back in ***/
        sbo = 0;
        int size = blocks;
        while (blocks > 0)
        {
          CARD_Read (&CardFile, &savebuffer[sbo], SectorSize, sbo);
          sbo += SectorSize;
          blocks -= SectorSize;
        }
        CARD_Close (&CardFile);
        CARD_Unmount (CARDSLOT);

        /*** Load State ***/
        system_load_state(&savebuffer[2112]);

        /*** Inform user ***/
        sprintf (action, "Loaded %d bytes successfully", size);
        WaitPrompt (action);
        return 1;
      }
    }
    WaitPrompt ("Invalid sector size");
    return 0;
  }
  WaitPrompt ("Unable to mount memory card");
  return 0; /*** Signal failure ***/
}
Beispiel #6
0
// This function should always be called for the FULL length cause CARD is lame like that.
s32 deviceHandler_CARD_writeFile(file_handle* file, void* data, u32 length) {
	
	if(gciInfo == NULL) {	// Swiss ID for this
		CARD_SetGameAndCompany();
	} else {	// Game specific
		char company[4], gamecode[8];
		memset(company, 0, 4);
		memset(gamecode, 0, 8);
		memcpy(gamecode, gciInfo->gamecode, 4);
		memcpy(company, gciInfo->company, 2);
		CARD_SetCompany(company);
		CARD_SetGamecode(gamecode);
	}
	
	card_file cardfile;
	unsigned int slot = (!strncmp((const char*)initial_CARDB.name, file->name, 7)), ret = 0;
	unsigned int adj_length = (length % 8192 == 0 ? length : (length + (8192-length%8192)))+8192;
	char *filename = NULL;
	char fname[CARD_FILENAMELEN+1];
	if(gciInfo != NULL) {
		memset(fname, 0, CARD_FILENAMELEN+1);
		memcpy(fname, gciInfo->filename, CARD_FILENAMELEN);
		filename = &fname[0];
		adj_length = gciInfo->filesize8*8192;
	} else {
		filename = getRelativeName(file->name);
	}
	
	// Open the file based on the slot & file name
	ret = CARD_Open(slot,filename, &cardfile);

	print_gecko("Tried to open: [%s] in slot %s got res: %i\r\n",filename, slot ? "B":"A", ret);
	
	if(ret == CARD_ERROR_NOFILE) {
		// If the file doesn't exist, create it.
		ret = CARD_Create(slot, filename,adj_length,&cardfile);
		print_gecko("Tried to create: [%s] in slot %s got res: %i\r\n",filename, slot ? "B":"A", ret);
		if(ret != CARD_ERROR_READY) {
			return ret;
		}
		ret = CARD_Open(slot,filename, &cardfile);
		print_gecko("Tried to open after create: [%s] in slot %s got res: %i\r\n",filename, slot ? "B":"A", ret);
	}
	
	if(ret != CARD_ERROR_READY) {
		// Something went wrong
		return -1;
	}

	// Write the icon, comment, etc.
	time_t gc_time = time (NULL);
	char *tmpBuffer = memalign(32,adj_length);
	memset(tmpBuffer,0,adj_length);
	if(gciInfo == NULL) {
		strcpy(tmpBuffer, filename);
		strcpy(tmpBuffer+0x20, ctime (&gc_time));
		memcpy(tmpBuffer+0x40, gamecube_rgb, sizeof(gamecube_rgb));	// Copy icon
		memcpy(tmpBuffer+0x2000, data, length);	// Copy data
	}
	else {
		data+=sizeof(GCI);
		memcpy(tmpBuffer, data, length);	// Copy data
	}
	
	// The file exists by now, write at offset.
	int amount_written = 0;
	while(amount_written != adj_length) {
		ret = CARD_Write(&cardfile, tmpBuffer+amount_written, card_sectorsize[slot], file->offset);
		file->offset += card_sectorsize[slot];
		amount_written += card_sectorsize[slot];
		print_gecko("Tried to write: [%s] in slot %s got res: %i\r\n",filename, slot ? "B":"A", ret);
		if(ret != CARD_ERROR_READY) break;
	}
	
	card_stat cardstat;
	if(gciInfo == NULL) {	// Swiss
		memset(&cardstat, 0, sizeof(card_stat));
		memcpy(&cardstat.filename, filename, CARD_FILENAMELEN-1);
		cardstat.time = gc_time;
		cardstat.comment_addr = 0;
		cardstat.icon_addr = 0x40;
		cardstat.icon_speed = CARD_SPEED_FAST;
		cardstat.banner_fmt = CARD_BANNER_NONE;
		cardstat.icon_fmt = CARD_ICON_RGB;
	}
	else {	// Info is coming from a GCI
		cardstat.banner_fmt = gciInfo->banner_fmt;
		cardstat.time = gciInfo->time;
		cardstat.icon_addr = gciInfo->icon_addr;
		cardstat.icon_fmt = gciInfo->icon_fmt;
		cardstat.icon_speed = gciInfo->icon_speed;
		cardstat.comment_addr = gciInfo->comment_addr;
	}
	CARD_SetStatus (slot, cardfile.filenum, &cardstat);
	
	free(tmpBuffer);
	CARD_Close(&cardfile);
	return ret == CARD_ERROR_READY ? length : ret;
}
Beispiel #7
0
/****************************************************************************
 * SaveMCFile
 * Write savebuffer to Memory Card file
 ***************************************************************************/
int
SaveMCFile (char *buf, int slot, char *filename, int datasize, bool silent)
{
	int CardError;
	unsigned int blocks;
	unsigned int SectorSize;
	char msg[80];

	if(datasize <= 0)
		return 0;

	/*** Initialize Card System ***/
	memset (SysArea, 0, CARD_WORKAREA);
	CARD_Init ("VBA0", "00");

	/*** Try to mount the card ***/
	CardError = MountCard(slot, NOTSILENT);

	if (CardError == 0)
	{
		/*** Get Sector Size ***/
		CARD_GetSectorSize (slot, &SectorSize);

		/*** Calculate number of blocks required ***/
		blocks = (datasize / SectorSize) * SectorSize;
		if (datasize % SectorSize)
			blocks += SectorSize;

		/*** Does this file exist ? ***/
		if (CardFileExists (filename, slot))
		{
			/*** Try to open the file ***/
			CardError = CARD_Open (slot, filename, &CardFile);
			if (CardError)
			{
				CARD_Unmount (slot);
				WaitPrompt("Unable to open card file!");
				return 0;
			}

			if ( (s32)blocks > CardFile.len )  /*** new data is longer ***/
			{
				CARD_Close (&CardFile);

				/*** Try to create temp file to check available space ***/
				CardError = CARD_Create (slot, "TEMPFILESNES9XGX201", blocks, &CardFile);
				if (CardError)
				{
					CARD_Unmount (slot);
					WaitPrompt("Not enough space to update file!");
					return 0;
				}

				/*** Delete the temporary file ***/
				CARD_Close (&CardFile);
				CardError = CARD_Delete(slot, "TEMPFILESNES9XGX201");
				if (CardError)
				{
					CARD_Unmount (slot);
					WaitPrompt("Unable to delete temporary file!");
					return 0;
				}

				/*** Delete the existing shorter file ***/
				CardError = CARD_Delete(slot, filename);
				if (CardError)
				{
					CARD_Unmount (slot);
					WaitPrompt("Unable to delete existing file!");
					return 0;
				}

				/*** Create new, longer file ***/
				CardError = CARD_Create (slot, filename, blocks, &CardFile);
				if (CardError)
				{
					CARD_Unmount (slot);
					WaitPrompt("Unable to create updated card file!");
					return 0;
				}
			}
		}
		else  /*** no file existed, create new one ***/
		{
			/*** Create new file ***/
			CardError = CARD_Create (slot, filename, blocks, &CardFile);
			if (CardError)
			{
				CARD_Unmount (slot);
				if ( CardError == CARD_ERROR_INSSPACE )
					WaitPrompt("Not enough space to create file!");
				else
					WaitPrompt("Unable to create card file!");
				return 0;
			}
		}

		/*** Now, have an open file handle, ready to send out the data ***/
		CARD_GetStatus (slot, CardFile.filenum, &CardStatus);
		CardStatus.icon_addr = 0x0;
		CardStatus.icon_fmt = 2;
		CardStatus.icon_speed = 1;
		CardStatus.comment_addr = 2048;
		CARD_SetStatus (slot, CardFile.filenum, &CardStatus);

		int byteswritten = 0;
		int bytesleft = blocks;
		while (bytesleft > 0)
		{
			CardError =
				CARD_Write (&CardFile, buf + byteswritten,
							SectorSize, byteswritten);
			bytesleft -= SectorSize;
			byteswritten += SectorSize;

			sprintf (msg, "Wrote %d of %d bytes", byteswritten, blocks);
			ShowProgress (msg, byteswritten, blocks);
		}

		CARD_Close (&CardFile);
		CARD_Unmount (slot);

		if ( GCSettings.VerifySaves )
		{
			/*** Verify the written file, but only up to the length we wrote
				 because the file could be longer due to past writes    ***/
			if ( VerifyMCFile (buf, slot, filename, byteswritten) )
				return byteswritten;
			else
				return 0;
		}
		else
			return byteswritten;
	}
	else
		if (slot == CARD_SLOTA)
			WaitPrompt("Unable to Mount Slot A Memory Card!");
		else
			WaitPrompt("Unable to Mount Slot B Memory Card!");

	return 0;
}
Beispiel #8
0
void MCManage(int mode, int slot)
{

	char mcFilename[80];
	int CardError;
	card_dir CardDir;
	card_file CardFile;
	int SectorSize;
	int found = 0;
	int FileSize;
	int actualSize;
	int savedBytes=0;
	char debug[128];

	CARDSLOT = slot;
	/*** Build the file name ***/
	sprintf(mcFilename, "FCEU-%08x.fcs", iNESGameCRC32);

	/*** Mount the Card ***/
	CARD_Init("FCEU", "00");
	
	/*** Try for memory card in slot A ***/
	CardError = CARD_Mount(CARDSLOT, SysArea, CardRemoved );

	if ( CardError >= 0 )
	{
		/*** Get card sector size ***/
		CardError = CARD_GetSectorSize(CARDSLOT, &SectorSize);				

		switch ( mode ) {

			case 0 : {	/*** Save Game ***/
				/*** Look for this file ***/
				CardError = CARD_FindFirst(CARDSLOT, &CardDir, true);

				found = 0;

				card_stat CardStatus;
				while ( CardError != CARD_ERROR_NOFILE )
				{
					CardError = CARD_FindNext(&CardDir);
					if ( strcmp(CardDir.filename, mcFilename) == 0 )
					found = 1;
				}

				/*** Determine number of sectors required ***/
				savedBytes = actualSize = GCFCEUSS_Save();
				sprintf(debug, "Saving in MC ...");
				ShowAction(debug);

				FileSize = ( actualSize / SectorSize ) * SectorSize;
				if ( actualSize % SectorSize )
					FileSize += SectorSize;

				/*** Now write the file out ***/
				if ( !found )
					CardError = CARD_Create(CARDSLOT, mcFilename, FileSize, &CardFile);
				else
					CardError = CARD_Open(CARDSLOT, mcFilename, &CardFile);

				CARD_GetStatus( CARDSLOT, CardFile.filenum, &CardStatus);
				CardStatus.icon_addr = 0;
				CardStatus.icon_fmt = 2;
				CardStatus.icon_speed = 1;
				CardStatus.comment_addr = sizeof(saveicon);
				CARD_SetStatus( CARDSLOT, CardFile.filenum, &CardStatus);

				/*** Haha! libogc only write one block at a time! ***/	
				if ( CardError == 0 )
				{
					int sbo = 0;
					while ( actualSize > 0 )
					{
						CardError = CARD_Write(&CardFile, &statebuffer[sbo], SectorSize, sbo );
						actualSize -= SectorSize;
						sbo += SectorSize;
					}

					CardError = CARD_Close(&CardFile);
					sprintf(debug, "Saved %d bytes successfully!", savedBytes);
					WaitPrompt(debug);
				} 
				else WaitPrompt("Save Failed!");

				CARD_Unmount(CARDSLOT);

				} 
				break;	/*** End save ***/

			case 1: {	/*** Load state ***/
                /*** Look for this file ***/
                CardError = CARD_FindFirst(CARDSLOT, &CardDir, true);
					
				memopen();	/*** Clear the buffer ***/

                found = 0;

                while ( CardError != CARD_ERROR_NOFILE )
                {
                    CardError = CARD_FindNext(&CardDir);
                    if ( strcmp(CardDir.filename, mcFilename) == 0 )
                       found = 1;
                }

				if ( found == 0 )
				{
					WaitPrompt("No Save Game Found");
					CARD_Unmount(CARDSLOT);
					return;
				}

				/*** Load the file into memory ***/
				CardError = CARD_Open(CARDSLOT, mcFilename, &CardFile);
				CardError = CARD_Read(&CardFile, &statebuffer, SectorSize, 0);
				
				/*** Get actual size of the file ***/
				memcpy(&actualSize, &statebuffer[FILESIZEOFFSET], 4);
				savedBytes = actualSize;
				
				int sbo = SectorSize;
				actualSize -= SectorSize;	
				while( actualSize > 0 )
				{
					CARD_Read(&CardFile, &statebuffer[sbo], SectorSize, sbo);
					actualSize -= SectorSize;
					sbo += SectorSize;
				}
				CARD_Close(&CardFile);

				/*** Finally, do load ***/
				GCFCEUSS_Load();

				CARD_Unmount(CARDSLOT);
				sprintf(debug, "Loaded %d bytes successfully!", savedBytes);
				WaitPrompt(debug);

				} 
				break;	/*** End load ***/
	
			default: break;	
		}
	} else {
		WaitPrompt("Cannot Mount Memory Card!");
	} 
}