1 /*
2 libfat.c
3 Simple functionality for startup, mounting and unmounting of FAT-based devices.
4
5 Copyright (c) 2006 Michael "Chishm" Chisholm
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10 1. Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation and/or
14 other materials provided with the distribution.
15 3. The name of the author may not be used to endorse or promote products derived
16 from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "common.h"
33 #include "partition.h"
34 #include "fatfile.h"
35 #include "fatdir.h"
36 #include "lock.h"
37 #include "mem_allocate.h"
38 #include "disc.h"
39
40 #ifndef LIBFAT_PC
41 #include <sys/iosupport.h>
42 #include <unistd.h>
43 #endif
44
45
46 static const struct devoptab_t dotab_fat = {
47 "fat",
48 sizeof (FILE_STRUCT),
49 _FAT_open_r,
50 _FAT_close_r,
51 _FAT_write_r,
52 _FAT_read_r,
53 _FAT_seek_r,
54 _FAT_fstat_r,
55 _FAT_stat_r,
56 _FAT_link_r,
57 _FAT_unlink_r,
58 _FAT_chdir_r,
59 _FAT_rename_r,
60 _FAT_mkdir_r,
61 sizeof (DIR_STATE_STRUCT),
62 _FAT_diropen_r,
63 _FAT_dirreset_r,
64 _FAT_dirnext_r,
65 _FAT_dirclose_r,
66 #ifndef LIBFAT_PC
67 _FAT_statvfs_r,
68 #endif
69 _FAT_ftruncate_r,
70 _FAT_fsync_r,
71 NULL, /* Device data */
72 NULL,
73 NULL
74 };
75
76 #ifdef LIBFAT_PC
77 static struct devoptab_t* _sole_device = NULL;
GetDeviceOpTab(const char * name)78 struct devoptab_t* GetDeviceOpTab(const char* name)
79 {
80 return _sole_device;
81 }
82 #endif
83
84
fatMount(const char * name,const DISC_INTERFACE * interface,sec_t startSector,uint32_t cacheSize,uint32_t SectorsPerPage)85 bool fatMount (const char* name, const DISC_INTERFACE* interface, sec_t startSector, uint32_t cacheSize, uint32_t SectorsPerPage) {
86 PARTITION* partition;
87 struct devoptab_t* devops;
88 //char* nameCopy;
89
90 if(!name || strlen(name) > 8 || !interface)
91 return false;
92
93 if(!interface->startup())
94 return false;
95
96 if(!interface->isInserted())
97 return false;
98
99 //char devname[10];
100 //sprintf(devname, "%s:", name);
101 //if(FindDevice(devname) >= 0)
102 // return true;
103
104 devops = (struct devoptab_t*)_FAT_mem_allocate (sizeof(struct devoptab_t) + strlen(name) + 1);
105 if (!devops) {
106 return false;
107 }
108
109 #ifdef LIBFAT_PC
110 _sole_device = devops;
111 #endif
112
113 // Use the space allocated at the end of the devoptab struct for storing the name
114 //nameCopy = (char*)(devops+1);
115
116 // Initialize the file system
117 partition = _FAT_partition_constructor (interface, cacheSize, SectorsPerPage, startSector);
118 if (!partition) {
119 _FAT_mem_free (devops);
120 return false;
121 }
122
123 // Add an entry for this device to the devoptab table
124 memcpy (devops, &dotab_fat, sizeof(dotab_fat));
125 //strcpy (nameCopy, name);
126 //devops->name = nameCopy;
127 devops->deviceData = partition;
128
129 //AddDevice (devops);
130
131 return true;
132 }
133
fatMountSimple(const char * name,const DISC_INTERFACE * interface)134 bool fatMountSimple (const char* name, const DISC_INTERFACE* interface) {
135 return fatMount (name, interface, 0, DEFAULT_CACHE_PAGES, DEFAULT_SECTORS_PAGE);
136 }
137
fatUnmountDirect(struct devoptab_t * devops)138 void fatUnmountDirect (struct devoptab_t *devops) {
139 PARTITION* partition = (PARTITION*)devops->deviceData;
140 _FAT_partition_destructor (partition);
141 _FAT_mem_free (devops);
142 _sole_device = NULL;
143 }
144
145 //void fatUnmount (const char* name) {
146 // struct devoptab_t *devops;
147 // PARTITION* partition;
148 //
149 // if(!name)
150 // return;
151 //
152 // devops = (struct devoptab_t*)GetDeviceOpTab (name);
153 // if (!devops) {
154 // return;
155 // }
156 //
157 // // Perform a quick check to make sure we're dealing with a libfat controlled device
158 // //if (devops->open_r != dotab_fat.open_r) {
159 // // return;
160 // //}
161 //
162 // if (RemoveDevice (name) == -1) {
163 // return;
164 // }
165 //
166 // partition = (PARTITION*)devops->deviceData;
167 // _FAT_partition_destructor (partition);
168 // _FAT_mem_free (devops);
169 //}
170
171 //bool fatInit (uint32_t cacheSize, bool setAsDefaultDevice) {
172 // int i;
173 // int defaultDevice = -1;
174 // const DISC_INTERFACE *disc;
175 //
176 // for (i = 0;
177 // _FAT_disc_interfaces[i].name != NULL && _FAT_disc_interfaces[i].getInterface != NULL;
178 // i++)
179 // {
180 // disc = _FAT_disc_interfaces[i].getInterface();
181 // if (fatMount (_FAT_disc_interfaces[i].name, disc, 0, cacheSize, DEFAULT_SECTORS_PAGE)) {
182 // // The first device to successfully mount is set as the default
183 // if (defaultDevice < 0) {
184 // defaultDevice = i;
185 // }
186 // }
187 // }
188 //
189 // if (defaultDevice < 0) {
190 // // None of our devices mounted
191 // return false;
192 // }
193 //
194 // if (setAsDefaultDevice) {
195 // char filePath[MAXPATHLEN * 2];
196 // strcpy (filePath, _FAT_disc_interfaces[defaultDevice].name);
197 // strcat (filePath, ":/");
198 //#ifdef ARGV_MAGIC
199 // if ( __system_argv->argvMagic == ARGV_MAGIC && __system_argv->argc >= 1 && strrchr( __system_argv->argv[0], '/' )!=NULL ) {
200 // // Check the app's path against each of our mounted devices, to see
201 // // if we can support it. If so, change to that path.
202 // for (i = 0;
203 // _FAT_disc_interfaces[i].name != NULL && _FAT_disc_interfaces[i].getInterface != NULL;
204 // i++)
205 // {
206 // if ( !strncasecmp( __system_argv->argv[0], _FAT_disc_interfaces[i].name,
207 // strlen(_FAT_disc_interfaces[i].name)))
208 // {
209 // char *lastSlash;
210 // strcpy(filePath, __system_argv->argv[0]);
211 // lastSlash = strrchr( filePath, '/' );
212 //
213 // if ( NULL != lastSlash) {
214 // if ( *(lastSlash - 1) == ':') lastSlash++;
215 // *lastSlash = 0;
216 // }
217 // }
218 // }
219 // }
220 //#endif
221 // chdir (filePath);
222 // }
223 //
224 // return true;
225 //}
226
227 //bool fatInitDefault (void) {
228 // return fatInit (DEFAULT_CACHE_PAGES, true);
229 //}
230
231 //void fatGetVolumeLabel (const char* name, char *label) {
232 // struct devoptab_t *devops;
233 // PARTITION* partition;
234 // char *buf;
235 // int namelen,i;
236 //
237 // if(!name || !label)
238 // return;
239 //
240 // namelen = strlen(name);
241 // buf=(char*)_FAT_mem_allocate(sizeof(char)*namelen+2);
242 // strcpy(buf,name);
243 //
244 // if (name[namelen-1] == '/') {
245 // buf[namelen-1]='\0';
246 // namelen--;
247 // }
248 //
249 // if (name[namelen-1] != ':') {
250 // buf[namelen]=':';
251 // buf[namelen+1]='\0';
252 // }
253 //
254 // devops = (struct devoptab_t*)GetDeviceOpTab(buf);
255 //
256 // for(i=0;buf[i]!='\0' && buf[i]!=':';i++);
257 // if (!devops || strncasecmp(buf,devops->name,i)) {
258 // _FAT_mem_free(buf);
259 // return;
260 // }
261 //
262 // _FAT_mem_free(buf);
263 //
264 // // Perform a quick check to make sure we're dealing with a libfat controlled device
265 // if (devops->open_r != dotab_fat.open_r) {
266 // return;
267 // }
268 //
269 // partition = (PARTITION*)devops->deviceData;
270 //
271 // if(!_FAT_directory_getVolumeLabel(partition, label)) {
272 // strncpy(label,partition->label,11);
273 // label[11]='\0';
274 // }
275 // if(!strncmp(label, "NO NAME", 7)) label[0]='\0';
276 //}
277