3 Simple functionality for startup, mounting and unmounting of FAT-based devices.
5 Copyright (c) 2006 Michael "Chishm" Chisholm
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
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.
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.
29 #include <sys/iosupport.h>
35 #include "partition.h"
39 #include "mem_allocate.h"
42 static const devoptab_t dotab_fat
= {
57 sizeof (DIR_STATE_STRUCT
),
65 NULL
, /* Device data */
70 bool fatMount (const char* name
, const DISC_INTERFACE
* interface
, sec_t startSector
, uint32_t cacheSize
, uint32_t SectorsPerPage
) {
75 if(!name
|| strlen(name
) > 8 || !interface
)
78 if(!interface
->startup())
81 if(!interface
->isInserted())
85 sprintf(devname
, "%s:", name
);
86 if(FindDevice(devname
) >= 0)
89 devops
= _FAT_mem_allocate (sizeof(devoptab_t
) + strlen(name
) + 1);
93 // Use the space allocated at the end of the devoptab struct for storing the name
94 nameCopy
= (char*)(devops
+1);
96 // Initialize the file system
97 partition
= _FAT_partition_constructor (interface
, cacheSize
, SectorsPerPage
, startSector
);
99 _FAT_mem_free (devops
);
103 // Add an entry for this device to the devoptab table
104 memcpy (devops
, &dotab_fat
, sizeof(dotab_fat
));
105 strcpy (nameCopy
, name
);
106 devops
->name
= nameCopy
;
107 devops
->deviceData
= partition
;
114 bool fatMountSimple (const char* name
, const DISC_INTERFACE
* interface
) {
115 return fatMount (name
, interface
, 0, DEFAULT_CACHE_PAGES
, DEFAULT_SECTORS_PAGE
);
118 void fatUnmount (const char* name
) {
120 PARTITION
* partition
;
125 devops
= (devoptab_t
*)GetDeviceOpTab (name
);
130 // Perform a quick check to make sure we're dealing with a libfat controlled device
131 if (devops
->open_r
!= dotab_fat
.open_r
) {
135 if (RemoveDevice (name
) == -1) {
139 partition
= (PARTITION
*)devops
->deviceData
;
140 _FAT_partition_destructor (partition
);
141 _FAT_mem_free (devops
);
144 bool fatInit (uint32_t cacheSize
, bool setAsDefaultDevice
) {
146 int defaultDevice
= -1;
147 const DISC_INTERFACE
*disc
;
150 _FAT_disc_interfaces
[i
].name
!= NULL
&& _FAT_disc_interfaces
[i
].getInterface
!= NULL
;
153 disc
= _FAT_disc_interfaces
[i
].getInterface();
154 if (fatMount (_FAT_disc_interfaces
[i
].name
, disc
, 0, cacheSize
, DEFAULT_SECTORS_PAGE
)) {
155 // The first device to successfully mount is set as the default
156 if (defaultDevice
< 0) {
162 if (defaultDevice
< 0) {
163 // None of our devices mounted
167 if (setAsDefaultDevice
) {
168 char filePath
[MAXPATHLEN
* 2];
169 strcpy (filePath
, _FAT_disc_interfaces
[defaultDevice
].name
);
170 strcat (filePath
, ":/");
172 if ( __system_argv
->argvMagic
== ARGV_MAGIC
&& __system_argv
->argc
>= 1 && strrchr( __system_argv
->argv
[0], '/' )!=NULL
) {
173 // Check the app's path against each of our mounted devices, to see
174 // if we can support it. If so, change to that path.
176 _FAT_disc_interfaces
[i
].name
!= NULL
&& _FAT_disc_interfaces
[i
].getInterface
!= NULL
;
179 if ( !strncasecmp( __system_argv
->argv
[0], _FAT_disc_interfaces
[i
].name
,
180 strlen(_FAT_disc_interfaces
[i
].name
)))
183 strcpy(filePath
, __system_argv
->argv
[0]);
184 lastSlash
= strrchr( filePath
, '/' );
186 if ( NULL
!= lastSlash
) {
187 if ( *(lastSlash
- 1) == ':') lastSlash
++;
200 bool fatInitDefault (void) {
201 return fatInit (DEFAULT_CACHE_PAGES
, true);
204 void fatGetVolumeLabel (const char* name
, char *label
) {
206 PARTITION
* partition
;
213 namelen
= strlen(name
);
214 buf
=(char*)_FAT_mem_allocate(sizeof(char)*namelen
+2);
217 if (name
[namelen
-1] == '/') {
222 if (name
[namelen
-1] != ':') {
227 devops
= (devoptab_t
*)GetDeviceOpTab(buf
);
229 for(i
=0;buf
[i
]!='\0' && buf
[i
]!=':';i
++);
230 if (!devops
|| strncasecmp(buf
,devops
->name
,i
)) {
237 // Perform a quick check to make sure we're dealing with a libfat controlled device
238 if (devops
->open_r
!= dotab_fat
.open_r
) {
242 partition
= (PARTITION
*)devops
->deviceData
;
244 if(!_FAT_directory_getVolumeLabel(partition
, label
)) {
245 strncpy(label
,partition
->label
,11);
248 if(!strncmp(label
, "NO NAME", 7)) label
[0]='\0';