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

182 lines
4 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/MemoryBlob.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:
enum FormatVersion
{
NCA2_FORMAT,
NCA3_FORMAT
};
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,
};
2018-03-18 09:08:42 +00:00
enum KeyBankIndex
2017-07-18 14:17:32 +00:00
{
2018-03-18 09:08:42 +00:00
KEY_AESXTS_0,
KEY_AESXTS_1,
KEY_AESCTR,
2017-07-18 14:17:32 +00:00
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;
crypto::sha::sSha256Hash hash;
const sSection& operator=(const sSection& other)
{
offset = other.offset;
size = other.size;
hash = other.hash;
return *this;
}
bool operator==(const sSection& other) const
{
return (offset == other.offset) \
&& (size == other.size) \
&& (hash == other.hash);
}
bool operator!=(const sSection& other) const
{
2018-03-21 12:29:08 +00:00
return !operator==(other);
2017-07-06 11:17:21 +00:00
}
};
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();
2018-03-18 09:08:42 +00:00
FormatVersion getFormatVersion() const;
void setFormatVersion(FormatVersion ver);
2017-07-18 14:17:32 +00:00
DistributionType getDistributionType() const;
void setDistributionType(DistributionType type);
ContentType getContentType() const;
void setContentType(ContentType type);
2018-03-18 09:08:42 +00:00
byte_t getCryptoType() const;
void setCryptoType(byte_t type);
byte_t getKaekIndex() const;
void setKaekIndex(byte_t 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";
2018-03-18 09:08:42 +00:00
const std::string kNca2Sig = "NCA2";
const std::string kNca3Sig = "NCA3";
2017-07-06 11:17:21 +00:00
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
{
char signature[4];
byte_t distribution_type;
byte_t content_type;
2018-03-18 09:08:42 +00:00
byte_t crypto_type; // KeyGeneration
byte_t key_area_encryption_key_index;
le_uint64_t nca_size;
le_uint64_t program_id;
le_uint32_t content_index;
le_uint32_t sdk_addon_version;
2018-03-18 09:08:42 +00:00
byte_t crypto_type_2;
byte_t reserved_2[0xf];
byte_t rights_id[0x10];
2017-07-06 11:17:21 +00:00
struct sNcaSection
{
le_uint32_t start; // block units
le_uint32_t end; // block units
byte_t enabled;
byte_t reserved[7];
} section[kSectionNum];
crypto::sha::sSha256Hash section_hash[kSectionNum];
crypto::aes::sAes128Key enc_aes_key[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
2018-03-18 09:08:42 +00:00
FormatVersion mFormatVersion;
2017-07-18 14:17:32 +00:00
DistributionType mDistributionType;
ContentType mContentType;
2018-03-18 09:08:42 +00:00
byte_t mCryptoType;
byte_t mKaekIndex;
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
}