BFRES (File Format)

From MK8
Revision as of 05:09, 19 January 2015 by LordNed (talk | contribs) (Added a link to a partial extractor tool.)
Jump to navigation Jump to search

BFRES is an archive format used on Wii U and in Mario Kart 8 to store graphics data. Typically, a BFRES file will contain information for one model including textures and animations, but this is not necessarily the case. BFRES seems to be a direct upgrade of the BRRES format used on Wii. Due to their vast size, large BFRES files are often compressed using YAZ0 compression.

Format

This article describes the BFRES file format seen in Mario Kart 8. It is unknown if the file format differs between games like the BRRES format does. The basic architecture of a BFRES file is any number of subfiles each in one of 12 groups governing format and purpose. Each subfile is something like a model or a texture. Unless otherwise noted, all offsets in BFRES files are relative to themselves, not the beginning of the file which is more common in other formats.

Header (FRES)

Every BFRES file begins with an 0x6c byte FRES header.

Offset Size Description
0x00 4 "FRES" File identifier, ASCII string.
0x04 1 Unknown has value 0x03 in Mario Kart 8 files.[FRES 1]
0x05 1 Unknown has value 0x00, 0x03 or 0x04 in Mario Kart 8 files.
0x06 1 Unknown has value 0x00 in Mario Kart 8 files.
0x07 1 Unknown has value 0x01, 0x02 or 0x04 in Mario Kart 8 files.
0x08 2 Byte order mark (BOM): 0xFE,0xFF for big endian and 0xFF,0xFE for little endian. Seems to indicate endian-ness of embedded files.
0x0a 2 Unknown Probably a version number[FRES 2], always has value 0x0010 in Mario Kart 8 files.
0x0c 4 File length including all headers, in bytes.
0x10 4 File alignment a power of 2. Most specific data alignment required in the file. The graphics card often requires data to be aligned to specific power of 2 boundaries. If the file is loaded into memory aligned to this value[FRES 3], then all addresses will be correct for the graphics card.
0x14 4 File name offset (without file extension).
0x18 4 String table Length. Length in bytes of the String Table.
0x1c 4 String table offset.
0x20 4 × 12 File offsets. Offsets to index groups for each of the 12 subfile types. 0 indicates that a particular subfile type is not present.
0x50 2 × 12 File counts. Numer of files in the index groups for each of the 12 subfile types. 0 for subfile types which are not present.
0x68 4 Unknown always 0 in Mario Kart 8 files.
0x6c End of FRES header
  1. Almost certainly 4 separate bytes as have not yet read BOM.
  2. This is an assumption because of the parallel with BRRES.
  3. That is to say, the address of the data is a multiple of this value.

Index Group

The BFRES Index Group is a data structure that occurs very often within BFRES files and subfiles. It is a binary search tree in which a series of named data pointers are stored which are used to represent subfiles. It has a 0x08 byte header, as follows.

Offset Size Description
0x00 4 Length of group in bytes.
0x04 4 Number in group (excluding the root entry).
0x08 End of index group header

This is then followed by a number of entries equal to the number in the header. The first entry is never an actual data entry, but instead a reference point. Each entry is a 0x10 byte structure as follows.

Offset Type Description
0x00 4 Search value a value used in the binary search tree traversal algorithm.
0x04 2 Left index used in binary search tree traversal algorithm.
0x06 2 Right index used in binary search tree traversal algorithm.
0x08 4 Name pointer. Offset to the name of this entry.
0x0c 4 Data pointer. Offset to the data of this entry.
0x10 End of entry

It is unknown how the binary search tree algorithm works but it may be similar to the BRRES Index Group.

String Table

The BFRES String Table is a table that occurs near the end of the BFRES file. There is no header information for the table. References to the table are stored as offsets to stored to strings directly. The strings are stored in ASCII order (so Z comes before a). The length of each string is stored in the 4 bytes before that string and the strings are also null terminated. Each length is 4 byte aligned. The pointer in the FRES header points to the length of the first string.

Sub Files

The format and purpose of subfiles is determined entirely by the index group they're in of the 12 main index groups references by the FRES header.

Index Format
0 FMDL model.
1 FTEX texture.
2 FSKA data.
3 FSHU data. Unknown why 3 indices all use the same format.
4
5
6 FTXP data.
7 Unknown not seen in Mario Kart 8.
8 FVIS bone visibility.
9 FSHA data.
10 FSCN scene data.
11 Embedded file. The data offset points to a offset & length pair which describe the embedded file. The embedded files always seem to be placed at the end of the BFRES fille. Shader source code has been seen in embedded files, though this is not typically the case.

Tools

The following tool can operate on BFRES files: