2017-07-05 08:57:14 +00:00
|
|
|
#include "NcaHeader.h"
|
|
|
|
#include <fnd/exception.h>
|
|
|
|
|
2017-07-06 11:17:21 +00:00
|
|
|
using namespace nx;
|
|
|
|
|
|
|
|
|
2017-07-05 08:57:14 +00:00
|
|
|
void NcaHeader::exportBinary()
|
|
|
|
{
|
2017-07-05 15:38:14 +00:00
|
|
|
mBinaryBlob.alloc(sizeof(sNcaHeader));
|
2017-07-06 03:30:27 +00:00
|
|
|
sNcaHeader* hdr = (sNcaHeader*)mBinaryBlob.getBytes();
|
2017-07-05 15:38:14 +00:00
|
|
|
|
|
|
|
hdr->set_signature(kNcaSig.c_str());
|
|
|
|
hdr->set_block_size(kDefaultBlockSize);
|
|
|
|
hdr->set_nca_size(mNcaSize);
|
|
|
|
hdr->set_program_id(mProgramId);
|
|
|
|
hdr->set_unk0(mUnk0);
|
|
|
|
|
|
|
|
// TODO: properly reconstruct NCA layout? atm in hands of user
|
|
|
|
|
2017-07-06 10:55:58 +00:00
|
|
|
for (size_t i = 0; i < mSections.getSize(); i++)
|
2017-07-05 15:38:14 +00:00
|
|
|
{
|
|
|
|
// determine section index
|
2017-07-06 10:55:58 +00:00
|
|
|
u8 section = mSections.getSize() - 1 - i;
|
2017-07-05 15:38:14 +00:00
|
|
|
|
2017-07-06 10:55:58 +00:00
|
|
|
hdr->section(section).set_start(sizeToBlockNum(mSections[i].offset));
|
|
|
|
hdr->section(section).set_end(sizeToBlockNum(mSections[i].offset) + sizeToBlockNum(mSections[i].size));
|
2017-07-05 15:38:14 +00:00
|
|
|
hdr->section(section).set_key_type(mSections[i].key_type);
|
|
|
|
hdr->section_hash(section) = mSections[i].hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < kAesKeyNum; i++)
|
|
|
|
{
|
|
|
|
hdr->aes_key(i) = mAesKeys[i];
|
|
|
|
}
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
void NcaHeader::importBinary(const u8 * bytes, size_t len)
|
2017-07-05 08:57:14 +00:00
|
|
|
{
|
2017-07-15 08:28:01 +00:00
|
|
|
if (len < sizeof(sNcaHeader))
|
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "NCA header size is too small");
|
|
|
|
}
|
|
|
|
|
2017-07-05 08:57:14 +00:00
|
|
|
clearVariables();
|
2017-07-15 08:28:01 +00:00
|
|
|
|
2017-07-05 15:38:14 +00:00
|
|
|
mBinaryBlob.alloc(sizeof(sNcaHeader));
|
2017-07-06 03:30:27 +00:00
|
|
|
memcpy(mBinaryBlob.getBytes(), bytes, sizeof(sNcaHeader));
|
2017-07-05 08:57:14 +00:00
|
|
|
|
2017-07-06 03:30:27 +00:00
|
|
|
sNcaHeader* hdr = (sNcaHeader*)mBinaryBlob.getBytes();
|
2017-07-05 08:57:14 +00:00
|
|
|
|
|
|
|
if (memcmp(hdr->signature(), kNcaSig.c_str(), 4) != 0)
|
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "NCA header corrupt");
|
|
|
|
}
|
2017-07-15 08:28:01 +00:00
|
|
|
|
2017-07-05 15:38:14 +00:00
|
|
|
mBlockSize = hdr->block_size();
|
|
|
|
mNcaSize = hdr->nca_size();
|
|
|
|
mProgramId = hdr->program_id();
|
|
|
|
mUnk0 = hdr->unk0();
|
2017-07-05 08:57:14 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < kSectionNum; i++)
|
|
|
|
{
|
|
|
|
// determine section index
|
|
|
|
u8 section = kSectionNum - 1 - i;
|
2017-07-15 08:28:01 +00:00
|
|
|
|
2017-07-05 08:57:14 +00:00
|
|
|
// skip sections that don't exist
|
|
|
|
if (hdr->section(section).start() == 0 && hdr->section(section).end() == 0) continue;
|
2017-07-15 08:28:01 +00:00
|
|
|
|
2017-07-05 08:57:14 +00:00
|
|
|
// add high level struct
|
2017-07-06 10:55:58 +00:00
|
|
|
mSections.addElement({ blockNumToSize(hdr->section(section).start()), blockNumToSize(hdr->section(section).end() - hdr->section(section).start()), hdr->section(section).key_type(), hdr->section_hash(section) });
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < kAesKeyNum; i++)
|
|
|
|
{
|
2017-07-06 10:55:58 +00:00
|
|
|
mAesKeys.addElement(hdr->aes_key(i));
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
void nx::NcaHeader::clear()
|
2017-07-06 03:30:27 +00:00
|
|
|
{
|
2017-07-15 08:28:01 +00:00
|
|
|
clearVariables();
|
2017-07-06 03:30:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-05 08:57:14 +00:00
|
|
|
u64 NcaHeader::getNcaSize() const
|
|
|
|
{
|
2017-07-05 15:38:14 +00:00
|
|
|
return mNcaSize;
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NcaHeader::setNcaSize(u64 size)
|
|
|
|
{
|
2017-07-05 15:38:14 +00:00
|
|
|
mNcaSize = size;
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 NcaHeader::getProgramId() const
|
|
|
|
{
|
2017-07-05 15:38:14 +00:00
|
|
|
return mProgramId;
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NcaHeader::setProgramId(u64 program_id)
|
|
|
|
{
|
2017-07-05 15:38:14 +00:00
|
|
|
mProgramId = program_id;
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 NcaHeader::getUnk() const
|
|
|
|
{
|
2017-07-05 15:38:14 +00:00
|
|
|
return mUnk0;
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 10:55:58 +00:00
|
|
|
const fnd::List<NcaHeader::sSection>& NcaHeader::getSections() const
|
2017-07-05 08:57:14 +00:00
|
|
|
{
|
2017-07-05 15:38:14 +00:00
|
|
|
return mSections;
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NcaHeader::addSection(const sSection & section)
|
|
|
|
{
|
2017-07-06 10:55:58 +00:00
|
|
|
if (mSections.getSize() >= kSectionNum)
|
2017-07-05 08:57:14 +00:00
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "Too many NCA sections");
|
|
|
|
}
|
2017-07-06 10:55:58 +00:00
|
|
|
mSections.addElement(section);
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 10:55:58 +00:00
|
|
|
const fnd::List<crypto::aes::sAes128Key>& NcaHeader::getAesKeys() const
|
2017-07-05 08:57:14 +00:00
|
|
|
{
|
2017-07-05 15:38:14 +00:00
|
|
|
return mAesKeys;
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NcaHeader::addKey(const crypto::aes::sAes128Key & key)
|
|
|
|
{
|
2017-07-06 10:55:58 +00:00
|
|
|
if (mAesKeys.getSize() >= kAesKeyNum)
|
2017-07-05 08:57:14 +00:00
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "Too many NCA aes keys");
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:55:58 +00:00
|
|
|
mAesKeys.addElement(key);
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NcaHeader::clearVariables()
|
|
|
|
{
|
2017-07-05 15:38:14 +00:00
|
|
|
mBlockSize = 0;
|
|
|
|
mNcaSize = 0;
|
|
|
|
mProgramId = 0;
|
|
|
|
mUnk0 = 0;
|
|
|
|
mSections.clear();
|
|
|
|
mAesKeys.clear();
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 NcaHeader::blockNumToSize(u32 block_num) const
|
|
|
|
{
|
2017-07-05 15:38:14 +00:00
|
|
|
return block_num*mBlockSize;
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 NcaHeader::sizeToBlockNum(u64 real_size) const
|
|
|
|
{
|
2017-07-05 15:38:14 +00:00
|
|
|
return align(real_size, mBlockSize)/mBlockSize;
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 10:55:58 +00:00
|
|
|
bool NcaHeader::isEqual(const NcaHeader & other) const
|
|
|
|
{
|
|
|
|
return (mBlockSize == other.mBlockSize) \
|
|
|
|
&& (mNcaSize == other.mNcaSize) \
|
|
|
|
&& (mProgramId == other.mProgramId) \
|
|
|
|
&& (mUnk0 == other.mUnk0) \
|
|
|
|
&& (mSections == other.mSections) \
|
|
|
|
&& (mAesKeys == other.mAesKeys);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NcaHeader::copyFrom(const NcaHeader & other)
|
|
|
|
{
|
|
|
|
if (other.getSize())
|
|
|
|
{
|
|
|
|
importBinary(other.getBytes(), other.getSize());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->mBinaryBlob.clear();
|
|
|
|
mBlockSize = other.mBlockSize;
|
|
|
|
mNcaSize = other.mNcaSize;
|
|
|
|
mProgramId = other.mProgramId;
|
|
|
|
mUnk0 = other.mUnk0;
|
|
|
|
mSections = other.mSections;
|
|
|
|
mAesKeys = other.mAesKeys;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 08:57:14 +00:00
|
|
|
NcaHeader::NcaHeader()
|
|
|
|
{
|
|
|
|
clearVariables();
|
|
|
|
}
|
|
|
|
|
|
|
|
NcaHeader::NcaHeader(const NcaHeader & other)
|
|
|
|
{
|
2017-07-06 10:55:58 +00:00
|
|
|
copyFrom(other);
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
2017-07-15 08:28:01 +00:00
|
|
|
NcaHeader::NcaHeader(const u8 * bytes, size_t len)
|
2017-07-05 08:57:14 +00:00
|
|
|
{
|
2017-07-15 08:28:01 +00:00
|
|
|
importBinary(bytes, len);
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 10:55:58 +00:00
|
|
|
bool NcaHeader::operator==(const NcaHeader & other) const
|
|
|
|
{
|
|
|
|
return isEqual(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NcaHeader::operator!=(const NcaHeader & other) const
|
2017-07-06 03:30:27 +00:00
|
|
|
{
|
2017-07-06 10:55:58 +00:00
|
|
|
return !isEqual(other);
|
2017-07-06 03:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NcaHeader::operator=(const NcaHeader & other)
|
|
|
|
{
|
|
|
|
this->importBinary(other.getBytes(), other.getSize());
|
|
|
|
}
|
|
|
|
|
2017-07-05 08:57:14 +00:00
|
|
|
const u8 * NcaHeader::getBytes() const
|
|
|
|
{
|
2017-07-06 03:30:27 +00:00
|
|
|
return mBinaryBlob.getBytes();
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t NcaHeader::getSize() const
|
|
|
|
{
|
2017-07-06 03:30:27 +00:00
|
|
|
return mBinaryBlob.getSize();
|
2017-07-05 08:57:14 +00:00
|
|
|
}
|