mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-04-22 04:24:48 +00:00
fusee-primary: use reference counting for sd device/filesystem
This commit is contained in:
parent
bd5ec4be4e
commit
5c73dd1453
3 changed files with 83 additions and 34 deletions
|
@ -20,57 +20,98 @@
|
|||
#include "lib/log.h"
|
||||
|
||||
FATFS sd_fs;
|
||||
static bool g_sd_mounted = false;
|
||||
static bool g_sd_initialized = false;
|
||||
static int g_sd_device_rc = 0;
|
||||
static int g_sd_fs_rc = 0;
|
||||
sdmmc_t g_sd_sdmmc;
|
||||
sdmmc_device_t g_sd_device;
|
||||
|
||||
bool acquire_sd_device(void)
|
||||
{
|
||||
/* Already initialized. */
|
||||
if (g_sd_device_rc++ > 0)
|
||||
return true;
|
||||
|
||||
/* Enable AHB redirection if necessary. */
|
||||
mc_acquire_ahb_redirect();
|
||||
|
||||
if (sdmmc_device_sd_init(&g_sd_device, &g_sd_sdmmc, SDMMC_BUS_WIDTH_4BIT, SDMMC_SPEED_UHS_SDR104))
|
||||
return true;
|
||||
|
||||
mc_release_ahb_redirect();
|
||||
|
||||
g_sd_device_rc--;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void release_sd_device(void)
|
||||
{
|
||||
/* No need to finalize. */
|
||||
if (--g_sd_device_rc > 0)
|
||||
return;
|
||||
|
||||
sdmmc_device_finish(&g_sd_device);
|
||||
|
||||
/* Disable AHB redirection if necessary. */
|
||||
mc_release_ahb_redirect();
|
||||
}
|
||||
|
||||
bool mount_sd(void)
|
||||
{
|
||||
/* Already mounted. */
|
||||
if (g_sd_mounted)
|
||||
if (g_sd_fs_rc++ > 0)
|
||||
return true;
|
||||
|
||||
/* Enable AHB redirection if necessary. */
|
||||
mc_acquire_ahb_redirect();
|
||||
|
||||
if (!g_sd_initialized) {
|
||||
/* Initialize SD. */
|
||||
if (sdmmc_device_sd_init(&g_sd_device, &g_sd_sdmmc, SDMMC_BUS_WIDTH_4BIT, SDMMC_SPEED_UHS_SDR104))
|
||||
{
|
||||
g_sd_initialized = true;
|
||||
|
||||
/* Mount SD. */
|
||||
if (f_mount(&sd_fs, "", 1) == FR_OK) {
|
||||
print(SCREEN_LOG_LEVEL_INFO, "Mounted SD card!\n");
|
||||
g_sd_mounted = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
fatal_error("Failed to initialize the SD card!.\n");
|
||||
/* Make sure SD device is initialized. */
|
||||
if (!acquire_sd_device()) {
|
||||
g_sd_fs_rc--;
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Mount SD filesystem. */
|
||||
if (f_mount(&sd_fs, "", 1) == FR_OK) {
|
||||
print(SCREEN_LOG_LEVEL_INFO, "Mounted SD card!\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
return g_sd_mounted;
|
||||
release_sd_device();
|
||||
|
||||
g_sd_fs_rc--;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void unmount_sd(void)
|
||||
{
|
||||
if (g_sd_mounted)
|
||||
{
|
||||
f_mount(NULL, "", 1);
|
||||
sdmmc_device_finish(&g_sd_device);
|
||||
g_sd_mounted = false;
|
||||
}
|
||||
if (--g_sd_fs_rc > 0)
|
||||
return;
|
||||
|
||||
f_mount(NULL, "", 1);
|
||||
|
||||
/* Disable AHB redirection if necessary. */
|
||||
mc_release_ahb_redirect();
|
||||
release_sd_device();
|
||||
}
|
||||
|
||||
void temporary_unmount_sd(bool *was_mounted)
|
||||
{
|
||||
if (g_sd_fs_rc > 0) {
|
||||
f_mount(NULL, "", 1);
|
||||
*was_mounted = true;
|
||||
} else {
|
||||
*was_mounted = false;
|
||||
}
|
||||
}
|
||||
|
||||
void temporary_remount_sd(void)
|
||||
{
|
||||
if (f_mount(&sd_fs, "", 1) != FR_OK) {
|
||||
fatal_error("Failed to remount SD card after temporary operation!\n");
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t get_file_size(const char *filename)
|
||||
{
|
||||
/* SD card hasn't been mounted yet. */
|
||||
if (!g_sd_mounted)
|
||||
if (g_sd_fs_rc == 0)
|
||||
return 0;
|
||||
|
||||
/* Open the file for reading. */
|
||||
|
@ -90,7 +131,7 @@ uint32_t get_file_size(const char *filename)
|
|||
int read_from_file(void *dst, uint32_t dst_size, const char *filename)
|
||||
{
|
||||
/* SD card hasn't been mounted yet. */
|
||||
if (!g_sd_mounted)
|
||||
if (g_sd_fs_rc == 0)
|
||||
return 0;
|
||||
|
||||
/* Open the file for reading. */
|
||||
|
@ -112,7 +153,7 @@ int read_from_file(void *dst, uint32_t dst_size, const char *filename)
|
|||
int write_to_file(void *src, uint32_t src_size, const char *filename)
|
||||
{
|
||||
/* SD card hasn't been mounted yet. */
|
||||
if (!g_sd_mounted)
|
||||
if (g_sd_fs_rc == 0)
|
||||
return 0;
|
||||
|
||||
/* Open the file for writing. */
|
||||
|
@ -129,4 +170,4 @@ int write_to_file(void *src, uint32_t src_size, const char *filename)
|
|||
f_close(&f);
|
||||
|
||||
return (res == FR_OK) ? (int)bw : 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,8 +26,14 @@
|
|||
extern sdmmc_t g_sd_sdmmc;
|
||||
extern sdmmc_device_t g_sd_device;
|
||||
|
||||
bool acquire_sd_device(void);
|
||||
void release_sd_device(void);
|
||||
bool mount_sd(void);
|
||||
void unmount_sd(void);
|
||||
|
||||
void temporary_unmount_sd(bool *was_mounted);
|
||||
void temporary_remount_sd(void);
|
||||
|
||||
uint32_t get_file_size(const char *filename);
|
||||
int read_from_file(void *dst, uint32_t dst_size, const char *filename);
|
||||
int write_to_file(void *src, uint32_t src_size, const char *filename);
|
||||
|
|
|
@ -81,7 +81,9 @@ static void setup_env(void) {
|
|||
setup_exception_handlers();
|
||||
|
||||
/* Mount the SD card. */
|
||||
mount_sd();
|
||||
if (!mount_sd()) {
|
||||
fatal_error("Failed to mount SD card!\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void cleanup_env(void) {
|
||||
|
|
Loading…
Add table
Reference in a new issue