2018-09-07 15:00:13 +00:00
|
|
|
/*
|
2019-04-08 02:00:49 +00:00
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
2018-09-07 15:00:13 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2019-06-20 11:04:33 +00:00
|
|
|
|
2018-06-14 23:50:01 +00:00
|
|
|
#include <switch.h>
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
|
|
|
|
#include "fsmitm_layeredrom.hpp"
|
2019-01-29 10:29:29 +00:00
|
|
|
#include "../utils.hpp"
|
|
|
|
#include "../debug.hpp"
|
2018-06-14 23:50:01 +00:00
|
|
|
|
2018-07-02 16:15:30 +00:00
|
|
|
IStorage::~IStorage() = default;
|
2018-06-14 23:50:01 +00:00
|
|
|
|
2019-06-03 19:15:39 +00:00
|
|
|
LayeredRomFS::LayeredRomFS(std::shared_ptr<IROStorage> s_r, std::shared_ptr<IROStorage> f_r, u64 tid) : storage_romfs(s_r), file_romfs(f_r), title_id(tid) {
|
2018-06-14 23:50:01 +00:00
|
|
|
/* Start building the new virtual romfs. */
|
|
|
|
RomFSBuildContext build_ctx(this->title_id);
|
2018-06-28 05:02:06 +00:00
|
|
|
this->p_source_infos = std::shared_ptr<std::vector<RomFSSourceInfo>>(new std::vector<RomFSSourceInfo>(), [](std::vector<RomFSSourceInfo> *to_delete) {
|
|
|
|
for (unsigned int i = 0; i < to_delete->size(); i++) {
|
|
|
|
(*to_delete)[i].Cleanup();
|
|
|
|
}
|
|
|
|
delete to_delete;
|
|
|
|
});
|
2018-06-14 23:50:01 +00:00
|
|
|
if (Utils::IsSdInitialized()) {
|
|
|
|
build_ctx.MergeSdFiles();
|
|
|
|
}
|
|
|
|
if (this->file_romfs) {
|
2018-06-19 18:07:31 +00:00
|
|
|
build_ctx.MergeRomStorage(this->file_romfs.get(), RomFSDataSource::FileRomFS);
|
2018-06-14 23:50:01 +00:00
|
|
|
}
|
|
|
|
if (this->storage_romfs) {
|
2018-06-19 18:07:31 +00:00
|
|
|
build_ctx.MergeRomStorage(this->storage_romfs.get(), RomFSDataSource::BaseRomFS);
|
2018-06-14 23:50:01 +00:00
|
|
|
}
|
|
|
|
build_ctx.Build(this->p_source_infos.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Result LayeredRomFS::Read(void *buffer, size_t size, u64 offset) {
|
2018-11-05 05:52:26 +00:00
|
|
|
/* Size zero reads should always succeed. */
|
|
|
|
if (size == 0) {
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2018-11-05 05:52:26 +00:00
|
|
|
}
|
2019-06-20 11:04:33 +00:00
|
|
|
|
2018-06-14 23:50:01 +00:00
|
|
|
/* Validate size. */
|
|
|
|
u64 virt_size = (*this->p_source_infos)[this->p_source_infos->size() - 1].virtual_offset + (*this->p_source_infos)[this->p_source_infos->size() - 1].size;
|
|
|
|
if (offset >= virt_size) {
|
2019-03-22 15:49:10 +00:00
|
|
|
return ResultFsInvalidOffset;
|
2018-06-14 23:50:01 +00:00
|
|
|
}
|
|
|
|
if (virt_size - offset < size) {
|
|
|
|
size = virt_size - offset;
|
|
|
|
}
|
|
|
|
/* Find first source info via binary search. */
|
|
|
|
u32 cur_source_ind = 0;
|
|
|
|
u32 low = 0, high = this->p_source_infos->size() - 1;
|
|
|
|
while (low <= high) {
|
|
|
|
u32 mid = (low + high) / 2;
|
|
|
|
if ((*this->p_source_infos)[mid].virtual_offset > offset) {
|
|
|
|
/* Too high. */
|
|
|
|
high = mid - 1;
|
|
|
|
} else {
|
|
|
|
/* sources[mid].virtual_offset <= offset, invariant */
|
|
|
|
if (mid == this->p_source_infos->size() - 1 || (*this->p_source_infos)[mid + 1].virtual_offset > offset) {
|
|
|
|
/* Success */
|
|
|
|
cur_source_ind = mid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
low = mid + 1;
|
|
|
|
}
|
|
|
|
}
|
2019-06-20 11:04:33 +00:00
|
|
|
|
2018-06-14 23:50:01 +00:00
|
|
|
size_t read_so_far = 0;
|
|
|
|
while (read_so_far < size) {
|
|
|
|
RomFSSourceInfo *cur_source = &((*this->p_source_infos)[cur_source_ind]);
|
|
|
|
if (cur_source->virtual_offset + cur_source->size > offset) {
|
|
|
|
u64 cur_read_size = size - read_so_far;
|
|
|
|
if (cur_read_size > cur_source->size - (offset - cur_source->virtual_offset)) {
|
|
|
|
cur_read_size = cur_source->size - (offset - cur_source->virtual_offset);
|
|
|
|
}
|
2018-06-28 05:02:06 +00:00
|
|
|
switch (cur_source->type) {
|
2018-11-04 21:56:07 +00:00
|
|
|
case RomFSDataSource::MetaData:
|
|
|
|
{
|
|
|
|
FsFile file;
|
2019-06-20 11:04:33 +00:00
|
|
|
R_ASSERT(Utils::OpenSdFileForAtmosphere(this->title_id, ROMFS_METADATA_FILE_PATH, FS_OPEN_READ, &file));
|
2018-11-04 21:56:07 +00:00
|
|
|
size_t out_read;
|
2019-06-20 11:04:33 +00:00
|
|
|
R_ASSERT(fsFileRead(&file, (offset - cur_source->virtual_offset), (void *)((uintptr_t)buffer + read_so_far), cur_read_size, FS_READOPTION_NONE, &out_read));
|
2019-09-28 01:04:58 +00:00
|
|
|
STS_ASSERT(out_read == cur_read_size);
|
2018-11-04 21:56:07 +00:00
|
|
|
fsFileClose(&file);
|
|
|
|
}
|
|
|
|
break;
|
2018-06-28 05:02:06 +00:00
|
|
|
case RomFSDataSource::LooseFile:
|
|
|
|
{
|
|
|
|
FsFile file;
|
2019-06-20 11:04:33 +00:00
|
|
|
R_ASSERT(Utils::OpenRomFSSdFile(this->title_id, cur_source->loose_source_info.path, FS_OPEN_READ, &file));
|
2018-06-28 05:02:06 +00:00
|
|
|
size_t out_read;
|
2019-06-20 11:04:33 +00:00
|
|
|
R_ASSERT(fsFileRead(&file, (offset - cur_source->virtual_offset), (void *)((uintptr_t)buffer + read_so_far), cur_read_size, FS_READOPTION_NONE, &out_read));
|
2019-09-28 01:04:58 +00:00
|
|
|
STS_ASSERT(out_read == cur_read_size);
|
2018-06-28 05:02:06 +00:00
|
|
|
fsFileClose(&file);
|
2018-06-14 23:50:01 +00:00
|
|
|
}
|
2018-06-28 05:02:06 +00:00
|
|
|
break;
|
|
|
|
case RomFSDataSource::Memory:
|
|
|
|
{
|
|
|
|
memcpy((void *)((uintptr_t)buffer + read_so_far), cur_source->memory_source_info.data + (offset - cur_source->virtual_offset), cur_read_size);
|
2018-06-14 23:50:01 +00:00
|
|
|
}
|
2018-06-28 05:02:06 +00:00
|
|
|
break;
|
|
|
|
case RomFSDataSource::BaseRomFS:
|
|
|
|
{
|
2019-06-20 11:04:33 +00:00
|
|
|
R_ASSERT(this->storage_romfs->Read((void *)((uintptr_t)buffer + read_so_far), cur_read_size, cur_source->base_source_info.offset + (offset - cur_source->virtual_offset)));
|
2018-06-14 23:50:01 +00:00
|
|
|
}
|
2018-06-28 05:02:06 +00:00
|
|
|
break;
|
|
|
|
case RomFSDataSource::FileRomFS:
|
|
|
|
{
|
2019-06-20 11:04:33 +00:00
|
|
|
R_ASSERT(this->file_romfs->Read((void *)((uintptr_t)buffer + read_so_far), cur_read_size, cur_source->base_source_info.offset + (offset - cur_source->virtual_offset)));
|
2018-06-19 18:07:31 +00:00
|
|
|
}
|
2018-06-28 05:02:06 +00:00
|
|
|
break;
|
2019-09-28 22:13:20 +00:00
|
|
|
STS_UNREACHABLE_DEFAULT_CASE();
|
2018-06-28 05:02:06 +00:00
|
|
|
}
|
2018-06-14 23:50:01 +00:00
|
|
|
read_so_far += cur_read_size;
|
2018-10-19 03:15:18 +00:00
|
|
|
offset += cur_read_size;
|
2018-06-14 23:50:01 +00:00
|
|
|
} else {
|
|
|
|
/* Handle padding explicitly. */
|
|
|
|
cur_source_ind++;
|
|
|
|
/* Zero out the padding we skip, here. */
|
2018-10-19 03:15:18 +00:00
|
|
|
memset((void *)((uintptr_t)buffer + read_so_far), 0, ((*this->p_source_infos)[cur_source_ind]).virtual_offset - offset);
|
|
|
|
read_so_far += ((*this->p_source_infos)[cur_source_ind]).virtual_offset - offset;
|
|
|
|
offset = ((*this->p_source_infos)[cur_source_ind]).virtual_offset;
|
2018-06-14 23:50:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-20 11:04:33 +00:00
|
|
|
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2018-06-14 23:50:01 +00:00
|
|
|
}
|
|
|
|
Result LayeredRomFS::GetSize(u64 *out_size) {
|
|
|
|
*out_size = (*this->p_source_infos)[this->p_source_infos->size() - 1].virtual_offset + (*this->p_source_infos)[this->p_source_infos->size() - 1].size;
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2018-06-14 23:50:01 +00:00
|
|
|
}
|
2019-06-03 19:15:39 +00:00
|
|
|
Result LayeredRomFS::OperateRange(FsOperationId operation_type, u64 offset, u64 size, FsRangeInfo *out_range_info) {
|
2018-06-14 23:50:01 +00:00
|
|
|
/* TODO: How should I implement this for a virtual romfs? */
|
2019-06-03 19:15:39 +00:00
|
|
|
if (operation_type == FsOperationId_QueryRange) {
|
2018-06-14 23:50:01 +00:00
|
|
|
*out_range_info = {0};
|
|
|
|
}
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2018-06-19 18:07:31 +00:00
|
|
|
}
|