Changes

Jump to navigation Jump to search
4,436 bytes added ,  18:19, 3 January 2018
Nuke outdated content and upload new one. Sorry for removing another nice but outdated image
Line 101: Line 101:  
* 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
 
* 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 ==
+
== Components and partitions ==
   −
A savegame can have one or two partitions (specified by the [[DISA and DIFF|DISA container format]]). The first partition is called the SAVE partition, and the second optional partition is called the DATA partition.
+
A savegame, after unwrapping the DISA container, consists of the following components:
   −
=== The SAVE partition ===
+
* SAVE header
 +
* directory hash table
 +
* file hash table
 +
* file allocation table
 +
* directory entry table
 +
* file entry table
 +
* data region
   −
'''Finding the folders table:'''
+
A DISA container can have one or two partitions, and correspondingly a savegame has two possible layouts. The layout is determined by the parameter <code>duplicate data</code> passed in [[FS:FormatSaveData]] or [[FS:CreateSystemSaveData]].
* If DATA partition exists: At folder table exact offset from the SAVE struct (from the beginning of the struct).
  −
* Otherwise: The 'folder table offset' * 'folder table media' (=0x200) from the 'filestore offset'. (usually 0 from filebase)
     −
'''Finding the files table:'''
+
=== Layout for <code>duplicate data = true</code> ===
* If DATA partition exists: At file table exact offset from the SAVE struct (from the beginning of the struct).
  −
* Otherwise: The 'file table offset' * 'file table media' (=0x200) from the 'filestore offset'.
     −
'''Detemining the filestore base:'''
+
The DISA container only has one partition which is always configured as external IVFC level 4 disabled (see [[DISA and DIFF|DISA format for details]]). All components are stored in this partition as
* If DATA partition exists: At file base from the DATA's DIFI struct into the DATA partition.
  −
* Otherwise: At the 'filestore offset' from the beginning of the SAVE struct.
     −
Folder's entry structure:
+
* SAVE header at the beginning
<pre>
+
* directory hash table
struct folder_entry {
+
* file hash table
    u32 parent_folder_index;    // 0 for the root folder (the second entry)
+
* file allocation table
    u8  filename[0x10];          // "" for the root folder (the second entry)
+
* data region
    u32 next_folder_index;      // 0 if the last folder
+
** directory entry table is allocated inside data region
    u32 first_sub_folder_index;  // 0 if has no sub folder
+
** file entry table as well
    u32 first_sub_file_index;    // 0 if has no sub file(?)
+
** all file data is also allocated here
    u32 unk1;                    // flags? always 0?
+
 
    u32 unk2;                    // the index of the next folder with the same path hash? 0 if the end?
+
In this layout, all data is duplicated by DISA's DPFS tree, which is what the parameter <code>duplicate data</code> implies.
}
+
 
struct dummy_folder_entry {
+
=== Layout for <code>duplicate data = false</code> ===
    u32 total_entry_count;
  −
    u32 unk;                    // max_entry_count? FolderMapSize related?
  −
    u32 zero_padding[7];
  −
    u32 next_dummy_folder_index; // 0 if the last dummy folder;
  −
}
  −
</pre>
     −
File's entry structure:
+
The DISA container has two partitions. Partition A is always configured as external IVFC level 4 disabled, and partition B is configured as it enabled. Components are stored among the two partitions as
<pre>
  −
struct file_entry {
  −
    u32 parent_folder_index;
  −
    u8  filename[0x10];
  −
    u32 next_file_index;      // 0 if the last file
  −
    u32 unk1;                  // looks like time stamp?
  −
    u32 block_offset;
  −
    u64 file_size;
  −
    u32 unk2;                  // flags?
  −
    u32 unk3;                  // the index of the next file with the same path hash? 0 if the end?
  −
}
  −
struct dummy_file_entry {
  −
    u32 total_entry_count;
  −
    u32 unk;                  // max_entry_count? FileMapSize related?
  −
    u32 zero_padding[9];
  −
    u32 next_dummy_file_index; // 0 if the last dummy file;
  −
}
  −
</pre>
     −
Both table contains some "dummy" entries, which are probably left there when deleting files/folders, reserved for future use. The first entry of the two tables are always a dummy entry (as the "first" dummy entry).
+
* Partition A
 +
** SAVE header at the beginning.
 +
** directory hash table
 +
** file hash table
 +
** file allocation table
 +
** directory entry table
 +
** file entry table
 +
* Partition B
 +
** used as data region entirely, and only has file data allocated.
   −
Reading the files out is as simple as taking the file base offset and adding (block_offset * 0x200) to it.
+
In this layout, all file system metadata is duplicated by partition A DPFS tree, but file data is not as partition B has external IVFC level 4.
   −
