diff --git a/documents/00index_e.html b/documents/00index_e.html index b3b8adb..c385601 100644 --- a/documents/00index_e.html +++ b/documents/00index_e.html @@ -17,7 +17,7 @@
layer -

FatFs is a generic FAT/exFAT filesystem module for small embedded systems. The FatFs module is written in compliance with ANSI C (C89) and completely separated from the disk I/O layer. Therefore it is independent of the platform. It can be incorporated into small microcontrollers with limited resource, such as 8051, PIC, AVR, ARM, Z80, RX and etc. Also Petit FatFs module for tiny microcontrollers is available here.

+

FatFs is a generic FAT/exFAT filesystem module for small embedded systems. The FatFs module is written in compliance with ANSI C (C89) and completely separated from the disk control layer. Therefore it is independent of the platforms and storage devices. It can be incorporated into small microcontrollers with limited resource, such as 8051, PIC, AVR, ARM, Z80, RX and etc. Also Petit FatFs module for tiny microcontrollers is available here.

Features

+ +
+

Unicode API

+

By default, FatFs uses ANSI/OEM code set on the API even in LFN configuration. FatFs can also switch the character encoding on the API to Unicode by configuration option FF_LFN_UNICODE. This means that FatFs is compliant with the full featured LFN specification. The data type TCHAR specifies path name strings on the API is an alias of either char(ANSI/OEM or UTF-8), WCHAR(UTF-16) or DWORD(UTF-32) depends on that option. For more information, refer to the description in the file name.

+

Note that setting of code page, FF_CODE_PAGE, has actually no meaning when FatFs is configured for the Unicode API. It should be set 437 to reduce the module size. However it still affects code conversion of string I/O functions when FF_STRF_ENCODE == 0, and also backward compatibility with legacy systems. In this case, the code page may need to be configured properly if it is considered a problem.

+
+ +
+

exFAT Filesystem

+

