mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-08-11 18:50:07 +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"
|
#include "lib/log.h"
|
||||||
|
|
||||||
FATFS sd_fs;
|
FATFS sd_fs;
|
||||||
static bool g_sd_mounted = false;
|
static int g_sd_device_rc = 0;
|
||||||
static bool g_sd_initialized = false;
|
static int g_sd_fs_rc = 0;
|
||||||
sdmmc_t g_sd_sdmmc;
|
sdmmc_t g_sd_sdmmc;
|
||||||
sdmmc_device_t g_sd_device;
|
sdmmc_device_t g_sd_device;
|
||||||
|
|
||||||
|
bool acquire_sd_device(void)
|
||||||
bool mount_sd(void)
|
|
||||||
{
|
{
|
||||||
/* Already mounted. */
|
/* Already initialized. */
|
||||||
if (g_sd_mounted)
|
if (g_sd_device_rc++ > 0)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
/* Enable AHB redirection if necessary. */
|
/* Enable AHB redirection if necessary. */
|
||||||
mc_acquire_ahb_redirect();
|
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))
|
if (sdmmc_device_sd_init(&g_sd_device, &g_sd_sdmmc, SDMMC_BUS_WIDTH_4BIT, SDMMC_SPEED_UHS_SDR104))
|
||||||
{
|
return true;
|
||||||
g_sd_initialized = true;
|
|
||||||
|
|
||||||
/* Mount SD. */
|
mc_release_ahb_redirect();
|
||||||
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");
|
|
||||||
}
|
|
||||||
|
|
||||||
return g_sd_mounted;
|
g_sd_device_rc--;
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void unmount_sd(void)
|
void release_sd_device(void)
|
||||||
{
|
{
|
||||||
if (g_sd_mounted)
|
/* No need to finalize. */
|
||||||
{
|
if (--g_sd_device_rc > 0)
|
||||||
f_mount(NULL, "", 1);
|
return;
|
||||||
|
|
||||||
sdmmc_device_finish(&g_sd_device);
|
sdmmc_device_finish(&g_sd_device);
|
||||||
g_sd_mounted = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Disable AHB redirection if necessary. */
|
/* Disable AHB redirection if necessary. */
|
||||||
mc_release_ahb_redirect();
|
mc_release_ahb_redirect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool mount_sd(void)
|
||||||
|
{
|
||||||
|
/* Already mounted. */
|
||||||
|
if (g_sd_fs_rc++ > 0)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
release_sd_device();
|
||||||
|
|
||||||
|
g_sd_fs_rc--;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void unmount_sd(void)
|
||||||
|
{
|
||||||
|
if (--g_sd_fs_rc > 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
f_mount(NULL, "", 1);
|
||||||
|
|
||||||
|
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)
|
uint32_t get_file_size(const char *filename)
|
||||||
{
|
{
|
||||||
/* SD card hasn't been mounted yet. */
|
/* SD card hasn't been mounted yet. */
|
||||||
if (!g_sd_mounted)
|
if (g_sd_fs_rc == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* Open the file for reading. */
|
/* 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)
|
int read_from_file(void *dst, uint32_t dst_size, const char *filename)
|
||||||
{
|
{
|
||||||
/* SD card hasn't been mounted yet. */
|
/* SD card hasn't been mounted yet. */
|
||||||
if (!g_sd_mounted)
|
if (g_sd_fs_rc == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* Open the file for reading. */
|
/* 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)
|
int write_to_file(void *src, uint32_t src_size, const char *filename)
|
||||||
{
|
{
|
||||||
/* SD card hasn't been mounted yet. */
|
/* SD card hasn't been mounted yet. */
|
||||||
if (!g_sd_mounted)
|
if (g_sd_fs_rc == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* Open the file for writing. */
|
/* Open the file for writing. */
|
||||||
|
|
|
@ -26,8 +26,14 @@
|
||||||
extern sdmmc_t g_sd_sdmmc;
|
extern sdmmc_t g_sd_sdmmc;
|
||||||
extern sdmmc_device_t g_sd_device;
|
extern sdmmc_device_t g_sd_device;
|
||||||
|
|
||||||
|
bool acquire_sd_device(void);
|
||||||
|
void release_sd_device(void);
|
||||||
bool mount_sd(void);
|
bool mount_sd(void);
|
||||||
void unmount_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);
|
uint32_t get_file_size(const char *filename);
|
||||||
int read_from_file(void *dst, uint32_t dst_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);
|
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();
|
setup_exception_handlers();
|
||||||
|
|
||||||
/* Mount the SD card. */
|
/* Mount the SD card. */
|
||||||
mount_sd();
|
if (!mount_sd()) {
|
||||||
|
fatal_error("Failed to mount SD card!\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cleanup_env(void) {
|
static void cleanup_env(void) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue