Castlevania - Dawn of Sorrow savefile corrupted when switching BIOS

Ask questions about DraStic or discuss compatibility issues here.
Post Reply
bt04
Posts:3
Joined:Tue Jan 03, 2017 12:17 pm
Castlevania - Dawn of Sorrow savefile corrupted when switching BIOS

Post by bt04 » Tue Jan 03, 2017 12:53 pm

Problem:
My savefile (Castlevania - Dawn of Sorrow (USA).dsv) gets corrupted and deleted after adding the official DS BIOS files to the system directory (nds_bios_arm9.bin and nds_bios_arm7.bin)

The error message is "Save data is corrupted Data has been deleted"

The result is identical to this DeSmuME bug: https://sourceforge.net/p/desmume/bugs/978/
I'm guessing it could be due to a similar CRC generation issue found in DeSmuME custom BIOS
the bios SWI crc is used by the game to protect the savefile. our built-in nonbios crc implementation was wrong, so the crcs would not match between a bios and nonbios run
Question:
Is there a way to export, copy, or convert the savegame or savestate in a way that is compatible with the official DS BIOS?

Additional Info:
This started after trying to use the savefile in DeSmuME and noticing that the savefile was not compatible. I started using official DS BIOS in the hope that this would make a compatible savefile across both emulators. I tried loading an old savestate (created using custom BIOS) -> saving from within the game (using official DS BIOS) -> resetting the game, but it still wiped out the save.

Using Drastic r2.5.0.3a build 89

Exophase
Posts:1715
Joined:Mon Aug 05, 2013 9:08 pm

Re: Castlevania - Dawn of Sorrow savefile corrupted when switching BIOS

Post by Exophase » Tue Jan 03, 2017 3:15 pm

I remember finding a problem with DeSmuME's CRC SWIs but I guess DraStic must still have issues too. Hopefully that can be fixed as well.. Is there any other reason you're using the DS BIOSes?

Unfortunately you can't switch to a different BIOS using the same savestate with DraStic, but you might be able to do this in DeSmuME (based on how it does BIOS HLE). If you can you might be able to make a save after loading the savestate, using the different BIOS, which would hopefully insert the right CRC.

If this doesn't work then I don't know what else can be done outside of manually editing the save file with a hex editor to insert the correct CRC. But that would require actually calculating the CRC somehow, which would take a non-trivial amount of reverse engineering.

bt04
Posts:3
Joined:Tue Jan 03, 2017 12:17 pm

Re: Castlevania - Dawn of Sorrow savefile corrupted when switching BIOS

Post by bt04 » Tue Jan 03, 2017 11:43 pm

Is there any other reason you're using the DS BIOSes?
No, just want to play the same game on play on PC, phone, or tablet, whatever's most convenient. I started playing this one on the tablet, but only found the savefile problem after about 20hrs of progress. It seems that using the official BIOS has the best success rate for cross platform compatibility for savefiles. I did prove that starting a new game with Official BIOS does work in both DeSmuME and DraStic. Drastic gets better fps of course!
you can't switch to a different BIOS using the same savestate with DraStic, but you might be able to do this in DeSmuME
Great idea, but I couldn't get the savestate to load in DeSmuME. I had assumed savestates are not compatible across emulators.
I made two types of savestate with and without "Store in-game Saves in Savestates" enabled in DraStic
"Load State From" and selecting "Castlevania - Dawn of Sorrow (USA)_8.dss"
I've also tried renaming the file to Castlevania - Dawn of Sorrow (USA).ds8 and choosing "Load State -> 8" from the menu.

I've tried this both with and without "Use External BIOS images" enabled in DeSmuME
that would require actually calculating the CRC somehow, which would take a non-trivial amount of reverse engineering.
There's on a few common CRC algorithms if you know the size of the payload and where the CRC bytes are stored in the in savefile, I might be able to figure it out. It should be something like trim the header and footer from the savefile and zero out the CRC bytes. Then run it through a couple of CRC generators to see if it matches a known good savefile.

Exophase
Posts:1715
Joined:Mon Aug 05, 2013 9:08 pm

Re: Castlevania - Dawn of Sorrow savefile corrupted when switching BIOS

Post by Exophase » Wed Jan 04, 2017 3:44 pm

You are right, savestates are not interchangeable between the two emulators. When I say make a savestate, I mean in DeSmuME. So I guess it'd be like this:

1) Load whichever BIOS lets you load your current save in DeSmuME
2) Make a savestate
3) Switch to using the other BIOS
4) Load the savestate and hope it works/doesn't crash
5) Make a new save
6) See if that still works with the BIOS in DeSmuME, then DraStic
bt04 wrote:There's on a few common CRC algorithms if you know the size of the payload and where the CRC bytes are stored in the in savefile, I might be able to figure it out. It should be something like trim the header and footer from the savefile and zero out the CRC bytes. Then run it through a couple of CRC generators to see if it matches a known good savefile.
Yeah, it's the knowing what the CRC is over and where the CRC is stored that are the annoying parts. But yeah it's doable with enough trial and error, maybe...

bt04
Posts:3
Joined:Tue Jan 03, 2017 12:17 pm

Re: Castlevania - Dawn of Sorrow savefile corrupted when switching BIOS

Post by bt04 » Wed Jan 04, 2017 10:05 pm

1) Load whichever BIOS lets you load your current save in DeSmuME
That's where I'm stuck because the only savefile I can load in DeSmuME is a clean one that has been created with Official DS BIOS using DraStic. The contents of the file are wiped as soon as either emulator detects bad CRC.

The good news is that this issue has been already solved in DeSmuME, so I looked into the source code for the CRC calculation, which has this awesome comment:
https://github.com/libretro/desmume/blo ... .cpp#L1108

Code: Select all

TEMPLATE static u32 getCRC16()
{
	//dawn of sorrow uses this to checksum its save data;
	//if this implementation is wrong, then it won't match what the real bios returns, 
	//and savefiles created with a bios will be invalid when loaded with non-bios (and vice-versa).
	//Once upon a time, desmume was doing this wrongly; this was due to its mis-use of high bits of the input CRC.
	//Additionally, that implementation was not handling odd sizes and addresses correctly (but this was discovered independently)
	//We have confirmed that the crc16 works the same on the arm9 and arm7.
	//The following call is left here so we can A/B test with the old version. Glad I left it, because we keep coming back to this code.
	//u32 old = getCRC16_old_and_broken<PROCNUM>(cpu->R[0],cpu->R[1],cpu->R[2])
I'm hoping that DraStic custom BIOS can be compared against this function from DeSmuME and fixed if necessary. That would allow me to load the savestate in a new Drastic release using custom BIOS and create an in-game savefile with correct CRC.

Post Reply