2018-11-05 13:11:08 +00:00
|
|
|
#include "IniProcess.h"
|
|
|
|
|
2021-10-13 07:33:47 +00:00
|
|
|
#include "util.h"
|
2021-10-13 05:39:45 +00:00
|
|
|
#include "KipProcess.h"
|
2018-11-05 13:11:08 +00:00
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
nstool::IniProcess::IniProcess() :
|
2021-10-12 06:26:19 +00:00
|
|
|
mModuleName("nstool::IniProcess"),
|
2018-11-05 13:11:08 +00:00
|
|
|
mFile(),
|
2021-09-28 11:15:54 +00:00
|
|
|
mCliOutputMode(true, false, false, false),
|
2018-11-05 13:11:08 +00:00
|
|
|
mVerify(false),
|
|
|
|
mKipExtractPath()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::IniProcess::process()
|
2018-11-05 13:11:08 +00:00
|
|
|
{
|
|
|
|
importHeader();
|
|
|
|
importKipList();
|
2021-09-28 11:15:54 +00:00
|
|
|
if (mCliOutputMode.show_basic_info)
|
2018-11-05 13:11:08 +00:00
|
|
|
{
|
|
|
|
displayHeader();
|
|
|
|
displayKipList();
|
|
|
|
}
|
2021-10-12 06:26:19 +00:00
|
|
|
if (mKipExtractPath.isSet())
|
2018-11-05 13:11:08 +00:00
|
|
|
{
|
|
|
|
extractKipList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::IniProcess::setInputFile(const std::shared_ptr<tc::io::IStream>& file)
|
2018-11-05 13:11:08 +00:00
|
|
|
{
|
|
|
|
mFile = file;
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::IniProcess::setCliOutputMode(CliOutputMode type)
|
2018-11-05 13:11:08 +00:00
|
|
|
{
|
|
|
|
mCliOutputMode = type;
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::IniProcess::setVerifyMode(bool verify)
|
2018-11-05 13:11:08 +00:00
|
|
|
{
|
|
|
|
mVerify = verify;
|
|
|
|
}
|
|
|
|
|
2021-10-12 06:26:19 +00:00
|
|
|
void nstool::IniProcess::setKipExtractPath(const tc::io::Path& path)
|
2018-11-05 13:11:08 +00:00
|
|
|
{
|
|
|
|
mKipExtractPath = path;
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::IniProcess::importHeader()
|
2018-11-05 13:11:08 +00:00
|
|
|
{
|
2021-10-12 06:26:19 +00:00
|
|
|
if (mFile == nullptr)
|
|
|
|
{
|
|
|
|
throw tc::Exception(mModuleName, "No file reader set.");
|
|
|
|
}
|
|
|
|
if (mFile->canRead() == false || mFile->canSeek() == false)
|
2018-11-05 13:11:08 +00:00
|
|
|
{
|
2021-10-12 06:26:19 +00:00
|
|
|
throw tc::NotSupportedException(mModuleName, "Input stream requires read/seek permissions.");
|
2018-11-05 13:11:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 06:26:19 +00:00
|
|
|
// check if file_size is smaller than INI header size
|
2022-06-29 13:19:36 +00:00
|
|
|
if (tc::io::IOUtil::castInt64ToSize(mFile->length()) < sizeof(pie::hac::sIniHeader))
|
2018-11-05 13:11:08 +00:00
|
|
|
{
|
2021-10-13 07:33:47 +00:00
|
|
|
throw tc::Exception(mModuleName, "Corrupt INI: file too small.");
|
2018-11-05 13:11:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 06:26:19 +00:00
|
|
|
// read ini
|
2022-06-29 13:19:36 +00:00
|
|
|
tc::ByteData scratch = tc::ByteData(sizeof(pie::hac::sIniHeader));
|
2021-10-12 06:26:19 +00:00
|
|
|
mFile->seek(0, tc::io::SeekOrigin::Begin);
|
|
|
|
mFile->read(scratch.data(), scratch.size());
|
2018-11-05 13:11:08 +00:00
|
|
|
|
2021-10-12 06:26:19 +00:00
|
|
|
// parse ini header
|
2018-11-05 13:11:08 +00:00
|
|
|
mHdr.fromBytes(scratch.data(), scratch.size());
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::IniProcess::importKipList()
|
2018-11-05 13:11:08 +00:00
|
|
|
{
|
|
|
|
// kip pos info
|
2022-06-29 13:19:36 +00:00
|
|
|
int64_t kip_pos = tc::io::IOUtil::castSizeToInt64(sizeof(pie::hac::sIniHeader));
|
2021-10-13 05:39:45 +00:00
|
|
|
int64_t kip_size = 0;
|
2018-11-05 13:11:08 +00:00
|
|
|
|
|
|
|
// tmp data to determine size
|
2022-06-29 13:19:36 +00:00
|
|
|
pie::hac::sKipHeader hdr_raw;
|
|
|
|
pie::hac::KernelInitialProcessHeader hdr;
|
2018-11-05 13:11:08 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < mHdr.getKipNum(); i++)
|
|
|
|
{
|
2021-10-13 05:39:45 +00:00
|
|
|
mFile->seek(kip_pos, tc::io::SeekOrigin::Begin);
|
|
|
|
mFile->read((byte_t*)&hdr_raw, sizeof(hdr_raw));
|
|
|
|
hdr.fromBytes((byte_t*)&hdr_raw, sizeof(hdr_raw));
|
2018-11-05 13:11:08 +00:00
|
|
|
kip_size = getKipSizeFromHeader(hdr);
|
2021-10-13 07:33:47 +00:00
|
|
|
mKipList.push_back({hdr, std::make_shared<tc::io::SubStream>(tc::io::SubStream(mFile, kip_pos, kip_size))});
|
2018-11-05 13:11:08 +00:00
|
|
|
kip_pos += kip_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::IniProcess::displayHeader()
|
2018-11-05 13:11:08 +00:00
|
|
|
{
|
2021-10-12 06:26:19 +00:00
|
|
|
fmt::print("[INI Header]\n");
|
|
|
|
fmt::print(" Size: 0x{:x}\n", mHdr.getSize());
|
|
|
|
fmt::print(" KIP Num: {:d}\n", mHdr.getKipNum());
|
2018-11-05 13:11:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::IniProcess::displayKipList()
|
2018-11-05 13:11:08 +00:00
|
|
|
{
|
2021-10-13 07:33:47 +00:00
|
|
|
for (auto itr = mKipList.begin(); itr != mKipList.end(); itr++)
|
2018-11-05 13:11:08 +00:00
|
|
|
{
|
|
|
|
KipProcess obj;
|
|
|
|
|
2021-10-13 07:33:47 +00:00
|
|
|
obj.setInputFile(itr->stream);
|
2018-11-05 13:11:08 +00:00
|
|
|
obj.setCliOutputMode(mCliOutputMode);
|
|
|
|
obj.setVerifyMode(mVerify);
|
|
|
|
|
|
|
|
obj.process();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::IniProcess::extractKipList()
|
2018-11-05 13:11:08 +00:00
|
|
|
{
|
|
|
|
// allocate cache memory
|
2021-10-13 07:33:47 +00:00
|
|
|
tc::ByteData cache = tc::ByteData(kCacheSize);
|
2018-11-05 13:11:08 +00:00
|
|
|
|
|
|
|
// make extract dir
|
2022-06-29 13:19:36 +00:00
|
|
|
tc::io::LocalFileSystem local_fs;
|
2021-10-13 07:33:47 +00:00
|
|
|
local_fs.createDirectory(mKipExtractPath.get());
|
2018-11-05 13:11:08 +00:00
|
|
|
|
2021-10-13 07:33:47 +00:00
|
|
|
// out path for extracted KIP
|
|
|
|
tc::io::Path out_path;
|
2018-11-05 13:11:08 +00:00
|
|
|
|
2021-10-13 07:33:47 +00:00
|
|
|
// extract KIPs
|
|
|
|
for (auto itr = mKipList.begin(); itr != mKipList.end(); itr++)
|
2018-11-05 13:11:08 +00:00
|
|
|
{
|
2021-10-13 07:33:47 +00:00
|
|
|
out_path = mKipExtractPath.get();
|
|
|
|
out_path += fmt::format("{:s}.kip", itr->hdr.getName());
|
2018-11-05 13:11:08 +00:00
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
if (mCliOutputMode.show_basic_info)
|
2022-06-29 13:19:36 +00:00
|
|
|
fmt::print("Saving {:s}...\n", out_path.to_string());
|
2021-10-13 07:33:47 +00:00
|
|
|
|
|
|
|
writeStreamToFile(itr->stream, out_path, cache);
|
2018-11-05 13:11:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:19:36 +00:00
|
|
|
int64_t nstool::IniProcess::getKipSizeFromHeader(const pie::hac::KernelInitialProcessHeader& hdr) const
|
2021-10-13 07:33:47 +00:00
|
|
|
{
|
|
|
|
// the order of elements in a KIP are sequential, there are no file offsets
|
2022-06-29 13:19:36 +00:00
|
|
|
return int64_t(sizeof(pie::hac::sKipHeader)) + int64_t(hdr.getTextSegmentInfo().file_layout.size + hdr.getRoSegmentInfo().file_layout.size + hdr.getDataSegmentInfo().file_layout.size);
|
2018-11-05 13:11:08 +00:00
|
|
|
}
|