2020-02-26 09:05:51 +00:00
|
|
|
#include "PfsProcess.h"
|
2021-10-02 14:53:21 +00:00
|
|
|
#include "util.h"
|
2020-02-26 09:05:51 +00:00
|
|
|
|
|
|
|
#include <nn/hac/PartitionFsUtil.h>
|
2021-10-02 14:53:21 +00:00
|
|
|
#include <tc/io/LocalStorage.h>
|
2020-02-26 09:05:51 +00:00
|
|
|
|
2021-10-02 14:57:35 +00:00
|
|
|
#include <tc/io/VirtualFileSystem.h>
|
|
|
|
#include <nn/hac/PartitionFsMetaGenerator.h>
|
|
|
|
|
2018-04-24 05:24:20 +00:00
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
nstool::PfsProcess::PfsProcess() :
|
2021-10-02 14:53:21 +00:00
|
|
|
mModuleName("nstool::PfsProcess"),
|
2018-09-23 03:29:22 +00:00
|
|
|
mFile(),
|
2021-09-28 11:15:54 +00:00
|
|
|
mCliOutputMode(true, false, false, false),
|
2018-04-24 05:24:20 +00:00
|
|
|
mVerify(false),
|
2021-10-02 14:57:35 +00:00
|
|
|
mPfs(),
|
|
|
|
mFileSystem(),
|
|
|
|
mFsProcess()
|
2018-04-24 05:24:20 +00:00
|
|
|
{
|
2021-10-08 10:05:51 +00:00
|
|
|
mFsProcess.setFsFormatName("PartitionFS");
|
2018-04-24 05:24:20 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 10:05:51 +00:00
|
|
|
void nstool::PfsProcess::process()
|
2018-08-13 17:14:21 +00:00
|
|
|
{
|
2021-10-02 14:53:21 +00:00
|
|
|
if (mFile == nullptr)
|
|
|
|
{
|
|
|
|
throw tc::Exception(mModuleName, "No file reader set.");
|
|
|
|
}
|
2021-10-03 04:01:23 +00:00
|
|
|
if (mFile->canRead() == false || mFile->canSeek() == false)
|
|
|
|
{
|
|
|
|
throw tc::NotSupportedException(mModuleName, "Input stream requires read/seek permissions.");
|
|
|
|
}
|
2021-10-02 14:53:21 +00:00
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
tc::ByteData scratch;
|
2018-08-13 17:14:21 +00:00
|
|
|
|
2021-10-02 14:53:21 +00:00
|
|
|
// read base header to determine complete header size
|
|
|
|
if (mFile->length() < tc::io::IOUtil::castSizeToInt64(sizeof(nn::hac::sPfsHeader)))
|
2018-08-13 17:14:21 +00:00
|
|
|
{
|
2021-10-02 14:53:21 +00:00
|
|
|
throw tc::Exception(mModuleName, "Corrupt PartitionFs: File too small");
|
2018-08-13 17:14:21 +00:00
|
|
|
}
|
2021-10-02 14:53:21 +00:00
|
|
|
|
|
|
|
scratch = tc::ByteData(sizeof(nn::hac::sPfsHeader));
|
|
|
|
mFile->seek(0, tc::io::SeekOrigin::Begin);
|
|
|
|
mFile->read(scratch.data(), scratch.size());
|
2018-08-13 17:14:21 +00:00
|
|
|
if (validateHeaderMagic(((nn::hac::sPfsHeader*)scratch.data())) == false)
|
|
|
|
{
|
2021-10-02 14:53:21 +00:00
|
|
|
throw tc::Exception(mModuleName, "Corrupt PartitionFs: Header had incorrect struct magic.");
|
2018-08-13 17:14:21 +00:00
|
|
|
}
|
2021-10-02 14:53:21 +00:00
|
|
|
|
|
|
|
// read complete size header
|
2018-08-13 17:14:21 +00:00
|
|
|
size_t pfsHeaderSize = determineHeaderSize(((nn::hac::sPfsHeader*)scratch.data()));
|
2021-10-02 14:53:21 +00:00
|
|
|
if (mFile->length() < tc::io::IOUtil::castSizeToInt64(pfsHeaderSize))
|
|
|
|
{
|
|
|
|
throw tc::Exception(mModuleName, "Corrupt PartitionFs: File too small");
|
|
|
|
}
|
|
|
|
|
|
|
|
scratch = tc::ByteData(pfsHeaderSize);
|
|
|
|
mFile->seek(0, tc::io::SeekOrigin::Begin);
|
|
|
|
mFile->read(scratch.data(), scratch.size());
|
|
|
|
|
|
|
|
// process PFS
|
2018-08-13 17:14:21 +00:00
|
|
|
mPfs.fromBytes(scratch.data(), scratch.size());
|
2021-10-02 14:57:35 +00:00
|
|
|
|
|
|
|
// create virtual filesystem
|
|
|
|
mFileSystem = std::make_shared<tc::io::VirtualFileSystem>(tc::io::VirtualFileSystem(nn::hac::PartitionFsMetaGenerator(mFile, mVerify ? nn::hac::PartitionFsMetaGenerator::ValidationMode_Warn : nn::hac::PartitionFsMetaGenerator::ValidationMode_None)));
|
|
|
|
mFsProcess.setInputFileSystem(mFileSystem);
|
2021-10-08 10:05:51 +00:00
|
|
|
|
|
|
|
// set properties for FsProcess
|
|
|
|
mFsProcess.setFsProperties({
|
|
|
|
fmt::format("Type: {:s}", nn::hac::PartitionFsUtil::getFsTypeAsString(mPfs.getFsType())),
|
|
|
|
fmt::format("FileNum: {:d}", mPfs.getFileList().size())
|
|
|
|
});
|
|
|
|
|
|
|
|
mFsProcess.process();
|
2018-08-13 17:14:21 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 07:41:02 +00:00
|
|
|
void nstool::PfsProcess::setInputFile(const std::shared_ptr<tc::io::IStream>& file)
|
|
|
|
{
|
|
|
|
mFile = file;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nstool::PfsProcess::setCliOutputMode(CliOutputMode type)
|
|
|
|
{
|
|
|
|
mCliOutputMode = type;
|
|
|
|
mFsProcess.setShowFsInfo(mCliOutputMode.show_basic_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nstool::PfsProcess::setVerifyMode(bool verify)
|
|
|
|
{
|
|
|
|
mVerify = verify;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nstool::PfsProcess::setShowFsTree(bool show_fs_tree)
|
|
|
|
{
|
|
|
|
mFsProcess.setShowFsTree(show_fs_tree);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nstool::PfsProcess::setFsRootLabel(const std::string& root_label)
|
|
|
|
{
|
|
|
|
mFsProcess.setFsRootLabel(root_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nstool::PfsProcess::setExtractJobs(const std::vector<nstool::ExtractJob>& extract_jobs)
|
|
|
|
{
|
|
|
|
mFsProcess.setExtractJobs(extract_jobs);
|
|
|
|
}
|
|
|
|
|
2021-10-08 10:05:51 +00:00
|
|
|
const nn::hac::PartitionFsHeader& nstool::PfsProcess::getPfsHeader() const
|
2018-05-26 13:13:21 +00:00
|
|
|
{
|
2021-10-08 10:05:51 +00:00
|
|
|
return mPfs;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::shared_ptr<tc::io::IStorage>& nstool::PfsProcess::getFileSystem() const
|
|
|
|
{
|
|
|
|
return mFileSystem;
|
2018-05-26 13:13:21 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
size_t nstool::PfsProcess::determineHeaderSize(const nn::hac::sPfsHeader* hdr)
|
2018-05-26 13:13:21 +00:00
|
|
|
{
|
|
|
|
size_t fileEntrySize = 0;
|
2021-10-02 14:53:21 +00:00
|
|
|
if (hdr->st_magic.unwrap() == nn::hac::pfs::kPfsStructMagic)
|
2018-08-07 07:17:51 +00:00
|
|
|
fileEntrySize = sizeof(nn::hac::sPfsFile);
|
2018-05-26 13:13:21 +00:00
|
|
|
else
|
2018-08-07 07:17:51 +00:00
|
|
|
fileEntrySize = sizeof(nn::hac::sHashedPfsFile);
|
2018-05-26 13:13:21 +00:00
|
|
|
|
2021-10-02 14:53:21 +00:00
|
|
|
return sizeof(nn::hac::sPfsHeader) + hdr->file_num.unwrap() * fileEntrySize + hdr->name_table_size.unwrap();
|
2018-05-26 13:13:21 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
bool nstool::PfsProcess::validateHeaderMagic(const nn::hac::sPfsHeader* hdr)
|
2018-05-26 13:13:21 +00:00
|
|
|
{
|
2021-10-02 14:53:21 +00:00
|
|
|
return hdr->st_magic.unwrap() == nn::hac::pfs::kPfsStructMagic || hdr->st_magic.unwrap() == nn::hac::pfs::kHashedPfsStructMagic;
|
2018-08-13 17:14:21 +00:00
|
|
|
}
|