libctr9
Nintendo 3DS ARM9 library
protocol_ntr.h
1 // Copyright 2014 Normmatt
2 // Licensed under GPLv2 or any later version
3 // Refer to the license.txt file included.
4 
5 #pragma once
6 
7 #include <stdint.h>
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 #define REG_NTRCARDMCNT (*(volatile uint16_t*)0x10164000)
14 #define REG_NTRCARDMDATA (*(volatile uint16_t*)0x10164002)
15 #define REG_NTRCARDROMCNT (*(volatile uint32_t*)0x10164004)
16 #define REG_NTRCARDCMD ((volatile uint8_t*)0x10164008)
17 #define REG_NTRCARDSEEDX_L (*(volatile uint32_t*)0x10164010)
18 #define REG_NTRCARDSEEDY_L (*(volatile uint32_t*)0x10164014)
19 #define REG_NTRCARDSEEDX_H (*(volatile uint16_t*)0x10164018)
20 #define REG_NTRCARDSEEDY_H (*(volatile uint16_t*)0x1016401A)
21 #define REG_NTRCARDFIFO (*(volatile uint32_t*)0x1016401C)
22 
23 #define NTRCARD_PAGESIZE_0 (0<<24)
24 #define NTRCARD_PAGESIZE_4 (7u<<24)
25 #define NTRCARD_PAGESIZE_512 (1u<<24)
26 #define NTRCARD_PAGESIZE_1K (2u<<24)
27 #define NTRCARD_PAGESIZE_2K (3u<<24)
28 #define NTRCARD_PAGESIZE_4K (4u<<24)
29 #define NTRCARD_PAGESIZE_8K (5u<<24)
30 #define NTRCARD_PAGESIZE_16K (6u<<24)
31 
32 #define NTRCARD_ACTIVATE (1u<<31) // when writing, get the ball rolling
33 #define NTRCARD_WR (1u<<30) // Card write enable
34 #define NTRCARD_nRESET (1u<<29) // value on the /reset pin (1 = high out, not a reset state, 0 = low out = in reset)
35 #define NTRCARD_SEC_LARGE (1u<<28) // Use "other" secure area mode, which tranfers blocks of 0x1000 bytes at a time
36 #define NTRCARD_CLK_SLOW (1u<<27) // Transfer clock rate (0 = 6.7MHz, 1 = 4.2MHz)
37 #define NTRCARD_BLK_SIZE(n) (((n)&0x7u)<<24) // Transfer block size, (0 = None, 1..6 = (0x100 << n) bytes, 7 = 4 bytes)
38 #define NTRCARD_SEC_CMD (1u<<22) // The command transfer will be hardware encrypted (KEY2)
39 #define NTRCARD_DELAY2(n) (((n)&0x3Fu)<<16) // Transfer delay length part 2
40 #define NTRCARD_SEC_SEED (1u<<15) // Apply encryption (KEY2) seed to hardware registers
41 #define NTRCARD_SEC_EN (1u<<14) // Security enable
42 #define NTRCARD_SEC_DAT (1u<<13) // The data transfer will be hardware encrypted (KEY2)
43 #define NTRCARD_DELAY1(n) ((n)&0x1FFFu) // Transfer delay length part 1
44 
45 // 3 bits in b10..b8 indicate something
46 // read bits
47 #define NTRCARD_BUSY (1u<<31) // when reading, still expecting incomming data?
48 #define NTRCARD_DATA_READY (1u<<23) // when reading, REG_NTRCARDFIFO has another word of data and is good to go
49 
50 // Card commands
51 #define NTRCARD_CMD_DUMMY 0x9Fu
52 #define NTRCARD_CMD_HEADER_READ 0x00u
53 #define NTRCARD_CMD_HEADER_CHIPID 0x90u
54 #define NTRCARD_CMD_ACTIVATE_BF 0x3Cu // Go into blowfish (KEY1) encryption mode
55 #define NTRCARD_CMD_ACTIVATE_SEC 0x40u // Go into hardware (KEY2) encryption mode
56 #define NTRCARD_CMD_SECURE_CHIPID 0x10u
57 #define NTRCARD_CMD_SECURE_READ 0x20u
58 #define NTRCARD_CMD_DISABLE_SEC 0x60u // Leave hardware (KEY2) encryption mode
59 #define NTRCARD_CMD_DATA_MODE 0xA0u
60 #define NTRCARD_CMD_DATA_READ 0xB7u
61 #define NTRCARD_CMD_DATA_CHIPID 0xB8u
62 
63 #define NTRCARD_CR1_ENABLE 0x8000u
64 #define NTRCARD_CR1_IRQ 0x4000u
65 
66 #define NTRKEY_PARAM 0x3F1FFFu
67 
68 void NTR_SendCommand(const uint32_t command[2], uint32_t pageSize, uint32_t latency, void* buffer);
69 
70 #ifdef __cplusplus
71 }
72 #endif
73