RomFS

RomFS

Overview #

RomFS (or Read-Only Filesystem) is part of the NCCH format, and is used as external file storage.

RomFS can be used:

  • in conjunction with the ExeFS of a NCCH
  • to contain the game manual accessible from the Home Menu
  • or to contain game cartridge update data

(There may be more implementations in the future)

Format #

The RomFS is wrapped inside a IVFC hash-tree container, and the actual data is structured like a node-based tree, starting at the root level directory node, moving down to other directory and file nodes. Each directory node has pointers to child directory nodes and siblings, together with a pointer to the first file node for that directory. Each file node has pointers to their next file node, together with information about the actual file data.

The RomFS IVFC hash-tree header is 0x5C bytes long and is structured as follows:

STARTSIZEDESCRIPTION
0x000x4Magic “IVFC”
0x040x4Magic number 0x10000
0x080x4Master hash size
0x0C0x8Level 1 logical offset
0x140x8Level 1 hashdata size
0x1C0x4Level 1 block size, in log2
0x200x4Reserved
0x240x8Level 2 logical offset
0x2C0x8Level 2 hashdata size
0x340x4Level 2 block size, in log2.
0x380x4Reserved
0x3C0x8Level 3 logical offset
0x440x8Level 3 hashdata size
0x4C0x4Level 3 block size, in log2.
0x500x4Reserved
0x540x4Reserved
0x580x4Optional info size.

Level 3 Format #

The Level 3 partition of a RomFS consists of a header specifying offsets to four tables, followed by filedata (aligned to 16-bytes). Though their size varies by RomFS contents, the tables are always sequential and in the same order, as follows:

STARTSIZEDESCRIPTION
0x00x28Header
0x28VariesDirectory HashKey Table
VariesVariesDirectory Metadata Table
VariesVariesFile HashKey Table
VariesVariesFile Metadata Table
VariesVariesFile Data

Level 3 Header Format #

RomFS Header data is always 0x28 bytes long, and follows this layout (offsets are from the start of the header):

STARTSIZEDESCRIPTION
0x00x4Header Length
0x40x4Directory Hash Table Offset
0x80x4Directory Hash Table Length
0xC0x4Directory Metadata Table Offset
0x100x4Directory Metadata Table Length
0x140x4File Hash Table Offset
0x180x4File Hash Table Length
0x1C0x4File Metadata Table Offset
0x200x4File Metadata Table Length
0x240x4File Data Offset

Directory Metadata Structure #

When a RomFS is built, directories are added recursively starting with the root. When a directory is added, all of its files are added to the File Metadata Table, then all of its subdirectories (if any), are added to the table. If any of the directory’s subdirectories have their own subdirectories, the current directory’s subdirectories are all added before the subdirectories’ subdirectories are added.

A Metadata entry for a directory has the following structure (all values are initialized to 0xFFFFFFFF, and remain that way when unused):

STARTSIZEDESCRIPTION
0x00x4Offset of Parent Directory (self if Root)
0x40x4Offset of next Sibling Directory
0x80x4Offset of first Child Directory (Subdirectory)
0xC0x4Offset of first File (in File Metadata Table)
0x100x4Offset of next Directory in the same Hash Table bucket
0x140x4Name Length
0x18Name Length (rounded up to multiple of 4)Directory Name (Unicode)

File Metadata Structure #

A Metadata entry for a file has the following structure (all values are initialized to 0xFFFFFFFF, and remain that way when unused):

STARTSIZEDESCRIPTION
0x00x4Offset of Containing Directory (within Directory Metadata Table)
0x40x4Offset of next Sibling File
0x80x8Offset of File’s Data
0x100x8Length of File’s Data
0x180x4Offset of next File in the same Hash Table bucket
0x1C0x4Name Length
0x20Name Length (rounded up to multiple of 4)File Name (Unicode)

Hash Table Structure #

For both files and directories, a 🔗 separate chaining hash table is created for quick lookup.

A hash table consists of a number of buckets, all initialized to 0xFFFFFFFF. The size of the table is dependent on the number of entries in the relevant MetaData table (it’s probably intended to always be the smallest prime number greater than or equal to the number of entries, but the implementation was lazy), illustrated by the following code (C#):

        public static byte[] GetHashTableLength(uint numEntries)
        {
            uint count = numEntries;
            if (numEntries < 3)
                count = 3;
            else if (numEntries < 19)
                count |= 1;
            else
            {
                while (count % 2 == 0
                    || count % 3 == 0
                    || count % 5 == 0
                    || count % 7 == 0
                    || count % 11 == 0
                    || count % 13 == 0
                    || count % 17 == 0)
                {
                    count++;
                }
            }
            return count;
        }

The hash function is based off directory/file name (byte array taken from Metadata entry) and Parent Directory’s offset (C#):

        public static uint CalcPathHash(byte[] Name, uint ParentOffset)
        {
            uint hash = ParentOffset ^ 123456789;
            for (int i = 0; i < Name.Length; i += 2)
            {
                hash = (hash >> 5) | (hash << 27);
                hash ^= (ushort)((Name[i]) | (Name[i + 1] << 8));
            }
            return hash;
        }

Each directory/file is put into the ith bucket, where i is the hash taken modulus of bucket count. The directories/files in the same bucket form a linked list, with the value in hash table as the offset to the head element. When creating the hash table, a latter added directory/file is always added as the head element of the linked list.