[nx] Move structs and consts from NpdmHeader to npdm.h

This commit is contained in:
jakcron 2018-04-21 17:36:09 +08:00
parent c02c5650c0
commit fac2753afd
3 changed files with 119 additions and 105 deletions

View file

@ -3,6 +3,7 @@
#include <fnd/types.h>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialiseableBinary.h>
#include <nx/npdm.h>
namespace nx
{
@ -10,19 +11,6 @@ namespace nx
public fnd::ISerialiseableBinary
{
public:
enum InstructionType
{
INSTR_32BIT,
INSTR_64BIT,
};
enum ProcAddrSpaceType
{
ADDR_SPACE_64BIT = 1,
ADDR_SPACE_32BIT,
ADDR_SPACE_32BIT_NO_RESERVED,
};
struct sSection
{
size_t offset;
@ -66,11 +54,11 @@ namespace nx
void clear();
size_t getNpdmSize() const;
InstructionType getInstructionType() const;
void setInstructionType(InstructionType type);
npdm::InstructionType getInstructionType() const;
void setInstructionType(npdm::InstructionType type);
ProcAddrSpaceType getProcAddressSpaceType() const;
void setProcAddressSpaceType(ProcAddrSpaceType type);
npdm::ProcAddrSpaceType getProcAddressSpaceType() const;
void setProcAddressSpaceType(npdm::ProcAddrSpaceType type);
byte_t getMainThreadPriority() const;
void setMainThreadPriority(byte_t priority);
@ -97,81 +85,13 @@ namespace nx
void setAcidSize(size_t size);
private:
const std::string kModuleName = "NPDM_HEADER";
const std::string kNpdmStructSig = "META";
static const size_t kNameMaxLen = 0x10;
static const size_t kProductCodeMaxLen = 0x10;
static const uint32_t kMaxPriority = BIT(6) -1 ;
static const size_t kNpdmAlignSize = 0x10;
#pragma pack (push, 1)
struct sNpdmHeader
{
private:
byte_t signature_[4]; // be"META"
byte_t reserved_0[8];
byte_t flags_;
byte_t reserved_1;
byte_t main_thread_priority_; // 0-63 inclusive
byte_t main_thread_cpu_id_;
byte_t reserved_2[8];
uint32_t version_;
uint32_t main_thread_stack_size_; // default 4096
byte_t name_[kNameMaxLen]; // important
byte_t product_code_[kProductCodeMaxLen]; // can be empty
byte_t reserved_3[48];
// Access Control Info
struct sNpdmSection
{
private:
uint32_t offset_;
uint32_t size_;
public:
uint32_t offset() const { return le_word(offset_); }
void set_offset(uint32_t offset) { offset_ = le_word(offset); }
uint32_t size() const { return le_word(size_); }
void set_size(uint32_t size) { size_ = le_word(size); }
} aci_, acid_;
public:
const char* signature() const { return (const char*)signature_; }
void set_signature(const char* signature) { memcpy(signature_, signature, 4); }
byte_t flags() const { return flags_; }
void set_flags(byte_t flags) { flags_ = flags; }
byte_t main_thread_priority() const { return main_thread_priority_; }
void set_main_thread_priority(byte_t priority) { main_thread_priority_ = priority; }
byte_t main_thread_cpu_id() const { return main_thread_cpu_id_; }
void set_main_thread_cpu_id(byte_t cpu_id) { main_thread_cpu_id_ = cpu_id; }
uint32_t version() const { return le_word(version_); }
void set_version(uint32_t version) { version_ = le_word(version); }
uint32_t main_thread_stack_size() const { return le_word(main_thread_stack_size_); }
void set_main_thread_stack_size(uint32_t size) { main_thread_stack_size_ = le_word(size); }
const char* name() const { return (const char*)name_; }
void set_name(const char* name) { strncpy((char*)name_, name, kNameMaxLen); }
const char* product_code() const { return (const char*)product_code_; }
void set_product_code(const char* product_code) { strncpy((char*)product_code_, product_code, kProductCodeMaxLen); }
const sNpdmSection& aci() const { return aci_; }
sNpdmSection& aci() { return aci_; }
const sNpdmSection& acid() const { return acid_; }
sNpdmSection& acid() { return acid_; }
};
#pragma pack (pop)
// raw binary
fnd::MemoryBlob mBinaryBlob;
// variables
InstructionType mInstructionType;
ProcAddrSpaceType mProcAddressSpaceType;
npdm::InstructionType mInstructionType;
npdm::ProcAddrSpaceType mProcAddressSpaceType;
byte_t mMainThreadPriority;
byte_t mMainThreadCpuId;
uint32_t mVersion;

View file

@ -0,0 +1,94 @@
#pragma once
#include <string>
#include <fnd/types.h>
#include <crypto/aes.h>
#include <crypto/sha.h>
#include <fnd/ISerialiseableBinary.h>
namespace nx
{
namespace npdm
{
const std::string kNpdmStructSig = "META";
static const size_t kNameMaxLen = 0x10;
static const size_t kProductCodeMaxLen = 0x10;
static const uint32_t kMaxPriority = BIT(6) -1 ;
static const size_t kNpdmAlignSize = 0x10;
enum InstructionType
{
INSTR_32BIT,
INSTR_64BIT,
};
enum ProcAddrSpaceType
{
ADDR_SPACE_64BIT = 1,
ADDR_SPACE_32BIT,
ADDR_SPACE_32BIT_NO_RESERVED,
};
}
#pragma pack(push,1)
struct sNpdmHeader
{
private:
byte_t signature_[4]; // be"META"
byte_t reserved_0[8];
byte_t flags_;
byte_t reserved_1;
byte_t main_thread_priority_; // 0-63 inclusive
byte_t main_thread_cpu_id_;
byte_t reserved_2[8];
uint32_t version_;
uint32_t main_thread_stack_size_; // default 4096
byte_t name_[npdm::kNameMaxLen]; // important
byte_t product_code_[npdm::kProductCodeMaxLen]; // can be empty
byte_t reserved_3[48];
// Access Control Info
struct sNpdmSection
{
private:
uint32_t offset_;
uint32_t size_;
public:
uint32_t offset() const { return le_word(offset_); }
void set_offset(uint32_t offset) { offset_ = le_word(offset); }
uint32_t size() const { return le_word(size_); }
void set_size(uint32_t size) { size_ = le_word(size); }
} aci_, acid_;
public:
const char* signature() const { return (const char*)signature_; }
void set_signature(const char* signature) { memcpy(signature_, signature, 4); }
byte_t flags() const { return flags_; }
void set_flags(byte_t flags) { flags_ = flags; }
byte_t main_thread_priority() const { return main_thread_priority_; }
void set_main_thread_priority(byte_t priority) { main_thread_priority_ = priority; }
byte_t main_thread_cpu_id() const { return main_thread_cpu_id_; }
void set_main_thread_cpu_id(byte_t cpu_id) { main_thread_cpu_id_ = cpu_id; }
uint32_t version() const { return le_word(version_); }
void set_version(uint32_t version) { version_ = le_word(version); }
uint32_t main_thread_stack_size() const { return le_word(main_thread_stack_size_); }
void set_main_thread_stack_size(uint32_t size) { main_thread_stack_size_ = le_word(size); }
const char* name() const { return (const char*)name_; }
void set_name(const char* name) { strncpy((char*)name_, name, npdm::kNameMaxLen); }
const char* product_code() const { return (const char*)product_code_; }
void set_product_code(const char* product_code) { strncpy((char*)product_code_, product_code, npdm::kProductCodeMaxLen); }
const sNpdmSection& aci() const { return aci_; }
sNpdmSection& aci() { return aci_; }
const sNpdmSection& acid() const { return acid_; }
sNpdmSection& acid() { return acid_; }
};
#pragma pack(pop)
}

View file

@ -44,8 +44,8 @@ size_t nx::NpdmHeader::getSize() const
void nx::NpdmHeader::calculateOffsets()
{
mAcidPos.offset = align(sizeof(sNpdmHeader), kNpdmAlignSize);
mAciPos.offset = mAcidPos.offset + align(mAcidPos.size, kNpdmAlignSize);
mAcidPos.offset = align(sizeof(sNpdmHeader), npdm::kNpdmAlignSize);
mAciPos.offset = mAcidPos.offset + align(mAcidPos.size, npdm::kNpdmAlignSize);
}
bool nx::NpdmHeader::isEqual(const NpdmHeader & other) const
@ -88,7 +88,7 @@ void nx::NpdmHeader::exportBinary()
mBinaryBlob.alloc(sizeof(sNpdmHeader));
sNpdmHeader* hdr = (sNpdmHeader*)mBinaryBlob.getBytes();
hdr->set_signature(kNpdmStructSig.c_str());
hdr->set_signature(npdm::kNpdmStructSig.c_str());
byte_t flag = ((byte_t)(mInstructionType & 1) | (byte_t)((mProcAddressSpaceType & 3) << 1)) & 0xf;
hdr->set_flags(flag);
hdr->set_main_thread_priority(mMainThreadPriority);
@ -118,24 +118,24 @@ void nx::NpdmHeader::importBinary(const byte_t * bytes, size_t len)
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize());
sNpdmHeader* hdr = (sNpdmHeader*)mBinaryBlob.getBytes();
if (memcmp(kNpdmStructSig.c_str(), hdr->signature(), 4) != 0)
if (memcmp(npdm::kNpdmStructSig.c_str(), hdr->signature(), 4) != 0)
{
throw fnd::Exception(kModuleName, "NPDM header corrupt");
}
byte_t flag = hdr->flags() & 0xf;
mInstructionType = (InstructionType)(flag & 1);
mProcAddressSpaceType = (ProcAddrSpaceType)((flag >> 1) & 3);
mInstructionType = (npdm::InstructionType)(flag & 1);
mProcAddressSpaceType = (npdm::ProcAddrSpaceType)((flag >> 1) & 3);
mMainThreadPriority = hdr->main_thread_priority();
mMainThreadCpuId = hdr->main_thread_cpu_id();
mVersion = hdr->version();
mMainThreadStackSize = hdr->main_thread_stack_size();
mName = std::string(hdr->name(), kNameMaxLen);
mName = std::string(hdr->name(), npdm::kNameMaxLen);
if (mName[0] == '\0')
{
mName.clear();
}
mProductCode = std::string(hdr->product_code(), kProductCodeMaxLen);
mProductCode = std::string(hdr->product_code(), npdm::kProductCodeMaxLen);
if (mProductCode[0] == '\0')
{
mProductCode.clear();
@ -149,8 +149,8 @@ void nx::NpdmHeader::importBinary(const byte_t * bytes, size_t len)
void nx::NpdmHeader::clear()
{
mBinaryBlob.clear();
mInstructionType = INSTR_64BIT;
mProcAddressSpaceType = ADDR_SPACE_64BIT;
mInstructionType = npdm::INSTR_64BIT;
mProcAddressSpaceType = npdm::ADDR_SPACE_64BIT;
mMainThreadPriority = 0;
mMainThreadCpuId = 0;
mVersion = 0;
@ -168,22 +168,22 @@ size_t nx::NpdmHeader::getNpdmSize() const
return MAX(mAcidPos.offset + mAcidPos.size, mAciPos.offset + mAciPos.size);
}
nx::NpdmHeader::InstructionType nx::NpdmHeader::getInstructionType() const
nx::npdm::InstructionType nx::NpdmHeader::getInstructionType() const
{
return mInstructionType;
}
void nx::NpdmHeader::setInstructionType(InstructionType type)
void nx::NpdmHeader::setInstructionType(npdm::InstructionType type)
{
mInstructionType = type;
}
nx::NpdmHeader::ProcAddrSpaceType nx::NpdmHeader::getProcAddressSpaceType() const
nx::npdm::ProcAddrSpaceType nx::NpdmHeader::getProcAddressSpaceType() const
{
return mProcAddressSpaceType;
}
void nx::NpdmHeader::setProcAddressSpaceType(ProcAddrSpaceType type)
void nx::NpdmHeader::setProcAddressSpaceType(npdm::ProcAddrSpaceType type)
{
mProcAddressSpaceType = type;
}
@ -195,7 +195,7 @@ byte_t nx::NpdmHeader::getMainThreadPriority() const
void nx::NpdmHeader::setMainThreadPriority(byte_t priority)
{
if (priority > kMaxPriority)
if (priority > npdm::kMaxPriority)
{
throw fnd::Exception(kModuleName, "Illegal main thread priority (range 0-63)");
}
@ -240,7 +240,7 @@ const std::string & nx::NpdmHeader::getName() const
void nx::NpdmHeader::setName(const std::string & name)
{
if (name.length() > kNameMaxLen)
if (name.length() > npdm::kNameMaxLen)
{
throw fnd::Exception(kModuleName, "Name is too long");
}
@ -255,7 +255,7 @@ const std::string & nx::NpdmHeader::getProductCode() const
void nx::NpdmHeader::setProductCode(const std::string & product_code)
{
if (product_code.length() > kProductCodeMaxLen)
if (product_code.length() > npdm::kProductCodeMaxLen)
{
throw fnd::Exception(kModuleName, "Product Code is too long");
}