2016-03-03 13:27:51 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2017-02-17 15:54:37 +01:00
|
|
|
#define NAND_SYSNAND (1UL<<0)
|
|
|
|
#define NAND_EMUNAND (1UL<<1)
|
|
|
|
#define NAND_IMGNAND (1UL<<2)
|
|
|
|
#define NAND_ZERONAND (1UL<<3)
|
2016-03-15 16:25:27 +01:00
|
|
|
|
2017-05-31 15:48:14 +02:00
|
|
|
// hardcoded start sectors
|
2017-05-26 00:50:31 +02:00
|
|
|
#define SECTOR_D0K3 0x000001
|
2017-01-05 02:50:41 +01:00
|
|
|
#define SECTOR_SECRET 0x000096
|
|
|
|
|
2017-04-12 00:27:02 +02:00
|
|
|
// filenames for sector 0x96
|
|
|
|
#define SECTOR_NAME "sector0x96.bin"
|
|
|
|
#define SECRET_NAME "secret_sector.bin"
|
2017-05-26 00:50:31 +02:00
|
|
|
|
|
|
|
// 0x110...0x118 in the NAND NCSD header
|
|
|
|
// see: https://www.3dbrew.org/wiki/NCSD#NCSD_header
|
|
|
|
#define NP_TYPE_NONE 0
|
|
|
|
#define NP_TYPE_STD 1
|
|
|
|
#define NP_TYPE_FAT 2 // this is of our own making
|
|
|
|
#define NP_TYPE_FIRM 3
|
|
|
|
#define NP_TYPE_AGB 4
|
|
|
|
#define NP_TYPE_NCSD 5 // this is of our own making
|
|
|
|
#define NP_TYPE_D0K3 6 // my own partition ^_^
|
|
|
|
#define NP_TYPE_SECRET 7 // this is of our own making
|
|
|
|
#define NP_TYPE_BONUS 8 // this is of our own making
|
|
|
|
|
|
|
|
// 0x118...0x120 in the NAND NCSD header
|
|
|
|
// see: https://www.3dbrew.org/wiki/NCSD#NCSD_header
|
|
|
|
#define NP_SUBTYPE_NONE 0
|
|
|
|
#define NP_SUBTYPE_TWL 1
|
|
|
|
#define NP_SUBTYPE_CTR 2
|
|
|
|
#define NP_SUBTYPE_CTR_N 3
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
u32 sector;
|
|
|
|
u32 count;
|
|
|
|
u32 keyslot;
|
|
|
|
} __attribute__((packed)) NandPartitionInfo;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
u32 offset;
|
|
|
|
u32 size;
|
|
|
|
} __attribute__((packed)) NandNcsdPartition;
|
|
|
|
|
|
|
|
// see: https://www.3dbrew.org/wiki/NCSD#NCSD_header
|
|
|
|
typedef struct {
|
|
|
|
u8 signature[0x100];
|
|
|
|
u8 magic[4];
|
|
|
|
u32 size;
|
|
|
|
u64 mediaId; // this is zero
|
|
|
|
u8 partitions_fs_type[8];
|
|
|
|
u8 partitions_crypto_type[8];
|
|
|
|
NandNcsdPartition partitions[8];
|
|
|
|
u8 unknown[0x5E];
|
|
|
|
u8 twl_mbr[0x42];
|
|
|
|
} __attribute__((packed)) NandNcsdHeader;
|
|
|
|
|
2017-04-12 00:27:02 +02:00
|
|
|
|
2016-03-03 13:27:51 +01:00
|
|
|
bool InitNandCrypto(void);
|
2016-03-22 19:24:21 +01:00
|
|
|
bool CheckSlot0x05Crypto(void);
|
2016-05-15 18:16:45 +02:00
|
|
|
bool CheckSector0x96Crypto(void);
|
2016-03-03 13:27:51 +01:00
|
|
|
|
2017-06-01 15:11:41 +02:00
|
|
|
void CryptNand(void* buffer, u32 sector, u32 count, u32 keyslot);
|
|
|
|
void CryptSector0x96(void* buffer, bool encrypt);
|
|
|
|
int ReadNandBytes(void* buffer, u64 offset, u64 count, u32 keyslot, u32 nand_src);
|
|
|
|
int WriteNandBytes(const void* buffer, u64 offset, u64 count, u32 keyslot, u32 nand_dst);
|
|
|
|
int ReadNandSectors(void* buffer, u32 sector, u32 count, u32 keyslot, u32 src);
|
|
|
|
int WriteNandSectors(const void* buffer, u32 sector, u32 count, u32 keyslot, u32 dest);
|
2016-03-03 13:27:51 +01:00
|
|
|
|
2017-05-31 15:48:14 +02:00
|
|
|
u32 ValidateNandNcsdHeader(NandNcsdHeader* header);
|
2017-05-26 00:50:31 +02:00
|
|
|
u32 GetNandNcsdMinSizeSectors(NandNcsdHeader* ncsd);
|
2017-05-31 23:26:45 +02:00
|
|
|
u32 GetNandMinSizeSectors(u32 nand_src);
|
2017-05-31 15:48:14 +02:00
|
|
|
u32 GetNandSizeSectors(u32 src);
|
2017-05-26 00:50:31 +02:00
|
|
|
u32 GetNandNcsdPartitionInfo(NandPartitionInfo* info, u32 type, u32 subtype, u32 index, NandNcsdHeader* ncsd);
|
|
|
|
u32 GetNandPartitionInfo(NandPartitionInfo* info, u32 type, u32 subtype, u32 index, u32 nand_src);
|
2016-03-15 16:25:27 +01:00
|
|
|
|
2017-04-12 00:27:02 +02:00
|
|
|
u32 GetLegitSector0x96(u8* sector);
|
2017-04-19 14:14:02 +02:00
|
|
|
u32 GetOtpHash(void* hash);
|
|
|
|
u32 GetNandCid(void* cid);
|
2017-04-12 00:27:02 +02:00
|
|
|
|
2017-01-17 21:44:16 +01:00
|
|
|
bool CheckMultiEmuNand(void);
|
2017-05-31 23:26:45 +02:00
|
|
|
u32 AutoEmuNandBase(bool reset);
|
2017-01-17 21:44:16 +01:00
|
|
|
u32 GetEmuNandBase(void);
|
2017-05-31 23:26:45 +02:00
|
|
|
u32 SetEmuNandBase(u32 base_sector);
|