117 lines
3.5 KiB
C
Raw Normal View History

#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)
#define NAND_TYPE_O3DS (1UL<<4)
#define NAND_TYPE_N3DS (1UL<<5)
#define NAND_TYPE_NO3DS (1UL<<6)
#define NAND_TYPE_TWL (1UL<<7)
// minimum size of NAND memory
2017-01-02 17:37:08 +01:00
#define NAND_MIN_SECTORS_O3DS 0x1D7800
#define NAND_MIN_SECTORS_N3DS 0x26C000
// start sectors of partitions
#define SECTOR_TWL 0x000000
#define SECTOR_D0K3 0x000001
#define SECTOR_SECRET 0x000096
#define SECTOR_TWLN 0x000097
#define SECTOR_TWLP 0x04808D
#define SECTOR_AGBSAVE 0x058800
#define SECTOR_FIRM0 0x058980
#define SECTOR_FIRM1 0x05A980
#define SECTOR_CTR 0x05C980
// sizes of partitions (in sectors)
#define SIZE_TWL 0x058800
#define SIZE_TWLN 0x047DA9
#define SIZE_TWLP 0x0105B3
#define SIZE_AGBSAVE 0x000180
#define SIZE_FIRM0 0x002000
#define SIZE_FIRM1 0x002000
#define SIZE_CTR_O3DS 0x17AE80
#define SIZE_CTR_N3DS 0x20F680
// filenames for sector 0x96
#define SECTOR_NAME "sector0x96.bin"
#define SECRET_NAME "secret_sector.bin"
#define OTP_NAME "otp.bin"
#define OTP_BIG_NAME "otp0x108.bin"
// 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;
bool InitNandCrypto(void);
bool CheckSlot0x05Crypto(void);
2016-05-15 18:16:45 +02:00
bool CheckSector0x96Crypto(void);
void CryptNand(u8* buffer, u32 sector, u32 count, u32 keyslot);
2016-05-15 18:16:45 +02:00
void CryptSector0x96(u8* buffer, bool encrypt);
int ReadNandBytes(u8* buffer, u64 offset, u64 count, u32 keyslot, u32 nand_src);
int WriteNandBytes(const u8* buffer, u64 offset, u64 count, u32 keyslot, u32 nand_dst);
2016-04-04 22:45:49 +02:00
int ReadNandSectors(u8* buffer, u32 sector, u32 count, u32 keyslot, u32 src);
int WriteNandSectors(const u8* buffer, u32 sector, u32 count, u32 keyslot, u32 dest);
u32 GetNandNcsdMinSizeSectors(NandNcsdHeader* ncsd);
2016-04-04 22:45:49 +02:00
u64 GetNandSizeSectors(u32 src);
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);
u64 GetNandUnusedSectors(u32 src);
u32 CheckNandMbr(u8* mbr);
2017-01-02 17:37:08 +01:00
u32 CheckNandHeader(u8* header);
2016-04-09 21:50:50 +02:00
u32 CheckNandType(u32 src);
u32 GetLegitSector0x96(u8* sector);
u32 GetOtpHash(void* hash);
u32 GetNandCid(void* cid);
bool CheckMultiEmuNand(void);
u32 InitEmuNandBase(bool reset);
u32 GetEmuNandBase(void);