2016-12-10 15:32:03 +01:00
|
|
|
#include "fsinit.h"
|
|
|
|
#include "fsdrive.h"
|
|
|
|
#include "virtual.h"
|
|
|
|
#include "sddata.h"
|
|
|
|
#include "image.h"
|
|
|
|
#include "ff.h"
|
|
|
|
|
2017-09-09 19:12:28 +02:00
|
|
|
// FATFS filesystem objects (x10)
|
|
|
|
static FATFS fs[NORM_FS];
|
2016-12-10 15:32:03 +01:00
|
|
|
|
|
|
|
// currently open file systems
|
|
|
|
static bool fs_mounted[NORM_FS] = { false };
|
|
|
|
|
|
|
|
bool InitSDCardFS() {
|
|
|
|
fs_mounted[0] = (f_mount(fs, "0:", 1) == FR_OK);
|
|
|
|
return fs_mounted[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InitExtFS() {
|
2018-02-26 23:58:37 +01:00
|
|
|
static bool ramdrv_ready = false;
|
2020-08-24 21:27:19 -07:00
|
|
|
|
2016-12-10 15:32:03 +01:00
|
|
|
for (u32 i = 1; i < NORM_FS; i++) {
|
|
|
|
char fsname[8];
|
2023-03-20 23:13:51 -05:00
|
|
|
snprintf(fsname, sizeof(fsname), "%lu:", i);
|
2016-12-10 15:32:03 +01:00
|
|
|
if (fs_mounted[i]) continue;
|
|
|
|
fs_mounted[i] = (f_mount(fs + i, fsname, 1) == FR_OK);
|
2018-02-26 23:58:37 +01:00
|
|
|
if ((!fs_mounted[i] || !ramdrv_ready) && (i == NORM_FS - 1) && !(GetMountState() & IMG_NAND)) {
|
2018-01-24 23:32:06 +01:00
|
|
|
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
|
|
|
|
if (!buffer) bkpt; // whatever, this won't go wrong anyways
|
2019-12-11 00:08:35 +01:00
|
|
|
f_mkfs(fsname, NULL, buffer, STD_BUFFER_SIZE); // format ramdrive if required
|
2018-01-24 23:32:06 +01:00
|
|
|
free(buffer);
|
2016-12-13 16:00:14 +01:00
|
|
|
f_mount(NULL, fsname, 1);
|
|
|
|
fs_mounted[i] = (f_mount(fs + i, fsname, 1) == FR_OK);
|
2018-02-26 23:58:37 +01:00
|
|
|
ramdrv_ready = true;
|
2016-12-13 16:00:14 +01:00
|
|
|
}
|
2016-12-10 15:32:03 +01:00
|
|
|
}
|
|
|
|
SetupNandSdDrive("A:", "0:", "1:/private/movable.sed", 0);
|
|
|
|
SetupNandSdDrive("B:", "0:", "4:/private/movable.sed", 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InitImgFS(const char* path) {
|
2017-08-01 01:16:15 +02:00
|
|
|
// find drive # of the last image FAT drive
|
|
|
|
u32 drv_i = NORM_FS - IMGN_FS;
|
2017-08-07 23:33:08 +02:00
|
|
|
char fsname[8];
|
2017-08-01 01:16:15 +02:00
|
|
|
for (; drv_i < NORM_FS; drv_i++) {
|
2023-03-20 23:13:51 -05:00
|
|
|
snprintf(fsname, sizeof(fsname), "%lu:", drv_i);
|
2017-08-07 23:33:08 +02:00
|
|
|
if (!(DriveType(fsname)&DRV_IMAGE)) break;
|
2017-08-01 01:16:15 +02:00
|
|
|
}
|
2019-09-21 10:37:14 -04:00
|
|
|
// deinit virtual filesystem
|
|
|
|
DeinitVirtualImageDrive();
|
2016-12-10 15:32:03 +01:00
|
|
|
// deinit image filesystem
|
2017-02-28 16:10:09 +01:00
|
|
|
DismountDriveType(DRV_IMAGE);
|
2016-12-10 15:32:03 +01:00
|
|
|
// (re)mount image, done if path == NULL
|
2017-10-16 02:02:24 +02:00
|
|
|
u64 type = MountImage(path);
|
2016-12-20 14:41:03 +01:00
|
|
|
InitVirtualImageDrive();
|
2017-08-07 23:33:08 +02:00
|
|
|
if ((type&IMG_NAND) && (drv_i < NORM_FS)) drv_i = NORM_FS;
|
|
|
|
else if ((type&IMG_FAT) && (drv_i < NORM_FS - IMGN_FS + 1)) drv_i = NORM_FS - IMGN_FS + 1;
|
2016-12-10 15:32:03 +01:00
|
|
|
// reinit image filesystem
|
2017-08-01 01:16:15 +02:00
|
|
|
for (u32 i = NORM_FS - IMGN_FS; i < drv_i; i++) {
|
2023-03-20 23:13:51 -05:00
|
|
|
snprintf(fsname, sizeof(fsname), "%lu:", i);
|
2016-12-10 15:32:03 +01:00
|
|
|
fs_mounted[i] = (f_mount(fs + i, fsname, 1) == FR_OK);
|
|
|
|
}
|
2017-02-20 22:59:03 +01:00
|
|
|
return GetMountState();
|
2016-12-10 15:32:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeinitExtFS() {
|
2020-07-31 02:18:44 -04:00
|
|
|
InitImgFS(NULL);
|
2016-12-10 15:32:03 +01:00
|
|
|
SetupNandSdDrive(NULL, NULL, NULL, 0);
|
|
|
|
SetupNandSdDrive(NULL, NULL, NULL, 1);
|
|
|
|
for (u32 i = NORM_FS - 1; i > 0; i--) {
|
|
|
|
if (fs_mounted[i]) {
|
|
|
|
char fsname[8];
|
2023-03-20 23:13:51 -05:00
|
|
|
snprintf(fsname, sizeof(fsname), "%lu:", i);
|
2016-12-10 15:32:03 +01:00
|
|
|
f_mount(NULL, fsname, 1);
|
|
|
|
fs_mounted[i] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeinitSDCardFS() {
|
2018-03-06 23:35:12 +01:00
|
|
|
DismountDriveType(DRV_SDCARD|DRV_EMUNAND|DRV_ALIAS);
|
2016-12-10 15:32:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DismountDriveType(u32 type) { // careful with this - no safety checks
|
|
|
|
if (type & DriveType(GetMountPath()))
|
|
|
|
InitImgFS(NULL); // image is mounted from type -> unmount image drive, too
|
2016-12-19 14:32:22 +01:00
|
|
|
if (type & DRV_SDCARD) {
|
|
|
|
SetupNandSdDrive(NULL, NULL, NULL, 0);
|
|
|
|
SetupNandSdDrive(NULL, NULL, NULL, 1);
|
|
|
|
}
|
|
|
|
for (u32 i = 0; i < NORM_FS; i++) {
|
2016-12-10 15:32:03 +01:00
|
|
|
char fsname[8];
|
2023-03-20 23:13:51 -05:00
|
|
|
snprintf(fsname, sizeof(fsname), "%lu:", i);
|
2016-12-10 15:32:03 +01:00
|
|
|
if (!fs_mounted[i] || !(type & DriveType(fsname)))
|
|
|
|
continue;
|
|
|
|
f_mount(NULL, fsname, 1);
|
|
|
|
fs_mounted[i] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-19 14:32:22 +01:00
|
|
|
bool CheckSDMountState(void) {
|
|
|
|
return fs_mounted[0] || fs_mounted[4] || fs_mounted[5] || fs_mounted[6];
|
|
|
|
}
|
|
|
|
|
2016-12-10 15:32:03 +01:00
|
|
|
int GetMountedFSNum(const char* path) {
|
|
|
|
char alias[256];
|
|
|
|
dealias_path(alias, path);
|
|
|
|
int fsnum = *alias - (int) '0';
|
|
|
|
if ((fsnum < 0) || (fsnum >= NORM_FS) || (alias[1] != ':') || !fs_mounted[fsnum])
|
|
|
|
return -1;
|
|
|
|
return fsnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
FATFS* GetMountedFSObject(const char* path) {
|
|
|
|
int fsnum = GetMountedFSNum(path);
|
|
|
|
return (fsnum >= 0) ? fs + fsnum : NULL;
|
|
|
|
}
|