diff options
Diffstat (limited to 'nuttx/fs/fat')
-rw-r--r-- | nuttx/fs/fat/Kconfig | 66 | ||||
-rw-r--r-- | nuttx/fs/fat/Make.defs | 53 | ||||
-rw-r--r-- | nuttx/fs/fat/fs_configfat.c | 963 | ||||
-rw-r--r-- | nuttx/fs/fat/fs_fat32.c | 2412 | ||||
-rw-r--r-- | nuttx/fs/fat/fs_fat32.h | 937 | ||||
-rw-r--r-- | nuttx/fs/fat/fs_fat32attrib.c | 189 | ||||
-rw-r--r-- | nuttx/fs/fat/fs_fat32dirent.c | 2947 | ||||
-rw-r--r-- | nuttx/fs/fat/fs_fat32util.c | 1848 | ||||
-rw-r--r-- | nuttx/fs/fat/fs_mkfatfs.c | 312 | ||||
-rw-r--r-- | nuttx/fs/fat/fs_mkfatfs.h | 170 | ||||
-rw-r--r-- | nuttx/fs/fat/fs_writefat.c | 544 |
11 files changed, 0 insertions, 10441 deletions
diff --git a/nuttx/fs/fat/Kconfig b/nuttx/fs/fat/Kconfig deleted file mode 100644 index 1de613ce4..000000000 --- a/nuttx/fs/fat/Kconfig +++ /dev/null @@ -1,66 +0,0 @@ -# -# For a description of the syntax of this configuration file, -# see misc/tools/kconfig-language.txt. -# - -config FS_FAT - bool "FAT file system" - default n - depends on !DISABLE_MOUNTPOINT - ---help--- - Enable FAT filesystem support - -if FS_FAT -config FAT_LCNAMES - bool "FAT upper/lower names" - default n - ---help--- - Enable use of the NT-style upper/lower case 8.3 - file name support. - -config FAT_LFN - bool "FAT long file names" - default n - ---help--- - Enable FAT long file names. NOTE: Microsoft claims - patents on FAT long file name technology. Please read the - disclaimer in the top-level COPYING file and only enable this - feature if you understand these issues. - -config FAT_MAXFNAME - int "FAT maximum file name size" - depends on FAT_LFN - ---help--- - If CONFIG_FAT_LFN is defined, then the default, maximum long file - name is 255 bytes. This can eat up a lot of memory (especially stack - space). If you are willing to live with some non-standard, short long - file names, then define this value to be something more reasonable. A - good choice would be the same value as selected for NAME_MAX which will - limit the visibility of longer file names anyway. - -config FS_FATTIME - bool "FAT timestamps" - default n - ---help--- - Support FAT date and time. NOTE: There is not - much sense in supporting FAT date and time unless you have a - hardware RTC or other way to get the time and date. - -config FAT_DMAMEMORY - bool "DMA memory allocator" - default n - ---help--- - The FAT file system allocates two I/O buffers for data transfer, each - are the size of one device sector. One of the buffers is allocated - once for each FAT volume that is mounted; the other buffers are - allocated each time a FAT file is opened. - - Some hardware, however, may require special DMA-capable memory in - order to perform the the transfers. If FAT_DMAMEMORY is defined - then the architecture-specific hardware must provide the funtions - fat_dma_alloc() and fat_dma_free(): fat_dmalloc() will allocate - DMA-capable memory of the specified size; fat_dmafree() is the - corresponding function that will be called to free the DMA-capable - memory. - -endif diff --git a/nuttx/fs/fat/Make.defs b/nuttx/fs/fat/Make.defs deleted file mode 100644 index 2769ab602..000000000 --- a/nuttx/fs/fat/Make.defs +++ /dev/null @@ -1,53 +0,0 @@ -############################################################################ -# Make.defs -# -# Copyright (C) 2008, 2011, 2013 Gregory Nutt. All rights reserved. -# Author: Gregory Nutt <gnutt@nuttx.org> -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: -# -# 1. Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# 2. Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in -# the documentation and/or other materials provided with the -# distribution. -# 3. Neither the name Nuttx nor the names of its contributors may be -# used to endorse or promote products derived from this software -# without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS -# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, -# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS -# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED -# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN -# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -# POSSIBILITY OF SUCH DAMAGE. -# -############################################################################ - -ifeq ($(CONFIG_FS_FAT),y) -# Files required for FAT file system support - -ASRCS += -CSRCS += fs_fat32.c fs_fat32dirent.c fs_fat32attrib.c fs_fat32util.c - -# Files required for mkfatfs utility function - -ASRCS += -CSRCS += fs_mkfatfs.c fs_configfat.c fs_writefat.c - -# Include FAT build support - -DEPPATH += --dep-path fat -VPATH += :fat -CFLAGS += ${shell $(INCDIR) $(INCDIROPT) "$(CC)" $(TOPDIR)$(DELIM)fs$(DELIM)fat} - -endif diff --git a/nuttx/fs/fat/fs_configfat.c b/nuttx/fs/fat/fs_configfat.c deleted file mode 100644 index 04141ee2b..000000000 --- a/nuttx/fs/fat/fs_configfat.c +++ /dev/null @@ -1,963 +0,0 @@ -/**************************************************************************** - * fs/fat/fs_configfat.c - * - * Copyright (C) 2008-2009 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt <gnutt@nuttx.org> - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name NuttX nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <nuttx/config.h> - -#include <stdint.h> -#include <string.h> -#include <debug.h> -#include <errno.h> - -#include <nuttx/fs/fs.h> -#include <nuttx/fs/fat.h> -#include <nuttx/fs/mkfatfs.h> - -#include "fs_internal.h" -#include "fs_fat32.h" -#include "fs_mkfatfs.h" - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#define NDX12 0 -#define NDX16 1 -#define NDX32 2 - -#define fatconfig12 fatconfig[NDX12] -#define fatconfig16 fatconfig[NDX16] -#define fatconfig32 fatconfig[NDX32] - -/* JMP rel8 and NOP opcodes */ - -#define OPCODE_JMP_REL8 0xeb -#define OPCODE_NOP 0x90 - -#define BOOTCODE_MSGOFFSET 29 - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -struct fat_config_s -{ - uint32_t fc_navailsects; /* The number of available sectors */ - uint32_t fc_nfatsects; /* The number of sectors in one FAT */ - uint32_t fc_nclusters; /* The number of clusters in the filesystem */ - uint32_t fc_rsvdseccount; /* The number of reserved sectors */ -}; - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/* Reverse engineered, generic boot message logic for non-bootable disk. - * Message begins at offset 29; Sector relative offset must be poked into - * offset 3. - */ - -static uint8_t g_bootcodeblob[] = -{ - 0x0e, 0x1f, 0xbe, 0x00, 0x7c, 0xac, 0x22, 0xc0, 0x74, 0x0b, 0x56, - 0xb4, 0x0e, 0xbb, 0x07, 0x00, 0xcd, 0x10, 0x5e, 0xeb, 0xf0, 0x32, - 0xe4, 0xcd, 0x16, 0xcd, 0x19, 0xeb, 0xfe, 0x54, 0x68, 0x69, 0x73, - 0x20, 0x69, 0x73, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x61, 0x20, 0x62, - 0x6f, 0x6f, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x64, 0x69, 0x73, - 0x6b, 0x2e, 0x20, 0x20, 0x50, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x20, - 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x20, 0x61, 0x20, 0x62, 0x6f, - 0x6f, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x66, 0x6c, 0x6f, 0x70, - 0x70, 0x79, 0x20, 0x61, 0x6e, 0x64, 0x0d, 0x0a, 0x70, 0x72, 0x65, - 0x73, 0x73, 0x20, 0x61, 0x6e, 0x79, 0x20, 0x6b, 0x65, 0x79, 0x20, - 0x74, 0x6f, 0x20, 0x74, 0x72, 0x79, 0x20, 0x61, 0x67, 0x61, 0x69, - 0x6e, 0x20, 0x2e, 0x2e, 0x2e, 0x0d, 0x0a, 0x00 -}; - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: mkfatfs_nfatsect12 - * - * Description: - * Calculate the number of sectors need for one fat in a FAT12 file system. - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * navailsects - The number of sectors available for both FAT and data. - * This is a precalculated value equal to the total number of sectors - * minus the number of root directory sectors and minus the number of - * reserved sectors. - * - * Return: - * 0: That calculation would have overflowed - * >0: The size of one FAT in sectors. - * - ****************************************************************************/ -static inline uint32_t -mkfatfs_nfatsect12(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, - uint32_t navailsects) -{ -#ifdef CONFIG_HAVE_LONG_LONG - uint64_t denom; - uint64_t numer; -#else - uint32_t denom; - uint32_t numer; -#endif - - /* For FAT12, the cluster number is held in a 12-bit number or 1.5 bytes per - * cluster reference. So each FAT sector will hold sectorsize/1.5 cluster - * references (except for the first sector of each FAT which has two reserved - * 12-bit values). And the total number of FAT sectors needed is: - * - * nfatsects = (1.5 * (ndataclust + 2) / sectorsize) - * - * where: - * - * ndataclust = ndatasect / clustsize - * nvailsects = nfatsects + ndatasect - * - * The solution to this set of linear equations is: - * - * nfatsects = (3 * navailsects + 6 * clustersize) / - * (3 * nfats + 2 * sectorsize * clustersize) - * - * The numerator would overflow uint32_t if: - * - * 3 * navailsects + 6 * clustersize > 0xffffffff - * - * Or - * - * navailsects > 0x55555555 - 2 * clustersize - */ - -#ifndef CONFIG_HAVE_LONG_LONG - if (navailsects <= (0x55555555 - (1 << (fmt->ff_clustshift + 1)))) - { -#endif - - denom = (fmt->ff_nfats << 1) + fmt->ff_nfats - + (var->fv_sectorsize << (fmt->ff_clustshift + 1)); - numer = (navailsects << 1) + navailsects - + (1 << (fmt->ff_clustshift + 2)) + (1 << (fmt->ff_clustshift + 1)); - return (uint32_t)((numer + denom - 1) / denom); - -#ifndef CONFIG_HAVE_LONG_LONG - } - else - { - return 0; - } -#endif -} - -/**************************************************************************** - * Name: mkfatfs_nfatsect16 - * - * Description: - * Calculate the number of sectors need for one fat in a FAT16 file system. - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * navailsects - The number of sectors available for both FAT and data. - * This is a precalculated value equal to the total number of sectors - * minus the number of root directory sectors and minus the number of - * reserved sectors. - * - * Return: - * The size of one FAT in sectors. - * - ****************************************************************************/ -static inline uint32_t -mkfatfs_nfatsect16(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, - uint32_t navailsects) -{ -#ifdef CONFIG_HAVE_LONG_LONG - uint64_t denom; - uint64_t numer; -#else - uint32_t denom; - uint32_t numer; -#endif - - /* For FAT16, the cluster number is held in a 16-bit number or 2 bytes per - * cluster reference. So each FAT sector will hold sectorsize/2 cluster - * references (except for the first sector of each FAT which has two reserved - * 16-bit values). And the total number of FAT sectors needed is: - * - * nfatsects = (2 * (ndataclust + 2) / sectorsize) - * - * where: - * - * ndataclust = ndatasect / clustsize - * nvailsects = nfatsects + ndatasect - * - * The solution to this set of linear equations is: - * - * nfatsects = (navailsects + 2 * clustersize) / - * (nfats + sectorsize * clustersize / 2) - * - * Overflow in the calculation of the numerator could occur if: - * - * navailsects > 0xffffffff - 2 * clustersize - */ - - if (fmt->ff_clustshift == 0) - { - denom = fmt->ff_nfats + (var->fv_sectorsize >> 1); - numer = navailsects + 2; - } - else - { - denom = fmt->ff_nfats + (var->fv_sectorsize << (fmt->ff_clustshift - 1)); - numer = navailsects + (1 << (fmt->ff_clustshift + 1)); - } - return (uint32_t)((numer + denom - 1) / denom); -} - -/**************************************************************************** - * Name: mkfatfs_nfatsect32 - * - * Description: - * Calculate the number of sectors need for one fat in a FAT32 file system. - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * navailsects - The number of sectors available for both FAT and data. - * This is a precalculated value equal to the total number of sectors - * minus the number of root directory sectors and minus the number of - * reserved sectors. - * - * Return: - * The size of one FAT in sectors. - * - ****************************************************************************/ -static inline uint32_t -mkfatfs_nfatsect32(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, - uint32_t navailsects) -{ -#ifdef CONFIG_HAVE_LONG_LONG - uint64_t denom; - uint64_t numer; -#else - uint32_t denom; - uint32_t numer; -#endif - - /* For FAT32, the cluster number is held in a 32-bit number or 4 bytes per - * cluster reference. So each FAT sector will hold sectorsize/4 cluster - * references (except for the first sector of each FAT which has three reserved - * 32-bit values). And the total number of FAT sectors needed is: - * - * nfatsects = (4 * (ndataclust + 3) / sectorsize) - * - * where: - * - * ndataclust = ndatasect / clustsize - * nvailsects = nfatsects + ndatasect - * - * The solution to this set of linear equations is: - * - * nfatsects = (navailsects + 3 * clustersize) / - * (nfats + sectorsize * clustersize / 4) - * - * Overflow in the 32-bit calculation of the numerator could occur if: - * - * navailsects > 0xffffffff - 3 * clustersize - */ - - if (fmt->ff_clustshift == 0) - { - denom = fmt->ff_nfats + (var->fv_sectorsize >> 2); - numer = navailsects + 3; - } - else if (fmt->ff_clustshift == 1) - { - denom = fmt->ff_nfats + (var->fv_sectorsize >> 1); - numer = navailsects + 6; - } - else - { - denom = fmt->ff_nfats + (var->fv_sectorsize << (fmt->ff_clustshift - 2)); - numer = navailsects + (1 << (fmt->ff_clustshift + 1)) + (1 << fmt->ff_clustshift); - } - return (uint32_t)((numer + denom - 1) / denom); -} - -/**************************************************************************** - * Name: mkfatfs_clustersearchlimits - * - * Description: - * Pick the starting and ending cluster size to use in the search for the - * the optimal cluster size. - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * - * Return: - * Starting cluster size is set in fmt->ff_clustshift; Final cluster - * size is the returned value. - * - ****************************************************************************/ -static inline uint8_t -mkfatfs_clustersearchlimits(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var) -{ - uint8_t mxclustshift; - - /* Did the caller already pick the cluster size? If not, the clustshift value - * will be 0xff - */ - - if (fmt->ff_clustshift == 0xff) - { - /* Pick a starting size based on the number of sectors on the device */ - - if (fmt->ff_nsectors < 2048) - { - /* 2k sectors, start wit 1 sector/cluster. */ - fmt->ff_clustshift = 0; - } - else if (fmt->ff_nsectors < 4096) - { - /* 4k sectors, start with 2 sector/cluster. */ - fmt->ff_clustshift = 1; - } - else if (fmt->ff_nsectors < 8192) - { - /* 8k sectors, start with 4 sector/cluster. */ - fmt->ff_clustshift = 2; - } - else if (fmt->ff_nsectors < 16384) - { - /* 16k sectors, start with 8 sector/cluster. */ - fmt->ff_clustshift = 3; - } - else if (fmt->ff_nsectors < 32768) - { - /* 32k sectors, start with 16 sector/cluster. */ - fmt->ff_clustshift = 4; - } - else - { - /* Otherwise, 32 sector/cluster. */ - fmt->ff_clustshift = 5; - } - - /* Wherever the search starts, it will end with the maximum of - * 128 sectors per cluster - */ - - mxclustshift = 7; - } - else - { - /* The caller has selected a cluster size. There will be no search! - * Just set the maximum to the caller specificed value. - */ - - mxclustshift = fmt->ff_clustshift; - } - return mxclustshift; -} - -/**************************************************************************** - * Name: mkfatfs_tryfat12 - * - * Description: - * Try to define a FAT12 filesystem on the device using the candidate - * sectors per cluster - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * fatconfig - FAT12-specific configuration - * - * Return: - * Zero on success configuration of a FAT12 file system; negated errno - * on failure - * - ****************************************************************************/ -static inline int -mkfatfs_tryfat12(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, - FAR struct fat_config_s *config) -{ - uint32_t maxnclusters; - - /* Calculate the number sectors in one FAT required to access all of the - * available sectors. - */ - - config->fc_nfatsects = mkfatfs_nfatsect12(fmt, var, config->fc_navailsects); - if (config->fc_nfatsects > 0) - { - /* Calculate the number of clusters available given the number of available - * sectors and the number of those that will be used for FAT: - */ - - config->fc_nclusters = - (config->fc_navailsects - - fmt->ff_nfats * config->fc_nfatsects) >> fmt->ff_clustshift; - - /* Calculate the maximum number of clusters that could be supported by a - * FAT of this size. - * - * maxnclusters = nfatsects * sectorsize / 1.5 - 2 - */ - - maxnclusters = (config->fc_nfatsects >> (var->fv_sectshift + 1)) / 3; - if (maxnclusters > FAT_MAXCLUST12) - { - maxnclusters = FAT_MAXCLUST12; - } - fvdbg("nfatsects=%u nclusters=%u (max=%u)\n", - config->fc_nfatsects, config->fc_nclusters, maxnclusters); - - /* Check if this number of clusters would overflow the maximum for - * FAT12 (remembering that two FAT cluster slots are reserved). - */ - - if (config->fc_nclusters + 2 > maxnclusters) - { - fvdbg("Too many clusters for FAT12\n"); - return -ENFILE; - } - } - return 0; -} - -/**************************************************************************** - * Name: mkfatfs_tryfat16 - * - * Description: - * Try to define a FAT16 filesystem on the device using the candidate - * sectors per cluster - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * fatconfig - FAT16-specific configuration - * - * Return: - * Zero on success configuration of a FAT16 file system; negated errno - * on failure - * - ****************************************************************************/ -static inline int -mkfatfs_tryfat16(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, - FAR struct fat_config_s *config) -{ - uint32_t maxnclusters; - - /* Calculate the number sectors in one FAT required to access all of the - * available sectors. - */ - - config->fc_nfatsects = mkfatfs_nfatsect16(fmt, var, config->fc_navailsects); - if (config->fc_nfatsects > 0) - { - /* Calculate the number of clusters available given the number of available - * sectors and the number of those that will be used for FAT: - */ - - config->fc_nclusters = - (config->fc_navailsects - - fmt->ff_nfats * config->fc_nfatsects) >> fmt->ff_clustshift; - - /* Calculate the maximum number of clusters that could be supported by a - * FAT of this size. - * - * maxnclusters = nfatsects * sectorsize / 2 - 2 - */ - - maxnclusters = config->fc_nfatsects << (var->fv_sectorsize - 1); - if (maxnclusters > FAT_MAXCLUST16) - { - maxnclusters = FAT_MAXCLUST16; - } - fvdbg("nfatsects=%u nclusters=%u (min=%u max=%u)\n", - config->fc_nfatsects, config->fc_nclusters, FAT_MINCLUST16, maxnclusters); - - /* Check if this number of clusters would overflow the maximum for - * FAT16 (remembering that two FAT cluster slots are reserved). - * Check the lower limit as well. The FAT12 is distinguished from FAT16 - * by comparing the number of clusters on the device agains a known - * threshold. If a small FAT16 file system were created, then it would - * be confused as a FAT12 at mount time. - */ - - if ((config->fc_nclusters + 2 > maxnclusters) || - (config->fc_nclusters < FAT_MINCLUST16)) - { - fvdbg("Too few or too many clusters for FAT16\n"); - return -ENFILE; - } - } - return 0; -} - -/**************************************************************************** - * Name: mkfatfs_tryfat32 - * - * Description: - * Try to define a FAT32 filesystem on the device using the candidate - * sectors per cluster - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * fatconfig - FAT32-specific configuration - * - * Return: - * Zero on success configuration of a FAT32 file system; negated errno - * on failure - * - ****************************************************************************/ -static inline int -mkfatfs_tryfat32(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, - FAR struct fat_config_s *config) -{ - uint32_t maxnclusters; - - /* Calculate the number sectors in one FAT required to access all of the - * available sectors. - */ - - config->fc_nfatsects = mkfatfs_nfatsect32(fmt, var, config->fc_navailsects); - if (config->fc_nfatsects > 0) - { - /* Calculate the number of clusters available given the number of available - * sectors and the number of those that will be used for FAT: - */ - - config->fc_nclusters = - (config->fc_navailsects - - fmt->ff_nfats * config->fc_nfatsects) >> fmt->ff_clustshift; - - /* Calculate the maximum number of clusters that could be supported by a - * FAT of this size. - * - * maxnclusters = nfatsects * sectorsize / 4 - 2 - */ - - maxnclusters = (config->fc_nfatsects << (var->fv_sectshift - 2)); - if (maxnclusters > FAT_MAXCLUST32) - { - maxnclusters = FAT_MAXCLUST32; - } - fvdbg("nfatsects=%u nclusters=%u (max=%u)\n", - config->fc_nfatsects, config->fc_nclusters, maxnclusters); - - /* Check if this number of clusters would overflow the maximum for - * FAT32 (remembering that two FAT cluster slots are reserved). - */ - - if ((config->fc_nclusters + 3 > maxnclusters) || - (config->fc_nclusters < FAT_MINCLUST32 && fmt->ff_fattype != 32)) - { - fvdbg("Too few or too many clusters for FAT32\n"); - return -ENFILE; - } - } - return 0; -} - -/**************************************************************************** - * Name: mkfatfs_selectfat - * - * Description: - * The cluster search has succeeded, select the specified FAT FS - * - * Input: - * fattype - The FAT size selected - * fmt - Caller specified format parameters - * var - Format parameters that are not caller specifiable. - * - * Return: - * None - * - ****************************************************************************/ - -static inline void -mkfatfs_selectfat(int fattype, FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var, FAR struct fat_config_s *config) -{ - /* Return the appropriate information about the selected file system. */ - - fdbg("Selected FAT%d\n", fattype); - var->fv_fattype = fattype; - var->fv_nclusters = config->fc_nclusters; - var->fv_nfatsects = config->fc_nfatsects; - fmt->ff_rsvdseccount = config->fc_rsvdseccount; -} - -/**************************************************************************** - * Name: mkfatfs_clustersearch - * - * Description: - * Search to find the smallest (reasonable) cluster size for the FAT file - * system. - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ - -static inline int -mkfatfs_clustersearch(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var) -{ - struct fat_config_s fatconfig[3]; - uint8_t mxclustshift; - - memset(fatconfig, 0, 3*sizeof(struct fat_config_s)); - - /* Select the reserved sector count for each FAT size */ - - if (fmt->ff_rsvdseccount) - { - fatconfig12.fc_rsvdseccount = fmt->ff_rsvdseccount; - fatconfig16.fc_rsvdseccount = fmt->ff_rsvdseccount; - - if (fmt->ff_rsvdseccount < 2) - { - fvdbg("At least 2 reserved sectors needed by FAT32\n"); - fatconfig32.fc_rsvdseccount = 2; - } - else - { - fatconfig32.fc_rsvdseccount = fmt->ff_rsvdseccount; - } - } - else - { - fatconfig12.fc_rsvdseccount = 1; - fatconfig16.fc_rsvdseccount = 1; - fatconfig32.fc_rsvdseccount = 32; - } - - /* Determine the number of sectors needed by the root directory. - * This is a constant value, independent of cluster size for FAT12/16 - */ - - if (var->fv_fattype != 32) - { - /* Calculate the number of sectors reqired to contain the selected - * number of root directory entries. This value is save in the var - * structure but will be overwritten if FAT32 is selected. FAT32 uses - * a cluster chain for the root directory, so the concept of the number - * of root directory entries does not apply to FAT32 - */ - - var->fv_nrootdirsects = - ((fmt->ff_rootdirentries << DIR_SHIFT) + var->fv_sectorsize - 1) >> var->fv_sectshift; - - /* The number of data sectors available (includes the fat itself) - * This value is a constant for FAT12/16, but not FAT32 because the - * size of the root directory cluster changes - */ - - fatconfig12.fc_navailsects = - fatconfig16.fc_navailsects = - fmt->ff_nsectors - var->fv_nrootdirsects - fatconfig12.fc_rsvdseccount; - } - - /* Select an initial and terminal clustersize to use in the search (if these - * values were not provided by the caller) - */ - - mxclustshift = mkfatfs_clustersearchlimits(fmt, var); - - do - { - fvdbg("Configuring with %d sectors/cluster...\n", 1 << fmt->ff_clustshift); - - /* Check if FAT12 has not been excluded */ - - if (var->fv_fattype == 0 || var->fv_fattype == 12) - { - /* Try to configure a FAT12 filesystem with this cluster size */ - - if (mkfatfs_tryfat12(fmt, var, &fatconfig12) != 0) - { - { - fvdbg("Cannot format FAT12 at %u sectors/cluster\n", 1 << fmt->ff_clustshift); - fatconfig12.fc_nfatsects = 0; - fatconfig12.fc_nclusters = 0; - } - } - } - - /* Check if FAT16 has not been excluded */ - - if (var->fv_fattype == 0 || var->fv_fattype == 16) - { - /* Try to configure a FAT16 filesystem with this cluster size */ - - if (mkfatfs_tryfat16(fmt, var, &fatconfig16) != 0) - { - { - fvdbg("Cannot format FAT16 at %u sectors/cluster\n", 1 << fmt->ff_clustshift); - fatconfig16.fc_nfatsects = 0; - fatconfig16.fc_nclusters = 0; - } - } - } - - /* If either FAT12 or 16 was configured at this sector/cluster setting, - * then finish the configuration and break out now - */ - - if (fatconfig12.fc_nclusters || fatconfig16.fc_nclusters) - { - if ((!var->fv_fattype && fatconfig16.fc_nclusters > fatconfig12.fc_nclusters) || - (var ->fv_fattype == 16)) - { - /* The caller has selected FAT16 -OR- no FAT type has been selected, but - * the FAT16 selection has more clusters. Select FAT16. - */ - - mkfatfs_selectfat(16, fmt, var, &fatconfig16); - } - else - { - /* The caller has selected FAT12 -OR- no FAT type has been selected, but - * the FAT12 selected has more clusters. Selected FAT12 - */ - - mkfatfs_selectfat(12, fmt, var, &fatconfig12); - } - return OK; - } - - /* Check if FAT32 has not been excluded */ - - if (var->fv_fattype == 0 || var->fv_fattype == 32) - { - /* The number of data sectors available (includes the fat itself) - * This value is a constant with respect to cluster sizefor FAT12/16, but not FAT32 - * because the size of the root directory cluster changes with cluster size. - */ - - fatconfig32.fc_navailsects = fmt->ff_nsectors - (1 << fmt->ff_clustshift) - fatconfig32.fc_rsvdseccount; - - /* Try to configure a FAT32 filesystem with this cluster size */ - - if (mkfatfs_tryfat32(fmt, var, &fatconfig32) != 0) - { - { - fvdbg("Cannot format FAT32 at %u sectors/cluster\n", 1 << fmt->ff_clustshift); - fatconfig32.fc_nfatsects = 0; - fatconfig32.fc_nclusters = 0; - } - } - else - { - /* Select FAT32 if we have not already done so */ - - mkfatfs_selectfat(32, fmt, var, &fatconfig32); - return OK; - } - } - - /* Otherwise, bump up the sectors/cluster for the next time around the loop. */ - - fmt->ff_clustshift++; - } - while (fmt->ff_clustshift <= mxclustshift); - return -ENFILE; -} - -/**************************************************************************** - * Global Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: mkfatfs_configfatfs - * - * Description: - * Based on the geometry of the block device and upon the caller-selected - * values, configure the FAT filesystem for the device. - * - * Input: - * fmt - Caller specified format parameters - * var - Holds disk geomtry data. Also, the location to return FAT - * configuration data - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ -int mkfatfs_configfatfs(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var) -{ - int ret; - - /* Select the number of root directory entries (FAT12/16 only). If FAT32 is selected, - * this value will be cleared later - */ - - if (!fmt->ff_rootdirentries) - { - /* The caller did not specify the number of root directory entries; use a default of 512. */ - - fmt->ff_rootdirentries = 512; - } - - /* Search to determine the smallest (reasonable) cluster size. A by-product - * of this search will be the selection of the FAT size (12/16/32) if the - * caller has not specified the FAT size - */ - - ret = mkfatfs_clustersearch(fmt, var); - if (ret < 0) - { - fdbg("Failed to set cluster size\n"); - return ret; - } - - /* Perform FAT specific initialization */ - - /* Set up boot jump assuming FAT 12/16 offset to bootcode */ - - var->fv_jump[0] = OPCODE_JMP_REL8; - var->fv_jump[2] = OPCODE_NOP; - var->fv_bootcode = g_bootcodeblob; - var->fv_bootcodesize = sizeof(g_bootcodeblob); - - if (var->fv_fattype != 32) - { - /* Set up additional, non-zero FAT12/16 fields */ - - /* Patch in the correct offset to the boot code */ - - var->fv_jump[1] = BS16_BOOTCODE - 2; - g_bootcodeblob[3] = BS16_BOOTCODE + BOOTCODE_MSGOFFSET; - } - else - { - /* Patch in the correct offset to the boot code */ - - var->fv_jump[1] = BS32_BOOTCODE - 2; - g_bootcodeblob[3] = BS32_BOOTCODE + BOOTCODE_MSGOFFSET; - - /* The root directory is a cluster chain... its is initialize size is one cluster */ - - var->fv_nrootdirsects = 1 << fmt->ff_clustshift; - - /* The number of reported root directory entries should should be zero for - * FAT32 because the root directory is a cluster chain. - */ - - fmt->ff_rootdirentries = 0; - - /* Verify the caller's backupboot selection */ - - if (fmt->ff_backupboot <= 1 || fmt->ff_backupboot >= fmt->ff_rsvdseccount) - { - fdbg("Invalid backup boot sector: %d\n", fmt->ff_backupboot); - fmt->ff_backupboot = 0; - } - - /* Check if the caller has selected a location for the backup boot record */ - - if (!fmt->ff_backupboot) - { - /* There must be reserved sectors in order to have a backup boot sector */ - - if (fmt->ff_rsvdseccount > 0 && fmt->ff_rsvdseccount >= 2) - { - /* Sector 0 is the MBR; 1... ff_rsvdseccount are reserved. Try the next - * the last reserved sector. - */ - - fmt->ff_backupboot = fmt->ff_rsvdseccount - 1; - if (fmt->ff_backupboot > 6) - { - /* Limit the location to within the first 7 */ - - fmt->ff_backupboot = 6; - } - } - } - } - - /* Report the selected fat type */ - - fmt->ff_fattype = var->fv_fattype; - - /* Describe the configured filesystem */ - -#ifdef CONFIG_DEBUG - fdbg("Sector size: %d bytes\n", var->fv_sectorsize); - fdbg("Number of sectors: %d sectors\n", fmt->ff_nsectors); - fdbg("FAT size: %d bits\n", var->fv_fattype); - fdbg("Number FATs: %d\n", fmt->ff_nfats); - fdbg("Sectors per cluster: %d sectors\n", 1 << fmt->ff_clustshift); - fdbg("FS size: %d sectors\n", var->fv_nfatsects); - fdbg(" %d clusters\n", var->fv_nclusters); - if (var->fv_fattype != 32) - { - fdbg("Root directory slots: %d\n", fmt->ff_rootdirentries); - } - fdbg("Volume ID: %08x\n", fmt->ff_volumeid); - fdbg("Volume Label: \"%c%c%c%c%c%c%c%c%c%c%c\"\n", - fmt->ff_volumelabel[0], fmt->ff_volumelabel[1], fmt->ff_volumelabel[2], - fmt->ff_volumelabel[3], fmt->ff_volumelabel[4], fmt->ff_volumelabel[5], - fmt->ff_volumelabel[6], fmt->ff_volumelabel[7], fmt->ff_volumelabel[8], - fmt->ff_volumelabel[9], fmt->ff_volumelabel[10]); -#endif - return OK; -} - diff --git a/nuttx/fs/fat/fs_fat32.c b/nuttx/fs/fat/fs_fat32.c deleted file mode 100644 index df8962b51..000000000 --- a/nuttx/fs/fat/fs_fat32.c +++ /dev/null @@ -1,2412 +0,0 @@ -/**************************************************************************** - * fs/fat/fs_fat32.c - * - * Copyright (C) 2007-2009, 2011-2013 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt <gnutt@nuttx.org> - * - * References: - * Microsoft FAT documentation - * Some good ideas were leveraged from the FAT implementation: - * 'Copyright (C) 2007, ChaN, all right reserved.' - * which has an unrestricted license. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name NuttX nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <nuttx/config.h> - -#include <sys/types.h> -#include <sys/stat.h> -#include <sys/statfs.h> - -#include <stdlib.h> -#include <unistd.h> -#include <string.h> -#include <semaphore.h> -#include <assert.h> -#include <fcntl.h> -#include <errno.h> -#include <debug.h> - -#include <nuttx/kmalloc.h> -#include <nuttx/fs/fs.h> -#include <nuttx/fs/fat.h> -#include <nuttx/fs/dirent.h> - -#include "fs_internal.h" -#include "fs_fat32.h" - -/**************************************************************************** - * Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -static int fat_open(FAR struct file *filep, const char *relpath, - int oflags, mode_t mode); -static int fat_close(FAR struct file *filep); -static ssize_t fat_read(FAR struct file *filep, char *buffer, size_t buflen); -static ssize_t fat_write(FAR struct file *filep, const char *buffer, - size_t buflen); -static off_t fat_seek(FAR struct file *filep, off_t offset, int whence); -static int fat_ioctl(FAR struct file *filep, int cmd, unsigned long arg); - -static int fat_sync(FAR struct file *filep); -static int fat_dup(FAR const struct file *oldp, FAR struct file *newp); - -static int fat_opendir(struct inode *mountpt, const char *relpath, - struct fs_dirent_s *dir); -static int fat_readdir(struct inode *mountpt, struct fs_dirent_s *dir); -static int fat_rewinddir(struct inode *mountpt, struct fs_dirent_s *dir); - -static int fat_bind(FAR struct inode *blkdriver, const void *data, - void **handle); -static int fat_unbind(void *handle, FAR struct inode **blkdriver); -static int fat_statfs(struct inode *mountpt, struct statfs *buf); - -static int fat_unlink(struct inode *mountpt, const char *relpath); -static int fat_mkdir(struct inode *mountpt, const char *relpath, - mode_t mode); -static int fat_rmdir(struct inode *mountpt, const char *relpath); -static int fat_rename(struct inode *mountpt, const char *oldrelpath, - const char *newrelpath); -static int fat_stat(struct inode *mountpt, const char *relpath, struct stat *buf); - -/**************************************************************************** - * Private Variables - ****************************************************************************/ - -/**************************************************************************** - * Public Variables - ****************************************************************************/ - -/* See fs_mount.c -- this structure is explicitly externed there. - * We use the old-fashioned kind of initializers so that this will compile - * with any compiler. - */ - -const struct mountpt_operations fat_operations = -{ - fat_open, /* open */ - fat_close, /* close */ - fat_read, /* read */ - fat_write, /* write */ - fat_seek, /* seek */ - fat_ioctl, /* ioctl */ - - fat_sync, /* sync */ - fat_dup, /* dup */ - - fat_opendir, /* opendir */ - NULL, /* closedir */ - fat_readdir, /* readdir */ - fat_rewinddir, /* rewinddir */ - - fat_bind, /* bind */ - fat_unbind, /* unbind */ - fat_statfs, /* statfs */ - - fat_unlink, /* unlinke */ - fat_mkdir, /* mkdir */ - fat_rmdir, /* rmdir */ - fat_rename, /* rename */ - fat_stat /* stat */ -}; - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: fat_open - ****************************************************************************/ - -static int fat_open(FAR struct file *filep, const char *relpath, - int oflags, mode_t mode) -{ - struct fat_dirinfo_s dirinfo; - struct inode *inode; - struct fat_mountpt_s *fs; - struct fat_file_s *ff; - uint8_t *direntry; - int ret; - - /* Sanity checks */ - - DEBUGASSERT(filep->f_priv == NULL && filep->f_inode != NULL); - - /* Get the mountpoint inode reference from the file structure and the - * mountpoint private data from the inode structure - */ - - inode = filep->f_inode; - fs = inode->i_private; - - DEBUGASSERT(fs != NULL); - - /* Check if the mount is still healthy */ - - fat_semtake(fs); - ret = fat_checkmount(fs); - if (ret != OK) - { - goto errout_with_semaphore; - } - - /* Initialize the directory info structure */ - - memset(&dirinfo, 0, sizeof(struct fat_dirinfo_s)); - - /* Locate the directory entry for this path */ - - ret = fat_finddirentry(fs, &dirinfo, relpath); - - /* Three possibililities: (1) a node exists for the relpath and - * dirinfo describes the directory entry of the entity, (2) the - * node does not exist, or (3) some error occurred. - */ - - if (ret == OK) - { - bool readonly; - - /* The name exists -- but is it a file or a directory? */ - - direntry = &fs->fs_buffer[dirinfo.fd_seq.ds_offset]; - if (dirinfo.fd_root || - (DIR_GETATTRIBUTES(direntry) & FATATTR_DIRECTORY)) - { - /* It is a directory */ - - ret = -EISDIR; - goto errout_with_semaphore; - } - - /* It would be an error if we are asked to create it exclusively */ - - if ((oflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) - { - /* Already exists -- can't create it exclusively */ - - ret = -EEXIST; - goto errout_with_semaphore; - } - -#ifdef CONFIG_FILE_MODE -# warning "Missing check for privileges based on inode->i_mode" -#endif - - /* Check if the caller has sufficient privileges to open the file */ - - readonly = ((DIR_GETATTRIBUTES(direntry) & FATATTR_READONLY) != 0); - if (((oflags & O_WRONLY) != 0) && readonly) - { - ret = -EACCES; - goto errout_with_semaphore; - } - - /* If O_TRUNC is specified and the file is opened for writing, - * then truncate the file. This operation requires that the file is - * writable, but we have already checked that. O_TRUNC without write - * access is ignored. - */ - - if ((oflags & (O_TRUNC|O_WRONLY)) == (O_TRUNC|O_WRONLY)) - { - /* Truncate the file to zero length */ - - ret = fat_dirtruncate(fs, &dirinfo); - if (ret < 0) - { - goto errout_with_semaphore; - } - } - - /* fall through to finish the file open operations */ - } - else if (ret == -ENOENT) - { - /* The file does not exist. Were we asked to create it? */ - - if ((oflags & O_CREAT) == 0) - { - /* No.. then we fail with -ENOENT */ - - ret = -ENOENT; - goto errout_with_semaphore; - } - - /* Yes.. create the file */ - - ret = fat_dircreate(fs, &dirinfo); - if (ret < 0) - { - goto errout_with_semaphore; - } - - /* Fall through to finish the file open operation */ - - direntry = &fs->fs_buffer[dirinfo.fd_seq.ds_offset]; - } - else - { - /* An error occurred while checking for file existence -- - * such as if an invalid path were provided. - */ - - goto errout_with_semaphore; - } - - /* Create an instance of the file private date to describe the opened - * file. - */ - - ff = (struct fat_file_s *)kzalloc(sizeof(struct fat_file_s)); - if (!ff) - { - ret = -ENOMEM; - goto errout_with_semaphore; - } - - /* Create a file buffer to support partial sector accesses */ - - ff->ff_buffer = (uint8_t*)fat_io_alloc(fs->fs_hwsectorsize); - if (!ff->ff_buffer) - { - ret = -ENOMEM; - goto errout_with_struct; - } - - /* Initialize the file private data (only need to initialize non-zero elements) */ - - ff->ff_oflags = oflags; - - /* Save information that can be used later to recover the directory entry */ - - ff->ff_dirsector = fs->fs_currentsector; - ff->ff_dirindex = dirinfo.dir.fd_index; - - /* File cluster/size info */ - - ff->ff_startcluster = - ((uint32_t)DIR_GETFSTCLUSTHI(direntry) << 16) | - DIR_GETFSTCLUSTLO(direntry); - - ff->ff_currentcluster = ff->ff_startcluster; - ff->ff_sectorsincluster = fs->fs_fatsecperclus; - ff->ff_size = DIR_GETFILESIZE(direntry); - - /* Attach the private date to the struct file instance */ - - filep->f_priv = ff; - - /* Then insert the new instance into the mountpoint structure. - * It needs to be there (1) to handle error conditions that effect - * all files, and (2) to inform the umount logic that we are busy - * (but a simple reference count could have done that). - */ - - ff->ff_next = fs->fs_head; - fs->fs_head = ff->ff_next; - - fat_semgive(fs); - - /* In write/append mode, we need to set the file pointer to the end of the file */ - - if ((oflags & (O_APPEND|O_WRONLY)) == (O_APPEND|O_WRONLY)) - { - off_t offset = fat_seek(filep, ff->ff_size, SEEK_SET); - if (offset < 0) - { - kfree(ff); - return (int)offset; - } - } - - return OK; - - /* Error exits -- goto's are nasty things, but they sure can make error - * handling a lot simpler. - */ - -errout_with_struct: - kfree(ff); - -errout_with_semaphore: - fat_semgive(fs); - return ret; -} - -/**************************************************************************** - * Name: fat_close - ****************************************************************************/ - -static int fat_close(FAR struct file *filep) -{ - struct inode *inode; - struct fat_file_s *ff; - struct fat_mountpt_s *fs; - int ret = OK; - - /* Sanity checks */ - - DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL); - - /* Recover our private data from the struct file instance */ - - ff = filep->f_priv; - inode = filep->f_inode; - fs = inode->i_private; - - /* Do not check if the mount is healthy. We must support closing of - * the file even when there is healthy mount. - */ - - /* Synchronize the file buffers and disk content; update times */ - - ret = fat_sync(filep); - - /* Then deallocate the memory structures created when the open method - * was called. - * - * Free the sector buffer that was used to manage partial sector accesses. - */ - - if (ff->ff_buffer) - { - (void)fs; /* Unused if fat_io_free == free(). */ - fat_io_free(ff->ff_buffer, fs->fs_hwsectorsize); - } - - /* Then free the file structure itself. */ - - kfree(ff); - filep->f_priv = NULL; - return ret; -} - -/**************************************************************************** - * Name: fat_read - ****************************************************************************/ - -static ssize_t fat_read(FAR struct file *filep, char *buffer, size_t buflen) -{ - struct inode *inode; - struct fat_mountpt_s *fs; - struct fat_file_s *ff; - unsigned int bytesread; - unsigned int readsize; - unsigned int nsectors; - size_t bytesleft; - int32_t cluster; - uint8_t *userbuffer = (uint8_t*)buffer; - int sectorindex; - int ret; - - /* Sanity checks */ - - DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL); - - /* Recover our private data from the struct file instance */ - - ff = filep->f_priv; - inode = filep->f_inode; - fs = inode->i_private; - - DEBUGASSERT(fs != NULL); - - /* Make sure that the mount is still healthy */ - - fat_semtake(fs); - ret = fat_checkmount(fs); - if (ret != OK) - { - goto errout_with_semaphore; - } - - /* Check if the file was opened with read access */ - - if ((ff->ff_oflags & O_RDOK) == 0) - { - ret = -EACCES; - goto errout_with_semaphore; - } - - /* Get the number of bytes left in the file */ - - bytesleft = ff->ff_size - filep->f_pos; - - /* Truncate read count so that it does not exceed the number - * of bytes left in the file. - */ - - if (buflen > bytesleft) - { - buflen = bytesleft; - } - - /* Get the first sector to read from. */ - - if (!ff->ff_currentsector) - { - /* The current sector can be determined from the current cluster - * and the file offset. - */ - - ret = fat_currentsector(fs, ff, filep->f_pos); - if (ret < 0) - { - return ret; - } - } - - /* Loop until either (1) all data has been transferred, or (2) an - * error occurs. We assume we start with the current sector - * (ff_currentsector) which may be uninitialized. - */ - - readsize = 0; - sectorindex = filep->f_pos & SEC_NDXMASK(fs); - - while (buflen > 0) - { - bytesread = 0; - - /* Check if the user has provided a buffer large enough to - * hold one or more complete sectors -AND- the read is - * aligned to a sector boundary. - */ - - nsectors = buflen / fs->fs_hwsectorsize; - if (nsectors > 0 && sectorindex == 0) - { - /* Read maximum contiguous sectors directly to the user's - * buffer without using our tiny read buffer. - * - * Limit the number of sectors that we read on this time - * through the loop to the remaining contiguous sectors - * in this cluster - */ - - if (nsectors > ff->ff_sectorsincluster) - { - nsectors = ff->ff_sectorsincluster; - } - - /* We are not sure of the state of the file buffer so - * the safest thing to do is just invalidate it - */ - - (void)fat_ffcacheinvalidate(fs, ff); - - /* Read all of the sectors directly into user memory */ - - ret = fat_hwread(fs, userbuffer, ff->ff_currentsector, nsectors); - if (ret < 0) - { - goto errout_with_semaphore; - } - - ff->ff_sectorsincluster -= nsectors; - ff->ff_currentsector += nsectors; - bytesread = nsectors * fs->fs_hwsectorsize; - } - else - { - /* We are reading a partial sector. First, read the whole sector - * into the file data buffer. This is a caching buffer so if - * it is already there then all is well. - */ - - ret = fat_ffcacheread(fs, ff, ff->ff_currentsector); - if (ret < 0) - { - goto errout_with_semaphore; - } - - /* Copy the partial sector into the user buffer */ - - bytesread = fs->fs_hwsectorsize - sectorindex; - if (bytesread > buflen) - { - /* We will not read to the end of the buffer */ - - bytesread = buflen; - } - else - { - /* We will read to the end of the buffer (or beyond) */ - - ff->ff_sectorsincluster--; - ff->ff_currentsector++; - } - - memcpy(userbuffer, &ff->ff_buffer[sectorindex], bytesread); - } - - /* Set up for the next sector read */ - - userbuffer += bytesread; - filep->f_pos += bytesread; - readsize += bytesread; - buflen -= bytesread; - sectorindex = filep->f_pos & SEC_NDXMASK(fs); - - /* Check if the current read stream has incremented to the next - * cluster boundary - */ - - if (ff->ff_sectorsincluster < 1) - { - /* Find the next cluster in the FAT. */ - - cluster = fat_getcluster(fs, ff->ff_currentcluster); - if (cluster < 2 || cluster >= fs->fs_nclusters) - { - ret = -EINVAL; /* Not the right error */ - goto errout_with_semaphore; - } - - /* Setup to read the first sector from the new cluster */ - - ff->ff_currentcluster = cluster; - ff->ff_currentsector = fat_cluster2sector(fs, cluster); - ff->ff_sectorsincluster = fs->fs_fatsecperclus; - } - } - - fat_semgive(fs); - return readsize; - -errout_with_semaphore: - fat_semgive(fs); - return ret; -} - -/**************************************************************************** - * Name: fat_write - ****************************************************************************/ - -static ssize_t fat_write(FAR struct file *filep, const char *buffer, - size_t buflen) -{ - struct inode *inode; - struct fat_mountpt_s *fs; - struct fat_file_s *ff; - int32_t cluster; - unsigned int byteswritten; - unsigned int writesize; - unsigned int nsectors; - uint8_t *userbuffer = (uint8_t*)buffer; - int sectorindex; - int ret; - - /* Sanity checks */ - - DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL); - - /* Recover our private data from the struct file instance */ - - ff = filep->f_priv; - inode = filep->f_inode; - fs = inode->i_private; - - DEBUGASSERT(fs != NULL); - - /* Make sure that the mount is still healthy */ - - fat_semtake(fs); - ret = fat_checkmount(fs); - if (ret != OK) - { - goto errout_with_semaphore; - } - - /* Check if the file was opened for write access */ - - if ((ff->ff_oflags & O_WROK) == 0) - { - ret = -EACCES; - goto errout_with_semaphore; - } - - /* Check if the file size would exceed the range of off_t */ - - if (ff->ff_size + buflen < ff->ff_size) - { - ret = -EFBIG; - goto errout_with_semaphore; - } - - /* Get the first sector to write to. */ - - if (!ff->ff_currentsector) - { - /* Has the starting cluster been defined? */ - - if (ff->ff_startcluster == 0) - { - /* No.. we have to create a new cluster chain */ - - ff->ff_startcluster = fat_createchain(fs); - ff->ff_currentcluster = ff->ff_startcluster; - ff->ff_sectorsincluster = fs->fs_fatsecperclus; - } - - /* The current sector can then be determined from the currentcluster - * and the file offset. - */ - - ret = fat_currentsector(fs, ff, filep->f_pos); - if (ret < 0) - { - return ret; - } - } - - /* Loop until either (1) all data has been transferred, or (2) an - * error occurs. We assume we start with the current sector in - * cache (ff_currentsector) - */ - - byteswritten = 0; - sectorindex = filep->f_pos & SEC_NDXMASK(fs); - - while (buflen > 0) - { - /* Check if the user has provided a buffer large enough to - * hold one or more complete sectors. - */ - - nsectors = buflen / fs->fs_hwsectorsize; - if (nsectors > 0 && sectorindex == 0) - { - /* Write maximum contiguous sectors directly from the user's - * buffer without using our tiny read buffer. - * - * Limit the number of sectors that we write on this time - * through the loop to the remaining contiguous sectors - * in this cluster - */ - - if (nsectors > ff->ff_sectorsincluster) - { - nsectors = ff->ff_sectorsincluster; - } - - /* We are not sure of the state of the sector cache so the - * safest thing to do is write back any dirty, cached sector - * and invalidate the current cache content. - */ - - (void)fat_ffcacheinvalidate(fs, ff); - - /* Write all of the sectors directly from user memory */ - - ret = fat_hwwrite(fs, userbuffer, ff->ff_currentsector, nsectors); - if (ret < 0) - { - goto errout_with_semaphore; - } - - ff->ff_sectorsincluster -= nsectors; - ff->ff_currentsector += nsectors; - writesize = nsectors * fs->fs_hwsectorsize; - ff->ff_bflags |= FFBUFF_MODIFIED; - } - else - { - /* We are writing a partial sector -OR- the current sector - * has not yet been filled. - * - * We will first have to read the full sector in memory as - * part of a read-modify-write operation. NOTE we don't - * have to read the data on a rare case: When we are extending - * the file (filep->f_pos == ff->ff_size) -AND- the new data - * happens to be aligned at the beginning of the sector - * (sectorindex == 0). - */ - - if (filep->f_pos < ff->ff_size || sectorindex != 0) - { - /* Read the current sector into memory (perhaps first flushing - * the old, dirty sector to disk). - */ - - ret = fat_ffcacheread(fs, ff, ff->ff_currentsector); - if (ret < 0) - { - goto errout_with_semaphore; - } - } - else - { - /* Flush unwritten data in the sector cache. */ - - ret = fat_ffcacheflush(fs, ff); - if (ret < 0) - { - goto errout_with_semaphore; - } - - /* Now mark the clean cache buffer as the current sector. */ - - ff->ff_cachesector = ff->ff_currentsector; - } - - /* Copy the partial sector from the user buffer */ - - writesize = fs->fs_hwsectorsize - sectorindex; - if (writesize > buflen) - { - /* We will not write to the end of the buffer. Set - * write size to the size of the user buffer. - */ - - writesize = buflen; - } - else - { - /* We will write to the end of the buffer (or beyond). Bump - * up the current sector number (actually the next sector number). - */ - - ff->ff_sectorsincluster--; - ff->ff_currentsector++; - } - - /* Copy the data into the cached sector and make sure that the - * cached sector is marked "dirty" - */ - - memcpy(&ff->ff_buffer[sectorindex], userbuffer, writesize); - ff->ff_bflags |= (FFBUFF_DIRTY|FFBUFF_VALID|FFBUFF_MODIFIED); - } - - /* Set up for the next write */ - - userbuffer += writesize; - filep->f_pos += writesize; - byteswritten += writesize; - buflen -= writesize; - sectorindex = filep->f_pos & SEC_NDXMASK(fs); - - /* Check if the current read stream has incremented to the next - * cluster boundary - */ - - if (ff->ff_sectorsincluster < 1) - { - /* Extend the current cluster by one (unless lseek was used to - * move the file position back from the end of the file) - */ - - cluster = fat_extendchain(fs, ff->ff_currentcluster); - - /* Verify the cluster number */ - - if (cluster < 0) - { - ret = cluster; - goto errout_with_semaphore; - } - else if (cluster < 2 || cluster >= fs->fs_nclusters) - { - ret = -ENOSPC; - goto errout_with_semaphore; - } - - /* Setup to write the first sector from the new cluster */ - - ff->ff_currentcluster = cluster; - ff->ff_sectorsincluster = fs->fs_fatsecperclus; - ff->ff_currentsector = fat_cluster2sector(fs, cluster); - } - } - - /* The transfer has completed without error. Update the file size */ - - if (filep->f_pos > ff->ff_size) - { - ff->ff_size = filep->f_pos; - } - - fat_semgive(fs); - return byteswritten; - -errout_with_semaphore: - fat_semgive(fs); - return ret; -} - -/**************************************************************************** - * Name: fat_seek - ****************************************************************************/ - -static off_t fat_seek(FAR struct file *filep, off_t offset, int whence) -{ - struct inode *inode; - struct fat_mountpt_s *fs; - struct fat_file_s *ff; - int32_t cluster; - off_t position; - unsigned int clustersize; - int ret; - - /* Sanity checks */ - - DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL); - - /* Recover our private data from the struct file instance */ - - ff = filep->f_priv; - inode = filep->f_inode; - fs = inode->i_private; - - DEBUGASSERT(fs != NULL); - - /* Map the offset according to the whence option */ - - switch (whence) - { - case SEEK_SET: /* The offset is set to offset bytes. */ - position = offset; - break; - - case SEEK_CUR: /* The offset is set to its current location plus - * offset bytes. */ - - position = offset + filep->f_pos; - break; - - case SEEK_END: /* The offset is set to the size of the file plus - * offset bytes. */ - - position = offset + ff->ff_size; - break; - - default: - return -EINVAL; - } - - /* Make sure that the mount is still healthy */ - - fat_semtake(fs); - ret = fat_checkmount(fs); - if (ret != OK) - { - goto errout_with_semaphore; - } - - /* Check if there is unwritten data in the file buffer */ - - ret = fat_ffcacheflush(fs, ff); - if (ret < 0) - { - goto errout_with_semaphore; - } - - /* Attempts to set the position beyound the end of file will - * work if the file is open for write access. - */ - - if (position > ff->ff_size && (ff->ff_oflags & O_WROK) == 0) - { - /* Otherwise, the position is limited to the file size */ - - position = ff->ff_size; - } - - /* Set file position to the beginning of the file (first cluster, - * first sector in cluster) - */ - - filep->f_pos = 0; - ff->ff_sectorsincluster = fs->fs_fatsecperclus; - - /* Get the start cluster of the file */ - - cluster = ff->ff_startcluster; - - /* Create a new cluster chain if the file does not have one (and - * if we are seeking beyond zero - */ - - if (!cluster && position > 0) - { - cluster = fat_createchain(fs); - if (cluster < 0) - { - ret = cluster; - goto errout_with_semaphore; - } - - ff->ff_startcluster = cluster; - } - - /* Move file position if necessary */ - - if (cluster) - { - /* If the file has a cluster chain, follow it to the - * requested position. - */ - - clustersize = fs->fs_fatsecperclus * fs->fs_hwsectorsize; - for (;;) - { - /* Skip over clusters prior to the one containing - * the requested position. - */ - - ff->ff_currentcluster = cluster; - if (position < clustersize) - { - break; - } - - /* Extend the cluster chain if write in enabled. NOTE: - * this is not consistent with the lseek description: - * "The lseek() function allows the file offset to be - * set beyond the end of the file (but this does not - * change the size of the file). If data is later written - * at this point, subsequent reads of the data in the - * gap (a "hole") return null bytes ('\0') until data - * is actually written into the gap." - */ - - if ((ff->ff_oflags & O_WROK) != 0) - { - /* Extend the cluster chain (fat_extendchain - * will follow the existing chain or add new - * clusters as needed. - */ - - cluster = fat_extendchain(fs, cluster); - } - else - { - /* Otherwise we can only follong the existing chain */ - - cluster = fat_getcluster(fs, cluster); - } - - if (cluster < 0) - { - /* An error occurred getting the cluster */ - - ret = cluster; - goto errout_with_semaphore; - } - - /* Zero means that there is no further clusters available - * in the chain. - */ - - if (cluster == 0) - { - /* At the position to the current locaiton and - * break out. - */ - - position = clustersize; - break; - } - - if (cluster >= fs->fs_nclusters) - { - ret = -ENOSPC; - goto errout_with_semaphore; - } - - /* Otherwise, update the position and continue looking */ - - filep->f_pos += clustersize; - position -= clustersize; - } - - /* We get here after we have found the sector containing - * the requested position. - * - * Save the new file position - */ - - filep->f_pos += position; - - /* Then get the current sector from the cluster and the offset - * into the cluster from the position - */ - - (void)fat_currentsector(fs, ff, filep->f_pos); - - /* Load the sector corresponding to the position */ - - if ((position & SEC_NDXMASK(fs)) != 0) - { - ret = fat_ffcacheread(fs, ff, ff->ff_currentsector); - if (ret < 0) - { - goto errout_with_semaphore; - } - } - } - - /* If we extended the size of the file, then mark the file as modified. */ - - if ((ff->ff_oflags & O_WROK) != 0 && filep->f_pos > ff->ff_size) - { - ff->ff_size = filep->f_pos; - ff->ff_bflags |= FFBUFF_MODIFIED; - } - - fat_semgive(fs); - return OK; - -errout_with_semaphore: - fat_semgive(fs); - return ret; -} - -/**************************************************************************** - * Name: fat_ioctl - ****************************************************************************/ - -static int fat_ioctl(FAR struct file *filep, int cmd, unsigned long arg) -{ - struct inode *inode; - struct fat_mountpt_s *fs; - int ret; - - /* Sanity checks */ - - DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL); - - /* Recover our private data from the struct file instance */ - - inode = filep->f_inode; - fs = inode->i_private; - - DEBUGASSERT(fs != NULL); - - /* Make sure that the mount is still healthy */ - - fat_semtake(fs); - ret = fat_checkmount(fs); - if (ret != OK) - { - fat_semgive(fs); - return ret; - } - - /* ioctl calls are just passed through to the contained block driver */ - - fat_semgive(fs); - return -ENOSYS; -} - -/**************************************************************************** - * Name: fat_sync - * - * Description: Synchronize the file state on disk to match internal, in- - * memory state. - * - ****************************************************************************/ - -static int fat_sync(FAR struct file *filep) -{ - struct inode *inode; - struct fat_mountpt_s *fs; - struct fat_file_s *ff; - uint32_t wrttime; - uint8_t *direntry; - int ret; - - /* Sanity checks */ - - DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL); - - /* Recover our private data from the struct file instance */ - - ff = filep->f_priv; - inode = filep->f_inode; - fs = inode->i_private; - - DEBUGASSERT(fs != NULL); - - /* Make sure that the mount is still healthy */ - - fat_semtake(fs); - ret = fat_checkmount(fs); - if (ret != OK) - { - goto errout_with_semaphore; - } - - /* Check if the has been modified in any way */ - - if ((ff->ff_bflags & FFBUFF_MODIFIED) != 0) - { - /* Flush any unwritten data in the file buffer */ - - ret = fat_ffcacheflush(fs, ff); - if (ret < 0) - { - goto errout_with_semaphore; - } - - /* Update the directory entry. First read the directory - * entry into the fs_buffer (preserving the ff_buffer) - */ - - ret = fat_fscacheread(fs, ff->ff_dirsector); - if (ret < 0) - { - goto errout_with_semaphore; - } - - /* Recover a pointer to the specific directory entry - * in the sector using the saved directory index. - */ - - direntry = &fs->fs_buffer[(ff->ff_dirindex & DIRSEC_NDXMASK(fs)) * DIR_SIZE]; - - /* Set the archive bit, set the write time, and update - * anything that may have* changed in the directory - * entry: the file size, and the start cluster - */ - - direntry[DIR_ATTRIBUTES] |= FATATTR_ARCHIVE; - - DIR_PUTFILESIZE(direntry, ff->ff_size); - DIR_PUTFSTCLUSTLO(direntry, ff->ff_startcluster); - DIR_PUTFSTCLUSTHI(direntry, ff->ff_startcluster >> 16); - - wrttime = fat_systime2fattime(); - DIR_PUTWRTTIME(direntry, wrttime & 0xffff); - DIR_PUTWRTDATE(direntry, wrttime >> 16); - - /* Clear the modified bit in the flags */ - - ff->ff_bflags &= ~FFBUFF_MODIFIED; - - /* Flush these change to disk and update FSINFO (if - * appropriate. - */ - - fs->fs_dirty = true; - ret = fat_updatefsinfo(fs); - } - -errout_with_semaphore: - fat_semgive(fs); - return ret; -} - -/**************************************************************************** - * Name: fat_dup - * - * Description: Duplicate open file data in the new file structure. - * - ****************************************************************************/ - -static int fat_dup(FAR const struct file *oldp, FAR struct file *newp) -{ - FAR struct fat_mountpt_s *fs; - FAR struct fat_file_s *oldff; - FAR struct fat_file_s *newff; - int ret; - - fvdbg("Dup %p->%p\n", oldp, newp); - - /* Sanity checks */ - - DEBUGASSERT(oldp->f_priv != NULL && - newp->f_priv == NULL && - newp->f_inode != NULL); - - /* Recover our private data from the struct file instance */ - - fs = (struct fat_mountpt_s *)oldp->f_inode->i_private; - DEBUGASSERT(fs != NULL); - - /* Check if the mount is still healthy */ - - fat_semtake(fs); - ret = fat_checkmount(fs); - if (ret != OK) - { - goto errout_with_semaphore; - } - - /* Recover the old private data from the old struct file instance */ - - oldff = oldp->f_priv; - - /* Create a new instance of the file private date to describe the - * dup'ed file. - */ - - newff = (struct fat_file_s *)kmalloc(sizeof(struct fat_file_s)); - if (!newff) - { - ret = -ENOMEM; - goto errout_with_semaphore; - } - - /* Create a file buffer to support partial sector accesses */ - - newff->ff_buffer = (uint8_t*)fat_io_alloc(fs->fs_hwsectorsize); - if (!newff->ff_buffer) - { - ret = -ENOMEM; - goto errout_with_struct; - } - - /* Copy the rest of the open open file state from the old file structure. - * There are some assumptions and potential issues here: - * - * 1) We assume that the higher level logic has copied the elements of - * the file structure, in particular, the file position. - * 2) There is a problem with ff_size if there are multiple opened - * file structures, each believing they know the size of the file. - * If one instance modifies the file length, then the new size of - * the opened file will be unknown to the other. That is a lurking - * bug! - * - * One good solution to this might be to add a refernce count to the - * file structure. Then, instead of dup'ing the whole structure - * as is done here, just increment the reference count on the - * structure. The would have to be integrated with open logic as - * well, however, so that the same file structure is re-used if the - * file is re-opened. - */ - - newff->ff_bflags = 0; /* File buffer flags */ - newff->ff_oflags = oldff->ff_oflags; /* File open flags */ - newff->ff_sectorsincluster = oldff->ff_sectorsincluster; /* Sectors remaining in cluster */ - newff->ff_dirindex = oldff->ff_dirindex; /* Index to directory entry */ - newff->ff_currentcluster = oldff->ff_currentcluster; /* Current cluster */ - newff->ff_dirsector = oldff->ff_dirsector; /* Sector containing directory entry */ - newff->ff_size = oldff->ff_size; /* Size of the file */ - newff->ff_startcluster = oldff->ff_startcluster; /* Start cluster of file on media */ - newff->ff_currentsector = oldff->ff_currentsector; /* Current sector */ - newff->ff_cachesector = 0; /* Sector in file buffer */ - - /* Attach the private date to the struct file instance */ - - newp->f_priv = newff; - - /* Then insert the new instance into the mountpoint structure. - * It needs to be there (1) to handle error conditions that effect - * all files, and (2) to inform the umount logic that we are busy - * (but a simple reference count could have done that). - */ - - newff->ff_next = fs->fs_head; - fs->fs_head = newff->ff_next; - - fat_semgive(fs); - return OK; - - /* Error exits -- goto's are nasty things, but they sure can make error - * handling a lot simpler. - */ - -errout_with_struct: - kfree(newff); - -errout_with_semaphore: - fat_semgive(fs); - return ret; -} - -/**************************************************************************** - * Name: fat_opendir - * - * Description: Open a directory for read access - * - ****************************************************************************/ - -static int fat_opendir(struct inode *mountpt, const char *relpath, struct fs_dirent_s *dir) -{ - struct fat_mountpt_s *fs; - struct fat_dirinfo_s dirinfo; - uint8_t *direntry; - int ret; - - /* Sanity checks */ - - DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL); - - /* Recover our private data from the inode instance */ - - fs = mountpt->i_private; - - /* Make sure that the mount is still healthy */ - - fat_semtake(fs); - ret = fat_checkmount(fs); - if (ret != OK) - { - goto errout_with_semaphore; - } - - /* Find the requested directory */ - - ret = fat_finddirentry(fs, &dirinfo, relpath); - if (ret < 0) - { - goto errout_with_semaphore; - } - direntry = &fs->fs_buffer[dirinfo.fd_seq.ds_offset]; - - /* Check if this is the root directory */ - - if (dirinfo.fd_root) - { - /* Handle the FAT12/16/32 root directory using the values setup by - * fat_finddirentry() above. - */ - - dir->u.fat.fd_startcluster = dirinfo.dir.fd_startcluster; - dir->u.fat.fd_currcluster = dirinfo.dir.fd_currcluster; - dir->u.fat.fd_currsector = dirinfo.dir.fd_currsector; - dir->u.fat.fd_index = dirinfo.dir.fd_index; - } - - /* This is not the root directory. Verify that it is some kind of directory */ - - else if ((DIR_GETATTRIBUTES(direntry) & FATATTR_DIRECTORY) == 0) - { - /* The entry is not a directory */ - - ret = -ENOTDIR; - goto errout_with_semaphore; - } - else - { - /* The entry is a directory (but not the root directory) */ - - dir->u.fat.fd_startcluster = - ((uint32_t)DIR_GETFSTCLUSTHI(direntry) << 16) | - DIR_GETFSTCLUSTLO(direntry); - dir->u.fat.fd_currcluster = dir->u.fat.fd_startcluster; - dir->u.fat.fd_currsector = fat_cluster2sector(fs, dir->u.fat.fd_currcluster); - dir->u.fat.fd_index = 2; - } - - fat_semgive(fs); - return OK; - -errout_with_semaphore: - fat_semgive(fs); - return ERROR; -} - -/**************************************************************************** - * Name: fat_readdir - * - * Description: Read the next directory entry - * - ****************************************************************************/ - -static int fat_readdir(struct inode *mountpt, struct fs_dirent_s *dir) -{ - struct fat_mountpt_s *fs; - unsigned int dirindex; - uint8_t *direntry; - uint8_t ch; - uint8_t attribute; - bool found; - int ret = OK; - - /* Sanity checks */ - - DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL); - - /* Recover our private data from the inode instance */ - - fs = mountpt->i_private; - - /* Make sure that the mount is still healthy */ - - fat_semtake(fs); - ret = fat_checkmount(fs); - if (ret != OK) - { - goto errout_with_semaphore; - } - - /* Read the next directory entry */ - - dir->fd_dir.d_name[0] = '\0'; - found = false; - - while (dir->u.fat.fd_currsector && !found) - { - ret = fat_fscacheread(fs, dir->u.fat.fd_currsector); - if (ret < 0) - { - goto errout_with_semaphore; - } - - /* Get a reference to the current directory entry */ - - dirindex = (dir->u.fat.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE; - direntry = &fs->fs_buffer[dirindex]; - - /* Has it reached to end of the directory */ - - ch = *direntry; - if (ch == DIR0_ALLEMPTY) - { - /* We signal the end of the directory by returning the - * special error -ENOENT - */ - - ret = -ENOENT; - goto errout_with_semaphore; - } - - /* No, is the current entry a valid entry? */ - - attribute = DIR_GETATTRIBUTES(direntry); - -#ifdef CONFIG_FAT_LFN - if (ch != DIR0_EMPTY && - ((attribute & FATATTR_VOLUMEID) == 0 || - ((ch & LDIR0_LAST) != 0 && attribute == LDDIR_LFNATTR))) -#else - if (ch != DIR0_EMPTY && (attribute & FATATTR_VOLUMEID) == 0) -#endif - { - /* Yes.. get the name from the directory entry. NOTE: For the case - * of the long file name entry, this will advance the several - * several directory entries. - */ - - ret = fat_dirname2path(fs, dir); - if (ret == OK) - { - /* The name was successfully extracted. Re-read the - * attributes: If this is long directory entry, then the - * attributes that we need will be the the final, short file - * name entry and not in the directory entry where we started - * looking for the file name. We can be assured that, on - * success, fat_dirname2path() will leave the short file name - * entry in the cache regardless of the kind of directory - * entry. We simply have to re-read it to cover the the long - * file name case. - */ - -#ifdef CONFIG_FAT_LFN - - /* Get a reference to the current, short file name directory - * entry. - */ - - dirindex = (dir->u.fat.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE; - direntry = &fs->fs_buffer[dirindex]; - - /* Then re-read the attributes from the short file name entry */ - - attribute = DIR_GETATTRIBUTES(direntry); -#endif - /* Now get the file type from the directory attributes. */ - - if ((attribute & FATATTR_DIRECTORY) == 0) - { - dir->fd_dir.d_type = DTYPE_FILE; - } - else - { - dir->fd_dir.d_type = DTYPE_DIRECTORY; - } - - /* Mark the entry found. We will set up the next directory index, - * and then exit with success. - */ - - found = true; - } - } - - /* Set up the next directory index */ - - if (fat_nextdirentry(fs, &dir->u.fat) != OK) - { - ret = -ENOENT; - goto errout_with_semaphore; - } - } - - fat_semgive(fs); - return OK; - -errout_with_semaphore: - fat_semgive(fs); - return ret; -} - -/**************************************************************************** - * Name: fat_rewindir - * - * Description: Reset directory read to the first entry - * - ****************************************************************************/ - -static int fat_rewinddir(struct inode *mountpt, struct fs_dirent_s *dir) -{ - struct fat_mountpt_s *fs; - int ret; - - /* Sanity checks */ - - DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL); - - /* Recover our private data from the inode instance */ - - fs = mountpt->i_private; - - /* Make sure that the mount is still healthy */ - - fat_semtake(fs); - ret = fat_checkmount(fs); - if (ret != OK) - { - goto errout_with_semaphore; - } - - /* Check if this is the root directory. If it is the root directory, we - * reset the fd_index to 0, starting with the initial, entry. - */ - - if (fs->fs_type != FSTYPE_FAT32 && - dir->u.fat.fd_startcluster == 0) - { - /* Handle the FAT12/16 root directory */ - - dir->u.fat.fd_currcluster = 0; - dir->u.fat.fd_currsector = fs->fs_rootbase; - dir->u.fat.fd_index = 0; - } - else if (fs->fs_type == FSTYPE_FAT32 && - dir->u.fat.fd_startcluster == fs->fs_rootbase) - { - /* Handle the FAT32 root directory */ - - dir->u.fat.fd_currcluster = dir->u.fat.fd_startcluster; - dir->u.fat.fd_currsector = fat_cluster2sector(fs, fs->fs_rootbase); - dir->u.fat.fd_index = 0; - } - - /* This is not the root directory. Here the fd_index is set to 2, skipping over - * both the "." and ".." entries. - */ - - else - { - dir->u.fat.fd_currcluster = dir->u.fat.fd_startcluster; - dir->u.fat.fd_currsector = fat_cluster2sector(fs, dir->u.fat.fd_currcluster); - dir->u.fat.fd_index = 2; - } - - fat_semgive(fs); - return OK; - -errout_with_semaphore: - fat_semgive(fs); - return ERROR; -} - -/**************************************************************************** - * Name: fat_bind - * - * Description: This implements a portion of the mount operation. This - * function allocates and initializes the mountpoint private data and - * binds the blockdriver inode to the filesystem private data. The final - * binding of the private data (containing the blockdriver) to the - * mountpoint is performed by mount(). - * - ****************************************************************************/ - -static int fat_bind(FAR struct inode *blkdriver, const void *data, - void **handle) -{ - struct fat_mountpt_s *fs; - int ret; - - /* Open the block driver */ - - if (!blkdriver || !blkdriver->u.i_bops) - { - return -ENODEV; - } - - if (blkdriver->u.i_bops->open && - blkdriver->u.i_bops->open(blkdriver) != OK) - { - return -ENODEV; - } - - /* Create an instance of the mountpt state structure */ - - fs = (struct fat_mountpt_s *)kzalloc(sizeof(struct fat_mountpt_s)); - if (!fs) - { - return -ENOMEM; - } - - /* Initialize the allocated mountpt state structure. The filesystem is - * responsible for one reference ont the blkdriver inode and does not - * have to addref() here (but does have to release in ubind(). - */ - - fs->fs_blkdriver = blkdriver; /* Save the block driver reference */ - sem_init(&fs->fs_sem, 0, 0); /* Initialize the semaphore that controls access */ - - /* Then get information about the FAT32 filesystem on the devices managed - * by this block driver. - */ - - ret = fat_mount(fs, true); - if (ret != 0) - { - sem_destroy(&fs->fs_sem); - kfree(fs); - return ret; - } - - *handle = (void*)fs; - fat_semgive(fs); - return OK; -} - -/**************************************************************************** - * Name: fat_unbind - * - * Description: This implements the filesystem portion of the umount - * operation. - * - ****************************************************************************/ - -static int fat_unbind(void *handle, FAR struct inode **blkdriver) -{ - struct fat_mountpt_s *fs = (struct fat_mountpt_s*)handle; - int ret; - - if (!fs) - { - return -EINVAL; - } - - /* Check if there are sill any files opened on the filesystem. */ - - ret = OK; /* Assume success */ - fat_semtake(fs); - if (fs->fs_head) - { - /* We cannot unmount now.. there are open files */ - - ret = -EBUSY; - } - else - { - /* Unmount ... close the block driver */ - - if (fs->fs_blkdriver) - { - struct inode *inode = fs->fs_blkdriver; - if (inode) - { - if (inode->u.i_bops && inode->u.i_bops->close) - { - (void)inode->u.i_bops->close(inode); - } - - /* We hold a reference to the block driver but should - * not but mucking with inodes in this context. So, we will just return - * our contained reference to the block driver inode and let the umount - * logic dispose of it. - */ - - if (blkdriver) - { - *blkdriver = inode; - } - } - } - - /* Release the mountpoint private data */ - - if (fs->fs_buffer) - { - fat_io_free(fs->fs_buffer, fs->fs_hwsectorsize); - } - - kfree(fs); - } - - fat_semgive(fs); - return ret; -} - -/**************************************************************************** - * Name: fat_statfs - * - * Description: Return filesystem statistics - * - ****************************************************************************/ - -static int fat_statfs(struct inode *mountpt, struct statfs *buf) -{ - struct fat_mountpt_s *fs; - int ret; - - /* Sanity checks */ - - DEBUGASSERT(mountpt && mountpt->i_private); - - /* Get the mountpoint private data from the inode structure */ - - fs = mountpt->i_private; - - /* Check if the mount is still healthy */ - - fat_semtake(fs); - ret = fat_checkmount(fs); - if (ret < 0) - { - goto errout_with_semaphore; - } - - /* Fill in the statfs info */ - - memset(buf, 0, sizeof(struct statfs)); - buf->f_type = MSDOS_SUPER_MAGIC; - - /* We will claim that the optimal transfer size is the size of a cluster in bytes */ - - buf->f_bsize = fs->fs_fatsecperclus * fs->fs_hwsectorsize; - - /* Everything else follows in units of clusters */ - - ret = fat_nfreeclusters(fs, &buf->f_bfree); /* Free blocks in the file system */ - if (ret >= 0) - { - buf->f_blocks = fs->fs_nclusters; /* Total data blocks in the file system */ - buf->f_bavail = buf->f_bfree; /* Free blocks avail to non-superuser */ -#ifdef CONFIG_FAT_LFN - buf->f_namelen = LDIR_MAXFNAME; /* Maximum length of filenames */ -#else - buf->f_namelen = (8+1+3); /* Maximum length of filenames */ -#endif - } - -errout_with_semaphore: - fat_semgive(fs); - return ret; -} - -/**************************************************************************** - * Name: fat_unlink - * - * Description: Remove a file - * - ****************************************************************************/ - -static int fat_unlink(struct inode *mountpt, const char *relpath) -{ - struct fat_mountpt_s *fs; - int ret; - - /* Sanity checks */ - - DEBUGASSERT(mountpt && mountpt->i_private); - - /* Get the mountpoint private data from the inode structure */ - - fs = mountpt->i_private; - - /* Check if the mount is still healthy */ - - fat_semtake(fs); - ret = fat_checkmount(fs); - if (ret == OK) - { - /* If the file is open, the correct behavior is to remove the file - * name, but to keep the file cluster chain in place until the last - * open reference to the file is closed. - */ - -#ifdef CONFIG_CPP_HAVE_WARNING -# warning "Need to defer deleting cluster chain if the file is open" -#endif - - /* Remove the file */ - - ret = fat_remove(fs, relpath, false); - } - - fat_semgive(fs); - return ret; -} - -/**************************************************************************** - * Name: fat_mkdir - * - * Description: Create a directory - * - ****************************************************************************/ - -static int fat_mkdir(struct inode *mountpt, const char *relpath, mode_t mode) -{ - struct fat_mountpt_s *fs; - struct fat_dirinfo_s dirinfo; - uint8_t *direntry; - uint8_t *direntry2; - off_t parentsector; - off_t dirsector; - int32_t dircluster; - uint32_t parentcluster; - uint32_t crtime; - unsigned int i; - int ret; - - /* Sanity checks */ - - DEBUGASSERT(mountpt && mountpt->i_private); - - /* Get the mountpoint private data from the inode structure */ - - fs = mountpt->i_private; - - /* Check if the mount is still healthy */ - - fat_semtake(fs); - ret = fat_checkmount(fs); - if (ret != OK) - { - goto errout_with_semaphore; - } - - /* Find the directory where the new directory should be created. */ - - ret = fat_finddirentry(fs, &dirinfo, relpath); - - /* If anything exists at this location, then we fail with EEXIST */ - - if (ret == OK) - { - ret = -EEXIST; - goto errout_with_semaphore; - } - - /* What we want to see is for fat_finddirentry to fail with -ENOENT. - * This error means that no failure occurred but that nothing exists - * with this name. NOTE: The name has already been set in dirinfo - * structure. - */ - - if (ret != -ENOENT) - { - goto errout_with_semaphore; - } - - /* NOTE: There is no check that dirinfo.fd_name contains the final - * directory name. We could be creating an intermediate directory - * in the full relpath. - */ - - /* Allocate a directory entry for the new directory in this directory */ - - ret = fat_allocatedirentry(fs, &dirinfo); - if (ret != OK) - { - goto errout_with_semaphore; - } - parentsector = fs->fs_currentsector; - - /* Allocate a cluster for new directory */ - - dircluster = fat_createchain(fs); - if (dircluster < 0) - { - ret = dircluster; - goto errout_with_semaphore; - } - else if (dircluster < 2) - { - ret = -ENOSPC; - goto errout_with_semaphore; - } - - dirsector = fat_cluster2sector(fs, dircluster); - if (dirsector < 0) - { - ret = dirsector; - goto errout_with_semaphore; - } - - /* Flush any existing, dirty data in fs_buffer (because we need - * it to create the directory entries. - */ - - ret = fat_fscacheflush(fs); - if (ret < 0) - { - goto errout_with_semaphore; - } - - /* Get a pointer to the first directory entry in the sector */ - - direntry = fs->fs_buffer; - - /* Now erase the contents of fs_buffer */ - - fs->fs_currentsector = dirsector; - memset(direntry, 0, fs->fs_hwsectorsize); - - /* Now clear all sectors in the new directory cluster (except for the first) */ - - for (i = 1; i < fs->fs_fatsecperclus; i++) - { - ret = fat_hwwrite(fs, direntry, ++dirsector, 1); - if (ret < 0) - { - goto errout_with_semaphore; - } - } - - /* Now create the "." directory entry in the first directory slot. These - * are special directory entries and are not handled by the normal directory - * management routines. - */ - - memset(&direntry[DIR_NAME], ' ', DIR_MAXFNAME); - direntry[DIR_NAME] = '.'; - DIR_PUTATTRIBUTES(direntry, FATATTR_DIRECTORY); - - crtime = fat_systime2fattime(); - DIR_PUTCRTIME(direntry, crtime & 0xffff); - DIR_PUTWRTTIME(direntry, crtime & 0xffff); - DIR_PUTCRDATE(direntry, crtime >> 16); - DIR_PUTWRTDATE(direntry, crtime >> 16); - - /* Create ".." directory entry in the second directory slot */ - - direntry2 = direntry + DIR_SIZE; - - /* So far, the two entries are nearly the same */ - - memcpy(direntry2, direntry, DIR_SIZE); - direntry2[DIR_NAME+1] = '.'; - - /* Now add the cluster information to both directory entries */ - - DIR_PUTFSTCLUSTHI(direntry, dircluster >> 16); - DIR_PUTFSTCLUSTLO(direntry, dircluster); - - parentcluster = dirinfo.dir.fd_startcluster; - if (fs->fs_type != FSTYPE_FAT32 && parentcluster == fs->fs_rootbase) - { - parentcluster = 0; - } - - DIR_PUTFSTCLUSTHI(direntry2, parentcluster >> 16); - DIR_PUTFSTCLUSTLO(direntry2, parentcluster); - - /* Save the first sector of the directory cluster and re-read - * the parentsector - */ - - fs->fs_dirty = true; - ret = fat_fscacheread(fs, parentsector); - if (ret < 0) - { - goto errout_with_semaphore; - } - - /* Write the new entry directory entry in the parent directory */ - - ret = fat_dirwrite(fs, &dirinfo, FATATTR_DIRECTORY, crtime); - if (ret < 0) - { - goto errout_with_semaphore; - } - - /* Set subdirectory start cluster. We assume that fat_dirwrite() did not - * change the sector in the cache. - */ - - direntry = &fs->fs_buffer[dirinfo.fd_seq.ds_offset]; - DIR_PUTFSTCLUSTLO(direntry, dircluster); - DIR_PUTFSTCLUSTHI(direntry, dircluster >> 16); - fs->fs_dirty = true; - - /* Now update the FAT32 FSINFO sector */ - - ret = fat_updatefsinfo(fs); - if (ret < 0) - { - goto errout_with_semaphore; - } - - fat_semgive(fs); - return OK; - -errout_with_semaphore: - fat_semgive(fs); - return ret; -} - -/**************************************************************************** - * Name: fat_rmdir - * - * Description: Remove a directory - * - ****************************************************************************/ - -int fat_rmdir(struct inode *mountpt, const char *relpath) -{ - struct fat_mountpt_s *fs; - int ret; - - /* Sanity checks */ - - DEBUGASSERT(mountpt && mountpt->i_private); - - /* Get the mountpoint private data from the inode structure */ - - fs = mountpt->i_private; - - /* Check if the mount is still healthy */ - - fat_semtake(fs); - ret = fat_checkmount(fs); - if (ret == OK) - { - /* If the directory is open, the correct behavior is to remove the directory - * name, but to keep the directory cluster chain in place until the last - * open reference to the directory is closed. - */ - -#ifdef CONFIG_CPP_HAVE_WARNING -# warning "Need to defer deleting cluster chain if the directory is open" -#endif - - /* Remove the directory */ - - ret = fat_remove(fs, relpath, true); - } - - fat_semgive(fs); - return ret; -} - -/**************************************************************************** - * Name: fat_rename - * - * Description: Rename a file or directory - * - ****************************************************************************/ - -int fat_rename(struct inode *mountpt, const char *oldrelpath, - const char *newrelpath) -{ - struct fat_mountpt_s *fs; - struct fat_dirinfo_s dirinfo; - struct fat_dirseq_s dirseq; - uint8_t *direntry; - uint8_t dirstate[DIR_SIZE-DIR_ATTRIBUTES]; - int ret; - - /* Sanity checks */ - - DEBUGASSERT(mountpt && mountpt->i_private); - - /* Get the mountpoint private data from the inode structure */ - - fs = mountpt->i_private; - - /* Check if the mount is still healthy */ - - fat_semtake(fs); - ret = fat_checkmount(fs); - if (ret != OK) - { - goto errout_with_semaphore; - } - - /* Find the directory entry for the oldrelpath (there may be multiple - * directory entries if long file name support is enabled). - */ - - ret = fat_finddirentry(fs, &dirinfo, oldrelpath); - if (ret != OK) - { - /* Some error occurred -- probably -ENOENT */ - - goto errout_with_semaphore; - } - - /* One more check: Make sure that the oldrelpath does not refer to the - * root directory. We can't rename the root directory. - */ - - if (dirinfo.fd_root) - { - ret = -EXDEV; - goto errout_with_semaphore; - } - - /* Save the information that will need to recover the directory sector and - * directory entry offset to the old directory. - * - * Save the positional information of the old directory entry. - */ - - memcpy(&dirseq, &dirinfo.fd_seq, sizeof(struct fat_dirseq_s)); - - /* Save the non-name-related portion of the directory entry intact */ - - direntry = &fs->fs_buffer[dirinfo.fd_seq.ds_offset]; - memcpy(dirstate, &direntry[DIR_ATTRIBUTES], DIR_SIZE-DIR_ATTRIBUTES); - - /* Now find the directory where we should create the newpath object */ - - ret = fat_finddirentry(fs, &dirinfo, newrelpath); - if (ret == OK) - { - /* It is an error if the object at newrelpath already exists */ - - ret = -EEXIST; - goto errout_with_semaphore; - } - - /* What we expect is -ENOENT mean that the full directory path was - * followed but that the object does not exists in the terminal directory. - */ - - if (ret != -ENOENT) - { - goto errout_with_semaphore; - } - - /* Reserve a directory entry. If long file name support is enabled, then - * this might, in fact, allocate a sequence of directory entries. A side - * effect of fat_allocatedirentry() in either case is that it leaves the - * short file name entry in the sector cache. - */ - - ret = fat_allocatedirentry(fs, &dirinfo); - if (ret != OK) - { - goto errout_with_semaphore; - } - - /* Then write the new file name into the directory entry. This, of course, - * may involve writing multiple directory entries if long file name - * support is enabled. A side effect of fat_allocatedirentry() in either - * case is that it leaves the short file name entry in the sector cache. - */ - - ret = fat_dirnamewrite(fs, &dirinfo); - if (ret < 0) - { - goto errout_with_semaphore; - } - - /* Copy the unchanged information into the new short file name entry. */ - - direntry = &fs->fs_buffer[dirinfo.fd_seq.ds_offset]; - memcpy(&direntry[DIR_ATTRIBUTES], dirstate, DIR_SIZE-DIR_ATTRIBUTES); - fs->fs_dirty = true; - - /* Remove the old entry, flushing the new directory entry to disk. If - * the old file name was a long file name, then multiple directory - * entries may be freed. - */ - - ret = fat_freedirentry(fs, &dirseq); - if (ret < 0) - { - goto errout_with_semaphore; - } - - /* Write the old entry to disk and update FSINFO if necessary */ - - ret = fat_updatefsinfo(fs); - if (ret < 0) - { - goto errout_with_semaphore; - } - - fat_semgive(fs); - return OK; - -errout_with_semaphore: - fat_semgive(fs); - return ret; -} - -/**************************************************************************** - * Name: fat_stat - * - * Description: Return information about a file or directory - * - ****************************************************************************/ - -static int fat_stat(struct inode *mountpt, const char *relpath, struct stat *buf) -{ - struct fat_mountpt_s *fs; - struct fat_dirinfo_s dirinfo; - uint16_t fatdate; - uint16_t date2; - uint16_t fattime; - uint8_t *direntry; - uint8_t attribute; - int ret; - - /* Sanity checks */ - - DEBUGASSERT(mountpt && mountpt->i_private); - - /* Get the mountpoint private data from the inode structure */ - - fs = mountpt->i_private; - - /* Check if the mount is still healthy */ - - fat_semtake(fs); - ret = fat_checkmount(fs); - if (ret != OK) - { - goto errout_with_semaphore; - } - - /* Find the directory entry corresponding to relpath. */ - - ret = fat_finddirentry(fs, &dirinfo, relpath); - - /* If nothing was found, then we fail with the reported error */ - - if (ret < 0) - { - goto errout_with_semaphore; - } - - memset(buf, 0, sizeof(struct stat)); - if (dirinfo.fd_root) - { - /* It's directory name of the mount point */ - - buf->st_mode = S_IFDIR|S_IROTH|S_IRGRP|S_IRUSR|S_IWOTH|S_IWGRP|S_IWUSR; - ret = OK; - goto errout_with_semaphore; - } - - /* Get the FAT attribute and map it so some meaningful mode_t values */ - - direntry = &fs->fs_buffer[dirinfo.fd_seq.ds_offset]; - attribute = DIR_GETATTRIBUTES(direntry); - if ((attribute & FATATTR_VOLUMEID) != 0) - { - ret = -ENOENT; - goto errout_with_semaphore; - } - - /* Set the access permissions. The file/directory is always readable - * by everyone but may be writeable by no-one. - */ - - buf->st_mode = S_IROTH|S_IRGRP|S_IRUSR; - if ((attribute & FATATTR_READONLY) == 0) - { - buf->st_mode |= S_IWOTH|S_IWGRP|S_IWUSR; - } - - /* We will report only types file or directory */ - - if ((attribute & FATATTR_DIRECTORY) != 0) - { - buf->st_mode |= S_IFDIR; - } - else - { - buf->st_mode |= S_IFREG; - } - - /* File/directory size, access block size */ - - buf->st_size = DIR_GETFILESIZE(direntry); - buf->st_blksize = fs->fs_fatsecperclus * fs->fs_hwsectorsize; - buf->st_blocks = (buf->st_size + buf->st_blksize - 1) / buf->st_blksize; - - /* Times */ - - fatdate = DIR_GETWRTDATE(direntry); - fattime = DIR_GETWRTTIME(direntry); - buf->st_mtime = fat_fattime2systime(fattime, fatdate); - - date2 = DIR_GETLASTACCDATE(direntry); - if (fatdate == date2) - { - buf->st_atime = buf->st_mtime; - } - else - { - buf->st_atime = fat_fattime2systime(0, date2); - } - - fatdate = DIR_GETCRDATE(direntry); - fattime = DIR_GETCRTIME(direntry); - buf->st_ctime = fat_fattime2systime(fattime, fatdate); - - ret = OK; - -errout_with_semaphore: - fat_semgive(fs); - return ret; -} - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - diff --git a/nuttx/fs/fat/fs_fat32.h b/nuttx/fs/fat/fs_fat32.h deleted file mode 100644 index 81f3f4675..000000000 --- a/nuttx/fs/fat/fs_fat32.h +++ /dev/null @@ -1,937 +0,0 @@ -/**************************************************************************** - * fs/fat/fs_fat32.h - * - * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt <gnutt@nuttx.org> - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name NuttX nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -#ifndef __FS_FAT_FS_FAT32_H -#define __FS_FAT_FS_FAT32_H - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <nuttx/config.h> - -#include <sys/types.h> -#include <stdint.h> -#include <stdbool.h> -#include <semaphore.h> -#include <time.h> - -#include <nuttx/kmalloc.h> -#include <nuttx/fs/dirent.h> - -/**************************************************************************** - * Definitions - ****************************************************************************/ - -/**************************************************************************** - * These offsets describes the master boot record. - * - * The folowing fields are common to FAT12/16/32 (but all value descriptions - * refer to the interpretation under FAT32. - */ - -#define BS_JUMP 0 /* 3@0: Jump instruction to boot code (ignored) */ -#define BS_OEMNAME 3 /* 8@3: Usually "MSWIN4.1" */ -#define BS_BYTESPERSEC 11 /* 2@11: Bytes per sector: 512, 1024, 2048, 4096 */ -#define BS_SECPERCLUS 13 /* 1@13: Sectors per allocation unit: 2**n, n=0..7 */ -#define BS_RESVDSECCOUNT 14 /* 2@14: Reserved sector count: Usually 32 */ -#define BS_NUMFATS 16 /* 1@16: Number of FAT data structures: always 2 */ -#define BS_ROOTENTCNT 17 /* 2@17: FAT12/16: Must be 0 for FAT32 */ -#define BS_TOTSEC16 19 /* 2@19: FAT12/16: Must be 0, see BS_TOTSEC32 */ -#define BS_MEDIA 21 /* 1@21: Media code: f0, f8, f9-fa, fc-ff */ -#define BS_FATSZ16 22 /* 2@22: FAT12/16: Must be 0, see BS_FATSZ32 */ -#define BS_SECPERTRK 24 /* 2@24: Sectors per track geometry value */ -#define BS_NUMHEADS 26 /* 2@26: Number of heads geometry value */ -#define BS_HIDSEC 28 /* 4@28: Count of hidden sectors preceding FAT */ -#define BS_TOTSEC32 32 /* 4@32: Total count of sectors on the volume */ - -/* The following fields are only valid for FAT12/16 */ - -#define BS16_DRVNUM 36 /* 1@36: Drive number for MSDOS bootstrap */ - /* 1@37: Reserved (zero) */ -#define BS16_BOOTSIG 38 /* 1@38: Extended boot signature: 0x29 if following valid */ -#define BS16_VOLID 39 /* 4@39: Volume serial number */ -#define BS16_VOLLAB 43 /* 11@43: Volume label */ -#define BS16_FILESYSTYPE 54 /* 8@54: "FAT12 ", "FAT16 ", or "FAT " */ - -#define BS16_BOOTCODE 62 /* Boot code may be placed in the remainder of the sector */ -#define BS16_BOOTCODESIZE 448 - -/* The following fields are only valid for FAT32 */ - -#define BS32_FATSZ32 36 /* 4@36: Count of sectors occupied by one FAT */ -#define BS32_EXTFLAGS 40 /* 2@40: 0-3:Active FAT, 7=0 both FATS, 7=1 one FAT */ -#define BS32_FSVER 42 /* 2@42: MSB:Major LSB:Minor revision number (0.0) */ -#define BS32_ROOTCLUS 44 /* 4@44: Cluster no. of 1st cluster of root dir */ -#define BS32_FSINFO 48 /* 2@48: Sector number of fsinfo structure. Usually 1. */ -#define BS32_BKBOOTSEC 50 /* 2@50: Sector number of boot record. Usually 6 */ - /* 12@52: Reserved (zero) */ -#define BS32_DRVNUM 64 /* 1@64: Drive number for MSDOS bootstrap */ - /* 1@65: Reserved (zero) */ -#define BS32_BOOTSIG 66 /* 1@66: Extended boot signature: 0x29 if following valid */ -#define BS32_VOLID 67 /* 4@67: Volume serial number */ -#define BS32_VOLLAB 71 /* 11@71: Volume label */ -#define BS32_FILESYSTYPE 82 /* 8@82: "FAT12 ", "FAT16 ", or "FAT " */ - -#define BS32_BOOTCODE 90 /* Boot code may be placed in the remainder of the sector */ -#define BS32_BOOTCODESIZE 420 - -/* If the sector is not an MBR, then it could have a partition table at - * this offset. - */ - -#define MBR_TABLE 446 - -/* The magic bytes at the end of the MBR are common to FAT12/16/32 */ - -#define BS_SIGNATURE 510 /* 2@510: Valid MBRs have 0x55aa here */ - -#define BOOT_SIGNATURE16 0xaa55 -#define BOOT_SIGNATURE32 0xaa550000 - -/* The extended boot signature (BS16/32_BOOTSIG) */ - -#define EXTBOOT_SIGNATURE 0x29 - -/**************************************************************************** - * These offsets describes the partition table. - */ - /* 446@0: Generally unused and zero; but may - * include IDM Boot Manager menu entry at 8@394 */ -#define PART_ENTRY(n) (446+((n) << 4)) /* n = 0,1,2,3 */ -#define PART_ENTRY1 446 /* 16@446: Partition table, first entry */ -#define PART_ENTRY2 462 /* 16@462: Partition table, second entry */ -#define PART_ENTRY3 478 /* 16@478: Partition table, third entry */ -#define PART_ENTRY4 494 /* 16@494: Partition table, fourth entry */ -#define PART_SIGNATURE 510 /* 2@510: Valid partitions have 0x55aa here */ - -/**************************************************************************** - * These offsets describes one partition table entry. NOTE that ent entries - * are aligned to 16-bit offsets so that the STARTSECTOR and SIZE values are - * not properly aligned. - */ - -#define PART_BOOTINDICATOR 0 /* 1@0: Boot indicator (0x80: active;0x00:otherwise) */ -#define PART_STARTCHS 1 /* 3@1: Starting Cylinder/Head/Sector values */ -#define PART_TYPE 4 /* 1@4: Partition type description */ -#define PART_ENDCHS 5 /* 3@5: Ending Cylinder/Head/Sector values */ -#define PART_STARTSECTOR 8 /* 4@8: Starting sector */ -#define PART_SIZE 12 /* 4@12: Partition size (in sectors) */ - -/**************************************************************************** - * Partition table types. - */ - -#define PART_TYPE_NONE 0 /* No partition */ -#define PART_TYPE_FAT12 1 /* FAT12 */ -#define PART_TYPE_FAT16A 4 /* FAT16 (Partition smaller than 32MB) */ -#define PART_TYPE_EXT 5 /* Extended MS-DOS Partition */ -#define PART_TYPE_FAT16B 6 /* FAT16 (Partition larger than 32MB) */ -#define PART_TYPE_FAT32 11 /* FAT32 (Partition up to 2048Gb) */ -#define PART_TYPE_FAT32X 12 /* Same as 11, but uses LBA1 0x13 extensions */ -#define PART_TYPE_FAT16X 14 /* Same as 6, but uses LBA1 0x13 extensions */ -#define PART_TYPE_EXTX 15 /* Same as 5, but uses LBA1 0x13 extensions */ - -/**************************************************************************** - * Each FAT "short" 8.3 file name directory entry is 32-bytes long. - * - * Sizes and limits - */ - -/**************************************************************************** - * Each FAT "short" 8.3 file name directory entry is 32-bytes long. - * - * Sizes and limits - */ - -#define DIR_MAXFNAME 11 /* Max short name size is 8+3 = 11 */ - -/* The following define offsets relative to the beginning of a directory - * entry. - */ - -#define DIR_NAME 0 /* 11@ 0: NAME: 8 bytes + 3 byte extension */ -#define DIR_ATTRIBUTES 11 /* 1@11: File attibutes (see below) */ -#define DIR_NTRES 12 /* 1@12: Reserved for use by NT */ -#define DIR_CRTTIMETENTH 13 /* 1@13: Tenth sec creation timestamp */ -#define DIR_CRTIME 14 /* 2@14: Time file created */ -#define DIR_CRDATE 16 /* 2@16: Date file created */ -#define DIR_LASTACCDATE 18 /* 2@19: Last access date */ -#define DIR_FSTCLUSTHI 20 /* 2@20: MS first cluster number */ -#define DIR_WRTTIME 22 /* 2@22: Time of last write */ -#define DIR_WRTDATE 24 /* 2@24: Date of last write */ -#define DIR_FSTCLUSTLO 26 /* 2@26: LS first cluster number */ -#define DIR_FILESIZE 28 /* 4@28: File size in bytes */ -#define DIR_SIZE 32 /* The size of one directory entry */ -#define DIR_SHIFT 5 /* log2 of DIR_SIZE */ - -/* First byte of the directory name has special meanings: */ - -#define DIR0_EMPTY 0xe5 /* The directory entry is empty */ -#define DIR0_ALLEMPTY 0x00 /* This entry and all following are empty */ -#define DIR0_E5 0x05 /* The actual value is 0xe5 */ - -/* NTRES flags in the FAT directory */ - -#define FATNTRES_LCNAME 0x08 /* Lower case in name */ -#define FATNTRES_LCEXT 0x10 /* Lower case in extension */ - -/* Directory indexing helper. Each directory entry is 32-bytes in length. - * The number of directory entries in a sector then varies with the size - * of the sector supported in hardware. - */ - -#define DIRSEC_NDXMASK(f) (((f)->fs_hwsectorsize - 1) >> 5) -#define DIRSEC_NDIRS(f) (((f)->fs_hwsectorsize) >> 5) -#define DIRSEC_BYTENDX(f,i) (((i) & DIRSEC_NDXMASK(fs)) << 5) - -#define SEC_NDXMASK(f) ((f)->fs_hwsectorsize - 1) -#define SEC_NSECTORS(f,n) ((n) / (f)->fs_hwsectorsize) - -#define CLUS_NDXMASK(f) ((f)->fs_fatsecperclus - 1) - -/**************************************************************************** - * The FAT "long" file name (LFN) directory entry */ - -#ifdef CONFIG_FAT_LFN - -/* Sizes and limits */ - -# ifndef CONFIG_FAT_MAXFNAME /* The maximum support filename can be limited */ -# define LDIR_MAXFNAME 255 /* Max unicode characters in file name */ -# elif CONFIG_FAT_MAXFNAME <= 255 -# define LDIR_MAXFNAME CONFIG_FAT_MAXFNAME -# else -# error "Illegal value for CONFIG_FAT_MAXFNAME" -# endif - -# define LDIR_MAXLFNCHARS 13 /* Max unicode characters in one LFN entry */ -# define LDIR_MAXLFNS 20 /* Max number of LFN entries */ - -/* LFN directory entry offsets */ - -# define LDIR_SEQ 0 /* 1@ 0: Sequence number */ -# define LDIR_WCHAR1_5 1 /* 10@ 1: File name characters 1-5 (5 Unicode characters) */ -# define LDIR_ATTRIBUTES 11 /* 1@11: File attributes (always 0x0f) */ -# define LDIR_NTRES 12 /* 1@12: Reserved for use by NT (always 0x00) */ -# define LDIR_CHECKSUM 13 /* 1@13: Checksum of the DOS filename */ -# define LDIR_WCHAR6_11 14 /* 12@14: File name characters 6-11 (6 Unicode characters) */ -# define LDIR_FSTCLUSTLO 26 /* 2@26: First cluster (always 0x0000) */ -# define LDIR_WCHAR12_13 28 /* 4@28: File name characters 12-13 (2 Unicode characters) */ - -/* LFN sequence number and allocation status */ - -# define LDIR0_EMPTY DIR0_EMPTY /* The directory entry is empty */ -# define LDIR0_ALLEMPTY DIR0_ALLEMPTY /* This entry and all following are empty */ -# define LDIR0_E5 DIR0_E5 /* The actual value is 0xe5 */ -# define LDIR0_LAST 0x40 /* Last LFN in file name (appears first) */ -# define LDIR0_SEQ_MASK 0x1f /* Mask for sequence number (1-20) */ - -/* The LFN entry attribute */ - -# define LDDIR_LFNATTR 0x0f -#endif - -/**************************************************************************** - * File system types */ - -#define FSTYPE_FAT12 0 -#define FSTYPE_FAT16 1 -#define FSTYPE_FAT32 2 - -/* File buffer flags */ - -#define FFBUFF_VALID 1 -#define FFBUFF_DIRTY 2 -#define FFBUFF_MODIFIED 4 - -/**************************************************************************** - * These offset describe the FSINFO sector - */ - -#define FSI_LEADSIG 0 /* 4@0: 0x41615252 = "RRaA" */ - /* 480@4: Reserved (zero) */ -#define FSI_STRUCTSIG 484 /* 4@484: 0x61417272 = "rrAa" */ -#define FSI_FREECOUNT 488 /* 4@488: Last free cluster count on volume */ -#define FSI_NXTFREE 492 /* 4@492: Cluster number of 1st free cluster */ - /* 12@496: Reserved (zero) */ -#define FSI_TRAILSIG 508 /* 4@508: 0xaa550000 */ - -/**************************************************************************** - * FAT values - */ - -#define FAT_EOF 0x0ffffff8 -#define FAT_BAD 0x0ffffff7 - -/**************************************************************************** - * Maximum cluster by FAT type. This is the key value used to distinquish - * between FAT12, 16, and 32. - */ - -/* FAT12: For M$, the calculation is ((1 << 12) - 19). But we will follow the - * Linux tradition of allowing slightly more clusters for FAT12. - */ - -#define FAT_MAXCLUST12 ((1 << 12) - 16) - -/* FAT16: For M$, the calculation is ((1 << 16) - 19). (The uint32_t cast is - * needed for architectures where int is only 16 bits). - */ - -#define FAT_MINCLUST16 (FAT_MAXCLUST12 + 1) -#define FAT_MAXCLUST16 (((uint32_t)1 << 16) - 16) - -/* FAT32: M$ reserves the MS 4 bits of a FAT32 FAT entry so only 18 bits are - * available. For M$, the calculation is ((1 << 28) - 19). (The uint32_t cast - * is needed for architectures where int is only 16 bits). - */ - -#define FAT_MINCLUST32 65524 -/* #define FAT_MINCLUST32 (FAT_MAXCLUST16 + 1) */ -#define FAT_MAXCLUST32 (((uint32_t)1 << 28) - 16) - -/**************************************************************************** - * Access to data in raw sector data */ - -#define UBYTE_VAL(p,o) (((uint8_t*)(p))[o]) -#define UBYTE_PTR(p,o) &UBYTE_VAL(p,o) -#define UBYTE_PUT(p,o,v) (UBYTE_VAL(p,o)=(uint8_t)(v)) - -#define UINT16_PTR(p,o) ((uint16_t*)UBYTE_PTR(p,o)) -#define UINT16_VAL(p,o) (*UINT16_PTR(p,o)) -#define UINT16_PUT(p,o,v) (UINT16_VAL(p,o)=(uint16_t)(v)) - -#define UINT32_PTR(p,o) ((uint32_t*)UBYTE_PTR(p,o)) -#define UINT32_VAL(p,o) (*UINT32_PTR(p,o)) -#define UINT32_PUT(p,o,v) (UINT32_VAL(p,o)=(uint32_t)(v)) - -/* Regardless of the endian-ness of the target or alignment of the data, no - * special operations are required for byte, string or byte array accesses. - * The FAT data stream is little endian so multiple byte values must be - * accessed byte-by-byte for big-endian targets. - */ - -#define MBR_GETSECPERCLUS(p) UBYTE_VAL(p,BS_SECPERCLUS) -#define MBR_GETNUMFATS(p) UBYTE_VAL(p,BS_NUMFATS) -#define MBR_GETMEDIA(p) UBYTE_VAL(p,BS_MEDIA) -#define MBR_GETDRVNUM16(p) UBYTE_VAL(p,BS16_DRVNUM) -#define MBR_GETDRVNUM32(p) UBYTE_VAL(p,BS32_DRVNUM) -#define MBR_GETBOOTSIG16(p) UBYTE_VAL(p,BS16_BOOTSIG) -#define MBR_GETBOOTSIG32(p) UBYTE_VAL(p,BS32_BOOTSIG) - -#define PART_GETTYPE(n,p) UBYTE_VAL(p,PART_ENTRY(n)+PART_TYPE) -#define PART1_GETTYPE(p) UBYTE_VAL(p,PART_ENTRY1+PART_TYPE) -#define PART2_GETTYPE(p) UBYTE_VAL(p,PART_ENTRY2+PART_TYPE) -#define PART3_GETTYPE(p) UBYTE_VAL(p,PART_ENTRY3+PART_TYPE) -#define PART4_GETTYPE(p) UBYTE_VAL(p,PART_ENTRY4+PART_TYPE) - -#define DIR_GETATTRIBUTES(p) UBYTE_VAL(p,DIR_ATTRIBUTES) -#define DIR_GETNTRES(p) UBYTE_VAL(p,DIR_NTRES) -#define DIR_GETCRTTIMETENTH(p) UBYTE_VAL(p,DIR_CRTTIMETENTH) - -#ifdef CONFIG_FAT_LFN -# define LDIR_GETSEQ(p) UBYTE_VAL(p,LDIR_SEQ) -# define LDIR_GETATTRIBUTES(p) UBYTE_VAL(p,LDIR_ATTRIBUTES) -# define LDIR_GETNTRES(p) UBYTE_VAL(p,LDIR_NTRES) -# define LDIR_GETCHECKSUM(p) UBYTE_VAL(p,LDIR_CHECKSUM) -#endif - -#define MBR_PUTSECPERCLUS(p,v) UBYTE_PUT(p,BS_SECPERCLUS,v) -#define MBR_PUTNUMFATS(p,v) UBYTE_PUT(p,BS_NUMFATS,v) -#define MBR_PUTMEDIA(p,v) UBYTE_PUT(p,BS_MEDIA,v) -#define MBR_PUTDRVNUM16(p,v) UBYTE_PUT(p,BS16_DRVNUM,v) -#define MBR_PUTDRVNUM32(p,v) UBYTE_PUT(p,BS32_DRVNUM,v) -#define MBR_PUTBOOTSIG16(p,v) UBYTE_PUT(p,BS16_BOOTSIG,v) -#define MBR_PUTBOOTSIG32(p,v) UBYTE_PUT(p,BS32_BOOTSIG,v) - -#define PART_PUTTYPE(n,p,v) UBYTE_PUT(p,PART_ENTRY(n)+PART_TYPE,v) -#define PART1_PUTTYPE(p,v) UBYTE_PUT(p,PART_ENTRY1+PART_TYPE,v) -#define PART2_PUTTYPE(p,v) UBYTE_PUT(p,PART_ENTRY2+PART_TYPE,v) -#define PART3_PUTTYPE(p,v) UBYTE_PUT(p,PART_ENTRY3+PART_TYPE,v) -#define PART4_PUTTYPE(p,v) UBYTE_PUT(p,PART_ENTRY4+PART_TYPE,v) - -#define DIR_PUTATTRIBUTES(p,v) UBYTE_PUT(p,DIR_ATTRIBUTES,v) -#define DIR_PUTNTRES(p,v) UBYTE_PUT(p,DIR_NTRES,v) -#define DIR_PUTCRTTIMETENTH(p,v) UBYTE_PUT(p,DIR_CRTTIMETENTH,v) - -#ifdef CONFIG_FAT_LFN -# define LDIR_PUTSEQ(p,v) UBYTE_PUT(p,LDIR_SEQ,v) -# define LDIR_PUTATTRIBUTES(p,v) UBYTE_PUT(p,LDIR_ATTRIBUTES,v) -# define LDIR_PUTNTRES(p,v) UBYTE_PUT(p,LDIR_NTRES,v) -# define LDIR_PUTCHECKSUM(p,v) UBYTE_PUT(p,LDIR_CHECKSUM,v) -#endif - -/* For the all targets, unaligned values need to be accessed byte-by-byte. - * Some architectures may handle unaligned accesses with special interrupt - * handlers. But even in that case, it is more efficient to avoid the traps. - */ - -/* Unaligned multi-byte access macros */ - -#define MBR_GETBYTESPERSEC(p) fat_getuint16(UBYTE_PTR(p,BS_BYTESPERSEC)) -#define MBR_GETROOTENTCNT(p) fat_getuint16(UBYTE_PTR(p,BS_ROOTENTCNT)) -#define MBR_GETTOTSEC16(p) fat_getuint16(UBYTE_PTR(p,BS_TOTSEC16)) -#define MBR_GETVOLID16(p) fat_getuint32(UBYTE_PTR(p,BS16_VOLID)) -#define MBR_GETVOLID32(p) fat_getuint32(UBYTE_PTR(p,BS32_VOLID)) - -#define PART_GETSTARTSECTOR(n,p) fat_getuint32(UBYTE_PTR(p,PART_ENTRY(n)+PART_STARTSECTOR)) -#define PART_GETSIZE(n,p) fat_getuint32(UBYTE_PTR(p,PART_ENTRY(n)+PART_SIZE)) -#define PART1_GETSTARTSECTOR(p) fat_getuint32(UBYTE_PTR(p,PART_ENTRY1+PART_STARTSECTOR)) -#define PART1_GETSIZE(p) fat_getuint32(UBYTE_PTR(p,PART_ENTRY1+PART_SIZE)) -#define PART2_GETSTARTSECTOR(p) fat_getuint32(UBYTE_PTR(p,PART_ENTRY2+PART_STARTSECTOR)) -#define PART2_GETSIZE(p) fat_getuint32(UBYTE_PTR(p,PART_ENTRY2+PART_SIZE)) -#define PART3_GETSTARTSECTOR(p) fat_getuint32(UBYTE_PTR(p,PART_ENTRY3+PART_STARTSECTOR)) -#define PART3_GETSIZE(p) fat_getuint32(UBYTE_PTR(p,PART_ENTRY3+PART_SIZE)) -#define PART4_GETSTARTSECTOR(p) fat_getuint32(UBYTE_PTR(p,PART_ENTRY4+PART_STARTSECTOR)) -#define PART4_GETSIZE(p) fat_getuint32(UBYTE_PTR(p,PART_ENTRY4+PART_SIZE)) - -#define MBR_PUTBYTESPERSEC(p,v) fat_putuint16(UBYTE_PTR(p,BS_BYTESPERSEC),v) -#define MBR_PUTROOTENTCNT(p,v) fat_putuint16(UBYTE_PTR(p,BS_ROOTENTCNT),v) -#define MBR_PUTTOTSEC16(p,v) fat_putuint16(UBYTE_PTR(p,BS_TOTSEC16),v) -#define MBR_PUTVOLID16(p,v) fat_putuint32(UBYTE_PTR(p,BS16_VOLID),v) -#define MBR_PUTVOLID32(p,v) fat_putuint32(UBYTE_PTR(p,BS32_VOLID),v) - -#define PART_PUTSTARTSECTOR(n,p,v) fat_putuint32(UBYTE_PTR(p,PART_ENTRY(n)+PART_STARTSECTOR),v) -#define PART_PUTSIZE(n,p,v) fat_putuint32(UBYTE_PTR(p,PART_ENTRY(n)+PART_SIZE),v) -#define PART1_PUTSTARTSECTOR(p,v) fat_putuint32(UBYTE_PTR(p,PART_ENTRY1+PART_STARTSECTOR),v) -#define PART1_PUTSIZE(p,v) fat_putuint32(UBYTE_PTR(p,PART_ENTRY1+PART_SIZE),v) -#define PART2_PUTSTARTSECTOR(p,v) fat_putuint32(UBYTE_PTR(p,PART_ENTRY2+PART_STARTSECTOR),v) -#define PART2_PUTSIZE(p,v) fat_putuint32(UBYTE_PTR(p,PART_ENTRY2+PART_SIZE),v) -#define PART3_PUTSTARTSECTOR(p,v) fat_putuint32(UBYTE_PTR(p,PART_ENTRY3+PART_STARTSECTOR),v) -#define PART3_PUTSIZE(p,v) fat_putuint32(UBYTE_PTR(p,PART_ENTRY3+PART_SIZE),v) -#define PART4_PUTSTARTSECTOR(p,v) fat_putuint32(UBYTE_PTR(p,PART_ENTRY4+PART_STARTSECTOR),v) -#define PART4_PUTSIZE(p,v) fat_putuint32(UBYTE_PTR(p,PART_ENTRY4+PART_SIZE),v) - -#ifdef CONFIG_FAT_LFN -# define LDIR_PTRWCHAR1_5(p) UBYTE_PTR(p,LDIR_WCHAR1_5) -# define LDIR_PTRWCHAR6_11(p) UBYTE_PTR(p,LDIR_WCHAR6_11) -# define LDIR_PTRWCHAR12_13(p) UBYTE_PTR(p,LDIR_WCHAR12_13) -#endif - -/* But for multi-byte values, the endian-ness of the target vs. the little - * endian order of the byte stream or alignment of the data within the byte - * stream can force special, byte-by-byte accesses. - */ - -#ifdef CONFIG_ENDIAN_BIG - -/* If the target is big-endian, then even aligned multi-byte values must be - * accessed byte-by-byte. - */ - -# define MBR_GETRESVDSECCOUNT(p) fat_getuint16(UBYTE_PTR(p,BS_RESVDSECCOUNT)) -# define MBR_GETFATSZ16(p) fat_getuint16(UBYTE_PTR(p,BS_FATSZ16)) -# define MBR_GETSECPERTRK(p) fat_getuint16(UBYTE_PTR(p,BS_SECPERTRK)) -# define MBR_GETNUMHEADS(p) fat_getuint16(UBYTE_PTR(p,BS_NUMHEADS)) -# define MBR_GETHIDSEC(p) fat_getuint32(UBYTE_PTR(p,BS_HIDSEC)) -# define MBR_GETTOTSEC32(p) fat_getuint32(UBYTE_PTR(p,BS_TOTSEC32)) -# define MBR_GETFATSZ32(p) fat_getuint32(UBYTE_PTR(p,BS32_FATSZ32)) -# define MBR_GETEXTFLAGS(p) fat_getuint16(UBYTE_PTR(p,BS32_EXTFLAGS)) -# define MBR_GETFSVER(p) fat_getuint16(UBYTE_PTR(p,BS32_FSVER)) -# define MBR_GETROOTCLUS(p) fat_getuint32(UBYTE_PTR(p,BS32_ROOTCLUS)) -# define MBR_GETFSINFO(p) fat_getuint16(UBYTE_PTR(p,BS32_FSINFO)) -# define MBR_GETBKBOOTSEC(p) fat_getuint16(UBYTE_PTR(p,BS32_BKBOOTSEC)) -# define MBR_GETSIGNATURE(p) fat_getuint16(UBYTE_PTR(p,BS_SIGNATURE)) - -# define FSI_GETLEADSIG(p) fat_getuint32(UBYTE_PTR(p,FSI_LEADSIG)) -# define FSI_GETSTRUCTSIG(p) fat_getuint32(UBYTE_PTR(p,FSI_STRUCTSIG)) -# define FSI_GETFREECOUNT(p) fat_getuint32(UBYTE_PTR(p,FSI_FREECOUNT)) -# define FSI_GETNXTFREE(p) fat_getuint32(UBYTE_PTR(p,FSI_NXTFREE)) -# define FSI_GETTRAILSIG(p) fat_getuint32(UBYTE_PTR(p,FSI_TRAILSIG)) - -# define DIR_GETCRTIME(p) fat_getuint16(UBYTE_PTR(p,DIR_CRTIME)) -# define DIR_GETCRDATE(p) fat_getuint16(UBYTE_PTR(p,DIR_CRDATE)) -# define DIR_GETLASTACCDATE(p) fat_getuint16(UBYTE_PTR(p,DIR_LASTACCDATE)) -# define DIR_GETFSTCLUSTHI(p) fat_getuint16(UBYTE_PTR(p,DIR_FSTCLUSTHI)) -# define DIR_GETWRTTIME(p) fat_getuint16(UBYTE_PTR(p,DIR_WRTTIME)) -# define DIR_GETWRTDATE(p) fat_getuint16(UBYTE_PTR(p,DIR_WRTDATE)) -# define DIR_GETFSTCLUSTLO(p) fat_getuint16(UBYTE_PTR(p,DIR_FSTCLUSTLO)) -# define DIR_GETFILESIZE(p) fat_getuint32(UBYTE_PTR(p,DIR_FILESIZE)) - -# ifdef CONFIG_FAT_LFN -# define LDIR_GETWCHAR1(p) fat_getuint16(UBYTE_PTR(p,LDIR_WCHAR1_5)) -# define LDIR_GETWCHAR2(p) fat_getuint16(UBYTE_PTR(p,LDIR_WCHAR1_5+2)) -# define LDIR_GETWCHAR3(p) fat_getuint16(UBYTE_PTR(p,LDIR_WCHAR1_5+4)) -# define LDIR_GETWCHAR4(p) fat_getuint16(UBYTE_PTR(p,LDIR_WCHAR1_5+6)) -# define LDIR_GETWCHAR5(p) fat_getuint16(UBYTE_PTR(p,LDIR_WCHAR1_5+8)) -# define LDIR_GETWCHAR6(p) fat_getuint16(UBYTE_PTR(p,LDIR_WCHAR6_11)) -# define LDIR_GETWCHAR7(p) fat_getuint16(UBYTE_PTR(p,LDIR_WCHAR6_11+2)) -# define LDIR_GETWCHAR8(p) fat_getuint16(UBYTE_PTR(p,LDIR_WCHAR6_11+4)) -# define LDIR_GETWCHAR8(p) fat_getuint16(UBYTE_PTR(p,LDIR_WCHAR6_11+6)) -# define LDIR_GETWCHAR10(p) fat_getuint16(UBYTE_PTR(p,LDIR_WCHAR6_11+8)) -# define LDIR_GETWCHAR11(p) fat_getuint16(UBYTE_PTR(p,LDIR_WCHAR6_11+10)) -# define LDIR_GETWCHAR12(p) fat_getuint16(UBYTE_PTR(p,LDIR_WCHAR12_13)) -# define LDIR_GETWCHAR13(p) fat_getuint16(UBYTE_PTR(p,LDIR_WCHAR12_13+2)) -# define LDIR_GETFSTCLUSTLO(p) fat_getuint16(UBYTE_PTR(p,LDIR_FSTCLUSTLO)) -# endif - -# define FSI_GETLEADSIG(p) fat_getuint32(UBYTE_PTR(p,FSI_LEADSIG)) -# define FSI_GETSTRUCTSIG(p) fat_getuint32(UBYTE_PTR(p,FSI_STRUCTSIG)) -# define FSI_GETFREECOUNT(p) fat_getuint32(UBYTE_PTR(p,FSI_FREECOUNT)) -# define FSI_GETNXTFREE(p) fat_getuint32(UBYTE_PTR(p,FSI_NXTFREE)) -# define FSI_GETTRAILSIG(p) fat_getuint32(UBYTE_PTR(p,FSI_TRAILSIG)) - -# define FAT_GETFAT16(p,i) fat_getuint16(UBYTE_PTR(p,i)) -# define FAT_GETFAT32(p,i) fat_getuint32(UBYTE_PTR(p,i)) - -# define MBR_PUTRESVDSECCOUNT(p,v) fat_putuint16(UBYTE_PTR(p,BS_RESVDSECCOUNT),v) -# define MBR_PUTFATSZ16(p,v) fat_putuint16(UBYTE_PTR(p,BS_FATSZ16),v) -# define MBR_PUTSECPERTRK(p,v) fat_putuint16(UBYTE_PTR(p,BS_SECPERTRK),v) -# define MBR_PUTNUMHEADS(p,v) fat_putuint16(UBYTE_PTR(p,BS_NUMHEADS),v) -# define MBR_PUTHIDSEC(p,v) fat_putuint32(UBYTE_PTR(p,BS_HIDSEC),v) -# define MBR_PUTTOTSEC32(p,v) fat_putuint32(UBYTE_PTR(p,BS_TOTSEC32),v) -# define MBR_PUTFATSZ32(p,v) fat_putuint32(UBYTE_PTR(p,BS32_FATSZ32),v) -# define MBR_PUTEXTFLAGS(p,v) fat_putuint16(UBYTE_PTR(p,BS32_EXTFLAGS),v) -# define MBR_PUTFSVER(p,v) fat_putuint16(UBYTE_PTR(p,BS32_FSVER),v) -# define MBR_PUTROOTCLUS(p,v) fat_putuint32(UBYTE_PTR(p,BS32_ROOTCLUS),v) -# define MBR_PUTFSINFO(p,v) fat_putuint16(UBYTE_PTR(p,BS32_FSINFO),v) -# define MBR_PUTBKBOOTSEC(p,v) fat_putuint16(UBYTE_PTR(p,BS32_BKBOOTSEC),v) -# define MBR_PUTSIGNATURE(p,v) fat_putuint16(UBYTE_PTR(p,BS_SIGNATURE),v) - -# define FSI_PUTLEADSIG(p,v) fat_putuint32(UBYTE_PTR(p,FSI_LEADSIG),v) -# define FSI_PUTSTRUCTSIG(p,v) fat_putuint32(UBYTE_PTR(p,FSI_STRUCTSIG),v) -# define FSI_PUTFREECOUNT(p,v) fat_putuint32(UBYTE_PTR(p,FSI_FREECOUNT),v) -# define FSI_PUTNXTFREE(p,v) fat_putuint32(UBYTE_PTR(p,FSI_NXTFREE),v) -# define FSI_PUTTRAILSIG(p,v) fat_putuint32(UBYTE_PTR(p,FSI_TRAILSIG),v) - -# define DIR_PUTCRTIME(p,v) fat_putuint16(UBYTE_PTR(p,DIR_CRTIME),v) -# define DIR_PUTCRDATE(p,v) fat_putuint16(UBYTE_PTR(p,DIR_CRDATE),v) -# define DIR_PUTLASTACCDATE(p,v) fat_putuint16(UBYTE_PTR(p,DIR_LASTACCDATE),v) -# define DIR_PUTFSTCLUSTHI(p,v) fat_putuint16(UBYTE_PTR(p,DIR_FSTCLUSTHI),v) -# define DIR_PUTWRTTIME(p,v) fat_putuint16(UBYTE_PTR(p,DIR_WRTTIME),v) -# define DIR_PUTWRTDATE(p,v) fat_putuint16(UBYTE_PTR(p,DIR_WRTDATE),v) -# define DIR_PUTFSTCLUSTLO(p,v) fat_putuint16(UBYTE_PTR(p,DIR_FSTCLUSTLO),v) -# define DIR_PUTFILESIZE(p,v) fat_putuint32(UBYTE_PTR(p,DIR_FILESIZE),v) - -# ifdef CONFIG_FAT_LFN -# define LDIR_PUTWCHAR1(p) fat_putuint16(UBYTE_PTR(p,LDIR_WCHAR1_5),v) -# define LDIR_PUTWCHAR2(p) fat_putuint16(UBYTE_PTR(p,LDIR_WCHAR1_5+2),v) -# define LDIR_PUTWCHAR3(p) fat_putuint16(UBYTE_PTR(p,LDIR_WCHAR1_5+4),v) -# define LDIR_PUTWCHAR4(p) fat_putuint16(UBYTE_PTR(p,LDIR_WCHAR1_5+6),v) -# define LDIR_PUTWCHAR5(p) fat_putuint16(UBYTE_PTR(p,LDIR_WCHAR1_5+8),v) -# define LDIR_PUTWCHAR6(p) fat_putuint16(UBYTE_PTR(p,LDIR_WCHAR6_11),v) -# define LDIR_PUTWCHAR7(p) fat_putuint16(UBYTE_PTR(p,LDIR_WCHAR6_11+2),v) -# define LDIR_PUTWCHAR8(p) fat_putuint16(UBYTE_PTR(p,LDIR_WCHAR6_11+4),v) -# define LDIR_PUTWCHAR8(p) fat_putuint16(UBYTE_PTR(p,LDIR_WCHAR6_11+6),v) -# define LDIR_PUTWCHAR10(p) fat_putuint16(UBYTE_PTR(p,LDIR_WCHAR6_11+8),v) -# define LDIR_PUTWCHAR11(p) fat_putuint16(UBYTE_PTR(p,LDIR_WCHAR6_11+10),v) -# define LDIR_PUTWCHAR12(p) fat_putuint16(UBYTE_PTR(p,LDIR_WCHAR12_13),v) -# define LDIR_PUTWCHAR13(p) fat_putuint16(UBYTE_PTR(p,LDIR_WCHAR12_13+2),v) -# endif - -# define FSI_PUTLEADSIG(p,v) fat_putuint32(UBYTE_PTR(p,FSI_LEADSIG),v) -# define FSI_PUTSTRUCTSIG(p,v) fat_putuint32(UBYTE_PTR(p,FSI_STRUCTSIG),v) -# define FSI_PUTFREECOUNT(p,v) fat_putuint32(UBYTE_PTR(p,FSI_FREECOUNT),v) -# define FSI_PUTNXTFREE(p,v) fat_putuint32(UBYTE_PTR(p,FSI_NXTFREE),v) -# define FSI_PUTTRAILSIG(p,v) fat_putuint32(UBYTE_PTR(p,FSI_TRAILSIG),v) - -# define FAT_PUTFAT16(p,i,v) fat_putuint16(UBYTE_PTR(p,i),v) -# define FAT_PUTFAT32(p,i,v) fat_putuint32(UBYTE_PTR(p,i),v) - -#else - -/* But nothing special has to be done for the little endian-case for access - * to aligned mulitbyte values. - */ - -# define MBR_GETRESVDSECCOUNT(p) UINT16_VAL(p,BS_RESVDSECCOUNT) -# define MBR_GETFATSZ16(p) UINT16_VAL(p,BS_FATSZ16) -# define MBR_GETSECPERTRK(p) UINT16_VAL(p,BS_SECPERTRK) -# define MBR_GETNUMHEADS(p) UINT16_VAL(p,BS_NUMHEADS) -# define MBR_GETHIDSEC(p) UINT32_VAL(p,BS_HIDSEC) -# define MBR_GETTOTSEC32(p) UINT32_VAL(p,BS_TOTSEC32) -# define MBR_GETFATSZ32(p) UINT32_VAL(p,BS32_FATSZ32) -# define MBR_GETEXTFLAGS(p) UINT16_VAL(p,BS32_EXTFLAGS) -# define MBR_GETFSVER(p) UINT16_VAL(p,BS32_FSVER) -# define MBR_GETROOTCLUS(p) UINT32_VAL(p,BS32_ROOTCLUS) -# define MBR_GETFSINFO(p) UINT16_VAL(p,BS32_FSINFO) -# define MBR_GETBKBOOTSEC(p) UINT16_VAL(p,BS32_BKBOOTSEC) -# define MBR_GETSIGNATURE(p) UINT16_VAL(p,BS_SIGNATURE) - -# define FSI_GETLEADSIG(p) UINT32_VAL(p,FSI_LEADSIG) -# define FSI_GETSTRUCTSIG(p) UINT32_VAL(p,FSI_STRUCTSIG) -# define FSI_GETFREECOUNT(p) UINT32_VAL(p,FSI_FREECOUNT) -# define FSI_GETNXTFREE(p) UINT32_VAL(p,FSI_NXTFREE) -# define FSI_GETTRAILSIG(p) UINT32_VAL(p,FSI_TRAILSIG) - -# define DIR_GETCRTIME(p) UINT16_VAL(p,DIR_CRTIME) -# define DIR_GETCRDATE(p) UINT16_VAL(p,DIR_CRDATE) -# define DIR_GETLASTACCDATE(p) UINT16_VAL(p,DIR_LASTACCDATE) -# define DIR_GETFSTCLUSTHI(p) UINT16_VAL(p,DIR_FSTCLUSTHI) -# define DIR_GETWRTTIME(p) UINT16_VAL(p,DIR_WRTTIME) -# define DIR_GETWRTDATE(p) UINT16_VAL(p,DIR_WRTDATE) -# define DIR_GETFSTCLUSTLO(p) UINT16_VAL(p,DIR_FSTCLUSTLO) -# define DIR_GETFILESIZE(p) UINT32_VAL(p,DIR_FILESIZE) - -# ifdef CONFIG_FAT_LFN -# define LDIR_GETWCHAR1(p) UINT16_VAL(p,LDIR_WCHAR1_5) -# define LDIR_GETWCHAR2(p) UINT16_VAL(p,LDIR_WCHAR1_5+2) -# define LDIR_GETWCHAR3(p) UINT16_VAL(p,LDIR_WCHAR1_5+4) -# define LDIR_GETWCHAR4(p) UINT16_VAL(p,LDIR_WCHAR1_5+6) -# define LDIR_GETWCHAR5(p) UINT16_VAL(p,LDIR_WCHAR1_5+8) -# define LDIR_GETWCHAR6(p) UINT16_VAL(p,LDIR_WCHAR6_11) -# define LDIR_GETWCHAR7(p) UINT16_VAL(p,LDIR_WCHAR6_11+2) -# define LDIR_GETWCHAR8(p) UINT16_VAL(p,LDIR_WCHAR6_11+4) -# define LDIR_GETWCHAR9(p) UINT16_VAL(p,LDIR_WCHAR6_11+6) -# define LDIR_GETWCHAR10(p) UINT16_VAL(p,LDIR_WCHAR6_11+8) -# define LDIR_GETWCHAR11(p) UINT16_VAL(p,LDIR_WCHAR6_11+10) -# define LDIR_GETWCHAR12(p) UINT16_VAL(p,LDIR_WCHAR12_13) -# define LDIR_GETWCHAR13(p) UINT16_VAL(p,LDIR_WCHAR12_13+2) -# endif - -# define FSI_GETLEADSIG(p) UINT32_VAL(p,FSI_LEADSIG) -# define FSI_GETSTRUCTSIG(p) UINT32_VAL(p,FSI_STRUCTSIG) -# define FSI_GETFREECOUNT(p) UINT32_VAL(p,FSI_FREECOUNT) -# define FSI_GETNXTFREE(p) UINT32_VAL(p,FSI_NXTFREE) -# define FSI_GETTRAILSIG(p) UINT32_VAL(p,FSI_TRAILSIG) - -# define FAT_GETFAT16(p,i) UINT16_VAL(p,i) -# define FAT_GETFAT32(p,i) UINT32_VAL(p,i) - -# define MBR_PUTRESVDSECCOUNT(p,v) UINT16_PUT(p,BS_RESVDSECCOUNT,v) -# define MBR_PUTFATSZ16(p,v) UINT16_PUT(p,BS_FATSZ16,v) -# define MBR_PUTSECPERTRK(p,v) UINT16_PUT(p,BS_SECPERTRK,v) -# define MBR_PUTNUMHEADS(p,v) UINT16_PUT(p,BS_NUMHEADS,v) -# define MBR_PUTHIDSEC(p,v) UINT32_PUT(p,BS_HIDSEC,v) -# define MBR_PUTTOTSEC32(p,v) UINT32_PUT(p,BS_TOTSEC32,v) -# define MBR_PUTFATSZ32(p,v) UINT32_PUT(p,BS32_FATSZ32,v) -# define MBR_PUTEXTFLAGS(p,v) UINT16_PUT(p,BS32_EXTFLAGS,v) -# define MBR_PUTFSVER(p,v) UINT16_PUT(p,BS32_FSVER,v) -# define MBR_PUTROOTCLUS(p,v) UINT32_PUT(p,BS32_ROOTCLUS,v) -# define MBR_PUTFSINFO(p,v) UINT16_PUT(p,BS32_FSINFO,v) -# define MBR_PUTBKBOOTSEC(p,v) UINT16_PUT(p,BS32_BKBOOTSEC,v) -# define MBR_PUTSIGNATURE(p,v) UINT16_PUT(p,BS_SIGNATURE,v) - -# define FSI_PUTLEADSIG(p,v) UINT32_PUT(p,FSI_LEADSIG,v) -# define FSI_PUTSTRUCTSIG(p,v) UINT32_PUT(p,FSI_STRUCTSIG,v) -# define FSI_PUTFREECOUNT(p,v) UINT32_PUT(p,FSI_FREECOUNT,v) -# define FSI_PUTNXTFREE(p,v) UINT32_PUT(p,FSI_NXTFREE,v) -# define FSI_PUTTRAILSIG(p,v) UINT32_PUT(p,FSI_TRAILSIG,v) - -# define DIR_PUTCRTIME(p,v) UINT16_PUT(p,DIR_CRTIME,v) -# define DIR_PUTCRDATE(p,v) UINT16_PUT(p,DIR_CRDATE,v) -# define DIR_PUTLASTACCDATE(p,v) UINT16_PUT(p,DIR_LASTACCDATE,v) -# define DIR_PUTFSTCLUSTHI(p,v) UINT16_PUT(p,DIR_FSTCLUSTHI,v) -# define DIR_PUTWRTTIME(p,v) UINT16_PUT(p,DIR_WRTTIME,v) -# define DIR_PUTWRTDATE(p,v) UINT16_PUT(p,DIR_WRTDATE,v) -# define DIR_PUTFSTCLUSTLO(p,v) UINT16_PUT(p,DIR_FSTCLUSTLO,v) -# define DIR_PUTFILESIZE(p,v) UINT32_PUT(p,DIR_FILESIZE,v) - -# ifdef CONFIG_FAT_LFN -# define LDIR_PUTWCHAR1(p,v) UINT16_PUT(p,LDIR_WCHAR1_5,v) -# define LDIR_PUTWCHAR2(p,v) UINT16_PUT(p,LDIR_WCHAR1_5+2,v) -# define LDIR_PUTWCHAR3(p,v) UINT16_PUT(p,LDIR_WCHAR1_5+4,v) -# define LDIR_PUTWCHAR4(p,v) UINT16_PUT(p,LDIR_WCHAR1_5+6,v) -# define LDIR_PUTWCHAR5(p,v) UINT16_PUT(p,LDIR_WCHAR1_5+8,v) -# define LDIR_PUTWCHAR6(p,v) UINT16_PUT(p,LDIR_WCHAR6_11,v) -# define LDIR_PUTWCHAR7(p,v) UINT16_PUT(p,LDIR_WCHAR6_11+2,v) -# define LDIR_PUTWCHAR8(p,v) UINT16_PUT(p,LDIR_WCHAR6_11+4,v) -# define LDIR_PUTWCHAR9(p,v) UINT16_PUT(p,LDIR_WCHAR6_11+6,v) -# define LDIR_PUTWCHAR10(p,v) UINT16_PUT(p,LDIR_WCHAR6_11+8,v) -# define LDIR_PUTWCHAR11(p,v) UINT16_PUT(p,LDIR_WCHAR6_11+10,v) -# define LDIR_PUTWCHAR12(p,v) UINT16_PUT(p,LDIR_WCHAR12_13,v) -# define LDIR_PUTWCHAR13(p,v) UINT16_PUT(p,LDIR_WCHAR12_13+2,v) -# endif - -# define FSI_PUTLEADSIG(p,v) UINT32_PUT(p,FSI_LEADSIG,v) -# define FSI_PUTSTRUCTSIG(p,v) UINT32_PUT(p,FSI_STRUCTSIG,v) -# define FSI_PUTFREECOUNT(p,v) UINT32_PUT(p,FSI_FREECOUNT,v) -# define FSI_PUTNXTFREE(p,v) UINT32_PUT(p,FSI_NXTFREE,v) -# define FSI_PUTTRAILSIG(p,v) UINT32_PUT(p,FSI_TRAILSIG,v) - -# define FAT_PUTFAT16(p,i,v) UINT16_PUT(p,i,v) -# define FAT_PUTFAT32(p,i,v) UINT32_PUT(p,i,v) - -#endif - -/**************************************************************************** - * Name: fat_io_alloc and fat_io_free - * - * Description: - * The FAT file system allocates two I/O buffers for data transfer, each - * are the size of one device sector. One of the buffers is allocated - * once for each FAT volume that is mounted; the other buffers are - * allocated each time a FAT file is opened. - * - * Some hardware, however, may require special DMA-capable memory in - * order to perform the the transfers. If CONFIG_FAT_DMAMEMORY is defined - * then the architecture-specific hardware must provide the funtions - * fat_dma_alloc() and fat_dma_free() as prototyped below: fat_dmalloc() - * will allocate DMA-capable memory of the specified size; fat_dmafree() - * is the corresponding function that will be called to free the DMA- - * capable memory. - * - ****************************************************************************/ - -#ifdef CONFIG_FAT_DMAMEMORY -# define fat_io_alloc(s) fat_dma_alloc(s) -# define fat_io_free(m,s) fat_dma_free(m,s) -#else -# define fat_io_alloc(s) kmalloc(s) -# define fat_io_free(m,s) kfree(m) -#endif - -/**************************************************************************** - * Public Types - ****************************************************************************/ - -/* This structure represents the overall mountpoint state. An instance of this - * structure is retained as inode private data on each mountpoint that is - * mounted with a fat32 filesystem. - */ - -struct fat_file_s; -struct fat_mountpt_s -{ - struct inode *fs_blkdriver; /* The block driver inode that hosts the FAT32 fs */ - struct fat_file_s *fs_head; /* A list to all files opened on this mountpoint */ - - sem_t fs_sem; /* Used to assume thread-safe access */ - off_t fs_hwsectorsize; /* HW: Sector size reported by block driver*/ - off_t fs_hwnsectors; /* HW: The number of sectors reported by the hardware */ - off_t fs_fatbase; /* Logical block of start of filesystem (past resd sectors) */ - off_t fs_rootbase; /* MBR: Cluster no. of 1st cluster of root dir */ - off_t fs_database; /* Logical block of start data sectors */ - off_t fs_fsinfo; /* MBR: Sector number of FSINFO sector */ - off_t fs_currentsector; /* The sector number buffered in fs_buffer */ - uint32_t fs_nclusters; /* Maximum number of data clusters */ - uint32_t fs_nfatsects; /* MBR: Count of sectors occupied by one fat */ - uint32_t fs_fattotsec; /* MBR: Total count of sectors on the volume */ - uint32_t fs_fsifreecount; /* FSI: Last free cluster count on volume */ - uint32_t fs_fsinextfree; /* FSI: Cluster number of 1st free cluster */ - uint16_t fs_fatresvdseccount; /* MBR: The total number of reserved sectors */ - uint16_t fs_rootentcnt; /* MBR: Count of 32-bit root directory entries */ - bool fs_mounted; /* true: The file system is ready */ - bool fs_dirty; /* true: fs_buffer is dirty */ - bool fs_fsidirty; /* true: FSINFO sector must be written to disk */ - uint8_t fs_type; /* FSTYPE_FAT12, FSTYPE_FAT16, or FSTYPE_FAT32 */ - uint8_t fs_fatnumfats; /* MBR: Number of FATs (probably 2) */ - uint8_t fs_fatsecperclus; /* MBR: Sectors per allocation unit: 2**n, n=0..7 */ - uint8_t *fs_buffer; /* This is an allocated buffer to hold one sector - * from the device */ -}; - -/* This structure represents on open file under the mountpoint. An instance - * of this structure is retained as struct file specific information on each - * opened file. - */ - -struct fat_file_s -{ - struct fat_file_s *ff_next; /* Retained in a singly linked list */ - uint8_t ff_bflags; /* The file buffer flags */ - uint8_t ff_oflags; /* Flags provided when file was opened */ - uint8_t ff_sectorsincluster; /* Sectors remaining in cluster */ - uint16_t ff_dirindex; /* Index into ff_dirsector to directory entry */ - uint32_t ff_currentcluster; /* Current cluster being accessed */ - off_t ff_dirsector; /* Sector containing the directory entry */ - off_t ff_size; /* Size of the file in bytes */ - off_t ff_startcluster; /* Start cluster of file on media */ - off_t ff_currentsector; /* Current sector being operated on */ - off_t ff_cachesector; /* Current sector in the file buffer */ - uint8_t *ff_buffer; /* File buffer (for partial sector accesses) */ -}; - -/* This structure holds the sequency of directory entries used by one - * file element (directory or file). For short file names, this is - * single diretory entry. But for long file names, the is a sequence - * of directory entries. Long directory name entries appear in reverse - * order: Last, next-to-last, ..., first. The "first" long file name - * directory is then following by the short directory name entry. The - * short file name entry contains the real meat of the file data. - * - * So it takes the sector number and entry offset of the last long - * file name entry and of the short file name entry to define the - * sequence. In the case of short file names, the sector number and - * offset will be the same. - */ - -struct fat_dirseq_s -{ - /* Sector offsets */ - - uint16_t ds_offset; /* Sector offset to short file name entry */ -#ifdef CONFIG_FAT_LFN - uint16_t ds_lfnoffset; /* Sector offset to last long file name entry */ -#endif - - /* Sector and cluster numbers */ - - off_t ds_sector; /* Sector of the short file name entry */ -#ifdef CONFIG_FAT_LFN - off_t ds_cluster; /* Cluster containing the short file name entry */ - off_t ds_lfnsector; /* Sector of the last long name entry */ - off_t ds_lfncluster; /* Cluster containing the long file name entry */ - off_t ds_startsector; /* Starting sector of the directory */ -#endif -}; - -/* This structure is used internally for describing directory entries */ - -struct fat_dirinfo_s -{ - /* The file/directory name */ - -#ifdef CONFIG_FAT_LFN - uint8_t fd_lfname[LDIR_MAXFNAME+1]; /* Long filename with terminator */ -#endif - uint8_t fd_name[DIR_MAXFNAME]; /* Short 8.3 alias filename (no terminator) */ - - /* NT flags are not used */ - -#ifdef CONFIG_FAT_LCNAMES - uint8_t fd_ntflags; /* NTRes lower case flags */ -#endif - - /* TRUE if this is the root directory */ - - bool fd_root; - - /* The following provides the sequence of directory entries used by the - * file or directory. - */ - - struct fat_dirseq_s fd_seq; /* Directory sequence */ - - /* This is part of the opendir, readdir, ... logic */ - - struct fs_fatdir_s dir; /* Used with opendir, readdir, etc. */ -}; - -/* Generic helper macros ****************************************************/ - -#ifndef MIN -# define MIN(a,b) (a < b ? a : b) -#endif - -#ifndef MAX -# define MAX(a,b) (a > b ? a : b) -#endif - -/**************************************************************************** - * Global Variables - ****************************************************************************/ - -/**************************************************************************** - * Public Function Prototypes - ****************************************************************************/ - -#undef EXTERN -#if defined(__cplusplus) -#define EXTERN extern "C" -extern "C" { -#else -#define EXTERN extern -#endif - -/* Utitilies to handle unaligned or byte swapped accesses */ - -EXTERN uint16_t fat_getuint16(uint8_t *ptr); -EXTERN uint32_t fat_getuint32(uint8_t *ptr); -EXTERN void fat_putuint16(uint8_t *ptr, uint16_t value16); -EXTERN void fat_putuint32(uint8_t *ptr, uint32_t value32); - -/* Manage the per-mount semaphore that protects access to shared resources */ - -EXTERN void fat_semtake(struct fat_mountpt_s *fs); -EXTERN void fat_semgive(struct fat_mountpt_s *fs); - -/* Get the current time for FAT creation and write times */ - -EXTERN uint32_t fat_systime2fattime(void); -EXTERN time_t fat_fattime2systime(uint16_t fattime, uint16_t fatdate); - -/* Handle hardware interactions for mounting */ - -EXTERN int fat_mount(struct fat_mountpt_s *fs, bool writeable); -EXTERN int fat_checkmount(struct fat_mountpt_s *fs); - -/* low-level hardware access */ - -EXTERN int fat_hwread(struct fat_mountpt_s *fs, uint8_t *buffer, - off_t sector, unsigned int nsectors); -EXTERN int fat_hwwrite(struct fat_mountpt_s *fs, uint8_t *buffer, - off_t sector, unsigned int nsectors); - -/* Cluster / cluster chain access helpers */ - -EXTERN off_t fat_cluster2sector(struct fat_mountpt_s *fs, uint32_t cluster); -EXTERN off_t fat_getcluster(struct fat_mountpt_s *fs, uint32_t clusterno); -EXTERN int fat_putcluster(struct fat_mountpt_s *fs, uint32_t clusterno, - off_t startsector); -EXTERN int fat_removechain(struct fat_mountpt_s *fs, uint32_t cluster); -EXTERN int32_t fat_extendchain(struct fat_mountpt_s *fs, uint32_t cluster); - -#define fat_createchain(fs) fat_extendchain(fs, 0) - -/* Help for traversing directory trees and accessing directory entries */ - -EXTERN int fat_nextdirentry(struct fat_mountpt_s *fs, struct fs_fatdir_s *dir); -EXTERN int fat_finddirentry(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo, - const char *path); -EXTERN int fat_dirnamewrite(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo); -EXTERN int fat_dirwrite(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo, - uint8_t attributes, uint32_t fattime); -EXTERN int fat_allocatedirentry(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo); -EXTERN int fat_freedirentry(struct fat_mountpt_s *fs, struct fat_dirseq_s *seq); -EXTERN int fat_dirname2path(struct fat_mountpt_s *fs, struct fs_dirent_s *dir); - -/* File creation and removal helpers */ - -EXTERN int fat_dirtruncate(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo); -EXTERN int fat_dircreate(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo); -EXTERN int fat_remove(struct fat_mountpt_s *fs, const char *relpath, bool directory); - -/* Mountpoint and file buffer cache (for partial sector accesses) */ - -EXTERN int fat_fscacheflush(struct fat_mountpt_s *fs); -EXTERN int fat_fscacheread(struct fat_mountpt_s *fs, off_t sector); -EXTERN int fat_ffcacheflush(struct fat_mountpt_s *fs, struct fat_file_s *ff); -EXTERN int fat_ffcacheread(struct fat_mountpt_s *fs, struct fat_file_s *ff, off_t sector); -EXTERN int fat_ffcacheinvalidate(struct fat_mountpt_s *fs, struct fat_file_s *ff); - -/* FSINFO sector support */ - -EXTERN int fat_updatefsinfo(struct fat_mountpt_s *fs); -EXTERN int fat_nfreeclusters(struct fat_mountpt_s *fs, off_t *pfreeclusters); -EXTERN int fat_currentsector(struct fat_mountpt_s *fs, struct fat_file_s *ff, off_t position); - -#undef EXTERN -#if defined(__cplusplus) -} -#endif - -#endif /* __FS_FAT_FS_FAT32_H */ diff --git a/nuttx/fs/fat/fs_fat32attrib.c b/nuttx/fs/fat/fs_fat32attrib.c deleted file mode 100644 index f58ed81bb..000000000 --- a/nuttx/fs/fat/fs_fat32attrib.c +++ /dev/null @@ -1,189 +0,0 @@ -/**************************************************************************** - * fs/fat/fs_fat32attrib.c - * - * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt <gnutt@nuttx.org> - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name NuttX nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <nuttx/config.h> - -#include <stdint.h> -#include <errno.h> - -#include <nuttx/fs/fs.h> -#include <nuttx/fs/fat.h> - -#include "fs_internal.h" -#include "fs_fat32.h" - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: fat_attrib - ****************************************************************************/ - -static int fat_attrib(const char *path, fat_attrib_t *retattrib, - fat_attrib_t setbits, fat_attrib_t clearbits) -{ - struct fat_mountpt_s *fs; - struct fat_dirinfo_s dirinfo; - FAR struct inode *inode; - const char *relpath = NULL; - uint8_t *direntry; - uint8_t oldattributes; - uint8_t newattributes; - int ret; - - /* Get an inode for this file */ - - inode = inode_find(path, &relpath); - if (!inode) - { - /* There is no mountpoint that includes in this path */ - - ret = ENOENT; - goto errout; - } - - /* Verify that the inode is a valid mountpoint. */ - - if (!INODE_IS_MOUNTPT(inode) || !inode->u.i_mops || !inode->i_private) - { - ret = ENXIO; - goto errout_with_inode; - } - - /* Get the mountpoint private data from the inode structure */ - - fs = inode->i_private; - - /* Check if the mount is still healthy */ - - fat_semtake(fs); - ret = fat_checkmount(fs); - if (ret != OK) - { - goto errout_with_semaphore; - } - - /* Find the file/directory entry for the oldrelpath */ - - ret = fat_finddirentry(fs, &dirinfo, relpath); - if (ret != OK) - { - /* Some error occurred -- probably -ENOENT */ - - goto errout_with_semaphore; - } - - /* Make sure that we found some valid file or directory */ - - if (dirinfo.fd_root) - { - /* Ooops.. we found the root directory */ - - ret = EACCES; - goto errout_with_semaphore; - } - - /* Get the current attributes */ - - direntry = &fs->fs_buffer[dirinfo.fd_seq.ds_offset]; - oldattributes = DIR_GETATTRIBUTES(direntry); - newattributes = oldattributes; - - /* Set or clear any bits as requested */ - - newattributes &= ~(clearbits & (FATATTR_READONLY|FATATTR_HIDDEN|FATATTR_SYSTEM|FATATTR_ARCHIVE)); - newattributes |= (setbits & (FATATTR_READONLY|FATATTR_HIDDEN|FATATTR_SYSTEM|FATATTR_ARCHIVE)); - - /* Did any thingchange? */ - - if (newattributes != oldattributes) - { - DIR_PUTATTRIBUTES(direntry, newattributes); - fs->fs_dirty = true; - ret = fat_updatefsinfo(fs); - if (ret != OK) - { - ret = -ret; - goto errout_with_semaphore; - } - } - - /* Success */ - - if (retattrib) - { - *retattrib = newattributes; - } - - fat_semgive(fs); - inode_release(inode); - return OK; - -errout_with_semaphore: - fat_semgive(fs); -errout_with_inode: - inode_release(inode); -errout: - *get_errno_ptr() = ret; - return ERROR; -} - -/**************************************************************************** - * Global Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: fat_getattrib - ****************************************************************************/ - -int fat_getattrib(const char *path, fat_attrib_t *attrib) -{ - return fat_attrib(path, attrib, 0, 0); -} - -/**************************************************************************** - * Name: fat_setattrib - ****************************************************************************/ - -int fat_setattrib(const char *path, fat_attrib_t setbits, fat_attrib_t clearbits) -{ - return fat_attrib(path, NULL, setbits, clearbits); -} - diff --git a/nuttx/fs/fat/fs_fat32dirent.c b/nuttx/fs/fat/fs_fat32dirent.c deleted file mode 100644 index 18cf67847..000000000 --- a/nuttx/fs/fat/fs_fat32dirent.c +++ /dev/null @@ -1,2947 +0,0 @@ -/**************************************************************************** - * fs/fat/fs_fat32dirent.c - * - * Copyright (C) 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt <gnutt@nuttx.org> - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name NuttX nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -/**************************************************************************** - * NOTE: If CONFIG_FAT_LFN is defined, then there may be some legal, patent - * issues. The following was extracted from the entry "File Allocation Table - * from Wikipedia, the free encyclopedia: - * - * "On December 3, 2003 Microsoft announced it would be offering licenses - * for use of its FAT specification and 'associated intellectual property', - * at the cost of a US$0.25 royalty per unit sold, with a $250,000 maximum - * royalty per license agreement. - * - * o "U.S. Patent 5,745,902 (http://www.google.com/patents?vid=5745902) - - * Method and system for accessing a file using file names having - * different file name formats. ... - * o "U.S. Patent 5,579,517 (http://www.google.com/patents?vid=5579517) - - * Common name space for long and short filenames. ... - * o "U.S. Patent 5,758,352 (http://www.google.com/patents?vid=5758352) - - * Common name space for long and short filenames. ... - * o "U.S. Patent 6,286,013 (http://www.google.com/patents?vid=6286013) - - * Method and system for providing a common name space for long and - * short file names in an operating system. ... - * - * "Many technical commentators have concluded that these patents only cover - * FAT implementations that include support for long filenames, and that - * removable solid state media and consumer devices only using short names - * would be unaffected. ..." - * - * So you have been forewarned: Use the long filename at your own risk! - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <nuttx/config.h> - -#include <sys/types.h> - -#include <stdint.h> -#include <stdbool.h> -#include <stdlib.h> -#include <string.h> -#include <ctype.h> -#include <assert.h> -#include <errno.h> -#include <debug.h> - -#include <nuttx/fs/fs.h> -#include <nuttx/fs/fat.h> - -#include "fs_internal.h" -#include "fs_fat32.h" - -/**************************************************************************** - * Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -enum fat_case_e -{ - FATCASE_UNKNOWN = 0, - FATCASE_UPPER, - FATCASE_LOWER -}; - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -#ifdef CONFIG_FAT_LFN -static uint8_t fat_lfnchecksum(const uint8_t *sfname); -#endif -static inline int fat_parsesfname(const char **path, - struct fat_dirinfo_s *dirinfo, - char *terminator); -#ifdef CONFIG_FAT_LFN -static inline int fat_parselfname(const char **path, - struct fat_dirinfo_s *dirinfo, - char *terminator); -static inline int fat_createalias(struct fat_dirinfo_s *dirinfo); -static inline int fat_findalias(struct fat_mountpt_s *fs, - struct fat_dirinfo_s *dirinfo); -static inline int fat_uniquealias(struct fat_mountpt_s *fs, - struct fat_dirinfo_s *dirinfo); -#endif -static int fat_path2dirname(const char **path, struct fat_dirinfo_s *dirinfo, - char *terminator); -static int fat_findsfnentry(struct fat_mountpt_s *fs, - struct fat_dirinfo_s *dirinfo); -#ifdef CONFIG_FAT_LFN -static bool fat_cmplfnchunk(uint8_t *chunk, const uint8_t *substr, int nchunk); -static bool fat_cmplfname(const uint8_t *direntry, const uint8_t *substr); -static inline int fat_findlfnentry(struct fat_mountpt_s *fs, - struct fat_dirinfo_s *dirinfo); - -#endif -static inline int fat_allocatesfnentry(struct fat_mountpt_s *fs, - struct fat_dirinfo_s *dirinfo); -#ifdef CONFIG_FAT_LFN -static inline int fat_allocatelfnentry(struct fat_mountpt_s *fs, - struct fat_dirinfo_s *dirinfo); -#endif -static inline int fat_getsfname(uint8_t *direntry, char *buffer, - unsigned int buflen); -#ifdef CONFIG_FAT_LFN -static void fat_getlfnchunk(uint8_t *chunk, uint8_t *dest, int nchunk); -static inline int fat_getlfname(struct fat_mountpt_s *fs, struct fs_dirent_s *dir); -#endif -static int fat_putsfname(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo); -#ifdef CONFIG_FAT_LFN -static void fat_initlfname(uint8_t *chunk, int nchunk); -static void fat_putlfnchunk(uint8_t *chunk, const uint8_t *src, int nchunk); -static int fat_putlfname(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo); -#endif -static int fat_putsfdirentry(struct fat_mountpt_s *fs, - struct fat_dirinfo_s *dirinfo, - uint8_t attributes, uint32_t fattime); - -/**************************************************************************** - * Private Variables - ****************************************************************************/ - -/**************************************************************************** - * Public Variables - ****************************************************************************/ - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: fat_lfnchecksum - * - * Desciption: Caculate the checksum of . - * - ****************************************************************************/ - -#ifdef CONFIG_FAT_LFN -static uint8_t fat_lfnchecksum(const uint8_t *sfname) -{ - uint8_t sum = 0; - int i; - - for (i = DIR_MAXFNAME; i; i--) - { - sum = ((sum & 1) << 7) + (sum >> 1) + *sfname++; - } - - return sum; -} -#endif - -/**************************************************************************** - * Name: fat_parsesfname - * - * Desciption: Convert a user filename into a properly formatted FAT - * (short 8.3) filename as it would appear in a directory entry. Here are - * the rules for the 8+3 short file name in the directory: - * - * The first byte: - * - * 0xe5 = The directory is free - * 0x00 = This directory and all following directories are free - * 0x05 = Really 0xe5 - * 0x20 = May NOT be ' ' - * - * Other characters may be any characters except for the following: - * - * 0x00-0x1f = (except for 0x00 and 0x05 in the first byte) - * 0x22 = '"' - * 0x2a-0x2c = '*', '+', ',' - * 0x2e-0x2f = '.', '/' - * 0x3a-0x3f = ':', ';', '<', '=', '>', '?' - * 0x5b-0x5d = '[', '\\', ;]' - * 0x7c = '|' - * - * '.' May only occur once within string and only within the first 9 - * bytes. The '.' is not save in the directory, but is implicit in - * 8+3 format. - * - * Lower case characters are not allowed in directory names (without some - * poorly documented operations on the NTRes directory byte). Lower case - * codes may represent different characters in other character sets ("DOS - * code pages". The logic below does not, at present, support any other - * character sets. - * - * Returned value: - * OK - The path refers to a valid 8.3 FAT file name and has been properly - * converted and stored in dirinfo. - * <0 - Otherwise an negated error is returned meaning that the string is - * not a valid 8+3 because: - * - * 1. Contains characters not in the printable character set, - * 2. Contains forbidden characters or multiple '.' characters - * 3. File name or extension is too long. - * - * If CONFIG_FAT_LFN is defined and CONFIG_FAT_LCNAMES is NOT - * defined, then: - * - * 4a. File name or extension contains lower case characters. - * - * If CONFIG_FAT_LFN is defined and CONFIG_FAT_LCNAMES is defined, - * then: - * - * 4b. File name or extension is not all the same case. - * - ****************************************************************************/ - -static inline int fat_parsesfname(const char **path, - struct fat_dirinfo_s *dirinfo, - char *terminator) -{ -#ifdef CONFIG_FAT_LCNAMES - unsigned int ntlcenable = FATNTRES_LCNAME | FATNTRES_LCEXT; - unsigned int ntlcfound = 0; -#ifdef CONFIG_FAT_LFN - enum fat_case_e namecase = FATCASE_UNKNOWN; - enum fat_case_e extcase = FATCASE_UNKNOWN; -#endif -#endif - const char *node = *path; - int endndx; - uint8_t ch; - int ndx = 0; - - /* Initialized the name with all spaces */ - - memset(dirinfo->fd_name, ' ', DIR_MAXFNAME); - - /* Loop until the name is successfully parsed or an error occurs */ - - endndx = 8; - for (;;) - { - /* Get the next byte from the path */ - - ch = *node++; - - /* Check if this the last byte in this node of the name */ - - if ((ch == '\0' || ch == '/') && ndx != 0 ) - { - /* Return the accumulated NT flags and the terminating character */ - -#ifdef CONFIG_FAT_LCNAMES - dirinfo->fd_ntflags = ntlcfound & ntlcenable; -#endif - *terminator = ch; - *path = node; - return OK; - } - - /* Accept only the printable character set (excluding space). Note - * that the first byte of the name could be 0x05 meaning that is it - * 0xe5, but this is not a printable character in this character in - * either case. - */ - - else if (!isgraph(ch)) - { - goto errout; - } - - /* Check for transition from name to extension. Only one '.' is - * permitted and it must be within first 9 characters - */ - - else if (ch == '.' && endndx == 8) - { - /* Starting the extension */ - - ndx = 8; - endndx = 11; - continue; - } - - /* Reject printable characters forbidden by FAT */ - - else if (ch == '"' || (ch >= '*' && ch <= ',') || - ch == '.' || ch == '/' || - (ch >= ':' && ch <= '?') || - (ch >= '[' && ch <= ']') || - (ch == '|')) - { - goto errout; - } - - /* Check for upper case characters */ - -#ifdef CONFIG_FAT_LCNAMES - else if (isupper(ch)) - { - /* Some or all of the characters in the name or extension - * are upper case. Force all of the characters to be interpreted - * as upper case. - */ - - if (endndx == 8) - { - /* Is there mixed case in the name? */ - -#ifdef CONFIG_FAT_LFN - if (namecase == FATCASE_LOWER) - { - /* Mixed case in the name -- use the long file name */ - - goto errout; - } - - /* So far, only upper case in the name*/ - - namecase = FATCASE_UPPER; -#endif - - /* Clear lower case name bit in mask*/ - - ntlcenable &= ~FATNTRES_LCNAME; - } - else - { - /* Is there mixed case in the extension? */ - -#ifdef CONFIG_FAT_LFN - if (extcase == FATCASE_LOWER) - { - /* Mixed case in the extension -- use the long file name */ - - goto errout; - } - - /* So far, only upper case in the extension*/ - - extcase = FATCASE_UPPER; -#endif - - /* Clear lower case extension in mask */ - - ntlcenable &= ~FATNTRES_LCEXT; - } - } -#endif - - /* Check for lower case characters */ - - else if (islower(ch)) - { -#if defined(CONFIG_FAT_LFN) && !defined(CONFIG_FAT_LCNAMES) - /* If lower case characters are present, then a long file - * name will be constructed. - */ - - goto errout; -#else - /* Convert the character to upper case */ - - ch = toupper(ch); - - /* Some or all of the characters in the name or extension - * are lower case. They can be interpreted as lower case if - * only if all of the characters in the name or extension are - * lower case. - */ - -#ifdef CONFIG_FAT_LCNAMES - if (endndx == 8) - { - /* Is there mixed case in the name? */ - -#ifdef CONFIG_FAT_LFN - if (namecase == FATCASE_UPPER) - { - /* Mixed case in the name -- use the long file name */ - - goto errout; - } - - /* So far, only lower case in the name*/ - - namecase = FATCASE_LOWER; -#endif - - /* Set lower case name bit */ - - ntlcfound |= FATNTRES_LCNAME; - } - else - { - /* Is there mixed case in the extension? */ - -#ifdef CONFIG_FAT_LFN - if (extcase == FATCASE_UPPER) - { - /* Mixed case in the extension -- use the long file name */ - - goto errout; - } - - /* So far, only lower case in the extension*/ - - extcase = FATCASE_LOWER; -#endif - - /* Set lower case extension bit */ - - ntlcfound |= FATNTRES_LCEXT; - } -#endif -#endif /* CONFIG_FAT_LFN && !CONFIG_FAT_LCNAMES */ - } - - /* Check if the file name exceeds the size permitted (without - * long file name support). - */ - - if (ndx >= endndx) - { - goto errout; - } - - /* Save next character in the accumulated name */ - - dirinfo->fd_name[ndx++] = ch; - } - - errout: - return -EINVAL; -} - -/**************************************************************************** - * Name: fat_parselfname - * - * Desciption: Convert a user filename into a properly formatted FAT - * long filename as it would appear in a directory entry. Here are - * the rules for the long file name in the directory: - * - * Valid characters are the same as for short file names EXCEPT: - * - * 1. '+', ',', ';', '=', '[', and ']' are accepted in the file name - * 2. '.' (dot) can occur more than once in a filename. Extension is - * the substring after the last dot. - * - * Returned value: - * OK - The path refers to a valid long file name and has been properly - * stored in dirinfo. - * <0 - Otherwise an negated error is returned meaning that the string is - * not a valid long file name: - * - * 1. Contains characters not in the printable character set, - * 2. Contains forbidden characters - * 3. File name is too long. - * - ****************************************************************************/ - -#ifdef CONFIG_FAT_LFN -static inline int fat_parselfname(const char **path, - struct fat_dirinfo_s *dirinfo, - char *terminator) -{ - const char *node = *path; - uint8_t ch; - int ndx = 0; - - /* Loop until the name is successfully parsed or an error occurs */ - - for (;;) - { - /* Get the next byte from the path */ - - ch = *node++; - - /* Check if this the last byte in this node of the name */ - - if ((ch == '\0' || ch == '/') && ndx != 0 ) - { - /* Null terminate the string */ - - dirinfo->fd_lfname[ndx] = '\0'; - - /* Return the remaining sub-string and the terminating character. */ - - *terminator = ch; - *path = node; - return OK; - } - - /* Accept only the printable character set (including space) */ - - else if (!isprint(ch)) - { - goto errout; - } - - /* Reject printable characters forbidden by FAT */ - - else if (ch == '"' || ch == '*' || ch == '/' || ch == ':' || - ch == '<' || ch == '>' || ch == '?' || ch == '\\' || - ch == '|') - { - goto errout; - } - - /* Check if the file name exceeds the size permitted. */ - - if (ndx >= LDIR_MAXFNAME) - { - goto errout; - } - - /* Save next character in the accumulated name */ - - dirinfo->fd_lfname[ndx++] = ch; - } - - errout: - dirinfo->fd_lfname[0] = '\0'; - return -EINVAL; -} -#endif - -/**************************************************************************** - * Name: fat_createalias - * - * Desciption: Given a valid long file name, create a short filename alias. - * Here are the rules for creation of the alias: - * - * 1. All uppercase - * 2. All dots except the last deleted - * 3. First 6 (uppercase) characters used as a base - * 4. Then ~1. The number is increased if the file already exists in the - * directory. If the number exeeds >10, then character stripped off the - * base. - * 5. The extension is the first 3 uppercase chars of extension. - * - * This function is called only from fat_putlfname() - * - * Returned value: - * OK - The alias was created correctly. - * <0 - Otherwise an negated error is returned. - * - ****************************************************************************/ - -#ifdef CONFIG_FAT_LFN -static inline int fat_createalias(struct fat_dirinfo_s *dirinfo) -{ - uint8_t ch; /* Current character being processed */ - char *ext; /* Pointer to the extension substring */ - char *src; /* Pointer to the long file name source */ - int len; /* Total length of the long file name */ - int namechars; /* Number of characters available in long name */ - int extchars; /* Number of characters available in long name extension */ - int endndx; /* Maximum index into the short name array */ - int ndx; /* Index to store next character */ - - /* First, let's decide what is name and what is extension */ - - len = strlen((char*)dirinfo->fd_lfname); - ext = strrchr((char*)dirinfo->fd_lfname, '.'); - if (ext) - { - ptrdiff_t tmp; - - /* ext points to the final '.'. The difference in bytes from the - * beginning of the string is then the name length. - */ - - tmp = ext - (char*)dirinfo->fd_lfname; - namechars = tmp; - - /* And the rest, exluding the '.' is the extension. */ - - extchars = len - namechars - 1; - ext++; - } - else - { - /* No '.' found. It is all name and no extension. */ - - namechars = len; - extchars = 0; - } - - /* Alias are always all upper case */ - -#ifdef CONFIG_FAT_LCNAMES - dirinfo->fd_ntflags = 0; -#endif - - /* Initialized the short name with all spaces */ - - memset(dirinfo->fd_name, ' ', DIR_MAXFNAME); - - /* Handle a special case where there is no name. Windows seems to use - * the extension plus random stuff then ~1 to pat to 8 bytes. Some - * examples: - * - * a.b -> a.b No long name - * a., -> A26BE~1._ Padded name to make unique, _ replaces , - * .b -> B1DD2~1 Extension used as name - * .bbbbbbb -> BBBBBB~1 Extension used as name - * a.bbbbbbb -> AAD39~1.BBB Padded name to make unique. - * aaa.bbbbbbb -> AAA~1.BBBB Not padded, already unique? - * ,.bbbbbbb -> _82AF~1.BBB _ replaces , - * +[],.bbbbbbb -> ____~1.BBB _ replaces +[], - */ - - if (namechars < 1) - { - /* Use the extension as the name */ - - DEBUGASSERT(ext && extchars > 0); - src = ext; - ext = NULL; - namechars = extchars; - extchars = 0; - } - else - { - src = (char*)dirinfo->fd_lfname; - } - - /* Then copy the name and extension, handling upper case conversions and - * excluding forbidden characters. - */ - - ndx = 0; /* Position to write the next name character */ - endndx = 6; /* Maximum index before we write ~! and switch to the extension */ - - for (;;) - { - /* Get the next byte from the path. Break out of the loop if we - * encounter the end of null-terminated the long file name string. - */ - - ch = *src++; - if (ch == '\0') - { - /* This is the end of the source string. Do we need to add ~1. We - * will do that if we were parsing the name part when the endo of - * string was encountered. - */ - - if (endndx == 6) - { - /* Write the ~1 at the end of the name */ - - dirinfo->fd_name[ndx++] = '~'; - dirinfo->fd_name[ndx] = '1'; - } - - /* In any event, we are done */ - - return OK; - } - - /* Exclude those few characters included in long file names, but - * excluded in short file name: '+', ',', ';', '=', '[', ']', and '.' - */ - - if (ch == '+' || ch == ',' || ch == '.' || ch == ';' || - ch == '=' || ch == '[' || ch == ']' || ch == '|') - { - /* Use the underbar character instead */ - - ch = '_'; - } - - /* Handle lower case characters */ - - ch = toupper(ch); - - /* We now have a valid character to add to the name or extension. */ - - dirinfo->fd_name[ndx++] = ch; - - /* Did we just add a character to the name? */ - - if (endndx == 6) - { - /* Decrement the number of characters available in the name - * portion of the long name. - */ - - namechars--; - - /* Is it time to add ~1 to the string? We will do that if - * either (1) we have already added the maximum number of - * characters to the short name, or (2) if there are no further - * characters available in the name portion of the long name. - */ - - if (namechars < 1 || ndx == 6) - { - /* Write the ~1 at the end of the name */ - - dirinfo->fd_name[ndx++] = '~'; - dirinfo->fd_name[ndx] = '1'; - - /* Then switch to the extension (if there is one) */ - - if (!ext || extchars < 1) - { - return OK; - } - - ndx = 8; - endndx = 11; - src = ext; - } - } - - /* No.. we just added a character to the extension */ - - else - { - /* Decrement the number of characters available in the name - * portion of the long name - */ - - extchars--; - - /* Is the extension complete? */ - - if (extchars < 1 || ndx == 11) - { - return OK; - } - } - } -} -#endif - -/**************************************************************************** - * Name: fat_findalias - * - * Desciption: Make sure that the short alias for the long file name is - * unique, ie., that there is no other - * - * NOTE: This function does not restore the directory entry that was in the - * sector cache - * - * Returned value: - * OK - The alias is unique. - * <0 - Otherwise an negated error is returned. - * - ****************************************************************************/ - -#ifdef CONFIG_FAT_LFN -static inline int fat_findalias(struct fat_mountpt_s *fs, - struct fat_dirinfo_s *dirinfo) -{ - struct fat_dirinfo_s tmpinfo; - - /* Save the current directory info. */ - - memcpy(&tmpinfo, dirinfo, sizeof(struct fat_dirinfo_s)); - - /* Then re-initialize to the beginning of the current directory, starting - * with the first entry. - */ - - tmpinfo.dir.fd_startcluster = tmpinfo.dir.fd_currcluster; - tmpinfo.dir.fd_currsector = tmpinfo.fd_seq.ds_startsector; - tmpinfo.dir.fd_index = 0; - - /* Search for the single short file name directory entry in this directory */ - - return fat_findsfnentry(fs, &tmpinfo); -} -#endif - -/**************************************************************************** - * Name: fat_uniquealias - * - * Desciption: Make sure that the short alias for the long file name is - * unique, modifying the alias as necessary to assure uniqueness. - * - * NOTE: This function does not restore the directory entry that was in the - * sector cache - * - * information upon return. - * Returned value: - * OK - The alias is unique. - * <0 - Otherwise an negated error is returned. - * - ****************************************************************************/ - -#ifdef CONFIG_FAT_LFN -static inline int fat_uniquealias(struct fat_mountpt_s *fs, - struct fat_dirinfo_s *dirinfo) -{ - int tilde; - int lsdigit; - int ret; - int i; - - /* Find the position of the tilde character in the short name. The tilde - * can not occur in positions 0 or 7: - */ - - for (tilde = 1; tilde < 7 && dirinfo->fd_name[tilde] != '~'; tilde++); - if (tilde >= 7) - { - return -EINVAL; - } - - /* The least significant number follows the digit (and must be '1') */ - - lsdigit = tilde + 1; - DEBUGASSERT(dirinfo->fd_name[lsdigit] == '1'); - - /* Search for the single short file name directory entry in this directory */ - - while ((ret = fat_findalias(fs, dirinfo)) == OK) - { - /* Adjust the numeric value after the '~' to make the file name unique */ - - for (i = lsdigit; i > 0; i--) - { - /* If we have backed up to the tilde position, then we have to move - * the tilde back one position. - */ - - if (i == tilde) - { - /* Is there space to back up the tilde? */ - - if (tilde <= 1) - { - /* No.. then we cannot add the name to the directory. - * What is the likelihood of that happening? - */ - - return -ENOSPC; - } - - /* Back up the tilde and break out of the inner loop */ - - tilde--; - dirinfo->fd_name[tilde] = '~'; - dirinfo->fd_name[tilde+1] = '1'; - break; - } - - /* We are not yet at the tilde,. Check if this digit has already - * reached its maximum value. - */ - - else if (dirinfo->fd_name[i] < '9') - { - /* No, it has not.. just increment the LS digit and break out of - * the inner loop. - */ - - dirinfo->fd_name[i]++; - break; - } - - /* Yes.. Reset the digit to '0' and loop to adjust the digit before - * this one. - */ - - else - { - dirinfo->fd_name[i] = '0'; - } - } - } - - /* The while loop terminated because of an error; fat_findalias() - * returned something other than OK. The only acceptable error is - * -ENOENT, meaning that the short file name directory does not - * exist in this directory. - */ - - if (ret == -ENOENT) - { - ret = OK; - } - - return ret; -} -#endif - -/**************************************************************************** - * Name: fat_path2dirname - * - * Desciption: Convert a user filename into a properly formatted FAT - * (short 8.3) filename as it would appear in a directory entry. - * - ****************************************************************************/ - -static int fat_path2dirname(const char **path, struct fat_dirinfo_s *dirinfo, - char *terminator) -{ -#ifdef CONFIG_FAT_LFN - int ret; - - /* Assume no long file name */ - - dirinfo->fd_lfname[0] = '\0'; - - /* Then parse the (assumed) 8+3 short file name */ - - ret = fat_parsesfname(path, dirinfo, terminator); - if (ret < 0) - { - /* No, the name is not a valid short 8+3 file name. Try parsing - * the long file name. - */ - - ret = fat_parselfname(path, dirinfo, terminator); - } - - return ret; -#else - /* Only short, 8+3 filenames supported */ - - return fat_parsesfname(path, dirinfo, terminator); -#endif -} - -/**************************************************************************** - * Name: fat_findsfnentry - * - * Desciption: Find a short file name directory entry. Returns OK if the - * directory exists; -ENOENT if it does not. - * - ****************************************************************************/ - -static int fat_findsfnentry(struct fat_mountpt_s *fs, - struct fat_dirinfo_s *dirinfo) -{ - uint16_t diroffset; - uint8_t *direntry; -#ifdef CONFIG_FAT_LFN - off_t startsector; -#endif - int ret; - - /* Save the starting sector of the directory. This is not really needed - * for short name entries, but this keeps things consistent with long - * file name entries.. - */ - -#ifdef CONFIG_FAT_LFN - startsector = dirinfo->dir.fd_currsector; -#endif - - /* Search, beginning with the current sector, for a directory entry with - * the matching short name - */ - - for (;;) - { - /* Read the next sector into memory */ - - ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector); - if (ret < 0) - { - return ret; - } - - /* Get a pointer to the directory entry */ - - diroffset = DIRSEC_BYTENDX(fs, dirinfo->dir.fd_index); - direntry = &fs->fs_buffer[diroffset]; - - /* Check if we are at the end of the directory */ - - if (direntry[DIR_NAME] == DIR0_ALLEMPTY) - { - return -ENOENT; - } - - /* Check if we have found the directory entry that we are looking for */ - - if (direntry[DIR_NAME] != DIR0_EMPTY && - !(DIR_GETATTRIBUTES(direntry) & FATATTR_VOLUMEID) && - !memcmp(&direntry[DIR_NAME], dirinfo->fd_name, DIR_MAXFNAME) ) - { - /* Yes.. Return success */ - - dirinfo->fd_seq.ds_sector = fs->fs_currentsector; - dirinfo->fd_seq.ds_offset = diroffset; -#ifdef CONFIG_FAT_LFN - dirinfo->fd_seq.ds_cluster = dirinfo->dir.fd_currcluster; - dirinfo->fd_seq.ds_startsector = startsector; - - /* Position the last long file name directory entry at the same - * position. - */ - - dirinfo->fd_seq.ds_lfnsector = dirinfo->fd_seq.ds_sector; - dirinfo->fd_seq.ds_lfnoffset = dirinfo->fd_seq.ds_offset; - dirinfo->fd_seq.ds_lfncluster = dirinfo->fd_seq.ds_cluster; -#endif - return OK; - } - - /* No... get the next directory index and try again */ - - if (fat_nextdirentry(fs, &dirinfo->dir) != OK) - { - return -ENOENT; - } - } -} - -/**************************************************************************** - * Name: fat_cmplfnchunk - * - * Desciption: There are 13 characters per LFN entry, broken up into three - * chunks for characts 1-5, 6-11, and 12-13. This function will perform - * the comparison of a single chunk. - * - ****************************************************************************/ - -#ifdef CONFIG_FAT_LFN -static bool fat_cmplfnchunk(uint8_t *chunk, const uint8_t *substr, int nchunk) -{ - wchar_t wch; - uint8_t ch; - int i; - - /* Check bytes 1-nchunk */ - - for (i = 0; i < nchunk; i++) - { - /* Get the next character from the name string (which might be the NUL - * terminating character). - */ - - if (*substr == '\0') - { - ch = '\0'; - } - else - { - ch = *substr++; - } - - /* Get the next unicode character from the chunk. We only handle - * ASCII. For ASCII, the upper byte should be zero and the lower - * should match the ASCII code. - */ - - wch = (wchar_t)fat_getuint16((uint8_t*)chunk); - if ((wch & 0xff) != (wchar_t)ch) - { - return false; - } - - /* The characters match. If we just matched the NUL terminating - * character, then the strings match and we are finished. - */ - - if (ch == '\0') - { - return true; - } - - /* Try the next character from the directory entry. */ - - chunk += sizeof(wchar_t); - } - - /* All of the characters in the chunk match.. Return success */ - - return true; -} -#endif - -/**************************************************************************** - * Name: fat_cmplfname - * - * Desciption: Given an LFN directory entry, compare a substring of the name - * to a portion in the directory entry. - * - ****************************************************************************/ - -#ifdef CONFIG_FAT_LFN -static bool fat_cmplfname(const uint8_t *direntry, const uint8_t *substr) -{ - uint8_t *chunk; - int len; - bool match; - - /* How much of string do we have to compare? (including the NUL - * terminator). - */ - - len = strlen((char*)substr) + 1; - - /* Check bytes 1-5 */ - - chunk = LDIR_PTRWCHAR1_5(direntry); - match = fat_cmplfnchunk(chunk, substr, 5); - if (match && len > 5) - { - /* Check bytes 6-11 */ - - chunk = LDIR_PTRWCHAR6_11(direntry); - match = fat_cmplfnchunk(chunk, &substr[5], 6); - if (match && len > 11) - { - /* Check bytes 12-13 */ - - chunk = LDIR_PTRWCHAR12_13(direntry); - match = fat_cmplfnchunk(chunk, &substr[11], 2); - } - } - - return match; -} -#endif - -/**************************************************************************** - * Name: fat_findlfnentry - * - * Desciption: Find a sequence of long file name directory entries. - * - * NOTE: As a side effect, this function returns with the sector containing - * the short file name directory entry in the cache. - * - ****************************************************************************/ - -#ifdef CONFIG_FAT_LFN -static inline int fat_findlfnentry(struct fat_mountpt_s *fs, - struct fat_dirinfo_s *dirinfo) -{ - uint16_t diroffset; - uint8_t *direntry; - uint8_t lastseq; - uint8_t seqno; - uint8_t nfullentries; - uint8_t nentries; - uint8_t remainder; - uint8_t checksum = 0; - off_t startsector; - int offset; - int namelen; - int ret; - - /* Get the length of the long file name (size of the fd_lfname array is - * LDIR_MAXFNAME+1 we do not have to check the length of the string). - */ - - namelen = strlen((char*)dirinfo->fd_lfname); - DEBUGASSERT(namelen <= LDIR_MAXFNAME+1); - - /* How many LFN directory entries are we expecting? */ - - nfullentries = namelen / LDIR_MAXLFNCHARS; - remainder = namelen - nfullentries * LDIR_MAXLFNCHARS; - nentries = nfullentries; - if (remainder > 0) - { - nentries++; - } - DEBUGASSERT(nentries > 0 && nentries <= LDIR_MAXLFNS); - - /* This is the first sequency number we are looking for, the sequence - * number of the last LFN entry (remember that they appear in reverse - * order.. from last to first). - */ - - lastseq = LDIR0_LAST | nentries; - seqno = lastseq; - - /* Save the starting sector of the directory. This is needed later to - * re-scan the directory, looking duplicate short alias names. - */ - - startsector = dirinfo->dir.fd_currsector; - - /* Search, beginning with the current sector, for a directory entry this - * the match shore name - */ - - for (;;) - { - /* Read the next sector into memory */ - - ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector); - if (ret < 0) - { - return ret; - } - - /* Get a pointer to the directory entry */ - - diroffset = DIRSEC_BYTENDX(fs, dirinfo->dir.fd_index); - direntry = &fs->fs_buffer[diroffset]; - - /* Check if we are at the end of the directory */ - - if (direntry[DIR_NAME] == DIR0_ALLEMPTY) - { - return -ENOENT; - } - - /* Is this an LFN entry? Does it have the sequence number we are - * looking for? - */ - - if (LDIR_GETATTRIBUTES(direntry) != LDDIR_LFNATTR || - LDIR_GETSEQ(direntry) != seqno) - { - /* No, restart the search at the next entry */ - - seqno = lastseq; - goto next_entry; - } - - /* Yes.. If this is not the "last" LFN entry, then the checksum must - * also be the same. - */ - - if (seqno == lastseq) - { - /* Just save the checksum for subsequent checks */ - - checksum = LDIR_GETCHECKSUM(direntry); - } - - /* Not the first entry in the sequence. Does the checksum match the - * previous sequences? - */ - - else if (checksum != LDIR_GETCHECKSUM(direntry)) - { - /* No, restart the search at the next entry */ - - seqno = lastseq; - goto next_entry; - } - - /* Check if the name substring in this LFN matches the corresponding - * substring of the name we are looking for. - */ - - offset = ((seqno & LDIR0_SEQ_MASK) - 1) * LDIR_MAXLFNCHARS; - if (fat_cmplfname(direntry, &dirinfo->fd_lfname[offset])) - { - /* Yes.. it matches. Check the sequence number. Is this the - * "last" LFN entry (i.e., the one that appears first)? - */ - - if (seqno == lastseq) - { - /* Yes.. Save information about this LFN entry position */ - - dirinfo->fd_seq.ds_lfnsector = fs->fs_currentsector; - dirinfo->fd_seq.ds_lfnoffset = diroffset; - dirinfo->fd_seq.ds_lfncluster = dirinfo->dir.fd_currcluster; - dirinfo->fd_seq.ds_startsector = startsector; - seqno &= LDIR0_SEQ_MASK; - } - - /* Is this the first sequence number (i.e., the LFN entry that - * will appear last)? - */ - - if (seqno == 1) - { - /* We have found all of the LFN entries. The next directory - * entry should be the one containing the short file name - * alias and all of the meat about the file or directory. - */ - - if (fat_nextdirentry(fs, &dirinfo->dir) != OK) - { - return -ENOENT; - } - - /* Make sure that the directory entry is in the sector cache */ - - ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector); - if (ret < 0) - { - return ret; - } - - /* Get a pointer to the directory entry */ - - diroffset = DIRSEC_BYTENDX(fs, dirinfo->dir.fd_index); - direntry = &fs->fs_buffer[diroffset]; - - /* Verify the checksum */ - - if (fat_lfnchecksum(&direntry[DIR_NAME]) == checksum) - { - /* Success! Save the position of the directory entry and - * return success. - */ - - dirinfo->fd_seq.ds_sector = fs->fs_currentsector; - dirinfo->fd_seq.ds_offset = diroffset; - dirinfo->fd_seq.ds_cluster = dirinfo->dir.fd_currcluster; - return OK; - } - - /* Bad news.. reset and continue with this entry (which is - * probably not an LFN entry unless the file systen is - * seriously corrupted. - */ - - seqno = lastseq; - continue; - } - - /* No.. there are more LFN entries to go. Decrement the sequence - * number and check the next directory entry. - */ - - seqno--; - } - else - { - /* No.. the names do not match. Restart the search at the next - * entry. - */ - - seqno = lastseq; - } - - /* Continue at the next directory entry */ - -next_entry: - if (fat_nextdirentry(fs, &dirinfo->dir) != OK) - { - return -ENOENT; - } - } -} -#endif - -/**************************************************************************** - * Name: fat_allocatesfnentry - * - * Desciption: Find a free directory entry for a short file name entry. - * - ****************************************************************************/ - -static inline int fat_allocatesfnentry(struct fat_mountpt_s *fs, - struct fat_dirinfo_s *dirinfo) -{ - uint16_t diroffset; - uint8_t *direntry; -#ifdef CONFIG_FAT_LFN - off_t startsector; -#endif - uint8_t ch; - int ret; - - /* Save the sector number of the first sector of the directory. We don't - * really need this for short file name entries; this is just done for - * consistency with the long file name logic. - */ - -#ifdef CONFIG_FAT_LFN - startsector = dirinfo->dir.fd_currsector; -#endif - - /* Then search for a free short file name directory entry */ - - for (;;) - { - /* Read the directory sector into fs_buffer */ - - ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector); - if (ret < 0) - { - /* Make sure that the return value is NOT -ENOSPC */ - - return -EIO; - } - - /* Get a pointer to the entry at fd_index */ - - diroffset = (dirinfo->dir.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE; - direntry = &fs->fs_buffer[diroffset]; - - /* Check if this directory entry is empty */ - - ch = direntry[DIR_NAME]; - if (ch == DIR0_ALLEMPTY || ch == DIR0_EMPTY) - { - /* It is empty -- we have found a directory entry */ - - dirinfo->fd_seq.ds_sector = fs->fs_currentsector; - dirinfo->fd_seq.ds_offset = diroffset; -#ifdef CONFIG_FAT_LFN - dirinfo->fd_seq.ds_cluster = dirinfo->dir.fd_currcluster; - dirinfo->fd_seq.ds_startsector = startsector; - - /* Set the "last" long file name offset to the same entry */ - - dirinfo->fd_seq.ds_lfnsector = dirinfo->fd_seq.ds_sector; - dirinfo->fd_seq.ds_lfnoffset = dirinfo->fd_seq.ds_offset; - dirinfo->fd_seq.ds_lfncluster = dirinfo->fd_seq.ds_cluster; -#endif - return OK; - } - - /* It is not empty try the next one */ - - ret = fat_nextdirentry(fs, &dirinfo->dir); - if (ret < 0) - { - /* This will return -ENOSPC if we have examined all of the - * directory entries without finding a free entry. - */ - - return ret; - } - } -} - -/**************************************************************************** - * Name: fat_allocatelfnentry - * - * Desciption: Find a sequence of free directory entries for a several long - * and one short file name entry. - * - * On entry, dirinfo.dir refers to the first interesting entry the directory. - * - ****************************************************************************/ - -#ifdef CONFIG_FAT_LFN -static inline int fat_allocatelfnentry(struct fat_mountpt_s *fs, - struct fat_dirinfo_s *dirinfo) -{ - uint16_t diroffset; - uint8_t *direntry; - off_t startsector; - uint8_t nentries; - uint8_t remainder; - uint8_t needed; - uint8_t ch; - int namelen; - int ret; - - /* Get the length of the long file name (size of the fd_lfname array is - * LDIR_MAXFNAME+1 we do not have to check the length of the string). - */ - - namelen = strlen((char *)dirinfo->fd_lfname); - DEBUGASSERT(namelen <= LDIR_MAXFNAME+1); - - /* How many LFN directory entries are we expecting? */ - - nentries = namelen / LDIR_MAXLFNCHARS; - remainder = namelen - nentries * LDIR_MAXLFNCHARS; - if (remainder > 0) - { - nentries++; - } - DEBUGASSERT(nentries > 0 && nentries <= LDIR_MAXLFNS); - - /* Plus another for short file name entry that follows the sequence of LFN - * entries. - */ - - nentries++; - - /* Save the sector number of the first sector of the directory. We will - * need this later for re-scanning the directory to verify that a FAT file - * name is unique. - */ - - startsector = dirinfo->dir.fd_currsector; - - /* Now, search the directory looking for a sequence for free entries that - * long. - */ - - needed = nentries; - for (;;) - { - /* Read the directory sector into fs_buffer */ - - ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector); - if (ret < 0) - { - /* Make sure that the return value is NOT -ENOSPC */ - - return -EIO; - } - - /* Get a pointer to the entry at fd_index */ - - diroffset = (dirinfo->dir.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE; - direntry = &fs->fs_buffer[diroffset]; - - /* Check if this directory entry is empty */ - - ch = LDIR_GETSEQ(direntry); - if (ch == DIR0_ALLEMPTY || ch == DIR0_EMPTY) - { - /* It is empty -- we have found a directory entry. Is this the - * "last" LFN entry (i.e., the one that occurs first)? - */ - - if (needed == nentries) - { - /* Yes.. remember the position of this entry */ - - dirinfo->fd_seq.ds_lfnsector = fs->fs_currentsector; - dirinfo->fd_seq.ds_lfnoffset = diroffset; - dirinfo->fd_seq.ds_lfncluster = dirinfo->dir.fd_currcluster; - dirinfo->fd_seq.ds_startsector = startsector; - } - - /* Is this last entry we need (i.e., the entry for the short - * file name entry)? - */ - - if (needed <= 1) - { - /* Yes.. remember the position of this entry and return - * success. - */ - - dirinfo->fd_seq.ds_sector = fs->fs_currentsector; - dirinfo->fd_seq.ds_offset = diroffset; - dirinfo->fd_seq.ds_cluster = dirinfo->dir.fd_currcluster; - return OK; - } - - /* Otherwise, just decrement the number of directory entries - * needed and continue looking. - */ - - needed--; - } - - /* The directory entry is not available */ - - else - { - /* Reset the search and continue looking */ - - needed = nentries; - } - - /* Try the next directory entry */ - - ret = fat_nextdirentry(fs, &dirinfo->dir); - if (ret < 0) - { - /* This will return -ENOSPC if we have examined all of the - * directory entries without finding a free entry. - */ - - return ret; - } - } -} -#endif - -/**************************************************************************** - * Name: fat_getsfname - * - * Desciption: Get the 8.3 filename from a directory entry. On entry, the - * short file name entry is already in the cache. - * - ****************************************************************************/ - -static inline int fat_getsfname(uint8_t *direntry, char *buffer, - unsigned int buflen) -{ -#ifdef CONFIG_FAT_LCNAMES - uint8_t ntflags; -#endif - int ch; - int ndx; - - /* Check if we will be doing upper to lower case conversions */ - -#ifdef CONFIG_FAT_LCNAMES - ntflags = DIR_GETNTRES(direntry); -#endif - - /* Reserve a byte for the NUL terminator */ - - buflen--; - - /* Get the 8-byte filename */ - - for (ndx = 0; ndx < 8 && buflen > 0; ndx++) - { - /* Get the next filename character from the directory entry */ - - ch = direntry[ndx]; - - /* Any space (or ndx==8) terminates the filename */ - - if (ch == ' ') - { - break; - } - - /* In this version, we never write 0xe5 in the directory filenames - * (because we do not handle any character sets where 0xe5 is valid - * in a filaname), but we could encounted this in a filesystem - * written by some other system - */ - - if (ndx == 0 && ch == DIR0_E5) - { - ch = 0xe5; - } - - /* Check if we should perform upper to lower case conversion - * of the (whole) filename. - */ - -#ifdef CONFIG_FAT_LCNAMES - if (ntflags & FATNTRES_LCNAME && isupper(ch)) - { - ch = tolower(ch); - } -#endif - /* Copy the next character into the filename */ - - *buffer++ = ch; - buflen--; - } - - /* Check if there is an extension */ - - if (direntry[8] != ' ' && buflen > 0) - { - /* Yes, output the dot before the extension */ - - *buffer++ = '.'; - buflen--; - - /* Then output the (up to) 3 character extension */ - - for (ndx = 8; ndx < 11 && buflen > 0; ndx++) - { - /* Get the next extensions character from the directory entry */ - - ch = direntry[DIR_NAME + ndx]; - - /* Any space (or ndx==11) terminates the extension */ - - if (ch == ' ') - { - break; - } - - /* Check if we should perform upper to lower case conversion - * of the (whole) filename. - */ - -#ifdef CONFIG_FAT_LCNAMES - if (ntflags & FATNTRES_LCEXT && isupper(ch)) - { - ch = tolower(ch); - } -#endif - /* Copy the next character into the filename */ - - *buffer++ = ch; - buflen--; - } - } - - /* Put a null terminator at the end of the filename. We don't have to - * check if there is room because we reserved a byte for the NUL - * terminator at the beginning of this function. - */ - - *buffer = '\0'; - return OK; -} - -/**************************************************************************** - * Name: fat_getlfnchunk - * - * Desciption: There are 13 characters per LFN entry, broken up into three - * chunks for characts 1-5, 6-11, and 12-13. This function will get the - * file name characters from one chunk. - * - ****************************************************************************/ - -#ifdef CONFIG_FAT_LFN -static void fat_getlfnchunk(uint8_t *chunk, uint8_t *dest, int nchunk) -{ - wchar_t wch; - int i; - - /* Copy bytes 1-nchunk */ - - for (i = 0; i < nchunk; i++) - { - /* Get the next unicode character from the chunk. We only handle ASCII. - * For ASCII, the upper byte should be zero and the lower should match - * the ASCII code. - */ - - wch = (wchar_t)fat_getuint16(chunk); - *dest++ = (uint8_t)(wch & 0xff); - chunk += sizeof(wchar_t); - } -} -#endif - -/**************************************************************************** - * Name: fat_getlfname - * - * Desciption: Get the long filename from a sequence of directory entries. - * On entry, the "last" long file name entry is in the cache. Returns with - * the short file name entry in the cache. - * - ****************************************************************************/ - -#ifdef CONFIG_FAT_LFN -static inline int fat_getlfname(struct fat_mountpt_s *fs, struct fs_dirent_s *dir) -{ - uint8_t lfname[LDIR_MAXLFNCHARS]; - uint16_t diroffset; - uint8_t *direntry; - uint8_t seqno; - uint8_t rawseq; - uint8_t offset; - uint8_t checksum; - int nsrc; - int ret; - int i; - - /* Get a reference to the current directory entry */ - - diroffset = (dir->u.fat.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE; - direntry = &fs->fs_buffer[diroffset]; - - /* Get the starting sequence number */ - - seqno = LDIR_GETSEQ(direntry); - DEBUGASSERT((seqno & LDIR0_LAST) != 0); - - /* Sanity check */ - - rawseq = (seqno & LDIR0_SEQ_MASK); - if (rawseq < 1 || rawseq > LDIR_MAXLFNS) - { - return -EINVAL; - } - - /* Save the checksum value */ - - checksum = LDIR_GETCHECKSUM(direntry); - - /* Loop until the whole file name has been transferred */ - - for (;;) - { - /* Get the string offset associated with the "last" entry. */ - - offset = (rawseq - 1) * LDIR_MAXLFNCHARS; - - /* Will any of this file name fit into the destination buffer? */ - - if (offset < NAME_MAX) - { - /* Yes.. extract and convert the unicode name */ - - fat_getlfnchunk(LDIR_PTRWCHAR1_5(direntry), lfname, 5); - fat_getlfnchunk(LDIR_PTRWCHAR6_11(direntry), &lfname[5], 6); - fat_getlfnchunk(LDIR_PTRWCHAR12_13(direntry), &lfname[11], 2); - - /* Ignore trailing spaces on the "last" directory entry. The - * number of characters avaiable is LDIR_MAXLFNCHARS or that - * minus the number of trailing spaces on the "last" directory - * entry. - */ - - nsrc = LDIR_MAXLFNCHARS; - if ((seqno & LDIR0_LAST) != 0) - { - /* Reduce the number of characters by the number of trailing - * spaces. - */ - - for (; nsrc > 0 && lfname[nsrc-1] == ' '; nsrc--); - - /* Further reduce the length so that it fits in the destination - * buffer. - */ - - if (offset + nsrc > NAME_MAX) - { - nsrc = NAME_MAX - offset; - } - - /* Add a null terminator to the destination string (the actual - * length of the destination buffer is NAME_MAX+1, so the NUL - * terminator will fit). - */ - - dir->fd_dir.d_name[offset+nsrc] = '\0'; - } - - /* Then transfer the characters */ - - for (i = 0; i < nsrc && offset+i < NAME_MAX; i++) - { - dir->fd_dir.d_name[offset+i] = lfname[i]; - } - } - - /* Read next directory entry */ - - if (fat_nextdirentry(fs, &dir->u.fat) != OK) - { - return -ENOENT; - } - - /* Make sure that the directory sector into the sector cache */ - - ret = fat_fscacheread(fs, dir->u.fat.fd_currsector); - if (ret < 0) - { - return ret; - } - - /* Get a reference to the current directory entry */ - - diroffset = (dir->u.fat.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE; - direntry = &fs->fs_buffer[diroffset]; - - /* Get the next expected sequence number. */ - - seqno = --rawseq; - if (seqno < 1) - { - /* We just completed processing the "first" long file name entry - * and we just read the short file name entry. Verify that the - * checksum of the short file name matches the checksum that we - * found in the long file name entries. - */ - - if (fat_lfnchecksum(direntry) == checksum) - { - /* Yes.. return success! */ - - return OK; - } - - /* No, the checksum is bad. */ - - return -EINVAL; - } - - /* Verify the next long file name entry. Is this an LFN entry? Does it - * have the sequence number we are looking for? Does the checksum - * match the previous entries? - */ - - if (LDIR_GETATTRIBUTES(direntry) != LDDIR_LFNATTR || - LDIR_GETSEQ(direntry) != seqno || - LDIR_GETCHECKSUM(direntry) != checksum) - { - return -EINVAL; - } - } -} -#endif - -/**************************************************************************** - * Name: fat_putsfname - * - * Desciption: Write the short directory entry name. - * - * Assumption: The directory sector is in the cache. - * - ****************************************************************************/ - -static int fat_putsfname(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo) -{ - uint8_t *direntry = &fs->fs_buffer[dirinfo->fd_seq.ds_offset]; - - /* Write the short directory entry */ - - memcpy(&direntry[DIR_NAME], dirinfo->fd_name, DIR_MAXFNAME); -#ifdef CONFIG_FAT_LCNAMES - DIR_PUTNTRES(direntry, dirinfo->fd_ntflags); -#else - DIR_PUTNTRES(direntry, 0); -#endif - fs->fs_dirty = true; - return OK; -} - -/**************************************************************************** - * Name: fat_initlfname - * - * Desciption: There are 13 characters per LFN entry, broken up into three - * chunks for characts 1-5, 6-11, and 12-13. This function will put the - * 0xffff characters into one chunk. - * - ****************************************************************************/ - -#ifdef CONFIG_FAT_LFN -static void fat_initlfname(uint8_t *chunk, int nchunk) -{ - int i; - - /* Initialize unicode characters 1-nchunk */ - - for (i = 0; i < nchunk; i++) - { - /* The write the 16-bit 0xffff character into the directory entry. */ - - fat_putuint16((uint8_t *)chunk, (uint16_t)0xffff); - chunk += sizeof(wchar_t); - } -} -#endif - -/**************************************************************************** - * Name: fat_putlfnchunk - * - * Desciption: There are 13 characters per LFN entry, broken up into three - * chunks for characts 1-5, 6-11, and 12-13. This function will put the - * file name characters into one chunk. - * - ****************************************************************************/ - -#ifdef CONFIG_FAT_LFN -static void fat_putlfnchunk(uint8_t *chunk, const uint8_t *src, int nchunk) -{ - uint16_t wch; - int i; - - /* Write bytes 1-nchunk */ - - for (i = 0; i < nchunk; i++) - { - /* Get the next ascii character from the name substring and convert it - * to unicode. The upper byte should be zero and the lower should be - * the ASCII code. The write the unicode character into the directory - * entry. - */ - - wch = (uint16_t)*src++; - fat_putuint16(chunk, wch); - chunk += sizeof(wchar_t); - } -} -#endif - -/**************************************************************************** - * Name: fat_putlfname - * - * Desciption: Write the long filename into a sequence of directory entries. - * On entry, the "last" long file name entry is in the cache. Returns with - * the short file name entry in the cache. - * - ****************************************************************************/ - -#ifdef CONFIG_FAT_LFN -static int fat_putlfname(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo) -{ - uint16_t diroffset; - uint8_t *direntry; - uint8_t nfullentries; - uint8_t nentries; - uint8_t remainder; - uint8_t offset; - uint8_t seqno; - uint8_t checksum; - int namelen; - int ret; - - /* Get the length of the long file name (size of the fd_lfname array is - * LDIR_MAXFNAME+1 we do not have to check the length of the string). - * NOTE that remainder is conditionally incremented to include the NUL - * terminating character that may also need be written to the directory - * entry. NUL terminating is not required if length is multiple of - * LDIR_MAXLFNCHARS (13). - */ - - namelen = strlen((char*)dirinfo->fd_lfname); - DEBUGASSERT(namelen <= LDIR_MAXFNAME+1); - - /* How many LFN directory entries do we need to write? */ - - nfullentries = namelen / LDIR_MAXLFNCHARS; - remainder = namelen - nfullentries * LDIR_MAXLFNCHARS; - nentries = nfullentries; - if (remainder > 0) - { - nentries++; - remainder++; - } - DEBUGASSERT(nentries > 0 && nentries <= LDIR_MAXLFNS); - - /* Create the short file name alias */ - - ret = fat_createalias(dirinfo); - if (ret < 0) - { - return ret; - } - - /* Set up the initial positional data */ - - dirinfo->dir.fd_currcluster = dirinfo->fd_seq.ds_lfncluster; - dirinfo->dir.fd_currsector = dirinfo->fd_seq.ds_lfnsector; - dirinfo->dir.fd_index = dirinfo->fd_seq.ds_lfnoffset / DIR_SIZE; - - /* Make sure that the alias is unique in this directory*/ - - ret = fat_uniquealias(fs, dirinfo); - if (ret < 0) - { - return ret; - } - - /* Get the short file name checksum */ - - checksum = fat_lfnchecksum(dirinfo->fd_name); - - /* Setup the starting sequence number */ - - seqno = LDIR0_LAST | nentries; - - /* Make sure that the sector containing the "last" long file name entry - * is in the sector cache (it probably is not). - */ - - ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector); - if (ret < 0) - { - return ret; - } - - /* Now loop, writing each long file name entry */ - - for (;;) - { - /* Get the string offset associated with the directory entry. */ - - offset = (nentries - 1) * LDIR_MAXLFNCHARS; - - /* Get a reference to the current directory entry */ - - diroffset = (dirinfo->dir.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE; - direntry = &fs->fs_buffer[diroffset]; - - /* Is this the "last" LFN directory entry? */ - - if ((seqno & LDIR0_LAST) != 0 && remainder != 0) - { - int nbytes; - - /* Initialize the "last" directory entry name to all 0xffff */ - - fat_initlfname(LDIR_PTRWCHAR1_5(direntry), 5); - fat_initlfname(LDIR_PTRWCHAR6_11(direntry), 6); - fat_initlfname(LDIR_PTRWCHAR12_13(direntry), 2); - - /* Store the tail portion of the long file name in directory entry */ - - nbytes = MIN(5, remainder); - fat_putlfnchunk(LDIR_PTRWCHAR1_5(direntry), - &dirinfo->fd_lfname[offset], nbytes); - remainder -= nbytes; - - if (remainder > 0) - { - nbytes = MIN(6, remainder); - fat_putlfnchunk(LDIR_PTRWCHAR6_11(direntry), - &dirinfo->fd_lfname[offset+5], nbytes); - remainder -= nbytes; - } - - if (remainder > 0) - { - nbytes = MIN(2, remainder); - fat_putlfnchunk(LDIR_PTRWCHAR12_13(direntry), - &dirinfo->fd_lfname[offset+11], nbytes); - remainder -= nbytes; - } - - /* The remainder should now be zero */ - - DEBUGASSERT(remainder == 0); - } - else - { - /* Store a portion long file name in this directory entry */ - - fat_putlfnchunk(LDIR_PTRWCHAR1_5(direntry), - &dirinfo->fd_lfname[offset], 5); - fat_putlfnchunk(LDIR_PTRWCHAR6_11(direntry), - &dirinfo->fd_lfname[offset+5], 6); - fat_putlfnchunk(LDIR_PTRWCHAR12_13(direntry), - &dirinfo->fd_lfname[offset+11], 2); - } - - /* Write the remaining directory entries */ - - LDIR_PUTSEQ(direntry, seqno); - LDIR_PUTATTRIBUTES(direntry, LDDIR_LFNATTR); - LDIR_PUTNTRES(direntry, 0); - LDIR_PUTCHECKSUM(direntry, checksum); - fs->fs_dirty = true; - - /* Read next directory entry */ - - if (fat_nextdirentry(fs, &dirinfo->dir) != OK) - { - return -ENOENT; - } - - /* Make sure that the sector containing the directory entry is in the - * sector cache - */ - - ret = fat_fscacheread(fs, dirinfo->dir.fd_currsector); - if (ret < 0) - { - return ret; - } - - /* Decrement the number of entries and get the next sequence number. */ - - if (--nentries <= 0) - { - /* We have written all of the long file name entries to the media - * and we have the short file name entry in the cache. We can - * just return success. - */ - - return OK; - } - - /* The sequence number is just the number of entries left to be - * written. - */ - - seqno = nentries; - } -} -#endif - -/**************************************************************************** - * Name: fat_putsfdirentry - * - * Desciption: Write a short file name directory entry - * - * Assumption: The directory sector is in the cache. The caller will write - * sector information. - * - ****************************************************************************/ - -static int fat_putsfdirentry(struct fat_mountpt_s *fs, - struct fat_dirinfo_s *dirinfo, - uint8_t attributes, uint32_t fattime) -{ - uint8_t *direntry; - - /* Initialize the 32-byte directory entry */ - - direntry = &fs->fs_buffer[dirinfo->fd_seq.ds_offset]; - memset(direntry, 0, DIR_SIZE); - - /* Directory name info */ - - (void)fat_putsfname(fs, dirinfo); - - /* Set the attribute attribute, write time, creation time */ - - DIR_PUTATTRIBUTES(direntry, attributes); - - /* Set the time information */ - - DIR_PUTWRTTIME(direntry, fattime & 0xffff); - DIR_PUTCRTIME(direntry, fattime & 0xffff); - DIR_PUTWRTDATE(direntry, fattime >> 16); - DIR_PUTCRDATE(direntry, fattime >> 16); - - fs->fs_dirty = true; - return OK; -} - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: fat_finddirentry - * - * Desciption: Given a path to something that may or may not be in the file - * system, return the description of the directory entry of the requested - * item. - * - * NOTE: As a side effect, this function returns with the sector containing - * the short file name directory entry in the cache. - * - ****************************************************************************/ - -int fat_finddirentry(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo, - const char *path) -{ - off_t cluster; - uint8_t *direntry; - char terminator; - int ret; - - /* Initialize to traverse the chain. Set it to the cluster of the root - * directory - */ - - cluster = fs->fs_rootbase; - if (fs->fs_type == FSTYPE_FAT32) - { - /* For FAT32, the root directory is variable sized and is a cluster - * chain like any other directory. fs_rootbase holds the first - * cluster of the root directory. - */ - - dirinfo->dir.fd_startcluster = cluster; - dirinfo->dir.fd_currcluster = cluster; - dirinfo->dir.fd_currsector = fat_cluster2sector(fs, cluster); - } - else - { - /* For FAT12/16, the first sector of the root directory is a sector - * relative to the first sector of the fat volume. - */ - - dirinfo->dir.fd_startcluster = 0; - dirinfo->dir.fd_currcluster = 0; - dirinfo->dir.fd_currsector = cluster; - } - - /* fd_index is the index into the current directory table. It is set to the - * the first, entry in the root directory. - */ - - dirinfo->dir.fd_index = 0; - - /* If no path was provided, then the root directory must be exactly what - * the caller is looking for. - */ - - if (*path == '\0') - { - dirinfo->fd_root = true; - return OK; - } - - /* This is not the root directory */ - - dirinfo->fd_root = false; - - /* Now loop until the directory entry corresponding to the path is found */ - - for (;;) - { - /* Convert the next the path segment name into the kind of name that - * we would see in the directory entry. - */ - - ret = fat_path2dirname(&path, dirinfo, &terminator); - if (ret < 0) - { - /* ERROR: The filename contains invalid characters or is - * too long. - */ - - return ret; - } - - /* Is this a path segment a long or a short file. Was a long file - * name parsed? - */ - -#ifdef CONFIG_FAT_LFN - if (dirinfo->fd_lfname[0] != '\0') - { - /* Yes.. Search for the sequence of long file name directory - * entries. NOTE: As a side effect, this function returns with - * the sector containing the short file name directory entry - * in the cache. - */ - - ret = fat_findlfnentry(fs, dirinfo); - } - else -#endif - { - /* No.. Search for the single short file name directory entry */ - - ret = fat_findsfnentry(fs, dirinfo); - } - - /* Did we find the directory entries? */ - - if (ret < 0) - { - return ret; - } - - /* If the terminator character in the path was the end of the string - * then we have successfully found the directory entry that describes - * the path. - */ - - if (!terminator) - { - /* Return success meaning that the description the matching - * directory entry is in dirinfo. - */ - - return OK; - } - - /* No.. then we have found one of the intermediate directories on - * the way to the final path target. In this case, make sure - * the thing that we found is, indeed, a directory. - */ - - direntry = &fs->fs_buffer[dirinfo->fd_seq.ds_offset]; - if (!(DIR_GETATTRIBUTES(direntry) & FATATTR_DIRECTORY)) - { - /* Ooops.. we found something else */ - - return -ENOTDIR; - } - - /* Get the cluster number of this directory */ - - cluster = - ((uint32_t)DIR_GETFSTCLUSTHI(direntry) << 16) | - DIR_GETFSTCLUSTLO(direntry); - - /* Then restart scanning at the new directory, skipping over both the - * '.' and '..' entries that exist in all directories EXCEPT the root - * directory. - */ - - dirinfo->dir.fd_startcluster = cluster; - dirinfo->dir.fd_currcluster = cluster; - dirinfo->dir.fd_currsector = fat_cluster2sector(fs, cluster); - dirinfo->dir.fd_index = 2; - } -} - -/**************************************************************************** - * Name: fat_allocatedirentry - * - * Desciption: Find a free directory entry - * - ****************************************************************************/ - -int fat_allocatedirentry(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo) -{ - int32_t cluster; - off_t sector; - int ret; - int i; - - /* Re-initialize directory object */ - - cluster = dirinfo->dir.fd_startcluster; - - /* Loop until we successfully allocate the sequence of directory entries - * or until to fail to extend the directory cluster chain. - */ - - for (;;) - { - /* Can this cluster chain be extended */ - - if (cluster) - { - /* Cluster chain can be extended */ - - dirinfo->dir.fd_currcluster = cluster; - dirinfo->dir.fd_currsector = fat_cluster2sector(fs, cluster); - } - else - { - /* Fixed size FAT12/16 root directory is at fixed offset/size */ - - dirinfo->dir.fd_currsector = fs->fs_rootbase; - } - - /* Start at the first entry in the root directory. */ - - dirinfo->dir.fd_index = 0; - - /* Is this a path segment a long or a short file. Was a long file - * name parsed? - */ - -#ifdef CONFIG_FAT_LFN - if (dirinfo->fd_lfname[0] != '\0') - { - /* Yes.. Allocate for the sequence of long file name directory - * entries plus a short file name directory entry. - */ - - ret = fat_allocatelfnentry(fs, dirinfo); - } - - /* No.. Allocate only a short file name directory entry */ - - else -#endif - { - ret = fat_allocatesfnentry(fs, dirinfo); - } - - /* Did we successfully allocate the directory entries? If the error - * value is -ENOSPC, then we can try to extend the directory cluster - * (we can't handle other return values) - */ - - if (ret == OK || ret != -ENOSPC) - { - return ret; - } - - /* If we get here, then we have reached the end of the directory table - * in this sector without finding a free directory entry. - * - * It this is a fixed size directory entry, then this is an error. - * Otherwise, we can try to extend the directory cluster chain to - * make space for the new directory entry. - */ - - if (!cluster) - { - /* The size is fixed */ - - return -ENOSPC; - } - - /* Try to extend the cluster chain for this directory */ - - cluster = fat_extendchain(fs, dirinfo->dir.fd_currcluster); - if (cluster < 0) - { - return cluster; - } - - /* Flush out any cached data in fs_buffer.. we are going to use - * it to initialize the new directory cluster. - */ - - ret = fat_fscacheflush(fs); - if (ret < 0) - { - return ret; - } - - /* Clear all sectors comprising the new directory cluster */ - - fs->fs_currentsector = fat_cluster2sector(fs, cluster); - memset(fs->fs_buffer, 0, fs->fs_hwsectorsize); - - sector = fs->fs_currentsector; - for (i = fs->fs_fatsecperclus; i; i--) - { - ret = fat_hwwrite(fs, fs->fs_buffer, sector, 1); - if (ret < 0) - { - return ret; - } - sector++; - } - } -} - -/**************************************************************************** - * Name: fat_freedirentry - * - * Desciption: Free the directory entry. - * - * NOTE: As a side effect, this function returns with the sector containing - * the deleted short file name directory entry in the cache. - * - ****************************************************************************/ - -int fat_freedirentry(struct fat_mountpt_s *fs, struct fat_dirseq_s *seq) -{ -#ifdef CONFIG_FAT_LFN - struct fs_fatdir_s dir; - uint16_t diroffset; - uint8_t *direntry; - int ret; - - /* Set it to the cluster containing the "last" LFN entry (that appears - * first on the media). - */ - - dir.fd_currcluster = seq->ds_lfncluster; - dir.fd_currsector = seq->ds_lfnsector; - dir.fd_index = seq->ds_lfnoffset / DIR_SIZE; - - /* Free all of the directory entries used for the sequence of long file name - * and for the single short file name entry. - */ - - for (;;) - { - /* Read the directory sector into the sector cache */ - - ret = fat_fscacheread(fs, dir.fd_currsector); - if (ret < 0) - { - return ret; - } - - /* Get a pointer to the directory entry */ - - diroffset = (dir.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE; - direntry = &fs->fs_buffer[diroffset]; - - /* Then mark the entry as deleted */ - - direntry[DIR_NAME] = DIR0_EMPTY; - fs->fs_dirty = true; - - /* Did we just free the single short file name entry? */ - - if (dir.fd_currsector == seq->ds_sector && - diroffset == seq->ds_offset) - { - /* Yes.. then we are finished. flush anything remaining in the - * cache and return, probably successfully. - */ - - return fat_fscacheflush(fs); - } - - /* There are more entries to go.. Try the next directory entry */ - - ret = fat_nextdirentry(fs, &dir); - if (ret < 0) - { - return ret; - } - } - -#else - uint8_t *direntry; - int ret; - - /* Free the single short file name entry. - * - * Make sure that the sector containing the directory entry is in the - * cache. - */ - - ret = fat_fscacheread(fs, seq->ds_sector); - if (ret == OK) - { - /* Then mark the entry as deleted */ - - direntry = &fs->fs_buffer[seq->ds_offset]; - direntry[DIR_NAME] = DIR0_EMPTY; - fs->fs_dirty = true; - } - - return ret; -#endif -} - -/**************************************************************************** - * Name: fat_dirname2path - * - * Desciption: Convert a filename in a raw directory entry into a user - * filename. This is essentially the inverse operation of that performed - * by fat_path2dirname. See that function for more details. - * - ****************************************************************************/ - -int fat_dirname2path(struct fat_mountpt_s *fs, struct fs_dirent_s *dir) -{ - uint16_t diroffset; - uint8_t *direntry; -#ifdef CONFIG_FAT_LFN - uint8_t attribute; -#endif - - /* Get a reference to the current directory entry */ - - diroffset = (dir->u.fat.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE; - direntry = &fs->fs_buffer[diroffset]; - - /* Does this entry refer to the last entry of a long file name? */ - -#ifdef CONFIG_FAT_LFN - attribute = DIR_GETATTRIBUTES(direntry); - if (((*direntry & LDIR0_LAST) != 0 && attribute == LDDIR_LFNATTR)) - { - /* Yes.. Get the name from a sequence of long file name directory - * entries. - */ - - return fat_getlfname(fs, dir); - } - else -#endif - { - /* No.. Get the name from a short file name directory entries */ - - return fat_getsfname(direntry, dir->fd_dir.d_name, NAME_MAX+1); - } -} - -/**************************************************************************** - * Name: fat_dirnamewrite - * - * Desciption: Write the (possibly long) directory entry name. This function - * is called only from fat_rename to write the new file name. - * - * Assumption: The directory sector containing the short file name entry - * is in the cache. *NOT* the sector containing the last long file name - * entry! - * - ****************************************************************************/ - -int fat_dirnamewrite(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo) -{ -#ifdef CONFIG_FAT_LFN - int ret; - - /* Is this a long file name? */ - - if (dirinfo->fd_lfname[0] != '\0') - { - /* Write the sequence of long file name directory entries (this function - * also creates the short file name alias). - */ - - ret = fat_putlfname(fs, dirinfo); - if (ret != OK) - { - return ret; - } - } - - /* On return, fat_lfsfname() will leave the short file name entry in the - * cache. So we can just fall throught to write that directory entry, perhaps - * using the short file name alias for the long file name. - */ -#endif - - return fat_putsfname(fs, dirinfo); -} - -/**************************************************************************** - * Name: fat_dirwrite - * - * Desciption: Write a directory entry, possibly with a long file name. - * Called from: - * - * fat_mkdir() to write the new FAT directory entry. - * fat_dircreate() to create any new directory entry. - * - * Assumption: The directory sector is in the cache. The caller will write - * sector information. - * - ****************************************************************************/ - -int fat_dirwrite(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo, - uint8_t attributes, uint32_t fattime) -{ -#ifdef CONFIG_FAT_LFN - int ret; - - /* Does this directory entry have a long file name? */ - - if (dirinfo->fd_lfname[0] != '\0') - { - /* Write the sequence of long file name directory entries (this function - * also creates the short file name alias). - */ - - ret = fat_putlfname(fs, dirinfo); - if (ret != OK) - { - return ret; - } - } - - /* On return, fat_lfsfname() will leave the short file name entry in the - * cache. So we can just fall throught to write that directory entry, perhaps - * using the short file name alias for the long file name. - */ -#endif - - /* Put the short file name entry data */ - - return fat_putsfdirentry(fs, dirinfo, attributes, fattime); -} - -/**************************************************************************** - * Name: fat_dircreate - * - * Desciption: Create a directory entry for a new file - * - ****************************************************************************/ - -int fat_dircreate(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo) -{ - uint32_t fattime; - int ret; - - /* Allocate a directory entry. If long file name support is enabled, then - * this might, in fact, allocate a sequence of directory entries. - */ - - ret = fat_allocatedirentry(fs, dirinfo); - if (ret != OK) - { - /* Failed to allocate the required directory entry or entries. */ - - return ret; - } - - /* Write the directory entry (or entries) with the current time and the - * ARCHIVE attribute. - */ - - fattime = fat_systime2fattime(); - return fat_dirwrite(fs, dirinfo, FATATTR_ARCHIVE, fattime); -} - -/**************************************************************************** - * Name: fat_remove - * - * Desciption: Remove a directory or file from the file system. This - * implements both rmdir() and unlink(). - * - ****************************************************************************/ - -int fat_remove(struct fat_mountpt_s *fs, const char *relpath, bool directory) -{ - struct fat_dirinfo_s dirinfo; - uint32_t dircluster; - uint8_t *direntry; - int ret; - - /* Find the directory entry referring to the entry to be deleted */ - - ret = fat_finddirentry(fs, &dirinfo, relpath); - if (ret != OK) - { - /* No such path */ - - return -ENOENT; - } - - /* Check if this is a FAT12/16 root directory */ - - if (dirinfo.fd_root) - { - /* The root directory cannot be removed */ - - return -EPERM; - } - - /* The object has to have write access to be deleted */ - - direntry = &fs->fs_buffer[dirinfo.fd_seq.ds_offset]; - if ((DIR_GETATTRIBUTES(direntry) & FATATTR_READONLY) != 0) - { - /* It is a read-only entry */ - - return -EACCES; - } - - /* Get the directory sector and cluster containing the entry to be deleted. */ - - dircluster = - ((uint32_t)DIR_GETFSTCLUSTHI(direntry) << 16) | - DIR_GETFSTCLUSTLO(direntry); - - /* Is this entry a directory? */ - - if (DIR_GETATTRIBUTES(direntry) & FATATTR_DIRECTORY) - { - /* It is a sub-directory. Check if we are be asked to remove - * a directory or a file. - */ - - if (!directory) - { - /* We are asked to delete a file */ - - return -EISDIR; - } - - /* We are asked to delete a directory. Check if this sub-directory is - * empty (i.e., that there are no valid entries other than the initial - * '.' and '..' entries). - */ - - dirinfo.dir.fd_currcluster = dircluster; - dirinfo.dir.fd_currsector = fat_cluster2sector(fs, dircluster); - dirinfo.dir.fd_index = 2; - - /* Loop until either (1) an entry is found in the directory (error), - * (2) the directory is found to be empty, or (3) some error occurs. - */ - - for (;;) - { - unsigned int subdirindex; - uint8_t *subdirentry; - - /* Make sure that the sector containing the of the subdirectory - * sector is in the cache - */ - - ret = fat_fscacheread(fs, dirinfo.dir.fd_currsector); - if (ret < 0) - { - return ret; - } - - /* Get a reference to the next entry in the directory */ - - subdirindex = (dirinfo.dir.fd_index & DIRSEC_NDXMASK(fs)) * DIR_SIZE; - subdirentry = &fs->fs_buffer[subdirindex]; - - /* Is this the last entry in the direcory? */ - - if (subdirentry[DIR_NAME] == DIR0_ALLEMPTY) - { - /* Yes then the directory is empty. Break out of the - * loop and delete the directory. - */ - - break; - } - - /* Check if the next entry refers to a file or directory */ - - if (subdirentry[DIR_NAME] != DIR0_EMPTY && - !(DIR_GETATTRIBUTES(subdirentry) & FATATTR_VOLUMEID)) - { - /* The directory is not empty */ - - return -ENOTEMPTY; - } - - /* Get the next directory entry */ - - ret = fat_nextdirentry(fs, &dirinfo.dir); - if (ret < 0) - { - return ret; - } - } - } - else - { - /* It is a file. Check if we are be asked to remove a directory - * or a file. - */ - - if (directory) - { - /* We are asked to remove a directory */ - - return -ENOTDIR; - } - } - - /* Mark the directory entry 'deleted'. If long file name support is - * enabled, then multiple directory entries may be freed. - */ - - ret = fat_freedirentry(fs, &dirinfo.fd_seq); - if (ret < 0) - { - return ret; - } - - /* And remove the cluster chain making up the subdirectory */ - - ret = fat_removechain(fs, dircluster); - if (ret < 0) - { - return ret; - } - - /* Update the FSINFO sector (FAT32) */ - - ret = fat_updatefsinfo(fs); - if (ret < 0) - { - return ret; - } - - return OK; -} diff --git a/nuttx/fs/fat/fs_fat32util.c b/nuttx/fs/fat/fs_fat32util.c deleted file mode 100644 index 8edef7735..000000000 --- a/nuttx/fs/fat/fs_fat32util.c +++ /dev/null @@ -1,1848 +0,0 @@ -/**************************************************************************** - * fs/fat/fs_fat32util.c - * - * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt <gnutt@nuttx.org> - * - * References: - * Microsoft FAT documentation - * Some good ideas were leveraged from the FAT implementation: - * 'Copyright (C) 2007, ChaN, all right reserved.' - * which has an unrestricted license. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name NuttX nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <nuttx/config.h> - -#include <sys/types.h> -#include <stdint.h> -#include <stdbool.h> -#include <stdlib.h> -#include <string.h> -#include <time.h> -#include <semaphore.h> -#include <assert.h> -#include <errno.h> -#include <debug.h> - -#include <nuttx/kmalloc.h> -#include <nuttx/fs/fs.h> -#include <nuttx/fs/fat.h> - -#include "fs_internal.h" -#include "fs_fat32.h" - -/**************************************************************************** - * Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Private Variables - ****************************************************************************/ - -/**************************************************************************** - * Public Variables - ****************************************************************************/ - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: fat_checkfsinfo - * - * Desciption: Read the FAT32 FSINFO sector - * - ****************************************************************************/ - -static int fat_checkfsinfo(struct fat_mountpt_s *fs) -{ - /* Make sure that the fsinfo sector is in the cache */ - - if (fat_fscacheread(fs, fs->fs_fsinfo) == OK) - { - /* Verify that this is, indeed, an FSINFO sector */ - - if (FSI_GETLEADSIG(fs->fs_buffer) == 0x41615252 && - FSI_GETSTRUCTSIG(fs->fs_buffer) == 0x61417272 && - FSI_GETTRAILSIG(fs->fs_buffer) == BOOT_SIGNATURE32) - { - fs->fs_fsifreecount = FSI_GETFREECOUNT(fs->fs_buffer); - fs->fs_fsinextfree = FSI_GETNXTFREE(fs->fs_buffer); - return OK; - } - } - - return -ENODEV; -} - -/**************************************************************************** - * Name: fat_checkbootrecord - * - * Desciption: Read a sector and verify that it is a a FAT boot record. - * - ****************************************************************************/ - -static int fat_checkbootrecord(struct fat_mountpt_s *fs) -{ - uint32_t ndatasectors; - uint32_t ntotalfatsects; - uint16_t rootdirsectors = 0; - bool notfat32 = false; - - /* Verify the MBR signature at offset 510 in the sector (true even - * if the sector size is greater than 512. All FAT file systems have - * this signature. On a FAT32 volume, the RootEntCount , FatSz16, and - * FatSz32 values should always be zero. The FAT sector size should - * match the reported hardware sector size. - */ - - if (MBR_GETSIGNATURE(fs->fs_buffer) != BOOT_SIGNATURE16 || - MBR_GETBYTESPERSEC(fs->fs_buffer) != fs->fs_hwsectorsize) - { - fdbg("ERROR: Signature: %04x FS sectorsize: %d HW sectorsize: %d\n", - MBR_GETSIGNATURE(fs->fs_buffer), MBR_GETBYTESPERSEC(fs->fs_buffer), - fs->fs_hwsectorsize); - - return -EINVAL; - } - - /* Verify the FAT32 file system type. The determination of the file - * system type is based on the number of clusters on the volume: FAT12 - * volume has <= FAT_MAXCLUST12 (4084) clusters, a FAT16 volume has <= - * FAT_MINCLUST16 (microsfoft says < 65,525) clusters, and any larger - * is FAT32. - * - * Get the number of 32-bit directory entries in root directory (zero - * for FAT32). - */ - - fs->fs_rootentcnt = MBR_GETROOTENTCNT(fs->fs_buffer); - if (fs->fs_rootentcnt != 0) - { - notfat32 = true; /* Must be zero for FAT32 */ - rootdirsectors = (32 * fs->fs_rootentcnt + fs->fs_hwsectorsize - 1) / fs->fs_hwsectorsize; - } - - /* Determine the number of sectors in a FAT. */ - - fs->fs_nfatsects = MBR_GETFATSZ16(fs->fs_buffer); /* Should be zero */ - if (fs->fs_nfatsects) - { - notfat32 = true; /* Must be zero for FAT32 */ - } - else - { - fs->fs_nfatsects = MBR_GETFATSZ32(fs->fs_buffer); - } - - if (!fs->fs_nfatsects || fs->fs_nfatsects >= fs->fs_hwnsectors) - { - fdbg("ERROR: fs_nfatsects %d fs_hwnsectors: %d\n", - fs->fs_nfatsects, fs->fs_hwnsectors); - - return -EINVAL; - } - - /* Get the total number of sectors on the volume. */ - - fs->fs_fattotsec = MBR_GETTOTSEC16(fs->fs_buffer); /* Should be zero */ - if (fs->fs_fattotsec) - { - notfat32 = true; /* Must be zero for FAT32 */ - } - else - { - fs->fs_fattotsec = MBR_GETTOTSEC32(fs->fs_buffer); - } - - if (!fs->fs_fattotsec || fs->fs_fattotsec > fs->fs_hwnsectors) - { - fdbg("ERROR: fs_fattotsec %d fs_hwnsectors: %d\n", - fs->fs_fattotsec, fs->fs_hwnsectors); - - return -EINVAL; - } - - /* Get the total number of reserved sectors */ - - fs->fs_fatresvdseccount = MBR_GETRESVDSECCOUNT(fs->fs_buffer); - if (fs->fs_fatresvdseccount > fs->fs_hwnsectors) - { - fdbg("ERROR: fs_fatresvdseccount %d fs_hwnsectors: %d\n", - fs->fs_fatresvdseccount, fs->fs_hwnsectors); - - return -EINVAL; - } - - /* Get the number of FATs. This is probably two but could have other values */ - - fs->fs_fatnumfats = MBR_GETNUMFATS(fs->fs_buffer); - ntotalfatsects = fs->fs_fatnumfats * fs->fs_nfatsects; - - /* Get the total number of data sectors */ - - ndatasectors = fs->fs_fattotsec - fs->fs_fatresvdseccount - ntotalfatsects - rootdirsectors; - if (ndatasectors > fs->fs_hwnsectors) - { - fdbg("ERROR: ndatasectors %d fs_hwnsectors: %d\n", - ndatasectors, fs->fs_hwnsectors); - - return -EINVAL; - } - - /* Get the sectors per cluster */ - - fs->fs_fatsecperclus = MBR_GETSECPERCLUS(fs->fs_buffer); - - /* Calculate the number of clusters */ - - fs->fs_nclusters = ndatasectors / fs->fs_fatsecperclus; - - /* Finally, the test: */ - - if (fs->fs_nclusters <= FAT_MAXCLUST12) - { - fs->fs_fsinfo = 0; - fs->fs_type = FSTYPE_FAT12; - } - else if (fs->fs_nclusters <= FAT_MAXCLUST16) - { - fs->fs_fsinfo = 0; - fs->fs_type = FSTYPE_FAT16; - } - else if (!notfat32) - { - fs->fs_fsinfo = fs->fs_fatbase + MBR_GETFSINFO(fs->fs_buffer); - fs->fs_type = FSTYPE_FAT32; - } - else - { - fdbg("ERROR: notfat32: %d fs_nclusters: %d\n", - notfat32, fs->fs_nclusters); - - return -EINVAL; - } - - /* We have what appears to be a valid FAT filesystem! Save a few more things - * from the boot record that we will need later. - */ - - fs->fs_fatbase += fs->fs_fatresvdseccount; - - if (fs->fs_type == FSTYPE_FAT32) - { - fs->fs_rootbase = MBR_GETROOTCLUS(fs->fs_buffer); - } - else - { - fs->fs_rootbase = fs->fs_fatbase + ntotalfatsects; - } - - fs->fs_database = fs->fs_fatbase + ntotalfatsects + fs->fs_rootentcnt / DIRSEC_NDIRS(fs); - fs->fs_fsifreecount = 0xffffffff; - - return OK; -} - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: fat_getuint16 - ****************************************************************************/ - -uint16_t fat_getuint16(uint8_t *ptr) -{ -#ifdef CONFIG_ENDIAN_BIG - /* The bytes always have to be swapped if the target is big-endian */ - - return ((uint16_t)ptr[0] << 8) | ptr[1]; -#else - /* Byte-by-byte transfer is still necessary if the address is un-aligned */ - - return ((uint16_t)ptr[1] << 8) | ptr[0]; -#endif -} - -/**************************************************************************** - * Name: fat_getuint32 - ****************************************************************************/ - -uint32_t fat_getuint32(uint8_t *ptr) -{ -#ifdef CONFIG_ENDIAN_BIG - /* The bytes always have to be swapped if the target is big-endian */ - - return ((uint32_t)fat_getuint16(&ptr[0]) << 16) | fat_getuint16(&ptr[2]); -#else - /* Byte-by-byte transfer is still necessary if the address is un-aligned */ - - return ((uint32_t)fat_getuint16(&ptr[2]) << 16) | fat_getuint16(&ptr[0]); -#endif -} - -/**************************************************************************** - * Name: fat_putuint16 - ****************************************************************************/ - -void fat_putuint16(uint8_t *ptr, uint16_t value16) -{ - uint8_t *val = (uint8_t*)&value16; -#ifdef CONFIG_ENDIAN_BIG - /* The bytes always have to be swapped if the target is big-endian */ - - ptr[0] = val[1]; - ptr[1] = val[0]; -#else - /* Byte-by-byte transfer is still necessary if the address is un-aligned */ - - ptr[0] = val[0]; - ptr[1] = val[1]; -#endif -} - -/**************************************************************************** - * Name: fat_putuint32 - ****************************************************************************/ - -void fat_putuint32(uint8_t *ptr, uint32_t value32) -{ - uint16_t *val = (uint16_t*)&value32; -#ifdef CONFIG_ENDIAN_BIG - /* The bytes always have to be swapped if the target is big-endian */ - - fat_putuint16(&ptr[0], val[2]); - fat_putuint16(&ptr[2], val[0]); -#else - /* Byte-by-byte transfer is still necessary if the address is un-aligned */ - - fat_putuint16(&ptr[0], val[0]); - fat_putuint16(&ptr[2], val[2]); -#endif -} - -/**************************************************************************** - * Name: fat_semtake - ****************************************************************************/ - -void fat_semtake(struct fat_mountpt_s *fs) -{ - /* Take the semaphore (perhaps waiting) */ - - while (sem_wait(&fs->fs_sem) != 0) - { - /* The only case that an error should occur here is if - * the wait was awakened by a signal. - */ - - ASSERT(*get_errno_ptr() == EINTR); - } -} - -/**************************************************************************** - * Name: fat_semgive - ****************************************************************************/ - -void fat_semgive(struct fat_mountpt_s *fs) -{ - sem_post(&fs->fs_sem); -} - -/**************************************************************************** - * Name: fat_systime2fattime - * - * Desciption: Get the system time convert to a time and and date suitble - * for writing into the FAT FS. - * - * TIME in LS 16-bits: - * Bits 0:4 = 2 second count (0-29 representing 0-58 seconds) - * Bits 5-10 = minutes (0-59) - * Bits 11-15 = hours (0-23) - * DATE in MS 16-bits - * Bits 0:4 = Day of month (1-31) - * Bits 5:8 = Month of year (1-12) - * Bits 9:15 = Year from 1980 (0-127 representing 1980-2107) - * - ****************************************************************************/ - -uint32_t fat_systime2fattime(void) -{ - /* Unless you have a hardware RTC or some other to get accurate time, then - * there is no reason to support FAT time. - */ - -#ifdef CONFIG_FS_FATTIME - struct timespec ts; - struct tm tm; - int ret; - - /* Get the current time in seconds and nanoseconds */ - - ret = clock_gettime(CLOCK_REALTIME, &ts); - if (ret == OK) - { - /* Break done the seconds in date and time units */ - - if (gmtime_r((FAR const time_t *)&ts.tv_sec, &tm) != NULL) - { - /* FAT can only represent dates since 1980. struct tm can - * represent dates since 1900. - */ - - if (tm.tm_year >= 80) - { - uint16_t fattime; - uint16_t fatdate; - - fattime = (tm.tm_sec >> 1) & 0x001f; /* Bits 0-4: 2 second count (0-29) */ - fattime |= (tm.tm_min << 5) & 0x07e0; /* Bits 5-10: minutes (0-59) */ - fattime |= (tm.tm_hour << 11) & 0xf800; /* Bits 11-15: hours (0-23) */ - - fatdate = tm.tm_mday & 0x001f; /* Bits 0-4: Day of month (1-31) */ - fatdate |= ((tm.tm_mon+1) << 5) & 0x01e0; /* Bits 5-8: Month of year (1-12) */ - fatdate |= ((tm.tm_year-80) << 9) & 0xfe00; /* Bits 9-15: Year from 1980 */ - - return (uint32_t)fatdate << 16 | (uint32_t)fattime; - } - } - } -#endif - return 0; -} - -/**************************************************************************** - * Name: fat_fattime2systime - * - * Desciption: Convert FAT data and time to a system time_t - * - * 16-bit FAT time: - * Bits 0:4 = 2 second count (0-29 representing 0-58 seconds) - * Bits 5-10 = minutes (0-59) - * Bits 11-15 = hours (0-23) - * 16-bit FAT date: - * Bits 0:4 = Day of month (1-31) - * Bits 5:8 = Month of year (1-12) - * Bits 9:15 = Year from 1980 (0-127 representing 1980-2107) - * - ****************************************************************************/ - -time_t fat_fattime2systime(uint16_t fattime, uint16_t fatdate) -{ - /* Unless you have a hardware RTC or some other to get accurate time, then - * there is no reason to support FAT time. - */ - -#ifdef CONFIG_FS_FATTIME - struct tm tm; - unsigned int tmp; - - /* Break out the date and time */ - - tm.tm_sec = (fattime & 0x001f) << 1; /* Bits 0-4: 2 second count (0-29) */ - tm.tm_min = (fattime & 0x07e0) >> 5; /* Bits 5-10: minutes (0-59) */ - tm.tm_hour = (fattime & 0xf800) >> 11; /* Bits 11-15: hours (0-23) */ - - tm.tm_mday = (fatdate & 0x001f); /* Bits 0-4: Day of month (1-31) */ - tmp = ((fatdate & 0x01e0) >> 5); /* Bits 5-8: Month of year (1-12) */ - tm.tm_mon = tmp > 0 ? tmp-1 : 0; - tm.tm_year = ((fatdate & 0xfe00) >> 9) + 80; /* Bits 9-15: Year from 1980 */ - - /* Then convert the broken out time into seconds since the epoch */ - - return mktime(&tm); -#else - return 0; -#endif -} - -/**************************************************************************** - * Name: fat_mount - * - * Desciption: This function is called only when the mountpoint is first - * established. It initializes the mountpoint structure and verifies - * that a valid FAT32 filesystem is provided by the block driver. - * - * The caller should hold the mountpoint semaphore - * - ****************************************************************************/ - -int fat_mount(struct fat_mountpt_s *fs, bool writeable) -{ - FAR struct inode *inode; - struct geometry geo; - int ret; - - /* Assume that the mount is successful */ - - fs->fs_mounted = true; - - /* Check if there is media available */ - - inode = fs->fs_blkdriver; - if (!inode || !inode->u.i_bops || !inode->u.i_bops->geometry || - inode->u.i_bops->geometry(inode, &geo) != OK || !geo.geo_available) - { - ret = -ENODEV; - goto errout; - } - - /* Make sure that that the media is write-able (if write access is needed) */ - - if (writeable && !geo.geo_writeenabled) - { - ret = -EACCES; - goto errout; - } - - /* Save the hardware geometry */ - - fs->fs_hwsectorsize = geo.geo_sectorsize; - fs->fs_hwnsectors = geo.geo_nsectors; - - /* Allocate a buffer to hold one hardware sector */ - - fs->fs_buffer = (uint8_t*)fat_io_alloc(fs->fs_hwsectorsize); - if (!fs->fs_buffer) - { - ret = -ENOMEM; - goto errout; - } - - /* Search FAT boot record on the drive. First check at sector zero. This - * could be either the boot record or a partition that refers to the boot - * record. - * - * First read sector zero. This will be the first access to the drive and a - * likely failure point. - */ - - fs->fs_fatbase = 0; - ret = fat_hwread(fs, fs->fs_buffer, 0, 1); - if (ret < 0) - { - goto errout_with_buffer; - } - - ret = fat_checkbootrecord(fs); - if (ret != OK) - { - /* The contents of sector 0 is not a boot record. It could be a - * partition, however. Assume it is a partition and get the offset - * into the partition table. This table is at offset MBR_TABLE and is - * indexed by 16x the partition number. - */ - - int i; - for (i = 0; i < 4; i++) - { - /* Check if the partition exists and, if so, get the bootsector for that - * partition and see if we can find the boot record there. - */ - - uint8_t part = PART_GETTYPE(i, fs->fs_buffer); - fvdbg("Partition %d, offset %d, type %d\n", i, PART_ENTRY(i), part); - - if (part == 0) - { - fvdbg("No partition %d\n", i); - continue; - } - - /* There appears to be a partition, get the sector number of the - * partition (LBA) - */ - - fs->fs_fatbase = PART_GETSTARTSECTOR(i, fs->fs_buffer); - - /* Read the new candidate boot sector */ - - ret = fat_hwread(fs, fs->fs_buffer, fs->fs_fatbase, 1); - if (ret < 0) - { - /* Failed to read the sector */ - - goto errout_with_buffer; - } - - /* Check if this is a boot record */ - - ret = fat_checkbootrecord(fs); - if (ret == OK) - { - /* Break out of the loop if a valid boot record is found */ - - fvdbg("MBR found in partition %d\n", i); - break; - } - - /* Re-read sector 0 so that we can check the next partition */ - - fvdbg("Partition %d is not an MBR\n", i); - ret = fat_hwread(fs, fs->fs_buffer, 0, 1); - if (ret < 0) - { - goto errout_with_buffer; - } - } - - if (i > 3) - { - fdbg("No valid MBR\n"); - goto errout_with_buffer; - } - } - - /* We have what appears to be a valid FAT filesystem! Now read the - * FSINFO sector (FAT32 only) - */ - - if (fs->fs_type == FSTYPE_FAT32) - { - ret = fat_checkfsinfo(fs); - if (ret != OK) - { - goto errout_with_buffer; - } - } - - /* We did it! */ - - fdbg("FAT%d:\n", fs->fs_type == 0 ? 12 : fs->fs_type == 1 ? 16 : 32); - fdbg("\tHW sector size: %d\n", fs->fs_hwsectorsize); - fdbg("\t sectors: %d\n", fs->fs_hwnsectors); - fdbg("\tFAT reserved: %d\n", fs->fs_fatresvdseccount); - fdbg("\t sectors: %d\n", fs->fs_fattotsec); - fdbg("\t start sector: %d\n", fs->fs_fatbase); - fdbg("\t root sector: %d\n", fs->fs_rootbase); - fdbg("\t root entries: %d\n", fs->fs_rootentcnt); - fdbg("\t data sector: %d\n", fs->fs_database); - fdbg("\t FSINFO sector: %d\n", fs->fs_fsinfo); - fdbg("\t Num FATs: %d\n", fs->fs_fatnumfats); - fdbg("\t FAT sectors: %d\n", fs->fs_nfatsects); - fdbg("\t sectors/cluster: %d\n", fs->fs_fatsecperclus); - fdbg("\t max clusters: %d\n", fs->fs_nclusters); - fdbg("\tFSI free count %d\n", fs->fs_fsifreecount); - fdbg("\t next free %d\n", fs->fs_fsinextfree); - - return OK; - - errout_with_buffer: - fat_io_free(fs->fs_buffer, fs->fs_hwsectorsize); - fs->fs_buffer = 0; - errout: - fs->fs_mounted = false; - return ret; -} - -/**************************************************************************** - * Name: fat_checkmount - * - * Desciption: Check if the mountpoint is still valid. - * - * The caller should hold the mountpoint semaphore - * - ****************************************************************************/ - -int fat_checkmount(struct fat_mountpt_s *fs) -{ - /* If the fs_mounted flag is false, then we have already handled the loss - * of the mount. - */ - - if (fs && fs->fs_mounted) - { - /* We still think the mount is healthy. Check an see if this is - * still the case - */ - - if (fs->fs_blkdriver) - { - struct inode *inode = fs->fs_blkdriver; - if (inode && inode->u.i_bops && inode->u.i_bops->geometry) - { - struct geometry geo; - int errcode = inode->u.i_bops->geometry(inode, &geo); - if (errcode == OK && geo.geo_available && !geo.geo_mediachanged) - { - return OK; - } - } - } - - /* If we get here, the mount is NOT healthy */ - - fs->fs_mounted = false; - } - - return -ENODEV; -} - -/**************************************************************************** - * Name: fat_hwread - * - * Desciption: Read the specified sector into the sector buffer - * - ****************************************************************************/ - -int fat_hwread(struct fat_mountpt_s *fs, uint8_t *buffer, off_t sector, - unsigned int nsectors) -{ - int ret = -ENODEV; - if (fs && fs->fs_blkdriver ) - { - struct inode *inode = fs->fs_blkdriver; - if (inode && inode->u.i_bops && inode->u.i_bops->read) - { - ssize_t nSectorsRead = inode->u.i_bops->read(inode, buffer, - sector, nsectors); - if (nSectorsRead == nsectors) - { - ret = OK; - } - else if (nSectorsRead < 0) - { - ret = nSectorsRead; - } - } - } - return ret; -} - -/**************************************************************************** - * Name: fat_hwwrite - * - * Desciption: Write the sector buffer to the specified sector - * - ****************************************************************************/ - -int fat_hwwrite(struct fat_mountpt_s *fs, uint8_t *buffer, off_t sector, - unsigned int nsectors) -{ - int ret = -ENODEV; - if (fs && fs->fs_blkdriver ) - { - struct inode *inode = fs->fs_blkdriver; - if (inode && inode->u.i_bops && inode->u.i_bops->write) - { - ssize_t nSectorsWritten = - inode->u.i_bops->write(inode, buffer, sector, nsectors); - - if (nSectorsWritten == nsectors) - { - ret = OK; - } - else if (nSectorsWritten < 0) - { - ret = nSectorsWritten; - } - } - } - return ret; -} - -/**************************************************************************** - * Name: fat_cluster2sector - * - * Desciption: Convert a cluster number to a start sector number - * - ****************************************************************************/ - -off_t fat_cluster2sector(struct fat_mountpt_s *fs, uint32_t cluster ) -{ - cluster -= 2; - if (cluster >= fs->fs_nclusters - 2) - { - return -EINVAL; - } - return cluster * fs->fs_fatsecperclus + fs->fs_database; -} - -/**************************************************************************** - * Name: fat_getcluster - * - * Desciption: Get the next cluster start from the FAT. - * - * Return: <0: error, 0:cluster unassigned, >=0: start sector of cluster - * - ****************************************************************************/ - -off_t fat_getcluster(struct fat_mountpt_s *fs, uint32_t clusterno) -{ - /* Verify that the cluster number is within range */ - - if (clusterno >= 2 && clusterno < fs->fs_nclusters) - { - /* Okay.. Read the next cluster from the FAT. The way we will do - * this depends on the type of FAT filesystm we are dealing with. - */ - - switch (fs->fs_type) - { - case FSTYPE_FAT12 : - { - off_t fatsector; - unsigned int fatoffset; - unsigned int cluster; - unsigned int fatindex; - - /* FAT12 is more complex because it has 12-bits (1.5 bytes) - * per FAT entry. Get the offset to the first byte: - */ - - fatoffset = (clusterno * 3) / 2; - fatsector = fs->fs_fatbase + SEC_NSECTORS(fs, fatoffset); - - /* Read the sector at this offset */ - - if (fat_fscacheread(fs, fatsector) < 0) - { - /* Read error */ - - break; - } - - /* Get the first, LS byte of the cluster from the FAT */ - - fatindex = fatoffset & SEC_NDXMASK(fs); - cluster = fs->fs_buffer[fatindex]; - - /* With FAT12, the second byte of the cluster number may lie in - * a different sector than the first byte. - */ - - fatindex++; - if (fatindex >= fs->fs_hwsectorsize) - { - fatsector++; - fatindex = 0; - - if (fat_fscacheread(fs, fatsector) < 0) - { - /* Read error */ - - break; - } - } - - /* Get the second, MS byte of the cluster for 16-bits. The - * does not depend on the endian-ness of the target, but only - * on the fact that the byte stream is little-endian. - */ - - cluster |= (unsigned int)fs->fs_buffer[fatindex] << 8; - - /* Now, pick out the correct 12 bit cluster start sector value */ - - if ((clusterno & 1) != 0) - { - /* Odd.. take the MS 12-bits */ - - cluster >>= 4; - } - else - { - /* Even.. take the LS 12-bits */ - - cluster &= 0x0fff; - } - return cluster; - } - - case FSTYPE_FAT16 : - { - unsigned int fatoffset = 2 * clusterno; - off_t fatsector = fs->fs_fatbase + SEC_NSECTORS(fs, fatoffset); - unsigned int fatindex = fatoffset & SEC_NDXMASK(fs); - - if (fat_fscacheread(fs, fatsector) < 0) - { - /* Read error */ - break; - } - return FAT_GETFAT16(fs->fs_buffer, fatindex); - } - - case FSTYPE_FAT32 : - { - unsigned int fatoffset = 4 * clusterno; - off_t fatsector = fs->fs_fatbase + SEC_NSECTORS(fs, fatoffset); - unsigned int fatindex = fatoffset & SEC_NDXMASK(fs); - - if (fat_fscacheread(fs, fatsector) < 0) - { - /* Read error */ - break; - } - return FAT_GETFAT32(fs->fs_buffer, fatindex) & 0x0fffffff; - } - default: - break; - } - } - - /* There is no cluster information, or an error occured */ - - return (off_t)-EINVAL; -} - -/**************************************************************************** - * Name: fat_putcluster - * - * Desciption: Write a new cluster into the FAT - * - ****************************************************************************/ - -int fat_putcluster(struct fat_mountpt_s *fs, uint32_t clusterno, off_t nextcluster) -{ - /* Verify that the cluster number is within range. Zero erases the cluster. */ - - if (clusterno == 0 || (clusterno >= 2 && clusterno < fs->fs_nclusters)) - { - /* Okay.. Write the next cluster into the FAT. The way we will do - * this depends on the type of FAT filesystm we are dealing with. - */ - - switch (fs->fs_type) - { - case FSTYPE_FAT12 : - { - off_t fatsector; - unsigned int fatoffset; - unsigned int fatindex; - uint8_t value; - - /* FAT12 is more complex because it has 12-bits (1.5 bytes) - * per FAT entry. Get the offset to the first byte: - */ - - fatoffset = (clusterno * 3) / 2; - fatsector = fs->fs_fatbase + SEC_NSECTORS(fs, fatoffset); - - /* Make sure that the sector at this offset is in the cache */ - - if (fat_fscacheread(fs, fatsector)< 0) - { - /* Read error */ - - break; - } - - /* Get the LS byte first handling the 12-bit alignment within - * the 16-bits - */ - - fatindex = fatoffset & SEC_NDXMASK(fs); - if ((clusterno & 1) != 0) - { - /* Save the LS four bits of the next cluster */ - - value = (fs->fs_buffer[fatindex] & 0x0f) | nextcluster << 4; - } - else - { - /* Save the LS eight bits of the next cluster */ - - value = (uint8_t)nextcluster; - } - - fs->fs_buffer[fatindex] = value; - - /* With FAT12, the second byte of the cluster number may lie in - * a different sector than the first byte. - */ - - fatindex++; - if (fatindex >= fs->fs_hwsectorsize) - { - /* Read the next sector */ - - fatsector++; - fatindex = 0; - - /* Set the dirty flag to make sure the sector that we - * just modified is written out. - */ - - fs->fs_dirty = true; - if (fat_fscacheread(fs, fatsector) < 0) - { - /* Read error */ - - break; - } - } - - /* Output the MS byte first handling the 12-bit alignment within - * the 16-bits - */ - - if ((clusterno & 1) != 0) - { - /* Save the MS eight bits of the next cluster */ - - value = (uint8_t)(nextcluster >> 4); - } - else - { - /* Save the MS four bits of the next cluster */ - - value = (fs->fs_buffer[fatindex] & 0xf0) | ((nextcluster >> 8) & 0x0f); - } - - fs->fs_buffer[fatindex] = value; - } - break; - - case FSTYPE_FAT16 : - { - unsigned int fatoffset = 2 * clusterno; - off_t fatsector = fs->fs_fatbase + SEC_NSECTORS(fs, fatoffset); - unsigned int fatindex = fatoffset & SEC_NDXMASK(fs); - - if (fat_fscacheread(fs, fatsector) < 0) - { - /* Read error */ - - break; - } - FAT_PUTFAT16(fs->fs_buffer, fatindex, nextcluster & 0xffff); - } - break; - - case FSTYPE_FAT32 : - { - unsigned int fatoffset = 4 * clusterno; - off_t fatsector = fs->fs_fatbase + SEC_NSECTORS(fs, fatoffset); - unsigned int fatindex = fatoffset & SEC_NDXMASK(fs); - - if (fat_fscacheread(fs, fatsector) < 0) - { - /* Read error */ - - break; - } - FAT_PUTFAT32(fs->fs_buffer, fatindex, nextcluster & 0x0fffffff); - } - break; - - default: - return -EINVAL; - } - - /* Mark the modified sector as "dirty" and return success */ - - fs->fs_dirty = true; - return OK; - } - - return -EINVAL; -} - -/**************************************************************************** - * Name: fat_removechain - * - * Desciption: Remove an entire chain of clusters, starting with 'cluster' - * - ****************************************************************************/ - -int fat_removechain(struct fat_mountpt_s *fs, uint32_t cluster) -{ - int32_t nextcluster; - int ret; - - /* Loop while there are clusters in the chain */ - - while (cluster >= 2 && cluster < fs->fs_nclusters) - { - /* Get the next cluster after the current one */ - - nextcluster = fat_getcluster(fs, cluster); - if (nextcluster < 0) - { - /* Error! */ - return nextcluster; - } - - /* Then nullify current cluster -- removing it from the chain */ - - ret = fat_putcluster(fs, cluster, 0); - if (ret < 0) - { - return ret; - } - - /* Update FSINFINFO data */ - - if (fs->fs_fsifreecount != 0xffffffff) - { - fs->fs_fsifreecount++; - fs->fs_fsidirty = 1; - } - - /* Then set up to remove the next cluster */ - - cluster = nextcluster; - } - - return OK; -} - -/**************************************************************************** - * Name: fat_extendchain - * - * Desciption: Add a new cluster to the chain following cluster (if cluster - * is non-NULL). if cluster is zero, then a new chain is created. - * - * Return: <0:error, 0: no free cluster, >=2: new cluster number - * - ****************************************************************************/ - -int32_t fat_extendchain(struct fat_mountpt_s *fs, uint32_t cluster) -{ - off_t startsector; - uint32_t newcluster; - uint32_t startcluster; - int ret; - - /* The special value 0 is used when the new chain should start */ - - if (cluster == 0) - { - /* The FSINFO NextFree entry should be a good starting point - * in the search for a new cluster - */ - - startcluster = fs->fs_fsinextfree; - if (startcluster == 0 || startcluster >= fs->fs_nclusters) - { - /* But it is bad.. we have to start at the beginning */ - startcluster = 1; - } - } - else - { - /* We are extending an existing chain. Verify that this - * is a valid cluster by examining its start sector. - */ - - startsector = fat_getcluster(fs, cluster); - if (startsector < 0) - { - /* An error occurred, return the error value */ - - return startsector; - } - else if (startsector < 2) - { - /* Oops.. this cluster does not exist. */ - - return 0; - } - else if (startsector < fs->fs_nclusters) - { - /* It is already followed by next cluster */ - - return startsector; - } - - /* Okay.. it checks out */ - - startcluster = cluster; - } - - /* Loop until (1) we discover that there are not free clusters - * (return 0), an errors occurs (return -errno), or (3) we find - * the next cluster (return the new cluster number). - */ - - newcluster = startcluster; - for (;;) - { - /* Examine the next cluster in the FAT */ - - newcluster++; - if (newcluster >= fs->fs_nclusters) - { - /* If we hit the end of the available clusters, then - * wrap back to the beginning because we might have - * started at a non-optimal place. But don't continue - * past the start cluster. - */ - - newcluster = 2; - if (newcluster > startcluster) - { - /* We are back past the starting cluster, then there - * is no free cluster. - */ - - return 0; - } - } - - /* We have a candidate cluster. Check if the cluster number is - * mapped to a group of sectors. - */ - - startsector = fat_getcluster(fs, newcluster); - if (startsector == 0) - { - /* Found have found a free cluster break out*/ - break; - } - else if (startsector < 0) - { - /* Some error occurred, return the error number */ - return startsector; - } - - /* We wrap all the back to the starting cluster? If so, then - * there are no free clusters. - */ - - if (newcluster == startcluster) - { - return 0; - } - } - - /* We get here only if we break out with an available cluster - * number in 'newcluster' Now mark that cluster as in-use. - */ - - ret = fat_putcluster(fs, newcluster, 0x0fffffff); - if (ret < 0) - { - /* An error occurred */ - return ret; - } - - /* And link if to the start cluster (if any)*/ - - if (cluster) - { - /* There is a start cluster -- link it */ - - ret = fat_putcluster(fs, cluster, newcluster); - if (ret < 0) - { - return ret; - } - } - - /* And update the FINSINFO for the next time we have to search */ - - fs->fs_fsinextfree = newcluster; - if (fs->fs_fsifreecount != 0xffffffff) - { - fs->fs_fsifreecount--; - fs->fs_fsidirty = 1; - } - - /* Return then number of the new cluster that was added to the chain */ - - return newcluster; -} - -/**************************************************************************** - * Name: fat_nextdirentry - * - * Desciption: Read the next directory entry from the sector in cache, - * reading the next sector(s) in the cluster as necessary. This function - * must return -ENOSPC if if fails because there are no further entries - * available in the directory. - * - ****************************************************************************/ - -int fat_nextdirentry(struct fat_mountpt_s *fs, struct fs_fatdir_s *dir) -{ - unsigned int cluster; - unsigned int ndx; - - /* Increment the index to the next 32-byte directory entry */ - - ndx = dir->fd_index + 1; - - /* Check if all of the directory entries in this sectory have - * been examined. - */ - - if ((ndx & (DIRSEC_NDIRS(fs)-1)) == 0) - { - /* Yes, then we will have to read the next sector */ - - dir->fd_currsector++; - - /* For FAT12/16, the root directory is a group of sectors relative - * to the first sector of the fat volume. - */ - - if (!dir->fd_currcluster) - { - /* For FAT12/16, the boot record tells us number of 32-bit directories - * that are contained in the root directory. This should correspond to - * an even number of sectors. - */ - - if (ndx >= fs->fs_rootentcnt) - { - /* When we index past this count, we have examined all of the entries in - * the root directory. - */ - - return -ENOSPC; - } - } - else - { - /* Not a FAT12/16 root directory, check if we have examined the entire - * cluster comprising the directory. - * - * The current sector within the cluster is the entry number divided - * byte the number of entries per sector - */ - - int sector = ndx / DIRSEC_NDIRS(fs); - - /* We are finished with the cluster when the last sector of the cluster - * has been examined. - */ - - if ((sector & (fs->fs_fatsecperclus-1)) == 0) - { - /* Get next cluster */ - - cluster = fat_getcluster(fs, dir->fd_currcluster); - - /* Check if a valid cluster was obtained. */ - - if (cluster < 2 || cluster >= fs->fs_nclusters) - { - /* No, we have probably reached the end of the cluster list */ - - return -ENOSPC; - } - - /* Initialize for new cluster */ - - dir->fd_currcluster = cluster; - dir->fd_currsector = fat_cluster2sector(fs, cluster); - } - } - } - - /* Save the new index into dir->fd_currsector */ - - dir->fd_index = ndx; - return OK; -} - -/**************************************************************************** - * Name: fat_dirtruncate - * - * Desciption: Truncate an existing file to zero length - * - * Assumptions: The caller holds mountpoint semaphore, fs_buffer holds - * the directory entry, the directory entry sector (fd_sector) is - * currently in the sector cache. - * - ****************************************************************************/ - -int fat_dirtruncate(struct fat_mountpt_s *fs, struct fat_dirinfo_s *dirinfo) -{ - unsigned int startcluster; - uint32_t writetime; - uint8_t *direntry; - off_t savesector; - int ret; - - /* Get start cluster of the file to truncate */ - - direntry = &fs->fs_buffer[dirinfo->fd_seq.ds_offset]; - startcluster = - ((uint32_t)DIR_GETFSTCLUSTHI(direntry) << 16) | - DIR_GETFSTCLUSTLO(direntry); - - /* Clear the cluster start value in the directory and set the file size - * to zero. This makes the file look empty but also have to dispose of - * all of the clusters in the chain. - */ - - DIR_PUTFSTCLUSTHI(direntry, 0); - DIR_PUTFSTCLUSTLO(direntry, 0); - DIR_PUTFILESIZE(direntry, 0); - - /* Set the ARCHIVE attribute and update the write time */ - - DIR_PUTATTRIBUTES(direntry, FATATTR_ARCHIVE); - - writetime = fat_systime2fattime(); - DIR_PUTWRTTIME(direntry, writetime & 0xffff); - DIR_PUTWRTDATE(direntry, writetime > 16); - - /* This sector needs to be written back to disk eventually */ - - fs->fs_dirty = true; - - /* Now remove the entire cluster chain comprising the file */ - - savesector = fs->fs_currentsector; - ret = fat_removechain(fs, startcluster); - if (ret < 0) - { - return ret; - } - - /* Setup FSINFO to resuse this cluster next */ - - fs->fs_fsinextfree = startcluster - 1; - - /* Make sure that the directory is still in the cache */ - - return fat_fscacheread(fs, savesector); -} - -/**************************************************************************** - * Name: fat_fscacheflush - * - * Desciption: Flush any dirty sector if fs_buffer as necessary - * - ****************************************************************************/ - -int fat_fscacheflush(struct fat_mountpt_s *fs) -{ - int ret; - - /* Check if the fs_buffer is dirty. In this case, we will write back the - * contents of fs_buffer. - */ - - if (fs->fs_dirty) - { - /* Write the dirty sector */ - - ret = fat_hwwrite(fs, fs->fs_buffer, fs->fs_currentsector, 1); - if (ret < 0) - { - return ret; - } - - /* Does the sector lie in the FAT region? */ - - if (fs->fs_currentsector >= fs->fs_fatbase && - fs->fs_currentsector < fs->fs_fatbase + fs->fs_nfatsects) - { - /* Yes, then make the change in the FAT copy as well */ - int i; - - for (i = fs->fs_fatnumfats; i >= 2; i--) - { - fs->fs_currentsector += fs->fs_nfatsects; - ret = fat_hwwrite(fs, fs->fs_buffer, fs->fs_currentsector, 1); - if (ret < 0) - { - return ret; - } - } - } - - /* No longer dirty */ - - fs->fs_dirty = false; - } - return OK; -} - -/**************************************************************************** - * Name: fat_fscacheread - * - * Desciption: Read the specified sector into the sector cache, flushing any - * existing dirty sectors as necessary. - * - ****************************************************************************/ - -int fat_fscacheread(struct fat_mountpt_s *fs, off_t sector) -{ - int ret; - - /* fs->fs_currentsector holds the current sector that is buffered in - * fs->fs_buffer. If the requested sector is the same as this sector, then - * we do nothing. Otherwise, we will have to read the new sector. - */ - - if (fs->fs_currentsector != sector) - { - /* We will need to read the new sector. First, flush the cached - * sector if it is dirty. - */ - - ret = fat_fscacheflush(fs); - if (ret < 0) - { - return ret; - } - - /* Then read the specified sector into the cache */ - - ret = fat_hwread(fs, fs->fs_buffer, sector, 1); - if (ret < 0) - { - return ret; - } - - /* Update the cached sector number */ - - fs->fs_currentsector = sector; - } - - return OK; -} - -/**************************************************************************** - * Name: fat_ffcacheflush - * - * Desciption: Flush any dirty sectors as necessary - * - ****************************************************************************/ - -int fat_ffcacheflush(struct fat_mountpt_s *fs, struct fat_file_s *ff) -{ - int ret; - - /* Check if the ff_buffer is dirty. In this case, we will write back the - * contents of ff_buffer. - */ - - if (ff->ff_cachesector && - (ff->ff_bflags & (FFBUFF_DIRTY|FFBUFF_VALID)) == (FFBUFF_DIRTY|FFBUFF_VALID)) - { - /* Write the dirty sector */ - - ret = fat_hwwrite(fs, ff->ff_buffer, ff->ff_cachesector, 1); - if (ret < 0) - { - return ret; - } - - /* No longer dirty, but still valid */ - - ff->ff_bflags &= ~FFBUFF_DIRTY; - } - - return OK; -} - -/**************************************************************************** - * Name: fat_ffcacheread - * - * Desciption: Read the specified sector into the sector cache, flushing any - * existing dirty sectors as necessary. - * - ****************************************************************************/ - -int fat_ffcacheread(struct fat_mountpt_s *fs, struct fat_file_s *ff, off_t sector) -{ - int ret; - - /* ff->ff_cachesector holds the current sector that is buffered in - * ff->ff_buffer. If the requested sector is the same as this sector, then - * we do nothing. Otherwise, we will have to read the new sector. - */ - - if (ff->ff_cachesector != sector || (ff->ff_bflags & FFBUFF_VALID) == 0) - { - /* We will need to read the new sector. First, flush the cached - * sector if it is dirty. - */ - - ret = fat_ffcacheflush(fs, ff); - if (ret < 0) - { - return ret; - } - - /* Then read the specified sector into the cache */ - - ret = fat_hwread(fs, ff->ff_buffer, sector, 1); - if (ret < 0) - { - return ret; - } - - /* Update the cached sector number */ - - ff->ff_cachesector = sector; - ff->ff_bflags |= FFBUFF_VALID; - } - return OK; -} - -/**************************************************************************** - * Name: fat_ffcacheread - * - * Desciption: Invalidate the current file buffer contents - * - ****************************************************************************/ - -int fat_ffcacheinvalidate(struct fat_mountpt_s *fs, struct fat_file_s *ff) -{ - int ret; - - /* Is there anything valid in the buffer now? */ - - if ((ff->ff_bflags & FFBUFF_VALID) != 0) - { - /* We will invalidate the buffered sector */ - - ret = fat_ffcacheflush(fs, ff); - if (ret < 0) - { - return ret; - } - - /* Then discard the current cache contents */ - - ff->ff_bflags &= ~FFBUFF_VALID; - ff->ff_cachesector = 0; - } - return OK; -} - -/**************************************************************************** - * Name: fat_updatefsinfo - * - * Desciption: Flush evertyhing buffered for the mountpoint and update - * the FSINFO sector, if appropriate - * - ****************************************************************************/ - -int fat_updatefsinfo(struct fat_mountpt_s *fs) -{ - int ret; - - /* Flush the fs_buffer if it is dirty */ - - ret = fat_fscacheflush(fs); - if (ret == OK) - { - /* The FSINFO sector only has to be update for the case of a FAT32 file - * system. Check if the file system type.. If this is a FAT32 file - * system then the fs_fsidirty flag will indicate if the FSINFO sector - * needs to be re-written. - */ - - if (fs->fs_type == FSTYPE_FAT32 && fs->fs_fsidirty) - { - /* Create an image of the FSINFO sector in the fs_buffer */ - - memset(fs->fs_buffer, 0, fs->fs_hwsectorsize); - FSI_PUTLEADSIG(fs->fs_buffer, 0x41615252); - FSI_PUTSTRUCTSIG(fs->fs_buffer, 0x61417272); - FSI_PUTFREECOUNT(fs->fs_buffer, fs->fs_fsifreecount); - FSI_PUTNXTFREE(fs->fs_buffer, fs->fs_fsinextfree); - FSI_PUTTRAILSIG(fs->fs_buffer, BOOT_SIGNATURE32); - - /* Then flush this to disk */ - - fs->fs_currentsector = fs->fs_fsinfo; - fs->fs_dirty = true; - ret = fat_fscacheflush(fs); - - /* No longer dirty */ - - fs->fs_fsidirty = false; - } - } - return ret; -} - -/**************************************************************************** - * Name: fat_nfreeclusters - * - * Desciption: Get the number of free clusters - * - ****************************************************************************/ - -int fat_nfreeclusters(struct fat_mountpt_s *fs, off_t *pfreeclusters) -{ - uint32_t nfreeclusters; - - /* If number of the first free cluster is valid, then just return that value. */ - - if (fs->fs_fsifreecount <= fs->fs_nclusters - 2) - { - *pfreeclusters = fs->fs_fsifreecount; - return OK; - } - - /* Otherwise, we will have to count the number of free clusters */ - - nfreeclusters = 0; - if (fs->fs_type == FSTYPE_FAT12) - { - off_t sector; - - /* Examine every cluster in the fat */ - - for (sector = 2; sector < fs->fs_nclusters; sector++) - { - - /* If the cluster is unassigned, then increment the count of free clusters */ - - if ((uint16_t)fat_getcluster(fs, sector) == 0) - { - nfreeclusters++; - } - } - } - else - { - unsigned int cluster; - off_t fatsector; - unsigned int offset; - int ret; - - fatsector = fs->fs_fatbase; - offset = fs->fs_hwsectorsize; - - /* Examine each cluster in the fat */ - - for (cluster = fs->fs_nclusters; cluster > 0; cluster--) - { - /* If we are starting a new sector, then read the new sector in fs_buffer */ - - if (offset >= fs->fs_hwsectorsize) - { - ret = fat_fscacheread(fs, fatsector++); - if (ret < 0) - { - return ret; - } - - /* Reset the offset to the next FAT entry. - * Increment the sector number to read next time around. - */ - - offset = 0; - fatsector++; - } - - /* FAT16 and FAT32 differ only on the size of each cluster start - * sector number in the FAT. - */ - - if (fs->fs_type == FSTYPE_FAT16) - { - if (FAT_GETFAT16(fs->fs_buffer, offset) == 0) - { - nfreeclusters++; - } - offset += 2; - } - else - { - if (FAT_GETFAT32(fs->fs_buffer, offset) == 0) - { - nfreeclusters++; - } - - offset += 4; - } - } - } - - fs->fs_fsifreecount = nfreeclusters; - if (fs->fs_type == FSTYPE_FAT32) - { - fs->fs_fsidirty = true; - } - - *pfreeclusters = nfreeclusters; - return OK; -} - -/**************************************************************************** - * Name: fat_nfreeclusters - * - * Desciption: - * Given the file position, set the correct current sector to access. - * - ****************************************************************************/ - -int fat_currentsector(struct fat_mountpt_s *fs, struct fat_file_s *ff, - off_t position) -{ - int sectoroffset; - - if (position <= ff->ff_size ) - { - /* sectoroffset is the sector number offset into the current cluster */ - - sectoroffset = SEC_NSECTORS(fs, position) & CLUS_NDXMASK(fs); - - /* The current cluster is the first sector of the cluster plus - * the sector offset - */ - - ff->ff_currentsector = fat_cluster2sector(fs, ff->ff_currentcluster) - + sectoroffset; - - /* The remainder is the number of sectors left in the cluster to be - * read/written - */ - - ff->ff_sectorsincluster = fs->fs_fatsecperclus - sectoroffset; - - fvdbg("position=%d currentsector=%d sectorsincluster=%d\n", - position, ff->ff_currentsector, ff->ff_sectorsincluster); - - return OK; - } - - /* The position does not lie within the file */ - - return -ENOSPC; -} - - diff --git a/nuttx/fs/fat/fs_mkfatfs.c b/nuttx/fs/fat/fs_mkfatfs.c deleted file mode 100644 index 384aa9356..000000000 --- a/nuttx/fs/fat/fs_mkfatfs.c +++ /dev/null @@ -1,312 +0,0 @@ -/**************************************************************************** - * fs/fat/fs_writefat.c - * - * Copyright (C) 2008-2009 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt <gnutt@nuttx.org> - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name NuttX nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <nuttx/config.h> - -#include <stdint.h> -#include <stdlib.h> -#include <string.h> -#include <debug.h> -#include <errno.h> - -#include <nuttx/fs/fs.h> -#include <nuttx/fs/fat.h> -#include <nuttx/fs/mkfatfs.h> - -#include "fs_internal.h" -#include "fs_fat32.h" -#include "fs_mkfatfs.h" - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: mkfatfs_getgeometry - * - * Description: - * Get the sector size and number of sectors of the underlying block - * device. - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ - -static inline int mkfatfs_getgeometry(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var) -{ - struct geometry geometry; - int ret; - - /* Get the device geometry */ - - ret = DEV_GEOMETRY(geometry); - if (ret < 0) - { - fdbg("geometry() returned %d\n", ret); - return ret; - } - - if (!geometry.geo_available || !geometry.geo_writeenabled) - { - fdbg("Media is not available\n", ret); - return -ENODEV; - } - - /* Check if the user provided maxblocks was provided and, if so, that is it less than - * the actual number of blocks on the device. - */ - - if (fmt->ff_nsectors != 0) - { - if (fmt->ff_nsectors > geometry.geo_nsectors) - { - fdbg("User maxblocks (%d) exceeds blocks on device (%d)\n", - fmt->ff_nsectors, geometry.geo_nsectors); - return -EINVAL; - } - } - else - { - /* Use the actual number of blocks on the device */ - - fmt->ff_nsectors = geometry.geo_nsectors; - } - - /* Verify that we can handle this sector size */ - - var->fv_sectorsize = geometry.geo_sectorsize; - switch (var->fv_sectorsize) - { - case 512: - var->fv_sectshift = 9; - break; - - case 1024: - var->fv_sectshift = 10; - break; - - case 2048: - var->fv_sectshift = 11; - break; - - case 4096: - var->fv_sectshift = 12; - break; - - default: - fdbg("Unsupported sector size: %d\n", var->fv_sectorsize); - return -EPERM; - } - return 0; -} - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: mkfatfs - * - * Description: - * Make a FAT file system image on the specified block device - * - * Inputs: - * pathname - the full path to a registered block driver - * fmt - Describes characteristics of the desired filesystem - * - * Return: - * Zero (OK) on success; -1 (ERROR) on failure with errno set appropriately: - * - * EINVAL - NULL block driver string, bad number of FATS in 'fmt', bad FAT - * size in 'fmt', bad cluster size in 'fmt' - * ENOENT - 'pathname' does not refer to anything in the filesystem. - * ENOTBLK - 'pathname' does not refer to a block driver - * EACCES - block driver does not support wrie or geometry methods - * - * Assumptions: - * - The caller must assure that the block driver is not mounted and not in - * use when this function is called. The result of formatting a mounted - * device is indeterminate (but likely not good). - * - ****************************************************************************/ -int mkfatfs(FAR const char *pathname, FAR struct fat_format_s *fmt) -{ - struct fat_var_s var; - int ret; - - /* Initialize */ - - memset(&var, 0, sizeof(struct fat_var_s)); - - /* Get the filesystem creation time */ - - var.fv_createtime = fat_systime2fattime(); - - /* Verify format options (only when DEBUG enabled) */ - -#ifdef CONFIG_DEBUG - if (!pathname) - { - fdbg("No block driver path\n"); - ret = -EINVAL; - goto errout; - } - - if (fmt->ff_nfats < 1 || fmt->ff_nfats > 4) - { - fdbg("Invalid number of fats: %d\n", fmt->ff_nfats); - ret = -EINVAL; - goto errout; - } - - if (fmt->ff_fattype != 0 && fmt->ff_fattype != 12 && - fmt->ff_fattype != 16 && fmt->ff_fattype != 32) - { - fdbg("Invalid FAT size: %d\n", fmt->ff_fattype); - ret = -EINVAL; - goto errout; - } -#endif - var.fv_fattype = fmt->ff_fattype; - - /* The valid range off ff_clustshift is {0,1,..7} corresponding to - * cluster sizes of {1,2,..128} sectors. The special value of 0xff - * means that we should autoselect the cluster sizel. - */ -#ifdef CONFIG_DEBUG - if (fmt->ff_clustshift > 7 && fmt->ff_clustshift != 0xff) - { - fdbg("Invalid cluster shift value: %d\n", fmt->ff_clustshift); - ret = -EINVAL; - goto errout; - } - - if (fmt->ff_rootdirentries != 0 && (fmt->ff_rootdirentries < 16 || fmt->ff_rootdirentries > 32767)) - { - fdbg("Invalid number of root dir entries: %d\n", fmt->ff_rootdirentries); - ret = -EINVAL; - goto errout; - } - - if (fmt->ff_rsvdseccount != 0 && (fmt->ff_rsvdseccount < 1 || fmt->ff_rsvdseccount > 32767)) - { - fdbg("Invalid number of reserved sectors: %d\n", fmt->ff_rsvdseccount); - ret = -EINVAL; - goto errout; - } -#endif - - /* Find the inode of the block driver indentified by 'source' */ - - ret = open_blockdriver(pathname, 0, &var.fv_inode); - if (ret < 0) - { - fdbg("Failed to open %s\n", pathname); - goto errout; - } - - /* Make sure that the inode supports the write and geometry methods at a minimum */ - - if (!var.fv_inode->u.i_bops->write || !var.fv_inode->u.i_bops->geometry) - { - fdbg("%s does not support write or geometry methods\n", pathname); - ret = -EACCES; - goto errout_with_driver; - } - - /* Determine the volume configuration based upon the input values and upon the - * reported device geometry. - */ - - ret = mkfatfs_getgeometry(fmt, &var); - if (ret < 0) - { - goto errout_with_driver; - } - - /* Configure the file system */ - - ret = mkfatfs_configfatfs(fmt, &var); - if (ret < 0) - { - goto errout_with_driver; - } - - /* Allocate a buffer that will be working sector memory */ - - var.fv_sect = (uint8_t*)malloc(var.fv_sectorsize); - if (!var.fv_sect) - { - fdbg("Failed to allocate working buffers\n"); - goto errout_with_driver; - } - - /* Write the filesystem to media */ - - ret = mkfatfs_writefatfs(fmt, &var); - -errout_with_driver: - /* Close the driver */ - - (void)close_blockdriver(var.fv_inode); - -errout: - /* Release all allocated memory */ - - if (var.fv_sect) - { - free(var.fv_sect); - } - - /* Return any reported errors */ - - if (ret < 0) - { - errno = -ret; - return ERROR; - } - return OK; -} diff --git a/nuttx/fs/fat/fs_mkfatfs.h b/nuttx/fs/fat/fs_mkfatfs.h deleted file mode 100644 index 05801c92d..000000000 --- a/nuttx/fs/fat/fs_mkfatfs.h +++ /dev/null @@ -1,170 +0,0 @@ -/**************************************************************************** - * fs/fat/fs_mkfat.h - * - * Copyright (C) 2008-2009 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt <gnutt@nuttx.org> - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name NuttX nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -#ifndef __FS_FAT_FS_MKATFS_H -#define __FS_FAT_FS_MKATFS_H - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <nuttx/config.h> -#include <stdint.h> - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/* Only the "hard drive" media type is used */ - -#define FAT_DEFAULT_MEDIA_TYPE 0xf8 - -/* Default hard driver geometry */ - -#define FAT_DEFAULT_SECPERTRK 63 -#define FAT_DEFAULT_NUMHEADS 255 - -/* FSINFO is always at this sector */ - -#define FAT_DEFAULT_FSINFO_SECTOR 1 - -/* FAT32 foot cluster number */ - -#define FAT32_DEFAULT_ROOT_CLUSTER 2 - -/* Macros to simplify direct block driver access */ - -#define DEV_OPEN() \ - var->fb_inode->u.i_bops->open ? \ - var->fv_inode->u.i_bops->open(var->fv_inode) : \ - 0 -#define DEV_CLOSE() \ - var->fb_inode->u.i_bops->close ? \ - var->fv_inode->u.i_bops->close(var->fv_inode) : \ - 0 -#define DEV_READ(buf, sect, nsect) \ - var->fv_inode->u.i_bops->read(var->fv_inode, buf, sect, nsect) -#define DEV_WRITE(buf, sect, nsect) \ - var->fv_inode->u.i_bops->write(var->fv_inode, buf, sect, nsect) -#define DEV_GEOMETRY(geo) \ - var->fv_inode->u.i_bops->geometry(var->fv_inode, &geo) -#define DEV_IOCTL(cmd, arg) \ - var->fv_inode->u.i_bops->ioctl(var->fv_inode, cmd, arg) - -/**************************************************************************** - * Public Types - ****************************************************************************/ - -/* This structure (plus the user-provided struct fat_format_s) describes - * the format FAT file system. All "global" variables used in the format - * logic are contained in this structure so that is possible to format two - * block devices concurrently. - */ - -struct fat_var_s -{ - struct inode *fv_inode; /* The block driver "handle" */ - uint8_t fv_jump[3]; /* 3-byte boot jump instruction */ - uint8_t fv_sectshift; /* Log2 of fv_sectorsize */ - uint8_t fv_nrootdirsects; /* Number of root directory sectors */ - uint8_t fv_fattype; /* FAT size: 0 (not determined), 12, 16, or 32 */ - uint16_t fv_bootcodesize; /* Size of array at fv_bootcode */ - uint32_t fv_createtime; /* Creation time */ - uint32_t fv_sectorsize; /* Size of one hardware sector */ - uint32_t fv_nfatsects; /* Number of sectors in each FAT */ - uint32_t fv_nclusters; /* Number of clusters */ - uint8_t *fv_sect; /* Allocated working sector buffer */ - const uint8_t *fv_bootcode; /* Points to boot code to put into MBR */ -}; - -/**************************************************************************** - * Global Variables - ****************************************************************************/ - -/**************************************************************************** - * Public Function Prototypes - ****************************************************************************/ - -#undef EXTERN -#if defined(__cplusplus) -#define EXTERN extern "C" -extern "C" { -#else -#define EXTERN extern -#endif - -/**************************************************************************** - * Name: mkfatfs_configfatfs - * - * Description: - * Based on the geometry of the block device and upon the caller-selected - * values, configure the FAT filesystem for the device. - * - * Input: - * fmt - Caller specified format parameters - * var - Holds disk geomtry data. Also, the location to return FAT - * configuration data - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ -EXTERN int mkfatfs_configfatfs(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var); - -/**************************************************************************** - * Name: mkfatfs_writefat - * - * Description: - * Write the configured fat filesystem to the block device - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ -EXTERN int mkfatfs_writefatfs(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var); - -#undef EXTERN -#if defined(__cplusplus) -} -#endif - -#endif /* __FS_FAT_FS_MKATFS_H */ diff --git a/nuttx/fs/fat/fs_writefat.c b/nuttx/fs/fat/fs_writefat.c deleted file mode 100644 index 564be5b50..000000000 --- a/nuttx/fs/fat/fs_writefat.c +++ /dev/null @@ -1,544 +0,0 @@ -/**************************************************************************** - * fs/fat/fs_writefat.c - * - * Copyright (C) 2008-2009, 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt <gnutt@nuttx.org> - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name NuttX nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <nuttx/config.h> - -#include <sys/types.h> -#include <stdint.h> -#include <string.h> -#include <errno.h> - -#include <nuttx/fs/fs.h> -#include <nuttx/fs/fat.h> -#include <nuttx/fs/mkfatfs.h> - -#include "fs_internal.h" -#include "fs_fat32.h" -#include "fs_mkfatfs.h" - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: mkfatfs_initmbr - * - * Description: - * Initialize the sector image of a masterbood record - * - * Input: - * fmt - User specified format parameters - * var - Other format parameters that are not user specifiable - * - * Return: - * None; caller is responsible for providing valid parameters. - * - ****************************************************************************/ -static inline void mkfatfs_initmbr(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var) -{ - memset(var->fv_sect, 0, var->fv_sectorsize); - - /* 3@0: Jump instruction to boot code */ - - memcpy(&var->fv_sect[BS_JUMP], var->fv_jump, 3); - - /* 8@3: Usually "MSWIN4.1" */ - - strcpy((char*)&var->fv_sect[BS_OEMNAME], "NUTTX "); - - /* 2@11: Bytes per sector: 512, 1024, 2048, 4096 */ - - MBR_PUTBYTESPERSEC(var->fv_sect, var->fv_sectorsize); - - /* 1@13: Sectors per allocation unit: 2**n, n=0..7 */ - - MBR_PUTSECPERCLUS(var->fv_sect, (1 << fmt->ff_clustshift)); - - /* 2@14: Reserved sector count: Usually 32 */ - - MBR_PUTRESVDSECCOUNT(var->fv_sect, fmt->ff_rsvdseccount); - - /* 1@16: Number of FAT data structures: always 2 */ - - MBR_PUTNUMFATS(var->fv_sect, fmt->ff_nfats); - - /* 2@17: FAT12/16: Must be 0 for FAT32 */ - - MBR_PUTROOTENTCNT(var->fv_sect, fmt->ff_rootdirentries); - - /* 2@19: FAT12/16: Must be 0, see BS_TOTSEC32. - * Handled with 4@32: Total count of sectors on the volume */ - - if (fmt->ff_nsectors >= 65536) - { - MBR_PUTTOTSEC32(var->fv_sect, fmt->ff_nsectors); - } - else - { - MBR_PUTTOTSEC16(var->fv_sect, (uint16_t)fmt->ff_nsectors); - } - - /* 1@21: Media code: f0, f8, f9-fa, fc-ff */ - - MBR_PUTMEDIA(var->fv_sect, FAT_DEFAULT_MEDIA_TYPE); /* Only "hard drive" supported */ - - /* 2@22: FAT12/16: Must be 0, see BS32_FATSZ32 -- handled in FAT specific logic */ - - /* 2@24: Sectors per track geometry value and 2@26: Number of heads geometry value */ - - MBR_PUTSECPERTRK(var->fv_sect, FAT_DEFAULT_SECPERTRK); - MBR_PUTNUMHEADS(var->fv_sect, FAT_DEFAULT_NUMHEADS); - - /* 4@28: Count of hidden sectors preceding FAT */ - - MBR_PUTHIDSEC(var->fv_sect, fmt->ff_hidsec); - - /* 4@32: Total count of sectors on the volume -- handled above */ - - /* Most of the rest of the sector depends on the FAT size */ - - if (fmt->ff_fattype != 32) - { - /* 2@22: FAT12/16: Must be 0, see BS32_FATSZ32 */ - - MBR_PUTFATSZ16(var->fv_sect, (uint16_t)var->fv_nfatsects); - - /* The following fields are only valid for FAT12/16 */ - /* 1@36: Drive number for MSDOS bootstrap -- left zero */ - /* 1@37: Reserved (zero) */ - /* 1@38: Extended boot signature: 0x29 if following valid */ - - MBR_PUTBOOTSIG16(var->fv_sect, EXTBOOT_SIGNATURE); - - /* 4@39: Volume serial number */ - - MBR_PUTVOLID16(var->fv_sect, fmt->ff_volumeid); - - /* 11@43: Volume label */ - - memcpy(&var->fv_sect[BS16_VOLLAB], fmt->ff_volumelabel, 11); - - /* 8@54: "FAT12 ", "FAT16 ", or "FAT " */ - - if (fmt->ff_fattype == 12) - { - memcpy(&var->fv_sect[BS16_FILESYSTYPE], "FAT12 ", 8); - } - else /* if (fmt->ff_fattype == 16) */ - { - memcpy(&var->fv_sect[BS16_FILESYSTYPE], "FAT16 ", 8); - } - - /* Boot code may be placed in the remainder of the sector */ - - memcpy(&var->fv_sect[BS16_BOOTCODE], var->fv_bootcode, var->fv_bootcodesize); - } - else - { - /* The following fields are only valid for FAT32 */ - /* 4@36: Count of sectors occupied by one FAT */ - - MBR_PUTFATSZ32(var->fv_sect, var->fv_nfatsects); - - /* 2@40: 0-3:Active FAT, 7=0 both FATS, 7=1 one FAT -- left zero*/ - /* 2@42: MSB:Major LSB:Minor revision number (0.0) -- left zero */ - /* 4@44: Cluster no. of 1st cluster of root dir */ - - MBR_PUTROOTCLUS(var->fv_sect, FAT32_DEFAULT_ROOT_CLUSTER); - - /* 2@48: Sector number of fsinfo structure. Usually 1. */ - - MBR_PUTFSINFO(var->fv_sect, FAT_DEFAULT_FSINFO_SECTOR); - - /* 2@50: Sector number of boot record. Usually 6 */ - - MBR_PUTBKBOOTSEC(var->fv_sect, fmt->ff_backupboot); - - /* 12@52: Reserved (zero) */ - /* 1@64: Drive number for MSDOS bootstrap -- left zero */ - /* 1@65: Reserved (zero) */ - /* 1@66: Extended boot signature: 0x29 if following valid */ - - MBR_PUTBOOTSIG32(var->fv_sect, EXTBOOT_SIGNATURE); - - /* 4@67: Volume serial number */ - - MBR_PUTVOLID32(var->fv_sect, fmt->ff_volumeid); - - /* 11@71: Volume label */ - - memcpy(&var->fv_sect[BS32_VOLLAB], fmt->ff_volumelabel, 11); - - /* 8@82: "FAT12 ", "FAT16 ", or "FAT " */ - - memcpy(&var->fv_sect[BS32_FILESYSTYPE], "FAT32 ", 8); - - /* Boot code may be placed in the remainder of the sector */ - - memcpy(&var->fv_sect[BS16_BOOTCODE], var->fv_bootcode, var->fv_bootcodesize); - } - - /* The magic bytes at the end of the MBR are common to FAT12/16/32 */ - /* 2@510: Valid MBRs have 0x55aa here */ - - MBR_PUTSIGNATURE(var->fv_sect, BOOT_SIGNATURE16); -} - -/**************************************************************************** - * Name: mkfatfs_initfsinfo - * - * Description: - * Initialize the FAT32 FSINFO sector image - * - * Input: - * fmt - User specified format parameters - * var - Other format parameters that are not user specifiable - * - * Return: - * None; caller is responsible for providing valid parameters. - * - ****************************************************************************/ -static inline void mkfatfs_initfsinfo(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var) -{ - memset(var->fv_sect, 0, var->fv_sectorsize); - - /* 4@0: 0x41615252 = "RRaA" */ - - FSI_PUTLEADSIG(var->fv_sect, 0x41615252); - - /* 480@4: Reserved (zero) */ - /* 4@484: 0x61417272 = "rrAa" */ - - FSI_PUTSTRUCTSIG(var->fv_sect, 0x61417272); - - /* 4@488: Last free cluster count on volume */ - - FSI_PUTFREECOUNT(var->fv_sect, var->fv_nclusters - 1); - - /* 4@492: Cluster number of 1st free cluster */ - - FSI_PUTNXTFREE(var->fv_sect, FAT32_DEFAULT_ROOT_CLUSTER); - - /* 12@496: Reserved (zero) */ - /* 4@508: 0xaa550000 */ - - FSI_PUTTRAILSIG(var->fv_sect, BOOT_SIGNATURE32); -} - -/**************************************************************************** - * Name: mkfatfs_initrootdir - * - * Description: - * Initialize one root directory sector image - * - * Input: - * fmt - User specified format parameters - * var - Other format parameters that are not user specifiable - * sectno - On FAT32, the root directory is a cluster chain. - * This value indicates which sector of the cluster should be produced. - * - * Return: - * None; caller is responsible for providing valid parameters. - * - ****************************************************************************/ -static inline void mkfatfs_initrootdir(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var, int sectno) -{ - memset(var->fv_sect, 0, var->fv_sectorsize); - if (sectno == 0) - { - /* It is only necessary to set data in the first sector of the directory */ - - if (memcmp(fmt->ff_volumelabel, " ", 11)) - { - memcpy(&var->fv_sect[DIR_NAME], fmt->ff_volumelabel, 11); - } - - DIR_PUTATTRIBUTES(var->fv_sect, FATATTR_VOLUMEID); - DIR_PUTCRTIME(var->fv_sect, var->fv_createtime & 0xffff); - DIR_PUTWRTTIME(var->fv_sect, var->fv_createtime & 0xffff); - DIR_PUTCRDATE(var->fv_sect, var->fv_createtime >> 16); - DIR_PUTWRTDATE(var->fv_sect, var->fv_createtime >> 16); - } -} - -/**************************************************************************** - * Name: mkfatfs_writembr - * - * Description: - * Write the master boot record and, for FAT32, the backup boot record and - * the fsinfo sector. - * - * Input: - * fmt - User specified format parameters - * var - Other format parameters that are not user specifiable - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ - -static inline int mkfatfs_writembr(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var) -{ - int sectno; - int ret; - - /* Create an image of the configured master boot record */ - - mkfatfs_initmbr(fmt, var); - - /* Write the master boot record as sector zero */ - - ret = DEV_WRITE(var->fv_sect, 0, 1); - - /* Write all of the reserved sectors */ - - memset(var->fv_sect, 0, var->fv_sectorsize); - for (sectno = 1; sectno < fmt->ff_rsvdseccount && ret >= 0; sectno++) - { - ret = DEV_WRITE(var->fv_sect, sectno, 1); - } - - /* Write FAT32-specific sectors */ - - if (ret >= 0 && fmt->ff_fattype == 32) - { - /* Write the backup master boot record */ - - if (fmt->ff_backupboot != 0) - { - /* Create another copy of the configured master boot record */ - - mkfatfs_initmbr(fmt, var); - - /* Write it to the backup location */ - - ret = DEV_WRITE(var->fv_sect, fmt->ff_backupboot, 1); - } - - if (ret >= 0) - { - /* Create an image of the fsinfo sector*/ - - mkfatfs_initfsinfo(fmt, var); - - /* Write the fsinfo sector */ - - ret = DEV_WRITE(var->fv_sect, FAT_DEFAULT_FSINFO_SECTOR, 1); - } - } - - return ret; -} - -/**************************************************************************** - * Name: mkfatfs_writefat - * - * Description: - * Write the FAT sectors - * - * Input: - * fmt - User specified format parameters - * var - Other format parameters that are not user specifiable - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ - -static inline int mkfatfs_writefat(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var) -{ - off_t offset = fmt->ff_rsvdseccount; - int fatno; - int sectno; - int ret; - - /* Loop for each FAT copy */ - - for (fatno = 0; fatno < fmt->ff_nfats; fatno++) - { - /* Loop for each sector in the FAT */ - - for (sectno = 0; sectno < var->fv_nfatsects; sectno++) - { - memset(var->fv_sect, 0, var->fv_sectorsize); - - /* Mark cluster allocations in sector one of each FAT */ - - if (sectno == 0) - { - memset(var->fv_sect, 0, var->fv_sectorsize); - switch(fmt->ff_fattype) - { - case 12: - /* Mark the first two full FAT entries -- 24 bits, 3 bytes total */ - - memset(var->fv_sect, 0xff, 3); - break; - - case 16: - /* Mark the first two full FAT entries -- 32 bits, 4 bytes total */ - - memset(var->fv_sect, 0xff, 4); - break; - - case 32: - default: /* Shouldn't happen */ - /* Mark the first two full FAT entries -- 64 bits, 8 bytes total */ - - memset(var->fv_sect, 0xff, 8); - - /* Cluster 2 is used as the root directory. Mark as EOF */ - - var->fv_sect[8] = 0xf8; - memset(&var->fv_sect[9], 0xff, 3); - break; - } - - /* Save the media type in the first byte of the FAT */ - - var->fv_sect[0] = FAT_DEFAULT_MEDIA_TYPE; - } - - /* Write the FAT sector */ - - ret = DEV_WRITE(var->fv_sect, offset, 1); - if (ret < 0) - { - return ret; - } - offset++; - } - } - return OK; -} - -/**************************************************************************** - * Name: mkfatfs_writerootdir - * - * Description: - * Write the root directory sectors - * - * Input: - * fmt - User specified format parameters - * var - Other format parameters that are not user specifiable - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ - -static inline int mkfatfs_writerootdir(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var) -{ - off_t offset = fmt->ff_rsvdseccount + fmt->ff_nfats * var->fv_nfatsects; - int ret; - int i; - - /* Write the root directory after the last FAT. This is the root directory - * area for FAT12/16, and the first cluster on FAT32. - */ - - for (i = 0; i < var->fv_nrootdirsects; i++) - { - /* Format the next sector of the root directory */ - - mkfatfs_initrootdir(fmt, var, i); - - /* Write the next sector of the root directory */ - - ret = DEV_WRITE(var->fv_sect, offset, 1); - if (ret < 0) - { - return ret; - } - offset++; - } - return 0; -} - -/**************************************************************************** - * Global Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: mkfatfs_writefat - * - * Description: - * Write the configured fat filesystem to the block device - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ - -int mkfatfs_writefatfs(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var) -{ - int ret; - - /* Write the master boot record (also the backup and fsinfo sectors) */ - - ret = mkfatfs_writembr(fmt, var); - - /* Write FATs */ - - if (ret >= 0) - { - ret = mkfatfs_writefat(fmt, var); - } - - /* Write the root directory after the last FAT. */ - - if (ret >= 0) - { - ret = mkfatfs_writerootdir(fmt, var); - } - return ret; -} - |