2019-06-26 22:46:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "ldr_ecs.hpp"
|
|
|
|
|
2019-10-24 09:30:10 +00:00
|
|
|
namespace ams::ldr::ecs {
|
2019-06-26 22:46:19 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* Convenience definition. */
|
|
|
|
constexpr size_t DeviceNameSizeMax = 0x20;
|
|
|
|
constexpr size_t MaxExternalContentSourceCount = 0x10;
|
|
|
|
|
|
|
|
/* Types. */
|
|
|
|
class ExternalContentSource {
|
|
|
|
NON_COPYABLE(ExternalContentSource);
|
|
|
|
NON_MOVEABLE(ExternalContentSource);
|
|
|
|
private:
|
|
|
|
char device_name[DeviceNameSizeMax];
|
|
|
|
public:
|
2019-10-15 04:40:05 +00:00
|
|
|
ExternalContentSource(const char *dn) {
|
|
|
|
std::strncpy(this->device_name, dn, sizeof(this->device_name));
|
2019-06-26 22:46:19 +00:00
|
|
|
this->device_name[sizeof(this->device_name) - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
~ExternalContentSource() {
|
|
|
|
fsdevUnmountDevice(this->device_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *GetDeviceName() const {
|
|
|
|
return this->device_name;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Global storage. */
|
|
|
|
std::unordered_map<u64, ExternalContentSource> g_map;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* API. */
|
2019-10-28 04:43:01 +00:00
|
|
|
const char *Get(ncm::ProgramId program_id) {
|
|
|
|
auto it = g_map.find(static_cast<u64>(program_id));
|
2019-06-26 22:46:19 +00:00
|
|
|
if (it == g_map.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return it->second.GetDeviceName();
|
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
Result Set(Handle *out, ncm::ProgramId program_id) {
|
2019-10-24 08:40:44 +00:00
|
|
|
/* TODO: Is this an appropriate error? */
|
|
|
|
R_UNLESS(g_map.size() < MaxExternalContentSourceCount, ldr::ResultTooManyArguments());
|
2019-06-26 22:46:19 +00:00
|
|
|
|
|
|
|
/* Clear any sources. */
|
2019-10-28 04:43:01 +00:00
|
|
|
R_ASSERT(Clear(program_id));
|
2019-06-26 22:46:19 +00:00
|
|
|
|
|
|
|
/* Generate mountpoint. */
|
|
|
|
char device_name[DeviceNameSizeMax];
|
2019-10-28 04:43:01 +00:00
|
|
|
std::snprintf(device_name, DeviceNameSizeMax, "ecs-%016lx", static_cast<u64>(program_id));
|
2019-06-26 22:46:19 +00:00
|
|
|
|
|
|
|
/* Create session. */
|
2019-09-28 01:04:58 +00:00
|
|
|
os::ManagedHandle server, client;
|
2019-06-28 00:37:33 +00:00
|
|
|
R_TRY(svcCreateSession(server.GetPointer(), client.GetPointer(), 0, 0));
|
2019-06-26 22:46:19 +00:00
|
|
|
|
|
|
|
/* Create service. */
|
|
|
|
Service srv;
|
|
|
|
serviceCreate(&srv, client.Move());
|
|
|
|
FsFileSystem fs = { srv };
|
2019-10-24 08:40:44 +00:00
|
|
|
auto fs_guard = SCOPE_GUARD { fsFsClose(&fs); };
|
2019-06-26 22:46:19 +00:00
|
|
|
|
|
|
|
/* Try to mount. */
|
2019-10-24 08:40:44 +00:00
|
|
|
R_UNLESS(fsdevMountDevice(device_name, fs) >= 0, fs::ResultMountNameAlreadyExists());
|
|
|
|
fs_guard.Cancel();
|
2019-06-26 22:46:19 +00:00
|
|
|
|
|
|
|
/* Add to map. */
|
2019-10-28 04:43:01 +00:00
|
|
|
g_map.emplace(static_cast<u64>(program_id), device_name);
|
2019-06-26 22:46:19 +00:00
|
|
|
*out = server.Move();
|
2019-10-24 08:40:44 +00:00
|
|
|
return ResultSuccess();
|
2019-06-26 22:46:19 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
Result Clear(ncm::ProgramId program_id) {
|
2019-06-26 22:46:19 +00:00
|
|
|
/* Delete if present. */
|
2019-10-28 04:43:01 +00:00
|
|
|
g_map.erase(static_cast<u64>(program_id));
|
2019-10-24 08:40:44 +00:00
|
|
|
return ResultSuccess();
|
2019-06-26 22:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|