nstool/lib/libhac/source/XciHeader.cpp

481 lines
11 KiB
C++
Raw Normal View History

2018-08-07 07:17:51 +00:00
#include <nn/hac/XciHeader.h>
2018-04-21 09:37:30 +00:00
2018-08-07 07:17:51 +00:00
nn::hac::XciHeader::XciHeader()
2018-06-24 08:18:54 +00:00
{
clear();
}
2018-08-07 07:17:51 +00:00
nn::hac::XciHeader::XciHeader(const XciHeader& other)
2018-04-21 09:37:30 +00:00
{
2018-06-24 08:18:54 +00:00
*this = other;
2018-04-21 09:37:30 +00:00
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::operator=(const XciHeader& other)
2018-04-21 09:37:30 +00:00
{
mRomAreaStartPage = other.mRomAreaStartPage;
mBackupAreaStartPage = other.mBackupAreaStartPage;
mKekIndex = other.mKekIndex;
mTitleKeyDecIndex = other.mTitleKeyDecIndex;
mRomSize = other.mRomSize;
mCardHeaderVersion = other.mCardHeaderVersion;
mFlags = other.mFlags;
mPackageId = other.mPackageId;
mValidDataEndPage = other.mValidDataEndPage;
mAesCbcIv = other.mAesCbcIv;
mPartitionFsHeaderAddress = other.mPartitionFsHeaderAddress;
mPartitionFsHeaderSize = other.mPartitionFsHeaderSize;
mPartitionFsHeaderHash = other.mPartitionFsHeaderHash;
mInitialDataHash = other.mInitialDataHash;
mSelSec = other.mSelSec;
mSelT1Key = other.mSelT1Key;
mSelKey = other.mSelKey;
mLimAreaPage = other.mLimAreaPage;
mFwVersion[0] = other.mFwVersion[0];
mFwVersion[1] = other.mFwVersion[1];
mAccCtrl1 = other.mAccCtrl1;
mWait1TimeRead = other.mWait1TimeRead;
mWait2TimeRead = other.mWait2TimeRead;
mWait1TimeWrite = other.mWait1TimeWrite;
mWait2TimeWrite = other.mWait2TimeWrite;
mFwMode = other.mFwMode;
mUppVersion = other.mUppVersion;
memcpy(mUppHash, other.mUppHash, xci::kUppHashLen);
mUppId = other.mUppId;
}
2018-08-07 07:17:51 +00:00
bool nn::hac::XciHeader::operator==(const XciHeader& other) const
2018-04-21 09:37:30 +00:00
{
return (mRomAreaStartPage == other.mRomAreaStartPage)
&& (mBackupAreaStartPage == other.mBackupAreaStartPage)
&& (mKekIndex == other.mKekIndex)
&& (mTitleKeyDecIndex == other.mTitleKeyDecIndex)
&& (mRomSize == other.mRomSize)
&& (mCardHeaderVersion == other.mCardHeaderVersion)
&& (mFlags == other.mFlags)
&& (mPackageId == other.mPackageId)
&& (mValidDataEndPage == other.mValidDataEndPage)
&& (mAesCbcIv == other.mAesCbcIv)
&& (mPartitionFsHeaderAddress == other.mPartitionFsHeaderAddress)
&& (mPartitionFsHeaderSize == other.mPartitionFsHeaderSize)
&& (mPartitionFsHeaderHash == other.mPartitionFsHeaderHash)
&& (mInitialDataHash == other.mInitialDataHash)
&& (mSelSec == other.mSelSec)
&& (mSelT1Key == other.mSelT1Key)
&& (mSelKey == other.mSelKey)
&& (mLimAreaPage == other.mLimAreaPage)
&& (mFwVersion[0] == other.mFwVersion[0])
&& (mFwVersion[1] == other.mFwVersion[1])
&& (mAccCtrl1 == other.mAccCtrl1)
&& (mWait1TimeRead == other.mWait1TimeRead)
&& (mWait2TimeRead == other.mWait2TimeRead)
&& (mWait1TimeWrite == other.mWait1TimeWrite)
&& (mWait2TimeWrite == other.mWait2TimeWrite)
&& (mFwMode == other.mFwMode)
&& (mUppVersion == other.mUppVersion)
&& (memcmp(mUppHash, other.mUppHash, xci::kUppHashLen) == 0)
&& (mUppId == other.mUppId);
2018-04-21 09:37:30 +00:00
}
2018-08-07 07:17:51 +00:00
bool nn::hac::XciHeader::operator!=(const XciHeader& other) const
2018-04-21 09:37:30 +00:00
{
2018-06-24 08:18:54 +00:00
return !(*this == other);
2018-04-21 09:37:30 +00:00
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::toBytes()
2018-04-21 09:37:30 +00:00
{
fnd::Exception(kModuleName, "exportBinary() not implemented");
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::fromBytes(const byte_t* data, size_t len)
2018-04-21 09:37:30 +00:00
{
// check input data size
if (len < sizeof(sXciHeader))
{
throw fnd::Exception(kModuleName, "XCI header size is too small");
}
// clear internal members
clear();
// allocate internal local binary copy
2018-06-24 08:18:54 +00:00
mRawBinary.alloc(sizeof(sXciHeader));
memcpy(mRawBinary.data(), data, mRawBinary.size());
2018-04-21 09:37:30 +00:00
// get sXciHeader ptr
2018-08-07 07:17:51 +00:00
const nn::hac::sXciHeader* hdr = (const nn::hac::sXciHeader*)mRawBinary.data();
2018-04-21 09:37:30 +00:00
// check XCI signature
if (hdr->st_magic.get() != xci::kXciStructMagic)
2018-04-21 09:37:30 +00:00
{
throw fnd::Exception(kModuleName, "XCI header corrupt");
}
mRomAreaStartPage = hdr->rom_area_start_page.get();
mBackupAreaStartPage = hdr->backup_area_start_page.get();
mKekIndex = hdr->key_flag & 7;
mTitleKeyDecIndex = (hdr->key_flag >> 4) & 7;
mRomSize = hdr->rom_size;
mCardHeaderVersion = hdr->card_header_version;
mFlags = hdr->flags;
mPackageId = hdr->package_id.get();
mValidDataEndPage = hdr->valid_data_end_page.get();
for (size_t i = 0; i < crypto::aes::kAesBlockSize; i++)
mAesCbcIv.iv[i] = hdr->aescbc_iv.iv[15-i];
mPartitionFsHeaderAddress = hdr->partition_fs_header_address.get();
mPartitionFsHeaderSize = hdr->partition_fs_header_size.get();
mPartitionFsHeaderHash = hdr->partition_fs_header_hash;
mInitialDataHash = hdr->initial_data_hash;
mSelSec = hdr->sel_sec.get();
mSelT1Key = hdr->sel_t1_key.get();
mSelKey = hdr->sel_key.get();
mLimAreaPage = hdr->lim_area.get();
// if decrypted
if (hdr->reserved_02[sizeof(hdr->reserved_02)-1] == 0x00 && hdr->reserved_02[sizeof(hdr->reserved_02)-2] == 0x00)
{
mFwVersion[xci::FWVER_MAJOR] = hdr->fw_version[xci::FWVER_MAJOR].get();
mFwVersion[xci::FWVER_MINOR] = hdr->fw_version[xci::FWVER_MINOR].get();
mAccCtrl1 = hdr->acc_ctrl_1.get();
mWait1TimeRead = hdr->wait_1_time_read.get();
mWait2TimeRead = hdr->wait_2_time_read.get();
mWait1TimeWrite = hdr->wait_1_time_write.get();
mWait2TimeWrite = hdr->wait_2_time_write.get();
mFwMode = hdr->fw_mode.get();
mUppVersion = hdr->upp_version.get();
memcpy(mUppHash, hdr->upp_hash, xci::kUppHashLen);
mUppId = hdr->upp_id.get();
}
}
2018-08-07 07:17:51 +00:00
const fnd::Vec<byte_t>& nn::hac::XciHeader::getBytes() const
2018-06-24 08:18:54 +00:00
{
return mRawBinary;
}
2018-04-21 09:37:30 +00:00
// variables
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::clear()
2018-04-21 09:37:30 +00:00
{
mRomAreaStartPage = 0;
mBackupAreaStartPage = 0;
mKekIndex = 0;
mTitleKeyDecIndex = 0;
mRomSize = 0;
mCardHeaderVersion = 0;
mFlags = 0;
mPackageId = 0;
mValidDataEndPage = 0;
memset(mAesCbcIv.iv, 0, sizeof(mAesCbcIv));
mPartitionFsHeaderAddress = 0;
mPartitionFsHeaderSize = 0;
memset(mPartitionFsHeaderHash.bytes, 0, sizeof(mPartitionFsHeaderHash));
memset(mInitialDataHash.bytes, 0, sizeof(mInitialDataHash));
mSelSec = 0;
mSelT1Key = 0;
mSelKey = 0;
mLimAreaPage = 0;
mFwVersion[0] = 0;
mFwVersion[1] = 0;
mAccCtrl1 = 0;
mWait1TimeRead = 0;
mWait2TimeRead = 0;
mWait1TimeWrite = 0;
mWait2TimeWrite = 0;
mFwMode = 0;
mUppVersion = 0;
memset(mUppHash, 0, xci::kUppHashLen);
mUppId = 0;
}
2018-08-07 07:17:51 +00:00
uint32_t nn::hac::XciHeader::getRomAreaStartPage() const
2018-04-21 09:37:30 +00:00
{
return mRomAreaStartPage;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setRomAreaStartPage(uint32_t startPage)
2018-04-21 09:37:30 +00:00
{
mRomAreaStartPage = startPage;
}
2018-08-07 07:17:51 +00:00
uint32_t nn::hac::XciHeader::getBackupAreaStartPage() const
2018-04-21 09:37:30 +00:00
{
return mBackupAreaStartPage;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setBackupAreaStartPage(uint32_t startPage)
2018-04-21 09:37:30 +00:00
{
mBackupAreaStartPage = startPage;
}
2018-08-07 07:17:51 +00:00
byte_t nn::hac::XciHeader::getKekIndex() const
2018-04-21 09:37:30 +00:00
{
return mKekIndex;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setKekIndex(byte_t kekIndex)
2018-04-21 09:37:30 +00:00
{
mKekIndex = kekIndex;
}
2018-08-07 07:17:51 +00:00
byte_t nn::hac::XciHeader::getTitleKeyDecIndex() const
2018-04-21 09:37:30 +00:00
{
return mTitleKeyDecIndex;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setTitleKeyDecIndex(byte_t index)
2018-04-21 09:37:30 +00:00
{
mTitleKeyDecIndex = index;
}
2018-08-07 07:17:51 +00:00
byte_t nn::hac::XciHeader::getRomSizeType() const
2018-04-21 09:37:30 +00:00
{
return mRomSize;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setRomSizeType(byte_t romSizeType)
2018-04-21 09:37:30 +00:00
{
mRomSize = romSizeType;
}
2018-08-07 07:17:51 +00:00
byte_t nn::hac::XciHeader::getCardHeaderVersion() const
2018-04-21 09:37:30 +00:00
{
return mCardHeaderVersion;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setCardHeaderVersion(byte_t version)
2018-04-21 09:37:30 +00:00
{
mCardHeaderVersion = version;
}
2018-08-07 07:17:51 +00:00
byte_t nn::hac::XciHeader::getFlags() const
2018-04-21 09:37:30 +00:00
{
return mFlags;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setFlags(byte_t flags)
2018-04-21 09:37:30 +00:00
{
mFlags = flags;
}
2018-08-07 07:17:51 +00:00
uint64_t nn::hac::XciHeader::getPackageId() const
2018-04-21 09:37:30 +00:00
{
return mPackageId;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setPackageId(uint64_t id)
2018-04-21 09:37:30 +00:00
{
mPackageId = id;
}
2018-08-07 07:17:51 +00:00
uint32_t nn::hac::XciHeader::getValidDataEndPage() const
2018-04-21 09:37:30 +00:00
{
return mValidDataEndPage;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setValidDataEndPage(uint32_t page)
2018-04-21 09:37:30 +00:00
{
mValidDataEndPage = page;
}
2018-08-07 07:17:51 +00:00
const crypto::aes::sAesIvCtr& nn::hac::XciHeader::getAesCbcIv() const
2018-04-21 09:37:30 +00:00
{
return mAesCbcIv;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setAesCbcIv(const crypto::aes::sAesIvCtr& iv)
2018-04-21 09:37:30 +00:00
{
mAesCbcIv = iv;
}
2018-08-07 07:17:51 +00:00
uint64_t nn::hac::XciHeader::getPartitionFsAddress() const
2018-04-21 09:37:30 +00:00
{
return mPartitionFsHeaderAddress;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setPartitionFsAddress(uint64_t address)
2018-04-21 09:37:30 +00:00
{
mPartitionFsHeaderAddress = address;
}
2018-08-07 07:17:51 +00:00
uint64_t nn::hac::XciHeader::getPartitionFsSize() const
2018-04-21 09:37:30 +00:00
{
return mPartitionFsHeaderSize;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setPartitionFsSize(uint64_t size)
2018-04-21 09:37:30 +00:00
{
mPartitionFsHeaderSize = size;
}
2018-08-07 07:17:51 +00:00
const crypto::sha::sSha256Hash& nn::hac::XciHeader::getPartitionFsHash() const
2018-04-21 09:37:30 +00:00
{
return mPartitionFsHeaderHash;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setPartitionFsHash(const crypto::sha::sSha256Hash& hash)
2018-04-21 09:37:30 +00:00
{
mPartitionFsHeaderHash = hash;
}
2018-08-07 07:17:51 +00:00
const crypto::sha::sSha256Hash& nn::hac::XciHeader::getInitialDataHash() const
2018-04-21 09:37:30 +00:00
{
return mInitialDataHash;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setInitialDataHash(const crypto::sha::sSha256Hash& hash)
2018-04-21 09:37:30 +00:00
{
mInitialDataHash = hash;
}
2018-08-07 07:17:51 +00:00
uint32_t nn::hac::XciHeader::getSelSec() const
2018-04-21 09:37:30 +00:00
{
return mSelSec;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setSelSec(uint32_t sel_sec)
2018-04-21 09:37:30 +00:00
{
mSelSec = sel_sec;
}
2018-08-07 07:17:51 +00:00
uint32_t nn::hac::XciHeader::getSelT1Key() const
2018-04-21 09:37:30 +00:00
{
return mSelT1Key;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setSelT1Key(uint32_t sel_t1_key)
2018-04-21 09:37:30 +00:00
{
mSelT1Key = sel_t1_key;
}
2018-08-07 07:17:51 +00:00
uint32_t nn::hac::XciHeader::getSelKey() const
2018-04-21 09:37:30 +00:00
{
return mSelKey;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setSelKey(uint32_t sel_key)
2018-04-21 09:37:30 +00:00
{
mSelKey = sel_key;
}
2018-08-07 07:17:51 +00:00
uint32_t nn::hac::XciHeader::getLimAreaPage() const
2018-04-21 09:37:30 +00:00
{
return mLimAreaPage;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setLimAreaPage(uint32_t page)
2018-04-21 09:37:30 +00:00
{
mLimAreaPage = page;
}
2018-08-07 07:17:51 +00:00
uint32_t nn::hac::XciHeader::getFwVerMajor() const
2018-04-21 09:37:30 +00:00
{
return mFwVersion[xci::FWVER_MAJOR];
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setFwVerMajor(uint32_t ver)
2018-04-21 09:37:30 +00:00
{
mFwVersion[xci::FWVER_MAJOR] = ver;
}
2018-08-07 07:17:51 +00:00
uint32_t nn::hac::XciHeader::getFwVerMinor() const
2018-04-21 09:37:30 +00:00
{
return mFwVersion[xci::FWVER_MINOR];
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setFwVerMinor(uint32_t ver)
2018-04-21 09:37:30 +00:00
{
mFwVersion[xci::FWVER_MINOR] = ver;
}
2018-08-07 07:17:51 +00:00
uint32_t nn::hac::XciHeader::getAccCtrl1() const
2018-04-21 09:37:30 +00:00
{
return mAccCtrl1;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setAccCtrl1(uint32_t acc_ctrl_1)
2018-04-21 09:37:30 +00:00
{
mAccCtrl1 = acc_ctrl_1;
}
2018-08-07 07:17:51 +00:00
uint32_t nn::hac::XciHeader::getWait1TimeRead() const
2018-04-21 09:37:30 +00:00
{
return mWait1TimeRead;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setWait1TimeRead(uint32_t seconds)
2018-04-21 09:37:30 +00:00
{
mWait1TimeRead = seconds;
}
2018-08-07 07:17:51 +00:00
uint32_t nn::hac::XciHeader::getWait2TimeRead() const
2018-04-21 09:37:30 +00:00
{
return mWait2TimeRead;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setWait2TimeRead(uint32_t seconds)
2018-04-21 09:37:30 +00:00
{
mWait2TimeRead = seconds;
}
2018-08-07 07:17:51 +00:00
uint32_t nn::hac::XciHeader::getWait1TimeWrite() const
2018-04-21 09:37:30 +00:00
{
return mWait1TimeWrite;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setWait1TimeWrite(uint32_t seconds)
2018-04-21 09:37:30 +00:00
{
mWait1TimeWrite = seconds;
}
2018-08-07 07:17:51 +00:00
uint32_t nn::hac::XciHeader::getWait2TimeWrite() const
2018-04-21 09:37:30 +00:00
{
return mWait2TimeWrite;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setWait2TimeWrite(uint32_t seconds)
2018-04-21 09:37:30 +00:00
{
mWait2TimeWrite = seconds;
}
2018-08-07 07:17:51 +00:00
uint32_t nn::hac::XciHeader::getFwMode() const
2018-04-21 09:37:30 +00:00
{
return mFwMode;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setFwMode(uint32_t fw_mode)
2018-04-21 09:37:30 +00:00
{
mFwMode = fw_mode;
}
2018-08-07 07:17:51 +00:00
uint32_t nn::hac::XciHeader::getUppVersion() const
2018-04-21 09:37:30 +00:00
{
return mUppVersion;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setUppVersion(uint32_t version)
2018-04-21 09:37:30 +00:00
{
mUppVersion = version;
}
2018-08-07 07:17:51 +00:00
const byte_t* nn::hac::XciHeader::getUppHash() const
2018-04-21 09:37:30 +00:00
{
return mUppHash;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setUppHash(const byte_t* hash)
2018-04-21 09:37:30 +00:00
{
memcpy(mUppHash, hash, xci::kUppHashLen);
}
2018-08-07 07:17:51 +00:00
uint64_t nn::hac::XciHeader::getUppId() const
2018-04-21 09:37:30 +00:00
{
return mUppId;
}
2018-08-07 07:17:51 +00:00
void nn::hac::XciHeader::setUppId(uint64_t id)
2018-04-21 09:37:30 +00:00
{
mUppId = id;
}