diff --git a/CMakeLists.txt b/CMakeLists.txt index 55b8760bb..f3459d1a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -148,7 +148,7 @@ set(PICA_SOURCE_FILES src/core/PICA/gpu.cpp src/core/PICA/regs.cpp src/core/PICA set(LOADER_SOURCE_FILES src/core/loader/elf.cpp src/core/loader/ncsd.cpp src/core/loader/ncch.cpp src/core/loader/3dsx.cpp src/core/loader/lz77.cpp) set(FS_SOURCE_FILES src/core/fs/archive_self_ncch.cpp src/core/fs/archive_save_data.cpp src/core/fs/archive_sdmc.cpp src/core/fs/archive_ext_save_data.cpp src/core/fs/archive_ncch.cpp src/core/fs/romfs.cpp - src/core/fs/ivfc.cpp src/core/fs/archive_user_save_data.cpp + src/core/fs/ivfc.cpp src/core/fs/archive_user_save_data.cpp src/core/fs/archive_system_save_data.cpp ) set(APPLET_SOURCE_FILES src/core/applets/applet.cpp src/core/applets/mii_selector.cpp src/core/applets/software_keyboard.cpp src/core/applets/applet_manager.cpp) @@ -183,6 +183,7 @@ set(HEADER_FILES include/emulator.hpp include/helpers.hpp include/termcolor.hpp include/applets/applet.hpp include/applets/mii_selector.hpp include/math_util.hpp include/services/soc.hpp include/services/news_u.hpp include/applets/software_keyboard.hpp include/applets/applet_manager.hpp include/fs/archive_user_save_data.hpp include/services/amiibo_device.hpp include/services/nfc_types.hpp include/swap.hpp include/services/csnd.hpp include/services/nwm_uds.hpp + include/fs/archive_system_save_data.hpp ) cmrc_add_resource_library( diff --git a/include/fs/archive_system_save_data.hpp b/include/fs/archive_system_save_data.hpp new file mode 100644 index 000000000..fcba5e731 --- /dev/null +++ b/include/fs/archive_system_save_data.hpp @@ -0,0 +1,32 @@ +#pragma once +#include "archive_base.hpp" + +class SystemSaveDataArchive : public ArchiveBase { + public: + SystemSaveDataArchive(Memory& mem) : ArchiveBase(mem) {} + + u64 getFreeBytes() override { + Helpers::panic("Unimplemented GetFreeBytes for SystemSaveData archive"); + return 32_MB; + } + + std::string name() override { return "SystemSaveData"; } + + //HorizonResult createDirectory(const FSPath& path) override; + HorizonResult createFile(const FSPath& path, u64 size) override; + + HorizonResult deleteFile(const FSPath& path) override { + Helpers::panic("Unimplemented DeleteFile for SystemSaveData archive"); + return Result::Success; + }; + + Rust::Result openArchive(const FSPath& path) override; + //Rust::Result openDirectory(const FSPath& path) override; + + FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override; + + std::optional readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override { + Helpers::panic("Unimplemented ReadFile for SystemSaveData archive"); + return {}; + }; +}; \ No newline at end of file diff --git a/include/kernel/handles.hpp b/include/kernel/handles.hpp index 749534b80..9a53eb3d8 100644 --- a/include/kernel/handles.hpp +++ b/include/kernel/handles.hpp @@ -25,7 +25,8 @@ namespace KernelHandles { HID, // HID service (Handles input-related things including gyro. Does NOT handle New3DS controls or CirclePadPro) HTTP, // HTTP service (Handles HTTP requests) IR_USER, // One of 3 infrared communication services - FRD, // Friend service (Miiverse friend service) + FRD_A, // Friend service (Miiverse friend service) + FRD_U, FS, // Filesystem service GPU, // GPU service LCD, // LCD service (Used for configuring the displays) @@ -82,7 +83,8 @@ namespace KernelHandles { case HID: return "HID"; case HTTP: return "HTTP"; case IR_USER: return "IR:USER"; - case FRD: return "FRD"; + case FRD_A: return "FRD:A"; + case FRD_U: return "FRD:U"; case FS: return "FS"; case GPU: return "GSP::GPU"; case LCD: return "GSP::LCD"; diff --git a/include/services/frd.hpp b/include/services/frd.hpp index 65eb7041e..46a6e272d 100644 --- a/include/services/frd.hpp +++ b/include/services/frd.hpp @@ -15,7 +15,6 @@ struct FriendKey { static_assert(sizeof(FriendKey) == 16); class FRDService { - Handle handle = KernelHandles::FRD; Memory& mem; MAKE_LOG_FUNCTION(log, frdLogger) @@ -48,7 +47,13 @@ class FRDService { static_assert(sizeof(Profile) == 8); public: + enum class Type { + A, // frd:a + N, // frd:n + U, // frd:u + }; + FRDService(Memory& mem) : mem(mem) {} void reset(); - void handleSyncRequest(u32 messagePointer); + void handleSyncRequest(u32 messagePointer, Type type); }; \ No newline at end of file diff --git a/include/services/fs.hpp b/include/services/fs.hpp index 2dff3544a..dbaf8a1b2 100644 --- a/include/services/fs.hpp +++ b/include/services/fs.hpp @@ -5,6 +5,7 @@ #include "fs/archive_save_data.hpp" #include "fs/archive_sdmc.hpp" #include "fs/archive_self_ncch.hpp" +#include "fs/archive_system_save_data.hpp" #include "fs/archive_user_save_data.hpp" #include "helpers.hpp" #include "kernel_types.hpp" @@ -33,6 +34,7 @@ class FSService { ExtSaveDataArchive extSaveData_sdmc; ExtSaveDataArchive sharedExtSaveData_nand; + SystemSaveDataArchive systemSaveData; ArchiveBase* getArchiveFromID(u32 id, const FSPath& archivePath); Rust::Result openArchiveHandle(u32 archiveID, const FSPath& path); @@ -77,7 +79,8 @@ class FSService { public: FSService(Memory& mem, Kernel& kernel, const EmulatorConfig& config) : mem(mem), saveData(mem), sharedExtSaveData_nand(mem, "../SharedFiles/NAND", true), extSaveData_sdmc(mem, "SDMC"), sdmc(mem), selfNcch(mem), - ncch(mem), userSaveData1(mem, ArchiveID::UserSaveData1), userSaveData2(mem, ArchiveID::UserSaveData2), kernel(kernel), config(config) {} + ncch(mem), userSaveData1(mem, ArchiveID::UserSaveData1), userSaveData2(mem, ArchiveID::UserSaveData2), kernel(kernel), config(config), + systemSaveData(mem) {} void reset(); void handleSyncRequest(u32 messagePointer); diff --git a/src/core/fs/archive_save_data.cpp b/src/core/fs/archive_save_data.cpp index aa94cdc4f..8ac51b316 100644 --- a/src/core/fs/archive_save_data.cpp +++ b/src/core/fs/archive_save_data.cpp @@ -12,8 +12,9 @@ HorizonResult SaveDataArchive::createFile(const FSPath& path, u64 size) { fs::path p = IOFile::getAppData() / "SaveData"; p += fs::path(path.utf16_string).make_preferred(); - if (fs::exists(p)) + if (fs::exists(p)) { return Result::FS::AlreadyExists; + } IOFile file(p.string().c_str(), "wb"); @@ -33,7 +34,7 @@ HorizonResult SaveDataArchive::createFile(const FSPath& path, u64 size) { return Result::FS::FileTooLarge; } - Helpers::panic("SaveDataArchive::OpenFile: Failed"); + Helpers::panic("SaveDataArchive::CreateFile: Failed"); return Result::Success; } diff --git a/src/core/fs/archive_system_save_data.cpp b/src/core/fs/archive_system_save_data.cpp new file mode 100644 index 000000000..4f63dc631 --- /dev/null +++ b/src/core/fs/archive_system_save_data.cpp @@ -0,0 +1,85 @@ +#include +#include "fs/archive_system_save_data.hpp" + +namespace fs = std::filesystem; + +Rust::Result SystemSaveDataArchive::openArchive(const FSPath& path) { + if (path.type != PathType::Binary) { + Helpers::panic("Unimplemented path type for SystemSaveData::OpenArchive"); + } + + return Ok((ArchiveBase*)this); +} + +FileDescriptor SystemSaveDataArchive::openFile(const FSPath& path, const FilePerms& perms) { + // TODO: Validate this. Temporarily copied from SaveData archive + + if (path.type == PathType::UTF16) { + if (!isPathSafe(path)) { + Helpers::panic("Unsafe path in SystemSaveData::OpenFile"); + } + + if (perms.raw == 0 || (perms.create() && !perms.write())) { + Helpers::panic("[SystemSaveData] Unsupported flags for OpenFile"); + } + + fs::path p = IOFile::getAppData() / ".." / "SharedFiles" / "SystemSaveData"; + p += fs::path(path.utf16_string).make_preferred(); + + const char* permString = perms.write() ? "r+b" : "rb"; + + if (fs::exists(p)) { // Return file descriptor if the file exists + IOFile file(p.string().c_str(), permString); + return file.isOpen() ? file.getHandle() : FileError; + } else { + // If the file is not found, create it if the create flag is on + if (perms.create()) { + IOFile file(p.string().c_str(), "wb"); // Create file + file.close(); // Close it + + file.open(p.string().c_str(), permString); // Reopen with proper perms + return file.isOpen() ? file.getHandle() : FileError; + } else { + return FileError; + } + } + } + + Helpers::panic("SystemSaveData::OpenFile: Failed"); + return FileError; +} + +HorizonResult SystemSaveDataArchive::createFile(const FSPath& path, u64 size) { + if (path.type == PathType::UTF16) { + if (!isPathSafe(path)) { + Helpers::panic("Unsafe path in SystemSaveData::CreateFile"); + } + + fs::path p = IOFile::getAppData() / ".." / "SharedFiles" / "SystemSaveData"; + p += fs::path(path.utf16_string).make_preferred(); + + if (fs::exists(p)) { + return Result::FS::AlreadyExists; + } + + IOFile file(p.string().c_str(), "wb"); + + // If the size is 0, leave the file empty and return success + if (size == 0) { + file.close(); + return Result::Success; + } + + // If it is not empty, seek to size - 1 and write a 0 to create a file of size "size" + else if (file.seek(size - 1, SEEK_SET) && file.writeBytes("", 1).second == 1) { + file.close(); + return Result::Success; + } + + file.close(); + return Result::FS::FileTooLarge; + } + + Helpers::panic("SystemSaveData::CreateFile: Failed"); + return Result::Success; +} \ No newline at end of file diff --git a/src/core/kernel/kernel.cpp b/src/core/kernel/kernel.cpp index dfebfb6db..b21c4c584 100644 --- a/src/core/kernel/kernel.cpp +++ b/src/core/kernel/kernel.cpp @@ -228,6 +228,13 @@ void Kernel::getProcessInfo() { } switch (type) { + // Returns the amount of + total supervisor-mode stack size + page-rounded size of the external handle table + case 1: + Helpers::warn("GetProcessInfo: Unimplemented type 1"); + regs[1] = 0; + regs[2] = 0; + break; + // According to 3DBrew: Amount of private (code, data, heap) memory used by the process + total supervisor-mode // stack size + page-rounded size of the external handle table case 2: diff --git a/src/core/services/frd.cpp b/src/core/services/frd.cpp index d4a439df7..fecb33226 100644 --- a/src/core/services/frd.cpp +++ b/src/core/services/frd.cpp @@ -27,7 +27,7 @@ namespace FRDCommands { void FRDService::reset() { loggedIn = false; } -void FRDService::handleSyncRequest(u32 messagePointer) { +void FRDService::handleSyncRequest(u32 messagePointer, FRDService::Type type) { const u32 command = mem.read32(messagePointer); switch (command) { case FRDCommands::AttachToEventNotification: attachToEventNotification(messagePointer); break; diff --git a/src/core/services/fs.cpp b/src/core/services/fs.cpp index 29646a8e1..0ecc792bd 100644 --- a/src/core/services/fs.cpp +++ b/src/core/services/fs.cpp @@ -53,6 +53,7 @@ void FSService::initializeFilesystem() { const auto savePath = IOFile::getAppData() / "SaveData"; // Create SaveData const auto formatPath = IOFile::getAppData() / "FormatInfo"; // Create folder for storing archive formatting info + const auto systemSaveDataPath = IOFile::getAppData() / ".." / "SharedFiles" / "SystemSaveData"; namespace fs = std::filesystem; @@ -71,6 +72,10 @@ void FSService::initializeFilesystem() { if (!fs::is_directory(formatPath)) { fs::create_directories(formatPath); } + + if (!fs::is_directory(systemSaveDataPath)) { + fs::create_directories(systemSaveDataPath); + } } ArchiveBase* FSService::getArchiveFromID(u32 id, const FSPath& archivePath) { @@ -85,6 +90,7 @@ ArchiveBase* FSService::getArchiveFromID(u32 id, const FSPath& archivePath) { case ArchiveID::SharedExtSaveData: return &sharedExtSaveData_nand; + case ArchiveID::SystemSaveData: return &systemSaveData; case ArchiveID::SDMC: return &sdmc; case ArchiveID::SavedataAndNcch: return &ncch; // This can only access NCCH outside of FSPXI default: diff --git a/src/core/services/service_manager.cpp b/src/core/services/service_manager.cpp index 3696b0d1b..e70a36acb 100644 --- a/src/core/services/service_manager.cpp +++ b/src/core/services/service_manager.cpp @@ -111,7 +111,8 @@ static std::map serviceMap = { { "hid:USER", KernelHandles::HID }, { "http:C", KernelHandles::HTTP }, { "ir:USER", KernelHandles::IR_USER }, - { "frd:u", KernelHandles::FRD }, + { "frd:a", KernelHandles::FRD_A }, + { "frd:u", KernelHandles::FRD_U }, { "fs:USER", KernelHandles::FS }, { "gsp::Gpu", KernelHandles::GPU }, { "gsp::Lcd", KernelHandles::LCD }, @@ -211,7 +212,8 @@ void ServiceManager::sendCommandToService(u32 messagePointer, Handle handle) { case KernelHandles::HID: hid.handleSyncRequest(messagePointer); break; case KernelHandles::HTTP: http.handleSyncRequest(messagePointer); break; case KernelHandles::IR_USER: ir_user.handleSyncRequest(messagePointer); break; - case KernelHandles::FRD: frd.handleSyncRequest(messagePointer); break; + case KernelHandles::FRD_A: frd.handleSyncRequest(messagePointer, FRDService::Type::A); break; + case KernelHandles::FRD_U: frd.handleSyncRequest(messagePointer, FRDService::Type::U); break; case KernelHandles::LCD: gsp_lcd.handleSyncRequest(messagePointer); break; case KernelHandles::LDR_RO: ldr.handleSyncRequest(messagePointer); break; case KernelHandles::MCU_HWC: mcu_hwc.handleSyncRequest(messagePointer); break;