Implement NCM
This commit is contained in:
90
stratosphere/ncm/source/impl/lr_manager.cpp
Normal file
90
stratosphere/ncm/source/impl/lr_manager.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Adubbz
|
||||
*
|
||||
* 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 "../lr_contentlocationresolver.hpp"
|
||||
#include "../lr_redirectonlylocationresolver.hpp"
|
||||
#include "lr_manager.hpp"
|
||||
|
||||
namespace sts::lr::impl {
|
||||
|
||||
namespace {
|
||||
|
||||
BoundedMap<ncm::StorageId, std::shared_ptr<ILocationResolver>, 5> g_location_resolvers;
|
||||
std::shared_ptr<RegisteredLocationResolverInterface> g_registered_location_resolver = nullptr;
|
||||
std::shared_ptr<AddOnContentLocationResolverInterface> g_add_on_content_location_resolver = nullptr;
|
||||
HosMutex g_mutex;
|
||||
|
||||
}
|
||||
|
||||
Result OpenLocationResolver(Out<std::shared_ptr<ILocationResolver>> out, ncm::StorageId storage_id) {
|
||||
std::scoped_lock lk(g_mutex);
|
||||
auto resolver = g_location_resolvers.Find(storage_id);
|
||||
|
||||
if (!resolver) {
|
||||
if (storage_id == ncm::StorageId::Host) {
|
||||
g_location_resolvers[storage_id] = std::make_shared<RedirectOnlyLocationResolverInterface>();
|
||||
} else {
|
||||
auto content_resolver = std::make_shared<ContentLocationResolverInterface>(storage_id);
|
||||
R_TRY(content_resolver->Refresh());
|
||||
g_location_resolvers[storage_id] = std::move(content_resolver);
|
||||
}
|
||||
}
|
||||
|
||||
/* Make a copy of the resolver for output. */
|
||||
auto tmp_resolver = g_location_resolvers[storage_id];
|
||||
out.SetValue(std::move(tmp_resolver));
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result OpenRegisteredLocationResolver(Out<std::shared_ptr<RegisteredLocationResolverInterface>> out) {
|
||||
std::scoped_lock lk(g_mutex);
|
||||
|
||||
if (!g_registered_location_resolver) {
|
||||
g_registered_location_resolver = std::make_shared<RegisteredLocationResolverInterface>();
|
||||
}
|
||||
|
||||
/* Make a copy of the resolver for output. */
|
||||
auto tmp_resolver = g_registered_location_resolver;
|
||||
out.SetValue(std::move(tmp_resolver));
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result RefreshLocationResolver(ncm::StorageId storage_id) {
|
||||
std::scoped_lock lk(g_mutex);
|
||||
auto resolver = g_location_resolvers.Find(storage_id);
|
||||
|
||||
if (!resolver) {
|
||||
return ResultLrUnknownStorageId;
|
||||
}
|
||||
|
||||
(*resolver)->Refresh();
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result OpenAddOnContentLocationResolver(Out<std::shared_ptr<AddOnContentLocationResolverInterface>> out) {
|
||||
std::scoped_lock lk(g_mutex);
|
||||
|
||||
if (!g_add_on_content_location_resolver) {
|
||||
g_add_on_content_location_resolver = std::make_shared<AddOnContentLocationResolverInterface>();
|
||||
}
|
||||
|
||||
/* Make a copy of the resolver for output. */
|
||||
auto tmp_resolver = g_add_on_content_location_resolver;
|
||||
out.SetValue(std::move(tmp_resolver));
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
}
|
||||
33
stratosphere/ncm/source/impl/lr_manager.hpp
Normal file
33
stratosphere/ncm/source/impl/lr_manager.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Adubbz
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <switch.h>
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
#include "../lr_addoncontentlocationresolver.hpp"
|
||||
#include "../lr_ilocationresolver.hpp"
|
||||
#include "../lr_registeredlocationresolver.hpp"
|
||||
|
||||
namespace sts::lr::impl {
|
||||
|
||||
/* Location Resolver API. */
|
||||
Result OpenLocationResolver(Out<std::shared_ptr<ILocationResolver>> out, ncm::StorageId storage_id);
|
||||
Result OpenRegisteredLocationResolver(Out<std::shared_ptr<RegisteredLocationResolverInterface>> out);
|
||||
Result RefreshLocationResolver(ncm::StorageId storage_id);
|
||||
Result OpenAddOnContentLocationResolver(Out<std::shared_ptr<AddOnContentLocationResolverInterface>> out);
|
||||
|
||||
}
|
||||
130
stratosphere/ncm/source/impl/lr_redirection.cpp
Normal file
130
stratosphere/ncm/source/impl/lr_redirection.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Adubbz
|
||||
*
|
||||
* 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 "lr_redirection.hpp"
|
||||
|
||||
namespace sts::lr::impl {
|
||||
|
||||
bool LocationRedirector::FindRedirection(Path *out, ncm::TitleId title_id) {
|
||||
if (this->redirection_list.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (auto it = this->redirection_list.begin(); it != this->redirection_list.end(); it++) {
|
||||
if (it->title_id == title_id) {
|
||||
*out = it->path;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void LocationRedirector::SetRedirection(ncm::TitleId title_id, const Path &path, u32 flags) {
|
||||
this->EraseRedirection(title_id);
|
||||
auto redirection = new LocationRedirection(title_id, path, flags);
|
||||
this->redirection_list.push_back(*redirection);
|
||||
}
|
||||
|
||||
void LocationRedirector::SetRedirectionFlags(ncm::TitleId title_id, u32 flags) {
|
||||
if (!this->redirection_list.empty()) {
|
||||
for (auto it = this->redirection_list.begin(); it != this->redirection_list.end(); it++) {
|
||||
if (it->title_id == title_id) {
|
||||
it->flags = flags;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LocationRedirector::EraseRedirection(ncm::TitleId title_id) {
|
||||
if (!this->redirection_list.empty()) {
|
||||
for (auto it = this->redirection_list.begin(); it != this->redirection_list.end(); it++) {
|
||||
if (it->title_id == title_id) {
|
||||
auto old = it;
|
||||
this->redirection_list.erase(old);
|
||||
delete &(*old);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LocationRedirector::ClearRedirections(u32 flags) {
|
||||
for (auto it = this->redirection_list.begin(); it != this->redirection_list.end();) {
|
||||
if ((it->flags & flags) == flags) {
|
||||
auto old = it;
|
||||
it = this->redirection_list.erase(it);
|
||||
delete &(*old);
|
||||
} else {
|
||||
it++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool RegisteredLocationRedirector::FindRedirection(Path *out, ncm::TitleId title_id) {
|
||||
auto redirection = this->redirections.Find(title_id);
|
||||
|
||||
if (redirection) {
|
||||
*out = *redirection;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool RegisteredLocationRedirector::SetRedirection(ncm::TitleId title_id, const Path& path) {
|
||||
if (this->redirections.IsFull()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this->redirections[title_id] = path;
|
||||
return true;
|
||||
}
|
||||
|
||||
void RegisteredLocationRedirector::EraseRedirection(ncm::TitleId title_id) {
|
||||
this->redirections.Remove(title_id);
|
||||
}
|
||||
|
||||
void RegisteredLocationRedirector::ClearRedirections() {
|
||||
this->redirections.RemoveAll();
|
||||
}
|
||||
|
||||
bool AddOnContentRedirector::FindRedirection(ncm::StorageId *out, ncm::TitleId title_id) {
|
||||
auto redirection = this->redirections.Find(title_id);
|
||||
|
||||
if (redirection) {
|
||||
*out = *redirection;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Result AddOnContentRedirector::SetRedirection(ncm::TitleId title_id, ncm::StorageId storage_id) {
|
||||
if (this->redirections.IsFull()) {
|
||||
return ResultLrTooManyRegisteredPaths;
|
||||
}
|
||||
|
||||
this->redirections[title_id] = storage_id;
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
void AddOnContentRedirector::ClearRedirections() {
|
||||
this->redirections.RemoveAll();
|
||||
}
|
||||
|
||||
}
|
||||
78
stratosphere/ncm/source/impl/lr_redirection.hpp
Normal file
78
stratosphere/ncm/source/impl/lr_redirection.hpp
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Adubbz
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <switch.h>
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
#include "../lr_types.hpp"
|
||||
|
||||
namespace sts::lr::impl {
|
||||
|
||||
enum RedirectionFlags {
|
||||
RedirectionFlags_None = (0 << 0),
|
||||
RedirectionFlags_Application = (1 << 0),
|
||||
};
|
||||
|
||||
class LocationRedirection : public util::IntrusiveListBaseNode<LocationRedirection> {
|
||||
NON_COPYABLE(LocationRedirection);
|
||||
NON_MOVEABLE(LocationRedirection);
|
||||
|
||||
public:
|
||||
ncm::TitleId title_id;
|
||||
Path path;
|
||||
u32 flags;
|
||||
|
||||
LocationRedirection(ncm::TitleId title_id, const Path& path, u32 flags) :
|
||||
title_id(title_id), path(path), flags(flags) {
|
||||
}
|
||||
};
|
||||
|
||||
class LocationRedirector {
|
||||
NON_COPYABLE(LocationRedirector);
|
||||
NON_MOVEABLE(LocationRedirector);
|
||||
private:
|
||||
sts::util::IntrusiveListBaseTraits<LocationRedirection>::ListType redirection_list;
|
||||
public:
|
||||
LocationRedirector() {}
|
||||
|
||||
bool FindRedirection(Path *out, ncm::TitleId title_id);
|
||||
void SetRedirection(ncm::TitleId title_id, const Path &path, u32 flags = RedirectionFlags_None);
|
||||
void SetRedirectionFlags(ncm::TitleId title_id, u32 flags);
|
||||
void EraseRedirection(ncm::TitleId title_id);
|
||||
void ClearRedirections(u32 flags = RedirectionFlags_None);
|
||||
};
|
||||
|
||||
class RegisteredLocationRedirector {
|
||||
private:
|
||||
BoundedMap<ncm::TitleId, Path, 16> redirections;
|
||||
public:
|
||||
bool FindRedirection(Path *out, ncm::TitleId title_id);
|
||||
bool SetRedirection(ncm::TitleId title_id, const Path& path);
|
||||
void EraseRedirection(ncm::TitleId title_id);
|
||||
void ClearRedirections();
|
||||
};
|
||||
|
||||
class AddOnContentRedirector {
|
||||
private:
|
||||
BoundedMap<ncm::TitleId, ncm::StorageId, 128> redirections;
|
||||
public:
|
||||
bool FindRedirection(ncm::StorageId *out, ncm::TitleId title_id);
|
||||
Result SetRedirection(ncm::TitleId title_id, ncm::StorageId storage_id);
|
||||
void ClearRedirections();
|
||||
};
|
||||
|
||||
}
|
||||
695
stratosphere/ncm/source/impl/ncm_content_manager.cpp
Normal file
695
stratosphere/ncm/source/impl/ncm_content_manager.cpp
Normal file
@@ -0,0 +1,695 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Adubbz
|
||||
*
|
||||
* 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 <switch.h>
|
||||
#include <stratosphere.hpp>
|
||||
#include <stratosphere/kvdb/kvdb_memory_key_value_store.hpp>
|
||||
#include <optional>
|
||||
|
||||
#include "../ncm_contentmetadatabase.hpp"
|
||||
#include "../ncm_contentstorage.hpp"
|
||||
#include "../ncm_fs.hpp"
|
||||
#include "../ncm_make_path.hpp"
|
||||
#include "../ncm_readonlycontentstorage.hpp"
|
||||
#include "ncm_content_manager.hpp"
|
||||
|
||||
namespace sts::ncm::impl {
|
||||
|
||||
namespace {
|
||||
|
||||
struct ContentStorageEntry {
|
||||
NON_COPYABLE(ContentStorageEntry);
|
||||
NON_MOVEABLE(ContentStorageEntry);
|
||||
|
||||
char mount_point[16];
|
||||
char root_path[128];
|
||||
StorageId storage_id;
|
||||
FsContentStorageId content_storage_id;
|
||||
std::shared_ptr<IContentStorage> content_storage;
|
||||
|
||||
inline ContentStorageEntry() : storage_id(StorageId::None),
|
||||
content_storage_id(FS_CONTENTSTORAGEID_NandSystem), content_storage(nullptr) {
|
||||
mount_point[0] = '\0';
|
||||
root_path[0] = '\0';
|
||||
}
|
||||
|
||||
inline void Initialize(StorageId storage_id, FsContentStorageId content_storage_id) {
|
||||
this->storage_id = storage_id;
|
||||
this->content_storage_id = content_storage_id;
|
||||
this->content_storage = nullptr;
|
||||
MountName mount_name = CreateUniqueMountName();
|
||||
strcpy(this->mount_point, mount_name.name);
|
||||
snprintf(this->root_path, 0x80, "%s:/", this->mount_point);
|
||||
}
|
||||
};
|
||||
|
||||
struct SaveDataMeta {
|
||||
u64 id;
|
||||
u64 size;
|
||||
u64 journal_size;
|
||||
u32 flags;
|
||||
FsSaveDataSpaceId space_id;
|
||||
} PACKED;
|
||||
|
||||
static_assert(sizeof(SaveDataMeta) == 0x20, "SaveDataMeta definition!");
|
||||
|
||||
struct ContentMetaDBEntry {
|
||||
NON_COPYABLE(ContentMetaDBEntry);
|
||||
NON_MOVEABLE(ContentMetaDBEntry);
|
||||
|
||||
char mount_point[16];
|
||||
char meta_path[128];
|
||||
StorageId storage_id;
|
||||
SaveDataMeta save_meta;
|
||||
std::shared_ptr<IContentMetaDatabase> content_meta_database;
|
||||
std::optional<kvdb::MemoryKeyValueStore<ContentMetaKey>> kvs;
|
||||
u32 max_content_metas;
|
||||
|
||||
inline ContentMetaDBEntry() : storage_id(StorageId::None), save_meta({0}),
|
||||
content_meta_database(nullptr), kvs(std::nullopt), max_content_metas(0) {
|
||||
mount_point[0] = '\0';
|
||||
meta_path[0] = '\0';
|
||||
}
|
||||
|
||||
Result Initialize(StorageId storage_id, SaveDataMeta& save_meta, size_t max_content_metas) {
|
||||
this->storage_id = storage_id;
|
||||
this->max_content_metas = max_content_metas;
|
||||
this->save_meta = save_meta;
|
||||
this->content_meta_database = nullptr;
|
||||
this->kvs.reset();
|
||||
MountName mount_name = CreateUniqueMountName();
|
||||
strcpy(this->mount_point, mount_name.name);
|
||||
this->mount_point[0] = '#';
|
||||
snprintf(this->meta_path, 0x80, "%s:/meta", this->mount_point);
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result InitializeGameCard(size_t max_content_metas) {
|
||||
this->storage_id = StorageId::GameCard;
|
||||
this->max_content_metas = max_content_metas;
|
||||
this->content_meta_database = nullptr;
|
||||
this->kvs.reset();
|
||||
return ResultSuccess;
|
||||
}
|
||||
};
|
||||
|
||||
constexpr size_t MaxContentStorageEntries = 8;
|
||||
constexpr size_t MaxContentMetaDBEntries = 8;
|
||||
|
||||
HosMutex g_mutex;
|
||||
bool g_initialized = false;
|
||||
ContentStorageEntry g_content_storage_entries[MaxContentStorageEntries];
|
||||
ContentMetaDBEntry g_content_meta_entries[MaxContentMetaDBEntries];
|
||||
u32 g_num_content_storage_entries;
|
||||
u32 g_num_content_meta_entries;
|
||||
|
||||
ContentStorageEntry* FindContentStorageEntry(StorageId storage_id) {
|
||||
for (size_t i = 0; i < MaxContentStorageEntries; i++) {
|
||||
ContentStorageEntry* entry = &g_content_storage_entries[i];
|
||||
|
||||
if (entry->storage_id == storage_id) {
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ContentMetaDBEntry* FindContentMetaDBEntry(StorageId storage_id) {
|
||||
for (size_t i = 0; i < MaxContentMetaDBEntries; i++) {
|
||||
ContentMetaDBEntry* entry = &g_content_meta_entries[i];
|
||||
|
||||
if (entry->storage_id == storage_id) {
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Result InitializeContentManager() {
|
||||
std::scoped_lock<HosMutex> lk(g_mutex);
|
||||
|
||||
/* Already initialized. */
|
||||
if (g_initialized) {
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
size_t cur_storage_index = g_num_content_storage_entries;
|
||||
|
||||
for (size_t i = 0; i < MaxContentStorageEntries; i++) {
|
||||
ContentStorageEntry* entry = &g_content_storage_entries[i];
|
||||
entry->storage_id = StorageId::None;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < MaxContentMetaDBEntries; i++) {
|
||||
ContentMetaDBEntry* entry = &g_content_meta_entries[i];
|
||||
entry->storage_id = StorageId::None;
|
||||
}
|
||||
|
||||
g_num_content_storage_entries++;
|
||||
auto storage_entry = &g_content_storage_entries[cur_storage_index];
|
||||
|
||||
/* First, setup the NandSystem storage entry. */
|
||||
storage_entry->Initialize(StorageId::NandSystem, FS_CONTENTSTORAGEID_NandSystem);
|
||||
|
||||
if (R_FAILED(VerifyContentStorage(StorageId::NandSystem))) {
|
||||
R_TRY(CreateContentStorage(StorageId::NandSystem));
|
||||
}
|
||||
|
||||
R_TRY(ActivateContentStorage(StorageId::NandSystem));
|
||||
|
||||
/* Next, the NandSystem content meta entry. */
|
||||
SaveDataMeta nand_system_save_meta;
|
||||
nand_system_save_meta.id = 0x8000000000000120;
|
||||
nand_system_save_meta.size = 0x6c000;
|
||||
nand_system_save_meta.journal_size = 0x6c000;
|
||||
nand_system_save_meta.flags = FsSaveDataFlags_SurviveFactoryReset| FsSaveDataFlags_SurviveFactoryResetForRefurbishment;
|
||||
nand_system_save_meta.space_id = FsSaveDataSpaceId_NandSystem;
|
||||
|
||||
size_t cur_meta_index = g_num_content_meta_entries;
|
||||
g_num_content_meta_entries++;
|
||||
auto content_meta_entry = &g_content_meta_entries[cur_meta_index];
|
||||
|
||||
R_TRY(content_meta_entry->Initialize(StorageId::NandSystem, nand_system_save_meta, 0x800));
|
||||
|
||||
if (R_FAILED(VerifyContentMetaDatabase(StorageId::NandSystem))) {
|
||||
R_TRY(CreateContentMetaDatabase(StorageId::NandSystem));
|
||||
|
||||
/* TODO: N supports a number of unused modes here, we don't bother implementing them currently. */
|
||||
}
|
||||
|
||||
u32 current_flags = 0;
|
||||
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_200 && R_SUCCEEDED(GetSaveDataFlags(¤t_flags, 0x8000000000000120)) && current_flags != (FsSaveDataFlags_SurviveFactoryReset | FsSaveDataFlags_SurviveFactoryResetForRefurbishment)) {
|
||||
SetSaveDataFlags(0x8000000000000120, FsSaveDataSpaceId_NandSystem, FsSaveDataFlags_SurviveFactoryReset | FsSaveDataFlags_SurviveFactoryResetForRefurbishment);
|
||||
}
|
||||
|
||||
R_TRY(ActivateContentMetaDatabase(StorageId::NandSystem));
|
||||
|
||||
/* Now for NandUser's content storage entry. */
|
||||
cur_meta_index = g_num_content_meta_entries;
|
||||
g_num_content_meta_entries++;
|
||||
storage_entry = &g_content_storage_entries[cur_storage_index];
|
||||
storage_entry->Initialize(StorageId::NandUser, FS_CONTENTSTORAGEID_NandUser);
|
||||
|
||||
/* And NandUser's content meta entry. */
|
||||
SaveDataMeta nand_user_save_meta;
|
||||
nand_user_save_meta.id = 0x8000000000000121;
|
||||
nand_user_save_meta.size = 0x29e000;
|
||||
nand_user_save_meta.journal_size = 0x29e000;
|
||||
nand_user_save_meta.flags = 0;
|
||||
nand_user_save_meta.space_id = FsSaveDataSpaceId_NandSystem;
|
||||
|
||||
cur_meta_index = g_num_content_meta_entries;
|
||||
g_num_content_meta_entries++;
|
||||
content_meta_entry = &g_content_meta_entries[cur_meta_index];
|
||||
|
||||
R_TRY(content_meta_entry->Initialize(StorageId::NandUser, nand_user_save_meta, 0x2000));
|
||||
|
||||
/*
|
||||
Beyond this point N no longer appears to bother
|
||||
incrementing the count for content storage entries or content meta entries.
|
||||
*/
|
||||
|
||||
/* Next SdCard's content storage entry. */
|
||||
g_content_storage_entries[2].Initialize(StorageId::SdCard, FS_CONTENTSTORAGEID_SdCard);
|
||||
|
||||
/* And SdCard's content meta entry. */
|
||||
SaveDataMeta sd_card_save_meta;
|
||||
sd_card_save_meta.id = 0x8000000000000124;
|
||||
sd_card_save_meta.size = 0xa08000;
|
||||
sd_card_save_meta.journal_size = 0xa08000;
|
||||
sd_card_save_meta.flags = 0;
|
||||
sd_card_save_meta.space_id = FsSaveDataSpaceId_SdCard;
|
||||
|
||||
content_meta_entry = &g_content_meta_entries[2];
|
||||
R_TRY(content_meta_entry->Initialize(StorageId::SdCard, sd_card_save_meta, 0x2000));
|
||||
|
||||
/* GameCard's content storage entry. */
|
||||
/* N doesn't set a content storage id for game cards, so we'll just use 0 (NandSystem). */
|
||||
g_content_storage_entries[3].Initialize(StorageId::GameCard, FS_CONTENTSTORAGEID_NandSystem);
|
||||
|
||||
/* Lasty, GameCard's content meta entry. */
|
||||
content_meta_entry = &g_content_meta_entries[3];
|
||||
R_TRY(content_meta_entry->InitializeGameCard(0x800));
|
||||
|
||||
g_initialized = true;
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
void FinalizeContentManager() {
|
||||
{
|
||||
std::scoped_lock<HosMutex> lk(g_mutex);
|
||||
|
||||
for (size_t i = 0; i < MaxContentStorageEntries; i++) {
|
||||
ContentStorageEntry* entry = &g_content_storage_entries[i];
|
||||
InactivateContentStorage(entry->storage_id);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < MaxContentMetaDBEntries; i++) {
|
||||
ContentMetaDBEntry* entry = &g_content_meta_entries[i];
|
||||
InactivateContentMetaDatabase(entry->storage_id);
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < MaxContentMetaDBEntries; i++) {
|
||||
ContentMetaDBEntry* entry = &g_content_meta_entries[i];
|
||||
entry->kvs.reset();
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < MaxContentStorageEntries; i++) {
|
||||
ContentStorageEntry* entry = &g_content_storage_entries[i];
|
||||
entry->content_storage = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
Result CreateContentStorage(StorageId storage_id) {
|
||||
std::scoped_lock<HosMutex> lk(g_mutex);
|
||||
|
||||
if (storage_id == StorageId::None || static_cast<u8>(storage_id) == 6) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
ContentStorageEntry* entry = FindContentStorageEntry(storage_id);
|
||||
|
||||
if (!entry) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
R_TRY(MountContentStorage(entry->mount_point, entry->content_storage_id));
|
||||
|
||||
ON_SCOPE_EXIT {
|
||||
Unmount(entry->mount_point);
|
||||
};
|
||||
|
||||
R_TRY(EnsureDirectoryRecursively(entry->root_path));
|
||||
R_TRY(EnsureContentAndPlaceHolderRoot(entry->root_path));
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result VerifyContentStorage(StorageId storage_id) {
|
||||
std::scoped_lock<HosMutex> lk(g_mutex);
|
||||
|
||||
if (storage_id == StorageId::None || static_cast<u8>(storage_id) == 6) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
ContentStorageEntry* entry = FindContentStorageEntry(storage_id);
|
||||
|
||||
if (!entry) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
MountName mount_name = CreateUniqueMountName();
|
||||
char mount_root[128] = {0};
|
||||
strcpy(mount_root, mount_name.name);
|
||||
strcat(mount_root, strchr(entry->root_path, ':'));
|
||||
R_TRY(MountContentStorage(mount_name.name, entry->content_storage_id));
|
||||
|
||||
ON_SCOPE_EXIT {
|
||||
Unmount(mount_name.name);
|
||||
};
|
||||
|
||||
R_TRY(CheckContentStorageDirectoriesExist(mount_root));
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result OpenContentStorage(std::shared_ptr<IContentStorage>* out, StorageId storage_id) {
|
||||
std::scoped_lock<HosMutex> lk(g_mutex);
|
||||
|
||||
if (storage_id == StorageId::None || static_cast<u8>(storage_id) == 6) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
ContentStorageEntry* entry = FindContentStorageEntry(storage_id);
|
||||
|
||||
if (!entry) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
auto content_storage = entry->content_storage;
|
||||
|
||||
if (!content_storage) {
|
||||
switch (storage_id) {
|
||||
case StorageId::GameCard:
|
||||
return ResultNcmGameCardContentStorageNotActive;
|
||||
|
||||
case StorageId::NandSystem:
|
||||
return ResultNcmNandSystemContentStorageNotActive;
|
||||
|
||||
case StorageId::NandUser:
|
||||
return ResultNcmNandUserContentStorageNotActive;
|
||||
|
||||
case StorageId::SdCard:
|
||||
return ResultNcmSdCardContentStorageNotActive;
|
||||
|
||||
default:
|
||||
return ResultNcmUnknownContentStorageNotActive;
|
||||
}
|
||||
}
|
||||
|
||||
*out = std::move(content_storage);
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result CloseContentStorageForcibly(StorageId storage_id) {
|
||||
std::scoped_lock<HosMutex> lk(g_mutex);
|
||||
|
||||
if (storage_id == StorageId::None) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
ContentStorageEntry* entry = FindContentStorageEntry(storage_id);
|
||||
|
||||
if (!entry) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
if (!entry->content_storage) {
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
/* N doesn't bother checking the result of this */
|
||||
entry->content_storage->DisableForcibly();
|
||||
Unmount(entry->mount_point);
|
||||
entry->content_storage = nullptr;
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result ActivateContentStorage(StorageId storage_id) {
|
||||
std::scoped_lock<HosMutex> lk(g_mutex);
|
||||
|
||||
if (storage_id == StorageId::None || static_cast<u8>(storage_id) == 6) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
ContentStorageEntry* entry = FindContentStorageEntry(storage_id);
|
||||
|
||||
if (!entry) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
/* Already activated. */
|
||||
if (entry->content_storage != nullptr) {
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
if (storage_id == StorageId::GameCard) {
|
||||
FsGameCardHandle gc_hnd;
|
||||
R_TRY(GetGameCardHandle(&gc_hnd));
|
||||
R_TRY(MountGameCardPartition(entry->mount_point, gc_hnd, FsGameCardPartiton_Secure));
|
||||
auto mount_guard = SCOPE_GUARD { Unmount(entry->mount_point); };
|
||||
auto content_storage = std::make_shared<ReadOnlyContentStorageInterface>();
|
||||
|
||||
R_TRY(content_storage->Initialize(entry->root_path, path::MakeContentPathFlat));
|
||||
entry->content_storage = std::move(content_storage);
|
||||
mount_guard.Cancel();
|
||||
} else {
|
||||
R_TRY(MountContentStorage(entry->mount_point, entry->content_storage_id));
|
||||
auto mount_guard = SCOPE_GUARD { Unmount(entry->mount_point); };
|
||||
MakeContentPathFunc content_path_func = nullptr;
|
||||
MakePlaceHolderPathFunc placeholder_path_func = nullptr;
|
||||
bool delay_flush = false;
|
||||
auto content_storage = std::make_shared<ContentStorageInterface>();
|
||||
|
||||
switch (storage_id) {
|
||||
case StorageId::NandSystem:
|
||||
content_path_func = path::MakeContentPathFlat;
|
||||
placeholder_path_func = path::MakePlaceHolderPathFlat;
|
||||
break;
|
||||
|
||||
case StorageId::SdCard:
|
||||
delay_flush = true;
|
||||
default:
|
||||
content_path_func = path::MakeContentPathHashByteLayered;
|
||||
placeholder_path_func = path::MakePlaceHolderPathHashByteLayered;
|
||||
break;
|
||||
}
|
||||
|
||||
R_TRY(content_storage->Initialize(entry->root_path, content_path_func, placeholder_path_func, delay_flush));
|
||||
entry->content_storage = std::move(content_storage);
|
||||
mount_guard.Cancel();
|
||||
}
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result InactivateContentStorage(StorageId storage_id) {
|
||||
std::scoped_lock<HosMutex> lk(g_mutex);
|
||||
|
||||
if (storage_id == StorageId::None || static_cast<u8>(storage_id) == 6) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
ContentStorageEntry* entry = FindContentStorageEntry(storage_id);
|
||||
|
||||
if (!entry) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
/* Already inactivated. */
|
||||
if (entry->content_storage == nullptr) {
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
entry->content_storage->DisableForcibly();
|
||||
entry->content_storage = nullptr;
|
||||
Unmount(entry->mount_point);
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result CreateContentMetaDatabase(StorageId storage_id) {
|
||||
std::scoped_lock<HosMutex> lk(g_mutex);
|
||||
|
||||
if (storage_id == StorageId::None || storage_id == StorageId::GameCard || static_cast<u8>(storage_id) == 6) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
ContentMetaDBEntry* entry = FindContentMetaDBEntry(storage_id);
|
||||
|
||||
if (!entry) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
/* N doesn't bother checking the result of this. */
|
||||
fsDisableAutoSaveDataCreation();
|
||||
|
||||
R_TRY_CATCH(MountSystemSaveData(entry->mount_point, entry->save_meta.space_id, entry->save_meta.id)) {
|
||||
R_CATCH(ResultFsTargetNotFound) {
|
||||
R_TRY(fsCreate_SystemSaveData(entry->save_meta.space_id, entry->save_meta.id, entry->save_meta.size, entry->save_meta.journal_size, entry->save_meta.flags));
|
||||
R_TRY(MountSystemSaveData(entry->mount_point, entry->save_meta.space_id, entry->save_meta.id));
|
||||
}
|
||||
} R_END_TRY_CATCH;
|
||||
|
||||
ON_SCOPE_EXIT {
|
||||
Unmount(entry->mount_point);
|
||||
};
|
||||
|
||||
R_TRY(EnsureDirectoryRecursively(entry->mount_point));
|
||||
R_TRY(fsdevCommitDevice(entry->mount_point));
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result VerifyContentMetaDatabase(StorageId storage_id) {
|
||||
std::scoped_lock<HosMutex> lk(g_mutex);
|
||||
|
||||
if (storage_id == StorageId::GameCard) {
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
if (storage_id == StorageId::None || static_cast<u8>(storage_id) == 6) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
ContentMetaDBEntry* entry = FindContentMetaDBEntry(storage_id);
|
||||
|
||||
if (!entry) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
bool mounted_save_data = false;
|
||||
|
||||
if (!entry->content_meta_database) {
|
||||
R_TRY(MountSystemSaveData(entry->mount_point, entry->save_meta.space_id, entry->save_meta.id));
|
||||
mounted_save_data = true;
|
||||
}
|
||||
|
||||
bool has_meta_path = false;
|
||||
R_TRY(HasDirectory(&has_meta_path, entry->meta_path));
|
||||
if (!has_meta_path) {
|
||||
return ResultNcmInvalidContentMetaDatabase;
|
||||
}
|
||||
|
||||
if (mounted_save_data) {
|
||||
Unmount(entry->mount_point);
|
||||
}
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result OpenContentMetaDatabase(std::shared_ptr<IContentMetaDatabase>* out, StorageId storage_id) {
|
||||
std::scoped_lock<HosMutex> lk(g_mutex);
|
||||
|
||||
if (storage_id == StorageId::None || static_cast<u8>(storage_id) == 6) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
ContentMetaDBEntry* entry = FindContentMetaDBEntry(storage_id);
|
||||
|
||||
if (!entry) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
std::shared_ptr<IContentMetaDatabase> content_meta_db = entry->content_meta_database;
|
||||
|
||||
if (!content_meta_db) {
|
||||
switch (storage_id) {
|
||||
case StorageId::GameCard:
|
||||
return ResultNcmGameCardContentMetaDatabaseNotActive;
|
||||
|
||||
case StorageId::NandSystem:
|
||||
return ResultNcmNandSystemContentMetaDatabaseNotActive;
|
||||
|
||||
case StorageId::NandUser:
|
||||
return ResultNcmNandUserContentMetaDatabaseNotActive;
|
||||
|
||||
case StorageId::SdCard:
|
||||
return ResultNcmSdCardContentMetaDatabaseNotActive;
|
||||
|
||||
default:
|
||||
return ResultNcmUnknownContentMetaDatabaseNotActive;
|
||||
}
|
||||
}
|
||||
|
||||
*out = std::move(content_meta_db);
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result CloseContentMetaDatabaseForcibly(StorageId storage_id) {
|
||||
std::scoped_lock<HosMutex> lk(g_mutex);
|
||||
|
||||
if (storage_id == StorageId::None) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
ContentMetaDBEntry* entry = FindContentMetaDBEntry(storage_id);
|
||||
|
||||
if (!entry) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
std::shared_ptr<IContentMetaDatabase> content_meta_db = entry->content_meta_database;
|
||||
|
||||
if (!content_meta_db) {
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
/* N doesn't bother checking the result of this */
|
||||
content_meta_db->DisableForcibly();
|
||||
|
||||
if (storage_id != StorageId::GameCard) {
|
||||
Unmount(entry->mount_point);
|
||||
}
|
||||
|
||||
entry->content_meta_database = nullptr;
|
||||
entry->kvs.reset();
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result CleanupContentMetaDatabase(StorageId storage_id) {
|
||||
std::scoped_lock<HosMutex> lk(g_mutex);
|
||||
|
||||
if (storage_id == StorageId::None || static_cast<u8>(storage_id) == 6) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
ContentMetaDBEntry* entry = FindContentMetaDBEntry(storage_id);
|
||||
|
||||
if (!entry) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
R_TRY(fsDeleteSaveDataFileSystemBySaveDataSpaceId(entry->save_meta.space_id, entry->save_meta.id));
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result ActivateContentMetaDatabase(StorageId storage_id) {
|
||||
std::scoped_lock<HosMutex> lk(g_mutex);
|
||||
|
||||
if (storage_id == StorageId::None || static_cast<u8>(storage_id) == 6) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
ContentMetaDBEntry* entry = FindContentMetaDBEntry(storage_id);
|
||||
|
||||
/* Already activated. */
|
||||
if (entry->content_meta_database != nullptr) {
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
/* Make a brand new kvs. N doesn't quite do this, but we will for cleanliness. */
|
||||
entry->kvs.emplace();
|
||||
|
||||
if (storage_id != StorageId::GameCard) {
|
||||
R_TRY(MountSystemSaveData(entry->mount_point, entry->save_meta.space_id, entry->save_meta.id));
|
||||
auto mount_guard = SCOPE_GUARD { Unmount(entry->mount_point); };
|
||||
R_TRY(entry->kvs->Initialize(entry->meta_path, entry->max_content_metas));
|
||||
R_TRY(entry->kvs->Load());
|
||||
|
||||
auto content_meta_database = std::make_shared<ContentMetaDatabaseInterface>(&*entry->kvs, entry->mount_point);
|
||||
entry->content_meta_database = std::move(content_meta_database);
|
||||
mount_guard.Cancel();
|
||||
} else {
|
||||
R_TRY(entry->kvs->Initialize(entry->max_content_metas));
|
||||
R_TRY(entry->kvs->Load());
|
||||
auto content_meta_database = std::make_shared<OnMemoryContentMetaDatabaseInterface>(&*entry->kvs);
|
||||
entry->content_meta_database = std::move(content_meta_database);
|
||||
}
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result InactivateContentMetaDatabase(StorageId storage_id) {
|
||||
std::scoped_lock<HosMutex> lk(g_mutex);
|
||||
|
||||
if (storage_id == StorageId::None || static_cast<u8>(storage_id) == 6) {
|
||||
return ResultNcmUnknownStorage;
|
||||
}
|
||||
|
||||
ContentMetaDBEntry* entry = FindContentMetaDBEntry(storage_id);
|
||||
|
||||
/* Already inactivated. */
|
||||
if (entry->content_meta_database == nullptr) {
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
entry->content_meta_database->DisableForcibly();
|
||||
entry->content_meta_database = nullptr;
|
||||
/* This should lead to Index's destructor performing cleanup for us. */
|
||||
entry->kvs.reset();
|
||||
|
||||
if (storage_id != StorageId::GameCard) {
|
||||
Unmount(entry->mount_point);
|
||||
}
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
}
|
||||
47
stratosphere/ncm/source/impl/ncm_content_manager.hpp
Normal file
47
stratosphere/ncm/source/impl/ncm_content_manager.hpp
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Adubbz
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <switch.h>
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
#include "../ncm_icontentmetadatabase.hpp"
|
||||
#include "../ncm_icontentstorage.hpp"
|
||||
|
||||
namespace sts::ncm::impl {
|
||||
|
||||
/* Initialization/Finalization. */
|
||||
Result InitializeContentManager();
|
||||
void FinalizeContentManager();
|
||||
|
||||
/* Content Storage Management. */
|
||||
Result CreateContentStorage(StorageId storage_id);
|
||||
Result VerifyContentStorage(StorageId storage_id);
|
||||
Result OpenContentStorage(std::shared_ptr<IContentStorage>* out, StorageId storage_id);
|
||||
Result CloseContentStorageForcibly(StorageId storage_id);
|
||||
Result ActivateContentStorage(StorageId storage_id);
|
||||
Result InactivateContentStorage(StorageId storage_id);
|
||||
|
||||
/* Content Meta Database Management. */
|
||||
Result CreateContentMetaDatabase(StorageId storage_id);
|
||||
Result VerifyContentMetaDatabase(StorageId storage_id);
|
||||
Result OpenContentMetaDatabase(std::shared_ptr<IContentMetaDatabase>* out, StorageId storage_id);
|
||||
Result CloseContentMetaDatabaseForcibly(StorageId storage_id);
|
||||
Result CleanupContentMetaDatabase(StorageId storage_id);
|
||||
Result ActivateContentMetaDatabase(StorageId storage_id);
|
||||
Result InactivateContentMetaDatabase(StorageId storage_id);
|
||||
|
||||
}
|
||||
233
stratosphere/ncm/source/impl/ncm_placeholder_accessor.cpp
Normal file
233
stratosphere/ncm/source/impl/ncm_placeholder_accessor.cpp
Normal file
@@ -0,0 +1,233 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Adubbz
|
||||
*
|
||||
* 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 "ncm_placeholder_accessor.hpp"
|
||||
#include "../ncm_fs.hpp"
|
||||
#include "../ncm_utils.hpp"
|
||||
#include "../ncm_make_path.hpp"
|
||||
#include "../ncm_path_utils.hpp"
|
||||
|
||||
namespace sts::ncm::impl {
|
||||
|
||||
unsigned int PlaceHolderAccessor::GetDirectoryDepth() {
|
||||
if (this->make_placeholder_path_func == static_cast<MakePlaceHolderPathFunc>(path::MakePlaceHolderPathFlat)) {
|
||||
return 1;
|
||||
} else if (this->make_placeholder_path_func == static_cast<MakePlaceHolderPathFunc>(path::MakePlaceHolderPathHashByteLayered)) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
std::abort();
|
||||
}
|
||||
|
||||
void PlaceHolderAccessor::GetPlaceHolderPathUncached(char* placeholder_path_out, PlaceHolderId placeholder_id) {
|
||||
std::scoped_lock<HosMutex> lock(this->cache_mutex);
|
||||
|
||||
if (placeholder_id != InvalidUuid) {
|
||||
CacheEntry* found_cache = NULL;
|
||||
|
||||
for (size_t i = 0; i < PlaceHolderAccessor::MaxCaches; i++) {
|
||||
CacheEntry* cache = &this->caches[i];
|
||||
|
||||
if (placeholder_id == cache->id) {
|
||||
found_cache = cache;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found_cache) {
|
||||
/* Flush and close */
|
||||
fsync(fileno(found_cache->handle));
|
||||
fclose(found_cache->handle);
|
||||
std::fill(found_cache->id.uuid, found_cache->id.uuid + sizeof(PlaceHolderId), 0);
|
||||
}
|
||||
}
|
||||
|
||||
this->GetPlaceHolderPath(placeholder_path_out, placeholder_id);
|
||||
}
|
||||
|
||||
Result PlaceHolderAccessor::Create(PlaceHolderId placeholder_id, size_t size) {
|
||||
char placeholder_path[FS_MAX_PATH] = {0};
|
||||
|
||||
this->EnsureRecursively(placeholder_id);
|
||||
this->GetPlaceHolderPathUncached(placeholder_path, placeholder_id);
|
||||
|
||||
R_TRY_CATCH(fsdevCreateFile(placeholder_path, size, FS_CREATE_BIG_FILE)) {
|
||||
R_CATCH(ResultFsPathAlreadyExists) {
|
||||
return ResultNcmPlaceHolderAlreadyExists;
|
||||
}
|
||||
} R_END_TRY_CATCH;
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result PlaceHolderAccessor::Delete(PlaceHolderId placeholder_id) {
|
||||
char placeholder_path[FS_MAX_PATH] = {0};
|
||||
|
||||
this->GetPlaceHolderPathUncached(placeholder_path, placeholder_id);
|
||||
|
||||
R_TRY_CATCH(fsdevDeleteDirectoryRecursively(placeholder_path)) {
|
||||
R_CATCH(ResultFsPathNotFound) {
|
||||
return ResultNcmPlaceHolderNotFound;
|
||||
}
|
||||
} R_END_TRY_CATCH;
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result PlaceHolderAccessor::Open(FILE** out_handle, PlaceHolderId placeholder_id) {
|
||||
if (this->LoadFromCache(out_handle, placeholder_id)) {
|
||||
return ResultSuccess;
|
||||
}
|
||||
char placeholder_path[FS_MAX_PATH] = {0};
|
||||
|
||||
this->GetPlaceHolderPath(placeholder_path, placeholder_id);
|
||||
errno = 0;
|
||||
*out_handle = fopen(placeholder_path, "w+b");
|
||||
|
||||
if (errno != 0) {
|
||||
return fsdevGetLastResult();
|
||||
}
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result PlaceHolderAccessor::SetSize(PlaceHolderId placeholder_id, size_t size) {
|
||||
char placeholder_path[FS_MAX_PATH] = {0};
|
||||
errno = 0;
|
||||
this->GetPlaceHolderPath(placeholder_path, placeholder_id);
|
||||
truncate(placeholder_path, size);
|
||||
|
||||
if (errno != 0) {
|
||||
R_TRY_CATCH(fsdevGetLastResult()) {
|
||||
R_CATCH(ResultFsPathNotFound) {
|
||||
return ResultNcmPlaceHolderNotFound;
|
||||
}
|
||||
} R_END_TRY_CATCH;
|
||||
}
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result PlaceHolderAccessor::GetSize(bool* found_in_cache, size_t* out_size, PlaceHolderId placeholder_id) {
|
||||
FILE* f = NULL;
|
||||
|
||||
/* Set the scope for the scoped_lock. */
|
||||
{
|
||||
std::scoped_lock<HosMutex> lock(this->cache_mutex);
|
||||
|
||||
if (placeholder_id == InvalidUuid) {
|
||||
*found_in_cache = false;
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
CacheEntry* cache_entry = this->FindInCache(placeholder_id);
|
||||
|
||||
if (cache_entry == nullptr) {
|
||||
*found_in_cache = false;
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
cache_entry->id = InvalidUuid;
|
||||
f = cache_entry->handle;
|
||||
}
|
||||
|
||||
this->FlushCache(f, placeholder_id);
|
||||
|
||||
errno = 0;
|
||||
fseek(f, 0L, SEEK_END);
|
||||
size_t size = ftell(f);
|
||||
fseek(f, 0L, SEEK_SET);
|
||||
|
||||
if (errno != 0) {
|
||||
return fsdevGetLastResult();
|
||||
}
|
||||
|
||||
*found_in_cache = true;
|
||||
*out_size = size;
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result PlaceHolderAccessor::EnsureRecursively(PlaceHolderId placeholder_id) {
|
||||
char placeholder_path[FS_MAX_PATH] = {0};
|
||||
this->GetPlaceHolderPath(placeholder_path, placeholder_id);
|
||||
R_TRY(EnsureParentDirectoryRecursively(placeholder_path));
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
bool PlaceHolderAccessor::LoadFromCache(FILE** out_handle, PlaceHolderId placeholder_id) {
|
||||
std::scoped_lock<HosMutex> lk(this->cache_mutex);
|
||||
CacheEntry *entry = this->FindInCache(placeholder_id);
|
||||
if (entry == nullptr) {
|
||||
return false;
|
||||
}
|
||||
entry->id = InvalidUuid;
|
||||
*out_handle = entry->handle;
|
||||
return true;
|
||||
}
|
||||
|
||||
PlaceHolderAccessor::CacheEntry *PlaceHolderAccessor::FindInCache(PlaceHolderId placeholder_id) {
|
||||
if (placeholder_id == InvalidUuid) {
|
||||
return nullptr;
|
||||
}
|
||||
for (size_t i = 0; i < MaxCaches; i++) {
|
||||
if (placeholder_id == this->caches[i].id) {
|
||||
return &this->caches[i];
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void PlaceHolderAccessor::FlushCache(FILE* handle, PlaceHolderId placeholder_id) {
|
||||
std::scoped_lock<HosMutex> lk(this->cache_mutex);
|
||||
CacheEntry* cache = nullptr;
|
||||
|
||||
/* Find an empty cache */
|
||||
for (size_t i = 0; i < MaxCaches; i++) {
|
||||
if (placeholder_id != InvalidUuid) {
|
||||
cache = &this->caches[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* No empty caches found. Let's clear cache 0. */
|
||||
if (cache == nullptr) {
|
||||
cache = &this->caches[0];
|
||||
|
||||
/* Flush and close */
|
||||
fsync(fileno(cache->handle));
|
||||
fclose(cache->handle);
|
||||
cache->id = InvalidUuid;
|
||||
}
|
||||
|
||||
cache->id = placeholder_id;
|
||||
cache->handle = handle;
|
||||
cache->counter = this->cur_counter;
|
||||
this->cur_counter++;
|
||||
}
|
||||
|
||||
void PlaceHolderAccessor::ClearAllCaches() {
|
||||
for (size_t i = 0; i < MaxCaches; i++) {
|
||||
CacheEntry* cache = &this->caches[i];
|
||||
|
||||
if (cache->id != InvalidUuid) {
|
||||
fsync(fileno(cache->handle));
|
||||
fclose(cache->handle);
|
||||
cache->id = InvalidUuid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
76
stratosphere/ncm/source/impl/ncm_placeholder_accessor.hpp
Normal file
76
stratosphere/ncm/source/impl/ncm_placeholder_accessor.hpp
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Adubbz
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <switch.h>
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
#include "../ncm_types.hpp"
|
||||
#include "../ncm_path_utils.hpp"
|
||||
|
||||
namespace sts::ncm::impl {
|
||||
|
||||
class PlaceHolderAccessor {
|
||||
public:
|
||||
class CacheEntry {
|
||||
public:
|
||||
PlaceHolderId id;
|
||||
FILE* handle;
|
||||
u64 counter;
|
||||
};
|
||||
|
||||
public:
|
||||
static constexpr size_t MaxCaches = 0x2;
|
||||
|
||||
CacheEntry caches[MaxCaches];
|
||||
char* root_path;
|
||||
u64 cur_counter;
|
||||
HosMutex cache_mutex;
|
||||
MakePlaceHolderPathFunc make_placeholder_path_func;
|
||||
bool delay_flush;
|
||||
|
||||
PlaceHolderAccessor() : cur_counter(0), delay_flush(false) {
|
||||
for (size_t i = 0; i < MaxCaches; i++) {
|
||||
caches[i].id = InvalidUuid;
|
||||
}
|
||||
}
|
||||
|
||||
inline void GetPlaceHolderRootPath(char* out_placeholder_root) {
|
||||
path::GetPlaceHolderRootPath(out_placeholder_root, this->root_path);
|
||||
}
|
||||
|
||||
inline void GetPlaceHolderPath(char* out_placeholder_path, PlaceHolderId placeholder_id) {
|
||||
char placeholder_root_path[FS_MAX_PATH] = {0};
|
||||
this->GetPlaceHolderRootPath(placeholder_root_path);
|
||||
this->make_placeholder_path_func(out_placeholder_path, placeholder_id, placeholder_root_path);
|
||||
}
|
||||
|
||||
unsigned int GetDirectoryDepth();
|
||||
void GetPlaceHolderPathUncached(char* out_placeholder_path, PlaceHolderId placeholder_id);
|
||||
Result Create(PlaceHolderId placeholder_id, size_t size);
|
||||
Result Delete(PlaceHolderId placeholder_id);
|
||||
Result Open(FILE** out_handle, PlaceHolderId placeholder_id);
|
||||
Result SetSize(PlaceHolderId placeholder_id, size_t size);
|
||||
Result GetSize(bool* found_in_cache, size_t* out_size, PlaceHolderId placeholder_id);
|
||||
Result EnsureRecursively(PlaceHolderId placeholder_id);
|
||||
|
||||
CacheEntry *FindInCache(PlaceHolderId placeholder_id);
|
||||
bool LoadFromCache(FILE** out_handle, PlaceHolderId placeholder_id);
|
||||
void FlushCache(FILE* handle, PlaceHolderId placeholder_id);
|
||||
void ClearAllCaches();
|
||||
};
|
||||
|
||||
}
|
||||
31
stratosphere/ncm/source/impl/ncm_rights_cache.cpp
Normal file
31
stratosphere/ncm/source/impl/ncm_rights_cache.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Adubbz
|
||||
*
|
||||
* 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 "ncm_rights_cache.hpp"
|
||||
|
||||
namespace sts::ncm::impl {
|
||||
|
||||
namespace {
|
||||
|
||||
RightsIdCache g_rights_id_cache;
|
||||
|
||||
}
|
||||
|
||||
RightsIdCache* GetRightsIdCache() {
|
||||
return &g_rights_id_cache;
|
||||
}
|
||||
|
||||
}
|
||||
44
stratosphere/ncm/source/impl/ncm_rights_cache.hpp
Normal file
44
stratosphere/ncm/source/impl/ncm_rights_cache.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Adubbz
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <switch.h>
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
#include "../ncm_types.hpp"
|
||||
|
||||
namespace sts::ncm::impl {
|
||||
|
||||
class RightsIdCache {
|
||||
public:
|
||||
static constexpr size_t MaxEntries = 0x80;
|
||||
public:
|
||||
struct Entry {
|
||||
public:
|
||||
Uuid uuid;
|
||||
FsRightsId rights_id;
|
||||
u64 key_generation;
|
||||
u64 last_accessed = 1;
|
||||
};
|
||||
|
||||
Entry entries[MaxEntries];
|
||||
u64 counter = 2;
|
||||
HosMutex mutex;
|
||||
};
|
||||
|
||||
RightsIdCache* GetRightsIdCache();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user