diff --git a/documents/00index_e.html b/documents/00index_e.html index 9ada28b..743f5d2 100644 --- a/documents/00index_e.html +++ b/documents/00index_e.html @@ -5,6 +5,8 @@ + + FatFs - Generic FAT Filesystem Module @@ -25,7 +27,7 @@
  • Various configuration options to support for:
  • System Configurations @@ -55,6 +56,7 @@
  • FF_FS_EXFAT
  • FF_FS_NORTC
  • FF_NORTC_MON, FF_NORTC_MDAY, FF_NORTC_YEAR
  • +
  • FF_FS_NOFSINFO
  • FF_FS_LOCK
  • FF_FS_REENTRANT
  • FF_FS_TIMEOUT
  • @@ -116,7 +118,7 @@

    Namespace and Locale Configurations

    FF_CODE_PAGE

    -

    This option specifies the OEM code page to be used on the target system. Incorrect setting of the code page can cause a file open failure. If any non-ASCII character is not used for the path name, there is no difference between any code page settings. Set it 437 anyway.

    +

    This option specifies the OEM code page used on the target system. Incorrect setting of the code page can cause a file open failure. If any non-ASCII character is not used for path name or character encoding is Unicode, there is no difference between any code page settings. Set it 437 anyway.

    @@ -157,7 +159,7 @@

    LFN function requiers certain internal working buffer for the file name. This option defines size of the buffer and the value can be in range of 12 to 255 in UTF-16 encoding unit of the LFN. The buffer occupies (FF_MAX_LFN + 1) * 2 bytes and additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled. It is recommended to be set 255 to fully support the LFN specification. This option has no effect when LFN is not enabled.

    FF_LFN_UNICODE

    -

    This option switches character encoding for the file name on the API. When Unicode is selected, FF_CODE_PAGE has actually no meaning except for compatibility with legacy systems, such as MS-DOS and any system without support for LFN. FatFs supports the code point upto U+10FFFF.

    +

    This option switches character encoding for the file name on the API. When Unicode is selected, FF_CODE_PAGE has actually no meaning except for compatibility with legacy systems, such as MS-DOS and any system without support for LFN. FatFs supports the code point up to U+10FFFF.

    ValueCode page
    0Includes all code pages below and set by f_setcp()
    @@ -204,7 +206,7 @@

    Volume/Drive Configurations

    FF_VOLUMES

    -

    This option configures number of volumes (logical drives upto 10) to be used.

    +

    This option configures number of volumes (logical drives up to 10) to be used.

    FF_STR_VOLUME_ID

    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.

    @@ -226,11 +228,35 @@ const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sdc","usb"};

    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.

    FF_MIN_SS, FF_MAX_SS

    -

    This set of options defines the extent of sector size used on 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.

    +

    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.

    + +

    FF_LBA64

    +

    This option switches media access interface to 64-bit LBA and enables GUID Partition Table (GPT) for partition management, Enabled (1) or Disabled (0). exFAT filesystem needs to be enabled to enable this feature.

    + +

    FF_MIN_GPT

    +

    This option specifies the threshold of determination of partitioning format when create patitions on the drive in f_mkfs and f_fdisk function. When number of sectors on the drive is equal or larger than this value, the drive will be partitioned in GPT. This option has no effect when FF_LBA64 == 0.

    FF_USE_TRIM

    Disable (0) or Enable (1). This option switches ATA-TRIM function. To enable Trim function, also CTRL_TRIM command should be implemented to the disk_ioctl function.

    + + + +
    +

    System Configurations

    + +

    FF_FS_TINY

    +

    Normal (0) or Tiny (1). The tiny configuration reduces size of the FIL structure, file object, FF_MAX_SS bytes each. Instead of private sector buffer eliminated from the file object, common sector buffer in the FATFS structure, filesystem object, is used for the file data transfer.

    + +

    FF_FS_EXFAT

    +

    This option switches support for exFAT filesystem in addition to the FAT/FAT32 filesystem, Enabled (1) or Disabled (0). To enable exFAT, also LFN must be enabled and configureing FF_LFN_UNICODE >= 1 and FF_MAX_LFN == 255 is recommended for full-featured exFAT function. Note that enabling exFAT discards ANSI C (C89) compatibility and wants C99 because of need for 64-bit integer type.

    + +

    FF_FS_NORTC

    +

    Use RTC (0) or Do not use RTC (1). This option controls timestamp function. If the system does not have any RTC function or valid timestamp is not needed, set FF_FS_NORTC to 1 to disable the timestamp function. Every objects modified by FatFs will have a fixed timestamp defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR. To use the timestamp function, set FF_FS_NORTC == 0 and add get_fattime function to the project to get current time form the RTC. This option has no effect at read-only configuration.

    + +

    FF_NORTC_MON, FF_NORTC_MDAY, FF_NORTC_YEAR

    +

    This set of options defines the time to be used at no RTC systems. This option has no effect at read-only configuration or FF_FS_NORTC == 0.

    +

    FF_FS_NOFSINFO

    0 to 3. If you need to know correct free space on the FAT32 volume, set bit 0 of this option, and f_getfree function at first time after volume mount will force a full FAT scan. Bit 1 controls the use of last allocated cluster number.

    ValueCharacter EncodingTCHAR
    0ANSI/OEM in current CPchar
    @@ -241,24 +267,6 @@ const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sdc","usb"};
    bit1=1Do not trust last allocated cluster number in the FSINFO.
    - - - -
    -

    System Configurations

    - -

    FF_FS_TINY

    -

    Normal (0) or Tiny (1). At the tiny configuration, size of the file object FIL is reduced FF_MAX_SS bytes. Instead of private data buffer eliminated from the file object, common sector buffer in the filesystem object FATFS is used for the file data transfer.

    - -

    FF_FS_EXFAT

    -

    This option switches support for the exFAT filesystem in addition to the FAT/FAT32 filesystem, Enabled (1) or Disabled (0). To enable exFAT, also LFN must be enabled and configureing FF_LFN_UNICODE >= 1 and FF_MAX_LFN == 255 is recommended for full-featured exFAT function. Note that enabling exFAT discards ANSI C (C89) compatibility because of need for 64-bit integer type.

    - -

    FF_FS_NORTC

    -

    Use RTC (0) or Do not use RTC (1). This option controls timestamp function. If the system does not have any RTC function or valid timestamp is not needed, set FF_FS_NORTC to 1 to disable the timestamp function. Every objects modified by FatFs will have a fixed timestamp defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR. To use the timestamp function, set FF_FS_NORTC == 0 and add get_fattime function to the project to get current time form the RTC. This option has no effect at read-only configuration.

    - -

    FF_NORTC_MON, FF_NORTC_MDAY, FF_NORTC_YEAR

    -

    This set of options defines the time to be used at no RTC systems. This option has no effect at read-only configuration or FF_FS_NORTC == 0.

    -

    FF_FS_LOCK

    This option switches file lock function to control duplicated file open and illegal operations to open objects. Note that the file lock function is independent of re-entrancy. This option must be 0 at read-only configuration.

    diff --git a/documents/doc/dioctl.html b/documents/doc/dioctl.html index a03173e..0eacd48 100644 --- a/documents/doc/dioctl.html +++ b/documents/doc/dioctl.html @@ -57,35 +57,35 @@ DRESULT disk_ioctl (
    - - - - - + + + + +
    Standard ioctl command used by FatFs
    CommandDescription
    CTRL_SYNCMake sure that the device has finished pending write process. If the disk I/O module has a write back cache, the dirty buffers must be written back to the media immediately. Nothing to do for this command if each write operation to the media is completed within the disk_write function.
    GET_SECTOR_COUNTReturns number of available sectors on the drive into the DWORD variable pointed by buff. This command is used by f_mkfs and f_fdisk function to determine the volume/partition size to be created. Required at FF_USE_MKFS == 1.
    GET_SECTOR_SIZEReturns sector size of the device into the WORD variable pointed by buff. Valid return values for this command 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 is never used and the device must work at that sector size.
    GET_BLOCK_SIZEReturns erase block size of the flash memory media in unit of sector into the DWORD variable pointed by buff. The allowable value is 1 to 32768 in power of 2. Return 1 if the erase block size 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 erase block boundary. Required at FF_USE_MKFS == 1.
    CTRL_TRIMInforms the device the data on the block of sectors is no longer needed and it can be erased. The sector block is specified by a DWORD array {<start sector>, <end sector>} 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. Required at FF_USE_TRIM == 1.
    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 media immediately. Nothing to do for this command if each write operation to the media is completed within the disk_write function.
    GET_SECTOR_COUNTRetrieves number of available sectors, the largest allowable LBA + 1, on the drive into the LBA_t variable 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 used for read/write function into the WORD variable 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 only.
    GET_BLOCK_SIZERetrieves erase block size of the flash memory media in unit of sector into the DWORD variable pointed by buff. The allowable value is 1 to 32768 in power of 2. Return 1 if the erase block size 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 erase block boundary. It is required when FF_USE_MKFS == 1.
    CTRL_TRIMInforms the device 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>} 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.

    FatFs never uses any device dependent command nor user defined command. Following table shows an example of non-standard commands which may be useful for some applications.

    - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + +
    Example of optional ioctl command
    CommandDescription
    CTRL_FORMATCreate a physical format on the media. If buff is not null, it is pointer to the call-back function for progress notification.
    CTRL_POWER_IDLEPut the device idle state. STA_NOINIT in the current status flags may not be set if the device goes active state by generic read/write function.
    CTRL_POWER_OFFPut the device off state. Shut-down the power to the device and deinitialize the device interface if needed. STA_NOINIT in the current status flags must be set. The device goes active state by disk_initialize function.
    CTRL_LOCKLock media eject mechanism.
    CTRL_UNLOCKUnlock media eject mechanism.
    CTRL_EJECTEject media cartridge. STA_NOINIT and STA_NODISK in status flag are set after the function succeeded.
    CTRL_GET_SMARTRead SMART information.
    MMC_GET_TYPEGet card type. The type flags, bit0:MMCv3, bit1:SDv1, bit2:SDv2+ and bit3:LBA, is stored to a BYTE variable pointed by buff. (MMC/SDC specific command)
    MMC_GET_CSDRead CSD register into a 16-byte buffer pointed by buff. (MMC/SDC specific command)
    MMC_GET_CIDRead CID register into a 16-byte buffer pointed by buff. (MMC/SDC specific command)
    MMC_GET_OCRRead OCR register into a 4-byte buffer pointed by buff. (MMC/SDC specific command)
    MMC_GET_SDSTATRead SDSTATUS register into a 64-byte buffer pointed by buff. (SDC specific command)
    ATA_GET_REVGet the revision string into a 16-byte buffer pointed by buff. (ATA/CFC specific command)
    ATA_GET_MODELGet the model string into a 40-byte buffer pointed by buff. (ATA/CFC specific command)
    ATA_GET_SNGet the serial number string into a 20-byte buffer pointed by buff. (ATA/CFC specific command)
    ISDIO_READRead a block of iSDIO registers specified by command structure pointed by buff. (FlashAir specific command)
    ISDIO_WRITEWrite a block of data to iSDIO registers specified by command structure pointed by buff. (FlashAir specific command)
    ISDIO_MRITEChange bits in an iSDIO register specified by command structure pointed by buff. (FlashAir specific command)
    CTRL_FORMATCreates a physical format on the media. If buff is not null, it is pointer to the call-back function for progress notification.
    CTRL_POWER_IDLEPuts the device idle state. STA_NOINIT in the current status flags may not be set if the device goes active state by generic read/write function.
    CTRL_POWER_OFFPuts the device off state. Shut-down the power to the device and deinitialize the device interface if needed. STA_NOINIT in the current status flags must be set. The device goes active state by disk_initialize function.
    CTRL_LOCKLocks media eject mechanism.
    CTRL_UNLOCKUnlocks media eject mechanism.
    CTRL_EJECTEjects media cartridge. STA_NOINIT and STA_NODISK in status flag are set after the function succeeded.
    CTRL_GET_SMARTReads SMART information.
    MMC_GET_TYPEGets card type. The type flags, bit0:MMCv3, bit1:SDv1, bit2:SDv2+ and bit3:LBA, is stored to a BYTE variable pointed by buff. (MMC/SDC specific command)
    MMC_GET_CSDReads CSD register and sets it into a 16-byte buffer pointed by buff. (MMC/SDC specific command)
    MMC_GET_CIDReads CID register and sets it into a 16-byte buffer pointed by buff. (MMC/SDC specific command)
    MMC_GET_OCRReads OCR register and sets it into a 4-byte buffer pointed by buff. (MMC/SDC specific command)
    MMC_GET_SDSTATReads SDSTATUS register and sets it into a 64-byte buffer pointed by buff. (SDC specific command)
    ATA_GET_REVGets the revision string and sets it into a 16-byte buffer pointed by buff. (ATA/CFC specific command)
    ATA_GET_MODELGets the model string and sets it into a 40-byte buffer pointed by buff. (ATA/CFC specific command)
    ATA_GET_SNGets the serial number string and sets it into a 20-byte buffer pointed by buff. (ATA/CFC specific command)
    ISDIO_READReads a block of iSDIO registers specified by command structure pointed by buff. (FlashAir specific command)
    ISDIO_WRITEWrites a block of data to iSDIO registers specified by command structure pointed by buff. (FlashAir specific command)
    ISDIO_MRITEChanges bits in an iSDIO register specified by command structure pointed by buff. (FlashAir specific command)
    diff --git a/documents/doc/dread.html b/documents/doc/dread.html index 52d60f1..71821ae 100644 --- a/documents/doc/dread.html +++ b/documents/doc/dread.html @@ -18,7 +18,7 @@ DRESULT disk_read ( BYTE pdrv, /* [IN] Physical drive number */ BYTE* buff, /* [OUT] Pointer to the read data buffer */ - DWORD sector, /* [IN] Start sector number */ + LBA_t sector, /* [IN] Start sector number */ UINT count /* [IN] Number of sectros to read */ ); @@ -32,7 +32,7 @@ DRESULT disk_read (
    buff
    Pointer to the first item of the byte array to store read data. Size of read data will be the sector size * count bytes.
    sector
    -
    Start sector number in 32-bit LBA.
    +
    Start sector number in 32-bit or 64-bit LBA. The data type LBA_t is an alias of DWORD or QWORD depends on the configuration option.
    count
    Number of sectors to read.
    @@ -56,13 +56,14 @@ DRESULT disk_read (

    Description

    -

    Read/write operation to the generic storage devices, such as memory card, hadddisk and optical disk, is done in unit of block of data bytes called sector. FatFs supports the sector size in range of 512 to 4096 bytes. When FatFs is configured for fixed sector size (FF_MIN_SS == FF_MAX_SS, this is the most case), the read/write function must work at that sector size. When FatFs is configured for variable sector size (FF_MIN_SS < FF_MAX_SS), the sector size of medium is inquired with disk_ioctl function immediately following disk_initialize function succeeded.

    -

    The memory address specified by buff is not that always aligned to word boundary because the argument is defined as BYTE*. The unaligned read/write request can occure at direct transfer. If the bus architecture, especially DMA controller, does not allow unaligned memory access, it should be solved in this function. There are some workarounds described below to avoid this issue.

    +

    Read/write operation to the generic storage devices, such as memory card, hadddisk and optical disk, is done in unit of block of data bytes called sector. FatFs supports the sector size in range of 512 to 4096 bytes. When FatFs is configured for fixed sector size (FF_MIN_SS == FF_MAX_SS, this is the most case), the generic read/write function must work at the sector size only. When FatFs is configured for variable sector size (FF_MIN_SS < FF_MAX_SS), the sector size of medium is inquired with disk_ioctl function after disk_initialize function succeeded.

    +

    There are some considerations about the memory addres passed via buff. It is not that always aligned with the word boundary because the argument is defined as BYTE*. The unaligned transfer request can occure at direct transfer. If the bus architecture, especially DMA controller, does not allow unaligned memory access, it should be solved in this function. If it is the case, there are some workarounds described below to avoid this issue.

    +

    Also the memory area may be out of reach in DMA. This is the case if it is located on the tightly coupled memory which is usually used for stack. Use double buffered transfer, or avoid to define file I/O buffer, FATFS and FIL structure as local variables where on the stack.

    Generally, a multiple sector read request must not be split into single sector transactions to the storage device, or read throughput gets worse.

    diff --git a/documents/doc/dwrite.html b/documents/doc/dwrite.html index 846da65..0241245 100644 --- a/documents/doc/dwrite.html +++ b/documents/doc/dwrite.html @@ -18,7 +18,7 @@ DRESULT disk_write ( BYTE pdrv, /* [IN] Physical drive number */ const BYTE* buff, /* [IN] Pointer to the data to be written */ - DWORD sector, /* [IN] Sector number to write from */ + LBA_t sector, /* [IN] Sector number to write from */ UINT count /* [IN] Number of sectors to write */ ); @@ -32,7 +32,7 @@ DRESULT disk_write (
    buff
    Pointer to the first item of the byte array to be written. The size of data to be written is sector size * count bytes.
    sector
    -
    Start sector number in 32-bit LBA.
    +
    Start sector number in 32-bit or 64-bit LBA. The data type LBA_t is an alias of DWORD or QWORD depends on the configuration option.
    count
    Number of sectors to write.
    diff --git a/documents/doc/expand.html b/documents/doc/expand.html index 12e5559..8588f32 100644 --- a/documents/doc/expand.html +++ b/documents/doc/expand.html @@ -19,7 +19,7 @@ FRESULT f_expand ( FIL* fp, /* [IN] File object */ FSIZE_t fsz, /* [IN] File size expanded to */ - BYTE opt /* [IN] Operation mode */ + BYTE opt /* [IN] Allocation mode */ ); @@ -32,7 +32,7 @@ FRESULT f_expand (
    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
    -
    Operation mode. Prepare only (0) or Allocate now (1).
    +
    Allocation mode. Prepare to allocate (0) or Allocate now (1).
    @@ -52,16 +52,16 @@ FRESULT f_expand (

    Description

    -

    The f_expand function prepares or allocates a contiguous data area to the file. When opt is 1, the function allocates a contiguous data area to the file. Unlike expansion of file by f_lseek function, the file must be truncated prior to use this function and read/write pointer of the file stays at top of the file after the function. The file content allocated with this function is undefined because no data is written to the file in this process. The function can fail with FR_DENIED due to some reasons below.

    +

    The f_expand function prepares or allocates a contiguous data area to the file. When opt is 1, the function allocates a contiguous data area to the file. Unlike expansion of file size by f_lseek function, the file must be truncated prior to use this function and read/write pointer of the file stays at offset 0 after the function call. The file content allocated with this function is undefined because no data is written to the file in this process. The function can fail with FR_DENIED due to some reasons below.

    -

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

    -

    The contiguous file would have an advantage at time-critical read/write operations. It eliminates some overheads in the filesystem and the storage media caused by random access due to fragmented file data. Especially FAT access for the contiguous file on the exFAT volume is completely eliminated and storage media will be accessed sequentially.

    -

    Also the contiguous file can be easily accessed directly via low-level disk functions. But this is not recommended in consideration for future compatibility. Use this function to examine if the file is contiguous or not.

    +

    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 that 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. But this is not recommended in consideration for 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.

    diff --git a/documents/doc/fdisk.html b/documents/doc/fdisk.html index d58db7e..e4cc46d 100644 --- a/documents/doc/fdisk.html +++ b/documents/doc/fdisk.html @@ -16,9 +16,9 @@

    The f_fdisk fucntion divides a physical drive.

     FRESULT f_fdisk (
    -  BYTE  pdrv,        /* [IN] Physical drive number */
    -  const DWORD* szt,  /* [IN] Partition map table */
    -  void* work         /* [IN] Work area */
    +  BYTE  pdrv,         /* [IN] Physical drive number */
    +  const LBA_t ptbl[], /* [IN] Partition map table */
    +  void* work          /* [IN] Work area */
     );
     
    @@ -28,10 +28,10 @@ FRESULT f_fdisk (
    pdrv
    Specifies the physical drive to be divided. This is not the logical drive number but the drive identifier passed to the low level disk functions.
    -
    szt
    -
    Pointer to the first item of the partition map table.
    +
    ptbl
    +
    List of partition size to create on the drive. The data type LBA_t is an alias of DWORD or QWORD depends on the configuration option FF_LBA64.
    work
    -
    Pointer to the function work area. The size must be at least FF_MAX_SS bytes. When a null pointer is given, the function allocates a memory block for the working buffer (at only FF_USE_LFN == 3).
    +
    Pointer to the function work area. The size must be at least FF_MAX_SS bytes. When a null pointer is given with FF_USE_LFN = 3, a memory block is obtained in this function for the working buffer.
    @@ -49,7 +49,8 @@ FRESULT f_fdisk (

    Description

    -

    The f_fdisk function creates partitions on the physical drive. The partitioning format is in generic FDISK format, so that it can create upto four primary partitions. Logical volumes in the extended partition is not supported. The partition map table with four items specifies how to divide the physical drive. The first item specifies the size of first primary partition and fourth item specifies the fourth primary partition. If the value is less than or equal to 100, it specifies the partition size in percentage of the entire drive space. If it is larger than 100, it specifies the partition size in unit of sector. The partitions are located on the drive in order of from first item.

    +

    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 clipped at the size of remaining space.

    +

    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.

    @@ -60,9 +61,9 @@ FRESULT f_fdisk (

    Example

    -    /* Volume management table defined by user (required when FF_MULTI_PARTITION == 1) */
    +    /* Volume mapping table defined by user (required when FF_MULTI_PARTITION == 1) */
     
    -    PARTITION VolToPart[] = {
    +    PARTITION VolToPart[FF_VOLUMES] = {
             {0, 1},    /* "0:" ==> Physical drive 0, 1st partition */
             {0, 2},    /* "1:" ==> Physical drive 0, 2nd partition */
             {1, 0}     /* "2:" ==> Physical drive 1, auto detection */
    @@ -71,13 +72,15 @@ FRESULT f_fdisk (
     
         /* Initialize a brand-new disk drive mapped to physical drive 0 */
     
    -    DWORD plist[] = {50, 50, 0, 0};  /* Divide drive into two partitions */
    -    BYTE work[FF_MAX_SS];
    +    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 */
    +                 /* {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_mkfs("0:", FM_ANY, work, sizeof work);    /* Create FAT volume on the logical drive 0 */
    -    f_mkfs("1:", FM_ANY, 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 */
     
     
    diff --git a/documents/doc/filename.html b/documents/doc/filename.html index 31e9e3d..3a260a9 100644 --- a/documents/doc/filename.html +++ b/documents/doc/filename.html @@ -10,14 +10,14 @@ -

    Path Names on the FatFs

    +

    Path Names on the FatFs API

    Format of the Path Names

    The format of path name on the FatFs module is similer to the filename specs of DOS/Windos as follows:

    [drive#:][/]directory/file
    -

    The FatFs module supports long file name (LFN) and 8.3 format file name (SFN). The LFN can be used when FF_USE_LFN >= 1. The sub directories are separated with a \ or / in the same way as DOS/Windows API. Duplicated separators are skipped and ignored. Only a difference is that the heading drive prefix to specify logical drive is in a numeral + colon. When drive prefix is omitted, the drive number is assumed as default drive (drive 0 or current drive).

    -

    Control characters (\0 to \x1F) are recognized as end of the path name. Leading/embedded white spaces in the path name are valid as a part of the name at LFN configuration but the white space is recognized as end of the path name at non-LFN configuration. Trailing white spaces and dots are ignored at both configurations.

    +

    The FatFs module supports long file name (LFN) and 8.3 format file name (SFN). The LFN can be used when FF_USE_LFN >= 1. The sub-directories are separated with a \ or / as the same way as DOS/Windows API. Duplicated separators are skipped and ignored. Only a difference is that the heading drive prefix to specify the logical drive number is in a numeral (0-9) + a colon, while it is an alphabet (A-Z) + a colon in DOS/Windows. The logical drive number is identifier to specify the FAT volume to be accessed. When drive prefix is omitted, the logical drive number is assumed as default drive.

    +

    Control characters (\0 to \x1F) are recognized as end of the path name. Leading/embedded white spaces in the path name are valid as a part of the name in LFN configuration but the white space is recognized as end of the path name in non-LFN configuration. Trailing white spaces and dots are ignored in both configurations.

    In default configuration (FF_FS_RPATH == 0), it does not have a concept of current directory like OS oriented filesystem. Every object on the volume is always specified in full path name that followed from the root directory. Dot directory names (".", "..") are not allowed. Heading separator is ignored and it can be exist or omitted. The default drive is fixed to drive 0.

    When relative path is enabled (FF_FS_RPATH >= 1), specified path is followed from the root directory if a heading separator is exist. If not, it is followed from the current directory of the drive set by f_chdir function. Dot names are also allowed for the path names. The default drive is the current drive set by f_chdrive function.

    @@ -42,44 +42,47 @@

    Legal Characters and Case Sensitivity

    -

    On the FAT filesystem, legal characters for object name (file/directory name) are, 0-9 A-Z ! # $ % & ' ( ) - @ ^ _ ` { } ~ and any extended character. The valid character codes of extended characters are depends on the configured code page. Under LFN supported system, also + , ; = [ ] and white space are legal for the object name and the white spaces and dots can be placed anywhere in the path name except for end of the name.

    +

    On the FAT filesystem, legal characters for object name (file/directory name) are, 0-9 A-Z ! # $ % & ' ( ) - @ ^ _ ` { } ~ and any extended character. The valid character codes of extended characters are depends on the configured code page. Under LFN supported system, also + , ; = [ ] and white space are legal for the object name and the white spaces and dots can be placed anywhere in the path name except end of the name.

    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. 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 function is enabled.

    -

    As for the MS-DOS and PC DOS for CJK (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 any 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 at only non-LFN with DBCS configuration (DOS/DBCS specs). But at LFN configuration, FatFs works with case-insensitive to the extended character (WindowsNT specs).

    +

    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 any 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). But in LFN configuration, FatFs works with case-insensitive to the extended character (WindowsNT specs).

    Unicode API

    -

    The path names are input/output in either ANSI/OEM code or Unicode depends on the configuration options. The type of arguments which specifies the path names is defined as TCHAR. It is an alias of char by default and the code set used for the path name string is ANSI/OEM specifid by FF_CODE_PAGE. When FF_LFN_UNICODE is set to 1 or larger, the type of the TCHAR is switched to proper type to support the Unicode string. When Unicode API is specified by this option, the full-featured LFN specification is supported and the Unicode specific characters, such as ✝☪✡☸☭, can also be used for the path name. It also affects data types and encoding of the string I/O functions. To define literal strings, _T(s) and _TEXT(s) macro are available to select either ANSI/OEM or Unicode automatically. The code shown below is an example to define the literal strings.

    +

    The path names are input/output in either ANSI/OEM code or Unicode depends on the configuration options. The type of arguments which specifies the path names is defined as TCHAR. It is an alias of char by default and the code set used for the path name string is ANSI/OEM specifid by FF_CODE_PAGE. When FF_LFN_UNICODE is set to 1 or larger, the type of the TCHAR is switched to proper type to support the Unicode string. When Unicode API is specified by this option, the full-featured LFN specification is supported and the Unicode specific characters, such as ✝☪✡☸☭, can also be used for the path name. It also affects data types and encoding of the string I/O functions. To define literal strings, _T(s) and _TEXT(s) macro are available to specify the string in proper type. The code shown below is an example to define the literal strings.

    - f_open(fp, "filename.txt", FA_READ);      /* ANSI/OEM string (char) */
    - f_open(fp, L"filename.txt", FA_READ);     /* UTF-16 string (WCHAR) */
    - f_open(fp, u8"filename.txt", FA_READ);    /* UTF-8 string (char) */
    - f_open(fp, U"filename.txt", FA_READ);     /* UTF-32 string (DWORD) */
    - f_open(fp, _T("filename.txt"), FA_READ);  /* Changed by configuration (TCHAR) */
    + f_open(fp, "filename.txt", FA_READ);      /* ANSI/OEM string (char) */
    + f_open(fp, L"filename.txt", FA_READ);     /* UTF-16 string (WCHAR) */
    + f_open(fp, u8"filename.txt", FA_READ);    /* UTF-8 string (char) */
    + f_open(fp, U"filename.txt", FA_READ);     /* UTF-32 string (DWORD) */
    + f_open(fp, _T("filename.txt"), FA_READ);  /* Changed by configuration (TCHAR) */
     

    Volume Management

    -

    FatFs module requires dynamic work area, filesystem object, for each volume (logical drive). It is registered/unregistered to the FatFs module by f_mount function. By default, each logical drive is bound to the physical drive with the same drive number and an FAT volume on the drive is serched by the volume mount process. It reads boot sectors and checks it if it is an FAT boot sector in order of sector 0 as SFD format, 1st partition, 2nd partition, 3rd partition and 4th partition as FDISK format.

    -

    When FF_MULTI_PARTITION = 1 is specified by configuration option, each individual logical drive is bound to the partition on the physical drive specified by volume management table. The volume management table needs to be defined by user to resolve the mappings of logical drives and partitions. Following code is an example of a volume management table.

    +

    By default, each logical drive is associated with the physical drive in same drive number. An FAT volume on the physical drive is serched by the volume mount process. It reads boot sectors and checks it if it is an FAT boot sector in order of LBA 0 as SFD format, 1st partition, 2nd partition, 3rd partition, ..., as MBR or GPT format.

    +

    When multiple partition feature is enabled, FF_MULTI_PARTITION = 1, each individual logical drive is associated with arbitrary partition or drive specified by volume management table, VolToPart[]. The table needs to be defined by user to resolve the mappings of logical drives and partitions. Following code is an example of a volume management table.

    -Example: "0:", "1:" and "2:" are tied to three pri-partitions on the physical drive 0 (fixed drive)
    -         "3:" is tied to an FAT volume on the physical drive 1 (removable drive)
    +Example: "0:", "1:" and "2:" are associated with three partitions on the physical drive 0 (fixed drive)
    +         "3:" is associated with physical drive 1 (removable drive)
     
     PARTITION VolToPart[FF_VOLUMES] = {
    -    {0, 1},     /* "0:" ==> Physical drive 0, 1st partition */
    -    {0, 2},     /* "1:" ==> Physical drive 0, 2nd partition */
    -    {0, 3},     /* "2:" ==> Physical drive 0, 3rd partition */
    -    {1, 0}      /* "3:" ==> Physical drive 1, auto detection */
    +    {0, 1},     /* "0:" ==> 1st partition on physical drive 0 */
    +    {0, 2},     /* "1:" ==> 2nd partition on physical drive 0 */
    +    {0, 3},     /* "2:" ==> 3rd partition on physical drive 0 */
    +    {1, 0}      /* "3:" ==> Physical drive 1 */
     };
     
    relationship between logical drive and physical drive

    There are some considerations on using multi-partition configuration.

      -
    • The physical drive that has two or more mounted partitions must be non-removable. Media change while a system operation is prohibited.
    • -
    • Only four primary partitions can be specified. Extended partition is not supported.
    • -
    • Windows does not support multiple volumes on the removable storage. Only first parition will be recognized.
    • +
    • The physical drive that hosts two or more mounted partitions must be non-removable, or all volumes on the drive must be unmounted when remove the medium.
    • +
    • When make any change to the VolToPart[], corresponding volume should be unmounted prior to make change the item.
    • +
    • On the MBR format drive, up to four primary partitions (1-4) can be specified. The partition 1 specifies the first item in the partition table and the partition 2 specifies the second one, and so on. Logical patitions in the extended partition is not supported.
    • +
    • On the GPT format drive, there is no limitation. The partition 1 specifies the first Microsoft basic data partition found in the partition table and the partition 2 specifies the second one found, and so on.
    • +
    • Windows does not support multiple volumes on the storage with removable class. Only the first parition found on the drive will be mounted.
    • +
    • For further information, refer to f_fdisk and f_mkfsfunction.
    diff --git a/documents/doc/findfirst.html b/documents/doc/findfirst.html index 106d75a..495cfad 100644 --- a/documents/doc/findfirst.html +++ b/documents/doc/findfirst.html @@ -61,12 +61,12 @@ FRESULT f_findfirst (

    Description

    -

    After the directory specified by path could be opened, it starts to search the directory for items with the name specified by pattern. If the first item is found, the information about the object is stored into the file information structure fno.

    -

    The matching pattern can contain wildcard characters (? and *). A ? matches an any character and an * matches an any string in length of zero or longer. When support of long file name is enabled, only fname[] is tested at FF_USE_FIND == 1 and also altname[] is tested at FF_USE_FIND == 2. In this revision, there are some differences listed below between FatFs and standard systems in matching condition.

    +

    After the directory specified by path could be opened, it starts to search the directory for items with the matching pattern specified by pattern. If the first item is found, the information about the item is stored into the file information structure fno.

    +

    The matching pattern can contain wildcard characters (? and *). For example, a ? matches an any character, an * matches an any string in length of zero or longer and ???* matches an any string in length of 3 characters or longer. When LFN is enabled, only fname[] is tested when FF_USE_FIND == 1 and also altname[] is tested when FF_USE_FIND == 2. In this revision, there are some differences listed below between FatFs and standard systems in matching condition.

      -
    • "*.*" never matches any name without extension while it matches any name with or without extension at the standard systems.
    • -
    • Any pattern terminated with a period never matches any name while it matches any name without extensiton at the standard systems.
    • -
    • DBCS extended characters are compared in case-sensitive at LFN with ANSI/OEM API.
    • +
    • "*.*" never matches any name without extension while it matches any name with or without extension in standard systems.
    • +
    • Any pattern terminated with a period never matches any name while it matches the name without extensiton in standard systems.
    • +
    • DBCS extended characters are compared in case-sensitive when LFN is enabled with ANSI/OEM API.
    @@ -85,13 +85,13 @@ FRESULT f_findfirst ( void find_image_file (void) { FRESULT fr; /* Return value */ - DIR dj; /* Directory search object */ + DIR dj; /* Directory object */ FILINFO fno; /* File information */ fr = f_findfirst(&dj, &fno, "", "dsc*.jpg"); /* Start to search for photo files */ while (fr == FR_OK && fno.fname[0]) { /* Repeat while an item is found */ - printf("%s\n", fno.fname); /* Display the object name */ + printf("%s\n", fno.fname); /* Print the object name */ fr = f_findnext(&dj, &fno); /* Search for next item */ } diff --git a/documents/doc/getfree.html b/documents/doc/getfree.html index a7bc882..bf2386c 100644 --- a/documents/doc/getfree.html +++ b/documents/doc/getfree.html @@ -53,7 +53,7 @@ FRESULT f_getfree (

    Descriptions

    -

    The f_getfree function gets number of free clusters on the volume. The member csize in the filesystem object indicates number of sectors per cluster, so that the free space in unit of sector can be calcurated with this information. When FSINFO structure on the FAT32 volume is not in sync, this function can return an incorrect free cluster count. To avoid this problem, FatFs can be forced full FAT scan by FF_FS_NOFSINFO option.

    +

    The f_getfree function gets number of free clusters on the volume. The member csize in the filesystem object indicates number of sectors per cluster, so that the free space in unit of sector can be calcurated with this information. In case of FSINFO structure on the FAT32 volume is not in sync, this function can return an incorrect free cluster count. To avoid this problem, FatFs can be forced full FAT scan by FF_FS_NOFSINFO option.

    diff --git a/documents/doc/lseek.html b/documents/doc/lseek.html index 3600fa3..e21a1c4 100644 --- a/documents/doc/lseek.html +++ b/documents/doc/lseek.html @@ -49,13 +49,13 @@ FRESULT f_lseek (

    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.

    -

    When an offset beyond the file size is specified at write mode, the file size is expanded to the specified offset. The file data in the expanded area 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 the expected value, 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. 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 the expected value, either of followings has been occured.

    • End of file. The specified ofs was clipped at end of the file because the file has been opened in read-only mode.
    • Disk full. There is no free space on the volume to expand the file.
    -

    The fast seek function enables fast backward/long seek operations without FAT access by using an on-memory CLMT (cluster link map table). It is applied to f_read and f_write function as well, however, the file size cannot be expanded by f_write, f_lseek function while the file is at fast seek mode.

    -

    The fast seek mode is enabled when the member cltbl in the file object is not NULL. The CLMT must be created into the DWORD array prior to use the fast seek function. To create the CLMT, set address of the DWORD array to the member cltbl in the open file object, set the size of array in unit of items to the first item and call the f_lseek function with ofs = CREATE_LINKMAP. After the function succeeded and CLMT is created, no FAT access is occured in subsequent f_read, f_write, f_lseek function to the file. The number of items used or required is returned into the first item of the array. The number of items to be used is (number of the file fragments + 1) * 2. For example, when the file is fragmented in 5, 12 items in the array will be used. If the function failed with FR_NOT_ENOUGH_CORE, the given array size is insufficient for the file.

    +

    The fast seek feature enables fast backward/long seek operations without FAT access by using an on-memory CLMT (cluster link map table). It is applied to f_read and f_write function as well, however, the file size cannot be expanded by f_write, f_lseek function while the file is at fast seek mode.

    +

    The fast seek mode is enabled when FF_USE_FASTSEEK = 1. The CLMT must be created into the DWORD array prior to use the fast seek mode. To create the CLMT, set address of the DWORD array to the member cltbl in the open file object, set the size of array in unit of items to the first item and call f_lseek function with ofs = CREATE_LINKMAP. After the function succeeded and CLMT is created, no FAT access is occured in subsequent f_read, f_write, f_lseek function to the file. The number of items used or required is returned into the first item of the array. The number of items to be used is (number of the file fragments + 1) * 2. For example, 12 items in the array will be used for the file fragmented in 5 portions. If the function failed with FR_NOT_ENOUGH_CORE, the size of given array is insufficient for the file.

    @@ -103,7 +103,7 @@ FRESULT f_lseek ( res = f_close(fp);
    -/* Using fast seek function */
    +/* Using fast seek mode */
     
         DWORD clmt[SZ_TBL];                    /* Cluster link map table buffer */
     
    @@ -111,7 +111,7 @@ FRESULT f_lseek (
     
         res = f_lseek(fp, ofs1);               /* This is normal seek (cltbl is nulled on file open) */
     
    -    fp->cltbl = clmt;                      /* Enable fast seek function (cltbl != NULL) */
    +    fp->cltbl = clmt;                      /* Enable fast seek mode (cltbl != NULL) */
         clmt[0] = SZ_TBL;                      /* Set table size */
         res = f_lseek(fp, CREATE_LINKMAP);     /* Create CLMT */
         ...
    diff --git a/documents/doc/mkfs.html b/documents/doc/mkfs.html
    index 4ea6937..ebb9726 100644
    --- a/documents/doc/mkfs.html
    +++ b/documents/doc/mkfs.html
    @@ -13,14 +13,13 @@
     
     

    f_mkfs

    -

    The f_mkfs fucntion creates an FAT/exFAT volume on the logical drive.

    +

    The f_mkfs function creates an FAT/exFAT volume on the logical drive.

     FRESULT f_mkfs (
    -  const TCHAR* path,  /* [IN] Logical drive number */
    -  BYTE  opt,          /* [IN] Format options */
    -  DWORD au,           /* [IN] Size of the allocation unit */
    -  void* work,         /* [-]  Working buffer */
    -  UINT len            /* [IN] Size of working buffer */
    +  const TCHAR*  path,  /* [IN] Logical drive number */
    +  const MKFS_PARM* opt,/* [IN] Format options */
    +  void*  work,         /* [-]  Working buffer */
    +  UINT  len            /* [IN] Size of working buffer */
     );
     
    @@ -29,15 +28,25 @@ FRESULT f_mkfs (

    Parameters

    path
    -
    Pointer to the null-terminated string specifies the logical drive to be formatted. If it has no drive number in it, it means the default drive. The logical drive may or may not be mounted for the format process.
    +
    Pointer to the null-terminated string specifies the logical drive to be formatted. If it does not have a drive number in it, it means to specify the default drive. The logical drive may or may not have been mounted for the format process.
    opt
    -
    Specifies the format option in combination of 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 FAT type to be created on the volume. If two or more types are specified, one out of them will be selected depends on the volume size and au. The flag FM_SFD specifies to create the volume on the drive in SFD format.
    -
    au
    -
    Specifies size of the allocation unit (cluter) in unit of byte. The valid value is n times the sector size. The n is power of 2 from 1 to 128 for FAT volume and upto 16MiB for exFAT volume. If zero is given, the default allocation unit size is selected depends on the volume size.
    +
    Specifies the structure that is holding format options. If a null pointer is given, it gives the function all options in default value. The format option structure has five members described below:
    +
    +
    BYTE fmt
    +
    Specifies 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 FAT type to be created on the volume. 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.
    +
    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 and up to 16 MB for exFAT volume. If a zero (the default value) or any invalid value is given, the default allocation unit size depends on the volume size is used.
    +
    UINT n_align
    +
    Specifies alignment of the volume data area (file allocation pool, usually erase block boundary of flash 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.
    +
    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_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.
    +
    work
    -
    Pointer to the working buffer used for the format process. When a null pointer is given, the function allocates a memory block for the working buffer and len has no effect (at only FF_USE_LFN == 3).
    +
    Pointer to the working buffer used for the format process. When a null pointer is given with FF_USE_LFN == 3, the function obtains a memory block in this function for the working buffer.
    len
    -
    Size of the working buffer in unit of byte. It needs to be the sector size of the corresponding physical drive at least. Plenty of working buffer reduces number of write transactions to the drive and the format process will finish quickly.
    +
    Size of the working buffer in unit of byte. It needs to be FF_MAX_SS at least. Plenty of working buffer reduces number of write transactions to the drive and the format process will finish quickly.
    @@ -57,11 +66,12 @@ FRESULT f_mkfs (

    Description

    -

    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 cannot be valid on the volume, the function will fail with FR_MKFS_ABORTED. The minimum drive size is 128 sectors with FM_SFD option.

    -

    The allocation unit, also called 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 as the size of allocation unit. Therefore the size of allocation unit is a trade-off between space efficiency and performance. For the large storages in GB order, 32768 bytes or larger (this is automatically selected by default) is recommended for most case unless extremely many small files are created on a volume.

    -

    There are three disk partitioning formats, FDISK, SFD and GPT. The 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 on the MBR (maser boot record, the first sector of the physical drive). The SFD (super-floppy disk) is non-partitioned disk format. The FAT volume starts at the first sector of the physical drive without any disk partitioning. It is usually used for floppy disk, optical disk and most super-floppy media. Some systems support only either one of the two disk formats and the other is not supported. The GPT (GUID Partition Table) is a newly defined format for large storage devices. FatFs does not support the storages with GPT.

    -

    When the logical drive to be formatted is bound to a physical drive and FM_SFD is not specified, a primary partition occupies whole drive space is created in FDISK format, and then the FAT volume is created in the partition. When FM_SFD is specified, the FAT volume occupies from the first sector of the physical drive is created in SFD format.

    -

    When the logical drive to be formatted is bound to a specific partition (1-4) by support of multiple partition (FF_MULTI_PARTITION == 1), the FAT volume is created on the partition and FM_SFD flag is ignored. The physical drive needs to be partitioned with f_fdisk function or any other partitioning tools prior to create the FAT volume with this function.

    +

    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 cannot be valid on the volume, the function will fail with FR_MKFS_ABORTED.

    +

    The allocation unit, also knows 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 storages in GB order, 32768 bytes or larger (this is automatically selected by default) is recommended for most case unless extremely many small files are created on a volume.

    +

    When the logical drive to be formatted is associated with a physical drive (FF_MULTI_PARTITION = 0) and FM_SFD flag is not specified, a partition occupies entire disk 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), the FAT volume is created on the partition specified by the volume mapping table and FM_SFD flag is ignored. The physical drive needs to be partitioned with f_fdisk function or any partitioning tool prior to create the FAT volume with this function.

    +

    There are three standard disk partitioning formats, MBR, GPT and SFD. The MBR format (aka 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. SFD (super-floppy disk) is non-partitioned disk format. The FAT volume 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 on the internal storage in non-standard format. The partitions are mapped as physical drives identified in disk_* functions. For such systems, SFD format is suitable to create the FAT volume on the partition.

    @@ -82,11 +92,11 @@ int main (void) BYTE work[FF_MAX_SS]; /* Work area (larger is better for processing time) */ - /* Create FAT volume */ - res = f_mkfs("", FM_ANY, 0, work, sizeof work); + /* Format the default drive with default parameters */ + res = f_mkfs("", 0, work, sizeof work); if (res) ... - /* Register work area */ + /* Gives a work area to the default drive */ f_mount(&fs, "", 0); /* Create a file as new */ @@ -109,7 +119,7 @@ int main (void)

    Return

    diff --git a/documents/doc/mount.html b/documents/doc/mount.html index 3f70ef7..ed38442 100644 --- a/documents/doc/mount.html +++ b/documents/doc/mount.html @@ -13,7 +13,7 @@

    f_mount

    -

    The f_mount fucntion registers/unregisters filesystem object to the FatFs module.

    +

    The f_mount fucntion gives work area to the FatFs module.

     FRESULT f_mount (
       FATFS*       fs,    /* [IN] Filesystem object */
    @@ -50,7 +50,7 @@ FRESULT f_mount (
     
     

    Description

    -

    FatFs requires work area (filesystem object) for each logical drives (FAT volumes). Prior to perform file/directory operations, a filesystem object needs to be registered with f_mount function to the logical drive. The file/directory API functions get ready to work after this procedure. If there is any open object of file or directory on the logical drive, the object will be invalidated by this function.

    +

    FatFs requires work area (filesystem object) for each logical drives (FAT volumes). Prior to perform any file/directory operations, a filesystem object needs to be registered with f_mount function for the logical drive. The file/directory API functions get ready to work after this procedure. Some volume management functions, f_mkfs, f_fdisk and f_setcp, do not want a filesystem object.

    The f_mount function registers/unregisters a filesystem object to the FatFs module as follows:

    1. Determines the logical drive which specified by path.
    2. @@ -58,6 +58,7 @@ FRESULT f_mount (
    3. Clears and registers the new work area to the volume if fs is not NULL.
    4. Performs volume mount process to the volume if forced mounting is specified.
    +

    If there is any open object of file or directory on the logical drive, the object will be invalidated by this function.

    If forced mounting is not specified (opt = 0), this function always succeeds regardless of the physical drive status. It only clears (de-initializes) the given work area and registers its address to the internal table and no activity of the physical drive in this function. The volume mount process will be attempted on subsequent file/directroy function if the filesystem object is not initialized. (delayed mounting) The volume mount processes, initialize the corresponding physical drive, find the FAT volume in it and then initialize the work area, is performed in the subsequent file/directory functions when either of following conditions is true.

    • Filesystem object has not been initialized. It is de-initialized by f_mount function.
    • diff --git a/documents/doc/open.html b/documents/doc/open.html index c039826..d90e8c4 100644 --- a/documents/doc/open.html +++ b/documents/doc/open.html @@ -42,7 +42,7 @@ FRESULT f_open (
    FA_OPEN_ALWAYSOpens the file if it is existing. If not, a new file will be created.
    FA_OPEN_APPENDSame as FA_OPEN_ALWAYS except the read/write pointer is set end of the file.
    -Mode flags of POSIX fopen() corresponds to FatFs mode flags as follows:
    +Mode flags in POSIX fopen() function corresponds to FatFs mode flags as follows:
    @@ -111,7 +111,7 @@ int main (void) FRESULT fr; /* FatFs return code */ - /* Register work area to the default drive */ + /* Gives a work area to the default drive */ f_mount(&FatFs, "", 0); /* Open a text file */ @@ -141,7 +141,7 @@ int main (void) UINT br, bw; /* File read/write count */ - /* Register work area for each logical drive */ + /* Gives work area to each logical drive */ f_mount(&fs0, "0:", 0); f_mount(&fs1, "1:", 0); @@ -155,10 +155,10 @@ int main (void) /* Copy source to destination */ for (;;) { - fr = f_read(&fsrc, buffer, sizeof buffer, &br); /* Read a chunk of source file */ - if (fr || br == 0) break; /* error or eof */ - fr = f_write(&fdst, buffer, br, &bw); /* Write it to the destination file */ - if (fr || bw < br) break; /* error or disk full */ + f_read(&fsrc, buffer, sizeof buffer, &br); /* Read a chunk of data from the source file */ + if (br == 0) break; /* error or eof */ + f_write(&fdst, buffer, br, &bw); /* Write it to the destination file */ + if (bw < br) break; /* error or disk full */ } /* Close open files */ diff --git a/documents/doc/printf.html b/documents/doc/printf.html index 82be70c..457b633 100644 --- a/documents/doc/printf.html +++ b/documents/doc/printf.html @@ -47,12 +47,13 @@ int f_printf (

    Description

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

    -    %[flag][width][type]
    +    %[flag][width][prefix][type]
     
    -
    flag
    Padding options. A - specifies left justified. A 0 specifies zero padded.
    -
    width
    Minimum width of the field, 1-99 or *. If the width of generated string is less than the specified value, rest field is padded with white spaces or zeros. An * specifies the value comes from an argument in int type.
    -
    type
    c s d u o x b and prefix l specify type of the argument, character, string, signed integer in decimal, unsigned integer in decimal, unsigned integer in octal, unsigned integer in hexdecimal and unsigned integer in binary respectively. If sizeof (long) is greater than sizeof (int) (this is typical of 8/16-bit systems), a prefix l needs to be explicitly specified for long integer argument. These characters except for x are case insensitive.
    +
    flag
    Padding options. A '0' specifies zero padded. A '-' specifies left justified.
    +
    width
    Minimum width of the field, '1'-'99' or '*'. If the width of generated string is less than the specified value, rest field is padded with white spaces or zeros. An * specifies the value comes from an argument in int type. The default value is zero.
    +
    prefix
    Size prefix 'l' for long integer argument. If sizeof (long) == sizeof (int) (this is typical of 32-bit systems), the size prefix can be omitted.
    +
    type
    'c','s','d','u','o','x','b', specify type of the argument and output format, character, string, signed integer in decimal, unsigned integer in decimal, unsigned integer in octal, unsigned integer in hexdecimal and unsigned integer in binary respectively. These characters except 'x' are case insensitive. An 'X' generates a hexdecimal in up-case.

    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.

    diff --git a/documents/doc/rc.html b/documents/doc/rc.html index 114317b..6fce2a6 100644 --- a/documents/doc/rc.html +++ b/documents/doc/rc.html @@ -11,7 +11,7 @@

    Return Code of API Functions

    -

    Most of API functions return common result code as enum type FRESULT. When an API function succeeded, it returns zero (FR_OK), otherwise it returns non-zero value indicates type of error.

    +

    Most of API functions return common result code in enum type FRESULT. When an API function succeeded, it returns zero (FR_OK), otherwise it returns non-zero value indicates type of error.

    @@ -46,13 +46,13 @@ Note that if once this error occured at any operation to an open file, the file
    Could not find the file in the directory.
    FR_NO_PATH
    -
    Could not find the path, some directory in the path name could not be found.
    +
    Could not find the path. A directory in the path name could not be found.
    FR_INVALID_NAME
    The given string is invalid as the path name. One of the following possibilities is suspected.
      -
    • There is any character not allowed for the file name.
    • -
    • The string is out of 8.3 format. (at non-LFN cfg.)
    • +
    • There is a character not allowed for the file name.
    • +
    • The file name is out of 8.3 format. (at non-LFN cfg.)
    • FF_MAX_LFN is insufficient for the file name. (at LFN cfg.)
    • There is any character encoding error in the string.
    @@ -72,7 +72,7 @@ Note that if once this error occured at any operation to an open file, the file
    FR_EXIST
    -
    Name collision, an object with the same name is already existing.
    +
    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: @@ -87,7 +87,7 @@ Note that if once this error occured at any operation to an open file, the file
    A write mode operation against the write-protected media.
    FR_INVALID_DRIVE
    -
    Invalid drive number is specified in the path name. A null pointer is given as the path name. (Related option: FF_VOLUMES)
    +
    Invalid drive number is specified in the path name or a null pointer is given as the path name. (Related option: FF_VOLUMES)
    FR_NOT_ENABLED
    Work area for the logical drive has not been registered by f_mount function.
    @@ -99,7 +99,7 @@ Note that if once this error occured at any operation to an open file, the file
    The f_mkfs function aborted before start in format due to a reason as follows:
    • It is impossible to format with the given parameters.
    • -
    • The size of volume is too small. 128 sectors minimum with FM_SFD.
    • +
    • The size of volume is too small. 128 sectors minimum with FM_SFD option.
    • The partition bound to the logical drive coulud not be found. (Related option: FF_MULTI_PARTITION)
    diff --git a/documents/doc/read.html b/documents/doc/read.html index 12c57cd..d9ceda3 100644 --- a/documents/doc/read.html +++ b/documents/doc/read.html @@ -30,11 +30,11 @@ FRESULT f_read (
    fp
    Pointer to the open file object.
    buff
    -
    Pointer to the buffer to store read data.
    +
    Pointer to the buffer to store the read data.
    btr
    Number of bytes to read in range of UINT type.
    br
    -
    Pointer to the UINT variable to return number of bytes read. This value is always valid after the function call regardless of the function return code.
    +
    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.
    @@ -54,7 +54,7 @@ FRESULT f_read (

    Description

    -

    The function starts to read data from the file at the position pointed by the 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 reached 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. 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 reached end of the file during read operation.

    @@ -64,6 +64,12 @@ FRESULT f_read ( +
    +

    Example

    +

    Refer to the example in f_open.

    +
    + +

    See Also

    f_open, fgets, f_write, f_close, FIL

    diff --git a/documents/doc/readdir.html b/documents/doc/readdir.html index 2d0ad6e..80ce50a 100644 --- a/documents/doc/readdir.html +++ b/documents/doc/readdir.html @@ -48,7 +48,7 @@ FRESULT f_readdir (

    Description

    -

    The f_readdir function reads a directory item, informations about the object. Every item in the directory can be read in sequence by f_readdir function calls. Dot entries ("." and "..") in the sub-directory are filtered out and they will never appear in the read items. When all directory items 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_readdir function reads a directory item, informations about the object. Items in the directory can be read in sequence by f_readdir function calls. Dot entries ("." and "..") in the sub-directory are filtered out and they will never appear in the read items. When all directory items 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.

    When support of long file name (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 some reason listed below, short file name is stored to the fname[] and altname[] has a null string.

    • The item has no LFN. (Not the case at exFAT volume)
    • diff --git a/documents/doc/sdir.html b/documents/doc/sdir.html index 699a8d6..19c66ec 100644 --- a/documents/doc/sdir.html +++ b/documents/doc/sdir.html @@ -19,7 +19,7 @@ FFOBJID obj; /* Object identifier */ DWORD dptr; /* Current read/write offset */ DWORD clust; /* Current cluster */ - DWORD sect; /* Current sector */ + LBA_t sect; /* Current sector */ BYTE* dir; /* Pointer to the current SFN entry in the win[] */ BYTE* fn; /* Pointer to the SFN buffer (in/out) {file[8],ext[3],status[1]} */ #if FF_USE_LFN diff --git a/documents/doc/sfatfs.html b/documents/doc/sfatfs.html index a9312f5..79079e1 100644 --- a/documents/doc/sfatfs.html +++ b/documents/doc/sfatfs.html @@ -31,7 +31,7 @@ BYTE* dirbuf; /* Directory entry block scratchpad buffer */ #endif #if FF_FS_REENTRANT - FF_SYNC_t sobj; /* Identifier of sync object */ + FF_SYNC_t sobj; /* Identifier of sync object */ #endif #if !FF_FS_READONLY DWORD last_clust; /* FSINFO: Last allocated cluster (0xFFFFFFFF if invalid) */ @@ -47,11 +47,11 @@ #endif DWORD n_fatent; /* Number of FAT entries (Number of clusters + 2) */ DWORD fsize; /* Sectors per FAT */ - DWORD volbase; /* Volume base LBA */ - DWORD fatbase; /* FAT base LBA */ - DWORD dirbase; /* Root directory base (LBA|Cluster) */ - DWORD database; /* Data base LBA */ - DWORD winsect; /* Sector LBA appearing in the win[] */ + LBA_t volbase; /* Volume base LBA */ + LBA_t fatbase; /* FAT base LBA */ + LBA_t dirbase; /* Root directory base (LBA|Cluster) */ + LBA_t database; /* Data base LBA */ + LBA_t winsect; /* Sector LBA appearing in the win[] */ BYTE win[FF_MAX_SS]; /* Disk access window for directory, FAT (and file data at tiny cfg) */ } FATFS; diff --git a/documents/doc/sfile.html b/documents/doc/sfile.html index 599b8d2..4d44314 100644 --- a/documents/doc/sfile.html +++ b/documents/doc/sfile.html @@ -22,9 +22,9 @@ BYTE err; /* Abort flag (error code) */ FSIZE_t fptr; /* File read/write pointer (Byte offset origin from top of the file) */ DWORD clust; /* Current cluster of fptr (One cluster behind if fptr is on the cluster boundary. Invalid if fptr == 0.) */ - DWORD sect; /* Current data sector (Can be invalid if fptr is on the cluster boundary.)*/ + LBA_t sect; /* Current data sector (Can be invalid if fptr is on the cluster boundary.)*/ #if !FF_FS_READONLY - DWORD dir_sect; /* Sector number containing the directory entry */ + LBA_t dir_sect; /* Sector number containing the directory entry */ BYTE* dir_ptr; /* Ponter to the directory entry in the window */ #endif #if FF_USE_FASTSEEK diff --git a/documents/doc/sfileinfo.html b/documents/doc/sfileinfo.html index 7717580..c0fd55f 100644 --- a/documents/doc/sfileinfo.html +++ b/documents/doc/sfileinfo.html @@ -13,7 +13,7 @@

      FILINFO

      -

      The FILINFO structure holds information about the object returned by f_readdir, f_findfirst, f_findnext and f_stat function. Be careful in the size of structure when LFN is enabled.

      +

      The FILINFO structure holds information about the object retrieved by f_readdir, f_findfirst, f_findnext and f_stat function. Be careful in the size of structure when LFN is enabled.

       typedef struct {
           FSIZE_t fsize;               /* File size */
      @@ -33,9 +33,9 @@
       

      Members

      fsize
      -
      Indicates size of the file in unit of byte. FSIZE_t is an alias of integer type either DWORD(32-bit) or QWORD(64-bit) depends on the configuration option FF_FS_EXFAT. Do not care when the item is a directory.
      +
      Size of the file in unit of byte. FSIZE_t is an alias of integer type either DWORD(32-bit) or QWORD(64-bit) depends on the configuration option FF_FS_EXFAT. Do not care if the item is a sub-directory.
      fdate
      -
      Indicates the date when the file was modified or the directory was created.
      +
      The date when the file was modified or the directory was created.
      bit15:9
      Year origin from 1980 (0..127)
      @@ -46,7 +46,7 @@
      ftime
      -
      Indicates the time when the file was modified or the directory was created.
      +
      The time when the file was modified or the directory was created.
      bit15:11
      Hour (0..23)
      @@ -57,7 +57,7 @@
      fattrib
      -
      Indicates the attribute flags in combination of:
      +
      The attribute flags in combination of:
    POSIXFatFs
    "r"FA_READ
    @@ -68,9 +68,9 @@
    FlagMeaning
    AM_RDORead-only. Write mode open and deleting is rejected.
    fname[]
    -
    The null-terminated object name is stored. A null string is stored when no item to read and it indicates this structure is invalid. The size of fname[] and altname[] each can be configured at LFN configuration.
    +
    Null-terminated object name. A null string is stored when no item to read and it indicates this structure is invalid. The size of fname[] and altname[] each can be configured at LFN configuration.
    altname[]
    -
    Alternative object name is stored if available. This member is not available at non-LFN configuration.
    +
    Alternative object name is stored if available. This member is not available in non-LFN configuration.

    Return

    diff --git a/documents/doc/stat.html b/documents/doc/stat.html index cc5c03d..4ed35a5 100644 --- a/documents/doc/stat.html +++ b/documents/doc/stat.html @@ -54,7 +54,7 @@ FRESULT f_stat (

    Description

    -

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

    +

    The f_stat function checks the existence of a file or sub-directory. If not exist, the function returns with FR_NO_FILE. If 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.

    diff --git a/documents/doc/sync.html b/documents/doc/sync.html index 39cac91..e3f09d7 100644 --- a/documents/doc/sync.html +++ b/documents/doc/sync.html @@ -44,12 +44,12 @@ FRESULT f_sync (

    Description

    -

    The f_sync function performs the same process as f_close function but the file is left opened and can continue read/write/seek operations to the file. This is suitable for the applications that open files for a long time in write mode, such as data logger. Performing f_sync function in certain interval can minimize the risk of data loss due to a sudden blackout or an unintentional media removal. For more information, refer to application note.

    +

    The f_sync function performs the same process as f_close function but the file is left opened and can continue read/write/seek operations to the file. This is suitable for the applications that open files for a long time in write mode, such as data logger. Performing f_sync function in certain interval can minimize the risk of data loss due to a sudden blackout, wrong media removal or unrecoverable disk error. For more information, refer to application note.

     Case 1. Normal write sequence
     
    -                                Time -->                                     ↓Power off after close
    -OwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwC
    +                                Time -->                                     ↓Normal shutdown
    +OwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwC <Power off>
     
     
     Case 2. Without using f_sync()
    diff --git a/documents/doc/write.html b/documents/doc/write.html
    index f6772d7..1aa8e0f 100644
    --- a/documents/doc/write.html
    +++ b/documents/doc/write.html
    @@ -34,7 +34,7 @@ FRESULT f_write (
     
    btw
    Specifies number of bytes to write in range of UINT type.
    bw
    -
    Pointer to the UINT variable to return the number of bytes written. This value is always valid after the function call regardless of the function return code.
    +
    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.
    @@ -54,7 +54,7 @@ FRESULT f_write (

    Description

    -

    The function starts to write data to the file at the position pointed by the 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. 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.

    @@ -64,6 +64,12 @@ FRESULT f_write (
    +
    +

    Example

    +

    Refer to the example in f_open.

    +
    + +

    See Also

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

    diff --git a/documents/res/app4.c b/documents/res/app4.c index e8547fc..656a061 100644 --- a/documents/res/app4.c +++ b/documents/res/app4.c @@ -10,9 +10,9 @@ #include "diskio.h" /* Declarations of disk functions */ -static -DWORD pn ( /* Pseudo random number generator */ - DWORD pns /* 0:Initialize, !0:Read */ + +static DWORD pn ( /* Pseudo random number generator */ + DWORD pns /* 0:Initialize, !0:Read */ ) { static DWORD lfsr; @@ -50,8 +50,8 @@ int test_diskio ( printf("test_diskio(%u, %u, 0x%08X, 0x%08X)\n", pdrv, ncyc, (UINT)buff, sz_buff); - if (sz_buff < _MAX_SS + 4) { - printf("Insufficient work area to run program.\n"); + if (sz_buff < FF_MAX_SS + 8) { + printf("Insufficient work area to run the program.\n"); return 1; } @@ -115,7 +115,7 @@ int test_diskio ( } /* Single sector write test */ - printf("**** Single sector write test 1 ****\n"); + printf("**** Single sector write test ****\n"); lba = 0; for (n = 0, pn(pns); n < sz_sect; n++) pbuff[n] = (BYTE)pn(0); printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba); @@ -145,52 +145,56 @@ int test_diskio ( } for (n = 0, pn(pns); n < sz_sect && pbuff[n] == (BYTE)pn(0); n++) ; if (n == sz_sect) { - printf(" Data matched.\n"); + printf(" Read data matched.\n"); } else { - printf("Failed: Read data differs from the data written.\n"); + printf(" Read data differs from the data written.\n"); return 10; } pns++; printf("**** Multiple sector write test ****\n"); - lba = 1; ns = sz_buff / sz_sect; + lba = 5; ns = sz_buff / sz_sect; if (ns > 4) ns = 4; - for (n = 0, pn(pns); n < (UINT)(sz_sect * ns); n++) pbuff[n] = (BYTE)pn(0); - printf(" disk_write(%u, 0x%X, %lu, %u)", pdrv, (UINT)pbuff, lba, ns); - dr = disk_write(pdrv, pbuff, lba, ns); - if (dr == RES_OK) { - printf(" - ok.\n"); + if (ns > 1) { + for (n = 0, pn(pns); n < (UINT)(sz_sect * ns); n++) pbuff[n] = (BYTE)pn(0); + printf(" disk_write(%u, 0x%X, %lu, %u)", pdrv, (UINT)pbuff, lba, ns); + dr = disk_write(pdrv, pbuff, lba, ns); + if (dr == RES_OK) { + printf(" - ok.\n"); + } else { + printf(" - failed.\n"); + return 11; + } + printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv); + dr = disk_ioctl(pdrv, CTRL_SYNC, 0); + if (dr == RES_OK) { + printf(" - ok.\n"); + } else { + printf(" - failed.\n"); + return 12; + } + memset(pbuff, 0, sz_sect * ns); + printf(" disk_read(%u, 0x%X, %lu, %u)", pdrv, (UINT)pbuff, lba, ns); + dr = disk_read(pdrv, pbuff, lba, ns); + if (dr == RES_OK) { + printf(" - ok.\n"); + } else { + printf(" - failed.\n"); + return 13; + } + for (n = 0, pn(pns); n < (UINT)(sz_sect * ns) && pbuff[n] == (BYTE)pn(0); n++) ; + if (n == (UINT)(sz_sect * ns)) { + printf(" Read data matched.\n"); + } else { + printf(" Read data differs from the data written.\n"); + return 14; + } } else { - printf(" - failed.\n"); - return 11; - } - printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv); - dr = disk_ioctl(pdrv, CTRL_SYNC, 0); - if (dr == RES_OK) { - printf(" - ok.\n"); - } else { - printf(" - failed.\n"); - return 12; - } - memset(pbuff, 0, sz_sect * ns); - printf(" disk_read(%u, 0x%X, %lu, %u)", pdrv, (UINT)pbuff, lba, ns); - dr = disk_read(pdrv, pbuff, lba, ns); - if (dr == RES_OK) { - printf(" - ok.\n"); - } else { - printf(" - failed.\n"); - return 13; - } - for (n = 0, pn(pns); n < (UINT)(sz_sect * ns) && pbuff[n] == (BYTE)pn(0); n++) ; - if (n == (UINT)(sz_sect * ns)) { - printf(" Data matched.\n"); - } else { - printf("Failed: Read data differs from the data written.\n"); - return 14; + printf(" Test skipped.\n"); } pns++; - printf("**** Single sector write test (misaligned address) ****\n"); + printf("**** Single sector write test (unaligned buffer address) ****\n"); lba = 5; for (n = 0, pn(pns); n < sz_sect; n++) pbuff[n+3] = (BYTE)pn(0); printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+3), lba); @@ -220,9 +224,9 @@ int test_diskio ( } for (n = 0, pn(pns); n < sz_sect && pbuff[n+5] == (BYTE)pn(0); n++) ; if (n == sz_sect) { - printf(" Data matched.\n"); + printf(" Read data matched.\n"); } else { - printf("Failed: Read data differs from the data written.\n"); + printf(" Read data differs from the data written.\n"); return 18; } pns++; @@ -274,9 +278,9 @@ int test_diskio ( } for (n = 0, pn(pns); pbuff[n] == (BYTE)pn(0) && n < (UINT)(sz_sect * 2); n++) ; if (n == (UINT)(sz_sect * 2)) { - printf(" Data matched.\n"); + printf(" Read data matched.\n"); } else { - printf("Failed: Read data differs from the data written.\n"); + printf(" Read data differs from the data written.\n"); return 24; } } else { diff --git a/documents/res/app5.c b/documents/res/app5.c index 6a0819d..1f0205a 100644 --- a/documents/res/app5.c +++ b/documents/res/app5.c @@ -21,7 +21,7 @@ FRESULT test_contiguous_file ( #else clsz = (DWORD)fp->obj.fs->csize * fp->obj.fs->ssize; #endif - fsz = fp->obj.objsize; + fsz = f_size(fp); if (fsz > 0) { clst = fp->obj.sclust - 1; /* A cluster leading the first cluster for first test */ while (fsz) { diff --git a/documents/res/app6.c b/documents/res/app6.c new file mode 100644 index 0000000..69c065e --- /dev/null +++ b/documents/res/app6.c @@ -0,0 +1,61 @@ +/*---------------------------------------------------------------------*/ +/* Raw Read/Write Throughput Checker */ +/*---------------------------------------------------------------------*/ + +#include +#include +#include "diskio.h" +#include "ff.h" + + +int test_raw_speed ( + BYTE pdrv, /* Physical drive number */ + DWORD lba, /* Start LBA for read/write test */ + DWORD len, /* Number of bytes to read/write (must be multiple of sz_buff) */ + void* buff, /* Read/write buffer */ + UINT sz_buff /* Size of read/write buffer (must be multiple of FF_MAX_SS) */ +) +{ + WORD ss; + DWORD ofs, tmr; + + +#if FF_MIN_SS != FF_MAX_SS + if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &ss) != RES_OK) { + printf("\ndisk_ioctl() failed.\n"); + return 0; + } +#else + ss = FF_MAX_SS; +#endif + + printf("Starting raw write test at sector %lu in %u bytes of data chunks...", lba, sz_buff); + tmr = systimer(); + for (ofs = 0; ofs < len / ss; ofs += sz_buff / ss) { + if (disk_write(pdrv, buff, lba + ofs, sz_buff / ss) != RES_OK) { + printf("\ndisk_write() failed.\n"); + return 0; + } + } + if (disk_ioctl(pdrv, CTRL_SYNC, 0) != RES_OK) { + printf("\ndisk_ioctl() failed.\n"); + return 0; + } + tmr = systimer() - tmr; + printf("\n%lu bytes written and it took %lu timer ticks.\n", len, tmr); + + printf("Starting raw read test at sector %lu in %u bytes of data chunks...", lba, sz_buff); + tmr = systimer(); + for (ofs = 0; ofs < len / ss; ofs += sz_buff / ss) { + if (disk_read(pdrv, buff, lba + ofs, sz_buff / ss) != RES_OK) { + printf("\ndisk_read() failed.\n"); + return 0; + } + } + tmr = systimer() - tmr; + printf("\n%lu bytes read and it took %lu timer ticks.\n", len, tmr); + + printf("Test completed.\n"); + return 1; +} + diff --git a/documents/res/funcs.png b/documents/res/funcs.png index 022cd74..f381ec5 100644 Binary files a/documents/res/funcs.png and b/documents/res/funcs.png differ diff --git a/documents/res/layers2.png b/documents/res/layers2.png index 406c453..c7dbef4 100644 Binary files a/documents/res/layers2.png and b/documents/res/layers2.png differ diff --git a/documents/res/mkfs.xls b/documents/res/mkfs.xls deleted file mode 100644 index ee6b2bf..0000000 Binary files a/documents/res/mkfs.xls and /dev/null differ diff --git a/documents/res/mkfs.xlsx b/documents/res/mkfs.xlsx new file mode 100644 index 0000000..6024888 Binary files /dev/null and b/documents/res/mkfs.xlsx differ diff --git a/documents/res/modules.png b/documents/res/modules.png index 885a15a..d7e69a3 100644 Binary files a/documents/res/modules.png and b/documents/res/modules.png differ diff --git a/documents/updates.txt b/documents/updates.txt index 42405b9..9825533 100644 --- a/documents/updates.txt +++ b/documents/updates.txt @@ -1,3 +1,11 @@ +R0.14 (October 14, 2019) + Added support for 64-bit LBA and GUID partition table (FF_LBA64 = 1) + Changed some API functions, f_mkfs() and f_fdisk(). + Fixed f_open() function cannot find the file with file name in length of FF_MAX_LFN characters. + Fixed f_readdir() function cannot retrieve long file names in length of FF_MAX_LFN - 1 characters. + Fixed f_readdir() function returns file names with wrong case conversion. (appeared at R0.12) + Fixed f_mkfs() function can fail to create exFAT volume in the second partition. (appeared at R0.12) + R0.13c (October 14, 2018) Supported stdint.h for C99 and later. (integer.h was included in ff.h) Fixed reading a directory gets infinite loop when the last directory entry is not empty. (appeared at R0.12) diff --git a/source/00history.txt b/source/00history.txt index db12a9e..cb8753b 100644 --- a/source/00history.txt +++ b/source/00history.txt @@ -328,3 +328,13 @@ R0.13c (October 14, 2018) Fixed creating a sub-directory in the fragmented sub-directory on the exFAT volume collapses FAT chain of the parent directory. (appeared at R0.12) Fixed f_getcwd() cause output buffer overrun when the buffer has a valid drive number. (appeared at R0.13b) + + +R0.14 (October 14, 2019) + Added support for 64-bit LBA and GUID partition table (FF_LBA64 = 1) + Changed some API functions, f_mkfs() and f_fdisk(). + Fixed f_open() function cannot find the file with file name in length of FF_MAX_LFN characters. + Fixed f_readdir() function cannot retrieve long file names in length of FF_MAX_LFN - 1 characters. + Fixed f_readdir() function returns file names with wrong case conversion. (appeared at R0.12) + Fixed f_mkfs() function can fail to create exFAT volume in the second partition. (appeared at R0.12) + diff --git a/source/00readme.txt b/source/00readme.txt index dcccbdb..234c675 100644 --- a/source/00readme.txt +++ b/source/00readme.txt @@ -1,4 +1,4 @@ -FatFs Module Source Files R0.13c +FatFs Module Source Files R0.14 FILES diff --git a/source/diskio.c b/source/diskio.c index 08ffcc8..27f1331 100644 --- a/source/diskio.c +++ b/source/diskio.c @@ -1,5 +1,5 @@ /*-----------------------------------------------------------------------*/ -/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */ +/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2019 */ /*-----------------------------------------------------------------------*/ /* If a working storage control module is available, it should be */ /* attached to the FatFs via a glue function rather than modifying it. */ @@ -99,7 +99,7 @@ DSTATUS disk_initialize ( DRESULT disk_read ( BYTE pdrv, /* Physical drive nmuber to identify the drive */ BYTE *buff, /* Data buffer to store read data */ - DWORD sector, /* Start sector in LBA */ + LBA_t sector, /* Start sector in LBA */ UINT count /* Number of sectors to read */ ) { @@ -149,7 +149,7 @@ DRESULT disk_read ( DRESULT disk_write ( BYTE pdrv, /* Physical drive nmuber to identify the drive */ const BYTE *buff, /* Data to be written */ - DWORD sector, /* Start sector in LBA */ + LBA_t sector, /* Start sector in LBA */ UINT count /* Number of sectors to write */ ) { diff --git a/source/diskio.h b/source/diskio.h index 31776b8..e4ead78 100644 --- a/source/diskio.h +++ b/source/diskio.h @@ -1,5 +1,5 @@ /*-----------------------------------------------------------------------/ -/ Low level disk interface modlue include file (C)ChaN, 2014 / +/ Low level disk interface modlue include file (C)ChaN, 2019 / /-----------------------------------------------------------------------*/ #ifndef _DISKIO_DEFINED @@ -28,8 +28,8 @@ typedef enum { DSTATUS disk_initialize (BYTE pdrv); DSTATUS disk_status (BYTE pdrv); -DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count); -DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count); +DRESULT disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count); +DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count); DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff); diff --git a/source/ff.c b/source/ff.c index 290f577..b8ac2e3 100644 --- a/source/ff.c +++ b/source/ff.c @@ -1,8 +1,8 @@ /*----------------------------------------------------------------------------/ -/ FatFs - Generic FAT Filesystem Module R0.13c / +/ FatFs - Generic FAT Filesystem Module R0.14 / /-----------------------------------------------------------------------------/ / -/ Copyright (C) 2018, ChaN, all right reserved. +/ Copyright (C) 2019, 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 @@ -29,7 +29,7 @@ ---------------------------------------------------------------------------*/ -#if FF_DEFINED != 86604 /* Revision ID */ +#if FF_DEFINED != 86606 /* Revision ID */ #error Wrong include file (ff.h). #endif @@ -204,6 +204,27 @@ #define PTE_StLba 8 /* MBR PTE: Start in LBA */ #define PTE_SizLba 12 /* MBR PTE: Size in LBA */ +#define GPTH_Sign 0 /* GPT: Header signature (8-byte) */ +#define GPTH_Rev 8 /* GPT: Revision (DWORD) */ +#define GPTH_Size 12 /* GPT: Header size (DWORD) */ +#define GPTH_Bcc 16 /* GPT: Header BCC (DWORD) */ +#define GPTH_CurLba 24 /* GPT: Main header LBA (QWORD) */ +#define GPTH_BakLba 32 /* GPT: Backup header LBA (QWORD) */ +#define GPTH_FstLba 40 /* GPT: First LBA for partitions (QWORD) */ +#define GPTH_LstLba 48 /* GPT: Last LBA for partitions (QWORD) */ +#define GPTH_DskGuid 56 /* GPT: Disk GUID (16-byte) */ +#define GPTH_PtOfs 72 /* GPT: Partation table LBA (QWORD) */ +#define GPTH_PtNum 80 /* GPT: Number of table entries (DWORD) */ +#define GPTH_PteSize 84 /* GPT: Size of table entry (DWORD) */ +#define GPTH_PtBcc 88 /* GPT: Partation table BCC (DWORD) */ +#define SZ_GPTE 128 /* GPT: Size of partition table entry */ +#define GPTE_PtGuid 0 /* GPT PTE: Partition type GUID (16-byte) */ +#define GPTE_UpGuid 16 /* GPT PTE: Partition unique GUID (16-byte) */ +#define GPTE_FstLba 32 /* GPT PTE: First LBA (QWORD) */ +#define GPTE_LstLba 40 /* GPT PTE: Last LBA inclusive (QWORD) */ +#define GPTE_Flags 48 /* GPT PTE: Flags (QWORD) */ +#define GPTE_Name 56 /* GPT PTE: Name */ + /* Post process on fatal error in the file operations */ #define ABORT(fs, res) { fp->err = (BYTE)(res); LEAVE_FF(fs, res); } @@ -220,12 +241,12 @@ #endif -/* Definitions of volume - physical location conversion */ +/* Definitions of logical drive - 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 index */ #else -#define LD2PD(vol) (BYTE)(vol) /* Each logical drive is bound to the same physical drive number */ +#define LD2PD(vol) (BYTE)(vol) /* Each logical drive is associated with the same physical drive number */ #define LD2PT(vol) 0 /* Find first valid partition or in SFD */ #endif @@ -406,6 +427,7 @@ typedef struct { /* DBCS code range |----- 1st byte -----| |----------- 2nd byte -----------| */ +/* <------> <------> <------> <------> <------> */ #define TBL_DC932 {0x81, 0x9F, 0xE0, 0xFC, 0x40, 0x7E, 0x80, 0xFC, 0x00, 0x00} #define TBL_DC936 {0x81, 0xFE, 0x00, 0x00, 0x40, 0x7E, 0x80, 0xFE, 0x00, 0x00} #define TBL_DC949 {0x81, 0xFE, 0x00, 0x00, 0x41, 0x5A, 0x61, 0x7A, 0x81, 0xFE} @@ -452,6 +474,14 @@ static const char* const VolumeStr[FF_VOLUMES] = {FF_VOLUME_STRS}; /* Pre-define #endif #endif +#if FF_LBA64 +#if FF_MIN_GPT > 0x100000000 +#error Wrong FF_MIN_GPT setting +#endif +static const BYTE GUID_MS_Basic[16] = {0xA2,0xA0,0xD0,0xEB,0xE5,0xB9,0x33,0x44,0x87,0xC0,0x68,0xB6,0xB7,0x26,0x99,0xC7}; +#endif + + /*--------------------------------*/ /* LFN/Directory working buffer */ @@ -697,7 +727,7 @@ static int chk_chr (const char* str, int chr) /* NZ:contained, ZR:not contained } -/* Test if the character is DBC 1st byte */ +/* Test if the byte is DBC 1st byte */ static int dbc_1st (BYTE c) { #if FF_CODE_PAGE == 0 /* Variable code page */ @@ -717,7 +747,7 @@ static int dbc_1st (BYTE c) } -/* Test if the character is DBC 2nd byte */ +/* Test if the byte is DBC 2nd byte */ static int dbc_2nd (BYTE c) { #if FF_CODE_PAGE == 0 /* Variable code page */ @@ -741,8 +771,8 @@ static int dbc_2nd (BYTE c) #if FF_USE_LFN -/* Get a character from TCHAR string in defined API encodeing */ -static DWORD tchar2uni ( /* Returns character in UTF-16 encoding (>=0x10000 on double encoding unit, 0xFFFFFFFF on decode error) */ +/* Get a Unicode code point from the TCHAR string in defined API encodeing */ +static DWORD tchar2uni ( /* Returns a character in UTF-16 encoding (>=0x10000 on surrogate pair, 0xFFFFFFFF on decode error) */ const TCHAR** str /* Pointer to pointer to TCHAR string in configured encoding */ ) { @@ -763,7 +793,7 @@ static DWORD tchar2uni ( /* Returns character in UTF-16 encoding (>=0x10000 on d BYTE b; int nf; - uc = (BYTE)*p++; /* Get a unit */ + uc = (BYTE)*p++; /* Get an encoding unit */ if (uc & 0x80) { /* Multiple byte code? */ if ((uc & 0xE0) == 0xC0) { /* 2-byte sequence? */ uc &= 0x1F; nf = 1; @@ -789,7 +819,7 @@ static DWORD tchar2uni ( /* Returns character in UTF-16 encoding (>=0x10000 on d #elif FF_LFN_UNICODE == 3 /* UTF-32 input */ uc = (TCHAR)*p++; /* Get a unit */ - if (uc >= 0x110000) return 0xFFFFFFFF; /* Wrong code? */ + if (uc >= 0x110000 || IsSurrogate(uc)) return 0xFFFFFFFF; /* Wrong code? */ if (uc >= 0x010000) uc = 0xD800DC00 | ((uc - 0x10000) << 6 & 0x3FF0000) | (uc & 0x3FF); /* Make a surrogate pair if needed */ #else /* ANSI/OEM input */ @@ -816,7 +846,7 @@ static DWORD tchar2uni ( /* Returns character in UTF-16 encoding (>=0x10000 on d /* Output a TCHAR string in defined API encoding */ static BYTE put_utf ( /* Returns number of encoding units written (0:buffer overflow or wrong encoding) */ - DWORD chr, /* UTF-16 encoded character (Double encoding unit char if >=0x10000) */ + DWORD chr, /* UTF-16 encoded character (Surrogate pair if >=0x10000) */ TCHAR* buf, /* Output buffer */ UINT szb /* Size of the buffer */ ) @@ -976,9 +1006,9 @@ static UINT inc_lock ( /* Increment object open counter and returns its index (0 for (i = 0; i < FF_FS_LOCK; i++) { /* Find the object */ - if (Files[i].fs == dp->obj.fs && - Files[i].clu == dp->obj.sclust && - Files[i].ofs == dp->dptr) break; + if (Files[i].fs == dp->obj.fs + && Files[i].clu == dp->obj.sclust + && Files[i].ofs == dp->dptr) break; } if (i == FF_FS_LOCK) { /* Not opened. Register it as new. */ @@ -1046,8 +1076,8 @@ static FRESULT sync_window ( /* Returns FR_OK or FR_DISK_ERR */ FRESULT res = FR_OK; - if (fs->wflag) { /* Is the disk access window dirty */ - if (disk_write(fs->pdrv, fs->win, fs->winsect, 1) == RES_OK) { /* Write back the window */ + if (fs->wflag) { /* Is the disk access window dirty? */ + if (disk_write(fs->pdrv, fs->win, fs->winsect, 1) == RES_OK) { /* Write it back into the volume */ fs->wflag = 0; /* Clear window dirty flag */ if (fs->winsect - fs->fatbase < fs->fsize) { /* Is it in the 1st FAT? */ if (fs->n_fats == 2) disk_write(fs->pdrv, fs->win, fs->winsect + fs->fsize, 1); /* Reflect it to 2nd FAT if needed */ @@ -1062,23 +1092,23 @@ static FRESULT sync_window ( /* Returns FR_OK or FR_DISK_ERR */ static FRESULT move_window ( /* Returns FR_OK or FR_DISK_ERR */ - FATFS* fs, /* Filesystem object */ - DWORD sector /* Sector number to make appearance in the fs->win[] */ + FATFS* fs, /* Filesystem object */ + LBA_t sect /* Sector LBA to make appearance in the fs->win[] */ ) { FRESULT res = FR_OK; - if (sector != fs->winsect) { /* Window offset changed? */ + if (sect != fs->winsect) { /* Window offset changed? */ #if !FF_FS_READONLY - res = sync_window(fs); /* Write-back changes */ + res = sync_window(fs); /* Flush the window */ #endif if (res == FR_OK) { /* Fill sector window with new data */ - if (disk_read(fs->pdrv, fs->win, sector, 1) != RES_OK) { - sector = 0xFFFFFFFF; /* Invalidate window if read data is not valid */ + if (disk_read(fs->pdrv, fs->win, sect, 1) != RES_OK) { + sect = (LBA_t)0 - 1; /* Invalidate window if read data is not valid */ res = FR_DISK_ERR; } - fs->winsect = sector; + fs->winsect = sect; } } return res; @@ -1129,14 +1159,14 @@ static FRESULT sync_fs ( /* Returns FR_OK or FR_DISK_ERR */ /* Get physical sector number from cluster number */ /*-----------------------------------------------------------------------*/ -static DWORD clst2sect ( /* !=0:Sector number, 0:Failed (invalid cluster#) */ +static LBA_t clst2sect ( /* !=0:Sector number, 0:Failed (invalid cluster#) */ FATFS* fs, /* Filesystem object */ DWORD clst /* Cluster# to be converted */ ) { clst -= 2; /* Cluster number is origin from 2 */ if (clst >= fs->n_fatent - 2) return 0; /* Is it invalid cluster number? */ - return fs->database + fs->csize * clst; /* Start sector number of the cluster */ + return fs->database + (LBA_t)fs->csize * clst; /* Start sector number of the cluster */ } @@ -1185,7 +1215,7 @@ static DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, 2..0x7FFFFFF case FS_EXFAT : if ((obj->objsize != 0 && obj->sclust != 0) || obj->stat == 0) { /* Object except root dir must have valid data length */ DWORD cofs = clst - obj->sclust; /* Offset from start cluster */ - DWORD clen = (DWORD)((obj->objsize - 1) / SS(fs)) / fs->csize; /* Number of clusters - 1 */ + DWORD clen = (DWORD)((LBA_t)((obj->objsize - 1) / SS(fs)) / fs->csize); /* Number of clusters - 1 */ if (obj->stat == 2 && cofs <= clen) { /* Is it a contiguous chain? */ val = (cofs == clen) ? 0x7FFFFFFF : clst + 1; /* No data on the FAT, generate the value */ @@ -1241,12 +1271,12 @@ static FRESULT put_fat ( /* FR_OK(0):succeeded, !=0:error */ res = move_window(fs, fs->fatbase + (bc / SS(fs))); if (res != FR_OK) break; p = fs->win + bc++ % SS(fs); - *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val; /* Put 1st byte */ + *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val; /* Update 1st byte */ fs->wflag = 1; res = move_window(fs, fs->fatbase + (bc / SS(fs))); if (res != FR_OK) break; p = fs->win + bc % SS(fs); - *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F)); /* Put 2nd byte */ + *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F)); /* Update 2nd byte */ fs->wflag = 1; break; @@ -1337,7 +1367,7 @@ static FRESULT change_bitmap ( { BYTE bm; UINT i; - DWORD sect; + LBA_t sect; clst -= 2; /* The first bit corresponds to cluster #2 */ @@ -1426,7 +1456,7 @@ static FRESULT remove_chain ( /* FR_OK(0):succeeded, !=0:error */ DWORD scl = clst, ecl = clst; #endif #if FF_USE_TRIM - DWORD rt[2]; + LBA_t rt[2]; #endif if (clst < 2 || clst >= fs->n_fatent) return FR_INT_ERR; /* Check if in valid range */ @@ -1462,9 +1492,9 @@ static FRESULT remove_chain ( /* FR_OK(0):succeeded, !=0:error */ } #endif #if FF_USE_TRIM - rt[0] = clst2sect(fs, scl); /* Start of data area freed */ - rt[1] = clst2sect(fs, ecl) + fs->csize - 1; /* End of data area freed */ - disk_ioctl(fs->pdrv, CTRL_TRIM, rt); /* Inform device the data in the block is no longer needed */ + rt[0] = clst2sect(fs, scl); /* Start of data area to be freed */ + rt[1] = clst2sect(fs, ecl) + fs->csize - 1; /* End of data area to be freed */ + disk_ioctl(fs->pdrv, CTRL_TRIM, rt); /* Inform storage device that the data in the block may be erased */ #endif scl = ecl = nxt; } @@ -1646,7 +1676,7 @@ static FRESULT dir_clear ( /* Returns FR_OK or FR_DISK_ERR */ DWORD clst /* Directory table to clear */ ) { - DWORD sect; + LBA_t sect; UINT n, szb; BYTE *ibuf; @@ -1695,7 +1725,7 @@ static FRESULT dir_sdi ( /* FR_OK(0):succeeded, !=0:error */ dp->dptr = ofs; /* Set current offset */ clst = dp->obj.sclust; /* Table start cluster (0:root) */ if (clst == 0 && fs->fs_type >= FS_FAT32) { /* Replace cluster# 0 with root cluster# */ - clst = fs->dirbase; + clst = (DWORD)fs->dirbase; if (FF_FS_EXFAT) dp->obj.stat = 0; /* exFAT: Root dir has an FAT chain */ } @@ -1884,7 +1914,7 @@ static int cmp_lfn ( /* 1:matched, 0:not matched */ 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 || ff_wtoupper(uc) != ff_wtoupper(lfnbuf[i++])) { /* Compare it */ + if (i >= FF_MAX_LFN + 1 || ff_wtoupper(uc) != ff_wtoupper(lfnbuf[i++])) { /* Compare it */ return 0; /* Not matched */ } wc = uc; @@ -1920,15 +1950,15 @@ static int pick_lfn ( /* 1:succeeded, 0:buffer overflow or invalid LFN entry */ 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) return 0; /* Buffer overflow? */ + if (i >= FF_MAX_LFN + 1) return 0; /* Buffer overflow? */ lfnbuf[i++] = wc = uc; /* Store it */ } else { if (uc != 0xFFFF) return 0; /* Check filler */ } } - if (dir[LDIR_Ord] & LLEF) { /* Put terminator if it is the last LFN part */ - if (i >= FF_MAX_LFN) return 0; /* Buffer overflow? */ + 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; } @@ -1963,7 +1993,7 @@ static void put_lfn ( 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 left locations */ + 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 */ @@ -1989,22 +2019,22 @@ static void gen_numname ( BYTE ns[8], c; UINT i, j; WCHAR wc; - DWORD sr; + DWORD sreg; mem_cpy(dst, src, 11); if (seq > 5) { /* In case of many collisions, generate a hash number instead of sequential number */ - sr = seq; + sreg = seq; while (*lfn) { /* Create a CRC as hash value */ wc = *lfn++; for (i = 0; i < 16; i++) { - sr = (sr << 1) + (wc & 1); + sreg = (sreg << 1) + (wc & 1); wc >>= 1; - if (sr & 0x10000) sr ^= 0x11021; + if (sreg & 0x10000) sreg ^= 0x11021; } } - seq = (UINT)sr; + seq = (UINT)sreg; } /* itoa (hexdecimal) */ @@ -2523,9 +2553,9 @@ static FRESULT dir_register ( /* FR_OK:succeeded, FR_DENIED:no free entry or too res = load_obj_xdir(&dj, &dp->obj); /* Load the object status */ if (res != FR_OK) return res; dp->obj.objsize += (DWORD)fs->csize * SS(fs); /* Increase the directory size by cluster size */ - st_qword(fs->dirbuf + XDIR_FileSize, dp->obj.objsize); /* Update the allocation status */ + st_qword(fs->dirbuf + XDIR_FileSize, dp->obj.objsize); st_qword(fs->dirbuf + XDIR_ValidFileSize, dp->obj.objsize); - fs->dirbuf[XDIR_GenFlags] = dp->obj.stat | 1; + fs->dirbuf[XDIR_GenFlags] = dp->obj.stat | 1; /* Update the allocation status */ res = store_xdir(&dj); /* Store the object status */ if (res != FR_OK) return res; } @@ -2649,6 +2679,7 @@ static void get_fileinfo ( { UINT si, di; #if FF_USE_LFN + BYTE lcf; WCHAR wc, hs; FATFS *fs = dp->obj.fs; #else @@ -2709,9 +2740,10 @@ static void get_fileinfo ( if (di == 0) { /* If LFN and SFN both are invalid, this object is inaccesible */ fno->fname[di++] = '?'; } else { - for (si = di = 0; fno->altname[si]; si++, di++) { /* Copy altname[] to fname[] with case information */ + for (si = di = 0, lcf = NS_BODY; fno->altname[si]; si++, di++) { /* Copy altname[] to fname[] with case information */ wc = (WCHAR)fno->altname[si]; - if (IsUpper(wc) && (dp->dir[DIR_NTres] & ((si >= 9) ? NS_EXT : NS_BODY))) wc += 0x20; + if (wc == '.') lcf = NS_EXT; + if (IsUpper(wc) && (dp->dir[DIR_NTres] & lcf)) wc += 0x20; fno->fname[di] = (TCHAR)wc; } } @@ -2849,9 +2881,13 @@ static FRESULT create_name ( /* FR_OK: successful, FR_INVALID_NAME: could not cr if (di >= FF_MAX_LFN) return FR_INVALID_NAME; /* Reject too long name */ lfn[di++] = wc; /* Store the Unicode character */ } - while (*p == '/' || *p == '\\') p++; /* Skip duplicated separators if exist */ - *path = p; /* Return pointer to the next segment */ - cf = (wc < ' ') ? NS_LAST : 0; /* Set last segment flag if end of the path */ + if (wc < ' ') { /* End of path? */ + cf = NS_LAST; /* Set last segment flag */ + } else { + cf = 0; /* Next segment follows */ + while (*p == '/' || *p == '\\') p++; /* Skip duplicated separators if exist */ + } + *path = p; /* Return pointer to the next segment */ #if FF_FS_RPATH != 0 if ((di == 1 && lfn[di - 1] == '.') || @@ -3178,23 +3214,94 @@ static int get_ldnumber ( /* Returns logical drive number (-1:invalid drive numb +/*-----------------------------------------------------------------------*/ +/* GPT support functions */ +/*-----------------------------------------------------------------------*/ + +#if FF_LBA64 + +/* Calculate CRC32 in byte-by-byte */ + +static DWORD crc32 ( /* Returns next CRC value */ + DWORD crc, /* Current CRC value */ + BYTE d /* A byte to be processed */ +) +{ + BYTE b; + + + for (b = 1; b; b <<= 1) { + crc ^= (d & b) ? 1 : 0; + crc = (crc & 1) ? crc >> 1 ^ 0xEDB88320 : crc >> 1; + } + return crc; +} + + +/* Check validity of GPT header */ + +static int test_gpt_header ( /* 0:Invalid, 1:Valid */ + const BYTE* gpth /* Pointer to the GPT header */ +) +{ + UINT i; + DWORD bcc; + + + if (mem_cmp(gpth + GPTH_Sign, "EFI PART" "\0\0\1\0" "\x5C\0\0", 16)) return 0; /* Check sign, version (1.0) and length (92) */ + for (i = 0, bcc = 0xFFFFFFFF; i < 92; i++) { /* Check header BCC */ + bcc = crc32(bcc, i - GPTH_Bcc < 4 ? 0 : gpth[i]); + } + if (~bcc != ld_dword(gpth + GPTH_Bcc)) return 0; + if (ld_dword(gpth + GPTH_PteSize) != SZ_GPTE) return 0; /* Table entry size (must be SZ_GPTE bytes) */ + if (ld_dword(gpth + GPTH_PtNum) > 128) return 0; /* Table size (must be 128 entries or less) */ + + return 1; +} + +#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 */ +) +{ + UINT r; + + + if (seed == 0) seed = 1; + do { + for (r = 0; r < 8; r++) seed = seed & 1 ? seed >> 1 ^ 0xA3000000 : seed >> 1; /* Shift 8 bits the 32-bit LFSR */ + *buff++ = (BYTE)seed; + } while (--n); + return seed; +} + +#endif +#endif + + + /*-----------------------------------------------------------------------*/ /* Load a sector and check if it is an FAT VBR */ /*-----------------------------------------------------------------------*/ -static BYTE check_fs ( /* 0:FAT, 1:exFAT, 2:Valid BS but not FAT, 3:Not a BS, 4:Disk error */ +/* Check what the sector is */ + +static UINT check_fs ( /* 0:FAT VBR, 1:exFAT VBR, 2:Valid BS but not FAT, 3:Invalid BS, 4:Disk error */ FATFS* fs, /* Filesystem object */ - DWORD sect /* Sector# (lba) to load and check if it is an FAT-VBR or not */ + LBA_t sect /* Sector to load and check if it is an FAT-VBR or not */ ) { - fs->wflag = 0; fs->winsect = 0xFFFFFFFF; /* Invaidate window */ - if (move_window(fs, sect) != FR_OK) return 4; /* Load boot record */ + fs->wflag = 0; fs->winsect = (LBA_t)0 - 1; /* Invaidate window */ + if (move_window(fs, sect) != FR_OK) return 4; /* Load the boot sector */ - if (ld_word(fs->win + BS_55AA) != 0xAA55) return 3; /* Check boot record signature (always here regardless of the sector size) */ + if (ld_word(fs->win + BS_55AA) != 0xAA55) return 3; /* Check boot signature (always here regardless of the sector size) */ + + if (FF_FS_EXFAT && !mem_cmp(fs->win + BS_JmpBoot, "\xEB\x76\x90" "EXFAT ", 11)) return 1; /* Check if exFAT VBR */ -#if FF_FS_EXFAT - if (!mem_cmp(fs->win + BS_JmpBoot, "\xEB\x76\x90" "EXFAT ", 11)) return 1; /* Check if exFAT VBR */ -#endif if (fs->win[BS_JmpBoot] == 0xE9 || fs->win[BS_JmpBoot] == 0xEB || fs->win[BS_JmpBoot] == 0xE8) { /* Valid JumpBoot code? */ if (!mem_cmp(fs->win + BS_FilSysType, "FAT", 3)) return 0; /* Is it an FAT VBR? */ if (!mem_cmp(fs->win + BS_FilSysType32, "FAT32", 5)) return 0; /* Is it an FAT32 VBR? */ @@ -3203,25 +3310,76 @@ static BYTE check_fs ( /* 0:FAT, 1:exFAT, 2:Valid BS but not FAT, 3:Not a BS, 4: } +/* Find an FAT volume */ +/* (It supports only generic partitioning rules, MBR, GPT and SFD) */ + +static UINT find_volume ( /* Returns BS status found in the hosting drive */ + FATFS* fs, /* Filesystem object */ + UINT part /* Partition to fined = 0:auto, 1..:forced */ +) +{ + UINT fmt, i; + DWORD mbr_pt[4]; + + + fmt = check_fs(fs, 0); /* Load sector 0 and check if it is an FAT VBR as SFD */ + if (fmt != 2 && (fmt >= 3 || part == 0)) return fmt; /* Returns if it is a FAT VBR as auto scan, not a BS or disk error */ + + /* Sector 0 is not an FAT VBR or forced partition number wants a partition */ + +#if FF_LBA64 + if (fs->win[MBR_Table + PTE_System] == 0xEE) { /* GPT protective MBR? */ + DWORD n_ent, v_ent, ofs; + QWORD pt_lba; + + if (move_window(fs, 1) != FR_OK) return 4; /* Load GPT header sector (next to MBR) */ + if (!test_gpt_header(fs->win)) return 3; /* Check if GPT header is valid */ + n_ent = ld_dword(fs->win + GPTH_PtNum); /* Number of entries */ + pt_lba = ld_qword(fs->win + GPTH_PtOfs); /* Table location */ + for (v_ent = i = 0; i < n_ent; i++) { /* Find FAT partition */ + if (move_window(fs, pt_lba + i * SZ_GPTE / SS(fs)) != FR_OK) return 4; /* PT sector */ + ofs = i * SZ_GPTE % SS(fs); /* Offset in the sector */ + if (!mem_cmp(fs->win + ofs + GPTE_PtGuid, GUID_MS_Basic, 16)) { /* MS basic data partition? */ + v_ent++; + fmt = check_fs(fs, ld_qword(fs->win + ofs + GPTE_FstLba)); /* Load VBR and check status */ + if (part == 0 && fmt <= 1) return fmt; /* Auto search (valid FAT volume found first) */ + if (part != 0 && v_ent == part) return fmt; /* Forced partition order (regardless of it is valid or not) */ + } + } + return 3; /* Not found */ + } +#endif + if (FF_MULTI_PARTITION && part > 4) return 3; /* MBR has 4 partitions max */ + for (i = 0; i < 4; i++) { /* Load partition offset in the MBR */ + mbr_pt[i] = ld_dword(fs->win + MBR_Table + i * SZ_PTE + PTE_StLba); + } + i = part ? part - 1 : 0; /* Table index to find first */ + do { /* Find an FAT volume */ + fmt = mbr_pt[i] ? check_fs(fs, mbr_pt[i]) : 3; /* Check if the partition is FAT */ + } while (part == 0 && fmt >= 2 && ++i < 4); + return fmt; +} + + /*-----------------------------------------------------------------------*/ /* Determine logical drive number and mount the volume if needed */ /*-----------------------------------------------------------------------*/ -static FRESULT find_volume ( /* FR_OK(0): successful, !=0: an error occurred */ +static FRESULT mount_volume ( /* FR_OK(0): successful, !=0: an error occurred */ const TCHAR** path, /* Pointer to pointer to the path name (drive number) */ FATFS** rfs, /* Pointer to pointer to the found filesystem object */ BYTE mode /* !=0: Check write protection for write access */ ) { - BYTE fmt, *pt; int vol; DSTATUS stat; - DWORD bsect, fasize, tsect, sysect, nclst, szbfat, br[4]; + LBA_t bsect; + DWORD tsect, sysect, fasize, nclst, szbfat; WORD nrsv; FATFS *fs; - UINT i; + UINT fmt; /* Get logical drive number */ @@ -3244,15 +3402,15 @@ static FRESULT find_volume ( /* FR_OK(0): successful, !=0: an error occurred */ if (!FF_FS_READONLY && mode && (stat & STA_PROTECT)) { /* Check write protection if needed */ return FR_WRITE_PROTECTED; } - return FR_OK; /* The filesystem object is valid */ + return FR_OK; /* The filesystem object is already valid */ } } /* The filesystem object is not valid. */ - /* Following code attempts to mount the volume. (analyze BPB and initialize the filesystem object) */ + /* Following code attempts to mount the volume. (find a FAT volume, analyze the BPB and initialize the filesystem object) */ fs->fs_type = 0; /* Clear the filesystem object */ - fs->pdrv = LD2PD(vol); /* Bind the logical drive and a physical drive */ + fs->pdrv = LD2PD(vol); /* Volume hosting physical drive */ stat = disk_initialize(fs->pdrv); /* Initialize the physical drive */ if (stat & STA_NOINIT) { /* Check if the initialization succeeded */ return FR_NOT_READY; /* Failed to initialize due to no medium or hard error */ @@ -3265,30 +3423,18 @@ static FRESULT find_volume ( /* FR_OK(0): successful, !=0: an error occurred */ if (SS(fs) > FF_MAX_SS || SS(fs) < FF_MIN_SS || (SS(fs) & (SS(fs) - 1))) return FR_DISK_ERR; #endif - /* Find an FAT partition on the drive. Supports only generic partitioning rules, FDISK (MBR) and SFD (w/o partition). */ - bsect = 0; - fmt = check_fs(fs, bsect); /* Load sector 0 and check if it is an FAT-VBR as SFD */ - if (fmt == 2 || (fmt < 2 && LD2PT(vol) != 0)) { /* Not an FAT-VBR or forced partition number */ - for (i = 0; i < 4; i++) { /* Get partition offset */ - pt = fs->win + (MBR_Table + i * SZ_PTE); - br[i] = pt[PTE_System] ? ld_dword(pt + PTE_StLba) : 0; - } - i = LD2PT(vol); /* Partition number: 0:auto, 1-4:forced */ - if (i != 0) i--; - do { /* Find an FAT volume */ - bsect = br[i]; - fmt = bsect ? check_fs(fs, bsect) : 3; /* Check the partition */ - } while (LD2PT(vol) == 0 && fmt >= 2 && ++i < 4); - } + /* Find an FAT volume on the drive */ + fmt = find_volume(fs, LD2PT(vol)); if (fmt == 4) return FR_DISK_ERR; /* An error occured in the disk I/O layer */ if (fmt >= 2) return FR_NO_FILESYSTEM; /* No FAT volume is found */ + bsect = fs->winsect; /* Volume location */ /* An FAT volume is found (bsect). Following code initializes the filesystem object */ #if FF_FS_EXFAT if (fmt == 1) { QWORD maxlba; - DWORD so, cv, bcl; + DWORD so, cv, bcl, i; for (i = BPB_ZeroedEx; i < BPB_ZeroedEx + 53 && fs->win[i] == 0; i++) ; /* Check zero filler */ if (i < BPB_ZeroedEx + 53) return FR_NO_FILESYSTEM; @@ -3300,7 +3446,7 @@ static FRESULT find_volume ( /* FR_OK(0): successful, !=0: an error occurred */ } maxlba = ld_qword(fs->win + BPB_TotSecEx) + bsect; /* Last LBA + 1 of the volume */ - if (maxlba >= 0x100000000) return FR_NO_FILESYSTEM; /* (It cannot be handled in 32-bit LBA) */ + if (!FF_LBA64 && maxlba >= 0x100000000) return FR_NO_FILESYSTEM; /* (It cannot be handled in 32-bit LBA) */ fs->fsize = ld_dword(fs->win + BPB_FatSzEx); /* Number of sectors per FAT */ @@ -3326,7 +3472,7 @@ static FRESULT find_volume ( /* FR_OK(0): successful, !=0: an error occurred */ for (;;) { /* Find the bitmap entry in the root directory (in only first cluster) */ if (i == 0) { if (so >= fs->csize) return FR_NO_FILESYSTEM; /* Not found? */ - if (move_window(fs, clst2sect(fs, fs->dirbase) + so) != FR_OK) return FR_DISK_ERR; + if (move_window(fs, clst2sect(fs, (DWORD)fs->dirbase) + so) != FR_OK) return FR_DISK_ERR; so++; } if (fs->win[i] == ET_BITMAP) break; /* Is it a bitmap entry? */ @@ -3426,7 +3572,7 @@ static FRESULT find_volume ( /* FR_OK(0): successful, !=0: an error occurred */ #endif /* !FF_FS_READONLY */ } - fs->fs_type = fmt; /* FAT sub-type */ + fs->fs_type = (BYTE)fmt;/* FAT sub-type */ fs->id = ++Fsid; /* Volume mount ID */ #if FF_USE_LFN == 1 fs->lfnbuf = LfnBuf; /* Static LFN working buffer */ @@ -3531,7 +3677,7 @@ FRESULT f_mount ( if (opt == 0) return FR_OK; /* Do not mount now, it will be mounted later */ - res = find_volume(&path, &fs, 0); /* Force mounted the volume */ + res = mount_volume(&path, &fs, 0); /* Force mounted the volume */ LEAVE_FF(fs, res); } @@ -3552,7 +3698,8 @@ FRESULT f_open ( DIR dj; FATFS *fs; #if !FF_FS_READONLY - DWORD dw, cl, bcs, clst, sc; + DWORD cl, bcs, clst; + LBA_t sc; FSIZE_t ofs; #endif DEF_NAMBUF @@ -3562,7 +3709,7 @@ FRESULT f_open ( /* Get logical drive number */ mode &= FF_FS_READONLY ? FA_READ : FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_CREATE_NEW | FA_OPEN_ALWAYS | FA_OPEN_APPEND; - res = find_volume(&path, &fs, mode); + res = mount_volume(&path, &fs, mode); if (res == FR_OK) { dj.obj.fs = fs; INIT_NAMBUF(fs); @@ -3625,10 +3772,10 @@ FRESULT f_open ( st_dword(dj.dir + DIR_FileSize, 0); fs->wflag = 1; if (cl != 0) { /* Remove the cluster chain if exist */ - dw = fs->winsect; + sc = fs->winsect; res = remove_chain(&dj.obj, cl, 0); if (res == FR_OK) { - res = move_window(fs, dw); + res = move_window(fs, sc); fs->last_clst = cl - 1; /* Reuse the cluster hole */ } } @@ -3704,7 +3851,8 @@ FRESULT f_open ( } fp->clust = clst; if (res == FR_OK && ofs % SS(fs)) { /* Fill sector buffer if not on the sector boundary */ - if ((sc = clst2sect(fs, clst)) == 0) { + sc = clst2sect(fs, clst); + if (sc == 0) { res = FR_INT_ERR; } else { fp->sect = sc + (DWORD)(ofs / SS(fs)); @@ -3741,7 +3889,8 @@ FRESULT f_read ( { FRESULT res; FATFS *fs; - DWORD clst, sect; + DWORD clst; + LBA_t sect; FSIZE_t remain; UINT rcnt, cc, csect; BYTE *rbuff = (BYTE*)buff; @@ -3811,7 +3960,7 @@ FRESULT f_read ( #endif fp->sect = sect; } - rcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes left in the sector */ + rcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes remains in the sector */ if (rcnt > btr) rcnt = btr; /* Clip it by btr if needed */ #if FF_FS_TINY if (move_window(fs, fp->sect) != FR_OK) ABORT(fs, FR_DISK_ERR); /* Move sector window */ @@ -3841,7 +3990,8 @@ FRESULT f_write ( { FRESULT res; FATFS *fs; - DWORD clst, sect; + DWORD clst; + LBA_t sect; UINT wcnt, cc, csect; const BYTE *wbuff = (const BYTE*)buff; @@ -3929,7 +4079,7 @@ FRESULT f_write ( #endif fp->sect = sect; } - wcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes left in the sector */ + wcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes remains in the sector */ if (wcnt > btw) wcnt = btw; /* Clip it by btw if needed */ #if FF_FS_TINY if (move_window(fs, fp->sect) != FR_OK) ABORT(fs, FR_DISK_ERR); /* Move sector window */ @@ -3989,9 +4139,9 @@ FRESULT f_sync ( if (res == FR_OK) { fs->dirbuf[XDIR_Attr] |= AM_ARC; /* Set archive attribute to indicate that the file has been changed */ fs->dirbuf[XDIR_GenFlags] = fp->obj.stat | 1; /* Update file allocation information */ - st_dword(fs->dirbuf + XDIR_FstClus, fp->obj.sclust); - st_qword(fs->dirbuf + XDIR_FileSize, fp->obj.objsize); - st_qword(fs->dirbuf + XDIR_ValidFileSize, fp->obj.objsize); + st_dword(fs->dirbuf + XDIR_FstClus, fp->obj.sclust); /* Update start cluster */ + st_qword(fs->dirbuf + XDIR_FileSize, fp->obj.objsize); /* Update file size */ + st_qword(fs->dirbuf + XDIR_ValidFileSize, fp->obj.objsize); /* (FatFs does not support Valid File Size feature) */ st_dword(fs->dirbuf + XDIR_ModTime, tm); /* Update modified time */ fs->dirbuf[XDIR_ModTime10] = 0; st_dword(fs->dirbuf + XDIR_AccTime, 0); @@ -4101,7 +4251,7 @@ FRESULT f_chdir ( /* Get logical drive */ - res = find_volume(&path, &fs, 0); + res = mount_volume(&path, &fs, 0); if (res == FR_OK) { dj.obj.fs = fs; INIT_NAMBUF(fs); @@ -4162,9 +4312,9 @@ FRESULT f_getcwd ( TCHAR *tp = buff; #if FF_VOLUMES >= 2 UINT vl; -#endif #if FF_STR_VOLUME_ID const char *vp; +#endif #endif FILINFO fno; DEF_NAMBUF @@ -4172,7 +4322,7 @@ FRESULT f_getcwd ( /* Get logical drive */ buff[0] = 0; /* Set null string to get current volume */ - res = find_volume((const TCHAR**)&buff, &fs, 0); /* Get current volume */ + res = mount_volume((const TCHAR**)&buff, &fs, 0); /* Get current volume */ if (res == FR_OK) { dj.obj.fs = fs; INIT_NAMBUF(fs); @@ -4256,10 +4406,12 @@ FRESULT f_lseek ( { FRESULT res; FATFS *fs; - DWORD clst, bcs, nsect; + DWORD clst, bcs; + LBA_t nsect; FSIZE_t ifptr; #if FF_USE_FASTSEEK - DWORD cl, pcl, ncl, tcl, dsc, tlen, ulen, *tbl; + DWORD cl, pcl, ncl, tcl, tlen, ulen, *tbl; + LBA_t dsc; #endif res = validate(&fp->obj, &fs); /* Check validity of the file object */ @@ -4423,7 +4575,7 @@ FRESULT f_opendir ( if (!dp) return FR_INVALID_OBJECT; /* Get logical drive */ - res = find_volume(&path, &fs, 0); + res = mount_volume(&path, &fs, 0); if (res == FR_OK) { dp->obj.fs = fs; INIT_NAMBUF(fs); @@ -4605,7 +4757,7 @@ FRESULT f_stat ( /* Get logical drive */ - res = find_volume(&path, &dj.obj.fs, 0); + res = mount_volume(&path, &dj.obj.fs, 0); if (res == FR_OK) { INIT_NAMBUF(dj.obj.fs); res = follow_path(&dj, path); /* Follow the file path */ @@ -4637,13 +4789,14 @@ FRESULT f_getfree ( { FRESULT res; FATFS *fs; - DWORD nfree, clst, sect, stat; + DWORD nfree, clst, stat; + LBA_t sect; UINT i; FFOBJID obj; /* Get logical drive */ - res = find_volume(&path, &fs, 0); + res = mount_volume(&path, &fs, 0); if (res == FR_OK) { *fatfs = fs; /* Return ptr to the fs object */ /* If free_clst is valid, return it without full FAT scan */ @@ -4783,7 +4936,7 @@ FRESULT f_unlink ( /* Get logical drive */ - res = find_volume(&path, &fs, FA_WRITE); + res = mount_volume(&path, &fs, FA_WRITE); if (res == FR_OK) { dj.obj.fs = fs; INIT_NAMBUF(fs); @@ -4874,7 +5027,7 @@ FRESULT f_mkdir ( DEF_NAMBUF - res = find_volume(&path, &fs, FA_WRITE); /* Get logical drive */ + res = mount_volume(&path, &fs, FA_WRITE); /* Get logical drive */ if (res == FR_OK) { dj.obj.fs = fs; INIT_NAMBUF(fs); @@ -4913,7 +5066,7 @@ FRESULT f_mkdir ( if (fs->fs_type == FS_EXFAT) { /* Initialize directory entry block */ st_dword(fs->dirbuf + XDIR_ModTime, tm); /* Created time */ st_dword(fs->dirbuf + XDIR_FstClus, dcl); /* Table start cluster */ - st_dword(fs->dirbuf + XDIR_FileSize, (DWORD)fs->csize * SS(fs)); /* File size needs to be valid */ + st_dword(fs->dirbuf + XDIR_FileSize, (DWORD)fs->csize * SS(fs)); /* Directory size needs to be valid */ st_dword(fs->dirbuf + XDIR_ValidFileSize, (DWORD)fs->csize * SS(fs)); fs->dirbuf[XDIR_GenFlags] = 3; /* Initialize the object flag */ fs->dirbuf[XDIR_Attr] = AM_DIR; /* Attribute */ @@ -4955,12 +5108,12 @@ FRESULT f_rename ( DIR djo, djn; FATFS *fs; BYTE buf[FF_FS_EXFAT ? SZDIRE * 2 : SZDIRE], *dir; - DWORD dw; + LBA_t sect; DEF_NAMBUF get_ldnumber(&path_new); /* Snip the drive number of new name off */ - res = find_volume(&path_old, &fs, FA_WRITE); /* Get logical drive of the old object */ + res = mount_volume(&path_old, &fs, FA_WRITE); /* Get logical drive of the old object */ if (res == FR_OK) { djo.obj.fs = fs; INIT_NAMBUF(fs); @@ -5014,12 +5167,12 @@ FRESULT f_rename ( if (!(dir[DIR_Attr] & AM_DIR)) dir[DIR_Attr] |= AM_ARC; /* Set archive attribute if it is a file */ fs->wflag = 1; if ((dir[DIR_Attr] & AM_DIR) && djo.obj.sclust != djn.obj.sclust) { /* Update .. entry in the sub-directory if needed */ - dw = clst2sect(fs, ld_clust(fs, dir)); - if (dw == 0) { + sect = clst2sect(fs, ld_clust(fs, dir)); + if (sect == 0) { res = FR_INT_ERR; } else { /* Start of critical section where an interruption can cause a cross-link */ - res = move_window(fs, dw); + res = move_window(fs, sect); dir = fs->win + SZDIRE * 1; /* Ptr to .. entry */ if (res == FR_OK && dir[1] == '.') { st_clust(fs, dir, djn.obj.sclust); @@ -5068,7 +5221,7 @@ FRESULT f_chmod ( DEF_NAMBUF - res = find_volume(&path, &fs, FA_WRITE); /* Get logical drive */ + res = mount_volume(&path, &fs, FA_WRITE); /* Get logical drive */ if (res == FR_OK) { dj.obj.fs = fs; INIT_NAMBUF(fs); @@ -5114,7 +5267,7 @@ FRESULT f_utime ( DEF_NAMBUF - res = find_volume(&path, &fs, FA_WRITE); /* Get logical drive */ + res = mount_volume(&path, &fs, FA_WRITE); /* Get logical drive */ if (res == FR_OK) { dj.obj.fs = fs; INIT_NAMBUF(fs); @@ -5163,7 +5316,7 @@ FRESULT f_getlabel ( WCHAR wc; /* Get logical drive */ - res = find_volume(&path, &fs, 0); + res = mount_volume(&path, &fs, 0); /* Get volume label */ if (res == FR_OK && label) { @@ -5261,7 +5414,7 @@ FRESULT f_setlabel ( #endif /* Get logical drive */ - res = find_volume(&label, &fs, FA_WRITE); + res = mount_volume(&label, &fs, FA_WRITE); if (res != FR_OK) LEAVE_FF(fs, res); #if FF_FS_EXFAT @@ -5464,7 +5617,8 @@ FRESULT f_forward ( { FRESULT res; FATFS *fs; - DWORD clst, sect; + DWORD clst; + LBA_t sect; FSIZE_t remain; UINT rcnt, csect; BYTE *dbuf; @@ -5509,7 +5663,7 @@ FRESULT f_forward ( dbuf = fp->buf; #endif fp->sect = sect; - rcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes left in the sector */ + rcnt = SS(fs) - (UINT)fp->fptr % SS(fs); /* Number of bytes remains in the sector */ if (rcnt > btf) rcnt = btf; /* Clip it by btr if needed */ rcnt = (*func)(dbuf + ((UINT)fp->fptr % SS(fs)), rcnt); /* Forward the file data */ if (rcnt == 0) ABORT(fs, FR_INT_ERR); @@ -5521,137 +5675,326 @@ FRESULT f_forward ( -#if FF_USE_MKFS && !FF_FS_READONLY +#if !FF_FS_READONLY && FF_USE_MKFS /*-----------------------------------------------------------------------*/ /* Create an FAT/exFAT volume */ /*-----------------------------------------------------------------------*/ -FRESULT f_mkfs ( - const TCHAR* path, /* Logical drive number */ - BYTE opt, /* Format option */ - DWORD au, /* Size of allocation unit (cluster) [byte] */ - void* work, /* Pointer to working buffer (null: use heap memory) */ - UINT len /* Size of working buffer [byte] */ +#define N_SEC_TRACK 63 /* Sectors per track for determination of drive CHS */ +#define GPT_ALIGN 0x100000 /* Alignment of partitions in GPT [byte] (>=128KB) */ +#define GPT_ITEMS 128 /* Number of GPT table size (>=128, sector aligned) */ + + +/* Create partitions on the physical drive */ + +static FRESULT create_partition ( + BYTE drv, /* Physical drive number */ + const LBA_t plst[], /* Partition list */ + UINT sys, /* System ID (for only MBR, temp setting) and bit8:GPT */ + BYTE* buf /* Working buffer for a sector */ +) +{ + UINT i, cy; + LBA_t sz_drv; + DWORD sz_drv32, s_lba32, n_lba32; + BYTE *pte, hd, n_hd, sc, n_sc; + + /* Get drive size */ + if (disk_ioctl(drv, GET_SECTOR_COUNT, &sz_drv) != RES_OK) return FR_DISK_ERR; + +#if FF_LBA64 + if (sz_drv >= FF_MIN_GPT) { /* Create partitions in GPT */ + WORD ss; + UINT sz_pt, pi, si, ofs; + DWORD bcc, rnd, align; + QWORD s_lba64, n_lba64, sz_pool, s_bpt; + static const BYTE gpt_mbr[16] = {0x00, 0x00, 0x02, 0x00, 0xEE, 0xFE, 0xFF, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF}; + +#if FF_MAX_SS != FF_MIN_SS + if (disk_ioctl(drv, GET_SECTOR_SIZE, &ss) != RES_OK) return FR_DISK_ERR; /* Get sector size */ + if (ss > FF_MAX_SS || ss < FF_MIN_SS || (ss & (ss - 1))) return FR_DISK_ERR; +#else + ss = FF_MAX_SS; +#endif + rnd = GET_FATTIME(); /* Random seed */ + align = GPT_ALIGN / ss; /* Partition alignment [sector] */ + sz_pt = GPT_ITEMS * SZ_GPTE / ss; /* Size of PT [sector] */ + s_bpt = sz_drv - sz_pt - 1; /* Backup PT start sector */ + s_lba64 = 2 + sz_pt; /* First allocatable sector */ + sz_pool = s_bpt - s_lba64; /* Size of allocatable area */ + bcc = 0xFFFFFFFF; n_lba64 = 1; + pi = si = 0; /* partition table index, size table index */ + do { + if (pi * SZ_GPTE % ss == 0) mem_set(buf, 0, ss); /* Clean the buffer if needed */ + if (n_lba64 != 0) { /* Is the size table not termintated? */ + s_lba64 = (s_lba64 + align - 1) & ((QWORD)0 - align); /* Align partition start */ + n_lba64 = plst[si++]; /* Get a partition size */ + if (n_lba64 <= 100) { /* Is the size in percentage? */ + n_lba64 = sz_pool * n_lba64 / 100; + n_lba64 = (n_lba64 + align - 1) & ((QWORD)0 - align); /* Align partition end (only if in percentage) */ + } + if (s_lba64 + n_lba64 > s_bpt) { /* Clip at end of the pool */ + n_lba64 = (s_lba64 < s_bpt) ? s_bpt - s_lba64 : 0; + } + } + if (n_lba64 != 0) { /* Add a partition? */ + ofs = pi * SZ_GPTE % ss; + mem_cpy(buf + ofs + GPTE_PtGuid, GUID_MS_Basic, 16); /* Partition GUID (Microsoft Basic Data) */ + rnd = make_rand(rnd, buf + ofs + GPTE_UpGuid, 16); /* Unique partition GUID */ + st_qword(buf + ofs + GPTE_FstLba, s_lba64); /* Partition start LBA */ + st_qword(buf + ofs + GPTE_LstLba, s_lba64 + n_lba64 - 1); /* Partition end LBA */ + s_lba64 += n_lba64; /* Next partition LBA */ + } + if ((pi + 1) * SZ_GPTE % ss == 0) { /* Write the 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; /* Primary table */ + if (disk_write(drv, buf, s_bpt + pi * SZ_GPTE / ss, 1) != RES_OK) return FR_DISK_ERR; /* Secondary table */ + } + } while (++pi < GPT_ITEMS); + + /* Create primary GPT header */ + mem_set(buf, 0, ss); + mem_cpy(buf + GPTH_Sign, "EFI PART" "\0\0\1\0" "\x5C\0\0", 16); /* Signature, version (1.0) and size (92) */ + st_dword(buf + GPTH_PtBcc, ~bcc); /* Table check sum */ + st_qword(buf + GPTH_CurLba, 1); /* LBA of this header */ + st_qword(buf + GPTH_BakLba, sz_drv - 1); /* LBA of another header */ + st_qword(buf + GPTH_FstLba, 2 + sz_pt); /* LBA of first allocatable sector */ + st_qword(buf + GPTH_LstLba, s_bpt - 1); /* LBA of last allocatable sector */ + st_dword(buf + GPTH_PteSize, SZ_GPTE); /* Size of a table entry */ + st_dword(buf + GPTH_PtNum, GPT_ITEMS); /* Number of table entries */ + st_dword(buf + GPTH_PtOfs, 2); /* LBA of this table */ + rnd = make_rand(rnd, buf + GPTH_DskGuid, 16); /* Disk GUID */ + for (i = 0, bcc= 0xFFFFFFFF; i < 92; bcc = crc32(bcc, buf[i++])) ; /* Calculate header check sum */ + st_dword(buf + GPTH_Bcc, ~bcc); /* Header check sum */ + if (disk_write(drv, buf, 1, 1) != RES_OK) return FR_DISK_ERR; + + /* Create secondary GPT header */ + st_qword(buf + GPTH_CurLba, sz_drv - 1); /* LBA of this header */ + st_qword(buf + GPTH_BakLba, 1); /* LBA of another header */ + st_qword(buf + GPTH_PtOfs, s_bpt); /* LBA of this table */ + st_dword(buf + GPTH_Bcc, 0); + for (i = 0, bcc= 0xFFFFFFFF; i < 92; bcc = crc32(bcc, buf[i++])) ; /* Calculate header check sum */ + st_dword(buf + GPTH_Bcc, ~bcc); /* Header check sum */ + if (disk_write(drv, buf, sz_drv - 1, 1) != RES_OK) return FR_DISK_ERR; + + /* Create protective MBR */ + mem_set(buf, 0, ss); + mem_cpy(buf + MBR_Table, gpt_mbr, 16); /* Create a GPT partition */ + st_word(buf + BS_55AA, 0xAA55); + if (disk_write(drv, buf, 0, 1) != RES_OK) return FR_DISK_ERR; + + } else +#endif + { /* Create partitions in MBR */ + sz_drv32 = (DWORD)sz_drv; + n_sc = N_SEC_TRACK; /* Determine drive CHS without any consideration of the drive geometry */ + for (n_hd = 8; n_hd != 0 && sz_drv32 / n_hd / n_sc > 1024; n_hd *= 2) ; + if (n_hd == 0) n_hd = 255; /* Number of heads needs to be <256 */ + + mem_set(buf, 0, FF_MAX_SS); /* Clear MBR */ + pte = buf + MBR_Table; /* Partition table in the MBR */ + for (i = 0, s_lba32 = n_sc; i < 4 && s_lba32 != 0 && s_lba32 < sz_drv32; i++, s_lba32 += n_lba32) { + n_lba32 = (DWORD)plst[i]; /* Get partition size */ + if (n_lba32 <= 100) n_lba32 = (n_lba32 == 100) ? sz_drv32 : sz_drv32 / 100 * n_lba32; /* Size in percentage? */ + if (s_lba32 + n_lba32 > sz_drv32 || s_lba32 + n_lba32 < s_lba32) n_lba32 = sz_drv32 - s_lba32; /* Clip at drive size */ + if (n_lba32 == 0) break; /* End of table or no sector to allocate? */ + + st_dword(pte + PTE_StLba, s_lba32); /* Start LBA */ + st_dword(pte + PTE_SizLba, n_lba32); /* Number of sectors */ + pte[PTE_System] = (BYTE)sys; /* System type */ + + cy = (UINT)(s_lba32 / n_sc / n_hd); /* Start cylinder */ + hd = (BYTE)(s_lba32 / n_sc % n_hd); /* Start head */ + sc = (BYTE)(s_lba32 % n_sc + 1); /* Start sector */ + pte[PTE_StHead] = hd; + pte[PTE_StSec] = (BYTE)((cy >> 2 & 0xC0) | sc); + pte[PTE_StCyl] = (BYTE)cy; + + cy = (UINT)((s_lba32 + n_lba32 - 1) / n_sc / n_hd); /* End cylinder */ + hd = (BYTE)((s_lba32 + n_lba32 - 1) / n_sc % n_hd); /* End head */ + sc = (BYTE)((s_lba32 + n_lba32 - 1) % n_sc + 1); /* End sector */ + pte[PTE_EdHead] = hd; + pte[PTE_EdSec] = (BYTE)((cy >> 2 & 0xC0) | sc); + pte[PTE_EdCyl] = (BYTE)cy; + + pte += SZ_PTE; /* Next entry */ + } + + st_word(buf + BS_55AA, 0xAA55); /* MBR signature */ + if (disk_write(drv, buf, 0, 1) != RES_OK) return FR_DISK_ERR; /* Write it to the MBR */ + } + + return FR_OK; +} + + + +FRESULT f_mkfs ( + const TCHAR* path, /* Logical drive number */ + const MKFS_PARM* opt, /* Format options */ + void* work, /* Pointer to working buffer (null: use heap memory) */ + UINT len /* Size of working buffer [byte] */ ) { - const UINT n_fats = 1; /* Number of FATs for FAT/FAT32 volume (1 or 2) */ - const UINT n_rootdir = 512; /* Number of root directory entries for FAT volume */ 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) */ - BYTE fmt, sys, *buf, *pte, pdrv, part; + static const MKFS_PARM defopt = {FM_ANY, 0, 0, 0, 0}; /* Default parameter */ + BYTE fsopt, fsty, sys, *buf, *pte, pdrv, ipart; WORD ss; /* Sector size */ - DWORD szb_buf, sz_buf, sz_blk, n_clst, pau, sect, nsect, n; - DWORD b_vol, b_fat, b_data; /* Base LBA for volume, fat, data */ - DWORD sz_vol, sz_rsv, sz_fat, sz_dir; /* Size for volume, fat, dir, data */ - UINT i; + DWORD sz_buf, sz_blk, n_clst, pau, nsect, n; + LBA_t sz_vol, b_vol, b_fat, b_data; /* Size of volume, Base LBA of volume, fat, 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 */ int vol; - DSTATUS stat; -#if FF_USE_TRIM || FF_FS_EXFAT - DWORD tbl[3]; -#endif + DSTATUS ds; + FRESULT fr; /* Check mounted drive and clear work area */ vol = get_ldnumber(&path); /* Get target logical drive */ if (vol < 0) return FR_INVALID_DRIVE; - if (FatFs[vol]) FatFs[vol]->fs_type = 0; /* Clear the volume if mounted */ - pdrv = LD2PD(vol); /* Physical drive */ - part = LD2PT(vol); /* Partition (0:create as new, 1-4:get from partition table) */ + if (FatFs[vol]) FatFs[vol]->fs_type = 0; /* Clear the fs object if mounted */ + pdrv = LD2PD(vol); /* Physical drive */ + ipart = LD2PT(vol); /* Partition (0:create as new, 1..:get from partition table) */ + if (!opt) opt = &defopt; /* Use default parameter if it is not given */ - /* Check physical drive status */ - stat = disk_initialize(pdrv); - if (stat & STA_NOINIT) return FR_NOT_READY; - if (stat & STA_PROTECT) return FR_WRITE_PROTECTED; - if (disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_blk) != RES_OK || !sz_blk || sz_blk > 32768 || (sz_blk & (sz_blk - 1))) sz_blk = 1; /* Erase block to align data area */ -#if FF_MAX_SS != FF_MIN_SS /* Get sector size of the medium if variable sector size cfg. */ + /* Get physical drive status (sz_drv, sz_blk, ss) */ + ds = disk_initialize(pdrv); + if (ds & STA_NOINIT) return FR_NOT_READY; + if (ds & STA_PROTECT) return FR_WRITE_PROTECTED; + sz_blk = opt->align; + if (sz_blk == 0 && disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_blk) != RES_OK) sz_blk = 1; + if (sz_blk == 0 || sz_blk > 0x8000 || (sz_blk & (sz_blk - 1))) sz_blk = 1; +#if FF_MAX_SS != FF_MIN_SS if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &ss) != RES_OK) return FR_DISK_ERR; if (ss > FF_MAX_SS || ss < FF_MIN_SS || (ss & (ss - 1))) return FR_DISK_ERR; #else ss = FF_MAX_SS; #endif - if ((au != 0 && au < ss) || au > 0x1000000 || (au & (au - 1))) return FR_INVALID_PARAMETER; /* Check if au is valid */ - au /= ss; /* Cluster size in unit of sector */ + /* Options for FAT sub-type and FAT parameters */ + fsopt = opt->fmt & (FM_ANY | FM_SFD); + n_fat = (opt->n_fat >= 1 && opt->n_fat <= 2) ? opt->n_fat : 1; + n_root = (opt->n_root >= 1 && opt->n_root <= 32768 && (opt->n_root % (ss / SZDIRE)) == 0) ? opt->n_root : 512; + sz_au = (opt->au_size <= 0x1000000 && (opt->au_size & (opt->au_size - 1)) == 0) ? opt->au_size : 0; + sz_au /= ss; /* Byte --> Sector */ /* Get working buffer */ + sz_buf = len / ss; /* Size of working buffer [sector] */ + if (sz_buf == 0) return FR_NOT_ENOUGH_CORE; + buf = (BYTE*)work; /* Working buffer */ #if FF_USE_LFN == 3 - if (!work) { /* Use heap memory for working buffer */ - for (szb_buf = MAX_MALLOC, buf = 0; szb_buf >= ss && (buf = ff_memalloc(szb_buf)) == 0; szb_buf /= 2) ; - sz_buf = szb_buf / ss; /* Size of working buffer (sector) */ - } else + if (!buf) buf = ff_memalloc(sz_buf * ss); /* Use heap memory for working buffer */ #endif - { - buf = (BYTE*)work; /* Working buffer */ - sz_buf = len / ss; /* Size of working buffer (sector) */ - szb_buf = sz_buf * ss; /* Size of working buffer (byte) */ - } - if (!buf || sz_buf == 0) return FR_NOT_ENOUGH_CORE; + if (!buf) return FR_NOT_ENOUGH_CORE; /* Determine where the volume to be located (b_vol, sz_vol) */ - if (FF_MULTI_PARTITION && part != 0) { - /* Get partition information from partition table in the MBR */ + b_vol = sz_vol = 0; + if (FF_MULTI_PARTITION && ipart != 0) { /* Is the volume associated with any specific partition? */ + /* Get partition location from the existing partition table */ if (disk_read(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Load MBR */ if (ld_word(buf + BS_55AA) != 0xAA55) LEAVE_MKFS(FR_MKFS_ABORTED); /* Check if MBR is valid */ - pte = buf + (MBR_Table + (part - 1) * SZ_PTE); - if (pte[PTE_System] == 0) LEAVE_MKFS(FR_MKFS_ABORTED); /* No partition? */ - b_vol = ld_dword(pte + PTE_StLba); /* Get volume start sector */ - sz_vol = ld_dword(pte + PTE_SizLba); /* Get volume size */ - } else { - /* Create a single-partition in this function */ +#if FF_LBA64 + if (buf[MBR_Table + PTE_System] == 0xEE) { /* GPT protective MBR? */ + DWORD n_ent, ofs; + QWORD pt_lba; + + /* Get the partition location from GPT */ + if (disk_read(pdrv, buf, 1, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Load GPT header sector (next to MBR) */ + if (!test_gpt_header(buf)) LEAVE_MKFS(FR_MKFS_ABORTED); /* Check if GPT header is valid */ + n_ent = ld_dword(buf + GPTH_PtNum); /* Number of entries */ + pt_lba = ld_qword(buf + GPTH_PtOfs); /* Table start sector */ + ofs = i = 0; + while (n_ent) { /* Find MS Basic partition with order of ipart */ + if (ofs == 0 && disk_read(pdrv, buf, pt_lba++, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Get PT sector */ + if (!mem_cmp(buf + ofs + GPTE_PtGuid, GUID_MS_Basic, 16) && ++i == ipart) { /* MS basic data partition? */ + b_vol = ld_qword(buf + ofs + GPTE_FstLba); + sz_vol = ld_qword(buf + ofs + GPTE_LstLba) - b_vol + 1; + break; + } + n_ent--; ofs = (ofs + SZ_GPTE) % ss; /* Next entry */ + } + if (n_ent == 0) LEAVE_MKFS(FR_MKFS_ABORTED); /* Partition not found */ + fsopt |= 0x80; /* Partitioning is in GPT */ + } else +#endif + { /* Get the partition location from MBR partition table */ + pte = buf + (MBR_Table + (ipart - 1) * SZ_PTE); + if (ipart > 4 || pte[PTE_System] == 0) LEAVE_MKFS(FR_MKFS_ABORTED); /* No partition? */ + b_vol = ld_dword(pte + PTE_StLba); /* Get volume start sector */ + sz_vol = ld_dword(pte + PTE_SizLba); /* Get volume size */ + } + } else { /* The volume is associated with a physical drive */ if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_vol) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); - b_vol = (opt & FM_SFD) ? 0 : 63; /* Volume start sector */ - if (sz_vol < b_vol) LEAVE_MKFS(FR_MKFS_ABORTED); - sz_vol -= b_vol; /* Volume size */ + if (!(fsopt & FM_SFD)) { /* To be partitioned? */ + /* Create a single-partition on the drive in this function */ +#if FF_LBA64 + if (sz_vol >= FF_MIN_GPT) { /* Which partition type to create, MBR or GPT? */ + fsopt |= 0x80; /* Partitioning is in GPT */ + b_vol = GPT_ALIGN / ss; sz_vol -= b_vol + GPT_ITEMS * SZ_GPTE / ss + 1; /* Estimated partition offset and size */ + } else +#endif + { /* Partitioning is in MBR */ + if (sz_vol > N_SEC_TRACK) { + b_vol = N_SEC_TRACK; sz_vol -= b_vol; /* Estimated partition offset and size */ + } + } + } } if (sz_vol < 128) LEAVE_MKFS(FR_MKFS_ABORTED); /* Check if volume size is >=128s */ - /* Pre-determine the FAT type */ - do { - if (FF_FS_EXFAT && (opt & FM_EXFAT)) { /* exFAT possible? */ - if ((opt & FM_ANY) == FM_EXFAT || sz_vol >= 0x4000000 || au > 128) { /* exFAT only, vol >= 64Ms or au > 128s ? */ - fmt = FS_EXFAT; break; + /* Now start to create a 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 ? */ + fsty = FS_EXFAT; break; } } - if (au > 128) LEAVE_MKFS(FR_INVALID_PARAMETER); /* Too large au for FAT/FAT32 */ - if (opt & FM_FAT32) { /* FAT32 possible? */ - if ((opt & FM_ANY) == FM_FAT32 || !(opt & FM_FAT)) { /* FAT32 only or no-FAT? */ - fmt = FS_FAT32; break; +#if FF_LBA64 + if (sz_vol >= 0x100000000) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too large volume for FAT/FAT32 */ +#endif + if (sz_au > 128) sz_au = 128; /* Invalid AU for FAT/FAT32? */ + if (fsopt & FM_FAT32) { /* FAT32 possible? */ + if (!(fsopt & FM_FAT)) { /* no-FAT? */ + fsty = FS_FAT32; break; } } - if (!(opt & FM_FAT)) LEAVE_MKFS(FR_INVALID_PARAMETER); /* no-FAT? */ - fmt = FS_FAT16; + if (!(fsopt & FM_FAT)) LEAVE_MKFS(FR_INVALID_PARAMETER); /* no-FAT? */ + fsty = FS_FAT16; } while (0); #if FF_FS_EXFAT - if (fmt == FS_EXFAT) { /* Create an exFAT volume */ - DWORD szb_bit, szb_case, sum, nb, cl; + if (fsty == FS_EXFAT) { /* Create an exFAT volume */ + DWORD szb_bit, szb_case, sum, nb, cl, tbl[3]; WCHAR ch, si; UINT j, st; BYTE b; - if (sz_vol < 0x1000) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume? */ + if (sz_vol < 0x1000) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume for exFAT? */ #if FF_USE_TRIM - tbl[0] = b_vol; tbl[1] = b_vol + sz_vol - 1; /* Inform the device the volume area may be erased */ - disk_ioctl(pdrv, CTRL_TRIM, tbl); + lba[0] = b_vol; lba[1] = b_vol + sz_vol - 1; /* Inform storage device that the volume area may be erased */ + disk_ioctl(pdrv, CTRL_TRIM, lba); #endif /* Determine FAT location, data location and number of clusters */ - if (au == 0) { /* au auto-selection */ - au = 8; - if (sz_vol >= 0x80000) au = 64; /* >= 512Ks */ - if (sz_vol >= 0x4000000) au = 256; /* >= 64Ms */ + if (sz_au == 0) { /* AU auto-selection */ + sz_au = 8; + if (sz_vol >= 0x80000) sz_au = 64; /* >= 512Ks */ + if (sz_vol >= 0x4000000) sz_au = 256; /* >= 64Ms */ } b_fat = b_vol + 32; /* FAT start at offset 32 */ - sz_fat = ((sz_vol / au + 2) * 4 + ss - 1) / ss; /* Number of FAT sectors */ - b_data = (b_fat + sz_fat + sz_blk - 1) & ~(sz_blk - 1); /* Align data area to the erase block boundary */ - if (b_data >= sz_vol / 2) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume? */ - n_clst = (sz_vol - (b_data - b_vol)) / au; /* Number of clusters */ + sz_fat = (DWORD)((sz_vol / sz_au + 2) * 4 + ss - 1) / ss; /* Number of FAT sectors */ + b_data = (b_fat + sz_fat + sz_blk - 1) & ~((LBA_t)sz_blk - 1); /* Align data area to the erase block boundary */ + if (b_data - b_vol >= sz_vol / 2) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume? */ + n_clst = (DWORD)(sz_vol - (b_data - b_vol)) / sz_au; /* Number of clusters */ if (n_clst <16) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too few clusters? */ if (n_clst > MAX_EXFAT) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too many clusters? */ - szb_bit = (n_clst + 7) / 8; /* Size of allocation bitmap */ - tbl[0] = (szb_bit + au * ss - 1) / (au * ss); /* Number of allocation bitmap clusters */ + szb_bit = (n_clst + 7) / 8; /* Size of allocation bitmap */ + tbl[0] = (szb_bit + sz_au * ss - 1) / (sz_au * ss); /* Number of allocation bitmap clusters */ /* Create a compressed up-case table */ - sect = b_data + au * tbl[0]; /* Table start sector */ + sect = b_data + sz_au * tbl[0]; /* Table start sector */ sum = 0; /* Table checksum to be stored in the 82 entry */ st = 0; si = 0; i = 0; j = 0; szb_case = 0; do { @@ -5679,22 +6022,22 @@ FRESULT f_mkfs ( sum = xsum32(buf[i + 0] = (BYTE)ch, sum); /* Put it into the write buffer */ sum = xsum32(buf[i + 1] = (BYTE)(ch >> 8), sum); i += 2; szb_case += 2; - if (si == 0 || i == szb_buf) { /* Write buffered data when buffer full or end of process */ + if (si == 0 || i == sz_buf * ss) { /* Write buffered data when buffer full or end of process */ n = (i + ss - 1) / ss; if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); sect += n; i = 0; } } while (si); - tbl[1] = (szb_case + au * ss - 1) / (au * ss); /* Number of up-case table clusters */ + tbl[1] = (szb_case + sz_au * ss - 1) / (sz_au * ss); /* Number of up-case table clusters */ tbl[2] = 1; /* Number of root dir clusters */ /* Initialize the allocation bitmap */ sect = b_data; nsect = (szb_bit + ss - 1) / ss; /* Start of bitmap and number of sectors */ nb = tbl[0] + tbl[1] + tbl[2]; /* Number of clusters in-use by system */ do { - mem_set(buf, 0, szb_buf); - for (i = 0; nb >= 8 && i < szb_buf; buf[i++] = 0xFF, nb -= 8) ; - for (b = 1; nb != 0 && i < szb_buf; buf[i] |= b, b <<= 1, nb--) ; + mem_set(buf, 0, sz_buf * ss); + for (i = 0; nb >= 8 && i < sz_buf * ss; buf[i++] = 0xFF, nb -= 8) ; + for (b = 1; nb != 0 && i < sz_buf * ss; buf[i] |= b, b <<= 1, nb--) ; n = (nsect > sz_buf) ? sz_buf : nsect; /* Write the buffered data */ if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); sect += n; nsect -= n; @@ -5704,34 +6047,34 @@ FRESULT f_mkfs ( sect = b_fat; nsect = sz_fat; /* Start of FAT and number of FAT sectors */ j = nb = cl = 0; do { - mem_set(buf, 0, szb_buf); i = 0; /* Clear work area and reset write index */ - if (cl == 0) { /* Set entry 0 and 1 */ + mem_set(buf, 0, sz_buf * ss); i = 0; /* Clear work area and reset write index */ + if (cl == 0) { /* Set FAT [0] and FAT[1] */ st_dword(buf + i, 0xFFFFFFF8); i += 4; cl++; st_dword(buf + i, 0xFFFFFFFF); i += 4; cl++; } do { /* Create chains of bitmap, up-case and root dir */ - while (nb != 0 && i < szb_buf) { /* Create a chain */ + while (nb != 0 && i < sz_buf * ss) { /* Create a chain */ st_dword(buf + i, (nb > 1) ? cl + 1 : 0xFFFFFFFF); i += 4; cl++; nb--; } if (nb == 0 && j < 3) nb = tbl[j++]; /* Next chain */ - } while (nb != 0 && i < szb_buf); + } while (nb != 0 && i < sz_buf * ss); n = (nsect > sz_buf) ? sz_buf : nsect; /* Write the buffered data */ if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); sect += n; nsect -= n; } while (nsect); /* Initialize the root directory */ - mem_set(buf, 0, szb_buf); - buf[SZDIRE * 0 + 0] = ET_VLABEL; /* Volume label entry */ - buf[SZDIRE * 1 + 0] = ET_BITMAP; /* Bitmap entry */ - st_dword(buf + SZDIRE * 1 + 20, 2); /* cluster */ - st_dword(buf + SZDIRE * 1 + 24, szb_bit); /* size */ - buf[SZDIRE * 2 + 0] = ET_UPCASE; /* Up-case table entry */ - st_dword(buf + SZDIRE * 2 + 4, sum); /* sum */ - st_dword(buf + SZDIRE * 2 + 20, 2 + tbl[0]); /* cluster */ - st_dword(buf + SZDIRE * 2 + 24, szb_case); /* size */ - sect = b_data + au * (tbl[0] + tbl[1]); nsect = au; /* Start of the root directory and number of sectors */ + mem_set(buf, 0, sz_buf * ss); + buf[SZDIRE * 0 + 0] = ET_VLABEL; /* Volume label entry (no label) */ + buf[SZDIRE * 1 + 0] = ET_BITMAP; /* Bitmap entry */ + st_dword(buf + SZDIRE * 1 + 20, 2); /* cluster */ + st_dword(buf + SZDIRE * 1 + 24, szb_bit); /* size */ + buf[SZDIRE * 2 + 0] = ET_UPCASE; /* Up-case table entry */ + st_dword(buf + SZDIRE * 2 + 4, sum); /* sum */ + st_dword(buf + SZDIRE * 2 + 20, 2 + tbl[0]); /* cluster */ + st_dword(buf + SZDIRE * 2 + 24, szb_case); /* size */ + sect = b_data + sz_au * (tbl[0] + tbl[1]); nsect = sz_au; /* Start of the root directory and number of sectors */ do { /* Fill root directory sectors */ n = (nsect > sz_buf) ? sz_buf : nsect; if (disk_write(pdrv, buf, sect, n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); @@ -5745,17 +6088,17 @@ FRESULT f_mkfs ( /* Main record (+0) */ mem_set(buf, 0, ss); mem_cpy(buf + BS_JmpBoot, "\xEB\x76\x90" "EXFAT ", 11); /* Boot jump code (x86), OEM name */ - st_dword(buf + BPB_VolOfsEx, b_vol); /* Volume offset in the physical drive [sector] */ - st_dword(buf + BPB_TotSecEx, sz_vol); /* Volume size [sector] */ - st_dword(buf + BPB_FatOfsEx, b_fat - b_vol); /* FAT offset [sector] */ + st_qword(buf + BPB_VolOfsEx, b_vol); /* Volume offset in the physical drive [sector] */ + st_qword(buf + BPB_TotSecEx, sz_vol); /* Volume size [sector] */ + st_dword(buf + BPB_FatOfsEx, (DWORD)(b_fat - b_vol)); /* FAT offset [sector] */ st_dword(buf + BPB_FatSzEx, sz_fat); /* FAT size [sector] */ - st_dword(buf + BPB_DataOfsEx, b_data - b_vol); /* Data offset [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 + tbl[0] + tbl[1]); /* Root dir cluster # */ st_dword(buf + BPB_VolIDEx, GET_FATTIME()); /* 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_SecPerClusEx] = 0, i = au; i >>= 1; buf[BPB_SecPerClusEx]++) ; /* Log2 of cluster size [sector] */ + 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) */ st_word(buf + BS_BootCodeEx, 0xFEEB); /* Boot code (x86) */ @@ -5786,79 +6129,82 @@ FRESULT f_mkfs ( #endif /* FF_FS_EXFAT */ { /* Create an FAT/FAT32 volume */ do { - pau = au; + pau = sz_au; /* Pre-determine number of clusters and FAT sub-type */ - if (fmt == FS_FAT32) { /* FAT32 volume */ - if (pau == 0) { /* au auto-selection */ - n = sz_vol / 0x20000; /* Volume size in unit of 128KS */ + if (fsty == FS_FAT32) { /* FAT32 volume */ + if (pau == 0) { /* AU auto-selection */ + n = (DWORD)sz_vol / 0x20000; /* Volume size in unit of 128KS */ for (i = 0, pau = 1; cst32[i] && cst32[i] <= n; i++, pau <<= 1) ; /* Get from table */ } - n_clst = sz_vol / pau; /* Number of clusters */ + n_clst = (DWORD)sz_vol / pau; /* Number of clusters */ sz_fat = (n_clst * 4 + 8 + ss - 1) / ss; /* FAT size [sector] */ sz_rsv = 32; /* Number of reserved sectors */ sz_dir = 0; /* No static directory */ if (n_clst <= MAX_FAT16 || n_clst > MAX_FAT32) LEAVE_MKFS(FR_MKFS_ABORTED); } else { /* FAT volume */ if (pau == 0) { /* au auto-selection */ - n = sz_vol / 0x1000; /* Volume size in unit of 4KS */ + n = (DWORD)sz_vol / 0x1000; /* Volume size in unit of 4KS */ for (i = 0, pau = 1; cst[i] && cst[i] <= n; i++, pau <<= 1) ; /* Get from table */ } - n_clst = sz_vol / pau; + n_clst = (DWORD)sz_vol / pau; if (n_clst > MAX_FAT12) { n = n_clst * 2 + 4; /* FAT size [byte] */ } else { - fmt = FS_FAT12; + fsty = FS_FAT12; n = (n_clst * 3 + 1) / 2 + 3; /* FAT size [byte] */ } sz_fat = (n + ss - 1) / ss; /* FAT size [sector] */ sz_rsv = 1; /* Number of reserved sectors */ - sz_dir = (DWORD)n_rootdir * SZDIRE / ss; /* Rootdir size [sector] */ + sz_dir = (DWORD)n_root * SZDIRE / ss; /* Root dir size [sector] */ } b_fat = b_vol + sz_rsv; /* FAT base */ - b_data = b_fat + sz_fat * n_fats + sz_dir; /* Data base */ + b_data = b_fat + sz_fat * n_fat + sz_dir; /* Data base */ - /* Align data base to erase block boundary (for flash memory media) */ - n = ((b_data + sz_blk - 1) & ~(sz_blk - 1)) - b_data; /* Next nearest erase block from current data base */ - if (fmt == FS_FAT32) { /* FAT32: Move FAT base */ + /* Align data area to erase block boundary (for flash memory media) */ + n = (DWORD)(((b_data + sz_blk - 1) & ~(sz_blk - 1)) - b_data); /* Sectors to next nearest from current data base */ + if (fsty == FS_FAT32) { /* FAT32: Move FAT */ sz_rsv += n; b_fat += n; - } else { /* FAT: Expand FAT size */ - sz_fat += n / n_fats; + } else { /* FAT: Expand FAT */ + if (n % n_fat) { /* Adjust fractional error if needed */ + n--; sz_rsv++; b_fat++; + } + sz_fat += n / n_fat; } /* Determine number of clusters and final check of validity of the FAT sub-type */ - if (sz_vol < b_data + pau * 16 - b_vol) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume */ - n_clst = (sz_vol - sz_rsv - sz_fat * n_fats - sz_dir) / pau; - if (fmt == FS_FAT32) { - if (n_clst <= MAX_FAT16) { /* Too few clusters for FAT32 */ - if (au == 0 && (au = pau / 2) != 0) continue; /* Adjust cluster size and retry */ + if (sz_vol < b_data + pau * 16 - b_vol) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume? */ + n_clst = ((DWORD)sz_vol - sz_rsv - sz_fat * n_fat - sz_dir) / pau; + if (fsty == FS_FAT32) { + if (n_clst <= MAX_FAT16) { /* Too few clusters for FAT32? */ + if (sz_au == 0 && (sz_au = pau / 2) != 0) continue; /* Adjust cluster size and retry */ LEAVE_MKFS(FR_MKFS_ABORTED); } } - if (fmt == FS_FAT16) { + if (fsty == FS_FAT16) { if (n_clst > MAX_FAT16) { /* Too many clusters for FAT16 */ - if (au == 0 && (pau * 2) <= 64) { - au = pau * 2; continue; /* Adjust cluster size and retry */ + if (sz_au == 0 && (pau * 2) <= 64) { + sz_au = pau * 2; continue; /* Adjust cluster size and retry */ } - if ((opt & FM_FAT32)) { - fmt = FS_FAT32; continue; /* Switch type to FAT32 and retry */ + if ((fsopt & FM_FAT32)) { + fsty = FS_FAT32; continue; /* Switch type to FAT32 and retry */ } - if (au == 0 && (au = pau * 2) <= 128) continue; /* Adjust cluster size and retry */ + if (sz_au == 0 && (sz_au = pau * 2) <= 128) continue; /* Adjust cluster size and retry */ LEAVE_MKFS(FR_MKFS_ABORTED); } if (n_clst <= MAX_FAT12) { /* Too few clusters for FAT16 */ - if (au == 0 && (au = pau * 2) <= 128) continue; /* Adjust cluster size and retry */ + if (sz_au == 0 && (sz_au = pau * 2) <= 128) continue; /* Adjust cluster size and retry */ LEAVE_MKFS(FR_MKFS_ABORTED); } } - if (fmt == FS_FAT12 && n_clst > MAX_FAT12) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too many clusters for FAT12 */ + if (fsty == FS_FAT12 && n_clst > MAX_FAT12) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too many clusters for FAT12 */ /* Ok, it is the valid cluster configuration */ break; } while (1); #if FF_USE_TRIM - tbl[0] = b_vol; tbl[1] = b_vol + sz_vol - 1; /* Inform the device the volume area can be erased */ - disk_ioctl(pdrv, CTRL_TRIM, tbl); + lba[0] = b_vol; lba[1] = b_vol + sz_vol - 1; /* Inform storage device that the volume area may be erased */ + disk_ioctl(pdrv, CTRL_TRIM, lba); #endif /* Create FAT VBR */ mem_set(buf, 0, ss); @@ -5866,18 +6212,18 @@ FRESULT f_mkfs ( st_word(buf + BPB_BytsPerSec, ss); /* Sector size [byte] */ buf[BPB_SecPerClus] = (BYTE)pau; /* Cluster size [sector] */ st_word(buf + BPB_RsvdSecCnt, (WORD)sz_rsv); /* Size of reserved area */ - buf[BPB_NumFATs] = (BYTE)n_fats; /* Number of FATs */ - st_word(buf + BPB_RootEntCnt, (WORD)((fmt == FS_FAT32) ? 0 : n_rootdir)); /* Number of root directory entries */ + buf[BPB_NumFATs] = (BYTE)n_fat; /* Number of FATs */ + st_word(buf + BPB_RootEntCnt, (WORD)((fsty == FS_FAT32) ? 0 : n_root)); /* Number of root directory entries */ if (sz_vol < 0x10000) { st_word(buf + BPB_TotSec16, (WORD)sz_vol); /* Volume size in 16-bit LBA */ } else { - st_dword(buf + BPB_TotSec32, sz_vol); /* Volume size in 32-bit LBA */ + st_dword(buf + BPB_TotSec32, (DWORD)sz_vol); /* Volume size in 32-bit LBA */ } buf[BPB_Media] = 0xF8; /* Media descriptor byte */ st_word(buf + BPB_SecPerTrk, 63); /* Number of sectors per track (for int13) */ st_word(buf + BPB_NumHeads, 255); /* Number of heads (for int13) */ - st_dword(buf + BPB_HiddSec, b_vol); /* Volume offset in the physical drive [sector] */ - if (fmt == FS_FAT32) { + st_dword(buf + BPB_HiddSec, (DWORD)b_vol); /* Volume offset in the physical drive [sector] */ + if (fsty == FS_FAT32) { st_dword(buf + BS_VolID32, GET_FATTIME()); /* VSN */ st_dword(buf + BPB_FATSz32, sz_fat); /* FAT size [sector] */ st_dword(buf + BPB_RootClus32, 2); /* Root directory cluster # (2) */ @@ -5897,7 +6243,7 @@ FRESULT f_mkfs ( if (disk_write(pdrv, buf, b_vol, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it to the VBR sector */ /* Create FSINFO record if needed */ - if (fmt == FS_FAT32) { + if (fsty == FS_FAT32) { disk_write(pdrv, buf, b_vol + 6, 1); /* Write backup VBR (VBR + 6) */ mem_set(buf, 0, ss); st_dword(buf + FSI_LeadSig, 0x41615252); @@ -5910,27 +6256,27 @@ FRESULT f_mkfs ( } /* Initialize FAT area */ - mem_set(buf, 0, (UINT)szb_buf); + mem_set(buf, 0, sz_buf * ss); sect = b_fat; /* FAT start sector */ - for (i = 0; i < n_fats; i++) { /* Initialize FATs each */ - if (fmt == FS_FAT32) { - st_dword(buf + 0, 0xFFFFFFF8); /* Entry 0 */ - st_dword(buf + 4, 0xFFFFFFFF); /* Entry 1 */ - st_dword(buf + 8, 0x0FFFFFFF); /* Entry 2 (root directory) */ + for (i = 0; i < n_fat; i++) { /* Initialize FATs each */ + 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) */ } else { - st_dword(buf + 0, (fmt == FS_FAT12) ? 0xFFFFF8 : 0xFFFFFFF8); /* Entry 0 and 1 */ + st_dword(buf + 0, (fsty == FS_FAT12) ? 0xFFFFF8 : 0xFFFFFFF8); /* FAT[0] and FAT[1] */ } nsect = sz_fat; /* Number of FAT sectors */ 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); - mem_set(buf, 0, ss); + mem_set(buf, 0, ss); /* Rest of FAT all are cleared */ sect += n; nsect -= n; } while (nsect); } /* Initialize root directory (fill with zero) */ - nsect = (fmt == FS_FAT32) ? pau : sz_dir; /* Number of root directory sectors */ + nsect = (fsty == FS_FAT32) ? pau : sz_dir; /* Number of root directory sectors */ do { n = (nsect > sz_buf) ? sz_buf : nsect; if (disk_write(pdrv, buf, sect, (UINT)n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); @@ -5938,44 +6284,36 @@ FRESULT f_mkfs ( } while (nsect); } - /* Determine system ID in the partition table */ - if (FF_FS_EXFAT && fmt == FS_EXFAT) { - sys = 0x07; /* HPFS/NTFS/exFAT */ + /* A FAT volume has been created here */ + + /* Determine system ID in the MBR partition table */ + if (FF_FS_EXFAT && fsty == FS_EXFAT) { + sys = 0x07; /* exFAT */ } else { - if (fmt == FS_FAT32) { + if (fsty == FS_FAT32) { sys = 0x0C; /* FAT32X */ } else { if (sz_vol >= 0x10000) { sys = 0x06; /* FAT12/16 (large) */ } else { - sys = (fmt == FS_FAT16) ? 0x04 : 0x01; /* FAT16 : FAT12 */ + sys = (fsty == FS_FAT16) ? 0x04 : 0x01; /* FAT16 : FAT12 */ } } } /* Update partition information */ - if (FF_MULTI_PARTITION && part != 0) { /* Created in the existing partition */ - /* Update system ID in the partition table */ - if (disk_read(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Read the MBR */ - buf[MBR_Table + (part - 1) * SZ_PTE + PTE_System] = sys; /* Set system ID */ - if (disk_write(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it back to the MBR */ - } else { /* Created as a new single partition */ - if (!(opt & FM_SFD)) { /* Create partition table if in FDISK format */ - mem_set(buf, 0, ss); - st_word(buf + BS_55AA, 0xAA55); /* MBR signature */ - pte = buf + MBR_Table; /* Create partition table for single partition in the drive */ - pte[PTE_Boot] = 0; /* Boot indicator */ - pte[PTE_StHead] = 1; /* Start head */ - pte[PTE_StSec] = 1; /* Start sector */ - pte[PTE_StCyl] = 0; /* Start cylinder */ - pte[PTE_System] = sys; /* System type */ - n = (b_vol + sz_vol) / (63 * 255); /* (End CHS may be invalid) */ - pte[PTE_EdHead] = 254; /* End head */ - pte[PTE_EdSec] = (BYTE)(((n >> 2) & 0xC0) | 63); /* End sector */ - pte[PTE_EdCyl] = (BYTE)n; /* End cylinder */ - st_dword(pte + PTE_StLba, b_vol); /* Start offset in LBA */ - st_dword(pte + PTE_SizLba, sz_vol); /* Size in sectors */ - if (disk_write(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it to the MBR */ + if (FF_MULTI_PARTITION && ipart != 0) { /* Volume is in the existing partition */ + if (!FF_LBA64 || !(fsopt & 0x80)) { + /* Update system ID in the partition table */ + if (disk_read(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Read the MBR */ + buf[MBR_Table + (ipart - 1) * SZ_PTE + PTE_System] = sys; /* Set system ID */ + if (disk_write(pdrv, buf, 0, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it back to the MBR */ + } + } else { /* Volume as a new single partition */ + if (!(fsopt & FM_SFD)) { /* Create partition table if not in SFD */ + lba[0] = sz_vol, lba[1] = 0; + fr = create_partition(pdrv, lba, sys, buf); + if (fr != FR_OK) LEAVE_MKFS(fr); } } @@ -5986,6 +6324,7 @@ FRESULT f_mkfs ( + #if FF_MULTI_PARTITION /*-----------------------------------------------------------------------*/ /* Create Partition Table on the Physical Drive */ @@ -5993,75 +6332,27 @@ FRESULT f_mkfs ( FRESULT f_fdisk ( BYTE pdrv, /* Physical drive number */ - const DWORD* szt, /* Pointer to the size table for each partitions */ + const LBA_t ptbl[], /* Pointer to the size table for each partitions */ void* work /* Pointer to the working buffer (null: use heap memory) */ ) { - UINT i, n, sz_cyl, tot_cyl, b_cyl, e_cyl, p_cyl; - BYTE s_hd, e_hd, *p, *buf = (BYTE*)work; + BYTE *buf = (BYTE*)work; DSTATUS stat; - DWORD sz_disk, sz_part, s_part; - FRESULT res; stat = disk_initialize(pdrv); if (stat & STA_NOINIT) return FR_NOT_READY; if (stat & STA_PROTECT) return FR_WRITE_PROTECTED; - if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_disk)) return FR_DISK_ERR; - - buf = (BYTE*)work; #if FF_USE_LFN == 3 if (!buf) buf = ff_memalloc(FF_MAX_SS); /* Use heap memory for working buffer */ #endif if (!buf) return FR_NOT_ENOUGH_CORE; - /* Determine the CHS without any consideration of the drive geometry */ - for (n = 16; n < 256 && sz_disk / n / 63 > 1024; n *= 2) ; - if (n == 256) n--; - e_hd = (BYTE)(n - 1); - sz_cyl = 63 * n; - tot_cyl = sz_disk / sz_cyl; - - /* Create partition table */ - mem_set(buf, 0, FF_MAX_SS); - p = buf + MBR_Table; b_cyl = 0; - for (i = 0; i < 4; i++, p += SZ_PTE) { - p_cyl = (szt[i] <= 100U) ? (DWORD)tot_cyl * szt[i] / 100 : szt[i] / sz_cyl; /* Number of cylinders */ - if (p_cyl == 0) continue; - s_part = (DWORD)sz_cyl * b_cyl; - sz_part = (DWORD)sz_cyl * p_cyl; - if (i == 0) { /* Exclude first track of cylinder 0 */ - s_hd = 1; - s_part += 63; sz_part -= 63; - } else { - s_hd = 0; - } - e_cyl = b_cyl + p_cyl - 1; /* End cylinder */ - if (e_cyl >= tot_cyl) LEAVE_MKFS(FR_INVALID_PARAMETER); - - /* Set partition table */ - p[1] = s_hd; /* Start head */ - p[2] = (BYTE)(((b_cyl >> 2) & 0xC0) | 1); /* Start sector */ - p[3] = (BYTE)b_cyl; /* Start cylinder */ - p[4] = 0x07; /* System type (temporary setting) */ - p[5] = e_hd; /* End head */ - p[6] = (BYTE)(((e_cyl >> 2) & 0xC0) | 63); /* End sector */ - p[7] = (BYTE)e_cyl; /* End cylinder */ - st_dword(p + 8, s_part); /* Start sector in LBA */ - st_dword(p + 12, sz_part); /* Number of sectors */ - - /* Next partition */ - b_cyl += p_cyl; - } - st_word(p, 0xAA55); /* MBR signature (always at offset 510) */ - - /* Write it to the MBR */ - res = (disk_write(pdrv, buf, 0, 1) == RES_OK && disk_ioctl(pdrv, CTRL_SYNC, 0) == RES_OK) ? FR_OK : FR_DISK_ERR; - LEAVE_MKFS(res); + LEAVE_MKFS(create_partition(pdrv, ptbl, 0x07, buf)); } #endif /* FF_MULTI_PARTITION */ -#endif /* FF_USE_MKFS && !FF_FS_READONLY */ +#endif /* !FF_FS_READONLY && FF_USE_MKFS */ @@ -6075,7 +6366,7 @@ FRESULT f_fdisk ( /*-----------------------------------------------------------------------*/ TCHAR* f_gets ( - TCHAR* buff, /* Pointer to the string buffer to read */ + TCHAR* buff, /* Pointer to the buffer to store read string */ int len, /* Size of string buffer (items) */ FIL* fp /* Pointer to the file object */ ) @@ -6098,49 +6389,51 @@ TCHAR* f_gets ( if (FF_LFN_UNICODE == 2) len -= (FF_STRF_ENCODE == 0) ? 3 : 4; if (FF_LFN_UNICODE == 3) len -= 1; while (nc < len) { -#if FF_STRF_ENCODE == 0 /* Read a character in ANSI/OEM */ - f_read(fp, s, 1, &rc); - if (rc != 1) break; +#if FF_STRF_ENCODE == 0 /* Read a character in ANSI/OEM */ + f_read(fp, s, 1, &rc); /* Get a code unit */ + if (rc != 1) break; /* EOF? */ wc = s[0]; - if (dbc_1st((BYTE)wc)) { - f_read(fp, s, 1, &rc); - if (rc != 1 || !dbc_2nd(s[0])) continue; + if (dbc_1st((BYTE)wc)) { /* DBC 1st byte? */ + f_read(fp, s, 1, &rc); /* Get DBC 2nd byte */ + if (rc != 1 || !dbc_2nd(s[0])) continue; /* Wrong code? */ wc = wc << 8 | s[0]; } - dc = ff_oem2uni(wc, CODEPAGE); + dc = ff_oem2uni(wc, CODEPAGE); /* OEM --> */ if (dc == 0) continue; #elif FF_STRF_ENCODE == 1 || FF_STRF_ENCODE == 2 /* Read a character in UTF-16LE/BE */ - f_read(fp, s, 2, &rc); - if (rc != 2) break; + f_read(fp, s, 2, &rc); /* Get a code unit */ + if (rc != 2) break; /* EOF? */ dc = (FF_STRF_ENCODE == 1) ? ld_word(s) : s[0] << 8 | s[1]; - if (IsSurrogateL(dc)) continue; - if (IsSurrogateH(dc)) { - f_read(fp, s, 2, &rc); - if (rc != 2) break; + if (IsSurrogateL(dc)) continue; /* Broken surrogate pair? */ + if (IsSurrogateH(dc)) { /* High surrogate? */ + f_read(fp, s, 2, &rc); /* Get low surrogate */ + if (rc != 2) break; /* EOF? */ wc = (FF_STRF_ENCODE == 1) ? ld_word(s) : s[0] << 8 | s[1]; - if (!IsSurrogateL(wc)) continue; - dc = ((dc & 0x3FF) + 0x40) << 10 | (wc & 0x3FF); + if (!IsSurrogateL(wc)) continue; /* Broken surrogate pair? */ + dc = ((dc & 0x3FF) + 0x40) << 10 | (wc & 0x3FF); /* Merge surrogate pair */ } #else /* Read a character in UTF-8 */ - f_read(fp, s, 1, &rc); - if (rc != 1) break; + f_read(fp, s, 1, &rc); /* Get a code unit */ + if (rc != 1) break; /* EOF? */ dc = s[0]; - if (dc >= 0x80) { /* Multi-byte character? */ + if (dc >= 0x80) { /* Multi-byte sequence? */ ct = 0; - if ((dc & 0xE0) == 0xC0) { dc &= 0x1F; ct = 1; } /* 2-byte? */ - if ((dc & 0xF0) == 0xE0) { dc &= 0x0F; ct = 2; } /* 3-byte? */ - if ((dc & 0xF8) == 0xF0) { dc &= 0x07; ct = 3; } /* 4-byte? */ + if ((dc & 0xE0) == 0xC0) { dc &= 0x1F; ct = 1; } /* 2-byte sequence? */ + if ((dc & 0xF0) == 0xE0) { dc &= 0x0F; ct = 2; } /* 3-byte sequence? */ + if ((dc & 0xF8) == 0xF0) { dc &= 0x07; ct = 3; } /* 4-byte sequence? */ if (ct == 0) continue; f_read(fp, s, ct, &rc); /* Get trailing bytes */ if (rc != ct) break; rc = 0; - do { /* Merge trailing bytes */ + do { /* Merge the byte sequence */ if ((s[rc] & 0xC0) != 0x80) break; dc = dc << 6 | (s[rc] & 0x3F); } while (++rc < ct); if (rc != ct || dc < 0x80 || IsSurrogate(dc) || dc >= 0x110000) continue; /* Wrong encoding? */ } #endif + /* A code point is avaialble 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 */ if (FF_LFN_UNICODE == 1 && dc >= 0x10000) { /* Out of BMP at UTF-16? */ @@ -6150,22 +6443,22 @@ TCHAR* f_gets ( *p++ = (TCHAR)dc; nc++; if (dc == '\n') break; /* End of line? */ #elif FF_LFN_UNICODE == 2 /* Output it in UTF-8 encoding */ - if (dc < 0x80) { /* 1-byte */ + if (dc < 0x80) { /* Single byte? */ *p++ = (TCHAR)dc; nc++; if (dc == '\n') break; /* End of line? */ } else { - if (dc < 0x800) { /* 2-byte */ + if (dc < 0x800) { /* 2-byte sequence? */ *p++ = (TCHAR)(0xC0 | (dc >> 6 & 0x1F)); *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F)); nc += 2; } else { - if (dc < 0x10000) { /* 3-byte */ + if (dc < 0x10000) { /* 3-byte sequence? */ *p++ = (TCHAR)(0xE0 | (dc >> 12 & 0x0F)); *p++ = (TCHAR)(0x80 | (dc >> 6 & 0x3F)); *p++ = (TCHAR)(0x80 | (dc >> 0 & 0x3F)); nc += 3; - } else { /* 4-byte */ + } else { /* 4-byte sequence? */ *p++ = (TCHAR)(0xF0 | (dc >> 18 & 0x07)); *p++ = (TCHAR)(0x80 | (dc >> 12 & 0x3F)); *p++ = (TCHAR)(0x80 | (dc >> 6 & 0x3F)); @@ -6177,11 +6470,11 @@ TCHAR* f_gets ( #endif } -#else /* Byte-by-byte without any conversion (ANSI/OEM API) */ +#else /* Byte-by-byte read without any conversion (ANSI/OEM API) */ len -= 1; /* Make a room for the terminator */ while (nc < len) { - f_read(fp, s, 1, &rc); - if (rc != 1) break; + f_read(fp, s, 1, &rc); /* Get a byte */ + if (rc != 1) break; /* EOF? */ dc = s[0]; if (FF_USE_STRFUNC == 2 && dc == '\r') continue; *p++ = (TCHAR)dc; nc++; @@ -6199,10 +6492,12 @@ TCHAR* f_gets ( #if !FF_FS_READONLY #include /*-----------------------------------------------------------------------*/ -/* Put a Character to the File */ +/* Put a Character to the File (sub-functions) */ /*-----------------------------------------------------------------------*/ -typedef struct { /* Putchar output buffer and work area */ +/* Putchar 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 */ #if FF_USE_LFN && FF_LFN_UNICODE == 1 @@ -6215,10 +6510,9 @@ typedef struct { /* Putchar output buffer and work area */ } putbuff; -static void putc_bfd ( /* Buffered write with code conversion */ - putbuff* pb, - TCHAR c -) +/* Buffered write with code conversion */ + +static void putc_bfd (putbuff* pb, TCHAR c) { UINT n; int i, nc; @@ -6240,24 +6534,24 @@ static void putc_bfd ( /* Buffered write with code conversion */ #if FF_USE_LFN && FF_LFN_UNICODE #if FF_LFN_UNICODE == 1 /* UTF-16 input */ - if (IsSurrogateH(c)) { - pb->hs = c; return; + if (IsSurrogateH(c)) { /* High surrogate? */ + pb->hs = c; return; /* Save it for next */ } hs = pb->hs; pb->hs = 0; - if (hs != 0) { - if (!IsSurrogateL(c)) hs = 0; + if (hs != 0) { /* There is a leading high surrogate */ + if (!IsSurrogateL(c)) hs = 0; /* Discard high surrogate if not a surrogate pair */ } else { - if (IsSurrogateL(c)) return; + 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? */ pb->bs[pb->wi = 0] = (BYTE)c; /* Save 1st byte */ - if ((BYTE)c < 0x80) break; /* 1-byte? */ - if (((BYTE)c & 0xE0) == 0xC0) pb->ct = 1; /* 2-byte? */ - if (((BYTE)c & 0xF0) == 0xE0) pb->ct = 2; /* 3-byte? */ - if (((BYTE)c & 0xF1) == 0xF0) pb->ct = 3; /* 4-byte? */ + if ((BYTE)c < 0x80) break; /* Single byte? */ + 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 & 0xF1) == 0xF0) pb->ct = 3; /* 4-byte sequence? */ return; } else { /* In the multi-byte sequence */ if (((BYTE)c & 0xC0) != 0x80) { /* Broken sequence? */ @@ -6270,12 +6564,12 @@ static void putc_bfd ( /* Buffered write with code conversion */ } tp = (TCHAR*)pb->bs; dc = tchar2uni(&tp); /* UTF-8 ==> UTF-16 */ - if (dc == 0xFFFFFFFF) return; + if (dc == 0xFFFFFFFF) return; /* Wrong code? */ wc = (WCHAR)dc; hs = (WCHAR)(dc >> 16); #elif FF_LFN_UNICODE == 3 /* UTF-32 input */ - if (IsSurrogate(c) || c >= 0x110000) return; - if (c >= 0x10000) { + if (IsSurrogate(c) || c >= 0x110000) return; /* Discard invalid code */ + if (c >= 0x10000) { /* Out of BMP? */ hs = (WCHAR)(0xD800 | ((c >> 10) - 0x40)); /* Make high surrogate */ wc = 0xDC00 | (c & 0x3FF); /* Make low surrogate */ } else { @@ -6283,25 +6577,26 @@ static void putc_bfd ( /* Buffered write with code conversion */ wc = (WCHAR)c; } #endif + /* A code point in UTF-16 is available in hs and wc */ -#if FF_STRF_ENCODE == 1 /* Write a character in UTF-16LE */ - if (hs != 0) { +#if FF_STRF_ENCODE == 1 /* Write a code point in UTF-16LE */ + if (hs != 0) { /* Surrogate pair? */ st_word(&pb->buf[i], hs); i += 2; nc++; } st_word(&pb->buf[i], wc); i += 2; -#elif FF_STRF_ENCODE == 2 /* Write a character in UTF-16BE */ - if (hs != 0) { +#elif FF_STRF_ENCODE == 2 /* Write a code point in UTF-16BE */ + if (hs != 0) { /* Surrogate pair? */ pb->buf[i++] = (BYTE)(hs >> 8); pb->buf[i++] = (BYTE)hs; nc++; } pb->buf[i++] = (BYTE)(wc >> 8); pb->buf[i++] = (BYTE)wc; -#elif FF_STRF_ENCODE == 3 /* Write it in UTF-8 */ - if (hs != 0) { /* 4-byte */ +#elif FF_STRF_ENCODE == 3 /* Write a code point in UTF-8 */ + if (hs != 0) { /* 4-byte sequence? */ nc += 3; hs = (hs & 0x3FF) + 0x40; pb->buf[i++] = (BYTE)(0xF0 | hs >> 8); @@ -6309,13 +6604,13 @@ static void putc_bfd ( /* Buffered write with code conversion */ pb->buf[i++] = (BYTE)(0x80 | (hs & 3) << 4 | (wc >> 6 & 0x0F)); pb->buf[i++] = (BYTE)(0x80 | (wc & 0x3F)); } else { - if (wc < 0x80) { /* 1-byte */ + if (wc < 0x80) { /* Single byte? */ pb->buf[i++] = (BYTE)wc; } else { - if (wc < 0x800) { /* 2-byte */ + if (wc < 0x800) { /* 2-byte sequence? */ nc += 1; pb->buf[i++] = (BYTE)(0xC0 | wc >> 6); - } else { /* 3-byte */ + } else { /* 3-byte sequence */ nc += 2; pb->buf[i++] = (BYTE)(0xE0 | wc >> 12); pb->buf[i++] = (BYTE)(0x80 | (wc >> 6 & 0x3F)); @@ -6323,7 +6618,7 @@ static void putc_bfd ( /* Buffered write with code conversion */ pb->buf[i++] = (BYTE)(0x80 | (wc & 0x3F)); } } -#else /* Write it in ANSI/OEM */ +#else /* Write a code point in ANSI/OEM */ if (hs != 0) return; wc = ff_uni2oem(wc, CODEPAGE); /* UTF-16 ==> ANSI/OEM */ if (wc == 0) return; @@ -6333,7 +6628,7 @@ static void putc_bfd ( /* Buffered write with code conversion */ pb->buf[i++] = (BYTE)wc; #endif -#else /* ANSI/OEM input (without re-encode) */ +#else /* ANSI/OEM input (without re-encoding) */ pb->buf[i++] = (BYTE)c; #endif @@ -6346,9 +6641,9 @@ static void putc_bfd ( /* Buffered write with code conversion */ } -static int putc_flush ( /* Flush left characters in the buffer */ - putbuff* pb -) +/* Flush remaining characters in the buffer */ + +static int putc_flush (putbuff* pb) { UINT nw; @@ -6359,10 +6654,9 @@ static int putc_flush ( /* Flush left characters in the buffer */ } -static void putc_init ( /* Initialize write buffer */ - putbuff* pb, - FIL* fp -) +/* Initialize write buffer */ + +static void putc_init (putbuff* pb, FIL* fp) { mem_set(pb, 0, sizeof (putbuff)); pb->fp = fp; diff --git a/source/ff.h b/source/ff.h index a0792b2..5225041 100644 --- a/source/ff.h +++ b/source/ff.h @@ -1,8 +1,8 @@ /*----------------------------------------------------------------------------/ -/ FatFs - Generic FAT Filesystem module R0.13c / +/ FatFs - Generic FAT Filesystem module R0.14 / /-----------------------------------------------------------------------------/ / -/ Copyright (C) 2018, ChaN, all right reserved. +/ Copyright (C) 2019, 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,7 +20,7 @@ #ifndef FF_DEFINED -#define FF_DEFINED 86604 /* Revision ID */ +#define FF_DEFINED 86606 /* Revision ID */ #ifdef __cplusplus extern "C" { @@ -45,16 +45,16 @@ typedef unsigned __int64 QWORD; 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 uint16_t WCHAR; /* 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 */ #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 short WCHAR; /* 16-bit unsigned integer */ typedef unsigned long DWORD; /* 32-bit unsigned integer */ +typedef WORD WCHAR; /* UTF-16 character type */ #endif @@ -65,7 +65,7 @@ typedef struct { BYTE pd; /* Physical drive number */ BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */ } PARTITION; -extern PARTITION VolToPart[]; /* Volume - Partition resolution table */ +extern PARTITION VolToPart[]; /* Volume - Partition mapping table */ #endif #if FF_STR_VOLUME_ID @@ -105,15 +105,24 @@ typedef char TCHAR; -/* Type of file size variables */ +/* Type of file size and LBA variables */ #if FF_FS_EXFAT #if FF_INTDEF != 2 #error exFAT feature wants C99 or later #endif typedef QWORD FSIZE_t; +#if FF_LBA64 +typedef QWORD LBA_t; #else +typedef DWORD LBA_t; +#endif +#else +#if FF_LBA64 +#error exFAT needs to be enabled when enable 64-bit LBA +#endif typedef DWORD FSIZE_t; +typedef DWORD LBA_t; #endif @@ -155,14 +164,14 @@ typedef struct { #endif DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */ DWORD fsize; /* Size of an FAT [sectors] */ - DWORD volbase; /* Volume base sector */ - DWORD fatbase; /* FAT base sector */ - DWORD dirbase; /* Root directory base sector/cluster */ - DWORD database; /* Data base sector */ + LBA_t volbase; /* Volume base sector */ + LBA_t fatbase; /* FAT base sector */ + LBA_t dirbase; /* Root directory base sector/cluster */ + LBA_t database; /* Data base sector */ #if FF_FS_EXFAT - DWORD bitbase; /* Allocation bitmap base sector */ + LBA_t bitbase; /* Allocation bitmap base sector */ #endif - DWORD winsect; /* Current sector appearing in the win[] */ + LBA_t winsect; /* Current sector appearing in the win[] */ BYTE win[FF_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */ } FATFS; @@ -199,9 +208,9 @@ typedef struct { BYTE err; /* Abort flag (error code) */ FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */ DWORD clust; /* Current cluster of fpter (invalid when fptr is 0) */ - DWORD sect; /* Sector number appearing in buf[] (0:invalid) */ + LBA_t sect; /* Sector number appearing in buf[] (0:invalid) */ #if !FF_FS_READONLY - DWORD dir_sect; /* Sector number containing the directory entry (not used at exFAT) */ + LBA_t dir_sect; /* Sector number containing the directory entry (not used at exFAT) */ BYTE* dir_ptr; /* Pointer to the directory entry in the win[] (not used at exFAT) */ #endif #if FF_USE_FASTSEEK @@ -220,7 +229,7 @@ typedef struct { FFOBJID obj; /* Object identifier */ DWORD dptr; /* Current read/write offset */ DWORD clust; /* Current cluster */ - DWORD sect; /* Current sector (0:Read operation has terminated) */ + LBA_t sect; /* Current sector (0:Read operation has terminated) */ BYTE* dir; /* Pointer to the directory item in the win[] */ BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */ #if FF_USE_LFN @@ -250,6 +259,18 @@ typedef struct { +/* Format parameter structure (MKFS_PARM) */ + +typedef struct { + BYTE fmt; /* Format option (FM_FAT, FM_FAT32, FM_EXFAT and FM_SFD) */ + BYTE n_fat; /* Number of FATs */ + UINT align; /* Data area alignment (sector) */ + UINT n_root; /* Number of root directory entries */ + DWORD au_size; /* Cluster size (byte) */ +} MKFS_PARM; + + + /* File function return code (FRESULT) */ typedef enum { @@ -305,10 +326,10 @@ FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get numbe FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */ FRESULT f_setlabel (const TCHAR* label); /* Set volume label */ FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */ -FRESULT f_expand (FIL* fp, FSIZE_t szf, BYTE opt); /* Allocate a contiguous block to the file */ +FRESULT f_expand (FIL* fp, FSIZE_t fsz, BYTE opt); /* Allocate a contiguous block to the file */ FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */ -FRESULT f_mkfs (const TCHAR* path, BYTE opt, DWORD au, void* work, UINT len); /* Create a FAT volume */ -FRESULT f_fdisk (BYTE pdrv, const DWORD* szt, void* work); /* Divide a physical drive into some partitions */ +FRESULT f_mkfs (const TCHAR* path, const MKFS_PARM* opt, void* work, UINT len); /* Create a FAT volume */ +FRESULT f_fdisk (BYTE pdrv, const LBA_t ptbl[], void* work); /* Divide a physical drive into some partitions */ FRESULT f_setcp (WORD cp); /* Set current code page */ int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */ int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */ diff --git a/source/ffconf.h b/source/ffconf.h index 5bd2f21..bdbe320 100644 --- a/source/ffconf.h +++ b/source/ffconf.h @@ -2,7 +2,7 @@ / FatFs Functional Configurations /---------------------------------------------------------------------------*/ -#define FFCONF_DEF 86604 /* Revision ID */ +#define FFCONF_DEF 86606 /* Revision ID */ /*---------------------------------------------------------------------------/ / Function Configurations @@ -102,7 +102,7 @@ /* 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. / @@ -110,11 +110,11 @@ / 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 255 to fully support LFN +/ be in range of 12 to 255. It is recommended to be set it 255 to fully support 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 -/ ff_memfree() in ffsystem.c, need to be added to the project. */ +/ ff_memfree() exemplified in ffsystem.c, need to be added to the project. */ #define FF_LFN_UNICODE 0 @@ -200,24 +200,22 @@ / GET_SECTOR_SIZE command. */ +#define FF_LBA64 0 +/* This option switches support for 64-bit LBA. (0:Disable or 1:Enable) +/ To enable the 64-bit LBA, also exFAT needs to be enabled. (FF_FS_EXFAT == 1) */ + + +#define FF_MIN_GPT 0x100000000 +/* Minimum number of sectors to switch GPT format to create partition in f_mkfs and +/ f_fdisk function. 0x100000000 max. 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. */ -#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 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. -/ bit0=1: Do not trust free cluster count in the FSINFO. -/ bit1=0: Use last allocated cluster number in the FSINFO if available. -/ bit1=1: Do not trust last allocated cluster number in the FSINFO. -*/ - - /*---------------------------------------------------------------------------/ / System Configurations @@ -239,7 +237,7 @@ #define FF_FS_NORTC 0 #define FF_NORTC_MON 1 #define FF_NORTC_MDAY 1 -#define FF_NORTC_YEAR 2018 +#define FF_NORTC_YEAR 2019 /* The option FF_FS_NORTC switches timestamp functiton. If the system does not have / any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable / the timestamp function. Every object modified by FatFs will have a fixed timestamp @@ -247,7 +245,19 @@ / 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, / FF_NORTC_MDAY and FF_NORTC_YEAR have no effect. -/ These options have no effect at read-only configuration (FF_FS_READONLY = 1). */ +/ 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 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. +/ bit0=1: Do not trust free cluster count in the FSINFO. +/ bit1=0: Use last allocated cluster number in the FSINFO if available. +/ bit1=1: Do not trust last allocated cluster number in the FSINFO. +*/ #define FF_FS_LOCK 0 diff --git a/source/ffunicode.c b/source/ffunicode.c index 349901b..a69b24c 100644 --- a/source/ffunicode.c +++ b/source/ffunicode.c @@ -1,5 +1,5 @@ /*------------------------------------------------------------------------*/ -/* Unicode handling functions for FatFs R0.13c */ +/* Unicode handling functions for FatFs R0.13+ */ /*------------------------------------------------------------------------*/ /* This module will occupy a huge memory in the .const section when the / / FatFs is configured for LFN with DBCS. If the system has any Unicode / @@ -7,7 +7,7 @@ / that function to avoid silly memory consumption. / /-------------------------------------------------------------------------*/ /* -/ Copyright (C) 2018, ChaN, all right reserved. +/ Copyright (C) 2014, 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 @@ -25,11 +25,7 @@ #include "ff.h" -#if FF_USE_LFN /* This module will be blanked at non-LFN configuration */ - -#if FF_DEFINED != 86604 /* Revision ID */ -#error Wrong include file (ff.h). -#endif +#if FF_USE_LFN /* This module will be blanked if non-LFN configuration */ #define MERGE2(a, b) a ## b #define CVTBL(tbl, cp) MERGE2(tbl, cp) @@ -15245,7 +15241,7 @@ WCHAR ff_uni2oem ( /* Returns OEM code character, zero on error */ return c; } -WCHAR ff_oem2uni ( /* Returns Unicode character, zero on error */ +WCHAR ff_oem2uni ( /* Returns Unicode character in UTF-16, zero on error */ WCHAR oem, /* OEM code to be converted */ WORD cp /* Code page for the conversion */ ) @@ -15312,7 +15308,7 @@ WCHAR ff_uni2oem ( /* Returns OEM code character, zero on error */ } -WCHAR ff_oem2uni ( /* Returns Unicode character, zero on error */ +WCHAR ff_oem2uni ( /* Returns Unicode character in UTF-16, zero on error */ WCHAR oem, /* OEM code to be converted */ WORD cp /* Code page for the conversion */ ) @@ -15411,7 +15407,7 @@ WCHAR ff_uni2oem ( /* Returns OEM code character, zero on error */ } -WCHAR ff_oem2uni ( /* Returns Unicode character, zero on error */ +WCHAR ff_oem2uni ( /* Returns Unicode character in UTF-16, zero on error */ WCHAR oem, /* OEM code to be converted (DBC if >=0x100) */ WORD cp /* Code page for the conversion */ )