2017-10-01 18:32:43 +02:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <cinttypes>
|
|
|
|
#include <cryptopp/sha.h>
|
|
|
|
#include "common/alignment.h"
|
|
|
|
#include "common/file_util.h"
|
|
|
|
#include "common/logging/log.h"
|
2018-09-04 18:28:38 +02:00
|
|
|
#include "core/file_sys/cia_common.h"
|
2017-10-01 18:32:43 +02:00
|
|
|
#include "core/file_sys/title_metadata.h"
|
|
|
|
#include "core/loader/loader.h"
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// FileSys namespace
|
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
2017-10-22 00:52:48 +02:00
|
|
|
Loader::ResultStatus TitleMetadata::Load(const std::string& file_path) {
|
|
|
|
FileUtil::IOFile file(file_path, "rb");
|
2017-10-01 18:32:43 +02:00
|
|
|
if (!file.IsOpen())
|
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
|
2017-10-22 00:52:48 +02:00
|
|
|
std::vector<u8> file_data(file.GetSize());
|
|
|
|
|
|
|
|
if (!file.ReadBytes(file_data.data(), file.GetSize()))
|
2017-10-01 18:32:43 +02:00
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
|
2017-10-22 00:52:48 +02:00
|
|
|
Loader::ResultStatus result = Load(file_data);
|
|
|
|
if (result != Loader::ResultStatus::Success)
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_ERROR(Service_FS, "Failed to load TMD from file {}!", file_path);
|
2017-10-01 18:32:43 +02:00
|
|
|
|
2017-10-22 00:52:48 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-05-01 15:53:42 +02:00
|
|
|
Loader::ResultStatus TitleMetadata::Load(const std::vector<u8>& file_data, std::size_t offset) {
|
2018-09-06 22:03:28 +02:00
|
|
|
std::size_t total_size = static_cast<std::size_t>(file_data.size() - offset);
|
2017-10-22 00:52:48 +02:00
|
|
|
if (total_size < sizeof(u32_be))
|
2017-10-01 18:32:43 +02:00
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
|
2017-10-22 00:52:48 +02:00
|
|
|
memcpy(&signature_type, &file_data[offset], sizeof(u32_be));
|
|
|
|
|
|
|
|
// Signature lengths are variable, and the body follows the signature
|
|
|
|
u32 signature_size = GetSignatureSize(signature_type);
|
2018-10-15 17:26:35 +02:00
|
|
|
if (signature_size == 0) {
|
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
}
|
2017-10-22 00:52:48 +02:00
|
|
|
|
2017-10-01 18:32:43 +02:00
|
|
|
// The TMD body start position is rounded to the nearest 0x40 after the signature
|
2018-09-06 22:03:28 +02:00
|
|
|
std::size_t body_start = Common::AlignUp(signature_size + sizeof(u32), 0x40);
|
|
|
|
std::size_t body_end = body_start + sizeof(Body);
|
2017-10-01 18:32:43 +02:00
|
|
|
|
2017-10-22 00:52:48 +02:00
|
|
|
if (total_size < body_end)
|
2017-10-01 18:32:43 +02:00
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
|
2017-10-22 00:52:48 +02:00
|
|
|
// Read signature + TMD body, then load the amount of ContentChunks specified
|
|
|
|
tmd_signature.resize(signature_size);
|
|
|
|
memcpy(tmd_signature.data(), &file_data[offset + sizeof(u32_be)], signature_size);
|
|
|
|
memcpy(&tmd_body, &file_data[offset + body_start], sizeof(TitleMetadata::Body));
|
|
|
|
|
2018-09-06 22:03:28 +02:00
|
|
|
std::size_t expected_size =
|
2018-07-23 23:08:14 +02:00
|
|
|
body_start + sizeof(Body) + static_cast<u16>(tmd_body.content_count) * sizeof(ContentChunk);
|
2017-10-22 00:52:48 +02:00
|
|
|
if (total_size < expected_size) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_ERROR(Service_FS, "Malformed TMD, expected size 0x{:x}, got 0x{:x}!", expected_size,
|
2018-06-29 15:56:12 +02:00
|
|
|
total_size);
|
2017-10-22 00:52:48 +02:00
|
|
|
return Loader::ResultStatus::ErrorInvalidFormat;
|
|
|
|
}
|
|
|
|
|
2017-10-01 18:32:43 +02:00
|
|
|
for (u16 i = 0; i < tmd_body.content_count; i++) {
|
|
|
|
ContentChunk chunk;
|
2017-10-22 00:52:48 +02:00
|
|
|
|
|
|
|
memcpy(&chunk, &file_data[offset + body_end + (i * sizeof(ContentChunk))],
|
|
|
|
sizeof(ContentChunk));
|
|
|
|
tmd_chunks.push_back(chunk);
|
2017-10-01 18:32:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
2017-10-22 00:52:48 +02:00
|
|
|
Loader::ResultStatus TitleMetadata::Save(const std::string& file_path) {
|
|
|
|
FileUtil::IOFile file(file_path, "wb");
|
2017-10-01 18:32:43 +02:00
|
|
|
if (!file.IsOpen())
|
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
|
|
|
|
if (!file.WriteBytes(&signature_type, sizeof(u32_be)))
|
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
|
|
|
|
// Signature lengths are variable, and the body follows the signature
|
|
|
|
u32 signature_size = GetSignatureSize(signature_type);
|
2018-10-15 17:26:35 +02:00
|
|
|
if (signature_size == 0) {
|
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
}
|
2017-10-01 18:32:43 +02:00
|
|
|
|
|
|
|
if (!file.WriteBytes(tmd_signature.data(), signature_size))
|
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
|
|
|
|
// The TMD body start position is rounded to the nearest 0x40 after the signature
|
2018-09-06 22:03:28 +02:00
|
|
|
std::size_t body_start = Common::AlignUp(signature_size + sizeof(u32), 0x40);
|
2017-10-01 18:32:43 +02:00
|
|
|
file.Seek(body_start, SEEK_SET);
|
|
|
|
|
|
|
|
// Update our TMD body values and hashes
|
|
|
|
tmd_body.content_count = static_cast<u16>(tmd_chunks.size());
|
|
|
|
|
|
|
|
// TODO(shinyquagsire23): Do TMDs with more than one contentinfo exist?
|
|
|
|
// For now we'll just adjust the first index to hold all content chunks
|
|
|
|
// and ensure that no further content info data exists.
|
|
|
|
tmd_body.contentinfo = {};
|
|
|
|
tmd_body.contentinfo[0].index = 0;
|
|
|
|
tmd_body.contentinfo[0].command_count = static_cast<u16>(tmd_chunks.size());
|
|
|
|
|
|
|
|
CryptoPP::SHA256 chunk_hash;
|
|
|
|
for (u16 i = 0; i < tmd_body.content_count; i++) {
|
|
|
|
chunk_hash.Update(reinterpret_cast<u8*>(&tmd_chunks[i]), sizeof(ContentChunk));
|
|
|
|
}
|
|
|
|
chunk_hash.Final(tmd_body.contentinfo[0].hash.data());
|
|
|
|
|
|
|
|
CryptoPP::SHA256 contentinfo_hash;
|
2018-09-06 22:03:28 +02:00
|
|
|
for (std::size_t i = 0; i < tmd_body.contentinfo.size(); i++) {
|
2017-10-01 18:32:43 +02:00
|
|
|
chunk_hash.Update(reinterpret_cast<u8*>(&tmd_body.contentinfo[i]), sizeof(ContentInfo));
|
|
|
|
}
|
|
|
|
chunk_hash.Final(tmd_body.contentinfo_hash.data());
|
|
|
|
|
|
|
|
// Write our TMD body, then write each of our ContentChunks
|
|
|
|
if (file.WriteBytes(&tmd_body, sizeof(TitleMetadata::Body)) != sizeof(TitleMetadata::Body))
|
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
|
|
|
|
for (u16 i = 0; i < tmd_body.content_count; i++) {
|
|
|
|
ContentChunk chunk = tmd_chunks[i];
|
|
|
|
if (file.WriteBytes(&chunk, sizeof(ContentChunk)) != sizeof(ContentChunk))
|
|
|
|
return Loader::ResultStatus::Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Loader::ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 TitleMetadata::GetTitleID() const {
|
|
|
|
return tmd_body.title_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 TitleMetadata::GetTitleType() const {
|
|
|
|
return tmd_body.title_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 TitleMetadata::GetTitleVersion() const {
|
|
|
|
return tmd_body.title_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 TitleMetadata::GetSystemVersion() const {
|
|
|
|
return tmd_body.system_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t TitleMetadata::GetContentCount() const {
|
|
|
|
return tmd_chunks.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 TitleMetadata::GetBootContentID() const {
|
|
|
|
return tmd_chunks[TMDContentIndex::Main].id;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 TitleMetadata::GetManualContentID() const {
|
|
|
|
return tmd_chunks[TMDContentIndex::Manual].id;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 TitleMetadata::GetDLPContentID() const {
|
|
|
|
return tmd_chunks[TMDContentIndex::DLP].id;
|
|
|
|
}
|
|
|
|
|
2020-06-20 20:09:56 +02:00
|
|
|
u32 TitleMetadata::GetContentIDByIndex(std::size_t index) const {
|
2017-10-06 00:12:35 +02:00
|
|
|
return tmd_chunks[index].id;
|
|
|
|
}
|
|
|
|
|
2020-06-20 20:09:56 +02:00
|
|
|
u16 TitleMetadata::GetContentTypeByIndex(std::size_t index) const {
|
2017-10-06 00:12:35 +02:00
|
|
|
return tmd_chunks[index].type;
|
|
|
|
}
|
|
|
|
|
2020-06-20 20:09:56 +02:00
|
|
|
u64 TitleMetadata::GetContentSizeByIndex(std::size_t index) const {
|
2017-10-06 00:12:35 +02:00
|
|
|
return tmd_chunks[index].size;
|
|
|
|
}
|
|
|
|
|
2020-06-20 20:09:56 +02:00
|
|
|
std::array<u8, 16> TitleMetadata::GetContentCTRByIndex(std::size_t index) const {
|
2018-09-04 18:28:38 +02:00
|
|
|
std::array<u8, 16> ctr{};
|
|
|
|
std::memcpy(ctr.data(), &tmd_chunks[index].index, sizeof(u16));
|
|
|
|
return ctr;
|
|
|
|
}
|
|
|
|
|
2017-10-01 18:32:43 +02:00
|
|
|
void TitleMetadata::SetTitleID(u64 title_id) {
|
|
|
|
tmd_body.title_id = title_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TitleMetadata::SetTitleType(u32 type) {
|
|
|
|
tmd_body.title_type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TitleMetadata::SetTitleVersion(u16 version) {
|
|
|
|
tmd_body.title_version = version;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TitleMetadata::SetSystemVersion(u64 version) {
|
|
|
|
tmd_body.system_version = version;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TitleMetadata::AddContentChunk(const ContentChunk& chunk) {
|
|
|
|
tmd_chunks.push_back(chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TitleMetadata::Print() const {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_DEBUG(Service_FS, "{} chunks", static_cast<u32>(tmd_body.content_count));
|
2017-10-01 18:32:43 +02:00
|
|
|
|
|
|
|
// Content info describes ranges of content chunks
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_DEBUG(Service_FS, "Content info:");
|
2018-09-06 22:03:28 +02:00
|
|
|
for (std::size_t i = 0; i < tmd_body.contentinfo.size(); i++) {
|
2017-10-01 18:32:43 +02:00
|
|
|
if (tmd_body.contentinfo[i].command_count == 0)
|
|
|
|
break;
|
|
|
|
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_DEBUG(Service_FS, " Index {:04X}, Command Count {:04X}",
|
2018-06-29 15:56:12 +02:00
|
|
|
static_cast<u32>(tmd_body.contentinfo[i].index),
|
|
|
|
static_cast<u32>(tmd_body.contentinfo[i].command_count));
|
2017-10-01 18:32:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// For each content info, print their content chunk range
|
2018-09-06 22:03:28 +02:00
|
|
|
for (std::size_t i = 0; i < tmd_body.contentinfo.size(); i++) {
|
2017-10-01 18:32:43 +02:00
|
|
|
u16 index = static_cast<u16>(tmd_body.contentinfo[i].index);
|
|
|
|
u16 count = static_cast<u16>(tmd_body.contentinfo[i].command_count);
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
continue;
|
|
|
|
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_DEBUG(Service_FS, "Content chunks for content info index {}:", i);
|
2017-10-01 18:32:43 +02:00
|
|
|
for (u16 j = index; j < index + count; j++) {
|
|
|
|
// Don't attempt to print content we don't have
|
|
|
|
if (j > tmd_body.content_count)
|
|
|
|
break;
|
|
|
|
|
|
|
|
const ContentChunk& chunk = tmd_chunks[j];
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_DEBUG(Service_FS, " ID {:08X}, Index {:04X}, Type {:04x}, Size {:016X}",
|
2018-06-29 15:56:12 +02:00
|
|
|
static_cast<u32>(chunk.id), static_cast<u32>(chunk.index),
|
|
|
|
static_cast<u32>(chunk.type), static_cast<u64>(chunk.size));
|
2017-10-01 18:32:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace FileSys
|