Difference between revisions of "Savegames"

From 3dbrew
Jump to navigation Jump to search
(→‎Partitions: more on dpfs and ivfc)
Line 441: Line 441:
 
When a save EEPROM contains all xFFFF blocks it's assumed uninitialized by the game cartridges and it initializes default data in place, without prompting the user.  
 
When a save EEPROM contains all xFFFF blocks it's assumed uninitialized by the game cartridges and it initializes default data in place, without prompting the user.  
  
 +
I got a new game SplinterCell3D-Pal and I downloaded the save and it was 128KB of 0xFF, except the first 0x10 bytes which were the letter 'Z' (uppercase) --[[User:Elisherer|Elisherer]] 22:41, 15 October 2011 (CEST)
 +
 +
=== Fun Facts ===
 +
 +
If you have facts that you found out by looking at the binary files please share them here:
 +
 +
* From one save to another the game backups the last files that were in the partition and the entire image header in "random" locations.. --[[User:Elisherer|Elisherer]] 22:41, 15 October 2011 (CEST)
  
 
[[セーブデータ|Japanese]]
 
[[セーブデータ|Japanese]]

Revision as of 21:41, 15 October 2011

This page describes the format, de/encryption, etc. of savegames found in 3DS game cartridges/gamecards. You can find savegames from various 3DS games on the Games page.


Encryption

On the 3DS savegames are stored much like on the DS, that is on a FLASH chip in the gamecart. On the DS these savegames were stored in plain-text but on the 3DS a layer of encryption was added. This is highly likely a streamcipher, as the contents of several savegames exhibit the odd behavior that xor-ing certain parts of the savegame together will result in the plain-text appearing.

The reason this works is because the stream cipher used has a period of 512 bytes. That is to say, it will repeat the same keystream after 512 bytes. The way you encrypt with a stream cipher is you XOR your data with the keystream as it is produced. Unfortunately, if your streamcipher repeats and you are encrypting a known plain-text (in our case, zeros) you are basically giving away your valuable keystream.

So how do you use this to decrypt a savegame on a 3DS? First off, you chunk up the savegame into 512 byte chunks. Then, you bin these chunks by their contents, discarding any that contain only FF. Now look for the most common chunk. This is your keystream. Now XOR the keystream with your original savegame and you should have a fully decrypted savegame. XOR with the keystream again to produce an encrypted savegame.

Wear leveling

The 3DS employs a wear leveling scheme on the savegame FLASH chips. This is done through the usage of blockmaps and a journal. The blockmap is located at offset 0 of the flash chip, and is immediately followed by the journal. The initial state is dictated by the blockmap, and the journal is then applied to that.

First, there are 8 bytes whose purposes are currently unknown. Then comes the actual blockmap. The blockmap structure is simple:

struct header_entry {
        uint8_t phys_sec; // when bit7 is set, block has checksums, otherwise checksums are all zero
        uint8_t alloc_cnt;
        uint8_t chksums[8];
} __attribute__((__packed__));

There's one entry per sector, counting from physical sector 1 (sector 0 contains the blockmap/journal).

The 2 bytes that follow the blockmap are the CRC16 (with starting value 0xFFFF (like modbus)) of the first 8 bytes and the blockmap.

Then comes the journal. The journal structure is as follows:

struct sector_entry {
        uint8_t virt_sec;       // Mapped to sector
        uint8_t prev_virt_sec;  // Physical sector previously mapped to
        uint8_t phys_sec;       // Mapped from sector
        uint8_t prev_phys_sec;  // Virtual sector previously mapped to
        uint8_t phys_realloc_cnt;       // Amount of times physical sector has been remapped
        uint8_t virt_realloc_cnt;       // Amount of times virtual sector has been remapped
        uint8_t chksums[8];
} __attribute__((__packed__));

struct long_sector_entry{
        struct sector_entry sector;
        struct sector_entry dupe;
        uint32_t magic;
}__attribute__((__packed__));

With magic being a constant 0x080d6ce0.

The checksums in the blockmap/journal entries work as follows:

  • each byte is the checksum of an encrypted 0x200 bytes large block
  • to calculate the checksum, a CRC16 of the block (with starting value 0xFFFF) is calculated, and the two bytes of the CRC16 are XORed together to produce the 8bit checksum

Partitions

There can be multiple partitions on the chip. The partitions are represented by tables of DIFI blobs inside a DISA structure. The order of the DIFI blobs is the order of the partitions in the chip.

DISA

  • If the uint32 @ 0x168 into the image in the DISA is a %1=1, then first table is is hashed, otherwise the second DIFI table is hashed.
  • If the table has more then 1 DIFI then the uint32 @ 0x168 is the offset from the DATA partition to the file base (masked with 0xFFFFFFFE).
