Virtual File System
Introduction
This chapter describes the types, usage scenarios, and operational methods of the Virtual File System (VFS).
The VFS implements two underlying file systems: FatFS and LittleFS.
FatFS offers advantages in compatibility, cross-platform support, and development convenience.
LittleFS excels in power-loss recovery, flash lifespan optimization, and low resource consumption.
Users can utilize VFS to abstract away differences between file systems and focus on development requirements.
VFS provides universal file operation interfaces such as fopen, fclose, fwrite, and fread (refer to Common File Operation).
The Key-Value (KV) and the BT FTL are implemented based on these universal file operations.
The VFS architecture is illustrated below:
VFS Initialization
The VFS on Flash contains two independent regions:
The VFS1 region serves as the configuration storage partition (prefix
vfs) and uses LittleFS (with power-loss protection support). It is also used by KV and BT FTL.The VFS2 region (prefix
vfs2) is reserved for user use; by default, it is not used and no Flash space is allocated to it. Users may enable it as FatFS according to their requirements.
The on-chip Flash targets (VFS1, VFS2, and the read-only images within the APP image) are registered automatically during system startup according to the configuration; no registration code is needed in the application.
Note
If VFS mount operation on flash fails, the system checks whether the flash is blank (all 0xFF). If true, flash programming will be executed to initialize VFS.
To use FatFS on the VFS2 region, navigate to
CONFIG File Systemvia Configuring Project (menuconfig) and selectFATFSinVFS2 partition.To use read-only FatFS, check
FATFS read only (all FATFS targets)inFATFS options. This option is global: every FatFS volume becomes read-only and must be registered withVFS_RO, and a mount failure never formats the volume, so a blank VFS2 partition stays unmounted.To use exFAT, check
FATFS Support EXFATinFATFS options.
Usage of VFS
VFS on Flash
If you need to adjust the VFS location, refer to Flash Layout Modification Guide to modify the VFS1 or VFS2 regions.
Caution
The VFS1 region must exist, and its size should always be equal to or greater than 128KB.
There are two VFS regions at most.
Note
When the capacity of the VFS partition on the flash storing and running the firmware is insufficient, a second Flash can be connected externally via the standard SPI interface to expand storage space. Subsequent documents uniformly refer to this external Flash as Secondary Flash.
VFS on SD card
Not supported.
Not supported.
Not supported.
Not supported.
Not supported.
The SD card is only supported by FatFS. Navigate to CONFIG File System via Configuring Project (menuconfig), select SD card, SDIO mode and choose FATFS in File system on SD card. Then register the SD card with VFS_TARGET_SD.
The SD card is only supported by FatFS. Navigate to CONFIG File System via Configuring Project (menuconfig), select SD card, SDIO mode and choose FATFS in File system on SD card. Then register the SD card with VFS_TARGET_SD.
Not supported.
The SD card is only supported by FatFS. Navigate to CONFIG File System via Configuring Project (menuconfig), select SD card, SDIO mode and choose FATFS in File system on SD card. Then register the SD card with VFS_TARGET_SD.
VFS within APP Image
If the content in the file system requires upgrades, such as replacing prompt tones and voice packages in voice solutions, VFS provides more convenient configuration capabilities. Detailed steps are as follows:
Prepare the read-only files and convert them into a FAT-formatted bin file. Refer to section VFS Bin File Generation for the method.
Name the bin file
fatfs.binand place it in{SDK}\build_RTL8721Dx.Refer to Configuring Project (menuconfig) to navigate to the
CONFIG File Systemconfiguration, then selectRead-only FATFS image within APP image. This option cannot be selected together withFATFSinVFS2 partition.Rebuild the application firmware.
The application firmware (app.bin) will include a read-only VFS area in FAT format, which will be mounted during the startup process.
Prepare the read-only files and convert them into a FAT-formatted bin file. Refer to section VFS Bin File Generation for the method.
Name the bin file
fatfs.binand place it in{SDK}\build_RTLxxx.Refer to Configuring Project (menuconfig) to navigate to the
CONFIG File Systemconfiguration, then selectRead-only FATFS image within APP image. This option cannot be selected together withFATFSinVFS2 partition.Rebuild the application firmware.
The application firmware (app.bin) will include a read-only VFS area in FAT format, which will be mounted during the startup process.
Prepare the read-only files and convert them into a FAT-formatted bin file. Refer to section VFS Bin File Generation for the method.
Name the bin file
fatfs.binand place it in{SDK}\build_RTLxxx.Refer to Configuring Project (menuconfig) to navigate to the
CONFIG File Systemconfiguration, then selectRead-only FATFS image within APP image. This option cannot be selected together withFATFSinVFS2 partition.Rebuild the application firmware.
The application firmware (app.bin) will include a read-only VFS area in FAT format, which will be mounted during the startup process.
Prepare the read-only files and convert them into a FAT-formatted bin file. Refer to section VFS Bin File Generation for the method.
Name the bin file
fatfs.binand place it in{SDK}\build_RTLxxx.Refer to Configuring Project (menuconfig) to navigate to the
CONFIG File Systemconfiguration, then selectRead-only FATFS image within APP image. This option cannot be selected together withFATFSinVFS2 partition.Rebuild the application firmware.
The application firmware (app.bin) will include a read-only VFS area in FAT format, which will be mounted during the startup process.
Prepare the read-only files and convert them into a FAT-formatted bin file. Refer to section VFS Bin File Generation for the method.
Name the bin file
fatfs.binand place it in{SDK}\build_RTLxxx.Refer to Configuring Project (menuconfig) to navigate to the
CONFIG File Systemconfiguration, then selectRead-only FATFS image within APP image. This option cannot be selected together withFATFSinVFS2 partition.Rebuild the application firmware.
The application firmware (app.bin) will include a read-only VFS area in FAT format, which will be mounted during the startup process.
Prepare the read-only files and convert them into a FAT-formatted bin file. Refer to section VFS Bin File Generation for the method.
Name the bin file
fatfs.binand place it in{SDK}\build_RTL8730E.Refer to Configuring Project (menuconfig) to navigate to the
CONFIG File Systemconfiguration, then selectRead-only FATFS image within APP image. This option cannot be selected together withFATFSinVFS2 partition.Rebuild the application firmware.
The application firmware (app.bin) will include a read-only VFS area in FAT format, which will be mounted during the startup process.
Prepare the read-only files and convert them into a FAT-formatted bin file. Refer to section VFS Bin File Generation for the method.
Name the bin file
fatfs.binand place it in{SDK}\build_RTL8721F.Refer to Configuring Project (menuconfig) to navigate to the
CONFIG File Systemconfiguration, then selectRead-only FATFS image within APP image. This option cannot be selected together withFATFSinVFS2 partition.Rebuild the application firmware.
The application firmware (app.bin) will include a read-only VFS area in FAT format, which will be mounted during the startup process.
Prepare the read-only files and convert them into a FAT-formatted bin file. Refer to section VFS Bin File Generation for the method.
Name the bin file
fatfs.binand place it in{SDK}\build_RTL8720F.Refer to Configuring Project (menuconfig) to navigate to the
CONFIG File Systemconfiguration, then selectRead-only FATFS image within APP image. This option cannot be selected together withFATFSinVFS2 partition.Rebuild the application firmware.
The application firmware (app.bin) will include a read-only VFS area in FAT format, which will be mounted during the startup process.
Prepare the read-only files and convert them into a FAT-formatted bin file. Refer to section VFS Bin File Generation for the method.
Name the bin file
fatfs.binand place it in{SDK}\build_RTL8735C.Refer to Configuring Project (menuconfig) to navigate to the
CONFIG File Systemconfiguration, then selectRead-only FATFS image within APP image. This option cannot be selected together withFATFSinVFS2 partition.Rebuild the application firmware.
The application firmware (app.bin) will include a read-only VFS area in FAT format, which will be mounted during the startup process.
After startup, the log File System Init Success indicates that VFS initialization has finished. If the FAT file system fails to mount, an error log such as fat1 init fail is printed.
When subsequent updates to the FAT file system content are required, they can be updated alongside image via OTA .
For the file usage method, refer to Section Common File Operation or Key-Value Operation.
Note
To optimize the effective space utilization of the file system, only FAT format is currently supported.
To prevent damage to the OTA area, the firmware size containing the file system cannot exceed the size of the Application Partitions in the Flash Layout Modification Guide .
This function operates exclusively on flash memory.
Common File Operation
The common file operation interfaces used in VFS are listed below:
API |
Parameter |
Description |
|---|---|---|
fopen |
|
Open the filename pointed to, by filename using the given mode |
fclose |
|
Close the stream |
fread |
|
Read data from the given stream by ptr into the array pointed to |
fwrite |
|
Write data from the array pointed to by ptr to the given stream |
fseek |
|
Set the file position of the stream to the given offset |
rewind |
|
Set the file position to the beginning of the file of the given stream |
fgetpos |
|
Get the current file position of the stream and writes it to pos |
fsetpos |
|
Set the file position of the given stream to the given position |
fflush |
|
Flush the output buffer of a stream |
remove |
|
Delete the given filename so that it is no longer accessible |
rename |
|
Cause the filename referred to from old_filename to new_filename |
feof |
|
Test the end-of-file indicator for the given stream |
ferror |
|
Test the error indicator for the given stream |
ftell |
|
Return the current file position of the given stream |
ftruncate |
|
Truncate a file to a specified length |
opendir |
|
Open a directory |
readdir |
|
Read a directory |
closedir |
|
Close a directory |
rmdir |
|
Remove a directory |
mkdir |
|
Make a directory |
access |
|
Determine accessibility of a file |
stat |
|
Get file status |
Users can refer to VFS example to test how common file operations work. Test logs should be like below:
[example_vfs_thread] fwrite succeeded !!!
[example_vfs_thread] fread succeeded !!!
[example_vfs_thread] remove file succeeded !!!
Key-Value Operation
Simple KV interfaces are also provided for users. All KV APIs are placed in kv.c.
Users can refer to KV example to test how KV APIs work. Test logs should be like below:
rt_kv_set success, write 28 letters.
rt_kv_get success, read 28 letters.
rt_kv_delete success.
Note
KV operations occur in the KV directory under the VFS1 partition, where keys correspond to filenames and values to file contents.
KV is bound to the VFS1 target and depends on VFS1 using LittleFS. The prefix can be queried with vfs_get_prefix(VFS_TARGET_FLASH_VFS1).
FTL
Based on VFS, FTL interface layer is provided:
The Flash Translation Layer (FTL) is a system software layer specifically designed for Bluetooth (BT) to optimize the usability of Flash memory as data storage medium.
By translating upper-layer logical address requests into VFS file operations, FTL enhances Flash memory’s read/write performance while inheriting VFS’ advanced management features like wear leveling and garbage collection.
Architecture Diagram:
Standard BLE Applications
Bluetooth Host protocol stack defaults to 344 bytes FTL allocation.
The SDK maintains 4 BLE connections by default (216 bytes FTL per connection), adjustable via RTK_BLE_GAP_MAX_LINKS.
BLE Mesh Applications
Requires additional FTL space beyond Host stack and BLE connections:
Mesh device
Base protocol stack consumes 1752 bytes FTL.
Default supports 20-node network (16 bytes RPL space per node), adjustable via bt_mesh_rpl_num.
Mesh Provisioner
Base protocol stack consumes 1280 bytes FTL.
Default supports 20-node provisioning (20 bytes Device Key + 16 bytes RPL per node), adjustable via bt_mesh_dev_key_num and bt_mesh_rpl_num respectively.
General Configuration
Default pre-allocates 3000 bytes FTL.
When calculated requirements exceed baseline, explicitly configure via bt_mesh_flash_size.
The table below lists three typical BLE application scenarios and their corresponding FTL size requirements:
Scenario |
Calculation |
FTL size |
|---|---|---|
3 BLE connections, No Mesh usage |
344 + 216 * 3 = 992 |
992 bytes |
4 BLE connections, Mesh device (50 nodes in network) |
Since 1752 + 16 * 50 <= 3000, 344 + 216 * 4 + 3000 = 4208 |
4208 bytes |
5 BLE connections, Mesh provisioner (50 provisionable nodes) |
Since 1280 + (20 + 16) * 50 > 3000, 344 + 216 * 5 + 1280 + (20 + 16) * 50 = 4504 |
4504 bytes |
Note
FTL module auto-enables with BT activation. Refer to BT Application Development Guide for details.
FTL data is stored in the VFS1 partition and depends on VFS1 using LittleFS.
Code Conversion
The conversion between Unicode and other codes is not supported on FatFS by default.
Modify the macro FF_CODE_PAGE in ffconf.h to enable the code conversion function, where FF_CODE_PAGE should be chosen as code page number which is desired.
#define FF_CODE_PAGE 999
/* This option specifies the OEM code page to be used on the target system.
/ Incorrect code page setting can cause a file open failure.
/ 437 - U.S.
/ 720 - Arabic
/ 737 - Greek
/ 771 - KBL
/ 775 - Baltic
/ 850 - Latin 1
/ 852 - Latin 2
/ 855 - Cyrillic
/ 857 - Turkish
/ 860 - Portuguese
/ 861 - Icelandic
/ 862 - Hebrew
/ 863 - Canadian French
/ 864 - Arabic
/ 865 - Nordic
/ 866 - Russian
/ 869 - Greek 2
/ 932 - Japanese (DBCS)
/ 936 - Simplified Chinese (DBCS)
/ 949 - Korean (DBCS)
/ 950 - Traditional Chinese (DBCS)
/ 999 - Realtek defined for code size
/ 0 - Include all code pages above and configured by f_setcp()
*/
VFS Encryption
For special storage security needs, users can configure encryption and decryption interfaces of VFS.
This functionality requires users to prepare encryption/decryption functions and keys in advance by referring to the VFS encryption/decryption example.
After successfully running the example program, the test logs should include the following key information:
[example_vfs_encrypt_thread] fwrite succeeded !!!
[example_vfs_encrypt_thread] fread succeeded !!!
[example_vfs_encrypt_thread] remove file succeeded !!!
Note
Plaintext will be padded according to the length of grouped data. It will take more cost of memory space if using vfs encryption. The encryption callbacks take effect per volume: setting them through any prefix of a volume applies to all prefixes of the same volume, including aliases registered later, so the same volume never mixes ciphertext and plaintext. Different volumes (for example VFS1 and an SD card) can use different keys, or one encrypted and one plaintext. Setting the callbacks again on a volume that already has them is ignored.
VFS Bin File Generation
If data needs to be placed in the Flash in advance, VFS bin file can be generated on PC. After generating the bin file, it should be downloaded to VFS region according to the Flash layout.
Prepare a needed object folder including files before generating LittleFS bin files. For example:
ls -R test
Output Example:
test:
AUDIO KV
test/AUDIO
audio_test.mp3
test/KV
kv_test.txt
AUDIO and KV directories will be LittleFS directory in the Flash. The test directory is equivalent to the root directory.
Use the vfs.py to generate the LittleFS binary file,
cd {SDK}/tools/image_scripts
python vfs.py -t LITTLEFS -s 4096 -c 32 -dir test -out littlefs.bin
Where:
- -s:
block size
- -c:
block count
- -dir:
source directory
- -out:
output LittleFS bin file name
Output Example:
littlefs.bin has been successfully generated.
args:
├─ type: LITTLEFS
├─ block_size: 4096
├─ block_count: 32
├─ image_size: 131072
├─ source_directory: test
└─ output_image: littlefs.bin
Note
-s 4096 is the default configuration of LittleFS. Users must align this value with the block_size defined in the
lfs_configstruct within littlefs_adapter.c.-c 32 should be configured based on the VFS partition size described in VFS on Flash, where the partition size equals block size × block count.
By default the tool estimates the space the source directory needs and automatically increases
block_countwhen the value given by-cis too small, printingWarning: Adjusting block count from X to Y. As a result the generated image size may be larger thanblock_size × -c. Add-f/--fixed_block_countto disable this and build strictly with the given-c(the command fails if the source directory does not fit).
The following options extend step 2 for specific scenarios:
NAND (LBM) target. For a raw NAND partition, pass
--littlefs-target nandinstead of-s/-c. The tool builds the LittleFS image and wraps it into an LBM physical NAND image in one step:python vfs.py -t LITTLEFS --littlefs-target nand --size 32M --page-size 2048 --block-pages 64 -dir test -out littlefs_nand.bin
Where
--sizeis the VFS partition size (e.g.32M/0x2000000, and must match the firmware VFS partition),--page-sizeis the NAND page size (default 2048), and--block-pagesis the NAND pages per erase block (default 64).-s/-care ignored in this mode because the block size and count are derived from the NAND geometry.Read-only image inside the app image (rolfs). Add
--rolfs-header(LITTLEFS only) to prepend a 32-byte header so the runtime VFS can locate the payload inside the app image and mount it read-only at therolfs:prefix. When-cis not given, the image is packed to the minimum size that fits the source directory.
Download the image to the Flash.
Refer to
Image Download
to download the binary file into Flash. The start address of Image Tool should be StartAddr of VFS Flash region mentioned in Section VFS on Flash. The end address of Image Tool should be it’s EndAddr + 1.
(Optional) Unpack an image back to a directory for inspection.
python vfs.py -t LITTLEFS -u out_dir -dir littlefs.bin
Here -dir is the input image file and -u is the output directory. Add -s if the image block size is not the 4096 default.
Prepare a needed object folder including files before generating FatFS bin files. For example:
ls -R test
Output Example:
test:
AUDIO KV
test/AUDIO
audio_test.mp3
test/KV
kv_test.txt
AUDIO and KV directories will be FatFS directory in the Flash. The test directory is equivalent to the root directory.
Use the vfs.py to generate the FatFS binary file,
cd {SDK}/tools/image_scripts
python vfs.py -t FATFS -s 512 -c 256 -dir test -out fatfs.bin
Where:
- -s:
sector size
- -c:
sector count
- -dir:
source directory
- -out:
output FatFS bin file name
Output Example:
fatfs.bin has been successfully generated.
args:
├─ type: FATFS
├─ sector_size: 512
├─ sector_count: 256
├─ image_size: 131072
├─ source_directory: test
└─ output_image: fatfs.bin
Note
-s 512 is the default configuration of FatFS which can be omitted.
-c 256 should be configured based on the VFS partition size described in VFS on Flash, where the partition size equals sector size × sector count.
By default the tool estimates the space the source directory needs and automatically increases
sector_countwhen the value given by-cis too small, printingWarning: Adjusting block count from X to Y. As a result the generated image size may be larger thansector_size × -c. Add-f/--fixed_block_countto disable this and build strictly with the given-c(the command fails if the source directory does not fit).
Download the image to the Flash.
Refer to
Image Download
to download the binary file into Flash. The start address of Image Tool should be StartAddr of VFS Flash region mentioned in Section VFS on Flash. The end address of Image Tool should be it’s EndAddr + 1.
(Optional) Unpack an image back to a directory for inspection.
python vfs.py -t FATFS -u out_dir -dir fatfs.bin
Here -dir is the input image file and -u is the output directory.
VFS on Second Flash
Second Flash supports two NOR Flash expansion methods, as compared below:
Type |
data flash |
normal external flash |
Configuration option |
|
|
Flash read/write performance |
High |
Medium |
PSRAM enable support |
No |
Yes |
SPI pin assignment location |
/ |
|
External SPI NAND Flash
Both expansion methods above use a NOR Flash. Besides them, an external SPI NAND Flash can be connected to the SPI master bus to expand the storage space.
Navigate to CONFIG File System via Configuring Project (menuconfig), select Second flash → Second flash is external SPI NAND (else SPI NOR), and choose LittleFS in File system on second flash. Then register the file system as usual:
vfs_target_register(prefix, VFS_TARGET_SECOND_FLASH, VFS_RW);
Both file systems run on top of the same logical block mapping layer as the on-chip SPI NAND, so bad blocks are masked in the same way and the file system only sees continuously numbered good blocks.
The SPI pins are assigned in vfs_second_nand_spi.h. Modify that header if the board routes them elsewhere.
Note
The target used for registration is always
VFS_TARGET_SECOND_FLASH. The file system is determined byFile system on second flashin menuconfig, not by a registration parameter.FatFS is not supported on the external SPI NAND.
A page size of 2 KB and 64 pages (128 KB) per erase block are verified. Other geometries are read from the ONFI parameter page during initialization, and require no code change.
The first mount on a blank chip scans every physical block (two page reads per block), which takes a few seconds on a 1 Gbit part. This is expected and happens only once.