nod/lib/DiscGCN.cpp

260 lines
8.4 KiB
C++
Raw Normal View History

2015-07-02 18:33:55 +00:00
#include "NOD/DiscGCN.hpp"
2015-07-14 02:24:17 +00:00
#define BUFFER_SZ 0x8000
2015-06-28 05:43:53 +00:00
namespace NOD
{
2015-06-30 00:07:46 +00:00
class PartitionGCN : public DiscBase::IPartition
{
public:
PartitionGCN(const DiscGCN& parent, Kind kind, uint64_t offset)
2015-06-30 00:07:46 +00:00
: IPartition(parent, kind, offset)
{
/* GCN-specific header reads */
2015-06-30 06:46:19 +00:00
std::unique_ptr<IPartReadStream> s = beginReadStream(0x420);
2015-06-30 00:07:46 +00:00
uint32_t vals[3];
s->read(vals, 12);
m_dolOff = SBig(vals[0]);
m_fstOff = SBig(vals[1]);
m_fstSz = SBig(vals[2]);
s->seek(0x2440 + 0x14);
s->read(vals, 8);
2015-07-04 17:30:00 +00:00
m_apploaderSz = 32 + SBig(vals[0]) + SBig(vals[1]);
2015-06-30 00:07:46 +00:00
/* Yay files!! */
2015-08-05 21:45:25 +00:00
parseFST(*s);
2015-07-10 03:11:30 +00:00
/* Also make DOL header and size handy */
s->seek(m_dolOff);
2015-08-05 21:45:25 +00:00
parseDOL(*s);
2015-06-30 00:07:46 +00:00
}
2015-06-30 06:46:19 +00:00
class PartReadStream : public IPartReadStream
2015-06-30 00:07:46 +00:00
{
const PartitionGCN& m_parent;
std::unique_ptr<IDiscIO::IReadStream> m_dio;
2015-07-14 02:24:17 +00:00
uint64_t m_offset;
size_t m_curBlock = SIZE_MAX;
uint8_t m_buf[BUFFER_SZ];
2015-06-30 00:07:46 +00:00
public:
PartReadStream(const PartitionGCN& parent, uint64_t offset)
2015-07-14 02:24:17 +00:00
: m_parent(parent), m_offset(offset)
{
size_t block = m_offset / BUFFER_SZ;
m_dio = m_parent.m_parent.getDiscIO().beginReadStream(block * BUFFER_SZ);
m_dio->read(m_buf, BUFFER_SZ);
m_curBlock = block;
}
void seek(int64_t offset, int whence)
2015-07-14 02:24:17 +00:00
{
if (whence == SEEK_SET)
m_offset = offset;
else if (whence == SEEK_CUR)
m_offset += offset;
else
return;
size_t block = m_offset / BUFFER_SZ;
if (block != m_curBlock)
{
m_dio->seek(block * BUFFER_SZ);
m_dio->read(m_buf, BUFFER_SZ);
m_curBlock = block;
}
}
uint64_t position() const {return m_offset;}
uint64_t read(void* buf, uint64_t length)
2015-07-14 02:24:17 +00:00
{
size_t block = m_offset / BUFFER_SZ;
size_t cacheOffset = m_offset % BUFFER_SZ;
uint64_t cacheSize;
uint64_t rem = length;
uint8_t* dst = (uint8_t*)buf;
while (rem)
{
if (block != m_curBlock)
{
m_dio->read(m_buf, BUFFER_SZ);
m_curBlock = block;
}
cacheSize = rem;
if (cacheSize + cacheOffset > BUFFER_SZ)
cacheSize = BUFFER_SZ - cacheOffset;
memcpy(dst, m_buf + cacheOffset, cacheSize);
dst += cacheSize;
rem -= cacheSize;
cacheOffset = 0;
++block;
}
m_offset += length;
return dst - (uint8_t*)buf;
}
2015-06-30 00:07:46 +00:00
};
std::unique_ptr<IPartReadStream> beginReadStream(uint64_t offset) const
2015-06-30 00:07:46 +00:00
{
2015-06-30 06:46:19 +00:00
return std::unique_ptr<IPartReadStream>(new PartReadStream(*this, offset));
2015-06-30 00:07:46 +00:00
}
};
2015-06-28 05:43:53 +00:00
DiscGCN::DiscGCN(std::unique_ptr<IDiscIO>&& dio)
: DiscBase(std::move(dio))
{
2015-06-30 00:07:46 +00:00
/* One lone partition for GCN */
2015-11-21 01:15:33 +00:00
m_partitions.emplace_back(new PartitionGCN(*this, IPartition::Kind::Data, 0));
2015-06-28 05:43:53 +00:00
}
class PartitionBuilderGCN : public DiscBuilderBase::PartitionBuilderBase
2015-06-28 05:43:53 +00:00
{
2016-01-21 06:30:37 +00:00
uint64_t m_curUser = 0x57058000;
uint32_t m_fstMemoryAddr;
2016-01-25 02:00:02 +00:00
2016-01-21 06:30:37 +00:00
public:
2016-01-25 02:00:02 +00:00
class PartWriteStream : public IPartWriteStream
{
const PartitionBuilderGCN& m_parent;
uint64_t m_offset;
std::unique_ptr<IFileIO::IWriteStream> m_fio;
public:
PartWriteStream(const PartitionBuilderGCN& parent, uint64_t offset)
: m_parent(parent), m_offset(offset)
{
m_fio = m_parent.m_parent.getFileIO().beginWriteStream(offset);
}
void close() {m_fio.reset();}
uint64_t position() const {return m_offset;}
uint64_t write(const void* buf, uint64_t length)
{
uint64_t len = m_fio->write(buf, length);
m_offset += len;
return len;
}
void seek(size_t off)
{
m_offset = off;
m_fio = m_parent.m_parent.getFileIO().beginWriteStream(off);
}
};
PartitionBuilderGCN(DiscBuilderBase& parent, Kind kind,
2016-01-21 06:30:37 +00:00
const char gameID[6], const char* gameTitle, uint32_t fstMemoryAddr)
: DiscBuilderBase::PartitionBuilderBase(parent, kind, gameID, gameTitle), m_fstMemoryAddr(fstMemoryAddr) {}
2016-01-21 06:30:37 +00:00
2016-01-25 02:00:02 +00:00
uint64_t userAllocate(uint64_t reqSz, IPartWriteStream& ws)
2016-01-21 06:30:37 +00:00
{
m_curUser -= reqSz;
m_curUser &= 0xfffffffffffffff0;
if (m_curUser < 0x30000)
{
LogModule.report(LogVisor::FatalError, "user area low mark reached");
return -1;
}
2016-01-25 02:00:02 +00:00
static_cast<PartWriteStream&>(ws).seek(m_curUser);
2016-01-21 06:30:37 +00:00
return m_curUser;
}
2016-01-22 23:45:58 +00:00
uint32_t packOffset(uint64_t offset) const
{
return offset;
}
2016-01-25 02:00:02 +00:00
std::unique_ptr<IPartWriteStream> beginWriteStream(uint64_t offset)
{
return std::make_unique<PartWriteStream>(*this, offset);
}
2016-01-21 06:30:37 +00:00
bool buildFromDirectory(const SystemChar* dirIn, const SystemChar* dolIn, const SystemChar* apploaderIn)
{
2016-01-25 02:00:02 +00:00
std::unique_ptr<IPartWriteStream> ws = beginWriteStream(0);
bool result = DiscBuilderBase::PartitionBuilderBase::buildFromDirectory(*ws, dirIn, dolIn, apploaderIn);
2016-01-21 06:30:37 +00:00
if (!result)
return false;
2016-01-25 02:00:02 +00:00
ws = beginWriteStream(0);
2016-01-21 06:30:37 +00:00
Header header(m_gameID, m_gameTitle.c_str(), false);
header.write(*ws);
2016-01-25 02:00:02 +00:00
ws = beginWriteStream(0x2440);
2016-01-25 05:55:31 +00:00
std::unique_ptr<IFileIO::IReadStream> rs = NewFileIO(apploaderIn)->beginReadStream();
2016-01-21 06:30:37 +00:00
char buf[8192];
size_t xferSz = 0;
SystemString apploaderName(apploaderIn);
++m_parent.m_progressIdx;
while (true)
{
2016-01-25 05:55:31 +00:00
size_t rdSz = rs->read(buf, 8192);
2016-01-21 06:30:37 +00:00
if (!rdSz)
break;
ws->write(buf, rdSz);
xferSz += rdSz;
if (0x2440 + xferSz >= m_curUser)
LogModule.report(LogVisor::FatalError,
"apploader flows into user area (one or the other is too big)");
m_parent.m_progressCB(m_parent.m_progressIdx, apploaderName, xferSz);
}
size_t fstOff = ROUND_UP_32(xferSz);
size_t fstSz = sizeof(FSTNode) * m_buildNodes.size();
for (size_t i=0 ; i<fstOff-xferSz ; ++i)
ws->write("\xff", 1);
2016-01-21 20:46:07 +00:00
fstOff += 0x2440;
2016-01-21 06:30:37 +00:00
ws->write(m_buildNodes.data(), fstSz);
for (const std::string& str : m_buildNames)
ws->write(str.data(), str.size()+1);
fstSz += m_buildNameOff;
fstSz = ROUND_UP_32(fstSz);
if (fstOff + fstSz >= m_curUser)
LogModule.report(LogVisor::FatalError,
"FST flows into user area (one or the other is too big)");
2016-01-25 02:00:02 +00:00
ws = beginWriteStream(0x420);
2016-01-21 06:30:37 +00:00
uint32_t vals[7];
vals[0] = SBig(uint32_t(m_dolOffset));
vals[1] = SBig(uint32_t(fstOff));
vals[2] = SBig(uint32_t(fstSz));
vals[3] = SBig(uint32_t(fstSz));
vals[4] = SBig(uint32_t(m_fstMemoryAddr));
vals[5] = SBig(uint32_t(m_curUser));
vals[6] = SBig(uint32_t(0x57058000 - m_curUser));
ws->write(vals, sizeof(vals));
return true;
}
};
2016-01-20 03:17:24 +00:00
2016-01-21 06:30:37 +00:00
bool DiscBuilderGCN::buildFromDirectory(const SystemChar* dirIn, const SystemChar* dolIn,
const SystemChar* apploaderIn)
2016-01-25 05:55:31 +00:00
{
2016-01-25 02:00:02 +00:00
m_fileIO->beginWriteStream();
if (!CheckFreeSpace(m_outPath, 0x57058000))
{
LogModule.report(LogVisor::Error, _S("not enough free disk space for %s"), m_outPath);
return false;
}
2016-02-02 20:42:17 +00:00
++m_progressIdx;
m_progressCB(m_progressIdx, _S("Preallocating image"), -1);
m_fileIO->beginWriteStream(0x57058000 - 1)->write("", 1);
2016-01-25 02:00:02 +00:00
2016-01-21 06:30:37 +00:00
PartitionBuilderGCN& pb = static_cast<PartitionBuilderGCN&>(*m_partitions[0]);
return pb.buildFromDirectory(dirIn, dolIn, apploaderIn);
}
2016-01-20 03:17:24 +00:00
2016-01-21 06:30:37 +00:00
DiscBuilderGCN::DiscBuilderGCN(const SystemChar* outPath, const char gameID[6], const char* gameTitle,
uint32_t fstMemoryAddr, std::function<void(size_t, const SystemString&, size_t)> progressCB)
2016-01-25 02:00:02 +00:00
: DiscBuilderBase(outPath, progressCB)
2016-01-21 06:30:37 +00:00
{
PartitionBuilderGCN* partBuilder = new PartitionBuilderGCN(*this, PartitionBuilderBase::Kind::Data,
2016-01-21 06:30:37 +00:00
gameID, gameTitle, fstMemoryAddr);
m_partitions.emplace_back(partBuilder);
2015-06-28 05:43:53 +00:00
}
}