High-level formatting

From SEGGER Knowledge Base
Jump to navigation Jump to search

High-level formatting

General information

The high-level formatting is a file system operation that can be used to initialize the structure of the file system on a storage device. Typically, this operation has to be performed only once on a lifetime of a storage device before writing any data to the storage device. The high-level format operation can also be performed on a storage device that is already formatted and that contains files and directories. However, all the files and directories will be lost after the high-level format operation.

The reason why this operation is called high-level is to differentiate it from the other type of format operation that is performed by the NAND and NOR drivers and is called low-level formatting. High-level indicates that the operation is performed in the file system layer that is located above the driver layer in the emFile software stack where the low-level formatting operation is implemented.

Theory of operation

An application can perform a high-level format operation via the FS_Format() API function. This API function can be used to perform a high-level format for any of the file system types supported by emFile. The type of file system used by the high-level format operation is determined the file system support enabled in emFile at compile time via FS_SUPPORT_FAT, FS_SUPPORT_EFS or FS_SUPPORT_EXFAT configuration defines. If emFile is configured to support more than one file system types, then the type of file system used by high-level format operation has to be specified by calling the FS_SetFSType() API function.

In addition, emFile comes with specialized API functions for formatting a storage device as FAT and exFAT. These API functions format a storage device according to the specification SD Association. The FS_FAT_FormatSD API function can be used to format a storage device as FA12, FAT16 or FAT32 while the FS_EXFAT_FormatSD can be used to format a storage device as exFAT.

The high-level formatting operation is initializing at least the following regions of a file system:

  • Boot sector
  • Allocation table
  • Root directory

The format API functions may initialize other regions of the file system depending on the file system type.

The boot sector stores basic information about the structure of the file system such as the file system type, the size of an allocation unit called cluster, the total number of clusters and so on. The allocation table stores information about what clusters are allocated and what clusters are belonging to the same file or directory. The root directory is the main entry point for the access to the files and directories stored on the file system. The high-level format information marks all the clusters in the allocation table as not being in use and creates an empty root directory.

Cluster size

A cluster is the minimum storage space that the file system is able to allocate at once for a file or directory and it consists of one or more consecutive logical sectors. The size of a cluster has an influence on the file system performance and on the storage space utilization. Larger clusters provide a better performance because of the reduced number of accesses to the allocation table the file system has to perform for the same amount of accessed data. On the other hand smaller clusters make better use of the storage space if the application works with files of a relatively small size. The high-level formatting API functions of the file system are implemented to use a default cluster size that provides a balance between these.

By default, the size of the cluster is calculated based on the logical sector size and on the total number of logical sectors available on a storage device. As an option, the FS_Format() API function is able to format a storage device using a specified cluster size.

Performance

The time it takes the high-level format operation to complete depends mainly on the capacity of the storage device and secondly on the cluster size. The following table lists the performance measurement results when formatting a 4 GB SD card and an 128 MB NAND flash device as FAT. The test was performed on a SEGGER Flasher PRO XL hardware. The rows marked in bold are the format parameters that are used by default by the FS_Format() API function.

SD card (4 GB, 512-byte logical sector)
Function File system type Num. clusters Cluster size [bytes] Num. sectors written Time [ms]
FS_FAT_FormatSD() FAT32 121216 32768 1968 827
FS_Format() FAT32 7652704 512 119582 26510
FS_Format() FAT32 3856720 1024 60271 13649
FS_Format() FAT32 1935952 2048 30261 8264
FS_Format() FAT32 969874 4096 15171 5710
FS_Format() FAT32 485411 8192 7609 2844
FS_Format() FAT32 242824 16384 3835 1083
FS_Format() FAT32 121441 32768 1969 584
FS_Format() FAT16 60732 65536 512 582
NAND flash (128 MB, 2048-byte logical sector)
Function File system type Num. clusters Cluster size [bytes] Num. sectors written Time [ms]
FS_FAT_FormatSD() FAT12 1947 65536 17 174
FS_Format() FAT16 62239 2048 134 583
FS_Format() FAT16 31149 4096 74 758
FS_Format() FAT16 15582 8192 44 450
FS_Format() FAT16 7793 16384 28 189
FS_Format() FAT12 3897 32768 18 413
FS_Format() FAT12 1948 65536 16 162