fusee: read from emuSD, if enabled

This commit is contained in:
Christoph Baumann
2025-05-15 13:32:40 +02:00
parent 40bb434a64
commit 6f3f468a5b
24 changed files with 747 additions and 404 deletions

View File

@@ -30,10 +30,12 @@ namespace ams::fs {
constinit bool g_is_sys_mounted = false;
constinit bool g_is_boot1_mounted = false;
constinit bool g_is_boot1_off_mounted = false;
constinit bool g_is_emusd_mounted = false;
alignas(0x10) constinit FATFS g_sd_fs = {};
alignas(0x10) constinit FATFS g_sys_fs = {};
alignas(0x10) constinit FATFS g_boot1_fs = {};
alignas(0x10) constinit FATFS g_emusd_fs = {};
alignas(0x10) constinit FIL g_files[MaxFiles] = {};
alignas(0x10) constinit DIR g_dirs[MaxDirectories] = {};
@@ -122,53 +124,75 @@ namespace ams::fs {
}
bool MountEmuSD() {
if(!g_is_emusd_mounted) {
g_is_emusd_mounted = f_mount(std::addressof(g_emusd_fs), "emusd:", 1) == FR_OK;
}
return g_is_emusd_mounted;
}
void UnmountEmuSD () {
if(g_is_emusd_mounted) {
f_unmount("emusd:");
}
g_is_emusd_mounted = false;
}
bool MountBoot1() {
/* Can't mount both */
AMS_ASSERT(!g_is_boot1_mounted && !g_is_boot1_off_mounted);
g_is_boot1_mounted = f_mount(std::addressof(g_boot1_fs), "boot1:", 1) == FR_OK;
if (!g_is_boot1_mounted && !g_is_boot1_off_mounted) {
g_is_boot1_mounted = f_mount(std::addressof(g_boot1_fs), "boot1:", 1) == FR_OK;
}
return g_is_boot1_mounted;
}
void UnmountBoot1 () {
AMS_ASSERT(g_is_boot1_mounted);
f_unmount("boot1:");
if (g_is_boot1_mounted) {
f_unmount("boot1:");
}
g_is_boot1_mounted = false;
}
bool MountBoot1Off() {
/* Can't mount both */
AMS_ASSERT(!g_is_boot1_mounted && !g_is_boot1_off_mounted);
g_is_boot1_off_mounted = f_mount(std::addressof(g_boot1_fs), "boot1_off:", 1) == FR_OK;
if (!g_is_boot1_mounted && !g_is_boot1_off_mounted) {
g_is_boot1_off_mounted = f_mount(std::addressof(g_boot1_fs), "boot1_off:", 1) == FR_OK;
}
return g_is_boot1_off_mounted;
}
void UnmountBoot1Off () {
AMS_ASSERT(g_is_boot1_off_mounted);
f_unmount("boot1_off:");
if (g_is_boot1_off_mounted) {
f_unmount("boot1_off:");
}
g_is_boot1_off_mounted = false;
}
bool MountSys() {
AMS_ASSERT(!g_is_sys_mounted);
g_is_sys_mounted = f_mount(std::addressof(g_sys_fs), "sys:", 1) == FR_OK;
if (!g_is_sys_mounted) {
g_is_sys_mounted = f_mount(std::addressof(g_sys_fs), "sys:", 1) == FR_OK;
}
return g_is_sys_mounted;
}
void UnmountSys() {
AMS_ASSERT(g_is_sys_mounted);
f_unmount("sys:");
if (g_is_sys_mounted) {
f_unmount("sys:");
}
g_is_sys_mounted = false;
}
bool MountSdCard() {
AMS_ASSERT(!g_is_sd_mounted);
g_is_sd_mounted = f_mount(std::addressof(g_sd_fs), "sdmc:", 1) == FR_OK;
if (!g_is_sd_mounted) {
g_is_sd_mounted = f_mount(std::addressof(g_sd_fs), "sdmc:", 1) == FR_OK;
}
return g_is_sd_mounted;
}
void UnmountSdCard() {
AMS_ASSERT(g_is_sd_mounted);
f_unmount("sdmc:");
if (g_is_sd_mounted) {
f_unmount("sdmc:");
}
g_is_sd_mounted = false;
}

View File

@@ -108,6 +108,9 @@ namespace ams::fs {
bool MountBoot1Off();
void UnmountBoot1Off();
bool MountEmuSD();
void UnmountEmuSD();
Result ChangeDrive(const char *path);
Result ChangeDirectory(const char *path);

View File

@@ -16,6 +16,9 @@
#pragma once
#include <exosphere.hpp>
#include "fusee_fs_api.hpp"
#include "../fusee_mmc.hpp"
#include "../fusee_fatal.hpp"
#include "../fusee_sd_card.hpp"
namespace ams::fs {
@@ -155,4 +158,95 @@ namespace ams::fs {
virtual Result SetSize(s64 size) override;
};
class SdCardStorage : public fs::IStorage {
public:
virtual Result Read(s64 offset, void *buffer, size_t size) override {
if (!util::IsAligned(offset, sdmmc::SectorSize) || !util::IsAligned(size, sdmmc::SectorSize)) {
nxboot::ShowFatalError("SdCard: unaligned access to %" PRIx64 ", size=%" PRIx64"\n", static_cast<u64>(offset), static_cast<u64>(size));
}
R_RETURN(nxboot::ReadSdCard(buffer, size, offset / sdmmc::SectorSize, size / sdmmc::SectorSize));
}
virtual Result Flush() override {
R_SUCCEED();
}
virtual Result GetSize(s64 *out) override {
u32 num_sectors;
R_TRY(nxboot::GetSdCardMemoryCapacity(std::addressof(num_sectors)));
*out = static_cast<s64>(num_sectors) * static_cast<s64>(sdmmc::SectorSize);
R_SUCCEED();
}
virtual Result Write(s64 offset, const void *buffer, size_t size) override {
if (!util::IsAligned(offset, sdmmc::SectorSize) || !util::IsAligned(size, sdmmc::SectorSize)) {
nxboot::ShowFatalError("SdCard: unaligned access to %" PRIx64 ", size=%" PRIx64"\n", static_cast<u64>(offset), static_cast<u64>(size));
}
R_RETURN(nxboot::WriteSdCard(offset / sdmmc::SectorSize, size / sdmmc::SectorSize, buffer, size));
}
virtual Result SetSize(s64 size) override {
R_THROW(fs::ResultUnsupportedOperation());
}
};
template<sdmmc::MmcPartition Partition>
class MmcPartitionStorage : public fs::IStorage {
public:
constexpr MmcPartitionStorage() { /* ... */ }
virtual Result Read(s64 offset, void *buffer, size_t size) override {
if (!util::IsAligned(offset, sdmmc::SectorSize) || !util::IsAligned(size, sdmmc::SectorSize)) {
nxboot::ShowFatalError("SdCard: unaligned access to %" PRIx64 ", size=%" PRIx64"\n", static_cast<u64>(offset), static_cast<u64>(size));
}
R_RETURN(nxboot::ReadMmc(buffer, size, Partition, offset / sdmmc::SectorSize, size / sdmmc::SectorSize));
}
virtual Result Flush() override {
R_SUCCEED();
}
virtual Result GetSize(s64 *out) override {
u32 num_sectors;
R_TRY(nxboot::GetMmcMemoryCapacity(std::addressof(num_sectors), Partition));
*out = static_cast<s64>(num_sectors) * static_cast<s64>(sdmmc::SectorSize);
R_SUCCEED();
}
virtual Result Write(s64 offset, const void *buffer, size_t size) override {
if (!util::IsAligned(offset, sdmmc::SectorSize) || !util::IsAligned(size, sdmmc::SectorSize)) {
nxboot::ShowFatalError("SdCard: unaligned access to %" PRIx64 ", size=%" PRIx64"\n", static_cast<u64>(offset), static_cast<u64>(size));
}
R_RETURN(nxboot::WriteMmc(Partition, offset / sdmmc::SectorSize, size / sdmmc::SectorSize, buffer, size));
}
virtual Result SetSize(s64 size) override {
R_THROW(fs::ResultUnsupportedOperation());
}
};
class MultiFileStorage : public fs::IStorage {
private:
s64 m_file_size;
fs::FileHandle m_handles[64];
bool m_open[64];
int m_file_path_ofs;
char m_base_path[0x300];
private:
void EnsureFile(int id);
public:
MultiFileStorage(const char *base_path);
virtual Result Read(s64 offset, void *buffer, size_t size) override;
virtual Result Flush() override;
virtual Result GetSize(s64 *out) override;
virtual Result Write(s64 offset, const void *buffer, size_t size) override;
virtual Result SetSize(s64 size) override;
};
}

View File

@@ -0,0 +1,112 @@
/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <exosphere.hpp>
#include "fusee_fs_storage.hpp"
namespace ams::fs {
void MultiFileStorage::EnsureFile(int id) {
if (!m_open[id]) {
/* Update path. */
m_base_path[m_file_path_ofs + 1] = '0' + (id % 10);
m_base_path[m_file_path_ofs + 0] = '0' + (id / 10);
/* Open new file. */
const Result result = fs::OpenFile(m_handles + id, m_base_path, fs::OpenMode_Read);
if (R_FAILED(result)) {
nxboot::ShowFatalError("Failed to open part %02d (%s): 0x%08" PRIx32 "!\n", id, m_base_path, result.GetValue());
}
m_open[id] = true;
}
}
MultiFileStorage::MultiFileStorage(const char *base_path) {
util::Strlcpy(m_base_path, base_path, sizeof(m_base_path) - 2);
m_file_path_ofs = strlen(m_base_path);
for (size_t i = 0; i < util::size(m_handles); i++) {
m_open[i] = false;
}
std::memcpy(m_base_path + m_file_path_ofs, "00", 3);
Result r;
if (R_FAILED(r = fs::OpenFile(std::addressof(m_handles[0]), m_base_path, fs::OpenMode_Read))) {
nxboot::ShowFatalError("Failed to open part %02d (%s): 0x%08" PRIx32 "!\n", 0, m_base_path, r.GetValue());
}
m_open[0] = true;
}
Result MultiFileStorage::Read(s64 offset, void *buffer, size_t size) {
int file = offset / m_file_size;
s64 subofs = offset % m_file_size;
u8 *cur_dst = static_cast<u8 *>(buffer);
for (/* ... */; size > 0; ++file) {
/* Ensure the current file is open. */
EnsureFile(file);
/* Perform the current read. */
const size_t cur_size = std::min<size_t>(m_file_size - subofs, size);
R_TRY(fs::ReadFile(m_handles[file], subofs, cur_dst, cur_size));
/* Advance. */
cur_dst += cur_size;
size -= cur_size;
subofs = 0;
}
R_SUCCEED();
}
Result MultiFileStorage::Flush() {
R_THROW(fs::ResultUnsupportedOperation());
}
Result MultiFileStorage::GetSize(s64 *out) {
R_THROW(fs::ResultUnsupportedOperation());
}
Result MultiFileStorage::Write(s64 offset, const void *buffer, size_t size) {
int file = offset / m_file_size;
s64 subofs = offset % m_file_size;
const u8 *cur_src = static_cast<const u8 *>(buffer);
for (/* ... */; size > 0; ++file) {
/* Ensure the current file is open. */
EnsureFile(file);
/* Perform the current read. */
const size_t cur_size = std::min<size_t>(m_file_size - subofs, size);
R_TRY(fs::WriteFile(m_handles[file], subofs, cur_src, cur_size, fs::WriteOption::Flush));
/* Advance. */
cur_src += cur_size;
size -= cur_size;
subofs = 0;
}
R_SUCCEED();
}
Result MultiFileStorage::SetSize(s64 size) {
R_THROW(fs::ResultUnsupportedOperation());
}
}