Skip to content

Commit

Permalink
Merge pull request #273 from wheremyfoodat/SystemSaveData
Browse files Browse the repository at this point in the history
Add some SystemSaveData and FRD things
  • Loading branch information
wheremyfoodat authored Sep 13, 2023
2 parents eade124 + 1332488 commit 7fdffd5
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
32 changes: 32 additions & 0 deletions include/fs/archive_system_save_data.hpp
Original file line number Diff line number Diff line change
@@ -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<ArchiveBase*, HorizonResult> openArchive(const FSPath& path) override;
//Rust::Result<DirectorySession, HorizonResult> openDirectory(const FSPath& path) override;

FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override;

std::optional<u32> readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override {
Helpers::panic("Unimplemented ReadFile for SystemSaveData archive");
return {};
};
};
6 changes: 4 additions & 2 deletions include/kernel/handles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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";
Expand Down
9 changes: 7 additions & 2 deletions include/services/frd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ struct FriendKey {
static_assert(sizeof(FriendKey) == 16);

class FRDService {
Handle handle = KernelHandles::FRD;
Memory& mem;
MAKE_LOG_FUNCTION(log, frdLogger)

Expand Down Expand Up @@ -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);
};
5 changes: 4 additions & 1 deletion include/services/fs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -33,6 +34,7 @@ class FSService {

ExtSaveDataArchive extSaveData_sdmc;
ExtSaveDataArchive sharedExtSaveData_nand;
SystemSaveDataArchive systemSaveData;

ArchiveBase* getArchiveFromID(u32 id, const FSPath& archivePath);
Rust::Result<Handle, HorizonResult> openArchiveHandle(u32 archiveID, const FSPath& path);
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/core/fs/archive_save_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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;
}

Expand Down
85 changes: 85 additions & 0 deletions src/core/fs/archive_system_save_data.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <algorithm>
#include "fs/archive_system_save_data.hpp"

namespace fs = std::filesystem;

Rust::Result<ArchiveBase*, HorizonResult> 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<PathType::UTF16>(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<PathType::UTF16>(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;
}
7 changes: 7 additions & 0 deletions src/core/kernel/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ void Kernel::getProcessInfo() {
}

switch (type) {
// Returns the amount of <related unused field> + 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:
Expand Down
2 changes: 1 addition & 1 deletion src/core/services/frd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/core/services/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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) {
Expand All @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions src/core/services/service_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ static std::map<std::string, Handle> 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 },
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7fdffd5

Please sign in to comment.