summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar bunnei2017-10-12 21:21:49 -0400
committerGravatar bunnei2017-10-12 21:21:49 -0400
commit72b03025ac4ef0d8633c2f3e55b513cd149c59e5 (patch)
treef1fbeb915a0b3df8e4e988a6a562a763e18ea666 /src
parenthle: Remove a large amount of 3ds-specific service code. (diff)
downloadyuzu-72b03025ac4ef0d8633c2f3e55b513cd149c59e5.tar.gz
yuzu-72b03025ac4ef0d8633c2f3e55b513cd149c59e5.tar.xz
yuzu-72b03025ac4ef0d8633c2f3e55b513cd149c59e5.zip
Remove lots more 3DS-specific code.
Diffstat (limited to 'src')
-rw-r--r--src/citra_qt/configuration/configure_system.cpp94
-rw-r--r--src/citra_qt/main.cpp14
-rw-r--r--src/core/file_sys/archive_extsavedata.cpp245
-rw-r--r--src/core/file_sys/archive_extsavedata.h89
-rw-r--r--src/core/file_sys/archive_ncch.cpp114
-rw-r--r--src/core/file_sys/archive_ncch.h34
-rw-r--r--src/core/file_sys/archive_other_savedata.cpp145
-rw-r--r--src/core/file_sys/archive_other_savedata.h52
-rw-r--r--src/core/file_sys/archive_savedata.cpp33
-rw-r--r--src/core/file_sys/archive_savedata.h33
-rw-r--r--src/core/file_sys/archive_sdmc.cpp379
-rw-r--r--src/core/file_sys/archive_sdmc.h66
-rw-r--r--src/core/file_sys/archive_sdmcwriteonly.cpp70
-rw-r--r--src/core/file_sys/archive_sdmcwriteonly.h57
-rw-r--r--src/core/file_sys/archive_selfncch.cpp297
-rw-r--r--src/core/file_sys/archive_selfncch.h54
-rw-r--r--src/core/file_sys/archive_source_sd_savedata.cpp97
-rw-r--r--src/core/file_sys/archive_source_sd_savedata.h32
-rw-r--r--src/core/file_sys/archive_systemsavedata.cpp77
-rw-r--r--src/core/file_sys/archive_systemsavedata.h61
-rw-r--r--src/core/hle/service/cfg/cfg.cpp716
-rw-r--r--src/core/hle/service/cfg/cfg.h369
-rw-r--r--src/core/hle/service/cfg/cfg_i.cpp64
-rw-r--r--src/core/hle/service/cfg/cfg_i.h22
-rw-r--r--src/core/hle/service/cfg/cfg_nor.cpp23
-rw-r--r--src/core/hle/service/cfg/cfg_nor.h22
-rw-r--r--src/core/hle/service/cfg/cfg_s.cpp41
-rw-r--r--src/core/hle/service/cfg/cfg_s.h22
-rw-r--r--src/core/hle/service/cfg/cfg_u.cpp31
-rw-r--r--src/core/hle/service/cfg/cfg_u.h22
-rw-r--r--src/core/hle/service/fs/archive.cpp605
-rw-r--r--src/core/hle/service/fs/archive.h276
-rw-r--r--src/core/hle/service/fs/fs_user.cpp1045
-rw-r--r--src/core/hle/service/fs/fs_user.h23
-rw-r--r--src/core/hle/service/hid/hid.cpp425
-rw-r--r--src/core/hle/service/hid/hid.h266
-rw-r--r--src/core/hle/service/hid/hid_spvr.cpp29
-rw-r--r--src/core/hle/service/hid/hid_spvr.h22
-rw-r--r--src/core/hle/service/hid/hid_user.cpp29
-rw-r--r--src/core/hle/service/hid/hid_user.h28
-rw-r--r--src/core/hle/service/ns/ns.cpp16
-rw-r--r--src/core/hle/service/ns/ns.h16
-rw-r--r--src/core/hle/service/ns/ns_s.cpp34
-rw-r--r--src/core/hle/service/ns/ns_s.h21
-rw-r--r--src/core/loader/3dsx.cpp350
-rw-r--r--src/core/loader/3dsx.h46
-rw-r--r--src/core/loader/loader.cpp31
-rw-r--r--src/core/loader/loader.h4
-rw-r--r--src/core/loader/ncch.cpp263
-rw-r--r--src/core/loader/ncch.h80
50 files changed, 8 insertions, 6976 deletions
diff --git a/src/citra_qt/configuration/configure_system.cpp b/src/citra_qt/configuration/configure_system.cpp
index 88a067c12..d83c2db23 100644
--- a/src/citra_qt/configuration/configure_system.cpp
+++ b/src/citra_qt/configuration/configure_system.cpp
@@ -6,8 +6,6 @@
6#include "citra_qt/configuration/configure_system.h" 6#include "citra_qt/configuration/configure_system.h"
7#include "citra_qt/ui_settings.h" 7#include "citra_qt/ui_settings.h"
8#include "core/core.h" 8#include "core/core.h"
9#include "core/hle/service/cfg/cfg.h"
10#include "core/hle/service/fs/archive.h"
11#include "ui_configure_system.h" 9#include "ui_configure_system.h"
12 10
13static const std::array<int, 12> days_in_month = {{ 11static const std::array<int, 12> days_in_month = {{
@@ -29,100 +27,14 @@ ConfigureSystem::~ConfigureSystem() {}
29 27
30void ConfigureSystem::setConfiguration() { 28void ConfigureSystem::setConfiguration() {
31 enabled = !Core::System::GetInstance().IsPoweredOn(); 29 enabled = !Core::System::GetInstance().IsPoweredOn();
32
33 if (!enabled) {
34 ReadSystemSettings();
35 ui->group_system_settings->setEnabled(false);
36 } else {
37 // This tab is enabled only when game is not running (i.e. all service are not initialized).
38 // Temporarily register archive types and load the config savegame file to memory.
39 Service::FS::RegisterArchiveTypes();
40 ResultCode result = Service::CFG::LoadConfigNANDSaveFile();
41 Service::FS::UnregisterArchiveTypes();
42
43 if (result.IsError()) {
44 ui->label_disable_info->setText(tr("Failed to load system settings data."));
45 ui->group_system_settings->setEnabled(false);
46 enabled = false;
47 return;
48 }
49
50 ReadSystemSettings();
51 ui->label_disable_info->hide();
52 }
53} 30}
54 31
55void ConfigureSystem::ReadSystemSettings() { 32void ConfigureSystem::ReadSystemSettings() {
56 // set username
57 username = Service::CFG::GetUsername();
58 // TODO(wwylele): Use this when we move to Qt 5.5
59 // ui->edit_username->setText(QString::fromStdU16String(username));
60 ui->edit_username->setText(
61 QString::fromUtf16(reinterpret_cast<const ushort*>(username.data())));
62
63 // set birthday
64 std::tie(birthmonth, birthday) = Service::CFG::GetBirthday();
65 ui->combo_birthmonth->setCurrentIndex(birthmonth - 1);
66 updateBirthdayComboBox(
67 birthmonth -
68 1); // explicitly update it because the signal from setCurrentIndex is not reliable
69 ui->combo_birthday->setCurrentIndex(birthday - 1);
70
71 // set system language
72 language_index = Service::CFG::GetSystemLanguage();
73 ui->combo_language->setCurrentIndex(language_index);
74
75 // set sound output mode
76 sound_index = Service::CFG::GetSoundOutputMode();
77 ui->combo_sound->setCurrentIndex(sound_index);
78
79 // set the console id
80 u64 console_id = Service::CFG::GetConsoleUniqueId();
81 ui->label_console_id->setText(
82 tr("Console ID: 0x%1").arg(QString::number(console_id, 16).toUpper()));
83} 33}
84 34
85void ConfigureSystem::applyConfiguration() { 35void ConfigureSystem::applyConfiguration() {
86 if (!enabled) 36 if (!enabled)
87 return; 37 return;
88
89 bool modified = false;
90
91 // apply username
92 // TODO(wwylele): Use this when we move to Qt 5.5
93 // std::u16string new_username = ui->edit_username->text().toStdU16String();
94 std::u16string new_username(
95 reinterpret_cast<const char16_t*>(ui->edit_username->text().utf16()));
96 if (new_username != username) {
97 Service::CFG::SetUsername(new_username);
98 modified = true;
99 }
100
101 // apply birthday
102 int new_birthmonth = ui->combo_birthmonth->currentIndex() + 1;
103 int new_birthday = ui->combo_birthday->currentIndex() + 1;
104 if (birthmonth != new_birthmonth || birthday != new_birthday) {
105 Service::CFG::SetBirthday(new_birthmonth, new_birthday);
106 modified = true;
107 }
108
109 // apply language
110 int new_language = ui->combo_language->currentIndex();
111 if (language_index != new_language) {
112 Service::CFG::SetSystemLanguage(static_cast<Service::CFG::SystemLanguage>(new_language));
113 modified = true;
114 }
115
116 // apply sound
117 int new_sound = ui->combo_sound->currentIndex();
118 if (sound_index != new_sound) {
119 Service::CFG::SetSoundOutputMode(static_cast<Service::CFG::SoundOutputMode>(new_sound));
120 modified = true;
121 }
122
123 // update the config savegame if any item is modified.
124 if (modified)
125 Service::CFG::UpdateConfigNANDSavegame();
126} 38}
127 39
128void ConfigureSystem::updateBirthdayComboBox(int birthmonth_index) { 40void ConfigureSystem::updateBirthdayComboBox(int birthmonth_index) {
@@ -160,10 +72,6 @@ void ConfigureSystem::refreshConsoleID() {
160 QMessageBox::No | QMessageBox::Yes); 72 QMessageBox::No | QMessageBox::Yes);
161 if (reply == QMessageBox::No) 73 if (reply == QMessageBox::No)
162 return; 74 return;
163 u32 random_number; 75 u64 console_id{};
164 u64 console_id;
165 Service::CFG::GenerateConsoleUniqueId(random_number, console_id);
166 Service::CFG::SetConsoleUniqueId(random_number, console_id);
167 Service::CFG::UpdateConfigNANDSavegame();
168 ui->label_console_id->setText("Console ID: 0x" + QString::number(console_id, 16).toUpper()); 76 ui->label_console_id->setText("Console ID: 0x" + QString::number(console_id, 16).toUpper());
169} 77}
diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp
index 8adbcfe86..943aee30d 100644
--- a/src/citra_qt/main.cpp
+++ b/src/citra_qt/main.cpp
@@ -39,7 +39,6 @@
39#include "common/scope_exit.h" 39#include "common/scope_exit.h"
40#include "common/string_util.h" 40#include "common/string_util.h"
41#include "core/core.h" 41#include "core/core.h"
42#include "core/file_sys/archive_source_sd_savedata.h"
43#include "core/gdbstub/gdbstub.h" 42#include "core/gdbstub/gdbstub.h"
44#include "core/loader/loader.h" 43#include "core/loader/loader.h"
45#include "core/settings.h" 44#include "core/settings.h"
@@ -541,18 +540,7 @@ void GMainWindow::OnGameListLoadFile(QString game_path) {
541} 540}
542 541
543void GMainWindow::OnGameListOpenSaveFolder(u64 program_id) { 542void GMainWindow::OnGameListOpenSaveFolder(u64 program_id) {
544 std::string sdmc_dir = FileUtil::GetUserPath(D_SDMC_IDX); 543 UNIMPLEMENTED();
545 std::string path = FileSys::ArchiveSource_SDSaveData::GetSaveDataPathFor(sdmc_dir, program_id);
546 QString qpath = QString::fromStdString(path);
547
548 QDir dir(qpath);
549 if (!dir.exists()) {
550 QMessageBox::critical(this, tr("Error Opening Save Folder"), tr("Folder does not exist!"));
551 return;
552 }
553
554 LOG_INFO(Frontend, "Opening save data path for program_id=%" PRIu64, program_id);
555 QDesktopServices::openUrl(QUrl::fromLocalFile(qpath));
556} 544}
557 545
558void GMainWindow::OnMenuLoadFile() { 546void GMainWindow::OnMenuLoadFile() {
diff --git a/src/core/file_sys/archive_extsavedata.cpp b/src/core/file_sys/archive_extsavedata.cpp
deleted file mode 100644
index 4867c9d17..000000000
--- a/src/core/file_sys/archive_extsavedata.cpp
+++ /dev/null
@@ -1,245 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <memory>
7#include <vector>
8#include "common/common_types.h"
9#include "common/file_util.h"
10#include "common/logging/log.h"
11#include "common/string_util.h"
12#include "core/file_sys/archive_extsavedata.h"
13#include "core/file_sys/disk_archive.h"
14#include "core/file_sys/errors.h"
15#include "core/file_sys/path_parser.h"
16#include "core/file_sys/savedata_archive.h"
17#include "core/hle/service/fs/archive.h"
18
19////////////////////////////////////////////////////////////////////////////////////////////////////
20// FileSys namespace
21
22namespace FileSys {
23
24/**
25 * A modified version of DiskFile for fixed-size file used by ExtSaveData
26 * The file size can't be changed by SetSize or Write.
27 */
28class FixSizeDiskFile : public DiskFile {
29public:
30 FixSizeDiskFile(FileUtil::IOFile&& file, const Mode& mode) : DiskFile(std::move(file), mode) {
31 size = GetSize();
32 }
33
34 bool SetSize(u64 size) const override {
35 return false;
36 }
37
38 ResultVal<size_t> Write(u64 offset, size_t length, bool flush,
39 const u8* buffer) const override {
40 if (offset > size) {
41 return ERR_WRITE_BEYOND_END;
42 } else if (offset == size) {
43 return MakeResult<size_t>(0);
44 }
45
46 if (offset + length > size) {
47 length = size - offset;
48 }
49
50 return DiskFile::Write(offset, length, flush, buffer);
51 }
52
53private:
54 u64 size{};
55};
56
57/**
58 * Archive backend for general extsave data archive type.
59 * The behaviour of ExtSaveDataArchive is almost the same as SaveDataArchive, except for
60 * - file size can't be changed once created (thus creating zero-size file and openning with create
61 * flag are prohibited);
62 * - always open a file with read+write permission.
63 */
64class ExtSaveDataArchive : public SaveDataArchive {
65public:
66 explicit ExtSaveDataArchive(const std::string& mount_point) : SaveDataArchive(mount_point) {}
67
68 std::string GetName() const override {
69 return "ExtSaveDataArchive: " + mount_point;
70 }
71
72 ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path,
73 const Mode& mode) const override {
74 LOG_DEBUG(Service_FS, "called path=%s mode=%01X", path.DebugStr().c_str(), mode.hex);
75
76 const PathParser path_parser(path);
77
78 if (!path_parser.IsValid()) {
79 LOG_ERROR(Service_FS, "Invalid path %s", path.DebugStr().c_str());
80 return ERROR_INVALID_PATH;
81 }
82
83 if (mode.hex == 0) {
84 LOG_ERROR(Service_FS, "Empty open mode");
85 return ERROR_UNSUPPORTED_OPEN_FLAGS;
86 }
87
88 if (mode.create_flag) {
89 LOG_ERROR(Service_FS, "Create flag is not supported");
90 return ERROR_UNSUPPORTED_OPEN_FLAGS;
91 }
92
93 const auto full_path = path_parser.BuildHostPath(mount_point);
94
95 switch (path_parser.GetHostStatus(mount_point)) {
96 case PathParser::InvalidMountPoint:
97 LOG_CRITICAL(Service_FS, "(unreachable) Invalid mount point %s", mount_point.c_str());
98 return ERROR_FILE_NOT_FOUND;
99 case PathParser::PathNotFound:
100 LOG_ERROR(Service_FS, "Path not found %s", full_path.c_str());
101 return ERROR_PATH_NOT_FOUND;
102 case PathParser::FileInPath:
103 case PathParser::DirectoryFound:
104 LOG_ERROR(Service_FS, "Unexpected file or directory in %s", full_path.c_str());
105 return ERROR_UNEXPECTED_FILE_OR_DIRECTORY;
106 case PathParser::NotFound:
107 LOG_ERROR(Service_FS, "%s not found", full_path.c_str());
108 return ERROR_FILE_NOT_FOUND;
109 case PathParser::FileFound:
110 break; // Expected 'success' case
111 }
112
113 FileUtil::IOFile file(full_path, "r+b");
114 if (!file.IsOpen()) {
115 LOG_CRITICAL(Service_FS, "(unreachable) Unknown error opening %s", full_path.c_str());
116 return ERROR_FILE_NOT_FOUND;
117 }
118
119 Mode rwmode;
120 rwmode.write_flag.Assign(1);
121 rwmode.read_flag.Assign(1);
122 auto disk_file = std::make_unique<FixSizeDiskFile>(std::move(file), rwmode);
123 return MakeResult<std::unique_ptr<FileBackend>>(std::move(disk_file));
124 }
125
126 ResultCode CreateFile(const Path& path, u64 size) const override {
127 if (size == 0) {
128 LOG_ERROR(Service_FS, "Zero-size file is not supported");
129 return ERROR_UNSUPPORTED_OPEN_FLAGS;
130 }
131 return SaveDataArchive::CreateFile(path, size);
132 }
133};
134
135std::string GetExtSaveDataPath(const std::string& mount_point, const Path& path) {
136 std::vector<u8> vec_data = path.AsBinary();
137 const u32* data = reinterpret_cast<const u32*>(vec_data.data());
138 u32 save_low = data[1];
139 u32 save_high = data[2];
140 return Common::StringFromFormat("%s%08X/%08X/", mount_point.c_str(), save_high, save_low);
141}
142
143std::string GetExtDataContainerPath(const std::string& mount_point, bool shared) {
144 if (shared)
145 return Common::StringFromFormat("%sdata/%s/extdata/", mount_point.c_str(), SYSTEM_ID);
146
147 return Common::StringFromFormat("%sNintendo 3DS/%s/%s/extdata/", mount_point.c_str(), SYSTEM_ID,
148 SDCARD_ID);
149}
150
151Path ConstructExtDataBinaryPath(u32 media_type, u32 high, u32 low) {
152 std::vector<u8> binary_path;
153 binary_path.reserve(12);
154
155 // Append each word byte by byte
156
157 // The first word is the media type
158 for (unsigned i = 0; i < 4; ++i)
159 binary_path.push_back((media_type >> (8 * i)) & 0xFF);
160
161 // Next is the low word
162 for (unsigned i = 0; i < 4; ++i)
163 binary_path.push_back((low >> (8 * i)) & 0xFF);
164
165 // Next is the high word
166 for (unsigned i = 0; i < 4; ++i)
167 binary_path.push_back((high >> (8 * i)) & 0xFF);
168
169 return {binary_path};
170}
171
172ArchiveFactory_ExtSaveData::ArchiveFactory_ExtSaveData(const std::string& mount_location,
173 bool shared)
174 : shared(shared), mount_point(GetExtDataContainerPath(mount_location, shared)) {
175 LOG_DEBUG(Service_FS, "Directory %s set as base for ExtSaveData.", mount_point.c_str());
176}
177
178bool ArchiveFactory_ExtSaveData::Initialize() {
179 if (!FileUtil::CreateFullPath(mount_point)) {
180 LOG_ERROR(Service_FS, "Unable to create ExtSaveData base path.");
181 return false;
182 }
183
184 return true;
185}
186
187ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_ExtSaveData::Open(const Path& path) {
188 std::string fullpath = GetExtSaveDataPath(mount_point, path) + "user/";
189 if (!FileUtil::Exists(fullpath)) {
190 // TODO(Subv): Verify the archive behavior of SharedExtSaveData compared to ExtSaveData.
191 // ExtSaveData seems to return FS_NotFound (120) when the archive doesn't exist.
192 if (!shared) {
193 return ERR_NOT_FOUND_INVALID_STATE;
194 } else {
195 return ERR_NOT_FORMATTED;
196 }
197 }
198 auto archive = std::make_unique<ExtSaveDataArchive>(fullpath);
199 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
200}
201
202ResultCode ArchiveFactory_ExtSaveData::Format(const Path& path,
203 const FileSys::ArchiveFormatInfo& format_info) {
204 // These folders are always created with the ExtSaveData
205 std::string user_path = GetExtSaveDataPath(mount_point, path) + "user/";
206 std::string boss_path = GetExtSaveDataPath(mount_point, path) + "boss/";
207 FileUtil::CreateFullPath(user_path);
208 FileUtil::CreateFullPath(boss_path);
209
210 // Write the format metadata
211 std::string metadata_path = GetExtSaveDataPath(mount_point, path) + "metadata";
212 FileUtil::IOFile file(metadata_path, "wb");
213
214 if (!file.IsOpen()) {
215 // TODO(Subv): Find the correct error code
216 return ResultCode(-1);
217 }
218
219 file.WriteBytes(&format_info, sizeof(format_info));
220 return RESULT_SUCCESS;
221}
222
223ResultVal<ArchiveFormatInfo> ArchiveFactory_ExtSaveData::GetFormatInfo(const Path& path) const {
224 std::string metadata_path = GetExtSaveDataPath(mount_point, path) + "metadata";
225 FileUtil::IOFile file(metadata_path, "rb");
226
227 if (!file.IsOpen()) {
228 LOG_ERROR(Service_FS, "Could not open metadata information for archive");
229 // TODO(Subv): Verify error code
230 return ERR_NOT_FORMATTED;
231 }
232
233 ArchiveFormatInfo info = {};
234 file.ReadBytes(&info, sizeof(info));
235 return MakeResult<ArchiveFormatInfo>(info);
236}
237
238void ArchiveFactory_ExtSaveData::WriteIcon(const Path& path, const u8* icon_data,
239 size_t icon_size) {
240 std::string game_path = FileSys::GetExtSaveDataPath(GetMountPoint(), path);
241 FileUtil::IOFile icon_file(game_path + "icon", "wb");
242 icon_file.WriteBytes(icon_data, icon_size);
243}
244
245} // namespace FileSys
diff --git a/src/core/file_sys/archive_extsavedata.h b/src/core/file_sys/archive_extsavedata.h
deleted file mode 100644
index f705ade1c..000000000
--- a/src/core/file_sys/archive_extsavedata.h
+++ /dev/null
@@ -1,89 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <memory>
8#include <string>
9#include "common/common_types.h"
10#include "core/file_sys/archive_backend.h"
11#include "core/hle/result.h"
12
13////////////////////////////////////////////////////////////////////////////////////////////////////
14// FileSys namespace
15
16namespace FileSys {
17
18/// File system interface to the ExtSaveData archive
19class ArchiveFactory_ExtSaveData final : public ArchiveFactory {
20public:
21 ArchiveFactory_ExtSaveData(const std::string& mount_point, bool shared);
22
23 /**
24 * Initialize the archive.
25 * @return true if it initialized successfully
26 */
27 bool Initialize();
28
29 std::string GetName() const override {
30 return "ExtSaveData";
31 }
32
33 ResultVal<std::unique_ptr<ArchiveBackend>> Open(const Path& path) override;
34 ResultCode Format(const Path& path, const FileSys::ArchiveFormatInfo& format_info) override;
35 ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override;
36
37 const std::string& GetMountPoint() const {
38 return mount_point;
39 }
40
41 /**
42 * Writes the SMDH icon of the ExtSaveData to file
43 * @param path Path of this ExtSaveData
44 * @param icon_data Binary data of the icon
45 * @param icon_size Size of the icon data
46 */
47 void WriteIcon(const Path& path, const u8* icon_data, size_t icon_size);
48
49private:
50 bool shared; ///< Whether this archive represents an ExtSaveData archive or a SharedExtSaveData
51 /// archive
52
53 /**
54 * This holds the full directory path for this archive, it is only set after a successful call
55 * to Open, this is formed as `<base extsavedatapath>/<type>/<high>/<low>`.
56 * See GetExtSaveDataPath for the code that extracts this data from an archive path.
57 */
58 std::string mount_point;
59};
60
61/**
62 * Constructs a path to the concrete ExtData archive in the host filesystem based on the
63 * input Path and base mount point.
64 * @param mount_point The base mount point of the ExtSaveData archives.
65 * @param path The path that identifies the requested concrete ExtSaveData archive.
66 * @returns The complete path to the specified extdata archive in the host filesystem
67 */
68std::string GetExtSaveDataPath(const std::string& mount_point, const Path& path);
69
70/**
71 * Constructs a path to the base folder to hold concrete ExtSaveData archives in the host file
72 * system.
73 * @param mount_point The base folder where this folder resides, ie. SDMC or NAND.
74 * @param shared Whether this ExtSaveData container is for SharedExtSaveDatas or not.
75 * @returns The path to the base ExtSaveData archives' folder in the host file system
76 */
77std::string GetExtDataContainerPath(const std::string& mount_point, bool shared);
78
79/**
80 * Constructs a FileSys::Path object that refers to the ExtData archive identified by
81 * the specified media type, high save id and low save id.
82 * @param media_type The media type where the archive is located (NAND / SDMC)
83 * @param high The high word of the save id for the archive
84 * @param low The low word of the save id for the archive
85 * @returns A FileSys::Path to the wanted archive
86 */
87Path ConstructExtDataBinaryPath(u32 media_type, u32 high, u32 low);
88
89} // namespace FileSys
diff --git a/src/core/file_sys/archive_ncch.cpp b/src/core/file_sys/archive_ncch.cpp
deleted file mode 100644
index e8c5be983..000000000
--- a/src/core/file_sys/archive_ncch.cpp
+++ /dev/null
@@ -1,114 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <memory>
7#include <vector>
8#include "common/common_types.h"
9#include "common/file_util.h"
10#include "common/logging/log.h"
11#include "common/string_util.h"
12#include "core/core.h"
13#include "core/file_sys/archive_ncch.h"
14#include "core/file_sys/errors.h"
15#include "core/file_sys/ivfc_archive.h"
16#include "core/file_sys/ncch_container.h"
17#include "core/file_sys/title_metadata.h"
18#include "core/hle/service/fs/archive.h"
19#include "core/loader/loader.h"
20
21////////////////////////////////////////////////////////////////////////////////////////////////////
22// FileSys namespace
23
24namespace FileSys {
25
26static std::string GetNCCHContainerPath(const std::string& nand_directory) {
27 return Common::StringFromFormat("%s%s/title/", nand_directory.c_str(), SYSTEM_ID);
28}
29
30static std::string GetNCCHPath(const std::string& mount_point, u32 high, u32 low) {
31 u32 content_id = 0;
32
33 // TODO(shinyquagsire23): Title database should be doing this path lookup
34 std::string content_path =
35 Common::StringFromFormat("%s%08x/%08x/content/", mount_point.c_str(), high, low);
36 std::string tmd_path = content_path + "00000000.tmd";
37 TitleMetadata tmd(tmd_path);
38 if (tmd.Load() == Loader::ResultStatus::Success) {
39 content_id = tmd.GetBootContentID();
40 }
41
42 return Common::StringFromFormat("%s%08x.app", content_path.c_str(), content_id);
43}
44
45ArchiveFactory_NCCH::ArchiveFactory_NCCH(const std::string& nand_directory)
46 : mount_point(GetNCCHContainerPath(nand_directory)) {}
47
48ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_NCCH::Open(const Path& path) {
49 auto vec = path.AsBinary();
50 const u32* data = reinterpret_cast<u32*>(vec.data());
51 u32 high = data[1];
52 u32 low = data[0];
53 std::string file_path = GetNCCHPath(mount_point, high, low);
54
55 std::shared_ptr<FileUtil::IOFile> romfs_file;
56 u64 romfs_offset = 0;
57 u64 romfs_size = 0;
58 auto ncch_container = NCCHContainer(file_path);
59
60 if (ncch_container.ReadRomFS(romfs_file, romfs_offset, romfs_size) !=
61 Loader::ResultStatus::Success) {
62 // High Title ID of the archive: The category (https://3dbrew.org/wiki/Title_list).
63 constexpr u32 shared_data_archive = 0x0004009B;
64 constexpr u32 system_data_archive = 0x000400DB;
65
66 // Low Title IDs.
67 constexpr u32 mii_data = 0x00010202;
68 constexpr u32 region_manifest = 0x00010402;
69 constexpr u32 ng_word_list = 0x00010302;
70
71 LOG_DEBUG(Service_FS, "Full Path: %s. Category: 0x%X. Path: 0x%X.", path.DebugStr().c_str(),
72 high, low);
73
74 if (high == shared_data_archive) {
75 if (low == mii_data) {
76 LOG_ERROR(Service_FS, "Failed to get a handle for shared data archive: Mii data. ");
77 Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSystemFiles,
78 "Mii data");
79 } else if (low == region_manifest) {
80 LOG_ERROR(Service_FS,
81 "Failed to get a handle for shared data archive: region manifest.");
82 Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSystemFiles,
83 "Region manifest");
84 }
85 } else if (high == system_data_archive) {
86 if (low == ng_word_list) {
87 LOG_ERROR(Service_FS,
88 "Failed to get a handle for system data archive: NG bad word list.");
89 Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSystemFiles,
90 "NG bad word list");
91 }
92 }
93 return ERROR_NOT_FOUND;
94 }
95
96 auto archive = std::make_unique<IVFCArchive>(romfs_file, romfs_offset, romfs_size);
97 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
98}
99
100ResultCode ArchiveFactory_NCCH::Format(const Path& path,
101 const FileSys::ArchiveFormatInfo& format_info) {
102 LOG_ERROR(Service_FS, "Attempted to format a NCCH archive.");
103 // TODO: Verify error code
104 return ResultCode(ErrorDescription::NotAuthorized, ErrorModule::FS, ErrorSummary::NotSupported,
105 ErrorLevel::Permanent);
106}
107
108ResultVal<ArchiveFormatInfo> ArchiveFactory_NCCH::GetFormatInfo(const Path& path) const {
109 // TODO(Subv): Implement
110 LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive %s", GetName().c_str());
111 return ResultCode(-1);
112}
113
114} // namespace FileSys
diff --git a/src/core/file_sys/archive_ncch.h b/src/core/file_sys/archive_ncch.h
deleted file mode 100644
index 753b91f96..000000000
--- a/src/core/file_sys/archive_ncch.h
+++ /dev/null
@@ -1,34 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <memory>
8#include <string>
9#include "core/file_sys/archive_backend.h"
10#include "core/hle/result.h"
11
12////////////////////////////////////////////////////////////////////////////////////////////////////
13// FileSys namespace
14
15namespace FileSys {
16
17/// File system interface to the NCCH archive
18class ArchiveFactory_NCCH final : public ArchiveFactory {
19public:
20 explicit ArchiveFactory_NCCH(const std::string& mount_point);
21
22 std::string GetName() const override {
23 return "NCCH";
24 }
25
26 ResultVal<std::unique_ptr<ArchiveBackend>> Open(const Path& path) override;
27 ResultCode Format(const Path& path, const FileSys::ArchiveFormatInfo& format_info) override;
28 ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override;
29
30private:
31 std::string mount_point;
32};
33
34} // namespace FileSys
diff --git a/src/core/file_sys/archive_other_savedata.cpp b/src/core/file_sys/archive_other_savedata.cpp
deleted file mode 100644
index d3cf080da..000000000
--- a/src/core/file_sys/archive_other_savedata.cpp
+++ /dev/null
@@ -1,145 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <tuple>
6#include "core/file_sys/archive_other_savedata.h"
7#include "core/file_sys/errors.h"
8#include "core/hle/kernel/process.h"
9#include "core/hle/service/fs/archive.h"
10
11////////////////////////////////////////////////////////////////////////////////////////////////////
12// FileSys namespace
13
14namespace FileSys {
15
16// TODO(wwylele): The storage info in exheader should be checked before accessing these archives
17
18using Service::FS::MediaType;
19
20namespace {
21
22template <typename T>
23ResultVal<std::tuple<MediaType, u64>> ParsePath(const Path& path, T program_id_reader) {
24 if (path.GetType() != Binary) {
25 LOG_ERROR(Service_FS, "Wrong path type %d", static_cast<int>(path.GetType()));
26 return ERROR_INVALID_PATH;
27 }
28
29 std::vector<u8> vec_data = path.AsBinary();
30
31 if (vec_data.size() != 12) {
32 LOG_ERROR(Service_FS, "Wrong path length %zu", vec_data.size());
33 return ERROR_INVALID_PATH;
34 }
35
36 const u32* data = reinterpret_cast<const u32*>(vec_data.data());
37 auto media_type = static_cast<MediaType>(data[0]);
38
39 if (media_type != MediaType::SDMC && media_type != MediaType::GameCard) {
40 LOG_ERROR(Service_FS, "Unsupported media type %u", static_cast<u32>(media_type));
41
42 // Note: this is strange, but the error code was verified with a real 3DS
43 return ERROR_UNSUPPORTED_OPEN_FLAGS;
44 }
45
46 return MakeResult<std::tuple<MediaType, u64>>(media_type, program_id_reader(data));
47}
48
49ResultVal<std::tuple<MediaType, u64>> ParsePathPermitted(const Path& path) {
50 return ParsePath(path,
51 [](const u32* data) -> u64 { return (data[1] << 8) | 0x0004000000000000ULL; });
52}
53
54ResultVal<std::tuple<MediaType, u64>> ParsePathGeneral(const Path& path) {
55 return ParsePath(
56 path, [](const u32* data) -> u64 { return data[1] | (static_cast<u64>(data[2]) << 32); });
57}
58
59} // namespace
60
61ArchiveFactory_OtherSaveDataPermitted::ArchiveFactory_OtherSaveDataPermitted(
62 std::shared_ptr<ArchiveSource_SDSaveData> sd_savedata)
63 : sd_savedata_source(sd_savedata) {}
64
65ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_OtherSaveDataPermitted::Open(
66 const Path& path) {
67 MediaType media_type;
68 u64 program_id;
69 CASCADE_RESULT(std::tie(media_type, program_id), ParsePathPermitted(path));
70
71 if (media_type == MediaType::GameCard) {
72 LOG_WARNING(Service_FS, "(stubbed) Unimplemented media type GameCard");
73 return ERROR_GAMECARD_NOT_INSERTED;
74 }
75
76 return sd_savedata_source->Open(program_id);
77}
78
79ResultCode ArchiveFactory_OtherSaveDataPermitted::Format(
80 const Path& path, const FileSys::ArchiveFormatInfo& format_info) {
81 LOG_ERROR(Service_FS, "Attempted to format a OtherSaveDataPermitted archive.");
82 return ERROR_INVALID_PATH;
83}
84
85ResultVal<ArchiveFormatInfo> ArchiveFactory_OtherSaveDataPermitted::GetFormatInfo(
86 const Path& path) const {
87 MediaType media_type;
88 u64 program_id;
89 CASCADE_RESULT(std::tie(media_type, program_id), ParsePathPermitted(path));
90
91 if (media_type == MediaType::GameCard) {
92 LOG_WARNING(Service_FS, "(stubbed) Unimplemented media type GameCard");
93 return ERROR_GAMECARD_NOT_INSERTED;
94 }
95
96 return sd_savedata_source->GetFormatInfo(program_id);
97}
98
99ArchiveFactory_OtherSaveDataGeneral::ArchiveFactory_OtherSaveDataGeneral(
100 std::shared_ptr<ArchiveSource_SDSaveData> sd_savedata)
101 : sd_savedata_source(sd_savedata) {}
102
103ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_OtherSaveDataGeneral::Open(
104 const Path& path) {
105 MediaType media_type;
106 u64 program_id;
107 CASCADE_RESULT(std::tie(media_type, program_id), ParsePathGeneral(path));
108
109 if (media_type == MediaType::GameCard) {
110 LOG_WARNING(Service_FS, "(stubbed) Unimplemented media type GameCard");
111 return ERROR_GAMECARD_NOT_INSERTED;
112 }
113
114 return sd_savedata_source->Open(program_id);
115}
116
117ResultCode ArchiveFactory_OtherSaveDataGeneral::Format(
118 const Path& path, const FileSys::ArchiveFormatInfo& format_info) {
119 MediaType media_type;
120 u64 program_id;
121 CASCADE_RESULT(std::tie(media_type, program_id), ParsePathGeneral(path));
122
123 if (media_type == MediaType::GameCard) {
124 LOG_WARNING(Service_FS, "(stubbed) Unimplemented media type GameCard");
125 return ERROR_GAMECARD_NOT_INSERTED;
126 }
127
128 return sd_savedata_source->Format(program_id, format_info);
129}
130
131ResultVal<ArchiveFormatInfo> ArchiveFactory_OtherSaveDataGeneral::GetFormatInfo(
132 const Path& path) const {
133 MediaType media_type;
134 u64 program_id;
135 CASCADE_RESULT(std::tie(media_type, program_id), ParsePathGeneral(path));
136
137 if (media_type == MediaType::GameCard) {
138 LOG_WARNING(Service_FS, "(stubbed) Unimplemented media type GameCard");
139 return ERROR_GAMECARD_NOT_INSERTED;
140 }
141
142 return sd_savedata_source->GetFormatInfo(program_id);
143}
144
145} // namespace FileSys
diff --git a/src/core/file_sys/archive_other_savedata.h b/src/core/file_sys/archive_other_savedata.h
deleted file mode 100644
index d80725158..000000000
--- a/src/core/file_sys/archive_other_savedata.h
+++ /dev/null
@@ -1,52 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "core/file_sys/archive_source_sd_savedata.h"
8
9////////////////////////////////////////////////////////////////////////////////////////////////////
10// FileSys namespace
11
12namespace FileSys {
13
14/// File system interface to the OtherSaveDataPermitted archive
15class ArchiveFactory_OtherSaveDataPermitted final : public ArchiveFactory {
16public:
17 explicit ArchiveFactory_OtherSaveDataPermitted(
18 std::shared_ptr<ArchiveSource_SDSaveData> sd_savedata_source);
19
20 std::string GetName() const override {
21 return "OtherSaveDataPermitted";
22 }
23
24 ResultVal<std::unique_ptr<ArchiveBackend>> Open(const Path& path) override;
25 ResultCode Format(const Path& path, const FileSys::ArchiveFormatInfo& format_info) override;
26 ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override;
27
28private:
29 std::string mount_point;
30 std::shared_ptr<ArchiveSource_SDSaveData> sd_savedata_source;
31};
32
33/// File system interface to the OtherSaveDataGeneral archive
34class ArchiveFactory_OtherSaveDataGeneral final : public ArchiveFactory {
35public:
36 explicit ArchiveFactory_OtherSaveDataGeneral(
37 std::shared_ptr<ArchiveSource_SDSaveData> sd_savedata_source);
38
39 std::string GetName() const override {
40 return "OtherSaveDataGeneral";
41 }
42
43 ResultVal<std::unique_ptr<ArchiveBackend>> Open(const Path& path) override;
44 ResultCode Format(const Path& path, const FileSys::ArchiveFormatInfo& format_info) override;
45 ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override;
46
47private:
48 std::string mount_point;
49 std::shared_ptr<ArchiveSource_SDSaveData> sd_savedata_source;
50};
51
52} // namespace FileSys
diff --git a/src/core/file_sys/archive_savedata.cpp b/src/core/file_sys/archive_savedata.cpp
deleted file mode 100644
index 67076c73f..000000000
--- a/src/core/file_sys/archive_savedata.cpp
+++ /dev/null
@@ -1,33 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "core/file_sys/archive_savedata.h"
6#include "core/hle/kernel/process.h"
7
8////////////////////////////////////////////////////////////////////////////////////////////////////
9// FileSys namespace
10
11namespace FileSys {
12
13ArchiveFactory_SaveData::ArchiveFactory_SaveData(
14 std::shared_ptr<ArchiveSource_SDSaveData> sd_savedata)
15 : sd_savedata_source(sd_savedata) {}
16
17ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SaveData::Open(const Path& path) {
18 UNIMPLEMENTED();
19 return {}; //sd_savedata_source->Open(Kernel::g_current_process->codeset->program_id);
20}
21
22ResultCode ArchiveFactory_SaveData::Format(const Path& path,
23 const FileSys::ArchiveFormatInfo& format_info) {
24 UNIMPLEMENTED();
25 return RESULT_SUCCESS; //sd_savedata_source->Format(Kernel::g_current_process->codeset->program_id, format_info);
26}
27
28ResultVal<ArchiveFormatInfo> ArchiveFactory_SaveData::GetFormatInfo(const Path& path) const {
29 UNIMPLEMENTED();
30 return {}; //sd_savedata_source->GetFormatInfo(Kernel::g_current_process->codeset->program_id);
31}
32
33} // namespace FileSys
diff --git a/src/core/file_sys/archive_savedata.h b/src/core/file_sys/archive_savedata.h
deleted file mode 100644
index 41aa6f189..000000000
--- a/src/core/file_sys/archive_savedata.h
+++ /dev/null
@@ -1,33 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "core/file_sys/archive_source_sd_savedata.h"
8
9////////////////////////////////////////////////////////////////////////////////////////////////////
10// FileSys namespace
11
12namespace FileSys {
13
14/// File system interface to the SaveData archive
15class ArchiveFactory_SaveData final : public ArchiveFactory {
16public:
17 explicit ArchiveFactory_SaveData(std::shared_ptr<ArchiveSource_SDSaveData> sd_savedata_source);
18
19 std::string GetName() const override {
20 return "SaveData";
21 }
22
23 ResultVal<std::unique_ptr<ArchiveBackend>> Open(const Path& path) override;
24 ResultCode Format(const Path& path, const FileSys::ArchiveFormatInfo& format_info) override;
25
26 ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override;
27
28private:
29 std::string mount_point;
30 std::shared_ptr<ArchiveSource_SDSaveData> sd_savedata_source;
31};
32
33} // namespace FileSys
diff --git a/src/core/file_sys/archive_sdmc.cpp b/src/core/file_sys/archive_sdmc.cpp
deleted file mode 100644
index fe3dce5d4..000000000
--- a/src/core/file_sys/archive_sdmc.cpp
+++ /dev/null
@@ -1,379 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <memory>
7#include "common/file_util.h"
8#include "common/logging/log.h"
9#include "core/file_sys/archive_sdmc.h"
10#include "core/file_sys/disk_archive.h"
11#include "core/file_sys/errors.h"
12#include "core/file_sys/path_parser.h"
13#include "core/settings.h"
14
15////////////////////////////////////////////////////////////////////////////////////////////////////
16// FileSys namespace
17
18namespace FileSys {
19
20ResultVal<std::unique_ptr<FileBackend>> SDMCArchive::OpenFile(const Path& path,
21 const Mode& mode) const {
22 Mode modified_mode;
23 modified_mode.hex = mode.hex;
24
25 // SDMC archive always opens a file with at least read permission
26 modified_mode.read_flag.Assign(1);
27
28 return OpenFileBase(path, modified_mode);
29}
30
31ResultVal<std::unique_ptr<FileBackend>> SDMCArchive::OpenFileBase(const Path& path,
32 const Mode& mode) const {
33 LOG_DEBUG(Service_FS, "called path=%s mode=%01X", path.DebugStr().c_str(), mode.hex);
34
35 const PathParser path_parser(path);
36
37 if (!path_parser.IsValid()) {
38 LOG_ERROR(Service_FS, "Invalid path %s", path.DebugStr().c_str());
39 return ERROR_INVALID_PATH;
40 }
41
42 if (mode.hex == 0) {
43 LOG_ERROR(Service_FS, "Empty open mode");
44 return ERROR_INVALID_OPEN_FLAGS;
45 }
46
47 if (mode.create_flag && !mode.write_flag) {
48 LOG_ERROR(Service_FS, "Create flag set but write flag not set");
49 return ERROR_INVALID_OPEN_FLAGS;
50 }
51
52 const auto full_path = path_parser.BuildHostPath(mount_point);
53
54 switch (path_parser.GetHostStatus(mount_point)) {
55 case PathParser::InvalidMountPoint:
56 LOG_CRITICAL(Service_FS, "(unreachable) Invalid mount point %s", mount_point.c_str());
57 return ERROR_NOT_FOUND;
58 case PathParser::PathNotFound:
59 case PathParser::FileInPath:
60 LOG_ERROR(Service_FS, "Path not found %s", full_path.c_str());
61 return ERROR_NOT_FOUND;
62 case PathParser::DirectoryFound:
63 LOG_ERROR(Service_FS, "%s is not a file", full_path.c_str());
64 return ERROR_UNEXPECTED_FILE_OR_DIRECTORY_SDMC;
65 case PathParser::NotFound:
66 if (!mode.create_flag) {
67 LOG_ERROR(Service_FS, "Non-existing file %s can't be open without mode create.",
68 full_path.c_str());
69 return ERROR_NOT_FOUND;
70 } else {
71 // Create the file
72 FileUtil::CreateEmptyFile(full_path);
73 }
74 break;
75 case PathParser::FileFound:
76 break; // Expected 'success' case
77 }
78
79 FileUtil::IOFile file(full_path, mode.write_flag ? "r+b" : "rb");
80 if (!file.IsOpen()) {
81 LOG_CRITICAL(Service_FS, "(unreachable) Unknown error opening %s", full_path.c_str());
82 return ERROR_NOT_FOUND;
83 }
84
85 auto disk_file = std::make_unique<DiskFile>(std::move(file), mode);
86 return MakeResult<std::unique_ptr<FileBackend>>(std::move(disk_file));
87}
88
89ResultCode SDMCArchive::DeleteFile(const Path& path) const {
90 const PathParser path_parser(path);
91
92 if (!path_parser.IsValid()) {
93 LOG_ERROR(Service_FS, "Invalid path %s", path.DebugStr().c_str());
94 return ERROR_INVALID_PATH;
95 }
96
97 const auto full_path = path_parser.BuildHostPath(mount_point);
98
99 switch (path_parser.GetHostStatus(mount_point)) {
100 case PathParser::InvalidMountPoint:
101 LOG_CRITICAL(Service_FS, "(unreachable) Invalid mount point %s", mount_point.c_str());
102 return ERROR_NOT_FOUND;
103 case PathParser::PathNotFound:
104 case PathParser::FileInPath:
105 case PathParser::NotFound:
106 LOG_ERROR(Service_FS, "%s not found", full_path.c_str());
107 return ERROR_NOT_FOUND;
108 case PathParser::DirectoryFound:
109 LOG_ERROR(Service_FS, "%s is not a file", full_path.c_str());
110 return ERROR_UNEXPECTED_FILE_OR_DIRECTORY_SDMC;
111 case PathParser::FileFound:
112 break; // Expected 'success' case
113 }
114
115 if (FileUtil::Delete(full_path)) {
116 return RESULT_SUCCESS;
117 }
118
119 LOG_CRITICAL(Service_FS, "(unreachable) Unknown error deleting %s", full_path.c_str());
120 return ERROR_NOT_FOUND;
121}
122
123ResultCode SDMCArchive::RenameFile(const Path& src_path, const Path& dest_path) const {
124 const PathParser path_parser_src(src_path);
125
126 // TODO: Verify these return codes with HW
127 if (!path_parser_src.IsValid()) {
128 LOG_ERROR(Service_FS, "Invalid src path %s", src_path.DebugStr().c_str());
129 return ERROR_INVALID_PATH;
130 }
131
132 const PathParser path_parser_dest(dest_path);
133
134 if (!path_parser_dest.IsValid()) {
135 LOG_ERROR(Service_FS, "Invalid dest path %s", dest_path.DebugStr().c_str());
136 return ERROR_INVALID_PATH;
137 }
138
139 const auto src_path_full = path_parser_src.BuildHostPath(mount_point);
140 const auto dest_path_full = path_parser_dest.BuildHostPath(mount_point);
141
142 if (FileUtil::Rename(src_path_full, dest_path_full)) {
143 return RESULT_SUCCESS;
144 }
145
146 // TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't
147 // exist or similar. Verify.
148 return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
149 ErrorSummary::NothingHappened, ErrorLevel::Status);
150}
151
152template <typename T>
153static ResultCode DeleteDirectoryHelper(const Path& path, const std::string& mount_point,
154 T deleter) {
155 const PathParser path_parser(path);
156
157 if (!path_parser.IsValid()) {
158 LOG_ERROR(Service_FS, "Invalid path %s", path.DebugStr().c_str());
159 return ERROR_INVALID_PATH;
160 }
161
162 if (path_parser.IsRootDirectory())
163 return ERROR_NOT_FOUND;
164
165 const auto full_path = path_parser.BuildHostPath(mount_point);
166
167 switch (path_parser.GetHostStatus(mount_point)) {
168 case PathParser::InvalidMountPoint:
169 LOG_CRITICAL(Service_FS, "(unreachable) Invalid mount point %s", mount_point.c_str());
170 return ERROR_NOT_FOUND;
171 case PathParser::PathNotFound:
172 case PathParser::NotFound:
173 LOG_ERROR(Service_FS, "Path not found %s", full_path.c_str());
174 return ERROR_NOT_FOUND;
175 case PathParser::FileInPath:
176 case PathParser::FileFound:
177 LOG_ERROR(Service_FS, "Unexpected file in path %s", full_path.c_str());
178 return ERROR_UNEXPECTED_FILE_OR_DIRECTORY_SDMC;
179 case PathParser::DirectoryFound:
180 break; // Expected 'success' case
181 }
182
183 if (deleter(full_path)) {
184 return RESULT_SUCCESS;
185 }
186
187 LOG_ERROR(Service_FS, "Directory not empty %s", full_path.c_str());
188 return ERROR_UNEXPECTED_FILE_OR_DIRECTORY_SDMC;
189}
190
191ResultCode SDMCArchive::DeleteDirectory(const Path& path) const {
192 return DeleteDirectoryHelper(path, mount_point, FileUtil::DeleteDir);
193}
194
195ResultCode SDMCArchive::DeleteDirectoryRecursively(const Path& path) const {
196 return DeleteDirectoryHelper(
197 path, mount_point, [](const std::string& p) { return FileUtil::DeleteDirRecursively(p); });
198}
199
200ResultCode SDMCArchive::CreateFile(const FileSys::Path& path, u64 size) const {
201 const PathParser path_parser(path);
202
203 if (!path_parser.IsValid()) {
204 LOG_ERROR(Service_FS, "Invalid path %s", path.DebugStr().c_str());
205 return ERROR_INVALID_PATH;
206 }
207
208 const auto full_path = path_parser.BuildHostPath(mount_point);
209
210 switch (path_parser.GetHostStatus(mount_point)) {
211 case PathParser::InvalidMountPoint:
212 LOG_CRITICAL(Service_FS, "(unreachable) Invalid mount point %s", mount_point.c_str());
213 return ERROR_NOT_FOUND;
214 case PathParser::PathNotFound:
215 case PathParser::FileInPath:
216 LOG_ERROR(Service_FS, "Path not found %s", full_path.c_str());
217 return ERROR_NOT_FOUND;
218 case PathParser::DirectoryFound:
219 LOG_ERROR(Service_FS, "%s already exists", full_path.c_str());
220 return ERROR_UNEXPECTED_FILE_OR_DIRECTORY_SDMC;
221 case PathParser::FileFound:
222 LOG_ERROR(Service_FS, "%s already exists", full_path.c_str());
223 return ERROR_ALREADY_EXISTS;
224 case PathParser::NotFound:
225 break; // Expected 'success' case
226 }
227
228 if (size == 0) {
229 FileUtil::CreateEmptyFile(full_path);
230 return RESULT_SUCCESS;
231 }
232
233 FileUtil::IOFile file(full_path, "wb");
234 // Creates a sparse file (or a normal file on filesystems without the concept of sparse files)
235 // We do this by seeking to the right size, then writing a single null byte.
236 if (file.Seek(size - 1, SEEK_SET) && file.WriteBytes("", 1) == 1) {
237 return RESULT_SUCCESS;
238 }
239
240 LOG_ERROR(Service_FS, "Too large file");
241 return ResultCode(ErrorDescription::TooLarge, ErrorModule::FS, ErrorSummary::OutOfResource,
242 ErrorLevel::Info);
243}
244
245ResultCode SDMCArchive::CreateDirectory(const Path& path) const {
246 const PathParser path_parser(path);
247
248 if (!path_parser.IsValid()) {
249 LOG_ERROR(Service_FS, "Invalid path %s", path.DebugStr().c_str());
250 return ERROR_INVALID_PATH;
251 }
252
253 const auto full_path = path_parser.BuildHostPath(mount_point);
254
255 switch (path_parser.GetHostStatus(mount_point)) {
256 case PathParser::InvalidMountPoint:
257 LOG_CRITICAL(Service_FS, "(unreachable) Invalid mount point %s", mount_point.c_str());
258 return ERROR_NOT_FOUND;
259 case PathParser::PathNotFound:
260 case PathParser::FileInPath:
261 LOG_ERROR(Service_FS, "Path not found %s", full_path.c_str());
262 return ERROR_NOT_FOUND;
263 case PathParser::DirectoryFound:
264 case PathParser::FileFound:
265 LOG_ERROR(Service_FS, "%s already exists", full_path.c_str());
266 return ERROR_ALREADY_EXISTS;
267 case PathParser::NotFound:
268 break; // Expected 'success' case
269 }
270
271 if (FileUtil::CreateDir(mount_point + path.AsString())) {
272 return RESULT_SUCCESS;
273 }
274
275 LOG_CRITICAL(Service_FS, "(unreachable) Unknown error creating %s", mount_point.c_str());
276 return ResultCode(ErrorDescription::NoData, ErrorModule::FS, ErrorSummary::Canceled,
277 ErrorLevel::Status);
278}
279
280ResultCode SDMCArchive::RenameDirectory(const Path& src_path, const Path& dest_path) const {
281 const PathParser path_parser_src(src_path);
282
283 // TODO: Verify these return codes with HW
284 if (!path_parser_src.IsValid()) {
285 LOG_ERROR(Service_FS, "Invalid src path %s", src_path.DebugStr().c_str());
286 return ERROR_INVALID_PATH;
287 }
288
289 const PathParser path_parser_dest(dest_path);
290
291 if (!path_parser_dest.IsValid()) {
292 LOG_ERROR(Service_FS, "Invalid dest path %s", dest_path.DebugStr().c_str());
293 return ERROR_INVALID_PATH;
294 }
295
296 const auto src_path_full = path_parser_src.BuildHostPath(mount_point);
297 const auto dest_path_full = path_parser_dest.BuildHostPath(mount_point);
298
299 if (FileUtil::Rename(src_path_full, dest_path_full)) {
300 return RESULT_SUCCESS;
301 }
302
303 // TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't
304 // exist or similar. Verify.
305 return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
306 ErrorSummary::NothingHappened, ErrorLevel::Status);
307}
308
309ResultVal<std::unique_ptr<DirectoryBackend>> SDMCArchive::OpenDirectory(const Path& path) const {
310 const PathParser path_parser(path);
311
312 if (!path_parser.IsValid()) {
313 LOG_ERROR(Service_FS, "Invalid path %s", path.DebugStr().c_str());
314 return ERROR_INVALID_PATH;
315 }
316
317 const auto full_path = path_parser.BuildHostPath(mount_point);
318
319 switch (path_parser.GetHostStatus(mount_point)) {
320 case PathParser::InvalidMountPoint:
321 LOG_CRITICAL(Service_FS, "(unreachable) Invalid mount point %s", mount_point.c_str());
322 return ERROR_NOT_FOUND;
323 case PathParser::PathNotFound:
324 case PathParser::NotFound:
325 case PathParser::FileFound:
326 LOG_ERROR(Service_FS, "%s not found", full_path.c_str());
327 return ERROR_NOT_FOUND;
328 case PathParser::FileInPath:
329 LOG_ERROR(Service_FS, "Unexpected file in path %s", full_path.c_str());
330 return ERROR_UNEXPECTED_FILE_OR_DIRECTORY_SDMC;
331 case PathParser::DirectoryFound:
332 break; // Expected 'success' case
333 }
334
335 auto directory = std::make_unique<DiskDirectory>(full_path);
336 return MakeResult<std::unique_ptr<DirectoryBackend>>(std::move(directory));
337}
338
339u64 SDMCArchive::GetFreeBytes() const {
340 // TODO: Stubbed to return 1GiB
341 return 1024 * 1024 * 1024;
342}
343
344ArchiveFactory_SDMC::ArchiveFactory_SDMC(const std::string& sdmc_directory)
345 : sdmc_directory(sdmc_directory) {
346 LOG_DEBUG(Service_FS, "Directory %s set as SDMC.", sdmc_directory.c_str());
347}
348
349bool ArchiveFactory_SDMC::Initialize() {
350 if (!Settings::values.use_virtual_sd) {
351 LOG_WARNING(Service_FS, "SDMC disabled by config.");
352 return false;
353 }
354
355 if (!FileUtil::CreateFullPath(sdmc_directory)) {
356 LOG_ERROR(Service_FS, "Unable to create SDMC path.");
357 return false;
358 }
359
360 return true;
361}
362
363ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SDMC::Open(const Path& path) {
364 auto archive = std::make_unique<SDMCArchive>(sdmc_directory);
365 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
366}
367
368ResultCode ArchiveFactory_SDMC::Format(const Path& path,
369 const FileSys::ArchiveFormatInfo& format_info) {
370 // This is kind of an undesirable operation, so let's just ignore it. :)
371 return RESULT_SUCCESS;
372}
373
374ResultVal<ArchiveFormatInfo> ArchiveFactory_SDMC::GetFormatInfo(const Path& path) const {
375 // TODO(Subv): Implement
376 LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive %s", GetName().c_str());
377 return ResultCode(-1);
378}
379} // namespace FileSys
diff --git a/src/core/file_sys/archive_sdmc.h b/src/core/file_sys/archive_sdmc.h
deleted file mode 100644
index f6c70bfcc..000000000
--- a/src/core/file_sys/archive_sdmc.h
+++ /dev/null
@@ -1,66 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <memory>
8#include <string>
9#include "core/file_sys/archive_backend.h"
10#include "core/hle/result.h"
11
12////////////////////////////////////////////////////////////////////////////////////////////////////
13// FileSys namespace
14
15namespace FileSys {
16
17/// Archive backend for SDMC archive
18class SDMCArchive : public ArchiveBackend {
19public:
20 explicit SDMCArchive(const std::string& mount_point_) : mount_point(mount_point_) {}
21
22 std::string GetName() const override {
23 return "SDMCArchive: " + mount_point;
24 }
25
26 ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path,
27 const Mode& mode) const override;
28 ResultCode DeleteFile(const Path& path) const override;
29 ResultCode RenameFile(const Path& src_path, const Path& dest_path) const override;
30 ResultCode DeleteDirectory(const Path& path) const override;
31 ResultCode DeleteDirectoryRecursively(const Path& path) const override;
32 ResultCode CreateFile(const Path& path, u64 size) const override;
33 ResultCode CreateDirectory(const Path& path) const override;
34 ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const override;
35 ResultVal<std::unique_ptr<DirectoryBackend>> OpenDirectory(const Path& path) const override;
36 u64 GetFreeBytes() const override;
37
38protected:
39 ResultVal<std::unique_ptr<FileBackend>> OpenFileBase(const Path& path, const Mode& mode) const;
40 std::string mount_point;
41};
42
43/// File system interface to the SDMC archive
44class ArchiveFactory_SDMC final : public ArchiveFactory {
45public:
46 explicit ArchiveFactory_SDMC(const std::string& mount_point);
47
48 /**
49 * Initialize the archive.
50 * @return true if it initialized successfully
51 */
52 bool Initialize();
53
54 std::string GetName() const override {
55 return "SDMC";
56 }
57
58 ResultVal<std::unique_ptr<ArchiveBackend>> Open(const Path& path) override;
59 ResultCode Format(const Path& path, const FileSys::ArchiveFormatInfo& format_info) override;
60 ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override;
61
62private:
63 std::string sdmc_directory;
64};
65
66} // namespace FileSys
diff --git a/src/core/file_sys/archive_sdmcwriteonly.cpp b/src/core/file_sys/archive_sdmcwriteonly.cpp
deleted file mode 100644
index 244aef48a..000000000
--- a/src/core/file_sys/archive_sdmcwriteonly.cpp
+++ /dev/null
@@ -1,70 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <memory>
6#include "common/file_util.h"
7#include "core/file_sys/archive_sdmcwriteonly.h"
8#include "core/file_sys/directory_backend.h"
9#include "core/file_sys/errors.h"
10#include "core/file_sys/file_backend.h"
11#include "core/settings.h"
12
13////////////////////////////////////////////////////////////////////////////////////////////////////
14// FileSys namespace
15
16namespace FileSys {
17
18ResultVal<std::unique_ptr<FileBackend>> SDMCWriteOnlyArchive::OpenFile(const Path& path,
19 const Mode& mode) const {
20 if (mode.read_flag) {
21 LOG_ERROR(Service_FS, "Read flag is not supported");
22 return ERROR_INVALID_READ_FLAG;
23 }
24 return SDMCArchive::OpenFileBase(path, mode);
25}
26
27ResultVal<std::unique_ptr<DirectoryBackend>> SDMCWriteOnlyArchive::OpenDirectory(
28 const Path& path) const {
29 LOG_ERROR(Service_FS, "Not supported");
30 return ERROR_UNSUPPORTED_OPEN_FLAGS;
31}
32
33ArchiveFactory_SDMCWriteOnly::ArchiveFactory_SDMCWriteOnly(const std::string& mount_point)
34 : sdmc_directory(mount_point) {
35 LOG_DEBUG(Service_FS, "Directory %s set as SDMCWriteOnly.", sdmc_directory.c_str());
36}
37
38bool ArchiveFactory_SDMCWriteOnly::Initialize() {
39 if (!Settings::values.use_virtual_sd) {
40 LOG_WARNING(Service_FS, "SDMC disabled by config.");
41 return false;
42 }
43
44 if (!FileUtil::CreateFullPath(sdmc_directory)) {
45 LOG_ERROR(Service_FS, "Unable to create SDMC path.");
46 return false;
47 }
48
49 return true;
50}
51
52ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SDMCWriteOnly::Open(const Path& path) {
53 auto archive = std::make_unique<SDMCWriteOnlyArchive>(sdmc_directory);
54 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
55}
56
57ResultCode ArchiveFactory_SDMCWriteOnly::Format(const Path& path,
58 const FileSys::ArchiveFormatInfo& format_info) {
59 // TODO(wwylele): hwtest this
60 LOG_ERROR(Service_FS, "Attempted to format a SDMC write-only archive.");
61 return ResultCode(-1);
62}
63
64ResultVal<ArchiveFormatInfo> ArchiveFactory_SDMCWriteOnly::GetFormatInfo(const Path& path) const {
65 // TODO(Subv): Implement
66 LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive %s", GetName().c_str());
67 return ResultCode(-1);
68}
69
70} // namespace FileSys
diff --git a/src/core/file_sys/archive_sdmcwriteonly.h b/src/core/file_sys/archive_sdmcwriteonly.h
deleted file mode 100644
index 9cd38d96f..000000000
--- a/src/core/file_sys/archive_sdmcwriteonly.h
+++ /dev/null
@@ -1,57 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "core/file_sys/archive_sdmc.h"
8
9////////////////////////////////////////////////////////////////////////////////////////////////////
10// FileSys namespace
11
12namespace FileSys {
13
14/**
15 * Archive backend for SDMC write-only archive.
16 * The behaviour of SDMCWriteOnlyArchive is almost the same as SDMCArchive, except for
17 * - OpenDirectory is unsupported;
18 * - OpenFile with read flag is unsupported.
19 */
20class SDMCWriteOnlyArchive : public SDMCArchive {
21public:
22 explicit SDMCWriteOnlyArchive(const std::string& mount_point) : SDMCArchive(mount_point) {}
23
24 std::string GetName() const override {
25 return "SDMCWriteOnlyArchive: " + mount_point;
26 }
27
28 ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path,
29 const Mode& mode) const override;
30
31 ResultVal<std::unique_ptr<DirectoryBackend>> OpenDirectory(const Path& path) const override;
32};
33
34/// File system interface to the SDMC write-only archive
35class ArchiveFactory_SDMCWriteOnly final : public ArchiveFactory {
36public:
37 explicit ArchiveFactory_SDMCWriteOnly(const std::string& mount_point);
38
39 /**
40 * Initialize the archive.
41 * @return true if it initialized successfully
42 */
43 bool Initialize();
44
45 std::string GetName() const override {
46 return "SDMCWriteOnly";
47 }
48
49 ResultVal<std::unique_ptr<ArchiveBackend>> Open(const Path& path) override;
50 ResultCode Format(const Path& path, const FileSys::ArchiveFormatInfo& format_info) override;
51 ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override;
52
53private:
54 std::string sdmc_directory;
55};
56
57} // namespace FileSys
diff --git a/src/core/file_sys/archive_selfncch.cpp b/src/core/file_sys/archive_selfncch.cpp
deleted file mode 100644
index 3222000cf..000000000
--- a/src/core/file_sys/archive_selfncch.cpp
+++ /dev/null
@@ -1,297 +0,0 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <array>
6#include <cinttypes>
7#include "common/common_types.h"
8#include "common/logging/log.h"
9#include "common/swap.h"
10#include "core/file_sys/archive_selfncch.h"
11#include "core/file_sys/errors.h"
12#include "core/file_sys/ivfc_archive.h"
13#include "core/hle/kernel/process.h"
14
15////////////////////////////////////////////////////////////////////////////////////////////////////
16// FileSys namespace
17
18namespace FileSys {
19
20enum class SelfNCCHFilePathType : u32 {
21 RomFS = 0,
22 Code = 1, // This is not supported by SelfNCCHArchive but by archive 0x2345678E
23 ExeFS = 2,
24 UpdateRomFS = 5, // This is presumably for accessing the RomFS of the update patch.
25};
26
27struct SelfNCCHFilePath {
28 u32_le type;
29 std::array<char, 8> exefs_filename;
30};
31static_assert(sizeof(SelfNCCHFilePath) == 12, "NCCHFilePath has wrong size!");
32
33// A read-only file created from a block of data. It only allows you to read the entire file at
34// once, in a single read operation.
35class ExeFSSectionFile final : public FileBackend {
36public:
37 explicit ExeFSSectionFile(std::shared_ptr<std::vector<u8>> data_) : data(std::move(data_)) {}
38
39 ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override {
40 if (offset != 0) {
41 LOG_ERROR(Service_FS, "offset must be zero!");
42 return ERROR_UNSUPPORTED_OPEN_FLAGS;
43 }
44
45 if (length != data->size()) {
46 LOG_ERROR(Service_FS, "size must match the file size!");
47 return ERROR_INCORRECT_EXEFS_READ_SIZE;
48 }
49
50 std::memcpy(buffer, data->data(), data->size());
51 return MakeResult<size_t>(data->size());
52 }
53
54 ResultVal<size_t> Write(u64 offset, size_t length, bool flush,
55 const u8* buffer) const override {
56 LOG_ERROR(Service_FS, "The file is read-only!");
57 return ERROR_UNSUPPORTED_OPEN_FLAGS;
58 }
59
60 u64 GetSize() const override {
61 return data->size();
62 }
63
64 bool SetSize(u64 size) const override {
65 return false;
66 }
67
68 bool Close() const override {
69 return true;
70 }
71
72 void Flush() const override {}
73
74private:
75 std::shared_ptr<std::vector<u8>> data;
76};
77
78// SelfNCCHArchive represents the running application itself. From this archive the application can
79// open RomFS and ExeFS, excluding the .code section.
80class SelfNCCHArchive final : public ArchiveBackend {
81public:
82 explicit SelfNCCHArchive(const NCCHData& ncch_data_) : ncch_data(ncch_data_) {}
83
84 std::string GetName() const override {
85 return "SelfNCCHArchive";
86 }
87
88 ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path, const Mode&) const override {
89 // Note: SelfNCCHArchive doesn't check the open mode.
90
91 if (path.GetType() != LowPathType::Binary) {
92 LOG_ERROR(Service_FS, "Path need to be Binary");
93 return ERROR_INVALID_PATH;
94 }
95
96 std::vector<u8> binary = path.AsBinary();
97 if (binary.size() != sizeof(SelfNCCHFilePath)) {
98 LOG_ERROR(Service_FS, "Wrong path size %zu", binary.size());
99 return ERROR_INVALID_PATH;
100 }
101
102 SelfNCCHFilePath file_path;
103 std::memcpy(&file_path, binary.data(), sizeof(SelfNCCHFilePath));
104
105 switch (static_cast<SelfNCCHFilePathType>(file_path.type)) {
106 case SelfNCCHFilePathType::UpdateRomFS:
107 return OpenUpdateRomFS();
108
109 case SelfNCCHFilePathType::RomFS:
110 return OpenRomFS();
111
112 case SelfNCCHFilePathType::Code:
113 LOG_ERROR(Service_FS, "Reading the code section is not supported!");
114 return ERROR_COMMAND_NOT_ALLOWED;
115
116 case SelfNCCHFilePathType::ExeFS: {
117 const auto& raw = file_path.exefs_filename;
118 auto end = std::find(raw.begin(), raw.end(), '\0');
119 std::string filename(raw.begin(), end);
120 return OpenExeFS(filename);
121 }
122 default:
123 LOG_ERROR(Service_FS, "Unknown file type %u!", static_cast<u32>(file_path.type));
124 return ERROR_INVALID_PATH;
125 }
126 }
127
128 ResultCode DeleteFile(const Path& path) const override {
129 LOG_ERROR(Service_FS, "Unsupported");
130 return ERROR_UNSUPPORTED_OPEN_FLAGS;
131 }
132
133 ResultCode RenameFile(const Path& src_path, const Path& dest_path) const override {
134 LOG_ERROR(Service_FS, "Unsupported");
135 return ERROR_UNSUPPORTED_OPEN_FLAGS;
136 }
137
138 ResultCode DeleteDirectory(const Path& path) const override {
139 LOG_ERROR(Service_FS, "Unsupported");
140 return ERROR_UNSUPPORTED_OPEN_FLAGS;
141 }
142
143 ResultCode DeleteDirectoryRecursively(const Path& path) const override {
144 LOG_ERROR(Service_FS, "Unsupported");
145 return ERROR_UNSUPPORTED_OPEN_FLAGS;
146 }
147
148 ResultCode CreateFile(const Path& path, u64 size) const override {
149 LOG_ERROR(Service_FS, "Unsupported");
150 return ERROR_UNSUPPORTED_OPEN_FLAGS;
151 }
152
153 ResultCode CreateDirectory(const Path& path) const override {
154 LOG_ERROR(Service_FS, "Unsupported");
155 return ERROR_UNSUPPORTED_OPEN_FLAGS;
156 }
157
158 ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const override {
159 LOG_ERROR(Service_FS, "Unsupported");
160 return ERROR_UNSUPPORTED_OPEN_FLAGS;
161 }
162
163 ResultVal<std::unique_ptr<DirectoryBackend>> OpenDirectory(const Path& path) const override {
164 LOG_ERROR(Service_FS, "Unsupported");
165 return ERROR_UNSUPPORTED_OPEN_FLAGS;
166 }
167
168 u64 GetFreeBytes() const override {
169 return 0;
170 }
171
172private:
173 ResultVal<std::unique_ptr<FileBackend>> OpenRomFS() const {
174 if (ncch_data.romfs_file) {
175 return MakeResult<std::unique_ptr<FileBackend>>(std::make_unique<IVFCFile>(
176 ncch_data.romfs_file, ncch_data.romfs_offset, ncch_data.romfs_size));
177 } else {
178 LOG_INFO(Service_FS, "Unable to read RomFS");
179 return ERROR_ROMFS_NOT_FOUND;
180 }
181 }
182
183 ResultVal<std::unique_ptr<FileBackend>> OpenUpdateRomFS() const {
184 if (ncch_data.update_romfs_file) {
185 return MakeResult<std::unique_ptr<FileBackend>>(std::make_unique<IVFCFile>(
186 ncch_data.update_romfs_file, ncch_data.update_romfs_offset,
187 ncch_data.update_romfs_size));
188 } else {
189 LOG_INFO(Service_FS, "Unable to read update RomFS");
190 return ERROR_ROMFS_NOT_FOUND;
191 }
192 }
193
194 ResultVal<std::unique_ptr<FileBackend>> OpenExeFS(const std::string& filename) const {
195 if (filename == "icon") {
196 if (ncch_data.icon) {
197 return MakeResult<std::unique_ptr<FileBackend>>(
198 std::make_unique<ExeFSSectionFile>(ncch_data.icon));
199 }
200
201 LOG_WARNING(Service_FS, "Unable to read icon");
202 return ERROR_EXEFS_SECTION_NOT_FOUND;
203 }
204
205 if (filename == "logo") {
206 if (ncch_data.logo) {
207 return MakeResult<std::unique_ptr<FileBackend>>(
208 std::make_unique<ExeFSSectionFile>(ncch_data.logo));
209 }
210
211 LOG_WARNING(Service_FS, "Unable to read logo");
212 return ERROR_EXEFS_SECTION_NOT_FOUND;
213 }
214
215 if (filename == "banner") {
216 if (ncch_data.banner) {
217 return MakeResult<std::unique_ptr<FileBackend>>(
218 std::make_unique<ExeFSSectionFile>(ncch_data.banner));
219 }
220
221 LOG_WARNING(Service_FS, "Unable to read banner");
222 return ERROR_EXEFS_SECTION_NOT_FOUND;
223 }
224
225 LOG_ERROR(Service_FS, "Unknown ExeFS section %s!", filename.c_str());
226 return ERROR_INVALID_PATH;
227 }
228
229 NCCHData ncch_data;
230};
231
232void ArchiveFactory_SelfNCCH::Register(Loader::AppLoader& app_loader) {
233 u64 program_id = 0;
234 if (app_loader.ReadProgramId(program_id) != Loader::ResultStatus::Success) {
235 LOG_WARNING(
236 Service_FS,
237 "Could not read program id when registering with SelfNCCH, this might be a 3dsx file");
238 }
239
240 LOG_DEBUG(Service_FS, "Registering program %016" PRIX64 " with the SelfNCCH archive factory",
241 program_id);
242
243 if (ncch_data.find(program_id) != ncch_data.end()) {
244 LOG_WARNING(Service_FS, "Registering program %016" PRIX64
245 " with SelfNCCH will override existing mapping",
246 program_id);
247 }
248
249 NCCHData& data = ncch_data[program_id];
250
251 std::shared_ptr<FileUtil::IOFile> romfs_file_;
252 if (Loader::ResultStatus::Success ==
253 app_loader.ReadRomFS(romfs_file_, data.romfs_offset, data.romfs_size)) {
254
255 data.romfs_file = std::move(romfs_file_);
256 }
257
258 std::shared_ptr<FileUtil::IOFile> update_romfs_file;
259 if (Loader::ResultStatus::Success ==
260 app_loader.ReadUpdateRomFS(update_romfs_file, data.update_romfs_offset,
261 data.update_romfs_size)) {
262
263 data.update_romfs_file = std::move(update_romfs_file);
264 }
265
266 std::vector<u8> buffer;
267
268 if (Loader::ResultStatus::Success == app_loader.ReadIcon(buffer))
269 data.icon = std::make_shared<std::vector<u8>>(std::move(buffer));
270
271 buffer.clear();
272 if (Loader::ResultStatus::Success == app_loader.ReadLogo(buffer))
273 data.logo = std::make_shared<std::vector<u8>>(std::move(buffer));
274
275 buffer.clear();
276 if (Loader::ResultStatus::Success == app_loader.ReadBanner(buffer))
277 data.banner = std::make_shared<std::vector<u8>>(std::move(buffer));
278}
279
280ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SelfNCCH::Open(const Path& path) {
281 //auto archive = std::make_unique<SelfNCCHArchive>(
282 // ncch_data[Kernel::g_current_process->codeset->program_id]);
283 //return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
284 return {};
285}
286
287ResultCode ArchiveFactory_SelfNCCH::Format(const Path&, const FileSys::ArchiveFormatInfo&) {
288 LOG_ERROR(Service_FS, "Attempted to format a SelfNCCH archive.");
289 return ERROR_INVALID_PATH;
290}
291
292ResultVal<ArchiveFormatInfo> ArchiveFactory_SelfNCCH::GetFormatInfo(const Path&) const {
293 LOG_ERROR(Service_FS, "Attempted to get format info of a SelfNCCH archive");
294 return ERROR_INVALID_PATH;
295}
296
297} // namespace FileSys
diff --git a/src/core/file_sys/archive_selfncch.h b/src/core/file_sys/archive_selfncch.h
deleted file mode 100644
index 0d6d6766e..000000000
--- a/src/core/file_sys/archive_selfncch.h
+++ /dev/null
@@ -1,54 +0,0 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <memory>
8#include <string>
9#include <unordered_map>
10#include <vector>
11#include "common/common_types.h"
12#include "core/file_sys/archive_backend.h"
13#include "core/hle/result.h"
14#include "core/loader/loader.h"
15
16////////////////////////////////////////////////////////////////////////////////////////////////////
17// FileSys namespace
18
19namespace FileSys {
20
21struct NCCHData {
22 std::shared_ptr<std::vector<u8>> icon;
23 std::shared_ptr<std::vector<u8>> logo;
24 std::shared_ptr<std::vector<u8>> banner;
25 std::shared_ptr<FileUtil::IOFile> romfs_file;
26 u64 romfs_offset = 0;
27 u64 romfs_size = 0;
28
29 std::shared_ptr<FileUtil::IOFile> update_romfs_file;
30 u64 update_romfs_offset = 0;
31 u64 update_romfs_size = 0;
32};
33
34/// File system interface to the SelfNCCH archive
35class ArchiveFactory_SelfNCCH final : public ArchiveFactory {
36public:
37 ArchiveFactory_SelfNCCH() = default;
38
39 /// Registers a loaded application so that we can open its SelfNCCH archive when requested.
40 void Register(Loader::AppLoader& app_loader);
41
42 std::string GetName() const override {
43 return "SelfNCCH";
44 }
45 ResultVal<std::unique_ptr<ArchiveBackend>> Open(const Path& path) override;
46 ResultCode Format(const Path& path, const FileSys::ArchiveFormatInfo& format_info) override;
47 ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override;
48
49private:
50 /// Mapping of ProgramId -> NCCHData
51 std::unordered_map<u64, NCCHData> ncch_data;
52};
53
54} // namespace FileSys
diff --git a/src/core/file_sys/archive_source_sd_savedata.cpp b/src/core/file_sys/archive_source_sd_savedata.cpp
deleted file mode 100644
index a7e331724..000000000
--- a/src/core/file_sys/archive_source_sd_savedata.cpp
+++ /dev/null
@@ -1,97 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "common/file_util.h"
6#include "common/logging/log.h"
7#include "common/string_util.h"
8#include "core/file_sys/archive_source_sd_savedata.h"
9#include "core/file_sys/errors.h"
10#include "core/file_sys/savedata_archive.h"
11#include "core/hle/service/fs/archive.h"
12
13////////////////////////////////////////////////////////////////////////////////////////////////////
14// FileSys namespace
15
16namespace FileSys {
17
18namespace {
19
20std::string GetSaveDataContainerPath(const std::string& sdmc_directory) {
21 return Common::StringFromFormat("%sNintendo 3DS/%s/%s/title/", sdmc_directory.c_str(),
22 SYSTEM_ID, SDCARD_ID);
23}
24
25std::string GetSaveDataPath(const std::string& mount_location, u64 program_id) {
26 u32 high = static_cast<u32>(program_id >> 32);
27 u32 low = static_cast<u32>(program_id & 0xFFFFFFFF);
28 return Common::StringFromFormat("%s%08x/%08x/data/00000001/", mount_location.c_str(), high,
29 low);
30}
31
32std::string GetSaveDataMetadataPath(const std::string& mount_location, u64 program_id) {
33 u32 high = static_cast<u32>(program_id >> 32);
34 u32 low = static_cast<u32>(program_id & 0xFFFFFFFF);
35 return Common::StringFromFormat("%s%08x/%08x/data/00000001.metadata", mount_location.c_str(),
36 high, low);
37}
38
39} // namespace
40
41ArchiveSource_SDSaveData::ArchiveSource_SDSaveData(const std::string& sdmc_directory)
42 : mount_point(GetSaveDataContainerPath(sdmc_directory)) {
43 LOG_DEBUG(Service_FS, "Directory %s set as SaveData.", mount_point.c_str());
44}
45
46ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveSource_SDSaveData::Open(u64 program_id) {
47 std::string concrete_mount_point = GetSaveDataPath(mount_point, program_id);
48 if (!FileUtil::Exists(concrete_mount_point)) {
49 // When a SaveData archive is created for the first time, it is not yet formatted and the
50 // save file/directory structure expected by the game has not yet been initialized.
51 // Returning the NotFormatted error code will signal the game to provision the SaveData
52 // archive with the files and folders that it expects.
53 return ERR_NOT_FORMATTED;
54 }
55
56 auto archive = std::make_unique<SaveDataArchive>(std::move(concrete_mount_point));
57 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
58}
59
60ResultCode ArchiveSource_SDSaveData::Format(u64 program_id,
61 const FileSys::ArchiveFormatInfo& format_info) {
62 std::string concrete_mount_point = GetSaveDataPath(mount_point, program_id);
63 FileUtil::DeleteDirRecursively(concrete_mount_point);
64 FileUtil::CreateFullPath(concrete_mount_point);
65
66 // Write the format metadata
67 std::string metadata_path = GetSaveDataMetadataPath(mount_point, program_id);
68 FileUtil::IOFile file(metadata_path, "wb");
69
70 if (file.IsOpen()) {
71 file.WriteBytes(&format_info, sizeof(format_info));
72 return RESULT_SUCCESS;
73 }
74 return RESULT_SUCCESS;
75}
76
77ResultVal<ArchiveFormatInfo> ArchiveSource_SDSaveData::GetFormatInfo(u64 program_id) const {
78 std::string metadata_path = GetSaveDataMetadataPath(mount_point, program_id);
79 FileUtil::IOFile file(metadata_path, "rb");
80
81 if (!file.IsOpen()) {
82 LOG_ERROR(Service_FS, "Could not open metadata information for archive");
83 // TODO(Subv): Verify error code
84 return ERR_NOT_FORMATTED;
85 }
86
87 ArchiveFormatInfo info = {};
88 file.ReadBytes(&info, sizeof(info));
89 return MakeResult<ArchiveFormatInfo>(info);
90}
91
92std::string ArchiveSource_SDSaveData::GetSaveDataPathFor(const std::string& mount_point,
93 u64 program_id) {
94 return GetSaveDataPath(GetSaveDataContainerPath(mount_point), program_id);
95}
96
97} // namespace FileSys
diff --git a/src/core/file_sys/archive_source_sd_savedata.h b/src/core/file_sys/archive_source_sd_savedata.h
deleted file mode 100644
index b5fe43cc1..000000000
--- a/src/core/file_sys/archive_source_sd_savedata.h
+++ /dev/null
@@ -1,32 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <memory>
8#include <string>
9#include "core/file_sys/archive_backend.h"
10#include "core/hle/result.h"
11
12////////////////////////////////////////////////////////////////////////////////////////////////////
13// FileSys namespace
14
15namespace FileSys {
16
17/// A common source of SD save data archive
18class ArchiveSource_SDSaveData {
19public:
20 explicit ArchiveSource_SDSaveData(const std::string& mount_point);
21
22 ResultVal<std::unique_ptr<ArchiveBackend>> Open(u64 program_id);
23 ResultCode Format(u64 program_id, const FileSys::ArchiveFormatInfo& format_info);
24 ResultVal<ArchiveFormatInfo> GetFormatInfo(u64 program_id) const;
25
26 static std::string GetSaveDataPathFor(const std::string& mount_point, u64 program_id);
27
28private:
29 std::string mount_point;
30};
31
32} // namespace FileSys
diff --git a/src/core/file_sys/archive_systemsavedata.cpp b/src/core/file_sys/archive_systemsavedata.cpp
deleted file mode 100644
index 81423bffd..000000000
--- a/src/core/file_sys/archive_systemsavedata.cpp
+++ /dev/null
@@ -1,77 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <memory>
7#include <vector>
8#include "common/common_types.h"
9#include "common/file_util.h"
10#include "common/string_util.h"
11#include "core/file_sys/archive_systemsavedata.h"
12#include "core/file_sys/errors.h"
13#include "core/file_sys/savedata_archive.h"
14#include "core/hle/service/fs/archive.h"
15
16////////////////////////////////////////////////////////////////////////////////////////////////////
17// FileSys namespace
18
19namespace FileSys {
20
21std::string GetSystemSaveDataPath(const std::string& mount_point, const Path& path) {
22 std::vector<u8> vec_data = path.AsBinary();
23 const u32* data = reinterpret_cast<const u32*>(vec_data.data());
24 u32 save_low = data[1];
25 u32 save_high = data[0];
26 return Common::StringFromFormat("%s%08X/%08X/", mount_point.c_str(), save_low, save_high);
27}
28
29std::string GetSystemSaveDataContainerPath(const std::string& mount_point) {
30 return Common::StringFromFormat("%sdata/%s/sysdata/", mount_point.c_str(), SYSTEM_ID);
31}
32
33Path ConstructSystemSaveDataBinaryPath(u32 high, u32 low) {
34 std::vector<u8> binary_path;
35 binary_path.reserve(8);
36
37 // Append each word byte by byte
38
39 // First is the high word
40 for (unsigned i = 0; i < 4; ++i)
41 binary_path.push_back((high >> (8 * i)) & 0xFF);
42
43 // Next is the low word
44 for (unsigned i = 0; i < 4; ++i)
45 binary_path.push_back((low >> (8 * i)) & 0xFF);
46
47 return {binary_path};
48}
49
50ArchiveFactory_SystemSaveData::ArchiveFactory_SystemSaveData(const std::string& nand_path)
51 : base_path(GetSystemSaveDataContainerPath(nand_path)) {}
52
53ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SystemSaveData::Open(const Path& path) {
54 std::string fullpath = GetSystemSaveDataPath(base_path, path);
55 if (!FileUtil::Exists(fullpath)) {
56 // TODO(Subv): Check error code, this one is probably wrong
57 return ERR_NOT_FORMATTED;
58 }
59 auto archive = std::make_unique<SaveDataArchive>(fullpath);
60 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
61}
62
63ResultCode ArchiveFactory_SystemSaveData::Format(const Path& path,
64 const FileSys::ArchiveFormatInfo& format_info) {
65 std::string fullpath = GetSystemSaveDataPath(base_path, path);
66 FileUtil::DeleteDirRecursively(fullpath);
67 FileUtil::CreateFullPath(fullpath);
68 return RESULT_SUCCESS;
69}
70
71ResultVal<ArchiveFormatInfo> ArchiveFactory_SystemSaveData::GetFormatInfo(const Path& path) const {
72 // TODO(Subv): Implement
73 LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive %s", GetName().c_str());
74 return ResultCode(-1);
75}
76
77} // namespace FileSys
diff --git a/src/core/file_sys/archive_systemsavedata.h b/src/core/file_sys/archive_systemsavedata.h
deleted file mode 100644
index 52eb6c630..000000000
--- a/src/core/file_sys/archive_systemsavedata.h
+++ /dev/null
@@ -1,61 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <memory>
8#include <string>
9#include "common/common_types.h"
10#include "core/file_sys/archive_backend.h"
11#include "core/hle/result.h"
12
13////////////////////////////////////////////////////////////////////////////////////////////////////
14// FileSys namespace
15
16namespace FileSys {
17
18/// File system interface to the SystemSaveData archive
19class ArchiveFactory_SystemSaveData final : public ArchiveFactory {
20public:
21 explicit ArchiveFactory_SystemSaveData(const std::string& mount_point);
22
23 ResultVal<std::unique_ptr<ArchiveBackend>> Open(const Path& path) override;
24 ResultCode Format(const Path& path, const FileSys::ArchiveFormatInfo& format_info) override;
25 ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override;
26
27 std::string GetName() const override {
28 return "SystemSaveData";
29 }
30
31private:
32 std::string base_path;
33};
34
35/**
36 * Constructs a path to the concrete SystemSaveData archive in the host filesystem based on the
37 * input Path and base mount point.
38 * @param mount_point The base mount point of the SystemSaveData archives.
39 * @param path The path that identifies the requested concrete SystemSaveData archive.
40 * @returns The complete path to the specified SystemSaveData archive in the host filesystem
41 */
42std::string GetSystemSaveDataPath(const std::string& mount_point, const Path& path);
43
44/**
45 * Constructs a path to the base folder to hold concrete SystemSaveData archives in the host file
46 * system.
47 * @param mount_point The base folder where this folder resides, ie. SDMC or NAND.
48 * @returns The path to the base SystemSaveData archives' folder in the host file system
49 */
50std::string GetSystemSaveDataContainerPath(const std::string& mount_point);
51
52/**
53 * Constructs a FileSys::Path object that refers to the SystemSaveData archive identified by
54 * the specified high save id and low save id.
55 * @param high The high word of the save id for the archive
56 * @param low The low word of the save id for the archive
57 * @returns A FileSys::Path to the wanted archive
58 */
59Path ConstructSystemSaveDataBinaryPath(u32 high, u32 low);
60
61} // namespace FileSys
diff --git a/src/core/hle/service/cfg/cfg.cpp b/src/core/hle/service/cfg/cfg.cpp
deleted file mode 100644
index f78c25fb2..000000000
--- a/src/core/hle/service/cfg/cfg.cpp
+++ /dev/null
@@ -1,716 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <array>
7#include <cryptopp/osrng.h>
8#include <cryptopp/sha.h>
9#include "common/file_util.h"
10#include "common/logging/log.h"
11#include "common/string_util.h"
12#include "common/swap.h"
13#include "core/file_sys/archive_systemsavedata.h"
14#include "core/file_sys/errors.h"
15#include "core/file_sys/file_backend.h"
16#include "core/hle/ipc.h"
17#include "core/hle/ipc_helpers.h"
18#include "core/hle/result.h"
19#include "core/hle/service/cfg/cfg.h"
20#include "core/hle/service/cfg/cfg_i.h"
21#include "core/hle/service/cfg/cfg_nor.h"
22#include "core/hle/service/cfg/cfg_s.h"
23#include "core/hle/service/cfg/cfg_u.h"
24#include "core/hle/service/fs/archive.h"
25#include "core/hle/service/service.h"
26#include "core/memory.h"
27#include "core/settings.h"
28
29namespace Service {
30namespace CFG {
31
32/// The maximum number of block entries that can exist in the config file
33static const u32 CONFIG_FILE_MAX_BLOCK_ENTRIES = 1479;
34
35namespace {
36
37/**
38 * The header of the config savedata file,
39 * contains information about the blocks in the file
40 */
41struct SaveFileConfig {
42 u16 total_entries; ///< The total number of set entries in the config file
43 u16 data_entries_offset; ///< The offset where the data for the blocks start, this is hardcoded
44 /// to 0x455C as per hardware
45 SaveConfigBlockEntry block_entries[CONFIG_FILE_MAX_BLOCK_ENTRIES]; ///< The block headers, the
46 /// maximum possible value is
47 /// 1479 as per hardware
48 u32 unknown; ///< This field is unknown, possibly padding, 0 has been observed in hardware
49};
50static_assert(sizeof(SaveFileConfig) == 0x455C,
51 "SaveFileConfig header must be exactly 0x455C bytes");
52
53enum ConfigBlockID {
54 StereoCameraSettingsBlockID = 0x00050005,
55 SoundOutputModeBlockID = 0x00070001,
56 ConsoleUniqueID1BlockID = 0x00090000,
57 ConsoleUniqueID2BlockID = 0x00090001,
58 ConsoleUniqueID3BlockID = 0x00090002,
59 UsernameBlockID = 0x000A0000,
60 BirthdayBlockID = 0x000A0001,
61 LanguageBlockID = 0x000A0002,
62 CountryInfoBlockID = 0x000B0000,
63 CountryNameBlockID = 0x000B0001,
64 StateNameBlockID = 0x000B0002,
65 EULAVersionBlockID = 0x000D0000,
66 ConsoleModelBlockID = 0x000F0004,
67};
68
69struct UsernameBlock {
70 char16_t username[10]; ///< Exactly 20 bytes long, padded with zeros at the end if necessary
71 u32 zero;
72 u32 ng_word;
73};
74static_assert(sizeof(UsernameBlock) == 0x1C, "UsernameBlock must be exactly 0x1C bytes");
75
76struct BirthdayBlock {
77 u8 month; ///< The month of the birthday
78 u8 day; ///< The day of the birthday
79};
80static_assert(sizeof(BirthdayBlock) == 2, "BirthdayBlock must be exactly 2 bytes");
81
82struct ConsoleModelInfo {
83 u8 model; ///< The console model (3DS, 2DS, etc)
84 u8 unknown[3]; ///< Unknown data
85};
86static_assert(sizeof(ConsoleModelInfo) == 4, "ConsoleModelInfo must be exactly 4 bytes");
87
88struct ConsoleCountryInfo {
89 u8 unknown[3]; ///< Unknown data
90 u8 country_code; ///< The country code of the console
91};
92static_assert(sizeof(ConsoleCountryInfo) == 4, "ConsoleCountryInfo must be exactly 4 bytes");
93}
94
95static const ConsoleModelInfo CONSOLE_MODEL = {NINTENDO_3DS_XL, {0, 0, 0}};
96static const u8 CONSOLE_LANGUAGE = LANGUAGE_EN;
97static const UsernameBlock CONSOLE_USERNAME_BLOCK = {u"CITRA", 0, 0};
98static const BirthdayBlock PROFILE_BIRTHDAY = {3, 25}; // March 25th, 2014
99static const u8 SOUND_OUTPUT_MODE = SOUND_SURROUND;
100static const u8 UNITED_STATES_COUNTRY_ID = 49;
101/// TODO(Subv): Find what the other bytes are
102static const ConsoleCountryInfo COUNTRY_INFO = {{0, 0, 0}, UNITED_STATES_COUNTRY_ID};
103
104/**
105 * TODO(Subv): Find out what this actually is, these values fix some NaN uniforms in some games,
106 * for example Nintendo Zone
107 * Thanks Normmatt for providing this information
108 */
109static const std::array<float, 8> STEREO_CAMERA_SETTINGS = {
110 62.0f, 289.0f, 76.80000305175781f, 46.08000183105469f,
111 10.0f, 5.0f, 55.58000183105469f, 21.56999969482422f,
112};
113static_assert(sizeof(STEREO_CAMERA_SETTINGS) == 0x20,
114 "STEREO_CAMERA_SETTINGS must be exactly 0x20 bytes");
115
116static const u32 CONFIG_SAVEFILE_SIZE = 0x8000;
117static std::array<u8, CONFIG_SAVEFILE_SIZE> cfg_config_file_buffer;
118
119static Service::FS::ArchiveHandle cfg_system_save_data_archive;
120static const std::vector<u8> cfg_system_savedata_id = {
121 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x01, 0x00,
122};
123
124static u32 preferred_region_code = 0;
125
126void GetCountryCodeString(Service::Interface* self) {
127 u32* cmd_buff = Kernel::GetCommandBuffer();
128 u32 country_code_id = cmd_buff[1];
129
130 if (country_code_id >= country_codes.size() || 0 == country_codes[country_code_id]) {
131 LOG_ERROR(Service_CFG, "requested country code id=%d is invalid", country_code_id);
132 cmd_buff[1] = ResultCode(ErrorDescription::NotFound, ErrorModule::Config,
133 ErrorSummary::WrongArgument, ErrorLevel::Permanent)
134 .raw;
135 return;
136 }
137
138 cmd_buff[1] = 0;
139 cmd_buff[2] = country_codes[country_code_id];
140}
141
142void GetCountryCodeID(Service::Interface* self) {
143 u32* cmd_buff = Kernel::GetCommandBuffer();
144 u16 country_code = static_cast<u16>(cmd_buff[1]);
145 u16 country_code_id = 0;
146
147 // The following algorithm will fail if the first country code isn't 0.
148 DEBUG_ASSERT(country_codes[0] == 0);
149
150 for (u16 id = 0; id < country_codes.size(); ++id) {
151 if (country_codes[id] == country_code) {
152 country_code_id = id;
153 break;
154 }
155 }
156
157 if (0 == country_code_id) {
158 LOG_ERROR(Service_CFG, "requested country code name=%c%c is invalid", country_code & 0xff,
159 country_code >> 8);
160 cmd_buff[1] = ResultCode(ErrorDescription::NotFound, ErrorModule::Config,
161 ErrorSummary::WrongArgument, ErrorLevel::Permanent)
162 .raw;
163 cmd_buff[2] = 0xFFFF;
164 return;
165 }
166
167 cmd_buff[1] = 0;
168 cmd_buff[2] = country_code_id;
169}
170
171u32 GetRegionValue() {
172 if (Settings::values.region_value == Settings::REGION_VALUE_AUTO_SELECT)
173 return preferred_region_code;
174
175 return Settings::values.region_value;
176}
177
178void SecureInfoGetRegion(Service::Interface* self) {
179 u32* cmd_buff = Kernel::GetCommandBuffer();
180
181 cmd_buff[1] = RESULT_SUCCESS.raw;
182 cmd_buff[2] = GetRegionValue();
183}
184
185void GenHashConsoleUnique(Service::Interface* self) {
186 IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x03, 1, 0);
187 const u32 app_id_salt = rp.Pop<u32>() & 0x000FFFFF;
188
189 IPC::RequestBuilder rb = rp.MakeBuilder(3, 0);
190
191 std::array<u8, 12> buffer;
192 const ResultCode result = GetConfigInfoBlock(ConsoleUniqueID2BlockID, 8, 2, buffer.data());
193 rb.Push(result);
194 if (result.IsSuccess()) {
195 std::memcpy(&buffer[8], &app_id_salt, sizeof(u32));
196 std::array<u8, CryptoPP::SHA256::DIGESTSIZE> hash;
197 CryptoPP::SHA256().CalculateDigest(hash.data(), buffer.data(), sizeof(buffer));
198 u32 low, high;
199 memcpy(&low, &hash[hash.size() - 8], sizeof(u32));
200 memcpy(&high, &hash[hash.size() - 4], sizeof(u32));
201 rb.Push(low);
202 rb.Push(high);
203 } else {
204 rb.Push<u32>(0);
205 rb.Push<u32>(0);
206 }
207
208 LOG_DEBUG(Service_CFG, "called app_id_salt=0x%X", app_id_salt);
209}
210
211void GetRegionCanadaUSA(Service::Interface* self) {
212 u32* cmd_buff = Kernel::GetCommandBuffer();
213
214 cmd_buff[1] = RESULT_SUCCESS.raw;
215
216 u8 canada_or_usa = 1;
217 if (canada_or_usa == GetRegionValue()) {
218 cmd_buff[2] = 1;
219 } else {
220 cmd_buff[2] = 0;
221 }
222}
223
224void GetSystemModel(Service::Interface* self) {
225 u32* cmd_buff = Kernel::GetCommandBuffer();
226 u32 data;
227
228 // TODO(Subv): Find out the correct error codes
229 cmd_buff[1] =
230 Service::CFG::GetConfigInfoBlock(0x000F0004, 4, 0x8, reinterpret_cast<u8*>(&data)).raw;
231 cmd_buff[2] = data & 0xFF;
232}
233
234void GetModelNintendo2DS(Service::Interface* self) {
235 u32* cmd_buff = Kernel::GetCommandBuffer();
236 u32 data;
237
238 // TODO(Subv): Find out the correct error codes
239 cmd_buff[1] =
240 Service::CFG::GetConfigInfoBlock(0x000F0004, 4, 0x8, reinterpret_cast<u8*>(&data)).raw;
241
242 u8 model = data & 0xFF;
243 if (model == Service::CFG::NINTENDO_2DS)
244 cmd_buff[2] = 0;
245 else
246 cmd_buff[2] = 1;
247}
248
249void GetConfigInfoBlk2(Service::Interface* self) {
250 u32* cmd_buff = Kernel::GetCommandBuffer();
251 u32 size = cmd_buff[1];
252 u32 block_id = cmd_buff[2];
253 VAddr data_pointer = cmd_buff[4];
254
255 if (!Memory::IsValidVirtualAddress(data_pointer)) {
256 cmd_buff[1] = -1; // TODO(Subv): Find the right error code
257 return;
258 }
259
260 std::vector<u8> data(size);
261 cmd_buff[1] = Service::CFG::GetConfigInfoBlock(block_id, size, 0x2, data.data()).raw;
262 Memory::WriteBlock(data_pointer, data.data(), data.size());
263}
264
265void GetConfigInfoBlk8(Service::Interface* self) {
266 u32* cmd_buff = Kernel::GetCommandBuffer();
267 u32 size = cmd_buff[1];
268 u32 block_id = cmd_buff[2];
269 VAddr data_pointer = cmd_buff[4];
270
271 if (!Memory::IsValidVirtualAddress(data_pointer)) {
272 cmd_buff[1] = -1; // TODO(Subv): Find the right error code
273 return;
274 }
275
276 std::vector<u8> data(size);
277 cmd_buff[1] = Service::CFG::GetConfigInfoBlock(block_id, size, 0x8, data.data()).raw;
278 Memory::WriteBlock(data_pointer, data.data(), data.size());
279}
280
281void SetConfigInfoBlk4(Service::Interface* self) {
282 u32* cmd_buff = Kernel::GetCommandBuffer();
283 u32 block_id = cmd_buff[1];
284 u32 size = cmd_buff[2];
285 VAddr data_pointer = cmd_buff[4];
286
287 if (!Memory::IsValidVirtualAddress(data_pointer)) {
288 cmd_buff[1] = -1; // TODO(Subv): Find the right error code
289 return;
290 }
291
292 std::vector<u8> data(size);
293 Memory::ReadBlock(data_pointer, data.data(), data.size());
294 cmd_buff[1] = Service::CFG::SetConfigInfoBlock(block_id, size, 0x4, data.data()).raw;
295}
296
297void UpdateConfigNANDSavegame(Service::Interface* self) {
298 u32* cmd_buff = Kernel::GetCommandBuffer();
299 cmd_buff[1] = Service::CFG::UpdateConfigNANDSavegame().raw;
300}
301
302void FormatConfig(Service::Interface* self) {
303 u32* cmd_buff = Kernel::GetCommandBuffer();
304 cmd_buff[1] = Service::CFG::FormatConfig().raw;
305}
306
307static ResultVal<void*> GetConfigInfoBlockPointer(u32 block_id, u32 size, u32 flag) {
308 // Read the header
309 SaveFileConfig* config = reinterpret_cast<SaveFileConfig*>(cfg_config_file_buffer.data());
310
311 auto itr =
312 std::find_if(std::begin(config->block_entries), std::end(config->block_entries),
313 [&](const SaveConfigBlockEntry& entry) { return entry.block_id == block_id; });
314
315 if (itr == std::end(config->block_entries)) {
316 LOG_ERROR(Service_CFG, "Config block 0x%X with flags %u and size %u was not found",
317 block_id, flag, size);
318 return ResultCode(ErrorDescription::NotFound, ErrorModule::Config,
319 ErrorSummary::WrongArgument, ErrorLevel::Permanent);
320 }
321
322 if ((itr->flags & flag) == 0) {
323 LOG_ERROR(Service_CFG, "Invalid flag %u for config block 0x%X with size %u", flag, block_id,
324 size);
325 return ResultCode(ErrorDescription::NotAuthorized, ErrorModule::Config,
326 ErrorSummary::WrongArgument, ErrorLevel::Permanent);
327 }
328
329 if (itr->size != size) {
330 LOG_ERROR(Service_CFG, "Invalid size %u for config block 0x%X with flags %u", size,
331 block_id, flag);
332 return ResultCode(ErrorDescription::InvalidSize, ErrorModule::Config,
333 ErrorSummary::WrongArgument, ErrorLevel::Permanent);
334 }
335
336 void* pointer;
337
338 // The data is located in the block header itself if the size is less than 4 bytes
339 if (itr->size <= 4)
340 pointer = &itr->offset_or_data;
341 else
342 pointer = &cfg_config_file_buffer[itr->offset_or_data];
343
344 return MakeResult<void*>(pointer);
345}
346
347ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, void* output) {
348 void* pointer;
349 CASCADE_RESULT(pointer, GetConfigInfoBlockPointer(block_id, size, flag));
350 memcpy(output, pointer, size);
351
352 return RESULT_SUCCESS;
353}
354
355ResultCode SetConfigInfoBlock(u32 block_id, u32 size, u32 flag, const void* input) {
356 void* pointer;
357 CASCADE_RESULT(pointer, GetConfigInfoBlockPointer(block_id, size, flag));
358 memcpy(pointer, input, size);
359 return RESULT_SUCCESS;
360}
361
362ResultCode CreateConfigInfoBlk(u32 block_id, u16 size, u16 flags, const void* data) {
363 SaveFileConfig* config = reinterpret_cast<SaveFileConfig*>(cfg_config_file_buffer.data());
364 if (config->total_entries >= CONFIG_FILE_MAX_BLOCK_ENTRIES)
365 return ResultCode(-1); // TODO(Subv): Find the right error code
366
367 // Insert the block header with offset 0 for now
368 config->block_entries[config->total_entries] = {block_id, 0, size, flags};
369 if (size > 4) {
370 u32 offset = config->data_entries_offset;
371 // Perform a search to locate the next offset for the new data
372 // use the offset and size of the previous block to determine the new position
373 for (int i = config->total_entries - 1; i >= 0; --i) {
374 // Ignore the blocks that don't have a separate data offset
375 if (config->block_entries[i].size > 4) {
376 offset = config->block_entries[i].offset_or_data + config->block_entries[i].size;
377 break;
378 }
379 }
380
381 config->block_entries[config->total_entries].offset_or_data = offset;
382
383 // Write the data at the new offset
384 memcpy(&cfg_config_file_buffer[offset], data, size);
385 } else {
386 // The offset_or_data field in the header contains the data itself if it's 4 bytes or less
387 memcpy(&config->block_entries[config->total_entries].offset_or_data, data, size);
388 }
389
390 ++config->total_entries;
391 return RESULT_SUCCESS;
392}
393
394ResultCode DeleteConfigNANDSaveFile() {
395 FileSys::Path path("/config");
396 return Service::FS::DeleteFileFromArchive(cfg_system_save_data_archive, path);
397}
398
399ResultCode UpdateConfigNANDSavegame() {
400 FileSys::Mode mode = {};
401 mode.write_flag.Assign(1);
402 mode.create_flag.Assign(1);
403
404 FileSys::Path path("/config");
405
406 auto config_result = Service::FS::OpenFileFromArchive(cfg_system_save_data_archive, path, mode);
407 ASSERT_MSG(config_result.Succeeded(), "could not open file");
408
409 auto config = std::move(config_result).Unwrap();
410 config->backend->Write(0, CONFIG_SAVEFILE_SIZE, 1, cfg_config_file_buffer.data());
411
412 return RESULT_SUCCESS;
413}
414
415ResultCode FormatConfig() {
416 ResultCode res = DeleteConfigNANDSaveFile();
417 // The delete command fails if the file doesn't exist, so we have to check that too
418 if (!res.IsSuccess() && res != FileSys::ERROR_FILE_NOT_FOUND) {
419 return res;
420 }
421 // Delete the old data
422 cfg_config_file_buffer.fill(0);
423 // Create the header
424 SaveFileConfig* config = reinterpret_cast<SaveFileConfig*>(cfg_config_file_buffer.data());
425 // This value is hardcoded, taken from 3dbrew, verified by hardware, it's always the same value
426 config->data_entries_offset = 0x455C;
427
428 // Insert the default blocks
429 u8 zero_buffer[0xC0] = {};
430
431 // 0x00030001 - Unknown
432 res = CreateConfigInfoBlk(0x00030001, 0x8, 0xE, zero_buffer);
433 if (!res.IsSuccess())
434 return res;
435
436 res = CreateConfigInfoBlk(StereoCameraSettingsBlockID, sizeof(STEREO_CAMERA_SETTINGS), 0xE,
437 STEREO_CAMERA_SETTINGS.data());
438 if (!res.IsSuccess())
439 return res;
440
441 res = CreateConfigInfoBlk(SoundOutputModeBlockID, sizeof(SOUND_OUTPUT_MODE), 0xE,
442 &SOUND_OUTPUT_MODE);
443 if (!res.IsSuccess())
444 return res;
445
446 u32 random_number;
447 u64 console_id;
448 GenerateConsoleUniqueId(random_number, console_id);
449
450 u64_le console_id_le = console_id;
451 res = CreateConfigInfoBlk(ConsoleUniqueID1BlockID, sizeof(console_id_le), 0xE, &console_id_le);
452 if (!res.IsSuccess())
453 return res;
454
455 res = CreateConfigInfoBlk(ConsoleUniqueID2BlockID, sizeof(console_id_le), 0xE, &console_id_le);
456 if (!res.IsSuccess())
457 return res;
458
459 u32_le random_number_le = random_number;
460 res = CreateConfigInfoBlk(ConsoleUniqueID3BlockID, sizeof(random_number_le), 0xE,
461 &random_number_le);
462 if (!res.IsSuccess())
463 return res;
464
465 res = CreateConfigInfoBlk(UsernameBlockID, sizeof(CONSOLE_USERNAME_BLOCK), 0xE,
466 &CONSOLE_USERNAME_BLOCK);
467 if (!res.IsSuccess())
468 return res;
469
470 res = CreateConfigInfoBlk(BirthdayBlockID, sizeof(PROFILE_BIRTHDAY), 0xE, &PROFILE_BIRTHDAY);
471 if (!res.IsSuccess())
472 return res;
473
474 res = CreateConfigInfoBlk(LanguageBlockID, sizeof(CONSOLE_LANGUAGE), 0xE, &CONSOLE_LANGUAGE);
475 if (!res.IsSuccess())
476 return res;
477
478 res = CreateConfigInfoBlk(CountryInfoBlockID, sizeof(COUNTRY_INFO), 0xE, &COUNTRY_INFO);
479 if (!res.IsSuccess())
480 return res;
481
482 u16_le country_name_buffer[16][0x40] = {};
483 std::u16string region_name = Common::UTF8ToUTF16("Gensokyo");
484 for (size_t i = 0; i < 16; ++i) {
485 std::copy(region_name.cbegin(), region_name.cend(), country_name_buffer[i]);
486 }
487 // 0x000B0001 - Localized names for the profile Country
488 res = CreateConfigInfoBlk(CountryNameBlockID, sizeof(country_name_buffer), 0xE,
489 country_name_buffer);
490 if (!res.IsSuccess())
491 return res;
492 // 0x000B0002 - Localized names for the profile State/Province
493 res = CreateConfigInfoBlk(StateNameBlockID, sizeof(country_name_buffer), 0xE,
494 country_name_buffer);
495 if (!res.IsSuccess())
496 return res;
497
498 // 0x000B0003 - Unknown, related to country/address (zip code?)
499 res = CreateConfigInfoBlk(0x000B0003, 0x4, 0xE, zero_buffer);
500 if (!res.IsSuccess())
501 return res;
502
503 // 0x000C0000 - Unknown
504 res = CreateConfigInfoBlk(0x000C0000, 0xC0, 0xE, zero_buffer);
505 if (!res.IsSuccess())
506 return res;
507
508 // 0x000C0001 - Unknown
509 res = CreateConfigInfoBlk(0x000C0001, 0x14, 0xE, zero_buffer);
510 if (!res.IsSuccess())
511 return res;
512
513 // 0x000D0000 - Accepted EULA version
514 res = CreateConfigInfoBlk(EULAVersionBlockID, 0x4, 0xE, zero_buffer);
515 if (!res.IsSuccess())
516 return res;
517
518 res = CreateConfigInfoBlk(ConsoleModelBlockID, sizeof(CONSOLE_MODEL), 0xC, &CONSOLE_MODEL);
519 if (!res.IsSuccess())
520 return res;
521
522 // 0x00170000 - Unknown
523 res = CreateConfigInfoBlk(0x00170000, 0x4, 0xE, zero_buffer);
524 if (!res.IsSuccess())
525 return res;
526
527 // Save the buffer to the file
528 res = UpdateConfigNANDSavegame();
529 if (!res.IsSuccess())
530 return res;
531 return RESULT_SUCCESS;
532}
533
534ResultCode LoadConfigNANDSaveFile() {
535 // Open the SystemSaveData archive 0x00010017
536 FileSys::Path archive_path(cfg_system_savedata_id);
537 auto archive_result =
538 Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SystemSaveData, archive_path);
539
540 // If the archive didn't exist, create the files inside
541 if (archive_result.Code() == FileSys::ERR_NOT_FORMATTED) {
542 // Format the archive to create the directories
543 Service::FS::FormatArchive(Service::FS::ArchiveIdCode::SystemSaveData,
544 FileSys::ArchiveFormatInfo(), archive_path);
545
546 // Open it again to get a valid archive now that the folder exists
547 archive_result =
548 Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SystemSaveData, archive_path);
549 }
550
551 ASSERT_MSG(archive_result.Succeeded(), "Could not open the CFG SystemSaveData archive!");
552
553 cfg_system_save_data_archive = *archive_result;
554
555 FileSys::Path config_path("/config");
556 FileSys::Mode open_mode = {};
557 open_mode.read_flag.Assign(1);
558
559 auto config_result = Service::FS::OpenFileFromArchive(*archive_result, config_path, open_mode);
560
561 // Read the file if it already exists
562 if (config_result.Succeeded()) {
563 auto config = std::move(config_result).Unwrap();
564 config->backend->Read(0, CONFIG_SAVEFILE_SIZE, cfg_config_file_buffer.data());
565 return RESULT_SUCCESS;
566 }
567
568 return FormatConfig();
569}
570
571void Init() {
572 AddService(new CFG_I);
573 AddService(new CFG_NOR);
574 AddService(new CFG_S);
575 AddService(new CFG_U);
576
577 LoadConfigNANDSaveFile();
578
579 preferred_region_code = 0;
580}
581
582void Shutdown() {}
583
584/// Checks if the language is available in the chosen region, and returns a proper one
585static SystemLanguage AdjustLanguageInfoBlock(u32 region, SystemLanguage language) {
586 static const std::array<std::vector<SystemLanguage>, 7> region_languages{{
587 // JPN
588 {LANGUAGE_JP},
589 // USA
590 {LANGUAGE_EN, LANGUAGE_FR, LANGUAGE_ES, LANGUAGE_PT},
591 // EUR
592 {LANGUAGE_EN, LANGUAGE_FR, LANGUAGE_DE, LANGUAGE_IT, LANGUAGE_ES, LANGUAGE_NL, LANGUAGE_PT,
593 LANGUAGE_RU},
594 // AUS
595 {LANGUAGE_EN, LANGUAGE_FR, LANGUAGE_DE, LANGUAGE_IT, LANGUAGE_ES, LANGUAGE_NL, LANGUAGE_PT,
596 LANGUAGE_RU},
597 // CHN
598 {LANGUAGE_ZH},
599 // KOR
600 {LANGUAGE_KO},
601 // TWN
602 {LANGUAGE_TW},
603 }};
604 const auto& available = region_languages[region];
605 if (std::find(available.begin(), available.end(), language) == available.end()) {
606 return available[0];
607 }
608 return language;
609}
610
611void SetPreferredRegionCode(u32 region_code) {
612 preferred_region_code = region_code;
613 LOG_INFO(Service_CFG, "Preferred region code set to %u", preferred_region_code);
614
615 if (Settings::values.region_value == Settings::REGION_VALUE_AUTO_SELECT) {
616 const SystemLanguage current_language = GetSystemLanguage();
617 const SystemLanguage adjusted_language =
618 AdjustLanguageInfoBlock(region_code, current_language);
619 if (current_language != adjusted_language) {
620 LOG_WARNING(Service_CFG, "System language %d does not fit the region. Adjusted to %d",
621 static_cast<int>(current_language), static_cast<int>(adjusted_language));
622 SetSystemLanguage(adjusted_language);
623 }
624 }
625}
626
627void SetUsername(const std::u16string& name) {
628 ASSERT(name.size() <= 10);
629 UsernameBlock block{};
630 name.copy(block.username, name.size());
631 SetConfigInfoBlock(UsernameBlockID, sizeof(block), 4, &block);
632}
633
634std::u16string GetUsername() {
635 UsernameBlock block;
636 GetConfigInfoBlock(UsernameBlockID, sizeof(block), 8, &block);
637
638 // the username string in the block isn't null-terminated,
639 // so we need to find the end manually.
640 std::u16string username(block.username, ARRAY_SIZE(block.username));
641 const size_t pos = username.find(u'\0');
642 if (pos != std::u16string::npos)
643 username.erase(pos);
644 return username;
645}
646
647void SetBirthday(u8 month, u8 day) {
648 BirthdayBlock block = {month, day};
649 SetConfigInfoBlock(BirthdayBlockID, sizeof(block), 4, &block);
650}
651
652std::tuple<u8, u8> GetBirthday() {
653 BirthdayBlock block;
654 GetConfigInfoBlock(BirthdayBlockID, sizeof(block), 8, &block);
655 return std::make_tuple(block.month, block.day);
656}
657
658void SetSystemLanguage(SystemLanguage language) {
659 u8 block = language;
660 SetConfigInfoBlock(LanguageBlockID, sizeof(block), 4, &block);
661}
662
663SystemLanguage GetSystemLanguage() {
664 u8 block;
665 GetConfigInfoBlock(LanguageBlockID, sizeof(block), 8, &block);
666 return static_cast<SystemLanguage>(block);
667}
668
669void SetSoundOutputMode(SoundOutputMode mode) {
670 u8 block = mode;
671 SetConfigInfoBlock(SoundOutputModeBlockID, sizeof(block), 4, &block);
672}
673
674SoundOutputMode GetSoundOutputMode() {
675 u8 block;
676 GetConfigInfoBlock(SoundOutputModeBlockID, sizeof(block), 8, &block);
677 return static_cast<SoundOutputMode>(block);
678}
679
680void GenerateConsoleUniqueId(u32& random_number, u64& console_id) {
681 CryptoPP::AutoSeededRandomPool rng;
682 random_number = rng.GenerateWord32(0, 0xFFFF);
683 u64_le local_friend_code_seed;
684 rng.GenerateBlock(reinterpret_cast<CryptoPP::byte*>(&local_friend_code_seed),
685 sizeof(local_friend_code_seed));
686 console_id = (local_friend_code_seed & 0x3FFFFFFFF) | (static_cast<u64>(random_number) << 48);
687}
688
689ResultCode SetConsoleUniqueId(u32 random_number, u64 console_id) {
690 u64_le console_id_le = console_id;
691 ResultCode res =
692 SetConfigInfoBlock(ConsoleUniqueID1BlockID, sizeof(console_id_le), 0xE, &console_id_le);
693 if (!res.IsSuccess())
694 return res;
695
696 res = SetConfigInfoBlock(ConsoleUniqueID2BlockID, sizeof(console_id_le), 0xE, &console_id_le);
697 if (!res.IsSuccess())
698 return res;
699
700 u32_le random_number_le = random_number;
701 res = SetConfigInfoBlock(ConsoleUniqueID3BlockID, sizeof(random_number_le), 0xE,
702 &random_number_le);
703 if (!res.IsSuccess())
704 return res;
705
706 return RESULT_SUCCESS;
707}
708
709u64 GetConsoleUniqueId() {
710 u64_le console_id_le;
711 GetConfigInfoBlock(ConsoleUniqueID2BlockID, sizeof(console_id_le), 0xE, &console_id_le);
712 return console_id_le;
713}
714
715} // namespace CFG
716} // namespace Service
diff --git a/src/core/hle/service/cfg/cfg.h b/src/core/hle/service/cfg/cfg.h
deleted file mode 100644
index 282b6936b..000000000
--- a/src/core/hle/service/cfg/cfg.h
+++ /dev/null
@@ -1,369 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <array>
8#include <string>
9#include "common/common_types.h"
10
11union ResultCode;
12
13namespace Service {
14
15class Interface;
16
17namespace CFG {
18
19enum SystemModel {
20 NINTENDO_3DS = 0,
21 NINTENDO_3DS_XL = 1,
22 NEW_NINTENDO_3DS = 2,
23 NINTENDO_2DS = 3,
24 NEW_NINTENDO_3DS_XL = 4
25};
26
27enum SystemLanguage {
28 LANGUAGE_JP = 0,
29 LANGUAGE_EN = 1,
30 LANGUAGE_FR = 2,
31 LANGUAGE_DE = 3,
32 LANGUAGE_IT = 4,
33 LANGUAGE_ES = 5,
34 LANGUAGE_ZH = 6,
35 LANGUAGE_KO = 7,
36 LANGUAGE_NL = 8,
37 LANGUAGE_PT = 9,
38 LANGUAGE_RU = 10,
39 LANGUAGE_TW = 11
40};
41
42enum SoundOutputMode { SOUND_MONO = 0, SOUND_STEREO = 1, SOUND_SURROUND = 2 };
43
44/// Block header in the config savedata file
45struct SaveConfigBlockEntry {
46 u32 block_id; ///< The id of the current block
47 u32 offset_or_data; ///< This is the absolute offset to the block data if the size is greater
48 /// than 4 bytes, otherwise it contains the data itself
49 u16 size; ///< The size of the block
50 u16 flags; ///< The flags of the block, possibly used for access control
51};
52
53static constexpr u16 C(const char code[2]) {
54 return code[0] | (code[1] << 8);
55}
56
57static const std::array<u16, 187> country_codes = {{
58 0, C("JP"), 0, 0, 0, 0, 0, 0, // 0-7
59 C("AI"), C("AG"), C("AR"), C("AW"), C("BS"), C("BB"), C("BZ"), C("BO"), // 8-15
60 C("BR"), C("VG"), C("CA"), C("KY"), C("CL"), C("CO"), C("CR"), C("DM"), // 16-23
61 C("DO"), C("EC"), C("SV"), C("GF"), C("GD"), C("GP"), C("GT"), C("GY"), // 24-31
62 C("HT"), C("HN"), C("JM"), C("MQ"), C("MX"), C("MS"), C("AN"), C("NI"), // 32-39
63 C("PA"), C("PY"), C("PE"), C("KN"), C("LC"), C("VC"), C("SR"), C("TT"), // 40-47
64 C("TC"), C("US"), C("UY"), C("VI"), C("VE"), 0, 0, 0, // 48-55
65 0, 0, 0, 0, 0, 0, 0, 0, // 56-63
66 C("AL"), C("AU"), C("AT"), C("BE"), C("BA"), C("BW"), C("BG"), C("HR"), // 64-71
67 C("CY"), C("CZ"), C("DK"), C("EE"), C("FI"), C("FR"), C("DE"), C("GR"), // 72-79
68 C("HU"), C("IS"), C("IE"), C("IT"), C("LV"), C("LS"), C("LI"), C("LT"), // 80-87
69 C("LU"), C("MK"), C("MT"), C("ME"), C("MZ"), C("NA"), C("NL"), C("NZ"), // 88-95
70 C("NO"), C("PL"), C("PT"), C("RO"), C("RU"), C("RS"), C("SK"), C("SI"), // 96-103
71 C("ZA"), C("ES"), C("SZ"), C("SE"), C("CH"), C("TR"), C("GB"), C("ZM"), // 104-111
72 C("ZW"), C("AZ"), C("MR"), C("ML"), C("NE"), C("TD"), C("SD"), C("ER"), // 112-119
73 C("DJ"), C("SO"), C("AD"), C("GI"), C("GG"), C("IM"), C("JE"), C("MC"), // 120-127
74 C("TW"), 0, 0, 0, 0, 0, 0, 0, // 128-135
75 C("KR"), 0, 0, 0, 0, 0, 0, 0, // 136-143
76 C("HK"), C("MO"), 0, 0, 0, 0, 0, 0, // 144-151
77 C("ID"), C("SG"), C("TH"), C("PH"), C("MY"), 0, 0, 0, // 152-159
78 C("CN"), 0, 0, 0, 0, 0, 0, 0, // 160-167
79 C("AE"), C("IN"), C("EG"), C("OM"), C("QA"), C("KW"), C("SA"), C("SY"), // 168-175
80 C("BH"), C("JO"), 0, 0, 0, 0, 0, 0, // 176-183
81 C("SM"), C("VA"), C("BM"), // 184-186
82}};
83
84/**
85 * CFG::GetCountryCodeString service function
86 * Inputs:
87 * 1 : Country Code ID
88 * Outputs:
89 * 1 : Result of function, 0 on success, otherwise error code
90 * 2 : Country's 2-char string
91 */
92void GetCountryCodeString(Service::Interface* self);
93
94/**
95 * CFG::GetCountryCodeID service function
96 * Inputs:
97 * 1 : Country Code 2-char string
98 * Outputs:
99 * 1 : Result of function, 0 on success, otherwise error code
100 * 2 : Country Code ID
101 */
102void GetCountryCodeID(Service::Interface* self);
103
104u32 GetRegionValue();
105
106/**
107 * CFG::SecureInfoGetRegion service function
108 * Inputs:
109 * 1 : None
110 * Outputs:
111 * 0 : Result Header code
112 * 1 : Result of function, 0 on success, otherwise error code
113 * 2 : Region value loaded from SecureInfo offset 0x100
114 */
115void SecureInfoGetRegion(Service::Interface* self);
116
117/**
118 * CFG::GenHashConsoleUnique service function
119 * Inputs:
120 * 1 : 20 bit application ID salt
121 * Outputs:
122 * 0 : Result Header code
123 * 1 : Result of function, 0 on success, otherwise error code
124 * 2 : Hash/"ID" lower word
125 * 3 : Hash/"ID" upper word
126 */
127void GenHashConsoleUnique(Service::Interface* self);
128
129/**
130 * CFG::GetRegionCanadaUSA service function
131 * Inputs:
132 * 1 : None
133 * Outputs:
134 * 0 : Result Header code
135 * 1 : Result of function, 0 on success, otherwise error code
136 * 2 : 1 if the system is a Canada or USA model, 0 otherwise
137 */
138void GetRegionCanadaUSA(Service::Interface* self);
139
140/**
141 * CFG::GetSystemModel service function
142 * Inputs:
143 * 0 : 0x00050000
144 * Outputs:
145 * 1 : Result of function, 0 on success, otherwise error code
146 * 2 : Model of the console
147 */
148void GetSystemModel(Service::Interface* self);
149
150/**
151 * CFG::GetModelNintendo2DS service function
152 * Inputs:
153 * 0 : 0x00060000
154 * Outputs:
155 * 1 : Result of function, 0 on success, otherwise error code
156 * 2 : 0 if the system is a Nintendo 2DS, 1 otherwise
157 */
158void GetModelNintendo2DS(Service::Interface* self);
159
160/**
161 * CFG::GetConfigInfoBlk2 service function
162 * Inputs:
163 * 0 : 0x00010082
164 * 1 : Size
165 * 2 : Block ID
166 * 3 : Descriptor for the output buffer
167 * 4 : Output buffer pointer
168 * Outputs:
169 * 1 : Result of function, 0 on success, otherwise error code
170 */
171void GetConfigInfoBlk2(Service::Interface* self);
172
173/**
174 * CFG::GetConfigInfoBlk8 service function
175 * Inputs:
176 * 0 : 0x04010082 / 0x08010082
177 * 1 : Size
178 * 2 : Block ID
179 * 3 : Descriptor for the output buffer
180 * 4 : Output buffer pointer
181 * Outputs:
182 * 1 : Result of function, 0 on success, otherwise error code
183 */
184void GetConfigInfoBlk8(Service::Interface* self);
185
186/**
187 * CFG::SetConfigInfoBlk4 service function
188 * Inputs:
189 * 0 : 0x04020082 / 0x08020082
190 * 1 : Block ID
191 * 2 : Size
192 * 3 : Descriptor for the output buffer
193 * 4 : Output buffer pointer
194 * Outputs:
195 * 1 : Result of function, 0 on success, otherwise error code
196 * Note:
197 * The parameters order is different from GetConfigInfoBlk2/8's,
198 * where Block ID and Size are switched.
199 */
200void SetConfigInfoBlk4(Service::Interface* self);
201
202/**
203 * CFG::UpdateConfigNANDSavegame service function
204 * Inputs:
205 * 0 : 0x04030000 / 0x08030000
206 * Outputs:
207 * 1 : Result of function, 0 on success, otherwise error code
208 */
209void UpdateConfigNANDSavegame(Service::Interface* self);
210
211/**
212 * CFG::FormatConfig service function
213 * Inputs:
214 * 0 : 0x08060000
215 * Outputs:
216 * 1 : Result of function, 0 on success, otherwise error code
217 */
218void FormatConfig(Service::Interface* self);
219
220/**
221 * Reads a block with the specified id and flag from the Config savegame buffer
222 * and writes the output to output. The input size must match exactly the size of the requested
223 * block.
224 *
225 * @param block_id The id of the block we want to read
226 * @param size The size of the block we want to read
227 * @param flag The requested block must have this flag set
228 * @param output A pointer where we will write the read data
229 * @returns ResultCode indicating the result of the operation, 0 on success
230 */
231ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, void* output);
232
233/**
234 * Reads data from input and writes to a block with the specified id and flag
235 * in the Config savegame buffer. The input size must match exactly the size of the target block.
236 *
237 * @param block_id The id of the block we want to write
238 * @param size The size of the block we want to write
239 * @param flag The target block must have this flag set
240 * @param input A pointer where we will read data and write to Config savegame buffer
241 * @returns ResultCode indicating the result of the operation, 0 on success
242 */
243ResultCode SetConfigInfoBlock(u32 block_id, u32 size, u32 flag, const void* input);
244
245/**
246 * Creates a block with the specified id and writes the input data to the cfg savegame buffer in
247 * memory. The config savegame file in the filesystem is not updated.
248 *
249 * @param block_id The id of the block we want to create
250 * @param size The size of the block we want to create
251 * @param flags The flags of the new block
252 * @param data A pointer containing the data we will write to the new block
253 * @returns ResultCode indicating the result of the operation, 0 on success
254 */
255ResultCode CreateConfigInfoBlk(u32 block_id, u16 size, u16 flags, const void* data);
256
257/**
258 * Deletes the config savegame file from the filesystem, the buffer in memory is not affected
259 * @returns ResultCode indicating the result of the operation, 0 on success
260 */
261ResultCode DeleteConfigNANDSaveFile();
262
263/**
264 * Writes the config savegame memory buffer to the config savegame file in the filesystem
265 * @returns ResultCode indicating the result of the operation, 0 on success
266 */
267ResultCode UpdateConfigNANDSavegame();
268
269/**
270 * Re-creates the config savegame file in memory and the filesystem with the default blocks
271 * @returns ResultCode indicating the result of the operation, 0 on success
272 */
273ResultCode FormatConfig();
274
275/**
276 * Open the config savegame file and load it to the memory buffer
277 * @returns ResultCode indicating the result of the operation, 0 on success
278 */
279ResultCode LoadConfigNANDSaveFile();
280
281/// Initialize the config service
282void Init();
283
284/// Shutdown the config service
285void Shutdown();
286
287/**
288 * Set the region code preferred by the game so that CFG will adjust to it when the region setting
289 * is auto.
290 * @param region_code the preferred region code to set
291 */
292void SetPreferredRegionCode(u32 region_code);
293
294// Utilities for frontend to set config data.
295// Note: before calling these functions, LoadConfigNANDSaveFile should be called,
296// and UpdateConfigNANDSavegame should be called after making changes to config data.
297
298/**
299 * Sets the username in config savegame.
300 * @param name the username to set. The maximum size is 10 in char16_t.
301 */
302void SetUsername(const std::u16string& name);
303
304/**
305 * Gets the username from config savegame.
306 * @returns the username
307 */
308std::u16string GetUsername();
309
310/**
311 * Sets the profile birthday in config savegame.
312 * @param month the month of birthday.
313 * @param day the day of the birthday.
314 */
315void SetBirthday(u8 month, u8 day);
316
317/**
318 * Gets the profile birthday from the config savegame.
319 * @returns a tuple of (month, day) of birthday
320 */
321std::tuple<u8, u8> GetBirthday();
322
323/**
324 * Sets the system language in config savegame.
325 * @param language the system language to set.
326 */
327void SetSystemLanguage(SystemLanguage language);
328
329/**
330 * Gets the system language from config savegame.
331 * @returns the system language
332 */
333SystemLanguage GetSystemLanguage();
334
335/**
336 * Sets the sound output mode in config savegame.
337 * @param mode the sound output mode to set
338 */
339void SetSoundOutputMode(SoundOutputMode mode);
340
341/**
342 * Gets the sound output mode from config savegame.
343 * @returns the sound output mode
344 */
345SoundOutputMode GetSoundOutputMode();
346
347/**
348 * Generates a new random console unique id.
349 * @param random_number a random generated 16bit number stored at 0x90002, used for generating the
350 * console_id
351 * @param console_id the randomly created console id
352 */
353void GenerateConsoleUniqueId(u32& random_number, u64& console_id);
354
355/**
356 * Sets the random_number and the console unique id in the config savegame.
357 * @param random_number the random_number to set
358 * @param console_id the console id to set
359 */
360ResultCode SetConsoleUniqueId(u32 random_number, u64 console_id);
361
362/**
363 * Gets the console unique id from config savegame.
364 * @returns the console unique id
365 */
366u64 GetConsoleUniqueId();
367
368} // namespace CFG
369} // namespace Service
diff --git a/src/core/hle/service/cfg/cfg_i.cpp b/src/core/hle/service/cfg/cfg_i.cpp
deleted file mode 100644
index e8db0fc42..000000000
--- a/src/core/hle/service/cfg/cfg_i.cpp
+++ /dev/null
@@ -1,64 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "core/hle/service/cfg/cfg.h"
6#include "core/hle/service/cfg/cfg_i.h"
7
8namespace Service {
9namespace CFG {
10
11const Interface::FunctionInfo FunctionTable[] = {
12 // cfg common
13 {0x00010082, GetConfigInfoBlk2, "GetConfigInfoBlk2"},
14 {0x00020000, SecureInfoGetRegion, "SecureInfoGetRegion"},
15 {0x00030040, GenHashConsoleUnique, "GenHashConsoleUnique"},
16 {0x00040000, GetRegionCanadaUSA, "GetRegionCanadaUSA"},
17 {0x00050000, GetSystemModel, "GetSystemModel"},
18 {0x00060000, GetModelNintendo2DS, "GetModelNintendo2DS"},
19 {0x00070040, nullptr, "WriteToFirstByteCfgSavegame"},
20 {0x00080080, nullptr, "GoThroughTable"},
21 {0x00090040, GetCountryCodeString, "GetCountryCodeString"},
22 {0x000A0040, GetCountryCodeID, "GetCountryCodeID"},
23 {0x000B0000, nullptr, "IsFangateSupported"},
24 // cfg:i
25 {0x04010082, GetConfigInfoBlk8, "GetConfigInfoBlk8"},
26 {0x04020082, SetConfigInfoBlk4, "SetConfigInfoBlk4"},
27 {0x04030000, UpdateConfigNANDSavegame, "UpdateConfigNANDSavegame"},
28 {0x04040042, nullptr, "GetLocalFriendCodeSeedData"},
29 {0x04050000, nullptr, "GetLocalFriendCodeSeed"},
30 {0x04060000, SecureInfoGetRegion, "SecureInfoGetRegion"},
31 {0x04070000, nullptr, "SecureInfoGetByte101"},
32 {0x04080042, nullptr, "SecureInfoGetSerialNo"},
33 {0x04090000, nullptr, "UpdateConfigBlk00040003"},
34 {0x08010082, GetConfigInfoBlk8, "GetConfigInfoBlk8"},
35 {0x08020082, SetConfigInfoBlk4, "SetConfigInfoBlk4"},
36 {0x08030000, UpdateConfigNANDSavegame, "UpdateConfigNANDSavegame"},
37 {0x080400C2, nullptr, "CreateConfigInfoBlk"},
38 {0x08050000, nullptr, "DeleteConfigNANDSavefile"},
39 {0x08060000, FormatConfig, "FormatConfig"},
40 {0x08080000, nullptr, "UpdateConfigBlk1"},
41 {0x08090000, nullptr, "UpdateConfigBlk2"},
42 {0x080A0000, nullptr, "UpdateConfigBlk3"},
43 {0x080B0082, nullptr, "SetGetLocalFriendCodeSeedData"},
44 {0x080C0042, nullptr, "SetLocalFriendCodeSeedSignature"},
45 {0x080D0000, nullptr, "DeleteCreateNANDLocalFriendCodeSeed"},
46 {0x080E0000, nullptr, "VerifySigLocalFriendCodeSeed"},
47 {0x080F0042, nullptr, "GetLocalFriendCodeSeedData"},
48 {0x08100000, nullptr, "GetLocalFriendCodeSeed"},
49 {0x08110084, nullptr, "SetSecureInfo"},
50 {0x08120000, nullptr, "DeleteCreateNANDSecureInfo"},
51 {0x08130000, nullptr, "VerifySigSecureInfo"},
52 {0x08140042, nullptr, "SecureInfoGetData"},
53 {0x08150042, nullptr, "SecureInfoGetSignature"},
54 {0x08160000, SecureInfoGetRegion, "SecureInfoGetRegion"},
55 {0x08170000, nullptr, "SecureInfoGetByte101"},
56 {0x08180042, nullptr, "SecureInfoGetSerialNo"},
57};
58
59CFG_I::CFG_I() {
60 Register(FunctionTable);
61}
62
63} // namespace CFG
64} // namespace Service \ No newline at end of file
diff --git a/src/core/hle/service/cfg/cfg_i.h b/src/core/hle/service/cfg/cfg_i.h
deleted file mode 100644
index 8cfd47633..000000000
--- a/src/core/hle/service/cfg/cfg_i.h
+++ /dev/null
@@ -1,22 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "core/hle/service/service.h"
8
9namespace Service {
10namespace CFG {
11
12class CFG_I final : public Interface {
13public:
14 CFG_I();
15
16 std::string GetPortName() const override {
17 return "cfg:i";
18 }
19};
20
21} // namespace CFG
22} // namespace Service \ No newline at end of file
diff --git a/src/core/hle/service/cfg/cfg_nor.cpp b/src/core/hle/service/cfg/cfg_nor.cpp
deleted file mode 100644
index 4ce02d115..000000000
--- a/src/core/hle/service/cfg/cfg_nor.cpp
+++ /dev/null
@@ -1,23 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "core/hle/service/cfg/cfg.h"
6#include "core/hle/service/cfg/cfg_nor.h"
7
8namespace Service {
9namespace CFG {
10
11const Interface::FunctionInfo FunctionTable[] = {
12 {0x00010040, nullptr, "Initialize"},
13 {0x00020000, nullptr, "Shutdown"},
14 {0x00050082, nullptr, "ReadData"},
15 {0x00060082, nullptr, "WriteData"},
16};
17
18CFG_NOR::CFG_NOR() {
19 Register(FunctionTable);
20}
21
22} // namespace CFG
23} // namespace Service
diff --git a/src/core/hle/service/cfg/cfg_nor.h b/src/core/hle/service/cfg/cfg_nor.h
deleted file mode 100644
index c337718e7..000000000
--- a/src/core/hle/service/cfg/cfg_nor.h
+++ /dev/null
@@ -1,22 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "core/hle/service/service.h"
8
9namespace Service {
10namespace CFG {
11
12class CFG_NOR final : public Interface {
13public:
14 CFG_NOR();
15
16 std::string GetPortName() const override {
17 return "cfg:nor";
18 }
19};
20
21} // namespace CFG
22} // namespace Service
diff --git a/src/core/hle/service/cfg/cfg_s.cpp b/src/core/hle/service/cfg/cfg_s.cpp
deleted file mode 100644
index 9386fe33d..000000000
--- a/src/core/hle/service/cfg/cfg_s.cpp
+++ /dev/null
@@ -1,41 +0,0 @@
1// Copyright 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "core/hle/service/cfg/cfg.h"
6#include "core/hle/service/cfg/cfg_s.h"
7
8namespace Service {
9namespace CFG {
10
11const Interface::FunctionInfo FunctionTable[] = {
12 // cfg common
13 {0x00010082, GetConfigInfoBlk2, "GetConfigInfoBlk2"},
14 {0x00020000, SecureInfoGetRegion, "SecureInfoGetRegion"},
15 {0x00030040, GenHashConsoleUnique, "GenHashConsoleUnique"},
16 {0x00040000, GetRegionCanadaUSA, "GetRegionCanadaUSA"},
17 {0x00050000, GetSystemModel, "GetSystemModel"},
18 {0x00060000, GetModelNintendo2DS, "GetModelNintendo2DS"},
19 {0x00070040, nullptr, "WriteToFirstByteCfgSavegame"},
20 {0x00080080, nullptr, "GoThroughTable"},
21 {0x00090040, GetCountryCodeString, "GetCountryCodeString"},
22 {0x000A0040, GetCountryCodeID, "GetCountryCodeID"},
23 {0x000B0000, nullptr, "IsFangateSupported"},
24 // cfg:s
25 {0x04010082, GetConfigInfoBlk8, "GetConfigInfoBlk8"},
26 {0x04020082, SetConfigInfoBlk4, "SetConfigInfoBlk4"},
27 {0x04030000, UpdateConfigNANDSavegame, "UpdateConfigNANDSavegame"},
28 {0x04040042, nullptr, "GetLocalFriendCodeSeedData"},
29 {0x04050000, nullptr, "GetLocalFriendCodeSeed"},
30 {0x04060000, nullptr, "SecureInfoGetRegion"},
31 {0x04070000, nullptr, "SecureInfoGetByte101"},
32 {0x04080042, nullptr, "SecureInfoGetSerialNo"},
33 {0x04090000, nullptr, "UpdateConfigBlk00040003"},
34};
35
36CFG_S::CFG_S() {
37 Register(FunctionTable);
38}
39
40} // namespace CFG
41} // namespace Service
diff --git a/src/core/hle/service/cfg/cfg_s.h b/src/core/hle/service/cfg/cfg_s.h
deleted file mode 100644
index 99fea46ee..000000000
--- a/src/core/hle/service/cfg/cfg_s.h
+++ /dev/null
@@ -1,22 +0,0 @@
1// Copyright 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "core/hle/service/service.h"
8
9namespace Service {
10namespace CFG {
11
12class CFG_S final : public Interface {
13public:
14 CFG_S();
15
16 std::string GetPortName() const override {
17 return "cfg:s";
18 }
19};
20
21} // namespace CFG
22} // namespace Service
diff --git a/src/core/hle/service/cfg/cfg_u.cpp b/src/core/hle/service/cfg/cfg_u.cpp
deleted file mode 100644
index 7b66fee22..000000000
--- a/src/core/hle/service/cfg/cfg_u.cpp
+++ /dev/null
@@ -1,31 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "core/hle/service/cfg/cfg.h"
6#include "core/hle/service/cfg/cfg_u.h"
7
8namespace Service {
9namespace CFG {
10
11const Interface::FunctionInfo FunctionTable[] = {
12 // cfg common
13 {0x00010082, GetConfigInfoBlk2, "GetConfigInfoBlk2"},
14 {0x00020000, SecureInfoGetRegion, "SecureInfoGetRegion"},
15 {0x00030040, GenHashConsoleUnique, "GenHashConsoleUnique"},
16 {0x00040000, GetRegionCanadaUSA, "GetRegionCanadaUSA"},
17 {0x00050000, GetSystemModel, "GetSystemModel"},
18 {0x00060000, GetModelNintendo2DS, "GetModelNintendo2DS"},
19 {0x00070040, nullptr, "WriteToFirstByteCfgSavegame"},
20 {0x00080080, nullptr, "GoThroughTable"},
21 {0x00090040, GetCountryCodeString, "GetCountryCodeString"},
22 {0x000A0040, GetCountryCodeID, "GetCountryCodeID"},
23 {0x000B0000, nullptr, "IsFangateSupported"},
24};
25
26CFG_U::CFG_U() {
27 Register(FunctionTable);
28}
29
30} // namespace CFG
31} // namespace Service
diff --git a/src/core/hle/service/cfg/cfg_u.h b/src/core/hle/service/cfg/cfg_u.h
deleted file mode 100644
index fc7844714..000000000
--- a/src/core/hle/service/cfg/cfg_u.h
+++ /dev/null
@@ -1,22 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "core/hle/service/service.h"
8
9namespace Service {
10namespace CFG {
11
12class CFG_U final : public Interface {
13public:
14 CFG_U();
15
16 std::string GetPortName() const override {
17 return "cfg:u";
18 }
19};
20
21} // namespace CFG
22} // namespace Service
diff --git a/src/core/hle/service/fs/archive.cpp b/src/core/hle/service/fs/archive.cpp
deleted file mode 100644
index 4ee7df73c..000000000
--- a/src/core/hle/service/fs/archive.cpp
+++ /dev/null
@@ -1,605 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <cstddef>
6#include <memory>
7#include <system_error>
8#include <type_traits>
9#include <unordered_map>
10#include <utility>
11#include <boost/container/flat_map.hpp>
12#include "common/assert.h"
13#include "common/common_types.h"
14#include "common/file_util.h"
15#include "common/logging/log.h"
16#include "core/file_sys/archive_backend.h"
17#include "core/file_sys/archive_extsavedata.h"
18#include "core/file_sys/archive_ncch.h"
19#include "core/file_sys/archive_other_savedata.h"
20#include "core/file_sys/archive_savedata.h"
21#include "core/file_sys/archive_sdmc.h"
22#include "core/file_sys/archive_sdmcwriteonly.h"
23#include "core/file_sys/archive_selfncch.h"
24#include "core/file_sys/archive_systemsavedata.h"
25#include "core/file_sys/directory_backend.h"
26#include "core/file_sys/errors.h"
27#include "core/file_sys/file_backend.h"
28#include "core/hle/ipc.h"
29#include "core/hle/kernel/client_port.h"
30#include "core/hle/kernel/client_session.h"
31#include "core/hle/kernel/handle_table.h"
32#include "core/hle/kernel/server_session.h"
33#include "core/hle/result.h"
34#include "core/hle/service/fs/archive.h"
35#include "core/hle/service/fs/fs_user.h"
36#include "core/hle/service/service.h"
37#include "core/memory.h"
38
39// Specializes std::hash for ArchiveIdCode, so that we can use it in std::unordered_map.
40// Workaroung for libstdc++ bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60970
41namespace std {
42template <>
43struct hash<Service::FS::ArchiveIdCode> {
44 typedef Service::FS::ArchiveIdCode argument_type;
45 typedef std::size_t result_type;
46
47 result_type operator()(const argument_type& id_code) const {
48 typedef std::underlying_type<argument_type>::type Type;
49 return std::hash<Type>()(static_cast<Type>(id_code));
50 }
51};
52} // namespace std
53
54static constexpr Kernel::Handle INVALID_HANDLE{};
55
56namespace Service {
57namespace FS {
58
59// Command to access archive file
60enum class FileCommand : u32 {
61 Dummy1 = 0x000100C6,
62 Control = 0x040100C4,
63 OpenSubFile = 0x08010100,
64 Read = 0x080200C2,
65 Write = 0x08030102,
66 GetSize = 0x08040000,
67 SetSize = 0x08050080,
68 GetAttributes = 0x08060000,
69 SetAttributes = 0x08070040,
70 Close = 0x08080000,
71 Flush = 0x08090000,
72 SetPriority = 0x080A0040,
73 GetPriority = 0x080B0000,
74 OpenLinkFile = 0x080C0000,
75};
76
77// Command to access directory
78enum class DirectoryCommand : u32 {
79 Dummy1 = 0x000100C6,
80 Control = 0x040100C4,
81 Read = 0x08010042,
82 Close = 0x08020000,
83};
84
85File::File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path& path)
86 : path(path), priority(0), backend(std::move(backend)) {}
87
88File::~File() {}
89
90void File::HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) {
91 using Kernel::ClientSession;
92 using Kernel::ServerSession;
93 using Kernel::SharedPtr;
94
95 u32* cmd_buff = Kernel::GetCommandBuffer();
96 FileCommand cmd = static_cast<FileCommand>(cmd_buff[0]);
97 switch (cmd) {
98
99 // Read from file...
100 case FileCommand::Read: {
101 u64 offset = cmd_buff[1] | ((u64)cmd_buff[2]) << 32;
102 u32 length = cmd_buff[3];
103 u32 address = cmd_buff[5];
104 LOG_TRACE(Service_FS, "Read %s: offset=0x%llx length=%d address=0x%x", GetName().c_str(),
105 offset, length, address);
106
107 if (offset + length > backend->GetSize()) {
108 LOG_ERROR(Service_FS,
109 "Reading from out of bounds offset=0x%llX length=0x%08X file_size=0x%llX",
110 offset, length, backend->GetSize());
111 }
112
113 std::vector<u8> data(length);
114 ResultVal<size_t> read = backend->Read(offset, data.size(), data.data());
115 if (read.Failed()) {
116 cmd_buff[1] = read.Code().raw;
117 return;
118 }
119 Memory::WriteBlock(address, data.data(), *read);
120 cmd_buff[2] = static_cast<u32>(*read);
121 break;
122 }
123
124 // Write to file...
125 case FileCommand::Write: {
126 u64 offset = cmd_buff[1] | ((u64)cmd_buff[2]) << 32;
127 u32 length = cmd_buff[3];
128 u32 flush = cmd_buff[4];
129 u32 address = cmd_buff[6];
130 LOG_TRACE(Service_FS, "Write %s: offset=0x%llx length=%d address=0x%x, flush=0x%x",
131 GetName().c_str(), offset, length, address, flush);
132
133 std::vector<u8> data(length);
134 Memory::ReadBlock(address, data.data(), data.size());
135 ResultVal<size_t> written = backend->Write(offset, data.size(), flush != 0, data.data());
136 if (written.Failed()) {
137 cmd_buff[1] = written.Code().raw;
138 return;
139 }
140 cmd_buff[2] = static_cast<u32>(*written);
141 break;
142 }
143
144 case FileCommand::GetSize: {
145 LOG_TRACE(Service_FS, "GetSize %s", GetName().c_str());
146 u64 size = backend->GetSize();
147 cmd_buff[2] = (u32)size;
148 cmd_buff[3] = size >> 32;
149 break;
150 }
151
152 case FileCommand::SetSize: {
153 u64 size = cmd_buff[1] | ((u64)cmd_buff[2] << 32);
154 LOG_TRACE(Service_FS, "SetSize %s size=%llu", GetName().c_str(), size);
155 backend->SetSize(size);
156 break;
157 }
158
159 case FileCommand::Close: {
160 LOG_TRACE(Service_FS, "Close %s", GetName().c_str());
161 backend->Close();
162 break;
163 }
164
165 case FileCommand::Flush: {
166 LOG_TRACE(Service_FS, "Flush");
167 backend->Flush();
168 break;
169 }
170
171 case FileCommand::OpenLinkFile: {
172 LOG_WARNING(Service_FS, "(STUBBED) File command OpenLinkFile %s", GetName().c_str());
173 auto sessions = ServerSession::CreateSessionPair(GetName());
174 ClientConnected(std::get<SharedPtr<ServerSession>>(sessions));
175 cmd_buff[3] = Kernel::g_handle_table.Create(std::get<SharedPtr<ClientSession>>(sessions))
176 .ValueOr(INVALID_HANDLE);
177 break;
178 }
179
180 case FileCommand::SetPriority: {
181 priority = cmd_buff[1];
182 LOG_TRACE(Service_FS, "SetPriority %u", priority);
183 break;
184 }
185
186 case FileCommand::GetPriority: {
187 cmd_buff[2] = priority;
188 LOG_TRACE(Service_FS, "GetPriority");
189 break;
190 }
191
192 // Unknown command...
193 default:
194 LOG_ERROR(Service_FS, "Unknown command=0x%08X!", cmd);
195 ResultCode error = UnimplementedFunction(ErrorModule::FS);
196 cmd_buff[1] = error.raw; // TODO(Link Mauve): use the correct error code for that.
197 return;
198 }
199 cmd_buff[1] = RESULT_SUCCESS.raw; // No error
200}
201
202Directory::Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend,
203 const FileSys::Path& path)
204 : path(path), backend(std::move(backend)) {}
205
206Directory::~Directory() {}
207
208void Directory::HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) {
209 u32* cmd_buff = Kernel::GetCommandBuffer();
210 DirectoryCommand cmd = static_cast<DirectoryCommand>(cmd_buff[0]);
211 switch (cmd) {
212 // Read from directory...
213 case DirectoryCommand::Read: {
214 u32 count = cmd_buff[1];
215 u32 address = cmd_buff[3];
216 std::vector<FileSys::Entry> entries(count);
217 LOG_TRACE(Service_FS, "Read %s: count=%d", GetName().c_str(), count);
218
219 // Number of entries actually read
220 u32 read = backend->Read(static_cast<u32>(entries.size()), entries.data());
221 cmd_buff[2] = read;
222 Memory::WriteBlock(address, entries.data(), read * sizeof(FileSys::Entry));
223 break;
224 }
225
226 case DirectoryCommand::Close: {
227 LOG_TRACE(Service_FS, "Close %s", GetName().c_str());
228 backend->Close();
229 break;
230 }
231
232 // Unknown command...
233 default:
234 LOG_ERROR(Service_FS, "Unknown command=0x%08X!", cmd);
235 ResultCode error = UnimplementedFunction(ErrorModule::FS);
236 cmd_buff[1] = error.raw; // TODO(Link Mauve): use the correct error code for that.
237 return;
238 }
239 cmd_buff[1] = RESULT_SUCCESS.raw; // No error
240}
241
242////////////////////////////////////////////////////////////////////////////////////////////////////
243
244using FileSys::ArchiveBackend;
245using FileSys::ArchiveFactory;
246
247/**
248 * Map of registered archives, identified by id code. Once an archive is registered here, it is
249 * never removed until UnregisterArchiveTypes is called.
250 */
251static boost::container::flat_map<ArchiveIdCode, std::unique_ptr<ArchiveFactory>> id_code_map;
252
253/**
254 * Map of active archive handles. Values are pointers to the archives in `idcode_map`.
255 */
256static std::unordered_map<ArchiveHandle, std::unique_ptr<ArchiveBackend>> handle_map;
257static ArchiveHandle next_handle;
258
259static ArchiveBackend* GetArchive(ArchiveHandle handle) {
260 auto itr = handle_map.find(handle);
261 return (itr == handle_map.end()) ? nullptr : itr->second.get();
262}
263
264ResultVal<ArchiveHandle> OpenArchive(ArchiveIdCode id_code, FileSys::Path& archive_path) {
265 LOG_TRACE(Service_FS, "Opening archive with id code 0x%08X", id_code);
266
267 auto itr = id_code_map.find(id_code);
268 if (itr == id_code_map.end()) {
269 return FileSys::ERROR_NOT_FOUND;
270 }
271
272 CASCADE_RESULT(std::unique_ptr<ArchiveBackend> res, itr->second->Open(archive_path));
273
274 // This should never even happen in the first place with 64-bit handles,
275 while (handle_map.count(next_handle) != 0) {
276 ++next_handle;
277 }
278 handle_map.emplace(next_handle, std::move(res));
279 return MakeResult<ArchiveHandle>(next_handle++);
280}
281
282ResultCode CloseArchive(ArchiveHandle handle) {
283 if (handle_map.erase(handle) == 0)
284 return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
285 else
286 return RESULT_SUCCESS;
287}
288
289// TODO(yuriks): This might be what the fs:REG service is for. See the Register/Unregister calls in
290// http://3dbrew.org/wiki/Filesystem_services#ProgramRegistry_service_.22fs:REG.22
291ResultCode RegisterArchiveType(std::unique_ptr<FileSys::ArchiveFactory>&& factory,
292 ArchiveIdCode id_code) {
293 auto result = id_code_map.emplace(id_code, std::move(factory));
294
295 bool inserted = result.second;
296 ASSERT_MSG(inserted, "Tried to register more than one archive with same id code");
297
298 auto& archive = result.first->second;
299 LOG_DEBUG(Service_FS, "Registered archive %s with id code 0x%08X", archive->GetName().c_str(),
300 id_code);
301 return RESULT_SUCCESS;
302}
303
304ResultVal<std::shared_ptr<File>> OpenFileFromArchive(ArchiveHandle archive_handle,
305 const FileSys::Path& path,
306 const FileSys::Mode mode) {
307 ArchiveBackend* archive = GetArchive(archive_handle);
308 if (archive == nullptr)
309 return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
310
311 auto backend = archive->OpenFile(path, mode);
312 if (backend.Failed())
313 return backend.Code();
314
315 auto file = std::shared_ptr<File>(new File(std::move(backend).Unwrap(), path));
316 return MakeResult<std::shared_ptr<File>>(std::move(file));
317}
318
319ResultCode DeleteFileFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) {
320 ArchiveBackend* archive = GetArchive(archive_handle);
321 if (archive == nullptr)
322 return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
323
324 return archive->DeleteFile(path);
325}
326
327ResultCode RenameFileBetweenArchives(ArchiveHandle src_archive_handle,
328 const FileSys::Path& src_path,
329 ArchiveHandle dest_archive_handle,
330 const FileSys::Path& dest_path) {
331 ArchiveBackend* src_archive = GetArchive(src_archive_handle);
332 ArchiveBackend* dest_archive = GetArchive(dest_archive_handle);
333 if (src_archive == nullptr || dest_archive == nullptr)
334 return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
335
336 if (src_archive == dest_archive) {
337 return src_archive->RenameFile(src_path, dest_path);
338 } else {
339 // TODO: Implement renaming across archives
340 return UnimplementedFunction(ErrorModule::FS);
341 }
342}
343
344ResultCode DeleteDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) {
345 ArchiveBackend* archive = GetArchive(archive_handle);
346 if (archive == nullptr)
347 return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
348
349 return archive->DeleteDirectory(path);
350}
351
352ResultCode DeleteDirectoryRecursivelyFromArchive(ArchiveHandle archive_handle,
353 const FileSys::Path& path) {
354 ArchiveBackend* archive = GetArchive(archive_handle);
355 if (archive == nullptr)
356 return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
357
358 return archive->DeleteDirectoryRecursively(path);
359}
360
361ResultCode CreateFileInArchive(ArchiveHandle archive_handle, const FileSys::Path& path,
362 u64 file_size) {
363 ArchiveBackend* archive = GetArchive(archive_handle);
364 if (archive == nullptr)
365 return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
366
367 return archive->CreateFile(path, file_size);
368}
369
370ResultCode CreateDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) {
371 ArchiveBackend* archive = GetArchive(archive_handle);
372 if (archive == nullptr)
373 return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
374
375 return archive->CreateDirectory(path);
376}
377
378ResultCode RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle,
379 const FileSys::Path& src_path,
380 ArchiveHandle dest_archive_handle,
381 const FileSys::Path& dest_path) {
382 ArchiveBackend* src_archive = GetArchive(src_archive_handle);
383 ArchiveBackend* dest_archive = GetArchive(dest_archive_handle);
384 if (src_archive == nullptr || dest_archive == nullptr)
385 return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
386
387 if (src_archive == dest_archive) {
388 return src_archive->RenameDirectory(src_path, dest_path);
389 } else {
390 // TODO: Implement renaming across archives
391 return UnimplementedFunction(ErrorModule::FS);
392 }
393}
394
395ResultVal<std::shared_ptr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle,
396 const FileSys::Path& path) {
397 ArchiveBackend* archive = GetArchive(archive_handle);
398 if (archive == nullptr)
399 return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
400
401 auto backend = archive->OpenDirectory(path);
402 if (backend.Failed())
403 return backend.Code();
404
405 auto directory = std::shared_ptr<Directory>(new Directory(std::move(backend).Unwrap(), path));
406 return MakeResult<std::shared_ptr<Directory>>(std::move(directory));
407}
408
409ResultVal<u64> GetFreeBytesInArchive(ArchiveHandle archive_handle) {
410 ArchiveBackend* archive = GetArchive(archive_handle);
411 if (archive == nullptr)
412 return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
413 return MakeResult<u64>(archive->GetFreeBytes());
414}
415
416ResultCode FormatArchive(ArchiveIdCode id_code, const FileSys::ArchiveFormatInfo& format_info,
417 const FileSys::Path& path) {
418 auto archive_itr = id_code_map.find(id_code);
419 if (archive_itr == id_code_map.end()) {
420 return UnimplementedFunction(ErrorModule::FS); // TODO(Subv): Find the right error
421 }
422
423 return archive_itr->second->Format(path, format_info);
424}
425
426ResultVal<FileSys::ArchiveFormatInfo> GetArchiveFormatInfo(ArchiveIdCode id_code,
427 FileSys::Path& archive_path) {
428 auto archive = id_code_map.find(id_code);
429 if (archive == id_code_map.end()) {
430 return UnimplementedFunction(ErrorModule::FS); // TODO(Subv): Find the right error
431 }
432
433 return archive->second->GetFormatInfo(archive_path);
434}
435
436ResultCode CreateExtSaveData(MediaType media_type, u32 high, u32 low, VAddr icon_buffer,
437 u32 icon_size, const FileSys::ArchiveFormatInfo& format_info) {
438 // Construct the binary path to the archive first
439 FileSys::Path path =
440 FileSys::ConstructExtDataBinaryPath(static_cast<u32>(media_type), high, low);
441
442 auto archive = id_code_map.find(media_type == MediaType::NAND ? ArchiveIdCode::SharedExtSaveData
443 : ArchiveIdCode::ExtSaveData);
444
445 if (archive == id_code_map.end()) {
446 return UnimplementedFunction(ErrorModule::FS); // TODO(Subv): Find the right error
447 }
448
449 auto ext_savedata = static_cast<FileSys::ArchiveFactory_ExtSaveData*>(archive->second.get());
450
451 ResultCode result = ext_savedata->Format(path, format_info);
452 if (result.IsError())
453 return result;
454
455 if (!Memory::IsValidVirtualAddress(icon_buffer))
456 return ResultCode(-1); // TODO(Subv): Find the right error code
457
458 std::vector<u8> smdh_icon(icon_size);
459 Memory::ReadBlock(icon_buffer, smdh_icon.data(), smdh_icon.size());
460 ext_savedata->WriteIcon(path, smdh_icon.data(), smdh_icon.size());
461 return RESULT_SUCCESS;
462}
463
464ResultCode DeleteExtSaveData(MediaType media_type, u32 high, u32 low) {
465 // Construct the binary path to the archive first
466 FileSys::Path path =
467 FileSys::ConstructExtDataBinaryPath(static_cast<u32>(media_type), high, low);
468
469 std::string media_type_directory;
470 if (media_type == MediaType::NAND) {
471 media_type_directory = FileUtil::GetUserPath(D_NAND_IDX);
472 } else if (media_type == MediaType::SDMC) {
473 media_type_directory = FileUtil::GetUserPath(D_SDMC_IDX);
474 } else {
475 LOG_ERROR(Service_FS, "Unsupported media type %u", media_type);
476 return ResultCode(-1); // TODO(Subv): Find the right error code
477 }
478
479 // Delete all directories (/user, /boss) and the icon file.
480 std::string base_path =
481 FileSys::GetExtDataContainerPath(media_type_directory, media_type == MediaType::NAND);
482 std::string extsavedata_path = FileSys::GetExtSaveDataPath(base_path, path);
483 if (FileUtil::Exists(extsavedata_path) && !FileUtil::DeleteDirRecursively(extsavedata_path))
484 return ResultCode(-1); // TODO(Subv): Find the right error code
485 return RESULT_SUCCESS;
486}
487
488ResultCode DeleteSystemSaveData(u32 high, u32 low) {
489 // Construct the binary path to the archive first
490 FileSys::Path path = FileSys::ConstructSystemSaveDataBinaryPath(high, low);
491
492 std::string nand_directory = FileUtil::GetUserPath(D_NAND_IDX);
493 std::string base_path = FileSys::GetSystemSaveDataContainerPath(nand_directory);
494 std::string systemsavedata_path = FileSys::GetSystemSaveDataPath(base_path, path);
495 if (!FileUtil::DeleteDirRecursively(systemsavedata_path))
496 return ResultCode(-1); // TODO(Subv): Find the right error code
497 return RESULT_SUCCESS;
498}
499
500ResultCode CreateSystemSaveData(u32 high, u32 low) {
501 // Construct the binary path to the archive first
502 FileSys::Path path = FileSys::ConstructSystemSaveDataBinaryPath(high, low);
503
504 std::string nand_directory = FileUtil::GetUserPath(D_NAND_IDX);
505 std::string base_path = FileSys::GetSystemSaveDataContainerPath(nand_directory);
506 std::string systemsavedata_path = FileSys::GetSystemSaveDataPath(base_path, path);
507 if (!FileUtil::CreateFullPath(systemsavedata_path))
508 return ResultCode(-1); // TODO(Subv): Find the right error code
509 return RESULT_SUCCESS;
510}
511
512void RegisterArchiveTypes() {
513 // TODO(Subv): Add the other archive types (see here for the known types:
514 // http://3dbrew.org/wiki/FS:OpenArchive#Archive_idcodes).
515
516 std::string sdmc_directory = FileUtil::GetUserPath(D_SDMC_IDX);
517 std::string nand_directory = FileUtil::GetUserPath(D_NAND_IDX);
518 auto sdmc_factory = std::make_unique<FileSys::ArchiveFactory_SDMC>(sdmc_directory);
519 if (sdmc_factory->Initialize())
520 RegisterArchiveType(std::move(sdmc_factory), ArchiveIdCode::SDMC);
521 else
522 LOG_ERROR(Service_FS, "Can't instantiate SDMC archive with path %s",
523 sdmc_directory.c_str());
524
525 auto sdmcwo_factory = std::make_unique<FileSys::ArchiveFactory_SDMCWriteOnly>(sdmc_directory);
526 if (sdmcwo_factory->Initialize())
527 RegisterArchiveType(std::move(sdmcwo_factory), ArchiveIdCode::SDMCWriteOnly);
528 else
529 LOG_ERROR(Service_FS, "Can't instantiate SDMCWriteOnly archive with path %s",
530 sdmc_directory.c_str());
531
532 // Create the SaveData archive
533 auto sd_savedata_source = std::make_shared<FileSys::ArchiveSource_SDSaveData>(sdmc_directory);
534 auto savedata_factory = std::make_unique<FileSys::ArchiveFactory_SaveData>(sd_savedata_source);
535 RegisterArchiveType(std::move(savedata_factory), ArchiveIdCode::SaveData);
536 auto other_savedata_permitted_factory =
537 std::make_unique<FileSys::ArchiveFactory_OtherSaveDataPermitted>(sd_savedata_source);
538 RegisterArchiveType(std::move(other_savedata_permitted_factory),
539 ArchiveIdCode::OtherSaveDataPermitted);
540 auto other_savedata_general_factory =
541 std::make_unique<FileSys::ArchiveFactory_OtherSaveDataGeneral>(sd_savedata_source);
542 RegisterArchiveType(std::move(other_savedata_general_factory),
543 ArchiveIdCode::OtherSaveDataGeneral);
544
545 auto extsavedata_factory =
546 std::make_unique<FileSys::ArchiveFactory_ExtSaveData>(sdmc_directory, false);
547 if (extsavedata_factory->Initialize())
548 RegisterArchiveType(std::move(extsavedata_factory), ArchiveIdCode::ExtSaveData);
549 else
550 LOG_ERROR(Service_FS, "Can't instantiate ExtSaveData archive with path %s",
551 extsavedata_factory->GetMountPoint().c_str());
552
553 auto sharedextsavedata_factory =
554 std::make_unique<FileSys::ArchiveFactory_ExtSaveData>(nand_directory, true);
555 if (sharedextsavedata_factory->Initialize())
556 RegisterArchiveType(std::move(sharedextsavedata_factory), ArchiveIdCode::SharedExtSaveData);
557 else
558 LOG_ERROR(Service_FS, "Can't instantiate SharedExtSaveData archive with path %s",
559 sharedextsavedata_factory->GetMountPoint().c_str());
560
561 // Create the NCCH archive, basically a small variation of the RomFS archive
562 auto savedatacheck_factory = std::make_unique<FileSys::ArchiveFactory_NCCH>(nand_directory);
563 RegisterArchiveType(std::move(savedatacheck_factory), ArchiveIdCode::NCCH);
564
565 auto systemsavedata_factory =
566 std::make_unique<FileSys::ArchiveFactory_SystemSaveData>(nand_directory);
567 RegisterArchiveType(std::move(systemsavedata_factory), ArchiveIdCode::SystemSaveData);
568
569 auto selfncch_factory = std::make_unique<FileSys::ArchiveFactory_SelfNCCH>();
570 RegisterArchiveType(std::move(selfncch_factory), ArchiveIdCode::SelfNCCH);
571}
572
573void RegisterSelfNCCH(Loader::AppLoader& app_loader) {
574 auto itr = id_code_map.find(ArchiveIdCode::SelfNCCH);
575 if (itr == id_code_map.end()) {
576 LOG_ERROR(Service_FS,
577 "Could not register a new NCCH because the SelfNCCH archive hasn't been created");
578 return;
579 }
580
581 auto* factory = static_cast<FileSys::ArchiveFactory_SelfNCCH*>(itr->second.get());
582 factory->Register(app_loader);
583}
584
585void UnregisterArchiveTypes() {
586 id_code_map.clear();
587}
588
589/// Initialize archives
590void ArchiveInit() {
591 next_handle = 1;
592
593 AddService(new FS::Interface);
594
595 RegisterArchiveTypes();
596}
597
598/// Shutdown archives
599void ArchiveShutdown() {
600 handle_map.clear();
601 UnregisterArchiveTypes();
602}
603
604} // namespace FS
605} // namespace Service
diff --git a/src/core/hle/service/fs/archive.h b/src/core/hle/service/fs/archive.h
deleted file mode 100644
index e3c8fc2ef..000000000
--- a/src/core/hle/service/fs/archive.h
+++ /dev/null
@@ -1,276 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <memory>
8#include <string>
9#include "common/common_types.h"
10#include "core/file_sys/archive_backend.h"
11#include "core/hle/kernel/hle_ipc.h"
12#include "core/hle/result.h"
13
14namespace FileSys {
15class DirectoryBackend;
16class FileBackend;
17}
18
19/// The unique system identifier hash, also known as ID0
20static constexpr char SYSTEM_ID[]{"00000000000000000000000000000000"};
21/// The scrambled SD card CID, also known as ID1
22static constexpr char SDCARD_ID[]{"00000000000000000000000000000000"};
23
24namespace Loader {
25class AppLoader;
26}
27
28namespace Service {
29namespace FS {
30
31/// Supported archive types
32enum class ArchiveIdCode : u32 {
33 SelfNCCH = 0x00000003,
34 SaveData = 0x00000004,
35 ExtSaveData = 0x00000006,
36 SharedExtSaveData = 0x00000007,
37 SystemSaveData = 0x00000008,
38 SDMC = 0x00000009,
39 SDMCWriteOnly = 0x0000000A,
40 NCCH = 0x2345678A,
41 OtherSaveDataGeneral = 0x567890B2,
42 OtherSaveDataPermitted = 0x567890B4,
43};
44
45/// Media types for the archives
46enum class MediaType : u32 { NAND = 0, SDMC = 1, GameCard = 2 };
47
48typedef u64 ArchiveHandle;
49
50class File final : public Kernel::SessionRequestHandler {
51public:
52 File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path& path);
53 ~File();
54
55 std::string GetName() const {
56 return "Path: " + path.DebugStr();
57 }
58
59 FileSys::Path path; ///< Path of the file
60 u32 priority; ///< Priority of the file. TODO(Subv): Find out what this means
61 std::unique_ptr<FileSys::FileBackend> backend; ///< File backend interface
62
63protected:
64 void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
65};
66
67class Directory final : public Kernel::SessionRequestHandler {
68public:
69 Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend, const FileSys::Path& path);
70 ~Directory();
71
72 std::string GetName() const {
73 return "Directory: " + path.DebugStr();
74 }
75
76 FileSys::Path path; ///< Path of the directory
77 std::unique_ptr<FileSys::DirectoryBackend> backend; ///< File backend interface
78
79protected:
80 void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
81};
82
83/**
84 * Opens an archive
85 * @param id_code IdCode of the archive to open
86 * @param archive_path Path to the archive, used with Binary paths
87 * @return Handle to the opened archive
88 */
89ResultVal<ArchiveHandle> OpenArchive(ArchiveIdCode id_code, FileSys::Path& archive_path);
90
91/**
92 * Closes an archive
93 * @param handle Handle to the archive to close
94 */
95ResultCode CloseArchive(ArchiveHandle handle);
96
97/**
98 * Registers an Archive type, instances of which can later be opened using its IdCode.
99 * @param factory File system backend interface to the archive
100 * @param id_code Id code used to access this type of archive
101 */
102ResultCode RegisterArchiveType(std::unique_ptr<FileSys::ArchiveFactory>&& factory,
103 ArchiveIdCode id_code);
104
105/**
106 * Open a File from an Archive
107 * @param archive_handle Handle to an open Archive object
108 * @param path Path to the File inside of the Archive
109 * @param mode Mode under which to open the File
110 * @return The opened File object
111 */
112ResultVal<std::shared_ptr<File>> OpenFileFromArchive(ArchiveHandle archive_handle,
113 const FileSys::Path& path,
114 const FileSys::Mode mode);
115
116/**
117 * Delete a File from an Archive
118 * @param archive_handle Handle to an open Archive object
119 * @param path Path to the File inside of the Archive
120 * @return Whether deletion succeeded
121 */
122ResultCode DeleteFileFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path);
123
124/**
125 * Rename a File between two Archives
126 * @param src_archive_handle Handle to the source Archive object
127 * @param src_path Path to the File inside of the source Archive
128 * @param dest_archive_handle Handle to the destination Archive object
129 * @param dest_path Path to the File inside of the destination Archive
130 * @return Whether rename succeeded
131 */
132ResultCode RenameFileBetweenArchives(ArchiveHandle src_archive_handle,
133 const FileSys::Path& src_path,
134 ArchiveHandle dest_archive_handle,
135 const FileSys::Path& dest_path);
136
137/**
138 * Delete a Directory from an Archive
139 * @param archive_handle Handle to an open Archive object
140 * @param path Path to the Directory inside of the Archive
141 * @return Whether deletion succeeded
142 */
143ResultCode DeleteDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path);
144
145/**
146 * Delete a Directory and anything under it from an Archive
147 * @param archive_handle Handle to an open Archive object
148 * @param path Path to the Directory inside of the Archive
149 * @return Whether deletion succeeded
150 */
151ResultCode DeleteDirectoryRecursivelyFromArchive(ArchiveHandle archive_handle,
152 const FileSys::Path& path);
153
154/**
155 * Create a File in an Archive
156 * @param archive_handle Handle to an open Archive object
157 * @param path Path to the File inside of the Archive
158 * @param file_size The size of the new file, filled with zeroes
159 * @return File creation result code
160 */
161ResultCode CreateFileInArchive(ArchiveHandle archive_handle, const FileSys::Path& path,
162 u64 file_size);
163
164/**
165 * Create a Directory from an Archive
166 * @param archive_handle Handle to an open Archive object
167 * @param path Path to the Directory inside of the Archive
168 * @return Whether creation of directory succeeded
169 */
170ResultCode CreateDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path);
171
172/**
173 * Rename a Directory between two Archives
174 * @param src_archive_handle Handle to the source Archive object
175 * @param src_path Path to the Directory inside of the source Archive
176 * @param dest_archive_handle Handle to the destination Archive object
177 * @param dest_path Path to the Directory inside of the destination Archive
178 * @return Whether rename succeeded
179 */
180ResultCode RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle,
181 const FileSys::Path& src_path,
182 ArchiveHandle dest_archive_handle,
183 const FileSys::Path& dest_path);
184
185/**
186 * Open a Directory from an Archive
187 * @param archive_handle Handle to an open Archive object
188 * @param path Path to the Directory inside of the Archive
189 * @return The opened Directory object
190 */
191ResultVal<std::shared_ptr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle,
192 const FileSys::Path& path);
193
194/**
195 * Get the free space in an Archive
196 * @param archive_handle Handle to an open Archive object
197 * @return The number of free bytes in the archive
198 */
199ResultVal<u64> GetFreeBytesInArchive(ArchiveHandle archive_handle);
200
201/**
202 * Erases the contents of the physical folder that contains the archive
203 * identified by the specified id code and path
204 * @param id_code The id of the archive to format
205 * @param format_info Format information about the new archive
206 * @param path The path to the archive, if relevant.
207 * @return ResultCode 0 on success or the corresponding code on error
208 */
209ResultCode FormatArchive(ArchiveIdCode id_code, const FileSys::ArchiveFormatInfo& format_info,
210 const FileSys::Path& path = FileSys::Path());
211
212/**
213 * Retrieves the format info about the archive of the specified type and path.
214 * The format info is supplied by the client code when creating archives.
215 * @param id_code The id of the archive
216 * @param archive_path The path of the archive, if relevant
217 * @return The format info of the archive, or the corresponding error code if failed.
218 */
219ResultVal<FileSys::ArchiveFormatInfo> GetArchiveFormatInfo(ArchiveIdCode id_code,
220 FileSys::Path& archive_path);
221
222/**
223 * Creates a blank SharedExtSaveData archive for the specified extdata ID
224 * @param media_type The media type of the archive to create (NAND / SDMC)
225 * @param high The high word of the extdata id to create
226 * @param low The low word of the extdata id to create
227 * @param icon_buffer VAddr of the SMDH icon for this ExtSaveData
228 * @param icon_size Size of the SMDH icon
229 * @param format_info Format information about the new archive
230 * @return ResultCode 0 on success or the corresponding code on error
231 */
232ResultCode CreateExtSaveData(MediaType media_type, u32 high, u32 low, VAddr icon_buffer,
233 u32 icon_size, const FileSys::ArchiveFormatInfo& format_info);
234
235/**
236 * Deletes the SharedExtSaveData archive for the specified extdata ID
237 * @param media_type The media type of the archive to delete (NAND / SDMC)
238 * @param high The high word of the extdata id to delete
239 * @param low The low word of the extdata id to delete
240 * @return ResultCode 0 on success or the corresponding code on error
241 */
242ResultCode DeleteExtSaveData(MediaType media_type, u32 high, u32 low);
243
244/**
245 * Deletes the SystemSaveData archive folder for the specified save data id
246 * @param high The high word of the SystemSaveData archive to delete
247 * @param low The low word of the SystemSaveData archive to delete
248 * @return ResultCode 0 on success or the corresponding code on error
249 */
250ResultCode DeleteSystemSaveData(u32 high, u32 low);
251
252/**
253 * Creates the SystemSaveData archive folder for the specified save data id
254 * @param high The high word of the SystemSaveData archive to create
255 * @param low The low word of the SystemSaveData archive to create
256 * @return ResultCode 0 on success or the corresponding code on error
257 */
258ResultCode CreateSystemSaveData(u32 high, u32 low);
259
260/// Initialize archives
261void ArchiveInit();
262
263/// Shutdown archives
264void ArchiveShutdown();
265
266/// Registers a new NCCH file with the SelfNCCH archive factory
267void RegisterSelfNCCH(Loader::AppLoader& app_loader);
268
269/// Register all archive types
270void RegisterArchiveTypes();
271
272/// Unregister all archive types
273void UnregisterArchiveTypes();
274
275} // namespace FS
276} // namespace Service
diff --git a/src/core/hle/service/fs/fs_user.cpp b/src/core/hle/service/fs/fs_user.cpp
deleted file mode 100644
index b9eab7838..000000000
--- a/src/core/hle/service/fs/fs_user.cpp
+++ /dev/null
@@ -1,1045 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "common/assert.h"
6#include "common/common_types.h"
7#include "common/file_util.h"
8#include "common/logging/log.h"
9#include "common/scope_exit.h"
10#include "common/string_util.h"
11#include "core/core.h"
12#include "core/file_sys/errors.h"
13#include "core/hle/ipc.h"
14#include "core/hle/ipc_helpers.h"
15#include "core/hle/kernel/client_port.h"
16#include "core/hle/kernel/client_session.h"
17#include "core/hle/kernel/server_session.h"
18#include "core/hle/result.h"
19#include "core/hle/service/fs/archive.h"
20#include "core/hle/service/fs/fs_user.h"
21#include "core/settings.h"
22
23////////////////////////////////////////////////////////////////////////////////////////////////////
24// Namespace FS_User
25
26using Kernel::ClientSession;
27using Kernel::ServerSession;
28using Kernel::SharedPtr;
29
30namespace Service {
31namespace FS {
32
33static u32 priority = -1; ///< For SetPriority and GetPriority service functions
34
35static ArchiveHandle MakeArchiveHandle(u32 low_word, u32 high_word) {
36 return (u64)low_word | ((u64)high_word << 32);
37}
38
39static void Initialize(Service::Interface* self) {
40 u32* cmd_buff = Kernel::GetCommandBuffer();
41
42 // TODO(Link Mauve): check the behavior when cmd_buff[1] isn't 32, as per
43 // http://3dbrew.org/wiki/FS:Initialize#Request
44 cmd_buff[1] = RESULT_SUCCESS.raw;
45}
46
47/**
48 * FS_User::OpenFile service function
49 * Inputs:
50 * 1 : Transaction
51 * 2 : Archive handle lower word
52 * 3 : Archive handle upper word
53 * 4 : Low path type
54 * 5 : Low path size
55 * 6 : Open flags
56 * 7 : Attributes
57 * 8 : (LowPathSize << 14) | 2
58 * 9 : Low path data pointer
59 * Outputs:
60 * 1 : Result of function, 0 on success, otherwise error code
61 * 3 : File handle
62 */
63static void OpenFile(Service::Interface* self) {
64 // The helper should be passed by argument to the function
65 IPC::RequestParser rp(Kernel::GetCommandBuffer(), {0x080201C2});
66 rp.Pop<u32>(); // Always 0 ?
67
68 ArchiveHandle archive_handle = rp.Pop<u64>();
69 auto filename_type = static_cast<FileSys::LowPathType>(rp.Pop<u32>());
70 u32 filename_size = rp.Pop<u32>();
71 FileSys::Mode mode;
72 mode.hex = rp.Pop<u32>();
73 u32 attributes = rp.Pop<u32>(); // TODO(Link Mauve): do something with those attributes.
74 VAddr filename_ptr = rp.PopStaticBuffer();
75 FileSys::Path file_path(filename_type, filename_size, filename_ptr);
76
77 LOG_DEBUG(Service_FS, "path=%s, mode=%u attrs=%u", file_path.DebugStr().c_str(), mode.hex,
78 attributes);
79
80 ResultVal<std::shared_ptr<File>> file_res =
81 OpenFileFromArchive(archive_handle, file_path, mode);
82 IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
83 rb.Push(file_res.Code());
84 if (file_res.Succeeded()) {
85 std::shared_ptr<File> file = *file_res;
86 auto sessions = ServerSession::CreateSessionPair(file->GetName());
87 file->ClientConnected(std::get<SharedPtr<ServerSession>>(sessions));
88
89 rb.PushMoveHandles(
90 Kernel::g_handle_table.Create(std::get<SharedPtr<ClientSession>>(sessions)).Unwrap());
91 } else {
92 rb.PushMoveHandles(0);
93 LOG_ERROR(Service_FS, "failed to get a handle for file %s", file_path.DebugStr().c_str());
94 }
95}
96
97/**
98 * FS_User::OpenFileDirectly service function
99 * Inputs:
100 * 1 : Transaction
101 * 2 : Archive ID
102 * 3 : Archive low path type
103 * 4 : Archive low path size
104 * 5 : File low path type
105 * 6 : File low path size
106 * 7 : Flags
107 * 8 : Attributes
108 * 9 : (ArchiveLowPathSize << 14) | 0x802
109 * 10 : Archive low path
110 * 11 : (FileLowPathSize << 14) | 2
111 * 12 : File low path
112 * Outputs:
113 * 1 : Result of function, 0 on success, otherwise error code
114 * 3 : File handle
115 */
116static void OpenFileDirectly(Service::Interface* self) {
117 u32* cmd_buff = Kernel::GetCommandBuffer();
118
119 auto archive_id = static_cast<FS::ArchiveIdCode>(cmd_buff[2]);
120 auto archivename_type = static_cast<FileSys::LowPathType>(cmd_buff[3]);
121 u32 archivename_size = cmd_buff[4];
122 auto filename_type = static_cast<FileSys::LowPathType>(cmd_buff[5]);
123 u32 filename_size = cmd_buff[6];
124 FileSys::Mode mode;
125 mode.hex = cmd_buff[7];
126 u32 attributes = cmd_buff[8]; // TODO(Link Mauve): do something with those attributes.
127 u32 archivename_ptr = cmd_buff[10];
128 u32 filename_ptr = cmd_buff[12];
129 FileSys::Path archive_path(archivename_type, archivename_size, archivename_ptr);
130 FileSys::Path file_path(filename_type, filename_size, filename_ptr);
131
132 LOG_DEBUG(Service_FS, "archive_id=0x%08X archive_path=%s file_path=%s, mode=%u attributes=%u",
133 static_cast<u32>(archive_id), archive_path.DebugStr().c_str(),
134 file_path.DebugStr().c_str(), mode.hex, attributes);
135
136 ResultVal<ArchiveHandle> archive_handle = OpenArchive(archive_id, archive_path);
137 if (archive_handle.Failed()) {
138 LOG_ERROR(Service_FS,
139 "Failed to get a handle for archive archive_id=0x%08X archive_path=%s",
140 static_cast<u32>(archive_id), archive_path.DebugStr().c_str());
141 cmd_buff[1] = archive_handle.Code().raw;
142 cmd_buff[3] = 0;
143 return;
144 }
145 SCOPE_EXIT({ CloseArchive(*archive_handle); });
146
147 ResultVal<std::shared_ptr<File>> file_res =
148 OpenFileFromArchive(*archive_handle, file_path, mode);
149 cmd_buff[1] = file_res.Code().raw;
150 if (file_res.Succeeded()) {
151 std::shared_ptr<File> file = *file_res;
152 auto sessions = ServerSession::CreateSessionPair(file->GetName());
153 file->ClientConnected(std::get<SharedPtr<ServerSession>>(sessions));
154
155 cmd_buff[3] =
156 Kernel::g_handle_table.Create(std::get<SharedPtr<ClientSession>>(sessions)).Unwrap();
157 } else {
158 cmd_buff[3] = 0;
159 LOG_ERROR(Service_FS, "failed to get a handle for file %s mode=%u attributes=%u",
160 file_path.DebugStr().c_str(), mode.hex, attributes);
161 }
162}
163
164/*
165 * FS_User::DeleteFile service function
166 * Inputs:
167 * 2 : Archive handle lower word
168 * 3 : Archive handle upper word
169 * 4 : File path string type
170 * 5 : File path string size
171 * 7 : File path string data
172 * Outputs:
173 * 1 : Result of function, 0 on success, otherwise error code
174 */
175static void DeleteFile(Service::Interface* self) {
176 u32* cmd_buff = Kernel::GetCommandBuffer();
177
178 ArchiveHandle archive_handle = MakeArchiveHandle(cmd_buff[2], cmd_buff[3]);
179 auto filename_type = static_cast<FileSys::LowPathType>(cmd_buff[4]);
180 u32 filename_size = cmd_buff[5];
181 u32 filename_ptr = cmd_buff[7];
182
183 FileSys::Path file_path(filename_type, filename_size, filename_ptr);
184
185 LOG_DEBUG(Service_FS, "type=%u size=%u data=%s", static_cast<u32>(filename_type), filename_size,
186 file_path.DebugStr().c_str());
187
188 cmd_buff[1] = DeleteFileFromArchive(archive_handle, file_path).raw;
189}
190
191/*
192 * FS_User::RenameFile service function
193 * Inputs:
194 * 2 : Source archive handle lower word
195 * 3 : Source archive handle upper word
196 * 4 : Source file path type
197 * 5 : Source file path size
198 * 6 : Dest archive handle lower word
199 * 7 : Dest archive handle upper word
200 * 8 : Dest file path type
201 * 9 : Dest file path size
202 * 11: Source file path string data
203 * 13: Dest file path string
204 * Outputs:
205 * 1 : Result of function, 0 on success, otherwise error code
206 */
207static void RenameFile(Service::Interface* self) {
208 u32* cmd_buff = Kernel::GetCommandBuffer();
209
210 ArchiveHandle src_archive_handle = MakeArchiveHandle(cmd_buff[2], cmd_buff[3]);
211 auto src_filename_type = static_cast<FileSys::LowPathType>(cmd_buff[4]);
212 u32 src_filename_size = cmd_buff[5];
213 ArchiveHandle dest_archive_handle = MakeArchiveHandle(cmd_buff[6], cmd_buff[7]);
214 ;
215 auto dest_filename_type = static_cast<FileSys::LowPathType>(cmd_buff[8]);
216 u32 dest_filename_size = cmd_buff[9];
217 u32 src_filename_ptr = cmd_buff[11];
218 u32 dest_filename_ptr = cmd_buff[13];
219
220 FileSys::Path src_file_path(src_filename_type, src_filename_size, src_filename_ptr);
221 FileSys::Path dest_file_path(dest_filename_type, dest_filename_size, dest_filename_ptr);
222
223 LOG_DEBUG(Service_FS,
224 "src_type=%u src_size=%u src_data=%s dest_type=%u dest_size=%u dest_data=%s",
225 static_cast<u32>(src_filename_type), src_filename_size,
226 src_file_path.DebugStr().c_str(), static_cast<u32>(dest_filename_type),
227 dest_filename_size, dest_file_path.DebugStr().c_str());
228
229 cmd_buff[1] = RenameFileBetweenArchives(src_archive_handle, src_file_path, dest_archive_handle,
230 dest_file_path)
231 .raw;
232}
233
234/*
235 * FS_User::DeleteDirectory service function
236 * Inputs:
237 * 2 : Archive handle lower word
238 * 3 : Archive handle upper word
239 * 4 : Directory path string type
240 * 5 : Directory path string size
241 * 7 : Directory path string data
242 * Outputs:
243 * 1 : Result of function, 0 on success, otherwise error code
244 */
245static void DeleteDirectory(Service::Interface* self) {
246 u32* cmd_buff = Kernel::GetCommandBuffer();
247
248 ArchiveHandle archive_handle = MakeArchiveHandle(cmd_buff[2], cmd_buff[3]);
249 auto dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[4]);
250 u32 dirname_size = cmd_buff[5];
251 u32 dirname_ptr = cmd_buff[7];
252
253 FileSys::Path dir_path(dirname_type, dirname_size, dirname_ptr);
254
255 LOG_DEBUG(Service_FS, "type=%u size=%u data=%s", static_cast<u32>(dirname_type), dirname_size,
256 dir_path.DebugStr().c_str());
257
258 cmd_buff[1] = DeleteDirectoryFromArchive(archive_handle, dir_path).raw;
259}
260
261/*
262 * FS_User::DeleteDirectoryRecursively service function
263 * Inputs:
264 * 0 : Command header 0x08070142
265 * 1 : Transaction
266 * 2 : Archive handle lower word
267 * 3 : Archive handle upper word
268 * 4 : Directory path string type
269 * 5 : Directory path string size
270 * 7 : Directory path string data
271 * Outputs:
272 * 1 : Result of function, 0 on success, otherwise error code
273 */
274static void DeleteDirectoryRecursively(Service::Interface* self) {
275 u32* cmd_buff = Kernel::GetCommandBuffer();
276
277 ArchiveHandle archive_handle = MakeArchiveHandle(cmd_buff[2], cmd_buff[3]);
278 auto dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[4]);
279 u32 dirname_size = cmd_buff[5];
280 u32 dirname_ptr = cmd_buff[7];
281
282 FileSys::Path dir_path(dirname_type, dirname_size, dirname_ptr);
283
284 LOG_DEBUG(Service_FS, "type=%u size=%u data=%s", static_cast<u32>(dirname_type), dirname_size,
285 dir_path.DebugStr().c_str());
286
287 cmd_buff[1] = DeleteDirectoryRecursivelyFromArchive(archive_handle, dir_path).raw;
288}
289
290/*
291 * FS_User::CreateFile service function
292 * Inputs:
293 * 0 : Command header 0x08080202
294 * 2 : Archive handle lower word
295 * 3 : Archive handle upper word
296 * 4 : File path string type
297 * 5 : File path string size
298 * 7-8 : File size
299 * 10: File path string data
300 * Outputs:
301 * 1 : Result of function, 0 on success, otherwise error code
302 */
303static void CreateFile(Service::Interface* self) {
304 u32* cmd_buff = Kernel::GetCommandBuffer();
305
306 ArchiveHandle archive_handle = MakeArchiveHandle(cmd_buff[2], cmd_buff[3]);
307 auto filename_type = static_cast<FileSys::LowPathType>(cmd_buff[4]);
308 u32 filename_size = cmd_buff[5];
309 u64 file_size = ((u64)cmd_buff[8] << 32) | cmd_buff[7];
310 u32 filename_ptr = cmd_buff[10];
311
312 FileSys::Path file_path(filename_type, filename_size, filename_ptr);
313
314 LOG_DEBUG(Service_FS, "type=%u size=%llu data=%s", static_cast<u32>(filename_type), file_size,
315 file_path.DebugStr().c_str());
316
317 cmd_buff[1] = CreateFileInArchive(archive_handle, file_path, file_size).raw;
318}
319
320/*
321 * FS_User::CreateDirectory service function
322 * Inputs:
323 * 2 : Archive handle lower word
324 * 3 : Archive handle upper word
325 * 4 : Directory path string type
326 * 5 : Directory path string size
327 * 8 : Directory path string data
328 * Outputs:
329 * 1 : Result of function, 0 on success, otherwise error code
330 */
331static void CreateDirectory(Service::Interface* self) {
332 u32* cmd_buff = Kernel::GetCommandBuffer();
333
334 ArchiveHandle archive_handle = MakeArchiveHandle(cmd_buff[2], cmd_buff[3]);
335 auto dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[4]);
336 u32 dirname_size = cmd_buff[5];
337 u32 dirname_ptr = cmd_buff[8];
338
339 FileSys::Path dir_path(dirname_type, dirname_size, dirname_ptr);
340
341 LOG_DEBUG(Service_FS, "type=%u size=%d data=%s", static_cast<u32>(dirname_type), dirname_size,
342 dir_path.DebugStr().c_str());
343
344 cmd_buff[1] = CreateDirectoryFromArchive(archive_handle, dir_path).raw;
345}
346
347/*
348 * FS_User::RenameDirectory service function
349 * Inputs:
350 * 2 : Source archive handle lower word
351 * 3 : Source archive handle upper word
352 * 4 : Source dir path type
353 * 5 : Source dir path size
354 * 6 : Dest archive handle lower word
355 * 7 : Dest archive handle upper word
356 * 8 : Dest dir path type
357 * 9 : Dest dir path size
358 * 11: Source dir path string data
359 * 13: Dest dir path string
360 * Outputs:
361 * 1 : Result of function, 0 on success, otherwise error code
362 */
363static void RenameDirectory(Service::Interface* self) {
364 u32* cmd_buff = Kernel::GetCommandBuffer();
365
366 ArchiveHandle src_archive_handle = MakeArchiveHandle(cmd_buff[2], cmd_buff[3]);
367 auto src_dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[4]);
368 u32 src_dirname_size = cmd_buff[5];
369 ArchiveHandle dest_archive_handle = MakeArchiveHandle(cmd_buff[6], cmd_buff[7]);
370 auto dest_dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[8]);
371 u32 dest_dirname_size = cmd_buff[9];
372 u32 src_dirname_ptr = cmd_buff[11];
373 u32 dest_dirname_ptr = cmd_buff[13];
374
375 FileSys::Path src_dir_path(src_dirname_type, src_dirname_size, src_dirname_ptr);
376 FileSys::Path dest_dir_path(dest_dirname_type, dest_dirname_size, dest_dirname_ptr);
377
378 LOG_DEBUG(
379 Service_FS, "src_type=%u src_size=%u src_data=%s dest_type=%u dest_size=%u dest_data=%s",
380 static_cast<u32>(src_dirname_type), src_dirname_size, src_dir_path.DebugStr().c_str(),
381 static_cast<u32>(dest_dirname_type), dest_dirname_size, dest_dir_path.DebugStr().c_str());
382
383 cmd_buff[1] = RenameDirectoryBetweenArchives(src_archive_handle, src_dir_path,
384 dest_archive_handle, dest_dir_path)
385 .raw;
386}
387
388/**
389 * FS_User::OpenDirectory service function
390 * Inputs:
391 * 1 : Archive handle low word
392 * 2 : Archive handle high word
393 * 3 : Low path type
394 * 4 : Low path size
395 * 7 : (LowPathSize << 14) | 2
396 * 8 : Low path data pointer
397 * Outputs:
398 * 1 : Result of function, 0 on success, otherwise error code
399 * 3 : Directory handle
400 */
401static void OpenDirectory(Service::Interface* self) {
402 u32* cmd_buff = Kernel::GetCommandBuffer();
403
404 ArchiveHandle archive_handle = MakeArchiveHandle(cmd_buff[1], cmd_buff[2]);
405 auto dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[3]);
406 u32 dirname_size = cmd_buff[4];
407 u32 dirname_ptr = cmd_buff[6];
408
409 FileSys::Path dir_path(dirname_type, dirname_size, dirname_ptr);
410
411 LOG_DEBUG(Service_FS, "type=%u size=%u data=%s", static_cast<u32>(dirname_type), dirname_size,
412 dir_path.DebugStr().c_str());
413
414 ResultVal<std::shared_ptr<Directory>> dir_res =
415 OpenDirectoryFromArchive(archive_handle, dir_path);
416 cmd_buff[1] = dir_res.Code().raw;
417 if (dir_res.Succeeded()) {
418 std::shared_ptr<Directory> directory = *dir_res;
419 auto sessions = ServerSession::CreateSessionPair(directory->GetName());
420 directory->ClientConnected(std::get<SharedPtr<ServerSession>>(sessions));
421
422 cmd_buff[3] =
423 Kernel::g_handle_table.Create(std::get<SharedPtr<ClientSession>>(sessions)).Unwrap();
424 } else {
425 LOG_ERROR(Service_FS, "failed to get a handle for directory type=%d size=%d data=%s",
426 dirname_type, dirname_size, dir_path.DebugStr().c_str());
427 }
428}
429
430/**
431 * FS_User::OpenArchive service function
432 * Inputs:
433 * 1 : Archive ID
434 * 2 : Archive low path type
435 * 3 : Archive low path size
436 * 4 : (LowPathSize << 14) | 2
437 * 5 : Archive low path
438 * Outputs:
439 * 1 : Result of function, 0 on success, otherwise error code
440 * 2 : Archive handle lower word (unused)
441 * 3 : Archive handle upper word (same as file handle)
442 */
443static void OpenArchive(Service::Interface* self) {
444 u32* cmd_buff = Kernel::GetCommandBuffer();
445
446 auto archive_id = static_cast<FS::ArchiveIdCode>(cmd_buff[1]);
447 auto archivename_type = static_cast<FileSys::LowPathType>(cmd_buff[2]);
448 u32 archivename_size = cmd_buff[3];
449 u32 archivename_ptr = cmd_buff[5];
450 FileSys::Path archive_path(archivename_type, archivename_size, archivename_ptr);
451
452 LOG_DEBUG(Service_FS, "archive_id=0x%08X archive_path=%s", static_cast<u32>(archive_id),
453 archive_path.DebugStr().c_str());
454
455 ResultVal<ArchiveHandle> handle = OpenArchive(archive_id, archive_path);
456 cmd_buff[1] = handle.Code().raw;
457 if (handle.Succeeded()) {
458 cmd_buff[2] = *handle & 0xFFFFFFFF;
459 cmd_buff[3] = (*handle >> 32) & 0xFFFFFFFF;
460 } else {
461 cmd_buff[2] = cmd_buff[3] = 0;
462 LOG_ERROR(Service_FS,
463 "failed to get a handle for archive archive_id=0x%08X archive_path=%s",
464 static_cast<u32>(archive_id), archive_path.DebugStr().c_str());
465 }
466}
467
468/**
469 * FS_User::CloseArchive service function
470 * Inputs:
471 * 0 : 0x080E0080
472 * 1 : Archive handle low word
473 * 2 : Archive handle high word
474 * Outputs:
475 * 0 : ??? TODO(yuriks): Verify return header
476 * 1 : Result of function, 0 on success, otherwise error code
477 */
478static void CloseArchive(Service::Interface* self) {
479 u32* cmd_buff = Kernel::GetCommandBuffer();
480
481 ArchiveHandle archive_handle = MakeArchiveHandle(cmd_buff[1], cmd_buff[2]);
482 cmd_buff[1] = CloseArchive(archive_handle).raw;
483}
484
485/*
486* FS_User::IsSdmcDetected service function
487* Outputs:
488* 1 : Result of function, 0 on success, otherwise error code
489* 2 : Whether the Sdmc could be detected
490*/
491static void IsSdmcDetected(Service::Interface* self) {
492 u32* cmd_buff = Kernel::GetCommandBuffer();
493
494 cmd_buff[1] = 0;
495 cmd_buff[2] = Settings::values.use_virtual_sd ? 1 : 0;
496}
497
498/**
499 * FS_User::IsSdmcWriteable service function
500 * Outputs:
501 * 0 : Command header 0x08180000
502 * 1 : Result of function, 0 on success, otherwise error code
503 * 2 : Whether the Sdmc is currently writeable
504 */
505static void IsSdmcWriteable(Service::Interface* self) {
506 u32* cmd_buff = Kernel::GetCommandBuffer();
507
508 cmd_buff[1] = RESULT_SUCCESS.raw;
509 // If the SD isn't enabled, it can't be writeable...else, stubbed true
510 cmd_buff[2] = Settings::values.use_virtual_sd ? 1 : 0;
511
512 LOG_DEBUG(Service_FS, " (STUBBED)");
513}
514
515/**
516 * FS_User::FormatSaveData service function,
517 * formats the SaveData specified by the input path.
518 * Inputs:
519 * 0 : 0x084C0242
520 * 1 : Archive ID
521 * 2 : Archive path type
522 * 3 : Archive path size
523 * 4 : Size in Blocks (1 block = 512 bytes)
524 * 5 : Number of directories
525 * 6 : Number of files
526 * 7 : Directory bucket count
527 * 8 : File bucket count
528 * 9 : Duplicate data
529 * 10 : (PathSize << 14) | 2
530 * 11 : Archive low path
531 * Outputs:
532 * 1 : Result of function, 0 on success, otherwise error code
533 */
534static void FormatSaveData(Service::Interface* self) {
535 u32* cmd_buff = Kernel::GetCommandBuffer();
536 LOG_WARNING(Service_FS, "(STUBBED)");
537
538 auto archive_id = static_cast<FS::ArchiveIdCode>(cmd_buff[1]);
539 auto archivename_type = static_cast<FileSys::LowPathType>(cmd_buff[2]);
540 u32 archivename_size = cmd_buff[3];
541 u32 archivename_ptr = cmd_buff[11];
542 FileSys::Path archive_path(archivename_type, archivename_size, archivename_ptr);
543
544 LOG_DEBUG(Service_FS, "archive_path=%s", archive_path.DebugStr().c_str());
545
546 if (archive_id != FS::ArchiveIdCode::SaveData) {
547 LOG_ERROR(Service_FS, "tried to format an archive different than SaveData, %u",
548 static_cast<u32>(archive_id));
549 cmd_buff[1] = FileSys::ERROR_INVALID_PATH.raw;
550 return;
551 }
552
553 if (archive_path.GetType() != FileSys::LowPathType::Empty) {
554 // TODO(Subv): Implement formatting the SaveData of other games
555 LOG_ERROR(Service_FS, "archive LowPath type other than empty is currently unsupported");
556 cmd_buff[1] = UnimplementedFunction(ErrorModule::FS).raw;
557 return;
558 }
559
560 FileSys::ArchiveFormatInfo format_info;
561 format_info.duplicate_data = cmd_buff[9] & 0xFF;
562 format_info.number_directories = cmd_buff[5];
563 format_info.number_files = cmd_buff[6];
564 format_info.total_size = cmd_buff[4] * 512;
565
566 cmd_buff[1] = FormatArchive(ArchiveIdCode::SaveData, format_info).raw;
567}
568
569/**
570 * FS_User::FormatThisUserSaveData service function
571 * Inputs:
572 * 0: 0x080F0180
573 * 1 : Size in Blocks (1 block = 512 bytes)
574 * 2 : Number of directories
575 * 3 : Number of files
576 * 4 : Directory bucket count
577 * 5 : File bucket count
578 * 6 : Duplicate data
579 * Outputs:
580 * 1 : Result of function, 0 on success, otherwise error code
581 */
582static void FormatThisUserSaveData(Service::Interface* self) {
583 u32* cmd_buff = Kernel::GetCommandBuffer();
584
585 FileSys::ArchiveFormatInfo format_info;
586 format_info.duplicate_data = cmd_buff[6] & 0xFF;
587 format_info.number_directories = cmd_buff[2];
588 format_info.number_files = cmd_buff[3];
589 format_info.total_size = cmd_buff[1] * 512;
590
591 cmd_buff[1] = FormatArchive(ArchiveIdCode::SaveData, format_info).raw;
592
593 LOG_TRACE(Service_FS, "called");
594}
595
596/**
597 * FS_User::GetFreeBytes service function
598 * Inputs:
599 * 0: 0x08120080
600 * 1: Archive handle low word
601 * 2: Archive handle high word
602 * Outputs:
603 * 1: Result of function, 0 on success, otherwise error code
604 * 2: Free byte count low word
605 * 3: Free byte count high word
606 */
607static void GetFreeBytes(Service::Interface* self) {
608 u32* cmd_buff = Kernel::GetCommandBuffer();
609
610 ArchiveHandle archive_handle = MakeArchiveHandle(cmd_buff[1], cmd_buff[2]);
611 ResultVal<u64> bytes_res = GetFreeBytesInArchive(archive_handle);
612
613 cmd_buff[1] = bytes_res.Code().raw;
614 if (bytes_res.Succeeded()) {
615 cmd_buff[2] = (u32)*bytes_res;
616 cmd_buff[3] = *bytes_res >> 32;
617 } else {
618 cmd_buff[2] = 0;
619 cmd_buff[3] = 0;
620 }
621}
622
623/**
624 * FS_User::CreateExtSaveData service function
625 * Inputs:
626 * 0 : 0x08510242
627 * 1 : Media type (NAND / SDMC)
628 * 2 : Low word of the saveid to create
629 * 3 : High word of the saveid to create
630 * 4 : Unknown
631 * 5 : Number of directories
632 * 6 : Number of files
633 * 7-8 : Size limit
634 * 9 : Size of the SMDH icon
635 * 10: (SMDH Size << 4) | 0x0000000A
636 * 11: Pointer to the SMDH icon for the new ExtSaveData
637 * Outputs:
638 * 1 : Result of function, 0 on success, otherwise error code
639 */
640static void CreateExtSaveData(Service::Interface* self) {
641 // TODO(Subv): Figure out the other parameters.
642 u32* cmd_buff = Kernel::GetCommandBuffer();
643 MediaType media_type = static_cast<MediaType>(cmd_buff[1] & 0xFF);
644 u32 save_low = cmd_buff[2];
645 u32 save_high = cmd_buff[3];
646 u32 icon_size = cmd_buff[9];
647 VAddr icon_buffer = cmd_buff[11];
648
649 LOG_WARNING(
650 Service_FS,
651 "(STUBBED) savedata_high=%08X savedata_low=%08X cmd_buff[3]=%08X "
652 "cmd_buff[4]=%08X cmd_buff[5]=%08X cmd_buff[6]=%08X cmd_buff[7]=%08X cmd_buff[8]=%08X "
653 "icon_size=%08X icon_descriptor=%08X icon_buffer=%08X",
654 save_high, save_low, cmd_buff[3], cmd_buff[4], cmd_buff[5], cmd_buff[6], cmd_buff[7],
655 cmd_buff[8], icon_size, cmd_buff[10], icon_buffer);
656
657 FileSys::ArchiveFormatInfo format_info;
658 format_info.number_directories = cmd_buff[5];
659 format_info.number_files = cmd_buff[6];
660 format_info.duplicate_data = false;
661 format_info.total_size = 0;
662 cmd_buff[1] =
663 CreateExtSaveData(media_type, save_high, save_low, icon_buffer, icon_size, format_info).raw;
664}
665
666/**
667 * FS_User::DeleteExtSaveData service function
668 * Inputs:
669 * 0 : 0x08520100
670 * 1 : Media type (NAND / SDMC)
671 * 2 : Low word of the saveid to create
672 * 3 : High word of the saveid to create
673 * 4 : Unknown
674 * Outputs:
675 * 1 : Result of function, 0 on success, otherwise error code
676 */
677static void DeleteExtSaveData(Service::Interface* self) {
678 u32* cmd_buff = Kernel::GetCommandBuffer();
679 MediaType media_type = static_cast<MediaType>(cmd_buff[1] & 0xFF);
680 u32 save_low = cmd_buff[2];
681 u32 save_high = cmd_buff[3];
682 u32 unknown = cmd_buff[4]; // TODO(Subv): Figure out what this is
683
684 LOG_WARNING(Service_FS, "(STUBBED) save_low=%08X save_high=%08X media_type=%08X unknown=%08X",
685 save_low, save_high, cmd_buff[1] & 0xFF, unknown);
686
687 cmd_buff[1] = DeleteExtSaveData(media_type, save_high, save_low).raw;
688}
689
690/**
691 * FS_User::CardSlotIsInserted service function.
692 * Inputs:
693 * 0 : 0x08210000
694 * Outputs:
695 * 1 : Result of function, 0 on success, otherwise error code
696 * 2 : Whether there is a game card inserted into the slot or not.
697 */
698static void CardSlotIsInserted(Service::Interface* self) {
699 u32* cmd_buff = Kernel::GetCommandBuffer();
700 cmd_buff[1] = RESULT_SUCCESS.raw;
701 cmd_buff[2] = 0;
702 LOG_WARNING(Service_FS, "(STUBBED) called");
703}
704
705/**
706 * FS_User::DeleteSystemSaveData service function.
707 * Inputs:
708 * 0 : 0x08570080
709 * 1 : High word of the SystemSaveData id to delete
710 * 2 : Low word of the SystemSaveData id to delete
711 * Outputs:
712 * 1 : Result of function, 0 on success, otherwise error code
713 */
714static void DeleteSystemSaveData(Service::Interface* self) {
715 u32* cmd_buff = Kernel::GetCommandBuffer();
716 u32 savedata_high = cmd_buff[1];
717 u32 savedata_low = cmd_buff[2];
718
719 cmd_buff[1] = DeleteSystemSaveData(savedata_high, savedata_low).raw;
720}
721
722/**
723 * FS_User::CreateSystemSaveData service function.
724 * Inputs:
725 * 0 : 0x08560240
726 * 1 : u8 MediaType of the system save data
727 * 2 : SystemSaveData id to create
728 * 3 : Total size
729 * 4 : Block size
730 * 5 : Number of directories
731 * 6 : Number of files
732 * 7 : Directory bucket count
733 * 8 : File bucket count
734 * 9 : u8 Whether to duplicate data or not
735 * Outputs:
736 * 1 : Result of function, 0 on success, otherwise error code
737 */
738static void CreateSystemSaveData(Service::Interface* self) {
739 u32* cmd_buff = Kernel::GetCommandBuffer();
740 u32 savedata_high = cmd_buff[1];
741 u32 savedata_low = cmd_buff[2];
742
743 LOG_WARNING(
744 Service_FS,
745 "(STUBBED) savedata_high=%08X savedata_low=%08X cmd_buff[3]=%08X "
746 "cmd_buff[4]=%08X cmd_buff[5]=%08X cmd_buff[6]=%08X cmd_buff[7]=%08X cmd_buff[8]=%08X "
747 "cmd_buff[9]=%08X",
748 savedata_high, savedata_low, cmd_buff[3], cmd_buff[4], cmd_buff[5], cmd_buff[6],
749 cmd_buff[7], cmd_buff[8], cmd_buff[9]);
750
751 cmd_buff[1] = CreateSystemSaveData(savedata_high, savedata_low).raw;
752}
753
754/**
755 * FS_User::CreateLegacySystemSaveData service function.
756 * This function appears to be obsolete and seems to have been replaced by
757 * command 0x08560240 (CreateSystemSaveData).
758 *
759 * Inputs:
760 * 0 : 0x08100200
761 * 1 : SystemSaveData id to create
762 * 2 : Total size
763 * 3 : Block size
764 * 4 : Number of directories
765 * 5 : Number of files
766 * 6 : Directory bucket count
767 * 7 : File bucket count
768 * 8 : u8 Duplicate data
769 * Outputs:
770 * 1 : Result of function, 0 on success, otherwise error code
771 */
772static void CreateLegacySystemSaveData(Service::Interface* self) {
773 u32* cmd_buff = Kernel::GetCommandBuffer();
774 u32 savedata_id = cmd_buff[1];
775
776 LOG_WARNING(
777 Service_FS,
778 "(STUBBED) savedata_id=%08X cmd_buff[3]=%08X "
779 "cmd_buff[4]=%08X cmd_buff[5]=%08X cmd_buff[6]=%08X cmd_buff[7]=%08X cmd_buff[8]=%08X "
780 "cmd_buff[9]=%08X",
781 savedata_id, cmd_buff[3], cmd_buff[4], cmd_buff[5], cmd_buff[6], cmd_buff[7], cmd_buff[8],
782 cmd_buff[9]);
783
784 cmd_buff[0] = IPC::MakeHeader(0x810, 0x1, 0);
785 // With this command, the SystemSaveData always has save_high = 0 (Always created in the NAND)
786 cmd_buff[1] = CreateSystemSaveData(0, savedata_id).raw;
787}
788
789/**
790 * FS_User::InitializeWithSdkVersion service function.
791 * Inputs:
792 * 0 : 0x08610042
793 * 1 : Used SDK Version
794 * 2 : ProcessId Header
795 * 3 : placeholder for ProcessId
796 * Outputs:
797 * 1 : Result of function, 0 on success, otherwise error code
798 */
799static void InitializeWithSdkVersion(Service::Interface* self) {
800 u32* cmd_buff = Kernel::GetCommandBuffer();
801
802 const u32 version = cmd_buff[1];
803 self->SetVersion(version);
804
805 if (cmd_buff[2] == IPC::CallingPidDesc()) {
806 LOG_WARNING(Service_FS, "(STUBBED) called, version: 0x%08X", version);
807 cmd_buff[1] = RESULT_SUCCESS.raw;
808 } else {
809 LOG_ERROR(Service_FS, "ProcessId Header must be 0x20");
810 cmd_buff[1] = IPC::ERR_INVALID_BUFFER_DESCRIPTOR.raw;
811 }
812}
813
814/**
815 * FS_User::SetPriority service function.
816 * Inputs:
817 * 0 : 0x08620040
818 * 1 : priority
819 * Outputs:
820 * 1 : Result of function, 0 on success, otherwise error code
821 */
822static void SetPriority(Service::Interface* self) {
823 u32* cmd_buff = Kernel::GetCommandBuffer();
824
825 priority = cmd_buff[1];
826
827 cmd_buff[1] = RESULT_SUCCESS.raw;
828
829 LOG_DEBUG(Service_FS, "called priority=0x%X", priority);
830}
831
832/**
833 * FS_User::GetPriority service function.
834 * Inputs:
835 * 0 : 0x08630000
836 * Outputs:
837 * 1 : Result of function, 0 on success, otherwise error code
838 * 2 : priority
839 */
840static void GetPriority(Service::Interface* self) {
841 u32* cmd_buff = Kernel::GetCommandBuffer();
842
843 if (priority == -1) {
844 LOG_INFO(Service_FS, "priority was not set, priority=0x%X", priority);
845 }
846
847 cmd_buff[1] = RESULT_SUCCESS.raw;
848 cmd_buff[2] = priority;
849
850 LOG_DEBUG(Service_FS, "called priority=0x%X", priority);
851}
852
853/**
854 * FS_User::GetArchiveResource service function.
855 * Inputs:
856 * 0 : 0x08490040
857 * 1 : Media type
858 * Outputs:
859 * 1 : Result of function, 0 on success, otherwise error code
860 * 2 : Sector byte-size
861 * 3 : Cluster byte-size
862 * 4 : Partition capacity in clusters
863 * 5 : Available free space in clusters
864 */
865static void GetArchiveResource(Service::Interface* self) {
866 u32* cmd_buff = Kernel::GetCommandBuffer();
867
868 LOG_WARNING(Service_FS, "(STUBBED) called Media type=0x%08X", cmd_buff[1]);
869
870 cmd_buff[1] = RESULT_SUCCESS.raw;
871 cmd_buff[2] = 512;
872 cmd_buff[3] = 16384;
873 cmd_buff[4] = 0x80000; // 8GiB capacity
874 cmd_buff[5] = 0x80000; // 8GiB free
875}
876
877/**
878 * FS_User::GetFormatInfo service function.
879 * Inputs:
880 * 0 : 0x084500C2
881 * 1 : Archive ID
882 * 2 : Archive path type
883 * 3 : Archive path size
884 * 4 : (PathSize << 14) | 2
885 * 5 : Archive low path
886 * Outputs:
887 * 0 : 0x08450140
888 * 1 : Result of function, 0 on success, otherwise error code
889 * 2 : Total size
890 * 3 : Number of directories
891 * 4 : Number of files
892 * 5 : Duplicate data
893 */
894static void GetFormatInfo(Service::Interface* self) {
895 u32* cmd_buff = Kernel::GetCommandBuffer();
896
897 auto archive_id = static_cast<FS::ArchiveIdCode>(cmd_buff[1]);
898 auto archivename_type = static_cast<FileSys::LowPathType>(cmd_buff[2]);
899 u32 archivename_size = cmd_buff[3];
900 u32 archivename_ptr = cmd_buff[5];
901 FileSys::Path archive_path(archivename_type, archivename_size, archivename_ptr);
902
903 LOG_DEBUG(Service_FS, "archive_path=%s", archive_path.DebugStr().c_str());
904
905 cmd_buff[0] = IPC::MakeHeader(0x0845, 5, 0);
906
907 auto format_info = GetArchiveFormatInfo(archive_id, archive_path);
908
909 if (format_info.Failed()) {
910 LOG_ERROR(Service_FS, "Failed to retrieve the format info");
911 cmd_buff[1] = format_info.Code().raw;
912 return;
913 }
914
915 cmd_buff[1] = RESULT_SUCCESS.raw;
916 cmd_buff[2] = format_info->total_size;
917 cmd_buff[3] = format_info->number_directories;
918 cmd_buff[4] = format_info->number_files;
919 cmd_buff[5] = format_info->duplicate_data;
920}
921
922const Interface::FunctionInfo FunctionTable[] = {
923 {0x000100C6, nullptr, "Dummy1"},
924 {0x040100C4, nullptr, "Control"},
925 {0x08010002, Initialize, "Initialize"},
926 {0x080201C2, OpenFile, "OpenFile"},
927 {0x08030204, OpenFileDirectly, "OpenFileDirectly"},
928 {0x08040142, DeleteFile, "DeleteFile"},
929 {0x08050244, RenameFile, "RenameFile"},
930 {0x08060142, DeleteDirectory, "DeleteDirectory"},
931 {0x08070142, DeleteDirectoryRecursively, "DeleteDirectoryRecursively"},
932 {0x08080202, CreateFile, "CreateFile"},
933 {0x08090182, CreateDirectory, "CreateDirectory"},
934 {0x080A0244, RenameDirectory, "RenameDirectory"},
935 {0x080B0102, OpenDirectory, "OpenDirectory"},
936 {0x080C00C2, OpenArchive, "OpenArchive"},
937 {0x080D0144, nullptr, "ControlArchive"},
938 {0x080E0080, CloseArchive, "CloseArchive"},
939 {0x080F0180, FormatThisUserSaveData, "FormatThisUserSaveData"},
940 {0x08100200, CreateLegacySystemSaveData, "CreateLegacySystemSaveData"},
941 {0x08110040, nullptr, "DeleteSystemSaveData"},
942 {0x08120080, GetFreeBytes, "GetFreeBytes"},
943 {0x08130000, nullptr, "GetCardType"},
944 {0x08140000, nullptr, "GetSdmcArchiveResource"},
945 {0x08150000, nullptr, "GetNandArchiveResource"},
946 {0x08160000, nullptr, "GetSdmcFatfsError"},
947 {0x08170000, IsSdmcDetected, "IsSdmcDetected"},
948 {0x08180000, IsSdmcWriteable, "IsSdmcWritable"},
949 {0x08190042, nullptr, "GetSdmcCid"},
950 {0x081A0042, nullptr, "GetNandCid"},
951 {0x081B0000, nullptr, "GetSdmcSpeedInfo"},
952 {0x081C0000, nullptr, "GetNandSpeedInfo"},
953 {0x081D0042, nullptr, "GetSdmcLog"},
954 {0x081E0042, nullptr, "GetNandLog"},
955 {0x081F0000, nullptr, "ClearSdmcLog"},
956 {0x08200000, nullptr, "ClearNandLog"},
957 {0x08210000, CardSlotIsInserted, "CardSlotIsInserted"},
958 {0x08220000, nullptr, "CardSlotPowerOn"},
959 {0x08230000, nullptr, "CardSlotPowerOff"},
960 {0x08240000, nullptr, "CardSlotGetCardIFPowerStatus"},
961 {0x08250040, nullptr, "CardNorDirectCommand"},
962 {0x08260080, nullptr, "CardNorDirectCommandWithAddress"},
963 {0x08270082, nullptr, "CardNorDirectRead"},
964 {0x082800C2, nullptr, "CardNorDirectReadWithAddress"},
965 {0x08290082, nullptr, "CardNorDirectWrite"},
966 {0x082A00C2, nullptr, "CardNorDirectWriteWithAddress"},
967 {0x082B00C2, nullptr, "CardNorDirectRead_4xIO"},
968 {0x082C0082, nullptr, "CardNorDirectCpuWriteWithoutVerify"},
969 {0x082D0040, nullptr, "CardNorDirectSectorEraseWithoutVerify"},
970 {0x082E0040, nullptr, "GetProductInfo"},
971 {0x082F0040, nullptr, "GetProgramLaunchInfo"},
972 {0x08300182, nullptr, "CreateExtSaveData"},
973 {0x08310180, nullptr, "CreateSharedExtSaveData"},
974 {0x08320102, nullptr, "ReadExtSaveDataIcon"},
975 {0x08330082, nullptr, "EnumerateExtSaveData"},
976 {0x08340082, nullptr, "EnumerateSharedExtSaveData"},
977 {0x08350080, nullptr, "DeleteExtSaveData"},
978 {0x08360080, nullptr, "DeleteSharedExtSaveData"},
979 {0x08370040, nullptr, "SetCardSpiBaudRate"},
980 {0x08380040, nullptr, "SetCardSpiBusMode"},
981 {0x08390000, nullptr, "SendInitializeInfoTo9"},
982 {0x083A0100, nullptr, "GetSpecialContentIndex"},
983 {0x083B00C2, nullptr, "GetLegacyRomHeader"},
984 {0x083C00C2, nullptr, "GetLegacyBannerData"},
985 {0x083D0100, nullptr, "CheckAuthorityToAccessExtSaveData"},
986 {0x083E00C2, nullptr, "QueryTotalQuotaSize"},
987 {0x083F00C0, nullptr, "GetExtDataBlockSize"},
988 {0x08400040, nullptr, "AbnegateAccessRight"},
989 {0x08410000, nullptr, "DeleteSdmcRoot"},
990 {0x08420040, nullptr, "DeleteAllExtSaveDataOnNand"},
991 {0x08430000, nullptr, "InitializeCtrFileSystem"},
992 {0x08440000, nullptr, "CreateSeed"},
993 {0x084500C2, GetFormatInfo, "GetFormatInfo"},
994 {0x08460102, nullptr, "GetLegacyRomHeader2"},
995 {0x08470180, nullptr, "FormatCtrCardUserSaveData"},
996 {0x08480042, nullptr, "GetSdmcCtrRootPath"},
997 {0x08490040, GetArchiveResource, "GetArchiveResource"},
998 {0x084A0002, nullptr, "ExportIntegrityVerificationSeed"},
999 {0x084B0002, nullptr, "ImportIntegrityVerificationSeed"},
1000 {0x084C0242, FormatSaveData, "FormatSaveData"},
1001 {0x084D0102, nullptr, "GetLegacySubBannerData"},
1002 {0x084E0342, nullptr, "UpdateSha256Context"},
1003 {0x084F0102, nullptr, "ReadSpecialFile"},
1004 {0x08500040, nullptr, "GetSpecialFileSize"},
1005 {0x08510242, CreateExtSaveData, "CreateExtSaveData"},
1006 {0x08520100, DeleteExtSaveData, "DeleteExtSaveData"},
1007 {0x08530142, nullptr, "ReadExtSaveDataIcon"},
1008 {0x085400C0, nullptr, "GetExtDataBlockSize"},
1009 {0x08550102, nullptr, "EnumerateExtSaveData"},
1010 {0x08560240, CreateSystemSaveData, "CreateSystemSaveData"},
1011 {0x08570080, DeleteSystemSaveData, "DeleteSystemSaveData"},
1012 {0x08580000, nullptr, "StartDeviceMoveAsSource"},
1013 {0x08590200, nullptr, "StartDeviceMoveAsDestination"},
1014 {0x085A00C0, nullptr, "SetArchivePriority"},
1015 {0x085B0080, nullptr, "GetArchivePriority"},
1016 {0x085C00C0, nullptr, "SetCtrCardLatencyParameter"},
1017 {0x085D01C0, nullptr, "SetFsCompatibilityInfo"},
1018 {0x085E0040, nullptr, "ResetCardCompatibilityParameter"},
1019 {0x085F0040, nullptr, "SwitchCleanupInvalidSaveData"},
1020 {0x08600042, nullptr, "EnumerateSystemSaveData"},
1021 {0x08610042, InitializeWithSdkVersion, "InitializeWithSdkVersion"},
1022 {0x08620040, SetPriority, "SetPriority"},
1023 {0x08630000, GetPriority, "GetPriority"},
1024 {0x08640000, nullptr, "GetNandInfo"},
1025 {0x08650140, nullptr, "SetSaveDataSecureValue"},
1026 {0x086600C0, nullptr, "GetSaveDataSecureValue"},
1027 {0x086700C4, nullptr, "ControlSecureSave"},
1028 {0x08680000, nullptr, "GetMediaType"},
1029 {0x08690000, nullptr, "GetNandEraseCount"},
1030 {0x086A0082, nullptr, "ReadNandReport"},
1031 {0x087A0180, nullptr, "AddSeed"},
1032 {0x088600C0, nullptr, "CheckUpdatedDat"},
1033};
1034
1035////////////////////////////////////////////////////////////////////////////////////////////////////
1036// Interface class
1037
1038Interface::Interface() {
1039
1040 priority = -1;
1041 Register(FunctionTable);
1042}
1043
1044} // namespace FS
1045} // namespace Service
diff --git a/src/core/hle/service/fs/fs_user.h b/src/core/hle/service/fs/fs_user.h
deleted file mode 100644
index bb6ab195e..000000000
--- a/src/core/hle/service/fs/fs_user.h
+++ /dev/null
@@ -1,23 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "core/hle/service/service.h"
8
9namespace Service {
10namespace FS {
11
12/// Interface to "fs:USER" service
13class Interface : public Service::Interface {
14public:
15 Interface();
16
17 std::string GetPortName() const override {
18 return "fs:USER";
19 }
20};
21
22} // namespace FS
23} // namespace Service
diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp
index 379fbd71c..f838713a3 100644
--- a/src/core/hle/service/hid/hid.cpp
+++ b/src/core/hle/service/hid/hid.cpp
@@ -2,437 +2,18 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <algorithm>
6#include <atomic>
7#include <cmath>
8#include <memory>
9#include "common/logging/log.h" 5#include "common/logging/log.h"
10#include "core/3ds.h"
11#include "core/core.h"
12#include "core/core_timing.h"
13#include "core/frontend/input.h"
14#include "core/hle/ipc.h"
15#include "core/hle/kernel/event.h"
16#include "core/hle/kernel/handle_table.h"
17#include "core/hle/kernel/shared_memory.h"
18#include "core/hle/service/hid/hid.h" 6#include "core/hle/service/hid/hid.h"
19#include "core/hle/service/hid/hid_spvr.h"
20#include "core/hle/service/hid/hid_user.h"
21#include "core/hle/service/service.h" 7#include "core/hle/service/service.h"
22 8
23namespace Service { 9namespace Service {
24namespace HID { 10namespace HID {
25 11
26// Handle to shared memory region designated to HID_User service 12void Init() {}
27static Kernel::SharedPtr<Kernel::SharedMemory> shared_mem;
28 13
29// Event handles 14void Shutdown() {}
30static Kernel::SharedPtr<Kernel::Event> event_pad_or_touch_1;
31static Kernel::SharedPtr<Kernel::Event> event_pad_or_touch_2;
32static Kernel::SharedPtr<Kernel::Event> event_accelerometer;
33static Kernel::SharedPtr<Kernel::Event> event_gyroscope;
34static Kernel::SharedPtr<Kernel::Event> event_debug_pad;
35 15
36static u32 next_pad_index; 16void ReloadInputDevices() {}
37static u32 next_touch_index;
38static u32 next_accelerometer_index;
39static u32 next_gyroscope_index;
40
41static int enable_accelerometer_count; // positive means enabled
42static int enable_gyroscope_count; // positive means enabled
43
44static int pad_update_event;
45static int accelerometer_update_event;
46static int gyroscope_update_event;
47
48// Updating period for each HID device. These empirical values are measured from a 11.2 3DS.
49constexpr u64 pad_update_ticks = BASE_CLOCK_RATE_ARM11 / 234;
50constexpr u64 accelerometer_update_ticks = BASE_CLOCK_RATE_ARM11 / 104;
51constexpr u64 gyroscope_update_ticks = BASE_CLOCK_RATE_ARM11 / 101;
52
53constexpr float accelerometer_coef = 512.0f; // measured from hw test result
54constexpr float gyroscope_coef = 14.375f; // got from hwtest GetGyroscopeLowRawToDpsCoefficient call
55
56static std::atomic<bool> is_device_reload_pending;
57static std::array<std::unique_ptr<Input::ButtonDevice>, Settings::NativeButton::NUM_BUTTONS_HID>
58 buttons;
59static std::unique_ptr<Input::AnalogDevice> circle_pad;
60static std::unique_ptr<Input::MotionDevice> motion_device;
61static std::unique_ptr<Input::TouchDevice> touch_device;
62
63DirectionState GetStickDirectionState(s16 circle_pad_x, s16 circle_pad_y) {
64 // 30 degree and 60 degree are angular thresholds for directions
65 constexpr float TAN30 = 0.577350269f;
66 constexpr float TAN60 = 1 / TAN30;
67 // a circle pad radius greater than 40 will trigger circle pad direction
68 constexpr int CIRCLE_PAD_THRESHOLD_SQUARE = 40 * 40;
69 DirectionState state{false, false, false, false};
70
71 if (circle_pad_x * circle_pad_x + circle_pad_y * circle_pad_y > CIRCLE_PAD_THRESHOLD_SQUARE) {
72 float t = std::abs(static_cast<float>(circle_pad_y) / circle_pad_x);
73
74 if (circle_pad_x != 0 && t < TAN60) {
75 if (circle_pad_x > 0)
76 state.right = true;
77 else
78 state.left = true;
79 }
80
81 if (circle_pad_x == 0 || t > TAN30) {
82 if (circle_pad_y > 0)
83 state.up = true;
84 else
85 state.down = true;
86 }
87 }
88
89 return state;
90}
91
92static void LoadInputDevices() {
93 std::transform(Settings::values.buttons.begin() + Settings::NativeButton::BUTTON_HID_BEGIN,
94 Settings::values.buttons.begin() + Settings::NativeButton::BUTTON_HID_END,
95 buttons.begin(), Input::CreateDevice<Input::ButtonDevice>);
96 circle_pad = Input::CreateDevice<Input::AnalogDevice>(
97 Settings::values.analogs[Settings::NativeAnalog::CirclePad]);
98 motion_device = Input::CreateDevice<Input::MotionDevice>(Settings::values.motion_device);
99 touch_device = Input::CreateDevice<Input::TouchDevice>(Settings::values.touch_device);
100}
101
102static void UnloadInputDevices() {
103 for (auto& button : buttons) {
104 button.reset();
105 }
106 circle_pad.reset();
107 motion_device.reset();
108 touch_device.reset();
109}
110
111static void UpdatePadCallback(u64 userdata, int cycles_late) {
112 SharedMem* mem = reinterpret_cast<SharedMem*>(shared_mem->GetPointer());
113
114 if (is_device_reload_pending.exchange(false))
115 LoadInputDevices();
116
117 PadState state;
118 using namespace Settings::NativeButton;
119 state.a.Assign(buttons[A - BUTTON_HID_BEGIN]->GetStatus());
120 state.b.Assign(buttons[B - BUTTON_HID_BEGIN]->GetStatus());
121 state.x.Assign(buttons[X - BUTTON_HID_BEGIN]->GetStatus());
122 state.y.Assign(buttons[Y - BUTTON_HID_BEGIN]->GetStatus());
123 state.right.Assign(buttons[Right - BUTTON_HID_BEGIN]->GetStatus());
124 state.left.Assign(buttons[Left - BUTTON_HID_BEGIN]->GetStatus());
125 state.up.Assign(buttons[Up - BUTTON_HID_BEGIN]->GetStatus());
126 state.down.Assign(buttons[Down - BUTTON_HID_BEGIN]->GetStatus());
127 state.l.Assign(buttons[L - BUTTON_HID_BEGIN]->GetStatus());
128 state.r.Assign(buttons[R - BUTTON_HID_BEGIN]->GetStatus());
129 state.start.Assign(buttons[Start - BUTTON_HID_BEGIN]->GetStatus());
130 state.select.Assign(buttons[Select - BUTTON_HID_BEGIN]->GetStatus());
131
132 // Get current circle pad position and update circle pad direction
133 float circle_pad_x_f, circle_pad_y_f;
134 std::tie(circle_pad_x_f, circle_pad_y_f) = circle_pad->GetStatus();
135 constexpr int MAX_CIRCLEPAD_POS = 0x9C; // Max value for a circle pad position
136 s16 circle_pad_x = static_cast<s16>(circle_pad_x_f * MAX_CIRCLEPAD_POS);
137 s16 circle_pad_y = static_cast<s16>(circle_pad_y_f * MAX_CIRCLEPAD_POS);
138 const DirectionState direction = GetStickDirectionState(circle_pad_x, circle_pad_y);
139 state.circle_up.Assign(direction.up);
140 state.circle_down.Assign(direction.down);
141 state.circle_left.Assign(direction.left);
142 state.circle_right.Assign(direction.right);
143
144 mem->pad.current_state.hex = state.hex;
145 mem->pad.index = next_pad_index;
146 next_pad_index = (next_pad_index + 1) % mem->pad.entries.size();
147
148 // Get the previous Pad state
149 u32 last_entry_index = (mem->pad.index - 1) % mem->pad.entries.size();
150 PadState old_state = mem->pad.entries[last_entry_index].current_state;
151
152 // Compute bitmask with 1s for bits different from the old state
153 PadState changed = {{(state.hex ^ old_state.hex)}};
154
155 // Get the current Pad entry
156 PadDataEntry& pad_entry = mem->pad.entries[mem->pad.index];
157
158 // Update entry properties
159 pad_entry.current_state.hex = state.hex;
160 pad_entry.delta_additions.hex = changed.hex & state.hex;
161 pad_entry.delta_removals.hex = changed.hex & old_state.hex;
162 pad_entry.circle_pad_x = circle_pad_x;
163 pad_entry.circle_pad_y = circle_pad_y;
164
165 // If we just updated index 0, provide a new timestamp
166 if (mem->pad.index == 0) {
167 mem->pad.index_reset_ticks_previous = mem->pad.index_reset_ticks;
168 mem->pad.index_reset_ticks = (s64)CoreTiming::GetTicks();
169 }
170
171 mem->touch.index = next_touch_index;
172 next_touch_index = (next_touch_index + 1) % mem->touch.entries.size();
173
174 // Get the current touch entry
175 TouchDataEntry& touch_entry = mem->touch.entries[mem->touch.index];
176 bool pressed = false;
177 float x, y;
178 std::tie(x, y, pressed) = touch_device->GetStatus();
179 touch_entry.x = static_cast<u16>(x * Core::kScreenBottomWidth);
180 touch_entry.y = static_cast<u16>(y * Core::kScreenBottomHeight);
181 touch_entry.valid.Assign(pressed ? 1 : 0);
182
183 // TODO(bunnei): We're not doing anything with offset 0xA8 + 0x18 of HID SharedMemory, which
184 // supposedly is "Touch-screen entry, which contains the raw coordinate data prior to being
185 // converted to pixel coordinates." (http://3dbrew.org/wiki/HID_Shared_Memory#Offset_0xA8).
186
187 // If we just updated index 0, provide a new timestamp
188 if (mem->touch.index == 0) {
189 mem->touch.index_reset_ticks_previous = mem->touch.index_reset_ticks;
190 mem->touch.index_reset_ticks = (s64)CoreTiming::GetTicks();
191 }
192
193 // Signal both handles when there's an update to Pad or touch
194 event_pad_or_touch_1->Signal();
195 event_pad_or_touch_2->Signal();
196
197 // Reschedule recurrent event
198 CoreTiming::ScheduleEvent(pad_update_ticks - cycles_late, pad_update_event);
199}
200
201static void UpdateAccelerometerCallback(u64 userdata, int cycles_late) {
202 SharedMem* mem = reinterpret_cast<SharedMem*>(shared_mem->GetPointer());
203
204 mem->accelerometer.index = next_accelerometer_index;
205 next_accelerometer_index = (next_accelerometer_index + 1) % mem->accelerometer.entries.size();
206
207 Math::Vec3<float> accel;
208 std::tie(accel, std::ignore) = motion_device->GetStatus();
209 accel *= accelerometer_coef;
210 // TODO(wwylele): do a time stretch like the one in UpdateGyroscopeCallback
211 // The time stretch formula should be like
212 // stretched_vector = (raw_vector - gravity) * stretch_ratio + gravity
213
214 AccelerometerDataEntry& accelerometer_entry =
215 mem->accelerometer.entries[mem->accelerometer.index];
216
217 accelerometer_entry.x = static_cast<s16>(accel.x);
218 accelerometer_entry.y = static_cast<s16>(accel.y);
219 accelerometer_entry.z = static_cast<s16>(accel.z);
220
221 // Make up "raw" entry
222 // TODO(wwylele):
223 // From hardware testing, the raw_entry values are approximately, but not exactly, as twice as
224 // corresponding entries (or with a minus sign). It may caused by system calibration to the
225 // accelerometer. Figure out how it works, or, if no game reads raw_entry, the following three
226 // lines can be removed and leave raw_entry unimplemented.
227 mem->accelerometer.raw_entry.x = -2 * accelerometer_entry.x;
228 mem->accelerometer.raw_entry.z = 2 * accelerometer_entry.y;
229 mem->accelerometer.raw_entry.y = -2 * accelerometer_entry.z;
230
231 // If we just updated index 0, provide a new timestamp
232 if (mem->accelerometer.index == 0) {
233 mem->accelerometer.index_reset_ticks_previous = mem->accelerometer.index_reset_ticks;
234 mem->accelerometer.index_reset_ticks = (s64)CoreTiming::GetTicks();
235 }
236
237 event_accelerometer->Signal();
238
239 // Reschedule recurrent event
240 CoreTiming::ScheduleEvent(accelerometer_update_ticks - cycles_late, accelerometer_update_event);
241}
242
243static void UpdateGyroscopeCallback(u64 userdata, int cycles_late) {
244 SharedMem* mem = reinterpret_cast<SharedMem*>(shared_mem->GetPointer());
245
246 mem->gyroscope.index = next_gyroscope_index;
247 next_gyroscope_index = (next_gyroscope_index + 1) % mem->gyroscope.entries.size();
248
249 GyroscopeDataEntry& gyroscope_entry = mem->gyroscope.entries[mem->gyroscope.index];
250
251 Math::Vec3<float> gyro;
252 std::tie(std::ignore, gyro) = motion_device->GetStatus();
253 double stretch = Core::System::GetInstance().perf_stats.GetLastFrameTimeScale();
254 gyro *= gyroscope_coef * static_cast<float>(stretch);
255 gyroscope_entry.x = static_cast<s16>(gyro.x);
256 gyroscope_entry.y = static_cast<s16>(gyro.y);
257 gyroscope_entry.z = static_cast<s16>(gyro.z);
258
259 // Make up "raw" entry
260 mem->gyroscope.raw_entry.x = gyroscope_entry.x;
261 mem->gyroscope.raw_entry.z = -gyroscope_entry.y;
262 mem->gyroscope.raw_entry.y = gyroscope_entry.z;
263
264 // If we just updated index 0, provide a new timestamp
265 if (mem->gyroscope.index == 0) {
266 mem->gyroscope.index_reset_ticks_previous = mem->gyroscope.index_reset_ticks;
267 mem->gyroscope.index_reset_ticks = (s64)CoreTiming::GetTicks();
268 }
269
270 event_gyroscope->Signal();
271
272 // Reschedule recurrent event
273 CoreTiming::ScheduleEvent(gyroscope_update_ticks - cycles_late, gyroscope_update_event);
274}
275
276void GetIPCHandles(Service::Interface* self) {
277 u32* cmd_buff = Kernel::GetCommandBuffer();
278
279 cmd_buff[1] = 0; // No error
280 cmd_buff[2] = 0x14000000; // IPC Command Structure translate-header
281 // TODO(yuriks): Return error from SendSyncRequest is this fails (part of IPC marshalling)
282 cmd_buff[3] = Kernel::g_handle_table.Create(Service::HID::shared_mem).Unwrap();
283 cmd_buff[4] = Kernel::g_handle_table.Create(Service::HID::event_pad_or_touch_1).Unwrap();
284 cmd_buff[5] = Kernel::g_handle_table.Create(Service::HID::event_pad_or_touch_2).Unwrap();
285 cmd_buff[6] = Kernel::g_handle_table.Create(Service::HID::event_accelerometer).Unwrap();
286 cmd_buff[7] = Kernel::g_handle_table.Create(Service::HID::event_gyroscope).Unwrap();
287 cmd_buff[8] = Kernel::g_handle_table.Create(Service::HID::event_debug_pad).Unwrap();
288}
289
290void EnableAccelerometer(Service::Interface* self) {
291 u32* cmd_buff = Kernel::GetCommandBuffer();
292
293 ++enable_accelerometer_count;
294
295 // Schedules the accelerometer update event if the accelerometer was just enabled
296 if (enable_accelerometer_count == 1) {
297 CoreTiming::ScheduleEvent(accelerometer_update_ticks, accelerometer_update_event);
298 }
299
300 cmd_buff[1] = RESULT_SUCCESS.raw;
301
302 LOG_DEBUG(Service_HID, "called");
303}
304
305void DisableAccelerometer(Service::Interface* self) {
306 u32* cmd_buff = Kernel::GetCommandBuffer();
307
308 --enable_accelerometer_count;
309
310 // Unschedules the accelerometer update event if the accelerometer was just disabled
311 if (enable_accelerometer_count == 0) {
312 CoreTiming::UnscheduleEvent(accelerometer_update_event, 0);
313 }
314
315 cmd_buff[1] = RESULT_SUCCESS.raw;
316
317 LOG_DEBUG(Service_HID, "called");
318}
319
320void EnableGyroscopeLow(Service::Interface* self) {
321 u32* cmd_buff = Kernel::GetCommandBuffer();
322
323 ++enable_gyroscope_count;
324
325 // Schedules the gyroscope update event if the gyroscope was just enabled
326 if (enable_gyroscope_count == 1) {
327 CoreTiming::ScheduleEvent(gyroscope_update_ticks, gyroscope_update_event);
328 }
329
330 cmd_buff[1] = RESULT_SUCCESS.raw;
331
332 LOG_DEBUG(Service_HID, "called");
333}
334
335void DisableGyroscopeLow(Service::Interface* self) {
336 u32* cmd_buff = Kernel::GetCommandBuffer();
337
338 --enable_gyroscope_count;
339
340 // Unschedules the gyroscope update event if the gyroscope was just disabled
341 if (enable_gyroscope_count == 0) {
342 CoreTiming::UnscheduleEvent(gyroscope_update_event, 0);
343 }
344
345 cmd_buff[1] = RESULT_SUCCESS.raw;
346
347 LOG_DEBUG(Service_HID, "called");
348}
349
350void GetGyroscopeLowRawToDpsCoefficient(Service::Interface* self) {
351 u32* cmd_buff = Kernel::GetCommandBuffer();
352
353 cmd_buff[1] = RESULT_SUCCESS.raw;
354
355 f32 coef = gyroscope_coef;
356 memcpy(&cmd_buff[2], &coef, 4);
357}
358
359void GetGyroscopeLowCalibrateParam(Service::Interface* self) {
360 u32* cmd_buff = Kernel::GetCommandBuffer();
361
362 cmd_buff[1] = RESULT_SUCCESS.raw;
363
364 const s16 param_unit = 6700; // an approximate value taken from hw
365 GyroscopeCalibrateParam param = {
366 {0, param_unit, -param_unit}, {0, param_unit, -param_unit}, {0, param_unit, -param_unit},
367 };
368 memcpy(&cmd_buff[2], &param, sizeof(param));
369
370 LOG_WARNING(Service_HID, "(STUBBED) called");
371}
372
373void GetSoundVolume(Service::Interface* self) {
374 u32* cmd_buff = Kernel::GetCommandBuffer();
375
376 const u8 volume = 0x3F; // TODO(purpasmart): Find out if this is the max value for the volume
377
378 cmd_buff[1] = RESULT_SUCCESS.raw;
379 cmd_buff[2] = volume;
380
381 LOG_WARNING(Service_HID, "(STUBBED) called");
382}
383
384void Init() {
385 using namespace Kernel;
386
387 AddService(new HID_U_Interface);
388 AddService(new HID_SPVR_Interface);
389
390 is_device_reload_pending.store(true);
391
392 using Kernel::MemoryPermission;
393 shared_mem =
394 SharedMemory::Create(nullptr, 0x1000, MemoryPermission::ReadWrite, MemoryPermission::Read,
395 0, Kernel::MemoryRegion::BASE, "HID:SharedMemory");
396
397 next_pad_index = 0;
398 next_touch_index = 0;
399 next_accelerometer_index = 0;
400 next_gyroscope_index = 0;
401
402 enable_accelerometer_count = 0;
403 enable_gyroscope_count = 0;
404
405 // Create event handles
406 event_pad_or_touch_1 = Event::Create(ResetType::OneShot, "HID:EventPadOrTouch1");
407 event_pad_or_touch_2 = Event::Create(ResetType::OneShot, "HID:EventPadOrTouch2");
408 event_accelerometer = Event::Create(ResetType::OneShot, "HID:EventAccelerometer");
409 event_gyroscope = Event::Create(ResetType::OneShot, "HID:EventGyroscope");
410 event_debug_pad = Event::Create(ResetType::OneShot, "HID:EventDebugPad");
411
412 // Register update callbacks
413 pad_update_event = CoreTiming::RegisterEvent("HID::UpdatePadCallback", UpdatePadCallback);
414 accelerometer_update_event =
415 CoreTiming::RegisterEvent("HID::UpdateAccelerometerCallback", UpdateAccelerometerCallback);
416 gyroscope_update_event =
417 CoreTiming::RegisterEvent("HID::UpdateGyroscopeCallback", UpdateGyroscopeCallback);
418
419 CoreTiming::ScheduleEvent(pad_update_ticks, pad_update_event);
420}
421
422void Shutdown() {
423 shared_mem = nullptr;
424 event_pad_or_touch_1 = nullptr;
425 event_pad_or_touch_2 = nullptr;
426 event_accelerometer = nullptr;
427 event_gyroscope = nullptr;
428 event_debug_pad = nullptr;
429 UnloadInputDevices();
430}
431
432void ReloadInputDevices() {
433 is_device_reload_pending.store(true);
434}
435 17
436} // namespace HID 18} // namespace HID
437
438} // namespace Service 19} // namespace Service
diff --git a/src/core/hle/service/hid/hid.h b/src/core/hle/service/hid/hid.h
index ef25926b5..a1d227dfe 100644
--- a/src/core/hle/service/hid/hid.h
+++ b/src/core/hle/service/hid/hid.h
@@ -4,270 +4,9 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <array>
8#ifndef _MSC_VER
9#include <cstddef>
10#endif
11#include "common/bit_field.h"
12#include "common/common_funcs.h"
13#include "common/common_types.h"
14#include "core/settings.h"
15
16namespace Service { 7namespace Service {
17
18class Interface;
19
20namespace HID { 8namespace HID {
21 9
22/**
23 * Structure of a Pad controller state.
24 */
25struct PadState {
26 union {
27 u32 hex{};
28
29 BitField<0, 1, u32> a;
30 BitField<1, 1, u32> b;
31 BitField<2, 1, u32> select;
32 BitField<3, 1, u32> start;
33 BitField<4, 1, u32> right;
34 BitField<5, 1, u32> left;
35 BitField<6, 1, u32> up;
36 BitField<7, 1, u32> down;
37 BitField<8, 1, u32> r;
38 BitField<9, 1, u32> l;
39 BitField<10, 1, u32> x;
40 BitField<11, 1, u32> y;
41
42 BitField<28, 1, u32> circle_right;
43 BitField<29, 1, u32> circle_left;
44 BitField<30, 1, u32> circle_up;
45 BitField<31, 1, u32> circle_down;
46 };
47};
48
49/**
50 * Structure of a single entry of Pad state history within HID shared memory
51 */
52struct PadDataEntry {
53 PadState current_state;
54 PadState delta_additions;
55 PadState delta_removals;
56
57 s16 circle_pad_x;
58 s16 circle_pad_y;
59};
60
61/**
62 * Structure of a single entry of touch state history within HID shared memory
63 */
64struct TouchDataEntry {
65 u16 x; ///< Y-coordinate of a touchpad press on the lower screen
66 u16 y; ///< X-coordinate of a touchpad press on the lower screen
67 BitField<0, 7, u32> valid; ///< Set to 1 when this entry contains actual X/Y data, otherwise 0
68};
69
70/**
71 * Structure of a single entry of accelerometer state history within HID shared memory
72 */
73struct AccelerometerDataEntry {
74 s16 x;
75 s16 y;
76 s16 z;
77};
78
79/**
80 * Structure of a single entry of gyroscope state history within HID shared memory
81 */
82struct GyroscopeDataEntry {
83 s16 x;
84 s16 y;
85 s16 z;
86};
87
88/**
89 * Structure of data stored in HID shared memory
90 */
91struct SharedMem {
92 /// Pad data, this is used for buttons and the circle pad
93 struct {
94 s64 index_reset_ticks; ///< CPU tick count for when HID module updated entry index 0
95 s64 index_reset_ticks_previous; ///< Previous `index_reset_ticks`
96 u32 index; ///< Index of the last updated pad state entry
97
98 INSERT_PADDING_WORDS(0x2);
99
100 PadState current_state; ///< Current state of the pad buttons
101
102 // TODO(bunnei): Implement `raw_circle_pad_data` field
103 u32 raw_circle_pad_data; ///< Raw (analog) circle pad data, before being converted
104
105 INSERT_PADDING_WORDS(0x1);
106
107 std::array<PadDataEntry, 8> entries; ///< Last 8 pad entries
108 } pad;
109
110 /// Touchpad data, this is used for touchpad input
111 struct {
112 s64 index_reset_ticks; ///< CPU tick count for when HID module updated entry index 0
113 s64 index_reset_ticks_previous; ///< Previous `index_reset_ticks`
114 u32 index; ///< Index of the last updated touch entry
115
116 INSERT_PADDING_WORDS(0x1);
117
118 // TODO(bunnei): Implement `raw_entry` field
119 TouchDataEntry raw_entry; ///< Raw (analog) touch data, before being converted
120
121 std::array<TouchDataEntry, 8> entries; ///< Last 8 touch entries, in pixel coordinates
122 } touch;
123
124 /// Accelerometer data
125 struct {
126 s64 index_reset_ticks; ///< CPU tick count for when HID module updated entry index 0
127 s64 index_reset_ticks_previous; ///< Previous `index_reset_ticks`
128 u32 index; ///< Index of the last updated accelerometer entry
129
130 INSERT_PADDING_WORDS(0x1);
131
132 AccelerometerDataEntry raw_entry;
133 INSERT_PADDING_BYTES(2);
134
135 std::array<AccelerometerDataEntry, 8> entries;
136 } accelerometer;
137
138 /// Gyroscope data
139 struct {
140 s64 index_reset_ticks; ///< CPU tick count for when HID module updated entry index 0
141 s64 index_reset_ticks_previous; ///< Previous `index_reset_ticks`
142 u32 index; ///< Index of the last updated accelerometer entry
143
144 INSERT_PADDING_WORDS(0x1);
145
146 GyroscopeDataEntry raw_entry;
147 INSERT_PADDING_BYTES(2);
148
149 std::array<GyroscopeDataEntry, 32> entries;
150 } gyroscope;
151};
152
153/**
154 * Structure of calibrate params that GetGyroscopeLowCalibrateParam returns
155 */
156struct GyroscopeCalibrateParam {
157 struct {
158 // TODO (wwylele): figure out the exact meaning of these params
159 s16 zero_point;
160 s16 positive_unit_point;
161 s16 negative_unit_point;
162 } x, y, z;
163};
164
165// TODO: MSVC does not support using offsetof() on non-static data members even though this
166// is technically allowed since C++11. This macro should be enabled once MSVC adds
167// support for that.
168#ifndef _MSC_VER
169#define ASSERT_REG_POSITION(field_name, position) \
170 static_assert(offsetof(SharedMem, field_name) == position * 4, \
171 "Field " #field_name " has invalid position")
172
173ASSERT_REG_POSITION(pad.index_reset_ticks, 0x0);
174ASSERT_REG_POSITION(touch.index_reset_ticks, 0x2A);
175
176#undef ASSERT_REG_POSITION
177#endif // !defined(_MSC_VER)
178
179struct DirectionState {
180 bool up;
181 bool down;
182 bool left;
183 bool right;
184};
185
186/// Translates analog stick axes to directions. This is exposed for ir_rst module to use.
187DirectionState GetStickDirectionState(s16 circle_pad_x, s16 circle_pad_y);
188
189/**
190 * HID::GetIPCHandles service function
191 * Inputs:
192 * None
193 * Outputs:
194 * 1 : Result of function, 0 on success, otherwise error code
195 * 2 : IPC Command Structure translate-header
196 * 3 : Handle to HID shared memory
197 * 4 : Event signaled by HID
198 * 5 : Event signaled by HID
199 * 6 : Event signaled by HID
200 * 7 : Gyroscope event
201 * 8 : Event signaled by HID
202 */
203void GetIPCHandles(Interface* self);
204
205/**
206 * HID::EnableAccelerometer service function
207 * Inputs:
208 * None
209 * Outputs:
210 * 1 : Result of function, 0 on success, otherwise error code
211 */
212void EnableAccelerometer(Interface* self);
213
214/**
215 * HID::DisableAccelerometer service function
216 * Inputs:
217 * None
218 * Outputs:
219 * 1 : Result of function, 0 on success, otherwise error code
220 */
221void DisableAccelerometer(Interface* self);
222
223/**
224 * HID::EnableGyroscopeLow service function
225 * Inputs:
226 * None
227 * Outputs:
228 * 1 : Result of function, 0 on success, otherwise error code
229 */
230void EnableGyroscopeLow(Interface* self);
231
232/**
233 * HID::DisableGyroscopeLow service function
234 * Inputs:
235 * None
236 * Outputs:
237 * 1 : Result of function, 0 on success, otherwise error code
238 */
239void DisableGyroscopeLow(Interface* self);
240
241/**
242 * HID::GetSoundVolume service function
243 * Inputs:
244 * None
245 * Outputs:
246 * 1 : Result of function, 0 on success, otherwise error code
247 * 2 : u8 output value
248 */
249void GetSoundVolume(Interface* self);
250
251/**
252 * HID::GetGyroscopeLowRawToDpsCoefficient service function
253 * Inputs:
254 * None
255 * Outputs:
256 * 1 : Result of function, 0 on success, otherwise error code
257 * 2 : float output value
258 */
259void GetGyroscopeLowRawToDpsCoefficient(Service::Interface* self);
260
261/**
262 * HID::GetGyroscopeLowCalibrateParam service function
263 * Inputs:
264 * None
265 * Outputs:
266 * 1 : Result of function, 0 on success, otherwise error code
267 * 2~6 (18 bytes) : struct GyroscopeCalibrateParam
268 */
269void GetGyroscopeLowCalibrateParam(Service::Interface* self);
270
271/// Initialize HID service 10/// Initialize HID service
272void Init(); 11void Init();
273 12
@@ -276,5 +15,6 @@ void Shutdown();
276 15
277/// Reload input devices. Used when input configuration changed 16/// Reload input devices. Used when input configuration changed
278void ReloadInputDevices(); 17void ReloadInputDevices();
279} 18
280} 19} // namespace HID
20} // namespace Service
diff --git a/src/core/hle/service/hid/hid_spvr.cpp b/src/core/hle/service/hid/hid_spvr.cpp
deleted file mode 100644
index 09007e304..000000000
--- a/src/core/hle/service/hid/hid_spvr.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
1// Copyright 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "core/hle/service/hid/hid.h"
6#include "core/hle/service/hid/hid_spvr.h"
7
8namespace Service {
9namespace HID {
10
11const Interface::FunctionInfo FunctionTable[] = {
12 {0x000A0000, GetIPCHandles, "GetIPCHandles"},
13 {0x000B0000, nullptr, "StartAnalogStickCalibration"},
14 {0x000E0000, nullptr, "GetAnalogStickCalibrateParam"},
15 {0x00110000, EnableAccelerometer, "EnableAccelerometer"},
16 {0x00120000, DisableAccelerometer, "DisableAccelerometer"},
17 {0x00130000, EnableGyroscopeLow, "EnableGyroscopeLow"},
18 {0x00140000, DisableGyroscopeLow, "DisableGyroscopeLow"},
19 {0x00150000, GetGyroscopeLowRawToDpsCoefficient, "GetGyroscopeLowRawToDpsCoefficient"},
20 {0x00160000, GetGyroscopeLowCalibrateParam, "GetGyroscopeLowCalibrateParam"},
21 {0x00170000, GetSoundVolume, "GetSoundVolume"},
22};
23
24HID_SPVR_Interface::HID_SPVR_Interface() {
25 Register(FunctionTable);
26}
27
28} // namespace HID
29} // namespace Service
diff --git a/src/core/hle/service/hid/hid_spvr.h b/src/core/hle/service/hid/hid_spvr.h
deleted file mode 100644
index ba61583d2..000000000
--- a/src/core/hle/service/hid/hid_spvr.h
+++ /dev/null
@@ -1,22 +0,0 @@
1// Copyright 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "core/hle/service/service.h"
8
9namespace Service {
10namespace HID {
11
12class HID_SPVR_Interface : public Service::Interface {
13public:
14 HID_SPVR_Interface();
15
16 std::string GetPortName() const override {
17 return "hid:SPVR";
18 }
19};
20
21} // namespace HID
22} // namespace Service \ No newline at end of file
diff --git a/src/core/hle/service/hid/hid_user.cpp b/src/core/hle/service/hid/hid_user.cpp
deleted file mode 100644
index 42591543c..000000000
--- a/src/core/hle/service/hid/hid_user.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
1// Copyright 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "core/hle/service/hid/hid.h"
6#include "core/hle/service/hid/hid_user.h"
7
8namespace Service {
9namespace HID {
10
11const Interface::FunctionInfo FunctionTable[] = {
12 {0x000A0000, GetIPCHandles, "GetIPCHandles"},
13 {0x000B0000, nullptr, "StartAnalogStickCalibration"},
14 {0x000E0000, nullptr, "GetAnalogStickCalibrateParam"},
15 {0x00110000, EnableAccelerometer, "EnableAccelerometer"},
16 {0x00120000, DisableAccelerometer, "DisableAccelerometer"},
17 {0x00130000, EnableGyroscopeLow, "EnableGyroscopeLow"},
18 {0x00140000, DisableGyroscopeLow, "DisableGyroscopeLow"},
19 {0x00150000, GetGyroscopeLowRawToDpsCoefficient, "GetGyroscopeLowRawToDpsCoefficient"},
20 {0x00160000, GetGyroscopeLowCalibrateParam, "GetGyroscopeLowCalibrateParam"},
21 {0x00170000, GetSoundVolume, "GetSoundVolume"},
22};
23
24HID_U_Interface::HID_U_Interface() {
25 Register(FunctionTable);
26}
27
28} // namespace HID
29} // namespace Service
diff --git a/src/core/hle/service/hid/hid_user.h b/src/core/hle/service/hid/hid_user.h
deleted file mode 100644
index baf7fed79..000000000
--- a/src/core/hle/service/hid/hid_user.h
+++ /dev/null
@@ -1,28 +0,0 @@
1// Copyright 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "core/hle/service/service.h"
8
9// This service is used for interfacing to physical user controls.
10// Uses include game pad controls, touchscreen, accelerometers, gyroscopes, and debug pad.
11
12namespace Service {
13namespace HID {
14
15/**
16 * HID service interface.
17 */
18class HID_U_Interface : public Service::Interface {
19public:
20 HID_U_Interface();
21
22 std::string GetPortName() const override {
23 return "hid:USER";
24 }
25};
26
27} // namespace HID
28} // namespace Service \ No newline at end of file
diff --git a/src/core/hle/service/ns/ns.cpp b/src/core/hle/service/ns/ns.cpp
deleted file mode 100644
index 9e19c38bf..000000000
--- a/src/core/hle/service/ns/ns.cpp
+++ /dev/null
@@ -1,16 +0,0 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "core/hle/service/ns/ns.h"
6#include "core/hle/service/ns/ns_s.h"
7
8namespace Service {
9namespace NS {
10
11void InstallInterfaces(SM::ServiceManager& service_manager) {
12 std::make_shared<NS_S>()->InstallAsService(service_manager);
13}
14
15} // namespace NS
16} // namespace Service
diff --git a/src/core/hle/service/ns/ns.h b/src/core/hle/service/ns/ns.h
deleted file mode 100644
index c3d67d98c..000000000
--- a/src/core/hle/service/ns/ns.h
+++ /dev/null
@@ -1,16 +0,0 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "core/hle/service/service.h"
8
9namespace Service {
10namespace NS {
11
12/// Registers all NS services with the specified service manager.
13void InstallInterfaces(SM::ServiceManager& service_manager);
14
15} // namespace NS
16} // namespace Service
diff --git a/src/core/hle/service/ns/ns_s.cpp b/src/core/hle/service/ns/ns_s.cpp
deleted file mode 100644
index d952888dc..000000000
--- a/src/core/hle/service/ns/ns_s.cpp
+++ /dev/null
@@ -1,34 +0,0 @@
1// Copyright 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "core/hle/service/ns/ns_s.h"
6
7namespace Service {
8namespace NS {
9
10NS_S::NS_S() : ServiceFramework("ns:s", 2) {
11 static const FunctionInfo functions[] = {
12 {0x000100C0, nullptr, "LaunchFIRM"},
13 {0x000200C0, nullptr, "LaunchTitle"},
14 {0x00030000, nullptr, "TerminateApplication"},
15 {0x00040040, nullptr, "TerminateProcess"},
16 {0x000500C0, nullptr, "LaunchApplicationFIRM"},
17 {0x00060042, nullptr, "SetFIRMParams4A0"},
18 {0x00070042, nullptr, "CardUpdateInitialize"},
19 {0x00080000, nullptr, "CardUpdateShutdown"},
20 {0x000D0140, nullptr, "SetTWLBannerHMAC"},
21 {0x000E0000, nullptr, "ShutdownAsync"},
22 {0x00100180, nullptr, "RebootSystem"},
23 {0x00110100, nullptr, "TerminateTitle"},
24 {0x001200C0, nullptr, "SetApplicationCpuTimeLimit"},
25 {0x00150140, nullptr, "LaunchApplication"},
26 {0x00160000, nullptr, "RebootSystemClean"},
27 };
28 RegisterHandlers(functions);
29}
30
31NS_S::~NS_S() = default;
32
33} // namespace NS
34} // namespace Service
diff --git a/src/core/hle/service/ns/ns_s.h b/src/core/hle/service/ns/ns_s.h
deleted file mode 100644
index 660ae453f..000000000
--- a/src/core/hle/service/ns/ns_s.h
+++ /dev/null
@@ -1,21 +0,0 @@
1// Copyright 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "core/hle/kernel/kernel.h"
8#include "core/hle/service/service.h"
9
10namespace Service {
11namespace NS {
12
13/// Interface to "ns:s" service
14class NS_S final : public ServiceFramework<NS_S> {
15public:
16 NS_S();
17 ~NS_S();
18};
19
20} // namespace NS
21} // namespace Service
diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp
deleted file mode 100644
index 7b0342cc9..000000000
--- a/src/core/loader/3dsx.cpp
+++ /dev/null
@@ -1,350 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <vector>
7#include "common/logging/log.h"
8#include "core/file_sys/archive_selfncch.h"
9#include "core/hle/kernel/process.h"
10#include "core/hle/kernel/resource_limit.h"
11#include "core/hle/service/fs/archive.h"
12#include "core/loader/3dsx.h"
13#include "core/memory.h"
14
15namespace Loader {
16
17/*
18 * File layout:
19 * - File header
20 * - Code, rodata and data relocation table headers
21 * - Code segment
22 * - Rodata segment
23 * - Loadable (non-BSS) part of the data segment
24 * - Code relocation table
25 * - Rodata relocation table
26 * - Data relocation table
27 *
28 * Memory layout before relocations are applied:
29 * [0..codeSegSize) -> code segment
30 * [codeSegSize..rodataSegSize) -> rodata segment
31 * [rodataSegSize..dataSegSize) -> data segment
32 *
33 * Memory layout after relocations are applied: well, however the loader sets it up :)
34 * The entrypoint is always the start of the code segment.
35 * The BSS section must be cleared manually by the application.
36 */
37
38enum THREEDSX_Error { ERROR_NONE = 0, ERROR_READ = 1, ERROR_FILE = 2, ERROR_ALLOC = 3 };
39
40static const u32 RELOCBUFSIZE = 512;
41static const unsigned int NUM_SEGMENTS = 3;
42
43// File header
44#pragma pack(1)
45struct THREEDSX_Header {
46 u32 magic;
47 u16 header_size, reloc_hdr_size;
48 u32 format_ver;
49 u32 flags;
50
51 // Sizes of the code, rodata and data segments +
52 // size of the BSS section (uninitialized latter half of the data segment)
53 u32 code_seg_size, rodata_seg_size, data_seg_size, bss_size;
54 // offset and size of smdh
55 u32 smdh_offset, smdh_size;
56 // offset to filesystem
57 u32 fs_offset;
58};
59
60// Relocation header: all fields (even extra unknown fields) are guaranteed to be relocation counts.
61struct THREEDSX_RelocHdr {
62 // # of absolute relocations (that is, fix address to post-relocation memory layout)
63 u32 cross_segment_absolute;
64 // # of cross-segment relative relocations (that is, 32bit signed offsets that need to be
65 // patched)
66 u32 cross_segment_relative;
67 // more?
68
69 // Relocations are written in this order:
70 // - Absolute relocations
71 // - Relative relocations
72};
73
74// Relocation entry: from the current pointer, skip X words and patch Y words
75struct THREEDSX_Reloc {
76 u16 skip, patch;
77};
78#pragma pack()
79
80struct THREEloadinfo {
81 u8* seg_ptrs[3]; // code, rodata & data
82 u32 seg_addrs[3];
83 u32 seg_sizes[3];
84};
85
86static u32 TranslateAddr(u32 addr, const THREEloadinfo* loadinfo, u32* offsets) {
87 if (addr < offsets[0])
88 return loadinfo->seg_addrs[0] + addr;
89 if (addr < offsets[1])
90 return loadinfo->seg_addrs[1] + addr - offsets[0];
91 return loadinfo->seg_addrs[2] + addr - offsets[1];
92}
93
94using Kernel::CodeSet;
95using Kernel::SharedPtr;
96
97static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr,
98 SharedPtr<CodeSet>* out_codeset) {
99 if (!file.IsOpen())
100 return ERROR_FILE;
101
102 // Reset read pointer in case this file has been read before.
103 file.Seek(0, SEEK_SET);
104
105 THREEDSX_Header hdr;
106 if (file.ReadBytes(&hdr, sizeof(hdr)) != sizeof(hdr))
107 return ERROR_READ;
108
109 THREEloadinfo loadinfo;
110 // loadinfo segments must be a multiple of 0x1000
111 loadinfo.seg_sizes[0] = (hdr.code_seg_size + 0xFFF) & ~0xFFF;
112 loadinfo.seg_sizes[1] = (hdr.rodata_seg_size + 0xFFF) & ~0xFFF;
113 loadinfo.seg_sizes[2] = (hdr.data_seg_size + 0xFFF) & ~0xFFF;
114 u32 offsets[2] = {loadinfo.seg_sizes[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1]};
115 u32 n_reloc_tables = hdr.reloc_hdr_size / sizeof(u32);
116 std::vector<u8> program_image(loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] +
117 loadinfo.seg_sizes[2]);
118
119 loadinfo.seg_addrs[0] = base_addr;
120 loadinfo.seg_addrs[1] = loadinfo.seg_addrs[0] + loadinfo.seg_sizes[0];
121 loadinfo.seg_addrs[2] = loadinfo.seg_addrs[1] + loadinfo.seg_sizes[1];
122 loadinfo.seg_ptrs[0] = program_image.data();
123 loadinfo.seg_ptrs[1] = loadinfo.seg_ptrs[0] + loadinfo.seg_sizes[0];
124 loadinfo.seg_ptrs[2] = loadinfo.seg_ptrs[1] + loadinfo.seg_sizes[1];
125
126 // Skip header for future compatibility
127 file.Seek(hdr.header_size, SEEK_SET);
128
129 // Read the relocation headers
130 std::vector<u32> relocs(n_reloc_tables * NUM_SEGMENTS);
131 for (unsigned int current_segment = 0; current_segment < NUM_SEGMENTS; ++current_segment) {
132 size_t size = n_reloc_tables * sizeof(u32);
133 if (file.ReadBytes(&relocs[current_segment * n_reloc_tables], size) != size)
134 return ERROR_READ;
135 }
136
137 // Read the segments
138 if (file.ReadBytes(loadinfo.seg_ptrs[0], hdr.code_seg_size) != hdr.code_seg_size)
139 return ERROR_READ;
140 if (file.ReadBytes(loadinfo.seg_ptrs[1], hdr.rodata_seg_size) != hdr.rodata_seg_size)
141 return ERROR_READ;
142 if (file.ReadBytes(loadinfo.seg_ptrs[2], hdr.data_seg_size - hdr.bss_size) !=
143 hdr.data_seg_size - hdr.bss_size)
144 return ERROR_READ;
145
146 // BSS clear
147 memset((char*)loadinfo.seg_ptrs[2] + hdr.data_seg_size - hdr.bss_size, 0, hdr.bss_size);
148
149 // Relocate the segments
150 for (unsigned int current_segment = 0; current_segment < NUM_SEGMENTS; ++current_segment) {
151 for (unsigned current_segment_reloc_table = 0; current_segment_reloc_table < n_reloc_tables;
152 current_segment_reloc_table++) {
153 u32 n_relocs = relocs[current_segment * n_reloc_tables + current_segment_reloc_table];
154 if (current_segment_reloc_table >= 2) {
155 // We are not using this table - ignore it because we don't know what it dose
156 file.Seek(n_relocs * sizeof(THREEDSX_Reloc), SEEK_CUR);
157 continue;
158 }
159 THREEDSX_Reloc reloc_table[RELOCBUFSIZE];
160
161 u32* pos = (u32*)loadinfo.seg_ptrs[current_segment];
162 const u32* end_pos = pos + (loadinfo.seg_sizes[current_segment] / 4);
163
164 while (n_relocs) {
165 u32 remaining = std::min(RELOCBUFSIZE, n_relocs);
166 n_relocs -= remaining;
167
168 if (file.ReadBytes(reloc_table, remaining * sizeof(THREEDSX_Reloc)) !=
169 remaining * sizeof(THREEDSX_Reloc))
170 return ERROR_READ;
171
172 for (unsigned current_inprogress = 0;
173 current_inprogress < remaining && pos < end_pos; current_inprogress++) {
174 const auto& table = reloc_table[current_inprogress];
175 LOG_TRACE(Loader, "(t=%d,skip=%u,patch=%u)", current_segment_reloc_table,
176 static_cast<u32>(table.skip), static_cast<u32>(table.patch));
177 pos += table.skip;
178 s32 num_patches = table.patch;
179 while (0 < num_patches && pos < end_pos) {
180 u32 in_addr = base_addr + static_cast<u32>(reinterpret_cast<u8*>(pos) -
181 program_image.data());
182 u32 orig_data = *pos;
183 u32 sub_type = orig_data >> (32 - 4);
184 u32 addr = TranslateAddr(orig_data & ~0xF0000000, &loadinfo, offsets);
185 LOG_TRACE(Loader, "Patching %08X <-- rel(%08X,%d) (%08X)", in_addr, addr,
186 current_segment_reloc_table, *pos);
187 switch (current_segment_reloc_table) {
188 case 0: {
189 if (sub_type != 0)
190 return ERROR_READ;
191 *pos = addr;
192 break;
193 }
194 case 1: {
195 u32 data = addr - in_addr;
196 switch (sub_type) {
197 case 0: // 32-bit signed offset
198 *pos = data;
199 break;
200 case 1: // 31-bit signed offset
201 *pos = data & ~(1U << 31);
202 break;
203 default:
204 return ERROR_READ;
205 }
206 break;
207 }
208 default:
209 break; // this should never happen
210 }
211 pos++;
212 num_patches--;
213 }
214 }
215 }
216 }
217 }
218
219 // Create the CodeSet
220 SharedPtr<CodeSet> code_set = CodeSet::Create("", 0);
221
222 code_set->code.offset = loadinfo.seg_ptrs[0] - program_image.data();
223 code_set->code.addr = loadinfo.seg_addrs[0];
224 code_set->code.size = loadinfo.seg_sizes[0];
225
226 code_set->rodata.offset = loadinfo.seg_ptrs[1] - program_image.data();
227 code_set->rodata.addr = loadinfo.seg_addrs[1];
228 code_set->rodata.size = loadinfo.seg_sizes[1];
229
230 code_set->data.offset = loadinfo.seg_ptrs[2] - program_image.data();
231 code_set->data.addr = loadinfo.seg_addrs[2];
232 code_set->data.size = loadinfo.seg_sizes[2];
233
234 code_set->entrypoint = code_set->code.addr;
235 code_set->memory = std::make_shared<std::vector<u8>>(std::move(program_image));
236
237 LOG_DEBUG(Loader, "code size: 0x%X", loadinfo.seg_sizes[0]);
238 LOG_DEBUG(Loader, "rodata size: 0x%X", loadinfo.seg_sizes[1]);
239 LOG_DEBUG(Loader, "data size: 0x%X (including 0x%X of bss)", loadinfo.seg_sizes[2],
240 hdr.bss_size);
241
242 *out_codeset = code_set;
243 return ERROR_NONE;
244}
245
246FileType AppLoader_THREEDSX::IdentifyType(FileUtil::IOFile& file) {
247 u32 magic;
248 file.Seek(0, SEEK_SET);
249 if (1 != file.ReadArray<u32>(&magic, 1))
250 return FileType::Error;
251
252 if (MakeMagic('3', 'D', 'S', 'X') == magic)
253 return FileType::THREEDSX;
254
255 return FileType::Error;
256}
257
258ResultStatus AppLoader_THREEDSX::Load(Kernel::SharedPtr<Kernel::Process>& process) {
259 if (is_loaded)
260 return ResultStatus::ErrorAlreadyLoaded;
261
262 if (!file.IsOpen())
263 return ResultStatus::Error;
264
265 SharedPtr<CodeSet> codeset;
266 if (Load3DSXFile(file, Memory::PROCESS_IMAGE_VADDR, &codeset) != ERROR_NONE)
267 return ResultStatus::Error;
268 codeset->name = filename;
269
270 process = Kernel::Process::Create("main");
271 process->LoadModule(codeset, codeset->entrypoint);
272 process->svc_access_mask.set();
273 process->address_mappings = default_address_mappings;
274
275 // Attach the default resource limit (APPLICATION) to the process
276 process->resource_limit =
277 Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
278 process->Run(codeset->entrypoint, 48, Kernel::DEFAULT_STACK_SIZE);
279
280 Service::FS::RegisterSelfNCCH(*this);
281
282 is_loaded = true;
283 return ResultStatus::Success;
284}
285
286ResultStatus AppLoader_THREEDSX::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file,
287 u64& offset, u64& size) {
288 if (!file.IsOpen())
289 return ResultStatus::Error;
290
291 // Reset read pointer in case this file has been read before.
292 file.Seek(0, SEEK_SET);
293
294 THREEDSX_Header hdr;
295 if (file.ReadBytes(&hdr, sizeof(THREEDSX_Header)) != sizeof(THREEDSX_Header))
296 return ResultStatus::Error;
297
298 if (hdr.header_size != sizeof(THREEDSX_Header))
299 return ResultStatus::Error;
300
301 // Check if the 3DSX has a RomFS...
302 if (hdr.fs_offset != 0) {
303 u32 romfs_offset = hdr.fs_offset;
304 u32 romfs_size = static_cast<u32>(file.GetSize()) - hdr.fs_offset;
305
306 LOG_DEBUG(Loader, "RomFS offset: 0x%08X", romfs_offset);
307 LOG_DEBUG(Loader, "RomFS size: 0x%08X", romfs_size);
308
309 // We reopen the file, to allow its position to be independent from file's
310 romfs_file = std::make_shared<FileUtil::IOFile>(filepath, "rb");
311 if (!romfs_file->IsOpen())
312 return ResultStatus::Error;
313
314 offset = romfs_offset;
315 size = romfs_size;
316
317 return ResultStatus::Success;
318 }
319 LOG_DEBUG(Loader, "3DSX has no RomFS");
320 return ResultStatus::ErrorNotUsed;
321}
322
323ResultStatus AppLoader_THREEDSX::ReadIcon(std::vector<u8>& buffer) {
324 if (!file.IsOpen())
325 return ResultStatus::Error;
326
327 // Reset read pointer in case this file has been read before.
328 file.Seek(0, SEEK_SET);
329
330 THREEDSX_Header hdr;
331 if (file.ReadBytes(&hdr, sizeof(THREEDSX_Header)) != sizeof(THREEDSX_Header))
332 return ResultStatus::Error;
333
334 if (hdr.header_size != sizeof(THREEDSX_Header))
335 return ResultStatus::Error;
336
337 // Check if the 3DSX has a SMDH...
338 if (hdr.smdh_offset != 0) {
339 file.Seek(hdr.smdh_offset, SEEK_SET);
340 buffer.resize(hdr.smdh_size);
341
342 if (file.ReadBytes(&buffer[0], hdr.smdh_size) != hdr.smdh_size)
343 return ResultStatus::Error;
344
345 return ResultStatus::Success;
346 }
347 return ResultStatus::ErrorNotUsed;
348}
349
350} // namespace Loader
diff --git a/src/core/loader/3dsx.h b/src/core/loader/3dsx.h
deleted file mode 100644
index 1e59bbb9d..000000000
--- a/src/core/loader/3dsx.h
+++ /dev/null
@@ -1,46 +0,0 @@
1// Copyright 2014 Dolphin Emulator Project / Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <string>
8#include "common/common_types.h"
9#include "core/loader/loader.h"
10
11////////////////////////////////////////////////////////////////////////////////////////////////////
12// Loader namespace
13
14namespace Loader {
15
16/// Loads an 3DSX file
17class AppLoader_THREEDSX final : public AppLoader {
18public:
19 AppLoader_THREEDSX(FileUtil::IOFile&& file, const std::string& filename,
20 const std::string& filepath)
21 : AppLoader(std::move(file)), filename(std::move(filename)), filepath(filepath) {}
22
23 /**
24 * Returns the type of the file
25 * @param file FileUtil::IOFile open file
26 * @return FileType found, or FileType::Error if this loader doesn't know it
27 */
28 static FileType IdentifyType(FileUtil::IOFile& file);
29
30 FileType GetFileType() override {
31 return IdentifyType(file);
32 }
33
34 ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override;
35
36 ResultStatus ReadIcon(std::vector<u8>& buffer) override;
37
38 ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset,
39 u64& size) override;
40
41private:
42 std::string filename;
43 std::string filepath;
44};
45
46} // namespace Loader
diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp
index 73318c584..46b2c28d2 100644
--- a/src/core/loader/loader.cpp
+++ b/src/core/loader/loader.cpp
@@ -31,9 +31,7 @@ FileType IdentifyFile(FileUtil::IOFile& file) {
31 if (FileType::Error != type) \ 31 if (FileType::Error != type) \
32 return type; 32 return type;
33 33
34 CHECK_TYPE(THREEDSX)
35 CHECK_TYPE(ELF) 34 CHECK_TYPE(ELF)
36 CHECK_TYPE(NCCH)
37 CHECK_TYPE(NSO) 35 CHECK_TYPE(NSO)
38 CHECK_TYPE(NRO) 36 CHECK_TYPE(NRO)
39 37
@@ -58,33 +56,13 @@ FileType GuessFromExtension(const std::string& extension_) {
58 if (extension == ".elf" || extension == ".axf") 56 if (extension == ".elf" || extension == ".axf")
59 return FileType::ELF; 57 return FileType::ELF;
60 58
61 if (extension == ".cci" || extension == ".3ds")
62 return FileType::CCI;
63
64 if (extension == ".cxi")
65 return FileType::CXI;
66
67 if (extension == ".3dsx")
68 return FileType::THREEDSX;
69
70 if (extension == ".cia")
71 return FileType::CIA;
72
73 return FileType::Unknown; 59 return FileType::Unknown;
74} 60}
75 61
76const char* GetFileTypeString(FileType type) { 62const char* GetFileTypeString(FileType type) {
77 switch (type) { 63 switch (type) {
78 case FileType::CCI:
79 return "NCSD";
80 case FileType::CXI:
81 return "NCCH";
82 case FileType::CIA:
83 return "CIA";
84 case FileType::ELF: 64 case FileType::ELF:
85 return "ELF"; 65 return "ELF";
86 case FileType::THREEDSX:
87 return "3DSX";
88 case FileType::Error: 66 case FileType::Error:
89 case FileType::Unknown: 67 case FileType::Unknown:
90 break; 68 break;
@@ -106,19 +84,10 @@ static std::unique_ptr<AppLoader> GetFileLoader(FileUtil::IOFile&& file, FileTyp
106 const std::string& filepath) { 84 const std::string& filepath) {
107 switch (type) { 85 switch (type) {
108 86
109 // 3DSX file format.
110 case FileType::THREEDSX:
111 return std::make_unique<AppLoader_THREEDSX>(std::move(file), filename, filepath);
112
113 // Standard ELF file format. 87 // Standard ELF file format.
114 case FileType::ELF: 88 case FileType::ELF:
115 return std::make_unique<AppLoader_ELF>(std::move(file), filename); 89 return std::make_unique<AppLoader_ELF>(std::move(file), filename);
116 90
117 // NCCH/NCSD container formats.
118 case FileType::CXI:
119 case FileType::CCI:
120 return std::make_unique<AppLoader_NCCH>(std::move(file), filepath);
121
122 // NX NSO file format. 91 // NX NSO file format.
123 case FileType::NSO: 92 case FileType::NSO:
124 return std::make_unique<AppLoader_NSO>(std::move(file), filepath); 93 return std::make_unique<AppLoader_NSO>(std::move(file), filepath);
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h
index 311785d05..ac4e7acc2 100644
--- a/src/core/loader/loader.h
+++ b/src/core/loader/loader.h
@@ -29,11 +29,7 @@ namespace Loader {
29enum class FileType { 29enum class FileType {
30 Error, 30 Error,
31 Unknown, 31 Unknown,
32 CCI,
33 CXI,
34 CIA,
35 ELF, 32 ELF,
36 THREEDSX, // 3DSX
37 NSO, 33 NSO,
38 NRO, 34 NRO,
39}; 35};
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp
deleted file mode 100644
index e33a37b2e..000000000
--- a/src/core/loader/ncch.cpp
+++ /dev/null
@@ -1,263 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <cinttypes>
7#include <codecvt>
8#include <cstring>
9#include <locale>
10#include <memory>
11#include "common/logging/log.h"
12#include "common/string_util.h"
13#include "common/swap.h"
14#include "core/core.h"
15#include "core/file_sys/archive_selfncch.h"
16#include "core/file_sys/ncch_container.h"
17#include "core/file_sys/title_metadata.h"
18#include "core/hle/kernel/process.h"
19#include "core/hle/kernel/resource_limit.h"
20#include "core/hle/service/cfg/cfg.h"
21#include "core/hle/service/fs/archive.h"
22#include "core/loader/ncch.h"
23#include "core/loader/smdh.h"
24#include "core/memory.h"
25#include "network/network.h"
26
27////////////////////////////////////////////////////////////////////////////////////////////////////
28// Loader namespace
29
30namespace Loader {
31
32static const u64 UPDATE_MASK = 0x0000000e00000000;
33
34FileType AppLoader_NCCH::IdentifyType(FileUtil::IOFile& file) {
35 u32 magic;
36 file.Seek(0x100, SEEK_SET);
37 if (1 != file.ReadArray<u32>(&magic, 1))
38 return FileType::Error;
39
40 if (MakeMagic('N', 'C', 'S', 'D') == magic)
41 return FileType::CCI;
42
43 if (MakeMagic('N', 'C', 'C', 'H') == magic)
44 return FileType::CXI;
45
46 return FileType::Error;
47}
48
49static std::string GetUpdateNCCHPath(u64_le program_id) {
50 u32 high = static_cast<u32>((program_id | UPDATE_MASK) >> 32);
51 u32 low = static_cast<u32>((program_id | UPDATE_MASK) & 0xFFFFFFFF);
52
53 // TODO(shinyquagsire23): Title database should be doing this path lookup
54 std::string content_path = Common::StringFromFormat(
55 "%sNintendo 3DS/%s/%s/title/%08x/%08x/content/", FileUtil::GetUserPath(D_SDMC_IDX).c_str(),
56 SYSTEM_ID, SDCARD_ID, high, low);
57 std::string tmd_path = content_path + "00000000.tmd";
58
59 u32 content_id = 0;
60 FileSys::TitleMetadata tmd(tmd_path);
61 if (tmd.Load() == ResultStatus::Success) {
62 content_id = tmd.GetBootContentID();
63 }
64
65 return Common::StringFromFormat("%s%08x.app", content_path.c_str(), content_id);
66}
67
68std::pair<boost::optional<u32>, ResultStatus> AppLoader_NCCH::LoadKernelSystemMode() {
69 if (!is_loaded) {
70 ResultStatus res = base_ncch.Load();
71 if (res != ResultStatus::Success) {
72 return std::make_pair(boost::none, res);
73 }
74 }
75
76 // Set the system mode as the one from the exheader.
77 return std::make_pair(overlay_ncch->exheader_header.arm11_system_local_caps.system_mode.Value(),
78 ResultStatus::Success);
79}
80
81ResultStatus AppLoader_NCCH::LoadExec(Kernel::SharedPtr<Kernel::Process>& process) {
82 using Kernel::CodeSet;
83 using Kernel::SharedPtr;
84
85 if (!is_loaded)
86 return ResultStatus::ErrorNotLoaded;
87
88 std::vector<u8> code;
89 u64_le program_id;
90 if (ResultStatus::Success == ReadCode(code) &&
91 ResultStatus::Success == ReadProgramId(program_id)) {
92 std::string process_name = Common::StringFromFixedZeroTerminatedBuffer(
93 (const char*)overlay_ncch->exheader_header.codeset_info.name, 8);
94
95 SharedPtr<CodeSet> codeset = CodeSet::Create(process_name, program_id);
96
97 codeset->code.offset = 0;
98 codeset->code.addr = overlay_ncch->exheader_header.codeset_info.text.address;
99 codeset->code.size =
100 overlay_ncch->exheader_header.codeset_info.text.num_max_pages * Memory::PAGE_SIZE;
101
102 codeset->rodata.offset = codeset->code.offset + codeset->code.size;
103 codeset->rodata.addr = overlay_ncch->exheader_header.codeset_info.ro.address;
104 codeset->rodata.size =
105 overlay_ncch->exheader_header.codeset_info.ro.num_max_pages * Memory::PAGE_SIZE;
106
107 // TODO(yuriks): Not sure if the bss size is added to the page-aligned .data size or just
108 // to the regular size. Playing it safe for now.
109 u32 bss_page_size = (overlay_ncch->exheader_header.codeset_info.bss_size + 0xFFF) & ~0xFFF;
110 code.resize(code.size() + bss_page_size, 0);
111
112 codeset->data.offset = codeset->rodata.offset + codeset->rodata.size;
113 codeset->data.addr = overlay_ncch->exheader_header.codeset_info.data.address;
114 codeset->data.size =
115 overlay_ncch->exheader_header.codeset_info.data.num_max_pages * Memory::PAGE_SIZE +
116 bss_page_size;
117
118 codeset->entrypoint = codeset->code.addr;
119 codeset->memory = std::make_shared<std::vector<u8>>(std::move(code));
120
121 process = Kernel::Process::Create("main");
122 process->LoadModule(codeset, codeset->entrypoint);
123
124 // Attach a resource limit to the process based on the resource limit category
125 process->resource_limit =
126 Kernel::ResourceLimit::GetForCategory(static_cast<Kernel::ResourceLimitCategory>(
127 overlay_ncch->exheader_header.arm11_system_local_caps.resource_limit_category));
128
129 // Set the default CPU core for this process
130 process->ideal_processor =
131 overlay_ncch->exheader_header.arm11_system_local_caps.ideal_processor;
132
133 // Copy data while converting endianness
134 std::array<u32, ARRAY_SIZE(overlay_ncch->exheader_header.arm11_kernel_caps.descriptors)>
135 kernel_caps;
136 std::copy_n(overlay_ncch->exheader_header.arm11_kernel_caps.descriptors, kernel_caps.size(),
137 begin(kernel_caps));
138 process->ParseKernelCaps(kernel_caps.data(), kernel_caps.size());
139
140 s32 priority = overlay_ncch->exheader_header.arm11_system_local_caps.priority;
141 u32 stack_size = overlay_ncch->exheader_header.codeset_info.stack_size;
142 process->Run(codeset->entrypoint, priority, stack_size);
143 return ResultStatus::Success;
144 }
145 return ResultStatus::Error;
146}
147
148void AppLoader_NCCH::ParseRegionLockoutInfo() {
149 std::vector<u8> smdh_buffer;
150 if (ReadIcon(smdh_buffer) == ResultStatus::Success && smdh_buffer.size() >= sizeof(SMDH)) {
151 SMDH smdh;
152 memcpy(&smdh, smdh_buffer.data(), sizeof(SMDH));
153 u32 region_lockout = smdh.region_lockout;
154 constexpr u32 REGION_COUNT = 7;
155 for (u32 region = 0; region < REGION_COUNT; ++region) {
156 if (region_lockout & 1) {
157 Service::CFG::SetPreferredRegionCode(region);
158 break;
159 }
160 region_lockout >>= 1;
161 }
162 }
163}
164
165ResultStatus AppLoader_NCCH::Load(Kernel::SharedPtr<Kernel::Process>& process) {
166 u64_le ncch_program_id;
167
168 if (is_loaded)
169 return ResultStatus::ErrorAlreadyLoaded;
170
171 ResultStatus result = base_ncch.Load();
172 if (result != ResultStatus::Success)
173 return result;
174
175 ReadProgramId(ncch_program_id);
176 std::string program_id{Common::StringFromFormat("%016" PRIX64, ncch_program_id)};
177
178 LOG_INFO(Loader, "Program ID: %s", program_id.c_str());
179
180 update_ncch.OpenFile(GetUpdateNCCHPath(ncch_program_id));
181 result = update_ncch.Load();
182 if (result == ResultStatus::Success) {
183 overlay_ncch = &update_ncch;
184 }
185
186 Core::Telemetry().AddField(Telemetry::FieldType::Session, "ProgramId", program_id);
187
188 if (auto room_member = Network::GetRoomMember().lock()) {
189 Network::GameInfo game_info;
190 ReadTitle(game_info.name);
191 game_info.id = ncch_program_id;
192 room_member->SendGameInfo(game_info);
193 }
194
195 is_loaded = true; // Set state to loaded
196
197 result = LoadExec(process); // Load the executable into memory for booting
198 if (ResultStatus::Success != result)
199 return result;
200
201 Service::FS::RegisterSelfNCCH(*this);
202
203 ParseRegionLockoutInfo();
204
205 return ResultStatus::Success;
206}
207
208ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) {
209 return overlay_ncch->LoadSectionExeFS(".code", buffer);
210}
211
212ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) {
213 return overlay_ncch->LoadSectionExeFS("icon", buffer);
214}
215
216ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) {
217 return overlay_ncch->LoadSectionExeFS("banner", buffer);
218}
219
220ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) {
221 return overlay_ncch->LoadSectionExeFS("logo", buffer);
222}
223
224ResultStatus AppLoader_NCCH::ReadProgramId(u64& out_program_id) {
225 ResultStatus result = base_ncch.ReadProgramId(out_program_id);
226 if (result != ResultStatus::Success)
227 return result;
228
229 return ResultStatus::Success;
230}
231
232ResultStatus AppLoader_NCCH::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset,
233 u64& size) {
234 return base_ncch.ReadRomFS(romfs_file, offset, size);
235}
236
237ResultStatus AppLoader_NCCH::ReadUpdateRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file,
238 u64& offset, u64& size) {
239 ResultStatus result = update_ncch.ReadRomFS(romfs_file, offset, size);
240
241 if (result != ResultStatus::Success)
242 return base_ncch.ReadRomFS(romfs_file, offset, size);
243}
244
245ResultStatus AppLoader_NCCH::ReadTitle(std::string& title) {
246 std::vector<u8> data;
247 Loader::SMDH smdh;
248 ReadIcon(data);
249
250 if (!Loader::IsValidSMDH(data)) {
251 return ResultStatus::ErrorInvalidFormat;
252 }
253
254 memcpy(&smdh, data.data(), sizeof(Loader::SMDH));
255
256 const auto& short_title = smdh.GetShortTitle(SMDH::TitleLanguage::English);
257 auto title_end = std::find(short_title.begin(), short_title.end(), u'\0');
258 title = Common::UTF16ToUTF8(std::u16string{short_title.begin(), title_end});
259
260 return ResultStatus::Success;
261}
262
263} // namespace Loader
diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h
deleted file mode 100644
index 09230ae33..000000000
--- a/src/core/loader/ncch.h
+++ /dev/null
@@ -1,80 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <memory>
8#include "common/common_types.h"
9#include "common/swap.h"
10#include "core/file_sys/ncch_container.h"
11#include "core/loader/loader.h"
12
13////////////////////////////////////////////////////////////////////////////////////////////////////
14// Loader namespace
15
16namespace Loader {
17
18/// Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI)
19class AppLoader_NCCH final : public AppLoader {
20public:
21 AppLoader_NCCH(FileUtil::IOFile&& file, const std::string& filepath)
22 : AppLoader(std::move(file)), filepath(filepath), base_ncch(filepath),
23 overlay_ncch(&base_ncch) {}
24
25 /**
26 * Returns the type of the file
27 * @param file FileUtil::IOFile open file
28 * @return FileType found, or FileType::Error if this loader doesn't know it
29 */
30 static FileType IdentifyType(FileUtil::IOFile& file);
31
32 FileType GetFileType() override {
33 return IdentifyType(file);
34 }
35
36 ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) override;
37
38 /**
39 * Loads the Exheader and returns the system mode for this application.
40 * @returns A pair with the optional system mode, and and the status.
41 */
42 std::pair<boost::optional<u32>, ResultStatus> LoadKernelSystemMode() override;
43
44 ResultStatus ReadCode(std::vector<u8>& buffer) override;
45
46 ResultStatus ReadIcon(std::vector<u8>& buffer) override;
47
48 ResultStatus ReadBanner(std::vector<u8>& buffer) override;
49
50 ResultStatus ReadLogo(std::vector<u8>& buffer) override;
51
52 ResultStatus ReadProgramId(u64& out_program_id) override;
53
54 ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset,
55 u64& size) override;
56
57 ResultStatus ReadUpdateRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset,
58 u64& size) override;
59
60 ResultStatus ReadTitle(std::string& title) override;
61
62private:
63 /**
64 * Loads .code section into memory for booting
65 * @param process The newly created process
66 * @return ResultStatus result of function
67 */
68 ResultStatus LoadExec(Kernel::SharedPtr<Kernel::Process>& process);
69
70 /// Reads the region lockout info in the SMDH and send it to CFG service
71 void ParseRegionLockoutInfo();
72
73 FileSys::NCCHContainer base_ncch;
74 FileSys::NCCHContainer update_ncch;
75 FileSys::NCCHContainer* overlay_ncch;
76
77 std::string filepath;
78};
79
80} // namespace Loader