mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-07-01 22:08:55 -07:00
4d6b170769
* Fixing compiler warnings with -Wextra * More warnings suppression, WIP * Even more warning fixes * Added new lines at end of text files. * Padding fix * Additional fixes to warnings on different build configurations; added -Wextra to default build pipeline * Fixes for Secplus v1 * -additional warnings * +-Wredundant-decls fixes * FuriHal: print stack overflow task name in console * FuriHal: add missing include Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
6141 lines
201 KiB
C
6141 lines
201 KiB
C
/*----------------------------------------------------------------------------/
|
|
/ FatFs - Generic FAT file system module R0.12c /
|
|
/-----------------------------------------------------------------------------/
|
|
/
|
|
/ Copyright (C) 2017, ChaN, all right reserved.
|
|
/
|
|
/ FatFs module is an open source software. Redistribution and use of FatFs in
|
|
/ source and binary forms, with or without modification, are permitted provided
|
|
/ that the following condition is met:
|
|
/
|
|
/ 1. Redistributions of source code must retain the above copyright notice,
|
|
/ this condition and the following disclaimer.
|
|
/
|
|
/ This software is provided by the copyright holder and contributors "AS IS"
|
|
/ and any warranties related to this software are DISCLAIMED.
|
|
/ The copyright owner or contributors be NOT LIABLE for any damages caused
|
|
/ by use of this software.
|
|
/----------------------------------------------------------------------------*/
|
|
|
|
|
|
#include "ff.h" /* Declarations of FatFs API */
|
|
#include "diskio.h" /* Declarations of device I/O functions */
|
|
|
|
|
|
/*--------------------------------------------------------------------------
|
|
|
|
Module Private Definitions
|
|
|
|
---------------------------------------------------------------------------*/
|
|
|
|
#if _FATFS != 68300 /* Revision ID */
|
|
#error Wrong include file (ff.h).
|
|
#endif
|
|
|
|
|
|
/* DBCS code ranges and SBCS upper conversion tables */
|
|
|
|
#if _CODE_PAGE == 932 /* Japanese Shift-JIS */
|
|
#define _DF1S 0x81 /* DBC 1st byte range 1 start */
|
|
#define _DF1E 0x9F /* DBC 1st byte range 1 end */
|
|
#define _DF2S 0xE0 /* DBC 1st byte range 2 start */
|
|
#define _DF2E 0xFC /* DBC 1st byte range 2 end */
|
|
#define _DS1S 0x40 /* DBC 2nd byte range 1 start */
|
|
#define _DS1E 0x7E /* DBC 2nd byte range 1 end */
|
|
#define _DS2S 0x80 /* DBC 2nd byte range 2 start */
|
|
#define _DS2E 0xFC /* DBC 2nd byte range 2 end */
|
|
|
|
#elif _CODE_PAGE == 936 /* Simplified Chinese GBK */
|
|
#define _DF1S 0x81
|
|
#define _DF1E 0xFE
|
|
#define _DS1S 0x40
|
|
#define _DS1E 0x7E
|
|
#define _DS2S 0x80
|
|
#define _DS2E 0xFE
|
|
|
|
#elif _CODE_PAGE == 949 /* Korean */
|
|
#define _DF1S 0x81
|
|
#define _DF1E 0xFE
|
|
#define _DS1S 0x41
|
|
#define _DS1E 0x5A
|
|
#define _DS2S 0x61
|
|
#define _DS2E 0x7A
|
|
#define _DS3S 0x81
|
|
#define _DS3E 0xFE
|
|
|
|
#elif _CODE_PAGE == 950 /* Traditional Chinese Big5 */
|
|
#define _DF1S 0x81
|
|
#define _DF1E 0xFE
|
|
#define _DS1S 0x40
|
|
#define _DS1E 0x7E
|
|
#define _DS2S 0xA1
|
|
#define _DS2E 0xFE
|
|
|
|
#elif _CODE_PAGE == 437 /* U.S. */
|
|
#define _DF1S 0
|
|
#define _EXCVT {0x80,0x9A,0x45,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \
|
|
0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
|
0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
|
|
0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
|
0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD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
#define _DF1S 0
|
|
#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
|
|
0x90,0x92,0x92,0x93,0x94,0x95,0x96,0x97,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, \
|
|
0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0xAA,0x92,0x93,0x94,0x95,0x96, \
|
|
0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
|
0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
|
0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
|
0x97,0xEA,0xEB,0xEC,0xE4,0xED,0xEE,0xEF,0xF5,0xF0,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
|
|
0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
|
|
|
#elif _CODE_PAGE == 771 /* KBL */
|
|
#define _DF1S 0
|
|
#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
|
|
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
|
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
|
|
0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD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F6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
|
|
|
#elif _CODE_PAGE == 850 /* Latin 1 */
|
|
#define _DF1S 0
|
|
#define _EXCVT {0x43,0x55,0x45,0x41,0x41,0x41,0x41,0x43,0x45,0x45,0x45,0x49,0x49,0x49,0x41,0x41, \
|
|
0x45,0x92,0x92,0x4F,0x4F,0x4F,0x55,0x55,0x59,0x4F,0x55,0x4F,0x9C,0x4F,0x9E,0x9F, \
|
|
0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
|
|
0xB0,0xB1,0xB2,0xB3,0xB4,0x41,0x41,0x41,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
|
0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0x41,0x41,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
|
0xD1,0xD1,0x45,0x45,0x45,0x49,0x49,0x49,0x49,0xD9,0xDA,0xDB,0xDC,0xDD,0x49,0xDF, \
|
|
0x4F,0xE1,0x4F,0x4F,0x4F,0x4F,0xE6,0xE8,0xE8,0x55,0x55,0x55,0x59,0x59,0xEE,0xEF, \
|
|
0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
|
|
|
#elif _CODE_PAGE == 852 /* Latin 2 */
|
|
#define _DF1S 0
|
|
#define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xDE,0x8F,0x80,0x9D,0xD3,0x8A,0x8A,0xD7,0x8D,0x8E,0x8F, \
|
|
0x90,0x91,0x91,0xE2,0x99,0x95,0x95,0x97,0x97,0x99,0x9A,0x9B,0x9B,0x9D,0x9E,0xAC, \
|
|
0xB5,0xD6,0xE0,0xE9,0xA4,0xA4,0xA6,0xA6,0xA8,0xA8,0xAA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GE == 863 /* Canadian-French */
|
|
#define _DF1S 0
|
|
#define _EXCVT {0x43,0x55,0x45,0x41,0x41,0x41,0x86,0x43,0x45,0x45,0x45,0x49,0x49,0x8D,0x41,0x8F, \
|
|
0x45,0x45,0x45,0x4F,0x45,0x49,0x55,0x55,0x98,0x4F,0x55,0x9B,0x9C,0x55,0x55,0x9F, \
|
|
0xA0,0xA1,0x4F,0x55,0xA4,0xA5,0xA6,0xA7,0x49,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
|
|
0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
|
0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
|
0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
|
0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, \
|
|
0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
|
|
|
#elif _CODE_PAGE == 864 /* Arabic */
|
|
#define _DF1S 0
|
|
#define _EXCVT {0x80,0x9A,0x45,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F, \
|
|
0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
|
0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
|
|
0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0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, \
|
|
0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
|
|
|
#elif _CODE_PAGE == 866 /* Russian */
|
|
#define _DF1S 0
|
|
#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
|
|
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
|
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
|
|
0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
|
0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
|
0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
|
|
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
|
|
0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
|
|
|
|
#elif _CODE_PAGE == 869 /* Greek 2 */
|
|
#define _DF1S 0
|
|
#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, \
|
|
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x86,0x9C,0x8D,0x8F,0x90, \
|
|
0x91,0x90,0x92,0x95,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, \
|
|
0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
|
|
0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, \
|
|
0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xA4,0xA5,0xA6,0xD9,0xDA,0xDB,0xDC,0xA7,0xA8,0xDF, \
|
|
0xA9,0xAA,0xAC,0xAD,0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xCF,0xCF,0xD0,0xEF, \
|
|
0xF0,0xF1,0xD1,0xD2,0xD3,0xF5,0xD4,0xF7,0xF8,0xF9,0xD5,0x96,0x95,0x98,0xFE,0xFF}
|
|
|
|
#elif _CODE_PAGE == 1 /* ASCII (for only non-LFN cfg) */
|
|
#if _USE_LFN != 0
|
|
#error Cannot enable LFN without valid code page.
|
|
#endif
|
|
#define _DF1S 0
|
|
|
|
#else
|
|
#error Unknown code page
|
|
|
|
#endif
|
|
|
|
|
|
/* Character code support macros */
|
|
#define IsUpper(c) (((c)>='A')&&((c)<='Z'))
|
|
#define IsLower(c) (((c)>='a')&&((c)<='z'))
|
|
#define IsDigit(c) (((c)>='0')&&((c)<='9'))
|
|
|
|
#if _DF1S != 0 /* Code page is DBCS */
|
|
|
|
#ifdef _DF2S /* Two 1st byte areas */
|
|
#define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))
|
|
#else /* One 1st byte area */
|
|
#define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)
|
|
#endif
|
|
|
|
#ifdef _DS3S /* Three 2nd byte areas */
|
|
#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))
|
|
#else /* Two 2nd byte areas */
|
|
#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))
|
|
#endif
|
|
|
|
#else /* Code page is SBCS */
|
|
|
|
#define IsDBCS1(c) 0
|
|
#define IsDBCS2(c) 0
|
|
|
|
#endif /* _DF1S */
|
|
|
|
|
|
/* Additional file attribute bits for internal use */
|
|
#define AM_VOL 0x08 /* Volume label */
|
|
#define AM_LFN 0x0F /* LFN entry */
|
|
#define AM_MASK 0x3F /* Mask of defined bits */
|
|
|
|
|
|
/* Additional file access control and file status flags for internal use */
|
|
#define FA_SEEKEND 0x20 /* Seek to end of the file on file open */
|
|
#define FA_MODIFIED 0x40 /* File has been modified */
|
|
#define FA_DIRTY 0x80 /* FIL.buf[] needs to be written-back */
|
|
|
|
|
|
/* Name status flags in fn[] */
|
|
#define NSFLAG 11 /* Index of the name status byte */
|
|
#define NS_LOSS 0x01 /* Out of 8.3 format */
|
|
#define NS_LFN 0x02 /* Force to create LFN entry */
|
|
#define NS_LAST 0x04 /* Last segment */
|
|
#define NS_BODY 0x08 /* Lower case flag (body) */
|
|
#define NS_EXT 0x10 /* Lower case flag (ext) */
|
|
#define NS_DOT 0x20 /* Dot entry */
|
|
#define NS_NOLFN 0x40 /* Do not find LFN */
|
|
#define NS_NONAME 0x80 /* Not followed */
|
|
|
|
|
|
/* Limits and boundaries */
|
|
#define MAX_DIR 0x200000 /* Max size of FAT directory */
|
|
#define MAX_DIR_EX 0x10000000 /* Max size of exFAT directory */
|
|
#define MAX_FAT12 0xFF5 /* Max FAT12 clusters (differs from specs, but correct for real DOS/Windows behavior) */
|
|
#define MAX_FAT16 0xFFF5 /* Max FAT16 clusters (differs from specs, but correct for real DOS/Windows behavior) */
|
|
#define MAX_FAT32 0x0FFFFFF5 /* Max FAT32 clusters (not specified, practical limit) */
|
|
#define MAX_EXFAT 0x7FFFFFFD /* Max exFAT clusters (differs from specs, implementation limit) */
|
|
|
|
|
|
/* FatFs refers the FAT structure as simple byte array instead of structure member
|
|
/ because the C structure is not binary compatible between different platforms */
|
|
|
|
#define BS_JmpBoot 0 /* x86 jump instruction (3-byte) */
|
|
#define BS_OEMName 3 /* OEM name (8-byte) */
|
|
#define BPB_BytsPerSec 11 /* Sector size [byte] (WORD) */
|
|
#define BPB_SecPerClus 13 /* Cluster size [sector] (BYTE) */
|
|
#define BPB_RsvdSecCnt 14 /* Size of reserved area [sector] (WORD) */
|
|
#define BPB_NumFATs 16 /* Number of FATs (BYTE) */
|
|
#define BPB_RootEntCnt 17 /* Size of root directory area for FAT12/16 [entry] (WORD) */
|
|
#define BPB_TotSec16 19 /* Volume size (16-bit) [sector] (WORD) */
|
|
#define BPB_Media 21 /* Media descriptor byte (BYTE) */
|
|
#define BPB_FATSz16 22 /* FAT size (16-bit) [sector] (WORD) */
|
|
#define BPB_SecPerTrk 24 /* Track size for int13h [sector] (WORD) */
|
|
#define BPB_NumHeads 26 /* Number of heads for int13h (WORD) */
|
|
#define BPB_HiddSec 28 /* Volume offset from top of the drive (DWORD) */
|
|
#define BPB_TotSec32 32 /* Volume size (32-bit) [sector] (DWORD) */
|
|
#define BS_DrvNum 36 /* Physical drive number for int13h (BYTE) */
|
|
#define BS_NTres 37 /* Error flag (BYTE) */
|
|
#define BS_BootSig 38 /* Extended boot signature (BYTE) */
|
|
#define BS_VolID 39 /* Volume serial number (DWORD) */
|
|
#define BS_VolLab 43 /* Volume label string (8-byte) */
|
|
#define BS_FilSysType 54 /* File system type string (8-byte) */
|
|
#define BS_BootCode 62 /* Boot code (448-byte) */
|
|
#define BS_55AA 510 /* Signature word (WORD) */
|
|
|
|
#define BPB_FATSz32 36 /* FAT32: FAT size [sector] (DWORD) */
|
|
#define BPB_ExtFlags32 40 /* FAT32: Extended flags (WORD) */
|
|
#define BPB_FSVer32 42 /* FAT32: File system version (WORD) */
|
|
#define BPB_RootClus32 44 /* FAT32: Root directory cluster (DWORD) */
|
|
#define BPB_FSInfo32 48 /* FAT32: Offset of FSINFO sector (WORD) */
|
|
#define BPB_BkBootSec32 50 /* FAT32: Offset of backup boot sector (WORD) */
|
|
#define BS_DrvNum32 64 /* FAT32: Physical drive number for int13h (BYTE) */
|
|
#define BS_NTres32 65 /* FAT32: Error flag (BYTE) */
|
|
#define BS_BootSig32 66 /* FAT32: Extended boot signature (BYTE) */
|
|
#define BS_VolID32 67 /* FAT32: Volume serial number (DWORD) */
|
|
#define BS_VolLab32 71 /* FAT32: Volume label string (8-byte) */
|
|
#define BS_FilSysType32 82 /* FAT32: File system type string (8-byte) */
|
|
#define BS_BootCode32 90 /* FAT32: Boot code (420-byte) */
|
|
|
|
#define BPB_ZeroedEx 11 /* exFAT: MBZ field (53-byte) */
|
|
#define BPB_VolOfsEx 64 /* exFAT: Volume offset from top of the drive [sector] (QWORD) */
|
|
#define BPB_TotSecEx 72 /* exFAT: Volume size [sector] (QWORD) */
|
|
#define BPB_FatOfsEx 80 /* exFAT: FAT offset from top of the volume [sector] (DWORD) */
|
|
#define BPB_FatSzEx 84 /* exFAT: FAT size [sector] (DWORD) */
|
|
#define BPB_DataOfsEx 88 /* exFAT: Data offset from top of the volume [sector] (DWORD) */
|
|
#define BPB_NumClusEx 92 /* exFAT: Number of clusters (DWORD) */
|
|
#define BPB_RootClusEx 96 /* exFAT: Root directory start cluster (DWORD) */
|
|
#define BPB_VolIDEx 100 /* exFAT: Volume serial number (DWORD) */
|
|
#define BPB_FSVerEx 104 /* exFAT: File system version (WORD) */
|
|
#define BPB_VolFlagEx 106 /* exFAT: Volume flags (BYTE) */
|
|
#define BPB_ActFatEx 107 /* exFAT: Active FAT flags (BYTE) */
|
|
#define BPB_BytsPerSecEx 108 /* exFAT: Log2 of sector size in unit of byte (BYTE) */
|
|
#define BPB_SecPerClusEx 109 /* exFAT: Log2 of cluster size in unit of sector (BYTE) */
|
|
#define BPB_NumFATsEx 110 /* exFAT: Number of FATs (BYTE) */
|
|
#define BPB_DrvNumEx 111 /* exFAT: Physical drive number for int13h (BYTE) */
|
|
#define BPB_PercInUseEx 112 /* exFAT: Percent in use (BYTE) */
|
|
#define BPB_RsvdEx 113 /* exFAT: Reserved (7-byte) */
|
|
#define BS_BootCodeEx 120 /* exFAT: Boot code (390-byte) */
|
|
|
|
#define DIR_Name 0 /* Short file name (11-byte) */
|
|
#define DIR_Attr 11 /* Attribute (BYTE) */
|
|
#define DIR_NTres 12 /* Lower case flag (BYTE) */
|
|
#define DIR_CrtTime10 13 /* Created time sub-second (BYTE) */
|
|
#define DIR_CrtTime 14 /* Created time (DWORD) */
|
|
#define DIR_LstAccDate 18 /* Last accessed date (WORD) */
|
|
#define DIR_FstClusHI 20 /* Higher 16-bit of first cluster (WORD) */
|
|
#define DIR_ModTime 22 /* Modified time (DWORD) */
|
|
#define DIR_FstClusLO 26 /* Lower 16-bit of first cluster (WORD) */
|
|
#define DIR_FileSize 28 /* File size (DWORD) */
|
|
#define LDIR_Ord 0 /* LFN: LFN order and LLE flag (BYTE) */
|
|
#define LDIR_Attr 11 /* LFN: LFN attribute (BYTE) */
|
|
#define LDIR_Type 12 /* LFN: Entry type (BYTE) */
|
|
#define LDIR_Chksum 13 /* LFN: Checksum of the SFN (BYTE) */
|
|
#define LDIR_FstClusLO 26 /* LFN: MBZ field (WORD) */
|
|
#define XDIR_Type 0 /* exFAT: Type of exFAT directory entry (BYTE) */
|
|
#define XDIR_NumLabel 1 /* exFAT: Number of volume label characters (BYTE) */
|
|
#define XDIR_Label 2 /* exFAT: Volume label (11-WORD) */
|
|
#define XDIR_CaseSum 4 /* exFAT: Sum of case conversion table (DWORD) */
|
|
#define XDIR_NumSec 1 /* exFAT: Number of secondary entries (BYTE) */
|
|
#define XDIR_SetSum 2 /* exFAT: Sum of the set of directory entries (WORD) */
|
|
#define XDIR_Attr 4 /* exFAT: File attribute (WORD) */
|
|
#define XDIR_CrtTime 8 /* exFAT: Created time (DWORD) */
|
|
#define XDIR_ModTime 12 /* exFAT: Modified time (DWORD) */
|
|
#define XDIR_AccTime 16 /* exFAT: Last accessed time (DWORD) */
|
|
#define XDIR_CrtTime10 20 /* exFAT: Created time subsecond (BYTE) */
|
|
#define XDIR_ModTime10 21 /* exFAT: Modified time subsecond (BYTE) */
|
|
#define XDIR_CrtTZ 22 /* exFAT: Created timezone (BYTE) */
|
|
#define XDIR_ModTZ 23 /* exFAT: Modified timezone (BYTE) */
|
|
#define XDIR_AccTZ 24 /* exFAT: Last accessed timezone (BYTE) */
|
|
#define XDIR_GenFlags 33 /* exFAT: General secondary flags (WORD) */
|
|
#define XDIR_NumName 35 /* exFAT: Number of file name characters (BYTE) */
|
|
#define XDIR_NameHash 36 /* exFAT: Hash of file name (WORD) */
|
|
#define XDIR_ValidFileSize 40 /* exFAT: Valid file size (QWORD) */
|
|
#define XDIR_FstClus 52 /* exFAT: First cluster of the file data (DWORD) */
|
|
#define XDIR_FileSize 56 /* exFAT: File/Directory size (QWORD) */
|
|
|
|
#define SZDIRE 32 /* Size of a directory entry */
|
|
#define DDEM 0xE5 /* Deleted directory entry mark set to DIR_Name[0] */
|
|
#define RDDEM 0x05 /* Replacement of the character collides with DDEM */
|
|
#define LLEF 0x40 /* Last long entry flag in LDIR_Ord */
|
|
|
|
#define FSI_LeadSig 0 /* FAT32 FSI: Leading signature (DWORD) */
|
|
#define FSI_StrucSig 484 /* FAT32 FSI: Structure signature (DWORD) */
|
|
#define FSI_Free_Count 488 /* FAT32 FSI: Number of free clusters (DWORD) */
|
|
#define FSI_Nxt_Free 492 /* FAT32 FSI: Last allocated cluster (DWORD) */
|
|
|
|
#define MBR_Table 446 /* MBR: Offset of partition table in the MBR */
|
|
#define SZ_PTE 16 /* MBR: Size of a partition table entry */
|
|
#define PTE_Boot 0 /* MBR PTE: Boot indicator */
|
|
#define PTE_StHead 1 /* MBR PTE: Start head */
|
|
#define PTE_StSec 2 /* MBR PTE: Start sector */
|
|
#define PTE_StCyl 3 /* MBR PTE: Start cylinder */
|
|
#define PTE_System 4 /* MBR PTE: System ID */
|
|
#define PTE_EdHead 5 /* MBR PTE: End head */
|
|
#define PTE_EdSec 6 /* MBR PTE: End sector */
|
|
#define PTE_EdCyl 7 /* MBR PTE: End cylinder */
|
|
#define PTE_StLba 8 /* MBR PTE: Start in LBA */
|
|
#define PTE_SizLba 12 /* MBR PTE: Size in LBA */
|
|
|
|
|
|
/* Post process after fatal error on file operation */
|
|
#define ABORT(fs, res) { fp->err = (BYTE)(res); LEAVE_FF(fs, res); }
|
|
|
|
|
|
/* Reentrancy related */
|
|
#if _FS_REENTRANT
|
|
#if _USE_LFN == 1
|
|
#error Static LFN work area cannot be used at thread-safe configuration
|
|
#endif
|
|
#define ENTER_FF(fs) { if (!lock_fs(fs)) return FR_TIMEOUT; }
|
|
#define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; }
|
|
#else
|
|
#define ENTER_FF(fs)
|
|
#define LEAVE_FF(fs, res) return res
|
|
#endif
|
|
|
|
|
|
/* Definitions of volume - partition conversion */
|
|
#if _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 LD2PT(vol) 0 /* Find first valid partition or in SFD */
|
|
#endif
|
|
|
|
|
|
/* Definitions of sector size */
|
|
#if (_MAX_SS < _MIN_SS) || (_MAX_SS != 512 && _MAX_SS != 1024 && _MAX_SS != 2048 && _MAX_SS != 4096) || (_MIN_SS != 512 && _MIN_SS != 1024 && _MIN_SS != 2048 && _MIN_SS != 4096)
|
|
#error Wrong sector size configuration
|
|
#endif
|
|
#if _MAX_SS == _MIN_SS
|
|
#define SS(fs) ((UINT)_MAX_SS) /* Fixed sector size */
|
|
#else
|
|
#define SS(fs) ((fs)->ssize) /* Variable sector size */
|
|
#endif
|
|
|
|
|
|
/* Timestamp */
|
|
#if _FS_NORTC == 1
|
|
#if _NORTC_YEAR < 1980 || _NORTC_YEAR > 2107 || _NORTC_MON < 1 || _NORTC_MON > 12 || _NORTC_MDAY < 1 || _NORTC_MDAY > 31
|
|
#error Invalid _FS_NORTC settings
|
|
#endif
|
|
#define GET_FATTIME() ((DWORD)(_NORTC_YEAR - 1980) << 25 | (DWORD)_NORTC_MON << 21 | (DWORD)_NORTC_MDAY << 16)
|
|
#else
|
|
#define GET_FATTIME() get_fattime()
|
|
#endif
|
|
|
|
|
|
/* File lock controls */
|
|
#if _FS_LOCK != 0
|
|
#if _FS_READONLY
|
|
#error _FS_LOCK must be 0 at read-only configuration
|
|
#endif
|
|
typedef struct {
|
|
FATFS *fs; /* Object ID 1, volume (NULL:blank entry) */
|
|
DWORD clu; /* Object ID 2, containing directory (0:root) */
|
|
DWORD ofs; /* Object ID 3, offset in the directory */
|
|
WORD ctr; /* Object open counter, 0:none, 0x01..0xFF:read mode open count, 0x100:write mode */
|
|
} FILESEM;
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------
|
|
|
|
Module Private Work Area
|
|
|
|
---------------------------------------------------------------------------*/
|
|
|
|
/* Remark: Variables defined here without initial value shall be guaranteed
|
|
/ zero/null at start-up. If not, the linker option or start-up routine is
|
|
/ not compliance with C standard. */
|
|
|
|
#if _VOLUMES < 1 || _VOLUMES > 10
|
|
#error Wrong _VOLUMES setting
|
|
#endif
|
|
static FATFS *FatFs[_VOLUMES]; /* Pointer to the file system objects (logical drives) */
|
|
static WORD Fsid; /* File system mount ID */
|
|
|
|
#if _FS_RPATH != 0 && _VOLUMES >= 2
|
|
static BYTE CurrVol; /* Current drive */
|
|
#endif
|
|
|
|
#if _FS_LOCK != 0
|
|
static FILESEM Files[_FS_LOCK]; /* Open object lock semaphores */
|
|
#endif
|
|
|
|
#if _USE_LFN == 0 /* Non-LFN configuration */
|
|
#define DEF_NAMBUF
|
|
#define INIT_NAMBUF(fs)
|
|
#define FREE_NAMBUF()
|
|
|
|
#else /* LFN configuration */
|
|
#if _MAX_LFN < 12 || _MAX_LFN > 255
|
|
#error Wrong _MAX_LFN value
|
|
#endif
|
|
#define MAXDIRB(nc) ((nc + 44U) / 15 * SZDIRE)
|
|
|
|
#if _USE_LFN == 1 /* LFN enabled with static working buffer */
|
|
#if _FS_EXFAT
|
|
static BYTE DirBuf[MAXDIRB(_MAX_LFN)]; /* Directory entry block scratchpad buffer */
|
|
#endif
|
|
static WCHAR LfnBuf[_MAX_LFN + 1]; /* LFN enabled with static working buffer */
|
|
#define DEF_NAMBUF
|
|
#define INIT_NAMBUF(fs)
|
|
#define FREE_NAMBUF()
|
|
|
|
#elif _USE_LFN == 2 /* LFN enabled with dynamic working buffer on the stack */
|
|
#if _FS_EXFAT
|
|
#define DEF_NAMBUF WCHAR lbuf[_MAX_LFN+1]; BYTE dbuf[MAXDIRB(_MAX_LFN)];
|
|
#define INIT_NAMBUF(fs) { (fs)->lfnbuf = lbuf; (fs)->dirbuf = dbuf; }
|
|
#define FREE_NAMBUF()
|
|
#else
|
|
#define DEF_NAMBUF WCHAR lbuf[_MAX_LFN+1];
|
|
#define INIT_NAMBUF(fs) { (fs)->lfnbuf = lbuf; }
|
|
#define FREE_NAMBUF()
|
|
#endif
|
|
|
|
#elif _USE_LFN == 3 /* LFN enabled with dynamic working buffer on the heap */
|
|
#if _FS_EXFAT
|
|
#define DEF_NAMBUF WCHAR *lfn;
|
|
#define INIT_NAMBUF(fs) { lfn = ff_memalloc((_MAX_LFN+1)*2 + MAXDIRB(_MAX_LFN)); if (!lfn) LEAVE_FF(fs, FR_NOT_ENOUGH_CORE); (fs)->lfnbuf = lfn; (fs)->dirbuf = (BYTE*)(lfn+_MAX_LFN+1); }
|
|
#define FREE_NAMBUF() ff_memfree(lfn)
|
|
#else
|
|
#define DEF_NAMBUF WCHAR *lfn;
|
|
#define INIT_NAMBUF(fs) { lfn = ff_memalloc((_MAX_LFN+1)*2); if (!lfn) LEAVE_FF(fs, FR_NOT_ENOUGH_CORE); (fs)->lfnbuf = lfn; }
|
|
#define FREE_NAMBUF() ff_memfree(lfn)
|
|
#endif
|
|
|
|
#else
|
|
#error Wrong _USE_LFN setting
|
|
|
|
#endif
|
|
#endif /* else _USE_LFN == 0 */
|
|
|
|
#ifdef _EXCVT
|
|
static const BYTE ExCvt[] = _EXCVT; /* Upper conversion table for SBCS extended characters */
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------
|
|
|
|
Module Private Functions
|
|
|
|
---------------------------------------------------------------------------*/
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
/* Load/Store multi-byte word in the FAT structure */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
WORD ld_word (const BYTE* ptr) /* Load a 2-byte little-endian word */
|
|
{
|
|
WORD rv;
|
|
|
|
rv = ptr[1];
|
|
rv = rv << 8 | ptr[0];
|
|
return rv;
|
|
}
|
|
|
|
static
|
|
DWORD ld_dword (const BYTE* ptr) /* Load a 4-byte little-endian word */
|
|
{
|
|
DWORD rv;
|
|
|
|
rv = ptr[3];
|
|
rv = rv << 8 | ptr[2];
|
|
rv = rv << 8 | ptr[1];
|
|
rv = rv << 8 | ptr[0];
|
|
return rv;
|
|
}
|
|
|
|
#if _FS_EXFAT
|
|
static
|
|
QWORD ld_qword (const BYTE* ptr) /* Load an 8-byte little-endian word */
|
|
{
|
|
QWORD rv;
|
|
|
|
rv = ptr[7];
|
|
rv = rv << 8 | ptr[6];
|
|
rv = rv << 8 | ptr[5];
|
|
rv = rv << 8 | ptr[4];
|
|
rv = rv << 8 | ptr[3];
|
|
rv = rv << 8 | ptr[2];
|
|
rv = rv << 8 | ptr[1];
|
|
rv = rv << 8 | ptr[0];
|
|
return rv;
|
|
}
|
|
#endif
|
|
|
|
#if !_FS_READONLY
|
|
static
|
|
void st_word (BYTE* ptr, WORD val) /* Store a 2-byte word in little-endian */
|
|
{
|
|
*ptr++ = (BYTE)val; val >>= 8;
|
|
*ptr++ = (BYTE)val;
|
|
}
|
|
|
|
static
|
|
void st_dword (BYTE* ptr, DWORD val) /* Store a 4-byte word in little-endian */
|
|
{
|
|
*ptr++ = (BYTE)val; val >>= 8;
|
|
*ptr++ = (BYTE)val; val >>= 8;
|
|
*ptr++ = (BYTE)val; val >>= 8;
|
|
*ptr++ = (BYTE)val;
|
|
}
|
|
|
|
#if _FS_EXFAT
|
|
static
|
|
void st_qword (BYTE* ptr, QWORD val) /* Store an 8-byte word in little-endian */
|
|
{
|
|
*ptr++ = (BYTE)val; val >>= 8;
|
|
*ptr++ = (BYTE)val; val >>= 8;
|
|
*ptr++ = (BYTE)val; val >>= 8;
|
|
*ptr++ = (BYTE)val; val >>= 8;
|
|
*ptr++ = (BYTE)val; val >>= 8;
|
|
*ptr++ = (BYTE)val; val >>= 8;
|
|
*ptr++ = (BYTE)val; val >>= 8;
|
|
*ptr++ = (BYTE)val;
|
|
}
|
|
#endif
|
|
#endif /* !_FS_READONLY */
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
/* String functions */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/* Copy memory to memory */
|
|
static
|
|
void mem_cpy (void* dst, const void* src, UINT cnt) {
|
|
BYTE *d = (BYTE*)dst;
|
|
const BYTE *s = (const BYTE*)src;
|
|
|
|
if (cnt) {
|
|
do {
|
|
*d++ = *s++;
|
|
} while (--cnt);
|
|
}
|
|
}
|
|
|
|
/* Fill memory block */
|
|
static
|
|
void mem_set (void* dst, int val, UINT cnt) {
|
|
BYTE *d = (BYTE*)dst;
|
|
|
|
do {
|
|
*d++ = (BYTE)val;
|
|
} while (--cnt);
|
|
}
|
|
|
|
/* Compare memory block */
|
|
static
|
|
int mem_cmp (const void* dst, const void* src, UINT cnt) { /* ZR:same, NZ:different */
|
|
const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src;
|
|
int r = 0;
|
|
|
|
do {
|
|
r = *d++ - *s++;
|
|
} while (--cnt && r == 0);
|
|
|
|
return r;
|
|
}
|
|
|
|
/* Check if chr is contained in the string */
|
|
static
|
|
int chk_chr (const char* str, int chr) { /* NZ:contained, ZR:not contained */
|
|
while (*str && *str != chr) str++;
|
|
return *str;
|
|
}
|
|
|
|
|
|
|
|
|
|
#if _FS_REENTRANT
|
|
/*-----------------------------------------------------------------------*/
|
|
/* Request/Release grant to access the volume */
|
|
/*-----------------------------------------------------------------------*/
|
|
static
|
|
int lock_fs (
|
|
FATFS* fs /* File system object */
|
|
)
|
|
{
|
|
return (fs && ff_req_grant(fs->sobj)) ? 1 : 0;
|
|
}
|
|
|
|
|
|
static
|
|
void unlock_fs (
|
|
FATFS* fs, /* File system object */
|
|
FRESULT res /* Result code to be returned */
|
|
)
|
|
{
|
|
if (fs && res != FR_NOT_ENABLED && res != FR_INVALID_DRIVE && res != FR_TIMEOUT) {
|
|
ff_rel_grant(fs->sobj);
|
|
}
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if _FS_LOCK != 0
|
|
/*-----------------------------------------------------------------------*/
|
|
/* File lock control functions */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
FRESULT chk_lock ( /* Check if the file can be accessed */
|
|
DIR* dp, /* Directory object pointing the file to be checked */
|
|
int acc /* Desired access type (0:Read, 1:Write, 2:Delete/Rename) */
|
|
)
|
|
{
|
|
UINT i, be;
|
|
|
|
/* Search file semaphore table */
|
|
for (i = be = 0; i < _FS_LOCK; i++) {
|
|
if (Files[i].fs) { /* Existing entry */
|
|
if (Files[i].fs == dp->obj.fs && /* Check if the object matched with an open object */
|
|
Files[i].clu == dp->obj.sclust &&
|
|
Files[i].ofs == dp->dptr) break;
|
|
} else { /* Blank entry */
|
|
be = 1;
|
|
}
|
|
}
|
|
if (i == _FS_LOCK) { /* The object is not opened */
|
|
return (be || acc == 2) ? FR_OK : FR_TOO_MANY_OPEN_FILES; /* Is there a blank entry for new object? */
|
|
}
|
|
|
|
/* The object has been opened. Reject any open against writing file and all write mode open */
|
|
return (acc || Files[i].ctr == 0x100) ? FR_LOCKED : FR_OK;
|
|
}
|
|
|
|
|
|
static
|
|
int enq_lock (void) /* Check if an entry is available for a new object */
|
|
{
|
|
UINT i;
|
|
|
|
for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ;
|
|
return (i == _FS_LOCK) ? 0 : 1;
|
|
}
|
|
|
|
|
|
static
|
|
UINT inc_lock ( /* Increment object open counter and returns its index (0:Internal error) */
|
|
DIR* dp, /* Directory object pointing the file to register or increment */
|
|
int acc /* Desired access (0:Read, 1:Write, 2:Delete/Rename) */
|
|
)
|
|
{
|
|
UINT i;
|
|
|
|
|
|
for (i = 0; i < _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 (i == _FS_LOCK) { /* Not opened. Register it as new. */
|
|
for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ;
|
|
if (i == _FS_LOCK) return 0; /* No free entry to register (int err) */
|
|
Files[i].fs = dp->obj.fs;
|
|
Files[i].clu = dp->obj.sclust;
|
|
Files[i].ofs = dp->dptr;
|
|
Files[i].ctr = 0;
|
|
}
|
|
|
|
if (acc && Files[i].ctr) return 0; /* Access violation (int err) */
|
|
|
|
Files[i].ctr = acc ? 0x100 : Files[i].ctr + 1; /* Set semaphore value */
|
|
|
|
return i + 1;
|
|
}
|
|
|
|
|
|
static
|
|
FRESULT dec_lock ( /* Decrement object open counter */
|
|
UINT i /* Semaphore index (1..) */
|
|
)
|
|
{
|
|
WORD n;
|
|
FRESULT res;
|
|
|
|
|
|
if (--i < _FS_LOCK) { /* Shift index number origin from 0 */
|
|
n = Files[i].ctr;
|
|
if (n == 0x100) n = 0; /* If write mode open, delete the entry */
|
|
if (n > 0) n--; /* Decrement read mode open count */
|
|
Files[i].ctr = n;
|
|
if (n == 0) Files[i].fs = 0; /* Delete the entry if open count gets zero */
|
|
res = FR_OK;
|
|
} else {
|
|
res = FR_INT_ERR; /* Invalid index nunber */
|
|
}
|
|
return res;
|
|
}
|
|
|
|
|
|
static
|
|
void clear_lock ( /* Clear lock entries of the volume */
|
|
FATFS *fs
|
|
)
|
|
{
|
|
UINT i;
|
|
|
|
for (i = 0; i < _FS_LOCK; i++) {
|
|
if (Files[i].fs == fs) Files[i].fs = 0;
|
|
}
|
|
}
|
|
|
|
#endif /* _FS_LOCK != 0 */
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
/* Move/Flush disk access window in the file system object */
|
|
/*-----------------------------------------------------------------------*/
|
|
#if !_FS_READONLY
|
|
static
|
|
FRESULT sync_window ( /* Returns FR_OK or FR_DISK_ERROR */
|
|
FATFS* fs /* File system object */
|
|
)
|
|
{
|
|
DWORD wsect;
|
|
UINT nf;
|
|
FRESULT res = FR_OK;
|
|
|
|
|
|
if (fs->wflag) { /* Write back the sector if it is dirty */
|
|
wsect = fs->winsect; /* Current sector number */
|
|
if (disk_write(fs->drv, fs->win, wsect, 1) != RES_OK) {
|
|
res = FR_DISK_ERR;
|
|
} else {
|
|
fs->wflag = 0;
|
|
if (wsect - fs->fatbase < fs->fsize) { /* Is it in the FAT area? */
|
|
for (nf = fs->n_fats; nf >= 2; nf--) { /* Reflect the change to all FAT copies */
|
|
wsect += fs->fsize;
|
|
disk_write(fs->drv, fs->win, wsect, 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
#endif
|
|
|
|
|
|
static
|
|
FRESULT move_window ( /* Returns FR_OK or FR_DISK_ERROR */
|
|
FATFS* fs, /* File system object */
|
|
DWORD sector /* Sector number to make appearance in the fs->win[] */
|
|
)
|
|
{
|
|
FRESULT res = FR_OK;
|
|
|
|
|
|
if (sector != fs->winsect) { /* Window offset changed? */
|
|
#if !_FS_READONLY
|
|
res = sync_window(fs); /* Write-back changes */
|
|
#endif
|
|
if (res == FR_OK) { /* Fill sector window with new data */
|
|
if (disk_read(fs->drv, fs->win, sector, 1) != RES_OK) {
|
|
sector = 0xFFFFFFFF; /* Invalidate window if data is not reliable */
|
|
res = FR_DISK_ERR;
|
|
}
|
|
fs->winsect = sector;
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
|
|
|
|
|
|
#if !_FS_READONLY
|
|
/*-----------------------------------------------------------------------*/
|
|
/* Synchronize file system and strage device */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
FRESULT sync_fs ( /* FR_OK:succeeded, !=0:error */
|
|
FATFS* fs /* File system object */
|
|
)
|
|
{
|
|
FRESULT res;
|
|
|
|
|
|
res = sync_window(fs);
|
|
if (res == FR_OK) {
|
|
/* Update FSInfo sector if needed */
|
|
if (fs->fs_type == FS_FAT32 && fs->fsi_flag == 1) {
|
|
/* Create FSInfo structure */
|
|
mem_set(fs->win, 0, SS(fs));
|
|
st_word(fs->win + BS_55AA, 0xAA55);
|
|
st_dword(fs->win + FSI_LeadSig, 0x41615252);
|
|
st_dword(fs->win + FSI_StrucSig, 0x61417272);
|
|
st_dword(fs->win + FSI_Free_Count, fs->free_clst);
|
|
st_dword(fs->win + FSI_Nxt_Free, fs->last_clst);
|
|
/* Write it into the FSInfo sector */
|
|
fs->winsect = fs->volbase + 1;
|
|
disk_write(fs->drv, fs->win, fs->winsect, 1);
|
|
fs->fsi_flag = 0;
|
|
}
|
|
/* Make sure that no pending write process in the physical drive */
|
|
if (disk_ioctl(fs->drv, CTRL_SYNC, 0) != RES_OK) res = FR_DISK_ERR;
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
/* Get sector# from cluster# */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
DWORD clust2sect ( /* !=0:Sector number, 0:Failed (invalid cluster#) */
|
|
FATFS* fs, /* File system object */
|
|
DWORD clst /* Cluster# to be converted */
|
|
)
|
|
{
|
|
clst -= 2;
|
|
if (clst >= fs->n_fatent - 2) return 0; /* Invalid cluster# */
|
|
return clst * fs->csize + fs->database;
|
|
}
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
/* FAT access - Read value of a FAT entry */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, 2..0x7FFFFFFF:Cluster status */
|
|
_FDID* obj, /* Corresponding object */
|
|
DWORD clst /* Cluster number to get the value */
|
|
)
|
|
{
|
|
UINT wc, bc;
|
|
DWORD val;
|
|
FATFS *fs = obj->fs;
|
|
|
|
|
|
if (clst < 2 || clst >= fs->n_fatent) { /* Check if in valid range */
|
|
val = 1; /* Internal error */
|
|
|
|
} else {
|
|
val = 0xFFFFFFFF; /* Default value falls on disk error */
|
|
|
|
switch (fs->fs_type) {
|
|
case FS_FAT12 :
|
|
bc = (UINT)clst; bc += bc / 2;
|
|
if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK) break;
|
|
wc = fs->win[bc++ % SS(fs)];
|
|
if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK) break;
|
|
wc |= fs->win[bc % SS(fs)] << 8;
|
|
val = (clst & 1) ? (wc >> 4) : (wc & 0xFFF);
|
|
break;
|
|
|
|
case FS_FAT16 :
|
|
if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2))) != FR_OK) break;
|
|
val = ld_word(fs->win + clst * 2 % SS(fs));
|
|
break;
|
|
|
|
case FS_FAT32 :
|
|
if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK) break;
|
|
val = ld_dword(fs->win + clst * 4 % SS(fs)) & 0x0FFFFFFF;
|
|
break;
|
|
#if _FS_EXFAT
|
|
case FS_EXFAT :
|
|
if (obj->objsize) {
|
|
DWORD cofs = clst - obj->sclust; /* Offset from start cluster */
|
|
DWORD clen = (DWORD)((obj->objsize - 1) / SS(fs)) / fs->csize; /* Number of clusters - 1 */
|
|
|
|
if (obj->stat == 2) { /* Is there no valid chain on the FAT? */
|
|
if (cofs <= clen) {
|
|
val = (cofs == clen) ? 0x7FFFFFFF : clst + 1; /* Generate the value */
|
|
break;
|
|
}
|
|
}
|
|
if (obj->stat == 3 && cofs < obj->n_cont) { /* Is it in the 1st fragment? */
|
|
val = clst + 1; /* Generate the value */
|
|
break;
|
|
}
|
|
if (obj->stat != 2) { /* Get value from FAT if FAT chain is valid */
|
|
if (obj->n_frag != 0) { /* Is it on the growing edge? */
|
|
val = 0x7FFFFFFF; /* Generate EOC */
|
|
} else {
|
|
if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK) break;
|
|
val = ld_dword(fs->win + clst * 4 % SS(fs)) & 0x7FFFFFFF;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
__attribute__ ((fallthrough));
|
|
#endif
|
|
default:
|
|
val = 1; /* Internal error */
|
|
}
|
|
}
|
|
|
|
return val;
|
|
}
|
|
|
|
|
|
|
|
|
|
#if !_FS_READONLY
|
|
/*-----------------------------------------------------------------------*/
|
|
/* FAT access - Change value of a FAT entry */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
FRESULT put_fat ( /* FR_OK(0):succeeded, !=0:error */
|
|
FATFS* fs, /* Corresponding file system object */
|
|
DWORD clst, /* FAT index number (cluster number) to be changed */
|
|
DWORD val /* New value to be set to the entry */
|
|
)
|
|
{
|
|
UINT bc;
|
|
BYTE *p;
|
|
FRESULT res = FR_INT_ERR;
|
|
|
|
if (clst >= 2 && clst < fs->n_fatent) { /* Check if in valid range */
|
|
switch (fs->fs_type) {
|
|
case FS_FAT12 : /* Bitfield items */
|
|
bc = (UINT)clst; bc += bc / 2;
|
|
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;
|
|
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));
|
|
fs->wflag = 1;
|
|
break;
|
|
|
|
case FS_FAT16 : /* WORD aligned items */
|
|
res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));
|
|
if (res != FR_OK) break;
|
|
st_word(fs->win + clst * 2 % SS(fs), (WORD)val);
|
|
fs->wflag = 1;
|
|
break;
|
|
|
|
case FS_FAT32 : /* DWORD aligned items */
|
|
#if _FS_EXFAT
|
|
case FS_EXFAT :
|
|
#endif
|
|
res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));
|
|
if (res != FR_OK) break;
|
|
if (!_FS_EXFAT || fs->fs_type != FS_EXFAT) {
|
|
val = (val & 0x0FFFFFFF) | (ld_dword(fs->win + clst * 4 % SS(fs)) & 0xF0000000);
|
|
}
|
|
st_dword(fs->win + clst * 4 % SS(fs), val);
|
|
fs->wflag = 1;
|
|
break;
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
#endif /* !_FS_READONLY */
|
|
|
|
|
|
|
|
|
|
#if _FS_EXFAT && !_FS_READONLY
|
|
/*-----------------------------------------------------------------------*/
|
|
/* exFAT: Accessing FAT and Allocation Bitmap */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/*--------------------------------------*/
|
|
/* Find a contiguous free cluster block */
|
|
/*--------------------------------------*/
|
|
|
|
static
|
|
DWORD find_bitmap ( /* 0:Not found, 2..:Cluster block found, 0xFFFFFFFF:Disk error */
|
|
FATFS* fs, /* File system object */
|
|
DWORD clst, /* Cluster number to scan from */
|
|
DWORD ncl /* Number of contiguous clusters to find (1..) */
|
|
)
|
|
{
|
|
BYTE bm, bv;
|
|
UINT i;
|
|
DWORD val, scl, ctr;
|
|
|
|
|
|
clst -= 2; /* The first bit in the bitmap corresponds to cluster #2 */
|
|
if (clst >= fs->n_fatent - 2) clst = 0;
|
|
scl = val = clst; ctr = 0;
|
|
for (;;) {
|
|
if (move_window(fs, fs->database + val / 8 / SS(fs)) != FR_OK) return 0xFFFFFFFF; /* (assuming bitmap is located top of the cluster heap) */
|
|
i = val / 8 % SS(fs); bm = 1 << (val % 8);
|
|
do {
|
|
do {
|
|
bv = fs->win[i] & bm; bm <<= 1; /* Get bit value */
|
|
if (++val >= fs->n_fatent - 2) { /* Next cluster (with wrap-around) */
|
|
val = 0; bm = 0; i = SS(fs);
|
|
}
|
|
if (!bv) { /* Is it a free cluster? */
|
|
if (++ctr == ncl) return scl + 2; /* Check if run length is sufficient for required */
|
|
} else {
|
|
scl = val; ctr = 0; /* Encountered a cluster in-use, restart to scan */
|
|
}
|
|
if (val == clst) return 0; /* All cluster scanned? */
|
|
} while (bm);
|
|
bm = 1;
|
|
} while (++i < SS(fs));
|
|
}
|
|
}
|
|
|
|
|
|
/*----------------------------------------*/
|
|
/* Set/Clear a block of allocation bitmap */
|
|
/*----------------------------------------*/
|
|
|
|
static
|
|
FRESULT change_bitmap (
|
|
FATFS* fs, /* File system object */
|
|
DWORD clst, /* Cluster number to change from */
|
|
DWORD ncl, /* Number of clusters to be changed */
|
|
int bv /* bit value to be set (0 or 1) */
|
|
)
|
|
{
|
|
BYTE bm;
|
|
UINT i;
|
|
DWORD sect;
|
|
|
|
clst -= 2; /* The first bit corresponds to cluster #2 */
|
|
sect = fs->database + clst / 8 / SS(fs); /* Sector address (assuming bitmap is located top of the cluster heap) */
|
|
i = clst / 8 % SS(fs); /* Byte offset in the sector */
|
|
bm = 1 << (clst % 8); /* Bit mask in the byte */
|
|
for (;;) {
|
|
if (move_window(fs, sect++) != FR_OK) return FR_DISK_ERR;
|
|
do {
|
|
do {
|
|
if (bv == (int)((fs->win[i] & bm) != 0)) return FR_INT_ERR; /* Is the bit expected value? */
|
|
fs->win[i] ^= bm; /* Flip the bit */
|
|
fs->wflag = 1;
|
|
if (--ncl == 0) return FR_OK; /* All bits processed? */
|
|
} while (bm <<= 1); /* Next bit */
|
|
bm = 1;
|
|
} while (++i < SS(fs)); /* Next byte */
|
|
i = 0;
|
|
}
|
|
}
|
|
|
|
|
|
/*---------------------------------------------*/
|
|
/* Fill the first fragment of the FAT chain */
|
|
/*---------------------------------------------*/
|
|
|
|
static
|
|
FRESULT fill_first_frag (
|
|
_FDID* obj /* Pointer to the corresponding object */
|
|
)
|
|
{
|
|
FRESULT res;
|
|
DWORD cl, n;
|
|
|
|
if (obj->stat == 3) { /* Has the object been changed 'fragmented'? */
|
|
for (cl = obj->sclust, n = obj->n_cont; n; cl++, n--) { /* Create cluster chain on the FAT */
|
|
res = put_fat(obj->fs, cl, cl + 1);
|
|
if (res != FR_OK) return res;
|
|
}
|
|
obj->stat = 0; /* Change status 'FAT chain is valid' */
|
|
}
|
|
return FR_OK;
|
|
}
|
|
|
|
|
|
/*---------------------------------------------*/
|
|
/* Fill the last fragment of the FAT chain */
|
|
/*---------------------------------------------*/
|
|
|
|
static
|
|
FRESULT fill_last_frag (
|
|
_FDID* obj, /* Pointer to the corresponding object */
|
|
DWORD lcl, /* Last cluster of the fragment */
|
|
DWORD term /* Value to set the last FAT entry */
|
|
)
|
|
{
|
|
FRESULT res;
|
|
|
|
while (obj->n_frag > 0) { /* Create the last chain on the FAT */
|
|
res = put_fat(obj->fs, lcl - obj->n_frag + 1, (obj->n_frag > 1) ? lcl - obj->n_frag + 2 : term);
|
|
if (res != FR_OK) return res;
|
|
obj->n_frag--;
|
|
}
|
|
return FR_OK;
|
|
}
|
|
|
|
#endif /* _FS_EXFAT && !_FS_READONLY */
|
|
|
|
|
|
|
|
#if !_FS_READONLY
|
|
/*-----------------------------------------------------------------------*/
|
|
/* FAT handling - Remove a cluster chain */
|
|
/*-----------------------------------------------------------------------*/
|
|
static
|
|
FRESULT remove_chain ( /* FR_OK(0):succeeded, !=0:error */
|
|
_FDID* obj, /* Corresponding object */
|
|
DWORD clst, /* Cluster to remove a chain from */
|
|
DWORD pclst /* Previous cluster of clst (0:an entire chain) */
|
|
)
|
|
{
|
|
FRESULT res = FR_OK;
|
|
DWORD nxt;
|
|
FATFS *fs = obj->fs;
|
|
#if _FS_EXFAT || _USE_TRIM
|
|
DWORD scl = clst, ecl = clst;
|
|
#endif
|
|
#if _USE_TRIM
|
|
DWORD rt[2];
|
|
#endif
|
|
|
|
if (clst < 2 || clst >= fs->n_fatent) return FR_INT_ERR; /* Check if in valid range */
|
|
|
|
/* Mark the previous cluster 'EOC' on the FAT if it exists */
|
|
if (pclst && (!_FS_EXFAT || fs->fs_type != FS_EXFAT || obj->stat != 2)) {
|
|
res = put_fat(fs, pclst, 0xFFFFFFFF);
|
|
if (res != FR_OK) return res;
|
|
}
|
|
|
|
/* Remove the chain */
|
|
do {
|
|
nxt = get_fat(obj, clst); /* Get cluster status */
|
|
if (nxt == 0) break; /* Empty cluster? */
|
|
if (nxt == 1) return FR_INT_ERR; /* Internal error? */
|
|
if (nxt == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error? */
|
|
if (!_FS_EXFAT || fs->fs_type != FS_EXFAT) {
|
|
res = put_fat(fs, clst, 0); /* Mark the cluster 'free' on the FAT */
|
|
if (res != FR_OK) return res;
|
|
}
|
|
if (fs->free_clst < fs->n_fatent - 2) { /* Update FSINFO */
|
|
fs->free_clst++;
|
|
fs->fsi_flag |= 1;
|
|
}
|
|
#if _FS_EXFAT || _USE_TRIM
|
|
if (ecl + 1 == nxt) { /* Is next cluster contiguous? */
|
|
ecl = nxt;
|
|
} else { /* End of contiguous cluster block */
|
|
#if _FS_EXFAT
|
|
if (fs->fs_type == FS_EXFAT) {
|
|
res = change_bitmap(fs, scl, ecl - scl + 1, 0); /* Mark the cluster block 'free' on the bitmap */
|
|
if (res != FR_OK) return res;
|
|
}
|
|
#endif
|
|
#if _USE_TRIM
|
|
rt[0] = clust2sect(fs, scl); /* Start sector */
|
|
rt[1] = clust2sect(fs, ecl) + fs->csize - 1; /* End sector */
|
|
disk_ioctl(fs->drv, CTRL_TRIM, rt); /* Inform device the block can be erased */
|
|
#endif
|
|
scl = ecl = nxt;
|
|
}
|
|
#endif
|
|
clst = nxt; /* Next cluster */
|
|
} while (clst < fs->n_fatent); /* Repeat while not the last link */
|
|
|
|
#if _FS_EXFAT
|
|
if (fs->fs_type == FS_EXFAT) {
|
|
if (pclst == 0) { /* Does the object have no chain? */
|
|
obj->stat = 0; /* Change the object status 'initial' */
|
|
} else {
|
|
if (obj->stat == 3 && pclst >= obj->sclust && pclst <= obj->sclust + obj->n_cont) { /* Did the chain get contiguous? */
|
|
obj->stat = 2; /* Change the object status 'contiguous' */
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
return FR_OK;
|
|
}
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
/* FAT handling - Stretch a chain or Create a new chain */
|
|
/*-----------------------------------------------------------------------*/
|
|
static
|
|
DWORD create_chain ( /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:Disk error, >=2:New cluster# */
|
|
_FDID* obj, /* Corresponding object */
|
|
DWORD clst /* Cluster# to stretch, 0:Create a new chain */
|
|
)
|
|
{
|
|
DWORD cs, ncl, scl;
|
|
FRESULT res;
|
|
FATFS *fs = obj->fs;
|
|
|
|
|
|
if (clst == 0) { /* Create a new chain */
|
|
scl = fs->last_clst; /* Get suggested cluster to start from */
|
|
if (scl == 0 || scl >= fs->n_fatent) scl = 1;
|
|
}
|
|
else { /* Stretch current chain */
|
|
cs = get_fat(obj, clst); /* Check the cluster status */
|
|
if (cs < 2) return 1; /* Invalid FAT value */
|
|
if (cs == 0xFFFFFFFF) return cs; /* A disk error occurred */
|
|
if (cs < fs->n_fatent) return cs; /* It is already followed by next cluster */
|
|
scl = clst;
|
|
}
|
|
|
|
#if _FS_EXFAT
|
|
if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */
|
|
ncl = find_bitmap(fs, scl, 1); /* Find a free cluster */
|
|
if (ncl == 0 || ncl == 0xFFFFFFFF) return ncl; /* No free cluster or hard error? */
|
|
res = change_bitmap(fs, ncl, 1, 1); /* Mark the cluster 'in use' */
|
|
if (res == FR_INT_ERR) return 1;
|
|
if (res == FR_DISK_ERR) return 0xFFFFFFFF;
|
|
if (clst == 0) { /* Is it a new chain? */
|
|
obj->stat = 2; /* Set status 'contiguous' */
|
|
} else { /* It is a stretched chain */
|
|
if (obj->stat == 2 && ncl != scl + 1) { /* Is the chain got fragmented? */
|
|
obj->n_cont = scl - obj->sclust; /* Set size of the contiguous part */
|
|
obj->stat = 3; /* Change status 'just fragmented' */
|
|
}
|
|
}
|
|
if (obj->stat != 2) { /* Is the file non-contiguous? */
|
|
if (ncl == clst + 1) { /* Is the cluster next to previous one? */
|
|
obj->n_frag = obj->n_frag ? obj->n_frag + 1 : 2; /* Increment size of last framgent */
|
|
} else { /* New fragment */
|
|
if (obj->n_frag == 0) obj->n_frag = 1;
|
|
res = fill_last_frag(obj, clst, ncl); /* Fill last fragment on the FAT and link it to new one */
|
|
if (res == FR_OK) obj->n_frag = 1;
|
|
}
|
|
}
|
|
} else
|
|
#endif
|
|
{ /* On the FAT12/16/32 volume */
|
|
ncl = scl; /* Start cluster */
|
|
for (;;) {
|
|
ncl++; /* Next cluster */
|
|
if (ncl >= fs->n_fatent) { /* Check wrap-around */
|
|
ncl = 2;
|
|
if (ncl > scl) return 0; /* No free cluster */
|
|
}
|
|
cs = get_fat(obj, ncl); /* Get the cluster status */
|
|
if (cs == 0) break; /* Found a free cluster */
|
|
if (cs == 1 || cs == 0xFFFFFFFF) return cs; /* An error occurred */
|
|
if (ncl == scl) return 0; /* No free cluster */
|
|
}
|
|
res = put_fat(fs, ncl, 0xFFFFFFFF); /* Mark the new cluster 'EOC' */
|
|
if (res == FR_OK && clst != 0) {
|
|
res = put_fat(fs, clst, ncl); /* Link it from the previous one if needed */
|
|
}
|
|
}
|
|
|
|
if (res == FR_OK) { /* Update FSINFO if function succeeded. */
|
|
fs->last_clst = ncl;
|
|
if (fs->free_clst <= fs->n_fatent - 2) fs->free_clst--;
|
|
fs->fsi_flag |= 1;
|
|
} else {
|
|
ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1; /* Failed. Generate error status */
|
|
}
|
|
|
|
return ncl; /* Return new cluster number or error status */
|
|
}
|
|
|
|
#endif /* !_FS_READONLY */
|
|
|
|
|
|
|
|
|
|
#if _USE_FASTSEEK
|
|
/*-----------------------------------------------------------------------*/
|
|
/* FAT handling - Convert offset into cluster with link map table */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
DWORD clmt_clust ( /* <2:Error, >=2:Cluster number */
|
|
FIL* fp, /* Pointer to the file object */
|
|
FSIZE_t ofs /* File offset to be converted to cluster# */
|
|
)
|
|
{
|
|
DWORD cl, ncl, *tbl;
|
|
FATFS *fs = fp->obj.fs;
|
|
|
|
|
|
tbl = fp->cltbl + 1; /* Top of CLMT */
|
|
cl = (DWORD)(ofs / SS(fs) / fs->csize); /* Cluster order from top of the file */
|
|
for (;;) {
|
|
ncl = *tbl++; /* Number of cluters in the fragment */
|
|
if (ncl == 0) return 0; /* End of table? (error) */
|
|
if (cl < ncl) break; /* In this fragment? */
|
|
cl -= ncl; tbl++; /* Next fragment */
|
|
}
|
|
return cl + *tbl; /* Return the cluster number */
|
|
}
|
|
|
|
#endif /* _USE_FASTSEEK */
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
/* Directory handling - Set directory index */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
FRESULT dir_sdi ( /* FR_OK(0):succeeded, !=0:error */
|
|
DIR* dp, /* Pointer to directory object */
|
|
DWORD ofs /* Offset of directory table */
|
|
)
|
|
{
|
|
DWORD csz, clst;
|
|
FATFS *fs = dp->obj.fs;
|
|
|
|
|
|
if (ofs >= (DWORD)((_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR) || ofs % SZDIRE) { /* Check range of offset and alignment */
|
|
return FR_INT_ERR;
|
|
}
|
|
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;
|
|
if (_FS_EXFAT) dp->obj.stat = 0; /* exFAT: Root dir has an FAT chain */
|
|
}
|
|
|
|
if (clst == 0) { /* Static table (root-directory in FAT12/16) */
|
|
if (ofs / SZDIRE >= fs->n_rootdir) return FR_INT_ERR; /* Is index out of range? */
|
|
dp->sect = fs->dirbase;
|
|
|
|
} else { /* Dynamic table (sub-directory or root-directory in FAT32+) */
|
|
csz = (DWORD)fs->csize * SS(fs); /* Bytes per cluster */
|
|
while (ofs >= csz) { /* Follow cluster chain */
|
|
clst = get_fat(&dp->obj, clst); /* Get next cluster */
|
|
if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */
|
|
if (clst < 2 || clst >= fs->n_fatent) return FR_INT_ERR; /* Reached to end of table or internal error */
|
|
ofs -= csz;
|
|
}
|
|
dp->sect = clust2sect(fs, clst);
|
|
}
|
|
dp->clust = clst; /* Current cluster# */
|
|
if (!dp->sect) return FR_INT_ERR;
|
|
dp->sect += ofs / SS(fs); /* Sector# of the directory entry */
|
|
dp->dir = fs->win + (ofs % SS(fs)); /* Pointer to the entry in the win[] */
|
|
|
|
return FR_OK;
|
|
}
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
/* Directory handling - Move directory table index next */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
FRESULT dir_next ( /* FR_OK(0):succeeded, FR_NO_FILE:End of table, FR_DENIED:Could not stretch */
|
|
DIR* dp, /* Pointer to the directory object */
|
|
int stretch /* 0: Do not stretch table, 1: Stretch table if needed */
|
|
)
|
|
{
|
|
DWORD ofs, clst;
|
|
FATFS *fs = dp->obj.fs;
|
|
#if !_FS_READONLY
|
|
UINT n;
|
|
#endif
|
|
|
|
ofs = dp->dptr + SZDIRE; /* Next entry */
|
|
if (!dp->sect || ofs >= (DWORD)((_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR)) return FR_NO_FILE; /* Report EOT when offset has reached max value */
|
|
|
|
if (ofs % SS(fs) == 0) { /* Sector changed? */
|
|
dp->sect++; /* Next sector */
|
|
|
|
if (!dp->clust) { /* Static table */
|
|
if (ofs / SZDIRE >= fs->n_rootdir) { /* Report EOT if it reached end of static table */
|
|
dp->sect = 0; return FR_NO_FILE;
|
|
}
|
|
}
|
|
else { /* Dynamic table */
|
|
if ((ofs / SS(fs) & (fs->csize - 1)) == 0) { /* Cluster changed? */
|
|
clst = get_fat(&dp->obj, dp->clust); /* Get next cluster */
|
|
if (clst <= 1) return FR_INT_ERR; /* Internal error */
|
|
if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */
|
|
if (clst >= fs->n_fatent) { /* Reached end of dynamic table */
|
|
#if !_FS_READONLY
|
|
if (!stretch) { /* If no stretch, report EOT */
|
|
dp->sect = 0; return FR_NO_FILE;
|
|
}
|
|
clst = create_chain(&dp->obj, dp->clust); /* Allocate a cluster */
|
|
if (clst == 0) return FR_DENIED; /* No free cluster */
|
|
if (clst == 1) return FR_INT_ERR; /* Internal error */
|
|
if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */
|
|
/* Clean-up the stretched table */
|
|
if (_FS_EXFAT) dp->obj.stat |= 4; /* The directory needs to be updated */
|
|
if (sync_window(fs) != FR_OK) return FR_DISK_ERR; /* Flush disk access window */
|
|
mem_set(fs->win, 0, SS(fs)); /* Clear window buffer */
|
|
for (n = 0, fs->winsect = clust2sect(fs, clst); n < fs->csize; n++, fs->winsect++) { /* Fill the new cluster with 0 */
|
|
fs->wflag = 1;
|
|
if (sync_window(fs) != FR_OK) return FR_DISK_ERR;
|
|
}
|
|
fs->winsect -= n; /* Restore window offset */
|
|
#else
|
|
if (!stretch) dp->sect = 0; /* (this line is to suppress compiler warning) */
|
|
dp->sect = 0; return FR_NO_FILE; /* Report EOT */
|
|
#endif
|
|
}
|
|
dp->clust = clst; /* Initialize data for new cluster */
|
|
dp->sect = clust2sect(fs, clst);
|
|
}
|
|
}
|
|
}
|
|
dp->dptr = ofs; /* Current entry */
|
|
dp->dir = fs->win + ofs % SS(fs); /* Pointer to the entry in the win[] */
|
|
|
|
return FR_OK;
|
|
}
|
|
|
|
|
|
|
|
|
|
#if !_FS_READONLY
|
|
/*-----------------------------------------------------------------------*/
|
|
/* Directory handling - Reserve a block of directory entries */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
FRESULT dir_alloc ( /* FR_OK(0):succeeded, !=0:error */
|
|
DIR* dp, /* Pointer to the directory object */
|
|
UINT nent /* Number of contiguous entries to allocate */
|
|
)
|
|
{
|
|
FRESULT res;
|
|
UINT n;
|
|
FATFS *fs = dp->obj.fs;
|
|
|
|
|
|
res = dir_sdi(dp, 0);
|
|
if (res == FR_OK) {
|
|
n = 0;
|
|
do {
|
|
res = move_window(fs, dp->sect);
|
|
if (res != FR_OK) break;
|
|
#if _FS_EXFAT
|
|
if ((fs->fs_type == FS_EXFAT) ? (int)((dp->dir[XDIR_Type] & 0x80) == 0) : (int)(dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0)) {
|
|
#else
|
|
if (dp->dir[DIR_Name] == DDEM || dp->dir[DIR_Name] == 0) {
|
|
#endif
|
|
if (++n == nent) break; /* A block of contiguous free entries is found */
|
|
} else {
|
|
n = 0; /* Not a blank entry. Restart to search */
|
|
}
|
|
res = dir_next(dp, 1);
|
|
} while (res == FR_OK); /* Next entry with table stretch enabled */
|
|
}
|
|
|
|
if (res == FR_NO_FILE) res = FR_DENIED; /* No directory entry to allocate */
|
|
return res;
|
|
}
|
|
|
|
#endif /* !_FS_READONLY */
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
/* FAT: Directory handling - Load/Store start cluster number */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
DWORD ld_clust ( /* Returns the top cluster value of the SFN entry */
|
|
FATFS* fs, /* Pointer to the fs object */
|
|
const BYTE* dir /* Pointer to the key entry */
|
|
)
|
|
{
|
|
DWORD cl;
|
|
|
|
cl = ld_word(dir + DIR_FstClusLO);
|
|
if (fs->fs_type == FS_FAT32) {
|
|
cl |= (DWORD)ld_word(dir + DIR_FstClusHI) << 16;
|
|
}
|
|
|
|
return cl;
|
|
}
|
|
|
|
|
|
#if !_FS_READONLY
|
|
static
|
|
void st_clust (
|
|
FATFS* fs, /* Pointer to the fs object */
|
|
BYTE* dir, /* Pointer to the key entry */
|
|
DWORD cl /* Value to be set */
|
|
)
|
|
{
|
|
st_word(dir + DIR_FstClusLO, (WORD)cl);
|
|
if (fs->fs_type == FS_FAT32) {
|
|
st_word(dir + DIR_FstClusHI, (WORD)(cl >> 16));
|
|
}
|
|
}
|
|
#endif
|
|
|
|
|
|
|
|
#if _USE_LFN != 0
|
|
/*------------------------------------------------------------------------*/
|
|
/* FAT-LFN: LFN handling */
|
|
/*------------------------------------------------------------------------*/
|
|
static
|
|
const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30}; /* Offset of LFN characters in the directory entry */
|
|
|
|
|
|
/*--------------------------------------------------------*/
|
|
/* FAT-LFN: Compare a part of file name with an LFN entry */
|
|
/*--------------------------------------------------------*/
|
|
static
|
|
int cmp_lfn ( /* 1:matched, 0:not matched */
|
|
const WCHAR* lfnbuf, /* Pointer to the LFN working buffer to be compared */
|
|
BYTE* dir /* Pointer to the directory entry containing the part of LFN */
|
|
)
|
|
{
|
|
UINT i, s;
|
|
WCHAR wc, uc;
|
|
|
|
|
|
if (ld_word(dir + LDIR_FstClusLO) != 0) return 0; /* Check LDIR_FstClusLO */
|
|
|
|
i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */
|
|
|
|
for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */
|
|
uc = ld_word(dir + LfnOfs[s]); /* Pick an LFN character */
|
|
if (wc) {
|
|
if (i >= _MAX_LFN || ff_wtoupper(uc) != ff_wtoupper(lfnbuf[i++])) { /* Compare it */
|
|
return 0; /* Not matched */
|
|
}
|
|
wc = uc;
|
|
} else {
|
|
if (uc != 0xFFFF) return 0; /* Check filler */
|
|
}
|
|
}
|
|
|
|
if ((dir[LDIR_Ord] & LLEF) && wc && lfnbuf[i]) return 0; /* Last segment matched but different length */
|
|
|
|
return 1; /* The part of LFN matched */
|
|
}
|
|
|
|
|
|
#if _FS_MINIMIZE <= 1 || _FS_RPATH >= 2 || _USE_LABEL || _FS_EXFAT
|
|
/*-----------------------------------------------------*/
|
|
/* FAT-LFN: Pick a part of file name from an LFN entry */
|
|
/*-----------------------------------------------------*/
|
|
static
|
|
int pick_lfn ( /* 1:succeeded, 0:buffer overflow or invalid LFN entry */
|
|
WCHAR* lfnbuf, /* Pointer to the LFN working buffer */
|
|
BYTE* dir /* Pointer to the LFN entry */
|
|
)
|
|
{
|
|
UINT i, s;
|
|
WCHAR wc, uc;
|
|
|
|
|
|
if (ld_word(dir + LDIR_FstClusLO) != 0) return 0; /* Check LDIR_FstClusLO is 0 */
|
|
|
|
i = ((dir[LDIR_Ord] & ~LLEF) - 1) * 13; /* Offset in the LFN buffer */
|
|
|
|
for (wc = 1, s = 0; s < 13; s++) { /* Process all characters in the entry */
|
|
uc = ld_word(dir + LfnOfs[s]); /* Pick an LFN character */
|
|
if (wc) {
|
|
if (i >= _MAX_LFN) 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 >= _MAX_LFN) return 0; /* Buffer overflow? */
|
|
lfnbuf[i] = 0;
|
|
}
|
|
|
|
return 1; /* The part of LFN is valid */
|
|
}
|
|
#endif
|
|
|
|
|
|
#if !_FS_READONLY
|
|
/*-----------------------------------------*/
|
|
/* FAT-LFN: Create an entry of LFN entries */
|
|
/*-----------------------------------------*/
|
|
static
|
|
void put_lfn (
|
|
const WCHAR* lfn, /* Pointer to the LFN */
|
|
BYTE* dir, /* Pointer to the LFN entry to be created */
|
|
BYTE ord, /* LFN order (1-20) */
|
|
BYTE sum /* Checksum of the corresponding SFN */
|
|
)
|
|
{
|
|
UINT i, s;
|
|
WCHAR wc;
|
|
|
|
|
|
dir[LDIR_Chksum] = sum; /* Set checksum */
|
|
dir[LDIR_Attr] = AM_LFN; /* Set attribute. LFN entry */
|
|
dir[LDIR_Type] = 0;
|
|
st_word(dir + LDIR_FstClusLO, 0);
|
|
|
|
i = (ord - 1) * 13; /* Get offset in the LFN working buffer */
|
|
s = wc = 0;
|
|
do {
|
|
if (wc != 0xFFFF) wc = lfn[i++]; /* Get an effective character */
|
|
st_word(dir + LfnOfs[s], wc); /* Put it */
|
|
if (wc == 0) wc = 0xFFFF; /* Padding characters for left locations */
|
|
} 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 */
|
|
}
|
|
|
|
#endif /* !_FS_READONLY */
|
|
#endif /* _USE_LFN != 0 */
|
|
|
|
|
|
|
|
#if _USE_LFN != 0 && !_FS_READONLY
|
|
/*-----------------------------------------------------------------------*/
|
|
/* FAT-LFN: Create a Numbered SFN */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
void gen_numname (
|
|
BYTE* dst, /* Pointer to the buffer to store numbered SFN */
|
|
const BYTE* src, /* Pointer to SFN */
|
|
const WCHAR* lfn, /* Pointer to LFN */
|
|
UINT seq /* Sequence number */
|
|
)
|
|
{
|
|
BYTE ns[8], c;
|
|
UINT i, j;
|
|
WCHAR wc;
|
|
DWORD sr;
|
|
|
|
|
|
mem_cpy(dst, src, 11);
|
|
|
|
if (seq > 5) { /* In case of many collisions, generate a hash number instead of sequential number */
|
|
sr = seq;
|
|
while (*lfn) { /* Create a CRC */
|
|
wc = *lfn++;
|
|
for (i = 0; i < 16; i++) {
|
|
sr = (sr << 1) + (wc & 1);
|
|
wc >>= 1;
|
|
if (sr & 0x10000) sr ^= 0x11021;
|
|
}
|
|
}
|
|
seq = (UINT)sr;
|
|
}
|
|
|
|
/* itoa (hexdecimal) */
|
|
i = 7;
|
|
do {
|
|
c = (BYTE)((seq % 16) + '0');
|
|
if (c > '9') c += 7;
|
|
ns[i--] = c;
|
|
seq /= 16;
|
|
} while (seq);
|
|
ns[i] = '~';
|
|
|
|
/* Append the number */
|
|
for (j = 0; j < i && dst[j] != ' '; j++) {
|
|
if (IsDBCS1(dst[j])) {
|
|
if (j == i - 1) break;
|
|
j++;
|
|
}
|
|
}
|
|
do {
|
|
dst[j++] = (i < 8) ? ns[i++] : ' ';
|
|
} while (j < 8);
|
|
}
|
|
#endif /* _USE_LFN != 0 && !_FS_READONLY */
|
|
|
|
|
|
|
|
#if _USE_LFN != 0
|
|
/*-----------------------------------------------------------------------*/
|
|
/* FAT-LFN: Calculate checksum of an SFN entry */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
BYTE sum_sfn (
|
|
const BYTE* dir /* Pointer to the SFN entry */
|
|
)
|
|
{
|
|
BYTE sum = 0;
|
|
UINT n = 11;
|
|
|
|
do {
|
|
sum = (sum >> 1) + (sum << 7) + *dir++;
|
|
} while (--n);
|
|
return sum;
|
|
}
|
|
|
|
#endif /* _USE_LFN != 0 */
|
|
|
|
|
|
|
|
#if _FS_EXFAT
|
|
/*-----------------------------------------------------------------------*/
|
|
/* exFAT: Checksum */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
WORD xdir_sum ( /* Get checksum of the directoly block */
|
|
const BYTE* dir /* Directory entry block to be calculated */
|
|
)
|
|
{
|
|
UINT i, szblk;
|
|
WORD sum;
|
|
|
|
|
|
szblk = (dir[XDIR_NumSec] + 1) * SZDIRE;
|
|
for (i = sum = 0; i < szblk; i++) {
|
|
if (i == XDIR_SetSum) { /* Skip sum field */
|
|
i++;
|
|
} else {
|
|
sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + dir[i];
|
|
}
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
|
|
|
|
static
|
|
WORD xname_sum ( /* Get check sum (to be used as hash) of the name */
|
|
const WCHAR* name /* File name to be calculated */
|
|
)
|
|
{
|
|
WCHAR chr;
|
|
WORD sum = 0;
|
|
|
|
|
|
while ((chr = *name++) != 0) {
|
|
chr = ff_wtoupper(chr); /* File name needs to be ignored case */
|
|
sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + (chr & 0xFF);
|
|
sum = ((sum & 1) ? 0x8000 : 0) + (sum >> 1) + (chr >> 8);
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
|
|
#if !_FS_READONLY && _USE_MKFS
|
|
static
|
|
DWORD xsum32 (
|
|
BYTE dat, /* Data to be sumed */
|
|
DWORD sum /* Previous value */
|
|
)
|
|
{
|
|
sum = ((sum & 1) ? 0x80000000 : 0) + (sum >> 1) + dat;
|
|
return sum;
|
|
}
|
|
#endif
|
|
|
|
|
|
#if _FS_MINIMIZE <= 1 || _FS_RPATH >= 2
|
|
/*------------------------------------------------------*/
|
|
/* exFAT: Get object information from a directory block */
|
|
/*------------------------------------------------------*/
|
|
|
|
static
|
|
void get_xdir_info (
|
|
BYTE* dirb, /* Pointer to the direcotry entry block 85+C0+C1s */
|
|
FILINFO* fno /* Buffer to store the extracted file information */
|
|
)
|
|
{
|
|
UINT di, si;
|
|
WCHAR w;
|
|
#if !_LFN_UNICODE
|
|
UINT nc;
|
|
#endif
|
|
|
|
/* Get file name */
|
|
di = 0;
|
|
#if _LFN_UNICODE
|
|
for (si = SZDIRE * 2; di < dirb[XDIR_NumName]; si += 2, di++) {
|
|
if ((si % SZDIRE) == 0) si += 2; /* Skip entry type field */
|
|
w = ld_word(dirb + si); /* Get a character */
|
|
if (di >= _MAX_LFN) { di = 0; break; } /* Buffer overflow --> inaccessible object name */
|
|
fno->fname[di] = w; /* Store it */
|
|
}
|
|
#else
|
|
for (si = SZDIRE * 2, nc = 0; nc < dirb[XDIR_NumName]; si += 2, nc++) {
|
|
if ((si % SZDIRE) == 0) si += 2; /* Skip entry type field */
|
|
w = ff_convert(ld_word(dirb + si), 0); /* Get a character and Unicode -> OEM */
|
|
if (_DF1S && w >= 0x100) { /* Is it a double byte char? (always false at SBCS cfg) */
|
|
fno->fname[di++] = (char)(w >> 8); /* Put 1st byte of the DBC */
|
|
}
|
|
if (w == 0 || di >= _MAX_LFN) { di = 0; break; } /* Invalid char or buffer overflow --> inaccessible object name */
|
|
fno->fname[di++] = (char)w;
|
|
}
|
|
#endif
|
|
if (di == 0) fno->fname[di++] = '?'; /* Inaccessible object name? */
|
|
fno->fname[di] = 0; /* Terminate file name */
|
|
|
|
fno->altname[0] = 0; /* No SFN */
|
|
fno->fattrib = dirb[XDIR_Attr]; /* Attribute */
|
|
fno->fsize = (fno->fattrib & AM_DIR) ? 0 : ld_qword(dirb + XDIR_FileSize); /* Size */
|
|
fno->ftime = ld_word(dirb + XDIR_ModTime + 0); /* Time */
|
|
fno->fdate = ld_word(dirb + XDIR_ModTime + 2); /* Date */
|
|
}
|
|
|
|
#endif /* _FS_MINIMIZE <= 1 || _FS_RPATH >= 2 */
|
|
|
|
|
|
/*-----------------------------------*/
|
|
/* exFAT: Get a directry entry block */
|
|
/*-----------------------------------*/
|
|
|
|
static
|
|
FRESULT load_xdir ( /* FR_INT_ERR: invalid entry block */
|
|
DIR* dp /* Pointer to the reading direcotry object pointing the 85 entry */
|
|
)
|
|
{
|
|
FRESULT res;
|
|
UINT i, sz_ent;
|
|
BYTE* dirb = dp->obj.fs->dirbuf; /* Pointer to the on-memory direcotry entry block 85+C0+C1s */
|
|
|
|
|
|
/* Load 85 entry */
|
|
res = move_window(dp->obj.fs, dp->sect);
|
|
if (res != FR_OK) return res;
|
|
if (dp->dir[XDIR_Type] != 0x85) return FR_INT_ERR;
|
|
mem_cpy(dirb + 0, dp->dir, SZDIRE);
|
|
sz_ent = (dirb[XDIR_NumSec] + 1) * SZDIRE;
|
|
if (sz_ent < 3 * SZDIRE || sz_ent > 19 * SZDIRE) return FR_INT_ERR;
|
|
|
|
/* Load C0 entry */
|
|
res = dir_next(dp, 0);
|
|
if (res != FR_OK) return res;
|
|
res = move_window(dp->obj.fs, dp->sect);
|
|
if (res != FR_OK) return res;
|
|
if (dp->dir[XDIR_Type] != 0xC0) return FR_INT_ERR;
|
|
mem_cpy(dirb + SZDIRE, dp->dir, SZDIRE);
|
|
if (MAXDIRB(dirb[XDIR_NumName]) > sz_ent) return FR_INT_ERR;
|
|
|
|
/* Load C1 entries */
|
|
i = SZDIRE * 2; /* C1 offset */
|
|
do {
|
|
res = dir_next(dp, 0);
|
|
if (res != FR_OK) return res;
|
|
res = move_window(dp->obj.fs, dp->sect);
|
|
if (res != FR_OK) return res;
|
|
if (dp->dir[XDIR_Type] != 0xC1) return FR_INT_ERR;
|
|
if (i < MAXDIRB(_MAX_LFN)) mem_cpy(dirb + i, dp->dir, SZDIRE);
|
|
} while ((i += SZDIRE) < sz_ent);
|
|
|
|
/* Sanity check (do it when accessible object name) */
|
|
if (i <= MAXDIRB(_MAX_LFN)) {
|
|
if (xdir_sum(dirb) != ld_word(dirb + XDIR_SetSum)) return FR_INT_ERR;
|
|
}
|
|
return FR_OK;
|
|
}
|
|
|
|
|
|
#if !_FS_READONLY || _FS_RPATH != 0
|
|
/*------------------------------------------------*/
|
|
/* exFAT: Load the object's directory entry block */
|
|
/*------------------------------------------------*/
|
|
static
|
|
FRESULT load_obj_dir (
|
|
DIR* dp, /* Blank directory object to be used to access containing direcotry */
|
|
const _FDID* obj /* Object with its containing directory information */
|
|
)
|
|
{
|
|
FRESULT res;
|
|
|
|
/* Open object containing directory */
|
|
dp->obj.fs = obj->fs;
|
|
dp->obj.sclust = obj->c_scl;
|
|
dp->obj.stat = (BYTE)obj->c_size;
|
|
dp->obj.objsize = obj->c_size & 0xFFFFFF00;
|
|
dp->blk_ofs = obj->c_ofs;
|
|
|
|
res = dir_sdi(dp, dp->blk_ofs); /* Goto object's entry block */
|
|
if (res == FR_OK) {
|
|
res = load_xdir(dp); /* Load the object's entry block */
|
|
}
|
|
return res;
|
|
}
|
|
#endif
|
|
|
|
|
|
#if !_FS_READONLY
|
|
/*-----------------------------------------------*/
|
|
/* exFAT: Store the directory block to the media */
|
|
/*-----------------------------------------------*/
|
|
static
|
|
FRESULT store_xdir (
|
|
DIR* dp /* Pointer to the direcotry object */
|
|
)
|
|
{
|
|
FRESULT res;
|
|
UINT nent;
|
|
BYTE* dirb = dp->obj.fs->dirbuf; /* Pointer to the direcotry entry block 85+C0+C1s */
|
|
|
|
/* Create set sum */
|
|
st_word(dirb + XDIR_SetSum, xdir_sum(dirb));
|
|
nent = dirb[XDIR_NumSec] + 1;
|
|
|
|
/* Store the set of directory to the volume */
|
|
res = dir_sdi(dp, dp->blk_ofs);
|
|
while (res == FR_OK) {
|
|
res = move_window(dp->obj.fs, dp->sect);
|
|
if (res != FR_OK) break;
|
|
mem_cpy(dp->dir, dirb, SZDIRE);
|
|
dp->obj.fs->wflag = 1;
|
|
if (--nent == 0) break;
|
|
dirb += SZDIRE;
|
|
res = dir_next(dp, 0);
|
|
}
|
|
return (res == FR_OK || res == FR_DISK_ERR) ? res : FR_INT_ERR;
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------*/
|
|
/* exFAT: Create a new directory enrty block */
|
|
/*-------------------------------------------*/
|
|
|
|
static
|
|
void create_xdir (
|
|
BYTE* dirb, /* Pointer to the direcotry entry block buffer */
|
|
const WCHAR* lfn /* Pointer to the nul terminated file name */
|
|
)
|
|
{
|
|
UINT i;
|
|
BYTE nb, nc;
|
|
WCHAR chr;
|
|
|
|
|
|
/* Create 85+C0 entry */
|
|
mem_set(dirb, 0, 2 * SZDIRE);
|
|
dirb[XDIR_Type] = 0x85;
|
|
dirb[XDIR_Type + SZDIRE] = 0xC0;
|
|
|
|
/* Create C1 entries */
|
|
nc = 0; nb = 1; chr = 1; i = SZDIRE * 2;
|
|
do {
|
|
dirb[i++] = 0xC1; dirb[i++] = 0; /* Entry type C1 */
|
|
do { /* Fill name field */
|
|
if (chr && (chr = lfn[nc]) != 0) nc++; /* Get a character if exist */
|
|
st_word(dirb + i, chr); /* Store it */
|
|
} while ((i += 2) % SZDIRE != 0);
|
|
nb++;
|
|
} while (lfn[nc]); /* Fill next entry if any char follows */
|
|
|
|
dirb[XDIR_NumName] = nc; /* Set name length */
|
|
dirb[XDIR_NumSec] = nb; /* Set block length */
|
|
st_word(dirb + XDIR_NameHash, xname_sum(lfn)); /* Set name hash */
|
|
}
|
|
|
|
#endif /* !_FS_READONLY */
|
|
#endif /* _FS_EXFAT */
|
|
|
|
|
|
|
|
#if _FS_MINIMIZE <= 1 || _FS_RPATH >= 2 || _USE_LABEL || _FS_EXFAT
|
|
/*-----------------------------------------------------------------------*/
|
|
/* Read an object from the directory */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
FRESULT dir_read (
|
|
DIR* dp, /* Pointer to the directory object */
|
|
int vol /* Filtered by 0:file/directory or 1:volume label */
|
|
)
|
|
{
|
|
FRESULT res = FR_NO_FILE;
|
|
FATFS *fs = dp->obj.fs;
|
|
BYTE a, c;
|
|
#if _USE_LFN != 0
|
|
BYTE ord = 0xFF, sum = 0xFF;
|
|
#endif
|
|
|
|
while (dp->sect) {
|
|
res = move_window(fs, dp->sect);
|
|
if (res != FR_OK) break;
|
|
c = dp->dir[DIR_Name]; /* Test for the entry type */
|
|
if (c == 0) {
|
|
res = FR_NO_FILE; break; /* Reached to end of the directory */
|
|
}
|
|
#if _FS_EXFAT
|
|
if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */
|
|
if (_USE_LABEL && vol) {
|
|
if (c == 0x83) break; /* Volume label entry? */
|
|
} else {
|
|
if (c == 0x85) { /* Start of the file entry block? */
|
|
dp->blk_ofs = dp->dptr; /* Get location of the block */
|
|
res = load_xdir(dp); /* Load the entry block */
|
|
if (res == FR_OK) {
|
|
dp->obj.attr = fs->dirbuf[XDIR_Attr] & AM_MASK; /* Get attribute */
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
} else
|
|
#endif
|
|
{ /* On the FAT12/16/32 volume */
|
|
dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK; /* Get attribute */
|
|
#if _USE_LFN != 0 /* LFN configuration */
|
|
if (c == DDEM || c == '.' || (int)((a & ~AM_ARC) == AM_VOL) != vol) { /* An entry without valid data */
|
|
ord = 0xFF;
|
|
} else {
|
|
if (a == AM_LFN) { /* An LFN entry is found */
|
|
if (c & LLEF) { /* Is it start of an LFN sequence? */
|
|
sum = dp->dir[LDIR_Chksum];
|
|
c &= (BYTE)~LLEF; ord = c;
|
|
dp->blk_ofs = dp->dptr;
|
|
}
|
|
/* Check LFN validity and capture it */
|
|
ord = (c == ord && sum == dp->dir[LDIR_Chksum] && pick_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF;
|
|
} else { /* An SFN entry is found */
|
|
if (ord || sum != sum_sfn(dp->dir)) { /* Is there a valid LFN? */
|
|
dp->blk_ofs = 0xFFFFFFFF; /* It has no LFN. */
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
#else /* Non LFN configuration */
|
|
if (c != DDEM && c != '.' && a != AM_LFN && (int)((a & ~AM_ARC) == AM_VOL) == vol) { /* Is it a valid entry? */
|
|
break;
|
|
}
|
|
#endif
|
|
}
|
|
res = dir_next(dp, 0); /* Next entry */
|
|
if (res != FR_OK) break;
|
|
}
|
|
|
|
if (res != FR_OK) dp->sect = 0; /* Terminate the read operation on error or EOT */
|
|
return res;
|
|
}
|
|
|
|
#endif /* _FS_MINIMIZE <= 1 || _USE_LABEL || _FS_RPATH >= 2 */
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
/* Directory handling - Find an object in the directory */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
FRESULT dir_find ( /* FR_OK(0):succeeded, !=0:error */
|
|
DIR* dp /* Pointer to the directory object with the file name */
|
|
)
|
|
{
|
|
FRESULT res;
|
|
FATFS *fs = dp->obj.fs;
|
|
BYTE c;
|
|
#if _USE_LFN != 0
|
|
BYTE a, ord, sum;
|
|
#endif
|
|
|
|
res = dir_sdi(dp, 0); /* Rewind directory object */
|
|
if (res != FR_OK) return res;
|
|
#if _FS_EXFAT
|
|
if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */
|
|
BYTE nc;
|
|
UINT di, ni;
|
|
WORD hash = xname_sum(fs->lfnbuf); /* Hash value of the name to find */
|
|
|
|
while ((res = dir_read(dp, 0)) == FR_OK) { /* Read an item */
|
|
#if _MAX_LFN < 255
|
|
if (fs->dirbuf[XDIR_NumName] > _MAX_LFN) continue; /* Skip comparison if inaccessible object name */
|
|
#endif
|
|
if (ld_word(fs->dirbuf + XDIR_NameHash) != hash) continue; /* Skip comparison if hash mismatched */
|
|
for (nc = fs->dirbuf[XDIR_NumName], di = SZDIRE * 2, ni = 0; nc; nc--, di += 2, ni++) { /* Compare the name */
|
|
if ((di % SZDIRE) == 0) di += 2;
|
|
if (ff_wtoupper(ld_word(fs->dirbuf + di)) != ff_wtoupper(fs->lfnbuf[ni])) break;
|
|
}
|
|
if (nc == 0 && !fs->lfnbuf[ni]) break; /* Name matched? */
|
|
}
|
|
return res;
|
|
}
|
|
#endif
|
|
/* On the FAT12/16/32 volume */
|
|
#if _USE_LFN != 0
|
|
ord = sum = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */
|
|
#endif
|
|
do {
|
|
res = move_window(fs, dp->sect);
|
|
if (res != FR_OK) break;
|
|
c = dp->dir[DIR_Name];
|
|
if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
|
|
#if _USE_LFN != 0 /* LFN configuration */
|
|
dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK;
|
|
if (c == DDEM || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */
|
|
ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */
|
|
} else {
|
|
if (a == AM_LFN) { /* An LFN entry is found */
|
|
if (!(dp->fn[NSFLAG] & NS_NOLFN)) {
|
|
if (c & LLEF) { /* Is it start of LFN sequence? */
|
|
sum = dp->dir[LDIR_Chksum];
|
|
c &= (BYTE)~LLEF; ord = c; /* LFN start order */
|
|
dp->blk_ofs = dp->dptr; /* Start offset of LFN */
|
|
}
|
|
/* Check validity of the LFN entry and compare it with given name */
|
|
ord = (c == ord && sum == dp->dir[LDIR_Chksum] && cmp_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF;
|
|
}
|
|
} else { /* An SFN entry is found */
|
|
if (!ord && sum == sum_sfn(dp->dir)) break; /* LFN matched? */
|
|
if (!(dp->fn[NSFLAG] & NS_LOSS) && !mem_cmp(dp->dir, dp->fn, 11)) break; /* SFN matched? */
|
|
ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF; /* Reset LFN sequence */
|
|
}
|
|
}
|
|
#else /* Non LFN configuration */
|
|
dp->obj.attr = dp->dir[DIR_Attr] & AM_MASK;
|
|
if (!(dp->dir[DIR_Attr] & AM_VOL) && !mem_cmp(dp->dir, dp->fn, 11)) break; /* Is it a valid entry? */
|
|
#endif
|
|
res = dir_next(dp, 0); /* Next entry */
|
|
} while (res == FR_OK);
|
|
|
|
return res;
|
|
}
|
|
|
|
|
|
|
|
|
|
#if !_FS_READONLY
|
|
/*-----------------------------------------------------------------------*/
|
|
/* Register an object to the directory */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
FRESULT dir_register ( /* FR_OK:succeeded, FR_DENIED:no free entry or too many SFN collision, FR_DISK_ERR:disk error */
|
|
DIR* dp /* Target directory with object name to be created */
|
|
)
|
|
{
|
|
FRESULT res;
|
|
FATFS *fs = dp->obj.fs;
|
|
#if _USE_LFN != 0 /* LFN configuration */
|
|
UINT n, nlen, nent;
|
|
BYTE sn[12], sum;
|
|
|
|
|
|
if (dp->fn[NSFLAG] & (NS_DOT | NS_NONAME)) return FR_INVALID_NAME; /* Check name validity */
|
|
for (nlen = 0; fs->lfnbuf[nlen]; nlen++) ; /* Get lfn length */
|
|
|
|
#if _FS_EXFAT
|
|
if (fs->fs_type == FS_EXFAT) { /* On the exFAT volume */
|
|
DIR dj;
|
|
|
|
nent = (nlen + 14) / 15 + 2; /* Number of entries to allocate (85+C0+C1s) */
|
|
res = dir_alloc(dp, nent); /* Allocate entries */
|
|
if (res != FR_OK) return res;
|
|
dp->blk_ofs = dp->dptr - SZDIRE * (nent - 1); /* Set the allocated entry block offset */
|
|
|
|
if (dp->obj.sclust != 0 && (dp->obj.stat & 4)) { /* Has the sub-directory been stretched? */
|
|
dp->obj.objsize += (DWORD)fs->csize * SS(fs); /* Increase the directory size by cluster size */
|
|
res = fill_first_frag(&dp->obj); /* Fill first fragment on the FAT if needed */
|
|
if (res != FR_OK) return res;
|
|
res = fill_last_frag(&dp->obj, dp->clust, 0xFFFFFFFF); /* Fill last fragment on the FAT if needed */
|
|
if (res != FR_OK) return res;
|
|
res = load_obj_dir(&dj, &dp->obj); /* Load the object status */
|
|
if (res != FR_OK) return res;
|
|
st_qword(fs->dirbuf + XDIR_FileSize, dp->obj.objsize); /* Update the allocation status */
|
|
st_qword(fs->dirbuf + XDIR_ValidFileSize, dp->obj.objsize);
|
|
fs->dirbuf[XDIR_GenFlags] = dp->obj.stat | 1;
|
|
res = store_xdir(&dj); /* Store the object status */
|
|
if (res != FR_OK) return res;
|
|
}
|
|
|
|
create_xdir(fs->dirbuf, fs->lfnbuf); /* Create on-memory directory block to be written later */
|
|
return FR_OK;
|
|
}
|
|
#endif
|
|
/* On the FAT12/16/32 volume */
|
|
mem_cpy(sn, dp->fn, 12);
|
|
if (sn[NSFLAG] & NS_LOSS) { /* When LFN is out of 8.3 format, generate a numbered name */
|
|
dp->fn[NSFLAG] = NS_NOLFN; /* Find only SFN */
|
|
for (n = 1; n < 100; n++) {
|
|
gen_numname(dp->fn, sn, fs->lfnbuf, n); /* Generate a numbered name */
|
|
res = dir_find(dp); /* Check if the name collides with existing SFN */
|
|
if (res != FR_OK) break;
|
|
}
|
|
if (n == 100) return FR_DENIED; /* Abort if too many collisions */
|
|
if (res != FR_NO_FILE) return res; /* Abort if the result is other than 'not collided' */
|
|
dp->fn[NSFLAG] = sn[NSFLAG];
|
|
}
|
|
|
|
/* Create an SFN with/without LFNs. */
|
|
nent = (sn[NSFLAG] & NS_LFN) ? (nlen + 12) / 13 + 1 : 1; /* Number of entries to allocate */
|
|
res = dir_alloc(dp, nent); /* Allocate entries */
|
|
if (res == FR_OK && --nent) { /* Set LFN entry if needed */
|
|
res = dir_sdi(dp, dp->dptr - nent * SZDIRE);
|
|
if (res == FR_OK) {
|
|
sum = sum_sfn(dp->fn); /* Checksum value of the SFN tied to the LFN */
|
|
do { /* Store LFN entries in bottom first */
|
|
res = move_window(fs, dp->sect);
|
|
if (res != FR_OK) break;
|
|
put_lfn(fs->lfnbuf, dp->dir, (BYTE)nent, sum);
|
|
fs->wflag = 1;
|
|
res = dir_next(dp, 0); /* Next entry */
|
|
} while (res == FR_OK && --nent);
|
|
}
|
|
}
|
|
|
|
#else /* Non LFN configuration */
|
|
res = dir_alloc(dp, 1); /* Allocate an entry for SFN */
|
|
|
|
#endif
|
|
|
|
/* Set SFN entry */
|
|
if (res == FR_OK) {
|
|
res = move_window(fs, dp->sect);
|
|
if (res == FR_OK) {
|
|
mem_set(dp->dir, 0, SZDIRE); /* Clean the entry */
|
|
mem_cpy(dp->dir + DIR_Name, dp->fn, 11); /* Put SFN */
|
|
#if _USE_LFN != 0
|
|
dp->dir[DIR_NTres] = dp->fn[NSFLAG] & (NS_BODY | NS_EXT); /* Put NT flag */
|
|
#endif
|
|
fs->wflag = 1;
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
#endif /* !_FS_READONLY */
|
|
|
|
|
|
|
|
#if !_FS_READONLY && _FS_MINIMIZE == 0
|
|
/*-----------------------------------------------------------------------*/
|
|
/* Remove an object from the directory */
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
static
|
|
FRESULT dir_remove ( /* FR_OK:Succeeded, FR_DISK_ERR:A disk error */
|
|
DIR* dp /* Directory object pointing the entry to be removed */
|
|
)
|
|
{
|
|
FRESULT res;
|
|
FATFS *fs = dp->obj.fs;
|
|
#if _USE_LFN != 0 /* LFN configuration */
|
|
DWORD last = dp->dptr;
|
|
|
|
res = (dp->blk_ofs == 0xFFFFFFFF) ? FR_OK : dir_sdi(dp, dp->blk_ofs); /* Goto top of the entry block if LFN is exist */
|
|
if (res == FR_OK) {
|
|
do {
|
|
res = move_window(fs, dp->sect);
|
|
if (res != FR_OK) break;
|
|
/* Mark an entry 'deleted' */
|
|
if (_FS_EXFAT && fs->fs_type == FS_EXFAT) { /* On the exFAT volume */
|
|
dp->dir[XDIR_Type] &= 0x7F;
|
|
} else { /* On the FAT12/16/32 volume */
|
|
dp->dir[DIR_Name] = DDEM;
|
|
}
|
|
fs->wflag = 1;
|
|
if (dp->dptr >= last) break; /* If reached last entry then all entries of the object has been deleted. */
|
|
res = dir_next(dp, 0); /* Next entry */
|
|
} while (res == FR_OK);
|
|
if (res == FR_NO_FILE) res = FR_INT_ERR |