Skip to content
Snippets Groups Projects
fs_fat32util.c 52.4 KiB
Newer Older
patacongo's avatar
patacongo committed
/****************************************************************************
 * fs/fat/fs_fat32util.c
patacongo's avatar
patacongo committed
 *
patacongo's avatar
patacongo committed
 *   Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
patacongo's avatar
patacongo committed
 *   Author: Gregory Nutt <gnutt@nuttx.org>
patacongo's avatar
patacongo committed
 *
 * 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>
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
patacongo's avatar
patacongo committed
#include <stdlib.h>
#include <string.h>
patacongo's avatar
patacongo committed
#include <time.h>
patacongo's avatar
patacongo committed
#include <semaphore.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>

patacongo's avatar
patacongo committed
#include <nuttx/kmalloc.h>
patacongo's avatar
patacongo committed
#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_checkfsinfo
 *
 * Desciption: Read the FAT32 FSINFO sector
 *
 ****************************************************************************/

static int fat_checkfsinfo(struct fat_mountpt_s *fs)
{
patacongo's avatar
patacongo committed
  /* Make sure that the fsinfo sector is in the cache */
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
  if (fat_fscacheread(fs, fs->fs_fsinfo) == OK)
patacongo's avatar
patacongo committed
    {
patacongo's avatar
patacongo committed
      /* 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;
        }
patacongo's avatar
patacongo committed
    }
  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;
patacongo's avatar
patacongo committed

  /* 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.
   */

patacongo's avatar
patacongo committed
  if (MBR_GETSIGNATURE(fs->fs_buffer) != BOOT_SIGNATURE16 ||
patacongo's avatar
patacongo committed
      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);

patacongo's avatar
patacongo committed
      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
patacongo's avatar
patacongo committed
   * volume has <= FAT_MAXCLUST12 (4084) clusters, a FAT16 volume has <=
   * FAT_MINCLUST16 (microsfoft says < 65,525) clusters, and any larger
   * is FAT32.
patacongo's avatar
patacongo committed
   *
   * Get the number of 32-bit directory entries in root directory (zero
patacongo's avatar
patacongo committed
   * for FAT32).
patacongo's avatar
patacongo committed
   */

  fs->fs_rootentcnt = MBR_GETROOTENTCNT(fs->fs_buffer);
  if (fs->fs_rootentcnt != 0)
patacongo's avatar
patacongo committed
    {
      notfat32       = true; /* Must be zero for FAT32 */
patacongo's avatar
patacongo committed
      rootdirsectors = (32 * fs->fs_rootentcnt  + fs->fs_hwsectorsize - 1) / fs->fs_hwsectorsize;
patacongo's avatar
patacongo committed
    }
patacongo's avatar
patacongo committed

  /* Determine the number of sectors in a FAT. */

  fs->fs_nfatsects = MBR_GETFATSZ16(fs->fs_buffer); /* Should be zero */
  if (fs->fs_nfatsects)
patacongo's avatar
patacongo committed
    {
      notfat32 = true; /* Must be zero for FAT32 */
patacongo's avatar
patacongo committed
    }
  else
    {
      fs->fs_nfatsects = MBR_GETFATSZ32(fs->fs_buffer);
patacongo's avatar
patacongo committed
    }

  if (!fs->fs_nfatsects || fs->fs_nfatsects >= fs->fs_hwnsectors)
patacongo's avatar
patacongo committed
    {
      fdbg("ERROR: fs_nfatsects %d fs_hwnsectors: %d\n",
           fs->fs_nfatsects, fs->fs_hwnsectors);

patacongo's avatar
patacongo committed
      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 */
patacongo's avatar
patacongo committed
    }
  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);

patacongo's avatar
patacongo committed
      return -ENODEV;
    }

  /* 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);

patacongo's avatar
patacongo committed
      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;
patacongo's avatar
patacongo committed

  /* Get the total number of data sectors */

  ndatasectors = fs->fs_fattotsec - fs->fs_fatresvdseccount - ntotalfatsects - rootdirsectors;
