Difference between revisions of "Inner FAT"

From 3dbrew
Jump to navigation Jump to search
(Layout Variants)
(Copy FAT info from savegame)
Line 71: Line 71:
 
** file entry table as well
 
** file entry table as well
 
** title entries (title info or ticket) are allocated as normal files in the data region as well.
 
** title entries (title info or ticket) are allocated as normal files in the data region as well.
 +
 +
== 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 filesystem header. Each entry corresponds to a block in the data region (the block size is defined in filesystem 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)
 +
 +
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
 +
 +
Here is an example: [https://raw.githubusercontent.com/wwylele/3ds-save-tool/master/disa-fat.png]
 +
 +
For extdata, because only two "files" (directory and file entry tables) are allocated in the data region, and their size never changes once the extdata is created, they are guaranteed continuous in the data region, and the FAT degenerates to two big nodes. Therefore, instead of going through FAT, the offset and size of directory / file entry table can be found directly by offset = entry_table_starting block * data_region_block_size + data_region_offset and size = entry_table_block_count * data_region_block_size.

Revision as of 22:30, 3 May 2019

This page describes a common FAT-like file system used in Savegames, Extdata and Title Database. This file system format has several variants depending on which kind of data it stores. All the three kinds of data that use this file system structure also happen to use the DISA and DIFF container as well, but there is no direct relationship between the file system and the DISA/DIFF container. All data formats described here is in the inner data of the DISA/DIFF container (i.e. IVFC level 4). Please refer to the DISA/DIFF page for how to unwrap it first before trying to extract the file system.

Overview

The file system consists of the following components:

  • header
  • directory hash table
  • file hash table
  • file allocation table
  • directory entry table
  • file entry table
  • data region

The file allocation table (FAT) forms several linked lists inside, each of which represents a "file" allocated in the data region. Please refer to the File Allocation Table section below for more detail. In some variants, the directory entry table and the file entry table are also allocated as two special "files" in the data region, managed by the FAT, while in others they are stand-alone tables located outside the data region.

Layout Variants

Four variants of the file system layout has been identified. A summary diagram can be found here: [1]

Savegame, duplicate data = true

Such savegame is a single DISA container that only has one partition which is always configured as external IVFC level 4 disabled (see DISA format for details). All components are stored in this partition as

  • filesystem header at the beginning
  • directory hash table
  • file hash table
  • file allocation table
  • data region
    • directory entry table is allocated inside data region
    • file entry table as well
    • all file data is also allocated here

In this layout, all data is duplicated by DISA's DPFS tree, which is what the parameter duplicate data implies.

Savegame, duplicate data = false

Such savegame is a single DISA container that 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

  • Partition A
    • filesystem header at the beginning.
    • directory hash table
    • file hash table
    • (stand-alone) file allocation table
    • (stand-alone) directory entry table
    • file entry table
  • Partition B
    • used as data region entirely, and only has file data allocated.

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.

Extdata

An extdata consists of several DIFF containers (device files), among which the special device file 00000000/00000001 contains the inner FAT system, while other devices contains normal subfiles of the extdata. Please refer to Extdata for detail. The special file 00000000/00000001 contists of the following components

  • filesystem header at the beginning
  • directory hash table
  • file hash table
  • file allocation table (degenerate, because the data region only has two "files": the directory entry table and the file entry table)
  • data region
    • directory entry table allocated inside data region
    • file entry table as well
    • normal subfiles are NOT in the data region. They are in their DIFF containers instead.

Title database

All Title Database files are DIFF containers. Except for certs.db, all of them uses this filesystem in the DIFF inner data, which consists of

  • database-specific pre-header at the beginning (See Title Database)
  • filesystem header
  • directory Hash Table (degenerate and always has only one bucket, as there is only one directory for "root")
  • file Hash Table
  • file allocation table
  • data region
    • directory entry table allocated inside data region (degenerate, as there is only one directory for "root")
    • file entry table as well
    • title entries (title info or ticket) are allocated as normal files in the data region as well.

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 filesystem header. Each entry corresponds to a block in the data region (the block size is defined in filesystem 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.

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 n entries starting from FAT[k] is in the following format:

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])
  • Note: all indices above are entry indices (block index + 1)

All free blocks that are not allocated to any files also form a node chain in the allocation table. The head index of this "free chain" is recorded in FAT[0].Index_V. Other fields of FAT[0] are all zero

Here is an example: [2]

For extdata, because only two "files" (directory and file entry tables) are allocated in the data region, and their size never changes once the extdata is created, they are guaranteed continuous in the data region, and the FAT degenerates to two big nodes. Therefore, instead of going through FAT, the offset and size of directory / file entry table can be found directly by offset = entry_table_starting block * data_region_block_size + data_region_offset and size = entry_table_block_count * data_region_block_size.