Rework fs_dev mount code

This commit is contained in:
TuxSH 2018-05-06 18:39:46 +02:00
parent a29e800661
commit 2c07b5a2fb
4 changed files with 39 additions and 44 deletions

View file

@ -75,26 +75,34 @@ typedef struct fsdev_fsdevice_t {
FATFS fatfs; FATFS fatfs;
} fsdev_fsdevice_t; } fsdev_fsdevice_t;
static fsdev_fsdevice_t g_devices[10] = { 0 }; static fsdev_fsdevice_t g_devices[FF_VOLUMES] = { 0 };
const char *VolumeStr[FF_VOLUMES] = { 0 };
/* Restrictions: 10 drives max, and **update FF_VOLUME_STRS accordingly** */ int fsdev_mount_device(const char *name, unsigned int id) {
int fsdev_mount_device(const char *name) { fsdev_fsdevice_t *device = &g_devices[id];
FRESULT rc;
char drname[40];
if (id >= FF_VOLUMES) {
errno = EINVAL;
return -1;
}
if (name[0] == '\0' || strlen(name) > 32) { if (name[0] == '\0' || strlen(name) > 32) {
errno = ENAMETOOLONG; errno = ENAMETOOLONG;
return -1; return -1;
} }
if (FindDevice(name) != -1 || g_devices[id].setup) {
if (FindDevice(name) != -1) {
errno = EEXIST; /* Device already exists */ errno = EEXIST; /* Device already exists */
return -1; return -1;
} }
/* Find a free slot and use it */ strcpy(device->name, name);
for (size_t i = 0; i < 10; i++) {
if (!g_devices[i].setup) { memcpy(&device->devoptab, &g_fsdev_devoptab, sizeof(devoptab_t));
fsdev_fsdevice_t *device = &g_devices[i]; device->devoptab.name = device->name;
FRESULT rc; device->devoptab.deviceData = device;
char drname[40]; VolumeStr[id] = device->name;
strcpy(drname, name); strcpy(drname, name);
strcat(drname, ":"); strcat(drname, ":");
@ -104,24 +112,15 @@ int fsdev_mount_device(const char *name) {
return fsdev_convert_rc(NULL, rc); return fsdev_convert_rc(NULL, rc);
} }
strcpy(device->name, name);
memcpy(&device->devoptab, &g_fsdev_devoptab, sizeof(devoptab_t));
device->devoptab.name = device->name;
device->devoptab.deviceData = device;
if (AddDevice(&device->devoptab) == -1) { if (AddDevice(&device->devoptab) == -1) {
f_unmount(drname);
VolumeStr[id] = NULL;
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
} else { } else {
device->setup = true; device->setup = true;
return 0; return 0;
} }
}
}
errno = ENOMEM;
return -1;
} }
int fsdev_set_default_device(const char *name) { int fsdev_set_default_device(const char *name) {
@ -175,10 +174,11 @@ int fsdev_unmount_device(const char *name) {
} }
int fsdev_mount_all(void) { int fsdev_mount_all(void) {
static const char* const volid[] = { FF_VOLUME_STRS }; /* Change this accordingly: */
static const char* const volumes[] = { "sdmc" };
for (size_t i = 0; i < sizeof(volid)/sizeof(volid[0]); i++) { for (size_t i = 0; i < FF_VOLUMES; i++) {
int ret = fsdev_mount_device(volid[i]); int ret = fsdev_mount_device(volumes[i], i);
if (ret != 0) { if (ret != 0) {
return ret; return ret;
} }
@ -188,10 +188,8 @@ int fsdev_mount_all(void) {
} }
int fsdev_unmount_all(void) { int fsdev_unmount_all(void) {
static const char* const volid[] = { FF_VOLUME_STRS }; for (size_t i = 0; i < FF_VOLUMES; i++) {
int ret = fsdev_unmount_device(VolumeStr[i]);
for (size_t i = 0; i < sizeof(volid)/sizeof(volid[0]); i++) {
int ret = fsdev_unmount_device(volid[i]);
if (ret != 0) { if (ret != 0) {
return ret; return ret;
} }

View file

@ -5,7 +5,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
int fsdev_mount_device(const char *name); int fsdev_mount_device(const char *name, unsigned int id);
int fsdev_set_default_device(const char *name); int fsdev_set_default_device(const char *name);
int fsdev_unmount_device(const char *name); int fsdev_unmount_device(const char *name);

View file

@ -168,7 +168,7 @@
#define FF_STR_VOLUME_ID 1 #define FF_STR_VOLUME_ID 1
#define FF_VOLUME_STRS "sdmc" //#define FF_VOLUME_STRS "sdmc"
/* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings. /* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings.
/ When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive / When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive
/ number in the path name. FF_VOLUME_STRS defines the volume ID strings for each / number in the path name. FF_VOLUME_STRS defines the volume ID strings for each

View file

@ -47,9 +47,6 @@ int main(int argc, void **argv) {
/* TODO: What other hardware init should we do here? */ /* TODO: What other hardware init should we do here? */
/* Setup console/stdout. */
console_resume(args.lfb, args.console_row, args.console_col);
printf(u8"Welcome to Atmosphère Fusée Stage 2!\n"); printf(u8"Welcome to Atmosphère Fusée Stage 2!\n");
printf("Stage 2 executing from: %s\n", (const char *)argv[STAGE2_ARGV_PROGRAM_PATH]); printf("Stage 2 executing from: %s\n", (const char *)argv[STAGE2_ARGV_PROGRAM_PATH]);