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>
34 #include "partition.h"
38 #include "mem_allocate.h"
41 static const devoptab_t dotab_fat
= {
56 sizeof (DIR_STATE_STRUCT
),
64 NULL
, /* Device data */
69 bool fatMount (const char* name
, const DISC_INTERFACE
* interface
, sec_t startSector
, uint32_t cacheSize
, uint32_t SectorsPerPage
) {
74 if(!name
|| !interface
)
77 if(!interface
->startup())
80 if(!interface
->isInserted())
83 devops
= _FAT_mem_allocate (sizeof(devoptab_t
) + strlen(name
) + 1);
87 // Use the space allocated at the end of the devoptab struct for storing the name
88 nameCopy
= (char*)(devops
+1);
90 // Initialize the file system
91 partition
= _FAT_partition_constructor (interface
, cacheSize
, SectorsPerPage
, startSector
);
93 _FAT_mem_free (devops
);
97 // Add an entry for this device to the devoptab table
98 memcpy (devops
, &dotab_fat
, sizeof(dotab_fat
));
99 strcpy (nameCopy
, name
);
100 devops
->name
= nameCopy
;
101 devops
->deviceData
= partition
;
108 bool fatMountSimple (const char* name
, const DISC_INTERFACE
* interface
) {
109 return fatMount (name
, interface
, 0, DEFAULT_CACHE_PAGES
, DEFAULT_SECTORS_PAGE
);
112 void fatUnmount (const char* name
) {
114 PARTITION
* partition
;
119 devops
= (devoptab_t
*)GetDeviceOpTab (name
);
124 // Perform a quick check to make sure we're dealing with a libfat controlled device
125 if (devops
->open_r
!= dotab_fat
.open_r
) {
129 if (RemoveDevice (name
) == -1) {
133 partition
= (PARTITION
*)devops
->deviceData
;
134 _FAT_partition_destructor (partition
);
135 _FAT_mem_free (devops
);
138 bool fatInit (uint32_t cacheSize
, bool setAsDefaultDevice
) {
140 int defaultDevice
= -1;
141 const DISC_INTERFACE
*disc
;
144 _FAT_disc_interfaces
[i
].name
!= NULL
&& _FAT_disc_interfaces
[i
].getInterface
!= NULL
;
147 disc
= _FAT_disc_interfaces
[i
].getInterface();
148 if (fatMount (_FAT_disc_interfaces
[i
].name
, disc
, 0, cacheSize
, DEFAULT_SECTORS_PAGE
)) {
149 // The first device to successfully mount is set as the default
150 if (defaultDevice
< 0) {
156 if (defaultDevice
< 0) {
157 // None of our devices mounted
161 if (setAsDefaultDevice
) {
162 char filePath
[MAXPATHLEN
* 2];
163 strcpy (filePath
, _FAT_disc_interfaces
[defaultDevice
].name
);
164 strcat (filePath
, ":/");
166 if ( __system_argv
->argvMagic
== ARGV_MAGIC
&& __system_argv
->argc
>= 1 && strrchr( __system_argv
->argv
[0], '/' )!=NULL
) {
167 // Check the app's path against each of our mounted devices, to see
168 // if we can support it. If so, change to that path.
170 _FAT_disc_interfaces
[i
].name
!= NULL
&& _FAT_disc_interfaces
[i
].getInterface
!= NULL
;
173 if ( !strncasecmp( __system_argv
->argv
[0], _FAT_disc_interfaces
[i
].name
,
174 strlen(_FAT_disc_interfaces
[i
].name
)))
177 strcpy(filePath
, __system_argv
->argv
[0]);
178 lastSlash
= strrchr( filePath
, '/' );
180 if ( NULL
!= lastSlash
) {
181 if ( *(lastSlash
- 1) == ':') lastSlash
++;
194 bool fatInitDefault (void) {
195 return fatInit (DEFAULT_CACHE_PAGES
, true);
198 void fatGetVolumeLabel (const char* name
, char *label
) {
200 PARTITION
* partition
;
207 namelen
= strlen(name
);
208 buf
=(char*)_FAT_mem_allocate(sizeof(char)*namelen
+2);
211 if (name
[namelen
-1] == '/') {
216 if (name
[namelen
-1] != ':') {
221 devops
= (devoptab_t
*)GetDeviceOpTab(buf
);
223 for(i
=0;buf
[i
]!='\0' && buf
[i
]!=':';i
++);
224 if (!devops
|| strncasecmp(buf
,devops
->name
,i
)) {
231 // Perform a quick check to make sure we're dealing with a libfat controlled device
232 if (devops
->open_r
!= dotab_fat
.open_r
) {
236 partition
= (PARTITION
*)devops
->deviceData
;
238 if(!_FAT_directory_getVolumeLabel(partition
, label
)) {
239 strncpy(label
,partition
->label
,11);
242 if(!strncmp(label
, "NO NAME", 7)) label
[0]='\0';