patacongo's avatar
patacongo committed
  if (ndatasectors > fs->fs_hwnsectors)
    {
      fdbg("ERROR: ndatasectors %d fs_hwnsectors: %d\n",
           ndatasectors, fs->fs_hwnsectors);

patacongo's avatar
patacongo committed
      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: */

patacongo's avatar
patacongo committed
  if (fs->fs_nclusters <= FAT_MAXCLUST12)
patacongo's avatar
patacongo committed
    {
      fs->fs_fsinfo = 0;
      fs->fs_type   = FSTYPE_FAT12;
    }
patacongo's avatar
patacongo committed
  else if (fs->fs_nclusters <= FAT_MAXCLUST16)
patacongo's avatar
patacongo committed
    {
      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;
patacongo's avatar
patacongo committed
    }
  else
    {
      fdbg("ERROR: notfat32: %d fs_nclusters: %d\n",
           notfat32, fs->fs_nclusters);

patacongo's avatar
patacongo committed
      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; 
patacongo's avatar
patacongo committed
    }

  fs->fs_database     = fs->fs_fatbase + ntotalfatsects + fs->fs_rootentcnt / DIRSEC_NDIRS(fs);
patacongo's avatar
patacongo committed
  fs->fs_fsifreecount = 0xffffffff;

  return OK;
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: fat_getuint16
 ****************************************************************************/

uint16_t fat_getuint16(uint8_t *ptr)
patacongo's avatar
patacongo committed
{
#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];
patacongo's avatar
patacongo committed
#else
  /* Byte-by-byte transfer is still necessary if the address is un-aligned */

  return ((uint16_t)ptr[1] << 8) | ptr[0];
patacongo's avatar
patacongo committed
#endif
}

/****************************************************************************
 * Name: fat_getuint32
 ****************************************************************************/

uint32_t fat_getuint32(uint8_t *ptr)
patacongo's avatar
patacongo committed
{
#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]);
patacongo's avatar
patacongo committed
#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]);
patacongo's avatar
patacongo committed
#endif
}

/****************************************************************************
 * Name: fat_putuint16
 ****************************************************************************/

void fat_putuint16(uint8_t *ptr, uint16_t value16)
patacongo's avatar
patacongo committed
{
  uint8_t *val = (uint8_t*)&value16;
patacongo's avatar
patacongo committed
#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)
patacongo's avatar
patacongo committed
{
  uint16_t *val = (uint16_t*)&value32;
patacongo's avatar
patacongo committed
#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
 *
patacongo's avatar
patacongo committed
 * Desciption: Get the system time convert to a time and and date suitble
patacongo's avatar
patacongo committed
 * 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
patacongo's avatar
patacongo committed
 *      Bits 0:4   = Day of month (1-31)
patacongo's avatar
patacongo committed
 *      Bits 5:8   = Month of year (1-12)
 *      Bits 9:15  = Year from 1980 (0-127 representing 1980-2107)
 *
 ****************************************************************************/

uint32_t fat_systime2fattime(void)
patacongo's avatar
patacongo committed
{
patacongo's avatar
patacongo committed
  /* 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_settime(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
patacongo's avatar
patacongo committed
    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:
patacongo's avatar
patacongo committed
 *      Bits 0:4   = Day of month (1-31)
patacongo's avatar
patacongo committed
 *      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)
patacongo's avatar
patacongo committed
{
patacongo's avatar
patacongo committed
  /* 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  =  (fatdate & 0x001f) <<  1;       /* Bits 0-4: 2 second count (0-29) */
  tm.tm_min  =  (fatdate & 0x07e0) >>  5;       /* Bits 5-10: minutes (0-59) */
  tm.tm_hour =  (fatdate & 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
patacongo's avatar
patacongo committed
    return 0;
patacongo's avatar
patacongo committed
#endif
patacongo's avatar
patacongo committed
}

/****************************************************************************
 * 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)
patacongo's avatar
patacongo committed
{
  FAR struct inode *inode;
  struct geometry geo;
  int ret;

  /* Assume that the mount is successful */

  fs->fs_mounted = true;
patacongo's avatar
patacongo committed

  /* 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 */

patacongo's avatar
patacongo committed
  fs->fs_buffer = (uint8_t*)kmalloc(fs->fs_hwsectorsize);
patacongo's avatar
patacongo committed
  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)
patacongo's avatar
patacongo committed
    {
      /* 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.
patacongo's avatar
patacongo committed
       */

       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.
            */
patacongo's avatar
patacongo committed
 
          uint8_t part = PART_GETTYPE(i, fs->fs_buffer);
          fvdbg("Partition %d, offset %d, type %d\n", i, PART_ENTRY(i), part);
patacongo's avatar
patacongo committed

          if (part == 0)
            {
              fvdbg("No partition %d\n", i);
              continue;
            }
patacongo's avatar
patacongo committed

          /* There appears to be a partition, get the sector number of the
           * partition (LBA)
           */
patacongo's avatar
patacongo committed

          fs->fs_fatbase = PART_GETSTARTSECTOR(i, fs->fs_buffer);
patacongo's avatar
patacongo committed

          /* 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 */
patacongo's avatar
patacongo committed

              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;
            }
        }
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
        {
          fdbg("No valid MBR\n");
          goto errout_with_buffer;
patacongo's avatar
patacongo committed
        }
    }

  /* 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)
      {
patacongo's avatar
patacongo committed
        goto errout_with_buffer;
patacongo's avatar
patacongo committed
      }
  }

  /* 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);
patacongo's avatar
patacongo committed
  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:
patacongo's avatar
patacongo committed
  kfree(fs->fs_buffer);
patacongo's avatar
patacongo committed
  fs->fs_buffer = 0;
 errout:
  fs->fs_mounted = false;
patacongo's avatar
patacongo committed
  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
patacongo's avatar
patacongo committed
   * of the mount.
   */

  if (fs && fs->fs_mounted)
    {
      struct fat_file_s *file;

      /* 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;
patacongo's avatar
patacongo committed

      /* Make sure that this is flagged in every opened file */

      for (file = fs->fs_head; file; file = file->ff_next)
        {
          file->ff_open = false;
patacongo's avatar
patacongo committed
        }
    }
  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,
patacongo's avatar
patacongo committed
               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,
patacongo's avatar
patacongo committed
                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 )
patacongo's avatar
patacongo committed
{
  cluster -= 2;
  if (cluster >= fs->fs_nclusters - 2)
    {
       return -EINVAL;
    }
  return cluster * fs->fs_fatsecperclus + fs->fs_database;
}

/****************************************************************************
 * Name: fat_getcluster
 *
patacongo's avatar
patacongo committed
 * Desciption: Get the next cluster start from the FAT.
patacongo's avatar
patacongo committed
 *
 * Return:  <0: error, 0:cluster unassigned, >=0: start sector of cluster
 *
 ****************************************************************************/

off_t fat_getcluster(struct fat_mountpt_s *fs, uint32_t clusterno)
patacongo's avatar
patacongo committed
{
  /* 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 :
            {
patacongo's avatar
patacongo committed
              unsigned int fatoffset;
patacongo's avatar
patacongo committed
              unsigned int cluster;
patacongo's avatar
patacongo committed
              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 */
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
                  break;
                }

              /* Get the first, LS byte of the cluster from the FAT */

patacongo's avatar
patacongo committed
              fatindex = fatoffset & SEC_NDXMASK(fs);
              cluster  = fs->fs_buffer[fatindex];
patacongo's avatar
patacongo committed

              /* 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 */
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
                      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.
               */

patacongo's avatar
patacongo committed
              cluster |= (unsigned int)fs->fs_buffer[fatindex] << 8;
patacongo's avatar
patacongo committed

              /* Now, pick out the correct 12 bit cluster start sector value */

              if ((clusterno & 1) != 0)
                {
                  /* Odd.. take the MS 12-bits */
patacongo's avatar
patacongo committed

                  cluster >>= 4;
patacongo's avatar
patacongo committed
                }
              else
                {
                  /* Even.. take the LS 12-bits */
patacongo's avatar
patacongo committed

                  cluster &= 0x0fff;
patacongo's avatar
patacongo committed
                }
patacongo's avatar
patacongo committed
              return cluster;
patacongo's avatar
patacongo committed
            }

          case FSTYPE_FAT16 :
            {
              unsigned int fatoffset = 2 * clusterno;
              off_t        fatsector = fs->fs_fatbase + SEC_NSECTORS(fs, fatoffset);
patacongo's avatar
patacongo committed
              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);
patacongo's avatar
patacongo committed
              unsigned int fatindex  = fatoffset & SEC_NDXMASK(fs);

              if (fat_fscacheread(fs, fatsector) < 0)
                {
                  /* Read error */
                  break;
                }
patacongo's avatar
patacongo committed
              return FAT_GETFAT32(fs->fs_buffer, fatindex) & 0x0fffffff;
patacongo's avatar
patacongo committed
            }
          default:
              break;
        }
    }

  /* There is no cluster information, or an error occured */

patacongo's avatar
patacongo committed
}

/****************************************************************************
 * Name: fat_putcluster
 *
patacongo's avatar
patacongo committed
 * Desciption: Write a new cluster into the FAT
patacongo's avatar
patacongo committed
 *
 ****************************************************************************/

int fat_putcluster(struct fat_mountpt_s *fs, uint32_t clusterno, off_t nextcluster)
patacongo's avatar
patacongo committed
{
  /* 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 :
            {
patacongo's avatar
patacongo committed
              unsigned int fatoffset;
              unsigned int fatindex;
              uint8_t      value;
patacongo's avatar
patacongo committed

              /* 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 */
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
                  break;
                }

patacongo's avatar
patacongo committed
              /* Get the LS byte first handling the 12-bit alignment within
patacongo's avatar
patacongo committed
               * the 16-bits
               */

              fatindex = fatoffset & SEC_NDXMASK(fs);
              if ((clusterno & 1) != 0)
                {
patacongo's avatar
patacongo committed
                  /* Save the LS four bits of the next cluster */

                  value = (fs->fs_buffer[fatindex] & 0x0f) | nextcluster << 4;
patacongo's avatar
patacongo committed
                }
              else
                {
patacongo's avatar
patacongo committed
                  /* Save the LS eight bits of the next cluster */

                  value = (uint8_t)nextcluster;
patacongo's avatar
patacongo committed
                }
patacongo's avatar
patacongo committed
              fs->fs_buffer[fatindex] = value;

              /* With FAT12, the second byte of the cluster number may lie in
               * a different sector than the first byte.
               */