Here's a follow-up example from the Legend of Zelda: Ocarina of Time 3D:
+
=== SAVE Header ===
<pre>
  −
//FST entry = SAVE base + File base + (FST offset * 0x200) + (FST entry # * 0x30)
  −
//0x2600    = 0x2000    + 0x400    + (0x1        * 0x200) + (0x0        * 0x30)
     −
00002600: 03000000 09000000 00000000 00000000  ................
+
The SAVE header defines the rest components of the savegame. All &quot;offsets&quot; in the table are relative to the beginning of SAVE image, while all &quot;starting block index&quot; are relative to the beginning of data region.
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  Ü...............
  −
</pre>
     −
{| class="wikitable"
+
{| class="wikitable" border="1"
|-
+
! Offset
! Start
   
! Length
 
! Length
 
! Description
 
! Description
Line 186: Line 157:  
| 0x00
 
| 0x00
 
| 4
 
| 4
| Magic ("SAVE")
+
| Magic &quot;SAVE&quot;
 
|-
 
|-
 
| 0x04
 
| 0x04
 
| 4
 
| 4
| Magic Number (0x40000)
+
| Magic 0x40000
 
|-
 
|-
 
| 0x08
 
| 0x08
 
| 8
 
| 8
| Offset to data in this SAVE header(normally 0x20)
+
| File system Information offset (0x20)
 
|-
 
|-
 
| 0x10
 
| 0x10
 
| 8
 
| 8
| Partition Size [medias]
+
| Image size in blocks
 
|-
 
|-
 
| 0x18
 
| 0x18
 
| 4
 
| 4
| Partition Media Size
+
| Image block size
 
|-
 
|-
 
| 0x1C
 
| 0x1C
| 8
+
| 4
| Unknown
+
| Padding
 +
|-
 +
|
 +
 
 +
|
 +
 
 +
| Below is File system Information
 +
|-
 +
| 0x20
 +
| 4
 +
| Unknown:thinking:
 
|-
 
|-
 
| 0x24
 
| 0x24
 
| 4
 
| 4
| Media-size for the below sections
+
| Data region block size
 
|-
 
|-
 
| 0x28
 
| 0x28
 
| 8
 
| 8
| Folder Hash Table Offset
+
| Directory hash table offset
 
|-
 
|-
 
| 0x30
 
| 0x30
 
| 4
 
| 4
| Folder Hash Table Size / Bucket Count
+
| Directory hash table bucket count
 
|-
 
|-
 
| 0x34
 
| 0x34
 
| 4
 
| 4
| Unknown, Folder Hash Table size-related
+
| Padding
 
|-
 
|-
 
| 0x38
 
| 0x38
 
| 8
 
| 8
| File Hash Table Offset
+
| File hash table offset
 
|-
 
|-
 
| 0x40
 
| 0x40
 
| 4
 
| 4
| File Hash Table Size / Bucket Count
+
| File hash table bucket count
 
|-
 
|-
 
| 0x44
 
| 0x44
 
| 4
 
| 4
| Unknown, File Hash Table size-related
+
| Padding
 
|-
 
|-
 
| 0x48
 
| 0x48
 
| 8
 
| 8
| BlockMap Offset
+
| File allocation table offset
 
|-
 
|-
 
| 0x50
 
| 0x50
 
| 4
 
| 4
| BlockMap Size
+
| File allocation table entry count
 
|-
 
|-
 
| 0x54
 
| 0x54
 
| 4
 
| 4
| Unknown, BlockMap size-related
+
| Padding
 
|-
 
|-
 
| 0x58
 
| 0x58
 
| 8
 
| 8
| File store offset (from SAVE)
+
| Data region offset (if no partition B)
 
|-
 
|-
 
| 0x60
 
| 0x60
 
| 4
 
| 4
| File store length [medias]
+
| Data region block count (= File allocation table entry count)
 
|-
 
|-
 
| 0x64
 
| 0x64
 
| 4
 
| 4
| Unknown, File store size-related
+
| Padding
 
|-
 
|-
 
| 0x68
 
| 0x68
| 4/8
+
| 8
| Folders Table offset (8 bytes in DATA)
+
| If partition B exists: directory entry table offset;
 
|-
 
|-
| 0x6C
+
|
| 4
+
 
| Folders Table Length (medias) (Only in no DATA)
+
|
 +
 
 +
| otherwise: u32 directory entry table starting block index + u32 directory entry table block count
 
|-
 
|-
 
| 0x70
 
| 0x70
 
| 4
 
| 4
| Maximum Folder Count
+
| Maximum directory count
 
|-
 
|-
 
| 0x74
 
| 0x74
 
| 4
 
| 4
| Unknown, Folders Table size-related
+
| Padding
 
|-
 
|-
 
| 0x78
 
| 0x78
| 4/8
+
| 8
| Files Table offset (8 bytes in DATA)
+
| If partition B exists: file entry table offset;
 
|-
 
|-
| 0x7C
+
|
| 4
+
 
| Files Table Length (medias) (Only in no DATA)
+
|
 +
 
 +
| otherwise: u32 file entry table starting block index + u32 file entry table block count
 
|-
 
|-
 
| 0x80
 
| 0x80
 
| 4
 
| 4
| Maximum File Count
+
| Maximum file count
 
|-
 
|-
 
| 0x84
 
| 0x84
 
| 4
 
| 4
| Unknown, Files Table size-related
+
| Padding
 +
|}
 +
 
 +
* The file/directory bucket count &amp; maximum count are specified by the parameters of [[FS:FormatSaveData]] or [[FS:CreateSystemSaveData]].
 +
* When partition B doesn't exist, directory &amp; file entry tables are allocated in the data region, and while be marked allocated in file allocation table as if they are two normal files. However, only continuous allocation has been observed, so directly reading <code>block_count * block_size</code> bytes from <code>data_region + starting_block_index * block_size</code> should be safe. See the section [[#File Allocation Table]] below for more information.
 +
 
 +
=== Directory Entry Table ===
 +
 
 +
The directory entry table is an array of the entry type shown below. It describes the directory hierarchy of the file system.
 +
 
 +
{| class="wikitable" border="1"
 +
! Offset
 +
! Length
 +
! Description
 +
|-
 +
| 0x00
 +
| 4
 +
| Parent directory index. 0 for root
 
|-
 
|-
 +
| 0x04
 +
| 16
 +
| ASCII directory name in. All zero for root
 +
|-
 +
| 0x14
 +
| 4
 +
| Next sibling directory index. 0 if this is the last one
 +
|-
 +
| 0x18
 +
| 4
 +
| First subdirectory index. 0 if not exists
 +
|-
 +
| 0x1C
 +
| 4
 +
| First file index in file entry table. 0 for empty directory
 +
|-
 +
| 0x20
 +
| 4
 +
| Padding / zero?
 +
|-
 +
| 0x24
 +
| 4
 +
| Index of the next directory in the same hash table bucket. 0 if this is the last one
 
|}
 
|}
   −