The exFAT (Microsoft's Extended File Allocation Table) filesystem is a succession of the FAT/FAT32 filesystem which has been widely used in embedded systems, consumer devices and portable storage media. It is adopted by SDA (SD Association) as the filesystem for SDXC card, 64 GB and larger, and they are being shipped with this format. Therefore the exFAT is one of the standard filesystems for removable media as well as FAT. The exFAT filesystem allows the file size beyond the 4 GB limit what FAT filesystem allows up to and some filesystem overhead, especially cluster allocation delay, are reduced as well. These features allow to record the large data without dividing into some files and improve the write throughput to the file.

+

Note that the exFAT filesystem is a patent of Microsoft Corporation. The exFAT feature of FatFs is an implementation based on US. Pat. App. Pub. No. 2009/0164440 A1. FatFs module can switch the exFAT on or off by a configuration option, FF_FS_EXFAT. When enable the exFAT for the commercial products, a license by Microsoft will be needed depends on the final destination of the products.

+

Remarks: Enabling exFAT discards C89 compatibility and it wants C99 because of need for 64-bit integer type.

+
+ +
+

64-bit LBA

+

LBA (Logical Block Addressing) is an addressing method to specify the location of data block, called sector, on the storage media. It is a simple linear address beginning from 0 as the first sector, 1 as the second sector and so on. The host system does not need to consider how the data block is located and managed in the storage device. FatFs supports only LBA for the media access. 32-bit LBA is a common size in the most LBA scheme. It can address up to 232 sectors, 2 TB in 512 bytes/sector. When a storage device larger than 2 TB is used, larger sector size or 64-bit LBA will be needed to address the entire sectors of the storage device.

+

By default, FatFs works in 32-bit LBA for media access. FatFs can also switch it to 64-bit LBA by a configuration option FF_LBA64. It also enables GPT (GUID Partition Table) for partiotion management on the storage device. For further information about GPT, refer to f_mkfs and f_fdisk function.

+
+ +
+

Re-entrancy

+

The file operations of two tasks to the different volumes each other is always re-entrant and it can work concurrently without any mutual exclusion regardless of the configurations except when LFN is enabled with static working buffer (FF_USE_LFN = 1).

+

The file operations of two tasks to the same volume is not thread-safe by default. FatFs can also be configured to make it thread-safe by an option FF_FS_REENTRANT. When a file function is called while the volume is being accessed by another task, the file function to the volume will be suspended until that task leaves the file function. If the wait time exceeded a period defined by FF_TIMEOUT, the file function will abort with FR_TIMEOUT. The timeout feature might not be supported on the some OSs. To enable this feature, OS dependent synchronization control functions, ff_mutex_create/ff_mutex_delete/ff_mutex_take/ff_mutex_give, need to be added to the project. There is an example code in the ffsystem.c for some OSs.

+

Note that there is an exception on the re-entrancy for f_mount and f_mkfs function. You will know why it is. These volume management functions are always not thread-safe to the volume being processed. When use these functions, other tasks need to avoid to access the corresponding volume.

+
+ + + + + + + + +
FunctionCase 1Case 2Case 3
disk_statusYesYesYes(*)
disk_initializeNoYesYes(*)
disk_readNoYesYes(*)
disk_writeNoYesYes(*)
disk_ioctlNoYesYes(*)
get_fattimeNoYesYes
+ +Case 1: Same volume.
+Case 2: Different volume on the same drive.
+Case 3: Different volume on the different drive.
+(*) In only different drive number. +
+
+

Remarks: This section describes on the re-entrancy of the FatFs module itself. The FF_FS_REENTRANT option enables only exclusive use of each filesystem objects and FatFs does not that prevent to re-enter the storage device control functions. Thus the device control layer needs to be always thread-safe when FatFs API is re-entered for different volumes. Right table shows which control function can be re-entered when FatFs API is re-entered on some conditions.

+
+ +
+

Duplicated File Open

+

FatFs module does not support the read/write collision control of duplicated open to a file. The duplicated open is permitted only when each of open method to a file is read mode. The duplicated open with one or more write mode to a file is always prohibited, and also open file must not be renamed or deleted. A violation of these rules can cause data collaption.

+

The file lock control can be enabled by FF_FS_LOCK option. The value of option defines the number of open objects to manage simultaneously. In this case, if any opening, renaming or removing against the file shareing rule that described above is attempted, the file function will be rejected with FR_LOCKED. If number of open objects, files and sub-directories, is equal to FF_FS_LOCK, an extra f_open/f_opendir function will fail with FR_TOO_MANY_OPEN_FILES.

+
+ +
+

Performance Effective File Access

+

For good read/write throughput on the small embedded systems with limited size of memory, application programmer should consider what process is done in the FatFs module. The file data on the volume is transferred in following sequence by f_read function.

+

Figure 1. Sector unaligned read (short)
+ +

+

Figure 2. Sector unaligned read (long)
+ +

+

Figure 3. Fully sector aligned read
+ +

+

The file I/O buffer is a sector buffer to read/write a part of data on the sector. The sector buffer is either file private sector buffer on each file object or shared sector buffer in the filesystem object. The buffer configuration option FF_FS_TINY determins which sector buffer is used for the file data transfer. When tiny buffer configuration (1) is selected, data memory consumption is reduced FF_MAX_SS bytes each file object. In this case, FatFs module uses only a sector buffer in the filesystem object for file data transfer and FAT/directory access. The disadvantage of the tiny buffer configuration is: the FAT data cached in the sector buffer will be lost by file data transfer and it must be reloaded at every cluster boundary. However it will be suitable for most application from view point of the decent performance and low memory comsumption.

+

Figure 1 shows that a partial sector, sector unaligned part of the file, is transferred via the file I/O buffer. At long data transfer shown in Figure 2, middle of transfer data that covers one or more sector is transferred to the application buffer directly. Figure 3 shows that the case of entier transfer data is aligned to the sector boundary. In this case, file I/O buffer is not used. On the direct transfer, the maximum extent of sectors are read with disk_read function at a time but the multiple sector transfer is divided at cluster boundary even if it is contiguous.

+

Therefore taking effort to sector aligned read/write accesss eliminates buffered data transfer and the read/write performance will be improved. Besides the effect, cached FAT data will not be flushed by file data transfer at the tiny configuration, so that it can achieve same performance as non-tiny configuration with small memory footprint.

+
+ +
+

Considerations on Flash Memory Media

+

To maximize the write performance of flash memory media, such as SDC, CFC and U Disk, it must be controlled in consideration of its characteristitcs.

+

Using Mutiple-Sector Write

+
+Figure 6. Comparison between Multiple/Single Sector Write
+fig.6 +
+

The write throughput of the flash memory media becomes the worst at single sector write transaction. The write throughput increases as the number of sectors per a write transaction as shown in Figure 6. This effect more appers at faster interface speed and the performance ratio often becomes grater than ten. This result is clearly explaining how fast is multiple block write (W:16K, 32 sectors) than single block write (W:100, 1 sector), and also larger card tends to be slow at single block write. Number of write transactions also affects life time of the flash memory media. When compared at same amount of write data, the single sector write in Figure 6 above wears flash memory media 16 times more than multiple sector write in Figure 6 below. Single sector write is pretty pain for the flash memory media.

+

Therefore the application program should write the data in large block as possible. The ideal write chunk size and alighment is size of sector, and size of cluster is the best. Of course all layers between the application and the storage device must have consideration on multiple sector write, however most of open-source memory card drivers lack it. Do not split a multiple sector write request into single sector write transactions or the write throughput gets poor. Note that FatFs module and its sample disk drivers supprt multiple sector read/write operation.

+

Forcing Memory Erase

+

When remove a file with f_unlink function, the data clusters occupied by the file are marked 'free' on the FAT. But the data sectors containing the file data are not that applied any process, so that the file data left occupies a part of the flash memory array as 'live block'. If the file data can be erased on removing the file, those data blocks will be turned into the free block pool. This may skip internal block erase operation to the data block on next write operation. As the result the write performance might be improved. FatFs can manage this function by setting FF_USE_TRIM to 1. Note that because this effect is from an expectation of internal process of the storage device, it is not that always effective. Most applications will not need this function. Also f_unlink function can take a time when remove a large file.

+
+ +
+

Critical Section

+

If a write operation to the FAT volume is interrupted due to an accidental failure, such as sudden blackout, wrong media removal and unrecoverable disk error, the FAT structure on the volume can be broken. Following images shows the critical section of the FatFs module.

+
+Figure 4. Long critical section
+fig.4 +
+
+Figure 5. Minimized critical section
+fig.5 +
+
+

An interruption in the red section can cause a cross link; as a result, the object being changed can be lost. If an interruption in the yellow section is occured, there is one or more possibility listed below.

+ +

Each case does not affect any file not opened in write mode. To minimize risk of data loss, the critical section can be minimized by minimizing the time that file is opened in write mode or using f_sync function as shown in Figure 5.

+
+ +
+

Various Usable Functions for FatFs Projects

+

These are examples of extended use of FatFs APIs. New item will be added when useful code example is found.

+
    +
  1. Open or Create File for Append (superseded by FA_OPEN_APPEND flag added at R0.12)
  2. +
  3. Delete Non-empty Sub-directory (for R0.12 and later)
  4. +
  5. Create Contiguous File (superseded by f_expand function added at R0.12)
  6. +
  7. Test if the File is Contiguous or Not
  8. +
  9. Compatibility Checker for Storage Device Control Module
  10. +
  11. Performance Checker for Storage Device Control Module
  12. +
  13. FAT Volume Image Creator (Pre-creating built-in FAT volume)
  14. +
  15. Virtual Drive Feature (refer to lpc176x/ in ffsample.zip)
  16. +
  17. Embedded Unicode String Utilities (OEMxxx→Unicode, Unicode→OEMxxx, Unicode→Unicode)
  18. +
+
+ +
+

About FatFs License

+

FatFs has being developped as a personal project of the author, ChaN. It is free from the code anyone else wrote at current release. Following code block shows a copy of the FatFs license document that included in the source files.

+
+/*----------------------------------------------------------------------------/
+/  FatFs - Generic FAT Filesystem Module  Rx.xx                               /
+/-----------------------------------------------------------------------------/
+/
+/ Copyright (C) 20xx, ChaN, all right reserved.
+/
+/ FatFs module is an open source software. Redistribution and use of FatFs in
+/ source and binary forms, with or without modification, are permitted provided
+/ that the following condition is met:
+/
+/ 1. Redistributions of source code must retain the above copyright notice,
+/    this condition and the following disclaimer.
+/
+/ This software is provided by the copyright holder and contributors "AS IS"
+/ and any warranties related to this software are DISCLAIMED.
+/ The copyright owner or contributors be NOT LIABLE for any damages caused
+/ by use of this software.
+/----------------------------------------------------------------------------*/
+
+

Therefore FatFs license is one of the BSD-style licenses but there is a significant feature. FatFs is mainly intended for embedded systems. In order to extend the usability for commercial products, the redistributions of FatFs in binary form, such as embedded code, binary library and any forms without source code, does not need to include about FatFs in the documentations. This is equivalent to the 1-clause BSD license. Of course FatFs is compatible with the most of open source software licenses includes GNU GPL. When you redistribute the FatFs source code with any changes or create a fork, the license can also be changed to GNU GPL, BSD-style license or any open source software license that compatible with FatFs license.

+
+ + +

Return

+ + diff --git a/documents/doc/chdir.html b/documents/doc/chdir.html index 93e116c..a91ab0f 100644 --- a/documents/doc/chdir.html +++ b/documents/doc/chdir.html @@ -3,8 +3,6 @@ - - FatFs - f_chdir @@ -83,6 +81,7 @@ FRESULT f_chdir (

f_chdrive, f_getcwd

+

Return

diff --git a/documents/doc/chdrive.html b/documents/doc/chdrive.html index 4dfc7d8..fd821c4 100644 --- a/documents/doc/chdrive.html +++ b/documents/doc/chdrive.html @@ -3,8 +3,6 @@ - - FatFs - f_chdrive @@ -65,6 +63,7 @@ FRESULT f_chdrive (

f_chdir, f_getcwd

+

Return

diff --git a/documents/doc/chmod.html b/documents/doc/chmod.html index 91e520f..ebc46e8 100644 --- a/documents/doc/chmod.html +++ b/documents/doc/chmod.html @@ -3,8 +3,6 @@ - - FatFs - f_chmod @@ -84,6 +82,7 @@ FRESULT f_chmod ( +

Return

diff --git a/documents/doc/close.html b/documents/doc/close.html index f4cd2df..3fe9827 100644 --- a/documents/doc/close.html +++ b/documents/doc/close.html @@ -3,8 +3,6 @@ - - FatFs - f_close @@ -60,6 +58,7 @@ FRESULT f_close (

f_open, f_read, f_write, f_sync, FIL, FATFS

+

Return

diff --git a/documents/doc/closedir.html b/documents/doc/closedir.html index 3f5df57..bf4febe 100644 --- a/documents/doc/closedir.html +++ b/documents/doc/closedir.html @@ -3,8 +3,6 @@ - - FatFs - f_closedir @@ -59,6 +57,7 @@ FRESULT f_closedir (

f_opendir, f_readdir, DIR

+

Return

diff --git a/documents/doc/config.html b/documents/doc/config.html index 788e075..198a199 100644 --- a/documents/doc/config.html +++ b/documents/doc/config.html @@ -3,8 +3,6 @@ - - FatFs - Configuration Options @@ -105,29 +103,29 @@

Disable (0) or Enable (1) f_forward function.

FF_USE_STRFUNC

-

This option switches string functions, f_gets, f_putc, f_puts and f_printf. These functions are equivalents of regular string stream I/O functions in POSIX. If sprintf is available and code conversion is not needed, f_write with sprintf will be efficient in code size and performance rather than f_printf. When enable this feature, stdarg.h is included in ff.c.

+

This option switches string I/O functions, f_gets, f_putc, f_puts and f_printf. These functions are the equivalents of regular string stream I/O functions in POSIX. If sprintf is available and code conversion is not needed, f_write with sprintf will be efficient in code size and performance better than f_printf. When enable this feature, stdarg.h is included in ff.c.

- - + +
ValueDescription
0Disable string functions.
1Enable string functions without LF-CRLF conversion.
2Enable string functions with LF-CRLF conversion.
1Enable string functions without LF - CRLF conversion.
2Enable string functions with LF - CRLF conversion.

This option switches support for long long integer argument in f_printf.

-

Disable (0) or Enable (1). When enable this feature, C standard needs to be C99 or later.

+

Disable (0) or Enable (1). When enable this feature, C standard needs to be C99 or later. This option has no effect when FF_USE_STRFUNC == 0.

-

This option switches support for floating point argument in f_printf. When enable this feature, C standard needs to be C99 or later and math.h is included in ff.c.

+

This option switches support for floating point argument in f_printf. When enable this feature, C standard needs to be C99 or later and math.h is included in ff.c. This option has no effect when FF_USE_STRFUNC == 0.

- +
ValueDescription
0Disable floating point argument.
1Enable floating point argument in type 'f', 'e' and 'E'.
2Enable it with decimal separator ',' instead of '.'.
2Same as 1 but with decimal separator ',' instead of '.' in output string.

FF_STRF_ENCODE

-

When character encoding on the API is Unicode (FF_LFN_UNICODE >= 1), string I/O functions enabled by FF_USE_STRFUNC convert the character encoding in it. This option defines the assumption of character encoding on the file to be read/written via those functions. When LFN is not enabled or FF_LFN_UNICODE == 0, the string functions work without any code conversion and this option has no effect.

+

When the character encoding on the API is Unicode (FF_LFN_UNICODE >= 1), string I/O functions enabled by FF_USE_STRFUNC convert the character encoding in it. This option defines the assumption of character encoding on the file to be read/written via the string I/O functions. When LFN is not enabled or FF_LFN_UNICODE == 0, the string I/O functions work without any code conversion and this option has no effect.

@@ -228,9 +226,9 @@

This option switches the support for string volume ID. When arbitrary string for the volume ID is enabled for the drive prefix, also pre-defined strings by FF_VOLUME_STRS or user defined strings can be used as drive prefix in the path name. Numeric drive number is always valid regardless of this option, and also either format of drive prefix can be enabled by this option.

ValueCharacter encoding on the file
0ANSI/OEM in current code page
- - - + + +
ValueDescriptionExample
0Only DOS/Windows style drive prefix in numeric ID can be used.1:/filename
1Also DOS/Windows style drive prefix in string ID can be used.flash:/filename
2Also Unix style drive prefix in string ID can be used./flash/filename
0DOS/Windows style drive prefix in numeric ID.1:/filename
10 + DOS/Windows style drive prefix in arbitry string ID.flash:/filename
20 + Unix style drive prefix in arbitry string ID./flash/filename

FF_VOLUME_STRS

@@ -241,7 +239,7 @@ const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sd","usb"};

FF_MULTI_PARTITION

-

Disable (0) or Enable (1). This option switches multi-partition function. By default (0), each logical drive number is bound to the same physical drive number and only a volume in the physical drive is mounted. When enabled, each logical drive is bound to the partition on the physical drive listed in the user defined partition resolution table VolToPart[]. Also f_fdisk funciton will be available. For more information, read here.

+

This option switches multi-partition featuer. By default (0), each logical drive number is bound to the same physical drive number and only one volume found in the physical drive is mounted. When it is enabled (1), each logical drive is bound to the specific partition listed in the user defined partition resolution table VolToPart[]. Also f_fdisk funciton is available to create the arbitrary partitions on the physical drive. For more information, read here.

FF_MIN_SS, FF_MAX_SS

This set of options defines the extent of sector size used for the low level disk I/O interface, disk_read and disk_write function. Valid values are 512, 1024, 2048 and 4096. FF_MIN_SS defines minimum sector size and FF_MAX_SS defines the maximum sector size. Always set both 512 for memory card and harddisk. But a larger value may be required for on-board flash memory and some type of optical media. When FF_MAX_SS > FF_MIN_SS, support of variable sector size is enabled and GET_SECTOR_SIZE command needs to be implemented to the disk_ioctl function.

@@ -299,6 +297,7 @@ const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sd","usb"}; +

Return

diff --git a/documents/doc/dinit.html b/documents/doc/dinit.html index c668c66..688118c 100644 --- a/documents/doc/dinit.html +++ b/documents/doc/dinit.html @@ -3,8 +3,6 @@ - - FatFs - disk_initialize @@ -41,6 +39,7 @@ DSTATUS disk_initialize (

Remarks: This function needs to be under control of FatFs module. Application program MUST NOT call this function while FatFs is in use, or FAT structure on the volume can be broken. To re-initialize the filesystem, use f_mount function instead.

+

Return

diff --git a/documents/doc/dioctl.html b/documents/doc/dioctl.html index fc5f24c..3c45bc9 100644 --- a/documents/doc/dioctl.html +++ b/documents/doc/dioctl.html @@ -3,8 +3,6 @@ - - FatFs - disk_ioctl @@ -58,9 +56,9 @@ DRESULT disk_ioctl ( Standard ioctl command used by FatFs CommandDescription CTRL_SYNCMakes sure that the device has finished pending write process. If the disk I/O layer or storage device has a write-back cache, the dirty cache data must be committed to the medium immediately. Nothing to do for this command if each write operation to the medium is completed in the disk_write function. -GET_SECTOR_COUNTRetrieves number of available sectors, the largest allowable LBA + 1, on the drive into the LBA_t variable that pointed by buff. This command is used by f_mkfs and f_fdisk function to determine the size of volume/partition to be created. It is required when FF_USE_MKFS == 1. -GET_SECTOR_SIZERetrieves sector size, minimum data unit for generic read/write, into the WORD variable that pointed by buff. Valid sector sizes are 512, 1024, 2048 and 4096. This command is required only if FF_MAX_SS > FF_MIN_SS. When FF_MAX_SS == FF_MIN_SS, this command will be never used and the read/write function must work in FF_MAX_SS bytes/sector. -GET_BLOCK_SIZERetrieves erase block size in unit of sector of the flash memory media into the DWORD variable that pointed by buff. The allowable value is 1 to 32768 in power of 2. Return 1 if the value is unknown or non flash memory media. This command is used by only f_mkfs function and it attempts to align data area on the suggested block boundary. It is required when FF_USE_MKFS == 1. Note that FatFs does not have FTL (flash translation layer). Either disk I/O layter or storage device must have an FTL in it. +GET_SECTOR_COUNTRetrieves number of available sectors (the largest allowable LBA + 1) on the drive into the LBA_t variable that pointed by buff. This command is used by f_mkfs and f_fdisk function to determine the size of volume/partition to be created. +GET_SECTOR_SIZERetrieves sector size (minimum data unit for generic read/write) into the WORD variable that pointed by buff. Valid sector sizes are 512, 1024, 2048 and 4096. This command is required only if FF_MAX_SS > FF_MIN_SS. When FF_MAX_SS == FF_MIN_SS, this command will never be used and the disk_read and disk_write function must work in FF_MAX_SS bytes/sector. +GET_BLOCK_SIZERetrieves erase block size in unit of sector of the flash memory media into the DWORD variable that pointed by buff. The allowable value is 1 to 32768 in power of 2. Return 1 if it is unknown or in non flash memory media. This command is used by f_mkfs function with block size not specified and it attempts to align the data area on the suggested block boundary. Note that FatFs does not have FTL (flash translation layer), so that either disk I/O layter or storage device must have an FTL in it. CTRL_TRIMInforms the disk I/O layter or the storage device that the data on the block of sectors is no longer needed and it can be erased. The sector block is specified in an LBA_t array {<Start LBA>, <End LBA>} that pointed by buff. This is an identical command to Trim of ATA device. Nothing to do for this command if this funcion is not supported or not a flash memory device. FatFs does not check the result code and the file function is not affected even if the sector block was not erased well. This command is called on remove a cluster chain and in the f_mkfs function. It is required when FF_USE_TRIM == 1. diff --git a/documents/doc/dread.html b/documents/doc/dread.html index 1be191b..c87c9db 100644 --- a/documents/doc/dread.html +++ b/documents/doc/dread.html @@ -3,8 +3,6 @@ - - FatFs - disk_read diff --git a/documents/doc/dstat.html b/documents/doc/dstat.html index a891489..404ca24 100644 --- a/documents/doc/dstat.html +++ b/documents/doc/dstat.html @@ -3,8 +3,6 @@ - - FatFs - disk_status @@ -43,6 +41,7 @@ DSTATUS disk_status ( +

Return

diff --git a/documents/doc/dwrite.html b/documents/doc/dwrite.html index fef5106..bc2ae0b 100644 --- a/documents/doc/dwrite.html +++ b/documents/doc/dwrite.html @@ -3,8 +3,6 @@ - - FatFs - disk_write @@ -60,7 +58,7 @@ DRESULT disk_write (

Description

The specified memory address is not that always aligned to word boundary because the argument is defined as BYTE*. For more information, refer to the description of disk_read function.

Generally, a multiple sector write request (count > 1) must not be split into single sector transactions to the storage device, or the file write throughput will be drastically decreased.

-

FatFs expects delayed write function of the disk control layer. The write operation to the media does not need to be completed when return from this function by what write operation is in progress or data is only stored into the write-back cache. But write data on the buff is invalid after return from this function. The write completion request is done by CTRL_SYNC command of disk_ioctl function. Therefore, if a delayed write function is implemented, the write throughput of the filesystem will be improved.

+

FatFs expects delayed write function in the disk control layer. The write operation to the media does not need to be completed when return from this function by what write operation is in progress or data is only stored into the write-back cache. The write data on the buff is invalid after return from this function. The write completion request is done by CTRL_SYNC command of disk_ioctl function. Therefore, if a delayed write function is implemented, the write throughput of the filesystem will be improved.

Remarks: Application program MUST NOT call this function, or FAT structure on the volume can be collapsed.

diff --git a/documents/doc/eof.html b/documents/doc/eof.html index efacfbf..549ae4e 100644 --- a/documents/doc/eof.html +++ b/documents/doc/eof.html @@ -3,8 +3,6 @@ - - FatFs - f_eof @@ -57,6 +55,7 @@ int f_eof (

f_open, f_lseek, FIL

+

Return

diff --git a/documents/doc/error.html b/documents/doc/error.html index bee9685..b122dc2 100644 --- a/documents/doc/error.html +++ b/documents/doc/error.html @@ -3,8 +3,6 @@ - - FatFs - f_error @@ -57,6 +55,7 @@ int f_error (

f_open, FIL

+

Return

diff --git a/documents/doc/expand.html b/documents/doc/expand.html index 7620ba9..86a0824 100644 --- a/documents/doc/expand.html +++ b/documents/doc/expand.html @@ -3,8 +3,6 @@ - - FatFs - f_expand @@ -28,7 +26,7 @@ FRESULT f_expand (

Parameters

fp
-
Pointer to the open file object.
+
Pointer to the open file object. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
fsz
Number of bytes in size to prepare or allocate for the file. The data type FSIZE_t is an alias of either DWORD(32-bit) or QWORD(64-bit) depends on the configuration option FF_FS_EXFAT.
opt
@@ -61,7 +59,7 @@ FRESULT f_expand (

When opt is 0, the function finds a contiguous data area and set it as suggested point for next allocation. The subsequent cluster allocation begins at top of the contiguous area found by this function. Thus the file allocation is guaranteed be contiguous and without allocation delay until the file size reaches this size unless any other changes to the volume is performed.

The contiguous file has an advantage for time-critical read/write operations. It eliminates some overheads in the filesystem and the storage device caused by random access for fragmented file.

-

Also the contiguous file can be easily accessed directly via low-level disk functions. However, this is not recommended in consideration of portability and future compatibility. If the file has not been confirmed be contiguous, use this function to examine if the file is contiguous or not.

+

The contiguou files can easily be accessed via low-level disk functions. However, this is not recommended in consideration of portability and future compatibility. If the file has not been confirmed be contiguous, use this function to examine if the file is contiguous or not.

@@ -76,16 +74,16 @@ FRESULT f_expand ( /* Creating a contiguous file */ /* Create a new file */ - res = f_open(fp = malloc(sizeof (FIL)), "file.dat", FA_WRITE|FA_CREATE_ALWAYS); + res = f_open(fp = malloc(sizeof (FIL)), "file.dat", FA_WRITE|FA_CREATE_ALWAYS); if (res) { /* Check if the file has been opened */ free(fp); die("Failed to open the file."); } - /* Alloacte a 100 MiB of contiguous area to the file */ + /* Allocate a 100 MiB of contiguous area to the file */ res = f_expand(fp, 104857600, 1); if (res) { /* Check if the file has been expanded */ - f_close(fp); + f_close(fp); free(fp); die("Failed to allocate contiguous area."); } @@ -101,7 +99,7 @@ FRESULT f_expand ( lba = fp->obj.fs->database + fp->obj.fs->csize * (fp->obj.sclust - 2); /* Write 2048 sectors from top of the file at a time */ - res = disk_write(drv, buffer, lba, 2048); + res = disk_write(drv, data, lba, 2048);
@@ -112,6 +110,7 @@ FRESULT f_expand (

f_open, f_lseek, FIL

+

Return

diff --git a/documents/doc/fattime.html b/documents/doc/fattime.html index 002733a..227c06e 100644 --- a/documents/doc/fattime.html +++ b/documents/doc/fattime.html @@ -3,8 +3,6 @@ - - FatFs - get_fattime diff --git a/documents/doc/fdisk.html b/documents/doc/fdisk.html index d8c40bd..1f0fd49 100644 --- a/documents/doc/fdisk.html +++ b/documents/doc/fdisk.html @@ -3,8 +3,6 @@ - - FatFs - f_fdisk @@ -49,7 +47,7 @@ FRESULT f_fdisk (

Description

-

The f_fdisk function creates partitions on the physical drive. The partitioning format can be in generic MBR or GPT. The partition map table specifies how to divide the physical drive. The first item specifies the size of the first partition and the partitions are located on the drive in order of from the first item. When the value of item is less than or equal to 100, it specifies the partition size in percentage of the entire drive space. When it is larger than 100, it specifies number of sectors. The partition map table is terminated by a zero, no space is remaining for next allocation or 4th partition is created in MBR format. If the specified size is larger than remaining space on the drive, the partition is truncated at end of the drive.

+

The f_fdisk function creates partitions on the physical drive. The partitioning format can be in generic MBR or GPT. The partition map table specifies how to divide the physical drive. The first item specifies the size of the first partition and the partitions are located on the drive in order of from the first item. When the value of item is less than or equal to 100, it specifies the partition size in percentage of the entire drive space. When it is larger than 100, it specifies number of sectors. The partition map table is terminated by a zero, 4th partition in MBR format or no remainin space for next allocation. If the specified size is larger than remaining space on the drive, the partition is truncated at end of the drive.

By default, partitions are created in MBR format. It can create upto four primary partitions on a drive. GPT format is used to create the partitions when 64-bit LBA is enabled (FF_LBA64 = 1) and the drive size is equal to or larger than FF_MIN_GPT sectors. It can create over ten partitions on a drive.

@@ -64,23 +62,23 @@ FRESULT f_fdisk ( /* Volume mapping table defined by user (required when FF_MULTI_PARTITION == 1) */ PARTITION VolToPart[FF_VOLUMES] = { - {0, 1}, /* "0:" ==> 1st partition in PD#0 */ - {0, 2}, /* "1:" ==> 2nd partition in PD#0 */ - {1, 0} /* "2:" ==> PD#1 as removable drive */ + {0, 1}, /* "0:" ==> 1st partition in physical drive 0 */ + {0, 2}, /* "1:" ==> 2nd partition in physical drive 0 */ + {1, 0} /* "2:" ==> Physical drive 1 as removable drive */ };
     /* Initialize a brand-new disk drive mapped to physical drive 0 */
 
     BYTE work[FF_MAX_SS];         /* Working buffer */
-    LBA_t plist[] = {50, 50, 0};  /* Divide the drive into two partitions */
-                 /* {0x10000000, 100}; 256M sectors for 1st partition and left all for 2nd partition */
+    LBA_t plist[] = {50, 50, 0};  /* Divide the drive by 2 */
+                 /* {0x10000000, 100}; 256M sectors for the 1st partition and the remaining for the 2nd partition */
                  /* {20, 20, 20, 0}; 20% for 3 partitions each and remaing space is left not allocated */
 
-    f_fdisk(0, plist, work);                    /* Divide physical drive 0 */
+    f_fdisk(0, plist, work);            /* Divide the physical drive 0 */
 
-    f_mkfs("0:", 0, work, sizeof work); /* Create FAT volume on the logical drive 0 */
-    f_mkfs("1:", 0, work, sizeof work); /* Create FAT volume on the logical drive 1 */
+    f_mkfs("0:", 0, work, sizeof work); /* Create FAT volume on the logical drive 0 */
+    f_mkfs("1:", 0, work, sizeof work); /* Create FAT volume on the logical drive 1 */
 
 
@@ -90,6 +88,7 @@ FRESULT f_fdisk (

Volume management, f_mkfs

+

Return

diff --git a/documents/doc/filename.html b/documents/doc/filename.html index 8ff028d..bf28457 100644 --- a/documents/doc/filename.html +++ b/documents/doc/filename.html @@ -3,8 +3,6 @@ - - FatFs - Path Names @@ -36,15 +34,15 @@ /..Invalid nameThe root directory (sticks the top level)

Also the drive prefix can be in pre-defined arbitrary string. When the option FF_STR_VOLUME_ID == 1, also arbitrary string volume ID can be used as drive prefix. e.g. "flash:file1.txt", "ram:temp.dat" or "sd:". If the srting does not match any volume ID, the function fails with FR_INVALID_DRIVE.

-

When FF_STR_VOLUME_ID == 2, Unix style drive prefix can be used. e.g. "/flash/file1.txt", "/ram/temp.dat" or "/sd". If a heading separator is exist, it is treated as start of drive prefix and in absolute path. Any form as "root directory in current drive" and "current directory in specified drive" cannot be used. Double dot name cannot traverse the drives such as "/flash/../ram/foo.dat".

+

When FF_STR_VOLUME_ID == 2, Unix style drive prefix can be used. e.g. "/flash/file1.txt", "/ram/temp.dat" or "/sd". If a heading separator is exist, it is treated as an absolute path with a heading node ID. Any form as "root directory in current drive" and "current directory in specified drive" cannot be used. Double dot name cannot traverse the volumes such as "/flash/../ram/foo.dat".

Remark: In this revision, double dot name ".." cannot follow the parent directory on the exFAT volume. It will work as "." and stay there.

Legal Characters and Case Sensitivity

In the generic FAT filesystems, the legal characters for object name (file/directory name) are, 0-9 A-Z ! # $ % & ' ( ) - @ ^ _ ` { } ~ in ASCII and extended characters \x80 to \xFF. In the FAT filesystems with LFN extention, also + , ; = [ ], white space and extended characters U+000080 to U+10FFFF are legal for the object name. White spaces and dots can be placed anywhere in the path name except end of the name. Trailing white spaces and dots are ignored.

-

FAT filesystem is case-insensitive to the object names on the volume. Object name on the FAT volume is compared in case-insensitive. For instance, these three names, file.txt, File.Txt and FILE.TXT, are identical on the FAT filesystem. This is applied to extended charactres as well. When an object is created on the FAT volume, up converted name is recorded to the SFN entry, and the raw name is recorded to the LFN entry when LFN extension is exist.

-

As for the MS-DOS and PC DOS for CJK (DOS/DBCS), extended characters ware recorded to the SFN entry without up-case conversion and compared in case-sensitive. This causes a problem on compatibility with Windows system when the object with extended characters is created on the volume by DOS/DBCS system; therfore the object names with DBCS extended characters should not be used on the FAT volume shared by those systems. FatFs works with case-sensitive to the extended characters in only non-LFN with DBCS configuration (DOS/DBCS specs). However, FatFs works with case-insensitive to the extended character (WindowsNT specs) in LFN configuration.

+

FAT filesystem is case-insensitive to the object names on the volume. Object names on the FAT volume are compared in case-insensitive. For instance, these three names, file.txt, File.Txt and FILE.TXT, are identical on the FAT filesystem. This is applied to extended charactres as well. When an object is created on the FAT volume, up converted name is recorded to the SFN entry, and the original name is recorded to the LFN entry if LFN extension is enabled.

+

On the MS-DOS and PC DOS for Chinese, Japanese and Korean (DOS/DBCS), extended characters are recorded to the SFN entry without up-case conversion and compared in case-sensitive. This causes a problem on compatibility with Windows system when the object with extended characters is created on the volume by DOS/DBCS system; therfore the object names with DBCS extended characters should not be used on the FAT volume shared by those systems. FatFs works with case-sensitive to the extended characters in only non-LFN with DBCS configuration (DOS/DBCS specs). However, FatFs works with case-insensitive to the extended character (WindowsNT specs) in LFN configuration.

@@ -84,11 +82,12 @@ PARTITION VolToPart[FF_VOLUMES] = {
  • On the MBR format drive, up to four primary partitions (1-4) can be specified. The partition number 1 specifies the first item in the partition table and the partition number 2 specifies the second one, and so on. The logical patitions (5-) in the extended partition is not supported.
  • On the GPT format drive, the partition number 1 specifies the first Microsoft BDP found in the partition table and the partition number 2 specifies the second one found, and so on.
  • Windows 10 earlier than 1703 does not support multiple volumes on the physical drive with removable class. Only the first parition found on the drive will be mounted. Windows OS does not support SFD format on the physical drive with non-removable class.
  • -
  • Some systems manage the on-board storage in non-standard partition format and each partition is mapped as physical drive in disk_* functions. For such system, FF_MULTI_PARTITION should be always 0.
  • -
  • For further information about the volume management, refer to the description in f_fdisk and f_mkfs.
  • +
  • Some systems manage the on-board storage in non-standard partition format and each partition is mapped as physical drive in disk_* functions. For such system, FF_MULTI_PARTITION should be always 0 and use FM_SFD flag in f_mkfs.
  • +
  • For further information about the volume management, refer to the description in f_mkfs and f_fdisk.
  • +

    Return

    diff --git a/documents/doc/findfirst.html b/documents/doc/findfirst.html index 5f1e344..d3ae56d 100644 --- a/documents/doc/findfirst.html +++ b/documents/doc/findfirst.html @@ -3,8 +3,6 @@ - - FatFs - f_findfirst @@ -28,7 +26,7 @@ FRESULT f_findfirst (

    Parameters

    dp
    -
    Pointer to the blank directory object.
    +
    Pointer to the blank directory object. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
    fno
    Pointer to the file information structure to store the information about the found item.
    path
    @@ -99,10 +97,10 @@ void find_image_file (void) while (fr == FR_OK && fno.fname[0]) { /* Repeat while an item is found */ printf("%s\n", fno.fname); /* Print the object name */ - fr = f_findnext(&dj, &fno); /* Search for next item */ + fr = f_findnext(&dj, &fno); /* Search for next item */ } - f_closedir(&dj); + f_closedir(&dj); } diff --git a/documents/doc/findnext.html b/documents/doc/findnext.html index f39f0fb..1800f61 100644 --- a/documents/doc/findnext.html +++ b/documents/doc/findnext.html @@ -3,8 +3,6 @@ - - FatFs - f_findnext @@ -64,6 +62,7 @@ FRESULT f_findnext (

    f_findfirst, f_closedir, DIR, FILINFO

    +

    Return

    diff --git a/documents/doc/forward.html b/documents/doc/forward.html index fdb5f49..056a48e 100644 --- a/documents/doc/forward.html +++ b/documents/doc/forward.html @@ -3,8 +3,6 @@ - - FatFs - f_forward @@ -28,13 +26,13 @@ FRESULT f_forward (

    Parameters

    fp
    -
    Pointer to the open file object.
    +
    Pointer to the open file object. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
    func
    Pointer to the user-defined data streaming function. For details, refer to the sample code.
    btf
    Number of bytes to forward in range of UINT.
    bf
    -
    Pointer to the UINT variable to return number of bytes forwarded.
    +
    Pointer to the variable in UINT type to return number of bytes forwarded.
    @@ -109,11 +107,11 @@ FRESULT play_file ( UINT dmy; /* Open the audio file in read only mode */ - rc = f_open(&fil, fn, FA_READ); + rc = f_open(&fil, fn, FA_READ); if (rc) return rc; /* Repeat until the file pointer reaches end of the file */ - while (rc == FR_OK && !f_eof(&fil)) { + while (rc == FR_OK && !f_eof(&fil)) { /* some processes... */ @@ -122,7 +120,7 @@ FRESULT play_file ( } /* Close the file and return */ - f_close(&fil); + f_close(&fil); return rc; } @@ -134,6 +132,7 @@ FRESULT play_file (

    f_open, fgets, f_write, f_close, FIL

    +

    Return

    diff --git a/documents/doc/getcwd.html b/documents/doc/getcwd.html index 6eb9a58..22a31e1 100644 --- a/documents/doc/getcwd.html +++ b/documents/doc/getcwd.html @@ -3,8 +3,6 @@ - - FatFs - f_getcwd @@ -78,6 +76,7 @@ FRESULT f_getcwd (

    f_chdrive, f_chdir

    +

    Return

    diff --git a/documents/doc/getfree.html b/documents/doc/getfree.html index 5e4f419..ac378a7 100644 --- a/documents/doc/getfree.html +++ b/documents/doc/getfree.html @@ -3,8 +3,6 @@ - - FatFs - f_getfree diff --git a/documents/doc/getlabel.html b/documents/doc/getlabel.html index 0ec6d0d..d7d3362 100644 --- a/documents/doc/getlabel.html +++ b/documents/doc/getlabel.html @@ -3,8 +3,6 @@ - - FatFs - f_getlabel @@ -27,7 +25,7 @@ FRESULT f_getlabel (

    Parameters

    path
    -
    Pointer to the null-terminated string that specifies the logical drive. Null-string specifies the default drive.
    +
    Pointer to the null-terminated string that specifies the logical drive. Null-string specifies the default drive. If a null pointer is given, the function fails with FR_INVALID_DRIVE.
    label
    Pointer to the buffer to store the volume label. If the volume has no label, a null-string will be returned. Set null pointer if this information is not needed. The buffer size should be shown below at least to avoid buffer overflow.
    diff --git a/documents/doc/gets.html b/documents/doc/gets.html index 89d9560..f4a22ca 100644 --- a/documents/doc/gets.html +++ b/documents/doc/gets.html @@ -3,8 +3,6 @@ - -FatFs - f_gets @@ -45,13 +43,13 @@ TCHAR* f_gets (

    Description

    The read operation continues until a '\n' is stored, reached end of the file or the buffer is filled with len - 1 characters. The read string is terminated with a '\0'. When no character to read or any error occured during read operation, it returns a null pointer. The status of EOF and error can be examined with f_eof and f_error function.

    -

    When FatFs is configured to Unicode API (FF_LFN_UNICODE >= 1), data types on the srting fuctions, f_putc, f_puts, f_printf and f_gets, is also switched to Unicode. The character encoding on the file to be read via this function is assumed as FF_STRF_ENCODE. If the character encoding on the file differs from that on the API, it is converted in this function. In this case, input characters with wrong encoding will be lost.

    +

    When FatFs is configured to Unicode API (FF_LFN_UNICODE >= 1), data types on the srting fuctions, f_putc, f_puts, f_printf and f_gets, is also switched to Unicode. The character encoding on the file to be read via this function is assumed as FF_STRF_ENCODE. If the character encoding differs between file data and API, it is converted in this function. Input characters with wrong encoding for output will be lost.

    QuickInfo

    -

    This is a wrapper function of f_read function. Available when FF_USE_STRFUNC >= 1. When it is set to 2, '\r's contained in the file are stripped out.

    +

    This is a wrapper function of f_read function. Available when FF_USE_STRFUNC >= 1. When it is set to 2, '\r's contained in the read data are stripped off.

    @@ -60,6 +58,7 @@ TCHAR* f_gets (

    f_open, f_read, f_putc, f_puts, f_printf, f_close, FIL

    +

    Return

    diff --git a/documents/doc/lseek.html b/documents/doc/lseek.html index ffd1f62..a0ff7f9 100644 --- a/documents/doc/lseek.html +++ b/documents/doc/lseek.html @@ -3,8 +3,6 @@ - -FatFs - f_lseek @@ -32,7 +30,7 @@ FRESULT f_rewind (

    Parameters

    fp
    -
    Pointer to the open file object.
    +
    Pointer to the open file object. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
    ofs
    Byte offset from top of the file to set read/write pointer. The data type FSIZE_t is an alias of either DWORD(32-bit) or QWORD(64-bit) depends on the configuration option FF_FS_EXFAT.
    @@ -53,11 +51,12 @@ FRESULT f_rewind (

    Description

    -

    File read/write ponter in the open file object points the data byte to be read/written at next read/write operation. It advances as the number of bytes read/written. The f_lseek function moves the file read/write pointer without any read/write operation to the file. The f_rewind function is impremented as a macro.

    +

    File read/write ponter in the open file object points the data byte to be read/written at next read/write operation. It advances as the number of bytes read/written. The f_lseek function moves the file read/write pointer without read/write operation to the file. The f_rewind function is impremented as a macro.

     #define f_rewind(fp) f_lseek((fp), 0)
     
    -

    If an offset beyond the file size is specified in write mode, the file size is expanded to the specified offset. The file data in the expanded part is undefined, because no data is written to the file in this process. This is suitable to pre-allocate a data area to the file quickly for fast write operation. When a contiguous data area needs to be allocated to the file, use f_expand function instead. After the f_lseek function succeeded, the current read/write pointer should be checked in order to make sure the read/write pointer has been moved correctry. In case of the read/write pointer is not pointing expected offset, either of followings has been occured.

    +

    When an offset beyond the file size is specified in write mode, the file size is expanded to the specified offset in this function. The file data in the expanded part is undefined, because no data is written to the file in this process. Be careful about these behaviours differ from POSIX fseek function. This is suitable to pre-allocate a data area to the file for subsequent fast write operation. If a contiguous data area needs to be allocated to the file, use f_expand function instead.

    +

    After the f_lseek function succeeded, the current read/write pointer should be checked in order to make sure the read/write pointer has been moved correctry. In case of the read/write pointer is not pointing expected offset, either of followings has been occured.

    • End of file. The specified ofs was clipped at end of the file in read-only mode.
    • Disk full. There is no free space on the volume to expand the file.
    • @@ -78,7 +77,7 @@ FRESULT f_rewind (
           /* Open file */
           fp = malloc(sizeof (FIL));
      -    res = f_open(fp, "file.dat", FA_READ|FA_WRITE);
      +    res = f_open(fp, "file.dat", FA_READ|FA_WRITE);
           if (res) ...
       
           /* Set read/write pointer to 5000 */
      @@ -96,15 +95,15 @@ FRESULT f_rewind (
       
       /* Cluster pre-allocation (to prevent buffer overrun on streaming write) */
       
      -    res = f_open(fp, recfile, FA_CREATE_NEW | FA_WRITE);   /* Create a file */
      +    res = f_open(fp, recfile, FA_CREATE_NEW | FA_WRITE);   /* Create a file */
       
           res = f_lseek(fp, PRE_SIZE);             /* Expand file size (cluster pre-allocation) */
      -    if (res || f_tell(fp) != PRE_SIZE) ...   /* Check if the file has been expanded successfly */
      +    if (res || f_tell(fp) != PRE_SIZE) ...   /* Check if the file has been expanded successfly */
       
           res = f_lseek(fp, OFS_DATA);             /* Record data stream with free from cluster allocation delay */
           ...                                      /* Write operation should be aligned to sector boundary to optimize the write throughput */
       
      -    res = f_truncate(fp);                    /* Truncate unused area */
      +    res = f_truncate(fp);                    /* Truncate unused area */
           res = f_lseek(fp, OFS_HEADER);           /* Set file header */
           ...
       
      @@ -115,7 +114,7 @@ FRESULT f_rewind (
       
           DWORD clmt[SZ_TBL];                    /* Cluster link map table buffer */
       
      -    res = f_open(fp, fname, FA_READ | FA_WRITE);   /* Open a file */
      +    res = f_open(fp, fname, FA_READ | FA_WRITE);   /* Open a file */
       
           res = f_lseek(fp, ofs1);               /* This is normal seek (cltbl is nulled on file open) */
       
      @@ -134,6 +133,7 @@ FRESULT f_rewind (
       

      f_open, f_truncate, f_expand, FIL

    +

    Return

    diff --git a/documents/doc/mkdir.html b/documents/doc/mkdir.html index de32f83..7027cd8 100644 --- a/documents/doc/mkdir.html +++ b/documents/doc/mkdir.html @@ -3,8 +3,6 @@ - -FatFs - f_mkdir @@ -75,6 +73,7 @@ FRESULT f_mkdir ( +

    Return

    diff --git a/documents/doc/mkfs.html b/documents/doc/mkfs.html index f40ebb0..8a452a0 100644 --- a/documents/doc/mkfs.html +++ b/documents/doc/mkfs.html @@ -3,8 +3,6 @@ - -FatFs - f_mkfs @@ -36,12 +34,12 @@ FRESULT f_mkfs (
    Specifies a combination of FAT type flags, FM_FAT, FM_FAT32, FM_EXFAT and bitwise-or of these three, FM_ANY. FM_EXFAT is ignored when exFAT is not enabled. These flags specify which type of FAT volume to be created. If two or more types are specified, one out of them will be selected depends on the volume size and au_size. The flag FM_SFD specifies to create the volume on the drive in SFD format. The default value is FM_ANY.
    BYTE n_fat
    Specifies number of FAT copies on the FAT/FAT32 volume. Valid value for this member is 1 or 2. The default value (0) and any invaid value gives 1. If the FAT type is exFAT, this member has no effect.
    -
    UINT n_align
    -
    Specifies alignment of the volume data area (file allocation pool, usually erase block boundary of flash memory media) in unit of sector. The valid value for this member is between 1 and 32768 inclusive in power of 2. If a zero (the default value) or any invalid value is given, the function obtains the block size from lower layer with disk_ioctl function.
    -
    DWORD au_size
    -
    Specifies size of the allocation unit (cluter) in unit of byte. The valid value is power of 2 between sector size and 128 * sector size inclusive for FAT/FAT32 volume, or up to 16 MB for exFAT volume. If a zero (default value) or any invalid value is given, the function uses default allocation unit size depends on the volume size.
    +
    UINT align
    +
    Specifies alignment of the volume data area (file allocation pool, usually erase block boundary of flash memory media) in unit of sector. The valid value for this member is between 1 and 32768 inclusive in power of 2. If a zero (the default value) or any invalid value is given, the function obtains the block size from lower layer with disk_ioctl function.
    UINT n_root
    Specifies number of root directory entries on the FAT volume. Valid value for this member is up to 32768 and aligned to sector size / 32. The default value (0) and any invaid value gives 512. If the FAT type is FAT32 or exFAT, this member has no effect.
    +
    DWORD au_size
    +
    Specifies size of the cluster (allocation unit) in unit of byte. The valid value for this member is between sector size and 128 * sector size inclusive in power of 2 for FAT/FAT32 volume and up to 16 MB in power of 2 for exFAT volume. If a zero (default value) or any invalid value is given, the function uses a default cluster size depends on the volume size.
    work
    Pointer to the working buffer used for the format process. If a null pointer is given with FF_USE_LFN == 3, the function uses a len bytes of heap memory in this function.
    @@ -69,7 +67,7 @@ FRESULT f_mkfs (

    The FAT sub-type, FAT12/FAT16/FAT32, of FAT volume except exFAT is determined by only number of clusters on the volume and nothing else, according to the FAT specification issued by Microsoft. Thus the FAT sub-type of created volume depends on the volume size and the cluster size. In case of the combination of FAT type and cluter size specified by argument is not valid for the volume size, the function will fail with FR_MKFS_ABORTED.

    The allocation unit, also known as cluster, is a unit of disk space allocation for files. When the size of allocation unit is 32768 bytes, a file with 100 bytes in size occupies 32768 bytes of disk space. The space efficiency of disk usage gets worse as increasing size of allocation unit, but, on the other hand, the read/write performance increases. Therefore the size of allocation unit is a trade-off between space efficiency and performance. For the large volumes in GB order, 32768 bytes or larger, automatically selected by default, is recommended for most case unless extremely many small files are created in the volume.

    When the logical drive to be formatted is associated with a physical drive (FF_MULTI_PARTITION == 0 or VolToPart[].pt == 0) and FM_SFD flag is not specified, a partition occupies entire drive space is created and then the FAT volume is created in the partition. When FM_SFD flag is specified, the FAT volume is created without any disk partitioning.

    -

    When the logical drive to be formatted is associated with a specific partition by multiple partition feature (FF_MULTI_PARTITION == 1 and VolToPart[].pt > 0), the FAT volume is created in the partition of the physical drive specified by volume mapping table and FM_SFD flag is ignored. The hosting physical drive needs to be partitioned with f_fdisk function or any partitioning tool prior to create the FAT volume with this function. If the partition is not exist, the function aborts with FR_MKFS_ABORTED.

    +

    When the logical drive to be formatted is associated with a specific partition by multiple partition feature (FF_MULTI_PARTITION == 1 and VolToPart[].pt > 0), the FAT volume is created in the partition of the physical drive specified by volume mapping table and FM_SFD flag is ignored. The hosting physical drive needs to be partitioned with f_fdisk function or some partitioning tool prior to create the FAT volume with this function. If the partition is not exist, the function fails with FR_MKFS_ABORTED.

    There are three standard disk partitioning formats, MBR, GPT and SFD. The MBR format, also known as FDISK format, is usually used for harddisk, memory card and U disk. It can divide a physical drive into one or more partitions with a partition table. The GPT, GUID Partition Table, is a newly defined patitioning format for large storage devices. FatFs suppors the GPT only when 64-bit LBA is enabled. The SFD, Super-Floppy Disk, is non-partitioned disk format. The FAT volume is located at LBA 0 and occupies the entire physical drive without any disk partitioning. It is usually used for floppy disk, optical disk and most super-floppy media. Some combination of systems and media support only either partitioned format or non-partitioned format and the other is not supported.

    Some systems manage the partitions in the on-board storage in non-standard format. The partitions are mapped as physical drives identified by pdrv in disk_* functions. For such systems, SFD format is suitable to create the FAT volume in the partition.

    @@ -97,21 +95,21 @@ int main (void) if (res) ... /* Give a work area to the default drive */ - f_mount(&fs, "", 0); + f_mount(&fs, "", 0); /* Create a file as new */ - res = f_open(&fil, "hello.txt", FA_CREATE_NEW | FA_WRITE); + res = f_open(&fil, "hello.txt", FA_CREATE_NEW | FA_WRITE); if (res) ... /* Write a message */ - f_write(&fil, "Hello, World!\r\n", 15, &bw); + f_write(&fil, "Hello, World!\r\n", 15, &bw); if (bw != 15) ... /* Close the file */ - f_close(&fil); + f_close(&fil); /* Unregister work area */ - f_mount(0, "", 0); + f_mount(0, "", 0); ... @@ -122,6 +120,7 @@ int main (void)

    Example of volume size and format parameters, Volume management, f_fdisk

    +

    Return

    diff --git a/documents/doc/mount.html b/documents/doc/mount.html index 57f40be..f0383b9 100644 --- a/documents/doc/mount.html +++ b/documents/doc/mount.html @@ -3,8 +3,6 @@ - -FatFs - f_mount @@ -92,19 +90,19 @@ int main (void) FATFS *fs; /* Ponter to the filesystem object */ - fs = malloc(sizeof (FATFS)); /* Get work area for the volume */ - f_mount(fs, "", 0); /* Mount the default drive */ + fs = malloc(sizeof (FATFS)); /* Get work area for the volume */ + f_mount(fs, "", 0); /* Mount the default drive */ - f_open(... /* Here any file API can be used */ + f_open(... /* Here any file API can be used */ ... - f_mount(fs, "", 0); /* Re-mount the default drive to reinitialize the filesystem */ + f_mount(fs, "", 0); /* Re-mount the default drive to reinitialize the filesystem */ ... - f_mount(0, "", 0); /* Unmount the default drive */ - free(fs); /* Here the work area can be discarded */ + f_unmount(""); /* Unmount the default drive */ + free(fs); /* Here the work area can be discarded */ ... } @@ -117,6 +115,7 @@ int main (void)

    f_open, FATFS

    +

    Return

    diff --git a/documents/doc/open.html b/documents/doc/open.html index cc8b87a..b533249 100644 --- a/documents/doc/open.html +++ b/documents/doc/open.html @@ -3,8 +3,6 @@ - -FatFs - f_open @@ -27,19 +25,19 @@ FRESULT f_open (

    Parameters

    fp
    -
    Pointer to the blank file object structure.
    +
    Pointer to the blank file object structure. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
    path
    -
    Pointer to the null-terminated string that specifies the file name to open or create.
    +
    Pointer to the null-terminated string that specifies the file name to open or create. If a null pointer is given, the function fails with FR_INVALID_DRIVE.
    mode
    Mode flags that specifies the type of access and open method for the file. It is specified by a combination of following flags.
    - - - - + + + +
    FlagsMeaning
    FA_READSpecifies read access to the file. Data can be read from the file.
    FA_WRITESpecifies write access to the file. Data can be written to the file. Combine with FA_READ for read-write access.
    FA_OPEN_EXISTINGOpens a file. The function fails if the file is not existing. (Default)
    FA_CREATE_NEWCreates a new file. The function fails with FR_EXIST if the file is existing.
    FA_CREATE_ALWAYSCreates a new file. If the file is existing, it will be truncated and overwritten.
    FA_OPEN_ALWAYSOpens the file if it is existing. If not, a new file will be created.
    FA_OPEN_EXISTINGOpens the file. The function fails if the file is not existing. (Default)
    FA_CREATE_ALWAYSCreates a new file. If the file is existing, the file is truncated and overwritten.
    FA_CREATE_NEWCreates a new file. The function fails if the file is existing.
    FA_OPEN_ALWAYSOpens the file. If it is not exist, a new file is created.
    FA_OPEN_APPENDSame as FA_OPEN_ALWAYS except the read/write pointer is set end of the file.
    Mode flags in POSIX fopen() function corresponds to FatFs mode flags as follows:
    @@ -86,7 +84,8 @@ Mode flags in POSIX fopen() function corresponds to FatFs mode flags as follows:

    Description

    -

    The f_open function opens a file and creates a file object. The file object is an identifier for subsequent operations to the file. Open file should be closed with f_close function after the session of the file access. If any change to the file has been made and not closed prior to power off, media removal or re-mount, or the file can be collapsed.

    +

    The f_open function opens a file and creates a file object. It is the identifier for subsequent read/write operations to the file. After the function succeeded, the file object is valid. If the function failed, the file object is set invalid.

    +

    Open file should be closed with f_close function after the session of the file access. If any change to the file has been made and not closed prior to power off, media removal or re-mount, or the file can be collapsed.

    If duplicated file open is needed, read here carefully. However duplicated open of a file with any write mode flag is always prohibited.

    @@ -112,19 +111,19 @@ int main (void) /* Give a work area to the default drive */ - f_mount(&FatFs, "", 0); + f_mount(&FatFs, "", 0); /* Open a text file */ fr = f_open(&fil, "message.txt", FA_READ); if (fr) return (int)fr; /* Read every line and display it */ - while (f_gets(line, sizeof line, &fil)) { + while (f_gets(line, sizeof line, &fil)) { printf(line); } /* Close the file */ - f_close(&fil); + f_close(&fil); return 0; } @@ -142,8 +141,8 @@ int main (void) /* Give work areas to each logical drive */ - f_mount(&fs0, "0:", 0); - f_mount(&fs1, "1:", 0); + f_mount(&fs0, "0:", 0); + f_mount(&fs1, "1:", 0); /* Open source file on the drive 1 */ fr = f_open(&fsrc, "1:file.bin", FA_READ); @@ -155,19 +154,19 @@ int main (void) /* Copy source to destination */ for (;;) { - fr = f_read(&fsrc, buffer, sizeof buffer, &br); /* Read a chunk of data from the source file */ + fr = f_read(&fsrc, buffer, sizeof buffer, &br); /* Read a chunk of data from the source file */ if (br == 0) break; /* error or eof */ - fr = f_write(&fdst, buffer, br, &bw); /* Write it to the destination file */ + fr = f_write(&fdst, buffer, br, &bw); /* Write it to the destination file */ if (bw < br) break; /* error or disk full */ } /* Close open files */ - f_close(&fsrc); - f_close(&fdst); + f_close(&fsrc); + f_close(&fdst); /* Unregister work area prior to discard it */ - f_unmount("0:"); - f_unmount("1:"); + f_unmount("0:"); + f_unmount("1:"); return (int)fr; } @@ -180,6 +179,7 @@ int main (void)

    f_read, f_write, f_close, FIL, FATFS

    +

    Return

    diff --git a/documents/doc/opendir.html b/documents/doc/opendir.html index 215c37a..1955bc3 100644 --- a/documents/doc/opendir.html +++ b/documents/doc/opendir.html @@ -3,8 +3,6 @@ - - FatFs - f_opendir @@ -26,9 +24,9 @@ FRESULT f_opendir (

    Parameters

    dp
    -
    Pointer to the blank directory object to create a new one.
    +
    Pointer to the blank directory object to create a new one. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
    path
    -
    Pointer to the null-terminated string that specifies the directory name to be opened.
    +
    Pointer to the null-terminated string that specifies the directory name to be opened. If a null pointer is given, the function fails with FR_INVALID_DRIVE.
    @@ -70,6 +68,7 @@ FRESULT f_opendir (

    f_readdir, f_closedir, DIR

    +

    Return

    diff --git a/documents/doc/printf.html b/documents/doc/printf.html index eb64e8d..c1e1f29 100644 --- a/documents/doc/printf.html +++ b/documents/doc/printf.html @@ -1,115 +1,114 @@ - - - - - - - - -FatFs - f_printf - - - - -
    -

    f_printf

    -

    The f_printf function writes formatted string to the file.

    -
    -int f_printf (
    -  FIL* fp,          /* [IN] File object */
    -  const TCHAR* fmt, /* [IN] Format stirng */
    -  ...
    -);
    -
    -
    - -
    -

    Parameters

    -
    -
    fp
    -
    Pointer to the open file object structure.
    -
    fmt
    -
    Pointer to the null '\0' terminated format string. The terminator character will not be output.
    -
    ...
    -
    Optional arguments...
    - -
    -
    - - -
    -

    Return Values

    -

    When the string was written successfuly, it returns number of character encoding units written to the file. When the function failed due to disk full or an error, a negative value will be returned.

    -
    - - -
    -

    Description

    -

    The format control directive is a sub-set of standard library shown as follows:

    -
    -    %[flag][width][precision][size]type
    -
    -
    -
    flag
    Padding option. A - specifies left-aligned. A 0 specifies zero padded. The default setting is in right-aligned and space padded.
    -
    width
    Minimum width of the field, 1-99 or *. If the width of generated string is less than the minimum width, rest field is padded with spaces or zeros. An * specifies the value comes from an argument in int type. The default setting is zero.
    -
    precision
    Specifies number of fractional digits or maximum width of string, .0-.99 or .*. If the number is omitted, it is same as .0. Default setting is 6 for number and no limit for string.
    -
    size
    Specifies size of integer argument, l(long) and ll(long long). If sizeof (long) == sizeof (int) is true (this is typical of 32-bit systems), prefix l can be omitted for long integer argument. The default size is int for integer argument and floating point argument is always assumed double as the default argument promotion.
    -
    type
    Specifies type of the output format and the argument as shown below. The length of generated string is in assumtion of int is 32-bit. - - - - - - - - - - - -
    TypeFormatArgumentLength
    cCharacterint,
    long,
    long long
    1 character.
    dSigned decimal1 to 11 (20 for ll) characters.
    uUnsigned decimal1 to 10 (20 for ll) characters.
    oUnsigned octal1 to 11 (22 for ll) characters.
    x XUnsigned hexdecimal1 to 8 (16 for ll) characters.
    bUnsigned binary1 to 32 characters. Limited to lower 32 digits when ll is specified.
    sStringTCHAR*As input string. Null pointer generates a null string.
    fFloating point
    (decimal)
    double1 to 31 characters. If the number of characters exceeds 31, it writes "±OV". Not a number and infinite write "NaN" and "±INF".
    e EFloating point
    (e notation)
    4 to 31 characters. If the number of characters exceeds 31 or exponent exceeds +99, it writes "±OV".
    -
    -
    -

    When FatFs is configured for Unicode API (FF_LFN_UNICODE >= 1), character encoding on the string fuctions, f_putc, f_puts, f_printf and f_gets function, is also switched to Unicode. The Unicode characters in multiple encoding unit, such as surrogate pair and multi-byte sequence, should not be divided into two function calls, or the character will be lost. The character encoding on the file to be written via this function is selected by FF_STRF_ENCODE. The characters with wrong encoding or invalid for the output encoding will be lost.

    -

    If sprintf is used in the project and code conversion is not needed, f_write with sprintf will be efficient in code size and performance rather than f_printf.

    -
    - - -
    -

    QuickInfo

    -

    This is a wrapper function of f_write function. Available when FF_FS_READONLY == 0 and FF_USE_STRFUNC >= 1. When FF_USE_STRFUNC == 2, '\n's in the generated string are written as '\r'+'\n' each.

    -
    - - -
    -

    Example

    -
    -    f_printf(fp, "%d", 1234);             /* "1234" */
    -    f_printf(fp, "%6d,%3d%%", -200, 5);   /* "  -200,  5%" */
    -    f_printf(fp, "%-6u", 100);            /* "100   " */
    -    f_printf(fp, "%ld", 12345678);        /* "12345678" */
    -    f_printf(fp, "%llu", 0x100000000);    /* "4294967296"   (FF_PRINT_LLI) */
    -    f_printf(fp, "%lld", -1LL);           /* "-1"           (FF_PRINT_LLI) */
    -    f_printf(fp, "%04x", 0xA3);           /* "00a3" */
    -    f_printf(fp, "%08lX", 0x123ABC);      /* "00123ABC" */
    -    f_printf(fp, "%016b", 0x550F);        /* "0101010100001111" */
    -    f_printf(fp, "%*d", 6, 100);          /* "   100" */
    -    f_printf(fp, "%s", "abcdefg");        /* "abcdefg" */
    -    f_printf(fp, "%5s", "abc");           /* "  abc" */
    -    f_printf(fp, "%-5s", "abc");          /* "abc  " */
    -    f_printf(fp, "%.5s", "abcdefg");      /* "abcde" */
    -    f_printf(fp, "%-5.2s", "abcdefg");    /* "ab   " */
    -    f_printf(fp, "%c", 'a');              /* "a" */
    -    f_printf(fp, "%12f", 10.0);           /* "   10.000000" (FF_PRINT_FLOAT) */
    -    f_printf(fp, "%.4E", 123.45678);      /* "1.2346E+02"   (FF_PRINT_FLOAT) */
    -
    -
    - - -
    -

    See Also

    -

    f_open, f_putc, f_puts, f_gets, f_close, FIL

    -
    - -

    Return

    - - + + + + + + +FatFs - f_printf + + + + +
    +

    f_printf

    +

    The f_printf function writes formatted string to the file.

    +
    +int f_printf (
    +  FIL* fp,          /* [IN] File object */
    +  const TCHAR* fmt, /* [IN] Format stirng */
    +  ...
    +);
    +
    +
    + +
    +

    Parameters

    +
    +
    fp
    +
    Pointer to the open file object structure.
    +
    fmt
    +
    Pointer to the null '\0' terminated format string. The terminator character will not be output.
    +
    ...
    +
    Optional arguments...
    + +
    +
    + + +
    +

    Return Values

    +

    When the string was written successfuly, it returns number of character encoding units written to the file. When the function failed due to disk full or an error, a negative value will be returned.

    +
    + + +
    +

    Description

    +

    The format control directive is a sub-set of standard library shown as follows:

    +
    +    %[flag][width][precision][size]type
    +
    +
    +
    flag
    Padding option. A - specifies left-aligned. A 0 specifies zero padded. The default setting is in right-aligned and space padded.
    +
    width
    Minimum width of the field, 1-99 or *. If the width of generated string is less than the minimum width, rest field is padded with spaces or zeros. An * specifies the value comes from an argument in int type. The default setting is zero.
    +
    precision
    Specifies number of fractional digits or maximum width of string, .0-.99 or .*. If the number is omitted, it is same as .0. Default setting is 6 for number and no limit for string.
    +
    size
    Specifies size of integer argument, l(long) and ll(long long). If sizeof (long) == sizeof (int) is true (this is typical of 32-bit systems), prefix l can be omitted for long integer argument. The default size is int for integer argument and floating point argument is always assumed double as the default argument promotion.
    +
    type
    Specifies type of the output format and the argument as shown below. The length of generated string is in assumtion of int is 32-bit. + + + + + + + + + + + +
    TypeFormatArgumentLength
    cCharacterint,
    long,
    long long
    1 character.
    dSigned decimal1 to 11 (20 for ll) characters.
    uUnsigned decimal1 to 10 (20 for ll) characters.
    oUnsigned octal1 to 11 (22 for ll) characters.
    x XUnsigned hexdecimal1 to 8 (16 for ll) characters.
    bUnsigned binary1 to 32 characters. Limited to lower 32 digits when ll is specified.
    sStringTCHAR*As input string. A null pointer generates a zero-length string.
    fFloating point
    (decimal)
    double1 to 31 characters. If the number of characters exceeds 31, it writes "±OV". Not a number and infinite value write "NaN" and "±INF" respectively.
    e EFloating point
    (e notation)
    4 to 31 characters. If the number of characters exceeds 31 or exponent exceeds +99, it writes "±OV".
    +
    +
    +

    When FatFs is configured for Unicode API (FF_LFN_UNICODE >= 1), character encoding on the string fuctions, f_putc, f_puts, f_printf and f_gets function, is also switched to Unicode. The Unicode characters in multiple encoding unit, such as surrogate pair and multi-byte sequence, should not be divided into two function calls, or the character will be lost. The character encoding on the file to be written via this function is selected by FF_STRF_ENCODE. If the character encoding differs between file data and API, it is converted in this function. Input characters with wrong encoding for output will be lost.

    +

    If sprintf is used in the project and code conversion is not needed, f_write with sprintf will be efficient in code size and performance rather than f_printf.

    +
    + + +
    +

    QuickInfo

    +

    This is a wrapper function of f_write function. Available when FF_FS_READONLY == 0 and FF_USE_STRFUNC >= 1. When FF_USE_STRFUNC == 2, '\n's in the generated string are written as '\r'+'\n' each.

    +
    + + +
    +

    Example

    +
    +    f_printf(fp, "%d", 1234);             /* "1234" */
    +    f_printf(fp, "%6d,%3d%%", -200, 5);   /* "  -200,  5%" */
    +    f_printf(fp, "%-6u", 100);            /* "100   " */
    +    f_printf(fp, "%ld", 12345678);        /* "12345678" */
    +    f_printf(fp, "%llu", 0x100000000);    /* "4294967296"   (FF_PRINT_LLI) */
    +    f_printf(fp, "%lld", -1LL);           /* "-1"           (FF_PRINT_LLI) */
    +    f_printf(fp, "%04x", 0xA3);           /* "00a3" */
    +    f_printf(fp, "%08lX", 0x123ABC);      /* "00123ABC" */
    +    f_printf(fp, "%016b", 0x550F);        /* "0101010100001111" */
    +    f_printf(fp, "%*d", 6, 100);          /* "   100" */
    +    f_printf(fp, "%s", "abcdefg");        /* "abcdefg" */
    +    f_printf(fp, "%5s", "abc");           /* "  abc" */
    +    f_printf(fp, "%-5s", "abc");          /* "abc  " */
    +    f_printf(fp, "%.5s", "abcdefg");      /* "abcde" */
    +    f_printf(fp, "%-5.2s", "abcdefg");    /* "ab   " */
    +    f_printf(fp, "%c", 'a');              /* "a" */
    +    f_printf(fp, "%12f", 10.0);           /* "   10.000000" (FF_PRINT_FLOAT) */
    +    f_printf(fp, "%.4E", 123.45678);      /* "1.2346E+02"   (FF_PRINT_FLOAT) */
    +
    +
    + + +
    +

    See Also

    +

    f_open, f_putc, f_puts, f_gets, f_close, FIL

    +
    + + +

    Return

    + + diff --git a/documents/doc/putc.html b/documents/doc/putc.html index 747b5fd..e48fbb1 100644 --- a/documents/doc/putc.html +++ b/documents/doc/putc.html @@ -3,8 +3,6 @@ - - FatFs - f_putc @@ -55,6 +53,7 @@ int f_putc (

    f_open, f_puts, f_printf, f_gets, f_close, FIL

    +

    Return

    diff --git a/documents/doc/puts.html b/documents/doc/puts.html index 3695942..2def40f 100644 --- a/documents/doc/puts.html +++ b/documents/doc/puts.html @@ -3,8 +3,6 @@ - - FatFs - f_puts @@ -41,7 +39,7 @@ int f_puts (

    Description

    -

    When FatFs is configured for Unicode API (FF_LFN_UNICODE >= 1), character encoding on the string fuctions, f_putc, f_puts, f_printf and f_gets function, is also switched to Unicode. The input Unicode characters in multiple encoding unit, such as surrogate pair and multi-byte sequence, should not be divided into two function calls, or the character will be lost. The character encoding on the file to be written via this functions is selected by FF_STRF_ENCODE. The characters with wrong encoding or invalid for the output encoding will be lost.

    +

    When FatFs is configured for Unicode API (FF_LFN_UNICODE >= 1), character encoding on the string fuctions, f_putc, f_puts, f_printf and f_gets function, is also switched to Unicode. The input Unicode characters in multiple encoding unit, such as surrogate pair and multi-byte sequence, should not be divided into two function calls, or the character will be lost. The character encoding on the file to be written via this functions is selected by FF_STRF_ENCODE. If the character encoding differs between file data and API, it is converted in this function. Input characters with wrong encoding for output will be lost.

    @@ -56,6 +54,7 @@ int f_puts (

    f_open, f_putc, f_printf, f_gets, f_close, FIL

    +

    Return

    diff --git a/documents/doc/rc.html b/documents/doc/rc.html index 8f7fffa..89823ec 100644 --- a/documents/doc/rc.html +++ b/documents/doc/rc.html @@ -3,8 +3,6 @@ - - FatFs - API Return Code @@ -75,10 +73,10 @@ Note that if once this error occured in the operation to an open file, the file
    Name collision. An object with the same name is already existing in the directory.
    FR_INVALID_OBJECT
    -
    The file/directory object is invalid or a null pointer is given. There are some reasons as follows: +
    The file/directory object is invalid or the pointer is null. There are some reasons as follows:
      -
    • It has been closed, or the structure has been collapsed.
    • -
    • It has been invalidated. Open objects on the volume are invalidated by voulme mount process.
    • +
    • The file/directory has been closed.
    • +
    • The file/directory object has been invalidated or the structure has been collapsed. Open objects on the volume will be invalidated by a voulme mount process.
    • Physical drive is not ready to work due to a media removal.
    @@ -130,6 +128,7 @@ Note that if once this error occured in the operation to an open file, the file
    The given parameter is invalid or there is an inconsistent for the volume.
    +

    Return

    diff --git a/documents/doc/read.html b/documents/doc/read.html index 9dff623..0dd7f53 100644 --- a/documents/doc/read.html +++ b/documents/doc/read.html @@ -3,8 +3,6 @@ - - FatFs - f_read @@ -28,13 +26,13 @@ FRESULT f_read (

    Parameters

    fp
    -
    Pointer to the open file object.
    +
    Pointer to the open file object structure. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
    buff
    Pointer to the buffer to store the read data.
    btr
    Number of bytes to read in range of UINT type. If the file needs to be read fast, it should be read in large chunk as possible.
    br
    -
    Pointer to the UINT variable that receives number of bytes read. This value is always valid after the function call regardless of the function return code. If the return value is equal to btr, the function return code should be FR_OK.
    +
    Pointer to the variable in UINT type that receives number of bytes read. This value is always valid after the function call regardless of the function return code. If the return value is equal to btr, the function return code should be FR_OK.
    @@ -54,7 +52,7 @@ FRESULT f_read (

    Description

    -

    The function starts to read data from the file at the file offset pointed by read/write pointer. The read/write pointer advances as number of bytes read. After the function succeeded, *br should be checked to detect end of the file. In case of *br < btr, it means the read/write pointer hit end of the file during read operation.

    +

    The function starts to read data from the file at the file offset pointed by read/write pointer of the file object. The read/write pointer advances as number of bytes read. After the function succeeded, *br should be checked to detect end of the file. In case of *br < btr, it means the read/write pointer hit end of the file during read operation.

    @@ -75,6 +73,7 @@ FRESULT f_read (

    f_open, fgets, f_write, f_close, FIL

    +

    Return

    diff --git a/documents/doc/readdir.html b/documents/doc/readdir.html index 52a96b9..f7209a6 100644 --- a/documents/doc/readdir.html +++ b/documents/doc/readdir.html @@ -3,8 +3,6 @@ - - FatFs - f_readdir @@ -53,19 +51,19 @@ FRESULT f_rewinddir (

    Description

    -

    The f_readdir function reads a directory item, informations about the object, from the open directory. Items in the directory can be read in sequence by f_readdir function calls. When all items in the directory have been read and no item to read, a null string is stored into the fno->fname[] without any error. When a null pointer is given to the fno, the read index of the directory object is rewinded. The f_rewinddir function is implemented as a macro.

    +

    The f_readdir function reads a directory item, informations about the object, from the open directory. Items in the directory can be read by f_readdir function calls in order of the directory table. When all items in the directory have been read and no item to read any more, a null string in fno->fname[] will be returned without an error. If a null pointer is given to the fno, the read index of the directory object will be rewound. The f_rewinddir function is implemented as a macro.

     #define f_rewinddir(dp) f_readdir((dp), 0)
     
    -

    When LFN is enabled, a member altname[] is defined in the file information structure to store the short file name of the object. If the long file name is not accessible due to a reason listed below, short file name is stored to the fname[] and the altname[] has a null string.

    +

    When LFN is enabled, a member altname[] is defined in the file information structure to store the short file name of the object. If the long file name is not accessible due to any reason listed below, short file name is stored to the fname[] and the altname[] has a null string.

      -
    • The item has no LFN. (Not the case in exFAT volume)
    • -
    • FF_MAX_LFN is insufficient to handle the LFN. (Not the case in FF_MAX_LFN == 255)
    • +
    • The item has no LFN. (Not the case on the exFAT volume)
    • +
    • FF_MAX_LFN is insufficient to handle the LFN. (Not the case when FF_MAX_LFN == 255)
    • FF_LFN_BUF is insufficient to store the LFN.
    • -
    • The LFN contains some character not defined in current CP. (Not the case in FF_LFN_UNICODE != 0)
    • +
    • The LFN contains some character not defined in current CP. (Not the case when FF_LFN_UNICODE != 0)
    -

    There is an issue on read directories in exFAT volume. The exFAT does not support short file name. This means no name can be returned on the condition above. If it is the case, "?" is returned as the file name to indicate that the object is not accessible. To avoid this problem, configure FatFs FF_LFN_UNICODE != 0 and FF_MAX_LFN == 255 to support the full feature of LFN specification.

    -

    Dot entries ("." and "..") in the sub-directory of FAT volume are filtered out and they will never appear in the read items because exFAT lacks dot entries in the sub-directory.

    +

    There is an issue on read the directories on the exFAT volume. The exFAT does not support short file name. This means no name can be returned on the condition above. If it is the case, "?" is returned as the file name to indicate that the object is not accessible. To avoid this problem, configure FatFs FF_LFN_UNICODE != 0 and FF_MAX_LFN == 255 to support the full feature of LFN specification.

    +

    Dot entries ("." and "..") in the sub-directory of FAT volume are filtered out and they will never appear in the read items because of the consistency with exFAT which lacks dot entries in the sub-directory.

    @@ -78,6 +76,41 @@ FRESULT f_rewinddir (

    Sample Code

    +/* List contents of a directory */
    +
    +FRESULT list_dir (const char *path)
    +{
    +    FRESULT res;
    +    DIR dir;
    +    FILINFO fno;
    +    int nfile, ndir;
    +
    +
    +    res = f_opendir(&dir, path);                       /* Open the directory */
    +    if (res == FR_OK) {
    +        nfile = ndir = 0;
    +        for (;;) {
    +            res = f_readdir(&dir, &fno);                   /* Read a directory item */
    +            if (res != FR_OK || fno.fname[0] == 0) break;  /* Error or end of dir */
    +            if (fno.fattrib & AM_DIR) {            /* Directory */
    +                printf("   <DIR>   %s\n", fno.fname);
    +                ndir++;
    +            } else {                               /* File */
    +                printf("%10u %s\n", fno.fsize, fno.fname);
    +                nfile++;
    +            }
    +        }
    +        f_closedir(&dir);
    +        printf("%d dirs, %d files.\n", ndir, nfile);
    +    } else {
    +        printf("Failed to open \"%s\". (%u)\n", path, res);
    +    }
    +    return res;
    +}
    +
    +
    +/* Recursive scan of all items in the directory */
    +
     FRESULT scan_files (
         char* path        /* Start node to be scanned (***also used as work area***) */
     )
    @@ -88,7 +121,7 @@ FRESULT scan_files (
         static FILINFO fno;
     
     
    -    res = f_opendir(&dir, path);                       /* Open the directory */
    +    res = f_opendir(&dir, path);                       /* Open the directory */
         if (res == FR_OK) {
             for (;;) {
                 res = f_readdir(&dir, &fno);                   /* Read a directory item */
    @@ -103,7 +136,7 @@ FRESULT scan_files (
                     printf("%s/%s\n", path, fno.fname);
                 }
             }
    -        f_closedir(&dir);
    +        f_closedir(&dir);
         }
     
         return res;
    @@ -117,7 +150,7 @@ int main (void)
         char buff[256];
     
     
    -    res = f_mount(&fs, "", 1);
    +    res = f_mount(&fs, "", 1);
         if (res == FR_OK) {
             strcpy(buff, "/");
             res = scan_files(buff);
    @@ -134,6 +167,7 @@ int main (void)
     

    f_opendir, f_closedir, f_stat, FILINFO, DIR

    +

    Return

    diff --git a/documents/doc/rename.html b/documents/doc/rename.html index 326af0d..9919bd8 100644 --- a/documents/doc/rename.html +++ b/documents/doc/rename.html @@ -3,8 +3,6 @@ - - FatFs - f_rename diff --git a/documents/doc/sdir.html b/documents/doc/sdir.html index eb18cc3..d137a07 100644 --- a/documents/doc/sdir.html +++ b/documents/doc/sdir.html @@ -3,8 +3,6 @@ - - FatFs - DIR @@ -33,6 +31,7 @@ +

    Return

    diff --git a/documents/doc/setcp.html b/documents/doc/setcp.html index a1d124b..9d5c774 100644 --- a/documents/doc/setcp.html +++ b/documents/doc/setcp.html @@ -3,8 +3,6 @@ - - FatFs - f_setcp diff --git a/documents/doc/setlabel.html b/documents/doc/setlabel.html index 9eb5e78..e547bff 100644 --- a/documents/doc/setlabel.html +++ b/documents/doc/setlabel.html @@ -3,8 +3,6 @@ - - FatFs - f_setlabel @@ -25,7 +23,7 @@ FRESULT f_setlabel (

    Parameters

    label
    -
    Pointer to the null-terminated string that specifies the volume label to be set.
    +
    Pointer to the null-terminated string that specifies the volume label to be set. If a null pointer is given, the function fails with FR_INVALID_DRIVE.
    @@ -49,7 +47,7 @@ FRESULT f_setlabel (

    Description

    -

    When the string has a drive prefix, the volume label will be set to the volume specified by the drive prefix. Unix style volume ID cannot be used to specify the volume. If drive number is not specified, the volume label will be set to the default drive. If length of the given volume label is zero, the volume label on the volume will be removed. The format of the volume label is as shown below:

    +

    When the string has a drive prefix, the volume label will be set to the volume specified by the drive prefix. If drive number is not specified, the volume label will be set to the default drive. If length of the given volume label is zero, the volume label on the volume will be removed. The format of the volume label is as shown below:

    • Up to 11 bytes long as conversion of OEM code page at FAT volume.
    • Up to 11 characters long at exFAT volume.
    • @@ -77,6 +75,9 @@ FRESULT f_setlabel ( /* Remove volume label of the drive 2 */ f_setlabel("2:"); + + /* Set volume label in Unix style volume id */ + f_setlabel("/tfcard/LOG DISK");
    diff --git a/documents/doc/sfatfs.html b/documents/doc/sfatfs.html index 6ecd6e5..b002ccf 100644 --- a/documents/doc/sfatfs.html +++ b/documents/doc/sfatfs.html @@ -3,8 +3,6 @@ - - FatFs - FATFS diff --git a/documents/doc/sfile.html b/documents/doc/sfile.html index 9234d32..c361483 100644 --- a/documents/doc/sfile.html +++ b/documents/doc/sfile.html @@ -3,8 +3,6 @@ - - FatFs - FIL @@ -38,6 +36,7 @@ +

    Return

    diff --git a/documents/doc/sfileinfo.html b/documents/doc/sfileinfo.html index db0f45e..ca40ac3 100644 --- a/documents/doc/sfileinfo.html +++ b/documents/doc/sfileinfo.html @@ -3,8 +3,6 @@ - - FatFs - FILINFO @@ -73,6 +71,7 @@
    Alternative object name is stored if available. This member is not available in non-LFN configuration.
    +

    Return

    diff --git a/documents/doc/size.html b/documents/doc/size.html index 0be1a76..6946145 100644 --- a/documents/doc/size.html +++ b/documents/doc/size.html @@ -3,8 +3,6 @@ - - FatFs - f_size @@ -57,6 +55,7 @@ FSIZE_t f_size (

    f_open, f_lseek, FIL

    +

    Return

    diff --git a/documents/doc/stat.html b/documents/doc/stat.html index a4e4fc8..841fef1 100644 --- a/documents/doc/stat.html +++ b/documents/doc/stat.html @@ -3,8 +3,6 @@ - - FatFs - f_stat @@ -26,7 +24,7 @@ FRESULT f_stat (

    Parameters

    path
    -
    Pointer to the null-terminated string that specifies the object to get its information. The object must not be the root direcotry.
    +
    Pointer to the null-terminated string that specifies the object to get its information. The object must not be the root direcotry. If a null pointer is given, the function fails with FR_INVALID_DRIVE.
    fno
    Pointer to the blank FILINFO structure to store the information of the object. Set null pointer if this information is not needed.
    @@ -54,8 +52,8 @@ FRESULT f_stat (

    Description

    -

    The f_stat function checks the existence of a file or sub-directory in the directory. If it is not exist, the function returns with FR_NO_FILE. If it is exist, the function returns with FR_OK and the informations about the object, size, timestamp and attribute, is stored to the file information structure. For details of the file information, refer to the FILINFO structure and f_readdir function.

    -

    Note that the file information comes from the meta data in the directory. If the file has been opend and modified, the file will need to be synched or closed in order to obtain the latest file information.

    +

    The f_stat function checks the existence of a file or sub-directory. If it is not exist, the function returns with FR_NO_FILE. If it is exist, the function returns with FR_OK and the informations about the object, size, timestamp and attribute, is stored to the file information structure. For details of the file information, refer to the FILINFO structure and f_readdir function.

    +

    Note that the file information comes from the meta data in the directory. If the file has been opend and modified, the file will need to be synced or closed in order to obtain the latest file information. This function cannot be used to retrieve the long file name with the short file name.

    @@ -92,6 +90,7 @@ FRESULT f_stat ( break; case FR_NO_FILE: + case FR_NO_PATH: printf("\"%s\" is not exist.\n", fname); break; @@ -107,6 +106,7 @@ FRESULT f_stat (

    f_opendir, f_readdir, FILINFO

    +

    Return

    diff --git a/documents/doc/sync.html b/documents/doc/sync.html index 9fb42a0..f135c5b 100644 --- a/documents/doc/sync.html +++ b/documents/doc/sync.html @@ -3,8 +3,6 @@ - - FatFs - f_sync @@ -68,7 +66,7 @@ C - f_close() w - f_write() S - f_sync() -

    However there is no sense in f_sync function immediataly before f_close function because it performs f_sync function in it. In other words, the differnce between those functions is that the file object is invalidated or not.

    +

    However there is no sense in f_sync function jsut before f_close function, because the f_close performs f_sync in it. Actually, the differnce between these functions is that the file object is invalidated or not.

    @@ -83,6 +81,7 @@ S - f_sync()

    f_close, Critical section

    +

    Return

    diff --git a/documents/doc/tell.html b/documents/doc/tell.html index 075f033..c40ebe5 100644 --- a/documents/doc/tell.html +++ b/documents/doc/tell.html @@ -3,8 +3,6 @@ - - FatFs - f_tell @@ -57,6 +55,7 @@ FSIZE_t f_tell (

    f_open, f_lseek, FIL

    +

    Return

    diff --git a/documents/doc/truncate.html b/documents/doc/truncate.html index 0ac012f..bbc335b 100644 --- a/documents/doc/truncate.html +++ b/documents/doc/truncate.html @@ -3,8 +3,6 @@ - - FatFs - f_truncate @@ -25,7 +23,7 @@ FRESULT f_truncate (

    Parameter

    fp
    -
    Pointer to the open file object to be truncated.
    +
    Pointer to the open file object to be truncated. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
    diff --git a/documents/doc/unlink.html b/documents/doc/unlink.html index 7809648..f8e1d20 100644 --- a/documents/doc/unlink.html +++ b/documents/doc/unlink.html @@ -3,8 +3,6 @@ - - FatFs - f_unlink @@ -25,7 +23,7 @@ FRESULT f_unlink (

    Parameter

    path
    -
    Pointer to a null-terminated string that specifies the file or sub-directory to be removed.
    +
    Pointer to a null-terminated string that specifies the file or sub-directory to be removed. If a null pointer is given, the function fails with FR_INVALID_DRIVE.
    diff --git a/documents/doc/utime.html b/documents/doc/utime.html index 40e39eb..4d8f78b 100644 --- a/documents/doc/utime.html +++ b/documents/doc/utime.html @@ -3,8 +3,6 @@ - - FatFs - f_utime @@ -26,7 +24,7 @@ FRESULT f_utime (

    Parameters

    path
    -
    Pointer to the null-terminated string that specifies an object to be changed.
    +
    Pointer to the null-terminated string that specifies an object to be changed. If a null pointer is given, the function fails with FR_INVALID_DRIVE.
    fno
    Pointer to the file information structure that has a timestamp to be set in member fdate and ftime. Do not care any other members.
    @@ -94,6 +92,7 @@ FRESULT set_timestamp (

    f_stat, FILINFO

    +

    Return

    diff --git a/documents/doc/write.html b/documents/doc/write.html index 3d16674..78d525e 100644 --- a/documents/doc/write.html +++ b/documents/doc/write.html @@ -3,8 +3,6 @@ - - FatFs - f_write @@ -28,13 +26,13 @@ FRESULT f_write (

    Parameters

    fp
    -
    Pointer to the open file object structure.
    +
    Pointer to the open file object structure. If a null pointer is given, the function fails with FR_INVALID_OBJECT.
    buff
    Pointer to the data to be written.
    btw
    Specifies number of bytes to write in range of UINT type. If the data needs to be written fast, it should be written in large chunk as possible.
    bw
    -
    Pointer to the UINT variable that receives the number of bytes written. This value is always valid after the function call regardless of the function return code. If the return value is equal to btw, the function return code should be FR_OK.
    +
    Pointer to the variable in UINT type that receives the number of bytes written. This value is always valid after the function call regardless of the function return code. If the return value is equal to btw, the function return code should be FR_OK.
    @@ -54,7 +52,7 @@ FRESULT f_write (

    Description

    -

    The function starts to write data to the file at the file offset pointed by read/write pointer. The read/write pointer advances as number of bytes written. After the function succeeded, *bw should be checked to detect the disk full. In case of *bw < btw, it means the volume got full during the write operation. The function can take a time when the volume is full or close to full.

    +

    The function starts to write data to the file at the file offset pointed by read/write pointer of the file object. The read/write pointer advances as number of bytes written. After the function succeeded, *bw should be checked to detect the disk full. In case of *bw < btw, it means the volume got full during the write operation. The function can take a time when the volume is full or close to full.

    @@ -75,6 +73,7 @@ FRESULT f_write (

    f_open, f_read, fputc, fputs, fprintf, f_close, FIL

    +

    Return

    diff --git a/documents/updates.html b/documents/updates.html index 29d881f..24b8fd4 100644 --- a/documents/updates.html +++ b/documents/updates.html @@ -6,7 +6,7 @@ -FatFs - Known Problems +FatFs - Updates and Migration Notes

    Updates and Migration Notes

    @@ -23,7 +23,7 @@ Fixed string functions cannot write the unicode characters not in BMP when F Fixed a compatibility issue in identification of GPT header.
    -User provided synchronization functions, ff_cre_syncobj, ff_del_syncobj, ff_req_grant and ff_rel_grant, needed when FF_FS_REENTRANT are replaced by ff_mutex_create, ff_mutex_delete, ff_mutex_take and ff_mutex_give respectively. For examples, see ffsystem.c.
    +User provided synchronization functions, ff_cre_syncobj, ff_del_syncobj, ff_req_grant and ff_rel_grant, needed when FF_FS_REENTRANT are replaced with ff_mutex_create, ff_mutex_delete, ff_mutex_take and ff_mutex_give respectively. For example, see ffsystem.c.
    FF_SYNC_t is removed from the configuration options.
    @@ -172,10 +172,12 @@ Fixed errors in the case conversion teble of Unicode (cc*.c).
    Usage and members of FINFO sructure used in f_readdir is changed.
    +Dot entries in the sub-directory are never appear in f_readdir.
    +".." does not work as path name in exFAT volume.
    +f_getcwd does not work in exFAT volume.
    Many members in FIL and DIR structure are changed.
    To use f_chmod, _USE_CHMOD needs to be set.
    _WORD_ACCESS is removed from the configuration options.
    -Dot entries are never appear in f_reeddir.
    diff --git a/source/00history.txt b/source/00history.txt index 4fa1d7a..29c76b0 100644 --- a/source/00history.txt +++ b/source/00history.txt @@ -367,3 +367,11 @@ R0.15 (November 6, 2022) Fixed string functions cannot write the unicode characters not in BMP when FF_LFN_UNICODE == 2 (UTF-8). Fixed a compatibility issue in identification of GPT header. + + +R0.15a (November 22, 2024) + Fixed a complie error when FF_FS_LOCK != 0. + Fixed a potential issue when work FatFs concurrency with FF_FS_REENTRANT, FF_VOLUMES >= 2 and FF_FS_LOCK > 0. + Made f_setlabel() accept a volume label in Unix style volume ID when FF_STR_VOLUME_ID == 2. + Made FatFs update PercInUse field in exFAT VBR. (A preceding f_getfree() is needed for the accuracy) + diff --git a/source/ff.c b/source/ff.c index 2091847..ccd7b15 100644 --- a/source/ff.c +++ b/source/ff.c @@ -1,8 +1,8 @@ /*----------------------------------------------------------------------------/ -/ FatFs - Generic FAT Filesystem Module R0.15 w/patch3 / +/ FatFs - Generic FAT Filesystem Module R0.15a / /-----------------------------------------------------------------------------/ / -/ Copyright (C) 2022, ChaN, all right reserved. +/ Copyright (C) 2024, ChaN, all right reserved. / / FatFs module is an open source software. Redistribution and use of FatFs in / source and binary forms, with or without modification, are permitted provided @@ -30,17 +30,17 @@ ---------------------------------------------------------------------------*/ -#if FF_DEFINED != 80286 /* Revision ID */ +#if FF_DEFINED != 5380 /* Revision ID */ #error Wrong include file (ff.h). #endif /* Limits and boundaries */ -#define MAX_DIR 0x200000 /* Max size of FAT directory */ -#define MAX_DIR_EX 0x10000000 /* Max size of exFAT directory */ +#define MAX_DIR 0x200000 /* Max size of FAT directory (byte) */ +#define MAX_DIR_EX 0x10000000 /* Max size of exFAT directory (byte) */ #define MAX_FAT12 0xFF5 /* Max FAT12 clusters (differs from specs, but right for real DOS/Windows behavior) */ #define MAX_FAT16 0xFFF5 /* Max FAT16 clusters (differs from specs, but right for real DOS/Windows behavior) */ -#define MAX_FAT32 0x0FFFFFF5 /* Max FAT32 clusters (not specified, practical limit) */ +#define MAX_FAT32 0x0FFFFFF5 /* Max FAT32 clusters (not defined in specs, practical limit) */ #define MAX_EXFAT 0x7FFFFFFD /* Max exFAT clusters (differs from specs, implementation limit) */ @@ -89,7 +89,7 @@ #define ET_FILENAME 0xC1 /* Name extension */ -/* FatFs refers the FAT structure as simple byte array instead of structure member +/* FatFs refers the FAT structures as simple byte array instead of structure member / because the C structure is not binary compatible between different platforms */ #define BS_JmpBoot 0 /* x86 jump instruction (3-byte) */ @@ -113,7 +113,7 @@ #define BS_VolLab 43 /* Volume label string (8-byte) */ #define BS_FilSysType 54 /* Filesystem type string (8-byte) */ #define BS_BootCode 62 /* Boot code (448-byte) */ -#define BS_55AA 510 /* Signature word (WORD) */ +#define BS_55AA 510 /* Boot signature (WORD, for VBR and MBR) */ #define BPB_FATSz32 36 /* FAT32: FAT size [sector] (DWORD) */ #define BPB_ExtFlags32 40 /* FAT32: Extended flags (WORD) */ @@ -139,18 +139,18 @@ #define BPB_RootClusEx 96 /* exFAT: Root directory start cluster (DWORD) */ #define BPB_VolIDEx 100 /* exFAT: Volume serial number (DWORD) */ #define BPB_FSVerEx 104 /* exFAT: Filesystem version (WORD) */ -#define BPB_VolFlagEx 106 /* exFAT: Volume flags (WORD) */ +#define BPB_VolFlagEx 106 /* exFAT: Volume flags (WORD, out of check sum calculation) */ #define BPB_BytsPerSecEx 108 /* exFAT: Log2 of sector size in unit of byte (BYTE) */ #define BPB_SecPerClusEx 109 /* exFAT: Log2 of cluster size in unit of sector (BYTE) */ #define BPB_NumFATsEx 110 /* exFAT: Number of FATs (BYTE) */ #define BPB_DrvNumEx 111 /* exFAT: Physical drive number for int13h (BYTE) */ -#define BPB_PercInUseEx 112 /* exFAT: Percent in use (BYTE) */ +#define BPB_PercInUseEx 112 /* exFAT: Percent in use (BYTE, out of check sum calculation) */ #define BPB_RsvdEx 113 /* exFAT: Reserved (7-byte) */ #define BS_BootCodeEx 120 /* exFAT: Boot code (390-byte) */ #define DIR_Name 0 /* Short file name (11-byte) */ #define DIR_Attr 11 /* Attribute (BYTE) */ -#define DIR_NTres 12 /* Lower case flag (BYTE) */ +#define DIR_NTres 12 /* Low case flags of SFN (BYTE) */ #define DIR_CrtTime10 13 /* Created time sub-second (BYTE) */ #define DIR_CrtTime 14 /* Created time (DWORD) */ #define DIR_LstAccDate 18 /* Last accessed date (WORD) */ @@ -194,17 +194,18 @@ #define FSI_StrucSig 484 /* FAT32 FSI: Structure signature (DWORD) */ #define FSI_Free_Count 488 /* FAT32 FSI: Number of free clusters (DWORD) */ #define FSI_Nxt_Free 492 /* FAT32 FSI: Last allocated cluster (DWORD) */ +#define FSI_TrailSig 498 /* FAT32 FSI: Trailing signature (DWORD) */ #define MBR_Table 446 /* MBR: Offset of partition table in the MBR */ #define SZ_PTE 16 /* MBR: Size of a partition table entry */ #define PTE_Boot 0 /* MBR PTE: Boot indicator */ -#define PTE_StHead 1 /* MBR PTE: Start head */ -#define PTE_StSec 2 /* MBR PTE: Start sector */ -#define PTE_StCyl 3 /* MBR PTE: Start cylinder */ +#define PTE_StHead 1 /* MBR PTE: Start head in CHS */ +#define PTE_StSec 2 /* MBR PTE: Start sector in CHS */ +#define PTE_StCyl 3 /* MBR PTE: Start cylinder in CHS */ #define PTE_System 4 /* MBR PTE: System ID */ -#define PTE_EdHead 5 /* MBR PTE: End head */ -#define PTE_EdSec 6 /* MBR PTE: End sector */ -#define PTE_EdCyl 7 /* MBR PTE: End cylinder */ +#define PTE_EdHead 5 /* MBR PTE: End head in CHS */ +#define PTE_EdSec 6 /* MBR PTE: End sector in CHS */ +#define PTE_EdCyl 7 /* MBR PTE: End cylinder in CHS */ #define PTE_StLba 8 /* MBR PTE: Start in LBA */ #define PTE_SizLba 12 /* MBR PTE: Size in LBA */ @@ -245,10 +246,10 @@ #endif -/* Definitions of logical drive - physical location conversion */ +/* Definitions of logical drive to physical location conversion */ #if FF_MULTI_PARTITION -#define LD2PD(vol) VolToPart[vol].pd /* Get physical drive number */ -#define LD2PT(vol) VolToPart[vol].pt /* Get partition number (0:auto search, 1..:forced partition number) */ +#define LD2PD(vol) VolToPart[vol].pd /* Get physical drive number from the mapping table */ +#define LD2PT(vol) VolToPart[vol].pt /* Get partition number from the mapping table (0:auto search, 1-:forced partition number) */ #else #define LD2PD(vol) (BYTE)(vol) /* Each logical drive is associated with the same physical drive number */ #define LD2PT(vol) 0 /* Auto partition search */ @@ -282,11 +283,11 @@ #if FF_FS_READONLY #error FF_FS_LOCK must be 0 at read-only configuration #endif -typedef struct { - FATFS* fs; /* Object ID 1, volume (NULL:blank entry) */ - DWORD clu; /* Object ID 2, containing directory (0:root) */ - DWORD ofs; /* Object ID 3, offset in the directory */ - UINT ctr; /* Object open counter, 0:none, 0x01..0xFF:read mode open count, 0x100:write mode */ +typedef struct { /* Open object identifier with status */ + FATFS* fs; /* Object ID 1, volume (NULL:blank entry) */ + DWORD clu; /* Object ID 2, containing directory (0:root) */ + DWORD ofs; /* Object ID 3, offset in the directory */ + UINT ctr; /* Object open status, 0:none, 0x01..0xFF:read mode open count, 0x100:write mode */ } FILESEM; #endif @@ -465,7 +466,7 @@ static FATFS *FatFs[FF_VOLUMES]; /* Pointer to the filesystem objects (logical d static WORD Fsid; /* Filesystem mount ID */ #if FF_FS_RPATH != 0 -static BYTE CurrVol; /* Current drive set by f_chdrive() */ +static BYTE CurrVol; /* Current drive number set by f_chdrive() */ #endif #if FF_FS_LOCK @@ -567,8 +568,8 @@ static WCHAR LfnBuf[FF_MAX_LFN + 1]; /* LFN working buffer */ #if FF_CODE_PAGE == 0 /* Run-time code page configuration */ #define CODEPAGE CodePage static WORD CodePage; /* Current code page */ -static const BYTE* ExCvt; /* Ptr to SBCS up-case table Ct???[] (null:not used) */ -static const BYTE* DbcTbl; /* Ptr to DBCS code range table Dc???[] (null:not used) */ +static const BYTE* ExCvt; /* Pointer to SBCS up-case table Ct???[] (null:disabled) */ +static const BYTE* DbcTbl; /* Pointer to DBCS code range table Dc???[] (null:disabled) */ static const BYTE Ct437[] = TBL_CT437; static const BYTE Ct720[] = TBL_CT720; @@ -744,7 +745,7 @@ static DWORD tchar2uni ( /* Returns a character in UTF-16 encoding (>=0x10000 on #if FF_LFN_UNICODE == 1 /* UTF-16 input */ WCHAR wc; - uc = *p++; /* Get a unit */ + uc = *p++; /* Get an encoding unit */ if (IsSurrogate(uc)) { /* Surrogate? */ wc = *p++; /* Get low surrogate */ if (!IsSurrogateH(uc) || !IsSurrogateL(wc)) return 0xFFFFFFFF; /* Wrong surrogate? */ @@ -752,7 +753,7 @@ static DWORD tchar2uni ( /* Returns a character in UTF-16 encoding (>=0x10000 on } #elif FF_LFN_UNICODE == 2 /* UTF-8 input */ - BYTE b; + BYTE tb; int nf; uc = (BYTE)*p++; /* Get an encoding unit */ @@ -766,10 +767,10 @@ static DWORD tchar2uni ( /* Returns a character in UTF-16 encoding (>=0x10000 on } else { /* Wrong sequence */ return 0xFFFFFFFF; } - do { /* Get trailing bytes */ - b = (BYTE)*p++; - if ((b & 0xC0) != 0x80) return 0xFFFFFFFF; /* Wrong sequence? */ - uc = uc << 6 | (b & 0x3F); + do { /* Get and merge trailing bytes */ + tb = (BYTE)*p++; + if ((tb & 0xC0) != 0x80) return 0xFFFFFFFF; /* Wrong sequence? */ + uc = uc << 6 | (tb & 0x3F); } while (--nf != 0); if (uc < 0x80 || IsSurrogate(uc) || uc >= 0x110000) return 0xFFFFFFFF; /* Wrong code? */ if (uc >= 0x010000) uc = 0xD800DC00 | ((uc - 0x10000) << 6 & 0x3FF0000) | (uc & 0x3FF); /* Make a surrogate pair if needed */ @@ -781,14 +782,14 @@ static DWORD tchar2uni ( /* Returns a character in UTF-16 encoding (>=0x10000 on if (uc >= 0x010000) uc = 0xD800DC00 | ((uc - 0x10000) << 6 & 0x3FF0000) | (uc & 0x3FF); /* Make a surrogate pair if needed */ #else /* ANSI/OEM input */ - BYTE b; + BYTE sb; WCHAR wc; wc = (BYTE)*p++; /* Get a byte */ if (dbc_1st((BYTE)wc)) { /* Is it a DBC 1st byte? */ - b = (BYTE)*p++; /* Get 2nd byte */ - if (!dbc_2nd(b)) return 0xFFFFFFFF; /* Invalid code? */ - wc = (wc << 8) + b; /* Make a DBC */ + sb = (BYTE)*p++; /* Get 2nd byte */ + if (!dbc_2nd(sb)) return 0xFFFFFFFF; /* Invalid code? */ + wc = (wc << 8) + sb; /* Make a DBC */ } if (wc != 0) { wc = ff_oem2uni(wc, CODEPAGE); /* ANSI/OEM ==> Unicode */ @@ -880,7 +881,7 @@ static UINT put_utf ( /* Returns number of encoding units written (0:buffer over *buf++ = (TCHAR)wc; /* Store DBC 2nd byte */ return 2; } - if (wc == 0 || szb < 1) return 0; /* Invalid char or buffer overflow? */ + if (wc == 0 || szb < 1) return 0; /* Invalid character or buffer overflow? */ *buf++ = (TCHAR)wc; /* Store the character */ return 1; #endif @@ -941,7 +942,7 @@ static void unlock_volume ( #if FF_FS_LOCK /*-----------------------------------------------------------------------*/ -/* File shareing control functions */ +/* File sharing control functions */ /*-----------------------------------------------------------------------*/ static FRESULT chk_share ( /* Check if the file can be accessed */ @@ -1116,17 +1117,30 @@ static FRESULT sync_fs ( /* Returns FR_OK or FR_DISK_ERR */ res = sync_window(fs); if (res == FR_OK) { - if (fs->fs_type == FS_FAT32 && fs->fsi_flag == 1) { /* FAT32: Update FSInfo sector if needed */ - /* Create FSInfo structure */ - memset(fs->win, 0, sizeof fs->win); - st_word(fs->win + BS_55AA, 0xAA55); /* Boot signature */ - st_dword(fs->win + FSI_LeadSig, 0x41615252); /* Leading signature */ - st_dword(fs->win + FSI_StrucSig, 0x61417272); /* Structure signature */ - st_dword(fs->win + FSI_Free_Count, fs->free_clst); /* Number of free clusters */ - st_dword(fs->win + FSI_Nxt_Free, fs->last_clst); /* Last allocated culuster */ - fs->winsect = fs->volbase + 1; /* Write it into the FSInfo sector (Next to VBR) */ - disk_write(fs->pdrv, fs->win, fs->winsect, 1); + if (fs->fsi_flag == 1) { /* Allocation changed? */ fs->fsi_flag = 0; + if (fs->fs_type == FS_FAT32) { /* FAT32: Update FSInfo sector */ + /* Create FSInfo structure */ + memset(fs->win, 0, sizeof fs->win); + st_dword(fs->win + FSI_LeadSig, 0x41615252); /* Leading signature */ + st_dword(fs->win + FSI_StrucSig, 0x61417272); /* Structure signature */ + st_dword(fs->win + FSI_Free_Count, fs->free_clst); /* Number of free clusters */ + st_dword(fs->win + FSI_Nxt_Free, fs->last_clst); /* Last allocated culuster */ + st_dword(fs->win + FSI_TrailSig, 0xAA550000); /* Trailing signature */ + disk_write(fs->pdrv, fs->win, fs->winsect = fs->volbase + 1, 1); /* Write it into the FSInfo sector (Next to VBR) */ + } +#if FF_FS_EXFAT + else if (fs->fs_type == FS_EXFAT) { /* exFAT: Update PercInUse field in BPB */ + if (disk_read(fs->pdrv, fs->win, fs->winsect = fs->volbase, 1) == RES_OK) { /* Load VBR */ + BYTE perc_inuse = (fs->free_clst <= fs->n_fatent - 2) ? (BYTE)((QWORD)(fs->n_fatent - 2 - fs->free_clst) * 100 / (fs->n_fatent - 2)) : 0xFF; /* Precent in use 0-100 or 0xFF(unknown) */ + + if (fs->win[BPB_PercInUseEx] != perc_inuse) { /* Write it back into VBR if needed */ + fs->win[BPB_PercInUseEx] = perc_inuse; + disk_write(fs->pdrv, fs->win, fs->winsect, 1); + } + } + } +#endif } /* Make sure that no pending write process in the lower layer */ if (disk_ioctl(fs->pdrv, CTRL_SYNC, 0) != RES_OK) res = FR_DISK_ERR; @@ -1462,7 +1476,7 @@ static FRESULT remove_chain ( /* FR_OK(0):succeeded, !=0:error */ res = put_fat(fs, clst, 0); /* Mark the cluster 'free' on the FAT */ if (res != FR_OK) return res; } - if (fs->free_clst < fs->n_fatent - 2) { /* Update FSINFO */ + if (fs->free_clst < fs->n_fatent - 2) { /* Update allocation information if it is valid */ fs->free_clst++; fs->fsi_flag |= 1; } @@ -1485,7 +1499,7 @@ static FRESULT remove_chain ( /* FR_OK(0):succeeded, !=0:error */ } #endif clst = nxt; /* Next cluster */ - } while (clst < fs->n_fatent); /* Repeat while not the last link */ + } while (clst < fs->n_fatent); /* Repeat until the last link */ #if FF_FS_EXFAT /* Some post processes for chain status */ @@ -1605,10 +1619,12 @@ static DWORD create_chain ( /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:D } } - if (res == FR_OK) { /* Update FSINFO if function succeeded. */ + if (res == FR_OK) { /* Update allocation information if the function succeeded */ fs->last_clst = ncl; - if (fs->free_clst <= fs->n_fatent - 2) fs->free_clst--; - fs->fsi_flag |= 1; + if (fs->free_clst > 0 && fs->free_clst <= fs->n_fatent - 2) { + fs->free_clst--; + fs->fsi_flag |= 1; + } } else { ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1; /* Failed. Generate error status */ } @@ -1885,31 +1901,31 @@ static void st_clust ( /*--------------------------------------------------------*/ static int cmp_lfn ( /* 1:matched, 0:not matched */ - const WCHAR* lfnbuf, /* Pointer to the LFN working buffer to be compared */ - BYTE* dir /* Pointer to the directory entry containing the part of LFN */ + const WCHAR* lfnbuf, /* Pointer to the LFN to be compared */ + BYTE* dir /* Pointer to the LFN entry */ ) { - UINT i, s; - WCHAR wc, uc; + UINT ni, di; + WCHAR pchr, chr; - if (ld_word(dir + LDIR_FstClusLO) != 0) return 0; /* Check LDIR_FstClusLO */ + if (ld_word(dir + LDIR_FstClusLO) != 0) return 0; /* Check if LDIR_FstClusLO is 0 */ - i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */ + ni = (UINT)((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the name to be compared */ - for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */ - uc = ld_word(dir + LfnOfs[s]); /* Pick an LFN character */ - if (wc != 0) { - if (i >= FF_MAX_LFN + 1 || ff_wtoupper(uc) != ff_wtoupper(lfnbuf[i++])) { /* Compare it */ + for (pchr = 1, di = 0; di < 13; di++) { /* Process all characters in the entry */ + chr = ld_word(dir + LfnOfs[di]); /* Pick a character from the entry */ + if (pchr != 0) { + if (ni >= FF_MAX_LFN + 1 || ff_wtoupper(chr) != ff_wtoupper(lfnbuf[ni++])) { /* Compare it with name */ return 0; /* Not matched */ } - wc = uc; + pchr = chr; } else { - if (uc != 0xFFFF) return 0; /* Check filler */ + if (chr != 0xFFFF) return 0; /* Check filler */ } } - if ((dir[LDIR_Ord] & LLEF) && wc && lfnbuf[i]) return 0; /* Last segment matched but different length */ + if ((dir[LDIR_Ord] & LLEF) && pchr && lfnbuf[ni]) return 0; /* Last name segment matched but different length */ return 1; /* The part of LFN matched */ } @@ -1921,31 +1937,31 @@ static int cmp_lfn ( /* 1:matched, 0:not matched */ /*-----------------------------------------------------*/ static int pick_lfn ( /* 1:succeeded, 0:buffer overflow or invalid LFN entry */ - WCHAR* lfnbuf, /* Pointer to the LFN working buffer */ - BYTE* dir /* Pointer to the LFN entry */ + WCHAR* lfnbuf, /* Pointer to the name buffer to be stored */ + const BYTE* dir /* Pointer to the LFN entry */ ) { - UINT i, s; - WCHAR wc, uc; + UINT ni, di; + WCHAR pchr, chr; - if (ld_word(dir + LDIR_FstClusLO) != 0) return 0; /* Check LDIR_FstClusLO is 0 */ + if (ld_word(dir + LDIR_FstClusLO) != 0) return 0; /* Check if LDIR_FstClusLO is 0 */ - i = ((dir[LDIR_Ord] & ~LLEF) - 1) * 13; /* Offset in the LFN buffer */ + ni = (UINT)((dir[LDIR_Ord] & ~LLEF) - 1) * 13; /* Offset in the name buffer */ - for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */ - uc = ld_word(dir + LfnOfs[s]); /* Pick an LFN character */ - if (wc != 0) { - if (i >= FF_MAX_LFN + 1) return 0; /* Buffer overflow? */ - lfnbuf[i++] = wc = uc; /* Store it */ + for (pchr = 1, di = 0; di < 13; di++) { /* Process all characters in the entry */ + chr = ld_word(dir + LfnOfs[di]); /* Pick a character from the entry */ + if (pchr != 0) { + if (ni >= FF_MAX_LFN + 1) return 0; /* Buffer overflow? */ + lfnbuf[ni++] = pchr = chr; /* Store it */ } else { - if (uc != 0xFFFF) return 0; /* Check filler */ + if (chr != 0xFFFF) return 0; /* Check filler */ } } - if (dir[LDIR_Ord] & LLEF && wc != 0) { /* Put terminator if it is the last LFN part and not terminated */ - if (i >= FF_MAX_LFN + 1) return 0; /* Buffer overflow? */ - lfnbuf[i] = 0; + if (dir[LDIR_Ord] & LLEF && pchr != 0) { /* Put terminator if it is the last LFN part and not terminated */ + if (ni >= FF_MAX_LFN + 1) return 0; /* Buffer overflow? */ + lfnbuf[ni] = 0; } return 1; /* The part of LFN is valid */ @@ -1965,24 +1981,24 @@ static void put_lfn ( BYTE sum /* Checksum of the corresponding SFN */ ) { - UINT i, s; - WCHAR wc; + UINT ni, di; + WCHAR chr; dir[LDIR_Chksum] = sum; /* Set checksum */ - dir[LDIR_Attr] = AM_LFN; /* Set attribute. LFN entry */ + dir[LDIR_Attr] = AM_LFN; /* Set attribute */ dir[LDIR_Type] = 0; st_word(dir + LDIR_FstClusLO, 0); - i = (ord - 1) * 13; /* Get offset in the LFN working buffer */ - s = wc = 0; - do { - if (wc != 0xFFFF) wc = lfn[i++]; /* Get an effective character */ - st_word(dir + LfnOfs[s], wc); /* Put it */ - if (wc == 0) wc = 0xFFFF; /* Padding characters for following items */ - } while (++s < 13); - if (wc == 0xFFFF || !lfn[i]) ord |= LLEF; /* Last LFN part is the start of LFN sequence */ - dir[LDIR_Ord] = ord; /* Set the LFN order */ + ni = (UINT)(ord - 1) * 13; /* Offset in the name */ + di = chr = 0; + do { /* Fill the directory entry */ + if (chr != 0xFFFF) chr = lfn[ni++]; /* Get an effective character */ + st_word(dir + LfnOfs[di], chr); /* Set it */ + if (chr == 0) chr = 0xFFFF; /* Padding characters after the terminator */ + } while (++di < 13); + if (chr == 0xFFFF || !lfn[ni]) ord |= LLEF; /* Last LFN part is the start of an enrty set */ + dir[LDIR_Ord] = ord; /* Set order in the entry set */ } #endif /* !FF_FS_READONLY */ @@ -2005,25 +2021,25 @@ static void gen_numname ( BYTE ns[8], c; UINT i, j; WCHAR wc; - DWORD sreg; + DWORD crc_sreg; memcpy(dst, src, 11); /* Prepare the SFN to be modified */ if (seq > 5) { /* In case of many collisions, generate a hash number instead of sequential number */ - sreg = seq; - while (*lfn) { /* Create a CRC as hash value */ + crc_sreg = seq; + while (*lfn) { /* Create a CRC value as a hash of LFN */ wc = *lfn++; for (i = 0; i < 16; i++) { - sreg = (sreg << 1) + (wc & 1); + crc_sreg = (crc_sreg << 1) + (wc & 1); wc >>= 1; - if (sreg & 0x10000) sreg ^= 0x11021; + if (crc_sreg & 0x10000) crc_sreg ^= 0x11021; } } - seq = (UINT)sreg; + seq = (UINT)crc_sreg; } - /* Make suffix (~ + hexadecimal) */ + /* Make suffix (~ + hexdecimal) */ i = 7; do { c = (BYTE)((seq % 16) + '0'); seq /= 16; @@ -2082,7 +2098,7 @@ static WORD xdir_sum ( /* Get checksum of the directoly entry block */ WORD sum; - szblk = (dir[XDIR_NumSec] + 1) * SZDIRE; /* Number of bytes of the entry block */ + szblk = ((UINT)dir[XDIR_NumSec] + 1) * SZDIRE; /* Number of bytes of the entry block */ for (i = sum = 0; i < szblk; i++) { if (i == XDIR_SetSum) { /* Skip 2-byte sum field */ i++; @@ -2138,13 +2154,13 @@ static FRESULT load_xdir ( /* FR_INT_ERR: invalid entry block */ BYTE *dirb = dp->obj.fs->dirbuf; /* Pointer to the on-memory directory entry block 85+C0+C1s */ - /* Load file directory entry */ + /* Load file-directory entry */ res = move_window(dp->obj.fs, dp->sect); if (res != FR_OK) return res; - if (dp->dir[XDIR_Type] != ET_FILEDIR) return FR_INT_ERR; /* Invalid order */ + if (dp->dir[XDIR_Type] != ET_FILEDIR) return FR_INT_ERR; /* Invalid order? */ memcpy(dirb + 0 * SZDIRE, dp->dir, SZDIRE); - sz_ent = (dirb[XDIR_NumSec] + 1) * SZDIRE; - if (sz_ent < 3 * SZDIRE || sz_ent > 19 * SZDIRE) return FR_INT_ERR; + sz_ent = ((UINT)dirb[XDIR_NumSec] + 1) * SZDIRE; /* Size of this entry block */ + if (sz_ent < 3 * SZDIRE || sz_ent > 19 * SZDIRE) return FR_INT_ERR; /* Invalid block size? */ /* Load stream extension entry */ res = dir_next(dp, 0); @@ -2152,9 +2168,9 @@ static FRESULT load_xdir ( /* FR_INT_ERR: invalid entry block */ if (res != FR_OK) return res; res = move_window(dp->obj.fs, dp->sect); if (res != FR_OK) return res; - if (dp->dir[XDIR_Type] != ET_STREAM) return FR_INT_ERR; /* Invalid order */ + if (dp->dir[XDIR_Type] != ET_STREAM) return FR_INT_ERR; /* Invalid order? */ memcpy(dirb + 1 * SZDIRE, dp->dir, SZDIRE); - if (MAXDIRB(dirb[XDIR_NumName]) > sz_ent) return FR_INT_ERR; + if (MAXDIRB(dirb[XDIR_NumName]) > sz_ent) return FR_INT_ERR; /* Invalid block size for the name? */ /* Load file name entries */ i = 2 * SZDIRE; /* Name offset to load */ @@ -2164,14 +2180,15 @@ static FRESULT load_xdir ( /* FR_INT_ERR: invalid entry block */ if (res != FR_OK) return res; res = move_window(dp->obj.fs, dp->sect); if (res != FR_OK) return res; - if (dp->dir[XDIR_Type] != ET_FILENAME) return FR_INT_ERR; /* Invalid order */ - if (i < MAXDIRB(FF_MAX_LFN)) memcpy(dirb + i, dp->dir, SZDIRE); + if (dp->dir[XDIR_Type] != ET_FILENAME) return FR_INT_ERR; /* Invalid order? */ + if (i < MAXDIRB(FF_MAX_LFN)) memcpy(dirb + i, dp->dir, SZDIRE); /* Load name entries only if the object is accessible */ } while ((i += SZDIRE) < sz_ent); /* Sanity check (do it for only accessible object) */ if (i <= MAXDIRB(FF_MAX_LFN)) { if (xdir_sum(dirb) != ld_word(dirb + XDIR_SetSum)) return FR_INT_ERR; } + return FR_OK; } @@ -2233,22 +2250,24 @@ static FRESULT store_xdir ( { FRESULT res; UINT nent; - BYTE *dirb = dp->obj.fs->dirbuf; /* Pointer to the directory entry block 85+C0+C1s */ + BYTE *dirb = dp->obj.fs->dirbuf; /* Pointer to the entry set 85+C0+C1s */ - /* Create set sum */ - st_word(dirb + XDIR_SetSum, xdir_sum(dirb)); - nent = dirb[XDIR_NumSec] + 1; - /* Store the directory entry block to the directory */ - res = dir_sdi(dp, dp->blk_ofs); + st_word(dirb + XDIR_SetSum, xdir_sum(dirb)); /* Create check sum */ + + /* Store the entry set to the directory */ + nent = dirb[XDIR_NumSec] + 1; /* Number of entries */ + res = dir_sdi(dp, dp->blk_ofs); /* Top of the entry set */ while (res == FR_OK) { + /* Set an entry to the directory */ res = move_window(dp->obj.fs, dp->sect); if (res != FR_OK) break; memcpy(dp->dir, dirb, SZDIRE); dp->obj.fs->wflag = 1; - if (--nent == 0) break; + + if (--nent == 0) break; /* All done? */ dirb += SZDIRE; - res = dir_next(dp, 0); + res = dir_next(dp, 0); /* Next entry */ } return (res == FR_OK || res == FR_DISK_ERR) ? res : FR_INT_ERR; } @@ -2265,30 +2284,30 @@ static void create_xdir ( ) { UINT i; - BYTE nc1, nlen; - WCHAR wc; + BYTE n_c1, nlen; + WCHAR chr; - /* Create file-directory and stream-extension entry */ + /* Create file-directory and stream-extension entry (1st and 2nd entry) */ memset(dirb, 0, 2 * SZDIRE); dirb[0 * SZDIRE + XDIR_Type] = ET_FILEDIR; dirb[1 * SZDIRE + XDIR_Type] = ET_STREAM; - /* Create file-name entries */ - i = SZDIRE * 2; /* Top of file_name entries */ - nlen = nc1 = 0; wc = 1; + /* Create file name entries (3rd enrty and follows) */ + i = SZDIRE * 2; /* Top of file name entries */ + nlen = n_c1 = 0; chr = 1; do { dirb[i++] = ET_FILENAME; dirb[i++] = 0; do { /* Fill name field */ - if (wc != 0 && (wc = lfn[nlen]) != 0) nlen++; /* Get a character if exist */ - st_word(dirb + i, wc); /* Store it */ + if (chr != 0 && (chr = lfn[nlen]) != 0) nlen++; /* Get a character if exist */ + st_word(dirb + i, chr); /* Store it */ i += 2; } while (i % SZDIRE != 0); - nc1++; - } while (lfn[nlen]); /* Fill next entry if any char follows */ + n_c1++; + } while (lfn[nlen]); /* Fill next C1 entry if any char follows */ dirb[XDIR_NumName] = nlen; /* Set name length */ - dirb[XDIR_NumSec] = 1 + nc1; /* Set secondary count (C0 + C1s) */ + dirb[XDIR_NumSec] = 1 + n_c1; /* Set secondary count (C0 + C1s) */ st_word(dirb + XDIR_NameHash, xname_sum(lfn)); /* Set name hash */ } @@ -2424,26 +2443,27 @@ static FRESULT dir_find ( /* FR_OK(0):succeeded, !=0:error */ res = move_window(fs, dp->sect); if (res != FR_OK) break; c = dp->dir[DIR_Name]; - if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */ + if (c == 0) { res = FR_NO_FILE; break; } /* Reached end of directory table */ #if FF_USE_LFN /* LFN configuration */ dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK; if (c == DDEM || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */ ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */ } else { - if (a == AM_LFN) { /* An LFN entry is found */ + if (a == AM_LFN) { /* Is it an LFN entry? */ if (!(dp->fn[NSFLAG] & NS_NOLFN)) { - if (c & LLEF) { /* Is it start of LFN sequence? */ - sum = dp->dir[LDIR_Chksum]; - c &= (BYTE)~LLEF; ord = c; /* LFN start order */ - dp->blk_ofs = dp->dptr; /* Start offset of LFN */ + if (c & LLEF) { /* Is it start of an entry set? */ + c &= (BYTE)~LLEF; + ord = c; /* Number of LFN entries */ + dp->blk_ofs = dp->dptr; /* Start offset of LFN */ + sum = dp->dir[LDIR_Chksum]; /* Sum of the SFN */ } /* Check validity of the LFN entry and compare it with given name */ ord = (c == ord && sum == dp->dir[LDIR_Chksum] && cmp_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF; } - } else { /* An SFN entry is found */ + } else { /* SFN entry */ if (ord == 0 && sum == sum_sfn(dp->dir)) break; /* LFN matched? */ if (!(dp->fn[NSFLAG] & NS_LOSS) && !memcmp(dp->dir, dp->fn, 11)) break; /* SFN matched? */ - ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */ + ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Not matched, reset LFN sequence */ } } #else /* Non LFN configuration */ @@ -2777,9 +2797,9 @@ static DWORD get_achar ( /* Get a character and advance ptr */ chr = (BYTE)*(*ptr)++; /* Get a byte */ if (IsLower(chr)) chr -= 0x20; /* To upper ASCII char */ #if FF_CODE_PAGE == 0 - if (ExCvt && chr >= 0x80) chr = ExCvt[chr - 0x80]; /* To upper SBCS extended char */ + if (ExCvt && chr >= 0x80) chr = ExCvt[chr - 0x80]; /* To upper (SBCS extended char) */ #elif FF_CODE_PAGE < 900 - if (chr >= 0x80) chr = ExCvt[chr - 0x80]; /* To upper SBCS extended char */ + if (chr >= 0x80) chr = ExCvt[chr - 0x80]; /* To upper (SBCS extended char) */ #endif #if FF_CODE_PAGE == 0 || FF_CODE_PAGE >= 900 if (dbc_1st((BYTE)chr)) { /* Get DBC 2nd byte if needed */ @@ -3143,70 +3163,64 @@ static int get_ldnumber ( /* Returns logical drive number (-1:invalid drive numb { const TCHAR *tp; const TCHAR *tt; - TCHAR tc; + TCHAR chr; int i; - int vol = -1; #if FF_STR_VOLUME_ID /* Find string volume ID */ - const char *sp; - char c; + const char *vsp; + char vchr; #endif tt = tp = *path; - if (!tp) return vol; /* Invalid path name? */ + if (!tp) return -1; /* Invalid path name? */ do { /* Find a colon in the path */ - tc = *tt++; - } while (!IsTerminator(tc) && tc != ':'); + chr = *tt++; + } while (!IsTerminator(chr) && chr != ':'); - if (tc == ':') { /* DOS/Windows style volume ID? */ + if (chr == ':') { /* Is there a DOS/Windows style volume ID? */ i = FF_VOLUMES; - if (IsDigit(*tp) && tp + 2 == tt) { /* Is there a numeric volume ID + colon? */ - i = (int)*tp - '0'; /* Get the LD number */ + if (IsDigit(*tp) && tp + 2 == tt) { /* Is it a numeric volume ID + colon? */ + i = (int)*tp - '0'; /* Get the logical drive number */ } -#if FF_STR_VOLUME_ID == 1 /* Arbitrary string is enabled */ +#if FF_STR_VOLUME_ID == 1 /* Arbitrary string volume ID is enabled */ else { - i = 0; + i = 0; /* Find volume ID string in the preconfigured table */ do { - sp = VolumeStr[i]; tp = *path; /* This string volume ID and path name */ - do { /* Compare the volume ID with path name */ - c = *sp++; tc = *tp++; - if (IsLower(c)) c -= 0x20; - if (IsLower(tc)) tc -= 0x20; - } while (c && (TCHAR)c == tc); - } while ((c || tp != tt) && ++i < FF_VOLUMES); /* Repeat for each id until pattern match */ + vsp = VolumeStr[i]; tp = *path; /* Preconfigured string and path name to test */ + do { /* Compare the volume ID with path name in case-insensitive */ + vchr = *vsp++; chr = *tp++; + if (IsLower(vchr)) vchr -= 0x20; + if (IsLower(chr)) chr -= 0x20; + } while (vchr && (TCHAR)vchr == chr); + } while ((vchr || tp != tt) && ++i < FF_VOLUMES); /* Repeat for each id until pattern match */ } #endif - if (i < FF_VOLUMES) { /* If a volume ID is found, get the drive number and strip it */ - vol = i; /* Drive number */ - *path = tt; /* Snip the drive prefix off */ - } - return vol; + if (i >= FF_VOLUMES) return -1; /* Not found or invalid volume ID */ + *path = tt; /* Snip the drive prefix off */ + return i; /* Return the found drive number */ } #if FF_STR_VOLUME_ID == 2 /* Unix style volume ID is enabled */ if (*tp == '/') { /* Is there a volume ID? */ while (*(tp + 1) == '/') tp++; /* Skip duplicated separator */ i = 0; do { - tt = tp; sp = VolumeStr[i]; /* Path name and this string volume ID */ - do { /* Compare the volume ID with path name */ - c = *sp++; tc = *(++tt); - if (IsLower(c)) c -= 0x20; - if (IsLower(tc)) tc -= 0x20; - } while (c && (TCHAR)c == tc); - } while ((c || (tc != '/' && !IsTerminator(tc))) && ++i < FF_VOLUMES); /* Repeat for each ID until pattern match */ - if (i < FF_VOLUMES) { /* If a volume ID is found, get the drive number and strip it */ - vol = i; /* Drive number */ - *path = tt; /* Snip the drive prefix off */ - } - return vol; + vsp = VolumeStr[i]; tt = tp; /* Preconfigured string and path name to test */ + do { /* Compare the volume ID with path name in case-insensitive */ + vchr = *vsp++; chr = *(++tt); + if (IsLower(vchr)) vchr -= 0x20; + if (IsLower(chr)) chr -= 0x20; + } while (vchr && (TCHAR)vchr == chr); + } while ((vchr || (chr != '/' && !IsTerminator(chr))) && ++i < FF_VOLUMES); /* Repeat for each ID until pattern match */ + if (i >= FF_VOLUMES) return -1; /* Not found (invalid volume ID) */ + *path = tt; /* Snip the node name off */ + return i; /* Return the found drive number */ } #endif - /* No drive prefix is found */ + /* No drive prefix */ #if FF_FS_RPATH != 0 - vol = CurrVol; /* Default drive is current drive */ + return (int)CurrVol; /* Default drive is current drive */ #else - vol = 0; /* Default drive is 0 */ + return 0; /* Default drive is 0 */ #endif - return vol; /* Return the default drive */ } @@ -3261,11 +3275,11 @@ static int test_gpt_header ( /* 0:Invalid, 1:Valid */ #if !FF_FS_READONLY && FF_USE_MKFS -/* Generate random value */ -static DWORD make_rand ( - DWORD seed, /* Seed value */ - BYTE *buff, /* Output buffer */ - UINT n /* Data length */ +/* Generate a random value */ +static DWORD make_rand ( /* Returns a seed value for next */ + DWORD seed, /* Seed value */ + BYTE *buff, /* Output buffer */ + UINT n /* Data length */ ) { UINT r; @@ -3310,20 +3324,20 @@ static UINT check_fs ( /* 0:FAT/FAT32 VBR, 1:exFAT VBR, 2:Not FAT and valid BS, if (sign == 0xAA55 && !memcmp(fs->win + BS_FilSysType32, "FAT32 ", 8)) { return 0; /* It is an FAT32 VBR */ } - /* FAT volumes formatted with early MS-DOS lack BS_55AA and BS_FilSysType, so FAT VBR needs to be identified without them. */ + /* FAT volumes created in the early MS-DOS era lack BS_55AA and BS_FilSysType, so FAT VBR needs to be identified without them. */ w = ld_word(fs->win + BPB_BytsPerSec); b = fs->win[BPB_SecPerClus]; if ((w & (w - 1)) == 0 && w >= FF_MIN_SS && w <= FF_MAX_SS /* Properness of sector size (512-4096 and 2^n) */ && b != 0 && (b & (b - 1)) == 0 /* Properness of cluster size (2^n) */ - && ld_word(fs->win + BPB_RsvdSecCnt) != 0 /* Properness of reserved sectors (MNBZ) */ - && (UINT)fs->win[BPB_NumFATs] - 1 <= 1 /* Properness of FATs (1 or 2) */ - && ld_word(fs->win + BPB_RootEntCnt) != 0 /* Properness of root dir entries (MNBZ) */ - && (ld_word(fs->win + BPB_TotSec16) >= 128 || ld_dword(fs->win + BPB_TotSec32) >= 0x10000) /* Properness of volume sectors (>=128) */ + && ld_word(fs->win + BPB_RsvdSecCnt) != 0 /* Properness of number of reserved sectors (MNBZ) */ + && (UINT)fs->win[BPB_NumFATs] - 1 <= 1 /* Properness of number of FATs (1 or 2) */ + && ld_word(fs->win + BPB_RootEntCnt) != 0 /* Properness of root dir size (MNBZ) */ + && (ld_word(fs->win + BPB_TotSec16) >= 128 || ld_dword(fs->win + BPB_TotSec32) >= 0x10000) /* Properness of volume size (>=128) */ && ld_word(fs->win + BPB_FATSz16) != 0) { /* Properness of FAT size (MNBZ) */ return 0; /* It can be presumed an FAT VBR */ } } - return sign == 0xAA55 ? 2 : 3; /* Not an FAT VBR (valid or invalid BS) */ + return sign == 0xAA55 ? 2 : 3; /* Not an FAT VBR (with valid or invalid BS) */ } @@ -3505,7 +3519,8 @@ static FRESULT mount_volume ( /* FR_OK(0): successful, !=0: an error occurred */ } #if !FF_FS_READONLY - fs->last_clst = fs->free_clst = 0xFFFFFFFF; /* Initialize cluster allocation information */ + fs->last_clst = fs->free_clst = 0xFFFFFFFF; /* Invalidate cluster allocation information */ + fs->fsi_flag = 0; /* Enable to sync PercInUse value in VBR */ #endif fmt = FS_EXFAT; /* FAT sub-type */ } else @@ -3564,27 +3579,25 @@ static FRESULT mount_volume ( /* FR_OK(0): successful, !=0: an error occurred */ #if !FF_FS_READONLY /* Get FSInfo if available */ - fs->last_clst = fs->free_clst = 0xFFFFFFFF; /* Initialize cluster allocation information */ - fs->fsi_flag = 0x80; -#if (FF_FS_NOFSINFO & 3) != 3 - if (fmt == FS_FAT32 /* Allow to update FSInfo only if BPB_FSInfo32 == 1 */ - && ld_word(fs->win + BPB_FSInfo32) == 1 + fs->last_clst = fs->free_clst = 0xFFFFFFFF; /* Invalidate cluster allocation information */ + fs->fsi_flag = 0x80; /* Disable FSInfo by default */ + if (fmt == FS_FAT32 + && ld_word(fs->win + BPB_FSInfo32) == 1 /* FAT32: Enable FSInfo feature only if FSInfo sector is next to VBR */ && move_window(fs, bsect + 1) == FR_OK) { fs->fsi_flag = 0; - if (ld_word(fs->win + BS_55AA) == 0xAA55 /* Load FSInfo data if available */ - && ld_dword(fs->win + FSI_LeadSig) == 0x41615252 - && ld_dword(fs->win + FSI_StrucSig) == 0x61417272) + if ( ld_dword(fs->win + FSI_LeadSig) == 0x41615252 /* Load FSInfo data if available */ + && ld_dword(fs->win + FSI_StrucSig) == 0x61417272 + && ld_dword(fs->win + FSI_TrailSig) == 0xAA550000) { -#if (FF_FS_NOFSINFO & 1) == 0 +#if (FF_FS_NOFSINFO & 1) == 0 /* Get free cluster count if trust it */ fs->free_clst = ld_dword(fs->win + FSI_Free_Count); #endif -#if (FF_FS_NOFSINFO & 2) == 0 +#if (FF_FS_NOFSINFO & 2) == 0 /* Get next free cluster if rtust it */ fs->last_clst = ld_dword(fs->win + FSI_Nxt_Free); #endif } } -#endif /* (FF_FS_NOFSINFO & 3) != 3 */ #endif /* !FF_FS_READONLY */ } @@ -3623,7 +3636,7 @@ static FRESULT validate ( /* Returns FR_OK or FR_INVALID_OBJECT */ if (obj && obj->fs && obj->fs->fs_type && obj->id == obj->fs->id) { /* Test if the object is valid */ #if FF_FS_REENTRANT if (lock_volume(obj->fs, 0)) { /* Take a grant to access the volume */ - if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) { /* Test if the hosting phsical drive is kept initialized */ + if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) { /* Test if the hosting physical drive is kept initialized */ res = FR_OK; } else { unlock_volume(obj->fs, FR_OK); /* Invalidated volume, abort to access */ @@ -3632,7 +3645,7 @@ static FRESULT validate ( /* Returns FR_OK or FR_INVALID_OBJECT */ res = FR_TIMEOUT; } #else - if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) { /* Test if the hosting phsical drive is kept initialized */ + if (!(disk_status(obj->fs->pdrv) & STA_NOINIT)) { /* Test if the hosting physical drive is kept initialized */ res = FR_OK; } #endif @@ -3673,7 +3686,7 @@ FRESULT f_mount ( if (vol < 0) return FR_INVALID_DRIVE; cfs = FatFs[vol]; /* Pointer to the filesystem object of the volume */ - if (cfs) { /* Unregister current filesystem object if regsitered */ + if (cfs) { /* Unregister current filesystem object if registered */ FatFs[vol] = 0; #if FF_FS_LOCK clear_share(cfs); @@ -4619,7 +4632,7 @@ FRESULT f_opendir ( if (dp->obj.attr & AM_DIR) { /* This object is a sub-directory */ #if FF_FS_EXFAT if (fs->fs_type == FS_EXFAT) { - dp->obj.c_scl = dp->obj.sclust; /* Get containing directory inforamation */ + dp->obj.c_scl = dp->obj.sclust; /* Get containing directory information */ dp->obj.c_size = ((DWORD)dp->obj.objsize & 0xFFFFFF00) | dp->obj.stat; dp->obj.c_ofs = dp->blk_ofs; init_alloc_info(fs, &dp->obj); /* Get object allocation info */ @@ -4818,7 +4831,7 @@ FRESULT f_stat ( FRESULT f_getfree ( const TCHAR* path, /* Logical drive number */ DWORD* nclst, /* Pointer to a variable to return number of free clusters */ - FATFS** fatfs /* Pointer to return pointer to corresponding filesystem object */ + FATFS** fatfs /* Pointer to a pointer to return corresponding filesystem object */ ) { FRESULT res; @@ -4837,7 +4850,7 @@ FRESULT f_getfree ( if (fs->free_clst <= fs->n_fatent - 2) { *nclst = fs->free_clst; } else { - /* Scan FAT to obtain number of free clusters */ + /* Scan FAT to obtain the correct free cluster count */ nfree = 0; if (fs->fs_type == FS_FAT12) { /* FAT12: Scan bit field FAT entries */ clst = 2; obj.fs = fs; @@ -4860,16 +4873,16 @@ FRESULT f_getfree ( clst = fs->n_fatent - 2; /* Number of clusters */ sect = fs->bitbase; /* Bitmap sector */ i = 0; /* Offset in the sector */ - do { /* Counts numbuer of bits with zero in the bitmap */ + do { /* Counts numbuer of clear bits (free clusters) in the bitmap */ if (i == 0) { /* New sector? */ res = move_window(fs, sect++); if (res != FR_OK) break; } - for (b = 8, bm = ~fs->win[i]; b && clst; b--, clst--) { + for (b = 8, bm = ~fs->win[i]; b && clst; b--, clst--) { /* Count clear bits in a byte */ nfree += bm & 1; bm >>= 1; } - i = (i + 1) % SS(fs); + i = (i + 1) % SS(fs); /* Next byte */ } while (clst); } else #endif @@ -4883,11 +4896,11 @@ FRESULT f_getfree ( if (res != FR_OK) break; } if (fs->fs_type == FS_FAT16) { - if (ld_word(fs->win + i) == 0) nfree++; - i += 2; + if (ld_word(fs->win + i) == 0) nfree++; /* FAT16: Is this cluster free? */ + i += 2; /* Next entry */ } else { - if ((ld_dword(fs->win + i) & 0x0FFFFFFF) == 0) nfree++; - i += 4; + if ((ld_dword(fs->win + i) & 0x0FFFFFFF) == 0) nfree++; /* FAT32: Is this cluster free? */ + i += 4; /* Next entry */ } i %= SS(fs); } while (--clst); @@ -4895,8 +4908,8 @@ FRESULT f_getfree ( } if (res == FR_OK) { /* Update parameters if succeeded */ *nclst = nfree; /* Return the free clusters */ - fs->free_clst = nfree; /* Now free_clst is valid */ - fs->fsi_flag |= 1; /* FAT32: FSInfo is to be updated */ + fs->free_clst = nfree; /* Now free cluster count is valid */ + fs->fsi_flag |= 1; /* FAT32/exfAT : Allocation information is to be updated */ } } } @@ -5098,7 +5111,7 @@ FRESULT f_mkdir ( st_clust(fs, fs->win + SZDIRE, pcl); fs->wflag = 1; } - res = dir_register(&dj); /* Register the object to the parent directoy */ + res = dir_register(&dj); /* Register the object to the parent directory */ } } if (res == FR_OK) { @@ -5213,7 +5226,7 @@ FRESULT f_rename ( } else { /* Start of critical section where an interruption can cause a cross-link */ res = move_window(fs, sect); - dir = fs->win + SZDIRE * 1; /* Ptr to .. entry */ + dir = fs->win + SZDIRE * 1; /* Pointer to .. entry */ if (res == FR_OK && dir[1] == '.') { st_clust(fs, dir, djn.obj.sclust); fs->wflag = 1; @@ -5416,7 +5429,7 @@ FRESULT f_getlabel ( /* Get volume serial number */ if (res == FR_OK && vsn) { - res = move_window(fs, fs->volbase); + res = move_window(fs, fs->volbase); /* Load VBR */ if (res == FR_OK) { switch (fs->fs_type) { case FS_EXFAT: @@ -5427,10 +5440,10 @@ FRESULT f_getlabel ( di = BS_VolID32; break; - default: - di = BS_VolID; + default: /* FAT12/16 */ + di = fs->win[BS_BootSig] == 0x29 ? BS_VolID : 0; } - *vsn = ld_dword(fs->win + di); + *vsn = di ? ld_dword(fs->win + di) : 0; /* Get VSN in the VBR */ } } @@ -5466,7 +5479,6 @@ FRESULT f_setlabel ( for ( ; *label == '/'; label++) ; /* Snip the separators off */ #endif - #if FF_FS_EXFAT if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */ memset(dirvn, 0, 22); @@ -5775,19 +5787,19 @@ static FRESULT create_partition ( rnd = (DWORD)sz_drv + GET_FATTIME(); /* Random seed */ align = GPT_ALIGN / ss; /* Partition alignment for GPT [sector] */ sz_ptbl = GPT_ITEMS * SZ_GPTE / ss; /* Size of partition table [sector] */ - top_bpt = sz_drv - sz_ptbl - 1; /* Backup partition table start sector */ - nxt_alloc = 2 + sz_ptbl; /* First allocatable sector */ - sz_pool = top_bpt - nxt_alloc; /* Size of allocatable area */ + top_bpt = sz_drv - sz_ptbl - 1; /* Backup partition table start LBA */ + nxt_alloc = 2 + sz_ptbl; /* First allocatable LBA */ + sz_pool = top_bpt - nxt_alloc; /* Size of allocatable area [sector] */ bcc = 0xFFFFFFFF; sz_part = 1; - pi = si = 0; /* partition table index, size table index */ + pi = si = 0; /* partition table index, map index */ do { if (pi * SZ_GPTE % ss == 0) memset(buf, 0, ss); /* Clean the buffer if needed */ if (sz_part != 0) { /* Is the size table not termintated? */ - nxt_alloc = (nxt_alloc + align - 1) & ((QWORD)0 - align); /* Align partition start */ + nxt_alloc = (nxt_alloc + align - 1) & ((QWORD)0 - align); /* Align partition start LBA */ sz_part = plst[si++]; /* Get a partition size */ if (sz_part <= 100) { /* Is the size in percentage? */ - sz_part = sz_pool * sz_part / 100; - sz_part = (sz_part + align - 1) & ((QWORD)0 - align); /* Align partition end (only if in percentage) */ + sz_part = sz_pool * sz_part / 100; /* Sectors in percentage */ + sz_part = (sz_part + align - 1) & ((QWORD)0 - align); /* Align partition end LBA (only if in percentage) */ } if (nxt_alloc + sz_part > top_bpt) { /* Clip the size at end of the pool */ sz_part = (nxt_alloc < top_bpt) ? top_bpt - nxt_alloc : 0; @@ -5797,11 +5809,11 @@ static FRESULT create_partition ( ofs = pi * SZ_GPTE % ss; memcpy(buf + ofs + GPTE_PtGuid, GUID_MS_Basic, 16); /* Set partition GUID (Microsoft Basic Data) */ rnd = make_rand(rnd, buf + ofs + GPTE_UpGuid, 16); /* Set unique partition GUID */ - st_qword(buf + ofs + GPTE_FstLba, nxt_alloc); /* Set partition start sector */ - st_qword(buf + ofs + GPTE_LstLba, nxt_alloc + sz_part - 1); /* Set partition end sector */ - nxt_alloc += sz_part; /* Next allocatable sector */ + st_qword(buf + ofs + GPTE_FstLba, nxt_alloc); /* Set partition start LBA */ + st_qword(buf + ofs + GPTE_LstLba, nxt_alloc + sz_part - 1); /* Set partition end LBA */ + nxt_alloc += sz_part; /* Next allocatable LBA */ } - if ((pi + 1) * SZ_GPTE % ss == 0) { /* Write the buffer if it is filled up */ + if ((pi + 1) * SZ_GPTE % ss == 0) { /* Write the sector buffer if it is filled up */ for (i = 0; i < ss; bcc = crc32(bcc, buf[i++])) ; /* Calculate table check sum */ if (disk_write(drv, buf, 2 + pi * SZ_GPTE / ss, 1) != RES_OK) return FR_DISK_ERR; /* Write to primary table */ if (disk_write(drv, buf, top_bpt + pi * SZ_GPTE / ss, 1) != RES_OK) return FR_DISK_ERR; /* Write to secondary table */ @@ -5855,20 +5867,20 @@ static FRESULT create_partition ( if (nxt_alloc32 + sz_part32 > sz_drv32 || nxt_alloc32 + sz_part32 < nxt_alloc32) sz_part32 = sz_drv32 - nxt_alloc32; /* Clip at drive size */ if (sz_part32 == 0) break; /* End of table or no sector to allocate? */ - st_dword(pte + PTE_StLba, nxt_alloc32); /* Start LBA */ - st_dword(pte + PTE_SizLba, sz_part32); /* Number of sectors */ + st_dword(pte + PTE_StLba, nxt_alloc32); /* Partition start LBA sector */ + st_dword(pte + PTE_SizLba, sz_part32); /* Size of partition [sector] */ pte[PTE_System] = sys; /* System type */ - cy = (UINT)(nxt_alloc32 / n_sc / n_hd); /* Start cylinder */ - hd = (BYTE)(nxt_alloc32 / n_sc % n_hd); /* Start head */ - sc = (BYTE)(nxt_alloc32 % n_sc + 1); /* Start sector */ + cy = (UINT)(nxt_alloc32 / n_sc / n_hd); /* Partitio start CHS cylinder */ + hd = (BYTE)(nxt_alloc32 / n_sc % n_hd); /* Partition start CHS head */ + sc = (BYTE)(nxt_alloc32 % n_sc + 1); /* Partition start CHS sector */ pte[PTE_StHead] = hd; pte[PTE_StSec] = (BYTE)((cy >> 2 & 0xC0) | sc); pte[PTE_StCyl] = (BYTE)cy; - cy = (UINT)((nxt_alloc32 + sz_part32 - 1) / n_sc / n_hd); /* End cylinder */ - hd = (BYTE)((nxt_alloc32 + sz_part32 - 1) / n_sc % n_hd); /* End head */ - sc = (BYTE)((nxt_alloc32 + sz_part32 - 1) % n_sc + 1); /* End sector */ + cy = (UINT)((nxt_alloc32 + sz_part32 - 1) / n_sc / n_hd); /* Partition end CHS cylinder */ + hd = (BYTE)((nxt_alloc32 + sz_part32 - 1) / n_sc % n_hd); /* Partition end CHS head */ + sc = (BYTE)((nxt_alloc32 + sz_part32 - 1) % n_sc + 1); /* Partition end CHS sector */ pte[PTE_EdHead] = hd; pte[PTE_EdSec] = (BYTE)((cy >> 2 & 0xC0) | sc); pte[PTE_EdCyl] = (BYTE)cy; @@ -5892,18 +5904,18 @@ FRESULT f_mkfs ( UINT len /* Size of working buffer [byte] */ ) { - static const WORD cst[] = {1, 4, 16, 64, 256, 512, 0}; /* Cluster size boundary for FAT volume (4Ks unit) */ - static const WORD cst32[] = {1, 2, 4, 8, 16, 32, 0}; /* Cluster size boundary for FAT32 volume (128Ks unit) */ + static const WORD cst[] = {1, 4, 16, 64, 256, 512, 0}; /* Cluster size boundary for FAT volume (4K sector unit) */ + static const WORD cst32[] = {1, 2, 4, 8, 16, 32, 0}; /* Cluster size boundary for FAT32 volume (128K sector unit) */ static const MKFS_PARM defopt = {FM_ANY, 0, 0, 0, 0}; /* Default parameter */ BYTE fsopt, fsty, sys, pdrv, ipart; BYTE *buf; BYTE *pte; WORD ss; /* Sector size */ DWORD sz_buf, sz_blk, n_clst, pau, nsect, n, vsn; - LBA_t sz_vol, b_vol, b_fat, b_data; /* Size of volume, Base LBA of volume, fat, data */ + LBA_t sz_vol, b_vol, b_fat, b_data; /* Volume size, base LBA of volume, base LBA of FAT and base LBA of data */ LBA_t sect, lba[2]; - DWORD sz_rsv, sz_fat, sz_dir, sz_au; /* Size of reserved, fat, dir, data, cluster */ - UINT n_fat, n_root, i; /* Index, Number of FATs and Number of roor dir entries */ + DWORD sz_rsv, sz_fat, sz_dir, sz_au; /* Size of reserved area, FAT area, directry area, data area and cluster */ + UINT n_fat, n_root, i; /* Number of FATs, number of roor directory entries and some index */ int vol; DSTATUS ds; FRESULT res; @@ -5924,7 +5936,7 @@ FRESULT f_mkfs ( /* Get physical drive parameters (sz_drv, sz_blk and ss) */ if (!opt) opt = &defopt; /* Use default parameter if it is not given */ sz_blk = opt->align; - if (sz_blk == 0) disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_blk); /* Block size from the paramter or lower layer */ + if (sz_blk == 0) disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_blk); /* Block size from the parameter or lower layer */ if (sz_blk == 0 || sz_blk > 0x8000 || (sz_blk & (sz_blk - 1))) sz_blk = 1; /* Use default if the block size is invalid */ #if FF_MAX_SS != FF_MIN_SS if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &ss) != RES_OK) return FR_DISK_ERR; @@ -6002,13 +6014,13 @@ FRESULT f_mkfs ( } } } - if (sz_vol < 128) LEAVE_MKFS(FR_MKFS_ABORTED); /* Check if volume size is >=128s */ + if (sz_vol < 128) LEAVE_MKFS(FR_MKFS_ABORTED); /* Check if volume size is >=128 sectors */ /* Now start to create an FAT volume at b_vol and sz_vol */ do { /* Pre-determine the FAT type */ if (FF_FS_EXFAT && (fsopt & FM_EXFAT)) { /* exFAT possible? */ - if ((fsopt & FM_ANY) == FM_EXFAT || sz_vol >= 0x4000000 || sz_au > 128) { /* exFAT only, vol >= 64MS or sz_au > 128S ? */ + if ((fsopt & FM_ANY) == FM_EXFAT || sz_vol >= 0x4000000 || sz_au > 128) { /* exFAT only, vol >= 64M sectors or sz_au > 128 sectors ? */ fsty = FS_EXFAT; break; } } @@ -6025,7 +6037,7 @@ FRESULT f_mkfs ( fsty = FS_FAT16; } while (0); - vsn = (DWORD)sz_vol + GET_FATTIME(); /* VSN generated from current time and partitiion size */ + vsn = (DWORD)sz_vol + GET_FATTIME(); /* VSN generated from current time and partition size */ #if FF_FS_EXFAT if (fsty == FS_EXFAT) { /* Create an exFAT volume */ @@ -6091,7 +6103,7 @@ FRESULT f_mkfs ( } } while (si); clen[1] = (szb_case + sz_au * ss - 1) / (sz_au * ss); /* Number of up-case table clusters */ - clen[2] = 1; /* Number of root dir clusters */ + clen[2] = 1; /* Number of root directory clusters */ /* Initialize the allocation bitmap */ sect = b_data; nsect = (szb_bit + ss - 1) / ss; /* Start of bitmap and number of bitmap sectors */ @@ -6113,7 +6125,8 @@ FRESULT f_mkfs ( st_dword(buf + i, 0xFFFFFFF8); i += 4; clu++; st_dword(buf + i, 0xFFFFFFFF); i += 4; clu++; } - do { /* Create chains of bitmap, up-case and root dir */ + + do { /* Create chains of bitmap, up-case and root directory */ while (nbit != 0 && i < sz_buf * ss) { /* Create a chain */ st_dword(buf + i, (nbit > 1) ? clu + 1 : 0xFFFFFFFF); i += 4; clu++; nbit--; @@ -6155,10 +6168,10 @@ FRESULT f_mkfs ( st_dword(buf + BPB_FatSzEx, sz_fat); /* FAT size [sector] */ st_dword(buf + BPB_DataOfsEx, (DWORD)(b_data - b_vol)); /* Data offset [sector] */ st_dword(buf + BPB_NumClusEx, n_clst); /* Number of clusters */ - st_dword(buf + BPB_RootClusEx, 2 + clen[0] + clen[1]); /* Root dir cluster # */ + st_dword(buf + BPB_RootClusEx, 2 + clen[0] + clen[1]); /* Root directory cluster number */ st_dword(buf + BPB_VolIDEx, vsn); /* VSN */ st_word(buf + BPB_FSVerEx, 0x100); /* Filesystem version (1.00) */ - for (buf[BPB_BytsPerSecEx] = 0, i = ss; i >>= 1; buf[BPB_BytsPerSecEx]++) ; /* Log2 of sector size [byte] */ + for (buf[BPB_BytsPerSecEx] = 0, i = ss; i >>= 1; buf[BPB_BytsPerSecEx]++) ; /* Log2 of sector size [byte] */ for (buf[BPB_SecPerClusEx] = 0, i = sz_au; i >>= 1; buf[BPB_SecPerClusEx]++) ; /* Log2 of cluster size [sector] */ buf[BPB_NumFATsEx] = 1; /* Number of FATs */ buf[BPB_DrvNumEx] = 0x80; /* Drive number (for int13) */ @@ -6216,7 +6229,7 @@ FRESULT f_mkfs ( } sz_fat = (n + ss - 1) / ss; /* FAT size [sector] */ sz_rsv = 1; /* Number of reserved sectors */ - sz_dir = (DWORD)n_root * SZDIRE / ss; /* Root dir size [sector] */ + sz_dir = (DWORD)n_root * SZDIRE / ss; /* Root directory size [sector] */ } b_fat = b_vol + sz_rsv; /* FAT base */ b_data = b_fat + sz_fat * n_fat + sz_dir; /* Data base */ @@ -6323,7 +6336,7 @@ FRESULT f_mkfs ( if (fsty == FS_FAT32) { st_dword(buf + 0, 0xFFFFFFF8); /* FAT[0] */ st_dword(buf + 4, 0xFFFFFFFF); /* FAT[1] */ - st_dword(buf + 8, 0x0FFFFFFF); /* FAT[2] (root directory) */ + st_dword(buf + 8, 0x0FFFFFFF); /* FAT[2] (root directory at cluster# 2) */ } else { st_dword(buf + 0, (fsty == FS_FAT12) ? 0xFFFFF8 : 0xFFFFFFF8); /* FAT[0] and FAT[1] */ } @@ -6331,7 +6344,7 @@ FRESULT f_mkfs ( do { /* Fill FAT sectors */ n = (nsect > sz_buf) ? sz_buf : nsect; if (disk_write(pdrv, buf, sect, (UINT)n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); - memset(buf, 0, ss); /* Rest of FAT all are cleared */ + memset(buf, 0, ss); /* Rest of FAT area is initially zero */ sect += n; nsect -= n; } while (nsect); } @@ -6502,7 +6515,7 @@ TCHAR* f_gets ( if (rc != ct || dc < 0x80 || IsSurrogate(dc) || dc >= 0x110000) continue; /* Wrong encoding? */ } #endif - /* A code point is avaialble in dc to be output */ + /* A code point is available in dc to be output */ if (FF_USE_STRFUNC == 2 && dc == '\r') continue; /* Strip \r off if needed */ #if FF_LFN_UNICODE == 1 || FF_LFN_UNICODE == 3 /* Output it in UTF-16/32 encoding */ @@ -6567,8 +6580,8 @@ TCHAR* f_gets ( /* Output buffer and work area */ typedef struct { - FIL *fp; /* Ptr to the writing file */ - int idx, nchr; /* Write index of buf[] (-1:error), number of encoding units written */ + FIL *fp; /* Pointer to the writing file */ + int idx, nchr; /* Write index of buf[] (-1:error), number of written encoding units */ #if FF_USE_LFN && FF_LFN_UNICODE == 1 WCHAR hs; #elif FF_USE_LFN && FF_LFN_UNICODE == 2 @@ -6608,23 +6621,23 @@ static void putc_bfd (putbuff* pb, TCHAR c) } hs = pb->hs; pb->hs = 0; if (hs != 0) { /* Is there a leading high-surrogate? */ - if (!IsSurrogateL(c)) hs = 0; /* Discard high-surrogate if not a surrogate pair */ + if (!IsSurrogateL(c)) hs = 0; /* Discard high-surrogate if a stray high-surrogate */ } else { if (IsSurrogateL(c)) return; /* Discard stray low-surrogate */ } wc = c; #elif FF_LFN_UNICODE == 2 /* UTF-8 input */ for (;;) { - if (pb->ct == 0) { /* Out of multi-byte sequence? */ + if (pb->ct == 0) { /* Not in the multi-byte sequence? */ pb->bs[pb->wi = 0] = (BYTE)c; /* Save 1st byte */ if ((BYTE)c < 0x80) break; /* Single byte code? */ if (((BYTE)c & 0xE0) == 0xC0) pb->ct = 1; /* 2-byte sequence? */ if (((BYTE)c & 0xF0) == 0xE0) pb->ct = 2; /* 3-byte sequence? */ if (((BYTE)c & 0xF8) == 0xF0) pb->ct = 3; /* 4-byte sequence? */ - return; /* Wrong leading byte (discard it) */ + return; /* Invalid leading byte (discard it) */ } else { /* In the multi-byte sequence */ if (((BYTE)c & 0xC0) != 0x80) { /* Broken sequence? */ - pb->ct = 0; continue; /* Discard the sequense */ + pb->ct = 0; continue; /* Discard the sequence */ } pb->bs[++pb->wi] = (BYTE)c; /* Save the trailing byte */ if (--pb->ct == 0) break; /* End of the sequence? */ @@ -6710,7 +6723,7 @@ static void putc_bfd (putbuff* pb, TCHAR c) } -/* Flush remaining characters in the buffer */ +/* Flush characters left in the buffer and return number of characters written */ static int putc_flush (putbuff* pb) { @@ -6718,7 +6731,9 @@ static int putc_flush (putbuff* pb) if ( pb->idx >= 0 /* Flush buffered characters to the file */ && f_write(pb->fp, pb->buf, (UINT)pb->idx, &nw) == FR_OK - && (UINT)pb->idx == nw) return pb->nchr; + && (UINT)pb->idx == nw) { + return pb->nchr; + } return -1; } @@ -6826,8 +6841,8 @@ static void ftoa ( TCHAR fmt /* Notation */ ) { - int d; - int e = 0, m = 0; + int digit; + int exp = 0, mag = 0; char sign = 0; double w; const char *er = 0; @@ -6848,18 +6863,18 @@ static void ftoa ( } else { if (fmt == 'f') { /* Decimal notation? */ val += i10x(0 - prec) / 2; /* Round (nearest) */ - m = ilog10(val); - if (m < 0) m = 0; - if (m + prec + 3 >= SZ_NUM_BUF) er = "OV"; /* Buffer overflow? */ + mag = ilog10(val); + if (mag < 0) mag = 0; + if (mag + prec + 3 >= SZ_NUM_BUF) er = "OV"; /* Buffer overflow? */ } else { /* E notation */ if (val != 0) { /* Not a true zero? */ val += i10x(ilog10(val) - prec) / 2; /* Round (nearest) */ - e = ilog10(val); - if (e > 99 || prec + 7 >= SZ_NUM_BUF) { /* Buffer overflow or E > +99? */ + exp = ilog10(val); + if (exp > 99 || prec + 7 >= SZ_NUM_BUF) { /* Buffer overflow or E > +99? */ er = "OV"; } else { - if (e < -99) e = -99; - val /= i10x(e); /* Normalize */ + if (exp < -99) exp = -99; + val /= i10x(exp); /* Normalize */ } } } @@ -6867,20 +6882,20 @@ static void ftoa ( if (!er) { /* Not error condition */ if (sign == '-') *buf++ = sign; /* Add a - if negative value */ do { /* Put decimal number */ - if (m == -1) *buf++ = ds; /* Insert a decimal separator when get into fractional part */ - w = i10x(m); /* Snip the highest digit d */ - d = (int)(val / w); val -= d * w; - *buf++ = (char)('0' + d); /* Put the digit */ - } while (--m >= -prec); /* Output all digits specified by prec */ + if (mag == -1) *buf++ = ds; /* Insert a decimal separator when get into fractional part */ + w = i10x(mag); /* Snip the highest digit d */ + digit = (int)(val / w); val -= digit * w; + *buf++ = (char)('0' + digit); /* Put the digit */ + } while (--mag >= -prec); /* Output all digits specified by prec */ if (fmt != 'f') { /* Put exponent if needed */ *buf++ = (char)fmt; - if (e < 0) { - e = 0 - e; *buf++ = '-'; + if (exp < 0) { + exp = 0 - exp; *buf++ = '-'; } else { *buf++ = '+'; } - *buf++ = (char)('0' + e / 10); - *buf++ = (char)('0' + e % 10); + *buf++ = (char)('0' + exp / 10); + *buf++ = (char)('0' + exp % 10); } } } @@ -6904,17 +6919,17 @@ int f_printf ( { va_list arp; putbuff pb; - UINT i, j, w, f, r; + UINT i, j, width, flag, radix; int prec; #if FF_PRINT_LLI && FF_INTDEF == 2 - QWORD v; + QWORD val; #else - DWORD v; + DWORD val; #endif TCHAR *tp; - TCHAR tc, pad; + TCHAR chr, pad; TCHAR nul = 0; - char d, str[SZ_NUM_BUF]; + char digit, str[SZ_NUM_BUF]; putc_init(&pb, fp); @@ -6922,64 +6937,64 @@ int f_printf ( va_start(arp, fmt); for (;;) { - tc = *fmt++; - if (tc == 0) break; /* End of format string */ - if (tc != '%') { /* Not an escape character (pass-through) */ - putc_bfd(&pb, tc); + chr = *fmt++; + if (chr == 0) break; /* End of format string */ + if (chr != '%') { /* Not an escape character (pass-through) */ + putc_bfd(&pb, chr); continue; } - f = w = 0; pad = ' '; prec = -1; /* Initialize parms */ - tc = *fmt++; - if (tc == '0') { /* Flag: '0' padded */ - pad = '0'; tc = *fmt++; - } else if (tc == '-') { /* Flag: Left aligned */ - f = 2; tc = *fmt++; + flag = width = 0; pad = ' '; prec = -1; /* Initialize the parameters */ + chr = *fmt++; + if (chr == '0') { /* Flag: '0' padded */ + pad = '0'; chr = *fmt++; + } else if (chr == '-') { /* Flag: Left aligned */ + flag = 2; chr = *fmt++; } - if (tc == '*') { /* Minimum width from an argument */ - w = va_arg(arp, int); - tc = *fmt++; + if (chr == '*') { /* Minimum width from an argument */ + width = (UINT)va_arg(arp, int); + chr = *fmt++; } else { - while (IsDigit(tc)) { /* Minimum width */ - w = w * 10 + tc - '0'; - tc = *fmt++; + while (IsDigit(chr)) { /* Minimum width */ + width = width * 10 + chr - '0'; + chr = *fmt++; } } - if (tc == '.') { /* Precision */ - tc = *fmt++; - if (tc == '*') { /* Precision from an argument */ + if (chr == '.') { /* Precision */ + chr = *fmt++; + if (chr == '*') { /* Precision from an argument */ prec = va_arg(arp, int); - tc = *fmt++; + chr = *fmt++; } else { prec = 0; - while (IsDigit(tc)) { /* Precision */ - prec = prec * 10 + tc - '0'; - tc = *fmt++; + while (IsDigit(chr)) { /* Precision */ + prec = prec * 10 + chr - '0'; + chr = *fmt++; } } } - if (tc == 'l') { /* Size: long int */ - f |= 4; tc = *fmt++; + if (chr == 'l') { /* Size: long int */ + flag |= 4; chr = *fmt++; #if FF_PRINT_LLI && FF_INTDEF == 2 - if (tc == 'l') { /* Size: long long int */ - f |= 8; tc = *fmt++; + if (chr == 'l') { /* Size: long long int */ + flag |= 8; chr = *fmt++; } #endif } - if (tc == 0) break; /* End of format string */ - switch (tc) { /* Atgument type is... */ + if (chr == 0) break; /* End of format string */ + switch (chr) { /* Atgument type is... */ case 'b': /* Unsigned binary */ - r = 2; break; + radix = 2; break; case 'o': /* Unsigned octal */ - r = 8; break; + radix = 8; break; case 'd': /* Signed decimal */ case 'u': /* Unsigned decimal */ - r = 10; break; + radix = 10; break; case 'x': /* Unsigned hexadecimal (lower case) */ case 'X': /* Unsigned hexadecimal (upper case) */ - r = 16; break; + radix = 16; break; case 'c': /* Character */ putc_bfd(&pb, (TCHAR)va_arg(arp, int)); @@ -6987,64 +7002,65 @@ int f_printf ( case 's': /* String */ tp = va_arg(arp, TCHAR*); /* Get a pointer argument */ - if (!tp) tp = &nul; /* Null ptr generates a null string */ + if (!tp) tp = &nul; /* Null pointer generates a null string */ for (j = 0; tp[j]; j++) ; /* j = tcslen(tp) */ - if (prec >= 0 && j > (UINT)prec) j = prec; /* Limited length of string body */ - for ( ; !(f & 2) && j < w; j++) putc_bfd(&pb, pad); /* Left pads */ - while (*tp && prec--) putc_bfd(&pb, *tp++); /* Body */ - while (j++ < w) putc_bfd(&pb, ' '); /* Right pads */ + if (prec >= 0 && j > (UINT)prec) j = (UINT)prec; /* Limited length of string body */ + for ( ; !(flag & 2) && j < width; j++) putc_bfd(&pb, pad); /* Left padding */ + while (*tp && prec--) putc_bfd(&pb, *tp++); /* Body */ + while (j++ < width) putc_bfd(&pb, ' '); /* Right padding */ continue; #if FF_PRINT_FLOAT && FF_INTDEF == 2 case 'f': /* Floating point (decimal) */ case 'e': /* Floating point (e) */ case 'E': /* Floating point (E) */ - ftoa(str, va_arg(arp, double), prec, tc); /* Make a floating point string */ - for (j = strlen(str); !(f & 2) && j < w; j++) putc_bfd(&pb, pad); /* Left pads */ + ftoa(str, va_arg(arp, double), prec, chr); /* Make a floating point string */ + for (j = strlen(str); !(flag & 2) && j < width; j++) putc_bfd(&pb, pad); /* Leading pads */ for (i = 0; str[i]; putc_bfd(&pb, str[i++])) ; /* Body */ - while (j++ < w) putc_bfd(&pb, ' '); /* Right pads */ + while (j++ < width) putc_bfd(&pb, ' '); /* Trailing pads */ continue; #endif default: /* Unknown type (pass-through) */ - putc_bfd(&pb, tc); continue; + putc_bfd(&pb, chr); + continue; } /* Get an integer argument and put it in numeral */ #if FF_PRINT_LLI && FF_INTDEF == 2 - if (f & 8) { /* long long argument? */ - v = (QWORD)va_arg(arp, long long); - } else if (f & 4) { /* long argument? */ - v = (tc == 'd') ? (QWORD)(long long)va_arg(arp, long) : (QWORD)va_arg(arp, unsigned long); + if (flag & 8) { /* long long argument? */ + val = (QWORD)va_arg(arp, long long); + } else if (flag & 4) { /* long argument? */ + val = (chr == 'd') ? (QWORD)(long long)va_arg(arp, long) : (QWORD)va_arg(arp, unsigned long); } else { /* int/short/char argument */ - v = (tc == 'd') ? (QWORD)(long long)va_arg(arp, int) : (QWORD)va_arg(arp, unsigned int); + val = (chr == 'd') ? (QWORD)(long long)va_arg(arp, int) : (QWORD)va_arg(arp, unsigned int); } - if (tc == 'd' && (v & 0x8000000000000000)) { /* Negative value? */ - v = 0 - v; f |= 1; + if (chr == 'd' && (val & 0x8000000000000000)) { /* Negative value? */ + val = 0 - val; flag |= 1; } #else - if (f & 4) { /* long argument? */ - v = (DWORD)va_arg(arp, long); + if (flag & 4) { /* long argument? */ + val = (DWORD)va_arg(arp, long); } else { /* int/short/char argument */ - v = (tc == 'd') ? (DWORD)(long)va_arg(arp, int) : (DWORD)va_arg(arp, unsigned int); + val = (chr == 'd') ? (DWORD)(long)va_arg(arp, int) : (DWORD)va_arg(arp, unsigned int); } - if (tc == 'd' && (v & 0x80000000)) { /* Negative value? */ - v = 0 - v; f |= 1; + if (chr == 'd' && (val & 0x80000000)) { /* Negative value? */ + val = 0 - val; flag |= 1; } #endif i = 0; do { /* Make an integer number string */ - d = (char)(v % r); v /= r; - if (d > 9) d += (tc == 'x') ? 0x27 : 0x07; - str[i++] = d + '0'; - } while (v && i < SZ_NUM_BUF); - if (f & 1) str[i++] = '-'; /* Sign */ + digit = (char)(val % radix) + '0'; val /= radix; + if (digit > '9') digit += (chr == 'x') ? 0x27 : 0x07; + str[i++] = digit; + } while (val && i < SZ_NUM_BUF); + if (flag & 1) str[i++] = '-'; /* Sign */ /* Write it */ - for (j = i; !(f & 2) && j < w; j++) { /* Left pads */ + for (j = i; !(flag & 2) && j < width; j++) { /* Leading pads */ putc_bfd(&pb, pad); } - do { /* Body */ + do { /* Body */ putc_bfd(&pb, (TCHAR)str[--i]); } while (i); - while (j++ < w) { /* Right pads */ + while (j++ < width) { /* Trailing pads */ putc_bfd(&pb, ' '); } } diff --git a/source/ff.h b/source/ff.h index d88febc..e39df1d 100644 --- a/source/ff.h +++ b/source/ff.h @@ -1,8 +1,8 @@ /*----------------------------------------------------------------------------/ -/ FatFs - Generic FAT Filesystem module R0.15 / +/ FatFs - Generic FAT Filesystem module R0.15a / /-----------------------------------------------------------------------------/ / -/ Copyright (C) 2022, ChaN, all right reserved. +/ Copyright (C) 2024, ChaN, all right reserved. / / FatFs module is an open source software. Redistribution and use of FatFs in / source and binary forms, with or without modification, are permitted provided @@ -20,14 +20,15 @@ #ifndef FF_DEFINED -#define FF_DEFINED 80286 /* Revision ID */ +#define FF_DEFINED 5380 /* Revision ID */ #ifdef __cplusplus extern "C" { #endif +#if !defined(FFCONF_DEF) #include "ffconf.h" /* FatFs configuration options */ - +#endif #if FF_DEFINED != FFCONF_DEF #error Wrong configuration file (ffconf.h). #endif @@ -48,18 +49,18 @@ typedef unsigned __int64 QWORD; #include typedef unsigned int UINT; /* int must be 16-bit or 32-bit */ typedef unsigned char BYTE; /* char must be 8-bit */ -typedef uint16_t WORD; /* 16-bit unsigned integer */ -typedef uint32_t DWORD; /* 32-bit unsigned integer */ -typedef uint64_t QWORD; /* 64-bit unsigned integer */ -typedef WORD WCHAR; /* UTF-16 character type */ +typedef uint16_t WORD; /* 16-bit unsigned */ +typedef uint32_t DWORD; /* 32-bit unsigned */ +typedef uint64_t QWORD; /* 64-bit unsigned */ +typedef WORD WCHAR; /* UTF-16 code unit */ #else /* Earlier than C99 */ #define FF_INTDEF 1 typedef unsigned int UINT; /* int must be 16-bit or 32-bit */ typedef unsigned char BYTE; /* char must be 8-bit */ -typedef unsigned short WORD; /* 16-bit unsigned integer */ -typedef unsigned long DWORD; /* 32-bit unsigned integer */ -typedef WORD WCHAR; /* UTF-16 character type */ +typedef unsigned short WORD; /* short must be 16-bit */ +typedef unsigned long DWORD; /* long must be 32-bit */ +typedef WORD WCHAR; /* UTF-16 code unit */ #endif @@ -113,15 +114,15 @@ typedef char TCHAR; #if FF_MULTI_PARTITION /* Multiple partition configuration */ typedef struct { - BYTE pd; /* Physical drive number */ - BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */ + BYTE pd; /* Associated physical drive */ + BYTE pt; /* Associated partition (0:Auto detect, 1-4:Forced partition) */ } PARTITION; -extern PARTITION VolToPart[]; /* Volume - Partition mapping table */ +extern PARTITION VolToPart[]; /* Volume to partition mapping table */ #endif #if FF_STR_VOLUME_ID #ifndef FF_VOLUME_STRS -extern const char* VolumeStr[FF_VOLUMES]; /* User defied volume ID */ +extern const char* VolumeStr[FF_VOLUMES]; /* User defined volume ID table */ #endif #endif @@ -130,12 +131,12 @@ extern const char* VolumeStr[FF_VOLUMES]; /* User defied volume ID */ /* Filesystem object structure (FATFS) */ typedef struct { - BYTE fs_type; /* Filesystem type (0:not mounted) */ + BYTE fs_type; /* Filesystem type (0:blank filesystem object) */ BYTE pdrv; /* Volume hosting physical drive */ BYTE ldrv; /* Logical drive number (used only when FF_FS_REENTRANT) */ BYTE n_fats; /* Number of FATs (1 or 2) */ - BYTE wflag; /* win[] status (b0:dirty) */ - BYTE fsi_flag; /* FSINFO status (b7:disabled, b0:dirty) */ + BYTE wflag; /* win[] status (1:dirty) */ + BYTE fsi_flag; /* Allocation information control (b7:disabled, b0:dirty) */ WORD id; /* Volume mount ID */ WORD n_rootdir; /* Number of root directory entries (FAT12/16) */ WORD csize; /* Cluster size [sectors] */ @@ -146,11 +147,11 @@ typedef struct { WCHAR* lfnbuf; /* LFN working buffer */ #endif #if FF_FS_EXFAT - BYTE* dirbuf; /* Directory entry block scratchpad buffer for exFAT */ + BYTE* dirbuf; /* Directory entry block scratch pad buffer for exFAT */ #endif #if !FF_FS_READONLY - DWORD last_clst; /* Last allocated cluster */ - DWORD free_clst; /* Number of free clusters */ + DWORD last_clst; /* Last allocated cluster (Unknown if >= n_fatent) */ + DWORD free_clst; /* Number of free clusters (Unknown if >= n_fatent-2) */ #endif #if FF_FS_RPATH DWORD cdir; /* Current directory start cluster (0:root) */ @@ -272,24 +273,24 @@ typedef struct { /* File function return code (FRESULT) */ typedef enum { - FR_OK = 0, /* (0) Succeeded */ + FR_OK = 0, /* (0) Function succeeded */ FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */ FR_INT_ERR, /* (2) Assertion failed */ - FR_NOT_READY, /* (3) The physical drive cannot work */ + FR_NOT_READY, /* (3) The physical drive does not work */ FR_NO_FILE, /* (4) Could not find the file */ FR_NO_PATH, /* (5) Could not find the path */ FR_INVALID_NAME, /* (6) The path name format is invalid */ - FR_DENIED, /* (7) Access denied due to prohibited access or directory full */ - FR_EXIST, /* (8) Access denied due to prohibited access */ + FR_DENIED, /* (7) Access denied due to a prohibited access or directory full */ + FR_EXIST, /* (8) Access denied due to a prohibited access */ FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */ FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */ FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */ FR_NOT_ENABLED, /* (12) The volume has no work area */ - FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */ - FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */ - FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */ + FR_NO_FILESYSTEM, /* (13) Could not find a valid FAT volume */ + FR_MKFS_ABORTED, /* (14) The f_mkfs function aborted due to some problem */ + FR_TIMEOUT, /* (15) Could not take control of the volume within defined period */ FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */ - FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */ + FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated or given buffer is insufficient in size */ FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */ FR_INVALID_PARAMETER /* (19) Given parameter is invalid */ } FRESULT; @@ -375,7 +376,7 @@ DWORD ff_wtoupper (DWORD uni); /* Unicode upper-case conversion */ void* ff_memalloc (UINT msize); /* Allocate memory block */ void ff_memfree (void* mblock); /* Free memory block */ #endif -#if FF_FS_REENTRANT /* Sync functions */ +#if FF_FS_REENTRANT /* Sync functions */ int ff_mutex_create (int vol); /* Create a sync object */ void ff_mutex_delete (int vol); /* Delete a sync object */ int ff_mutex_take (int vol); /* Lock sync object */ @@ -389,7 +390,7 @@ void ff_mutex_give (int vol); /* Unlock sync object */ /* Flags and Offset Address */ /*--------------------------------------------------------------*/ -/* File access mode and open method flags (3rd argument of f_open) */ +/* File access mode and open method flags (3rd argument of f_open function) */ #define FA_READ 0x01 #define FA_WRITE 0x02 #define FA_OPEN_EXISTING 0x00 @@ -398,10 +399,10 @@ void ff_mutex_give (int vol); /* Unlock sync object */ #define FA_OPEN_ALWAYS 0x10 #define FA_OPEN_APPEND 0x30 -/* Fast seek controls (2nd argument of f_lseek) */ +/* Fast seek controls (2nd argument of f_lseek function) */ #define CREATE_LINKMAP ((FSIZE_t)0 - 1) -/* Format options (2nd argument of f_mkfs) */ +/* Format options (2nd argument of f_mkfs function) */ #define FM_FAT 0x01 #define FM_FAT32 0x02 #define FM_EXFAT 0x04 diff --git a/source/ffconf.h b/source/ffconf.h index 46abe70..9e3b3cf 100644 --- a/source/ffconf.h +++ b/source/ffconf.h @@ -2,7 +2,7 @@ / Configurations of FatFs Module /---------------------------------------------------------------------------*/ -#define FFCONF_DEF 80286 /* Revision ID */ +#define FFCONF_DEF 5380 /* Revision ID */ /*---------------------------------------------------------------------------/ / Function Configurations @@ -31,45 +31,45 @@ #define FF_USE_MKFS 0 -/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */ +/* This option switches f_mkfs(). (0:Disable or 1:Enable) */ #define FF_USE_FASTSEEK 0 -/* This option switches fast seek function. (0:Disable or 1:Enable) */ +/* This option switches fast seek feature. (0:Disable or 1:Enable) */ #define FF_USE_EXPAND 0 -/* This option switches f_expand function. (0:Disable or 1:Enable) */ +/* This option switches f_expand(). (0:Disable or 1:Enable) */ #define FF_USE_CHMOD 0 -/* This option switches attribute manipulation functions, f_chmod() and f_utime(). +/* This option switches attribute control API functions, f_chmod() and f_utime(). / (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */ #define FF_USE_LABEL 0 -/* This option switches volume label functions, f_getlabel() and f_setlabel(). +/* This option switches volume label API functions, f_getlabel() and f_setlabel(). / (0:Disable or 1:Enable) */ #define FF_USE_FORWARD 0 -/* This option switches f_forward() function. (0:Disable or 1:Enable) */ +/* This option switches f_forward(). (0:Disable or 1:Enable) */ #define FF_USE_STRFUNC 0 -#define FF_PRINT_LLI 1 -#define FF_PRINT_FLOAT 1 +#define FF_PRINT_LLI 0 +#define FF_PRINT_FLOAT 0 #define FF_STRF_ENCODE 3 -/* FF_USE_STRFUNC switches string functions, f_gets(), f_putc(), f_puts() and -/ f_printf(). +/* FF_USE_STRFUNC switches the string API functions, f_gets(), f_putc(), f_puts() +/ and f_printf(). / / 0: Disable. FF_PRINT_LLI, FF_PRINT_FLOAT and FF_STRF_ENCODE have no effect. -/ 1: Enable without LF-CRLF conversion. -/ 2: Enable with LF-CRLF conversion. +/ 1: Enable without LF - CRLF conversion. +/ 2: Enable with LF - CRLF conversion. / / FF_PRINT_LLI = 1 makes f_printf() support long long argument and FF_PRINT_FLOAT = 1/2 / makes f_printf() support floating point argument. These features want C99 or later. -/ When FF_LFN_UNICODE >= 1 with LFN enabled, string functions convert the character +/ When FF_LFN_UNICODE >= 1 with LFN enabled, string API functions convert the character / encoding in it. FF_STRF_ENCODE selects assumption of character encoding ON THE FILE / to be read/written via those functions. / @@ -118,15 +118,15 @@ /* The FF_USE_LFN switches the support for LFN (long file name). / / 0: Disable LFN. FF_MAX_LFN has no effect. -/ 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe. +/ 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe. / 2: Enable LFN with dynamic working buffer on the STACK. / 3: Enable LFN with dynamic working buffer on the HEAP. / -/ To enable the LFN, ffunicode.c needs to be added to the project. The LFN function +/ To enable the LFN, ffunicode.c needs to be added to the project. The LFN feature / requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and / additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled. / The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can -/ be in range of 12 to 255. It is recommended to be set it 255 to fully support LFN +/ be in range of 12 to 255. It is recommended to be set 255 to fully support the LFN / specification. / When use stack for the working buffer, take care on stack overflow. When use heap / memory for the working buffer, memory management functions, ff_memalloc() and @@ -156,9 +156,9 @@ #define FF_FS_RPATH 0 /* This option configures support for relative path. / -/ 0: Disable relative path and remove related functions. +/ 0: Disable relative path and remove related API functions. / 1: Enable relative path. f_chdir() and f_chdrive() are available. -/ 2: f_getcwd() function is available in addition to 1. +/ 2: f_getcwd() is available in addition to 1. */ @@ -175,7 +175,7 @@ /* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings. / When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive / number in the path name. FF_VOLUME_STRS defines the volume ID strings for each -/ logical drives. Number of items must not be less than FF_VOLUMES. Valid +/ logical drive. Number of items must not be less than FF_VOLUMES. Valid / characters for the volume ID strings are A-Z, a-z and 0-9, however, they are / compared in case-insensitive. If FF_STR_VOLUME_ID >= 1 and FF_VOLUME_STRS is / not defined, a user defined volume string table is needed as: @@ -188,9 +188,9 @@ /* This option switches support for multiple volumes on the physical drive. / By default (0), each logical drive number is bound to the same physical drive / number and only an FAT volume found on the physical drive will be mounted. -/ When this function is enabled (1), each logical drive number can be bound to +/ When this feature is enabled (1), each logical drive number can be bound to / arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk() -/ function will be available. */ +/ will be available. */ #define FF_MIN_SS 512 @@ -198,8 +198,8 @@ /* This set of options configures the range of sector size to be supported. (512, / 1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and / harddisk, but a larger value may be required for on-board flash memory and some -/ type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured -/ for variable sector size mode and disk_ioctl() function needs to implement +/ type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is +/ configured for variable sector size mode and disk_ioctl() needs to implement / GET_SECTOR_SIZE command. */ @@ -209,14 +209,14 @@ #define FF_MIN_GPT 0x10000000 -/* Minimum number of sectors to switch GPT as partitioning format in f_mkfs and -/ f_fdisk function. 0x100000000 max. This option has no effect when FF_LBA64 == 0. */ +/* Minimum number of sectors to switch GPT as partitioning format in f_mkfs() and +/ f_fdisk(). 2^32 sectors maximum. This option has no effect when FF_LBA64 == 0. */ #define FF_USE_TRIM 0 /* This option switches support for ATA-TRIM. (0:Disable or 1:Enable) -/ To enable Trim function, also CTRL_TRIM command should be implemented to the -/ disk_ioctl() function. */ +/ To enable this feature, also CTRL_TRIM command should be implemented to +/ the disk_ioctl(). */ @@ -238,22 +238,22 @@ #define FF_FS_NORTC 0 -#define FF_NORTC_MON 1 +#define FF_NORTC_MON 11 #define FF_NORTC_MDAY 1 -#define FF_NORTC_YEAR 2022 +#define FF_NORTC_YEAR 2024 /* The option FF_FS_NORTC switches timestamp feature. If the system does not have / an RTC or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable the / timestamp feature. Every object modified by FatFs will have a fixed timestamp / defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time. -/ To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be -/ added to the project to read current time form real-time clock. FF_NORTC_MON, +/ To enable timestamp function (FF_FS_NORTC = 0), get_fattime() need to be added +/ to the project to read current time form real-time clock. FF_NORTC_MON, / FF_NORTC_MDAY and FF_NORTC_YEAR have no effect. / These options have no effect in read-only configuration (FF_FS_READONLY = 1). */ #define FF_FS_NOFSINFO 0 /* If you need to know correct free space on the FAT32 volume, set bit 0 of this -/ option, and f_getfree() function at the first time after volume mount will force +/ option, and f_getfree() at the first time after volume mount will force / a full FAT scan. Bit 1 controls the use of last allocated cluster number. / / bit0=0: Use free cluster count in the FSINFO if available. @@ -280,13 +280,13 @@ /* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs / module itself. Note that regardless of this option, file access to different / volume is always re-entrant and volume control functions, f_mount(), f_mkfs() -/ and f_fdisk() function, are always not re-entrant. Only file/directory access -/ to the same volume is under control of this featuer. +/ and f_fdisk(), are always not re-entrant. Only file/directory access to +/ the same volume is under control of this featuer. / / 0: Disable re-entrancy. FF_FS_TIMEOUT have no effect. / 1: Enable re-entrancy. Also user provided synchronization handlers, -/ ff_mutex_create(), ff_mutex_delete(), ff_mutex_take() and ff_mutex_give() -/ function, must be added to the project. Samples are available in ffsystem.c. +/ ff_mutex_create(), ff_mutex_delete(), ff_mutex_take() and ff_mutex_give(), +/ must be added to the project. Samples are available in ffsystem.c. / / The FF_FS_TIMEOUT defines timeout period in unit of O/S time tick. */