2018-10-22 13:30:49 +00:00
|
|
|
#include <nn/hac/Meta.h>
|
2017-07-17 08:21:39 +00:00
|
|
|
|
2018-06-27 05:03:46 +00:00
|
|
|
#include <fnd/SimpleTextOutput.h>
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
nn::hac::Meta::Meta()
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
nn::hac::Meta::Meta(const Meta & other) :
|
|
|
|
Meta()
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
2018-06-24 08:18:54 +00:00
|
|
|
*this = other;
|
2017-07-17 08:21:39 +00:00
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
void nn::hac::Meta::operator=(const Meta & other)
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
2018-06-29 06:45:36 +00:00
|
|
|
mRawBinary = other.mRawBinary;
|
|
|
|
mInstructionType = other.mInstructionType;
|
|
|
|
mProcAddressSpaceType = other.mProcAddressSpaceType;
|
|
|
|
mMainThreadPriority = other.mMainThreadPriority;
|
|
|
|
mMainThreadCpuId = other.mMainThreadCpuId;
|
|
|
|
mVersion = other.mVersion;
|
|
|
|
mMainThreadStackSize = other.mMainThreadStackSize;
|
|
|
|
mName = other.mName;
|
|
|
|
mProductCode = other.mProductCode;
|
|
|
|
mAci = other.mAci;
|
|
|
|
mAcid = other.mAcid;
|
2017-07-17 08:21:39 +00:00
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
bool nn::hac::Meta::operator==(const Meta & other) const
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
2018-06-29 06:45:36 +00:00
|
|
|
return (mInstructionType == other.mInstructionType) \
|
|
|
|
&& (mProcAddressSpaceType == other.mProcAddressSpaceType) \
|
|
|
|
&& (mMainThreadPriority == other.mMainThreadPriority) \
|
|
|
|
&& (mMainThreadCpuId == other.mMainThreadCpuId) \
|
|
|
|
&& (mVersion == other.mVersion) \
|
|
|
|
&& (mMainThreadStackSize == other.mMainThreadStackSize) \
|
|
|
|
&& (mName == other.mName) \
|
|
|
|
&& (mProductCode == other.mProductCode) \
|
2018-06-24 08:18:54 +00:00
|
|
|
&& (mAci == other.mAci) \
|
|
|
|
&& (mAcid == other.mAcid);
|
2017-07-17 08:21:39 +00:00
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
bool nn::hac::Meta::operator!=(const Meta & other) const
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
2018-06-24 08:18:54 +00:00
|
|
|
return !(*this == other);
|
2017-07-17 08:21:39 +00:00
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
void nn::hac::Meta::toBytes()
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
2018-06-29 06:45:36 +00:00
|
|
|
if (mAcid.getBytes().size() == 0)
|
|
|
|
mAcid.toBytes();
|
|
|
|
|
|
|
|
if (mAci.getBytes().size() == 0)
|
|
|
|
mAci.toBytes();
|
|
|
|
|
|
|
|
|
|
|
|
// determine section layout
|
|
|
|
struct sLayout {
|
|
|
|
uint32_t offset, size;
|
|
|
|
} acid, aci;
|
|
|
|
|
2018-10-07 04:31:41 +00:00
|
|
|
acid.offset = (uint32_t)align(sizeof(sMetaHeader), meta::kSectionAlignSize);
|
2018-06-29 06:45:36 +00:00
|
|
|
acid.size = (uint32_t)mAcid.getBytes().size();
|
2018-10-07 04:31:41 +00:00
|
|
|
aci.offset = (uint32_t)(acid.offset + align(acid.size, meta::kSectionAlignSize));
|
2018-06-29 06:45:36 +00:00
|
|
|
aci.size = (uint32_t)mAci.getBytes().size();
|
|
|
|
|
|
|
|
|
|
|
|
// get total size
|
2018-10-07 04:31:41 +00:00
|
|
|
size_t total_size = _MAX(_MAX(acid.offset + acid.size, aci.offset + aci.size), align(sizeof(sMetaHeader), meta::kSectionAlignSize));
|
2018-06-29 06:45:36 +00:00
|
|
|
|
|
|
|
mRawBinary.alloc(total_size);
|
2018-10-07 04:31:41 +00:00
|
|
|
sMetaHeader* hdr = (sMetaHeader*)mRawBinary.data();
|
2018-06-29 06:45:36 +00:00
|
|
|
|
|
|
|
// set type
|
2018-10-07 04:31:41 +00:00
|
|
|
hdr->st_magic = meta::kMetaStructMagic;
|
2018-06-29 06:45:36 +00:00
|
|
|
|
|
|
|
// set variables
|
|
|
|
byte_t flag = ((byte_t)(mInstructionType & 1) | (byte_t)((mProcAddressSpaceType & 3) << 1)) & 0xf;
|
|
|
|
hdr->flags = flag;
|
|
|
|
hdr->main_thread_priority = mMainThreadPriority;
|
|
|
|
hdr->main_thread_cpu_id = mMainThreadCpuId;
|
|
|
|
hdr->version = mVersion;
|
|
|
|
hdr->main_thread_stack_size = mMainThreadStackSize;
|
2018-10-07 04:31:41 +00:00
|
|
|
strncpy(hdr->name, mName.c_str(), meta::kNameMaxLen);
|
|
|
|
strncpy(hdr->product_code, mProductCode.c_str(), meta::kProductCodeMaxLen);
|
2018-06-29 06:45:36 +00:00
|
|
|
|
|
|
|
// set offset/size
|
|
|
|
hdr->aci.offset = aci.offset;
|
|
|
|
hdr->aci.size = aci.size;
|
|
|
|
hdr->acid.offset = acid.offset;
|
|
|
|
hdr->acid.size = acid.size;
|
|
|
|
|
|
|
|
// write aci & acid
|
|
|
|
if (mAci.getBytes().size() > 0)
|
|
|
|
{
|
|
|
|
memcpy(mRawBinary.data() + aci.offset, mAci.getBytes().data(), mAci.getBytes().size());
|
|
|
|
}
|
|
|
|
if (mAcid.getBytes().size() > 0)
|
|
|
|
{
|
|
|
|
memcpy(mRawBinary.data() + acid.offset, mAcid.getBytes().data(), mAcid.getBytes().size());
|
|
|
|
}
|
2017-07-17 08:21:39 +00:00
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
void nn::hac::Meta::fromBytes(const byte_t* data, size_t len)
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
2018-06-29 06:45:36 +00:00
|
|
|
// check size
|
2018-10-07 04:31:41 +00:00
|
|
|
if (len < sizeof(sMetaHeader))
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
2018-10-07 04:31:41 +00:00
|
|
|
throw fnd::Exception(kModuleName, "META binary is too small");
|
2018-06-29 06:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// clear variables
|
2017-07-17 08:21:39 +00:00
|
|
|
clear();
|
|
|
|
|
2018-06-29 06:45:36 +00:00
|
|
|
// save a copy of the header
|
2018-10-07 04:31:41 +00:00
|
|
|
sMetaHeader hdr;
|
|
|
|
memcpy((void*)&hdr, data, sizeof(sMetaHeader));
|
2018-06-29 06:45:36 +00:00
|
|
|
|
|
|
|
// check magic
|
2018-10-07 04:31:41 +00:00
|
|
|
if (hdr.st_magic.get() != meta::kMetaStructMagic)
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
2018-10-07 04:31:41 +00:00
|
|
|
throw fnd::Exception(kModuleName, "META header corrupt (unrecognised struct signature)");
|
2018-06-29 06:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// save variables
|
|
|
|
byte_t flag = hdr.flags & 0xf;
|
2018-10-07 04:31:41 +00:00
|
|
|
mInstructionType = (meta::InstructionType)(flag & 1);
|
|
|
|
mProcAddressSpaceType = (meta::ProcAddrSpaceType)((flag >> 1) & 3);
|
2018-06-29 06:45:36 +00:00
|
|
|
mMainThreadPriority = hdr.main_thread_priority;
|
|
|
|
mMainThreadCpuId = hdr.main_thread_cpu_id;
|
|
|
|
mVersion = hdr.version.get();
|
|
|
|
mMainThreadStackSize = hdr.main_thread_stack_size.get();
|
2018-10-07 04:31:41 +00:00
|
|
|
mName = std::string(hdr.name, _MIN(strlen(hdr.name), meta::kNameMaxLen));
|
|
|
|
mProductCode = std::string(hdr.product_code, _MIN(strlen(hdr.product_code), meta::kProductCodeMaxLen));
|
2018-06-29 06:45:36 +00:00
|
|
|
|
|
|
|
// total size
|
2018-10-07 04:31:41 +00:00
|
|
|
size_t total_size = _MAX(_MAX(hdr.acid.offset.get() + hdr.acid.size.get(), hdr.aci.offset.get() + hdr.aci.size.get()), sizeof(sMetaHeader));
|
2017-07-17 08:21:39 +00:00
|
|
|
|
|
|
|
// check size
|
2018-06-29 06:45:36 +00:00
|
|
|
if (total_size > len)
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
2018-10-07 04:31:41 +00:00
|
|
|
throw fnd::Exception(kModuleName, "META binary too small");
|
2017-07-17 08:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// save local copy
|
2018-06-29 06:45:36 +00:00
|
|
|
mRawBinary.alloc(total_size);
|
2018-06-24 08:18:54 +00:00
|
|
|
memcpy(mRawBinary.data(), data, mRawBinary.size());
|
2017-07-17 08:21:39 +00:00
|
|
|
|
|
|
|
// import Aci/Acid
|
2018-06-29 06:45:36 +00:00
|
|
|
if (hdr.aci.size.get())
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
2018-06-29 06:45:36 +00:00
|
|
|
mAci.fromBytes(mRawBinary.data() + hdr.aci.offset.get(), hdr.aci.size.get());
|
2017-07-17 08:21:39 +00:00
|
|
|
}
|
2018-06-29 06:45:36 +00:00
|
|
|
if (hdr.acid.size.get())
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
2018-06-29 06:45:36 +00:00
|
|
|
mAcid.fromBytes(mRawBinary.data() + hdr.acid.offset.get(), hdr.acid.size.get());
|
2018-06-24 08:18:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
const fnd::Vec<byte_t>& nn::hac::Meta::getBytes() const
|
2018-06-24 08:18:54 +00:00
|
|
|
{
|
|
|
|
return mRawBinary;
|
2017-07-17 08:21:39 +00:00
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
void nn::hac::Meta::clear()
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
2018-06-29 06:45:36 +00:00
|
|
|
mRawBinary.clear();
|
2018-10-07 04:31:41 +00:00
|
|
|
mInstructionType = meta::INSTR_64BIT;
|
|
|
|
mProcAddressSpaceType = meta::ADDR_SPACE_64BIT;
|
2018-06-29 06:45:36 +00:00
|
|
|
mMainThreadPriority = 0;
|
|
|
|
mMainThreadCpuId = 0;
|
|
|
|
mVersion = 0;
|
|
|
|
mMainThreadStackSize = 0;
|
|
|
|
mName.clear();
|
|
|
|
mProductCode.clear();
|
2017-07-17 08:21:39 +00:00
|
|
|
mAci.clear();
|
|
|
|
mAcid.clear();
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
nn::hac::meta::InstructionType nn::hac::Meta::getInstructionType() const
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
|
|
|
return mInstructionType;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
void nn::hac::Meta::setInstructionType(meta::InstructionType type)
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
|
|
|
mInstructionType = type;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
nn::hac::meta::ProcAddrSpaceType nn::hac::Meta::getProcAddressSpaceType() const
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
|
|
|
return mProcAddressSpaceType;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
void nn::hac::Meta::setProcAddressSpaceType(meta::ProcAddrSpaceType type)
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
|
|
|
mProcAddressSpaceType = type;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
byte_t nn::hac::Meta::getMainThreadPriority() const
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
|
|
|
return mMainThreadPriority;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
void nn::hac::Meta::setMainThreadPriority(byte_t priority)
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
2018-10-07 04:31:41 +00:00
|
|
|
if (priority > meta::kMaxPriority)
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "Illegal main thread priority (range 0-63)");
|
|
|
|
}
|
|
|
|
|
|
|
|
mMainThreadPriority = priority;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
byte_t nn::hac::Meta::getMainThreadCpuId() const
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
|
|
|
return mMainThreadCpuId;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
void nn::hac::Meta::setMainThreadCpuId(byte_t core_num)
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
|
|
|
mMainThreadCpuId = core_num;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
uint32_t nn::hac::Meta::getVersion() const
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
|
|
|
return mVersion;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
void nn::hac::Meta::setVersion(uint32_t version)
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
|
|
|
mVersion = version;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
uint32_t nn::hac::Meta::getMainThreadStackSize() const
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
|
|
|
return mMainThreadStackSize;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
void nn::hac::Meta::setMainThreadStackSize(uint32_t size)
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
|
|
|
mMainThreadStackSize = size;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
const std::string & nn::hac::Meta::getName() const
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
|
|
|
return mName;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
void nn::hac::Meta::setName(const std::string & name)
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
2018-10-07 04:31:41 +00:00
|
|
|
if (name.length() > meta::kNameMaxLen)
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "Name is too long");
|
|
|
|
}
|
|
|
|
|
|
|
|
mName = name;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
const std::string & nn::hac::Meta::getProductCode() const
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
|
|
|
return mProductCode;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
void nn::hac::Meta::setProductCode(const std::string & product_code)
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
2018-10-07 04:31:41 +00:00
|
|
|
if (product_code.length() > meta::kProductCodeMaxLen)
|
2018-06-29 06:45:36 +00:00
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "Product Code is too long");
|
|
|
|
}
|
|
|
|
|
|
|
|
mProductCode = product_code;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
const nn::hac::AccessControlInfo & nn::hac::Meta::getAci() const
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
|
|
|
return mAci;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
void nn::hac::Meta::setAci(const AccessControlInfo & aci)
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
|
|
|
mAci = aci;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
const nn::hac::AccessControlInfoDesc & nn::hac::Meta::getAcid() const
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
|
|
|
return mAcid;
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:30:49 +00:00
|
|
|
void nn::hac::Meta::setAcid(const AccessControlInfoDesc & acid)
|
2017-07-17 08:21:39 +00:00
|
|
|
{
|
|
|
|
mAcid = acid;
|
2018-06-24 08:18:54 +00:00
|
|
|
}
|