Port CnmtProcess to libtoolchain.

This commit is contained in:
jakcron 2021-09-30 19:41:57 +08:00
parent f86716b95b
commit 0c6efad603
2 changed files with 83 additions and 82 deletions

View file

@ -1,11 +1,9 @@
#include "CnmtProcess.h" #include "CnmtProcess.h"
#include <iostream>
#include <iomanip>
#include <nn/hac/ContentMetaUtil.h> #include <nn/hac/ContentMetaUtil.h>
nstool::CnmtProcess::CnmtProcess() : nstool::CnmtProcess::CnmtProcess() :
mModuleName("nstool::CnmtProcess"),
mFile(), mFile(),
mCliOutputMode(true, false, false, false), mCliOutputMode(true, false, false, false),
mVerify(false) mVerify(false)
@ -42,96 +40,102 @@ const nn::hac::ContentMeta& nstool::CnmtProcess::getContentMeta() const
void nstool::CnmtProcess::importCnmt() void nstool::CnmtProcess::importCnmt()
{ {
tc::ByteData scratch; if (mFile == nullptr)
if (*mFile == nullptr)
{ {
throw tc::Exception(kModuleName, "No file reader set."); throw tc::Exception(mModuleName, "No file reader set.");
} }
scratch.alloc((*mFile)->size());
(*mFile)->read(scratch.data(), 0, scratch.size());
// check if file_size is greater than 20MB, don't import.
size_t cnmt_file_size = tc::io::IOUtil::castInt64ToSize(mFile->length());
if (cnmt_file_size > (0x100000 * 20))
{
throw tc::Exception(mModuleName, "File too large.");
}
// read cnmt
tc::ByteData scratch = tc::ByteData(cnmt_file_size);
mFile->seek(0, tc::io::SeekOrigin::Begin);
mFile->read(scratch.data(), scratch.size());
// parse cnmt
mCnmt.fromBytes(scratch.data(), scratch.size()); mCnmt.fromBytes(scratch.data(), scratch.size());
} }
void nstool::CnmtProcess::displayCnmt() void nstool::CnmtProcess::displayCnmt()
{ {
std::cout << "[ContentMeta]" << std::endl; const nn::hac::sContentMetaHeader* cnmt_hdr = (const nn::hac::sContentMetaHeader*)mCnmt.getBytes().data();
std::cout << " TitleId: 0x" << std::hex << std::setw(16) << std::setfill('0') << mCnmt.getTitleId() << std::endl; fmt::print("[ContentMeta]\n");
std::cout << " Version: " << nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getTitleVersion()) << " (v" << std::dec << mCnmt.getTitleVersion() << ")"<< std::endl; fmt::print(" TitleId: 0x{:016x}\n", mCnmt.getTitleId());
std::cout << " Type: " << nn::hac::ContentMetaUtil::getContentMetaTypeAsString(mCnmt.getContentMetaType()) << " (" << std::dec << (uint32_t)mCnmt.getContentMetaType() << ")" << std::endl; fmt::print(" Version: {:s} (v{:d})\n", nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getTitleVersion()), mCnmt.getTitleVersion());
std::cout << " Attributes: 0x" << std::hex << mCnmt.getAttribute().to_ullong(); fmt::print(" Type: {:s} (v{:d})\n", nn::hac::ContentMetaUtil::getContentMetaTypeAsString(mCnmt.getContentMetaType()), (uint32_t)mCnmt.getContentMetaType());
if (mCnmt.getAttribute().any()) fmt::print(" Attributes: 0x{:x}", *((byte_t*)&cnmt_hdr->attributes));
if (mCnmt.getAttribute().size())
{ {
std::vector<std::string> attribute_list; std::vector<std::string> attribute_list;
for (size_t flag = 0; flag < mCnmt.getAttribute().size(); flag++) for (auto itr = mCnmt.getAttribute().begin(); itr != mCnmt.getAttribute().end(); itr++)
{ {
attribute_list.push_back(nn::hac::ContentMetaUtil::getContentMetaAttributeFlagAsString(nn::hac::cnmt::ContentMetaAttributeFlag(*itr)));
if (mCnmt.getAttribute().test(flag))
{
attribute_list.push_back(nn::hac::ContentMetaUtil::getContentMetaAttributeFlagAsString(nn::hac::cnmt::ContentMetaAttributeFlag(flag)));
}
} }
std::cout << " ["; fmt::print(" [");
for (auto itr = attribute_list.begin(); itr != attribute_list.end(); itr++) for (auto itr = attribute_list.begin(); itr != attribute_list.end(); itr++)
{ {
std::cout << *itr; fmt::print("{:s}",*itr);
if ((itr + 1) != attribute_list.end()) if ((itr + 1) != attribute_list.end())
{ {
std::cout << ", "; fmt::print(", ");
} }
} }
std::cout << "]"; fmt::print("]");
} }
std::cout << std::endl; fmt::print("\n");
std::cout << " StorageId: " << nn::hac::ContentMetaUtil::getStorageIdAsString(mCnmt.getStorageId()) << " (" << std::dec << (uint32_t)mCnmt.getStorageId() << ")" << std::endl; fmt::print(" StorageId: {:s} ({:d})\n", nn::hac::ContentMetaUtil::getStorageIdAsString(mCnmt.getStorageId()), (uint32_t)mCnmt.getStorageId());
std::cout << " ContentInstallType: " << nn::hac::ContentMetaUtil::getContentInstallTypeAsString(mCnmt.getContentInstallType()) << " (" << std::dec << (uint32_t)mCnmt.getContentInstallType() << ")" << std::endl; fmt::print(" ContentInstallType: {:s} ({:d})\n", nn::hac::ContentMetaUtil::getContentInstallTypeAsString(mCnmt.getContentInstallType()),(uint32_t)mCnmt.getContentInstallType());
std::cout << " RequiredDownloadSystemVersion: " << nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getRequiredDownloadSystemVersion()) << " (v" << mCnmt.getRequiredDownloadSystemVersion() << ")"<< std::endl; fmt::print(" RequiredDownloadSystemVersion: {:s} (v{:d})\n", nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getRequiredDownloadSystemVersion()), mCnmt.getRequiredDownloadSystemVersion());
switch(mCnmt.getContentMetaType()) switch(mCnmt.getContentMetaType())
{ {
case (nn::hac::cnmt::ContentMetaType::Application): case (nn::hac::cnmt::ContentMetaType::Application):
std::cout << " ApplicationExtendedHeader:" << std::endl; fmt::print(" ApplicationExtendedHeader:\n");
std::cout << " RequiredApplicationVersion: " << nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getApplicationMetaExtendedHeader().getRequiredApplicationVersion()) << " (v" << std::dec << mCnmt.getApplicationMetaExtendedHeader().getRequiredApplicationVersion() << ")"<< std::endl; fmt::print(" RequiredApplicationVersion: {:s} (v{:d})\n", nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getApplicationMetaExtendedHeader().getRequiredApplicationVersion()), mCnmt.getApplicationMetaExtendedHeader().getRequiredApplicationVersion());
std::cout << " RequiredSystemVersion: " << nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getApplicationMetaExtendedHeader().getRequiredSystemVersion()) << " (v" << std::dec << mCnmt.getApplicationMetaExtendedHeader().getRequiredSystemVersion() << ")"<< std::endl; fmt::print(" RequiredSystemVersion: {:s} (v{:d})\n", nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getApplicationMetaExtendedHeader().getRequiredSystemVersion()), mCnmt.getApplicationMetaExtendedHeader().getRequiredSystemVersion());
std::cout << " PatchId: 0x" << std::hex << std::setw(16) << std::setfill('0') << mCnmt.getApplicationMetaExtendedHeader().getPatchId() << std::endl; fmt::print(" PatchId: 0x{:016x}\n", mCnmt.getApplicationMetaExtendedHeader().getPatchId());
break; break;
case (nn::hac::cnmt::ContentMetaType::Patch): case (nn::hac::cnmt::ContentMetaType::Patch):
std::cout << " PatchMetaExtendedHeader:" << std::endl; fmt::print(" PatchMetaExtendedHeader:\n");
std::cout << " RequiredSystemVersion: " << nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getPatchMetaExtendedHeader().getRequiredSystemVersion()) << " (v" << std::dec << mCnmt.getPatchMetaExtendedHeader().getRequiredSystemVersion() << ")"<< std::endl; fmt::print(" RequiredSystemVersion: {:s} (v{:d})\n", nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getPatchMetaExtendedHeader().getRequiredSystemVersion()), mCnmt.getPatchMetaExtendedHeader().getRequiredSystemVersion());
std::cout << " ApplicationId: 0x" << std::hex << std::setw(16) << std::setfill('0') << mCnmt.getPatchMetaExtendedHeader().getApplicationId() << std::endl; fmt::print(" ApplicationId: 0x{:016x}\n", mCnmt.getPatchMetaExtendedHeader().getApplicationId());
break; break;
case (nn::hac::cnmt::ContentMetaType::AddOnContent): case (nn::hac::cnmt::ContentMetaType::AddOnContent):
std::cout << " AddOnContentMetaExtendedHeader:" << std::endl; fmt::print(" AddOnContentMetaExtendedHeader:\n");
std::cout << " RequiredApplicationVersion: " << nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getAddOnContentMetaExtendedHeader().getRequiredApplicationVersion()) << " (v" << std::dec << mCnmt.getAddOnContentMetaExtendedHeader().getRequiredApplicationVersion() << ")" << std::endl; fmt::print(" RequiredApplicationVersion: {:s} (v{:d})\n", nn::hac::ContentMetaUtil::getVersionAsString(mCnmt.getAddOnContentMetaExtendedHeader().getRequiredApplicationVersion()), mCnmt.getAddOnContentMetaExtendedHeader().getRequiredApplicationVersion());
std::cout << " ApplicationId: 0x" << std::hex << std::setw(16) << std::setfill('0') << mCnmt.getAddOnContentMetaExtendedHeader().getApplicationId() << std::endl; fmt::print(" ApplicationId: 0x{:016x}\n", mCnmt.getAddOnContentMetaExtendedHeader().getApplicationId());
break; break;
case (nn::hac::cnmt::ContentMetaType::Delta): case (nn::hac::cnmt::ContentMetaType::Delta):
std::cout << " DeltaMetaExtendedHeader:" << std::endl; fmt::print(" DeltaMetaExtendedHeader:\n");
std::cout << " ApplicationId: 0x" << std::hex << std::setw(16) << std::setfill('0') << mCnmt.getDeltaMetaExtendedHeader().getApplicationId() << std::endl; fmt::print(" ApplicationId: 0x{:016x}\n", mCnmt.getDeltaMetaExtendedHeader().getApplicationId());
break; break;
default: default:
break; break;
} }
if (mCnmt.getContentInfo().size() > 0) if (mCnmt.getContentInfo().size() > 0)
{ {
printf(" ContentInfo:\n"); fmt::print(" ContentInfo:\n");
for (size_t i = 0; i < mCnmt.getContentInfo().size(); i++) for (size_t i = 0; i < mCnmt.getContentInfo().size(); i++)
{ {
const nn::hac::ContentInfo& info = mCnmt.getContentInfo()[i]; const nn::hac::ContentInfo& info = mCnmt.getContentInfo()[i];
std::cout << " " << std::dec << i << std::endl; fmt::print(" {:d}\n", i);
std::cout << " Type: " << nn::hac::ContentMetaUtil::getContentTypeAsString(info.getContentType()) << " (" << std::dec << (uint32_t)info.getContentType() << ")" << std::endl; fmt::print(" Type: {:s} ({:d})\n", nn::hac::ContentMetaUtil::getContentTypeAsString(info.getContentType()), (uint32_t)info.getContentType());
std::cout << " Id: " << fnd::SimpleTextOutput::arrayToString(info.getContentId().data(), info.getContentId().size(), false, "") << std::endl; fmt::print(" Id: {:s}\n", tc::cli::FormatUtil::formatBytesAsString(info.getContentId().data(), info.getContentId().size(), false, ""));
std::cout << " Size: 0x" << std::hex << info.getContentSize() << std::endl; fmt::print(" Size: 0x{:x}\n", info.getContentSize());
std::cout << " Hash: " << fnd::SimpleTextOutput::arrayToString(info.getContentHash().bytes, sizeof(info.getContentHash()), false, "") << std::endl; fmt::print(" Hash: {:s}\n", tc::cli::FormatUtil::formatBytesAsString(info.getContentHash().data(), info.getContentHash().size(), false, ""));
} }
} }
if (mCnmt.getContentMetaInfo().size() > 0) if (mCnmt.getContentMetaInfo().size() > 0)
{ {
std::cout << " ContentMetaInfo:" << std::endl; fmt::print(" ContentMetaInfo:\n");
displayContentMetaInfoList(mCnmt.getContentMetaInfo(), " "); displayContentMetaInfoList(mCnmt.getContentMetaInfo(), " ");
} }
@ -139,31 +143,31 @@ void nstool::CnmtProcess::displayCnmt()
if (mCnmt.getContentMetaType() == nn::hac::cnmt::ContentMetaType::Patch && mCnmt.getPatchMetaExtendedHeader().getExtendedDataSize() != 0) if (mCnmt.getContentMetaType() == nn::hac::cnmt::ContentMetaType::Patch && mCnmt.getPatchMetaExtendedHeader().getExtendedDataSize() != 0)
{ {
// this is stubbed as the raw output is for development purposes // this is stubbed as the raw output is for development purposes
//std::cout << " PatchMetaExtendedData:" << std::endl; //fmt::print(" PatchMetaExtendedData:\n");
//fnd::SimpleTextOutput::hxdStyleDump(mCnmt.getPatchMetaExtendedData().data(), mCnmt.getPatchMetaExtendedData().size()); //tc::cli::FormatUtil::formatBytesAsHxdHexString(mCnmt.getPatchMetaExtendedData().data(), mCnmt.getPatchMetaExtendedData().size());
} }
else if (mCnmt.getContentMetaType() == nn::hac::cnmt::ContentMetaType::Delta && mCnmt.getDeltaMetaExtendedHeader().getExtendedDataSize() != 0) else if (mCnmt.getContentMetaType() == nn::hac::cnmt::ContentMetaType::Delta && mCnmt.getDeltaMetaExtendedHeader().getExtendedDataSize() != 0)
{ {
// this is stubbed as the raw output is for development purposes // this is stubbed as the raw output is for development purposes
//std::cout << " DeltaMetaExtendedData:" << std::endl; //fmt::print(" DeltaMetaExtendedData:\n");
//fnd::SimpleTextOutput::hxdStyleDump(mCnmt.getDeltaMetaExtendedData().data(), mCnmt.getDeltaMetaExtendedData().size()); //tc::cli::FormatUtil::formatBytesAsHxdHexString(mCnmt.getDeltaMetaExtendedData().data(), mCnmt.getDeltaMetaExtendedData().size());
} }
else if (mCnmt.getContentMetaType() == nn::hac::cnmt::ContentMetaType::SystemUpdate && mCnmt.getSystemUpdateMetaExtendedHeader().getExtendedDataSize() != 0) else if (mCnmt.getContentMetaType() == nn::hac::cnmt::ContentMetaType::SystemUpdate && mCnmt.getSystemUpdateMetaExtendedHeader().getExtendedDataSize() != 0)
{ {
std::cout << " SystemUpdateMetaExtendedData:" << std::endl; fmt::print(" SystemUpdateMetaExtendedData:\n");
std::cout << " FormatVersion: " << std::dec << mCnmt.getSystemUpdateMetaExtendedData().getFormatVersion() << std::endl; fmt::print(" FormatVersion: {:d}\n", mCnmt.getSystemUpdateMetaExtendedData().getFormatVersion());
std::cout << " FirmwareVariation:" << std::endl; fmt::print(" FirmwareVariation:\n");
auto variation_info = mCnmt.getSystemUpdateMetaExtendedData().getFirmwareVariationInfo(); auto variation_info = mCnmt.getSystemUpdateMetaExtendedData().getFirmwareVariationInfo();
for (size_t i = 0; i < mCnmt.getSystemUpdateMetaExtendedData().getFirmwareVariationInfo().size(); i++) for (size_t i = 0; i < mCnmt.getSystemUpdateMetaExtendedData().getFirmwareVariationInfo().size(); i++)
{ {
std::cout << " " << std::dec << i << std::endl; fmt::print(" {:d}\n", i);
std::cout << " FirmwareVariationId: 0x" << std::hex << variation_info[i].variation_id << std::endl; fmt::print(" FirmwareVariationId: 0x{:x}\n", variation_info[i].variation_id);
if (mCnmt.getSystemUpdateMetaExtendedData().getFormatVersion() == 2) if (mCnmt.getSystemUpdateMetaExtendedData().getFormatVersion() == 2)
{ {
std::cout << " ReferToBase: " << std::boolalpha << variation_info[i].meta.empty() << std::endl; fmt::print(" ReferToBase: {}\n", variation_info[i].meta.empty());
if (variation_info[i].meta.empty() == false) if (variation_info[i].meta.empty() == false)
{ {
std::cout << " ContentMeta:" << std::endl; fmt::print(" ContentMeta:\n");
displayContentMetaInfoList(variation_info[i].meta, " "); displayContentMetaInfoList(variation_info[i].meta, " ");
} }
} }
@ -172,39 +176,37 @@ void nstool::CnmtProcess::displayCnmt()
std::cout << " Digest: " << fnd::SimpleTextOutput::arrayToString(mCnmt.getDigest().data(), mCnmt.getDigest().size(), false, "") << std::endl; fmt::print(" Digest: {:s}\n", tc::cli::FormatUtil::formatBytesAsString(mCnmt.getDigest().data(), mCnmt.getDigest().size(), false, ""));
} }
void nstool::CnmtProcess::displayContentMetaInfo(const nn::hac::ContentMetaInfo& content_meta_info, const std::string& prefix) void nstool::CnmtProcess::displayContentMetaInfo(const nn::hac::ContentMetaInfo& content_meta_info, const std::string& prefix)
{ {
std::cout << prefix << "Id: 0x" << std::hex << std::setw(16) << std::setfill('0') << content_meta_info.getTitleId() << std::endl; const nn::hac::sContentMetaInfo* content_meta_info_raw = (const nn::hac::sContentMetaInfo*)content_meta_info.getBytes().data();
std::cout << prefix << "Version: " << nn::hac::ContentMetaUtil::getVersionAsString(content_meta_info.getTitleVersion()) << " (v" << std::dec << content_meta_info.getTitleVersion() << ")"<< std::endl; fmt::print("{:s}Id: 0x{:016x}\n", prefix, content_meta_info.getTitleId());
std::cout << prefix << "Type: " << nn::hac::ContentMetaUtil::getContentMetaTypeAsString(content_meta_info.getContentMetaType()) << " (" << std::dec << (uint32_t)content_meta_info.getContentMetaType() << ")" << std::endl; fmt::print("{:s}Version: {:s} (v{:d})\n", prefix, nn::hac::ContentMetaUtil::getVersionAsString(content_meta_info.getTitleVersion()), content_meta_info.getTitleVersion());
std::cout << prefix << "Attributes: 0x" << std::hex << content_meta_info.getAttribute().to_ullong(); fmt::print("{:s}Type: {:s} ({:d})\n", prefix, nn::hac::ContentMetaUtil::getContentMetaTypeAsString(content_meta_info.getContentMetaType()), (uint32_t)content_meta_info.getContentMetaType());
if (content_meta_info.getAttribute().any()) fmt::print("{:s}Attributes: 0x{:x}", prefix, *((byte_t*)&content_meta_info_raw->attributes) );
if (content_meta_info.getAttribute().size())
{ {
std::vector<std::string> attribute_list; std::vector<std::string> attribute_list;
for (size_t flag = 0; flag < content_meta_info.getAttribute().size(); flag++) for (auto itr = content_meta_info.getAttribute().begin(); itr != content_meta_info.getAttribute().end(); itr++)
{ {
attribute_list.push_back(nn::hac::ContentMetaUtil::getContentMetaAttributeFlagAsString(nn::hac::cnmt::ContentMetaAttributeFlag(*itr)));
if (content_meta_info.getAttribute().test(flag))
{
attribute_list.push_back(nn::hac::ContentMetaUtil::getContentMetaAttributeFlagAsString(nn::hac::cnmt::ContentMetaAttributeFlag(flag)));
}
} }
std::cout << " ["; fmt::print(" [");
for (auto itr = attribute_list.begin(); itr != attribute_list.end(); itr++) for (auto itr = attribute_list.begin(); itr != attribute_list.end(); itr++)
{ {
std::cout << *itr; fmt::print("{:s}",*itr);
if ((itr + 1) != attribute_list.end()) if ((itr + 1) != attribute_list.end())
{ {
std::cout << ", "; fmt::print(", ");
} }
} }
std::cout << "]"; fmt::print("]");
} }
fmt::print("\n");
} }
void nstool::CnmtProcess::displayContentMetaInfoList(const std::vector<nn::hac::ContentMetaInfo>& content_meta_info_list, const std::string& prefix) void nstool::CnmtProcess::displayContentMetaInfoList(const std::vector<nn::hac::ContentMetaInfo>& content_meta_info_list, const std::string& prefix)
@ -212,8 +214,7 @@ void nstool::CnmtProcess::displayContentMetaInfoList(const std::vector<nn::hac::
for (size_t i = 0; i < content_meta_info_list.size(); i++) for (size_t i = 0; i < content_meta_info_list.size(); i++)
{ {
const nn::hac::ContentMetaInfo& info = mCnmt.getContentMetaInfo()[i]; const nn::hac::ContentMetaInfo& info = mCnmt.getContentMetaInfo()[i];
std::cout << prefix << std::dec << i << std::endl; fmt::print("{:s}{:d}\n", i);
displayContentMetaInfo(info, prefix + " "); displayContentMetaInfo(info, prefix + " ");
std::cout << std::endl;
} }
} }

View file

@ -19,7 +19,7 @@ public:
const nn::hac::ContentMeta& getContentMeta() const; const nn::hac::ContentMeta& getContentMeta() const;
private: private:
const std::string kModuleName = "CnmtProcess"; std::string mModuleName;
std::shared_ptr<tc::io::IStream> mFile; std::shared_ptr<tc::io::IStream> mFile;
CliOutputMode mCliOutputMode; CliOutputMode mCliOutputMode;