nstool/lib/libnx/source/NcaHeader.cpp

306 lines
6.6 KiB
C++
Raw Normal View History

#include <nx/NcaHeader.h>
2017-07-05 08:57:14 +00:00
2017-07-06 11:17:21 +00:00
using namespace nx;
2017-07-05 08:57:14 +00:00
void NcaHeader::exportBinary()
{
mBinaryBlob.alloc(sizeof(sNcaHeader));
sNcaHeader* hdr = (sNcaHeader*)mBinaryBlob.getBytes();
hdr->set_signature(kNcaSig.c_str());
2017-07-18 14:17:32 +00:00
hdr->set_distribution_type(mDistributionType);
hdr->set_content_type(mContentType);
hdr->set_key_generation(mEncryptionType);
hdr->set_key_area_encryption_key_index(mKeyIndex);
hdr->set_nca_size(mNcaSize);
hdr->set_program_id(mProgramId);
2017-07-18 14:17:32 +00:00
hdr->set_content_index(mContentIndex);
hdr->set_sdk_addon_version(mSdkAddonVersion);
// TODO: properly reconstruct NCA layout? atm in hands of user
for (size_t i = 0; i < mSections.getSize(); i++)
{
// determine section index
u8 section = mSections.getSize() - 1 - i;
hdr->section(section).set_start(sizeToBlockNum(mSections[i].offset));
hdr->section(section).set_end(sizeToBlockNum(mSections[i].offset) + sizeToBlockNum(mSections[i].size));
2017-07-18 14:17:32 +00:00
hdr->section(section).set_enabled(1);
hdr->section_hash(section) = mSections[i].hash;
}
for (size_t i = 0; i < kAesKeyNum; i++)
{
2017-07-18 14:17:32 +00:00
hdr->enc_aes_key(i) = mEncAesKeys[i];
}
2017-07-05 08:57:14 +00:00
}
void NcaHeader::importBinary(const u8 * bytes, size_t len)
2017-07-05 08:57:14 +00:00
{
if (len < sizeof(sNcaHeader))
{
throw fnd::Exception(kModuleName, "NCA header size is too small");
}
clear();
mBinaryBlob.alloc(sizeof(sNcaHeader));
memcpy(mBinaryBlob.getBytes(), bytes, sizeof(sNcaHeader));
2017-07-05 08:57:14 +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-18 14:17:32 +00:00
mDistributionType = (DistributionType)hdr->distribution_type();
mContentType = (ContentType)hdr->content_type();
mEncryptionType = (EncryptionType)hdr->key_generation();
mKeyIndex = (EncryptionKeyIndex)hdr->key_area_encryption_key_index();
mNcaSize = hdr->nca_size();
mProgramId = hdr->program_id();
2017-07-18 14:17:32 +00:00
mContentIndex = hdr->content_index();
mSdkAddonVersion = hdr->sdk_addon_version();
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-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-18 14:17:32 +00:00
EncryptionType encType = mEncryptionType;
if (encType == CRYPT_AUTO)
{
if (mContentType == TYPE_PROGRAM && section == SECTION_LOGO)
{
encType = CRYPT_NONE;
}
else
{
encType = CRYPT_AESCTR;
}
}
2017-07-05 08:57:14 +00:00
// add high level struct
2017-07-18 14:17:32 +00:00
mSections.addElement({ blockNumToSize(hdr->section(section).start()), blockNumToSize(hdr->section(section).end() - hdr->section(section).start()), encType, hdr->section_hash(section) });
2017-07-05 08:57:14 +00:00
}
for (size_t i = 0; i < kAesKeyNum; i++)
{
2017-07-18 14:17:32 +00:00
mEncAesKeys.addElement(hdr->enc_aes_key(i));
2017-07-05 08:57:14 +00:00
}
}
void nx::NcaHeader::clear()
{
2017-07-18 14:17:32 +00:00
mDistributionType = DIST_DOWNLOAD;
mContentType = TYPE_PROGRAM;
mEncryptionType = CRYPT_AUTO;
mKeyIndex = KEY_DEFAULT;
mNcaSize = 0;
mProgramId = 0;
2017-07-18 14:17:32 +00:00
mContentIndex = 0;
mSdkAddonVersion = 0;
mSections.clear();
2017-07-18 14:17:32 +00:00
mEncAesKeys.clear();
}
nx::NcaHeader::DistributionType nx::NcaHeader::getDistributionType() const
{
return mDistributionType;
}
void nx::NcaHeader::setDistributionType(DistributionType type)
{
mDistributionType = type;
}
nx::NcaHeader::ContentType nx::NcaHeader::getContentType() const
{
return mContentType;
}
void nx::NcaHeader::setContentType(ContentType type)
{
mContentType = type;
}
nx::NcaHeader::EncryptionType nx::NcaHeader::getEncryptionType() const
{
return mEncryptionType;
}
void nx::NcaHeader::setEncryptionType(EncryptionType type)
{
mEncryptionType = type;
}
nx::NcaHeader::EncryptionKeyIndex nx::NcaHeader::getKeyIndex() const
{
return mKeyIndex;
}
void nx::NcaHeader::setKeyIndex(EncryptionKeyIndex index)
{
mKeyIndex = index;
}
2017-07-05 08:57:14 +00:00
u64 NcaHeader::getNcaSize() const
{
return mNcaSize;
2017-07-05 08:57:14 +00:00
}
void NcaHeader::setNcaSize(u64 size)
{
mNcaSize = size;
2017-07-05 08:57:14 +00:00
}
u64 NcaHeader::getProgramId() const
{
return mProgramId;
2017-07-05 08:57:14 +00:00
}
void NcaHeader::setProgramId(u64 program_id)
{
mProgramId = program_id;
2017-07-05 08:57:14 +00:00
}
2017-07-18 14:17:32 +00:00
u32 nx::NcaHeader::getContentIndex() const
{
return mContentIndex;
}
void nx::NcaHeader::setContentIndex(u32 index)
{
mContentIndex = index;
}
u32 nx::NcaHeader::getSdkAddonVersion() const
{
return mSdkAddonVersion;
}
void nx::NcaHeader::setSdkAddonVersion(u32 version)
2017-07-05 08:57:14 +00:00
{
2017-07-18 14:17:32 +00:00
mSdkAddonVersion = version;
2017-07-05 08:57:14 +00:00
}
const fnd::List<NcaHeader::sSection>& NcaHeader::getSections() const
2017-07-05 08:57:14 +00:00
{
return mSections;
2017-07-05 08:57:14 +00:00
}
void NcaHeader::addSection(const sSection & section)
{
if (mSections.getSize() >= kSectionNum)
2017-07-05 08:57:14 +00:00
{
throw fnd::Exception(kModuleName, "Too many NCA sections");
}
mSections.addElement(section);
2017-07-05 08:57:14 +00:00
}
2017-07-18 14:17:32 +00:00
const fnd::List<crypto::aes::sAes128Key>& NcaHeader::getEncAesKeys() const
2017-07-05 08:57:14 +00:00
{
2017-07-18 14:17:32 +00:00
return mEncAesKeys;
2017-07-05 08:57:14 +00:00
}
2017-07-18 14:17:32 +00:00
void NcaHeader::addEncAesKey(const crypto::aes::sAes128Key & key)
2017-07-05 08:57:14 +00:00
{
2017-07-18 14:17:32 +00:00
if (mEncAesKeys.getSize() >= kAesKeyNum)
2017-07-05 08:57:14 +00:00
{
throw fnd::Exception(kModuleName, "Too many NCA aes keys");
}
2017-07-18 14:17:32 +00:00
mEncAesKeys.addElement(key);
2017-07-05 08:57:14 +00:00
}
u64 NcaHeader::blockNumToSize(u32 block_num) const
{
2017-07-18 14:17:32 +00:00
return block_num*kBlockSize;
2017-07-05 08:57:14 +00:00
}
u32 NcaHeader::sizeToBlockNum(u64 real_size) const
{
2017-07-18 14:17:32 +00:00
return align(real_size, kBlockSize)/kBlockSize;
2017-07-05 08:57:14 +00:00
}
bool NcaHeader::isEqual(const NcaHeader & other) const
{
2017-07-18 14:17:32 +00:00
return (mDistributionType == other.mDistributionType) \
&& (mContentType == other.mContentType) \
&& (mEncryptionType == other.mEncryptionType) \
&& (mKeyIndex == other.mKeyIndex) \
&& (mNcaSize == other.mNcaSize) \
&& (mProgramId == other.mProgramId) \
2017-07-18 14:17:32 +00:00
&& (mContentIndex == other.mContentIndex) \
&& (mSdkAddonVersion == other.mSdkAddonVersion) \
&& (mSections == other.mSections) \
2017-07-18 14:17:32 +00:00
&& (mEncAesKeys == other.mEncAesKeys);
}
void NcaHeader::copyFrom(const NcaHeader & other)
{
if (other.getSize())
{
importBinary(other.getBytes(), other.getSize());
}
else
{
2017-07-18 14:17:32 +00:00
mBinaryBlob.clear();
mDistributionType = other.mDistributionType;
mContentType = other.mContentType;
mEncryptionType = other.mEncryptionType;
mKeyIndex = other.mKeyIndex;
mNcaSize = other.mNcaSize;
mProgramId = other.mProgramId;
2017-07-18 14:17:32 +00:00
mContentIndex = other.mContentIndex;
mSdkAddonVersion = other.mSdkAddonVersion;
mSections = other.mSections;
2017-07-18 14:17:32 +00:00
mEncAesKeys = other.mEncAesKeys;
}
}
2017-07-05 08:57:14 +00:00
NcaHeader::NcaHeader()
{
clear();
2017-07-05 08:57:14 +00:00
}
NcaHeader::NcaHeader(const NcaHeader & other)
{
copyFrom(other);
2017-07-05 08:57:14 +00:00
}
NcaHeader::NcaHeader(const u8 * bytes, size_t len)
2017-07-05 08:57:14 +00:00
{
importBinary(bytes, len);
2017-07-05 08:57:14 +00:00
}
bool NcaHeader::operator==(const NcaHeader & other) const
{
return isEqual(other);
}
bool NcaHeader::operator!=(const NcaHeader & other) const
{
return !isEqual(other);
}
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
{
return mBinaryBlob.getBytes();
2017-07-05 08:57:14 +00:00
}
size_t NcaHeader::getSize() const
{
return mBinaryBlob.getSize();
2017-07-05 08:57:14 +00:00
}