nstool/lib/nx/NcaHeader.h

226 lines
6 KiB
C
Raw Normal View History

2017-07-05 08:57:14 +00:00
#pragma once
#include <string>
2017-07-05 08:57:14 +00:00
#include <fnd/types.h>
#include <fnd/memory_blob.h>
#include <fnd/List.h>
2017-07-05 08:57:14 +00:00
#include <crypto/aes.h>
#include <crypto/sha.h>
#include <fnd/ISerialiseableBinary.h>
2017-07-05 08:57:14 +00:00
2017-07-06 11:17:21 +00:00
namespace nx
2017-07-05 08:57:14 +00:00
{
class NcaHeader :
public fnd::ISerialiseableBinary
2017-07-05 08:57:14 +00:00
{
2017-07-06 11:17:21 +00:00
public:
2017-07-18 14:17:32 +00:00
enum DistributionType
{
DIST_DOWNLOAD,
DIST_GAME_CARD
};
enum ContentType
{
TYPE_PROGRAM,
TYPE_META,
TYPE_CONTROL,
TYPE_MANUAL,
TYPE_DATA,
};
enum EncryptionType
{
CRYPT_AUTO,
CRYPT_NONE,
CRYPT_AESCTR = 3
};
enum EncryptionKeyIndex
{
KEY_UNUSED_0,
KEY_UNUSED_1,
KEY_DEFAULT,
KEY_UNUSED_3,
};
2017-07-06 11:17:21 +00:00
struct sSection
{
2017-07-06 11:17:21 +00:00
u64 offset;
u64 size;
2017-07-18 14:17:32 +00:00
EncryptionType enc_type;
2017-07-06 11:17:21 +00:00
crypto::sha::sSha256Hash hash;
const sSection& operator=(const sSection& other)
{
offset = other.offset;
size = other.size;
2017-07-18 14:17:32 +00:00
enc_type = other.enc_type;
2017-07-06 11:17:21 +00:00
hash = other.hash;
return *this;
}
bool operator==(const sSection& other) const
{
return (offset == other.offset) \
&& (size == other.size) \
2017-07-18 14:17:32 +00:00
&& (enc_type == other.enc_type) \
2017-07-06 11:17:21 +00:00
&& (hash == other.hash);
}
bool operator!=(const sSection& other) const
{
return operator==(other);
}
};
2017-07-18 14:17:32 +00:00
static const size_t kBlockSize = 0x200;
2017-07-06 11:17:21 +00:00
NcaHeader();
NcaHeader(const NcaHeader& other);
NcaHeader(const u8* bytes, size_t len);
2017-07-06 11:17:21 +00:00
bool operator==(const NcaHeader& other) const;
bool operator!=(const NcaHeader& other) const;
void operator=(const NcaHeader& other);
// to be used after export
const u8* getBytes() const;
size_t getSize() const;
// export/import binary
void exportBinary();
void importBinary(const u8* bytes, size_t len);
// variables
void clear();
2017-07-18 14:17:32 +00:00
DistributionType getDistributionType() const;
void setDistributionType(DistributionType type);
ContentType getContentType() const;
void setContentType(ContentType type);
EncryptionType getEncryptionType() const;
void setEncryptionType(EncryptionType type);
EncryptionKeyIndex getKeyIndex() const;
void setKeyIndex(EncryptionKeyIndex index);
2017-07-06 11:17:21 +00:00
u64 getNcaSize() const;
void setNcaSize(u64 size);
u64 getProgramId() const;
void setProgramId(u64 program_id);
2017-07-18 14:17:32 +00:00
u32 getContentIndex() const;
void setContentIndex(u32 index);
u32 getSdkAddonVersion() const;
void setSdkAddonVersion(u32 version);
2017-07-06 11:17:21 +00:00
const fnd::List<sSection>& getSections() const;
void addSection(const sSection& section);
2017-07-18 14:17:32 +00:00
const fnd::List<crypto::aes::sAes128Key>& getEncAesKeys() const;
void addEncAesKey(const crypto::aes::sAes128Key& key);
2017-07-05 08:57:14 +00:00
2017-07-06 11:17:21 +00:00
private:
const std::string kModuleName = "NCA_HEADER";
const std::string kNcaSig = "NCA2";
static const size_t kSectionNum = 4;
static const size_t kAesKeyNum = 4;
2017-07-18 14:17:32 +00:00
static const u32 kDefaultSdkAddonVersion = 721920;
enum ProgramPartitionId
{
SECTION_CODE,
SECTION_DATA,
SECTION_LOGO,
};
2017-07-05 08:57:14 +00:00
#pragma pack (push, 1)
2017-07-06 11:17:21 +00:00
struct sNcaHeader
2017-07-05 08:57:14 +00:00
{
private:
2017-07-06 11:17:21 +00:00
u8 signature_[4];
2017-07-18 14:17:32 +00:00
u8 distribution_type_;
u8 content_type_;
u8 key_generation_;
u8 key_area_encryption_key_index_;
2017-07-06 11:17:21 +00:00
u64 nca_size_;
u64 program_id_;
2017-07-18 14:17:32 +00:00
u32 content_index_;
u32 sdk_addon_version_;
2017-07-06 11:17:21 +00:00
u8 reserved_2[0x20];
struct sNcaSection
{
private:
u32 start_; // block units
u32 end_; // block units
2017-07-18 14:17:32 +00:00
u8 enabled_;
2017-07-06 11:17:21 +00:00
u8 reserved[7];
public:
u32 start() const { return le_word(start_); }
void set_start(u32 offset) { start_ = le_word(offset); }
u32 end() const { return le_word(end_); }
void set_end(u32 offset) { end_ = le_word(offset); }
2017-07-18 14:17:32 +00:00
u8 enabled() const { return enabled_; }
void set_enabled(u8 is_enabled) { enabled_ = is_enabled; }
2017-07-06 11:17:21 +00:00
} section_[kSectionNum];
crypto::sha::sSha256Hash section_hash_[kSectionNum];
2017-07-18 14:17:32 +00:00
crypto::aes::sAes128Key enc_aes_keys_[kAesKeyNum];
2017-07-05 08:57:14 +00:00
public:
2017-07-06 11:17:21 +00:00
const char* signature() const { return (const char*)signature_; }
void set_signature(const char* signature) { memcpy(signature_, signature, 4); }
2017-07-05 08:57:14 +00:00
2017-07-18 14:17:32 +00:00
u8 distribution_type() const { return distribution_type_; }
void set_distribution_type(u8 type) { distribution_type_ = type; }
u8 content_type() const { return content_type_; }
void set_content_type(u8 type) { content_type_ = type; }
u8 key_generation() const { return key_generation_; }
void set_key_generation(u8 type) { key_generation_ = type; }
u8 key_area_encryption_key_index() const { return key_area_encryption_key_index_; }
void set_key_area_encryption_key_index(u8 index) { key_area_encryption_key_index_ = index; }
2017-07-05 08:57:14 +00:00
2017-07-06 11:17:21 +00:00
u64 nca_size() const { return le_dword(nca_size_); }
void set_nca_size(u64 nca_size) { nca_size_ = le_dword(nca_size); }
2017-07-05 08:57:14 +00:00
2017-07-06 11:17:21 +00:00
u64 program_id() const { return le_dword(program_id_); }
void set_program_id(u64 program_id) { program_id_ = le_dword(program_id); }
2017-07-05 08:57:14 +00:00
2017-07-18 14:17:32 +00:00
u32 content_index() const { return le_word(content_index_); }
void set_content_index(u32 index) { content_index_ = le_word(index); }
u32 sdk_addon_version() const { return le_word(sdk_addon_version_); }
void set_sdk_addon_version(u32 version) { sdk_addon_version_ = le_word(version); }
2017-07-05 08:57:14 +00:00
2017-07-06 11:17:21 +00:00
const sNcaSection& section(u8 index) const { return section_[index%kSectionNum]; }
sNcaSection& section(u8 index) { return section_[index%kSectionNum]; }
2017-07-05 08:57:14 +00:00
2017-07-06 11:17:21 +00:00
const crypto::sha::sSha256Hash& section_hash(u8 index) const { return section_hash_[index%kSectionNum]; }
crypto::sha::sSha256Hash& section_hash(u8 index) { return section_hash_[index%kSectionNum]; }
2017-07-05 08:57:14 +00:00
2017-07-18 14:17:32 +00:00
const crypto::aes::sAes128Key& enc_aes_key(u8 index) const { return enc_aes_keys_[index%kAesKeyNum]; }
crypto::aes::sAes128Key& enc_aes_key(u8 index) { return enc_aes_keys_[index%kAesKeyNum]; }
2017-07-06 11:17:21 +00:00
};
#pragma pack (pop)
2017-07-05 08:57:14 +00:00
2017-07-06 11:17:21 +00:00
// binary
fnd::MemoryBlob mBinaryBlob;
// data
2017-07-18 14:17:32 +00:00
DistributionType mDistributionType;
ContentType mContentType;
EncryptionType mEncryptionType;
EncryptionKeyIndex mKeyIndex;
2017-07-06 11:17:21 +00:00
u64 mNcaSize;
u64 mProgramId;
2017-07-18 14:17:32 +00:00
u32 mContentIndex;
u32 mSdkAddonVersion;
2017-07-06 11:17:21 +00:00
fnd::List<sSection> mSections;
2017-07-18 14:17:32 +00:00
fnd::List<crypto::aes::sAes128Key> mEncAesKeys;
2017-07-06 11:17:21 +00:00
u64 blockNumToSize(u32 block_num) const;
u32 sizeToBlockNum(u64 real_size) const;
bool isEqual(const NcaHeader& other) const;
void copyFrom(const NcaHeader& other);
2017-07-05 08:57:14 +00:00
};
2017-07-06 11:17:21 +00:00
}