nstool/lib/libnx/include/nx/NpdmHeader.h

190 lines
5 KiB
C
Raw Normal View History

2017-07-07 00:18:33 +00:00
#pragma once
#include <string>
#include <fnd/types.h>
#include <fnd/MemoryBlob.h>
#include <fnd/ISerialiseableBinary.h>
2017-07-07 00:18:33 +00:00
namespace nx
{
class NpdmHeader :
public fnd::ISerialiseableBinary
2017-07-07 00:18:33 +00:00
{
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;
size_t size;
void operator=(const sSection& other)
{
offset = other.offset;
size = other.size;
}
bool operator==(const sSection& other) const
{
return (offset == other.offset) \
&& (size == other.size);
}
bool operator!=(const sSection& other) const
{
return !operator==(other);
}
};
NpdmHeader();
NpdmHeader(const NpdmHeader& other);
2018-03-22 05:26:22 +00:00
NpdmHeader(const byte_t* bytes, size_t len);
2017-07-07 00:18:33 +00:00
bool operator==(const NpdmHeader& other) const;
bool operator!=(const NpdmHeader& other) const;
void operator=(const NpdmHeader& other);
// to be used after export
2018-03-22 05:26:22 +00:00
const byte_t* getBytes() const;
2017-07-07 00:18:33 +00:00
size_t getSize() const;
// export/import binary
void exportBinary();
2018-03-22 05:26:22 +00:00
void importBinary(const byte_t* bytes, size_t len);
2017-07-07 00:18:33 +00:00
// variables
void clear();
2017-07-07 00:18:33 +00:00
size_t getNpdmSize() const;
InstructionType getInstructionType() const;
void setInstructionType(InstructionType type);
ProcAddrSpaceType getProcAddressSpaceType() const;
void setProcAddressSpaceType(ProcAddrSpaceType type);
2018-03-22 05:26:22 +00:00
byte_t getMainThreadPriority() const;
void setMainThreadPriority(byte_t priority);
2017-07-07 00:18:33 +00:00
2018-03-22 05:26:22 +00:00
byte_t getMainThreadCpuId() const;
void setMainThreadCpuId(byte_t cpu_id);
2017-07-07 00:18:33 +00:00
2018-03-22 05:26:22 +00:00
uint32_t getVersion() const;
void setVersion(uint32_t version);
2017-07-07 00:18:33 +00:00
2018-03-22 05:26:22 +00:00
uint32_t getMainThreadStackSize() const;
void setMainThreadStackSize(uint32_t size);
2017-07-07 00:18:33 +00:00
const std::string& getName() const;
void setName(const std::string& name);
const std::string& getProductCode() const;
void setProductCode(const std::string& product_code);
const sSection& getAciPos() const;
void setAciSize(size_t size);
2017-07-07 00:18:33 +00:00
const sSection& getAcidPos() const;
void setAcidSize(size_t size);
2017-07-07 00:18:33 +00:00
private:
const std::string kModuleName = "NPDM_HEADER";
const std::string kNpdmStructSig = "META";
static const size_t kNameMaxLen = 0x10;
static const size_t kProductCodeMaxLen = 0x10;
2018-03-22 05:26:22 +00:00
static const uint32_t kMaxPriority = BIT(6) -1 ;
2017-07-07 00:18:33 +00:00
static const size_t kNpdmAlignSize = 0x10;
#pragma pack (push, 1)
struct sNpdmHeader
{
private:
2018-03-22 05:26:22 +00:00
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];
2017-07-07 00:18:33 +00:00
// Access Control Info
struct sNpdmSection
{
private:
2018-03-22 05:26:22 +00:00
uint32_t offset_;
uint32_t size_;
2017-07-07 00:18:33 +00:00
public:
2018-03-22 05:26:22 +00:00
uint32_t offset() const { return le_word(offset_); }
void set_offset(uint32_t offset) { offset_ = le_word(offset); }
2017-07-07 00:18:33 +00:00
2018-03-22 05:26:22 +00:00
uint32_t size() const { return le_word(size_); }
void set_size(uint32_t size) { size_ = le_word(size); }
2017-07-07 00:18:33 +00:00
} aci_, acid_;
public:
const char* signature() const { return (const char*)signature_; }
void set_signature(const char* signature) { memcpy(signature_, signature, 4); }
2018-03-22 05:26:22 +00:00
byte_t flags() const { return flags_; }
void set_flags(byte_t flags) { flags_ = flags; }
2017-07-07 00:18:33 +00:00
2018-03-22 05:26:22 +00:00
byte_t main_thread_priority() const { return main_thread_priority_; }
void set_main_thread_priority(byte_t priority) { main_thread_priority_ = priority; }
2017-07-07 00:18:33 +00:00
2018-03-22 05:26:22 +00:00
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; }
2017-07-07 00:18:33 +00:00
2018-03-22 05:26:22 +00:00
uint32_t version() const { return le_word(version_); }
void set_version(uint32_t version) { version_ = le_word(version); }
2017-07-07 00:18:33 +00:00
2018-03-22 05:26:22 +00:00
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); }
2017-07-07 00:18:33 +00:00
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;
2018-03-22 05:26:22 +00:00
byte_t mMainThreadPriority;
byte_t mMainThreadCpuId;
uint32_t mVersion;
uint32_t mMainThreadStackSize;
2017-07-07 00:18:33 +00:00
std::string mName;
std::string mProductCode;
sSection mAciPos;
sSection mAcidPos;
void calculateOffsets();
bool isEqual(const NpdmHeader& other) const;
void copyFrom(const NpdmHeader& other);
};
}