1 | /* |
---|
2 | * MSDOS Initialization support routine implementation |
---|
3 | * |
---|
4 | * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia |
---|
5 | * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru> |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.OARcorp.com/rtems/license.html. |
---|
10 | * |
---|
11 | * @(#) $Id$ |
---|
12 | */ |
---|
13 | |
---|
14 | #if HAVE_CONFIG_H |
---|
15 | #include "config.h" |
---|
16 | #endif |
---|
17 | |
---|
18 | #include <sys/types.h> |
---|
19 | #include <fcntl.h> |
---|
20 | #include <unistd.h> |
---|
21 | #include <stdlib.h> |
---|
22 | #include <stdio.h> |
---|
23 | |
---|
24 | #include <assert.h> |
---|
25 | #include <rtems.h> |
---|
26 | #include <rtems/libio_.h> |
---|
27 | |
---|
28 | #include "fat.h" |
---|
29 | #include "fat_fat_operations.h" |
---|
30 | #include "fat_file.h" |
---|
31 | |
---|
32 | #include "msdos.h" |
---|
33 | |
---|
34 | /* msdos_initialize_support -- |
---|
35 | * MSDOS filesystem initialization |
---|
36 | * |
---|
37 | * PARAMETERS: |
---|
38 | * temp_mt_entry - mount table entry |
---|
39 | * op_table - filesystem operations table |
---|
40 | * file_handlers - file operations table |
---|
41 | * directory_handlers - directory operations table |
---|
42 | * |
---|
43 | * RETURNS: |
---|
44 | * RC_OK and filled temp_mt_entry on success, or -1 if error occured |
---|
45 | * (errno set apropriately) |
---|
46 | * |
---|
47 | */ |
---|
48 | int |
---|
49 | msdos_initialize_support( |
---|
50 | rtems_filesystem_mount_table_entry_t *temp_mt_entry, |
---|
51 | rtems_filesystem_operations_table *op_table, |
---|
52 | rtems_filesystem_file_handlers_r *file_handlers, |
---|
53 | rtems_filesystem_file_handlers_r *directory_handlers |
---|
54 | ) |
---|
55 | { |
---|
56 | int rc = RC_OK; |
---|
57 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
58 | msdos_fs_info_t *fs_info = NULL; |
---|
59 | fat_file_fd_t *fat_fd = NULL; |
---|
60 | unsigned32 cl_buf_size; |
---|
61 | |
---|
62 | fs_info = (msdos_fs_info_t *)calloc(1, sizeof(msdos_fs_info_t)); |
---|
63 | if (!fs_info) |
---|
64 | set_errno_and_return_minus_one(ENOMEM); |
---|
65 | |
---|
66 | temp_mt_entry->fs_info = fs_info; |
---|
67 | |
---|
68 | rc = fat_init_volume_info(temp_mt_entry); |
---|
69 | if (rc != RC_OK) |
---|
70 | { |
---|
71 | free(fs_info); |
---|
72 | return rc; |
---|
73 | } |
---|
74 | |
---|
75 | fs_info->file_handlers = file_handlers; |
---|
76 | fs_info->directory_handlers = directory_handlers; |
---|
77 | |
---|
78 | /* |
---|
79 | * open fat-file which correspondes to root directory |
---|
80 | * (so inode number 0x00000010 is always used for root directory) |
---|
81 | */ |
---|
82 | rc = fat_file_open(temp_mt_entry, FAT_ROOTDIR_CLUSTER_NUM, 0, &fat_fd); |
---|
83 | if (rc != RC_OK) |
---|
84 | { |
---|
85 | fat_shutdown_drive(temp_mt_entry); |
---|
86 | free(fs_info); |
---|
87 | return rc; |
---|
88 | } |
---|
89 | |
---|
90 | /* again: unfortunately "fat-file" is just almost fat file :( */ |
---|
91 | fat_fd->fat_file_type = FAT_DIRECTORY; |
---|
92 | fat_fd->size_limit = MSDOS_MAX_DIR_LENGHT; |
---|
93 | fat_fd->info_cln = FAT_ROOTDIR_CLUSTER_NUM; |
---|
94 | fat_fd->info_ofs = 0; |
---|
95 | fat_fd->cln = fs_info->fat.vol.rdir_cl; |
---|
96 | |
---|
97 | fat_fd->map.file_cln = 0; |
---|
98 | fat_fd->map.disk_cln = fat_fd->cln; |
---|
99 | |
---|
100 | /* if we have FAT12/16 */ |
---|
101 | if ( fat_fd->cln == 0 ) |
---|
102 | { |
---|
103 | fat_fd->fat_file_size = fs_info->fat.vol.rdir_size; |
---|
104 | cl_buf_size = (fs_info->fat.vol.bpc > fs_info->fat.vol.rdir_size) ? |
---|
105 | fs_info->fat.vol.bpc : |
---|
106 | fs_info->fat.vol.rdir_size; |
---|
107 | } |
---|
108 | else |
---|
109 | { |
---|
110 | rc = fat_file_size(temp_mt_entry, fat_fd); |
---|
111 | if ( rc != RC_OK ) |
---|
112 | { |
---|
113 | fat_file_close(temp_mt_entry, fat_fd); |
---|
114 | fat_shutdown_drive(temp_mt_entry); |
---|
115 | free(fs_info); |
---|
116 | return rc; |
---|
117 | } |
---|
118 | cl_buf_size = fs_info->fat.vol.bpc; |
---|
119 | } |
---|
120 | |
---|
121 | fs_info->cl_buf = (char *)calloc(cl_buf_size, sizeof(char)); |
---|
122 | if (fs_info->cl_buf == NULL) |
---|
123 | { |
---|
124 | fat_file_close(temp_mt_entry, fat_fd); |
---|
125 | fat_shutdown_drive(temp_mt_entry); |
---|
126 | free(fs_info); |
---|
127 | set_errno_and_return_minus_one(ENOMEM); |
---|
128 | } |
---|
129 | |
---|
130 | sc = rtems_semaphore_create(3, |
---|
131 | 1, |
---|
132 | RTEMS_BINARY_SEMAPHORE | RTEMS_FIFO, |
---|
133 | RTEMS_INHERIT_PRIORITY, |
---|
134 | &fs_info->vol_sema); |
---|
135 | if (sc != RTEMS_SUCCESSFUL) |
---|
136 | { |
---|
137 | fat_file_close(temp_mt_entry, fat_fd); |
---|
138 | fat_shutdown_drive(temp_mt_entry); |
---|
139 | free(fs_info->cl_buf); |
---|
140 | free(fs_info); |
---|
141 | set_errno_and_return_minus_one( EIO ); |
---|
142 | } |
---|
143 | |
---|
144 | temp_mt_entry->mt_fs_root.node_access = fat_fd; |
---|
145 | temp_mt_entry->mt_fs_root.handlers = directory_handlers; |
---|
146 | temp_mt_entry->mt_fs_root.ops = op_table; |
---|
147 | |
---|
148 | return rc; |
---|
149 | } |
---|