⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fs_fat32util.c

📁 這是一個實時嵌入式作業系統 實作了MCS51 ARM等MCU
💻 C
📖 第 1 页 / 共 5 页
字号:
/**************************************************************************** * fs_fat32util.c * *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. *   Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * 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 <stdlib.h>#include <string.h>#include <ctype.h>#include <semaphore.h>#include <assert.h>#include <errno.h>#include <debug.h>#include <nuttx/fs.h>#include <nuttx/fat.h>#include "fs_internal.h"#include "fs_fat32.h"/**************************************************************************** * Definitions ****************************************************************************//**************************************************************************** * Private Types ****************************************************************************//**************************************************************************** * Private Function Prototypes ****************************************************************************//**************************************************************************** * Private Variables ****************************************************************************//**************************************************************************** * Public Variables ****************************************************************************//**************************************************************************** * Private Functions ****************************************************************************//**************************************************************************** * Name: fat_path2dirname * * Desciption:  Convert a user filename into a properly formatted FAT *   (short) filname as it would appear in a directory entry.  Here are the *    rules for the 11 byte 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 ' ' * *   Any bytes *     0x00-0x1f = (except for 0x00 and 0x05 in the first byte) *     0x22      = '"' *     0x2a-0x2c = '*', '+', ',' *     0x2e-0x2f = '.', '/' *     0x3a-0x3f = ':', ';', '<', '=', '>', '?' *     0x5b-0x5d = '[', '\\', ;]' *     0x7c      = '|' * *   Upper case characters are not allowed in directory names (without some *   poorly documented operatgions 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. * ****************************************************************************/static inline int fat_path2dirname(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;#endif    const char *node = *path;    int endndx;    ubyte ch;    int ndx = 0;    /* Initialized the name with all spaces */    memset(dirinfo->fd_name, ' ', 8+3);     /* 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.  Note 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 */        else if (ch == '.')          {            /* 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 charaters */#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)                {                  /* Clear lower case name bit in mask*/                  ntlcenable &= FATNTRES_LCNAME;                }              else                {                  /* Clear lower case extension in mask */                  ntlcenable &= FATNTRES_LCNAME;                }          }#endif        /* Check for lower case characters */        else if (islower(ch))          {            /* 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)              {                /* Set lower case name bit */                ntlcfound |= FATNTRES_LCNAME;              }            else              {                /* Set lower case extension bit */                ntlcfound |= FATNTRES_LCNAME;              }#endif          }        /* 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_checkfsinfo * * Desciption: Read the FAT32 FSINFO sector * ****************************************************************************/static int fat_checkfsinfo(struct fat_mountpt_s *fs){  /* 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_fsinextfree  = FSI_GETFREECOUNT(fs->fs_buffer);      fs->fs_fsifreecount = 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  ndatasectors;  uint32  ntotalfatsects;  uint16  rootdirsectors = 0;  boolean 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)    {      return -ENODEV;    }  /* 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)    {      return -ENODEV;    }  /* 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)    {      return -ENODEV;    }  /* Get the total number of reserved sectors */  fs->fs_fatresvdseccount = MBR_GETRESVDSECCOUNT(fs->fs_buffer);  if (fs->fs_fatresvdseccount > fs->fs_hwnsectors)    {      return -ENODEV;    }  /* 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)    {      return -ENODEV;    }  /* 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    {      return -ENODEV;    }  /* 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 fat_getuint16(ubyte *ptr){#ifdef CONFIG_ENDIAN_BIG  /* The bytes always have to be swapped if the target is big-endian */  return ((uint16)ptr[0] << 8) | ptr[1];#else  /* Byte-by-byte transfer is still necessary if the address is un-aligned */  return ((uint16)ptr[1] << 8) | ptr[0];#endif}/**************************************************************************** * Name: fat_getuint32 ****************************************************************************/uint32 fat_getuint32(ubyte *ptr){#ifdef CONFIG_ENDIAN_BIG  /* The bytes always have to be swapped if the target is big-endian */  return ((uint32)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)fat_getuint16(&ptr[2]) << 16) | fat_getuint16(&ptr[0]);#endif}/**************************************************************************** * Name: fat_putuint16 ****************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -