summaryrefslogtreecommitdiffstats
path: root/cpukit/libfs/src/dosfs
diff options
context:
space:
mode:
Diffstat (limited to 'cpukit/libfs/src/dosfs')
-rw-r--r--cpukit/libfs/src/dosfs/.cvsignore4
-rw-r--r--cpukit/libfs/src/dosfs/dosfs.h88
-rw-r--r--cpukit/libfs/src/dosfs/fat.c863
-rw-r--r--cpukit/libfs/src/dosfs/fat.h517
-rw-r--r--cpukit/libfs/src/dosfs/fat_fat_operations.c436
-rw-r--r--cpukit/libfs/src/dosfs/fat_fat_operations.h59
-rw-r--r--cpukit/libfs/src/dosfs/fat_file.c993
-rw-r--r--cpukit/libfs/src/dosfs/fat_file.h192
-rw-r--r--cpukit/libfs/src/dosfs/msdos.h467
-rw-r--r--cpukit/libfs/src/dosfs/msdos_conv.c317
-rw-r--r--cpukit/libfs/src/dosfs/msdos_create.c267
-rw-r--r--cpukit/libfs/src/dosfs/msdos_dir.c706
-rw-r--r--cpukit/libfs/src/dosfs/msdos_eval.c437
-rw-r--r--cpukit/libfs/src/dosfs/msdos_file.c503
-rw-r--r--cpukit/libfs/src/dosfs/msdos_format.c1127
-rw-r--r--cpukit/libfs/src/dosfs/msdos_free.c56
-rw-r--r--cpukit/libfs/src/dosfs/msdos_fsunmount.c70
-rw-r--r--cpukit/libfs/src/dosfs/msdos_handlers_dir.c36
-rw-r--r--cpukit/libfs/src/dosfs/msdos_handlers_file.c36
-rw-r--r--cpukit/libfs/src/dosfs/msdos_init.c64
-rw-r--r--cpukit/libfs/src/dosfs/msdos_initsupp.c149
-rw-r--r--cpukit/libfs/src/dosfs/msdos_misc.c1731
-rw-r--r--cpukit/libfs/src/dosfs/msdos_mknod.c84
-rw-r--r--cpukit/libfs/src/dosfs/msdos_node_type.c58
-rw-r--r--cpukit/libfs/src/dosfs/msdos_rename.c92
25 files changed, 9352 insertions, 0 deletions
diff --git a/cpukit/libfs/src/dosfs/.cvsignore b/cpukit/libfs/src/dosfs/.cvsignore
new file mode 100644
index 0000000000..4b0640c9c5
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/.cvsignore
@@ -0,0 +1,4 @@
+config.h
+config.h.in
+stamp-h
+stamp-h.in
diff --git a/cpukit/libfs/src/dosfs/dosfs.h b/cpukit/libfs/src/dosfs/dosfs.h
new file mode 100644
index 0000000000..5b6cdec2bf
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/dosfs.h
@@ -0,0 +1,88 @@
+/**
+ * @file rtems/dosfs.h
+ *
+ * Application interface to MSDOS filesystem.
+ */
+
+/*
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ */
+
+#ifndef _RTEMS_DOSFS_H
+#define _RTEMS_DOSFS_H
+
+#include <rtems.h>
+#include <rtems/libio.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int rtems_dosfs_initialize(rtems_filesystem_mount_table_entry_t *mt_entry,
+ const void *data);
+
+#define MSDOS_FMT_FATANY 0
+#define MSDOS_FMT_FAT12 1
+#define MSDOS_FMT_FAT16 2
+#define MSDOS_FMT_FAT32 3
+
+#define MSDOS_FMT_INFO_LEVEL_NONE (0)
+#define MSDOS_FMT_INFO_LEVEL_INFO (1)
+#define MSDOS_FMT_INFO_LEVEL_DETAIL (2)
+#define MSDOS_FMT_INFO_LEVEL_DEBUG (3)
+
+/*
+ * data to be filled out for formatter: parameters for format call
+ * any parameter set to 0 or NULL will be automatically detected/computed
+ */
+typedef struct {
+ const char *OEMName; /* OEM Name string or NULL */
+ const char *VolLabel; /* Volume Label string or NULL */
+ uint32_t sectors_per_cluster; /* request value: sectors per cluster */
+ uint32_t fat_num; /* request value: number of FATs on disk */
+ uint32_t files_per_root_dir; /* request value: file entries in root */
+ uint8_t fattype; /* request value: MSDOS_FMT_FAT12/16/32 */
+ uint8_t media; /* media code. default: 0xF8 */
+ bool quick_format; /* true: do not clear out data sectors */
+ uint32_t cluster_align; /* requested value: cluster alignment */
+ /* make sector number of first sector */
+ /* of first cluster divisible by this */
+ /* value. This can optimize clusters */
+ /* to be located at start of track */
+ /* or start of flash block */
+ int info_level; /* The amount of info to output */
+} msdos_format_request_param_t;
+
+/*=========================================================================*\
+| Function: |
+\*-------------------------------------------------------------------------*/
+int msdos_format
+(
+/*-------------------------------------------------------------------------*\
+| Purpose: |
+| format device with msdos filesystem |
++---------------------------------------------------------------------------+
+| Input Parameters: |
+\*-------------------------------------------------------------------------*/
+ const char *devname, /* device name */
+ const msdos_format_request_param_t *rqdata /* requested fmt parameters */
+ /* set to NULL for automatic */
+ /* determination */
+ );
+/*-------------------------------------------------------------------------*\
+| Return Value: |
+| 0, if success, -1 and errno if failed |
+\*=========================================================================*/
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/cpukit/libfs/src/dosfs/fat.c b/cpukit/libfs/src/dosfs/fat.c
new file mode 100644
index 0000000000..ba9ae23e86
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/fat.c
@@ -0,0 +1,863 @@
+/*
+ * fat.c
+ *
+ * Low-level operations on a volume with FAT filesystem
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * @(#) $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+
+#include <rtems/libio_.h>
+
+#include "fat.h"
+#include "fat_fat_operations.h"
+
+int
+fat_buf_access(fat_fs_info_t *fs_info, uint32_t blk, int op_type,
+ rtems_bdbuf_buffer **buf)
+{
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ uint8_t i;
+ bool sec_of_fat;
+
+
+ if (fs_info->c.state == FAT_CACHE_EMPTY)
+ {
+ if (op_type == FAT_OP_TYPE_READ)
+ sc = rtems_bdbuf_read(fs_info->vol.dev, blk, &fs_info->c.buf);
+ else
+ sc = rtems_bdbuf_get(fs_info->vol.dev, blk, &fs_info->c.buf);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+ fs_info->c.blk_num = blk;
+ fs_info->c.modified = 0;
+ fs_info->c.state = FAT_CACHE_ACTUAL;
+ }
+
+ sec_of_fat = ((fs_info->c.blk_num >= fs_info->vol.fat_loc) &&
+ (fs_info->c.blk_num < fs_info->vol.rdir_loc));
+
+ if (fs_info->c.blk_num != blk)
+ {
+ if (fs_info->c.modified)
+ {
+ if (sec_of_fat && !fs_info->vol.mirror)
+ memcpy(fs_info->sec_buf, fs_info->c.buf->buffer,
+ fs_info->vol.bps);
+
+ sc = rtems_bdbuf_release_modified(fs_info->c.buf);
+ fs_info->c.state = FAT_CACHE_EMPTY;
+ fs_info->c.modified = 0;
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ if (sec_of_fat && !fs_info->vol.mirror)
+ {
+ rtems_bdbuf_buffer *b;
+
+ for (i = 1; i < fs_info->vol.fats; i++)
+ {
+ sc = rtems_bdbuf_get(fs_info->vol.dev,
+ fs_info->c.blk_num +
+ fs_info->vol.fat_length * i,
+ &b);
+ if ( sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(ENOMEM);
+ memcpy(b->buffer, fs_info->sec_buf, fs_info->vol.bps);
+ sc = rtems_bdbuf_release_modified(b);
+ if ( sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(ENOMEM);
+ }
+ }
+ }
+ else
+ {
+ sc = rtems_bdbuf_release(fs_info->c.buf);
+ fs_info->c.state = FAT_CACHE_EMPTY;
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ }
+ if (op_type == FAT_OP_TYPE_READ)
+ sc = rtems_bdbuf_read(fs_info->vol.dev, blk, &fs_info->c.buf);
+ else
+ sc = rtems_bdbuf_get(fs_info->vol.dev, blk, &fs_info->c.buf);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+ fs_info->c.blk_num = blk;
+ fs_info->c.state = FAT_CACHE_ACTUAL;
+ }
+ *buf = fs_info->c.buf;
+ return RC_OK;
+}
+
+int
+fat_buf_release(fat_fs_info_t *fs_info)
+{
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ uint8_t i;
+ bool sec_of_fat;
+
+ if (fs_info->c.state == FAT_CACHE_EMPTY)
+ return RC_OK;
+
+ sec_of_fat = ((fs_info->c.blk_num >= fs_info->vol.fat_loc) &&
+ (fs_info->c.blk_num < fs_info->vol.rdir_loc));
+
+ if (fs_info->c.modified)
+ {
+ if (sec_of_fat && !fs_info->vol.mirror)
+ memcpy(fs_info->sec_buf, fs_info->c.buf->buffer, fs_info->vol.bps);
+
+ sc = rtems_bdbuf_release_modified(fs_info->c.buf);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+ fs_info->c.modified = 0;
+
+ if (sec_of_fat && !fs_info->vol.mirror)
+ {
+ rtems_bdbuf_buffer *b;
+
+ for (i = 1; i < fs_info->vol.fats; i++)
+ {
+ sc = rtems_bdbuf_get(fs_info->vol.dev,
+ fs_info->c.blk_num +
+ fs_info->vol.fat_length * i,
+ &b);
+ if ( sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(ENOMEM);
+ memcpy(b->buffer, fs_info->sec_buf, fs_info->vol.bps);
+ sc = rtems_bdbuf_release_modified(b);
+ if ( sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(ENOMEM);
+ }
+ }
+ }
+ else
+ {
+ sc = rtems_bdbuf_release(fs_info->c.buf);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+ }
+ fs_info->c.state = FAT_CACHE_EMPTY;
+ return RC_OK;
+}
+
+/* _fat_block_read --
+ * This function reads 'count' bytes from device filesystem is mounted on,
+ * starts at 'start+offset' position where 'start' computed in sectors
+ * and 'offset' is offset inside sector (reading may cross sectors
+ * boundary; in this case assumed we want to read sequential sector(s))
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * start - sector num to start read from
+ * offset - offset inside sector 'start'
+ * count - count of bytes to read
+ * buff - buffer provided by user
+ *
+ * RETURNS:
+ * bytes read on success, or -1 if error occured
+ * and errno set appropriately
+ */
+ssize_t
+_fat_block_read(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t start,
+ uint32_t offset,
+ uint32_t count,
+ void *buff
+ )
+{
+ int rc = RC_OK;
+ register fat_fs_info_t *fs_info = mt_entry->fs_info;
+ ssize_t cmpltd = 0;
+ uint32_t blk = start;
+ uint32_t ofs = offset;
+ rtems_bdbuf_buffer *block = NULL;
+ uint32_t c = 0;
+
+ while (count > 0)
+ {
+ rc = fat_buf_access(fs_info, blk, FAT_OP_TYPE_READ, &block);
+ if (rc != RC_OK)
+ return -1;
+
+ c = MIN(count, (fs_info->vol.bps - ofs));
+ memcpy((buff + cmpltd), (block->buffer + ofs), c);
+
+ count -= c;
+ cmpltd += c;
+ blk++;
+ ofs = 0;
+ }
+ return cmpltd;
+}
+
+/* _fat_block_write --
+ * This function write 'count' bytes to device filesystem is mounted on,
+ * starts at 'start+offset' position where 'start' computed in sectors
+ * and 'offset' is offset inside sector (writing may cross sectors
+ * boundary; in this case assumed we want to write sequential sector(s))
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * start - sector num to start read from
+ * offset - offset inside sector 'start'
+ * count - count of bytes to write
+ * buff - buffer provided by user
+ *
+ * RETURNS:
+ * bytes written on success, or -1 if error occured
+ * and errno set appropriately
+ */
+ssize_t
+_fat_block_write(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t start,
+ uint32_t offset,
+ uint32_t count,
+ const void *buff)
+{
+ int rc = RC_OK;
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ ssize_t cmpltd = 0;
+ uint32_t blk = start;
+ uint32_t ofs = offset;
+ rtems_bdbuf_buffer *block = NULL;
+ uint32_t c = 0;
+
+ while(count > 0)
+ {
+ c = MIN(count, (fs_info->vol.bps - ofs));
+
+ if (c == fs_info->vol.bps)
+ rc = fat_buf_access(fs_info, blk, FAT_OP_TYPE_GET, &block);
+ else
+ rc = fat_buf_access(fs_info, blk, FAT_OP_TYPE_READ, &block);
+ if (rc != RC_OK)
+ return -1;
+
+ memcpy((block->buffer + ofs), (buff + cmpltd), c);
+
+ fat_buf_mark_modified(fs_info);
+
+ count -= c;
+ cmpltd +=c;
+ blk++;
+ ofs = 0;
+ }
+ return cmpltd;
+}
+
+/* _fat_block_release --
+ * This function works around the hack that hold a bdbuf and does
+ * not release it.
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ *
+ * RETURNS:
+ * 0 on success, or -1 if error occured and errno set appropriately
+ */
+int
+_fat_block_release(
+ rtems_filesystem_mount_table_entry_t *mt_entry)
+{
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ return fat_buf_release(fs_info);
+}
+
+/* fat_cluster_read --
+ * wrapper for reading a whole cluster at once
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * cln - number of cluster to read
+ * buff - buffer provided by user
+ *
+ * RETURNS:
+ * bytes read on success, or -1 if error occured
+ * and errno set appropriately
+ */
+ssize_t
+fat_cluster_read(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t cln,
+ void *buff
+ )
+{
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t fsec = 0;
+
+ fsec = fat_cluster_num_to_sector_num(mt_entry, cln);
+
+ return _fat_block_read(mt_entry, fsec, 0,
+ fs_info->vol.spc << fs_info->vol.sec_log2, buff);
+}
+
+/* fat_cluster_write --
+ * wrapper for writting a whole cluster at once
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * cln - number of cluster to write
+ * buff - buffer provided by user
+ *
+ * RETURNS:
+ * bytes written on success, or -1 if error occured
+ * and errno set appropriately
+ */
+ssize_t
+fat_cluster_write(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t cln,
+ const void *buff
+ )
+{
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t fsec = 0;
+
+ fsec = fat_cluster_num_to_sector_num(mt_entry, cln);
+
+ return _fat_block_write(mt_entry, fsec, 0,
+ fs_info->vol.spc << fs_info->vol.sec_log2, buff);
+}
+
+/* fat_init_volume_info --
+ * Get inforamtion about volume on which filesystem is mounted on
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured
+ * and errno set appropriately
+ */
+int
+fat_init_volume_info(rtems_filesystem_mount_table_entry_t *mt_entry)
+{
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ int rc = RC_OK;
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ register fat_vol_t *vol = &fs_info->vol;
+ uint32_t data_secs = 0;
+ char boot_rec[FAT_MAX_BPB_SIZE];
+ char fs_info_sector[FAT_USEFUL_INFO_SIZE];
+ ssize_t ret = 0;
+ struct stat stat_buf;
+ int i = 0;
+ rtems_bdbuf_buffer *block = NULL;
+
+ rc = stat(mt_entry->dev, &stat_buf);
+ if (rc == -1)
+ return rc;
+
+ /* Must be a block device. */
+ if (!S_ISBLK(stat_buf.st_mode))
+ rtems_set_errno_and_return_minus_one(ENOTTY);
+
+ /* check that device is registred as block device and lock it */
+ vol->dd = rtems_disk_obtain(stat_buf.st_rdev);
+ if (vol->dd == NULL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ vol->dev = stat_buf.st_rdev;
+
+ /* Read boot record */
+ /* FIXME: Asserts FAT_MAX_BPB_SIZE < bdbuf block size */
+ sc = rtems_bdbuf_read( vol->dev, 0, &block);
+ if (sc != RTEMS_SUCCESSFUL)
+ {
+ rtems_disk_release(vol->dd);
+ rtems_set_errno_and_return_minus_one( EIO);
+ }
+
+ memcpy( boot_rec, block->buffer, FAT_MAX_BPB_SIZE);
+
+ sc = rtems_bdbuf_release( block);
+ if (sc != RTEMS_SUCCESSFUL)
+ {
+ rtems_disk_release(vol->dd);
+ rtems_set_errno_and_return_minus_one( EIO );
+ }
+
+ /* Evaluate boot record */
+ vol->bps = FAT_GET_BR_BYTES_PER_SECTOR(boot_rec);
+
+ if ( (vol->bps != 512) &&
+ (vol->bps != 1024) &&
+ (vol->bps != 2048) &&
+ (vol->bps != 4096))
+ {
+ rtems_disk_release(vol->dd);
+ rtems_set_errno_and_return_minus_one( EINVAL );
+ }
+
+ for (vol->sec_mul = 0, i = (vol->bps >> FAT_SECTOR512_BITS); (i & 1) == 0;
+ i >>= 1, vol->sec_mul++);
+ for (vol->sec_log2 = 0, i = vol->bps; (i & 1) == 0;
+ i >>= 1, vol->sec_log2++);
+
+ vol->spc = FAT_GET_BR_SECTORS_PER_CLUSTER(boot_rec);
+ /*
+ * "sectors per cluster" of zero is invalid
+ * (and would hang the following loop)
+ */
+ if (vol->spc == 0)
+ {
+ rtems_disk_release(vol->dd);
+ rtems_set_errno_and_return_minus_one(EINVAL);
+ }
+
+ for (vol->spc_log2 = 0, i = vol->spc; (i & 1) == 0;
+ i >>= 1, vol->spc_log2++);
+
+ /*
+ * "bytes per cluster" value greater than 32K is invalid
+ */
+ if ((vol->bpc = vol->bps << vol->spc_log2) > MS_BYTES_PER_CLUSTER_LIMIT)
+ {
+ rtems_disk_release(vol->dd);
+ rtems_set_errno_and_return_minus_one(EINVAL);
+ }
+
+ for (vol->bpc_log2 = 0, i = vol->bpc; (i & 1) == 0;
+ i >>= 1, vol->bpc_log2++);
+
+ vol->fats = FAT_GET_BR_FAT_NUM(boot_rec);
+ vol->fat_loc = FAT_GET_BR_RESERVED_SECTORS_NUM(boot_rec);
+
+ vol->rdir_entrs = FAT_GET_BR_FILES_PER_ROOT_DIR(boot_rec);
+
+ /* calculate the count of sectors occupied by the root directory */
+ vol->rdir_secs = ((vol->rdir_entrs * FAT_DIRENTRY_SIZE) + (vol->bps - 1)) /
+ vol->bps;
+
+ vol->rdir_size = vol->rdir_secs << vol->sec_log2;
+
+ if ( (FAT_GET_BR_SECTORS_PER_FAT(boot_rec)) != 0)
+ vol->fat_length = FAT_GET_BR_SECTORS_PER_FAT(boot_rec);
+ else
+ vol->fat_length = FAT_GET_BR_SECTORS_PER_FAT32(boot_rec);
+
+ vol->data_fsec = vol->fat_loc + vol->fats * vol->fat_length +
+ vol->rdir_secs;
+
+ /* for FAT12/16 root dir starts at(sector) */
+ vol->rdir_loc = vol->fat_loc + vol->fats * vol->fat_length;
+
+ if ( (FAT_GET_BR_TOTAL_SECTORS_NUM16(boot_rec)) != 0)
+ vol->tot_secs = FAT_GET_BR_TOTAL_SECTORS_NUM16(boot_rec);
+ else
+ vol->tot_secs = FAT_GET_BR_TOTAL_SECTORS_NUM32(boot_rec);
+
+ data_secs = vol->tot_secs - vol->data_fsec;
+
+ vol->data_cls = data_secs / vol->spc;
+
+ /* determine FAT type at least */
+ if ( vol->data_cls < FAT_FAT12_MAX_CLN)
+ {
+ vol->type = FAT_FAT12;
+ vol->mask = FAT_FAT12_MASK;
+ vol->eoc_val = FAT_FAT12_EOC;
+ }
+ else
+ {
+ if ( vol->data_cls < FAT_FAT16_MAX_CLN)
+ {
+ vol->type = FAT_FAT16;
+ vol->mask = FAT_FAT16_MASK;
+ vol->eoc_val = FAT_FAT16_EOC;
+ }
+ else
+ {
+ vol->type = FAT_FAT32;
+ vol->mask = FAT_FAT32_MASK;
+ vol->eoc_val = FAT_FAT32_EOC;
+ }
+ }
+
+ if (vol->type == FAT_FAT32)
+ {
+ vol->rdir_cl = FAT_GET_BR_FAT32_ROOT_CLUSTER(boot_rec);
+
+ vol->mirror = FAT_GET_BR_EXT_FLAGS(boot_rec) & FAT_BR_EXT_FLAGS_MIRROR;
+ if (vol->mirror)
+ vol->afat = FAT_GET_BR_EXT_FLAGS(boot_rec) & FAT_BR_EXT_FLAGS_FAT_NUM;
+ else
+ vol->afat = 0;
+
+ vol->info_sec = FAT_GET_BR_FAT32_FS_INFO_SECTOR(boot_rec);
+ if( vol->info_sec == 0 )
+ {
+ rtems_disk_release(vol->dd);
+ rtems_set_errno_and_return_minus_one( EINVAL );
+ }
+ else
+ {
+ ret = _fat_block_read(mt_entry, vol->info_sec , 0,
+ FAT_FSI_LEADSIG_SIZE, fs_info_sector);
+ if ( ret < 0 )
+ {
+ rtems_disk_release(vol->dd);
+ return -1;
+ }
+
+ if (FAT_GET_FSINFO_LEAD_SIGNATURE(fs_info_sector) !=
+ FAT_FSINFO_LEAD_SIGNATURE_VALUE)
+ {
+ _fat_block_release(mt_entry);
+ rtems_disk_release(vol->dd);
+ rtems_set_errno_and_return_minus_one( EINVAL );
+ }
+ else
+ {
+ ret = _fat_block_read(mt_entry, vol->info_sec , FAT_FSI_INFO,
+ FAT_USEFUL_INFO_SIZE, fs_info_sector);
+ if ( ret < 0 )
+ {
+ _fat_block_release(mt_entry);
+ rtems_disk_release(vol->dd);
+ return -1;
+ }
+
+ vol->free_cls = FAT_GET_FSINFO_FREE_CLUSTER_COUNT(fs_info_sector);
+ vol->next_cl = FAT_GET_FSINFO_NEXT_FREE_CLUSTER(fs_info_sector);
+ rc = fat_fat32_update_fsinfo_sector(mt_entry, 0xFFFFFFFF,
+ 0xFFFFFFFF);
+ if ( rc != RC_OK )
+ {
+ _fat_block_release(mt_entry);
+ rtems_disk_release(vol->dd);
+ return rc;
+ }
+ }
+ }
+ }
+ else
+ {
+ vol->rdir_cl = 0;
+ vol->mirror = 0;
+ vol->afat = 0;
+ vol->free_cls = 0xFFFFFFFF;
+ vol->next_cl = 0xFFFFFFFF;
+ }
+
+ _fat_block_release(mt_entry);
+
+ vol->afat_loc = vol->fat_loc + vol->fat_length * vol->afat;
+
+ /* set up collection of fat-files fd */
+ fs_info->vhash = calloc(FAT_HASH_SIZE, sizeof(rtems_chain_control));
+ if ( fs_info->vhash == NULL )
+ {
+ rtems_disk_release(vol->dd);
+ rtems_set_errno_and_return_minus_one( ENOMEM );
+ }
+
+ for (i = 0; i < FAT_HASH_SIZE; i++)
+ rtems_chain_initialize_empty(fs_info->vhash + i);
+
+ fs_info->rhash = calloc(FAT_HASH_SIZE, sizeof(rtems_chain_control));
+ if ( fs_info->rhash == NULL )
+ {
+ rtems_disk_release(vol->dd);
+ free(fs_info->vhash);
+ rtems_set_errno_and_return_minus_one( ENOMEM );
+ }
+ for (i = 0; i < FAT_HASH_SIZE; i++)
+ rtems_chain_initialize_empty(fs_info->rhash + i);
+
+ fs_info->uino_pool_size = FAT_UINO_POOL_INIT_SIZE;
+ fs_info->uino_base = (vol->tot_secs << vol->sec_mul) << 4;
+ fs_info->index = 0;
+ fs_info->uino = (char *)calloc(fs_info->uino_pool_size, sizeof(char));
+ if ( fs_info->uino == NULL )
+ {
+ rtems_disk_release(vol->dd);
+ free(fs_info->vhash);
+ free(fs_info->rhash);
+ rtems_set_errno_and_return_minus_one( ENOMEM );
+ }
+ fs_info->sec_buf = (uint8_t *)calloc(vol->bps, sizeof(uint8_t));
+ if (fs_info->sec_buf == NULL)
+ {
+ rtems_disk_release(vol->dd);
+ free(fs_info->vhash);
+ free(fs_info->rhash);
+ free(fs_info->uino);
+ rtems_set_errno_and_return_minus_one( ENOMEM );
+ }
+
+ return RC_OK;
+}
+
+/* fat_shutdown_drive --
+ * Free all allocated resources and synchronize all necessary data
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured
+ * and errno set appropriately
+ */
+int
+fat_shutdown_drive(rtems_filesystem_mount_table_entry_t *mt_entry)
+{
+ int rc = RC_OK;
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ int i = 0;
+
+ if (fs_info->vol.type & FAT_FAT32)
+ {
+ rc = fat_fat32_update_fsinfo_sector(mt_entry, fs_info->vol.free_cls,
+ fs_info->vol.next_cl);
+ if ( rc != RC_OK )
+ rc = -1;
+ }
+
+ fat_buf_release(fs_info);
+
+ if (rtems_bdbuf_syncdev(fs_info->vol.dev) != RTEMS_SUCCESSFUL)
+ rc = -1;
+
+ for (i = 0; i < FAT_HASH_SIZE; i++)
+ {
+ rtems_chain_node *node = NULL;
+ rtems_chain_control *the_chain = fs_info->vhash + i;
+
+ while ( (node = rtems_chain_get(the_chain)) != NULL )
+ free(node);
+ }
+
+ for (i = 0; i < FAT_HASH_SIZE; i++)
+ {
+ rtems_chain_node *node = NULL;
+ rtems_chain_control *the_chain = fs_info->rhash + i;
+
+ while ( (node = rtems_chain_get(the_chain)) != NULL )
+ free(node);
+ }
+
+ free(fs_info->vhash);
+ free(fs_info->rhash);
+
+ free(fs_info->uino);
+ free(fs_info->sec_buf);
+ rtems_disk_release(fs_info->vol.dd);
+
+ if (rc)
+ errno = EIO;
+ return rc;
+}
+
+/* fat_init_clusters_chain --
+ * Zeroing contents of all clusters in the chain
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * start_cluster_num - num of first cluster in the chain
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured
+ * and errno set appropriately
+ */
+int
+fat_init_clusters_chain(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t start_cln
+ )
+{
+ int rc = RC_OK;
+ ssize_t ret = 0;
+ register fat_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t cur_cln = start_cln;
+ char *buf;
+
+ buf = calloc(fs_info->vol.bpc, sizeof(char));
+ if ( buf == NULL )
+ rtems_set_errno_and_return_minus_one( EIO );
+
+ while ((cur_cln & fs_info->vol.mask) < fs_info->vol.eoc_val)
+ {
+ ret = fat_cluster_write(mt_entry, cur_cln, buf);
+ if ( ret == -1 )
+ {
+ free(buf);
+ return -1;
+ }
+
+ rc = fat_get_fat_cluster(mt_entry, cur_cln, &cur_cln);
+ if ( rc != RC_OK )
+ {
+ free(buf);
+ return rc;
+ }
+
+ }
+ free(buf);
+ return rc;
+}
+
+#define FAT_UNIQ_INO_BASE 0x0FFFFF00
+
+#define FAT_UNIQ_INO_IS_BUSY(index, arr) \
+ (((arr)[((index)>>3)]>>((index) & (8-1))) & 0x01)
+
+#define FAT_SET_UNIQ_INO_BUSY(index, arr) \
+ ((arr)[((index)>>3)] |= (0x01<<((index) & (8-1))))
+
+#define FAT_SET_UNIQ_INO_FREE(index, arr) \
+ ((arr)[((index)>>3)] &= (~(0x01<<((index) & (8-1)))))
+
+/* fat_get_unique_ino --
+ * Allocate unique ino from unique ino pool
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ *
+ * RETURNS:
+ * unique inode number on success, or 0 if there is no free unique inode
+ * number in the pool
+ *
+ * ATTENTION:
+ * 0 means FAILED !!!
+ *
+ */
+uint32_t
+fat_get_unique_ino(rtems_filesystem_mount_table_entry_t *mt_entry)
+{
+ register fat_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t j = 0;
+ bool resrc_unsuff = false;
+
+ while (!resrc_unsuff)
+ {
+ for (j = 0; j < fs_info->uino_pool_size; j++)
+ {
+ if (!FAT_UNIQ_INO_IS_BUSY(fs_info->index, fs_info->uino))
+ {
+ FAT_SET_UNIQ_INO_BUSY(fs_info->index, fs_info->uino);
+ return (fs_info->uino_base + fs_info->index);
+ }
+ fs_info->index++;
+ if (fs_info->index >= fs_info->uino_pool_size)
+ fs_info->index = 0;
+ }
+
+ if ((fs_info->uino_pool_size << 1) < (0x0FFFFFFF - fs_info->uino_base))
+ {
+ fs_info->uino_pool_size <<= 1;
+ fs_info->uino = realloc(fs_info->uino, fs_info->uino_pool_size);
+ if (fs_info->uino != NULL)
+ fs_info->index = fs_info->uino_pool_size;
+ else
+ resrc_unsuff = true;
+ }
+ else
+ resrc_unsuff = true;
+ }
+ return 0;
+}
+
+/* fat_free_unique_ino --
+ * Return unique ino to unique ino pool
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * ino - inode number to free
+ *
+ * RETURNS:
+ * None
+ */
+void
+fat_free_unique_ino(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t ino
+ )
+{
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+
+ FAT_SET_UNIQ_INO_FREE((ino - fs_info->uino_base), fs_info->uino);
+}
+
+/* fat_ino_is_unique --
+ * Test whether ino is from unique ino pool
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * ino - ino to be tested
+ *
+ * RETURNS:
+ * true if ino is allocated from unique ino pool, false otherwise
+ */
+inline bool
+fat_ino_is_unique(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t ino
+ )
+{
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+
+ return (ino >= fs_info->uino_base);
+}
+
+/* fat_fat32_update_fsinfo_sector --
+ * Synchronize fsinfo sector for FAT32 volumes
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * free_count - count of free clusters
+ * next_free - the next free cluster num
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set appropriately)
+ */
+int
+fat_fat32_update_fsinfo_sector(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t free_count,
+ uint32_t next_free
+ )
+{
+ ssize_t ret1 = 0, ret2 = 0;
+ register fat_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t le_free_count = 0;
+ uint32_t le_next_free = 0;
+
+ le_free_count = CT_LE_L(free_count);
+ le_next_free = CT_LE_L(next_free);
+
+ ret1 = _fat_block_write(mt_entry,
+ fs_info->vol.info_sec,
+ FAT_FSINFO_FREE_CLUSTER_COUNT_OFFSET,
+ 4,
+ (char *)(&le_free_count));
+
+ ret2 = _fat_block_write(mt_entry,
+ fs_info->vol.info_sec,
+ FAT_FSINFO_NEXT_FREE_CLUSTER_OFFSET,
+ 4,
+ (char *)(&le_next_free));
+
+ if ( (ret1 < 0) || (ret2 < 0) )
+ return -1;
+
+ return RC_OK;
+}
diff --git a/cpukit/libfs/src/dosfs/fat.h b/cpukit/libfs/src/dosfs/fat.h
new file mode 100644
index 0000000000..8d62662c10
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/fat.h
@@ -0,0 +1,517 @@
+/*
+ * fat.h
+ *
+ * Constants/data structures/prototypes for low-level operations on a volume
+ * with FAT filesystem
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ */
+
+#ifndef __DOSFS_FAT_H__
+#define __DOSFS_FAT_H__
+
+#include <string.h>
+
+#include <rtems/seterr.h>
+
+#include <rtems/score/cpu.h>
+#include <errno.h>
+#include <rtems/bdbuf.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef RC_OK
+#define RC_OK 0
+#endif
+
+/*
+ * Remember that all FAT file system on disk data structure is
+ * "little endian"!
+ * (derived from linux)
+ */
+/*
+ * Conversion from and to little-endian byte order. (no-op on i386/i486)
+ *
+ * Naming: Ca_b_c, where a: F = from, T = to, b: LE = little-endian,
+ * BE = big-endian, c: W = word (16 bits), L = longword (32 bits)
+ */
+
+#if (CPU_BIG_ENDIAN == TRUE)
+# define CF_LE_W(v) CPU_swap_u16((uint16_t)(v))
+# define CF_LE_L(v) CPU_swap_u32((uint32_t)(v))
+# define CT_LE_W(v) CPU_swap_u16((uint16_t)(v))
+# define CT_LE_L(v) CPU_swap_u32((uint32_t)(v))
+#else
+# define CF_LE_W(v) (v)
+# define CF_LE_L(v) (v)
+# define CT_LE_W(v) (v)
+# define CT_LE_L(v) (v)
+#endif
+
+#define MIN(a, b) (((a) < (b)) ? (a) : (b))
+
+#define FAT_HASH_SIZE 2
+#define FAT_HASH_MODULE FAT_HASH_SIZE
+
+
+#define FAT_SECTOR512_SIZE 512 /* sector size (bytes) */
+#define FAT_SECTOR512_BITS 9 /* log2(SECTOR_SIZE) */
+
+/* maximum + 1 number of clusters for FAT12 */
+#define FAT_FAT12_MAX_CLN 4085
+
+/* maximum + 1 number of clusters for FAT16 */
+#define FAT_FAT16_MAX_CLN 65525
+
+#define FAT_FAT12 0x01
+#define FAT_FAT16 0x02
+#define FAT_FAT32 0x04
+
+#define FAT_UNDEFINED_VALUE (uint32_t)0xFFFFFFFF
+
+#define FAT_FAT12_EOC 0x0FF8
+#define FAT_FAT16_EOC 0xFFF8
+#define FAT_FAT32_EOC (uint32_t)0x0FFFFFF8
+
+#define FAT_FAT12_FREE 0x0000
+#define FAT_FAT16_FREE 0x0000
+#define FAT_FAT32_FREE 0x00000000
+
+#define FAT_GENFAT_EOC (uint32_t)0xFFFFFFFF
+#define FAT_GENFAT_FREE (uint32_t)0x00000000
+
+#define FAT_FAT12_SHIFT 0x04
+
+#define FAT_FAT12_MASK 0x00000FFF
+#define FAT_FAT16_MASK 0x0000FFFF
+#define FAT_FAT32_MASK (uint32_t)0x0FFFFFFF
+
+#define FAT_MAX_BPB_SIZE 90
+#define FAT_TOTAL_MBR_SIZE 512
+
+/* size of useful information in FSInfo sector */
+#define FAT_USEFUL_INFO_SIZE 12
+
+#define FAT_GET_ADDR(x, ofs) ((uint8_t *)(x) + (ofs))
+
+#define FAT_GET_VAL8(x, ofs) (uint8_t)(*((uint8_t *)(x) + (ofs)))
+
+#define FAT_GET_VAL16(x, ofs) \
+ (uint16_t)( (*((uint8_t *)(x) + (ofs))) | \
+ ((*((uint8_t *)(x) + (ofs) + 1)) << 8) )
+
+#define FAT_GET_VAL32(x, ofs) \
+ (uint32_t)( (uint32_t)(*((uint8_t *)(x) + (ofs))) | \
+ ((uint32_t)(*((uint8_t *)(x) + (ofs) + 1)) << 8) | \
+ ((uint32_t)(*((uint8_t *)(x) + (ofs) + 2)) << 16) | \
+ ((uint32_t)(*((uint8_t *)(x) + (ofs) + 3)) << 24) )
+
+#define FAT_SET_VAL8(x, ofs,val) \
+ (*((uint8_t *)(x)+(ofs))=(uint8_t)(val))
+
+#define FAT_SET_VAL16(x, ofs,val) do { \
+ FAT_SET_VAL8((x),(ofs),(val)); \
+ FAT_SET_VAL8((x),(ofs)+1,(val)>>8);\
+ } while (0)
+
+#define FAT_SET_VAL32(x, ofs,val) do { \
+ uint32_t val1 = val; \
+ FAT_SET_VAL16((x),(ofs),(val1)&0xffff);\
+ FAT_SET_VAL16((x),(ofs)+2,(val1)>>16);\
+ } while (0)
+
+/* macros to access boot sector fields */
+#define FAT_GET_BR_JMPBOOT(x) FAT_GET_VAL8( x, 0)
+#define FAT_SET_BR_JMPBOOT(x,val) FAT_SET_VAL8( x, 0,val)
+
+#define FAT_GET_ADDR_BR_OEMNAME(x) FAT_GET_ADDR( x, 3)
+#define FAT_BR_OEMNAME_SIZE (8)
+
+#define FAT_GET_BR_BYTES_PER_SECTOR(x) FAT_GET_VAL16(x, 11)
+#define FAT_SET_BR_BYTES_PER_SECTOR(x,val) FAT_SET_VAL16(x, 11,val)
+
+#define FAT_GET_BR_SECTORS_PER_CLUSTER(x) FAT_GET_VAL8( x, 13)
+#define FAT_SET_BR_SECTORS_PER_CLUSTER(x,val)FAT_SET_VAL8( x, 13,val)
+
+#define FAT_GET_BR_RESERVED_SECTORS_NUM(x) FAT_GET_VAL16(x, 14)
+#define FAT_SET_BR_RESERVED_SECTORS_NUM(x,val) FAT_SET_VAL16(x, 14,val)
+
+#define FAT_GET_BR_FAT_NUM(x) FAT_GET_VAL8( x, 16)
+#define FAT_SET_BR_FAT_NUM(x,val) FAT_SET_VAL8( x, 16,val)
+
+#define FAT_GET_BR_FILES_PER_ROOT_DIR(x) FAT_GET_VAL16(x, 17)
+#define FAT_SET_BR_FILES_PER_ROOT_DIR(x,val) FAT_SET_VAL16(x, 17,val)
+
+#define FAT_GET_BR_TOTAL_SECTORS_NUM16(x) FAT_GET_VAL16(x, 19)
+#define FAT_SET_BR_TOTAL_SECTORS_NUM16(x,val)FAT_SET_VAL16(x, 19,val)
+
+#define FAT_GET_BR_MEDIA(x) FAT_GET_VAL8( x, 21)
+#define FAT_SET_BR_MEDIA(x,val) FAT_SET_VAL8( x, 21,val)
+
+#define FAT_GET_BR_SECTORS_PER_FAT(x) FAT_GET_VAL16(x, 22)
+#define FAT_SET_BR_SECTORS_PER_FAT(x,val) FAT_SET_VAL16(x, 22,val)
+
+#define FAT_GET_BR_SECTORS_PER_TRACK(x) FAT_GET_VAL16(x, 24)
+#define FAT_SET_BR_SECTORS_PER_TRACK(x,val) FAT_SET_VAL16(x, 24,val)
+
+#define FAT_GET_BR_NUMBER_OF_HEADS(x) FAT_GET_VAL16(x, 26)
+#define FAT_SET_BR_NUMBER_OF_HEADS(x,val) FAT_SET_VAL16(x, 26,val)
+
+#define FAT_GET_BR_HIDDEN_SECTORS(x) FAT_GET_VAL32(x, 28)
+#define FAT_SET_BR_HIDDEN_SECTORS(x,val) FAT_SET_VAL32(x, 28,val)
+
+#define FAT_GET_BR_TOTAL_SECTORS_NUM32(x) FAT_GET_VAL32(x, 32)
+#define FAT_SET_BR_TOTAL_SECTORS_NUM32(x,val) FAT_SET_VAL32(x, 32,val)
+ /* --- start of FAT12/16 specific fields */
+#define FAT_GET_BR_DRVNUM(x) FAT_GET_VAL8( x, 36)
+#define FAT_SET_BR_DRVNUM(x,val) FAT_SET_VAL8( x, 36,val)
+
+#define FAT_GET_BR_RSVD1(x) FAT_GET_VAL8( x, 37)
+#define FAT_SET_BR_RSVD1(x,val) FAT_SET_VAL8( x, 37,val)
+
+#define FAT_GET_BR_BOOTSIG(x) FAT_GET_VAL8( x, 38)
+#define FAT_SET_BR_BOOTSIG(x,val) FAT_SET_VAL8( x, 38,val)
+#define FAT_BR_BOOTSIG_VAL (0x29)
+
+#define FAT_GET_BR_VOLID(x) FAT_GET_VAL32(x, 39)
+#define FAT_SET_BR_VOLID(x,val) FAT_SET_VAL32(x, 39,val)
+
+#define FAT_GET_ADDR_BR_VOLLAB(x) FAT_GET_ADDR (x, 43)
+#define FAT_BR_VOLLAB_SIZE (11)
+
+#define FAT_GET_ADDR_BR_FILSYSTYPE(x) FAT_GET_ADDR (x, 54)
+#define FAT_BR_FILSYSTYPE_SIZE (8)
+ /* --- end of FAT12/16 specific fields */
+ /* --- start of FAT32 specific fields */
+#define FAT_GET_BR_SECTORS_PER_FAT32(x) FAT_GET_VAL32(x, 36)
+#define FAT_SET_BR_SECTORS_PER_FAT32(x,val) FAT_SET_VAL32(x, 36,val)
+
+#define FAT_GET_BR_EXT_FLAGS(x) FAT_GET_VAL16(x, 40)
+#define FAT_SET_BR_EXT_FLAGS(x,val) FAT_SET_VAL16(x, 40,val)
+
+#define FAT_GET_BR_FSVER(x) FAT_GET_VAL16(x, 42)
+#define FAT_SET_BR_FSVER(x,val) FAT_SET_VAL16(x, 42,val)
+
+#define FAT_GET_BR_FAT32_ROOT_CLUSTER(x) FAT_GET_VAL32(x, 44)
+#define FAT_SET_BR_FAT32_ROOT_CLUSTER(x,val) FAT_SET_VAL32(x, 44,val)
+
+#define FAT_GET_BR_FAT32_FS_INFO_SECTOR(x) FAT_GET_VAL16(x, 48)
+#define FAT_SET_BR_FAT32_FS_INFO_SECTOR(x,val) FAT_SET_VAL16(x, 48,val)
+
+#define FAT_GET_BR_FAT32_BK_BOOT_SECTOR(x) FAT_GET_VAL16(x, 50)
+#define FAT_SET_BR_FAT32_BK_BOOT_SECTOR(x,val) FAT_SET_VAL16(x, 50,val)
+
+#define FAT_GET_ADDR_BR_FAT32_RESERVED(x) FAT_GET_ADDR (x, 52)
+#define FAT_BR_FAT32_RESERVED_SIZE (12)
+
+#define FAT_GET_BR_FAT32_DRVNUM(x) FAT_GET_VAL8( x, 64)
+#define FAT_SET_BR_FAT32_DRVNUM(x,val) FAT_SET_VAL8( x, 64,val)
+
+#define FAT_GET_BR_FAT32_RSVD1(x) FAT_GET_VAL8( x, 65)
+#define FAT_SET_BR_FAT32_RSVD1(x,val) FAT_SET_VAL8( x, 65,val)
+
+#define FAT_GET_BR_FAT32_BOOTSIG(x) FAT_GET_VAL8( x, 66)
+#define FAT_SET_BR_FAT32_BOOTSIG(x,val) FAT_SET_VAL8( x, 66,val)
+#define FAT_BR_FAT32_BOOTSIG_VAL (0x29)
+
+#define FAT_GET_BR_FAT32_VOLID(x) FAT_GET_VAL32(x, 67)
+#define FAT_SET_BR_FAT32_VOLID(x,val) FAT_SET_VAL32(x, 67,val)
+
+#define FAT_GET_ADDR_BR_FAT32_VOLLAB(x) FAT_GET_ADDR (x, 71)
+#define FAT_BR_FAT32_VOLLAB_SIZE (11)
+
+#define FAT_GET_ADDR_BR_FAT32_FILSYSTYPE(x) FAT_GET_ADDR (x, 82)
+#define FAT_BR_FAT32_FILSYSTYPE_SIZE (8)
+ /* --- end of FAT32 specific fields */
+
+#define FAT_GET_BR_SIGNATURE(x) FAT_GET_VAL16(x,510)
+#define FAT_SET_BR_SIGNATURE(x,val) FAT_SET_VAL16(x,510,val)
+#define FAT_BR_SIGNATURE_VAL (0xAA55)
+
+ /*
+ * FAT32 FSINFO description
+ */
+#define FAT_GET_FSINFO_LEAD_SIGNATURE(x) FAT_GET_VAL32(x, 0)
+#define FAT_SET_FSINFO_LEAD_SIGNATURE(x,val) FAT_SET_VAL32(x, 0,val)
+#define FAT_FSINFO_LEAD_SIGNATURE_VALUE (0x41615252)
+
+#define FAT_GET_FSINFO_STRUC_SIGNATURE(x) FAT_GET_VAL32(x,484)
+#define FAT_SET_FSINFO_STRUC_SIGNATURE(x,val) FAT_SET_VAL32(x,484,val)
+#define FAT_FSINFO_STRUC_SIGNATURE_VALUE (0x61417272)
+
+#define FAT_GET_FSINFO_TRAIL_SIGNATURE(x) FAT_GET_VAL32(x,508)
+#define FAT_SET_FSINFO_TRAIL_SIGNATURE(x,val) FAT_SET_VAL32(x,508,val)
+#define FAT_FSINFO_TRAIL_SIGNATURE_VALUE (0xAA550000)
+/*
+ * I read FSInfo sector from offset 484 to access the information, so offsets
+ * of these fields a relative
+ */
+#define FAT_GET_FSINFO_FREE_CLUSTER_COUNT(x) FAT_GET_VAL32(x, 4)
+#define FAT_SET_FSINFO_FREE_CLUSTER_COUNT(x,val) FAT_SET_VAL32(x, 4,val)
+#define FAT_GET_FSINFO_NEXT_FREE_CLUSTER(x) FAT_GET_VAL32(x, 8)
+#define FAT_SET_FSINFO_NEXT_FREE_CLUSTER(x,val) FAT_SET_VAL32(x, 8,val)
+
+#define FAT_FSI_INFO 484
+#define FAT_FSINFO_STRUCT_OFFSET 488
+#define FAT_FSINFO_FREE_CLUSTER_COUNT_OFFSET (FAT_FSINFO_STRUCT_OFFSET+0)
+
+#define FAT_FSINFO_NEXT_FREE_CLUSTER_OFFSET (FAT_FSINFO_STRUCT_OFFSET+4)
+
+#define FAT_RSRVD_CLN 0x02
+
+#define FAT_FSI_LEADSIG_SIZE 0x04
+
+#define FAT_TOTAL_FSINFO_SIZE 512
+
+#define MS_BYTES_PER_CLUSTER_LIMIT 0x8000 /* 32K */
+
+#define FAT_BR_EXT_FLAGS_MIRROR 0x0080
+
+#define FAT_BR_EXT_FLAGS_FAT_NUM 0x000F
+
+#define FAT_BR_MEDIA_FIXED 0xf8
+
+#define FAT_DIRENTRY_SIZE 32
+
+#define FAT_DIRENTRIES_PER_SEC512 16
+
+/*
+ * Volume descriptor
+ * Description of the volume the FAT filesystem is located on - generally
+ * the fields of the structure correspond to Boot Sector and BPB Structure
+ * fields
+ */
+typedef struct fat_vol_s
+{
+ uint16_t bps; /* bytes per sector */
+ uint8_t sec_log2; /* log2 of bps */
+ uint8_t sec_mul; /* log2 of 512bts sectors number per sector */
+ uint8_t spc; /* sectors per cluster */
+ uint8_t spc_log2; /* log2 of spc */
+ uint16_t bpc; /* bytes per cluster */
+ uint8_t bpc_log2; /* log2 of bytes per cluster */
+ uint8_t fats; /* number of FATs */
+ uint8_t type; /* FAT type */
+ uint32_t mask;
+ uint32_t eoc_val;
+ uint16_t fat_loc; /* FAT start */
+ uint32_t fat_length; /* sectors per FAT */
+ uint32_t rdir_loc; /* root directory start */
+ uint16_t rdir_entrs; /* files per root directory */
+ uint32_t rdir_secs; /* sectors per root directory */
+ uint32_t rdir_size; /* root directory size in bytes */
+ uint32_t tot_secs; /* total count of sectors */
+ uint32_t data_fsec; /* first data sector */
+ uint32_t data_cls; /* count of data clusters */
+ uint32_t rdir_cl; /* first cluster of the root directory */
+ uint16_t info_sec; /* FSInfo Sector Structure location */
+ uint32_t free_cls; /* last known free clusters count */
+ uint32_t next_cl; /* next free cluster number */
+ uint8_t mirror; /* mirroring enabla/disable */
+ uint32_t afat_loc; /* active FAT location */
+ uint8_t afat; /* the number of active FAT */
+ dev_t dev; /* device ID */
+ rtems_disk_device *dd; /* disk device (see libblock) */
+ void *private_data; /* reserved */
+} fat_vol_t;
+
+
+typedef struct fat_cache_s
+{
+ uint32_t blk_num;
+ bool modified;
+ uint8_t state;
+ rtems_bdbuf_buffer *buf;
+} fat_cache_t;
+
+/*
+ * This structure identifies the instance of the filesystem on the FAT
+ * ("fat-file") level.
+ */
+typedef struct fat_fs_info_s
+{
+ fat_vol_t vol; /* volume descriptor */
+ rtems_chain_control *vhash; /* "vhash" of fat-file descriptors */
+ rtems_chain_control *rhash; /* "rhash" of fat-file descriptors */
+ char *uino; /* array of unique ino numbers */
+ uint32_t index;
+ uint32_t uino_pool_size; /* size */
+ uint32_t uino_base;
+ fat_cache_t c; /* cache */
+ uint8_t *sec_buf; /* just placeholder for anything */
+} fat_fs_info_t;
+
+/*
+ * FAT position is a the cluster and the offset into the
+ * cluster.
+ */
+typedef struct fat_pos_s
+{
+ uint32_t cln;
+ uint32_t ofs;
+} fat_pos_t;
+
+/*
+ * If the name we looking for is file we store not only first data cluster
+ * number, but and cluster number and offset for directory entry for this
+ * name. We also add the LFN start offset so we can delete it the whole
+ * file name. We can then use this to delete the file.
+ */
+typedef struct fat_dir_pos_s
+{
+ fat_pos_t sname;
+ fat_pos_t lname;
+} fat_dir_pos_t;
+
+/*
+ * Set the long name entries to this value for a short file name.
+ */
+#define FAT_FILE_SHORT_NAME (0xffffffff)
+
+#define FAT_FAT_OFFSET(fat_type, cln) \
+ ((fat_type) & FAT_FAT12 ? ((cln) + ((cln) >> 1)) : \
+ (fat_type) & FAT_FAT16 ? ((cln) << 1) : \
+ ((cln) << 2))
+
+#define FAT_CLUSTER_IS_ODD(n) ((n) & 0x0001)
+
+#define FAT12_SHIFT 0x4 /* half of a byte */
+
+/* initial size of array of unique ino */
+#define FAT_UINO_POOL_INIT_SIZE 0x100
+
+/* cache support */
+#define FAT_CACHE_EMPTY 0x0
+#define FAT_CACHE_ACTUAL 0x1
+
+#define FAT_OP_TYPE_READ 0x1
+#define FAT_OP_TYPE_GET 0x2
+
+static inline void
+fat_dir_pos_init(
+ fat_dir_pos_t *dir_pos
+ )
+{
+ dir_pos->sname.cln = 0;
+ dir_pos->sname.ofs = 0;
+ dir_pos->lname.cln = FAT_FILE_SHORT_NAME;
+ dir_pos->lname.ofs = FAT_FILE_SHORT_NAME;
+}
+
+static inline uint32_t
+fat_cluster_num_to_sector_num(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t cln
+ )
+{
+ register fat_fs_info_t *fs_info = mt_entry->fs_info;
+
+ if ( (cln == 0) && (fs_info->vol.type & (FAT_FAT12 | FAT_FAT16)) )
+ return fs_info->vol.rdir_loc;
+
+ return (((cln - FAT_RSRVD_CLN) << fs_info->vol.spc_log2) +
+ fs_info->vol.data_fsec);
+}
+
+static inline uint32_t
+fat_cluster_num_to_sector512_num(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t cln
+ )
+{
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+
+ if (cln == 1)
+ return 1;
+
+ return (fat_cluster_num_to_sector_num(mt_entry, cln) <<
+ fs_info->vol.sec_mul);
+}
+
+static inline void
+fat_buf_mark_modified(fat_fs_info_t *fs_info)
+{
+ fs_info->c.modified = true;
+}
+
+int
+fat_buf_access(fat_fs_info_t *fs_info, uint32_t blk, int op_type,
+ rtems_bdbuf_buffer **buf);
+
+int
+fat_buf_release(fat_fs_info_t *fs_info);
+
+ssize_t
+_fat_block_read(rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t start,
+ uint32_t offset,
+ uint32_t count,
+ void *buff);
+
+ssize_t
+_fat_block_write(rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t start,
+ uint32_t offset,
+ uint32_t count,
+ const void *buff);
+
+int
+_fat_block_release(rtems_filesystem_mount_table_entry_t *mt_entry);
+
+ssize_t
+fat_cluster_read(rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t cln,
+ void *buff);
+
+ssize_t
+fat_cluster_write(rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t cln,
+ const void *buff);
+
+int
+fat_init_volume_info(rtems_filesystem_mount_table_entry_t *mt_entry);
+
+int
+fat_init_clusters_chain(rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t start_cln);
+
+uint32_t
+fat_cluster_num_to_sector_num(rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t cln);
+
+int
+fat_shutdown_drive(rtems_filesystem_mount_table_entry_t *mt_entry);
+
+
+uint32_t
+fat_get_unique_ino(rtems_filesystem_mount_table_entry_t *mt_entry);
+
+bool
+fat_ino_is_unique(rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t ino);
+
+void
+fat_free_unique_ino(rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t ino);
+
+int
+fat_fat32_update_fsinfo_sector(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t free_count,
+ uint32_t next_free
+ );
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __DOSFS_FAT_H__ */
diff --git a/cpukit/libfs/src/dosfs/fat_fat_operations.c b/cpukit/libfs/src/dosfs/fat_fat_operations.c
new file mode 100644
index 0000000000..c4f82805e8
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/fat_fat_operations.c
@@ -0,0 +1,436 @@
+/*
+ * fat_fat_operations.c
+ *
+ * General operations on File Allocation Table
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * @(#) $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+
+#include <rtems/libio_.h>
+
+#include "fat.h"
+#include "fat_fat_operations.h"
+
+/* fat_scan_fat_for_free_clusters --
+ * Allocate chain of free clusters from Files Allocation Table
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * chain - the number of the first allocated cluster (first cluster
+ * in the chain)
+ * count - count of clusters to allocate (chain length)
+ *
+ * RETURNS:
+ * RC_OK on success, or error code if error occured (errno set
+ * appropriately)
+ *
+ *
+ */
+int
+fat_scan_fat_for_free_clusters(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t *chain,
+ uint32_t count,
+ uint32_t *cls_added,
+ uint32_t *last_cl
+ )
+{
+ int rc = RC_OK;
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t cl4find = 2;
+ uint32_t next_cln = 0;
+ uint32_t save_cln = 0;
+ uint32_t data_cls_val = fs_info->vol.data_cls + 2;
+ uint32_t i = 2;
+
+ *cls_added = 0;
+
+ if (count == 0)
+ return rc;
+
+ if (fs_info->vol.next_cl != FAT_UNDEFINED_VALUE)
+ cl4find = fs_info->vol.next_cl;
+
+ /*
+ * fs_info->vol.data_cls is exactly the count of data clusters
+ * starting at cluster 2, so the maximum valid cluster number is
+ * (fs_info->vol.data_cls + 1)
+ */
+ while (i < data_cls_val)
+ {
+ rc = fat_get_fat_cluster(mt_entry, cl4find, &next_cln);
+ if ( rc != RC_OK )
+ {
+ if (*cls_added != 0)
+ fat_free_fat_clusters_chain(mt_entry, (*chain));
+ return rc;
+ }
+
+ if (next_cln == FAT_GENFAT_FREE)
+ {
+ /*
+ * We are enforced to process allocation of the first free cluster
+ * by separate 'if' statement because otherwise undo function
+ * wouldn't work properly
+ */
+ if (*cls_added == 0)
+ {
+ *chain = cl4find;
+ rc = fat_set_fat_cluster(mt_entry, cl4find, FAT_GENFAT_EOC);
+ if ( rc != RC_OK )
+ {
+ /*
+ * this is the first cluster we tried to allocate so no
+ * cleanup activity needed
+ */
+ return rc;
+ }
+ }
+ else
+ {
+ /* set EOC value to new allocated cluster */
+ rc = fat_set_fat_cluster(mt_entry, cl4find, FAT_GENFAT_EOC);
+ if ( rc != RC_OK )
+ {
+ /* cleanup activity */
+ fat_free_fat_clusters_chain(mt_entry, (*chain));
+ return rc;
+ }
+
+ rc = fat_set_fat_cluster(mt_entry, save_cln, cl4find);
+ if ( rc != RC_OK )
+ {
+ /* cleanup activity */
+ fat_free_fat_clusters_chain(mt_entry, (*chain));
+ /* trying to save last allocated cluster for future use */
+ fat_set_fat_cluster(mt_entry, cl4find, FAT_GENFAT_FREE);
+ fat_buf_release(fs_info);
+ return rc;
+ }
+ }
+
+ save_cln = cl4find;
+ (*cls_added)++;
+
+ /* have we satisfied request ? */
+ if (*cls_added == count)
+ {
+ fs_info->vol.next_cl = save_cln;
+ if (fs_info->vol.free_cls != 0xFFFFFFFF)
+ fs_info->vol.free_cls -= (*cls_added);
+ *last_cl = save_cln;
+ fat_buf_release(fs_info);
+ return rc;
+ }
+ }
+ i++;
+ cl4find++;
+ if (cl4find >= data_cls_val)
+ cl4find = 2;
+ }
+
+ fs_info->vol.next_cl = save_cln;
+ if (fs_info->vol.free_cls != 0xFFFFFFFF)
+ fs_info->vol.free_cls -= (*cls_added);
+
+ *last_cl = save_cln;
+ fat_buf_release(fs_info);
+ return RC_OK;
+}
+
+/* fat_free_fat_clusters_chain --
+ * Free chain of clusters in Files Allocation Table.
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * chain - number of the first cluster in the chain
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set appropriately)
+ */
+int
+fat_free_fat_clusters_chain(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t chain
+ )
+{
+ int rc = RC_OK, rc1 = RC_OK;
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t cur_cln = chain;
+ uint32_t next_cln = 0;
+ uint32_t freed_cls_cnt = 0;
+
+ while ((cur_cln & fs_info->vol.mask) < fs_info->vol.eoc_val)
+ {
+ rc = fat_get_fat_cluster(mt_entry, cur_cln, &next_cln);
+ if ( rc != RC_OK )
+ {
+ if(fs_info->vol.free_cls != FAT_UNDEFINED_VALUE)
+ fs_info->vol.free_cls += freed_cls_cnt;
+
+ fat_buf_release(fs_info);
+ return rc;
+ }
+
+ rc = fat_set_fat_cluster(mt_entry, cur_cln, FAT_GENFAT_FREE);
+ if ( rc != RC_OK )
+ rc1 = rc;
+
+ freed_cls_cnt++;
+ cur_cln = next_cln;
+ }
+
+ fs_info->vol.next_cl = chain;
+ if (fs_info->vol.free_cls != FAT_UNDEFINED_VALUE)
+ fs_info->vol.free_cls += freed_cls_cnt;
+
+ fat_buf_release(fs_info);
+ if (rc1 != RC_OK)
+ return rc1;
+
+ return RC_OK;
+}
+
+/* fat_get_fat_cluster --
+ * Fetches the contents of the cluster (link to next cluster in the chain)
+ * from Files Allocation Table.
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * cln - number of cluster to fetch the contents from
+ * ret_val - contents of the cluster 'cln' (link to next cluster in
+ * the chain)
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured
+ * and errno set appropriately
+ */
+int
+fat_get_fat_cluster(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t cln,
+ uint32_t *ret_val
+ )
+{
+ int rc = RC_OK;
+ register fat_fs_info_t *fs_info = mt_entry->fs_info;
+ rtems_bdbuf_buffer *block0 = NULL;
+ uint32_t sec = 0;
+ uint32_t ofs = 0;
+
+ /* sanity check */
+ if ( (cln < 2) || (cln > (fs_info->vol.data_cls + 1)) )
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ sec = (FAT_FAT_OFFSET(fs_info->vol.type, cln) >> fs_info->vol.sec_log2) +
+ fs_info->vol.afat_loc;
+ ofs = FAT_FAT_OFFSET(fs_info->vol.type, cln) & (fs_info->vol.bps - 1);
+
+ rc = fat_buf_access(fs_info, sec, FAT_OP_TYPE_READ, &block0);
+ if (rc != RC_OK)
+ return rc;
+
+ switch ( fs_info->vol.type )
+ {
+ case FAT_FAT12:
+ /*
+ * we are enforced in complex computations for FAT12 to escape CPU
+ * align problems for some architectures
+ */
+ *ret_val = (*((uint8_t *)(block0->buffer + ofs)));
+ if ( ofs == (fs_info->vol.bps - 1) )
+ {
+ rc = fat_buf_access(fs_info, sec + 1, FAT_OP_TYPE_READ,
+ &block0);
+ if (rc != RC_OK)
+ return rc;
+
+ *ret_val |= (*((uint8_t *)(block0->buffer)))<<8;
+ }
+ else
+ {
+ *ret_val |= (*((uint8_t *)(block0->buffer + ofs + 1)))<<8;
+ }
+
+ if ( FAT_CLUSTER_IS_ODD(cln) )
+ *ret_val = (*ret_val) >> FAT12_SHIFT;
+ else
+ *ret_val = (*ret_val) & FAT_FAT12_MASK;
+ break;
+
+ case FAT_FAT16:
+ *ret_val = *((uint16_t *)(block0->buffer + ofs));
+ *ret_val = CF_LE_W(*ret_val);
+ break;
+
+ case FAT_FAT32:
+ *ret_val = *((uint32_t *)(block0->buffer + ofs));
+ *ret_val = CF_LE_L(*ret_val);
+ break;
+
+ default:
+ rtems_set_errno_and_return_minus_one(EIO);
+ break;
+ }
+
+ return RC_OK;
+}
+
+/* fat_set_fat_cluster --
+ * Set the contents of the cluster (link to next cluster in the chain)
+ * from Files Allocation Table.
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * cln - number of cluster to set contents to
+ * in_val - value to set
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured
+ * and errno set appropriately
+ */
+int
+fat_set_fat_cluster(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t cln,
+ uint32_t in_val
+ )
+{
+ int rc = RC_OK;
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t sec = 0;
+ uint32_t ofs = 0;
+ uint16_t fat16_clv = 0;
+ uint32_t fat32_clv = 0;
+ rtems_bdbuf_buffer *block0 = NULL;
+
+ /* sanity check */
+ if ( (cln < 2) || (cln > (fs_info->vol.data_cls + 1)) )
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ sec = (FAT_FAT_OFFSET(fs_info->vol.type, cln) >> fs_info->vol.sec_log2) +
+ fs_info->vol.afat_loc;
+ ofs = FAT_FAT_OFFSET(fs_info->vol.type, cln) & (fs_info->vol.bps - 1);
+
+ rc = fat_buf_access(fs_info, sec, FAT_OP_TYPE_READ, &block0);
+ if (rc != RC_OK)
+ return rc;
+
+ switch ( fs_info->vol.type )
+ {
+ case FAT_FAT12:
+ if ( FAT_CLUSTER_IS_ODD(cln) )
+ {
+ fat16_clv = ((uint16_t )in_val) << FAT_FAT12_SHIFT;
+ *((uint8_t *)(block0->buffer + ofs)) =
+ (*((uint8_t *)(block0->buffer + ofs))) & 0x0F;
+
+ *((uint8_t *)(block0->buffer + ofs)) =
+ (*((uint8_t *)(block0->buffer + ofs))) |
+ (uint8_t )(fat16_clv & 0x00FF);
+
+ fat_buf_mark_modified(fs_info);
+
+ if ( ofs == (fs_info->vol.bps - 1) )
+ {
+ rc = fat_buf_access(fs_info, sec + 1, FAT_OP_TYPE_READ,
+ &block0);
+ if (rc != RC_OK)
+ return rc;
+
+ *((uint8_t *)(block0->buffer)) &= 0x00;
+
+ *((uint8_t *)(block0->buffer)) =
+ (*((uint8_t *)(block0->buffer))) |
+ (uint8_t )((fat16_clv & 0xFF00)>>8);
+
+ fat_buf_mark_modified(fs_info);
+ }
+ else
+ {
+ *((uint8_t *)(block0->buffer + ofs + 1)) &= 0x00;
+
+ *((uint8_t *)(block0->buffer + ofs + 1)) =
+ (*((uint8_t *)(block0->buffer + ofs + 1))) |
+ (uint8_t )((fat16_clv & 0xFF00)>>8);
+ }
+ }
+ else
+ {
+ fat16_clv = ((uint16_t )in_val) & FAT_FAT12_MASK;
+ *((uint8_t *)(block0->buffer + ofs)) &= 0x00;
+
+ *((uint8_t *)(block0->buffer + ofs)) =
+ (*((uint8_t *)(block0->buffer + ofs))) |
+ (uint8_t )(fat16_clv & 0x00FF);
+
+ fat_buf_mark_modified(fs_info);
+
+ if ( ofs == (fs_info->vol.bps - 1) )
+ {
+ rc = fat_buf_access(fs_info, sec + 1, FAT_OP_TYPE_READ,
+ &block0);
+ if (rc != RC_OK)
+ return rc;
+
+ *((uint8_t *)(block0->buffer)) =
+ (*((uint8_t *)(block0->buffer))) & 0xF0;
+
+ *((uint8_t *)(block0->buffer)) =
+ (*((uint8_t *)(block0->buffer))) |
+ (uint8_t )((fat16_clv & 0xFF00)>>8);
+
+ fat_buf_mark_modified(fs_info);
+ }
+ else
+ {
+ *((uint8_t *)(block0->buffer + ofs + 1)) =
+ (*((uint8_t *)(block0->buffer + ofs + 1))) & 0xF0;
+
+ *((uint8_t *)(block0->buffer + ofs+1)) =
+ (*((uint8_t *)(block0->buffer + ofs+1))) |
+ (uint8_t )((fat16_clv & 0xFF00)>>8);
+ }
+ }
+ break;
+
+ case FAT_FAT16:
+ *((uint16_t *)(block0->buffer + ofs)) =
+ (uint16_t )(CT_LE_W(in_val));
+ fat_buf_mark_modified(fs_info);
+ break;
+
+ case FAT_FAT32:
+ fat32_clv = CT_LE_L((in_val & FAT_FAT32_MASK));
+
+ *((uint32_t *)(block0->buffer + ofs)) =
+ (*((uint32_t *)(block0->buffer + ofs))) & (CT_LE_L(0xF0000000));
+
+ *((uint32_t *)(block0->buffer + ofs)) =
+ fat32_clv | (*((uint32_t *)(block0->buffer + ofs)));
+
+ fat_buf_mark_modified(fs_info);
+ break;
+
+ default:
+ rtems_set_errno_and_return_minus_one(EIO);
+ break;
+
+ }
+
+ return RC_OK;
+}
diff --git a/cpukit/libfs/src/dosfs/fat_fat_operations.h b/cpukit/libfs/src/dosfs/fat_fat_operations.h
new file mode 100644
index 0000000000..d516057ad0
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/fat_fat_operations.h
@@ -0,0 +1,59 @@
+/*
+ * fat_fat_operations.h
+ *
+ * Constants/data structures/prototypes for operations on Files Allocation
+ * Table
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ */
+#ifndef __DOSFS_FAT_FAT_OPERATIONS_H__
+#define __DOSFS_FAT_FAT_OPERATIONS_H__
+
+#include <rtems.h>
+#include <rtems/libio_.h>
+
+#include <rtems/bdbuf.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "fat.h"
+
+int
+fat_get_fat_cluster(rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t cln,
+ uint32_t *ret_val);
+
+int
+fat_set_fat_cluster(rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t cln,
+ uint32_t in_val);
+
+int
+fat_scan_fat_for_free_clusters(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t *chain,
+ uint32_t count,
+ uint32_t *cls_added,
+ uint32_t *last_cl
+);
+
+int
+fat_free_fat_clusters_chain(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t chain
+);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __DOSFS_FAT_FAT_OPERATIONS_H__ */
diff --git a/cpukit/libfs/src/dosfs/fat_file.c b/cpukit/libfs/src/dosfs/fat_file.c
new file mode 100644
index 0000000000..9a3ad6677e
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/fat_file.c
@@ -0,0 +1,993 @@
+/*
+ * fat_file.c
+ *
+ * General operations on "fat-file"
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * @(#) $Id$
+ *
+ */
+
+#define MSDOS_TRACE 1
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <time.h>
+
+#include <rtems/libio_.h>
+
+#include "fat.h"
+#include "fat_fat_operations.h"
+#include "fat_file.h"
+
+static inline void
+_hash_insert(rtems_chain_control *hash, uint32_t key1, uint32_t key2,
+ fat_file_fd_t *el);
+
+static inline void
+_hash_delete(rtems_chain_control *hash, uint32_t key1, uint32_t key2,
+ fat_file_fd_t *el);
+
+static inline int
+_hash_search(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ rtems_chain_control *hash,
+ uint32_t key1,
+ uint32_t key2,
+ fat_file_fd_t **ret
+);
+
+static off_t
+fat_file_lseek(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ uint32_t file_cln,
+ uint32_t *disk_cln
+);
+
+/* fat_file_open --
+ * Open fat-file. Two hash tables are accessed by key
+ * constructed from cluster num and offset of the node (i.e.
+ * files/directories are distinguished by location on the disk).
+ * First, hash table("vhash") consists of fat-file descriptors corresponded
+ * to "valid" files is accessed. Search is made by 2 fields equal to key
+ * constructed. If descriptor is found in the "vhash" - return it.
+ * Otherwise search is made in hash table("rhash") consits of fat-file
+ * descriptors corresponded to "removed-but-still-open" files with the
+ * same keys.
+ * If search failed, new fat-file descriptor is added to "vhash"
+ * with both key fields equal to constructed key. Otherwise new fat-file
+ * descriptor is added to "vhash" with first key field equal to key
+ * constructed and the second equal to an unique (unique among all values
+ * of second key fields) value.
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * pos - cluster and offset of the node
+ * fat_fd - placeholder for returned fat-file descriptor
+ *
+ * RETURNS:
+ * RC_OK and pointer to opened descriptor on success, or -1 if error
+ * occured (errno set appropriately)
+ */
+int
+fat_file_open(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_dir_pos_t *dir_pos,
+ fat_file_fd_t **fat_fd
+ )
+{
+ int rc = RC_OK;
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ fat_file_fd_t *lfat_fd = NULL;
+ uint32_t key = 0;
+
+ /* construct key */
+ key = fat_construct_key(mt_entry, &dir_pos->sname);
+
+ /* access "valid" hash table */
+ rc = _hash_search(mt_entry, fs_info->vhash, key, 0, &lfat_fd);
+ if ( rc == RC_OK )
+ {
+ /* return pointer to fat_file_descriptor allocated before */
+ (*fat_fd) = lfat_fd;
+ lfat_fd->links_num++;
+ return rc;
+ }
+
+ /* access "removed-but-still-open" hash table */
+ rc = _hash_search(mt_entry, fs_info->rhash, key, key, &lfat_fd);
+
+ lfat_fd = (*fat_fd) = (fat_file_fd_t*)malloc(sizeof(fat_file_fd_t));
+ if ( lfat_fd == NULL )
+ rtems_set_errno_and_return_minus_one( ENOMEM );
+
+ memset(lfat_fd, 0, sizeof(fat_file_fd_t));
+
+ lfat_fd->links_num = 1;
+ lfat_fd->flags &= ~FAT_FILE_REMOVED;
+ lfat_fd->map.last_cln = FAT_UNDEFINED_VALUE;
+
+ lfat_fd->dir_pos = *dir_pos;
+
+ if ( rc != RC_OK )
+ lfat_fd->ino = key;
+ else
+ {
+ lfat_fd->ino = fat_get_unique_ino(mt_entry);
+
+ if ( lfat_fd->ino == 0 )
+ {
+ free((*fat_fd));
+ /*
+ * XXX: kernel resource is unsufficient, but not the memory,
+ * but there is no suitable errno :(
+ */
+ rtems_set_errno_and_return_minus_one( ENOMEM );
+ }
+ }
+ _hash_insert(fs_info->vhash, key, lfat_fd->ino, lfat_fd);
+
+ /*
+ * other fields of fat-file descriptor will be initialized on upper
+ * level
+ */
+
+ return RC_OK;
+}
+
+
+/* fat_file_reopen --
+ * Increment by 1 number of links
+ *
+ * PARAMETERS:
+ * fat_fd - fat-file descriptor
+ *
+ * RETURNS:
+ * RC_OK
+ */
+int
+fat_file_reopen(fat_file_fd_t *fat_fd)
+{
+ fat_fd->links_num++;
+ return RC_OK;
+}
+
+/* fat_file_close --
+ * Close fat-file. If count of links to fat-file
+ * descriptor is greater than 1 (i.e. somebody esle holds pointer
+ * to this descriptor) just decrement it. Otherwise
+ * do the following. If this descriptor corresponded to removed fat-file
+ * then free clusters contained fat-file data, delete descriptor from
+ * "rhash" table and free memory allocated by descriptor. If descriptor
+ * correspondes to non-removed fat-file and 'ino' field has value from
+ * unique inode numbers pool then set count of links to descriptor to zero
+ * and leave it in hash, otherwise delete descriptor from "vhash" and free
+ * memory allocated by the descriptor
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * fat_fd - fat-file descriptor
+ *
+ * RETURNS:
+ * RC_OK, or -1 if error occured (errno set appropriately)
+ */
+int
+fat_file_close(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd
+ )
+{
+ int rc = RC_OK;
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t key = 0;
+
+ /*
+ * if links_num field of fat-file descriptor is greater than 1
+ * decrement the count of links and return
+ */
+ if (fat_fd->links_num > 1)
+ {
+ fat_fd->links_num--;
+ return rc;
+ }
+
+ key = fat_construct_key(mt_entry, &fat_fd->dir_pos.sname);
+
+ if (fat_fd->flags & FAT_FILE_REMOVED)
+ {
+ rc = fat_file_truncate(mt_entry, fat_fd, 0);
+ if ( rc != RC_OK )
+ return rc;
+
+ _hash_delete(fs_info->rhash, key, fat_fd->ino, fat_fd);
+
+ if ( fat_ino_is_unique(mt_entry, fat_fd->ino) )
+ fat_free_unique_ino(mt_entry, fat_fd->ino);
+
+ free(fat_fd);
+ }
+ else
+ {
+ if (fat_ino_is_unique(mt_entry, fat_fd->ino))
+ {
+ fat_fd->links_num = 0;
+ }
+ else
+ {
+ _hash_delete(fs_info->vhash, key, fat_fd->ino, fat_fd);
+ free(fat_fd);
+ }
+ }
+ /*
+ * flush any modified "cached" buffer back to disk
+ */
+ rc = fat_buf_release(fs_info);
+
+ return rc;
+}
+
+/* fat_file_read --
+ * Read 'count' bytes from 'start' position from fat-file. This
+ * interface hides the architecture of fat-file, represents it as
+ * linear file
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * fat_fd - fat-file descriptor
+ * start - offset in fat-file (in bytes) to read from
+ * count - count of bytes to read
+ * buf - buffer provided by user
+ *
+ * RETURNS:
+ * the number of bytes read on success, or -1 if error occured (errno
+ * set appropriately)
+ */
+ssize_t
+fat_file_read(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ uint32_t start,
+ uint32_t count,
+ uint8_t *buf
+)
+{
+ int rc = RC_OK;
+ ssize_t ret = 0;
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t cmpltd = 0;
+ uint32_t cur_cln = 0;
+ uint32_t cl_start = 0;
+ uint32_t save_cln = 0;
+ uint32_t ofs = 0;
+ uint32_t save_ofs;
+ uint32_t sec = 0;
+ uint32_t byte = 0;
+ uint32_t c = 0;
+
+ /* it couldn't be removed - otherwise cache update will be broken */
+ if (count == 0)
+ return cmpltd;
+
+ /*
+ * >= because start is offset and computed from 0 and file_size
+ * computed from 1
+ */
+ if ( start >= fat_fd->fat_file_size )
+ return FAT_EOF;
+
+ if ((count > fat_fd->fat_file_size) ||
+ (start > fat_fd->fat_file_size - count))
+ count = fat_fd->fat_file_size - start;
+
+ if ((FAT_FD_OF_ROOT_DIR(fat_fd)) &&
+ (fs_info->vol.type & (FAT_FAT12 | FAT_FAT16)))
+ {
+ sec = fat_cluster_num_to_sector_num(mt_entry, fat_fd->cln);
+ sec += (start >> fs_info->vol.sec_log2);
+ byte = start & (fs_info->vol.bps - 1);
+
+ ret = _fat_block_read(mt_entry, sec, byte, count, buf);
+ if ( ret < 0 )
+ return -1;
+
+ return ret;
+ }
+
+ cl_start = start >> fs_info->vol.bpc_log2;
+ save_ofs = ofs = start & (fs_info->vol.bpc - 1);
+
+ rc = fat_file_lseek(mt_entry, fat_fd, cl_start, &cur_cln);
+ if (rc != RC_OK)
+ return rc;
+
+ while (count > 0)
+ {
+ c = MIN(count, (fs_info->vol.bpc - ofs));
+
+ sec = fat_cluster_num_to_sector_num(mt_entry, cur_cln);
+ sec += (ofs >> fs_info->vol.sec_log2);
+ byte = ofs & (fs_info->vol.bps - 1);
+
+ ret = _fat_block_read(mt_entry, sec, byte, c, buf + cmpltd);
+ if ( ret < 0 )
+ return -1;
+
+ count -= c;
+ cmpltd += c;
+ save_cln = cur_cln;
+ rc = fat_get_fat_cluster(mt_entry, cur_cln, &cur_cln);
+ if ( rc != RC_OK )
+ return rc;
+
+ ofs = 0;
+ }
+
+ /* update cache */
+ /* XXX: check this - I'm not sure :( */
+ fat_fd->map.file_cln = cl_start +
+ ((save_ofs + cmpltd - 1) >> fs_info->vol.bpc_log2);
+ fat_fd->map.disk_cln = save_cln;
+
+ return cmpltd;
+}
+
+/* fat_file_write --
+ * Write 'count' bytes of data from user supplied buffer to fat-file
+ * starting at offset 'start'. This interface hides the architecture
+ * of fat-file, represents it as linear file
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * fat_fd - fat-file descriptor
+ * start - offset(in bytes) to write from
+ * count - count
+ * buf - buffer provided by user
+ *
+ * RETURNS:
+ * number of bytes actually written to the file on success, or -1 if
+ * error occured (errno set appropriately)
+ */
+ssize_t
+fat_file_write(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ uint32_t start,
+ uint32_t count,
+ const uint8_t *buf
+ )
+{
+ int rc = 0;
+ ssize_t ret = 0;
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t cmpltd = 0;
+ uint32_t cur_cln = 0;
+ uint32_t save_cln = 0; /* FIXME: This might be incorrect, cf. below */
+ uint32_t cl_start = 0;
+ uint32_t ofs = 0;
+ uint32_t save_ofs;
+ uint32_t sec = 0;
+ uint32_t byte = 0;
+ uint32_t c = 0;
+
+ if ( count == 0 )
+ return cmpltd;
+
+ if ( start > fat_fd->fat_file_size )
+ rtems_set_errno_and_return_minus_one( EIO );
+
+ if ((count > fat_fd->size_limit) ||
+ (start > fat_fd->size_limit - count))
+ rtems_set_errno_and_return_minus_one( EIO );
+
+ rc = fat_file_extend(mt_entry, fat_fd, start + count, &c);
+ if (rc != RC_OK)
+ return rc;
+
+ /*
+ * check whether there was enough room on device to locate
+ * file of 'start + count' bytes
+ */
+ if (c != (start + count))
+ count = c - start;
+
+ if ((FAT_FD_OF_ROOT_DIR(fat_fd)) &&
+ (fs_info->vol.type & (FAT_FAT12 | FAT_FAT16)))
+ {
+ sec = fat_cluster_num_to_sector_num(mt_entry, fat_fd->cln);
+ sec += (start >> fs_info->vol.sec_log2);
+ byte = start & (fs_info->vol.bps - 1);
+
+ ret = _fat_block_write(mt_entry, sec, byte, count, buf);
+ if ( ret < 0 )
+ return -1;
+
+ return ret;
+ }
+
+ cl_start = start >> fs_info->vol.bpc_log2;
+ save_ofs = ofs = start & (fs_info->vol.bpc - 1);
+
+ rc = fat_file_lseek(mt_entry, fat_fd, cl_start, &cur_cln);
+ if (rc != RC_OK)
+ return rc;
+
+ while (count > 0)
+ {
+ c = MIN(count, (fs_info->vol.bpc - ofs));
+
+ sec = fat_cluster_num_to_sector_num(mt_entry, cur_cln);
+ sec += (ofs >> fs_info->vol.sec_log2);
+ byte = ofs & (fs_info->vol.bps - 1);
+
+ ret = _fat_block_write(mt_entry, sec, byte, c, buf + cmpltd);
+ if ( ret < 0 )
+ return -1;
+
+ count -= c;
+ cmpltd += c;
+ save_cln = cur_cln;
+ rc = fat_get_fat_cluster(mt_entry, cur_cln, &cur_cln);
+ if ( rc != RC_OK )
+ return rc;
+
+ ofs = 0;
+ }
+
+ /* update cache */
+ /* XXX: check this - I'm not sure :( */
+ fat_fd->map.file_cln = cl_start +
+ ((save_ofs + cmpltd - 1) >> fs_info->vol.bpc_log2);
+ fat_fd->map.disk_cln = save_cln;
+
+ return cmpltd;
+}
+
+/* fat_file_extend --
+ * Extend fat-file. If new length less than current fat-file size -
+ * do nothing. Otherwise calculate necessary count of clusters to add,
+ * allocate it and add new clusters chain to the end of
+ * existing clusters chain.
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * fat_fd - fat-file descriptor
+ * new_length - new length
+ * a_length - placeholder for result - actual new length of file
+ *
+ * RETURNS:
+ * RC_OK and new length of file on success, or -1 if error occured (errno
+ * set appropriately)
+ */
+int
+fat_file_extend(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ uint32_t new_length,
+ uint32_t *a_length
+ )
+{
+ int rc = RC_OK;
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t chain = 0;
+ uint32_t bytes2add = 0;
+ uint32_t cls2add = 0;
+ uint32_t old_last_cl;
+ uint32_t last_cl = 0;
+ uint32_t bytes_remain = 0;
+ uint32_t cls_added;
+
+ *a_length = new_length;
+
+ if (new_length <= fat_fd->fat_file_size)
+ return RC_OK;
+
+ if ((FAT_FD_OF_ROOT_DIR(fat_fd)) &&
+ (fs_info->vol.type & (FAT_FAT12 | FAT_FAT16)))
+ rtems_set_errno_and_return_minus_one( ENOSPC );
+
+ bytes_remain = (fs_info->vol.bpc -
+ (fat_fd->fat_file_size & (fs_info->vol.bpc - 1))) &
+ (fs_info->vol.bpc - 1);
+
+ bytes2add = new_length - fat_fd->fat_file_size;
+
+ if (bytes2add > bytes_remain)
+ bytes2add -= bytes_remain;
+ else
+ bytes2add = 0;
+
+ /*
+ * if in last cluster allocated for the file there is enough room to
+ * handle extention (hence we don't need to add even one cluster to the
+ * file ) - return
+ */
+ if (bytes2add == 0)
+ return RC_OK;
+
+ cls2add = ((bytes2add - 1) >> fs_info->vol.bpc_log2) + 1;
+
+ rc = fat_scan_fat_for_free_clusters(mt_entry, &chain, cls2add,
+ &cls_added, &last_cl);
+
+ /* this means that low level I/O error occured */
+ if (rc != RC_OK)
+ return rc;
+
+ /* this means that no space left on device */
+ if ((cls_added == 0) && (bytes_remain == 0))
+ rtems_set_errno_and_return_minus_one(ENOSPC);
+
+ /* check wether we satisfied request for 'cls2add' clusters */
+ if (cls2add != cls_added)
+ *a_length = new_length -
+ ((cls2add - cls_added - 1) << fs_info->vol.bpc_log2) -
+ (bytes2add & (fs_info->vol.bpc - 1));
+
+ /* add new chain to the end of existed */
+ if ( fat_fd->fat_file_size == 0 )
+ {
+ fat_fd->map.disk_cln = fat_fd->cln = chain;
+ fat_fd->map.file_cln = 0;
+ }
+ else
+ {
+ if (fat_fd->map.last_cln != FAT_UNDEFINED_VALUE)
+ {
+ old_last_cl = fat_fd->map.last_cln;
+ }
+ else
+ {
+ rc = fat_file_ioctl(mt_entry, fat_fd, F_CLU_NUM,
+ (fat_fd->fat_file_size - 1), &old_last_cl);
+ if ( rc != RC_OK )
+ {
+ fat_free_fat_clusters_chain(mt_entry, chain);
+ return rc;
+ }
+ }
+
+ rc = fat_set_fat_cluster(mt_entry, old_last_cl, chain);
+ if ( rc != RC_OK )
+ {
+ fat_free_fat_clusters_chain(mt_entry, chain);
+ return rc;
+ }
+ fat_buf_release(fs_info);
+ }
+
+ /* update number of the last cluster of the file if it changed */
+ if (cls_added != 0)
+ {
+ fat_fd->map.last_cln = last_cl;
+ if (fat_fd->fat_file_type == FAT_DIRECTORY)
+ {
+ rc = fat_init_clusters_chain(mt_entry, chain);
+ if ( rc != RC_OK )
+ {
+ fat_free_fat_clusters_chain(mt_entry, chain);
+ return rc;
+ }
+ }
+ }
+
+ fat_fd->fat_file_size = new_length;
+
+ return RC_OK;
+}
+
+/* fat_file_truncate --
+ * Truncate fat-file. If new length greater than current fat-file size -
+ * do nothing. Otherwise find first cluster to free and free all clusters
+ * in the chain starting from this cluster.
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * fat_fd - fat-file descriptor
+ * new_length - new length
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set appropriately)
+ */
+int
+fat_file_truncate(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ uint32_t new_length
+ )
+{
+ int rc = RC_OK;
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t cur_cln = 0;
+ uint32_t cl_start = 0;
+ uint32_t new_last_cln = FAT_UNDEFINED_VALUE;
+
+
+ if ( new_length >= fat_fd->fat_file_size )
+ return rc;
+
+ assert(fat_fd->fat_file_size);
+
+ cl_start = (new_length + fs_info->vol.bpc - 1) >> fs_info->vol.bpc_log2;
+
+ if ((cl_start << fs_info->vol.bpc_log2) >= fat_fd->fat_file_size)
+ return RC_OK;
+
+ if (cl_start != 0)
+ {
+ rc = fat_file_lseek(mt_entry, fat_fd, cl_start - 1, &new_last_cln);
+ if (rc != RC_OK)
+ return rc;
+
+ }
+
+ rc = fat_file_lseek(mt_entry, fat_fd, cl_start, &cur_cln);
+ if (rc != RC_OK)
+ return rc;
+
+ rc = fat_free_fat_clusters_chain(mt_entry, cur_cln);
+ if (rc != RC_OK)
+ return rc;
+
+ if (cl_start != 0)
+ {
+ rc = fat_set_fat_cluster(mt_entry, new_last_cln, FAT_GENFAT_EOC);
+ if ( rc != RC_OK )
+ return rc;
+ fat_fd->map.file_cln = cl_start - 1;
+ fat_fd->map.disk_cln = new_last_cln;
+ fat_fd->map.last_cln = new_last_cln;
+ }
+ return RC_OK;
+}
+
+/* fat_file_ioctl --
+ * F_CLU_NUM:
+ * make mapping between serial number of the cluster in fat-file and
+ * its real number on the volume
+ *
+ * PARAMETERS:
+ * fat_fd - fat-file descriptor
+ * mt_entry - mount table entry
+ * cmd - command
+ * ...
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured and errno set appropriately
+ */
+int
+fat_file_ioctl(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ int cmd,
+ ...)
+{
+ int rc = RC_OK;
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t cur_cln = 0;
+ uint32_t cl_start = 0;
+ uint32_t pos = 0;
+ uint32_t *ret;
+ va_list ap;
+
+ va_start(ap, cmd);
+
+ switch (cmd)
+ {
+ case F_CLU_NUM:
+ pos = va_arg(ap, uint32_t );
+ ret = va_arg(ap, uint32_t *);
+
+ /* sanity check */
+ if ( pos >= fat_fd->fat_file_size ) {
+ va_end(ap);
+ rtems_set_errno_and_return_minus_one( EIO );
+ }
+
+ if ((FAT_FD_OF_ROOT_DIR(fat_fd)) &&
+ (fs_info->vol.type & (FAT_FAT12 | FAT_FAT16)))
+ {
+ /* cluster 0 (zero) reserved for root dir */
+ *ret = 0;
+ rc = RC_OK;
+ break;
+ }
+
+ cl_start = pos >> fs_info->vol.bpc_log2;
+
+ rc = fat_file_lseek(mt_entry, fat_fd, cl_start, &cur_cln);
+ if ( rc != RC_OK )
+ break;
+
+ *ret = cur_cln;
+ break;
+
+ default:
+ errno = EINVAL;
+ rc = -1;
+ break;
+ }
+ va_end(ap);
+ return rc;
+}
+
+/* fat_file_mark_removed --
+ * Remove the fat-file descriptor from "valid" hash table, insert it
+ * into "removed-but-still-open" hash table and set up "removed" bit.
+ *
+ * PARAMETERS:
+ * fat_fd - fat-file descriptor
+ * mt_entry - mount table entry
+ *
+ * RETURNS:
+ * None
+ */
+void
+fat_file_mark_removed(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd
+ )
+{
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t key = 0;
+
+ key = fat_construct_key(mt_entry, &fat_fd->dir_pos.sname);
+
+ _hash_delete(fs_info->vhash, key, fat_fd->ino, fat_fd);
+
+ _hash_insert(fs_info->rhash, key, fat_fd->ino, fat_fd);
+
+ fat_fd->flags |= FAT_FILE_REMOVED;
+}
+
+/* fat_file_datasync --
+ * Synchronize fat-file - flush all buffered data to the media.
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * fat_fd - fat-file descriptor
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured and errno set appropriately
+ */
+int
+fat_file_datasync(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd
+ )
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t cur_cln = fat_fd->cln;
+ rtems_bdbuf_buffer *block = NULL;
+ uint32_t sec = 0;
+ uint32_t i = 0;
+
+ if (fat_fd->fat_file_size == 0)
+ return RC_OK;
+
+ /*
+ * we can use only one bdbuf :( and we also know that cache is useless
+ * for sync operation, so don't use it
+ */
+ rc = fat_buf_release(fs_info);
+ if (rc != RC_OK)
+ return rc;
+
+ /* for each cluster of the file ... */
+ while ((cur_cln & fs_info->vol.mask) < fs_info->vol.eoc_val)
+ {
+ sec = fat_cluster_num_to_sector_num(mt_entry, cur_cln);
+ /* for each sector in cluster ... */
+ for ( i = 0; i < fs_info->vol.spc; i++ )
+ {
+ /* ... sync it */
+ sc = rtems_bdbuf_read(fs_info->vol.dev, (sec + i), &block);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one( EIO );
+
+ sc = rtems_bdbuf_sync(block);
+ if ( sc != RTEMS_SUCCESSFUL )
+ rtems_set_errno_and_return_minus_one( EIO );
+ }
+
+ rc = fat_get_fat_cluster(mt_entry, cur_cln, &cur_cln);
+ if ( rc != RC_OK )
+ return rc;
+ }
+ return rc;
+}
+
+/* fat_file_size --
+ * Calculate fat-file size - fat-file is nothing that clusters chain, so
+ * go through all clusters in the chain and count it. Only
+ * special case is root directory for FAT12/16 volumes.
+ * This function is used only for directories which are fat-files with
+ * non-zero length, hence 'fat_fd->cln' always contains valid data.
+ * Calculated size is stored in 'fat_file_size' field of fat-file
+ * descriptor.
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * fat_fd - fat-file descriptor
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set appropriately)
+ */
+int
+fat_file_size(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd
+ )
+{
+ int rc = RC_OK;
+ fat_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t cur_cln = fat_fd->cln;
+ uint32_t save_cln = 0;
+
+ /* Have we requested root dir size for FAT12/16? */
+ if ((FAT_FD_OF_ROOT_DIR(fat_fd)) &&
+ (fs_info->vol.type & (FAT_FAT12 | FAT_FAT16)))
+ {
+ fat_fd->fat_file_size = fs_info->vol.rdir_size;
+ return rc;
+ }
+
+ fat_fd->fat_file_size = 0;
+
+ while ((cur_cln & fs_info->vol.mask) < fs_info->vol.eoc_val)
+ {
+ save_cln = cur_cln;
+ rc = fat_get_fat_cluster(mt_entry, cur_cln, &cur_cln);
+ if ( rc != RC_OK )
+ return rc;
+
+ fat_fd->fat_file_size += fs_info->vol.bpc;
+ }
+ fat_fd->map.last_cln = save_cln;
+ return rc;
+}
+
+/* hash support routines */
+
+/* _hash_insert --
+ * Insert elemnt into hash based on key 'key1'
+ *
+ * PARAMETERS:
+ * hash - hash element will be inserted into
+ * key1 - key on which insertion is based on
+ * key2 - not used during insertion
+ * el - element to insert
+ *
+ * RETURNS:
+ * None
+ */
+static inline void
+_hash_insert(rtems_chain_control *hash, uint32_t key1, uint32_t key2,
+ fat_file_fd_t *el)
+{
+ rtems_chain_append((hash) + ((key1) % FAT_HASH_MODULE), &(el)->link);
+}
+
+
+/* _hash_delete --
+ * Remove element from hash
+ *
+ * PARAMETERS:
+ * hash - hash element will be removed from
+ * key1 - not used
+ * key2 - not used
+ * el - element to delete
+ *
+ * RETURNS:
+ * None
+ */
+static inline void
+_hash_delete(rtems_chain_control *hash, uint32_t key1, uint32_t key2,
+ fat_file_fd_t *el)
+{
+ rtems_chain_extract(&(el)->link);
+}
+
+/* _hash_search --
+ * Search element in hash. If both keys match pointer to found element
+ * is returned
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * hash - hash element will be removed from
+ * key1 - search key
+ * key2 - search key
+ * ret - placeholder for result
+ *
+ * RETURNS:
+ * 0 and pointer to found element on success, -1 otherwise
+ */
+static inline int
+_hash_search(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ rtems_chain_control *hash,
+ uint32_t key1,
+ uint32_t key2,
+ fat_file_fd_t **ret
+ )
+{
+ uint32_t mod = (key1) % FAT_HASH_MODULE;
+ rtems_chain_node *the_node = rtems_chain_first(hash + mod);
+
+ for ( ; !rtems_chain_is_tail((hash) + mod, the_node) ; )
+ {
+ fat_file_fd_t *ffd = (fat_file_fd_t *)the_node;
+ uint32_t ck = fat_construct_key(mt_entry, &ffd->dir_pos.sname);
+
+ if ( (key1) == ck)
+ {
+ if ( ((key2) == 0) || ((key2) == ffd->ino) )
+ {
+ *ret = (void *)the_node;
+ return 0;
+ }
+ }
+ the_node = the_node->next;
+ }
+ return -1;
+}
+
+static off_t
+fat_file_lseek(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ uint32_t file_cln,
+ uint32_t *disk_cln
+ )
+{
+ int rc = RC_OK;
+
+ if (file_cln == fat_fd->map.file_cln)
+ *disk_cln = fat_fd->map.disk_cln;
+ else
+ {
+ uint32_t cur_cln;
+ uint32_t count;
+ uint32_t i;
+
+ if (file_cln > fat_fd->map.file_cln)
+ {
+ cur_cln = fat_fd->map.disk_cln;
+ count = file_cln - fat_fd->map.file_cln;
+ }
+ else
+ {
+ cur_cln = fat_fd->cln;
+ count = file_cln;
+ }
+
+ /* skip over the clusters */
+ for (i = 0; i < count; i++)
+ {
+ rc = fat_get_fat_cluster(mt_entry, cur_cln, &cur_cln);
+ if ( rc != RC_OK )
+ return rc;
+ }
+
+ /* update cache */
+ fat_fd->map.file_cln = file_cln;
+ fat_fd->map.disk_cln = cur_cln;
+
+ *disk_cln = cur_cln;
+ }
+ return RC_OK;
+}
diff --git a/cpukit/libfs/src/dosfs/fat_file.h b/cpukit/libfs/src/dosfs/fat_file.h
new file mode 100644
index 0000000000..01ab73ef98
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/fat_file.h
@@ -0,0 +1,192 @@
+/*
+ * fat_file.h
+ *
+ * Constants/data structures/prototypes for operations on "fat-file"
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ */
+#ifndef __DOSFS_FAT_FILE_H__
+#define __DOSFS_FAT_FILE_H__
+
+#include <rtems.h>
+#include <rtems/libio_.h>
+
+#include <time.h>
+
+#include "fat.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* "fat-file" representation
+ *
+ * the idea is: fat-file is nothing but a cluster chain, any open fat-file is
+ * represented in system by fat-file descriptor and has well-known
+ * file interface:
+ *
+ * fat_file_open()
+ * fat_file_close()
+ * fat_file_read()
+ * fat_file_write()
+ *
+ * Such interface hides the architecture of fat-file and represents it like
+ * linear file
+ */
+
+typedef rtems_filesystem_node_types_t fat_file_type_t;
+
+#define FAT_DIRECTORY RTEMS_FILESYSTEM_DIRECTORY
+#define FAT_FILE RTEMS_FILESYSTEM_MEMORY_FILE
+
+typedef struct fat_file_map_s
+{
+ uint32_t file_cln;
+ uint32_t disk_cln;
+ uint32_t last_cln;
+} fat_file_map_t;
+/*
+ * descriptor of a fat-file
+ *
+ * To each particular clusters chain
+ */
+typedef struct fat_file_fd_s
+{
+ rtems_chain_node link; /*
+ * fat-file descriptors organized into hash;
+ * collision lists are handled via link
+ * field
+ */
+ uint32_t links_num; /*
+ * the number of fat_file_open call on
+ * this fat-file
+ */
+ uint32_t ino; /* inode, file serial number :)))) */
+ fat_file_type_t fat_file_type;
+ uint32_t size_limit;
+ uint32_t fat_file_size; /* length */
+ uint32_t cln;
+ fat_dir_pos_t dir_pos;
+ uint8_t flags;
+ fat_file_map_t map;
+ time_t mtime;
+
+} fat_file_fd_t;
+
+#define FAT_FILE_REMOVED 0x01
+
+#define FAT_FILE_IS_REMOVED(p)\
+ (((p)->flags & FAT_FILE_REMOVED) ? 1 : 0)
+
+/* ioctl macros */
+#define F_CLU_NUM 0x01
+
+/*
+ * Each file and directory on a MSDOS volume is unique identified by it
+ * location, i.e. location of it 32 Bytes Directory Entry Structure. We can
+ * distinguish them by cluster number it locates on and offset inside this
+ * cluster. But root directory on any volumes (FAT12/16/32) has no 32 Bytes
+ * Directory Entry Structure corresponded to it. So we assume 32 Bytes
+ * Directory Entry Structure of root directory locates at cluster 1 (invalid
+ * cluaster number) and offset 0
+ */
+#define FAT_ROOTDIR_CLUSTER_NUM 0x01
+
+#define FAT_FD_OF_ROOT_DIR(fat_fd) \
+ ((fat_fd->dir_pos.sname.cln == FAT_ROOTDIR_CLUSTER_NUM) && \
+ (fat_fd->dir_pos.sname.ofs == 0))
+
+#define FAT_EOF 0x00
+
+/* fat_construct_key --
+ * Construct key for hash access: convert (cluster num, offset) to
+ * (sector512 num, new offset) and than construct key as
+ * key = (sector512 num) << 4 | (new offset)
+ *
+ * PARAMETERS:
+ * cl - cluster number
+ * ofs - offset inside cluster 'cl'
+ * mt_entry - mount table entry
+ *
+ * RETURNS:
+ * constructed key
+ */
+static inline uint32_t
+fat_construct_key(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_pos_t *pos)
+{
+ return ( ((fat_cluster_num_to_sector512_num(mt_entry, pos->cln) +
+ (pos->ofs >> FAT_SECTOR512_BITS)) << 4) +
+ ((pos->ofs >> 5) & (FAT_DIRENTRIES_PER_SEC512 - 1)) );
+}
+
+/* Prototypes for "fat-file" operations */
+int
+fat_file_open(rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_dir_pos_t *dir_pos,
+ fat_file_fd_t **fat_fd);
+
+int
+fat_file_reopen(fat_file_fd_t *fat_fd);
+
+int
+fat_file_close(rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd);
+
+ssize_t
+fat_file_read(rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ uint32_t start,
+ uint32_t count,
+ uint8_t *buf);
+
+ssize_t
+fat_file_write(rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ uint32_t start,
+ uint32_t count,
+ const uint8_t *buf);
+
+int
+fat_file_extend(rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ uint32_t new_length,
+ uint32_t *a_length);
+
+int
+fat_file_truncate(rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ uint32_t new_length);
+
+int
+fat_file_datasync(rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd);
+
+
+int
+fat_file_ioctl(rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ int cmd,
+ ...);
+
+int
+fat_file_size(rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd);
+
+void
+fat_file_mark_removed(rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __DOSFS_FAT_FILE_H__ */
diff --git a/cpukit/libfs/src/dosfs/msdos.h b/cpukit/libfs/src/dosfs/msdos.h
new file mode 100644
index 0000000000..696dac37cf
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/msdos.h
@@ -0,0 +1,467 @@
+/*
+ * msdos.h
+ *
+ * The MSDOS filesystem constants/data structures/prototypes
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ */
+#ifndef __DOSFS_MSDOS_H__
+#define __DOSFS_MSDOS_H__
+
+#include <rtems.h>
+#include <rtems/libio_.h>
+
+#include "fat.h"
+#include "fat_file.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define MSDOS_NAME_NOT_FOUND_ERR 0x7D01
+
+/*
+ * This structure identifies the instance of the filesystem on the MSDOS
+ * level.
+ */
+typedef struct msdos_fs_info_s
+{
+ fat_fs_info_t fat; /*
+ * volume
+ * description
+ */
+ const rtems_filesystem_file_handlers_r *directory_handlers; /*
+ * a set of routines
+ * that handles the
+ * nodes of directory
+ * type
+ */
+ const rtems_filesystem_file_handlers_r *file_handlers; /*
+ * a set of routines
+ * that handles the
+ * nodes of file
+ * type
+ */
+ rtems_id vol_sema; /*
+ * semaphore
+ * associated with
+ * the volume
+ */
+ uint8_t *cl_buf; /*
+ * just placeholder
+ * for anything
+ */
+} msdos_fs_info_t;
+
+/* a set of routines that handle the nodes which are directories */
+extern const rtems_filesystem_file_handlers_r msdos_dir_handlers;
+
+/* a set of routines that handle the nodes which are files */
+extern const rtems_filesystem_file_handlers_r msdos_file_handlers;
+
+/* Volume semaphore timeout value. This value can be changed to a number
+ * of ticks to help debugging or if you need such a */
+#define MSDOS_VOLUME_SEMAPHORE_TIMEOUT RTEMS_NO_TIMEOUT
+
+/* Node types */
+#define MSDOS_DIRECTORY RTEMS_FILESYSTEM_DIRECTORY
+#define MSDOS_REGULAR_FILE RTEMS_FILESYSTEM_MEMORY_FILE
+#define MSDOS_HARD_LINK RTEMS_FILESYSTEM_HARD_LINK /* pseudo type */
+
+typedef rtems_filesystem_node_types_t msdos_node_type_t;
+
+/*
+ * Macros for fetching fields from 32 bytes long FAT Directory Entry
+ * Structure
+ */
+#define MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE 32 /* 32 bytes */
+
+#define MSDOS_DIR_NAME(x) (char *)((x) + 0)
+#define MSDOS_DIR_ENTRY_TYPE(x) (uint8_t *)((x) + 0)
+#define MSDOS_DIR_ATTR(x) (uint8_t *)((x) + 11)
+#define MSDOS_DIR_NT_RES(x) (uint8_t *)((x) + 12)
+#define MSDOS_DIR_LFN_CHECKSUM(x) (uint8_t *)((x) + 13)
+#define MSDOS_DIR_CRT_TIME_TENTH(x) (uint8_t *)((x) + 13)
+#define MSDOS_DIR_CRT_TIME(x) (uint16_t *)((x) + 14)
+#define MSDOS_DIR_CRT_DATE(x) (uint16_t *)((x) + 16)
+#define MSDOS_DIR_LAST_ACCESS_DATE(x) (uint16_t *)((x) + 18)
+#define MSDOS_DIR_FIRST_CLUSTER_HI(x) (uint16_t *)((x) + 20)
+#define MSDOS_DIR_WRITE_TIME(x) (uint16_t *)((x) + 22)
+#define MSDOS_DIR_WRITE_DATE(x) (uint16_t *)((x) + 24)
+#define MSDOS_DIR_FIRST_CLUSTER_LOW(x) (uint16_t *)((x) + 26)
+#define MSDOS_DIR_FILE_SIZE(x) (uint32_t *)((x) + 28)
+
+#define MSDOS_EXTRACT_CLUSTER_NUM(p) \
+ (uint32_t)( (CF_LE_W(*MSDOS_DIR_FIRST_CLUSTER_LOW(p))) | \
+ ((uint32_t)(CF_LE_W((*MSDOS_DIR_FIRST_CLUSTER_HI(p))))<<16) )
+
+/*
+ * Fields offset in 32 bytes long FAT Directory Entry
+ * Structure
+ */
+#define MSDOS_FILE_SIZE_OFFSET 28
+#define MSDOS_FILE_NAME_OFFSET 0
+#define MSDOS_FIRST_CLUSTER_HI_OFFSET 20
+#define MSDOS_FIRST_CLUSTER_LOW_OFFSET 26
+#define MSDOS_FILE_WDATE_OFFSET 24
+#define MSDOS_FILE_WTIME_OFFSET 22
+#define MSDOS_FILE_ADATE_OFFSET 18
+
+/*
+ * Possible values of DIR_Attr field of 32 bytes long FAT Directory Entry
+ * Structure
+ */
+#define MSDOS_ATTR_READ_ONLY 0x01
+#define MSDOS_ATTR_HIDDEN 0x02
+#define MSDOS_ATTR_SYSTEM 0x04
+#define MSDOS_ATTR_VOLUME_ID 0x08
+#define MSDOS_ATTR_DIRECTORY 0x10
+#define MSDOS_ATTR_ARCHIVE 0x20
+#define MSDOS_ATTR_LFN (MSDOS_ATTR_READ_ONLY | \
+ MSDOS_ATTR_HIDDEN | \
+ MSDOS_ATTR_SYSTEM | \
+ MSDOS_ATTR_VOLUME_ID)
+#define MSDOS_ATTR_LFN_MASK (MSDOS_ATTR_READ_ONLY | \
+ MSDOS_ATTR_HIDDEN | \
+ MSDOS_ATTR_SYSTEM | \
+ MSDOS_ATTR_VOLUME_ID | \
+ MSDOS_ATTR_DIRECTORY | \
+ MSDOS_ATTR_ARCHIVE)
+
+#define MSDOS_LAST_LONG_ENTRY 0x40
+#define MSDOS_LAST_LONG_ENTRY_MASK 0x3F
+
+#define MSDOS_DT_2SECONDS_MASK 0x1F /* seconds divided by 2 */
+#define MSDOS_DT_2SECONDS_SHIFT 0
+#define MSDOS_DT_MINUTES_MASK 0x7E0 /* minutes */
+#define MSDOS_DT_MINUTES_SHIFT 5
+#define MSDOS_DT_HOURS_MASK 0xF800 /* hours */
+#define MSDOS_DT_HOURS_SHIFT 11
+
+#define MSDOS_DD_DAY_MASK 0x1F /* day of month */
+#define MSDOS_DD_DAY_SHIFT 0
+#define MSDOS_DD_MONTH_MASK 0x1E0 /* month */
+#define MSDOS_DD_MONTH_SHIFT 5
+#define MSDOS_DD_YEAR_MASK 0xFE00 /* year - 1980 */
+#define MSDOS_DD_YEAR_SHIFT 9
+
+
+/*
+ * Possible values of DIR_Name[0] field of 32 bytes long FAT Directory Entry
+ * Structure
+ */
+#define MSDOS_THIS_DIR_ENTRY_EMPTY 0xE5
+#define MSDOS_THIS_DIR_ENTRY_AND_REST_EMPTY 0x00
+
+/*
+ * Number of characters per directory entry for a long filename.
+ */
+#define MSDOS_LFN_LEN_PER_ENTRY (13)
+
+/*
+ * Macros for names parsing and formatting
+ */
+#define msdos_is_separator(_ch) rtems_filesystem_is_separator(_ch)
+
+#define MSDOS_SHORT_BASE_LEN 8 /* 8 characters */
+#define MSDOS_SHORT_EXT_LEN 3 /* 3 characters */
+#define MSDOS_SHORT_NAME_LEN (MSDOS_SHORT_BASE_LEN+\
+ MSDOS_SHORT_EXT_LEN) /* 11 chars */
+#define MSDOS_NAME_MAX_LNF_LEN (255)
+#define MSDOS_NAME_MAX MSDOS_SHORT_NAME_LEN
+#define MSDOS_NAME_MAX_WITH_DOT (MSDOS_NAME_MAX + 1)
+#define MSDOS_NAME_MAX_LFN_WITH_DOT (260)
+
+
+extern const char *const MSDOS_DOT_NAME; /* ".", padded to MSDOS_NAME chars */
+extern const char *const MSDOS_DOTDOT_NAME; /* ".", padded to MSDOS_NAME chars */
+
+typedef enum msdos_name_types_e
+{
+ MSDOS_NAME_INVALID = 0, /* Unknown name type. Has invalid characters. */
+ MSDOS_NAME_SHORT, /* Name can be short. */
+ MSDOS_NAME_LONG /* Name is long; cannot be short. */
+} msdos_name_type_t;
+
+typedef enum msdos_token_types_e
+{
+ MSDOS_NO_MORE_PATH,
+ MSDOS_CURRENT_DIR,
+ MSDOS_UP_DIR,
+ MSDOS_NAME,
+ MSDOS_INVALID_TOKEN
+} msdos_token_types_t;
+
+/* Others macros */
+#define MSDOS_RES_NT_VALUE 0x00
+#define MSDOS_INIT_DIR_SIZE 0x00
+
+/* "dot" entry offset in a directory */
+#define MSDOS_DOT_DIR_ENTRY_OFFSET 0x00 /* first entry in directory */
+
+/* "dotdot" entry offset in a directory */
+#define MSDOS_DOTDOT_DIR_ENTRY_OFFSET 0x20 /* second entry in directory */
+
+/* 'p' should be char* */
+#define DOT_NODE_P(p) ((char *)(p))
+#define DOTDOT_NODE_P(p) ((char *)((p) + MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE))
+
+/* Size limits for files and directories */
+#define MSDOS_MAX_DIR_LENGHT 0x200000 /* 2,097,152 bytes */
+#define MSDOS_MAX_FILE_SIZE 0xFFFFFFFF /* 4 Gb */
+
+/*
+ * The number of 32 bytes long FAT Directory Entry
+ * Structures per 512 bytes sector
+ */
+#define MSDOS_DPS512_NUM 16
+
+/* Prototypes */
+int msdos_shut_down(rtems_filesystem_mount_table_entry_t *temp_mt_entry);
+
+int msdos_eval_path(
+ const char *pathname, /* IN */
+ size_t pathnamelen, /* IN */
+ int flags, /* IN */
+ rtems_filesystem_location_info_t *pathloc /* IN/OUT */
+);
+
+int msdos_eval4make(
+ const char *path, /* IN */
+ rtems_filesystem_location_info_t *pathloc, /* IN/OUT */
+ const char **name /* OUT */
+);
+
+int msdos_unlink(rtems_filesystem_location_info_t *pathloc /* IN */);
+
+int msdos_free_node_info(rtems_filesystem_location_info_t *pathloc /* IN */);
+
+rtems_filesystem_node_types_t msdos_node_type(rtems_filesystem_location_info_t *pathloc);
+
+int msdos_mknod(
+ const char *path, /* IN */
+ mode_t mode, /* IN */
+ dev_t dev, /* IN */
+ rtems_filesystem_location_info_t *pathloc /* IN/OUT */
+);
+
+int msdos_utime(
+ rtems_filesystem_location_info_t *pathloc, /* IN */
+ time_t actime, /* IN */
+ time_t modtime /* IN */
+);
+
+int msdos_rename(rtems_filesystem_location_info_t *old_parent_loc,
+ rtems_filesystem_location_info_t *old_loc,
+ rtems_filesystem_location_info_t *new_parent_loc,
+ const char *new_name);
+
+int msdos_initialize_support(
+ rtems_filesystem_mount_table_entry_t *temp_mt_entry,
+ const rtems_filesystem_operations_table *op_table,
+ const rtems_filesystem_file_handlers_r *file_handlers,
+ const rtems_filesystem_file_handlers_r *directory_handlers
+);
+
+int msdos_file_open(
+ rtems_libio_t *iop, /* IN */
+ const char *pathname, /* IN */
+ uint32_t flag, /* IN */
+ uint32_t mode /* IN */
+);
+
+int msdos_file_close(rtems_libio_t *iop /* IN */);
+
+ssize_t msdos_file_read(
+ rtems_libio_t *iop, /* IN */
+ void *buffer, /* IN */
+ size_t count /* IN */
+);
+
+ssize_t msdos_file_write(
+ rtems_libio_t *iop, /* IN */
+ const void *buffer, /* IN */
+ size_t count /* IN */
+);
+
+rtems_off64_t msdos_file_lseek(
+ rtems_libio_t *iop, /* IN */
+ rtems_off64_t offset, /* IN */
+ int whence /* IN */
+);
+
+int msdos_file_stat(
+ rtems_filesystem_location_info_t *loc, /* IN */
+ struct stat *buf /* OUT */
+);
+
+int
+msdos_file_ftruncate(
+ rtems_libio_t *iop, /* IN */
+ rtems_off64_t length /* IN */
+);
+
+int msdos_file_sync(rtems_libio_t *iop);
+
+int msdos_file_datasync(rtems_libio_t *iop);
+
+int msdos_file_ioctl(
+ rtems_libio_t *iop, /* IN */
+ uint32_t command, /* IN */
+ void *buffer /* IN */
+);
+
+int
+msdos_dir_chmod(
+ rtems_filesystem_location_info_t *pathloc, /* IN */
+ mode_t mode /* IN */
+);
+
+int msdos_file_rmnod(rtems_filesystem_location_info_t *parent_pathloc, /* IN */
+ rtems_filesystem_location_info_t *pathloc /* IN */);
+
+int msdos_dir_open(
+ rtems_libio_t *iop, /* IN */
+ const char *pathname, /* IN */
+ uint32_t flag, /* IN */
+ uint32_t mode /* IN */
+);
+
+int msdos_dir_close(rtems_libio_t *iop /* IN */);
+
+ssize_t msdos_dir_read(
+ rtems_libio_t *iop, /* IN */
+ void *buffer, /* IN */
+ size_t count /* IN */
+);
+
+rtems_off64_t msdos_dir_lseek(
+ rtems_libio_t *iop, /* IN */
+ rtems_off64_t offset, /* IN */
+ int whence /* IN */
+);
+
+int
+msdos_file_chmod(
+ rtems_filesystem_location_info_t *pathloc, /* IN */
+ mode_t mode /* IN */
+);
+
+int msdos_dir_rmnod(rtems_filesystem_location_info_t *parent_pathloc, /* IN */
+ rtems_filesystem_location_info_t *pathloc /* IN */);
+
+int msdos_dir_sync(rtems_libio_t *iop);
+
+int msdos_dir_stat(
+ rtems_filesystem_location_info_t *loc, /* IN */
+ struct stat *buf /* OUT */
+);
+
+int msdos_creat_node(rtems_filesystem_location_info_t *parent_loc,
+ msdos_node_type_t type,
+ const char *name,
+ int name_len,
+ mode_t mode,
+ const fat_file_fd_t *link_fd);
+
+/* Misc prototypes */
+msdos_token_types_t msdos_get_token(const char *path,
+ int pathlen,
+ const char **token,
+ int *token_len);
+
+int msdos_find_name(
+ rtems_filesystem_location_info_t *parent_loc,
+ const char *name,
+ int name_len
+);
+
+int msdos_get_name_node(
+ rtems_filesystem_location_info_t *parent_loc,
+ bool create_node,
+ const char *name,
+ int name_len,
+ msdos_name_type_t name_type,
+ fat_dir_pos_t *dir_pos,
+ char *name_dir_entry
+);
+
+int msdos_dir_info_remove(rtems_filesystem_location_info_t *pathloc);
+
+msdos_name_type_t msdos_long_to_short(const char *lfn, int lfn_len,
+ char* sfn, int sfn_len);
+
+int msdos_filename_unix2dos(const char *un, int unlen, char *dn);
+
+void msdos_date_unix2dos(
+ unsigned int tsp, uint16_t *ddp,
+ uint16_t *dtp);
+
+unsigned int msdos_date_dos2unix(unsigned int dd, unsigned int dt);
+
+int msdos_set_first_cluster_num(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd
+);
+
+int msdos_set_file_size(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd
+);
+
+int msdos_set_first_char4file_name(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_dir_pos_t *dir_pos,
+ unsigned char first_char
+);
+
+int msdos_set_dir_wrt_time_and_date(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd
+);
+
+
+int msdos_dir_is_empty(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ bool *ret_val
+);
+
+int msdos_find_name_in_fat_file(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ bool create_node,
+ const char *name,
+ int name_len,
+ msdos_name_type_t name_type,
+ fat_dir_pos_t *dir_pos,
+ char *name_dir_entry
+);
+
+int msdos_find_node_by_cluster_num_in_fat_file(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ uint32_t cl4find,
+ fat_dir_pos_t *dir_pos,
+ char *dir_entry
+);
+
+int msdos_get_dotdot_dir_info_cluster_num_and_offset(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t cln,
+ fat_dir_pos_t *dir_pos,
+ char *dir_entry
+);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __DOSFS_MSDOS_H__ */
diff --git a/cpukit/libfs/src/dosfs/msdos_conv.c b/cpukit/libfs/src/dosfs/msdos_conv.c
new file mode 100644
index 0000000000..95b25814db
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/msdos_conv.c
@@ -0,0 +1,317 @@
+/*
+ * Adaptation of NetBSD code for RTEMS by Victor V. Vengerov <vvv@oktet.ru>
+ */
+/* $NetBSD: msdosfs_conv.c,v 1.10 1994/12/27 18:36:24 mycroft Exp $ */
+/*
+ * Written by Paul Popelka (paulp@uts.amdahl.com)
+ *
+ * You can do anything you want with this software, just don't say you wrote
+ * it, and don't remove this notice.
+ *
+ * This software is provided "as is".
+ *
+ * The author supplies this software to be publicly redistributed on the
+ * understanding that the author is not responsible for the correct
+ * functioning of this software in any circumstances and is not liable for
+ * any damages caused by this software.
+ *
+ * October 1992
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <rtems.h>
+#include "msdos.h"
+
+/* #define SECONDSPERDAY (24 * 60 * 60) */
+#define SECONDSPERDAY ((uint32_t) 86400)
+
+/*
+ * Days in each month in a regular year.
+ */
+static uint16_t regyear[] = {
+ 31, 28, 31, 30, 31, 30,
+ 31, 31, 30, 31, 30, 31
+};
+
+/*
+ * Days in each month in a leap year.
+ */
+static uint16_t leapyear[] = {
+ 31, 29, 31, 30, 31, 30,
+ 31, 31, 30, 31, 30, 31
+};
+
+/*
+ * Variables used to remember parts of the last time conversion. Maybe we
+ * can avoid a full conversion.
+ */
+static uint32_t lasttime;
+static uint32_t lastday;
+static uint16_t lastddate;
+static uint16_t lastdtime;
+
+/*
+ * Convert the unix version of time to dos's idea of time to be used in
+ * file timestamps. The passed in unix time is assumed to be in GMT.
+ */
+void
+msdos_date_unix2dos(unsigned int t, uint16_t *ddp,
+ uint16_t *dtp)
+{
+ uint32_t days;
+ uint32_t inc;
+ uint32_t year;
+ uint32_t month;
+ uint16_t *months;
+
+ /*
+ * If the time from the last conversion is the same as now, then
+ * skip the computations and use the saved result.
+ */
+ if (lasttime != t) {
+ lasttime = t;
+ lastdtime = (((t % 60) >> 1) << MSDOS_DT_2SECONDS_SHIFT)
+ + (((t / 60) % 60) << MSDOS_DT_MINUTES_SHIFT)
+ + (((t / 3600) % 24) << MSDOS_DT_HOURS_SHIFT);
+
+ /*
+ * If the number of days since 1970 is the same as the last
+ * time we did the computation then skip all this leap year
+ * and month stuff.
+ */
+ days = t / (SECONDSPERDAY);
+ if (days != lastday) {
+ lastday = days;
+ for (year = 1970;; year++) {
+ inc = year & 0x03 ? 365 : 366;
+ if (days < inc)
+ break;
+ days -= inc;
+ }
+ months = year & 0x03 ? regyear : leapyear;
+ for (month = 0; month < 12; month++) {
+ if (days < months[month])
+ break;
+ days -= months[month];
+ }
+ lastddate = ((days + 1) << MSDOS_DD_DAY_SHIFT)
+ + ((month + 1) << MSDOS_DD_MONTH_SHIFT);
+ /*
+ * Remember dos's idea of time is relative to 1980.
+ * unix's is relative to 1970. If somehow we get a
+ * time before 1980 then don't give totally crazy
+ * results.
+ */
+ if (year > 1980)
+ lastddate += (year - 1980) <<
+ MSDOS_DD_YEAR_SHIFT;
+ }
+ }
+ *dtp = lastdtime;
+ *ddp = lastddate;
+}
+
+/*
+ * The number of days between Jan 1, 1970 and Jan 1, 1980. In that
+ * interval there were 8 regular years and 2 leap years.
+ */
+/* #define DAYSTO1980 ((8 * 365) + (2 * 366)) */
+#define DAYSTO1980 ((uint32_t) 3652)
+
+static uint16_t lastdosdate;
+static uint32_t lastseconds;
+
+/*
+ * Convert from dos' idea of time to unix'. This will probably only be
+ * called from the stat(), and fstat() system calls and so probably need
+ * not be too efficient.
+ */
+unsigned int
+msdos_date_dos2unix(unsigned int dd, unsigned int dt)
+{
+ uint32_t seconds;
+ uint32_t m, month;
+ uint32_t y, year;
+ uint32_t days;
+ uint16_t *months;
+
+ seconds = 2 * ((dt & MSDOS_DT_2SECONDS_MASK) >> MSDOS_DT_2SECONDS_SHIFT)
+ + ((dt & MSDOS_DT_MINUTES_MASK) >> MSDOS_DT_MINUTES_SHIFT) * 60
+ + ((dt & MSDOS_DT_HOURS_MASK) >> MSDOS_DT_HOURS_SHIFT) * 3600;
+ /*
+ * If the year, month, and day from the last conversion are the
+ * same then use the saved value.
+ */
+ if (lastdosdate != dd) {
+ lastdosdate = dd;
+ days = 0;
+ year = (dd & MSDOS_DD_YEAR_MASK) >> MSDOS_DD_YEAR_SHIFT;
+ for (y = 0; y < year; y++)
+ days += y & 0x03 ? 365 : 366;
+ months = year & 0x03 ? regyear : leapyear;
+ /*
+ * Prevent going from 0 to 0xffffffff in the following
+ * loop.
+ */
+ month = (dd & MSDOS_DD_MONTH_MASK) >> MSDOS_DD_MONTH_SHIFT;
+ if (month == 0) {
+ month = 1;
+ }
+ for (m = 0; m < month - 1; m++)
+ days += months[m];
+ days += ((dd & MSDOS_DD_DAY_MASK) >> MSDOS_DD_DAY_SHIFT) - 1;
+ lastseconds = (days + DAYSTO1980) * SECONDSPERDAY;
+ }
+ return seconds + lastseconds;
+}
+
+static const uint8_t msdos_map[] = {
+ 0, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */
+ 0, 0, 0, 0, 0, 0, 0, 0, /* 08-0f */
+ 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */
+ 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */
+ 0, '!', 0, '#', '$', '%', '&', '\'', /* 20-27 */
+ '(', ')', 0, '+', 0, '-', 0, 0, /* 28-2f */
+ '0', '1', '2', '3', '4', '5', '6', '7', /* 30-37 */
+ '8', '9', 0, 0, 0, 0, 0, 0, /* 38-3f */
+ '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', /* 40-47 */
+ 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', /* 48-4f */
+ 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', /* 50-57 */
+ 'X', 'Y', 'Z', 0, 0, 0, '^', '_', /* 58-5f */
+ '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', /* 60-67 */
+ 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', /* 68-6f */
+ 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', /* 70-77 */
+ 'X', 'Y', 'Z', '{', 0, '}', '~', 0, /* 78-7f */
+ 0, 0, 0, 0, 0, 0, 0, 0, /* 80-87 */
+ 0, 0, 0, 0, 0, 0, 0, 0, /* 88-8f */
+ 0, 0, 0, 0, 0, 0, 0, 0, /* 90-97 */
+ 0, 0, 0, 0, 0, 0, 0, 0, /* 98-9f */
+ 0, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */
+ 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */
+ 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */
+ 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */
+ 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */
+ 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */
+ 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */
+ 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */
+ 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */
+ 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */
+ 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */
+ 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */
+#if OLD_TABLE
+/* 00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* 08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* 10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* 18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* 20 */ 0x00, 0x21, 0x00, 0x23, 0x24, 0x25, 0x26, 0x27, /* !"#$%&' */
+/* 28 */ 0x28, 0x29, 0x00, 0x00, 0x00, 0x2D, 0x2E, 0x00, /* ()*+,-./ */
+/* 30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 01234567 */
+/* 38 */ 0x38, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 89:;<=>? */
+/* 40 */ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* @ABCDEFG */
+/* 48 */ 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, /* HIJKLMNO */
+/* 50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* PQRSTUVW */
+/* 58 */ 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x00, 0x5E, 0x5F, /* XYZ[\]^_ */
+/* 60 */ 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* `abcdefg */
+/* 68 */ 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, /* hijklmno */
+/* 70 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* pqrstuvw */
+/* 78 */ 0x58, 0x59, 0x5A, 0x5B, 0x7C, 0x00, 0x7E, 0x00, /* xyz{|}~ */
+/* 80 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* 88 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* 90 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* 98 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* A0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* A8 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* B0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* B8 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* C0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* C8 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* D0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* D8 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* E0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* E8 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* F0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/* F8 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+#endif
+};
+/*
+ * Convert a unix filename to a DOS filename. Return -1 if wrong name is
+ * supplied.
+ */
+int
+msdos_filename_unix2dos(const char *un, int unlen, char *dn)
+{
+ int i;
+ uint8_t c;
+
+ /*
+ * Fill the dos filename string with blanks. These are DOS's pad
+ * characters.
+ */
+ for (i = 0; i <= 10; i++)
+ dn[i] = ' ';
+
+ /*
+ * The filenames "." and ".." are handled specially, since they
+ * don't follow dos filename rules.
+ */
+ if (un[0] == '.' && unlen == 1) {
+ dn[0] = '.';
+ return 0;
+ }
+ if (un[0] == '.' && un[1] == '.' && unlen == 2) {
+ dn[0] = '.';
+ dn[1] = '.';
+ return 0;
+ }
+
+ /*
+ * Remove any dots from the start of a file name.
+ */
+ while (unlen && (*un == '.')) {
+ un++;
+ unlen--;
+ }
+
+ /*
+ * Copy the unix filename into the dos filename string upto the end
+ * of string, a '.', or 8 characters. Whichever happens first stops
+ * us. This forms the name portion of the dos filename. Fold to
+ * upper case.
+ */
+ for (i = 0; i <= 7 && unlen && (c = *un) && c != '.'; i++) {
+ if (msdos_map[c] == 0)
+ break;
+ dn[i] = msdos_map[c];
+ un++;
+ unlen--;
+ }
+
+ /*
+ * Strip any further characters up to a '.' or the end of the
+ * string.
+ */
+ while (unlen && (c = *un)) {
+ un++;
+ unlen--;
+ /* Make sure we've skipped over the dot before stopping. */
+ if (c == '.')
+ break;
+ }
+
+ /*
+ * Copy in the extension part of the name, if any. Force to upper
+ * case. Note that the extension is allowed to contain '.'s.
+ * Filenames in this form are probably inaccessable under dos.
+ */
+ for (i = 8; i <= 10 && unlen && (c = *un); i++) {
+ if (msdos_map[c] == 0)
+ break;
+ dn[i] = msdos_map[c];
+ un++;
+ unlen--;
+ }
+ return 0;
+}
diff --git a/cpukit/libfs/src/dosfs/msdos_create.c b/cpukit/libfs/src/dosfs/msdos_create.c
new file mode 100644
index 0000000000..ec5862a178
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/msdos_create.c
@@ -0,0 +1,267 @@
+/*
+ * Routine to create a new MSDOS filesystem node
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ *
+ */
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <rtems/libio_.h>
+#include <time.h>
+
+#include "fat.h"
+#include "fat_fat_operations.h"
+#include "fat_file.h"
+
+#include "msdos.h"
+
+/* msdos_creat_node --
+ * Create a new node. Determine if the name is a long name. If long we to
+ * scan the directory to create a short entry.
+ *
+ *
+
+
+
+ * If a new node is file, FAT 32 Bytes Directory
+ * Entry Structure is initialized, free space is found in parent
+ * directory and structure is written to the disk. In case of directory,
+ * all above steps present and also new cluster is allocated for a
+ * new directory and dot and dotdot nodes are created in alloceted cluster.
+ *
+ * PARAMETERS:
+ * parent_loc - parent (directory we are going to create node in)
+ * type - new node type (file or directory)
+ * name - new node name
+ * mode - mode
+ * link_info - fs_info of existing node for a pseudo "hard-link"
+ * (see msdos_file.c, msdos_link for documentation)
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set appropriately).
+ *
+ */
+int
+msdos_creat_node(rtems_filesystem_location_info_t *parent_loc,
+ msdos_node_type_t type,
+ const char *name,
+ int name_len,
+ mode_t mode,
+ const fat_file_fd_t *link_fd)
+{
+ int rc = RC_OK;
+ ssize_t ret = 0;
+ msdos_fs_info_t *fs_info = parent_loc->mt_entry->fs_info;
+ fat_file_fd_t *parent_fat_fd = parent_loc->node_access;
+ fat_file_fd_t *fat_fd = NULL;
+ time_t time_ret = 0;
+ uint16_t time_val = 0;
+ uint16_t date = 0;
+ fat_dir_pos_t dir_pos;
+ msdos_name_type_t name_type;
+ char short_node[MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE];
+ char dot_dotdot[MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE * 2];
+ char link_node[MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE];
+ uint32_t sec = 0;
+ uint32_t byte = 0;
+
+ fat_dir_pos_init(&dir_pos);
+
+ memset(short_node, 0, MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE);
+ memset(dot_dotdot, 0, MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE * 2);
+
+ name_type = msdos_long_to_short (name, name_len,
+ MSDOS_DIR_NAME(short_node),
+ MSDOS_NAME_MAX);
+
+ /* fill reserved field */
+ *MSDOS_DIR_NT_RES(short_node) = MSDOS_RES_NT_VALUE;
+
+ /* set up last write date and time */
+ time_ret = time(NULL);
+ if ( time_ret == -1 )
+ return -1;
+
+ msdos_date_unix2dos(time_ret, &date, &time_val);
+ *MSDOS_DIR_CRT_TIME(short_node) = CT_LE_W(time_val);
+ *MSDOS_DIR_CRT_DATE(short_node) = CT_LE_W(date);
+ *MSDOS_DIR_WRITE_TIME(short_node) = CT_LE_W(time_val);
+ *MSDOS_DIR_WRITE_DATE(short_node) = CT_LE_W(date);
+ *MSDOS_DIR_LAST_ACCESS_DATE(short_node) = CT_LE_W(date);
+
+ /* initialize directory/file size */
+ *MSDOS_DIR_FILE_SIZE(short_node) = MSDOS_INIT_DIR_SIZE;
+
+ if (type == MSDOS_DIRECTORY) {
+ *MSDOS_DIR_ATTR(short_node) |= MSDOS_ATTR_DIRECTORY;
+ }
+ else if (type == MSDOS_HARD_LINK) {
+ /*
+ * when we establish a (temporary) hard link,
+ * we must copy some information from the original
+ * node to the newly created
+ */
+ /*
+ * read the original directory entry
+ */
+ sec = fat_cluster_num_to_sector_num(parent_loc->mt_entry,
+ link_fd->dir_pos.sname.cln);
+ sec += (link_fd->dir_pos.sname.ofs >> fs_info->fat.vol.sec_log2);
+ byte = (link_fd->dir_pos.sname.ofs & (fs_info->fat.vol.bps - 1));
+
+ ret = _fat_block_read(parent_loc->mt_entry,
+ sec, byte, MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE,
+ link_node);
+ if (ret < 0) {
+ return -1;
+ }
+ /*
+ * copy various attributes
+ */
+ *MSDOS_DIR_ATTR(short_node) =*MSDOS_DIR_ATTR(link_node);
+ *MSDOS_DIR_CRT_TIME_TENTH(short_node)=*MSDOS_DIR_CRT_TIME_TENTH(link_node);
+ *MSDOS_DIR_CRT_TIME(short_node) =*MSDOS_DIR_CRT_TIME(link_node);
+ *MSDOS_DIR_CRT_DATE(short_node) =*MSDOS_DIR_CRT_DATE(link_node);
+
+ /*
+ * copy/set "file size", "first cluster"
+ */
+ *MSDOS_DIR_FILE_SIZE(short_node) =*MSDOS_DIR_FILE_SIZE(link_node);
+
+ *MSDOS_DIR_FIRST_CLUSTER_LOW(short_node) =
+ *MSDOS_DIR_FIRST_CLUSTER_LOW(link_node);
+ *MSDOS_DIR_FIRST_CLUSTER_HI(short_node) =
+ *MSDOS_DIR_FIRST_CLUSTER_HI(link_node);
+ /*
+ * set "archive bit" due to changes
+ */
+ *MSDOS_DIR_ATTR(short_node) |= MSDOS_ATTR_ARCHIVE;
+ }
+ else { /* regular file... */
+ *MSDOS_DIR_ATTR(short_node) |= MSDOS_ATTR_ARCHIVE;
+ }
+
+ /*
+ * find free space in the parent directory and write new initialized
+ * FAT 32 Bytes Directory Entry Structure to the disk
+ */
+ rc = msdos_get_name_node(parent_loc, true, name, name_len,
+ name_type, &dir_pos, short_node);
+ if ( rc != RC_OK )
+ return rc;
+
+ /*
+ * if we create a new file we are done, if directory there are more steps
+ * to do
+ */
+ if (type == MSDOS_DIRECTORY)
+ {
+ /* open new directory as fat-file */
+ rc = fat_file_open(parent_loc->mt_entry, &dir_pos, &fat_fd);
+ if (rc != RC_OK)
+ goto err;
+
+ /*
+ * we opened fat-file for node we just created, so initialize fat-file
+ * descritor
+ */
+ fat_fd->fat_file_size = 0;
+ fat_fd->fat_file_type = FAT_DIRECTORY;
+ fat_fd->size_limit = MSDOS_MAX_DIR_LENGHT;
+
+ /*
+ * dot and dotdot entries are identical to new node except the
+ * names
+ */
+ memcpy(DOT_NODE_P(dot_dotdot), short_node,
+ MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE);
+ memcpy(DOTDOT_NODE_P(dot_dotdot), short_node,
+ MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE);
+ memcpy(MSDOS_DIR_NAME(DOT_NODE_P(dot_dotdot)), MSDOS_DOT_NAME,
+ MSDOS_NAME_MAX);
+ memcpy(MSDOS_DIR_NAME(DOTDOT_NODE_P(dot_dotdot)), MSDOS_DOTDOT_NAME,
+ MSDOS_NAME_MAX);
+
+ /* set up cluster num for dotdot entry */
+ /*
+ * here we can ommit FAT32 condition because for all FAT types dirs
+ * right under root dir should contain 0 in dotdot entry but for
+ * FAT12/16 parent_fat_fd->cluster_num always contains such value
+ */
+ if ((FAT_FD_OF_ROOT_DIR(parent_fat_fd)) &&
+ (fs_info->fat.vol.type & FAT_FAT32))
+ {
+ *MSDOS_DIR_FIRST_CLUSTER_LOW(DOTDOT_NODE_P(dot_dotdot)) = 0x0000;
+ *MSDOS_DIR_FIRST_CLUSTER_HI(DOTDOT_NODE_P(dot_dotdot)) = 0x0000;
+ }
+ else
+ {
+ *MSDOS_DIR_FIRST_CLUSTER_LOW(DOTDOT_NODE_P(dot_dotdot)) =
+ CT_LE_W((uint16_t )((parent_fat_fd->cln) & 0x0000FFFF));
+ *MSDOS_DIR_FIRST_CLUSTER_HI(DOTDOT_NODE_P(dot_dotdot)) =
+ CT_LE_W((uint16_t )(((parent_fat_fd->cln) & 0xFFFF0000)>>16));
+ }
+
+ /*
+ * write dot and dotdot entries to new fat-file: currently fat-file
+ * correspondes to a new node is zero length, so it will be extended
+ * by one cluster and entries will be written
+ */
+ ret = fat_file_write(parent_loc->mt_entry, fat_fd, 0,
+ MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE * 2,
+ (uint8_t *)dot_dotdot);
+ if (ret < 0)
+ {
+ rc = -1;
+ goto error;
+ }
+
+ /* increment fat-file size by cluster size */
+ fat_fd->fat_file_size += fs_info->fat.vol.bpc;
+
+ /* set up cluster num for dot entry */
+ *MSDOS_DIR_FIRST_CLUSTER_LOW(DOT_NODE_P(dot_dotdot)) =
+ CT_LE_W((uint16_t )((fat_fd->cln) & 0x0000FFFF));
+ *MSDOS_DIR_FIRST_CLUSTER_HI(DOT_NODE_P(dot_dotdot)) =
+ CT_LE_W((uint16_t )(((fat_fd->cln) & 0xFFFF0000) >> 16));
+
+ /* rewrite dot entry */
+ ret = fat_file_write(parent_loc->mt_entry, fat_fd, 0,
+ MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE,
+ (uint8_t *)DOT_NODE_P(dot_dotdot));
+ if (ret < 0)
+ {
+ rc = -1;
+ goto error;
+ }
+
+ /* write first cluster num of a new directory to disk */
+ rc = msdos_set_first_cluster_num(parent_loc->mt_entry, fat_fd);
+ if (rc != RC_OK)
+ goto error;
+
+ fat_file_close(parent_loc->mt_entry, fat_fd);
+ }
+ return RC_OK;
+
+error:
+ fat_file_close(parent_loc->mt_entry, fat_fd);
+
+err:
+ /* mark the used 32bytes structure on the disk as free */
+ msdos_set_first_char4file_name(parent_loc->mt_entry, &dir_pos, 0xE5);
+ return rc;
+}
diff --git a/cpukit/libfs/src/dosfs/msdos_dir.c b/cpukit/libfs/src/dosfs/msdos_dir.c
new file mode 100644
index 0000000000..7e6bcf3db6
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/msdos_dir.c
@@ -0,0 +1,706 @@
+/*
+ * MSDOS directory handlers implementation
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ */
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <ctype.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <rtems/libio_.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <dirent.h>
+
+#include "fat.h"
+#include "fat_fat_operations.h"
+#include "fat_file.h"
+
+#include "msdos.h"
+
+/* msdos_dir_open --
+ * Open fat-file which correspondes to the directory being opened and
+ * set offset field of file control block to zero.
+ *
+ * PARAMETERS:
+ * iop - file control block
+ * pathname - name
+ * flag - flags
+ * mode - mode
+ *
+ * RETURNS:
+ * RC_OK, if directory opened successfully, or -1 if error occured (errno
+ * set apropriately)
+ */
+int
+msdos_dir_open(rtems_libio_t *iop, const char *pathname, uint32_t flag,
+ uint32_t mode)
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = iop->pathinfo.mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = iop->pathinfo.node_access;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one( EIO );
+
+ rc = fat_file_reopen(fat_fd);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+
+ iop->offset = 0;
+ rtems_semaphore_release(fs_info->vol_sema);
+ return RC_OK;
+}
+
+/* msdos_dir_close --
+ * Close fat-file which correspondes to the directory being closed
+ *
+ * PARAMETERS:
+ * iop - file control block
+ *
+ * RETURNS:
+ * RC_OK, if directory closed successfully, or -1 if error occured (errno
+ * set apropriately.
+ */
+int
+msdos_dir_close(rtems_libio_t *iop)
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = iop->pathinfo.mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = iop->pathinfo.node_access;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one( EIO );
+
+ rc = fat_file_close(iop->pathinfo.mt_entry, fat_fd);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return RC_OK;
+}
+
+/* msdos_format_dirent_with_dot --
+ * This routine convert a (short) MSDOS filename as present on disk
+ * (fixed 8+3 characters, filled with blanks, without separator dot)
+ * to a "normal" format, with between 0 and 8 name chars,
+ * a separating dot and up to 3 extension characters
+ * Rules to work:
+ * - copy any (0-8) "name" part characters that are non-blank
+ * - if an extension exists, append a dot
+ * - copy any (0-3) non-blank extension characters
+ * - append a '\0' (dont count it for the rturn code
+ *
+ * PARAMETERS:
+ * dst: pointer to destination char array (must be big enough)
+ * src: pointer to source characters
+ *
+ *
+ * RETURNS:
+ * the number of bytes (without trailing '\0'(written to destination
+ */
+static ssize_t
+msdos_format_dirent_with_dot(char *dst,const char *src)
+{
+ ssize_t len;
+ int i;
+ const char *src_tmp;
+
+ /*
+ * find last non-blank character of base name
+ */
+ for ((i = MSDOS_SHORT_BASE_LEN ,
+ src_tmp = src + MSDOS_SHORT_BASE_LEN-1);
+ ((i > 0) &&
+ (*src_tmp == ' '));
+ i--,src_tmp--)
+ {};
+ /*
+ * copy base name to destination
+ */
+ src_tmp = src;
+ len = i;
+ while (i-- > 0) {
+ *dst++ = tolower((unsigned char)(*src_tmp++));
+ }
+ /*
+ * find last non-blank character of extension
+ */
+ for ((i = MSDOS_SHORT_EXT_LEN ,
+ src_tmp = src + MSDOS_SHORT_BASE_LEN+MSDOS_SHORT_EXT_LEN-1);
+ ((i > 0) &&
+ (*src_tmp == ' '));
+ i--,src_tmp--)
+ {};
+ /*
+ * extension is not empty
+ */
+ if (i > 0) {
+ *dst++ = '.'; /* append dot */
+ len += i + 1; /* extension + dot */
+ src_tmp = src + MSDOS_SHORT_BASE_LEN;
+ while (i-- > 0) {
+ *dst++ = tolower((unsigned char)(*src_tmp++));
+ len++;
+ }
+ }
+ *dst = '\0'; /* terminate string */
+
+ return len;
+}
+
+/* msdos_dir_read --
+ * This routine will read the next directory entry based on the directory
+ * offset. The offset should be equal to -n- time the size of an
+ * individual dirent structure. If n is not an integer multiple of the
+ * sizeof a dirent structure, an integer division will be performed to
+ * determine directory entry that will be returned in the buffer. Count
+ * should reflect -m- times the sizeof dirent bytes to be placed in the
+ * buffer.
+ * If there are not -m- dirent elements from the current directory
+ * position to the end of the exisiting file, the remaining entries will
+ * be placed in the buffer and the returned value will be equal to
+ * -m actual- times the size of a directory entry.
+ *
+ * PARAMETERS:
+ * iop - file control block
+ * buffer - buffer provided by user
+ * count - count of bytes to read
+ *
+ * RETURNS:
+ * the number of bytes read on success, or -1 if error occured (errno
+ * set apropriately).
+ */
+ssize_t
+msdos_dir_read(rtems_libio_t *iop, void *buffer, size_t count)
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = iop->pathinfo.mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = iop->pathinfo.node_access;
+ fat_file_fd_t *tmp_fat_fd = NULL;
+ struct dirent tmp_dirent;
+ uint32_t start = 0;
+ ssize_t ret = 0;
+ uint32_t cmpltd = 0;
+ uint32_t j = 0, i = 0;
+ uint32_t bts2rd = 0;
+ uint32_t cur_cln = 0;
+ uint32_t lfn_start = FAT_FILE_SHORT_NAME;
+ uint8_t lfn_checksum = 0;
+ int lfn_entries = 0;
+
+ /*
+ * cast start and count - protect against using sizes that are not exact
+ * multiples of the -dirent- size. These could result in unexpected
+ * results
+ */
+ start = iop->offset / sizeof(struct dirent);
+ count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
+
+ /*
+ * optimization: we know that root directory for FAT12/16 volumes is
+ * sequential set of sectors and any cluster is sequential set of sectors
+ * too, so read such set of sectors is quick operation for low-level IO
+ * layer.
+ */
+ bts2rd = (FAT_FD_OF_ROOT_DIR(fat_fd) &&
+ (fs_info->fat.vol.type & (FAT_FAT12 | FAT_FAT16))) ?
+ fat_fd->fat_file_size :
+ fs_info->fat.vol.bpc;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ while (count > 0)
+ {
+ /*
+ * fat-file is already opened by open call, so read it
+ * Always read directory fat-file from the beggining because of MSDOS
+ * directories feature :( - we should count elements currently
+ * present in the directory because there may be holes :)
+ */
+ ret = fat_file_read(iop->pathinfo.mt_entry, fat_fd, (j * bts2rd),
+ bts2rd, fs_info->cl_buf);
+ if (ret < MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ rtems_set_errno_and_return_minus_one(EIO);
+ }
+
+ for (i = 0; i < ret; i += MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE)
+ {
+ char* entry = (char*) fs_info->cl_buf + i;
+
+ /*
+ * Is this directory from here on empty ?
+ */
+ if ((*MSDOS_DIR_ENTRY_TYPE(entry)) ==
+ MSDOS_THIS_DIR_ENTRY_AND_REST_EMPTY)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return cmpltd;
+ }
+
+ /* Is the directory entry empty */
+ if ((*MSDOS_DIR_ENTRY_TYPE(entry)) == MSDOS_THIS_DIR_ENTRY_EMPTY)
+ continue;
+
+ /* Is the directory entry empty a volume label */
+ if (((*MSDOS_DIR_ATTR(entry)) & MSDOS_ATTR_VOLUME_ID) &&
+ ((*MSDOS_DIR_ATTR(entry) & MSDOS_ATTR_LFN_MASK) != MSDOS_ATTR_LFN))
+ continue;
+
+ /*
+ * Check the attribute to see if the entry is for a long file
+ * name.
+ */
+ if ((*MSDOS_DIR_ATTR(entry) & MSDOS_ATTR_LFN_MASK) ==
+ MSDOS_ATTR_LFN)
+ {
+ int o;
+ char* p;
+ int q;
+
+ /*
+ * Is this is the first entry of a LFN ?
+ */
+ if (lfn_start == FAT_FILE_SHORT_NAME)
+ {
+ /*
+ * The first entry must have the last long entry flag set.
+ */
+ if ((*MSDOS_DIR_ENTRY_TYPE(entry) &
+ MSDOS_LAST_LONG_ENTRY) == 0)
+ continue;
+
+ /*
+ * Remember the start location of the long file name.
+ */
+ lfn_start =
+ ((j * bts2rd) + i) / MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE;
+
+ /*
+ * Get the number of entries so we can count down and
+ * also the checksum of the short entry.
+ */
+ lfn_entries = (*MSDOS_DIR_ENTRY_TYPE(entry) &
+ MSDOS_LAST_LONG_ENTRY_MASK);
+ lfn_checksum = *MSDOS_DIR_LFN_CHECKSUM(entry);
+ memset (tmp_dirent.d_name, 0, sizeof(tmp_dirent.d_name));
+ }
+
+ /*
+ * If the entry number or the check sum do not match
+ * forget this series of long directory entries. These could
+ * be orphaned entries depending on the history of the
+ * disk.
+ */
+ if ((lfn_entries != (*MSDOS_DIR_ENTRY_TYPE(entry) &
+ MSDOS_LAST_LONG_ENTRY_MASK)) ||
+ (lfn_checksum != *MSDOS_DIR_LFN_CHECKSUM(entry)))
+ {
+ lfn_start = FAT_FILE_SHORT_NAME;
+ continue;
+ }
+
+ /*
+ * Extract the file name into the directory entry. The data is
+ * stored in UNICODE characters (16bit). No translation is
+ * currently supported.
+ *
+ * The DOS maximum length is 255 characters without the
+ * trailing nul character. We need to range check the length to
+ * fit in the directory entry name field.
+ */
+
+ lfn_entries--;
+ p = entry + 1;
+ o = lfn_entries * MSDOS_LFN_LEN_PER_ENTRY;
+
+ for (q = 0; q < MSDOS_LFN_LEN_PER_ENTRY; q++)
+ {
+ if (o >= (sizeof(tmp_dirent.d_name) - 1))
+ break;
+
+ tmp_dirent.d_name[o++] = *p;
+
+ if (*p == '\0')
+ break;
+
+ switch (q)
+ {
+ case 4:
+ p += 5;
+ break;
+ case 10:
+ p += 4;
+ break;
+ default:
+ p += 2;
+ break;
+ }
+ }
+ }
+ else
+ {
+ fat_dir_pos_t dir_pos;
+
+ /*
+ * Skip active entries until get the entry to start from.
+ */
+ if (start)
+ {
+ lfn_start = FAT_FILE_SHORT_NAME;
+ start--;
+ continue;
+ }
+
+ /*
+ * Move the entry to the return buffer
+ *
+ * unfortunately there is no method to extract ino except to
+ * open fat-file descriptor :( ... so, open it
+ */
+
+ /* get number of cluster we are working with */
+ rc = fat_file_ioctl(iop->pathinfo.mt_entry, fat_fd, F_CLU_NUM,
+ j * bts2rd, &cur_cln);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+
+ fat_dir_pos_init(&dir_pos);
+ dir_pos.sname.cln = cur_cln;
+ dir_pos.sname.ofs = i;
+ rc = fat_file_open(iop->pathinfo.mt_entry, &dir_pos, &tmp_fat_fd);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+
+ /* fill in dirent structure */
+ /* XXX: from what and in what d_off should be computed ?! */
+ tmp_dirent.d_off = start + cmpltd;
+ tmp_dirent.d_reclen = sizeof(struct dirent);
+ tmp_dirent.d_ino = tmp_fat_fd->ino;
+
+ /*
+ * If a long file name check if the correct number of
+ * entries have been found and if the checksum is correct.
+ * If not return the short file name.
+ */
+ if (lfn_start != FAT_FILE_SHORT_NAME)
+ {
+ uint8_t cs = 0;
+ uint8_t* p = (uint8_t*) entry;
+ int i;
+
+ for (i = 0; i < 11; i++, p++)
+ cs = ((cs & 1) ? 0x80 : 0) + (cs >> 1) + *p;
+
+ if (lfn_entries || (lfn_checksum != cs))
+ lfn_start = FAT_FILE_SHORT_NAME;
+ }
+
+ if (lfn_start == FAT_FILE_SHORT_NAME)
+ {
+ /*
+ * convert dir entry from fixed 8+3 format (without dot)
+ * to 0..8 + 1dot + 0..3 format
+ */
+ tmp_dirent.d_namlen = msdos_format_dirent_with_dot(
+ tmp_dirent.d_name, entry); /* src text */
+ }
+ else
+ {
+ tmp_dirent.d_namlen = strlen(tmp_dirent.d_name);
+ }
+
+ memcpy(buffer + cmpltd, &tmp_dirent, sizeof(struct dirent));
+
+ iop->offset = iop->offset + sizeof(struct dirent);
+ cmpltd += (sizeof(struct dirent));
+ count -= (sizeof(struct dirent));
+
+ /* inode number extracted, close fat-file */
+ rc = fat_file_close(iop->pathinfo.mt_entry, tmp_fat_fd);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+ }
+
+ if (count <= 0)
+ break;
+ }
+ j++;
+ }
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return cmpltd;
+}
+
+/* msdos_dir_write --
+ * no write for directory
+ */
+
+/* msdos_dir_lseek --
+ *
+ * This routine will behave in one of three ways based on the state of
+ * argument whence. Based on the state of its value the offset argument will
+ * be interpreted using one of the following methods:
+ *
+ * SEEK_SET - offset is the absolute byte offset from the start of the
+ * logical start of the dirent sequence that represents the
+ * directory
+ * SEEK_CUR - offset is used as the relative byte offset from the current
+ * directory position index held in the iop structure
+ * SEEK_END - N/A --> This will cause an assert.
+ *
+ * PARAMETERS:
+ * iop - file control block
+ * offset - offset
+ * whence - predefine directive
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno
+ * set apropriately).
+ */
+rtems_off64_t
+msdos_dir_lseek(rtems_libio_t *iop, rtems_off64_t offset, int whence)
+{
+ switch (whence)
+ {
+ case SEEK_SET:
+ case SEEK_CUR:
+ break;
+ /*
+ * Movement past the end of the directory via lseek is not a
+ * permitted operation
+ */
+ case SEEK_END:
+ default:
+ rtems_set_errno_and_return_minus_one( EINVAL );
+ break;
+ }
+ return RC_OK;
+}
+
+/* msdos_dir_stat --
+ *
+ * This routine will obtain the following information concerning the current
+ * directory:
+ * st_dev device id
+ * st_ino node serial number :)
+ * st_mode mode extracted from the node
+ * st_size total size in bytes
+ * st_blksize blocksize for filesystem I/O
+ * st_blocks number of blocks allocated
+ * stat_mtime time of last modification
+ *
+ * PARAMETERS:
+ * loc - this directory
+ * buf - stat buffer provided by user
+ *
+ * RETURNS:
+ * RC_OK and filled stat buffer on success, or -1 if error occured (errno
+ * set apropriately).
+ */
+int
+msdos_dir_stat(
+ rtems_filesystem_location_info_t *loc,
+ struct stat *buf
+ )
+{
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = loc->mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = loc->node_access;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ buf->st_dev = fs_info->fat.vol.dev;
+ buf->st_ino = fat_fd->ino;
+ buf->st_mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO;
+ buf->st_rdev = 0ll;
+ buf->st_size = fat_fd->fat_file_size;
+ buf->st_blocks = fat_fd->fat_file_size >> FAT_SECTOR512_BITS;
+ buf->st_blksize = fs_info->fat.vol.bps;
+ buf->st_mtime = fat_fd->mtime;
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return RC_OK;
+}
+
+/* msdos_dir_truncate --
+ * No truncate for directory.
+ *
+ * PARAMETERS:
+ *
+ * RETURNS:
+ *
+ */
+
+/* msdos_dir_sync --
+ * The following routine does a syncronization on a MSDOS directory node.
+ * DIR_WrtTime, DIR_WrtDate and DIR_fileSize fields of 32 Bytes Directory
+ * Entry Structure should not be updated for directories, so only call
+ * to corresponding fat-file routine.
+ *
+ * PARAMETERS:
+ * iop - file control block
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set apropriately).
+ */
+int
+msdos_dir_sync(rtems_libio_t *iop)
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ fat_file_fd_t *fat_fd = iop->pathinfo.node_access;
+ msdos_fs_info_t *fs_info = iop->pathinfo.mt_entry->fs_info;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ rc = fat_file_datasync(iop->pathinfo.mt_entry, fat_fd);
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+}
+
+/* msdos_dir_chmod --
+ * Change the attributes of the directory. This currently does
+ * nothing and returns no error.
+ *
+ * PARAMETERS:
+ * pathloc - node description
+ * mode - the new mode
+ *
+ * RETURNS:
+ * RC_OK always
+ */
+int
+msdos_dir_chmod(rtems_filesystem_location_info_t *pathloc,
+ mode_t mode)
+{
+ return RC_OK;
+}
+
+/* msdos_dir_rmnod --
+ * Remove directory node.
+ *
+ * Check that this directory node is not opened as fat-file, is empty and
+ * not filesystem root node. If all this conditions met then delete.
+ *
+ * PARAMETERS:
+ * pathloc - node description
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set apropriately).
+ */
+int
+msdos_dir_rmnod(rtems_filesystem_location_info_t *parent_pathloc,
+ rtems_filesystem_location_info_t *pathloc)
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = pathloc->mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = pathloc->node_access;
+ bool is_empty = false;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ /*
+ * We deny attempts to delete open directory (if directory is current
+ * directory we assume it is open one)
+ */
+ if (fat_fd->links_num > 1)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ rtems_set_errno_and_return_minus_one(EBUSY);
+ }
+
+ /*
+ * You cannot remove a node that still has children
+ */
+ rc = msdos_dir_is_empty(pathloc->mt_entry, fat_fd, &is_empty);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+
+ if (!is_empty)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ rtems_set_errno_and_return_minus_one(ENOTEMPTY);
+ }
+
+ /*
+ * You cannot remove the file system root node.
+ */
+ if (pathloc->mt_entry->mt_fs_root.node_access == pathloc->node_access)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ rtems_set_errno_and_return_minus_one(EBUSY);
+ }
+
+ /*
+ * You cannot remove a mountpoint.
+ * not used - mount() not implemenetd yet.
+ */
+
+ /* mark file removed */
+ rc = msdos_set_first_char4file_name(pathloc->mt_entry, &fat_fd->dir_pos,
+ MSDOS_THIS_DIR_ENTRY_EMPTY);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+
+ fat_file_mark_removed(pathloc->mt_entry, fat_fd);
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+}
diff --git a/cpukit/libfs/src/dosfs/msdos_eval.c b/cpukit/libfs/src/dosfs/msdos_eval.c
new file mode 100644
index 0000000000..c128be8ff5
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/msdos_eval.c
@@ -0,0 +1,437 @@
+/*
+ * MSDOS evaluation routines
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+
+#include <rtems/libio_.h>
+
+#include "fat.h"
+#include "fat_fat_operations.h"
+#include "fat_file.h"
+
+#include "msdos.h"
+
+/* msdos_set_handlers --
+ * Set handlers for the node with specified type(i.e. handlers for file
+ * or directory).
+ *
+ * PARAMETERS:
+ * loc - node description
+ *
+ * RETURNS:
+ * None
+ */
+static void
+msdos_set_handlers(rtems_filesystem_location_info_t *loc)
+{
+ msdos_fs_info_t *fs_info = loc->mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = loc->node_access;
+
+ if (fat_fd->fat_file_type == FAT_DIRECTORY)
+ loc->handlers = fs_info->directory_handlers;
+ else
+ loc->handlers = fs_info->file_handlers;
+}
+
+/* msdos_eval_path --
+ *
+ * The following routine evaluate path for a node that wishes to be
+ * accessed. Structure 'pathloc' is returned with a pointer to the
+ * node to be accessed.
+ *
+ * PARAMETERS:
+ * pathname - path for evaluation
+ * flags - flags
+ * pathloc - node description (IN/OUT)
+ *
+ * RETURNS:
+ * RC_OK and filled pathloc on success, or -1 if error occured
+ * (errno set appropriately)
+ *
+ */
+int
+msdos_eval_path(
+ const char *pathname,
+ size_t pathnamelen,
+ int flags,
+ rtems_filesystem_location_info_t *pathloc
+ )
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = pathloc->mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = NULL;
+ rtems_filesystem_location_info_t newloc;
+ int i = 0;
+ int token_len = 0;
+ msdos_token_types_t type = MSDOS_CURRENT_DIR;
+ const char *token;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ if (!pathloc->node_access)
+ {
+ errno = ENOENT;
+ rc = -1;
+ goto err;
+ }
+
+ fat_fd = pathloc->node_access;
+
+ rc = fat_file_reopen(fat_fd);
+ if (rc != RC_OK)
+ goto err;
+
+ while ((type != MSDOS_NO_MORE_PATH) && (type != MSDOS_INVALID_TOKEN))
+ {
+ type = msdos_get_token(&pathname[i], pathnamelen, &token, &token_len);
+ pathnamelen -= token_len;
+ i += token_len;
+
+ fat_fd = pathloc->node_access;
+
+ switch (type)
+ {
+ case MSDOS_UP_DIR:
+ /*
+ * Only a directory can be decended into.
+ */
+ if (fat_fd->fat_file_type != FAT_DIRECTORY)
+ {
+ errno = ENOTSUP;
+ rc = -1;
+ goto error;
+ }
+
+ /*
+ * Am I at the root of this mounted filesystem?
+ */
+ if (pathloc->node_access ==
+ pathloc->mt_entry->mt_fs_root.node_access)
+ {
+ /*
+ * Am I at the root of all filesystems?
+ * XXX: MSDOS is not supposed to be base fs.
+ */
+ if (pathloc->node_access ==
+ rtems_filesystem_root.node_access)
+ {
+ break; /* Throw out the .. in this case */
+ }
+ else
+ {
+ newloc = pathloc->mt_entry->mt_point_node;
+ *pathloc = newloc;
+
+ rc = fat_file_close(pathloc->mt_entry, fat_fd);
+ if (rc != RC_OK)
+ goto err;
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return (*pathloc->ops->evalpath_h)(&(pathname[i-token_len]),
+ pathnamelen + token_len,
+ flags, pathloc);
+ }
+ }
+ else
+ {
+ rc = msdos_find_name(pathloc, token, token_len);
+ if (rc != RC_OK)
+ {
+ if (rc == MSDOS_NAME_NOT_FOUND_ERR)
+ {
+ errno = ENOENT;
+ rc = -1;
+ }
+ goto error;
+ }
+ }
+ break;
+
+ case MSDOS_NAME:
+ /*
+ * Only a directory can be decended into.
+ */
+ if (fat_fd->fat_file_type != FAT_DIRECTORY)
+ {
+ errno = ENOTSUP;
+ rc = -1;
+ goto error;
+ }
+
+ /*
+ * Otherwise find the token name in the present location and
+ * set the node access to the point we have found.
+ */
+ rc = msdos_find_name(pathloc, token, token_len);
+ if (rc != RC_OK)
+ {
+ if (rc == MSDOS_NAME_NOT_FOUND_ERR)
+ {
+ errno = ENOENT;
+ rc = -1;
+ }
+ goto error;
+ }
+ break;
+
+ case MSDOS_NO_MORE_PATH:
+ case MSDOS_CURRENT_DIR:
+ break;
+
+ case MSDOS_INVALID_TOKEN:
+ errno = ENAMETOOLONG;
+ rc = -1;
+ goto error;
+ break;
+
+ }
+ }
+
+ /*
+ * Always return the root node.
+ *
+ * If we are at a node that is a mount point. Set loc to the
+ * new fs root node and let let the mounted filesystem set the handlers.
+ *
+ * NOTE: The behavior of stat() on a mount point appears to be
+ * questionable.
+ * NOTE: MSDOS filesystem currently doesn't support mount functionality ->
+ * action not implemented
+ */
+ fat_fd = pathloc->node_access;
+
+ msdos_set_handlers(pathloc);
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return RC_OK;
+
+error:
+ fat_file_close(pathloc->mt_entry, fat_fd);
+
+err:
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+}
+
+/* msdos_eval4make --
+ * The following routine evaluate path for a new node to be created.
+ * 'pathloc' is returned with a pointer to the parent of the new node.
+ * 'name' is returned with a pointer to the first character in the
+ * new node name. The parent node is verified to be a directory.
+ *
+ * PARAMETERS:
+ * path - path for evaluation
+ * pathloc - IN/OUT (start point for evaluation/parent directory for
+ * creation)
+ * name - new node name
+ *
+ * RETURNS:
+ * RC_OK, filled pathloc for parent directory and name of new node on
+ * success, or -1 if error occured (errno set appropriately)
+ */
+int
+msdos_eval4make(
+ const char *path,
+ rtems_filesystem_location_info_t *pathloc,
+ const char **name
+ )
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = pathloc->mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = NULL;
+ rtems_filesystem_location_info_t newloc;
+ msdos_token_types_t type;
+ int i = 0;
+ int token_len;
+ const char *token;
+ bool done = false;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ if (!pathloc->node_access)
+ {
+ errno = ENOENT;
+ rc = -1;
+ goto err;
+ }
+
+ fat_fd = pathloc->node_access;
+
+ rc = fat_file_reopen(fat_fd);
+ if (rc != RC_OK)
+ goto err;
+
+ while (!done)
+ {
+ type = msdos_get_token(&path[i], strlen(&path[i]), &token, &token_len);
+ i += token_len;
+ fat_fd = pathloc->node_access;
+
+ switch (type)
+ {
+ case MSDOS_UP_DIR:
+ /*
+ * Only a directory can be decended into.
+ */
+ if (fat_fd->fat_file_type != FAT_DIRECTORY)
+ {
+ errno = ENOTDIR;
+ rc = -1;
+ goto error;
+ }
+
+ /*
+ * Am I at the root of this mounted filesystem?
+ */
+ if (pathloc->node_access ==
+ pathloc->mt_entry->mt_fs_root.node_access)
+ {
+ /*
+ * Am I at the root of all filesystems?
+ * XXX: MSDOS is not supposed to be base fs.
+ */
+ if (pathloc->node_access ==
+ rtems_filesystem_root.node_access)
+ {
+ break; /* Throw out the .. in this case */
+ }
+ else
+ {
+ newloc = pathloc->mt_entry->mt_point_node;
+ *pathloc = newloc;
+
+ rc = fat_file_close(pathloc->mt_entry, fat_fd);
+ if (rc != RC_OK)
+ goto err;
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return (*pathloc->ops->evalformake_h)(&path[i-token_len],
+ pathloc, name);
+ }
+ }
+ else
+ {
+ rc = msdos_find_name(pathloc, token, token_len);
+ if (rc != RC_OK)
+ {
+ if (rc == MSDOS_NAME_NOT_FOUND_ERR)
+ {
+ errno = ENOENT;
+ rc = -1;
+ }
+ goto error;
+ }
+ }
+ break;
+
+ case MSDOS_NAME:
+ /*
+ * Only a directory can be decended into.
+ */
+ if (fat_fd->fat_file_type != FAT_DIRECTORY)
+ {
+ errno = ENOTDIR;
+ rc = -1;
+ goto error;
+ }
+
+ /*
+ * Otherwise find the token name in the present location and
+ * set the node access to the point we have found.
+ */
+ rc = msdos_find_name(pathloc, token, token_len);
+ if (rc)
+ {
+ if (rc != MSDOS_NAME_NOT_FOUND_ERR)
+ {
+ errno = ENOENT;
+ rc = -1;
+ goto error;
+ }
+ else
+ done = true;
+ }
+ break;
+
+ case MSDOS_NO_MORE_PATH:
+ errno = EEXIST;
+ rc = -1;
+ goto error;
+ break;
+
+ case MSDOS_CURRENT_DIR:
+ break;
+
+ case MSDOS_INVALID_TOKEN:
+ errno = ENAMETOOLONG;
+ rc = -1;
+ goto error;
+ break;
+
+ }
+ }
+
+ *name = &path[i - token_len];
+
+ /*
+ * We have evaluated the path as far as we can.
+ * Verify there is not any invalid stuff at the end of the name.
+ */
+ for( ; path[i] != '\0'; i++)
+ {
+ if (!msdos_is_separator(path[i]))
+ {
+ errno = ENOENT;
+ rc = -1;
+ goto error;
+ }
+ }
+
+ fat_fd = pathloc->node_access;
+
+ if (fat_fd->fat_file_type != FAT_DIRECTORY)
+ {
+ errno = ENOTDIR;
+ rc = -1;
+ goto error;
+ }
+
+ msdos_set_handlers(pathloc);
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return RC_OK;
+
+error:
+ fat_file_close(pathloc->mt_entry, fat_fd);
+
+err:
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+}
diff --git a/cpukit/libfs/src/dosfs/msdos_file.c b/cpukit/libfs/src/dosfs/msdos_file.c
new file mode 100644
index 0000000000..0b05296df9
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/msdos_file.c
@@ -0,0 +1,503 @@
+/*
+ * MSDOS file handlers implementation
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ */
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <errno.h>
+
+#include <rtems.h>
+#include <rtems/libio.h>
+#include <rtems/libio_.h>
+
+#include "fat.h"
+#include "fat_fat_operations.h"
+#include "fat_file.h"
+
+#include "msdos.h"
+
+/* msdos_file_open --
+ * Open fat-file which correspondes to the file
+ *
+ * PARAMETERS:
+ * iop - file control block
+ * pathname - name
+ * flag - flags
+ * mode - mode
+ *
+ * RETURNS:
+ * RC_OK, if file opened successfully, or -1 if error occured
+ * and errno set appropriately
+ */
+int
+msdos_file_open(rtems_libio_t *iop, const char *pathname, uint32_t flag,
+ uint32_t mode)
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = iop->pathinfo.mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = iop->pathinfo.node_access;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ rc = fat_file_reopen(fat_fd);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+
+ if (iop->flags & LIBIO_FLAGS_APPEND)
+ iop->offset = fat_fd->fat_file_size;
+
+ iop->size = fat_fd->fat_file_size;
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return RC_OK;
+}
+
+/* msdos_file_close --
+ * Close fat-file which correspondes to the file. If fat-file descriptor
+ * which correspondes to the file is not marked "removed", synchronize
+ * size, first cluster number, write time and date fields of the file.
+ *
+ * PARAMETERS:
+ * iop - file control block
+ *
+ * RETURNS:
+ * RC_OK, if file closed successfully, or -1 if error occured (errno set
+ * appropriately)
+ */
+int
+msdos_file_close(rtems_libio_t *iop)
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = iop->pathinfo.mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = iop->pathinfo.node_access;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ /*
+ * if fat-file descriptor is not marked as "removed", synchronize
+ * size, first cluster number, write time and date fields of the file
+ */
+ if (!FAT_FILE_IS_REMOVED(fat_fd))
+ {
+ rc = msdos_set_first_cluster_num(iop->pathinfo.mt_entry, fat_fd);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+
+ rc = msdos_set_file_size(iop->pathinfo.mt_entry, fat_fd);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+
+ rc = msdos_set_dir_wrt_time_and_date(iop->pathinfo.mt_entry, fat_fd);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+ }
+
+ rc = fat_file_close(iop->pathinfo.mt_entry, fat_fd);
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+}
+
+/* msdos_file_read --
+ * This routine read from file pointed to by file control block into
+ * the specified data buffer provided by user
+ *
+ * PARAMETERS:
+ * iop - file control block
+ * buffer - buffer provided by user
+ * count - the number of bytes to read
+ *
+ * RETURNS:
+ * the number of bytes read on success, or -1 if error occured (errno set
+ * appropriately)
+ */
+ssize_t
+msdos_file_read(rtems_libio_t *iop, void *buffer, size_t count)
+{
+ ssize_t ret = 0;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = iop->pathinfo.mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = iop->pathinfo.node_access;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ ret = fat_file_read(iop->pathinfo.mt_entry, fat_fd, iop->offset, count,
+ buffer);
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return ret;
+}
+
+/* msdos_file_write --
+ * This routine writes the specified data buffer into the file pointed to
+ * by file control block.
+ *
+ * PARAMETERS:
+ * iop - file control block
+ * buffer - data to write
+ * count - count of bytes to write
+ *
+ * RETURNS:
+ * the number of bytes written on success, or -1 if error occured
+ * and errno set appropriately
+ */
+ssize_t
+msdos_file_write(rtems_libio_t *iop,const void *buffer, size_t count)
+{
+ ssize_t ret = 0;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = iop->pathinfo.mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = iop->pathinfo.node_access;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ ret = fat_file_write(iop->pathinfo.mt_entry, fat_fd, iop->offset, count,
+ buffer);
+ if (ret < 0)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return -1;
+ }
+
+ /*
+ * update file size in both fat-file descriptor and file control block if
+ * file was extended
+ */
+ if (iop->offset + ret > fat_fd->fat_file_size)
+ fat_fd->fat_file_size = iop->offset + ret;
+
+ iop->size = fat_fd->fat_file_size;
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return ret;
+}
+
+/* msdos_file_lseek --
+ * Process lseek call to the file: extend file if lseek is up to the end
+ * of the file.
+ *
+ * PARAMETERS:
+ * iop - file control block
+ * offset - new offset
+ * whence - predefine directive
+ *
+ * RETURNS:
+ * new offset on success, or -1 if error occured (errno set
+ * appropriately).
+ */
+rtems_off64_t
+msdos_file_lseek(rtems_libio_t *iop, rtems_off64_t offset, int whence)
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = iop->pathinfo.mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = iop->pathinfo.node_access;
+ uint32_t real_size = 0;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ rc = fat_file_extend(iop->pathinfo.mt_entry, fat_fd, iop->offset,
+ &real_size);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+
+ if (real_size > fat_fd->fat_file_size)
+ fat_fd->fat_file_size = iop->offset = real_size;
+
+ iop->size = fat_fd->fat_file_size;
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return iop->offset;
+}
+
+/* msdos_file_stat --
+ *
+ * PARAMETERS:
+ * loc - node description
+ * buf - stat buffer provided by user
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set appropriately)
+ */
+int
+msdos_file_stat(
+ rtems_filesystem_location_info_t *loc,
+ struct stat *buf
+ )
+{
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = loc->mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = loc->node_access;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ buf->st_dev = fs_info->fat.vol.dev;
+ buf->st_ino = fat_fd->ino;
+ buf->st_mode = S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO;
+ buf->st_rdev = 0ll;
+ buf->st_size = fat_fd->fat_file_size;
+ buf->st_blocks = fat_fd->fat_file_size >> FAT_SECTOR512_BITS;
+ buf->st_blksize = fs_info->fat.vol.bps;
+ buf->st_mtime = fat_fd->mtime;
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return RC_OK;
+}
+
+/* msdos_file_ftruncate --
+ * Truncate the file (if new length is greater then current do nothing).
+ *
+ * PARAMETERS:
+ * iop - file control block
+ * length - new length
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set appropriately).
+ */
+int
+msdos_file_ftruncate(rtems_libio_t *iop, rtems_off64_t length)
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = iop->pathinfo.mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = iop->pathinfo.node_access;
+
+ if (length >= fat_fd->fat_file_size)
+ return RC_OK;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ rc = fat_file_truncate(iop->pathinfo.mt_entry, fat_fd, length);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+
+ /*
+ * fat_file_truncate do nothing if new length >= fat-file size, so update
+ * file size only if length < fat-file size
+ */
+ if (length < fat_fd->fat_file_size)
+ iop->size = fat_fd->fat_file_size = length;
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return RC_OK;
+}
+
+/* msdos_file_sync --
+ * Synchronize file - synchronize file data and if file is not removed
+ * synchronize file metadata.
+ *
+ * PARAMETERS:
+ * iop - file control block
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set appropriately)
+ */
+int
+msdos_file_sync(rtems_libio_t *iop)
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ fat_file_fd_t *fat_fd = iop->pathinfo.node_access;
+ msdos_fs_info_t *fs_info = iop->pathinfo.mt_entry->fs_info;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ /* synchronize file data */
+ rc = fat_file_datasync(iop->pathinfo.mt_entry, fat_fd);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+
+ /*
+ * if fat-file descriptor is not marked "removed" - synchronize file
+ * metadata
+ */
+ if (!FAT_FILE_IS_REMOVED(fat_fd))
+ {
+ rc = msdos_set_first_cluster_num(iop->pathinfo.mt_entry, fat_fd);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+ rc = msdos_set_file_size(iop->pathinfo.mt_entry, fat_fd);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+ rc = msdos_set_dir_wrt_time_and_date(iop->pathinfo.mt_entry, fat_fd);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+ }
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return RC_OK;
+}
+
+/* msdos_file_datasync --
+ * Synchronize file - synchronize only file data (metadata is letf intact).
+ *
+ * PARAMETERS:
+ * iop - file control block
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set appropriately)
+ */
+int
+msdos_file_datasync(rtems_libio_t *iop)
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ fat_file_fd_t *fat_fd = iop->pathinfo.node_access;
+ msdos_fs_info_t *fs_info = iop->pathinfo.mt_entry->fs_info;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ /* synchronize file data */
+ rc = fat_file_datasync(iop->pathinfo.mt_entry, fat_fd);
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return RC_OK;
+}
+
+
+/* msdos_file_ioctl --
+ *
+ *
+ * PARAMETERS:
+ * iop - file control block
+ * ...
+ *
+ * RETURNS:
+ *
+ */
+int
+msdos_file_ioctl(rtems_libio_t *iop,uint32_t command, void *buffer)
+{
+ int rc = RC_OK;
+
+ return rc;
+}
+
+/* msdos_file_chmod --
+ * Change the attributes of the file. This currently does
+ * nothing and returns no error.
+ *
+ * PARAMETERS:
+ * pathloc - node description
+ * mode - the new mode
+ *
+ * RETURNS:
+ * RC_OK always
+ */
+int
+msdos_file_chmod(rtems_filesystem_location_info_t *pathloc,
+ mode_t mode)
+{
+ return RC_OK;
+}
+
+/* msdos_file_rmnod --
+ * Remove node associated with a file - set up first name character to
+ * predefined value(and write it to the disk), and mark fat-file which
+ * correspondes to the file as "removed"
+ *
+ * PARAMETERS:
+ * pathloc - node description
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set appropriately)
+ */
+int
+msdos_file_rmnod(rtems_filesystem_location_info_t *parent_pathloc,
+ rtems_filesystem_location_info_t *pathloc)
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = pathloc->mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = pathloc->node_access;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ /* mark file removed */
+ rc = msdos_set_first_char4file_name(pathloc->mt_entry,
+ &fat_fd->dir_pos,
+ MSDOS_THIS_DIR_ENTRY_EMPTY);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+
+ fat_file_mark_removed(pathloc->mt_entry, fat_fd);
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return RC_OK;
+}
diff --git a/cpukit/libfs/src/dosfs/msdos_format.c b/cpukit/libfs/src/dosfs/msdos_format.c
new file mode 100644
index 0000000000..1394f811c4
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/msdos_format.c
@@ -0,0 +1,1127 @@
+/*===============================================================*\
+| Project: RTEMS msdos format functionality |
++-----------------------------------------------------------------+
+| File: msdos_format.c |
++-----------------------------------------------------------------+
+| Copyright (c) 2004 IMD |
+| Ingenieurbuero fuer Microcomputertechnik Th. Doerfler |
+| <Thomas.Doerfler@imd-systems.de> |
+| all rights reserved |
++-----------------------------------------------------------------+
+| this file contains msdos_format function. This function |
+| formats a disk partition conforming to MS-DOS conventions |
+| |
+| The license and distribution terms for this file may be |
+| found in the file LICENSE in this distribution or at |
+| http://www.rtems.com/license/LICENSE. |
+| |
++-----------------------------------------------------------------+
+| date history ID |
+| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
+| 29.10.04 creation doe |
+\*===============================================================*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <ctype.h>
+
+#include <rtems/libio_.h>
+
+#include "fat.h"
+#include "fat_fat_operations.h"
+#include "msdos.h"
+#include "dosfs.h"
+
+typedef struct {
+ uint32_t bytes_per_sector;
+ uint32_t totl_sector_cnt;
+ uint32_t rsvd_sector_cnt;
+
+ uint32_t sectors_per_cluster;
+ uint32_t sectors_per_fat;
+
+ uint32_t fat_start_sec;
+ uint32_t files_per_root_dir;
+ uint32_t root_dir_sectors;
+ uint32_t root_dir_start_sec;
+ uint32_t root_dir_fmt_sec_cnt;
+ uint32_t mbr_copy_sec; /* location of copy of mbr or 0 */
+ uint32_t fsinfo_sec; /* location of fsinfo sector or 0 */
+ uint8_t fat_num;
+ uint8_t media_code;
+ uint8_t fattype;
+ char OEMName[FAT_BR_OEMNAME_SIZE+1];
+ char VolLabel[FAT_BR_VOLLAB_SIZE+1];
+ bool VolLabel_present;
+ uint32_t vol_id;
+} msdos_format_param_t;
+
+/*
+ * Formatted output.
+ */
+static void
+msdos_format_printf (const msdos_format_request_param_t *rqdata,
+ int info_level,
+ const char *format, ...)
+{
+ va_list args;
+ va_start (args, format);
+ if (rqdata != NULL && rqdata->info_level >= info_level)
+ {
+ vfprintf (stdout, format, args);
+ fflush (stdout);
+ }
+ va_end (args);
+}
+
+/*=========================================================================*\
+| Function: |
+\*-------------------------------------------------------------------------*/
+static int msdos_format_read_sec
+(
+/*-------------------------------------------------------------------------*\
+| Purpose: |
+| function to read a sector |
++---------------------------------------------------------------------------+
+| Input Parameters: |
+\*-------------------------------------------------------------------------*/
+ int fd, /* file descriptor index */
+ uint32_t start_sector, /* sector number to write to */
+ uint32_t sector_size, /* size of sector */
+ char *buffer /* buffer with read data into */
+ )
+/*-------------------------------------------------------------------------*\
+| Return Value: |
+| 0, if success, -1 and errno if failed |
+\*=========================================================================*/
+{
+ int ret_val = 0;
+
+ if (0 > lseek(fd,((off_t)start_sector)*sector_size,SEEK_SET)) {
+ ret_val = -1;
+ }
+ if (ret_val == 0) {
+ if (0 > read(fd,buffer,sector_size)) {
+ ret_val = -1;
+ }
+ }
+
+ return ret_val;
+}
+
+/*=========================================================================*\
+| Function: |
+\*-------------------------------------------------------------------------*/
+static int msdos_format_write_sec
+(
+/*-------------------------------------------------------------------------*\
+| Purpose: |
+| function to write to a sector |
++---------------------------------------------------------------------------+
+| Input Parameters: |
+\*-------------------------------------------------------------------------*/
+ int fd, /* file descriptor index */
+ uint32_t start_sector, /* sector number to write to */
+ uint32_t sector_size, /* size of sector */
+ const char *buffer /* buffer with write data */
+ )
+/*-------------------------------------------------------------------------*\
+| Return Value: |
+| 0, if success, -1 and errno if failed |
+\*=========================================================================*/
+{
+ int ret_val = 0;
+
+ if (0 > lseek(fd,((off_t)start_sector)*sector_size,SEEK_SET)) {
+ ret_val = -1;
+ }
+ if (ret_val == 0) {
+ if (0 > write(fd,buffer,sector_size)) {
+ ret_val = -1;
+ }
+ }
+
+ return ret_val;
+}
+
+/*=========================================================================* \
+| Function: |
+\*-------------------------------------------------------------------------*/
+static int msdos_format_fill_sectors
+(
+/*-------------------------------------------------------------------------*\
+| Purpose: |
+| function to fill sectors with byte |
++---------------------------------------------------------------------------+
+| Input Parameters: |
+\*-------------------------------------------------------------------------*/
+ const msdos_format_request_param_t *rqdata,
+ int fd, /* file descriptor index */
+ uint32_t start_sector, /* sector number to fill to */
+ uint32_t sector_cnt, /* number of sectors to fill to */
+ uint32_t sector_size, /* size of sector */
+ const char fill_byte /* byte to fill into sectors */
+ )
+/*-------------------------------------------------------------------------*\
+| Return Value: |
+| 0, if success, -1 and errno if failed |
+\*=========================================================================*/
+{
+ int ret_val = 0;
+ char *fill_buffer = NULL;
+ uint32_t total_sectors = sector_cnt;
+ int last_percent = -1;
+
+ /*
+ * allocate and fill buffer
+ */
+ if (ret_val == 0) {
+ fill_buffer = malloc(sector_size);
+ if (fill_buffer == NULL) {
+ errno = ENOMEM;
+ ret_val = -1;
+ }
+ else {
+ memset(fill_buffer,fill_byte,sector_size);
+ }
+ }
+
+ msdos_format_printf (rqdata, MSDOS_FMT_INFO_LEVEL_DETAIL,
+ "Filling : ");
+ /*
+ * write to consecutive sectors
+ */
+ while ((ret_val == 0) &&
+ (sector_cnt > 0)) {
+ int percent = (sector_cnt * 100) / total_sectors;
+ if (percent != last_percent) {
+ if ((percent & 1) == 0)
+ msdos_format_printf (rqdata, MSDOS_FMT_INFO_LEVEL_DETAIL, ".");
+ last_percent = percent;
+ }
+ ret_val = msdos_format_write_sec(fd,start_sector,sector_size,fill_buffer);
+ start_sector++;
+ sector_cnt--;
+ }
+
+ msdos_format_printf (rqdata, MSDOS_FMT_INFO_LEVEL_DETAIL, "\n");
+
+ if (ret_val)
+ msdos_format_printf (rqdata, MSDOS_FMT_INFO_LEVEL_INFO,
+ "filling error on sector: %d\n", start_sector);
+
+ /*
+ * cleanup
+ */
+ if (fill_buffer != NULL) {
+ free(fill_buffer);
+ fill_buffer = NULL;
+ }
+ return ret_val;
+}
+
+
+/*=========================================================================*\
+| Function: |
+\*-------------------------------------------------------------------------*/
+static int msdos_format_gen_volid
+(
+/*-------------------------------------------------------------------------*\
+| Purpose: |
+| function to generate a pseudo-random volume id |
++---------------------------------------------------------------------------+
+| Input Parameters: |
+\*-------------------------------------------------------------------------*/
+ uint32_t *volid_ptr /* volume ID return pointer */
+ )
+/*-------------------------------------------------------------------------*\
+| Return Value: |
+| 0, if success, -1 and errno if failed |
+\*=========================================================================*/
+{
+ int ret_val = 0;
+ int rc;
+ struct timeval time_value;
+
+ rc = rtems_clock_get_tod_timeval(&time_value);
+ if (rc == RTEMS_SUCCESSFUL) {
+ *volid_ptr = time_value.tv_sec + time_value.tv_sec;
+ }
+ else {
+ *volid_ptr = rand();
+ }
+
+ return ret_val;
+}
+
+
+/*=========================================================================*\
+| Function: |
+\*-------------------------------------------------------------------------*/
+static int msdos_format_eval_sectors_per_cluster
+(
+/*-------------------------------------------------------------------------*\
+| Purpose: |
+| function to check/adjust sectors_per_cluster to legal values |
++---------------------------------------------------------------------------+
+| Input Parameters: |
+\*-------------------------------------------------------------------------*/
+ int fattype, /* type code of FAT (FAT_FAT12 ...) */
+ uint32_t bytes_per_sector, /* byte count per sector (512) */
+ uint32_t fatdata_sec_cnt, /* sectors available for FAT and data */
+ uint8_t fat_num, /* number of fat copies */
+ uint32_t sectors_per_cluster, /* sectors per cluster (requested) */
+ uint32_t *sectors_per_cluster_adj, /* ret: sec per cluster (granted) */
+ uint32_t *sectors_per_fat_ptr /* ret: sectors needed for one FAT */
+ )
+/*-------------------------------------------------------------------------*\
+| Return Value: |
+| 0, if success, -1 and errno if failed |
+\*=========================================================================*/
+{
+
+ bool finished = false;
+ int ret_val = 0;
+ uint32_t fatdata_cluster_cnt;
+ uint32_t fat_capacity;
+ uint32_t sectors_per_fat;
+ uint32_t data_cluster_cnt;
+ /*
+ * ensure, that maximum cluster size (32KByte) is not exceeded
+ */
+ while (MS_BYTES_PER_CLUSTER_LIMIT / bytes_per_sector < sectors_per_cluster) {
+ sectors_per_cluster /= 2;
+ }
+
+ do {
+ /*
+ * compute number of data clusters for current data:
+ * - compute cluster count for data AND fat
+ * - compute storage size for FAT
+ * - subtract from total cluster count
+ */
+ fatdata_cluster_cnt = fatdata_sec_cnt/sectors_per_cluster;
+ if (fattype == FAT_FAT12) {
+ fat_capacity = fatdata_cluster_cnt * 3 / 2;
+ }
+ else if (fattype == FAT_FAT16) {
+ fat_capacity = fatdata_cluster_cnt * 2;
+ }
+ else { /* FAT32 */
+ fat_capacity = fatdata_cluster_cnt * 4;
+ }
+
+ sectors_per_fat = ((fat_capacity
+ + (bytes_per_sector - 1))
+ / bytes_per_sector);
+
+ data_cluster_cnt = (fatdata_cluster_cnt -
+ (((sectors_per_fat * fat_num)
+ + (sectors_per_cluster - 1))
+ / sectors_per_cluster));
+ /*
+ * data cluster count too big? then make sectors bigger
+ */
+ if (((fattype == FAT_FAT12) && (data_cluster_cnt > FAT_FAT12_MAX_CLN)) ||
+ ((fattype == FAT_FAT16) && (data_cluster_cnt > FAT_FAT16_MAX_CLN))) {
+ sectors_per_cluster *= 2;
+ }
+ else {
+ finished = true;
+ }
+ /*
+ * when maximum cluster size is exceeded, we have invalid data, abort...
+ */
+ if ((sectors_per_cluster * bytes_per_sector)
+ > MS_BYTES_PER_CLUSTER_LIMIT) {
+ ret_val = EINVAL;
+ finished = true;
+ }
+ } while (!finished);
+
+ if (ret_val != 0) {
+ rtems_set_errno_and_return_minus_one(ret_val);
+ }
+ else {
+ *sectors_per_cluster_adj = sectors_per_cluster;
+ *sectors_per_fat_ptr = sectors_per_fat;
+ return 0;
+ }
+}
+
+
+/*=========================================================================*\
+| Function: |
+\*-------------------------------------------------------------------------*/
+static int msdos_format_determine_fmt_params
+(
+/*-------------------------------------------------------------------------*\
+| Purpose: |
+| determine parameters for formatting |
++---------------------------------------------------------------------------+
+| Input Parameters: |
+\*-------------------------------------------------------------------------*/
+ const rtems_disk_device *dd, /* disk device structure */
+ const msdos_format_request_param_t *rqdata, /* requested fmt parameters */
+ msdos_format_param_t *fmt_params/* computed fmt parameters */
+ )
+/*-------------------------------------------------------------------------*\
+| Return Value: |
+| 0, if success, -1 and errno if failed |
+\*=========================================================================*/
+{
+ int ret_val = 0;
+ uint32_t fatdata_sect_cnt;
+ uint32_t onebit;
+ uint32_t sectors_per_cluster_adj = 0;
+ uint64_t total_size = 0;
+
+ memset(fmt_params,0,sizeof(*fmt_params));
+ /*
+ * this one is fixed in this implementation.
+ * At least one thing we don't have to magically guess...
+ */
+ if (ret_val == 0) {
+ fmt_params->bytes_per_sector = dd->block_size;
+ fmt_params->totl_sector_cnt = dd->size;
+ total_size = dd->block_size * dd->size;
+ msdos_format_printf (rqdata, MSDOS_FMT_INFO_LEVEL_DETAIL,
+ "bytes per sector: %d\ntotal sectors: %d\ntotal size: %lu\n",
+ dd->block_size, dd->size, total_size);
+ }
+ /*
+ * determine number of FATs
+ */
+ if (ret_val == 0) {
+ if ((rqdata == NULL) ||
+ (rqdata->fat_num == 0)) {
+ fmt_params->fat_num = 2;
+ }
+ else if (rqdata->fat_num <= 6) {
+ fmt_params->fat_num = rqdata->fat_num;
+ }
+ else {
+ ret_val = EINVAL;
+ }
+ }
+
+ if (ret_val == 0)
+ msdos_format_printf (rqdata, MSDOS_FMT_INFO_LEVEL_DETAIL,
+ "number of fats: %d\n", fmt_params->fat_num);
+
+ /*
+ * Now we get sort of a loop when determining things:
+ * The FAT type (FAT12/16/32) is determined ONLY from the
+ * data cluster count:
+ * Disks with data cluster count < 4085 are FAT12.
+ * Disks with data cluster count < 65525 are FAT16.
+ * The rest is FAT32 (no FAT128 available yet :-)
+ *
+ * The number of data clusters is the
+ * total capacity
+ * minus reserved sectors
+ * minus root directory ares
+ * minus storage needed for the FAT (and its copy/copies).
+ *
+ * The last item once again depends on the FAT type and the cluster count.
+ *
+ * So here is what we do in this formatter:
+ * - If a FAT type is requested from the caller, we try to modify
+ * the cluster size, until the data cluster count is in range
+ * - If no FAT type is given, we estimate a useful FAT type from
+ * the disk capacity and then adapt the cluster size
+ */
+
+ /*
+ * determine characteristic values:
+ * - number of sectors
+ * - number of reserved sectors
+ * - number of used sectors
+ * - sectors per cluster
+ */
+ /*
+ * determine FAT type and sectors per cluster
+ * depends on
+ */
+ if (ret_val == 0) {
+ fmt_params->sectors_per_cluster = 1;
+ if ((rqdata != NULL) &&
+ (rqdata->fattype == MSDOS_FMT_FAT12)) {
+ fmt_params->fattype = FAT_FAT12;
+ }
+ else if ((rqdata != NULL) &&
+ (rqdata->fattype == MSDOS_FMT_FAT16)) {
+ fmt_params->fattype = FAT_FAT16;
+ }
+ else if ((rqdata != NULL) &&
+ (rqdata->fattype == MSDOS_FMT_FAT32)) {
+ fmt_params->fattype = FAT_FAT32;
+ }
+ else if ((rqdata != NULL) &&
+ (rqdata->fattype != MSDOS_FMT_FATANY)) {
+ ret_val = -1;
+ errno = EINVAL;
+ }
+ else {
+ /*
+ * limiting values for disk size, fat type, sectors per cluster
+ * NOTE: maximum sect_per_clust is arbitrarily choosen with values that
+ * are a compromise concerning capacity and efficency
+ */
+ if (fmt_params->totl_sector_cnt
+ < ((uint32_t)FAT_FAT12_MAX_CLN)*8) {
+ fmt_params->fattype = FAT_FAT12;
+ /* start trying with small clusters */
+ fmt_params->sectors_per_cluster = 2;
+ }
+ else if (fmt_params->totl_sector_cnt
+ < ((uint32_t)FAT_FAT16_MAX_CLN)*32) {
+ fmt_params->fattype = FAT_FAT16;
+ /* start trying with small clusters */
+ fmt_params->sectors_per_cluster = 2;
+ }
+ else {
+ #define ONE_GB (1024L * 1024L * 1024L)
+ uint32_t gigs = (total_size + ONE_GB) / ONE_GB;
+ int b;
+ fmt_params->fattype = FAT_FAT32;
+ /* scale with the size of disk... */
+ for (b = 31; b > 0; b--)
+ if ((gigs & (1 << b)) != 0)
+ break;
+ fmt_params->sectors_per_cluster = 1 << b;
+ }
+ }
+ /*
+ * try to use user requested cluster size
+ */
+ if ((rqdata != NULL) &&
+ (rqdata->sectors_per_cluster > 0)) {
+ fmt_params->sectors_per_cluster =
+ rqdata->sectors_per_cluster;
+ }
+ /*
+ * check sectors per cluster.
+ * must be power of 2
+ * must be smaller than or equal to 128
+ * sectors_per_cluster*bytes_per_sector must not be bigger than 32K
+ */
+ for (onebit = 128;onebit >= 1;onebit = onebit>>1) {
+ if (fmt_params->sectors_per_cluster >= onebit) {
+ fmt_params->sectors_per_cluster = onebit;
+ if (fmt_params->sectors_per_cluster
+ <= 32768L/fmt_params->bytes_per_sector) {
+ /* value is small enough so this value is ok */
+ onebit = 1;
+ }
+ }
+ }
+ }
+
+ if (ret_val == 0) {
+ msdos_format_printf (rqdata, MSDOS_FMT_INFO_LEVEL_DETAIL,
+ "sectors per cluster: %d\n", fmt_params->sectors_per_cluster);
+
+ if (fmt_params->fattype == FAT_FAT32) {
+ /* recommended: for FAT32, always set reserved sector count to 32 */
+ fmt_params->rsvd_sector_cnt = 32;
+ /* for FAT32, always set files per root directory 0 */
+ fmt_params->files_per_root_dir = 0;
+ /* location of copy of MBR */
+ fmt_params->mbr_copy_sec = 6;
+ /* location of fsinfo sector */
+ fmt_params->fsinfo_sec = 1;
+
+ }
+ else {
+ /* recommended: for FAT12/FAT16, always set reserved sector count to 1 */
+ fmt_params->rsvd_sector_cnt = 1;
+ /* recommended: for FAT16, set files per root directory to 512 */
+ /* for FAT12/FAT16, set files per root directory */
+ /* must fill up an even count of sectors */
+ if ((rqdata != NULL) &&
+ (rqdata->files_per_root_dir > 0)) {
+ fmt_params->files_per_root_dir = rqdata->files_per_root_dir;
+ }
+ else {
+ if (fmt_params->fattype == FAT_FAT16) {
+ fmt_params->files_per_root_dir = 512;
+ }
+ else {
+ fmt_params->files_per_root_dir = 64;
+ }
+ }
+ fmt_params->files_per_root_dir = (fmt_params->files_per_root_dir +
+ (2*fmt_params->bytes_per_sector/
+ FAT_DIRENTRY_SIZE-1));
+ fmt_params->files_per_root_dir -= (fmt_params->files_per_root_dir %
+ (2*fmt_params->bytes_per_sector
+ /FAT_DIRENTRY_SIZE));
+ }
+ fmt_params->root_dir_sectors =
+ (((fmt_params->files_per_root_dir * FAT_DIRENTRY_SIZE)
+ + fmt_params->bytes_per_sector - 1)
+ / fmt_params->bytes_per_sector);
+ }
+ if (ret_val == 0) {
+ fatdata_sect_cnt = (fmt_params->totl_sector_cnt -
+ fmt_params->rsvd_sector_cnt -
+ fmt_params->root_dir_sectors);
+
+ /*
+ * check values to get legal arrangement of FAT type and cluster count
+ */
+
+ ret_val = msdos_format_eval_sectors_per_cluster
+ (fmt_params->fattype,
+ fmt_params->bytes_per_sector,
+ fatdata_sect_cnt,
+ fmt_params->fat_num,
+ fmt_params->sectors_per_cluster,
+ &sectors_per_cluster_adj,
+ &(fmt_params->sectors_per_fat));
+ fmt_params->sectors_per_cluster = sectors_per_cluster_adj;
+ }
+
+ /*
+ * determine media code
+ */
+ if (ret_val == 0) {
+ if ((rqdata != NULL) &&
+ (rqdata->media != 0)) {
+ const char valid_media_codes[] =
+ {0xF0,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF};
+ if (NULL==memchr(valid_media_codes,
+ rqdata->media,
+ sizeof(valid_media_codes))) {
+ ret_val = -1;
+ errno = EINVAL;
+ }
+ else {
+ fmt_params->media_code = rqdata->media;
+ }
+ }
+ else {
+ fmt_params->media_code = FAT_BR_MEDIA_FIXED;
+ }
+ }
+ /*
+ * determine location and size of root directory
+ * for formatting
+ */
+ if (fmt_params->root_dir_sectors > 0) {
+ fmt_params->root_dir_start_sec =
+ fmt_params->rsvd_sector_cnt
+ + (fmt_params-> fat_num*fmt_params->sectors_per_fat);
+ fmt_params->root_dir_fmt_sec_cnt = fmt_params->root_dir_sectors;
+ }
+ else {
+ /*
+ * for FAT32: root directory is in cluster 2
+ */
+ fmt_params->root_dir_start_sec =
+ fmt_params->rsvd_sector_cnt
+ + (fmt_params-> fat_num*fmt_params->sectors_per_fat);
+ fmt_params->root_dir_fmt_sec_cnt = fmt_params->sectors_per_cluster;
+ }
+ /*
+ * determine usable OEMName
+ */
+ if (ret_val == 0) {
+ const char *from;
+ char *to = fmt_params->OEMName;
+ int cnt;
+ from = "RTEMS"; /* default: make "from" point to OS Name */
+ if ((rqdata != NULL) &&
+ (rqdata->OEMName != NULL)) {
+ from = rqdata->OEMName;
+ }
+ for (cnt = 0;
+ cnt < (sizeof(fmt_params->OEMName)-1);
+ cnt++) {
+ if (isprint((unsigned char)*from)) {
+ *to++ = *from++;
+ }
+ else {
+ /*
+ * non-printable character in given name, so keep stuck
+ * at that character and replace all following characters
+ * with a ' '
+ */
+ *to++=' ';
+ }
+ *to = '\0';
+ }
+ }
+
+ /*
+ * determine usable Volume Label
+ */
+ if (ret_val == 0) {
+ const char *from;
+ char *to = fmt_params->VolLabel;
+ int cnt;
+ from = ""; /* default: make "from" point to empty string */
+ if ((rqdata != NULL) &&
+ (rqdata->VolLabel != NULL)) {
+ from = rqdata->VolLabel;
+ fmt_params->VolLabel_present = true;
+ }
+ for (cnt = 0;
+ cnt < (sizeof(fmt_params->VolLabel)-1);
+ cnt++) {
+ if (isprint((unsigned char)*from)) {
+ *to++ = *from++;
+ }
+ else {
+ /*
+ * non-printable character in given name, so keep stuck
+ * at that character and replace all following characters
+ * with a ' '
+ */
+ *to++=' ';
+ }
+ *to = '\0';
+ }
+ }
+
+ /*
+ * determine usable Volume ID
+ */
+ if (ret_val == 0) {
+ msdos_format_gen_volid(&(fmt_params->vol_id));
+ }
+ /*
+ * Phuuu.... That's it.
+ */
+ if (ret_val != 0) {
+ rtems_set_errno_and_return_minus_one(ret_val);
+ }
+ else {
+ return 0;
+ }
+}
+/*=========================================================================*\
+| Function: |
+\*-------------------------------------------------------------------------*/
+static int msdos_format_gen_mbr
+(
+/*-------------------------------------------------------------------------*\
+| Purpose: |
+| create master boot record content from parameter set |
++---------------------------------------------------------------------------+
+| Input Parameters: |
+\*-------------------------------------------------------------------------*/
+ char mbr[], /* sector buffer */
+ const msdos_format_param_t *fmt_params/* computed fmt parameters */
+ )
+/*-------------------------------------------------------------------------*\
+| Return Value: |
+| 0, if success, -1 and errno if failed |
+\*=========================================================================*/
+{
+ uint32_t total_sectors_num16 = 0;
+ uint32_t total_sectors_num32 = 0;
+
+ /* store total sector count in either 16 or 32 bit field in mbr */
+ if (fmt_params->totl_sector_cnt < 0x10000) {
+ total_sectors_num16 = fmt_params->totl_sector_cnt;
+ }
+ else {
+ total_sectors_num32 = fmt_params->totl_sector_cnt;
+ }
+ /*
+ * finally we are there: let's fill in the values into the MBR
+ * but first clear the MRB leaving the partition table.
+ */
+#define RTEMS_IDE_PARTITION_TABLE_OFFSET 0x1be
+#define RTEMS_IDE_PARTITION_TABLE_SIZE (4 * 16)
+ memset(mbr,0,RTEMS_IDE_PARTITION_TABLE_OFFSET);
+ memset(mbr + RTEMS_IDE_PARTITION_TABLE_OFFSET + RTEMS_IDE_PARTITION_TABLE_SIZE,
+ 0,
+ FAT_TOTAL_MBR_SIZE - (RTEMS_IDE_PARTITION_TABLE_OFFSET + RTEMS_IDE_PARTITION_TABLE_SIZE));
+ /*
+ * FIXME: fill jmpBoot and Boot code...
+ * with 0xEB,....
+ */
+ /*
+ * fill OEMName
+ */
+ memcpy(FAT_GET_ADDR_BR_OEMNAME(mbr),
+ fmt_params->OEMName,
+ FAT_BR_OEMNAME_SIZE);
+ FAT_SET_BR_BYTES_PER_SECTOR(mbr , fmt_params->bytes_per_sector);
+ FAT_SET_BR_SECTORS_PER_CLUSTER(mbr , fmt_params->sectors_per_cluster);
+ FAT_SET_BR_RESERVED_SECTORS_NUM(mbr, fmt_params->rsvd_sector_cnt);
+
+ /* number of FATs on medium */
+ FAT_SET_BR_FAT_NUM(mbr , 2); /* standard/recommended value */
+ FAT_SET_BR_FILES_PER_ROOT_DIR(mbr , fmt_params->files_per_root_dir);
+ FAT_SET_BR_TOTAL_SECTORS_NUM16(mbr , total_sectors_num16);
+ FAT_SET_BR_MEDIA(mbr , fmt_params->media_code);
+
+ FAT_SET_BR_SECTORS_PER_TRACK(mbr , 255); /* only needed for INT13... */
+ FAT_SET_BR_NUMBER_OF_HEADS(mbr , 6); /* only needed for INT13... */
+ FAT_SET_BR_HIDDEN_SECTORS(mbr , 1); /* only needed for INT13... */
+
+ FAT_SET_BR_TOTAL_SECTORS_NUM32(mbr , total_sectors_num32);
+ if (fmt_params->fattype != FAT_FAT32) {
+ FAT_SET_BR_SECTORS_PER_FAT(mbr ,fmt_params->sectors_per_fat);
+ FAT_SET_BR_DRVNUM(mbr , 0); /* only needed for INT13... */
+ FAT_SET_BR_RSVD1(mbr , 0); /* fill with zero */
+ FAT_SET_BR_BOOTSIG(mbr , FAT_BR_BOOTSIG_VAL);
+ FAT_SET_BR_VOLID(mbr , fmt_params->vol_id); /* volume id */
+ memcpy(FAT_GET_ADDR_BR_VOLLAB(mbr),
+ fmt_params->VolLabel,
+ FAT_BR_VOLLAB_SIZE);
+ memcpy(FAT_GET_ADDR_BR_FILSYSTYPE(mbr),
+ (fmt_params->fattype == FAT_FAT12)
+ ? "FAT12 "
+ : "FAT16 ",
+ FAT_BR_FILSYSTYPE_SIZE);
+ }
+ else {
+ FAT_SET_BR_SECTORS_PER_FAT32(mbr ,fmt_params->sectors_per_fat);
+ FAT_SET_BR_EXT_FLAGS(mbr , 0);
+ FAT_SET_BR_FSVER(mbr , 0); /* FAT32 Version:0.0 */
+ FAT_SET_BR_FAT32_ROOT_CLUSTER(mbr , 2); /* put root dir to cluster 2 */
+ FAT_SET_BR_FAT32_FS_INFO_SECTOR(mbr, 1); /* Put fsinfo to rsrvd sec 1*/
+ FAT_SET_BR_FAT32_BK_BOOT_SECTOR(mbr, fmt_params->mbr_copy_sec ); /* Put MBR copy to rsrvd sec */
+ memset(FAT_GET_ADDR_BR_FAT32_RESERVED(mbr),0,FAT_BR_FAT32_RESERVED_SIZE);
+
+ FAT_SET_BR_FAT32_DRVNUM(mbr , 0); /* only needed for INT13... */
+ FAT_SET_BR_FAT32_RSVD1(mbr , 0); /* fill with zero */
+ FAT_SET_BR_FAT32_BOOTSIG(mbr ,FAT_BR_FAT32_BOOTSIG_VAL);
+ FAT_SET_BR_FAT32_VOLID(mbr , 0); /* not set */
+ memset(FAT_GET_ADDR_BR_FAT32_VOLLAB(mbr) ,0,FAT_BR_VOLLAB_SIZE);
+ memcpy(FAT_GET_ADDR_BR_FAT32_FILSYSTYPE(mbr),
+ "FAT32 ",
+ FAT_BR_FILSYSTYPE_SIZE);
+ }
+ /*
+ * add boot record signature
+ */
+ FAT_SET_BR_SIGNATURE(mbr, FAT_BR_SIGNATURE_VAL);
+
+ /*
+ * add jump to boot loader at start of sector
+ */
+ FAT_SET_VAL8(mbr,0,0xeb);
+ FAT_SET_VAL8(mbr,1,0x3c);
+ FAT_SET_VAL8(mbr,2,0x90);
+ /*
+ * FIXME: a nice little PC boot loader would be nice here.
+ * but where can I get one for free?
+ */
+ /*
+ * Phuuu.... That's it.
+ */
+ return 0;
+}
+
+/*=========================================================================*\
+| Function: |
+\*-------------------------------------------------------------------------*/
+static int msdos_format_gen_fsinfo
+(
+/*-------------------------------------------------------------------------*\
+| Purpose: |
+| create FAT32 fsinfo sector |
++---------------------------------------------------------------------------+
+| Input Parameters: |
+\*-------------------------------------------------------------------------*/
+ char fsinfo[] /* sector buffer */
+ )
+/*-------------------------------------------------------------------------*\
+| Return Value: |
+| 0, if success, -1 and errno if failed |
+\*=========================================================================*/
+{
+ /*
+ * clear fsinfo sector data
+ */
+ memset(fsinfo,0,FAT_TOTAL_FSINFO_SIZE);
+ /*
+ * write LEADSIG, STRUCTSIG, TRAILSIG
+ */
+ FAT_SET_FSINFO_LEAD_SIGNATURE (fsinfo,FAT_FSINFO_LEAD_SIGNATURE_VALUE );
+ FAT_SET_FSINFO_STRUC_SIGNATURE(fsinfo,FAT_FSINFO_STRUC_SIGNATURE_VALUE);
+ FAT_SET_FSINFO_TRAIL_SIGNATURE(fsinfo,FAT_FSINFO_TRAIL_SIGNATURE_VALUE);
+ /*
+ * write "empty" values for free cluster count and next cluster number
+ */
+ FAT_SET_FSINFO_FREE_CLUSTER_COUNT(fsinfo+FAT_FSI_INFO,
+ 0xffffffff);
+ FAT_SET_FSINFO_NEXT_FREE_CLUSTER (fsinfo+FAT_FSI_INFO,
+ 0xffffffff);
+ return 0;
+}
+
+/*=========================================================================*\
+| Function: |
+\*-------------------------------------------------------------------------*/
+int msdos_format
+(
+/*-------------------------------------------------------------------------*\
+| Purpose: |
+| format device with msdos filesystem |
++---------------------------------------------------------------------------+
+| Input Parameters: |
+\*-------------------------------------------------------------------------*/
+ const char *devname, /* device name */
+ const msdos_format_request_param_t *rqdata /* requested fmt parameters */
+ /* set to NULL for automatic */
+ /* determination */
+ )
+/*-------------------------------------------------------------------------*\
+| Return Value: |
+| 0, if success, -1 and errno if failed |
+\*=========================================================================*/
+{
+ char tmp_sec[FAT_TOTAL_MBR_SIZE];
+ int rc;
+ rtems_disk_device *dd = NULL;
+ struct stat stat_buf;
+ int ret_val = 0;
+ int fd = -1;
+ int i;
+ msdos_format_param_t fmt_params;
+
+ /*
+ * open device for writing
+ */
+ msdos_format_printf (rqdata, MSDOS_FMT_INFO_LEVEL_DETAIL, "open device\n");
+ fd = open(devname, O_RDWR);
+ if (fd == -1) {
+ ret_val= -1;
+ }
+
+ /*
+ * sanity check on device
+ */
+ msdos_format_printf (rqdata, MSDOS_FMT_INFO_LEVEL_DETAIL,
+ "stat check: %s\n", devname);
+ if (ret_val == 0) {
+ rc = fstat(fd, &stat_buf);
+ ret_val = rc;
+ }
+
+ msdos_format_printf (rqdata, MSDOS_FMT_INFO_LEVEL_INFO,
+ "formating: %s\n", devname);
+ /* rtems feature: no block devices, all are character devices */
+ if ((ret_val == 0) && (!S_ISBLK(stat_buf.st_mode))) {
+ errno = ENOTTY;
+ ret_val = -1;
+ }
+
+ /* check that device is registered as block device and lock it */
+ if (ret_val == 0) {
+ dd = rtems_disk_obtain(stat_buf.st_rdev);
+ if (dd == NULL) {
+ errno = ENOTTY;
+ ret_val = -1;
+ }
+ }
+
+ /*
+ * compute formatting parameters
+ */
+ if (ret_val == 0) {
+ ret_val = msdos_format_determine_fmt_params(dd,rqdata,&fmt_params);
+ }
+ /*
+ * if requested, write whole disk/partition with 0xe5
+ */
+ if ((ret_val == 0) &&
+ (rqdata != NULL) &&
+ !(rqdata->quick_format)) {
+ ret_val = msdos_format_fill_sectors
+ (rqdata,
+ fd,
+ 0, /* start sector */
+ fmt_params.totl_sector_cnt, /* sector count */
+ fmt_params.bytes_per_sector,
+ 0xe5);
+ }
+
+ /*
+ * create master boot record
+ */
+ if (ret_val == 0) {
+ /*
+ * Read the current MBR to obtain the partition table.
+ */
+ msdos_format_printf (rqdata, MSDOS_FMT_INFO_LEVEL_DETAIL,
+ "read MRB sector\n");
+ ret_val = msdos_format_read_sec(fd,
+ 0,
+ fmt_params.bytes_per_sector,
+ tmp_sec);
+ if (ret_val == 0) {
+ msdos_format_printf (rqdata, MSDOS_FMT_INFO_LEVEL_DETAIL,
+ "generate MRB sector\n");
+ ret_val = msdos_format_gen_mbr(tmp_sec,&fmt_params);
+ }
+
+ /*
+ * write master boot record to disk
+ * also write copy of MBR to disk
+ */
+ if (ret_val == 0) {
+ msdos_format_printf (rqdata, MSDOS_FMT_INFO_LEVEL_DETAIL,
+ "write MRB sector\n");
+ ret_val = msdos_format_write_sec(fd,
+ 0,
+ fmt_params.bytes_per_sector,
+ tmp_sec);
+ }
+ if ((ret_val == 0) &&
+ (fmt_params.mbr_copy_sec != 0)) {
+ /*
+ * write copy of MBR
+ */
+ msdos_format_printf (rqdata, MSDOS_FMT_INFO_LEVEL_DETAIL,
+ "write back up MRB sector\n");
+ ret_val = msdos_format_write_sec(fd,
+ fmt_params.mbr_copy_sec ,
+ fmt_params.bytes_per_sector,
+ tmp_sec);
+ }
+ }
+ /*
+ * for FAT32: initialize info sector on disk
+ */
+ if ((ret_val == 0) &&
+ (fmt_params.fsinfo_sec != 0)) {
+ ret_val = msdos_format_gen_fsinfo(tmp_sec);
+ }
+ /*
+ * write fsinfo sector
+ */
+ if ((ret_val == 0) &&
+ (fmt_params.fsinfo_sec != 0)) {
+ ret_val = msdos_format_write_sec(fd,
+ fmt_params.fsinfo_sec,
+ fmt_params.bytes_per_sector,
+ tmp_sec);
+ }
+ /*
+ * write FAT as all empty
+ * -> write all FAT sectors as zero
+ */
+ if (ret_val == 0) {
+ ret_val = msdos_format_fill_sectors
+ (rqdata,
+ fd,
+ fmt_params.rsvd_sector_cnt, /* start sector */
+ fmt_params.fat_num*fmt_params.sectors_per_fat,/* sector count */
+ fmt_params.bytes_per_sector,
+ 0x00);
+ }
+ /*
+ * clear/init root directory
+ * -> write all directory sectors as 0x00
+ */
+ if (ret_val == 0) {
+ ret_val = msdos_format_fill_sectors
+ (rqdata,
+ fd,
+ fmt_params.root_dir_start_sec, /* start sector */
+ fmt_params.root_dir_fmt_sec_cnt, /* sector count */
+ fmt_params.bytes_per_sector,
+ 0x00);
+ }
+ /*
+ * write volume label to first entry of directory
+ */
+ if ((ret_val == 0) && fmt_params.VolLabel_present) {
+ memset(tmp_sec,0,sizeof(tmp_sec));
+ memcpy(MSDOS_DIR_NAME(tmp_sec),fmt_params.VolLabel,MSDOS_SHORT_NAME_LEN);
+ *MSDOS_DIR_ATTR(tmp_sec) = MSDOS_ATTR_VOLUME_ID;
+ ret_val = msdos_format_write_sec
+ (fd,
+ fmt_params.root_dir_start_sec,
+ fmt_params.bytes_per_sector,
+ tmp_sec);
+ }
+ /*
+ * write FAT entry 0 as (0xffffff00|Media_type)EOC,
+ * write FAT entry 1 as EOC
+ * allocate directory in a FAT32 FS
+ */
+ if ((ret_val == 0) && fmt_params.VolLabel_present){
+ /*
+ * empty sector: all clusters are free/do not link further on
+ */
+ memset(tmp_sec,0,sizeof(tmp_sec));
+
+ switch(fmt_params.fattype) {
+ case FAT_FAT12:
+ /* LSBits of FAT entry 0: media_type */
+ FAT_SET_VAL8(tmp_sec,0,(fmt_params.media_code));
+ /* MSBits of FAT entry 0:0xf, LSBits of FAT entry 1: LSB of EOC */
+ FAT_SET_VAL8(tmp_sec,1,(0x0f | (FAT_FAT12_EOC << 4)));
+ /* MSBits of FAT entry 1: MSBits of EOC */
+ FAT_SET_VAL8(tmp_sec,2,(FAT_FAT12_EOC >> 4));
+ break;
+
+ case FAT_FAT16:
+ /* FAT entry 0: 0xff00|media_type */
+ FAT_SET_VAL8(tmp_sec,0,fmt_params.media_code);
+ FAT_SET_VAL8(tmp_sec,1,0xff);
+ /* FAT entry 1: EOC */
+ FAT_SET_VAL16(tmp_sec,2,FAT_FAT16_EOC);
+ break;
+
+ case FAT_FAT32:
+ /* FAT entry 0: 0xffffff00|media_type */
+ FAT_SET_VAL32(tmp_sec,0,0xffffff00|fmt_params.media_code);
+ /* FAT entry 1: EOC */
+ FAT_SET_VAL32(tmp_sec,4,FAT_FAT32_EOC);
+ break;
+
+ default:
+ ret_val = -1;
+ errno = EINVAL;
+ }
+ if (fmt_params.fattype == FAT_FAT32) {
+ /*
+ * only first valid cluster (cluster number 2) belongs
+ * to root directory, and is end of chain
+ * mark this in every copy of the FAT
+ */
+ FAT_SET_VAL32(tmp_sec,8,FAT_FAT32_EOC);
+ }
+ for (i = 0;
+ (i < fmt_params.fat_num) && (ret_val == 0);
+ i++) {
+ ret_val = msdos_format_write_sec
+ (fd,
+ fmt_params.rsvd_sector_cnt
+ + (i * fmt_params.sectors_per_fat),
+ fmt_params.bytes_per_sector,
+ tmp_sec);
+ }
+ }
+ /*
+ * cleanup:
+ * sync and unlock disk
+ * free any data structures (not needed now)
+ */
+ if (fd != -1) {
+ close(fd);
+ }
+ if (dd != NULL) {
+ rtems_disk_release(dd);
+ }
+ return ret_val;
+}
diff --git a/cpukit/libfs/src/dosfs/msdos_free.c b/cpukit/libfs/src/dosfs/msdos_free.c
new file mode 100644
index 0000000000..90fc586a10
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/msdos_free.c
@@ -0,0 +1,56 @@
+/*
+ * Free node handler implementation for the filesystem
+ * operations table.
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <rtems.h>
+#include <rtems/libio_.h>
+
+#include <errno.h>
+
+#include "fat.h"
+#include "fat_fat_operations.h"
+#include "fat_file.h"
+
+#include "msdos.h"
+
+/* msdos_free_node_info --
+ * Call fat-file close routine.
+ *
+ * PARAMETERS:
+ * pathloc - node description
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 code if error occured
+ *
+ */
+int
+msdos_free_node_info(rtems_filesystem_location_info_t *pathloc)
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = pathloc->mt_entry->fs_info;
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ rc = fat_file_close(pathloc->mt_entry, pathloc->node_access);
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+}
diff --git a/cpukit/libfs/src/dosfs/msdos_fsunmount.c b/cpukit/libfs/src/dosfs/msdos_fsunmount.c
new file mode 100644
index 0000000000..310da9c8ff
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/msdos_fsunmount.c
@@ -0,0 +1,70 @@
+/*
+ * MSDOS shut down handler implementation
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <rtems.h>
+#include <rtems/libio_.h>
+
+#include "fat.h"
+#include "fat_fat_operations.h"
+#include "fat_file.h"
+
+#include "msdos.h"
+
+/* msdos_shut_down --
+ * Shut down MSDOS filesystem - free all allocated resources (don't
+ * return if deallocation of some resource failed - free as much as
+ * possible).
+ *
+ * PARAMETERS:
+ * temp_mt_entry - mount table entry
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set apropriately).
+ *
+ */
+int
+msdos_shut_down(rtems_filesystem_mount_table_entry_t *temp_mt_entry)
+{
+ int rc = RC_OK;
+ msdos_fs_info_t *fs_info = temp_mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = temp_mt_entry->mt_fs_root.node_access;
+
+ /* close fat-file which correspondes to root directory */
+ if (fat_file_close(temp_mt_entry, fat_fd) != RC_OK)
+ {
+ /* no return - try to free as much as possible */
+ rc = -1;
+ }
+
+ if (fat_shutdown_drive(temp_mt_entry) != RC_OK)
+ {
+ /* no return - try to free as much as possible */
+ rc = -1;
+ }
+
+ rtems_semaphore_delete(fs_info->vol_sema);
+ free(fs_info->cl_buf);
+ free(temp_mt_entry->fs_info);
+
+ return rc;
+}
diff --git a/cpukit/libfs/src/dosfs/msdos_handlers_dir.c b/cpukit/libfs/src/dosfs/msdos_handlers_dir.c
new file mode 100644
index 0000000000..cc32af86d4
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/msdos_handlers_dir.c
@@ -0,0 +1,36 @@
+/*
+ * Directory Handlers Table for MSDOS filesystem
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <rtems/libio.h>
+#include "msdos.h"
+
+const rtems_filesystem_file_handlers_r msdos_dir_handlers = {
+ msdos_dir_open,
+ msdos_dir_close,
+ msdos_dir_read,
+ rtems_filesystem_default_write,
+ rtems_filesystem_default_ioctl,
+ msdos_dir_lseek,
+ msdos_dir_stat,
+ msdos_dir_chmod,
+ rtems_filesystem_default_ftruncate,
+ rtems_filesystem_default_fpathconf,
+ msdos_dir_sync,
+ msdos_dir_sync,
+ rtems_filesystem_default_fcntl,
+ msdos_dir_rmnod
+};
diff --git a/cpukit/libfs/src/dosfs/msdos_handlers_file.c b/cpukit/libfs/src/dosfs/msdos_handlers_file.c
new file mode 100644
index 0000000000..18d4fdd018
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/msdos_handlers_file.c
@@ -0,0 +1,36 @@
+/*
+ * File Operations Table for MSDOS filesystem
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <rtems/libio.h>
+#include "msdos.h"
+
+const rtems_filesystem_file_handlers_r msdos_file_handlers = {
+ msdos_file_open,
+ msdos_file_close,
+ msdos_file_read,
+ msdos_file_write,
+ msdos_file_ioctl,
+ msdos_file_lseek,
+ msdos_file_stat,
+ msdos_file_chmod,
+ msdos_file_ftruncate,
+ rtems_filesystem_default_fpathconf,
+ msdos_file_sync,
+ msdos_file_datasync,
+ rtems_filesystem_default_fcntl,
+ msdos_file_rmnod
+};
diff --git a/cpukit/libfs/src/dosfs/msdos_init.c b/cpukit/libfs/src/dosfs/msdos_init.c
new file mode 100644
index 0000000000..217e0c1f3a
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/msdos_init.c
@@ -0,0 +1,64 @@
+/*
+ * Init routine for MSDOS
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <rtems/libio_.h>
+#include "dosfs.h"
+#include "msdos.h"
+
+const rtems_filesystem_operations_table msdos_ops = {
+ .evalpath_h = msdos_eval_path,
+ .evalformake_h = msdos_eval4make,
+ .link_h = rtems_filesystem_default_link,
+ .unlink_h = msdos_file_rmnod,
+ .node_type_h = msdos_node_type,
+ .mknod_h = msdos_mknod,
+ .chown_h = rtems_filesystem_default_chown,
+ .freenod_h = msdos_free_node_info,
+ .mount_h = rtems_filesystem_default_mount,
+ .fsmount_me_h = rtems_dosfs_initialize,
+ .unmount_h = rtems_filesystem_default_unmount,
+ .fsunmount_me_h = msdos_shut_down,
+ .utime_h = rtems_filesystem_default_utime,
+ .eval_link_h = rtems_filesystem_default_evaluate_link,
+ .symlink_h = rtems_filesystem_default_symlink,
+ .readlink_h = rtems_filesystem_default_readlink,
+ .rename_h = msdos_rename,
+ .statvfs_h = rtems_filesystem_default_statvfs
+};
+
+/* msdos_initialize --
+ * MSDOS filesystem initialization. Called when mounting an
+ * MSDOS filesystem.
+ *
+ * PARAMETERS:
+ * temp_mt_entry - mount table entry
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set apropriately).
+ *
+ */
+int rtems_dosfs_initialize(rtems_filesystem_mount_table_entry_t *mt_entry,
+ const void *data)
+{
+ int rc;
+
+ rc = msdos_initialize_support(mt_entry,
+ &msdos_ops,
+ &msdos_file_handlers,
+ &msdos_dir_handlers);
+ return rc;
+}
diff --git a/cpukit/libfs/src/dosfs/msdos_initsupp.c b/cpukit/libfs/src/dosfs/msdos_initsupp.c
new file mode 100644
index 0000000000..fc10fda71a
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/msdos_initsupp.c
@@ -0,0 +1,149 @@
+/*
+ * MSDOS Initialization support routine implementation
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <rtems.h>
+#include <rtems/libio_.h>
+
+#include "fat.h"
+#include "fat_fat_operations.h"
+#include "fat_file.h"
+
+#include "msdos.h"
+
+/* msdos_initialize_support --
+ * MSDOS filesystem initialization
+ *
+ * PARAMETERS:
+ * temp_mt_entry - mount table entry
+ * op_table - filesystem operations table
+ * file_handlers - file operations table
+ * directory_handlers - directory operations table
+ *
+ * RETURNS:
+ * RC_OK and filled temp_mt_entry on success, or -1 if error occured
+ * (errno set apropriately)
+ *
+ */
+int
+msdos_initialize_support(
+ rtems_filesystem_mount_table_entry_t *temp_mt_entry,
+ const rtems_filesystem_operations_table *op_table,
+ const rtems_filesystem_file_handlers_r *file_handlers,
+ const rtems_filesystem_file_handlers_r *directory_handlers
+ )
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = NULL;
+ fat_file_fd_t *fat_fd = NULL;
+ fat_dir_pos_t root_pos;
+ uint32_t cl_buf_size;
+
+ fs_info = (msdos_fs_info_t *)calloc(1, sizeof(msdos_fs_info_t));
+ if (!fs_info)
+ rtems_set_errno_and_return_minus_one(ENOMEM);
+
+ temp_mt_entry->fs_info = fs_info;
+
+ rc = fat_init_volume_info(temp_mt_entry);
+ if (rc != RC_OK)
+ {
+ free(fs_info);
+ return rc;
+ }
+
+ fs_info->file_handlers = file_handlers;
+ fs_info->directory_handlers = directory_handlers;
+
+ /*
+ * open fat-file which correspondes to root directory
+ * (so inode number 0x00000010 is always used for root directory)
+ */
+ fat_dir_pos_init(&root_pos);
+ root_pos.sname.cln = FAT_ROOTDIR_CLUSTER_NUM;
+ rc = fat_file_open(temp_mt_entry, &root_pos, &fat_fd);
+ if (rc != RC_OK)
+ {
+ fat_shutdown_drive(temp_mt_entry);
+ free(fs_info);
+ return rc;
+ }
+
+ /* again: unfortunately "fat-file" is just almost fat file :( */
+ fat_fd->fat_file_type = FAT_DIRECTORY;
+ fat_fd->size_limit = MSDOS_MAX_DIR_LENGHT;
+ fat_fd->cln = fs_info->fat.vol.rdir_cl;
+
+ fat_fd->map.file_cln = 0;
+ fat_fd->map.disk_cln = fat_fd->cln;
+
+ /* if we have FAT12/16 */
+ if ( fat_fd->cln == 0 )
+ {
+ fat_fd->fat_file_size = fs_info->fat.vol.rdir_size;
+ cl_buf_size = (fs_info->fat.vol.bpc > fs_info->fat.vol.rdir_size) ?
+ fs_info->fat.vol.bpc :
+ fs_info->fat.vol.rdir_size;
+ }
+ else
+ {
+ rc = fat_file_size(temp_mt_entry, fat_fd);
+ if ( rc != RC_OK )
+ {
+ fat_file_close(temp_mt_entry, fat_fd);
+ fat_shutdown_drive(temp_mt_entry);
+ free(fs_info);
+ return rc;
+ }
+ cl_buf_size = fs_info->fat.vol.bpc;
+ }
+
+ fs_info->cl_buf = (uint8_t *)calloc(cl_buf_size, sizeof(char));
+ if (fs_info->cl_buf == NULL)
+ {
+ fat_file_close(temp_mt_entry, fat_fd);
+ fat_shutdown_drive(temp_mt_entry);
+ free(fs_info);
+ rtems_set_errno_and_return_minus_one(ENOMEM);
+ }
+
+ sc = rtems_semaphore_create(3,
+ 1,
+ RTEMS_BINARY_SEMAPHORE | RTEMS_FIFO,
+ 0,
+ &fs_info->vol_sema);
+ if (sc != RTEMS_SUCCESSFUL)
+ {
+ fat_file_close(temp_mt_entry, fat_fd);
+ fat_shutdown_drive(temp_mt_entry);
+ free(fs_info->cl_buf);
+ free(fs_info);
+ rtems_set_errno_and_return_minus_one( EIO );
+ }
+
+ temp_mt_entry->mt_fs_root.node_access = fat_fd;
+ temp_mt_entry->mt_fs_root.handlers = directory_handlers;
+ temp_mt_entry->mt_fs_root.ops = op_table;
+
+ return rc;
+}
diff --git a/cpukit/libfs/src/dosfs/msdos_misc.c b/cpukit/libfs/src/dosfs/msdos_misc.c
new file mode 100644
index 0000000000..d49b89048b
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/msdos_misc.c
@@ -0,0 +1,1731 @@
+/*
+ * Miscellaneous routines implementation for MSDOS filesystem
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ */
+
+#define MSDOS_TRACE 1
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <ctype.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <rtems/libio_.h>
+
+#include "fat.h"
+#include "fat_fat_operations.h"
+#include "fat_file.h"
+
+#include "msdos.h"
+
+
+#include <stdio.h>
+
+/*
+ * External strings. Saves spave this way.
+ */
+const char *const MSDOS_DOT_NAME = ". ";
+const char *const MSDOS_DOTDOT_NAME = ".. ";
+
+/* msdos_is_valid_name_char --
+ * Routine to check the character in a file or directory name.
+ * The characters support in the short file name are letters,
+ * digits, or characters with code points values greater than
+ * 127 (not sure what this last is) plus the following special
+ * characters "$%'-_@~`!(){}^#&". The must be uppercase.
+ *
+ * The following 6 characters are allowed in a long names,
+ * " +,;=[]" including a space and lower case letters.
+ *
+ * PARAMETERS:
+ * ch - character to check.
+ *
+ * RETURNS:
+ * MSDOS_NAME_INVALID - Not valid in a long or short name.
+ * MSDOS_NAME_SHORT - Valid in a short name or long name.
+ * MSDOS_NAME_LONG - Valid in a long name only.
+ *
+ */
+static msdos_name_type_t
+msdos_is_valid_name_char(const char ch)
+{
+ if (strchr(" +,;=[]", ch) != NULL)
+ return MSDOS_NAME_LONG;
+
+ if ((ch == '.') || isalnum((unsigned char)ch) ||
+ (strchr("$%'-_@~`!(){}^#&", ch) != NULL))
+ return MSDOS_NAME_SHORT;
+
+ return MSDOS_NAME_INVALID;
+}
+
+/* msdos_short_hex_number --
+ * Routine to set the hex number in the SFN.
+ *
+ * PARAMETERS:
+ * name - name to change
+ * num - number to set
+ *
+ * RETURNS:
+ * nothing
+ *
+ */
+static void
+msdos_short_name_hex(char* sfn, int num)
+{
+ static const char* hex = "0123456789ABCDEF";
+ char* c = MSDOS_DIR_NAME(sfn);
+ int i;
+ for (i = 0; i < 2; i++, c++)
+ if ((*c == ' ') || (*c == '.'))
+ *c = '_';
+ for (i = 0; i < 4; i++, c++)
+ *c = hex[(num >> ((3 - i) * 4)) & 0xf];
+ *c++ = '~';
+ *c++ = '1';
+}
+
+/* msdos_name_type --
+ * Routine the type of file name.
+ *
+ * PARAMETERS:
+ * name - name to check
+ *
+ * RETURNS:
+ * true the name is long, else the name is short.
+ *
+ */
+#define MSDOS_NAME_TYPE_PRINT 0
+static msdos_name_type_t
+msdos_name_type(const char *name, int name_len)
+{
+ bool lowercase = false;
+ bool uppercase = false;
+ int dot_at = -1;
+ int count = 0;
+
+ while (*name && (count < name_len))
+ {
+ bool is_dot = *name == '.';
+ msdos_name_type_t type = msdos_is_valid_name_char(*name);
+
+#if MSDOS_NAME_TYPE_PRINT
+ printf ("MSDOS_NAME_TYPE: c:%02x type:%d\n", *name, type);
+#endif
+
+ if ((type == MSDOS_NAME_INVALID) || (type == MSDOS_NAME_LONG))
+ return type;
+
+ if (dot_at >= 0)
+ {
+ if (is_dot || ((count - dot_at) > 3))
+ {
+#if MSDOS_NAME_TYPE_PRINT
+ printf ("MSDOS_NAME_TYPE: LONG[1]: is_dot:%d, at:%d cnt\n",
+ is_dot, dot_at, count);
+#endif
+ return MSDOS_NAME_LONG;
+ }
+ }
+ else
+ {
+ if (count == 8 && !is_dot)
+ {
+#if MSDOS_NAME_TYPE_PRINT
+ printf ("MSDOS_NAME_TYPE: LONG[2]: is_dot:%d, at:%d cnt\n",
+ is_dot, dot_at, count);
+#endif
+ return MSDOS_NAME_LONG;
+ }
+ }
+
+ if (is_dot)
+ dot_at = count;
+ else if ((*name >= 'A') && (*name <= 'Z'))
+ uppercase = true;
+ else if ((*name >= 'a') && (*name <= 'z'))
+ lowercase = true;
+
+ count++;
+ name++;
+ }
+
+ if (lowercase && uppercase)
+ {
+#if MSDOS_NAME_TYPE_PRINT
+ printf ("MSDOS_NAME_TYPE: LONG[3]\n");
+#endif
+ return MSDOS_NAME_LONG;
+ }
+
+#if MSDOS_NAME_TYPE_PRINT
+ printf ("MSDOS_NAME_TYPE: SHORT[1]\n");
+#endif
+ return MSDOS_NAME_SHORT;
+}
+
+/* msdos_long_to_short --
+ * Routine to creates a short name from a long. Start the end of the
+ *
+ * PARAMETERS:
+ * name - name to check
+ *
+ * RETURNS:
+ * true the name is long, else the name is short.
+ *
+ */
+#define MSDOS_L2S_PRINT 0
+msdos_name_type_t
+msdos_long_to_short(const char *lfn, int lfn_len, char* sfn, int sfn_len)
+{
+ msdos_name_type_t type;
+ int i;
+
+ /*
+ * Fill with spaces. This is how a short directory entry is padded.
+ */
+ memset (sfn, ' ', sfn_len);
+
+ /*
+ * Handle '.' and '..' specially.
+ */
+ if ((lfn[0] == '.') && (lfn_len == 1))
+ {
+ sfn[0] = '.';
+#if MSDOS_L2S_PRINT
+ printf ("MSDOS_L2S: SHORT[1]: lfn:'%s' SFN:'%s'\n", lfn, sfn);
+#endif
+ return MSDOS_NAME_SHORT;
+ }
+
+ if ((lfn[0] == '.') && (lfn[1] == '.') && (lfn_len == 2))
+ {
+ sfn[0] = sfn[1] = '.';
+#if MSDOS_L2S_PRINT
+ printf ("MSDOS_L2S: SHORT[2]: lfn:'%s' SFN:'%s'\n", lfn, sfn);
+#endif
+ return MSDOS_NAME_SHORT;
+ }
+
+ /*
+ * Filenames with only blanks and dots are not allowed!
+ */
+ for (i = 0; i < lfn_len; i++)
+ if ((lfn[i] != ' ') && (lfn[i] != '.'))
+ break;
+
+ if (i == lfn_len)
+ {
+#if MSDOS_L2S_PRINT
+ printf ("MSDOS_L2S: INVALID[1]: lfn:'%s' SFN:'%s'\n", lfn, sfn);
+#endif
+ return MSDOS_NAME_INVALID;
+ }
+
+ /*
+ * Is this a short name ?
+ */
+
+ type = msdos_name_type (lfn, lfn_len);
+
+ if (type == MSDOS_NAME_INVALID)
+ {
+#if MSDOS_L2S_PRINT
+ printf ("MSDOS_L2S: INVALID[2]: lfn:'%s' SFN:'%s'\n", lfn, sfn);
+#endif
+ return MSDOS_NAME_INVALID;
+ }
+
+ msdos_filename_unix2dos (lfn, lfn_len, sfn);
+
+#if MSDOS_L2S_PRINT
+ printf ("MSDOS_L2S: TYPE:%d lfn:'%s' SFN:'%s'\n", type, lfn, sfn);
+#endif
+ return type;
+}
+
+/* msdos_get_token --
+ * Routine to get a token (name or separator) from the path.
+ *
+ * PARAMETERS:
+ * path - path to get token from
+ * ret_token - returned token
+ * token_len - length of returned token
+ *
+ * RETURNS:
+ * token type, token and token length
+ *
+ */
+msdos_token_types_t
+msdos_get_token(const char *path,
+ int pathlen,
+ const char **ret_token,
+ int *ret_token_len)
+{
+ msdos_token_types_t type = MSDOS_NAME;
+ int i = 0;
+
+ *ret_token = NULL;
+ *ret_token_len = 0;
+
+ if (pathlen == 0)
+ return MSDOS_NO_MORE_PATH;
+
+ /*
+ * Check for a separator.
+ */
+ while (!msdos_is_separator(path[i]) && (i < pathlen))
+ {
+ if ( !msdos_is_valid_name_char(path[i]) )
+ return MSDOS_INVALID_TOKEN;
+ ++i;
+ if ( i == MSDOS_NAME_MAX_LFN_WITH_DOT )
+ return MSDOS_INVALID_TOKEN;
+ }
+
+ *ret_token = path;
+
+ /*
+ * If it is just a separator then it is the current dir.
+ */
+ if ( i == 0 )
+ {
+ if ( (*path != '\0') && pathlen )
+ {
+ i++;
+ type = MSDOS_CURRENT_DIR;
+ }
+ else
+ type = MSDOS_NO_MORE_PATH;
+ }
+
+ /*
+ * Set the token and token_len to the token start and length.
+ */
+ *ret_token_len = i;
+
+ /*
+ * If we copied something that was not a seperator see if
+ * it was a special name.
+ */
+ if ( type == MSDOS_NAME )
+ {
+ if ((i == 2) && ((*ret_token)[0] == '.') && ((*ret_token)[1] == '.'))
+ {
+ type = MSDOS_UP_DIR;
+ return type;
+ }
+
+ if ((i == 1) && ((*ret_token)[0] == '.'))
+ {
+ type = MSDOS_CURRENT_DIR;
+ return type;
+ }
+ }
+
+ return type;
+}
+
+
+/* msdos_find_name --
+ * Find the node which correspondes to the name, open fat-file which
+ * correspondes to the found node and close fat-file which correspondes
+ * to the node we searched in.
+ *
+ * PARAMETERS:
+ * parent_loc - parent node description
+ * name - name to find
+ *
+ * RETURNS:
+ * RC_OK and updated 'parent_loc' on success, or -1 if error
+ * occured (errno set apropriately)
+ *
+ */
+int
+msdos_find_name(
+ rtems_filesystem_location_info_t *parent_loc,
+ const char *name,
+ int name_len
+ )
+{
+ int rc = RC_OK;
+ msdos_fs_info_t *fs_info = parent_loc->mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = NULL;
+ msdos_name_type_t name_type;
+ fat_dir_pos_t dir_pos;
+ unsigned short time_val = 0;
+ unsigned short date = 0;
+ char node_entry[MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE];
+
+ memset(node_entry, 0, MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE);
+
+ name_type = msdos_long_to_short (name,
+ name_len,
+ MSDOS_DIR_NAME(node_entry),
+ MSDOS_NAME_MAX);
+
+ /*
+ * find the node which correspondes to the name in the directory pointed by
+ * 'parent_loc'
+ */
+ rc = msdos_get_name_node(parent_loc, false, name, name_len, name_type,
+ &dir_pos, node_entry);
+ if (rc != RC_OK)
+ return rc;
+
+ if (((*MSDOS_DIR_ATTR(node_entry)) & MSDOS_ATTR_VOLUME_ID) ||
+ ((*MSDOS_DIR_ATTR(node_entry) & MSDOS_ATTR_LFN_MASK) == MSDOS_ATTR_LFN))
+ return MSDOS_NAME_NOT_FOUND_ERR;
+
+ /* open fat-file corresponded to the found node */
+ rc = fat_file_open(parent_loc->mt_entry, &dir_pos, &fat_fd);
+ if (rc != RC_OK)
+ return rc;
+
+ fat_fd->dir_pos = dir_pos;
+
+ /*
+ * I don't like this if, but: we should do it, or should write new file
+ * size and first cluster num to the disk after each write operation
+ * (even if one byte is written - that is TOO slow) because
+ * otherwise real values of these fields stored in fat-file descriptor
+ * may be accidentally rewritten with wrong values stored on the disk
+ */
+ if (fat_fd->links_num == 1)
+ {
+ fat_fd->cln = MSDOS_EXTRACT_CLUSTER_NUM(node_entry);
+
+ time_val = *MSDOS_DIR_WRITE_TIME(node_entry);
+ date = *MSDOS_DIR_WRITE_DATE(node_entry);
+
+ fat_fd->mtime = msdos_date_dos2unix(CF_LE_W(date), CF_LE_W(time_val));
+
+ if ((*MSDOS_DIR_ATTR(node_entry)) & MSDOS_ATTR_DIRECTORY)
+ {
+ fat_fd->fat_file_type = FAT_DIRECTORY;
+ fat_fd->size_limit = MSDOS_MAX_DIR_LENGHT;
+
+ rc = fat_file_size(parent_loc->mt_entry, fat_fd);
+ if (rc != RC_OK)
+ {
+ fat_file_close(parent_loc->mt_entry, fat_fd);
+ return rc;
+ }
+ }
+ else
+ {
+ fat_fd->fat_file_size = CF_LE_L(*MSDOS_DIR_FILE_SIZE(node_entry));
+ fat_fd->fat_file_type = FAT_FILE;
+ fat_fd->size_limit = MSDOS_MAX_FILE_SIZE;
+ }
+
+ /* these data is not actual for zero-length fat-file */
+ fat_fd->map.file_cln = 0;
+ fat_fd->map.disk_cln = fat_fd->cln;
+
+ if ((fat_fd->fat_file_size != 0) &&
+ (fat_fd->fat_file_size <= fs_info->fat.vol.bpc))
+ {
+ fat_fd->map.last_cln = fat_fd->cln;
+ }
+ else
+ {
+ fat_fd->map.last_cln = FAT_UNDEFINED_VALUE;
+ }
+ }
+
+ /* close fat-file corresponded to the node we searched in */
+ rc = fat_file_close(parent_loc->mt_entry, parent_loc->node_access);
+ if (rc != RC_OK)
+ {
+ fat_file_close(parent_loc->mt_entry, fat_fd);
+ return rc;
+ }
+
+ /* update node_info_ptr field */
+ parent_loc->node_access = fat_fd;
+
+ return rc;
+}
+
+/* msdos_get_name_node --
+ * This routine is used in two ways: for a new node creation (a) or for
+ * search the node which correspondes to the name parameter (b).
+ * In case (a) 'name' should be set up to NULL and 'name_dir_entry' should
+ * point to initialized 32 bytes structure described a new node.
+ * In case (b) 'name' should contain a valid string.
+ *
+ * (a): reading fat-file which correspondes to directory we are going to
+ * create node in. If free slot is found write contents of
+ * 'name_dir_entry' into it. If reach end of fat-file and no free
+ * slot found, write 32 bytes to the end of fat-file.
+ *
+ * (b): reading fat-file which correspondes to directory and trying to
+ * find slot with the name field == 'name' parameter
+ *
+ *
+ * PARAMETERS:
+ * parent_loc - node description to create node in or to find name in
+ * name - NULL or name to find
+ * paux - identify a node location on the disk -
+ * cluster num and offset inside the cluster
+ * short_dir_entry - node to create/placeholder for found node (IN/OUT)
+ *
+ * RETURNS:
+ * RC_OK, filled aux_struct_ptr and name_dir_entry on success, or -1 if
+ * error occured (errno set apropriately)
+ *
+ */
+int
+msdos_get_name_node(
+ rtems_filesystem_location_info_t *parent_loc,
+ bool create_node,
+ const char *name,
+ int name_len,
+ msdos_name_type_t name_type,
+ fat_dir_pos_t *dir_pos,
+ char *name_dir_entry
+ )
+{
+ int rc = RC_OK;
+ fat_file_fd_t *fat_fd = parent_loc->node_access;
+ uint32_t dotdot_cln = 0;
+
+ /* find name in fat-file which corresponds to the directory */
+ rc = msdos_find_name_in_fat_file(parent_loc->mt_entry, fat_fd,
+ create_node, name, name_len, name_type,
+ dir_pos, name_dir_entry);
+ if ((rc != RC_OK) && (rc != MSDOS_NAME_NOT_FOUND_ERR))
+ return rc;
+
+ if (!create_node)
+ {
+ /* if we search for valid name and name not found -> return */
+ if (rc == MSDOS_NAME_NOT_FOUND_ERR)
+ return rc;
+
+ /*
+ * if we have deal with ".." - it is a special case :(((
+ *
+ * Really, we should return cluster num and offset not of ".." slot, but
+ * slot which correspondes to real directory name.
+ */
+ if (rc == RC_OK)
+ {
+ if (strncmp(name, "..", 2) == 0)
+ {
+ dotdot_cln = MSDOS_EXTRACT_CLUSTER_NUM((name_dir_entry));
+
+ /* are we right under root dir ? */
+ if (dotdot_cln == 0)
+ {
+ /*
+ * we can relax about first_char field - it never should be
+ * used for root dir
+ */
+ fat_dir_pos_init(dir_pos);
+ dir_pos->sname.cln = FAT_ROOTDIR_CLUSTER_NUM;
+ }
+ else
+ {
+ rc =
+ msdos_get_dotdot_dir_info_cluster_num_and_offset(parent_loc->mt_entry,
+ dotdot_cln,
+ dir_pos,
+ name_dir_entry);
+ if (rc != RC_OK)
+ return rc;
+ }
+ }
+ }
+ }
+ return rc;
+}
+
+/*
+ * msdos_get_dotdot_dir_info_cluster_num_and_offset
+ *
+ * Unfortunately, in general, we cann't work here in fat-file ideologic
+ * (open fat_file "..", get ".." and ".", open "..", find an entry ...)
+ * because if we open
+ * fat-file ".." it may happend that we have two different fat-file
+ * descriptors ( for real name of directory and ".." name ) for a single
+ * file ( cluster num of both pointers to the same cluster )
+ * But...we do it because we protected by semaphore
+ *
+ */
+
+/* msdos_get_dotdot_dir_info_cluster_num_and_offset --
+ * Get cluster num and offset not of ".." slot, but slot which correspondes
+ * to real directory name.
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * cln - data cluster num extracted drom ".." slot
+ * paux - identify a node location on the disk -
+ * number of cluster and offset inside the cluster
+ * dir_entry - placeholder for found node
+ *
+ * RETURNS:
+ * RC_OK, filled 'paux' and 'dir_entry' on success, or -1 if error occured
+ * (errno set apropriately)
+ *
+ */
+int
+msdos_get_dotdot_dir_info_cluster_num_and_offset(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ uint32_t cln,
+ fat_dir_pos_t *dir_pos,
+ char *dir_entry
+ )
+{
+ int rc = RC_OK;
+ msdos_fs_info_t *fs_info = mt_entry->fs_info;
+ fat_file_fd_t *fat_fd = NULL;
+ char dot_node[MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE];
+ char dotdot_node[MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE];
+ uint32_t cl4find = 0;
+
+ /*
+ * open fat-file corresponded to ".."
+ */
+ rc = fat_file_open(mt_entry, dir_pos, &fat_fd);
+ if (rc != RC_OK)
+ return rc;
+
+ fat_fd->cln = cln;
+ fat_fd->fat_file_type = FAT_DIRECTORY;
+ fat_fd->size_limit = MSDOS_MAX_DIR_LENGHT;
+
+ fat_fd->map.file_cln = 0;
+ fat_fd->map.disk_cln = fat_fd->cln;
+
+ rc = fat_file_size(mt_entry, fat_fd);
+ if (rc != RC_OK)
+ {
+ fat_file_close(mt_entry, fat_fd);
+ return rc;
+ }
+
+ /* find "." node in opened directory */
+ memset(dot_node, 0, MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE);
+ msdos_long_to_short(".", 1, dot_node, MSDOS_SHORT_NAME_LEN);
+ rc = msdos_find_name_in_fat_file(mt_entry, fat_fd, false, ".", 1,
+ MSDOS_NAME_SHORT, dir_pos, dot_node);
+
+ if (rc != RC_OK)
+ {
+ fat_file_close(mt_entry, fat_fd);
+ return rc;
+ }
+
+ /* find ".." node in opened directory */
+ memset(dotdot_node, 0, MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE);
+ msdos_long_to_short("..", 2, dotdot_node, MSDOS_SHORT_NAME_LEN);
+ rc = msdos_find_name_in_fat_file(mt_entry, fat_fd, false, "..", 2,
+ MSDOS_NAME_SHORT, dir_pos,
+ dotdot_node);
+
+ if (rc != RC_OK)
+ {
+ fat_file_close(mt_entry, fat_fd);
+ return rc;
+ }
+
+ cl4find = MSDOS_EXTRACT_CLUSTER_NUM(dot_node);
+
+ /* close fat-file corresponded to ".." directory */
+ rc = fat_file_close(mt_entry, fat_fd);
+ if ( rc != RC_OK )
+ return rc;
+
+ if ( (MSDOS_EXTRACT_CLUSTER_NUM(dotdot_node)) == 0)
+ {
+ /*
+ * we handle root dir for all FAT types in the same way with the
+ * ordinary directories ( through fat_file_* calls )
+ */
+ fat_dir_pos_init(dir_pos);
+ dir_pos->sname.cln = FAT_ROOTDIR_CLUSTER_NUM;
+ }
+
+ /* open fat-file corresponded to second ".." */
+ rc = fat_file_open(mt_entry, dir_pos, &fat_fd);
+ if (rc != RC_OK)
+ return rc;
+
+ if ((MSDOS_EXTRACT_CLUSTER_NUM(dotdot_node)) == 0)
+ fat_fd->cln = fs_info->fat.vol.rdir_cl;
+ else
+ fat_fd->cln = MSDOS_EXTRACT_CLUSTER_NUM(dotdot_node);
+
+ fat_fd->fat_file_type = FAT_DIRECTORY;
+ fat_fd->size_limit = MSDOS_MAX_DIR_LENGHT;
+
+ fat_fd->map.file_cln = 0;
+ fat_fd->map.disk_cln = fat_fd->cln;
+
+ rc = fat_file_size(mt_entry, fat_fd);
+ if (rc != RC_OK)
+ {
+ fat_file_close(mt_entry, fat_fd);
+ return rc;
+ }
+
+ /* in this directory find slot with specified cluster num */
+ rc = msdos_find_node_by_cluster_num_in_fat_file(mt_entry, fat_fd, cl4find,
+ dir_pos, dir_entry);
+ if (rc != RC_OK)
+ {
+ fat_file_close(mt_entry, fat_fd);
+ return rc;
+ }
+ rc = fat_file_close(mt_entry, fat_fd);
+ return rc;
+}
+
+
+/* msdos_set_dir_wrt_time_and_date --
+ * Write last write date and time for a file to the disk (to corresponded
+ * 32bytes node)
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * fat_fd - fat-file descriptor
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set apropriately).
+ *
+ */
+int
+msdos_set_dir_wrt_time_and_date(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd
+ )
+{
+ ssize_t ret1 = 0, ret2 = 0, ret3 = 0;
+ msdos_fs_info_t *fs_info = mt_entry->fs_info;
+ uint16_t time_val;
+ uint16_t date;
+ uint32_t sec = 0;
+ uint32_t byte = 0;
+
+ msdos_date_unix2dos(fat_fd->mtime, &date, &time_val);
+
+ /*
+ * calculate input for _fat_block_write: convert (cluster num, offset) to
+ * (sector num, new offset)
+ */
+ sec = fat_cluster_num_to_sector_num(mt_entry, fat_fd->dir_pos.sname.cln);
+ sec += (fat_fd->dir_pos.sname.ofs >> fs_info->fat.vol.sec_log2);
+ /* byte points to start of 32bytes structure */
+ byte = fat_fd->dir_pos.sname.ofs & (fs_info->fat.vol.bps - 1);
+
+ time_val = CT_LE_W(time_val);
+ ret1 = _fat_block_write(mt_entry, sec, byte + MSDOS_FILE_WTIME_OFFSET,
+ 2, (char *)(&time_val));
+ date = CT_LE_W(date);
+ ret2 = _fat_block_write(mt_entry, sec, byte + MSDOS_FILE_WDATE_OFFSET,
+ 2, (char *)(&date));
+ ret3 = _fat_block_write(mt_entry, sec, byte + MSDOS_FILE_ADATE_OFFSET,
+ 2, (char *)(&date));
+
+ if ( (ret1 < 0) || (ret2 < 0) || (ret3 < 0) )
+ return -1;
+
+ return RC_OK;
+}
+
+/* msdos_set_first_cluster_num --
+ * Write number of first cluster of the file to the disk (to corresponded
+ * 32bytes slot)
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * fat_fd - fat-file descriptor
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured
+ *
+ */
+int
+msdos_set_first_cluster_num(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd
+ )
+{
+ ssize_t ret1 = 0, ret2 = 0;
+ msdos_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t new_cln = fat_fd->cln;
+ uint16_t le_cl_low = 0;
+ uint16_t le_cl_hi = 0;
+ uint32_t sec = 0;
+ uint32_t byte = 0;
+
+ /*
+ * calculate input for _fat_block_write: convert (cluster num, offset) to
+ * (sector num, new offset)
+ */
+ sec = fat_cluster_num_to_sector_num(mt_entry, fat_fd->dir_pos.sname.cln);
+ sec += (fat_fd->dir_pos.sname.ofs >> fs_info->fat.vol.sec_log2);
+ /* byte from points to start of 32bytes structure */
+ byte = fat_fd->dir_pos.sname.ofs & (fs_info->fat.vol.bps - 1);
+
+ le_cl_low = CT_LE_W((uint16_t )(new_cln & 0x0000FFFF));
+ ret1 = _fat_block_write(mt_entry, sec,
+ byte + MSDOS_FIRST_CLUSTER_LOW_OFFSET, 2,
+ (char *)(&le_cl_low));
+ le_cl_hi = CT_LE_W((uint16_t )((new_cln & 0xFFFF0000) >> 16));
+ ret2 = _fat_block_write(mt_entry, sec,
+ byte + MSDOS_FIRST_CLUSTER_HI_OFFSET, 2,
+ (char *)(&le_cl_hi));
+ if ( (ret1 < 0) || (ret2 < 0) )
+ return -1;
+
+ return RC_OK;
+}
+
+
+/* msdos_set_file size --
+ * Write file size of the file to the disk (to corresponded 32bytes slot)
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * fat_fd - fat-file descriptor
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set apropriately).
+ *
+ */
+int
+msdos_set_file_size(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd
+ )
+{
+ ssize_t ret = 0;
+ msdos_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t le_new_length = 0;
+ uint32_t sec = 0;
+ uint32_t byte = 0;
+
+ sec = fat_cluster_num_to_sector_num(mt_entry, fat_fd->dir_pos.sname.cln);
+ sec += (fat_fd->dir_pos.sname.ofs >> fs_info->fat.vol.sec_log2);
+ byte = (fat_fd->dir_pos.sname.ofs & (fs_info->fat.vol.bps - 1));
+
+ le_new_length = CT_LE_L((fat_fd->fat_file_size));
+ ret = _fat_block_write(mt_entry, sec, byte + MSDOS_FILE_SIZE_OFFSET, 4,
+ (char *)(&le_new_length));
+ if ( ret < 0 )
+ return -1;
+
+ return RC_OK;
+}
+
+/*
+ * We should not check whether this routine is called for root dir - it
+ * never can happend
+ */
+
+/* msdos_set_first_char4file_name --
+ * Write first character of the name of the file to the disk (to
+ * corresponded 32bytes slot)
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * cl - number of cluster
+ * ofs - offset inside cluster
+ * fchar - character to set up
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set apropriately)
+ *
+ */
+int
+msdos_set_first_char4file_name(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_dir_pos_t *dir_pos,
+ unsigned char fchar
+ )
+{
+ ssize_t ret;
+ msdos_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t dir_block_size;
+ fat_pos_t start = dir_pos->lname;
+ fat_pos_t end = dir_pos->sname;
+
+ if ((end.cln == fs_info->fat.vol.rdir_cl) &&
+ (fs_info->fat.vol.type & (FAT_FAT12 | FAT_FAT16)))
+ dir_block_size = fs_info->fat.vol.rdir_size;
+ else
+ dir_block_size = fs_info->fat.vol.bpc;
+
+ if (dir_pos->lname.cln == FAT_FILE_SHORT_NAME)
+ start = dir_pos->sname;
+
+ /*
+ * We handle the changes directly due the way the short file
+ * name code was written rather than use the fat_file_write
+ * interface.
+ */
+ while (true)
+ {
+ uint32_t sec = (fat_cluster_num_to_sector_num(mt_entry, start.cln) +
+ (start.ofs >> fs_info->fat.vol.sec_log2));
+ uint32_t byte = (start.ofs & (fs_info->fat.vol.bps - 1));;
+
+ ret = _fat_block_write(mt_entry, sec, byte + MSDOS_FILE_NAME_OFFSET, 1,
+ &fchar);
+ if (ret < 0)
+ return -1;
+
+ if ((start.cln == end.cln) && (start.ofs == end.ofs))
+ break;
+
+ start.ofs += MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE;
+ if (start.ofs >= dir_block_size)
+ {
+ int rc;
+ if ((end.cln == fs_info->fat.vol.rdir_cl) &&
+ (fs_info->fat.vol.type & (FAT_FAT12 | FAT_FAT16)))
+ break;
+ rc = fat_get_fat_cluster(mt_entry, start.cln, &start.cln);
+ if ( rc != RC_OK )
+ return rc;
+ start.ofs = 0;
+ }
+ }
+
+ return RC_OK;
+}
+
+/* msdos_dir_is_empty --
+ * Check whether directory which correspondes to the fat-file descriptor is
+ * empty.
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * fat_fd - fat-file descriptor
+ * ret_val - placeholder for result
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured
+ *
+ */
+int
+msdos_dir_is_empty(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ bool *ret_val
+ )
+{
+ ssize_t ret = 0;
+ msdos_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t j = 0, i = 0;
+
+ /* dir is not empty */
+ *ret_val = false;
+
+ while ((ret = fat_file_read(mt_entry, fat_fd, j * fs_info->fat.vol.bps,
+ fs_info->fat.vol.bps,
+ fs_info->cl_buf)) != FAT_EOF)
+ {
+ if (ret < MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE)
+ return -1;
+
+ assert(ret == fs_info->fat.vol.bps);
+
+ /* have to look at the DIR_NAME as "raw" 8-bit data */
+ for (i = 0;
+ i < fs_info->fat.vol.bps;
+ i += MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE)
+ {
+ char* entry = (char*) fs_info->cl_buf + i;
+
+ /*
+ * If the entry is empty, a long file name entry, or '.' or '..'
+ * then consider it as empty.
+ *
+ * Just ignore long file name entries. They must have a short entry to
+ * be valid.
+ */
+ if (((*MSDOS_DIR_ENTRY_TYPE(entry)) ==
+ MSDOS_THIS_DIR_ENTRY_EMPTY) ||
+ ((*MSDOS_DIR_ATTR(entry) & MSDOS_ATTR_LFN_MASK) ==
+ MSDOS_ATTR_LFN) ||
+ (strncmp(MSDOS_DIR_NAME((entry)), MSDOS_DOT_NAME,
+ MSDOS_SHORT_NAME_LEN) == 0) ||
+ (strncmp(MSDOS_DIR_NAME((entry)),
+ MSDOS_DOTDOT_NAME,
+ MSDOS_SHORT_NAME_LEN) == 0))
+ continue;
+
+ /*
+ * Nothing more to look at.
+ */
+ if ((*MSDOS_DIR_NAME(entry)) ==
+ MSDOS_THIS_DIR_ENTRY_AND_REST_EMPTY)
+ {
+ *ret_val = true;
+ return RC_OK;
+ }
+
+ /*
+ * Short file name entries mean not empty.
+ */
+ return RC_OK;
+ }
+ j++;
+ }
+ *ret_val = true;
+ return RC_OK;
+}
+
+/* msdos_create_name_in_fat_file --
+ * This routine creates an entry in the fat file for the file name
+ * provided by the user. The directory entry passed is the short
+ * file name and is added as it. If the file name is long a long
+ * file name set of entries is added.
+ *
+ * Scan the directory for the file and if not found add the new entry.
+ * When scanning remember the offset in the file where the directory
+ * entry can be added.
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * fat_fd - fat-file descriptor
+ * name - NULL or name to find
+ * paux - identify a node location on the disk -
+ * number of cluster and offset inside the cluster
+ * name_dir_entry - node to create/placeholder for found node
+ *
+ * RETURNS:
+ * RC_OK on success, or error code if error occured (errno set
+ * appropriately)
+ *
+ */
+#define MSDOS_FIND_PRINT 0
+int msdos_find_name_in_fat_file(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ bool create_node,
+ const char *name,
+ int name_len,
+ msdos_name_type_t name_type,
+ fat_dir_pos_t *dir_pos,
+ char *name_dir_entry
+ )
+{
+ ssize_t ret = 0;
+ msdos_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t dir_offset = 0;
+ uint32_t dir_entry = 0;
+ uint32_t bts2rd = 0;
+ fat_pos_t lfn_start;
+ bool lfn_matched = false;
+ uint8_t lfn_checksum = 0;
+ int lfn_entries;
+ int lfn_entry = 0;
+ uint32_t empty_space_offset = 0;
+ uint32_t empty_space_entry = 0;
+ uint32_t empty_space_count = 0;
+ bool empty_space_found = false;
+ uint32_t entries_per_block;
+ bool read_cluster = false;
+
+ assert(name_len > 0);
+
+ fat_dir_pos_init(dir_pos);
+
+ lfn_start.cln = lfn_start.ofs = FAT_FILE_SHORT_NAME;
+
+ /*
+ * Set the number of short entries needed to store the LFN. If the name
+ * is short still check for possible long entries with the short name.
+ *
+ * In PR1491 we need to have a LFN for a short file name entry. To
+ * test this make this test always fail, ie add "0 &&".
+ */
+ if (create_node && (name_type == MSDOS_NAME_SHORT))
+ lfn_entries = 0;
+ else
+ lfn_entries =
+ ((name_len - 1) + MSDOS_LFN_LEN_PER_ENTRY) / MSDOS_LFN_LEN_PER_ENTRY;
+
+ if (FAT_FD_OF_ROOT_DIR(fat_fd) &&
+ (fs_info->fat.vol.type & (FAT_FAT12 | FAT_FAT16)))
+ bts2rd = fat_fd->fat_file_size;
+ else
+ bts2rd = fs_info->fat.vol.bpc;
+
+ entries_per_block = bts2rd / MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE;
+
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[1] nt:%d, cn:%i ebp:%li bts2rd:%li lfne:%d nl:%i n:%s\n",
+ name_type, create_node, entries_per_block, bts2rd,
+ lfn_entries, name_len, name);
+#endif
+ /*
+ * Scan the directory seeing if the file is present. While
+ * doing this see if a suitable location can be found to
+ * create the entry if the name is not found.
+ */
+ while ((ret = fat_file_read(mt_entry, fat_fd, (dir_offset * bts2rd),
+ bts2rd, fs_info->cl_buf)) != FAT_EOF)
+ {
+ bool remainder_empty = false;
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[2] dir_offset:%li\n", dir_offset);
+#endif
+
+ if (ret < MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ assert(ret == bts2rd);
+
+ /* have to look at the DIR_NAME as "raw" 8-bit data */
+ for (dir_entry = 0;
+ dir_entry < bts2rd;
+ dir_entry += MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE)
+ {
+ char* entry = (char*) fs_info->cl_buf + dir_entry;
+
+ /*
+ * See if the entry is empty or the remainder of the directory is
+ * empty ? Localise to make the code read better.
+ */
+ bool entry_empty = (*MSDOS_DIR_ENTRY_TYPE(entry) ==
+ MSDOS_THIS_DIR_ENTRY_EMPTY);
+ remainder_empty = (*MSDOS_DIR_ENTRY_TYPE(entry) ==
+ MSDOS_THIS_DIR_ENTRY_AND_REST_EMPTY);
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[3] re:%i ee:%i do:%li de:%li(%ld)\n",
+ remainder_empty, entry_empty, dir_offset,
+ dir_entry, (dir_entry / MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE));
+#endif
+ /*
+ * Remember where the we are, ie the start, so we can come back
+ * to here and write the long file name if this is the start of
+ * a series of empty entries. If empty_space_count is 0 then
+ * we are currently not inside an empty series of entries. It
+ * is a count of empty entries.
+ */
+ if (empty_space_count == 0)
+ {
+ empty_space_entry = dir_entry;
+ empty_space_offset = dir_offset;
+ }
+
+ if (remainder_empty)
+ {
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[3.1] cn:%i esf:%i\n", create_node, empty_space_found);
+#endif
+ /*
+ * If just looking and there is no more entries in the
+ * directory - return name-not-found
+ */
+ if (!create_node)
+ return MSDOS_NAME_NOT_FOUND_ERR;
+
+ /*
+ * Lets go and write the directory entries. If we have not found
+ * any available space add the remaining number of entries to any that
+ * we may have already found that are just before this entry. If more
+ * are needed FAT_EOF is returned by the read and we extend the file.
+ */
+ if (!empty_space_found)
+ {
+ empty_space_count +=
+ entries_per_block - (dir_entry / MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE);
+ empty_space_found = true;
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[3.2] esf:%i esc%i\n", empty_space_found, empty_space_count);
+#endif
+ }
+ break;
+ }
+ else if (entry_empty)
+ {
+ if (create_node)
+ {
+ /*
+ * Remainder is not empty so is this entry empty ?
+ */
+ empty_space_count++;
+
+ if (empty_space_count == (lfn_entries + 1))
+ empty_space_found = true;
+ }
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[4.1] esc:%li esf:%i\n",
+ empty_space_count, empty_space_found);
+#endif
+ }
+ else
+ {
+ /*
+ * A valid entry so handle it.
+ *
+ * If empty space has not been found we need to start the
+ * count again.
+ */
+ if (create_node && !empty_space_found)
+ {
+ empty_space_entry = 0;
+ empty_space_count = 0;
+ }
+
+ /*
+ * Check the attribute to see if the entry is for a long
+ * file name.
+ */
+ if ((*MSDOS_DIR_ATTR(entry) & MSDOS_ATTR_LFN_MASK) ==
+ MSDOS_ATTR_LFN)
+ {
+ char* p;
+ int o;
+ int i;
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[4.2] lfn:%c entry:%i checksum:%i\n",
+ lfn_start.cln == FAT_FILE_SHORT_NAME ? 'f' : 't',
+ *MSDOS_DIR_ENTRY_TYPE(entry) & MSDOS_LAST_LONG_ENTRY_MASK,
+ *MSDOS_DIR_LFN_CHECKSUM(entry));
+#endif
+ /*
+ * If we are not already processing a LFN see if this is
+ * the first entry of a LFN ?
+ */
+ if (lfn_start.cln == FAT_FILE_SHORT_NAME)
+ {
+ lfn_matched = false;
+
+ /*
+ * The first entry must have the last long entry
+ * flag set.
+ */
+ if ((*MSDOS_DIR_ENTRY_TYPE(entry) &
+ MSDOS_LAST_LONG_ENTRY) == 0)
+ continue;
+
+ /*
+ * Does the number of entries in the LFN directory
+ * entry match the number we expect for this
+ * file name. Note we do not know the number of
+ * characters in the entry so this is check further
+ * on when the characters are checked.
+ */
+ if (lfn_entries != (*MSDOS_DIR_ENTRY_TYPE(entry) &
+ MSDOS_LAST_LONG_ENTRY_MASK))
+ continue;
+
+ /*
+ * Get the checksum of the short entry.
+ */
+ lfn_start.cln = dir_offset;
+ lfn_start.ofs = dir_entry;
+ lfn_entry = lfn_entries;
+ lfn_checksum = *MSDOS_DIR_LFN_CHECKSUM(entry);
+
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[4.3] lfn_checksum:%i\n",
+ *MSDOS_DIR_LFN_CHECKSUM(entry));
+#endif
+ }
+
+ /*
+ * If the entry number or the check sum do not match
+ * forget this series of long directory entries. These
+ * could be orphaned entries depending on the history
+ * of the disk.
+ */
+ if ((lfn_entry != (*MSDOS_DIR_ENTRY_TYPE(entry) &
+ MSDOS_LAST_LONG_ENTRY_MASK)) ||
+ (lfn_checksum != *MSDOS_DIR_LFN_CHECKSUM(entry)))
+ {
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[4.4] no match\n");
+#endif
+ lfn_start.cln = FAT_FILE_SHORT_NAME;
+ continue;
+ }
+
+ lfn_entry--;
+ o = lfn_entry * MSDOS_LFN_LEN_PER_ENTRY;
+ p = entry + 1;
+
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[5] lfne:%i\n", lfn_entry);
+#endif
+ for (i = 0; i < MSDOS_LFN_LEN_PER_ENTRY; i++)
+ {
+#if MSDOS_FIND_PRINT > 1
+ printf ("MSFS:[6] o:%i i:%i *p:%c(%02x) name[o + i]:%c(%02x)\n",
+ o, i, *p, *p, name[o + i], name[o + i]);
+#endif
+ if (*p == '\0')
+ {
+ /*
+ * If this is the first entry, ie the last part of the
+ * long file name and the length does not match then
+ * the file names do not match.
+ */
+ if (((lfn_entry + 1) == lfn_entries) &&
+ ((o + i) != name_len))
+ lfn_start.cln = FAT_FILE_SHORT_NAME;
+ break;
+ }
+
+ if (((o + i) >= name_len) || (*p != name[o + i]))
+ {
+ lfn_start.cln = FAT_FILE_SHORT_NAME;
+ break;
+ }
+
+ switch (i)
+ {
+ case 4:
+ p += 5;
+ break;
+ case 10:
+ p += 4;
+ break;
+ default:
+ p += 2;
+ break;
+ }
+ }
+
+ lfn_matched = ((lfn_entry == 0) &&
+ (lfn_start.cln != FAT_FILE_SHORT_NAME));
+
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[8.1] lfn_matched:%i\n", lfn_matched);
+#endif
+ }
+ else
+ {
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[9.1] SFN entry, lfn_matched:%i\n", lfn_matched);
+#endif
+ /*
+ * SFN entry found.
+ *
+ * If a LFN has been found and it matched check the
+ * entries have all been found and the checksum is
+ * correct. If this is the case return the short file
+ * name entry.
+ */
+ if (lfn_matched)
+ {
+ uint8_t cs = 0;
+ uint8_t* p = (uint8_t*) MSDOS_DIR_NAME(entry);
+ int i;
+
+ for (i = 0; i < MSDOS_SHORT_NAME_LEN; i++, p++)
+ cs = ((cs & 1) ? 0x80 : 0) + (cs >> 1) + *p;
+
+ if (lfn_entry || (lfn_checksum != cs))
+ lfn_matched = false;
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[9.2] checksum, lfn_matched:%i, lfn_entry:%i, lfn_checksum:%02x/%02x\n",
+ lfn_matched, lfn_entry, lfn_checksum, cs);
+#endif
+ }
+
+ /*
+ * If the long file names matched or the file name is
+ * short and they match then we have the entry. We will not
+ * match a long file name against a short file name because
+ * a long file name that generates a matching short file
+ * name is not a long file name.
+ */
+ if (lfn_matched ||
+ ((name_type == MSDOS_NAME_SHORT) &&
+ (lfn_start.cln == FAT_FILE_SHORT_NAME) &&
+ (memcmp(MSDOS_DIR_NAME(entry),
+ MSDOS_DIR_NAME(name_dir_entry),
+ MSDOS_SHORT_NAME_LEN) == 0)))
+ {
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[9.3] SNF found\n");
+#endif
+ /*
+ * We get the entry we looked for - fill the position
+ * structure and the 32 bytes of the short entry
+ */
+ int rc = fat_file_ioctl(mt_entry, fat_fd, F_CLU_NUM,
+ dir_offset * bts2rd,
+ &dir_pos->sname.cln);
+ if (rc != RC_OK)
+ return rc;
+
+ dir_pos->sname.ofs = dir_entry;
+
+ if (lfn_start.cln != FAT_FILE_SHORT_NAME)
+ {
+ rc = fat_file_ioctl(mt_entry, fat_fd, F_CLU_NUM,
+ lfn_start.cln * bts2rd,
+ &lfn_start.cln);
+ if (rc != RC_OK)
+ return rc;
+ }
+
+ dir_pos->lname.cln = lfn_start.cln;
+ dir_pos->lname.ofs = lfn_start.ofs;
+
+ memcpy(name_dir_entry, entry,
+ MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE);
+ return RC_OK;
+ }
+
+ lfn_start.cln = FAT_FILE_SHORT_NAME;
+ lfn_matched = false;
+ }
+ }
+ }
+
+ if (remainder_empty)
+ break;
+
+ dir_offset++;
+ }
+
+ /*
+ * If we are not to create the entry return a not found error.
+ */
+ if (!create_node)
+ return MSDOS_NAME_NOT_FOUND_ERR;
+
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[8.1] WRITE do:%ld esc:%ld eso:%ld ese:%ld\n",
+ dir_offset, empty_space_count, empty_space_offset, empty_space_entry);
+#endif
+
+ /*
+ * If a long file name calculate the checksum of the short file name
+ * data to place in each long file name entry. First set the short
+ * file name to the slot of the SFN entry. This will mean no clashes
+ * in this directory.
+ */
+ lfn_checksum = 0;
+ if (name_type == MSDOS_NAME_LONG)
+ {
+ int slot = (((empty_space_offset * bts2rd) + empty_space_entry) /
+ MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE) + lfn_entries + 1;
+ msdos_short_name_hex(MSDOS_DIR_NAME(name_dir_entry), slot);
+ }
+
+ if (lfn_entries)
+ {
+ uint8_t* p = (uint8_t*) MSDOS_DIR_NAME(name_dir_entry);
+ int i;
+ for (i = 0; i < 11; i++, p++)
+ lfn_checksum =
+ ((lfn_checksum & 1) ? 0x80 : 0) + (lfn_checksum >> 1) + *p;
+ }
+
+ /*
+ * If there is no space available then extend the file. The
+ * empty_space_count is a count of empty entries in the currently
+ * read cluster so if 0 there is no space. Note, dir_offset will
+ * be at the next cluster so we can just make empty_space_offset
+ * that value.
+ */
+ if (empty_space_count == 0)
+ {
+ read_cluster = true;
+ empty_space_offset = dir_offset;
+ empty_space_entry = 0;
+ }
+
+ /*
+ * Have we read past the empty block ? If so go back and read it again.
+ */
+ if (dir_offset != empty_space_offset)
+ read_cluster = true;
+
+ /*
+ * Handle the entry writes.
+ */
+ lfn_start.cln = lfn_start.ofs = FAT_FILE_SHORT_NAME;
+ lfn_entry = 0;
+
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[9] read_cluster:%d eso:%ld ese:%ld\n",
+ read_cluster, empty_space_offset, empty_space_entry);
+#endif
+
+ /*
+ * The one more is the short entry.
+ */
+ while (lfn_entry < (lfn_entries + 1))
+ {
+ int length = 0;
+
+ if (read_cluster)
+ {
+ uint32_t new_length;
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[9.1] eso:%li\n", empty_space_offset);
+#endif
+ ret = fat_file_read(mt_entry, fat_fd,
+ (empty_space_offset * bts2rd), bts2rd,
+ fs_info->cl_buf);
+
+ if (ret != bts2rd)
+ {
+ if (ret != FAT_EOF)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[9.2] extending file:%li\n", empty_space_offset);
+#endif
+ ret = fat_file_extend (mt_entry, fat_fd, empty_space_offset * bts2rd,
+ &new_length);
+
+ if (ret != RC_OK)
+ return ret;
+
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[9.3] extended: %d <-> %d\n", new_length, empty_space_offset * bts2rd);
+#endif
+ if (new_length != (empty_space_offset * bts2rd))
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ memset(fs_info->cl_buf, 0, bts2rd);
+
+ ret = fat_file_write(mt_entry, fat_fd,
+ empty_space_offset * bts2rd,
+ bts2rd, fs_info->cl_buf);
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[9.4] clear write: %d\n", ret);
+#endif
+ if (ret == -1)
+ return ret;
+ else if (ret != bts2rd)
+ rtems_set_errno_and_return_minus_one(EIO);
+ }
+ }
+
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[10] eso:%li\n", empty_space_offset);
+#endif
+
+ for (dir_entry = empty_space_entry;
+ dir_entry < bts2rd;
+ dir_entry += MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE)
+ {
+ char* entry = (char*) fs_info->cl_buf + dir_entry;
+ char* p;
+ const char* n;
+ int i;
+ char fill = 0;
+
+ length += MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE;
+ lfn_entry++;
+
+#if MSDOS_FIND_PRINT
+ printf ("MSFS:[10] de:%li(%li) length:%i lfn_entry:%i\n",
+ dir_entry, (dir_entry / MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE),
+ length, lfn_entry);
+#endif
+ /*
+ * Time to write the short file name entry.
+ */
+ if (lfn_entry == (lfn_entries + 1))
+ {
+ /* get current cluster number */
+ int rc = fat_file_ioctl(mt_entry, fat_fd, F_CLU_NUM,
+ empty_space_offset * bts2rd,
+ &dir_pos->sname.cln);
+ if (rc != RC_OK)
+ return rc;
+
+ dir_pos->sname.ofs = dir_entry;
+
+ if (lfn_start.cln != FAT_FILE_SHORT_NAME)
+ {
+ rc = fat_file_ioctl(mt_entry, fat_fd, F_CLU_NUM,
+ lfn_start.cln * bts2rd,
+ &lfn_start.cln);
+ if (rc != RC_OK)
+ return rc;
+ }
+
+ dir_pos->lname.cln = lfn_start.cln;
+ dir_pos->lname.ofs = lfn_start.ofs;
+
+ /* write new node entry */
+ memcpy (entry, (uint8_t *) name_dir_entry,
+ MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE);
+ break;
+ }
+
+ /*
+ * This is a long file name and we need to write
+ * a long file name entry. See if this is the
+ * first entry written and if so remember the
+ * the location of the long file name.
+ */
+ if (lfn_start.cln == FAT_FILE_SHORT_NAME)
+ {
+ lfn_start.cln = empty_space_offset;
+ lfn_start.ofs = dir_entry;
+ }
+
+ /*
+ * Clear the entry before loading the data.
+ */
+ memset (entry, 0, MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE);
+
+ *MSDOS_DIR_LFN_CHECKSUM(entry) = lfn_checksum;
+
+ p = entry + 1;
+ n = name + (lfn_entries - lfn_entry) * MSDOS_LFN_LEN_PER_ENTRY;
+
+ for (i = 0; i < MSDOS_LFN_LEN_PER_ENTRY; i++)
+ {
+ if (*n != 0)
+ {
+ *p = *n;
+ n++;
+ }
+ else
+ {
+ p [0] = fill;
+ p [1] = fill;
+ fill = 0xff;
+ }
+
+ switch (i)
+ {
+ case 4:
+ p += 5;
+ break;
+ case 10:
+ p += 4;
+ break;
+ default:
+ p += 2;
+ break;
+ }
+ }
+
+ *MSDOS_DIR_ENTRY_TYPE(entry) = (lfn_entries - lfn_entry) + 1;
+ if (lfn_entry == 1)
+ *MSDOS_DIR_ENTRY_TYPE(entry) |= MSDOS_LAST_LONG_ENTRY;
+ *MSDOS_DIR_ATTR(entry) |= MSDOS_ATTR_LFN;
+ }
+
+ ret = fat_file_write(mt_entry, fat_fd,
+ (empty_space_offset * bts2rd) + empty_space_entry,
+ length, fs_info->cl_buf + empty_space_entry);
+ if (ret == -1)
+ return ret;
+ else if (ret != length)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ empty_space_offset++;
+ empty_space_entry = 0;
+ read_cluster = true;
+ }
+
+ return 0;
+}
+
+/* msdos_find_node_by_cluster_num_in_fat_file --
+ * Find node with specified number of cluster in fat-file.
+ *
+ * Note, not updated in the LFN change because it is only used
+ * for . and .. entries and these are always short.
+ *
+ * PARAMETERS:
+ * mt_entry - mount table entry
+ * fat_fd - fat-file descriptor
+ * cl4find - number of cluster to find
+ * paux - identify a node location on the disk -
+ * cluster num and offset inside the cluster
+ * dir_entry - placeholder for found node
+ *
+ * RETURNS:
+ * RC_OK on success, or error code if error occured
+ *
+ */
+int msdos_find_node_by_cluster_num_in_fat_file(
+ rtems_filesystem_mount_table_entry_t *mt_entry,
+ fat_file_fd_t *fat_fd,
+ uint32_t cl4find,
+ fat_dir_pos_t *dir_pos,
+ char *dir_entry
+ )
+{
+ int rc = RC_OK;
+ ssize_t ret = 0;
+ msdos_fs_info_t *fs_info = mt_entry->fs_info;
+ uint32_t bts2rd = 0;
+ uint32_t i = 0, j = 0;
+
+ if (FAT_FD_OF_ROOT_DIR(fat_fd) &&
+ (fs_info->fat.vol.type & (FAT_FAT12 | FAT_FAT16)))
+ bts2rd = fat_fd->fat_file_size;
+ else
+ bts2rd = fs_info->fat.vol.bpc;
+
+ while ((ret = fat_file_read(mt_entry, fat_fd, j * bts2rd, bts2rd,
+ fs_info->cl_buf)) != FAT_EOF)
+ {
+ if ( ret < MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE )
+ rtems_set_errno_and_return_minus_one( EIO );
+
+ assert(ret == bts2rd);
+
+ for (i = 0; i < bts2rd; i += MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE)
+ {
+ char* entry = (char*) fs_info->cl_buf + i;
+
+ /* if this and all rest entries are empty - return not-found */
+ if ((*MSDOS_DIR_ENTRY_TYPE(entry)) ==
+ MSDOS_THIS_DIR_ENTRY_AND_REST_EMPTY)
+ return MSDOS_NAME_NOT_FOUND_ERR;
+
+ /* if this entry is empty - skip it */
+ if ((*MSDOS_DIR_ENTRY_TYPE(entry)) ==
+ MSDOS_THIS_DIR_ENTRY_EMPTY)
+ continue;
+
+ /* if get a non-empty entry - compare clusters num */
+ if (MSDOS_EXTRACT_CLUSTER_NUM(entry) == cl4find)
+ {
+ /* on success fill aux structure and copy all 32 bytes */
+ rc = fat_file_ioctl(mt_entry, fat_fd, F_CLU_NUM, j * bts2rd,
+ &dir_pos->sname.cln);
+ if (rc != RC_OK)
+ return rc;
+
+ dir_pos->sname.ofs = i;
+ dir_pos->lname.cln = FAT_FILE_SHORT_NAME;
+ dir_pos->lname.ofs = FAT_FILE_SHORT_NAME;
+
+ memcpy(dir_entry, entry,
+ MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE);
+ return RC_OK;
+ }
+ }
+ j++;
+ }
+ return MSDOS_NAME_NOT_FOUND_ERR;
+}
diff --git a/cpukit/libfs/src/dosfs/msdos_mknod.c b/cpukit/libfs/src/dosfs/msdos_mknod.c
new file mode 100644
index 0000000000..ad4ad50d00
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/msdos_mknod.c
@@ -0,0 +1,84 @@
+/*
+ * Routine for node creation in MSDOS filesystem.
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <rtems.h>
+
+#include <rtems/libio_.h>
+
+#include "fat.h"
+#include "fat_fat_operations.h"
+#include "fat_file.h"
+
+#include "msdos.h"
+
+/* msdos_mknod --
+ * The following function checks spelling and formats name for a new node,
+ * determines type of the node to be created and creates it.
+ *
+ * PARAMETERS:
+ * name - file name to create
+ * mode - node type
+ * dev - dev
+ * pathloc - parent directory description
+ *
+ * RETURNS:
+ * RC_OK on succes, or -1 if error occured and set errno
+ *
+ */
+int msdos_mknod(
+ const char *name,
+ mode_t mode,
+ dev_t dev,
+ rtems_filesystem_location_info_t *pathloc
+)
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = pathloc->mt_entry->fs_info;
+ msdos_token_types_t type = 0;
+
+ /*
+ * Figure out what type of msdos node this is.
+ */
+ if (S_ISDIR(mode))
+ {
+ type = MSDOS_DIRECTORY;
+ }
+ else if (S_ISREG(mode))
+ {
+ type = MSDOS_REGULAR_FILE;
+ }
+ else
+ rtems_set_errno_and_return_minus_one(EINVAL);
+
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ /* Create an MSDOS node */
+ rc = msdos_creat_node(pathloc, type, name, strlen(name), mode, NULL);
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+}
diff --git a/cpukit/libfs/src/dosfs/msdos_node_type.c b/cpukit/libfs/src/dosfs/msdos_node_type.c
new file mode 100644
index 0000000000..877cf60492
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/msdos_node_type.c
@@ -0,0 +1,58 @@
+/*
+ * The following returns the type of node that the loc refers to.
+ *
+ * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
+ * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <rtems.h>
+
+#include <rtems/libio_.h>
+
+#include "fat.h"
+#include "fat_fat_operations.h"
+#include "fat_file.h"
+
+#include "msdos.h"
+
+/* msdos_node_type --
+ * Determine type of the node that the pathloc refers to.
+ *
+ * PARAMETERS:
+ * pathloc - node description
+ *
+ * RETURNS:
+ * node type
+ *
+ */
+rtems_filesystem_node_types_t
+msdos_node_type(rtems_filesystem_location_info_t *pathloc)
+{
+ fat_file_fd_t *fat_fd;
+
+ /*
+ * we don't need to obtain the volume semaphore here because node_type_h
+ * call always follows evalpath_h call(hence link increment occured) and
+ * hence node_access memory can't be freed during processing node_type_h
+ * call
+ */
+ fat_fd = pathloc->node_access;
+
+ return fat_fd->fat_file_type;
+}
diff --git a/cpukit/libfs/src/dosfs/msdos_rename.c b/cpukit/libfs/src/dosfs/msdos_rename.c
new file mode 100644
index 0000000000..1f5a6d9c6a
--- /dev/null
+++ b/cpukit/libfs/src/dosfs/msdos_rename.c
@@ -0,0 +1,92 @@
+/*
+ * Routine to rename a MSDOS filesystem node
+ *
+ * Copyright (C) 2010 Chris Johns <chrisj@rtems.org>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * @(#) $Id$
+ *
+ */
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <rtems/libio_.h>
+#include <time.h>
+
+#include "fat.h"
+#include "fat_fat_operations.h"
+#include "fat_file.h"
+
+#include "msdos.h"
+
+/* msdos_rename --
+ * Rename the node by removing the exitsing directory entry and creating a
+ * new one.
+ *
+ * PARAMETERS:
+ * old_parent_loc - node description for the "old parent" node
+ * old_loc - node description for the "old" node
+ * new_parent_loc - node description for the "parent" node
+ * name - name of new node
+ *
+ * RETURNS:
+ * RC_OK on success, or -1 if error occured (errno set appropriately)
+ */
+int
+msdos_rename(rtems_filesystem_location_info_t *old_parent_loc,
+ rtems_filesystem_location_info_t *old_loc,
+ rtems_filesystem_location_info_t *new_parent_loc,
+ const char *new_name)
+{
+ int rc = RC_OK;
+ rtems_status_code sc = RTEMS_SUCCESSFUL;
+ msdos_fs_info_t *fs_info = new_parent_loc->mt_entry->fs_info;
+ fat_file_fd_t *old_fat_fd = old_loc->node_access;
+ const char *token;
+ int len;
+
+ /*
+ * check spelling and format new node name
+ */
+ if (MSDOS_NAME != msdos_get_token(new_name, strlen(new_name), &token, &len)) {
+ rtems_set_errno_and_return_minus_one(ENAMETOOLONG);
+ }
+ /*
+ * lock volume
+ */
+ sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
+ MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
+ if (sc != RTEMS_SUCCESSFUL)
+ rtems_set_errno_and_return_minus_one(EIO);
+
+ /*
+ * create new directory entry as "hard link", copying relevant info from
+ * existing file
+ */
+ rc = msdos_creat_node(new_parent_loc,
+ MSDOS_HARD_LINK,new_name,len,S_IFREG,
+ old_fat_fd);
+ if (rc != RC_OK)
+ {
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+ }
+
+ /*
+ * mark file removed
+ */
+ rc = msdos_set_first_char4file_name(old_loc->mt_entry,
+ &old_fat_fd->dir_pos,
+ MSDOS_THIS_DIR_ENTRY_EMPTY);
+
+ rtems_semaphore_release(fs_info->vol_sema);
+ return rc;
+}