2015-01-01 17:39:27 +00:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2015-06-21 14:44:11 +00:00
|
|
|
#include <algorithm>
|
2016-04-05 12:29:55 +00:00
|
|
|
#include <memory>
|
2015-06-21 14:44:11 +00:00
|
|
|
#include <vector>
|
|
|
|
#include "common/common_types.h"
|
2015-01-01 17:39:27 +00:00
|
|
|
#include "common/file_util.h"
|
2015-05-06 07:06:12 +00:00
|
|
|
#include "common/logging/log.h"
|
2015-06-20 21:24:01 +00:00
|
|
|
#include "common/string_util.h"
|
2017-05-23 23:46:30 +00:00
|
|
|
#include "core/core.h"
|
2016-10-21 14:10:55 +00:00
|
|
|
#include "core/file_sys/archive_ncch.h"
|
2017-05-24 23:51:31 +00:00
|
|
|
#include "core/file_sys/errors.h"
|
2015-06-21 14:44:11 +00:00
|
|
|
#include "core/file_sys/ivfc_archive.h"
|
2017-09-26 04:21:39 +00:00
|
|
|
#include "core/file_sys/ncch_container.h"
|
2017-10-01 16:41:40 +00:00
|
|
|
#include "core/file_sys/title_metadata.h"
|
2015-01-04 01:46:05 +00:00
|
|
|
#include "core/hle/service/fs/archive.h"
|
2017-09-26 04:21:39 +00:00
|
|
|
#include "core/loader/loader.h"
|
2015-01-01 17:39:27 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// FileSys namespace
|
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
2016-10-21 14:10:55 +00:00
|
|
|
static std::string GetNCCHContainerPath(const std::string& nand_directory) {
|
2016-12-16 05:37:38 +00:00
|
|
|
return Common::StringFromFormat("%s%s/title/", nand_directory.c_str(), SYSTEM_ID);
|
2015-01-04 01:46:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 14:10:55 +00:00
|
|
|
static std::string GetNCCHPath(const std::string& mount_point, u32 high, u32 low) {
|
2017-10-01 16:41:40 +00:00
|
|
|
u32 content_id = 0;
|
|
|
|
|
|
|
|
// TODO(shinyquagsire23): Title database should be doing this path lookup
|
|
|
|
std::string content_path =
|
|
|
|
Common::StringFromFormat("%s%08x/%08x/content/", mount_point.c_str(), high, low);
|
|
|
|
std::string tmd_path = content_path + "00000000.tmd";
|
|
|
|
TitleMetadata tmd(tmd_path);
|
|
|
|
if (tmd.Load() == Loader::ResultStatus::Success) {
|
|
|
|
content_id = tmd.GetBootContentID();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Common::StringFromFormat("%s%08x.app", content_path.c_str(), content_id);
|
2015-01-04 01:46:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 14:10:55 +00:00
|
|
|
ArchiveFactory_NCCH::ArchiveFactory_NCCH(const std::string& nand_directory)
|
|
|
|
: mount_point(GetNCCHContainerPath(nand_directory)) {}
|
2015-01-01 17:39:27 +00:00
|
|
|
|
2016-10-21 14:10:55 +00:00
|
|
|
ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_NCCH::Open(const Path& path) {
|
2015-01-01 17:39:27 +00:00
|
|
|
auto vec = path.AsBinary();
|
|
|
|
const u32* data = reinterpret_cast<u32*>(vec.data());
|
2017-05-23 23:46:30 +00:00
|
|
|
u32 high = data[1];
|
|
|
|
u32 low = data[0];
|
|
|
|
std::string file_path = GetNCCHPath(mount_point, high, low);
|
2015-01-01 17:39:27 +00:00
|
|
|
|
2017-09-26 04:21:39 +00:00
|
|
|
std::shared_ptr<FileUtil::IOFile> romfs_file;
|
|
|
|
u64 romfs_offset = 0;
|
|
|
|
u64 romfs_size = 0;
|
|
|
|
auto ncch_container = NCCHContainer(file_path);
|
|
|
|
|
|
|
|
if (ncch_container.ReadRomFS(romfs_file, romfs_offset, romfs_size) !=
|
|
|
|
Loader::ResultStatus::Success) {
|
2017-05-23 23:46:30 +00:00
|
|
|
// High Title ID of the archive: The category (https://3dbrew.org/wiki/Title_list).
|
2017-06-02 21:03:38 +00:00
|
|
|
constexpr u32 shared_data_archive = 0x0004009B;
|
|
|
|
constexpr u32 system_data_archive = 0x000400DB;
|
2017-05-23 23:46:30 +00:00
|
|
|
|
|
|
|
// Low Title IDs.
|
2017-06-02 21:03:38 +00:00
|
|
|
constexpr u32 mii_data = 0x00010202;
|
|
|
|
constexpr u32 region_manifest = 0x00010402;
|
|
|
|
constexpr u32 ng_word_list = 0x00010302;
|
2017-05-23 23:46:30 +00:00
|
|
|
|
|
|
|
LOG_DEBUG(Service_FS, "Full Path: %s. Category: 0x%X. Path: 0x%X.", path.DebugStr().c_str(),
|
|
|
|
high, low);
|
|
|
|
|
|
|
|
if (high == shared_data_archive) {
|
|
|
|
if (low == mii_data) {
|
|
|
|
LOG_ERROR(Service_FS, "Failed to get a handle for shared data archive: Mii data. ");
|
|
|
|
Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSystemFiles,
|
|
|
|
"Mii data");
|
|
|
|
} else if (low == region_manifest) {
|
|
|
|
LOG_ERROR(Service_FS,
|
2017-06-02 21:03:38 +00:00
|
|
|
"Failed to get a handle for shared data archive: region manifest.");
|
2017-05-23 23:46:30 +00:00
|
|
|
Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSystemFiles,
|
|
|
|
"Region manifest");
|
|
|
|
}
|
|
|
|
} else if (high == system_data_archive) {
|
|
|
|
if (low == ng_word_list) {
|
|
|
|
LOG_ERROR(Service_FS,
|
|
|
|
"Failed to get a handle for system data archive: NG bad word list.");
|
|
|
|
Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSystemFiles,
|
|
|
|
"NG bad word list");
|
|
|
|
}
|
|
|
|
}
|
2017-05-24 23:51:31 +00:00
|
|
|
return ERROR_NOT_FOUND;
|
2015-01-01 17:39:27 +00:00
|
|
|
}
|
2015-02-06 13:53:14 +00:00
|
|
|
|
2017-09-26 04:21:39 +00:00
|
|
|
auto archive = std::make_unique<IVFCArchive>(romfs_file, romfs_offset, romfs_size);
|
2015-02-06 13:53:14 +00:00
|
|
|
return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
|
|
|
|
}
|
|
|
|
|
2016-10-21 14:10:55 +00:00
|
|
|
ResultCode ArchiveFactory_NCCH::Format(const Path& path,
|
|
|
|
const FileSys::ArchiveFormatInfo& format_info) {
|
|
|
|
LOG_ERROR(Service_FS, "Attempted to format a NCCH archive.");
|
2015-02-06 13:53:14 +00:00
|
|
|
// TODO: Verify error code
|
2016-09-18 00:38:01 +00:00
|
|
|
return ResultCode(ErrorDescription::NotAuthorized, ErrorModule::FS, ErrorSummary::NotSupported,
|
|
|
|
ErrorLevel::Permanent);
|
2015-01-01 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 14:10:55 +00:00
|
|
|
ResultVal<ArchiveFormatInfo> ArchiveFactory_NCCH::GetFormatInfo(const Path& path) const {
|
2015-12-28 18:51:44 +00:00
|
|
|
// TODO(Subv): Implement
|
|
|
|
LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive %s", GetName().c_str());
|
|
|
|
return ResultCode(-1);
|
|
|
|
}
|
|
|
|
|
2015-01-01 17:39:27 +00:00
|
|
|
} // namespace FileSys
|