wut/libraries/wutdevoptab_sd/devoptab_sd.c

112 lines
2.5 KiB
C
Raw Normal View History

2018-06-13 14:36:15 +02:00
#include "devoptab_sd.h"
2018-05-27 13:11:45 +02:00
static devoptab_t
__wut_fs_devoptab =
{
2018-06-13 14:36:15 +02:00
.name = "sd",
2018-05-27 13:11:45 +02:00
.structSize = sizeof(__wut_fs_file_t),
.open_r = __wut_fs_open,
.close_r = __wut_fs_close,
.write_r = __wut_fs_write,
.read_r = __wut_fs_read,
.seek_r = __wut_fs_seek,
.fstat_r = __wut_fs_fstat,
.stat_r = __wut_fs_stat,
.link_r = __wut_fs_link,
.unlink_r = __wut_fs_unlink,
.chdir_r = __wut_fs_chdir,
.rename_r = __wut_fs_rename,
.mkdir_r = __wut_fs_mkdir,
.dirStateSize = sizeof(__wut_fs_dir_t),
.diropen_r = __wut_fs_diropen,
.dirreset_r = __wut_fs_dirreset,
.dirnext_r = __wut_fs_dirnext,
.dirclose_r = __wut_fs_dirclose,
.statvfs_r = __wut_fs_statvfs,
.ftruncate_r = __wut_fs_ftruncate,
.fsync_r = __wut_fs_fsync,
.deviceData = NULL,
.chmod_r = __wut_fs_chmod,
.fchmod_r = __wut_fs_fchmod,
.rmdir_r = __wut_fs_rmdir,
2018-06-13 14:36:15 +02:00
// .lstat_r
// .utimes_r
2018-05-27 13:11:45 +02:00
};
FSClient *
2018-06-13 14:36:15 +02:00
__wut_devoptab_sd_client = NULL;
2018-05-27 13:11:45 +02:00
static BOOL
__wut_fs_initialised = FALSE;
2018-05-27 13:11:45 +02:00
FSStatus
2018-06-13 14:36:15 +02:00
__init_wut_devoptab_sd()
2018-05-27 13:11:45 +02:00
{
FSStatus rc = 0;
if (__wut_fs_initialised) {
return rc;
}
2018-06-13 14:36:15 +02:00
__wut_devoptab_sd_client = memalign(0x20, sizeof(FSClient));
2018-05-27 13:11:45 +02:00
FSCmdBlock fsCmd;
FSMountSource mountSource;
char mountPath[0x80];
char workDir[0x83];
FSInit();
2018-06-13 14:36:15 +02:00
rc = FSAddClient(__wut_devoptab_sd_client, -1);
2018-05-27 13:11:45 +02:00
if (rc < 0) {
2018-06-13 14:36:15 +02:00
free(__wut_devoptab_sd_client);
2018-05-27 13:11:45 +02:00
return rc;
}
FSInitCmdBlock(&fsCmd);
if (rc >= 0) {
int dev = AddDevice(&__wut_fs_devoptab);
if(dev != -1) {
setDefaultDevice(dev);
__wut_fs_initialised = TRUE;
2018-05-27 13:11:45 +02:00
// Mount the SD card
2018-06-13 14:36:15 +02:00
rc = FSGetMountSource(__wut_devoptab_sd_client, &fsCmd, FS_MOUNT_SOURCE_SD, &mountSource, -1);
2018-05-27 13:11:45 +02:00
if (rc < 0) {
return rc;
}
2018-06-13 14:36:15 +02:00
rc = FSMount(__wut_devoptab_sd_client, &fsCmd, &mountSource, mountPath, 0x80, -1);
2018-05-27 13:11:45 +02:00
if (rc >= 0) {
// chdir to SD root for general use
2018-06-13 14:36:15 +02:00
strcpy(workDir, "sd:");
2018-05-27 13:11:45 +02:00
strcat(workDir, mountPath);
chdir(workDir);
}
} else {
2018-06-13 14:36:15 +02:00
FSDelClient(__wut_devoptab_sd_client, -1);
free(__wut_devoptab_sd_client);
2018-05-27 13:11:45 +02:00
return dev;
}
}
return rc;
}
FSStatus
2018-06-13 14:36:15 +02:00
__fini_wut_devoptab_sd()
2018-05-27 13:11:45 +02:00
{
FSStatus rc = 0;
if (!__wut_fs_initialised) {
return rc;
}
2018-06-13 14:36:15 +02:00
FSDelClient(__wut_devoptab_sd_client, -1);
free(__wut_devoptab_sd_client);
2018-05-27 13:11:45 +02:00
return rc;
}