7612bf90 |
1 | |
2 | #include "3ds_utils.h" |
3 | |
61d76999 |
4 | #define GET_VERSION_MAJOR(version) ((version) >>24) |
5 | |
7612bf90 |
6 | typedef int (*ctr_callback_type)(void); |
7 | |
8 | int srvGetServiceHandle(unsigned int* out, const char* name); |
9 | int svcCloseHandle(unsigned int handle); |
10 | int svcBackdoor(ctr_callback_type); |
61d76999 |
11 | int32_t svcGetSystemInfo(int64_t* out, uint32_t type, int32_t param); |
12 | void ctr_clear_cache(void); |
13 | |
14 | static int has_rosalina; |
15 | |
16 | void check_rosalina(void) { |
17 | int64_t version; |
18 | uint32_t major; |
19 | |
20 | has_rosalina = 0; |
7612bf90 |
21 | |
61d76999 |
22 | if (!svcGetSystemInfo(&version, 0x10000, 0)) { |
23 | major = GET_VERSION_MAJOR(version); |
24 | |
25 | if (major >= 8) |
26 | has_rosalina = 1; |
27 | } |
28 | } |
7612bf90 |
29 | |
30 | static void ctr_enable_all_svc_kernel(void) |
31 | { |
32 | __asm__ volatile("cpsid aif"); |
33 | |
34 | unsigned int* svc_access_control = *(*(unsigned int***)0xFFFF9000 + 0x22) - 0x6; |
35 | |
36 | svc_access_control[0]=0xFFFFFFFE; |
37 | svc_access_control[1]=0xFFFFFFFF; |
38 | svc_access_control[2]=0xFFFFFFFF; |
39 | svc_access_control[3]=0x3FFFFFFF; |
40 | } |
41 | |
7612bf90 |
42 | static void ctr_enable_all_svc(void) |
43 | { |
44 | svcBackdoor((ctr_callback_type)ctr_enable_all_svc_kernel); |
45 | } |
46 | |
fd587b67 |
47 | static void ctr_clean_invalidate_kernel(void) |
7612bf90 |
48 | { |
fd587b67 |
49 | __asm__ volatile( |
50 | "mrs r1, cpsr\n" |
51 | "cpsid aif\n" // disable interrupts |
52 | "mov r0, #0\n" |
53 | "mcr p15, 0, r0, c7, c10, 0\n" // clean dcache |
54 | "mcr p15, 0, r0, c7, c10, 4\n" // DSB |
55 | "mcr p15, 0, r0, c7, c5, 0\n" // invalidate icache+BTAC |
56 | "msr cpsr_cx, r1\n" // restore interrupts |
57 | ::: "r0", "r1"); |
7612bf90 |
58 | } |
59 | |
7612bf90 |
60 | void ctr_flush_invalidate_cache(void) |
61 | { |
61d76999 |
62 | if (has_rosalina) { |
63 | ctr_clear_cache(); |
64 | } else { |
65 | // __asm__ volatile("svc 0x2E\n\t"); |
66 | // __asm__ volatile("svc 0x4B\n\t"); |
67 | svcBackdoor((ctr_callback_type)ctr_clean_invalidate_kernel); |
68 | } |
7612bf90 |
69 | } |
70 | |
71 | int ctr_svchack_init(void) |
72 | { |
61d76999 |
73 | extern unsigned int __ctr_svchax; |
7612bf90 |
74 | extern unsigned int __service_ptr; |
75 | |
61d76999 |
76 | if(__ctr_svchax) |
77 | return 1; /* All services have already been enabled */ |
78 | |
7612bf90 |
79 | if(__service_ptr) |
80 | return 0; |
81 | |
82 | /* CFW */ |
83 | ctr_enable_all_svc(); |
84 | return 1; |
85 | } |
86 | |