Start Length Description
0x00 4 Magic ("DISA")
0x04 4 Unknown (maybe magic, the same in all the save files so far)
0x08 8 Partition table size
0x10 8 Offset to primary partition table in DISA
0x18 8 Offset to secondary partition table in DISA
0x20 8 Partition table's length
0x28 8 SAVE Partition entry offset in the partition table
0x30 8 SAVE Partition entry length in the partition table
0x38 8 DATA Partition entry offset in the partition table
0x40 8 DATA Partition entry length in the partition table
0x48 8 SAVE Partition offset
0x50 8 SAVE Partition length
0x58 8 DATA Partition offset
0x60 8 DATA Partition length
0x68 4 Active table (and the offset to the filebase)
0x6C 0x20 Hash from active table
0x8C 4*29 Unknown
  • The hash in the DISA hashes the Active Table (starting from tables's offset to tables's offset + table length) with SHA256.
  • The partitions offsets points to a 0x1000 long block which isn't understood yet. The actual information starts after that block.

The DIFIs table @ 0x200 (into the image) is written twice, (Meaning, if there's 4 DIFI blobs then the table is 2 DIFIs long).

The second table is for backup. The active table is mentioned at 0x13C into the image (1=First table, other=Second Table)

DIFI

These 0x130 large blobs describe the partitions. Every DIFI blob describes a partition. Partitions are catted together, so after the end of one partition is the beginning of the next.

Actually DIFI blobs are 0x12C large because the last 4 are not used and appear 0xFFFFFFFF at the encrypted image.

For most games there's only 1 partition (The SAVE partition) and some (like Asphalt 3D, Steel Diver & Lego Star Wars III) has 2 partitions.

  • 2 Partitions means that the files inside the SAVE partition is on the other partition (we would call it DATA partition).
  • No more than 2 partitions have been seen yet (and can't be becuase of the DISA known structure).
Start Length Description
0x00 4 Magic ("DIFI")
0x04 4 Magic Number (0x10000)
0x08 8 Offset to "IVFC" blob in DIFI (usually 0x44)
0x10 8 Size of "IVFC" blob
0x18 8 Offset to "DPFS" blob in DIFI (usually 0xBC)
0x20 8 Size of "DPFS" blob
0x28 8 Offset to the hash in DIFI (usually 0x010C)
0x30 8 Size of this hash
0x38 4 Flags (1 means DATA partition)
0x3C 8 File base offset (for DATA partitions)

IVFC

Start Length Description
0x00 4 Magic ("IVFC")
0x04 4 Magic Number (0x20000)
0x08 8 Unknown (0x20?)
0x10 8 First Hash Offset
0x18 8 First Hash Length
0x20 8 First Hash Block Size (1<<value)
0x28 8 Second Hash Offset
0x30 8 Second Hash Length
0x38 8 Second Hash Block Size (1<<value)
0x40 8 HashTable Offset
0x48 8 HashTable Length
0x50 8 HashTable Block Size (1<<value)
0x58 8 FileSystem Offset
0x60 8 FileSystem Length
0x68 8 FileSystem Block Size (1<<value)
0x70 8 Unknown (usually 0x78=120)
  • First & Second hash are not understood yet.

DPFS

Start Length Description
0x00 4 Magic ("DPFS")
0x04 4 Magic Number (0x10000)
0x08 8 Offset To First table
0x10 8 First table length
0x18 8 First table block size (1<<value)
0x20 8 Offset To Second table
0x28 8 Second table length
0x30 8 Second table block size (1<<value)
0x38 8 Offset to Data
0x40 8 Data Length
0x48 8 Data block size (1<<value)
  • Every block this table point to is written twice (concatenated). You can see that the offset to the next block is twice the length (except the data which always begin after 0x1000).

The first partition's data starts at 0x2000. First comes the hashtable (usually start @ 0x40 into the partition) and then the filesystem.

The hashtable entries' size is 2^x where x is the 'Hashed block size' from the IVFC block.

Hash

After the DIFI,IVFC & DPFS comes a 0x20 long hash, it is unknown what it's hashing.

The SAVE file system

Savefiles are stored on the FLASH in a custom filesystem called SAVE. SAVE has a header which describes where the various bits of the filesystem live.

  • The SAVE filesystem works with a backup. There are two SAVE blocks inside the partition concatenated. It seems that the second is always the updated one...

Finding the file system file base:

  • If there's no DATA partiton the file base will be at the uint32 @ 0x58 into the SAVE header.
  • Otherwise the file base will be at ('active_table' & 0xFFFFFFFE) into the DATA partition.

Finding the file system table:

  • If the uint32 @ 0x58 and uint32 @ 0x6C into the SAVE header aren't 0 then the table is at [@0x58] + [@0x6C]*0x200.
  • Otherwise, (meaning the files are at another partition) the exact offset into the SAVE is @ 0x68.

File system's entry structure:

 struct fs_entry {
     u32 node_cnt;
     u8  filename[0x10];
     u32 index;
     u32 unk1; // magic?
     u32 block_offset;
     u32 file_size;
     u32 unk2;
     u32 unk3; // flags and/or date?
     u32 unk4;
 }

The first entry is the root directory, easily identifiable by the node_cnt being larger than 1. The node_cnt includes the root directory itself, so there are node_cnt - 1 files in the root directory. The entries that follow after the root directory are the actual files. Reading them out is as simple as taking the file base offset and adding (block_offset * 0x200) to it.

Here's a follow-up example from the Legend of Zelda: Ocarina of Time 3D:

//FST entry = SAVE base + File base + (FST offset * 0x200) + (FST entry # * 0x30)
//0x2600    = 0x2000    + 0x400     + (0x1        * 0x200) + (0x0         * 0x30)

00002600: 03000000 09000000 00000000 00000000  ................
00002610: 00000000 00000000 00000000 00000000  ................
00002620: 00000000 00000000 00000000 00000000  ................
00002630: 01000000 73797374 656D2E64 61740000  ....system.dat..
00002640: 00000000 00000000 D57B1100 02000000  ........Õ{......
00002650: 22000000 00000000 E8121500 00000000  ".......è.......
00002660: 01000000 73617665 30302E62 696E0000  ....save00.bin..
00002670: 00000000 01000000 69921100 03000000  ........i’......
00002680: DC140000 00000000 04000000 00000000  Ü...............

Example for a different file with different SAVE structure:

00002400 53415645 00000400 20000000 00000000 SAVE.... ....... //Save from Steel Diver
00002410 28030000 00000000 00020000 00000000 (...............
00002420 00000000 00020000 88000000 00000000 ................
00002430 03000000 00020000 94000000 00000000 ................
00002440 43000000 00020000 A0010000 00000000 C...............
00002450 28030000 00020000 00000000 00000000 (............... //[0x58] = 0 and 
00002460 28030000 00020000 E81A0000 00000000 (............... //[0x6C] = 0, but
00002470 00000000 00020000 381B0000 00000000 ........8....... //On offset 0x68 There's an exact offset to the FST 
00002480 40000000 00020000 01000000 00000000 @............... //meaning a uint32_t at 0x68 into the SAVE struct
00002490 00000000 00000000 00000000 02000000 ................

00003F30 00000000 00000000 04000000 41000000 ............A... // first fs_entry '04 00 00 00'
00003F40 00000000 00000000 00000000 00000000 ................ //exectly 0x1B38 from the save header struct
00003F50 00000000 00000000 00000000 00000000 ................
00003F60 00000000 00000000 01000000 67686F73 ............ghos
00003F70 742E7374 30370000 00000000 00000000 t.st07..........
00003F80 D57B1100 00000000 7E290000 00000000 .{......~)......
00003F90 00000000 00000000 01000000 73617665 ............save
00003FA0 2E737562 00000000 00000000 01000000 .sub............
00003FB0 D57B1100 15000000 9C090000 00000000 .{..............
00003FC0 00000000 00000000 01000000 73617665 ............save
00003FD0 2E706572 69730000 00000000 02000000 .peris..........
00003FE0 D57B1100 1A000000 29070000 00000000 .{......).......
00003FF0 00000000 00000000 00000000 00000000 ................

known struct until now:

struct save_header {
        char magic[4];           //'SAVE'
        u8 unknown0[0x54];
        u32 file_base_offset;
        u8 unknown1[0x10];
        u32 fst_block_offset;    //FST is in [file_base_offset] * 0x200 + [fst_block_offset]
        u8 unknown2[8];          //or (if no filebase)
        uint32 fst_exact_offset; //The exact offset from the header start
}

Initialization

When a save EEPROM contains all xFFFF blocks it's assumed uninitialized by the game cartridges and it initializes default data in place, without prompting the user.

I got a new game SplinterCell3D-Pal and I downloaded the save and it was 128KB of 0xFF, except the first 0x10 bytes which were the letter 'Z' (uppercase) --Elisherer 22:41, 15 October 2011 (CEST)

Fun Facts

If you have facts that you found out by looking at the binary files please share them here:

  • From one save to another the game backups the last files that were in the partition and the entire image header in "random" locations.. --Elisherer 22:41, 15 October 2011 (CEST)

Japanese