Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:16

0001 /**
0002  * @file
0003  *
0004  * @ingroup libfs_msdos MSDOS FileSystem
0005  *
0006  * @brief MSDOS Filesystem Initialization
0007  */
0008 
0009 /*
0010  *  Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
0011  *  Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
0012  *
0013  *  The license and distribution terms for this file may be
0014  *  found in the file LICENSE in this distribution or at
0015  *  http://www.rtems.org/license/LICENSE.
0016  */
0017 
0018 #ifdef HAVE_CONFIG_H
0019 #include "config.h"
0020 #endif
0021 
0022 #include <sys/types.h>
0023 #include <fcntl.h>
0024 #include <unistd.h>
0025 #include <stdlib.h>
0026 #include <stdio.h>
0027 
0028 #include <rtems.h>
0029 #include <rtems/libio_.h>
0030 
0031 #include "fat.h"
0032 #include "fat_fat_operations.h"
0033 #include "fat_file.h"
0034 
0035 #include "msdos.h"
0036 
0037 /* msdos_initialize_support --
0038  *     MSDOS filesystem initialization
0039  *
0040  * PARAMETERS:
0041  *     temp_mt_entry      - mount table entry
0042  *     op_table           - filesystem operations table
0043  *     file_handlers      - file operations table
0044  *     directory_handlers - directory operations table
0045  *
0046  * RETURNS:
0047  *     RC_OK and filled temp_mt_entry on success, or -1 if error occurred
0048  *     (errno set apropriately)
0049  *
0050  */
0051 int
0052 msdos_initialize_support(
0053     rtems_filesystem_mount_table_entry_t    *temp_mt_entry,
0054     const rtems_filesystem_operations_table *op_table,
0055     const rtems_filesystem_file_handlers_r  *file_handlers,
0056     const rtems_filesystem_file_handlers_r  *directory_handlers,
0057     rtems_dosfs_convert_control             *converter
0058     )
0059 {
0060     int                rc = RC_OK;
0061     msdos_fs_info_t   *fs_info = NULL;
0062     fat_file_fd_t     *fat_fd = NULL;
0063     fat_dir_pos_t      root_pos;
0064     uint32_t           cl_buf_size;
0065 
0066     fs_info = (msdos_fs_info_t *)calloc(1, sizeof(msdos_fs_info_t));
0067     if (!fs_info)
0068         rtems_set_errno_and_return_minus_one(ENOMEM);
0069 
0070     temp_mt_entry->fs_info = fs_info;
0071 
0072     fs_info->converter = converter;
0073 
0074     rc = fat_init_volume_info(&fs_info->fat, temp_mt_entry->dev);
0075     if (rc != RC_OK)
0076     {
0077         free(fs_info);
0078         return rc;
0079     }
0080 
0081     fs_info->file_handlers      = file_handlers;
0082     fs_info->directory_handlers = directory_handlers;
0083 
0084     /*
0085      * open fat-file which correspondes to  root directory
0086      * (so inode number 0x00000010 is always used for root directory)
0087      */
0088     fat_dir_pos_init(&root_pos);
0089     root_pos.sname.cln = FAT_ROOTDIR_CLUSTER_NUM;
0090     rc = fat_file_open(&fs_info->fat, &root_pos, &fat_fd);
0091     if (rc != RC_OK)
0092     {
0093         fat_shutdown_drive(&fs_info->fat);
0094         free(fs_info);
0095         return rc;
0096     }
0097 
0098     /* again: unfortunately "fat-file" is just almost fat file :( */
0099     fat_fd->fat_file_type = FAT_DIRECTORY;
0100     fat_fd->size_limit = MSDOS_MAX_DIR_LENGTH;
0101     fat_fd->cln = fs_info->fat.vol.rdir_cl;
0102 
0103     fat_fd->map.file_cln = 0;
0104     fat_fd->map.disk_cln = fat_fd->cln;
0105 
0106     /* if we have FAT12/16 */
0107     if ( fat_fd->cln == 0 )
0108     {
0109         fat_fd->fat_file_size = fs_info->fat.vol.rdir_size;
0110         cl_buf_size = (fs_info->fat.vol.bpc > fs_info->fat.vol.rdir_size) ?
0111                       fs_info->fat.vol.bpc                                :
0112                       fs_info->fat.vol.rdir_size;
0113     }
0114     else
0115     {
0116         rc = fat_file_size(&fs_info->fat, fat_fd);
0117         if ( rc != RC_OK )
0118         {
0119             fat_file_close(&fs_info->fat, fat_fd);
0120             fat_shutdown_drive(&fs_info->fat);
0121             free(fs_info);
0122             return rc;
0123         }
0124         cl_buf_size = fs_info->fat.vol.bpc;
0125     }
0126 
0127     fs_info->cl_buf = (uint8_t *)calloc(cl_buf_size, sizeof(char));
0128     if (fs_info->cl_buf == NULL)
0129     {
0130         fat_file_close(&fs_info->fat, fat_fd);
0131         fat_shutdown_drive(&fs_info->fat);
0132         free(fs_info);
0133         rtems_set_errno_and_return_minus_one(ENOMEM);
0134     }
0135 
0136     rtems_recursive_mutex_init(&fs_info->vol_mutex,
0137                                RTEMS_FILESYSTEM_TYPE_DOSFS);
0138 
0139     temp_mt_entry->mt_fs_root->location.node_access = fat_fd;
0140     temp_mt_entry->mt_fs_root->location.handlers = directory_handlers;
0141     temp_mt_entry->ops = op_table;
0142 
0143     return rc;
0144 }