2006-07-14 04:42:37 +02:00
|
|
|
/*
|
|
|
|
libfat.c
|
|
|
|
Simple functionality for startup, mounting and unmounting of FAT-based devices.
|
|
|
|
|
|
|
|
Copyright (c) 2006 Michael "Chishm" Chisholm
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
|
|
|
3. The name of the author may not be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
2006-07-11 - Chishm
|
|
|
|
* Original release
|
2006-08-13 11:43:38 +02:00
|
|
|
|
|
|
|
2006-08-13 - Chishm
|
|
|
|
* Moved all externally visible directory related functions to fatdir
|
2006-08-14 14:04:56 +02:00
|
|
|
|
|
|
|
2006-08-14 - Chishm
|
|
|
|
* Added extended devoptab_t functions
|
2007-01-10 11:48:46 +01:00
|
|
|
|
|
|
|
2007-01-10 - Chishm
|
|
|
|
* fatInit now sets the correct path when setAsDefaultDevice
|
2007-01-11 12:23:18 +01:00
|
|
|
|
|
|
|
2007-01-11 - Chishm
|
|
|
|
* Added missing #include <unistd.h>
|
2007-10-25 14:52:08 +02:00
|
|
|
|
|
|
|
2007-10-25 - Chishm
|
|
|
|
* Added statvfs functionality
|
2006-07-14 04:42:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/iosupport.h>
|
2007-01-11 12:23:18 +01:00
|
|
|
#include <unistd.h>
|
2008-05-10 21:35:18 +02:00
|
|
|
#include <string.h>
|
2006-07-14 04:42:37 +02:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "partition.h"
|
|
|
|
#include "fatfile.h"
|
2006-08-13 11:43:38 +02:00
|
|
|
#include "fatdir.h"
|
2006-07-14 04:42:37 +02:00
|
|
|
|
2008-05-10 21:35:18 +02:00
|
|
|
#ifdef GBA
|
|
|
|
#define DEFAULT_CACHE_PAGES 2
|
|
|
|
#else
|
|
|
|
#define DEFAULT_CACHE_PAGES 8
|
|
|
|
#endif
|
2006-07-16 09:37:24 +02:00
|
|
|
|
2006-07-14 04:42:37 +02:00
|
|
|
const devoptab_t dotab_fat = {
|
|
|
|
"fat",
|
|
|
|
sizeof (FILE_STRUCT),
|
|
|
|
_FAT_open_r,
|
|
|
|
_FAT_close_r,
|
|
|
|
_FAT_write_r,
|
|
|
|
_FAT_read_r,
|
|
|
|
_FAT_seek_r,
|
|
|
|
_FAT_fstat_r,
|
|
|
|
_FAT_stat_r,
|
|
|
|
_FAT_link_r,
|
|
|
|
_FAT_unlink_r,
|
2006-08-14 13:59:25 +02:00
|
|
|
_FAT_chdir_r,
|
|
|
|
_FAT_rename_r,
|
|
|
|
_FAT_mkdir_r,
|
|
|
|
sizeof (DIR_STATE_STRUCT),
|
|
|
|
_FAT_diropen_r,
|
|
|
|
_FAT_dirreset_r,
|
|
|
|
_FAT_dirnext_r,
|
2007-10-25 14:52:08 +02:00
|
|
|
_FAT_dirclose_r,
|
|
|
|
_FAT_statvfs_r
|
2006-07-14 04:42:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
bool fatInit (u32 cacheSize, bool setAsDefaultDevice) {
|
|
|
|
|
2008-05-10 21:35:18 +02:00
|
|
|
int i;
|
|
|
|
bool device = false, setDefault = false;
|
|
|
|
|
|
|
|
if ( PI_MAX_PARTITIONS == 1 ) {
|
|
|
|
if ( _FAT_partition_mount ( 0 , cacheSize) ) {
|
|
|
|
_FAT_partition_setDefaultInterface (0);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2006-07-14 04:42:37 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2008-05-10 21:35:18 +02:00
|
|
|
for ( i = 1; i < PI_MAX_PARTITIONS; i++ ) {
|
|
|
|
device = _FAT_partition_mount ( i , cacheSize);
|
|
|
|
if ( device && !setDefault ) {
|
|
|
|
_FAT_partition_setDefaultInterface (i);
|
|
|
|
setDefault = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !setDefault ) return false;
|
|
|
|
}
|
2006-07-14 04:42:37 +02:00
|
|
|
|
|
|
|
AddDevice (&dotab_fat);
|
|
|
|
|
|
|
|
if (setAsDefaultDevice) {
|
2008-05-10 21:35:18 +02:00
|
|
|
char filePath[MAXPATHLEN * 2] = "fat:/";
|
|
|
|
#ifndef GBA
|
|
|
|
if ( __system_argv->argvMagic == ARGV_MAGIC && __system_argv->argc >= 1 ) {
|
2008-05-23 12:37:33 +02:00
|
|
|
|
|
|
|
if ( !strncasecmp( __system_argv->argv[0], "fat", 3)) {
|
|
|
|
|
|
|
|
strcpy(filePath, __system_argv->argv[0]);
|
|
|
|
char *lastSlash = strrchr( filePath, '/' );
|
|
|
|
|
|
|
|
if ( NULL != lastSlash) {
|
|
|
|
if ( *(lastSlash - 1) == ':') lastSlash++;
|
|
|
|
*lastSlash = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-05-10 21:35:18 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
chdir (filePath);
|
2006-07-14 04:42:37 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-07-16 09:37:24 +02:00
|
|
|
bool fatInitDefault (void) {
|
2008-05-10 21:35:18 +02:00
|
|
|
return fatInit (DEFAULT_CACHE_PAGES, true);
|
2006-07-16 09:37:24 +02:00
|
|
|
}
|
|
|
|
|
2006-07-14 04:42:37 +02:00
|
|
|
bool fatMountNormalInterface (PARTITION_INTERFACE partitionNumber, u32 cacheSize) {
|
|
|
|
return _FAT_partition_mount (partitionNumber, cacheSize);
|
|
|
|
}
|
|
|
|
|
2006-07-25 12:36:42 +02:00
|
|
|
bool fatMountCustomInterface (const IO_INTERFACE* device, u32 cacheSize) {
|
2006-07-14 04:42:37 +02:00
|
|
|
return _FAT_partition_mountCustomInterface (device, cacheSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool fatUnmount (PARTITION_INTERFACE partitionNumber) {
|
|
|
|
return _FAT_partition_unmount (partitionNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool fatUnsafeUnmount (PARTITION_INTERFACE partitionNumber) {
|
|
|
|
return _FAT_partition_unsafeUnmount (partitionNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool fatSetDefaultInterface (PARTITION_INTERFACE partitionNumber) {
|
|
|
|
return _FAT_partition_setDefaultInterface (partitionNumber);
|
|
|
|
}
|