* The folder and file hash table are tables of uint32. They are probably for quick lookup and are similar to those in [[RomFS]]
+
There are also some dummy entries in the array:
* The BlockMap is a map of the blocks in the filestore. An entry in the BlockMap is 2 uint32: {uint32 start_block; uint32 end_block; }. This is still being researched. (You can use [[3DSExplorer]] to see those maps.
     −
'''Summary Drawing'''
+
{| class="wikitable" border="1"
 +
! Offset
 +
! Length
 +
! Description
 +
|-
 +
| 0x00
 +
| 4
 +
| Current Total entry count
 +
|-
 +
| 0x04
 +
| 4
 +
| Maximum entry count = maximum directory count + 2
 +
|-
 +
| 0x08
 +
| 28
 +
| Padding / All zero
 +
|-
 +
| 0x24
 +
| 4
 +
| Index of the next dummy entry. 0 if this is the last one
 +
|}
 +
 
 +
The 0-th entry of the array is always a dummy entry, which functions as the head of the dummy entry linked list. The 1-st entry of the array is always the root. Therefore maximum entry count is two more than maximum directory count. Dummy entries are left there when deleting directories, and reserved for future use.
 +
 
 +
=== File Entry Table ===
 +
 
 +
The file entry table is an array of the entry type shown below. It contains information for each file.
 +
 
 +
{| class="wikitable" border="1"
 +
! Offset
 +
! Length
 +
! Description
 +
|-
 +
| 0x00
 +
| 4
 +
| Parent directory index in directory entry table
 +
|-
 +
| 0x04
 +
| 16
 +
| ASCII file name
 +
|-
 +
| 0x14
 +
| 4
 +
| Next sibling file index. 0 if this is the last one
 +
|-
 +
| 0x18
 +
| 4
 +
| Padding
 +
|-
 +
| 0x1C
 +
| 4
 +
| First block index in data region. 0x80000000 if the file is just created and has no data.
 +
|-
 +
| 0x20
 +
| 8
 +
| File Size
 +
|-
 +
| 0x28
 +
| 4
 +
| Padding?
 +
|-
 +
| 0x2C
 +
| 4
 +
| Index of the next file in the same hash table bucket. 0 if this is the last one
 +
|}
 +
 
 +
Like directory entry table, file entry table also has some dummy entries:
 +
 
 +
{| class="wikitable" border="1"
 +
! Offset
 +
! Length
 +
! Description
 +
|-
 +
| 0x00
 +
| 4
 +
| Current total entry count
 +
|-
 +
| 0x04
 +
| 4
 +
| Maximum entry count = maximum file count + 1
 +
|-
 +
| 0x08
 +
| 36
 +
| Padding / All zero
 +
|-
 +
| 0x2C
 +
| 4
 +
| Index of the next dummy entry. 0 if this is the last one
 +
|}
 +
 
 +
The 0-th entry of the array is always a dummy entry, which functions as the head of the dummy entry linked list. Therefore maximum entry count is one more than maximum file count. Dummy entries are left there when deleting files, and reserved for future use.
 +
 
 +
=== Directory Hash Table &amp; File Hash Table ===
 +
 
 +
This is a u32 array of size = bucket count, each of which is an index to the directory / file entry table. The directory / file name is hashed and its entry index is put to the corresponding bucket. If there is already a directory/file entry in the bucket, then it appends to the linked list formed by <code>Index of the next directory/file in the same hash table bucket</code> field in the directory/file entry table. i.e. this is a hash table using separate chaining with linked lists
 +
 
 +
The hash function takes the parent index and the name as key. The function is equivalent to
 +
 
 +
<pre>uint32_t GetBucket(
 +
    char name[16], // takes all 16 bytes including trailing zeros
 +
    uint32_t parent_dir_index,
 +
    uint32_t bucket_count
 +
) {
 +
    uint32_t hash = parent_dir_index ^ 0x091A2B3C;
 +
    for (int i = 0; i &lt; 4; ++i) {
 +
        hash = (hash &gt;&gt; 1) | (hash &lt;&lt; 31);
 +
        hash ^= (uint32_t)name[i * 4]
 +
        hash ^= (uint32_t)name[i * 4 + 1] &lt;&lt; 8
 +
        hash ^= (uint32_t)name[i * 4 + 2] &lt;&lt; 16
 +
        hash ^= (uint32_t)name[i * 4 + 3] &lt;&lt; 24
 +
    }
 +
    return hash % bucket_count;
 +
}
 +
</pre>
 +
=== File Allocation Table ===
 +
 
 +
The file allocation table is an array of a 8-byte entry shown below. The array size is actually ''one larger than'' the size recorded in the SAVE header. Each entry corresponds to a block in the data region (the block size is defined in SAVE header). However, the 0th entry corresponds to nothing, so the corresponding block index is off by one. e.g. entry 31 in this table corresponds to block 30 in the data region.
 +
 
 +
{| class="wikitable" border="1"
 +
! Offset
 +
! Length
 +
! Description
 +
|-
 +
| 0x00
 +
| 4
 +
| bit[0:30]: Index U; bit[31]: Flag U
 +
|-
 +
| 0x04
 +
| 4
 +
| bit[0:30]: Index V; bit[31]: Flag V
 +
|}
 +
 
 +
Entries in this table forms several chains, representing how blocks in the data region should be linked together. However, unlike normal FAT systems, which uses chains of entries, 3DS savegames use chain of ''nodes''. Each node spans one or multiple entries.
 +
 
 +
One node spanning <code>n</code> entries starting from <code>FAT[k]</code> is in the following format:
 +
 
 +
<pre>FAT[k + 0]:
 +
    Index_U = index of the first entry of the previous node. 0 if this is the first node.
 +
    Index_V = index of the first entry of the next node. 0 if this is the last node.
 +
    Flag_U set if this is the first node.
 +
    Flag_V set if this node has multiple entries.
 +
 
 +
FAT[k + 1]:
 +
    Index_U = k (the first entry index of this node)
 +
    Index_V = k + n - 1 (the last entry index of this node)
 +
    Flag_U always set
 +
    Flag_V always clear
 +
 
 +
FAT[k + 2] ~ FAT[k + n - 2]:
 +
    All these entries are uninitialized
 +
 
 +
FAT[k + n - 1]:
 +
    Index_U = k
 +
    Index_V = k + n - 1
 +
    Flag_U always set
 +
    Flag_V always clear
 +
    (Same values as FAT[k + 1])
 +
</pre>
 +
* Note: all indices above are entry indices (block index + 1)
   −
[[File:Sfsave_drawing.png]]
+
All free blocks that are not allocated to any files also form a node chain in the allocation table. The head index of this &quot;free chain&quot; is recorded in <code>FAT[0].Index_V</code>. Other fields of <code>FAT[0]</code> are all zero
    
== Initialization ==
 
== Initialization ==
242

edits

Navigation menu